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
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
|
/* $FreeBSD$ */
/* $KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* 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. Neither the name of the project 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 PROJECT 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 PROJECT 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 _rcsid[] = "$KAME: route6d.c,v 1.64 2001/05/08 04:36:37 itojun Exp $";
#endif
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include <syslog.h>
#include <stddef.h>
#include <errno.h>
#include <err.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <sys/uio.h>
#include <net/if.h>
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
#include <net/if_var.h>
#endif /* __FreeBSD__ >= 3 */
#define KERNEL 1
#define _KERNEL 1
#include <net/route.h>
#undef KERNEL
#undef _KERNEL
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include "route6d.h"
#define MAXFILTER 40
#ifdef DEBUG
#define INIT_INTERVAL6 6
#else
#define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest. */
#endif
/* alignment constraint for routing socket */
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
/*
* Following two macros are highly depending on KAME Release
*/
#define IN6_LINKLOCAL_IFINDEX(addr) \
((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
#define SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
do { \
(addr).s6_addr[2] = ((index) >> 8) & 0xff; \
(addr).s6_addr[3] = (index) & 0xff; \
} while (0)
struct ifc { /* Configuration of an interface */
char *ifc_name; /* if name */
struct ifc *ifc_next;
int ifc_index; /* if index */
int ifc_mtu; /* if mtu */
int ifc_metric; /* if metric */
u_int ifc_flags; /* flags */
short ifc_cflags; /* IFC_XXX */
struct in6_addr ifc_mylladdr; /* my link-local address */
struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
struct iff *ifc_filter; /* filter structure */
struct ifac *ifc_addr; /* list of AF_INET6 addresses */
int ifc_joined; /* joined to ff02::9 */
};
struct ifac { /* Adddress associated to an interface */
struct ifc *ifa_conf; /* back pointer */
struct ifac *ifa_next;
struct in6_addr ifa_addr; /* address */
struct in6_addr ifa_raddr; /* remote address, valid in p2p */
int ifa_plen; /* prefix length */
};
struct iff {
int iff_type;
struct in6_addr iff_addr;
int iff_plen;
struct iff *iff_next;
};
struct ifc *ifc;
int nifc; /* number of valid ifc's */
struct ifc **index2ifc;
int nindex2ifc;
struct ifc *loopifcp = NULL; /* pointing to loopback */
int loopifindex = 0; /* ditto */
fd_set sockvec; /* vector to select() for receiving */
int rtsock; /* the routing socket */
int ripsock; /* socket to send/receive RIP datagram */
struct rip6 *ripbuf; /* packet buffer for sending */
/*
* Maintain the routes in a linked list. When the number of the routes
* grows, somebody would like to introduce a hash based or a radix tree
* based structure. I believe the number of routes handled by RIP is
* limited and I don't have to manage a complex data structure, however.
*
* One of the major drawbacks of the linear linked list is the difficulty
* of representing the relationship between a couple of routes. This may
* be a significant problem when we have to support route aggregation with
* supressing the specifices covered by the aggregate.
*/
struct riprt {
struct riprt *rrt_next; /* next destination */
struct riprt *rrt_same; /* same destination - future use */
struct netinfo6 rrt_info; /* network info */
struct in6_addr rrt_gw; /* gateway */
u_long rrt_flags; /* kernel routing table flags */
u_long rrt_rflags; /* route6d routing table flags */
time_t rrt_t; /* when the route validated */
int rrt_index; /* ifindex from which this route got */
};
struct riprt *riprt = 0;
int dflag = 0; /* debug flag */
int qflag = 0; /* quiet flag */
int nflag = 0; /* don't update kernel routing table */
int aflag = 0; /* age out even the statically defined routes */
int hflag = 0; /* don't split horizon */
int lflag = 0; /* exchange site local routes */
int sflag = 0; /* announce static routes w/ split horizon */
int Sflag = 0; /* announce static routes to every interface */
unsigned long routetag = 0; /* route tag attached on originating case */
char *filter[MAXFILTER];
int filtertype[MAXFILTER];
int nfilter = 0;
pid_t pid;
struct sockaddr_storage ripsin;
struct rtentry rtentry;
int interval = 1;
time_t nextalarm = 0;
time_t sup_trig_update = 0;
FILE *rtlog = NULL;
int logopened = 0;
static u_long seq = 0;
volatile int signo;
volatile sig_atomic_t seenalrm;
volatile sig_atomic_t seenquit;
volatile sig_atomic_t seenusr1;
#define RRTF_AGGREGATE 0x08000000
#define RRTF_NOADVERTISE 0x10000000
#define RRTF_NH_NOT_LLADDR 0x20000000
#define RRTF_SENDANYWAY 0x40000000
#define RRTF_CHANGED 0x80000000
int main __P((int, char **));
void sighandler __P((int));
void ripalarm __P((void));
void riprecv __P((void));
void ripsend __P((struct ifc *, struct sockaddr_in6 *, int));
int out_filter __P((struct riprt *, struct ifc *));
void init __P((void));
void sockopt __P((struct ifc *));
void ifconfig __P((void));
void ifconfig1 __P((const char *, const struct sockaddr *, struct ifc *, int));
void rtrecv __P((void));
int rt_del __P((const struct sockaddr_in6 *, const struct sockaddr_in6 *,
const struct sockaddr_in6 *));
int rt_deladdr __P((struct ifc *, const struct sockaddr_in6 *,
const struct sockaddr_in6 *));
void filterconfig __P((void));
int getifmtu __P((int));
const char *rttypes __P((struct rt_msghdr *));
const char *rtflags __P((struct rt_msghdr *));
const char *ifflags __P((int));
int ifrt __P((struct ifc *, int));
void ifrt_p2p __P((struct ifc *, int));
void applymask __P((struct in6_addr *, struct in6_addr *));
void applyplen __P((struct in6_addr *, int));
void ifrtdump __P((int));
void ifdump __P((int));
void ifdump0 __P((FILE *, const struct ifc *));
void rtdump __P((int));
void rt_entry __P((struct rt_msghdr *, int));
void rtdexit __P((void));
void riprequest __P((struct ifc *, struct netinfo6 *, int,
struct sockaddr_in6 *));
void ripflush __P((struct ifc *, struct sockaddr_in6 *));
void sendrequest __P((struct ifc *));
int sin6mask2len __P((const struct sockaddr_in6 *));
int mask2len __P((const struct in6_addr *, int));
int sendpacket __P((struct sockaddr_in6 *, int));
int addroute __P((struct riprt *, const struct in6_addr *, struct ifc *));
int delroute __P((struct netinfo6 *, struct in6_addr *));
struct in6_addr *getroute __P((struct netinfo6 *, struct in6_addr *));
void krtread __P((int));
int tobeadv __P((struct riprt *, struct ifc *));
char *allocopy __P((char *));
char *hms __P((void));
const char *inet6_n2p __P((const struct in6_addr *));
struct ifac *ifa_match __P((const struct ifc *, const struct in6_addr *, int));
struct in6_addr *plen2mask __P((int));
struct riprt *rtsearch __P((struct netinfo6 *, struct riprt **));
int ripinterval __P((int));
time_t ripsuptrig __P((void));
void fatal __P((const char *, ...))
__attribute__((__format__(__printf__, 1, 2)));
void trace __P((int, const char *, ...))
__attribute__((__format__(__printf__, 2, 3)));
void tracet __P((int, const char *, ...))
__attribute__((__format__(__printf__, 2, 3)));
unsigned int if_maxindex __P((void));
struct ifc *ifc_find __P((char *));
struct iff *iff_find __P((struct ifc *, int));
void setindex2ifc __P((int, struct ifc *));
#define MALLOC(type) ((type *)malloc(sizeof(type)))
int
main(argc, argv)
int argc;
char **argv;
{
int ch;
int error = 0;
struct ifc *ifcp;
sigset_t mask, omask;
FILE *pidfile;
char *progname;
char *ep;
progname = strrchr(*argv, '/');
if (progname)
progname++;
else
progname = *argv;
pid = getpid();
while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) {
switch (ch) {
case 'A':
case 'N':
case 'O':
case 'T':
case 'L':
if (nfilter >= MAXFILTER) {
fatal("Exceeds MAXFILTER");
/*NOTREACHED*/
}
filtertype[nfilter] = ch;
filter[nfilter++] = allocopy(optarg);
break;
case 't':
ep = NULL;
routetag = strtoul(optarg, &ep, 0);
if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
fatal("invalid route tag");
/*NOTREACHED*/
}
break;
case 'R':
if ((rtlog = fopen(optarg, "w")) == NULL) {
fatal("Can not write to routelog");
/*NOTREACHED*/
}
break;
#define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
FLAG('a', aflag, 1); break;
FLAG('d', dflag, 1); break;
FLAG('D', dflag, 2); break;
FLAG('h', hflag, 1); break;
FLAG('l', lflag, 1); break;
FLAG('n', nflag, 1); break;
FLAG('q', qflag, 1); break;
FLAG('s', sflag, 1); break;
FLAG('S', Sflag, 1); break;
#undef FLAG
default:
fatal("Invalid option specified, terminating");
/*NOTREACHED*/
}
}
argc -= optind;
argv += optind;
if (argc > 0) {
fatal("bogus extra arguments");
/*NOTREACHED*/
}
if (geteuid()) {
nflag = 1;
fprintf(stderr, "No kernel update is allowed\n");
}
if (dflag == 0) {
if (daemon(0, 0) < 0) {
fatal("daemon");
/*NOTREACHED*/
}
}
openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
logopened++;
if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
fatal("malloc");
memset(ripbuf, 0, RIP6_MAXMTU);
ripbuf->rip6_cmd = RIP6_RESPONSE;
ripbuf->rip6_vers = RIP6_VERSION;
ripbuf->rip6_res1[0] = 0;
ripbuf->rip6_res1[1] = 0;
init();
ifconfig();
for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
if (ifcp->ifc_index < 0) {
fprintf(stderr,
"No ifindex found at %s (no link-local address?)\n",
ifcp->ifc_name);
error++;
}
}
if (error)
exit(1);
if (loopifcp == NULL) {
fatal("No loopback found");
/*NOTREACHED*/
}
#ifdef __FreeBSD__
sranddev();
#else
srand((unsigned)(time(NULL)^(pid<<16)));
#endif
loopifindex = loopifcp->ifc_index;
for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
ifrt(ifcp, 0);
filterconfig();
krtread(0);
if (dflag)
ifrtdump(0);
pid = getpid();
if ((pidfile = fopen(ROUTE6D_PID, "w")) != NULL) {
fprintf(pidfile, "%d\n", pid);
fclose(pidfile);
}
if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
fatal("malloc");
/*NOTREACHED*/
}
memset(ripbuf, 0, RIP6_MAXMTU);
ripbuf->rip6_cmd = RIP6_RESPONSE;
ripbuf->rip6_vers = RIP6_VERSION;
ripbuf->rip6_res1[0] = 0;
ripbuf->rip6_res1[1] = 0;
if (signal(SIGALRM, sighandler) == SIG_ERR ||
signal(SIGQUIT, sighandler) == SIG_ERR ||
signal(SIGTERM, sighandler) == SIG_ERR ||
signal(SIGUSR1, sighandler) == SIG_ERR ||
signal(SIGHUP, sighandler) == SIG_ERR ||
signal(SIGINT, sighandler) == SIG_ERR) {
fatal("signal");
/*NOTREACHED*/
}
/*
* To avoid rip packet congestion (not on a cable but in this
* process), wait for a moment to send the first RIP6_RESPONSE
* packets.
*/
alarm(ripinterval(INIT_INTERVAL6));
for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
sendrequest(ifcp);
}
syslog(LOG_INFO, "**** Started ****");
sigemptyset(&mask);
sigaddset(&mask, SIGALRM);
while (1) {
fd_set recvec;
if (seenalrm) {
ripalarm();
seenalrm = 0;
continue;
}
if (seenquit) {
rtdexit();
seenquit = 0;
continue;
}
if (seenusr1) {
ifrtdump(SIGUSR1);
seenusr1 = 0;
continue;
}
FD_COPY(&sockvec, &recvec);
signo = 0;
switch (select(FD_SETSIZE, &recvec, 0, 0, 0)) {
case -1:
if (errno != EINTR) {
fatal("select");
/*NOTREACHED*/
}
continue;
case 0:
continue;
default:
if (FD_ISSET(ripsock, &recvec)) {
sigprocmask(SIG_BLOCK, &mask, &omask);
riprecv();
sigprocmask(SIG_SETMASK, &omask, NULL);
}
if (FD_ISSET(rtsock, &recvec)) {
sigprocmask(SIG_BLOCK, &mask, &omask);
rtrecv();
sigprocmask(SIG_SETMASK, &omask, NULL);
}
}
}
}
void
sighandler(sig)
int sig;
{
signo = sig;
switch (signo) {
case SIGALRM:
seenalrm++;
break;
case SIGQUIT:
case SIGTERM:
seenquit++;
break;
case SIGUSR1:
case SIGHUP:
case SIGINT:
seenusr1++;
break;
}
}
/*
* gracefully exits after resetting sockopts.
*/
/* ARGSUSED */
void
rtdexit()
{
struct riprt *rrt;
alarm(0);
for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
if (rrt->rrt_rflags & RRTF_AGGREGATE) {
delroute(&rrt->rrt_info, &rrt->rrt_gw);
}
}
close(ripsock);
close(rtsock);
syslog(LOG_INFO, "**** Terminated ****");
closelog();
exit(1);
}
/*
* Called periodically:
* 1. age out the learned route. remove it if necessary.
* 2. submit RIP6_RESPONSE packets.
* Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
* to invoke this function in every 1 or 5 or 10 seconds only to age the
* routes more precisely.
*/
/* ARGSUSED */
void
ripalarm()
{
struct ifc *ifcp;
struct riprt *rrt, *rrt_prev, *rrt_next;
time_t t_lifetime, t_holddown;
/* age the RIP routes */
rrt_prev = 0;
t_lifetime = time(NULL) - RIP_LIFETIME;
t_holddown = t_lifetime - RIP_HOLDDOWN;
for (rrt = riprt; rrt; rrt = rrt_next) {
rrt_next = rrt->rrt_next;
if (rrt->rrt_t == 0) {
rrt_prev = rrt;
continue;
}
if (rrt->rrt_t < t_holddown) {
if (rrt_prev) {
rrt_prev->rrt_next = rrt->rrt_next;
} else {
riprt = rrt->rrt_next;
}
delroute(&rrt->rrt_info, &rrt->rrt_gw);
free(rrt);
continue;
}
if (rrt->rrt_t < t_lifetime)
rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
rrt_prev = rrt;
}
/* Supply updates */
for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
ripsend(ifcp, &ifcp->ifc_ripsin, 0);
}
alarm(ripinterval(SUPPLY_INTERVAL6));
}
void
init()
{
int error;
const int int0 = 0, int1 = 1, int255 = 255;
struct addrinfo hints, *res;
char port[10];
ifc = (struct ifc *)NULL;
nifc = 0;
nindex2ifc = 0; /*initial guess*/
index2ifc = NULL;
snprintf(port, sizeof(port), "%d", RIP6_PORT);
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
error = getaddrinfo(NULL, port, &hints, &res);
if (error) {
fatal("%s", gai_strerror(error));
/*NOTREACHED*/
}
if (res->ai_next) {
fatal(":: resolved to multiple address");
/*NOTREACHED*/
}
ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (ripsock < 0) {
fatal("rip socket");
/*NOTREACHED*/
}
#ifdef IPV6_V6ONLY
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
&int1, sizeof(int1)) < 0) {
fatal("rip IPV6_V6ONLY");
/*NOTREACHED*/
}
#endif
if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
fatal("rip bind");
/*NOTREACHED*/
}
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
&int255, sizeof(int255)) < 0) {
fatal("rip IPV6_MULTICAST_HOPS");
/*NOTREACHED*/
}
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
&int0, sizeof(int0)) < 0) {
fatal("rip IPV6_MULTICAST_LOOP");
/*NOTREACHED*/
}
#ifdef IPV6_RECVPKTINFO
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
&int1, sizeof(int1)) < 0) {
fatal("rip IPV6_RECVPKTINFO");
/*NOTREACHED*/
}
#else /* old adv. API */
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
&int1, sizeof(int1)) < 0) {
fatal("rip IPV6_PKTINFO");
/*NOTREACHED*/
}
#endif
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET6;
hints.ai_socktype = SOCK_DGRAM;
error = getaddrinfo(RIP6_DEST, port, &hints, &res);
if (error) {
fatal("%s", gai_strerror(error));
/*NOTREACHED*/
}
if (res->ai_next) {
fatal("%s resolved to multiple address", RIP6_DEST);
/*NOTREACHED*/
}
memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
#ifdef FD_ZERO
FD_ZERO(&sockvec);
#else
memset(&sockvec, 0, sizeof(sockvec));
#endif
FD_SET(ripsock, &sockvec);
if (nflag == 0) {
if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
fatal("route socket");
/*NOTREACHED*/
}
FD_SET(rtsock, &sockvec);
} else
rtsock = -1; /*just for safety */
}
#define RIPSIZE(n) \
(sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
/*
* ripflush flushes the rip datagram stored in the rip buffer
*/
static int nrt;
static struct netinfo6 *np;
void
ripflush(ifcp, sin6)
struct ifc *ifcp;
struct sockaddr_in6 *sin6;
{
int i;
int error;
if (ifcp)
tracet(1, "Send(%s): info(%d) to %s.%d\n",
ifcp->ifc_name, nrt,
inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
else
tracet(1, "Send: info(%d) to %s.%d\n",
nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
if (dflag >= 2) {
np = ripbuf->rip6_nets;
for (i = 0; i < nrt; i++, np++) {
if (np->rip6_metric == NEXTHOP_METRIC) {
if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
trace(2, " NextHop reset");
else {
trace(2, " NextHop %s",
inet6_n2p(&np->rip6_dest));
}
} else {
trace(2, " %s/%d[%d]",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
}
if (np->rip6_tag) {
trace(2, " tag=0x%04x",
ntohs(np->rip6_tag) & 0xffff);
}
trace(2, "\n");
}
}
error = sendpacket(sin6, RIPSIZE(nrt));
if (error == EAFNOSUPPORT) {
/* Protocol not supported */
tracet(1, "Could not send info to %s (%s): "
"set IFF_UP to 0\n",
ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
}
nrt = 0; np = ripbuf->rip6_nets;
}
/*
* Generate RIP6_RESPONSE packets and send them.
*/
void
ripsend(ifcp, sin6, flag)
struct ifc *ifcp;
struct sockaddr_in6 *sin6;
int flag;
{
struct riprt *rrt;
struct in6_addr *nh; /* next hop */
int maxrte;
if (ifcp == NULL) {
/*
* Request from non-link local address is not
* a regular route6d update.
*/
maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
sizeof(struct udphdr) -
sizeof(struct rip6) + sizeof(struct netinfo6)) /
sizeof(struct netinfo6);
nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
if (rrt->rrt_rflags & RRTF_NOADVERTISE)
continue;
/* Put the route to the buffer */
*np = rrt->rrt_info;
np++; nrt++;
if (nrt == maxrte) {
ripflush(NULL, sin6);
nh = NULL;
}
}
if (nrt) /* Send last packet */
ripflush(NULL, sin6);
return;
}
if ((flag & RRTF_SENDANYWAY) == 0 &&
(qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
return;
/* -N: no use */
if (iff_find(ifcp, 'N') != NULL)
return;
/* -T: generate default route only */
if (iff_find(ifcp, 'T') != NULL) {
struct netinfo6 rrt_info;
memset(&rrt_info, 0, sizeof(struct netinfo6));
rrt_info.rip6_dest = in6addr_any;
rrt_info.rip6_plen = 0;
rrt_info.rip6_metric = 1;
rrt_info.rip6_metric += ifcp->ifc_metric;
rrt_info.rip6_tag = htons(routetag & 0xffff);
np = ripbuf->rip6_nets;
*np = rrt_info;
nrt = 1;
ripflush(ifcp, sin6);
return;
}
maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
sizeof(struct udphdr) -
sizeof(struct rip6) + sizeof(struct netinfo6)) /
sizeof(struct netinfo6);
nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
if (rrt->rrt_rflags & RRTF_NOADVERTISE)
continue;
/* Need to check filter here */
if (out_filter(rrt, ifcp) == 0)
continue;
/* Check split horizon and other conditions */
if (tobeadv(rrt, ifcp) == 0)
continue;
/* Only considers the routes with flag if specified */
if ((flag & RRTF_CHANGED) &&
(rrt->rrt_rflags & RRTF_CHANGED) == 0)
continue;
/* Check nexthop */
if (rrt->rrt_index == ifcp->ifc_index &&
!IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
(rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
if (nrt == maxrte - 2)
ripflush(ifcp, sin6);
np->rip6_dest = rrt->rrt_gw;
if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest))
SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0);
np->rip6_plen = 0;
np->rip6_tag = 0;
np->rip6_metric = NEXTHOP_METRIC;
nh = &rrt->rrt_gw;
np++; nrt++;
}
} else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
!IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
/* Reset nexthop */
if (nrt == maxrte - 2)
ripflush(ifcp, sin6);
memset(np, 0, sizeof(struct netinfo6));
np->rip6_metric = NEXTHOP_METRIC;
nh = NULL;
np++; nrt++;
}
/* Put the route to the buffer */
*np = rrt->rrt_info;
np++; nrt++;
if (nrt == maxrte) {
ripflush(ifcp, sin6);
nh = NULL;
}
}
if (nrt) /* Send last packet */
ripflush(ifcp, sin6);
}
/*
* outbound filter logic, per-route/interface.
*/
int
out_filter(rrt, ifcp)
struct riprt *rrt;
struct ifc *ifcp;
{
struct iff *iffp;
struct in6_addr ia;
int ok;
/*
* -A: filter out less specific routes, if we have aggregated
* route configured.
*/
for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
if (iffp->iff_type != 'A')
continue;
if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
continue;
ia = rrt->rrt_info.rip6_dest;
applyplen(&ia, iffp->iff_plen);
if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
return 0;
}
/*
* if it is an aggregated route, advertise it only to the
* interfaces specified on -A.
*/
if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
ok = 0;
for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
if (iffp->iff_type != 'A')
continue;
if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
&iffp->iff_addr)) {
ok = 1;
break;
}
}
if (!ok)
return 0;
}
/*
* -O: advertise only if prefix matches the configured prefix.
*/
if (iff_find(ifcp, 'O')) {
ok = 0;
for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
if (iffp->iff_type != 'O')
continue;
if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
continue;
ia = rrt->rrt_info.rip6_dest;
applyplen(&ia, iffp->iff_plen);
if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
ok = 1;
break;
}
}
if (!ok)
return 0;
}
/* the prefix should be advertised */
return 1;
}
/*
* Determine if the route is to be advertised on the specified interface.
* It checks options specified in the arguments and the split horizon rule.
*/
int
tobeadv(rrt, ifcp)
struct riprt *rrt;
struct ifc *ifcp;
{
/* Special care for static routes */
if (rrt->rrt_flags & RTF_STATIC) {
/* XXX don't advertise reject/blackhole routes */
if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
return 0;
if (Sflag) /* Yes, advertise it anyway */
return 1;
if (sflag && rrt->rrt_index != ifcp->ifc_index)
return 1;
return 0;
}
/* Regular split horizon */
if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
return 0;
return 1;
}
/*
* Send a rip packet actually.
*/
int
sendpacket(sin6, len)
struct sockaddr_in6 *sin6;
int len;
{
/*
* MSG_DONTROUTE should not be specified when it responds with a
* RIP6_REQUEST message. SO_DONTROUTE has been specified to
* other sockets.
*/
struct msghdr m;
struct cmsghdr *cm;
struct iovec iov[2];
u_char cmsgbuf[256];
struct in6_pktinfo *pi;
int idx;
struct sockaddr_in6 sincopy;
/* do not overwrite the given sin */
sincopy = *sin6;
sin6 = &sincopy;
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
idx = IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr);
SET_IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr, 0);
} else
idx = 0;
m.msg_name = (caddr_t)sin6;
m.msg_namelen = sizeof(*sin6);
iov[0].iov_base = (caddr_t)ripbuf;
iov[0].iov_len = len;
m.msg_iov = iov;
m.msg_iovlen = 1;
if (!idx) {
m.msg_control = NULL;
m.msg_controllen = 0;
} else {
memset(cmsgbuf, 0, sizeof(cmsgbuf));
cm = (struct cmsghdr *)cmsgbuf;
m.msg_control = (caddr_t)cm;
m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
cm->cmsg_level = IPPROTO_IPV6;
cm->cmsg_type = IPV6_PKTINFO;
pi = (struct in6_pktinfo *)CMSG_DATA(cm);
memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
pi->ipi6_ifindex = idx;
}
if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
trace(1, "sendmsg: %s\n", strerror(errno));
return errno;
}
return 0;
}
/*
* Receive and process RIP packets. Update the routes/kernel forwarding
* table if necessary.
*/
void
riprecv()
{
struct ifc *ifcp, *ic;
struct sockaddr_in6 fsock;
struct in6_addr nh; /* next hop */
struct rip6 *rp;
struct netinfo6 *np, *nq;
struct riprt *rrt;
int len, nn, need_trigger, idx;
char buf[4 * RIP6_MAXMTU];
time_t t;
struct msghdr m;
struct cmsghdr *cm;
struct iovec iov[2];
u_char cmsgbuf[256];
struct in6_pktinfo *pi;
struct iff *iffp;
struct in6_addr ia;
int ok;
time_t t_half_lifetime;
need_trigger = 0;
m.msg_name = (caddr_t)&fsock;
m.msg_namelen = sizeof(fsock);
iov[0].iov_base = (caddr_t)buf;
iov[0].iov_len = sizeof(buf);
m.msg_iov = iov;
m.msg_iovlen = 1;
cm = (struct cmsghdr *)cmsgbuf;
m.msg_control = (caddr_t)cm;
m.msg_controllen = sizeof(cmsgbuf);
if ((len = recvmsg(ripsock, &m, 0)) < 0) {
fatal("recvmsg");
/*NOTREACHED*/
}
idx = 0;
for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
cm;
cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
if (cm->cmsg_level == IPPROTO_IPV6 &&
cm->cmsg_type == IPV6_PKTINFO) {
pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
idx = pi->ipi6_ifindex;
break;
}
}
if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr))
SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx);
nh = fsock.sin6_addr;
nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
sizeof(struct netinfo6);
rp = (struct rip6 *)buf;
np = rp->rip6_nets;
if (rp->rip6_vers != RIP6_VERSION) {
trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
return;
}
if (rp->rip6_cmd == RIP6_REQUEST) {
if (idx && idx < nindex2ifc) {
ifcp = index2ifc[idx];
riprequest(ifcp, np, nn, &fsock);
} else {
riprequest(NULL, np, nn, &fsock);
}
return;
}
if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
trace(1, "Packets from non-ll addr: %s\n",
inet6_n2p(&fsock.sin6_addr));
return; /* Ignore packets from non-link-local addr */
}
idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
if (!ifcp) {
trace(1, "Packets to unknown interface index %d\n", idx);
return; /* Ignore it */
}
if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
return; /* The packet is from me; ignore */
if (rp->rip6_cmd != RIP6_RESPONSE) {
trace(1, "Invalid command %d\n", rp->rip6_cmd);
return;
}
/* -N: no use */
if (iff_find(ifcp, 'N') != NULL)
return;
tracet(1, "Recv(%s): from %s.%d info(%d)\n",
ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
t = time(NULL);
t_half_lifetime = t - (RIP_LIFETIME/2);
for (; nn; nn--, np++) {
if (np->rip6_metric == NEXTHOP_METRIC) {
/* modify neighbor address */
if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
nh = np->rip6_dest;
SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
} else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
nh = fsock.sin6_addr;
trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
} else {
nh = fsock.sin6_addr;
trace(1, "\tInvalid Nexthop: %s\n",
inet6_n2p(&np->rip6_dest));
}
continue;
}
if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
continue;
}
if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
continue;
}
if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
continue;
}
/* may need to pass sitelocal prefix in some case, however*/
if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
continue;
}
trace(2, "\tnetinfo6: %s/%d [%d]",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
if (np->rip6_tag)
trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
if (dflag >= 2) {
ia = np->rip6_dest;
applyplen(&ia, np->rip6_plen);
if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
trace(2, " [junk outside prefix]");
}
/*
* -L: listen only if the prefix matches the configuration
*/
ok = 1; /* if there's no L filter, it is ok */
for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
if (iffp->iff_type != 'L')
continue;
ok = 0;
if (np->rip6_plen < iffp->iff_plen)
continue;
/* special rule: ::/0 means default, not "in /0" */
if (iffp->iff_plen == 0 && np->rip6_plen > 0)
continue;
ia = np->rip6_dest;
applyplen(&ia, iffp->iff_plen);
if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
ok = 1;
break;
}
}
if (!ok) {
trace(2, " (filtered)\n");
continue;
}
trace(2, "\n");
np->rip6_metric++;
np->rip6_metric += ifcp->ifc_metric;
if (np->rip6_metric > HOPCNT_INFINITY6)
np->rip6_metric = HOPCNT_INFINITY6;
applyplen(&np->rip6_dest, np->rip6_plen);
if ((rrt = rtsearch(np, NULL)) != NULL) {
if (rrt->rrt_t == 0)
continue; /* Intf route has priority */
nq = &rrt->rrt_info;
if (nq->rip6_metric > np->rip6_metric) {
if (rrt->rrt_index == ifcp->ifc_index &&
IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
/* Small metric from the same gateway */
nq->rip6_metric = np->rip6_metric;
} else {
/* Better route found */
rrt->rrt_index = ifcp->ifc_index;
/* Update routing table */
delroute(nq, &rrt->rrt_gw);
rrt->rrt_gw = nh;
*nq = *np;
addroute(rrt, &nh, ifcp);
}
rrt->rrt_rflags |= RRTF_CHANGED;
rrt->rrt_t = t;
need_trigger = 1;
} else if (nq->rip6_metric < np->rip6_metric &&
rrt->rrt_index == ifcp->ifc_index &&
IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
/* Got worse route from same gw */
nq->rip6_metric = np->rip6_metric;
rrt->rrt_t = t;
rrt->rrt_rflags |= RRTF_CHANGED;
need_trigger = 1;
} else if (nq->rip6_metric == np->rip6_metric &&
np->rip6_metric < HOPCNT_INFINITY6) {
if (rrt->rrt_index == ifcp->ifc_index &&
IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
/* same metric, same route from same gw */
rrt->rrt_t = t;
} else if (rrt->rrt_t < t_half_lifetime) {
/* Better route found */
rrt->rrt_index = ifcp->ifc_index;
/* Update routing table */
delroute(nq, &rrt->rrt_gw);
rrt->rrt_gw = nh;
*nq = *np;
addroute(rrt, &nh, ifcp);
rrt->rrt_rflags |= RRTF_CHANGED;
rrt->rrt_t = t;
}
}
/*
* if nq->rip6_metric == HOPCNT_INFINITY6 then
* do not update age value. Do nothing.
*/
} else if (np->rip6_metric < HOPCNT_INFINITY6) {
/* Got a new valid route */
if ((rrt = MALLOC(struct riprt)) == NULL) {
fatal("malloc: struct riprt");
/*NOTREACHED*/
}
memset(rrt, 0, sizeof(*rrt));
nq = &rrt->rrt_info;
rrt->rrt_same = NULL;
rrt->rrt_index = ifcp->ifc_index;
rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
rrt->rrt_gw = nh;
*nq = *np;
applyplen(&nq->rip6_dest, nq->rip6_plen);
if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
rrt->rrt_flags |= RTF_HOST;
/* Put the route to the list */
rrt->rrt_next = riprt;
riprt = rrt;
/* Update routing table */
addroute(rrt, &nh, ifcp);
rrt->rrt_rflags |= RRTF_CHANGED;
need_trigger = 1;
rrt->rrt_t = t;
}
}
/* XXX need to care the interval between triggered updates */
if (need_trigger) {
if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
for (ic = ifc; ic; ic = ic->ifc_next) {
if (ifcp->ifc_index == ic->ifc_index)
continue;
if (ic->ifc_flags & IFF_UP)
ripsend(ic, &ic->ifc_ripsin,
RRTF_CHANGED);
}
}
/* Reset the flag */
for (rrt = riprt; rrt; rrt = rrt->rrt_next)
rrt->rrt_rflags &= ~RRTF_CHANGED;
}
}
/*
* Send all routes request packet to the specified interface.
*/
void
sendrequest(ifcp)
struct ifc *ifcp;
{
struct netinfo6 *np;
int error;
if (ifcp->ifc_flags & IFF_LOOPBACK)
return;
ripbuf->rip6_cmd = RIP6_REQUEST;
np = ripbuf->rip6_nets;
memset(np, 0, sizeof(struct netinfo6));
np->rip6_metric = HOPCNT_INFINITY6;
tracet(1, "Send rtdump Request to %s (%s)\n",
ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
if (error == EAFNOSUPPORT) {
/* Protocol not supported */
tracet(1, "Could not send rtdump Request to %s (%s): "
"set IFF_UP to 0\n",
ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
}
ripbuf->rip6_cmd = RIP6_RESPONSE;
}
/*
* Process a RIP6_REQUEST packet.
*/
void
riprequest(ifcp, np, nn, sin6)
struct ifc *ifcp;
struct netinfo6 *np;
int nn;
struct sockaddr_in6 *sin6;
{
int i;
struct riprt *rrt;
if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
/* Specific response, don't split-horizon */
trace(1, "\tRIP Request\n");
for (i = 0; i < nn; i++, np++) {
rrt = rtsearch(np, NULL);
if (rrt)
np->rip6_metric = rrt->rrt_info.rip6_metric;
else
np->rip6_metric = HOPCNT_INFINITY6;
}
(void)sendpacket(sin6, RIPSIZE(nn));
return;
}
/* Whole routing table dump */
trace(1, "\tRIP Request -- whole routing table\n");
ripsend(ifcp, sin6, RRTF_SENDANYWAY);
}
/*
* Get information of each interface.
*/
void
ifconfig()
{
struct ifaddrs *ifap, *ifa;
struct ifc *ifcp;
struct ipv6_mreq mreq;
int s;
if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
fatal("socket");
/*NOTREACHED*/
}
if (getifaddrs(&ifap) != 0) {
fatal("getifaddrs");
/*NOTREACHED*/
}
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family != AF_INET6)
continue;
ifcp = ifc_find(ifa->ifa_name);
/* we are interested in multicast-capable interfaces */
if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
continue;
if (!ifcp) {
/* new interface */
if ((ifcp = MALLOC(struct ifc)) == NULL) {
fatal("malloc: struct ifc");
/*NOTREACHED*/
}
memset(ifcp, 0, sizeof(*ifcp));
ifcp->ifc_index = -1;
ifcp->ifc_next = ifc;
ifc = ifcp;
nifc++;
ifcp->ifc_name = allocopy(ifa->ifa_name);
ifcp->ifc_addr = 0;
ifcp->ifc_filter = 0;
ifcp->ifc_flags = ifa->ifa_flags;
trace(1, "newif %s <%s>\n", ifcp->ifc_name,
ifflags(ifcp->ifc_flags));
if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
loopifcp = ifcp;
} else {
/* update flag, this may be up again */
if (ifcp->ifc_flags != ifa->ifa_flags) {
trace(1, "%s: <%s> -> ", ifcp->ifc_name,
ifflags(ifcp->ifc_flags));
trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
ifcp->ifc_cflags |= IFC_CHANGED;
}
ifcp->ifc_flags = ifa->ifa_flags;
}
ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s);
if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
&& 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
mreq.ipv6mr_interface = ifcp->ifc_index;
if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
&mreq, sizeof(mreq)) < 0) {
fatal("IPV6_JOIN_GROUP");
/*NOTREACHED*/
}
trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
ifcp->ifc_joined++;
}
}
close(s);
freeifaddrs(ifap);
}
void
ifconfig1(name, sa, ifcp, s)
const char *name;
const struct sockaddr *sa;
struct ifc *ifcp;
int s;
{
struct in6_ifreq ifr;
const struct sockaddr_in6 *sin6;
struct ifac *ifa;
int plen;
char buf[BUFSIZ];
sin6 = (const struct sockaddr_in6 *)sa;
ifr.ifr_addr = *sin6;
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
fatal("ioctl: SIOCGIFNETMASK_IN6");
/*NOTREACHED*/
}
plen = sin6mask2len(&ifr.ifr_addr);
if ((ifa = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
/* same interface found */
/* need check if something changed */
/* XXX not yet implemented */
return;
}
/*
* New address is found
*/
if ((ifa = MALLOC(struct ifac)) == NULL) {
fatal("malloc: struct ifac");
/*NOTREACHED*/
}
memset(ifa, 0, sizeof(*ifa));
ifa->ifa_conf = ifcp;
ifa->ifa_next = ifcp->ifc_addr;
ifcp->ifc_addr = ifa;
ifa->ifa_addr = sin6->sin6_addr;
ifa->ifa_plen = plen;
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
ifr.ifr_addr = *sin6;
if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
fatal("ioctl: SIOCGIFDSTADDR_IN6");
/*NOTREACHED*/
}
ifa->ifa_raddr = ifr.ifr_dstaddr.sin6_addr;
inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, buf, sizeof(buf));
trace(1, "found address %s/%d -- %s\n",
inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen, buf);
} else {
trace(1, "found address %s/%d\n",
inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen);
}
if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
ifcp->ifc_mylladdr = ifa->ifa_addr;
ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifa->ifa_addr);
memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr,
ifcp->ifc_index);
setindex2ifc(ifcp->ifc_index, ifcp);
ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
if (ifcp->ifc_mtu > RIP6_MAXMTU)
ifcp->ifc_mtu = RIP6_MAXMTU;
if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
fatal("ioctl: SIOCGIFMETRIC");
/*NOTREACHED*/
}
ifcp->ifc_metric = ifr.ifr_metric;
trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
} else
ifcp->ifc_cflags |= IFC_CHANGED;
}
/*
* Receive and process routing messages.
* Update interface information as necesssary.
*/
void
rtrecv()
{
char buf[BUFSIZ];
char *p, *q;
struct rt_msghdr *rtm;
struct ifa_msghdr *ifam;
struct if_msghdr *ifm;
int len;
struct ifc *ifcp, *ic;
int iface = 0, rtable = 0;
struct sockaddr_in6 *rta[RTAX_MAX];
struct sockaddr_in6 mask;
int i, addrs;
struct riprt *rrt;
if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
perror("read from rtsock");
exit(1);
}
if (len < sizeof(*rtm)) {
trace(1, "short read from rtsock: %d (should be > %lu)\n",
len, (u_long)sizeof(*rtm));
return;
}
for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
/* safety against bogus message */
if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
trace(1, "bogus rtmsg: length=%d\n",
((struct rt_msghdr *)p)->rtm_msglen);
break;
}
rtm = NULL;
ifam = NULL;
ifm = NULL;
switch (((struct rt_msghdr *)p)->rtm_type) {
case RTM_NEWADDR:
case RTM_DELADDR:
ifam = (struct ifa_msghdr *)p;
addrs = ifam->ifam_addrs;
q = (char *)(ifam + 1);
break;
case RTM_IFINFO:
ifm = (struct if_msghdr *)p;
addrs = ifm->ifm_addrs;
q = (char *)(ifm + 1);
break;
default:
rtm = (struct rt_msghdr *)p;
addrs = rtm->rtm_addrs;
q = (char *)(rtm + 1);
if (rtm->rtm_version != RTM_VERSION) {
trace(1, "unexpected rtmsg version %d "
"(should be %d)\n",
rtm->rtm_version, RTM_VERSION);
continue;
}
if (rtm->rtm_pid == pid) {
#if 0
trace(1, "rtmsg looped back to me, ignored\n");
#endif
continue;
}
break;
}
memset(&rta, 0, sizeof(rta));
for (i = 0; i < RTAX_MAX; i++) {
if (addrs & (1 << i)) {
rta[i] = (struct sockaddr_in6 *)q;
q += ROUNDUP(rta[i]->sin6_len);
}
}
trace(1, "rtsock: %s (addrs=%x)\n",
rttypes((struct rt_msghdr *)p), addrs);
if (dflag >= 2) {
for (i = 0;
i < ((struct rt_msghdr *)p)->rtm_msglen;
i++) {
fprintf(stderr, "%02x ", p[i] & 0xff);
if (i % 16 == 15) fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
/*
* Easy ones first.
*
* We may be able to optimize by using ifm->ifm_index or
* ifam->ifam_index. For simplicity we don't do that here.
*/
switch (((struct rt_msghdr *)p)->rtm_type) {
case RTM_NEWADDR:
case RTM_IFINFO:
iface++;
continue;
case RTM_ADD:
rtable++;
continue;
case RTM_LOSING:
case RTM_MISS:
case RTM_RESOLVE:
case RTM_GET:
case RTM_LOCK:
/* nothing to be done here */
trace(1, "\tnothing to be done, ignored\n");
continue;
}
#if 0
if (rta[RTAX_DST] == NULL) {
trace(1, "\tno destination, ignored\n");
continue;
}
if (rta[RTAX_DST]->sin6_family != AF_INET6) {
trace(1, "\taf mismatch, ignored\n");
continue;
}
if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
trace(1, "\tlinklocal destination, ignored\n");
continue;
}
if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
trace(1, "\tloopback destination, ignored\n");
continue; /* Loopback */
}
if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
trace(1, "\tmulticast destination, ignored\n");
continue;
}
#endif
/* hard ones */
switch (((struct rt_msghdr *)p)->rtm_type) {
case RTM_NEWADDR:
case RTM_IFINFO:
case RTM_ADD:
case RTM_LOSING:
case RTM_MISS:
case RTM_RESOLVE:
case RTM_GET:
case RTM_LOCK:
/* should already be handled */
fatal("rtrecv: never reach here");
/*NOTREACHED*/
case RTM_DELETE:
if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
trace(1, "\tsome of dst/gw/netamsk are "
"unavailable, ignored\n");
break;
}
if ((rtm->rtm_flags & RTF_HOST) != 0) {
mask.sin6_len = sizeof(mask);
memset(&mask.sin6_addr, 0xff,
sizeof(mask.sin6_addr));
rta[RTAX_NETMASK] = &mask;
} else if (!rta[RTAX_NETMASK]) {
trace(1, "\tsome of dst/gw/netamsk are "
"unavailable, ignored\n");
break;
}
if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
rta[RTAX_NETMASK]) == 0) {
rtable++; /*just to be sure*/
}
break;
case RTM_CHANGE:
case RTM_REDIRECT:
trace(1, "\tnot supported yet, ignored\n");
break;
case RTM_DELADDR:
if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
trace(1, "\tno netmask or ifa given, ignored\n");
break;
}
if (ifam->ifam_index < nindex2ifc)
ifcp = index2ifc[ifam->ifam_index];
else
ifcp = NULL;
if (!ifcp) {
trace(1, "\tinvalid ifam_index %d, ignored\n",
ifam->ifam_index);
break;
}
if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
iface++;
break;
case RTM_OLDADD:
case RTM_OLDDEL:
trace(1, "\tnot supported yet, ignored\n");
break;
}
}
if (iface) {
trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
ifconfig();
for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
if (ifcp->ifc_cflags & IFC_CHANGED) {
if (ifrt(ifcp, 1)) {
for (ic = ifc; ic; ic = ic->ifc_next) {
if (ifcp->ifc_index == ic->ifc_index)
continue;
if (ic->ifc_flags & IFF_UP)
ripsend(ic, &ic->ifc_ripsin,
RRTF_CHANGED);
}
/* Reset the flag */
for (rrt = riprt; rrt; rrt = rrt->rrt_next)
rrt->rrt_rflags &= ~RRTF_CHANGED;
}
ifcp->ifc_cflags &= ~IFC_CHANGED;
}
}
if (rtable) {
trace(1, "rtsock: read routing table again\n");
krtread(1);
}
}
/*
* remove specified route from the internal routing table.
*/
int
rt_del(sdst, sgw, smask)
const struct sockaddr_in6 *sdst;
const struct sockaddr_in6 *sgw;
const struct sockaddr_in6 *smask;
{
const struct in6_addr *dst = NULL;
const struct in6_addr *gw = NULL;
int prefix;
struct netinfo6 ni6;
struct riprt *rrt = NULL;
time_t t_lifetime;
if (sdst->sin6_family != AF_INET6) {
trace(1, "\tother AF, ignored\n");
return -1;
}
if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
|| IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
|| IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
trace(1, "\taddress %s not interesting, ignored\n",
inet6_n2p(&sdst->sin6_addr));
return -1;
}
dst = &sdst->sin6_addr;
if (sgw->sin6_family == AF_INET6) {
/* easy case */
gw = &sgw->sin6_addr;
prefix = sin6mask2len(smask);
} else if (sgw->sin6_family == AF_LINK) {
/*
* Interface route... a hard case. We need to get the prefix
* length from the kernel, but we now are parsing rtmsg.
* We'll purge matching routes from my list, then get the
* fresh list.
*/
struct riprt *longest;
trace(1, "\t%s is an interface route, guessing prefixlen\n",
inet6_n2p(dst));
longest = NULL;
for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
&sdst->sin6_addr)
&& IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
if (!longest
|| longest->rrt_info.rip6_plen <
rrt->rrt_info.rip6_plen) {
longest = rrt;
}
}
}
rrt = longest;
if (!rrt) {
trace(1, "\tno matching interface route found\n");
return -1;
}
gw = &in6addr_loopback;
prefix = rrt->rrt_info.rip6_plen;
} else {
trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
return -1;
}
trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
trace(1, "gw %s\n", inet6_n2p(gw));
t_lifetime = time(NULL) - RIP_LIFETIME;
/* age route for interface address */
memset(&ni6, 0, sizeof(ni6));
ni6.rip6_dest = *dst;
ni6.rip6_plen = prefix;
applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
ni6.rip6_plen);
if (!rrt && (rrt = rtsearch(&ni6, NULL)) == NULL) {
trace(1, "\tno route found\n");
return -1;
}
#if 0
if ((rrt->rrt_flags & RTF_STATIC) == 0) {
trace(1, "\tyou can delete static routes only\n");
} else
#endif
if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
trace(1, "\tgw mismatch: %s <-> ",
inet6_n2p(&rrt->rrt_gw));
trace(1, "%s\n", inet6_n2p(gw));
} else {
trace(1, "\troute found, age it\n");
if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
rrt->rrt_t = t_lifetime;
rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
}
}
return 0;
}
/*
* remove specified address from internal interface/routing table.
*/
int
rt_deladdr(ifcp, sifa, smask)
struct ifc *ifcp;
const struct sockaddr_in6 *sifa;
const struct sockaddr_in6 *smask;
{
const struct in6_addr *addr = NULL;
int prefix;
struct ifac *ifa = NULL;
struct netinfo6 ni6;
struct riprt *rrt = NULL;
time_t t_lifetime;
int updated = 0;
if (sifa->sin6_family != AF_INET6) {
trace(1, "\tother AF, ignored\n");
return -1;
}
addr = &sifa->sin6_addr;
prefix = sin6mask2len(smask);
trace(1, "\tdeleting %s/%d from %s\n",
inet6_n2p(addr), prefix, ifcp->ifc_name);
ifa = ifa_match(ifcp, addr, prefix);
if (!ifa) {
trace(1, "\tno matching ifa found for %s/%d on %s\n",
inet6_n2p(addr), prefix, ifcp->ifc_name);
return -1;
}
if (ifa->ifa_conf != ifcp) {
trace(1, "\taddress table corrupt: back pointer does not match "
"(%s != %s)\n",
ifcp->ifc_name, ifa->ifa_conf->ifc_name);
return -1;
}
/* remove ifa from interface */
if (ifcp->ifc_addr == ifa)
ifcp->ifc_addr = ifa->ifa_next;
else {
struct ifac *p;
for (p = ifcp->ifc_addr; p; p = p->ifa_next) {
if (p->ifa_next == ifa) {
p->ifa_next = ifa->ifa_next;
break;
}
}
}
ifa->ifa_next = NULL;
ifa->ifa_conf = NULL;
t_lifetime = time(NULL) - RIP_LIFETIME;
/* age route for interface address */
memset(&ni6, 0, sizeof(ni6));
ni6.rip6_dest = ifa->ifa_addr;
ni6.rip6_plen = ifa->ifa_plen;
applyplen(&ni6.rip6_dest, ni6.rip6_plen);
trace(1, "\tfind interface route %s/%d on %d\n",
inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
struct in6_addr none;
memset(&none, 0, sizeof(none));
if (rrt->rrt_index == ifcp->ifc_index &&
(IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
trace(1, "\troute found, age it\n");
if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
rrt->rrt_t = t_lifetime;
rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
}
updated++;
} else {
trace(1, "\tnon-interface route found: %s/%d on %d\n",
inet6_n2p(&rrt->rrt_info.rip6_dest),
rrt->rrt_info.rip6_plen,
rrt->rrt_index);
}
} else
trace(1, "\tno interface route found\n");
/* age route for p2p destination */
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
memset(&ni6, 0, sizeof(ni6));
ni6.rip6_dest = ifa->ifa_raddr;
ni6.rip6_plen = 128;
applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
trace(1, "\tfind p2p route %s/%d on %d\n",
inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
ifcp->ifc_index);
if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
if (rrt->rrt_index == ifcp->ifc_index &&
IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) {
trace(1, "\troute found, age it\n");
if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
rrt->rrt_t = t_lifetime;
rrt->rrt_info.rip6_metric =
HOPCNT_INFINITY6;
updated++;
}
} else {
trace(1, "\tnon-p2p route found: %s/%d on %d\n",
inet6_n2p(&rrt->rrt_info.rip6_dest),
rrt->rrt_info.rip6_plen,
rrt->rrt_index);
}
} else
trace(1, "\tno p2p route found\n");
}
return updated ? 0 : -1;
}
/*
* Get each interface address and put those interface routes to the route
* list.
*/
int
ifrt(ifcp, again)
struct ifc *ifcp;
int again;
{
struct ifac *ifa;
struct riprt *rrt, *search_rrt, *prev_rrt, *loop_rrt;
struct netinfo6 *np;
time_t t_lifetime;
int need_trigger = 0;
if (ifcp->ifc_flags & IFF_LOOPBACK)
return 0; /* ignore loopback */
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
ifrt_p2p(ifcp, again);
return 0;
}
for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
if (IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
#if 0
trace(1, "route: %s on %s: "
"skip linklocal interface address\n",
inet6_n2p(&ifa->ifa_addr), ifcp->ifc_name);
#endif
continue;
}
if (IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_addr)) {
#if 0
trace(1, "route: %s: skip unspec interface address\n",
ifcp->ifc_name);
#endif
continue;
}
if (ifcp->ifc_flags & IFF_UP) {
if ((rrt = MALLOC(struct riprt)) == NULL)
fatal("malloc: struct riprt");
memset(rrt, 0, sizeof(*rrt));
rrt->rrt_same = NULL;
rrt->rrt_index = ifcp->ifc_index;
rrt->rrt_t = 0; /* don't age */
rrt->rrt_info.rip6_dest = ifa->ifa_addr;
rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
rrt->rrt_info.rip6_plen = ifa->ifa_plen;
rrt->rrt_flags = RTF_CLONING;
rrt->rrt_rflags |= RRTF_CHANGED;
applyplen(&rrt->rrt_info.rip6_dest, ifa->ifa_plen);
memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
#if 0
/* XXX why gateway address == network adddress? */
rrt->rrt_gw = ifa->ifa_addr;
#endif
np = &rrt->rrt_info;
search_rrt = rtsearch(np, &prev_rrt);
if (search_rrt != NULL) {
if (search_rrt->rrt_info.rip6_metric >
rrt->rrt_info.rip6_metric) {
if (prev_rrt)
prev_rrt->rrt_next = rrt->rrt_next;
else
riprt = rrt->rrt_next;
delroute(&rrt->rrt_info, &rrt->rrt_gw);
free(rrt);
} else {
/* Already have better route */
if (!again) {
trace(1, "route: %s/%d: "
"already registered (%s)\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen,
ifcp->ifc_name);
}
free(rrt);
continue;
}
}
/* Attach the route to the list */
trace(1, "route: %s/%d: register route (%s)\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen,
ifcp->ifc_name);
rrt->rrt_next = riprt;
riprt = rrt;
addroute(rrt, &rrt->rrt_gw, ifcp);
sendrequest(ifcp);
ripsend(ifcp, &ifcp->ifc_ripsin, 0);
need_trigger = 1;
} else {
for (loop_rrt = riprt; loop_rrt; loop_rrt = loop_rrt->rrt_next) {
if (loop_rrt->rrt_index == ifcp->ifc_index) {
t_lifetime = time(NULL) - RIP_LIFETIME;
if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
loop_rrt->rrt_t = t_lifetime;
loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
loop_rrt->rrt_rflags |= RRTF_CHANGED;
need_trigger = 1;
}
}
}
}
}
return need_trigger;
}
/*
* there are couple of p2p interface routing models. "behavior" lets
* you pick one. it looks that gated behavior fits best with BSDs,
* since BSD kernels do not look at prefix length on p2p interfaces.
*/
void
ifrt_p2p(ifcp, again)
struct ifc *ifcp;
int again;
{
struct ifac *ifa;
struct riprt *rrt, *orrt, *prevrrt;
struct netinfo6 *np;
struct in6_addr addr, dest;
int advert, ignore, i;
#define P2PADVERT_NETWORK 1
#define P2PADVERT_ADDR 2
#define P2PADVERT_DEST 4
#define P2PADVERT_MAX 4
const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
const char *category = "";
const char *noadv;
for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
addr = ifa->ifa_addr;
dest = ifa->ifa_raddr;
applyplen(&addr, ifa->ifa_plen);
applyplen(&dest, ifa->ifa_plen);
advert = ignore = 0;
switch (behavior) {
case CISCO:
/*
* honor addr/plen, just like normal shared medium
* interface. this may cause trouble if you reuse
* addr/plen on other interfaces.
*
* advertise addr/plen.
*/
advert |= P2PADVERT_NETWORK;
break;
case GATED:
/*
* prefixlen on p2p interface is meaningless.
* advertise addr/128 and dest/128.
*
* do not install network route to route6d routing
* table (if we do, it would prevent route installation
* for other p2p interface that shares addr/plen).
*
* XXX what should we do if dest is ::? it will not
* get announced anyways (see following filter),
* but we need to think.
*/
advert |= P2PADVERT_ADDR;
advert |= P2PADVERT_DEST;
ignore |= P2PADVERT_NETWORK;
break;
case ROUTE6D:
/*
* just for testing. actually the code is redundant
* given the current p2p interface address assignment
* rule for kame kernel.
*
* intent:
* A/n -> announce A/n
* A B/n, A and B share prefix -> A/n (= B/n)
* A B/n, do not share prefix -> A/128 and B/128
* actually, A/64 and A B/128 are the only cases
* permitted by the kernel:
* A/64 -> A/64
* A B/128 -> A/128 and B/128
*/
if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) {
if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
advert |= P2PADVERT_NETWORK;
else {
advert |= P2PADVERT_ADDR;
advert |= P2PADVERT_DEST;
ignore |= P2PADVERT_NETWORK;
}
} else
advert |= P2PADVERT_NETWORK;
break;
}
for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
if ((ignore & i) != 0)
continue;
if ((rrt = MALLOC(struct riprt)) == NULL) {
fatal("malloc: struct riprt");
/*NOTREACHED*/
}
memset(rrt, 0, sizeof(*rrt));
rrt->rrt_same = NULL;
rrt->rrt_index = ifcp->ifc_index;
rrt->rrt_t = 0; /* don't age */
switch (i) {
case P2PADVERT_NETWORK:
rrt->rrt_info.rip6_dest = ifa->ifa_addr;
rrt->rrt_info.rip6_plen = ifa->ifa_plen;
applyplen(&rrt->rrt_info.rip6_dest,
ifa->ifa_plen);
category = "network";
break;
case P2PADVERT_ADDR:
rrt->rrt_info.rip6_dest = ifa->ifa_addr;
rrt->rrt_info.rip6_plen = 128;
rrt->rrt_gw = in6addr_loopback;
category = "addr";
break;
case P2PADVERT_DEST:
rrt->rrt_info.rip6_dest = ifa->ifa_raddr;
rrt->rrt_info.rip6_plen = 128;
rrt->rrt_gw = ifa->ifa_addr;
category = "dest";
break;
}
if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
#if 0
trace(1, "route: %s: skip unspec/linklocal "
"(%s on %s)\n", category, ifcp->ifc_name);
#endif
free(rrt);
continue;
}
if ((advert & i) == 0) {
rrt->rrt_rflags |= RRTF_NOADVERTISE;
noadv = ", NO-ADV";
} else
noadv = "";
rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
np = &rrt->rrt_info;
orrt = rtsearch(np, &prevrrt);
if (!orrt) {
/* Attach the route to the list */
trace(1, "route: %s/%d: register route "
"(%s on %s%s)\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen,
category, ifcp->ifc_name, noadv);
rrt->rrt_next = riprt;
riprt = rrt;
} else if (rrt->rrt_index != orrt->rrt_index ||
rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
/* swap route */
rrt->rrt_next = orrt->rrt_next;
if (prevrrt)
prevrrt->rrt_next = rrt;
else
riprt = rrt;
free(orrt);
trace(1, "route: %s/%d: update (%s on %s%s)\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen,
category, ifcp->ifc_name, noadv);
} else {
/* Already found */
if (!again) {
trace(1, "route: %s/%d: "
"already registered (%s on %s%s)\n",
inet6_n2p(&np->rip6_dest),
np->rip6_plen, category,
ifcp->ifc_name, noadv);
}
free(rrt);
}
}
}
#undef P2PADVERT_NETWORK
#undef P2PADVERT_ADDR
#undef P2PADVERT_DEST
#undef P2PADVERT_MAX
}
int
getifmtu(ifindex)
int ifindex;
{
int mib[6];
char *buf;
size_t msize;
struct if_msghdr *ifm;
int mtu;
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = AF_INET6;
mib[4] = NET_RT_IFLIST;
mib[5] = ifindex;
if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
fatal("sysctl estimate NET_RT_IFLIST");
/*NOTREACHED*/
}
if ((buf = malloc(msize)) == NULL) {
fatal("malloc");
/*NOTREACHED*/
}
if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
fatal("sysctl NET_RT_IFLIST");
/*NOTREACHED*/
}
ifm = (struct if_msghdr *)buf;
mtu = ifm->ifm_data.ifi_mtu;
#ifdef __FreeBSD__
if (ifindex != ifm->ifm_index) {
fatal("ifindex does not match with ifm_index");
/*NOTREACHED*/
}
#endif
free(buf);
return mtu;
}
const char *
rttypes(rtm)
struct rt_msghdr *rtm;
{
#define RTTYPE(s, f) \
do { \
if (rtm->rtm_type == (f)) \
return (s); \
} while (0)
RTTYPE("ADD", RTM_ADD);
RTTYPE("DELETE", RTM_DELETE);
RTTYPE("CHANGE", RTM_CHANGE);
RTTYPE("GET", RTM_GET);
RTTYPE("LOSING", RTM_LOSING);
RTTYPE("REDIRECT", RTM_REDIRECT);
RTTYPE("MISS", RTM_MISS);
RTTYPE("LOCK", RTM_LOCK);
RTTYPE("OLDADD", RTM_OLDADD);
RTTYPE("OLDDEL", RTM_OLDDEL);
RTTYPE("RESOLVE", RTM_RESOLVE);
RTTYPE("NEWADDR", RTM_NEWADDR);
RTTYPE("DELADDR", RTM_DELADDR);
RTTYPE("IFINFO", RTM_IFINFO);
#ifdef RTM_OLDADD
RTTYPE("OLDADD", RTM_OLDADD);
#endif
#ifdef RTM_OLDDEL
RTTYPE("OLDDEL", RTM_OLDDEL);
#endif
#ifdef RTM_OIFINFO
RTTYPE("OIFINFO", RTM_OIFINFO);
#endif
#ifdef RTM_IFANNOUNCE
RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
#endif
#ifdef RTM_NEWMADDR
RTTYPE("NEWMADDR", RTM_NEWMADDR);
#endif
#ifdef RTM_DELMADDR
RTTYPE("DELMADDR", RTM_DELMADDR);
#endif
#undef RTTYPE
return NULL;
}
const char *
rtflags(rtm)
struct rt_msghdr *rtm;
{
static char buf[BUFSIZ];
/*
* letter conflict should be okay. painful when *BSD diverges...
*/
strlcpy(buf, "", sizeof(buf));
#define RTFLAG(s, f) \
do { \
if (rtm->rtm_flags & (f)) \
strlcat(buf, (s), sizeof(buf)); \
} while (0)
RTFLAG("U", RTF_UP);
RTFLAG("G", RTF_GATEWAY);
RTFLAG("H", RTF_HOST);
RTFLAG("R", RTF_REJECT);
RTFLAG("D", RTF_DYNAMIC);
RTFLAG("M", RTF_MODIFIED);
RTFLAG("d", RTF_DONE);
#ifdef RTF_MASK
RTFLAG("m", RTF_MASK);
#endif
RTFLAG("C", RTF_CLONING);
#ifdef RTF_CLONED
RTFLAG("c", RTF_CLONED);
#endif
#ifdef RTF_PRCLONING
RTFLAG("c", RTF_PRCLONING);
#endif
#ifdef RTF_WASCLONED
RTFLAG("W", RTF_WASCLONED);
#endif
RTFLAG("X", RTF_XRESOLVE);
RTFLAG("L", RTF_LLINFO);
RTFLAG("S", RTF_STATIC);
RTFLAG("B", RTF_BLACKHOLE);
#ifdef RTF_PROTO3
RTFLAG("3", RTF_PROTO3);
#endif
RTFLAG("2", RTF_PROTO2);
RTFLAG("1", RTF_PROTO1);
#ifdef RTF_BROADCAST
RTFLAG("b", RTF_BROADCAST);
#endif
#ifdef RTF_DEFAULT
RTFLAG("d", RTF_DEFAULT);
#endif
#ifdef RTF_ISAROUTER
RTFLAG("r", RTF_ISAROUTER);
#endif
#ifdef RTF_TUNNEL
RTFLAG("T", RTF_TUNNEL);
#endif
#ifdef RTF_AUTH
RTFLAG("A", RTF_AUTH);
#endif
#ifdef RTF_CRYPT
RTFLAG("E", RTF_CRYPT);
#endif
#undef RTFLAG
return buf;
}
const char *
ifflags(flags)
int flags;
{
static char buf[BUFSIZ];
strlcpy(buf, "", sizeof(buf));
#define IFFLAG(s, f) \
do { \
if (flags & f) { \
if (buf[0]) \
strlcat(buf, ",", sizeof(buf)); \
strlcat(buf, s, sizeof(buf)); \
} \
} while (0)
IFFLAG("UP", IFF_UP);
IFFLAG("BROADCAST", IFF_BROADCAST);
IFFLAG("DEBUG", IFF_DEBUG);
IFFLAG("LOOPBACK", IFF_LOOPBACK);
IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
#ifdef IFF_NOTRAILERS
IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
#endif
#ifdef IFF_SMART
IFFLAG("SMART", IFF_SMART);
#endif
IFFLAG("RUNNING", IFF_RUNNING);
IFFLAG("NOARP", IFF_NOARP);
IFFLAG("PROMISC", IFF_PROMISC);
IFFLAG("ALLMULTI", IFF_ALLMULTI);
IFFLAG("OACTIVE", IFF_OACTIVE);
IFFLAG("SIMPLEX", IFF_SIMPLEX);
IFFLAG("LINK0", IFF_LINK0);
IFFLAG("LINK1", IFF_LINK1);
IFFLAG("LINK2", IFF_LINK2);
IFFLAG("MULTICAST", IFF_MULTICAST);
#undef IFFLAG
return buf;
}
void
krtread(again)
int again;
{
int mib[6];
size_t msize;
char *buf, *p, *lim;
struct rt_msghdr *rtm;
int retry;
const char *errmsg;
retry = 0;
buf = NULL;
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = AF_INET6; /* Address family */
mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
mib[5] = 0; /* No flags */
do {
retry++;
errmsg = NULL;
if (buf)
free(buf);
if (sysctl(mib, 6, NULL, &msize, NULL, 0) < 0) {
errmsg = "sysctl estimate";
continue;
}
if ((buf = malloc(msize)) == NULL) {
errmsg = "malloc";
continue;
}
if (sysctl(mib, 6, buf, &msize, NULL, 0) < 0) {
errmsg = "sysctl NET_RT_DUMP";
continue;
}
} while (retry < 5 && errmsg != NULL);
if (errmsg) {
fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
(u_long)msize);
/*NOTREACHED*/
} else if (1 < retry)
syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
lim = buf + msize;
for (p = buf; p < lim; p += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)p;
rt_entry(rtm, again);
}
free(buf);
}
void
rt_entry(rtm, again)
struct rt_msghdr *rtm;
int again;
{
struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
char *rtmp, *ifname = NULL;
struct riprt *rrt, *orrt;
struct netinfo6 *np;
int s;
sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
(RTF_CLONING|RTF_XRESOLVE|RTF_LLINFO|RTF_BLACKHOLE)) {
return; /* not interested in the link route */
}
/* do not look at cloned routes */
#ifdef RTF_WASCLONED
if (rtm->rtm_flags & RTF_WASCLONED)
return;
#endif
#ifdef RTF_CLONED
if (rtm->rtm_flags & RTF_CLONED)
return;
#endif
/*
* do not look at dynamic routes.
* netbsd/openbsd cloned routes have UGHD.
*/
if (rtm->rtm_flags & RTF_DYNAMIC)
return;
rtmp = (char *)(rtm + 1);
/* Destination */
if ((rtm->rtm_addrs & RTA_DST) == 0)
return; /* ignore routes without destination address */
sin6_dst = (struct sockaddr_in6 *)rtmp;
rtmp += ROUNDUP(sin6_dst->sin6_len);
if (rtm->rtm_addrs & RTA_GATEWAY) {
sin6_gw = (struct sockaddr_in6 *)rtmp;
rtmp += ROUNDUP(sin6_gw->sin6_len);
}
if (rtm->rtm_addrs & RTA_NETMASK) {
sin6_mask = (struct sockaddr_in6 *)rtmp;
rtmp += ROUNDUP(sin6_mask->sin6_len);
}
if (rtm->rtm_addrs & RTA_GENMASK) {
sin6_genmask = (struct sockaddr_in6 *)rtmp;
rtmp += ROUNDUP(sin6_genmask->sin6_len);
}
if (rtm->rtm_addrs & RTA_IFP) {
sin6_ifp = (struct sockaddr_in6 *)rtmp;
rtmp += ROUNDUP(sin6_ifp->sin6_len);
}
/* Destination */
if (sin6_dst->sin6_family != AF_INET6)
return;
if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
return; /* Link-local */
if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
return; /* Loopback */
if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
return;
if ((rrt = MALLOC(struct riprt)) == NULL) {
fatal("malloc: struct riprt");
/*NOTREACHED*/
}
memset(rrt, 0, sizeof(*rrt));
np = &rrt->rrt_info;
rrt->rrt_same = NULL;
rrt->rrt_t = time(NULL);
if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
rrt->rrt_t = 0; /* Don't age static routes */
np->rip6_tag = 0;
np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
if (np->rip6_metric < 1)
np->rip6_metric = 1;
rrt->rrt_flags = rtm->rtm_flags;
np->rip6_dest = sin6_dst->sin6_addr;
/* Mask or plen */
if (rtm->rtm_flags & RTF_HOST)
np->rip6_plen = 128; /* Host route */
else if (sin6_mask)
np->rip6_plen = sin6mask2len(sin6_mask);
else
np->rip6_plen = 0;
orrt = rtsearch(np, NULL);
if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
/* Already found */
if (!again) {
trace(1, "route: %s/%d flags %s: already registered\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen,
rtflags(rtm));
}
free(rrt);
return;
}
/* Gateway */
if (!sin6_gw)
memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
else {
if (sin6_gw->sin6_family == AF_INET6)
rrt->rrt_gw = sin6_gw->sin6_addr;
else if (sin6_gw->sin6_family == AF_LINK) {
/* XXX in case ppp link? */
rrt->rrt_gw = in6addr_loopback;
} else
memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
}
trace(1, "route: %s/%d flags %s",
inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
/* Interface */
s = rtm->rtm_index;
if (s < nindex2ifc && index2ifc[s])
ifname = index2ifc[s]->ifc_name;
else {
trace(1, " not configured\n");
free(rrt);
return;
}
trace(1, " if %s sock %d", ifname, s);
rrt->rrt_index = s;
trace(1, "\n");
/* Check gateway */
if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
!IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)
#ifdef __FreeBSD__
&& (rrt->rrt_flags & RTF_LOCAL) == 0
#endif
) {
trace(0, "***** Gateway %s is not a link-local address.\n",
inet6_n2p(&rrt->rrt_gw));
trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
}
/* Put it to the route list */
if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
/* replace route list */
rrt->rrt_next = orrt->rrt_next;
*orrt = *rrt;
trace(1, "route: %s/%d flags %s: replace new route\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen,
rtflags(rtm));
free(rrt);
} else {
rrt->rrt_next = riprt;
riprt = rrt;
}
}
int
addroute(rrt, gw, ifcp)
struct riprt *rrt;
const struct in6_addr *gw;
struct ifc *ifcp;
{
struct netinfo6 *np;
u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
struct rt_msghdr *rtm;
struct sockaddr_in6 *sin6;
int len;
np = &rrt->rrt_info;
inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
np->rip6_metric - 1, buf2);
if (rtlog)
fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
np->rip6_metric - 1, buf2);
if (nflag)
return 0;
memset(buf, 0, sizeof(buf));
rtm = (struct rt_msghdr *)buf;
rtm->rtm_type = RTM_ADD;
rtm->rtm_version = RTM_VERSION;
rtm->rtm_seq = ++seq;
rtm->rtm_pid = pid;
rtm->rtm_flags = rrt->rrt_flags;
rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
rtm->rtm_inits = RTV_HOPCOUNT;
sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
/* Destination */
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = np->rip6_dest;
sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
/* Gateway */
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = *gw;
sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
/* Netmask */
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = *(plen2mask(np->rip6_plen));
sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
len = (char *)sin6 - (char *)buf;
rtm->rtm_msglen = len;
if (write(rtsock, buf, len) > 0)
return 0;
if (errno == EEXIST) {
trace(0, "ADD: Route already exists %s/%d gw %s\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
if (rtlog)
fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
} else {
trace(0, "Can not write to rtsock (addroute): %s\n",
strerror(errno));
if (rtlog)
fprintf(rtlog, "\tCan not write to rtsock: %s\n",
strerror(errno));
}
return -1;
}
int
delroute(np, gw)
struct netinfo6 *np;
struct in6_addr *gw;
{
u_char buf[BUFSIZ], buf2[BUFSIZ];
struct rt_msghdr *rtm;
struct sockaddr_in6 *sin6;
int len;
inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
np->rip6_plen, buf2);
if (rtlog)
fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
if (nflag)
return 0;
memset(buf, 0, sizeof(buf));
rtm = (struct rt_msghdr *)buf;
rtm->rtm_type = RTM_DELETE;
rtm->rtm_version = RTM_VERSION;
rtm->rtm_seq = ++seq;
rtm->rtm_pid = pid;
rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
if (np->rip6_plen == sizeof(struct in6_addr) * 8)
rtm->rtm_flags |= RTF_HOST;
rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
/* Destination */
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = np->rip6_dest;
sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
/* Gateway */
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = *gw;
sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
/* Netmask */
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = *(plen2mask(np->rip6_plen));
sin6 = (struct sockaddr_in6 *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
len = (char *)sin6 - (char *)buf;
rtm->rtm_msglen = len;
if (write(rtsock, buf, len) >= 0)
return 0;
if (errno == ESRCH) {
trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
if (rtlog)
fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
} else {
trace(0, "Can not write to rtsock (delroute): %s\n",
strerror(errno));
if (rtlog)
fprintf(rtlog, "\tCan not write to rtsock: %s\n",
strerror(errno));
}
return -1;
}
struct in6_addr *
getroute(np, gw)
struct netinfo6 *np;
struct in6_addr *gw;
{
u_char buf[BUFSIZ];
u_long myseq;
int len;
struct rt_msghdr *rtm;
struct sockaddr_in6 *sin6;
rtm = (struct rt_msghdr *)buf;
len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
memset(rtm, 0, len);
rtm->rtm_type = RTM_GET;
rtm->rtm_version = RTM_VERSION;
myseq = ++seq;
rtm->rtm_seq = myseq;
rtm->rtm_addrs = RTA_DST;
rtm->rtm_msglen = len;
sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = np->rip6_dest;
if (write(rtsock, buf, len) < 0) {
if (errno == ESRCH) /* No such route found */
return NULL;
perror("write to rtsock");
exit(1);
}
do {
if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
perror("read from rtsock");
exit(1);
}
rtm = (struct rt_msghdr *)buf;
} while (rtm->rtm_seq != myseq || rtm->rtm_pid != pid);
sin6 = (struct sockaddr_in6 *)&buf[sizeof(struct rt_msghdr)];
if (rtm->rtm_addrs & RTA_DST) {
sin6 = (struct sockaddr_in6 *)
((char *)sin6 + ROUNDUP(sin6->sin6_len));
}
if (rtm->rtm_addrs & RTA_GATEWAY) {
*gw = sin6->sin6_addr;
return gw;
}
return NULL;
}
const char *
inet6_n2p(p)
const struct in6_addr *p;
{
static char buf[BUFSIZ];
return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
}
void
ifrtdump(sig)
int sig;
{
ifdump(sig);
rtdump(sig);
}
void
ifdump(sig)
int sig;
{
struct ifc *ifcp;
FILE *dump;
int i;
if (sig == 0)
dump = stderr;
else
if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
dump = stderr;
fprintf(dump, "%s: Interface Table Dump\n", hms());
fprintf(dump, " Number of interfaces: %d\n", nifc);
for (i = 0; i < 2; i++) {
fprintf(dump, " %sadvertising interfaces:\n", i ? "non-" : "");
for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
if (i == 0) {
if ((ifcp->ifc_flags & IFF_UP) == 0)
continue;
if (iff_find(ifcp, 'N') != NULL)
continue;
} else {
if (ifcp->ifc_flags & IFF_UP)
continue;
}
ifdump0(dump, ifcp);
}
}
fprintf(dump, "\n");
if (dump != stderr)
fclose(dump);
}
void
ifdump0(dump, ifcp)
FILE *dump;
const struct ifc *ifcp;
{
struct ifac *ifa;
struct iff *iffp;
char buf[BUFSIZ];
const char *ft;
int addr;
fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
inet6_n2p(&ifcp->ifc_mylladdr),
ifcp->ifc_mtu, ifcp->ifc_metric);
for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr,
buf, sizeof(buf));
fprintf(dump, "\t%s/%d -- %s\n",
inet6_n2p(&ifa->ifa_addr),
ifa->ifa_plen, buf);
} else {
fprintf(dump, "\t%s/%d\n",
inet6_n2p(&ifa->ifa_addr),
ifa->ifa_plen);
}
}
if (ifcp->ifc_filter) {
fprintf(dump, "\tFilter:");
for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
addr = 0;
switch (iffp->iff_type) {
case 'A':
ft = "Aggregate"; addr++; break;
case 'N':
ft = "No-use"; break;
case 'O':
ft = "Advertise-only"; addr++; break;
case 'T':
ft = "Default-only"; break;
case 'L':
ft = "Listen-only"; addr++; break;
default:
snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
ft = buf;
addr++;
break;
}
fprintf(dump, " %s", ft);
if (addr) {
fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
iffp->iff_plen);
}
}
fprintf(dump, "\n");
}
}
void
rtdump(sig)
int sig;
{
struct riprt *rrt;
char buf[BUFSIZ];
FILE *dump;
time_t t, age;
if (sig == 0)
dump = stderr;
else
if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
dump = stderr;
t = time(NULL);
fprintf(dump, "\n%s: Routing Table Dump\n", hms());
for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
if (rrt->rrt_t == 0)
age = 0;
else
age = t - rrt->rrt_t;
inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
buf, sizeof(buf));
fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
index2ifc[rrt->rrt_index]->ifc_name,
inet6_n2p(&rrt->rrt_gw),
rrt->rrt_info.rip6_metric, (long)age);
if (rrt->rrt_info.rip6_tag) {
fprintf(dump, " tag(0x%04x)",
ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
}
if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
fprintf(dump, " NOT-LL");
if (rrt->rrt_rflags & RRTF_NOADVERTISE)
fprintf(dump, " NO-ADV");
fprintf(dump, "\n");
}
fprintf(dump, "\n");
if (dump != stderr)
fclose(dump);
}
/*
* Parse the -A (and -O) options and put corresponding filter object to the
* specified interface structures. Each of the -A/O option has the following
* syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
* -O 5f09:c400::/32,ef0,ef1 (only when match)
*/
void
filterconfig()
{
int i;
char *p, *ap, *iflp, *ifname;
struct iff ftmp, *iff_obj;
struct ifc *ifcp;
struct riprt *rrt;
#if 0
struct in6_addr gw;
#endif
for (i = 0; i < nfilter; i++) {
ap = filter[i];
iflp = NULL;
ifcp = NULL;
if (filtertype[i] == 'N' || filtertype[i] == 'T') {
iflp = ap;
goto ifonly;
}
if ((p = index(ap, ',')) != NULL) {
*p++ = '\0';
iflp = p;
}
if ((p = index(ap, '/')) == NULL) {
fatal("no prefixlen specified for '%s'", ap);
/*NOTREACHED*/
}
*p++ = '\0';
if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) {
fatal("invalid prefix specified for '%s'", ap);
/*NOTREACHED*/
}
ftmp.iff_plen = atoi(p);
ftmp.iff_next = NULL;
applyplen(&ftmp.iff_addr, ftmp.iff_plen);
ifonly:
ftmp.iff_type = filtertype[i];
if (iflp == NULL || *iflp == '\0') {
fatal("no interface specified for '%s'", ap);
/*NOTREACHED*/
}
/* parse the interface listing portion */
while (iflp) {
ifname = iflp;
if ((iflp = index(iflp, ',')) != NULL)
*iflp++ = '\0';
ifcp = ifc_find(ifname);
if (ifcp == NULL) {
fatal("no interface %s exists", ifname);
/*NOTREACHED*/
}
iff_obj = (struct iff *)malloc(sizeof(struct iff));
if (iff_obj == NULL) {
fatal("malloc of iff_obj");
/*NOTREACHED*/
}
memcpy((void *)iff_obj, (void *)&ftmp,
sizeof(struct iff));
/* link it to the interface filter */
iff_obj->iff_next = ifcp->ifc_filter;
ifcp->ifc_filter = iff_obj;
}
/*
* -A: aggregate configuration.
*/
if (filtertype[i] != 'A')
continue;
/* put the aggregate to the kernel routing table */
rrt = (struct riprt *)malloc(sizeof(struct riprt));
if (rrt == NULL) {
fatal("malloc: rrt");
/*NOTREACHED*/
}
memset(rrt, 0, sizeof(struct riprt));
rrt->rrt_info.rip6_dest = ftmp.iff_addr;
rrt->rrt_info.rip6_plen = ftmp.iff_plen;
rrt->rrt_info.rip6_metric = 1;
rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
rrt->rrt_gw = in6addr_loopback;
rrt->rrt_flags = RTF_UP | RTF_REJECT;
rrt->rrt_rflags = RRTF_AGGREGATE;
rrt->rrt_t = 0;
rrt->rrt_index = loopifindex;
#if 0
if (getroute(&rrt->rrt_info, &gw)) {
#if 0
/*
* When the address has already been registered in the
* kernel routing table, it should be removed
*/
delroute(&rrt->rrt_info, &gw);
#else
/* it is safer behavior */
errno = EINVAL;
fatal("%s/%u already in routing table, "
"cannot aggregate",
inet6_n2p(&rrt->rrt_info.rip6_dest),
rrt->rrt_info.rip6_plen);
/*NOTREACHED*/
#endif
}
#endif
/* Put the route to the list */
rrt->rrt_next = riprt;
riprt = rrt;
trace(1, "Aggregate: %s/%d for %s\n",
inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen,
ifcp->ifc_name);
/* Add this route to the kernel */
if (nflag) /* do not modify kernel routing table */
continue;
addroute(rrt, &in6addr_loopback, loopifcp);
}
}
/***************** utility functions *****************/
/*
* Returns a pointer to ifac whose address and prefix length matches
* with the address and prefix length specified in the arguments.
*/
struct ifac *
ifa_match(ifcp, ia, plen)
const struct ifc *ifcp;
const struct in6_addr *ia;
int plen;
{
struct ifac *ifa;
for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) &&
ifa->ifa_plen == plen)
break;
}
return ifa;
}
/*
* Return a pointer to riprt structure whose address and prefix length
* matches with the address and prefix length found in the argument.
* Note: This is not a rtalloc(). Therefore exact match is necessary.
*/
struct riprt *
rtsearch(np, prev_rrt)
struct netinfo6 *np;
struct riprt **prev_rrt;
{
struct riprt *rrt;
if (prev_rrt)
*prev_rrt = NULL;
for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
&np->rip6_dest))
return rrt;
if (prev_rrt)
*prev_rrt = rrt;
}
if (prev_rrt)
*prev_rrt = NULL;
return 0;
}
int
sin6mask2len(sin6)
const struct sockaddr_in6 *sin6;
{
return mask2len(&sin6->sin6_addr,
sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
}
int
mask2len(addr, lenlim)
const struct in6_addr *addr;
int lenlim;
{
int i = 0, j;
const u_char *p = (const u_char *)addr;
for (j = 0; j < lenlim; j++, p++) {
if (*p != 0xff)
break;
i += 8;
}
if (j < lenlim) {
switch (*p) {
#define MASKLEN(m, l) case m: do { i += l; break; } while (0)
MASKLEN(0xfe, 7); break;
MASKLEN(0xfc, 6); break;
MASKLEN(0xf8, 5); break;
MASKLEN(0xf0, 4); break;
MASKLEN(0xe0, 3); break;
MASKLEN(0xc0, 2); break;
MASKLEN(0x80, 1); break;
#undef MASKLEN
}
}
return i;
}
void
applymask(addr, mask)
struct in6_addr *addr, *mask;
{
int i;
u_long *p, *q;
p = (u_long *)addr; q = (u_long *)mask;
for (i = 0; i < 4; i++)
*p++ &= *q++;
}
static const u_char plent[8] = {
0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
};
void
applyplen(ia, plen)
struct in6_addr *ia;
int plen;
{
u_char *p;
int i;
p = ia->s6_addr;
for (i = 0; i < 16; i++) {
if (plen <= 0)
*p = 0;
else if (plen < 8)
*p &= plent[plen];
p++, plen -= 8;
}
}
static const int pl2m[9] = {
0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
};
struct in6_addr *
plen2mask(n)
int n;
{
static struct in6_addr ia;
u_char *p;
int i;
memset(&ia, 0, sizeof(struct in6_addr));
p = (u_char *)&ia;
for (i = 0; i < 16; i++, p++, n -= 8) {
if (n >= 8) {
*p = 0xff;
continue;
}
*p = pl2m[n];
break;
}
return &ia;
}
char *
allocopy(p)
char *p;
{
int len = strlen(p) + 1;
char *q = (char *)malloc(len);
if (!q) {
fatal("malloc");
/*NOTREACHED*/
}
strlcpy(q, p, len);
return q;
}
char *
hms()
{
static char buf[BUFSIZ];
time_t t;
struct tm *tm;
t = time(NULL);
if ((tm = localtime(&t)) == 0) {
fatal("localtime");
/*NOTREACHED*/
}
snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
tm->tm_sec);
return buf;
}
#define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
int
ripinterval(timer)
int timer;
{
double r = rand();
interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
nextalarm = time(NULL) + interval;
return interval;
}
time_t
ripsuptrig()
{
time_t t;
double r = rand();
t = (int)(RIP_TRIG_INT6_MIN +
(RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
sup_trig_update = time(NULL) + t;
return t;
}
void
#ifdef __STDC__
fatal(const char *fmt, ...)
#else
fatal(fmt, va_alist)
char *fmt;
va_dcl
#endif
{
va_list ap;
char buf[1024];
#ifdef __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
vsnprintf(buf, sizeof(buf), fmt, ap);
perror(buf);
syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
rtdexit();
va_end(ap);
}
void
#ifdef __STDC__
tracet(int level, const char *fmt, ...)
#else
tracet(level, fmt, va_alist)
int level;
char *fmt;
va_dcl
#endif
{
va_list ap;
#ifdef __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
if (level <= dflag) {
fprintf(stderr, "%s: ", hms());
vfprintf(stderr, fmt, ap);
}
if (dflag) {
if (level > 0)
vsyslog(LOG_DEBUG, fmt, ap);
else
vsyslog(LOG_WARNING, fmt, ap);
}
va_end(ap);
}
void
#ifdef __STDC__
trace(int level, const char *fmt, ...)
#else
trace(level, fmt, va_alist)
int level;
char *fmt;
va_dcl
#endif
{
va_list ap;
#ifdef __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
if (level <= dflag)
vfprintf(stderr, fmt, ap);
if (dflag) {
if (level > 0)
vsyslog(LOG_DEBUG, fmt, ap);
else
vsyslog(LOG_WARNING, fmt, ap);
}
va_end(ap);
}
unsigned int
if_maxindex()
{
struct if_nameindex *p, *p0;
unsigned int max = 0;
p0 = if_nameindex();
for (p = p0; p && p->if_index && p->if_name; p++) {
if (max < p->if_index)
max = p->if_index;
}
if_freenameindex(p0);
return max;
}
struct ifc *
ifc_find(name)
char *name;
{
struct ifc *ifcp;
for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
if (strcmp(name, ifcp->ifc_name) == 0)
return ifcp;
}
return (struct ifc *)NULL;
}
struct iff *
iff_find(ifcp, type)
struct ifc *ifcp;
int type;
{
struct iff *iffp;
for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
if (iffp->iff_type == type)
return iffp;
}
return NULL;
}
void
setindex2ifc(idx, ifcp)
int idx;
struct ifc *ifcp;
{
int n;
struct ifc **p;
if (!index2ifc) {
nindex2ifc = 5; /*initial guess*/
index2ifc = (struct ifc **)
malloc(sizeof(*index2ifc) * nindex2ifc);
if (index2ifc == NULL) {
fatal("malloc");
/*NOTREACHED*/
}
memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
}
n = nindex2ifc;
while (nindex2ifc <= idx)
nindex2ifc *= 2;
if (n != nindex2ifc) {
p = (struct ifc **)realloc(index2ifc,
sizeof(*index2ifc) * nindex2ifc);
if (p == NULL) {
fatal("realloc");
/*NOTREACHED*/
}
memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
index2ifc = p;
}
index2ifc[idx] = ifcp;
}
|