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
76 #define AF_PREFIX_SIZE (sizeof(int))
78 #define AF_PREFIX_SIZE (0)
82 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
85 vpn_progress(vpninfo, PRG_ERR,
86 _("No vpnc-script configured. Need Solaris IP-setting code\n"));
90 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
95 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
97 perror(_("open net"));
100 memset(&ifr, 0, sizeof(ifr));
101 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
104 struct sockaddr_in addr;
106 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
107 perror(_("SIOCGIFFLAGS"));
109 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
110 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
111 perror(_("SIOCSIFFLAGS"));
113 addr.sin_family = AF_INET;
114 addr.sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
115 memcpy(&ifr.ifr_addr, &addr, sizeof(addr));
116 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
117 perror(_("SIOCSIFADDR"));
120 ifr.ifr_mtu = vpninfo->mtu;
121 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
122 perror(_("SIOCSIFMTU"));
130 static int setenv_int(const char *opt, int value)
133 sprintf(buf, "%d", value);
134 return setenv(opt, buf, 1);
137 static int netmasklen(struct in_addr addr)
141 for (masklen = 0; masklen < 32; masklen++) {
142 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
148 static int process_split_xxclude(struct openconnect_info *vpninfo,
149 int include, const char *route, int *v4_incs,
153 const char *in_ex = include?"IN":"EX";
157 slash = strchr(route, '/');
161 vpn_progress(vpninfo, PRG_ERR,
162 _("Discard bad split include: \"%s\"\n"),
165 vpn_progress(vpninfo, PRG_ERR,
166 _("Discard bad split exclude: \"%s\"\n"),
173 if (strchr(route, ':')) {
174 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_ADDR", in_ex,
176 setenv(envname, route, 1);
178 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_MASKLEN", in_ex,
180 setenv(envname, slash+1, 1);
186 if (!inet_aton(route, &addr)) {
192 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *v4_incs);
193 setenv(envname, route, 1);
195 /* Put it back how we found it */
198 if (!inet_aton(slash+1, &addr))
201 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *v4_incs);
202 setenv(envname, slash+1, 1);
204 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *v4_incs);
205 setenv_int(envname, netmasklen(addr));
211 static int appendenv(const char *opt, const char *new)
214 char *old = getenv(opt);
218 snprintf(buf, 1023, "%s %s", old, new);
220 snprintf(buf, 1023, "%s", new);
222 return setenv(opt, buf, 1);
225 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
230 struct vpn_option *opt;
232 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
233 buflen += 2 + strlen(opt->option) + strlen(opt->value);
235 env_buf = malloc(buflen + 1);
241 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
242 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
243 "%s=%s\n", opt->option, opt->value);
245 setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
249 static void set_banner(struct openconnect_info *vpninfo)
254 if (!vpninfo->banner || !(banner = malloc(strlen(vpninfo->banner)))) {
255 unsetenv("CISCO_BANNER");
262 if (*p == '%' && isxdigit((int)(unsigned char)p[1]) &&
263 isxdigit((int)(unsigned char)p[2])) {
264 *(q++) = unhex(p + 1);
270 setenv("CISCO_BANNER", banner, 1);
275 static void set_script_env(struct openconnect_info *vpninfo)
278 int ret = getnameinfo(vpninfo->peer_addr, vpninfo->peer_addrlen, host,
279 sizeof(host), NULL, 0, NI_NUMERICHOST);
281 setenv("VPNGATEWAY", host, 1);
283 setenv("reason", "connect", 1);
285 unsetenv("CISCO_SPLIT_INC");
286 unsetenv("CISCO_SPLIT_EXC");
288 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
290 if (vpninfo->vpn_addr) {
291 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
292 if (vpninfo->vpn_netmask) {
296 if (inet_aton(vpninfo->vpn_addr, &addr) &&
297 inet_aton(vpninfo->vpn_netmask, &mask)) {
300 addr.s_addr &= mask.s_addr;
301 netaddr = inet_ntoa(addr);
303 setenv("INTERNAL_IP4_NETADDR", netaddr, 1);
304 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
305 setenv_int("INTERNAL_IP4_NETMASKLEN", netmasklen(mask));
309 if (vpninfo->vpn_addr6) {
310 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
311 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
314 if (vpninfo->vpn_dns[0])
315 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
317 unsetenv("INTERNAL_IP4_DNS");
318 if (vpninfo->vpn_dns[1])
319 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
320 if (vpninfo->vpn_dns[2])
321 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
323 if (vpninfo->vpn_nbns[0])
324 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
326 unsetenv("INTERNAL_IP4_NBNS");
327 if (vpninfo->vpn_nbns[1])
328 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
329 if (vpninfo->vpn_nbns[2])
330 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
332 if (vpninfo->vpn_domain)
333 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
334 else unsetenv ("CISCO_DEF_DOMAIN");
336 if (vpninfo->vpn_proxy_pac)
337 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
339 if (vpninfo->split_includes) {
340 struct split_include *this = vpninfo->split_includes;
341 int nr_split_includes = 0;
342 int nr_v6_split_includes = 0;
345 process_split_xxclude(vpninfo, 1, this->route,
347 &nr_v6_split_includes);
350 if (nr_split_includes)
351 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
352 if (nr_v6_split_includes)
353 setenv_int("CISCO_IPV6_SPLIT_INC", nr_v6_split_includes);
355 if (vpninfo->split_excludes) {
356 struct split_include *this = vpninfo->split_excludes;
357 int nr_split_excludes = 0;
358 int nr_v6_split_excludes = 0;
361 process_split_xxclude(vpninfo, 0, this->route,
363 &nr_v6_split_excludes);
366 if (nr_split_excludes)
367 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
368 if (nr_v6_split_excludes)
369 setenv_int("CISCO_IPV6_SPLIT_EXC", nr_v6_split_excludes);
371 setenv_cstp_opts(vpninfo);
374 static int script_config_tun(struct openconnect_info *vpninfo)
376 if (system(vpninfo->vpnc_script)) {
378 vpn_progress(vpninfo, PRG_ERR,
379 _("Failed to spawn script '%s': %s\n"),
380 vpninfo->vpnc_script, strerror(e));
386 void script_reconnect (struct openconnect_info *vpninfo)
388 setenv("reason", "reconnect", 1);
389 script_config_tun(vpninfo);
392 /* Set up a tuntap device. */
393 int setup_tun(struct openconnect_info *vpninfo)
397 set_script_env(vpninfo);
399 if (vpninfo->script_tun) {
403 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
404 perror(_("socketpair"));
414 setenv_int("VPNFD", fds[1]);
415 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
420 vpninfo->script_tun = child;
421 vpninfo->ifname = strdup(_("(script)"));
423 #ifdef IFF_TUN /* Linux */
427 tun_fd = open("/dev/net/tun", O_RDWR);
429 /* Android has /dev/tun instead of /dev/net/tun
430 Since other systems might have too, just try it
431 as a fallback instead of using ifdef __ANDROID__ */
433 tun_fd = open("/dev/tun", O_RDWR);
436 /* If the error on /dev/tun is ENOENT, that's boring.
437 Use the error we got on /dev/net/tun instead */
438 if (errno != -ENOENT)
441 vpn_progress(vpninfo, PRG_ERR,
442 _("Failed to open tun device: %s\n"),
446 memset(&ifr, 0, sizeof(ifr));
447 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
449 strncpy(ifr.ifr_name, vpninfo->ifname,
450 sizeof(ifr.ifr_name) - 1);
451 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
452 vpn_progress(vpninfo, PRG_ERR,
453 _("TUNSETIFF failed: %s\n"),
457 if (!vpninfo->ifname)
458 vpninfo->ifname = strdup(ifr.ifr_name);
459 #elif defined (__sun__)
460 static char tun_name[80];
461 int tun2_fd, ip_fd = open("/dev/ip", O_RDWR);
466 perror(_("open /dev/ip"));
470 tun_fd = open("/dev/tun", O_RDWR);
472 perror(_("open /dev/tun"));
477 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
479 perror(_("Failed to create new tun"));
485 tun2_fd = open("/dev/tun", O_RDWR);
487 perror(_("open /dev/tun again"));
492 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
493 perror(_("Can't push IP"));
499 if (ioctl(tun2_fd, IF_UNITSEL, &unit_nr) < 0) {
500 perror(_("Can't select unit"));
506 mux_id = ioctl(ip_fd, I_LINK, tun2_fd);
508 perror(_("Can't link tun to IP"));
516 sprintf(tun_name, "tun%d", unit_nr);
517 vpninfo->ifname = strdup(tun_name);
519 memset(&ifr, 0, sizeof(ifr));
520 strcpy(ifr.ifr_name, tun_name);
521 ifr.ifr_ip_muxid = mux_id;
523 if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
524 perror(_("Set mux id"));
529 vpninfo->ip_fd = ip_fd;
531 #else /* BSD et al have /dev/tun$x devices */
532 static char tun_name[80];
534 for (i = 0; i < 255; i++) {
535 sprintf(tun_name, "/dev/tun%d", i);
536 tun_fd = open(tun_name, O_RDWR);
541 perror(_("open tun"));
544 vpninfo->ifname = strdup(tun_name + 5);
547 if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
548 perror(_("TUNSIFHEAD"));
553 if (vpninfo->vpnc_script) {
554 setenv("TUNDEV", vpninfo->ifname, 1);
555 script_config_tun(vpninfo);
556 /* We have to set the MTU for ourselves, because the script doesn't */
557 local_config_tun(vpninfo, 1);
559 local_config_tun(vpninfo, 0);
562 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
564 vpninfo->tun_fd = tun_fd;
566 if (vpninfo->select_nfds <= tun_fd)
567 vpninfo->select_nfds = tun_fd + 1;
569 FD_SET(tun_fd, &vpninfo->select_rfds);
571 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
576 static struct pkt *out_pkt;
578 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
583 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
585 int len = vpninfo->mtu;
588 out_pkt = malloc(sizeof(struct pkt) + len);
590 vpn_progress(vpninfo, PRG_ERR, "Allocation failed\n");
596 /* On Solaris, if we're actually using tuntap and *not* just
597 passing packets through a UNIX socket to a child process,
598 we have to use getmsg() to ensure that we only get *one*
600 if (!vpninfo->script_tun) {
605 strb.buf = out_pkt->data;
608 ret = getmsg(vpninfo->tun_fd, NULL, &strb, &flags);
612 if (!(ret & MOREDATA))
614 out_pkt->len = strb.len;
616 #endif /* __sun__ ... */
618 len = read(vpninfo->tun_fd, out_pkt->data - AF_PREFIX_SIZE, len + AF_PREFIX_SIZE);
622 out_pkt->len = len - AF_PREFIX_SIZE;
625 queue_packet(&vpninfo->outgoing_queue, out_pkt);
629 vpninfo->outgoing_qlen++;
630 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
631 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
635 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
636 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
639 /* The kernel returns -ENOMEM when the queue is full, so theoretically
640 we could handle that and retry... but it doesn't let us poll() for
641 the no-longer-full situation, so let's not bother. */
642 while (vpninfo->incoming_queue) {
643 struct pkt *this = vpninfo->incoming_queue;
644 unsigned char *data = this->data;
647 #ifdef TUN_HAS_AF_PREFIX
648 if (!vpninfo->script_tun) {
649 struct ip *iph = (void *)data;
654 else if (iph->ip_v == 4)
657 static int complained = 0;
660 vpn_progress(vpninfo, PRG_ERR,
661 _("Unknown packet (len %d) received: %02x %02x %02x %02x...\n"),
662 len, data[0], data[1], data[2], data[3]);
669 *(int *)data = htonl(type);
672 vpninfo->incoming_queue = this->next;
674 if (write(vpninfo->tun_fd, data, len) < 0) {
675 /* Handle death of "script" socket */
676 if (vpninfo->script_tun && errno == ENOTCONN) {
677 vpninfo->quit_reason = "Client connection terminated";
680 vpn_progress(vpninfo, PRG_ERR,
681 _("Failed to write incoming packet: %s\n"),
686 /* Work is not done if we just got rid of packets off the queue */
690 void shutdown_tun(struct openconnect_info *vpninfo)
692 if (vpninfo->script_tun) {
693 kill(vpninfo->script_tun, SIGHUP);
695 if (vpninfo->vpnc_script) {
696 setenv("reason", "disconnect", 1);
697 if (system(vpninfo->vpnc_script) == -1) {
698 vpn_progress(vpninfo, PRG_ERR,
699 _("Failed to spawn script '%s': %s\n"),
700 vpninfo->vpnc_script,
705 close(vpninfo->ip_fd);
710 close(vpninfo->tun_fd);
711 vpninfo->tun_fd = -1;