2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_common.h"
44 * Need VPN message types.
46 #include "gnunet_protocols.h"
49 * Should we print (interesting|debug) messages that can happen during
52 #define DEBUG GNUNET_NO
55 * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
57 #define MAX_SIZE 65536
61 * This is in linux/include/net/ipv6.h, but not always exported...
65 struct in6_addr ifr6_addr;
66 uint32_t ifr6_prefixlen;
67 unsigned int ifr6_ifindex;
73 * Creates a tun-interface called dev;
75 * @param dev is asumed to point to a char[IFNAMSIZ]
76 * if *dev == '\\0', uses the name supplied by the kernel;
77 * @return the fd to the tun or -1 on error
91 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
93 fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun",
100 fprintf (stderr, "File descriptor to large: %d", fd);
105 memset (&ifr, 0, sizeof (ifr));
106 ifr.ifr_flags = IFF_TUN;
109 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
111 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
113 fprintf (stderr, "Error with ioctl on `%s': %s\n", "/dev/net/tun",
118 strcpy (dev, ifr.ifr_name);
124 * @brief Sets the IPv6-Address given in address on the interface dev
126 * @param dev the interface to configure
127 * @param address the IPv6-Address
128 * @param prefix_len the length of the network-prefix
131 set_address6 (const char *dev, const char *address, unsigned long prefix_len)
134 struct in6_ifreq ifr6;
135 struct sockaddr_in6 sa6;
139 * parse the new address
141 memset (&sa6, 0, sizeof (struct sockaddr_in6));
142 sa6.sin6_family = AF_INET6;
143 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
145 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
150 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
152 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
156 memset (&ifr, 0, sizeof (struct ifreq));
158 * Get the index of the if
160 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
161 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
163 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
168 memset (&ifr6, 0, sizeof (struct in6_ifreq));
169 ifr6.ifr6_addr = sa6.sin6_addr;
170 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
171 ifr6.ifr6_prefixlen = prefix_len;
176 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
178 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
187 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
189 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
196 * Add the UP and RUNNING flags
198 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
199 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
201 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
209 fprintf (stderr, "close failed: %s\n", strerror (errno));
216 * @brief Sets the IPv4-Address given in address on the interface dev
218 * @param dev the interface to configure
219 * @param address the IPv4-Address
220 * @param mask the netmask
223 set_address4 (const char *dev, const char *address, const char *mask)
226 struct sockaddr_in *addr;
229 memset (&ifr, 0, sizeof (struct ifreq));
230 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
231 addr->sin_family = AF_INET;
236 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
238 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
243 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
245 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
249 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
254 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
256 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
264 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
265 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
267 fprintf (stderr, "Failed to parse address `%s': %s\n", mask,
276 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
278 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
287 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
289 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
296 * Add the UP and RUNNING flags
298 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
299 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
301 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
309 fprintf (stderr, "close failed: %s\n", strerror (errno));
317 * Start forwarding to and from the tunnel.
319 * @param fd_tun tunnel FD
325 * The buffer filled by reading from fd_tun
327 unsigned char buftun[MAX_SIZE];
328 ssize_t buftun_size = 0;
329 unsigned char *buftun_read = NULL;
332 * The buffer filled by reading from stdin
334 unsigned char bufin[MAX_SIZE];
335 ssize_t bufin_size = 0;
336 size_t bufin_rpos = 0;
337 unsigned char *bufin_read = NULL;
342 /* read refers to reading from fd_tun, writing to stdout */
345 /* write refers to reading from stdin, writing to fd_tun */
348 while ((1 == read_open) || (1 == write_open))
354 * We are supposed to read and the buffer is empty
355 * -> select on read from tun
357 if (read_open && (0 == buftun_size))
358 FD_SET (fd_tun, &fds_r);
361 * We are supposed to read and the buffer is not empty
362 * -> select on write to stdout
364 if (read_open && (0 != buftun_size))
368 * We are supposed to write and the buffer is empty
369 * -> select on read from stdin
371 if (write_open && (NULL == bufin_read))
375 * We are supposed to write and the buffer is not empty
376 * -> select on write to tun
378 if (write_open && (NULL != bufin_read))
379 FD_SET (fd_tun, &fds_w);
381 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
387 fprintf (stderr, "select failed: %s\n", strerror (errno));
393 if (FD_ISSET (fd_tun, &fds_r))
396 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
397 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
398 if (-1 == buftun_size)
400 fprintf (stderr, "read-error: %s\n", strerror (errno));
401 shutdown (fd_tun, SHUT_RD);
402 shutdown (1, SHUT_WR);
406 else if (0 == buftun_size)
408 fprintf (stderr, "EOF on tun\n");
409 shutdown (fd_tun, SHUT_RD);
410 shutdown (1, SHUT_WR);
416 buftun_read = buftun;
417 struct GNUNET_MessageHeader *hdr =
418 (struct GNUNET_MessageHeader *) buftun;
419 buftun_size += sizeof (struct GNUNET_MessageHeader);
420 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
421 hdr->size = htons (buftun_size);
424 else if (FD_ISSET (1, &fds_w))
426 ssize_t written = write (1, buftun_read, buftun_size);
433 fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
434 shutdown (fd_tun, SHUT_RD);
435 shutdown (1, SHUT_WR);
439 else if (0 == written)
441 fprintf (stderr, "write returned 0!?\n");
446 buftun_size -= written;
447 buftun_read += written;
451 if (FD_ISSET (0, &fds_r))
453 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
454 if (-1 == bufin_size)
456 fprintf (stderr, "read-error: %s\n", strerror (errno));
457 shutdown (0, SHUT_RD);
458 shutdown (fd_tun, SHUT_WR);
462 else if (0 == bufin_size)
465 fprintf (stderr, "EOF on stdin\n");
467 shutdown (0, SHUT_RD);
468 shutdown (fd_tun, SHUT_WR);
474 struct GNUNET_MessageHeader *hdr;
477 bufin_rpos += bufin_size;
478 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
480 hdr = (struct GNUNET_MessageHeader *) bufin;
481 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
483 fprintf (stderr, "protocol violation!\n");
486 if (ntohs (hdr->size) > bufin_rpos)
488 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
489 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
490 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
493 else if (FD_ISSET (fd_tun, &fds_w))
495 ssize_t written = write (fd_tun, bufin_read, bufin_size);
499 fprintf (stderr, "write-error to tun: %s\n", strerror (errno));
500 shutdown (0, SHUT_RD);
501 shutdown (fd_tun, SHUT_WR);
505 else if (0 == written)
507 fprintf (stderr, "write returned 0!?\n");
512 bufin_size -= written;
513 bufin_read += written;
516 memmove (bufin, bufin_read, bufin_rpos);
517 bufin_read = NULL; /* start reading again */
529 * Open VPN tunnel interface.
531 * @param argc must be 6
532 * @param argv 0: binary name (gnunet-helper-vpn)
533 * 1: tunnel interface name (gnunet-vpn)
534 * 2: IPv6 address (::1), "-" to disable
535 * 3: IPv6 netmask length in bits (64), ignored if #2 is "-"
536 * 4: IPv4 address (1.2.3.4), "-" to disable
537 * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
540 main (int argc, char **argv)
548 fprintf (stderr, "Fatal: must supply 5 arguments!\n");
552 strncpy (dev, argv[1], IFNAMSIZ);
553 dev[IFNAMSIZ - 1] = '\0';
555 if (-1 == (fd_tun = init_tun (dev)))
557 fprintf (stderr, "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
566 if (0 != strcmp (argv[2], "-"))
568 const char *address = argv[2];
569 long prefix_len = atol (argv[3]);
571 if ((prefix_len < 1) || (prefix_len > 127))
573 fprintf (stderr, "Fatal: prefix_len out of range\n");
577 set_address6 (dev, address, prefix_len);
580 if (0 != strcmp (argv[4], "-"))
582 const char *address = argv[4];
583 const char *mask = argv[5];
585 set_address4 (dev, address, mask);
588 uid_t uid = getuid ();
589 #ifdef HAVE_SETRESUID
590 if (0 != setresuid (uid, uid, uid))
592 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
597 if (0 != (setuid (uid) | seteuid (uid)))
599 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
605 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
607 fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
609 /* no exit, we might as well die with SIGPIPE should it ever happen */