2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008 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>
41 #include <net/if_tun.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_script_env(struct openconnect_info *vpninfo)
239 int ret = getnameinfo(vpninfo->peer_addr, vpninfo->peer_addrlen, host,
240 sizeof(host), NULL, 0, NI_NUMERICHOST);
242 setenv("VPNGATEWAY", host, 1);
243 setenv("TUNDEV", vpninfo->ifname, 1);
244 setenv("reason", "connect", 1);
245 unsetenv("CISCO_BANNER");
246 unsetenv("CISCO_SPLIT_INC");
247 unsetenv("CISCO_SPLIT_EXC");
249 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
251 if (vpninfo->vpn_addr) {
252 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
253 if (vpninfo->vpn_netmask) {
257 if (inet_aton(vpninfo->vpn_addr, &addr) &&
258 inet_aton(vpninfo->vpn_netmask, &mask)) {
261 addr.s_addr &= mask.s_addr;
262 netaddr = inet_ntoa(addr);
264 setenv("INTERNAL_IP4_NETADDR", netaddr, 1);
265 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
266 setenv_int("INTERNAL_IP4_NETMASKLEN", netmasklen(mask));
270 if (vpninfo->vpn_addr6) {
271 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
272 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
275 if (vpninfo->vpn_dns[0])
276 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
278 unsetenv("INTERNAL_IP4_DNS");
279 if (vpninfo->vpn_dns[1])
280 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
281 if (vpninfo->vpn_dns[2])
282 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
284 if (vpninfo->vpn_nbns[0])
285 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
287 unsetenv("INTERNAL_IP4_NBNS");
288 if (vpninfo->vpn_nbns[1])
289 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
290 if (vpninfo->vpn_nbns[2])
291 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
293 if (vpninfo->vpn_domain)
294 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
295 else unsetenv ("CISCO_DEF_DOMAIN");
297 if (vpninfo->vpn_proxy_pac)
298 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
300 if (vpninfo->split_includes) {
301 struct split_include *this = vpninfo->split_includes;
302 int nr_split_includes = 0;
303 int nr_v6_split_includes = 0;
306 process_split_xxclude(vpninfo, "IN", this->route,
308 &nr_v6_split_includes);
311 if (nr_split_includes)
312 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
313 if (nr_v6_split_includes)
314 setenv_int("CISCO_IPV6_SPLIT_INC", nr_v6_split_includes);
316 if (vpninfo->split_excludes) {
317 struct split_include *this = vpninfo->split_excludes;
318 int nr_split_excludes = 0;
319 int nr_v6_split_excludes = 0;
322 process_split_xxclude(vpninfo, "EX", this->route,
324 &nr_v6_split_excludes);
327 if (nr_split_excludes)
328 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
329 if (nr_v6_split_excludes)
330 setenv_int("CISCO_IPV6_SPLIT_EXC", nr_v6_split_excludes);
332 setenv_cstp_opts(vpninfo);
335 static int script_config_tun(struct openconnect_info *vpninfo)
337 set_script_env(vpninfo);
339 system(vpninfo->vpnc_script);
344 /* Set up a tuntap device. */
345 int setup_tun(struct openconnect_info *vpninfo)
349 if (vpninfo->script_tun) {
353 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
354 perror("socketpair");
364 setenv_int("VPNFD", fds[1]);
365 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
370 vpninfo->script_tun = child;
371 vpninfo->ifname = "(script)";
373 #ifdef IFF_TUN /* Linux */
376 tun_fd = open("/dev/net/tun", O_RDWR);
378 vpninfo->progress(vpninfo, PRG_ERR,
379 "Failed to open tun device: %s\n",
383 memset(&ifr, 0, sizeof(ifr));
384 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
386 strncpy(ifr.ifr_name, vpninfo->ifname,
387 sizeof(ifr.ifr_name) - 1);
388 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
389 vpninfo->progress(vpninfo, PRG_ERR,
390 "TUNSETIFF failed: %s\n",
394 if (!vpninfo->ifname)
395 vpninfo->ifname = strdup(ifr.ifr_name);
396 #elif defined (__sun__)
397 static char tun_name[80];
398 int tun2_fd, ip_fd = open("/dev/ip", O_RDWR);
403 perror("open /dev/ip");
407 tun_fd = open("/dev/tun", O_RDWR);
409 perror("open /dev/tun");
414 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
416 perror("Failed to create new tun");
422 tun2_fd = open("/dev/tun", O_RDWR);
424 perror("open /dev/tun again");
429 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
430 perror("Can't push IP");
436 if (ioctl(tun2_fd, IF_UNITSEL, &unit_nr) < 0) {
437 perror("Can't select unit");
443 mux_id = ioctl(ip_fd, I_PLINK, tun2_fd);
445 perror("Can't link tun to IP");
453 sprintf(tun_name, "tun%d", unit_nr);
454 vpninfo->ifname = tun_name;
456 memset(&ifr, 0, sizeof(ifr));
457 strcpy(ifr.ifr_name, tun_name);
458 ifr.ifr_ip_muxid = mux_id;
460 if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
461 perror("Set mux id");
463 ioctl(ip_fd, I_PUNLINK, mux_id);
467 /* Solaris tunctl needs this in order to tear it down */
468 vpninfo->progress(vpninfo, PRG_DEBUG, "mux id is %d\n", mux_id);
469 vpninfo->tun_muxid = mux_id;
470 vpninfo->ip_fd = ip_fd;
472 #else /* BSD et al have /dev/tun$x devices */
473 static char tun_name[80];
475 for (i = 0; i < 255; i++) {
476 sprintf(tun_name, "/dev/tun%d", i);
477 tun_fd = open(tun_name, O_RDWR);
485 vpninfo->ifname = tun_name + 5;
488 if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
489 perror("TUNSIFHEAD");
494 if (vpninfo->vpnc_script) {
495 script_config_tun(vpninfo);
496 /* We have to set the MTU for ourselves, because the script doesn't */
497 local_config_tun(vpninfo, 1);
499 local_config_tun(vpninfo, 0);
502 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
504 vpninfo->tun_fd = tun_fd;
506 if (vpninfo->select_nfds <= tun_fd)
507 vpninfo->select_nfds = tun_fd + 1;
509 FD_SET(tun_fd, &vpninfo->select_rfds);
511 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
516 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
518 unsigned char buf[2000];
522 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
523 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
524 unsigned char *pkt = buf;
525 #ifdef TUN_HAS_AF_PREFIX
529 if (queue_new_packet(&vpninfo->outgoing_queue, pkt,
534 vpninfo->outgoing_qlen++;
535 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
536 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
540 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
541 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
544 /* The kernel returns -ENOMEM when the queue is full, so theoretically
545 we could handle that and retry... but it doesn't let us poll() for
546 the no-longer-full situation, so let's not bother. */
547 while (vpninfo->incoming_queue) {
548 struct pkt *this = vpninfo->incoming_queue;
549 unsigned char *data = this->data;
552 #ifdef TUN_HAS_AF_PREFIX
553 struct ip *iph = (void *)data;
558 else if (iph->ip_v == 4)
561 static int complained = 0;
564 vpninfo->progress(vpninfo, PRG_ERR,
565 "Unknown packet (len %d) received: %02x %02x %02x %02x...\n",
566 len, data[0], data[1], data[2], data[3]);
573 *(int *)data = htonl(type);
575 vpninfo->incoming_queue = this->next;
577 if (write(vpninfo->tun_fd, data, len) < 0 &&
579 vpninfo->quit_reason = "Client connection terminated";
584 /* Work is not done if we just got rid of packets off the queue */
588 void shutdown_tun(struct openconnect_info *vpninfo)
590 if (vpninfo->script_tun) {
591 kill(vpninfo->script_tun, SIGHUP);
593 if (vpninfo->vpnc_script) {
594 setenv("TUNDEV", vpninfo->ifname, 1);
595 setenv("reason", "disconnect", 1);
596 system(vpninfo->vpnc_script);
599 if (ioctl(vpninfo->ip_fd, I_PUNLINK, vpninfo->tun_muxid) < 0)
600 perror("ioctl(I_PUNLINK)");
602 close(vpninfo->ip_fd);
607 close(vpninfo->tun_fd);
608 vpninfo->tun_fd = -1;