set up for invoking script for config
[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 #include <errno.h>
32
33 #include "anyconnect.h"
34
35 int local_config_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 net_fd;
41
42         net_fd = socket(PF_INET, SOCK_DGRAM, 0);
43         if (net_fd < 0) {
44                 perror("open net");
45                 return -EINVAL;
46         }
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");
54
55         addr = (struct sockaddr_in *) &ifr.ifr_addr;
56         while (cstp_opt) {
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)
71                                 perror("SIOCSIFMTU");
72                 }
73                 cstp_opt = cstp_opt->next;
74         }
75         close(net_fd);
76
77         return 0;
78 }
79
80 int script_config_tun(struct anyconnect_info *vpninfo)
81 {
82         fprintf(stderr, "FIXME: script config\n");
83         return -EINVAL;
84 }
85
86
87 /* Set up a tuntap device. */
88 int setup_tun(struct anyconnect_info *vpninfo)
89 {
90         struct ifreq ifr;
91         int tun_fd;
92         int pfd;
93
94         tun_fd = open("/dev/net/tun", O_RDWR);
95         if (tun_fd < 0) {
96                 perror("open tun");
97                 exit(1);
98         }
99         memset(&ifr, 0, sizeof(ifr));
100         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
101         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
102         if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
103                 perror("TUNSETIFF");
104                 exit(1);
105         }
106
107         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
108
109         if (vpninfo->vpnc_script)
110                 script_config_tun(vpninfo);
111         else
112                 local_config_tun(vpninfo);
113
114         /* Better still, use lwip and just provide a SOCKS server rather than
115            telling the kernel about it at all */
116         vpninfo->tun_fd = tun_fd;
117         pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
118
119         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
120
121         return 0;
122 }
123
124 int tun_mainloop(struct anyconnect_info *vpninfo, int *timeout)
125 {
126         char buf[2000];
127         int len;
128         int work_done = 0;
129
130         while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
131                 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
132                 work_done = 1;
133         }
134
135         /* The kernel returns -ENOMEM when the queue is full, so theoretically
136            we could handle that and retry... but it doesn't let us poll() for
137            the no-longer-full situation, so let's not bother. */
138         while (vpninfo->incoming_queue) {
139                 struct pkt *this = vpninfo->incoming_queue;
140                 vpninfo->incoming_queue = this->next;
141                 write(vpninfo->tun_fd, this->data, this->len);
142         }
143         /* Work is not done if we just got rid of packets off the queue */
144         return work_done;
145 }