2 This file is part of GNUnet.
3 (C) 2010 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-daemon-vpn.c
23 * @brief the helper for various vpn-daemons. 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
28 * The following list of people have reviewed this code and considered
29 * it safe since the last modification (if you reviewed it, please
30 * have your name added to the list):
35 #include <linux/if_tun.h>
38 * Need 'struct GNUNET_MessageHeader'.
40 #include "gnunet_common.h"
43 * Need VPN message types.
45 #include "gnunet_protocols.h"
48 * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
50 #define MAX_SIZE 65536
54 * This is in linux/include/net/ipv6.h, but not always exported...
58 struct in6_addr ifr6_addr;
59 uint32_t ifr6_prefixlen;
60 unsigned int ifr6_ifindex;
66 * Creates a tun-interface called dev;
68 * @param dev is asumed to point to a char[IFNAMSIZ]
69 * if *dev == '\\0', uses the name supplied by the kernel;
70 * @return the fd to the tun or -1 on error
84 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
86 fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun",
93 fprintf (stderr, "File descriptor to large: %d", fd);
98 memset (&ifr, 0, sizeof (ifr));
99 ifr.ifr_flags = IFF_TUN;
102 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
104 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
106 fprintf (stderr, "Error with ioctl on `%s': %s\n", "/dev/net/tun",
111 strcpy (dev, ifr.ifr_name);
117 * @brief Sets the IPv6-Address given in address on the interface dev
119 * @param dev the interface to configure
120 * @param address the IPv6-Address
121 * @param prefix_len the length of the network-prefix
124 set_address6 (const char *dev, const char *address, unsigned long prefix_len)
127 struct in6_ifreq ifr6;
128 struct sockaddr_in6 sa6;
132 * parse the new address
134 memset (&sa6, 0, sizeof (struct sockaddr_in6));
135 sa6.sin6_family = AF_INET6;
136 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
138 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
143 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
145 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
149 memset (&ifr, 0, sizeof (struct ifreq));
151 * Get the index of the if
153 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
154 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
156 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
161 memset (&ifr6, 0, sizeof (struct in6_ifreq));
162 ifr6.ifr6_addr = sa6.sin6_addr;
163 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
164 ifr6.ifr6_prefixlen = prefix_len;
169 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
171 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
180 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
182 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
189 * Add the UP and RUNNING flags
191 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
192 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
194 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
202 fprintf (stderr, "close failed: %s\n", strerror (errno));
209 * @brief Sets the IPv4-Address given in address on the interface dev
211 * @param dev the interface to configure
212 * @param address the IPv4-Address
213 * @param mask the netmask
216 set_address4 (const char *dev, const char *address, const char *mask)
219 struct sockaddr_in *addr;
222 memset (&ifr, 0, sizeof (struct ifreq));
223 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
224 addr->sin_family = AF_INET;
229 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
231 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
236 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
238 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
242 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
247 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
249 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
257 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
258 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
260 fprintf (stderr, "Failed to parse address `%s': %s\n", mask,
269 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
271 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
280 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
282 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
289 * Add the UP and RUNNING flags
291 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
292 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
294 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
302 fprintf (stderr, "close failed: %s\n", strerror (errno));
310 * Start forwarding to and from the tunnel.
312 * @param fd_tun tunnel FD
318 * The buffer filled by reading from fd_tun
320 unsigned char buftun[MAX_SIZE];
321 ssize_t buftun_size = 0;
322 unsigned char *buftun_read = NULL;
325 * The buffer filled by reading from stdin
327 unsigned char bufin[MAX_SIZE];
328 ssize_t bufin_size = 0;
329 size_t bufin_rpos = 0;
330 unsigned char *bufin_read = NULL;
335 /* read refers to reading from fd_tun, writing to stdout */
338 /* write refers to reading from stdin, writing to fd_tun */
341 while ((1 == read_open) || (1 == write_open))
347 * We are supposed to read and the buffer is empty
348 * -> select on read from tun
350 if (read_open && (0 == buftun_size))
351 FD_SET (fd_tun, &fds_r);
354 * We are supposed to read and the buffer is not empty
355 * -> select on write to stdout
357 if (read_open && (0 != buftun_size))
361 * We are supposed to write and the buffer is empty
362 * -> select on read from stdin
364 if (write_open && (NULL == bufin_read))
368 * We are supposed to write and the buffer is not empty
369 * -> select on write to tun
371 if (write_open && (NULL != bufin_read))
372 FD_SET (fd_tun, &fds_w);
374 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
380 fprintf (stderr, "select failed: %s\n", strerror (errno));
386 if (FD_ISSET (fd_tun, &fds_r))
389 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
390 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
391 if (-1 == buftun_size)
393 fprintf (stderr, "read-error: %s\n", strerror (errno));
394 shutdown (fd_tun, SHUT_RD);
395 shutdown (1, SHUT_WR);
399 else if (0 == buftun_size)
401 fprintf (stderr, "EOF on tun\n");
402 shutdown (fd_tun, SHUT_RD);
403 shutdown (1, SHUT_WR);
409 buftun_read = buftun;
410 struct GNUNET_MessageHeader *hdr =
411 (struct GNUNET_MessageHeader *) buftun;
412 buftun_size += sizeof (struct GNUNET_MessageHeader);
413 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
414 hdr->size = htons (buftun_size);
417 else if (FD_ISSET (1, &fds_w))
419 ssize_t written = write (1, buftun_read, buftun_size);
423 fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
424 shutdown (fd_tun, SHUT_RD);
425 shutdown (1, SHUT_WR);
429 else if (0 == written)
431 fprintf (stderr, "write returned 0!?\n");
436 buftun_size -= written;
437 buftun_read += written;
441 if (FD_ISSET (0, &fds_r))
443 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
444 if (-1 == bufin_size)
446 fprintf (stderr, "read-error: %s\n", strerror (errno));
447 shutdown (0, SHUT_RD);
448 shutdown (fd_tun, SHUT_WR);
452 else if (0 == bufin_size)
454 fprintf (stderr, "EOF on stdin\n");
455 shutdown (0, SHUT_RD);
456 shutdown (fd_tun, SHUT_WR);
462 struct GNUNET_MessageHeader *hdr;
465 bufin_rpos += bufin_size;
466 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
468 hdr = (struct GNUNET_MessageHeader *) bufin;
469 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
471 fprintf (stderr, "protocol violation!\n");
474 if (ntohs (hdr->size) > bufin_rpos)
476 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
477 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
478 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
481 else if (FD_ISSET (fd_tun, &fds_w))
483 ssize_t written = write (fd_tun, bufin_read, bufin_size);
487 fprintf (stderr, "write-error to tun: %s\n", strerror (errno));
488 shutdown (0, SHUT_RD);
489 shutdown (fd_tun, SHUT_WR);
493 else if (0 == written)
495 fprintf (stderr, "write returned 0!?\n");
500 bufin_size -= written;
501 bufin_read += written;
504 memmove (bufin, bufin_read, bufin_rpos);
505 bufin_read = NULL; /* start reading again */
517 * Open VPN tunnel interface.
519 * @param argc must be 6
520 * @param argv 0: binary name (gnunet-helper-vpn)
521 * 1: tunnel interface name (gnunet-vpn)
522 * 2: IPv6 address (::1)
523 * 3: IPv6 netmask length in bits (64)
524 * 4: IPv4 address (1.2.3.4)
525 * 5: IPv4 netmask (255.255.0.0)
528 main (int argc, char **argv)
536 fprintf (stderr, "Fatal: must supply 5 arguments!\n");
540 strncpy (dev, argv[1], IFNAMSIZ);
541 dev[IFNAMSIZ - 1] = '\0';
543 if (-1 == (fd_tun = init_tun (dev)))
545 fprintf (stderr, "Fatal: could not initialize tun-interface\n");
550 const char *address = argv[2];
551 long prefix_len = atol (argv[3]);
553 if ((prefix_len < 1) || (prefix_len > 127))
555 fprintf (stderr, "Fatal: prefix_len out of range\n");
559 set_address6 (dev, address, prefix_len);
563 const char *address = argv[4];
564 const char *mask = argv[5];
566 set_address4 (dev, address, mask);
569 uid_t uid = getuid ();
570 #ifdef HAVE_SETRESUID
571 if (0 != setresuid (uid, uid, uid))
573 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
578 if (0 != (setuid (uid) | seteuid (uid)))
580 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
586 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
588 fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
590 /* no exit, we might as well die with SIGPIPE should it ever happen */