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>
32 #include <linux/if_tun.h>
33 #elif defined(__sun__)
34 #include <net/if_tun.h>
36 #include <sys/sockio.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/in.h>
42 #include <netinet/ip.h>
44 #include <arpa/inet.h>
47 #include "openconnect.h"
50 #define TUN_HAS_AF_PREFIX 1
54 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
57 vpninfo->progress(vpninfo, PRG_ERR,
58 "No vpnc-script configured. Need Solaris IP-setting code\n");
62 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
67 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
72 memset(&ifr, 0, sizeof(ifr));
73 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
76 struct sockaddr_in addr;
78 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
79 perror("SIOCGIFFLAGS");
81 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
82 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
83 perror("SIOCSIFFLAGS");
85 addr.sin_family = AF_INET;
86 addr.sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
87 memcpy(&ifr.ifr_addr, &addr, sizeof(addr));
88 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
89 perror("SIOCSIFADDR");
92 ifr.ifr_mtu = vpninfo->mtu;
93 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
102 static int setenv_int(const char *opt, int value)
105 sprintf(buf, "%d", value);
106 return setenv(opt, buf, 1);
109 static int process_split_xxclude(struct openconnect_info *vpninfo,
110 char *in_ex, char *route, int *nr_incs)
117 slash = strchr(route, '/');
120 vpninfo->progress(vpninfo, PRG_ERR,
121 "Discard bad split %sclude: \"%s\"\n",
127 if (!inet_aton(route, &addr)) {
133 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *nr_incs);
134 setenv(envname, route, 1);
136 /* Put it back how we found it */
139 if (!inet_aton(slash+1, &addr))
142 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *nr_incs);
143 setenv(envname, slash+1, 1);
145 for (masklen = 0; masklen < 32; masklen++) {
146 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
149 masklen = 32 - masklen;
151 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *nr_incs);
152 setenv_int(envname, masklen);
158 static int appendenv(const char *opt, const char *new)
161 char *old = getenv(opt);
165 snprintf(buf, 1023, "%s %s", old, new);
167 snprintf(buf, 1023, "%s", new);
169 return setenv(opt, buf, 1);
172 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
177 struct vpn_option *opt;
179 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
180 buflen += 2 + strlen(opt->option) + strlen(opt->value);
182 env_buf = malloc(buflen + 1);
188 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
189 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
190 "%s=%s\n", opt->option, opt->value);
192 setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
196 static void set_script_env(struct openconnect_info *vpninfo)
198 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
200 setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
201 setenv("TUNDEV", vpninfo->ifname, 1);
202 setenv("reason", "connect", 1);
203 unsetenv("CISCO_BANNER");
204 unsetenv("CISCO_SPLIT_INC");
205 unsetenv("CISCO_SPLIT_EXC");
207 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
209 if (vpninfo->vpn_addr) {
210 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
211 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
213 if (vpninfo->vpn_addr6) {
214 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
215 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
218 if (vpninfo->vpn_dns[0])
219 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
221 unsetenv("INTERNAL_IP4_DNS");
222 if (vpninfo->vpn_dns[1])
223 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
224 if (vpninfo->vpn_dns[2])
225 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
227 if (vpninfo->vpn_nbns[0])
228 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
230 unsetenv("INTERNAL_IP4_NBNS");
231 if (vpninfo->vpn_nbns[1])
232 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
233 if (vpninfo->vpn_nbns[2])
234 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
236 if (vpninfo->vpn_domain)
237 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
238 else unsetenv ("CISCO_DEF_DOMAIN");
240 if (vpninfo->vpn_proxy_pac)
241 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
243 if (vpninfo->split_includes) {
244 struct split_include *this = vpninfo->split_includes;
245 int nr_split_includes = 0;
248 process_split_xxclude(vpninfo, "IN", this->route,
252 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
254 if (vpninfo->split_excludes) {
255 struct split_include *this = vpninfo->split_excludes;
256 int nr_split_excludes = 0;
259 process_split_xxclude(vpninfo, "EX", this->route,
263 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
265 setenv_cstp_opts(vpninfo);
268 static int script_config_tun(struct openconnect_info *vpninfo)
270 if (vpninfo->peer_addr->sa_family != AF_INET || !vpninfo->vpn_addr) {
271 vpninfo->progress(vpninfo, PRG_ERR,
272 "Script can only handle Legacy IP\n");
276 set_script_env(vpninfo);
278 system(vpninfo->vpnc_script);
283 /* Set up a tuntap device. */
284 int setup_tun(struct openconnect_info *vpninfo)
288 if (vpninfo->script_tun) {
292 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
293 perror("socketpair");
303 setenv_int("VPNFD", fds[1]);
304 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
309 vpninfo->script_tun = child;
310 vpninfo->ifname = "(script)";
312 #ifdef IFF_TUN /* Linux */
315 tun_fd = open("/dev/net/tun", O_RDWR);
317 vpninfo->progress(vpninfo, PRG_ERR,
318 "Failed to open tun device: %s\n",
322 memset(&ifr, 0, sizeof(ifr));
323 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
325 strncpy(ifr.ifr_name, vpninfo->ifname,
326 sizeof(ifr.ifr_name) - 1);
327 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
328 vpninfo->progress(vpninfo, PRG_ERR,
329 "TUNSETIFF failed: %s\n",
333 if (!vpninfo->ifname)
334 vpninfo->ifname = strdup(ifr.ifr_name);
335 #elif defined (__sun__)
336 static char tun_name[80];
337 int tun2_fd, ip_fd = open("/dev/ip", O_RDWR);
342 perror("open /dev/ip");
346 tun_fd = open("/dev/tun", O_RDWR);
348 perror("open /dev/tun");
353 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
355 perror("Failed to create new tun");
361 tun2_fd = open("/dev/tun", O_RDWR);
363 perror("open /dev/tun again");
368 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
369 perror("Can't push IP");
375 if (ioctl(tun2_fd, IF_UNITSEL, &unit_nr) < 0) {
376 perror("Can't select unit");
382 mux_id = ioctl(ip_fd, I_PLINK, tun2_fd);
384 perror("Can't link tun to IP");
392 sprintf(tun_name, "tun%d", unit_nr);
393 vpninfo->ifname = tun_name;
395 memset(&ifr, 0, sizeof(ifr));
396 strcpy(ifr.ifr_name, tun_name);
397 ifr.ifr_ip_muxid = mux_id;
399 if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
400 perror("Set mux id");
402 ioctl(ip_fd, I_PUNLINK, mux_id);
406 /* Solaris tunctl needs this in order to tear it down */
407 vpninfo->progress(vpninfo, PRG_DEBUG, "mux id is %d\n", mux_id);
408 vpninfo->tun_muxid = mux_id;
409 vpninfo->ip_fd = ip_fd;
411 #else /* BSD et al have /dev/tun$x devices */
412 static char tun_name[80];
414 for (i = 0; i < 255; i++) {
415 sprintf(tun_name, "/dev/tun%d", i);
416 tun_fd = open(tun_name, O_RDWR);
424 vpninfo->ifname = tun_name + 5;
426 if (vpninfo->vpnc_script) {
427 script_config_tun(vpninfo);
428 /* We have to set the MTU for ourselves, because the script doesn't */
429 local_config_tun(vpninfo, 1);
431 local_config_tun(vpninfo, 0);
434 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
436 vpninfo->tun_fd = tun_fd;
438 if (vpninfo->select_nfds <= tun_fd)
439 vpninfo->select_nfds = tun_fd + 1;
441 FD_SET(tun_fd, &vpninfo->select_rfds);
443 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
448 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
450 unsigned char buf[2000];
454 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
455 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
456 unsigned char *pkt = buf;
457 #ifdef TUN_HAS_AF_PREFIX
461 if (queue_new_packet(&vpninfo->outgoing_queue, pkt,
466 vpninfo->outgoing_qlen++;
467 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
468 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
472 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
473 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
476 /* The kernel returns -ENOMEM when the queue is full, so theoretically
477 we could handle that and retry... but it doesn't let us poll() for
478 the no-longer-full situation, so let's not bother. */
479 while (vpninfo->incoming_queue) {
480 struct pkt *this = vpninfo->incoming_queue;
481 unsigned char *data = this->data;
484 #ifdef TUN_HAS_AF_PREFIX
485 struct ip *iph = (void *)data;
490 else if (iph->ip_v == 4)
493 static int complained = 0;
496 vpninfo->progress(vpninfo, PRG_ERR,
497 "Unknown packet (len %d) received: %02x %02x %02x %02x...\n",
498 len, data[0], data[1], data[2], data[3]);
505 *(int *)data = htonl(type);
507 vpninfo->incoming_queue = this->next;
509 if (write(vpninfo->tun_fd, data, len) < 0 &&
511 vpninfo->quit_reason = "Client connection terminated";
516 /* Work is not done if we just got rid of packets off the queue */
520 void shutdown_tun(struct openconnect_info *vpninfo)
522 if (vpninfo->script_tun) {
523 kill(vpninfo->script_tun, SIGHUP);
525 if (vpninfo->vpnc_script) {
526 setenv("TUNDEV", vpninfo->ifname, 1);
527 setenv("reason", "disconnect", 1);
528 system(vpninfo->vpnc_script);
531 if (ioctl(vpninfo->ip_fd, I_PUNLINK, vpninfo->tun_muxid) < 0)
532 perror("ioctl(I_PUNLINK)");
534 close(vpninfo->ip_fd);
539 close(vpninfo->tun_fd);
540 vpninfo->tun_fd = -1;