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>
46 #include "openconnect.h"
49 * If an if_tun.h include file was found anywhere (by the Makefile), it's
50 * included. Else, we end up assuming that we have BSD-style devices such
58 * The OS X tun/tap driver doesn't provide a header file; you're expected
59 * to define this for yourself.
62 #define TUNSIFHEAD _IOW('t', 96, int)
66 * OpenBSD always puts the protocol family prefix onto packets. Other
67 * systems let us enable that with the TUNSIFHEAD ioctl, and some of them
68 * (e.g. FreeBSD) _need_ it otherwise they'll interpret IPv6 packets as IPv4.
70 #if defined(__OpenBSD__) || defined(TUNSIFHEAD)
71 #define TUN_HAS_AF_PREFIX 1
75 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
78 vpninfo->progress(vpninfo, PRG_ERR,
79 "No vpnc-script configured. Need Solaris IP-setting code\n");
83 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
88 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
93 memset(&ifr, 0, sizeof(ifr));
94 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
97 struct sockaddr_in addr;
99 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
100 perror("SIOCGIFFLAGS");
102 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
103 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
104 perror("SIOCSIFFLAGS");
106 addr.sin_family = AF_INET;
107 addr.sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
108 memcpy(&ifr.ifr_addr, &addr, sizeof(addr));
109 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
110 perror("SIOCSIFADDR");
113 ifr.ifr_mtu = vpninfo->mtu;
114 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
115 perror("SIOCSIFMTU");
123 static int setenv_int(const char *opt, int value)
126 sprintf(buf, "%d", value);
127 return setenv(opt, buf, 1);
130 static int netmasklen(struct in_addr addr)
134 for (masklen = 0; masklen < 32; masklen++) {
135 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
141 static int process_split_xxclude(struct openconnect_info *vpninfo,
142 char *in_ex, char *route, int *v4_incs,
149 slash = strchr(route, '/');
152 vpninfo->progress(vpninfo, PRG_ERR,
153 "Discard bad split %sclude: \"%s\"\n",
160 if (strchr(route, ':')) {
161 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_ADDR", in_ex,
163 setenv(envname, route, 1);
165 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_MASKLEN", in_ex,
167 setenv(envname, slash+1, 1);
173 if (!inet_aton(route, &addr)) {
179 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *v4_incs);
180 setenv(envname, route, 1);
182 /* Put it back how we found it */
185 if (!inet_aton(slash+1, &addr))
188 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *v4_incs);
189 setenv(envname, slash+1, 1);
191 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *v4_incs);
192 setenv_int(envname, netmasklen(addr));
198 static int appendenv(const char *opt, const char *new)
201 char *old = getenv(opt);
205 snprintf(buf, 1023, "%s %s", old, new);
207 snprintf(buf, 1023, "%s", new);
209 return setenv(opt, buf, 1);
212 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
217 struct vpn_option *opt;
219 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
220 buflen += 2 + strlen(opt->option) + strlen(opt->value);
222 env_buf = malloc(buflen + 1);
228 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
229 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
230 "%s=%s\n", opt->option, opt->value);
232 setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
236 static void set_banner(struct openconnect_info *vpninfo)
241 if (!vpninfo->banner || !(banner = malloc(strlen(vpninfo->banner)))) {
242 unsetenv("CISCO_BANNER");
249 if (*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
250 *(q++) = unhex(p + 1);
256 setenv("CISCO_BANNER", banner, 1);
261 static void set_script_env(struct openconnect_info *vpninfo)
264 int ret = getnameinfo(vpninfo->peer_addr, vpninfo->peer_addrlen, host,
265 sizeof(host), NULL, 0, NI_NUMERICHOST);
267 setenv("VPNGATEWAY", host, 1);
269 setenv("reason", "connect", 1);
271 unsetenv("CISCO_SPLIT_INC");
272 unsetenv("CISCO_SPLIT_EXC");
274 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
276 if (vpninfo->vpn_addr) {
277 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
278 if (vpninfo->vpn_netmask) {
282 if (inet_aton(vpninfo->vpn_addr, &addr) &&
283 inet_aton(vpninfo->vpn_netmask, &mask)) {
286 addr.s_addr &= mask.s_addr;
287 netaddr = inet_ntoa(addr);
289 setenv("INTERNAL_IP4_NETADDR", netaddr, 1);
290 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
291 setenv_int("INTERNAL_IP4_NETMASKLEN", netmasklen(mask));
295 if (vpninfo->vpn_addr6) {
296 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
297 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
300 if (vpninfo->vpn_dns[0])
301 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
303 unsetenv("INTERNAL_IP4_DNS");
304 if (vpninfo->vpn_dns[1])
305 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
306 if (vpninfo->vpn_dns[2])
307 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
309 if (vpninfo->vpn_nbns[0])
310 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
312 unsetenv("INTERNAL_IP4_NBNS");
313 if (vpninfo->vpn_nbns[1])
314 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
315 if (vpninfo->vpn_nbns[2])
316 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
318 if (vpninfo->vpn_domain)
319 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
320 else unsetenv ("CISCO_DEF_DOMAIN");
322 if (vpninfo->vpn_proxy_pac)
323 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
325 if (vpninfo->split_includes) {
326 struct split_include *this = vpninfo->split_includes;
327 int nr_split_includes = 0;
328 int nr_v6_split_includes = 0;
331 process_split_xxclude(vpninfo, "IN", this->route,
333 &nr_v6_split_includes);
336 if (nr_split_includes)
337 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
338 if (nr_v6_split_includes)
339 setenv_int("CISCO_IPV6_SPLIT_INC", nr_v6_split_includes);
341 if (vpninfo->split_excludes) {
342 struct split_include *this = vpninfo->split_excludes;
343 int nr_split_excludes = 0;
344 int nr_v6_split_excludes = 0;
347 process_split_xxclude(vpninfo, "EX", this->route,
349 &nr_v6_split_excludes);
352 if (nr_split_excludes)
353 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
354 if (nr_v6_split_excludes)
355 setenv_int("CISCO_IPV6_SPLIT_EXC", nr_v6_split_excludes);
357 setenv_cstp_opts(vpninfo);
360 static int script_config_tun(struct openconnect_info *vpninfo)
362 if (system(vpninfo->vpnc_script)) {
364 vpninfo->progress(vpninfo, PRG_ERR,
365 "Failed to spawn script '%s': %s\n",
366 vpninfo->vpnc_script, strerror(e));
373 /* Set up a tuntap device. */
374 int setup_tun(struct openconnect_info *vpninfo)
378 set_script_env(vpninfo);
380 if (vpninfo->script_tun) {
384 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
385 perror("socketpair");
395 setenv_int("VPNFD", fds[1]);
396 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
401 vpninfo->script_tun = child;
402 vpninfo->ifname = "(script)";
404 #ifdef IFF_TUN /* Linux */
407 tun_fd = open("/dev/net/tun", O_RDWR);
409 vpninfo->progress(vpninfo, PRG_ERR,
410 "Failed to open tun device: %s\n",
414 memset(&ifr, 0, sizeof(ifr));
415 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
417 strncpy(ifr.ifr_name, vpninfo->ifname,
418 sizeof(ifr.ifr_name) - 1);
419 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
420 vpninfo->progress(vpninfo, PRG_ERR,
421 "TUNSETIFF failed: %s\n",
425 if (!vpninfo->ifname)
426 vpninfo->ifname = strdup(ifr.ifr_name);
427 #elif defined (__sun__)
428 static char tun_name[80];
429 int tun2_fd, ip_fd = open("/dev/ip", O_RDWR);
434 perror("open /dev/ip");
438 tun_fd = open("/dev/tun", O_RDWR);
440 perror("open /dev/tun");
445 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
447 perror("Failed to create new tun");
453 tun2_fd = open("/dev/tun", O_RDWR);
455 perror("open /dev/tun again");
460 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
461 perror("Can't push IP");
467 if (ioctl(tun2_fd, IF_UNITSEL, &unit_nr) < 0) {
468 perror("Can't select unit");
474 mux_id = ioctl(ip_fd, I_PLINK, tun2_fd);
476 perror("Can't link tun to IP");
484 sprintf(tun_name, "tun%d", unit_nr);
485 vpninfo->ifname = tun_name;
487 memset(&ifr, 0, sizeof(ifr));
488 strcpy(ifr.ifr_name, tun_name);
489 ifr.ifr_ip_muxid = mux_id;
491 if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
492 perror("Set mux id");
494 ioctl(ip_fd, I_PUNLINK, mux_id);
498 /* Solaris tunctl needs this in order to tear it down */
499 vpninfo->progress(vpninfo, PRG_DEBUG, "mux id is %d\n", mux_id);
500 vpninfo->tun_muxid = mux_id;
501 vpninfo->ip_fd = ip_fd;
503 #else /* BSD et al have /dev/tun$x devices */
504 static char tun_name[80];
506 for (i = 0; i < 255; i++) {
507 sprintf(tun_name, "/dev/tun%d", i);
508 tun_fd = open(tun_name, O_RDWR);
516 vpninfo->ifname = tun_name + 5;
519 if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
520 perror("TUNSIFHEAD");
525 if (vpninfo->vpnc_script) {
526 setenv("TUNDEV", vpninfo->ifname, 1);
527 script_config_tun(vpninfo);
528 /* We have to set the MTU for ourselves, because the script doesn't */
529 local_config_tun(vpninfo, 1);
531 local_config_tun(vpninfo, 0);
534 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
536 vpninfo->tun_fd = tun_fd;
538 if (vpninfo->select_nfds <= tun_fd)
539 vpninfo->select_nfds = tun_fd + 1;
541 FD_SET(tun_fd, &vpninfo->select_rfds);
543 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
548 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
550 unsigned char buf[2000];
554 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
555 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
556 unsigned char *pkt = buf;
557 #ifdef TUN_HAS_AF_PREFIX
561 if (queue_new_packet(&vpninfo->outgoing_queue, pkt,
566 vpninfo->outgoing_qlen++;
567 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
568 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
572 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
573 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
576 /* The kernel returns -ENOMEM when the queue is full, so theoretically
577 we could handle that and retry... but it doesn't let us poll() for
578 the no-longer-full situation, so let's not bother. */
579 while (vpninfo->incoming_queue) {
580 struct pkt *this = vpninfo->incoming_queue;
581 unsigned char *data = this->data;
584 #ifdef TUN_HAS_AF_PREFIX
585 struct ip *iph = (void *)data;
590 else if (iph->ip_v == 4)
593 static int complained = 0;
596 vpninfo->progress(vpninfo, PRG_ERR,
597 "Unknown packet (len %d) received: %02x %02x %02x %02x...\n",
598 len, data[0], data[1], data[2], data[3]);
605 *(int *)data = htonl(type);
607 vpninfo->incoming_queue = this->next;
609 if (write(vpninfo->tun_fd, data, len) < 0 &&
611 vpninfo->quit_reason = "Client connection terminated";
616 /* Work is not done if we just got rid of packets off the queue */
620 void shutdown_tun(struct openconnect_info *vpninfo)
622 if (vpninfo->script_tun) {
623 kill(vpninfo->script_tun, SIGHUP);
625 if (vpninfo->vpnc_script) {
626 setenv("reason", "disconnect", 1);
627 if (system(vpninfo->vpnc_script) == -1) {
628 vpninfo->progress(vpninfo, PRG_ERR,
629 "Failed to spawn script '%s': %s\n",
630 vpninfo->vpnc_script,
635 if (ioctl(vpninfo->ip_fd, I_PUNLINK, vpninfo->tun_muxid) < 0)
636 perror("ioctl(I_PUNLINK)");
638 close(vpninfo->ip_fd);
643 close(vpninfo->tun_fd);
644 vpninfo->tun_fd = -1;