2 This file is part of GNUnet.
3 Copyright (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>
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 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
255 off += sizeof (struct ip_header);
257 icmp_pkt.type = ICMP_TIME_EXCEEDED;
259 icmp_pkt.checksum = 0;
261 memcpy (&packet[off], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header));
262 off += sizeof (struct icmp_ttl_exceeded_header);
264 /* ip header of the presumably 'lost' udp packet */
265 ip_pkt.vers_ihl = 0x45;
268 htons (sizeof (struct ip_header) + sizeof (struct udp_header));
269 ip_pkt.id = htons (0);
270 ip_pkt.flags_frag_offset = 0;
272 ip_pkt.proto = IPPROTO_UDP;
274 ip_pkt.src_ip = other->s_addr;
275 ip_pkt.dst_ip = dummy.s_addr;
277 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
278 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
279 off += sizeof (struct ip_header);
281 /* build UDP header */
282 udp_pkt.src_port = htons (NAT_TRAV_PORT);
283 udp_pkt.dst_port = htons (NAT_TRAV_PORT);
284 udp_pkt.length = htons (port);
286 memcpy (&packet[off], &udp_pkt, sizeof (struct udp_header));
287 off += sizeof (struct udp_header);
289 /* set ICMP checksum */
292 ((uint16_t *) & packet[sizeof (struct ip_header)],
293 sizeof (struct icmp_ttl_exceeded_header) +
294 sizeof (struct ip_header) + sizeof (struct udp_header)));
295 memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt,
296 sizeof (struct icmp_ttl_exceeded_header));
298 memset (&dst, 0, sizeof (dst));
299 dst.sin_family = AF_INET;
300 #if HAVE_SOCKADDR_IN_SIN_LEN
301 dst.sin_len = sizeof (struct sockaddr_in);
303 dst.sin_addr = *other;
305 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
309 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
311 else if (sizeof (packet) != (size_t) err)
313 fprintf (stderr, "Error: partial send of ICMP message with size %lu\n", (unsigned long) off);
319 * Send an ICMP message to the target.
321 * @param my_ip source address
322 * @param other target address
325 send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
327 struct ip_header ip_pkt;
328 struct icmp_ttl_exceeded_header icmp_ttl;
329 struct icmp_echo_header icmp_echo;
330 struct sockaddr_in dst;
331 char packet[sizeof (struct ip_header) * 2 +
332 sizeof (struct icmp_ttl_exceeded_header) +
333 sizeof (struct icmp_echo_header)];
337 /* ip header: send to (known) ip address */
339 ip_pkt.vers_ihl = 0x45;
342 ip_pkt.pkt_len = sizeof (packet); /* Workaround PR kern/21737 */
344 ip_pkt.pkt_len = htons (sizeof (packet));
346 ip_pkt.id = htons (PACKET_ID);
347 ip_pkt.flags_frag_offset = 0;
348 ip_pkt.ttl = IPDEFTTL;
349 ip_pkt.proto = IPPROTO_ICMP;
351 ip_pkt.src_ip = my_ip->s_addr;
352 ip_pkt.dst_ip = other->s_addr;
354 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
355 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
356 off = sizeof (ip_pkt);
358 /* icmp reply: time exceeded */
359 icmp_ttl.type = ICMP_TIME_EXCEEDED;
361 icmp_ttl.checksum = 0;
363 memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
364 off += sizeof (struct icmp_ttl_exceeded_header);
366 /* ip header of the presumably 'lost' udp packet */
367 ip_pkt.vers_ihl = 0x45;
370 htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
371 ip_pkt.id = htons (PACKET_ID);
372 ip_pkt.flags_frag_offset = 0;
373 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
374 ip_pkt.proto = IPPROTO_ICMP;
375 ip_pkt.src_ip = other->s_addr;
376 ip_pkt.dst_ip = dummy.s_addr;
379 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
380 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
381 off += sizeof (struct ip_header);
383 icmp_echo.type = ICMP_ECHO;
385 icmp_echo.reserved = htonl (port);
386 icmp_echo.checksum = 0;
389 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
390 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
392 /* no go back to calculate ICMP packet checksum */
393 off = sizeof (struct ip_header);
396 ((uint16_t *) & packet[off],
397 sizeof (struct icmp_ttl_exceeded_header) +
398 sizeof (struct ip_header) + sizeof (struct icmp_echo_header)));
399 memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
401 /* prepare for transmission */
402 memset (&dst, 0, sizeof (dst));
403 dst.sin_family = AF_INET;
404 #if HAVE_SOCKADDR_IN_SIN_LEN
405 dst.sin_len = sizeof (struct sockaddr_in);
407 dst.sin_addr = *other;
409 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
413 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
415 else if (sizeof (packet) != (size_t) err)
417 fprintf (stderr, "Error: partial send of ICMP message\n");
423 main (int argc, char *const *argv)
426 struct in_addr external;
427 struct in_addr target;
433 /* Create an ICMP raw socket for writing (only operation that requires root) */
434 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
435 raw_eno = errno; /* for later error checking */
437 /* now drop root privileges */
439 #ifdef HAVE_SETRESUID
440 if (0 != setresuid (uid, uid, uid))
442 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
447 if (0 != (setuid (uid) | seteuid (uid)))
449 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
456 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno));
461 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
463 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
468 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
470 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
478 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
482 if ((1 != inet_pton (AF_INET, argv[1], &external)) ||
483 (1 != inet_pton (AF_INET, argv[2], &target)))
485 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
489 if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
491 fprintf (stderr, "Error parsing port value `%s'\n", argv[3]);
496 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
498 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
502 send_icmp (&external, &target);
503 send_icmp_udp (&external, &target);
507 (void) close (rawsock);
511 /* end of gnunet-helper-nat-client.c */