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
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 vpn/gnunet-helper-vpn.c
23 * @brief the helper for the VPN service. Opens a virtual network-interface,
24 * sends data received on the if to stdout, sends data received on stdin to the
26 * @author Philipp Tölke
27 * @author Christian Grothoff
29 * The following list of people have reviewed this code and considered
30 * it safe since the last modification (if you reviewed it, please
31 * have your name added to the list):
36 #include <linux/if_tun.h>
39 * Need 'struct GNUNET_MessageHeader'.
41 #include "gnunet_crypto_lib.h"
42 #include "gnunet_common.h"
45 * Need VPN message types.
47 #include "gnunet_protocols.h"
50 * Should we print (interesting|debug) messages that can happen during
53 #define DEBUG GNUNET_NO
56 * Maximum size of a GNUnet message (GNUNET_MAX_MESSAGE_SIZE)
58 #define MAX_SIZE 65536
62 * This is in linux/include/net/ipv6.h, but not always exported...
66 struct in6_addr ifr6_addr;
67 uint32_t ifr6_prefixlen;
68 unsigned int ifr6_ifindex;
74 * Creates a tun-interface called dev;
76 * @param dev is asumed to point to a char[IFNAMSIZ]
77 * if *dev == '\\0', uses the name supplied by the kernel;
78 * @return the fd to the tun or -1 on error
92 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
95 "Error opening `%s': %s\n",
101 if (fd >= FD_SETSIZE)
104 "File descriptor to large: %d",
110 memset (&ifr, 0, sizeof (ifr));
111 ifr.ifr_flags = IFF_TUN;
114 strncpy (ifr.ifr_name,
123 "Error with ioctl on `%s': %s\n",
129 strcpy (dev, ifr.ifr_name);
135 * @brief Sets the IPv6-Address given in address on the interface dev
137 * @param dev the interface to configure
138 * @param address the IPv6-Address
139 * @param prefix_len the length of the network-prefix
142 set_address6 (const char *dev,
144 unsigned long prefix_len)
147 struct in6_ifreq ifr6;
148 struct sockaddr_in6 sa6;
152 * parse the new address
154 memset (&sa6, 0, sizeof (struct sockaddr_in6));
155 sa6.sin6_family = AF_INET6;
156 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
159 "Failed to parse IPv6 address `%s'\n",
164 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
167 "Error creating socket: %s\n",
172 memset (&ifr, 0, sizeof (struct ifreq));
174 * Get the index of the if
176 strncpy (ifr.ifr_name,
184 "ioctl failed at %d: %s\n",
191 memset (&ifr6, 0, sizeof (struct in6_ifreq));
192 ifr6.ifr6_addr = sa6.sin6_addr;
193 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
194 ifr6.ifr6_prefixlen = prefix_len;
204 "ioctl failed at line %d: %s\n",
219 "ioctl failed at line %d: %s\n",
227 * Add the UP and RUNNING flags
229 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
235 "ioctl failed at line %d: %s\n",
245 "close failed: %s\n",
253 * @brief Sets the IPv4-Address given in address on the interface dev
255 * @param dev the interface to configure
256 * @param address the IPv4-Address
257 * @param mask the netmask
260 set_address4 (const char *dev,
265 struct sockaddr_in *addr;
268 memset (&ifr, 0, sizeof (struct ifreq));
269 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
270 addr->sin_family = AF_INET;
275 if (1 != inet_pton (AF_INET,
277 &addr->sin_addr.s_addr))
280 "Failed to parse IPv4 address `%s'\n",
285 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
288 "Error creating socket: %s\n",
293 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
298 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
301 "ioctl failed at %d: %s\n",
311 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
312 if (1 != inet_pton (AF_INET,
314 &addr->sin_addr.s_addr))
317 "Failed to parse IPv4 address mask `%s'\n",
326 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
329 "ioctl failed at line %d: %s\n",
339 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
342 "ioctl failed at line %d: %s\n",
350 * Add the UP and RUNNING flags
352 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
353 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
356 "ioctl failed at line %d: %s\n",
366 "close failed: %s\n",
375 * Start forwarding to and from the tunnel.
377 * @param fd_tun tunnel FD
383 * The buffer filled by reading from fd_tun
385 unsigned char buftun[MAX_SIZE];
386 ssize_t buftun_size = 0;
387 unsigned char *buftun_read = NULL;
390 * The buffer filled by reading from stdin
392 unsigned char bufin[MAX_SIZE];
393 ssize_t bufin_size = 0;
394 size_t bufin_rpos = 0;
395 unsigned char *bufin_read = NULL;
400 /* read refers to reading from fd_tun, writing to stdout */
403 /* write refers to reading from stdin, writing to fd_tun */
406 while ((1 == read_open) && (1 == write_open))
412 * We are supposed to read and the buffer is empty
413 * -> select on read from tun
415 if (read_open && (0 == buftun_size))
416 FD_SET (fd_tun, &fds_r);
419 * We are supposed to read and the buffer is not empty
420 * -> select on write to stdout
422 if (read_open && (0 != buftun_size))
426 * We are supposed to write and the buffer is empty
427 * -> select on read from stdin
429 if (write_open && (NULL == bufin_read))
433 * We are supposed to write and the buffer is not empty
434 * -> select on write to tun
436 if (write_open && (NULL != bufin_read))
437 FD_SET (fd_tun, &fds_w);
439 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
446 "select failed: %s\n",
453 if (FD_ISSET (fd_tun, &fds_r))
456 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
457 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
458 if (-1 == buftun_size)
463 shutdown (fd_tun, SHUT_RD);
464 shutdown (1, SHUT_WR);
468 else if (0 == buftun_size)
470 fprintf (stderr, "EOF on tun\n");
471 shutdown (fd_tun, SHUT_RD);
472 shutdown (1, SHUT_WR);
478 buftun_read = buftun;
479 struct GNUNET_MessageHeader *hdr =
480 (struct GNUNET_MessageHeader *) buftun;
481 buftun_size += sizeof (struct GNUNET_MessageHeader);
482 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
483 hdr->size = htons (buftun_size);
486 else if (FD_ISSET (1, &fds_w))
488 ssize_t written = write (1,
498 "write-error to stdout: %s\n",
500 shutdown (fd_tun, SHUT_RD);
501 shutdown (1, SHUT_WR);
505 else if (0 == written)
508 "write returned 0!?\n");
513 buftun_size -= written;
514 buftun_read += written;
518 if (FD_ISSET (0, &fds_r))
520 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
521 if (-1 == bufin_size)
526 shutdown (0, SHUT_RD);
527 shutdown (fd_tun, SHUT_WR);
531 else if (0 == bufin_size)
534 fprintf (stderr, "EOF on stdin\n");
536 shutdown (0, SHUT_RD);
537 shutdown (fd_tun, SHUT_WR);
543 struct GNUNET_MessageHeader *hdr;
546 bufin_rpos += bufin_size;
547 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
549 hdr = (struct GNUNET_MessageHeader *) bufin;
550 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
553 "protocol violation!\n");
556 if (ntohs (hdr->size) > bufin_rpos)
558 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
559 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
560 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
563 else if (FD_ISSET (fd_tun, &fds_w))
565 ssize_t written = write (fd_tun,
572 "write-error to tun: %s\n",
574 shutdown (0, SHUT_RD);
575 shutdown (fd_tun, SHUT_WR);
579 else if (0 == written)
581 fprintf (stderr, "write returned 0!?\n");
586 bufin_size -= written;
587 bufin_read += written;
590 memmove (bufin, bufin_read, bufin_rpos);
591 bufin_read = NULL; /* start reading again */
603 * Open VPN tunnel interface.
605 * @param argc must be 6
606 * @param argv 0: binary name (gnunet-helper-vpn)
607 * 1: tunnel interface name (gnunet-vpn)
608 * 2: IPv6 address (::1), "-" to disable
609 * 3: IPv6 netmask length in bits (64), ignored if #2 is "-"
610 * 4: IPv4 address (1.2.3.4), "-" to disable
611 * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
614 main (int argc, char **argv)
622 fprintf (stderr, "Fatal: must supply 5 arguments!\n");
629 dev[IFNAMSIZ - 1] = '\0';
631 if (-1 == (fd_tun = init_tun (dev)))
634 "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
643 if (0 != strcmp (argv[2], "-"))
645 const char *address = argv[2];
646 long prefix_len = atol (argv[3]);
648 if ((prefix_len < 1) || (prefix_len > 127))
651 "Fatal: prefix_len out of range\n");
660 if (0 != strcmp (argv[4], "-"))
662 const char *address = argv[4];
663 const char *mask = argv[5];
665 set_address4 (dev, address, mask);
668 uid_t uid = getuid ();
669 #ifdef HAVE_SETRESUID
670 if (0 != setresuid (uid, uid, uid))
673 "Failed to setresuid: %s\n",
679 if (0 != (setuid (uid) | seteuid (uid)))
682 "Failed to setuid: %s\n",
689 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
692 "Failed to protect against SIGPIPE: %s\n",
694 /* no exit, we might as well die with SIGPIPE should it ever happen */