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-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>
64 /* The following constant is missing from FreeBSD 9.2 */
65 #ifndef ICMP_TIME_EXCEEDED
66 #define ICMP_TIME_EXCEEDED 11
70 * Must match IP given in the server.
72 #define DUMMY_IP "192.0.2.86"
74 #define NAT_TRAV_PORT 22225
77 * Must match packet ID used by gnunet-helper-nat-server.c
88 * Version (4 bits) + Internet header length (4 bits)
108 * Flags (3 bits) + Fragment offset (13 bits)
110 uint16_t flags_frag_offset;
133 * Destination address
139 * Format of ICMP packet.
141 struct icmp_ttl_exceeded_header
151 /* followed by original payload */
154 struct icmp_echo_header
166 * Beginning of UDP packet.
180 * Socket we use to send our fake ICMP replies.
185 * Target "dummy" address of the packet we pretend to respond to.
187 static struct in_addr dummy;
192 static uint16_t port;
196 * CRC-16 for IP/ICMP headers.
198 * @param data what to calculate the CRC over
199 * @param bytes number of bytes in data (must be multiple of 2)
200 * @return the CRC 16.
203 calc_checksum (const uint16_t * data, unsigned int bytes)
209 for (i = 0; i < bytes / 2; i++)
211 sum = (sum & 0xffff) + (sum >> 16);
212 sum = htons (0xffff - sum);
218 * Send an ICMP message to the target.
220 * @param my_ip source address
221 * @param other target address
224 send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
226 char packet[sizeof (struct ip_header) * 2 +
227 sizeof (struct icmp_ttl_exceeded_header) +
228 sizeof (struct udp_header)];
229 struct ip_header ip_pkt;
230 struct icmp_ttl_exceeded_header icmp_pkt;
231 struct udp_header udp_pkt;
232 struct sockaddr_in dst;
236 /* ip header: send to (known) ip address */
238 ip_pkt.vers_ihl = 0x45;
241 ip_pkt.pkt_len = sizeof (packet); /* Workaround PR kern/21737 */
243 ip_pkt.pkt_len = htons (sizeof (packet));
245 ip_pkt.id = htons (PACKET_ID);
246 ip_pkt.flags_frag_offset = 0;
248 ip_pkt.proto = IPPROTO_ICMP;
250 ip_pkt.src_ip = my_ip->s_addr;
251 ip_pkt.dst_ip = other->s_addr;
253 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
254 GNUNET_memcpy (&packet[off],
256 sizeof (struct ip_header));
257 off += sizeof (struct ip_header);
259 icmp_pkt.type = ICMP_TIME_EXCEEDED;
261 icmp_pkt.checksum = 0;
263 GNUNET_memcpy (&packet[off],
265 sizeof (struct icmp_ttl_exceeded_header));
266 off += sizeof (struct icmp_ttl_exceeded_header);
268 /* ip header of the presumably 'lost' udp packet */
269 ip_pkt.vers_ihl = 0x45;
272 htons (sizeof (struct ip_header) + sizeof (struct udp_header));
273 ip_pkt.id = htons (0);
274 ip_pkt.flags_frag_offset = 0;
276 ip_pkt.proto = IPPROTO_UDP;
278 ip_pkt.src_ip = other->s_addr;
279 ip_pkt.dst_ip = dummy.s_addr;
281 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
282 GNUNET_memcpy (&packet[off],
284 sizeof (struct ip_header));
285 off += sizeof (struct ip_header);
287 /* build UDP header */
288 udp_pkt.src_port = htons (NAT_TRAV_PORT);
289 udp_pkt.dst_port = htons (NAT_TRAV_PORT);
290 udp_pkt.length = htons (port);
292 GNUNET_memcpy (&packet[off],
294 sizeof (struct udp_header));
295 off += sizeof (struct udp_header);
297 /* set ICMP checksum */
300 ((uint16_t *) & packet[sizeof (struct ip_header)],
301 sizeof (struct icmp_ttl_exceeded_header) +
302 sizeof (struct ip_header) + sizeof (struct udp_header)));
303 GNUNET_memcpy (&packet[sizeof (struct ip_header)],
305 sizeof (struct icmp_ttl_exceeded_header));
307 memset (&dst, 0, sizeof (dst));
308 dst.sin_family = AF_INET;
309 #if HAVE_SOCKADDR_IN_SIN_LEN
310 dst.sin_len = sizeof (struct sockaddr_in);
312 dst.sin_addr = *other;
314 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
318 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
320 else if (sizeof (packet) != (size_t) err)
322 fprintf (stderr, "Error: partial send of ICMP message with size %lu\n", (unsigned long) off);
328 * Send an ICMP message to the target.
330 * @param my_ip source address
331 * @param other target address
334 send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
336 struct ip_header ip_pkt;
337 struct icmp_ttl_exceeded_header icmp_ttl;
338 struct icmp_echo_header icmp_echo;
339 struct sockaddr_in dst;
340 char packet[sizeof (struct ip_header) * 2 +
341 sizeof (struct icmp_ttl_exceeded_header) +
342 sizeof (struct icmp_echo_header)];
346 /* ip header: send to (known) ip address */
348 ip_pkt.vers_ihl = 0x45;
351 ip_pkt.pkt_len = sizeof (packet); /* Workaround PR kern/21737 */
353 ip_pkt.pkt_len = htons (sizeof (packet));
355 ip_pkt.id = htons (PACKET_ID);
356 ip_pkt.flags_frag_offset = 0;
357 ip_pkt.ttl = IPDEFTTL;
358 ip_pkt.proto = IPPROTO_ICMP;
360 ip_pkt.src_ip = my_ip->s_addr;
361 ip_pkt.dst_ip = other->s_addr;
363 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
364 GNUNET_memcpy (&packet[off],
366 sizeof (struct ip_header));
367 off = sizeof (ip_pkt);
369 /* icmp reply: time exceeded */
370 icmp_ttl.type = ICMP_TIME_EXCEEDED;
372 icmp_ttl.checksum = 0;
374 GNUNET_memcpy (&packet[off],
376 sizeof (struct icmp_ttl_exceeded_header));
377 off += sizeof (struct icmp_ttl_exceeded_header);
379 /* ip header of the presumably 'lost' udp packet */
380 ip_pkt.vers_ihl = 0x45;
383 htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
384 ip_pkt.id = htons (PACKET_ID);
385 ip_pkt.flags_frag_offset = 0;
386 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
387 ip_pkt.proto = IPPROTO_ICMP;
388 ip_pkt.src_ip = other->s_addr;
389 ip_pkt.dst_ip = dummy.s_addr;
392 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
393 GNUNET_memcpy (&packet[off],
395 sizeof (struct ip_header));
396 off += sizeof (struct ip_header);
398 icmp_echo.type = ICMP_ECHO;
400 icmp_echo.reserved = htonl (port);
401 icmp_echo.checksum = 0;
404 ((uint16_t *) &icmp_echo, sizeof (struct icmp_echo_header)));
405 GNUNET_memcpy (&packet[off],
407 sizeof (struct icmp_echo_header));
409 /* no go back to calculate ICMP packet checksum */
410 off = sizeof (struct ip_header);
413 ((uint16_t *) & packet[off],
414 sizeof (struct icmp_ttl_exceeded_header) +
415 sizeof (struct ip_header) + sizeof (struct icmp_echo_header)));
416 GNUNET_memcpy (&packet[off],
418 sizeof (struct icmp_ttl_exceeded_header));
420 /* prepare for transmission */
421 memset (&dst, 0, sizeof (dst));
422 dst.sin_family = AF_INET;
423 #if HAVE_SOCKADDR_IN_SIN_LEN
424 dst.sin_len = sizeof (struct sockaddr_in);
426 dst.sin_addr = *other;
428 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
432 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
434 else if (sizeof (packet) != (size_t) err)
436 fprintf (stderr, "Error: partial send of ICMP message\n");
442 main (int argc, char *const *argv)
445 struct in_addr external;
446 struct in_addr target;
452 /* Create an ICMP raw socket for writing (only operation that requires root) */
453 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
454 raw_eno = errno; /* for later error checking */
456 /* now drop root privileges */
458 #ifdef HAVE_SETRESUID
459 if (0 != setresuid (uid, uid, uid))
461 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
466 if (0 != (setuid (uid) | seteuid (uid)))
468 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
475 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno));
480 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
482 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
487 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
489 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
497 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
501 if ((1 != inet_pton (AF_INET, argv[1], &external)) ||
502 (1 != inet_pton (AF_INET, argv[2], &target)))
504 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
508 if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
510 fprintf (stderr, "Error parsing port value `%s'\n", argv[3]);
515 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
517 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
521 send_icmp (&external, &target);
522 send_icmp_udp (&external, &target);
526 (void) close (rawsock);
530 /* end of gnunet-helper-nat-client.c */