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 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/>.
20 * @file exit/gnunet-helper-exit.c
22 * @brief the helper for exit nodes. Opens a virtual
23 * network-interface, sends data received on the if to stdout, sends
24 * data received on stdin to the interface. The code also enables
25 * IPv4/IPv6 forwarding and NAT on the current system (the latter on
26 * an interface specified on the command-line); these changes to the
27 * network configuration are NOT automatically undone when the program
28 * is stopped (this is because we cannot be sure that some other
29 * application didn't enable them before or after us; also, these
30 * changes should be mostly harmless as it simply turns the system
33 * @author Philipp Tölke
34 * @author Christian Grothoff
36 * The following list of people have reviewed this code and considered
37 * it safe since the last modification (if you reviewed it, please
38 * have your name added to the list):
43 #include <linux/if_tun.h>
46 * Need 'struct GNUNET_MessageHeader'.
48 #include "gnunet_crypto_lib.h"
49 #include "gnunet_common.h"
52 * Need VPN message types.
54 #include "gnunet_protocols.h"
57 * Should we print (interesting|debug) messages that can happen during
60 #define DEBUG GNUNET_NO
63 * Maximum size of a GNUnet message (GNUNET_MAX_MESSAGE_SIZE)
65 #define MAX_SIZE 65536
68 * Path to 'sysctl' binary.
70 static const char *sbin_sysctl;
73 * Path to 'iptables' binary.
75 static const char *sbin_iptables;
80 * This is in linux/include/net/ipv6.h, but not always exported...
84 struct in6_addr ifr6_addr;
85 uint32_t ifr6_prefixlen; /* __u32 in the original */
92 * Open '/dev/null' and make the result the given
95 * @param target_fd desired FD to point to /dev/null
96 * @param flags open flags (O_RDONLY, O_WRONLY)
99 open_dev_null (int target_fd,
104 fd = open ("/dev/null", flags);
109 if (-1 == dup2 (fd, target_fd))
119 * Run the given command and wait for it to complete.
121 * @param file name of the binary to run
122 * @param cmd command line arguments (as given to 'execv')
123 * @return 0 on success, 1 on any error
126 fork_and_exec (const char *file,
143 /* we are the child process */
144 /* close stdin/stdout to not cause interference
145 with the helper's main protocol! */
147 open_dev_null (0, O_RDONLY);
149 open_dev_null (1, O_WRONLY);
150 (void) execv (file, cmd);
151 /* can only get here on error */
153 "exec `%s' failed: %s\n",
158 /* keep running waitpid as long as the only error we get is 'EINTR' */
159 while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
164 "waitpid failed: %s\n",
168 if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
170 /* child process completed and returned success, we're happy */
176 * Creates a tun-interface called dev;
178 * @param dev is asumed to point to a char[IFNAMSIZ]
179 * if *dev == '\\0', uses the name supplied by the kernel;
180 * @return the fd to the tun or -1 on error
194 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
196 fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun",
201 if (fd >= FD_SETSIZE)
203 fprintf (stderr, "File descriptor to large: %d", fd);
208 memset (&ifr, 0, sizeof (ifr));
209 ifr.ifr_flags = IFF_TUN;
212 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
214 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
217 "Error with ioctl on `%s': %s\n", "/dev/net/tun",
222 strcpy (dev, ifr.ifr_name);
228 * @brief Sets the IPv6-Address given in address on the interface dev
230 * @param dev the interface to configure
231 * @param address the IPv6-Address
232 * @param prefix_len the length of the network-prefix
235 set_address6 (const char *dev, const char *address, unsigned long prefix_len)
238 struct sockaddr_in6 sa6;
240 struct in6_ifreq ifr6;
243 * parse the new address
245 memset (&sa6, 0, sizeof (struct sockaddr_in6));
246 sa6.sin6_family = AF_INET6;
247 if (1 != inet_pton (AF_INET6, address, &sa6.sin6_addr))
249 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
254 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
256 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
260 memset (&ifr, 0, sizeof (struct ifreq));
262 * Get the index of the if
264 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
265 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
267 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
272 memset (&ifr6, 0, sizeof (struct in6_ifreq));
273 ifr6.ifr6_addr = sa6.sin6_addr;
274 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
275 ifr6.ifr6_prefixlen = prefix_len;
280 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
282 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
291 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
293 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
300 * Add the UP and RUNNING flags
302 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
303 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
305 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
313 fprintf (stderr, "close failed: %s\n", strerror (errno));
320 * @brief Sets the IPv4-Address given in address on the interface dev
322 * @param dev the interface to configure
323 * @param address the IPv4-Address
324 * @param mask the netmask
327 set_address4 (const char *dev, const char *address, const char *mask)
330 struct sockaddr_in *addr;
333 memset (&ifr, 0, sizeof (struct ifreq));
334 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
335 addr->sin_family = AF_INET;
340 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
342 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
347 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
349 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
353 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
358 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
360 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
368 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
369 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
371 fprintf (stderr, "Failed to parse address `%s': %s\n", mask,
380 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
382 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
391 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
393 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
400 * Add the UP and RUNNING flags
402 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
403 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
405 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
413 fprintf (stderr, "close failed: %s\n", strerror (errno));
421 * Start forwarding to and from the tunnel.
423 * @param fd_tun tunnel FD
429 * The buffer filled by reading from fd_tun
431 unsigned char buftun[MAX_SIZE];
432 ssize_t buftun_size = 0;
433 unsigned char *buftun_read = NULL;
436 * The buffer filled by reading from stdin
438 unsigned char bufin[MAX_SIZE];
439 ssize_t bufin_size = 0;
440 size_t bufin_rpos = 0;
441 unsigned char *bufin_read = NULL;
446 /* read refers to reading from fd_tun, writing to stdout */
449 /* write refers to reading from stdin, writing to fd_tun */
452 while ((1 == read_open) && (1 == write_open))
458 * We are supposed to read and the buffer is empty
459 * -> select on read from tun
461 if (read_open && (0 == buftun_size))
462 FD_SET (fd_tun, &fds_r);
465 * We are supposed to read and the buffer is not empty
466 * -> select on write to stdout
468 if (read_open && (0 != buftun_size))
472 * We are supposed to write and the buffer is empty
473 * -> select on read from stdin
475 if (write_open && (NULL == bufin_read))
479 * We are supposed to write and the buffer is not empty
480 * -> select on write to tun
482 if (write_open && (NULL != bufin_read))
483 FD_SET (fd_tun, &fds_w);
485 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
491 fprintf (stderr, "select failed: %s\n", strerror (errno));
497 if (FD_ISSET (fd_tun, &fds_r))
500 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
501 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
502 if (-1 == buftun_size)
507 shutdown (fd_tun, SHUT_RD);
508 shutdown (1, SHUT_WR);
512 else if (0 == buftun_size)
515 fprintf (stderr, "EOF on tun\n");
517 shutdown (fd_tun, SHUT_RD);
518 shutdown (1, SHUT_WR);
524 buftun_read = buftun;
525 struct GNUNET_MessageHeader *hdr =
526 (struct GNUNET_MessageHeader *) buftun;
527 buftun_size += sizeof (struct GNUNET_MessageHeader);
528 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
529 hdr->size = htons (buftun_size);
532 else if (FD_ISSET (1, &fds_w))
534 ssize_t written = write (1, buftun_read, buftun_size);
542 "write-error to stdout: %s\n",
544 shutdown (fd_tun, SHUT_RD);
545 shutdown (1, SHUT_WR);
549 else if (0 == written)
551 fprintf (stderr, "write returned 0!?\n");
556 buftun_size -= written;
557 buftun_read += written;
561 if (FD_ISSET (0, &fds_r))
563 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
564 if (-1 == bufin_size)
566 fprintf (stderr, "read-error: %s\n", strerror (errno));
567 shutdown (0, SHUT_RD);
568 shutdown (fd_tun, SHUT_WR);
572 else if (0 == bufin_size)
575 fprintf (stderr, "EOF on stdin\n");
577 shutdown (0, SHUT_RD);
578 shutdown (fd_tun, SHUT_WR);
584 struct GNUNET_MessageHeader *hdr;
587 bufin_rpos += bufin_size;
588 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
590 hdr = (struct GNUNET_MessageHeader *) bufin;
591 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
593 fprintf (stderr, "protocol violation!\n");
596 if (ntohs (hdr->size) > bufin_rpos)
598 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
599 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
600 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
603 else if (FD_ISSET (fd_tun, &fds_w))
605 ssize_t written = write (fd_tun, bufin_read, bufin_size);
609 fprintf (stderr, "write-error to tun: %s\n", strerror (errno));
610 shutdown (0, SHUT_RD);
611 shutdown (fd_tun, SHUT_WR);
615 else if (0 == written)
617 fprintf (stderr, "write returned 0!?\n");
622 bufin_size -= written;
623 bufin_read += written;
626 memmove (bufin, bufin_read, bufin_rpos);
627 bufin_read = NULL; /* start reading again */
639 * Open VPN tunnel interface.
641 * @param argc must be 6
642 * @param argv 0: binary name ("gnunet-helper-exit")
643 * 1: tunnel interface name ("gnunet-exit")
644 * 2: "physical" interface name ("eth0"), or "-" to not setup NAT
646 * 3: IPv6 address ("::1"), or "-" to skip IPv6
647 * 4: IPv6 netmask length in bits ("64") [ignored if #4 is "-"]
648 * 5: IPv4 address ("1.2.3.4"), or "-" to skip IPv4
649 * 6: IPv4 netmask ("255.255.0.0") [ignored if #4 is "-"]
652 main (int argc, char **argv)
660 fprintf (stderr, "Fatal: must supply 6 arguments!\n");
663 if ( (0 == strcmp (argv[3], "-")) &&
664 (0 == strcmp (argv[5], "-")) )
666 fprintf (stderr, "Fatal: disabling both IPv4 and IPv6 makes no sense.\n");
669 if (0 != strcmp (argv[2], "-"))
671 if (0 == access ("/sbin/iptables", X_OK))
672 sbin_iptables = "/sbin/iptables";
673 else if (0 == access ("/usr/sbin/iptables", X_OK))
674 sbin_iptables = "/usr/sbin/iptables";
678 "Fatal: executable iptables not found in approved directories: %s\n",
682 if (0 == access ("/sbin/sysctl", X_OK))
683 sbin_sysctl = "/sbin/sysctl";
684 else if (0 == access ("/usr/sbin/sysctl", X_OK))
685 sbin_sysctl = "/usr/sbin/sysctl";
689 "Fatal: executable sysctl not found in approved directories: %s\n",
695 strncpy (dev, argv[1], IFNAMSIZ);
696 dev[IFNAMSIZ - 1] = '\0';
698 if (-1 == (fd_tun = init_tun (dev)))
701 "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
710 if (0 != strcmp (argv[3], "-"))
713 const char *address = argv[3];
714 long prefix_len = atol (argv[4]);
716 if ((prefix_len < 1) || (prefix_len > 127))
718 fprintf (stderr, "Fatal: prefix_len out of range\n");
721 set_address6 (dev, address, prefix_len);
723 if (0 != strcmp (argv[2], "-"))
725 char *const sysctl_args[] =
727 "sysctl", "-w", "net.ipv6.conf.all.forwarding=1", NULL
729 if (0 != fork_and_exec (sbin_sysctl,
733 "Failed to enable IPv6 forwarding. Will continue anyway.\n");
738 if (0 != strcmp (argv[5], "-"))
741 const char *address = argv[5];
742 const char *mask = argv[6];
744 set_address4 (dev, address, mask);
746 if (0 != strcmp (argv[2], "-"))
749 char *const sysctl_args[] =
751 "sysctl", "-w", "net.ipv4.ip_forward=1", NULL
753 if (0 != fork_and_exec (sbin_sysctl,
757 "Failed to enable IPv4 forwarding. Will continue anyway.\n");
761 char *const iptables_args[] =
763 "iptables", "-t", "nat", "-A", "POSTROUTING", "-o", argv[2], "-j", "MASQUERADE", NULL
765 if (0 != fork_and_exec (sbin_iptables,
769 "Failed to enable IPv4 masquerading (NAT). Will continue anyway.\n");
775 uid_t uid = getuid ();
776 #ifdef HAVE_SETRESUID
777 if (0 != setresuid (uid, uid, uid))
779 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
784 if (0 != (setuid (uid) | seteuid (uid)))
786 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
792 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
794 fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
796 /* no exit, we might as well die with SIGPIPE should it ever happen */
801 (void) close (fd_tun);
805 /* end of gnunet-helper-exit.c */