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
45 /* Just needed for HAVE_SOCKADDR_IN_SIN_LEN test macro! */
46 #include "gnunet_config.h"
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <arpa/inet.h>
51 #include <sys/select.h>
53 #include <sys/types.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_icmp.h>
63 #include <netinet/in.h>
66 * Should we print some debug output?
71 * Must match IP given in the client.
73 #define DUMMY_IP "192.0.2.86"
76 * How often do we send our ICMP messages to receive replies?
78 #define ICMP_SEND_FREQUENCY_MS 500
87 * Version (4 bits) + Internet header length (4 bits)
107 * Flags (3 bits) + Fragment offset (13 bits)
109 uint16_t flags_frag_offset;
132 * Destination address
138 * Format of ICMP packet.
152 * Beginning of UDP packet.
164 * Socket we use to receive "fake" ICMP replies.
169 * Socket we use to send our ICMP requests.
174 * Target "dummy" address.
176 static struct in_addr dummy;
180 * CRC-16 for IP/ICMP headers.
182 * @param data what to calculate the CRC over
183 * @param bytes number of bytes in data (must be multiple of 2)
184 * @return the CRC 16.
187 calc_checksum(const uint16_t *data,
194 for (i=0;i<bytes/2;i++)
196 sum = (sum & 0xffff) + (sum >> 16);
197 sum = htons(0xffff - sum);
203 * Send an ICMP message to the dummy IP.
205 * @param my_ip source address (our ip address)
208 send_icmp_echo (const struct in_addr *my_ip)
210 struct icmp_packet icmp_echo;
211 struct sockaddr_in dst;
214 struct ip_packet ip_pkt;
215 struct icmp_packet icmp_pkt;
216 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
219 memset(&ip_pkt, 0, sizeof(ip_pkt));
220 ip_pkt.vers_ihl = 0x45;
222 ip_pkt.pkt_len = sizeof (packet);
224 ip_pkt.flags_frag_offset = 0;
225 ip_pkt.ttl = IPDEFTTL;
226 ip_pkt.proto = IPPROTO_ICMP;
228 ip_pkt.src_ip = my_ip->s_addr;
229 ip_pkt.dst_ip = dummy.s_addr;
230 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
231 memcpy (packet, &ip_pkt, sizeof (ip_pkt));
232 off += sizeof (ip_pkt);
234 icmp_echo.type = ICMP_ECHO;
236 icmp_echo.reserved = 0;
237 icmp_echo.checksum = 0;
238 icmp_echo.checksum = htons(calc_checksum((uint16_t*)&icmp_echo,
239 sizeof (struct icmp_packet)));
241 make_echo (my_ip, &icmp_echo);
243 memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
244 off += sizeof (icmp_echo);
246 memset (&dst, 0, sizeof (dst));
247 dst.sin_family = AF_INET;
248 #if HAVE_SOCKADDR_IN_SIN_LEN
249 dst.sin_len = sizeof (struct sockaddr_in);
251 dst.sin_addr = dummy;
252 err = sendto(rawsock,
254 (struct sockaddr*)&dst,
260 "sendto failed: %s\n", strerror(errno));
266 "Error: partial send of ICMP message\n");
272 * We've received an ICMP response. Process it.
275 process_icmp_response ()
280 struct ip_packet ip_pkt;
281 struct icmp_packet icmp_pkt;
282 struct udp_packet udp_pkt;
287 have = read (icmpsock, buf, sizeof (buf));
291 "Error reading raw socket: %s\n",
298 "Received message of %u bytes\n",
299 (unsigned int) have);
301 if (have == sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2 + sizeof(uint32_t))
305 else if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2)
309 "Received ICMP message of unexpected size: %u bytes\n",
310 (unsigned int) have);
315 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
316 off += sizeof (ip_pkt);
317 memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
318 off += sizeof (icmp_pkt);
319 if ( ((ip_pkt.proto != IPPROTO_ICMP) && (ip_pkt.proto != IPPROTO_UDP)) ||
320 (icmp_pkt.type != ICMP_TIME_EXCEEDED) ||
321 (icmp_pkt.code != 0) )
323 /* maybe we got an actual reply back... */
329 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
330 off += sizeof (ip_pkt);
335 &buf[sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2],
346 else if (ip_pkt.proto == IPPROTO_UDP)
357 ntohs((uint16_t) udp_pkt.length));
373 * Create an ICMP raw socket for reading.
375 * @return -1 on error
382 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
386 "Error opening RAW socket: %s\n",
390 if (ret >= FD_SETSIZE)
393 "Socket number too large (%d > %u)\n",
395 (unsigned int) FD_SETSIZE);
404 * Create an ICMP raw socket for writing.
406 * @return -1 on error
414 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
418 "Error opening RAW socket: %s\n",
429 "setsockopt failed: %s\n",
437 (char *)&one, sizeof(one)) == -1)
440 "setsockopt failed: %s\n",
453 struct in_addr external;
461 "This program must be started with our (internal NAT) IP as the only argument.\n");
464 if (1 != inet_pton (AF_INET, argv[1], &external))
467 "Error parsing IPv4 address: %s\n",
471 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
474 "Internal error converting dummy IP to binary.\n");
477 if (-1 == (icmpsock = make_icmp_socket()))
481 if (-1 == (rawsock = make_raw_socket()))
487 if (0 != setresuid (uid, uid, uid))
490 "Failed to setresuid: %s\n",
492 /* not critical, continue anyway */
497 FD_SET (icmpsock, &rs);
499 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
500 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
505 "select failed: %s\n",
509 if (FD_ISSET (icmpsock, &rs))
510 process_icmp_response ();
511 send_icmp_echo (&external);
513 /* select failed (internal error or OS out of resources) */
520 /* end of gnunet-nat-server.c */