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>
42 #include <sys/sockio.h>
45 #include "openconnect.h"
48 * If an if_tun.h include file was found anywhere (by the Makefile), it's
49 * included. Else, we end up assuming that we have BSD-style devices such
57 * The OS X tun/tap driver doesn't provide a header file; you're expected
58 * to define this for yourself.
61 #define TUNSIFHEAD _IOW('t', 96, int)
65 * OpenBSD always puts the protocol family prefix onto packets. Other
66 * systems let us enable that with the TUNSIFHEAD ioctl, and some of them
67 * (e.g. FreeBSD) _need_ it otherwise they'll interpret IPv6 packets as IPv4.
69 #if defined(__OpenBSD__) || defined(TUNSIFHEAD)
70 #define TUN_HAS_AF_PREFIX 1
74 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
77 vpninfo->progress(vpninfo, PRG_ERR,
78 "No vpnc-script configured. Need Solaris IP-setting code\n");
82 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
87 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
92 memset(&ifr, 0, sizeof(ifr));
93 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
96 struct sockaddr_in addr;
98 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
99 perror("SIOCGIFFLAGS");
101 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
102 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
103 perror("SIOCSIFFLAGS");
105 addr.sin_family = AF_INET;
106 addr.sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
107 memcpy(&ifr.ifr_addr, &addr, sizeof(addr));
108 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
109 perror("SIOCSIFADDR");
112 ifr.ifr_mtu = vpninfo->mtu;
113 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
114 perror("SIOCSIFMTU");
122 static int setenv_int(const char *opt, int value)
125 sprintf(buf, "%d", value);
126 return setenv(opt, buf, 1);
129 static int netmasklen(struct in_addr addr)
133 for (masklen = 0; masklen < 32; masklen++) {
134 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
140 static int process_split_xxclude(struct openconnect_info *vpninfo,
141 char *in_ex, char *route, int *v4_incs,
148 slash = strchr(route, '/');
151 vpninfo->progress(vpninfo, PRG_ERR,
152 "Discard bad split %sclude: \"%s\"\n",
159 if (strchr(route, ':')) {
160 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_ADDR", in_ex,
162 setenv(envname, route, 1);
164 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_MASKLEN", in_ex,
166 setenv(envname, slash+1, 1);
172 if (!inet_aton(route, &addr)) {
178 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *v4_incs);
179 setenv(envname, route, 1);
181 /* Put it back how we found it */
184 if (!inet_aton(slash+1, &addr))
187 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *v4_incs);
188 setenv(envname, slash+1, 1);
190 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *v4_incs);
191 setenv_int(envname, netmasklen(addr));
197 static int appendenv(const char *opt, const char *new)
200 char *old = getenv(opt);
204 snprintf(buf, 1023, "%s %s", old, new);
206 snprintf(buf, 1023, "%s", new);
208 return setenv(opt, buf, 1);
211 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
216 struct vpn_option *opt;
218 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
219 buflen += 2 + strlen(opt->option) + strlen(opt->value);
221 env_buf = malloc(buflen + 1);
227 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
228 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
229 "%s=%s\n", opt->option, opt->value);
231 setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
235 static void set_script_env(struct openconnect_info *vpninfo)
238 int ret = getnameinfo(vpninfo->peer_addr, vpninfo->peer_addrlen, host,
239 sizeof(host), NULL, 0, NI_NUMERICHOST);
241 setenv("VPNGATEWAY", host, 1);
242 setenv("TUNDEV", vpninfo->ifname, 1);
243 setenv("reason", "connect", 1);
244 unsetenv("CISCO_BANNER");
245 unsetenv("CISCO_SPLIT_INC");
246 unsetenv("CISCO_SPLIT_EXC");
248 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
250 if (vpninfo->vpn_addr) {
251 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
252 if (vpninfo->vpn_netmask) {
256 if (inet_aton(vpninfo->vpn_addr, &addr) &&
257 inet_aton(vpninfo->vpn_netmask, &mask)) {
260 addr.s_addr &= mask.s_addr;
261 netaddr = inet_ntoa(addr);
263 setenv("INTERNAL_IP4_NETADDR", netaddr, 1);
264 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
265 setenv_int("INTERNAL_IP4_NETMASKLEN", netmasklen(mask));
269 if (vpninfo->vpn_addr6) {
270 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
271 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
274 if (vpninfo->vpn_dns[0])
275 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
277 unsetenv("INTERNAL_IP4_DNS");
278 if (vpninfo->vpn_dns[1])
279 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
280 if (vpninfo->vpn_dns[2])
281 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
283 if (vpninfo->vpn_nbns[0])
284 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
286 unsetenv("INTERNAL_IP4_NBNS");
287 if (vpninfo->vpn_nbns[1])
288 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
289 if (vpninfo->vpn_nbns[2])
290 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
292 if (vpninfo->vpn_domain)
293 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
294 else unsetenv ("CISCO_DEF_DOMAIN");
296 if (vpninfo->vpn_proxy_pac)
297 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
299 if (vpninfo->split_includes) {
300 struct split_include *this = vpninfo->split_includes;
301 int nr_split_includes = 0;
302 int nr_v6_split_includes = 0;
305 process_split_xxclude(vpninfo, "IN", this->route,
307 &nr_v6_split_includes);
310 if (nr_split_includes)
311 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
312 if (nr_v6_split_includes)
313 setenv_int("CISCO_IPV6_SPLIT_INC", nr_v6_split_includes);
315 if (vpninfo->split_excludes) {
316 struct split_include *this = vpninfo->split_excludes;
317 int nr_split_excludes = 0;
318 int nr_v6_split_excludes = 0;
321 process_split_xxclude(vpninfo, "EX", this->route,
323 &nr_v6_split_excludes);
326 if (nr_split_excludes)
327 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
328 if (nr_v6_split_excludes)
329 setenv_int("CISCO_IPV6_SPLIT_EXC", nr_v6_split_excludes);
331 setenv_cstp_opts(vpninfo);
334 static int script_config_tun(struct openconnect_info *vpninfo)
336 set_script_env(vpninfo);
338 if (system(vpninfo->vpnc_script)) {
340 vpninfo->progress(vpninfo, PRG_ERR,
341 "Failed to spawn script '%s': %s\n",
342 vpninfo->vpnc_script, strerror(e));
349 /* Set up a tuntap device. */
350 int setup_tun(struct openconnect_info *vpninfo)
354 if (vpninfo->script_tun) {
358 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
359 perror("socketpair");
369 setenv_int("VPNFD", fds[1]);
370 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
375 vpninfo->script_tun = child;
376 vpninfo->ifname = "(script)";
378 #ifdef IFF_TUN /* Linux */
381 tun_fd = open("/dev/net/tun", O_RDWR);
383 vpninfo->progress(vpninfo, PRG_ERR,
384 "Failed to open tun device: %s\n",
388 memset(&ifr, 0, sizeof(ifr));
389 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
391 strncpy(ifr.ifr_name, vpninfo->ifname,
392 sizeof(ifr.ifr_name) - 1);
393 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
394 vpninfo->progress(vpninfo, PRG_ERR,
395 "TUNSETIFF failed: %s\n",
399 if (!vpninfo->ifname)
400 vpninfo->ifname = strdup(ifr.ifr_name);
401 #elif defined (__sun__)
402 static char tun_name[80];
403 int tun2_fd, ip_fd = open("/dev/ip", O_RDWR);
408 perror("open /dev/ip");
412 tun_fd = open("/dev/tun", O_RDWR);
414 perror("open /dev/tun");
419 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
421 perror("Failed to create new tun");
427 tun2_fd = open("/dev/tun", O_RDWR);
429 perror("open /dev/tun again");
434 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
435 perror("Can't push IP");
441 if (ioctl(tun2_fd, IF_UNITSEL, &unit_nr) < 0) {
442 perror("Can't select unit");
448 mux_id = ioctl(ip_fd, I_PLINK, tun2_fd);
450 perror("Can't link tun to IP");
458 sprintf(tun_name, "tun%d", unit_nr);
459 vpninfo->ifname = tun_name;
461 memset(&ifr, 0, sizeof(ifr));
462 strcpy(ifr.ifr_name, tun_name);
463 ifr.ifr_ip_muxid = mux_id;
465 if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
466 perror("Set mux id");
468 ioctl(ip_fd, I_PUNLINK, mux_id);
472 /* Solaris tunctl needs this in order to tear it down */
473 vpninfo->progress(vpninfo, PRG_DEBUG, "mux id is %d\n", mux_id);
474 vpninfo->tun_muxid = mux_id;
475 vpninfo->ip_fd = ip_fd;
477 #else /* BSD et al have /dev/tun$x devices */
478 static char tun_name[80];
480 for (i = 0; i < 255; i++) {
481 sprintf(tun_name, "/dev/tun%d", i);
482 tun_fd = open(tun_name, O_RDWR);
490 vpninfo->ifname = tun_name + 5;
493 if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
494 perror("TUNSIFHEAD");
499 if (vpninfo->vpnc_script) {
500 script_config_tun(vpninfo);
501 /* We have to set the MTU for ourselves, because the script doesn't */
502 local_config_tun(vpninfo, 1);
504 local_config_tun(vpninfo, 0);
507 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
509 vpninfo->tun_fd = tun_fd;
511 if (vpninfo->select_nfds <= tun_fd)
512 vpninfo->select_nfds = tun_fd + 1;
514 FD_SET(tun_fd, &vpninfo->select_rfds);
516 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
521 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
523 unsigned char buf[2000];
527 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
528 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
529 unsigned char *pkt = buf;
530 #ifdef TUN_HAS_AF_PREFIX
534 if (queue_new_packet(&vpninfo->outgoing_queue, pkt,
539 vpninfo->outgoing_qlen++;
540 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
541 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
545 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
546 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
549 /* The kernel returns -ENOMEM when the queue is full, so theoretically
550 we could handle that and retry... but it doesn't let us poll() for
551 the no-longer-full situation, so let's not bother. */
552 while (vpninfo->incoming_queue) {
553 struct pkt *this = vpninfo->incoming_queue;
554 unsigned char *data = this->data;
557 #ifdef TUN_HAS_AF_PREFIX
558 struct ip *iph = (void *)data;
563 else if (iph->ip_v == 4)
566 static int complained = 0;
569 vpninfo->progress(vpninfo, PRG_ERR,
570 "Unknown packet (len %d) received: %02x %02x %02x %02x...\n",
571 len, data[0], data[1], data[2], data[3]);
578 *(int *)data = htonl(type);
580 vpninfo->incoming_queue = this->next;
582 if (write(vpninfo->tun_fd, data, len) < 0 &&
584 vpninfo->quit_reason = "Client connection terminated";
589 /* Work is not done if we just got rid of packets off the queue */
593 void shutdown_tun(struct openconnect_info *vpninfo)
595 if (vpninfo->script_tun) {
596 kill(vpninfo->script_tun, SIGHUP);
598 if (vpninfo->vpnc_script) {
599 setenv("TUNDEV", vpninfo->ifname, 1);
600 setenv("reason", "disconnect", 1);
601 if (system(vpninfo->vpnc_script) == -1) {
602 vpninfo->progress(vpninfo, PRG_ERR,
603 "Failed to spawn script '%s': %s\n",
604 vpninfo->vpnc_script,
609 if (ioctl(vpninfo->ip_fd, I_PUNLINK, vpninfo->tun_muxid) < 0)
610 perror("ioctl(I_PUNLINK)");
612 close(vpninfo->ip_fd);
617 close(vpninfo->tun_fd);
618 vpninfo->tun_fd = -1;