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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 * Call memcpy() but check for @a n being 0 first. In the latter
71 * case, it is now safe to pass NULL for @a src or @a dst.
72 * Unlike traditional memcpy(), returns nothing.
74 * @param dst destination of the copy, may be NULL if @a n is zero
75 * @param src source of the copy, may be NULL if @a n is zero
76 * @param n number of bytes to copy
78 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
81 * Must match IP given in the server.
83 #define DUMMY_IP "192.0.2.86"
85 #define NAT_TRAV_PORT 22225
88 * Must match packet ID used by gnunet-helper-nat-server.c
99 * Version (4 bits) + Internet header length (4 bits)
119 * Flags (3 bits) + Fragment offset (13 bits)
121 uint16_t flags_frag_offset;
144 * Destination address
150 * Format of ICMP packet.
152 struct icmp_ttl_exceeded_header
162 /* followed by original payload */
165 struct icmp_echo_header
177 * Beginning of UDP packet.
191 * Socket we use to send our fake ICMP replies.
196 * Target "dummy" address of the packet we pretend to respond to.
198 static struct in_addr dummy;
203 static uint16_t port;
207 * CRC-16 for IP/ICMP headers.
209 * @param data what to calculate the CRC over
210 * @param bytes number of bytes in data (must be multiple of 2)
211 * @return the CRC 16.
214 calc_checksum (const uint16_t * data, unsigned int bytes)
220 for (i = 0; i < bytes / 2; i++)
222 sum = (sum & 0xffff) + (sum >> 16);
223 sum = htons (0xffff - sum);
229 * Send an ICMP message to the target.
231 * @param my_ip source address
232 * @param other target address
235 send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
237 char packet[sizeof (struct ip_header) * 2 +
238 sizeof (struct icmp_ttl_exceeded_header) +
239 sizeof (struct udp_header)];
240 struct ip_header ip_pkt;
241 struct icmp_ttl_exceeded_header icmp_pkt;
242 struct udp_header udp_pkt;
243 struct sockaddr_in dst;
247 /* ip header: send to (known) ip address */
249 ip_pkt.vers_ihl = 0x45;
252 ip_pkt.pkt_len = sizeof (packet); /* Workaround PR kern/21737 */
254 ip_pkt.pkt_len = htons (sizeof (packet));
256 ip_pkt.id = htons (PACKET_ID);
257 ip_pkt.flags_frag_offset = 0;
259 ip_pkt.proto = IPPROTO_ICMP;
261 ip_pkt.src_ip = my_ip->s_addr;
262 ip_pkt.dst_ip = other->s_addr;
264 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
265 GNUNET_memcpy (&packet[off],
267 sizeof (struct ip_header));
268 off += sizeof (struct ip_header);
270 icmp_pkt.type = ICMP_TIME_EXCEEDED;
272 icmp_pkt.checksum = 0;
274 GNUNET_memcpy (&packet[off],
276 sizeof (struct icmp_ttl_exceeded_header));
277 off += sizeof (struct icmp_ttl_exceeded_header);
279 /* ip header of the presumably 'lost' udp packet */
280 ip_pkt.vers_ihl = 0x45;
283 htons (sizeof (struct ip_header) + sizeof (struct udp_header));
284 ip_pkt.id = htons (0);
285 ip_pkt.flags_frag_offset = 0;
287 ip_pkt.proto = IPPROTO_UDP;
289 ip_pkt.src_ip = other->s_addr;
290 ip_pkt.dst_ip = dummy.s_addr;
292 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
293 GNUNET_memcpy (&packet[off],
295 sizeof (struct ip_header));
296 off += sizeof (struct ip_header);
298 /* build UDP header */
299 udp_pkt.src_port = htons (NAT_TRAV_PORT);
300 udp_pkt.dst_port = htons (NAT_TRAV_PORT);
301 udp_pkt.length = htons (port);
303 GNUNET_memcpy (&packet[off],
305 sizeof (struct udp_header));
306 off += sizeof (struct udp_header);
308 /* set ICMP checksum */
311 ((uint16_t *) & packet[sizeof (struct ip_header)],
312 sizeof (struct icmp_ttl_exceeded_header) +
313 sizeof (struct ip_header) + sizeof (struct udp_header)));
314 GNUNET_memcpy (&packet[sizeof (struct ip_header)],
316 sizeof (struct icmp_ttl_exceeded_header));
318 memset (&dst, 0, sizeof (dst));
319 dst.sin_family = AF_INET;
320 #if HAVE_SOCKADDR_IN_SIN_LEN
321 dst.sin_len = sizeof (struct sockaddr_in);
323 dst.sin_addr = *other;
325 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
329 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
331 else if (sizeof (packet) != (size_t) err)
333 fprintf (stderr, "Error: partial send of ICMP message with size %lu\n", (unsigned long) off);
339 * Send an ICMP message to the target.
341 * @param my_ip source address
342 * @param other target address
345 send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
347 struct ip_header ip_pkt;
348 struct icmp_ttl_exceeded_header icmp_ttl;
349 struct icmp_echo_header icmp_echo;
350 struct sockaddr_in dst;
351 char packet[sizeof (struct ip_header) * 2 +
352 sizeof (struct icmp_ttl_exceeded_header) +
353 sizeof (struct icmp_echo_header)];
357 /* ip header: send to (known) ip address */
359 ip_pkt.vers_ihl = 0x45;
362 ip_pkt.pkt_len = sizeof (packet); /* Workaround PR kern/21737 */
364 ip_pkt.pkt_len = htons (sizeof (packet));
366 ip_pkt.id = htons (PACKET_ID);
367 ip_pkt.flags_frag_offset = 0;
368 ip_pkt.ttl = IPDEFTTL;
369 ip_pkt.proto = IPPROTO_ICMP;
371 ip_pkt.src_ip = my_ip->s_addr;
372 ip_pkt.dst_ip = other->s_addr;
374 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
375 GNUNET_memcpy (&packet[off],
377 sizeof (struct ip_header));
378 off = sizeof (ip_pkt);
380 /* icmp reply: time exceeded */
381 icmp_ttl.type = ICMP_TIME_EXCEEDED;
383 icmp_ttl.checksum = 0;
385 GNUNET_memcpy (&packet[off],
387 sizeof (struct icmp_ttl_exceeded_header));
388 off += sizeof (struct icmp_ttl_exceeded_header);
390 /* ip header of the presumably 'lost' udp packet */
391 ip_pkt.vers_ihl = 0x45;
394 htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
395 ip_pkt.id = htons (PACKET_ID);
396 ip_pkt.flags_frag_offset = 0;
397 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
398 ip_pkt.proto = IPPROTO_ICMP;
399 ip_pkt.src_ip = other->s_addr;
400 ip_pkt.dst_ip = dummy.s_addr;
403 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
404 GNUNET_memcpy (&packet[off],
406 sizeof (struct ip_header));
407 off += sizeof (struct ip_header);
409 icmp_echo.type = ICMP_ECHO;
411 icmp_echo.reserved = htonl (port);
412 icmp_echo.checksum = 0;
415 ((uint16_t *) &icmp_echo, sizeof (struct icmp_echo_header)));
416 GNUNET_memcpy (&packet[off],
418 sizeof (struct icmp_echo_header));
420 /* no go back to calculate ICMP packet checksum */
421 off = sizeof (struct ip_header);
424 ((uint16_t *) & packet[off],
425 sizeof (struct icmp_ttl_exceeded_header) +
426 sizeof (struct ip_header) + sizeof (struct icmp_echo_header)));
427 GNUNET_memcpy (&packet[off],
429 sizeof (struct icmp_ttl_exceeded_header));
431 /* prepare for transmission */
432 memset (&dst, 0, sizeof (dst));
433 dst.sin_family = AF_INET;
434 #if HAVE_SOCKADDR_IN_SIN_LEN
435 dst.sin_len = sizeof (struct sockaddr_in);
437 dst.sin_addr = *other;
439 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
443 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
445 else if (sizeof (packet) != (size_t) err)
447 fprintf (stderr, "Error: partial send of ICMP message\n");
453 main (int argc, char *const *argv)
456 struct in_addr external;
457 struct in_addr target;
463 /* Create an ICMP raw socket for writing (only operation that requires root) */
464 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
465 raw_eno = errno; /* for later error checking */
467 /* now drop root privileges */
469 #ifdef HAVE_SETRESUID
470 if (0 != setresuid (uid, uid, uid))
472 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
477 if (0 != (setuid (uid) | seteuid (uid)))
479 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
486 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno));
491 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
493 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
498 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
500 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
508 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
512 if ((1 != inet_pton (AF_INET, argv[1], &external)) ||
513 (1 != inet_pton (AF_INET, argv[2], &target)))
515 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
519 if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
521 fprintf (stderr, "Error parsing port value `%s'\n", argv[3]);
526 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
528 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
532 send_icmp (&external, &target);
533 send_icmp_udp (&external, &target);
537 (void) close (rawsock);
541 /* end of gnunet-helper-nat-client.c */