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
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2025-present Eclipse ThreadX Contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** NetX Component */
/** */
/** Dynamic Host Configuration Protocol (DHCP) Server */
/** */
/**************************************************************************/
/**************************************************************************/
#define NX_DHCP_SERVER_SOURCE_CODE
/* Force error checking to be disabled in this module */
#ifndef NX_DISABLE_ERROR_CHECKING
#define NX_DISABLE_ERROR_CHECKING
#endif
/* Include necessary system files. */
#include "nx_api.h"
#ifndef NX_DISABLE_IPV4
#include "nx_ip.h"
#include "nx_udp.h"
#include "nxd_dhcp_server.h"
#include "tx_timer.h"
#include "nx_packet.h"
#include "nx_system.h"
/* Bring in externs for caller checking code. */
NX_CALLER_CHECKING_EXTERNS
#ifdef EL_PRINTF_ENABLE
#define EL_PRINTF printf
#endif
/* Define the DHCP Internal Function. */
static VOID _nx_dhcp_server_thread_entry(ULONG ip_instance);
static VOID _nx_dhcp_slow_periodic_timer_entry(ULONG info);
static VOID _nx_dhcp_fast_periodic_timer_entry(ULONG info);
static UINT _nx_dhcp_server_packet_process(NX_DHCP_SERVER *dhcp_ptr, NX_PACKET *packet_ptr);
static UINT _nx_dhcp_respond_to_client_message(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr);
static UINT _nx_dhcp_server_extract_information(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT **dhcp_client_ptr, NX_PACKET *packet_ptr, UINT iface_index);
static UINT _nx_dhcp_process_option_data(NX_DHCP_CLIENT *dhcp_ptr, CHAR *buffer, UCHAR value, UINT get_option_data, UINT size);
static UINT _nx_dhcp_add_option(UCHAR *bootp_message, UINT option, UINT size, ULONG value, UINT *index);
static UINT _nx_dhcp_add_requested_option(NX_DHCP_SERVER *dhcp_ptr, UINT iface_index, UCHAR *buffer, UINT option, UINT *index);
static UINT _nx_dhcp_set_server_options(NX_DHCP_SERVER *dhcp_ptr, CHAR *buffer, UINT buffer_length);
static UINT _nx_dhcp_load_server_options(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr, UCHAR *buffer, UINT option_type, UINT *index);
static UINT _nx_dhcp_parse_next_option(CHAR **buffer, UINT *digit, UINT length);
static UINT _nx_dhcp_server_get_data(UCHAR *data, UINT size, ULONG *value);
static VOID _nx_dhcp_server_store_data(UCHAR *data, UINT size, ULONG value);
static UINT _nx_dhcp_clear_client_session(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr);
static UINT _nx_dhcp_validate_client_message(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr);
static UINT _nx_dhcp_find_client_record_by_chaddr(NX_DHCP_SERVER *dhcp_ptr, UINT iface_index, ULONG client_mac_msw, ULONG client_mac_lsw,NX_DHCP_CLIENT **dhcp_client_ptr, UINT add_on);
static UINT _nx_dhcp_find_client_record_by_ip_address(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT **dhcp_client_ptr, UINT iface_index, ULONG assigned_ip_address);
static UINT _nx_dhcp_server_assign_ip_address(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr);
static UINT _nx_dhcp_find_interface_table_ip_address(NX_DHCP_SERVER *dhcp_ptr, UINT iface_index, ULONG ip_address, NX_DHCP_INTERFACE_IP_ADDRESS **return_interface_address);
static UINT _nx_dhcp_update_assignable_ip_address(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr, ULONG ip_address, UINT assign_status);
static UINT _nx_dhcp_find_ip_address_owner(NX_DHCP_INTERFACE_IP_ADDRESS *iface_owner, NX_DHCP_CLIENT *client_record_ptr, UINT *assigned_to_client);
static UINT _nx_dhcp_record_ip_address_owner(NX_DHCP_INTERFACE_IP_ADDRESS *iface_owner, NX_DHCP_CLIENT *client_record_ptr, UINT lease_time);
static UINT _nx_dhcp_clear_ip_address_owner(NX_DHCP_INTERFACE_IP_ADDRESS *iface_owner);
static VOID _nx_dhcp_server_socket_receive_notify(NX_UDP_SOCKET *socket_ptr);
/* To enable dhcp server output, define TESTOUTPUT. */
/* #define TESTOUTPUT 1 */
/* To enable packet dump, define PACKET_DUMP. */
/* #define PACKET_DUMP 1 */
#ifdef TESTOUTPUT
static int add_client = 0;
/* Define how often to print the server table of current clients.
For no output set to zero. For low volume, set to 1;
for very high volume, set higher so as not to overwelm the processor. */
#define TRACE_NTH_CLIENT_PACKET 1
#endif
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nxe_dhcp_server_create PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function checks for errors in the DHCP create function call. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* ip_ptr Pointer to IP instance */
/* stack_ptr Pointer to free memory */
/* stack_size Size of DHCP server stack */
/* name_ptr DHCP name pointer */
/* packet_pool Server packet pool for sending*/
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* _nx_dhcp_server_create Actual DHCP create function */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _nxe_dhcp_server_create(NX_DHCP_SERVER *dhcp_ptr, NX_IP *ip_ptr, VOID *stack_ptr, ULONG stack_size,
CHAR *name_ptr, NX_PACKET_POOL *packet_pool)
{
UINT status;
/* Check for pointer invalid input. */
if ((dhcp_ptr == NX_NULL) || (packet_pool == NX_NULL) ||(stack_ptr == NX_NULL) ||(ip_ptr == NX_NULL))
{
return(NX_PTR_ERROR);
}
if (stack_size == 0)
{
return(NX_DHCP_PARAMETER_ERROR);
}
/* Check for appropriate caller. */
NX_INIT_AND_THREADS_CALLER_CHECKING
/* Call actual DHCP create service. */
status = _nx_dhcp_server_create(dhcp_ptr, ip_ptr, stack_ptr, stack_size, name_ptr, packet_pool);
/* Return status. */
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_create PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function initializes the DHCP Server with necessary components.*/
/* It creates a packet pool for sending Server DHCP messages and a */
/* thread task for the DHCP server operation. It also sets the standard*/
/* DHCP options (e.g. lease time etc) for granting IP addresses. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* ip_ptr Pointer to IP instance */
/* stack_ptr Pointer to server thread on */
/* the stack */
/* stack_size Size of DHCP server stack */
/* name_ptr DHCP name pointer */
/* packet_pool Server packet pool for sending*/
/* DHCP replies to client */
/* */
/* OUTPUT */
/* NX_SUCCESS Successful completion status */
/* NX_DHCP_INADEQUATE_PACKET_POOL_PAYLOAD */
/* Packet payload too small error*/
/* NX_DHCP_NO_SERVER_OPTION_LIST Missing option list error */
/* */
/* status Completion status of socket */
/* and thread create calls */
/* */
/* CALLS */
/* */
/* nx_udp_socket_create Create the DHCP UDP socket */
/* nx_udp_socket_delete Delete the DHCP UDP socket */
/* tx_thread_create Create DHCP processing thread */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _nx_dhcp_server_create(NX_DHCP_SERVER *dhcp_ptr, NX_IP *ip_ptr, VOID *stack_ptr, ULONG stack_size,
CHAR *name_ptr, NX_PACKET_POOL *packet_pool_ptr)
{
UINT status;
UINT timer_ticks;
UINT i, j;
/* Initialize the DHCP control block to zero. */
memset((void *) dhcp_ptr, 0, sizeof(NX_DHCP_SERVER));
/* Check the Server packet pool is at least large enough for a full sized DHCP message as per
RFC 2131, plus header data. */
if (packet_pool_ptr -> nx_packet_pool_payload_size < NX_DHCP_MINIMUM_PACKET_PAYLOAD)
{
/* Return the error status. */
return(NX_DHCP_INADEQUATE_PACKET_POOL_PAYLOAD);
}
/* Set the packet pool pointer. */
dhcp_ptr -> nx_dhcp_packet_pool_ptr = packet_pool_ptr;
/* Save the DHCP name. */
dhcp_ptr -> nx_dhcp_name = name_ptr;
/* Set the DHCP Server attributes. */
dhcp_ptr -> nx_dhcp_ip_ptr = ip_ptr;
/* Loop through all the interface tables and clear table memory. */
for (i = 0; i < NX_MAX_PHYSICAL_INTERFACES; i++)
{
/* Clear all the table's IP address entries. */
for (j = 0; j < NX_DHCP_IP_ADDRESS_MAX_LIST_SIZE; j++)
{
memset(&dhcp_ptr -> nx_dhcp_interface_table[i].nx_dhcp_ip_address_list[j],
0, sizeof(NX_DHCP_INTERFACE_IP_ADDRESS));
}
/* Clear the DHCP interface itself. */
memset(&dhcp_ptr -> nx_dhcp_interface_table[i], 0, sizeof(NX_DHCP_INTERFACE_TABLE));
}
/* Loop through the entire client record table. */
for (i = 0; i < NX_DHCP_CLIENT_RECORD_TABLE_SIZE; i++)
{
/* Clear the client record. */
memset(&dhcp_ptr -> client_records[i], 0, sizeof(NX_DHCP_CLIENT));
}
/* Verify the application has defined a server option list. */
if (sizeof(NX_DHCP_SERVER_OPTION_LIST) == 1)
{
/* No, return the error status. */
return(NX_DHCP_NO_SERVER_OPTION_LIST);
}
/* Create the list of DHCP options the server can provide for the client. */
status = _nx_dhcp_set_server_options(dhcp_ptr, (CHAR *)NX_DHCP_SERVER_OPTION_LIST, sizeof(NX_DHCP_SERVER_OPTION_LIST) - 1);
/* Was the option list set successful? */
if (status != NX_SUCCESS)
{
/* No, return error status. */
return(status);
}
/* Update the dhcp structure ID. */
dhcp_ptr -> nx_dhcp_id = NX_DHCP_SERVER_ID;
/* Create the Socket and check the status */
status = nx_udp_socket_create(ip_ptr, &(dhcp_ptr -> nx_dhcp_socket), "NetX DHCP Server Socket",
NX_DHCP_TYPE_OF_SERVICE, NX_DHCP_FRAGMENT_OPTION, NX_DHCP_TIME_TO_LIVE, NX_DHCP_QUEUE_DEPTH);
/* Was the socket creation successful? */
if (status != NX_SUCCESS)
{
/* No, return error status. */
return(status);
}
/* Save the DHCP instance pointer in the socket. */
dhcp_ptr -> nx_dhcp_socket.nx_udp_socket_reserved_ptr = (void *) dhcp_ptr;
/* Set the DHCP request handler for packets received on the DHCP server socket. */
status = nx_udp_socket_receive_notify(&(dhcp_ptr -> nx_dhcp_socket), _nx_dhcp_server_socket_receive_notify);
/* Was the socket receive_notify set successful? */
if (status != NX_SUCCESS)
{
/* Delete the UDP socket. */
nx_udp_socket_delete(&(dhcp_ptr -> nx_dhcp_socket));
/* No, return error status. */
return status;
}
/* Create the DHCP processing thread. */
status = tx_thread_create(&(dhcp_ptr -> nx_dhcp_server_thread), "NetX DHCP Server Thread",
_nx_dhcp_server_thread_entry, (ULONG)(ALIGN_TYPE)dhcp_ptr,
stack_ptr, stack_size, NX_DHCP_SERVER_THREAD_PRIORITY,
NX_DHCP_SERVER_THREAD_PRIORITY, 1, TX_DONT_START);
NX_THREAD_EXTENSION_PTR_SET(&(dhcp_ptr -> nx_dhcp_server_thread), dhcp_ptr)
/* Determine if the thread creation was successful. */
if (status != NX_SUCCESS)
{
/* Delete the UDP socket. */
nx_udp_socket_delete(&(dhcp_ptr -> nx_dhcp_socket));
/* No, return error status. */
return(status);
}
/* Create the DHCP mutex. */
status = tx_mutex_create(&dhcp_ptr -> nx_dhcp_mutex, "DHCP Server Mutex", TX_NO_INHERIT);
/* Determine if the thread creation was successful. */
if (status != NX_SUCCESS)
{
/* Delete the UDP socket. */
nx_udp_socket_delete(&(dhcp_ptr -> nx_dhcp_socket));
/* Delete DHCP thead. */
tx_thread_delete(&(dhcp_ptr -> nx_dhcp_server_thread));
/* No, return error status. */
return(status);
}
/* Convert seconds to timer ticks. */
timer_ticks = NX_DHCP_SLOW_PERIODIC_TIME_INTERVAL * NX_IP_PERIODIC_RATE;
/* Create the timer for Client DHCP session. This will keep track of when leases expire
and when a client session has timed out. */
status = tx_timer_create(&(dhcp_ptr -> nx_dhcp_slow_periodic_timer), "DHCP Server IP Lease Timer",
_nx_dhcp_slow_periodic_timer_entry, (ULONG)(ALIGN_TYPE)dhcp_ptr,
timer_ticks, timer_ticks, TX_NO_ACTIVATE);
NX_TIMER_EXTENSION_PTR_SET(&(dhcp_ptr -> nx_dhcp_slow_periodic_timer), dhcp_ptr)
/* Convert seconds to timer ticks. */
timer_ticks = NX_DHCP_FAST_PERIODIC_TIME_INTERVAL * NX_IP_PERIODIC_RATE;
/* Create the timer for Client DHCP session. This will keep track of when leases expire
and when a client session has timed out. */
status += tx_timer_create(&(dhcp_ptr -> nx_dhcp_fast_periodic_timer), "DHCP Server Session Timer",
_nx_dhcp_fast_periodic_timer_entry, (ULONG)(ALIGN_TYPE)dhcp_ptr,
timer_ticks, timer_ticks, TX_NO_ACTIVATE);
NX_TIMER_EXTENSION_PTR_SET(&(dhcp_ptr -> nx_dhcp_fast_periodic_timer), dhcp_ptr)
/* Check for error. */
if (status != NX_SUCCESS)
{
/* Delete the UDP socket. */
nx_udp_socket_delete(&(dhcp_ptr -> nx_dhcp_socket));
/* Delete DHCP thead. */
tx_thread_delete(&(dhcp_ptr -> nx_dhcp_server_thread));
/* Delete ThreadX resources. */
tx_mutex_delete(&(dhcp_ptr -> nx_dhcp_mutex));
/* Delete DHCP timer. */
tx_timer_delete(&(dhcp_ptr -> nx_dhcp_slow_periodic_timer));
tx_timer_delete(&(dhcp_ptr -> nx_dhcp_fast_periodic_timer));
/* Return the error status. */
return(status);
}
/* Create the DHCP event flag instance. */
status = tx_event_flags_create(&dhcp_ptr -> nx_dhcp_server_events, "DHCP Server Events");
/* Check for error. */
if (status != TX_SUCCESS)
{
/* Delete the UDP socket. */
nx_udp_socket_delete(&(dhcp_ptr -> nx_dhcp_socket));
/* Delete DHCP thread. */
tx_thread_delete(&(dhcp_ptr -> nx_dhcp_server_thread));
/* Delete ThreadX resources. */
tx_mutex_delete(&(dhcp_ptr -> nx_dhcp_mutex));
/* Delete DHCP Timer. */
tx_timer_delete(&(dhcp_ptr -> nx_dhcp_slow_periodic_timer));
tx_timer_delete(&(dhcp_ptr -> nx_dhcp_fast_periodic_timer));
/* Return the error status. */
return(status);
}
/* Return a successful status. */
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_fast_periodic_timer_entry PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function watches the session timeout on each active client */
/* session. WHen it does it clears session data from the client record */
/* and resets the client state to INIT. If a iP address had been */
/* assigned, it is returned back to the server pool. */
/* */
/* INPUT */
/* */
/* info Generic pointer to DHCP server */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* tx_event_flags_set Adds a fast periodic event */
/* */
/* CALLED BY */
/* */
/* Application code */
/* */
/**************************************************************************/
static VOID _nx_dhcp_fast_periodic_timer_entry(ULONG info)
{
NX_DHCP_SERVER *dhcp_ptr;
/* Setup DHCP pointer. */
NX_TIMER_EXTENSION_PTR_GET(dhcp_ptr, NX_DHCP_SERVER, info)
/* Signal the DHCP Server fast periodic event. */
tx_event_flags_set(&(dhcp_ptr -> nx_dhcp_server_events), NX_DHCP_SERVER_FAST_PERIODIC_EVENT, TX_OR);
return;
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_slow_periodic_timer_entry PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function watches lease timeout on assigned IP address in the */
/* server database. When one has expired, the address is returned to */
/* the available pool and the Client is reset to the INIT state. */
/* */
/* INPUT */
/* */
/* info Generic pointer to DHCP server */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* tx_event_flags_set Adds a slow periodic event */
/* */
/* CALLED BY */
/* */
/* ThreadX system timer thread */
/* */
/**************************************************************************/
static VOID _nx_dhcp_slow_periodic_timer_entry(ULONG info)
{
NX_DHCP_SERVER *dhcp_ptr;
/* Setup DHCP pointer. */
NX_TIMER_EXTENSION_PTR_GET(dhcp_ptr, NX_DHCP_SERVER, info)
/* Signal the DHCP Server slow periodic event. */
tx_event_flags_set(&(dhcp_ptr -> nx_dhcp_server_events), NX_DHCP_SERVER_SLOW_PERIODIC_EVENT, TX_OR);
return;
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_socket_receive_notify PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function is notified by NetX Duo when a packet arrives in the */
/* server socket. It sets a flag which the DHCP server thread */
/* detect so it will know to process the incoming packet . */
/* */
/* INPUT */
/* */
/* socket_ptr Pointer to Server Socket */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* None */
/* tx_event_flags_set Adds a packet receive event */
/* CALLED BY */
/* */
/* Threads */
/* */
/**************************************************************************/
VOID _nx_dhcp_server_socket_receive_notify(NX_UDP_SOCKET *socket_ptr)
{
NX_DHCP_SERVER *dhcp_server_ptr;
/* Get a pointer to the DHCP server. */
dhcp_server_ptr = (NX_DHCP_SERVER *) socket_ptr -> nx_udp_socket_reserved_ptr;
/* Signal the DHCP Server it has a UDP packet on its socket receive queue. */
tx_event_flags_set(&(dhcp_server_ptr -> nx_dhcp_server_events), NX_DHCP_SERVER_RECEIVE_EVENT, TX_OR);
return;
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nxe_dhcp_server_delete PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function checks for errors in the DHCP delete function call. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* _nx_dhcp_server_delete Actual DHCP delete function */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _nxe_dhcp_server_delete(NX_DHCP_SERVER *dhcp_ptr)
{
UINT status;
/* Check for invalid input pointers. */
if (dhcp_ptr == NX_NULL)
return(NX_PTR_ERROR);
if (dhcp_ptr -> nx_dhcp_id != NX_DHCP_SERVER_ID)
{
return(NX_DHCP_PARAMETER_ERROR);
}
/* Check for appropriate caller. */
NX_THREADS_ONLY_CALLER_CHECKING
/* Call actual DHCP delete service. */
status = _nx_dhcp_server_delete(dhcp_ptr);
/* Return status. */
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_delete PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function deletes the DHCP server and releases all of its */
/* resources. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* nx_udp_socket_unbind Free the UDP socket port */
/* nx_udp_socket_delete Delete the DHCP UDP socket */
/* tx_thread_terminate Terminate DHCP thread */
/* tx_thread_terminate Terminate DHCP thread */
/* tx_thread_delete Delete DHCP thread */
/* tx_timer_delete Delete DHCP timer */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _nx_dhcp_server_delete(NX_DHCP_SERVER *dhcp_ptr)
{
/* Determine if DHCP is stopped. */
if (dhcp_ptr -> nx_dhcp_started != NX_FALSE)
{
/* Stop the DHCP server. */
_nx_dhcp_server_stop(dhcp_ptr);
}
/* Delete the UDP socket. */
nx_udp_socket_delete(&(dhcp_ptr -> nx_dhcp_socket));
/* Delete the session ("fast") timer. */
tx_timer_delete(&(dhcp_ptr -> nx_dhcp_fast_periodic_timer));
/* Delete the IP lease ("slow") timer. */
tx_timer_delete(&(dhcp_ptr -> nx_dhcp_slow_periodic_timer));
/* Suspend the DHCP processing thread. */
tx_thread_suspend(&(dhcp_ptr -> nx_dhcp_server_thread));
/* Terminate the DHCP processing thread. */
tx_thread_terminate(&(dhcp_ptr -> nx_dhcp_server_thread));
/* Delete the DHCP processing thread. */
tx_thread_delete(&(dhcp_ptr -> nx_dhcp_server_thread));
/* Delete the mutexes. */
tx_mutex_delete(&(dhcp_ptr -> nx_dhcp_mutex));
/* Delete the flag event. */
tx_event_flags_delete(&(dhcp_ptr -> nx_dhcp_server_events));
/* Clear the dhcp structure ID. */
dhcp_ptr -> nx_dhcp_id = 0;
/* Return a successful status. */
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nxe_dhcp_create_server_ip_address_list PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function performs error checking for the IP address list create*/
/* service. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* iface_index IP interface index to specify the */
/* DHCP server interface to use */
/* start_ip_address Starting IP address */
/* end_ip_address Ending IP address */
/* addresses_added Pointer to addresses added counter */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful completion status */
/* NX_PTR_ERROR Invalid pointer input */
/* */
/* CALLS */
/* */
/* _nx_dhcp_create_server_ip_address_list */
/* Actual IP address create service */
/* */
/* CALLED BY */
/* */
/* Application code */
/* */
/**************************************************************************/
UINT _nxe_dhcp_create_server_ip_address_list(NX_DHCP_SERVER *dhcp_ptr, UINT iface_index,
ULONG start_ip_address, ULONG end_ip_address, UINT *addresses_added)
{
UINT status;
/* Check for invalid input. */
if ((dhcp_ptr == NX_NULL) || (addresses_added == NX_NULL))
{
return(NX_PTR_ERROR);
}
/* Check for invalid non pointer input. */
if ((start_ip_address > end_ip_address) ||
(iface_index >= NX_MAX_PHYSICAL_INTERFACES) ||
(start_ip_address == NX_DHCP_NO_ADDRESS))
{
return(NX_DHCP_INVALID_IP_ADDRESS);
}
status = _nx_dhcp_create_server_ip_address_list(dhcp_ptr, iface_index, start_ip_address, end_ip_address, addresses_added);
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_create_server_ip_address_list PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function creates a list of available IP addresses for DHCP */
/* clients on the specified interface. The caller supplies a start and */
/* end IP address and all addresses in between are flagged as available.*/
/* The start ip address is assumed to be the first entry in the list, */
/* not appended to a previously started list. All the IP addresses may */
/* not fit in the server interface table; the number actually added is */
/* written to the 'addresses added' variable. The caller should check */
/* this output. */
/* */
/* If some addresses within the specified range need to be reserved or */
/* statically assigned the host application needs to set their lease to */
/* infinity (0xffffffff) and assigned status is set. This function also */
/* add the interface subnet, router, dns server for specified index. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP server */
/* iface_index Index specifying server interface*/
/* start_ip_address Beginning IP address in list. */
/* end_ip_address Ending IP address in list. */
/* addresses_added Number of addresses added to list*/
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful IP list creation */
/* NX_DHCP_SERVER_BAD_INTERFACE_INDEX Invalid interface index input */
/* NX_DHCP_INVALID_IP_ADDRESS_LIST Invalid IP list parameter input */
/* */
/* CALLS */
/* */
/* _nx_dhcp_update_assignable_ip_address */
/* Updates interface table entry */
/* with IP address status and owner*/
/* _nx_dhcp_clear_client_session Clears client record of session */
/* data from most recent client */
/* */
/* CALLED BY */
/* */
/* Application code */
/* */
/**************************************************************************/
UINT _nx_dhcp_create_server_ip_address_list(NX_DHCP_SERVER *dhcp_ptr, UINT iface_index,
ULONG start_ip_address, ULONG end_ip_address,
UINT *addresses_added)
{
UINT i;
ULONG next_ip_address;
NX_IP *ip_ptr;
NX_DHCP_INTERFACE_IP_ADDRESS *ip_address_entry_ptr;
NX_DHCP_INTERFACE_TABLE *dhcp_interface_table_ptr;
/* Check for an invalid interface index. */
if (iface_index >= NX_MAX_PHYSICAL_INTERFACES)
{
return NX_DHCP_SERVER_BAD_INTERFACE_INDEX;
}
/* Obtain DHCP Server mutex protection,. */
tx_mutex_get(&dhcp_ptr -> nx_dhcp_mutex, NX_WAIT_FOREVER);
*addresses_added = 0;
next_ip_address = start_ip_address;
/* Set local pointer to the list being created for convenience. */
dhcp_interface_table_ptr = &dhcp_ptr -> nx_dhcp_interface_table[iface_index];
/* Fill in the interface table with DHCP attributes for the specified interface. */
/* Set up a local variable for convenience. */
ip_ptr = dhcp_ptr -> nx_dhcp_ip_ptr;
/* Assign the IP interface specified by the IP interface index to the DHCP interface. */
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_incoming_interface = &(ip_ptr -> nx_ip_interface[iface_index]);
/* Set the server ip address based on the specified interface. */
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_server_ip_address = ip_ptr -> nx_ip_interface[iface_index].nx_interface_ip_address;
/* Set the dns server, subnet mask, and router (default gateway) for this interface. */
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_dns_ip_address = ip_ptr -> nx_ip_interface[iface_index].nx_interface_ip_address;
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_subnet_mask = ip_ptr -> nx_ip_interface[iface_index].nx_interface_ip_network_mask;
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_router_ip_address = ip_ptr -> nx_ip_gateway_address;
/* Make sure the start/end address subnet match the interface subnet. */
if ((dhcp_interface_table_ptr -> nx_dhcp_subnet_mask & start_ip_address) !=
(dhcp_interface_table_ptr -> nx_dhcp_subnet_mask & dhcp_interface_table_ptr -> nx_dhcp_server_ip_address))
{
/* Release DHCP Server mutex. */
tx_mutex_put(&dhcp_ptr -> nx_dhcp_mutex);
return(NX_DHCP_SERVER_BAD_INTERFACE_INDEX);
}
if ((dhcp_interface_table_ptr -> nx_dhcp_subnet_mask & end_ip_address) !=
(dhcp_interface_table_ptr -> nx_dhcp_subnet_mask & dhcp_interface_table_ptr -> nx_dhcp_server_ip_address))
{
/* Release DHCP Server mutex. */
tx_mutex_put(&dhcp_ptr -> nx_dhcp_mutex);
return(NX_DHCP_SERVER_BAD_INTERFACE_INDEX);
}
/* Zero out the list size. */
dhcp_interface_table_ptr -> nx_dhcp_address_list_size = 0;
/* Check for invalid list parameters. */
if ((start_ip_address > end_ip_address) || !start_ip_address || !end_ip_address)
{
/* Release DHCP Server mutex. */
tx_mutex_put(&dhcp_ptr -> nx_dhcp_mutex);
return(NX_DHCP_INVALID_IP_ADDRESS_LIST);
}
/* Clear out existing entries and start adding IP addresses at the beginning of the list. */
i = 0;
/* Fit as many IP addresses in the specified range as will fit in the table. */
while (i < NX_DHCP_IP_ADDRESS_MAX_LIST_SIZE && next_ip_address <= end_ip_address)
{
/* Set local pointer to the next entry for convenience. */
ip_address_entry_ptr = &dhcp_interface_table_ptr -> nx_dhcp_ip_address_list[i];
/* Clear existing entry; client identifier and available status
must be initialized to NULL and not assigned respectively. */
memset(ip_address_entry_ptr, 0, sizeof(NX_DHCP_INTERFACE_IP_ADDRESS));
/* Add the next IP address to the list. */
ip_address_entry_ptr -> nx_assignable_ip_address = next_ip_address;
/* Increase the list size. */
dhcp_interface_table_ptr -> nx_dhcp_address_list_size++;
next_ip_address++;
i++;
}
/* Set the actual number of available addresses added to the table. */
*addresses_added = dhcp_interface_table_ptr -> nx_dhcp_address_list_size;
/* Release DHCP Server mutex. */
tx_mutex_put(&dhcp_ptr -> nx_dhcp_mutex);
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nxe_dhcp_set_interface_network_parameters PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function performs error checking services for the set interface */
/* network parameters service. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP server */
/* iface_index Index specifying server interface*/
/* subnet_mask Network mask for DHCP clients */
/* default_gateway_address Router/default gateway for client*/
/* dns_server_address DNS server for DHCP clients */
/* */
/* OUTPUT */
/* */
/* status Actual completion status */
/* NX_PTR_ERROR Invalid interface index input */
/* NX_DHCP_INVALID_NETWORK_PARAMETERS Invalid network parameter input */
/* */
/* CALLS */
/* */
/* _nx_dhcp_update_assignable_ip_address */
/* Updates interface table entry */
/* with IP address status and owner*/
/* _nx_dhcp_clear_client_session Clears client record of session */
/* data from most recent client */
/* */
/* CALLED BY */
/* */
/* Application code */
/* */
/**************************************************************************/
UINT _nxe_dhcp_set_interface_network_parameters(NX_DHCP_SERVER *dhcp_ptr, UINT iface_index,
ULONG subnet_mask, ULONG default_gateway_address,
ULONG dns_server_address)
{
UINT status;
/* Check for invalid pointer input. */
if (dhcp_ptr == NX_NULL)
{
return(NX_PTR_ERROR);
}
/* Check for non pointer input. */
if ((subnet_mask == 0) || (default_gateway_address == 0) || (dns_server_address == 0))
{
return(NX_DHCP_INVALID_NETWORK_PARAMETERS);
}
/* Call the actual service. */
status = _nx_dhcp_set_interface_network_parameters(dhcp_ptr, iface_index,
subnet_mask, default_gateway_address,
dns_server_address);
/* Return completion status. */
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_set_interface_network_parameters PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function sets the DHCP server default options for 'network */
/* critical parameters:' gateway, dns server and network mask for the */
/* specified interface. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP server */
/* iface_index Index specifying server interface*/
/* subnet_mask Network mask for DHCP clients */
/* default_gateway_address Router/default gateway for client*/
/* dns_server_address DNS server for DHCP clients */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Valid parameters received */
/* NX_DHCP_SERVER_BAD_INTERFACE_INDEX Invalid interface index input */
/* NX_DHCP_INVALID_NETWORK_PARAMETERS Invalid network parameter input */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* Application code */
/* */
/**************************************************************************/
UINT _nx_dhcp_set_interface_network_parameters(NX_DHCP_SERVER *dhcp_ptr, UINT iface_index,
ULONG subnet_mask, ULONG default_gateway_address,
ULONG dns_server_address)
{
/* Check for invalid non pointer input. */
if (iface_index >= NX_MAX_PHYSICAL_INTERFACES)
{
return(NX_DHCP_SERVER_BAD_INTERFACE_INDEX);
}
/* The default gateway should be on the same network as the dhcp server interface. */
if ((default_gateway_address & subnet_mask) !=
(dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_server_ip_address &
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_subnet_mask))
{
return(NX_DHCP_INVALID_NETWORK_PARAMETERS);
}
/* Obtain DHCP Server mutex protection,. */
tx_mutex_get(&dhcp_ptr -> nx_dhcp_mutex, NX_WAIT_FOREVER);
/* Set the subnet, dns server, and router address for this interface. */
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_dns_ip_address = dns_server_address;
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_subnet_mask = subnet_mask;
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_router_ip_address = default_gateway_address;
/* Release DHCP Server mutex. */
tx_mutex_put(&dhcp_ptr -> nx_dhcp_mutex);
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nxe_dhcp_server_start PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function checks for errors in the DHCP start function call. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* _nx_dhcp_start Actual DHCP start function */
/* nx_udp_socket_bind Bind the DHCP UDP socket */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _nxe_dhcp_server_start(NX_DHCP_SERVER *dhcp_ptr)
{
UINT status;
/* Check for invalid input pointer. */
if (dhcp_ptr == NX_NULL)
return(NX_PTR_ERROR);
if (dhcp_ptr -> nx_dhcp_id != NX_DHCP_SERVER_ID)
{
return(NX_DHCP_PARAMETER_ERROR);
}
/* Check for appropriate caller. */
NX_INIT_AND_THREADS_CALLER_CHECKING
/* Call actual DHCP start service. */
status = _nx_dhcp_server_start(dhcp_ptr);
/* Return status. */
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_start PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function initiates the DHCP processing thread. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* nx_udp_socket_bind Bind DHCP socket */
/* nx_udp_socket_unbind Unbind DHCP socket */
/* tx_thread_resume Initiate DHCP processing */
/* tx_mutex_get Get the DHCP mutex */
/* tx_mutex_put Release the DHCP mutex */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _nx_dhcp_server_start(NX_DHCP_SERVER *dhcp_ptr)
{
UINT status;
/* Determine if DHCP has already been started. */
if (dhcp_ptr -> nx_dhcp_started)
{
/* Error DHCP has already been started. */
/* Return completion status. */
return(NX_DHCP_SERVER_ALREADY_STARTED);
}
dhcp_ptr -> nx_dhcp_started = NX_TRUE;
/* Bind the UDP socket to the DHCP Socket port. */
status = nx_udp_socket_bind(&(dhcp_ptr -> nx_dhcp_socket), NX_DHCP_SERVER_UDP_PORT, TX_WAIT_FOREVER);
/* Check status. */
if (status != NX_SUCCESS)
{
/* Error, unbind the DHCP socket. */
nx_udp_socket_unbind(&(dhcp_ptr -> nx_dhcp_socket));
/* Set status to DHCP error code. */
return(status);
}
/* Start the session ("fast") timer. */
tx_timer_activate(&(dhcp_ptr -> nx_dhcp_fast_periodic_timer));
/* Start the IP lease ("slow") timer. */
tx_timer_activate(&(dhcp_ptr -> nx_dhcp_slow_periodic_timer));
/* Start the DHCP server thread. */
tx_thread_resume(&(dhcp_ptr -> nx_dhcp_server_thread));
/* Return completion status. */
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nxe_dhcp_server_stop PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function checks for errors in the DHCP stop function call. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* _nx_dhcp_stop Actual DHCP stop function */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _nxe_dhcp_server_stop(NX_DHCP_SERVER *dhcp_ptr)
{
UINT status;
/* Check for invalid input pointer. */
if (dhcp_ptr == NX_NULL)
return(NX_PTR_ERROR);
if (dhcp_ptr -> nx_dhcp_id != NX_DHCP_SERVER_ID)
{
return(NX_DHCP_PARAMETER_ERROR);
}
/* Check for appropriate caller. */
NX_THREADS_ONLY_CALLER_CHECKING
/* Call actual DHCP stop service. */
status = _nx_dhcp_server_stop(dhcp_ptr);
/* Return status. */
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_stop PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function halts the DHCP processing thread. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* nx_udp_socket_unbind Unbind the DHCP UDP socket */
/* tx_thread_preemption_change Change the thread preemption */
/* tx_thread_suspend Suspend DHCP processing */
/* tx_thread_wait_abort Remove any thread suspension */
/* tx_timer_deactivate Deactivate timer */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _nx_dhcp_server_stop(NX_DHCP_SERVER *dhcp_ptr)
{
UINT current_preemption;
/* Determine if DHCP is started. */
if (dhcp_ptr -> nx_dhcp_started == NX_FALSE)
{
/* DHCP is not started so it can't be stopped. */
return(NX_DHCP_SERVER_NOT_STARTED);
}
/* Obtain mutex protection, which is to say don't interrupt a DHCP server
if it is processing a packet from a DHCP client. */
tx_mutex_get(&dhcp_ptr -> nx_dhcp_mutex, NX_WAIT_FOREVER);
/* Stop the session ("fast") timer. */
tx_timer_deactivate(&(dhcp_ptr -> nx_dhcp_fast_periodic_timer));
/* Stop the IP lease ("slow") timer. */
tx_timer_deactivate(&(dhcp_ptr -> nx_dhcp_slow_periodic_timer));
/* Clear the started flag here to ensure other threads can't issue a stop while
this stop is in progress. */
dhcp_ptr -> nx_dhcp_started = NX_FALSE;
/* Disable preemption for critical section. */
tx_thread_preemption_change(tx_thread_identify(), 0, ¤t_preemption);
/* Suspend the DHCP thread. */
tx_thread_suspend(&(dhcp_ptr -> nx_dhcp_server_thread));
/* Unbind the port. */
nx_udp_socket_unbind(&(dhcp_ptr -> nx_dhcp_socket));
/* Restore preemption. */
tx_thread_preemption_change(tx_thread_identify(), current_preemption, ¤t_preemption);
tx_mutex_put(&dhcp_ptr -> nx_dhcp_mutex);
/* Return completion status. */
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_thread_entry PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function is top level server processing thread for the DHCP */
/* service. It listens for and processes client requests in an infinite*/
/* loop. */
/* */
/* Note that the current implementation expects a Client to initiate */
/* a session with DISCOVER message rather than using the option of */
/* skipping to the REQUEST message. */
/* */
/* INPUT */
/* */
/* dhcp_instance Pointer to the DHCP server */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* _nx_dhcp_process Main DHCP processing function */
/* */
/* CALLED BY */
/* */
/* ThreadX */
/* */
/**************************************************************************/
static VOID _nx_dhcp_server_thread_entry(ULONG info)
{
NX_DHCP_SERVER *dhcp_ptr;
ULONG dhcp_events;
NX_DHCP_INTERFACE_TABLE *iface_table_ptr;
NX_DHCP_INTERFACE_IP_ADDRESS *iface_address_ptr;
NX_DHCP_CLIENT *dhcp_client_ptr;
UINT iface_index, i;
UINT status;
NX_PACKET *packet_ptr;
/* Setup the DHCP pointer. */
NX_THREAD_EXTENSION_PTR_GET(dhcp_ptr, NX_DHCP_SERVER, info)
/* Enter while loop. */
while(1)
{
/* Release the DHCP mutex. */
tx_mutex_put(&(dhcp_ptr -> nx_dhcp_mutex));
/* Pick up IP event flags. */
tx_event_flags_get(&dhcp_ptr -> nx_dhcp_server_events, NX_DHCP_SERVER_ALL_EVENTS,
TX_OR_CLEAR, &dhcp_events, TX_WAIT_FOREVER);
/* Obtain the DHCP mutex before processing the DHCP event. */
tx_mutex_get(&(dhcp_ptr -> nx_dhcp_mutex), TX_WAIT_FOREVER);
/* Check events. */
/* Packet receive event. */
if (dhcp_events & NX_DHCP_SERVER_RECEIVE_EVENT)
{
/* Loop to receive DHCP message. */
while(1)
{
/* Check for an incoming DHCP packet with non blocking option. */
status = _nx_udp_socket_receive(&dhcp_ptr -> nx_dhcp_socket, &packet_ptr, NX_NO_WAIT);
/* Check for packet receive errors. */
if (status != NX_SUCCESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Error receiving DHCP packet 0x%x\n", status);
#endif
break;
}
/* Process DHCP packet. */
_nx_dhcp_server_packet_process(dhcp_ptr, packet_ptr);
}
}
/* FAST periodic event. */
if (dhcp_events & NX_DHCP_SERVER_FAST_PERIODIC_EVENT)
{
/* Check the clients in active session with the server. */
for (i = 0; i < NX_DHCP_CLIENT_RECORD_TABLE_SIZE; i++)
{
/* Create a local pointer for convenience. */
dhcp_client_ptr = &dhcp_ptr -> client_records[i];
/* Skip empty records. */
if ((dhcp_client_ptr -> nx_dhcp_client_mac_lsw == 0) && (dhcp_client_ptr -> nx_dhcp_client_mac_msw == 0))
continue;
/* Skip clients not in active session with the Server, static members,
or client in the bound state. */
if (dhcp_client_ptr -> nx_dhcp_session_timeout == TX_WAIT_FOREVER)
continue;
if (dhcp_client_ptr -> nx_dhcp_client_state == NX_DHCP_STATE_BOUND)
continue;
/* Skip clients whose session timeout is not set e.g. previous session
failed and no longer configured with this server. */
if (dhcp_client_ptr -> nx_dhcp_session_timeout == 0)
continue;
if (dhcp_client_ptr -> nx_dhcp_assigned_ip_address == NX_DHCP_NO_ADDRESS)
continue;
/* Check how much time is left before decreasing the time remaining on the session timeout. */
if (dhcp_client_ptr -> nx_dhcp_session_timeout >= NX_DHCP_FAST_PERIODIC_TIME_INTERVAL)
{
/* Ok to decrement the full amount. */
dhcp_client_ptr -> nx_dhcp_session_timeout -= NX_DHCP_FAST_PERIODIC_TIME_INTERVAL;
}
else
{
/* Set to zero. Time's up! */
dhcp_client_ptr -> nx_dhcp_session_timeout = 0;
}
/* Has time expired on this session? */
if (dhcp_client_ptr -> nx_dhcp_session_timeout == 0)
{
/* Yes, the client has bailed on the session, intentionally or otherwise. If it wants to
get an IP address it must start again. Reset status to 'INIT'. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: client session has timed out. Clear session data\n");
#endif
/* Does the client have an assigned IP address? */
if (dhcp_client_ptr -> nx_dhcp_assigned_ip_address)
{
/* Yes, return it back to the available pool. */
_nx_dhcp_update_assignable_ip_address(dhcp_ptr, dhcp_client_ptr,
dhcp_client_ptr -> nx_dhcp_assigned_ip_address,
NX_DHCP_ADDRESS_STATUS_MARK_AVAILABLE);
/* The assigned address must be removed. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = NX_DHCP_NO_ADDRESS;
/* Clear the session data, including any data read from last client DHCP message. */
_nx_dhcp_clear_client_session(dhcp_ptr, dhcp_client_ptr);
}
}
}
}
/* Slow periodic event. */
if (dhcp_events & NX_DHCP_SERVER_SLOW_PERIODIC_EVENT)
{
for (iface_index = 0; iface_index < NX_MAX_PHYSICAL_INTERFACES; iface_index++)
{
/* Set a local pointer for convenience. */
iface_table_ptr = &dhcp_ptr -> nx_dhcp_interface_table[iface_index];
/* Check the interface addresses in the table for lease expiration. */
for (i = 0; i < iface_table_ptr -> nx_dhcp_address_list_size; i++)
{
iface_address_ptr = &iface_table_ptr -> nx_dhcp_ip_address_list[i];
/* Skip all the entries who are not assigned or assigned indefinately (static). */
if(iface_address_ptr -> assigned == NX_FALSE)
continue;
if(iface_address_ptr -> lease_time == TX_WAIT_FOREVER)
continue;
/* Check how much time is left before decreasing the time remaining on the lease timeout. */
if (iface_address_ptr -> lease_time >= NX_DHCP_SLOW_PERIODIC_TIME_INTERVAL)
{
/* Ok to decrement the full amount. */
iface_address_ptr -> lease_time -= NX_DHCP_SLOW_PERIODIC_TIME_INTERVAL;
}
else
{
/* Set to zero. Time's up! */
iface_address_ptr -> lease_time = 0;
}
/* Has time expired on this lease? */
if (iface_address_ptr -> lease_time == 0)
{
/* Yes, make this address available. */
/* Clear the 'owner' field. */
_nx_dhcp_clear_ip_address_owner(iface_address_ptr);
/* Look up the client in the server database by its assigned address. */
_nx_dhcp_find_client_record_by_ip_address(dhcp_ptr, &dhcp_client_ptr, iface_index,
iface_address_ptr -> nx_assignable_ip_address);
/* Did we find it? */
if (dhcp_client_ptr != NX_NULL)
{
/* Remove the assigned IP address and reset the Client to the INIT state. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = NX_DHCP_NO_ADDRESS;
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: client IP lease expired. Clear client data, release IP address\n");
#endif
/* Clear all session data if there is any for this client. */
_nx_dhcp_clear_client_session(dhcp_ptr, dhcp_client_ptr);
/* No, we should have this client in the server client records table but we don't.
That's all we can do right now. */
}
}
}
}
}
};
/* We only break out of this loop in the event of the server stopping
or a fatal error occurring.*/
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_respond_to_client_message PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function prepares a DHCP message response and sends it back to */
/* the Client. Most of the information is obtained from specified */
/* input or from client session record. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* dhcp_client_ptr Pointer to client to respond to*/
/* */
/* OUTPUT */
/* */
/* status Completion status from various*/
/* NetX calls */
/* */
/* CALLS */
/* */
/* nx_packet_allocate Allocate a DHCP packet */
/* nx_packet_release Release DHCP packet */
/* nx_udp_socket_send Send DHCP packet */
/* _nx_dhcp_add_option Add an option to the request */
/* _nx_dhcp_add_requested_option Adds non standard option to */
/* server reply to client */
/* _nx_dhcp_load_server_options Load option data from server */
/* option list */
/* _nx_dhcp_server_store_data Store data into buffer */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_listen_for_messages Service Client DHCP messages */
/* */
/**************************************************************************/
static UINT _nx_dhcp_respond_to_client_message(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr)
{
NX_PACKET *packet_ptr;
UCHAR *buffer;
UINT status;
UINT destination_ip_address;
UINT destination_port;
UINT index = 0;
#ifdef EL_PRINTF_ENABLE
/* Set local variables for convenience. */
EL_PRINTF("DHCPserv: respond to client message on interface %d\n", dhcp_client_ptr -> nx_dhcp_client_iface_index);
#endif
/* Allocate a DHCP packet. */
status = nx_packet_allocate(dhcp_ptr -> nx_dhcp_packet_pool_ptr, &packet_ptr, NX_IPv4_UDP_PACKET, NX_DHCP_PACKET_ALLOCATE_TIMEOUT);
/* Was the packet allocation successful? */
if (status != NX_SUCCESS)
{
/* Return status. */
return(status);
}
/* Setup the packet buffer pointer. */
buffer = packet_ptr -> nx_packet_prepend_ptr;
/* Clear the buffer memory. */
memset((void *) buffer, 0, NX_DHCP_OFFSET_END);
/* Setup the standard DHCP fields. */
buffer[NX_DHCP_OFFSET_OP] = NX_DHCP_OP_REPLY;
buffer[NX_DHCP_OFFSET_HTYPE] = (UCHAR)dhcp_client_ptr -> nx_dhcp_client_hwtype;
buffer[NX_DHCP_OFFSET_HLEN] = (UCHAR)dhcp_client_ptr -> nx_dhcp_client_hwlen;
buffer[NX_DHCP_OFFSET_HOPS] = 0;
buffer[NX_DHCP_OFFSET_BOOT_FILE] = 0;
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_XID, 4, dhcp_client_ptr -> nx_dhcp_xid);
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_SECS, 2, 0);
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_CLIENT_IP, 4, NX_DHCP_NO_ADDRESS);
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_SERVER_IP, 4, NX_DHCP_NO_ADDRESS);
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_RELAY_IP, 4, NX_DHCP_NO_ADDRESS);
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_FLAGS, 1, dhcp_client_ptr -> nx_dhcp_broadcast_flag_set);
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_CLIENT_HW, 2, dhcp_client_ptr -> nx_dhcp_client_mac_msw);
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_CLIENT_HW + 2, 4, dhcp_client_ptr -> nx_dhcp_client_mac_lsw);
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_VENDOR, 4, NX_DHCP_MAGIC_COOKIE);
/* Update the index. */
index = NX_DHCP_OFFSET_OPTIONS;
/* Add the list of options that go out with ALL server messages. */
status = _nx_dhcp_load_server_options(dhcp_ptr, dhcp_client_ptr, buffer, NX_DHCP_OPTIONS_FOR_ALL_REPLIES, &index);
/* Unless the server is sending a NACK, there are more options to add for the server reply. */
if (dhcp_client_ptr -> nx_dhcp_response_type_to_client != NX_DHCP_TYPE_DHCPNACK)
{
/* Add the list of options that go out with server ACK replies (standard network parameters). */
_nx_dhcp_load_server_options(dhcp_ptr, dhcp_client_ptr, buffer, NX_DHCP_OPTIONS_FOR_GENERIC_ACK, &index);
/* Determine if there are any message specific options to add. */
switch (dhcp_client_ptr -> nx_dhcp_message_type)
{
case NX_DHCP_TYPE_DHCPDISCOVER:
/* Are we returning an OFFER to the Client? */
if (dhcp_client_ptr -> nx_dhcp_response_type_to_client == NX_DHCP_TYPE_DHCPOFFER)
{
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_YOUR_IP, 4, dhcp_client_ptr -> nx_dhcp_your_ip_address);
/* Add the list of options that go out with server OFFER replies. */
_nx_dhcp_load_server_options(dhcp_ptr, dhcp_client_ptr, buffer, NX_DHCP_OPTIONS_FOR_REPLY_TO_OFFER, &index);
}
/* Increment the number of Discovery messages received. */
dhcp_ptr -> nx_dhcp_discoveries_received++;
break;
case NX_DHCP_TYPE_DHCPREQUEST:
/* Are we returning an ACK to the Client? */
if (dhcp_client_ptr -> nx_dhcp_response_type_to_client == NX_DHCP_TYPE_DHCPACK)
{
_nx_dhcp_server_store_data(buffer + NX_DHCP_OFFSET_YOUR_IP, 4, dhcp_client_ptr -> nx_dhcp_assigned_ip_address);
/* Add the list of options that go out with server ACKs replies. */
_nx_dhcp_load_server_options(dhcp_ptr, dhcp_client_ptr, buffer, NX_DHCP_OPTIONS_FOR_REPLY_TO_REQUEST, &index);
}
/* Increment the number of Request messages received. */
dhcp_ptr -> nx_dhcp_requests_received++;
break;
/* Note: DHCP Servers should not reply to NX_DHCP_REPLY_TO_RELEASE or NX_DHCP_REPLY_TO_DECLINE messages. */
case NX_DHCP_TYPE_DHCPRELEASE:
dhcp_ptr -> nx_dhcp_releases_received++;
break;
case NX_DHCP_TYPE_DHCPDECLINE:
dhcp_ptr -> nx_dhcp_declines_received++;
break;
case NX_DHCP_TYPE_DHCPINFORM:
/* At this time the server sends just standard network parameters to INFORM requests.
so no additional options to add. */
/* Increment the number of Inform messages received. */
dhcp_ptr -> nx_dhcp_informs_received++;
break;
/* No other DHCP messages are supported by NetX DHCP Server at this time. */
default:
break;
}
/* Determine which of the remaining client requested options the server has information for. */
_nx_dhcp_load_server_options(dhcp_ptr, dhcp_client_ptr, buffer, NX_DHCP_OPTIONS_REQUESTED_BY_CLIENT, &index);
}
/* Setup the packet pointers. */
/* Added the END option. */
*(buffer + index) = NX_DHCP_SERVER_OPTION_END;
index ++;
/* Check the option length. */
if (index > NX_DHCP_OFFSET_END)
{
packet_ptr -> nx_packet_length = index;
packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr + index;
}
else
{
packet_ptr -> nx_packet_length = NX_DHCP_OFFSET_END;
packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_END;
}
/* DHCP Server set the correct destination address and port to send resposne. RFC2131, Section4.1, Page23.
1. If the 'giaddr' field in a DHCP message from a client is non-zero, the server sends any return messages to the 'DHCP server' port on the
BOOTP relay agent whose address appears in 'giaddr'.
2. If the 'giaddr'field is zero and the 'ciaddr' field is nonzero, then the server unicasts DHCPOFFER and DHCPACK messages to the address in 'ciaddr'.
3. If 'giaddr' is zero and 'ciaddr' is zero, and the broadcast bit is set, then the server broadcasts DHCPOFFER and DHCPACK messages to 0xffffffff.
4. If the broadcast bit is not set and 'giaddr' is zero and 'ciaddr' is zero, then the server unicasts DHCPOFFER and DHCPACK messages to the client's hardware address and 'yiaddr' address.
5. In all cases, when 'giaddr' is zero, the server broadcasts any DHCPNAK messages to 0xffffffff. */
/* Check the 'giaddr' field. */
if (dhcp_client_ptr -> nx_dhcp_relay_ip_address)
{
/* Set the destination address and port. */
destination_ip_address = dhcp_client_ptr -> nx_dhcp_relay_ip_address;
destination_port = NX_DHCP_SERVER_UDP_PORT;
}
else
{
/* Check the DHCP response message type. */
if (dhcp_client_ptr -> nx_dhcp_response_type_to_client != NX_DHCP_TYPE_DHCPNACK)
{
/* Check the 'ciaddr' field*/
if (dhcp_client_ptr -> nx_dhcp_clientip_address)
{
destination_ip_address = dhcp_client_ptr -> nx_dhcp_clientip_address;
}
/* Check the broadcast bit. */
else if (dhcp_client_ptr -> nx_dhcp_broadcast_flag_set & NX_DHCP_FLAGS_BROADCAST)
{
destination_ip_address = NX_DHCP_BC_ADDRESS;
}
else
{
destination_ip_address = dhcp_client_ptr -> nx_dhcp_your_ip_address;
/* Add the dynamic arp entry. */
status = nx_arp_dynamic_entry_set(dhcp_ptr -> nx_dhcp_ip_ptr, destination_ip_address, dhcp_client_ptr -> nx_dhcp_client_mac_msw, dhcp_client_ptr -> nx_dhcp_client_mac_lsw);
/* Check the status. */
if (status)
{
nx_packet_release(packet_ptr);
return(status);
}
}
}
else
{
destination_ip_address = NX_DHCP_BC_ADDRESS;
}
/* Set the destination port. */
destination_port = NX_DHCP_CLIENT_UDP_PORT;
}
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: DHCP reply packet destination 0x%x\n", destination_ip_address);
EL_PRINTF("DHCPserv: DHCP reply buffer size (bytes) %d\n", packet_ptr -> nx_packet_length);
EL_PRINTF("DHCPserv: DHCP reply packet payload size (bytes) %d\n\n", packet_ptr -> nx_packet_pool_owner -> nx_packet_pool_payload_size);
#endif
#ifdef PACKET_DUMP
{
ULONG *work_ptr;
ULONG uword;
UINT i = 0;
UINT index = 0;
UINT length;
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: server reply to client - packet dump:\n");
#endif
work_ptr = (ULONG *)packet_ptr -> nx_packet_prepend_ptr;
length = ((packet_ptr -> nx_packet_length + sizeof(ULONG) - 1)/sizeof(ULONG));
while(i < length)
{
uword = *work_ptr;
if (index == 10)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("\n");
#endif
index = 0;
}
#ifdef EL_PRINTF_ENABLE
EL_PRINTF(" %08x", uword);
#endif
work_ptr++;
index++;
i++;
}
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("\n\n");
#endif
}
#endif /* #ifdef PACKET_DUMP */
/* Send the packet. */
status = nx_udp_socket_interface_send(&(dhcp_ptr -> nx_dhcp_socket), packet_ptr, destination_ip_address, destination_port, dhcp_client_ptr -> nx_dhcp_client_iface_index);
if (status != NX_SUCCESS)
{
nx_packet_release(packet_ptr);
}
/* Return completion status. */
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_clear_client_session PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function clears data from the current client session (e.g. read*/
/* from the most recent client message. It does remove anything in the */
/* interface table associated with this client's subnet, nor change the*/
/* client state. */
/* */
/* Permanent client info such as client host name, client mac address */
/* assigned IP address, or lease time is not cleared (left to the */
/* caller). */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* dhcp_client_ptr Pointer to DHCP Client */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful outcome */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_listen_for_messages Handle Client DHCP messages */
/* _nx_dhcp_fast_periodic_timer_entry Session timer entry function */
/* */
/**************************************************************************/
static UINT _nx_dhcp_clear_client_session(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr)
{
UINT i;
NX_PARAMETER_NOT_USED(dhcp_ptr);
dhcp_client_ptr -> nx_dhcp_message_type = 0;
dhcp_client_ptr -> nx_dhcp_response_type_to_client = 0;
dhcp_client_ptr -> nx_dhcp_xid = 0;
dhcp_client_ptr -> nx_dhcp_requested_ip_address = 0x0;
dhcp_client_ptr -> nx_dhcp_requested_lease_time = 0x0;
dhcp_client_ptr -> nx_dhcp_your_ip_address = 0x0;
dhcp_client_ptr -> nx_dhcp_clientip_address = 0x0;
dhcp_client_ptr -> nx_dhcp_server_id = 0x0;
dhcp_client_ptr -> nx_dhcp_clientrec_server_ip = 0x0;
dhcp_client_ptr -> nx_dhcp_broadcast_flag_set = NX_DHCP_FLAGS_BROADCAST;
dhcp_client_ptr -> nx_dhcp_session_timeout = 0;
dhcp_client_ptr -> nx_dhcp_destination_ip_address = 0;
dhcp_client_ptr -> nx_dhcp_source_ip_address = 0;
/* Clear out the option data. */
dhcp_client_ptr -> nx_dhcp_client_option_count = 0;
for (i = 0; i < NX_DHCP_CLIENT_OPTIONS_MAX; i++)
{
dhcp_client_ptr -> nx_dhcp_user_options[0] = 0;
}
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nxe_dhcp_clear_client_record PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function performs error checking for the clear client record */
/* service. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* dhcp_client_ptr Pointer to DHCP Client */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful outcome */
/* */
/* CALLS */
/* */
/* _nx_dhcp_clear_client_record Actual clear client record service */
/* */
/* CALLED BY */
/* */
/* Host Application */
/* */
/**************************************************************************/
UINT _nxe_dhcp_clear_client_record(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr)
{
UINT status;
/* Check for invalid input. */
if ((dhcp_ptr == NX_NULL) || (dhcp_client_ptr == NX_NULL))
{
return(NX_PTR_ERROR);
}
/* Check for appropriate caller. */
NX_THREADS_ONLY_CALLER_CHECKING
/* Call actual DHCP clear client record service. */
status = _nx_dhcp_clear_client_record(dhcp_ptr, dhcp_client_ptr);
/* Return status. */
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_clear_client_record PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function clears the entire client record from the server table.*/
/* It also frees up the client's IP address in the server IP address */
/* table. */
/* */
/* The DHCP server does not use this function, it is strictly for the */
/* host application to remove clients from the table (e.g. to free up */
/* room for new clients by removing old 'stale' client entries no */
/* longer configured with the server. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* dhcp_client_ptr Pointer to DHCP Client */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful outcome */
/* */
/* CALLS */
/* */
/* _nx_dhcp_clear_ip_address_owner Clear IP address owner */
/* */
/* CALLED BY */
/* */
/* Host Application */
/* */
/**************************************************************************/
UINT _nx_dhcp_clear_client_record(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr)
{
UINT i;
NX_DHCP_INTERFACE_TABLE *iface_table_ptr;
if (dhcp_client_ptr == 0x0)
{
/* Benign error. Return success. */
return(NX_SUCCESS);
}
/* Obtain DHCP Server mutex protection,. */
tx_mutex_get(&dhcp_ptr -> nx_dhcp_mutex, NX_WAIT_FOREVER);
/* Set local pointer for convenience. */
iface_table_ptr = &dhcp_ptr -> nx_dhcp_interface_table[dhcp_client_ptr -> nx_dhcp_client_iface_index];
i = 0;
/* Does the client have an assigned IP address? */
if (iface_table_ptr -> nx_dhcp_ip_address_list[i].nx_assignable_ip_address)
{
/* Yes, We need to free up the Client's assigned IP address in the server database. */
while (i < iface_table_ptr -> nx_dhcp_address_list_size)
{
/* Find the interface table entry by matching IP address. */
if (iface_table_ptr -> nx_dhcp_ip_address_list[i].nx_assignable_ip_address ==
dhcp_client_ptr -> nx_dhcp_assigned_ip_address)
{
/* Clear the owner information. */
_nx_dhcp_clear_ip_address_owner(&(iface_table_ptr -> nx_dhcp_ip_address_list[i]));
/* Address now available for DHCP client. */
break;
}
i++;
}
}
/* Ok to clear the Client record. */
memset(dhcp_client_ptr, 0, sizeof(NX_DHCP_CLIENT));
/* Update the total number of dhcp clients. */
if (dhcp_ptr -> nx_dhcp_number_clients > 0)
{
dhcp_ptr -> nx_dhcp_number_clients--;
}
/* Release DHCP Server mutex. */
tx_mutex_put(&dhcp_ptr -> nx_dhcp_mutex);
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_load_server_options PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function adds the specified options in the server's DHCP option */
/* list to the DHCP server response to the Client message. There is a */
/* required set of options that go out with all server messages, as well */
/* as a standard (user configurable) set of options for all 'ACK' */
/* messages. Lastly, there are certain options that are specific to the */
/* client message request e.g. RENEW vs OFFER etc */
/* */
/* There is no support for responding to specific client options in this */
/* revision. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* dhcp_client_ptr Pointer to client session record*/
/* buffer Packet buffer to load options to*/
/* option_type Category of options to load */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful completion status */
/* */
/* CALLS */
/* */
/* _nx_dhcp_add_option Add option to response (specify */
/* option and data to add)*/
/* _nx_dhcp_add_requested_option Add option to response */
/* (specify option and client */
/* interface) */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_respond_to_dhcp_message Create and send response back */
/* DHCP Client */
/* */
/**************************************************************************/
static UINT _nx_dhcp_load_server_options(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr, UCHAR *buffer, UINT option_type, UINT *index)
{
UINT i;
UINT iface_index;
UINT renew_time;
UINT rebind_time;
UINT lease_time;
/* Compute default lease, renew and rebind times to offer the client. */
lease_time = dhcp_client_ptr -> nx_dhcp_requested_lease_time;
#if (NX_DHCP_DEFAULT_LEASE_TIME >= 0xFFFFFFFF)
if (lease_time == 0)
#else
if ((lease_time == 0) || (lease_time > NX_DHCP_DEFAULT_LEASE_TIME))
#endif
{
lease_time = NX_DHCP_DEFAULT_LEASE_TIME;
}
renew_time = lease_time/2;
rebind_time = 7 * (lease_time/8);
/* Set a local pointer for convenience. */
iface_index = dhcp_client_ptr -> nx_dhcp_client_iface_index;
/* Determine what set of options to apply based on caller input. */
if (option_type == NX_DHCP_OPTIONS_FOR_ALL_REPLIES)
{
/* This is the generic set of options for ALL server replies. */
/* Set the DHCP message type the server is sending back to client. */
_nx_dhcp_add_option(buffer, NX_DHCP_SERVER_OPTION_DHCP_TYPE, NX_DHCP_SERVER_OPTION_DHCP_TYPE_SIZE,
dhcp_client_ptr -> nx_dhcp_response_type_to_client, index);
/* Add the server identifier to all messages to the client. */
_nx_dhcp_add_option(buffer, NX_DHCP_SERVER_OPTION_DHCP_SERVER_ID, NX_DHCP_SERVER_OPTION_DHCP_SERVER_SIZE,
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_server_ip_address, index);
return(NX_SUCCESS);
}
else if (option_type == NX_DHCP_OPTIONS_FOR_REPLY_TO_OFFER)
{
/* Offer an IP lease. */
_nx_dhcp_add_option(buffer, NX_DHCP_SERVER_OPTION_DHCP_LEASE, NX_DHCP_SERVER_OPTION_DHCP_LEASE_SIZE, lease_time, index);
/* With the computed renew time. */
_nx_dhcp_add_option(buffer, NX_DHCP_SERVER_OPTION_RENEWAL, NX_DHCP_SERVER_OPTION_RENEWAL_SIZE, renew_time, index);
/* And rebind time. */
_nx_dhcp_add_option(buffer, NX_DHCP_SERVER_OPTION_REBIND, NX_DHCP_SERVER_OPTION_REBIND_SIZE, rebind_time, index);
}
else if (option_type == NX_DHCP_OPTIONS_FOR_REPLY_TO_REQUEST)
{
/* Confirm the assigned IP lease, renew and rebind times. */
_nx_dhcp_add_option(buffer, NX_DHCP_SERVER_OPTION_DHCP_LEASE, NX_DHCP_SERVER_OPTION_DHCP_LEASE_SIZE, lease_time, index);
_nx_dhcp_add_option(buffer, NX_DHCP_SERVER_OPTION_RENEWAL, NX_DHCP_SERVER_OPTION_RENEWAL_SIZE, renew_time, index);
_nx_dhcp_add_option(buffer, NX_DHCP_SERVER_OPTION_REBIND, NX_DHCP_SERVER_OPTION_REBIND_SIZE, rebind_time, index);
}
else if (option_type == NX_DHCP_OPTIONS_FOR_REPLY_TO_INFORM)
{
/* The NetX DHCP Server reply to Inform messages includes the standard server option
list which is automatically included. */
}
else if (option_type == NX_DHCP_OPTIONS_FOR_GENERIC_ACK)
{
/* Add the standard DHCP server information (e.g. subnet, router IP etc) which are
appended to the required server options. */
for (i= NX_DHCP_REQUIRED_SERVER_OPTION_SIZE; i < dhcp_ptr -> nx_dhcp_server_option_count; i++)
{
/* Append this DHCP option to the client message. */
_nx_dhcp_add_requested_option(dhcp_ptr, dhcp_client_ptr -> nx_dhcp_client_iface_index,
buffer, dhcp_ptr -> nx_dhcp_server_options[i], index);
}
}
else if (option_type == NX_DHCP_OPTIONS_REQUESTED_BY_CLIENT)
{
/* The NetX DHSP Server does not (yet) support this feature in the current release. */
/* Clear the current DHCP Client's requested option list. */
for (i = 0; i < dhcp_client_ptr -> nx_dhcp_client_option_count; i++)
{
dhcp_client_ptr -> nx_dhcp_user_options[i] = 0;
}
dhcp_client_ptr -> nx_dhcp_client_option_count = 0;
}
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_set_default_server_options PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function parses a set of options from the supplied buffer into */
/* the server's list of options to supply data to the DHCP Client. If the*/
/* server is configured to use default option data, it will use this list*/
/* of options to compose the response to the Client. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* buffer Pointer to option list */
/* size Size of option list buffer */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful completion status */
/* status Actual error status */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_server_create Create the DHCP Server instance*/
/* */
/**************************************************************************/
static UINT _nx_dhcp_set_server_options(NX_DHCP_SERVER *dhcp_ptr, CHAR *buffer, UINT buffer_size)
{
UINT j;
UINT digit;
UINT status;
CHAR *work_ptr;
j = 0;
/* Search through the text for all options to load. */
while(buffer_size && (j < NX_DHCP_SERVER_OPTION_LIST_SIZE))
{
/* Check if we're off the end of the buffer. */
if (buffer == 0x0)
{
break;
}
work_ptr = buffer;
/* This should advance the buffer past the digit.*/
status = _nx_dhcp_parse_next_option(&buffer, &digit, buffer_size);
if (status)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Unable to set server DHCP option data list. Status 0x%x\n", status);
#endif
return(status);
}
buffer_size -= (UINT)(buffer - work_ptr);
/* Load the parsed option into the server 'option list.' */
dhcp_ptr -> nx_dhcp_server_options[j] = digit;
j++;
}
/* Update the number of server options in the list. */
dhcp_ptr -> nx_dhcp_server_option_count = j;
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_parse_next_option PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function parses space or comma separated numeric data from the */
/* supplied buffer, for example, the server option list. */
/* */
/* INPUT */
/* */
/* option_list Pointer to buffer to parse */
/* option Data parsed from buffer */
/* length Size of option list buffer */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful completion status */
/* NX_DHCP_INTERNAL_OPTION_PARSE_ERROR Parsing error status */
/* */
/* CALLS */
/* */
/* _nx_utility_string_to_uint Convert ascii number to integer*/
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_set_default_server_options Creates the server option list */
/* */
/**************************************************************************/
static UINT _nx_dhcp_parse_next_option(CHAR **option_list, UINT *option, UINT length)
{
CHAR c;
UINT j;
UINT buffer_index;
CHAR num_string[3];
CHAR *buffer;
/* Check for invalid input. */
if ((option_list == 0x0) || (option == 0) || (length == 0))
{
return(NX_DHCP_INTERNAL_OPTION_PARSE_ERROR);
}
buffer = *option_list;
memset(&num_string[0], 0, 3);
/* Initialize parsed option to undefined option (sorry, zero is taken by the PAD option
which I personally think was a bad programming decision). */
*option = 999;
j = 0;
buffer_index = 0;
/* Get the first character in the buffer. */
c = *buffer++;
/* Get the first digit (non separator character)*/
while ((c == ' ') || (c == ','))
{
c = *buffer++;
j++;
}
/* Now parse the next characters until the end of the buffer or next separator character. */
while ((j < length) && (buffer_index < 3))
{
/* Check for separator marking the end of the option string. */
if ((c == ' ') || (c == ',') || (c == 0x0))
break;
/* Check for an invalid digit or out of range option. */
if (c < 0x30 || c > 0x39 || buffer_index > 2)
{
return(NX_DHCP_BAD_OPTION_LIST_ERROR);
}
num_string[buffer_index] = c;
j++;
/* Append the character ('digit') into a number buffer. */
c = *buffer++;
buffer_index++;
}
*option_list += j;
/* Convert the number string to an actual unsigned integer. */
return(_nx_utility_string_to_uint(num_string, buffer_index, option));
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_packet_process PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function processes a Client DHCP message and extracts */
/* information from a client DHCP message to create or update the */
/* client record. It handles the DHCP message based on type, sets */
/* a timer on the client record if a response is expected from the */
/* client, and waits for another DHCP packet. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Message handled successfully */
/* NO_PACKET No packet received */
/* NX_DHCP_SERVER_BAD_INTERFACE_INDEX Packet interface not recognized*/
/* status Actual completion outcome */
/* */
/* CALLS */
/* */
/* nx_packet_release Release DHCP packet */
/* nx_udp_socket_receive Receive DHCP packet */
/* _nx_dhcp_server_extract_information Extract DHCP packet info */
/* _nx_dhcp_validate_client_message Process the Client message */
/* _nx_dhcp_server_assign_ip_address Assign IP address to Client */
/* _nx_dhcp_respond_to_client_message Create and send response back */
/* _nx_dhcp_clear_client_session Clears client session data */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_server_thread_entry DHCP Server thread task */
/* */
/**************************************************************************/
static UINT _nx_dhcp_server_packet_process(NX_DHCP_SERVER *dhcp_ptr, NX_PACKET *packet_ptr)
{
UINT status;
UINT iface_index;
NX_DHCP_CLIENT *dhcp_client_ptr = NX_NULL;
NX_PACKET *new_packet_ptr;
ULONG bytes_copied = 0;
ULONG offset;
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("\n");
EL_PRINTF("DHCPserv: Waiting to receive next packet\n");
#endif
/* Check for minimum sized DHCP packet (e.g. just the DHCP header and 0 bytes of option data). */
if (packet_ptr -> nx_packet_length < NX_DHCP_HEADER_SIZE)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Client DHCP packet is malformed or empty. Invalid DHCP packet.\n");
#endif
/* No; Release the original packet. */
nx_packet_release(packet_ptr);
/* Return successful completion status. */
return(NX_DHCP_MALFORMED_DHCP_PACKET);
}
/* Get the interface index. */
nx_udp_packet_info_extract(packet_ptr, NX_NULL, NX_NULL, NX_NULL, &iface_index);
/* Does the DHCP server have a table for this packet interface? */
if (iface_index >= NX_MAX_PHYSICAL_INTERFACES)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: No interface found for DHCP packet\n");
#endif
/* Release the original packet. */
nx_packet_release(packet_ptr);
/* No, return the error status. */
return(NX_DHCP_SERVER_BAD_INTERFACE_INDEX);
}
/* We will copy the received packet (datagram) over to a packet from the DHCP Server pool and release
the packet from the receive packet pool as soon as possible. */
status = nx_packet_allocate(dhcp_ptr -> nx_dhcp_packet_pool_ptr, &new_packet_ptr, NX_IPv4_UDP_PACKET, NX_DHCP_PACKET_ALLOCATE_TIMEOUT);
/* Check status. */
if (status != NX_SUCCESS)
{
/* Release the original packet. */
nx_packet_release(packet_ptr);
/* Error allocating packet, return error status. */
return(status);
}
/* Update the prepend pointer to make sure that the IP header and UDP header also are copied into new packet. */
packet_ptr -> nx_packet_prepend_ptr -= 28;
packet_ptr -> nx_packet_length += 28;
/* Verify the incoming packet does not exceed our DHCP Server packet payload. */
if ((ULONG)(new_packet_ptr -> nx_packet_data_end - new_packet_ptr -> nx_packet_prepend_ptr) < (packet_ptr -> nx_packet_length))
{
/* Release the original packet. */
nx_packet_release(packet_ptr);
/* Release the newly allocated packet and return an error. */
nx_packet_release(new_packet_ptr);
return(NX_DHCP_INADEQUATE_PACKET_POOL_PAYLOAD);
}
/* Initialize the offset to the beginning of the packet buffer. */
offset = 0;
status = nx_packet_data_extract_offset(packet_ptr, offset, (VOID *)new_packet_ptr -> nx_packet_prepend_ptr, packet_ptr -> nx_packet_length, &bytes_copied);
/* Check status. */
if ((status != NX_SUCCESS) || (bytes_copied != packet_ptr -> nx_packet_length))
{
/* Release the original packet. */
nx_packet_release(packet_ptr);
/* Release the allocated packet we'll never send. */
nx_packet_release(new_packet_ptr);
/* Error extracting packet buffer, return error status. */
return(status);
}
/* Update the new packet with the bytes copied, and removed.
For chained packets, this will reflect the total 'datagram' length. */
new_packet_ptr -> nx_packet_prepend_ptr += 28;
new_packet_ptr -> nx_packet_length = bytes_copied - 28;
new_packet_ptr -> nx_packet_append_ptr = new_packet_ptr -> nx_packet_prepend_ptr + new_packet_ptr -> nx_packet_length;
/* Now we can release the original packet. */
nx_packet_release(packet_ptr);
/* Extract the DHCP specific information from the packet. This will create new record or update existing
client record in the server database. */
status = _nx_dhcp_server_extract_information(dhcp_ptr, &dhcp_client_ptr, new_packet_ptr, iface_index);
#ifdef PACKET_DUMP
{
ULONG *work_ptr;
ULONG uword;
UINT i = 0;
UINT index = 0;
UINT length;
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: received client packet dump:\n");
#endif
/* Get a pointer to the IP header and adjust the packet length for IP and UDP header size. */
work_ptr = (ULONG *)(packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_UDP_HEADER) - sizeof(NX_IPV4_HEADER));
length = ((packet_ptr -> nx_packet_length + sizeof(ULONG) - 1)/sizeof(ULONG)) + sizeof(NX_UDP_HEADER) + sizeof(NX_IPV4_HEADER);
while(i < length)
{
uword = *work_ptr;
if (index == 10)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("\n");
#endif
index = 0;
}
#ifdef EL_PRINTF_ENABLE
EL_PRINTF(" %08x", uword);
#endif
work_ptr++;
index++;
i++;
}
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("\n\n");
#endif
}
#endif /* #ifdef PACKET_DUMP */
/* We are done with this packet now regardless of the success of the above operations. */
nx_packet_release(new_packet_ptr);
/* Check if we have an invalid client record. */
if ((status != NX_SUCCESS) && dhcp_client_ptr)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Error extracting DHCP packet (invalid): 0x%x\n", status);
#endif
/* Client record was created but there was a problem with the packet
or client message. Since we're not sure what got written to the Client record,
remove packet data out completely. */
_nx_dhcp_clear_client_record(dhcp_ptr, dhcp_client_ptr);
}
/* Check for general errors or failure to produce a client record. */
if ((status != NX_SUCCESS) || !dhcp_client_ptr)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Internal error extracting DHCP packet: 0x%x\n", status);
#endif
/* Return the error from extracting packet data. */
return(status);
}
/* Clear any previous response type in the client record. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = 0;
/* Validate the client message including how the server should respond to it. */
status = _nx_dhcp_validate_client_message(dhcp_ptr, dhcp_client_ptr);
/* Check for errors. */
if (status != NX_SUCCESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: DHCP packet failed validation: 0x%x\n", status);
#endif
/* Return all other errors as completion status. Do not send a message to the client. */
return(status);
}
/* Assign/confirm the assigned IP address of the Client if the server is not returning
a NACK, responding to an INFORM message, or not responding at all. */
if ((dhcp_client_ptr -> nx_dhcp_response_type_to_client != NX_DHCP_TYPE_DHCPNACK) &&
(dhcp_client_ptr -> nx_dhcp_response_type_to_client != NX_DHCP_TYPE_DHCPSILENT) &&
(dhcp_client_ptr -> nx_dhcp_message_type != NX_DHCP_TYPE_DHCPINFORM))
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: assigning IP address to DHCP client...\n");
#endif
/* Assign/confirm an IP address to the client. If the client is renewing or rebinding
this will verify they sent the correct IP address and reset the lease time. */
status = _nx_dhcp_server_assign_ip_address(dhcp_ptr, dhcp_client_ptr);
/* Has the server run out of available addresses? Handle this 'error'
condition separately. */
if (status == NX_DHCP_NO_AVAILABLE_IP_ADDRESSES)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Unable to assign IP address; none available\n");
#endif
/* Actually as yet, there is no callback to the host application
for handling this situation.
The server should still send a NACK to the Client that it cannot
provide what it requests. */
}
/* Check for errors other than running out of available IP addresses. */
else if (status != NX_SUCCESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Unable to assign IP address. Status: 0x%x\n", status);
#endif
/* Return all other errors as completion status. Do not send a message to the client. */
return(status);
}
}
#ifdef EL_PRINTF_ENABLE
if (dhcp_client_ptr -> nx_dhcp_response_type_to_client == NX_DHCP_TYPE_DHCPOFFER)
EL_PRINTF("DHCPserv: response to client: OFFER\n");
else if (dhcp_client_ptr -> nx_dhcp_response_type_to_client == NX_DHCP_TYPE_DHCPACK)
EL_PRINTF("DHCPserv: response to client: ACK\n");
else if (dhcp_client_ptr -> nx_dhcp_response_type_to_client == NX_DHCP_TYPE_DHCPNACK)
EL_PRINTF("DHCPserv: response to client: NACK\n");
else if (dhcp_client_ptr -> nx_dhcp_response_type_to_client == NX_DHCP_TYPE_DHCPSILENT)
EL_PRINTF("DHCPserv: response to client: SILENT (no response)\n");
#endif
/* Is there a response to send to the Client? */
if (dhcp_client_ptr -> nx_dhcp_response_type_to_client != NX_DHCP_TYPE_DHCPSILENT)
{
/* Yes; use the session data in the client record to compose the server reply. */
status = _nx_dhcp_respond_to_client_message(dhcp_ptr, dhcp_client_ptr);
/* Check for error. */
if (status != NX_SUCCESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Error sending response to DHCP client. Status: 0x%x\n", status);
#endif
/* Return error completion status. Error sending a message to client. */
return(status);
}
/* Update the client state if an ACK was sent successfully. */
if (dhcp_client_ptr -> nx_dhcp_response_type_to_client == NX_DHCP_TYPE_DHCPACK)
{
/* Was the client was in a requesting, renewing or rebinding state? */
if ((dhcp_client_ptr -> nx_dhcp_client_state == NX_DHCP_STATE_RENEWING) ||
(dhcp_client_ptr -> nx_dhcp_client_state == NX_DHCP_STATE_REBINDING) ||
(dhcp_client_ptr -> nx_dhcp_client_state == NX_DHCP_STATE_REQUESTING))
{
/* Yes, the client should now be bound to an IP address. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_BOUND;
}
}
}
/*
If the Client is BOUND, the session is over. Clear the session including
the sesison timeout.
If the Client is still in the BOOT or INIT state something was wrong with their
message or else the server had a problem. Clear the record and they
can start a new session with a retransmission of their request.
*/
/* Do we need to clear the session data depending? */
if (dhcp_client_ptr -> nx_dhcp_client_state == NX_DHCP_STATE_BOOT ||
dhcp_client_ptr -> nx_dhcp_client_state == NX_DHCP_STATE_INIT ||
dhcp_client_ptr -> nx_dhcp_client_state == NX_DHCP_STATE_BOUND)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: client session completed. Clear session data.\n");
#endif
/* Yes, clear all data from the session. Client assigned IP address
and hardware address are not cleared.*/
_nx_dhcp_clear_client_session(dhcp_ptr,dhcp_client_ptr);
}
else
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Waiting for DHCP client response...\n");
#endif
/* No; assume the Client is Renewing, rebinding, or selecting,
to set the session timeout to await the next Client response;
(not finished with this DHCP session yet). */
/* Assume we have received at least one message from the Client, so
reset the timer on the client session timeout. */
dhcp_client_ptr -> nx_dhcp_session_timeout = NX_DHCP_CLIENT_SESSION_TIMEOUT;
}
/* Output Server session data if enabled. */
#ifdef TESTOUTPUT
add_client++;
/* Only print out the session table depending on trace interval. */
if ((TRACE_NTH_CLIENT_PACKET > 0) && (add_client % TRACE_NTH_CLIENT_PACKET == 0))
{
UINT i;
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("\nClient\t\tAssigned\tLease\t\tSession\tState\n");
#endif
for (i = 0; i < dhcp_ptr -> nx_dhcp_number_clients; i++)
{
NX_DHCP_CLIENT *client_ptr;
client_ptr = &(dhcp_ptr -> client_records[i]);
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("0x%x 0x%x\t0x%08x\t0x%08x\t%d\t%d\n",
client_ptr -> nx_dhcp_client_mac_msw,
client_ptr -> nx_dhcp_client_mac_lsw,
client_ptr -> nx_dhcp_assigned_ip_address,
client_ptr -> nx_dhcp_requested_lease_time,
client_ptr -> nx_dhcp_session_timeout,
client_ptr -> nx_dhcp_client_state);
#endif
}
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("\nHost\tIP address\tAssigned? Lease\n");
EL_PRINTF("Interface 0\n");
for (i = 0; i < dhcp_ptr -> nx_dhcp_interface_table[0].nx_dhcp_address_list_size; i++)
{
NX_DHCP_INTERFACE_IP_ADDRESS *interface_address_ptr;
interface_address_ptr = &(dhcp_ptr -> nx_dhcp_interface_table[0].nx_dhcp_ip_address_list[i]);
EL_PRINTF("0x%x 0x%x\t0x%x\t%d\t 0x%x\n",
interface_address_ptr -> owner_mac_msw,
interface_address_ptr -> owner_mac_lsw,
interface_address_ptr -> nx_assignable_ip_address,
interface_address_ptr -> assigned,
interface_address_ptr -> lease_time);
}
EL_PRINTF("\nInterface 1\n");
for (i = 0; i < dhcp_ptr -> nx_dhcp_interface_table[1].nx_dhcp_address_list_size; i++)
{
NX_DHCP_INTERFACE_IP_ADDRESS *interface_address_ptr;
interface_address_ptr = &(dhcp_ptr -> nx_dhcp_interface_table[1].nx_dhcp_ip_address_list[i]);
EL_PRINTF("0x%x 0x%x\t0x%x\t%d\t 0x%x\n",
interface_address_ptr -> owner_mac_msw,
interface_address_ptr -> owner_mac_lsw,
interface_address_ptr -> nx_assignable_ip_address,
interface_address_ptr -> assigned,
interface_address_ptr -> lease_time);
}
#endif
}
#endif
/* Return actual outcome status. */
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_find_client_record_by_ip_address PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function looks up a client record using the client's last known*/
/* assigned IP address. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP server */
/* dhcp_client_ptr Pointer to DHCP client */
/* iface_index Client network interface index*/
/* assigned_ip_address Client's assigned IP address */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful completion */
/* NX_DHCP_CLIENT_RECORD_NOT_FOUND Client record not found in */
/* Server database */
/* */
/* CALLS */
/* */
/* _nx_dhcp_clear_client_record Remove Client record from */
/* Server database */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_slow_periodic_timer_entry Update IP lease time outs */
/* */
/**************************************************************************/
UINT _nx_dhcp_find_client_record_by_ip_address(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT **dhcp_client_ptr,
UINT iface_index, ULONG assigned_ip_address)
{
UINT i;
NX_DHCP_CLIENT *client_record_ptr;
/* Initialize the search results to unsuccessful. */
*dhcp_client_ptr = NX_NULL;
i = 0;
/* Records are not necessarily added and deleted sequentially,
so search the whole table until a match is found. */
while (i < NX_DHCP_CLIENT_RECORD_TABLE_SIZE)
{
/* Set local pointer for convenience. */
client_record_ptr = &dhcp_ptr -> client_records[i];
/* Check the mac address for a match. */
if (client_record_ptr -> nx_dhcp_assigned_ip_address == assigned_ip_address)
{
/* Verify the client packet interface matches the interface on record. */
if (client_record_ptr -> nx_dhcp_client_iface_index == iface_index)
{
/* Return the client record location. */
*dhcp_client_ptr = client_record_ptr;
return(NX_SUCCESS);
}
else
{
/* It appears the client has changed its subnet location but not hardware type e.g.
same mac address/hardware type but not interface. Clear the old record. */
/* This will remove the client record both in the
server's client table and in the IP address database. */
_nx_dhcp_clear_client_record(dhcp_ptr, client_record_ptr);
/* Search through the rest of the server records for
another instance of this client. Either way, if not found
with the expected interface a null pointer is returned,
or new record created depending on the caller. */
}
}
i++;
}
return(NX_DHCP_CLIENT_RECORD_NOT_FOUND);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_find_client_record_by_chaddr PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function looks up a client record by the client hardware mac */
/* address. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* iface_index Client interface index */
/* client_mac_msw MSB of client hardware address*/
/* client_mac_lsw LSB of client hardware address*/
/* dhcp_client_ptr Pointer to client record entry*/
/* add_on Option to add if not found */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Message handled successfully */
/* NX_DHCP_CLIENT_TABLE_FULL No more room in Client table */
/* */
/* CALLS */
/* */
/* _nx_dhcp_clear_client_record Removes client record from */
/* server table */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_server_extract_information Extract DHCP info from Client */
/* message */
/* */
/**************************************************************************/
static UINT _nx_dhcp_find_client_record_by_chaddr(NX_DHCP_SERVER *dhcp_ptr, UINT iface_index, ULONG client_mac_msw,
ULONG client_mac_lsw, NX_DHCP_CLIENT **dhcp_client_ptr, UINT add_on)
{
UINT i;
UINT available_index;
NX_DHCP_CLIENT *client_record_ptr;
/* Initialize the search results to unsuccessful. */
*dhcp_client_ptr = NX_NULL;
/* Initialize an available slot in the table as outside the boundary (e.g. no slots available). */
available_index = NX_DHCP_CLIENT_RECORD_TABLE_SIZE;
/* Records are not necessarily added and deleted sequentially,
so search the whole table until a match is found. */
i = 0;
while (i < NX_DHCP_CLIENT_RECORD_TABLE_SIZE)
{
/* Set local pointer for convenience. */
client_record_ptr = &dhcp_ptr -> client_records[i];
/* Skip empty records. Assume a client with no mac address is empty. */
if ((client_record_ptr -> nx_dhcp_client_mac_msw == 0) && (client_record_ptr -> nx_dhcp_client_mac_lsw == 0))
{
/* Flag the first empty record in case we need to add the current client to the table. */
if (i < available_index)
available_index = i;
i++;
continue;
}
/* Check the mac address of each record for a match. */
if ((client_record_ptr -> nx_dhcp_client_mac_msw == client_mac_msw) &&
(client_record_ptr -> nx_dhcp_client_mac_lsw == client_mac_lsw))
{
/* Verify the client packet interface matches the interface on record. */
if (client_record_ptr -> nx_dhcp_client_iface_index == iface_index)
{
/* Return the client record location. */
*dhcp_client_ptr = client_record_ptr;
return(NX_SUCCESS);
}
else
{
/* It appears the client has changed its location (subnet) but not hardware type e.g. same mac
address/hware type but not interface. Remove the client record entirely and
free up any assigned IP address in the server database. */
_nx_dhcp_clear_client_record(dhcp_ptr, client_record_ptr);
/* Continue searching through the rest of the server records for
another instance of this client. Either way, if not found
with the expected interface a null pointer is returned,
or new record created depending on the caller. */
}
}
i++;
}
/* Not found. Create a record for this client? */
if (add_on == NX_FALSE)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Client HW address not found. Do not add to server database\n");
#endif
/* No, we're done then. */
return(NX_SUCCESS);
}
/* Check if there is available room in the table for a new client. */
if (available_index >= NX_DHCP_CLIENT_RECORD_TABLE_SIZE)
{
/* No, we cannot add this client so the server's table. */
return(NX_DHCP_CLIENT_TABLE_FULL);
}
/* Set local pointer to an available slot. */
client_record_ptr = &dhcp_ptr -> client_records[available_index];
/* Add this client to the server's total number of clients. */
dhcp_ptr -> nx_dhcp_number_clients++;
/* Add the supplied information to the new client record. */
client_record_ptr -> nx_dhcp_client_mac_msw = client_mac_msw;
client_record_ptr -> nx_dhcp_client_mac_lsw = client_mac_lsw;
client_record_ptr -> nx_dhcp_client_iface_index = iface_index;
/* Initialize the client state as the init state. */
client_record_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
/* Return the location of the newly created client record. */
*dhcp_client_ptr = client_record_ptr;
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_find_interface_table_ip_address PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function looks up a table entry by IP address in the specified */
/* server interface table. If not found, a NULL entry pointer is */
/* returned but successful search completion. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* iface_index Network interface index */
/* ip_address IP address to look up */
/* return_interface_address Pointer to table entry */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Message handled successfully */
/* */
/* CALLS */
/* */
/* _nx_dhcp_clear_client_record Removes client record from */
/* server table */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_server_extract_information Extract DHCP info from Client */
/* message */
/* */
/**************************************************************************/
static UINT _nx_dhcp_find_interface_table_ip_address(NX_DHCP_SERVER *dhcp_ptr, UINT iface_index, ULONG ip_address,
NX_DHCP_INTERFACE_IP_ADDRESS **return_interface_address)
{
UINT i;
NX_DHCP_INTERFACE_TABLE *dhcp_interface_table_ptr;
/* Initialize search results to NULL (not found). */
*return_interface_address = NX_NULL;
/* Set a local varible to the IP address list for this client. */
dhcp_interface_table_ptr = &(dhcp_ptr -> nx_dhcp_interface_table[iface_index]);
/* Yes, search for an available ip address in the IP list for this interface */
for(i = 0; i < dhcp_interface_table_ptr -> nx_dhcp_address_list_size; i++)
{
/* Is this address a match? */
if (dhcp_interface_table_ptr -> nx_dhcp_ip_address_list[i].nx_assignable_ip_address == ip_address)
{
/* Yes, set a pointer to the location and return. */
*return_interface_address = &dhcp_interface_table_ptr -> nx_dhcp_ip_address_list[i];
/* And we're done! */
return(NX_SUCCESS);
}
}
/* Not found, so return null pointer and successful search status. */
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_update_assignable_ip_address PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function updates the IP address status in the server interface */
/* table address for the current client. This will return an error */
/* status if the IP address being updated is not owned by the client. */
/* */
/* Note: No changes are made to the associated client record. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* dhcp_client_ptr Pointer to DHCP client */
/* ip_address IP address to update */
/* assign_status Status to assign */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Entry updated successfully */
/* NX_DHCP_IP_ADDRESS_NOT_FOUND IP address not found */
/* NX_DHCP_IP_ADDRESS_ASSIGNED_TO_OTHER IP address not assigned to */
/* current client */
/* NX_DHCP_INVALID_UPDATE_ADDRESS_CMD Unknown status input */
/* */
/* CALLS */
/* */
/* _nx_dhcp_find_interface_table_ip_address */
/* Find IP address in server */
/* interface table */
/* _nx_dhcp_find_ip_address_owner Verify Client is address owner */
/* _nx_dhcp_clear_ip_address_owner Clear IP address owner */
/* _nx_dhcp_record_ip_address_owner Sets Client as IP address owner */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_validate_client_message Process DHCP Client message*/
/* _nx_dhcp_fast_periodic_timer_entry Timer on client sessions */
/* _nx_dhcp_slow_periodic_timer_entry Timer on IP address leases */
/* message */
/* */
/**************************************************************************/
static UINT _nx_dhcp_update_assignable_ip_address(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr,
ULONG ip_address, UINT assign_status)
{
UINT iface_index;
UINT assigned_to_client;
UINT lease_time;
NX_DHCP_INTERFACE_IP_ADDRESS *interface_address_ptr;
/* Create a local variable for convenience. */
iface_index = dhcp_client_ptr -> nx_dhcp_client_iface_index;
/* Look up the IP address in the server interface IP address table. */
_nx_dhcp_find_interface_table_ip_address(dhcp_ptr, iface_index, ip_address, &interface_address_ptr);
/* Was it found? */
if (interface_address_ptr == 0x0)
{
/* No, return an error status address not found. */
return(NX_DHCP_IP_ADDRESS_NOT_FOUND);
}
/* Is the Client releasing IP address? */
if (assign_status == NX_DHCP_ADDRESS_STATUS_MARK_AVAILABLE)
{
/* Check if owner of this IP address matches with this client's record. */
_nx_dhcp_find_ip_address_owner(interface_address_ptr, dhcp_client_ptr, &assigned_to_client);
/* Is the IP address owned (leased) to this client? */
if (assigned_to_client == NX_FALSE)
{
/* No, this IP address is assigned to another host. We cannot change
the IP address status. */
return(NX_DHCP_IP_ADDRESS_ASSIGNED_TO_OTHER);
}
/* Yes, clear the owner information. */
_nx_dhcp_clear_ip_address_owner(interface_address_ptr);
}
/* Was the IP address assigned externally from the DHCP process? */
else if (assign_status == NX_DHCP_ADDRESS_STATUS_ASSIGNED_EXT)
{
/* Check the lease time. */
if (interface_address_ptr -> lease_time)
{
/* If this already has a lease time (because we assigned it most likely) don't change it. */
lease_time = interface_address_ptr -> lease_time;
}
else
{
/* Otherwise, make this lease time infinity e.g. the time out will not expire on it.*/
lease_time = NX_WAIT_FOREVER;
}
/* Set the current client as the owner. */
_nx_dhcp_record_ip_address_owner(interface_address_ptr, dhcp_client_ptr, lease_time);
}
/* Is the client informing us the IP address is already in use (e.g. it has
sent the server a DECLINE message) or accepting another DHCP server's offer? */
else if (assign_status == NX_DHCP_ADDRESS_STATUS_ASSIGNED_OTHER)
{
/* Yes; Is the client is declining the IP address? */
if (dhcp_client_ptr -> nx_dhcp_message_type == NX_DHCP_TYPE_DHCPDECLINE)
{
/* Yes, remove the Client as owner of this IP lease.
Record owner information with null 'owner' ID since we don't know who the owner is. */
_nx_dhcp_record_ip_address_owner(interface_address_ptr, NX_NULL, NX_WAIT_FOREVER);
}
else
{
/* Record owner information. */
_nx_dhcp_record_ip_address_owner(interface_address_ptr, dhcp_client_ptr, NX_WAIT_FOREVER);
}
}
else
{
/* Received an invalid update address command. */
return(NX_DHCP_INVALID_UPDATE_ADDRESS_CMD);
}
/* Successful completion! */
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_find_ip_address_owner PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function finds the owner of the specified IP address and checks*/
/* it against the specified client record to insure the client is the */
/* owner of the IP address. The result of the search is updated in the */
/* assigned_to_client pointer. */
/* */
/* INPUT */
/* */
/* iface_owner Pointer to table entry */
/* dhcp_client_ptr Pointer to DHCP client */
/* assign_status Status to assign */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Table searched successfully */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_update_assignable_ip_address Update IP address status in */
/* the server database */
/* _nx_dhcp_server_assign_ip_address Assign an IP address to the */
/* current client */
/* */
/**************************************************************************/
static UINT _nx_dhcp_find_ip_address_owner(NX_DHCP_INTERFACE_IP_ADDRESS *iface_owner, NX_DHCP_CLIENT *client_record_ptr, UINT *assigned_to_client)
{
/* Initialize the value. */
*assigned_to_client = NX_FALSE;
/* Compare the owner in the interface table entry with the client record mac address. */
if ((iface_owner -> owner_hwtype == client_record_ptr -> nx_dhcp_client_hwtype) &&
(iface_owner -> owner_mac_msw == client_record_ptr -> nx_dhcp_client_mac_msw) &&
(iface_owner -> owner_mac_lsw == client_record_ptr -> nx_dhcp_client_mac_lsw))
{
/* They match; This verifies the IP address is assigned to this client. */
*assigned_to_client = NX_TRUE;
}
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_record_ip_address_owner PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function fills in the address owner in the interface table from*/
/* the specified client record. */
/* */
/* INPUT */
/* */
/* iface_owner Pointer to table entry */
/* client_record_ptr Pointer to DHCP client */
/* lease_time Lease duration in secs */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Table searched successfully */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_update_assignable_ip_address Update IP address status in */
/* the server database */
/* _nx_dhcp_server_assign_ip_address Assign an IP address to the */
/* current client */
/* */
/**************************************************************************/
static UINT _nx_dhcp_record_ip_address_owner(NX_DHCP_INTERFACE_IP_ADDRESS *iface_owner, NX_DHCP_CLIENT *client_record_ptr, UINT lease_time)
{
/* Check the client_record_ptr. */
if (client_record_ptr)
{
/* Parse the mac address and hardware type from the client record. */
iface_owner -> owner_hwtype = client_record_ptr -> nx_dhcp_client_hwtype;
iface_owner -> owner_mac_msw = client_record_ptr -> nx_dhcp_client_mac_msw;
iface_owner -> owner_mac_lsw = client_record_ptr -> nx_dhcp_client_mac_lsw;
}
else
{
/* We don't know who the owner is, set the owner information as zero. */
iface_owner -> owner_hwtype = 0;
iface_owner -> owner_mac_msw = 0;
iface_owner -> owner_mac_lsw = 0;;
}
/* Record the lease time. */
iface_owner -> lease_time = lease_time;
/* Set the assigned status. */
iface_owner -> assigned = NX_TRUE;
/* Return. */
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_clear_ip_address_owner PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function clears the address owner information. */
/* */
/* INPUT */
/* */
/* iface_owner Pointer to table entry */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_slow_periodic_timer_entry Update IP lease time outs */
/* _nx_dhcp_clear_client_record Clear Client record */
/* _nx_dhcp_update_assignable_ip_address Update IP address status in */
/* the server database */
/* _nx_dhcp_server_assign_ip_address Assign an IP address to the */
/* current client */
/* */
/**************************************************************************/
static UINT _nx_dhcp_clear_ip_address_owner(NX_DHCP_INTERFACE_IP_ADDRESS *iface_owner)
{
/* Clear the owner information. */
iface_owner -> owner_hwtype = 0;
iface_owner -> owner_mac_msw = 0;
iface_owner -> owner_mac_lsw = 0;
/* Clear the lease time. */
iface_owner -> lease_time = 0;
/* Clear the assigned status. */
iface_owner -> assigned = NX_FALSE;
/* Return. */
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_validate_client_message PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function verifies the client message is a valid DHCP request, */
/* determines if an IP address need to be assigned, what state the DHCP*/
/* Client should be advanced to, and what the server DHCP response type*/
/* should be. in the client record the specified client record. */
/* */
/* INPUT */
/* */
/* dhcp_ptr Pointer to DHCP Server */
/* client_record_ptr Pointer to DHCP client */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Table searched successfully */
/* */
/* CALLS */
/* */
/* _nx_dhcp_update_assignable_ip_address Update IP address status in */
/* the server database */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_listen_for_messages Listen for, process and respond*/
/* to DHCP Client messages */
/* */
/**************************************************************************/
static UINT _nx_dhcp_validate_client_message(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr)
{
ULONG server_ip_address;
UINT iface_index;
NX_DHCP_INTERFACE_TABLE *dhcp_interface_table_ptr;
/* Create a local variable for the client's interface for convenience. */
iface_index = dhcp_client_ptr -> nx_dhcp_client_iface_index;
dhcp_interface_table_ptr = &dhcp_ptr -> nx_dhcp_interface_table[iface_index];
if (dhcp_client_ptr -> nx_dhcp_xid == 0x0)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: DHCP packet missing transaction ID. Reject the packet silently. \n");
#endif
/* Return the request denied message to clients and return. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPSILENT;
return(NX_SUCCESS);
}
/* Client messages should never fill in the "Your (Client) IP" or "Next Server IP" fields. */
else if ((dhcp_client_ptr -> nx_dhcp_your_ip_address != NX_DHCP_NO_ADDRESS) ||
(dhcp_client_ptr -> nx_dhcp_clientrec_server_ip != NX_DHCP_NO_ADDRESS))
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! 'Your IP'/'Server IP' fields should not be filled in.\n");
#endif
/* Return the 'request denied' message to the client. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
}
/* Check for missing required fields. */
else if ((dhcp_client_ptr -> nx_dhcp_client_hwtype == 0) ||
(dhcp_client_ptr -> nx_dhcp_client_hwlen == 0))
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! DHCP packet missing MAC and/or hardware type\n");
#endif
/* Return the request denied message to clients and return. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
}
else
{
/* Do validation specific the for message type. This section will also
advance the Client DHCP state and set the server response message type.
*/
switch (dhcp_client_ptr -> nx_dhcp_message_type)
{
case NX_DHCP_TYPE_DHCPDISCOVER:
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Received DISCOVER message\n");
#endif
/* Determine if server needs to assign an IP address based on assigned IP address.
e.g. If this appears to be a retransmission of a previous IP address DISOVERY request,
only assign another IP address if the client appears not to have been assigned one already.*/
/* Initialize response back to be an offer pending validation check. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPOFFER;
/* Check for invalid IP address field(s). */
if (dhcp_client_ptr -> nx_dhcp_clientip_address != NX_DHCP_NO_ADDRESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! Client IP address field must not be filled in.\n");
#endif
/* Let the client know it has an invalid request. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
}
else
{
/* DHCP client state advances to SElECTING state. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_SELECTING;
}
break;
}
case NX_DHCP_TYPE_DHCPREQUEST:
{
/* This is a more complex message since a client request can be generated from
4 different states: boot, selecting, renew, rebind. This requires that
we determine the Client state based on what's previously known about
the client and which fields are filled in the Request message. */
/* Get the DHCP server IP address for the client package interface. */
server_ip_address = dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_server_ip_address;
/* Get the index of the client packet interface. */
iface_index = dhcp_client_ptr -> nx_dhcp_client_iface_index;
/* If the client does not specify a server id but is requesting an IP address... */
if ((dhcp_client_ptr -> nx_dhcp_server_id == 0) &&
(dhcp_client_ptr -> nx_dhcp_requested_ip_address != NX_DHCP_NO_ADDRESS))
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Received REQUEST (BOOT state) message\n");
#endif
/* ...Then it is in the boot state as per RFC 2131 4.3.2 (requests
an IP address but is skipping the DISCO message). */
/* This client may NOT specify a Client IP address e.g. that field must
be left blank. */
if (dhcp_client_ptr -> nx_dhcp_clientip_address != NX_DHCP_NO_ADDRESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! REQUEST message may not specify Client IP address.\n");
#endif
/* Let the client know it has an invalid request. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
break;
}
/* Server SHOULD send a DHCPNAK message to the client if the 'requested IP address' is incorrect, or is on the wrong network.
RFC 2131, Section 4.3.2, Page31. */
if ((dhcp_client_ptr -> nx_dhcp_assigned_ip_address != 0) &&
(dhcp_client_ptr -> nx_dhcp_assigned_ip_address != dhcp_client_ptr -> nx_dhcp_requested_ip_address))
{
ULONG ip_address_assigned = dhcp_client_ptr -> nx_dhcp_assigned_ip_address;
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! REQUEST message contains incorrect requested IP address. \n");
#endif
/* No; invalid request. Return the IP address tentatively assigned to
the client back to the available IP address pool. */
_nx_dhcp_update_assignable_ip_address(dhcp_ptr, dhcp_client_ptr, ip_address_assigned,
NX_DHCP_ADDRESS_STATUS_MARK_AVAILABLE);
/* Clear requested IP lease time because the client is not granted an IP address lease. */
dhcp_client_ptr -> nx_dhcp_requested_lease_time = 0;
/* Let the client know it made an invalid request. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
break;
}
/* Set the client state to the BOOT state */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_BOOT;
/* Indicate the server will grant the IP lease. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPACK;
/* Advance the client state to requesting (waiting for an ACK). */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_REQUESTING;
break;
}
/* Else if the client is specifying a server id, and a requested IP address... */
else if (dhcp_client_ptr -> nx_dhcp_server_id &&
(dhcp_client_ptr -> nx_dhcp_requested_ip_address != NX_DHCP_NO_ADDRESS))
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Received REQUEST (SELECT state) message\n");
#endif
/* ...Then the client is in the SELECT state as per RFC 2131 4.3.2 (sent a
DISCOVERY message and is responding to a server OFFER message). */
/* Client may NOT specify a Client IP address. */
if (dhcp_client_ptr -> nx_dhcp_clientip_address != NX_DHCP_NO_ADDRESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! REQUEST message may not specify Client IP address.\n");
#endif
/* Let the client know it has an invalid request. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
break;
}
/* Check if the Client has chosen this server's offer (e.g.server ID option in the
REQUEST message). */
if (dhcp_client_ptr -> nx_dhcp_server_id != server_ip_address)
{
ULONG ip_address_assigned;
/* We are not. */
/* Is the Client accepting the same IP address this server offered? */
if (dhcp_client_ptr -> nx_dhcp_assigned_ip_address == dhcp_client_ptr -> nx_dhcp_requested_ip_address)
{
/* Yes, so do not change the client record IP address and IP address
status in the server interface table as assigned. */
ip_address_assigned = dhcp_client_ptr -> nx_dhcp_assigned_ip_address;
}
else
{
/* No, it is accepting a different IP address from another server. */
ip_address_assigned = dhcp_client_ptr -> nx_dhcp_requested_ip_address;
/* So update the client record's assigned address to the one it is accepting
from another server. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = dhcp_client_ptr -> nx_dhcp_requested_ip_address;
}
/* Update the server interface table with the information. */
_nx_dhcp_update_assignable_ip_address(dhcp_ptr, dhcp_client_ptr, ip_address_assigned,
NX_DHCP_ADDRESS_STATUS_ASSIGNED_OTHER);
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: SILENCE! REQUEST message indicates Client accepting another server IP.\n");
#endif
/* If this client wants an IP address from this server it must start
over at the init state. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
/* No need to respond. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPSILENT;
return(NX_SUCCESS);
}
/* Make sure the Client did in fact receive an OFFER from this server (e.g. has
been tentatively assigned an IP address from us). */
if (dhcp_client_ptr -> nx_dhcp_assigned_ip_address == NX_DHCP_NO_ADDRESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! REQUEST message does not indicate valid assigned IP address.\n");
#endif
/* It did not. This is an invalid request. Let the client know. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
/* Clear requested IP lease times because the client was not assigned an IP address. */
dhcp_client_ptr -> nx_dhcp_requested_lease_time = 0;
/* Nothing more to do! */
break;
}
/* Does the Client's requested IP address match the "Your IP" field in the server OFFER
previously sent to this client (e.g. Client's assigned IP address)? */
else if (dhcp_client_ptr -> nx_dhcp_assigned_ip_address != dhcp_client_ptr -> nx_dhcp_requested_ip_address)
{
ULONG ip_address_assigned = dhcp_client_ptr -> nx_dhcp_assigned_ip_address;
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! REQUEST message contains requested IP address. \n");
#endif
/* No; invalid request. Return the IP address tentatively assigned to
the client back to the available IP address pool. */
_nx_dhcp_update_assignable_ip_address(dhcp_ptr, dhcp_client_ptr, ip_address_assigned,
NX_DHCP_ADDRESS_STATUS_MARK_AVAILABLE);
/* Clear requested IP lease time because the client is not granted an IP address lease. */
dhcp_client_ptr -> nx_dhcp_requested_lease_time = 0;
/* Let the client know it made an invalid request. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
break;
}
/* The Client accepts this Server's offer. We will grant the client the IP lease now. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPACK;
/* UPdate the client state to REQUESTING (waiting on an ACK). */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_REQUESTING;
}
/* Is this a RENEW or REBIND request? If so the server ID and requested IP options must
NOT be filled in as per RFC 2131 4.3.2. */
else if ((dhcp_client_ptr -> nx_dhcp_server_id == NX_DHCP_NO_ADDRESS) &&
(dhcp_client_ptr -> nx_dhcp_requested_ip_address == NX_DHCP_NO_ADDRESS))
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Received REQUEST (RENEW/REBIND state) message\n");
#endif
/* Yes, this is a renew/rebind request! */
/* Extending an IP lease is a administration decision vs RFC mandate. The
NetX DHCP Server automatically extends IP leases. */
/* Go ahead and ACK the renew request pending any DHCP violations. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPACK;
/* The "Client IP" address field MUST be filled in. */
if (dhcp_client_ptr -> nx_dhcp_clientip_address == NX_DHCP_NO_ADDRESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! REQUEST message no 'Client IP' address specified. \n");
#endif
/* This is an invalid renew/rebind request. Reject the request but
do not change or advance the Client state (SHOULD be bound). */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
break;
}
/* The "Client IP" address subnet should match the Client packet source IP address subnet. */
if ((dhcp_interface_table_ptr -> nx_dhcp_subnet_mask & dhcp_client_ptr -> nx_dhcp_clientip_address) !=
(dhcp_interface_table_ptr -> nx_dhcp_subnet_mask & dhcp_interface_table_ptr -> nx_dhcp_server_ip_address))
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! REQUEST message; the 'Client IP' address subnet does not match the DHCP server subnet. \n");
#endif
/* This is an invalid renew/rebind request. Reject the request but
do not change or advance the Client state (SHOULD be bound). */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
break;
}
/* Is there an assigned IP address in the Client record? */
if (dhcp_client_ptr -> nx_dhcp_assigned_ip_address == NX_DHCP_NO_ADDRESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! REQUEST message; server has no record of assigned IP address.\n");
#endif
/* No; possibly assigned by another DHCP server or outside of DHCP protocol.
Reject the renewal request. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
/* This client needs to start the configuration process with this
server from the INIT start if it wants an IP address from it. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
break;
}
/* Determine if this is a renew or a rebind request. */
if (dhcp_client_ptr -> nx_dhcp_destination_ip_address != NX_DHCP_BC_ADDRESS)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Received REQUEST (renewing) message\n");
#endif
/* Renewing: DHCP clients' renew requests must be unicast. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_RENEWING;
}
else
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Received REQUEST (rebinding) message\n");
#endif
/* Rebinding DHCP clients' rebind request must be broadcast. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_REBINDING;
}
}
else
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! Received unknown REQUEST message type.\n");
#endif
/* Do not respond to unknown requests. This is kind of a dealer's choice
because it is not specified in the RFC 2132 how to handle undefined
request IDs or message types. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPSILENT;
return(NX_SUCCESS);
}
break;
}
case NX_DHCP_TYPE_DHCPRELEASE:
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Received RELEASE response type.\n");
#endif
/* The Client is releasing it's IP address back to the server. A client is not
required to do this; usually it indicates the client may have moved on the
network.
Clear the client record and mark the IP address as available. If the request proves
invalid, this server will ignore it and let the Client's assigned IP lease expire. */
/* Regardless what happens the server does not send a reply to a RELEASE message. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPSILENT;
/* Was an address previously assigned? */
if (dhcp_client_ptr -> nx_dhcp_assigned_ip_address == NX_DHCP_NO_ADDRESS)
{
/* No; nothing to do here. */
return(NX_SUCCESS);
}
/* The client IP field must be filled in for RELEASE requests. Was it? */
if (dhcp_client_ptr -> nx_dhcp_clientip_address == NX_DHCP_NO_ADDRESS)
{
/* No; This is an invalid release request. */
return(NX_SUCCESS);
}
/* Does client IP field match what this Server assigned the Client? */
if (dhcp_client_ptr -> nx_dhcp_clientip_address != dhcp_client_ptr -> nx_dhcp_assigned_ip_address)
{
/* No; Not sure how this happened but let the existing assigned
IP address lease expire. Ignore this message. */
return(NX_SUCCESS);
}
/* A RELEASE request must be unicasted to the server. Was this a broadcast
message? */
if (dhcp_client_ptr -> nx_dhcp_destination_ip_address == NX_DHCP_BC_ADDRESS)
{
/* Yes, so not a valid RELEASE request. */
return(NX_SUCCESS);
}
else
{
UINT index = dhcp_client_ptr -> nx_dhcp_client_iface_index;
ULONG server_ip = (&dhcp_ptr -> nx_dhcp_interface_table[index]) ->nx_dhcp_server_ip_address;
/* Check that we are the intended server. */
if (dhcp_client_ptr -> nx_dhcp_destination_ip_address != server_ip)
{
/* We are not. Not a valid Release request. */
return(NX_SUCCESS);
}
}
/* Legitimate release request: return the IP address to the available pool,
and clear the client session. */
/* Release the client's assigned IP address (in the "CI-ADDR" field). */
_nx_dhcp_update_assignable_ip_address(dhcp_ptr, dhcp_client_ptr,
dhcp_client_ptr -> nx_dhcp_assigned_ip_address,
NX_DHCP_ADDRESS_STATUS_MARK_AVAILABLE);
/* Clear the Client's assigned IP address. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = NX_DHCP_NO_ADDRESS;
/* If this client wants to configure its network parameters with us it
must start at the INIT state. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
break;
}
case NX_DHCP_TYPE_DHCPDECLINE:
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Received DECLINE response type.\n");
#endif
/* The Client is informing us an IP address we assigned to it is apparently already
in use by another host. It needs to restart in the INIT/BOOT state for this
server to assign it another IP address. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
/* Regardless what happens do not send a reply. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPSILENT;
/* If no address previously assigned, silently ignore this message. */
if (dhcp_client_ptr -> nx_dhcp_assigned_ip_address == NX_DHCP_NO_ADDRESS)
{
/* This client never had an address assigned by this server. Do nothing. */
return(NX_SUCCESS);
}
/* A DECLINE message must specify the requested address. */
if (dhcp_client_ptr -> nx_dhcp_requested_ip_address == NX_DHCP_NO_ADDRESS)
{
/* This is an invalid decline request. Reject it silently. */
return(NX_SUCCESS);
}
/* A Decline request is supposed to be unicasted to the server. If it is not,
ignore it. */
if (dhcp_client_ptr -> nx_dhcp_destination_ip_address != NX_DHCP_BC_ADDRESS)
{
return(NX_SUCCESS);
}
else
{
UINT index = dhcp_client_ptr -> nx_dhcp_client_iface_index;
ULONG server_ip = (&dhcp_ptr -> nx_dhcp_interface_table[index]) ->nx_dhcp_server_ip_address;
/* Check that we are the intended server. */
if (dhcp_client_ptr -> nx_dhcp_server_id != server_ip)
{
/* We are not. Ignore the request. */
return(NX_SUCCESS);
}
}
/* Release the client's assigned IP address. */
_nx_dhcp_update_assignable_ip_address(dhcp_ptr, dhcp_client_ptr,
dhcp_client_ptr -> nx_dhcp_assigned_ip_address,
NX_DHCP_ADDRESS_STATUS_ASSIGNED_OTHER);
/* Clear the Client's assigned IP address. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = NX_DHCP_NO_ADDRESS;
break;
}
case NX_DHCP_TYPE_DHCPINFORM:
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: Received INFORM response type.\n");
#endif
/* A DHCP client sends an INFORM message when it has been assigned an IP address
with another server or outside DHCP but still needs local configuration data.
The DHCP server must update its database with their address (specified in the "CI-ADDR" field)
as no longer available. When the server forms a reply ACK, it must leave the
'Your IP' and lease time fields blank as per RFC 2131 3.4. */
/* A DHCP server must respond (ACK) to INFORM messages unless the message
is invalid. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPACK;
/* The INFORM message must have the Client IP address filled in to be valid. */
if (dhcp_client_ptr -> nx_dhcp_clientip_address == NX_DHCP_NO_ADDRESS)
{
/* Invalid message. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
}
/* It cannot have a requested IP option in the options. */
if (dhcp_client_ptr -> nx_dhcp_requested_ip_address != NX_DHCP_NO_ADDRESS)
{
/* Invalid message. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
}
/* Make sure the server interface table is up to date for this IP address. */
_nx_dhcp_update_assignable_ip_address(dhcp_ptr, dhcp_client_ptr, dhcp_client_ptr -> nx_dhcp_clientip_address,
NX_DHCP_ADDRESS_STATUS_ASSIGNED_EXT);
/* And update the client record assigned IP address in case it is not set yet. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = dhcp_client_ptr -> nx_dhcp_clientip_address;
/* Has client IP address leased (bound) from this server? */
if (dhcp_client_ptr -> nx_dhcp_client_state != NX_DHCP_STATE_BOUND)
{
/* No, so if this client needs to configure an IP address assigned
with this server it must start at the INIT state. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
}
break;
}
default:
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! Received unknown response type.\n");
#endif
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPSILENT;
return(NX_SUCCESS);
/* No further processing to do. Leave the client state unchanged, whatever it was/is. */
}
} /* switch case on client message*/
}
/* Were there were any conditions requiring a NACK response? */
if (dhcp_client_ptr -> nx_dhcp_response_type_to_client == NX_DHCP_TYPE_DHCPNACK)
{
/* Yes, the caller knows to send the NACK message. No processing to do. */
return NX_SUCCESS;
}
/* If this is not a DISCOVERY or REQUEST message, we are done! */
if ((dhcp_client_ptr -> nx_dhcp_message_type != NX_DHCP_TYPE_DHCPDISCOVER) &&
(dhcp_client_ptr -> nx_dhcp_message_type != NX_DHCP_TYPE_DHCPREQUEST))
{
return(NX_SUCCESS);
}
/* If the boot/init state client did not request an address, we are done. */
if (dhcp_client_ptr -> nx_dhcp_requested_ip_address == NX_DHCP_NO_ADDRESS)
{
return(NX_SUCCESS);
}
/* Check if the Client has requested a valid IP address. This is only permitted
with a DISCO or REQUEST messages while the Client is in either the BOOT/INIT
or SELECT states.
As per RFC 2131 4.3.1, the DHCP server should assign an IP address in order of preference as follows:
1) the client currently is bound to
2) client ip address in client records (e.g. previously assigned to)
3) the currently requested IP
4) the server will assign one. */
/* Find the currently binding source address in the server's database. */
if (dhcp_client_ptr -> nx_dhcp_source_ip_address != NX_DHCP_NO_ADDRESS)
{
/* Assign this as the Client's IP address. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = dhcp_client_ptr -> nx_dhcp_source_ip_address;
}
/* Next best option: Has the client previously been assigned an IP address? */
else if (dhcp_client_ptr -> nx_dhcp_assigned_ip_address)
{
/* Yes, let's leave it assigned to it. */
return(NX_SUCCESS);
}
else
{
/* Last option: Set the client's requested IP address as the one to assign. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = dhcp_client_ptr -> nx_dhcp_requested_ip_address;
}
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_assign_ip_address PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This service finds an available IP address for the client if none is */
/* provided, and updates the client's record with the new IP address. It */
/* updates the interface table entry with current client data (lease */
/* time, owner). */
/* */
/* If there already is an assigned IP address on the client record, this */
/* function will verify the IP address has not been assigned to another */
/* client before setting the current client as owner. If it has, this */
/* function will look for another available IP address for the client. */
/* */
/* Once an address is assigned, this function updates the fields in the */
/* interface pointer entry, and the "Your IP address" in its message back*/
/* to the client. */
/* */
/* INPUT */
/* */
/* dhcp_cptr Pointer to DHCP Server */
/* dhcp_client_ptr Pointer to client record */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful completion */
/* NX_DHCP_NO_AVAILABLE_IP_ADDRESSES No available address to assign*/
/* */
/* CALLS */
/* */
/* _nx_dhcp_find_interface_table_ip_address */
/* Look up IP address in */
/* server interface table */
/* _nx_dhcp_find_ip_address_owner Look up owner of an IP address*/
/* in server interfce table */
/* _nx_dhcp_record_ip_address_owner Set the client as IP address */
/* owner in interface table */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_validate_client_message Check Client DHCP messages for*/
/* proper data and determine */
/* server response */
/* */
/**************************************************************************/
static UINT _nx_dhcp_server_assign_ip_address(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT *dhcp_client_ptr)
{
NX_DHCP_INTERFACE_TABLE *dhcp_interface_table_ptr;
NX_DHCP_INTERFACE_IP_ADDRESS *interface_address_ptr;
UINT assigned_to_client;
UINT assigned_ip;
UINT lease_time;
/* Set a flag on the outcome of finding an available address. */
assigned_ip = NX_FALSE;
/* Compute the client lease time based on client's requested
lease time and server default lease time. */
lease_time = dhcp_client_ptr -> nx_dhcp_requested_lease_time;
#if (NX_DHCP_DEFAULT_LEASE_TIME >= 0xFFFFFFFF)
if (lease_time == 0)
#else
if ((lease_time == 0) || (lease_time > NX_DHCP_DEFAULT_LEASE_TIME))
#endif
{
lease_time = NX_DHCP_DEFAULT_LEASE_TIME;
}
interface_address_ptr = (NX_DHCP_INTERFACE_IP_ADDRESS *)NX_NULL;
/* Does the Client have a candidate already assigned? */
if (dhcp_client_ptr -> nx_dhcp_assigned_ip_address)
{
/* The client does have one assigned, but we need to verify it is available
or already assigned to the client. */
/* Look for this IP address in the specified interface IP address list (table). */
_nx_dhcp_find_interface_table_ip_address(dhcp_ptr, dhcp_client_ptr -> nx_dhcp_client_iface_index,
dhcp_client_ptr -> nx_dhcp_assigned_ip_address, &interface_address_ptr);
/* Was the ip address found? */
if (interface_address_ptr)
{
/* Yes; Is it assigned to anyone yet? */
if (interface_address_ptr -> assigned == NX_FALSE)
{
/* No, We will assign this one further below. */
}
else
{
/* Determine who if is assigned to. */
_nx_dhcp_find_ip_address_owner(interface_address_ptr, dhcp_client_ptr, &assigned_to_client);
/* Assigned to this client? */
if (assigned_to_client)
{
/* Yes, so we're done here, except for checking a few fields. */
/* Renew the lease time. */
interface_address_ptr -> lease_time = lease_time;
/* Set the Your IP address field for the server response message. */
dhcp_client_ptr -> nx_dhcp_your_ip_address = interface_address_ptr -> nx_assignable_ip_address;
return(NX_SUCCESS);
}
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! Cannot assign requested IP address (already assigned)\n");
#endif
/* No, assigned to someone else. We cannot provide this so return a NACK. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
/* Clear the client's assigned IP address since it is assigned to someone else. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = NX_DHCP_NO_ADDRESS;
return(NX_SUCCESS);
}
}
else
{
/* If this is a NOT discovery message, the server can not very well
assign a different IP address becaause an ACK reply cannot contain
different information. */
if (dhcp_client_ptr -> nx_dhcp_message_type != NX_DHCP_TYPE_DHCPDISCOVER)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! Cannot assign requested IP address (not owned by server)\n");
#endif
/* Apparently the client has asked for an IP address either not
in the server database or on another network. We cannot provide this
so return a NACK. */
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
/* Clear the client record's 'assigned' IP address since none was assigned. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = NX_DHCP_NO_ADDRESS;
/* Since the client did not get assigned an IP address
make sure the requested lease time is reset to zero. */
dhcp_client_ptr -> nx_dhcp_requested_lease_time = 0;
return(NX_SUCCESS);
}
/* For a Discovery request, the server will go ahead and find an
available IP address to offer the client. */
}
}
/* If the client doesn't have an address assigned yet, find one in the server table not
yet assigned. */
if (interface_address_ptr == 0x0)
{
UINT i;
/* Set a local varible to the IP address list for this client. */
dhcp_interface_table_ptr = &(dhcp_ptr -> nx_dhcp_interface_table[dhcp_client_ptr -> nx_dhcp_client_iface_index]);
/* Yes, search for an available ip address in the IP list for this interface */
for(i = 0; i < dhcp_interface_table_ptr -> nx_dhcp_address_list_size; i++)
{
/* Set a local pointer variable to the next address. */
interface_address_ptr = &dhcp_interface_table_ptr -> nx_dhcp_ip_address_list[i];
/* Is this address assigned already? */
if (interface_address_ptr -> assigned == NX_FALSE)
{
/* No, but it is now! */
/* Indicate the search was successful. */
assigned_ip = NX_TRUE;
break;
}
}
/* Check if we were able to find an available IP address. */
if (assigned_ip == NX_FALSE)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! Cannot assign requested IP address (not found in server IP list)\n");
#endif
/* The server is not obliged to respond, but it should let the client
know if it cannot provide what it requests. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
/* Since the client did not get assigned an IP address
make sure the requested lease time is reset to zero. */
dhcp_client_ptr -> nx_dhcp_requested_lease_time = 0;
/* Make sure the client record's 'assigned' IP address ir removed since it was not assigned. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = NX_DHCP_NO_ADDRESS;
/* No, return the error status. */
return(NX_DHCP_NO_AVAILABLE_IP_ADDRESSES);
}
}
/* Was the assigned ip address available? */
if (interface_address_ptr)
{
/* Set the client as the IP address owner in the server interface table. */
_nx_dhcp_record_ip_address_owner(interface_address_ptr, dhcp_client_ptr, lease_time);
/* Set the client's assigned IP address. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = interface_address_ptr -> nx_assignable_ip_address;
/* Set the Your IP address field for the server response message. */
dhcp_client_ptr -> nx_dhcp_your_ip_address = interface_address_ptr -> nx_assignable_ip_address;
/* We are done! */
return(NX_SUCCESS);
}
/* If we're here we were not able to assign an IP address. */
/* The server is not obliged to respond, but it should let the client
know if it cannot provide what it requests. */
dhcp_client_ptr -> nx_dhcp_client_state = NX_DHCP_STATE_INIT;
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: NACK! Server cannot assign requested IP address\n");
#endif
dhcp_client_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPNACK;
/* Make sure the client record's 'assigned' IP address ir removed since it was not assigned. */
dhcp_client_ptr -> nx_dhcp_assigned_ip_address = NX_DHCP_NO_ADDRESS;
/* Since the client did not get assigned an IP address
make sure the requested lease time is reset to zero. */
dhcp_client_ptr -> nx_dhcp_requested_lease_time = 0;
return(NX_DHCP_NO_AVAILABLE_IP_ADDRESSES);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_extract_information PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function extracts information from a DHCP Client packet and */
/* updates the server's client database. If no client record exists for*/
/* the client this function will create one for it. It does some */
/* message validation but primary extracts DHCP header and option */
/* fields to the client record. */
/* */
/* If an internal error is encountered parsing the packet buffer, this */
/* function returns the error status. If it finds the client DHCP */
/* message invalid, it returns successful completion but sets the DHCP */
/* server to respond with either NACK or silence. */
/* */
/* When this function is finished, the caller releases the packet. */
/* */
/* INPUT */
/* */
/* dhcp_cptr Pointer to DHCP Server */
/* dhcp_client_ptr Pointer to client record */
/* packet_ptr Pointer to DHCP client packet */
/* iface_index Interface index packet was */
/* received on */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful completion */
/* status Actual completion status */
/* */
/* CALLS */
/* */
/* _nx_dhcp_find_client_record_by_chaddr Find Client record using the */
/* parsed mac address */
/* _nx_dhcp_server_get_data Parse DHCP data from buffer */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_listen_for_messages Process DHCP Client messages */
/* */
/**************************************************************************/
static UINT _nx_dhcp_server_extract_information(NX_DHCP_SERVER *dhcp_ptr, NX_DHCP_CLIENT **dhcp_client_ptr,
NX_PACKET *packet_ptr, UINT iface_index)
{
UINT status;
ULONG value;
ULONG size = 0;
ULONG xid;
UCHAR *work_ptr;
ULONG client_mac_msw;
ULONG client_mac_lsw;
NX_IPV4_HEADER *ip_header_ptr;
NX_DHCP_CLIENT *temp_client_rec_ptr;
/* Initialize the DHCP Client pointer to null pending successful data extraction. */
*dhcp_client_ptr = NX_NULL;
/* Set up UCHAR pointer to HW address field to extract client hardware address
which we will use as the client's unique identifier, since not all clients may use
Option 61/Client Identifier. */
work_ptr = packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_CLIENT_HW;
/* Pickup the MSW of the MAC address. */
client_mac_msw = (((ULONG) work_ptr[0]) << 8) | ((ULONG) work_ptr[1]);
client_mac_lsw = (((ULONG) work_ptr[2]) << 24) |
(((ULONG) work_ptr[3]) << 16) |
(((ULONG) work_ptr[4]) << 8) |
((ULONG) work_ptr[5]);
/* Check the client hardware (mac address) field is filled in. */
if ((client_mac_msw == 0) && (client_mac_lsw == 0))
{
return(NX_DHCP_INVALID_HW_ADDRESS);
}
/* Look up the client record by IP address and interface index in Client Records table. */
status = _nx_dhcp_find_client_record_by_chaddr(dhcp_ptr, iface_index, client_mac_msw,
client_mac_lsw, &temp_client_rec_ptr, NX_TRUE);
/* Check for error during search. */
if ((status != NX_SUCCESS) || !temp_client_rec_ptr)
{
return(status);
}
/* Note: No need to check for NULL pointer because we asked to add the
record if no match is found. */
/* Get the client's current binding IP address from the packet interface. */
ip_header_ptr = (NX_IPV4_HEADER *)(packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_UDP_HEADER) - sizeof(NX_IPV4_HEADER));
temp_client_rec_ptr -> nx_dhcp_source_ip_address = ip_header_ptr -> nx_ip_header_source_ip;
temp_client_rec_ptr -> nx_dhcp_destination_ip_address = ip_header_ptr -> nx_ip_header_destination_ip;
/* Get the message type. */
status = _nx_dhcp_server_get_data(packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_OP, 1, &value);
if (status)
{
return(status);
}
/* Check this is a 'request' DHCP Client message (not to be confused with the REQUEST
message type. */
if (value != NX_DHCP_OP_REQUEST)
{
#ifdef EL_PRINTF_ENABLE
EL_PRINTF("DHCPserv: SILENCE! Client DHCP packet not coded as a BOOT Request. Invalid request.\n");
#endif
/* Set the server to ignore the 'reply'. */
temp_client_rec_ptr -> nx_dhcp_response_type_to_client = NX_DHCP_TYPE_DHCPSILENT;
/* Return successful completion status. */
return(NX_SUCCESS);
}
/* Get the hardware type. */
status = _nx_dhcp_server_get_data(packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_HTYPE, 1, &value);
if (status)
{
return(status);
}
/* This is the Client interface type which is used in the default client identifier tag. */
temp_client_rec_ptr -> nx_dhcp_client_hwtype = value;
/* Get the hardware length. */
status = _nx_dhcp_server_get_data(packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_HLEN, 1, &value);
if (status)
{
return(status);
}
/* This is the size of the hardware address. Used in the default client identifier tag. */
temp_client_rec_ptr -> nx_dhcp_client_hwlen = value;
/* Extract the message ID. */
status = _nx_dhcp_server_get_data(packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_XID, 4, &xid);
if (status)
{
return(status);
}
/* Save the client xid to use in the server's reply back. */
temp_client_rec_ptr -> nx_dhcp_xid = xid;
/* Check if response to client should unicast or (default) broadcast. */
status = _nx_dhcp_server_get_data(packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_FLAGS, 1, &value);
if (status)
{
return(status);
}
/* Get the Client's preference for unicast vs broadcast. */
temp_client_rec_ptr -> nx_dhcp_broadcast_flag_set = (value & NX_DHCP_FLAGS_BROADCAST) ? 0x80 : 0x0;
/* Extract the Requested Client IP address, if there is one. */
status = _nx_dhcp_server_get_data(packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_CLIENT_IP, 4, &value);
if (status)
{
return(status);
}
/* This is the Client IP address reported by the Client. */
temp_client_rec_ptr -> nx_dhcp_clientip_address = value;
/* Update the IP address assigned to the Client "Your/client IP address". */
status = _nx_dhcp_server_get_data(packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_YOUR_IP, 4, &value);
if (status)
{
return(status);
}
/* This is the "Your IP address" which only the server should fill in. Should be zero in
client messages. */
temp_client_rec_ptr -> nx_dhcp_your_ip_address = value;
/* Extract the "Next Server IP address". */
status = _nx_dhcp_server_get_data(packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_SERVER_IP, 4, &value);
if (status)
{
return(status);
}
temp_client_rec_ptr -> nx_dhcp_clientrec_server_ip = value;
/* Store relay IP address for DHCP server (on another subnet) if there is one. */
status = _nx_dhcp_server_get_data(packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_RELAY_IP, 4, &value);
if (status)
{
return(status);
}
temp_client_rec_ptr -> nx_dhcp_relay_ip_address = value;
/* Look for the marker indicating option data (magic cookie). */
status = _nx_dhcp_server_get_data(packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_VENDOR, 4, &value);
if (status)
{
return(status);
}
/* Note we don't store the magic cookie value. */
/* Are there user options? */
if (value == NX_DHCP_MAGIC_COOKIE)
{
/* Yes there are user options in the Client message. */
/* Move up the buffer pointer past the magic cookie thing. */
work_ptr = packet_ptr -> nx_packet_prepend_ptr + NX_DHCP_OFFSET_VENDOR + 4;
/* Extract all the client option data. */
while (*work_ptr != 0xFF)
{
/* Guard against a missing "END" marker by checking if we are at the end of the DHCP packet data. */
if (work_ptr + 2 > packet_ptr -> nx_packet_append_ptr)
{
/* Yes, Client must have sent a DHCP message with improperly terminated option
data (or Client is using a super large DHCP packet). */
return(NX_DHCP_IMPROPERLY_TERMINATED_OPTION);
}
/* Get the next option. */
_nx_dhcp_server_get_data(work_ptr, 1, &value);
/* Move up the buffer pointer to the option length. */
work_ptr++;
/* Get the option length. */
_nx_dhcp_server_get_data(work_ptr, 1, &size);
/* Move up the buffer pointer to the next option. */
work_ptr++;
/* Validate the size. */
if (work_ptr + size > packet_ptr -> nx_packet_append_ptr)
{
return(NX_DHCP_IMPROPERLY_TERMINATED_OPTION);
}
/* Is this the client ID option? */
if (value != NX_DHCP_SERVER_OPTION_CLIENT_ID)
{
/* Check if there is enough space to store client requested options. */
if (temp_client_rec_ptr -> nx_dhcp_client_option_count < NX_DHCP_CLIENT_OPTIONS_MAX)
{
/* Process as any other option. */
_nx_dhcp_process_option_data(temp_client_rec_ptr, (CHAR *)work_ptr, (UCHAR)value, NX_TRUE, (UINT)size);
}
}
/* Move up the buffer pointer past the current option data size to the next option. */
work_ptr += size;
}
}
/* Return the location of the client record. */
*dhcp_client_ptr = temp_client_rec_ptr;
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_get_option_data PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This routine extracts data from the specified location and adds the */
/* Client's requested option to the Client record. The caller can ask */
/* for the option data as well to be extracted and stored to client */
/* record. */
/* */
/* INPUT */
/* */
/* dhcp_client_ptr Pointer to DHCP Client */
/* buffer Pointer to data buffer */
/* option Option from Client DHCP packet */
/* size Size of data buffer */
/* get_option_data Indicate if server wants option*/
/* data stored in client record */
/* */
/* OUTPUT */
/* */
/* status Error status extracting data */
/* NX_SUCCESS Successful completion */
/* */
/* CALLS */
/* */
/* _nx_dhcp_server_get_data Extract data from specified */
/* buffer location */
/* memcpy Copy specified area of memory */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_server_extract_information Extract DHCP data from packet */
/* */
/**************************************************************************/
static UINT _nx_dhcp_process_option_data(NX_DHCP_CLIENT *dhcp_client_ptr, CHAR *buffer, UCHAR option, UINT get_option_data, UINT size)
{
UINT status;
ULONG option_value = 0;
/* Do we parse option data for this option? */
if (get_option_data)
{
/* Yes, store to local variable to be applied to the Client record below. */
status = _nx_dhcp_server_get_data((UCHAR *)buffer, size, &option_value);
if (status)
{
return(status);
}
}
switch (option)
{
case 1:
/* Subnet mask */
dhcp_client_ptr -> nx_dhcp_user_options[dhcp_client_ptr -> nx_dhcp_client_option_count] = option;
if (get_option_data)
{
dhcp_client_ptr -> nx_dhcp_subnet_mask = option_value;
}
dhcp_client_ptr -> nx_dhcp_client_option_count++;
break;
case 3:
/* Router or gateway */
dhcp_client_ptr -> nx_dhcp_user_options[dhcp_client_ptr -> nx_dhcp_client_option_count] = option;
if (get_option_data)
{
dhcp_client_ptr -> nx_dhcp_router_ip_address = option_value;
}
dhcp_client_ptr -> nx_dhcp_client_option_count++;
break;
case 6:
/* Domain name server */
dhcp_client_ptr -> nx_dhcp_user_options[dhcp_client_ptr -> nx_dhcp_client_option_count] = option;
if (get_option_data)
{
dhcp_client_ptr -> nx_dhcp_dns_ip_address = option_value;
}
dhcp_client_ptr -> nx_dhcp_client_option_count++;
break;
case 12:
/* Client host name */
dhcp_client_ptr -> nx_dhcp_user_options[dhcp_client_ptr -> nx_dhcp_client_option_count] = option;
if (get_option_data)
{
/* Only store as much of the host name as will fit in the host name buffer. */
size = (size <= NX_DHCP_CLIENT_HOSTNAME_MAX) ? size : NX_DHCP_CLIENT_HOSTNAME_MAX;
/* Clear the memory buffer before writing the host name to it. */
memset(&dhcp_client_ptr -> nx_dhcp_client_name[0], 0, NX_DHCP_CLIENT_HOSTNAME_MAX);
memcpy(&dhcp_client_ptr -> nx_dhcp_client_name[0], buffer, size); /* Use case of memcpy is verified. */
}
dhcp_client_ptr -> nx_dhcp_client_option_count++;
break;
case 50:
/* Client's requested IP address */
dhcp_client_ptr -> nx_dhcp_user_options[dhcp_client_ptr -> nx_dhcp_client_option_count] = option;
if (get_option_data)
{
dhcp_client_ptr -> nx_dhcp_requested_ip_address = option_value;
}
dhcp_client_ptr -> nx_dhcp_client_option_count++;
break;
case 51:
/* Client's requested IP address lease time */
dhcp_client_ptr -> nx_dhcp_user_options[dhcp_client_ptr -> nx_dhcp_client_option_count] = option;
if (get_option_data)
{
dhcp_client_ptr -> nx_dhcp_requested_lease_time = option_value;
}
dhcp_client_ptr -> nx_dhcp_client_option_count++;
break;
case 53:
/* Message type */
dhcp_client_ptr -> nx_dhcp_user_options[dhcp_client_ptr -> nx_dhcp_client_option_count] = option;
dhcp_client_ptr -> nx_dhcp_message_type = (UCHAR)option_value;
dhcp_client_ptr -> nx_dhcp_client_option_count++;
break;
case 54:
/* Server ID by which server identifies itself to client(usually server IP address).
The Client includes this option in a REQUEST message to identify which server
it wants to lease an IP address from. */
dhcp_client_ptr -> nx_dhcp_user_options[dhcp_client_ptr -> nx_dhcp_client_option_count] = option;
if (get_option_data)
{
dhcp_client_ptr -> nx_dhcp_server_id = option_value;
}
dhcp_client_ptr -> nx_dhcp_client_option_count++;
break;
case 55:
/* Check if there is enough space to store all the client requested options. */
if (dhcp_client_ptr -> nx_dhcp_client_option_count + size > NX_DHCP_CLIENT_OPTIONS_MAX)
{
size = NX_DHCP_CLIENT_OPTIONS_MAX - dhcp_client_ptr -> nx_dhcp_client_option_count;
}
/* Update the client record with that option. */
memcpy(&(dhcp_client_ptr -> nx_dhcp_user_options[dhcp_client_ptr -> nx_dhcp_client_option_count]), buffer, size); /* Use case of memcpy is verified. */
dhcp_client_ptr -> nx_dhcp_client_option_count += size;
break;
case 61:
/* Note Client ID is handled separately. */
dhcp_client_ptr -> nx_dhcp_user_options[dhcp_client_ptr -> nx_dhcp_client_option_count] = option;
dhcp_client_ptr -> nx_dhcp_client_option_count++;
break;
case 116: /* Auto configuration option */
case 60: /* Vendor class Identifier. */
/* Not supported at this time. */
break;
default:
break;
}
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_add_requested_option PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This routine receives options and which interface the DHCP client is*/
/* on and adds the server information if there is any for that option */
/* into the packet reply buffer. */
/* */
/* INPUT */
/* */
/* dhcp_message Pointer to message buffer */
/* iface_index Index to server interface table*/
/* buffer Pointer to buffer to write to */
/* option Option to add */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful completion status */
/* status Actual completion status */
/* */
/* CALLS */
/* */
/* _nx_dhcp_add_option Adds the actual option to the */
/* DHCP message buffer */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_respond_to_dhcp_message Prepare and send out DHCP */
/* response to client */
/* */
/**************************************************************************/
static UINT _nx_dhcp_add_requested_option(NX_DHCP_SERVER *dhcp_ptr, UINT iface_index, UCHAR *buffer, UINT option, UINT *index)
{
UINT status = NX_SUCCESS;
/* The NetX DHCP server provides a limited list of DHCP options for ACK responses. These do NOT
include the server's 'required' options. */
switch (option)
{
case 1: /* Subnet mask */
status = _nx_dhcp_add_option(buffer, option, NX_DHCP_SERVER_OPTION_SUBNET_MASK_SIZE,
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_subnet_mask, index);
break;
case 3: /* Router IP address */
status = _nx_dhcp_add_option(buffer, option, NX_DHCP_SERVER_OPTION_ROUTER_SIZE,
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_router_ip_address, index);
break;
case 6: /* DNS IP address */
status = _nx_dhcp_add_option(buffer, option, NX_DHCP_SERVER_OPTION_ADDRESS_SIZE,
dhcp_ptr -> nx_dhcp_interface_table[iface_index].nx_dhcp_dns_ip_address, index);
break;
/* The NetX DHCP server does not reply to other options at this time. */
default:
break;
}
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_add_option PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This routine adds a DHCP option data to the DHCP message in the */
/* supplied buffer. Adding the option includes adding the option code,*/
/* length of the data and option data value. */
/* */
/* INPUT */
/* */
/* dhcp_message Pointer to message buffer */
/* option Option to add */
/* value Value of Option to add */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* _nx_dhcp_server_store_data Store data value */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_process Process the DHCP state machine*/
/* */
/**************************************************************************/
static UINT _nx_dhcp_add_option(UCHAR *dhcp_message, UINT option, UINT size, ULONG value, UINT *index)
{
/* Store the option. */
*(dhcp_message + (*index)) = (UCHAR)option;
(*index) ++;
/* Store the option size. */
*(dhcp_message + (*index)) = (UCHAR)size;
(*index) ++;
/* Store the option value. */
_nx_dhcp_server_store_data(dhcp_message + (*index), size, value);
(*index) += size;
/* Return a successful completion. */
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_get_data PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This routine gets a data value from a buffer, assuming the data is */
/* stored in standard Network format (big endian). Up to 4 bytes of */
/* data are used, if there are more than 4 bytes, only the lower 4 */
/* bytes are returned. */
/* */
/* INPUT */
/* */
/* data Pointer to buffer data */
/* size Size of data value */
/* value Data value retrieved */
/* */
/* OUTPUT */
/* */
/* NX_SUCCESS Successful completion */
/* NX_DHCP_PARAMETER_ERROR Invalid parameter input */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_get_response Get response from server */
/* _nx_dhcp_server_extract_information Extract server information */
/* _nx_dhcp_update_address_list Update address list */
/* */
/**************************************************************************/
static UINT _nx_dhcp_server_get_data(UCHAR *data, UINT size, ULONG *value)
{
/* Check for invalid buffer parameters. */
if ((size == 0) || (data == 0x0) || (value == 0))
{
return(NX_DHCP_PARAMETER_ERROR);
}
/* Initialize value to zero. */
*value = 0;
/* Store each byte of data from buffer into result. */
while (size > 0)
{
/* Build return value. */
*value = (*value << 8) | *data++;
size--;
}
/* Return value. */
return(NX_SUCCESS);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_dhcp_server_store_data PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function stores a data value in a buffer in standard Network */
/* format (big endian) where the destination may be unaligned. Up to */
/* 4 bytes of data are stored, if the size is larger than 4 bytes, the */
/* remaining bytes are set to zero. */
/* */
/* INPUT */
/* */
/* data Pointer to buffer data */
/* size Size of data value */
/* value Value to store */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* _nx_dhcp_send_request Send DHCP request */
/* _nx_dhcp_add_option Add a DHCP option */
/* */
/**************************************************************************/
static VOID _nx_dhcp_server_store_data(UCHAR *data, UINT size, ULONG value)
{
/* Make sure that data is left justified. */
switch (size)
{
case 1:
value <<= 24;
break;
case 2:
value <<= 16;
break;
case 3:
value <<= 8;
break;
default:
break;
}
/* Store the value. */
while (size-- > 0)
{
*data = (UCHAR)((value >> 24) & 0xff);
data++;
value <<= 8;
}
}
#endif /* NX_DISABLE_IPV4 */
|