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-server.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 (or maybe BSDs, but never W32)
25 * @author Christian Grothoff
27 * This program will send ONE ICMP message every 500 ms RAW sockets
28 * to a DUMMY IP address and also listens for ICMP replies. 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 two RAW sockets 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)
43 * - Jacob Appelbaum (19 Dec 2011)
46 /* Just needed for HAVE_SOCKADDR_IN_SIN_LEN test macro! */
47 #include "gnunet_config.h"
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <arpa/inet.h>
54 #include <sys/select.h>
56 #include <sys/types.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_icmp.h>
66 #include <netinet/in.h>
68 /* The following constant is missing from FreeBSD 9.2 */
69 #ifndef ICMP_TIME_EXCEEDED
70 #define ICMP_TIME_EXCEEDED 11
74 * Call memcpy() but check for @a n being 0 first. In the latter
75 * case, it is now safe to pass NULL for @a src or @a dst.
76 * Unlike traditional memcpy(), returns nothing.
78 * @param dst destination of the copy, may be NULL if @a n is zero
79 * @param src source of the copy, may be NULL if @a n is zero
80 * @param n number of bytes to copy
82 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
85 * Should we print some debug output?
90 * Must match packet ID used by gnunet-helper-nat-client.c
95 * Must match IP given in the client.
97 #define DUMMY_IP "192.0.2.86"
102 #define NAT_TRAV_PORT 22225
105 * How often do we send our ICMP messages to receive replies?
107 #define ICMP_SEND_FREQUENCY_MS 500
116 * Version (4 bits) + Internet header length (4 bits)
136 * Flags (3 bits) + Fragment offset (13 bits)
138 uint16_t flags_frag_offset;
161 * Destination address
167 * Format of ICMP packet.
169 struct icmp_ttl_exceeded_header
179 /* followed by original payload */
182 struct icmp_echo_header
195 * Beginning of UDP packet.
209 * Socket we use to receive "fake" ICMP replies.
214 * Socket we use to send our ICMP requests.
219 * Socket we use to send our UDP requests.
224 * Target "dummy" address.
226 static struct in_addr dummy;
230 * CRC-16 for IP/ICMP headers.
232 * @param data what to calculate the CRC over
233 * @param bytes number of bytes in data (must be multiple of 2)
234 * @return the CRC 16.
237 calc_checksum (const uint16_t * data, unsigned int bytes)
243 for (i = 0; i < bytes / 2; i++)
245 sum = (sum & 0xffff) + (sum >> 16);
246 sum = htons (0xffff - sum);
252 * Send an ICMP message to the dummy IP.
254 * @param my_ip source address (our ip address)
257 send_icmp_echo (const struct in_addr *my_ip)
259 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
260 struct icmp_echo_header icmp_echo;
261 struct ip_header ip_pkt;
262 struct sockaddr_in dst;
267 ip_pkt.vers_ihl = 0x45;
269 ip_pkt.pkt_len = htons (sizeof (packet));
270 ip_pkt.id = htons (PACKET_ID);
271 ip_pkt.flags_frag_offset = 0;
272 ip_pkt.ttl = IPDEFTTL;
273 ip_pkt.proto = IPPROTO_ICMP;
275 ip_pkt.src_ip = my_ip->s_addr;
276 ip_pkt.dst_ip = dummy.s_addr;
278 htons (calc_checksum ((uint16_t *) & ip_pkt,
279 sizeof (struct ip_header)));
280 GNUNET_memcpy (&packet[off],
282 sizeof (struct ip_header));
283 off += sizeof (struct ip_header);
285 icmp_echo.type = ICMP_ECHO;
287 icmp_echo.checksum = 0;
288 icmp_echo.reserved = 0;
291 ((uint16_t *) & icmp_echo,
292 sizeof (struct icmp_echo_header)));
293 GNUNET_memcpy (&packet[off],
295 sizeof (struct icmp_echo_header));
296 off += sizeof (struct icmp_echo_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 = dummy;
304 err = sendto (rawsock,
308 (struct sockaddr *) &dst,
314 "sendto failed: %s\n",
318 else if (sizeof (packet) != err)
321 "Error: partial send of ICMP message\n");
327 * Send a UDP message to the dummy IP.
332 struct sockaddr_in dst;
335 memset (&dst, 0, sizeof (dst));
336 dst.sin_family = AF_INET;
337 #if HAVE_SOCKADDR_IN_SIN_LEN
338 dst.sin_len = sizeof (struct sockaddr_in);
340 dst.sin_addr = dummy;
341 dst.sin_port = htons (NAT_TRAV_PORT);
342 err = sendto (udpsock,
346 (struct sockaddr *) &dst,
352 "sendto failed: %s\n",
359 "Error: partial send of ICMP message\n");
365 * We've received an ICMP response. Process it.
368 process_icmp_response ()
372 struct in_addr source_ip;
373 struct ip_header ip_pkt;
374 struct icmp_ttl_exceeded_header icmp_ttl;
375 struct icmp_echo_header icmp_echo;
376 struct udp_header udp_pkt;
380 have = read (icmpsock, buf, sizeof (buf));
384 "Error reading raw socket: %s\n",
390 "Received message of %u bytes\n",
391 (unsigned int) have);
394 (ssize_t) (sizeof (struct ip_header) +
395 sizeof (struct icmp_ttl_exceeded_header) +
396 sizeof (struct ip_header)))
402 GNUNET_memcpy (&ip_pkt,
404 sizeof (struct ip_header));
405 off += sizeof (struct ip_header);
406 GNUNET_memcpy (&icmp_ttl,
408 sizeof (struct icmp_ttl_exceeded_header));
409 off += sizeof (struct icmp_ttl_exceeded_header);
410 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
412 /* different type than what we want */
415 /* grab source IP of 1st IP header */
416 source_ip.s_addr = ip_pkt.src_ip;
418 /* skip 2nd IP header */
419 GNUNET_memcpy (&ip_pkt,
421 sizeof (struct ip_header));
422 off += sizeof (struct ip_header);
424 switch (ip_pkt.proto)
428 (sizeof (struct ip_header) * 2 +
429 sizeof (struct icmp_ttl_exceeded_header) +
430 sizeof (struct icmp_echo_header)))
435 /* grab ICMP ECHO content */
436 GNUNET_memcpy (&icmp_echo,
438 sizeof (struct icmp_echo_header));
439 port = (uint16_t) ntohl (icmp_echo.reserved);
443 (sizeof (struct ip_header) * 2 +
444 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
449 /* grab UDP content */
450 GNUNET_memcpy (&udp_pkt,
452 sizeof (struct udp_header));
453 port = ntohs (udp_pkt.length);
456 /* different type than what we want */
461 fprintf (stdout, "%s\n",
462 inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)));
464 fprintf (stdout, "%s:%u\n",
465 inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)),
466 (unsigned int) port);
472 * Fully initialize the raw socket.
474 * @return -1 on error, 0 on success
489 "setsockopt failed: %s\n",
501 "setsockopt failed: %s\n",
510 * Create a UDP socket for writing.
512 * @param my_ip source address (our ip address)
513 * @return -1 on error
516 make_udp_socket (const struct in_addr *my_ip)
519 struct sockaddr_in addr;
521 ret = socket (AF_INET, SOCK_DGRAM, 0);
525 "Error opening UDP socket: %s\n",
529 memset (&addr, 0, sizeof (addr));
530 addr.sin_family = AF_INET;
531 #if HAVE_SOCKADDR_IN_SIN_LEN
532 addr.sin_len = sizeof (struct sockaddr_in);
534 addr.sin_addr = *my_ip;
535 addr.sin_port = htons (NAT_TRAV_PORT);
538 (struct sockaddr *) &addr,
542 "Error binding UDP socket to port %u: %s\n",
556 struct in_addr external;
565 /* Create an ICMP raw socket for reading (we'll check errors later) */
566 icmpsock = socket (AF_INET,
571 /* Create an (ICMP) raw socket for writing (we'll check errors later) */
572 rawsock = socket (AF_INET,
578 /* drop root rights */
580 #ifdef HAVE_SETRESUID
581 if (0 != setresuid (uid, uid, uid))
584 "Failed to setresuid: %s\n",
590 if (0 != (setuid (uid) | seteuid (uid)))
593 "Failed to setuid: %s\n",
600 /* Now that we run without root rights, we can do error checking... */
604 "This program must be started with our (internal NAT) IP as the only argument.\n");
608 if (1 != inet_pton (AF_INET, argv[1], &external))
611 "Error parsing IPv4 address: %s\n",
616 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
619 "Internal error converting dummy IP to binary.\n");
624 /* error checking icmpsock */
628 "Error opening RAW socket: %s\n",
629 strerror (icmp_eno));
633 if (icmpsock >= FD_SETSIZE)
635 /* this could happen if we were started with a large number of already-open
636 file descriptors... */
638 "Socket number too large (%d > %u)\n",
640 (unsigned int) FD_SETSIZE);
645 /* error checking rawsock */
649 "Error opening RAW socket: %s\n",
654 /* no need to check 'rawsock' against FD_SETSIZE as it is never used
657 if (0 != setup_raw_socket ())
663 if (-1 == (udpsock = make_udp_socket (&external)))
673 FD_SET (icmpsock, &rs);
675 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
676 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
681 "select failed: %s\n",
685 if (1 == getppid ()) /* Check the parent process id, if 1 the parent has died, so we should die too */
687 if (FD_ISSET (icmpsock, &rs))
689 process_icmp_response ();
692 if (0 == (++alt % 2))
693 send_icmp_echo (&external);
698 /* select failed (internal error or OS out of resources) */
702 (void) close (icmpsock);
704 (void) close (rawsock);
706 (void) close (udpsock);
711 /* end of gnunet-helper-nat-server.c */