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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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):
41 * Need 'struct GNUNET_MessageHeader'.
43 #include "gnunet_crypto_lib.h"
44 #include "gnunet_common.h"
47 * Need VPN message types.
49 #include "gnunet_protocols.h"
52 * Should we print (interesting|debug) messages that can happen during
55 #define DEBUG GNUNET_NO
58 * Maximum size of a GNUnet message (GNUNET_MAX_MESSAGE_SIZE)
60 #define MAX_SIZE 65536
64 * This is in linux/include/net/ipv6.h, but not always exported...
68 struct in6_addr ifr6_addr;
69 uint32_t ifr6_prefixlen;
70 unsigned int ifr6_ifindex;
76 * Creates a tun-interface called dev;
78 * @param dev is asumed to point to a char[IFNAMSIZ]
79 * if *dev == '\\0', uses the name supplied by the kernel;
80 * @return the fd to the tun or -1 on error
94 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
97 "Error opening `%s': %s\n",
103 if (fd >= FD_SETSIZE)
106 "File descriptor to large: %d",
112 memset (&ifr, 0, sizeof(ifr));
113 ifr.ifr_flags = IFF_TUN;
116 strncpy (ifr.ifr_name,
125 "Error with ioctl on `%s': %s\n",
131 strcpy (dev, ifr.ifr_name);
137 * @brief Sets the IPv6-Address given in address on the interface dev
139 * @param dev the interface to configure
140 * @param address the IPv6-Address
141 * @param prefix_len the length of the network-prefix
144 set_address6 (const char *dev,
146 unsigned long prefix_len)
149 struct in6_ifreq ifr6;
150 struct sockaddr_in6 sa6;
154 * parse the new address
156 memset (&sa6, 0, sizeof(struct sockaddr_in6));
157 sa6.sin6_family = AF_INET6;
158 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
161 "Failed to parse IPv6 address `%s'\n",
166 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
169 "Error creating socket: %s\n",
174 memset (&ifr, 0, sizeof(struct ifreq));
176 * Get the index of the if
178 strncpy (ifr.ifr_name,
186 "ioctl failed at %d: %s\n",
193 memset (&ifr6, 0, sizeof(struct in6_ifreq));
194 ifr6.ifr6_addr = sa6.sin6_addr;
195 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
196 ifr6.ifr6_prefixlen = prefix_len;
206 "ioctl failed at line %d: %s\n",
221 "ioctl failed at line %d: %s\n",
229 * Add the UP and RUNNING flags
231 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
237 "ioctl failed at line %d: %s\n",
247 "close failed: %s\n",
255 * @brief Sets the IPv4-Address given in address on the interface dev
257 * @param dev the interface to configure
258 * @param address the IPv4-Address
259 * @param mask the netmask
262 set_address4 (const char *dev,
267 struct sockaddr_in *addr;
270 memset (&ifr, 0, sizeof(struct ifreq));
271 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
272 addr->sin_family = AF_INET;
277 if (1 != inet_pton (AF_INET,
279 &addr->sin_addr.s_addr))
282 "Failed to parse IPv4 address `%s'\n",
287 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
290 "Error creating socket: %s\n",
295 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
300 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
303 "ioctl failed at %d: %s\n",
313 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
314 if (1 != inet_pton (AF_INET,
316 &addr->sin_addr.s_addr))
319 "Failed to parse IPv4 address mask `%s'\n",
328 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
331 "ioctl failed at line %d: %s\n",
341 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
344 "ioctl failed at line %d: %s\n",
352 * Add the UP and RUNNING flags
354 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
355 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
358 "ioctl failed at line %d: %s\n",
368 "close failed: %s\n",
377 * Start forwarding to and from the tunnel.
379 * @param fd_tun tunnel FD
385 * The buffer filled by reading from fd_tun
387 unsigned char buftun[MAX_SIZE];
388 ssize_t buftun_size = 0;
389 unsigned char *buftun_read = NULL;
392 * The buffer filled by reading from stdin
394 unsigned char bufin[MAX_SIZE];
395 ssize_t bufin_size = 0;
396 size_t bufin_rpos = 0;
397 unsigned char *bufin_read = NULL;
402 /* read refers to reading from fd_tun, writing to stdout */
405 /* write refers to reading from stdin, writing to fd_tun */
408 while ((1 == read_open) && (1 == write_open))
414 * We are supposed to read and the buffer is empty
415 * -> select on read from tun
417 if (read_open && (0 == buftun_size))
418 FD_SET (fd_tun, &fds_r);
421 * We are supposed to read and the buffer is not empty
422 * -> select on write to stdout
424 if (read_open && (0 != buftun_size))
428 * We are supposed to write and the buffer is empty
429 * -> select on read from stdin
431 if (write_open && (NULL == bufin_read))
435 * We are supposed to write and the buffer is not empty
436 * -> select on write to tun
438 if (write_open && (NULL != bufin_read))
439 FD_SET (fd_tun, &fds_w);
441 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
448 "select failed: %s\n",
455 if (FD_ISSET (fd_tun, &fds_r))
458 read (fd_tun, buftun + sizeof(struct GNUNET_MessageHeader),
459 MAX_SIZE - sizeof(struct GNUNET_MessageHeader));
460 if (-1 == buftun_size)
465 shutdown (fd_tun, SHUT_RD);
466 shutdown (1, SHUT_WR);
470 else if (0 == buftun_size)
472 fprintf (stderr, "EOF on tun\n");
473 shutdown (fd_tun, SHUT_RD);
474 shutdown (1, SHUT_WR);
480 buftun_read = buftun;
481 struct GNUNET_MessageHeader *hdr =
482 (struct GNUNET_MessageHeader *) buftun;
483 buftun_size += sizeof(struct GNUNET_MessageHeader);
484 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
485 hdr->size = htons (buftun_size);
488 else if (FD_ISSET (1, &fds_w))
490 ssize_t written = write (1,
500 "write-error to stdout: %s\n",
502 shutdown (fd_tun, SHUT_RD);
503 shutdown (1, SHUT_WR);
507 else if (0 == written)
510 "write returned 0!?\n");
515 buftun_size -= written;
516 buftun_read += written;
520 if (FD_ISSET (0, &fds_r))
522 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
523 if (-1 == bufin_size)
528 shutdown (0, SHUT_RD);
529 shutdown (fd_tun, SHUT_WR);
533 else if (0 == bufin_size)
536 fprintf (stderr, "EOF on stdin\n");
538 shutdown (0, SHUT_RD);
539 shutdown (fd_tun, SHUT_WR);
545 struct GNUNET_MessageHeader *hdr;
548 bufin_rpos += bufin_size;
549 if (bufin_rpos < sizeof(struct GNUNET_MessageHeader))
551 hdr = (struct GNUNET_MessageHeader *) bufin;
552 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
555 "protocol violation!\n");
558 if (ntohs (hdr->size) > bufin_rpos)
560 bufin_read = bufin + sizeof(struct GNUNET_MessageHeader);
561 bufin_size = ntohs (hdr->size) - sizeof(struct GNUNET_MessageHeader);
562 bufin_rpos -= bufin_size + sizeof(struct GNUNET_MessageHeader);
565 else if (FD_ISSET (fd_tun, &fds_w))
567 ssize_t written = write (fd_tun,
574 "write-error to tun: %s\n",
576 shutdown (0, SHUT_RD);
577 shutdown (fd_tun, SHUT_WR);
581 else if (0 == written)
583 fprintf (stderr, "write returned 0!?\n");
588 bufin_size -= written;
589 bufin_read += written;
592 memmove (bufin, bufin_read, bufin_rpos);
593 bufin_read = NULL; /* start reading again */
605 * Open VPN tunnel interface.
607 * @param argc must be 6
608 * @param argv 0: binary name (gnunet-helper-vpn)
609 * 1: tunnel interface name (gnunet-vpn)
610 * 2: IPv6 address (::1), "-" to disable
611 * 3: IPv6 netmask length in bits (64), ignored if #2 is "-"
612 * 4: IPv4 address (1.2.3.4), "-" to disable
613 * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
616 main (int argc, char **argv)
624 fprintf (stderr, "Fatal: must supply 5 arguments!\n");
631 dev[IFNAMSIZ - 1] = '\0';
633 if (-1 == (fd_tun = init_tun (dev)))
636 "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
645 if (0 != strcmp (argv[2], "-"))
647 const char *address = argv[2];
648 long prefix_len = atol (argv[3]);
650 if ((prefix_len < 1) || (prefix_len > 127))
653 "Fatal: prefix_len out of range\n");
663 if (0 != strcmp (argv[4], "-"))
665 const char *address = argv[4];
666 const char *mask = argv[5];
668 set_address4 (dev, address, mask);
671 uid_t uid = getuid ();
672 #ifdef HAVE_SETRESUID
673 if (0 != setresuid (uid, uid, uid))
676 "Failed to setresuid: %s\n",
682 if (0 != (setuid (uid) | seteuid (uid)))
685 "Failed to setuid: %s\n",
692 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
695 "Failed to protect against SIGPIPE: %s\n",
697 /* no exit, we might as well die with SIGPIPE should it ever happen */