summaryrefslogblamecommitdiffstats
path: root/private/ntos/tdi/tcpip/ip/iproute.c
blob: 3a0bb109d212a8a3508e13586be63e26f2823367 (plain) (tree)
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






























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                        
/********************************************************************/
/**                     Microsoft LAN Manager                      **/
/**               Copyright(c) Microsoft Corp., 1990-1995          **/
/********************************************************************/
/* :ts=4 */

//***   iproute.c - IP routing routines.
//
//  This file contains all the routines related to IP routing, including
//  routing table lookup and management routines.

#include    "oscfg.h"
#include    "cxport.h"
#include    "ndis.h"
#include    "ip.h"
#include    "ipdef.h"
#include    "ipinit.h"
#include    "info.h"
#include	"tdistat.h"
#include	"iproute.h"
#include	"iprtdef.h"
#include	"ipxmit.h"
#include	"igmp.h"
#include	"tdiinfo.h"
#include	"ipfilter.h"

extern  NetTableEntry   *NetTableList;      // Pointer to the net table.
extern  NetTableEntry   *DHCPNTE;			// Pointer to NTE being DHCP'd.

extern	NetTableEntry	*LoopNTE;			// Pointer to loopback NTE.
extern	Interface		LoopInterface;		// Pointer to loopback interface.

extern  AddrTypeCache   ATCache[];
extern  int             ATCIndex;

extern IP_STATUS SendICMPErr(IPAddr, IPHeader UNALIGNED *, uchar, uchar, ulong);
extern uchar ParseRcvdOptions(IPOptInfo *, OptIndex *);
extern void ULMTUNotify(IPAddr Dest, IPAddr Src, uchar Prot, void *Ptr,
		uint NewMTU);

extern	Interface		*IFList;
extern	Interface		*FirstIF;

#define ROUTE_TABLE_SIZE    32              // Size of the route table.
DEFINE_LOCK_STRUCTURE(RouteTableLock)

#define	FWPACKET_GROW_AMOUNT	20

#define FW_BUF_SIZE         256             // Size of a forwarding buffer.

#define FW_BUF_GROW_AMOUNT	30720		 	// Enough for 20 Ethernet packets.

#define NO_SR               0

RouteTableEntry     *RouteTable[ROUTE_TABLE_SIZE];

DEFINE_LOCK_STRUCTURE(FWPacketFreeLock)
DEFINE_LOCK_STRUCTURE(FWBufFreeLock)

PNDIS_PACKET    FWPacketFree;               // Free list of forwarding packets.
PNDIS_BUFFER    FWBufFree;                  // Free list of forwarding buffers.

uint			MaxFWPackets;				// Maximum number of forward packets allowed.
uint			CurrentFWPackets;			// Number of forwarding packets currently
											// allocated.
uint			MaxFWBufferSize;			// Maximum number of forwarding buffers allowed.
uint			CurrentFWBufferSize;		// Number of forwarding buffers allocated.

uchar           ForwardPackets;     		// Flag indicating whether we should forward.
uchar			RouterConfigured;			// TRUE if we were initially
											// configured as a router.
uchar           ForwardBCast;               // Flag indicating if we should forward bcasts.
RouteSendQ      *BCastRSQ;

uint			DefGWConfigured;			// Number of default gateways configed.
uint			DefGWActive;				// Number of def. gateways active.

uint			DeadGWDetect;
uint			PMTUDiscovery;

ProtInfo        *RtPI = NULL;

IPMask  IPMaskTable[] = {
    CLASSA_MASK,
    CLASSA_MASK,
    CLASSA_MASK,
    CLASSA_MASK,
    CLASSA_MASK,
    CLASSA_MASK,
    CLASSA_MASK,
    CLASSA_MASK,
    CLASSB_MASK,
    CLASSB_MASK,
    CLASSB_MASK,
    CLASSB_MASK,
    CLASSC_MASK,
    CLASSC_MASK,
    CLASSD_MASK,
    CLASSE_MASK };

extern void     TransmitFWPacket(PNDIS_PACKET, uint);

uint	MTUTable[] = {
	
	65535 - sizeof(IPHeader),
	32000 - sizeof(IPHeader),
	17914 - sizeof(IPHeader),
	8166 - sizeof(IPHeader),
	4352 - sizeof(IPHeader),
	2002 - sizeof(IPHeader),
	1492 - sizeof(IPHeader),
	1006 - sizeof(IPHeader),
	508 - sizeof(IPHeader),
	296 - sizeof(IPHeader),
	MIN_VALID_MTU - sizeof(IPHeader)
};

CTETimer	IPRouteTimer;

// Pointer to callout routine for dial on demand.
IPMapRouteToInterfacePtr	DODCallout;

// Pointer to packet filter handler.
IPPacketFilterPtr		ForwardFilterPtr;

RouteInterface	DummyInterface;              // Dummy interface.

#ifdef NT
#ifdef ALLOC_PRAGMA
//
// Make init code disposable.
//
int InitRouting(IPConfigInfo    *ci);

#pragma alloc_text(INIT, InitRouting)

#endif // ALLOC_PRAGMA
#endif // NT


#define	InsertAfterRTE(P, R)	(R)->rte_next = (P)->rte_next;\
								(P)->rte_next = (R)

#define	InsertRTE(R)	{\
						RouteTableEntry			*__P__; \
						__P__ = FindInsertPoint((R)); \
						InsertAfterRTE(__P__, (R)); \
						}

#define RemoveRTE(P, R)	(P)->rte_next = (R)->rte_next;

//** DuumyXmit - Dummy interface transmit handler.
//
//	A dummy routine that should never be called.
//
//  Entry:  Context         - NULL.
//          Packet          - Pointer to packet to be transmitted.
//          Dest            - Destination addres of packet.
//          RCE             - Pointer to RCE (should be NULL).
//
//  Returns: NDIS_STATUS_PENDING
//
NDIS_STATUS
DummyXmit(void *Context, PNDIS_PACKET Packet, IPAddr Dest, RouteCacheEntry *RCE)
{
    DbgPrint("TCPIP: Dummy Xmit called - NOT GOOD\n");
   	CTEAssert(FALSE);
	return NDIS_STATUS_SUCCESS;
}

//* DummyXfer - Dummy interface transfer data routine.
//
//	A dummy routine that should never be called.
//
//  Entry:  Context         - NULL.
//          TDContext       - Original packet that was sent.
//          Dummy           - Unused
//          Offset          - Offset in frame from which to start copying.
//          BytesToCopy     - Number of bytes to copy.
//          DestPacket      - Packet describing buffer to copy into.
//          BytesCopied     - Place to return bytes copied.
//
//  Returns: NDIS_STATUS_SUCCESS
//
NDIS_STATUS
DummyXfer(void *Context, NDIS_HANDLE TDContext, uint Dummy, uint Offset, uint BytesToCopy,
    PNDIS_PACKET DestPacket, uint *BytesCopied)
{
    DbgPrint("TCPIP: DummyXfer called - NOT GOOD\n");

    CTEAssert(FALSE);

	return NDIS_STATUS_FAILURE;
}

//* DummyClose - Dummy close routine.
//
//	A dummy routine that should never be called.
//
//  Entry:  Context     - Unused.
//
//  Returns: Nothing.
//
void
DummyClose(void *Context)
{
    DbgPrint("TCPIP: Dummy Close called - NOT GOOD\n");

	CTEAssert(FALSE);
}

//* DummyInvalidate - .
//
//	A dummy routine that should never be called.
//
//  Entry:  Context     - Unused.
//          RCE         - Pointer to RCE to be invalidated.
//
//  Returns: Nothing.
//
void
DummyInvalidate(void *Context, RouteCacheEntry *RCE)
{
    DbgPrint("TCPIP: Dummy Invalidate called - NOT GOOD\n");

	CTEAssert(FALSE);

}

//* DummyQInfo - Dummy query information handler.
//
//	A dummy routine that should never be called.
//
//	Input:	IFContext		- Interface context (unused).
//			ID				- TDIObjectID for object.
//			Buffer			- Buffer to put data into.
//			Size			- Pointer to size of buffer. On return, filled with
//								bytes copied.
//			Context			- Pointer to context block.
//
//	Returns: Status of attempt to query information.
//
int
DummyQInfo(void *IFContext, TDIObjectID *ID, PNDIS_BUFFER Buffer, uint *Size,
	void *Context)
{
    DbgPrint("TCPIP: DummyQInfo called - NOT GOOD\n");
	
    CTEAssert(FALSE);

	return TDI_INVALID_REQUEST;
}

//* DummySetInfo - Dummy query information handler.
//
//	A dummy routine that should never be called.
//
//	Input:	IFContext		- Interface context (unused).
//			ID				- TDIObjectID for object.
//			Buffer			- Buffer to put data into.
//			Size			- Pointer to size of buffer. On return, filled with
//								bytes copied.
//
//	Returns: Status of attempt to query information.
//
int
DummySetInfo(void *IFContext, TDIObjectID *ID, void *Buffer, uint Size)
{
    DbgPrint("TCPIP: DummySetInfo called - NOT GOOD\n");
	
    CTEAssert(FALSE);

	return TDI_INVALID_REQUEST;
}

//* DummyAddAddr - Dummy add address routine.
//
//  Called at init time when we need to initialize ourselves.
//
uint
DummyAddAddr(void *Context, uint Type, IPAddr Address, IPMask Mask, void *Context2)
{
	CTEAssert(FALSE);

    return TRUE;
}

//* DummyDelAddr - Dummy del address routine.
//
//  Called at init time when we need to initialize ourselves.
//
uint
DummyDelAddr(void *Context, uint Type, IPAddr Address, IPMask Mask)
{
    DbgPrint("TCPIP: DummyAddAddr called - NOT GOOD\n");
	
    CTEAssert(FALSE);

    return TRUE;
}

//*	DummyGetEList - Dummy get entity list.
//
//	A dummy routine that should never be called.
//
//	Input:	Context			- Unused.
//			EntityList		- Pointer to entity list to be filled in.
//			Count			- Pointer to number of entries in the list.
//
//	Returns Status of attempt to get the info.
//
int
DummyGetEList(void *Context, TDIEntityID *EntityList, uint *Count)
{
    DbgPrint("TCPIP: DummyGetEList called - NOT GOOD\n");

    CTEAssert(FALSE);

	return FALSE;
}

#ifdef _PNP_POWER
//* DerefIF - Dereference an interface.
//
// Called when we need to dereference an interface. We decrement the
// refcount, and if it goes to zero we signal whoever is blocked on
// it.
//
// Input: IF						- Interfaec to be dereferenced.
//
// Returns: Nothing.
//
void
DerefIF(Interface *IF)
{
	uint Original;

	Original = CTEInterlockedAddUlong(
									&IF->if_refcount,
									(ULONG)-1,
									&RouteTableLock
									);
	if (Original != 1) {
		return;
	} else {
		// We just decremented the last reference. Wake whoever is
		// blocked on it.
		CTEAssert(IF->if_block != NULL);
		CTESignal(IF->if_block, NDIS_STATUS_SUCCESS);
	}
}

//* LockedDerefIF - Dereference an interface w/RouteTableLock held.
//
// Called when we need to dereference an interface. We decrement the
// refcount, and if it goes to zero we signal whoever is blocked on
// it. The difference here is that we assume the caller already holds
// the RouteTableLock.
//
// Input: IF						- Interfaec to be dereferenced.
//
// Returns: Nothing.
//
void
LockedDerefIF(Interface *IF)
{
	uint Original;

	IF->if_refcount--;

	if (IF->if_refcount != 0) {
		return;
	} else {
		// We just decremented the last reference. Wake whoever is
		// blocked on it.
		CTEAssert(IF->if_block != NULL);
		CTESignal(IF->if_block, NDIS_STATUS_SUCCESS);
	}
}
#endif

//*	GetHashMask - Get mask to use with address when hashing.
//
//	Called when we need to decide on the mask to use when hashing. If the
//	supplied mask is the host mask or the default mask, we'll use that. Else
//	if the supplied mask is at least as specific as the net mask, we'll use the
//	net mask. Otherwise we drop back to the default mask.
//
//	Input:	Destination			- Destination we'll be hashing on.
//			Mask				- Caller supplied mask.
//
//	Returns:	Mask to use.
//
IPMask
GetHashMask(IPAddr Destination, IPMask Mask)
{
	IPMask		NetMask;
	
	if (Mask == HOST_MASK || Mask == DEFAULT_MASK)
		return Mask;
		
	NetMask = IPNetMask(Destination);
	
	if ((NetMask & Mask) == NetMask)
		return NetMask;
		
	return DEFAULT_MASK;
	
}

//** AddrOnIF - Check to see if a given address is local to an IF
//
//	Called when we want to see if a given address is a valid local address
//	for an interface. We walk down the chain of NTEs in the interface, and
//	see if we get a match. We assume the caller holds the RouteTableLock
//	at this point.
//
//	Input:	IF			- Interface to check.
//			Addr		- Address to check.
//
//	Returns: TRUE if Addr is an address for IF, FALSE otherwise.
//
uint
AddrOnIF(Interface *IF, IPAddr Addr)
{
	NetTableEntry		*NTE;

	NTE = IF->if_nte;
	while (NTE != NULL) {
		if ((NTE->nte_flags & NTE_VALID) && IP_ADDR_EQUAL(NTE->nte_addr, Addr))
			return TRUE;
		else
			NTE = NTE->nte_ifnext;
	}
	
	return FALSE;
}			

//** BestNTEForIF - Find the 'best match' NTE on a given interface.
//
//  This is a utility function that takes an  address and tries to find the
//  'best match' NTE on a given interface. This is really only useful when we
//	have multiple IP addresses on a single interface.
//
//  Input:  Address     - Source address of packet.
//          IF      	- Pointer to IF to be searched.
//
//  Returns: The 'best match' NTE.
//
NetTableEntry *
BestNTEForIF(IPAddr Address, Interface *IF)
{
    NetTableEntry   *CurrentNTE, *FoundNTE;

	if (IF->if_nte != NULL) {
		// Walk the list of NTEs, looking for a valid one.
		CurrentNTE = IF->if_nte;
		FoundNTE = NULL;
		do {
			if (CurrentNTE->nte_flags & NTE_VALID) {
	            if (IP_ADDR_EQUAL(Address & CurrentNTE->nte_mask,
	            	CurrentNTE->nte_addr & CurrentNTE->nte_mask))
	                return CurrentNTE;
				else
					if (FoundNTE == NULL)
						FoundNTE = CurrentNTE;
				
			}
			
			CurrentNTE = CurrentNTE->nte_ifnext;
		} while (CurrentNTE != NULL);
		
		// If we found a match, or we didn't and the destination is not
		// a broadcast, return the result. We have special case code to
		// handle broadcasts, since the interface doesn't really matter there.
		if (FoundNTE != NULL || (!IP_ADDR_EQUAL(Address, IP_LOCAL_BCST) &&
			!IP_ADDR_EQUAL(Address, IP_ZERO_BCST)))
			return FoundNTE;		
	
	}
	
	// An 'anonymous' I/F, or the address we're reaching is a broadcast and the
	// first interface has no address. Find a valid (non-loopback) address.
	for (CurrentNTE = NetTableList; CurrentNTE != NULL;
		CurrentNTE = CurrentNTE->nte_next) {
		if (CurrentNTE != LoopNTE && (CurrentNTE->nte_flags & NTE_VALID))
			return CurrentNTE;

	}
	
	return NULL;
			
}			

//** IsBCastonNTE - Determine if the specified addr. is a bcast on a spec. NTE.
//
//  This routine is called when we need to know if an address is a broadcast
//  on a particular net. We check in the order we expect to be most common - a
//	subnet bcast, an all ones broadcast, and then an all subnets broadcast.  We
//	return the type of broadcast it is, or return DEST_LOCAL if it's not a
//	broadcast.
//
//  Entry:  Address     - Address in question.
//          NTE         - NetTableEntry to check Address against.
//
//  Returns: Type of broadcast.
//
uchar
IsBCastOnNTE(IPAddr Address, NetTableEntry *NTE)
{
    IPMask      	Mask;
	IPAddr			BCastAddr;

	BCastAddr = NTE->nte_if->if_bcast;

	if (NTE->nte_flags & NTE_VALID) {
	
		Mask = NTE->nte_mask;

        if(Mask != 0xFFFFFFFF)
        {
	        if (IP_ADDR_EQUAL(Address, (NTE->nte_addr & Mask) | (BCastAddr & ~Mask)))
	            return DEST_SN_BCAST;
        }
	
	    // See if it's an all subnet's broadcast.
		if (!CLASSD_ADDR(Address)) {
		    Mask = IPNetMask(Address);
		
		    if (IP_ADDR_EQUAL(Address,
		    	(NTE->nte_addr & Mask) | (BCastAddr & ~Mask)))
		        return DEST_BCAST;
		} else {
			// This is a class D address. If we're allowed to receive
			// mcast datagrams, check our list.
		
			if (IGMPLevel == 2) {
				IGMPAddr		*AddrPtr;
				CTELockHandle	Handle;
				
				CTEGetLock(&NTE->nte_lock, &Handle);
				AddrPtr = NTE->nte_igmplist;
				while (AddrPtr != NULL) {
					if (IP_ADDR_EQUAL(Address, AddrPtr->iga_addr))
						break;
					else
						AddrPtr = AddrPtr->iga_next;
				}
				
				CTEFreeLock(&NTE->nte_lock, Handle);
				if (AddrPtr != NULL)
					return DEST_MCAST;
			}
		}
	}

    // A global bcast is certainly a bcast on this net.
    if (IP_ADDR_EQUAL(Address, BCastAddr))
        return DEST_BCAST;

    return DEST_LOCAL;

}

//** InvalidSourceAddress - Check to see if a source address is invalid.
//
//  This function takes an input address and checks to see if it is valid
//	if used as the source address of an incoming packet. An address is invalid
//	if it's 0, -1, a Class D or Class E address, is a net or subnet broadcast,
//	or has a 0 subnet or host part.
//
//  Input: Address      - Address to be check.
//
//  Returns: FALSE if the address is not invalid, TRUE if it is invalid.
//
uint
InvalidSourceAddress(IPAddr Address)
{
    NetTableEntry   *NTE;       // Pointer to current NTE.
    IPMask          Mask;       // Mask for address.
    uchar           Result;     // Result of broadcast check.
    IPMask          SNMask;
	IPAddr			MaskedAddress;
	IPAddr			LocalAddress;


    if ( !CLASSD_ADDR(Address) &&
		!CLASSE_ADDR(Address)  &&
		!IP_ADDR_EQUAL(Address, IP_ZERO_BCST) &&
		!IP_ADDR_EQUAL(Address, IP_LOCAL_BCST)
	   ) {
        // It's not an obvious broadcast. See if it's an all subnets
		// broadcast, or has a zero host part.
		Mask = IPNetMask(Address);
		MaskedAddress = Address & Mask;

		if (!IP_ADDR_EQUAL(Address, MaskedAddress) &&
			!IP_ADDR_EQUAL(Address, (MaskedAddress | ~Mask))
		   ) {
		   // It's not an all subnet's broadcast, and it has a non-zero
		   // host/subnet part. Walk our local IP addresses, and see if it's
		   // a subnet broadcast.
			NTE = NetTableList;
			do {

				LocalAddress = NTE->nte_addr;

				if ((NTE->nte_flags & NTE_VALID) &&
					!IP_LOOPBACK(LocalAddress)) {

					Mask = NTE->nte_mask;
					MaskedAddress = LocalAddress & Mask;
	
					if (IP_ADDR_EQUAL(Address, MaskedAddress) ||
						IP_ADDR_EQUAL(Address,
									(MaskedAddress |
									(NTE->nte_if->if_bcast & ~Mask)))) {
						return TRUE;
					}
				}
	
	
				NTE = NTE->nte_next;
			} while (NTE != NULL);

			return FALSE;
		}
	}

	return TRUE;
}

//** FlushATCache - Flush an address from the ATCache
//
//  This function takes an input address, and removes it from the ATCache,
//  if it is present.
//
//  Input: Address      - Address to be check.
//
//  Returns: Destination type.
//
void
FlushATCache(IPAddr Address)
{
    uint i;


    for (i=0; i<ATC_SIZE; i++) {
        if (ATCache[i].atc_flags & (ATCache[i].atc_addr == Address)) {
            ATCache[i].atc_flags = 0;
        }
    }
}

//** GetAddrType - Return the type of a specified address.
//
//  This function takes an input address, and determines what type it is. An
//  address can be local, bcast, remote, or remote bcast.
//
//  Input: Address      - Address to be check.
//
//  Returns: Destination type.
//
uchar
GetAddrType(IPAddr Address)
{
    NetTableEntry   *NTE;       // Pointer to current NTE.
    IPMask          Mask;       // Mask for address.
    uchar           Result;     // Result of broadcast check.
    IPMask          SNMask;
    uint            saveATCIndex;
    uint             i;

    saveATCIndex = ATCIndex & ATC_MASK;
    i = saveATCIndex;

    do {
        if (ATCache[i].atc_flags && (ATCache[i].atc_addr == Address)) {
            Result = ATCache[i].atc_type;
            if (ATCache[i].atc_flags && (ATCache[i].atc_addr == Address)) {
                return(Result);
            }
        }
        i = (--i) & ATC_MASK;
    } while (i != saveATCIndex );



    if (!CLASSE_ADDR(Address)) {
        // See if it's one of our local addresses, or a broadcast
        // on a local address.
        NTE = NetTableList;
        do {

            if (IP_ADDR_EQUAL(NTE->nte_addr, Address) &&
				(NTE->nte_flags & NTE_VALID)) {
                Result = DEST_LOCAL;
                goto gat_exit;
            }

            if ((Result = IsBCastOnNTE(Address, NTE)) != DEST_LOCAL) {
                goto gat_exit;
            }

            // See if the destination has a valid host part.
			if (NTE->nte_flags & NTE_VALID) {
	            SNMask = NTE->nte_mask;
	            if (IP_ADDR_EQUAL(Address & SNMask, NTE->nte_addr & SNMask)) {
	            	// On this subnet. See if the host part is invalid.
	
	                if (IP_ADDR_EQUAL(Address & SNMask, Address)) {
	                    Result = DEST_INVALID;        // Invalid 0 host part.
                        goto gat_exit;
                    }
	            }
			}
            NTE = NTE->nte_next;
        } while (NTE != NULL);

        // It's not a local address, see if it's loopback.
        if (IP_LOOPBACK(Address)) {
            Result = DEST_LOCAL;
            goto gat_exit;
        }

		// If we're doing IGMP, see if it's a Class D address. If it it,
		// return that.
		if (CLASSD_ADDR(Address)) {
			if (IGMPLevel != 0) {
				Result = DEST_REM_MCAST;
                goto gat_exit;
            }
            else {
				Result = DEST_INVALID;
                goto gat_exit;
            }
		}
				
        Mask = IPNetMask(Address);

        // Now check remote broadcast. When we get here we know that the
		// address is not a global broadcast, a subnet broadcast for a subnet
		// of which we're a member, or an all-subnets broadcast for a net of
		// which we're a member. Since we're avoiding making assumptions about
		// all subnet of a net having the same mask, we can't really check for
		// a remote subnet broadcast. We'll use the net mask and see if it's
		// a remote all-subnet's broadcast.
        if (IP_ADDR_EQUAL(Address, (Address & Mask) | (IP_LOCAL_BCST & ~Mask))) {
            Result = DEST_REM_BCAST;
            goto gat_exit;
        }

        // Check for invalid 0 parts. All we can do from here is see if he's
		// sending to a remote net with all zero subnet and host parts. We
		// can't check to see if he's sending to a remote subnet with an all
		// zero host part.
        if (IP_ADDR_EQUAL(Address, Address & Mask) ||
        	IP_ADDR_EQUAL(Address, NULL_IP_ADDR)) {
            Result = DEST_INVALID;
            goto gat_exit;
        }

        // Must be remote.
        Result = DEST_REMOTE;
        goto gat_exit;
    }

    Result = DEST_INVALID;

gat_exit:

    ++ATCIndex;

    i = ATCIndex & ATC_MASK;

    ATCache[i].atc_addr = Address;
    ATCache[i].atc_type = Result;
    ATCache[i].atc_flags = 1;
    return(Result);

}

//** IPHash - IP hash function.
//
// This is the function to compute the hash index from a masked address.
//
//  Input:  Address - Masked address to be hashed.
//
//  Returns: Hashed value.
//
uint
IPHash(IPAddr Address)
{
    uchar   *i = (uchar *)&Address;
    return (i[0] + i[1] + i[2] + i[3]) & (ROUTE_TABLE_SIZE-1);
}

//** GetLocalNTE - Get the local NTE for an incoming packet.
//
//  Called during receive processing to find a matching NTE for a packet.
//  First we check against the NTE we received it on, then against any NTE.
//
//  Input:  Address     - The dest. address of the packet.
//          NTE         - Pointer to NTE packet was received on - filled in on
//							exit w/correct NTE.
//
//  Returns: DEST_LOCAL if the packet is destined for this host, DEST_REMOTE if it needs to
//          be routed, DEST_SN_BCAST or DEST_BCAST if it's some sort of a broadcast.
uchar
GetLocalNTE(IPAddr Address, NetTableEntry **NTE)
{
    NetTableEntry   *LocalNTE = *NTE;
    IPMask          Mask;
    uchar           Result;
    int             i;
    Interface       *LocalIF;
    NetTableEntry   *OriginalNTE;

    // Quick check to see if it is for the NTE it came in on (the common case).
    if (IP_ADDR_EQUAL(Address, LocalNTE->nte_addr) &&
		(LocalNTE->nte_flags & NTE_VALID))
        return DEST_LOCAL;                              // For us, just return.

    // Now check to see if it's a broadcast of some sort on the interface it
    // came in on.
    if ((Result = IsBCastOnNTE(Address, LocalNTE)) != DEST_LOCAL)
        return Result;

    // The common cases failed us. Loop through the NetTable and see if
    // it is either a valid local address or is a broadcast on one of the NTEs
    // on the incoming interface. We won't check the NTE we've already looked
    // at. We look at all NTEs, including the loopback NTE, because a loopback
    // frame could come through here. Also, frames from ourselves to ourselves
    // will come in on the loopback NTE.

    i = 0;
    LocalIF = LocalNTE->nte_if;
    OriginalNTE = LocalNTE;
    LocalNTE = NetTableList;
    do {
        if (LocalNTE != OriginalNTE) {
            if (IP_ADDR_EQUAL(Address, LocalNTE->nte_addr) &&
				(LocalNTE->nte_flags & NTE_VALID)) {
                *NTE = LocalNTE;
                return DEST_LOCAL;		// For us, just return.
            }

            // If this NTE is on the same interface as the NTE it arrived on,
            // see if it's a broadcast.
            if (LocalIF == LocalNTE->nte_if)
                if ((Result = IsBCastOnNTE(Address, LocalNTE)) != DEST_LOCAL) {
                    *NTE = LocalNTE;
                    return Result;
                }

        }

        LocalNTE = LocalNTE->nte_next;

    } while (LocalNTE != NULL);

    // It's not a local address, see if it's loopback.
    if (IP_LOOPBACK(Address)) {
		*NTE = LoopNTE;
        return DEST_LOCAL;
	}

	// If it's a class D address and we're receiveing multicasts, handle it
	// here.
	if (CLASSD_ADDR(Address)) {
		if (IGMPLevel != 0)
			return DEST_REM_MCAST;
		else
			return DEST_INVALID;
	}
	
    // It's not local. Check to see if maybe it's a net broadcast for a net
    // of which we're not a member. If so, return remote bcast. We can't check
	// for subnet broadcast of subnets for which we're not a member, since we're
	// not making assumptions about all subnets of a single net having the
	// same mask. If we're here it's not a subnet broadcast for a net of which
	// we're a member, so we don't know a subnet mask for it. We'll just use
	// the net mask.
    Mask = IPNetMask(Address);
    if (IP_ADDR_EQUAL(Address, (Address & Mask) |
		((*NTE)->nte_if->if_bcast & ~Mask)))
        return DEST_REM_BCAST;

    // If it's to the 0 address, or a Class E address, or has an all-zero
    // subnet and net part, it's invalid.


    if (IP_ADDR_EQUAL(Address, IP_ZERO_BCST) ||
    	IP_ADDR_EQUAL(Address, (Address & Mask)) ||
        CLASSE_ADDR(Address))
        return DEST_INVALID;

	// If we're DHCPing the interface on which this came in we'll accept this.
	// If it came in as a broadcast a check in IPRcv() will reject it. If it's
	// a unicast to us we'll pass it up.
	if (DHCPNTE != NULL && DHCPNTE == *NTE) {
		return DEST_LOCAL;
	}

    return DEST_REMOTE;
}


//** FindSpecificRTE - Look for a particular RTE.
//
//	Called when we're adding a route and want to find a particular RTE.
//	We take in the destination, mask, first hop, and src addr, and search
//	the appropriate routeing table chain. We assume the caller has the
//	RouteTableLock held. If we find the match, we'll return a pointer to the
//	RTE with the RTE lock held, as well as a pointer to the previous RTE in
//	the chain.
//
//	Input:	Dest			- Destination to search for.
//			Mask			- Mask for destination.
//			FirstHop		- FirstHop to Dest.
//			OutIF			- Pointer to outgoing interface structure.
//			PrevRTE			- Place to put PrevRTE, if found.
//
//	Returns: Pointer to matching RTE if found, or NULL if not.
//
RouteTableEntry *
FindSpecificRTE(IPAddr Dest, IPMask Mask, IPAddr FirstHop, Interface *OutIF,
	RouteTableEntry **PrevRTE)
{
	uint			Index;
	IPMask			HashMask;
	RouteTableEntry	*TempRTE, *CurrentRTE;

	HashMask = GetHashMask(Dest, Mask);
	
	Index = IPHash(Dest & HashMask);
	TempRTE = STRUCT_OF(RouteTableEntry, &RouteTable[Index], rte_next);
	CurrentRTE = TempRTE->rte_next;

    //
    // If this has been called because user mode was trying to set the route
    // to INVALID, then the OUTIF will be DummyInterface, but we want to match
    // any interface, since the IF will have already been plumbed by DODCallOut
    //

    if(OutIF == (Interface *)&DummyInterface)
    {
        //
        // Match everything but the interface
        //

        while (CurrentRTE != NULL)
        {
            if (IP_ADDR_EQUAL(CurrentRTE->rte_dest,Dest) &&
                CurrentRTE->rte_mask == Mask &&
                IP_ADDR_EQUAL(CurrentRTE->rte_addr, FirstHop))
            {
                break;
            }

            TempRTE = CurrentRTE;
            CurrentRTE = CurrentRTE->rte_next;
        }
    }
    else
    {
        // Walk the table, looking for a match.
        while (CurrentRTE != NULL) {
            // See if everything matches.
            if (IP_ADDR_EQUAL(CurrentRTE->rte_dest,Dest) &&
                CurrentRTE->rte_mask == Mask &&
                IP_ADDR_EQUAL(CurrentRTE->rte_addr, FirstHop) &&
                CurrentRTE->rte_if == OutIF)
                break;

            TempRTE = CurrentRTE;
            CurrentRTE = CurrentRTE->rte_next;
        }	
	}
	
	*PrevRTE = TempRTE;
	return CurrentRTE;

}

//** IsRouteICMP - This function is used by Router Discovery to determine
//  how we learned about the route. We are not allowed to update or timeout
//  routes that were not learned about via icmp. If the route is new then
//  we treat it as icmp and add a new entry.
//	Input:	Dest			- Destination to search for.
//			Mask			- Mask for destination.
//			FirstHop		- FirstHop to Dest.
//			OutIF			- Pointer to outgoing interface structure.
//
//	Returns: TRUE if learned via ICMP, FALSE otherwise.
//
uint
IsRouteICMP(IPAddr Dest, IPMask Mask, IPAddr FirstHop, Interface *OutIF)
{
	RouteTableEntry			*RTE;
	RouteTableEntry			*TempRTE;

    RTE = FindSpecificRTE(Dest, Mask, FirstHop, OutIF, &TempRTE);

    if (RTE == NULL)
        return(TRUE);

    if (RTE->rte_proto == IRE_PROTO_ICMP) {
        return(TRUE);
    } else {
        return(FALSE);
    }
}


//** FindRTE - Find a matching RTE in a hash table chain.
//
//	Called when we want to find a matching RTE. We take in a destination,
//	a source, a hash index, and a maximum priority, and walk down the
//	chain specified by the index looking for a matching RTE. If we can find
//	one, we'll keep looking hoping for a match on the source address.
//
//	The caller must hold the RouteTableLock before calling this function.
//
//	Input:	Dest				- Destination we're trying to reach.
//			Source				- Source address to match.
//			Index				- Index of chain to search.
//			MaxPri				- Maximum acceptable priority.
//			MinPri				- Minimum acceptable priority.
//
//	Returns: Pointer to RTE if found, or NULL if not.
//
RouteTableEntry *
FindRTE(IPAddr Dest, IPAddr Source, uint Index, uint MaxPri, uint MinPri)
{
	RouteTableEntry				*CurrentRTE;
	uint						RTEPri;
	uint						Metric;
	RouteTableEntry				*FoundRTE;
	
	// First walk down the chain, skipping those RTEs that have a
	// a priority greater than what we want.
	
	CurrentRTE = RouteTable[Index];
	
	for (;;) {
		if (CurrentRTE == NULL)
			return NULL;			// Hit end of chain, bounce out.
			
		if (CurrentRTE->rte_priority <= MaxPri)
			break;					// He's a possible match.
		
		// Priority is too big. Try the next one.
		CurrentRTE = CurrentRTE->rte_next;
	}
	
	FoundRTE = NULL;
	
	// When we get here, we have a locked RTE with a priority less than or
	// equal to what was specifed.
	// Examine it, and if it doesn't match try the next one. If it does match
	// we'll stash it temporarily and keep looking for one that matches the
	// specifed source.
	for (;;) {
		
		// The invariant at the top of this loop is that CurrentRTE points to
		// a candidate RTE, locked with the handle in CurrentHandle.

		if (CurrentRTE->rte_flags & RTE_VALID) {
			// He's valid. Make sure he's at least the priority we need. If
			// he is, see if he matches. Otherwise we're done.

			if (CurrentRTE->rte_priority < MinPri) {
				// His priority is too small. Since the list is in sorted order,
				// all following routes must have too low a priority, so we're
				// done.
				return NULL;
			}

			if (IP_ADDR_EQUAL(Dest & CurrentRTE->rte_mask, CurrentRTE->rte_dest)) {
				// He's valid for this route. Save the current information,
				// and look for a matching source.
 				FoundRTE = CurrentRTE;

				if (!IP_ADDR_EQUAL(Source, NULL_IP_ADDR) &&
					!AddrOnIF(CurrentRTE->rte_if, Source)) {
					RTEPri = CurrentRTE->rte_priority;
					Metric = CurrentRTE->rte_metric;
					
					CurrentRTE = CurrentRTE->rte_next;
					
					// We've save the info. Starting at the next RTE, look for
					// an RTE that matches both the mask criteria and the source
					// address. The search will terminate when we hit the end
					// of the list, or the RTE we're examing has a different
					// (presumably lesser) priority (or greater metric), or we
					// find a match.
					while (CurrentRTE != NULL &&
						CurrentRTE->rte_priority == RTEPri &&
						CurrentRTE->rte_metric == Metric) {
						
						
						// Skip invalid route types.
						if (CurrentRTE->rte_flags & RTE_VALID) {
							if (IP_ADDR_EQUAL(Dest & CurrentRTE->rte_mask,
								CurrentRTE->rte_dest)) {
								if (AddrOnIF(CurrentRTE->rte_if, Source)) {
									// He matches the source. Free the old lock,
									// and break out.
									FoundRTE = CurrentRTE;
									break;
								}
							}
									
						}
						CurrentRTE = CurrentRTE->rte_next;
					}
				}
					
				// At this point, FoundRTE points to the RTE we want to return,
				// and *Handle has the lock handle for the RTE. Break out.
				break;
			}
				
		}
		
		CurrentRTE = CurrentRTE->rte_next;
		
		if (CurrentRTE != NULL) {
			continue;
		} else
			break;
	}
	
	return FoundRTE;
}

//* ValidateDefaultGWs - Mark all default gateways as valid.
//
//	Called to one or all of our default gateways as up. The caller specifies
//	the IP address of the one to mark as up, or NULL_IP_ADDR if they're all
//	supposed to be marked up. We return a count of how many we marked as
//	valid.
//
//	Input: IP address of G/W to mark as up.
//
//	Returns: Count of gateways marked as up.
//
uint
ValidateDefaultGWs(IPAddr Addr)
{
	RouteTableEntry		*RTE;
	uint				Count = 0;
	uint				Now = CTESystemUpTime() / 1000L;
	
	RTE = RouteTable[IPHash(0)];
	
	while (RTE != NULL) {
		if (RTE->rte_mask == DEFAULT_MASK && !(RTE->rte_flags & RTE_VALID) &&
			(IP_ADDR_EQUAL(Addr, NULL_IP_ADDR) ||
			IP_ADDR_EQUAL(Addr, RTE->rte_addr))) {
			RTE->rte_flags |= RTE_VALID;
			RTE->rte_valid = Now;
			Count++;
		}
		RTE = RTE->rte_next;
	}	

	DefGWActive += Count;
	return Count;
}

//*	InvalidateRCEChain - Invalidate the RCEs on an RCE.
//
//	Called to invalidate the RCE chain on an RTE. We assume the caller holds
//	the route table lock.
//
//	Input:	RTE			- RTE on which to invalidate RCEs.
//
//	Returns: Nothing.
//
void
InvalidateRCEChain(RouteTableEntry *RTE)
{
    CTELockHandle   RCEHandle;      // Lock handle for RCE being updated.
    RouteCacheEntry *TempRCE, *CurrentRCE;
	Interface		*OutIF;

	OutIF = RTE->rte_if;

	// If there is an RCE chain on this RCE, invalidate the RCEs on it. We still
	// hold the RouteTableLock, so RCE closes can't happen.


    CurrentRCE = RTE->rte_rcelist;
	RTE->rte_rcelist = NULL;

	// Walk down the list, nuking each RCE.
    while (CurrentRCE != NULL) {

        CTEGetLock(&CurrentRCE->rce_lock, &RCEHandle);

		if (CurrentRCE->rce_flags & RCE_VALID) {
			CTEAssert(CurrentRCE->rce_rte == RTE);
			CurrentRCE->rce_flags &= ~RCE_VALID;
        	CurrentRCE->rce_rte = (RouteTableEntry *)OutIF;
			if ((CurrentRCE->rce_flags & RCE_CONNECTED) &&
				CurrentRCE->rce_usecnt == 0) {

				(*(OutIF->if_invalidate))(OutIF->if_lcontext, CurrentRCE);
#ifdef _PNP_POWER
				if (CurrentRCE->rce_flags & RCE_REFERENCED) {
					LockedDerefIF(OutIF);
					CurrentRCE->rce_flags &= ~RCE_REFERENCED;
				}
#endif
			}
		} else
			CTEAssert(FALSE);

        TempRCE = CurrentRCE->rce_next;
        CTEFreeLock(&CurrentRCE->rce_lock, RCEHandle);
        CurrentRCE = TempRCE;
    }

}

//** FindValidIFForRTE - Find a valid inteface for an RTE.
//
//	Called when we're going to send a packet out a route that currently marked
//	as disconnected. If we have a valid callout routine we'll call it to find
//	the outgoing interface index, and set up the RTE to point at that interface.
//	This routine is called with the RouteTableLock held.
//
//	Input:	RTE			- A pointer to the RTE for the route being used.
//			Destination	- Destination IP address we're trying to reach.
//			Source		- Source IP address we're sending from.
//			Protocol	- Protocol type of packet that caused send.
//			Buffer		- Pointer to first part of packet that caused send.
//			Length		- Length of buffer.
//
//	Returns: A pointer to the RTE, or NULL if that RTE couldn't be connected.
//
RouteTableEntry *
FindValidIFForRTE(RouteTableEntry *RTE, IPAddr Destination, IPAddr Source,
	uchar Protocol, uchar *Buffer, uint Length)
{
	uint			NewIFIndex;
	Interface		*NewIF;
	NetTableEntry	*NewNTE;

	if (DODCallout != NULL) {
		// There is a callout. See if it can help us.
		NewIFIndex = (*DODCallout)(RTE->rte_context, Destination, Source,
			Protocol, Buffer, Length);
		if (NewIFIndex != INVALID_IF_INDEX) {
			// We got what should be a valid index. Walk our interface table list
			// and see if we can find a matching interface structure.
			for (NewIF = IFList; NewIF != NULL; NewIF = NewIF->if_next) {
				if (NewIF->if_index == NewIFIndex) {
					// Found one.
					break;
				}
			}
			if (NewIF != NULL) {
				// We found a matching structure. Set the RTE interface to point
				// to this, and mark as connected.
				if (RTE->rte_addr != IPADDR_LOCAL) {
					// See if the first hop of the route is a local address on this
					// new interface. If it is, mark it as local.
					for (NewNTE = NewIF->if_nte; NewNTE != NULL;
						NewNTE = NewNTE->nte_ifnext) {

						// Don't look at him if he's not valid.
						if (!(NewNTE->nte_flags & NTE_VALID)) {
							continue;
						}

						// See if the first hop in the RTE is equal to this IP
						// address.
						if (IP_ADDR_EQUAL(NewNTE->nte_addr, RTE->rte_addr)) {
							// It is, so mark as local and quit looking.
							RTE->rte_addr = IPADDR_LOCAL;
							RTE->rte_type = IRE_TYPE_DIRECT;
							break;
						}
					}
				}

				// Set the RTE to the new interface, and mark him as valid.
				RTE->rte_if = NewIF;
				RTE->rte_flags |= RTE_IF_VALID;
				RTE->rte_mtu = NewIF->if_mtu - sizeof(IPHeader);
				return RTE;
			} else
				CTEAssert(FALSE);
		}
	}

	// Either the callout is NULL, or the callout couldn't map a inteface index.
	return NULL;
}

//** LookupRTE - Lookup a routing table entry.
//
//  This routine looks up a routing table entry, and returns with the entry
//	locked if it finds one. If it doesn't find one, it returns NULL. This
//	routine assumes that the routing table is locked when it is called.
//
//  The routeing table is organized as an open hash table. The table contains
//	routes to hosts, subnets, and nets. Host routes are hashed on the host
//	address, other non-default routes on the destination anded with the net
//	mask, and default routes wind up in bucket 0. Within each bucket chain
//	the routes are sorted with the greatest priority (i.e. number of bits in the
//	route mask) first, and within each priority class the routes are sorted
//	with the lowest metric first. The caller may specify a maximum priority
//  for the route to be found. We look for routes in order of most specific to
//	least specifc, i.e. first host routes, then other non-default routes, and
//	finally default routes. We give preference to routes that are going out
//	on an interface with an address that matches the input source address.
//
//	It might be worthile in the future to split this up into multiple tables,
//	so that we have a table for host routes, a table for non-default routes,
//	and a list of default routes.
//
//  Entry:  Address     - Address for which a route is to be found.
//          Src         - IPAddr of source (may be 0).
//          MaxPri		- Maximum priority of route to find.
//
//  Returns: A pointer to the locked RTE if we find one, or NULL if we don't
//
RouteTableEntry *
LookupRTE(IPAddr Address,  IPAddr Src, uint MaxPri)
{
	RouteTableEntry		*RTE;
	
	// First try to find a host route, if we're allowed to.
	if (MaxPri == HOST_ROUTE_PRI) {
		RTE = FindRTE(Address, Src, IPHash(Address), MaxPri, MaxPri);
		if (RTE != NULL) {
			return RTE;
		}
	}
	
	// Don't have or weren't allowed to find a host route. See if we can
	// find a non-default route.
	if (MaxPri > DEFAULT_ROUTE_PRI) {
		RTE = FindRTE(Address, Src, IPHash(Address & IPNetMask(Address)),
			MaxPri, DEFAULT_ROUTE_PRI + 1);
		if (RTE != NULL) {
			return RTE;
		}
	}
	
	// No non-default route. Try a default route.
	RTE = FindRTE(Address, Src, IPHash(0), MaxPri, DEFAULT_ROUTE_PRI);

	return RTE;
		
}

//** GetRouteContext - Routine to get the route context for a specific route.
//
//	Called when we need to get the route context for a path, usually when we're adding
//	a route derived from an existing route. We return the route context for the
//	existing route, or NULL if we can't find one.
//
//	Input:	Destination			- Destination address of path.
//			Source				- Source address of path.
//
//	Returns: A ROUTE_CONTEXT, or NULL.
//
void *
GetRouteContext(IPAddr Destination, IPAddr Source)
{
	CTELockHandle		Handle;
	RouteTableEntry		*RTE;
	ROUTE_CONTEXT		Context;

	CTEGetLock(&RouteTableLock, &Handle);
	RTE = LookupRTE(Destination, Source, HOST_ROUTE_PRI);
	if (RTE != NULL) {
		Context = RTE->rte_context;
	} else
		Context = NULL;

	CTEFreeLock(&RouteTableLock, Handle);

    return(Context);
}

//* FindInsertPoint - Find out where to insert an RTE in the table.
//
//	Called to find out where to insert an RTE. We hash into the table,
//	and walk the chain until we hit the end or we find an RTE with a
//	lesser priority or a greater metric. Once we find the appropriate spot
//	we return a pointer to the RTE immediately prior to the one we want to
//	insert. We assume the caller holds the lock on the route table when calling
//	this function.
//
//	Input:	InsertRTE		- RTE we're going to (eventually) insert.
//
//	Returns: Pointer to RTE in insert after.
//
RouteTableEntry *
FindInsertPoint(RouteTableEntry *InsertRTE)
{
	RouteTableEntry				*PrevRTE, *CurrentRTE;
	IPMask						HashMask, Mask;
	uint						Priority, Metric;
	uint						Index;

	Priority = InsertRTE->rte_priority;
	Metric = InsertRTE->rte_metric;

	// First figure out where he should go. We'll hash on the whole address
	// if the mask allows us to, or on the net portion if this is a non-default
	// route.

	Mask = InsertRTE->rte_mask;
	HashMask = GetHashMask(InsertRTE->rte_dest, Mask);
	
	Index = IPHash(InsertRTE->rte_dest & HashMask);

	PrevRTE = STRUCT_OF(RouteTableEntry, &RouteTable[Index], rte_next);
	CurrentRTE = PrevRTE->rte_next;
	
	// Walk the table, looking for a place to insert it.
	while (CurrentRTE != NULL) {
		if (CurrentRTE->rte_priority < Priority) {
			break;
		}

		if (CurrentRTE->rte_priority == Priority) {
			// Priorities match. Check the metrics.
			if (CurrentRTE->rte_metric > Metric) {
				// Our metric is smaller than his, so we're done.
				break;
			}
		}

		// Either his priority is greater than ours or his metric is less
		// than or equal to ours. Check the next one.
		PrevRTE = CurrentRTE;
		CurrentRTE = CurrentRTE->rte_next;
	}

	// At this point, we've either found the correct spot or hit the end
	// of the list.
	return PrevRTE;
	
}


//** LookupNextHop - Look up the next hop
//
//  Called when we need to find the next hop on our way to a destination. We
//	call LookupRTE to find it, and return the appropriate information.
//
//	In a PnP build, the interface is referenced here.
//
//  Entry:  Destination     - IP address we're trying to reach.
//          Src             - Source address of datagram being routed.
//          NextHop         - Pointer to IP address of next hop (returned).
//			MTU				- Pointer to where to return max MTU used on the
//								route.
//
//  Returns: Pointer to outgoing interface if we found one, NULL otherwise.
//
Interface *
LookupNextHop(IPAddr Destination, IPAddr Src, IPAddr *NextHop, uint *MTU)
{
    CTELockHandle   TableLock;      // Lock handle for routing table.
    RouteTableEntry *Route;         // Pointer to route table entry for route.
	Interface		*IF;

    CTEGetLock(&RouteTableLock, &TableLock);
    Route = LookupRTE(Destination, Src, HOST_ROUTE_PRI);

    if (Route != (RouteTableEntry *)NULL) {
        IF = Route->rte_if;
		
		// If this is a direct route, send straight to the destination.
        *NextHop = IP_ADDR_EQUAL(Route->rte_addr, IPADDR_LOCAL) ? Destination :
			Route->rte_addr;
			
		*MTU = Route->rte_mtu;
#ifdef _PNP_POWER
		IF->if_refcount++;
#endif
        CTEFreeLock(&RouteTableLock, TableLock);
        return IF;          		
    } else {                        // Couldn't find a route.
        CTEFreeLock(&RouteTableLock, TableLock);
        return NULL;
    }
}

//** LookupNextHopWithBuffer - Look up the next hop, with packet information.
//
//  Called when we need to find the next hop on our way to a destination and we
//	have packet information that we may use for dial on demand support. We call
//	LookupRTE to find it, and return the appropriate information. We may bring up
//	the link if neccessary.
//
//	In a PnP build, the interface is referenced here.
//
//  Entry:  Destination     - IP address we're trying to reach.
//          Src             - Source address of datagram being routed.
//          NextHop         - Pointer to IP address of next hop (returned).
//			MTU				- Pointer to where to return max MTU used on the
//								route.
//			Protocol		- Protocol type for packet that's causing this lookup.
//			Buffer			- Pointer to first part of packet causing lookup.
//			Length			- Length of Buffer.
//
//  Returns: Pointer to outgoing interface if we found one, NULL otherwise.
//
Interface *
LookupNextHopWithBuffer(IPAddr Destination, IPAddr Src, IPAddr *NextHop,
	uint *MTU, uchar Protocol, uchar *Buffer, uint Length)
{
    CTELockHandle   TableLock;      // Lock handle for routing table.
    RouteTableEntry *Route;         // Pointer to route table entry for route.
	Interface		*IF;

    CTEGetLock(&RouteTableLock, &TableLock);
    Route = LookupRTE(Destination, Src, HOST_ROUTE_PRI);

    if (Route != (RouteTableEntry *)NULL) {
		
		// If this is a direct route, send straight to the destination.
        *NextHop = IP_ADDR_EQUAL(Route->rte_addr, IPADDR_LOCAL) ? Destination :
			Route->rte_addr;
			

		// See if the route we found is connected. If not, try to connect it.
		if (!(Route->rte_flags & RTE_IF_VALID)) {
			Route = FindValidIFForRTE(Route, Destination, Src, Protocol, Buffer,
				Length);
			if (Route == NULL) {
				// Couldn't bring it up.
                CTEFreeLock(&RouteTableLock, TableLock);
                return NULL;
			} else
				IF = Route->rte_if;
		} else
			IF = Route->rte_if;

		*MTU = Route->rte_mtu;
#ifdef _PNP_POWER
		IF->if_refcount++;
#endif
        CTEFreeLock(&RouteTableLock, TableLock);
        return IF;          		
    } else {                        // Couldn't find a route.
        CTEFreeLock(&RouteTableLock, TableLock);
        return NULL;
    }
}

//* RTReadNext - Read the next route in the table.
//
//  Called by the GetInfo code to read the next route in the table. We assume
//  the context passed in is valid, and the caller has the RouteTableLock.
//
//  Input:  Context     - Pointer to a RouteEntryContext.
//          Buffer      - Pointer to an IPRouteEntry structure.
//
//  Returns: TRUE if more data is available to be read, FALSE is not.
//
uint
RTReadNext(void *Context, void *Buffer)
{
    RouteEntryContext   *REContext = (RouteEntryContext *)Context;
    IPRouteEntry        *IPREntry = (IPRouteEntry *)Buffer;
    RouteTableEntry     *CurrentRTE;
    uint                i;
    uint                Now = CTESystemUpTime() / 1000L;
	Interface			*IF;
	NetTableEntry		*SrcNTE;

	CurrentRTE = REContext->rec_rte;
	
	// Fill in the buffer.
	IF = CurrentRTE->rte_if;

	IPREntry->ire_dest = CurrentRTE->rte_dest;
	IPREntry->ire_index = IF->if_index;
	IPREntry->ire_metric1 = CurrentRTE->rte_metric;
	IPREntry->ire_metric2 = IRE_METRIC_UNUSED;
	IPREntry->ire_metric3 = IRE_METRIC_UNUSED;
	IPREntry->ire_metric4 = IRE_METRIC_UNUSED;
	IPREntry->ire_metric5 = IRE_METRIC_UNUSED;
	if (IP_ADDR_EQUAL(CurrentRTE->rte_addr, IPADDR_LOCAL)) {
		SrcNTE = BestNTEForIF(CurrentRTE->rte_dest, IF);
		if (IF->if_nte != NULL && SrcNTE != NULL)
			IPREntry->ire_nexthop = SrcNTE->nte_addr;
		else
			IPREntry->ire_nexthop = IPREntry->ire_dest;
	} else {
		IPREntry->ire_nexthop = CurrentRTE->rte_addr;
	}
	IPREntry->ire_type = (CurrentRTE->rte_flags & RTE_VALID ?
		CurrentRTE->rte_type : IRE_TYPE_INVALID);
	IPREntry->ire_proto = CurrentRTE->rte_proto;	
	IPREntry->ire_age = Now - CurrentRTE->rte_valid;
	IPREntry->ire_mask = CurrentRTE->rte_mask;
	IPREntry->ire_context = CurrentRTE->rte_context;

    // We've filled it in. Now update the context.
    if (CurrentRTE->rte_next != NULL) {
        REContext->rec_rte = CurrentRTE->rte_next;
        return TRUE;
    } else {
        // The next RTE is NULL. Loop through the RouteTable looking for a new
        // one.
        i = REContext->rec_index + 1;
        while (i < ROUTE_TABLE_SIZE) {
            if (RouteTable[i] != NULL) {
                REContext->rec_rte = RouteTable[i];
                REContext->rec_index = i;
                return TRUE;
                break;
            } else
                i++;
        }

        REContext->rec_index = 0;
        REContext->rec_rte = NULL;
        return FALSE;
    }

}

//* RTValidateContext - Validate the context for reading the route table.
//
//  Called to start reading the route table sequentially. We take in
//  a context, and if the values are 0 we return information about the
//  first route in the table. Otherwise we make sure that the context value
//  is valid, and if it is we return TRUE.
//  We assume the caller holds the route table lock.
//
//  Input:  Context     - Pointer to a RouteEntryContext.
//          Valid       - Where to return information about context being
//                          valid.
//
//  Returns: TRUE if more data to be read in table, FALSE if not. *Valid set
//      to TRUE if input context is valid
//
uint
RTValidateContext(void *Context, uint *Valid)
{
    RouteEntryContext   *REContext = (RouteEntryContext *)Context;
    uint                i;
    RouteTableEntry     *TargetRTE;
    RouteTableEntry     *CurrentRTE;

    i = REContext->rec_index;
    TargetRTE = REContext->rec_rte;

    // If the context values are 0 and NULL, we're starting from the beginning.
    if (i == 0 && TargetRTE == NULL) {
        *Valid = TRUE;
        do {
            if ((CurrentRTE = RouteTable[i]) != NULL) {
                break;
            }
            i++;
        } while (i < ROUTE_TABLE_SIZE);

        if (CurrentRTE != NULL) {
            REContext->rec_index = i;
            REContext->rec_rte = CurrentRTE;
            return TRUE;
        } else
            return FALSE;

    } else {

        // We've been given a context. We just need to make sure that it's
        // valid.

        if (i < ROUTE_TABLE_SIZE) {
            CurrentRTE = RouteTable[i];
            while (CurrentRTE != NULL) {
                if (CurrentRTE == TargetRTE) {
                    *Valid = TRUE;
                    return TRUE;
                    break;
                } else {
                    CurrentRTE = CurrentRTE->rte_next;
                }
            }

        }

        // If we get here, we didn't find the matching RTE.
        *Valid = FALSE;
        return FALSE;

    }

}


//*	DeleteRTE - Delete an RTE.
//
//	Called when we need to delete an RTE. We assume the caller has the
//	RouteTableLock. We'll splice out the RTE, invalidate his RCEs, and
//	free the memory.
//
//	Input:	PrevRTE				- RTE in 'front' of one being deleted.
//			RTE					- RTE to be deleted.
//
//	Returns: Nothing.
//
void
DeleteRTE(RouteTableEntry *PrevRTE, RouteTableEntry *RTE)
{
	PrevRTE->rte_next = RTE->rte_next;		// Splice him from the table.
	IPSInfo.ipsi_numroutes--;

	if (RTE->rte_mask == DEFAULT_MASK) {
		// We're deleting a default route.
		DefGWConfigured--;
		if (RTE->rte_flags & RTE_VALID)
			DefGWActive--;
		if (DefGWActive == 0)
			ValidateDefaultGWs(NULL_IP_ADDR);
	}
	
	InvalidateRCEChain(RTE);
    // Free the old route.
    CTEFreeMem(RTE);

}

//*	DeleteRTEOnIF - Delete all RTEs on a particular IF.
//
//	A function called by RTWalk when we want to delete all RTEs on a particular
//	inteface. We just check the I/F of each RTE, and if it matches we return
//	FALSE.
//
//	Input:	RTE			- RTE to check.
//			Context 	- Interface on which we're deleting.
//
//	Returns: FALSE if we want to delete it, TRUE otherwise.
//
uint
DeleteRTEOnIF(RouteTableEntry *RTE, void *Context, void *Context1)
{
	Interface		*IF = (Interface *)Context;

	if (RTE->rte_if == IF && !IP_ADDR_EQUAL(RTE->rte_dest, IF->if_bcast))
		return FALSE;
	else
		return TRUE;
	
}

//*	InvalidateRCEOnIF - Invalidate all RCEs on a particular IF.
//
//	A function called by RTWalk when we want to invalidate all RCEs on a
//	particular inteface. We just check the I/F of each RTE, and if it
//	matches we call InvalidateRCEChain to invalidate the RCEs.
//
//	Input:	RTE			- RTE to check.
//			Context 	- Interface on which we're invalidating.
//
//	Returns: TRUE.
//
uint
InvalidateRCEOnIF(RouteTableEntry *RTE, void *Context, void *Context1)
{
	Interface		*IF = (Interface *)Context;

	if (RTE->rte_if == IF)
		InvalidateRCEChain(RTE);
	
	return TRUE;
	
}

//*	SetMTUOnIF - Set the MTU on an interface.
//
//	Called when we need to set the MTU on an interface.
//
//	Input:	RTE			- RTE to check.
//			Context		- Pointer to a context.
//			Context1	- Pointer to the new MTU.
//
//	Returns: TRUE.
//
uint
SetMTUOnIF(RouteTableEntry *RTE, void *Context, void *Context1)
{
	uint		NewMTU = *(uint *)Context1;
	Interface	*IF = (Interface *)Context;
	
	if (RTE->rte_if == IF)
		RTE->rte_mtu = NewMTU;
	
	return TRUE;
}

//*	SetMTUToAddr - Set the MTU to a specific address.
//
//	Called when we need to set the MTU to a specific address. We set the MTU
//	for all routes that use the specified address as a first hop to the new
//	MTU.
//
//	Input:	RTE			- RTE to check.
//			Context		- Pointer to a context.
//			Context1	- Pointer to the new MTU.
//
//	Returns: TRUE.
//
uint
SetMTUToAddr(RouteTableEntry *RTE, void *Context, void *Context1)
{
	uint		NewMTU = *(uint *)Context1;
	IPAddr		Addr = *(IPAddr *)Context;
	
	if (IP_ADDR_EQUAL(RTE->rte_addr, Addr))
		RTE->rte_mtu = NewMTU;
	
	return TRUE;
}

//*	RTWalk - Routine to walk the route table.
//
//	This routine walks the route table, calling the specified function
//	for each entry. If the called function returns FALSE, the RTE is
//	deleted.
//
//	Input:	CallFunc			- Function to call for each entry.
//			Context				- Context value to pass to each call.
//
//	Returns: Nothing.
//
void
RTWalk(uint (*CallFunc)(struct RouteTableEntry *, void *, void *),
	void *Context, void *Context1)
{
	uint			i;
	CTELockHandle	Handle;
	RouteTableEntry	*RTE, *PrevRTE;

	CTEGetLock(&RouteTableLock, &Handle);
	
	for (i = 0; i < ROUTE_TABLE_SIZE; i++) {
		
		PrevRTE = STRUCT_OF(RouteTableEntry, &RouteTable[i], rte_next);
		RTE = RouteTable[i];
		while (RTE != NULL) {
			if (!(*CallFunc)(RTE, Context, Context1)) {
				DeleteRTE(PrevRTE, RTE);
			} else {
				PrevRTE = RTE;
			}
			RTE = PrevRTE->rte_next;
		}
	}

	CTEFreeLock(&RouteTableLock, Handle);
}

//** AttachRCEToRTE - Attach an RCE to an RTE.
//
//  This procedure takes an RCE, finds the appropriate RTE, and attaches it.
//	We check to make sure that the source address is still valid.
//
//  Entry:  RCE             - RCE to be attached.
//			Protocol		- Protocol type for packet causing this call.
//			Buffer			- Pointer to buffer for packet causing this
//								call.
//			Length			- Length of buffer.
//
//  Returns: TRUE if we attach it, false if we don't.
//
uint
AttachRCEToRTE(RouteCacheEntry *RCE, uchar Protocol, uchar *Buffer, uint Length)
{
    CTELockHandle   TableHandle, RCEHandle;
	RouteTableEntry	*RTE;
	NetTableEntry	*NTE;
	uint			Status;


    CTEGetLock(&RouteTableLock, &TableHandle);
	
	for (NTE = NetTableList; NTE != NULL; NTE = NTE->nte_next)
		if ((NTE->nte_flags & NTE_VALID) &&
			IP_ADDR_EQUAL(RCE->rce_src, NTE->nte_addr))
			break;

	if (NTE == NULL) {
		// Didn't find a match.
		CTEFreeLock(&RouteTableLock, TableHandle);
		return FALSE;
	}

	RTE = LookupRTE(RCE->rce_dest, RCE->rce_src, HOST_ROUTE_PRI);
	
	// See if we found an RTE.
    if (RTE != NULL) {

		Status = TRUE;

		// Yep, we found one. Get the lock on the RCE, and make sure he's
		// not pointing at an RTE already. We also need to make sure that the usecnt
		// is 0, so that we can invalidate the RCE at the low level. If we set valid
		// to TRUE without doing this we may get into a wierd situation where we
		// link the RCE onto an RTE but the lower layer information is wrong, so we
		// send to IP address X at mac address Y. So to be safe we don't set valid
		// to TRUE until both usecnt is 0 and valid is FALSE. We'll keep coming
		// through this routine on every send until that happens.

        CTEGetLock(&RCE->rce_lock, &RCEHandle);
		if (RCE->rce_usecnt == 0) {
			// Nobody is using him, so we can link him up.
			if (!(RCE->rce_flags & RCE_VALID)) {
				Interface			*IF;
				// He's not valid. Invalidate the lower layer info, just in
				// case. Make sure he's connected before we try to do this. If
				// he's not marked as connected, don't bother to try and invalidate
				// him as there is no interface.
				if (RCE->rce_flags & RCE_CONNECTED) {
					IF = (Interface *)RCE->rce_rte;
					(*(IF->if_invalidate))(IF->if_lcontext, RCE);
#ifdef _PNP_POWER
					if (RCE->rce_flags & RCE_REFERENCED) {
						LockedDerefIF(IF);
						RCE->rce_flags &= ~RCE_REFERENCED;
					}
#endif
				} else {
					CTEAssert(!(RCE->rce_flags & RCE_REFERENCED));
				}

				// Link the RCE on the RTE, and set up the back pointer.
	            RCE->rce_rte = RTE;
				RCE->rce_flags |= RCE_VALID;
	            RCE->rce_next = RTE->rte_rcelist;
	            RTE->rte_rcelist = RCE;

				// Make sure the RTE is connected. If not, try to connect him.
				if (!(RTE->rte_flags & RTE_IF_VALID)) {
					// Not connected. Try to connect him.
					RTE = FindValidIFForRTE(RTE, RCE->rce_dest, RCE->rce_src,
						Protocol, Buffer, Length);
					if (RTE != NULL) {
						// Got one, so mark as connected.
						CTEAssert(!(RCE->rce_flags & RCE_REFERENCED));
#ifdef _PNP_POWER
						RCE->rce_flags |= (RCE_CONNECTED | RCE_REFERENCED);
						RTE->rte_if->if_refcount++;
#else
						RCE->rce_flags |= RCE_CONNECTED;

#endif
					} else {

						// Couldn't get a valid i/f. Mark the RCE as not connected,
						// and set up to fail this call.
						CTEAssert(FALSE);
						RCE->rce_flags &= ~RCE_CONNECTED;
						Status = FALSE;
					}
				} else {
					// The RTE is connected, mark the RCE as connected.
					CTEAssert(!(RCE->rce_flags & RCE_REFERENCED));
#ifdef _PNP_POWER
					RCE->rce_flags |= (RCE_CONNECTED | RCE_REFERENCED);
					RTE->rte_if->if_refcount++;
#else
					RCE->rce_flags |= RCE_CONNECTED;
#endif
				}
        	} else {
				// The RCE is valid. See if it's connected.
				if (!(RCE->rce_flags & RCE_CONNECTED)) {

					// Not connected, try to get a valid i/f.
					if (!(RTE->rte_flags & RTE_IF_VALID)) {
						RTE = FindValidIFForRTE(RTE, RCE->rce_dest, RCE->rce_src,
							Protocol, Buffer, Length);
						if (RTE != NULL) {
							RCE->rce_flags |= RCE_CONNECTED;
#ifdef _PNP_POWER
							CTEAssert(!(RCE->rce_flags & RCE_REFERENCED));
							RCE->rce_flags |= RCE_REFERENCED;
							RTE->rte_if->if_refcount++;
#endif
						} else {

							// Couldn't connect, so fail.
							CTEAssert(FALSE);
							Status = FALSE;
						}
					} else {	// Already connected, just mark as valid.
						RCE->rce_flags |= RCE_CONNECTED;
#ifdef _PNP_POWER
						if (!(RCE->rce_flags & RCE_REFERENCED)) {
							RCE->rce_flags |= RCE_REFERENCED;
							RTE->rte_if->if_refcount++;
						}
#endif
					}
				}
			}
		}

		// Free the locks and we're done.
        CTEFreeLock(&RCE->rce_lock, RCEHandle);
        CTEFreeLock(&RouteTableLock, TableHandle);
        return Status;
    } else {
        // No route! Fail the call.
        CTEFreeLock(&RouteTableLock, TableHandle);
        return FALSE;
    }

}
//** IPGetPInfo - Get information..
//
//	Called by an upper layer to get information about a path. We return the
//	MTU of the path and the maximum link speed to be expected on the path.
//
//	Input:	Dest		- Destination address.
//			Src			- Src address.
//			NewMTU		- Where to store path MTU (may be NULL).
//			MaxPathSpeed - Where to store maximum path speed (may be NULL).
//
//	Returns: Status of attempt to get new MTU.
//
IP_STATUS		
IPGetPInfo(IPAddr Dest, IPAddr Src, uint *NewMTU, uint *MaxPathSpeed)
{
	CTELockHandle		Handle;
	RouteTableEntry		*RTE;
	IP_STATUS			Status;
	
	CTEGetLock(&RouteTableLock, &Handle);
   	RTE = LookupRTE(Dest, Src, HOST_ROUTE_PRI);
	if (RTE != NULL) {
		if (NewMTU != NULL)
			*NewMTU = RTE->rte_mtu;
		if (MaxPathSpeed != NULL)
			*MaxPathSpeed = RTE->rte_if->if_speed;
		Status = IP_SUCCESS;
	} else
		Status = IP_DEST_HOST_UNREACHABLE;
	
	CTEFreeLock(&RouteTableLock, Handle);
	return Status;
	
}

//** IPCheckRoute - Check that a route is valid.
//
//	Called by an upper layer when it believes a route might be invalid.
//	We'll check if we can. If the upper layer is getting there through a
//	route derived via ICMP (presumably a redirect) we'll check to see
//	if it's been learned within the last minute. If it has, it's assumed
//	to still be valid. Otherwise, we'll mark it as down and try to find
//	another route there. If we can, we'll delete the old route. Otherwise
//	we'll leave it. If the route is through a default gateway we'll switch
//	to another one if we can. Otherwise, we'll just leave - we don't mess
//	with manually configured routes.
//
//	Input:	Dest			- Destination to be reached.
//			Src				- Src we're sending from.
//	
//	Returns: Nothing.
//
void
IPCheckRoute(IPAddr Dest, IPAddr Src)
{
	RouteTableEntry			*RTE;
	RouteTableEntry			*NewRTE;
	RouteTableEntry			*TempRTE;
	CTELockHandle			Handle;
	uint					Now = CTESystemUpTime() / 1000L;
	
	if (DeadGWDetect) {
		// We are doing dead G/W detection. Get the lock, and try and
		// find the route.
		CTEGetLock(&RouteTableLock, &Handle);
    	RTE = LookupRTE(Dest, Src, HOST_ROUTE_PRI);
		if (RTE != NULL && ((Now - RTE->rte_valid) > MIN_RT_VALID)) {
			
			// Found a route, and it's older than the minimum valid time. If it
			// goes through a G/W, and is a route we learned via ICMP or is a
			// default route, do something with it.
			if (!IP_ADDR_EQUAL(RTE->rte_addr, IPADDR_LOCAL)) {
				// It's not through a G/W.
				
				if (RTE->rte_proto == IRE_PROTO_ICMP) {
				
					// Came from ICMP. Mark as invalid, and then make sure
					// we have another route there.
					RTE->rte_flags &= ~RTE_VALID;
    				NewRTE = LookupRTE(Dest, Src, HOST_ROUTE_PRI);
					
					if (NewRTE == NULL) {
						// Can't get there any other way so leave this
						// one alone.
						RTE->rte_flags |= RTE_VALID;
					} else {
						// There is another route, so destroy this one. Use
						// FindSpecificRTE to find the previous RTE.
						TempRTE = FindSpecificRTE(RTE->rte_dest, RTE->rte_mask,
							RTE->rte_addr, RTE->rte_if, &NewRTE);
						CTEAssert(TempRTE == RTE);
						DeleteRTE(NewRTE, TempRTE);
					}
				} else {
					if (RTE->rte_mask == DEFAULT_MASK) {
						
						// This is a default gateway. If we have more than one
						// configured move to the next one.
						
						if (DefGWConfigured > 1) {
							// Have more than one. Try the next one. First
							// invalidate any RCEs on this G/W.
							
							InvalidateRCEChain(RTE);
							if (DefGWActive == 1) {
								// No more active. Revalidate all of them,
								// and try again.
								ValidateDefaultGWs(NULL_IP_ADDR);
								CTEAssert(DefGWActive == DefGWConfigured);
							} else {
								// More than one active, so invalidate this
								// one, and move to the next one. Stamp the
								// next one with a valid time of Now, so we
								// don't move from him too easily.
								--DefGWActive;
								RTE->rte_flags &= ~RTE_VALID;
								RTE = FindRTE(Dest, Src, IPHash(0),
									DEFAULT_ROUTE_PRI, DEFAULT_ROUTE_PRI);
								if (RTE == NULL) {
									// No more default gateways! This is bad.
									CTEAssert(FALSE);
									ValidateDefaultGWs(NULL_IP_ADDR);
									CTEAssert(DefGWActive == DefGWConfigured);
								} else {
									CTEAssert(RTE->rte_mask == DEFAULT_MASK);
									RTE->rte_valid = Now;
								}
							}
						}
					}
				}
			}
		}
		CTEFreeLock(&RouteTableLock, Handle);
	}	
}						
						
					
//** FindRCE - Find an RCE on an RTE.
//
//  A routine to find an RCE that's chained on an RTE. We assume the lock
//  is held on the RTE.
//
//  Entry:  RTE             - RTE to search.
//          Dest            - Destination address of RTE to find.
//          Src             - Source address of RTE to find.
//
//  Returns: Pointer to RTE found, or NULL.
//
RouteCacheEntry *
FindRCE(RouteTableEntry *RTE, IPAddr Dest, IPAddr Src)
{
    RouteCacheEntry     *CurrentRCE;

    CTEAssert(!IP_ADDR_EQUAL(Src, NULL_IP_ADDR));
    for (CurrentRCE = RTE->rte_rcelist; CurrentRCE != NULL;
        CurrentRCE = CurrentRCE->rce_next) {
        if ( IP_ADDR_EQUAL(CurrentRCE->rce_dest, Dest) &&
             IP_ADDR_EQUAL(CurrentRCE->rce_src, Src)) {
            break;
        }
    }
    return CurrentRCE;

}

//** OpenRCE - Open an RCE for a specific route.
//
//  Called by the upper layer to open an RCE. We look up the type of the address
//  - if it's invalid, we return 'Destination invalid'. If not, we look up the
//  route, fill in the RCE, and link it on the correct RTE.
//
//  As an added bonus, this routine will return the local address to use
//  to reach the destination.
//
//  Entry:  Address         - Address for which we are to open an RCE.
//          Src             - Source address we'll be using.
//          RCE             - Pointer to where to return pointer to RCE.
//          Type            - Pointer to where to return destination type.
//          MSS             - Pointer to where to return MSS for route.
//          OptInfo         - Pointer to option information, such as TOS and
//                              any source routing info.
//
//  Returns: Source IP address to use. This will be NULL_IP_ADDR if the
//          specified destination is unreachable for any reason.
//
IPAddr
OpenRCE(IPAddr Address, IPAddr Src, RouteCacheEntry **RCE, uchar *Type,
    ushort *MSS, IPOptInfo *OptInfo)
{
    RouteTableEntry     *RTE;           // Pointer to RTE to put RCE on.
    CTELockHandle       TableLock;
    uchar               LocalType;


    if (!IP_ADDR_EQUAL(OptInfo->ioi_addr, NULL_IP_ADDR))
        Address = OptInfo->ioi_addr;

	CTEGetLock(&RouteTableLock, &TableLock);

	// Make sure we're not in DHCP update.
	if (DHCPActivityCount != 0) {
		// We are updating DHCP. Just fail this now, since we're in an
		// indeterminate state.
		CTEFreeLock(&RouteTableLock, TableLock);
		return NULL_IP_ADDR;
	}

    LocalType = GetAddrType(Address);

    *Type = LocalType;

    // If the specified address isn't invalid, continue.
    if (LocalType != DEST_INVALID) {
        RouteCacheEntry     *NewRCE;

		// If he's specified a source address, loop through the NTE table
		// now and make sure it's valid.
		if (!IP_ADDR_EQUAL(Src, NULL_IP_ADDR)) {
			NetTableEntry	*NTE;

			for (NTE = NetTableList; NTE != NULL; NTE = NTE->nte_next)
				if ((NTE->nte_flags & NTE_VALID) &&
					IP_ADDR_EQUAL(Src, NTE->nte_addr))
					break;

			if (NTE == NULL) {
				// Didn't find a match.
				CTEFreeLock(&RouteTableLock, TableLock);
				return NULL_IP_ADDR;
			}
		}

        // Find the route for this guy. If we can't find one, return NULL.
		RTE = LookupRTE(Address, Src, HOST_ROUTE_PRI);

        if (RTE != (RouteTableEntry *)NULL) {
            CTELockHandle   RCEHandle;
            RouteCacheEntry *OldRCE;

			// We found one.
            *MSS = (ushort)RTE->rte_mtu;			// Return the route MTU.
			
			if (IP_LOOPBACK_ADDR(Src) && (RTE->rte_if != &LoopInterface)) {
				// The upper layer is sending from a loopback address, but the
				// destination isn't reachable through the loopback interface.
				// Fail the request.
				CTEFreeLock(&RouteTableLock, TableLock);
				return NULL_IP_ADDR;
			}

            // We have the RTE. Fill in the RCE, and link it on the RTE.
            if (!IP_ADDR_EQUAL(RTE->rte_addr, IPADDR_LOCAL))
                *Type |= DEST_OFFNET_BIT;   // Tell upper layer it's off
                                            // net.

            //
            // If no source address was specified, then use the best address
            // for the interface. This will generally prevent dynamic NTE's from
            // being chosen as the source for wildcard binds.
            //
            if (IP_ADDR_EQUAL(Src, NULL_IP_ADDR)) {

                if (LocalType == DEST_LOCAL)
                    Src = Address;
                else {
                    NetTableEntry *SrcNTE;

                    SrcNTE = BestNTEForIF(
                                 ADDR_FROM_RTE(RTE, Address),
                                 RTE->rte_if
                                 );

                    if (SrcNTE == NULL) {
                        // Can't find an address! Fail the request.
                        CTEFreeLock(&RouteTableLock, TableLock);
                        return NULL_IP_ADDR;
                    }

                    Src = SrcNTE->nte_addr;
                }
            }

            // Now, see if an RCE already exists for this.
            if ((OldRCE = FindRCE(RTE, Address, Src)) == NULL) {

                // Don't have an existing RCE. See if we can get a new one,
                // and fill it in.

		        NewRCE = CTEAllocMem(sizeof(RouteCacheEntry));
		        *RCE = NewRCE;

                if (NewRCE != NULL) {
                    CTEMemSet(NewRCE, 0, sizeof(RouteCacheEntry));

                    NewRCE->rce_src = Src;
                    NewRCE->rce_dtype = LocalType;
                    NewRCE->rce_cnt = 1;
                    CTEInitLock(&NewRCE->rce_lock);
                    NewRCE->rce_dest = Address;
                    NewRCE->rce_rte = RTE;
					NewRCE->rce_flags = RCE_VALID;
					if (RTE->rte_flags & RTE_IF_VALID) {
						NewRCE->rce_flags |= RCE_CONNECTED;
#ifdef _PNP_POWER
						//* Update the ref. count for this interface.
						NewRCE->rce_flags |= RCE_REFERENCED;
						RTE->rte_if->if_refcount++;
#endif
					}
                    NewRCE->rce_next = RTE->rte_rcelist;
                    RTE->rte_rcelist = NewRCE;
                }

                CTEFreeLock(&RouteTableLock, TableLock);
                return Src;
            } else {
                // We have an existing RCE. We'll return his source as the
                // valid source, bump the reference count, free the locks
                // and return.
                CTEGetLock(&OldRCE->rce_lock, &RCEHandle);
                OldRCE->rce_cnt++;
                *RCE = OldRCE;
                CTEFreeLock(&OldRCE->rce_lock, RCEHandle);
                CTEFreeLock(&RouteTableLock, TableLock);
                return Src;
            }
        } else {
            CTEFreeLock(&RouteTableLock, TableLock);
            return NULL_IP_ADDR;
        }
    }

	CTEFreeLock(&RouteTableLock, TableLock);
    return NULL_IP_ADDR;
}

//* CloseRCE - Close an RCE.
//
//  Called by the upper layer when it wants to close the RCE. We unlink it from
//  the RTE.
//
//  Entry:  RCE     - Pointer to the RCE to be closed.
//
//  Exit: Nothing.
//
void
CloseRCE(RouteCacheEntry *RCE)
{
    RouteTableEntry     *RTE;               // Route on which RCE is linked.
    RouteCacheEntry     *PrevRCE;
    CTELockHandle       TableLock; 			// Lock handles used.
    CTELockHandle       RCEHandle;
    Interface           *IF;

    if (RCE != NULL) {
        CTEGetLock(&RouteTableLock, &TableLock);
        CTEGetLock(&RCE->rce_lock, &RCEHandle);

        if (--RCE->rce_cnt == 0) {
			CTEAssert(RCE->rce_usecnt == 0);
        	if (RCE->rce_flags & RCE_VALID) {
            	// The RCE is valid, so we have a valid RTE in the pointer
            	// field. Walk down the RTE rcelist, looking for this guy.
			
				RTE = RCE->rce_rte;
				IF = RTE->rte_if;

            	PrevRCE = STRUCT_OF(RouteCacheEntry, &RTE->rte_rcelist,
            		rce_next);

            	// Walk down the list until we find him.
            	while (PrevRCE != NULL) {
                	if (PrevRCE->rce_next == RCE)
                    	break;
                	PrevRCE = PrevRCE->rce_next;
            	}

				CTEAssert(PrevRCE != NULL);
            	PrevRCE->rce_next = RCE->rce_next;
			} else
				IF = (Interface *)RCE->rce_rte;

			if (RCE->rce_flags & RCE_CONNECTED) {
				(*(IF->if_invalidate))(IF->if_lcontext, RCE);
			}

#ifdef _PNP_POWER
			if (RCE->rce_flags & RCE_REFERENCED) {
				LockedDerefIF(IF);
			}
#endif
            CTEFreeLock(&RCE->rce_lock, RCEHandle);
            CTEFreeMem(RCE);
        }
		else {
            CTEFreeLock(&RCE->rce_lock, RCEHandle);
        }

        CTEFreeLock(&RouteTableLock, TableLock);

    }

}


//* LockedAddRoute - Add a route to the routing table.
//
//  Called by AddRoute to add a route to the routing table. We assume the
//	route table lock is already held. If the route to be added already exists
//  we update it. Routes are identified by a (Destination, Mask, FirstHop,
//	Interface) tuple. If an exact match exists we'll update the metric, which
//	may cause us to promote RCEs from other RTEs, or we may be demoted in which
//	case we'll invalidate our RCEs and let them be reassigned at transmission
//	time.
//
//	If we have to create a new RTE we'll do so, and find the best previous
//	RTE, and promote RCEs from that one to the new one.
//
//	The route table is an open hash structure. Within each hash chain the
//	RTEs with the longest masks (the 'priority') come first, and within
//	each priority the RTEs with the smallest metric come first.
//
//
//  Entry:  Destination     - Destination address for which route is being
//								added.
//			Mask			- Mask for destination.
//			FirstHop		- First hop for address. Could be IPADDR_LOCAL.
//			OutIF			- Pointer to outgoing I/F.
//			MTU				- Maximum MTU for this route.
//			Metric			- Metric for this route.
//			Proto			- Protocol type to store in route.
//			AType			- Administrative type of route.
//
//  Returns: Status of attempt to add route.
//
IP_STATUS
LockedAddRoute(IPAddr Destination, IPMask Mask, IPAddr FirstHop,
	Interface *OutIF, uint MTU, uint Metric, uint Proto, uint AType,
	ROUTE_CONTEXT Context)
{
	uint				RouteType;			// SNMP route type.
	RouteTableEntry		*NewRTE, *OldRTE;	// Entries for new and previous
											// RTEs.
	RouteTableEntry		*PrevRTE;			// Pointer to previous RTE.
	CTELockHandle		RCEHandle;			// Lock handle for RCEs.
	uint				OldMetric;			// Previous metric in use.
	uint				OldPriority;		// Priority of previous route to
											// destination.
	RouteCacheEntry		*CurrentRCE;		// Current RCE being examined.
	RouteCacheEntry		*PrevRCE;			// Previous RCE examined.
	Interface			*IF;				// Interface being added on.
	uint				Priority;			// Priority of the route.
	uint				TempMask;			// Temporary copy of the mask.
	uint				Now = CTESystemUpTime() / 1000L;	// System up time,
											// in seconds.
	uint				MoveAny;			// TRUE if we'll move any RCE.
	ushort				OldFlags;

	// First do some consistency checks. Make sure that the Mask and
	// Destination agree.
	if (!IP_ADDR_EQUAL(Destination & Mask, Destination))
		return IP_BAD_DESTINATION;
	
	if (AType != ATYPE_PERM && AType != ATYPE_OVERRIDE && AType != ATYPE_TEMP)
		return IP_BAD_REQ;

#ifdef _PNP_POWER
	// If the interface is marked as going away, fail this.
	if (OutIF->if_flags & IF_FLAGS_DELETING) {
		return IP_BAD_REQ;
	}
#endif

	RouteType = IP_ADDR_EQUAL(FirstHop, IPADDR_LOCAL) ? IRE_TYPE_DIRECT :
		IRE_TYPE_INDIRECT;


	MTU = MAX(MTU, MIN_VALID_MTU);

	// If the outgoing interface has NTEs attached but none are valid, fail
	// this request unless it's a request to add the broadcast route.
	if (OutIF != (Interface *)&DummyInterface) {
		if (OutIF->if_ntecount == 0 && OutIF->if_nte != NULL &&
			!IP_ADDR_EQUAL(Destination, OutIF->if_bcast) ) {
			// This interface has NTEs attached, but none are valid. Fail the
			// request.
			return IP_BAD_REQ;
		}
	}
		

	// First look and see if the RTE already exists.
	NewRTE = FindSpecificRTE(Destination, Mask, FirstHop, OutIF, &PrevRTE);

	if (NewRTE != NULL) {
		
		// The RTE already exists, and we have a lock on it. See if we're
		// updating the metric. If we're not, just return. Otherwise
		// we'll remove the RTE and update the metric. If the metric is
		// increasing, walk down the RCE chain on the RTE and invalidate
		// the RCE back pointers so that they'll be revalidated upon
		// transmits, and then reinsert the RTE. If the metric is
		// decreasing we'll just fall through to the case below where we'll
		// promote RCEs and insert the RTE.


		// Update the things that won't cause routing table motion.
		NewRTE->rte_mtu = MTU;
		NewRTE->rte_proto = Proto;
		NewRTE->rte_valid = Now;
		NewRTE->rte_mtuchange = Now;
		NewRTE->rte_context = Context;
		OldFlags = NewRTE->rte_flags;
	
		// We always turn off the increase flag when a route is updated, so
		// that we take the long timeout to try to increase it. We also
		// always turn on the valid flag.
		NewRTE->rte_flags = (OldFlags & ~RTE_INCREASE) | RTE_VALID;
		if (OutIF != (Interface *)&DummyInterface) {
			NewRTE->rte_flags |= RTE_IF_VALID;
		} else {

            //
            // Invalidating a previously valid route
            //

            NewRTE->rte_flags &= ~RTE_IF_VALID;
        }
		
		// If the RTE is for a default gateway and the old flags indicate
		// he wasn't valid then we're essentially creating a new active
		// default gateway. In the case bump the active default gateway count.
		if (NewRTE->rte_mask == DEFAULT_MASK && !(OldFlags & RTE_VALID))
			DefGWActive++;

		// Need to update the metric, which will cause this RTE to move
		// in the table.
		OldMetric = NewRTE->rte_metric;			// Save the old one.
		RemoveRTE(PrevRTE, NewRTE);				// Pull him from the chain.
		NewRTE->rte_metric = Metric;

		// Now see if we're increasing or decreasing the metric, or
		// re-validating a previously invalid route. By definition, if the
		// route wasn't valid before this there can't have been any RCEs
		// attached to it, so we'll fall through to the promotion code
		// and see if we can move any onto it. Otherwise, if we're
		// demoting this route invalidate any RCEs on it.
		
		if (Metric >= OldMetric && (OldFlags & RTE_VALID)) {
			// We're increasing it, or leaving it the same. His valid state
			// may have changed, so we'll we'll invalidate any RCEs on him
			// now in any case.
			InsertRTE(NewRTE);
			
			InvalidateRCEChain(NewRTE);
			
            //
            // Whether the IF has changed or not, we can always overwrite it
            // We wait till here to overwrite because if old rte_if was
            // not dummy if, then we will want to invalidate RCEChain
            // Since the invalidate function is stored in the interface
            // structure we want to keep it around
            //

            NewRTE->rte_if = OutIF;

			return IP_SUCCESS;
		}


        NewRTE->rte_if = OutIF;

		// The metric is less than the old metric, so we're promoting this
		// RTE. Fall through to the code below that deals with this, after
		// saving the priority of the RTE.
		Priority = NewRTE->rte_priority;

	} else {
		
		// Didn't find a matching RTE. Allocate a new one, and fill it in.
		NewRTE = CTEAllocMem(sizeof(RouteTableEntry));
		
		if (NewRTE == NULL) {
			// Couldn't get the memory.
			return IP_NO_RESOURCES;
		}

		IPSInfo.ipsi_numroutes++;

		// Fill him in, and take the lock on him. To do this we'll need to
		// calculate the priority.
		Priority = 0;
		TempMask = Mask;
		
		while (TempMask) {
			Priority += TempMask & 1;
			TempMask >>= 1;
		}

		// Now initialize all of his fields.
		NewRTE->rte_priority = Priority;

		NewRTE->rte_dest = Destination;
		NewRTE->rte_mask = Mask;
		if (Mask == DEFAULT_MASK) {
			// We're adding a default route.
			DefGWConfigured++;
			DefGWActive++;
		}
		NewRTE->rte_addr = FirstHop;
		NewRTE->rte_metric = Metric;
		NewRTE->rte_mtu = MTU;
		NewRTE->rte_if = OutIF;
		NewRTE->rte_rcelist = NULL;
		NewRTE->rte_type = (ushort)RouteType;
		NewRTE->rte_flags = RTE_VALID;
		if (OutIF != (Interface *)&DummyInterface) {
			NewRTE->rte_flags |= RTE_IF_VALID;
		}
		NewRTE->rte_proto = Proto;
		NewRTE->rte_valid = Now;
		NewRTE->rte_mtuchange = Now;
		NewRTE->rte_admintype = AType;
		NewRTE->rte_context = Context;
	}

	// At this point, NewRTE points to an initialized RTE that is not in the
	// table. We hold the lock on NewRTE and on the RouteTable. First we'll
	// find where we'll eventually insert the new RTE (we can't insert it
	// yet, because we'll still need to search the table again and we can't
	// do that while we hold a lock on an element in the table). Then we'll
	// search the table for the next best route (i.e. a route with a priority
	// less than or equal to ours), and if we find one we'll promote RCEs from
	// that route to us. We'll actually have to search a chain of RTEs.
	
	PrevRTE = FindInsertPoint(NewRTE);
	OldRTE = LookupRTE(Destination, NULL_IP_ADDR, Priority);

	// If we found one, try to promote from him.
	if (OldRTE != NULL) {
		
		// Found another RTE, and we have the lock on it. Starting with him,
		// walk down the chain. At the start of this loop we know that the
		// route described by OldRTE can reach our destination. If his metric
		// is better than ours, we're done and we'll just insert our route.
		// If his priority and metric are equal to ours we'll promote only
		// those RCEs that exactly match our source address. Otherwise either
		// his priority or metric is worse than ours, and we'll promote all
		// appropriate RCEs.
		//
		// Since we specified the maximum priority in the call to LookupRTE
		// as our priority, we know the priority of the route we found
		// can't be greater than our priority.
		
		OldMetric = OldRTE->rte_metric;
		OldPriority = OldRTE->rte_priority;

		// We'll do the search if his priority is less than ours, or his
		// metric is > (i.e. worse than) our metric, or his metric equals
		// our metric and the old route is on a different interface. We
		// know OldPriority is <= our Priority, since we specified our
		// priority in the call to LookupRTE above.
		
		CTEAssert(OldPriority <= Priority);
		
		if (OldPriority < Priority || OldMetric > Metric ||
			(OldMetric == Metric && OldRTE->rte_if != OutIF)) {
			
			// We're going to search. Figure out the mask to use in comparing
			// source addresses.
			if (OldPriority == Priority && OldMetric == Metric)
				MoveAny = FALSE;		// Only promote an exact match.
			else
				MoveAny = TRUE;			// Promote any match.

			for (;;) {
				IF = OldRTE->rte_if;

				PrevRCE = STRUCT_OF(RouteCacheEntry, &OldRTE->rte_rcelist,
					rce_next);
				CurrentRCE = PrevRCE->rce_next;
				// Walk the list, promoting any that match.
				while (CurrentRCE != NULL) {
					CTEGetLock(&CurrentRCE->rce_lock, &RCEHandle);
					
					// If the masked source address matches our masked address,
					// and the destinations match, promote him.
					if ((MoveAny || AddrOnIF(OutIF, CurrentRCE->rce_src)) &&
						IP_ADDR_EQUAL(CurrentRCE->rce_dest & Mask, Destination)) {
						// He matches. Pull him from the list and mark him as invalid.
						// This will force a new lookup of the route next time he's
						// used, which as a side effect will cause the route to be
						// connected in the dial-on-demand case.
						CTEAssert(CurrentRCE->rce_flags & RCE_VALID);
						PrevRCE->rce_next = CurrentRCE->rce_next;
						CurrentRCE->rce_flags &= ~RCE_VALID;
						CurrentRCE->rce_rte = (RouteTableEntry *)IF;
						if (CurrentRCE->rce_usecnt == 0) {
							// No one's currently using him, so invalidate him.
							if (CurrentRCE->rce_flags & RCE_CONNECTED) {
								(*(IF->if_invalidate))(IF->if_lcontext, CurrentRCE);
#ifdef _PNP_POWER
								if (CurrentRCE->rce_flags & RCE_REFERENCED) {
									LockedDerefIF(IF);
									CurrentRCE->rce_flags &= ~RCE_REFERENCED;
								}
#endif
							} else {
#ifdef _PNP_POWER
								CTEAssert(!(CurrentRCE->rce_flags & RCE_REFERENCED));
#endif
							}
						}
						
					} else {
						// Doesn't match. Try the next one.
						PrevRCE = CurrentRCE;
					}
					CTEFreeLock(&CurrentRCE->rce_lock, RCEHandle);
					CurrentRCE = PrevRCE->rce_next;
				}

				// We've walked the RCE list on that old RTE. Look at the
				// next one. If it has the same priority and metric as the
				// old one, and also matches our destination, check it also. We
				// don't need to RTEs that don't match this criteria, since we know
				// RCEs are always kept on the 'best' RTE.
				OldRTE = OldRTE->rte_next;
				if (OldRTE != NULL) {
					// We have another one. Check it out.
					if (OldRTE->rte_priority == Priority &&
						OldRTE->rte_metric == OldMetric &&
						IP_ADDR_EQUAL(Destination & OldRTE->rte_mask,
							OldRTE->rte_dest))
						continue;			// It matches, so try to promote
											// RCEs.
				}
				break;	// Exit out of the for (;;) loop.
			}
		} else {
			// OldRTE is a better route than the one we're inserting, so don't
			// do anything.
		}
	}

	// At this point we're promoted any routes we need to, we hold the lock
	// on NewRTE which still isn't inserted, and PrevRTE describes where to
	// insert NewRTE. Insert it, free the lock, and return success.
	InsertAfterRTE(PrevRTE, NewRTE);
	return IP_SUCCESS;

}

//* AddRoute - Add a route to the routing table.
//
//	This is just a shell for the real add route routine. All we do is take
//	the route table lock, and call the LockedAddRoute routine to deal with
//	the request. This is done this way because there are certain routines that
//	need to be able to atomically examine and add routes.
//
//  Entry:  Destination     - Destination address for which route is being
//								added.
//			Mask			- Mask for destination.
//			FirstHop		- First hop for address. Could be IPADDR_LOCAL.
//			OutIF			- Pointer to outgoing I/F.
//			MTU				- Maximum MTU for this route.
//			Metric			- Metric for this route.
//			Proto			- Protocol type to store in route.
//			AType			- Administrative type of route.
//			Context 		- Context for this route.
//
//  Returns: Status of attempt to add route.
//
IP_STATUS
AddRoute(IPAddr Destination, IPMask Mask, IPAddr FirstHop,
	Interface *OutIF, uint MTU, uint Metric, uint Proto, uint AType,
	ROUTE_CONTEXT Context)
{
	CTELockHandle			TableHandle;
	IP_STATUS				Status;
	
	CTEGetLock(&RouteTableLock, &TableHandle);
	Status = LockedAddRoute(Destination, Mask, FirstHop, OutIF, MTU, Metric,
		Proto, AType, Context);
	
	CTEFreeLock(&RouteTableLock, TableHandle);
	return Status;
}

//* DeleteRoute - Delete a route from the routing table.
//
//  Called by upper layer or management code to delete a route from the routing
//	table. If we can't find the route we return an error. If we do find it, we
//	remove it, and invalidate any RCEs associated with it. These RCEs will be
//	reassigned the next time they're used. A route is uniquely identified by
//	a (Destination, Mask, FirstHop, Interface) tuple.
//
//  Entry:  Destination     - Destination address for which route is being
//								deleted.
//			Mask			- Mask for destination.
//          FirstHop        - First hop on way to Destination. -1 means route is
//								local.
//			OutIF			- Outgoing interface for route.
//
//  Returns: Status of attempt to delete route.
//
IP_STATUS
DeleteRoute(IPAddr Destination, IPMask Mask, IPAddr FirstHop, Interface *OutIF)
{
    RouteTableEntry *RTE;                   // RTE being deleted.
    RouteTableEntry *PrevRTE;               // Pointer to RTE in front of one
    										// being deleted.
    CTELockHandle   TableLock;              // Lock handle for table.


    // Look up the route by calling FindSpecificRTE. If we can't find it,
	// fail the call.
    CTEGetLock(&RouteTableLock, &TableLock);
	RTE = FindSpecificRTE(Destination, Mask, FirstHop, OutIF, &PrevRTE);

	if (RTE == NULL) {
		// Didn't find the route, so fail the call.
		CTEFreeLock(&RouteTableLock, TableLock);
		return IP_BAD_ROUTE;
	}

#ifndef NT
//
// Disable admin check for NT, because the RAS server needs to be able to
// delete a subnet route. This ability is restricted to Administrators only
// by NT security checks.
//
//
	if (RTE->rte_admintype == ATYPE_PERM) {
		// Can't delete a permanent route.
		CTEFreeLock(&RouteTableLock, TableLock);
		return IP_BAD_REQ;
	}
#endif
	
	// When we reach here we hold the lock on the RTE to be deleted, and
	// PrevRTE points to the RTE immediately ahead of ours in the table.
	// Call DeleteRTE to delete him.
	DeleteRTE(PrevRTE, RTE);

    CTEFreeLock(&RouteTableLock, TableLock);
    return IP_SUCCESS;


}

//** Redirect - Process a redirect request.
//
// This is the redirect handler . We treat all redirects as host redirects as per the
// host requirements RFC. We make a few sanity checks on the new first hop address, and then
// we look up the current route. If it's not through the source of the redirect, just return.
// If the current route to the destination is a host route, update the first hop and return.
// If the route is not a host route, remove any RCE for this route from the RTE, create a
// host route and place the RCE (if any) on the new RTE.
//
//  Entry:  NTE         - Pointer to NetTableEntry for net on which Redirect
//							arrived.
//          RDSrc       - IPAddress of source of redirect.
//          Target      - IPAddress being redirected.
//          Src         - Src IP address of DG that triggered RD.
//          FirstHop    - New first hop for Target.
//
//  Returns: Nothing.
//
void
Redirect(NetTableEntry *NTE, IPAddr RDSrc, IPAddr Target, IPAddr Src,
	IPAddr FirstHop)
{
	uint				MTU;
	RouteTableEntry		*RTE;
	CTELockHandle		Handle;

    if (IP_ADDR_EQUAL(FirstHop, NULL_IP_ADDR) || IP_LOOPBACK(FirstHop))
        return;                                 // Can't redirect to loopback
        										// address.

	CTEAssert(IP_ADDR_EQUAL(NTE->nte_addr, Src));

    // First make sure that this came from the gateway we're currently using to
    // get to Target, and then lookup up the route to the new first hop. The new
    // firsthop must be directly reachable, and on the same subnetwork or
    // physical interface on which we received the redirect.


	CTEGetLock(&RouteTableLock, &Handle);
	
	// Make sure the source of the redirect is the current first hop gateway.
    RTE = LookupRTE(Target, Src, HOST_ROUTE_PRI);
	if (RTE == NULL || IP_ADDR_EQUAL(RTE->rte_addr, IPADDR_LOCAL) ||
		!IP_ADDR_EQUAL(RTE->rte_addr, RDSrc)) {
		CTEFreeLock(&RouteTableLock, Handle);
		return;						// A bad redirect.
	}

	CTEAssert(RTE->rte_flags & RTE_IF_VALID);
	
	// If the current first hop gateway is a default gateway, see if we have
	// another default gateway at FirstHop that is down. If so, mark him as
	// up and invalidate the RCEs on this guy.
	if (RTE->rte_mask == DEFAULT_MASK && ValidateDefaultGWs(FirstHop) != 0) {
		// Have a default gateway that's been newly activated. Invalidate RCEs
		// on the route, and we're done.
		InvalidateRCEChain(RTE);
		CTEFreeLock(&RouteTableLock, Handle);
		return;
	}
		
	// We really need to add a host route through FirstHop. Make sure he's
	// a valid first hop.
	RTE = LookupRTE(FirstHop, Src, HOST_ROUTE_PRI);
    if (RTE == NULL) {
		CTEFreeLock(&RouteTableLock, Handle);
        return;                                 // Can't get there from here.
	}

	CTEAssert(RTE->rte_flags & RTE_IF_VALID);


    // Check to make sure the new first hop is directly reachable, and is on the
    // same subnet or physical interface we received the redirect on.
    if (!IP_ADDR_EQUAL(RTE->rte_addr, IPADDR_LOCAL) ||	// Not directly reachable
														// or wrong subnet.
         ((NTE->nte_addr & NTE->nte_mask) != (FirstHop & NTE->nte_mask))) {
		CTEFreeLock(&RouteTableLock, Handle);
        return;
	}

	MTU = RTE->rte_mtu;

    // Now add a host route. AddRoute will do the correct things with shifting
    // RCEs around. We know that FirstHop is on the same subnet as NTE (from
	// the check above), so it's valid to add the route to FirstHop as out
	// going through NTE.
	LockedAddRoute(Target, HOST_MASK, IP_ADDR_EQUAL(FirstHop, Target) ? IPADDR_LOCAL :
		FirstHop, NTE->nte_if, MTU, 1, IRE_PROTO_ICMP, ATYPE_OVERRIDE,
		RTE->rte_context);

	CTEFreeLock(&RouteTableLock, Handle);
}

//* GetRaisedMTU - Get the next largest MTU in table..
//
//	A utility function to search the MTU table for a larger value.
//
//	Input:	PrevMTU				- MTU we're currently using. We want the
//									next largest one.
//
//	Returns: New MTU size.
//
uint
GetRaisedMTU(uint PrevMTU)
{
	uint			i;
	
	for (i = (sizeof(MTUTable)/sizeof(uint)) - 1; i != 0; i--) {
		if (MTUTable[i] > PrevMTU)
			break;
	}
	
	return MTUTable[i];
}

//* GuessNewMTU - Guess a new MTU, giving a DG size too big.
//
//	A utility function to search the MTU table. As input we take in an MTU
//	size we believe to be too large, and search the table looking for the
//	next smallest one.
//
//	Input:	TooBig				- Size that's too big.
//
//	Returns: New MTU size.
//
uint
GuessNewMTU(uint TooBig)
{
	uint			i;
	
	for (i = 0; i < ((sizeof(MTUTable)/sizeof(uint)) - 1); i++)
		if (MTUTable[i] < TooBig)
			break;
	
	return MTUTable[i];
}

//*	RouteFragNeeded - Handle being told we need to fragment.
//
//	Called when we receive some external indication that we need to fragment
//	along a particular path. If we're doing MTU discovery we'll try to
//	update the route, if we can. We'll also notify the upper layers about
//	the new MTU.
//
//	Input:	IPH				- Pointer to IP Header of datagram needing
//								fragmentation.
//			NewMTU			- New MTU to be used (may be 0).
//
//	Returns: Nothing.
//
void
RouteFragNeeded(IPHeader UNALIGNED *IPH, ushort NewMTU)
{
	uint			OldMTU;
	CTELockHandle	Handle;
	RouteTableEntry	*RTE;
	ushort			HeaderLength;	
	
	// If we're not doing PMTU discovery, don't do anything.
	if (PMTUDiscovery) {
		
		// We're doing PMTU discovery. Correct the given new MTU for the IP
		// header size, which we don't save as we track MTUs.
		if (NewMTU != 0) {
			// Make sure the new MTU we got is at least the minimum valid size.
			NewMTU = MAX(NewMTU, MIN_VALID_MTU);
			NewMTU -= sizeof(IPHeader);
        }
			
		HeaderLength = (IPH->iph_verlen & (uchar)~IP_VER_FLAG) << 2;
		
		// Get the current routing information.

		CTEGetLock(&RouteTableLock, &Handle);
	
		// Find an RTE for the destination.
    	RTE = LookupRTE(IPH->iph_dest, IPH->iph_src, HOST_ROUTE_PRI);
		
		// If we couldn't find one, or the existing MTU is less than the new
		// MTU, give up now.
		
		if (RTE == NULL || (OldMTU = RTE->rte_mtu) < NewMTU) {
			// No RTE, or an invalid new MTU. Just bail out now.
			CTEFreeLock(&RouteTableLock, Handle);
			return;
		}
		
		// If the new MTU is zero, figure out what the new MTU should be.
		if (NewMTU == 0) {
			ushort			DGLength;
			
			// The new MTU is zero. We'll make a best guess what the new
			// MTU should be. We have the RTE for this route already.
			
			
			// Get the length of the datagram that triggered this. Since we'll
			// be comparing it against MTU values that we track without the
			// IP header size included, subtract off that amount.
			DGLength = (ushort)net_short(IPH->iph_length) - sizeof(IPHeader);
			
			// We may need to correct this as per RFC 1191 for dealing with
			// old style routers.
			if (DGLength >= OldMTU) {
				// The length of the datagram sent is not less than our
				// current MTU estimate, so we need to back it down (assuming
				// that the sending route has incorrectly added in the header
				// length).
				DGLength -= HeaderLength;
				
			}
			
			// If it's still larger than our current MTU, use the current
			// MTU. This could happen if the upper layer sends a burst of
			// packets which generate a sequence of ICMP discard messages. The
			// first one we receive will cause us to lower our MTU. We then
			// want to discard subsequent messages to avoid lowering it
			// too much. This could conceivably be a problem if our
			// first adjustment still results in an MTU that's too big,
			// but we should converge adequately fast anyway, and it's
			// better than accidentally underestimating the MTU.
			
			if (DGLength > OldMTU)
				NewMTU = OldMTU;
			else
				// Move down the table to the next lowest MTU.
				NewMTU = GuessNewMTU(DGLength);
		}
		
		// We have the new MTU. Now add it to the table as a host route.
		if (NewMTU != OldMTU)
			LockedAddRoute(IPH->iph_dest, HOST_MASK, RTE->rte_addr, RTE->rte_if,
				NewMTU, RTE->rte_metric, IRE_PROTO_ICMP, ATYPE_OVERRIDE,
				RTE->rte_context);
			
		CTEFreeLock(&RouteTableLock, Handle);
		
		// We've added the route. Now notify the upper layers of the change.
		ULMTUNotify(IPH->iph_dest, IPH->iph_src, IPH->iph_protocol,
			(void *)((uchar *)IPH + HeaderLength), NewMTU);

	}
}

//** IPRouteTimeout - IP routeing timeout handler.
//
//  The IP routeing timeout routine, called once a minute. We look at all
//	host routes, and if we raise the MTU on them we do so.
//
//  Entry:  Timer       - Timer being fired.
//          Context     - Pointer to NTE being time out.
//
//  Returns: Nothing.
//
void
IPRouteTimeout(CTEEvent *Timer, void *Context)
{
	uint			Now = CTESystemUpTime() / 1000L;
	CTELockHandle	Handle;
	uint			i;
	RouteTableEntry	*RTE, *PrevRTE;
	uint			RaiseMTU, Delta;
	Interface		*IF;
	IPAddr			Dest;
	uint			NewMTU;
	NetTableEntry	*NTE;
	
	CTEGetLock(&RouteTableLock, &Handle);
	
	for (i = 0; i < ROUTE_TABLE_SIZE; i++) {
		// Walk down each chain, looking at the host routes. If we're
		// doing PMTU discovery, see if we can raise the MTU.
		PrevRTE = STRUCT_OF(RouteTableEntry, &RouteTable[i], rte_next);
		RTE = RouteTable[i];
		while (RTE != NULL && RTE->rte_mask == HOST_MASK) {
			// Make sure he's valid.
			if (RTE->rte_flags & RTE_VALID) {
				if (PMTUDiscovery) {
					// Check to see if we can raise the MTU on this guy.
					Delta = Now - RTE->rte_mtuchange;
					
					if (RTE->rte_flags & RTE_INCREASE)
						RaiseMTU = (Delta >= MTU_INCREASE_TIME ? 1 : 0);
					else
						RaiseMTU = (Delta >= MTU_DECREASE_TIME ? 1 : 0);
					
					if (RaiseMTU) {
						// We need to raise this MTU. Set his change time to
						// Now, so we don't do this again, and figure out
						// what the new MTU should be.
						RTE->rte_mtuchange = Now;
						IF = RTE->rte_if;
						if (RTE->rte_mtu < IF->if_mtu) {
							
							RTE->rte_flags |= RTE_INCREASE;
							// This is a candidate for change. Figure out
							// what it should be.
							NewMTU = MIN(GetRaisedMTU(RTE->rte_mtu),
									IF->if_mtu);
							RTE->rte_mtu = NewMTU;
							Dest = RTE->rte_dest;
							
							// We have the new MTU. Free the lock, and walk
							// down the NTEs on the I/F. For each NTE,
							// call up to the upper layer and tell him what
							// his new MTU is.
							CTEFreeLock(&RouteTableLock, Handle);
							NTE = IF->if_nte;
							while (NTE != NULL) {
								if (NTE->nte_flags & NTE_VALID) {
									ULMTUNotify(Dest, NTE->nte_addr, 0, NULL,
										MIN(NewMTU, NTE->nte_mss));
								}
								NTE = NTE->nte_ifnext;
							}
							
							// We've notified everyone. Get the lock again,
							// and start from the first element of this
							// chain in case something's changed after we
							// free the lock. We've updated the mtuchange
							// time of this RTE, so we won't hit him again.									
							CTEGetLock(&RouteTableLock, &Handle);
							PrevRTE = STRUCT_OF(RouteTableEntry, &RouteTable[i],
								rte_next);
							RTE = RouteTable[i];
							continue;
						} else
							RTE->rte_flags &= ~RTE_INCREASE;
					}
				}
				// If this route came in via ICMP, and we have no RCEs on it,
				// and it's at least 10 minutes old, delete it.
				if (RTE->rte_proto == IRE_PROTO_ICMP &&
					RTE->rte_rcelist == NULL &&
					(Now - RTE->rte_valid) > MAX_ICMP_ROUTE_VALID) {
						// He needs to be deleted. Call DeleteRTE to do this.
						DeleteRTE(PrevRTE, RTE);
						RTE = PrevRTE->rte_next;
						continue;
				}
			}
			PrevRTE = RTE;
			RTE = RTE->rte_next;
		}
	}
	
	CTEFreeLock(&RouteTableLock, Handle);
	CTEStartTimer(&IPRouteTimer, IP_ROUTE_TIMEOUT, IPRouteTimeout, NULL);

}

//* FreeFWPacket - Free a forwarding packet when we're done with it.
//
//
//  Input:  Packet  - Packet to be freed.
//
//  Returns: Nothing.
//
void
FreeFWPacket(PNDIS_PACKET Packet)
{
    CTELockHandle   Handle;
    FWContext       *FWC;

//  BUGBUG - Portability issue

#ifdef VXD
    Packet->Private.Head = (PNDIS_BUFFER)NULL;
    Packet->Private.Count = 0;
    Packet->Private.PhysicalCount = 0;
    Packet->Private.TotalLength = 0;
#else // VXD
#ifdef NT
    //
    // BUGBUG: This is inefficient. Need something better.
    //
    NdisReinitializePacket(Packet);
#else // NT
#error Need portable way to do this.
#endif // NT
#endif // VXD

    FWC = (FWContext *)Packet->ProtocolReserved;
    if (FWC->fc_options) {
        CTEFreeMem(FWC->fc_options);
        FWC->fc_options = (uchar *)NULL;
    }

    if (FWC->fc_buffhead) {
        CTEGetLock(&FWBufFreeLock, &Handle);
        FWC->fc_bufftail->Next = FWBufFree;         // BUGBUG more portable.
        FWBufFree = FWC->fc_buffhead;
        CTEFreeLock(&FWBufFreeLock, Handle);
        FWC->fc_buffhead = (PNDIS_BUFFER)NULL;
    }
#ifdef _PNP_POWER
	// If there's an interface pointer here, dereference in now.
	if (FWC->fc_if != NULL) {
		DerefIF(FWC->fc_if);
		FWC->fc_if = NULL;
	}
#endif

    CTEGetLock(&FWPacketFreeLock, &Handle);
    FWC->fc_pc.pc_common.pc_link = FWPacketFree;
    FWPacketFree = Packet;
    CTEFreeLock(&FWPacketFreeLock, Handle);

}

//* GrowFWPackets - Grow the FW packet list, if we can.
//
//	Called when we need to allocate a FW packet, but don't have one. We'll try to grow the
//	FWPacket list now.
//
//	Input: Nothing.
//
//	Returns: TRUE if we succeeded in growing the list, FALSE otherwise.
//
uint
GrowFWPackets(void)
{
	CTELockHandle		Handle;
	IPHeader			*HeaderPtr;
	NDIS_HANDLE			BufferPool;
	NDIS_HANDLE			PacketPool;
	PNDIS_BUFFER		Buffer;
	PNDIS_PACKET		Packet;
	NDIS_STATUS			Status;
	uint				i;
	uint				AmountToGrow;

	CTEGetLock(&FWPacketFreeLock, &Handle);

	AmountToGrow = MIN(MaxFWPackets - CurrentFWPackets, FWPACKET_GROW_AMOUNT);
	HeaderPtr = NULL;

	if (AmountToGrow != 0) {

		// We have room to grow yet, so try to. First get the memory for our header buffers.
		HeaderPtr = CTEAllocMem(AmountToGrow * sizeof(IPHeader));
		if (HeaderPtr == (IPHeader *)NULL)
			goto failure;			// Couldn't get it.

		// Now try to get NDIS buffers for the headers.
		NdisAllocateBufferPool(&Status, &BufferPool, AmountToGrow);
		if (Status != NDIS_STATUS_SUCCESS) {
			goto failure;
		}

		// Now try to get the packets themselves.
		NdisAllocatePacketPool(&Status, &PacketPool, AmountToGrow, sizeof(FWContext));
		if (Status != NDIS_STATUS_SUCCESS) {
			NdisFreeBufferPool(BufferPool);
			goto failure;
		}

		// Since we have everything we need, update the current count.
		CurrentFWPackets += AmountToGrow;

		CTEFreeLock(&FWPacketFreeLock, Handle);

		// We got the resources we need. Loop through and put them on.
        for (i = 0; i < AmountToGrow; i++) {
            FWContext       *FWC;

            NdisAllocateBuffer(&Status, &Buffer, BufferPool, HeaderPtr,
            	sizeof(IPHeader));
            if (Status != NDIS_STATUS_SUCCESS)
                CTEAssert(FALSE);
            NdisAllocatePacket(&Status, &Packet, PacketPool);
            if (Status != NDIS_STATUS_SUCCESS)
                CTEAssert(FALSE);

            CTEMemSet(Packet->ProtocolReserved, 0, sizeof(FWContext));
            FWC = (FWContext *)Packet->ProtocolReserved;
            FWC->fc_hndisbuff = Buffer;
            FWC->fc_hbuff = HeaderPtr;
            FWC->fc_pc.pc_common.pc_flags = PACKET_FLAG_FW;
            FWC->fc_pc.pc_common.pc_owner = PACKET_OWNER_IP;
            FWC->fc_pc.pc_pi = RtPI;
            FWC->fc_pc.pc_context = Packet;

            FreeFWPacket(Packet);
            HeaderPtr++;
        }
		return TRUE;
	}

failure:
	CTEFreeLock(&FWPacketFreeLock, Handle);
	if (HeaderPtr != NULL) {
		CTEFreeMem(HeaderPtr);
	}
	return FALSE;
}

//* GrowFWBuffer - Grow the FW buffer pool, if we can.
//
//	Called when we need to grow the FW buffer pool. We'll grow it up to the maximum size
//	specified by the user.
//
//	Input: Nothing.
//
//	Returns: TRUE if we succeeded in growing the pool, FALSE otherwise.
//
uint
GrowFWBuffer(void)
{
	CTELockHandle		Handle;
	uint				AvailableBufferSpace;
	uint				NewBufferCount;
	uint				i;
	uchar				*BufferPtr = NULL;
	NDIS_STATUS			Status;
	PNDIS_BUFFER		Buffer;
	NDIS_HANDLE			BufferPool;

	CTEGetLock(&FWPacketFreeLock, &Handle);
	AvailableBufferSpace = MIN(MaxFWBufferSize - CurrentFWBufferSize, FW_BUF_GROW_AMOUNT);

	// If we have room to grow, do so.
	if (AvailableBufferSpace >= FW_BUF_SIZE) {
		// We have room to grow the buffer, so do so. First, round to a multiple of our
		// FW buffer size.
		NewBufferCount = AvailableBufferSpace / FW_BUF_SIZE;
		AvailableBufferSpace = NewBufferCount * FW_BUF_SIZE;

		// Allocate the resources we need.
		BufferPtr = CTEAllocMem(AvailableBufferSpace);
		if (BufferPtr == NULL) {
			goto failure;
		}

		NdisAllocateBufferPool(&Status, &BufferPool, NewBufferCount);
        if (Status != NDIS_STATUS_SUCCESS) {
			goto failure;
		}

		// We got what we needed. Now loop through and put them on the list.
        for (i = 0; i < NewBufferCount; i++) {
            NdisAllocateBuffer(&Status, &Buffer, BufferPool, BufferPtr, FW_BUF_SIZE);
            if (Status != NDIS_STATUS_SUCCESS)
                CTEAssert(FALSE);

            Buffer->Next =  FWBufFree;
            FWBufFree = Buffer;
            BufferPtr += FW_BUF_SIZE;
        }

		CurrentFWBufferSize += AvailableBufferSpace;
		CTEFreeLock(&FWPacketFreeLock, Handle);
		return TRUE;

	}

failure:
	CTEFreeLock(&FWPacketFreeLock, Handle);
	if (BufferPtr != NULL) {
		CTEFreeMem(BufferPtr);
	}
	return FALSE;

}

//* FWSendComplete  - Complete the transmission of a forwarded packet.
//
//  This is called when the send of a forwarded packet is done. We'll free the resources
//  and get the next send going, if there is one. If there isn't, we'll decrement the pending
//  count.
//
//  Input:  Packet      - Packet being completed.
//          Buffer      - Pointer to buffer chain being completed.
//
//  Returns: Nothing.
//
void
FWSendComplete(void *SendContext, PNDIS_BUFFER Buffer)
{
    PNDIS_PACKET    Packet = (PNDIS_PACKET)SendContext;
    FWContext       *FWC = (FWContext *)Packet->ProtocolReserved;
    RouteSendQ      *RSQ;
    CTELockHandle   Handle;
    FWQ             *NewFWQ;
    PNDIS_PACKET    NewPacket;


#ifdef  DEBUG
    if (!Buffer)
        DEBUGCHK;
#endif

    if (!IS_BCAST_DEST(FWC->fc_dtype))
        RSQ = &((RouteInterface *)FWC->fc_if)->ri_q;
    else
        RSQ = BCastRSQ;

    FreeFWPacket(Packet);

    CTEGetLock(&RSQ->rsq_lock, &Handle);
	CTEAssert(RSQ->rsq_pending <= RSQ->rsq_maxpending);
	
    RSQ->rsq_pending--;
	
	CTEAssert(*(int *)&RSQ->rsq_pending >= 0);
	
    if (RSQ->rsq_qlength != 0) {          // Have more to send.
		// Make sure we're not already running through this. If we are, quit.
		if (!RSQ->rsq_running) {
			
			// We could schedule this off for an event, but under NT that
			// could me a context switch for every completing packet in the
			// normal case. For now, just do it in a loop guarded with
			// rsq_running.
			RSQ->rsq_running = TRUE;
		
			// Loop while we haven't hit our send limit and we still have
			// stuff to send.
			while (RSQ->rsq_pending < RSQ->rsq_maxpending &&
                RSQ->rsq_qlength != 0) {
#ifdef DEBUG
	    		if (RSQ->rsq_qh.fq_next == &RSQ->rsq_qh)
	        		DEBUGCHK;                       // Empty Q!
#endif
				// Pull one off the queue, and update qlength.
		        NewFWQ = RSQ->rsq_qh.fq_next;
		        RSQ->rsq_qh.fq_next = NewFWQ->fq_next;
		        NewFWQ->fq_next->fq_prev = NewFWQ->fq_prev;
		        RSQ->rsq_qlength--;
				
				// Update pending before we send.
				RSQ->rsq_pending++;
		        CTEFreeLock(&RSQ->rsq_lock, Handle);
		        NewPacket = PACKET_FROM_FWQ(NewFWQ);
		        TransmitFWPacket(NewPacket,
		        	((FWContext *)NewPacket->ProtocolReserved)->fc_datalength);
		    	CTEGetLock(&RSQ->rsq_lock, &Handle);
			}
	
			RSQ->rsq_running = FALSE;
		}
    }
	
	CTEFreeLock(&RSQ->rsq_lock, Handle);

}

//* TransmitFWPacket - Transmit a forwarded packet on a link.
//
//  Called when we know we can send a packet. We fix up the header, and send it.
//
//  Input:  Packet      - Packet to be sent.
//          DataLength  - Length of data.
//
//  Returns: Nothing.
//
void
TransmitFWPacket(PNDIS_PACKET Packet, uint DataLength)
{
    FWContext       *FC = (FWContext *)Packet->ProtocolReserved;
    PNDIS_BUFFER    HBuffer, Buffer;
    IP_STATUS       Status;
    PVOID           VirtualAddress;
    UINT            BufLen;

    // Fix up the packet. Remove the existing buffer chain, and put our header on
    // the front.

    // BUGBUG - Get NDIS fixed to make this portable.
#ifdef VXD
    Buffer = Packet->Private.Head;
    HBuffer = FC->fc_hndisbuff;
    Packet->Private.Head = HBuffer;
    Packet->Private.Tail = HBuffer;
    HBuffer->Next = (PNDIS_BUFFER)NULL;
    Packet->Private.TotalLength = sizeof(IPHeader);
    Packet->Private.Count = 1;

    Packet->Private.PhysicalCount = ADDRESS_AND_SIZE_TO_SPAN_PAGES(HBuffer->VirtualAddress,
        sizeof(IPHeader));
#else // VXD
#ifdef NT
    Buffer = Packet->Private.Head;
    HBuffer = FC->fc_hndisbuff;
    Packet->Private.Head = HBuffer;
    Packet->Private.Tail = HBuffer;
    NDIS_BUFFER_LINKAGE(HBuffer) = (PNDIS_BUFFER)NULL;
    Packet->Private.TotalLength = sizeof(IPHeader);
    Packet->Private.Count = 1;

    NdisQueryBuffer(HBuffer, &VirtualAddress, &BufLen);

    Packet->Private.PhysicalCount =
        ADDRESS_AND_SIZE_TO_SPAN_PAGES(
            VirtualAddress,
            sizeof(IPHeader)
            );
#else // NT
#error HELP! Need to make this code portable.
#endif // NT
#endif // VXD

    // Figure out how to send it. If it's not a broadcast we'll either send it or
    // have it fragmented. If it is a broadcast we'll let our send broadcast routine
    // handle it.
    if (FC->fc_dtype != DEST_BCAST) {

        if ((DataLength + (uint)FC->fc_optlength) <= FC->fc_mtu)
            Status = SendIPPacket(FC->fc_if,  FC->fc_nexthop, Packet, Buffer,
            	FC->fc_hbuff, FC->fc_options, (uint)FC->fc_optlength);
        else {                          // Need to fragment this.
            BufferReference     *BR = CTEAllocMem(sizeof(BufferReference));

            if (BR == (BufferReference *)NULL) {        // Couldn't get a BufferReference
                FWSendComplete(Packet, Buffer);
                return;
            }
            BR->br_buffer = Buffer;
            BR->br_refcount = 0;
            CTEInitLock(&BR->br_lock);
            FC->fc_pc.pc_br = BR;
            Status = IPFragment(FC->fc_if, FC->fc_mtu, FC->fc_nexthop, Packet,
            	FC->fc_hbuff, Buffer, DataLength, FC->fc_options,
            	(uint)FC->fc_optlength, (int *)NULL);

            //
            // Fragmentation needed with the DF flag set should have been
            // handled in IPForward. We don't have the original header
            // any longer, so silently drop the packet.
            //
            CTEAssert(Status != IP_PACKET_TOO_BIG);
        }
    } else
        Status = SendIPBCast(FC->fc_srcnte, FC->fc_nexthop, Packet, FC->fc_hbuff,
        	Buffer, DataLength, FC->fc_options, (uint)FC->fc_optlength,
        	FC->fc_sos, &FC->fc_index);

    if (Status != IP_PENDING)
        FWSendComplete(Packet, Buffer);
}

//* SendFWPacket - Send a packet that needs to be forwarded.
//
//  This routine is invoked when we actually get around to sending a packet.
//  We look and see if we can give another queued send to the outgoing link,
//  and if so we send on that link. Otherwise we put it on the outgoing queue
//  and remove it later.
//
//  Input:  SrcNTE      - Source NTE of packet.
//          Packet      - Packet to be send, containg all needed context info.
//          Status      - Status of transfer data.
//          DataLength  - Length in bytes of data to be send.
//
//  Returns: Nothing.
//
void
SendFWPacket(PNDIS_PACKET Packet, NDIS_STATUS Status, uint DataLength)
{

    FWContext       *FC = (FWContext *)Packet->ProtocolReserved;
	Interface		*IF = FC->fc_if;
    RouteSendQ      *RSQ;
    CTELockHandle   Handle;

	if (Status == NDIS_STATUS_SUCCESS) {
		// Figure out which logical queue it belongs on, and if we don't already
		// have too many things going there, send it. If we can't send it now we'll
		// queue it for later.
		if (IS_BCAST_DEST(FC->fc_dtype))
			RSQ = BCastRSQ;
		else
			RSQ = &((RouteInterface *)IF)->ri_q;
	
		CTEGetLock(&RSQ->rsq_lock, &Handle);
	
		if (RSQ->rsq_pending < RSQ->rsq_maxpending && RSQ->rsq_qlength == 0) {
			// We can send on this interface.
			RSQ->rsq_pending++;
			CTEFreeLock(&RSQ->rsq_lock, Handle);
	
			TransmitFWPacket(Packet, DataLength);
	
		} else {                            // Need to queue this packet for later.
	
			FC->fc_datalength = DataLength;
			FC->fc_q.fq_next = &RSQ->rsq_qh;
			FC->fc_q.fq_prev = RSQ->rsq_qh.fq_prev;
			RSQ->rsq_qh.fq_prev->fq_next = &FC->fc_q;
			RSQ->rsq_qh.fq_prev = &FC->fc_q;
			RSQ->rsq_qlength++;
			CTEFreeLock(&RSQ->rsq_lock, Handle);
		}
	} else{
		IPSInfo.ipsi_outdiscards++;
		FreeFWPacket(Packet);
	}

}

//* RemoveRandomFWPacket - Remove a random packet from the FW queue.
//
//  Called when we run out of resources. We pick a random packet from the FW queue,
//  free it, and return. The caller will hopefully then get it for his own use.
//
//  Input:  RSQ     - Pointer to outgoing route send q..
//
//  Returns: TRUE if we free a packet, false if we didn't.
//
uchar
RemoveRandomFWPacket(RouteSendQ *RSQ)
{
    uint            Now = (uint)CTESystemUpTime();
    CTELockHandle   Handle;
    uint            PacketCount;
    PNDIS_PACKET    FreedPacket;
    FWQ             *CurrentFWQ;
#ifdef DEBUG
    FWQ             *FirstFWQ;
#endif

#ifdef DEBUG
    FirstFWQ = &RSQ->rsq_qh;
#endif

    CTEGetLock(&RSQ->rsq_lock, &Handle);
    if (RSQ->rsq_qlength) {                     // We have a least one in the list.


        PacketCount = Now % (RSQ->rsq_qlength + 1);
        if (PacketCount == RSQ->rsq_qlength) {
            CTEFreeLock(&RSQ->rsq_lock, Handle);
            return FALSE;
        }

        CurrentFWQ = RSQ->rsq_qh.fq_next;
        while (PacketCount--) {
#ifdef DEBUG
            if (CurrentFWQ == FirstFWQ)
                DEBUGCHK;
#endif
            CurrentFWQ = CurrentFWQ->fq_next;
        }

        // We've got the proper packet. Splice him out.
        CurrentFWQ->fq_next->fq_prev = CurrentFWQ->fq_prev;
        CurrentFWQ->fq_prev->fq_next = CurrentFWQ->fq_next;
        RSQ->rsq_qlength--;
        CTEFreeLock(&RSQ->rsq_lock, Handle);
        FreedPacket = PACKET_FROM_FWQ(CurrentFWQ);
        FreeFWPacket(FreedPacket);
        IPSInfo.ipsi_outdiscards++;
        return TRUE;
    }
    CTEFreeLock(&RSQ->rsq_lock, Handle);
    return FALSE;


}

//* GetFWBuffer - Get a list of buffers for forwarding.
//
//  This routine gets a list of buffers for forwarding, and puts the data into it. This
//  may involve calling TransferData, or we may be able to copy directly into them
//  ourselves.
//
//  Input:  SrcNTE          - Pointer to NTE on which packet was received.
//          Packet          - Packet being forwarded, used for TD.
//          Data            - Pointer to data buffer being forwarded.
//          DataLength      - Length in bytes of Data.
//          BufferLength    - Length in bytes available in buffer pointer to by Data.
//          Offset          - Offset into original data from which to transfer.
//          LContext1, LContext2 - Context values for the link layer.
//
//  Returns: NDIS_STATUS of attempt to get buffer.
//
NDIS_STATUS
GetFWBuffer(NetTableEntry *SrcNTE, PNDIS_PACKET Packet, uchar *Data,
	uint DataLength, uint BufferLength, uint Offset, NDIS_HANDLE LContext1,
	uint LContext2)
{
    CTELockHandle       Handle;
    uint                BufNeeded, i;
    PNDIS_BUFFER        FirstBuffer, CurrentBuffer;
    void                *DestPtr;
    Interface           *SrcIF;
    FWContext           *FWC;
    uint                BufLen;
	uint				LastBufSize;
#ifdef DEBUG
	uint				TotalBufferSize;
	PNDIS_BUFFER		TempBuffer;
#endif

    // Figure out how many buffers we need.
    BufNeeded = DataLength / FW_BUF_SIZE;
	LastBufSize = DataLength % FW_BUF_SIZE;
    if (LastBufSize != 0)
        BufNeeded++;

#ifdef  DEBUG
    if (!BufNeeded)
        DEBUGCHK;
#endif
    FWC = (FWContext *)Packet->ProtocolReserved;

    // Now run down the buffer free list, getting the buffers we need. If we
    // can't get enough the first time, we'll free a random packet from our
    // pending list and try again.
    for (;;) {
        CTEGetLock(&FWBufFreeLock, &Handle);
        FirstBuffer = FWBufFree;
        CurrentBuffer = STRUCT_OF(NDIS_BUFFER, &FWBufFree, Next);
        i = 0;
        do {
            CurrentBuffer = NDIS_BUFFER_LINKAGE(CurrentBuffer);
            if (!CurrentBuffer)
                break;
			
			// Zap this buffer length to the full buffer size, since it may
			// have been modified from a previous send.
            NdisAdjustBufferLength(CurrentBuffer, FW_BUF_SIZE);
            i++;
        } while (i < BufNeeded);

        if (i != BufNeeded) {               // We ran out of buffers. Free a packet and try again.
            RouteSendQ      *RSQ;

			if ((MaxFWBufferSize - CurrentFWBufferSize) <= FW_BUF_SIZE) {
				CTEFreeLock(&FWBufFreeLock, Handle);
				if (GrowFWBuffer()) {
					continue;
				}
			} else
				CTEFreeLock(&FWBufFreeLock, Handle);

            if (!IS_BCAST_DEST(FWC->fc_dtype))
                RSQ = &((RouteInterface *)FWC->fc_if)->ri_q;
            else
                RSQ = BCastRSQ;

            if (!RemoveRandomFWPacket(RSQ)) {
				if (IS_BCAST_DEST(FWC->fc_dtype))
                	return NDIS_STATUS_RESOURCES;
				
				// Couldn't get one for a non-broadcast packet. If the qlen is
				// 0 on the outgoing queue, we'll try other queues, on the
				// presumption that traffic through some other interface is
				// starving this one.
				if (RSQ->rsq_qlength == 0) {
					Interface		*IF;
					for (IF = IFList; IF != NULL; IF = IF->if_next) {
						RSQ = &((RouteInterface *)IF)->ri_q;
						if (RemoveRandomFWPacket(RSQ))
							break;
					}
					if (IF == NULL)
						return NDIS_STATUS_RESOURCES;
				} else
					return NDIS_STATUS_RESOURCES;
			}

			// Otherwise we'll fall through and try again, now that we have
			// hopefully put some more on the free queue.

        } else {
            // We have as many as we need. Update the free list.
            FWBufFree = NDIS_BUFFER_LINKAGE(CurrentBuffer);
            CTEFreeLock(&FWBufFreeLock, Handle);
            NDIS_BUFFER_LINKAGE(CurrentBuffer) = (PNDIS_BUFFER)NULL;
			
			// If we have a non-full last buffer, adjust it's size.
			if (LastBufSize != 0)
            	NdisAdjustBufferLength(CurrentBuffer, LastBufSize);
          	
          	FWC->fc_buffhead = FirstBuffer;
            FWC->fc_bufftail = CurrentBuffer;
            break;
        }
    }

    NdisChainBufferAtFront(Packet, FirstBuffer);

#ifdef DEBUG
	// Sanity check the buffer chain and packet.
	TempBuffer = FirstBuffer;
	TotalBufferSize = 0;
	while (TempBuffer != NULL) {
		TotalBufferSize += NdisBufferLength(TempBuffer);
		TempBuffer = NDIS_BUFFER_LINKAGE(TempBuffer);
	}
	
	CTEAssert(TotalBufferSize == DataLength);
	NdisQueryPacket(Packet, NULL, NULL, NULL, &TotalBufferSize);
	CTEAssert(TotalBufferSize == DataLength);
#endif

    // First buffer points to the list of buffers we have. If we can copy the
    // data here, do so, otherwise invoke the link's transfer data routine.
    if ((DataLength <= BufferLength) && (SrcNTE->nte_flags & NTE_COPY)) {
        while (DataLength) {
            uint        CopyLength;

#ifdef VXD
            DestPtr = FirstBuffer->VirtualAddress;
#else
            //
            // BUGBUG: This is inefficient.
            //
            NdisQueryBuffer(FirstBuffer, &DestPtr, &BufLen);
#endif
            CopyLength = MIN(DataLength, FW_BUF_SIZE);
			CTEAssert(CopyLength == NdisBufferLength(FirstBuffer));
            CTEMemCopy(DestPtr, Data, CopyLength);
            Data += CopyLength;
            DataLength -= CopyLength;
            FirstBuffer = NDIS_BUFFER_LINKAGE(FirstBuffer);
        }
        return NDIS_STATUS_SUCCESS;
    }

    // We need to call transfer data for this.

    SrcIF = SrcNTE->nte_if;
    return (*(SrcIF->if_transfer))(SrcIF->if_lcontext, LContext1, LContext2,
    	Offset, DataLength, Packet, &DataLength);


}


//* GetFWPacket - Get a packet for forwarding.
//
//  Called when we need to get a packet to forward a datagram.
//
//  Entry:  Packet      - Pointer to where to return a packet.
//          IF         	- Outgoing I/F for packet.
//          DestType    - Type of outgoing packet.
//
//  Returns: Pointer to header buffer.
//
//
IPHeader *
GetFWPacket(PNDIS_PACKET *Packet, Interface *IF, uchar DestType)
{
    CTELockHandle       Handle;
    PNDIS_PACKET        NewPacket;
    RouteSendQ          *RSQ;

    for (;;) {
        CTEGetLock(&FWPacketFreeLock, &Handle);
        if ((NewPacket = FWPacketFree) != (PNDIS_PACKET)NULL) {
            FWContext       *FWC;

            FWC = (FWContext *)NewPacket->ProtocolReserved;
            FWPacketFree = FWC->fc_pc.pc_common.pc_link;
            FWC->fc_pc.pc_common.pc_flags |= PACKET_FLAG_IPHDR;
            FWC->fc_pc.pc_br = NULL;
            *Packet = NewPacket;
            CTEFreeLock(&FWPacketFreeLock, Handle);
            return FWC->fc_hbuff;
        }

		// If we couldn't get one, try to grow the list if we can.
		if (MaxFWPackets > CurrentFWPackets) {
			CTEFreeLock(&FWPacketFreeLock, Handle);
			// We're allowed to grow, so try to.
			if (GrowFWPackets()) {
				continue;			// If we grew it, try again.
			}
		} else
			CTEFreeLock(&FWPacketFreeLock, Handle);

		// Either we weren't allowed to grow the list, or we tried to but couldn't. Try yo
		// get one that's on the queue already.
        if (!IS_BCAST_DEST(DestType))
            RSQ = &((RouteInterface *)IF)->ri_q;
        else
            RSQ = BCastRSQ;

        if (!RemoveRandomFWPacket(RSQ))
            break;
    }

    return (IPHeader *)NULL;
}


//** IPForward - Forward a packet.
//
//  The routine called when we need to forward a packet. We check if we're supposed
//  to act as a gateway, and if we are and the incoming packet is a bcast we check
//  and see if we're supposed to forward broadcasts. Assuming we're supposed to
//  forward it, we will process any options. If we find some, we do some validation
//  to make sure everything is good. After that, we look up the next hop. If we can't
//  find one, we'll issue an error.  Then we get a packet and buffers, and send it.
//
//  Input:  SrcNTE          - NTE for net on which we received this.
//          Header          - Pointer to received IPheader.
//          HeaderLength    - Length of header.
//          Data            - Pointer to data to be forwarded.
//          BufferLength    - Length in bytes available in the buffer.
//          DestType        - Type of destination.
//
//  Returns: Nothing.
//
void
IPForward(NetTableEntry *SrcNTE, IPHeader UNALIGNED *Header, uint HeaderLength,
    void *Data, uint BufferLength, NDIS_HANDLE LContext1, uint LContext2,
    uchar DestType)
{
    uchar           *Options;
    uchar           OptLength;
    OptIndex        Index;
    IPAddr          DestAddr;       // IP address we're routing towards.
    uchar           SendOnSource = FALSE;
    IPAddr          NextHop;        // Next hop IP address.
    PNDIS_PACKET    Packet;
    FWContext       *FWC;
    IPHeader        *NewHeader;     // New header.
    NDIS_STATUS     Status;
    uint            DataLength;
	CTELockHandle	TableHandle;
	uchar			ErrIndex;
	IPAddr			OutAddr;		// Address of interface we're send out on.
	Interface		*IF;			// Interface we're sending out on.
	uint			MTU;

    if (ForwardPackets) {

        DestAddr = Header->iph_dest;

        // If it's a broadcast, see if we can forward it. We won't forward it if broadcast
        // forwarding is turned off, or the destination if the local (all one's) broadcast,
        // or it's a multicast (Class D address). We'll pass through subnet broadcasts in
        // case there's a source route. This would be odd - maybe we should disable this?
        if (IS_BCAST_DEST(DestType)) {
            if (!ForwardBCast) {
                if (DestType > DEST_REMOTE)
                    IPSInfo.ipsi_inaddrerrors++;
                return;
            }
            if ((DestAddr == IP_LOCAL_BCST) ||
                (DestAddr == IP_ZERO_BCST) ||
				(DestType == DEST_SN_BCAST) ||
                CLASSD_ADDR(DestAddr)) {
                return;
            }
        } else
            if (DestType == DEST_REMOTE) {
                SrcNTE = BestNTEForIF(Header->iph_src, SrcNTE->nte_if);
				if (SrcNTE == NULL) {
					// Something bad happened.
					return;
				}
			}

        // If the TTL would expire, send a message.
        if (Header->iph_ttl <= 1) {
            IPSInfo.ipsi_inhdrerrors++;
            SendICMPErr(SrcNTE->nte_addr, Header, ICMP_TIME_EXCEED, TTL_IN_TRANSIT,0);
            return;
        }

        DataLength = net_short(Header->iph_length) - HeaderLength;

        Index.oi_srtype = NO_SR;            // So we know we don't have a source route.
        Index.oi_srindex = MAX_OPT_SIZE;
        Index.oi_rrindex = MAX_OPT_SIZE;
        Index.oi_tsindex = MAX_OPT_SIZE;

        // Now check for options, and process any we find.
        if (HeaderLength != sizeof(IPHeader)) {
            IPOptInfo       OptInfo;

            OptInfo.ioi_options = (uchar *)(Header + 1);
            OptInfo.ioi_optlength = HeaderLength - sizeof(IPHeader);
            // Validate options, and set up indices.
            if ((ErrIndex = ParseRcvdOptions(&OptInfo, &Index)) < MAX_OPT_SIZE) {
                IPSInfo.ipsi_inhdrerrors++;
                SendICMPErr(SrcNTE->nte_addr, Header, ICMP_PARAM_PROBLEM,
                	PTR_VALID, ((ulong)ErrIndex + sizeof(IPHeader)));
                return;
            }

            Options = CTEAllocMem(OptInfo.ioi_optlength);
            if (!Options) {
                IPSInfo.ipsi_outdiscards++;
                return;                                 // Couldn't get an
            }                                           // option buffer, return;

            // Now copy into our buffer.
            CTEMemCopy(Options, OptInfo.ioi_options, OptLength = OptInfo.ioi_optlength);

            // See if we have a source routing option, and if so we may need to process it. If
            // we have one, and the destination in the header is us, we need to update the
            // route and the header.
            if (Index.oi_srindex != MAX_OPT_SIZE) {
                if (DestType >= DEST_REMOTE) {          // Not for us.
                    if (Index.oi_srtype == IP_OPT_SSRR) {
                        // This packet is strict source routed, but we're not the destination!
                        // We can't continue from here - perhaps we should send an ICMP, but
                        // I'm not sure which one it would be.
                        CTEFreeMem(Options);
                        IPSInfo.ipsi_inaddrerrors++;
                        return;
                    }
                    Index.oi_srindex = MAX_OPT_SIZE;    // Don't need to update this.

                } else {    // This came here, we need to update the destination address.
                    uchar   *SROpt = Options + Index.oi_srindex;
                    uchar   Pointer;

                    Pointer = SROpt[IP_OPT_PTR] - 1;    // Index starts from one.

                    // Get the next hop address, and see if it's a broadcast.
                    DestAddr = *(IPAddr UNALIGNED *)&SROpt[Pointer];
                    DestType = GetAddrType(DestAddr);       // Find address type.
                    if (DestType == DEST_INVALID) {
                        SendICMPErr(SrcNTE->nte_addr, Header, ICMP_DEST_UNREACH, SR_FAILED, 0);
                        IPSInfo.ipsi_inhdrerrors++;
                        CTEFreeMem(Options);
                        return;
                    }

                    // If we came through here, any sort of broadcast needs to be sent out
                    // the way it came, so update that flag.
                    SendOnSource = TRUE;
                }
            }
        } else {    // No options.
            Options = (uchar *)NULL;
            OptLength = 0;
        }

        IPSInfo.ipsi_forwdatagrams++;

        // We've processed the options. Now look up the next hop. If we can't
        // find one, send back an error.
		IF = LookupNextHopWithBuffer(DestAddr, SrcNTE->nte_addr, &NextHop, &MTU,
			Header->iph_protocol, (uchar *)Data, BufferLength);

        if (IF == NULL) {
			// Couldn't find an outgoing route.
            IPSInfo.ipsi_outnoroutes++;
            SendICMPErr(SrcNTE->nte_addr, Header, ICMP_DEST_UNREACH,
            	HOST_UNREACH, 0);
            if (Options)
                CTEFreeMem(Options);
            return;
        }

        //
        // If the DF flag is set, make sure the packet doesn't need
        // fragmentation. If this is the case, send an ICMP error
        // now while we still have the original IP header. The ICMP
        // message includes the MTU so the source host can perform
        // Path MTU discovery.
        //
        if ( (Header->iph_offset & IP_DF_FLAG) &&
             ((DataLength + (uint)OptLength) > MTU)
           )
        {
            CTEAssert((MTU + sizeof(IPHeader)) >= 68);
            CTEAssert((MTU + sizeof(IPHeader)) <= 0xFFFF);

            IPSInfo.ipsi_fragfails++;
            SendICMPErr(SrcNTE->nte_addr, Header, ICMP_DEST_UNREACH,
                FRAG_NEEDED, net_long((ulong)(MTU + sizeof(IPHeader)))
                );

            if (Options)
                CTEFreeMem(Options);
#ifdef _PNP_POWER
			DerefIF(IF);
#endif
            return;
        }

		// See if we need to filter this packet. If we do, call the filter routine
		// to see if it's OK to forward it.
		if (ForwardFilterPtr != NULL) {
			FORWARD_ACTION		Action;

			Action = (*ForwardFilterPtr)(Header, Data, BufferLength,
				SrcNTE->nte_if->if_filtercontext, IF->if_filtercontext);

			if (Action != FORWARD) {
				IPSInfo.ipsi_outdiscards++;
				if (Options)
					CTEFreeMem(Options);
#ifdef _PNP_POWER
				DerefIF(IF);
#endif
				return;
			}
		}

        // If we have a strict source route and the next hop is not the one
        // specified, send back an error.
        if (Index.oi_srtype == IP_OPT_SSRR) {
            if (DestAddr != NextHop) {
                IPSInfo.ipsi_outnoroutes++;
                SendICMPErr(SrcNTE->nte_addr, Header, ICMP_DEST_UNREACH,
                	SR_FAILED, 0);
                CTEFreeMem(Options);
#ifdef _PNP_POWER
				DerefIF(IF);
#endif
				return;
            }
		}

		// Update the options, if we can and we need to.
        if ((DestType != DEST_BCAST) && Options != NULL) {
			NetTableEntry		*OutNTE;

			// Need to find a valid source address for the outgoing interface.
			CTEGetLock(&RouteTableLock, &TableHandle);
			OutNTE = BestNTEForIF(DestAddr, IF);
			if (OutNTE == NULL) {
				// No NTE for this IF. Something's wrong, just bail out.
				CTEFreeLock(&RouteTableLock, TableHandle);
				CTEFreeMem(Options);
#ifdef _PNP_POWER
				DerefIF(IF);
#endif
				return;
			} else {
				OutAddr = OutNTE->nte_addr;
				CTEFreeLock(&RouteTableLock, TableHandle);
			}

        	ErrIndex = UpdateOptions(Options, &Index,
        		(IP_LOOPBACK(OutAddr) ? DestAddr : OutAddr));
			
			if (ErrIndex != MAX_OPT_SIZE) {
            	IPSInfo.ipsi_inhdrerrors++;
            	SendICMPErr(OutAddr, Header, ICMP_PARAM_PROBLEM, PTR_VALID,
                	((ulong)ErrIndex + sizeof(IPHeader)));
				CTEFreeMem(Options);
#ifdef _PNP_POWER
				DerefIF(IF);
#endif
				return;
			}
		}


        // Send a redirect, if we need to. We'll send a redirect if the packet
		// is going out on the interface it came in on and the next hop address
		// is on the same subnet as the NTE we received it on, and if there
		// are no source route options. We also need to make sure that the
		// source of the datagram is on the I/F we received it on, so we don't
		// send a redirect to another gateway.
        // SendICMPErr will check and not send a redirect if this is a broadcast.
        if ((SrcNTE->nte_if == IF) &&
        	IP_ADDR_EQUAL(SrcNTE->nte_addr & SrcNTE->nte_mask,
        		NextHop & SrcNTE->nte_mask) &&
			IP_ADDR_EQUAL(SrcNTE->nte_addr & SrcNTE->nte_mask,
				Header->iph_src & SrcNTE->nte_mask))
        {
            if (Index.oi_srindex == MAX_OPT_SIZE)
            {

#ifdef REDIRECT_DEBUG

#define PR_IP_ADDR(x) \
    ((x)&0x000000ff),(((x)&0x0000ff00)>>8),(((x)&0x00ff0000)>>16),(((x)&0xff000000)>>24)


                DbgPrint("IP: Sending Redirect. IF = %x SRC_NTE = %x SrcNteIF = %x\n",
                         IF,SrcNTE,SrcNTE->nte_if);

                DbgPrint("IP: SrcNteAddr = %d.%d.%d.%d Mask = %d.%d.%d.%d\n",
                         PR_IP_ADDR(SrcNTE->nte_addr), PR_IP_ADDR(SrcNTE->nte_mask));

                DbgPrint("IP: NextHop = %d.%d.%d.%d Header Src = %d.%d.%d.%d, Dst = %d.%d.%d.%d\n",
                         PR_IP_ADDR(NextHop),
                         PR_IP_ADDR(Header->iph_src),
                         PR_IP_ADDR(Header->iph_dest));

#endif

                SendICMPErr(SrcNTE->nte_addr, Header, ICMP_REDIRECT,
                REDIRECT_HOST, NextHop);
            }
		}

        // We have the next hop. Now get a forwarding packet.
        if ((NewHeader = GetFWPacket(&Packet, IF, DestType)) !=
        	(IPHeader *)NULL) {

            // Got the header. Fill it in.

            NewHeader->iph_verlen = Header->iph_verlen;
            NewHeader->iph_tos = Header->iph_tos;
            NewHeader->iph_length = Header->iph_length;
            NewHeader->iph_id = Header->iph_id;
            NewHeader->iph_offset = Header->iph_offset;
            NewHeader->iph_protocol = Header->iph_protocol;
            NewHeader->iph_src = Header->iph_src;

            NewHeader->iph_dest = DestAddr;
            NewHeader->iph_ttl = Header->iph_ttl - 1;
            NewHeader->iph_xsum = 0;

            // Save the packet forwarding context info.
            FWC = (FWContext *)Packet->ProtocolReserved;
            FWC->fc_options = Options;
            FWC->fc_optlength = OptLength;
            FWC->fc_if = IF;
			FWC->fc_mtu = MTU;
			FWC->fc_srcnte = SrcNTE;
            FWC->fc_nexthop = NextHop;
            FWC->fc_sos = SendOnSource;
            FWC->fc_dtype = DestType;
            FWC->fc_index = Index;

			// Now that we have a packet, go ahead and transfer data the
			// data in if we need to.
            Status = GetFWBuffer(SrcNTE, Packet, Data, DataLength, BufferLength,
            	HeaderLength, LContext1, LContext2);

			// If the status is pending, don't do anything now. Otherwise,
			// if the status is success send the packet.
            if (Status != NDIS_STATUS_PENDING)
                if (Status == NDIS_STATUS_SUCCESS) {
                    SendFWPacket(Packet, Status, DataLength);
                } else {
					// Some sort of failure. Free the packet.
                    IPSInfo.ipsi_outdiscards++;
                    FreeFWPacket(Packet);
                }
        } else {                            // Couldn't get a packet, so drop this.
            IPSInfo.ipsi_outdiscards++;
            if (Options)
                CTEFreeMem(Options);
#ifdef _PNP_POWER
			DerefIF(IF);
#endif
        }
    } else {                                // Forward called, but forwarding
                                            // turned off.
        if (DestType != DEST_BCAST && DestType != DEST_SN_BCAST) {
            // No need to go through here for strictly broadcast packets,
            // although we want to bump the counters for remote bcast stuff.
            IPSInfo.ipsi_inaddrerrors++;

            if (!IS_BCAST_DEST(DestType)) {
                if (DestType == DEST_LOCAL)         // Called when local, must be SR.
                    SendICMPErr(SrcNTE->nte_addr, Header,
                        ICMP_DEST_UNREACH, SR_FAILED, 0);
            }
        }
    }

}

//*	AddNTERoutes - Add the routes for an NTE.
//
//	Called during initalization or during DHCP address assignment to add
//	routes. We add routes for the address of the NTE, including routes
//	to the subnet and the address itself.
//
//	Input:	NTE			- NTE for which to add routes.
//
//	Returns: TRUE if they were all added, FALSE if not.
//
uint
AddNTERoutes(NetTableEntry *NTE)
{
	IPMask			Mask, SNMask;
	Interface		*IF;
	CTELockHandle	Handle;
	IPAddr			AllSNBCast;
	IP_STATUS		Status;

	// First, add the route to the address itself. This is a route through
	// the loopback interface.

	if (AddRoute(NTE->nte_addr, HOST_MASK, IPADDR_LOCAL, LoopNTE->nte_if,
		LOOPBACK_MSS, 1, IRE_PROTO_LOCAL, ATYPE_OVERRIDE, NULL) != IP_SUCCESS)
		return FALSE;

	Mask = IPNetMask(NTE->nte_addr);
	IF = NTE->nte_if;

	// Now add the route for the all-subnet's broadcast, if one doesn't already
	// exist. There is special case code to handle this in SendIPBCast, so the
	// exact interface we add this on doesn't really matter.

    CTEGetLock(&RouteTableLock, &Handle);
	AllSNBCast = (NTE->nte_addr & Mask) | (IF->if_bcast & ~Mask);
    if (LookupRTE(AllSNBCast, NULL_IP_ADDR, HOST_ROUTE_PRI) == NULL) {
		
		Status = LockedAddRoute(AllSNBCast, HOST_MASK, IPADDR_LOCAL, IF,
			NTE->nte_mss, 1, IRE_PROTO_LOCAL, ATYPE_PERM, NULL);
		
	} else
		Status = IP_SUCCESS;

	CTEFreeLock(&RouteTableLock, Handle);
	
	if (Status != IP_SUCCESS)
		return FALSE;
	
	// If we're doing IGMP, add the route to the multicast address.
	if (IGMPLevel != 0) {
	    if (AddRoute(CLASSD_MASK, CLASSD_MASK, IPADDR_LOCAL, NTE->nte_if,
			NTE->nte_mss, 1, IRE_PROTO_LOCAL, ATYPE_PERM, NULL) != IP_SUCCESS)
			return FALSE;
	}
	
    if(NTE->nte_mask != HOST_MASK)
    {	
	    // And finally the route to the subnet.
        SNMask = NTE->nte_mask;
        if (AddRoute(NTE->nte_addr & SNMask, SNMask, IPADDR_LOCAL, NTE->nte_if,
		    NTE->nte_mss, 1, IRE_PROTO_LOCAL, ATYPE_PERM, NULL) != IP_SUCCESS)
		    return FALSE;
    }
	
	return TRUE;
}
	

#ifndef	CHICAGO
#pragma BEGIN_INIT
#endif

uint	BCastMinMTU = 0xffff;

//*	InitNTERouting -  do per NTE route initialization.
//
//	Called when we need to initialize per-NTE routing. For the specified NTE,
//	call AddNTERoutes to  add a route for a net bcast, subnet bcast, and local
//	attached subnet. The net bcast entry is sort of a filler - net and
//	global bcasts are always handled specially. For this reason we specify
//	the FirstInterface when adding the route. Subnet bcasts are assumed to
//	only go out on one interface, so the actual interface to be used is
//	specifed. If two interfaces are on the same subnet the last interface is
//	the one that will be used.
//
//	Input:	NTE			- NTE for which routing is to be initialized.
//			NumGWs		- Number of default gateways to add.
//			GWList		- List of default gateways.
//
//	Returns: TRUE if we succeed, FALSE if we don't.
//
uint
InitNTERouting(NetTableEntry *NTE, uint NumGWs, IPAddr *GWList)
{
	uint		i;
	Interface	*IF;
	
	CTERefillMem();
	if (NTE != LoopNTE) {
		BCastMinMTU = MIN(BCastMinMTU, NTE->nte_mss);
	
		IF = NTE->nte_if;
		AddRoute(IF->if_bcast, HOST_MASK, IPADDR_LOCAL,  FirstIF,
			BCastMinMTU, 1, IRE_PROTO_LOCAL, ATYPE_OVERRIDE, NULL);// Route for local
															// bcast.
        if (NTE->nte_flags & NTE_VALID) {
			if (!AddNTERoutes(NTE))
				return FALSE;
		
	        // Now add the default routes that are present on this net. We
			// don't check for errors here, but we should probably
			// log an error.
	        for (i = 0; i < NumGWs;i++) {
				IPAddr		GWAddr;
				
				GWAddr = net_long(GWList[i]);
				
				if (IP_ADDR_EQUAL(GWAddr, NTE->nte_addr)) {
					AddRoute(NULL_IP_ADDR, DEFAULT_MASK,
						IPADDR_LOCAL, NTE->nte_if, NTE->nte_mss, 1,
						IRE_PROTO_LOCAL, ATYPE_OVERRIDE, NULL);
				} else
					AddRoute(NULL_IP_ADDR, DEFAULT_MASK,
						net_long(GWList[i]), NTE->nte_if, NTE->nte_mss, 1,
						IRE_PROTO_LOCAL, ATYPE_OVERRIDE, NULL);
			}
		}
	}
	return TRUE;
}

#ifdef CHICAGO
#pragma	BEGIN_INIT
#endif

//* InitRouting - Initialize our routing table.
//
//  Called during initialization to initialize the routing table.
//
//  Entry: Nothing.
//
//  Returns: True if we succeeded, False if we didn't.
//
int
InitRouting(IPConfigInfo    *ci)
{
    int             i;

    CTERefillMem();

    CTEInitLock(&RouteTableLock);
	
	DefGWConfigured = 0;
	DefGWActive = 0;

	CTEMemSet(&DummyInterface, 0, sizeof(DummyInterface));
    DummyInterface.ri_if.if_xmit = DummyXmit;
    DummyInterface.ri_if.if_transfer = DummyXfer;
    DummyInterface.ri_if.if_close = DummyClose;
    DummyInterface.ri_if.if_invalidate = DummyInvalidate;
    DummyInterface.ri_if.if_qinfo = DummyQInfo;
    DummyInterface.ri_if.if_setinfo = DummySetInfo;
    DummyInterface.ri_if.if_getelist = DummyGetEList;
    DummyInterface.ri_if.if_addaddr = DummyAddAddr;
    DummyInterface.ri_if.if_deladdr = DummyDelAddr;
	DummyInterface.ri_if.if_bcast = IP_LOCAL_BCST;
	DummyInterface.ri_if.if_speed = 10000000;
	DummyInterface.ri_if.if_mtu = 1500;
	DummyInterface.ri_if.if_index = INVALID_IF_INDEX;

	for (i = 0; i < ROUTE_TABLE_SIZE; i++)
        RouteTable[i] = (RouteTableEntry *)NULL;

    // We've created at least one net. We need to add routing table entries for
    // the global broadcast address, as well as for subnet and net broadcasts,
    // and routing entries for the local subnet. We alse need to add a loopback
    // route for the loopback net. Below, we'll add a host route for ourselves
    // through the loopback net.
    AddRoute(LOOPBACK_ADDR & CLASSA_MASK, CLASSA_MASK, IPADDR_LOCAL,
    	LoopNTE->nte_if, LOOPBACK_MSS, 1, IRE_PROTO_LOCAL, ATYPE_PERM, NULL);  	
    											// Route for loopback.
	RouterConfigured = (uchar)ci->ici_gateway;

	CTEInitTimer(&IPRouteTimer);
	
	CTEStartTimer(&IPRouteTimer, IP_ROUTE_TIMEOUT, IPRouteTimeout, NULL);
    return TRUE;

}

//*	InitGateway - Initialize our gateway functionality.
//
//	Called during init. time to initialize our gateway functionality. If we're
//	not connfigured as a router, we do nothing. If we are, we allocate the
//	resources we need and do other router initialization.
//
//	Input:	ci			- Config info.
//
//	Returns:	TRUE if we succeed, FALSE if don't.
//
uint
InitGateway(IPConfigInfo *ci)
{
    uint            FWBufSize, FWPackets;
    uint            FWBufCount;
    NDIS_STATUS     Status;
    NDIS_HANDLE     BufferPool, FWBufferPool, PacketPool;
    IPHeader        *HeaderPtr = NULL;
    uchar           *FWBuffer = NULL;
    PNDIS_BUFFER    Buffer;
    PNDIS_PACKET    Packet;
    RouteInterface  *RtIF;
	NetTableEntry	*NTE;
	uint			i;

    // If we're going to be a router, allocate and initialize the resources we
    // need for that.
	BCastRSQ = NULL;
    if (RouterConfigured) {


		CTERefillMem();
        RtPI = CTEAllocMem(sizeof(ProtInfo));
        if (RtPI == (ProtInfo *)NULL)
            goto failure;

        RtPI->pi_xmitdone = FWSendComplete;

        CTEInitLock(&FWPacketFreeLock);
        CTEInitLock(&FWBufFreeLock);

		MaxFWBufferSize = ci->ici_maxfwbufsize;
		MaxFWPackets = ci->ici_maxfwpackets;
		FWBufSize = MIN(ci->ici_fwbufsize, MaxFWBufferSize);
        FWPackets = MIN(ci->ici_fwpackets, MaxFWPackets);

        for (NTE = NetTableList; NTE != NULL; NTE = NTE->nte_next) {
            RtIF = (RouteInterface *)NTE->nte_if;

            RtIF->ri_q.rsq_qh.fq_next = &RtIF->ri_q.rsq_qh;
            RtIF->ri_q.rsq_qh.fq_prev = &RtIF->ri_q.rsq_qh;
            RtIF->ri_q.rsq_running = FALSE;
            RtIF->ri_q.rsq_pending = 0;
            RtIF->ri_q.rsq_qlength = 0;
            CTEInitLock(&RtIF->ri_q.rsq_lock);
        }
	
        BCastRSQ = CTEAllocMem(sizeof(RouteSendQ));

        if (BCastRSQ == (RouteSendQ *)NULL)
            goto failure;

        BCastRSQ->rsq_qh.fq_next = &BCastRSQ->rsq_qh;
        BCastRSQ->rsq_qh.fq_prev = &BCastRSQ->rsq_qh;
        BCastRSQ->rsq_pending = 0;
        BCastRSQ->rsq_maxpending = DEFAULT_MAX_PENDING;
        BCastRSQ->rsq_qlength = 0;
		BCastRSQ->rsq_running = FALSE;
        CTEInitLock(&BCastRSQ->rsq_lock);

        RtIF = (RouteInterface *)&LoopInterface;
        RtIF->ri_q.rsq_maxpending = DEFAULT_MAX_PENDING;

        // Round the specified size down to a multiple of our FW buf size.
		CTERefillMem();
        FWBufCount = FWBufSize / FW_BUF_SIZE;
        FWBufSize = FWBufCount * FW_BUF_SIZE;

        // Allocate the buffers, packets, and memory for our header buffers.
        HeaderPtr = CTEAllocMem(FWPackets * sizeof(IPHeader));
        if (HeaderPtr == (IPHeader *)NULL)
            goto failure;

        NdisAllocateBufferPool(&Status, &BufferPool, FWPackets);
        if (Status != NDIS_STATUS_SUCCESS) {
            goto failure;						// Couldn't be a router, fail.
        }

        NdisAllocatePacketPool(&Status, &PacketPool, FWPackets, sizeof(FWContext));
        if (Status != NDIS_STATUS_SUCCESS) {
            NdisFreeBufferPool(BufferPool);
            goto failure;
        }

        // Allocate resources for our the buffer pool.
		CTERefillMem();
        FWBuffer = CTEAllocMem(FWBufSize);
        if (FWBuffer == NULL)  {                 // Couldn't get buffer space.
            NdisFreePacketPool(PacketPool);
            NdisFreeBufferPool(BufferPool);
            goto failure;
        }

        NdisAllocateBufferPool(&Status, &FWBufferPool, FWBufCount);
        if (Status != NDIS_STATUS_SUCCESS) {
            NdisFreePacketPool(PacketPool);
            NdisFreeBufferPool(BufferPool);
            goto failure;
        }

        // Everythings allocated. Put it all together and stick them on the
        // free list.
        for (i = 0; i < FWPackets; i++) {
            FWContext       *FWC;

            NdisAllocateBuffer(&Status, &Buffer, BufferPool, HeaderPtr,
            	sizeof(IPHeader));
            if (Status != NDIS_STATUS_SUCCESS)
                DEBUGCHK;
            NdisAllocatePacket(&Status, &Packet, PacketPool);
            if (Status != NDIS_STATUS_SUCCESS)
                DEBUGCHK;

            CTEMemSet(Packet->ProtocolReserved, 0, sizeof(FWContext));
            FWC = (FWContext *)Packet->ProtocolReserved;
            FWC->fc_hndisbuff = Buffer;
            FWC->fc_hbuff = HeaderPtr;
            FWC->fc_pc.pc_common.pc_flags = PACKET_FLAG_FW;
            FWC->fc_pc.pc_common.pc_owner = PACKET_OWNER_IP;
            FWC->fc_pc.pc_pi = RtPI;
            FWC->fc_pc.pc_context = Packet;

            FreeFWPacket(Packet);
            HeaderPtr++;
        }

        for (i = 0; i < FWBufCount; i++) {
            NdisAllocateBuffer(&Status, &Buffer, FWBufferPool, FWBuffer,
            	FW_BUF_SIZE);
            if (Status != NDIS_STATUS_SUCCESS)
                DEBUGCHK;

            Buffer->Next =  FWBufFree;          // BUGBUG portability
            FWBufFree = Buffer;
            FWBuffer += FW_BUF_SIZE;
        }

		CurrentFWPackets = FWPackets;
		CurrentFWBufferSize = FWBufSize;
		

#if 0
        ForwardBCast = (uchar)ci->ici_fwbcast;
#else
        ForwardBCast = FALSE;
#endif
        ForwardPackets = TRUE;
    }
	
	return TRUE;

failure:
	if (RtPI != NULL)
		CTEFreeMem(RtPI);
	if (BCastRSQ != NULL)
		CTEFreeMem(BCastRSQ);
	if (HeaderPtr != NULL)
		CTEFreeMem(HeaderPtr);
	if (FWBuffer != NULL)
		CTEFreeMem(FWBuffer);	

    ForwardBCast = FALSE;
    ForwardPackets = FALSE;
	RouterConfigured = FALSE;
	return FALSE;

}
#pragma END_INIT