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.h>
37 #include <arpa/inet.h>
40 #include "openconnect.h"
43 #define TUN_HAS_AF_PREFIX 1
46 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
51 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
56 memset(&ifr, 0, sizeof(ifr));
57 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
60 struct sockaddr_in *addr = (struct sockaddr_in *) &ifr.ifr_addr;
62 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
63 perror("SIOCGIFFLAGS");
65 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
66 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
67 perror("SIOCSIFFLAGS");
69 addr->sin_family = AF_INET;
70 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
71 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
72 perror("SIOCSIFADDR");
75 ifr.ifr_mtu = vpninfo->mtu;
76 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
84 static int setenv_int(const char *opt, int value)
87 sprintf(buf, "%d", value);
88 return setenv(opt, buf, 1);
91 static int process_split_xxclude(struct openconnect_info *vpninfo,
92 char *in_ex, char *route, int *nr_incs)
99 slash = strchr(route, '/');
102 vpninfo->progress(vpninfo, PRG_ERR,
103 "Discard bad split %sclude: \"%s\"\n",
109 if (!inet_aton(route, &addr)) {
115 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *nr_incs);
116 setenv(envname, route, 1);
118 /* Put it back how we found it */
121 if (!inet_aton(slash+1, &addr))
124 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *nr_incs);
125 setenv(envname, slash+1, 1);
127 for (masklen = 0; masklen < 32; masklen++) {
128 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
131 masklen = 32 - masklen;
133 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *nr_incs);
134 setenv_int(envname, masklen);
140 static int appendenv(const char *opt, const char *new)
143 char *old = getenv(opt);
147 snprintf(buf, 1023, "%s %s", old, new);
149 snprintf(buf, 1023, "%s", new);
151 return setenv(opt, buf, 1);
154 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
159 struct vpn_option *opt;
161 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
162 buflen += 2 + strlen(opt->option) + strlen(opt->value);
164 env_buf = malloc(buflen + 1);
170 for (opt = vpninfo->cstp_options; opt; opt = opt->next)
171 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
172 "%s=%s\n", opt->option, opt->value);
174 setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
178 static void set_script_env(struct openconnect_info *vpninfo)
180 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
182 setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
183 setenv("TUNDEV", vpninfo->ifname, 1);
184 setenv("reason", "connect", 1);
185 unsetenv("CISCO_BANNER");
186 unsetenv("CISCO_SPLIT_INC");
187 unsetenv("CISCO_SPLIT_EXC");
189 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
191 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
192 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
194 if (vpninfo->vpn_dns[0])
195 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
197 unsetenv("INTERNAL_IP4_DNS");
198 if (vpninfo->vpn_dns[1])
199 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
200 if (vpninfo->vpn_dns[2])
201 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
203 if (vpninfo->vpn_nbns[0])
204 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
206 unsetenv("INTERNAL_IP4_NBNS");
207 if (vpninfo->vpn_nbns[1])
208 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
209 if (vpninfo->vpn_nbns[2])
210 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
212 if (vpninfo->vpn_domain)
213 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
214 else unsetenv ("CISCO_DEF_DOMAIN");
216 if (vpninfo->vpn_proxy_pac)
217 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
219 if (vpninfo->split_includes) {
220 struct split_include *this = vpninfo->split_includes;
221 int nr_split_includes = 0;
224 process_split_xxclude(vpninfo, "IN", this->route,
228 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
230 if (vpninfo->split_excludes) {
231 struct split_include *this = vpninfo->split_excludes;
232 int nr_split_excludes = 0;
235 process_split_xxclude(vpninfo, "EX", this->route,
239 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
241 setenv_cstp_opts(vpninfo);
244 static int script_config_tun(struct openconnect_info *vpninfo)
246 if (vpninfo->peer_addr->sa_family != AF_INET) {
247 vpninfo->progress(vpninfo, PRG_ERR, "Script cannot handle anything but Legacy IP\n");
251 set_script_env(vpninfo);
253 system(vpninfo->vpnc_script);
258 /* Set up a tuntap device. */
259 int setup_tun(struct openconnect_info *vpninfo)
263 if (vpninfo->script_tun) {
267 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
268 perror("socketpair");
278 setenv_int("VPNFD", fds[1]);
279 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
284 vpninfo->script_tun = child;
285 vpninfo->ifname = "(script)";
287 #ifdef IFF_TUN /* Linux */
290 tun_fd = open("/dev/net/tun", O_RDWR);
292 vpninfo->progress(vpninfo, PRG_ERR,
293 "Failed to open tun device: %s\n",
297 memset(&ifr, 0, sizeof(ifr));
298 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
300 strncpy(ifr.ifr_name, vpninfo->ifname,
301 sizeof(ifr.ifr_name) - 1);
302 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
303 vpninfo->progress(vpninfo, PRG_ERR,
304 "TUNSETIFF failed: %s\n",
308 if (!vpninfo->ifname)
309 vpninfo->ifname = strdup(ifr.ifr_name);
311 #else /* BSD et al have /dev/tun$x devices */
312 static char tun_name[80];
314 for (i = 0; i < 255; i++) {
315 sprintf(tun_name, "/dev/tun%d", i);
316 tun_fd = open(tun_name, O_RDWR);
324 vpninfo->ifname = tun_name + 5;
326 if (vpninfo->vpnc_script) {
327 script_config_tun(vpninfo);
328 /* We have to set the MTU for ourselves, because the script doesn't */
329 local_config_tun(vpninfo, 1);
331 local_config_tun(vpninfo, 0);
334 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
336 vpninfo->tun_fd = tun_fd;
338 if (vpninfo->select_nfds <= tun_fd)
339 vpninfo->select_nfds = tun_fd + 1;
341 FD_SET(tun_fd, &vpninfo->select_rfds);
343 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
348 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
350 unsigned char buf[2000];
354 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
355 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
356 unsigned char *pkt = buf;
357 #ifdef TUN_HAS_AF_PREFIX
358 if (*(int *)buf != htonl(AF_INET))
363 if (queue_new_packet(&vpninfo->outgoing_queue, AF_INET,
368 vpninfo->outgoing_qlen++;
369 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
370 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
374 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
375 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
378 /* The kernel returns -ENOMEM when the queue is full, so theoretically
379 we could handle that and retry... but it doesn't let us poll() for
380 the no-longer-full situation, so let's not bother. */
381 while (vpninfo->incoming_queue) {
382 struct pkt *this = vpninfo->incoming_queue;
383 unsigned char *data = this->data;
386 vpninfo->incoming_queue = this->next;
388 #ifdef TUN_HAS_AF_PREFIX
391 *(int *)data = htonl(AF_INET);
394 if (write(vpninfo->tun_fd, data, len) < 0 &&
396 vpninfo->quit_reason = "Client connection terminated";
401 /* Work is not done if we just got rid of packets off the queue */