2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_common.h"
53 * Need VPN message types.
55 #include "gnunet_protocols.h"
58 * Should we print (interesting|debug) messages that can happen during
61 #define DEBUG GNUNET_NO
64 * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
66 #define MAX_SIZE 65536
69 * Path to 'sysctl' binary.
71 static const char *sbin_sysctl;
74 * Path to 'iptables' binary.
76 static const char *sbin_iptables;
81 * This is in linux/include/net/ipv6.h, but not always exported...
85 struct in6_addr ifr6_addr;
86 uint32_t ifr6_prefixlen; /* __u32 in the original */
93 * Open '/dev/null' and make the result the given
96 * @param target_fd desired FD to point to /dev/null
97 * @param flags open flags (O_RDONLY, O_WRONLY)
100 open_dev_null (int target_fd,
105 fd = open ("/dev/null", flags);
110 if (-1 == dup2 (fd, target_fd))
120 * Run the given command and wait for it to complete.
122 * @param file name of the binary to run
123 * @param cmd command line arguments (as given to 'execv')
124 * @return 0 on success, 1 on any error
127 fork_and_exec (const char *file,
144 /* we are the child process */
145 /* close stdin/stdout to not cause interference
146 with the helper's main protocol! */
148 open_dev_null (0, O_RDONLY);
150 open_dev_null (1, O_WRONLY);
151 (void) execv (file, cmd);
152 /* can only get here on error */
154 "exec `%s' failed: %s\n",
159 /* keep running waitpid as long as the only error we get is 'EINTR' */
160 while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
165 "waitpid failed: %s\n",
169 if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
171 /* child process completed and returned success, we're happy */
177 * Creates a tun-interface called dev;
179 * @param dev is asumed to point to a char[IFNAMSIZ]
180 * if *dev == '\\0', uses the name supplied by the kernel;
181 * @return the fd to the tun or -1 on error
195 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
197 fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun",
202 if (fd >= FD_SETSIZE)
204 fprintf (stderr, "File descriptor to large: %d", fd);
209 memset (&ifr, 0, sizeof (ifr));
210 ifr.ifr_flags = IFF_TUN;
213 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
215 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
218 "Error with ioctl on `%s': %s\n", "/dev/net/tun",
223 strcpy (dev, ifr.ifr_name);
229 * @brief Sets the IPv6-Address given in address on the interface dev
231 * @param dev the interface to configure
232 * @param address the IPv6-Address
233 * @param prefix_len the length of the network-prefix
236 set_address6 (const char *dev, const char *address, unsigned long prefix_len)
239 struct sockaddr_in6 sa6;
241 struct in6_ifreq ifr6;
244 * parse the new address
246 memset (&sa6, 0, sizeof (struct sockaddr_in6));
247 sa6.sin6_family = AF_INET6;
248 if (1 != inet_pton (AF_INET6, address, &sa6.sin6_addr))
250 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
255 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
257 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
261 memset (&ifr, 0, sizeof (struct ifreq));
263 * Get the index of the if
265 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
266 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
268 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
273 memset (&ifr6, 0, sizeof (struct in6_ifreq));
274 ifr6.ifr6_addr = sa6.sin6_addr;
275 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
276 ifr6.ifr6_prefixlen = prefix_len;
281 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
283 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
292 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
294 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
301 * Add the UP and RUNNING flags
303 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
304 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
306 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
314 fprintf (stderr, "close failed: %s\n", strerror (errno));
321 * @brief Sets the IPv4-Address given in address on the interface dev
323 * @param dev the interface to configure
324 * @param address the IPv4-Address
325 * @param mask the netmask
328 set_address4 (const char *dev, const char *address, const char *mask)
331 struct sockaddr_in *addr;
334 memset (&ifr, 0, sizeof (struct ifreq));
335 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
336 addr->sin_family = AF_INET;
341 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
343 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
348 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
350 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
354 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
359 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
361 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
369 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
370 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
372 fprintf (stderr, "Failed to parse address `%s': %s\n", mask,
381 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
383 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
392 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
394 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
401 * Add the UP and RUNNING flags
403 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
404 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
406 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
414 fprintf (stderr, "close failed: %s\n", strerror (errno));
422 * Start forwarding to and from the tunnel.
424 * @param fd_tun tunnel FD
430 * The buffer filled by reading from fd_tun
432 unsigned char buftun[MAX_SIZE];
433 ssize_t buftun_size = 0;
434 unsigned char *buftun_read = NULL;
437 * The buffer filled by reading from stdin
439 unsigned char bufin[MAX_SIZE];
440 ssize_t bufin_size = 0;
441 size_t bufin_rpos = 0;
442 unsigned char *bufin_read = NULL;
447 /* read refers to reading from fd_tun, writing to stdout */
450 /* write refers to reading from stdin, writing to fd_tun */
453 while ((1 == read_open) || (1 == write_open))
459 * We are supposed to read and the buffer is empty
460 * -> select on read from tun
462 if (read_open && (0 == buftun_size))
463 FD_SET (fd_tun, &fds_r);
466 * We are supposed to read and the buffer is not empty
467 * -> select on write to stdout
469 if (read_open && (0 != buftun_size))
473 * We are supposed to write and the buffer is empty
474 * -> select on read from stdin
476 if (write_open && (NULL == bufin_read))
480 * We are supposed to write and the buffer is not empty
481 * -> select on write to tun
483 if (write_open && (NULL != bufin_read))
484 FD_SET (fd_tun, &fds_w);
486 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
492 fprintf (stderr, "select failed: %s\n", strerror (errno));
498 if (FD_ISSET (fd_tun, &fds_r))
501 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
502 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
503 if (-1 == buftun_size)
505 fprintf (stderr, "read-error: %s\n", strerror (errno));
506 shutdown (fd_tun, SHUT_RD);
507 shutdown (1, SHUT_WR);
511 else if (0 == buftun_size)
514 fprintf (stderr, "EOF on tun\n");
516 shutdown (fd_tun, SHUT_RD);
517 shutdown (1, SHUT_WR);
523 buftun_read = buftun;
524 struct GNUNET_MessageHeader *hdr =
525 (struct GNUNET_MessageHeader *) buftun;
526 buftun_size += sizeof (struct GNUNET_MessageHeader);
527 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
528 hdr->size = htons (buftun_size);
531 else if (FD_ISSET (1, &fds_w))
533 ssize_t written = write (1, buftun_read, buftun_size);
540 fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
541 shutdown (fd_tun, SHUT_RD);
542 shutdown (1, SHUT_WR);
546 else if (0 == written)
548 fprintf (stderr, "write returned 0!?\n");
553 buftun_size -= written;
554 buftun_read += written;
558 if (FD_ISSET (0, &fds_r))
560 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
561 if (-1 == bufin_size)
563 fprintf (stderr, "read-error: %s\n", strerror (errno));
564 shutdown (0, SHUT_RD);
565 shutdown (fd_tun, SHUT_WR);
569 else if (0 == bufin_size)
572 fprintf (stderr, "EOF on stdin\n");
574 shutdown (0, SHUT_RD);
575 shutdown (fd_tun, SHUT_WR);
581 struct GNUNET_MessageHeader *hdr;
584 bufin_rpos += bufin_size;
585 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
587 hdr = (struct GNUNET_MessageHeader *) bufin;
588 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
590 fprintf (stderr, "protocol violation!\n");
593 if (ntohs (hdr->size) > bufin_rpos)
595 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
596 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
597 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
600 else if (FD_ISSET (fd_tun, &fds_w))
602 ssize_t written = write (fd_tun, bufin_read, bufin_size);
606 fprintf (stderr, "write-error to tun: %s\n", strerror (errno));
607 shutdown (0, SHUT_RD);
608 shutdown (fd_tun, SHUT_WR);
612 else if (0 == written)
614 fprintf (stderr, "write returned 0!?\n");
619 bufin_size -= written;
620 bufin_read += written;
623 memmove (bufin, bufin_read, bufin_rpos);
624 bufin_read = NULL; /* start reading again */
636 * Open VPN tunnel interface.
638 * @param argc must be 6
639 * @param argv 0: binary name ("gnunet-helper-exit")
640 * 1: tunnel interface name ("gnunet-exit")
641 * 2: IPv4 "physical" interface name ("eth0"), or "-" to not do IPv4 NAT
642 * 3: IPv6 address ("::1"), or "-" to skip IPv6
643 * 4: IPv6 netmask length in bits ("64") [ignored if #4 is "-"]
644 * 5: IPv4 address ("1.2.3.4"), or "-" to skip IPv4
645 * 6: IPv4 netmask ("255.255.0.0") [ignored if #4 is "-"]
648 main (int argc, char **argv)
656 fprintf (stderr, "Fatal: must supply 6 arguments!\n");
659 if ( (0 == strcmp (argv[3], "-")) &&
660 (0 == strcmp (argv[5], "-")) )
662 fprintf (stderr, "Fatal: disabling both IPv4 and IPv6 makes no sense.\n");
665 if (0 == access ("/sbin/iptables", X_OK))
666 sbin_iptables = "/sbin/iptables";
667 else if (0 == access ("/usr/sbin/iptables", X_OK))
668 sbin_iptables = "/usr/sbin/iptables";
672 "Fatal: executable iptables not found in approved directories: %s\n",
676 if (0 == access ("/sbin/sysctl", X_OK))
677 sbin_sysctl = "/sbin/sysctl";
678 else if (0 == access ("/usr/sbin/sysctl", X_OK))
679 sbin_sysctl = "/usr/sbin/sysctl";
683 "Fatal: executable sysctl not found in approved directories: %s\n",
688 strncpy (dev, argv[1], IFNAMSIZ);
689 dev[IFNAMSIZ - 1] = '\0';
691 if (-1 == (fd_tun = init_tun (dev)))
694 "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
703 if (0 != strcmp (argv[3], "-"))
706 const char *address = argv[3];
707 long prefix_len = atol (argv[4]);
709 if ((prefix_len < 1) || (prefix_len > 127))
711 fprintf (stderr, "Fatal: prefix_len out of range\n");
714 set_address6 (dev, address, prefix_len);
717 char *const sysctl_args[] =
719 "sysctl", "-w", "net.ipv6.conf.all.forwarding=1", NULL
721 if (0 != fork_and_exec (sbin_sysctl,
725 "Failed to enable IPv6 forwarding. Will continue anyway.\n");
730 if (0 != strcmp (argv[5], "-"))
733 const char *address = argv[5];
734 const char *mask = argv[6];
736 set_address4 (dev, address, mask);
739 char *const sysctl_args[] =
741 "sysctl", "-w", "net.ipv4.ip_forward=1", NULL
743 if (0 != fork_and_exec (sbin_sysctl,
747 "Failed to enable IPv4 forwarding. Will continue anyway.\n");
750 if (0 != strcmp (argv[2], "-"))
752 char *const iptables_args[] =
754 "iptables", "-t", "nat", "-A", "POSTROUTING", "-o", argv[2], "-j", "MASQUERADE", NULL
756 if (0 != fork_and_exec (sbin_iptables,
760 "Failed to enable IPv4 masquerading (NAT). Will continue anyway.\n");
765 uid_t uid = getuid ();
766 #ifdef HAVE_SETRESUID
767 if (0 != setresuid (uid, uid, uid))
769 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
774 if (0 != (setuid (uid) | seteuid (uid)))
776 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
782 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
784 fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
786 /* no exit, we might as well die with SIGPIPE should it ever happen */
791 (void) close (fd_tun);
795 /* end of gnunet-helper-exit.c */