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;
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");
66 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_netmask);
67 if (ioctl(net_fd, SIOCSIFNETMASK, &ifr) < 0)
68 perror("SIOCSIFNETMASK");
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 appendenv(const char *opt, const char *new)
90 char *old = getenv(opt);
94 snprintf(buf, 1023, "%s %s", old, new);
96 snprintf(buf, 1023, "%s", new);
98 return setenv(opt, buf, 1);
101 static int script_config_tun(struct openconnect_info *vpninfo)
103 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
105 if (vpninfo->peer_addr->sa_family != AF_INET) {
106 fprintf(stderr, "Script cannot handle anything but Legacy IP\n");
110 setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
111 setenv("TUNDEV", vpninfo->ifname, 1);
112 setenv("reason", "connect", 1);
113 unsetenv("CISCO_BANNER");
114 unsetenv("CISCO_SPLIT_INC");
116 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
118 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
119 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
121 if (vpninfo->vpn_dns[0])
122 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
124 unsetenv("INTERNAL_IP4_DNS");
125 if (vpninfo->vpn_dns[1])
126 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
127 if (vpninfo->vpn_dns[2])
128 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
130 if (vpninfo->vpn_nbns[0])
131 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
133 unsetenv("INTERNAL_IP4_NBNS");
134 if (vpninfo->vpn_nbns[1])
135 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
136 if (vpninfo->vpn_nbns[2])
137 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
139 if (vpninfo->vpn_domain)
140 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
141 else unsetenv ("CISCO_DEF_DOMAIN");
143 system(vpninfo->vpnc_script);
148 /* Set up a tuntap device. */
149 int setup_tun(struct openconnect_info *vpninfo)
155 tun_fd = open("/dev/net/tun", O_RDWR);
160 memset(&ifr, 0, sizeof(ifr));
161 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
163 strncpy(ifr.ifr_name, vpninfo->ifname,
164 sizeof(ifr.ifr_name) - 1);
165 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
169 if (!vpninfo->ifname)
170 vpninfo->ifname = strdup(ifr.ifr_name);
172 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
174 if (vpninfo->vpnc_script) {
175 script_config_tun(vpninfo);
176 /* We have to set the MTU for ourselves, because the script doesn't */
177 local_config_tun(vpninfo, 1);
179 local_config_tun(vpninfo, 0);
181 /* Better still, use lwip and just provide a SOCKS server rather than
182 telling the kernel about it at all */
183 vpninfo->tun_fd = tun_fd;
184 pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
186 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
191 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
197 while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
198 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
202 /* The kernel returns -ENOMEM when the queue is full, so theoretically
203 we could handle that and retry... but it doesn't let us poll() for
204 the no-longer-full situation, so let's not bother. */
205 while (vpninfo->incoming_queue) {
206 struct pkt *this = vpninfo->incoming_queue;
207 vpninfo->incoming_queue = this->next;
208 write(vpninfo->tun_fd, this->data, this->len);
210 /* Work is not done if we just got rid of packets off the queue */