2 * Open AnyConnect (SSL + DTLS) client
4 * © 2008 David Woodhouse <dwmw2@infradead.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to:
19 * Free Software Foundation, Inc.
20 * 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA
26 #include <sys/ioctl.h>
27 #include <linux/if_tun.h>
28 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
36 #include "openconnect.h"
38 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
43 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
48 memset(&ifr, 0, sizeof(ifr));
49 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
52 struct sockaddr_in *addr = (struct sockaddr_in *) &ifr.ifr_addr;
54 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
55 perror("SIOCGIFFLAGS");
57 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
58 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
59 perror("SIOCSIFFLAGS");
61 addr->sin_family = AF_INET;
62 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
63 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
64 perror("SIOCSIFADDR");
67 ifr.ifr_mtu = vpninfo->mtu;
68 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
76 static int setenv_int(const char *opt, int value)
79 sprintf(buf, "%d", value);
80 return setenv(opt, buf, 1);
83 static int appendenv(const char *opt, const char *new)
86 char *old = getenv(opt);
90 snprintf(buf, 1023, "%s %s", old, new);
92 snprintf(buf, 1023, "%s", new);
94 return setenv(opt, buf, 1);
97 static void set_script_env(struct openconnect_info *vpninfo)
99 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
101 setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
102 setenv("TUNDEV", vpninfo->ifname, 1);
103 setenv("reason", "connect", 1);
104 unsetenv("CISCO_BANNER");
105 unsetenv("CISCO_SPLIT_INC");
107 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
109 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
110 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
112 if (vpninfo->vpn_dns[0])
113 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
115 unsetenv("INTERNAL_IP4_DNS");
116 if (vpninfo->vpn_dns[1])
117 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
118 if (vpninfo->vpn_dns[2])
119 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
121 if (vpninfo->vpn_nbns[0])
122 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
124 unsetenv("INTERNAL_IP4_NBNS");
125 if (vpninfo->vpn_nbns[1])
126 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
127 if (vpninfo->vpn_nbns[2])
128 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
130 if (vpninfo->vpn_domain)
131 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
132 else unsetenv ("CISCO_DEF_DOMAIN");
135 static int script_config_tun(struct openconnect_info *vpninfo)
137 if (vpninfo->peer_addr->sa_family != AF_INET) {
138 vpninfo->progress(vpninfo, PRG_ERR, "Script cannot handle anything but Legacy IP\n");
142 set_script_env(vpninfo);
144 system(vpninfo->vpnc_script);
149 /* Set up a tuntap device. */
150 int setup_tun(struct openconnect_info *vpninfo)
156 if (vpninfo->script_tun) {
160 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
161 perror("socketpair");
171 setenv_int("VPNFD", fds[1]);
172 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
177 vpninfo->script_tun = child;
178 vpninfo->ifname = "(script)";
180 tun_fd = open("/dev/net/tun", O_RDWR);
185 memset(&ifr, 0, sizeof(ifr));
186 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
188 strncpy(ifr.ifr_name, vpninfo->ifname,
189 sizeof(ifr.ifr_name) - 1);
190 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
194 if (!vpninfo->ifname)
195 vpninfo->ifname = strdup(ifr.ifr_name);
197 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
199 if (vpninfo->vpnc_script) {
200 script_config_tun(vpninfo);
201 /* We have to set the MTU for ourselves, because the script doesn't */
202 local_config_tun(vpninfo, 1);
204 local_config_tun(vpninfo, 0);
207 vpninfo->tun_fd = tun_fd;
208 pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
210 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
215 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
221 while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
222 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
226 /* The kernel returns -ENOMEM when the queue is full, so theoretically
227 we could handle that and retry... but it doesn't let us poll() for
228 the no-longer-full situation, so let's not bother. */
229 while (vpninfo->incoming_queue) {
230 struct pkt *this = vpninfo->incoming_queue;
231 vpninfo->incoming_queue = this->next;
232 if (write(vpninfo->tun_fd, this->data, this->len) < 0 &&
234 vpninfo->quit_reason = "Client connection terminated";
238 /* Work is not done if we just got rid of packets off the queue */