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>
31 #include <linux/if_tun.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/in.h>
37 #include <netinet/ip.h>
39 #include <arpa/inet.h>
42 #include "openconnect.h"
45 #define TUN_HAS_AF_PREFIX 1
48 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
53 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
58 memset(&ifr, 0, sizeof(ifr));
59 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
62 struct sockaddr_in addr;
64 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
65 perror("SIOCGIFFLAGS");
67 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
68 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
69 perror("SIOCSIFFLAGS");
71 addr.sin_family = AF_INET;
72 addr.sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
73 memcpy(&ifr.ifr_addr, &addr, sizeof(addr));
74 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
75 perror("SIOCSIFADDR");
78 ifr.ifr_mtu = vpninfo->mtu;
79 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
87 static int setenv_int(const char *opt, int value)
90 sprintf(buf, "%d", value);
91 return setenv(opt, buf, 1);
94 static int process_split_xxclude(struct openconnect_info *vpninfo,
95 char *in_ex, char *route, int *nr_incs)
102 slash = strchr(route, '/');
105 vpninfo->progress(vpninfo, PRG_ERR,
106 "Discard bad split %sclude: \"%s\"\n",
112 if (!inet_aton(route, &addr)) {
118 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *nr_incs);
119 setenv(envname, route, 1);
121 /* Put it back how we found it */
124 if (!inet_aton(slash+1, &addr))
127 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *nr_incs);
128 setenv(envname, slash+1, 1);
130 for (masklen = 0; masklen < 32; masklen++) {
131 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
134 masklen = 32 - masklen;
136 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *nr_incs);
137 setenv_int(envname, masklen);
143 static int appendenv(const char *opt, const char *new)
146 char *old = getenv(opt);
150 snprintf(buf, 1023, "%s %s", old, new);
152 snprintf(buf, 1023, "%s", new);
154 return setenv(opt, buf, 1);
157 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
162 struct vpn_option *opt;
164 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
165 buflen += 2 + strlen(opt->option) + strlen(opt->value);
167 env_buf = malloc(buflen + 1);
173 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
174 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
175 "%s=%s\n", opt->option, opt->value);
177 setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
181 static void set_script_env(struct openconnect_info *vpninfo)
183 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
185 setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
186 setenv("TUNDEV", vpninfo->ifname, 1);
187 setenv("reason", "connect", 1);
188 unsetenv("CISCO_BANNER");
189 unsetenv("CISCO_SPLIT_INC");
190 unsetenv("CISCO_SPLIT_EXC");
192 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
194 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
195 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
197 if (vpninfo->vpn_dns[0])
198 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
200 unsetenv("INTERNAL_IP4_DNS");
201 if (vpninfo->vpn_dns[1])
202 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
203 if (vpninfo->vpn_dns[2])
204 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
206 if (vpninfo->vpn_nbns[0])
207 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
209 unsetenv("INTERNAL_IP4_NBNS");
210 if (vpninfo->vpn_nbns[1])
211 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
212 if (vpninfo->vpn_nbns[2])
213 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
215 if (vpninfo->vpn_domain)
216 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
217 else unsetenv ("CISCO_DEF_DOMAIN");
219 if (vpninfo->vpn_proxy_pac)
220 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
222 if (vpninfo->split_includes) {
223 struct split_include *this = vpninfo->split_includes;
224 int nr_split_includes = 0;
227 process_split_xxclude(vpninfo, "IN", this->route,
231 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
233 if (vpninfo->split_excludes) {
234 struct split_include *this = vpninfo->split_excludes;
235 int nr_split_excludes = 0;
238 process_split_xxclude(vpninfo, "EX", this->route,
242 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
244 setenv_cstp_opts(vpninfo);
247 static int script_config_tun(struct openconnect_info *vpninfo)
249 if (vpninfo->peer_addr->sa_family != AF_INET) {
250 vpninfo->progress(vpninfo, PRG_ERR, "Script cannot handle anything but Legacy IP\n");
254 set_script_env(vpninfo);
256 system(vpninfo->vpnc_script);
261 /* Set up a tuntap device. */
262 int setup_tun(struct openconnect_info *vpninfo)
266 if (vpninfo->script_tun) {
270 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
271 perror("socketpair");
281 setenv_int("VPNFD", fds[1]);
282 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
287 vpninfo->script_tun = child;
288 vpninfo->ifname = "(script)";
290 #ifdef IFF_TUN /* Linux */
293 tun_fd = open("/dev/net/tun", O_RDWR);
295 vpninfo->progress(vpninfo, PRG_ERR,
296 "Failed to open tun device: %s\n",
300 memset(&ifr, 0, sizeof(ifr));
301 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
303 strncpy(ifr.ifr_name, vpninfo->ifname,
304 sizeof(ifr.ifr_name) - 1);
305 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
306 vpninfo->progress(vpninfo, PRG_ERR,
307 "TUNSETIFF failed: %s\n",
311 if (!vpninfo->ifname)
312 vpninfo->ifname = strdup(ifr.ifr_name);
314 #else /* BSD et al have /dev/tun$x devices */
315 static char tun_name[80];
317 for (i = 0; i < 255; i++) {
318 sprintf(tun_name, "/dev/tun%d", i);
319 tun_fd = open(tun_name, O_RDWR);
327 vpninfo->ifname = tun_name + 5;
329 if (vpninfo->vpnc_script) {
330 script_config_tun(vpninfo);
331 /* We have to set the MTU for ourselves, because the script doesn't */
332 local_config_tun(vpninfo, 1);
334 local_config_tun(vpninfo, 0);
337 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
339 vpninfo->tun_fd = tun_fd;
341 if (vpninfo->select_nfds <= tun_fd)
342 vpninfo->select_nfds = tun_fd + 1;
344 FD_SET(tun_fd, &vpninfo->select_rfds);
346 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
351 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
353 unsigned char buf[2000];
357 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
358 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
359 unsigned char *pkt = buf;
362 type = ntohl(*(int *)buf);
363 if (type != AF_INET && type != AF_INET6)
368 struct ip *iph = (void *)buf;
371 else if (iph->ip_v == 4)
376 if (queue_new_packet(&vpninfo->outgoing_queue, type,
381 vpninfo->outgoing_qlen++;
382 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
383 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
387 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
388 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
391 /* The kernel returns -ENOMEM when the queue is full, so theoretically
392 we could handle that and retry... but it doesn't let us poll() for
393 the no-longer-full situation, so let's not bother. */
394 while (vpninfo->incoming_queue) {
395 struct pkt *this = vpninfo->incoming_queue;
396 unsigned char *data = this->data;
399 vpninfo->incoming_queue = this->next;
404 *(int *)data = htonl(this->type);
407 if (write(vpninfo->tun_fd, data, len) < 0 &&
409 vpninfo->quit_reason = "Client connection terminated";
414 /* Work is not done if we just got rid of packets off the queue */