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
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>
67 * Should we print some debug output?
72 * Must match IP given in the client.
74 #define DUMMY_IP "192.0.2.86"
79 #define NAT_TRAV_PORT 22225
82 * How often do we send our ICMP messages to receive replies?
84 #define ICMP_SEND_FREQUENCY_MS 500
93 * Version (4 bits) + Internet header length (4 bits)
113 * Flags (3 bits) + Fragment offset (13 bits)
115 uint16_t flags_frag_offset;
138 * Destination address
144 * Format of ICMP packet.
146 struct icmp_ttl_exceeded_header
156 /* followed by original payload */
159 struct icmp_echo_header
172 * Beginning of UDP packet.
186 * Socket we use to receive "fake" ICMP replies.
191 * Socket we use to send our ICMP requests.
196 * Socket we use to send our UDP requests.
201 * Target "dummy" address.
203 static struct in_addr dummy;
207 * CRC-16 for IP/ICMP headers.
209 * @param data what to calculate the CRC over
210 * @param bytes number of bytes in data (must be multiple of 2)
211 * @return the CRC 16.
214 calc_checksum(const uint16_t *data,
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 = sizeof (packet);
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],
259 sizeof (struct ip_header));
260 off += sizeof (struct ip_header);
262 icmp_echo.type = ICMP_ECHO;
264 icmp_echo.checksum = 0;
265 icmp_echo.reserved = 0;
266 icmp_echo.checksum = htons(calc_checksum((uint16_t*)&icmp_echo,
267 sizeof (struct icmp_echo_header)));
268 memcpy (&packet[off],
270 sizeof (struct icmp_echo_header));
271 off += sizeof (struct icmp_echo_header);
273 memset (&dst, 0, sizeof (dst));
274 dst.sin_family = AF_INET;
275 #if HAVE_SOCKADDR_IN_SIN_LEN
276 dst.sin_len = sizeof (struct sockaddr_in);
278 dst.sin_addr = dummy;
279 err = sendto(rawsock,
281 (struct sockaddr*)&dst,
282 sizeof(struct sockaddr_in));
287 "sendto failed: %s\n", strerror(errno));
290 else if (sizeof (packet) != err)
293 "Error: partial send of ICMP message\n");
299 * Send a UDP message to the dummy IP.
301 * @param my_ip source address (our ip address)
306 struct sockaddr_in dst;
309 memset (&dst, 0, sizeof (dst));
310 dst.sin_family = AF_INET;
311 #if HAVE_SOCKADDR_IN_SIN_LEN
312 dst.sin_len = sizeof (struct sockaddr_in);
314 dst.sin_addr = dummy;
315 dst.sin_port = htons (NAT_TRAV_PORT);
316 err = sendto(udpsock,
318 (struct sockaddr*)&dst,
324 "sendto failed: %s\n", strerror(errno));
330 "Error: partial send of ICMP message\n");
336 * We've received an ICMP response. Process it.
339 process_icmp_response ()
343 struct in_addr source_ip;
344 struct ip_header ip_pkt;
345 struct icmp_ttl_exceeded_header icmp_pkt;
346 struct icmp_echo_header icmp_echo_pkt;
347 struct udp_header udp_pkt;
351 have = read (icmpsock, buf, sizeof (buf));
355 "Error reading raw socket: %s\n",
361 "Received message of %u bytes\n",
362 (unsigned int) have);
364 if (have < sizeof (struct ip_header) + sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct ip_header) )
372 sizeof (struct ip_header));
373 off += sizeof (struct ip_header);
379 sizeof (struct icmp_ttl_exceeded_header));
380 off += sizeof (struct icmp_ttl_exceeded_header);
381 if ( (ICMP_TIME_EXCEEDED != icmp_pkt.type) ||
382 (0 != icmp_pkt.code) )
384 /* different type than what we want */
387 /* skip 2nd IP header */
388 off += sizeof (struct ip_header);
390 switch (ip_pkt.proto)
393 if (have != (sizeof (struct ip_header) * 2 +
394 sizeof (struct icmp_ttl_exceeded_header) +
395 sizeof (struct icmp_echo_header)) )
400 /* grab ICMP ECHO content */
401 memcpy (&icmp_echo_pkt,
403 sizeof (struct icmp_echo_header));
404 port = (uint16_t) htonl (icmp_echo_pkt.reserved);
407 if (have != (sizeof (struct ip_header) * 2 +
408 sizeof (struct icmp_ttl_exceeded_header) +
409 sizeof (struct udp_header)) )
414 /* grab UDP content */
417 sizeof (struct udp_header));
418 port = ntohs (udp_pkt.crc);
419 port = ntohs (udp_pkt.length);
422 /* different type than what we want */
446 * Create an ICMP raw socket for reading.
448 * @return -1 on error
455 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
459 "Error opening RAW socket: %s\n",
463 if (ret >= FD_SETSIZE)
466 "Socket number too large (%d > %u)\n",
468 (unsigned int) FD_SETSIZE);
477 * Create an ICMP raw socket for writing.
479 * @return -1 on error
487 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
491 "Error opening RAW socket: %s\n",
495 if (-1 == setsockopt(ret,
498 (char *)&one, sizeof(one)))
501 "setsockopt failed: %s\n",
506 if (-1 == setsockopt(ret,
509 (char *)&one, sizeof(one)))
512 "setsockopt failed: %s\n",
522 * Create a UDP socket for writinging.
524 * @return -1 on error
527 make_udp_socket (const struct in_addr *my_ip)
530 struct sockaddr_in addr;
532 ret = socket (AF_INET, SOCK_DGRAM, 0);
536 "Error opening UDP socket: %s\n",
543 addr.sin_family = AF_INET;
544 #if HAVE_SOCKADDR_IN_SIN_LEN
545 addr.sin_len = sizeof (struct sockaddr_in);
547 addr.sin_addr = *my_ip;
548 addr.sin_port = htons (NAT_TRAV_PORT);
555 "Error binding UDP socket to port %u: %s\n",
558 /* likely problematic, but not certain, try to continue */
568 struct in_addr external;
577 "This program must be started with our (internal NAT) IP as the only argument.\n");
580 if (1 != inet_pton (AF_INET, argv[1], &external))
583 "Error parsing IPv4 address: %s\n",
587 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
590 "Internal error converting dummy IP to binary.\n");
593 if (-1 == (icmpsock = make_icmp_socket()))
597 if (-1 == (rawsock = make_raw_socket()))
603 if (0 != setresuid (uid, uid, uid))
606 "Failed to setresuid: %s\n",
608 /* not critical, continue anyway */
610 if (-1 == (udpsock = make_udp_socket(&external)))
620 FD_SET (icmpsock, &rs);
622 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
623 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
628 "select failed: %s\n",
632 if (FD_ISSET (icmpsock, &rs))
633 process_icmp_response ();
634 if (0 == (++alt % 2))
635 send_icmp_echo (&external);
639 /* select failed (internal error or OS out of resources) */
647 /* end of gnunet-nat-server.c */