2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008-2010 Intel Corporation.
6 * Author: David Woodhouse <dwmw2@infradead.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * version 2.1, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA
25 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
38 #include <arpa/inet.h>
43 #include <sys/sockio.h>
44 #include <net/if_tun.h>
46 #error "Install TAP driver from http://www.whiteboard.ne.jp/~admin2/tuntap/"
50 #include "openconnect-internal.h"
53 * If an if_tun.h include file was found anywhere (by the Makefile), it's
54 * included. Else, we end up assuming that we have BSD-style devices such
62 * The OS X tun/tap driver doesn't provide a header file; you're expected
63 * to define this for yourself.
66 #define TUNSIFHEAD _IOW('t', 96, int)
70 * OpenBSD always puts the protocol family prefix onto packets. Other
71 * systems let us enable that with the TUNSIFHEAD ioctl, and some of them
72 * (e.g. FreeBSD) _need_ it otherwise they'll interpret IPv6 packets as IPv4.
74 #if defined(__OpenBSD__) || defined(TUNSIFHEAD)
75 #define TUN_HAS_AF_PREFIX 1
79 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
82 vpn_progress(vpninfo, PRG_ERR,
83 _("No vpnc-script configured. Need Solaris IP-setting code\n"));
87 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
92 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
94 perror(_("open net"));
97 memset(&ifr, 0, sizeof(ifr));
98 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
101 struct sockaddr_in addr;
103 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
104 perror(_("SIOCGIFFLAGS"));
106 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
107 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
108 perror(_("SIOCSIFFLAGS"));
110 addr.sin_family = AF_INET;
111 addr.sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
112 memcpy(&ifr.ifr_addr, &addr, sizeof(addr));
113 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
114 perror(_("SIOCSIFADDR"));
117 ifr.ifr_mtu = vpninfo->mtu;
118 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
119 perror(_("SIOCSIFMTU"));
127 static int setenv_int(const char *opt, int value)
130 sprintf(buf, "%d", value);
131 return setenv(opt, buf, 1);
134 static int netmasklen(struct in_addr addr)
138 for (masklen = 0; masklen < 32; masklen++) {
139 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
145 static int process_split_xxclude(struct openconnect_info *vpninfo,
146 int include, const char *route, int *v4_incs,
150 const char *in_ex = include?"IN":"EX";
154 slash = strchr(route, '/');
158 vpn_progress(vpninfo, PRG_ERR,
159 _("Discard bad split include: \"%s\"\n"),
162 vpn_progress(vpninfo, PRG_ERR,
163 _("Discard bad split exclude: \"%s\"\n"),
170 if (strchr(route, ':')) {
171 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_ADDR", in_ex,
173 setenv(envname, route, 1);
175 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_MASKLEN", in_ex,
177 setenv(envname, slash+1, 1);
183 if (!inet_aton(route, &addr)) {
189 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *v4_incs);
190 setenv(envname, route, 1);
192 /* Put it back how we found it */
195 if (!inet_aton(slash+1, &addr))
198 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *v4_incs);
199 setenv(envname, slash+1, 1);
201 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *v4_incs);
202 setenv_int(envname, netmasklen(addr));
208 static int appendenv(const char *opt, const char *new)
211 char *old = getenv(opt);
215 snprintf(buf, 1023, "%s %s", old, new);
217 snprintf(buf, 1023, "%s", new);
219 return setenv(opt, buf, 1);
222 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
227 struct vpn_option *opt;
229 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
230 buflen += 2 + strlen(opt->option) + strlen(opt->value);
232 env_buf = malloc(buflen + 1);
238 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
239 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
240 "%s=%s\n", opt->option, opt->value);
242 setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
246 static void set_banner(struct openconnect_info *vpninfo)
251 if (!vpninfo->banner || !(banner = malloc(strlen(vpninfo->banner)))) {
252 unsetenv("CISCO_BANNER");
259 if (*p == '%' && isxdigit((int)(unsigned char)p[1]) &&
260 isxdigit((int)(unsigned char)p[2])) {
261 *(q++) = unhex(p + 1);
267 setenv("CISCO_BANNER", banner, 1);
272 static void set_script_env(struct openconnect_info *vpninfo)
275 int ret = getnameinfo(vpninfo->peer_addr, vpninfo->peer_addrlen, host,
276 sizeof(host), NULL, 0, NI_NUMERICHOST);
278 setenv("VPNGATEWAY", host, 1);
281 unsetenv("CISCO_SPLIT_INC");
282 unsetenv("CISCO_SPLIT_EXC");
284 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
286 if (vpninfo->vpn_addr) {
287 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
288 if (vpninfo->vpn_netmask) {
292 if (inet_aton(vpninfo->vpn_addr, &addr) &&
293 inet_aton(vpninfo->vpn_netmask, &mask)) {
296 addr.s_addr &= mask.s_addr;
297 netaddr = inet_ntoa(addr);
299 setenv("INTERNAL_IP4_NETADDR", netaddr, 1);
300 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
301 setenv_int("INTERNAL_IP4_NETMASKLEN", netmasklen(mask));
305 if (vpninfo->vpn_addr6) {
306 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
307 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
310 if (vpninfo->vpn_dns[0])
311 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
313 unsetenv("INTERNAL_IP4_DNS");
314 if (vpninfo->vpn_dns[1])
315 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
316 if (vpninfo->vpn_dns[2])
317 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
319 if (vpninfo->vpn_nbns[0])
320 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
322 unsetenv("INTERNAL_IP4_NBNS");
323 if (vpninfo->vpn_nbns[1])
324 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
325 if (vpninfo->vpn_nbns[2])
326 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
328 if (vpninfo->vpn_domain)
329 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
330 else unsetenv ("CISCO_DEF_DOMAIN");
332 if (vpninfo->vpn_proxy_pac)
333 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
335 if (vpninfo->split_includes) {
336 struct split_include *this = vpninfo->split_includes;
337 int nr_split_includes = 0;
338 int nr_v6_split_includes = 0;
341 process_split_xxclude(vpninfo, 1, this->route,
343 &nr_v6_split_includes);
346 if (nr_split_includes)
347 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
348 if (nr_v6_split_includes)
349 setenv_int("CISCO_IPV6_SPLIT_INC", nr_v6_split_includes);
351 if (vpninfo->split_excludes) {
352 struct split_include *this = vpninfo->split_excludes;
353 int nr_split_excludes = 0;
354 int nr_v6_split_excludes = 0;
357 process_split_xxclude(vpninfo, 0, this->route,
359 &nr_v6_split_excludes);
362 if (nr_split_excludes)
363 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
364 if (nr_v6_split_excludes)
365 setenv_int("CISCO_IPV6_SPLIT_EXC", nr_v6_split_excludes);
367 setenv_cstp_opts(vpninfo);
370 int script_config_tun(struct openconnect_info *vpninfo, const char *reason)
372 if (!vpninfo->vpnc_script)
375 setenv("reason", reason, 1);
376 if (system(vpninfo->vpnc_script)) {
378 vpn_progress(vpninfo, PRG_ERR,
379 _("Failed to spawn script '%s' for %s: %s\n"),
380 vpninfo->vpnc_script, reason, strerror(e));
387 static int link_proto(int unit_nr, const char *devname, uint64_t flags)
389 int ip_fd, mux_id, tun2_fd;
392 tun2_fd = open("/dev/tun", O_RDWR);
394 perror(_("Could not /dev/tun for plumbing"));
397 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
398 perror(_("Can't push IP"));
403 sprintf(ifr.lifr_name, "tun%d", unit_nr);
404 ifr.lifr_ppa = unit_nr;
405 ifr.lifr_flags = flags;
407 if (ioctl(tun2_fd, SIOCSLIFNAME, &ifr) < 0) {
408 perror(_("Can't set ifname"));
413 ip_fd = open(devname, O_RDWR);
415 fprintf(stderr, _("Can't open %s: %s"), devname,
420 if (ioctl(ip_fd, I_PUSH, "arp") < 0) {
421 perror(_("Can't push ARP"));
427 mux_id = ioctl(ip_fd, I_LINK, tun2_fd);
429 fprintf(stderr, _("Can't plumb %s for IPv%d: %s\n"),
430 ifr.lifr_name, (flags == IFF_IPV4) ? 4 : 6,
443 static int os_setup_tun(struct openconnect_info *vpninfo)
447 #ifdef IFF_TUN /* Linux */
451 tun_fd = open("/dev/net/tun", O_RDWR);
453 /* Android has /dev/tun instead of /dev/net/tun
454 Since other systems might have too, just try it
455 as a fallback instead of using ifdef __ANDROID__ */
457 tun_fd = open("/dev/tun", O_RDWR);
460 /* If the error on /dev/tun is ENOENT, that's boring.
461 Use the error we got on /dev/net/tun instead */
462 if (errno != -ENOENT)
465 vpn_progress(vpninfo, PRG_ERR,
466 _("Failed to open tun device: %s\n"),
470 memset(&ifr, 0, sizeof(ifr));
471 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
473 strncpy(ifr.ifr_name, vpninfo->ifname,
474 sizeof(ifr.ifr_name) - 1);
475 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
476 vpn_progress(vpninfo, PRG_ERR,
477 _("TUNSETIFF failed: %s\n"),
481 if (!vpninfo->ifname)
482 vpninfo->ifname = strdup(ifr.ifr_name);
483 #elif defined (__sun__)
484 static char tun_name[80];
487 tun_fd = open("/dev/tun", O_RDWR);
489 perror(_("open /dev/tun"));
493 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
495 perror(_("Failed to create new tun"));
500 if (ioctl(tun_fd, I_SRDOPT, RMSGD) < 0) {
501 perror(_("Failed to put tun file descriptor into message-discard mode"));
506 sprintf(tun_name, "tun%d", unit_nr);
507 vpninfo->ifname = strdup(tun_name);
509 vpninfo->ip_fd = link_proto(unit_nr, "/dev/udp", IFF_IPV4);
510 if (vpninfo->ip_fd < 0) {
515 if (vpninfo->vpn_addr6) {
516 vpninfo->ip6_fd = link_proto(unit_nr, "/dev/udp6", IFF_IPV6);
517 if (vpninfo->ip6_fd < 0) {
519 close(vpninfo->ip_fd);
524 vpninfo->ip6_fd = -1;
526 #else /* BSD et al have /dev/tun$x devices */
527 static char tun_name[80];
529 for (i = 0; i < 255; i++) {
530 sprintf(tun_name, "/dev/tun%d", i);
531 tun_fd = open(tun_name, O_RDWR);
536 perror(_("open tun"));
539 vpninfo->ifname = strdup(tun_name + 5);
542 if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
543 perror(_("TUNSIFHEAD"));
551 /* Set up a tuntap device. */
552 int setup_tun(struct openconnect_info *vpninfo)
556 set_script_env(vpninfo);
558 if (vpninfo->script_tun) {
562 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
563 perror(_("socketpair"));
573 setenv_int("VPNFD", fds[1]);
574 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
579 vpninfo->script_tun = child;
580 vpninfo->ifname = strdup(_("(script)"));
582 script_config_tun(vpninfo, "pre-init");
584 tun_fd = os_setup_tun(vpninfo);
588 if (vpninfo->vpnc_script) {
589 setenv("TUNDEV", vpninfo->ifname, 1);
590 script_config_tun(vpninfo, "connect");
591 /* We have to set the MTU for ourselves, because the script doesn't */
592 local_config_tun(vpninfo, 1);
594 local_config_tun(vpninfo, 0);
597 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
599 vpninfo->tun_fd = tun_fd;
601 if (vpninfo->select_nfds <= tun_fd)
602 vpninfo->select_nfds = tun_fd + 1;
604 FD_SET(tun_fd, &vpninfo->select_rfds);
606 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
611 static struct pkt *out_pkt;
613 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
618 #ifdef TUN_HAS_AF_PREFIX
619 if (!vpninfo->script_tun)
620 prefix_size = sizeof(int);
623 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
625 int len = vpninfo->mtu;
628 out_pkt = malloc(sizeof(struct pkt) + len);
630 vpn_progress(vpninfo, PRG_ERR, "Allocation failed\n");
635 len = read(vpninfo->tun_fd, out_pkt->data - prefix_size, len + prefix_size);
636 if (len <= prefix_size)
638 out_pkt->len = len - prefix_size;
640 queue_packet(&vpninfo->outgoing_queue, out_pkt);
644 vpninfo->outgoing_qlen++;
645 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
646 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
650 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
651 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
654 /* The kernel returns -ENOMEM when the queue is full, so theoretically
655 we could handle that and retry... but it doesn't let us poll() for
656 the no-longer-full situation, so let's not bother. */
657 while (vpninfo->incoming_queue) {
658 struct pkt *this = vpninfo->incoming_queue;
659 unsigned char *data = this->data;
662 #ifdef TUN_HAS_AF_PREFIX
663 if (!vpninfo->script_tun) {
664 struct ip *iph = (void *)data;
669 else if (iph->ip_v == 4)
672 static int complained = 0;
675 vpn_progress(vpninfo, PRG_ERR,
676 _("Unknown packet (len %d) received: %02x %02x %02x %02x...\n"),
677 len, data[0], data[1], data[2], data[3]);
684 *(int *)data = htonl(type);
687 vpninfo->incoming_queue = this->next;
689 if (write(vpninfo->tun_fd, data, len) < 0) {
690 /* Handle death of "script" socket */
691 if (vpninfo->script_tun && errno == ENOTCONN) {
692 vpninfo->quit_reason = "Client connection terminated";
695 vpn_progress(vpninfo, PRG_ERR,
696 _("Failed to write incoming packet: %s\n"),
701 /* Work is not done if we just got rid of packets off the queue */
705 void shutdown_tun(struct openconnect_info *vpninfo)
707 if (vpninfo->script_tun) {
708 kill(vpninfo->script_tun, SIGHUP);
710 script_config_tun(vpninfo, "disconnect");
712 close(vpninfo->ip_fd);
714 if (vpninfo->ip6_fd != -1) {
715 close(vpninfo->ip6_fd);
716 vpninfo->ip6_fd = -1;
721 close(vpninfo->tun_fd);
722 vpninfo->tun_fd = -1;