Eliminate memcpy() for outgoing packets from tun device
[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 /* Set up a tuntap device. */
393 int setup_tun(struct openconnect_info *vpninfo)
394 {
395         int tun_fd;
396
397         set_script_env(vpninfo);
398
399         if (vpninfo->script_tun) {
400                 pid_t child;
401                 int fds[2];
402
403                 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
404                         perror(_("socketpair"));
405                         exit(1);
406                 }
407                 tun_fd = fds[0];
408                 child = fork();
409                 if (child < 0) {
410                         perror(_("fork"));
411                         exit(1);
412                 } else if (!child) {
413                         close(tun_fd);
414                         setenv_int("VPNFD", fds[1]);
415                         execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
416                         perror(_("execl"));
417                         exit(1);
418                 }
419                 close(fds[1]);
420                 vpninfo->script_tun = child;
421                 vpninfo->ifname = strdup(_("(script)"));
422         } else {
423 #ifdef IFF_TUN /* Linux */
424                 struct ifreq ifr;
425                 int tunerr;
426
427                 tun_fd = open("/dev/net/tun", O_RDWR);
428                 if (tun_fd < 0) {
429                         /* Android has /dev/tun instead of /dev/net/tun
430                            Since other systems might have too, just try it
431                            as a fallback instead of using ifdef __ANDROID__ */
432                         tunerr = errno;
433                         tun_fd = open("/dev/tun", O_RDWR);
434                 }
435                 if (tun_fd < 0) {
436                         /* If the error on /dev/tun is ENOENT, that's boring.
437                            Use the error we got on /dev/net/tun instead */
438                         if (errno != -ENOENT)
439                                 tunerr = errno;
440
441                         vpn_progress(vpninfo, PRG_ERR,
442                                      _("Failed to open tun device: %s\n"),
443                                      strerror(tunerr));
444                         exit(1);
445                 }
446                 memset(&ifr, 0, sizeof(ifr));
447                 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
448                 if (vpninfo->ifname)
449                         strncpy(ifr.ifr_name, vpninfo->ifname,
450                                 sizeof(ifr.ifr_name) - 1);
451                 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
452                         vpn_progress(vpninfo, PRG_ERR,
453                                      _("TUNSETIFF failed: %s\n"),
454                                      strerror(errno));
455                         exit(1);
456                 }
457                 if (!vpninfo->ifname)
458                         vpninfo->ifname = strdup(ifr.ifr_name);
459 #elif defined (__sun__)
460                 static char tun_name[80];
461                 int tun2_fd, ip_fd = open("/dev/ip", O_RDWR);
462                 int unit_nr, mux_id;
463                 struct ifreq ifr;
464
465                 if (ip_fd < 0) {
466                         perror(_("open /dev/ip"));
467                         return -EIO;
468                 }
469
470                 tun_fd = open("/dev/tun", O_RDWR);
471                 if (tun_fd < 0) {
472                         perror(_("open /dev/tun"));
473                         close(ip_fd);
474                         return -EIO;
475                 }
476
477                 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
478                 if (unit_nr < 0) {
479                         perror(_("Failed to create new tun"));
480                         close(tun_fd);
481                         close(ip_fd);
482                         return -EIO;
483                 }
484                 
485                 tun2_fd = open("/dev/tun", O_RDWR);
486                 if (tun2_fd < 0) {
487                         perror(_("open /dev/tun again"));
488                         close(tun_fd);
489                         close(ip_fd);
490                         return -EIO;
491                 }
492                 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
493                         perror(_("Can't push IP"));
494                         close(tun2_fd);
495                         close(tun_fd);
496                         close(ip_fd);
497                         return -EIO;
498                 }
499                 if (ioctl(tun2_fd, IF_UNITSEL, &unit_nr) < 0) {
500                         perror(_("Can't select unit"));
501                         close(tun2_fd);
502                         close(tun_fd);
503                         close(ip_fd);
504                         return -EIO;
505                 }
506                 mux_id = ioctl(ip_fd, I_LINK, tun2_fd);
507                 if (mux_id < 0) {
508                         perror(_("Can't link tun to IP"));
509                         close(tun2_fd);
510                         close(tun_fd);
511                         close(ip_fd);
512                         return -EIO;
513                 }
514                 close(tun2_fd);
515
516                 sprintf(tun_name, "tun%d", unit_nr);
517                 vpninfo->ifname = strdup(tun_name);
518
519                 memset(&ifr, 0, sizeof(ifr));
520                 strcpy(ifr.ifr_name, tun_name);
521                 ifr.ifr_ip_muxid = mux_id;
522
523                 if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
524                         perror(_("Set mux id"));
525                         close(tun_fd);
526                         close(ip_fd);
527                         return -EIO;
528                 }
529                 vpninfo->ip_fd = ip_fd;
530
531 #else /* BSD et al have /dev/tun$x devices */
532                 static char tun_name[80];
533                 int i;
534                 for (i = 0; i < 255; i++) {
535                         sprintf(tun_name, "/dev/tun%d", i);
536                         tun_fd = open(tun_name, O_RDWR);
537                         if (tun_fd >= 0)
538                                 break;
539                 }
540                 if (tun_fd < 0) {
541                         perror(_("open tun"));
542                         exit(1);
543                 }
544                 vpninfo->ifname = strdup(tun_name + 5);
545 #ifdef TUNSIFHEAD
546                 i = 1;
547                 if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
548                         perror(_("TUNSIFHEAD"));
549                         exit(1);
550                 }
551 #endif
552 #endif
553                 if (vpninfo->vpnc_script) {
554                         setenv("TUNDEV", vpninfo->ifname, 1);
555                         script_config_tun(vpninfo);
556                         /* We have to set the MTU for ourselves, because the script doesn't */
557                         local_config_tun(vpninfo, 1);
558                 } else
559                         local_config_tun(vpninfo, 0);
560         }
561
562         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
563
564         vpninfo->tun_fd = tun_fd;
565
566         if (vpninfo->select_nfds <= tun_fd)
567                 vpninfo->select_nfds = tun_fd + 1;
568
569         FD_SET(tun_fd, &vpninfo->select_rfds);
570
571         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
572
573         return 0;
574 }
575
576 static struct pkt *out_pkt;
577
578 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
579 {
580         int work_done = 0;
581         int moredata = 1;
582
583         if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
584                 while (moredata) {
585                         int len = vpninfo->mtu;
586
587                         if (!out_pkt) {
588                                 out_pkt = malloc(sizeof(struct pkt) + len);
589                                 if (!out_pkt) {
590                                         vpn_progress(vpninfo, PRG_ERR, "Allocation failed\n");
591                                         break;
592                                 }
593                         }
594
595 #ifdef __sun__
596                         /* On Solaris, if we're actually using tuntap and *not* just
597                            passing packets through a UNIX socket to a child process,
598                            we have to use getmsg() to ensure that we only get *one*
599                            packet at a time. */
600                         if (!vpninfo->script_tun) {
601                                 struct strbuf strb;
602                                 int flags = 0;
603                                 int ret;
604                                 
605                                 strb.buf = out_pkt->data;
606                                 strb.maxlen = len;
607                                 strb.len = 0;
608                                 ret = getmsg(vpninfo->tun_fd, NULL, &strb, &flags);
609                                 if (ret < 0)
610                                         break;
611
612                                 if (!(ret & MOREDATA))
613                                         moredata = 0;
614                                 out_pkt->len = strb.len;
615                         } else
616 #endif /* __sun__ ... */
617                         {
618                                 len = read(vpninfo->tun_fd, out_pkt->data - AF_PREFIX_SIZE, len + AF_PREFIX_SIZE);
619                                 if (len <= 0)
620                                         break;
621
622                                 out_pkt->len = len - AF_PREFIX_SIZE;
623                         }
624
625                         queue_packet(&vpninfo->outgoing_queue, out_pkt);
626                         out_pkt = NULL;
627
628                         work_done = 1;
629                         vpninfo->outgoing_qlen++;
630                         if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
631                                 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
632                                 break;
633                         }
634                 }
635         } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
636                 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
637         }
638
639         /* The kernel returns -ENOMEM when the queue is full, so theoretically
640            we could handle that and retry... but it doesn't let us poll() for
641            the no-longer-full situation, so let's not bother. */
642         while (vpninfo->incoming_queue) {
643                 struct pkt *this = vpninfo->incoming_queue;
644                 unsigned char *data = this->data;
645                 int len = this->len;
646
647 #ifdef TUN_HAS_AF_PREFIX
648                 if (!vpninfo->script_tun) {
649                         struct ip *iph = (void *)data;
650                         int type;
651
652                         if (iph->ip_v == 6)
653                                 type = AF_INET6;
654                         else if (iph->ip_v == 4)
655                                 type = AF_INET;
656                         else {
657                                 static int complained = 0;
658                                 if (!complained) {
659                                         complained = 1;
660                                         vpn_progress(vpninfo, PRG_ERR,
661                                                      _("Unknown packet (len %d) received: %02x %02x %02x %02x...\n"),
662                                                      len, data[0], data[1], data[2], data[3]);
663                                 }
664                                 free(this);
665                                 continue;
666                         }
667                         data -= 4;
668                         len += 4;
669                         *(int *)data = htonl(type);
670                 }
671 #endif
672                 vpninfo->incoming_queue = this->next;
673
674                 if (write(vpninfo->tun_fd, data, len) < 0) {
675                         /* Handle death of "script" socket */
676                         if (vpninfo->script_tun && errno == ENOTCONN) {
677                                 vpninfo->quit_reason = "Client connection terminated";
678                                 return 1;
679                         }
680                         vpn_progress(vpninfo, PRG_ERR,
681                                      _("Failed to write incoming packet: %s\n"),
682                                      strerror(errno));
683                 }
684                 free(this);
685         }
686         /* Work is not done if we just got rid of packets off the queue */
687         return work_done;
688 }
689
690 void shutdown_tun(struct openconnect_info *vpninfo)
691 {       
692         if (vpninfo->script_tun) {
693                 kill(vpninfo->script_tun, SIGHUP);
694         } else {
695                 if (vpninfo->vpnc_script) {
696                         setenv("reason", "disconnect", 1);
697                         if (system(vpninfo->vpnc_script) == -1) {
698                                 vpn_progress(vpninfo, PRG_ERR,
699                                              _("Failed to spawn script '%s': %s\n"),
700                                              vpninfo->vpnc_script,
701                                              strerror(errno));
702                         }
703                 }
704 #ifdef __sun__
705                 close(vpninfo->ip_fd);
706                 vpninfo->ip_fd = -1;
707 #endif
708         }
709
710         close(vpninfo->tun_fd);
711         vpninfo->tun_fd = -1;
712 }