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>
36 #include <netinet/in_systm.h>
37 #include <netinet/in.h>
38 #include <netinet/ip.h>
40 #include <arpa/inet.h>
43 #include "openconnect.h"
46 #define TUN_HAS_AF_PREFIX 1
49 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
54 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
59 memset(&ifr, 0, sizeof(ifr));
60 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
63 struct sockaddr_in addr;
65 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
66 perror("SIOCGIFFLAGS");
68 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
69 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
70 perror("SIOCSIFFLAGS");
72 addr.sin_family = AF_INET;
73 addr.sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
74 memcpy(&ifr.ifr_addr, &addr, sizeof(addr));
75 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
76 perror("SIOCSIFADDR");
79 ifr.ifr_mtu = vpninfo->mtu;
80 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
88 static int setenv_int(const char *opt, int value)
91 sprintf(buf, "%d", value);
92 return setenv(opt, buf, 1);
95 static int process_split_xxclude(struct openconnect_info *vpninfo,
96 char *in_ex, char *route, int *nr_incs)
103 slash = strchr(route, '/');
106 vpninfo->progress(vpninfo, PRG_ERR,
107 "Discard bad split %sclude: \"%s\"\n",
113 if (!inet_aton(route, &addr)) {
119 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *nr_incs);
120 setenv(envname, route, 1);
122 /* Put it back how we found it */
125 if (!inet_aton(slash+1, &addr))
128 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *nr_incs);
129 setenv(envname, slash+1, 1);
131 for (masklen = 0; masklen < 32; masklen++) {
132 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
135 masklen = 32 - masklen;
137 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *nr_incs);
138 setenv_int(envname, masklen);
144 static int appendenv(const char *opt, const char *new)
147 char *old = getenv(opt);
151 snprintf(buf, 1023, "%s %s", old, new);
153 snprintf(buf, 1023, "%s", new);
155 return setenv(opt, buf, 1);
158 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
163 struct vpn_option *opt;
165 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
166 buflen += 2 + strlen(opt->option) + strlen(opt->value);
168 env_buf = malloc(buflen + 1);
174 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
175 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
176 "%s=%s\n", opt->option, opt->value);
178 setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
182 static void set_script_env(struct openconnect_info *vpninfo)
184 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
186 setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
187 setenv("TUNDEV", vpninfo->ifname, 1);
188 setenv("reason", "connect", 1);
189 unsetenv("CISCO_BANNER");
190 unsetenv("CISCO_SPLIT_INC");
191 unsetenv("CISCO_SPLIT_EXC");
193 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
195 if (vpninfo->vpn_addr) {
196 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
197 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
199 if (vpninfo->vpn_addr6) {
200 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
201 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
204 if (vpninfo->vpn_dns[0])
205 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
207 unsetenv("INTERNAL_IP4_DNS");
208 if (vpninfo->vpn_dns[1])
209 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
210 if (vpninfo->vpn_dns[2])
211 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
213 if (vpninfo->vpn_nbns[0])
214 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
216 unsetenv("INTERNAL_IP4_NBNS");
217 if (vpninfo->vpn_nbns[1])
218 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
219 if (vpninfo->vpn_nbns[2])
220 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
222 if (vpninfo->vpn_domain)
223 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
224 else unsetenv ("CISCO_DEF_DOMAIN");
226 if (vpninfo->vpn_proxy_pac)
227 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
229 if (vpninfo->split_includes) {
230 struct split_include *this = vpninfo->split_includes;
231 int nr_split_includes = 0;
234 process_split_xxclude(vpninfo, "IN", this->route,
238 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
240 if (vpninfo->split_excludes) {
241 struct split_include *this = vpninfo->split_excludes;
242 int nr_split_excludes = 0;
245 process_split_xxclude(vpninfo, "EX", this->route,
249 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
251 setenv_cstp_opts(vpninfo);
254 static int script_config_tun(struct openconnect_info *vpninfo)
256 if (vpninfo->peer_addr->sa_family != AF_INET || !vpninfo->vpn_addr) {
257 vpninfo->progress(vpninfo, PRG_ERR,
258 "Script can only handle Legacy IP\n");
262 set_script_env(vpninfo);
264 system(vpninfo->vpnc_script);
269 /* Set up a tuntap device. */
270 int setup_tun(struct openconnect_info *vpninfo)
274 if (vpninfo->script_tun) {
278 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
279 perror("socketpair");
289 setenv_int("VPNFD", fds[1]);
290 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
295 vpninfo->script_tun = child;
296 vpninfo->ifname = "(script)";
298 #ifdef IFF_TUN /* Linux */
301 tun_fd = open("/dev/net/tun", O_RDWR);
303 vpninfo->progress(vpninfo, PRG_ERR,
304 "Failed to open tun device: %s\n",
308 memset(&ifr, 0, sizeof(ifr));
309 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
311 strncpy(ifr.ifr_name, vpninfo->ifname,
312 sizeof(ifr.ifr_name) - 1);
313 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
314 vpninfo->progress(vpninfo, PRG_ERR,
315 "TUNSETIFF failed: %s\n",
319 if (!vpninfo->ifname)
320 vpninfo->ifname = strdup(ifr.ifr_name);
322 #else /* BSD et al have /dev/tun$x devices */
323 static char tun_name[80];
325 for (i = 0; i < 255; i++) {
326 sprintf(tun_name, "/dev/tun%d", i);
327 tun_fd = open(tun_name, O_RDWR);
335 vpninfo->ifname = tun_name + 5;
337 if (vpninfo->vpnc_script) {
338 script_config_tun(vpninfo);
339 /* We have to set the MTU for ourselves, because the script doesn't */
340 local_config_tun(vpninfo, 1);
342 local_config_tun(vpninfo, 0);
345 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
347 vpninfo->tun_fd = tun_fd;
349 if (vpninfo->select_nfds <= tun_fd)
350 vpninfo->select_nfds = tun_fd + 1;
352 FD_SET(tun_fd, &vpninfo->select_rfds);
354 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
359 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
361 unsigned char buf[2000];
365 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
366 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
367 unsigned char *pkt = buf;
368 #ifdef TUN_HAS_AF_PREFIX
372 if (queue_new_packet(&vpninfo->outgoing_queue, pkt,
377 vpninfo->outgoing_qlen++;
378 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
379 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
383 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
384 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
387 /* The kernel returns -ENOMEM when the queue is full, so theoretically
388 we could handle that and retry... but it doesn't let us poll() for
389 the no-longer-full situation, so let's not bother. */
390 while (vpninfo->incoming_queue) {
391 struct pkt *this = vpninfo->incoming_queue;
392 unsigned char *data = this->data;
395 #ifdef TUN_HAS_AF_PREFIX
396 struct ip *iph = (void *)data;
401 else if (iph->ip_v == 4)
404 static int complained = 0;
407 vpninfo->progress(vpninfo, PRG_ERR,
408 "Unknown packet (len %d) received: %02x %02x %02x %02x...\n",
409 len, data[0], data[1], data[2], data[3]);
416 *(int *)data = htonl(type);
418 vpninfo->incoming_queue = this->next;
420 if (write(vpninfo->tun_fd, data, len) < 0 &&
422 vpninfo->quit_reason = "Client connection terminated";
427 /* Work is not done if we just got rid of packets off the queue */
431 void shutdown_tun(struct openconnect_info *vpninfo)
433 if (vpninfo->script_tun) {
434 kill(vpninfo->script_tun, SIGHUP);
435 } else if (vpninfo->vpnc_script) {
436 setenv("TUNDEV", vpninfo->ifname, 1);
437 setenv("reason", "disconnect", 1);
438 system(vpninfo->vpnc_script);
441 close(vpninfo->tun_fd);
442 vpninfo->tun_fd = -1;