-
Notifications
You must be signed in to change notification settings - Fork 3k
/
tun.c
5405 lines (4722 loc) · 136 KB
/
tun.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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
457
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* OpenVPN -- An application to securely tunnel IP networks
* over a single TCP/UDP port, with support for SSL/TLS-based
* session authentication and key exchange,
* packet encryption, packet authentication, and
* packet compression.
*
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Support routines for configuring and accessing TUN/TAP
* virtual network adapters.
*
* This file is based on the TUN/TAP driver interface routines
* from VTun by Maxim Krasnyansky <max_mk@yahoo.com>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_MSC_VER)
#include "config-msvc.h"
#endif
#include "syshead.h"
#include "tun.h"
#include "fdmisc.h"
#include "common.h"
#include "misc.h"
#include "socket.h"
#include "manage.h"
#include "route.h"
#include "win32.h"
#include "memdbg.h"
#ifdef WIN32
/* #define SIMULATE_DHCP_FAILED */ /* simulate bad DHCP negotiation */
#define NI_TEST_FIRST (1<<0)
#define NI_IP_NETMASK (1<<1)
#define NI_OPTIONS (1<<2)
static void netsh_ifconfig (const struct tuntap_options *to,
const char *flex_name,
const in_addr_t ip,
const in_addr_t netmask,
const unsigned int flags);
static void netsh_command (const struct argv *a, int n);
static const char *netsh_get_id (const char *dev_node, struct gc_arena *gc);
static DWORD get_adapter_index_flexible (const char *name);
#endif
#ifdef TARGET_SOLARIS
static void solaris_error_close (struct tuntap *tt, const struct env_set *es, const char *actual, bool unplumb_inet6);
#include <stropts.h>
#endif
#if defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H
#include <sys/kern_control.h>
#include <net/if_utun.h>
#include <sys/sys_domain.h>
#endif
static void clear_tuntap (struct tuntap *tuntap);
bool
is_dev_type (const char *dev, const char *dev_type, const char *match_type)
{
ASSERT (match_type);
if (!dev)
return false;
if (dev_type)
return !strcmp (dev_type, match_type);
else
return !strncmp (dev, match_type, strlen (match_type));
}
int
dev_type_enum (const char *dev, const char *dev_type)
{
if (is_dev_type (dev, dev_type, "tun"))
return DEV_TYPE_TUN;
else if (is_dev_type (dev, dev_type, "tap"))
return DEV_TYPE_TAP;
else if (is_dev_type (dev, dev_type, "null"))
return DEV_TYPE_NULL;
else
return DEV_TYPE_UNDEF;
}
const char *
dev_type_string (const char *dev, const char *dev_type)
{
switch (dev_type_enum (dev, dev_type))
{
case DEV_TYPE_TUN:
return "tun";
case DEV_TYPE_TAP:
return "tap";
case DEV_TYPE_NULL:
return "null";
default:
return "[unknown-dev-type]";
}
}
/*
* Try to predict the actual TUN/TAP device instance name,
* before the device is actually opened.
*/
const char *
guess_tuntap_dev (const char *dev,
const char *dev_type,
const char *dev_node,
struct gc_arena *gc)
{
#ifdef WIN32
const int dt = dev_type_enum (dev, dev_type);
if (dt == DEV_TYPE_TUN || dt == DEV_TYPE_TAP)
{
return netsh_get_id (dev_node, gc);
}
#endif
/* default case */
return dev;
}
/* --ifconfig-nowarn disables some options sanity checking */
static const char ifconfig_warn_how_to_silence[] = "(silence this warning with --ifconfig-nowarn)";
/*
* If !tun, make sure ifconfig_remote_netmask looks
* like a netmask.
*
* If tun, make sure ifconfig_remote_netmask looks
* like an IPv4 address.
*/
static void
ifconfig_sanity_check (bool tun, in_addr_t addr, int topology)
{
struct gc_arena gc = gc_new ();
const bool looks_like_netmask = ((addr & 0xFF000000) == 0xFF000000);
if (tun)
{
if (looks_like_netmask && (topology == TOP_NET30 || topology == TOP_P2P))
msg (M_WARN, "WARNING: Since you are using --dev tun with a point-to-point topology, the second argument to --ifconfig must be an IP address. You are using something (%s) that looks more like a netmask. %s",
print_in_addr_t (addr, 0, &gc),
ifconfig_warn_how_to_silence);
}
else /* tap */
{
if (!looks_like_netmask)
msg (M_WARN, "WARNING: Since you are using --dev tap, the second argument to --ifconfig must be a netmask, for example something like 255.255.255.0. %s",
ifconfig_warn_how_to_silence);
}
gc_free (&gc);
}
/*
* For TAP-style devices, generate a broadcast address.
*/
static in_addr_t
generate_ifconfig_broadcast_addr (in_addr_t local,
in_addr_t netmask)
{
return local | ~netmask;
}
/*
* Check that --local and --remote addresses do not
* clash with ifconfig addresses or subnet.
*/
static void
check_addr_clash (const char *name,
int type,
in_addr_t public,
in_addr_t local,
in_addr_t remote_netmask)
{
struct gc_arena gc = gc_new ();
#if 0
msg (M_INFO, "CHECK_ADDR_CLASH type=%d public=%s local=%s, remote_netmask=%s",
type,
print_in_addr_t (public, 0, &gc),
print_in_addr_t (local, 0, &gc),
print_in_addr_t (remote_netmask, 0, &gc));
#endif
if (public)
{
if (type == DEV_TYPE_TUN)
{
const in_addr_t test_netmask = 0xFFFFFF00;
const in_addr_t public_net = public & test_netmask;
const in_addr_t local_net = local & test_netmask;
const in_addr_t remote_net = remote_netmask & test_netmask;
if (public == local || public == remote_netmask)
msg (M_WARN,
"WARNING: --%s address [%s] conflicts with --ifconfig address pair [%s, %s]. %s",
name,
print_in_addr_t (public, 0, &gc),
print_in_addr_t (local, 0, &gc),
print_in_addr_t (remote_netmask, 0, &gc),
ifconfig_warn_how_to_silence);
if (public_net == local_net || public_net == remote_net)
msg (M_WARN,
"WARNING: potential conflict between --%s address [%s] and --ifconfig address pair [%s, %s] -- this is a warning only that is triggered when local/remote addresses exist within the same /24 subnet as --ifconfig endpoints. %s",
name,
print_in_addr_t (public, 0, &gc),
print_in_addr_t (local, 0, &gc),
print_in_addr_t (remote_netmask, 0, &gc),
ifconfig_warn_how_to_silence);
}
else if (type == DEV_TYPE_TAP)
{
const in_addr_t public_network = public & remote_netmask;
const in_addr_t virtual_network = local & remote_netmask;
if (public_network == virtual_network)
msg (M_WARN,
"WARNING: --%s address [%s] conflicts with --ifconfig subnet [%s, %s] -- local and remote addresses cannot be inside of the --ifconfig subnet. %s",
name,
print_in_addr_t (public, 0, &gc),
print_in_addr_t (local, 0, &gc),
print_in_addr_t (remote_netmask, 0, &gc),
ifconfig_warn_how_to_silence);
}
}
gc_free (&gc);
}
/*
* Issue a warning if ip/netmask (on the virtual IP network) conflicts with
* the settings on the local LAN. This is designed to flag issues where
* (for example) the OpenVPN server LAN is running on 192.168.1.x, but then
* an OpenVPN client tries to connect from a public location that is also running
* off of a router set to 192.168.1.x.
*/
void
check_subnet_conflict (const in_addr_t ip,
const in_addr_t netmask,
const char *prefix)
{
#if 0 /* too many false positives */
struct gc_arena gc = gc_new ();
in_addr_t lan_gw = 0;
in_addr_t lan_netmask = 0;
if (get_default_gateway (&lan_gw, &lan_netmask) && lan_netmask)
{
const in_addr_t lan_network = lan_gw & lan_netmask;
const in_addr_t network = ip & netmask;
/* do the two subnets defined by network/netmask and lan_network/lan_netmask intersect? */
if ((network & lan_netmask) == lan_network
|| (lan_network & netmask) == network)
{
msg (M_WARN, "WARNING: potential %s subnet conflict between local LAN [%s/%s] and remote VPN [%s/%s]",
prefix,
print_in_addr_t (lan_network, 0, &gc),
print_in_addr_t (lan_netmask, 0, &gc),
print_in_addr_t (network, 0, &gc),
print_in_addr_t (netmask, 0, &gc));
}
}
gc_free (&gc);
#endif
}
void
warn_on_use_of_common_subnets (void)
{
struct gc_arena gc = gc_new ();
struct route_gateway_info rgi;
const int needed = (RGI_ADDR_DEFINED|RGI_NETMASK_DEFINED);
get_default_gateway (&rgi);
if ((rgi.flags & needed) == needed)
{
const in_addr_t lan_network = rgi.gateway.addr & rgi.gateway.netmask;
if (lan_network == 0xC0A80000 || lan_network == 0xC0A80100)
msg (M_WARN, "NOTE: your local LAN uses the extremely common subnet address 192.168.0.x or 192.168.1.x. Be aware that this might create routing conflicts if you connect to the VPN server from public locations such as internet cafes that use the same subnet.");
}
gc_free (&gc);
}
/*
* Return a string to be used for options compatibility check
* between peers.
*/
const char *
ifconfig_options_string (const struct tuntap* tt, bool remote, bool disable, struct gc_arena *gc)
{
struct buffer out = alloc_buf_gc (256, gc);
if (tt->did_ifconfig_setup && !disable)
{
if (tt->type == DEV_TYPE_TAP || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET))
{
buf_printf (&out, "%s %s",
print_in_addr_t (tt->local & tt->remote_netmask, 0, gc),
print_in_addr_t (tt->remote_netmask, 0, gc));
}
else if (tt->type == DEV_TYPE_TUN)
{
const char *l, *r;
if (remote)
{
r = print_in_addr_t (tt->local, 0, gc);
l = print_in_addr_t (tt->remote_netmask, 0, gc);
}
else
{
l = print_in_addr_t (tt->local, 0, gc);
r = print_in_addr_t (tt->remote_netmask, 0, gc);
}
buf_printf (&out, "%s %s", r, l);
}
else
buf_printf (&out, "[undef]");
}
return BSTR (&out);
}
/*
* Return a status string describing wait state.
*/
const char *
tun_stat (const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc)
{
struct buffer out = alloc_buf_gc (64, gc);
if (tt)
{
if (rwflags & EVENT_READ)
{
buf_printf (&out, "T%s",
(tt->rwflags_debug & EVENT_READ) ? "R" : "r");
#ifdef WIN32
buf_printf (&out, "%s",
overlapped_io_state_ascii (&tt->reads));
#endif
}
if (rwflags & EVENT_WRITE)
{
buf_printf (&out, "T%s",
(tt->rwflags_debug & EVENT_WRITE) ? "W" : "w");
#ifdef WIN32
buf_printf (&out, "%s",
overlapped_io_state_ascii (&tt->writes));
#endif
}
}
else
{
buf_printf (&out, "T?");
}
return BSTR (&out);
}
/*
* Return true for point-to-point topology, false for subnet topology
*/
bool
is_tun_p2p (const struct tuntap *tt)
{
bool tun = false;
if (tt->type == DEV_TYPE_TAP || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET))
tun = false;
else if (tt->type == DEV_TYPE_TUN)
tun = true;
else
msg (M_FATAL, "Error: problem with tun vs. tap setting"); /* JYFIXME -- needs to be caught earlier, in init_tun? */
return tun;
}
/*
* Set the ifconfig_* environment variables, both for IPv4 and IPv6
*/
void
do_ifconfig_setenv (const struct tuntap *tt, struct env_set *es)
{
struct gc_arena gc = gc_new ();
const char *ifconfig_local = print_in_addr_t (tt->local, 0, &gc);
const char *ifconfig_remote_netmask = print_in_addr_t (tt->remote_netmask, 0, &gc);
/*
* Set environmental variables with ifconfig parameters.
*/
if (tt->did_ifconfig_setup)
{
bool tun = is_tun_p2p (tt);
setenv_str (es, "ifconfig_local", ifconfig_local);
if (tun)
{
setenv_str (es, "ifconfig_remote", ifconfig_remote_netmask);
}
else
{
const char *ifconfig_broadcast = print_in_addr_t (tt->broadcast, 0, &gc);
setenv_str (es, "ifconfig_netmask", ifconfig_remote_netmask);
setenv_str (es, "ifconfig_broadcast", ifconfig_broadcast);
}
}
if (tt->did_ifconfig_ipv6_setup)
{
const char *ifconfig_ipv6_local = print_in6_addr (tt->local_ipv6, 0, &gc);
const char *ifconfig_ipv6_remote = print_in6_addr (tt->remote_ipv6, 0, &gc);
setenv_str (es, "ifconfig_ipv6_local", ifconfig_ipv6_local);
setenv_int (es, "ifconfig_ipv6_netbits", tt->netbits_ipv6);
setenv_str (es, "ifconfig_ipv6_remote", ifconfig_ipv6_remote);
}
gc_free (&gc);
}
/*
* Init tun/tap object.
*
* Set up tuntap structure for ifconfig,
* but don't execute yet.
*/
struct tuntap *
init_tun (const char *dev, /* --dev option */
const char *dev_type, /* --dev-type option */
int topology, /* one of the TOP_x values */
const char *ifconfig_local_parm, /* --ifconfig parm 1 */
const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
const char *ifconfig_ipv6_local_parm, /* --ifconfig parm 1 IPv6 */
int ifconfig_ipv6_netbits_parm,
const char *ifconfig_ipv6_remote_parm, /* --ifconfig parm 2 IPv6 */
in_addr_t local_public,
in_addr_t remote_public,
const bool strict_warn,
struct env_set *es)
{
struct gc_arena gc = gc_new ();
struct tuntap *tt;
ALLOC_OBJ (tt, struct tuntap);
clear_tuntap (tt);
tt->type = dev_type_enum (dev, dev_type);
tt->topology = topology;
if (ifconfig_local_parm && ifconfig_remote_netmask_parm)
{
bool tun = false;
/*
* We only handle TUN/TAP devices here, not --dev null devices.
*/
tun = is_tun_p2p (tt);
/*
* Convert arguments to binary IPv4 addresses.
*/
tt->local = getaddr (
GETADDR_RESOLVE
| GETADDR_HOST_ORDER
| GETADDR_FATAL_ON_SIGNAL
| GETADDR_FATAL,
ifconfig_local_parm,
0,
NULL,
NULL);
tt->remote_netmask = getaddr (
(tun ? GETADDR_RESOLVE : 0)
| GETADDR_HOST_ORDER
| GETADDR_FATAL_ON_SIGNAL
| GETADDR_FATAL,
ifconfig_remote_netmask_parm,
0,
NULL,
NULL);
/*
* Look for common errors in --ifconfig parms
*/
if (strict_warn)
{
ifconfig_sanity_check (tt->type == DEV_TYPE_TUN, tt->remote_netmask, tt->topology);
/*
* If local_public or remote_public addresses are defined,
* make sure they do not clash with our virtual subnet.
*/
check_addr_clash ("local",
tt->type,
local_public,
tt->local,
tt->remote_netmask);
check_addr_clash ("remote",
tt->type,
remote_public,
tt->local,
tt->remote_netmask);
if (tt->type == DEV_TYPE_TAP || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET))
check_subnet_conflict (tt->local, tt->remote_netmask, "TUN/TAP adapter");
else if (tt->type == DEV_TYPE_TUN)
check_subnet_conflict (tt->local, IPV4_NETMASK_HOST, "TUN/TAP adapter");
}
/*
* If TAP-style interface, generate broadcast address.
*/
if (!tun)
{
tt->broadcast = generate_ifconfig_broadcast_addr (tt->local, tt->remote_netmask);
}
tt->did_ifconfig_setup = true;
}
if (ifconfig_ipv6_local_parm && ifconfig_ipv6_remote_parm)
{
/*
* Convert arguments to binary IPv6 addresses.
*/
if ( inet_pton( AF_INET6, ifconfig_ipv6_local_parm, &tt->local_ipv6 ) != 1 ||
inet_pton( AF_INET6, ifconfig_ipv6_remote_parm, &tt->remote_ipv6 ) != 1 )
{
msg( M_FATAL, "init_tun: problem converting IPv6 ifconfig addresses %s and %s to binary", ifconfig_ipv6_local_parm, ifconfig_ipv6_remote_parm );
}
tt->netbits_ipv6 = ifconfig_ipv6_netbits_parm;
tt->did_ifconfig_ipv6_setup = true;
}
/*
* Set environmental variables with ifconfig parameters.
*/
if (es) do_ifconfig_setenv(tt, es);
gc_free (&gc);
return tt;
}
/*
* Platform specific tun initializations
*/
void
init_tun_post (struct tuntap *tt,
const struct frame *frame,
const struct tuntap_options *options)
{
tt->options = *options;
#ifdef WIN32
overlapped_io_init (&tt->reads, frame, FALSE, true);
overlapped_io_init (&tt->writes, frame, TRUE, true);
tt->rw_handle.read = tt->reads.overlapped.hEvent;
tt->rw_handle.write = tt->writes.overlapped.hEvent;
tt->adapter_index = TUN_ADAPTER_INDEX_INVALID;
#endif
}
#if defined(WIN32) || \
defined(TARGET_DARWIN) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD)
/* some of the platforms will auto-add a "network route" pointing
* to the interface on "ifconfig tunX 2001:db8::1/64", others need
* an extra call to "route add..."
* -> helper function to simplify code below
*/
void add_route_connected_v6_net(struct tuntap * tt,
const struct env_set *es)
{
struct route_ipv6 r6;
r6.defined = true;
r6.network = tt->local_ipv6;
r6.netbits = tt->netbits_ipv6;
r6.gateway = tt->local_ipv6;
r6.metric = 0; /* connected route */
r6.metric_defined = true;
add_route_ipv6 (&r6, tt, 0, es);
}
void delete_route_connected_v6_net(struct tuntap * tt,
const struct env_set *es)
{
struct route_ipv6 r6;
r6.defined = true;
r6.network = tt->local_ipv6;
r6.netbits = tt->netbits_ipv6;
r6.gateway = tt->local_ipv6;
r6.metric = 0; /* connected route */
r6.metric_defined = true;
delete_route_ipv6 (&r6, tt, 0, es);
}
#endif
#if defined(TARGET_FREEBSD)||defined(TARGET_DRAGONFLY)
/* we can't use true subnet mode on tun on all platforms, as that
* conflicts with IPv6 (wants to use ND then, which we don't do),
* but the OSes want "a remote address that is different from ours"
* - so we construct one, normally the first in the subnet, but if
* this is the same as ours, use the second one.
* The actual address does not matter at all, as the tun interface
* is still point to point and no layer 2 resolution is done...
*/
const char *
create_arbitrary_remote( struct tuntap *tt, struct gc_arena * gc )
{
in_addr_t remote;
remote = (tt->local & tt->remote_netmask) +1;
if ( remote == tt->local ) remote ++;
return print_in_addr_t (remote, 0, gc);
}
#endif
/* execute the ifconfig command through the shell */
void
do_ifconfig (struct tuntap *tt,
const char *actual, /* actual device name */
int tun_mtu,
const struct env_set *es)
{
struct gc_arena gc = gc_new ();
if (tt->did_ifconfig_setup)
{
bool tun = false;
const char *ifconfig_local = NULL;
const char *ifconfig_remote_netmask = NULL;
const char *ifconfig_broadcast = NULL;
const char *ifconfig_ipv6_local = NULL;
const char *ifconfig_ipv6_remote = NULL;
bool do_ipv6 = false;
struct argv argv;
argv_init (&argv);
msg( M_INFO, "do_ifconfig, tt->ipv6=%d, tt->did_ifconfig_ipv6_setup=%d",
tt->ipv6, tt->did_ifconfig_ipv6_setup );
/*
* We only handle TUN/TAP devices here, not --dev null devices.
*/
tun = is_tun_p2p (tt);
/*
* Set ifconfig parameters
*/
ifconfig_local = print_in_addr_t (tt->local, 0, &gc);
ifconfig_remote_netmask = print_in_addr_t (tt->remote_netmask, 0, &gc);
if ( tt->ipv6 && tt->did_ifconfig_ipv6_setup )
{
ifconfig_ipv6_local = print_in6_addr (tt->local_ipv6, 0, &gc);
ifconfig_ipv6_remote = print_in6_addr (tt->remote_ipv6, 0, &gc);
do_ipv6 = true;
}
/*
* If TAP-style device, generate broadcast address.
*/
if (!tun)
ifconfig_broadcast = print_in_addr_t (tt->broadcast, 0, &gc);
#ifdef ENABLE_MANAGEMENT
if (management)
{
management_set_state (management,
OPENVPN_STATE_ASSIGN_IP,
NULL,
tt->local,
0);
}
#endif
#if defined(TARGET_LINUX)
#ifdef ENABLE_IPROUTE
/*
* Set the MTU for the device
*/
argv_printf (&argv,
"%s link set dev %s up mtu %d",
iproute_path,
actual,
tun_mtu
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Linux ip link set failed");
if (tun) {
/*
* Set the address for the device
*/
argv_printf (&argv,
"%s addr add dev %s local %s peer %s",
iproute_path,
actual,
ifconfig_local,
ifconfig_remote_netmask
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Linux ip addr add failed");
} else {
argv_printf (&argv,
"%s addr add dev %s %s/%d broadcast %s",
iproute_path,
actual,
ifconfig_local,
count_netmask_bits(ifconfig_remote_netmask),
ifconfig_broadcast
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Linux ip addr add failed");
}
if ( do_ipv6 )
{
argv_printf( &argv,
"%s -6 addr add %s/%d dev %s",
iproute_path,
ifconfig_ipv6_local,
tt->netbits_ipv6,
actual
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Linux ip -6 addr add failed");
}
tt->did_ifconfig = true;
#else
if (tun)
argv_printf (&argv,
"%s %s %s pointopoint %s mtu %d",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
else
argv_printf (&argv,
"%s %s %s netmask %s mtu %d broadcast %s",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu,
ifconfig_broadcast
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Linux ifconfig failed");
if ( do_ipv6 )
{
argv_printf (&argv,
"%s %s add %s/%d",
IFCONFIG_PATH,
actual,
ifconfig_ipv6_local,
tt->netbits_ipv6
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Linux ifconfig inet6 failed");
}
tt->did_ifconfig = true;
#endif /*ENABLE_IPROUTE*/
#elif defined(TARGET_SOLARIS)
/* Solaris 2.6 (and 7?) cannot set all parameters in one go...
* example:
* ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 up
* ifconfig tun2 netmask 255.255.255.255
*/
if (tun)
{
argv_printf (&argv,
"%s %s %s %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
argv_msg (M_INFO, &argv);
if (!openvpn_execve_check (&argv, es, 0, "Solaris ifconfig phase-1 failed"))
solaris_error_close (tt, es, actual, false);
argv_printf (&argv,
"%s %s netmask 255.255.255.255",
IFCONFIG_PATH,
actual
);
}
else
if (tt->topology == TOP_SUBNET)
{
argv_printf (&argv,
"%s %s %s %s netmask %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
else
argv_printf (&argv,
" %s %s %s netmask %s broadcast + up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask
);
argv_msg (M_INFO, &argv);
if (!openvpn_execve_check (&argv, es, 0, "Solaris ifconfig phase-2 failed"))
solaris_error_close (tt, es, actual, false);
if ( do_ipv6 )
{
argv_printf (&argv, "%s %s inet6 unplumb",
IFCONFIG_PATH, actual );
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, 0, NULL);
if ( tt->type == DEV_TYPE_TUN )
{
argv_printf (&argv,
"%s %s inet6 plumb %s/%d %s up",
IFCONFIG_PATH,
actual,
ifconfig_ipv6_local,
tt->netbits_ipv6,
ifconfig_ipv6_remote
);
}
else /* tap mode */
{
/* base IPv6 tap interface needs to be brought up first
*/
argv_printf (&argv, "%s %s inet6 plumb up",
IFCONFIG_PATH, actual );
argv_msg (M_INFO, &argv);
if (!openvpn_execve_check (&argv, es, 0, "Solaris ifconfig IPv6 (prepare) failed"))
solaris_error_close (tt, es, actual, true);
/* we might need to do "ifconfig %s inet6 auto-dhcp drop"
* after the system has noticed the interface and fired up
* the DHCPv6 client - but this takes quite a while, and the
* server will ignore the DHCPv6 packets anyway. So we don't.
*/
/* static IPv6 addresses need to go to a subinterface (tap0:1)
*/
argv_printf (&argv,
"%s %s inet6 addif %s/%d up",
IFCONFIG_PATH, actual,
ifconfig_ipv6_local, tt->netbits_ipv6 );
}
argv_msg (M_INFO, &argv);
if (!openvpn_execve_check (&argv, es, 0, "Solaris ifconfig IPv6 failed"))
solaris_error_close (tt, es, actual, true);
}
if (!tun && tt->topology == TOP_SUBNET)
{
/* Add a network route for the local tun interface */
struct route_ipv4 r;
CLEAR (r);
r.flags = RT_DEFINED | RT_METRIC_DEFINED;
r.network = tt->local & tt->remote_netmask;
r.netmask = tt->remote_netmask;
r.gateway = tt->local;
r.metric = 0;
add_route (&r, tt, 0, NULL, es);
}
tt->did_ifconfig = true;
#elif defined(TARGET_OPENBSD)
/*
* On OpenBSD, tun interfaces are persistent if created with
* "ifconfig tunX create", and auto-destroyed if created by
* opening "/dev/tunX" (so we just use the /dev/tunX)
*/
/* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
if (tun)
argv_printf (&argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up -link0",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
else
if ( tt->topology == TOP_SUBNET )
{
argv_printf (&argv,
"%s %s %s %s mtu %d netmask %s up -link0",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_local,
tun_mtu,
ifconfig_remote_netmask
);
}
else
argv_printf (&argv,
"%s %s %s netmask %s mtu %d broadcast %s link0",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu,
ifconfig_broadcast
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "OpenBSD ifconfig failed");
if ( do_ipv6 )
{
argv_printf (&argv,
"%s %s inet6 %s/%d",
IFCONFIG_PATH,
actual,
ifconfig_ipv6_local,
tt->netbits_ipv6
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "OpenBSD ifconfig inet6 failed");
/* and, hooray, we explicitely need to add a route... */
add_route_connected_v6_net(tt, es);
}
tt->did_ifconfig = true;
#elif defined(TARGET_NETBSD)
/* whether or not NetBSD can do IPv6 can be seen by the availability of
* the TUNSIFHEAD ioctl() - see next TARGET_NETBSD block for more details
*/
#ifdef TUNSIFHEAD
# define NETBSD_MULTI_AF
#endif
if (tun)
argv_printf (&argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
else
if ( tt->topology == TOP_SUBNET )
{
argv_printf (&argv,
"%s %s %s %s mtu %d netmask %s up",
IFCONFIG_PATH,