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
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
|
/*
* Copyright (C) 2006-2017 by Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/*! \file server/dhcpv6.c */
#include "dhcpd.h"
#ifdef DHCPv6
#ifdef DHCP4o6
static void forw_dhcpv4_query(struct packet *packet);
static void send_dhcpv4_response(struct data_string *raw);
static void recv_dhcpv4_query(struct data_string *raw);
static void dhcp4o6_dhcpv4_query(struct data_string *reply_ret,
struct packet *packet);
#endif
/*
* We use print_hex_1() to output DUID values. We could actually output
* the DUID with more information... MAC address if using type 1 or 3,
* and so on. However, RFC 3315 contains Grave Warnings against actually
* attempting to understand a DUID.
*/
/*
* TODO: gettext() or other method of localization for the messages
* for status codes (and probably for log formats eventually)
* TODO: refactoring (simplify, simplify, simplify)
* TODO: support multiple shared_networks on each interface (this
* will allow the server to issue multiple IPv6 addresses to
* a single interface)
*/
/*
* DHCPv6 Reply workflow assist. A Reply packet is built by various
* different functions; this gives us one location where we keep state
* regarding a reply.
*/
struct reply_state {
/* root level persistent state */
struct shared_network *shared;
struct host_decl *host;
struct subnet *subnet; /* Used to match fixed-addrs to subnet scopes. */
struct option_state *opt_state;
struct packet *packet;
struct data_string client_id;
/* IA level persistent state */
unsigned ia_count;
unsigned pd_count;
unsigned client_resources;
isc_boolean_t resources_included;
isc_boolean_t static_lease;
unsigned static_prefixes;
struct ia_xx *ia;
struct ia_xx *old_ia;
struct option_state *reply_ia;
struct data_string fixed;
struct iaddrcidrnet fixed_pref; /* static prefix for logging */
/* IAADDR/PREFIX level persistent state */
struct iasubopt *lease;
/*
* "t1", "t2", preferred, and valid lifetimes records for calculating
* t1 and t2 (min/max).
*/
u_int32_t renew, rebind, min_prefer, min_valid;
/* Client-requested valid and preferred lifetimes. */
u_int32_t client_valid, client_prefer;
/* Chosen values to transmit for valid and preferred lifetimes. */
u_int32_t send_valid, send_prefer;
/* Preferred prefix length (-1 is any). */
int preflen;
/* Index into the data field that has been consumed. */
unsigned cursor;
/* Space for the on commit statements for a fixed host */
struct on_star on_star;
union reply_buffer {
unsigned char data[65536];
struct dhcpv6_packet reply;
} buf;
};
/*
* Prototypes local to this file.
*/
static int get_encapsulated_IA_state(struct option_state **enc_opt_state,
struct data_string *enc_opt_data,
struct packet *packet,
struct option_cache *oc,
int offset);
static void build_dhcpv6_reply(struct data_string *, struct packet *);
static isc_result_t shared_network_from_packet6(struct shared_network **shared,
struct packet *packet);
static void seek_shared_host(struct host_decl **hp,
struct shared_network *shared);
static isc_boolean_t fixed_matches_shared(struct host_decl *host,
struct shared_network *shared);
static isc_result_t reply_process_ia_na(struct reply_state *reply,
struct option_cache *ia);
static isc_result_t reply_process_ia_ta(struct reply_state *reply,
struct option_cache *ia);
static isc_result_t reply_process_addr(struct reply_state *reply,
struct option_cache *addr);
static isc_boolean_t address_is_owned(struct reply_state *reply,
struct iaddr *addr);
static isc_boolean_t temporary_is_available(struct reply_state *reply,
struct iaddr *addr);
static isc_result_t find_client_temporaries(struct reply_state *reply);
static isc_result_t reply_process_try_addr(struct reply_state *reply,
struct iaddr *addr);
static isc_result_t find_client_address(struct reply_state *reply);
static isc_result_t reply_process_is_addressed(struct reply_state *reply,
struct binding_scope **scope,
struct group *group);
static isc_result_t reply_process_send_addr(struct reply_state *reply,
struct iaddr *addr);
static struct iasubopt *lease_compare(struct iasubopt *alpha,
struct iasubopt *beta);
static isc_result_t reply_process_ia_pd(struct reply_state *reply,
struct option_cache *ia_pd);
static struct group *find_group_by_prefix(struct reply_state *reply);
static isc_result_t reply_process_prefix(struct reply_state *reply,
struct option_cache *pref);
static isc_boolean_t prefix_is_owned(struct reply_state *reply,
struct iaddrcidrnet *pref);
static isc_result_t find_client_prefix(struct reply_state *reply);
static isc_result_t reply_process_try_prefix(struct reply_state *reply,
struct iaddrcidrnet *pref);
static isc_result_t reply_process_is_prefixed(struct reply_state *reply,
struct binding_scope **scope,
struct group *group);
static isc_result_t reply_process_send_prefix(struct reply_state *reply,
struct iaddrcidrnet *pref);
static struct iasubopt *prefix_compare(struct reply_state *reply,
struct iasubopt *alpha,
struct iasubopt *beta);
static void schedule_lease_timeout_reply(struct reply_state *reply);
static int eval_prefix_mode(int thislen, int preflen, int prefix_mode);
static isc_result_t pick_v6_prefix_helper(struct reply_state *reply,
int prefix_mode);
static void unicast_reject(struct data_string *reply_ret, struct packet *packet,
const struct data_string *client_id,
const struct data_string *server_id);
static isc_boolean_t is_unicast_option_defined(struct packet *packet);
static isc_result_t shared_network_from_requested_addr (struct shared_network
**shared,
struct packet* packet);
static isc_result_t get_first_ia_addr_val (struct packet* packet, int addr_type,
struct iaddr* iaddr);
static void
set_reply_tee_times(struct reply_state* reply, unsigned ia_cursor);
static const char *iasubopt_plen_str(struct iasubopt *lease);
static int release_on_roam(struct reply_state *reply);
static int reuse_lease6(struct reply_state *reply, struct iasubopt *lease);
static void shorten_lifetimes(struct reply_state *reply, struct iasubopt *lease,
time_t age, int threshold);
static void write_to_packet(struct reply_state *reply, unsigned ia_cursor);
static const char *iasubopt_plen_str(struct iasubopt *lease);
#ifdef DHCP4o6
/*
* \brief Omapi I/O handler
*
* The inter-process communication receive handler.
* Get the message, put it into the raw data_string
* and call \ref send_dhcpv4_response() (DHCPv6 side) or
* \ref recv_dhcpv4_query() (DHCPv4 side)
*
* \param h the OMAPI object
* \return a result for I/O success or error (used by the I/O subsystem)
*/
isc_result_t dhcpv4o6_handler(omapi_object_t *h) {
char buf[65536];
struct data_string raw;
int cc;
if (h->type != dhcp4o6_type)
return DHCP_R_INVALIDARG;
cc = recv(dhcp4o6_fd, buf, sizeof(buf), 0);
if (cc < DHCP_FIXED_NON_UDP + 32)
return ISC_R_UNEXPECTED;
memset(&raw, 0, sizeof(raw));
if (!buffer_allocate(&raw.buffer, cc, MDL)) {
log_error("dhcpv4o6_handler: no memory buffer.");
return ISC_R_NOMEMORY;
}
raw.data = raw.buffer->data;
raw.len = cc;
memcpy(raw.buffer->data, buf, cc);
if (local_family == AF_INET6) {
send_dhcpv4_response(&raw);
} else {
recv_dhcpv4_query(&raw);
}
data_string_forget(&raw, MDL);
return ISC_R_SUCCESS;
}
/*
* \brief Send the DHCPv4-response back to the DHCPv6 side
* (DHCPv6 server function)
*
* Format: interface:16 + address:16 + DHCPv6 DHCPv4-response message
*
* \param raw the IPC message content
*/
static void send_dhcpv4_response(struct data_string *raw) {
struct interface_info *ip;
char name[16 + 1];
struct sockaddr_in6 to_addr;
char pbuf[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
int send_ret;
memset(name, 0, sizeof(name));
memcpy(name, raw->data, 16);
for (ip = interfaces; ip != NULL; ip = ip->next) {
if (!strcmp(name, ip->name))
break;
}
if (ip == NULL) {
log_error("send_dhcpv4_response: can't find interface %s.",
name);
return;
}
memset(&to_addr, 0, sizeof(to_addr));
to_addr.sin6_family = AF_INET6;
memcpy(&to_addr.sin6_addr, raw->data + 16, 16);
if ((raw->data[32] == DHCPV6_RELAY_FORW) ||
(raw->data[32] == DHCPV6_RELAY_REPL)) {
to_addr.sin6_port = local_port;
} else {
to_addr.sin6_port = remote_port;
}
log_info("send_dhcpv4_response(): sending %s on %s to %s port %d",
dhcpv6_type_names[raw->data[32]],
name,
inet_ntop(AF_INET6, raw->data + 16, pbuf, sizeof(pbuf)),
ntohs(to_addr.sin6_port));
send_ret = send_packet6(ip, raw->data + 32, raw->len - 32, &to_addr);
if (send_ret < 0) {
log_error("send_dhcpv4_response: send_packet6(): %m");
} else if (send_ret != raw->len - 32) {
log_error("send_dhcpv4_response: send_packet6() "
"sent %d of %d bytes",
send_ret, raw->len - 32);
}
}
#endif /* DHCP4o6 */
/*
* Schedule lease timeouts for all of the iasubopts in the reply.
* This is currently used to schedule timeouts for soft leases.
*/
static void
schedule_lease_timeout_reply(struct reply_state *reply) {
struct iasubopt *tmp;
int i;
/* sanity check the reply */
if ((reply == NULL) || (reply->ia == NULL) || (reply->ia->iasubopt == NULL))
return;
/* walk through the list, scheduling as we go */
for (i = 0 ; i < reply->ia->num_iasubopt ; i++) {
tmp = reply->ia->iasubopt[i];
schedule_lease_timeout(tmp->ipv6_pool);
}
}
/*
* This function returns the time since DUID time start for the
* given time_t value.
*/
static u_int32_t
duid_time(time_t when) {
/*
* This time is modulo 2^32.
*/
while ((when - DUID_TIME_EPOCH) > 4294967295u) {
/* use 2^31 to avoid spurious compiler warnings */
when -= 2147483648u;
when -= 2147483648u;
}
return when - DUID_TIME_EPOCH;
}
/*
* Server DUID.
*
* This must remain the same for the lifetime of this server, because
* clients return the server DUID that we sent them in Request packets.
*
* We pick the server DUID like this:
*
* 1. Check dhcpd.conf - any value the administrator has configured
* overrides any possible values.
* 2. Check the leases.txt - we want to use the previous value if
* possible.
* 3. Check if dhcpd.conf specifies a type of server DUID to use,
* and generate that type.
* 4. Generate a type 1 (time + hardware address) DUID.
*/
static struct data_string server_duid;
/*
* Check if the server_duid has been set.
*/
isc_boolean_t
server_duid_isset(void) {
return (server_duid.data != NULL);
}
/*
* Return the server_duid.
*/
void
copy_server_duid(struct data_string *ds, const char *file, int line) {
data_string_copy(ds, &server_duid, file, line);
}
/*
* Set the server DUID to a specified value. This is used when
* the server DUID is stored in persistent memory (basically the
* leases.txt file).
*/
void
set_server_duid(struct data_string *new_duid) {
/* INSIST(new_duid != NULL); */
/* INSIST(new_duid->data != NULL); */
if (server_duid_isset()) {
data_string_forget(&server_duid, MDL);
}
data_string_copy(&server_duid, new_duid, MDL);
}
/*
* Set the server DUID based on the D6O_SERVERID option. This handles
* the case where the administrator explicitly put it in the dhcpd.conf
* file.
*/
isc_result_t
set_server_duid_from_option(void) {
struct option_state *opt_state;
struct option_cache *oc;
struct data_string option_duid;
isc_result_t ret_val;
opt_state = NULL;
if (!option_state_allocate(&opt_state, MDL)) {
log_fatal("No memory for server DUID.");
}
execute_statements_in_scope(NULL, NULL, NULL, NULL, NULL,
opt_state, &global_scope, root_group,
NULL, NULL);
oc = lookup_option(&dhcpv6_universe, opt_state, D6O_SERVERID);
if (oc == NULL) {
ret_val = ISC_R_NOTFOUND;
} else {
memset(&option_duid, 0, sizeof(option_duid));
if (!evaluate_option_cache(&option_duid, NULL, NULL, NULL,
opt_state, NULL, &global_scope,
oc, MDL)) {
ret_val = ISC_R_UNEXPECTED;
} else {
set_server_duid(&option_duid);
data_string_forget(&option_duid, MDL);
ret_val = ISC_R_SUCCESS;
}
}
option_state_dereference(&opt_state, MDL);
return ret_val;
}
/*
* DUID layout, as defined in RFC 3315, section 9.
*
* We support type 1 (hardware address plus time) and type 3 (hardware
* address).
*
* We can support type 2 for specific vendors in the future, if they
* publish the specification. And of course there may be additional
* types later.
*/
static int server_duid_type = DUID_LLT;
/*
* Set the DUID type.
*/
void
set_server_duid_type(int type) {
server_duid_type = type;
}
/*
* Generate a new server DUID. This is done if there was no DUID in
* the leases.txt or in the dhcpd.conf file.
*/
isc_result_t
generate_new_server_duid(void) {
struct interface_info *p;
u_int32_t time_val;
struct data_string generated_duid;
/*
* Verify we have a type that we support.
*/
if ((server_duid_type != DUID_LL) && (server_duid_type != DUID_LLT)) {
log_error("Invalid DUID type %d specified, "
"only LL and LLT types supported", server_duid_type);
return DHCP_R_INVALIDARG;
}
/*
* Find an interface with a hardware address.
* Any will do. :)
*/
for (p = interfaces; p != NULL; p = p->next) {
if (p->hw_address.hlen > 0) {
break;
}
}
if (p == NULL) {
return ISC_R_UNEXPECTED;
}
/*
* Build our DUID.
*/
memset(&generated_duid, 0, sizeof(generated_duid));
if (server_duid_type == DUID_LLT) {
time_val = duid_time(time(NULL));
generated_duid.len = 8 + p->hw_address.hlen - 1;
if (!buffer_allocate(&generated_duid.buffer,
generated_duid.len, MDL)) {
log_fatal("No memory for server DUID.");
}
generated_duid.data = generated_duid.buffer->data;
putUShort(generated_duid.buffer->data, DUID_LLT);
putUShort(generated_duid.buffer->data + 2,
p->hw_address.hbuf[0]);
putULong(generated_duid.buffer->data + 4, time_val);
memcpy(generated_duid.buffer->data + 8,
p->hw_address.hbuf+1, p->hw_address.hlen-1);
} else if (server_duid_type == DUID_LL) {
generated_duid.len = 4 + p->hw_address.hlen - 1;
if (!buffer_allocate(&generated_duid.buffer,
generated_duid.len, MDL)) {
log_fatal("No memory for server DUID.");
}
generated_duid.data = generated_duid.buffer->data;
putUShort(generated_duid.buffer->data, DUID_LL);
putUShort(generated_duid.buffer->data + 2,
p->hw_address.hbuf[0]);
memcpy(generated_duid.buffer->data + 4,
p->hw_address.hbuf+1, p->hw_address.hlen-1);
} else {
log_fatal("Unsupported server DUID type %d.", server_duid_type);
}
set_server_duid(&generated_duid);
data_string_forget(&generated_duid, MDL);
return ISC_R_SUCCESS;
}
/*
* Get the client identifier from the packet.
*/
isc_result_t
get_client_id(struct packet *packet, struct data_string *client_id) {
struct option_cache *oc;
/*
* Verify our client_id structure is empty.
*/
if ((client_id->data != NULL) || (client_id->len != 0)) {
return DHCP_R_INVALIDARG;
}
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_CLIENTID);
if (oc == NULL) {
return ISC_R_NOTFOUND;
}
if (!evaluate_option_cache(client_id, packet, NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
return ISC_R_FAILURE;
}
return ISC_R_SUCCESS;
}
/*
* Message validation, defined in RFC 3315, sections 15.2, 15.5, 15.7:
*
* Servers MUST discard any Solicit messages that do not include a
* Client Identifier option or that do include a Server Identifier
* option.
*/
int
valid_client_msg(struct packet *packet, struct data_string *client_id) {
int ret_val;
struct option_cache *oc;
struct data_string data;
ret_val = 0;
memset(client_id, 0, sizeof(*client_id));
memset(&data, 0, sizeof(data));
switch (get_client_id(packet, client_id)) {
case ISC_R_SUCCESS:
break;
case ISC_R_NOTFOUND:
log_debug("Discarding %s from %s; "
"client identifier missing",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr));
goto exit;
default:
log_error("Error processing %s from %s; "
"unable to evaluate Client Identifier",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr));
goto exit;
}
/*
* Required by RFC 3315, section 15.
*/
if (packet->unicast) {
log_debug("Discarding %s from %s; packet sent unicast "
"(CLIENTID %s)",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr),
print_hex_1(client_id->len, client_id->data, 60));
goto exit;
}
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_SERVERID);
if (oc != NULL) {
if (evaluate_option_cache(&data, packet, NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_debug("Discarding %s from %s; "
"server identifier found "
"(CLIENTID %s, SERVERID %s)",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr),
print_hex_1(client_id->len,
client_id->data, 60),
print_hex_2(data.len,
data.data, 60));
} else {
log_debug("Discarding %s from %s; "
"server identifier found "
"(CLIENTID %s)",
dhcpv6_type_names[packet->dhcpv6_msg_type],
print_hex_1(client_id->len,
client_id->data, 60),
piaddr(packet->client_addr));
}
goto exit;
}
/* looks good */
ret_val = 1;
exit:
if (data.len > 0) {
data_string_forget(&data, MDL);
}
if (!ret_val) {
if (client_id->len > 0) {
data_string_forget(client_id, MDL);
}
}
return ret_val;
}
/*
* Response validation, defined in RFC 3315, sections 15.4, 15.6, 15.8,
* 15.9 (slightly different wording, but same meaning):
*
* Servers MUST discard any received Request message that meet any of
* the following conditions:
*
* - the message does not include a Server Identifier option.
* - the contents of the Server Identifier option do not match the
* server's DUID.
* - the message does not include a Client Identifier option.
*/
int
valid_client_resp(struct packet *packet,
struct data_string *client_id,
struct data_string *server_id)
{
int ret_val;
struct option_cache *oc;
/* INSIST((duid.data != NULL) && (duid.len > 0)); */
ret_val = 0;
memset(client_id, 0, sizeof(*client_id));
memset(server_id, 0, sizeof(*server_id));
switch (get_client_id(packet, client_id)) {
case ISC_R_SUCCESS:
break;
case ISC_R_NOTFOUND:
log_debug("Discarding %s from %s; "
"client identifier missing",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr));
goto exit;
default:
log_error("Error processing %s from %s; "
"unable to evaluate Client Identifier",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr));
goto exit;
}
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_SERVERID);
if (oc == NULL) {
log_debug("Discarding %s from %s: "
"server identifier missing (CLIENTID %s)",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr),
print_hex_1(client_id->len, client_id->data, 60));
goto exit;
}
if (!evaluate_option_cache(server_id, packet, NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("Error processing %s from %s; "
"unable to evaluate Server Identifier (CLIENTID %s)",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr),
print_hex_1(client_id->len, client_id->data, 60));
goto exit;
}
if ((server_duid.len != server_id->len) ||
(memcmp(server_duid.data, server_id->data, server_duid.len) != 0)) {
log_debug("Discarding %s from %s; "
"not our server identifier "
"(CLIENTID %s, SERVERID %s, server DUID %s)",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr),
print_hex_1(client_id->len, client_id->data, 60),
print_hex_2(server_id->len, server_id->data, 60),
print_hex_3(server_duid.len, server_duid.data, 60));
goto exit;
}
/* looks good */
ret_val = 1;
exit:
if (!ret_val) {
if (server_id->len > 0) {
data_string_forget(server_id, MDL);
}
if (client_id->len > 0) {
data_string_forget(client_id, MDL);
}
}
return ret_val;
}
/*
* Information request validation, defined in RFC 3315, section 15.12:
*
* Servers MUST discard any received Information-request message that
* meets any of the following conditions:
*
* - The message includes a Server Identifier option and the DUID in
* the option does not match the server's DUID.
*
* - The message includes an IA option.
*/
int
valid_client_info_req(struct packet *packet, struct data_string *server_id) {
int ret_val;
struct option_cache *oc;
struct data_string client_id;
char client_id_str[80]; /* print_hex_1() uses maximum 60 characters,
plus a few more for extra information */
ret_val = 0;
memset(server_id, 0, sizeof(*server_id));
memset(&client_id, 0, sizeof(client_id));
/*
* Make a string that we can print out to give more
* information about the client if we need to.
*
* By RFC 3315, Section 18.1.5 clients SHOULD have a
* client-id on an Information-request packet, but it
* is not strictly necessary.
*/
if (get_client_id(packet, &client_id) == ISC_R_SUCCESS) {
snprintf(client_id_str, sizeof(client_id_str), " (CLIENTID %s)",
print_hex_1(client_id.len, client_id.data, 60));
data_string_forget(&client_id, MDL);
} else {
client_id_str[0] = '\0';
}
/*
* Required by RFC 3315, section 15.
*/
if (packet->unicast) {
log_debug("Discarding %s from %s; packet sent unicast%s",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr), client_id_str);
goto exit;
}
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_IA_NA);
if (oc != NULL) {
log_debug("Discarding %s from %s; "
"IA_NA option present%s",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr), client_id_str);
goto exit;
}
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_IA_TA);
if (oc != NULL) {
log_debug("Discarding %s from %s; "
"IA_TA option present%s",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr), client_id_str);
goto exit;
}
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_IA_PD);
if (oc != NULL) {
log_debug("Discarding %s from %s; "
"IA_PD option present%s",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr), client_id_str);
goto exit;
}
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_SERVERID);
if (oc != NULL) {
if (!evaluate_option_cache(server_id, packet, NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("Error processing %s from %s; "
"unable to evaluate Server Identifier%s",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr), client_id_str);
goto exit;
}
if ((server_duid.len != server_id->len) ||
(memcmp(server_duid.data, server_id->data,
server_duid.len) != 0)) {
log_debug("Discarding %s from %s; "
"not our server identifier "
"(SERVERID %s, server DUID %s)%s",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr),
print_hex_1(server_id->len,
server_id->data, 60),
print_hex_2(server_duid.len,
server_duid.data, 60),
client_id_str);
goto exit;
}
}
/* looks good */
ret_val = 1;
exit:
if (!ret_val) {
if (server_id->len > 0) {
data_string_forget(server_id, MDL);
}
}
return ret_val;
}
/*
* Options that we want to send, in addition to what was requested
* via the ORO.
*/
static const int required_opts[] = {
D6O_CLIENTID,
D6O_SERVERID,
D6O_STATUS_CODE,
D6O_PREFERENCE,
0
};
static const int required_opts_solicit[] = {
D6O_CLIENTID,
D6O_SERVERID,
D6O_IA_NA,
D6O_IA_TA,
D6O_IA_PD,
D6O_RAPID_COMMIT,
D6O_STATUS_CODE,
D6O_RECONF_ACCEPT,
D6O_PREFERENCE,
0
};
static const int required_opts_agent[] = {
D6O_INTERFACE_ID,
D6O_RELAY_MSG,
0
};
static const int required_opts_IA[] = {
D6O_IAADDR,
D6O_STATUS_CODE,
0
};
static const int required_opts_IA_PD[] = {
D6O_IAPREFIX,
D6O_STATUS_CODE,
0
};
static const int required_opts_STATUS_CODE[] = {
D6O_STATUS_CODE,
0
};
#ifdef DHCP4o6
static const int required_opts_4o6[] = {
D6O_DHCPV4_MSG,
0
};
#endif
static const int unicast_reject_opts[] = {
D6O_CLIENTID,
D6O_SERVERID,
D6O_STATUS_CODE,
0
};
/*
* Extracts from packet contents an IA_* option, storing the IA structure
* in its entirety in enc_opt_data, and storing any decoded DHCPv6 options
* in enc_opt_state for later lookup and evaluation. The 'offset' indicates
* where in the IA_* the DHCPv6 options commence.
*/
static int
get_encapsulated_IA_state(struct option_state **enc_opt_state,
struct data_string *enc_opt_data,
struct packet *packet,
struct option_cache *oc,
int offset)
{
/*
* Get the raw data for the encapsulated options.
*/
memset(enc_opt_data, 0, sizeof(*enc_opt_data));
if (!evaluate_option_cache(enc_opt_data, packet,
NULL, NULL, packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("get_encapsulated_IA_state: "
"error evaluating raw option.");
return 0;
}
if (enc_opt_data->len < offset) {
log_error("get_encapsulated_IA_state: raw option too small.");
data_string_forget(enc_opt_data, MDL);
return 0;
}
/*
* Now create the option state structure, and pass it to the
* function that parses options.
*/
*enc_opt_state = NULL;
if (!option_state_allocate(enc_opt_state, MDL)) {
log_error("get_encapsulated_IA_state: no memory for options.");
data_string_forget(enc_opt_data, MDL);
return 0;
}
if (!parse_option_buffer(*enc_opt_state,
enc_opt_data->data + offset,
enc_opt_data->len - offset,
&dhcpv6_universe)) {
log_error("get_encapsulated_IA_state: error parsing options.");
option_state_dereference(enc_opt_state, MDL);
data_string_forget(enc_opt_data, MDL);
return 0;
}
return 1;
}
static int
set_status_code(u_int16_t status_code, const char *status_message,
struct option_state *opt_state)
{
struct data_string d;
int ret_val;
memset(&d, 0, sizeof(d));
d.len = sizeof(status_code) + strlen(status_message);
if (!buffer_allocate(&d.buffer, d.len, MDL)) {
log_fatal("set_status_code: no memory for status code.");
}
d.data = d.buffer->data;
putUShort(d.buffer->data, status_code);
memcpy(d.buffer->data + sizeof(status_code),
status_message, d.len - sizeof(status_code));
if (!save_option_buffer(&dhcpv6_universe, opt_state,
d.buffer, (unsigned char *)d.data, d.len,
D6O_STATUS_CODE, 0)) {
log_error("set_status_code: error saving status code.");
ret_val = 0;
} else {
ret_val = 1;
}
data_string_forget(&d, MDL);
return ret_val;
}
void check_pool6_threshold(struct reply_state *reply,
struct iasubopt *lease)
{
struct ipv6_pond *pond;
isc_uint64_t used, count, high_threshold;
int poolhigh = 0, poollow = 0;
char *shared_name = "no name";
char tmp_addr[INET6_ADDRSTRLEN];
if ((lease->ipv6_pool == NULL) || (lease->ipv6_pool->ipv6_pond == NULL))
return;
pond = lease->ipv6_pool->ipv6_pond;
/* If the address range is too large to track, just skip all this. */
if (pond->jumbo_range == 1) {
return;
}
count = pond->num_total;
used = pond->num_active;
/* get network name for logging */
if ((pond->shared_network != NULL) &&
(pond->shared_network->name != NULL)) {
shared_name = pond->shared_network->name;
}
/* The logged flag indicates if we have already crossed the high
* threshold and emitted a log message. If it is set we check to
* see if we have re-crossed the low threshold and need to reset
* things. When we cross the high threshold we determine what
* the low threshold is and save it into the low_threshold value.
* When we cross that threshold we reset the logged flag and
* the low_threshold to 0 which allows the high threshold message
* to be emitted once again.
* if we haven't recrossed the boundry we don't need to do anything.
*/
if (pond->logged !=0) {
if (used <= pond->low_threshold) {
pond->low_threshold = 0;
pond->logged = 0;
log_error("Pool threshold reset - shared subnet: %s; "
"address: %s; low threshold %llu/%llu.",
shared_name,
inet_ntop(AF_INET6, &lease->addr,
tmp_addr, sizeof(tmp_addr)),
used, count);
}
return;
}
/* find the high threshold */
if (get_option_int(&poolhigh, &server_universe, reply->packet, NULL,
NULL, reply->packet->options, reply->opt_state,
reply->opt_state, &lease->scope,
SV_LOG_THRESHOLD_HIGH, MDL) == 0) {
/* no threshold bail out */
return;
}
/* We do have a threshold for this pool, see if its valid */
if ((poolhigh <= 0) || (poolhigh > 100)) {
/* not valid */
return;
}
/* we have a valid value, have we exceeded it */
high_threshold = FIND_POND6_PERCENT(count, poolhigh);
if (used < high_threshold) {
/* nope, no more to do */
return;
}
/* we've exceeded it, output a message */
log_error("Pool threshold exceeded - shared subnet: %s; "
"address: %s; high threshold %d%% %llu/%llu.",
shared_name,
inet_ntop(AF_INET6, &lease->addr, tmp_addr, sizeof(tmp_addr)),
poolhigh, used, count);
/* handle the low threshold now, if we don't
* have one we default to 0. */
if ((get_option_int(&poollow, &server_universe, reply->packet, NULL,
NULL, reply->packet->options, reply->opt_state,
reply->opt_state, &lease->scope,
SV_LOG_THRESHOLD_LOW, MDL) == 0) ||
(poollow > 100)) {
poollow = 0;
}
/*
* If the low theshold is higher than the high threshold we continue to log
* If it isn't then we set the flag saying we already logged and determine
* what the reset threshold is.
*/
if (poollow < poolhigh) {
pond->logged = 1;
pond->low_threshold = FIND_POND6_PERCENT(count, poollow);
}
}
/*
* We have a set of operations we do to set up the reply packet, which
* is the same for many message types.
*/
static int
start_reply(struct packet *packet,
const struct data_string *client_id,
const struct data_string *server_id,
struct option_state **opt_state,
struct dhcpv6_packet *reply)
{
struct option_cache *oc;
const unsigned char *server_id_data;
int server_id_len;
/*
* Build our option state for reply.
*/
*opt_state = NULL;
if (!option_state_allocate(opt_state, MDL)) {
log_error("start_reply: no memory for option_state.");
return 0;
}
execute_statements_in_scope(NULL, packet, NULL, NULL,
packet->options, *opt_state,
&global_scope, root_group, NULL, NULL);
/*
* A small bit of special handling for Solicit messages.
*
* We could move the logic into a flag, but for now just check
* explicitly.
*/
if (packet->dhcpv6_msg_type == DHCPV6_SOLICIT) {
reply->msg_type = DHCPV6_ADVERTISE;
/*
* If:
* - this message type supports rapid commit (Solicit), and
* - the server is configured to supply a rapid commit, and
* - the client requests a rapid commit,
* Then we add a rapid commit option, and send Reply (instead
* of an Advertise).
*/
oc = lookup_option(&dhcpv6_universe,
*opt_state, D6O_RAPID_COMMIT);
if (oc != NULL) {
oc = lookup_option(&dhcpv6_universe,
packet->options, D6O_RAPID_COMMIT);
if (oc != NULL) {
/* Rapid-commit in action. */
reply->msg_type = DHCPV6_REPLY;
} else {
/* Don't want a rapid-commit in advertise. */
delete_option(&dhcpv6_universe,
*opt_state, D6O_RAPID_COMMIT);
}
}
} else {
reply->msg_type = DHCPV6_REPLY;
/* Delete the rapid-commit from the sent options. */
oc = lookup_option(&dhcpv6_universe,
*opt_state, D6O_RAPID_COMMIT);
if (oc != NULL) {
delete_option(&dhcpv6_universe,
*opt_state, D6O_RAPID_COMMIT);
}
}
/*
* Use the client's transaction identifier for the reply.
*/
memcpy(reply->transaction_id, packet->dhcpv6_transaction_id,
sizeof(reply->transaction_id));
/*
* RFC 3315, section 18.2 says we need server identifier and
* client identifier.
*
* If the server ID is defined via the configuration file, then
* it will already be present in the option state at this point,
* so we don't need to set it.
*
* If we have a server ID passed in from the caller,
* use that, otherwise use the global DUID.
*/
oc = lookup_option(&dhcpv6_universe, *opt_state, D6O_SERVERID);
if (oc == NULL) {
if (server_id == NULL) {
server_id_data = server_duid.data;
server_id_len = server_duid.len;
} else {
server_id_data = server_id->data;
server_id_len = server_id->len;
}
if (!save_option_buffer(&dhcpv6_universe, *opt_state,
NULL, (unsigned char *)server_id_data,
server_id_len, D6O_SERVERID, 0)) {
log_error("start_reply: "
"error saving server identifier.");
return 0;
}
}
if (client_id->buffer != NULL) {
if (!save_option_buffer(&dhcpv6_universe, *opt_state,
client_id->buffer,
(unsigned char *)client_id->data,
client_id->len,
D6O_CLIENTID, 0)) {
log_error("start_reply: error saving "
"client identifier.");
return 0;
}
}
/*
* If the client accepts reconfiguration, let it know that we
* will send them.
*
* Note: we don't actually do this yet, but DOCSIS requires we
* claim to.
*/
oc = lookup_option(&dhcpv6_universe, packet->options,
D6O_RECONF_ACCEPT);
if (oc != NULL) {
if (!save_option_buffer(&dhcpv6_universe, *opt_state,
NULL, (unsigned char *)"", 0,
D6O_RECONF_ACCEPT, 0)) {
log_error("start_reply: "
"error saving RECONF_ACCEPT option.");
option_state_dereference(opt_state, MDL);
return 0;
}
}
return 1;
}
/*
* Try to get the IPv6 address the client asked for from the
* pool.
*
* addr is the result (should be a pointer to NULL on entry)
* pool is the pool to search in
* requested_addr is the address the client wants
*/
static isc_result_t
try_client_v6_address(struct iasubopt **addr,
struct ipv6_pool *pool,
const struct data_string *requested_addr)
{
struct in6_addr tmp_addr;
isc_result_t result;
if (requested_addr->len < sizeof(tmp_addr)) {
return DHCP_R_INVALIDARG;
}
memcpy(&tmp_addr, requested_addr->data, sizeof(tmp_addr));
if (IN6_IS_ADDR_UNSPECIFIED(&tmp_addr)) {
return ISC_R_FAILURE;
}
/*
* The address is not covered by this (or possibly any) dynamic
* range.
*/
if (!ipv6_in_pool(&tmp_addr, pool)) {
return ISC_R_ADDRNOTAVAIL;
}
if (lease6_exists(pool, &tmp_addr)) {
return ISC_R_ADDRINUSE;
}
result = iasubopt_allocate(addr, MDL);
if (result != ISC_R_SUCCESS) {
return result;
}
(*addr)->addr = tmp_addr;
(*addr)->plen = 0;
/* Default is soft binding for 2 minutes. */
result = add_lease6(pool, *addr, cur_time + 120);
if (result != ISC_R_SUCCESS) {
iasubopt_dereference(addr, MDL);
}
return result;
}
/*!
*
* \brief Get an IPv6 address for the client.
*
* Attempt to find a usable address for the client. We walk through
* the ponds checking for permit and deny then through the pools
* seeing if they have an available address.
*
* \param reply = the state structure for the current work on this request
* if we create a lease we return it using reply->lease
*
* \return
* ISC_R_SUCCESS = we were able to find an address and are returning a
* pointer to the lease
* ISC_R_NORESOURCES = there don't appear to be any free addresses. This
* is probabalistic. We don't exhaustively try the
* address range, instead we hash the duid and if
* the address derived from the hash is in use we
* hash the address. After a number of failures we
* conclude the pool is basically full.
*/
static isc_result_t
pick_v6_address(struct reply_state *reply)
{
struct ipv6_pool *p = NULL;
struct ipv6_pond *pond;
int i;
int start_pool;
unsigned int attempts;
char tmp_buf[INET6_ADDRSTRLEN];
struct iasubopt **addr = &reply->lease;
isc_uint64_t total = 0;
isc_uint64_t active = 0;
isc_uint64_t abandoned = 0;
int jumbo_range = 0;
char *shared_name = (reply->shared->name ?
reply->shared->name : "(no name)");
/*
* Do a quick walk through of the ponds and pools
* to see if we have any NA address pools
*/
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
if (pond->ipv6_pools == NULL)
continue;
for (i = 0; (p = pond->ipv6_pools[i]) != NULL; i++) {
if (p->pool_type == D6O_IA_NA)
break;
}
if (p != NULL)
break;
}
/* If we get here and p is NULL we have no useful pools */
if (p == NULL) {
log_debug("Unable to pick client address: "
"no IPv6 pools on this shared network");
return ISC_R_NORESOURCES;
}
/*
* We have at least one pool that could provide an address
* Now we walk through the ponds and pools again and check
* to see if the client is permitted and if an address is
* available
*
* Within a given pond we start looking at the last pool we
* allocated from, unless it had a collision trying to allocate
* an address. This will tend to move us into less-filled pools.
*/
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
isc_result_t result = ISC_R_FAILURE;
if (((pond->prohibit_list != NULL) &&
(permitted(reply->packet, pond->prohibit_list))) ||
((pond->permit_list != NULL) &&
(!permitted(reply->packet, pond->permit_list))))
continue;
#ifdef EUI_64
/* If pond is EUI-64 but client duid isn't a valid EUI-64
* id, then skip this pond */
if (pond->use_eui_64 &&
!valid_eui_64_duid(&reply->ia->iaid_duid, IAID_LEN)) {
continue;
}
#endif
start_pool = pond->last_ipv6_pool;
i = start_pool;
do {
p = pond->ipv6_pools[i];
if (p->pool_type == D6O_IA_NA) {
#ifdef EUI_64
if (pond->use_eui_64) {
result =
create_lease6_eui_64(p, addr,
&reply->ia->iaid_duid,
cur_time + 120);
}
else
#endif
{
result =
create_lease6(p, addr, &attempts,
&reply->ia->iaid_duid,
cur_time + 120);
}
if (result == ISC_R_SUCCESS) {
/*
* Record the pool used (or next one if
* there was a collision).
*/
if (attempts > 1) {
i++;
if (pond->ipv6_pools[i]
== NULL) {
i = 0;
}
}
pond->last_ipv6_pool = i;
log_debug("Picking pool address %s",
inet_ntop(AF_INET6,
&((*addr)->addr),
tmp_buf, sizeof(tmp_buf)));
return (ISC_R_SUCCESS);
}
}
i++;
if (pond->ipv6_pools[i] == NULL) {
i = 0;
}
} while (i != start_pool);
if (result == ISC_R_NORESOURCES) {
jumbo_range += pond->jumbo_range;
total += pond->num_total;
active += pond->num_active;
abandoned += pond->num_abandoned;
}
}
/*
* If we failed to pick an IPv6 address from any of the subnets.
* Presumably that means we have no addresses for the client.
*/
if (jumbo_range != 0) {
log_debug("Unable to pick client address: "
"no addresses available - shared network %s: "
" 2^64-1 < total, %llu active, %llu abandoned",
shared_name, active - abandoned, abandoned);
} else {
log_debug("Unable to pick client address: "
"no addresses available - shared network %s: "
"%llu total, %llu active, %llu abandoned",
shared_name, total, active - abandoned, abandoned);
}
return ISC_R_NORESOURCES;
}
/*
* Try to get the IPv6 prefix the client asked for from the
* prefix pool.
*
* pref is the result (should be a pointer to NULL on entry)
* pool is the prefix pool to search in
* requested_pref is the address the client wants
*/
static isc_result_t
try_client_v6_prefix(struct iasubopt **pref,
struct ipv6_pool *pool,
const struct data_string *requested_pref)
{
u_int8_t tmp_plen;
struct in6_addr tmp_pref;
struct iaddr ia;
isc_result_t result;
if (requested_pref->len < sizeof(tmp_plen) + sizeof(tmp_pref)) {
return DHCP_R_INVALIDARG;
}
tmp_plen = (int) requested_pref->data[0];
if ((tmp_plen < 3) || (tmp_plen > 128)) {
return ISC_R_FAILURE;
}
memcpy(&tmp_pref, requested_pref->data + 1, sizeof(tmp_pref));
if (IN6_IS_ADDR_UNSPECIFIED(&tmp_pref)) {
return ISC_R_FAILURE;
}
ia.len = 16;
memcpy(&ia.iabuf, &tmp_pref, 16);
if (!is_cidr_mask_valid(&ia, (int) tmp_plen)) {
return ISC_R_FAILURE;
}
if (!ipv6_in_pool(&tmp_pref, pool) ||
((int)tmp_plen != pool->units)) {
return ISC_R_ADDRNOTAVAIL;
}
if (prefix6_exists(pool, &tmp_pref, tmp_plen)) {
return ISC_R_ADDRINUSE;
}
result = iasubopt_allocate(pref, MDL);
if (result != ISC_R_SUCCESS) {
return result;
}
(*pref)->addr = tmp_pref;
(*pref)->plen = tmp_plen;
/* Default is soft binding for 2 minutes. */
result = add_lease6(pool, *pref, cur_time + 120);
if (result != ISC_R_SUCCESS) {
iasubopt_dereference(pref, MDL);
}
return result;
}
/*!
*
* \brief Get an IPv6 prefix for the client.
*
* Attempt to find a usable prefix for the client. Based upon the prefix
* length mode and the plen supplied by the client (if one), we make one
* or more calls to pick_v6_prefix_helper() to find a prefix as follows:
*
* PLM_IGNORE or client specifies a plen of zero, use the first available
* prefix regardless of it's length.
*
* PLM_PREFER – look for an exact match to client's plen first, if none
* found, use the first available prefix of any length
*
* PLM_EXACT – look for an exact match first, if none found then fail. This
* is the default behavior.
*
* PLM_MAXIMUM - look for an exact match first, then the first available whose
* prefix length is less than client's plen, otherwise fail.
*
* PLM_MINIMUM - look for an exact match first, then the first available whose
* prefix length is greater than client's plen, otherwise fail.
*
* Note that the selection mode is configurable at the global scope only via
* prefix-len-mode.
*
* \param reply = the state structure for the current work on this request
* if we create a lease we return it using reply->lease
*
* \return
* ISC_R_SUCCESS = we were able to find an prefix and are returning a
* pointer to the lease
* ISC_R_NORESOURCES = there don't appear to be any free addresses. This
* is probabalistic. We don't exhaustively try the
* address range, instead we hash the duid and if
* the address derived from the hash is in use we
* hash the address. After a number of failures we
* conclude the pool is basically full.
*/
static isc_result_t
pick_v6_prefix(struct reply_state *reply) {
struct ipv6_pool *p = NULL;
struct ipv6_pond *pond;
int i;
isc_result_t result;
/*
* Do a quick walk through of the ponds and pools
* to see if we have any prefix pools
*/
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
if (pond->ipv6_pools == NULL)
continue;
for (i = 0; (p = pond->ipv6_pools[i]) != NULL; i++) {
if (p->pool_type == D6O_IA_PD)
break;
}
if (p != NULL)
break;
}
/* If we get here and p is NULL we have no useful pools */
if (p == NULL) {
log_debug("Unable to pick client prefix: "
"no IPv6 pools on this shared network");
return ISC_R_NORESOURCES;
}
if (reply->preflen <= 0) {
/* If we didn't get a plen (-1) or client plen is 0, then just
* select first available (same as PLM_INGORE) */
result = pick_v6_prefix_helper(reply, PLM_IGNORE);
} else {
switch (prefix_length_mode) {
case PLM_PREFER:
/* First we look for an exact match, if not found
* then first available */
result = pick_v6_prefix_helper(reply, PLM_EXACT);
if (result != ISC_R_SUCCESS) {
result = pick_v6_prefix_helper(reply,
PLM_IGNORE);
}
break;
case PLM_EXACT:
/* Match exactly or fail */
result = pick_v6_prefix_helper(reply, PLM_EXACT);
break;
case PLM_MINIMUM:
case PLM_MAXIMUM:
/* First we look for an exact match, if not found
* then first available by mode */
result = pick_v6_prefix_helper(reply, PLM_EXACT);
if (result != ISC_R_SUCCESS) {
result = pick_v6_prefix_helper(reply,
prefix_length_mode);
}
break;
default:
/* First available */
result = pick_v6_prefix_helper(reply, PLM_IGNORE);
break;
}
}
if (result == ISC_R_SUCCESS) {
char tmp_buf[INET6_ADDRSTRLEN];
log_debug("Picking pool prefix %s/%u",
inet_ntop(AF_INET6, &(reply->lease->addr),
tmp_buf, sizeof(tmp_buf)),
(unsigned)(reply->lease->plen));
return (ISC_R_SUCCESS);
}
/*
* If we failed to pick an IPv6 prefix
* Presumably that means we have no prefixes for the client.
*/
log_debug("Unable to pick client prefix: no prefixes available");
return ISC_R_NORESOURCES;
}
/*!
*
* \brief Get an IPv6 prefix for the client based upon selection mode.
*
* We walk through the ponds checking for permit and deny. If a pond is
* permissable to use, loop through its PD pools checking prefix lengths
* against the client plen based on the prefix length mode, looking for
* available prefixes.
*
* \param reply = the state structure for the current work on this request
* if we create a lease we return it using reply->lease
* \prefix_mode = selection mode to use
*
* \return
* ISC_R_SUCCESS = we were able to find a prefix and are returning a
* pointer to the lease
* ISC_R_NORESOURCES = there don't appear to be any free addresses. This
* is probabalistic. We don't exhaustively try the
* address range, instead we hash the duid and if
* the address derived from the hash is in use we
* hash the address. After a number of failures we
* conclude the pool is basically full.
*/
isc_result_t
pick_v6_prefix_helper(struct reply_state *reply, int prefix_mode) {
struct ipv6_pool *p = NULL;
struct ipv6_pond *pond;
int i;
unsigned int attempts;
struct iasubopt **pref = &reply->lease;
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
if (((pond->prohibit_list != NULL) &&
(permitted(reply->packet, pond->prohibit_list))) ||
((pond->permit_list != NULL) &&
(!permitted(reply->packet, pond->permit_list))))
continue;
for (i = 0; (p = pond->ipv6_pools[i]) != NULL; i++) {
if ((p->pool_type == D6O_IA_PD) &&
(eval_prefix_mode(p->units, reply->preflen,
prefix_mode) == 1) &&
(create_prefix6(p, pref, &attempts,
&reply->ia->iaid_duid,
cur_time + 120) == ISC_R_SUCCESS)) {
return (ISC_R_SUCCESS);
}
}
}
return ISC_R_NORESOURCES;
}
/*!
*
* \brief Test a prefix length against another based on prefix length mode
*
* \param len - prefix length to test
* \param preflen - preferred prefix length against which to test
* \param prefix_mode - prefix selection mode with which to test
*
* Note that the case of preferred length of 0 is not short-cut here as it
* is assumed to be done at a higher level.
*
* \return 1 if the given length is usable based upon mode and a preferred
* length, 0 if not.
*/
int
eval_prefix_mode(int len, int preflen, int prefix_mode) {
int use_it = 1;
switch (prefix_mode) {
case PLM_EXACT:
use_it = (len == preflen);
break;
case PLM_MINIMUM:
/* they asked for a prefix length no "shorter" than preflen */
use_it = (len >= preflen);
break;
case PLM_MAXIMUM:
/* they asked for a prefix length no "longer" than preflen */
use_it = (len <= preflen);
break;
default:
/* otherwise use it */
break;
}
return (use_it);
}
/*
*! \file server/dhcpv6.c
*
* \brief construct a reply containing information about a client's lease
*
* lease_to_client() is called from several messages to construct a
* reply that contains all that we know about the client's correct lease
* (or projected lease).
*
* Solicit - "Soft" binding, ignore unknown addresses or bindings, just
* send what we "may" give them on a request.
*
* Request - "Hard" binding, but ignore supplied addresses (just provide what
* the client should really use).
*
* Renew - "Hard" binding, but client-supplied addresses are 'real'. Error
* Rebind out any "wrong" addresses the client sends. This means we send
* an empty IA_NA with a status code of NoBinding or NotOnLink or
* possibly send the address with zeroed lifetimes.
*
* Information-Request - No binding.
*
* The basic structure is to traverse the client-supplied data first, and
* validate and echo back any contents that can be. If the client-supplied
* data does not error out (on renew/rebind as above), but we did not send
* any addresses, attempt to allocate one.
*
* At the end of the this function we call commit_leases_timed() to
* fsync and rotate the file as necessary. commit_leases_timed() will
* check that we have written at least one lease to the file and that
* some time has passed before doing any fsync or file rewrite so we
* don't bother tracking if we did a write_ia during this function.
*/
/* TODO: look at client hints for lease times */
static void
lease_to_client(struct data_string *reply_ret,
struct packet *packet,
const struct data_string *client_id,
const struct data_string *server_id)
{
static struct reply_state reply;
struct option_cache *oc;
struct data_string packet_oro;
int i;
memset(&packet_oro, 0, sizeof(packet_oro));
/* Locate the client. */
if (shared_network_from_packet6(&reply.shared,
packet) != ISC_R_SUCCESS)
goto exit;
/*
* Initialize the reply.
*/
packet_reference(&reply.packet, packet, MDL);
data_string_copy(&reply.client_id, client_id, MDL);
if (!start_reply(packet, client_id, server_id, &reply.opt_state,
&reply.buf.reply))
goto exit;
/* Set the write cursor to just past the reply header. */
reply.cursor = REPLY_OPTIONS_INDEX;
/*
* Get the ORO from the packet, if any.
*/
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_ORO);
if (oc != NULL) {
if (!evaluate_option_cache(&packet_oro, packet,
NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("lease_to_client: error evaluating ORO.");
goto exit;
}
}
/*
* Find a host record that matches the packet, if any, and is
* valid for the shared network the client is on.
*/
if (find_hosts6(&reply.host, packet, client_id, MDL)) {
packet->known = 1;
seek_shared_host(&reply.host, reply.shared);
}
/* Process the client supplied IA's onto the reply buffer. */
reply.ia_count = 0;
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_IA_NA);
for (; oc != NULL ; oc = oc->next) {
isc_result_t status;
/* Start counting resources (addresses) offered. */
reply.client_resources = 0;
reply.resources_included = ISC_FALSE;
status = reply_process_ia_na(&reply, oc);
/*
* We continue to try other IA's whether we can address
* this one or not. Any other result is an immediate fail.
*/
if ((status != ISC_R_SUCCESS) &&
(status != ISC_R_NORESOURCES))
goto exit;
}
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_IA_TA);
for (; oc != NULL ; oc = oc->next) {
isc_result_t status;
/* Start counting resources (addresses) offered. */
reply.client_resources = 0;
reply.resources_included = ISC_FALSE;
status = reply_process_ia_ta(&reply, oc);
/*
* We continue to try other IA's whether we can address
* this one or not. Any other result is an immediate fail.
*/
if ((status != ISC_R_SUCCESS) &&
(status != ISC_R_NORESOURCES))
goto exit;
}
/* Same for IA_PD's. */
reply.pd_count = 0;
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_IA_PD);
for (; oc != NULL ; oc = oc->next) {
isc_result_t status;
/* Start counting resources (prefixes) offered. */
reply.client_resources = 0;
reply.resources_included = ISC_FALSE;
status = reply_process_ia_pd(&reply, oc);
/*
* We continue to try other IA_PD's whether we can address
* this one or not. Any other result is an immediate fail.
*/
if ((status != ISC_R_SUCCESS) &&
(status != ISC_R_NORESOURCES))
goto exit;
}
/*
* Make no reply if we gave no resources and is not
* for Information-Request.
*/
if ((reply.ia_count == 0) && (reply.pd_count == 0)) {
if (reply.packet->dhcpv6_msg_type !=
DHCPV6_INFORMATION_REQUEST)
goto exit;
/*
* Because we only execute statements on a per-IA basis,
* we need to execute statements in any non-IA reply to
* source configuration.
*/
execute_statements_in_scope(NULL, reply.packet, NULL, NULL,
reply.packet->options,
reply.opt_state, &global_scope,
reply.shared->group, root_group,
NULL);
/* Execute statements from class scopes. */
for (i = reply.packet->class_count; i > 0; i--) {
execute_statements_in_scope(NULL, reply.packet,
NULL, NULL,
reply.packet->options,
reply.opt_state,
&global_scope,
reply.packet->classes[i - 1]->group,
reply.shared->group, NULL);
}
/* Bring in any configuration from a host record. */
if (reply.host != NULL)
execute_statements_in_scope(NULL, reply.packet,
NULL, NULL,
reply.packet->options,
reply.opt_state,
&global_scope,
reply.host->group,
reply.shared->group, NULL);
}
/*
* RFC3315 section 17.2.2 (Solicit):
*
* If the server will not assign any addresses to any IAs in a
* subsequent Request from the client, the server MUST send an
* Advertise message to the client that includes only a Status
* Code option with code NoAddrsAvail and a status message for
* the user, a Server Identifier option with the server's DUID,
* and a Client Identifier option with the client's DUID.
*
* This has been updated by an errata such that the server
* can always send an IA.
*
* Section 18.2.1 (Request):
*
* If the server cannot assign any addresses to an IA in the
* message from the client, the server MUST include the IA in
* the Reply message with no addresses in the IA and a Status
* Code option in the IA containing status code NoAddrsAvail.
*
* Section 18.1.8 (Client Behavior):
*
* Leave unchanged any information about addresses the client has
* recorded in the IA but that were not included in the IA from
* the server.
* Sends a Renew/Rebind if the IA is not in the Reply message.
*/
/*
* Having stored the client's IA's, store any options that
* will fit in the remaining space.
*/
reply.cursor += store_options6((char *)reply.buf.data + reply.cursor,
sizeof(reply.buf) - reply.cursor,
reply.opt_state, reply.packet,
required_opts_solicit,
&packet_oro);
/* Return our reply to the caller. */
reply_ret->len = reply.cursor;
reply_ret->buffer = NULL;
if (!buffer_allocate(&reply_ret->buffer, reply.cursor, MDL)) {
log_fatal("No memory to store Reply.");
}
memcpy(reply_ret->buffer->data, reply.buf.data, reply.cursor);
reply_ret->data = reply_ret->buffer->data;
/* If appropriate commit and rotate the lease file */
(void) commit_leases_timed();
exit:
/* Cleanup. */
if (reply.shared != NULL)
shared_network_dereference(&reply.shared, MDL);
if (reply.host != NULL)
host_dereference(&reply.host, MDL);
if (reply.opt_state != NULL)
option_state_dereference(&reply.opt_state, MDL);
if (reply.packet != NULL)
packet_dereference(&reply.packet, MDL);
if (reply.client_id.data != NULL)
data_string_forget(&reply.client_id, MDL);
if (packet_oro.buffer != NULL)
data_string_forget(&packet_oro, MDL);
reply.renew = reply.rebind = reply.min_prefer = reply.min_valid = 0;
reply.cursor = 0;
}
/* Process a client-supplied IA_NA. This may append options to the tail of
* the reply packet being built in the reply_state structure.
*/
static isc_result_t
reply_process_ia_na(struct reply_state *reply, struct option_cache *ia) {
isc_result_t status = ISC_R_SUCCESS;
u_int32_t iaid;
unsigned ia_cursor;
struct option_state *packet_ia;
struct option_cache *oc;
struct data_string ia_data, data;
/* Initialize values that will get cleaned up on return. */
packet_ia = NULL;
memset(&ia_data, 0, sizeof(ia_data));
memset(&data, 0, sizeof(data));
/*
* Note that find_client_address() may set reply->lease.
*/
/* Make sure there is at least room for the header. */
if ((reply->cursor + IA_NA_OFFSET + 4) > sizeof(reply->buf)) {
log_error("reply_process_ia_na: Reply too long for IA.");
return ISC_R_NOSPACE;
}
/* Fetch the IA_NA contents. */
if (!get_encapsulated_IA_state(&packet_ia, &ia_data, reply->packet,
ia, IA_NA_OFFSET)) {
log_error("reply_process_ia_na: error evaluating ia");
status = ISC_R_FAILURE;
goto cleanup;
}
/* Extract IA_NA header contents. */
iaid = getULong(ia_data.data);
reply->renew = getULong(ia_data.data + 4);
reply->rebind = getULong(ia_data.data + 8);
/* Create an IA_NA structure. */
if (ia_allocate(&reply->ia, iaid, (char *)reply->client_id.data,
reply->client_id.len, MDL) != ISC_R_SUCCESS) {
log_error("reply_process_ia_na: no memory for ia.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
reply->ia->ia_type = D6O_IA_NA;
/* Cache pre-existing IA, if any. */
ia_hash_lookup(&reply->old_ia, ia_na_active,
(unsigned char *)reply->ia->iaid_duid.data,
reply->ia->iaid_duid.len, MDL);
/*
* Create an option cache to carry the IA_NA option contents, and
* execute any user-supplied values into it.
*/
if (!option_state_allocate(&reply->reply_ia, MDL)) {
status = ISC_R_NOMEMORY;
goto cleanup;
}
/* Check & cache the fixed host record. */
if ((reply->host != NULL) && (reply->host->fixed_addr != NULL)) {
struct iaddr tmp_addr;
if (!evaluate_option_cache(&reply->fixed, NULL, NULL, NULL,
NULL, NULL, &global_scope,
reply->host->fixed_addr, MDL)) {
log_error("reply_process_ia_na: unable to evaluate "
"fixed address.");
status = ISC_R_FAILURE;
goto cleanup;
}
if (reply->fixed.len < 16) {
log_error("reply_process_ia_na: invalid fixed address.");
status = DHCP_R_INVALIDARG;
goto cleanup;
}
/* Find the static lease's subnet. */
tmp_addr.len = 16;
memcpy(tmp_addr.iabuf, reply->fixed.data, 16);
if (find_grouped_subnet(&reply->subnet, reply->shared,
tmp_addr, MDL) == 0)
log_fatal("Impossible condition at %s:%d.", MDL);
reply->static_lease = ISC_TRUE;
} else
reply->static_lease = ISC_FALSE;
/*
* Save the cursor position at the start of the IA, so we can
* set length and adjust t1/t2 values later. We write a temporary
* header out now just in case we decide to adjust the packet
* within sub-process functions.
*/
ia_cursor = reply->cursor;
/* Initialize the IA_NA header. First the code. */
putUShort(reply->buf.data + reply->cursor, (unsigned)D6O_IA_NA);
reply->cursor += 2;
/* Then option length. */
putUShort(reply->buf.data + reply->cursor, 0x0Cu);
reply->cursor += 2;
/* Then IA_NA header contents; IAID. */
putULong(reply->buf.data + reply->cursor, iaid);
reply->cursor += 4;
/* We store the client's t1 for now, and may over-ride it later. */
putULong(reply->buf.data + reply->cursor, reply->renew);
reply->cursor += 4;
/* We store the client's t2 for now, and may over-ride it later. */
putULong(reply->buf.data + reply->cursor, reply->rebind);
reply->cursor += 4;
/*
* For each address in this IA_NA, decide what to do about it.
*
* Guidelines:
*
* The client leaves unchanged any information about addresses
* it has recorded but are not included ("cancel/break" below).
* A not included IA ("cleanup" below) could give a Renew/Rebind.
*/
oc = lookup_option(&dhcpv6_universe, packet_ia, D6O_IAADDR);
reply->min_valid = reply->min_prefer = INFINITE_TIME;
reply->client_valid = reply->client_prefer = 0;
for (; oc != NULL ; oc = oc->next) {
status = reply_process_addr(reply, oc);
/*
* Canceled means we did not allocate addresses to the
* client, but we're "done" with this IA - we set a status
* code. So transmit this reply, e.g., move on to the next
* IA.
*/
if (status == ISC_R_CANCELED)
break;
if ((status != ISC_R_SUCCESS) &&
(status != ISC_R_ADDRINUSE) &&
(status != ISC_R_ADDRNOTAVAIL))
goto cleanup;
}
reply->ia_count++;
/*
* If we fell through the above and never gave the client
* an address, give it one now.
*/
if ((status != ISC_R_CANCELED) && (reply->client_resources == 0)) {
status = find_client_address(reply);
if (status == ISC_R_NORESOURCES) {
switch (reply->packet->dhcpv6_msg_type) {
case DHCPV6_SOLICIT:
/*
* No address for any IA is handled
* by the caller.
*/
/* FALL THROUGH */
case DHCPV6_REQUEST:
/* Section 18.2.1 (Request):
*
* If the server cannot assign any addresses to
* an IA in the message from the client, the
* server MUST include the IA in the Reply
* message with no addresses in the IA and a
* Status Code option in the IA containing
* status code NoAddrsAvail.
*/
option_state_dereference(&reply->reply_ia, MDL);
if (!option_state_allocate(&reply->reply_ia,
MDL))
{
log_error("reply_process_ia_na: No "
"memory for option state "
"wipe.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
if (!set_status_code(STATUS_NoAddrsAvail,
"No addresses available "
"for this interface.",
reply->reply_ia)) {
log_error("reply_process_ia_na: Unable "
"to set NoAddrsAvail status "
"code.");
status = ISC_R_FAILURE;
goto cleanup;
}
status = ISC_R_SUCCESS;
break;
default:
/*
* RFC 3315 does not tell us to emit a status
* code in this condition, or anything else.
*
* If we included non-allocated addresses
* (zeroed lifetimes) in an IA, then the client
* will deconfigure them.
*
* So we want to include the IA even if we
* can't give it a new address if it includes
* zeroed lifetime addresses.
*
* We don't want to include the IA if we
* provide zero addresses including zeroed
* lifetimes.
*/
if (reply->resources_included)
status = ISC_R_SUCCESS;
else
goto cleanup;
break;
}
}
if (status != ISC_R_SUCCESS)
goto cleanup;
}
/*
* yes, goto's aren't the best but we also want to avoid extra
* indents
*/
if (status == ISC_R_CANCELED) {
/* We're replying with a status code so we still need to
* write it out in wire-format to the outbound buffer */
write_to_packet(reply, ia_cursor);
goto cleanup;
}
/*
* Handle static leases, we always log stuff and if it's
* a hard binding we run any commit statements that we have
*/
if (reply->static_lease) {
char tmp_addr[INET6_ADDRSTRLEN];
log_info("%s NA: address %s to client with duid %s iaid = %d "
"static",
dhcpv6_type_names[reply->buf.reply.msg_type],
inet_ntop(AF_INET6, reply->fixed.data, tmp_addr,
sizeof(tmp_addr)),
print_hex_1(reply->client_id.len,
reply->client_id.data, 60),
iaid);
if ((reply->buf.reply.msg_type == DHCPV6_REPLY) &&
(reply->on_star.on_commit != NULL)) {
execute_statements(NULL, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state, NULL,
reply->on_star.on_commit, NULL);
executable_statement_dereference
(&reply->on_star.on_commit, MDL);
}
goto cleanup;
}
/*
* If we have any addresses log what we are doing.
*/
if (reply->ia->num_iasubopt != 0) {
struct iasubopt *tmp;
int i;
char tmp_addr[INET6_ADDRSTRLEN];
for (i = 0 ; i < reply->ia->num_iasubopt ; i++) {
tmp = reply->ia->iasubopt[i];
log_info("%s NA: address %s to client with duid %s "
"iaid = %d valid for %u seconds",
dhcpv6_type_names[reply->buf.reply.msg_type],
inet_ntop(AF_INET6, &tmp->addr,
tmp_addr, sizeof(tmp_addr)),
print_hex_1(reply->client_id.len,
reply->client_id.data, 60),
iaid, tmp->valid);
}
}
/*
* If this is not a 'soft' binding, consume the new changes into
* the database (if any have been attached to the ia_na).
*
* Loop through the assigned dynamic addresses, referencing the
* leases onto this IA_NA rather than any old ones, and updating
* pool timers for each (if any).
*
* Note that we must do ddns_updates() before we test for lease
* reuse (so we'll know if DNS entries are different). To ensure
* we don't break any configs, we run on_commit statements before
* we do ddns_updates() just in case the former affects the later.
* This is symetrical with v4 logic. We always run on_commit and
* ddns_udpates() whether a lease is reused or renewed.
*/
if ((reply->ia->num_iasubopt != 0) &&
(reply->buf.reply.msg_type == DHCPV6_REPLY)) {
int must_commit = 0;
struct iasubopt *tmp;
struct data_string *ia_id;
int i;
for (i = 0 ; i < reply->ia->num_iasubopt ; i++) {
tmp = reply->ia->iasubopt[i];
if (tmp->ia != NULL) {
ia_dereference(&tmp->ia, MDL);
}
ia_reference(&tmp->ia, reply->ia, MDL);
/* If we have anything to do on commit do it now */
if (tmp->on_star.on_commit != NULL) {
execute_statements(NULL, reply->packet,
NULL, NULL,
reply->packet->options,
reply->opt_state,
&tmp->scope,
tmp->on_star.on_commit,
&tmp->on_star);
executable_statement_dereference
(&tmp->on_star.on_commit, MDL);
}
#if defined (NSUPDATE)
/* Perform ddns updates */
oc = lookup_option(&server_universe, reply->opt_state,
SV_DDNS_UPDATES);
if ((oc == NULL) ||
evaluate_boolean_option_cache(NULL, reply->packet,
NULL, NULL,
reply->packet->options,
reply->opt_state,
&tmp->scope,
oc, MDL)) {
ddns_updates(reply->packet, NULL, NULL,
tmp, NULL, reply->opt_state);
}
#endif
if (!reuse_lease6(reply, tmp)) {
/* Commit 'hard' bindings. */
must_commit = 1;
renew_lease6(tmp->ipv6_pool, tmp);
schedule_lease_timeout(tmp->ipv6_pool);
/* Do our threshold check. */
check_pool6_threshold(reply, tmp);
}
}
/* write the IA_NA in wire-format to the outbound buffer */
write_to_packet(reply, ia_cursor);
/* Remove any old ia from the hash. */
if (reply->old_ia != NULL) {
if (!release_on_roam(reply)) {
ia_id = &reply->old_ia->iaid_duid;
ia_hash_delete(ia_na_active,
(unsigned char *)ia_id->data,
ia_id->len, MDL);
}
ia_dereference(&reply->old_ia, MDL);
}
/* Put new ia into the hash. */
reply->ia->cltt = cur_time;
ia_id = &reply->ia->iaid_duid;
ia_hash_add(ia_na_active, (unsigned char *)ia_id->data,
ia_id->len, reply->ia, MDL);
/* If we couldn't reuse all of the iasubopts, we
* must update udpate the lease db */
if (must_commit) {
write_ia(reply->ia);
}
} else {
/* write the IA_NA in wire-format to the outbound buffer */
write_to_packet(reply, ia_cursor);
schedule_lease_timeout_reply(reply);
}
cleanup:
if (packet_ia != NULL)
option_state_dereference(&packet_ia, MDL);
if (reply->reply_ia != NULL)
option_state_dereference(&reply->reply_ia, MDL);
if (ia_data.data != NULL)
data_string_forget(&ia_data, MDL);
if (data.data != NULL)
data_string_forget(&data, MDL);
if (reply->ia != NULL)
ia_dereference(&reply->ia, MDL);
if (reply->old_ia != NULL)
ia_dereference(&reply->old_ia, MDL);
if (reply->lease != NULL)
iasubopt_dereference(&reply->lease, MDL);
if (reply->fixed.data != NULL)
data_string_forget(&reply->fixed, MDL);
if (reply->subnet != NULL)
subnet_dereference(&reply->subnet, MDL);
if (reply->on_star.on_expiry != NULL)
executable_statement_dereference
(&reply->on_star.on_expiry, MDL);
if (reply->on_star.on_release != NULL)
executable_statement_dereference
(&reply->on_star.on_release, MDL);
/*
* ISC_R_CANCELED is a status code used by the addr processing to
* indicate we're replying with a status code. This is still a
* success at higher layers.
*/
return((status == ISC_R_CANCELED) ? ISC_R_SUCCESS : status);
}
/*
* Writes the populated IA_xx in wire format to the reply buffer
*/
void
write_to_packet(struct reply_state *reply, unsigned ia_cursor) {
reply->cursor += store_options6((char *)reply->buf.data + reply->cursor,
sizeof(reply->buf) - reply->cursor,
reply->reply_ia, reply->packet,
(reply->ia->ia_type != D6O_IA_PD ?
required_opts_IA : required_opts_IA_PD),
NULL);
/* Reset the length of this IA to match what was just written. */
putUShort(reply->buf.data + ia_cursor + 2,
reply->cursor - (ia_cursor + 4));
if (reply->ia->ia_type != D6O_IA_TA) {
/* Calculate T1/T2 and stuff them in the reply */
set_reply_tee_times(reply, ia_cursor);
}
}
/*
* Process an IAADDR within a given IA_xA, storing any IAADDR reply contents
* into the reply's current ia-scoped option cache. Returns ISC_R_CANCELED
* in the event we are replying with a status code and do not wish to process
* more IAADDRs within this IA.
*/
static isc_result_t
reply_process_addr(struct reply_state *reply, struct option_cache *addr) {
u_int32_t pref_life, valid_life;
struct binding_scope **scope;
struct group *group;
struct subnet *subnet;
struct iaddr tmp_addr;
struct option_cache *oc;
struct data_string iaaddr, data;
isc_result_t status = ISC_R_SUCCESS;
#ifdef EUI_64
int invalid_for_eui_64 = 0;
#endif
/* Initializes values that will be cleaned up. */
memset(&iaaddr, 0, sizeof(iaaddr));
memset(&data, 0, sizeof(data));
/* Note that reply->lease may be set by address_is_owned() */
/*
* There is no point trying to process an incoming address if there
* is no room for an outgoing address.
*/
if ((reply->cursor + 28) > sizeof(reply->buf)) {
log_error("reply_process_addr: Out of room for address.");
return ISC_R_NOSPACE;
}
/* Extract this IAADDR option. */
if (!evaluate_option_cache(&iaaddr, reply->packet, NULL, NULL,
reply->packet->options, NULL, &global_scope,
addr, MDL) ||
(iaaddr.len < IAADDR_OFFSET)) {
log_error("reply_process_addr: error evaluating IAADDR.");
status = ISC_R_FAILURE;
goto cleanup;
}
/* The first 16 bytes are the IPv6 address. */
pref_life = getULong(iaaddr.data + 16);
valid_life = getULong(iaaddr.data + 20);
if ((reply->client_valid == 0) ||
(reply->client_valid > valid_life))
reply->client_valid = valid_life;
if ((reply->client_prefer == 0) ||
(reply->client_prefer > pref_life))
reply->client_prefer = pref_life;
/*
* Clients may choose to send :: as an address, with the idea to give
* hints about preferred-lifetime or valid-lifetime.
*/
tmp_addr.len = 16;
memset(tmp_addr.iabuf, 0, 16);
if (!memcmp(iaaddr.data, tmp_addr.iabuf, 16)) {
/* Status remains success; we just ignore this one. */
goto cleanup;
}
/* tmp_addr len remains 16 */
memcpy(tmp_addr.iabuf, iaaddr.data, 16);
/*
* Verify that this address is on the client's network.
*/
for (subnet = reply->shared->subnets ; subnet != NULL ;
subnet = subnet->next_sibling) {
if (addr_eq(subnet_number(tmp_addr, subnet->netmask),
subnet->net))
break;
}
#ifdef EUI_64
if (subnet) {
/* If the requested address falls into an EUI-64 pool, then
* we need to verify if it has EUI-64 duid AND the requested
* address is correct for that duid. If not we treat it just
* like an not-on-link request. */
struct ipv6_pool* pool = NULL;
struct in6_addr* addr = (struct in6_addr*)(iaaddr.data);
if ((find_ipv6_pool(&pool, D6O_IA_NA, addr) == ISC_R_SUCCESS)
&& (pool->ipv6_pond->use_eui_64) &&
(!valid_for_eui_64_pool(pool, &reply->client_id, 0, addr))) {
log_debug ("Requested address: %s,"
" not valid for EUI-64 pool",
pin6_addr(addr));
invalid_for_eui_64 = 1;
}
}
#endif
/* Address not found on shared network. */
#ifdef EUI_64
if ((subnet == NULL) || invalid_for_eui_64) {
#else
if (subnet == NULL) {
#endif
/* Ignore this address on 'soft' bindings. */
if (reply->packet->dhcpv6_msg_type == DHCPV6_SOLICIT) {
/* disable rapid commit */
reply->buf.reply.msg_type = DHCPV6_ADVERTISE;
delete_option(&dhcpv6_universe,
reply->opt_state,
D6O_RAPID_COMMIT);
/* status remains success */
goto cleanup;
}
/*
* RFC3315 section 18.2.1:
*
* If the server finds that the prefix on one or more IP
* addresses in any IA in the message from the client is not
* appropriate for the link to which the client is connected,
* the server MUST return the IA to the client with a Status
* Code option with the value NotOnLink.
*/
if (reply->packet->dhcpv6_msg_type == DHCPV6_REQUEST) {
/* Rewind the IA_NA to empty. */
option_state_dereference(&reply->reply_ia, MDL);
if (!option_state_allocate(&reply->reply_ia, MDL)) {
log_error("reply_process_addr: No memory for "
"option state wipe.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
/* Append a NotOnLink status code. */
if (!set_status_code(STATUS_NotOnLink,
"Address not for use on this "
"link.", reply->reply_ia)) {
log_error("reply_process_addr: Failure "
"setting status code.");
status = ISC_R_FAILURE;
goto cleanup;
}
/* Fin (no more IAADDRs). */
status = ISC_R_CANCELED;
goto cleanup;
}
/*
* RFC3315 sections 18.2.3 and 18.2.4 have identical language:
*
* If the server finds that any of the addresses are not
* appropriate for the link to which the client is attached,
* the server returns the address to the client with lifetimes
* of 0.
*/
if ((reply->packet->dhcpv6_msg_type != DHCPV6_RENEW) &&
(reply->packet->dhcpv6_msg_type != DHCPV6_REBIND)) {
log_error("It is impossible to lease a client that is "
"not sending a solicit, request, renew, or "
"rebind.");
status = ISC_R_FAILURE;
goto cleanup;
}
reply->send_prefer = reply->send_valid = 0;
goto send_addr;
}
/* Verify the address belongs to the client. */
if (!address_is_owned(reply, &tmp_addr)) {
/*
* For solicit and request, any addresses included are
* 'requested' addresses. For rebind, we actually have
* no direction on what to do from 3315 section 18.2.4!
* So I think the best bet is to try and give it out, and if
* we can't, zero lifetimes.
*/
if ((reply->packet->dhcpv6_msg_type == DHCPV6_SOLICIT) ||
(reply->packet->dhcpv6_msg_type == DHCPV6_REQUEST) ||
(reply->packet->dhcpv6_msg_type == DHCPV6_REBIND)) {
status = reply_process_try_addr(reply, &tmp_addr);
/*
* If the address is in use, or isn't in any dynamic
* range, continue as normal. If any other error was
* found, error out.
*/
if ((status != ISC_R_SUCCESS) &&
(status != ISC_R_ADDRINUSE) &&
(status != ISC_R_ADDRNOTAVAIL))
goto cleanup;
/*
* If we didn't honor this lease, for solicit and
* request we simply omit it from our answer. For
* rebind, we send it with zeroed lifetimes.
*/
if (reply->lease == NULL) {
if (reply->packet->dhcpv6_msg_type ==
DHCPV6_REBIND) {
reply->send_prefer = 0;
reply->send_valid = 0;
goto send_addr;
}
/* status remains success - ignore */
goto cleanup;
}
/*
* RFC3315 section 18.2.3:
*
* If the server cannot find a client entry for the IA the
* server returns the IA containing no addresses with a Status
* Code option set to NoBinding in the Reply message.
*
* On mismatch we (ab)use this pretending we have not the IA
* as soon as we have not an address.
*/
} else if (reply->packet->dhcpv6_msg_type == DHCPV6_RENEW) {
/* Rewind the IA_NA to empty. */
option_state_dereference(&reply->reply_ia, MDL);
if (!option_state_allocate(&reply->reply_ia, MDL)) {
log_error("reply_process_addr: No memory for "
"option state wipe.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
/* Append a NoBinding status code. */
if (!set_status_code(STATUS_NoBinding,
"Address not bound to this "
"interface.", reply->reply_ia)) {
log_error("reply_process_addr: Unable to "
"attach status code.");
status = ISC_R_FAILURE;
goto cleanup;
}
/* Fin (no more IAADDRs). */
status = ISC_R_CANCELED;
goto cleanup;
} else {
log_error("It is impossible to lease a client that is "
"not sending a solicit, request, renew, or "
"rebind message.");
status = ISC_R_FAILURE;
goto cleanup;
}
}
if (reply->static_lease) {
if (reply->host == NULL)
log_fatal("Impossible condition at %s:%d.", MDL);
scope = &global_scope;
group = reply->subnet->group;
} else {
if (reply->lease == NULL)
log_fatal("Impossible condition at %s:%d.", MDL);
scope = &reply->lease->scope;
group = reply->lease->ipv6_pool->ipv6_pond->group;
}
/*
* If client_resources is nonzero, then the reply_process_is_addressed
* function has executed configuration state into the reply option
* cache. We will use that valid cache to derive configuration for
* whether or not to engage in additional addresses, and similar.
*/
if (reply->client_resources != 0) {
unsigned limit = 1;
/*
* Does this client have "enough" addresses already? Default
* to one. Everybody gets one, and one should be enough for
* anybody.
*/
oc = lookup_option(&server_universe, reply->opt_state,
SV_LIMIT_ADDRS_PER_IA);
if (oc != NULL) {
if (!evaluate_option_cache(&data, reply->packet,
NULL, NULL,
reply->packet->options,
reply->opt_state,
scope, oc, MDL) ||
(data.len != 4)) {
log_error("reply_process_addr: unable to "
"evaluate addrs-per-ia value.");
status = ISC_R_FAILURE;
goto cleanup;
}
limit = getULong(data.data);
data_string_forget(&data, MDL);
}
/*
* If we wish to limit the client to a certain number of
* addresses, then omit the address from the reply.
*/
if (reply->client_resources >= limit)
goto cleanup;
}
status = reply_process_is_addressed(reply, scope, group);
if (status != ISC_R_SUCCESS)
goto cleanup;
send_addr:
status = reply_process_send_addr(reply, &tmp_addr);
cleanup:
if (iaaddr.data != NULL)
data_string_forget(&iaaddr, MDL);
if (data.data != NULL)
data_string_forget(&data, MDL);
if (reply->lease != NULL)
iasubopt_dereference(&reply->lease, MDL);
return status;
}
/*
* Verify the address belongs to the client. If we've got a host
* record with a fixed address, it has to be the assigned address
* (fault out all else). Otherwise it's a dynamic address, so lookup
* that address and make sure it belongs to this DUID:IAID pair.
*/
static isc_boolean_t
address_is_owned(struct reply_state *reply, struct iaddr *addr) {
int i;
struct ipv6_pond *pond;
/*
* This faults out addresses that don't match fixed addresses.
*/
if (reply->static_lease) {
if (reply->fixed.data == NULL)
log_fatal("Impossible condition at %s:%d.", MDL);
if (memcmp(addr->iabuf, reply->fixed.data, 16) == 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
if ((reply->old_ia == NULL) || (reply->old_ia->num_iasubopt == 0))
return (ISC_FALSE);
for (i = 0 ; i < reply->old_ia->num_iasubopt ; i++) {
struct iasubopt *tmp;
tmp = reply->old_ia->iasubopt[i];
if (memcmp(addr->iabuf, &tmp->addr, 16) == 0) {
if (lease6_usable(tmp) == ISC_FALSE) {
return (ISC_FALSE);
}
pond = tmp->ipv6_pool->ipv6_pond;
if (((pond->prohibit_list != NULL) &&
(permitted(reply->packet, pond->prohibit_list))) ||
((pond->permit_list != NULL) &&
(!permitted(reply->packet, pond->permit_list))))
return (ISC_FALSE);
iasubopt_reference(&reply->lease, tmp, MDL);
return (ISC_TRUE);
}
}
return (ISC_FALSE);
}
/* Process a client-supplied IA_TA. This may append options to the tail of
* the reply packet being built in the reply_state structure.
*/
static isc_result_t
reply_process_ia_ta(struct reply_state *reply, struct option_cache *ia) {
isc_result_t status = ISC_R_SUCCESS;
u_int32_t iaid;
unsigned ia_cursor;
struct option_state *packet_ia;
struct option_cache *oc;
struct data_string ia_data, data;
struct data_string iaaddr;
u_int32_t pref_life, valid_life;
struct iaddr tmp_addr;
/* Initialize values that will get cleaned up on return. */
packet_ia = NULL;
memset(&ia_data, 0, sizeof(ia_data));
memset(&data, 0, sizeof(data));
memset(&iaaddr, 0, sizeof(iaaddr));
/* Make sure there is at least room for the header. */
if ((reply->cursor + IA_TA_OFFSET + 4) > sizeof(reply->buf)) {
log_error("reply_process_ia_ta: Reply too long for IA.");
return ISC_R_NOSPACE;
}
/* Fetch the IA_TA contents. */
if (!get_encapsulated_IA_state(&packet_ia, &ia_data, reply->packet,
ia, IA_TA_OFFSET)) {
log_error("reply_process_ia_ta: error evaluating ia");
status = ISC_R_FAILURE;
goto cleanup;
}
/* Extract IA_TA header contents. */
iaid = getULong(ia_data.data);
/* Create an IA_TA structure. */
if (ia_allocate(&reply->ia, iaid, (char *)reply->client_id.data,
reply->client_id.len, MDL) != ISC_R_SUCCESS) {
log_error("reply_process_ia_ta: no memory for ia.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
reply->ia->ia_type = D6O_IA_TA;
/* Cache pre-existing IA, if any. */
ia_hash_lookup(&reply->old_ia, ia_ta_active,
(unsigned char *)reply->ia->iaid_duid.data,
reply->ia->iaid_duid.len, MDL);
/*
* Create an option cache to carry the IA_TA option contents, and
* execute any user-supplied values into it.
*/
if (!option_state_allocate(&reply->reply_ia, MDL)) {
status = ISC_R_NOMEMORY;
goto cleanup;
}
/*
* Temporary leases are dynamic by definition.
*/
reply->static_lease = ISC_FALSE;
/*
* Save the cursor position at the start of the IA, so we can
* set length later. We write a temporary
* header out now just in case we decide to adjust the packet
* within sub-process functions.
*/
ia_cursor = reply->cursor;
/* Initialize the IA_TA header. First the code. */
putUShort(reply->buf.data + reply->cursor, (unsigned)D6O_IA_TA);
reply->cursor += 2;
/* Then option length. */
putUShort(reply->buf.data + reply->cursor, 0x04u);
reply->cursor += 2;
/* Then IA_TA header contents; IAID. */
putULong(reply->buf.data + reply->cursor, iaid);
reply->cursor += 4;
/*
* Deal with an IAADDR for lifetimes.
* For all or none, process IAADDRs as hints.
*/
reply->min_valid = reply->min_prefer = INFINITE_TIME;
reply->client_valid = reply->client_prefer = 0;
oc = lookup_option(&dhcpv6_universe, packet_ia, D6O_IAADDR);
for (; oc != NULL; oc = oc->next) {
memset(&iaaddr, 0, sizeof(iaaddr));
if (!evaluate_option_cache(&iaaddr, reply->packet,
NULL, NULL,
reply->packet->options, NULL,
&global_scope, oc, MDL) ||
(iaaddr.len < IAADDR_OFFSET)) {
log_error("reply_process_ia_ta: error "
"evaluating IAADDR.");
status = ISC_R_FAILURE;
goto cleanup;
}
/* The first 16 bytes are the IPv6 address. */
pref_life = getULong(iaaddr.data + 16);
valid_life = getULong(iaaddr.data + 20);
if ((reply->client_valid == 0) ||
(reply->client_valid > valid_life))
reply->client_valid = valid_life;
if ((reply->client_prefer == 0) ||
(reply->client_prefer > pref_life))
reply->client_prefer = pref_life;
/* Nothing more if something has failed. */
if (status == ISC_R_CANCELED)
continue;
tmp_addr.len = 16;
memcpy(tmp_addr.iabuf, iaaddr.data, 16);
if (!temporary_is_available(reply, &tmp_addr))
goto bad_temp;
status = reply_process_is_addressed(reply,
&reply->lease->scope,
reply->lease->ipv6_pool->ipv6_pond->group);
if (status != ISC_R_SUCCESS)
goto bad_temp;
status = reply_process_send_addr(reply, &tmp_addr);
if (status != ISC_R_SUCCESS)
goto bad_temp;
if (reply->lease != NULL)
iasubopt_dereference(&reply->lease, MDL);
continue;
bad_temp:
/* Rewind the IA_TA to empty. */
option_state_dereference(&reply->reply_ia, MDL);
if (!option_state_allocate(&reply->reply_ia, MDL)) {
status = ISC_R_NOMEMORY;
goto cleanup;
}
status = ISC_R_CANCELED;
reply->client_resources = 0;
reply->resources_included = ISC_FALSE;
if (reply->lease != NULL)
iasubopt_dereference(&reply->lease, MDL);
}
reply->ia_count++;
/*
* Give the client temporary addresses.
*/
if (reply->client_resources != 0)
goto store;
status = find_client_temporaries(reply);
if (status == ISC_R_NORESOURCES) {
switch (reply->packet->dhcpv6_msg_type) {
case DHCPV6_SOLICIT:
/*
* No address for any IA is handled
* by the caller.
*/
/* FALL THROUGH */
case DHCPV6_REQUEST:
/* Section 18.2.1 (Request):
*
* If the server cannot assign any addresses to
* an IA in the message from the client, the
* server MUST include the IA in the Reply
* message with no addresses in the IA and a
* Status Code option in the IA containing
* status code NoAddrsAvail.
*/
option_state_dereference(&reply->reply_ia, MDL);
if (!option_state_allocate(&reply->reply_ia, MDL)) {
log_error("reply_process_ia_ta: No "
"memory for option state wipe.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
if (!set_status_code(STATUS_NoAddrsAvail,
"No addresses available "
"for this interface.",
reply->reply_ia)) {
log_error("reply_process_ia_ta: Unable "
"to set NoAddrsAvail status code.");
status = ISC_R_FAILURE;
goto cleanup;
}
status = ISC_R_SUCCESS;
break;
default:
/*
* We don't want to include the IA if we
* provide zero addresses including zeroed
* lifetimes.
*/
if (reply->resources_included)
status = ISC_R_SUCCESS;
else
goto cleanup;
break;
}
} else if (status != ISC_R_SUCCESS)
goto cleanup;
store:
/*
* yes, goto's aren't the best but we also want to avoid extra
* indents
*/
if (status == ISC_R_CANCELED) {
/* We're replying with a status code so we still need to
* write it out in wire-format to the outbound buffer */
write_to_packet(reply, ia_cursor);
goto cleanup;
}
/*
* If we have any addresses log what we are doing.
*/
if (reply->ia->num_iasubopt != 0) {
struct iasubopt *tmp;
int i;
char tmp_addr[INET6_ADDRSTRLEN];
for (i = 0 ; i < reply->ia->num_iasubopt ; i++) {
tmp = reply->ia->iasubopt[i];
log_info("%s TA: address %s to client with duid %s "
"iaid = %d valid for %u seconds",
dhcpv6_type_names[reply->buf.reply.msg_type],
inet_ntop(AF_INET6, &tmp->addr,
tmp_addr, sizeof(tmp_addr)),
print_hex_1(reply->client_id.len,
reply->client_id.data, 60),
iaid,
tmp->valid);
}
}
/*
* For hard bindings we consume the new changes into
* the database (if any have been attached to the ia_ta).
*
* Loop through the assigned dynamic addresses, referencing the
* leases onto this IA_TA rather than any old ones, and updating
* pool timers for each (if any).
*/
if ((reply->ia->num_iasubopt != 0) &&
(reply->buf.reply.msg_type == DHCPV6_REPLY)) {
int must_commit = 0;
struct iasubopt *tmp;
struct data_string *ia_id;
int i;
for (i = 0 ; i < reply->ia->num_iasubopt ; i++) {
tmp = reply->ia->iasubopt[i];
if (tmp->ia != NULL)
ia_dereference(&tmp->ia, MDL);
ia_reference(&tmp->ia, reply->ia, MDL);
/* If we have anything to do on commit do it now */
if (tmp->on_star.on_commit != NULL) {
execute_statements(NULL, reply->packet,
NULL, NULL,
reply->packet->options,
reply->opt_state,
&tmp->scope,
tmp->on_star.on_commit,
&tmp->on_star);
executable_statement_dereference
(&tmp->on_star.on_commit, MDL);
}
#if defined (NSUPDATE)
/*
* Perform ddns updates.
*/
oc = lookup_option(&server_universe, reply->opt_state,
SV_DDNS_UPDATES);
if ((oc == NULL) ||
evaluate_boolean_option_cache(NULL, reply->packet,
NULL, NULL,
reply->packet->options,
reply->opt_state,
&tmp->scope,
oc, MDL)) {
ddns_updates(reply->packet, NULL, NULL,
tmp, NULL, reply->opt_state);
}
#endif
if (!reuse_lease6(reply, tmp)) {
/* Commit 'hard' bindings. */
must_commit = 1;
renew_lease6(tmp->ipv6_pool, tmp);
schedule_lease_timeout(tmp->ipv6_pool);
/* Do our threshold check. */
check_pool6_threshold(reply, tmp);
}
}
/* write the IA_TA in wire-format to the outbound buffer */
write_to_packet(reply, ia_cursor);
/* Remove any old ia from the hash. */
if (reply->old_ia != NULL) {
if (!release_on_roam(reply)) {
ia_id = &reply->old_ia->iaid_duid;
ia_hash_delete(ia_ta_active,
(unsigned char *)ia_id->data,
ia_id->len, MDL);
}
ia_dereference(&reply->old_ia, MDL);
}
/* Put new ia into the hash. */
reply->ia->cltt = cur_time;
ia_id = &reply->ia->iaid_duid;
ia_hash_add(ia_ta_active, (unsigned char *)ia_id->data,
ia_id->len, reply->ia, MDL);
/* If we couldn't reuse all of the iasubopts, we
* must update udpate the lease db */
if (must_commit) {
write_ia(reply->ia);
}
} else {
/* write the IA_TA in wire-format to the outbound buffer */
write_to_packet(reply, ia_cursor);
schedule_lease_timeout_reply(reply);
}
cleanup:
if (packet_ia != NULL)
option_state_dereference(&packet_ia, MDL);
if (iaaddr.data != NULL)
data_string_forget(&iaaddr, MDL);
if (reply->reply_ia != NULL)
option_state_dereference(&reply->reply_ia, MDL);
if (ia_data.data != NULL)
data_string_forget(&ia_data, MDL);
if (data.data != NULL)
data_string_forget(&data, MDL);
if (reply->ia != NULL)
ia_dereference(&reply->ia, MDL);
if (reply->old_ia != NULL)
ia_dereference(&reply->old_ia, MDL);
if (reply->lease != NULL)
iasubopt_dereference(&reply->lease, MDL);
/*
* ISC_R_CANCELED is a status code used by the addr processing to
* indicate we're replying with other addresses. This is still a
* success at higher layers.
*/
return((status == ISC_R_CANCELED) ? ISC_R_SUCCESS : status);
}
/*
* Determines if a lease (iasubopt) can be reused without extending it.
* If dhcp-cache-threshold is greater than zero (i.e enabled) then
* a lease may be reused without going through a full renewal if
* it meets all the requirements. In short it must be active, younger
* than the threshold, and not have DNS changes.
*
* If it is determined that it can be reused, that a call to
* shorten_lifetimes() is made to reduce the valid and preferred lifetimes
* sent to the client by the age of the lease.
*
* Returns 1 if lease can be reused, 0 otherwise
*/
int
reuse_lease6(struct reply_state *reply, struct iasubopt *lease) {
int threshold = DEFAULT_CACHE_THRESHOLD;
struct option_cache* oc = NULL;
struct data_string d1;
time_t age;
time_t limit;
int reuse_it = 0;
/* In order to even qualify for reuse consideration:
* 1. Lease must be active
* 2. It must have been accepted at least once
* 3. DNS info must not have changed */
if ((lease->state != FTS_ACTIVE) ||
(lease->hard_lifetime_end_time == 0) ||
(lease->ddns_cb != NULL)) {
return (0);
}
/* Look up threshold value */
memset(&d1, 0, sizeof(struct data_string));
oc = lookup_option(&server_universe, reply->opt_state,
SV_CACHE_THRESHOLD);
if (oc &&
evaluate_option_cache(&d1, reply->packet, NULL, NULL,
reply->packet->options, reply->opt_state,
&lease->scope, oc, MDL)) {
if (d1.len == 1 && (d1.data[0] < 100)) {
threshold = d1.data[0];
}
data_string_forget(&d1, MDL);
}
if (threshold <= 0) {
return (0);
}
if (lease->valid >= MAX_TIME) {
/* Infinite leases are always reused. We have to make
* a choice because we cannot determine when they actually
* began, so we either always reuse them or we never do. */
log_debug ("reusing infinite lease for: %s%s",
pin6_addr(&lease->addr), iasubopt_plen_str(lease));
return (1);
}
age = cur_tv.tv_sec - (lease->hard_lifetime_end_time - lease->valid);
if (lease->valid <= (INT_MAX / threshold))
limit = lease->valid * threshold / 100;
else
limit = lease->valid / 100 * threshold;
if (age < limit) {
/* Reduce valid/preferred going to the client by age */
shorten_lifetimes(reply, lease, age, threshold);
reuse_it = 1;
}
return (reuse_it);
}
/*
* Reduces the valid and preferred lifetimes for a given lease (iasubopt)
*
* We cannot determine until after a iasubopt has been added to
* the reply if the lease can be reused. Therefore, when we do reuse a
* lease we need a way to alter the lifetimes that will be sent to the client.
* That's where this function comes in handy:
*
* Locate the iasubopt by it's address within the reply the reduce both
* the preferred and valid lifetimes by the given number of seconds.
*
* Note that this function, by necessity, works directly with the
* option_cache data. Sort of a no-no but I don't have any better ideas.
*/
void shorten_lifetimes(struct reply_state *reply, struct iasubopt *lease,
time_t age, int threshold) {
struct option_cache* oc = NULL;
int subopt_type;
int addr_offset;
int pref_offset;
int val_offset;
int exp_length;
if (reply->ia->ia_type != D6O_IA_PD) {
subopt_type = D6O_IAADDR;
addr_offset = IASUBOPT_NA_ADDR_OFFSET;
pref_offset = IASUBOPT_NA_PREF_OFFSET;
val_offset = IASUBOPT_NA_VALID_OFFSET;
exp_length = IASUBOPT_NA_LEN;
}
else {
subopt_type = D6O_IAPREFIX;
addr_offset = IASUBOPT_PD_PREFIX_OFFSET;
pref_offset = IASUBOPT_PD_PREF_OFFSET;
val_offset = IASUBOPT_PD_VALID_OFFSET;
exp_length = IASUBOPT_PD_LEN;
}
// loop through the iasubopts for the one that matches this lease
oc = lookup_option(&dhcpv6_universe, reply->reply_ia, subopt_type);
for (; oc != NULL ; oc = oc->next) {
if (oc->data.data == NULL || oc->data.len != exp_length) {
/* shouldn't happen */
continue;
}
/* If address matches (and for PDs the prefix len matches)
* we assume this is our subopt, so update the lifetimes */
if (!memcmp(oc->data.data + addr_offset, &lease->addr, 16) &&
(subopt_type != D6O_IA_PD ||
(oc->data.data[IASUBOPT_PD_PREFLEN_OFFSET] ==
lease->plen))) {
u_int32_t pref_life = getULong(oc->data.data +
pref_offset);
u_int32_t valid_life = getULong(oc->data.data +
val_offset);
if (pref_life < MAX_TIME && pref_life > age) {
pref_life -= age;
putULong((unsigned char*)(oc->data.data) +
pref_offset, pref_life);
if (reply->min_prefer > pref_life) {
reply->min_prefer = pref_life;
}
}
if (valid_life < MAX_TIME && valid_life > age) {
valid_life -= age;
putULong((unsigned char*)(oc->data.data) +
val_offset, valid_life);
if (reply->min_valid > reply->send_valid) {
reply->min_valid = valid_life;
}
}
log_debug ("Reusing lease for: %s%s, "
"age %ld secs < %d%%,"
" sending shortened lifetimes -"
" preferred: %u, valid %u",
pin6_addr(&lease->addr),
iasubopt_plen_str(lease),
age, threshold,
pref_life, valid_life);
break;
}
}
}
/*
* Verify the temporary address is available.
*/
static isc_boolean_t
temporary_is_available(struct reply_state *reply, struct iaddr *addr) {
struct in6_addr tmp_addr;
struct subnet *subnet;
struct ipv6_pool *pool = NULL;
struct ipv6_pond *pond = NULL;
int i;
memcpy(&tmp_addr, addr->iabuf, sizeof(tmp_addr));
/*
* Clients may choose to send :: as an address, with the idea to give
* hints about preferred-lifetime or valid-lifetime.
* So this is not a request for this address.
*/
if (IN6_IS_ADDR_UNSPECIFIED(&tmp_addr))
return ISC_FALSE;
/*
* Verify that this address is on the client's network.
*/
for (subnet = reply->shared->subnets ; subnet != NULL ;
subnet = subnet->next_sibling) {
if (addr_eq(subnet_number(*addr, subnet->netmask),
subnet->net))
break;
}
/* Address not found on shared network. */
if (subnet == NULL)
return ISC_FALSE;
/*
* Check if this address is owned (must be before next step).
*/
if (address_is_owned(reply, addr))
return ISC_TRUE;
/*
* Verify that this address is in a temporary pool and try to get it.
*/
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
if (((pond->prohibit_list != NULL) &&
(permitted(reply->packet, pond->prohibit_list))) ||
((pond->permit_list != NULL) &&
(!permitted(reply->packet, pond->permit_list))))
continue;
for (i = 0 ; (pool = pond->ipv6_pools[i]) != NULL ; i++) {
if (pool->pool_type != D6O_IA_TA)
continue;
if (ipv6_in_pool(&tmp_addr, pool))
break;
}
if (pool != NULL)
break;
}
if (pool == NULL)
return ISC_FALSE;
if (lease6_exists(pool, &tmp_addr))
return ISC_FALSE;
if (iasubopt_allocate(&reply->lease, MDL) != ISC_R_SUCCESS)
return ISC_FALSE;
reply->lease->addr = tmp_addr;
reply->lease->plen = 0;
/* Default is soft binding for 2 minutes. */
if (add_lease6(pool, reply->lease, cur_time + 120) != ISC_R_SUCCESS)
return ISC_FALSE;
return ISC_TRUE;
}
/*
* Get a temporary address per prefix.
*/
static isc_result_t
find_client_temporaries(struct reply_state *reply) {
int i;
struct ipv6_pool *p = NULL;
struct ipv6_pond *pond;
isc_result_t status = ISC_R_NORESOURCES;;
unsigned int attempts;
struct iaddr send_addr;
/*
* Do a quick walk through of the ponds and pools
* to see if we have any prefix pools
*/
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
if (pond->ipv6_pools == NULL)
continue;
for (i = 0; (p = pond->ipv6_pools[i]) != NULL; i++) {
if (p->pool_type == D6O_IA_TA)
break;
}
if (p != NULL)
break;
}
/* If we get here and p is NULL we have no useful pools */
if (p == NULL) {
log_debug("Unable to get client addresses: "
"no IPv6 pools on this shared network");
return ISC_R_NORESOURCES;
}
/*
* We have at least one pool that could provide an address
* Now we walk through the ponds and pools again and check
* to see if the client is permitted and if an address is
* available
*/
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
if (((pond->prohibit_list != NULL) &&
(permitted(reply->packet, pond->prohibit_list))) ||
((pond->permit_list != NULL) &&
(!permitted(reply->packet, pond->permit_list))))
continue;
for (i = 0; (p = pond->ipv6_pools[i]) != NULL; i++) {
if (p->pool_type != D6O_IA_TA) {
continue;
}
/*
* Get an address in this temporary pool.
*/
status = create_lease6(p, &reply->lease, &attempts,
&reply->client_id,
cur_time + 120);
if (status != ISC_R_SUCCESS) {
log_debug("Unable to get a temporary address.");
goto cleanup;
}
status = reply_process_is_addressed(reply,
&reply->lease->scope,
pond->group);
if (status != ISC_R_SUCCESS) {
goto cleanup;
}
send_addr.len = 16;
memcpy(send_addr.iabuf, &reply->lease->addr, 16);
status = reply_process_send_addr(reply, &send_addr);
if (status != ISC_R_SUCCESS) {
goto cleanup;
}
/*
* reply->lease can't be null as we use it above
* add check if that changes
*/
iasubopt_dereference(&reply->lease, MDL);
}
}
cleanup:
if (reply->lease != NULL) {
iasubopt_dereference(&reply->lease, MDL);
}
return status;
}
/*
* This function only returns failure on 'hard' failures. If it succeeds,
* it will leave a lease structure behind.
*/
static isc_result_t
reply_process_try_addr(struct reply_state *reply, struct iaddr *addr) {
isc_result_t status = ISC_R_ADDRNOTAVAIL;
struct ipv6_pool *pool = NULL;
struct ipv6_pond *pond = NULL;
int i;
struct data_string data_addr;
if ((reply == NULL) || (reply->shared == NULL) ||
(addr == NULL) || (reply->lease != NULL))
return (DHCP_R_INVALIDARG);
/*
* Do a quick walk through of the ponds and pools
* to see if we have any NA address pools
*/
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
if (pond->ipv6_pools == NULL)
continue;
for (i = 0; ; i++) {
pool = pond->ipv6_pools[i];
if ((pool == NULL) ||
(pool->pool_type == D6O_IA_NA))
break;
}
if (pool != NULL)
break;
}
/* If we get here and p is NULL we have no useful pools */
if (pool == NULL) {
return (ISC_R_ADDRNOTAVAIL);
}
memset(&data_addr, 0, sizeof(data_addr));
data_addr.len = addr->len;
data_addr.data = addr->iabuf;
/*
* We have at least one pool that could provide an address
* Now we walk through the ponds and pools again and check
* to see if the client is permitted and if an address is
* available
*
* Within a given pond we start looking at the last pool we
* allocated from, unless it had a collision trying to allocate
* an address. This will tend to move us into less-filled pools.
*/
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
if (((pond->prohibit_list != NULL) &&
(permitted(reply->packet, pond->prohibit_list))) ||
((pond->permit_list != NULL) &&
(!permitted(reply->packet, pond->permit_list))))
continue;
for (i = 0 ; (pool = pond->ipv6_pools[i]) != NULL ; i++) {
if (pool->pool_type != D6O_IA_NA)
continue;
status = try_client_v6_address(&reply->lease, pool,
&data_addr);
if (status == ISC_R_SUCCESS)
break;
}
if (status == ISC_R_SUCCESS)
break;
}
/* Note that this is just pedantry. There is no allocation to free. */
data_string_forget(&data_addr, MDL);
/* Return just the most recent status... */
return (status);
}
/* Look around for an address to give the client. First, look through the
* old IA for addresses we can extend. Second, try to allocate a new address.
* Finally, actually add that address into the current reply IA.
*/
static isc_result_t
find_client_address(struct reply_state *reply) {
struct iaddr send_addr;
isc_result_t status = ISC_R_NORESOURCES;
struct iasubopt *lease, *best_lease = NULL;
struct binding_scope **scope;
struct group *group;
int i;
if (reply->static_lease) {
if (reply->host == NULL)
return DHCP_R_INVALIDARG;
send_addr.len = 16;
memcpy(send_addr.iabuf, reply->fixed.data, 16);
scope = &global_scope;
group = reply->subnet->group;
goto send_addr;
}
if (reply->old_ia != NULL) {
for (i = 0 ; i < reply->old_ia->num_iasubopt ; i++) {
struct shared_network *candidate_shared;
struct ipv6_pond *pond;
lease = reply->old_ia->iasubopt[i];
candidate_shared = lease->ipv6_pool->shared_network;
pond = lease->ipv6_pool->ipv6_pond;
/*
* Look for the best lease on the client's shared
* network, that is still permitted
*/
if ((candidate_shared != reply->shared) ||
(lease6_usable(lease) != ISC_TRUE))
continue;
if (((pond->prohibit_list != NULL) &&
(permitted(reply->packet, pond->prohibit_list))) ||
((pond->permit_list != NULL) &&
(!permitted(reply->packet, pond->permit_list))))
continue;
best_lease = lease_compare(lease, best_lease);
}
}
/* Try to pick a new address if we didn't find one, or if we found an
* abandoned lease.
*/
if ((best_lease == NULL) || (best_lease->state == FTS_ABANDONED)) {
status = pick_v6_address(reply);
} else if (best_lease != NULL) {
iasubopt_reference(&reply->lease, best_lease, MDL);
status = ISC_R_SUCCESS;
}
/* Pick the abandoned lease as a last resort. */
if ((status == ISC_R_NORESOURCES) && (best_lease != NULL)) {
/* I don't see how this is supposed to be done right now. */
log_error("Best match for DUID %s is an abandoned address,"
" This may be a result of multiple clients attempting"
" to use this DUID",
print_hex_1(reply->client_id.len,
reply->client_id.data, 60));
/* iasubopt_reference(&reply->lease, best_lease, MDL); */
}
/* Give up now if we didn't find a lease. */
if (status != ISC_R_SUCCESS)
return status;
if (reply->lease == NULL)
log_fatal("Impossible condition at %s:%d.", MDL);
/* Draw binding scopes from the lease's binding scope, and config
* from the lease's containing subnet and higher. Note that it may
* be desirable to place the group attachment directly in the pool.
*/
scope = &reply->lease->scope;
group = reply->lease->ipv6_pool->ipv6_pond->group;
send_addr.len = 16;
memcpy(send_addr.iabuf, &reply->lease->addr, 16);
send_addr:
status = reply_process_is_addressed(reply, scope, group);
if (status != ISC_R_SUCCESS)
return status;
status = reply_process_send_addr(reply, &send_addr);
return status;
}
/* Once an address is found for a client, perform several common functions;
* Calculate and store valid and preferred lease times, draw client options
* into the option state.
*/
static isc_result_t
reply_process_is_addressed(struct reply_state *reply,
struct binding_scope **scope, struct group *group)
{
isc_result_t status = ISC_R_SUCCESS;
struct data_string data;
struct option_cache *oc;
struct option_state *tmp_options = NULL;
struct on_star *on_star;
int i;
/* Initialize values we will cleanup. */
memset(&data, 0, sizeof(data));
/*
* Find the proper on_star block to use. We use the
* one in the lease if we have a lease or the one in
* the reply if we don't have a lease because this is
* a static instance
*/
if (reply->lease) {
on_star = &reply->lease->on_star;
} else {
on_star = &reply->on_star;
}
/*
* Bring in the root configuration. We only do this to bring
* in the on * statements, as we didn't have the lease available
* we did it the first time.
*/
option_state_allocate(&tmp_options, MDL);
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options, tmp_options,
&global_scope, root_group, NULL,
on_star);
if (tmp_options != NULL) {
option_state_dereference(&tmp_options, MDL);
}
/*
* Bring configured options into the root packet level cache - start
* with the lease's closest enclosing group (passed in by the caller
* as 'group').
*/
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options, reply->opt_state,
scope, group, root_group, on_star);
/* Execute statements from class scopes. */
for (i = reply->packet->class_count; i > 0; i--) {
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state, scope,
reply->packet->classes[i - 1]->group,
group, on_star);
}
/*
* If there is a host record, over-ride with values configured there,
* without re-evaluating configuration from the previously executed
* group or its common enclosers.
*/
if (reply->host != NULL)
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state, scope,
reply->host->group, group,
on_star);
/* Determine valid lifetime. */
if (reply->client_valid == 0)
reply->send_valid = DEFAULT_DEFAULT_LEASE_TIME;
else
reply->send_valid = reply->client_valid;
oc = lookup_option(&server_universe, reply->opt_state,
SV_DEFAULT_LEASE_TIME);
if (oc != NULL) {
if (!evaluate_option_cache(&data, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state,
scope, oc, MDL) ||
(data.len != 4)) {
log_error("reply_process_is_addressed: unable to "
"evaluate default lease time");
status = ISC_R_FAILURE;
goto cleanup;
}
reply->send_valid = getULong(data.data);
data_string_forget(&data, MDL);
}
/* Check to see if the lease time would cause us to wrap
* in which case we make it infinite.
* The following doesn't work on at least some systems:
* (cur_time + reply->send_valid < cur_time)
*/
if (reply->send_valid != INFINITE_TIME) {
time_t test_time = cur_time + reply->send_valid;
if (test_time < cur_time)
reply->send_valid = INFINITE_TIME;
}
if (reply->client_prefer == 0)
reply->send_prefer = reply->send_valid;
else
reply->send_prefer = reply->client_prefer;
if ((reply->send_prefer >= reply->send_valid) &&
(reply->send_valid != INFINITE_TIME))
reply->send_prefer = (reply->send_valid / 2) +
(reply->send_valid / 8);
oc = lookup_option(&server_universe, reply->opt_state,
SV_PREFER_LIFETIME);
if (oc != NULL) {
if (!evaluate_option_cache(&data, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state,
scope, oc, MDL) ||
(data.len != 4)) {
log_error("reply_process_is_addressed: unable to "
"evaluate preferred lease time");
status = ISC_R_FAILURE;
goto cleanup;
}
reply->send_prefer = getULong(data.data);
data_string_forget(&data, MDL);
}
/* Note lowest values for later calculation of renew/rebind times. */
if (reply->min_prefer > reply->send_prefer)
reply->min_prefer = reply->send_prefer;
if (reply->min_valid > reply->send_valid)
reply->min_valid = reply->send_valid;
#if 0
/*
* XXX: Old 4.0.0 alpha code would change the host {} record
* XXX: uid upon lease assignment. This was intended to cover the
* XXX: case where a client first identifies itself using vendor
* XXX: options in a solicit, or request, but later neglects to include
* XXX: these options in a Renew or Rebind. It is not clear that this
* XXX: is required, and has some startling ramifications (such as
* XXX: how to recover this dynamic host {} state across restarts).
*/
if (reply->host != NULL)
change_host_uid(host, reply->client_id->data,
reply->client_id->len);
#endif /* 0 */
/* Perform dynamic lease related update work. */
if (reply->lease != NULL) {
/* Cached lifetimes */
reply->lease->prefer = reply->send_prefer;
reply->lease->valid = reply->send_valid;
/* Advance (or rewind) the valid lifetime.
* In the protocol 0xFFFFFFFF is infinite
* when connecting to the lease file MAX_TIME is
*/
if (reply->buf.reply.msg_type == DHCPV6_REPLY) {
if (reply->send_valid == INFINITE_TIME) {
reply->lease->soft_lifetime_end_time = MAX_TIME;
} else {
reply->lease->soft_lifetime_end_time =
cur_time + reply->send_valid;
}
/* Wait before renew! */
}
status = ia_add_iasubopt(reply->ia, reply->lease, MDL);
if (status != ISC_R_SUCCESS) {
log_fatal("reply_process_is_addressed: Unable to "
"attach lease to new IA: %s",
isc_result_totext(status));
}
/*
* If this is a new lease, make sure it is attached somewhere.
*/
if (reply->lease->ia == NULL) {
ia_reference(&reply->lease->ia, reply->ia, MDL);
}
}
/* Bring a copy of the relevant options into the IA scope. */
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options, reply->reply_ia,
scope, group, root_group, NULL);
/* Execute statements from class scopes. */
for (i = reply->packet->class_count; i > 0; i--) {
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options,
reply->reply_ia, scope,
reply->packet->classes[i - 1]->group,
group, NULL);
}
/*
* And bring in host record configuration, if any, but not to overlap
* the previous group or its common enclosers.
*/
if (reply->host != NULL)
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options,
reply->reply_ia, scope,
reply->host->group, group, NULL);
cleanup:
if (data.data != NULL)
data_string_forget(&data, MDL);
if (status == ISC_R_SUCCESS)
reply->client_resources++;
return status;
}
/* Simply send an IAADDR within the IA scope as described. */
static isc_result_t
reply_process_send_addr(struct reply_state *reply, struct iaddr *addr) {
isc_result_t status = ISC_R_SUCCESS;
struct data_string data;
memset(&data, 0, sizeof(data));
/* Now append the lease. */
data.len = IAADDR_OFFSET;
if (!buffer_allocate(&data.buffer, data.len, MDL)) {
log_error("reply_process_send_addr: out of memory"
"allocating new IAADDR buffer.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
data.data = data.buffer->data;
memcpy(data.buffer->data, addr->iabuf, 16);
putULong(data.buffer->data + 16, reply->send_prefer);
putULong(data.buffer->data + 20, reply->send_valid);
if (!append_option_buffer(&dhcpv6_universe, reply->reply_ia,
data.buffer, data.buffer->data,
data.len, D6O_IAADDR, 0)) {
log_error("reply_process_send_addr: unable "
"to save IAADDR option");
status = ISC_R_FAILURE;
goto cleanup;
}
reply->resources_included = ISC_TRUE;
cleanup:
if (data.data != NULL)
data_string_forget(&data, MDL);
return status;
}
/* Choose the better of two leases. */
static struct iasubopt *
lease_compare(struct iasubopt *alpha, struct iasubopt *beta) {
if (alpha == NULL)
return beta;
if (beta == NULL)
return alpha;
switch(alpha->state) {
case FTS_ACTIVE:
switch(beta->state) {
case FTS_ACTIVE:
/* Choose the lease with the longest lifetime (most
* likely the most recently allocated).
*/
if (alpha->hard_lifetime_end_time <
beta->hard_lifetime_end_time)
return beta;
else
return alpha;
case FTS_EXPIRED:
case FTS_ABANDONED:
return alpha;
default:
log_fatal("Impossible condition at %s:%d.", MDL);
}
break;
case FTS_EXPIRED:
switch (beta->state) {
case FTS_ACTIVE:
return beta;
case FTS_EXPIRED:
/* Choose the most recently expired lease. */
if (alpha->hard_lifetime_end_time <
beta->hard_lifetime_end_time)
return beta;
else if ((alpha->hard_lifetime_end_time ==
beta->hard_lifetime_end_time) &&
(alpha->soft_lifetime_end_time <
beta->soft_lifetime_end_time))
return beta;
else
return alpha;
case FTS_ABANDONED:
return alpha;
default:
log_fatal("Impossible condition at %s:%d.", MDL);
}
break;
case FTS_ABANDONED:
switch (beta->state) {
case FTS_ACTIVE:
case FTS_EXPIRED:
return alpha;
case FTS_ABANDONED:
/* Choose the lease that was abandoned longest ago. */
if (alpha->hard_lifetime_end_time <
beta->hard_lifetime_end_time)
return alpha;
else
return beta;
default:
log_fatal("Impossible condition at %s:%d.", MDL);
}
break;
default:
log_fatal("Impossible condition at %s:%d.", MDL);
}
log_fatal("Triple impossible condition at %s:%d.", MDL);
return NULL;
}
/* Process a client-supplied IA_PD. This may append options to the tail of
* the reply packet being built in the reply_state structure.
*/
static isc_result_t
reply_process_ia_pd(struct reply_state *reply, struct option_cache *ia) {
isc_result_t status = ISC_R_SUCCESS;
u_int32_t iaid;
unsigned ia_cursor;
struct option_state *packet_ia;
struct option_cache *oc;
struct data_string ia_data, data;
/* Initialize values that will get cleaned up on return. */
packet_ia = NULL;
memset(&ia_data, 0, sizeof(ia_data));
memset(&data, 0, sizeof(data));
/*
* Note that find_client_prefix() may set reply->lease.
*/
/* Make sure there is at least room for the header. */
if ((reply->cursor + IA_PD_OFFSET + 4) > sizeof(reply->buf)) {
log_error("reply_process_ia_pd: Reply too long for IA.");
return ISC_R_NOSPACE;
}
/* Fetch the IA_PD contents. */
if (!get_encapsulated_IA_state(&packet_ia, &ia_data, reply->packet,
ia, IA_PD_OFFSET)) {
log_error("reply_process_ia_pd: error evaluating ia");
status = ISC_R_FAILURE;
goto cleanup;
}
/* Extract IA_PD header contents. */
iaid = getULong(ia_data.data);
reply->renew = getULong(ia_data.data + 4);
reply->rebind = getULong(ia_data.data + 8);
/* Create an IA_PD structure. */
if (ia_allocate(&reply->ia, iaid, (char *)reply->client_id.data,
reply->client_id.len, MDL) != ISC_R_SUCCESS) {
log_error("reply_process_ia_pd: no memory for ia.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
reply->ia->ia_type = D6O_IA_PD;
/* Cache pre-existing IA_PD, if any. */
ia_hash_lookup(&reply->old_ia, ia_pd_active,
(unsigned char *)reply->ia->iaid_duid.data,
reply->ia->iaid_duid.len, MDL);
/*
* Create an option cache to carry the IA_PD option contents, and
* execute any user-supplied values into it.
*/
if (!option_state_allocate(&reply->reply_ia, MDL)) {
status = ISC_R_NOMEMORY;
goto cleanup;
}
/* Check & count the fixed prefix host records. */
reply->static_prefixes = 0;
if ((reply->host != NULL) && (reply->host->fixed_prefix != NULL)) {
struct iaddrcidrnetlist *fp;
for (fp = reply->host->fixed_prefix; fp != NULL;
fp = fp->next) {
reply->static_prefixes += 1;
}
}
/*
* Save the cursor position at the start of the IA_PD, so we can
* set length and adjust t1/t2 values later. We write a temporary
* header out now just in case we decide to adjust the packet
* within sub-process functions.
*/
ia_cursor = reply->cursor;
/* Initialize the IA_PD header. First the code. */
putUShort(reply->buf.data + reply->cursor, (unsigned)D6O_IA_PD);
reply->cursor += 2;
/* Then option length. */
putUShort(reply->buf.data + reply->cursor, 0x0Cu);
reply->cursor += 2;
/* Then IA_PD header contents; IAID. */
putULong(reply->buf.data + reply->cursor, iaid);
reply->cursor += 4;
/* We store the client's t1 for now, and may over-ride it later. */
putULong(reply->buf.data + reply->cursor, reply->renew);
reply->cursor += 4;
/* We store the client's t2 for now, and may over-ride it later. */
putULong(reply->buf.data + reply->cursor, reply->rebind);
reply->cursor += 4;
/*
* For each prefix in this IA_PD, decide what to do about it.
*/
oc = lookup_option(&dhcpv6_universe, packet_ia, D6O_IAPREFIX);
reply->min_valid = reply->min_prefer = INFINITE_TIME;
reply->client_valid = reply->client_prefer = 0;
reply->preflen = -1;
for (; oc != NULL ; oc = oc->next) {
status = reply_process_prefix(reply, oc);
/*
* Canceled means we did not allocate prefixes to the
* client, but we're "done" with this IA - we set a status
* code. So transmit this reply, e.g., move on to the next
* IA.
*/
if (status == ISC_R_CANCELED)
break;
if ((status != ISC_R_SUCCESS) &&
(status != ISC_R_ADDRINUSE) &&
(status != ISC_R_ADDRNOTAVAIL))
goto cleanup;
}
reply->pd_count++;
/*
* If we fell through the above and never gave the client
* a prefix, give it one now.
*/
if ((status != ISC_R_CANCELED) && (reply->client_resources == 0)) {
status = find_client_prefix(reply);
if (status == ISC_R_NORESOURCES) {
switch (reply->packet->dhcpv6_msg_type) {
case DHCPV6_SOLICIT:
/*
* No prefix for any IA is handled
* by the caller.
*/
/* FALL THROUGH */
case DHCPV6_REQUEST:
/* Same than for addresses. */
option_state_dereference(&reply->reply_ia, MDL);
if (!option_state_allocate(&reply->reply_ia,
MDL))
{
log_error("reply_process_ia_pd: No "
"memory for option state "
"wipe.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
if (!set_status_code(STATUS_NoPrefixAvail,
"No prefixes available "
"for this interface.",
reply->reply_ia)) {
log_error("reply_process_ia_pd: "
"Unable to set "
"NoPrefixAvail status "
"code.");
status = ISC_R_FAILURE;
goto cleanup;
}
status = ISC_R_SUCCESS;
break;
default:
if (reply->resources_included)
status = ISC_R_SUCCESS;
else
goto cleanup;
break;
}
}
if (status != ISC_R_SUCCESS)
goto cleanup;
}
/*
* yes, goto's aren't the best but we also want to avoid extra
* indents
*/
if (status == ISC_R_CANCELED) {
/* We're replying with a status code so we still need to
* write it out in wire-format to the outbound buffer */
write_to_packet(reply, ia_cursor);
goto cleanup;
}
/*
* Handle static prefixes, we always log stuff and if it's
* a hard binding we run any commit statements that we have
*/
if (reply->static_prefixes != 0) {
char tmp_addr[INET6_ADDRSTRLEN];
log_info("%s PD: address %s/%d to client with duid %s "
"iaid = %d static",
dhcpv6_type_names[reply->buf.reply.msg_type],
inet_ntop(AF_INET6, reply->fixed_pref.lo_addr.iabuf,
tmp_addr, sizeof(tmp_addr)),
reply->fixed_pref.bits,
print_hex_1(reply->client_id.len,
reply->client_id.data, 60),
iaid);
if ((reply->buf.reply.msg_type == DHCPV6_REPLY) &&
(reply->on_star.on_commit != NULL)) {
execute_statements(NULL, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state,
NULL, reply->on_star.on_commit,
NULL);
executable_statement_dereference
(&reply->on_star.on_commit, MDL);
}
goto cleanup;
}
/*
* If we have any addresses log what we are doing.
*/
if (reply->ia->num_iasubopt != 0) {
struct iasubopt *tmp;
int i;
char tmp_addr[INET6_ADDRSTRLEN];
for (i = 0 ; i < reply->ia->num_iasubopt ; i++) {
tmp = reply->ia->iasubopt[i];
log_info("%s PD: address %s/%d to client with duid %s"
" iaid = %d valid for %u seconds",
dhcpv6_type_names[reply->buf.reply.msg_type],
inet_ntop(AF_INET6, &tmp->addr,
tmp_addr, sizeof(tmp_addr)),
(int)tmp->plen,
print_hex_1(reply->client_id.len,
reply->client_id.data, 60),
iaid, tmp->valid);
}
}
/*
* If this is not a 'soft' binding, consume the new changes into
* the database (if any have been attached to the ia_pd).
*
* Loop through the assigned dynamic prefixes, referencing the
* prefixes onto this IA_PD rather than any old ones, and updating
* prefix pool timers for each (if any).
*
* If a lease can be reused we skip renewing it or checking the
* pool threshold. If it can't we flag that the IA must be commited
* to the db and do the renewal and pool check.
*/
if ((reply->buf.reply.msg_type == DHCPV6_REPLY) &&
(reply->ia->num_iasubopt != 0)) {
int must_commit = 0;
struct iasubopt *tmp;
struct data_string *ia_id;
int i;
for (i = 0 ; i < reply->ia->num_iasubopt ; i++) {
tmp = reply->ia->iasubopt[i];
if (tmp->ia != NULL)
ia_dereference(&tmp->ia, MDL);
ia_reference(&tmp->ia, reply->ia, MDL);
/* If we have anything to do on commit do it now */
if (tmp->on_star.on_commit != NULL) {
execute_statements(NULL, reply->packet,
NULL, NULL,
reply->packet->options,
reply->opt_state,
&tmp->scope,
tmp->on_star.on_commit,
&tmp->on_star);
executable_statement_dereference
(&tmp->on_star.on_commit, MDL);
}
if (!reuse_lease6(reply, tmp)) {
/* Commit 'hard' bindings. */
must_commit = 1;
renew_lease6(tmp->ipv6_pool, tmp);
schedule_lease_timeout(tmp->ipv6_pool);
/* Do our threshold check. */
check_pool6_threshold(reply, tmp);
}
}
/* write the IA_PD in wire-format to the outbound buffer */
write_to_packet(reply, ia_cursor);
/* Remove any old ia from the hash. */
if (reply->old_ia != NULL) {
if (!release_on_roam(reply)) {
ia_id = &reply->old_ia->iaid_duid;
ia_hash_delete(ia_pd_active,
(unsigned char *)ia_id->data,
ia_id->len, MDL);
}
ia_dereference(&reply->old_ia, MDL);
}
/* Put new ia into the hash. */
reply->ia->cltt = cur_time;
ia_id = &reply->ia->iaid_duid;
ia_hash_add(ia_pd_active, (unsigned char *)ia_id->data,
ia_id->len, reply->ia, MDL);
/* If we couldn't reuse all of the iasubopts, we
* must udpate the lease db */
if (must_commit) {
write_ia(reply->ia);
}
} else {
/* write the IA_PD in wire-format to the outbound buffer */
write_to_packet(reply, ia_cursor);
schedule_lease_timeout_reply(reply);
}
cleanup:
if (packet_ia != NULL)
option_state_dereference(&packet_ia, MDL);
if (reply->reply_ia != NULL)
option_state_dereference(&reply->reply_ia, MDL);
if (ia_data.data != NULL)
data_string_forget(&ia_data, MDL);
if (data.data != NULL)
data_string_forget(&data, MDL);
if (reply->ia != NULL)
ia_dereference(&reply->ia, MDL);
if (reply->old_ia != NULL)
ia_dereference(&reply->old_ia, MDL);
if (reply->lease != NULL)
iasubopt_dereference(&reply->lease, MDL);
if (reply->on_star.on_expiry != NULL)
executable_statement_dereference
(&reply->on_star.on_expiry, MDL);
if (reply->on_star.on_release != NULL)
executable_statement_dereference
(&reply->on_star.on_release, MDL);
/*
* ISC_R_CANCELED is a status code used by the prefix processing to
* indicate we're replying with a status code. This is still a
* success at higher layers.
*/
return((status == ISC_R_CANCELED) ? ISC_R_SUCCESS : status);
}
/*!
*
* \brief Find the proper scoping group for use with a v6 static prefix.
*
* We start by trying to find a subnet based on the given prefix and
* the shared network. If we don't find one then the prefix has been
* declared outside of any subnets. If there is a static address
* associated with the host we use it to try and find a subnet (this
* should succeed). If there isn't a static address we fall back
* to the shared subnet itself.
* Once we have a subnet we extract the group from it and return it.
*
* \param reply - the reply structure we use to collect information
* we will use the fields shared, fixed_pref and host
* from the structure
*
* \return a pointer to the group structure to use for scoping
*/
static struct group *
find_group_by_prefix(struct reply_state *reply) {
/* default group if we don't find anything better */
struct group *group = reply->shared->group;
struct subnet *subnet = NULL;
struct iaddr tmp_addr;
struct data_string fixed_addr;
/* Try with the prefix first */
if (find_grouped_subnet(&subnet, reply->shared,
reply->fixed_pref.lo_addr, MDL) != 0) {
group = subnet->group;
subnet_dereference(&subnet, MDL);
return (group);
}
/* Didn't find a subnet via prefix, what about fixed address */
/* The caller has already tested reply->host != NULL */
memset(&fixed_addr, 0, sizeof(fixed_addr));
if ((reply->host->fixed_addr != NULL) &&
(evaluate_option_cache(&fixed_addr, NULL, NULL, NULL,
NULL, NULL, &global_scope,
reply->host->fixed_addr, MDL))) {
if (fixed_addr.len >= 16) {
tmp_addr.len = 16;
memcpy(tmp_addr.iabuf, fixed_addr.data, 16);
if (find_grouped_subnet(&subnet, reply->shared,
tmp_addr, MDL) != 0) {
group = subnet->group;
subnet_dereference(&subnet, MDL);
}
}
data_string_forget(&fixed_addr, MDL);
}
/* return whatever we got */
return (group);
}
/*
* Process an IAPREFIX within a given IA_PD, storing any IAPREFIX reply
* contents into the reply's current ia_pd-scoped option cache. Returns
* ISC_R_CANCELED in the event we are replying with a status code and do
* not wish to process more IAPREFIXes within this IA_PD.
*/
static isc_result_t
reply_process_prefix(struct reply_state *reply, struct option_cache *pref) {
u_int32_t pref_life, valid_life;
struct binding_scope **scope;
struct iaddrcidrnet tmp_pref;
struct option_cache *oc;
struct data_string iapref, data;
isc_result_t status = ISC_R_SUCCESS;
struct group *group;
/* Initializes values that will be cleaned up. */
memset(&iapref, 0, sizeof(iapref));
memset(&data, 0, sizeof(data));
/* Note that reply->lease may be set by prefix_is_owned() */
/*
* There is no point trying to process an incoming prefix if there
* is no room for an outgoing prefix.
*/
if ((reply->cursor + 29) > sizeof(reply->buf)) {
log_error("reply_process_prefix: Out of room for prefix.");
return ISC_R_NOSPACE;
}
/* Extract this IAPREFIX option. */
if (!evaluate_option_cache(&iapref, reply->packet, NULL, NULL,
reply->packet->options, NULL, &global_scope,
pref, MDL) ||
(iapref.len < IAPREFIX_OFFSET)) {
log_error("reply_process_prefix: error evaluating IAPREFIX.");
status = ISC_R_FAILURE;
goto cleanup;
}
/*
* Layout: preferred and valid lifetimes followed by the prefix
* length and the IPv6 address.
*/
pref_life = getULong(iapref.data);
valid_life = getULong(iapref.data + 4);
if ((reply->client_valid == 0) ||
(reply->client_valid > valid_life))
reply->client_valid = valid_life;
if ((reply->client_prefer == 0) ||
(reply->client_prefer > pref_life))
reply->client_prefer = pref_life;
/*
* Clients may choose to send ::/0 as a prefix, with the idea to give
* hints about preferred-lifetime or valid-lifetime.
*/
tmp_pref.lo_addr.len = 16;
memset(tmp_pref.lo_addr.iabuf, 0, 16);
if ((iapref.data[8] == 0) &&
(memcmp(iapref.data + 9, tmp_pref.lo_addr.iabuf, 16) == 0)) {
/* Status remains success; we just ignore this one. */
goto cleanup;
}
/*
* Clients may choose to send ::/X as a prefix to specify a
* preferred/requested prefix length. Note X is never zero here.
*/
tmp_pref.bits = (int) iapref.data[8];
if (reply->preflen < 0) {
/* Cache the first preferred prefix length. */
reply->preflen = tmp_pref.bits;
}
if (memcmp(iapref.data + 9, tmp_pref.lo_addr.iabuf, 16) == 0) {
goto cleanup;
}
memcpy(tmp_pref.lo_addr.iabuf, iapref.data + 9, 16);
/* Verify the prefix belongs to the client. */
if (!prefix_is_owned(reply, &tmp_pref)) {
/* Same than for addresses. */
if ((reply->packet->dhcpv6_msg_type == DHCPV6_SOLICIT) ||
(reply->packet->dhcpv6_msg_type == DHCPV6_REQUEST) ||
(reply->packet->dhcpv6_msg_type == DHCPV6_REBIND)) {
status = reply_process_try_prefix(reply, &tmp_pref);
/* Either error out or skip this prefix. */
if ((status != ISC_R_SUCCESS) &&
(status != ISC_R_ADDRINUSE) &&
(status != ISC_R_ADDRNOTAVAIL))
goto cleanup;
if (reply->lease == NULL) {
if (reply->packet->dhcpv6_msg_type ==
DHCPV6_REBIND) {
reply->send_prefer = 0;
reply->send_valid = 0;
goto send_pref;
}
/* status remains success - ignore */
goto cleanup;
}
/*
* RFC3633 section 18.2.3:
*
* 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.
*
* On mismatch we (ab)use this pretending we have not the IA
* as soon as we have not a prefix.
*/
} else if (reply->packet->dhcpv6_msg_type == DHCPV6_RENEW) {
/* Rewind the IA_PD to empty. */
option_state_dereference(&reply->reply_ia, MDL);
if (!option_state_allocate(&reply->reply_ia, MDL)) {
log_error("reply_process_prefix: No memory "
"for option state wipe.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
/* Append a NoBinding status code. */
if (!set_status_code(STATUS_NoBinding,
"Prefix not bound to this "
"interface.", reply->reply_ia)) {
log_error("reply_process_prefix: Unable to "
"attach status code.");
status = ISC_R_FAILURE;
goto cleanup;
}
/* Fin (no more IAPREFIXes). */
status = ISC_R_CANCELED;
goto cleanup;
} else {
log_error("It is impossible to lease a client that is "
"not sending a solicit, request, renew, or "
"rebind message.");
status = ISC_R_FAILURE;
goto cleanup;
}
}
if (reply->static_prefixes > 0) {
if (reply->host == NULL)
log_fatal("Impossible condition at %s:%d.", MDL);
scope = &global_scope;
/* Copy the static prefix for logging and finding the group */
memcpy(&reply->fixed_pref, &tmp_pref, sizeof(tmp_pref));
/* Try to find a group for the static prefix */
group = find_group_by_prefix(reply);
} else {
if (reply->lease == NULL)
log_fatal("Impossible condition at %s:%d.", MDL);
scope = &reply->lease->scope;
group = reply->lease->ipv6_pool->ipv6_pond->group;
}
/*
* If client_resources is nonzero, then the reply_process_is_prefixed
* function has executed configuration state into the reply option
* cache. We will use that valid cache to derive configuration for
* whether or not to engage in additional prefixes, and similar.
*/
if (reply->client_resources != 0) {
unsigned limit = 1;
/*
* Does this client have "enough" prefixes already? Default
* to one. Everybody gets one, and one should be enough for
* anybody.
*/
oc = lookup_option(&server_universe, reply->opt_state,
SV_LIMIT_PREFS_PER_IA);
if (oc != NULL) {
if (!evaluate_option_cache(&data, reply->packet,
NULL, NULL,
reply->packet->options,
reply->opt_state,
scope, oc, MDL) ||
(data.len != 4)) {
log_error("reply_process_prefix: unable to "
"evaluate prefs-per-ia value.");
status = ISC_R_FAILURE;
goto cleanup;
}
limit = getULong(data.data);
data_string_forget(&data, MDL);
}
/*
* If we wish to limit the client to a certain number of
* prefixes, then omit the prefix from the reply.
*/
if (reply->client_resources >= limit)
goto cleanup;
}
status = reply_process_is_prefixed(reply, scope, group);
if (status != ISC_R_SUCCESS)
goto cleanup;
send_pref:
status = reply_process_send_prefix(reply, &tmp_pref);
cleanup:
if (iapref.data != NULL)
data_string_forget(&iapref, MDL);
if (data.data != NULL)
data_string_forget(&data, MDL);
if (reply->lease != NULL)
iasubopt_dereference(&reply->lease, MDL);
return status;
}
/*
* Verify the prefix belongs to the client. If we've got a host
* record with fixed prefixes, it has to be an assigned prefix
* (fault out all else). Otherwise it's a dynamic prefix, so lookup
* that prefix and make sure it belongs to this DUID:IAID pair.
*/
static isc_boolean_t
prefix_is_owned(struct reply_state *reply, struct iaddrcidrnet *pref) {
struct iaddrcidrnetlist *l;
int i;
struct ipv6_pond *pond;
/*
* This faults out prefixes that don't match fixed prefixes.
*/
if (reply->static_prefixes > 0) {
for (l = reply->host->fixed_prefix; l != NULL; l = l->next) {
if ((pref->bits == l->cidrnet.bits) &&
(memcmp(pref->lo_addr.iabuf,
l->cidrnet.lo_addr.iabuf, 16) == 0))
return (ISC_TRUE);
}
return (ISC_FALSE);
}
if ((reply->old_ia == NULL) ||
(reply->old_ia->num_iasubopt == 0))
return (ISC_FALSE);
for (i = 0 ; i < reply->old_ia->num_iasubopt ; i++) {
struct iasubopt *tmp;
tmp = reply->old_ia->iasubopt[i];
if ((pref->bits == (int) tmp->plen) &&
(memcmp(pref->lo_addr.iabuf, &tmp->addr, 16) == 0)) {
if (lease6_usable(tmp) == ISC_FALSE) {
return (ISC_FALSE);
}
pond = tmp->ipv6_pool->ipv6_pond;
if (((pond->prohibit_list != NULL) &&
(permitted(reply->packet, pond->prohibit_list))) ||
((pond->permit_list != NULL) &&
(!permitted(reply->packet, pond->permit_list))))
return (ISC_FALSE);
iasubopt_reference(&reply->lease, tmp, MDL);
return (ISC_TRUE);
}
}
return (ISC_FALSE);
}
/*
* This function only returns failure on 'hard' failures. If it succeeds,
* it will leave a prefix structure behind.
*/
static isc_result_t
reply_process_try_prefix(struct reply_state *reply,
struct iaddrcidrnet *pref) {
isc_result_t status = ISC_R_ADDRNOTAVAIL;
struct ipv6_pool *pool = NULL;
struct ipv6_pond *pond = NULL;
int i;
struct data_string data_pref;
if ((reply == NULL) || (reply->shared == NULL) ||
(pref == NULL) || (reply->lease != NULL))
return (DHCP_R_INVALIDARG);
/*
* Do a quick walk through of the ponds and pools
* to see if we have any prefix pools
*/
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
if (pond->ipv6_pools == NULL)
continue;
for (i = 0; (pool = pond->ipv6_pools[i]) != NULL; i++) {
if (pool->pool_type == D6O_IA_PD)
break;
}
if (pool != NULL)
break;
}
/* If we get here and p is NULL we have no useful pools */
if (pool == NULL) {
return (ISC_R_ADDRNOTAVAIL);
}
memset(&data_pref, 0, sizeof(data_pref));
data_pref.len = 17;
if (!buffer_allocate(&data_pref.buffer, data_pref.len, MDL)) {
log_error("reply_process_try_prefix: out of memory.");
return (ISC_R_NOMEMORY);
}
data_pref.data = data_pref.buffer->data;
data_pref.buffer->data[0] = (u_int8_t) pref->bits;
memcpy(data_pref.buffer->data + 1, pref->lo_addr.iabuf, 16);
/*
* We have at least one pool that could provide a prefix
* Now we walk through the ponds and pools again and check
* to see if the client is permitted and if an prefix is
* available
*
*/
for (pond = reply->shared->ipv6_pond; pond != NULL; pond = pond->next) {
if (((pond->prohibit_list != NULL) &&
(permitted(reply->packet, pond->prohibit_list))) ||
((pond->permit_list != NULL) &&
(!permitted(reply->packet, pond->permit_list))))
continue;
for (i = 0; (pool = pond->ipv6_pools[i]) != NULL; i++) {
if (pool->pool_type != D6O_IA_PD) {
continue;
}
status = try_client_v6_prefix(&reply->lease, pool,
&data_pref);
/* If we found it in this pool (either in use or available),
there is no need to look further. */
if ( (status == ISC_R_SUCCESS) || (status == ISC_R_ADDRINUSE) )
break;
}
if ( (status == ISC_R_SUCCESS) || (status == ISC_R_ADDRINUSE) )
break;
}
data_string_forget(&data_pref, MDL);
/* Return just the most recent status... */
return (status);
}
/* Look around for a prefix to give the client. First, look through the old
* IA_PD for prefixes we can extend. Second, try to allocate a new prefix.
* Finally, actually add that prefix into the current reply IA_PD.
*/
static isc_result_t
find_client_prefix(struct reply_state *reply) {
struct iaddrcidrnet send_pref;
isc_result_t status = ISC_R_NORESOURCES;
struct iasubopt *prefix, *best_prefix = NULL;
struct binding_scope **scope;
int i;
struct group *group;
if (reply->static_prefixes > 0) {
struct iaddrcidrnetlist *l;
if (reply->host == NULL)
return DHCP_R_INVALIDARG;
for (l = reply->host->fixed_prefix; l != NULL; l = l->next) {
if (l->cidrnet.bits == reply->preflen)
break;
}
if (l == NULL) {
/*
* If no fixed prefix has the preferred length,
* get the first one.
*/
l = reply->host->fixed_prefix;
}
memcpy(&send_pref, &l->cidrnet, sizeof(send_pref));
scope = &global_scope;
/* Copy the prefix for logging purposes */
memcpy(&reply->fixed_pref, &l->cidrnet, sizeof(send_pref));
/* Try to find a group for the static prefix */
group = find_group_by_prefix(reply);
goto send_pref;
}
if (reply->old_ia != NULL) {
for (i = 0 ; i < reply->old_ia->num_iasubopt ; i++) {
struct shared_network *candidate_shared;
struct ipv6_pond *pond;
prefix = reply->old_ia->iasubopt[i];
candidate_shared = prefix->ipv6_pool->shared_network;
pond = prefix->ipv6_pool->ipv6_pond;
/*
* Consider this prefix if it is in a global pool or
* if it is scoped in a pool under the client's shared
* network.
*/
if (((candidate_shared != NULL) &&
(candidate_shared != reply->shared)) ||
(lease6_usable(prefix) != ISC_TRUE))
continue;
/*
* And check if the prefix is still permitted
*/
if (((pond->prohibit_list != NULL) &&
(permitted(reply->packet, pond->prohibit_list))) ||
((pond->permit_list != NULL) &&
(!permitted(reply->packet, pond->permit_list))))
continue;
best_prefix = prefix_compare(reply, prefix,
best_prefix);
}
/*
* If we have prefix length hint and we're not igoring them,
* then toss the best match if it doesn't match the hint,
* unless this is in response to a rebind. In the latter
* case we're supposed to return it with zero lifetimes.
* (See rt45780) */
if (best_prefix && (reply->preflen > 0)
&& (prefix_length_mode != PLM_IGNORE)
&& (reply->preflen != best_prefix->plen)
&& (reply->packet->dhcpv6_msg_type != DHCPV6_REBIND)) {
best_prefix = NULL;
}
}
/* Try to pick a new prefix if we didn't find one, or if we found an
* abandoned prefix.
*/
if ((best_prefix == NULL) || (best_prefix->state == FTS_ABANDONED)) {
status = pick_v6_prefix(reply);
} else if (best_prefix != NULL) {
iasubopt_reference(&reply->lease, best_prefix, MDL);
status = ISC_R_SUCCESS;
}
/* Pick the abandoned prefix as a last resort. */
if ((status == ISC_R_NORESOURCES) && (best_prefix != NULL)) {
/* I don't see how this is supposed to be done right now. */
log_error("Reclaiming abandoned prefixes is not yet "
"supported. Treating this as an out of space "
"condition.");
/* iasubopt_reference(&reply->lease, best_prefix, MDL); */
}
/* Give up now if we didn't find a prefix. */
if (status != ISC_R_SUCCESS)
return status;
if (reply->lease == NULL)
log_fatal("Impossible condition at %s:%d.", MDL);
scope = &reply->lease->scope;
group = reply->lease->ipv6_pool->ipv6_pond->group;
send_pref.lo_addr.len = 16;
memcpy(send_pref.lo_addr.iabuf, &reply->lease->addr, 16);
send_pref.bits = (int) reply->lease->plen;
send_pref:
status = reply_process_is_prefixed(reply, scope, group);
if (status != ISC_R_SUCCESS)
return status;
status = reply_process_send_prefix(reply, &send_pref);
return status;
}
/* Once a prefix is found for a client, perform several common functions;
* Calculate and store valid and preferred prefix times, draw client options
* into the option state.
*/
static isc_result_t
reply_process_is_prefixed(struct reply_state *reply,
struct binding_scope **scope, struct group *group)
{
isc_result_t status = ISC_R_SUCCESS;
struct data_string data;
struct option_cache *oc;
struct option_state *tmp_options = NULL;
struct on_star *on_star;
int i;
/* Initialize values we will cleanup. */
memset(&data, 0, sizeof(data));
/*
* Find the proper on_star block to use. We use the
* one in the lease if we have a lease or the one in
* the reply if we don't have a lease because this is
* a static instance
*/
if (reply->lease) {
on_star = &reply->lease->on_star;
} else {
on_star = &reply->on_star;
}
/*
* Bring in the root configuration. We only do this to bring
* in the on * statements, as we didn't have the lease available
* we we did it the first time.
*/
option_state_allocate(&tmp_options, MDL);
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options, tmp_options,
&global_scope, root_group, NULL,
on_star);
if (tmp_options != NULL) {
option_state_dereference(&tmp_options, MDL);
}
/*
* Bring configured options into the root packet level cache - start
* with the lease's closest enclosing group (passed in by the caller
* as 'group').
*/
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options, reply->opt_state,
scope, group, root_group, on_star);
/* Execute statements from class scopes. */
for (i = reply->packet->class_count; i > 0; i--) {
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state, scope,
reply->packet->classes[i - 1]->group,
group, on_star);
}
/*
* If there is a host record, over-ride with values configured there,
* without re-evaluating configuration from the previously executed
* group or its common enclosers.
*/
if (reply->host != NULL)
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state, scope,
reply->host->group, group,
on_star);
/* Determine valid lifetime. */
if (reply->client_valid == 0)
reply->send_valid = DEFAULT_DEFAULT_LEASE_TIME;
else
reply->send_valid = reply->client_valid;
oc = lookup_option(&server_universe, reply->opt_state,
SV_DEFAULT_LEASE_TIME);
if (oc != NULL) {
if (!evaluate_option_cache(&data, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state,
scope, oc, MDL) ||
(data.len != 4)) {
log_error("reply_process_is_prefixed: unable to "
"evaluate default prefix time");
status = ISC_R_FAILURE;
goto cleanup;
}
reply->send_valid = getULong(data.data);
data_string_forget(&data, MDL);
}
/* Check to see if the lease time would cause us to wrap
* in which case we make it infinite.
* The following doesn't work on at least some systems:
* (cur_time + reply->send_valid < cur_time)
*/
if (reply->send_valid != INFINITE_TIME) {
time_t test_time = cur_time + reply->send_valid;
if (test_time < cur_time)
reply->send_valid = INFINITE_TIME;
}
if (reply->client_prefer == 0)
reply->send_prefer = reply->send_valid;
else
reply->send_prefer = reply->client_prefer;
if ((reply->send_prefer >= reply->send_valid) &&
(reply->send_valid != INFINITE_TIME))
reply->send_prefer = (reply->send_valid / 2) +
(reply->send_valid / 8);
oc = lookup_option(&server_universe, reply->opt_state,
SV_PREFER_LIFETIME);
if (oc != NULL) {
if (!evaluate_option_cache(&data, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state,
scope, oc, MDL) ||
(data.len != 4)) {
log_error("reply_process_is_prefixed: unable to "
"evaluate preferred prefix time");
status = ISC_R_FAILURE;
goto cleanup;
}
reply->send_prefer = getULong(data.data);
data_string_forget(&data, MDL);
}
/* Note lowest values for later calculation of renew/rebind times. */
if (reply->min_prefer > reply->send_prefer)
reply->min_prefer = reply->send_prefer;
if (reply->min_valid > reply->send_valid)
reply->min_valid = reply->send_valid;
/* Perform dynamic prefix related update work. */
if (reply->lease != NULL) {
/* Cached lifetimes */
reply->lease->prefer = reply->send_prefer;
reply->lease->valid = reply->send_valid;
/* Advance (or rewind) the valid lifetime.
* In the protocol 0xFFFFFFFF is infinite
* when connecting to the lease file MAX_TIME is
*/
if (reply->buf.reply.msg_type == DHCPV6_REPLY) {
if (reply->send_valid == INFINITE_TIME) {
reply->lease->soft_lifetime_end_time = MAX_TIME;
} else {
reply->lease->soft_lifetime_end_time =
cur_time + reply->send_valid;
}
/* Wait before renew! */
}
status = ia_add_iasubopt(reply->ia, reply->lease, MDL);
if (status != ISC_R_SUCCESS) {
log_fatal("reply_process_is_prefixed: Unable to "
"attach prefix to new IA_PD: %s",
isc_result_totext(status));
}
/*
* If this is a new prefix, make sure it is attached somewhere.
*/
if (reply->lease->ia == NULL) {
ia_reference(&reply->lease->ia, reply->ia, MDL);
}
}
/* Bring a copy of the relevant options into the IA_PD scope. */
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options, reply->reply_ia,
scope, group, root_group, NULL);
/* Execute statements from class scopes. */
for (i = reply->packet->class_count; i > 0; i--) {
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options,
reply->reply_ia, scope,
reply->packet->classes[i - 1]->group,
group, NULL);
}
/*
* And bring in host record configuration, if any, but not to overlap
* the previous group or its common enclosers.
*/
if (reply->host != NULL)
execute_statements_in_scope(NULL, reply->packet, NULL, NULL,
reply->packet->options,
reply->reply_ia, scope,
reply->host->group, group, NULL);
cleanup:
if (data.data != NULL)
data_string_forget(&data, MDL);
if (status == ISC_R_SUCCESS)
reply->client_resources++;
return status;
}
/* Simply send an IAPREFIX within the IA_PD scope as described. */
static isc_result_t
reply_process_send_prefix(struct reply_state *reply,
struct iaddrcidrnet *pref) {
isc_result_t status = ISC_R_SUCCESS;
struct data_string data;
memset(&data, 0, sizeof(data));
/* Now append the prefix. */
data.len = IAPREFIX_OFFSET;
if (!buffer_allocate(&data.buffer, data.len, MDL)) {
log_error("reply_process_send_prefix: out of memory"
"allocating new IAPREFIX buffer.");
status = ISC_R_NOMEMORY;
goto cleanup;
}
data.data = data.buffer->data;
putULong(data.buffer->data, reply->send_prefer);
putULong(data.buffer->data + 4, reply->send_valid);
data.buffer->data[8] = pref->bits;
memcpy(data.buffer->data + 9, pref->lo_addr.iabuf, 16);
if (!append_option_buffer(&dhcpv6_universe, reply->reply_ia,
data.buffer, data.buffer->data,
data.len, D6O_IAPREFIX, 0)) {
log_error("reply_process_send_prefix: unable "
"to save IAPREFIX option");
status = ISC_R_FAILURE;
goto cleanup;
}
reply->resources_included = ISC_TRUE;
cleanup:
if (data.data != NULL)
data_string_forget(&data, MDL);
return status;
}
/* Choose the better of two prefixes. */
static struct iasubopt *
prefix_compare(struct reply_state *reply,
struct iasubopt *alpha, struct iasubopt *beta) {
if (alpha == NULL)
return beta;
if (beta == NULL)
return alpha;
if (reply->preflen >= 0) {
if ((alpha->plen == reply->preflen) &&
(beta->plen != reply->preflen))
return alpha;
if ((beta->plen == reply->preflen) &&
(alpha->plen != reply->preflen))
return beta;
}
switch(alpha->state) {
case FTS_ACTIVE:
switch(beta->state) {
case FTS_ACTIVE:
/* Choose the prefix with the longest lifetime (most
* likely the most recently allocated).
*/
if (alpha->hard_lifetime_end_time <
beta->hard_lifetime_end_time)
return beta;
else
return alpha;
case FTS_EXPIRED:
case FTS_ABANDONED:
return alpha;
default:
log_fatal("Impossible condition at %s:%d.", MDL);
}
break;
case FTS_EXPIRED:
switch (beta->state) {
case FTS_ACTIVE:
return beta;
case FTS_EXPIRED:
/* Choose the most recently expired prefix. */
if (alpha->hard_lifetime_end_time <
beta->hard_lifetime_end_time)
return beta;
else if ((alpha->hard_lifetime_end_time ==
beta->hard_lifetime_end_time) &&
(alpha->soft_lifetime_end_time <
beta->soft_lifetime_end_time))
return beta;
else
return alpha;
case FTS_ABANDONED:
return alpha;
default:
log_fatal("Impossible condition at %s:%d.", MDL);
}
break;
case FTS_ABANDONED:
switch (beta->state) {
case FTS_ACTIVE:
case FTS_EXPIRED:
return alpha;
case FTS_ABANDONED:
/* Choose the prefix that was abandoned longest ago. */
if (alpha->hard_lifetime_end_time <
beta->hard_lifetime_end_time)
return alpha;
else
return beta;
default:
log_fatal("Impossible condition at %s:%d.", MDL);
}
break;
default:
log_fatal("Impossible condition at %s:%d.", MDL);
}
log_fatal("Triple impossible condition at %s:%d.", MDL);
return NULL;
}
/*
* Solicit is how a client starts requesting addresses.
*
* If the client asks for rapid commit, and we support it, we will
* allocate the addresses and reply.
*
* Otherwise we will send an advertise message.
*/
static void
dhcpv6_solicit(struct data_string *reply_ret, struct packet *packet) {
struct data_string client_id;
/*
* Validate our input.
*/
if (!valid_client_msg(packet, &client_id)) {
return;
}
lease_to_client(reply_ret, packet, &client_id, NULL);
/*
* Clean up.
*/
data_string_forget(&client_id, MDL);
}
/*
* Request is how a client actually requests addresses.
*
* Very similar to Solicit handling, except the server DUID is required.
*/
static void
dhcpv6_request(struct data_string *reply_ret, struct packet *packet) {
struct data_string client_id;
struct data_string server_id;
/*
* Validate our input.
*/
if (!valid_client_resp(packet, &client_id, &server_id)) {
return;
}
/* If the REQUEST arrived via unicast and unicast option isn't set,
* reject it per RFC 3315, Sec 18.2.1 */
if (packet->unicast == ISC_TRUE &&
is_unicast_option_defined(packet) == ISC_FALSE) {
unicast_reject(reply_ret, packet, &client_id, &server_id);
} else {
/*
* Issue our lease.
*/
lease_to_client(reply_ret, packet, &client_id, &server_id);
}
/*
* Cleanup.
*/
data_string_forget(&client_id, MDL);
data_string_forget(&server_id, MDL);
}
/* Find a DHCPv6 packet's shared network from hints in the packet.
*/
static isc_result_t
shared_network_from_packet6(struct shared_network **shared,
struct packet *packet)
{
const struct packet *chk_packet;
const struct in6_addr *link_addr, *first_link_addr;
struct iaddr tmp_addr;
struct subnet *subnet;
isc_result_t status;
if ((shared == NULL) || (*shared != NULL) || (packet == NULL))
return DHCP_R_INVALIDARG;
/*
* First, find the link address where the packet from the client
* first appeared (if this packet was relayed).
*/
first_link_addr = NULL;
chk_packet = packet->dhcpv6_container_packet;
while (chk_packet != NULL) {
link_addr = &chk_packet->dhcpv6_link_address;
if (!IN6_IS_ADDR_UNSPECIFIED(link_addr) &&
!IN6_IS_ADDR_LINKLOCAL(link_addr)) {
first_link_addr = link_addr;
break;
}
chk_packet = chk_packet->dhcpv6_container_packet;
}
/*
* If there is a relayed link address, find the subnet associated
* with that, and use that to get the appropriate
* shared_network.
*/
if (first_link_addr != NULL) {
tmp_addr.len = sizeof(*first_link_addr);
memcpy(tmp_addr.iabuf,
first_link_addr, sizeof(*first_link_addr));
subnet = NULL;
if (!find_subnet(&subnet, tmp_addr, MDL)) {
log_debug("No subnet found for link-address %s.",
piaddr(tmp_addr));
return ISC_R_NOTFOUND;
}
status = shared_network_reference(shared,
subnet->shared_network, MDL);
subnet_dereference(&subnet, MDL);
/*
* If there is no link address, we will use the interface
* that this packet came in on to pick the shared_network.
*/
} else if (packet->interface != NULL) {
status = shared_network_reference(shared,
packet->interface->shared_network,
MDL);
if (packet->dhcpv6_container_packet != NULL) {
log_info("[L2 Relay] No link address in relay packet "
"assuming L2 relay and using receiving "
"interface");
}
} else {
/*
* We shouldn't be able to get here but if there is no link
* address and no interface we don't know where to get the
* pool from log an error and return an error.
*/
log_error("No interface and no link address "
"can't determine pool");
status = DHCP_R_INVALIDARG;
}
return status;
}
/*
* When a client thinks it might be on a new link, it sends a
* Confirm message.
*
* From RFC3315 section 18.2.2:
*
* When the server receives a Confirm message, the server determines
* whether the addresses in the Confirm message are appropriate for the
* link to which the client is attached. If all of the addresses in the
* Confirm message pass this test, the server returns a status of
* Success. If any of the addresses do not pass this test, the server
* returns a status of NotOnLink. If the server is unable to perform
* this test (for example, the server does not have information about
* prefixes on the link to which the client is connected), or there were
* no addresses in any of the IAs sent by the client, the server MUST
* NOT send a reply to the client.
*/
static void
dhcpv6_confirm(struct data_string *reply_ret, struct packet *packet) {
struct shared_network *shared;
struct subnet *subnet;
struct option_cache *ia, *ta, *oc;
struct data_string cli_enc_opt_data, iaaddr, client_id, packet_oro;
struct option_state *cli_enc_opt_state, *opt_state;
struct iaddr cli_addr;
int pass;
isc_boolean_t inappropriate, has_addrs;
char reply_data[65536];
struct dhcpv6_packet *reply = (struct dhcpv6_packet *)reply_data;
int reply_ofs = (int)(offsetof(struct dhcpv6_packet, options));
/*
* Basic client message validation.
*/
memset(&client_id, 0, sizeof(client_id));
if (!valid_client_msg(packet, &client_id)) {
return;
}
/*
* Do not process Confirms that do not have IA's we do not recognize.
*/
ia = lookup_option(&dhcpv6_universe, packet->options, D6O_IA_NA);
ta = lookup_option(&dhcpv6_universe, packet->options, D6O_IA_TA);
if ((ia == NULL) && (ta == NULL))
return;
/*
* IA_PD's are simply ignored.
*/
delete_option(&dhcpv6_universe, packet->options, D6O_IA_PD);
/*
* Bit of variable initialization.
*/
opt_state = cli_enc_opt_state = NULL;
memset(&cli_enc_opt_data, 0, sizeof(cli_enc_opt_data));
memset(&iaaddr, 0, sizeof(iaaddr));
memset(&packet_oro, 0, sizeof(packet_oro));
/* Determine what shared network the client is connected to. We
* must not respond if we don't have any information about the
* network the client is on.
*/
shared = NULL;
if ((shared_network_from_packet6(&shared, packet) != ISC_R_SUCCESS) ||
(shared == NULL))
goto exit;
/* If there are no recorded subnets, then we have no
* information about this subnet - ignore Confirms.
*/
subnet = shared->subnets;
if (subnet == NULL)
goto exit;
/* Are the addresses in all the IA's appropriate for that link? */
has_addrs = inappropriate = ISC_FALSE;
pass = D6O_IA_NA;
while(!inappropriate) {
/* If we've reached the end of the IA_NA pass, move to the
* IA_TA pass.
*/
if ((pass == D6O_IA_NA) && (ia == NULL)) {
pass = D6O_IA_TA;
ia = ta;
}
/* If we've reached the end of all passes, we're done. */
if (ia == NULL)
break;
if (((pass == D6O_IA_NA) &&
!get_encapsulated_IA_state(&cli_enc_opt_state,
&cli_enc_opt_data,
packet, ia, IA_NA_OFFSET)) ||
((pass == D6O_IA_TA) &&
!get_encapsulated_IA_state(&cli_enc_opt_state,
&cli_enc_opt_data,
packet, ia, IA_TA_OFFSET))) {
goto exit;
}
oc = lookup_option(&dhcpv6_universe, cli_enc_opt_state,
D6O_IAADDR);
for ( ; oc != NULL ; oc = oc->next) {
if (!evaluate_option_cache(&iaaddr, packet, NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL) ||
(iaaddr.len < IAADDR_OFFSET)) {
log_error("dhcpv6_confirm: "
"error evaluating IAADDR.");
goto exit;
}
/* Copy out the IPv6 address for processing. */
cli_addr.len = 16;
memcpy(cli_addr.iabuf, iaaddr.data, 16);
data_string_forget(&iaaddr, MDL);
/* Record that we've processed at least one address. */
has_addrs = ISC_TRUE;
/* Find out if any subnets cover this address. */
for (subnet = shared->subnets ; subnet != NULL ;
subnet = subnet->next_sibling) {
if (addr_eq(subnet_number(cli_addr,
subnet->netmask),
subnet->net))
break;
}
/* If we reach the end of the subnet list, and no
* subnet matches the client address, then it must
* be inappropriate to the link (so far as our
* configuration says). Once we've found one
* inappropriate address, there is no reason to
* continue searching.
*/
if (subnet == NULL) {
inappropriate = ISC_TRUE;
break;
}
}
option_state_dereference(&cli_enc_opt_state, MDL);
data_string_forget(&cli_enc_opt_data, MDL);
/* Advance to the next IA_*. */
ia = ia->next;
}
/* If the client supplied no addresses, do not reply. */
if (!has_addrs)
goto exit;
/*
* Set up reply.
*/
if (!start_reply(packet, &client_id, NULL, &opt_state, reply)) {
goto exit;
}
/*
* Set our status.
*/
if (inappropriate) {
if (!set_status_code(STATUS_NotOnLink,
"Some of the addresses are not on link.",
opt_state)) {
goto exit;
}
} else {
if (!set_status_code(STATUS_Success,
"All addresses still on link.",
opt_state)) {
goto exit;
}
}
/*
* Only one option: add it.
*/
reply_ofs += store_options6(reply_data+reply_ofs,
sizeof(reply_data)-reply_ofs,
opt_state, packet,
required_opts, &packet_oro);
/*
* Return our reply to the caller.
*/
reply_ret->len = reply_ofs;
reply_ret->buffer = NULL;
if (!buffer_allocate(&reply_ret->buffer, reply_ofs, MDL)) {
log_fatal("No memory to store reply.");
}
reply_ret->data = reply_ret->buffer->data;
memcpy(reply_ret->buffer->data, reply, reply_ofs);
exit:
/* Cleanup any stale data strings. */
if (cli_enc_opt_data.buffer != NULL)
data_string_forget(&cli_enc_opt_data, MDL);
if (iaaddr.buffer != NULL)
data_string_forget(&iaaddr, MDL);
if (client_id.buffer != NULL)
data_string_forget(&client_id, MDL);
if (packet_oro.buffer != NULL)
data_string_forget(&packet_oro, MDL);
/* Release any stale option states. */
if (cli_enc_opt_state != NULL)
option_state_dereference(&cli_enc_opt_state, MDL);
if (opt_state != NULL)
option_state_dereference(&opt_state, MDL);
}
/*
* Renew is when a client wants to extend its lease/prefix, at time T1.
*
* We handle this the same as if the client wants a new lease/prefix,
* except for the error code of when addresses don't match.
*/
static void
dhcpv6_renew(struct data_string *reply, struct packet *packet) {
struct data_string client_id;
struct data_string server_id;
/*
* Validate the request.
*/
if (!valid_client_resp(packet, &client_id, &server_id)) {
return;
}
/* If the RENEW arrived via unicast and unicast option isn't set,
* reject it per RFC 3315, Sec 18.2.3 */
if (packet->unicast == ISC_TRUE &&
is_unicast_option_defined(packet) == ISC_FALSE) {
unicast_reject(reply, packet, &client_id, &server_id);
} else {
/*
* Renew our lease.
*/
lease_to_client(reply, packet, &client_id, &server_id);
}
/*
* Cleanup.
*/
data_string_forget(&server_id, MDL);
data_string_forget(&client_id, MDL);
}
/*
* Rebind is when a client wants to extend its lease, at time T2.
*
* We handle this the same as if the client wants a new lease, except
* for the error code of when addresses don't match.
*/
static void
dhcpv6_rebind(struct data_string *reply, struct packet *packet) {
struct data_string client_id;
if (!valid_client_msg(packet, &client_id)) {
return;
}
lease_to_client(reply, packet, &client_id, NULL);
data_string_forget(&client_id, MDL);
}
static void
ia_na_match_decline(const struct data_string *client_id,
const struct data_string *iaaddr,
struct iasubopt *lease)
{
char tmp_addr[INET6_ADDRSTRLEN];
log_error("Client %s reports address %s is "
"already in use by another host!",
print_hex_1(client_id->len, client_id->data, 60),
inet_ntop(AF_INET6, iaaddr->data,
tmp_addr, sizeof(tmp_addr)));
if (lease != NULL) {
decline_lease6(lease->ipv6_pool, lease);
lease->ia->cltt = cur_time;
write_ia(lease->ia);
}
}
static void
ia_na_nomatch_decline(const struct data_string *client_id,
const struct data_string *iaaddr,
u_int32_t *ia_na_id,
struct packet *packet,
char *reply_data,
int *reply_ofs,
int reply_len)
{
char tmp_addr[INET6_ADDRSTRLEN];
struct option_state *host_opt_state;
int len;
log_info("Client %s declines address %s, which is not offered to it.",
print_hex_1(client_id->len, client_id->data, 60),
inet_ntop(AF_INET6, iaaddr->data, tmp_addr, sizeof(tmp_addr)));
/*
* Create state for this IA_NA.
*/
host_opt_state = NULL;
if (!option_state_allocate(&host_opt_state, MDL)) {
log_error("ia_na_nomatch_decline: out of memory "
"allocating option_state.");
goto exit;
}
if (!set_status_code(STATUS_NoBinding, "Decline for unknown address.",
host_opt_state)) {
goto exit;
}
/*
* Insure we have enough space
*/
if (reply_len < (*reply_ofs + 16)) {
log_error("ia_na_nomatch_decline: "
"out of space for reply packet.");
goto exit;
}
/*
* Put our status code into the reply packet.
*/
len = store_options6(reply_data+(*reply_ofs)+16,
reply_len-(*reply_ofs)-16,
host_opt_state, packet,
required_opts_STATUS_CODE, NULL);
/*
* Store the non-encapsulated option data for this
* IA_NA into our reply packet. Defined in RFC 3315,
* section 22.4.
*/
/* option number */
putUShort((unsigned char *)reply_data+(*reply_ofs), D6O_IA_NA);
/* option length */
putUShort((unsigned char *)reply_data+(*reply_ofs)+2, len + 12);
/* IA_NA, copied from the client */
memcpy(reply_data+(*reply_ofs)+4, ia_na_id, 4);
/* t1 and t2, odd that we need them, but here it is */
putULong((unsigned char *)reply_data+(*reply_ofs)+8, 0);
putULong((unsigned char *)reply_data+(*reply_ofs)+12, 0);
/*
* Get ready for next IA_NA.
*/
*reply_ofs += (len + 16);
exit:
option_state_dereference(&host_opt_state, MDL);
}
static void
iterate_over_ia_na(struct data_string *reply_ret,
struct packet *packet,
const struct data_string *client_id,
const struct data_string *server_id,
const char *packet_type,
void (*ia_na_match)(),
void (*ia_na_nomatch)())
{
struct option_state *opt_state;
struct host_decl *packet_host;
struct option_cache *ia;
struct option_cache *oc;
/* cli_enc_... variables come from the IA_NA/IA_TA options */
struct data_string cli_enc_opt_data;
struct option_state *cli_enc_opt_state;
struct host_decl *host;
struct data_string iaaddr;
struct data_string fixed_addr;
char reply_data[65536];
struct dhcpv6_packet *reply = (struct dhcpv6_packet *)reply_data;
int reply_ofs = (int)(offsetof(struct dhcpv6_packet, options));
char status_msg[32];
struct iasubopt *lease;
struct ia_xx *existing_ia_na;
int i;
struct data_string key;
u_int32_t iaid;
/*
* Initialize to empty values, in case we have to exit early.
*/
opt_state = NULL;
memset(&cli_enc_opt_data, 0, sizeof(cli_enc_opt_data));
cli_enc_opt_state = NULL;
memset(&iaaddr, 0, sizeof(iaaddr));
memset(&fixed_addr, 0, sizeof(fixed_addr));
lease = NULL;
/*
* Find the host record that matches from the packet, if any.
*/
packet_host = NULL;
find_hosts6(&packet_host, packet, client_id, MDL);
/*
* Set our reply information.
*/
reply->msg_type = DHCPV6_REPLY;
memcpy(reply->transaction_id, packet->dhcpv6_transaction_id,
sizeof(reply->transaction_id));
/*
* Build our option state for reply.
*/
opt_state = NULL;
if (!option_state_allocate(&opt_state, MDL)) {
log_error("iterate_over_ia_na: no memory for option_state.");
goto exit;
}
execute_statements_in_scope(NULL, packet, NULL, NULL,
packet->options, opt_state,
&global_scope, root_group, NULL, NULL);
/*
* RFC 3315, section 18.2.7 tells us which options to include.
*/
oc = lookup_option(&dhcpv6_universe, opt_state, D6O_SERVERID);
if (oc == NULL) {
if (!save_option_buffer(&dhcpv6_universe, opt_state, NULL,
(unsigned char *)server_duid.data,
server_duid.len, D6O_SERVERID, 0)) {
log_error("iterate_over_ia_na: "
"error saving server identifier.");
goto exit;
}
}
if (!save_option_buffer(&dhcpv6_universe, opt_state,
client_id->buffer,
(unsigned char *)client_id->data,
client_id->len,
D6O_CLIENTID, 0)) {
log_error("iterate_over_ia_na: "
"error saving client identifier.");
goto exit;
}
snprintf(status_msg, sizeof(status_msg), "%s received.", packet_type);
if (!set_status_code(STATUS_Success, status_msg, opt_state)) {
goto exit;
}
/*
* Add our options that are not associated with any IA_NA or IA_TA.
*/
reply_ofs += store_options6(reply_data+reply_ofs,
sizeof(reply_data)-reply_ofs,
opt_state, packet,
required_opts, NULL);
/*
* Loop through the IA_NA reported by the client, and deal with
* addresses reported as already in use.
*/
for (ia = lookup_option(&dhcpv6_universe, packet->options, D6O_IA_NA);
ia != NULL; ia = ia->next) {
if (!get_encapsulated_IA_state(&cli_enc_opt_state,
&cli_enc_opt_data,
packet, ia, IA_NA_OFFSET)) {
goto exit;
}
iaid = getULong(cli_enc_opt_data.data);
/*
* XXX: It is possible that we can get multiple addresses
* sent by the client. We don't send multiple
* addresses, so this indicates a client error.
* We should check for multiple IAADDR options, log
* if found, and set as an error.
*/
oc = lookup_option(&dhcpv6_universe, cli_enc_opt_state,
D6O_IAADDR);
if (oc == NULL) {
/* no address given for this IA, ignore */
option_state_dereference(&cli_enc_opt_state, MDL);
data_string_forget(&cli_enc_opt_data, MDL);
continue;
}
memset(&iaaddr, 0, sizeof(iaaddr));
if (!evaluate_option_cache(&iaaddr, packet, NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("iterate_over_ia_na: "
"error evaluating IAADDR.");
goto exit;
}
/*
* Now we need to figure out which host record matches
* this IA_NA and IAADDR (encapsulated option contents
* matching a host record by option).
*
* XXX: We don't currently track IA_NA separately, but
* we will need to do this!
*/
host = NULL;
if (!find_hosts_by_option(&host, packet,
cli_enc_opt_state, MDL)) {
if (packet_host != NULL) {
host = packet_host;
} else {
host = NULL;
}
}
while (host != NULL) {
if (host->fixed_addr != NULL) {
if (!evaluate_option_cache(&fixed_addr, NULL,
NULL, NULL, NULL,
NULL, &global_scope,
host->fixed_addr,
MDL)) {
log_error("iterate_over_ia_na: error "
"evaluating host address.");
goto exit;
}
if ((iaaddr.len >= 16) &&
!memcmp(fixed_addr.data, iaaddr.data, 16)) {
data_string_forget(&fixed_addr, MDL);
break;
}
data_string_forget(&fixed_addr, MDL);
}
host = host->n_ipaddr;
}
if ((host == NULL) && (iaaddr.len >= IAADDR_OFFSET)) {
/*
* Find existing IA_NA.
*/
if (ia_make_key(&key, iaid,
(char *)client_id->data,
client_id->len,
MDL) != ISC_R_SUCCESS) {
log_fatal("iterate_over_ia_na: no memory for "
"key.");
}
existing_ia_na = NULL;
if (ia_hash_lookup(&existing_ia_na, ia_na_active,
(unsigned char *)key.data,
key.len, MDL)) {
/*
* Make sure this address is in the IA_NA.
*/
for (i=0; i<existing_ia_na->num_iasubopt; i++) {
struct iasubopt *tmp;
struct in6_addr *in6_addr;
tmp = existing_ia_na->iasubopt[i];
in6_addr = &tmp->addr;
if (memcmp(in6_addr,
iaaddr.data, 16) == 0) {
iasubopt_reference(&lease,
tmp, MDL);
break;
}
}
}
data_string_forget(&key, MDL);
}
if ((host != NULL) || (lease != NULL)) {
ia_na_match(client_id, &iaaddr, lease);
} else {
ia_na_nomatch(client_id, &iaaddr,
(u_int32_t *)cli_enc_opt_data.data,
packet, reply_data, &reply_ofs,
sizeof(reply_data));
}
if (lease != NULL) {
iasubopt_dereference(&lease, MDL);
}
data_string_forget(&iaaddr, MDL);
option_state_dereference(&cli_enc_opt_state, MDL);
data_string_forget(&cli_enc_opt_data, MDL);
}
/*
* Return our reply to the caller.
*/
reply_ret->len = reply_ofs;
reply_ret->buffer = NULL;
if (!buffer_allocate(&reply_ret->buffer, reply_ofs, MDL)) {
log_fatal("No memory to store reply.");
}
reply_ret->data = reply_ret->buffer->data;
memcpy(reply_ret->buffer->data, reply, reply_ofs);
exit:
if (lease != NULL) {
iasubopt_dereference(&lease, MDL);
}
if (fixed_addr.buffer != NULL) {
data_string_forget(&fixed_addr, MDL);
}
if (iaaddr.buffer != NULL) {
data_string_forget(&iaaddr, MDL);
}
if (cli_enc_opt_state != NULL) {
option_state_dereference(&cli_enc_opt_state, MDL);
}
if (cli_enc_opt_data.buffer != NULL) {
data_string_forget(&cli_enc_opt_data, MDL);
}
if (opt_state != NULL) {
option_state_dereference(&opt_state, MDL);
}
}
/*
* Decline means a client has detected that something else is using an
* address we gave it.
*
* Since we're only dealing with fixed leases for now, there's not
* much we can do, other that log the occurrence.
*
* When we start issuing addresses from pools, then we will have to
* record our declined addresses and issue another. In general with
* IPv6 there is no worry about DoS by clients exhausting space, but
* we still need to be aware of this possibility.
*/
/* TODO: IA_TA */
static void
dhcpv6_decline(struct data_string *reply, struct packet *packet) {
struct data_string client_id;
struct data_string server_id;
/*
* Validate our input.
*/
if (!valid_client_resp(packet, &client_id, &server_id)) {
return;
}
/* If the DECLINE arrived via unicast and unicast option isn't set,
* reject it per RFC 3315, Sec 18.2.7 */
if (packet->unicast == ISC_TRUE &&
is_unicast_option_defined(packet) == ISC_FALSE) {
unicast_reject(reply, packet, &client_id, &server_id);
} else {
/*
* Undefined for IA_PD.
*/
delete_option(&dhcpv6_universe, packet->options, D6O_IA_PD);
/*
* And operate on each IA_NA in this packet.
*/
iterate_over_ia_na(reply, packet, &client_id, &server_id,
"Decline", ia_na_match_decline,
ia_na_nomatch_decline);
}
data_string_forget(&server_id, MDL);
data_string_forget(&client_id, MDL);
}
static void
ia_na_match_release(const struct data_string *client_id,
const struct data_string *iaaddr,
struct iasubopt *lease)
{
char tmp_addr[INET6_ADDRSTRLEN];
log_info("Client %s releases address %s",
print_hex_1(client_id->len, client_id->data, 60),
inet_ntop(AF_INET6, iaaddr->data, tmp_addr, sizeof(tmp_addr)));
if (lease != NULL) {
release_lease6(lease->ipv6_pool, lease);
lease->ia->cltt = cur_time;
write_ia(lease->ia);
}
}
static void
ia_na_nomatch_release(const struct data_string *client_id,
const struct data_string *iaaddr,
u_int32_t *ia_na_id,
struct packet *packet,
char *reply_data,
int *reply_ofs,
int reply_len)
{
char tmp_addr[INET6_ADDRSTRLEN];
struct option_state *host_opt_state;
int len;
log_info("Client %s releases address %s, which is not leased to it.",
print_hex_1(client_id->len, client_id->data, 60),
inet_ntop(AF_INET6, iaaddr->data, tmp_addr, sizeof(tmp_addr)));
/*
* Create state for this IA_NA.
*/
host_opt_state = NULL;
if (!option_state_allocate(&host_opt_state, MDL)) {
log_error("ia_na_nomatch_release: out of memory "
"allocating option_state.");
goto exit;
}
if (!set_status_code(STATUS_NoBinding,
"Release for non-leased address.",
host_opt_state)) {
goto exit;
}
/*
* Insure we have enough space
*/
if (reply_len < (*reply_ofs + 16)) {
log_error("ia_na_nomatch_release: "
"out of space for reply packet.");
goto exit;
}
/*
* Put our status code into the reply packet.
*/
len = store_options6(reply_data+(*reply_ofs)+16,
reply_len-(*reply_ofs)-16,
host_opt_state, packet,
required_opts_STATUS_CODE, NULL);
/*
* Store the non-encapsulated option data for this
* IA_NA into our reply packet. Defined in RFC 3315,
* section 22.4.
*/
/* option number */
putUShort((unsigned char *)reply_data+(*reply_ofs), D6O_IA_NA);
/* option length */
putUShort((unsigned char *)reply_data+(*reply_ofs)+2, len + 12);
/* IA_NA, copied from the client */
memcpy(reply_data+(*reply_ofs)+4, ia_na_id, 4);
/* t1 and t2, odd that we need them, but here it is */
putULong((unsigned char *)reply_data+(*reply_ofs)+8, 0);
putULong((unsigned char *)reply_data+(*reply_ofs)+12, 0);
/*
* Get ready for next IA_NA.
*/
*reply_ofs += (len + 16);
exit:
option_state_dereference(&host_opt_state, MDL);
}
static void
ia_pd_match_release(const struct data_string *client_id,
const struct data_string *iapref,
struct iasubopt *prefix)
{
char tmp_addr[INET6_ADDRSTRLEN];
log_info("Client %s releases prefix %s/%u",
print_hex_1(client_id->len, client_id->data, 60),
inet_ntop(AF_INET6, iapref->data + 9,
tmp_addr, sizeof(tmp_addr)),
(unsigned) getUChar(iapref->data + 8));
if (prefix != NULL) {
release_lease6(prefix->ipv6_pool, prefix);
prefix->ia->cltt = cur_time;
write_ia(prefix->ia);
}
}
static void
ia_pd_nomatch_release(const struct data_string *client_id,
const struct data_string *iapref,
u_int32_t *ia_pd_id,
struct packet *packet,
char *reply_data,
int *reply_ofs,
int reply_len)
{
char tmp_addr[INET6_ADDRSTRLEN];
struct option_state *host_opt_state;
int len;
log_info("Client %s releases prefix %s/%u, which is not leased to it.",
print_hex_1(client_id->len, client_id->data, 60),
inet_ntop(AF_INET6, iapref->data + 9,
tmp_addr, sizeof(tmp_addr)),
(unsigned) getUChar(iapref->data + 8));
/*
* Create state for this IA_PD.
*/
host_opt_state = NULL;
if (!option_state_allocate(&host_opt_state, MDL)) {
log_error("ia_pd_nomatch_release: out of memory "
"allocating option_state.");
goto exit;
}
if (!set_status_code(STATUS_NoBinding,
"Release for non-leased prefix.",
host_opt_state)) {
goto exit;
}
/*
* Insure we have enough space
*/
if (reply_len < (*reply_ofs + 16)) {
log_error("ia_pd_nomatch_release: "
"out of space for reply packet.");
goto exit;
}
/*
* Put our status code into the reply packet.
*/
len = store_options6(reply_data+(*reply_ofs)+16,
reply_len-(*reply_ofs)-16,
host_opt_state, packet,
required_opts_STATUS_CODE, NULL);
/*
* Store the non-encapsulated option data for this
* IA_PD into our reply packet. Defined in RFC 3315,
* section 22.4.
*/
/* option number */
putUShort((unsigned char *)reply_data+(*reply_ofs), D6O_IA_PD);
/* option length */
putUShort((unsigned char *)reply_data+(*reply_ofs)+2, len + 12);
/* IA_PD, copied from the client */
memcpy(reply_data+(*reply_ofs)+4, ia_pd_id, 4);
/* t1 and t2, odd that we need them, but here it is */
putULong((unsigned char *)reply_data+(*reply_ofs)+8, 0);
putULong((unsigned char *)reply_data+(*reply_ofs)+12, 0);
/*
* Get ready for next IA_PD.
*/
*reply_ofs += (len + 16);
exit:
option_state_dereference(&host_opt_state, MDL);
}
static void
iterate_over_ia_pd(struct data_string *reply_ret,
struct packet *packet,
const struct data_string *client_id,
const struct data_string *server_id,
const char *packet_type,
void (*ia_pd_match)(),
void (*ia_pd_nomatch)())
{
struct data_string reply_new;
int reply_len;
struct option_state *opt_state;
struct host_decl *packet_host;
struct option_cache *ia;
struct option_cache *oc;
/* cli_enc_... variables come from the IA_PD options */
struct data_string cli_enc_opt_data;
struct option_state *cli_enc_opt_state;
struct host_decl *host;
struct data_string iaprefix;
char reply_data[65536];
int reply_ofs;
struct iasubopt *prefix;
struct ia_xx *existing_ia_pd;
int i;
struct data_string key;
u_int32_t iaid;
/*
* Initialize to empty values, in case we have to exit early.
*/
memset(&reply_new, 0, sizeof(reply_new));
opt_state = NULL;
memset(&cli_enc_opt_data, 0, sizeof(cli_enc_opt_data));
cli_enc_opt_state = NULL;
memset(&iaprefix, 0, sizeof(iaprefix));
prefix = NULL;
/*
* Compute the available length for the reply.
*/
reply_len = sizeof(reply_data) - reply_ret->len;
reply_ofs = 0;
/*
* Find the host record that matches from the packet, if any.
*/
packet_host = NULL;
find_hosts6(&packet_host, packet, client_id, MDL);
/*
* Build our option state for reply.
*/
opt_state = NULL;
if (!option_state_allocate(&opt_state, MDL)) {
log_error("iterate_over_ia_pd: no memory for option_state.");
goto exit;
}
execute_statements_in_scope(NULL, packet, NULL, NULL,
packet->options, opt_state,
&global_scope, root_group, NULL, NULL);
/*
* Loop through the IA_PD reported by the client, and deal with
* prefixes reported as already in use.
*/
for (ia = lookup_option(&dhcpv6_universe, packet->options, D6O_IA_PD);
ia != NULL; ia = ia->next) {
if (!get_encapsulated_IA_state(&cli_enc_opt_state,
&cli_enc_opt_data,
packet, ia, IA_PD_OFFSET)) {
goto exit;
}
iaid = getULong(cli_enc_opt_data.data);
oc = lookup_option(&dhcpv6_universe, cli_enc_opt_state,
D6O_IAPREFIX);
if (oc == NULL) {
/* no prefix given for this IA_PD, ignore */
option_state_dereference(&cli_enc_opt_state, MDL);
data_string_forget(&cli_enc_opt_data, MDL);
continue;
}
for (; oc != NULL; oc = oc->next) {
memset(&iaprefix, 0, sizeof(iaprefix));
if (!evaluate_option_cache(&iaprefix, packet, NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("iterate_over_ia_pd: "
"error evaluating IAPREFIX.");
goto exit;
}
/*
* Now we need to figure out which host record matches
* this IA_PD and IAPREFIX (encapsulated option contents
* matching a host record by option).
*
* XXX: We don't currently track IA_PD separately, but
* we will need to do this!
*/
host = NULL;
if (!find_hosts_by_option(&host, packet,
cli_enc_opt_state, MDL)) {
if (packet_host != NULL) {
host = packet_host;
} else {
host = NULL;
}
}
while (host != NULL) {
if (host->fixed_prefix != NULL) {
struct iaddrcidrnetlist *l;
int plen = (int) getUChar(iaprefix.data + 8);
for (l = host->fixed_prefix; l != NULL;
l = l->next) {
if (plen != l->cidrnet.bits)
continue;
if (memcmp(iaprefix.data + 9,
l->cidrnet.lo_addr.iabuf,
16) == 0)
break;
}
if ((l != NULL) && (iaprefix.len >= 17))
break;
}
host = host->n_ipaddr;
}
if ((host == NULL) && (iaprefix.len >= IAPREFIX_OFFSET)) {
/*
* Find existing IA_PD.
*/
if (ia_make_key(&key, iaid,
(char *)client_id->data,
client_id->len,
MDL) != ISC_R_SUCCESS) {
log_fatal("iterate_over_ia_pd: no memory for "
"key.");
}
existing_ia_pd = NULL;
if (ia_hash_lookup(&existing_ia_pd, ia_pd_active,
(unsigned char *)key.data,
key.len, MDL)) {
/*
* Make sure this prefix is in the IA_PD.
*/
for (i = 0;
i < existing_ia_pd->num_iasubopt;
i++) {
struct iasubopt *tmp;
u_int8_t plen;
plen = getUChar(iaprefix.data + 8);
tmp = existing_ia_pd->iasubopt[i];
if ((tmp->plen == plen) &&
(memcmp(&tmp->addr,
iaprefix.data + 9,
16) == 0)) {
iasubopt_reference(&prefix,
tmp, MDL);
break;
}
}
}
data_string_forget(&key, MDL);
}
if ((host != NULL) || (prefix != NULL)) {
ia_pd_match(client_id, &iaprefix, prefix);
} else {
ia_pd_nomatch(client_id, &iaprefix,
(u_int32_t *)cli_enc_opt_data.data,
packet, reply_data, &reply_ofs,
reply_len - reply_ofs);
}
if (prefix != NULL) {
iasubopt_dereference(&prefix, MDL);
}
data_string_forget(&iaprefix, MDL);
}
option_state_dereference(&cli_enc_opt_state, MDL);
data_string_forget(&cli_enc_opt_data, MDL);
}
/*
* Return our reply to the caller.
* The IA_NA routine has already filled at least the header.
*/
reply_new.len = reply_ret->len + reply_ofs;
if (!buffer_allocate(&reply_new.buffer, reply_new.len, MDL)) {
log_fatal("No memory to store reply.");
}
reply_new.data = reply_new.buffer->data;
memcpy(reply_new.buffer->data,
reply_ret->buffer->data, reply_ret->len);
memcpy(reply_new.buffer->data + reply_ret->len,
reply_data, reply_ofs);
data_string_forget(reply_ret, MDL);
data_string_copy(reply_ret, &reply_new, MDL);
data_string_forget(&reply_new, MDL);
exit:
if (prefix != NULL) {
iasubopt_dereference(&prefix, MDL);
}
if (iaprefix.buffer != NULL) {
data_string_forget(&iaprefix, MDL);
}
if (cli_enc_opt_state != NULL) {
option_state_dereference(&cli_enc_opt_state, MDL);
}
if (cli_enc_opt_data.buffer != NULL) {
data_string_forget(&cli_enc_opt_data, MDL);
}
if (opt_state != NULL) {
option_state_dereference(&opt_state, MDL);
}
}
/*
* Release means a client is done with the leases.
*/
static void
dhcpv6_release(struct data_string *reply, struct packet *packet) {
struct data_string client_id;
struct data_string server_id;
/*
* Validate our input.
*/
if (!valid_client_resp(packet, &client_id, &server_id)) {
return;
}
/* If the RELEASE arrived via unicast and unicast option isn't set,
* reject it per RFC 3315, Sec 18.2.6 */
if (packet->unicast == ISC_TRUE &&
is_unicast_option_defined(packet) == ISC_FALSE) {
unicast_reject(reply, packet, &client_id, &server_id);
} else {
/*
* And operate on each IA_NA in this packet.
*/
iterate_over_ia_na(reply, packet, &client_id, &server_id,
"Release", ia_na_match_release,
ia_na_nomatch_release);
/*
* And operate on each IA_PD in this packet.
*/
iterate_over_ia_pd(reply, packet, &client_id, &server_id,
"Release", ia_pd_match_release,
ia_pd_nomatch_release);
}
data_string_forget(&server_id, MDL);
data_string_forget(&client_id, MDL);
}
/*
* Information-Request is used by clients who have obtained an address
* from other means, but want configuration information from the server.
*/
static void
dhcpv6_information_request(struct data_string *reply, struct packet *packet) {
struct data_string client_id;
struct data_string server_id;
/*
* Validate our input.
*/
if (!valid_client_info_req(packet, &server_id)) {
return;
}
/*
* Get our client ID, if there is one.
*/
memset(&client_id, 0, sizeof(client_id));
if (get_client_id(packet, &client_id) != ISC_R_SUCCESS) {
data_string_forget(&client_id, MDL);
}
/*
* Use the lease_to_client() function. This will work fine,
* because the valid_client_info_req() insures that we
* don't have any IA that would cause us to allocate
* resources to the client.
*/
lease_to_client(reply, packet, &client_id,
server_id.data != NULL ? &server_id : NULL);
/*
* Cleanup.
*/
if (client_id.data != NULL) {
data_string_forget(&client_id, MDL);
}
data_string_forget(&server_id, MDL);
}
/*
* The Relay-forw message is sent by relays. It typically contains a
* single option, which encapsulates an entire packet.
*
* We need to build an encapsulated reply.
*/
/* XXX: this is very, very similar to do_packet6(), and should probably
be combined in a clever way */
/* DHCPv6 server side */
static void
dhcpv6_relay_forw(struct data_string *reply_ret, struct packet *packet) {
struct option_cache *oc;
struct data_string enc_opt_data;
struct packet *enc_packet;
unsigned char msg_type;
const struct dhcpv6_packet *msg;
const struct dhcpv6_relay_packet *relay;
struct data_string enc_reply;
char link_addr[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
char peer_addr[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
struct data_string a_opt, packet_ero;
struct option_state *opt_state;
static char reply_data[65536];
struct dhcpv6_relay_packet *reply;
int reply_ofs;
/*
* Initialize variables for early exit.
*/
opt_state = NULL;
memset(&a_opt, 0, sizeof(a_opt));
memset(&packet_ero, 0, sizeof(packet_ero));
memset(&enc_reply, 0, sizeof(enc_reply));
memset(&enc_opt_data, 0, sizeof(enc_opt_data));
enc_packet = NULL;
/*
* Get our encapsulated relay message.
*/
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_RELAY_MSG);
if (oc == NULL) {
inet_ntop(AF_INET6, &packet->dhcpv6_link_address,
link_addr, sizeof(link_addr));
inet_ntop(AF_INET6, &packet->dhcpv6_peer_address,
peer_addr, sizeof(peer_addr));
log_info("Relay-forward from %s with link address=%s and "
"peer address=%s missing Relay Message option.",
piaddr(packet->client_addr), link_addr, peer_addr);
goto exit;
}
if (!evaluate_option_cache(&enc_opt_data, NULL, NULL, NULL,
NULL, NULL, &global_scope, oc, MDL)) {
/* should be dhcpv6_relay_forw */
log_error("dhcpv6_forw_relay: error evaluating "
"relayed message.");
goto exit;
}
if (!packet6_len_okay((char *)enc_opt_data.data, enc_opt_data.len)) {
/* should be dhcpv6_relay_forw */
log_error("dhcpv6_forw_relay: encapsulated packet too short.");
goto exit;
}
/*
* Build a packet structure from this encapsulated packet.
*/
enc_packet = NULL;
if (!packet_allocate(&enc_packet, MDL)) {
/* should be dhcpv6_relay_forw */
log_error("dhcpv6_forw_relay: "
"no memory for encapsulated packet.");
goto exit;
}
if (!option_state_allocate(&enc_packet->options, MDL)) {
/* should be dhcpv6_relay_forw */
log_error("dhcpv6_forw_relay: "
"no memory for encapsulated packet's options.");
goto exit;
}
enc_packet->client_port = packet->client_port;
enc_packet->client_addr = packet->client_addr;
interface_reference(&enc_packet->interface, packet->interface, MDL);
enc_packet->dhcpv6_container_packet = packet;
msg_type = enc_opt_data.data[0];
if ((msg_type == DHCPV6_RELAY_FORW) ||
(msg_type == DHCPV6_RELAY_REPL)) {
int relaylen = (int)(offsetof(struct dhcpv6_relay_packet, options));
relay = (struct dhcpv6_relay_packet *)enc_opt_data.data;
enc_packet->dhcpv6_msg_type = relay->msg_type;
/* relay-specific data */
enc_packet->dhcpv6_hop_count = relay->hop_count;
memcpy(&enc_packet->dhcpv6_link_address,
relay->link_address, sizeof(relay->link_address));
memcpy(&enc_packet->dhcpv6_peer_address,
relay->peer_address, sizeof(relay->peer_address));
if (!parse_option_buffer(enc_packet->options,
relay->options,
enc_opt_data.len - relaylen,
&dhcpv6_universe)) {
/* no logging here, as parse_option_buffer() logs all
cases where it fails */
goto exit;
}
} else if ((msg_type == DHCPV6_DHCPV4_QUERY) ||
(msg_type == DHCPV6_DHCPV4_RESPONSE)) {
#ifdef DHCP4o6
if (!dhcpv4_over_dhcpv6 ||
(msg_type == DHCPV6_DHCPV4_RESPONSE)) {
log_error("dhcpv6_relay_forw: "
"unsupported %s message type.",
dhcpv6_type_names[msg_type]);
goto exit;
}
forw_dhcpv4_query(packet);
goto exit;
#else /* DHCP4o6 */
log_error("dhcpv6_relay_forw: unsupported %s message type.",
dhcpv6_type_names[msg_type]);
goto exit;
#endif /* DHCP4o6 */
} else {
int msglen = (int)(offsetof(struct dhcpv6_packet, options));
msg = (struct dhcpv6_packet *)enc_opt_data.data;
enc_packet->dhcpv6_msg_type = msg->msg_type;
/* message-specific data */
memcpy(enc_packet->dhcpv6_transaction_id,
msg->transaction_id,
sizeof(enc_packet->dhcpv6_transaction_id));
if (!parse_option_buffer(enc_packet->options,
msg->options,
enc_opt_data.len - msglen,
&dhcpv6_universe)) {
/* no logging here, as parse_option_buffer() logs all
cases where it fails */
goto exit;
}
}
/*
* This is recursive. It is possible to exceed maximum packet size.
* XXX: This will cause the packet send to fail.
*/
build_dhcpv6_reply(&enc_reply, enc_packet);
/*
* If we got no encapsulated data, then it is discarded, and
* our reply-forw is also discarded.
*/
if (enc_reply.data == NULL) {
goto exit;
}
/*
* Now we can use the reply_data buffer.
* Packet header stuff all comes from the forward message.
*/
reply = (struct dhcpv6_relay_packet *)reply_data;
reply->msg_type = DHCPV6_RELAY_REPL;
reply->hop_count = packet->dhcpv6_hop_count;
memcpy(reply->link_address, &packet->dhcpv6_link_address,
sizeof(reply->link_address));
memcpy(reply->peer_address, &packet->dhcpv6_peer_address,
sizeof(reply->peer_address));
reply_ofs = (int)(offsetof(struct dhcpv6_relay_packet, options));
/*
* Get the reply option state.
*/
opt_state = NULL;
if (!option_state_allocate(&opt_state, MDL)) {
log_error("dhcpv6_relay_forw: no memory for option state.");
goto exit;
}
/*
* Append the interface-id if present.
*/
oc = lookup_option(&dhcpv6_universe, packet->options,
D6O_INTERFACE_ID);
if (oc != NULL) {
if (!evaluate_option_cache(&a_opt, packet,
NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("dhcpv6_relay_forw: error evaluating "
"Interface ID.");
goto exit;
}
if (!save_option_buffer(&dhcpv6_universe, opt_state, NULL,
(unsigned char *)a_opt.data,
a_opt.len,
D6O_INTERFACE_ID, 0)) {
log_error("dhcpv6_relay_forw: error saving "
"Interface ID.");
goto exit;
}
data_string_forget(&a_opt, MDL);
}
/*
* Append our encapsulated stuff for caller.
*/
if (!save_option_buffer(&dhcpv6_universe, opt_state, NULL,
(unsigned char *)enc_reply.data,
enc_reply.len,
D6O_RELAY_MSG, 0)) {
log_error("dhcpv6_relay_forw: error saving Relay MSG.");
goto exit;
}
/*
* Get the ERO if any.
*/
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_ERO);
if (oc != NULL) {
unsigned req;
int i;
if (!evaluate_option_cache(&packet_ero, packet,
NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL) ||
(packet_ero.len & 1)) {
log_error("dhcpv6_relay_forw: error evaluating ERO.");
goto exit;
}
/* Decode and apply the ERO. */
for (i = 0; i < packet_ero.len; i += 2) {
req = getUShort(packet_ero.data + i);
/* Already in the reply? */
oc = lookup_option(&dhcpv6_universe, opt_state, req);
if (oc != NULL)
continue;
/* Get it from the packet if present. */
oc = lookup_option(&dhcpv6_universe,
packet->options,
req);
if (oc == NULL)
continue;
if (!evaluate_option_cache(&a_opt, packet,
NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("dhcpv6_relay_forw: error "
"evaluating option %u.", req);
goto exit;
}
if (!save_option_buffer(&dhcpv6_universe,
opt_state,
NULL,
(unsigned char *)a_opt.data,
a_opt.len,
req,
0)) {
log_error("dhcpv6_relay_forw: error saving "
"option %u.", req);
goto exit;
}
data_string_forget(&a_opt, MDL);
}
}
reply_ofs += store_options6(reply_data + reply_ofs,
sizeof(reply_data) - reply_ofs,
opt_state, packet,
required_opts_agent, &packet_ero);
/*
* Return our reply to the caller.
*/
reply_ret->len = reply_ofs;
reply_ret->buffer = NULL;
if (!buffer_allocate(&reply_ret->buffer, reply_ret->len, MDL)) {
log_fatal("No memory to store reply.");
}
reply_ret->data = reply_ret->buffer->data;
memcpy(reply_ret->buffer->data, reply_data, reply_ofs);
exit:
if (opt_state != NULL)
option_state_dereference(&opt_state, MDL);
if (a_opt.data != NULL) {
data_string_forget(&a_opt, MDL);
}
if (packet_ero.data != NULL) {
data_string_forget(&packet_ero, MDL);
}
if (enc_reply.data != NULL) {
data_string_forget(&enc_reply, MDL);
}
if (enc_opt_data.data != NULL) {
data_string_forget(&enc_opt_data, MDL);
}
if (enc_packet != NULL) {
packet_dereference(&enc_packet, MDL);
}
}
#ifdef DHCP4o6
/* \brief Internal processing of a relayed DHCPv4-query
* (DHCPv4 server side)
*
* Code copied from \ref dhcpv6_relay_forw() which itself is
* from \ref do_packet6().
*
* \param reply_ret pointer to the response
* \param packet the query
*/
static void
dhcp4o6_relay_forw(struct data_string *reply_ret, struct packet *packet) {
struct option_cache *oc;
struct data_string enc_opt_data;
struct packet *enc_packet;
unsigned char msg_type;
const struct dhcpv6_relay_packet *relay;
const struct dhcpv4_over_dhcpv6_packet *msg;
struct data_string enc_reply;
char link_addr[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
char peer_addr[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
struct data_string a_opt, packet_ero;
struct option_state *opt_state;
static char reply_data[65536];
struct dhcpv6_relay_packet *reply;
int reply_ofs;
/*
* Initialize variables for early exit.
*/
opt_state = NULL;
memset(&a_opt, 0, sizeof(a_opt));
memset(&packet_ero, 0, sizeof(packet_ero));
memset(&enc_reply, 0, sizeof(enc_reply));
memset(&enc_opt_data, 0, sizeof(enc_opt_data));
enc_packet = NULL;
/*
* Get our encapsulated relay message.
*/
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_RELAY_MSG);
if (oc == NULL) {
inet_ntop(AF_INET6, &packet->dhcpv6_link_address,
link_addr, sizeof(link_addr));
inet_ntop(AF_INET6, &packet->dhcpv6_peer_address,
peer_addr, sizeof(peer_addr));
log_info("Relay-forward from %s with link address=%s and "
"peer address=%s missing Relay Message option.",
piaddr(packet->client_addr), link_addr, peer_addr);
goto exit;
}
if (!evaluate_option_cache(&enc_opt_data, NULL, NULL, NULL,
NULL, NULL, &global_scope, oc, MDL)) {
log_error("dhcp4o6_relay_forw: error evaluating "
"relayed message.");
goto exit;
}
if (!packet6_len_okay((char *)enc_opt_data.data, enc_opt_data.len)) {
log_error("dhcp4o6_relay_forw: "
"encapsulated packet too short.");
goto exit;
}
/*
* Build a packet structure from this encapsulated packet.
*/
if (!packet_allocate(&enc_packet, MDL)) {
log_error("dhcp4o6_relay_forw: "
"no memory for encapsulated packet.");
goto exit;
}
if (!option_state_allocate(&enc_packet->options, MDL)) {
log_error("dhcp4o6_relay_forw: "
"no memory for encapsulated packet's options.");
goto exit;
}
enc_packet->client_port = packet->client_port;
enc_packet->client_addr = packet->client_addr;
interface_reference(&enc_packet->interface, packet->interface, MDL);
enc_packet->dhcpv6_container_packet = packet;
msg_type = enc_opt_data.data[0];
if ((msg_type == DHCPV6_RELAY_FORW) ||
(msg_type == DHCPV6_RELAY_REPL)) {
int relaylen = (int)(offsetof(struct dhcpv6_relay_packet, options));
relay = (struct dhcpv6_relay_packet *)enc_opt_data.data;
enc_packet->dhcpv6_msg_type = relay->msg_type;
/* relay-specific data */
enc_packet->dhcpv6_hop_count = relay->hop_count;
memcpy(&enc_packet->dhcpv6_link_address,
relay->link_address, sizeof(relay->link_address));
memcpy(&enc_packet->dhcpv6_peer_address,
relay->peer_address, sizeof(relay->peer_address));
if (!parse_option_buffer(enc_packet->options,
relay->options,
enc_opt_data.len - relaylen,
&dhcpv6_universe)) {
/* no logging here, as parse_option_buffer() logs all
cases where it fails */
goto exit;
}
} else if ((msg_type == DHCPV6_DHCPV4_QUERY) ||
(msg_type == DHCPV6_DHCPV4_RESPONSE)) {
int msglen =
(int)(offsetof(struct dhcpv4_over_dhcpv6_packet, options));
msg = (struct dhcpv4_over_dhcpv6_packet *)enc_opt_data.data;
enc_packet->dhcpv6_msg_type = msg->msg_type;
/* message-specific data */
memcpy(enc_packet->dhcp4o6_flags,
msg->flags,
sizeof(enc_packet->dhcp4o6_flags));
if (!parse_option_buffer(enc_packet->options,
msg->options,
enc_opt_data.len - msglen,
&dhcpv6_universe)) {
/* no logging here, as parse_option_buffer() logs all
cases where it fails */
goto exit;
}
} else {
log_error("dhcp4o6_relay_forw: unexpected message of type %d.",
(int)msg_type);
goto exit;
}
/*
* This is recursive. It is possible to exceed maximum packet size.
* XXX: This will cause the packet send to fail.
*/
build_dhcpv6_reply(&enc_reply, enc_packet);
/*
* If we got no encapsulated data, then it is discarded, and
* our reply-forw is also discarded.
*/
if (enc_reply.data == NULL) {
goto exit;
}
/*
* Now we can use the reply_data buffer.
* Packet header stuff all comes from the forward message.
*/
reply = (struct dhcpv6_relay_packet *)reply_data;
reply->msg_type = DHCPV6_RELAY_REPL;
reply->hop_count = packet->dhcpv6_hop_count;
memcpy(reply->link_address, &packet->dhcpv6_link_address,
sizeof(reply->link_address));
memcpy(reply->peer_address, &packet->dhcpv6_peer_address,
sizeof(reply->peer_address));
reply_ofs = (int)(offsetof(struct dhcpv6_relay_packet, options));
/*
* Get the reply option state.
*/
if (!option_state_allocate(&opt_state, MDL)) {
log_error("dhcp4o6_relay_forw: no memory for option state.");
goto exit;
}
/*
* Append the interface-id if present.
*/
oc = lookup_option(&dhcpv6_universe, packet->options,
D6O_INTERFACE_ID);
if (oc != NULL) {
if (!evaluate_option_cache(&a_opt, packet,
NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("dhcp4o6_relay_forw: error evaluating "
"Interface ID.");
goto exit;
}
if (!save_option_buffer(&dhcpv6_universe, opt_state, NULL,
(unsigned char *)a_opt.data,
a_opt.len,
D6O_INTERFACE_ID, 0)) {
log_error("dhcp4o6_relay_forw: error saving "
"Interface ID.");
goto exit;
}
data_string_forget(&a_opt, MDL);
}
/*
* Append our encapsulated stuff for caller.
*/
if (!save_option_buffer(&dhcpv6_universe, opt_state, NULL,
(unsigned char *)enc_reply.data,
enc_reply.len,
D6O_RELAY_MSG, 0)) {
log_error("dhcp4o6_relay_forw: error saving Relay MSG.");
goto exit;
}
/*
* Get the ERO if any.
*/
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_ERO);
if (oc != NULL) {
unsigned req;
int i;
if (!evaluate_option_cache(&packet_ero, packet,
NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL) ||
(packet_ero.len & 1)) {
log_error("dhcp4o6_relay_forw: error evaluating ERO.");
goto exit;
}
/* Decode and apply the ERO. */
for (i = 0; i < packet_ero.len; i += 2) {
req = getUShort(packet_ero.data + i);
/* Already in the reply? */
oc = lookup_option(&dhcpv6_universe, opt_state, req);
if (oc != NULL)
continue;
/* Get it from the packet if present. */
oc = lookup_option(&dhcpv6_universe,
packet->options,
req);
if (oc == NULL)
continue;
if (!evaluate_option_cache(&a_opt, packet,
NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("dhcp4o6_relay_forw: error "
"evaluating option %u.", req);
goto exit;
}
if (!save_option_buffer(&dhcpv6_universe,
opt_state,
NULL,
(unsigned char *)a_opt.data,
a_opt.len,
req,
0)) {
log_error("dhcp4o6_relay_forw: error saving "
"option %u.", req);
goto exit;
}
data_string_forget(&a_opt, MDL);
}
}
reply_ofs += store_options6(reply_data + reply_ofs,
sizeof(reply_data) - reply_ofs,
opt_state, packet,
required_opts_agent, &packet_ero);
/*
* Return our reply to the caller.
*/
reply_ret->len = reply_ofs;
reply_ret->buffer = NULL;
if (!buffer_allocate(&reply_ret->buffer, reply_ret->len, MDL)) {
log_fatal("No memory to store reply.");
}
reply_ret->data = reply_ret->buffer->data;
memcpy(reply_ret->buffer->data, reply_data, reply_ofs);
exit:
if (opt_state != NULL)
option_state_dereference(&opt_state, MDL);
if (a_opt.data != NULL) {
data_string_forget(&a_opt, MDL);
}
if (packet_ero.data != NULL) {
data_string_forget(&packet_ero, MDL);
}
if (enc_reply.data != NULL) {
data_string_forget(&enc_reply, MDL);
}
if (enc_opt_data.data != NULL) {
data_string_forget(&enc_opt_data, MDL);
}
if (enc_packet != NULL) {
packet_dereference(&enc_packet, MDL);
}
}
/*
* \brief Internal processing of a DHCPv4-query
* (DHCPv4 server function)
*
* Code copied from \ref do_packet().
*
* \param reply_ret pointer to the response
* \param packet the query
*/
static void
dhcp4o6_dhcpv4_query(struct data_string *reply_ret, struct packet *packet) {
struct option_cache *oc;
struct data_string enc_opt_data;
struct packet *enc_packet;
struct data_string enc_response;
struct option_state *opt_state;
static char response_data[65536];
struct dhcpv4_over_dhcpv6_packet *response;
int response_ofs;
/*
* Initialize variables for early exit.
*/
opt_state = NULL;
memset(&enc_response, 0, sizeof(enc_response));
memset(&enc_opt_data, 0, sizeof(enc_opt_data));
enc_packet = NULL;
/*
* Get our encapsulated relay message.
*/
oc = lookup_option(&dhcpv6_universe, packet->options, D6O_DHCPV4_MSG);
if (oc == NULL) {
log_info("DHCPv4-query from %s missing DHCPv4 Message option.",
piaddr(packet->client_addr));
goto exit;
}
if (!evaluate_option_cache(&enc_opt_data, NULL, NULL, NULL,
NULL, NULL, &global_scope, oc, MDL)) {
log_error("dhcp4o6_dhcpv4_query: error evaluating "
"DHCPv4 message.");
goto exit;
}
if (enc_opt_data.len < DHCP_FIXED_NON_UDP) {
log_error("dhcp4o6_dhcpv4_query: DHCPv4 packet too short.");
goto exit;
}
/*
* Build a packet structure from this encapsulated packet.
*/
if (!packet_allocate(&enc_packet, MDL)) {
log_error("dhcp4o6_dhcpv4_query: "
"no memory for encapsulated packet.");
goto exit;
}
enc_packet->raw = (struct dhcp_packet *)enc_opt_data.data;
enc_packet->packet_length = enc_opt_data.len;
enc_packet->dhcp4o6_response = &enc_response;
enc_packet->client_port = packet->client_port;
enc_packet->client_addr = packet->client_addr;
interface_reference(&enc_packet->interface, packet->interface, MDL);
enc_packet->dhcpv6_container_packet = packet;
if (packet->dhcp4o6_flags[0] & DHCP4O6_QUERY_UNICAST)
enc_packet->unicast = 1;
if (enc_packet->raw->hlen > sizeof(enc_packet->raw->chaddr)) {
log_info("dhcp4o6_dhcpv4_query: "
"discarding packet with bogus hlen.");
goto exit;
}
/* Allocate packet->options now so it is non-null for all packets */
if (!option_state_allocate (&enc_packet->options, MDL)) {
log_error("dhcp4o6_dhcpv4_query: no memory for options.");
goto exit;
}
/* If there's an option buffer, try to parse it. */
if (enc_packet->packet_length >= DHCP_FIXED_NON_UDP + 4) {
struct option_cache *op;
if (!parse_options(enc_packet)) {
if (enc_packet->options)
option_state_dereference
(&enc_packet->options, MDL);
packet_dereference (&enc_packet, MDL);
goto exit;
}
if (enc_packet->options_valid &&
(op = lookup_option(&dhcp_universe,
enc_packet->options,
DHO_DHCP_MESSAGE_TYPE))) {
struct data_string dp;
memset(&dp, 0, sizeof dp);
evaluate_option_cache(&dp, enc_packet, NULL, NULL,
enc_packet->options, NULL,
NULL, op, MDL);
if (dp.len > 0)
enc_packet->packet_type = dp.data[0];
else
enc_packet->packet_type = 0;
data_string_forget(&dp, MDL);
}
}
if (validate_packet(enc_packet) != 0) {
if (enc_packet->packet_type)
dhcp(enc_packet);
else
bootp(enc_packet);
}
/* If the caller kept the packet, they'll have upped the refcnt. */
packet_dereference(&enc_packet, MDL);
/*
* If we got no response data, then it is discarded, and
* our DHCPv4-response is also discarded.
*/
if (enc_response.data == NULL) {
goto exit;
}
/*
* Now we can use the response_data buffer.
*/
response = (struct dhcpv4_over_dhcpv6_packet *)response_data;
response->msg_type = DHCPV6_DHCPV4_RESPONSE;
response->flags[0] = response->flags[1] = response->flags[2] = 0;
response_ofs =
(int)(offsetof(struct dhcpv4_over_dhcpv6_packet, options));
/*
* Get the response option state.
*/
if (!option_state_allocate(&opt_state, MDL)) {
log_error("dhcp4o6_dhcpv4_query: no memory for option state.");
goto exit;
}
/*
* Append our encapsulated stuff for caller.
*/
if (!save_option_buffer(&dhcpv6_universe, opt_state, NULL,
(unsigned char *)enc_response.data,
enc_response.len,
D6O_DHCPV4_MSG, 0)) {
log_error("dhcp4o6_dhcpv4_query: error saving DHCPv4 MSG.");
goto exit;
}
response_ofs += store_options6(response_data + response_ofs,
sizeof(response_data) - response_ofs,
opt_state, packet,
required_opts_4o6, NULL);
/*
* Return our response to the caller.
*/
reply_ret->len = response_ofs;
reply_ret->buffer = NULL;
if (!buffer_allocate(&reply_ret->buffer, reply_ret->len, MDL)) {
log_fatal("dhcp4o6_dhcpv4_query: no memory to store reply.");
}
reply_ret->data = reply_ret->buffer->data;
memcpy(reply_ret->buffer->data, response_data, response_ofs);
exit:
if (opt_state != NULL)
option_state_dereference(&opt_state, MDL);
if (enc_response.data != NULL) {
data_string_forget(&enc_response, MDL);
}
if (enc_opt_data.data != NULL) {
data_string_forget(&enc_opt_data, MDL);
}
if (enc_packet != NULL) {
packet_dereference(&enc_packet, MDL);
}
}
/*
* \brief Forward a DHCPv4-query message to the DHCPv4 side
* (DHCPv6 server function)
*
* Format: interface:16 + address:16 + DHCPv6 DHCPv4-query message
*
* \brief packet the DHCPv6 DHCPv4-query message
*/
static void forw_dhcpv4_query(struct packet *packet) {
struct data_string ds;
unsigned len;
int cc;
/* Get the initial message. */
while (packet->dhcpv6_container_packet != NULL)
packet = packet->dhcpv6_container_packet;
/* Check the initial message. */
if ((packet->raw == NULL) ||
(packet->client_addr.len != 16) ||
(packet->interface == NULL)) {
log_error("forw_dhcpv4_query: can't find initial message.");
return;
}
/* Get a buffer. */
len = packet->packet_length + 32;
memset(&ds, 0, sizeof(ds));
if (!buffer_allocate(&ds.buffer, len, MDL)) {
log_error("forw_dhcpv4_query: "
"no memory for encapsulating packet.");
return;
}
ds.data = ds.buffer->data;
ds.len = len;
/* Fill the buffer. */
strncpy((char *)ds.buffer->data, packet->interface->name, 16);
memcpy(ds.buffer->data + 16,
packet->client_addr.iabuf, 16);
memcpy(ds.buffer->data + 32,
(unsigned char *)packet->raw,
packet->packet_length);
/* Forward to the DHCPv4 server. */
cc = send(dhcp4o6_fd, ds.data, ds.len, 0);
if (cc < 0)
log_error("forw_dhcpv4_query: send(): %m");
data_string_forget(&ds, MDL);
}
#endif
static void
dhcpv6_discard(struct packet *packet) {
/* INSIST(packet->msg_type > 0); */
/* INSIST(packet->msg_type < dhcpv6_type_name_max); */
log_debug("Discarding %s from %s; message type not handled by server",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr));
}
static void
build_dhcpv6_reply(struct data_string *reply, struct packet *packet) {
memset(reply, 0, sizeof(*reply));
/* I would like to classify the client once here, but
* as I don't want to classify all of the incoming packets
* I need to do it before handling specific types.
* We don't need to classify if we are tossing the packet
* or if it is a relay - the classification step will get
* done when we process the inner client packet.
*/
switch (packet->dhcpv6_msg_type) {
case DHCPV6_SOLICIT:
classify_client(packet);
dhcpv6_solicit(reply, packet);
break;
case DHCPV6_ADVERTISE:
dhcpv6_discard(packet);
break;
case DHCPV6_REQUEST:
classify_client(packet);
dhcpv6_request(reply, packet);
break;
case DHCPV6_CONFIRM:
classify_client(packet);
dhcpv6_confirm(reply, packet);
break;
case DHCPV6_RENEW:
classify_client(packet);
dhcpv6_renew(reply, packet);
break;
case DHCPV6_REBIND:
classify_client(packet);
dhcpv6_rebind(reply, packet);
break;
case DHCPV6_REPLY:
dhcpv6_discard(packet);
break;
case DHCPV6_RELEASE:
classify_client(packet);
dhcpv6_release(reply, packet);
break;
case DHCPV6_DECLINE:
classify_client(packet);
dhcpv6_decline(reply, packet);
break;
case DHCPV6_RECONFIGURE:
dhcpv6_discard(packet);
break;
case DHCPV6_INFORMATION_REQUEST:
classify_client(packet);
dhcpv6_information_request(reply, packet);
break;
case DHCPV6_RELAY_FORW:
#ifdef DHCP4o6
if (dhcpv4_over_dhcpv6 && (local_family == AF_INET))
dhcp4o6_relay_forw(reply, packet);
else
#endif /* DHCP4o6 */
dhcpv6_relay_forw(reply, packet);
break;
case DHCPV6_RELAY_REPL:
dhcpv6_discard(packet);
break;
case DHCPV6_LEASEQUERY:
classify_client(packet);
dhcpv6_leasequery(reply, packet);
break;
case DHCPV6_LEASEQUERY_REPLY:
dhcpv6_discard(packet);
break;
case DHCPV6_DHCPV4_QUERY:
#ifdef DHCP4o6
if (dhcpv4_over_dhcpv6) {
if (local_family == AF_INET6) {
forw_dhcpv4_query(packet);
} else {
dhcp4o6_dhcpv4_query(reply, packet);
}
} else
#endif /* DHCP4o6 */
dhcpv6_discard(packet);
break;
case DHCPV6_DHCPV4_RESPONSE:
dhcpv6_discard(packet);
break;
default:
/* XXX: would be nice if we had "notice" level,
as syslog, for this */
log_info("Discarding unknown DHCPv6 message type %d "
"from %s", packet->dhcpv6_msg_type,
piaddr(packet->client_addr));
}
}
static void
log_packet_in(const struct packet *packet) {
struct data_string s;
u_int32_t tid;
char tmp_addr[INET6_ADDRSTRLEN];
const void *addr;
memset(&s, 0, sizeof(s));
if (packet->dhcpv6_msg_type < dhcpv6_type_name_max) {
data_string_sprintfa(&s, "%s message from %s port %d",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr),
ntohs(packet->client_port));
} else {
data_string_sprintfa(&s,
"Unknown message type %d from %s port %d",
packet->dhcpv6_msg_type,
piaddr(packet->client_addr),
ntohs(packet->client_port));
}
if ((packet->dhcpv6_msg_type == DHCPV6_RELAY_FORW) ||
(packet->dhcpv6_msg_type == DHCPV6_RELAY_REPL)) {
addr = &packet->dhcpv6_link_address;
data_string_sprintfa(&s, ", link address %s",
inet_ntop(AF_INET6, addr,
tmp_addr, sizeof(tmp_addr)));
addr = &packet->dhcpv6_peer_address;
data_string_sprintfa(&s, ", peer address %s",
inet_ntop(AF_INET6, addr,
tmp_addr, sizeof(tmp_addr)));
} else if ((packet->dhcpv6_msg_type != DHCPV6_DHCPV4_QUERY) &&
(packet->dhcpv6_msg_type != DHCPV6_DHCPV4_RESPONSE)) {
tid = 0;
memcpy(((char *)&tid)+1, packet->dhcpv6_transaction_id, 3);
data_string_sprintfa(&s, ", transaction ID 0x%06X", tid);
/*
oc = lookup_option(&dhcpv6_universe, packet->options,
D6O_CLIENTID);
if (oc != NULL) {
memset(&tmp_ds, 0, sizeof(tmp_ds_));
if (!evaluate_option_cache(&tmp_ds, packet, NULL, NULL,
packet->options, NULL,
&global_scope, oc, MDL)) {
log_error("Error evaluating Client Identifier");
} else {
data_strint_sprintf(&s, ", client ID %s",
data_string_forget(&tmp_ds, MDL);
}
}
*/
}
log_info("%s", s.data);
data_string_forget(&s, MDL);
}
void
dhcpv6(struct packet *packet) {
struct data_string reply;
struct sockaddr_in6 to_addr;
int send_ret;
/*
* Log a message that we received this packet.
*/
log_packet_in(packet);
/*
* Build our reply packet.
*/
build_dhcpv6_reply(&reply, packet);
if (reply.data != NULL) {
/*
* Send our reply, if we have one.
*/
memset(&to_addr, 0, sizeof(to_addr));
to_addr.sin6_family = AF_INET6;
if ((packet->dhcpv6_msg_type == DHCPV6_RELAY_FORW) ||
(packet->dhcpv6_msg_type == DHCPV6_RELAY_REPL)) {
to_addr.sin6_port = local_port;
} else {
to_addr.sin6_port = remote_port;
}
#if defined (REPLY_TO_SOURCE_PORT)
/*
* This appears to have been included for testing so we would
* not need a root client, but was accidently left in the
* final code. We continue to include it in case
* some users have come to rely upon it, but leave
* it off by default as it's a bad idea.
*/
to_addr.sin6_port = packet->client_port;
#endif
memcpy(&to_addr.sin6_addr, packet->client_addr.iabuf,
sizeof(to_addr.sin6_addr));
log_info("Sending %s to %s port %d",
dhcpv6_type_names[reply.data[0]],
piaddr(packet->client_addr),
ntohs(to_addr.sin6_port));
send_ret = send_packet6(packet->interface,
reply.data, reply.len, &to_addr);
if (send_ret != reply.len) {
log_error("dhcpv6: send_packet6() sent %d of %d bytes",
send_ret, reply.len);
}
data_string_forget(&reply, MDL);
}
}
#ifdef DHCP4o6
/*
* \brief Receive a DHCPv4-query message from the DHCPv6 side
* (DHCPv4 server function)
*
* Receive a message with a DHCPv4-query inside from the DHCPv6 server.
* (code copied from \ref do_packet6() \ref and dhcpv6())
*
* Format: interface:16 + address:16 + DHCPv6 DHCPv4-query message
*
* \param raw the DHCPv6 DHCPv4-query message raw content
*/
static void recv_dhcpv4_query(struct data_string *raw) {
struct interface_info *ip;
char name[16 + 1];
struct iaddr iaddr;
struct packet *packet;
unsigned char msg_type;
const struct dhcpv6_relay_packet *relay;
const struct dhcpv4_over_dhcpv6_packet *msg;
struct data_string reply;
struct data_string ds;
unsigned len;
int cc;
memset(name, 0, sizeof(name));
memcpy(name, raw->data, 16);
for (ip = interfaces; ip != NULL; ip = ip->next) {
if (!strcmp(name, ip->name))
break;
}
if (ip == NULL) {
log_error("recv_dhcpv4_query: can't find interface %s.",
name);
return;
}
iaddr.len = 16;
memcpy(iaddr.iabuf, raw->data + 16, 16);
/*
* From do_packet6().
*/
if (!packet6_len_okay((char *)raw->data + 32, raw->len - 32)) {
log_error("recv_dhcpv4_query: "
"short packet from %s, len %d, dropped",
piaddr(iaddr), raw->len - 32);
return;
}
/*
* Build a packet structure.
*/
packet = NULL;
if (!packet_allocate(&packet, MDL)) {
log_error("recv_dhcpv4_query: no memory for packet.");
return;
}
if (!option_state_allocate(&packet->options, MDL)) {
log_error("recv_dhcpv4_query: no memory for options.");
packet_dereference(&packet, MDL);
return;
}
packet->raw = (struct dhcp_packet *)(raw->data + 32);
packet->packet_length = raw->len - 32;
packet->client_port = remote_port;
packet->client_addr = iaddr;
interface_reference(&packet->interface, ip, MDL);
msg_type = raw->data[32];
if ((msg_type == DHCPV6_RELAY_FORW) ||
(msg_type == DHCPV6_RELAY_REPL)) {
int relaylen =
(int)(offsetof(struct dhcpv6_relay_packet, options));
relay = (const struct dhcpv6_relay_packet *)(raw->data + 32);
packet->dhcpv6_msg_type = relay->msg_type;
/* relay-specific data */
packet->dhcpv6_hop_count = relay->hop_count;
memcpy(&packet->dhcpv6_link_address,
relay->link_address, sizeof(relay->link_address));
memcpy(&packet->dhcpv6_peer_address,
relay->peer_address, sizeof(relay->peer_address));
if (!parse_option_buffer(packet->options,
relay->options,
raw->len - 32 - relaylen,
&dhcpv6_universe)) {
/* no logging here, as parse_option_buffer() logs all
cases where it fails */
packet_dereference(&packet, MDL);
return;
}
} else if ((msg_type == DHCPV6_DHCPV4_QUERY) ||
(msg_type == DHCPV6_DHCPV4_RESPONSE)) {
int msglen =
(int)(offsetof(struct dhcpv4_over_dhcpv6_packet, options));
msg = (struct dhcpv4_over_dhcpv6_packet *)(raw->data + 32);
packet->dhcpv6_msg_type = msg->msg_type;
/* message-specific data */
memcpy(packet->dhcp4o6_flags, msg->flags,
sizeof(packet->dhcp4o6_flags));
if (!parse_option_buffer(packet->options,
msg->options,
raw->len - 32 - msglen,
&dhcpv6_universe)) {
/* no logging here, as parse_option_buffer() logs all
cases where it fails */
packet_dereference(&packet, MDL);
return;
}
} else {
log_error("recv_dhcpv4_query: unexpected message of type %d.",
(int)msg_type);
packet_dereference(&packet, MDL);
return;
}
/*
* From dhcpv6().
*/
/*
* Log a message that we received this packet.
*/
/* log_packet_in(packet); */
memset(&ds, 0, sizeof(ds));
if (packet->dhcpv6_msg_type < dhcpv6_type_name_max) {
data_string_sprintfa(&ds, "%s message from %s",
dhcpv6_type_names[packet->dhcpv6_msg_type],
piaddr(packet->client_addr));
} else {
data_string_sprintfa(&ds,
"Unknown message type %d from %s",
packet->dhcpv6_msg_type,
piaddr(packet->client_addr));
}
if ((packet->dhcpv6_msg_type == DHCPV6_RELAY_FORW) ||
(packet->dhcpv6_msg_type == DHCPV6_RELAY_REPL)) {
char tmp_addr[INET6_ADDRSTRLEN];
const void *addr;
addr = &packet->dhcpv6_link_address;
data_string_sprintfa(&ds, ", link address %s",
inet_ntop(AF_INET6, addr,
tmp_addr, sizeof(tmp_addr)));
addr = &packet->dhcpv6_peer_address;
data_string_sprintfa(&ds, ", peer address %s",
inet_ntop(AF_INET6, addr,
tmp_addr, sizeof(tmp_addr)));
} else if ((packet->dhcpv6_msg_type != DHCPV6_DHCPV4_QUERY) &&
(packet->dhcpv6_msg_type != DHCPV6_DHCPV4_RESPONSE)) {
u_int32_t tid = 0;
memcpy(((char *)&tid)+1, packet->dhcpv6_transaction_id, 3);
data_string_sprintfa(&ds, ", transaction ID 0x%06X", tid);
}
log_info("%s", ds.data);
data_string_forget(&ds, MDL);
/*
* Build our reply packet.
*/
build_dhcpv6_reply(&reply, packet);
packet_dereference(&packet, MDL);
if (reply.data == NULL)
return;
/*
* Forward the response.
*/
len = reply.len + 32;
memset(&ds, 0, sizeof(ds));
if (!buffer_allocate(&ds.buffer, len, MDL)) {
log_error("recv_dhcpv4_query: no memory.");
return;
}
ds.data = ds.buffer->data;
ds.len = len;
memcpy(ds.buffer->data, name, 16);
memcpy(ds.buffer->data + 16, iaddr.iabuf, 16);
memcpy(ds.buffer->data + 32, reply.data, reply.len);
cc = send(dhcp4o6_fd, ds.data, ds.len, 0);
if (cc < 0)
log_error("recv_dhcpv4_query: send(): %m");
data_string_forget(&ds, MDL);
}
#endif /* DHCP4o6 */
static void
seek_shared_host(struct host_decl **hp, struct shared_network *shared) {
struct host_decl *nofixed = NULL;
struct host_decl *seek, *hold = NULL;
/*
* Seek forward through fixed addresses for the right link.
*
* Note: how to do this for fixed prefixes???
*/
host_reference(&hold, *hp, MDL);
host_dereference(hp, MDL);
seek = hold;
while (seek != NULL) {
if (seek->fixed_addr == NULL)
nofixed = seek;
else if (fixed_matches_shared(seek, shared))
break;
seek = seek->n_ipaddr;
}
if ((seek == NULL) && (nofixed != NULL))
seek = nofixed;
if (seek != NULL)
host_reference(hp, seek, MDL);
}
static isc_boolean_t
fixed_matches_shared(struct host_decl *host, struct shared_network *shared) {
struct subnet *subnet;
struct data_string addr;
isc_boolean_t matched;
struct iaddr fixed;
if (host->fixed_addr == NULL)
return ISC_FALSE;
memset(&addr, 0, sizeof(addr));
if (!evaluate_option_cache(&addr, NULL, NULL, NULL, NULL, NULL,
&global_scope, host->fixed_addr, MDL))
return ISC_FALSE;
if (addr.len < 16) {
data_string_forget(&addr, MDL);
return ISC_FALSE;
}
fixed.len = 16;
memcpy(fixed.iabuf, addr.data, 16);
matched = ISC_FALSE;
for (subnet = shared->subnets ; subnet != NULL ;
subnet = subnet->next_sibling) {
if (addr_eq(subnet_number(fixed, subnet->netmask),
subnet->net)) {
matched = ISC_TRUE;
break;
}
}
data_string_forget(&addr, MDL);
return matched;
}
/*!
*
* \brief Constructs a REPLY with status of UseMulticast to a given packet
*
* Per RFC 3315 Secs 18.2.1,3,6 & 7, when a server rejects a client's
* unicast-sent packet, the response must only contain the client id,
* server id, and a status code option of 5 (UseMulticast). This function
* constructs such a packet and returns it as a data_string.
*
* \param reply_ret = data_string which will receive the newly constructed
* reply
* \param packet = client request which is being rejected
* \param client_id = data_string which contains the client id
* \param server_id = data_string which which contains the server id
*
*/
void
unicast_reject(struct data_string *reply_ret,
struct packet *packet,
const struct data_string *client_id,
const struct data_string *server_id)
{
struct reply_state reply;
memset(&reply, 0x0, sizeof(struct reply_state));
/* Locate the client. */
if (shared_network_from_packet6(&reply.shared, packet)
!= ISC_R_SUCCESS) {
log_error("unicast_reject: could not locate client.");
return;
}
/* Initialize the reply. */
packet_reference(&reply.packet, packet, MDL);
data_string_copy(&reply.client_id, client_id, MDL);
if (start_reply(packet, client_id, server_id, &reply.opt_state,
&reply.buf.reply)) {
/* Set the UseMulticast status code. */
if (!set_status_code(STATUS_UseMulticast,
"Unicast not allowed by server.",
reply.opt_state)) {
log_error("unicast_reject: Unable to set status code.");
} else {
/* Set write cursor to just past the reply header. */
reply.cursor = REPLY_OPTIONS_INDEX;
reply.cursor += store_options6(((char *)reply.buf.data
+ reply.cursor),
(sizeof(reply.buf)
- reply.cursor),
reply.opt_state,
reply.packet,
unicast_reject_opts,
NULL);
/* Return our reply to the caller. */
reply_ret->len = reply.cursor;
reply_ret->buffer = NULL;
if (!buffer_allocate(&reply_ret->buffer,
reply.cursor, MDL)) {
log_fatal("unicast_reject:"
"No memory to store Reply.");
}
memcpy(reply_ret->buffer->data, reply.buf.data,
reply.cursor);
reply_ret->data = reply_ret->buffer->data;
}
}
/* Cleanup. */
if (reply.shared != NULL)
shared_network_dereference(&reply.shared, MDL);
if (reply.opt_state != NULL)
option_state_dereference(&reply.opt_state, MDL);
if (reply.packet != NULL)
packet_dereference(&reply.packet, MDL);
if (reply.client_id.data != NULL)
data_string_forget(&reply.client_id, MDL);
}
/*!
*
* \brief Checks if the dhcp6.unicast option has been defined
*
* Scans the option space for the presence of the dhcp6.unicast option. The
* function attempts to map the inbound packet to a shared network first
* by an ip address specified via an D6O_IA_XX option and if that fails then
* by the packet's source information (e.g. relay link, link, or interace).
* Once the packet is mapped to a shared network, the function executes all
* statements from the network's group outward into a local option cache.
* The option cache is then scanned for the presence of unicast option. If
* the packet cannot be mapped to a shared network, the function returns
* ISC_FALSE.
* \param packet inbound packet from the client
*
* \return ISC_TRUE if the dhcp6.unicast option is defined, false otherwise.
*
*/
isc_boolean_t
is_unicast_option_defined(struct packet *packet) {
isc_boolean_t is_defined = ISC_FALSE;
struct option_state *opt_state = NULL;
struct option_cache *oc = NULL;
struct shared_network *shared = NULL;
if (!option_state_allocate(&opt_state, MDL)) {
log_fatal("is_unicast_option_defined:"
"No memory for option state.");
}
/* We try to map the packet to a network first by an IA_XX value.
* If that fails, we try by packet source. */
if (((shared_network_from_requested_addr(&shared, packet)
!= ISC_R_SUCCESS) &&
(shared_network_from_packet6(&shared, packet) != ISC_R_SUCCESS))
|| (shared == NULL)) {
/* @todo what would this really mean? I think wrong network
* logic will catch it */
log_error("is_unicast_option_defined:"
"cannot attribute packet to a network.");
return (ISC_FALSE);
}
/* Now that we've mapped it to a network, execute statments to that
* scope, looking for the unicast option. We don't care about the
* value of the option, only whether or not it is defined. */
execute_statements_in_scope(NULL, NULL, NULL, NULL, NULL, opt_state,
&global_scope, shared->group, NULL, NULL);
oc = lookup_option(&dhcpv6_universe, opt_state, D6O_UNICAST);
is_defined = (oc != NULL ? ISC_TRUE : ISC_FALSE);
log_debug("is_unicast_option_defined: option found : %d", is_defined);
if (shared != NULL) {
shared_network_dereference(&shared, MDL);
}
if (opt_state != NULL) {
option_state_dereference(&opt_state, MDL);
}
return (is_defined);
}
/*!
*
* \brief Maps a packet to a shared network based on the requested IP address
*
* The function attempts to find a subnet that matches the first requested IP
* address contained within the given packet. Note that it looks first for
* D6O_IA_NAs, then D6O_IA_PDs and lastly D6O_IA_TAs. If a matching network is
* found, a reference to it is returned in the parameter, shared.
*
* \param shared shared_network pointer which will receive the matching network
* \param packet inbound packet from the client
*
* \return ISC_R_SUCCESS if the packet can be mapped to a shared_network.
*
*/
static isc_result_t
shared_network_from_requested_addr (struct shared_network **shared,
struct packet* packet) {
struct iaddr iaddr;
struct subnet* subnet = NULL;
isc_result_t status = ISC_R_FAILURE;
/* Try to match first IA_ address or prefix we find to a subnet. In
* theory all IA_ values in a given request are supposed to be in the
* same subnet so we only need to try one right? */
if ((get_first_ia_addr_val(packet, D6O_IA_NA, &iaddr) != ISC_R_SUCCESS)
&& (get_first_ia_addr_val(packet, D6O_IA_PD, &iaddr)
!= ISC_R_SUCCESS)
&& (get_first_ia_addr_val(packet, D6O_IA_TA, &iaddr)
!= ISC_R_SUCCESS)) {
/* we found nothing to match against */
log_debug("share_network_from_request_addr: nothing to match");
return (ISC_R_FAILURE);
}
if (!find_subnet(&subnet, iaddr, MDL)) {
log_debug("shared_network_from_requested_addr:"
"No subnet found for addr %s.", piaddr(iaddr));
} else {
status = shared_network_reference(shared,
subnet->shared_network, MDL);
subnet_dereference(&subnet, MDL);
log_debug("shared_network_from_requested_addr:"
" found shared network %s for address %s.",
((*shared)->name ? (*shared)->name : "unnamed"),
piaddr(iaddr));
return (status);
}
return (ISC_R_FAILURE);
}
/*!
*
* \brief Retrieves the first IP address from a given packet of a given type
*
* Search a packet for options of a given type (D6O_IA_AN, D6O_IA_PD, or
* D6O_IA_TA) for the first non-blank IA_XX value and return its IP address
* component.
*
* \param packet packet received from the client
* \param addr_type the address option type (D6O_IA_NA , D6O_IA_PD, or
* D6O_IP_TA) to look for within the packet.
* \param iaddr pointer to the iaddr structure which will receive the extracted
* address.
*
* \return ISC_R_SUCCESS if an address was succesfully extracted, ISC_R_FALURE
* otherwise.
*
*/
static isc_result_t
get_first_ia_addr_val (struct packet* packet, int addr_type,
struct iaddr* iaddr) {
struct option_cache *ia;
struct option_cache *oc = NULL;
struct data_string cli_enc_opt_data;
struct option_state *cli_enc_opt_state;
int addr_opt_offset;
int addr_opt;
int addr_opt_data_len;
int ip_addr_offset;
isc_result_t status = ISC_R_FAILURE;
memset(iaddr, 0, sizeof(struct iaddr));
/* Set up address type specifics */
switch (addr_type) {
case D6O_IA_NA:
addr_opt_offset = IA_NA_OFFSET;
addr_opt = D6O_IAADDR;
addr_opt_data_len = 24;
ip_addr_offset = 0;
break;
case D6O_IA_TA:
addr_opt_offset = IA_TA_OFFSET;
addr_opt = D6O_IAADDR;
addr_opt_data_len = 24;
ip_addr_offset = 0;
break;
case D6O_IA_PD:
addr_opt_offset = IA_PD_OFFSET;
addr_opt = D6O_IAPREFIX;
addr_opt_data_len = 25;
ip_addr_offset = 9;
break;
default:
/* shouldn't be here */
log_error ("get_first_ia_addr_val: invalid opt type %d",
addr_type);
return (ISC_R_FAILURE);
}
/* Find the first, non-blank IA_XX value within an D6O_IA_XX option. */
for (ia = lookup_option(&dhcpv6_universe, packet->options, addr_type);
ia != NULL && oc == NULL; ia = ia->next) {
if (!get_encapsulated_IA_state(&cli_enc_opt_state,
&cli_enc_opt_data,
packet, ia, addr_opt_offset)) {
log_debug ("get_first_ia_addr_val:"
" couldn't unroll enclosing option");
return (ISC_R_FAILURE);
}
oc = lookup_option(&dhcpv6_universe, cli_enc_opt_state,
addr_opt);
if (oc == NULL) {
/* no address given for this IA, ignore */
option_state_dereference(&cli_enc_opt_state, MDL);
data_string_forget(&cli_enc_opt_data, MDL);
}
}
/* If we found a non-blank IA_XX then extract its ip address. */
if (oc != NULL) {
struct data_string iaddr_str;
memset(&iaddr_str, 0, sizeof(iaddr_str));
if (!evaluate_option_cache(&iaddr_str, packet, NULL, NULL,
packet->options, NULL, &global_scope,
oc, MDL)) {
log_error("get_first_ia_addr_val: "
"error evaluating IA_XX option.");
} else {
if (iaddr_str.len != addr_opt_data_len) {
log_error("shared_network_from_requested_addr:"
" invalid length %d, expected %d",
iaddr_str.len, addr_opt_data_len);
} else {
iaddr->len = 16;
memcpy (iaddr->iabuf,
iaddr_str.data + ip_addr_offset, 16);
status = ISC_R_SUCCESS;
}
data_string_forget(&iaddr_str, MDL);
}
option_state_dereference(&cli_enc_opt_state, MDL);
data_string_forget(&cli_enc_opt_data, MDL);
}
return (status);
}
/*
* \brief Calculates the reply T1/T2 times and stuffs them in outbound buffer
*
* T1/T2 time selection is kind of weird. We actually use DHCP * (v4) scoped
* options, dhcp-renewal-time and dhcp-rebinding-time, as handy existing places
* where these can be configured by an administrator. A value of zero tells the
* client it may choose its own value.
*
* When those options are not defined, the values will be set to zero unless
* the global option, dhcpv6-set-tee-times is enabled. When this option is
* enabled the values are calculated as recommended by RFC 3315, Section 22.4:
*
* T1 will be set to 0.5 times the shortest preferred lifetime
* in the IA_XX option. If the "shortest" preferred lifetime is
* 0xFFFFFFFF, T1 will set to 0xFFFFFFFF.
*
* T2 will be set to 0.8 times the shortest preferred lifetime
* in the IA_XX option. If the "shortest" preferred lifetime is
* 0xFFFFFFFF, T2 will set to 0xFFFFFFFF.
*
* Note that dhcpv6-set-tee-times is intended to be transitional and will
* likely be removed in 4.4.0, leaving the behavior as getting the values
* either from the configured parameters (if you want zeros, define them as
* zeros) or by calculating them per the RFC.
*
* \param reply - pointer to the reply_state structure
* \param ia_cursor - offset of the beginning of the IA_XX option within the
* reply's outbound data buffer
*/
static void
set_reply_tee_times(struct reply_state* reply, unsigned ia_cursor)
{
struct option_cache *oc;
int set_tee_times;
/* Found out if calculated values are enabled. */
oc = lookup_option(&server_universe, reply->opt_state,
SV_DHCPV6_SET_TEE_TIMES);
set_tee_times = (oc &&
evaluate_boolean_option_cache(NULL, reply->packet,
NULL, NULL,
reply->packet->options,
reply->opt_state,
&global_scope, oc, MDL));
oc = lookup_option(&dhcp_universe, reply->opt_state,
DHO_DHCP_RENEWAL_TIME);
if (oc != NULL) {
/* dhcp-renewal-time is defined, use it */
struct data_string data;
memset(&data, 0x00, sizeof(data));
if (!evaluate_option_cache(&data, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state, &global_scope,
oc, MDL) ||
(data.len != 4)) {
log_error("Invalid renewal time.");
reply->renew = 0;
} else {
reply->renew = getULong(data.data);
}
if (data.data != NULL)
data_string_forget(&data, MDL);
} else if (set_tee_times) {
/* Setting them is enabled so T1 is either infinite or
* 0.5 * the shortest preferred lifetime in the IA_XX */
if (reply->min_prefer == INFINITE_TIME)
reply->renew = INFINITE_TIME;
else
reply->renew = reply->min_prefer / 2;
} else {
/* Default is to let the client choose */
reply->renew = 0;
}
putULong(reply->buf.data + ia_cursor + 8, reply->renew);
/* Now T2. */
oc = lookup_option(&dhcp_universe, reply->opt_state,
DHO_DHCP_REBINDING_TIME);
if (oc != NULL) {
/* dhcp-rebinding-time is defined, use it */
struct data_string data;
memset(&data, 0x00, sizeof(data));
if (!evaluate_option_cache(&data, reply->packet, NULL, NULL,
reply->packet->options,
reply->opt_state, &global_scope,
oc, MDL) ||
(data.len != 4)) {
log_error("Invalid rebinding time.");
reply->rebind = 0;
} else {
reply->rebind = getULong(data.data);
}
if (data.data != NULL)
data_string_forget(&data, MDL);
} else if (set_tee_times) {
/* Setting them is enabled so T2 is either infinite or
* 0.8 * the shortest preferred lifetime in the reply */
if (reply->min_prefer == INFINITE_TIME)
reply->rebind = INFINITE_TIME;
else
reply->rebind = (reply->min_prefer / 5) * 4;
} else {
/* Default is to let the client choose */
reply->rebind = 0;
}
putULong(reply->buf.data + ia_cursor + 12, reply->rebind);
}
/*
* Releases the iasubopts in the pre-existing IA, if they are not in
* the same shared-network as the new IA.
*
* returns 1 if the release was done, 0 otherwise
*/
int
release_on_roam(struct reply_state* reply) {
struct ia_xx* old_ia = reply->old_ia;
struct iasubopt *lease = NULL;
int i;
if (old_ia == NULL || old_ia->num_iasubopt <= 0) {
return(0);
}
/* If the old shared-network and new are the same, client hasn't
* roamed, nothing to do. We only check the first one because you
* cannot have iasubopts on different shared-networks within a
* single ia. */
lease = old_ia->iasubopt[0];
if (lease->ipv6_pool->shared_network == reply->shared) {
return (0);
}
/* Old and new are on different shared networks so the client must
* roamed. Release the old leases. */
for (i = 0; i < old_ia->num_iasubopt; i++) {
lease = old_ia->iasubopt[i];
log_info("Client: %s roamed to new network,"
" releasing lease: %s%s",
print_hex_1(reply->client_id.len,
reply->client_id.data, 60),
pin6_addr(&lease->addr), iasubopt_plen_str(lease));
release_lease6(lease->ipv6_pool, lease);
lease->ia->cltt = cur_time;
write_ia(lease->ia);
}
return (1);
}
/*
* Convenience function which returns a string (static buffer)
* containing either a "/" followed by the prefix length or an
* empty string depending on the lease type
*/
const char *iasubopt_plen_str(struct iasubopt *lease) {
static char prefix_buf[16];
*prefix_buf = 0;
if ((lease->ia) && (lease->ia->ia_type == D6O_IA_PD)) {
sprintf(prefix_buf, "/%-d", lease->plen);
}
return (prefix_buf);
}
#endif /* DHCPv6 */
|