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 * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
60 #define MAX_SIZE 65536
63 * Path to 'sysctl' binary.
65 static const char *sbin_sysctl;
68 * Path to 'iptables' binary.
70 static const char *sbin_iptables;
75 * This is in linux/include/net/ipv6.h, but not always exported...
79 struct in6_addr ifr6_addr;
80 uint32_t ifr6_prefixlen;
81 unsigned int ifr6_ifindex;
88 * Run the given command and wait for it to complete.
90 * @param file name of the binary to run
91 * @param cmd command line arguments (as given to 'execv')
92 * @return 0 on success, 1 on any error
95 fork_and_exec (const char *file,
112 /* we are the child process */
113 /* close stdin/stdout to not cause interference
114 with the helper's main protocol! */
117 (void) execv (file, cmd);
118 /* can only get here on error */
120 "exec `%s' failed: %s\n",
125 /* keep running waitpid as long as the only error we get is 'EINTR' */
126 while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
131 "waitpid failed: %s\n",
135 if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
137 /* child process completed and returned success, we're happy */
143 * Creates a tun-interface called dev;
145 * @param dev is asumed to point to a char[IFNAMSIZ]
146 * if *dev == '\\0', uses the name supplied by the kernel;
147 * @return the fd to the tun or -1 on error
161 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
163 fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun",
168 if (fd >= FD_SETSIZE)
170 fprintf (stderr, "File descriptor to large: %d", fd);
174 memset (&ifr, 0, sizeof (ifr));
175 ifr.ifr_flags = IFF_TUN;
178 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
180 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
182 fprintf (stderr, "Error with ioctl on `%s': %s\n", "/dev/net/tun",
187 strcpy (dev, ifr.ifr_name);
193 * @brief Sets the IPv6-Address given in address on the interface dev
195 * @param dev the interface to configure
196 * @param address the IPv6-Address
197 * @param prefix_len the length of the network-prefix
200 set_address6 (const char *dev, const char *address, unsigned long prefix_len)
203 struct in6_ifreq ifr6;
204 struct sockaddr_in6 sa6;
208 * parse the new address
210 memset (&sa6, 0, sizeof (struct sockaddr_in6));
211 sa6.sin6_family = AF_INET6;
212 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
214 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
219 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
221 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
225 memset (&ifr, 0, sizeof (struct ifreq));
227 * Get the index of the if
229 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
230 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
232 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
237 memset (&ifr6, 0, sizeof (struct in6_ifreq));
238 ifr6.ifr6_addr = sa6.sin6_addr;
239 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
240 ifr6.ifr6_prefixlen = prefix_len;
245 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
247 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
256 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
258 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
265 * Add the UP and RUNNING flags
267 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
268 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
270 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
278 fprintf (stderr, "close failed: %s\n", strerror (errno));
285 * @brief Sets the IPv4-Address given in address on the interface dev
287 * @param dev the interface to configure
288 * @param address the IPv4-Address
289 * @param mask the netmask
292 set_address4 (const char *dev, const char *address, const char *mask)
295 struct sockaddr_in *addr;
298 memset (&ifr, 0, sizeof (struct ifreq));
299 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
300 addr->sin_family = AF_INET;
305 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
307 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
312 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
314 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
318 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
323 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
325 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
333 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
334 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
336 fprintf (stderr, "Failed to parse address `%s': %s\n", mask,
345 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
347 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
356 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
358 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
365 * Add the UP and RUNNING flags
367 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
368 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
370 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
378 fprintf (stderr, "close failed: %s\n", strerror (errno));
386 * Start forwarding to and from the tunnel.
388 * @param fd_tun tunnel FD
394 * The buffer filled by reading from fd_tun
396 unsigned char buftun[MAX_SIZE];
397 ssize_t buftun_size = 0;
398 unsigned char *buftun_read = NULL;
401 * The buffer filled by reading from stdin
403 unsigned char bufin[MAX_SIZE];
404 ssize_t bufin_size = 0;
405 size_t bufin_rpos = 0;
406 unsigned char *bufin_read = NULL;
411 /* read refers to reading from fd_tun, writing to stdout */
414 /* write refers to reading from stdin, writing to fd_tun */
417 while ((1 == read_open) || (1 == write_open))
423 * We are supposed to read and the buffer is empty
424 * -> select on read from tun
426 if (read_open && (0 == buftun_size))
427 FD_SET (fd_tun, &fds_r);
430 * We are supposed to read and the buffer is not empty
431 * -> select on write to stdout
433 if (read_open && (0 != buftun_size))
437 * We are supposed to write and the buffer is empty
438 * -> select on read from stdin
440 if (write_open && (NULL == bufin_read))
444 * We are supposed to write and the buffer is not empty
445 * -> select on write to tun
447 if (write_open && (NULL != bufin_read))
448 FD_SET (fd_tun, &fds_w);
450 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
456 fprintf (stderr, "select failed: %s\n", strerror (errno));
462 if (FD_ISSET (fd_tun, &fds_r))
465 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
466 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
467 if (-1 == buftun_size)
469 fprintf (stderr, "read-error: %s\n", strerror (errno));
470 shutdown (fd_tun, SHUT_RD);
471 shutdown (1, SHUT_WR);
475 else if (0 == buftun_size)
477 fprintf (stderr, "EOF on tun\n");
478 shutdown (fd_tun, SHUT_RD);
479 shutdown (1, SHUT_WR);
485 buftun_read = buftun;
486 struct GNUNET_MessageHeader *hdr =
487 (struct GNUNET_MessageHeader *) buftun;
488 buftun_size += sizeof (struct GNUNET_MessageHeader);
489 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
490 hdr->size = htons (buftun_size);
493 else if (FD_ISSET (1, &fds_w))
495 ssize_t written = write (1, buftun_read, buftun_size);
499 fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
500 shutdown (fd_tun, SHUT_RD);
501 shutdown (1, SHUT_WR);
505 else if (0 == written)
507 fprintf (stderr, "write returned 0!?\n");
512 buftun_size -= written;
513 buftun_read += written;
517 if (FD_ISSET (0, &fds_r))
519 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
520 if (-1 == bufin_size)
522 fprintf (stderr, "read-error: %s\n", strerror (errno));
523 shutdown (0, SHUT_RD);
524 shutdown (fd_tun, SHUT_WR);
528 else if (0 == bufin_size)
530 fprintf (stderr, "EOF on stdin\n");
531 shutdown (0, SHUT_RD);
532 shutdown (fd_tun, SHUT_WR);
538 struct GNUNET_MessageHeader *hdr;
541 bufin_rpos += bufin_size;
542 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
544 hdr = (struct GNUNET_MessageHeader *) bufin;
545 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
547 fprintf (stderr, "protocol violation!\n");
550 if (ntohs (hdr->size) > bufin_rpos)
552 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
553 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
554 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
557 else if (FD_ISSET (fd_tun, &fds_w))
559 ssize_t written = write (fd_tun, bufin_read, bufin_size);
563 fprintf (stderr, "write-error to tun: %s\n", strerror (errno));
564 shutdown (0, SHUT_RD);
565 shutdown (fd_tun, SHUT_WR);
569 else if (0 == written)
571 fprintf (stderr, "write returned 0!?\n");
576 bufin_size -= written;
577 bufin_read += written;
580 memmove (bufin, bufin_read, bufin_rpos);
581 bufin_read = NULL; /* start reading again */
593 * Open VPN tunnel interface.
595 * @param argc must be 6
596 * @param argv 0: binary name ("gnunet-helper-vpn")
597 * 1: tunnel interface name ("gnunet-vpn")
598 * 2: IPv4 "physical" interface name ("eth0"), or "%" to not do IPv4 NAT
599 * 3: IPv6 address ("::1"), or "-" to skip IPv6
600 * 4: IPv6 netmask length in bits ("64") [ignored if #4 is "-"]
601 * 5: IPv4 address ("1.2.3.4"), or "-" to skip IPv4
602 * 6: IPv4 netmask ("255.255.0.0") [ignored if #4 is "-"]
605 main (int argc, char **argv)
613 fprintf (stderr, "Fatal: must supply 5 arguments!\n");
616 if ( (0 == strcmp (argv[3], "-")) &&
617 (0 == strcmp (argv[5], "-")) )
619 fprintf (stderr, "Fatal: disabling both IPv4 and IPv6 makes no sense.\n");
622 if (0 == access ("/sbin/iptables", X_OK))
623 sbin_iptables = "/sbin/iptables";
624 else if (0 == access ("/usr/sbin/iptables", X_OK))
625 sbin_iptables = "/usr/sbin/iptables";
629 "Fatal: executable iptables not found in approved directories: %s\n",
633 if (0 == access ("/sbin/sysctl", X_OK))
634 sbin_sysctl = "/sbin/sysctl";
635 else if (0 == access ("/usr/sbin/sysctl", X_OK))
636 sbin_sysctl = "/usr/sbin/sysctl";
640 "Fatal: executable sysctl not found in approved directories: %s\n",
645 strncpy (dev, argv[1], IFNAMSIZ);
646 dev[IFNAMSIZ - 1] = '\0';
648 if (-1 == (fd_tun = init_tun (dev)))
650 fprintf (stderr, "Fatal: could not initialize tun-interface\n");
654 if (0 != strcmp (argv[3], "-"))
657 const char *address = argv[3];
658 long prefix_len = atol (argv[4]);
660 if ((prefix_len < 1) || (prefix_len > 127))
662 fprintf (stderr, "Fatal: prefix_len out of range\n");
665 set_address6 (dev, address, prefix_len);
668 char *const sysctl_args[] =
670 "sysctl", "-w", "net.ipv6.conf.all.forwarding=1", NULL
672 if (0 != fork_and_exec (sbin_sysctl,
676 "Failed to enable IPv6 forwarding. Will continue anyway.\n");
681 if (0 != strcmp (argv[5], "-"))
684 const char *address = argv[5];
685 const char *mask = argv[6];
687 set_address4 (dev, address, mask);
690 char *const sysctl_args[] =
692 "sysctl", "-w", "net.ipv4.ip_forward=1", NULL
694 if (0 != fork_and_exec (sbin_sysctl,
698 "Failed to enable IPv4 forwarding. Will continue anyway.\n");
701 if (0 != strcmp (argv[2], "%"))
703 char *const iptables_args[] =
705 "iptables", "-t", "nat", "-A", "POSTROUTING", "-o", argv[2], "-j", "MASQUERADE", NULL
707 if (0 != fork_and_exec (sbin_iptables,
711 "Failed to enable IPv4 masquerading (NAT). Will continue anyway.\n");
716 uid_t uid = getuid ();
717 #ifdef HAVE_SETRESUID
718 if (0 != setresuid (uid, uid, uid))
720 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
725 if (0 != (setuid (uid) | seteuid (uid)))
727 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
733 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
735 fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
737 /* no exit, we might as well die with SIGPIPE should it ever happen */
746 /* end of gnunet-helper-exit.c */