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