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/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)
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, unsigned int bytes)
221 for (i = 0; i < bytes / 2; i++)
223 sum = (sum & 0xffff) + (sum >> 16);
224 sum = htons (0xffff - sum);
230 * Send an ICMP message to the dummy IP.
232 * @param my_ip source address (our ip address)
235 send_icmp_echo (const struct in_addr *my_ip)
237 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
238 struct icmp_echo_header icmp_echo;
239 struct ip_header ip_pkt;
240 struct sockaddr_in dst;
245 ip_pkt.vers_ihl = 0x45;
247 ip_pkt.pkt_len = htons (sizeof (packet));
248 ip_pkt.id = htons (256);
249 ip_pkt.flags_frag_offset = 0;
250 ip_pkt.ttl = IPDEFTTL;
251 ip_pkt.proto = IPPROTO_ICMP;
253 ip_pkt.src_ip = my_ip->s_addr;
254 ip_pkt.dst_ip = dummy.s_addr;
255 ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt,
256 sizeof (struct ip_header)));
257 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
258 off += sizeof (struct ip_header);
260 icmp_echo.type = ICMP_ECHO;
262 icmp_echo.checksum = 0;
263 icmp_echo.reserved = 0;
264 icmp_echo.checksum = htons (calc_checksum ((uint16_t *) & icmp_echo,
265 sizeof (struct icmp_echo_header)));
266 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
267 off += sizeof (struct icmp_echo_header);
269 memset (&dst, 0, sizeof (dst));
270 dst.sin_family = AF_INET;
271 #if HAVE_SOCKADDR_IN_SIN_LEN
272 dst.sin_len = sizeof (struct sockaddr_in);
274 dst.sin_addr = dummy;
275 err = sendto (rawsock,
276 packet, off, 0, (struct sockaddr *) &dst, sizeof (dst));
280 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
283 else if (sizeof (packet) != err)
285 fprintf (stderr, "Error: partial send of ICMP message\n");
291 * Send a UDP message to the dummy IP.
296 struct sockaddr_in dst;
299 memset (&dst, 0, sizeof (dst));
300 dst.sin_family = AF_INET;
301 #if HAVE_SOCKADDR_IN_SIN_LEN
302 dst.sin_len = sizeof (struct sockaddr_in);
304 dst.sin_addr = dummy;
305 dst.sin_port = htons (NAT_TRAV_PORT);
306 err = sendto (udpsock, NULL, 0, 0, (struct sockaddr *) &dst, sizeof (dst));
310 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
315 fprintf (stderr, "Error: partial send of ICMP message\n");
321 * We've received an ICMP response. Process it.
324 process_icmp_response ()
328 struct in_addr source_ip;
329 struct ip_header ip_pkt;
330 struct icmp_ttl_exceeded_header icmp_ttl;
331 struct icmp_echo_header icmp_echo;
332 struct udp_header udp_pkt;
336 have = read (icmpsock, buf, sizeof (buf));
339 fprintf (stderr, "Error reading raw socket: %s\n", strerror (errno));
343 fprintf (stderr, "Received message of %u bytes\n", (unsigned int) have);
346 (ssize_t) (sizeof (struct ip_header) +
347 sizeof (struct icmp_ttl_exceeded_header) +
348 sizeof (struct ip_header)))
354 memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
355 off += sizeof (struct ip_header);
356 memcpy (&source_ip, &ip_pkt.src_ip, sizeof (source_ip));
357 memcpy (&icmp_ttl, &buf[off], sizeof (struct icmp_ttl_exceeded_header));
358 off += sizeof (struct icmp_ttl_exceeded_header);
359 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
361 /* different type than what we want */
364 /* skip 2nd IP header */
365 memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
366 off += sizeof (struct ip_header);
368 switch (ip_pkt.proto)
371 if (have != (sizeof (struct ip_header) * 2 +
372 sizeof (struct icmp_ttl_exceeded_header) +
373 sizeof (struct icmp_echo_header)))
378 /* grab ICMP ECHO content */
379 memcpy (&icmp_echo, &buf[off], sizeof (struct icmp_echo_header));
380 port = (uint16_t) ntohl (icmp_echo.reserved);
383 if (have != (sizeof (struct ip_header) * 2 +
384 sizeof (struct icmp_ttl_exceeded_header) +
385 sizeof (struct udp_header)))
390 /* grab UDP content */
391 memcpy (&udp_pkt, &buf[off], sizeof (struct udp_header));
392 port = ntohs (udp_pkt.length);
395 /* different type than what we want */
401 "%s\n", inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)));
406 &source_ip, buf, sizeof (buf)), (unsigned int) port);
412 * Create an ICMP raw socket for reading.
414 * @return -1 on error
421 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
424 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
427 if (ret >= FD_SETSIZE)
430 "Socket number too large (%d > %u)\n",
431 ret, (unsigned int) FD_SETSIZE);
440 * Create an ICMP raw socket for writing.
442 * @return -1 on error
450 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
453 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
456 if (-1 == setsockopt (ret,
457 SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
459 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
463 if (-1 == setsockopt (ret,
464 IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
466 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
475 * Create a UDP socket for writinging.
477 * @param my_ip source address (our ip address)
478 * @return -1 on error
481 make_udp_socket (const struct in_addr *my_ip)
484 struct sockaddr_in addr;
486 ret = socket (AF_INET, SOCK_DGRAM, 0);
489 fprintf (stderr, "Error opening UDP socket: %s\n", strerror (errno));
492 memset (&addr, 0, sizeof (addr));
493 addr.sin_family = AF_INET;
494 #if HAVE_SOCKADDR_IN_SIN_LEN
495 addr.sin_len = sizeof (struct sockaddr_in);
497 addr.sin_addr = *my_ip;
498 addr.sin_port = htons (NAT_TRAV_PORT);
500 if (0 != bind (ret, &addr, sizeof (addr)))
503 "Error binding UDP socket to port %u: %s\n",
504 NAT_TRAV_PORT, strerror (errno));
505 /* likely problematic, but not certain, try to continue */
512 main (int argc, char *const *argv)
514 struct in_addr external;
523 "This program must be started with our (internal NAT) IP as the only argument.\n");
526 if (1 != inet_pton (AF_INET, argv[1], &external))
528 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
531 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
533 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
536 if (-1 == (icmpsock = make_icmp_socket ()))
540 if (-1 == (rawsock = make_raw_socket ()))
546 if (0 != setresuid (uid, uid, uid))
548 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
549 /* not critical, continue anyway */
551 if (-1 == (udpsock = make_udp_socket (&external)))
561 FD_SET (icmpsock, &rs);
563 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
564 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
568 fprintf (stderr, "select failed: %s\n", strerror (errno));
571 if (1 == getppid ()) /* Check the parent process id, if 1 the parent has died, so we should die too */
573 if (FD_ISSET (icmpsock, &rs))
574 process_icmp_response ();
575 if (0 == (++alt % 2))
576 send_icmp_echo (&external);
580 /* select failed (internal error or OS out of resources) */
588 /* end of gnunet-helper-nat-server.c */