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, \
83 * Must match IP given in the server.
85 #define DUMMY_IP "192.0.2.86"
87 #define NAT_TRAV_PORT 22225
90 * Must match packet ID used by gnunet-helper-nat-server.c
100 * Version (4 bits) + Internet header length (4 bits)
120 * Flags (3 bits) + Fragment offset (13 bits)
122 uint16_t flags_frag_offset;
145 * Destination address
151 * Format of ICMP packet.
153 struct icmp_ttl_exceeded_header
163 /* followed by original payload */
166 struct icmp_echo_header
178 * Beginning of UDP packet.
192 * Socket we use to send our fake ICMP replies.
197 * Target "dummy" address of the packet we pretend to respond to.
199 static struct in_addr dummy;
204 static uint16_t port;
208 * CRC-16 for IP/ICMP headers.
210 * @param data what to calculate the CRC over
211 * @param bytes number of bytes in data (must be multiple of 2)
212 * @return the CRC 16.
215 calc_checksum (const uint16_t *data, unsigned int bytes)
221 for (i = 0; i < bytes / 2; i++)
223 sum = (sum & 0xffff) + (sum >> 16);
224 sum = htons (0xffff - sum);
230 * Send an ICMP message to the target.
232 * @param my_ip source address
233 * @param other target address
236 send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
238 char packet[sizeof(struct ip_header) * 2
239 + sizeof(struct icmp_ttl_exceeded_header)
240 + sizeof(struct udp_header)];
241 struct ip_header ip_pkt;
242 struct icmp_ttl_exceeded_header icmp_pkt;
243 struct udp_header udp_pkt;
244 struct sockaddr_in dst;
248 /* ip header: send to (known) ip address */
250 ip_pkt.vers_ihl = 0x45;
252 /* should this be BSD only? */
253 #if defined(BSD) && defined(__FreeBSD__) && defined(__FreeBSD_kernel__)
254 ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */
256 ip_pkt.pkt_len = htons (sizeof(packet));
258 ip_pkt.id = htons (PACKET_ID);
259 ip_pkt.flags_frag_offset = 0;
261 ip_pkt.proto = IPPROTO_ICMP;
263 ip_pkt.src_ip = my_ip->s_addr;
264 ip_pkt.dst_ip = other->s_addr;
266 htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header)));
267 GNUNET_memcpy (&packet[off],
269 sizeof(struct ip_header));
270 off += sizeof(struct ip_header);
272 icmp_pkt.type = ICMP_TIME_EXCEEDED;
274 icmp_pkt.checksum = 0;
276 GNUNET_memcpy (&packet[off],
278 sizeof(struct icmp_ttl_exceeded_header));
279 off += sizeof(struct icmp_ttl_exceeded_header);
281 /* ip header of the presumably 'lost' udp packet */
282 ip_pkt.vers_ihl = 0x45;
285 htons (sizeof(struct ip_header) + sizeof(struct udp_header));
286 ip_pkt.id = htons (0);
287 ip_pkt.flags_frag_offset = 0;
289 ip_pkt.proto = IPPROTO_UDP;
291 ip_pkt.src_ip = other->s_addr;
292 ip_pkt.dst_ip = dummy.s_addr;
294 htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header)));
295 GNUNET_memcpy (&packet[off],
297 sizeof(struct ip_header));
298 off += sizeof(struct ip_header);
300 /* build UDP header */
301 udp_pkt.src_port = htons (NAT_TRAV_PORT);
302 udp_pkt.dst_port = htons (NAT_TRAV_PORT);
303 udp_pkt.length = htons (port);
305 GNUNET_memcpy (&packet[off],
307 sizeof(struct udp_header));
308 off += sizeof(struct udp_header);
310 /* set ICMP checksum */
313 ((uint16_t *) &packet[sizeof(struct ip_header)],
314 sizeof(struct icmp_ttl_exceeded_header)
315 + sizeof(struct ip_header) + sizeof(struct udp_header)));
316 GNUNET_memcpy (&packet[sizeof(struct ip_header)],
318 sizeof(struct icmp_ttl_exceeded_header));
320 memset (&dst, 0, sizeof(dst));
321 dst.sin_family = AF_INET;
322 #if HAVE_SOCKADDR_IN_SIN_LEN
323 dst.sin_len = sizeof(struct sockaddr_in);
325 dst.sin_addr = *other;
327 sendto (rawsock, packet, sizeof(packet), 0, (struct sockaddr *) &dst,
331 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
333 else if (sizeof(packet) != (size_t) err)
335 fprintf (stderr, "Error: partial send of ICMP message with size %lu\n",
336 (unsigned long) off);
342 * Send an ICMP message to the target.
344 * @param my_ip source address
345 * @param other target address
348 send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
350 struct ip_header ip_pkt;
351 struct icmp_ttl_exceeded_header icmp_ttl;
352 struct icmp_echo_header icmp_echo;
353 struct sockaddr_in dst;
354 char packet[sizeof(struct ip_header) * 2
355 + sizeof(struct icmp_ttl_exceeded_header)
356 + sizeof(struct icmp_echo_header)];
360 /* ip header: send to (known) ip address */
362 ip_pkt.vers_ihl = 0x45;
364 #if defined(BSD) && defined(__FreeBSD__) && defined(__FreeBSD_kernel__)
365 ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */
367 ip_pkt.pkt_len = htons (sizeof(packet));
369 ip_pkt.id = htons (PACKET_ID);
370 ip_pkt.flags_frag_offset = 0;
371 ip_pkt.ttl = IPDEFTTL;
372 ip_pkt.proto = IPPROTO_ICMP;
374 ip_pkt.src_ip = my_ip->s_addr;
375 ip_pkt.dst_ip = other->s_addr;
377 htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header)));
378 GNUNET_memcpy (&packet[off],
380 sizeof(struct ip_header));
381 off = sizeof(ip_pkt);
383 /* icmp reply: time exceeded */
384 icmp_ttl.type = ICMP_TIME_EXCEEDED;
386 icmp_ttl.checksum = 0;
388 GNUNET_memcpy (&packet[off],
390 sizeof(struct icmp_ttl_exceeded_header));
391 off += sizeof(struct icmp_ttl_exceeded_header);
393 /* ip header of the presumably 'lost' udp packet */
394 ip_pkt.vers_ihl = 0x45;
397 htons (sizeof(struct ip_header) + sizeof(struct icmp_echo_header));
398 ip_pkt.id = htons (PACKET_ID);
399 ip_pkt.flags_frag_offset = 0;
400 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
401 ip_pkt.proto = IPPROTO_ICMP;
402 ip_pkt.src_ip = other->s_addr;
403 ip_pkt.dst_ip = dummy.s_addr;
406 htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header)));
407 GNUNET_memcpy (&packet[off],
409 sizeof(struct ip_header));
410 off += sizeof(struct ip_header);
412 icmp_echo.type = ICMP_ECHO;
414 icmp_echo.reserved = htonl (port);
415 icmp_echo.checksum = 0;
418 ((uint16_t *) &icmp_echo, sizeof(struct icmp_echo_header)));
419 GNUNET_memcpy (&packet[off],
421 sizeof(struct icmp_echo_header));
423 /* no go back to calculate ICMP packet checksum */
424 off = sizeof(struct ip_header);
427 ((uint16_t *) &packet[off],
428 sizeof(struct icmp_ttl_exceeded_header)
429 + sizeof(struct ip_header) + sizeof(struct icmp_echo_header)));
430 GNUNET_memcpy (&packet[off],
432 sizeof(struct icmp_ttl_exceeded_header));
434 /* prepare for transmission */
435 memset (&dst, 0, sizeof(dst));
436 dst.sin_family = AF_INET;
437 #if HAVE_SOCKADDR_IN_SIN_LEN
438 dst.sin_len = sizeof(struct sockaddr_in);
440 dst.sin_addr = *other;
442 sendto (rawsock, packet, sizeof(packet), 0, (struct sockaddr *) &dst,
446 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
448 else if (sizeof(packet) != (size_t) err)
450 fprintf (stderr, "Error: partial send of ICMP message\n");
456 main (int argc, char *const *argv)
459 struct in_addr external;
460 struct in_addr target;
466 /* Create an ICMP raw socket for writing (only operation that requires root) */
467 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
468 raw_eno = errno; /* for later error checking */
470 /* now drop root privileges */
472 #ifdef HAVE_SETRESUID
473 if (0 != setresuid (uid, uid, uid))
475 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
480 if (0 != (setuid (uid) | seteuid (uid)))
482 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
489 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno));
494 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one,
497 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
502 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof(one)))
504 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
512 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
516 if ((1 != inet_pton (AF_INET, argv[1], &external)) ||
517 (1 != inet_pton (AF_INET, argv[2], &target)))
519 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
523 if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
525 fprintf (stderr, "Error parsing port value `%s'\n", argv[3]);
530 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
532 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
536 send_icmp (&external, &target);
537 send_icmp_udp (&external, &target);
541 (void) close (rawsock);
546 /* end of gnunet-helper-nat-client.c */