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 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.
17 * @file vpn/gnunet-helper-vpn.c
18 * @brief the helper for the VPN service. Opens a virtual network-interface,
19 * sends data received on the if to stdout, sends data received on stdin to the
21 * @author Philipp Tölke
22 * @author Christian Grothoff
24 * The following list of people have reviewed this code and considered
25 * it safe since the last modification (if you reviewed it, please
26 * have your name added to the list):
31 #include <linux/if_tun.h>
34 * Need 'struct GNUNET_MessageHeader'.
36 #include "gnunet_crypto_lib.h"
37 #include "gnunet_common.h"
40 * Need VPN message types.
42 #include "gnunet_protocols.h"
45 * Should we print (interesting|debug) messages that can happen during
48 #define DEBUG GNUNET_NO
51 * Maximum size of a GNUnet message (GNUNET_MAX_MESSAGE_SIZE)
53 #define MAX_SIZE 65536
57 * This is in linux/include/net/ipv6.h, but not always exported...
61 struct in6_addr ifr6_addr;
62 uint32_t ifr6_prefixlen;
63 unsigned int ifr6_ifindex;
69 * Creates a tun-interface called dev;
71 * @param dev is asumed to point to a char[IFNAMSIZ]
72 * if *dev == '\\0', uses the name supplied by the kernel;
73 * @return the fd to the tun or -1 on error
87 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
90 "Error opening `%s': %s\n",
99 "File descriptor to large: %d",
105 memset (&ifr, 0, sizeof (ifr));
106 ifr.ifr_flags = IFF_TUN;
109 strncpy (ifr.ifr_name,
118 "Error with ioctl on `%s': %s\n",
124 strcpy (dev, ifr.ifr_name);
130 * @brief Sets the IPv6-Address given in address on the interface dev
132 * @param dev the interface to configure
133 * @param address the IPv6-Address
134 * @param prefix_len the length of the network-prefix
137 set_address6 (const char *dev,
139 unsigned long prefix_len)
142 struct in6_ifreq ifr6;
143 struct sockaddr_in6 sa6;
147 * parse the new address
149 memset (&sa6, 0, sizeof (struct sockaddr_in6));
150 sa6.sin6_family = AF_INET6;
151 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
154 "Failed to parse IPv6 address `%s'\n",
159 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
162 "Error creating socket: %s\n",
167 memset (&ifr, 0, sizeof (struct ifreq));
169 * Get the index of the if
171 strncpy (ifr.ifr_name,
179 "ioctl failed at %d: %s\n",
186 memset (&ifr6, 0, sizeof (struct in6_ifreq));
187 ifr6.ifr6_addr = sa6.sin6_addr;
188 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
189 ifr6.ifr6_prefixlen = prefix_len;
199 "ioctl failed at line %d: %s\n",
214 "ioctl failed at line %d: %s\n",
222 * Add the UP and RUNNING flags
224 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
230 "ioctl failed at line %d: %s\n",
240 "close failed: %s\n",
248 * @brief Sets the IPv4-Address given in address on the interface dev
250 * @param dev the interface to configure
251 * @param address the IPv4-Address
252 * @param mask the netmask
255 set_address4 (const char *dev,
260 struct sockaddr_in *addr;
263 memset (&ifr, 0, sizeof (struct ifreq));
264 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
265 addr->sin_family = AF_INET;
270 if (1 != inet_pton (AF_INET,
272 &addr->sin_addr.s_addr))
275 "Failed to parse IPv4 address `%s'\n",
280 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
283 "Error creating socket: %s\n",
288 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
293 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
296 "ioctl failed at %d: %s\n",
306 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
307 if (1 != inet_pton (AF_INET,
309 &addr->sin_addr.s_addr))
312 "Failed to parse IPv4 address mask `%s'\n",
321 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
324 "ioctl failed at line %d: %s\n",
334 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
337 "ioctl failed at line %d: %s\n",
345 * Add the UP and RUNNING flags
347 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
348 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
351 "ioctl failed at line %d: %s\n",
361 "close failed: %s\n",
370 * Start forwarding to and from the tunnel.
372 * @param fd_tun tunnel FD
378 * The buffer filled by reading from fd_tun
380 unsigned char buftun[MAX_SIZE];
381 ssize_t buftun_size = 0;
382 unsigned char *buftun_read = NULL;
385 * The buffer filled by reading from stdin
387 unsigned char bufin[MAX_SIZE];
388 ssize_t bufin_size = 0;
389 size_t bufin_rpos = 0;
390 unsigned char *bufin_read = NULL;
395 /* read refers to reading from fd_tun, writing to stdout */
398 /* write refers to reading from stdin, writing to fd_tun */
401 while ((1 == read_open) && (1 == write_open))
407 * We are supposed to read and the buffer is empty
408 * -> select on read from tun
410 if (read_open && (0 == buftun_size))
411 FD_SET (fd_tun, &fds_r);
414 * We are supposed to read and the buffer is not empty
415 * -> select on write to stdout
417 if (read_open && (0 != buftun_size))
421 * We are supposed to write and the buffer is empty
422 * -> select on read from stdin
424 if (write_open && (NULL == bufin_read))
428 * We are supposed to write and the buffer is not empty
429 * -> select on write to tun
431 if (write_open && (NULL != bufin_read))
432 FD_SET (fd_tun, &fds_w);
434 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
441 "select failed: %s\n",
448 if (FD_ISSET (fd_tun, &fds_r))
451 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
452 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
453 if (-1 == buftun_size)
458 shutdown (fd_tun, SHUT_RD);
459 shutdown (1, SHUT_WR);
463 else if (0 == buftun_size)
465 fprintf (stderr, "EOF on tun\n");
466 shutdown (fd_tun, SHUT_RD);
467 shutdown (1, SHUT_WR);
473 buftun_read = buftun;
474 struct GNUNET_MessageHeader *hdr =
475 (struct GNUNET_MessageHeader *) buftun;
476 buftun_size += sizeof (struct GNUNET_MessageHeader);
477 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
478 hdr->size = htons (buftun_size);
481 else if (FD_ISSET (1, &fds_w))
483 ssize_t written = write (1,
493 "write-error to stdout: %s\n",
495 shutdown (fd_tun, SHUT_RD);
496 shutdown (1, SHUT_WR);
500 else if (0 == written)
503 "write returned 0!?\n");
508 buftun_size -= written;
509 buftun_read += written;
513 if (FD_ISSET (0, &fds_r))
515 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
516 if (-1 == bufin_size)
521 shutdown (0, SHUT_RD);
522 shutdown (fd_tun, SHUT_WR);
526 else if (0 == bufin_size)
529 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)
548 "protocol violation!\n");
551 if (ntohs (hdr->size) > bufin_rpos)
553 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
554 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
555 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
558 else if (FD_ISSET (fd_tun, &fds_w))
560 ssize_t written = write (fd_tun,
567 "write-error to tun: %s\n",
569 shutdown (0, SHUT_RD);
570 shutdown (fd_tun, SHUT_WR);
574 else if (0 == written)
576 fprintf (stderr, "write returned 0!?\n");
581 bufin_size -= written;
582 bufin_read += written;
585 memmove (bufin, bufin_read, bufin_rpos);
586 bufin_read = NULL; /* start reading again */
598 * Open VPN tunnel interface.
600 * @param argc must be 6
601 * @param argv 0: binary name (gnunet-helper-vpn)
602 * 1: tunnel interface name (gnunet-vpn)
603 * 2: IPv6 address (::1), "-" to disable
604 * 3: IPv6 netmask length in bits (64), ignored if #2 is "-"
605 * 4: IPv4 address (1.2.3.4), "-" to disable
606 * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
609 main (int argc, char **argv)
617 fprintf (stderr, "Fatal: must supply 5 arguments!\n");
624 dev[IFNAMSIZ - 1] = '\0';
626 if (-1 == (fd_tun = init_tun (dev)))
629 "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
638 if (0 != strcmp (argv[2], "-"))
640 const char *address = argv[2];
641 long prefix_len = atol (argv[3]);
643 if ((prefix_len < 1) || (prefix_len > 127))
646 "Fatal: prefix_len out of range\n");
656 if (0 != strcmp (argv[4], "-"))
658 const char *address = argv[4];
659 const char *mask = argv[5];
661 set_address4 (dev, address, mask);
664 uid_t uid = getuid ();
665 #ifdef HAVE_SETRESUID
666 if (0 != setresuid (uid, uid, uid))
669 "Failed to setresuid: %s\n",
675 if (0 != (setuid (uid) | seteuid (uid)))
678 "Failed to setuid: %s\n",
685 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
688 "Failed to protect against SIGPIPE: %s\n",
690 /* no exit, we might as well die with SIGPIPE should it ever happen */