Attempt to handle IPv6
[platform/upstream/openconnect.git] / tun.c
1 /*
2  * OpenConnect (SSL + DTLS) VPN client
3  *
4  * Copyright © 2008 Intel Corporation.
5  *
6  * Author: David Woodhouse <dwmw2@infradead.org>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to:
19  *
20  *   Free Software Foundation, Inc.
21  *   51 Franklin Street, Fifth Floor,
22  *   Boston, MA 02110-1301 USA
23  */
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
29 #include <string.h>
30 #ifdef __linux__
31 #include <linux/if_tun.h>
32 #endif
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/in.h>
37 #include <netinet/ip.h>
38 #include <net/if.h>
39 #include <arpa/inet.h>
40 #include <errno.h>
41
42 #include "openconnect.h"
43
44 #ifdef __OpenBSD__
45 #define TUN_HAS_AF_PREFIX 1
46 #endif
47
48 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
49 {
50         struct ifreq ifr;
51         int net_fd;
52
53         net_fd = socket(PF_INET, SOCK_DGRAM, 0);
54         if (net_fd < 0) {
55                 perror("open net");
56                 return -EINVAL;
57         }
58         memset(&ifr, 0, sizeof(ifr));
59         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
60
61         if (!mtu_only) {
62                 struct sockaddr_in addr;
63
64                 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
65                         perror("SIOCGIFFLAGS");
66
67                 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
68                 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
69                         perror("SIOCSIFFLAGS");
70
71                 addr.sin_family = AF_INET;
72                 addr.sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
73                 memcpy(&ifr.ifr_addr, &addr, sizeof(addr));
74                 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
75                         perror("SIOCSIFADDR");
76         }
77
78         ifr.ifr_mtu = vpninfo->mtu;
79         if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
80                 perror("SIOCSIFMTU");
81
82         close(net_fd);
83
84         return 0;
85 }
86
87 static int setenv_int(const char *opt, int value)
88 {
89         char buf[16];
90         sprintf(buf, "%d", value);
91         return setenv(opt, buf, 1);
92 }
93
94 static int process_split_xxclude(struct openconnect_info *vpninfo,
95                                  char *in_ex, char *route, int *nr_incs)
96 {
97         struct in_addr addr;
98         int masklen;
99         char envname[80];
100         char *slash;
101
102         slash = strchr(route, '/');
103         if (!slash) {
104         badinc:
105                 vpninfo->progress(vpninfo, PRG_ERR,
106                                   "Discard bad split %sclude: \"%s\"\n",
107                                   in_ex, route);
108                 return -EINVAL;
109         }
110
111         *slash = 0;
112         if (!inet_aton(route, &addr)) {
113                 *slash = '/';
114                 goto badinc;
115         }
116
117         envname[79] = 0;
118         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *nr_incs);
119         setenv(envname, route, 1);
120
121         /* Put it back how we found it */
122         *slash = '/';
123
124         if (!inet_aton(slash+1, &addr))
125                 goto badinc;
126
127         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *nr_incs);
128         setenv(envname, slash+1, 1);
129
130         for (masklen = 0; masklen < 32; masklen++) {
131                 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
132                         break;
133         }
134         masklen = 32 - masklen;
135
136         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *nr_incs);
137         setenv_int(envname, masklen);
138
139         (*nr_incs)++;
140         return 0;
141 }
142
143 static int appendenv(const char *opt, const char *new)
144 {
145         char buf[1024];
146         char *old = getenv(opt);
147
148         buf[1023] = 0;
149         if (old)
150                 snprintf(buf, 1023, "%s %s", old, new);
151         else
152                 snprintf(buf, 1023, "%s", new);
153
154         return setenv(opt, buf, 1);
155 }
156
157 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
158 {
159         char *env_buf;
160         int buflen = 0;
161         int bufofs = 0;
162         struct vpn_option *opt;
163
164         for (opt = vpninfo->cstp_options; opt; opt = opt->next)
165                 buflen += 2 + strlen(opt->option) + strlen(opt->value);
166
167         env_buf = malloc(buflen + 1);
168         if (!env_buf)
169                 return;
170
171         env_buf[buflen] = 0;
172
173         for (opt = vpninfo->cstp_options; opt; opt = opt->next)
174                 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
175                                    "%s=%s\n", opt->option, opt->value);
176
177         setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
178         free(env_buf);
179 }
180
181 static void set_script_env(struct openconnect_info *vpninfo)
182 {
183         struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
184
185         setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
186         setenv("TUNDEV", vpninfo->ifname, 1);
187         setenv("reason", "connect", 1);
188         unsetenv("CISCO_BANNER");
189         unsetenv("CISCO_SPLIT_INC");
190         unsetenv("CISCO_SPLIT_EXC");
191
192         setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
193
194         if (vpninfo->vpn_addr) {
195                 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
196                 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
197         }
198         if (vpninfo->vpn_addr6) {
199                 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
200                 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
201         }
202
203         if (vpninfo->vpn_dns[0])
204                 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
205         else
206                 unsetenv("INTERNAL_IP4_DNS");
207         if (vpninfo->vpn_dns[1])
208                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
209         if (vpninfo->vpn_dns[2])
210                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
211
212         if (vpninfo->vpn_nbns[0])
213                 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
214         else
215                 unsetenv("INTERNAL_IP4_NBNS");
216         if (vpninfo->vpn_nbns[1])
217                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
218         if (vpninfo->vpn_nbns[2])
219                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
220
221         if (vpninfo->vpn_domain)
222                 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
223         else unsetenv ("CISCO_DEF_DOMAIN");
224
225         if (vpninfo->vpn_proxy_pac)
226                 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
227
228         if (vpninfo->split_includes) {
229                 struct split_include *this = vpninfo->split_includes;
230                 int nr_split_includes = 0;
231
232                 while (this) {
233                         process_split_xxclude(vpninfo, "IN", this->route,
234                                               &nr_split_includes);
235                         this = this->next;
236                 }
237                 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
238         }
239         if (vpninfo->split_excludes) {
240                 struct split_include *this = vpninfo->split_excludes;
241                 int nr_split_excludes = 0;
242
243                 while (this) {
244                         process_split_xxclude(vpninfo, "EX", this->route,
245                                               &nr_split_excludes);
246                         this = this->next;
247                 }
248                 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
249         }
250         setenv_cstp_opts(vpninfo);
251 }
252
253 static int script_config_tun(struct openconnect_info *vpninfo)
254 {
255         if (vpninfo->peer_addr->sa_family != AF_INET || !vpninfo->vpn_addr) {
256                 vpninfo->progress(vpninfo, PRG_ERR,
257                                   "Script can only handle Legacy IP\n");
258                 return -EINVAL;
259         }
260
261         set_script_env(vpninfo);
262
263         system(vpninfo->vpnc_script);
264         return 0;
265 }
266
267
268 /* Set up a tuntap device. */
269 int setup_tun(struct openconnect_info *vpninfo)
270 {
271         int tun_fd;
272
273         if (vpninfo->script_tun) {
274                 pid_t child;
275                 int fds[2];
276
277                 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
278                         perror("socketpair");
279                         exit(1);
280                 }
281                 tun_fd = fds[0];
282                 child = fork();
283                 if (child < 0) {
284                         perror("fork");
285                         exit(1);
286                 } else if (!child) {
287                         close(tun_fd);
288                         setenv_int("VPNFD", fds[1]);
289                         execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
290                         perror("execl");
291                         exit(1);
292                 }
293                 close(fds[1]);
294                 vpninfo->script_tun = child;
295                 vpninfo->ifname = "(script)";
296         } else {
297 #ifdef IFF_TUN /* Linux */
298                 struct ifreq ifr;
299
300                 tun_fd = open("/dev/net/tun", O_RDWR);
301                 if (tun_fd < 0) {
302                         vpninfo->progress(vpninfo, PRG_ERR,
303                                           "Failed to open tun device: %s\n",
304                                           strerror(errno));
305                         exit(1);
306                 }
307                 memset(&ifr, 0, sizeof(ifr));
308                 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
309                 if (vpninfo->ifname)
310                         strncpy(ifr.ifr_name, vpninfo->ifname,
311                                 sizeof(ifr.ifr_name) - 1);
312                 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
313                         vpninfo->progress(vpninfo, PRG_ERR,
314                                           "TUNSETIFF failed: %s\n",
315                                           strerror(errno));
316                         exit(1);
317                 }
318                 if (!vpninfo->ifname)
319                         vpninfo->ifname = strdup(ifr.ifr_name);
320
321 #else /* BSD et al have /dev/tun$x devices */
322                 static char tun_name[80];
323                 int i;
324                 for (i = 0; i < 255; i++) {
325                         sprintf(tun_name, "/dev/tun%d", i);
326                         tun_fd = open(tun_name, O_RDWR);
327                         if (tun_fd >= 0)
328                                 break;
329                 }
330                 if (tun_fd < 0) {
331                         perror("open tun");
332                         exit(1);
333                 }
334                 vpninfo->ifname = tun_name + 5;
335 #endif
336                 if (vpninfo->vpnc_script) {
337                         script_config_tun(vpninfo);
338                         /* We have to set the MTU for ourselves, because the script doesn't */
339                         local_config_tun(vpninfo, 1);
340                 } else
341                         local_config_tun(vpninfo, 0);
342         }
343
344         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
345
346         vpninfo->tun_fd = tun_fd;
347
348         if (vpninfo->select_nfds <= tun_fd)
349                 vpninfo->select_nfds = tun_fd + 1;
350
351         FD_SET(tun_fd, &vpninfo->select_rfds);
352
353         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
354
355         return 0;
356 }
357
358 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
359 {
360         unsigned char buf[2000];
361         int len;
362         int work_done = 0;
363
364         if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
365                 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
366                         unsigned char *pkt = buf;
367 #ifdef TUN_HAS_AF_PREFIX
368                         pkt += 4;
369                         len -= 4;
370 #endif
371                         if (queue_new_packet(&vpninfo->outgoing_queue, pkt,
372                                              len))
373                                 break;
374
375                         work_done = 1;
376                         vpninfo->outgoing_qlen++;
377                         if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
378                                 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
379                                 break;
380                         }
381                 }
382         } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
383                 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
384         }
385
386         /* The kernel returns -ENOMEM when the queue is full, so theoretically
387            we could handle that and retry... but it doesn't let us poll() for
388            the no-longer-full situation, so let's not bother. */
389         while (vpninfo->incoming_queue) {
390                 struct pkt *this = vpninfo->incoming_queue;
391                 unsigned char *data = this->data;
392                 int len = this->len;
393
394 #ifdef TUN_HAS_AF_PREFIX
395                 struct ip *iph = (void *)data;
396                 int type;
397
398                 if (iph->ip_v == 6)
399                         type = AF_INET6;
400                 else if (iph->ip_v == 4)
401                         type = AF_INET;
402                 else {
403                         static int complained = 0;
404                         if (!complained) {
405                                 complained = 1;
406                                 vpninfo->progress(vpninfo, PRG_ERR,
407                                                   "Unknown packet (len %d) received: %02x %02x %02x %02x...\n",
408                                                   len, data[0], data[1], data[2], data[3]);
409                         }
410                         free(this);
411                         continue;
412                 }
413                 data -= 4;
414                 len += 4;
415                 *(int *)data = htonl(type);
416 #endif
417                 vpninfo->incoming_queue = this->next;
418
419                 if (write(vpninfo->tun_fd, data, len) < 0 &&
420                     errno == ENOTCONN) {
421                         vpninfo->quit_reason = "Client connection terminated";
422                         return 1;
423                 }
424                 free(this);
425         }
426         /* Work is not done if we just got rid of packets off the queue */
427         return work_done;
428 }