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 int local_config_tun(struct anyconnect_info *vpninfo)
37 struct vpn_option *cstp_opt = vpninfo->cstp_options;
39 struct sockaddr_in *addr;
42 net_fd = socket(PF_INET, SOCK_DGRAM, 0);
47 memset(&ifr, 0, sizeof(ifr));
48 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
49 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
50 perror("SIOCGIFFLAGS");
51 ifr.ifr_flags |= IFF_UP;
52 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
53 perror("SIOCSIFFLAGS");
55 addr = (struct sockaddr_in *) &ifr.ifr_addr;
57 printf("CSTP option %s : %s\n", cstp_opt->option, cstp_opt->value);
58 if (!strcmp(cstp_opt->option, "X-CSTP-Address")) {
59 addr->sin_family = AF_INET;
60 addr->sin_addr.s_addr = inet_addr(cstp_opt->value);
61 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
62 perror("SIOCSIFADDR");
63 } else if (!strcmp(cstp_opt->option, "X-CSTP-Netmask")) {
64 addr->sin_family = AF_INET;
65 addr->sin_addr.s_addr = inet_addr(cstp_opt->value);
66 if (ioctl(net_fd, SIOCSIFNETMASK, &ifr) < 0)
67 perror("SIOCSIFNETMASK");
68 } else if (!strcmp(cstp_opt->option, "X-CSTP-MTU")) {
69 ifr.ifr_mtu = atol(cstp_opt->value);
70 if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
73 cstp_opt = cstp_opt->next;
80 int appendenv(const char *opt, const char *new)
83 char *old = getenv(opt);
87 snprintf(buf, 1023, "%s %s", old, new);
89 snprintf(buf, 1023, "%s", new);
91 return setenv(opt, buf, 1);
94 int script_config_tun(struct anyconnect_info *vpninfo)
96 struct vpn_option *cstp_opt = vpninfo->cstp_options;
97 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
99 if (vpninfo->peer_addr->sa_family != AF_INET) {
100 fprintf(stderr, "Script cannot handle anything but Legacy IP\n");
104 setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
105 setenv("TUNDEV", vpninfo->ifname, 1);
106 setenv("reason", "connect", 1);
107 unsetenv("MTU"); /* FIXME: vpnc-script doesn't handle this by default */
108 unsetenv("CISCO_BANNER");
109 unsetenv("CISCO_DEF_DOMAIN");
110 unsetenv("CISCO_SPLIT_INC");
111 unsetenv("INTERNAL_IP4_NBNS");
112 unsetenv("INTERNAL_IP4_DNS");
113 unsetenv("INTERNAL_IP4_NETMASK");
114 unsetenv("INTERNAL_IP4_ADDRESS");
117 printf("CSTP option %s : %s\n", cstp_opt->option, cstp_opt->value);
118 if (!strcmp(cstp_opt->option, "X-CSTP-MTU"))
119 setenv("MTU", cstp_opt->value, 1);
120 else if (!strcmp(cstp_opt->option, "X-CSTP-Address"))
121 setenv("INTERNAL_IP4_ADDRESS", cstp_opt->value, 1);
122 else if (!strcmp(cstp_opt->option, "X-CSTP-Netmask"))
123 setenv("INTERNAL_IP4_NETMASK", cstp_opt->value, 1);
124 else if (!strcmp(cstp_opt->option, "X-CSTP-DNS"))
125 appendenv("INTERNAL_IP4_DNS", cstp_opt->value);
126 else if (!strcmp(cstp_opt->option, "X-CSTP-NBNS"))
127 appendenv("INTERNAL_IP4_NBNS", cstp_opt->value);
129 cstp_opt = cstp_opt->next;
132 system(vpninfo->vpnc_script);
137 /* Set up a tuntap device. */
138 int setup_tun(struct anyconnect_info *vpninfo)
144 tun_fd = open("/dev/net/tun", O_RDWR);
149 memset(&ifr, 0, sizeof(ifr));
150 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
151 strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
152 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
157 fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
159 if (vpninfo->vpnc_script)
160 script_config_tun(vpninfo);
162 local_config_tun(vpninfo);
164 /* Better still, use lwip and just provide a SOCKS server rather than
165 telling the kernel about it at all */
166 vpninfo->tun_fd = tun_fd;
167 pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
169 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
174 int tun_mainloop(struct anyconnect_info *vpninfo, int *timeout)
180 while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
181 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
185 /* The kernel returns -ENOMEM when the queue is full, so theoretically
186 we could handle that and retry... but it doesn't let us poll() for
187 the no-longer-full situation, so let's not bother. */
188 while (vpninfo->incoming_queue) {
189 struct pkt *this = vpninfo->incoming_queue;
190 vpninfo->incoming_queue = this->next;
191 write(vpninfo->tun_fd, this->data, this->len);
193 /* Work is not done if we just got rid of packets off the queue */