2 This file is part of GNUnet.
3 Copyright (C) 2010, 2011, 2012 Christian Grothoff
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file exit/gnunet-helper-exit.c
24 * @brief the helper for exit nodes. Opens a virtual
25 * network-interface, sends data received on the if to stdout, sends
26 * data received on stdin to the interface. The code also enables
27 * IPv4/IPv6 forwarding and NAT on the current system (the latter on
28 * an interface specified on the command-line); these changes to the
29 * network configuration are NOT automatically undone when the program
30 * is stopped (this is because we cannot be sure that some other
31 * application didn't enable them before or after us; also, these
32 * changes should be mostly harmless as it simply turns the system
35 * @author Philipp Tölke
36 * @author Christian Grothoff
38 * The following list of people have reviewed this code and considered
39 * it safe since the last modification (if you reviewed it, please
40 * have your name added to the list):
45 #include <linux/if_tun.h>
48 * Need 'struct GNUNET_MessageHeader'.
50 #include "gnunet_crypto_lib.h"
51 #include "gnunet_common.h"
54 * Need VPN message types.
56 #include "gnunet_protocols.h"
59 * Should we print (interesting|debug) messages that can happen during
62 #define DEBUG GNUNET_NO
65 * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
67 #define MAX_SIZE 65536
70 * Path to 'sysctl' binary.
72 static const char *sbin_sysctl;
75 * Path to 'iptables' binary.
77 static const char *sbin_iptables;
82 * This is in linux/include/net/ipv6.h, but not always exported...
86 struct in6_addr ifr6_addr;
87 uint32_t ifr6_prefixlen; /* __u32 in the original */
94 * Open '/dev/null' and make the result the given
97 * @param target_fd desired FD to point to /dev/null
98 * @param flags open flags (O_RDONLY, O_WRONLY)
101 open_dev_null (int target_fd,
106 fd = open ("/dev/null", flags);
111 if (-1 == dup2 (fd, target_fd))
121 * Run the given command and wait for it to complete.
123 * @param file name of the binary to run
124 * @param cmd command line arguments (as given to 'execv')
125 * @return 0 on success, 1 on any error
128 fork_and_exec (const char *file,
145 /* we are the child process */
146 /* close stdin/stdout to not cause interference
147 with the helper's main protocol! */
149 open_dev_null (0, O_RDONLY);
151 open_dev_null (1, O_WRONLY);
152 (void) execv (file, cmd);
153 /* can only get here on error */
155 "exec `%s' failed: %s\n",
160 /* keep running waitpid as long as the only error we get is 'EINTR' */
161 while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
166 "waitpid failed: %s\n",
170 if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
172 /* child process completed and returned success, we're happy */
178 * Creates a tun-interface called dev;
180 * @param dev is asumed to point to a char[IFNAMSIZ]
181 * if *dev == '\\0', uses the name supplied by the kernel;
182 * @return the fd to the tun or -1 on error
196 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
198 fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun",
203 if (fd >= FD_SETSIZE)
205 fprintf (stderr, "File descriptor to large: %d", fd);
210 memset (&ifr, 0, sizeof (ifr));
211 ifr.ifr_flags = IFF_TUN;
214 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
216 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
219 "Error with ioctl on `%s': %s\n", "/dev/net/tun",
224 strcpy (dev, ifr.ifr_name);
230 * @brief Sets the IPv6-Address given in address on the interface dev
232 * @param dev the interface to configure
233 * @param address the IPv6-Address
234 * @param prefix_len the length of the network-prefix
237 set_address6 (const char *dev, const char *address, unsigned long prefix_len)
240 struct sockaddr_in6 sa6;
242 struct in6_ifreq ifr6;
245 * parse the new address
247 memset (&sa6, 0, sizeof (struct sockaddr_in6));
248 sa6.sin6_family = AF_INET6;
249 if (1 != inet_pton (AF_INET6, address, &sa6.sin6_addr))
251 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
256 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
258 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
262 memset (&ifr, 0, sizeof (struct ifreq));
264 * Get the index of the if
266 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
267 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
269 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
274 memset (&ifr6, 0, sizeof (struct in6_ifreq));
275 ifr6.ifr6_addr = sa6.sin6_addr;
276 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
277 ifr6.ifr6_prefixlen = prefix_len;
282 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
284 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
293 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
295 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
302 * Add the UP and RUNNING flags
304 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
305 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
307 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
315 fprintf (stderr, "close failed: %s\n", strerror (errno));
322 * @brief Sets the IPv4-Address given in address on the interface dev
324 * @param dev the interface to configure
325 * @param address the IPv4-Address
326 * @param mask the netmask
329 set_address4 (const char *dev, const char *address, const char *mask)
332 struct sockaddr_in *addr;
335 memset (&ifr, 0, sizeof (struct ifreq));
336 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
337 addr->sin_family = AF_INET;
342 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
344 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
349 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
351 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
355 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
360 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
362 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
370 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
371 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
373 fprintf (stderr, "Failed to parse address `%s': %s\n", mask,
382 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
384 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
393 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
395 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
402 * Add the UP and RUNNING flags
404 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
405 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
407 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
415 fprintf (stderr, "close failed: %s\n", strerror (errno));
423 * Start forwarding to and from the tunnel.
425 * @param fd_tun tunnel FD
431 * The buffer filled by reading from fd_tun
433 unsigned char buftun[MAX_SIZE];
434 ssize_t buftun_size = 0;
435 unsigned char *buftun_read = NULL;
438 * The buffer filled by reading from stdin
440 unsigned char bufin[MAX_SIZE];
441 ssize_t bufin_size = 0;
442 size_t bufin_rpos = 0;
443 unsigned char *bufin_read = NULL;
448 /* read refers to reading from fd_tun, writing to stdout */
451 /* write refers to reading from stdin, writing to fd_tun */
454 while ((1 == read_open) && (1 == write_open))
460 * We are supposed to read and the buffer is empty
461 * -> select on read from tun
463 if (read_open && (0 == buftun_size))
464 FD_SET (fd_tun, &fds_r);
467 * We are supposed to read and the buffer is not empty
468 * -> select on write to stdout
470 if (read_open && (0 != buftun_size))
474 * We are supposed to write and the buffer is empty
475 * -> select on read from stdin
477 if (write_open && (NULL == bufin_read))
481 * We are supposed to write and the buffer is not empty
482 * -> select on write to tun
484 if (write_open && (NULL != bufin_read))
485 FD_SET (fd_tun, &fds_w);
487 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
493 fprintf (stderr, "select failed: %s\n", strerror (errno));
499 if (FD_ISSET (fd_tun, &fds_r))
502 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
503 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
504 if (-1 == buftun_size)
509 shutdown (fd_tun, SHUT_RD);
510 shutdown (1, SHUT_WR);
514 else if (0 == buftun_size)
517 fprintf (stderr, "EOF on tun\n");
519 shutdown (fd_tun, SHUT_RD);
520 shutdown (1, SHUT_WR);
526 buftun_read = buftun;
527 struct GNUNET_MessageHeader *hdr =
528 (struct GNUNET_MessageHeader *) buftun;
529 buftun_size += sizeof (struct GNUNET_MessageHeader);
530 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
531 hdr->size = htons (buftun_size);
534 else if (FD_ISSET (1, &fds_w))
536 ssize_t written = write (1, buftun_read, buftun_size);
544 "write-error to stdout: %s\n",
546 shutdown (fd_tun, SHUT_RD);
547 shutdown (1, SHUT_WR);
551 else if (0 == written)
553 fprintf (stderr, "write returned 0!?\n");
558 buftun_size -= written;
559 buftun_read += written;
563 if (FD_ISSET (0, &fds_r))
565 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
566 if (-1 == bufin_size)
568 fprintf (stderr, "read-error: %s\n", strerror (errno));
569 shutdown (0, SHUT_RD);
570 shutdown (fd_tun, SHUT_WR);
574 else if (0 == bufin_size)
577 fprintf (stderr, "EOF on stdin\n");
579 shutdown (0, SHUT_RD);
580 shutdown (fd_tun, SHUT_WR);
586 struct GNUNET_MessageHeader *hdr;
589 bufin_rpos += bufin_size;
590 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
592 hdr = (struct GNUNET_MessageHeader *) bufin;
593 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
595 fprintf (stderr, "protocol violation!\n");
598 if (ntohs (hdr->size) > bufin_rpos)
600 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
601 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
602 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
605 else if (FD_ISSET (fd_tun, &fds_w))
607 ssize_t written = write (fd_tun, bufin_read, bufin_size);
611 fprintf (stderr, "write-error to tun: %s\n", strerror (errno));
612 shutdown (0, SHUT_RD);
613 shutdown (fd_tun, SHUT_WR);
617 else if (0 == written)
619 fprintf (stderr, "write returned 0!?\n");
624 bufin_size -= written;
625 bufin_read += written;
628 memmove (bufin, bufin_read, bufin_rpos);
629 bufin_read = NULL; /* start reading again */
641 * Open VPN tunnel interface.
643 * @param argc must be 6
644 * @param argv 0: binary name ("gnunet-helper-exit")
645 * 1: tunnel interface name ("gnunet-exit")
646 * 2: "physical" interface name ("eth0"), or "-" to not setup NAT
648 * 3: IPv6 address ("::1"), or "-" to skip IPv6
649 * 4: IPv6 netmask length in bits ("64") [ignored if #4 is "-"]
650 * 5: IPv4 address ("1.2.3.4"), or "-" to skip IPv4
651 * 6: IPv4 netmask ("255.255.0.0") [ignored if #4 is "-"]
654 main (int argc, char **argv)
662 fprintf (stderr, "Fatal: must supply 6 arguments!\n");
665 if ( (0 == strcmp (argv[3], "-")) &&
666 (0 == strcmp (argv[5], "-")) )
668 fprintf (stderr, "Fatal: disabling both IPv4 and IPv6 makes no sense.\n");
671 if (0 != strcmp (argv[2], "-"))
673 if (0 == access ("/sbin/iptables", X_OK))
674 sbin_iptables = "/sbin/iptables";
675 else if (0 == access ("/usr/sbin/iptables", X_OK))
676 sbin_iptables = "/usr/sbin/iptables";
680 "Fatal: executable iptables not found in approved directories: %s\n",
684 if (0 == access ("/sbin/sysctl", X_OK))
685 sbin_sysctl = "/sbin/sysctl";
686 else if (0 == access ("/usr/sbin/sysctl", X_OK))
687 sbin_sysctl = "/usr/sbin/sysctl";
691 "Fatal: executable sysctl not found in approved directories: %s\n",
697 strncpy (dev, argv[1], IFNAMSIZ);
698 dev[IFNAMSIZ - 1] = '\0';
700 if (-1 == (fd_tun = init_tun (dev)))
703 "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
712 if (0 != strcmp (argv[3], "-"))
715 const char *address = argv[3];
716 long prefix_len = atol (argv[4]);
718 if ((prefix_len < 1) || (prefix_len > 127))
720 fprintf (stderr, "Fatal: prefix_len out of range\n");
723 set_address6 (dev, address, prefix_len);
725 if (0 != strcmp (argv[2], "-"))
727 char *const sysctl_args[] =
729 "sysctl", "-w", "net.ipv6.conf.all.forwarding=1", NULL
731 if (0 != fork_and_exec (sbin_sysctl,
735 "Failed to enable IPv6 forwarding. Will continue anyway.\n");
740 if (0 != strcmp (argv[5], "-"))
743 const char *address = argv[5];
744 const char *mask = argv[6];
746 set_address4 (dev, address, mask);
748 if (0 != strcmp (argv[2], "-"))
751 char *const sysctl_args[] =
753 "sysctl", "-w", "net.ipv4.ip_forward=1", NULL
755 if (0 != fork_and_exec (sbin_sysctl,
759 "Failed to enable IPv4 forwarding. Will continue anyway.\n");
763 char *const iptables_args[] =
765 "iptables", "-t", "nat", "-A", "POSTROUTING", "-o", argv[2], "-j", "MASQUERADE", NULL
767 if (0 != fork_and_exec (sbin_iptables,
771 "Failed to enable IPv4 masquerading (NAT). Will continue anyway.\n");
777 uid_t uid = getuid ();
778 #ifdef HAVE_SETRESUID
779 if (0 != setresuid (uid, uid, uid))
781 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
786 if (0 != (setuid (uid) | seteuid (uid)))
788 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
794 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
796 fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
798 /* no exit, we might as well die with SIGPIPE should it ever happen */
803 (void) close (fd_tun);
807 /* end of gnunet-helper-exit.c */