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
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
29 #include <linux/if_tun.h>
31 #include <sys/types.h>
35 #include <netinet/in.h>
37 #include <arpa/inet.h>
40 #include "openconnect.h"
42 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
47 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
52 memset(&ifr, 0, sizeof(ifr));
53 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
56 struct sockaddr_in *addr = (struct sockaddr_in *) &ifr.ifr_addr;
58 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
59 perror("SIOCGIFFLAGS");
61 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
62 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
63 perror("SIOCSIFFLAGS");
65 addr->sin_family = AF_INET;
66 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
67 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
68 perror("SIOCSIFADDR");
71 ifr.ifr_mtu = vpninfo->mtu;
72 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
80 static int setenv_int(const char *opt, int value)
83 sprintf(buf, "%d", value);
84 return setenv(opt, buf, 1);
87 static int process_split_xxclude(struct openconnect_info *vpninfo,
88 char *in_ex, char *route, int *nr_incs)
95 slash = strchr(route, '/');
98 vpninfo->progress(vpninfo, PRG_ERR,
99 "Discard bad split %sclude: \"%s\"\n",
105 if (!inet_aton(route, &addr)) {
111 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *nr_incs);
112 setenv(envname, route, 1);
114 /* Put it back how we found it */
117 if (!inet_aton(slash+1, &addr))
120 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *nr_incs);
121 setenv(envname, slash+1, 1);
123 for (masklen = 0; masklen < 32; masklen++) {
124 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
127 masklen = 32 - masklen;
129 snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *nr_incs);
130 setenv_int(envname, masklen);
136 static int appendenv(const char *opt, const char *new)
139 char *old = getenv(opt);
143 snprintf(buf, 1023, "%s %s", old, new);
145 snprintf(buf, 1023, "%s", new);
147 return setenv(opt, buf, 1);
150 static void set_script_env(struct openconnect_info *vpninfo)
152 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
154 setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
155 setenv("TUNDEV", vpninfo->ifname, 1);
156 setenv("reason", "connect", 1);
157 unsetenv("CISCO_BANNER");
158 unsetenv("CISCO_SPLIT_INC");
159 unsetenv("CISCO_SPLIT_EXC");
161 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
163 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
164 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
166 if (vpninfo->vpn_dns[0])
167 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
169 unsetenv("INTERNAL_IP4_DNS");
170 if (vpninfo->vpn_dns[1])
171 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
172 if (vpninfo->vpn_dns[2])
173 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
175 if (vpninfo->vpn_nbns[0])
176 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
178 unsetenv("INTERNAL_IP4_NBNS");
179 if (vpninfo->vpn_nbns[1])
180 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
181 if (vpninfo->vpn_nbns[2])
182 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
184 if (vpninfo->vpn_domain)
185 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
186 else unsetenv ("CISCO_DEF_DOMAIN");
188 if (vpninfo->vpn_proxy_pac)
189 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
191 if (vpninfo->split_includes) {
192 struct split_include *this = vpninfo->split_includes;
193 int nr_split_includes = 0;
196 process_split_xxclude(vpninfo, "IN", this->route,
200 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
202 if (vpninfo->split_excludes) {
203 struct split_include *this = vpninfo->split_excludes;
204 int nr_split_excludes = 0;
207 process_split_xxclude(vpninfo, "EX", this->route,
211 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
215 static int script_config_tun(struct openconnect_info *vpninfo)
217 if (vpninfo->peer_addr->sa_family != AF_INET) {
218 vpninfo->progress(vpninfo, PRG_ERR, "Script cannot handle anything but Legacy IP\n");
222 set_script_env(vpninfo);
224 system(vpninfo->vpnc_script);
229 /* Set up a tuntap device. */
230 int setup_tun(struct openconnect_info *vpninfo)
235 if (vpninfo->script_tun) {
239 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
240 perror("socketpair");
250 setenv_int("VPNFD", fds[1]);
251 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
256 vpninfo->script_tun = child;
257 vpninfo->ifname = "(script)";
259 #ifdef IFF_TUN /* Linux */
260 tun_fd = open("/dev/net/tun", O_RDWR);
262 vpninfo->progress(vpninfo, PRG_ERR,
263 "Failed to open tun device: %s\n",
267 memset(&ifr, 0, sizeof(ifr));
268 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
270 strncpy(ifr.ifr_name, vpninfo->ifname,
271 sizeof(ifr.ifr_name) - 1);
272 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
273 vpninfo->progress(vpninfo, PRG_ERR,
274 "TUNSETIFF failed: %s\n",
278 if (!vpninfo->ifname)
279 vpninfo->ifname = strdup(ifr.ifr_name);
281 #else /* BSD et al have /dev/tun$x devices */
282 static char tun_name[80];
284 for (i = 0; i < 255; i++) {
285 sprintf(tun_name, "/dev/tun%d", i);
286 tun_fd = open(tun_name, O_RDWR);
294 vpninfo->ifname = tun_name + 5;
296 if (vpninfo->vpnc_script) {
297 script_config_tun(vpninfo);
298 /* We have to set the MTU for ourselves, because the script doesn't */
299 local_config_tun(vpninfo, 1);
301 local_config_tun(vpninfo, 0);
304 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
306 vpninfo->tun_fd = tun_fd;
308 if (vpninfo->select_nfds <= tun_fd)
309 vpninfo->select_nfds = tun_fd + 1;
311 FD_SET(tun_fd, &vpninfo->select_rfds);
313 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
318 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
324 if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
325 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
326 if (queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len))
330 vpninfo->outgoing_qlen++;
331 if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
332 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
336 } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
337 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
340 /* The kernel returns -ENOMEM when the queue is full, so theoretically
341 we could handle that and retry... but it doesn't let us poll() for
342 the no-longer-full situation, so let's not bother. */
343 while (vpninfo->incoming_queue) {
344 struct pkt *this = vpninfo->incoming_queue;
345 vpninfo->incoming_queue = this->next;
346 if (write(vpninfo->tun_fd, this->data, this->len) < 0 &&
348 vpninfo->quit_reason = "Client connection terminated";
353 /* Work is not done if we just got rid of packets off the queue */