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