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):
33 * - Christian Grothoff
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 * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
51 #define MAX_SIZE 65536
55 * This is in linux/include/net/ipv6.h, but not always exported...
59 struct in6_addr ifr6_addr;
60 uint32_t ifr6_prefixlen;
61 unsigned int ifr6_ifindex;
66 * Creates a tun-interface called dev;
67 * @param dev is asumed to point to a char[IFNAMSIZ]
68 * if *dev == '\\0', uses the name supplied by the kernel
69 * @return the fd to the tun or -1 on error
83 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
86 "Error opening `%s': %s\n", "/dev/net/tun", strerror (errno));
92 fprintf (stderr, "File descriptor to large: %d", fd);
96 memset (&ifr, 0, sizeof (ifr));
97 ifr.ifr_flags = IFF_TUN;
100 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
102 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
105 "Error with ioctl on `%s': %s\n",
106 "/dev/net/tun", strerror (errno));
110 strcpy (dev, ifr.ifr_name);
116 * @brief Sets the IPv6-Address given in address on the interface dev
118 * @param dev the interface to configure
119 * @param address the IPv6-Address
120 * @param prefix_len the length of the network-prefix
123 set_address6 (const char *dev,
125 unsigned long prefix_len)
128 struct in6_ifreq ifr6;
129 struct sockaddr_in6 sa6;
133 * parse the new address
135 memset (&sa6, 0, sizeof (struct sockaddr_in6));
136 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
139 "Failed to parse address `%s': %s\n",
140 address, strerror (errno));
144 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
147 "Error creating socket: %s\n",
152 sa6.sin6_family = AF_INET6;
153 memcpy (&ifr6.ifr6_addr,
155 sizeof (struct in6_addr));
159 * Get the index of the if
161 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
162 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
165 "ioctl failed at %d: %s\n",
170 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
172 ifr6.ifr6_prefixlen = prefix_len;
177 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
180 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
187 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
190 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
195 * Add the UP and RUNNING flags
197 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
198 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
201 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
207 fprintf (stderr, "close failed: %s\n", strerror (errno));
214 * @brief Sets the IPv4-Address given in address on the interface dev
216 * @param dev the interface to configure
217 * @param address the IPv4-Address
218 * @param mask the netmask
221 set_address4 (const char *dev,
226 struct sockaddr_in *addr;
229 memset (&ifr, 0, sizeof (struct ifreq));
230 addr = (struct sockaddr_in *) &(ifr.ifr_addr);
231 memset (addr, 0, sizeof (struct sockaddr_in));
232 addr->sin_family = AF_INET;
233 addr->sin_addr.s_addr = inet_addr (address);
238 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
241 "Failed to parse address `%s': %s\n",
242 address, strerror (errno));
247 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
250 "Error creating socket: %s\n",
255 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
260 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
263 "ioctl failed at %d: %s\n",
272 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
273 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
276 "Failed to parse address `%s': %s\n",
285 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
288 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
295 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
298 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
303 * Add the UP and RUNNING flags
305 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
306 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
309 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
315 fprintf (stderr, "close failed: %s\n", strerror (errno));
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;
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 */
344 /* write refers to reading from stdin, writing to fd_tun */
347 while ((1 == read_open) || (1 == write_open))
353 * We are supposed to read and the buffer is empty
354 * -> select on read from tun
356 if (read_open && (0 == buftun_size))
357 FD_SET (fd_tun, &fds_r);
360 * We are supposed to read and the buffer is not empty
361 * -> select on write to stdout
363 if (read_open && (0 != buftun_size))
367 * We are supposed to write and the buffer is empty
368 * -> select on read from stdin
370 if (write_open && (NULL == bufin_read))
374 * We are supposed to write and the buffer is not empty
375 * -> select on write to tun
377 if (write_open && (NULL != bufin_read))
378 FD_SET (fd_tun, &fds_w);
380 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
385 fprintf (stderr, "select failed: %s\n", strerror (errno));
391 if (FD_ISSET (fd_tun, &fds_r))
394 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
395 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
396 if (-1 == buftun_size)
398 fprintf (stderr, "read-error: %s\n", strerror (errno));
399 shutdown (fd_tun, SHUT_RD);
400 shutdown (1, SHUT_WR);
404 else if (0 == buftun_size)
406 fprintf (stderr, "EOF on tun\n");
407 shutdown (fd_tun, SHUT_RD);
408 shutdown (1, SHUT_WR);
414 buftun_read = buftun;
415 struct GNUNET_MessageHeader *hdr =
416 (struct GNUNET_MessageHeader *) buftun;
417 buftun_size += sizeof (struct GNUNET_MessageHeader);
418 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
419 hdr->size = htons (buftun_size);
422 else if (FD_ISSET (1, &fds_w))
424 ssize_t written = write (1, buftun_read, buftun_size);
428 "write-error to stdout: %s\n",
430 shutdown (fd_tun, SHUT_RD);
431 shutdown (1, SHUT_WR);
435 else if (0 == written)
438 "write returned 0!?\n");
443 buftun_size -= written;
444 buftun_read += written;
448 if (FD_ISSET (0, &fds_r))
450 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
451 if (-1 == bufin_size)
456 shutdown (0, SHUT_RD);
457 shutdown (fd_tun, SHUT_WR);
461 else if (0 == bufin_size)
465 shutdown (0, SHUT_RD);
466 shutdown (fd_tun, SHUT_WR);
472 struct GNUNET_MessageHeader *hdr;
475 bufin_rpos += bufin_size;
476 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
478 hdr = (struct GNUNET_MessageHeader *) bufin;
479 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
481 fprintf (stderr, "protocol violation!\n");
484 if (ntohs (hdr->size) > bufin_rpos)
486 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
487 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
488 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
491 else if (FD_ISSET (fd_tun, &fds_w))
493 ssize_t written = write (fd_tun, bufin_read, bufin_size);
496 fprintf (stderr, "write-error to tun: %s\n",
498 shutdown (0, SHUT_RD);
499 shutdown (fd_tun, SHUT_WR);
503 else if (0 == written)
506 "write returned 0!?\n");
511 bufin_size -= written;
512 bufin_read += written;
518 bufin_read = NULL; /* start reading again */
530 main (int argc, char **argv)
535 memset (dev, 0, IFNAMSIZ);
536 if (-1 == (fd_tun = init_tun (dev)))
539 "Fatal: could not initialize tun-interface\n");
546 "Fatal: must supply 4 arguments!\n");
551 const char *address = argv[1];
552 long prefix_len = atol(argv[2]);
554 if ( (prefix_len < 1) || (prefix_len > 127) )
556 fprintf(stderr, "Fatal: prefix_len out of range\n");
560 set_address6 (dev, address, prefix_len);
564 const char *address = argv[3];
565 const char *mask = argv[4];
567 set_address4 (dev, address, mask);
570 uid_t uid = getuid ();
571 if (0 != setresuid (uid, uid, uid))
573 "Failed to setresuid: %s\n",
575 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
577 "Failed to protect against SIGPIPE: %s\n",