-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathifconfig.c
More file actions
2960 lines (2605 loc) · 71.9 KB
/
ifconfig.c
File metadata and controls
2960 lines (2605 loc) · 71.9 KB
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
/* $NetBSD: ifconfig.c,v 1.130 2002/09/20 21:21:53 christos Exp $ */
/*-
* Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
#else
__RCSID("$NetBSD: ifconfig.c,v 1.130 2002/09/20 21:21:53 christos Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_ether.h>
#include <net/if_ieee80211.h>
#include <net/if_vlanvar.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#ifdef INET6
#include <netinet6/nd6.h>
#endif
#include <arpa/inet.h>
#include <netatalk/at.h>
#define NSIP
#include <netns/ns.h>
#include <netns/ns_if.h>
#include <netdb.h>
#define EON
#include <netiso/iso.h>
#include <netiso/iso_var.h>
#include <sys/protosw.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ifaddrs.h>
#include <util.h>
struct ifreq ifr, ridreq;
struct ifaliasreq addreq __attribute__((aligned(4)));
struct in_aliasreq in_addreq;
#ifdef INET6
struct in6_ifreq ifr6;
struct in6_ifreq in6_ridreq;
struct in6_aliasreq in6_addreq;
#endif
struct iso_ifreq iso_ridreq;
struct iso_aliasreq iso_addreq;
struct sockaddr_in netmask;
struct netrange at_nr; /* AppleTalk net range */
char name[30];
u_short flags;
int setaddr, setipdst, doalias;
u_long metric, mtu;
int clearaddr, s;
int newaddr = -1;
int conflicting = 0;
int nsellength = 1;
int af;
int aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag, vflag;
#ifdef INET6
int Lflag;
#endif
int reset_if_flags;
int explicit_prefix = 0;
u_int vlan_tag = (u_int)-1;
struct ifcapreq g_ifcr;
int g_ifcr_updated;
void notealias __P((const char *, int));
void notrailers __P((const char *, int));
void setifaddr __P((const char *, int));
void setifdstaddr __P((const char *, int));
void setifflags __P((const char *, int));
void setifcaps __P((const char *, int));
void setifbroadaddr __P((const char *, int));
void setifipdst __P((const char *, int));
void setifmetric __P((const char *, int));
void setifmtu __P((const char *, int));
void setifnwid __P((const char *, int));
void setifnwkey __P((const char *, int));
void setifpowersave __P((const char *, int));
void setifpowersavesleep __P((const char *, int));
void setifnetmask __P((const char *, int));
void setifprefixlen __P((const char *, int));
void setnsellength __P((const char *, int));
void setsnpaoffset __P((const char *, int));
void setatrange __P((const char *, int));
void setatphase __P((const char *, int));
void settunnel __P((const char *, const char *));
void deletetunnel __P((const char *, int));
#ifdef INET6
void setia6flags __P((const char *, int));
void setia6pltime __P((const char *, int));
void setia6vltime __P((const char *, int));
void setia6lifetime __P((const char *, const char *));
void setia6eui64 __P((const char *, int));
#endif
void checkatrange __P ((struct sockaddr_at *));
void setmedia __P((const char *, int));
void setmediaopt __P((const char *, int));
void unsetmediaopt __P((const char *, int));
void setmediainst __P((const char *, int));
void clone_create __P((const char *, int));
void clone_destroy __P((const char *, int));
void fixnsel __P((struct sockaddr_iso *));
void setvlan __P((const char *, int));
void setvlanif __P((const char *, int));
void unsetvlanif __P((const char *, int));
int main __P((int, char *[]));
/*
* Media stuff. Whenever a media command is first performed, the
* currently select media is grabbed for this interface. If `media'
* is given, the current media word is modifed. `mediaopt' commands
* only modify the set and clear words. They then operate on the
* current media word later.
*/
int media_current;
int mediaopt_set;
int mediaopt_clear;
int actions; /* Actions performed */
#define A_MEDIA 0x0001 /* media command */
#define A_MEDIAOPTSET 0x0002 /* mediaopt command */
#define A_MEDIAOPTCLR 0x0004 /* -mediaopt command */
#define A_MEDIAOPT (A_MEDIAOPTSET|A_MEDIAOPTCLR)
#define A_MEDIAINST 0x0008 /* instance or inst command */
#define NEXTARG 0xffffff
#define NEXTARG2 0xfffffe
const struct cmd {
const char *c_name;
int c_parameter; /* NEXTARG means next argv */
int c_action; /* defered action */
void (*c_func) __P((const char *, int));
void (*c_func2) __P((const char *, const char *));
} cmds[] = {
{ "up", IFF_UP, 0, setifflags } ,
{ "down", -IFF_UP, 0, setifflags },
{ "trailers", -1, 0, notrailers },
{ "-trailers", 1, 0, notrailers },
{ "arp", -IFF_NOARP, 0, setifflags },
{ "-arp", IFF_NOARP, 0, setifflags },
{ "debug", IFF_DEBUG, 0, setifflags },
{ "-debug", -IFF_DEBUG, 0, setifflags },
{ "alias", IFF_UP, 0, notealias },
{ "-alias", -IFF_UP, 0, notealias },
{ "delete", -IFF_UP, 0, notealias },
#ifdef notdef
#define EN_SWABIPS 0x1000
{ "swabips", EN_SWABIPS, 0, setifflags },
{ "-swabips", -EN_SWABIPS, 0, setifflags },
#endif
{ "netmask", NEXTARG, 0, setifnetmask },
{ "metric", NEXTARG, 0, setifmetric },
{ "mtu", NEXTARG, 0, setifmtu },
{ "nwid", NEXTARG, 0, setifnwid },
{ "nwkey", NEXTARG, 0, setifnwkey },
{ "-nwkey", -1, 0, setifnwkey },
{ "powersave", 1, 0, setifpowersave },
{ "-powersave", 0, 0, setifpowersave },
{ "powersavesleep", NEXTARG, 0, setifpowersavesleep },
{ "broadcast", NEXTARG, 0, setifbroadaddr },
{ "ipdst", NEXTARG, 0, setifipdst },
{ "prefixlen", NEXTARG, 0, setifprefixlen},
#ifdef INET6
{ "anycast", IN6_IFF_ANYCAST, 0, setia6flags },
{ "-anycast", -IN6_IFF_ANYCAST, 0, setia6flags },
{ "tentative", IN6_IFF_TENTATIVE, 0, setia6flags },
{ "-tentative", -IN6_IFF_TENTATIVE, 0, setia6flags },
{ "deprecated", IN6_IFF_DEPRECATED, 0, setia6flags },
{ "-deprecated", -IN6_IFF_DEPRECATED, 0, setia6flags },
{ "pltime", NEXTARG, 0, setia6pltime },
{ "vltime", NEXTARG, 0, setia6vltime },
{ "eui64", 0, 0, setia6eui64 },
#endif /*INET6*/
#ifndef INET_ONLY
{ "range", NEXTARG, 0, setatrange },
{ "phase", NEXTARG, 0, setatphase },
{ "snpaoffset", NEXTARG, 0, setsnpaoffset },
{ "nsellength", NEXTARG, 0, setnsellength },
#endif /* INET_ONLY */
{ "tunnel", NEXTARG2, 0, NULL,
settunnel } ,
{ "deletetunnel", 0, 0, deletetunnel },
{ "vlan", NEXTARG, 0, setvlan } ,
{ "vlanif", NEXTARG, 0, setvlanif } ,
{ "-vlanif", 0, 0, unsetvlanif } ,
#if 0
/* XXX `create' special-cased below */
{ "create", 0, 0, clone_create } ,
#endif
{ "destroy", 0, 0, clone_destroy } ,
{ "link0", IFF_LINK0, 0, setifflags } ,
{ "-link0", -IFF_LINK0, 0, setifflags } ,
{ "link1", IFF_LINK1, 0, setifflags } ,
{ "-link1", -IFF_LINK1, 0, setifflags } ,
{ "link2", IFF_LINK2, 0, setifflags } ,
{ "-link2", -IFF_LINK2, 0, setifflags } ,
{ "media", NEXTARG, A_MEDIA, setmedia },
{ "mediaopt", NEXTARG, A_MEDIAOPTSET, setmediaopt },
{ "-mediaopt", NEXTARG, A_MEDIAOPTCLR, unsetmediaopt },
{ "instance", NEXTARG, A_MEDIAINST, setmediainst },
{ "inst", NEXTARG, A_MEDIAINST, setmediainst },
{ "ip4csum", IFCAP_CSUM_IPv4,0, setifcaps },
{ "-ip4csum", -IFCAP_CSUM_IPv4,0, setifcaps },
{ "tcp4csum", IFCAP_CSUM_TCPv4,0, setifcaps },
{ "-tcp4csum", -IFCAP_CSUM_TCPv4,0, setifcaps },
{ "udp4csum", IFCAP_CSUM_UDPv4,0, setifcaps },
{ "-udp4csum", -IFCAP_CSUM_UDPv4,0, setifcaps },
{ "tcp6csum", IFCAP_CSUM_TCPv6,0, setifcaps },
{ "-tcp6csum", -IFCAP_CSUM_TCPv6,0, setifcaps },
{ "udp6csum", IFCAP_CSUM_UDPv6,0, setifcaps },
{ "-udp6csum", -IFCAP_CSUM_UDPv6,0, setifcaps },
{ "tcp4csum-rx",IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
{ "-tcp4csum-rx",-IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
{ "udp4csum-rx",IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
{ "-udp4csum-rx",-IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
{ 0, 0, 0, setifaddr },
{ 0, 0, 0, setifdstaddr },
};
void adjust_nsellength __P((void));
int getinfo __P((struct ifreq *));
int carrier __P((void));
void getsock __P((int));
void printall __P((const char *));
void list_cloners __P((void));
int prefix __P((void *, int));
void status __P((const struct sockaddr_dl *));
void usage __P((void));
const char *get_string __P((const char *, const char *, u_int8_t *, int *));
void print_string __P((const u_int8_t *, int));
char *sec2str __P((time_t));
const char *get_media_type_string __P((int));
const char *get_media_subtype_string __P((int));
int get_media_subtype __P((int, const char *));
int get_media_options __P((int, const char *));
int lookup_media_word __P((struct ifmedia_description *, int,
const char *));
void print_media_word __P((int, int, int));
void process_media_commands __P((void));
void init_current_media __P((void));
/*
* XNS support liberally adapted from code written at the University of
* Maryland principally by James O'Toole and Chris Torek.
*/
void in_alias __P((struct ifreq *));
void in_status __P((int));
void in_getaddr __P((const char *, int));
void in_getprefix __P((const char *, int));
#ifdef INET6
void in6_fillscopeid __P((struct sockaddr_in6 *sin6));
void in6_alias __P((struct in6_ifreq *));
void in6_status __P((int));
void in6_getaddr __P((const char *, int));
void in6_getprefix __P((const char *, int));
#endif
void at_status __P((int));
void at_getaddr __P((const char *, int));
void xns_status __P((int));
void xns_getaddr __P((const char *, int));
void iso_status __P((int));
void iso_getaddr __P((const char *, int));
void ieee80211_status __P((void));
void tunnel_status __P((void));
void vlan_status __P((void));
/* Known address families */
struct afswtch {
const char *af_name;
short af_af;
void (*af_status) __P((int));
void (*af_getaddr) __P((const char *, int));
void (*af_getprefix) __P((const char *, int));
u_long af_difaddr;
u_long af_aifaddr;
u_long af_gifaddr;
caddr_t af_ridreq;
caddr_t af_addreq;
} afs[] = {
#define C(x) ((caddr_t) &x)
{ "inet", AF_INET, in_status, in_getaddr, in_getprefix,
SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(ridreq), C(in_addreq) },
#ifdef INET6
{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
/*
* Deleting the first address before setting new one is
* not prefered way in this protocol.
*/
0,
C(in6_ridreq), C(in6_addreq) },
#endif
#ifndef INET_ONLY /* small version, for boot media */
{ "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(addreq), C(addreq) },
{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(ridreq), C(addreq) },
{ "iso", AF_ISO, iso_status, iso_getaddr, NULL,
SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
C(iso_ridreq), C(iso_addreq) },
#endif /* INET_ONLY */
{ 0, 0, 0, 0 }
};
struct afswtch *afp; /*the address family being set or asked about*/
struct afswtch *lookup_af __P((const char *));
int
main(argc, argv)
int argc;
char *argv[];
{
struct ifreq ifreq;
int ch;
/* Parse command-line options */
aflag = mflag = vflag = 0;
while ((ch = getopt(argc, argv, "AabCdlmsuv"
#ifdef INET6
"L"
#endif
)) != -1) {
switch (ch) {
case 'A':
warnx("-A is deprecated");
break;
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'C':
Cflag = 1;
break;
case 'd':
dflag = 1;
break;
#ifdef INET6
case 'L':
Lflag = 1;
break;
#endif
case 'l':
lflag = 1;
break;
case 'm':
mflag = 1;
break;
case 's':
sflag = 1;
break;
case 'u':
uflag = 1;
break;
case 'v':
vflag = 1;
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
/*
* -l means "list all interfaces", and is mutally exclusive with
* all other flags/commands.
*
* -C means "list all names of cloners", and it mutually exclusive
* with all other flags/commands.
*
* -a means "print status of all interfaces".
*/
if ((lflag || Cflag) && (aflag || mflag || vflag || argc))
usage();
#ifdef INET6
if ((lflag || Cflag) && Lflag)
usage();
#endif
if (lflag && Cflag)
usage();
if (Cflag) {
if (argc)
usage();
list_cloners();
exit(0);
}
if (aflag || lflag) {
if (argc > 1)
usage();
else if (argc == 1) {
afp = lookup_af(argv[0]);
if (afp == NULL)
usage();
}
if (afp)
af = ifr.ifr_addr.sa_family = afp->af_af;
else
af = ifr.ifr_addr.sa_family = afs[0].af_af;
printall(NULL);
exit(0);
}
/* Make sure there's an interface name. */
if (argc < 1)
usage();
(void) strncpy(name, argv[0], sizeof(name));
argc--; argv++;
/*
* NOTE: We must special-case the `create' command right
* here as we would otherwise fail in getinfo().
*/
if (argc > 0 && strcmp(argv[0], "create") == 0) {
clone_create(argv[0], 0);
argc--, argv++;
if (argc == 0)
exit(0);
}
/* Check for address family. */
afp = NULL;
if (argc > 0) {
afp = lookup_af(argv[0]);
if (afp != NULL) {
argv++;
argc--;
}
}
/* Initialize af, just for use in getinfo(). */
if (afp == NULL)
af = afs->af_af;
else
af = afp->af_af;
/* Get information about the interface. */
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (getinfo(&ifr) < 0)
exit(1);
if (sflag) {
if (argc != 0)
usage();
else
exit(carrier());
}
/* No more arguments means interface status. */
if (argc == 0) {
printall(name);
exit(0);
}
/* The following operations assume inet family as the default. */
if (afp == NULL)
afp = afs;
af = ifr.ifr_addr.sa_family = afp->af_af;
#ifdef INET6
/* initialization */
in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
#endif
/* Process commands. */
while (argc > 0) {
const struct cmd *p;
for (p = cmds; p->c_name; p++)
if (strcmp(argv[0], p->c_name) == 0)
break;
if (p->c_name == 0 && setaddr) {
if ((flags & IFF_POINTOPOINT) == 0) {
errx(EXIT_FAILURE,
"can't set destination address %s",
"on non-point-to-point link");
}
p++; /* got src, do dst */
}
if (p->c_func != NULL || p->c_func2 != NULL) {
if (p->c_parameter == NEXTARG) {
if (argc < 2)
errx(EXIT_FAILURE,
"'%s' requires argument",
p->c_name);
(*p->c_func)(argv[1], 0);
argc--, argv++;
} else if (p->c_parameter == NEXTARG2) {
if (argc < 3)
errx(EXIT_FAILURE,
"'%s' requires 2 arguments",
p->c_name);
(*p->c_func2)(argv[1], argv[2]);
argc -= 2, argv += 2;
} else
(*p->c_func)(argv[0], p->c_parameter);
actions |= p->c_action;
}
argc--, argv++;
}
/*
* See if multiple alias, -alias, or delete commands were
* specified. More than one constitutes an invalid command line
*/
if (conflicting > 1)
err(EXIT_FAILURE,
"Only one use of alias, -alias or delete is valid.");
/* Process any media commands that may have been issued. */
process_media_commands();
if (af == AF_INET6 && explicit_prefix == 0) {
/*
* Aggregatable address architecture defines all prefixes
* are 64. So, it is convenient to set prefixlen to 64 if
* it is not specified.
*/
setifprefixlen("64", 0);
/* in6_getprefix("64", MASK) if MASK is available here... */
}
#ifndef INET_ONLY
if (af == AF_ISO)
adjust_nsellength();
if (af == AF_APPLETALK)
checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
if (setipdst && af==AF_NS) {
struct nsip_req rq;
int size = sizeof(rq);
rq.rq_ns = addreq.ifra_addr;
rq.rq_ip = addreq.ifra_dstaddr;
if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
warn("encapsulation routing");
}
#endif /* INET_ONLY */
if (clearaddr) {
(void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
err(EXIT_FAILURE, "SIOCDIFADDR");
}
if (newaddr > 0) {
(void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
warn("SIOCAIFADDR");
}
if (g_ifcr_updated) {
(void) strncpy(g_ifcr.ifcr_name, name,
sizeof(g_ifcr.ifcr_name));
if (ioctl(s, SIOCSIFCAP, (caddr_t) &g_ifcr) == -1)
err(EXIT_FAILURE, "SIOCSIFCAP");
}
if (reset_if_flags) {
(void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
ifreq.ifr_flags = flags;
if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) == -1)
err(EXIT_FAILURE, "SIOCSIFFLAGS");
}
exit(0);
}
struct afswtch *
lookup_af(cp)
const char *cp;
{
struct afswtch *a;
for (a = afs; a->af_name != NULL; a++)
if (strcmp(a->af_name, cp) == 0)
return (a);
return (NULL);
}
void
getsock(naf)
int naf;
{
static int oaf = -1;
if (oaf == naf)
return;
if (oaf != -1)
close(s);
s = socket(naf, SOCK_DGRAM, 0);
if (s < 0)
oaf = -1;
else
oaf = naf;
}
int
getinfo(giifr)
struct ifreq *giifr;
{
getsock(af);
if (s < 0)
err(EXIT_FAILURE, "socket");
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)giifr) == -1) {
warn("SIOCGIFFLAGS %s", giifr->ifr_name);
return (-1);
}
flags = giifr->ifr_flags;
if (ioctl(s, SIOCGIFMETRIC, (caddr_t)giifr) == -1) {
warn("SIOCGIFMETRIC %s", giifr->ifr_name);
metric = 0;
} else
metric = giifr->ifr_metric;
if (ioctl(s, SIOCGIFMTU, (caddr_t)giifr) == -1)
mtu = 0;
else
mtu = giifr->ifr_mtu;
memset(&g_ifcr, 0, sizeof(g_ifcr));
strcpy(g_ifcr.ifcr_name, giifr->ifr_name);
(void) ioctl(s, SIOCGIFCAP, (caddr_t) &g_ifcr);
return (0);
}
void
printall(ifname)
const char *ifname;
{
struct ifaddrs *ifap, *ifa;
struct ifreq paifr;
const struct sockaddr_dl *sdl = NULL;
int idx;
char *p;
if (getifaddrs(&ifap) != 0)
err(EXIT_FAILURE, "getifaddrs");
p = NULL;
idx = 0;
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
memset(&paifr, 0, sizeof(paifr));
strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
memcpy(&paifr.ifr_addr, ifa->ifa_addr,
ifa->ifa_addr->sa_len);
}
if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
continue;
if (ifa->ifa_addr->sa_family == AF_LINK)
sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
if (p && strcmp(p, ifa->ifa_name) == 0)
continue;
(void) strncpy(name, ifa->ifa_name, sizeof(name));
name[sizeof(name) - 1] = '\0';
p = ifa->ifa_name;
if (getinfo(&paifr) < 0)
continue;
if (bflag && (ifa->ifa_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))
continue;
if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
continue;
if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
continue;
if (sflag && carrier())
continue;
idx++;
/*
* Are we just listing the interfaces?
*/
if (lflag) {
if (idx > 1)
putchar(' ');
fputs(name, stdout);
continue;
}
status(sdl);
sdl = NULL;
}
if (lflag)
putchar('\n');
freeifaddrs(ifap);
}
void
list_cloners(void)
{
struct if_clonereq ifcr;
char *cp, *buf;
int idx;
memset(&ifcr, 0, sizeof(ifcr));
getsock(AF_INET);
if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
if (buf == NULL)
err(EXIT_FAILURE, "unable to allocate cloner name buffer");
ifcr.ifcr_count = ifcr.ifcr_total;
ifcr.ifcr_buffer = buf;
if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
/*
* In case some disappeared in the mean time, clamp it down.
*/
if (ifcr.ifcr_count > ifcr.ifcr_total)
ifcr.ifcr_count = ifcr.ifcr_total;
for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
if (idx > 0)
putchar(' ');
printf("%s", cp);
}
putchar('\n');
free(buf);
return;
}
/*ARGSUSED*/
void
clone_create(addr, param)
const char *addr;
int param;
{
/* We're called early... */
getsock(AF_INET);
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCIFCREATE, &ifr) == -1)
err(EXIT_FAILURE, "SIOCIFCREATE");
}
/*ARGSUSED*/
void
clone_destroy(addr, param)
const char *addr;
int param;
{
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCIFDESTROY, &ifr) == -1)
err(EXIT_FAILURE, "SIOCIFDESTROY");
}
#define RIDADDR 0
#define ADDR 1
#define MASK 2
#define DSTADDR 3
/*ARGSUSED*/
void
setifaddr(addr, param)
const char *addr;
int param;
{
struct ifreq *siifr; /* XXX */
/*
* Delay the ioctl to set the interface addr until flags are all set.
* The address interpretation may depend on the flags,
* and the flags may change when the address is set.
*/
setaddr++;
if (newaddr == -1)
newaddr = 1;
if (doalias == 0 && afp->af_gifaddr != 0) {
siifr = (struct ifreq *)afp->af_ridreq;
(void) strncpy(siifr->ifr_name, name, sizeof(siifr->ifr_name));
siifr->ifr_addr.sa_family = afp->af_af;
if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
clearaddr = 1;
else if (errno == EADDRNOTAVAIL)
/* No address was assigned yet. */
;
else
err(EXIT_FAILURE, "SIOCGIFADDR");
}
(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
}
void
settunnel(src, dst)
const char *src, *dst;
{
struct addrinfo hints, *srcres, *dstres;
int ecode;
struct if_laddrreq req;
memset(&hints, 0, sizeof(hints));
hints.ai_family = afp->af_af;
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
if ((ecode = getaddrinfo(src, NULL, &hints, &srcres)) != 0)
errx(EXIT_FAILURE, "error in parsing address string: %s",
gai_strerror(ecode));
if ((ecode = getaddrinfo(dst, NULL, &hints, &dstres)) != 0)
errx(EXIT_FAILURE, "error in parsing address string: %s",
gai_strerror(ecode));
if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
errx(EXIT_FAILURE,
"source and destination address families do not match");
if (srcres->ai_addrlen > sizeof(req.addr) ||
dstres->ai_addrlen > sizeof(req.dstaddr))
errx(EXIT_FAILURE, "invalid sockaddr");
memset(&req, 0, sizeof(req));
strncpy(req.iflr_name, name, sizeof(req.iflr_name));
memcpy(&req.addr, srcres->ai_addr, srcres->ai_addrlen);
memcpy(&req.dstaddr, dstres->ai_addr, dstres->ai_addrlen);
#ifdef INET6
if (req.addr.ss_family == AF_INET6) {
struct sockaddr_in6 *s6, *d;
s6 = (struct sockaddr_in6 *)&req.addr;
d = (struct sockaddr_in6 *)&req.dstaddr;
if (s6->sin6_scope_id != d->sin6_scope_id) {
errx(EXIT_FAILURE, "scope mismatch");
/* NOTREACHED */
}
#ifdef __KAME__
/* embed scopeid */
if (s6->sin6_scope_id &&
(IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&s6->sin6_addr))) {
*(u_int16_t *)&s6->sin6_addr.s6_addr[2] =
htons(s6->sin6_scope_id);
}
if (d->sin6_scope_id &&
(IN6_IS_ADDR_LINKLOCAL(&d->sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&d->sin6_addr))) {
*(u_int16_t *)&d->sin6_addr.s6_addr[2] =
htons(d->sin6_scope_id);
}
#endif
}
#endif
if (ioctl(s, SIOCSLIFPHYADDR, &req) == -1)
warn("SIOCSLIFPHYADDR");
freeaddrinfo(srcres);
freeaddrinfo(dstres);
}
/* ARGSUSED */
void
deletetunnel(vname, param)
const char *vname;
int param;
{
if (ioctl(s, SIOCDIFPHYADDR, &ifr) == -1)
err(EXIT_FAILURE, "SIOCDIFPHYADDR");
}
void setvlan(val, d)
const char *val;
int d;
{
struct vlanreq vlr;
if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
!isdigit(ifr.ifr_name[4]))
errx(EXIT_FAILURE,
"``vlan'' valid only with vlan(4) interfaces");