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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file src/nat/gnunet-helper-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)
43 * - Jacob Appelbaum (19 Dec 2011)
46 /* Just needed for HAVE_SOCKADDR_IN_SIN_LEN test macro! */
47 #include "gnunet_config.h"
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <arpa/inet.h>
54 #include <sys/select.h>
56 #include <sys/types.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_icmp.h>
66 #include <netinet/in.h>
68 /* The following constant is missing from FreeBSD 9.2 */
69 #ifndef ICMP_TIME_EXCEEDED
70 #define ICMP_TIME_EXCEEDED 11
74 * Should we print some debug output?
79 * Must match packet ID used by gnunet-helper-nat-client.c
84 * Must match IP given in the client.
86 #define DUMMY_IP "192.0.2.86"
91 #define NAT_TRAV_PORT 22225
94 * How often do we send our ICMP messages to receive replies?
96 #define ICMP_SEND_FREQUENCY_MS 500
105 * Version (4 bits) + Internet header length (4 bits)
125 * Flags (3 bits) + Fragment offset (13 bits)
127 uint16_t flags_frag_offset;
150 * Destination address
156 * Format of ICMP packet.
158 struct icmp_ttl_exceeded_header
168 /* followed by original payload */
171 struct icmp_echo_header
184 * Beginning of UDP packet.
198 * Socket we use to receive "fake" ICMP replies.
203 * Socket we use to send our ICMP requests.
208 * Socket we use to send our UDP requests.
213 * Target "dummy" address.
215 static struct in_addr dummy;
219 * CRC-16 for IP/ICMP headers.
221 * @param data what to calculate the CRC over
222 * @param bytes number of bytes in data (must be multiple of 2)
223 * @return the CRC 16.
226 calc_checksum (const uint16_t * data, unsigned int bytes)
232 for (i = 0; i < bytes / 2; i++)
234 sum = (sum & 0xffff) + (sum >> 16);
235 sum = htons (0xffff - sum);
241 * Send an ICMP message to the dummy IP.
243 * @param my_ip source address (our ip address)
246 send_icmp_echo (const struct in_addr *my_ip)
248 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
249 struct icmp_echo_header icmp_echo;
250 struct ip_header ip_pkt;
251 struct sockaddr_in dst;
256 ip_pkt.vers_ihl = 0x45;
258 ip_pkt.pkt_len = htons (sizeof (packet));
259 ip_pkt.id = htons (PACKET_ID);
260 ip_pkt.flags_frag_offset = 0;
261 ip_pkt.ttl = IPDEFTTL;
262 ip_pkt.proto = IPPROTO_ICMP;
264 ip_pkt.src_ip = my_ip->s_addr;
265 ip_pkt.dst_ip = dummy.s_addr;
267 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
268 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
269 off += sizeof (struct ip_header);
271 icmp_echo.type = ICMP_ECHO;
273 icmp_echo.checksum = 0;
274 icmp_echo.reserved = 0;
277 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
278 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
279 off += sizeof (struct icmp_echo_header);
281 memset (&dst, 0, sizeof (dst));
282 dst.sin_family = AF_INET;
283 #if HAVE_SOCKADDR_IN_SIN_LEN
284 dst.sin_len = sizeof (struct sockaddr_in);
286 dst.sin_addr = dummy;
288 sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst));
292 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
295 else if (sizeof (packet) != err)
297 fprintf (stderr, "Error: partial send of ICMP message\n");
303 * Send a UDP message to the dummy IP.
308 struct sockaddr_in dst;
311 memset (&dst, 0, sizeof (dst));
312 dst.sin_family = AF_INET;
313 #if HAVE_SOCKADDR_IN_SIN_LEN
314 dst.sin_len = sizeof (struct sockaddr_in);
316 dst.sin_addr = dummy;
317 dst.sin_port = htons (NAT_TRAV_PORT);
318 err = sendto (udpsock, NULL, 0, 0, (struct sockaddr *) &dst, sizeof (dst));
322 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
327 fprintf (stderr, "Error: partial send of ICMP message\n");
333 * We've received an ICMP response. Process it.
336 process_icmp_response ()
340 struct in_addr source_ip;
341 struct ip_header ip_pkt;
342 struct icmp_ttl_exceeded_header icmp_ttl;
343 struct icmp_echo_header icmp_echo;
344 struct udp_header udp_pkt;
348 have = read (icmpsock, buf, sizeof (buf));
351 fprintf (stderr, "Error reading raw socket: %s\n", strerror (errno));
355 fprintf (stderr, "Received message of %u bytes\n", (unsigned int) have);
358 (ssize_t) (sizeof (struct ip_header) +
359 sizeof (struct icmp_ttl_exceeded_header) +
360 sizeof (struct ip_header)))
366 memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
367 off += sizeof (struct ip_header);
368 memcpy (&icmp_ttl, &buf[off], sizeof (struct icmp_ttl_exceeded_header));
369 off += sizeof (struct icmp_ttl_exceeded_header);
370 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
372 /* different type than what we want */
375 /* grab source IP of 1st IP header */
376 source_ip.s_addr = ip_pkt.src_ip;
378 /* skip 2nd IP header */
379 memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
380 off += sizeof (struct ip_header);
382 switch (ip_pkt.proto)
386 (sizeof (struct ip_header) * 2 +
387 sizeof (struct icmp_ttl_exceeded_header) +
388 sizeof (struct icmp_echo_header)))
393 /* grab ICMP ECHO content */
394 memcpy (&icmp_echo, &buf[off], sizeof (struct icmp_echo_header));
395 port = (uint16_t) ntohl (icmp_echo.reserved);
399 (sizeof (struct ip_header) * 2 +
400 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
405 /* grab UDP content */
406 memcpy (&udp_pkt, &buf[off], sizeof (struct udp_header));
407 port = ntohs (udp_pkt.length);
410 /* different type than what we want */
415 fprintf (stdout, "%s\n",
416 inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)));
418 fprintf (stdout, "%s:%u\n",
419 inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)),
420 (unsigned int) port);
426 * Fully initialize the raw socket.
428 * @return -1 on error, 0 on success
436 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
438 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
442 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
444 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
452 * Create a UDP socket for writing.
454 * @param my_ip source address (our ip address)
455 * @return -1 on error
458 make_udp_socket (const struct in_addr *my_ip)
461 struct sockaddr_in addr;
463 ret = socket (AF_INET, SOCK_DGRAM, 0);
467 "Error opening UDP socket: %s\n",
471 memset (&addr, 0, sizeof (addr));
472 addr.sin_family = AF_INET;
473 #if HAVE_SOCKADDR_IN_SIN_LEN
474 addr.sin_len = sizeof (struct sockaddr_in);
476 addr.sin_addr = *my_ip;
477 addr.sin_port = htons (NAT_TRAV_PORT);
480 (struct sockaddr *) &addr,
484 "Error binding UDP socket to port %u: %s\n",
495 main (int argc, char *const *argv)
497 struct in_addr external;
506 /* Create an ICMP raw socket for reading (we'll check errors later) */
507 icmpsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
510 /* Create an (ICMP) raw socket for writing (we'll check errors later) */
511 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
515 /* drop root rights */
517 #ifdef HAVE_SETRESUID
518 if (0 != setresuid (uid, uid, uid))
520 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
525 if (0 != (setuid (uid) | seteuid (uid)))
527 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
533 /* Now that we run without root rights, we can do error checking... */
537 "This program must be started with our (internal NAT) IP as the only argument.\n");
541 if (1 != inet_pton (AF_INET, argv[1], &external))
543 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
547 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
549 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
554 /* error checking icmpsock */
557 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (icmp_eno));
561 if (icmpsock >= FD_SETSIZE)
563 /* this could happen if we were started with a large number of already-open
564 file descriptors... */
565 fprintf (stderr, "Socket number too large (%d > %u)\n", icmpsock,
566 (unsigned int) FD_SETSIZE);
571 /* error checking rawsock */
574 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno));
578 /* no need to check 'rawsock' against FD_SETSIZE as it is never used
581 if (0 != setup_raw_socket ())
587 if (-1 == (udpsock = make_udp_socket (&external)))
597 FD_SET (icmpsock, &rs);
599 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
600 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
604 fprintf (stderr, "select failed: %s\n", strerror (errno));
607 if (1 == getppid ()) /* Check the parent process id, if 1 the parent has died, so we should die too */
609 if (FD_ISSET (icmpsock, &rs))
610 process_icmp_response ();
611 if (0 == (++alt % 2))
612 send_icmp_echo (&external);
617 /* select failed (internal error or OS out of resources) */
621 (void) close (icmpsock);
623 (void) close (rawsock);
625 (void) close (udpsock);
630 /* end of gnunet-helper-nat-server.c */