2 This file is part of GNUnet.
3 Copyright (C) 2010, 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 vpn/gnunet-helper-vpn.c
21 * @brief the helper for the VPN service. Opens a virtual network-interface,
22 * sends data received on the if to stdout, sends data received on stdin to the
24 * @author Philipp Tölke
25 * @author Christian Grothoff
27 * The following list of people have reviewed this code and considered
28 * it safe since the last modification (if you reviewed it, please
29 * have your name added to the list):
34 #include <linux/if_tun.h>
37 * Need 'struct GNUNET_MessageHeader'.
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_common.h"
43 * Need VPN message types.
45 #include "gnunet_protocols.h"
48 * Should we print (interesting|debug) messages that can happen during
51 #define DEBUG GNUNET_NO
54 * Maximum size of a GNUnet message (GNUNET_MAX_MESSAGE_SIZE)
56 #define MAX_SIZE 65536
60 * This is in linux/include/net/ipv6.h, but not always exported...
64 struct in6_addr ifr6_addr;
65 uint32_t ifr6_prefixlen;
66 unsigned int ifr6_ifindex;
72 * Creates a tun-interface called dev;
74 * @param dev is asumed to point to a char[IFNAMSIZ]
75 * if *dev == '\\0', uses the name supplied by the kernel;
76 * @return the fd to the tun or -1 on error
90 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
93 "Error opening `%s': %s\n",
102 "File descriptor to large: %d",
108 memset (&ifr, 0, sizeof (ifr));
109 ifr.ifr_flags = IFF_TUN;
112 strncpy (ifr.ifr_name,
121 "Error with ioctl on `%s': %s\n",
127 strcpy (dev, ifr.ifr_name);
133 * @brief Sets the IPv6-Address given in address on the interface dev
135 * @param dev the interface to configure
136 * @param address the IPv6-Address
137 * @param prefix_len the length of the network-prefix
140 set_address6 (const char *dev,
142 unsigned long prefix_len)
145 struct in6_ifreq ifr6;
146 struct sockaddr_in6 sa6;
150 * parse the new address
152 memset (&sa6, 0, sizeof (struct sockaddr_in6));
153 sa6.sin6_family = AF_INET6;
154 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
157 "Failed to parse IPv6 address `%s'\n",
162 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
165 "Error creating socket: %s\n",
170 memset (&ifr, 0, sizeof (struct ifreq));
172 * Get the index of the if
174 strncpy (ifr.ifr_name,
182 "ioctl failed at %d: %s\n",
189 memset (&ifr6, 0, sizeof (struct in6_ifreq));
190 ifr6.ifr6_addr = sa6.sin6_addr;
191 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
192 ifr6.ifr6_prefixlen = prefix_len;
202 "ioctl failed at line %d: %s\n",
217 "ioctl failed at line %d: %s\n",
225 * Add the UP and RUNNING flags
227 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
233 "ioctl failed at line %d: %s\n",
243 "close failed: %s\n",
251 * @brief Sets the IPv4-Address given in address on the interface dev
253 * @param dev the interface to configure
254 * @param address the IPv4-Address
255 * @param mask the netmask
258 set_address4 (const char *dev,
263 struct sockaddr_in *addr;
266 memset (&ifr, 0, sizeof (struct ifreq));
267 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
268 addr->sin_family = AF_INET;
273 if (1 != inet_pton (AF_INET,
275 &addr->sin_addr.s_addr))
278 "Failed to parse IPv4 address `%s'\n",
283 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
286 "Error creating socket: %s\n",
291 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
296 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
299 "ioctl failed at %d: %s\n",
309 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
310 if (1 != inet_pton (AF_INET,
312 &addr->sin_addr.s_addr))
315 "Failed to parse IPv4 address mask `%s'\n",
324 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
327 "ioctl failed at line %d: %s\n",
337 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
340 "ioctl failed at line %d: %s\n",
348 * Add the UP and RUNNING flags
350 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
351 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
354 "ioctl failed at line %d: %s\n",
364 "close failed: %s\n",
373 * Start forwarding to and from the tunnel.
375 * @param fd_tun tunnel FD
381 * The buffer filled by reading from fd_tun
383 unsigned char buftun[MAX_SIZE];
384 ssize_t buftun_size = 0;
385 unsigned char *buftun_read = NULL;
388 * The buffer filled by reading from stdin
390 unsigned char bufin[MAX_SIZE];
391 ssize_t bufin_size = 0;
392 size_t bufin_rpos = 0;
393 unsigned char *bufin_read = NULL;
398 /* read refers to reading from fd_tun, writing to stdout */
401 /* write refers to reading from stdin, writing to fd_tun */
404 while ((1 == read_open) && (1 == write_open))
410 * We are supposed to read and the buffer is empty
411 * -> select on read from tun
413 if (read_open && (0 == buftun_size))
414 FD_SET (fd_tun, &fds_r);
417 * We are supposed to read and the buffer is not empty
418 * -> select on write to stdout
420 if (read_open && (0 != buftun_size))
424 * We are supposed to write and the buffer is empty
425 * -> select on read from stdin
427 if (write_open && (NULL == bufin_read))
431 * We are supposed to write and the buffer is not empty
432 * -> select on write to tun
434 if (write_open && (NULL != bufin_read))
435 FD_SET (fd_tun, &fds_w);
437 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
444 "select failed: %s\n",
451 if (FD_ISSET (fd_tun, &fds_r))
454 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
455 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
456 if (-1 == buftun_size)
461 shutdown (fd_tun, SHUT_RD);
462 shutdown (1, SHUT_WR);
466 else if (0 == buftun_size)
468 fprintf (stderr, "EOF on tun\n");
469 shutdown (fd_tun, SHUT_RD);
470 shutdown (1, SHUT_WR);
476 buftun_read = buftun;
477 struct GNUNET_MessageHeader *hdr =
478 (struct GNUNET_MessageHeader *) buftun;
479 buftun_size += sizeof (struct GNUNET_MessageHeader);
480 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
481 hdr->size = htons (buftun_size);
484 else if (FD_ISSET (1, &fds_w))
486 ssize_t written = write (1,
496 "write-error to stdout: %s\n",
498 shutdown (fd_tun, SHUT_RD);
499 shutdown (1, SHUT_WR);
503 else if (0 == written)
506 "write returned 0!?\n");
511 buftun_size -= written;
512 buftun_read += written;
516 if (FD_ISSET (0, &fds_r))
518 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
519 if (-1 == bufin_size)
524 shutdown (0, SHUT_RD);
525 shutdown (fd_tun, SHUT_WR);
529 else if (0 == bufin_size)
532 fprintf (stderr, "EOF on stdin\n");
534 shutdown (0, SHUT_RD);
535 shutdown (fd_tun, SHUT_WR);
541 struct GNUNET_MessageHeader *hdr;
544 bufin_rpos += bufin_size;
545 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
547 hdr = (struct GNUNET_MessageHeader *) bufin;
548 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
551 "protocol violation!\n");
554 if (ntohs (hdr->size) > bufin_rpos)
556 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
557 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
558 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
561 else if (FD_ISSET (fd_tun, &fds_w))
563 ssize_t written = write (fd_tun,
570 "write-error to tun: %s\n",
572 shutdown (0, SHUT_RD);
573 shutdown (fd_tun, SHUT_WR);
577 else if (0 == written)
579 fprintf (stderr, "write returned 0!?\n");
584 bufin_size -= written;
585 bufin_read += written;
588 memmove (bufin, bufin_read, bufin_rpos);
589 bufin_read = NULL; /* start reading again */
601 * Open VPN tunnel interface.
603 * @param argc must be 6
604 * @param argv 0: binary name (gnunet-helper-vpn)
605 * 1: tunnel interface name (gnunet-vpn)
606 * 2: IPv6 address (::1), "-" to disable
607 * 3: IPv6 netmask length in bits (64), ignored if #2 is "-"
608 * 4: IPv4 address (1.2.3.4), "-" to disable
609 * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
612 main (int argc, char **argv)
620 fprintf (stderr, "Fatal: must supply 5 arguments!\n");
627 dev[IFNAMSIZ - 1] = '\0';
629 if (-1 == (fd_tun = init_tun (dev)))
632 "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
641 if (0 != strcmp (argv[2], "-"))
643 const char *address = argv[2];
644 long prefix_len = atol (argv[3]);
646 if ((prefix_len < 1) || (prefix_len > 127))
649 "Fatal: prefix_len out of range\n");
659 if (0 != strcmp (argv[4], "-"))
661 const char *address = argv[4];
662 const char *mask = argv[5];
664 set_address4 (dev, address, mask);
667 uid_t uid = getuid ();
668 #ifdef HAVE_SETRESUID
669 if (0 != setresuid (uid, uid, uid))
672 "Failed to setresuid: %s\n",
678 if (0 != (setuid (uid) | seteuid (uid)))
681 "Failed to setuid: %s\n",
688 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
691 "Failed to protect against SIGPIPE: %s\n",
693 /* no exit, we might as well die with SIGPIPE should it ever happen */