Add openconnect_vpninfo_new_with_cbdata() function to ease C++ integration
[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                                  char *in_ex, char *route, int *v4_incs,
147                                  int *v6_incs)
148 {
149         struct in_addr addr;
150         char envname[80];
151         char *slash;
152
153         slash = strchr(route, '/');
154         if (!slash) {
155         badinc:
156                 vpn_progress(vpninfo, PRG_ERR,
157                                   "Discard bad split %sclude: \"%s\"\n",
158                                   in_ex, route);
159                 return -EINVAL;
160         }
161
162         *slash = 0;
163
164         if (strchr(route, ':')) {
165                 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_ADDR", in_ex,
166                          *v6_incs);
167                 setenv(envname, route, 1);
168
169                 snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_MASKLEN", in_ex,
170                          *v6_incs);
171                 setenv(envname, slash+1, 1);
172
173                 (*v6_incs)++;
174                 return 0;
175         }
176                 
177         if (!inet_aton(route, &addr)) {
178                 *slash = '/';
179                 goto badinc;
180         }
181
182         envname[79] = 0;
183         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *v4_incs);
184         setenv(envname, route, 1);
185
186         /* Put it back how we found it */
187         *slash = '/';
188
189         if (!inet_aton(slash+1, &addr))
190                 goto badinc;
191
192         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *v4_incs);
193         setenv(envname, slash+1, 1);
194
195         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *v4_incs);
196         setenv_int(envname, netmasklen(addr));
197
198         (*v4_incs)++;
199         return 0;
200 }
201
202 static int appendenv(const char *opt, const char *new)
203 {
204         char buf[1024];
205         char *old = getenv(opt);
206
207         buf[1023] = 0;
208         if (old)
209                 snprintf(buf, 1023, "%s %s", old, new);
210         else
211                 snprintf(buf, 1023, "%s", new);
212
213         return setenv(opt, buf, 1);
214 }
215
216 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
217 {
218         char *env_buf;
219         int buflen = 0;
220         int bufofs = 0;
221         struct vpn_option *opt;
222
223         for (opt = vpninfo->cstp_options; opt; opt = opt->next)
224                 buflen += 2 + strlen(opt->option) + strlen(opt->value);
225
226         env_buf = malloc(buflen + 1);
227         if (!env_buf)
228                 return;
229
230         env_buf[buflen] = 0;
231
232         for (opt = vpninfo->cstp_options; opt; opt = opt->next)
233                 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
234                                    "%s=%s\n", opt->option, opt->value);
235
236         setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
237         free(env_buf);
238 }
239
240 static void set_banner(struct openconnect_info *vpninfo)
241 {
242         char *banner, *q;
243         const char *p;
244
245         if (!vpninfo->banner || !(banner = malloc(strlen(vpninfo->banner)))) {
246                 unsetenv("CISCO_BANNER");
247                 return;
248         }
249         p = vpninfo->banner;
250         q = banner;
251         
252         while (*p) {
253                 if (*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
254                         *(q++) = unhex(p + 1);
255                         p += 3;
256                 } else 
257                         *(q++) = *(p++);
258         }
259         *q = 0;
260         setenv("CISCO_BANNER", banner, 1);
261
262         free(banner);
263 }       
264
265 static void set_script_env(struct openconnect_info *vpninfo)
266 {
267         char host[80];
268         int ret = getnameinfo(vpninfo->peer_addr, vpninfo->peer_addrlen, host,
269                               sizeof(host), NULL, 0, NI_NUMERICHOST);
270         if (!ret)
271                 setenv("VPNGATEWAY", host, 1);
272
273         setenv("reason", "connect", 1);
274         set_banner(vpninfo);
275         unsetenv("CISCO_SPLIT_INC");
276         unsetenv("CISCO_SPLIT_EXC");
277
278         setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
279
280         if (vpninfo->vpn_addr) {
281                 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
282                 if (vpninfo->vpn_netmask) {
283                         struct in_addr addr;
284                         struct in_addr mask;
285
286                         if (inet_aton(vpninfo->vpn_addr, &addr) &&
287                             inet_aton(vpninfo->vpn_netmask, &mask)) {
288                                 char *netaddr;
289
290                                 addr.s_addr &= mask.s_addr;
291                                 netaddr = inet_ntoa(addr);
292
293                                 setenv("INTERNAL_IP4_NETADDR", netaddr, 1);
294                                 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
295                                 setenv_int("INTERNAL_IP4_NETMASKLEN", netmasklen(mask));
296                         }
297                 }
298         }
299         if (vpninfo->vpn_addr6) {
300                 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
301                 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
302         }
303
304         if (vpninfo->vpn_dns[0])
305                 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
306         else
307                 unsetenv("INTERNAL_IP4_DNS");
308         if (vpninfo->vpn_dns[1])
309                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
310         if (vpninfo->vpn_dns[2])
311                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
312
313         if (vpninfo->vpn_nbns[0])
314                 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
315         else
316                 unsetenv("INTERNAL_IP4_NBNS");
317         if (vpninfo->vpn_nbns[1])
318                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
319         if (vpninfo->vpn_nbns[2])
320                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
321
322         if (vpninfo->vpn_domain)
323                 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
324         else unsetenv ("CISCO_DEF_DOMAIN");
325
326         if (vpninfo->vpn_proxy_pac)
327                 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
328
329         if (vpninfo->split_includes) {
330                 struct split_include *this = vpninfo->split_includes;
331                 int nr_split_includes = 0;
332                 int nr_v6_split_includes = 0;
333
334                 while (this) {
335                         process_split_xxclude(vpninfo, "IN", this->route,
336                                               &nr_split_includes,
337                                               &nr_v6_split_includes);
338                         this = this->next;
339                 }
340                 if (nr_split_includes)
341                         setenv_int("CISCO_SPLIT_INC", nr_split_includes);
342                 if (nr_v6_split_includes)
343                         setenv_int("CISCO_IPV6_SPLIT_INC", nr_v6_split_includes);
344         }
345         if (vpninfo->split_excludes) {
346                 struct split_include *this = vpninfo->split_excludes;
347                 int nr_split_excludes = 0;
348                 int nr_v6_split_excludes = 0;
349
350                 while (this) {
351                         process_split_xxclude(vpninfo, "EX", this->route,
352                                               &nr_split_excludes,
353                                               &nr_v6_split_excludes);
354                         this = this->next;
355                 }
356                 if (nr_split_excludes)
357                         setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
358                 if (nr_v6_split_excludes)
359                         setenv_int("CISCO_IPV6_SPLIT_EXC", nr_v6_split_excludes);
360         }
361         setenv_cstp_opts(vpninfo);
362 }
363
364 static int script_config_tun(struct openconnect_info *vpninfo)
365 {
366         if (system(vpninfo->vpnc_script)) {
367                 int e = errno;
368                 vpn_progress(vpninfo, PRG_ERR,
369                                   "Failed to spawn script '%s': %s\n",
370                                   vpninfo->vpnc_script, strerror(e));
371                 return -e;
372         }
373         return 0;
374 }
375
376 void script_reconnect (struct openconnect_info *vpninfo)
377 {
378         setenv("reason", "reconnect", 1);
379         script_config_tun(vpninfo);
380 }
381
382 /* Set up a tuntap device. */
383 int setup_tun(struct openconnect_info *vpninfo)
384 {
385         int tun_fd;
386
387         set_script_env(vpninfo);
388
389         if (vpninfo->script_tun) {
390                 pid_t child;
391                 int fds[2];
392
393                 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
394                         perror("socketpair");
395                         exit(1);
396                 }
397                 tun_fd = fds[0];
398                 child = fork();
399                 if (child < 0) {
400                         perror("fork");
401                         exit(1);
402                 } else if (!child) {
403                         close(tun_fd);
404                         setenv_int("VPNFD", fds[1]);
405                         execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
406                         perror("execl");
407                         exit(1);
408                 }
409                 close(fds[1]);
410                 vpninfo->script_tun = child;
411                 vpninfo->ifname = "(script)";
412         } else {
413 #ifdef IFF_TUN /* Linux */
414                 struct ifreq ifr;
415                 int tunerr;
416
417                 tun_fd = open("/dev/net/tun", O_RDWR);
418                 if (tun_fd < 0) {
419                         /* Android has /dev/tun instead of /dev/net/tun
420                            Since other systems might have too, just try it
421                            as a fallback instead of using ifdef __ANDROID__ */
422                         tunerr = errno;
423                         tun_fd = open("/dev/tun", O_RDWR);
424                 }
425                 if (tun_fd < 0) {
426                         /* If the error on /dev/tun is ENOENT, that's boring.
427                            Use the error we got on /dev/net/tun instead */
428                         if (errno != -ENOENT)
429                                 tunerr = errno;
430
431                         vpn_progress(vpninfo, PRG_ERR,
432                                           "Failed to open tun device: %s\n",
433                                           strerror(tunerr));
434                         exit(1);
435                 }
436                 memset(&ifr, 0, sizeof(ifr));
437                 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
438                 if (vpninfo->ifname)
439                         strncpy(ifr.ifr_name, vpninfo->ifname,
440                                 sizeof(ifr.ifr_name) - 1);
441                 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
442                         vpn_progress(vpninfo, PRG_ERR,
443                                           "TUNSETIFF failed: %s\n",
444                                           strerror(errno));
445                         exit(1);
446                 }
447                 if (!vpninfo->ifname)
448                         vpninfo->ifname = strdup(ifr.ifr_name);
449 #elif defined (__sun__)
450                 static char tun_name[80];
451                 int tun2_fd, ip_fd = open("/dev/ip", O_RDWR);
452                 int unit_nr, mux_id;
453                 struct ifreq ifr;
454
455                 if (ip_fd < 0) {
456                         perror("open /dev/ip");
457                         return -EIO;
458                 }
459
460                 tun_fd = open("/dev/tun", O_RDWR);
461                 if (tun_fd < 0) {
462                         perror("open /dev/tun");
463                         close(ip_fd);
464                         return -EIO;
465                 }
466
467                 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
468                 if (unit_nr < 0) {
469                         perror("Failed to create new tun");
470                         close(tun_fd);
471                         close(ip_fd);
472                         return -EIO;
473                 }
474                 
475                 tun2_fd = open("/dev/tun", O_RDWR);
476                 if (tun2_fd < 0) {
477                         perror("open /dev/tun again");
478                         close(tun_fd);
479                         close(ip_fd);
480                         return -EIO;
481                 }
482                 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
483                         perror("Can't push IP");
484                         close(tun2_fd);
485                         close(tun_fd);
486                         close(ip_fd);
487                         return -EIO;
488                 }
489                 if (ioctl(tun2_fd, IF_UNITSEL, &unit_nr) < 0) {
490                         perror("Can't select unit");
491                         close(tun2_fd);
492                         close(tun_fd);
493                         close(ip_fd);
494                         return -EIO;
495                 }
496                 mux_id = ioctl(ip_fd, I_PLINK, tun2_fd);
497                 if (mux_id < 0) {
498                         perror("Can't link tun to IP");
499                         close(tun2_fd);
500                         close(tun_fd);
501                         close(ip_fd);
502                         return -EIO;
503                 }
504                 close(tun2_fd);
505
506                 sprintf(tun_name, "tun%d", unit_nr);
507                 vpninfo->ifname = tun_name;
508
509                 memset(&ifr, 0, sizeof(ifr));
510                 strcpy(ifr.ifr_name, tun_name);
511                 ifr.ifr_ip_muxid = mux_id;
512
513                 if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
514                         perror("Set mux id");
515                         close(tun_fd);
516                         ioctl(ip_fd, I_PUNLINK, mux_id);
517                         close(ip_fd);
518                         return -EIO;
519                 }
520                 /* Solaris tunctl needs this in order to tear it down */
521                 vpn_progress(vpninfo, PRG_DEBUG, "mux id is %d\n", mux_id);
522                 vpninfo->tun_muxid = mux_id;
523                 vpninfo->ip_fd = ip_fd;
524
525 #else /* BSD et al have /dev/tun$x devices */
526                 static char tun_name[80];
527                 int i;
528                 for (i = 0; i < 255; i++) {
529                         sprintf(tun_name, "/dev/tun%d", i);
530                         tun_fd = open(tun_name, O_RDWR);
531                         if (tun_fd >= 0)
532                                 break;
533                 }
534                 if (tun_fd < 0) {
535                         perror("open tun");
536                         exit(1);
537                 }
538                 vpninfo->ifname = tun_name + 5;
539 #ifdef TUNSIFHEAD
540                 i = 1;
541                 if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
542                         perror("TUNSIFHEAD");
543                         exit(1);
544                 }
545 #endif
546 #endif
547                 if (vpninfo->vpnc_script) {
548                         setenv("TUNDEV", vpninfo->ifname, 1);
549                         script_config_tun(vpninfo);
550                         /* We have to set the MTU for ourselves, because the script doesn't */
551                         local_config_tun(vpninfo, 1);
552                 } else
553                         local_config_tun(vpninfo, 0);
554         }
555
556         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
557
558         vpninfo->tun_fd = tun_fd;
559
560         if (vpninfo->select_nfds <= tun_fd)
561                 vpninfo->select_nfds = tun_fd + 1;
562
563         FD_SET(tun_fd, &vpninfo->select_rfds);
564
565         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
566
567         return 0;
568 }
569
570 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
571 {
572         unsigned char buf[2000];
573         int len;
574         int work_done = 0;
575
576         if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
577                 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
578                         unsigned char *pkt = buf;
579 #ifdef TUN_HAS_AF_PREFIX
580                         if (!vpninfo->script_tun) {
581                                 pkt += 4;
582                                 len -= 4;
583                         }
584 #endif
585                         if (queue_new_packet(&vpninfo->outgoing_queue, pkt,
586                                              len))
587                                 break;
588
589                         work_done = 1;
590                         vpninfo->outgoing_qlen++;
591                         if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
592                                 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
593                                 break;
594                         }
595                 }
596         } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
597                 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
598         }
599
600         /* The kernel returns -ENOMEM when the queue is full, so theoretically
601            we could handle that and retry... but it doesn't let us poll() for
602            the no-longer-full situation, so let's not bother. */
603         while (vpninfo->incoming_queue) {
604                 struct pkt *this = vpninfo->incoming_queue;
605                 unsigned char *data = this->data;
606                 int len = this->len;
607
608 #ifdef TUN_HAS_AF_PREFIX
609                 if (!vpninfo->script_tun) {
610                         struct ip *iph = (void *)data;
611                         int type;
612
613                         if (iph->ip_v == 6)
614                                 type = AF_INET6;
615                         else if (iph->ip_v == 4)
616                                 type = AF_INET;
617                         else {
618                                 static int complained = 0;
619                                 if (!complained) {
620                                         complained = 1;
621                                         vpn_progress(vpninfo, PRG_ERR,
622                                                           "Unknown packet (len %d) received: %02x %02x %02x %02x...\n",
623                                                           len, data[0], data[1], data[2], data[3]);
624                                 }
625                                 free(this);
626                                 continue;
627                         }
628                         data -= 4;
629                         len += 4;
630                         *(int *)data = htonl(type);
631                 }
632 #endif
633                 vpninfo->incoming_queue = this->next;
634
635                 if (write(vpninfo->tun_fd, data, len) < 0 &&
636                     errno == ENOTCONN) {
637                         vpninfo->quit_reason = "Client connection terminated";
638                         return 1;
639                 }
640                 free(this);
641         }
642         /* Work is not done if we just got rid of packets off the queue */
643         return work_done;
644 }
645
646 void shutdown_tun(struct openconnect_info *vpninfo)
647 {       
648         if (vpninfo->script_tun) {
649                 kill(vpninfo->script_tun, SIGHUP);
650         } else {
651                 if (vpninfo->vpnc_script) {
652                         setenv("reason", "disconnect", 1);
653                         if (system(vpninfo->vpnc_script) == -1) {
654                                 vpn_progress(vpninfo, PRG_ERR,
655                                                   "Failed to spawn script '%s': %s\n",
656                                                   vpninfo->vpnc_script,
657                                                   strerror(errno));
658                         }
659                 }
660 #ifdef __sun__
661                 if (ioctl(vpninfo->ip_fd, I_PUNLINK, vpninfo->tun_muxid) < 0)
662                         perror("ioctl(I_PUNLINK)");
663
664                 close(vpninfo->ip_fd);
665                 vpninfo->ip_fd = -1;
666 #endif
667         }
668
669         close(vpninfo->tun_fd);
670         vpninfo->tun_fd = -1;
671 }