Clean up CSTP option handling
[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 static int local_config_tun(struct anyconnect_info *vpninfo, int mtu_only)
36 {
37         struct ifreq ifr;
38         int net_fd;
39
40         net_fd = socket(PF_INET, SOCK_DGRAM, 0);
41         if (net_fd < 0) {
42                 perror("open net");
43                 return -EINVAL;
44         }
45         memset(&ifr, 0, sizeof(ifr));
46         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
47
48         if (!mtu_only) {
49                 struct sockaddr_in *addr = (struct sockaddr_in *) &ifr.ifr_addr;
50
51                 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
52                         perror("SIOCGIFFLAGS");
53
54                 ifr.ifr_flags |= IFF_UP;
55                 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
56                         perror("SIOCSIFFLAGS");
57
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");
62
63                 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_netmask);
64                 if (ioctl(net_fd, SIOCSIFNETMASK, &ifr) < 0)
65                         perror("SIOCSIFNETMASK");
66         }
67
68         ifr.ifr_mtu = vpninfo->mtu;
69         if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
70                 perror("SIOCSIFMTU");
71
72         close(net_fd);
73
74         return 0;
75 }
76
77 static int setenv_int(const char *opt, int value)
78 {
79         char buf[16];
80         sprintf(buf, "%d", value);
81         return setenv(opt, buf, 1);
82 }
83
84 static int appendenv(const char *opt, const char *new)
85 {
86         char buf[1024];
87         char *old = getenv(opt);
88
89         buf[1023] = 0;
90         if (old)
91                 snprintf(buf, 1023, "%s %s", old, new);
92         else
93                 snprintf(buf, 1023, "%s", new);
94
95         return setenv(opt, buf, 1);
96 }
97
98 static int script_config_tun(struct anyconnect_info *vpninfo)
99 {
100         struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
101
102         if (vpninfo->peer_addr->sa_family != AF_INET) {
103                 fprintf(stderr, "Script cannot handle anything but Legacy IP\n");
104                 return -EINVAL;
105         }
106
107         setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
108         setenv("TUNDEV", vpninfo->ifname, 1);
109         setenv("reason", "connect", 1);
110         unsetenv("MTU"); /* FIXME: vpnc-script doesn't handle this by default */
111         unsetenv("CISCO_BANNER");
112         unsetenv("CISCO_SPLIT_INC");
113
114         setenv_int("MTU", vpninfo->mtu);
115
116         setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
117         setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
118         
119         if (vpninfo->vpn_dns[0])
120                 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
121         else
122                 unsetenv("INTERNAL_IP4_DNS");
123         if (vpninfo->vpn_dns[1])
124                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
125         if (vpninfo->vpn_dns[2])
126                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
127
128         if (vpninfo->vpn_nbns[0])
129                 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
130         else
131                 unsetenv("INTERNAL_IP4_NBNS");
132         if (vpninfo->vpn_nbns[1])
133                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
134         if (vpninfo->vpn_nbns[2])
135                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
136
137         if (vpninfo->vpn_domain)
138                 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
139         else unsetenv ("CISCO_DEF_DOMAIN");
140
141         system(vpninfo->vpnc_script);
142         return 0;
143 }
144
145
146 /* Set up a tuntap device. */
147 int setup_tun(struct anyconnect_info *vpninfo)
148 {
149         struct ifreq ifr;
150         int tun_fd;
151         int pfd;
152
153         tun_fd = open("/dev/net/tun", O_RDWR);
154         if (tun_fd < 0) {
155                 perror("open tun");
156                 exit(1);
157         }
158         memset(&ifr, 0, sizeof(ifr));
159         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
160         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
161         if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
162                 perror("TUNSETIFF");
163                 exit(1);
164         }
165
166         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
167
168         if (vpninfo->vpnc_script) {
169                 script_config_tun(vpninfo);
170                 /* We have to set the MTU for ourselves, because the script doesn't */
171                 local_config_tun(vpninfo, 1);
172         } else
173                 local_config_tun(vpninfo, 0);
174
175         /* Better still, use lwip and just provide a SOCKS server rather than
176            telling the kernel about it at all */
177         vpninfo->tun_fd = tun_fd;
178         pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
179
180         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
181
182         return 0;
183 }
184
185 int tun_mainloop(struct anyconnect_info *vpninfo, int *timeout)
186 {
187         char buf[2000];
188         int len;
189         int work_done = 0;
190
191         while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
192                 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
193                 work_done = 1;
194         }
195
196         /* The kernel returns -ENOMEM when the queue is full, so theoretically
197            we could handle that and retry... but it doesn't let us poll() for
198            the no-longer-full situation, so let's not bother. */
199         while (vpninfo->incoming_queue) {
200                 struct pkt *this = vpninfo->incoming_queue;
201                 vpninfo->incoming_queue = this->next;
202                 write(vpninfo->tun_fd, this->data, this->len);
203         }
204         /* Work is not done if we just got rid of packets off the queue */
205         return work_done;
206 }