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
25 #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>
36 #include <arpa/inet.h>
39 #include "openconnect.h"
41 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
46 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
51 memset(&ifr, 0, sizeof(ifr));
52 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
55 struct sockaddr_in *addr = (struct sockaddr_in *) &ifr.ifr_addr;
57 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
58 perror("SIOCGIFFLAGS");
60 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
61 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
62 perror("SIOCSIFFLAGS");
64 addr->sin_family = AF_INET;
65 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
66 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
67 perror("SIOCSIFADDR");
70 ifr.ifr_mtu = vpninfo->mtu;
71 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
79 static int setenv_int(const char *opt, int value)
82 sprintf(buf, "%d", value);
83 return setenv(opt, buf, 1);
86 static int appendenv(const char *opt, const char *new)
89 char *old = getenv(opt);
93 snprintf(buf, 1023, "%s %s", old, new);
95 snprintf(buf, 1023, "%s", new);
97 return setenv(opt, buf, 1);
100 static void set_script_env(struct openconnect_info *vpninfo)
102 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
104 setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
105 setenv("TUNDEV", vpninfo->ifname, 1);
106 setenv("reason", "connect", 1);
107 unsetenv("CISCO_BANNER");
108 unsetenv("CISCO_SPLIT_INC");
110 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
112 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
113 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
115 if (vpninfo->vpn_dns[0])
116 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
118 unsetenv("INTERNAL_IP4_DNS");
119 if (vpninfo->vpn_dns[1])
120 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
121 if (vpninfo->vpn_dns[2])
122 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
124 if (vpninfo->vpn_nbns[0])
125 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
127 unsetenv("INTERNAL_IP4_NBNS");
128 if (vpninfo->vpn_nbns[1])
129 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
130 if (vpninfo->vpn_nbns[2])
131 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
133 if (vpninfo->vpn_domain)
134 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
135 else unsetenv ("CISCO_DEF_DOMAIN");
138 static int script_config_tun(struct openconnect_info *vpninfo)
140 if (vpninfo->peer_addr->sa_family != AF_INET) {
141 vpninfo->progress(vpninfo, PRG_ERR, "Script cannot handle anything but Legacy IP\n");
145 set_script_env(vpninfo);
147 system(vpninfo->vpnc_script);
152 /* Set up a tuntap device. */
153 int setup_tun(struct openconnect_info *vpninfo)
159 if (vpninfo->script_tun) {
163 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
164 perror("socketpair");
174 setenv_int("VPNFD", fds[1]);
175 execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
180 vpninfo->script_tun = child;
181 vpninfo->ifname = "(script)";
184 static char tun_name[80];
186 for (i=0; i < 255; i++) {
187 sprintf(tun_name, "/dev/tun%d", i);
188 tun_fd = open(tun_name, O_RDWR);
196 vpninfo->ifname = tun_name + 5;
199 tun_fd = open("/dev/net/tun", O_RDWR);
204 memset(&ifr, 0, sizeof(ifr));
205 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
207 strncpy(ifr.ifr_name, vpninfo->ifname,
208 sizeof(ifr.ifr_name) - 1);
209 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
213 if (!vpninfo->ifname)
214 vpninfo->ifname = strdup(ifr.ifr_name);
217 if (vpninfo->vpnc_script) {
218 script_config_tun(vpninfo);
219 /* We have to set the MTU for ourselves, because the script doesn't */
220 local_config_tun(vpninfo, 1);
222 local_config_tun(vpninfo, 0);
225 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
227 vpninfo->tun_fd = tun_fd;
228 pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
230 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
235 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
241 while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
242 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
246 /* The kernel returns -ENOMEM when the queue is full, so theoretically
247 we could handle that and retry... but it doesn't let us poll() for
248 the no-longer-full situation, so let's not bother. */
249 while (vpninfo->incoming_queue) {
250 struct pkt *this = vpninfo->incoming_queue;
251 vpninfo->incoming_queue = this->next;
252 if (write(vpninfo->tun_fd, this->data, this->len) < 0 &&
254 vpninfo->quit_reason = "Client connection terminated";
258 /* Work is not done if we just got rid of packets off the queue */