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
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
|
/* $KAME: dhcp6s.c,v 1.162 2005/10/04 11:53:32 suz Exp $ */
/*
* Copyright (C) 1998 and 1999 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.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/uio.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include <errno.h>
#include <net/if.h>
#ifdef __FreeBSD__
#include <net/if_var.h>
#endif
#include <netinet/in.h>
#ifdef __KAME__
#include <netinet6/in6_var.h>
#endif
#include <arpa/inet.h>
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <netdb.h>
#include <limits.h>
#include <dhcp6.h>
#include <config.h>
#include <common.h>
#include <timer.h>
#include <auth.h>
#include <base64.h>
#include <control.h>
#include <dhcp6_ctl.h>
#include <signal.h>
#include <lease.h>
#define DUID_FILE LOCALDBDIR "/dhcp6s_duid"
#define DHCP6S_CONF SYSCONFDIR "/dhcp6s.conf"
#define DEFAULT_KEYFILE SYSCONFDIR "/dhcp6sctlkey"
#define DHCP6S_PIDFILE "/var/run/dhcp6s.pid"
#define CTLSKEW 300
typedef enum { DHCP6_BINDING_IA } dhcp6_bindingtype_t;
struct dhcp6_binding {
TAILQ_ENTRY(dhcp6_binding) link;
dhcp6_bindingtype_t type;
/* identifier of the binding */
struct duid clientid;
/* additional identifiers for IA-based bindings */
int iatype;
u_int32_t iaid;
/*
* configuration information of this binding,
* which is type-dependent.
*/
union {
struct dhcp6_list uv_list;
} val;
#define val_list val.uv_list
u_int32_t duration;
time_t updatetime;
struct dhcp6_timer *timer;
};
static TAILQ_HEAD(, dhcp6_binding) dhcp6_binding_head;
struct relayinfo {
TAILQ_ENTRY(relayinfo) link;
u_int hcnt; /* hop count */
struct in6_addr linkaddr; /* link address */
struct in6_addr peeraddr; /* peer address */
struct dhcp6_vbuf relay_ifid; /* Interface ID (if provided) */
struct dhcp6_vbuf relay_msg; /* relay message */
};
TAILQ_HEAD(relayinfolist, relayinfo);
static int debug = 0;
static sig_atomic_t sig_flags = 0;
#define SIGF_TERM 0x1
const dhcp6_mode_t dhcp6_mode = DHCP6_MODE_SERVER;
char *device = NULL;
int ifidx;
int insock; /* inbound UDP port */
int outsock; /* outbound UDP port */
int ctlsock = -1; /* control TCP port */
char *ctladdr = DEFAULT_SERVER_CONTROL_ADDR;
char *ctlport = DEFAULT_SERVER_CONTROL_PORT;
static const struct sockaddr_in6 *sa6_any_downstream, *sa6_any_relay;
static struct msghdr rmh;
static char rdatabuf[BUFSIZ];
static int rmsgctllen;
static char *conffile = DHCP6S_CONF;
static char *rmsgctlbuf;
static struct duid server_duid;
static struct dhcp6_list arg_dnslist;
static char *ctlkeyfile = DEFAULT_KEYFILE;
static struct keyinfo *ctlkey = NULL;
static int ctldigestlen;
static char *pid_file = DHCP6S_PIDFILE;
static inline int get_val32 __P((char **, int *, u_int32_t *));
static inline int get_val __P((char **, int *, void *, size_t));
static void usage __P((void));
static void server6_init __P((void));
static void server6_mainloop __P((void));
static int server6_do_ctlcommand __P((char *, ssize_t));
static void server6_reload __P((void));
static void server6_stop __P((void));
static void server6_recv __P((int));
static void process_signals __P((void));
static void server6_signal __P((int));
static void free_relayinfo __P((struct relayinfo *));
static int process_relayforw __P((struct dhcp6 **, struct dhcp6opt **,
struct relayinfolist *, struct sockaddr *));
static int set_statelessinfo __P((int, struct dhcp6_optinfo *));
static int react_solicit __P((struct dhcp6_if *, struct dhcp6 *, ssize_t,
struct dhcp6_optinfo *, struct sockaddr *, int, struct relayinfolist *));
static int react_request __P((struct dhcp6_if *, struct in6_pktinfo *,
struct dhcp6 *, ssize_t, struct dhcp6_optinfo *, struct sockaddr *, int,
struct relayinfolist *));
static int react_renew __P((struct dhcp6_if *, struct in6_pktinfo *,
struct dhcp6 *, ssize_t, struct dhcp6_optinfo *, struct sockaddr *, int,
struct relayinfolist *));
static int react_rebind __P((struct dhcp6_if *, struct dhcp6 *, ssize_t,
struct dhcp6_optinfo *, struct sockaddr *, int, struct relayinfolist *));
static int react_release __P((struct dhcp6_if *, struct in6_pktinfo *,
struct dhcp6 *, ssize_t, struct dhcp6_optinfo *, struct sockaddr *, int,
struct relayinfolist *));
static int react_decline __P((struct dhcp6_if *, struct in6_pktinfo *,
struct dhcp6 *, ssize_t, struct dhcp6_optinfo *, struct sockaddr *, int,
struct relayinfolist *));
static int react_confirm __P((struct dhcp6_if *, struct in6_pktinfo *,
struct dhcp6 *, ssize_t,
struct dhcp6_optinfo *, struct sockaddr *, int, struct relayinfolist *));
static int react_informreq __P((struct dhcp6_if *, struct dhcp6 *, ssize_t,
struct dhcp6_optinfo *, struct sockaddr *, int, struct relayinfolist *));
static int server6_send __P((int, struct dhcp6_if *, struct dhcp6 *,
struct dhcp6_optinfo *, struct sockaddr *, int, struct dhcp6_optinfo *,
struct relayinfolist *, struct host_conf *));
static int make_ia_stcode __P((int, u_int32_t, u_int16_t,
struct dhcp6_list *));
static int update_ia __P((int, struct dhcp6_listval *,
struct dhcp6_list *, struct dhcp6_optinfo *));
static int release_binding_ia __P((struct dhcp6_listval *, struct dhcp6_list *,
struct dhcp6_optinfo *));
static int decline_binding_ia __P((struct dhcp6_listval *, struct dhcp6_list *,
struct dhcp6_optinfo *));
static int make_ia __P((struct dhcp6_listval *, struct dhcp6_list *,
struct dhcp6_list *, struct host_conf *, int));
static int make_match_ia __P((struct dhcp6_listval *, struct dhcp6_list *,
struct dhcp6_list *));
static int make_iana_from_pool __P((struct dhcp6_poolspec *,
struct dhcp6_listval *, struct dhcp6_list *));
static void calc_ia_timo __P((struct dhcp6_ia *, struct dhcp6_list *,
struct host_conf *));
static void update_binding_duration __P((struct dhcp6_binding *));
static struct dhcp6_binding *add_binding __P((struct duid *,
dhcp6_bindingtype_t, int, u_int32_t, void *));
static struct dhcp6_binding *find_binding __P((struct duid *,
dhcp6_bindingtype_t, int, u_int32_t));
static void update_binding __P((struct dhcp6_binding *));
static void remove_binding __P((struct dhcp6_binding *));
static void free_binding __P((struct dhcp6_binding *));
static struct dhcp6_timer *binding_timo __P((void *));
static struct dhcp6_listval *find_binding_ia __P((struct dhcp6_listval *,
struct dhcp6_binding *));
static char *bindingstr __P((struct dhcp6_binding *));
static int process_auth __P((struct dhcp6 *, ssize_t, struct host_conf *,
struct dhcp6_optinfo *, struct dhcp6_optinfo *));
static inline char *clientstr __P((struct host_conf *, struct duid *));
int
main(argc, argv)
int argc;
char **argv;
{
int ch, pid;
struct in6_addr a;
struct dhcp6_listval *dlv;
char *progname;
FILE *pidfp;
if ((progname = strrchr(*argv, '/')) == NULL)
progname = *argv;
else
progname++;
TAILQ_INIT(&arg_dnslist);
TAILQ_INIT(&dnslist);
TAILQ_INIT(&dnsnamelist);
TAILQ_INIT(&siplist);
TAILQ_INIT(&sipnamelist);
TAILQ_INIT(&ntplist);
TAILQ_INIT(&nislist);
TAILQ_INIT(&nisnamelist);
TAILQ_INIT(&nisplist);
TAILQ_INIT(&nispnamelist);
TAILQ_INIT(&bcmcslist);
TAILQ_INIT(&bcmcsnamelist);
srandom(time(NULL) & getpid());
while ((ch = getopt(argc, argv, "c:dDfk:n:p:P:")) != -1) {
switch (ch) {
case 'c':
conffile = optarg;
break;
case 'd':
debug = 1;
break;
case 'D':
debug = 2;
break;
case 'f':
foreground++;
break;
case 'k':
ctlkeyfile = optarg;
break;
case 'n':
warnx("-n dnsserv option was obsoleted. "
"use configuration file.");
if (inet_pton(AF_INET6, optarg, &a) != 1) {
errx(1, "invalid DNS server %s", optarg);
/* NOTREACHED */
}
if ((dlv = malloc(sizeof *dlv)) == NULL) {
errx(1, "malloc failed for a DNS server");
/* NOTREACHED */
}
dlv->val_addr6 = a;
TAILQ_INSERT_TAIL(&arg_dnslist, dlv, link);
break;
case 'p':
ctlport = optarg;
break;
case 'P':
pid_file = optarg;
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc != 1) {
usage();
/* NOTREACHED */
}
device = argv[0];
if (foreground == 0)
openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
setloglevel(debug);
if (ifinit(device) == NULL)
exit(1);
if ((cfparse(conffile)) != 0) {
dprintf(LOG_ERR, FNAME, "failed to parse configuration file");
exit(1);
}
if (foreground == 0) {
if (daemon(0, 0) < 0)
err(1, "daemon");
}
/* dump current PID */
pid = getpid();
if ((pidfp = fopen(pid_file, "w")) != NULL) {
fprintf(pidfp, "%d\n", pid);
fclose(pidfp);
}
/* prohibit a mixture of old and new style of DNS server config */
if (!TAILQ_EMPTY(&arg_dnslist)) {
if (!TAILQ_EMPTY(&dnslist)) {
dprintf(LOG_INFO, FNAME, "do not specify DNS servers "
"both by command line and by configuration file.");
exit(1);
}
dhcp6_move_list(&dnslist, &arg_dnslist);
TAILQ_INIT(&arg_dnslist);
}
server6_init();
server6_mainloop();
exit(0);
}
static void
usage()
{
fprintf(stderr,
"usage: dhcp6s [-c configfile] [-dDf] [-k ctlkeyfile] "
"[-p ctlport] [-P pidfile] intface\n");
exit(0);
}
/*------------------------------------------------------------*/
void
server6_init()
{
struct addrinfo hints;
struct addrinfo *res, *res2;
int error;
int on = 1;
struct ipv6_mreq mreq6;
static struct iovec iov;
static struct sockaddr_in6 sa6_any_downstream_storage;
static struct sockaddr_in6 sa6_any_relay_storage;
TAILQ_INIT(&dhcp6_binding_head);
if (lease_init() != 0) {
dprintf(LOG_ERR, FNAME, "failed to initialize the lease table");
exit(1);
}
ifidx = if_nametoindex(device);
if (ifidx == 0) {
dprintf(LOG_ERR, FNAME, "invalid interface %s", device);
exit(1);
}
/* get our DUID */
if (get_duid(DUID_FILE, &server_duid)) {
dprintf(LOG_ERR, FNAME, "failed to get a DUID");
exit(1);
}
if (dhcp6_ctl_authinit(ctlkeyfile, &ctlkey, &ctldigestlen) != 0) {
dprintf(LOG_NOTICE, FNAME,
"failed to initialize control message authentication");
/* run the server anyway */
}
/* initialize send/receive buffer */
iov.iov_base = (caddr_t)rdatabuf;
iov.iov_len = sizeof(rdatabuf);
rmh.msg_iov = &iov;
rmh.msg_iovlen = 1;
rmsgctllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
if ((rmsgctlbuf = (char *)malloc(rmsgctllen)) == NULL) {
dprintf(LOG_ERR, FNAME, "memory allocation failed");
exit(1);
}
/* initialize socket */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
error = getaddrinfo(NULL, DH6PORT_UPSTREAM, &hints, &res);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
insock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (insock < 0) {
dprintf(LOG_ERR, FNAME, "socket(insock): %s",
strerror(errno));
exit(1);
}
if (setsockopt(insock, SOL_SOCKET, SO_REUSEPORT, &on,
sizeof(on)) < 0) {
dprintf(LOG_ERR, FNAME, "setsockopt(insock, SO_REUSEPORT): %s",
strerror(errno));
exit(1);
}
if (setsockopt(insock, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) < 0) {
dprintf(LOG_ERR, FNAME, "setsockopt(insock, SO_REUSEADDR): %s",
strerror(errno));
exit(1);
}
#ifdef IPV6_RECVPKTINFO
if (setsockopt(insock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
sizeof(on)) < 0) {
dprintf(LOG_ERR, FNAME,
"setsockopt(inbound, IPV6_RECVPKTINFO): %s",
strerror(errno));
exit(1);
}
#else
if (setsockopt(insock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
sizeof(on)) < 0) {
dprintf(LOG_ERR, FNAME,
"setsockopt(inbound, IPV6_PKTINFO): %s",
strerror(errno));
exit(1);
}
#endif
#ifdef IPV6_V6ONLY
if (setsockopt(insock, IPPROTO_IPV6, IPV6_V6ONLY,
&on, sizeof(on)) < 0) {
dprintf(LOG_ERR, FNAME,
"setsockopt(inbound, IPV6_V6ONLY): %s", strerror(errno));
exit(1);
}
#endif
if (bind(insock, res->ai_addr, res->ai_addrlen) < 0) {
dprintf(LOG_ERR, FNAME, "bind(insock): %s", strerror(errno));
exit(1);
}
freeaddrinfo(res);
hints.ai_flags = 0;
error = getaddrinfo(DH6ADDR_ALLAGENT, DH6PORT_UPSTREAM, &hints, &res2);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
memset(&mreq6, 0, sizeof(mreq6));
mreq6.ipv6mr_interface = ifidx;
memcpy(&mreq6.ipv6mr_multiaddr,
&((struct sockaddr_in6 *)res2->ai_addr)->sin6_addr,
sizeof(mreq6.ipv6mr_multiaddr));
if (setsockopt(insock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
&mreq6, sizeof(mreq6))) {
dprintf(LOG_ERR, FNAME,
"setsockopt(insock, IPV6_JOIN_GROUP): %s",
strerror(errno));
exit(1);
}
freeaddrinfo(res2);
hints.ai_flags = 0;
error = getaddrinfo(DH6ADDR_ALLSERVER, DH6PORT_UPSTREAM,
&hints, &res2);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
memset(&mreq6, 0, sizeof(mreq6));
mreq6.ipv6mr_interface = ifidx;
memcpy(&mreq6.ipv6mr_multiaddr,
&((struct sockaddr_in6 *)res2->ai_addr)->sin6_addr,
sizeof(mreq6.ipv6mr_multiaddr));
if (setsockopt(insock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
&mreq6, sizeof(mreq6))) {
dprintf(LOG_ERR, FNAME,
"setsockopt(insock, IPV6_JOIN_GROUP): %s",
strerror(errno));
exit(1);
}
freeaddrinfo(res2);
hints.ai_flags = 0;
error = getaddrinfo(NULL, DH6PORT_DOWNSTREAM, &hints, &res);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
outsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (outsock < 0) {
dprintf(LOG_ERR, FNAME, "socket(outsock): %s",
strerror(errno));
exit(1);
}
/* set outgoing interface of multicast packets for DHCP reconfig */
if (setsockopt(outsock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
&ifidx, sizeof(ifidx)) < 0) {
dprintf(LOG_ERR, FNAME,
"setsockopt(outsock, IPV6_MULTICAST_IF): %s",
strerror(errno));
exit(1);
}
#if !defined(__linux__) && !defined(__sun__)
/* make the socket write-only */
if (shutdown(outsock, 0)) {
dprintf(LOG_ERR, FNAME, "shutdown(outbound, 0): %s",
strerror(errno));
exit(1);
}
#endif
freeaddrinfo(res);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
error = getaddrinfo("::", DH6PORT_DOWNSTREAM, &hints, &res);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
memcpy(&sa6_any_downstream_storage, res->ai_addr, res->ai_addrlen);
sa6_any_downstream =
(const struct sockaddr_in6*)&sa6_any_downstream_storage;
freeaddrinfo(res);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
error = getaddrinfo("::", DH6PORT_UPSTREAM, &hints, &res);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
memcpy(&sa6_any_relay_storage, res->ai_addr, res->ai_addrlen);
sa6_any_relay =
(const struct sockaddr_in6*)&sa6_any_relay_storage;
freeaddrinfo(res);
/* set up control socket */
if (ctlkey == NULL)
dprintf(LOG_NOTICE, FNAME, "skip opening control port");
else if (dhcp6_ctl_init(ctladdr, ctlport,
DHCP6CTL_DEF_COMMANDQUEUELEN, &ctlsock)) {
dprintf(LOG_ERR, FNAME,
"failed to initialize control channel");
exit(1);
}
if (signal(SIGTERM, server6_signal) == SIG_ERR) {
dprintf(LOG_WARNING, FNAME, "failed to set signal: %s",
strerror(errno));
exit(1);
}
return;
}
static void
process_signals()
{
if ((sig_flags & SIGF_TERM)) {
unlink(pid_file);
exit(0);
}
}
static void
server6_mainloop()
{
struct timeval *w;
int ret;
fd_set r;
int maxsock;
while (1) {
if (sig_flags)
process_signals();
w = dhcp6_check_timer();
FD_ZERO(&r);
FD_SET(insock, &r);
maxsock = insock;
if (ctlsock >= 0) {
FD_SET(ctlsock, &r);
maxsock = (insock > ctlsock) ? insock : ctlsock;
(void)dhcp6_ctl_setreadfds(&r, &maxsock);
}
ret = select(maxsock + 1, &r, NULL, NULL, w);
switch (ret) {
case -1:
if (errno != EINTR) {
dprintf(LOG_ERR, FNAME, "select: %s",
strerror(errno));
exit(1);
}
continue;
case 0: /* timeout */
break;
default:
break;
}
if (FD_ISSET(insock, &r))
server6_recv(insock);
if (ctlsock >= 0) {
if (FD_ISSET(ctlsock, &r)) {
(void)dhcp6_ctl_acceptcommand(ctlsock,
server6_do_ctlcommand);
}
(void)dhcp6_ctl_readcommand(&r);
}
}
}
static inline int
get_val32(bpp, lenp, valp)
char **bpp;
int *lenp;
u_int32_t *valp;
{
char *bp = *bpp;
int len = *lenp;
u_int32_t i32;
if (len < sizeof(*valp))
return (-1);
memcpy(&i32, bp, sizeof(i32));
*valp = ntohl(i32);
*bpp = bp + sizeof(*valp);
*lenp = len - sizeof(*valp);
return (0);
}
static inline int
get_val(bpp, lenp, valp, vallen)
char **bpp;
int *lenp;
void *valp;
size_t vallen;
{
char *bp = *bpp;
int len = *lenp;
if (len < vallen)
return (-1);
memcpy(valp, bp, vallen);
*bpp = bp + vallen;
*lenp = len - vallen;
return (0);
}
static int
server6_do_ctlcommand(buf, len)
char *buf;
ssize_t len;
{
struct dhcp6ctl *ctlhead;
struct dhcp6ctl_iaspec iaspec;
u_int16_t command, version;
u_int32_t p32, iaid, duidlen, ts, ts0;
struct duid duid;
struct dhcp6_binding *binding;
int commandlen;
char *bp;
time_t now;
ctlhead = (struct dhcp6ctl *)buf;
command = ntohs(ctlhead->command);
commandlen = (int)(ntohs(ctlhead->len));
version = ntohs(ctlhead->version);
if (len != sizeof(struct dhcp6ctl) + commandlen) {
dprintf(LOG_ERR, FNAME,
"assumption failure: command length mismatch");
return (DHCP6CTL_R_FAILURE);
}
/* replay protection and message authentication */
if ((now = time(NULL)) < 0) {
dprintf(LOG_ERR, FNAME, "failed to get current time: %s",
strerror(errno));
return (DHCP6CTL_R_FAILURE);
}
ts0 = (u_int32_t)now;
ts = ntohl(ctlhead->timestamp);
if (ts + CTLSKEW < ts0 || (ts - CTLSKEW) > ts0) {
dprintf(LOG_INFO, FNAME, "timestamp is out of range");
return (DHCP6CTL_R_FAILURE);
}
if (ctlkey == NULL) { /* should not happen!! */
dprintf(LOG_ERR, FNAME, "no secret key for control channel");
return (DHCP6CTL_R_FAILURE);
}
if (dhcp6_verify_mac(buf, len, DHCP6CTL_AUTHPROTO_UNDEF,
DHCP6CTL_AUTHALG_HMACMD5, sizeof(*ctlhead), ctlkey) != 0) {
dprintf(LOG_INFO, FNAME, "authentication failure");
return (DHCP6CTL_R_FAILURE);
}
bp = buf + sizeof(*ctlhead) + ctldigestlen;
commandlen -= ctldigestlen;
if (version > DHCP6CTL_VERSION) {
dprintf(LOG_INFO, FNAME, "unsupported version: %d", version);
return (DHCP6CTL_R_FAILURE);
}
switch (command) {
case DHCP6CTL_COMMAND_RELOAD:
if (commandlen != 0) {
dprintf(LOG_INFO, FNAME, "invalid command length "
"for reload: %d", commandlen);
return (DHCP6CTL_R_DONE);
}
server6_reload();
break;
case DHCP6CTL_COMMAND_STOP:
if (commandlen != 0) {
dprintf(LOG_INFO, FNAME, "invalid command length "
"for stop: %d", commandlen);
return (DHCP6CTL_R_DONE);
}
server6_stop();
break;
case DHCP6CTL_COMMAND_REMOVE:
if (get_val32(&bp, &commandlen, &p32))
return (DHCP6CTL_R_FAILURE);
if (p32 != DHCP6CTL_BINDING) {
dprintf(LOG_INFO, FNAME,
"unknown remove target: %ul", p32);
return (DHCP6CTL_R_FAILURE);
}
if (get_val32(&bp, &commandlen, &p32))
return (DHCP6CTL_R_FAILURE);
if (p32 != DHCP6CTL_BINDING_IA) {
dprintf(LOG_INFO, FNAME, "unknown binding type: %ul",
p32);
return (DHCP6CTL_R_FAILURE);
}
if (get_val(&bp, &commandlen, &iaspec, sizeof(iaspec)))
return (DHCP6CTL_R_FAILURE);
if (ntohl(iaspec.type) != DHCP6CTL_IA_PD &&
ntohl(iaspec.type) != DHCP6CTL_IA_NA) {
dprintf(LOG_INFO, FNAME, "unknown IA type: %ul",
ntohl(iaspec.type));
return (DHCP6CTL_R_FAILURE);
}
iaid = ntohl(iaspec.id);
duidlen = ntohl(iaspec.duidlen);
if (duidlen > commandlen) {
dprintf(LOG_INFO, FNAME, "DUID length mismatch");
return (DHCP6CTL_R_FAILURE);
}
duid.duid_len = (size_t)duidlen;
duid.duid_id = bp;
binding = find_binding(&duid, DHCP6_BINDING_IA,
DHCP6_LISTVAL_IAPD, iaid);
if (binding == NULL) {
binding = find_binding(&duid, DHCP6_BINDING_IA,
DHCP6_LISTVAL_IANA, iaid);
if (binding == NULL) {
dprintf(LOG_INFO, FNAME, "no such binding");
return (DHCP6CTL_R_FAILURE);
}
}
remove_binding(binding);
break;
default:
dprintf(LOG_INFO, FNAME,
"unknown control command: %d (len=%d)",
(int)command, commandlen);
return (DHCP6CTL_R_FAILURE);
}
return (DHCP6CTL_R_DONE);
}
static void
server6_reload()
{
/* reload the configuration file */
if (cfparse(conffile) != 0) {
dprintf(LOG_WARNING, FNAME,
"failed to reload configuration file");
return;
}
dprintf(LOG_NOTICE, FNAME, "server reloaded");
return;
}
static void
server6_stop()
{
/* Right now, we simply stop running */
dprintf(LOG_NOTICE, FNAME, "exiting");
exit (0);
}
static void
server6_recv(s)
int s;
{
ssize_t len;
struct sockaddr_storage from;
int fromlen;
struct msghdr mhdr;
struct iovec iov;
char cmsgbuf[BUFSIZ];
struct cmsghdr *cm;
struct in6_pktinfo *pi = NULL;
struct dhcp6_if *ifp;
struct dhcp6 *dh6;
struct dhcp6_optinfo optinfo;
struct dhcp6opt *optend;
struct relayinfolist relayinfohead;
struct relayinfo *relayinfo;
TAILQ_INIT(&relayinfohead);
memset(&iov, 0, sizeof(iov));
memset(&mhdr, 0, sizeof(mhdr));
iov.iov_base = rdatabuf;
iov.iov_len = sizeof(rdatabuf);
mhdr.msg_name = &from;
mhdr.msg_namelen = sizeof(from);
mhdr.msg_iov = &iov;
mhdr.msg_iovlen = 1;
mhdr.msg_control = (caddr_t)cmsgbuf;
mhdr.msg_controllen = sizeof(cmsgbuf);
if ((len = recvmsg(insock, &mhdr, 0)) < 0) {
dprintf(LOG_ERR, FNAME, "recvmsg: %s", strerror(errno));
return;
}
fromlen = mhdr.msg_namelen;
for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&mhdr); cm;
cm = (struct cmsghdr *)CMSG_NXTHDR(&mhdr, cm)) {
if (cm->cmsg_level == IPPROTO_IPV6 &&
cm->cmsg_type == IPV6_PKTINFO &&
cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
}
}
if (pi == NULL) {
dprintf(LOG_NOTICE, FNAME, "failed to get packet info");
return;
}
/*
* DHCPv6 server may receive a DHCPv6 packet from a non-listening
* interface, when a DHCPv6 relay agent is running on that interface.
* This check prevents such reception.
*/
if (pi->ipi6_ifindex != ifidx)
return;
if ((ifp = find_ifconfbyid((unsigned int)pi->ipi6_ifindex)) == NULL) {
dprintf(LOG_INFO, FNAME, "unexpected interface (%d)",
(unsigned int)pi->ipi6_ifindex);
return;
}
dh6 = (struct dhcp6 *)rdatabuf;
if (len < sizeof(*dh6)) {
dprintf(LOG_INFO, FNAME, "short packet (%d bytes)", len);
return;
}
dprintf(LOG_DEBUG, FNAME, "received %s from %s",
dhcp6msgstr(dh6->dh6_msgtype),
addr2str((struct sockaddr *)&from));
/*
* A server MUST discard any Solicit, Confirm, Rebind or
* Information-request messages it receives with a unicast
* destination address.
* [RFC3315 Section 15.]
*/
if (!IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) &&
(dh6->dh6_msgtype == DH6_SOLICIT ||
dh6->dh6_msgtype == DH6_CONFIRM ||
dh6->dh6_msgtype == DH6_REBIND ||
dh6->dh6_msgtype == DH6_INFORM_REQ)) {
dprintf(LOG_INFO, FNAME, "invalid unicast message");
return;
}
/*
* A server never receives a relay reply message. Since relay
* replay messages will annoy option parser below, we explicitly
* reject them here.
*/
if (dh6->dh6_msgtype == DH6_RELAY_REPLY) {
dprintf(LOG_INFO, FNAME, "relay reply message from %s",
addr2str((struct sockaddr *)&from));
return;
}
optend = (struct dhcp6opt *)(rdatabuf + len);
if (dh6->dh6_msgtype == DH6_RELAY_FORW) {
if (process_relayforw(&dh6, &optend, &relayinfohead,
(struct sockaddr *)&from)) {
goto end;
}
/* dh6 and optend should have been updated. */
len = (ssize_t)((char *)optend - (char *)dh6);
}
/*
* parse and validate options in the message
*/
dhcp6_init_options(&optinfo);
if (dhcp6_get_options((struct dhcp6opt *)(dh6 + 1),
optend, &optinfo) < 0) {
dprintf(LOG_INFO, FNAME, "failed to parse options");
goto end;
}
switch (dh6->dh6_msgtype) {
case DH6_SOLICIT:
(void)react_solicit(ifp, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_REQUEST:
(void)react_request(ifp, pi, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_RENEW:
(void)react_renew(ifp, pi, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_REBIND:
(void)react_rebind(ifp, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_RELEASE:
(void)react_release(ifp, pi, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_DECLINE:
(void)react_decline(ifp, pi, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_CONFIRM:
(void)react_confirm(ifp, pi, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_INFORM_REQ:
(void)react_informreq(ifp, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
default:
dprintf(LOG_INFO, FNAME, "unknown or unsupported msgtype (%s)",
dhcp6msgstr(dh6->dh6_msgtype));
break;
}
dhcp6_clear_options(&optinfo);
end:
while ((relayinfo = TAILQ_FIRST(&relayinfohead)) != NULL) {
TAILQ_REMOVE(&relayinfohead, relayinfo, link);
free_relayinfo(relayinfo);
}
return;
}
static void
free_relayinfo(relayinfo)
struct relayinfo *relayinfo;
{
if (relayinfo->relay_ifid.dv_buf)
dhcp6_vbuf_free(&relayinfo->relay_ifid);
if (relayinfo->relay_msg.dv_buf)
dhcp6_vbuf_free(&relayinfo->relay_msg);
free(relayinfo);
}
static int
process_relayforw(dh6p, optendp, relayinfohead, from)
struct dhcp6 **dh6p;
struct dhcp6opt **optendp;
struct relayinfolist *relayinfohead;
struct sockaddr *from;
{
struct dhcp6_relay *dh6relay = (struct dhcp6_relay *)*dh6p;
struct dhcp6opt *optend = *optendp;
struct relayinfo *relayinfo;
struct dhcp6_optinfo optinfo;
int len;
again:
len = (void *)optend - (void *)dh6relay;
if (len < sizeof (*dh6relay)) {
dprintf(LOG_INFO, FNAME, "short relay message from %s",
addr2str(from));
return (-1);
}
dprintf(LOG_DEBUG, FNAME,
"dhcp6 relay: hop=%d, linkaddr=%s, peeraddr=%s",
dh6relay->dh6relay_hcnt,
in6addr2str(&dh6relay->dh6relay_linkaddr, 0),
in6addr2str(&dh6relay->dh6relay_peeraddr, 0));
/*
* parse and validate options in the relay forward message.
*/
dhcp6_init_options(&optinfo);
if (dhcp6_get_options((struct dhcp6opt *)(dh6relay + 1),
optend, &optinfo) < 0) {
dprintf(LOG_INFO, FNAME, "failed to parse options");
return (-1);
}
/* A relay forward message must include a relay message option */
if (optinfo.relaymsg_msg == NULL) {
dprintf(LOG_INFO, FNAME, "relay forward from %s "
"without a relay message", addr2str(from));
return (-1);
}
/* relay message must contain a DHCPv6 message. */
len = optinfo.relaymsg_len;
if (len < sizeof (struct dhcp6)) {
dprintf(LOG_INFO, FNAME,
"short packet (%d bytes) in relay message", len);
return (-1);
}
if ((relayinfo = malloc(sizeof (*relayinfo))) == NULL) {
dprintf(LOG_ERR, FNAME, "failed to allocate relay info");
return (-1);
}
memset(relayinfo, 0, sizeof (*relayinfo));
relayinfo->hcnt = dh6relay->dh6relay_hcnt;
memcpy(&relayinfo->linkaddr, &dh6relay->dh6relay_linkaddr,
sizeof (relayinfo->linkaddr));
memcpy(&relayinfo->peeraddr, &dh6relay->dh6relay_peeraddr,
sizeof (relayinfo->peeraddr));
if (dhcp6_vbuf_copy(&relayinfo->relay_msg, &optinfo.relay_msg))
goto fail;
if (optinfo.ifidopt_id &&
dhcp6_vbuf_copy(&relayinfo->relay_ifid, &optinfo.ifidopt)) {
goto fail;
}
TAILQ_INSERT_HEAD(relayinfohead, relayinfo, link);
dhcp6_clear_options(&optinfo);
optend = (struct dhcp6opt *)(relayinfo->relay_msg.dv_buf + len);
dh6relay = (struct dhcp6_relay *)relayinfo->relay_msg.dv_buf;
if (dh6relay->dh6relay_msgtype != DH6_RELAY_FORW) {
*dh6p = (struct dhcp6 *)dh6relay;
*optendp = optend;
return (0);
}
goto again;
fail:
free_relayinfo(relayinfo);
dhcp6_clear_options(&optinfo);
return (-1);
}
/*
* Set stateless configuration information to a option structure.
* It is the caller's responsibility to deal with error cases.
*/
static int
set_statelessinfo(type, optinfo)
int type;
struct dhcp6_optinfo *optinfo;
{
/* SIP domain name */
if (dhcp6_copy_list(&optinfo->sipname_list, &sipnamelist)) {
dprintf(LOG_ERR, FNAME,
"failed to copy SIP domain list");
return (-1);
}
/* SIP server */
if (dhcp6_copy_list(&optinfo->sip_list, &siplist)) {
dprintf(LOG_ERR, FNAME, "failed to copy SIP servers");
return (-1);
}
/* DNS server */
if (dhcp6_copy_list(&optinfo->dns_list, &dnslist)) {
dprintf(LOG_ERR, FNAME, "failed to copy DNS servers");
return (-1);
}
/* DNS search list */
if (dhcp6_copy_list(&optinfo->dnsname_list, &dnsnamelist)) {
dprintf(LOG_ERR, FNAME, "failed to copy DNS search list");
return (-1);
}
/* NTP server */
if (dhcp6_copy_list(&optinfo->ntp_list, &ntplist)) {
dprintf(LOG_ERR, FNAME, "failed to copy NTP servers");
return (-1);
}
/* NIS domain name */
if (dhcp6_copy_list(&optinfo->nisname_list, &nisnamelist)) {
dprintf(LOG_ERR, FNAME,
"failed to copy NIS domain list");
return (-1);
}
/* NIS server */
if (dhcp6_copy_list(&optinfo->nis_list, &nislist)) {
dprintf(LOG_ERR, FNAME, "failed to copy NIS servers");
return (-1);
}
/* NIS+ domain name */
if (dhcp6_copy_list(&optinfo->nispname_list, &nispnamelist)) {
dprintf(LOG_ERR, FNAME,
"failed to copy NIS+ domain list");
return (-1);
}
/* NIS+ server */
if (dhcp6_copy_list(&optinfo->nisp_list, &nisplist)) {
dprintf(LOG_ERR, FNAME, "failed to copy NIS+ servers");
return (-1);
}
/* BCMCS domain name */
if (dhcp6_copy_list(&optinfo->bcmcsname_list, &bcmcsnamelist)) {
dprintf(LOG_ERR, FNAME,
"failed to copy BCMCS domain list");
return (-1);
}
/* BCMCS server */
if (dhcp6_copy_list(&optinfo->bcmcs_list, &bcmcslist)) {
dprintf(LOG_ERR, FNAME, "failed to copy BCMCS servers");
return (-1);
}
/*
* Information refresh time. Only include in a response to
* an Information-request message.
*/
if (type == DH6_INFORM_REQ &&
optrefreshtime != DH6OPT_REFRESHTIME_UNDEF) {
optinfo->refreshtime = (int64_t)optrefreshtime;
}
return (0);
}
static int
react_solicit(ifp, dh6, len, optinfo, from, fromlen, relayinfohead)
struct dhcp6_if *ifp;
struct dhcp6 *dh6;
ssize_t len;
struct dhcp6_optinfo *optinfo;
struct sockaddr *from;
int fromlen;
struct relayinfolist *relayinfohead;
{
struct dhcp6_optinfo roptinfo;
struct host_conf *client_conf;
int resptype, do_binding = 0, error;
/*
* Servers MUST discard any Solicit messages that do not include a
* Client Identifier option.
* [RFC3315 Section 15.2]
*/
if (optinfo->clientID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no client ID option");
return (-1);
} else {
dprintf(LOG_DEBUG, FNAME, "client ID %s",
duidstr(&optinfo->clientID));
}
/*
* Servers MUST discard any Solicit messages that do include a
* Server Identifier option.
* [RFC3315 Section 15.2]
*/
if (optinfo->serverID.duid_len) {
dprintf(LOG_INFO, FNAME, "server ID option found");
return (-1);
}
/* get per-host configuration for the client, if any. */
if ((client_conf = find_hostconf(&optinfo->clientID))) {
dprintf(LOG_DEBUG, FNAME, "found a host configuration for %s",
client_conf->name);
}
/*
* configure necessary options based on the options in solicit.
*/
dhcp6_init_options(&roptinfo);
/* process authentication */
if (process_auth(dh6, len, client_conf, optinfo, &roptinfo)) {
dprintf(LOG_INFO, FNAME, "failed to process authentication "
"information for %s",
clientstr(client_conf, &optinfo->clientID));
goto fail;
}
/* server identifier option */
if (duidcpy(&roptinfo.serverID, &server_duid)) {
dprintf(LOG_ERR, FNAME, "failed to copy server ID");
goto fail;
}
/* copy client information back */
if (duidcpy(&roptinfo.clientID, &optinfo->clientID)) {
dprintf(LOG_ERR, FNAME, "failed to copy client ID");
goto fail;
}
/* preference (if configured) */
if (ifp->server_pref != DH6OPT_PREF_UNDEF)
roptinfo.pref = ifp->server_pref;
/* add other configuration information */
if (set_statelessinfo(DH6_SOLICIT, &roptinfo)) {
dprintf(LOG_ERR, FNAME,
"failed to set other stateless information");
goto fail;
}
/*
* see if we have information for requested options, and if so,
* configure corresponding options.
*/
if (optinfo->rapidcommit && (ifp->allow_flags & DHCIFF_RAPID_COMMIT))
do_binding = 1;
/*
* The delegating router MUST include an IA_PD option, identifying any
* prefix(es) that the delegating router will delegate to the
* requesting router. [RFC3633 Section 11.2]
*/
if (!TAILQ_EMPTY(&optinfo->iapd_list)) {
int found = 0;
struct dhcp6_list conflist;
struct dhcp6_listval *iapd;
TAILQ_INIT(&conflist);
/* make a local copy of the configured prefixes */
if (client_conf &&
dhcp6_copy_list(&conflist, &client_conf->prefix_list)) {
dprintf(LOG_NOTICE, FNAME,
"failed to make local data");
goto fail;
}
for (iapd = TAILQ_FIRST(&optinfo->iapd_list); iapd;
iapd = TAILQ_NEXT(iapd, link)) {
/*
* find an appropriate prefix for each IA_PD,
* removing the adopted prefixes from the list.
* (dhcp6s cannot create IAs without client config)
*/
if (client_conf &&
make_ia(iapd, &conflist, &roptinfo.iapd_list,
client_conf, do_binding) > 0)
found = 1;
}
dhcp6_clear_list(&conflist);
if (!found) {
/*
* If the delegating router will not assign any
* prefixes to any IA_PDs in a subsequent Request from
* the requesting router, the delegating router MUST
* send an Advertise message to the requesting router
* that includes a Status Code option with code
* NoPrefixAvail.
* [dhcpv6-opt-prefix-delegation-01 Section 10.2]
*/
u_int16_t stcode = DH6OPT_STCODE_NOPREFIXAVAIL;
if (dhcp6_add_listval(&roptinfo.stcode_list,
DHCP6_LISTVAL_STCODE, &stcode, NULL) == NULL)
goto fail;
}
}
if (!TAILQ_EMPTY(&optinfo->iana_list)) {
int found = 0;
struct dhcp6_list conflist;
struct dhcp6_listval *iana;
if (client_conf == NULL && ifp->pool.name) {
if ((client_conf = create_dynamic_hostconf(&optinfo->clientID,
&ifp->pool)) == NULL)
dprintf(LOG_NOTICE, FNAME,
"failed to make host configuration");
}
TAILQ_INIT(&conflist);
/* make a local copy of the configured addresses */
if (client_conf &&
dhcp6_copy_list(&conflist, &client_conf->addr_list)) {
dprintf(LOG_NOTICE, FNAME,
"failed to make local data");
goto fail;
}
for (iana = TAILQ_FIRST(&optinfo->iana_list); iana;
iana = TAILQ_NEXT(iana, link)) {
/*
* find an appropriate address for each IA_NA,
* removing the adopted addresses from the list.
* (dhcp6s cannot create IAs without client config)
*/
if (client_conf &&
make_ia(iana, &conflist, &roptinfo.iana_list,
client_conf, do_binding) > 0)
found = 1;
}
dhcp6_clear_list(&conflist);
if (!found) {
u_int16_t stcode = DH6OPT_STCODE_NOADDRSAVAIL;
if (dhcp6_add_listval(&roptinfo.stcode_list,
DHCP6_LISTVAL_STCODE, &stcode, NULL) == NULL)
goto fail;
}
}
if (optinfo->rapidcommit && (ifp->allow_flags & DHCIFF_RAPID_COMMIT)) {
/*
* If the client has included a Rapid Commit option and the
* server has been configured to respond with committed address
* assignments and other resources, responds to the Solicit
* with a Reply message.
* [RFC3315 Section 17.2.1]
*/
roptinfo.rapidcommit = 1;
resptype = DH6_REPLY;
} else
resptype = DH6_ADVERTISE;
error = server6_send(resptype, ifp, dh6, optinfo, from, fromlen,
&roptinfo, relayinfohead, client_conf);
dhcp6_clear_options(&roptinfo);
return (error);
fail:
dhcp6_clear_options(&roptinfo);
return (-1);
}
static int
react_request(ifp, pi, dh6, len, optinfo, from, fromlen, relayinfohead)
struct dhcp6_if *ifp;
struct in6_pktinfo *pi;
struct dhcp6 *dh6;
ssize_t len;
struct dhcp6_optinfo *optinfo;
struct sockaddr *from;
int fromlen;
struct relayinfolist *relayinfohead;
{
struct dhcp6_optinfo roptinfo;
struct host_conf *client_conf;
/* message validation according to Section 15.4 of RFC3315 */
/* the message must include a Server Identifier option */
if (optinfo->serverID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no server ID option");
return (-1);
}
/* the contents of the Server Identifier option must match ours */
if (duidcmp(&optinfo->serverID, &server_duid)) {
dprintf(LOG_INFO, FNAME, "server ID mismatch");
return (-1);
}
/* the message must include a Client Identifier option */
if (optinfo->clientID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no client ID option");
return (-1);
}
/*
* configure necessary options based on the options in request.
*/
dhcp6_init_options(&roptinfo);
/* server identifier option */
if (duidcpy(&roptinfo.serverID, &server_duid)) {
dprintf(LOG_ERR, FNAME, "failed to copy server ID");
goto fail;
}
/* copy client information back */
if (duidcpy(&roptinfo.clientID, &optinfo->clientID)) {
dprintf(LOG_ERR, FNAME, "failed to copy client ID");
goto fail;
}
/* get per-host configuration for the client, if any. */
if ((client_conf = find_hostconf(&optinfo->clientID))) {
dprintf(LOG_DEBUG, FNAME,
"found a host configuration named %s", client_conf->name);
}
/* process authentication */
if (process_auth(dh6, len, client_conf, optinfo, &roptinfo)) {
dprintf(LOG_INFO, FNAME, "failed to process authentication "
"information for %s",
clientstr(client_conf, &optinfo->clientID));
goto fail;
}
/*
* When the server receives a Request message via unicast from a
* client to which the server has not sent a unicast option, the server
* discards the Request message and responds with a Reply message
* containing a Status Code option with value UseMulticast, a Server
* Identifier option containing the server's DUID, the Client
* Identifier option from the client message and no other options.
* [RFC3315 18.2.1]
* (Our current implementation never sends a unicast option.)
* Note: a request message encapsulated in a relay server option can be
* unicasted.
*/
if (!IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) &&
TAILQ_EMPTY(relayinfohead)) {
u_int16_t stcode = DH6OPT_STCODE_USEMULTICAST;
dprintf(LOG_INFO, FNAME, "unexpected unicast message from %s",
addr2str(from));
if (dhcp6_add_listval(&roptinfo.stcode_list,
DHCP6_LISTVAL_STCODE, &stcode, NULL) == NULL) {
dprintf(LOG_ERR, FNAME, "failed to add a status code");
goto fail;
}
server6_send(DH6_REPLY, ifp, dh6, optinfo, from,
fromlen, &roptinfo, relayinfohead, client_conf);
goto end;
}
/*
* See if we have to make a binding of some configuration information
* for the client.
*/
/*
* When a delegating router receives a Request message from a
* requesting router that contains an IA_PD option, and the delegating
* router is authorized to delegate prefix(es) to the requesting
* router, the delegating router selects the prefix(es) to be delegated
* to the requesting router.
* [RFC3633 Section 12.2]
*/
if (!TAILQ_EMPTY(&optinfo->iapd_list)) {
struct dhcp6_list conflist;
struct dhcp6_listval *iapd;
TAILQ_INIT(&conflist);
/* make a local copy of the configured prefixes */
if (client_conf &&
dhcp6_copy_list(&conflist, &client_conf->prefix_list)) {
dprintf(LOG_NOTICE, FNAME,
"failed to make local data");
goto fail;
}
for (iapd = TAILQ_FIRST(&optinfo->iapd_list); iapd;
iapd = TAILQ_NEXT(iapd, link)) {
/*
* Find an appropriate prefix for each IA_PD,
* removing the adopted prefixes from the list.
* The prefixes will be bound to the client.
*/
if (make_ia(iapd, &conflist, &roptinfo.iapd_list,
client_conf, 1) == 0) {
/*
* We could not find any prefixes for the IA.
* RFC3315 specifies to include NoAddrsAvail
* for the IA in the address configuration
* case (Section 18.2.1). We follow the same
* logic for prefix delegation as well.
*/
if (make_ia_stcode(DHCP6_LISTVAL_IAPD,
iapd->val_ia.iaid,
DH6OPT_STCODE_NOPREFIXAVAIL,
&roptinfo.iapd_list)) {
dprintf(LOG_NOTICE, FNAME,
"failed to make an option list");
dhcp6_clear_list(&conflist);
goto fail;
}
}
}
dhcp6_clear_list(&conflist);
}
if (!TAILQ_EMPTY(&optinfo->iana_list)) {
struct dhcp6_list conflist;
struct dhcp6_listval *iana;
if (client_conf == NULL && ifp->pool.name) {
if ((client_conf = create_dynamic_hostconf(&optinfo->clientID,
&ifp->pool)) == NULL)
dprintf(LOG_NOTICE, FNAME,
"failed to make host configuration");
}
TAILQ_INIT(&conflist);
/* make a local copy of the configured prefixes */
if (client_conf &&
dhcp6_copy_list(&conflist, &client_conf->addr_list)) {
dprintf(LOG_NOTICE, FNAME,
"failed to make local data");
goto fail;
}
for (iana = TAILQ_FIRST(&optinfo->iana_list); iana;
iana = TAILQ_NEXT(iana, link)) {
/*
* Find an appropriate address for each IA_NA,
* removing the adopted addresses from the list.
* The addresses will be bound to the client.
*/
if (make_ia(iana, &conflist, &roptinfo.iana_list,
client_conf, 1) == 0) {
if (make_ia_stcode(DHCP6_LISTVAL_IANA,
iana->val_ia.iaid,
DH6OPT_STCODE_NOADDRSAVAIL,
&roptinfo.iana_list)) {
dprintf(LOG_NOTICE, FNAME,
"failed to make an option list");
dhcp6_clear_list(&conflist);
goto fail;
}
}
}
dhcp6_clear_list(&conflist);
}
/*
* If the Request message contained an Option Request option, the
* server MUST include options in the Reply message for any options in
* the Option Request option the server is configured to return to the
* client.
* [RFC3315 18.2.1]
* Note: our current implementation always includes all information
* that we can provide. So we do not have to check the option request
* options.
*/
#if 0
for (opt = TAILQ_FIRST(&optinfo->reqopt_list); opt;
opt = TAILQ_NEXT(opt, link)) {
;
}
#endif
/*
* Add options to the Reply message for any other configuration
* information to be assigned to the client.
*/
if (set_statelessinfo(DH6_REQUEST, &roptinfo)) {
dprintf(LOG_ERR, FNAME,
"failed to set other stateless information");
goto fail;
}
/* send a reply message. */
(void)server6_send(DH6_REPLY, ifp, dh6, optinfo, from, fromlen,
&roptinfo, relayinfohead, client_conf);
end:
dhcp6_clear_options(&roptinfo);
return (0);
fail:
dhcp6_clear_options(&roptinfo);
return (-1);
}
static int
react_renew(ifp, pi, dh6, len, optinfo, from, fromlen, relayinfohead)
struct dhcp6_if *ifp;
struct in6_pktinfo *pi;
struct dhcp6 *dh6;
ssize_t len;
struct dhcp6_optinfo *optinfo;
struct sockaddr *from;
int fromlen;
struct relayinfolist *relayinfohead;
{
struct dhcp6_optinfo roptinfo;
struct dhcp6_listval *ia;
struct host_conf *client_conf;
/* message validation according to Section 15.6 of RFC3315 */
/* the message must include a Server Identifier option */
if (optinfo->serverID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no server ID option");
return (-1);
}
/* the contents of the Server Identifier option must match ours */
if (duidcmp(&optinfo->serverID, &server_duid)) {
dprintf(LOG_INFO, FNAME, "server ID mismatch");
return (-1);
}
/* the message must include a Client Identifier option */
if (optinfo->clientID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no client ID option");
return (-1);
}
/*
* configure necessary options based on the options in request.
*/
dhcp6_init_options(&roptinfo);
/* server identifier option */
if (duidcpy(&roptinfo.serverID, &server_duid)) {
dprintf(LOG_ERR, FNAME, "failed to copy server ID");
goto fail;
}
/* copy client information back */
if (duidcpy(&roptinfo.clientID, &optinfo->clientID)) {
dprintf(LOG_ERR, FNAME, "failed to copy client ID");
goto fail;
}
/* get per-host configuration for the client, if any. */
if ((client_conf = find_hostconf(&optinfo->clientID))) {
dprintf(LOG_DEBUG, FNAME,
"found a host configuration named %s", client_conf->name);
}
/* process authentication */
if (process_auth(dh6, len, client_conf, optinfo, &roptinfo)) {
dprintf(LOG_INFO, FNAME, "failed to process authentication "
"information for %s",
clientstr(client_conf, &optinfo->clientID));
goto fail;
}
/*
* When the server receives a Renew message via unicast from a
* client to which the server has not sent a unicast option, the server
* discards the Request message and responds with a Reply message
* containing a status code option with value UseMulticast, a Server
* Identifier option containing the server's DUID, the Client
* Identifier option from the client message and no other options.
* [RFC3315 18.2.3]
* (Our current implementation never sends a unicast option.)
*/
if (!IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) &&
TAILQ_EMPTY(relayinfohead)) {
u_int16_t stcode = DH6OPT_STCODE_USEMULTICAST;
dprintf(LOG_INFO, FNAME, "unexpected unicast message from %s",
addr2str(from));
if (dhcp6_add_listval(&roptinfo.stcode_list,
DHCP6_LISTVAL_STCODE, &stcode, NULL) == NULL) {
dprintf(LOG_ERR, FNAME, "failed to add a status code");
goto fail;
}
server6_send(DH6_REPLY, ifp, dh6, optinfo, from,
fromlen, &roptinfo, relayinfohead, client_conf);
goto end;
}
/*
* Locates the client's binding and verifies that the information
* from the client matches the information stored for that client.
*/
for (ia = TAILQ_FIRST(&optinfo->iapd_list); ia;
ia = TAILQ_NEXT(ia, link)) {
if (update_ia(DH6_RENEW, ia, &roptinfo.iapd_list, optinfo))
goto fail;
}
for (ia = TAILQ_FIRST(&optinfo->iana_list); ia;
ia = TAILQ_NEXT(ia, link)) {
if (update_ia(DH6_RENEW, ia, &roptinfo.iana_list, optinfo))
goto fail;
}
/* add other configuration information */
if (set_statelessinfo(DH6_RENEW, &roptinfo)) {
dprintf(LOG_ERR, FNAME,
"failed to set other stateless information");
goto fail;
}
(void)server6_send(DH6_REPLY, ifp, dh6, optinfo, from, fromlen,
&roptinfo, relayinfohead, client_conf);
end:
dhcp6_clear_options(&roptinfo);
return (0);
fail:
dhcp6_clear_options(&roptinfo);
return (-1);
}
static int
react_rebind(ifp, dh6, len, optinfo, from, fromlen, relayinfohead)
struct dhcp6_if *ifp;
struct dhcp6 *dh6;
ssize_t len;
struct dhcp6_optinfo *optinfo;
struct sockaddr *from;
int fromlen;
struct relayinfolist *relayinfohead;
{
struct dhcp6_optinfo roptinfo;
struct dhcp6_listval *ia;
struct host_conf *client_conf;
/* message validation according to Section 15.7 of RFC3315 */
/* the message must include a Client Identifier option */
if (optinfo->clientID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no client ID option");
return (-1);
}
/* the message must not include a server Identifier option */
if (optinfo->serverID.duid_len) {
dprintf(LOG_INFO, FNAME, "server ID option is included in "
"a rebind message");
return (-1);
}
/*
* configure necessary options based on the options in request.
*/
dhcp6_init_options(&roptinfo);
/* server identifier option */
if (duidcpy(&roptinfo.serverID, &server_duid)) {
dprintf(LOG_ERR, FNAME, "failed to copy server ID");
goto fail;
}
/* copy client information back */
if (duidcpy(&roptinfo.clientID, &optinfo->clientID)) {
dprintf(LOG_ERR, FNAME, "failed to copy client ID");
goto fail;
}
/* get per-host configuration for the client, if any. */
if ((client_conf = find_hostconf(&optinfo->clientID))) {
dprintf(LOG_DEBUG, FNAME,
"found a host configuration named %s", client_conf->name);
}
/* process authentication */
if (process_auth(dh6, len, client_conf, optinfo, &roptinfo)) {
dprintf(LOG_INFO, FNAME, "failed to process authentication "
"information for %s",
clientstr(client_conf, &optinfo->clientID));
goto fail;
}
/*
* Locates the client's binding and verifies that the information
* from the client matches the information stored for that client.
*/
for (ia = TAILQ_FIRST(&optinfo->iapd_list); ia;
ia = TAILQ_NEXT(ia, link)) {
if (update_ia(DH6_REBIND, ia, &roptinfo.iapd_list, optinfo))
goto fail;
}
for (ia = TAILQ_FIRST(&optinfo->iana_list); ia;
ia = TAILQ_NEXT(ia, link)) {
if (update_ia(DH6_REBIND, ia, &roptinfo.iana_list, optinfo))
goto fail;
}
/*
* If the returned iana/pd_list is empty, we do not have an explicit
* knowledge about validity nor invalidity for any IA_NA/PD information
* in the Rebind message. In this case, we should rather ignore the
* message than to send a Reply with empty information back to the
* client, which may annoy the recipient. However, if we have at least
* one useful information, either positive or negative, based on some
* explicit knowledge, we should reply with the responsible part.
*/
if (TAILQ_EMPTY(&roptinfo.iapd_list) &&
TAILQ_EMPTY(&roptinfo.iana_list)) {
dprintf(LOG_INFO, FNAME, "no useful information for a rebind");
goto fail; /* discard the rebind */
}
/* add other configuration information */
if (set_statelessinfo(DH6_REBIND, &roptinfo)) {
dprintf(LOG_ERR, FNAME,
"failed to set other stateless information");
goto fail;
}
(void)server6_send(DH6_REPLY, ifp, dh6, optinfo, from, fromlen,
&roptinfo, relayinfohead, client_conf);
dhcp6_clear_options(&roptinfo);
return (0);
fail:
dhcp6_clear_options(&roptinfo);
return (-1);
}
static int
react_release(ifp, pi, dh6, len, optinfo, from, fromlen, relayinfohead)
struct dhcp6_if *ifp;
struct in6_pktinfo *pi;
struct dhcp6 *dh6;
ssize_t len;
struct dhcp6_optinfo *optinfo;
struct sockaddr *from;
int fromlen;
struct relayinfolist *relayinfohead;
{
struct dhcp6_optinfo roptinfo;
struct dhcp6_listval *ia;
struct host_conf *client_conf;
u_int16_t stcode;
/* message validation according to Section 15.9 of RFC3315 */
/* the message must include a Server Identifier option */
if (optinfo->serverID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no server ID option");
return (-1);
}
/* the contents of the Server Identifier option must match ours */
if (duidcmp(&optinfo->serverID, &server_duid)) {
dprintf(LOG_INFO, FNAME, "server ID mismatch");
return (-1);
}
/* the message must include a Client Identifier option */
if (optinfo->clientID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no client ID option");
return (-1);
}
/*
* configure necessary options based on the options in request.
*/
dhcp6_init_options(&roptinfo);
/* server identifier option */
if (duidcpy(&roptinfo.serverID, &server_duid)) {
dprintf(LOG_ERR, FNAME, "failed to copy server ID");
goto fail;
}
/* copy client information back */
if (duidcpy(&roptinfo.clientID, &optinfo->clientID)) {
dprintf(LOG_ERR, FNAME, "failed to copy client ID");
goto fail;
}
/* get per-host configuration for the client, if any. */
if ((client_conf = find_hostconf(&optinfo->clientID))) {
dprintf(LOG_DEBUG, FNAME,
"found a host configuration named %s", client_conf->name);
}
/* process authentication */
if (process_auth(dh6, len, client_conf, optinfo, &roptinfo)) {
dprintf(LOG_INFO, FNAME, "failed to process authentication "
"information for %s",
clientstr(client_conf, &optinfo->clientID));
goto fail;
}
/*
* When the server receives a Release message via unicast from a
* client to which the server has not sent a unicast option, the server
* discards the Release message and responds with a Reply message
* containing a Status Code option with value UseMulticast, a Server
* Identifier option containing the server's DUID, the Client
* Identifier option from the client message and no other options.
* [RFC3315 18.2.6]
* (Our current implementation never sends a unicast option.)
*/
if (!IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) &&
TAILQ_EMPTY(relayinfohead)) {
u_int16_t stcode = DH6OPT_STCODE_USEMULTICAST;
dprintf(LOG_INFO, FNAME, "unexpected unicast message from %s",
addr2str(from));
if (dhcp6_add_listval(&roptinfo.stcode_list,
DHCP6_LISTVAL_STCODE, &stcode, NULL) == NULL) {
dprintf(LOG_ERR, FNAME, "failed to add a status code");
goto fail;
}
server6_send(DH6_REPLY, ifp, dh6, optinfo, from,
fromlen, &roptinfo, relayinfohead, client_conf);
goto end;
}
/*
* Locates the client's binding and verifies that the information
* from the client matches the information stored for that client.
*/
for (ia = TAILQ_FIRST(&optinfo->iapd_list); ia;
ia = TAILQ_NEXT(ia, link)) {
if (release_binding_ia(ia, &roptinfo.iapd_list, optinfo))
goto fail;
}
for (ia = TAILQ_FIRST(&optinfo->iana_list); ia;
ia = TAILQ_NEXT(ia, link)) {
if (release_binding_ia(ia, &roptinfo.iana_list, optinfo))
goto fail;
}
/*
* After all the addresses have been processed, the server generates a
* Reply message and includes a Status Code option with value Success.
* [RFC3315 Section 18.2.6]
*/
stcode = DH6OPT_STCODE_SUCCESS;
if (dhcp6_add_listval(&roptinfo.stcode_list,
DHCP6_LISTVAL_STCODE, &stcode, NULL) == NULL) {
dprintf(LOG_NOTICE, FNAME, "failed to add a status code");
goto fail;
}
(void)server6_send(DH6_REPLY, ifp, dh6, optinfo, from, fromlen,
&roptinfo, relayinfohead, client_conf);
end:
dhcp6_clear_options(&roptinfo);
return (0);
fail:
dhcp6_clear_options(&roptinfo);
return (-1);
}
static int
react_decline(ifp, pi, dh6, len, optinfo, from, fromlen, relayinfohead)
struct dhcp6_if *ifp;
struct in6_pktinfo *pi;
struct dhcp6 *dh6;
ssize_t len;
struct dhcp6_optinfo *optinfo;
struct sockaddr *from;
int fromlen;
struct relayinfolist *relayinfohead;
{
struct dhcp6_optinfo roptinfo;
struct dhcp6_listval *ia;
struct host_conf *client_conf;
u_int16_t stcode;
/* message validation according to Section 15.8 of RFC3315 */
/* the message must include a Server Identifier option */
if (optinfo->serverID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no server ID option");
return (-1);
}
/* the contents of the Server Identifier option must match ours */
if (duidcmp(&optinfo->serverID, &server_duid)) {
dprintf(LOG_INFO, FNAME, "server ID mismatch");
return (-1);
}
/* the message must include a Client Identifier option */
if (optinfo->clientID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no client ID option");
return (-1);
}
/*
* configure necessary options based on the options in request.
*/
dhcp6_init_options(&roptinfo);
/* server identifier option */
if (duidcpy(&roptinfo.serverID, &server_duid)) {
dprintf(LOG_ERR, FNAME, "failed to copy server ID");
goto fail;
}
/* copy client information back */
if (duidcpy(&roptinfo.clientID, &optinfo->clientID)) {
dprintf(LOG_ERR, FNAME, "failed to copy client ID");
goto fail;
}
/* get per-host configuration for the client, if any. */
if ((client_conf = find_hostconf(&optinfo->clientID))) {
dprintf(LOG_DEBUG, FNAME,
"found a host configuration named %s", client_conf->name);
}
/* process authentication */
if (process_auth(dh6, len, client_conf, optinfo, &roptinfo)) {
dprintf(LOG_INFO, FNAME, "failed to process authentication "
"information for %s",
clientstr(client_conf, &optinfo->clientID));
goto fail;
}
/*
* When the server receives a Decline message via unicast from a
* client to which the server has not sent a unicast option, the server
* discards the Decline message and responds with a Reply message
* containing a Status Code option with value UseMulticast, a Server
* Identifier option containing the server's DUID, the Client
* Identifier option from the client message and no other options.
* [RFC3315 18.2.6]
* (Our current implementation never sends a unicast option.)
*/
if (!IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) &&
TAILQ_EMPTY(relayinfohead)) {
stcode = DH6OPT_STCODE_USEMULTICAST;
dprintf(LOG_INFO, FNAME, "unexpected unicast message from %s",
addr2str(from));
if (dhcp6_add_listval(&roptinfo.stcode_list,
DHCP6_LISTVAL_STCODE, &stcode, NULL) == NULL) {
dprintf(LOG_ERR, FNAME, "failed to add a status code");
goto fail;
}
server6_send(DH6_REPLY, ifp, dh6, optinfo, from,
fromlen, &roptinfo, relayinfohead, client_conf);
goto end;
}
/*
* Locates the client's binding on IA-NA and verifies that the
* information from the client matches the information stored
* for that client. (IA-PD is just ignored [RFC3633 12.1])
*/
for (ia = TAILQ_FIRST(&optinfo->iana_list); ia;
ia = TAILQ_NEXT(ia, link)) {
if (decline_binding_ia(ia, &roptinfo.iana_list, optinfo))
goto fail;
}
/*
* After all the addresses have been processed, the server generates a
* Reply message and includes a Status Code option with value Success.
* [RFC3315 Section 18.2.7]
*/
stcode = DH6OPT_STCODE_SUCCESS;
if (dhcp6_add_listval(&roptinfo.stcode_list,
DHCP6_LISTVAL_STCODE, &stcode, NULL) == NULL) {
dprintf(LOG_NOTICE, FNAME, "failed to add a status code");
goto fail;
}
(void)server6_send(DH6_REPLY, ifp, dh6, optinfo, from, fromlen,
&roptinfo, relayinfohead, client_conf);
end:
dhcp6_clear_options(&roptinfo);
return (0);
fail:
dhcp6_clear_options(&roptinfo);
return (-1);
}
static int
react_confirm(ifp, pi, dh6, len, optinfo, from, fromlen, relayinfohead)
struct dhcp6_if *ifp;
struct in6_pktinfo *pi;
struct dhcp6 *dh6;
ssize_t len;
struct dhcp6_optinfo *optinfo;
struct sockaddr *from;
int fromlen;
struct relayinfolist *relayinfohead;
{
struct dhcp6_optinfo roptinfo;
struct dhcp6_list conflist;
struct dhcp6_listval *iana, *iaaddr;
struct host_conf *client_conf;
u_int16_t stcode = DH6OPT_STCODE_SUCCESS;
int error;
/* message validation according to Section 15.5 of RFC3315 */
/* the message may not include a Server Identifier option */
if (optinfo->serverID.duid_len) {
dprintf(LOG_INFO, FNAME, "server ID option found");
return (-1);
}
/* the message must include a Client Identifier option */
if (optinfo->clientID.duid_len == 0) {
dprintf(LOG_INFO, FNAME, "no client ID option");
return (-1);
}
dhcp6_init_options(&roptinfo);
/* server identifier option */
if (duidcpy(&roptinfo.serverID, &server_duid)) {
dprintf(LOG_ERR, FNAME, "failed to copy server ID");
goto fail;
}
/* copy client information back */
if (duidcpy(&roptinfo.clientID, &optinfo->clientID)) {
dprintf(LOG_ERR, FNAME, "failed to copy client ID");
goto fail;
}
/* get per-host configuration for the client, if any. */
if ((client_conf = find_hostconf(&optinfo->clientID))) {
dprintf(LOG_DEBUG, FNAME,
"found a host configuration named %s", client_conf->name);
}
/* process authentication */
if (process_auth(dh6, len, client_conf, optinfo, &roptinfo)) {
dprintf(LOG_INFO, FNAME, "failed to process authentication "
"information for %s",
clientstr(client_conf, &optinfo->clientID));
goto fail;
}
if (client_conf == NULL && ifp->pool.name) {
if ((client_conf = create_dynamic_hostconf(&optinfo->clientID,
&ifp->pool)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to make host configuration");
goto fail;
}
}
TAILQ_INIT(&conflist);
/* make a local copy of the configured addresses */
if (dhcp6_copy_list(&conflist, &client_conf->addr_list)) {
dprintf(LOG_NOTICE, FNAME,
"failed to make local data");
goto fail;
}
/*
* the message must include an IPv6 address to be confirmed
* [RFC3315 18.2]. (IA-PD is just ignored [RFC3633 12.1])
*/
if (TAILQ_EMPTY(&optinfo->iana_list)) {
dprintf(LOG_INFO, FNAME, "no IA-NA option found");
goto fail;
}
for (iana = TAILQ_FIRST(&optinfo->iana_list); iana;
iana = TAILQ_NEXT(iana, link)) {
if (TAILQ_EMPTY(&iana->sublist)) {
dprintf(LOG_INFO, FNAME,
"no IA-ADDR option found in IA-NA %d",
iana->val_ia.iaid);
goto fail;
}
/*
* check whether the confirmed prefix matches
* the prefix from where the message originates.
* XXX: prefix length is assumed to be 64
*/
for (iaaddr = TAILQ_FIRST(&iana->sublist); iaaddr;
iaaddr = TAILQ_NEXT(iaaddr, link)) {
struct in6_addr *confaddr = &iaaddr->val_statefuladdr6.addr;
struct in6_addr *linkaddr;
struct sockaddr_in6 *src = (struct sockaddr_in6 *)from;
if (!IN6_IS_ADDR_LINKLOCAL(&src->sin6_addr)) {
/* CONFIRM is relayed via a DHCP-relay */
struct relayinfo *relayinfo;
if (relayinfohead == NULL) {
dprintf(LOG_INFO, FNAME,
"no link-addr found");
goto fail;
}
relayinfo = TAILQ_LAST(relayinfohead, relayinfolist);
/* XXX: link-addr is supposed to be a global address */
linkaddr = &relayinfo->linkaddr;
} else {
/* CONFIRM is directly arrived */
linkaddr = &ifp->addr;
}
if (memcmp(linkaddr, confaddr, 8) != 0) {
dprintf(LOG_INFO, FNAME,
"%s does not seem to belong to %s's link",
in6addr2str(confaddr, 0),
in6addr2str(linkaddr, 0));
stcode = DH6OPT_STCODE_NOTONLINK;
goto send_reply;
}
}
}
/*
* even when the given address seems to be on the appropriate link,
* the confirm should be ignore if there's no corrensponding IA-NA
* configuration.
*/
for (iana = TAILQ_FIRST(&optinfo->iana_list); iana;
iana = TAILQ_NEXT(iana, link)) {
if (make_ia(iana, &conflist, &roptinfo.iana_list,
client_conf, 1) == 0) {
dprintf(LOG_DEBUG, FNAME,
"IA-NA configuration not found");
goto fail;
}
}
send_reply:
if (dhcp6_add_listval(&roptinfo.stcode_list,
DHCP6_LISTVAL_STCODE, &stcode, NULL) == NULL)
goto fail;
error = server6_send(DH6_REPLY, ifp, dh6, optinfo, from, fromlen,
&roptinfo, relayinfohead, client_conf);
dhcp6_clear_options(&roptinfo);
dhcp6_clear_list(&conflist);
return (error);
fail:
dhcp6_clear_options(&roptinfo);
dhcp6_clear_list(&conflist);
return (-1);
}
static int
react_informreq(ifp, dh6, len, optinfo, from, fromlen, relayinfohead)
struct dhcp6_if *ifp;
struct dhcp6 *dh6;
ssize_t len;
struct dhcp6_optinfo *optinfo;
struct sockaddr *from;
int fromlen;
struct relayinfolist *relayinfohead;
{
struct dhcp6_optinfo roptinfo;
int error;
/*
* An IA option is not allowed to appear in an Information-request
* message. Such a message SHOULD be discarded.
* [RFC3315 Section 15]
*/
if (!TAILQ_EMPTY(&optinfo->iapd_list)) {
dprintf(LOG_INFO, FNAME,
"information request contains an IA_PD option");
return (-1);
}
if (!TAILQ_EMPTY(&optinfo->iana_list)) {
dprintf(LOG_INFO, FNAME,
"information request contains an IA_NA option");
return (-1);
}
/* if a server identifier is included, it must match ours. */
if (optinfo->serverID.duid_len &&
duidcmp(&optinfo->serverID, &server_duid)) {
dprintf(LOG_INFO, FNAME, "server DUID mismatch");
return (-1);
}
/*
* configure necessary options based on the options in request.
*/
dhcp6_init_options(&roptinfo);
/* server identifier option */
if (duidcpy(&roptinfo.serverID, &server_duid)) {
dprintf(LOG_ERR, FNAME, "failed to copy server ID");
goto fail;
}
/* copy client information back (if provided) */
if (optinfo->clientID.duid_id &&
duidcpy(&roptinfo.clientID, &optinfo->clientID)) {
dprintf(LOG_ERR, FNAME, "failed to copy client ID");
goto fail;
}
/* set stateless information */
if (set_statelessinfo(DH6_INFORM_REQ, &roptinfo)) {
dprintf(LOG_ERR, FNAME,
"failed to set other stateless information");
goto fail;
}
error = server6_send(DH6_REPLY, ifp, dh6, optinfo, from, fromlen,
&roptinfo, relayinfohead, NULL);
dhcp6_clear_options(&roptinfo);
return (error);
fail:
dhcp6_clear_options(&roptinfo);
return (-1);
}
static int
update_ia(msgtype, iap, retlist, optinfo)
int msgtype;
struct dhcp6_listval *iap;
struct dhcp6_list *retlist;
struct dhcp6_optinfo *optinfo;
{
struct dhcp6_binding *binding;
struct host_conf *client_conf;
/* get per-host configuration for the client, if any. */
if ((client_conf = find_hostconf(&optinfo->clientID))) {
dprintf(LOG_DEBUG, FNAME,
"found a host configuration named %s", client_conf->name);
}
if ((binding = find_binding(&optinfo->clientID, DHCP6_BINDING_IA,
iap->type, iap->val_ia.iaid)) == NULL) {
/*
* Behavior in the case where the delegating router cannot
* find a binding for the requesting router's IA_PD as
* described in RFC3633 Section 12.2. It is derived from
* Sections 18.2.3 and 18.2.4 of RFC3315, and the two sets
* of behavior are identical.
*/
dprintf(LOG_INFO, FNAME, "no binding found for %s",
duidstr(&optinfo->clientID));
switch (msgtype) {
case DH6_RENEW:
/*
* If the delegating router cannot find a binding for
* the requesting router's IA_PD the delegating router
* returns the IA_PD containing no prefixes with a
* Status Code option set to NoBinding in the Reply
* message.
*/
if (make_ia_stcode(iap->type, iap->val_ia.iaid,
DH6OPT_STCODE_NOBINDING, retlist)) {
dprintf(LOG_NOTICE, FNAME,
"failed to make an option list");
return (-1);
}
break;
case DH6_REBIND:
/*
* If it can be determined the prefixes are not
* appropriate from the delegating router's explicit
* configuration, it MAY send a Reply message to
* the requesting router containing the IA_PD with the
* lifetimes of the prefixes in the IA_PD set to zero.
*
* If unable to determine, the Rebind message is
* discarded.
*
* XXX: it is not very clear what the explicit
* configuration means. Thus, we always discard the
* message.
*/
return (-1);
default: /* XXX: should be a bug */
dprintf(LOG_ERR, FNAME, "impossible message type %s",
dhcp6msgstr(msgtype));
return (-1);
}
} else { /* we found a binding */
struct dhcp6_list ialist;
struct dhcp6_listval *lv;
struct dhcp6_prefix prefix;
struct dhcp6_statefuladdr saddr;
struct dhcp6_ia ia;
TAILQ_INIT(&ialist);
update_binding(binding);
/* see if each information to be renewed is still valid. */
for (lv = TAILQ_FIRST(&iap->sublist); lv;
lv = TAILQ_NEXT(lv, link)) {
struct dhcp6_listval *blv;
switch (iap->type) {
case DHCP6_LISTVAL_IAPD:
if (lv->type != DHCP6_LISTVAL_PREFIX6)
continue;
prefix = lv->val_prefix6;
blv = dhcp6_find_listval(&binding->val_list,
DHCP6_LISTVAL_PREFIX6, &prefix, 0);
if (blv == NULL) {
dprintf(LOG_DEBUG, FNAME,
"%s/%d is not found in %s",
in6addr2str(&prefix.addr, 0),
prefix.plen, bindingstr(binding));
prefix.pltime = 0;
prefix.vltime = 0;
} else {
prefix.pltime =
blv->val_prefix6.pltime;
prefix.vltime =
blv->val_prefix6.vltime;
}
if (dhcp6_add_listval(&ialist,
DHCP6_LISTVAL_PREFIX6, &prefix, NULL)
== NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to copy binding info");
dhcp6_clear_list(&ialist);
return (-1);
}
break;
case DHCP6_LISTVAL_IANA:
if (lv->type != DHCP6_LISTVAL_STATEFULADDR6)
continue;
saddr = lv->val_statefuladdr6;
blv = dhcp6_find_listval(&binding->val_list,
DHCP6_LISTVAL_STATEFULADDR6, &saddr, 0);
if (blv == NULL) {
dprintf(LOG_DEBUG, FNAME,
"%s is not found in %s",
in6addr2str(&saddr.addr, 0),
bindingstr(binding));
saddr.pltime = 0;
saddr.vltime = 0;
} else {
saddr.pltime =
blv->val_statefuladdr6.pltime;
saddr.vltime =
blv->val_statefuladdr6.vltime;
}
if (dhcp6_add_listval(&ialist,
DHCP6_LISTVAL_STATEFULADDR6, &saddr, NULL)
== NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to copy binding info");
dhcp6_clear_list(&ialist);
return (-1);
}
break;
default:
dprintf(LOG_ERR, FNAME, "unsupported IA type");
return (-1); /* XXX */
}
}
memset(&ia, 0, sizeof(ia));
ia.iaid = binding->iaid;
/* determine appropriate T1 and T2 */
calc_ia_timo(&ia, &ialist, client_conf);
if (dhcp6_add_listval(retlist, iap->type,
&ia, &ialist) == NULL) {
dhcp6_clear_list(&ialist);
return (-1);
}
dhcp6_clear_list(&ialist);
}
return (0);
}
static int
release_binding_ia(iap, retlist, optinfo)
struct dhcp6_listval *iap;
struct dhcp6_list *retlist;
struct dhcp6_optinfo *optinfo;
{
struct dhcp6_binding *binding;
if ((binding = find_binding(&optinfo->clientID, DHCP6_BINDING_IA,
iap->type, iap->val_ia.iaid)) == NULL) {
/*
* For each IA in the Release message for which the server has
* no binding information, the server adds an IA option using
* the IAID from the Release message and includes a Status Code
* option with the value NoBinding in the IA option.
*/
if (make_ia_stcode(iap->type, iap->val_ia.iaid,
DH6OPT_STCODE_NOBINDING, retlist)) {
dprintf(LOG_NOTICE, FNAME,
"failed to make an option list");
return (-1);
}
} else {
struct dhcp6_listval *lv, *lvia;
/*
* If the IAs in the message are in a binding for the client
* and the addresses in the IAs have been assigned by the
* server to those IAs, the server deletes the addresses from
* the IAs and makes the addresses available for assignment to
* other clients.
* [RFC3315 Section 18.2.6]
* RFC3633 is not very clear about the similar case for IA_PD,
* but we apply the same logic.
*/
for (lv = TAILQ_FIRST(&iap->sublist); lv;
lv = TAILQ_NEXT(lv, link)) {
if ((lvia = find_binding_ia(lv, binding)) != NULL) {
switch (binding->iatype) {
case DHCP6_LISTVAL_IAPD:
dprintf(LOG_DEBUG, FNAME,
"bound prefix %s/%d "
"has been released",
in6addr2str(&lvia->val_prefix6.addr,
0),
lvia->val_prefix6.plen);
break;
case DHCP6_LISTVAL_IANA:
release_address(&lvia->val_prefix6.addr);
dprintf(LOG_DEBUG, FNAME,
"bound address %s "
"has been released",
in6addr2str(&lvia->val_prefix6.addr,
0));
break;
}
TAILQ_REMOVE(&binding->val_list, lvia, link);
dhcp6_clear_listval(lvia);
if (TAILQ_EMPTY(&binding->val_list)) {
/*
* if the binding has become empty,
* stop procedure.
*/
remove_binding(binding);
return (0);
}
}
}
}
return (0);
}
static int
decline_binding_ia(iap, retlist, optinfo)
struct dhcp6_listval *iap;
struct dhcp6_list *retlist;
struct dhcp6_optinfo *optinfo;
{
struct dhcp6_binding *binding;
struct dhcp6_listval *lv, *lvia;
if ((binding = find_binding(&optinfo->clientID, DHCP6_BINDING_IA,
iap->type, iap->val_ia.iaid)) == NULL) {
/*
* For each IA in the Decline message for which the server has
* no binding information, the server adds an IA option using
* the IAID from the Release message and includes a Status Code
* option with the value NoBinding in the IA option.
*/
if (make_ia_stcode(iap->type, iap->val_ia.iaid,
DH6OPT_STCODE_NOBINDING, retlist)) {
dprintf(LOG_NOTICE, FNAME,
"failed to make an option list");
return (-1);
}
return (0);
}
/*
* If the IAs in the message are in a binding for the client and the
* addresses in the IAs have been assigned by the server to those IAs,
* the server deletes the addresses from the IAs and makes the addresses
* available for assignment to other clients. [RFC3315 Section 18.2.7]
*/
for (lv = TAILQ_FIRST(&iap->sublist); lv;
lv = TAILQ_NEXT(lv, link)) {
if (binding->iatype != DHCP6_LISTVAL_IANA) {
/* should never reach here */
continue;
}
if ((lvia = find_binding_ia(lv, binding)) == NULL) {
dprintf(LOG_DEBUG, FNAME, "no binding found "
"for address %s",
in6addr2str(&lv->val_statefuladdr6.addr, 0));
continue;
}
dprintf(LOG_DEBUG, FNAME,
"bound address %s has been marked as declined",
in6addr2str(&lvia->val_statefuladdr6.addr, 0));
decline_address(&lvia->val_statefuladdr6.addr);
TAILQ_REMOVE(&binding->val_list, lvia, link);
dhcp6_clear_listval(lvia);
if (TAILQ_EMPTY(&binding->val_list)) {
/*
* if the binding has become empty,
* stop procedure.
*/
remove_binding(binding);
return (0);
}
}
return (0);
}
static void
server6_signal(sig)
int sig;
{
dprintf(LOG_INFO, FNAME, "received a signal (%d)", sig);
switch (sig) {
case SIGTERM:
sig_flags |= SIGF_TERM;
break;
}
}
static int
server6_send(type, ifp, origmsg, optinfo, from, fromlen,
roptinfo, relayinfohead, client_conf)
int type;
struct dhcp6_if *ifp;
struct dhcp6 *origmsg;
struct dhcp6_optinfo *optinfo, *roptinfo;
struct sockaddr *from;
int fromlen;
struct relayinfolist *relayinfohead;
struct host_conf *client_conf;
{
char replybuf[BUFSIZ];
struct sockaddr_in6 dst;
int len, optlen;
int relayed = 0;
struct dhcp6 *dh6;
struct relayinfo *relayinfo;
if (sizeof(struct dhcp6) > sizeof(replybuf)) {
dprintf(LOG_ERR, FNAME, "buffer size assumption failed");
return (-1);
}
dh6 = (struct dhcp6 *)replybuf;
len = sizeof(*dh6);
memset(dh6, 0, sizeof(*dh6));
dh6->dh6_msgtypexid = origmsg->dh6_msgtypexid;
dh6->dh6_msgtype = (u_int8_t)type;
/* set options in the reply message */
if ((optlen = dhcp6_set_options(type, (struct dhcp6opt *)(dh6 + 1),
(struct dhcp6opt *)(replybuf + sizeof(replybuf)), roptinfo)) < 0) {
dprintf(LOG_INFO, FNAME, "failed to construct reply options");
return (-1);
}
len += optlen;
/* calculate MAC if necessary, and put it to the message */
switch (roptinfo->authproto) {
case DHCP6_AUTHPROTO_DELAYED:
if (client_conf == NULL || client_conf->delayedkey == NULL) {
/* This case should have been caught earlier */
dprintf(LOG_ERR, FNAME, "authentication required "
"but not key provided");
break;
}
if (dhcp6_calc_mac((char *)dh6, len, roptinfo->authproto,
roptinfo->authalgorithm,
roptinfo->delayedauth_offset + sizeof(*dh6),
client_conf->delayedkey)) {
dprintf(LOG_WARNING, FNAME, "failed to calculate MAC");
return (-1);
}
break;
default:
break; /* do nothing */
}
/* construct a relay chain, if necessary */
for (relayinfo = TAILQ_FIRST(relayinfohead); relayinfo;
relayinfo = TAILQ_NEXT(relayinfo, link)) {
struct dhcp6_optinfo relayopt;
struct dhcp6_vbuf relaymsgbuf;
struct dhcp6_relay *dh6relay;
relayed = 1;
dhcp6_init_options(&relayopt);
relaymsgbuf.dv_len = len;
relaymsgbuf.dv_buf = replybuf;
if (dhcp6_vbuf_copy(&relayopt.relay_msg, &relaymsgbuf))
return (-1);
if (relayinfo->relay_ifid.dv_buf &&
dhcp6_vbuf_copy(&relayopt.ifidopt,
&relayinfo->relay_ifid)) {
dhcp6_vbuf_free(&relayopt.relay_msg);
return (-1);
}
/* we can safely reuse replybuf here */
dh6relay = (struct dhcp6_relay *)replybuf;
memset(dh6relay, 0, sizeof (*dh6relay));
dh6relay->dh6relay_msgtype = DH6_RELAY_REPLY;
dh6relay->dh6relay_hcnt = relayinfo->hcnt;
memcpy(&dh6relay->dh6relay_linkaddr, &relayinfo->linkaddr,
sizeof (dh6relay->dh6relay_linkaddr));
memcpy(&dh6relay->dh6relay_peeraddr, &relayinfo->peeraddr,
sizeof (dh6relay->dh6relay_peeraddr));
len = sizeof(*dh6relay);
if ((optlen = dhcp6_set_options(DH6_RELAY_REPLY,
(struct dhcp6opt *)(dh6relay + 1),
(struct dhcp6opt *)(replybuf + sizeof(replybuf)),
&relayopt)) < 0) {
dprintf(LOG_INFO, FNAME,
"failed to construct relay message");
dhcp6_clear_options(&relayopt);
return (-1);
}
len += optlen;
dhcp6_clear_options(&relayopt);
}
/* specify the destination and send the reply */
dst = relayed ? *sa6_any_relay : *sa6_any_downstream;
dst.sin6_addr = ((struct sockaddr_in6 *)from)->sin6_addr;
dst.sin6_scope_id = ((struct sockaddr_in6 *)from)->sin6_scope_id;
if (transmit_sa(outsock, (struct sockaddr *)&dst,
replybuf, len) != 0) {
dprintf(LOG_ERR, FNAME, "transmit %s to %s failed",
dhcp6msgstr(type), addr2str((struct sockaddr *)&dst));
return (-1);
}
dprintf(LOG_DEBUG, FNAME, "transmit %s to %s",
dhcp6msgstr(type), addr2str((struct sockaddr *)&dst));
return (0);
}
static int
make_ia_stcode(iatype, iaid, stcode, retlist)
int iatype;
u_int16_t stcode;
u_int32_t iaid;
struct dhcp6_list *retlist;
{
struct dhcp6_list stcode_list;
struct dhcp6_ia ia_empty;
memset(&ia_empty, 0, sizeof(ia_empty));
ia_empty.iaid = iaid;
TAILQ_INIT(&stcode_list);
if (dhcp6_add_listval(&stcode_list, DHCP6_LISTVAL_STCODE,
&stcode, NULL) == NULL) {
dprintf(LOG_NOTICE, FNAME, "failed to make an option list");
return (-1);
}
if (dhcp6_add_listval(retlist, iatype,
&ia_empty, &stcode_list) == NULL) {
dprintf(LOG_NOTICE, FNAME, "failed to make an option list");
dhcp6_clear_list(&stcode_list);
return (-1);
}
dhcp6_clear_list(&stcode_list);
return (0);
}
static int
make_ia(spec, conflist, retlist, client_conf, do_binding)
struct dhcp6_listval *spec;
struct dhcp6_list *conflist, *retlist;
struct host_conf *client_conf;
int do_binding;
{
struct dhcp6_binding *binding;
struct dhcp6_list ialist;
struct dhcp6_listval *specia;
struct dhcp6_ia ia;
int found = 0;
/*
* If we happen to have a binding already, update the binding and
* return it. Perhaps the request is being retransmitted.
*/
if ((binding = find_binding(&client_conf->duid, DHCP6_BINDING_IA,
spec->type, spec->val_ia.iaid)) != NULL) {
struct dhcp6_list *blist = &binding->val_list;
struct dhcp6_listval *bia, *v;
dprintf(LOG_DEBUG, FNAME, "we have a binding already: %s",
bindingstr(binding));
update_binding(binding);
memset(&ia, 0, sizeof(ia));
ia.iaid = spec->val_ia.iaid;
/* determine appropriate T1 and T2 */
calc_ia_timo(&ia, blist, client_conf);
if (dhcp6_add_listval(retlist, spec->type, &ia, blist)
== NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to copy binding info");
return (0);
}
/* remove bound values from the configuration */
for (bia = TAILQ_FIRST(blist); bia;
bia = TAILQ_NEXT(bia, link)) {
if ((v = dhcp6_find_listval(conflist,
bia->type, &bia->uv, 0)) != NULL) {
TAILQ_REMOVE(conflist, v, link);
dhcp6_clear_listval(v);
}
}
return (1);
}
/*
* trivial case:
* if the configuration is empty, we cannot make any IA.
*/
if (TAILQ_EMPTY(conflist)) {
if (spec->type != DHCP6_LISTVAL_IANA ||
client_conf->pool.name == NULL) {
return (0);
}
}
TAILQ_INIT(&ialist);
/* First, check if we can meet the client's requirement */
for (specia = TAILQ_FIRST(&spec->sublist); specia;
specia = TAILQ_NEXT(specia, link)) {
/* try to find an IA that matches the spec best. */
if (!TAILQ_EMPTY(conflist)) {
if (make_match_ia(specia, conflist, &ialist))
found++;
} else if (spec->type == DHCP6_LISTVAL_IANA &&
client_conf->pool.name != NULL) {
if (make_iana_from_pool(&client_conf->pool, specia, &ialist))
found++;
}
}
if (found == 0) {
if (!TAILQ_EMPTY(conflist)) {
struct dhcp6_listval *v;
/* use the first IA in the configuration list */
for (v = TAILQ_FIRST(conflist); v; v = TAILQ_NEXT(v, link)) {
if (spec->type != DHCP6_LISTVAL_IANA)
break; /* always use the first IA for non-IANA */
if (!is_leased(&v->val_statefuladdr6.addr))
break;
}
if (v && dhcp6_add_listval(&ialist, v->type, &v->uv, NULL)) {
found = 1;
TAILQ_REMOVE(conflist, v, link);
dhcp6_clear_listval(v);
}
} else if (spec->type == DHCP6_LISTVAL_IANA &&
client_conf->pool.name != NULL) {
if (make_iana_from_pool(&client_conf->pool, NULL, &ialist))
found = 1;
}
}
if (found) {
memset(&ia, 0, sizeof(ia));
ia.iaid = spec->val_ia.iaid;
/* determine appropriate T1 and T2 */
calc_ia_timo(&ia, &ialist, client_conf);
/* make a binding for the set if necessary */
if (do_binding) {
if (add_binding(&client_conf->duid, DHCP6_BINDING_IA,
spec->type, spec->val_ia.iaid, &ialist) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to make a binding");
found = 0;
}
}
if (found) {
/* make an IA for the set */
if (dhcp6_add_listval(retlist, spec->type,
&ia, &ialist) == NULL)
found = 0;
}
dhcp6_clear_list(&ialist);
}
return (found);
}
static int
make_match_ia(spec, conflist, retlist)
struct dhcp6_listval *spec;
struct dhcp6_list *conflist, *retlist;
{
struct dhcp6_listval *match;
int matched = 0;
/* do we have the exact value specified? */
match = dhcp6_find_listval(conflist, spec->type, &spec->uv, 0);
/* if not, make further search specific to the IA type. */
if (!match) {
switch (spec->type) {
case DHCP6_LISTVAL_PREFIX6:
match = dhcp6_find_listval(conflist, spec->type,
&spec->uv, MATCHLIST_PREFIXLEN);
break;
case DHCP6_LISTVAL_STATEFULADDR6:
/* No "partial match" for addresses */
if (is_leased(&spec->val_statefuladdr6.addr))
match = 0;
break;
default:
dprintf(LOG_ERR, FNAME, "unsupported IA type");
return (0); /* XXX */
}
}
/*
* if found, remove the matched entry from the configuration list
* and copy the value in the returned list.
*/
if (match) {
if (dhcp6_add_listval(retlist, match->type,
&match->uv, NULL)) {
matched = 1;
TAILQ_REMOVE(conflist, match, link);
dhcp6_clear_listval(match);
}
}
return (matched);
}
/* making sublist of iana */
static int
make_iana_from_pool(poolspec, spec, retlist)
struct dhcp6_poolspec *poolspec;
struct dhcp6_listval *spec;
struct dhcp6_list *retlist;
{
struct dhcp6_statefuladdr saddr;
struct pool_conf *pool;
int found = 0;
dprintf(LOG_DEBUG, FNAME, "called");
if ((pool = find_pool(poolspec->name)) == NULL) {
dprintf(LOG_ERR, FNAME, "pool '%s' not found", poolspec->name);
return (0);
}
if (spec) {
memcpy(&saddr.addr, &spec->val_statefuladdr6.addr, sizeof(saddr.addr));
if (is_available_in_pool(pool, &saddr.addr)) {
found = 1;
}
} else {
if (get_free_address_from_pool(pool, &saddr.addr)) {
found = 1;
}
}
if (found) {
saddr.pltime = poolspec->pltime;
saddr.vltime = poolspec->vltime;
if (!dhcp6_add_listval(retlist, DHCP6_LISTVAL_STATEFULADDR6,
&saddr, NULL)) {
return (0);
}
}
dprintf(LOG_DEBUG, FNAME, "returns (found=%d)", found);
return (found);
}
static void
calc_ia_timo(ia, ialist, client_conf)
struct dhcp6_ia *ia;
struct dhcp6_list *ialist; /* this should not be empty */
struct host_conf *client_conf; /* unused yet */
{
struct dhcp6_listval *iav;
u_int32_t base = DHCP6_DURATION_INFINITE;
int iatype;
iatype = TAILQ_FIRST(ialist)->type;
for (iav = TAILQ_FIRST(ialist); iav; iav = TAILQ_NEXT(iav, link)) {
if (iav->type != iatype) {
dprintf(LOG_ERR, FNAME,
"assumption failure: IA list is not consistent");
exit (1); /* XXX */
}
switch (iatype) {
case DHCP6_LISTVAL_PREFIX6:
case DHCP6_LISTVAL_STATEFULADDR6:
if (base == DHCP6_DURATION_INFINITE ||
iav->val_prefix6.pltime < base)
base = iav->val_prefix6.pltime;
break;
}
}
switch (iatype) {
case DHCP6_LISTVAL_PREFIX6:
case DHCP6_LISTVAL_STATEFULADDR6:
/*
* Configure the timeout parameters as recommended in
* Section 22.4 of RFC3315 and Section 9 of RFC3633.
* We could also set the parameters to 0 if we let the client
* decide the renew timing (not implemented yet).
*/
if (base == DHCP6_DURATION_INFINITE) {
ia->t1 = DHCP6_DURATION_INFINITE;
ia->t2 = DHCP6_DURATION_INFINITE;
} else {
ia->t1 = base / 2;
ia->t2 = (base * 4) / 5;
}
break;
}
}
static void
update_binding_duration(binding)
struct dhcp6_binding *binding;
{
struct dhcp6_list *ia_list = &binding->val_list;
struct dhcp6_listval *iav;
int duration = DHCP6_DURATION_INFINITE;
u_int32_t past, min_lifetime;
time_t now = time(NULL);
min_lifetime = 0;
past = (u_int32_t)(now >= binding->updatetime ?
now - binding->updatetime : 0);
switch (binding->type) {
case DHCP6_BINDING_IA:
/*
* Binding configuration is a list of IA parameters.
* Determine the minimum valid lifetime.
*/
for (iav = TAILQ_FIRST(ia_list); iav;
iav = TAILQ_NEXT(iav, link)) {
u_int32_t lifetime;
switch (binding->iatype) {
case DHCP6_LISTVAL_IAPD:
lifetime = iav->val_prefix6.vltime;
break;
case DHCP6_LISTVAL_IANA:
lifetime = iav->val_statefuladdr6.vltime;
break;
default:
dprintf(LOG_ERR, FNAME, "unsupported IA type");
return; /* XXX */
}
if (min_lifetime == 0 ||
(lifetime != DHCP6_DURATION_INFINITE &&
lifetime < min_lifetime))
min_lifetime = lifetime;
}
if (past < min_lifetime)
duration = min_lifetime - past;
else
duration = 0;
break;
default:
/* should be internal error. */
dprintf(LOG_ERR, FNAME, "unknown binding type (%d)",
binding->type);
return;
}
binding->duration = duration;
}
static struct dhcp6_binding *
add_binding(clientid, btype, iatype, iaid, val0)
struct duid *clientid;
dhcp6_bindingtype_t btype;
int iatype;
u_int32_t iaid;
void *val0;
{
struct dhcp6_binding *binding = NULL;
u_int32_t duration = DHCP6_DURATION_INFINITE;
if ((binding = malloc(sizeof(*binding))) == NULL) {
dprintf(LOG_NOTICE, FNAME, "failed to allocate memory");
return (NULL);
}
memset(binding, 0, sizeof(*binding));
binding->type = btype;
if (duidcpy(&binding->clientid, clientid)) {
dprintf(LOG_NOTICE, FNAME, "failed to copy DUID");
goto fail;
}
binding->iatype = iatype;
binding->iaid = iaid;
/* construct configuration information for this binding */
switch (btype) {
case DHCP6_BINDING_IA:
TAILQ_INIT(&binding->val_list);
if (dhcp6_copy_list(&binding->val_list,
(struct dhcp6_list *)val0)) {
dprintf(LOG_NOTICE, FNAME,
"failed to copy binding data");
goto fail;
}
/* lease address */
if (iatype == DHCP6_LISTVAL_IANA) {
struct dhcp6_list *ia_list = &binding->val_list;
struct dhcp6_listval *lv, *lv_next;
for (lv = TAILQ_FIRST(ia_list); lv; lv = lv_next) {
lv_next = TAILQ_NEXT(lv, link);
if (lv->type != DHCP6_LISTVAL_STATEFULADDR6) {
dprintf(LOG_ERR, FNAME,
"unexpected binding value type(%d)", lv->type);
continue;
}
if (!lease_address(&lv->val_statefuladdr6.addr)) {
dprintf(LOG_NOTICE, FNAME,
"cannot lease address %s",
in6addr2str(&lv->val_statefuladdr6.addr, 0));
TAILQ_REMOVE(ia_list, lv, link);
dhcp6_clear_listval(lv);
}
}
if (TAILQ_EMPTY(ia_list)) {
dprintf(LOG_NOTICE, FNAME, "cannot lease any address");
goto fail;
}
}
break;
default:
dprintf(LOG_ERR, FNAME, "unexpected binding type(%d)", btype);
goto fail;
}
/* calculate duration and start timer accordingly */
binding->updatetime = time(NULL);
update_binding_duration(binding);
if (binding->duration != DHCP6_DURATION_INFINITE) {
struct timeval timo;
binding->timer = dhcp6_add_timer(binding_timo, binding);
if (binding->timer == NULL) {
dprintf(LOG_NOTICE, FNAME, "failed to add timer");
goto fail;
}
timo.tv_sec = (long)duration;
timo.tv_usec = 0;
dhcp6_set_timer(&timo, binding->timer);
}
TAILQ_INSERT_TAIL(&dhcp6_binding_head, binding, link);
dprintf(LOG_DEBUG, FNAME, "add a new binding %s", bindingstr(binding));
return (binding);
fail:
if (binding)
free_binding(binding);
return (NULL);
}
static struct dhcp6_binding *
find_binding(clientid, btype, iatype, iaid)
struct duid *clientid;
dhcp6_bindingtype_t btype;
int iatype;
u_int32_t iaid;
{
struct dhcp6_binding *bp;
for (bp = TAILQ_FIRST(&dhcp6_binding_head); bp;
bp = TAILQ_NEXT(bp, link)) {
if (bp->type != btype || duidcmp(&bp->clientid, clientid))
continue;
if (btype == DHCP6_BINDING_IA &&
(bp->iatype != iatype || bp->iaid != iaid))
continue;
return (bp);
}
return (NULL);
}
static void
update_binding(binding)
struct dhcp6_binding *binding;
{
struct timeval timo;
dprintf(LOG_DEBUG, FNAME, "update binding %s for %s",
bindingstr(binding), duidstr(&binding->clientid));
/* update timestamp and calculate new duration */
binding->updatetime = time(NULL);
update_binding_duration(binding);
/* if the lease duration is infinite, there's nothing to do. */
if (binding->duration == DHCP6_DURATION_INFINITE)
return;
/* reset the timer with the duration */
timo.tv_sec = (long)binding->duration;
timo.tv_usec = 0;
dhcp6_set_timer(&timo, binding->timer);
}
static void
remove_binding(binding)
struct dhcp6_binding *binding;
{
dprintf(LOG_DEBUG, FNAME, "remove a binding %s",
bindingstr(binding));
if (binding->timer)
dhcp6_remove_timer(&binding->timer);
TAILQ_REMOVE(&dhcp6_binding_head, binding, link);
free_binding(binding);
}
static void
free_binding(binding)
struct dhcp6_binding *binding;
{
duidfree(&binding->clientid);
/* free configuration info in a type dependent manner. */
switch (binding->type) {
case DHCP6_BINDING_IA:
/* releaes address */
if (binding->iatype == DHCP6_LISTVAL_IANA) {
struct dhcp6_list *ia_list = &binding->val_list;
struct dhcp6_listval *lv;
for (lv = TAILQ_FIRST(ia_list); lv; lv = TAILQ_NEXT(lv, link)) {
if (lv->type != DHCP6_LISTVAL_STATEFULADDR6) {
dprintf(LOG_ERR, FNAME,
"unexpected binding value type(%d)", lv->type);
continue;
}
release_address(&lv->val_statefuladdr6.addr);
}
}
dhcp6_clear_list(&binding->val_list);
break;
default:
dprintf(LOG_ERR, FNAME, "unknown binding type %d",
binding->type);
break;
}
free(binding);
}
static struct dhcp6_timer *
binding_timo(arg)
void *arg;
{
struct dhcp6_binding *binding = (struct dhcp6_binding *)arg;
struct dhcp6_list *ia_list = &binding->val_list;
struct dhcp6_listval *iav, *iav_next;
time_t now = time(NULL);
u_int32_t past, lifetime;
struct timeval timo;
past = (u_int32_t)(now >= binding->updatetime ?
now - binding->updatetime : 0);
switch (binding->type) {
case DHCP6_BINDING_IA:
for (iav = TAILQ_FIRST(ia_list); iav; iav = iav_next) {
iav_next = TAILQ_NEXT(iav, link);
switch (binding->iatype) {
case DHCP6_LISTVAL_IAPD:
case DHCP6_LISTVAL_IANA:
lifetime = iav->val_prefix6.vltime;
break;
default:
dprintf(LOG_ERR, FNAME, "internal error: "
"unknown binding type (%d)",
binding->iatype);
return (NULL); /* XXX */
}
if (lifetime != DHCP6_DURATION_INFINITE &&
lifetime <= past) {
dprintf(LOG_DEBUG, FNAME, "bound prefix %s/%d"
" in %s has expired",
in6addr2str(&iav->val_prefix6.addr, 0),
iav->val_prefix6.plen,
bindingstr(binding));
if (binding->iatype == DHCP6_LISTVAL_IANA)
release_address(&iav->val_prefix6.addr);
TAILQ_REMOVE(ia_list, iav, link);
dhcp6_clear_listval(iav);
}
}
/* If all IA parameters have expired, remove the binding. */
if (TAILQ_EMPTY(ia_list)) {
remove_binding(binding);
return (NULL);
}
break;
default:
dprintf(LOG_ERR, FNAME, "unknown binding type %d",
binding->type);
return (NULL); /* XXX */
}
update_binding_duration(binding);
/* if the lease duration is infinite, there's nothing to do. */
if (binding->duration == DHCP6_DURATION_INFINITE)
return (NULL);
/* reset the timer with the duration */
timo.tv_sec = (long)binding->duration;
timo.tv_usec = 0;
dhcp6_set_timer(&timo, binding->timer);
return (binding->timer);
}
static struct dhcp6_listval *
find_binding_ia(key, binding)
struct dhcp6_listval *key;
struct dhcp6_binding *binding;
{
struct dhcp6_list *ia_list = &binding->val_list;
switch (binding->type) {
case DHCP6_BINDING_IA:
return (dhcp6_find_listval(ia_list, key->type, &key->uv, 0));
default:
dprintf(LOG_ERR, FNAME, "unknown binding type %d",
binding->type);
return (NULL); /* XXX */
}
}
static char *
bindingstr(binding)
struct dhcp6_binding *binding;
{
static char strbuf[LINE_MAX]; /* XXX: thread unsafe */
char *iatype = NULL;
switch (binding->type) {
case DHCP6_BINDING_IA:
switch (binding->iatype) {
case DHCP6_LISTVAL_IAPD:
iatype = "PD";
break;
case DHCP6_LISTVAL_IANA:
iatype = "NA";
break;
}
snprintf(strbuf, sizeof(strbuf),
"[IA: duid=%s, type=%s, iaid=%lu, duration=%lu]",
duidstr(&binding->clientid), iatype, (u_long)binding->iaid,
(u_long)binding->duration);
break;
default:
dprintf(LOG_ERR, FNAME, "unexpected binding type(%d)",
binding->type);
return ("???");
}
return (strbuf);
}
static int
process_auth(dh6, len, client_conf, optinfo, roptinfo)
struct dhcp6 *dh6;
ssize_t len;
struct host_conf *client_conf;
struct dhcp6_optinfo *optinfo, *roptinfo;
{
u_int8_t msgtype = dh6->dh6_msgtype;
int authenticated = 0;
struct keyinfo *key;
/*
* if the client wanted DHCPv6 authentication, check if a secret
* key is available for the client.
*/
switch (optinfo->authproto) {
case DHCP6_AUTHPROTO_UNDEF:
/*
* The client did not include authentication option. What if
* we had sent authentication information? The specification
* is not clear, but we should probably accept it, since the
* client MAY ignore the information in advertise messages.
*/
return (0);
case DHCP6_AUTHPROTO_DELAYED:
if (optinfo->authalgorithm != DHCP6_AUTHALG_HMACMD5) {
dprintf(LOG_INFO, FNAME, "unknown authentication "
"algorithm (%d) required by %s",
optinfo->authalgorithm,
clientstr(client_conf, &optinfo->clientID));
break; /* give up with this authentication */
}
if (optinfo->authrdm != DHCP6_AUTHRDM_MONOCOUNTER) {
dprintf(LOG_INFO, FNAME,
"unknown RDM (%d) required by %s",
optinfo->authrdm,
clientstr(client_conf, &optinfo->clientID));
break; /* give up with this authentication */
}
/* see if we have a key for the client */
if (client_conf == NULL || client_conf->delayedkey == NULL) {
dprintf(LOG_INFO, FNAME, "client %s wanted "
"authentication, but no key found",
clientstr(client_conf, &optinfo->clientID));
break;
}
key = client_conf->delayedkey;
dprintf(LOG_DEBUG, FNAME, "found key %s for client %s",
key->name, clientstr(client_conf, &optinfo->clientID));
if (msgtype == DH6_SOLICIT) {
if (!(optinfo->authflags & DHCP6OPT_AUTHFLAG_NOINFO)) {
/*
* A solicit message should not contain
* authentication information.
*/
dprintf(LOG_INFO, FNAME,
"authentication information "
"provided in solicit from %s",
clientstr(client_conf,
&optinfo->clientID));
/* accept it anyway. (or discard?) */
}
} else {
/* replay protection */
if (!client_conf->saw_previous_rd) {
dprintf(LOG_WARNING, FNAME,
"previous RD value for %s is unknown "
"(accept it)", clientstr(client_conf,
&optinfo->clientID));
} else {
if (dhcp6_auth_replaycheck(optinfo->authrdm,
client_conf->previous_rd,
optinfo->authrd)) {
dprintf(LOG_INFO, FNAME,
"possible replay attack detected "
"for client %s",
clientstr(client_conf,
&optinfo->clientID));
break;
}
}
if ((optinfo->authflags & DHCP6OPT_AUTHFLAG_NOINFO)) {
dprintf(LOG_INFO, FNAME,
"client %s did not provide authentication "
"information in %s",
clientstr(client_conf, &optinfo->clientID),
dhcp6msgstr(msgtype));
break;
}
/*
* The client MUST use the same key used by the server
* to generate the authentication information.
* [RFC3315 Section 21.4.4.3]
* The RFC does not say what the server should do if
* the client breaks this rule, but it should be
* natural to interpret this as authentication failure.
*/
if (optinfo->delayedauth_keyid != key->keyid ||
optinfo->delayedauth_realmlen != key->realmlen ||
memcmp(optinfo->delayedauth_realmval, key->realm,
key->realmlen) != 0) {
dprintf(LOG_INFO, FNAME, "authentication key "
"mismatch with client %s",
clientstr(client_conf,
&optinfo->clientID));
break;
}
/* check for the key lifetime */
if (dhcp6_validate_key(key)) {
dprintf(LOG_INFO, FNAME, "key %s has expired",
key->name);
break;
}
/* validate MAC */
if (dhcp6_verify_mac((char *)dh6, len,
optinfo->authproto, optinfo->authalgorithm,
optinfo->delayedauth_offset + sizeof(*dh6), key)
== 0) {
dprintf(LOG_DEBUG, FNAME,
"message authentication validated for "
"client %s", clientstr(client_conf,
&optinfo->clientID));
} else {
dprintf(LOG_INFO, FNAME, "invalid message "
"authentication");
break;
}
}
roptinfo->authproto = optinfo->authproto;
roptinfo->authalgorithm = optinfo->authalgorithm;
roptinfo->authrdm = optinfo->authrdm;
if (get_rdvalue(roptinfo->authrdm, &roptinfo->authrd,
sizeof(roptinfo->authrd))) {
dprintf(LOG_ERR, FNAME, "failed to get a replay "
"detection value for %s",
clientstr(client_conf, &optinfo->clientID));
break; /* XXX: try to recover? */
}
roptinfo->delayedauth_keyid = key->keyid;
roptinfo->delayedauth_realmlen = key->realmlen;
roptinfo->delayedauth_realmval =
malloc(roptinfo->delayedauth_realmlen);
if (roptinfo->delayedauth_realmval == NULL) {
dprintf(LOG_ERR, FNAME, "failed to allocate memory "
"for authentication realm for %s",
clientstr(client_conf, &optinfo->clientID));
break;
}
memcpy(roptinfo->delayedauth_realmval, key->realm,
roptinfo->delayedauth_realmlen);
authenticated = 1;
break;
default:
dprintf(LOG_INFO, FNAME, "client %s wanted authentication "
"with unsupported protocol (%d)",
clientstr(client_conf, &optinfo->clientID),
optinfo->authproto);
return (-1); /* or simply ignore it? */
}
if (authenticated == 0) {
if (msgtype != DH6_SOLICIT) {
/*
* If the message fails to pass the validation test,
* the server MUST discard the message.
* [RFC3315 Section 21.4.5.2]
*/
return (-1);
}
} else {
/* Message authenticated. Update RD counter. */
if (msgtype != DH6_SOLICIT && client_conf != NULL) {
client_conf->previous_rd = optinfo->authrd;
client_conf->saw_previous_rd = 1;
}
}
return (0);
}
static inline char *
clientstr(conf, duid)
struct host_conf *conf;
struct duid *duid;
{
if (conf != NULL)
return (conf->name);
return (duidstr(duid));
}
|