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
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
* Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2022 Oxide Computer Company
*/
#include "defs.h"
#include "tables.h"
#include <fcntl.h>
#include <sys/un.h>
static void initlog(void);
static void run_timeouts(void);
static void advertise(struct sockaddr_in6 *sin6, struct phyint *pi,
boolean_t no_prefixes);
static void solicit(struct sockaddr_in6 *sin6, struct phyint *pi);
static void initifs(boolean_t first);
static void check_if_removed(struct phyint *pi);
static void loopback_ra_enqueue(struct phyint *pi,
struct nd_router_advert *ra, int len);
static void loopback_ra_dequeue(void);
static void check_daemonize(void);
struct in6_addr all_nodes_mcast = { { 0xff, 0x2, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x1 } };
struct in6_addr all_routers_mcast = { { 0xff, 0x2, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x2 } };
static struct sockaddr_in6 v6allnodes = { AF_INET6, 0, 0,
{ 0xff, 0x2, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x1 } };
static struct sockaddr_in6 v6allrouters = { AF_INET6, 0, 0,
{ 0xff, 0x2, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x2 } };
static char **argv0; /* Saved for re-exec on SIGHUP */
static uint64_t packet[(IP_MAXPACKET + 1)/8];
static int show_ifs = 0;
static boolean_t already_daemonized = _B_FALSE;
int debug = 0;
int no_loopback = 0; /* Do not send RA packets to ourselves */
/*
* Size of routing socket message used by in.ndpd which includes the header,
* space for the RTA_DST, RTA_GATEWAY and RTA_NETMASK (each a sockaddr_in6)
* plus space for the RTA_IFP (a sockaddr_dl).
*/
#define NDP_RTM_MSGLEN sizeof (struct rt_msghdr) + \
sizeof (struct sockaddr_in6) + \
sizeof (struct sockaddr_in6) + \
sizeof (struct sockaddr_in6) + \
sizeof (struct sockaddr_dl)
/*
* These are referenced externally in tables.c in order to fill in the
* dynamic portions of the routing socket message and then to send the message
* itself.
*/
int rtsock = -1; /* Routing socket */
struct rt_msghdr *rt_msg; /* Routing socket message */
struct sockaddr_in6 *rta_gateway; /* RTA_GATEWAY sockaddr */
struct sockaddr_dl *rta_ifp; /* RTA_IFP sockaddr */
/*
* These sockets are used internally in this file.
*/
static int mibsock = -1; /* mib request socket */
static int cmdsock = -1; /* command socket */
static int ndpd_setup_cmd_listener(void);
static void ndpd_cmd_handler(int);
static int ndpd_process_cmd(int, ipadm_ndpd_msg_t *);
static int ndpd_send_error(int, int);
static int ndpd_set_autoconf(const char *, boolean_t);
static int ndpd_create_addrs(const char *, struct sockaddr_in6, int,
boolean_t, boolean_t, char *);
static int ndpd_delete_addrs(const char *);
static int phyint_check_ipadm_intfid(struct phyint *);
/*
* Return the current time in milliseconds truncated to
* fit in an integer.
*/
uint_t
getcurrenttime(void)
{
struct timeval tp;
if (gettimeofday(&tp, NULL) < 0) {
logperror("getcurrenttime: gettimeofday failed");
exit(1);
}
return (tp.tv_sec * 1000 + tp.tv_usec / 1000);
}
/*
* Output a preformated packet from the packet[] buffer.
*/
static void
sendpacket(struct sockaddr_in6 *sin6, int sock, int size, int flags)
{
int cc;
char abuf[INET6_ADDRSTRLEN];
cc = sendto(sock, (char *)packet, size, flags,
(struct sockaddr *)sin6, sizeof (*sin6));
if (cc < 0 || cc != size) {
if (cc < 0) {
logperror("sendpacket: sendto");
}
logmsg(LOG_ERR, "sendpacket: wrote %s %d chars, ret=%d\n",
inet_ntop(sin6->sin6_family,
(void *)&sin6->sin6_addr,
abuf, sizeof (abuf)),
size, cc);
}
}
/*
* If possible, place an ND_OPT_SOURCE_LINKADDR option at `optp'.
* Return the number of bytes placed in the option.
*/
static uint_t
add_opt_lla(struct phyint *pi, struct nd_opt_lla *optp)
{
uint_t optlen;
uint_t hwaddrlen;
struct lifreq lifr;
/* If this phyint doesn't have a link-layer address, bail */
if (phyint_get_lla(pi, &lifr) == -1)
return (0);
hwaddrlen = lifr.lifr_nd.lnr_hdw_len;
/* roundup to multiple of 8 and make padding zero */
optlen = ((sizeof (struct nd_opt_hdr) + hwaddrlen + 7) / 8) * 8;
bzero(optp, optlen);
optp->nd_opt_lla_type = ND_OPT_SOURCE_LINKADDR;
optp->nd_opt_lla_len = optlen / 8;
bcopy(lifr.lifr_nd.lnr_hdw_addr, optp->nd_opt_lla_hdw_addr, hwaddrlen);
return (optlen);
}
/* Send a Router Solicitation */
static void
solicit(struct sockaddr_in6 *sin6, struct phyint *pi)
{
int packetlen = 0;
struct nd_router_solicit *rs = (struct nd_router_solicit *)packet;
char *pptr = (char *)packet;
rs->nd_rs_type = ND_ROUTER_SOLICIT;
rs->nd_rs_code = 0;
rs->nd_rs_cksum = htons(0);
rs->nd_rs_reserved = htonl(0);
packetlen += sizeof (*rs);
pptr += sizeof (*rs);
/* add options */
packetlen += add_opt_lla(pi, (struct nd_opt_lla *)pptr);
if (debug & D_PKTOUT) {
print_route_sol("Sending solicitation to ", pi, rs, packetlen,
sin6);
}
sendpacket(sin6, pi->pi_sock, packetlen, 0);
}
/*
* Send a (set of) Router Advertisements and feed them back to ourselves
* for processing. Unless no_prefixes is set all prefixes are included.
* If there are too many prefix options to fit in one packet multiple
* packets will be sent - each containing a subset of the prefix options.
*/
static void
advertise(struct sockaddr_in6 *sin6, struct phyint *pi, boolean_t no_prefixes)
{
struct nd_opt_prefix_info *po;
char *pptr = (char *)packet;
struct nd_router_advert *ra;
struct adv_prefix *adv_pr;
int packetlen = 0;
ra = (struct nd_router_advert *)pptr;
ra->nd_ra_type = ND_ROUTER_ADVERT;
ra->nd_ra_code = 0;
ra->nd_ra_cksum = htons(0);
ra->nd_ra_curhoplimit = pi->pi_AdvCurHopLimit;
ra->nd_ra_flags_reserved = 0;
if (pi->pi_AdvManagedFlag)
ra->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
if (pi->pi_AdvOtherConfigFlag)
ra->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER;
if (pi->pi_adv_state == FINAL_ADV)
ra->nd_ra_router_lifetime = htons(0);
else
ra->nd_ra_router_lifetime = htons(pi->pi_AdvDefaultLifetime);
ra->nd_ra_reachable = htonl(pi->pi_AdvReachableTime);
ra->nd_ra_retransmit = htonl(pi->pi_AdvRetransTimer);
packetlen = sizeof (*ra);
pptr += sizeof (*ra);
if (pi->pi_adv_state == FINAL_ADV) {
if (debug & D_PKTOUT) {
print_route_adv("Sending advert (FINAL) to ", pi,
ra, packetlen, sin6);
}
sendpacket(sin6, pi->pi_sock, packetlen, 0);
/* Feed packet back in for router operation */
loopback_ra_enqueue(pi, ra, packetlen);
return;
}
/* add options */
packetlen += add_opt_lla(pi, (struct nd_opt_lla *)pptr);
pptr = (char *)packet + packetlen;
if (pi->pi_AdvLinkMTU != 0) {
struct nd_opt_mtu *mo = (struct nd_opt_mtu *)pptr;
mo->nd_opt_mtu_type = ND_OPT_MTU;
mo->nd_opt_mtu_len = sizeof (struct nd_opt_mtu) / 8;
mo->nd_opt_mtu_reserved = 0;
mo->nd_opt_mtu_mtu = htonl(pi->pi_AdvLinkMTU);
packetlen += sizeof (struct nd_opt_mtu);
pptr += sizeof (struct nd_opt_mtu);
}
if (no_prefixes) {
if (debug & D_PKTOUT) {
print_route_adv("Sending advert to ", pi,
ra, packetlen, sin6);
}
sendpacket(sin6, pi->pi_sock, packetlen, 0);
/* Feed packet back in for router operation */
loopback_ra_enqueue(pi, ra, packetlen);
return;
}
po = (struct nd_opt_prefix_info *)pptr;
for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL;
adv_pr = adv_pr->adv_pr_next) {
if (!adv_pr->adv_pr_AdvOnLinkFlag &&
!adv_pr->adv_pr_AdvAutonomousFlag) {
continue;
}
/*
* If the prefix doesn't fit in packet send
* what we have so far and start with new packet.
*/
if (packetlen + sizeof (*po) >
pi->pi_LinkMTU - sizeof (struct ip6_hdr)) {
if (debug & D_PKTOUT) {
print_route_adv("Sending advert "
"(FRAG) to ",
pi, ra, packetlen, sin6);
}
sendpacket(sin6, pi->pi_sock, packetlen, 0);
/* Feed packet back in for router operation */
loopback_ra_enqueue(pi, ra, packetlen);
packetlen = sizeof (*ra);
pptr = (char *)packet + sizeof (*ra);
po = (struct nd_opt_prefix_info *)pptr;
}
po->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
po->nd_opt_pi_len = sizeof (*po)/8;
po->nd_opt_pi_flags_reserved = 0;
if (adv_pr->adv_pr_AdvOnLinkFlag) {
po->nd_opt_pi_flags_reserved |=
ND_OPT_PI_FLAG_ONLINK;
}
if (adv_pr->adv_pr_AdvAutonomousFlag) {
po->nd_opt_pi_flags_reserved |=
ND_OPT_PI_FLAG_AUTO;
}
po->nd_opt_pi_prefix_len = adv_pr->adv_pr_prefix_len;
/*
* If both Adv*Expiration and Adv*Lifetime are
* set we prefer the former and make the lifetime
* decrement in real time.
*/
if (adv_pr->adv_pr_AdvValidRealTime) {
po->nd_opt_pi_valid_time =
htonl(adv_pr->adv_pr_AdvValidExpiration);
} else {
po->nd_opt_pi_valid_time =
htonl(adv_pr->adv_pr_AdvValidLifetime);
}
if (adv_pr->adv_pr_AdvPreferredRealTime) {
po->nd_opt_pi_preferred_time =
htonl(adv_pr->adv_pr_AdvPreferredExpiration);
} else {
po->nd_opt_pi_preferred_time =
htonl(adv_pr->adv_pr_AdvPreferredLifetime);
}
po->nd_opt_pi_reserved2 = htonl(0);
po->nd_opt_pi_prefix = adv_pr->adv_pr_prefix;
po++;
packetlen += sizeof (*po);
}
if (debug & D_PKTOUT) {
print_route_adv("Sending advert to ", pi,
ra, packetlen, sin6);
}
sendpacket(sin6, pi->pi_sock, packetlen, 0);
/* Feed packet back in for router operation */
loopback_ra_enqueue(pi, ra, packetlen);
}
/* Poll support */
static int pollfd_num = 0; /* Allocated and initialized */
static struct pollfd *pollfds = NULL;
/*
* Add fd to the set being polled. Returns 0 if ok; -1 if failed.
*/
int
poll_add(int fd)
{
int i;
int new_num;
struct pollfd *newfds;
/* Check if already present */
for (i = 0; i < pollfd_num; i++) {
if (pollfds[i].fd == fd)
return (0);
}
/* Check for empty spot already present */
for (i = 0; i < pollfd_num; i++) {
if (pollfds[i].fd == -1) {
pollfds[i].fd = fd;
return (0);
}
}
/* Allocate space for 32 more fds and initialize to -1 */
new_num = pollfd_num + 32;
newfds = realloc(pollfds, new_num * sizeof (struct pollfd));
if (newfds == NULL) {
logperror("realloc");
return (-1);
}
newfds[pollfd_num].fd = fd;
newfds[pollfd_num++].events = POLLIN;
for (i = pollfd_num; i < new_num; i++) {
newfds[i].fd = -1;
newfds[i].events = POLLIN;
}
pollfd_num = new_num;
pollfds = newfds;
return (0);
}
/*
* Remove fd from the set being polled. Returns 0 if ok; -1 if failed.
*/
int
poll_remove(int fd)
{
int i;
/* Check if already present */
for (i = 0; i < pollfd_num; i++) {
if (pollfds[i].fd == fd) {
pollfds[i].fd = -1;
return (0);
}
}
return (-1);
}
/*
* Extract information about the ifname (either a physical interface and
* the ":0" logical interface or just a logical interface).
* If the interface (still) exists in kernel set pr_in_use
* for caller to be able to detect interfaces that are removed.
* Starts sending advertisements/solicitations when new physical interfaces
* are detected.
*/
static void
if_process(int s, char *ifname, boolean_t first)
{
struct lifreq lifr;
struct phyint *pi;
struct prefix *pr;
char *cp;
char phyintname[LIFNAMSIZ + 1];
if (debug & D_IFSCAN)
logmsg(LOG_DEBUG, "if_process(%s)\n", ifname);
(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
if (ioctl(s, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
if (errno == ENXIO) {
/*
* Interface has disappeared
*/
return;
}
logperror("if_process: ioctl (get interface flags)");
return;
}
/*
* Ignore loopback, point-to-multipoint and VRRP interfaces.
* The IP addresses over VRRP interfaces cannot be auto-configured.
* Point-to-point interfaces always have IFF_MULTICAST set.
*/
if (!(lifr.lifr_flags & IFF_MULTICAST) ||
(lifr.lifr_flags & (IFF_LOOPBACK|IFF_VRRP))) {
return;
}
if (!(lifr.lifr_flags & IFF_IPV6))
return;
(void) strncpy(phyintname, ifname, sizeof (phyintname));
phyintname[sizeof (phyintname) - 1] = '\0';
if ((cp = strchr(phyintname, IF_SEPARATOR)) != NULL) {
*cp = '\0';
}
pi = phyint_lookup(phyintname);
if (pi == NULL) {
pi = phyint_create(phyintname);
if (pi == NULL) {
logmsg(LOG_ERR, "if_process: out of memory\n");
return;
}
/*
* if in.ndpd is restarted, check with ipmgmtd if there is any
* interface id to be configured for this interface.
*/
if (first) {
if (phyint_check_ipadm_intfid(pi) == -1)
logmsg(LOG_ERR, "Could not get ipadm info\n");
}
} else {
/*
* if the phyint already exists, synchronize it with
* the kernel state. For a newly created phyint, phyint_create
* calls phyint_init_from_k().
*/
(void) phyint_init_from_k(pi);
}
if (pi->pi_sock == -1 && !(pi->pi_kernel_state & PI_PRESENT)) {
/* Interface is not yet present */
if (debug & D_PHYINT) {
logmsg(LOG_DEBUG, "if_process: interface not yet "
"present %s\n", pi->pi_name);
}
return;
}
if (pi->pi_sock != -1) {
if (poll_add(pi->pi_sock) == -1) {
/*
* reset state.
*/
phyint_cleanup(pi);
}
}
/*
* Check if IFF_ROUTER has been turned off in kernel in which
* case we have to turn off AdvSendAdvertisements.
* The kernel will automatically turn off IFF_ROUTER if
* ip6_forwarding is turned off.
* Note that we do not switch back should IFF_ROUTER be turned on.
*/
if (!first &&
pi->pi_AdvSendAdvertisements && !(pi->pi_flags & IFF_ROUTER)) {
logmsg(LOG_INFO, "No longer a router on %s\n", pi->pi_name);
check_to_advertise(pi, START_FINAL_ADV);
pi->pi_AdvSendAdvertisements = 0;
pi->pi_sol_state = NO_SOLICIT;
}
/*
* Send advertisments and solicitation only if the interface is
* present in the kernel.
*/
if (pi->pi_kernel_state & PI_PRESENT) {
if (pi->pi_AdvSendAdvertisements) {
if (pi->pi_adv_state == NO_ADV)
check_to_advertise(pi, START_INIT_ADV);
} else {
if (pi->pi_sol_state == NO_SOLICIT)
check_to_solicit(pi, START_INIT_SOLICIT);
}
}
/*
* Track static kernel prefixes to prevent in.ndpd from clobbering
* them by creating a struct prefix for each prefix detected in the
* kernel.
*/
pr = prefix_lookup_name(pi, ifname);
if (pr == NULL) {
pr = prefix_create_name(pi, ifname);
if (pr == NULL) {
logmsg(LOG_ERR, "if_process: out of memory\n");
return;
}
if (prefix_init_from_k(pr) == -1) {
prefix_delete(pr);
return;
}
}
/* Detect prefixes which are removed */
if (pr->pr_kernel_state != 0)
pr->pr_in_use = _B_TRUE;
if ((lifr.lifr_flags & IFF_DUPLICATE) &&
!(lifr.lifr_flags & IFF_DHCPRUNNING) &&
(pr->pr_flags & IFF_TEMPORARY)) {
in6_addr_t *token;
int i;
char abuf[INET6_ADDRSTRLEN];
if (++pr->pr_attempts >= MAX_DAD_FAILURES) {
logmsg(LOG_ERR, "%s: token %s is duplicate after %d "
"attempts; disabling temporary addresses on %s",
pr->pr_name, inet_ntop(AF_INET6,
(void *)&pi->pi_tmp_token, abuf, sizeof (abuf)),
pr->pr_attempts, pi->pi_name);
pi->pi_TmpAddrsEnabled = 0;
tmptoken_delete(pi);
prefix_delete(pr);
return;
}
logmsg(LOG_WARNING, "%s: token %s is duplicate; trying again",
pr->pr_name, inet_ntop(AF_INET6, (void *)&pi->pi_tmp_token,
abuf, sizeof (abuf)));
if (!tmptoken_create(pi)) {
prefix_delete(pr);
return;
}
token = &pi->pi_tmp_token;
for (i = 0; i < 16; i++) {
/*
* prefix_create ensures that pr_prefix has all-zero
* bits after prefixlen.
*/
pr->pr_address.s6_addr[i] = pr->pr_prefix.s6_addr[i] |
token->s6_addr[i];
}
if (prefix_lookup_addr_match(pr) != NULL) {
prefix_delete(pr);
return;
}
pr->pr_CreateTime = getcurrenttime() / MILLISEC;
/*
* We've got a new token. Clearing PR_AUTO causes
* prefix_update_k to bring the interface up and set the
* address.
*/
pr->pr_kernel_state &= ~PR_AUTO;
prefix_update_k(pr);
}
}
static int ifsock = -1;
/*
* Scan all interfaces to detect changes as well as new and deleted intefaces
* 'first' is set for the initial call only. Do not effect anything.
*/
static void
initifs(boolean_t first)
{
char *buf;
int bufsize;
int numifs;
int n;
struct lifnum lifn;
struct lifconf lifc;
struct lifreq *lifr;
struct phyint *pi;
struct phyint *next_pi;
struct prefix *pr;
if (debug & D_IFSCAN)
logmsg(LOG_DEBUG, "Reading interface configuration\n");
if (ifsock < 0) {
ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
if (ifsock < 0) {
logperror("initifs: socket");
return;
}
}
lifn.lifn_family = AF_INET6;
lifn.lifn_flags = LIFC_NOXMIT | LIFC_TEMPORARY;
if (ioctl(ifsock, SIOCGLIFNUM, (char *)&lifn) < 0) {
logperror("initifs: ioctl (get interface numbers)");
return;
}
numifs = lifn.lifn_count;
bufsize = numifs * sizeof (struct lifreq);
buf = (char *)malloc(bufsize);
if (buf == NULL) {
logmsg(LOG_ERR, "initifs: out of memory\n");
return;
}
/*
* Mark the interfaces so that we can find phyints and prefixes
* which have disappeared from the kernel.
* if_process will set pr_in_use when it finds the interface
* in the kernel.
*/
for (pi = phyints; pi != NULL; pi = pi->pi_next) {
/*
* Before re-examining the state of the interfaces,
* PI_PRESENT should be cleared from pi_kernel_state.
*/
pi->pi_kernel_state &= ~PI_PRESENT;
for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
pr->pr_in_use = _B_FALSE;
}
}
lifc.lifc_family = AF_INET6;
lifc.lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY;
lifc.lifc_len = bufsize;
lifc.lifc_buf = buf;
if (ioctl(ifsock, SIOCGLIFCONF, (char *)&lifc) < 0) {
logperror("initifs: ioctl (get interface configuration)");
free(buf);
return;
}
lifr = (struct lifreq *)lifc.lifc_req;
for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifr++)
if_process(ifsock, lifr->lifr_name, first);
free(buf);
/*
* Detect phyints that have been removed from the kernel.
* Since we can't recreate it here (would require ifconfig plumb
* logic) we just terminate use of that phyint.
*/
for (pi = phyints; pi != NULL; pi = next_pi) {
next_pi = pi->pi_next;
/*
* If interface (still) exists in kernel, set
* pi_state to indicate that.
*/
if (pi->pi_kernel_state & PI_PRESENT) {
pi->pi_state |= PI_PRESENT;
}
check_if_removed(pi);
}
if (show_ifs)
phyint_print_all();
}
/*
* Router advertisement state machine. Used for everything but timer
* events which use advertise_event directly.
*/
void
check_to_advertise(struct phyint *pi, enum adv_events event)
{
uint_t delay;
enum adv_states old_state = pi->pi_adv_state;
if (debug & D_STATE) {
logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d\n",
pi->pi_name, (int)event, (int)old_state);
}
delay = advertise_event(pi, event, 0);
if (delay != TIMER_INFINITY) {
/* Make sure the global next event is updated */
timer_schedule(delay);
}
if (debug & D_STATE) {
logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d -> %d\n",
pi->pi_name, (int)event, (int)old_state,
(int)pi->pi_adv_state);
}
}
/*
* Router advertisement state machine.
* Return the number of milliseconds until next timeout (TIMER_INFINITY
* if never).
* For the ADV_TIMER event the caller passes in the number of milliseconds
* since the last timer event in the 'elapsed' parameter.
*/
uint_t
advertise_event(struct phyint *pi, enum adv_events event, uint_t elapsed)
{
uint_t delay;
if (debug & D_STATE) {
logmsg(LOG_DEBUG, "advertise_event(%s, %d, %d) state %d\n",
pi->pi_name, (int)event, elapsed, (int)pi->pi_adv_state);
}
check_daemonize();
if (!pi->pi_AdvSendAdvertisements)
return (TIMER_INFINITY);
if (pi->pi_flags & IFF_NORTEXCH) {
if (debug & D_PKTOUT) {
logmsg(LOG_DEBUG, "Suppress sending RA packet on %s "
"(no route exchange on interface)\n",
pi->pi_name);
}
return (TIMER_INFINITY);
}
switch (event) {
case ADV_OFF:
pi->pi_adv_state = NO_ADV;
return (TIMER_INFINITY);
case START_INIT_ADV:
if (pi->pi_adv_state == INIT_ADV)
return (pi->pi_adv_time_left);
pi->pi_adv_count = ND_MAX_INITIAL_RTR_ADVERTISEMENTS;
pi->pi_adv_time_left = 0;
pi->pi_adv_state = INIT_ADV;
break; /* send advertisement */
case START_FINAL_ADV:
if (pi->pi_adv_state == NO_ADV)
return (TIMER_INFINITY);
if (pi->pi_adv_state == FINAL_ADV)
return (pi->pi_adv_time_left);
pi->pi_adv_count = ND_MAX_FINAL_RTR_ADVERTISEMENTS;
pi->pi_adv_time_left = 0;
pi->pi_adv_state = FINAL_ADV;
break; /* send advertisement */
case RECEIVED_SOLICIT:
if (pi->pi_adv_state == NO_ADV)
return (TIMER_INFINITY);
if (pi->pi_adv_state == SOLICIT_ADV) {
if (pi->pi_adv_time_left != 0)
return (pi->pi_adv_time_left);
break;
}
delay = GET_RANDOM(0, ND_MAX_RA_DELAY_TIME);
if (delay < pi->pi_adv_time_left)
pi->pi_adv_time_left = delay;
if (pi->pi_adv_time_since_sent < ND_MIN_DELAY_BETWEEN_RAS) {
/*
* Send an advertisement (ND_MIN_DELAY_BETWEEN_RAS
* plus random delay) after the previous
* advertisement was sent.
*/
pi->pi_adv_time_left = delay +
ND_MIN_DELAY_BETWEEN_RAS -
pi->pi_adv_time_since_sent;
}
pi->pi_adv_state = SOLICIT_ADV;
break;
case ADV_TIMER:
if (pi->pi_adv_state == NO_ADV)
return (TIMER_INFINITY);
/* Decrease time left */
if (pi->pi_adv_time_left >= elapsed)
pi->pi_adv_time_left -= elapsed;
else
pi->pi_adv_time_left = 0;
/* Increase time since last advertisement was sent */
pi->pi_adv_time_since_sent += elapsed;
break;
default:
logmsg(LOG_ERR, "advertise_event: Unknown event %d\n",
(int)event);
return (TIMER_INFINITY);
}
if (pi->pi_adv_time_left != 0)
return (pi->pi_adv_time_left);
/* Send advertisement and calculate next time to send */
if (pi->pi_adv_state == FINAL_ADV) {
/* Omit the prefixes */
advertise(&v6allnodes, pi, _B_TRUE);
} else {
advertise(&v6allnodes, pi, _B_FALSE);
}
pi->pi_adv_time_since_sent = 0;
switch (pi->pi_adv_state) {
case SOLICIT_ADV:
/*
* The solicited advertisement has been sent.
* Revert to periodic advertisements.
*/
pi->pi_adv_state = REG_ADV;
/* FALLTHRU */
case REG_ADV:
pi->pi_adv_time_left =
GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
1000 * pi->pi_MaxRtrAdvInterval);
break;
case INIT_ADV:
if (--pi->pi_adv_count > 0) {
delay = GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
1000 * pi->pi_MaxRtrAdvInterval);
if (delay > ND_MAX_INITIAL_RTR_ADVERT_INTERVAL)
delay = ND_MAX_INITIAL_RTR_ADVERT_INTERVAL;
pi->pi_adv_time_left = delay;
} else {
pi->pi_adv_time_left =
GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
1000 * pi->pi_MaxRtrAdvInterval);
pi->pi_adv_state = REG_ADV;
}
break;
case FINAL_ADV:
if (--pi->pi_adv_count > 0) {
pi->pi_adv_time_left =
ND_MAX_INITIAL_RTR_ADVERT_INTERVAL;
} else {
pi->pi_adv_state = NO_ADV;
}
break;
}
if (pi->pi_adv_state != NO_ADV)
return (pi->pi_adv_time_left);
else
return (TIMER_INFINITY);
}
/*
* Router solicitation state machine. Used for everything but timer
* events which use solicit_event directly.
*/
void
check_to_solicit(struct phyint *pi, enum solicit_events event)
{
uint_t delay;
enum solicit_states old_state = pi->pi_sol_state;
if (debug & D_STATE) {
logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d\n",
pi->pi_name, (int)event, (int)old_state);
}
delay = solicit_event(pi, event, 0);
if (delay != TIMER_INFINITY) {
/* Make sure the global next event is updated */
timer_schedule(delay);
}
if (debug & D_STATE) {
logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d -> %d\n",
pi->pi_name, (int)event, (int)old_state,
(int)pi->pi_sol_state);
}
}
static void
daemonize_ndpd(void)
{
struct itimerval it;
boolean_t timerval = _B_TRUE;
/*
* Need to get current timer settings so they can be restored
* after the fork(), as the it_value and it_interval values for
* the ITIMER_REAL timer are reset to 0 in the child process.
*/
if (getitimer(ITIMER_REAL, &it) < 0) {
if (debug & D_TIMER)
logmsg(LOG_DEBUG,
"daemonize_ndpd: failed to get itimerval\n");
timerval = _B_FALSE;
}
/* Daemonize. */
if (daemon(0, 0) == -1) {
logperror("fork");
exit(1);
}
already_daemonized = _B_TRUE;
/*
* Restore timer values, if we were able to save them; if not,
* check and set the right value by calling run_timeouts().
*/
if (timerval) {
if (setitimer(ITIMER_REAL, &it, NULL) < 0) {
logperror("daemonize_ndpd: setitimer");
exit(2);
}
} else {
run_timeouts();
}
}
/*
* Check to see if the time is right to daemonize. The right time is when:
*
* 1. We haven't already daemonized.
* 2. We are not in debug mode.
* 3. All interfaces are marked IFF_NOXMIT.
* 4. All non-router interfaces have their prefixes set up and we're
* done sending router solicitations on those interfaces without
* prefixes.
*/
static void
check_daemonize(void)
{
struct phyint *pi;
if (already_daemonized || debug != 0)
return;
for (pi = phyints; pi != NULL; pi = pi->pi_next) {
if (!(pi->pi_flags & IFF_NOXMIT))
break;
}
/*
* If we can't transmit on any of the interfaces there is no reason
* to hold up progress.
*/
if (pi == NULL) {
daemonize_ndpd();
return;
}
/* Check all interfaces. If any are still soliciting, just return. */
for (pi = phyints; pi != NULL; pi = pi->pi_next) {
if (pi->pi_AdvSendAdvertisements ||
!(pi->pi_kernel_state & PI_PRESENT))
continue;
if (pi->pi_sol_state == INIT_SOLICIT)
return;
}
daemonize_ndpd();
}
/*
* Router solicitation state machine.
* Return the number of milliseconds until next timeout (TIMER_INFINITY
* if never).
* For the SOL_TIMER event the caller passes in the number of milliseconds
* since the last timer event in the 'elapsed' parameter.
*/
uint_t
solicit_event(struct phyint *pi, enum solicit_events event, uint_t elapsed)
{
if (debug & D_STATE) {
logmsg(LOG_DEBUG, "solicit_event(%s, %d, %d) state %d\n",
pi->pi_name, (int)event, elapsed, (int)pi->pi_sol_state);
}
if (pi->pi_AdvSendAdvertisements)
return (TIMER_INFINITY);
if (pi->pi_flags & IFF_NORTEXCH) {
if (debug & D_PKTOUT) {
logmsg(LOG_DEBUG, "Suppress sending RS packet on %s "
"(no route exchange on interface)\n",
pi->pi_name);
}
return (TIMER_INFINITY);
}
switch (event) {
case SOLICIT_OFF:
pi->pi_sol_state = NO_SOLICIT;
check_daemonize();
return (TIMER_INFINITY);
case SOLICIT_DONE:
pi->pi_sol_state = DONE_SOLICIT;
check_daemonize();
return (TIMER_INFINITY);
case RESTART_INIT_SOLICIT:
/*
* This event allows us to start solicitation over again
* without losing the RA flags. We start solicitation over
* when we are missing an interface prefix for a newly-
* encountered DHCP interface.
*/
if (pi->pi_sol_state == INIT_SOLICIT)
return (pi->pi_sol_time_left);
pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS;
pi->pi_sol_time_left =
GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY);
pi->pi_sol_state = INIT_SOLICIT;
break;
case START_INIT_SOLICIT:
if (pi->pi_sol_state == INIT_SOLICIT)
return (pi->pi_sol_time_left);
pi->pi_ra_flags = 0;
pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS;
pi->pi_sol_time_left =
GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY);
pi->pi_sol_state = INIT_SOLICIT;
break;
case SOL_TIMER:
if (pi->pi_sol_state == NO_SOLICIT)
return (TIMER_INFINITY);
/* Decrease time left */
if (pi->pi_sol_time_left >= elapsed)
pi->pi_sol_time_left -= elapsed;
else
pi->pi_sol_time_left = 0;
break;
default:
logmsg(LOG_ERR, "solicit_event: Unknown event %d\n",
(int)event);
return (TIMER_INFINITY);
}
if (pi->pi_sol_time_left != 0)
return (pi->pi_sol_time_left);
/* Send solicitation and calculate next time */
switch (pi->pi_sol_state) {
case INIT_SOLICIT:
solicit(&v6allrouters, pi);
if (--pi->pi_sol_count == 0) {
if (debug & D_STATE) {
logmsg(LOG_DEBUG, "solicit_event: no routers "
"found on %s; assuming default flags\n",
pi->pi_name);
}
if (pi->pi_autoconf && pi->pi_StatefulAddrConf) {
pi->pi_ra_flags |= ND_RA_FLAG_MANAGED |
ND_RA_FLAG_OTHER;
start_dhcp(pi);
}
pi->pi_sol_state = DONE_SOLICIT;
check_daemonize();
return (TIMER_INFINITY);
}
pi->pi_sol_time_left = ND_RTR_SOLICITATION_INTERVAL;
return (pi->pi_sol_time_left);
case NO_SOLICIT:
case DONE_SOLICIT:
return (TIMER_INFINITY);
default:
return (pi->pi_sol_time_left);
}
}
/*
* Timer mechanism using relative time (in milliseconds) from the
* previous timer event. Timers exceeding TIMER_INFINITY milliseconds
* will fire after TIMER_INFINITY milliseconds.
*/
static uint_t timer_previous; /* When last SIGALRM occurred */
static uint_t timer_next; /* Currently scheduled timeout */
static void
timer_init(void)
{
timer_previous = getcurrenttime();
timer_next = TIMER_INFINITY;
run_timeouts();
}
/*
* Make sure the next SIGALRM occurs delay milliseconds from the current
* time if not earlier.
* Handles getcurrenttime (32 bit integer holding milliseconds) wraparound
* by treating differences greater than 0x80000000 as negative.
*/
void
timer_schedule(uint_t delay)
{
uint_t now;
struct itimerval itimerval;
now = getcurrenttime();
if (debug & D_TIMER) {
logmsg(LOG_DEBUG, "timer_schedule(%u): now %u next %u\n",
delay, now, timer_next);
}
/* Will this timer occur before the currently scheduled SIGALRM? */
if (delay >= timer_next - now) {
if (debug & D_TIMER) {
logmsg(LOG_DEBUG, "timer_schedule(%u): no action - "
"next in %u ms\n",
delay, timer_next - now);
}
return;
}
if (delay == 0) {
/* Minimum allowed delay */
delay = 1;
}
timer_next = now + delay;
itimerval.it_value.tv_sec = delay / 1000;
itimerval.it_value.tv_usec = (delay % 1000) * 1000;
itimerval.it_interval.tv_sec = 0;
itimerval.it_interval.tv_usec = 0;
if (debug & D_TIMER) {
logmsg(LOG_DEBUG, "timer_schedule(%u): sec %lu usec %lu\n",
delay,
itimerval.it_value.tv_sec, itimerval.it_value.tv_usec);
}
if (setitimer(ITIMER_REAL, &itimerval, NULL) < 0) {
logperror("timer_schedule: setitimer");
exit(2);
}
}
/*
* Conditional running of timer. If more than 'minimal_time' millseconds
* since the timer routines were last run we run them.
* Used when packets arrive.
*/
static void
conditional_run_timeouts(uint_t minimal_time)
{
uint_t now;
uint_t elapsed;
now = getcurrenttime();
elapsed = now - timer_previous;
if (elapsed > minimal_time) {
if (debug & D_TIMER) {
logmsg(LOG_DEBUG, "conditional_run_timeouts: "
"elapsed %d\n", elapsed);
}
run_timeouts();
}
}
/*
* Timer has fired.
* Determine when the next timer event will occur by asking all
* the timer routines.
* Should not be called from a timer routine but in some cases this is
* done because the code doesn't know that e.g. it was called from
* ifconfig_timer(). In this case the nested run_timeouts will just return but
* the running run_timeouts will ensure to call all the timer functions by
* looping once more.
*/
static void
run_timeouts(void)
{
uint_t now;
uint_t elapsed;
uint_t next;
uint_t nexti;
struct phyint *pi;
struct phyint *next_pi;
struct prefix *pr;
struct prefix *next_pr;
struct adv_prefix *adv_pr;
struct adv_prefix *next_adv_pr;
struct router *dr;
struct router *next_dr;
static boolean_t timeout_running;
static boolean_t do_retry;
if (timeout_running) {
if (debug & D_TIMER)
logmsg(LOG_DEBUG, "run_timeouts: nested call\n");
do_retry = _B_TRUE;
return;
}
timeout_running = _B_TRUE;
retry:
/* How much time since the last time we were called? */
now = getcurrenttime();
elapsed = now - timer_previous;
timer_previous = now;
if (debug & D_TIMER)
logmsg(LOG_DEBUG, "run_timeouts: elapsed %d\n", elapsed);
next = TIMER_INFINITY;
for (pi = phyints; pi != NULL; pi = next_pi) {
next_pi = pi->pi_next;
nexti = phyint_timer(pi, elapsed);
if (nexti != TIMER_INFINITY && nexti < next)
next = nexti;
if (debug & D_TIMER) {
logmsg(LOG_DEBUG, "run_timeouts (pi %s): %d -> %u ms\n",
pi->pi_name, nexti, next);
}
for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
next_pr = pr->pr_next;
nexti = prefix_timer(pr, elapsed);
if (nexti != TIMER_INFINITY && nexti < next)
next = nexti;
if (debug & D_TIMER) {
logmsg(LOG_DEBUG, "run_timeouts (pr %s): "
"%d -> %u ms\n", pr->pr_name, nexti, next);
}
}
for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL;
adv_pr = next_adv_pr) {
next_adv_pr = adv_pr->adv_pr_next;
nexti = adv_prefix_timer(adv_pr, elapsed);
if (nexti != TIMER_INFINITY && nexti < next)
next = nexti;
if (debug & D_TIMER) {
logmsg(LOG_DEBUG, "run_timeouts "
"(adv pr on %s): %d -> %u ms\n",
adv_pr->adv_pr_physical->pi_name,
nexti, next);
}
}
for (dr = pi->pi_router_list; dr != NULL; dr = next_dr) {
next_dr = dr->dr_next;
nexti = router_timer(dr, elapsed);
if (nexti != TIMER_INFINITY && nexti < next)
next = nexti;
if (debug & D_TIMER) {
logmsg(LOG_DEBUG, "run_timeouts (dr): "
"%d -> %u ms\n", nexti, next);
}
}
if (pi->pi_TmpAddrsEnabled) {
nexti = tmptoken_timer(pi, elapsed);
if (nexti != TIMER_INFINITY && nexti < next)
next = nexti;
if (debug & D_TIMER) {
logmsg(LOG_DEBUG, "run_timeouts (tmp on %s): "
"%d -> %u ms\n", pi->pi_name, nexti, next);
}
}
}
/*
* Make sure the timer functions are run at least once
* an hour.
*/
if (next == TIMER_INFINITY)
next = 3600 * 1000; /* 1 hour */
if (debug & D_TIMER)
logmsg(LOG_DEBUG, "run_timeouts: %u ms\n", next);
timer_schedule(next);
if (do_retry) {
if (debug & D_TIMER)
logmsg(LOG_DEBUG, "run_timeouts: retry\n");
do_retry = _B_FALSE;
goto retry;
}
timeout_running = _B_FALSE;
}
static int eventpipe_read = -1; /* Used for synchronous signal delivery */
static int eventpipe_write = -1;
/*
* Ensure that signals are processed synchronously with the rest of
* the code by just writing a one character signal number on the pipe.
* The poll loop will pick this up and process the signal event.
*/
static void
sig_handler(int signo)
{
uchar_t buf = (uchar_t)signo;
if (eventpipe_write == -1) {
logmsg(LOG_ERR, "sig_handler: no pipe\n");
return;
}
if (write(eventpipe_write, &buf, sizeof (buf)) < 0)
logperror("sig_handler: write");
}
/*
* Pick up a signal "byte" from the pipe and process it.
*/
static void
in_signal(int fd)
{
uchar_t buf;
struct phyint *pi;
struct phyint *next_pi;
switch (read(fd, &buf, sizeof (buf))) {
case -1:
logperror("in_signal: read");
exit(1);
/* NOTREACHED */
case 1:
break;
case 0:
logmsg(LOG_ERR, "in_signal: read eof\n");
exit(1);
/* NOTREACHED */
default:
logmsg(LOG_ERR, "in_signal: read > 1\n");
exit(1);
}
if (debug & D_TIMER)
logmsg(LOG_DEBUG, "in_signal() got %d\n", buf);
switch (buf) {
case SIGALRM:
if (debug & D_TIMER) {
uint_t now = getcurrenttime();
logmsg(LOG_DEBUG, "in_signal(SIGALRM) delta %u\n",
now - timer_next);
}
timer_next = TIMER_INFINITY;
run_timeouts();
break;
case SIGHUP:
/* Re-read config file by exec'ing ourselves */
for (pi = phyints; pi != NULL; pi = next_pi) {
next_pi = pi->pi_next;
if (pi->pi_AdvSendAdvertisements)
check_to_advertise(pi, START_FINAL_ADV);
/*
* Remove all the configured addresses.
* Remove the addrobj names created with ipmgmtd.
* Release the dhcpv6 addresses if any.
* Cleanup the phyints.
*/
phyint_delete(pi);
}
/*
* Prevent fd leaks. Everything gets re-opened at start-up
* time. 0, 1, and 2 are closed and re-opened as
* /dev/null, so we'll leave those open.
*/
closefrom(3);
logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n");
(void) execv(argv0[0], argv0);
_exit(0177);
/* NOTREACHED */
case SIGUSR1:
logmsg(LOG_DEBUG, "Printing configuration:\n");
phyint_print_all();
break;
case SIGINT:
case SIGTERM:
case SIGQUIT:
for (pi = phyints; pi != NULL; pi = next_pi) {
next_pi = pi->pi_next;
if (pi->pi_AdvSendAdvertisements)
check_to_advertise(pi, START_FINAL_ADV);
phyint_delete(pi);
}
(void) unlink(NDPD_SNMP_SOCKET);
exit(0);
/* NOTREACHED */
case 255:
/*
* Special "signal" from loopback_ra_enqueue.
* Handle any queued loopback router advertisements.
*/
loopback_ra_dequeue();
break;
default:
logmsg(LOG_ERR, "in_signal: unknown signal: %d\n", buf);
}
}
/*
* Create pipe for signal delivery and set up signal handlers.
*/
static void
setup_eventpipe(void)
{
int fds[2];
struct sigaction act;
if ((pipe(fds)) < 0) {
logperror("setup_eventpipe: pipe");
exit(1);
}
eventpipe_read = fds[0];
eventpipe_write = fds[1];
if (poll_add(eventpipe_read) == -1) {
exit(1);
}
act.sa_handler = sig_handler;
act.sa_flags = SA_RESTART;
(void) sigaction(SIGALRM, &act, NULL);
(void) sigset(SIGHUP, sig_handler);
(void) sigset(SIGUSR1, sig_handler);
(void) sigset(SIGTERM, sig_handler);
(void) sigset(SIGINT, sig_handler);
(void) sigset(SIGQUIT, sig_handler);
}
/*
* Create a routing socket for receiving RTM_IFINFO messages and initialize
* the routing socket message header and as much of the sockaddrs as possible.
*/
static int
setup_rtsock(void)
{
int s;
int ret;
char *cp;
struct sockaddr_in6 *sin6;
s = socket(PF_ROUTE, SOCK_RAW, AF_INET6);
if (s == -1) {
logperror("socket(PF_ROUTE)");
exit(1);
}
ret = fcntl(s, F_SETFL, O_NDELAY|O_NONBLOCK);
if (ret < 0) {
logperror("fcntl(O_NDELAY)");
exit(1);
}
if (poll_add(s) == -1) {
exit(1);
}
/*
* Allocate storage for the routing socket message.
*/
rt_msg = (struct rt_msghdr *)malloc(NDP_RTM_MSGLEN);
if (rt_msg == NULL) {
logperror("malloc");
exit(1);
}
/*
* Initialize the routing socket message by zero-filling it and then
* setting the fields where are constant through the lifetime of the
* process.
*/
bzero(rt_msg, NDP_RTM_MSGLEN);
rt_msg->rtm_msglen = NDP_RTM_MSGLEN;
rt_msg->rtm_version = RTM_VERSION;
rt_msg->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFP;
rt_msg->rtm_pid = getpid();
if (rt_msg->rtm_pid < 0) {
logperror("getpid");
exit(1);
}
/*
* The RTA_DST sockaddr does not change during the lifetime of the
* process so it can be completely initialized at this time.
*/
cp = (char *)rt_msg + sizeof (struct rt_msghdr);
sin6 = (struct sockaddr_in6 *)cp;
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = in6addr_any;
/*
* Initialize the constant portion of the RTA_GATEWAY sockaddr.
*/
cp += sizeof (struct sockaddr_in6);
rta_gateway = (struct sockaddr_in6 *)cp;
rta_gateway->sin6_family = AF_INET6;
/*
* The RTA_NETMASK sockaddr does not change during the lifetime of the
* process so it can be completely initialized at this time.
*/
cp += sizeof (struct sockaddr_in6);
sin6 = (struct sockaddr_in6 *)cp;
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = in6addr_any;
/*
* Initialize the constant portion of the RTA_IFP sockaddr.
*/
cp += sizeof (struct sockaddr_in6);
rta_ifp = (struct sockaddr_dl *)cp;
rta_ifp->sdl_family = AF_LINK;
return (s);
}
static int
setup_mibsock(void)
{
int sock;
int ret;
int len;
struct sockaddr_un laddr;
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sock == -1) {
logperror("setup_mibsock: socket(AF_UNIX)");
exit(1);
}
bzero(&laddr, sizeof (laddr));
laddr.sun_family = AF_UNIX;
(void) strncpy(laddr.sun_path, NDPD_SNMP_SOCKET,
sizeof (laddr.sun_path));
len = sizeof (struct sockaddr_un);
(void) unlink(NDPD_SNMP_SOCKET);
ret = bind(sock, (struct sockaddr *)&laddr, len);
if (ret < 0) {
logperror("setup_mibsock: bind\n");
exit(1);
}
ret = fcntl(sock, F_SETFL, O_NONBLOCK);
if (ret < 0) {
logperror("fcntl(O_NONBLOCK)");
exit(1);
}
if (poll_add(sock) == -1) {
exit(1);
}
return (sock);
}
/*
* Retrieve one routing socket message. If RTM_IFINFO indicates
* new phyint do a full scan of the interfaces. If RTM_IFINFO
* indicates an existing phyint, only scan that phyint and associated
* prefixes.
*/
static void
process_rtsock(int rtsock)
{
int n;
#define MSG_SIZE 2048/8
int64_t msg[MSG_SIZE];
struct rt_msghdr *rtm;
struct if_msghdr *ifm;
struct phyint *pi;
struct prefix *pr;
boolean_t need_initifs = _B_FALSE;
boolean_t need_ifscan = _B_FALSE;
int64_t ifscan_msg[10][MSG_SIZE];
int ifscan_index = 0;
int i;
/* Empty the rtsock and coealesce all the work that we have */
while (ifscan_index < 10) {
n = read(rtsock, msg, sizeof (msg));
if (n <= 0) {
/* No more messages */
break;
}
rtm = (struct rt_msghdr *)msg;
if (rtm->rtm_version != RTM_VERSION) {
logmsg(LOG_ERR,
"process_rtsock: version %d not understood\n",
rtm->rtm_version);
return;
}
switch (rtm->rtm_type) {
case RTM_NEWADDR:
case RTM_DELADDR:
/*
* Some logical interface has changed - have to scan
* everything to determine what actually changed.
*/
if (debug & D_IFSCAN) {
logmsg(LOG_DEBUG, "process_rtsock: "
"message %d\n", rtm->rtm_type);
}
need_initifs = _B_TRUE;
break;
case RTM_IFINFO:
need_ifscan = _B_TRUE;
(void) memcpy(ifscan_msg[ifscan_index], rtm,
sizeof (msg));
ifscan_index++;
/* Handled below */
break;
default:
/* Not interesting */
break;
}
}
/*
* If we do full scan i.e initifs, we don't need to
* scan a particular interface as we should have
* done that as part of initifs.
*/
if (need_initifs) {
initifs(_B_FALSE);
return;
}
if (!need_ifscan)
return;
for (i = 0; i < ifscan_index; i++) {
ifm = (struct if_msghdr *)ifscan_msg[i];
if (debug & D_IFSCAN)
logmsg(LOG_DEBUG, "process_rtsock: index %d\n",
ifm->ifm_index);
pi = phyint_lookup_on_index(ifm->ifm_index);
if (pi == NULL) {
/*
* A new physical interface. Do a full scan of the
* to catch any new logical interfaces.
*/
initifs(_B_FALSE);
return;
}
if (ifm->ifm_flags != (uint_t)pi->pi_flags) {
if (debug & D_IFSCAN) {
logmsg(LOG_DEBUG, "process_rtsock: clr for "
"%s old flags 0x%llx new flags 0x%x\n",
pi->pi_name, pi->pi_flags, ifm->ifm_flags);
}
}
/*
* Mark the interfaces so that we can find phyints and prefixes
* which have disappeared from the kernel.
* if_process will set pr_in_use when it finds the
* interface in the kernel.
* Before re-examining the state of the interfaces,
* PI_PRESENT should be cleared from pi_kernel_state.
*/
pi->pi_kernel_state &= ~PI_PRESENT;
for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
pr->pr_in_use = _B_FALSE;
}
if (ifsock < 0) {
ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
if (ifsock < 0) {
logperror("process_rtsock: socket");
return;
}
}
if_process(ifsock, pi->pi_name, _B_FALSE);
for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
if_process(ifsock, pr->pr_name, _B_FALSE);
}
/*
* If interface (still) exists in kernel, set
* pi_state to indicate that.
*/
if (pi->pi_kernel_state & PI_PRESENT) {
pi->pi_state |= PI_PRESENT;
}
check_if_removed(pi);
if (show_ifs)
phyint_print_all();
}
}
static void
process_mibsock(int mibsock)
{
struct phyint *pi;
socklen_t fromlen;
struct sockaddr_un from;
ndpd_info_t ndpd_info;
ssize_t len;
int command;
fromlen = (socklen_t)sizeof (from);
len = recvfrom(mibsock, &command, sizeof (int), 0,
(struct sockaddr *)&from, &fromlen);
if (len < sizeof (int) || command != NDPD_SNMP_INFO_REQ) {
logperror("process_mibsock: bad command \n");
return;
}
ndpd_info.info_type = NDPD_SNMP_INFO_RESPONSE;
ndpd_info.info_version = NDPD_SNMP_INFO_VER;
ndpd_info.info_num_of_phyints = num_of_phyints;
(void) sendto(mibsock, &ndpd_info, sizeof (ndpd_info_t), 0,
(struct sockaddr *)&from, fromlen);
for (pi = phyints; pi != NULL; pi = pi->pi_next) {
int prefixes;
int routers;
struct prefix *prefix_list;
struct router *router_list;
ndpd_phyint_info_t phyint;
ndpd_prefix_info_t prefix;
ndpd_router_info_t router;
/*
* get number of prefixes
*/
routers = 0;
prefixes = 0;
prefix_list = pi->pi_prefix_list;
while (prefix_list != NULL) {
prefixes++;
prefix_list = prefix_list->pr_next;
}
/*
* get number of routers
*/
router_list = pi->pi_router_list;
while (router_list != NULL) {
routers++;
router_list = router_list->dr_next;
}
phyint.phyint_info_type = NDPD_PHYINT_INFO;
phyint.phyint_info_version = NDPD_PHYINT_INFO_VER;
phyint.phyint_index = pi->pi_index;
bcopy(pi->pi_config,
phyint.phyint_config, I_IFSIZE);
phyint.phyint_num_of_prefixes = prefixes;
phyint.phyint_num_of_routers = routers;
(void) sendto(mibsock, &phyint, sizeof (phyint), 0,
(struct sockaddr *)&from, fromlen);
/*
* Copy prefix information
*/
prefix_list = pi->pi_prefix_list;
while (prefix_list != NULL) {
prefix.prefix_info_type = NDPD_PREFIX_INFO;
prefix.prefix_info_version = NDPD_PREFIX_INFO_VER;
prefix.prefix_prefix = prefix_list->pr_prefix;
prefix.prefix_len = prefix_list->pr_prefix_len;
prefix.prefix_flags = prefix_list->pr_flags;
prefix.prefix_phyint_index = pi->pi_index;
prefix.prefix_ValidLifetime =
prefix_list->pr_ValidLifetime;
prefix.prefix_PreferredLifetime =
prefix_list->pr_PreferredLifetime;
prefix.prefix_OnLinkLifetime =
prefix_list->pr_OnLinkLifetime;
prefix.prefix_OnLinkFlag =
prefix_list->pr_OnLinkFlag;
prefix.prefix_AutonomousFlag =
prefix_list->pr_AutonomousFlag;
(void) sendto(mibsock, &prefix, sizeof (prefix), 0,
(struct sockaddr *)&from, fromlen);
prefix_list = prefix_list->pr_next;
}
/*
* Copy router information
*/
router_list = pi->pi_router_list;
while (router_list != NULL) {
router.router_info_type = NDPD_ROUTER_INFO;
router.router_info_version = NDPD_ROUTER_INFO_VER;
router.router_address = router_list->dr_address;
router.router_lifetime = router_list->dr_lifetime;
router.router_phyint_index = pi->pi_index;
(void) sendto(mibsock, &router, sizeof (router), 0,
(struct sockaddr *)&from, fromlen);
router_list = router_list->dr_next;
}
}
}
/*
* Look if the phyint or one of its prefixes have been removed from
* the kernel and take appropriate action.
* Uses pr_in_use and pi{,_kernel}_state.
*/
static void
check_if_removed(struct phyint *pi)
{
struct prefix *pr, *next_pr;
/*
* Detect prefixes which are removed.
* Static prefixes are just removed from our tables.
* Non-static prefixes are recreated i.e. in.ndpd takes precedence
* over manually removing prefixes via ifconfig.
*/
for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
next_pr = pr->pr_next;
if (!pr->pr_in_use) {
/* Clear everything except PR_STATIC */
pr->pr_kernel_state &= PR_STATIC;
if (pr->pr_state & PR_STATIC)
prefix_update_ipadm_addrobj(pr, _B_FALSE);
pr->pr_name[0] = '\0';
if (pr->pr_state & PR_STATIC) {
prefix_delete(pr);
} else if (!(pi->pi_kernel_state & PI_PRESENT)) {
/*
* Ensure that there are no future attempts to
* run prefix_update_k since the phyint is gone.
*/
pr->pr_state = pr->pr_kernel_state;
} else if (pr->pr_state != pr->pr_kernel_state) {
logmsg(LOG_INFO, "Prefix manually removed "
"on %s; recreating\n", pi->pi_name);
prefix_update_k(pr);
}
}
}
/*
* Detect phyints that have been removed from the kernel, and tear
* down any prefixes we created that are associated with that phyint.
* (NOTE: IPMP depends on in.ndpd tearing down these prefixes so an
* administrator can easily place an IP interface with ADDRCONF'd
* addresses into an IPMP group.)
*/
if (!(pi->pi_kernel_state & PI_PRESENT) &&
(pi->pi_state & PI_PRESENT)) {
logmsg(LOG_ERR, "Interface %s has been removed from kernel. "
"in.ndpd will no longer use it\n", pi->pi_name);
for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
next_pr = pr->pr_next;
if (pr->pr_state & PR_AUTO)
prefix_update_ipadm_addrobj(pr, _B_FALSE);
prefix_delete(pr);
}
/*
* Clear state so that should the phyint reappear we will
* start with initial advertisements or solicitations.
*/
phyint_cleanup(pi);
}
}
/*
* Queuing mechanism for router advertisements that are sent by in.ndpd
* and that also need to be processed by in.ndpd.
* Uses "signal number" 255 to indicate to the main poll loop
* that there is something to dequeue and send to incomining_ra().
*/
struct raq {
struct raq *raq_next;
struct phyint *raq_pi;
int raq_packetlen;
uchar_t *raq_packet;
};
static struct raq *raq_head = NULL;
/*
* Allocate a struct raq and memory for the packet.
* Send signal 255 to have poll dequeue.
*/
static void
loopback_ra_enqueue(struct phyint *pi, struct nd_router_advert *ra, int len)
{
struct raq *raq;
struct raq **raqp;
if (no_loopback)
return;
if (debug & D_PKTOUT)
logmsg(LOG_DEBUG, "loopback_ra_enqueue for %s\n", pi->pi_name);
raq = calloc(sizeof (struct raq), 1);
if (raq == NULL) {
logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n");
return;
}
raq->raq_packet = malloc(len);
if (raq->raq_packet == NULL) {
free(raq);
logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n");
return;
}
bcopy(ra, raq->raq_packet, len);
raq->raq_packetlen = len;
raq->raq_pi = pi;
/* Tail insert */
raqp = &raq_head;
while (*raqp != NULL)
raqp = &((*raqp)->raq_next);
*raqp = raq;
/* Signal for poll loop */
sig_handler(255);
}
/*
* Dequeue and process all queued advertisements.
*/
static void
loopback_ra_dequeue(void)
{
struct sockaddr_in6 from = IN6ADDR_LOOPBACK_INIT;
struct raq *raq;
if (debug & D_PKTIN)
logmsg(LOG_DEBUG, "loopback_ra_dequeue()\n");
while ((raq = raq_head) != NULL) {
raq_head = raq->raq_next;
raq->raq_next = NULL;
if (debug & D_PKTIN) {
logmsg(LOG_DEBUG, "loopback_ra_dequeue for %s\n",
raq->raq_pi->pi_name);
}
incoming_ra(raq->raq_pi,
(struct nd_router_advert *)raq->raq_packet,
raq->raq_packetlen, &from, _B_TRUE);
free(raq->raq_packet);
free(raq);
}
}
static void
usage(char *cmd)
{
(void) fprintf(stderr,
"usage: %s [ -adt ] [-f <config file>]\n", cmd);
}
int
main(int argc, char *argv[])
{
int i;
struct phyint *pi;
int c;
char *config_file = PATH_NDPD_CONF;
boolean_t file_required = _B_FALSE;
argv0 = argv;
srandom(gethostid());
(void) umask(0022);
while ((c = getopt(argc, argv, "adD:ntIf:")) != EOF) {
switch (c) {
case 'a':
/*
* The StatelessAddrConf variable in ndpd.conf, if
* present, will override this setting.
*/
ifdefaults[I_StatelessAddrConf].cf_value = 0;
break;
case 'd':
debug = D_ALL;
break;
case 'D':
i = strtol((char *)optarg, NULL, 0);
if (i == 0) {
(void) fprintf(stderr, "Bad debug flags: %s\n",
(char *)optarg);
exit(1);
}
debug |= i;
break;
case 'n':
no_loopback = 1;
break;
case 'I':
show_ifs = 1;
break;
case 't':
debug |= D_PKTIN | D_PKTOUT | D_PKTBAD;
break;
case 'f':
config_file = (char *)optarg;
file_required = _B_TRUE;
break;
case '?':
usage(argv[0]);
exit(1);
}
}
if (parse_config(config_file, file_required) == -1)
exit(2);
if (show_ifs)
phyint_print_all();
if (debug == 0)
initlog();
cmdsock = ndpd_setup_cmd_listener();
setup_eventpipe();
rtsock = setup_rtsock();
mibsock = setup_mibsock();
timer_init();
initifs(_B_TRUE);
check_daemonize();
for (;;) {
if (poll(pollfds, pollfd_num, -1) < 0) {
if (errno == EINTR)
continue;
logperror("main: poll");
exit(1);
}
for (i = 0; i < pollfd_num; i++) {
if (!(pollfds[i].revents & POLLIN))
continue;
if (pollfds[i].fd == eventpipe_read) {
in_signal(eventpipe_read);
break;
}
if (pollfds[i].fd == rtsock) {
process_rtsock(rtsock);
break;
}
if (pollfds[i].fd == mibsock) {
process_mibsock(mibsock);
break;
}
if (pollfds[i].fd == cmdsock) {
ndpd_cmd_handler(cmdsock);
break;
}
/*
* Run timer routine to advance clock if more than
* half a second since the clock was advanced.
* This limits CPU usage under severe packet
* arrival rates but it creates a slight inaccuracy
* in the timer mechanism.
*/
conditional_run_timeouts(500U);
for (pi = phyints; pi != NULL; pi = pi->pi_next) {
if (pollfds[i].fd == pi->pi_sock) {
in_data(pi);
break;
}
}
}
}
/* NOTREACHED */
return (0);
}
/*
* LOGGER
*/
static boolean_t logging = _B_FALSE;
static void
initlog(void)
{
logging = _B_TRUE;
openlog("in.ndpd", LOG_PID | LOG_CONS, LOG_DAEMON);
}
/* Print the date/time without a trailing carridge return */
static void
fprintdate(FILE *file)
{
char buf[BUFSIZ];
struct tm tms;
time_t now;
now = time(NULL);
(void) localtime_r(&now, &tms);
(void) strftime(buf, sizeof (buf), "%h %d %X", &tms);
(void) fprintf(file, "%s ", buf);
}
/* PRINTFLIKE2 */
void
logmsg(int level, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (logging) {
vsyslog(level, fmt, ap);
} else {
fprintdate(stderr);
(void) vfprintf(stderr, fmt, ap);
}
va_end(ap);
}
void
logperror(const char *str)
{
if (logging) {
syslog(LOG_ERR, "%s: %m\n", str);
} else {
fprintdate(stderr);
(void) fprintf(stderr, "%s: %s\n", str, strerror(errno));
}
}
void
logperror_pi(const struct phyint *pi, const char *str)
{
if (logging) {
syslog(LOG_ERR, "%s (interface %s): %m\n",
str, pi->pi_name);
} else {
fprintdate(stderr);
(void) fprintf(stderr, "%s (interface %s): %s\n",
str, pi->pi_name, strerror(errno));
}
}
void
logperror_pr(const struct prefix *pr, const char *str)
{
if (logging) {
syslog(LOG_ERR, "%s (prefix %s if %s): %m\n",
str, pr->pr_name, pr->pr_physical->pi_name);
} else {
fprintdate(stderr);
(void) fprintf(stderr, "%s (prefix %s if %s): %s\n",
str, pr->pr_name, pr->pr_physical->pi_name,
strerror(errno));
}
}
static int
ndpd_setup_cmd_listener(void)
{
int sock;
int ret;
struct sockaddr_un servaddr;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
logperror("socket");
exit(1);
}
bzero(&servaddr, sizeof (servaddr));
servaddr.sun_family = AF_UNIX;
(void) strlcpy(servaddr.sun_path, IPADM_UDS_PATH,
sizeof (servaddr.sun_path));
(void) unlink(servaddr.sun_path);
ret = bind(sock, (struct sockaddr *)&servaddr, sizeof (servaddr));
if (ret < 0) {
logperror("bind");
exit(1);
}
if (listen(sock, 30) < 0) {
logperror("listen");
exit(1);
}
if (poll_add(sock) == -1) {
logmsg(LOG_ERR, "command socket could not be added to the "
"polling set\n");
exit(1);
}
return (sock);
}
/*
* Commands received over the command socket come here
*/
static void
ndpd_cmd_handler(int sock)
{
int newfd;
struct sockaddr_storage peer;
socklen_t peerlen;
ipadm_ndpd_msg_t ndpd_msg;
int retval;
peerlen = sizeof (peer);
newfd = accept(sock, (struct sockaddr *)&peer, &peerlen);
if (newfd < 0) {
logperror("accept");
return;
}
retval = ipadm_ndpd_read(newfd, &ndpd_msg, sizeof (ndpd_msg));
if (retval != 0)
logperror("Could not read ndpd command");
retval = ndpd_process_cmd(newfd, &ndpd_msg);
if (retval != 0) {
logmsg(LOG_ERR, "ndpd command on interface %s failed with "
"error %s\n", ndpd_msg.inm_ifname, strerror(retval));
}
(void) close(newfd);
}
/*
* Process the commands received from the cmd listener socket.
*/
static int
ndpd_process_cmd(int newfd, ipadm_ndpd_msg_t *msg)
{
int err;
if (!ipadm_check_auth()) {
logmsg(LOG_ERR, "User not authorized to send the command\n");
(void) ndpd_send_error(newfd, EPERM);
return (EPERM);
}
switch (msg->inm_cmd) {
case IPADM_DISABLE_AUTOCONF:
err = ndpd_set_autoconf(msg->inm_ifname, _B_FALSE);
break;
case IPADM_ENABLE_AUTOCONF:
err = ndpd_set_autoconf(msg->inm_ifname, _B_TRUE);
break;
case IPADM_CREATE_ADDRS:
err = ndpd_create_addrs(msg->inm_ifname, msg->inm_intfid,
msg->inm_intfidlen, msg->inm_stateless,
msg->inm_stateful, msg->inm_aobjname);
break;
case IPADM_DELETE_ADDRS:
err = ndpd_delete_addrs(msg->inm_ifname);
break;
default:
err = EINVAL;
break;
}
(void) ndpd_send_error(newfd, err);
return (err);
}
static int
ndpd_send_error(int fd, int error)
{
return (ipadm_ndpd_write(fd, &error, sizeof (error)));
}
/*
* Disables/Enables autoconfiguration of addresses on the
* given physical interface.
* This is provided to support the legacy method of configuring IPv6
* addresses. i.e. `ifconfig bge0 inet6 plumb` will plumb the interface
* and start stateless and stateful autoconfiguration. If this function is
* not called with enable=_B_FALSE, no autoconfiguration will be done until
* ndpd_create_addrs() is called with an Interface ID.
*/
static int
ndpd_set_autoconf(const char *ifname, boolean_t enable)
{
struct phyint *pi;
pi = phyint_lookup((char *)ifname);
if (pi == NULL) {
/*
* If the physical interface was plumbed but no
* addresses were configured yet, phyint will not exist.
*/
pi = phyint_create((char *)ifname);
if (pi == NULL) {
logmsg(LOG_ERR, "could not create phyint for "
"interface %s", ifname);
return (ENOMEM);
}
}
pi->pi_autoconf = enable;
if (debug & D_PHYINT) {
logmsg(LOG_DEBUG, "ndpd_set_autoconf: %s autoconf for "
"interface %s\n", (enable ? "enabled" : "disabled"),
pi->pi_name);
}
return (0);
}
/*
* Create auto-configured addresses on the given interface using
* the given token as the interface id during the next Router Advertisement.
* Currently, only one token per interface is supported.
*/
static int
ndpd_create_addrs(const char *ifname, struct sockaddr_in6 intfid, int intfidlen,
boolean_t stateless, boolean_t stateful, char *addrobj)
{
struct phyint *pi;
struct lifreq lifr;
struct sockaddr_in6 *sin6;
int err;
pi = phyint_lookup((char *)ifname);
if (pi == NULL) {
/*
* If the physical interface was plumbed but no
* addresses were configured yet, phyint will not exist.
*/
pi = phyint_create((char *)ifname);
if (pi == NULL) {
if (debug & D_PHYINT)
logmsg(LOG_ERR, "could not create phyint "
"for interface %s", ifname);
return (ENOMEM);
}
} else if (pi->pi_autoconf) {
logmsg(LOG_ERR, "autoconfiguration already in progress\n");
return (EEXIST);
}
check_autoconf_var_consistency(pi, stateless, stateful);
if (intfidlen == 0) {
pi->pi_default_token = _B_TRUE;
if (ifsock < 0) {
ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
if (ifsock < 0) {
err = errno;
logperror("ndpd_create_addrs: socket");
return (err);
}
}
(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
if (ioctl(ifsock, SIOCGLIFTOKEN, (char *)&lifr) < 0) {
err = errno;
logperror("SIOCGLIFTOKEN");
return (err);
}
pi->pi_token = sin6->sin6_addr;
pi->pi_token_length = lifr.lifr_addrlen;
} else {
pi->pi_default_token = _B_FALSE;
pi->pi_token = intfid.sin6_addr;
pi->pi_token_length = intfidlen;
}
pi->pi_stateless = stateless;
pi->pi_stateful = stateful;
(void) strlcpy(pi->pi_ipadm_aobjname, addrobj,
sizeof (pi->pi_ipadm_aobjname));
/* We can allow autoconfiguration now. */
pi->pi_autoconf = _B_TRUE;
/* Restart the solicitations. */
if (pi->pi_sol_state == DONE_SOLICIT)
pi->pi_sol_state = NO_SOLICIT;
if (pi->pi_sol_state == NO_SOLICIT)
check_to_solicit(pi, START_INIT_SOLICIT);
if (debug & D_PHYINT)
logmsg(LOG_DEBUG, "ndpd_create_addrs: "
"added token to interface %s\n", pi->pi_name);
return (0);
}
/*
* This function deletes all addresses on the given interface
* with the given Interface ID.
*/
static int
ndpd_delete_addrs(const char *ifname)
{
struct phyint *pi;
struct prefix *pr, *next_pr;
struct lifreq lifr;
int err;
pi = phyint_lookup((char *)ifname);
if (pi == NULL) {
logmsg(LOG_ERR, "no phyint found for %s", ifname);
return (ENXIO);
}
if (IN6_IS_ADDR_UNSPECIFIED(&pi->pi_token)) {
logmsg(LOG_ERR, "token does not exist for %s", ifname);
return (ENOENT);
}
if (ifsock < 0) {
ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
if (ifsock < 0) {
err = errno;
logperror("ndpd_create_addrs: socket");
return (err);
}
}
/* Remove the prefixes for this phyint if they exist */
for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
next_pr = pr->pr_next;
if (pr->pr_name[0] == '\0') {
prefix_delete(pr);
continue;
}
/*
* Delete all the prefixes for the auto-configured
* addresses as well as the DHCPv6 addresses.
*/
(void) strncpy(lifr.lifr_name, pr->pr_name,
sizeof (lifr.lifr_name));
if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
err = errno;
logperror("SIOCGLIFFLAGS");
return (err);
}
if ((lifr.lifr_flags & IFF_ADDRCONF) ||
(lifr.lifr_flags & IFF_DHCPRUNNING)) {
prefix_update_ipadm_addrobj(pr, _B_FALSE);
}
prefix_delete(pr);
}
/*
* If we had started dhcpagent, we need to release the leases
* if any are required.
*/
if (pi->pi_stateful) {
(void) strncpy(lifr.lifr_name, pi->pi_name,
sizeof (lifr.lifr_name));
if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
err = errno;
logperror("SIOCGLIFFLAGS");
return (err);
}
if (lifr.lifr_flags & IFF_DHCPRUNNING)
release_dhcp(pi);
}
/*
* Reset the Interface ID on this phyint and stop autoconfigurations
* until a new interface ID is provided.
*/
pi->pi_token = in6addr_any;
pi->pi_token_length = 0;
pi->pi_autoconf = _B_FALSE;
pi->pi_ipadm_aobjname[0] = '\0';
/* Reset the stateless and stateful settings to default. */
pi->pi_stateless = pi->pi_StatelessAddrConf;
pi->pi_stateful = pi->pi_StatefulAddrConf;
if (debug & D_PHYINT) {
logmsg(LOG_DEBUG, "ndpd_delete_addrs: "
"removed token from interface %s\n", pi->pi_name);
}
return (0);
}
void
check_autoconf_var_consistency(struct phyint *pi, boolean_t stateless,
boolean_t stateful)
{
/*
* If StatelessAddrConf and StatelessAddrConf are set in
* /etc/inet/ndpd.conf, check if the new values override those
* settings. If so, log a warning.
*/
if ((pi->pi_StatelessAddrConf !=
ifdefaults[I_StatelessAddrConf].cf_value &&
stateless != pi->pi_StatelessAddrConf) ||
(pi->pi_StatefulAddrConf !=
ifdefaults[I_StatefulAddrConf].cf_value &&
stateful != pi->pi_StatefulAddrConf)) {
logmsg(LOG_ERR, "check_autoconf_var_consistency: "
"Overriding the StatelessAddrConf or StatefulAddrConf "
"settings in ndpd.conf with the new values for "
"interface %s\n", pi->pi_name);
}
}
/*
* If ipadm was used to start autoconfiguration and in.ndpd was restarted
* for some reason, in.ndpd has to resume autoconfiguration when it comes up.
* In this function, it scans the ipadm_addr_info() output to find a link-local
* on this interface with address type "addrconf" and extracts the interface id.
* It also stores the addrobj name to be used later when new addresses are
* created for the prefixes advertised by the router.
* If autoconfiguration was never started on this interface before in.ndpd
* was killed, then in.ndpd should refrain from configuring prefixes, even if
* there is a valid link-local on this interface, created by ipadm (identified
* if there is a valid addrobj name).
*/
static int
phyint_check_ipadm_intfid(struct phyint *pi)
{
ipadm_status_t status;
ipadm_addr_info_t *addrinfo;
struct ifaddrs *ifap;
ipadm_addr_info_t *ainfop;
struct sockaddr_in6 *sin6;
ipadm_handle_t iph;
if (ipadm_open(&iph, 0) != IPADM_SUCCESS) {
logmsg(LOG_ERR, "could not open handle to libipadm\n");
return (-1);
}
status = ipadm_addr_info(iph, pi->pi_name, &addrinfo,
IPADM_OPT_ZEROADDR, LIFC_NOXMIT|LIFC_TEMPORARY);
if (status != IPADM_SUCCESS) {
ipadm_close(iph);
return (-1);
}
pi->pi_autoconf = _B_TRUE;
for (ainfop = addrinfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) {
ifap = &ainfop->ia_ifa;
if (ifap->ifa_addr->sa_family != AF_INET6 ||
ainfop->ia_state == IFA_DISABLED)
continue;
sin6 = (struct sockaddr_in6 *)ifap->ifa_addr;
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
if (ainfop->ia_atype == IPADM_ADDR_IPV6_ADDRCONF) {
pi->pi_token = sin6->sin6_addr;
pi->pi_token._S6_un._S6_u32[0] = 0;
pi->pi_token._S6_un._S6_u32[1] = 0;
pi->pi_autoconf = _B_TRUE;
(void) strlcpy(pi->pi_ipadm_aobjname,
ainfop->ia_aobjname,
sizeof (pi->pi_ipadm_aobjname));
break;
}
/*
* If IFF_NOLINKLOCAL is set, then the link-local
* was created using ipadm. Do not autoconfigure until
* ipadm is explicitly used for autoconfiguration.
*/
if (ifap->ifa_flags & IFF_NOLINKLOCAL)
pi->pi_autoconf = _B_FALSE;
} else if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
strrchr(ifap->ifa_name, ':') == NULL) {
/* The interface was created using ipadm. */
pi->pi_autoconf = _B_FALSE;
}
}
ipadm_free_addr_info(addrinfo);
if (!pi->pi_autoconf) {
pi->pi_token = in6addr_any;
pi->pi_token_length = 0;
}
ipadm_close(iph);
return (0);
}
|