Make interface name configurable
[platform/upstream/openconnect.git] / tun.c
1 /*
2  * Open AnyConnect (SSL + DTLS) client
3  *
4  * © 2008 David Woodhouse <dwmw2@infradead.org>
5  *
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
9  * in all copies.
10  *
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.
19  */
20
21 #include <string.h>
22 #include <net/if.h>
23 #include <sys/ioctl.h>
24 #include <linux/if_tun.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31
32 #include "anyconnect.h"
33
34 /* Set up a tuntap device. */
35 int setup_tun(struct anyconnect_info *vpninfo)
36 {
37         struct vpn_option *cstp_opt = vpninfo->cstp_options;
38         struct ifreq ifr;
39         struct sockaddr_in *addr;
40         int tun_fd, net_fd;
41         int pfd;
42
43         tun_fd = open("/dev/net/tun", O_RDWR);
44         if (tun_fd < 0) {
45                 perror("open tun");
46                 exit(1);
47         }
48         memset(&ifr, 0, sizeof(ifr));
49         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
50         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
51         if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
52                 perror("TUNSETIFF");
53                 exit(1);
54         }
55
56         net_fd = socket(PF_INET, SOCK_DGRAM, 0);
57         if (net_fd < 0) {
58                 perror("open net");
59                 goto proceed;
60         }
61         memset(&ifr, 0, sizeof(ifr));
62         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
63         if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
64                 perror("SIOCGIFFLAGS");
65         ifr.ifr_flags |= IFF_UP;
66         if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
67                 perror("SIOCSIFFLAGS");
68
69         addr = (struct sockaddr_in *) &ifr.ifr_addr;
70         while (cstp_opt) {
71                 printf("CSTP option %s : %s\n", cstp_opt->option, cstp_opt->value);
72                 if (!strcmp(cstp_opt->option, "X-CSTP-Address")) {
73                         addr->sin_family = AF_INET;
74                         addr->sin_addr.s_addr = inet_addr(cstp_opt->value);
75                         if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
76                                 perror("SIOCSIFADDR");
77                 } else if (!strcmp(cstp_opt->option, "X-CSTP-Netmask")) {
78                         addr->sin_family = AF_INET;
79                         addr->sin_addr.s_addr = inet_addr(cstp_opt->value);
80                         if (ioctl(net_fd, SIOCSIFNETMASK, &ifr) < 0)
81                                 perror("SIOCSIFNETMASK");
82                 }
83                 cstp_opt = cstp_opt->next;
84         }
85         close(net_fd);
86
87 proceed:
88         /* Better still, use lwip and just provide a SOCKS server rather than
89            telling the kernel about it at all */
90         vpninfo->tun_fd = tun_fd;
91         pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
92
93         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
94
95         return 0;
96 }
97
98 int tun_mainloop(struct anyconnect_info *vpninfo, int *timeout)
99 {
100         char buf[2000];
101         int len;
102         int work_done = 0;
103
104         while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
105                 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
106                 work_done = 1;
107         }
108
109         /* The kernel returns -ENOMEM when the queue is full, so theoretically
110            we could handle that and retry... but it doesn't let us poll() for
111            the no-longer-full situation, so let's not bother. */
112         while (vpninfo->incoming_queue) {
113                 struct pkt *this = vpninfo->incoming_queue;
114                 vpninfo->incoming_queue = this->next;
115                 write(vpninfo->tun_fd, this->data, this->len);
116         }
117         /* Work is not done if we just got rid of packets off the queue */
118         return work_done;
119 }