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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
|
/* $OpenBSD: ifconfig.c,v 1.41 2000/11/10 18:18:05 itojun Exp $ */
/* $NetBSD: ifconfig.c,v 1.40 1997/10/01 02:19:43 enami Exp $ */
/*
* 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.
*/
/*-
* Copyright (c) 1997, 1998 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.
*/
#ifndef lint
static char 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
static char rcsid[] = "$OpenBSD: ifconfig.c,v 1.41 2000/11/10 18:18:05 itojun 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 <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet6/nd6.h>
#include <arpa/inet.h>
#include <netinet/ip_ipsp.h>
#include <netinet/if_ether.h>
#include <net/if_enc.h>
#include <net/if_ieee80211.h>
#include <netatalk/at.h>
#define NSIP
#include <netns/ns.h>
#include <netns/ns_if.h>
#define IPXIP
#include <netipx/ipx.h>
#include <netipx/ipx_if.h>
#include <netdb.h>
#define EON
#include <netiso/iso.h>
#include <netiso/iso_var.h>
#include <sys/protosw.h>
#ifndef INET_ONLY
#include <net/if_vlan_var.h>
#endif
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_IFADDRS_H
#include <ifaddrs.h>
#endif
struct ifreq ifr, ridreq;
struct ifaliasreq addreq;
#ifdef INET6
struct in6_ifreq ifr6;
struct in6_ifreq in6_ridreq;
struct in6_aliasreq in6_addreq __attribute__((aligned(4)));
#endif
struct iso_ifreq iso_ridreq;
struct iso_aliasreq iso_addreq;
struct sockaddr_in netmask;
struct netrange at_nr; /* AppleTalk net range */
int ipx_type = ETHERTYPE_II;
char name[30];
int flags, metric, mtu, setaddr, setipdst, doalias;
int clearaddr, s;
int newaddr = 0;
int nsellength = 1;
int af = AF_INET;
int dflag, mflag, lflag, uflag;
int reset_if_flags;
int explicit_prefix = 0;
#ifdef INET6
int Lflag = 1;
#endif
void notealias __P((char *, int));
void notrailers __P((char *, int));
void setifaddr __P((char *, int));
void setifdstaddr __P((char *, int));
void setifflags __P((char *, int));
void setifbroadaddr __P((char *));
void setifipdst __P((char *));
void setifmetric __P((char *));
void setifmtu __P((char *, int));
void setifnwid __P((char *, int));
void setifnetmask __P((char *));
void setifprefixlen __P((char *, int));
void setnsellength __P((char *));
void setsnpaoffset __P((char *));
void setipxframetype __P((char *, int));
void setatrange __P((char *, int));
void setatphase __P((char *, int));
void gifsettunnel __P((char *, char *));
void dstsa __P((char *));
void srcsa __P((char *));
void clearsa __P((char *));
#ifdef INET6
void setia6flags __P((char *, int));
void setia6pltime __P((char *, int));
void setia6vltime __P((char *, int));
void setia6lifetime __P((char *, char *));
#endif
void checkatrange __P ((struct sockaddr_at *));
void setmedia __P((char *, int));
void setmediaopt __P((char *, int));
void unsetmediaopt __P((char *, int));
void setmediainst __P((char *, int));
void setvlantag __P((char *, int));
void setvlandev __P((char *, int));
void unsetvlandev __P((char *, int));
void vlan_status ();
void fixnsel __P((struct sockaddr_iso *));
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 {
char *c_name;
int c_parameter; /* NEXTARG means next argv */
int c_action; /* defered action */
void (*c_func)();
} 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 },
{ "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 },
{ "pltime", NEXTARG, 0, setia6pltime },
{ "vltime", NEXTARG, 0, setia6vltime },
#endif /*INET6*/
#ifndef INET_ONLY
{ "range", NEXTARG, 0, setatrange },
{ "phase", NEXTARG, 0, setatphase },
{ "snpaoffset", NEXTARG, 0, setsnpaoffset },
{ "nsellength", NEXTARG, 0, setnsellength },
{ "802.2", ETHERTYPE_8022, 0, setipxframetype },
{ "802.2tr", ETHERTYPE_8022TR, 0, setipxframetype },
{ "802.3", ETHERTYPE_8023, 0, setipxframetype },
{ "snap", ETHERTYPE_SNAP, 0, setipxframetype },
{ "EtherII", ETHERTYPE_II, 0, setipxframetype },
{ "vlan", NEXTARG, 0, setvlantag },
{ "vlandev", NEXTARG, 0, setvlandev },
{ "-vlandev", 1, 0, unsetvlandev },
#endif /* INET_ONLY */
{ "giftunnel", NEXTARG2, 0, gifsettunnel } ,
{ "dstsa", NEXTARG, 0, dstsa } ,
{ "srcsa", NEXTARG, 0, srcsa } ,
{ "clearsa", NEXTARG, 0, clearsa } ,
{ "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 },
{ NULL, /*src*/ 0, 0, setifaddr },
{ NULL, /*dst*/ 0, 0, setifdstaddr },
{ NULL, /*illegal*/0, 0, NULL },
};
void adjust_nsellength();
int getinfo __P((struct ifreq *));
void getsock __P((int));
void printif __P((struct ifreq *, int));
void printb __P((char *, unsigned short, char *));
void status __P((int));
void usage();
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((const 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_status __P((int));
void in_getaddr __P((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((char *, int));
void in6_getprefix __P((char *, int));
#endif
void at_status __P((int));
void at_getaddr __P((char *, int));
void xns_status __P((int));
void xns_getaddr __P((char *, int));
void ipx_status __P((int));
void ipx_getaddr __P((char *, int));
void iso_status __P((int));
void iso_getaddr __P((char *, int));
void ieee80211_status __P((void));
/* Known address families */
const struct afswtch {
char *af_name;
short af_af;
void (*af_status)();
void (*af_getaddr)();
void (*af_getprefix)();
u_long af_difaddr;
u_long af_aifaddr;
caddr_t af_ridreq;
caddr_t af_addreq;
} afs[] = {
#define C(x) ((caddr_t) &x)
{ "inet", AF_INET, in_status, in_getaddr, NULL,
SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
#ifdef INET6
{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
SIOCDIFADDR_IN6, SIOCAIFADDR_IN6, 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, C(addreq), C(addreq) },
{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
{ "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL,
SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
{ "iso", AF_ISO, iso_status, iso_getaddr, NULL,
SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, C(iso_ridreq), C(iso_addreq) },
#endif /* INET_ONLY */
{ 0, 0, 0, 0 }
};
const struct afswtch *afp; /*the address family being set or asked about*/
int
main(argc, argv)
int argc;
char *argv[];
{
register const struct afswtch *rafp;
int aflag = 0;
int ifaliases = 0;
int i;
if (argc < 2)
usage();
argc--, argv++;
if (!strcmp(*argv, "-a"))
aflag = 1;
else if (!strcmp(*argv, "-A")) {
aflag = 1;
ifaliases = 1;
}
else if (!strcmp(*argv, "-ma") || !strcmp(*argv, "-am")) {
aflag = 1;
mflag = 1;
}
else if (!strcmp(*argv, "-mA") || !strcmp(*argv, "-Am")) {
aflag = 1;
ifaliases = 1;
mflag = 1;
}
else if (!strcmp(*argv, "-m")) {
mflag = 1;
argc--, argv++;
if (argc < 1)
usage();
strncpy(name, *argv, sizeof(name) - 1);
} else
strncpy(name, *argv, sizeof(name) - 1);
argc--, argv++;
if (argc > 0) {
for (afp = rafp = afs; rafp->af_name; rafp++)
if (strcmp(rafp->af_name, *argv) == 0) {
afp = rafp; argc--; argv++;
break;
}
rafp = afp;
af = ifr.ifr_addr.sa_family = rafp->af_af;
}
if (aflag) {
if (argc > 0)
usage();
printif(NULL, ifaliases);
exit(0);
}
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
if (argc == 0) {
printif(&ifr, 1);
exit(0);
}
#ifdef INET6
/* initialization */
in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
#endif
if (getinfo(&ifr) < 0)
exit(1);
while (argc > 0) {
register const struct cmd *p;
for (p = cmds; p->c_name; p++)
if (strcmp(*argv, p->c_name) == 0)
break;
if (p->c_name == 0 && setaddr)
for (i = setaddr; i > 0; i--) {
p++;
if (p->c_func == NULL)
errx(1, "extra address not accepted");
}
if (p->c_func) {
if (p->c_parameter == NEXTARG) {
if (argv[1] == NULL)
errx(1, "'%s' requires argument",
p->c_name);
(*p->c_func)(argv[1]);
argc--, argv++;
} else if (p->c_parameter == NEXTARG2) {
if ((argv[1] == NULL) ||
(argv[2] == NULL))
errx(1, "'%s' requires 2 arguments",
p->c_name);
(*p->c_func)(argv[1], argv[2]);
argc -= 2;
argv += 2;
} else
(*p->c_func)(*argv, p->c_parameter);
actions |= p->c_action;
}
argc--, argv++;
}
/* 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
switch (af) {
case AF_ISO:
adjust_nsellength();
break;
case AF_NS:
if (setipdst) {
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");
}
break;
case AF_IPX:
if (setipdst) {
struct ipxip_req rq;
int size = sizeof(rq);
rq.rq_ipx = addreq.ifra_addr;
rq.rq_ip = addreq.ifra_dstaddr;
if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
warn("encapsulation routing");
}
break;
case AF_APPLETALK:
checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
break;
}
#endif /* INET_ONLY */
if (clearaddr) {
int ret;
strncpy(rafp->af_ridreq, name, sizeof ifr.ifr_name);
if ((ret = ioctl(s, rafp->af_difaddr, rafp->af_ridreq)) < 0) {
if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
/* means no previous address for interface */
} else
warn("SIOCDIFADDR");
}
}
if (newaddr) {
strncpy(rafp->af_addreq, name, sizeof ifr.ifr_name);
if (ioctl(s, rafp->af_aifaddr, rafp->af_addreq) < 0)
warn("SIOCAIFADDR");
}
exit(0);
}
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(ifr)
struct ifreq *ifr;
{
getsock(af);
if (s < 0)
err(1, "socket");
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
warn("SIOCGIFFLAGS");
return (-1);
}
flags = ifr->ifr_flags;
if (ioctl(s, SIOCGIFMETRIC, (caddr_t)ifr) < 0) {
warn("SIOCGIFMETRIC");
metric = 0;
} else
metric = ifr->ifr_metric;
if (ioctl(s, SIOCGIFMTU, (caddr_t)ifr) < 0)
mtu = 0;
else
mtu = ifr->ifr_mtu;
return (0);
}
void
printif(ifrm, ifaliases)
struct ifreq *ifrm;
{
#ifdef HAVE_IFADDRS_H
struct ifaddrs *ifap, *ifa;
const char *namep;
struct ifreq *ifrp;
int count = 0, noinet = 1;
if (getifaddrs(&ifap) != 0)
err(1, "getifaddrs");
namep = NULL;
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifrm && strncmp(ifrm->ifr_name, ifa->ifa_name,
sizeof(ifrm->ifr_name)))
continue;
strncpy(name, ifa->ifa_name, sizeof(name));
name[sizeof(name) - 1] = '\0';
#ifdef INET6
/* quickhack: sizeof(ifr) < sizeof(ifr6) */
if (ifa->ifa_addr->sa_family == AF_INET6) {
ifrp = (struct ifreq *)&ifr6;
memset(&ifr6, 0, sizeof(ifr6));
} else {
ifrp = 𝔦
memset(&ifr, 0, sizeof(ifr));
}
#else
ifrp = 𝔦
memset(&ifr, 0, sizeof(ifr));
#endif
strncpy(ifrp->ifr_name, ifa->ifa_name, sizeof(ifrp->ifr_name));
/* XXX boundary check? */
memcpy(&ifrp->ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
if (ifa->ifa_addr->sa_family == AF_LINK) {
namep = ifa->ifa_name;
if (getinfo(ifrp) < 0)
continue;
status(1);
count++;
noinet = 1;
continue;
}
if (!namep || !strcmp(namep, ifa->ifa_name)) {
register const struct afswtch *p;
if (ifa->ifa_addr->sa_family == AF_INET &&
ifaliases == 0 && noinet == 0)
continue;
if ((p = afp) != NULL) {
if (ifa->ifa_addr->sa_family == p->af_af)
(*p->af_status)(1);
} else {
for (p = afs; p->af_name; p++) {
if (ifa->ifa_addr->sa_family == p->af_af)
(*p->af_status)(0);
}
}
count++;
if (ifa->ifa_addr->sa_family == AF_INET)
noinet = 0;
continue;
}
}
freeifaddrs(ifap);
if (count == 0) {
fprintf(stderr, "%s: no such interface\n", name);
exit(1);
}
#else
char *inbuf = NULL;
struct ifconf ifc;
struct ifreq ifreq, *ifrp;
int i, siz, len = 8192;
int count = 0, noinet = 1;
char ifrbuf[8192];
getsock(af);
if (s < 0)
err(1, "socket");
while (1) {
ifc.ifc_len = len;
ifc.ifc_buf = inbuf = realloc(inbuf, len);
if (inbuf == NULL)
err(1, "malloc");
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
err(1, "SIOCGIFCONF");
if (ifc.ifc_len + sizeof(ifreq) < len)
break;
len *= 2;
}
ifrp = ifc.ifc_req;
ifreq.ifr_name[0] = '\0';
for (i = 0; i < ifc.ifc_len; ) {
ifrp = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
memcpy(ifrbuf, ifrp, sizeof(*ifrp));
siz = ((struct ifreq *)ifrbuf)->ifr_addr.sa_len;
if (siz < sizeof(ifrp->ifr_addr))
siz = sizeof(ifrp->ifr_addr);
siz += sizeof(ifrp->ifr_name);
i += siz;
/* avoid alignment issue */
if (sizeof(ifrbuf) < siz)
errx(1, "ifr too big");
memcpy(ifrbuf, ifrp, siz);
ifrp = (struct ifreq *)ifrbuf;
if (ifrm && strncmp(ifrm->ifr_name, ifrp->ifr_name,
sizeof(ifrp->ifr_name)))
continue;
strncpy(name, ifrp->ifr_name, sizeof(ifrp->ifr_name));
if (ifrp->ifr_addr.sa_family == AF_LINK) {
ifreq = ifr = *ifrp;
if (getinfo(&ifreq) < 0)
continue;
status(1);
count++;
noinet = 1;
continue;
}
if (!strncmp(ifreq.ifr_name, ifrp->ifr_name,
sizeof(ifrp->ifr_name))) {
register const struct afswtch *p;
if (ifrp->ifr_addr.sa_family == AF_INET &&
ifaliases == 0 && noinet == 0)
continue;
ifr = *ifrp;
#ifdef INET6
/* quickhack: sizeof(ifr) < sizeof(ifr6) */
if (ifrp->ifr_addr.sa_family == AF_INET6)
bcopy(ifrp, &ifr6, sizeof(ifr6));
#endif
if ((p = afp) != NULL) {
if (ifr.ifr_addr.sa_family == p->af_af)
(*p->af_status)(1);
} else {
for (p = afs; p->af_name; p++) {
if (ifr.ifr_addr.sa_family == p->af_af)
(*p->af_status)(0);
}
}
count++;
if (ifrp->ifr_addr.sa_family == AF_INET)
noinet = 0;
continue;
}
}
free(inbuf);
if (count == 0) {
fprintf(stderr, "%s: no such interface\n", name);
exit(1);
}
#endif
}
#define RIDADDR 0
#define ADDR 1
#define MASK 2
#define DSTADDR 3
/*ARGSUSED*/
void
setifaddr(addr, param)
char *addr;
int param;
{
/*
* 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++;
newaddr = 1;
if (doalias == 0)
clearaddr = 1;
(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
}
void
gifsettunnel(src, dst)
char *src;
char *dst;
{
struct addrinfo hints, *srcres, *dstres;
struct ifaliasreq addreq;
int ecode;
#ifdef INET6
struct in6_aliasreq in6_addreq;
#endif /* INET6 */
memset(&hints, 0, sizeof(hints));
hints.ai_family = afp->af_af;
if ((ecode = getaddrinfo(src, NULL, &hints, &srcres)) != 0)
errx(1, "error in parsing address string: %s",
gai_strerror(ecode));
if ((ecode = getaddrinfo(dst, NULL, &hints, &dstres)) != 0)
errx(1, "error in parsing address string: %s",
gai_strerror(ecode));
if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
errx(1,
"source and destination address families do not match");
switch (srcres->ai_addr->sa_family)
{
case AF_INET:
bzero(&addreq, sizeof(addreq));
strncpy(addreq.ifra_name, name, IFNAMSIZ);
bcopy(srcres->ai_addr, &addreq.ifra_addr,
srcres->ai_addr->sa_len);
bcopy(dstres->ai_addr, &addreq.ifra_dstaddr,
dstres->ai_addr->sa_len);
if (ioctl(s, SIOCSIFPHYADDR, (struct ifreq *) &addreq) < 0)
warn("SIOCSIFPHYADDR");
break;
#ifdef INET6
case AF_INET6:
bzero(&in6_addreq, sizeof(in6_addreq));
strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
bcopy(srcres->ai_addr, &in6_addreq.ifra_addr,
srcres->ai_addr->sa_len);
bcopy(dstres->ai_addr, &in6_addreq.ifra_dstaddr,
dstres->ai_addr->sa_len);
if (ioctl(s, SIOCSIFPHYADDR_IN6,
(struct ifreq *) &in6_addreq) < 0)
warn("SIOCSIFPHYADDR");
break;
#endif /* INET6 */
default:
warn("address family not supported");
}
freeaddrinfo(srcres);
freeaddrinfo(dstres);
}
static void
handlesa(cmd, sa)
int cmd;
char *sa;
{
char *p1, *p2, *p;
struct ifsa ifsa;
struct addrinfo *res;
struct protoent *prnt;
int ecode;
bzero(&ifsa, sizeof(ifsa));
strlcpy(ifsa.sa_ifname, name, sizeof ifsa.sa_ifname);
p1 = strchr(sa, '/');
if (p1 == NULL)
errx(1, "invalid SA");
else
*(p1++) = '\0';
if (*p1 == '/')
errx(1, "missing SPI");
p2 = strchr(p1, '/');
if (p2 == NULL)
errx(1, "invalid SA");
else
*(p2++) = '\0';
if (*p2 == '\0')
errx(1, "invalid security protocol");
if ((ecode = getaddrinfo(sa, NULL, NULL, &res)) != 0)
errx(1, "error in parsing address string: %s",
gai_strerror(ecode));
bcopy(res->ai_addr, &ifsa.sa_dst, res->ai_addr->sa_len);
freeaddrinfo(res);
ifsa.sa_spi = htonl(strtoul(p1, &p, 16));
if ((p == NULL) || ((*p != '\0') && (*p != '/')))
errx(1, "bad SPI");
ifsa.sa_proto = strtoul(p2, &p, 10);
if ((p == NULL) || (*p != '\0')) {
prnt = getprotobyname(p2);
if (prnt == NULL)
errx(1, "bad security protocol");
ifsa.sa_proto = prnt->p_proto;
}
if (ioctl(s, cmd, (caddr_t)&ifsa) < 0) {
switch (cmd) {
case SIOCSENCDSTSA:
warn("SIOCSENCDSTSA");
break;
case SIOCSENCSRCSA:
warn("SIOCSENCSRCSA");
break;
case SIOCSENCCLEARSA:
warn("SIOCSENCCLEARSA");
break;
}
}
}
void
dstsa(sa)
char *sa;
{
handlesa(SIOCSENCDSTSA, sa);
}
void
srcsa(sa)
char *sa;
{
handlesa(SIOCSENCSRCSA, sa);
}
void
clearsa(sa)
char *sa;
{
handlesa(SIOCSENCCLEARSA, sa);
}
void
setifnetmask(addr)
char *addr;
{
(*afp->af_getaddr)(addr, MASK);
}
void
setifbroadaddr(addr)
char *addr;
{
(*afp->af_getaddr)(addr, DSTADDR);
}
void
setifipdst(addr)
char *addr;
{
in_getaddr(addr, DSTADDR);
setipdst++;
clearaddr = 0;
newaddr = 0;
}
#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
/*ARGSUSED*/
void
notealias(addr, param)
char *addr;
int param;
{
if (setaddr && doalias == 0 && param < 0)
memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
rqtosa(af_addreq)->sa_len);
doalias = param;
if (param < 0) {
clearaddr = 1;
newaddr = 0;
} else
clearaddr = 0;
}
/*ARGSUSED*/
void
notrailers(vname, value)
char *vname;
int value;
{
printf("Note: trailers are no longer sent, but always received\n");
}
/*ARGSUSED*/
void
setifdstaddr(addr, param)
char *addr;
int param;
{
setaddr++;
(*afp->af_getaddr)(addr, DSTADDR);
}
/*
* Note: doing an SIOCIGIFFLAGS scribbles on the union portion
* of the ifreq structure, which may confuse other parts of ifconfig.
* Make a private copy so we can avoid that.
*/
void
setifflags(vname, value)
char *vname;
int value;
{
struct ifreq my_ifr;
bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0)
err(1, "SIOCGIFFLAGS");
strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
flags = my_ifr.ifr_flags;
if (value < 0) {
value = -value;
flags &= ~value;
} else
flags |= value;
my_ifr.ifr_flags = flags;
if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
err(1, "SIOCSIFFLAGS");
}
#ifdef INET6
void
setia6flags(vname, value)
char *vname;
int value;
{
if (value < 0) {
value = -value;
in6_addreq.ifra_flags &= ~value;
} else
in6_addreq.ifra_flags |= value;
}
void
setia6pltime(val, d)
char *val;
int d;
{
setia6lifetime("pltime", val);
}
void
setia6vltime(val, d)
char *val;
int d;
{
setia6lifetime("vltime", val);
}
void
setia6lifetime(cmd, val)
char *cmd;
char *val;
{
time_t newval, t;
char *ep;
t = time(NULL);
newval = (time_t)strtoul(val, &ep, 0);
if (val == ep)
errx(1, "invalid %s", cmd);
if (afp->af_af != AF_INET6)
errx(1, "%s not allowed for the AF", cmd);
if (strcmp(cmd, "vltime") == 0) {
in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
in6_addreq.ifra_lifetime.ia6t_vltime = newval;
} else if (strcmp(cmd, "pltime") == 0) {
in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
in6_addreq.ifra_lifetime.ia6t_pltime = newval;
}
}
#endif
void
setifmetric(val)
char *val;
{
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
ifr.ifr_metric = atoi(val);
if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
warn("SIOCSIFMETRIC");
}
void
setifmtu(val, d)
char *val;
int d;
{
(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
ifr.ifr_mtu = atoi(val);
if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
warn("SIOCSIFMTU");
}
void
setifnwid(val, d)
char *val;
int d;
{
u_int8_t nwid[IEEE80211_NWID_LEN];
memset(&nwid, 0, sizeof(nwid));
(void)strncpy(nwid, val, sizeof(nwid));
(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
ifr.ifr_data = (caddr_t)nwid;
if (ioctl(s, SIOCS80211NWID, (caddr_t)&ifr) < 0)
warn("SIOCS80211NWID");
}
void
ieee80211_status()
{
u_int8_t nwid[IEEE80211_NWID_LEN + 1];
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_data = (caddr_t)nwid;
(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
nwid[IEEE80211_NWID_LEN] = 0;
if (ioctl(s, SIOCG80211NWID, (caddr_t)&ifr) == 0)
printf("\tnwid %s\n", nwid);
}
void
init_current_media()
{
struct ifmediareq ifmr;
/*
* If we have not yet done so, grab the currently-selected
* media.
*/
if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
(void) memset(&ifmr, 0, sizeof(ifmr));
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
/*
* If we get E2BIG, the kernel is telling us
* that there are more, so we can ignore it.
*/
if (errno != E2BIG)
err(1, "SGIOCGIFMEDIA");
}
media_current = ifmr.ifm_current;
}
/* Sanity. */
if (IFM_TYPE(media_current) == 0)
errx(1, "%s: no link type?", name);
}
void
process_media_commands()
{
if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
/* Nothing to do. */
return;
}
/*
* Media already set up, and commands sanity-checked. Set/clear
* any options, and we're ready to go.
*/
media_current |= mediaopt_set;
media_current &= ~mediaopt_clear;
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
ifr.ifr_media = media_current;
if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
err(1, "SIOCSIFMEDIA");
}
void
setmedia(val, d)
char *val;
int d;
{
int type, subtype, inst;
init_current_media();
/* Only one media command may be given. */
if (actions & A_MEDIA)
errx(1, "only one `media' command may be issued");
/* Must not come after mediaopt commands */
if (actions & A_MEDIAOPT)
errx(1, "may not issue `media' after `mediaopt' commands");
/*
* No need to check if `instance' has been issued; setmediainst()
* craps out if `media' has not been specified.
*/
type = IFM_TYPE(media_current);
inst = IFM_INST(media_current);
/* Look up the subtype. */
subtype = get_media_subtype(type, val);
/* Build the new current media word. */
media_current = IFM_MAKEWORD(type, subtype, 0, inst);
/* Media will be set after other processing is complete. */
}
void
setmediaopt(val, d)
char *val;
int d;
{
init_current_media();
/* Can only issue `mediaopt' once. */
if (actions & A_MEDIAOPTSET)
errx(1, "only one `mediaopt' command may be issued");
/* Can't issue `mediaopt' if `instance' has already been issued. */
if (actions & A_MEDIAINST)
errx(1, "may not issue `mediaopt' after `instance'");
mediaopt_set = get_media_options(IFM_TYPE(media_current), val);
/* Media will be set after other processing is complete. */
}
void
unsetmediaopt(val, d)
char *val;
int d;
{
init_current_media();
/* Can only issue `-mediaopt' once. */
if (actions & A_MEDIAOPTCLR)
errx(1, "only one `-mediaopt' command may be issued");
/* May not issue `media' and `-mediaopt'. */
if (actions & A_MEDIA)
errx(1, "may not issue both `media' and `-mediaopt'");
/*
* No need to check for A_MEDIAINST, since the test for A_MEDIA
* implicitly checks for A_MEDIAINST.
*/
mediaopt_clear = get_media_options(IFM_TYPE(media_current), val);
/* Media will be set after other processing is complete. */
}
void
setmediainst(val, d)
char *val;
int d;
{
int type, subtype, options, inst;
init_current_media();
/* Can only issue `instance' once. */
if (actions & A_MEDIAINST)
errx(1, "only one `instance' command may be issued");
/* Must have already specified `media' */
if ((actions & A_MEDIA) == 0)
errx(1, "must specify `media' before `instance'");
type = IFM_TYPE(media_current);
subtype = IFM_SUBTYPE(media_current);
options = IFM_OPTIONS(media_current);
inst = atoi(val);
if (inst < 0 || inst > IFM_INST_MAX)
errx(1, "invalid media instance: %s", val);
media_current = IFM_MAKEWORD(type, subtype, options, inst);
/* Media will be set after other processing is complete. */
}
const struct ifmedia_description ifm_type_descriptions[] =
IFM_TYPE_DESCRIPTIONS;
const struct ifmedia_description ifm_subtype_descriptions[] =
IFM_SUBTYPE_DESCRIPTIONS;
const struct ifmedia_description ifm_option_descriptions[] =
IFM_OPTION_DESCRIPTIONS;
const char *
get_media_type_string(mword)
int mword;
{
const struct ifmedia_description *desc;
for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
desc++) {
if (IFM_TYPE(mword) == desc->ifmt_word)
return (desc->ifmt_string);
}
return ("<unknown type>");
}
const char *
get_media_subtype_string(mword)
int mword;
{
const struct ifmedia_description *desc;
for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
desc++) {
if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword))
return (desc->ifmt_string);
}
return ("<unknown subtype>");
}
int
get_media_subtype(type, val)
int type;
const char *val;
{
int rval;
rval = lookup_media_word(ifm_subtype_descriptions, type, val);
if (rval == -1)
errx(1, "unknown %s media subtype: %s",
get_media_type_string(type), val);
return (rval);
}
int
get_media_options(type, val)
int type;
const char *val;
{
char *optlist, *str;
int option, rval = 0;
/* We muck with the string, so copy it. */
optlist = strdup(val);
if (optlist == NULL)
err(1, "strdup");
str = optlist;
/*
* Look up the options in the user-provided comma-separated list.
*/
for (; (str = strtok(str, ",")) != NULL; str = NULL) {
option = lookup_media_word(ifm_option_descriptions, type, str);
if (option == -1)
errx(1, "unknown %s media option: %s",
get_media_type_string(type), str);
rval |= option;
}
free(optlist);
return (rval);
}
int
lookup_media_word(desc, type, val)
const struct ifmedia_description *desc;
int type;
const char *val;
{
for (; desc->ifmt_string != NULL; desc++) {
if (IFM_TYPE_MATCH(desc->ifmt_word, type) &&
strcasecmp(desc->ifmt_string, val) == 0)
return (desc->ifmt_word);
}
return (-1);
}
void
print_media_word(ifmw, print_type, as_syntax)
int ifmw, print_type, as_syntax;
{
const struct ifmedia_description *desc;
int seen_option = 0;
if (print_type)
printf("%s ", get_media_type_string(ifmw));
printf("%s%s", as_syntax ? "media " : "",
get_media_subtype_string(ifmw));
/* Find options. */
for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
desc++) {
if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
(ifmw & desc->ifmt_word) != 0 &&
(seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
if (seen_option == 0)
printf(" %s", as_syntax ? "mediaopt " : "");
printf("%s%s", seen_option ? "," : "",
desc->ifmt_string);
seen_option |= IFM_OPTIONS(desc->ifmt_word);
}
}
if (IFM_INST(ifmw) != 0)
printf(" instance %d", IFM_INST(ifmw));
}
#define IFFBITS \
"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
static void
phys_status(force)
int force;
{
char psrcaddr[NI_MAXHOST];
char pdstaddr[NI_MAXHOST];
u_long srccmd, dstcmd;
struct ifreq *ifrp;
char *ver = "";
#ifdef NI_WITHSCOPEID
const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
#else
const int niflag = NI_NUMERICHOST;
#endif
#ifdef INET6
struct in6_ifreq in6_ifr;
int s6;
#endif /* INET6 */
force = 0; /*fool gcc*/
psrcaddr[0] = pdstaddr[0] = '\0';
#ifdef INET6
bzero(&in6_ifr, sizeof(in6_ifr));
strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
s6 = socket(AF_INET6, SOCK_DGRAM, 0);
if (s6 < 0) {
ifrp = 𝔦
srccmd = SIOCGIFPSRCADDR;
dstcmd = SIOCGIFPDSTADDR;
} else {
close(s6);
srccmd = SIOCGIFPSRCADDR_IN6;
dstcmd = SIOCGIFPDSTADDR_IN6;
ifrp = (struct ifreq *) &in6_ifr;
}
#else /* INET6 */
ifrp = 𝔦
srccmd = SIOCGIFPSRCADDR;
dstcmd = SIOCGIFPDSTADDR;
#endif /* INET6 */
if (0 <= ioctl(s, srccmd, (caddr_t)ifrp)) {
#ifdef INET6
if (ifrp->ifr_addr.sa_family == AF_INET6)
in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
#endif
getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
psrcaddr, NI_MAXHOST, 0, 0, niflag);
#ifdef INET6
if (ifrp->ifr_addr.sa_family == AF_INET6)
ver = "6";
#endif /* INET6 */
if (0 <= ioctl(s, dstcmd, (caddr_t)ifrp)) {
#ifdef INET6
if (ifrp->ifr_addr.sa_family == AF_INET6) {
in6_fillscopeid((struct sockaddr_in6 *)
&ifrp->ifr_addr);
}
#endif
getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
pdstaddr, NI_MAXHOST, 0, 0, niflag);
}
printf("\tphysical address inet%s %s --> %s\n", ver,
psrcaddr, pdstaddr);
}
}
const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
const struct ifmedia_status_description ifm_status_descriptions[] =
IFM_STATUS_DESCRIPTIONS;
/*
* Print the status of the interface. If an address family was
* specified, show it and it only; otherwise, show them all.
*/
void
status(link)
int link;
{
register const struct afswtch *p = afp;
struct ifmediareq ifmr;
int *media_list, i;
printf("%s: ", name);
printb("flags", flags, IFFBITS);
if (metric)
printf(" metric %d", metric);
if (mtu)
printf(" mtu %d", mtu);
putchar('\n');
#ifndef INET_ONLY
vlan_status();
#endif
ieee80211_status();
(void) memset(&ifmr, 0, sizeof(ifmr));
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
/*
* Interface doesn't support SIOC{G,S}IFMEDIA.
*/
goto proto_status;
}
if (ifmr.ifm_count == 0) {
warnx("%s: no media types?", name);
goto proto_status;
}
media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
if (media_list == NULL)
err(1, "malloc");
ifmr.ifm_ulist = media_list;
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
err(1, "SIOCGIFMEDIA");
printf("\tmedia: ");
print_media_word(ifmr.ifm_current, 1, 0);
if (ifmr.ifm_active != ifmr.ifm_current) {
putchar(' ');
putchar('(');
print_media_word(ifmr.ifm_active, 0, 0);
putchar(')');
}
putchar('\n');
if (ifmr.ifm_status & IFM_AVALID) {
const struct ifmedia_status_description *ifms;
int bitno, found = 0;
printf("\tstatus: ");
for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
for (ifms = ifm_status_descriptions;
ifms->ifms_valid != 0; ifms++) {
if (ifms->ifms_type !=
IFM_TYPE(ifmr.ifm_current) ||
ifms->ifms_valid !=
ifm_status_valid_list[bitno])
continue;
printf("%s%s", found ? ", " : "",
IFM_STATUS_DESC(ifms, ifmr.ifm_status));
found = 1;
/*
* For each valid indicator bit, there's
* only one entry for each media type, so
* terminate the inner loop now.
*/
break;
}
}
if (found == 0)
printf("unknown");
putchar('\n');
}
if (mflag) {
int type, printed_type = 0;
for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
if (IFM_TYPE(media_list[i]) == type) {
if (printed_type == 0) {
printf("\tsupported media:\n");
printed_type = 1;
}
printf("\t\t");
print_media_word(media_list[i], 0, 1);
printf("\n");
}
}
}
}
free(media_list);
proto_status:
if (link == 0) {
if ((p = afp) != NULL) {
(*p->af_status)(1);
} else for (p = afs; p->af_name; p++) {
ifr.ifr_addr.sa_family = p->af_af;
(*p->af_status)(0);
}
}
phys_status(0);
}
void
in_status(force)
int force;
{
struct sockaddr_in *sin, sin2;
char *inet_ntoa();
getsock(AF_INET);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
return;
err(1, "socket");
}
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
sin = (struct sockaddr_in *)&ifr.ifr_addr;
/*
* We keep the interface address and reset it before each
* ioctl() so we can get ifaliases information (as opposed
* to the primary interface netmask/dstaddr/broadaddr, if
* the ifr_addr field is zero).
*/
memcpy(&sin2, &ifr.ifr_addr, sizeof(sin2));
printf("\tinet %s ", inet_ntoa(sin->sin_addr));
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
if (errno != EADDRNOTAVAIL)
warn("SIOCGIFNETMASK");
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
} else
netmask.sin_addr =
((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
if (flags & IFF_POINTOPOINT) {
memcpy(&ifr.ifr_addr, &sin2, sizeof(sin2));
if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL)
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
else
warn("SIOCGIFDSTADDR");
}
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
sin = (struct sockaddr_in *)&ifr.ifr_dstaddr;
printf("--> %s ", inet_ntoa(sin->sin_addr));
}
printf("netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
if (flags & IFF_BROADCAST) {
memcpy(&ifr.ifr_addr, &sin2, sizeof(sin2));
if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL)
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
else
warn("SIOCGIFBRDADDR");
}
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
sin = (struct sockaddr_in *)&ifr.ifr_addr;
if (sin->sin_addr.s_addr != 0)
printf("broadcast %s", inet_ntoa(sin->sin_addr));
}
putchar('\n');
}
void
setifprefixlen(addr, d)
char *addr;
int d;
{
if (*afp->af_getprefix)
(*afp->af_getprefix)(addr, MASK);
explicit_prefix = 1;
}
#ifdef INET6
void
in6_fillscopeid(sin6)
struct sockaddr_in6 *sin6;
{
#if defined(__KAME__) && defined(KAME_SCOPEID)
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
sin6->sin6_scope_id =
ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
}
#endif
}
/* XXX not really an alias */
void
in6_alias(creq)
struct in6_ifreq *creq;
{
struct sockaddr_in6 *sin6;
struct in6_ifreq ifr6; /* shadows file static variable */
u_int32_t scopeid;
char hbuf[NI_MAXHOST];
#ifdef NI_WITHSCOPEID
const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
#else
const int niflag = NI_NUMERICHOST;
#endif
/* Get the non-alias address for this interface. */
getsock(AF_INET6);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
return;
err(1, "socket");
}
sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
in6_fillscopeid(sin6);
scopeid = sin6->sin6_scope_id;
if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
hbuf, sizeof(hbuf), NULL, 0, niflag) != 0)
strcpy(hbuf, "");
printf("\tinet6 %s", hbuf);
if (flags & IFF_POINTOPOINT) {
(void) memset(&ifr6, 0, sizeof(ifr6));
(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
ifr6.ifr_addr = creq->ifr_addr;
if (ioctl(s, SIOCGIFDSTADDR_IN6, (caddr_t)&ifr6) < 0) {
if (errno != EADDRNOTAVAIL)
warn("SIOCGIFDSTADDR_IN6");
(void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
ifr6.ifr_addr.sin6_family = AF_INET6;
ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
}
sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
in6_fillscopeid(sin6);
if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
hbuf, sizeof(hbuf), NULL, 0, niflag) != 0)
strcpy(hbuf, "");
printf(" -> %s", hbuf);
}
(void) memset(&ifr6, 0, sizeof(ifr6));
(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
ifr6.ifr_addr = creq->ifr_addr;
if (ioctl(s, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) {
if (errno != EADDRNOTAVAIL)
warn("SIOCGIFNETMASK_IN6");
} else {
sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
printf(" prefixlen %d", prefix(&sin6->sin6_addr,
sizeof(struct in6_addr)));
}
(void) memset(&ifr6, 0, sizeof(ifr6));
(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
ifr6.ifr_addr = creq->ifr_addr;
if (ioctl(s, SIOCGIFAFLAG_IN6, (caddr_t)&ifr6) < 0) {
if (errno != EADDRNOTAVAIL)
warn("SIOCGIFAFLAG_IN6");
} else {
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
printf(" anycast");
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
printf(" tentative");
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
printf(" duplicated");
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
printf(" detached");
if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
printf(" deprecated");
}
if (scopeid)
printf(" scopeid 0x%x", scopeid);
if (Lflag) {
struct in6_addrlifetime *lifetime;
(void) memset(&ifr6, 0, sizeof(ifr6));
(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
ifr6.ifr_addr = creq->ifr_addr;
lifetime = &ifr6.ifr_ifru.ifru_lifetime;
if (ioctl(s, SIOCGIFALIFETIME_IN6, (caddr_t)&ifr6) < 0) {
if (errno != EADDRNOTAVAIL)
warn("SIOCGIFALIFETIME_IN6");
} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
time_t t = time(NULL);
printf(" pltime ");
if (lifetime->ia6t_preferred) {
printf("%s", lifetime->ia6t_preferred < t
? "0"
: sec2str(lifetime->ia6t_preferred - t));
} else
printf("infty");
printf(" vltime ");
if (lifetime->ia6t_expire) {
printf("%s", lifetime->ia6t_expire < t
? "0"
: sec2str(lifetime->ia6t_expire - t));
} else
printf("infty");
}
}
printf("\n");
}
void
in6_status(force)
int force;
{
in6_alias((struct in6_ifreq *)&ifr6);
}
#endif /*INET6*/
#ifndef INET_ONLY
void
at_status(force)
int force;
{
struct sockaddr_at *sat, null_sat;
struct netrange *nr;
getsock(AF_APPLETALK);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
return;
err(1, "socket");
}
(void) memset(&ifr, 0, sizeof(ifr));
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
if (!force)
return;
(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
} else
warn("SIOCGIFADDR");
}
(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
sat = (struct sockaddr_at *)&ifr.ifr_addr;
(void) memset(&null_sat, 0, sizeof(null_sat));
nr = (struct netrange *) &sat->sat_zero;
printf("\tAppleTalk %d.%d range %d-%d phase %d",
ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
if (flags & IFF_POINTOPOINT) {
if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL)
(void) memset(&ifr.ifr_addr, 0,
sizeof(ifr.ifr_addr));
else
warn("SIOCGIFDSTADDR");
}
(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
if (!sat)
sat = &null_sat;
printf("--> %d.%d",
ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
}
if (flags & IFF_BROADCAST) {
/* note RTAX_BRD overlap with IFF_POINTOPOINT */
sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
if (sat)
printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
sat->sat_addr.s_node);
}
putchar('\n');
}
void
xns_status(force)
int force;
{
struct sockaddr_ns *sns;
getsock(AF_NS);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
return;
err(1, "socket");
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
if (!force)
return;
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
} else
warn("SIOCGIFADDR");
}
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
sns = (struct sockaddr_ns *)&ifr.ifr_addr;
printf("\tns %s ", ns_ntoa(sns->sns_addr));
if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL)
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
else
warn("SIOCGIFDSTADDR");
}
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
printf("--> %s ", ns_ntoa(sns->sns_addr));
}
putchar('\n');
}
void
setipxframetype(vname, type)
char *vname;
int type;
{
ipx_type = type;
}
void
ipx_status(force)
int force;
{
struct sockaddr_ipx *sipx;
getsock(AF_IPX);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
return;
err(1, "socket");
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
if (!force)
return;
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
} else
warn("SIOCGIFADDR");
}
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL)
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
else
warn("SIOCGIFDSTADDR");
}
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
sipx = (struct sockaddr_ipx *)&ifr.ifr_dstaddr;
printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
}
{
struct frame_types {
int type;
char *name;
} *p, frames[] = {
{ ETHERTYPE_8022, "802.2" },
{ ETHERTYPE_8022TR, "802.2tr" },
{ ETHERTYPE_8023, "802.3" },
{ ETHERTYPE_SNAP, "SNAP" },
{ ETHERTYPE_II, "EtherII" },
{ 0, NULL }
};
for (p = frames; p->name && p->type != sipx->sipx_type; p++);
if (p->name != NULL)
printf("frame %s ", p->name);
}
putchar('\n');
}
void
iso_status(force)
int force;
{
struct sockaddr_iso *siso;
struct iso_ifreq ifr;
getsock(AF_ISO);
if (s < 0) {
if (errno == EPROTONOSUPPORT)
return;
err(1, "socket");
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
if (!force)
return;
memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
} else
warn("SIOCGIFADDR_ISO");
}
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
siso = &ifr.ifr_Addr;
printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL)
memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
else
warn("SIOCGIFNETMASK_ISO");
} else {
printf(" netmask %s ", iso_ntoa(&siso->siso_addr));
}
if (flags & IFF_POINTOPOINT) {
if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) {
if (errno == EADDRNOTAVAIL)
memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
else
warn("SIOCGIFDSTADDR_ISO");
}
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
siso = &ifr.ifr_Addr;
printf("--> %s ", iso_ntoa(&siso->siso_addr));
}
putchar('\n');
}
#endif /* INET_ONLY */
struct in_addr inet_makeaddr();
#define SIN(x) ((struct sockaddr_in *) &(x))
struct sockaddr_in *sintab[] = {
SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
void
in_getaddr(s, which)
char *s;
int which;
{
register struct sockaddr_in *sin = sintab[which];
struct hostent *hp;
struct netent *np;
sin->sin_len = sizeof(*sin);
if (which != MASK)
sin->sin_family = AF_INET;
if (inet_aton(s, &sin->sin_addr) == 0) {
if ((hp = gethostbyname(s)))
memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
else if ((np = getnetbyname(s)))
sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
else
errx(1, "%s: bad value", s);
}
}
/*
* Print a value a la the %b format of the kernel's printf
*/
void
printb(s, v, bits)
char *s;
register char *bits;
register unsigned short v;
{
register int i, any = 0;
register char c;
if (bits && *bits == 8)
printf("%s=%o", s, v);
else
printf("%s=%x", s, v);
bits++;
if (bits) {
putchar('<');
while ((i = *bits++)) {
if (v & (1 << (i-1))) {
if (any)
putchar(',');
any = 1;
for (; (c = *bits) > 32; bits++)
putchar(c);
} else
for (; *bits > 32; bits++)
;
}
putchar('>');
}
}
#ifdef INET6
#define SIN6(x) ((struct sockaddr_in6 *) &(x))
struct sockaddr_in6 *sin6tab[] = {
SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
void
in6_getaddr(s, which)
char *s;
int which;
{
#ifndef KAME_SCOPEID
struct sockaddr_in6 *sin6 = sin6tab[which];
sin->sin6_len = sizeof(*sin6);
if (which != MASK)
sin6->sin6_family = AF_INET6;
if (inet_pton(AF_INET6, s, &sin6->sin6_addr) != 1)
errx(1, "%s: bad value", s);
#else
struct sockaddr_in6 *sin6 = sin6tab[which];
struct addrinfo hints, *res;
int error;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
error = getaddrinfo(s, "0", &hints, &res);
if (error)
errx(1, "%s: %s", s, gai_strerror(error));
if (res->ai_addrlen != sizeof(struct sockaddr_in6))
errx(1, "%s: bad value", s);
memcpy(sin6, res->ai_addr, res->ai_addrlen);
#ifdef __KAME__
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] == 0 &&
sin6->sin6_scope_id) {
*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
htons(sin6->sin6_scope_id & 0xffff);
sin6->sin6_scope_id = 0;
}
#endif
freeaddrinfo(res);
#endif
}
void
in6_getprefix(plen, which)
char *plen;
int which;
{
register struct sockaddr_in6 *sin = sin6tab[which];
register u_char *cp;
int len = strtol(plen, (char **)NULL, 10);
if ((len < 0) || (len > 128))
errx(1, "%s: bad value", plen);
sin->sin6_len = sizeof(*sin);
if (which != MASK)
sin->sin6_family = AF_INET6;
if ((len == 0) || (len == 128)) {
memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
return;
}
memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
*cp++ = 0xff;
*cp = 0xff << (8 - len);
}
int
prefix(val, size)
void *val;
int size;
{
register u_char *name = (u_char *)val;
register int byte, bit, plen = 0;
for (byte = 0; byte < size; byte++, plen += 8)
if (name[byte] != 0xff)
break;
if (byte == size)
return (plen);
for (bit = 7; bit != 0; bit--, plen++)
if (!(name[byte] & (1 << bit)))
break;
for (; bit != 0; bit--)
if (name[byte] & (1 << bit))
return(0);
byte++;
for (; byte < size; byte++)
if (name[byte])
return(0);
return (plen);
}
#endif /*INET6*/
#ifndef INET_ONLY
void
at_getaddr(addr, which)
char *addr;
int which;
{
struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
u_int net, node;
sat->sat_family = AF_APPLETALK;
sat->sat_len = sizeof(*sat);
if (which == MASK)
errx(1, "AppleTalk does not use netmasks");
if (sscanf(addr, "%u.%u", &net, &node) != 2 ||
net == 0 || net > 0xffff || node == 0 || node > 0xfe)
errx(1, "%s: illegal address", addr);
sat->sat_addr.s_net = htons(net);
sat->sat_addr.s_node = node;
}
void
setatrange(range, d)
char *range;
int d;
{
u_short first = 123, last = 123;
if (sscanf(range, "%hu-%hu", &first, &last) != 2 ||
first == 0 || first > 0xffff ||
last == 0 || last > 0xffff || first > last)
errx(1, "%s: illegal net range: %u-%u", range, first, last);
at_nr.nr_firstnet = htons(first);
at_nr.nr_lastnet = htons(last);
}
void
setatphase(phase, d)
char *phase;
int d;
{
if (!strcmp(phase, "1"))
at_nr.nr_phase = 1;
else if (!strcmp(phase, "2"))
at_nr.nr_phase = 2;
else
errx(1, "%s: illegal phase", phase);
}
void
checkatrange(sat)
struct sockaddr_at *sat;
{
if (at_nr.nr_phase == 0)
at_nr.nr_phase = 2; /* Default phase 2 */
if (at_nr.nr_firstnet == 0) /* Default range of one */
at_nr.nr_firstnet = at_nr.nr_lastnet = sat->sat_addr.s_net;
printf("\tatalk %d.%d range %d-%d phase %d\n",
ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
if ((u_short) ntohs(at_nr.nr_firstnet) >
(u_short) ntohs(sat->sat_addr.s_net) ||
(u_short) ntohs(at_nr.nr_lastnet) <
(u_short) ntohs(sat->sat_addr.s_net))
errx(1, "AppleTalk address is not in range");
*((struct netrange *) &sat->sat_zero) = at_nr;
}
#define SNS(x) ((struct sockaddr_ns *) &(x))
struct sockaddr_ns *snstab[] = {
SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
void
xns_getaddr(addr, which)
char *addr;
int which;
{
struct sockaddr_ns *sns = snstab[which];
struct ns_addr ns_addr();
sns->sns_family = AF_NS;
sns->sns_len = sizeof(*sns);
sns->sns_addr = ns_addr(addr);
if (which == MASK)
printf("Attempt to set XNS netmask will be ineffectual\n");
}
#define SIPX(x) ((struct sockaddr_ipx *) &(x))
struct sockaddr_ipx *sipxtab[] = {
SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
void
ipx_getaddr(addr, which)
char *addr;
int which;
{
struct sockaddr_ipx *sipx = sipxtab[which];
struct ipx_addr ipx_addr();
sipx->sipx_family = AF_IPX;
sipx->sipx_len = sizeof(*sipx);
sipx->sipx_addr = ipx_addr(addr);
sipx->sipx_type = ipx_type;
if (which == MASK)
printf("Attempt to set IPX netmask will be ineffectual\n");
}
#define SISO(x) ((struct sockaddr_iso *) &(x))
struct sockaddr_iso *sisotab[] = {
SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
void
iso_getaddr(addr, which)
char *addr;
int which;
{
register struct sockaddr_iso *siso = sisotab[which];
struct iso_addr *iso_addr();
siso->siso_addr = *iso_addr(addr);
if (which == MASK) {
siso->siso_len = TSEL(siso) - (caddr_t)(siso);
siso->siso_nlen = 0;
} else {
siso->siso_len = sizeof(*siso);
siso->siso_family = AF_ISO;
}
}
void
setsnpaoffset(val)
char *val;
{
iso_addreq.ifra_snpaoffset = atoi(val);
}
void
setnsellength(val)
char *val;
{
nsellength = atoi(val);
if (nsellength < 0)
errx(1, "negative NSEL length is absurd");
if (afp == 0 || afp->af_af != AF_ISO)
errx(1, "setting NSEL length valid only for iso");
}
void
fixnsel(s)
register struct sockaddr_iso *s;
{
if (s->siso_family == 0)
return;
s->siso_tlen = nsellength;
}
void
adjust_nsellength()
{
fixnsel(sisotab[RIDADDR]);
fixnsel(sisotab[ADDR]);
fixnsel(sisotab[DSTADDR]);
}
#endif /* INET_ONLY */
void
usage()
{
fprintf(stderr, "usage: ifconfig [ -m ] [ -a ] [ -A ] [ interface ]\n"
"\t[ [af] [ address [ dest_addr ] ] [ up ] [ down ] "
"[ netmask mask ] ]\n"
"\t[media media_type] [mediaopt media_option]\n"
"\t[ metric n ]\n"
"\t[ mtu n ]\n"
"\t[ nwid netword_id ]\n"
"\t[ dstsa address/spi/protocol ]\n"
"\t[ srcsa address/spi/protocol ]\n"
"\t[ clearsa address/spi/protocol ]\n"
"\t[ giftunnel srcaddress dstaddress ]\n"
"\t[ vlan n vlandev interface ]\n"
"\t[ arp | -arp ]\n"
"\t[ -802.2 | -802.3 | -802.2tr | -snap | -EtherII ]\n"
"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
" ifconfig [-a | -A | -am | -Am] [ af ]\n"
" ifconfig -m interface [af]\n");
exit(1);
}
#ifndef INET_ONLY
static int __tag = 0;
static int __have_tag = 0;
void vlan_status()
{
struct vlanreq vreq;
bzero((char *)&vreq, sizeof(struct vlanreq));
ifr.ifr_data = (caddr_t)&vreq;
if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
return;
if (vreq.vlr_tag || (vreq.vlr_parent[0] != '\0'))
printf("\tvlan: %d parent interface: %s\n",
vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ?
"<none>" : vreq.vlr_parent);
return;
}
void setvlantag(val, d)
char *val;
int d;
{
u_int16_t tag;
struct vlanreq vreq;
__tag = tag = atoi(val);
__have_tag = 1;
bzero((char *)&vreq, sizeof(struct vlanreq));
ifr.ifr_data = (caddr_t)&vreq;
if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCGETVLAN");
vreq.vlr_tag = tag;
if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCSETVLAN");
return;
}
void setvlandev(val, d)
char *val;
int d;
{
struct vlanreq vreq;
if (!__have_tag)
errx(1, "must specify both vlan tag and device");
bzero((char *)&vreq, sizeof(struct vlanreq));
ifr.ifr_data = (caddr_t)&vreq;
if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCGETVLAN");
strncpy(vreq.vlr_parent, val, sizeof(vreq.vlr_parent));
vreq.vlr_tag = __tag;
if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCSETVLAN");
return;
}
void unsetvlandev(val, d)
char *val;
int d;
{
struct vlanreq vreq;
bzero((char *)&vreq, sizeof(struct vlanreq));
ifr.ifr_data = (caddr_t)&vreq;
if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCGETVLAN");
bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
vreq.vlr_tag = 0;
if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCSETVLAN");
return;
}
#endif /* INET_ONLY */
#ifdef INET6
char *
sec2str(total)
time_t total;
{
static char result[256];
int days, hours, mins, secs;
int first = 1;
char *p = result;
char *end = &result[sizeof(result)];
if (0) { /*XXX*/
days = total / 3600 / 24;
hours = (total / 3600) % 24;
mins = (total / 60) % 60;
secs = total % 60;
if (days) {
first = 0;
p += snprintf(p, end - p, "%dd", days);
}
if (!first || hours) {
first = 0;
p += snprintf(p, end - p, "%dh", hours);
}
if (!first || mins) {
first = 0;
p += snprintf(p, end - p, "%dm", mins);
}
snprintf(p, end - p, "%ds", secs);
} else
snprintf(p, end - p, "%lu", (u_long)total);
return(result);
}
#endif
|