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
78 * Version (4 bits) + Internet header length (4 bits)
98 * Flags (3 bits) + Fragment offset (13 bits)
100 uint16_t flags_frag_offset;
123 * Destination address
129 * Format of ICMP packet.
131 struct icmp_ttl_exceeded_header
141 /* followed by original payload */
144 struct icmp_echo_header
156 * Beginning of UDP packet.
170 * Socket we use to send our fake ICMP replies.
175 * Target "dummy" address of the packet we pretend to respond to.
177 static struct in_addr dummy;
182 static uint16_t port;
186 * CRC-16 for IP/ICMP headers.
188 * @param data what to calculate the CRC over
189 * @param bytes number of bytes in data (must be multiple of 2)
190 * @return the CRC 16.
193 calc_checksum (const uint16_t * data, unsigned int bytes)
199 for (i = 0; i < bytes / 2; i++)
201 sum = (sum & 0xffff) + (sum >> 16);
202 sum = htons (0xffff - sum);
208 * Send an ICMP message to the target.
210 * @param my_ip source address
211 * @param other target address
214 send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
216 char packet[sizeof (struct ip_header) * 2 +
217 sizeof (struct icmp_ttl_exceeded_header) +
218 sizeof (struct udp_header)];
219 struct ip_header ip_pkt;
220 struct icmp_ttl_exceeded_header icmp_pkt;
221 struct udp_header udp_pkt;
222 struct sockaddr_in dst;
226 /* ip header: send to (known) ip address */
228 ip_pkt.vers_ihl = 0x45;
230 ip_pkt.pkt_len = htons (sizeof (packet));
231 ip_pkt.id = htons (256);
232 ip_pkt.flags_frag_offset = 0;
234 ip_pkt.proto = IPPROTO_ICMP;
236 ip_pkt.src_ip = my_ip->s_addr;
237 ip_pkt.dst_ip = other->s_addr;
239 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
240 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
241 off += sizeof (struct ip_header);
243 icmp_pkt.type = ICMP_TIME_EXCEEDED;
245 icmp_pkt.checksum = 0;
247 memcpy (&packet[off], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header));
248 off += sizeof (struct icmp_ttl_exceeded_header);
250 /* ip header of the presumably 'lost' udp packet */
251 ip_pkt.vers_ihl = 0x45;
254 htons (sizeof (struct ip_header) + sizeof (struct udp_header));
255 ip_pkt.id = htons (0);
256 ip_pkt.flags_frag_offset = 0;
258 ip_pkt.proto = IPPROTO_UDP;
260 ip_pkt.src_ip = other->s_addr;
261 ip_pkt.dst_ip = dummy.s_addr;
263 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
264 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
265 off += sizeof (struct ip_header);
267 /* build UDP header */
268 udp_pkt.src_port = htons (NAT_TRAV_PORT);
269 udp_pkt.dst_port = htons (NAT_TRAV_PORT);
270 udp_pkt.length = htons (port);
272 memcpy (&packet[off], &udp_pkt, sizeof (struct udp_header));
273 off += sizeof (struct udp_header);
275 /* set ICMP checksum */
278 ((uint16_t *) & packet[sizeof (struct ip_header)],
279 sizeof (struct icmp_ttl_exceeded_header) +
280 sizeof (struct ip_header) + sizeof (struct udp_header)));
281 memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt,
282 sizeof (struct icmp_ttl_exceeded_header));
284 memset (&dst, 0, sizeof (dst));
285 dst.sin_family = AF_INET;
286 #if HAVE_SOCKADDR_IN_SIN_LEN
287 dst.sin_len = sizeof (struct sockaddr_in);
289 dst.sin_addr = *other;
291 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
295 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
297 else if (sizeof (packet) != (size_t) err)
299 fprintf (stderr, "Error: partial send of ICMP message\n");
305 * Send an ICMP message to the target.
307 * @param my_ip source address
308 * @param other target address
311 send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
313 struct ip_header ip_pkt;
314 struct icmp_ttl_exceeded_header icmp_ttl;
315 struct icmp_echo_header icmp_echo;
316 struct sockaddr_in dst;
317 char packet[sizeof (struct ip_header) * 2 +
318 sizeof (struct icmp_ttl_exceeded_header) +
319 sizeof (struct icmp_echo_header)];
323 /* ip header: send to (known) ip address */
325 ip_pkt.vers_ihl = 0x45;
327 ip_pkt.pkt_len = htons (sizeof (packet));
328 ip_pkt.id = htons (256);
329 ip_pkt.flags_frag_offset = 0;
330 ip_pkt.ttl = IPDEFTTL;
331 ip_pkt.proto = IPPROTO_ICMP;
333 ip_pkt.src_ip = my_ip->s_addr;
334 ip_pkt.dst_ip = other->s_addr;
336 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
337 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
338 off = sizeof (ip_pkt);
340 /* icmp reply: time exceeded */
341 icmp_ttl.type = ICMP_TIME_EXCEEDED;
343 icmp_ttl.checksum = 0;
345 memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
346 off += sizeof (struct icmp_ttl_exceeded_header);
348 /* ip header of the presumably 'lost' udp packet */
349 ip_pkt.vers_ihl = 0x45;
352 htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
353 ip_pkt.id = htons (256);
354 ip_pkt.flags_frag_offset = 0;
355 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
356 ip_pkt.proto = IPPROTO_ICMP;
357 ip_pkt.src_ip = other->s_addr;
358 ip_pkt.dst_ip = dummy.s_addr;
361 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
362 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
363 off += sizeof (struct ip_header);
365 icmp_echo.type = ICMP_ECHO;
367 icmp_echo.reserved = htonl (port);
368 icmp_echo.checksum = 0;
371 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
372 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
374 /* no go back to calculate ICMP packet checksum */
375 off = sizeof (struct ip_header);
378 ((uint16_t *) & packet[off],
379 sizeof (struct icmp_ttl_exceeded_header) +
380 sizeof (struct ip_header) + sizeof (struct icmp_echo_header)));
381 memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
383 /* prepare for transmission */
384 memset (&dst, 0, sizeof (dst));
385 dst.sin_family = AF_INET;
386 #if HAVE_SOCKADDR_IN_SIN_LEN
387 dst.sin_len = sizeof (struct sockaddr_in);
389 dst.sin_addr = *other;
391 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
395 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
397 else if (sizeof (packet) != (size_t) err)
399 fprintf (stderr, "Error: partial send of ICMP message\n");
405 * Create an ICMP raw socket for writing.
407 * @return -1 on error
415 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
418 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
422 setsockopt (ret, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
424 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
429 setsockopt (ret, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
431 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
440 main (int argc, char *const *argv)
442 struct in_addr external;
443 struct in_addr target;
450 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
453 if ((1 != inet_pton (AF_INET, argv[1], &external)) ||
454 (1 != inet_pton (AF_INET, argv[2], &target)))
456 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
459 if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
461 fprintf (stderr, "Error parsing port value `%s'\n", argv[3]);
465 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
467 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
470 if (-1 == (rawsock = make_raw_socket ()))
473 if (0 != setresuid (uid, uid, uid))
475 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
476 /* not critical, continue anyway */
478 send_icmp (&external, &target);
479 send_icmp_udp (&external, &target);
484 /* end of gnunet-helper-nat-client.c */