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>
30 #include "anyconnect.h"
32 /* Set up a tuntap device. */
33 int setup_tun(struct anyconnect_info *vpninfo)
35 struct vpn_option *cstp_opt = vpninfo->cstp_options;
40 tun_fd = open("/dev/net/tun", O_RDWR);
45 memset(&ifr, 0, sizeof(ifr));
46 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
47 strncpy(ifr.ifr_name, "cisco0", sizeof(ifr.ifr_name) - 1);
48 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0){
53 /* FIXME: Configure it... */
55 printf("CSTP option %s : %s\n", cstp_opt->option, cstp_opt->value);
56 cstp_opt = cstp_opt->next;
59 /* Better still, use lwip and just provide a SOCKS server rather than
60 telling the kernel about it at all */
61 vpninfo->tun_fd = tun_fd;
62 pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
64 fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
69 int tun_mainloop(struct anyconnect_info *vpninfo, int *timeout)
75 while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
76 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
80 /* The kernel returns -ENOMEM when the queue is full, so theoretically
81 we could handle that and retry... but it doesn't let us poll() for
82 the no-longer-full situation, so let's not bother. */
83 while (vpninfo->incoming_queue) {
84 struct pkt *this = vpninfo->incoming_queue;
85 vpninfo->incoming_queue = this->next;
86 write(vpninfo->tun_fd, this->data, this->len);
88 /* Work is not done if we just got rid of packets off the queue */