2 This file is part of GNUnet.
3 Copyright (C) 2010 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file src/nat/gnunet-helper-nat-server.c
21 * @brief Tool to help bypass NATs using ICMP method; must run as root (SUID will do)
22 * This code will work under GNU/Linux only (or maybe BSDs, but never W32)
23 * @author Christian Grothoff
25 * This program will send ONE ICMP message every 500 ms RAW sockets
26 * to a DUMMY IP address and also listens for ICMP replies. Since
27 * it uses RAW sockets, it must be installed SUID or run as 'root'.
28 * In order to keep the security risk of the resulting SUID binary
29 * minimal, the program ONLY opens the two RAW sockets with root
30 * privileges, then drops them and only then starts to process
31 * command line arguments. The code also does not link against
32 * any shared libraries (except libc) and is strictly minimal
33 * (except for checking for errors). The following list of people
34 * have reviewed this code and considered it safe since the last
35 * modification (if you reviewed it, please have your name added
38 * - Christian Grothoff
40 * - Benjamin Kuperman (22 Aug 2010)
41 * - Jacob Appelbaum (19 Dec 2011)
44 /* Just needed for HAVE_SOCKADDR_IN_SIN_LEN test macro! */
45 #include "gnunet_config.h"
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <arpa/inet.h>
52 #include <sys/select.h>
54 #include <sys/types.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_icmp.h>
64 #include <netinet/in.h>
66 /* The following constant is missing from FreeBSD 9.2 */
67 #ifndef ICMP_TIME_EXCEEDED
68 #define ICMP_TIME_EXCEEDED 11
72 * Call memcpy() but check for @a n being 0 first. In the latter
73 * case, it is now safe to pass NULL for @a src or @a dst.
74 * Unlike traditional memcpy(), returns nothing.
76 * @param dst destination of the copy, may be NULL if @a n is zero
77 * @param src source of the copy, may be NULL if @a n is zero
78 * @param n number of bytes to copy
80 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
83 * Should we print some debug output?
88 * Must match packet ID used by gnunet-helper-nat-client.c
93 * Must match IP given in the client.
95 #define DUMMY_IP "192.0.2.86"
100 #define NAT_TRAV_PORT 22225
103 * How often do we send our ICMP messages to receive replies?
105 #define ICMP_SEND_FREQUENCY_MS 500
114 * Version (4 bits) + Internet header length (4 bits)
134 * Flags (3 bits) + Fragment offset (13 bits)
136 uint16_t flags_frag_offset;
159 * Destination address
165 * Format of ICMP packet.
167 struct icmp_ttl_exceeded_header
177 /* followed by original payload */
180 struct icmp_echo_header
193 * Beginning of UDP packet.
207 * Socket we use to receive "fake" ICMP replies.
212 * Socket we use to send our ICMP requests.
217 * Socket we use to send our UDP requests.
222 * Target "dummy" address.
224 static struct in_addr dummy;
228 * CRC-16 for IP/ICMP headers.
230 * @param data what to calculate the CRC over
231 * @param bytes number of bytes in data (must be multiple of 2)
232 * @return the CRC 16.
235 calc_checksum (const uint16_t * data, unsigned int bytes)
241 for (i = 0; i < bytes / 2; i++)
243 sum = (sum & 0xffff) + (sum >> 16);
244 sum = htons (0xffff - sum);
250 * Send an ICMP message to the dummy IP.
252 * @param my_ip source address (our ip address)
255 send_icmp_echo (const struct in_addr *my_ip)
257 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
258 struct icmp_echo_header icmp_echo;
259 struct ip_header ip_pkt;
260 struct sockaddr_in dst;
265 ip_pkt.vers_ihl = 0x45;
267 ip_pkt.pkt_len = htons (sizeof (packet));
268 ip_pkt.id = htons (PACKET_ID);
269 ip_pkt.flags_frag_offset = 0;
270 ip_pkt.ttl = IPDEFTTL;
271 ip_pkt.proto = IPPROTO_ICMP;
273 ip_pkt.src_ip = my_ip->s_addr;
274 ip_pkt.dst_ip = dummy.s_addr;
276 htons (calc_checksum ((uint16_t *) & ip_pkt,
277 sizeof (struct ip_header)));
278 GNUNET_memcpy (&packet[off],
280 sizeof (struct ip_header));
281 off += sizeof (struct ip_header);
283 icmp_echo.type = ICMP_ECHO;
285 icmp_echo.checksum = 0;
286 icmp_echo.reserved = 0;
289 ((uint16_t *) & icmp_echo,
290 sizeof (struct icmp_echo_header)));
291 GNUNET_memcpy (&packet[off],
293 sizeof (struct icmp_echo_header));
294 off += sizeof (struct icmp_echo_header);
296 memset (&dst, 0, sizeof (dst));
297 dst.sin_family = AF_INET;
298 #if HAVE_SOCKADDR_IN_SIN_LEN
299 dst.sin_len = sizeof (struct sockaddr_in);
301 dst.sin_addr = dummy;
302 err = sendto (rawsock,
306 (struct sockaddr *) &dst,
312 "sendto failed: %s\n",
316 else if (sizeof (packet) != err)
319 "Error: partial send of ICMP message\n");
325 * Send a UDP message to the dummy IP.
330 struct sockaddr_in dst;
333 memset (&dst, 0, sizeof (dst));
334 dst.sin_family = AF_INET;
335 #if HAVE_SOCKADDR_IN_SIN_LEN
336 dst.sin_len = sizeof (struct sockaddr_in);
338 dst.sin_addr = dummy;
339 dst.sin_port = htons (NAT_TRAV_PORT);
340 err = sendto (udpsock,
344 (struct sockaddr *) &dst,
350 "sendto failed: %s\n",
357 "Error: partial send of ICMP message\n");
363 * We've received an ICMP response. Process it.
366 process_icmp_response ()
370 struct in_addr source_ip;
371 struct ip_header ip_pkt;
372 struct icmp_ttl_exceeded_header icmp_ttl;
373 struct icmp_echo_header icmp_echo;
374 struct udp_header udp_pkt;
378 have = read (icmpsock, buf, sizeof (buf));
382 "Error reading raw socket: %s\n",
388 "Received message of %u bytes\n",
389 (unsigned int) have);
392 (ssize_t) (sizeof (struct ip_header) +
393 sizeof (struct icmp_ttl_exceeded_header) +
394 sizeof (struct ip_header)))
400 GNUNET_memcpy (&ip_pkt,
402 sizeof (struct ip_header));
403 off += sizeof (struct ip_header);
404 GNUNET_memcpy (&icmp_ttl,
406 sizeof (struct icmp_ttl_exceeded_header));
407 off += sizeof (struct icmp_ttl_exceeded_header);
408 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
410 /* different type than what we want */
413 /* grab source IP of 1st IP header */
414 source_ip.s_addr = ip_pkt.src_ip;
416 /* skip 2nd IP header */
417 GNUNET_memcpy (&ip_pkt,
419 sizeof (struct ip_header));
420 off += sizeof (struct ip_header);
422 switch (ip_pkt.proto)
426 (sizeof (struct ip_header) * 2 +
427 sizeof (struct icmp_ttl_exceeded_header) +
428 sizeof (struct icmp_echo_header)))
433 /* grab ICMP ECHO content */
434 GNUNET_memcpy (&icmp_echo,
436 sizeof (struct icmp_echo_header));
437 port = (uint16_t) ntohl (icmp_echo.reserved);
441 (sizeof (struct ip_header) * 2 +
442 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
447 /* grab UDP content */
448 GNUNET_memcpy (&udp_pkt,
450 sizeof (struct udp_header));
451 port = ntohs (udp_pkt.length);
454 /* different type than what we want */
459 fprintf (stdout, "%s\n",
460 inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)));
462 fprintf (stdout, "%s:%u\n",
463 inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)),
464 (unsigned int) port);
470 * Fully initialize the raw socket.
472 * @return -1 on error, 0 on success
487 "setsockopt failed: %s\n",
499 "setsockopt failed: %s\n",
508 * Create a UDP socket for writing.
510 * @param my_ip source address (our ip address)
511 * @return -1 on error
514 make_udp_socket (const struct in_addr *my_ip)
517 struct sockaddr_in addr;
519 ret = socket (AF_INET, SOCK_DGRAM, 0);
523 "Error opening UDP socket: %s\n",
527 memset (&addr, 0, sizeof (addr));
528 addr.sin_family = AF_INET;
529 #if HAVE_SOCKADDR_IN_SIN_LEN
530 addr.sin_len = sizeof (struct sockaddr_in);
532 addr.sin_addr = *my_ip;
533 addr.sin_port = htons (NAT_TRAV_PORT);
536 (struct sockaddr *) &addr,
540 "Error binding UDP socket to port %u: %s\n",
554 struct in_addr external;
563 /* Create an ICMP raw socket for reading (we'll check errors later) */
564 icmpsock = socket (AF_INET,
569 /* Create an (ICMP) raw socket for writing (we'll check errors later) */
570 rawsock = socket (AF_INET,
576 /* drop root rights */
578 #ifdef HAVE_SETRESUID
579 if (0 != setresuid (uid, uid, uid))
582 "Failed to setresuid: %s\n",
588 if (0 != (setuid (uid) | seteuid (uid)))
591 "Failed to setuid: %s\n",
598 /* Now that we run without root rights, we can do error checking... */
602 "This program must be started with our (internal NAT) IP as the only argument.\n");
606 if (1 != inet_pton (AF_INET, argv[1], &external))
609 "Error parsing IPv4 address: %s\n",
614 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
617 "Internal error converting dummy IP to binary.\n");
622 /* error checking icmpsock */
626 "Error opening RAW socket: %s\n",
627 strerror (icmp_eno));
631 if (icmpsock >= FD_SETSIZE)
633 /* this could happen if we were started with a large number of already-open
634 file descriptors... */
636 "Socket number too large (%d > %u)\n",
638 (unsigned int) FD_SETSIZE);
643 /* error checking rawsock */
647 "Error opening RAW socket: %s\n",
652 /* no need to check 'rawsock' against FD_SETSIZE as it is never used
655 if (0 != setup_raw_socket ())
661 if (-1 == (udpsock = make_udp_socket (&external)))
671 FD_SET (icmpsock, &rs);
673 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
674 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
679 "select failed: %s\n",
683 if (1 == getppid ()) /* Check the parent process id, if 1 the parent has died, so we should die too */
685 if (FD_ISSET (icmpsock, &rs))
687 process_icmp_response ();
690 if (0 == (++alt % 2))
691 send_icmp_echo (&external);
696 /* select failed (internal error or OS out of resources) */
700 (void) close (icmpsock);
702 (void) close (rawsock);
704 (void) close (udpsock);
709 /* end of gnunet-helper-nat-server.c */