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-client.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.
25 * @author Christian Grothoff
27 * This program will send ONE ICMP message using RAW sockets
28 * to the IP address specified as the second argument. 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 RAW socket 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/types.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip_icmp.h>
62 #include <netinet/in.h>
65 * Must match IP given in the server.
67 #define DUMMY_IP "192.0.2.86"
69 #define NAT_TRAV_PORT 22225
72 * Must match packet ID used by gnunet-helper-nat-server.c
83 * Version (4 bits) + Internet header length (4 bits)
103 * Flags (3 bits) + Fragment offset (13 bits)
105 uint16_t flags_frag_offset;
128 * Destination address
134 * Format of ICMP packet.
136 struct icmp_ttl_exceeded_header
146 /* followed by original payload */
149 struct icmp_echo_header
161 * Beginning of UDP packet.
175 * Socket we use to send our fake ICMP replies.
180 * Target "dummy" address of the packet we pretend to respond to.
182 static struct in_addr dummy;
187 static uint16_t port;
191 * CRC-16 for IP/ICMP headers.
193 * @param data what to calculate the CRC over
194 * @param bytes number of bytes in data (must be multiple of 2)
195 * @return the CRC 16.
198 calc_checksum (const uint16_t * data, unsigned int bytes)
204 for (i = 0; i < bytes / 2; i++)
206 sum = (sum & 0xffff) + (sum >> 16);
207 sum = htons (0xffff - sum);
213 * Send an ICMP message to the target.
215 * @param my_ip source address
216 * @param other target address
219 send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
221 char packet[sizeof (struct ip_header) * 2 +
222 sizeof (struct icmp_ttl_exceeded_header) +
223 sizeof (struct udp_header)];
224 struct ip_header ip_pkt;
225 struct icmp_ttl_exceeded_header icmp_pkt;
226 struct udp_header udp_pkt;
227 struct sockaddr_in dst;
231 /* ip header: send to (known) ip address */
233 ip_pkt.vers_ihl = 0x45;
235 ip_pkt.pkt_len = htons (sizeof (packet));
236 ip_pkt.id = htons (PACKET_ID);
237 ip_pkt.flags_frag_offset = 0;
239 ip_pkt.proto = IPPROTO_ICMP;
241 ip_pkt.src_ip = my_ip->s_addr;
242 ip_pkt.dst_ip = other->s_addr;
244 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
245 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
246 off += sizeof (struct ip_header);
248 icmp_pkt.type = ICMP_TIME_EXCEEDED;
250 icmp_pkt.checksum = 0;
252 memcpy (&packet[off], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header));
253 off += sizeof (struct icmp_ttl_exceeded_header);
255 /* ip header of the presumably 'lost' udp packet */
256 ip_pkt.vers_ihl = 0x45;
259 htons (sizeof (struct ip_header) + sizeof (struct udp_header));
260 ip_pkt.id = htons (0);
261 ip_pkt.flags_frag_offset = 0;
263 ip_pkt.proto = IPPROTO_UDP;
265 ip_pkt.src_ip = other->s_addr;
266 ip_pkt.dst_ip = dummy.s_addr;
268 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
269 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
270 off += sizeof (struct ip_header);
272 /* build UDP header */
273 udp_pkt.src_port = htons (NAT_TRAV_PORT);
274 udp_pkt.dst_port = htons (NAT_TRAV_PORT);
275 udp_pkt.length = htons (port);
277 memcpy (&packet[off], &udp_pkt, sizeof (struct udp_header));
278 off += sizeof (struct udp_header);
280 /* set ICMP checksum */
283 ((uint16_t *) & packet[sizeof (struct ip_header)],
284 sizeof (struct icmp_ttl_exceeded_header) +
285 sizeof (struct ip_header) + sizeof (struct udp_header)));
286 memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt,
287 sizeof (struct icmp_ttl_exceeded_header));
289 memset (&dst, 0, sizeof (dst));
290 dst.sin_family = AF_INET;
291 #if HAVE_SOCKADDR_IN_SIN_LEN
292 dst.sin_len = sizeof (struct sockaddr_in);
294 dst.sin_addr = *other;
296 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
300 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
302 else if (sizeof (packet) != (size_t) err)
304 fprintf (stderr, "Error: partial send of ICMP message with size %lu\n", (unsigned long) off);
310 * Send an ICMP message to the target.
312 * @param my_ip source address
313 * @param other target address
316 send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
318 struct ip_header ip_pkt;
319 struct icmp_ttl_exceeded_header icmp_ttl;
320 struct icmp_echo_header icmp_echo;
321 struct sockaddr_in dst;
322 char packet[sizeof (struct ip_header) * 2 +
323 sizeof (struct icmp_ttl_exceeded_header) +
324 sizeof (struct icmp_echo_header)];
328 /* ip header: send to (known) ip address */
330 ip_pkt.vers_ihl = 0x45;
332 ip_pkt.pkt_len = htons (sizeof (packet));
333 ip_pkt.id = htons (PACKET_ID);
334 ip_pkt.flags_frag_offset = 0;
335 ip_pkt.ttl = IPDEFTTL;
336 ip_pkt.proto = IPPROTO_ICMP;
338 ip_pkt.src_ip = my_ip->s_addr;
339 ip_pkt.dst_ip = other->s_addr;
341 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
342 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
343 off = sizeof (ip_pkt);
345 /* icmp reply: time exceeded */
346 icmp_ttl.type = ICMP_TIME_EXCEEDED;
348 icmp_ttl.checksum = 0;
350 memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
351 off += sizeof (struct icmp_ttl_exceeded_header);
353 /* ip header of the presumably 'lost' udp packet */
354 ip_pkt.vers_ihl = 0x45;
357 htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
358 ip_pkt.id = htons (PACKET_ID);
359 ip_pkt.flags_frag_offset = 0;
360 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
361 ip_pkt.proto = IPPROTO_ICMP;
362 ip_pkt.src_ip = other->s_addr;
363 ip_pkt.dst_ip = dummy.s_addr;
366 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
367 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
368 off += sizeof (struct ip_header);
370 icmp_echo.type = ICMP_ECHO;
372 icmp_echo.reserved = htonl (port);
373 icmp_echo.checksum = 0;
376 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
377 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
379 /* no go back to calculate ICMP packet checksum */
380 off = sizeof (struct ip_header);
383 ((uint16_t *) & packet[off],
384 sizeof (struct icmp_ttl_exceeded_header) +
385 sizeof (struct ip_header) + sizeof (struct icmp_echo_header)));
386 memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
388 /* prepare for transmission */
389 memset (&dst, 0, sizeof (dst));
390 dst.sin_family = AF_INET;
391 #if HAVE_SOCKADDR_IN_SIN_LEN
392 dst.sin_len = sizeof (struct sockaddr_in);
394 dst.sin_addr = *other;
396 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
400 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
402 else if (sizeof (packet) != (size_t) err)
404 fprintf (stderr, "Error: partial send of ICMP message\n");
410 main (int argc, char *const *argv)
413 struct in_addr external;
414 struct in_addr target;
420 /* Create an ICMP raw socket for writing (only operation that requires root) */
421 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
422 raw_eno = errno; /* for later error checking */
424 /* now drop root privileges */
426 #ifdef HAVE_SETRESUID
427 if (0 != setresuid (uid, uid, uid))
429 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
434 if (0 != (setuid (uid) | seteuid (uid)))
436 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
443 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno));
448 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
450 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
455 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
457 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
465 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
469 if ((1 != inet_pton (AF_INET, argv[1], &external)) ||
470 (1 != inet_pton (AF_INET, argv[2], &target)))
472 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
476 if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
478 fprintf (stderr, "Error parsing port value `%s'\n", argv[3]);
483 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
485 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
489 send_icmp (&external, &target);
490 send_icmp_udp (&external, &target);
494 (void) close (rawsock);
498 /* end of gnunet-helper-nat-client.c */