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 vpninfo->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);
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 char *in_ex, char *route, int *v4_incs,
153 slash = strchr(route, '/');
156 vpninfo->progress(vpninfo, PRG_ERR,
157 "Discard bad split %sclude: \"%s\"\n",
164 if (strchr(route, ':')) {
165 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_ADDR", in_ex,
167 setenv(envname, route, 1);
169 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_MASKLEN", in_ex,
171 setenv(envname, slash+1, 1);
177 if (!inet_aton(route, &addr)) {
183 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *v4_incs);
184 setenv(envname, route, 1);
186 /* Put it back how we found it */
189 if (!inet_aton(slash+1, &addr))
192 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *v4_incs);
193 setenv(envname, slash+1, 1);
195 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *v4_incs);
196 setenv_int(envname, netmasklen(addr));
202 static int appendenv(const char *opt, const char *new)
205 char *old = getenv(opt);
209 snprintf(buf, 1023, "%s %s", old, new);
211 snprintf(buf, 1023, "%s", new);
213 return setenv(opt, buf, 1);
216 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
221 struct vpn_option *opt;
223 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
224 buflen += 2 + strlen(opt->option) + strlen(opt->value);
226 env_buf = malloc(buflen + 1);
232 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
233 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
234 "%s=%s\n", opt->option, opt->value);
236 setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
240 static void set_banner(struct openconnect_info *vpninfo)
245 if (!vpninfo->banner || !(banner = malloc(strlen(vpninfo->banner)))) {
246 unsetenv("CISCO_BANNER");
253 if (*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
254 *(q++) = unhex(p + 1);
260 setenv("CISCO_BANNER", banner, 1);
265 static void set_script_env(struct openconnect_info *vpninfo)
268 int ret = getnameinfo(vpninfo->peer_addr, vpninfo->peer_addrlen, host,
269 sizeof(host), NULL, 0, NI_NUMERICHOST);
271 setenv("VPNGATEWAY", host, 1);
273 setenv("reason", "connect", 1);
275 unsetenv("CISCO_SPLIT_INC");
276 unsetenv("CISCO_SPLIT_EXC");
278 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
280 if (vpninfo->vpn_addr) {
281 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
282 if (vpninfo->vpn_netmask) {
286 if (inet_aton(vpninfo->vpn_addr, &addr) &&
287 inet_aton(vpninfo->vpn_netmask, &mask)) {
290 addr.s_addr &= mask.s_addr;
291 netaddr = inet_ntoa(addr);
293 setenv("INTERNAL_IP4_NETADDR", netaddr, 1);
294 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
295 setenv_int("INTERNAL_IP4_NETMASKLEN", netmasklen(mask));
299 if (vpninfo->vpn_addr6) {
300 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
301 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
304 if (vpninfo->vpn_dns[0])
305 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
307 unsetenv("INTERNAL_IP4_DNS");
308 if (vpninfo->vpn_dns[1])
309 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
310 if (vpninfo->vpn_dns[2])
311 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
313 if (vpninfo->vpn_nbns[0])
314 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
316 unsetenv("INTERNAL_IP4_NBNS");
317 if (vpninfo->vpn_nbns[1])
318 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
319 if (vpninfo->vpn_nbns[2])
320 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
322 if (vpninfo->vpn_domain)
323 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
324 else unsetenv ("CISCO_DEF_DOMAIN");
326 if (vpninfo->vpn_proxy_pac)
327 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
329 if (vpninfo->split_includes) {
330 struct split_include *this = vpninfo->split_includes;
331 int nr_split_includes = 0;
332 int nr_v6_split_includes = 0;
335 process_split_xxclude(vpninfo, "IN", this->route,
337 &nr_v6_split_includes);
340 if (nr_split_includes)
341 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
342 if (nr_v6_split_includes)
343 setenv_int("CISCO_IPV6_SPLIT_INC", nr_v6_split_includes);
345 if (vpninfo->split_excludes) {
346 struct split_include *this = vpninfo->split_excludes;
347 int nr_split_excludes = 0;
348 int nr_v6_split_excludes = 0;
351 process_split_xxclude(vpninfo, "EX", this->route,
353 &nr_v6_split_excludes);
356 if (nr_split_excludes)
357 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
358 if (nr_v6_split_excludes)
359 setenv_int("CISCO_IPV6_SPLIT_EXC", nr_v6_split_excludes);
361 setenv_cstp_opts(vpninfo);
364 static int script_config_tun(struct openconnect_info *vpninfo)
366 if (system(vpninfo->vpnc_script)) {
368 vpninfo->progress(vpninfo, PRG_ERR,
369 "Failed to spawn script '%s': %s\n",
370 vpninfo->vpnc_script, strerror(e));
377 /* Set up a tuntap device. */
378 int setup_tun(struct openconnect_info *vpninfo)
382 set_script_env(vpninfo);
384 if (vpninfo->script_tun) {
388 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
389 perror("socketpair");
399 setenv_int("VPNFD", fds[1]);
400 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
405 vpninfo->script_tun = child;
406 vpninfo->ifname = "(script)";
408 #ifdef IFF_TUN /* Linux */
412 tun_fd = open("/dev/net/tun", O_RDWR);
414 /* Android has /dev/tun instead of /dev/net/tun
415 Since other systems might have too, just try it
416 as a fallback instead of using ifdef __ANDROID__ */
418 tun_fd = open("/dev/tun", O_RDWR);
421 /* If the error on /dev/tun is ENOENT, that's boring.
422 Use the error we got on /dev/net/tun instead */
423 if (errno != -ENOENT)
426 vpninfo->progress(vpninfo, PRG_ERR,
427 "Failed to open tun device: %s\n",
431 memset(&ifr, 0, sizeof(ifr));
432 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
434 strncpy(ifr.ifr_name, vpninfo->ifname,
435 sizeof(ifr.ifr_name) - 1);
436 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
437 vpninfo->progress(vpninfo, PRG_ERR,
438 "TUNSETIFF failed: %s\n",
442 if (!vpninfo->ifname)
443 vpninfo->ifname = strdup(ifr.ifr_name);
444 #elif defined (__sun__)
445 static char tun_name[80];
446 int tun2_fd, ip_fd = open("/dev/ip", O_RDWR);
451 perror("open /dev/ip");
455 tun_fd = open("/dev/tun", O_RDWR);
457 perror("open /dev/tun");
462 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
464 perror("Failed to create new tun");
470 tun2_fd = open("/dev/tun", O_RDWR);
472 perror("open /dev/tun again");
477 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
478 perror("Can't push IP");
484 if (ioctl(tun2_fd, IF_UNITSEL, &unit_nr) < 0) {
485 perror("Can't select unit");
491 mux_id = ioctl(ip_fd, I_PLINK, tun2_fd);
493 perror("Can't link tun to IP");
501 sprintf(tun_name, "tun%d", unit_nr);
502 vpninfo->ifname = tun_name;
504 memset(&ifr, 0, sizeof(ifr));
505 strcpy(ifr.ifr_name, tun_name);
506 ifr.ifr_ip_muxid = mux_id;
508 if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
509 perror("Set mux id");
511 ioctl(ip_fd, I_PUNLINK, mux_id);
515 /* Solaris tunctl needs this in order to tear it down */
516 vpninfo->progress(vpninfo, PRG_DEBUG, "mux id is %d\n", mux_id);
517 vpninfo->tun_muxid = mux_id;
518 vpninfo->ip_fd = ip_fd;
520 #else /* BSD et al have /dev/tun$x devices */
521 static char tun_name[80];
523 for (i = 0; i < 255; i++) {
524 sprintf(tun_name, "/dev/tun%d", i);
525 tun_fd = open(tun_name, O_RDWR);
533 vpninfo->ifname = tun_name + 5;
536 if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
537 perror("TUNSIFHEAD");
542 if (vpninfo->vpnc_script) {
543 setenv("TUNDEV", vpninfo->ifname, 1);
544 script_config_tun(vpninfo);
545 /* We have to set the MTU for ourselves, because the script doesn't */
546 local_config_tun(vpninfo, 1);
548 local_config_tun(vpninfo, 0);
551 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
553 vpninfo->tun_fd = tun_fd;
555 if (vpninfo->select_nfds <= tun_fd)
556 vpninfo->select_nfds = tun_fd + 1;
558 FD_SET(tun_fd, &vpninfo->select_rfds);
560 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
565 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
567 unsigned char buf[2000];
571 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
572 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
573 unsigned char *pkt = buf;
574 #ifdef TUN_HAS_AF_PREFIX
575 if (!vpninfo->script_tun) {
580 if (queue_new_packet(&vpninfo->outgoing_queue, pkt,
585 vpninfo->outgoing_qlen++;
586 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
587 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
591 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
592 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
595 /* The kernel returns -ENOMEM when the queue is full, so theoretically
596 we could handle that and retry... but it doesn't let us poll() for
597 the no-longer-full situation, so let's not bother. */
598 while (vpninfo->incoming_queue) {
599 struct pkt *this = vpninfo->incoming_queue;
600 unsigned char *data = this->data;
603 #ifdef TUN_HAS_AF_PREFIX
604 if (!vpninfo->script_tun) {
605 struct ip *iph = (void *)data;
610 else if (iph->ip_v == 4)
613 static int complained = 0;
616 vpninfo->progress(vpninfo, PRG_ERR,
617 "Unknown packet (len %d) received: %02x %02x %02x %02x...\n",
618 len, data[0], data[1], data[2], data[3]);
625 *(int *)data = htonl(type);
628 vpninfo->incoming_queue = this->next;
630 if (write(vpninfo->tun_fd, data, len) < 0 &&
632 vpninfo->quit_reason = "Client connection terminated";
637 /* Work is not done if we just got rid of packets off the queue */
641 void shutdown_tun(struct openconnect_info *vpninfo)
643 if (vpninfo->script_tun) {
644 kill(vpninfo->script_tun, SIGHUP);
646 if (vpninfo->vpnc_script) {
647 setenv("reason", "disconnect", 1);
648 if (system(vpninfo->vpnc_script) == -1) {
649 vpninfo->progress(vpninfo, PRG_ERR,
650 "Failed to spawn script '%s': %s\n",
651 vpninfo->vpnc_script,
656 if (ioctl(vpninfo->ip_fd, I_PUNLINK, vpninfo->tun_muxid) < 0)
657 perror("ioctl(I_PUNLINK)");
659 close(vpninfo->ip_fd);
664 close(vpninfo->tun_fd);
665 vpninfo->tun_fd = -1;