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