Pass IPv6 routes separately from Legacy IP routes
[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 #include <signal.h>
31 #ifdef __linux__
32 #include <linux/if_tun.h>
33 #elif defined(__sun__)
34 #include <net/if_tun.h>
35 #include <stropts.h>
36 #include <sys/sockio.h>
37 #endif
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/in.h>
42 #include <netinet/ip.h>
43 #include <net/if.h>
44 #include <arpa/inet.h>
45 #include <errno.h>
46
47 #include "openconnect.h"
48
49 #ifdef __OpenBSD__
50 #define TUN_HAS_AF_PREFIX 1
51 #endif
52
53 #ifdef __sun__
54 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
55 {
56         if (!mtu_only)
57                 vpninfo->progress(vpninfo, PRG_ERR,
58                                   "No vpnc-script configured. Need Solaris IP-setting code\n");
59         return 0;
60 }
61 #else
62 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
63 {
64         struct ifreq ifr;
65         int net_fd;
66
67         net_fd = socket(PF_INET, SOCK_DGRAM, 0);
68         if (net_fd < 0) {
69                 perror("open net");
70                 return -EINVAL;
71         }
72         memset(&ifr, 0, sizeof(ifr));
73         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
74
75         if (!mtu_only) {
76                 struct sockaddr_in addr;
77
78                 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
79                         perror("SIOCGIFFLAGS");
80
81                 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
82                 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
83                         perror("SIOCSIFFLAGS");
84
85                 addr.sin_family = AF_INET;
86                 addr.sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
87                 memcpy(&ifr.ifr_addr, &addr, sizeof(addr));
88                 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
89                         perror("SIOCSIFADDR");
90         }
91
92         ifr.ifr_mtu = vpninfo->mtu;
93         if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
94                 perror("SIOCSIFMTU");
95
96         close(net_fd);
97
98         return 0;
99 }
100 #endif
101
102 static int setenv_int(const char *opt, int value)
103 {
104         char buf[16];
105         sprintf(buf, "%d", value);
106         return setenv(opt, buf, 1);
107 }
108
109 static int netmasklen(struct in_addr addr)
110 {
111         int masklen;
112
113         for (masklen = 0; masklen < 32; masklen++) {
114                 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
115                         break;
116         }
117         return 32 - masklen;
118 }
119
120 static int process_split_xxclude(struct openconnect_info *vpninfo,
121                                  char *in_ex, char *route, int *v4_incs,
122                                  int *v6_incs)
123 {
124         struct in_addr addr;
125         char envname[80];
126         char *slash;
127
128         slash = strchr(route, '/');
129         if (!slash) {
130         badinc:
131                 vpninfo->progress(vpninfo, PRG_ERR,
132                                   "Discard bad split %sclude: \"%s\"\n",
133                                   in_ex, route);
134                 return -EINVAL;
135         }
136
137         *slash = 0;
138
139         if (strchr(route, ':')) {
140                 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_ADDR", in_ex,
141                          *v6_incs);
142                 setenv(envname, route, 1);
143
144                 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_MASKLEN", in_ex,
145                          *v6_incs);
146                 setenv(envname, slash+1, 1);
147
148                 (*v6_incs)++;
149                 return 0;
150         }
151                 
152         if (!inet_aton(route, &addr)) {
153                 *slash = '/';
154                 goto badinc;
155         }
156
157         envname[79] = 0;
158         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *v4_incs);
159         setenv(envname, route, 1);
160
161         /* Put it back how we found it */
162         *slash = '/';
163
164         if (!inet_aton(slash+1, &addr))
165                 goto badinc;
166
167         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *v4_incs);
168         setenv(envname, slash+1, 1);
169
170         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *v4_incs);
171         setenv_int(envname, netmasklen(addr));
172
173         (*v4_incs)++;
174         return 0;
175 }
176
177 static int appendenv(const char *opt, const char *new)
178 {
179         char buf[1024];
180         char *old = getenv(opt);
181
182         buf[1023] = 0;
183         if (old)
184                 snprintf(buf, 1023, "%s %s", old, new);
185         else
186                 snprintf(buf, 1023, "%s", new);
187
188         return setenv(opt, buf, 1);
189 }
190
191 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
192 {
193         char *env_buf;
194         int buflen = 0;
195         int bufofs = 0;
196         struct vpn_option *opt;
197
198         for (opt = vpninfo->cstp_options; opt; opt = opt->next)
199                 buflen += 2 + strlen(opt->option) + strlen(opt->value);
200
201         env_buf = malloc(buflen + 1);
202         if (!env_buf)
203                 return;
204
205         env_buf[buflen] = 0;
206
207         for (opt = vpninfo->cstp_options; opt; opt = opt->next)
208                 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
209                                    "%s=%s\n", opt->option, opt->value);
210
211         setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
212         free(env_buf);
213 }
214
215 static void set_script_env(struct openconnect_info *vpninfo)
216 {
217         struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
218
219         setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
220         setenv("TUNDEV", vpninfo->ifname, 1);
221         setenv("reason", "connect", 1);
222         unsetenv("CISCO_BANNER");
223         unsetenv("CISCO_SPLIT_INC");
224         unsetenv("CISCO_SPLIT_EXC");
225
226         setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
227
228         if (vpninfo->vpn_addr) {
229                 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
230                 if (vpninfo->vpn_netmask) {
231                         struct in_addr addr;
232                         struct in_addr mask;
233
234                         if (inet_aton(vpninfo->vpn_addr, &addr) &&
235                             inet_aton(vpninfo->vpn_netmask, &mask)) {
236                                 char *netaddr;
237
238                                 addr.s_addr &= mask.s_addr;
239                                 netaddr = inet_ntoa(addr);
240
241                                 setenv("INTERNAL_IP4_NETADDR", netaddr, 1);
242                                 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
243                                 setenv_int("INTERNAL_IP4_NETMASKLEN", netmasklen(mask));
244                         }
245                 }
246         }
247         if (vpninfo->vpn_addr6) {
248                 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
249                 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
250         }
251
252         if (vpninfo->vpn_dns[0])
253                 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
254         else
255                 unsetenv("INTERNAL_IP4_DNS");
256         if (vpninfo->vpn_dns[1])
257                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
258         if (vpninfo->vpn_dns[2])
259                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
260
261         if (vpninfo->vpn_nbns[0])
262                 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
263         else
264                 unsetenv("INTERNAL_IP4_NBNS");
265         if (vpninfo->vpn_nbns[1])
266                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
267         if (vpninfo->vpn_nbns[2])
268                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
269
270         if (vpninfo->vpn_domain)
271                 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
272         else unsetenv ("CISCO_DEF_DOMAIN");
273
274         if (vpninfo->vpn_proxy_pac)
275                 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
276
277         if (vpninfo->split_includes) {
278                 struct split_include *this = vpninfo->split_includes;
279                 int nr_split_includes = 0;
280                 int nr_v6_split_includes = 0;
281
282                 while (this) {
283                         process_split_xxclude(vpninfo, "IN", this->route,
284                                               &nr_split_includes,
285                                               &nr_v6_split_includes);
286                         this = this->next;
287                 }
288                 if (nr_split_includes)
289                         setenv_int("CISCO_SPLIT_INC", nr_split_includes);
290                 if (nr_v6_split_includes)
291                         setenv_int("CISCO_IPV6_SPLIT_INC", nr_v6_split_includes);
292         }
293         if (vpninfo->split_excludes) {
294                 struct split_include *this = vpninfo->split_excludes;
295                 int nr_split_excludes = 0;
296                 int nr_v6_split_excludes = 0;
297
298                 while (this) {
299                         process_split_xxclude(vpninfo, "EX", this->route,
300                                               &nr_split_excludes,
301                                               &nr_v6_split_excludes);
302                         this = this->next;
303                 }
304                 if (nr_split_excludes)
305                         setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
306                 if (nr_v6_split_excludes)
307                         setenv_int("CISCO_IPV6_SPLIT_EXC", nr_v6_split_excludes);
308         }
309         setenv_cstp_opts(vpninfo);
310 }
311
312 static int script_config_tun(struct openconnect_info *vpninfo)
313 {
314         if (vpninfo->peer_addr->sa_family != AF_INET || !vpninfo->vpn_addr) {
315                 vpninfo->progress(vpninfo, PRG_ERR,
316                                   "Script can only handle Legacy IP\n");
317                 return -EINVAL;
318         }
319
320         set_script_env(vpninfo);
321
322         system(vpninfo->vpnc_script);
323         return 0;
324 }
325
326
327 /* Set up a tuntap device. */
328 int setup_tun(struct openconnect_info *vpninfo)
329 {
330         int tun_fd;
331
332         if (vpninfo->script_tun) {
333                 pid_t child;
334                 int fds[2];
335
336                 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
337                         perror("socketpair");
338                         exit(1);
339                 }
340                 tun_fd = fds[0];
341                 child = fork();
342                 if (child < 0) {
343                         perror("fork");
344                         exit(1);
345                 } else if (!child) {
346                         close(tun_fd);
347                         setenv_int("VPNFD", fds[1]);
348                         execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
349                         perror("execl");
350                         exit(1);
351                 }
352                 close(fds[1]);
353                 vpninfo->script_tun = child;
354                 vpninfo->ifname = "(script)";
355         } else {
356 #ifdef IFF_TUN /* Linux */
357                 struct ifreq ifr;
358
359                 tun_fd = open("/dev/net/tun", O_RDWR);
360                 if (tun_fd < 0) {
361                         vpninfo->progress(vpninfo, PRG_ERR,
362                                           "Failed to open tun device: %s\n",
363                                           strerror(errno));
364                         exit(1);
365                 }
366                 memset(&ifr, 0, sizeof(ifr));
367                 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
368                 if (vpninfo->ifname)
369                         strncpy(ifr.ifr_name, vpninfo->ifname,
370                                 sizeof(ifr.ifr_name) - 1);
371                 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
372                         vpninfo->progress(vpninfo, PRG_ERR,
373                                           "TUNSETIFF failed: %s\n",
374                                           strerror(errno));
375                         exit(1);
376                 }
377                 if (!vpninfo->ifname)
378                         vpninfo->ifname = strdup(ifr.ifr_name);
379 #elif defined (__sun__)
380                 static char tun_name[80];
381                 int tun2_fd, ip_fd = open("/dev/ip", O_RDWR);
382                 int unit_nr, mux_id;
383                 struct ifreq ifr;
384
385                 if (ip_fd < 0) {
386                         perror("open /dev/ip");
387                         return -EIO;
388                 }
389
390                 tun_fd = open("/dev/tun", O_RDWR);
391                 if (tun_fd < 0) {
392                         perror("open /dev/tun");
393                         close(ip_fd);
394                         return -EIO;
395                 }
396
397                 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
398                 if (unit_nr < 0) {
399                         perror("Failed to create new tun");
400                         close(tun_fd);
401                         close(ip_fd);
402                         return -EIO;
403                 }
404                 
405                 tun2_fd = open("/dev/tun", O_RDWR);
406                 if (tun2_fd < 0) {
407                         perror("open /dev/tun again");
408                         close(tun_fd);
409                         close(ip_fd);
410                         return -EIO;
411                 }
412                 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
413                         perror("Can't push IP");
414                         close(tun2_fd);
415                         close(tun_fd);
416                         close(ip_fd);
417                         return -EIO;
418                 }
419                 if (ioctl(tun2_fd, IF_UNITSEL, &unit_nr) < 0) {
420                         perror("Can't select unit");
421                         close(tun2_fd);
422                         close(tun_fd);
423                         close(ip_fd);
424                         return -EIO;
425                 }
426                 mux_id = ioctl(ip_fd, I_PLINK, tun2_fd);
427                 if (mux_id < 0) {
428                         perror("Can't link tun to IP");
429                         close(tun2_fd);
430                         close(tun_fd);
431                         close(ip_fd);
432                         return -EIO;
433                 }
434                 close(tun2_fd);
435
436                 sprintf(tun_name, "tun%d", unit_nr);
437                 vpninfo->ifname = tun_name;
438
439                 memset(&ifr, 0, sizeof(ifr));
440                 strcpy(ifr.ifr_name, tun_name);
441                 ifr.ifr_ip_muxid = mux_id;
442
443                 if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
444                         perror("Set mux id");
445                         close(tun_fd);
446                         ioctl(ip_fd, I_PUNLINK, mux_id);
447                         close(ip_fd);
448                         return -EIO;
449                 }
450                 /* Solaris tunctl needs this in order to tear it down */
451                 vpninfo->progress(vpninfo, PRG_DEBUG, "mux id is %d\n", mux_id);
452                 vpninfo->tun_muxid = mux_id;
453                 vpninfo->ip_fd = ip_fd;
454
455 #else /* BSD et al have /dev/tun$x devices */
456                 static char tun_name[80];
457                 int i;
458                 for (i = 0; i < 255; i++) {
459                         sprintf(tun_name, "/dev/tun%d", i);
460                         tun_fd = open(tun_name, O_RDWR);
461                         if (tun_fd >= 0)
462                                 break;
463                 }
464                 if (tun_fd < 0) {
465                         perror("open tun");
466                         exit(1);
467                 }
468                 vpninfo->ifname = tun_name + 5;
469 #endif
470                 if (vpninfo->vpnc_script) {
471                         script_config_tun(vpninfo);
472                         /* We have to set the MTU for ourselves, because the script doesn't */
473                         local_config_tun(vpninfo, 1);
474                 } else
475                         local_config_tun(vpninfo, 0);
476         }
477
478         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
479
480         vpninfo->tun_fd = tun_fd;
481
482         if (vpninfo->select_nfds <= tun_fd)
483                 vpninfo->select_nfds = tun_fd + 1;
484
485         FD_SET(tun_fd, &vpninfo->select_rfds);
486
487         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
488
489         return 0;
490 }
491
492 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
493 {
494         unsigned char buf[2000];
495         int len;
496         int work_done = 0;
497
498         if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
499                 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
500                         unsigned char *pkt = buf;
501 #ifdef TUN_HAS_AF_PREFIX
502                         pkt += 4;
503                         len -= 4;
504 #endif
505                         if (queue_new_packet(&vpninfo->outgoing_queue, pkt,
506                                              len))
507                                 break;
508
509                         work_done = 1;
510                         vpninfo->outgoing_qlen++;
511                         if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
512                                 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
513                                 break;
514                         }
515                 }
516         } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
517                 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
518         }
519
520         /* The kernel returns -ENOMEM when the queue is full, so theoretically
521            we could handle that and retry... but it doesn't let us poll() for
522            the no-longer-full situation, so let's not bother. */
523         while (vpninfo->incoming_queue) {
524                 struct pkt *this = vpninfo->incoming_queue;
525                 unsigned char *data = this->data;
526                 int len = this->len;
527
528 #ifdef TUN_HAS_AF_PREFIX
529                 struct ip *iph = (void *)data;
530                 int type;
531
532                 if (iph->ip_v == 6)
533                         type = AF_INET6;
534                 else if (iph->ip_v == 4)
535                         type = AF_INET;
536                 else {
537                         static int complained = 0;
538                         if (!complained) {
539                                 complained = 1;
540                                 vpninfo->progress(vpninfo, PRG_ERR,
541                                                   "Unknown packet (len %d) received: %02x %02x %02x %02x...\n",
542                                                   len, data[0], data[1], data[2], data[3]);
543                         }
544                         free(this);
545                         continue;
546                 }
547                 data -= 4;
548                 len += 4;
549                 *(int *)data = htonl(type);
550 #endif
551                 vpninfo->incoming_queue = this->next;
552
553                 if (write(vpninfo->tun_fd, data, len) < 0 &&
554                     errno == ENOTCONN) {
555                         vpninfo->quit_reason = "Client connection terminated";
556                         return 1;
557                 }
558                 free(this);
559         }
560         /* Work is not done if we just got rid of packets off the queue */
561         return work_done;
562 }
563
564 void shutdown_tun(struct openconnect_info *vpninfo)
565 {       
566         if (vpninfo->script_tun) {
567                 kill(vpninfo->script_tun, SIGHUP);
568         } else {
569                 if (vpninfo->vpnc_script) {
570                         setenv("TUNDEV", vpninfo->ifname, 1);
571                         setenv("reason", "disconnect", 1);
572                         system(vpninfo->vpnc_script);
573                 }
574 #ifdef __sun__
575                 if (ioctl(vpninfo->ip_fd, I_PUNLINK, vpninfo->tun_muxid) < 0)
576                         perror("ioctl(I_PUNLINK)");
577
578                 close(vpninfo->ip_fd);
579                 vpninfo->ip_fd = -1;
580 #endif
581         }
582
583         close(vpninfo->tun_fd);
584         vpninfo->tun_fd = -1;
585 }