2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file src/transport/gnunet-nat-server.c
23 * @brief Tool to help bypass NATs using ICMP method; must run as root (SUID will do)
24 * This code will work under GNU/Linux only (or maybe BSDs, but never W32)
25 * @author Christian Grothoff
27 * This program will send ONE ICMP message every 500 ms RAW sockets
28 * to a DUMMY IP address and also listens for ICMP replies. Since
29 * it uses RAW sockets, it must be installed SUID or run as 'root'.
30 * In order to keep the security risk of the resulting SUID binary
31 * minimal, the program ONLY opens the two RAW sockets with root
32 * privileges, then drops them and only then starts to process
33 * command line arguments. The code also does not link against
34 * any shared libraries (except libc) and is strictly minimal
35 * (except for checking for errors). The following list of people
36 * have reviewed this code and considered it safe since the last
37 * modification (if you reviewed it, please have your name added
40 * - Christian Grothoff
42 * - Benjamin Kuperman (22 Aug 2010)
45 /* Just needed for HAVE_SOCKADDR_IN_SIN_LEN test macro! */
46 #include "gnunet_config.h"
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <arpa/inet.h>
53 #include <sys/select.h>
55 #include <sys/types.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_icmp.h>
65 #include <netinet/in.h>
68 * Should we print some debug output?
73 * Must match IP given in the client.
75 #define DUMMY_IP "192.0.2.86"
80 #define NAT_TRAV_PORT 22225
83 * How often do we send our ICMP messages to receive replies?
85 #define ICMP_SEND_FREQUENCY_MS 500
94 * Version (4 bits) + Internet header length (4 bits)
114 * Flags (3 bits) + Fragment offset (13 bits)
116 uint16_t flags_frag_offset;
139 * Destination address
145 * Format of ICMP packet.
147 struct icmp_ttl_exceeded_header
157 /* followed by original payload */
160 struct icmp_echo_header
173 * Beginning of UDP packet.
187 * Socket we use to receive "fake" ICMP replies.
192 * Socket we use to send our ICMP requests.
197 * Socket we use to send our UDP requests.
202 * Target "dummy" address.
204 static struct in_addr dummy;
208 * CRC-16 for IP/ICMP headers.
210 * @param data what to calculate the CRC over
211 * @param bytes number of bytes in data (must be multiple of 2)
212 * @return the CRC 16.
215 calc_checksum(const uint16_t *data,
222 for (i=0;i<bytes/2;i++)
224 sum = (sum & 0xffff) + (sum >> 16);
225 sum = htons(0xffff - sum);
231 * Send an ICMP message to the dummy IP.
233 * @param my_ip source address (our ip address)
236 send_icmp_echo (const struct in_addr *my_ip)
238 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
239 struct icmp_echo_header icmp_echo;
240 struct ip_header ip_pkt;
241 struct sockaddr_in dst;
246 ip_pkt.vers_ihl = 0x45;
248 ip_pkt.pkt_len = htons (sizeof (packet));
249 ip_pkt.id = htons (256);
250 ip_pkt.flags_frag_offset = 0;
251 ip_pkt.ttl = IPDEFTTL;
252 ip_pkt.proto = IPPROTO_ICMP;
254 ip_pkt.src_ip = my_ip->s_addr;
255 ip_pkt.dst_ip = dummy.s_addr;
256 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
257 sizeof (struct ip_header)));
258 memcpy (&packet[off],
260 sizeof (struct ip_header));
261 off += sizeof (struct ip_header);
263 icmp_echo.type = ICMP_ECHO;
265 icmp_echo.checksum = 0;
266 icmp_echo.reserved = 0;
267 icmp_echo.checksum = htons(calc_checksum((uint16_t*)&icmp_echo,
268 sizeof (struct icmp_echo_header)));
269 memcpy (&packet[off],
271 sizeof (struct icmp_echo_header));
272 off += sizeof (struct icmp_echo_header);
274 memset (&dst, 0, sizeof (dst));
275 dst.sin_family = AF_INET;
276 #if HAVE_SOCKADDR_IN_SIN_LEN
277 dst.sin_len = sizeof (struct sockaddr_in);
279 dst.sin_addr = dummy;
280 err = sendto(rawsock,
282 (struct sockaddr*)&dst,
288 "sendto failed: %s\n", strerror(errno));
291 else if (sizeof (packet) != err)
294 "Error: partial send of ICMP message\n");
300 * Send a UDP message to the dummy IP.
305 struct sockaddr_in dst;
308 memset (&dst, 0, sizeof (dst));
309 dst.sin_family = AF_INET;
310 #if HAVE_SOCKADDR_IN_SIN_LEN
311 dst.sin_len = sizeof (struct sockaddr_in);
313 dst.sin_addr = dummy;
314 dst.sin_port = htons (NAT_TRAV_PORT);
315 err = sendto(udpsock,
317 (struct sockaddr*)&dst,
323 "sendto failed: %s\n", strerror(errno));
329 "Error: partial send of ICMP message\n");
335 * We've received an ICMP response. Process it.
338 process_icmp_response ()
342 struct in_addr source_ip;
343 struct ip_header ip_pkt;
344 struct icmp_ttl_exceeded_header icmp_ttl;
345 struct icmp_echo_header icmp_echo;
346 struct udp_header udp_pkt;
350 have = read (icmpsock, buf, sizeof (buf));
354 "Error reading raw socket: %s\n",
360 "Received message of %u bytes\n",
361 (unsigned int) have);
363 if (have < (ssize_t) (sizeof (struct ip_header) + sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct ip_header)))
371 sizeof (struct ip_header));
372 off += sizeof (struct ip_header);
378 sizeof (struct icmp_ttl_exceeded_header));
379 off += sizeof (struct icmp_ttl_exceeded_header);
380 if ( (ICMP_TIME_EXCEEDED != icmp_ttl.type) ||
381 (0 != icmp_ttl.code) )
383 /* different type than what we want */
386 /* skip 2nd IP header */
389 sizeof (struct ip_header));
390 off += sizeof (struct ip_header);
392 switch (ip_pkt.proto)
395 if (have != (sizeof (struct ip_header) * 2 +
396 sizeof (struct icmp_ttl_exceeded_header) +
397 sizeof (struct icmp_echo_header)) )
402 /* grab ICMP ECHO content */
405 sizeof (struct icmp_echo_header));
406 port = (uint16_t) ntohl (icmp_echo.reserved);
409 if (have != (sizeof (struct ip_header) * 2 +
410 sizeof (struct icmp_ttl_exceeded_header) +
411 sizeof (struct udp_header)) )
416 /* grab UDP content */
419 sizeof (struct udp_header));
420 port = ntohs (udp_pkt.length);
423 /* different type than what we want */
441 (unsigned int) port);
447 * Create an ICMP raw socket for reading.
449 * @return -1 on error
456 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
460 "Error opening RAW socket: %s\n",
464 if (ret >= FD_SETSIZE)
467 "Socket number too large (%d > %u)\n",
469 (unsigned int) FD_SETSIZE);
478 * Create an ICMP raw socket for writing.
480 * @return -1 on error
488 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
492 "Error opening RAW socket: %s\n",
496 if (-1 == setsockopt(ret,
499 (char *)&one, sizeof(one)))
502 "setsockopt failed: %s\n",
507 if (-1 == setsockopt(ret,
510 (char *)&one, sizeof(one)))
513 "setsockopt failed: %s\n",
523 * Create a UDP socket for writinging.
525 * @param my_ip source address (our ip address)
526 * @return -1 on error
529 make_udp_socket (const struct in_addr *my_ip)
532 struct sockaddr_in addr;
534 ret = socket (AF_INET, SOCK_DGRAM, 0);
538 "Error opening UDP socket: %s\n",
545 addr.sin_family = AF_INET;
546 #if HAVE_SOCKADDR_IN_SIN_LEN
547 addr.sin_len = sizeof (struct sockaddr_in);
549 addr.sin_addr = *my_ip;
550 addr.sin_port = htons (NAT_TRAV_PORT);
557 "Error binding UDP socket to port %u: %s\n",
560 /* likely problematic, but not certain, try to continue */
570 struct in_addr external;
579 "This program must be started with our (internal NAT) IP as the only argument.\n");
582 if (1 != inet_pton (AF_INET, argv[1], &external))
585 "Error parsing IPv4 address: %s\n",
589 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
592 "Internal error converting dummy IP to binary.\n");
595 if (-1 == (icmpsock = make_icmp_socket()))
599 if (-1 == (rawsock = make_raw_socket()))
605 if (0 != setresuid (uid, uid, uid))
608 "Failed to setresuid: %s\n",
610 /* not critical, continue anyway */
612 if (-1 == (udpsock = make_udp_socket(&external)))
622 FD_SET (icmpsock, &rs);
624 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
625 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
630 "select failed: %s\n",
634 if (FD_ISSET (icmpsock, &rs))
635 process_icmp_response ();
636 if (0 == (++alt % 2))
637 send_icmp_echo (&external);
641 /* select failed (internal error or OS out of resources) */
649 /* end of gnunet-nat-server.c */