/
tun.c
807 lines (696 loc) · 18.4 KB
2
* OpenConnect (SSL + DTLS) VPN client
4
* Copyright © 2008-2012 Intel Corporation.
6
7
8
* Author: David Woodhouse <dwmw2@infradead.org>
*
* 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.
12
* This program is distributed in the hope that it will be useful, but
13
14
15
16
17
18
19
20
21
22
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to:
*
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
23
24
*/
25
26
#include <sys/types.h>
#include <sys/stat.h>
27
#include <sys/socket.h>
28
#include <sys/ioctl.h>
29
#include <sys/wait.h>
30
#include <string.h>
31
#include <signal.h>
32
#include <fcntl.h>
33
#include <unistd.h>
34
#include <netdb.h>
35
#include <netinet/in_systm.h>
36
#include <netinet/in.h>
37
#include <netinet/ip.h>
38
#include <net/if.h>
39
#include <arpa/inet.h>
40
#include <errno.h>
41
#include <ctype.h>
42
43
#include <stdio.h>
#include <stdlib.h>
44
45
46
#if defined(__sun__)
#include <stropts.h>
#include <sys/sockio.h>
47
48
49
50
#include <net/if_tun.h>
#ifndef TUNNEWPPA
#error "Install TAP driver from http://www.whiteboard.ne.jp/~admin2/tuntap/"
#endif
53
#include "openconnect-internal.h"
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* If an if_tun.h include file was found anywhere (by the Makefile), it's
* included. Else, we end up assuming that we have BSD-style devices such
* as /dev/tun0 etc.
*/
#ifdef IF_TUN_HDR
#include IF_TUN_HDR
#endif
/*
* The OS X tun/tap driver doesn't provide a header file; you're expected
* to define this for yourself.
*/
#ifdef __APPLE__
#define TUNSIFHEAD _IOW('t', 96, int)
#endif
/*
* OpenBSD always puts the protocol family prefix onto packets. Other
* systems let us enable that with the TUNSIFHEAD ioctl, and some of them
* (e.g. FreeBSD) _need_ it otherwise they'll interpret IPv6 packets as IPv4.
*/
#if defined(__OpenBSD__) || defined(TUNSIFHEAD)
78
#define TUN_HAS_AF_PREFIX 1
79
80
#endif
81
static int set_tun_mtu(struct openconnect_info *vpninfo)
83
#ifndef __sun__ /* We don't know how to do this on Solaris */
84
struct ifreq ifr;
85
int net_fd;
87
88
net_fd = socket(PF_INET, SOCK_DGRAM, 0);
if (net_fd < 0) {
89
perror(_("open net"));
90
return -EINVAL;
93
memset(&ifr, 0, sizeof(ifr));
94
strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
95
ifr.ifr_mtu = vpninfo->actual_mtu;
97
if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
98
perror(_("SIOCSIFMTU"));
100
close(net_fd);
102
103
return 0;
}
106
107
108
109
110
111
112
static int setenv_int(const char *opt, int value)
{
char buf[16];
sprintf(buf, "%d", value);
return setenv(opt, buf, 1);
}
113
114
115
116
117
118
119
120
121
122
123
static int netmasklen(struct in_addr addr)
{
int masklen;
for (masklen = 0; masklen < 32; masklen++) {
if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
break;
}
return 32 - masklen;
}
124
static int process_split_xxclude(struct openconnect_info *vpninfo,
125
int include, const char *route, int *v4_incs,
126
int *v6_incs)
127
128
{
struct in_addr addr;
129
const char *in_ex = include?"IN":"EX";
130
131
132
133
134
135
char envname[80];
char *slash;
slash = strchr(route, '/');
if (!slash) {
badinc:
136
137
138
139
140
141
142
143
if (include)
vpn_progress(vpninfo, PRG_ERR,
_("Discard bad split include: \"%s\"\n"),
route);
else
vpn_progress(vpninfo, PRG_ERR,
_("Discard bad split exclude: \"%s\"\n"),
route);
144
145
146
147
return -EINVAL;
}
*slash = 0;
148
149
150
151
152
153
154
155
156
157
158
159
160
161
if (strchr(route, ':')) {
snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_ADDR", in_ex,
*v6_incs);
setenv(envname, route, 1);
snprintf(envname, 79, "CISCO_IPV6_SPLIT_%sC_%d_MASKLEN", in_ex,
*v6_incs);
setenv(envname, slash+1, 1);
(*v6_incs)++;
return 0;
}
162
163
164
165
166
167
if (!inet_aton(route, &addr)) {
*slash = '/';
goto badinc;
}
envname[79] = 0;
168
snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *v4_incs);
169
170
171
172
173
174
175
176
setenv(envname, route, 1);
/* Put it back how we found it */
*slash = '/';
if (!inet_aton(slash+1, &addr))
goto badinc;
177
snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *v4_incs);
178
179
setenv(envname, slash+1, 1);
180
snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *v4_incs);
181
setenv_int(envname, netmasklen(addr));
183
(*v4_incs)++;
184
185
186
return 0;
}
187
static int appendenv(const char *opt, const char *new)
188
189
190
191
192
193
194
195
196
197
198
199
200
{
char buf[1024];
char *old = getenv(opt);
buf[1023] = 0;
if (old)
snprintf(buf, 1023, "%s %s", old, new);
else
snprintf(buf, 1023, "%s", new);
return setenv(opt, buf, 1);
}
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
static void setenv_cstp_opts(struct openconnect_info *vpninfo)
{
char *env_buf;
int buflen = 0;
int bufofs = 0;
struct vpn_option *opt;
for (opt = vpninfo->cstp_options; opt; opt = opt->next)
buflen += 2 + strlen(opt->option) + strlen(opt->value);
env_buf = malloc(buflen + 1);
if (!env_buf)
return;
env_buf[buflen] = 0;
for (opt = vpninfo->cstp_options; opt; opt = opt->next)
bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
"%s=%s\n", opt->option, opt->value);
setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
free(env_buf);
}
225
226
227
228
229
static void set_banner(struct openconnect_info *vpninfo)
{
char *banner, *q;
const char *p;
230
if (!vpninfo->banner || !(banner = malloc(strlen(vpninfo->banner)+1))) {
231
232
233
234
235
236
237
unsetenv("CISCO_BANNER");
return;
}
p = vpninfo->banner;
q = banner;
while (*p) {
238
239
if (*p == '%' && isxdigit((int)(unsigned char)p[1]) &&
isxdigit((int)(unsigned char)p[2])) {
240
241
242
243
244
245
246
247
248
249
250
*(q++) = unhex(p + 1);
p += 3;
} else
*(q++) = *(p++);
}
*q = 0;
setenv("CISCO_BANNER", banner, 1);
free(banner);
}
251
static void set_script_env(struct openconnect_info *vpninfo)
253
254
255
256
257
char host[80];
int ret = getnameinfo(vpninfo->peer_addr, vpninfo->peer_addrlen, host,
sizeof(host), NULL, 0, NI_NUMERICHOST);
if (!ret)
setenv("VPNGATEWAY", host, 1);
259
set_banner(vpninfo);
260
unsetenv("CISCO_SPLIT_INC");
261
unsetenv("CISCO_SPLIT_EXC");
263
setenv_int("INTERNAL_IP4_MTU", vpninfo->actual_mtu);
265
266
if (vpninfo->vpn_addr) {
setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
if (vpninfo->vpn_netmask) {
struct in_addr addr;
struct in_addr mask;
if (inet_aton(vpninfo->vpn_addr, &addr) &&
inet_aton(vpninfo->vpn_netmask, &mask)) {
char *netaddr;
addr.s_addr &= mask.s_addr;
netaddr = inet_ntoa(addr);
setenv("INTERNAL_IP4_NETADDR", netaddr, 1);
setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
setenv_int("INTERNAL_IP4_NETMASKLEN", netmasklen(mask));
}
}
283
284
285
286
287
}
if (vpninfo->vpn_addr6) {
setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
}
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
if (vpninfo->vpn_dns[0])
setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
else
unsetenv("INTERNAL_IP4_DNS");
if (vpninfo->vpn_dns[1])
appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
if (vpninfo->vpn_dns[2])
appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
if (vpninfo->vpn_nbns[0])
setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
else
unsetenv("INTERNAL_IP4_NBNS");
if (vpninfo->vpn_nbns[1])
appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
if (vpninfo->vpn_nbns[2])
appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
if (vpninfo->vpn_domain)
setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
else unsetenv ("CISCO_DEF_DOMAIN");
311
312
313
if (vpninfo->vpn_proxy_pac)
setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
if (vpninfo->split_dns) {
char *list;
int len = 0;
struct split_include *dns = vpninfo->split_dns;
while (dns) {
len += strlen(dns->route) + 1;
dns = dns->next;
}
list = malloc(len);
if (list) {
char *p = list;
dns = vpninfo->split_dns;
while (1) {
strcpy(p, dns->route);
p += strlen(p);
dns = dns->next;
if (!dns)
break;
334
*(p++) = ',';
335
336
337
338
339
}
setenv("CISCO_SPLIT_DNS", list, 1);
free(list);
}
}
340
341
342
if (vpninfo->split_includes) {
struct split_include *this = vpninfo->split_includes;
int nr_split_includes = 0;
343
int nr_v6_split_includes = 0;
344
345
while (this) {
346
process_split_xxclude(vpninfo, 1, this->route,
347
348
&nr_split_includes,
&nr_v6_split_includes);
349
350
this = this->next;
}
351
352
353
354
if (nr_split_includes)
setenv_int("CISCO_SPLIT_INC", nr_split_includes);
if (nr_v6_split_includes)
setenv_int("CISCO_IPV6_SPLIT_INC", nr_v6_split_includes);
355
}
356
357
358
if (vpninfo->split_excludes) {
struct split_include *this = vpninfo->split_excludes;
int nr_split_excludes = 0;
359
int nr_v6_split_excludes = 0;
361
while (this) {
362
process_split_xxclude(vpninfo, 0, this->route,
363
364
&nr_split_excludes,
&nr_v6_split_excludes);
365
366
this = this->next;
}
367
368
369
370
if (nr_split_excludes)
setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
if (nr_v6_split_excludes)
setenv_int("CISCO_IPV6_SPLIT_EXC", nr_v6_split_excludes);
372
setenv_cstp_opts(vpninfo);
375
int script_config_tun(struct openconnect_info *vpninfo, const char *reason)
377
378
int ret;
379
if (!vpninfo->vpnc_script || vpninfo->script_tun)
380
381
382
return 0;
setenv("reason", reason, 1);
383
384
ret = system(vpninfo->vpnc_script);
if (ret == -1) {
385
int e = errno;
386
vpn_progress(vpninfo, PRG_ERR,
387
388
_("Failed to spawn script '%s' for %s: %s\n"),
vpninfo->vpnc_script, reason, strerror(e));
389
390
return -e;
}
391
392
393
394
395
396
397
398
399
400
401
402
403
if (!WIFEXITED(ret)) {
vpn_progress(vpninfo, PRG_ERR,
_("Script '%s' exited abnormally (%x)\n"),
vpninfo->vpnc_script, ret);
return -EIO;
}
ret = WEXITSTATUS(ret);
if (ret) {
vpn_progress(vpninfo, PRG_ERR,
_("Script '%s' returned error %d\n"),
vpninfo->vpnc_script, ret);
return -EIO;
}
404
return 0;
407
408
409
410
411
412
413
414
#ifdef __sun__
static int link_proto(int unit_nr, const char *devname, uint64_t flags)
{
int ip_fd, mux_id, tun2_fd;
struct lifreq ifr;
tun2_fd = open("/dev/tun", O_RDWR);
if (tun2_fd < 0) {
415
perror(_("Could not open /dev/tun for plumbing"));
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
return -EIO;
}
if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
perror(_("Can't push IP"));
close(tun2_fd);
return -EIO;
}
sprintf(ifr.lifr_name, "tun%d", unit_nr);
ifr.lifr_ppa = unit_nr;
ifr.lifr_flags = flags;
if (ioctl(tun2_fd, SIOCSLIFNAME, &ifr) < 0) {
perror(_("Can't set ifname"));
close(tun2_fd);
return -1;
}
ip_fd = open(devname, O_RDWR);
if (ip_fd < 0) {
fprintf(stderr, _("Can't open %s: %s"), devname,
strerror(errno));
close(tun2_fd);
return -1;
}
mux_id = ioctl(ip_fd, I_LINK, tun2_fd);
if (mux_id < 0) {
fprintf(stderr, _("Can't plumb %s for IPv%d: %s\n"),
ifr.lifr_name, (flags == IFF_IPV4) ? 4 : 6,
strerror(errno));
close(tun2_fd);
close(ip_fd);
return -1;
}
close(tun2_fd);
return ip_fd;
}
#endif
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#ifdef SIOCIFCREATE
static int bsd_open_tun(char *tun_name)
{
int fd;
int s;
struct ifreq ifr;
fd = open(tun_name, O_RDWR);
if (fd >= 0) {
return fd;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
return -1;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, tun_name + 5, sizeof(ifr.ifr_name) - 1);
if (!ioctl(s, SIOCIFCREATE, &ifr))
fd = open(tun_name, O_RDWR);
close(s);
}
return fd;
}
#else
#define bsd_open_tun(tun_name) open(tun_name, O_RDWR)
#endif
486
487
static int os_setup_tun(struct openconnect_info *vpninfo)
{
488
int tun_fd = -1;
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#ifdef IFF_TUN /* Linux */
struct ifreq ifr;
int tunerr;
tun_fd = open("/dev/net/tun", O_RDWR);
if (tun_fd < 0) {
/* Android has /dev/tun instead of /dev/net/tun
Since other systems might have too, just try it
as a fallback instead of using ifdef __ANDROID__ */
tunerr = errno;
tun_fd = open("/dev/tun", O_RDWR);
}
if (tun_fd < 0) {
/* If the error on /dev/tun is ENOENT, that's boring.
Use the error we got on /dev/net/tun instead */
505
if (errno != ENOENT)
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
tunerr = errno;
vpn_progress(vpninfo, PRG_ERR,
_("Failed to open tun device: %s\n"),
strerror(tunerr));
exit(1);
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if (vpninfo->ifname)
strncpy(ifr.ifr_name, vpninfo->ifname,
sizeof(ifr.ifr_name) - 1);
if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("TUNSETIFF failed: %s\n"),
strerror(errno));
exit(1);
}
if (!vpninfo->ifname)
vpninfo->ifname = strdup(ifr.ifr_name);
#elif defined (__sun__)
static char tun_name[80];
int unit_nr;
tun_fd = open("/dev/tun", O_RDWR);
if (tun_fd < 0) {
perror(_("open /dev/tun"));
return -EIO;
}
unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
if (unit_nr < 0) {
perror(_("Failed to create new tun"));
close(tun_fd);
return -EIO;
}
if (ioctl(tun_fd, I_SRDOPT, RMSGD) < 0) {
perror(_("Failed to put tun file descriptor into message-discard mode"));
close(tun_fd);
return -EIO;
}
sprintf(tun_name, "tun%d", unit_nr);
vpninfo->ifname = strdup(tun_name);
vpninfo->ip_fd = link_proto(unit_nr, "/dev/udp", IFF_IPV4);
if (vpninfo->ip_fd < 0) {
close(tun_fd);
return -EIO;
}
if (vpninfo->vpn_addr6) {
vpninfo->ip6_fd = link_proto(unit_nr, "/dev/udp6", IFF_IPV6);
if (vpninfo->ip6_fd < 0) {
close(tun_fd);
close(vpninfo->ip_fd);
vpninfo->ip_fd = -1;
return -EIO;
}
} else
vpninfo->ip6_fd = -1;
#else /* BSD et al have /dev/tun$x devices */
static char tun_name[80];
int i;
572
573
574
575
576
577
578
579
580
581
582
583
584
if (vpninfo->ifname) {
char *endp = NULL;
if (strncmp(vpninfo->ifname, "tun", 3) ||
((void)strtol(vpninfo->ifname + 3, &endp, 10), !endp) ||
*endp) {
vpn_progress(vpninfo, PRG_ERR,
_("Invalid interface name '%s'; must match 'tun%%d'\n"),
vpninfo->ifname);
return -EINVAL;
}
snprintf(tun_name, sizeof(tun_name),
"/dev/%s", vpninfo->ifname);
585
tun_fd = bsd_open_tun(tun_name);
586
587
588
589
590
591
592
if (tun_fd < 0) {
int err = errno;
vpn_progress(vpninfo, PRG_ERR,
_("Cannot open '%s': %s\n"),
tun_name, strerror(err));
return -EINVAL;
}
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
}
#ifdef HAVE_FDEVNAME_R
/* We don't have to iterate over the possible devices; on FreeBSD
at least, opening /dev/tun will give us the next available
device. */
if (tun_fd < 0) {
tun_fd = open("/dev/tun", O_RDWR);
if (tun_fd >= 0) {
if (!fdevname_r(tun_fd, tun_name, sizeof(tun_name)) ||
strncmp(tun_name, "tun", 3)) {
close(tun_fd);
tun_fd = -1;
} else
vpninfo->ifname = strdup(tun_name);
}
}
#endif
if (tun_fd < 0) {
611
612
for (i = 0; i < 255; i++) {
sprintf(tun_name, "/dev/tun%d", i);
613
tun_fd = bsd_open_tun(tun_name);
614
615
616
617
618
619
620
621
if (tun_fd >= 0)
break;
}
if (tun_fd < 0) {
perror(_("open tun"));
exit(1);
}
vpninfo->ifname = strdup(tun_name + 5);
622
623
624
625
626
627
628
629
630
631
632
633
}
#ifdef TUNSIFHEAD
i = 1;
if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
perror(_("TUNSIFHEAD"));
exit(1);
}
#endif
#endif
return tun_fd;
}
634
/* Set up a tuntap device. */
635
int setup_tun(struct openconnect_info *vpninfo)
636
637
638
{
int tun_fd;
639
640
set_script_env(vpninfo);
641
642
643
644
645
if (vpninfo->script_tun) {
pid_t child;
int fds[2];
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
646
perror(_("socketpair"));
647
648
649
650
651
exit(1);
}
tun_fd = fds[0];
child = fork();
if (child < 0) {
652
perror(_("fork"));
653
654
exit(1);
} else if (!child) {
655
656
if (setpgid(0, getpid()) < 0)
perror(_("setpgid"));
657
658
659
close(tun_fd);
setenv_int("VPNFD", fds[1]);
execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
660
perror(_("execl"));
661
662
exit(1);
}
663
close(fds[1]);
664
vpninfo->script_tun = child;
665
vpninfo->ifname = strdup(_("(script)"));
667
script_config_tun(vpninfo, "pre-init");
669
670
671
tun_fd = os_setup_tun(vpninfo);
if (tun_fd < 0)
return tun_fd;
673
674
675
676
677
setenv("TUNDEV", vpninfo->ifname, 1);
script_config_tun(vpninfo, "connect");
/* Ancient vpnc-scripts might not get this right */
set_tun_mtu(vpninfo);
680
681
fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
682
vpninfo->tun_fd = tun_fd;
684
685
686
687
if (vpninfo->select_nfds <= tun_fd)
vpninfo->select_nfds = tun_fd + 1;
FD_SET(tun_fd, &vpninfo->select_rfds);
689
690
fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
691
692
693
return 0;
}
694
695
static struct pkt *out_pkt;
696
int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
697
698
{
int work_done = 0;
699
700
701
702
703
704
int prefix_size = 0;
#ifdef TUN_HAS_AF_PREFIX
if (!vpninfo->script_tun)
prefix_size = sizeof(int);
#endif
706
if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
708
int len = vpninfo->actual_mtu;
709
710
711
712
713
714
715
716
717
if (!out_pkt) {
out_pkt = malloc(sizeof(struct pkt) + len);
if (!out_pkt) {
vpn_progress(vpninfo, PRG_ERR, "Allocation failed\n");
break;
}
}
718
719
720
721
len = read(vpninfo->tun_fd, out_pkt->data - prefix_size, len + prefix_size);
if (len <= prefix_size)
break;
out_pkt->len = len - prefix_size;
722
723
724
queue_packet(&vpninfo->outgoing_queue, out_pkt);
out_pkt = NULL;
725
726
727
work_done = 1;
vpninfo->outgoing_qlen++;
728
if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
729
730
731
732
FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
break;
}
}
733
} else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
734
FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
735
736
}
737
738
739
/* The kernel returns -ENOMEM when the queue is full, so theoretically
we could handle that and retry... but it doesn't let us poll() for
the no-longer-full situation, so let's not bother. */
740
741
while (vpninfo->incoming_queue) {
struct pkt *this = vpninfo->incoming_queue;
742
743
744
unsigned char *data = this->data;
int len = this->len;
745
#ifdef TUN_HAS_AF_PREFIX
746
747
748
749
750
751
752
753
754
755
756
757
if (!vpninfo->script_tun) {
struct ip *iph = (void *)data;
int type;
if (iph->ip_v == 6)
type = AF_INET6;
else if (iph->ip_v == 4)
type = AF_INET;
else {
static int complained = 0;
if (!complained) {
complained = 1;
758
vpn_progress(vpninfo, PRG_ERR,
759
760
_("Unknown packet (len %d) received: %02x %02x %02x %02x...\n"),
len, data[0], data[1], data[2], data[3]);
761
762
763
}
free(this);
continue;
765
766
767
data -= 4;
len += 4;
*(int *)data = htonl(type);
770
vpninfo->incoming_queue = this->next;
772
773
774
775
776
777
778
779
780
if (write(vpninfo->tun_fd, data, len) < 0) {
/* Handle death of "script" socket */
if (vpninfo->script_tun && errno == ENOTCONN) {
vpninfo->quit_reason = "Client connection terminated";
return 1;
}
vpn_progress(vpninfo, PRG_ERR,
_("Failed to write incoming packet: %s\n"),
strerror(errno));
782
free(this);
783
784
785
786
}
/* Work is not done if we just got rid of packets off the queue */
return work_done;
}
787
788
789
790
void shutdown_tun(struct openconnect_info *vpninfo)
{
if (vpninfo->script_tun) {
791
792
/* nuke the whole process group */
kill(-vpninfo->script_tun, SIGHUP);
793
} else {
794
script_config_tun(vpninfo, "disconnect");
795
796
797
#ifdef __sun__
close(vpninfo->ip_fd);
vpninfo->ip_fd = -1;
798
799
800
801
if (vpninfo->ip6_fd != -1) {
close(vpninfo->ip6_fd);
vpninfo->ip6_fd = -1;
}
802
#endif
803
804
805
806
807
}
close(vpninfo->tun_fd);
vpninfo->tun_fd = -1;
}