2 * Open AnyConnect (SSL + DTLS) client
4 * © 2008 David Woodhouse <dwmw2@infradead.org>
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose with or without fee is hereby granted, provided
8 * that the above copyright notice and this permission notice appear
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
12 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
14 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
16 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
17 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include <sys/ioctl.h>
24 #include <linux/if_tun.h>
25 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
33 #include "anyconnect.h"
35 static int local_config_tun(struct anyconnect_info *vpninfo, int mtu_only)
40 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
45 memset(&ifr, 0, sizeof(ifr));
46 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
49 struct sockaddr_in *addr = (struct sockaddr_in *) &ifr.ifr_addr;
51 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
52 perror("SIOCGIFFLAGS");
54 ifr.ifr_flags |= IFF_UP;
55 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
56 perror("SIOCSIFFLAGS");
58 addr->sin_family = AF_INET;
59 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
60 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
61 perror("SIOCSIFADDR");
63 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_netmask);
64 if (ioctl(net_fd, SIOCSIFNETMASK, &ifr) < 0)
65 perror("SIOCSIFNETMASK");
68 ifr.ifr_mtu = vpninfo->mtu;
69 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
77 static int setenv_int(const char *opt, int value)
80 sprintf(buf, "%d", value);
81 return setenv(opt, buf, 1);
84 static int appendenv(const char *opt, const char *new)
87 char *old = getenv(opt);
91 snprintf(buf, 1023, "%s %s", old, new);
93 snprintf(buf, 1023, "%s", new);
95 return setenv(opt, buf, 1);
98 static int script_config_tun(struct anyconnect_info *vpninfo)
100 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
102 if (vpninfo->peer_addr->sa_family != AF_INET) {
103 fprintf(stderr, "Script cannot handle anything but Legacy IP\n");
107 setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
108 setenv("TUNDEV", vpninfo->ifname, 1);
109 setenv("reason", "connect", 1);
110 unsetenv("CISCO_BANNER");
111 unsetenv("CISCO_SPLIT_INC");
113 setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
115 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
116 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
118 if (vpninfo->vpn_dns[0])
119 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
121 unsetenv("INTERNAL_IP4_DNS");
122 if (vpninfo->vpn_dns[1])
123 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
124 if (vpninfo->vpn_dns[2])
125 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
127 if (vpninfo->vpn_nbns[0])
128 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
130 unsetenv("INTERNAL_IP4_NBNS");
131 if (vpninfo->vpn_nbns[1])
132 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
133 if (vpninfo->vpn_nbns[2])
134 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
136 if (vpninfo->vpn_domain)
137 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
138 else unsetenv ("CISCO_DEF_DOMAIN");
140 system(vpninfo->vpnc_script);
145 /* Set up a tuntap device. */
146 int setup_tun(struct anyconnect_info *vpninfo)
152 tun_fd = open("/dev/net/tun", O_RDWR);
157 memset(&ifr, 0, sizeof(ifr));
158 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
159 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
160 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
165 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
167 if (vpninfo->vpnc_script) {
168 script_config_tun(vpninfo);
169 /* We have to set the MTU for ourselves, because the script doesn't */
170 local_config_tun(vpninfo, 1);
172 local_config_tun(vpninfo, 0);
174 /* Better still, use lwip and just provide a SOCKS server rather than
175 telling the kernel about it at all */
176 vpninfo->tun_fd = tun_fd;
177 pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
179 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
184 int tun_mainloop(struct anyconnect_info *vpninfo, int *timeout)
190 while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
191 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
195 /* The kernel returns -ENOMEM when the queue is full, so theoretically
196 we could handle that and retry... but it doesn't let us poll() for
197 the no-longer-full situation, so let's not bother. */
198 while (vpninfo->incoming_queue) {
199 struct pkt *this = vpninfo->incoming_queue;
200 vpninfo->incoming_queue = this->next;
201 write(vpninfo->tun_fd, this->data, this->len);
203 /* Work is not done if we just got rid of packets off the queue */