aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/pim6sd/pim6_proto.c
blob: 9de795e80ecb60dbe970dc897a5d0a338ca1d155 (plain) (blame)
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
/*
 * Copyright (C) 1999 LSIIT Laboratory.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * Copyright (C) 1998 WIDE Project.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 *  Copyright (c) 1998 by the University of Southern California.
 *  All rights reserved.
 *
 *  Permission to use, copy, modify, and distribute this software and
 *  its documentation in source and binary forms for lawful
 *  purposes and without fee is hereby granted, provided
 *  that the above copyright notice appear in all copies and that both
 *  the copyright notice and this permission notice appear in supporting
 *  documentation, and that any documentation, advertising materials,
 *  and other materials related to such distribution and use acknowledge
 *  that the software was developed by the University of Southern
 *  California and/or Information Sciences Institute.
 *  The name of the University of Southern California may not
 *  be used to endorse or promote products derived from this software
 *  without specific prior written permission.
 *
 *  THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
 *  ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
 *  PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
 *  INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
 *  NON-INFRINGEMENT.
 *
 *  IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
 *  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
 *  TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
 *  THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *  Other copyrights might apply to parts of this software and are so
 *  noted when applicable.
 *
 * $FreeBSD$
 */
/*
 *  Questions concerning this software should be directed to
 *  Mickael Hoerdt (hoerdt@clarinet.u-strasbg.fr) LSIIT Strasbourg.
 *
 */
/*
 * This program has been derived from pim6dd.
 * The pim6dd program is covered by the license in the accompanying file
 * named "LICENSE.pim6dd".
 */
/*
 * This program has been derived from pimd.
 * The pimd program is covered by the license in the accompanying file
 * named "LICENSE.pimd".
 *
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet6/pim6.h>
#include <netinet/ip6.h>
#include <syslog.h>
#include <stdlib.h>
#include "mrt.h"
#include "defs.h"
#include "vif.h"
#include "debug.h"
#include "pim6.h"
#include "pim6_proto.h"
#include "pimd.h"
#include "rp.h"
#include "mld6.h"
#include "timer.h"
#include "route.h"
#include "inet6.h"
#include "kern.h"
#include "routesock.h"

/*
 * Local functions definitions.
 */

static int parse_pim6_hello 			__P((char *pktPtr , int datalen , struct sockaddr_in6 *src,
						     u_int16 *holdtime));

static int send_pim6_register_stop 		__P((struct sockaddr_in6 *reg_src , struct sockaddr_in6 *reg_dst ,
	                       		     	     struct sockaddr_in6 *inner_source,
		                	             struct sockaddr_in6 *inner_grp));

static build_jp_message_t *get_jp6_working_buff __P(());
static void return_jp6_working_buff 		__P((pim_nbr_entry_t * pim_nbr));
static void pack_jp6_message 			__P((pim_nbr_entry_t * pim_nbr));
static void send_jp6_message 			__P((pim_nbr_entry_t * pim_nbr));
static int compare_metrics 			__P((u_int32 local_preference,
		                		     u_int32 local_metric,
		                		     struct sockaddr_in6 *local_address,
		                		     u_int32 remote_preference,
		                		     u_int32 remote_metric,
		                		     struct sockaddr_in6 *remote_address));

build_jp_message_t 	*build_jp_message_pool;
int			build_jp_message_pool_counter;
struct sockaddr_in6 sockaddr6_any = {sizeof(struct sockaddr_in6) , AF_INET6 ,0,0, IN6ADDR_ANY_INIT};
struct sockaddr_in6 sockaddr6_d;

struct pim6dstat pim6dstat;

/************************************************************************
 *                        PIM_HELLO
 ************************************************************************/
int
receive_pim6_hello(src, pim_message, datalen)
    struct sockaddr_in6 *src;
    register char       *pim_message;
    int                 datalen;
{
    mifi_t			mifi;
    struct uvif    		*v;
    register pim_nbr_entry_t 	*nbr,
                   		*prev_nbr,
                   		*new_nbr;
    u_int16         		holdtime;
    int             		bsr_length;
    u_int8         		*data_ptr;
    srcentry_t     		*srcentry_ptr;
    mrtentry_t     		*mrtentry_ptr;


    if ((mifi = find_vif_direct(src)) == NO_VIF)
    {
	/*
	 * Either a local vif or somehow received PIM_HELLO from non-directly
	 * connected router. Ignore it.
	 */

	if (local_address(src) == NO_VIF)
	    log(LOG_INFO, 0, "Ignoring PIM_HELLO from non-neighbor router %s",
		inet6_fmt(&src->sin6_addr));
	return (FALSE);
    }

    v = &uvifs[mifi];
    v->uv_in_pim6_hello++;	/* increment statistacs */
    if (v->uv_flags & (VIFF_DOWN | VIFF_DISABLED | MIFF_REGISTER))
	return (FALSE);		/* Shoudn't come on this interface */

    data_ptr = (u_int8 *) (pim_message + sizeof(struct pim));

    /* Get the Holdtime (in seconds) from the message. Return if error. */

    if (parse_pim6_hello(pim_message, datalen, src, &holdtime) == FALSE)
	return (FALSE);
    IF_DEBUG(DEBUG_PIM_HELLO | DEBUG_PIM_TIMER)
	log(LOG_DEBUG, 0, "PIM HELLO holdtime from %s is %u",
	    inet6_fmt(&src->sin6_addr), holdtime);

    for (prev_nbr = (pim_nbr_entry_t *) NULL, nbr = v->uv_pim_neighbors;
	 nbr != (pim_nbr_entry_t *) NULL;
	 prev_nbr = nbr, nbr = nbr->next)
    {
	/*
	 * The PIM neighbors are sorted in decreasing order of the network
	 * addresses (note that to be able to compare them correctly we must
	 * translate the addresses in host order.
	 */

	if (inet6_lessthan(src, &nbr->address))
	    continue;
	if (inet6_equal(src, &nbr->address))
	{
	    /* We already have an entry for this host */

	    if (0 == holdtime)
	    {
		/*
		 * Looks like we have a nice neighbor who is going down and
		 * wants to inform us by sending "holdtime=0". Thanks buddy
		 * and see you again!
		 */

		log(LOG_INFO, 0, "PIM HELLO received: neighbor %s going down",
		    inet6_fmt(&src->sin6_addr));
		delete_pim6_nbr(nbr);
		return (TRUE);
	    }
	    SET_TIMER(nbr->timer, holdtime);
	    return (TRUE);
	}
	else
	    /*
	     * No entry for this neighbor. Exit the loop and create an entry
	     * for it.
	     */
	    break;
    }

    /*
     * This is a new neighbor. Create a new entry for it. It must be added
     * right after `prev_nbr`
     */

    new_nbr = (pim_nbr_entry_t *) malloc(sizeof(pim_nbr_entry_t));
    new_nbr->address 		= *src;
    new_nbr->vifi 		= mifi;
    SET_TIMER(new_nbr->timer, holdtime);
    new_nbr->build_jp_message 	= (build_jp_message_t *) NULL;
    new_nbr->next 		= nbr;
    new_nbr->prev 		= prev_nbr;

    if (prev_nbr != (pim_nbr_entry_t *) NULL)
	prev_nbr->next = new_nbr;
    else
	v->uv_pim_neighbors = new_nbr;
    if (new_nbr->next != (pim_nbr_entry_t *) NULL)
	new_nbr->next->prev = new_nbr;

    v->uv_flags &= ~VIFF_NONBRS;
    v->uv_flags |= VIFF_PIM_NBR;

    /* Since a new neighbour has come up, let it know your existence */
    /*
     * XXX: TODO: not in the spec, but probably should send the message after
     * a short random period?
     */

    send_pim6_hello(v, pim_hello_holdtime);

    if (v->uv_flags & VIFF_DR)
    {
	/*
	 * If I am the current DR on that interface, so send an RP-Set
	 * message to the new neighbor.
	 */

	if ((bsr_length = create_pim6_bootstrap_message(pim6_send_buf)))
    		send_pim6(pim6_send_buf, &v->uv_linklocal->pa_addr , src , PIM_BOOTSTRAP,
	     		  bsr_length);


	/* The router with highest network address is the elected DR */
	if (inet6_lessthan(&v->uv_linklocal->pa_addr,&v->uv_pim_neighbors->address))
	{
	    /*
	     * I was the DR, but not anymore. Remove all register_vif from
	     * oif list for all directly connected sources (for vifi).
	     */
	    /* TODO: XXX: first entry is not used! */
	    for (srcentry_ptr = srclist->next;
		 srcentry_ptr != (srcentry_t *) NULL;
		 srcentry_ptr = srcentry_ptr->next)
	    {
		/* If not directly connected source for vifi */

		if ((srcentry_ptr->incoming != mifi)
		    || (srcentry_ptr->upstream != (pim_nbr_entry_t *) NULL))
		    continue;
		for (mrtentry_ptr = srcentry_ptr->mrtlink;
		     mrtentry_ptr != (mrtentry_t *) NULL;
		     mrtentry_ptr = mrtentry_ptr->srcnext)
		{
		    if (!(mrtentry_ptr->flags & MRTF_SG))
			continue;	/* This is not (S,G) entry */
		    /* Remove the register oif */
		    IF_CLR(reg_vif_num, &mrtentry_ptr->joined_oifs);
		    change_interfaces(mrtentry_ptr,
				      mrtentry_ptr->incoming,
				      &mrtentry_ptr->joined_oifs,
				      &mrtentry_ptr->pruned_oifs,
				      &mrtentry_ptr->leaves,
				      &mrtentry_ptr->asserted_oifs, 0);
		}
	    }
	    v->uv_flags &= ~VIFF_DR;
	    v->uv_flags &= ~VIFF_QUERIER;

	}
    }

    /*
     * TODO: XXX: does a new neighbor change any routing entries info? Need
     * to trigger joins?
     */

    IF_DEBUG(DEBUG_PIM_HELLO)
	log(LOG_DEBUG,0,"I'have got a new neighbor %s on vif %s",inet6_fmt(&src->sin6_addr),v->uv_name);
    return (TRUE);
}


void
delete_pim6_nbr(nbr_delete)
    pim_nbr_entry_t *nbr_delete;
{
    srcentry_t     *srcentry_ptr;
    srcentry_t     *srcentry_ptr_next;
    mrtentry_t     *mrtentry_ptr;
    mrtentry_t     *mrtentry_srcs;
    grpentry_t     *grpentry_ptr;
    pim_nbr_entry_t *new_nbr;
    cand_rp_t      *cand_rp_ptr;
    rp_grp_entry_t *rp_grp_entry_ptr;
    rpentry_t      *rpentry_ptr;
    struct uvif    *v;

    v = &uvifs[nbr_delete->vifi];

    /* Delete the entry from the pim_nbrs chain */

    if (nbr_delete->prev != (pim_nbr_entry_t *) NULL)
	nbr_delete->prev->next = nbr_delete->next;
    else
	v->uv_pim_neighbors = nbr_delete->next;
    if (nbr_delete->next != (pim_nbr_entry_t *) NULL)
	nbr_delete->next->prev = nbr_delete->prev;

    return_jp6_working_buff(nbr_delete);

    if (v->uv_pim_neighbors == (pim_nbr_entry_t *) NULL)
    {
	/* This was our last neighbor. */
	v->uv_flags &= ~VIFF_PIM_NBR;
	v->uv_flags |= (VIFF_NONBRS | VIFF_DR);
    }
    else
    {
	if  (inet6_greaterthan(&v->uv_linklocal->pa_addr,
	    &v->uv_pim_neighbors->address))

	    /*
	     * The first address is the new potential remote DR address, but
	     * the local address is the winner.
	     */

	    v->uv_flags |= VIFF_DR;
   	    v->uv_flags |= VIFF_QUERIER;
    }

    /* Update the source entries */
    for (srcentry_ptr = srclist; srcentry_ptr != (srcentry_t *) NULL;
	 srcentry_ptr = srcentry_ptr_next)
    {
	srcentry_ptr_next = srcentry_ptr->next;

	if (srcentry_ptr->upstream != nbr_delete)
	    continue;

	/* Reset the next hop (PIM) router */

	if (set_incoming(srcentry_ptr, PIM_IIF_SOURCE) == FALSE)
	{
	    /*
	     * Coudn't reset it. Sorry, the hext hop router toward that
	     * source is probably not a PIM router, or cannot find route at
	     * all, hence I cannot handle this source and have to delete it.
	     */
	    delete_srcentry(srcentry_ptr);
	}
	else
	    if (srcentry_ptr->upstream != (pim_nbr_entry_t *) NULL)
	    {
		/* Ignore the local or directly connected sources */
		/*
		 * Browse all MRT entries for this source and reset the
		 * upstream router. Note that the upstream router is not
		 * always toward the source: it could be toward the RP for
		 * example.
		 */
		new_nbr = srcentry_ptr->upstream;
		for (mrtentry_ptr = srcentry_ptr->mrtlink;
		     mrtentry_ptr != (mrtentry_t *) NULL;
		     mrtentry_ptr = mrtentry_ptr->srcnext)
		{
		    if (!(mrtentry_ptr->flags & MRTF_RP))
		    {
			mrtentry_ptr->upstream = srcentry_ptr->upstream;
			mrtentry_ptr->metric = srcentry_ptr->metric;
			mrtentry_ptr->preference = srcentry_ptr->preference;
			change_interfaces(mrtentry_ptr, srcentry_ptr->incoming,
					  &mrtentry_ptr->joined_oifs,
					  &mrtentry_ptr->pruned_oifs,
					  &mrtentry_ptr->leaves,
					  &mrtentry_ptr->asserted_oifs, 0);
		    }
		}
	    }
    }

    /* Update the RP entries */
    for (cand_rp_ptr = cand_rp_list; cand_rp_ptr != (cand_rp_t *) NULL;
	 cand_rp_ptr = cand_rp_ptr->next)
    {
	if (cand_rp_ptr->rpentry->upstream != nbr_delete)
	    continue;
	rpentry_ptr = cand_rp_ptr->rpentry;
	/* Reset the RP entry iif */
	/* TODO: check if error setting the iif! */
	if (local_address(&rpentry_ptr->address) == NO_VIF)
	{
		   set_incoming(rpentry_ptr, PIM_IIF_RP);
	}
	else
	{
	    rpentry_ptr->incoming = reg_vif_num;
	    rpentry_ptr->upstream = (pim_nbr_entry_t *) NULL;
	}
	mrtentry_ptr = rpentry_ptr->mrtlink;
	if (mrtentry_ptr != (mrtentry_t *) NULL)
	{
	    mrtentry_ptr->upstream = rpentry_ptr->upstream;
	    mrtentry_ptr->metric = rpentry_ptr->metric;
	    mrtentry_ptr->preference = rpentry_ptr->preference;
	    change_interfaces(mrtentry_ptr,
			      rpentry_ptr->incoming,
			      &mrtentry_ptr->joined_oifs,
			      &mrtentry_ptr->pruned_oifs,
			      &mrtentry_ptr->leaves,
			      &mrtentry_ptr->asserted_oifs, 0);
	}
	/* Update the group entries for this RP */
	for (rp_grp_entry_ptr = cand_rp_ptr->rp_grp_next;
	     rp_grp_entry_ptr != (rp_grp_entry_t *) NULL;
	     rp_grp_entry_ptr = rp_grp_entry_ptr->rp_grp_next)
	{
	    for (grpentry_ptr = rp_grp_entry_ptr->grplink;
		 grpentry_ptr != (grpentry_t *) NULL;
		 grpentry_ptr = grpentry_ptr->rpnext)
	    {
		mrtentry_ptr = grpentry_ptr->grp_route;
		if (mrtentry_ptr != (mrtentry_t *) NULL)
		{
		    mrtentry_ptr->upstream = rpentry_ptr->upstream;
		    mrtentry_ptr->metric = rpentry_ptr->metric;
		    mrtentry_ptr->preference = rpentry_ptr->preference;
		    change_interfaces(mrtentry_ptr,
				      rpentry_ptr->incoming,
				      &mrtentry_ptr->joined_oifs,
				      &mrtentry_ptr->pruned_oifs,
				      &mrtentry_ptr->leaves,
				      &mrtentry_ptr->asserted_oifs, 0);
		}
		/* Update only the (S,G)RPbit entries for this group */
		for (mrtentry_srcs = grpentry_ptr->mrtlink;
		     mrtentry_srcs != (mrtentry_t *) NULL;
		     mrtentry_srcs = mrtentry_srcs->grpnext)
		{
		    if (mrtentry_srcs->flags & MRTF_RP)
		    {
			mrtentry_ptr->upstream = rpentry_ptr->upstream;
			mrtentry_ptr->metric = rpentry_ptr->metric;
			mrtentry_ptr->preference = rpentry_ptr->preference;
			change_interfaces(mrtentry_srcs,
					  rpentry_ptr->incoming,
					  &mrtentry_srcs->joined_oifs,
					  &mrtentry_srcs->pruned_oifs,
					  &mrtentry_srcs->leaves,
					  &mrtentry_srcs->asserted_oifs, 0);
		    }
		}
	    }
	}
    }

    free((char *) nbr_delete);
}


/* TODO: simplify it! */
static int
parse_pim6_hello(pim_message, datalen, src, holdtime)
    char           	*pim_message;
    int             	datalen;
    struct sockaddr_in6 *src;
    u_int16        	*holdtime;
{
    u_int8         *pim_hello_message;
    u_int8         *data_ptr;
    u_int16         option_type;
    u_int16         option_length;
    int             holdtime_received_ok = FALSE;
    int             option_total_length;

    pim_hello_message = (u_int8 *) (pim_message + sizeof(struct pim));
    datalen -= sizeof(struct pim);
    for (; datalen >= sizeof(pim_hello_t);)
    {
	/* Ignore any data if shorter than (pim_hello header) */
	data_ptr = pim_hello_message;
	GET_HOSTSHORT(option_type, data_ptr);
	GET_HOSTSHORT(option_length, data_ptr);
	switch (option_type)
	{
	case PIM_MESSAGE_HELLO_HOLDTIME:
	    if (PIM_MESSAGE_HELLO_HOLDTIME_LENGTH != option_length)
	    {
		IF_DEBUG(DEBUG_PIM_HELLO)
		    log(LOG_DEBUG, 0,
		    "PIM HELLO Holdtime from %s: invalid OptionLength = %u",
			inet6_fmt(&src->sin6_addr), option_length);
		return (FALSE);
	    }
	    GET_HOSTSHORT(*holdtime, data_ptr);
	    holdtime_received_ok = TRUE;
	    break;
	default:
	    /* Ignore any unknown options */
	    break;
	}

	/* Move to the next option */
	/*
	 * XXX: TODO: If we are padding to the end of the 32 bit boundary,
	 * use the first method to move to the next option, otherwise simply
	 * (sizeof(pim_hello_t) + option_length).
	 */

#ifdef BOUNDARY_32_BIT
	option_total_length = (sizeof(pim_hello_t) + (option_length & ~0x3) +
			       ((option_length & 0x3) ? 4 : 0));
#else
	option_total_length = (sizeof(pim_hello_t) + option_length);
#endif				/* BOUNDARY_32_BIT */
	datalen -= option_total_length;
	pim_hello_message += option_total_length;
    }
    return (holdtime_received_ok);
}


int
send_pim6_hello(v, holdtime)
    struct uvif    *v;
    u_int16         holdtime;
{
    char           *buf;
    u_int8         *data_ptr;

    int             datalen;

    buf = pim6_send_buf + sizeof(struct pim);
    data_ptr = (u_int8 *) buf;
    PUT_HOSTSHORT(PIM_MESSAGE_HELLO_HOLDTIME, data_ptr);
    PUT_HOSTSHORT(PIM_MESSAGE_HELLO_HOLDTIME_LENGTH, data_ptr);
    PUT_HOSTSHORT(holdtime, data_ptr);

    datalen = data_ptr - (u_int8 *) buf;

    send_pim6(pim6_send_buf, &v->uv_linklocal->pa_addr,
	      &allpim6routers_group, PIM_HELLO, datalen);
    SET_TIMER(v->uv_pim_hello_timer, pim_hello_period);

    v->uv_out_pim6_hello++;
    return (TRUE);
}

/************************************************************************
 *                        PIM_REGISTER
 ************************************************************************/
/*
 * TODO: XXX: IF THE BORDER BIT IS SET, THEN FORWARD THE WHOLE PACKET FROM
 * USER SPACE AND AT THE SAME TIME IGNORE ANY CACHE_MISS SIGNALS FROM THE
 * KERNEL.
 */

int
receive_pim6_register(reg_src, reg_dst, pim_message, datalen)
    struct sockaddr_in6     *reg_src,
                    		*reg_dst;
    char           			*pim_message;
    int             		datalen;
{
    struct sockaddr_in6		inner_src,
                    		inner_grp;
    pim_register_t		*register_p;
    struct ip6_hdr    		*ip;
    u_int32         		borderBit,
                    		nullRegisterBit;
    mrtentry_t 			*mrtentry_ptr;
    mrtentry_t 			*mrtentry_ptr2;
    if_set			oifs;

    pim6dstat.in_pim6_register++;

    register_p = (pim_register_t *) (pim_message + sizeof(struct pim));

    borderBit = ntohl(register_p->reg_flags) & PIM_MESSAGE_REGISTER_BORDER_BIT;
    nullRegisterBit =
	ntohl(register_p->reg_flags) & PIM_MESSAGE_REGISTER_NULL_REGISTER_BIT;

    /* initialize the pointer to the encapsulated packet */
    ip = (struct ip6_hdr *) (register_p + 1);

    /*
     * We are keeping all addresses in network order,
     * so no need for byte order translation.
     */
    inner_src.sin6_addr = ip->ip6_src;
    inner_grp.sin6_addr = ip->ip6_dst;

    /* scope validation of the inner source and destination addresses */
    if (IN6_IS_ADDR_LINKLOCAL(&ip->ip6_src)) {
	    log(LOG_WARNING, 0,
		"receive_pim6_register: inner source(%s) has invalid scope",
		inet6_fmt(&ip->ip6_src));
    }
#ifdef notyet
    if (IN6_IS_ADDR_SITELOCAL)
	    inner_src.sin6_scope_id = addr2scopeid(&ip->ip6_src, &ip->ip6_dst,
						   reg_src, reg_dst);
    else
#endif
	    inner_src.sin6_scope_id = 0;

    if (IN6_IS_ADDR_MC_NODELOCAL(&ip->ip6_dst) ||
	IN6_IS_ADDR_MC_LINKLOCAL(&ip->ip6_dst)) {
	    log(LOG_WARNING, 0,
		"receive_pim6_register: inner group(%s) has invalid scope",
		inet6_fmt(&ip->ip6_dst));
	    return(FALSE);	/* XXX: can we discard it? */
    }
#ifdef notyet
    if (IN6_IS_ADDR_MC_SITELOCAL(&ip->ip6_dst))
	    inner_grp.sin6_scope_id = addr2scopeid(&ip->ip6_src, &ip->ip6_dst,
						   reg_src, reg_dst);
    else
	    inner_grp.sin6_scope_id = 0;
#endif
    inner_grp.sin6_scope_id = 0;

    mrtentry_ptr = find_route(&inner_src, &inner_grp,
			      MRTF_SG | MRTF_WC | MRTF_PMBR, DONT_CREATE);

    if (mrtentry_ptr == (mrtentry_t *) NULL)
    {

	/* No routing entry. Send REGISTER_STOP and return. */

	IF_DEBUG(DEBUG_PIM_REGISTER)
	    log(LOG_DEBUG, 0,
		"No routing entry for source %s and/or group %s",
		inet6_fmt(&inner_src.sin6_addr), inet6_fmt(&inner_grp.sin6_addr));

	/* TODO: XXX: shouldn't be inner_src=IN6ADDR_ANY? Not in the spec. */

	send_pim6_register_stop(reg_dst, reg_src, &inner_grp, &inner_src);
	return (TRUE);
    }

    /* XXX: not in the spec: check if I am the RP for that group */
	if (!inet6_equal(&my_cand_rp_address, reg_dst)
	|| (check_mrtentry_rp(mrtentry_ptr, &my_cand_rp_address) == FALSE))
    {
	send_pim6_register_stop(reg_dst, reg_src, &inner_grp, &inner_src);
	return (TRUE);
    }

    if (mrtentry_ptr->flags & MRTF_SG)
    {

	/* (S,G) found */
	/* TODO: check the timer again */

	SET_TIMER(mrtentry_ptr->timer, pim_data_timeout);	/* restart timer */
	if (!(mrtentry_ptr->flags & MRTF_SPT))
	{
		/* The SPT bit is not set */

	    if (!nullRegisterBit)
	    {
		calc_oifs(mrtentry_ptr, &oifs);
		if (IF_ISEMPTY(&oifs)
		    && (mrtentry_ptr->incoming == reg_vif_num))
		{
		    send_pim6_register_stop(reg_dst, reg_src, &inner_grp,
					   				&inner_src);
		    return (TRUE);
		}

		/*
		 * TODO: XXX: BUG!!! The data will be forwarded by the kernel
		 * MFC!!! Need to set a special flag for this routing entry
		 * so after a cache miss occur, the multicast packet will be
		 * forwarded from user space and won't install entry in the
		 * kernel MFC. The problem is that the kernel MFC doesn't
		 * know the PMBR address and simply sets the multicast
		 * forwarding cache to accept/forward all data coming from
		 * the register_vif.
		 */

		if (borderBit)
		{
		    if (!inet6_equal(&mrtentry_ptr->pmbr_addr,reg_src))
		    {
			send_pim6_register_stop(reg_dst, reg_src,
					       			&inner_grp, &inner_src);
			return (TRUE);

		    }
		}
		return (TRUE);
	    }
	    /* TODO: XXX: if NULL_REGISTER and has (S,G) with SPT=0, then..? */
	    return (TRUE);
	}
	else
	{
	    /* The SPT bit is set */
	    send_pim6_register_stop(reg_dst, reg_src, &inner_grp, &inner_src);
	    return (TRUE);
	}
    }
    if (mrtentry_ptr->flags & (MRTF_WC | MRTF_PMBR))
    {
	if (borderBit)
	{
	    /*
	     * Create (S,G) state. The oifs will be the copied from the
	     * existing (*,G) or (*,*,RP) entry.
	     */

	    mrtentry_ptr2 = find_route(&inner_src, &inner_grp, MRTF_SG,
				       CREATE);
	    if (mrtentry_ptr2 != (mrtentry_t *) NULL)
	    {
		mrtentry_ptr2->pmbr_addr = *reg_src;

		/* Clear the SPT flag */

		mrtentry_ptr2->flags &= ~(MRTF_SPT | MRTF_NEW);
		SET_TIMER(mrtentry_ptr2->timer, pim_data_timeout);

		/* TODO: explicitly call the Join/Prune send function? */

		FIRE_TIMER(mrtentry_ptr2->jp_timer);	/* Send the Join
							 * immediately */
		/*
		 * TODO: explicitly call this function?
		 * send_pim6_join_prune(mrtentry_ptr2->upstream->vifi,
		 * mrtentry_ptr2->upstream, pim_join_prune_holdtime);
		 */
	    }
	}
    }

    if (mrtentry_ptr->flags & MRTF_WC)
    {
	/* (*,G) entry */

	calc_oifs(mrtentry_ptr, &oifs);
	if (IF_ISEMPTY(&oifs))
	{
	    send_pim6_register_stop(reg_dst, reg_src, &inner_grp, &sockaddr6_any);
	    return (FALSE);
	}

	/* XXX: TODO: check with the spec again */

	else
	{
	    if (!nullRegisterBit)
	    {
		/* Install cache entry in the kernel */
		/*
		 * TODO: XXX: probably redundant here, because the
		 * decapsulated mcast packet in the kernel will result in
		 * CACHE_MISS
		 */

		struct sockaddr_in6    *mfc_source = &inner_src;


#ifdef KERNEL_MFC_WC_G
		if (!(mrtentry_ptr->flags & MRTF_MFC_CLONE_SG))
		    mfc_source = NULL;
#endif				/* KERNEL_MFC_WC_G */

		add_kernel_cache(mrtentry_ptr, mfc_source, &inner_grp, 0);
		k_chg_mfc(mld6_socket, mfc_source, &inner_grp,
			  mrtentry_ptr->incoming, &mrtentry_ptr->oifs,
			  &mrtentry_ptr->group->rpaddr);
		return (TRUE);
	    }
	}
	return (TRUE);
    }

    if (mrtentry_ptr->flags & MRTF_PMBR)
    {
	/* (*,*,RP) entry */
	if (!nullRegisterBit)
	{
	    struct sockaddr_in6		*mfc_source = &inner_src;

	    /*
	     * XXX: have to create either (S,G) or (*,G). The choice below is
	     * (*,G)
	     */

	    mrtentry_ptr2 = find_route(NULL, &inner_grp, MRTF_WC,
				       CREATE);
	    if (mrtentry_ptr2 == (mrtentry_t *) NULL)
		return (FALSE);
	    if (mrtentry_ptr2->flags & MRTF_NEW)
	    {
		/* TODO: something else? Have the feeling sth is missing */

		mrtentry_ptr2->flags &= ~MRTF_NEW;

		/* TODO: XXX: copy the timer from the (*,*,RP) entry? */

		COPY_TIMER(mrtentry_ptr->timer, mrtentry_ptr2->timer);
	    }
	    /* Install cache entry in the kernel */

#ifdef KERNEL_MFC_WC_G

	    if (!(mrtentry_ptr->flags & MRTF_MFC_CLONE_SG))
		mfc_source = NULL;
#endif				/* KERNEL_MFC_WC_G */
	    add_kernel_cache(mrtentry_ptr, mfc_source, &inner_grp, 0);
	    k_chg_mfc(mld6_socket, mfc_source, &inner_grp,
		      mrtentry_ptr->incoming, &mrtentry_ptr->oifs,
		      &mrtentry_ptr2->group->rpaddr);

	    return (TRUE);
	}
    }

    /* Shoudn't happen: invalid routing entry? */
    /* XXX: TODO: shoudn't be inner_src=IN6ADDR_ANY? Not in the spec. */

    send_pim6_register_stop(reg_dst, reg_src, &inner_grp, &inner_src);
    return (TRUE);
}


int
send_pim6_register(pkt)
    char *pkt;
{
    register struct ip6_hdr *ip6;
    static struct sockaddr_in6		source= {sizeof(source) , AF_INET6 };
    static struct sockaddr_in6 		group= {sizeof(group) , AF_INET6 };
    mifi_t          		mifi;
    rpentry_t      		*rpentry_ptr;
    mrtentry_t     		*mrtentry_ptr;
    mrtentry_t     		*mrtentry_ptr2;

    struct sockaddr_in6		*reg_src,
                    		*reg_dst;
    int             		pktlen = 0;
    char           		*buf;

    ip6=(struct ip6_hdr *)pkt;

    group.sin6_addr = ip6->ip6_dst;
    source.sin6_addr = ip6->ip6_src;

    if ((mifi = find_vif_direct_local(&source)) == NO_VIF)
	return (FALSE);

    if (!(uvifs[mifi].uv_flags & VIFF_DR))
	return (FALSE);		/* I am not the DR for that subnet */



    rpentry_ptr = rp_match(&group);
    if (rpentry_ptr == (rpentry_t *) NULL)
	return (FALSE);		/* No RP for this group */
    if (local_address(&rpentry_ptr->address) != NO_VIF)
	/* TODO: XXX: not sure it is working! */
	return (FALSE);		/* I am the RP for this group */

    mrtentry_ptr = find_route(&source, &group, MRTF_SG, CREATE);
    if (mrtentry_ptr == (mrtentry_t *) NULL)
	return (FALSE);		/* Cannot create (S,G) state */

    if (mrtentry_ptr->flags & MRTF_NEW)
    {
	/* A new entry */
	mrtentry_ptr->flags &= ~MRTF_NEW;
	RESET_TIMER(mrtentry_ptr->rs_timer);	/* Reset the
						 * Register-Suppression timer */
	if ((mrtentry_ptr2 = mrtentry_ptr->group->grp_route) ==
	    (mrtentry_t *) NULL)
	    mrtentry_ptr2 =
		mrtentry_ptr->group->active_rp_grp->rp->rpentry->mrtlink;
	if (mrtentry_ptr2 != (mrtentry_t *) NULL)
	{
	    FIRE_TIMER(mrtentry_ptr2->jp_timer);	/* Timeout the
							 * Join/Prune timer */
	    /*
	     * TODO: explicitly call this function?
	     * send_pim6_join_prune(mrtentry_ptr2->upstream->vifi,
	     * mrtentry_ptr2->upstream, pim_join_prune_holdtime);
	     */
	}
    }
    /* Restart the (S,G) Entry-timer */

    SET_TIMER(mrtentry_ptr->timer, pim_data_timeout);

    IF_TIMER_NOT_SET(mrtentry_ptr->rs_timer)
    {
	/*
	 * The Register-Suppression Timer is not running. Encapsulate the
	 * data and send to the RP.
	 */
	buf = pim6_send_buf + sizeof(struct pim);

	bzero(buf, sizeof(pim_register_t));	/* No flags set */
	buf += sizeof(pim_register_t);

	/* Copy the data packet at the back of the register packet */
	/* TODO: check pktlen. ntohs? */

	pktlen = ntohs(ip6->ip6_plen);
	pktlen +=sizeof(struct ip6_hdr);			/* XXX */

	bcopy((char *) ip6, buf, pktlen);
	pktlen += sizeof(pim_register_t);
	reg_src = max_global_address();
	reg_dst = &mrtentry_ptr->group->rpaddr;

    	send_pim6(pim6_send_buf, reg_src , reg_dst , PIM_REGISTER,
	     	  pktlen);

	pim6dstat.out_pim6_register++;

	return (TRUE);
    }
    return (TRUE);
}

int
send_pim6_null_register(mrtentry_ptr)
    mrtentry_t     *mrtentry_ptr;
{
	struct 				ip6_hdr *ip;
    pim_register_t 		*pim_register;
    int             		pktlen = 0;
    mifi_t          		mifi;
    struct sockaddr_in6  	*reg_source,
                    		*dest;

    /* No directly connected source; no local address */

    if ((mifi = find_vif_direct_local(&mrtentry_ptr->source->address)) == NO_VIF)
	return (FALSE);

    pim_register = (pim_register_t *) (pim6_send_buf  + sizeof(struct pim));
    bzero((char *) pim_register, sizeof(pim_register_t));
    pim_register->reg_flags = htonl(pim_register->reg_flags
				  | PIM_MESSAGE_REGISTER_NULL_REGISTER_BIT);

    /* include the dummy ip header */
    ip = (struct ip6_hdr *) (pim_register +1);
    ip->ip6_plen= 0;
    ip->ip6_flow=0;
    ip->ip6_vfc = 0x60;
    ip->ip6_hlim = MINHLIM;
    ip->ip6_nxt = IPPROTO_NONE;
    ip->ip6_src = mrtentry_ptr->source->address.sin6_addr;
    ip->ip6_dst = mrtentry_ptr->group->group.sin6_addr;

    pktlen = sizeof(pim_register_t) + sizeof(struct ip6_hdr);

    dest = &mrtentry_ptr->group->rpaddr;
    reg_source = max_global_address();

    send_pim6(pim6_send_buf, reg_source , dest, PIM_REGISTER,
	      pktlen);
    pim6dstat.out_pim6_register++; /* should be counted separately? */

    return (TRUE);
}


/************************************************************************
 *                        PIM_REGISTER_STOP
 ************************************************************************/
int
receive_pim6_register_stop(reg_src, reg_dst, pim_message, datalen)
    struct sockaddr_in6 	*reg_src,
                    		*reg_dst;
    char           		*pim_message;
    register int    		datalen;
{
    pim_register_stop_t 	*pim_regstop_p;
    pim6_encod_grp_addr_t 	encod_grp;
    pim6_encod_uni_addr_t 	encod_unisrc;
    struct sockaddr_in6		source,
							group;
    u_int8         		*data_ptr;
    mrtentry_t     		*mrtentry_ptr;
    if_set     			pruned_oifs;
    mifi_t			mifi;
    struct uvif 		*v;

    pim6dstat.in_pim6_register_stop++;

    pim_regstop_p = (pim_register_stop_t *) (pim_message +
					     sizeof(struct pim));
    data_ptr = (u_int8 *) & pim_regstop_p->encod_grp;
    GET_EGADDR6(&encod_grp, data_ptr);
    GET_EUADDR6(&encod_unisrc, data_ptr);

    group.sin6_addr = encod_grp.mcast_addr;

    /* scope validation of the inner source and destination addresses */

#ifdef notyet
    if (IN6_IS_ADDR_MC_SITELOCAL(&ip->ip6_dst))
	    group.sin6_scope_id = addr2scopeid(&ip->ip6_src, &ip->ip6_dst,
						   reg_src, reg_dst);
    else
#endif

    group.sin6_scope_id = 0;
    source.sin6_addr = encod_unisrc.unicast_addr;

    /* the source address must be global...but is it always true? */

#ifdef notyet
    if (IN6_IS_ADDR_SITELOCAL)
	    source.sin6_scope_id = addr2scopeid(&ip->ip6_src, &ip->ip6_dst,
						   reg_src, reg_dst);
    else
#endif

    source.sin6_scope_id = 0;

    if((mifi= find_vif_direct_local(&source))==NO_VIF)
    {
	    IF_DEBUG(DEBUG_PIM_REGISTER)
		    {
			    log(LOG_WARNING,0,
				"Received PIM_REGISTER_STOP from RP %s for a non "
				"direct-connect source %s",
				inet6_fmt(&reg_src->sin6_addr),
				inet6_fmt(&encod_unisrc.unicast_addr));
		    }
	    return FALSE;
    }

    v=&uvifs[mifi];


    group.sin6_scope_id = inet6_uvif2scopeid(&group, v);
    source.sin6_scope_id = inet6_uvif2scopeid(&source,
					      v);


    IF_DEBUG(DEBUG_PIM_REGISTER)
	{
		log(LOG_DEBUG, 0,
		    "Received PIM_REGISTER_STOP from RP %s to %s "
		    "source : %s group : %s",
		    inet6_fmt(&reg_src->sin6_addr),
		    inet6_fmt(&reg_dst->sin6_addr),
		    inet6_fmt(&encod_unisrc.unicast_addr),
		    inet6_fmt(&encod_grp.mcast_addr));
	}

    /* TODO: apply the group mask and do register_stop for all grp addresses */
    /* TODO: check for SourceAddress == 0 */


    mrtentry_ptr = find_route(&source, &group,
			      MRTF_SG, DONT_CREATE);
    if (mrtentry_ptr == (mrtentry_t *) NULL)
    {
	return (FALSE);
    }

    /*
     * XXX: not in the spec: check if the PIM_REGISTER_STOP originator is
     * really the RP
     */


    if (check_mrtentry_rp(mrtentry_ptr, reg_src) == FALSE)
    {
	return (FALSE);
    }

    /* restart the Register-Suppression timer */

    SET_TIMER(mrtentry_ptr->rs_timer, (0.5 * pim_register_suppression_timeout)
	      + (RANDOM() % (pim_register_suppression_timeout + 1)));
    /* Prune the register_vif from the outgoing list */

    IF_COPY(&mrtentry_ptr->pruned_oifs, &pruned_oifs);
    IF_SET(reg_vif_num, &pruned_oifs);
    change_interfaces(mrtentry_ptr, mrtentry_ptr->incoming,
		      &mrtentry_ptr->joined_oifs, &pruned_oifs,
		      &mrtentry_ptr->leaves,
		      &mrtentry_ptr->asserted_oifs, 0);
    return (TRUE);
}


/* TODO: optional rate limiting is not implemented yet */
/* Unicasts a REGISTER_STOP message to the DR */

static int
send_pim6_register_stop(reg_src, reg_dst, inner_grp, inner_src)
    struct sockaddr_in6		*reg_src,
                    		*reg_dst,
                    		*inner_grp,
                    		*inner_src;
{
    char           *buf;
    u_int8         *data_ptr;

    buf = pim6_send_buf +  sizeof(struct pim);
    data_ptr = (u_int8 *) buf;
    PUT_EGADDR6(inner_grp->sin6_addr, SINGLE_GRP_MSK6LEN, 0, data_ptr);
    PUT_EUADDR6(inner_src->sin6_addr, data_ptr);

    send_pim6(pim6_send_buf, reg_src , reg_dst , PIM_REGISTER_STOP,
	      data_ptr-(u_int8 *) buf);
    pim6dstat.out_pim6_register_stop++;

    return (TRUE);
}

/************************************************************************
 *                        PIM_JOIN_PRUNE
 ************************************************************************/
int
join_or_prune(mrtentry_ptr, upstream_router)
    mrtentry_t     *mrtentry_ptr;
    pim_nbr_entry_t *upstream_router;
{
    if_set     		entry_oifs;
    mrtentry_t     	*mrtentry_grp;

    if ((mrtentry_ptr == (mrtentry_t *) NULL))
    {
	IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
	    log(LOG_DEBUG,0,"Join_or_prune : mrtentry_ptr is null");
	return (PIM_ACTION_NOTHING);
    }
    if( upstream_router == (pim_nbr_entry_t *) NULL)
    {
	IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
	    log(LOG_DEBUG,0,"Join_or_prune : upstream_router is null");
	return (PIM_ACTION_NOTHING);
    }

    calc_oifs(mrtentry_ptr, &entry_oifs);
    if (mrtentry_ptr->flags & (MRTF_PMBR | MRTF_WC))
    {
	/* (*,*,RP) or (*,G) entry */
	/* The (*,*,RP) or (*,G) J/P messages are sent only toward the RP */

	if (upstream_router != mrtentry_ptr->upstream)
	    return (PIM_ACTION_NOTHING);

	/* TODO: XXX: Can we have (*,*,RP) prune? */

	if (IF_ISEMPTY(&entry_oifs))
	{
	    /* NULL oifs */
	    if (!(uvifs[mrtentry_ptr->incoming].uv_flags & VIFF_DR))
	    {
		/* I am not the DR for that subnet. */
		return (PIM_ACTION_PRUNE);
            }
	    if (IF_ISSET(mrtentry_ptr->incoming, &mrtentry_ptr->leaves))
		/* I am the DR and have local leaves */
		return (PIM_ACTION_JOIN);
	    /* Probably the last local member hast timeout */
	    return (PIM_ACTION_PRUNE);
	}
	return (PIM_ACTION_JOIN);
    }

    if (mrtentry_ptr->flags & MRTF_SG)
    {
	/* (S,G) entry */
	/* TODO: check again */
	if (mrtentry_ptr->upstream == upstream_router)
	{
	    if (!(mrtentry_ptr->flags & MRTF_RP))
	    {
		/* Upstream router toward S */
		if (IF_ISEMPTY(&entry_oifs))
		{
		    if (mrtentry_ptr->group->active_rp_grp != (rp_grp_entry_t *)NULL &&
			inet6_equal(&mrtentry_ptr->group->rpaddr,
				    &my_cand_rp_address))
		    {
			/*
			 * (S,G) at the RP. Don't send Join/Prune (see the
			 * end of Section 3.3.2)
			 */

			return (PIM_ACTION_NOTHING);
		    }
		    return (PIM_ACTION_PRUNE);
		}
		else
		    return (PIM_ACTION_JOIN);
	    }
	    else
	    {
		/* Upstream router toward RP */
		if (IF_ISEMPTY(&entry_oifs))
		    return (PIM_ACTION_PRUNE);
	    }
	}

	/*
	 * Looks like the case when the upstream router toward S is different
	 * from the upstream router toward RP
	 */

	if (mrtentry_ptr->group->active_rp_grp == (rp_grp_entry_t *) NULL)
	    return (PIM_ACTION_NOTHING);
	mrtentry_grp = mrtentry_ptr->group->grp_route;
	if (mrtentry_grp == (mrtentry_t *) NULL)
	    mrtentry_grp =
		mrtentry_ptr->group->active_rp_grp->rp->rpentry->mrtlink;
	if (mrtentry_grp == (mrtentry_t *) NULL)
	    return (PIM_ACTION_NOTHING);
	if (mrtentry_grp->upstream != upstream_router)
	    return (PIM_ACTION_NOTHING);	/* XXX: shoudn't happen */

	if ((!(mrtentry_ptr->flags & MRTF_RP))
	    && (mrtentry_ptr->flags & MRTF_SPT))
	{
	    return (PIM_ACTION_PRUNE);
	}
    }
    return (PIM_ACTION_NOTHING);
}

/* TODO: too long, simplify it! */
#define	PIM6_JOIN_PRUNE_MINLEN (4 + PIM6_ENCODE_UNI_ADDR_LEN + 4)

int
receive_pim6_join_prune(src, dst, pim_message, datalen)
    struct sockaddr_in6		*src,
                    		*dst;
    char           		*pim_message;
    register int    		datalen;
{
    mifi_t          		mifi;
    struct uvif    		*v;
    pim6_encod_uni_addr_t 	uni_target_addr;
    pim6_encod_grp_addr_t 	encod_group;
    pim6_encod_src_addr_t 	encod_src;
    u_int8         		*data_ptr;
    u_int8         		*data_ptr_start;
    u_int8          		num_groups;
    u_int8          		num_groups_tmp;
    int             		star_star_rp_found;
    u_int16         		holdtime;
    u_int16         		num_j_srcs;
    u_int16         		num_j_srcs_tmp;
    u_int16         		num_p_srcs;
    struct sockaddr_in6		source;
    struct sockaddr_in6         group;
    struct sockaddr_in6		target;
    struct in6_addr          	s_mask;
    struct in6_addr         	g_mask;
    u_int8          		s_flags;
    u_int8          		reserved;
    rpentry_t      		*rpentry_ptr;
    mrtentry_t     		*mrtentry_ptr;
    mrtentry_t     		*mrtentry_srcs;
    mrtentry_t     		*mrtentry_rp;
    grpentry_t     		*grpentry_ptr;
    u_int16         		jp_value;
    pim_nbr_entry_t 		*upstream_router;
    int             		my_action;
    int             		ignore_group;
    rp_grp_entry_t 		*rp_grp_entry_ptr;
    u_int8         		*data_ptr_group_j_start;
    u_int8         		*data_ptr_group_p_start;

    if ((mifi = find_vif_direct(src)) == NO_VIF)
    {
	/*
	 * Either a local vif or somehow received PIM_JOIN_PRUNE from
	 * non-directly connected router. Ignore it.
	 */

	if (local_address(src) == NO_VIF)
	    log(LOG_INFO, 0,
		"Ignoring PIM_JOIN_PRUNE from non-neighbor router %s",
		inet6_fmt(&src->sin6_addr));
	return (FALSE);
    }

    v = &uvifs[mifi];
    v->uv_in_pim6_join_prune++;
    if (uvifs[mifi].uv_flags &
	(VIFF_DOWN | VIFF_DISABLED | VIFF_NONBRS | MIFF_REGISTER))
    {
	return (FALSE);		/* Shoudn't come on this interface */
    }

    /* sanity check for the minimum length */
    if (datalen < PIM6_JOIN_PRUNE_MINLEN) {
	    log(LOG_NOTICE, 0,
		"receive_pim6_join_prune: Join/Prune message size(%u) is"
		" too short from %s on %s",
		datalen, inet6_fmt(&src->sin6_addr), v->uv_name);
	    return(FALSE);
    }
    datalen -= PIM6_JOIN_PRUNE_MINLEN;
    data_ptr = (u_int8 *) (pim_message + sizeof(struct pim));

    /* Get the target address */
    GET_EUADDR6(&uni_target_addr, data_ptr);
    GET_BYTE(reserved, data_ptr);
    GET_BYTE(num_groups, data_ptr);
    if (num_groups == 0)
	return (FALSE);		/* No indication for groups in the message */
    GET_HOSTSHORT(holdtime, data_ptr);
    target.sin6_len = sizeof(target);
    target.sin6_family = AF_INET6;
    target.sin6_addr = uni_target_addr.unicast_addr;
    target.sin6_scope_id = inet6_uvif2scopeid(&target, v);

    /* Sanity check for the message length through all the groups */
    num_groups_tmp = num_groups;
    data_ptr_start = data_ptr;
    while (num_groups_tmp--) {
	int srclen;

	/* group addr + #join + #src */
	if (datalen < PIM6_ENCODE_GRP_ADDR_LEN + sizeof(u_int32_t)) {
	    log(LOG_NOTICE, 0,
		"receive_pim6_join_prune: Join/Prune message from %s on %s is"
		" too short to contain enough data",
		inet6_fmt(&src->sin6_addr), v->uv_name);
	    return(FALSE);
	}
	datalen -= (PIM6_ENCODE_GRP_ADDR_LEN + sizeof(u_int32_t));
	data_ptr += PIM6_ENCODE_GRP_ADDR_LEN;

	/* joined source addresses and pruned source addresses */
	GET_HOSTSHORT(num_j_srcs, data_ptr);
	GET_HOSTSHORT(num_p_srcs, data_ptr);
	srclen = (num_j_srcs + num_p_srcs) * PIM6_ENCODE_SRC_ADDR_LEN;
	if (datalen < srclen) {
	    log(LOG_NOTICE, 0,
		"receive_pim6_join_prune: Join/Prune message from %s on %s is"
		" too short to contain enough data",
		inet6_fmt(&src->sin6_addr), v->uv_name);
	    return(FALSE);
	}
	datalen -= srclen;
	data_ptr += srclen;
    }
    data_ptr = data_ptr_start;
    num_groups_tmp = num_groups;

    if (!inet6_localif_address(&target, v) &&
	!IN6_IS_ADDR_UNSPECIFIED(&uni_target_addr.unicast_addr))
    {

	/* if I am not the targer of the join message */
	/*
	 * Join/Prune suppression code. This either modifies the J/P timers
	 * or triggers an overriding Join.
	 */
	/*
	 * Note that if we have (S,G) prune and (*,G) Join, we must send them
	 * in the same message. We don't bother to modify both timers here.
	 * The Join/Prune sending function will take care of that.
	 */

	upstream_router = find_pim6_nbr(&target);
	if (upstream_router == (pim_nbr_entry_t *) NULL)
	    return (FALSE);	/* I have no such neighbor */

	group.sin6_len = sizeof(group);
	group.sin6_family = AF_INET6;
	source.sin6_len = sizeof(source);
	source.sin6_family = AF_INET6;

	while (num_groups--)
	{
	    GET_EGADDR6(&encod_group, data_ptr);
	    GET_HOSTSHORT(num_j_srcs, data_ptr);
	    GET_HOSTSHORT(num_p_srcs, data_ptr);
	    if (encod_group.masklen > (sizeof(struct in6_addr) << 3))
		continue;
	    MASKLEN_TO_MASK6(encod_group.masklen, g_mask);
	    group.sin6_addr = encod_group.mcast_addr;
	    group.sin6_scope_id = inet6_uvif2scopeid(&group, v);

	    if (!IN6_IS_ADDR_MULTICAST(&group.sin6_addr))
	    {
		data_ptr +=
		    (num_j_srcs + num_p_srcs) * sizeof(pim6_encod_src_addr_t);
		continue;	/* Ignore this group and jump to the next */
	    }

	    if (inet6_equal(&group,&sockaddr6_d) &&
		(encod_src.masklen == STAR_STAR_RP_MSK6LEN))
	    {
		/* (*,*,RP) Join suppression */

		while (num_j_srcs--)
		{
		    GET_ESADDR6(&encod_src, data_ptr);
		    source.sin6_addr = encod_src.src_addr;
		    source.sin6_scope_id = inet6_uvif2scopeid(&source,
                                      v);
			/* sanity checks */
		    if (!inet6_valid_host(&source))
				continue;
		    if (encod_src.masklen >
                    (sizeof(struct in6_addr) << 3))
                    continue;


	   	    s_flags = encod_src.flags;
		    MASKLEN_TO_MASK6(encod_src.masklen, s_mask);
		    if ((s_flags & USADDR_RP_BIT) &&
			(s_flags & USADDR_WC_BIT))
		    {
			/* This is the RP address. */
			rpentry_ptr = rp_find(&source);
			if (rpentry_ptr == (rpentry_t *) NULL)
			    continue;	/* Don't have such RP. Ignore */
			mrtentry_rp = rpentry_ptr->mrtlink;
			my_action = join_or_prune(mrtentry_rp,
						  upstream_router);
			if (my_action != PIM_ACTION_JOIN)
			    continue;

			/* Check the holdtime */
			/* TODO: XXX: TIMER implem. dependency! */

			if (mrtentry_rp->jp_timer > holdtime)
			    continue;
			if ((mrtentry_rp->jp_timer == holdtime)
			    && (inet6_greaterthan(src, &v->uv_linklocal->pa_addr)))
			    continue;

			/*
			 * Set the Join/Prune suppression timer for this
			 * routing entry by increasing the current Join/Prune
			 * timer.
			 */
			jp_value = pim_join_prune_period +
			    0.5 * (RANDOM() % pim_join_prune_period);
			/* TODO: XXX: TIMER implem. dependency! */

			if (mrtentry_rp->jp_timer < jp_value)
			    SET_TIMER(mrtentry_rp->jp_timer, jp_value);
		    }
		}		/* num_j_srcs */

		while (num_p_srcs--)
		{
		    /*
		     * TODO: XXX: Can we have (*,*,RP) prune message? Not in
		     * the spec, but anyway, the code below can handle them:
		     * either suppress the local (*,*,RP) prunes or override
		     * the prunes by sending (*,*,RP) and/or (*,G) and/or
		     * (S,G) Join.
		     */

		    GET_ESADDR6(&encod_src, data_ptr);
		    source.sin6_addr = encod_src.src_addr;
		    source.sin6_scope_id = inet6_uvif2scopeid(&source,
                                      v);

		    if (!inet6_valid_host(&source))
			continue;

                    if (encod_src.masklen >
                    (sizeof(struct in6_addr) << 3))
                    continue;


		    s_flags = encod_src.flags;
		    MASKLEN_TO_MASK6(encod_src.masklen, s_mask);
		    if ((s_flags & USADDR_RP_BIT) &&
			(s_flags & USADDR_WC_BIT))
		    {
			/* This is the RP address. */
			rpentry_ptr = rp_find(&source);
			if (rpentry_ptr == (rpentry_t *) NULL)
			    continue;	/* Don't have such RP. Ignore */
			mrtentry_rp = rpentry_ptr->mrtlink;
			my_action = join_or_prune(mrtentry_rp,
						  upstream_router);
			if (my_action == PIM_ACTION_PRUNE)
			{
			    /* TODO: XXX: TIMER implem. dependency! */
			    if ((mrtentry_rp->jp_timer < holdtime)
				|| ((mrtentry_rp->jp_timer == holdtime)
			    	&& (inet6_greaterthan(src, &v->uv_linklocal->pa_addr))))
			    {
				/* Suppress the Prune */
				jp_value =  pim_join_prune_period+
				    0.5 * (RANDOM() % pim_join_prune_period);
				if (mrtentry_rp->jp_timer < jp_value)
				    SET_TIMER(mrtentry_rp->jp_timer, jp_value);
			    }
			}
			else
			    if (my_action == PIM_ACTION_JOIN)
			    {
				/* Override the Prune by scheduling a Join */
				jp_value = (RANDOM() % 11) / (10 * PIM_RANDOM_DELAY_JOIN_TIMEOUT);
				/* TODO: XXX: TIMER implem. dependency! */
				if (mrtentry_rp->jp_timer > jp_value)
				    SET_TIMER(mrtentry_rp->jp_timer, jp_value);
			    }
			/*
			 * Check all (*,G) and (S,G) matching to this RP. If
			 * my_action == JOIN, then send a Join and override
			 * the (*,*,RP) Prune.
			 */
			for (grpentry_ptr =
			     rpentry_ptr->cand_rp->rp_grp_next->grplink;
			     grpentry_ptr != (grpentry_t *) NULL;
			     grpentry_ptr = grpentry_ptr->rpnext)
			{
			    my_action = join_or_prune(grpentry_ptr->grp_route,
						      upstream_router);
			    if (my_action == PIM_ACTION_JOIN)
			    {

				jp_value = (RANDOM() % 11) / (10 * PIM_RANDOM_DELAY_JOIN_TIMEOUT);
				/* TODO: XXX: TIMER implem. dependency! */
				if (grpentry_ptr->grp_route->jp_timer >
				    jp_value)
				    SET_TIMER(grpentry_ptr->grp_route->jp_timer, jp_value);
			    }
			    for (mrtentry_srcs = grpentry_ptr->mrtlink;
				 mrtentry_srcs != (mrtentry_t *) NULL;
				 mrtentry_srcs = mrtentry_srcs->grpnext)
			    {
				my_action = join_or_prune(mrtentry_srcs,
							  upstream_router);
				if (my_action == PIM_ACTION_JOIN)
				{

				    jp_value = (RANDOM() % 11) / (10 * PIM_RANDOM_DELAY_JOIN_TIMEOUT);
				    /* TODO: XXX: TIMER implem. dependency! */
				    if (mrtentry_srcs->jp_timer > jp_value)
					SET_TIMER(mrtentry_srcs->jp_timer, jp_value);
				}
			    }	/* For all (S,G) */
			}	/* For all (*,G) */
		    }
		}		/* num_p_srcs */
		continue;	/* This was (*,*,RP) suppression */
	    }

	    /* (*,G) or (S,G) suppression */
	    /*
	     * TODO: XXX: currently, accumulated groups (i.e. group_masklen <
	     * group_address_lengt) are not implemented. Just need to create
	     * a loop and apply the procedure below for all groups matching
	     * the prefix.
	     */

	    while (num_j_srcs--)
	    {
		GET_ESADDR6(&encod_src, data_ptr);
		source.sin6_addr  = encod_src.src_addr;
	        source.sin6_scope_id = inet6_uvif2scopeid(&source,v);

		if (!inet6_valid_host(&source))
		    continue;
	        if (encod_src.masklen >
                (sizeof(struct in6_addr) << 3))
                continue;


		s_flags = encod_src.flags;
		MASKLEN_TO_MASK6(encod_src.masklen, s_mask);

		if ((s_flags & USADDR_RP_BIT) && (s_flags & USADDR_WC_BIT))
		{
		    /* (*,G) JOIN_REQUEST (toward the RP) */
		    mrtentry_ptr = find_route(&sockaddr6_any , &group, MRTF_WC,
					      DONT_CREATE);
		    my_action = join_or_prune(mrtentry_ptr, upstream_router);
		    if (my_action != PIM_ACTION_JOIN)
			continue;
		    /* (*,G) Join suppresion */
		    if (!inet6_equal(&source,&mrtentry_ptr->group->active_rp_grp->rp->rpentry->address))
			continue;	/* The RP address doesn't match.
					 * Ignore. */

		    /* Check the holdtime */
		    /* TODO: XXX: TIMER implem. dependency! */
		    if (mrtentry_ptr->jp_timer > holdtime)
			continue;
		    if ((mrtentry_ptr->jp_timer == holdtime)
		    && (inet6_greaterthan(src, &v->uv_linklocal->pa_addr)))
			continue;
		    jp_value = pim_join_prune_period +
			0.5 * (RANDOM() % pim_join_prune_period);
		    if (mrtentry_ptr->jp_timer < jp_value)
			SET_TIMER(mrtentry_ptr->jp_timer, jp_value);
		    continue;
		}		/* End of (*,G) Join suppression */

		/* (S,G) Join suppresion */
		mrtentry_ptr = find_route(&source, &group, MRTF_SG,
					  DONT_CREATE);
		my_action = join_or_prune(mrtentry_ptr, upstream_router);
		if (my_action != PIM_ACTION_JOIN)
		    continue;

		/* Check the holdtime */
		/* TODO: XXX: TIMER implem. dependency! */
		if (mrtentry_ptr->jp_timer > holdtime)
		    continue;
		if ((mrtentry_ptr->jp_timer == holdtime)
		    && (inet6_greaterthan(src, &v->uv_linklocal->pa_addr)))
		    continue;
		jp_value = pim_join_prune_period +
		    0.5 * (RANDOM() % pim_join_prune_period);
		if (mrtentry_ptr->jp_timer < jp_value)
		    SET_TIMER(mrtentry_ptr->jp_timer, jp_value);
		continue;
	    }

	    /* Prunes suppression */
	    while (num_p_srcs--)
	    {
		GET_ESADDR6(&encod_src, data_ptr);
		source.sin6_addr = encod_src.src_addr;
	        source.sin6_scope_id = inet6_uvif2scopeid(&source,
                                 v);
	        if (encod_src.masklen >
                (sizeof(struct in6_addr) << 3))
                continue;


		if (!inet6_valid_host(&source))
		    continue;
		s_flags = encod_src.flags;
		MASKLEN_TO_MASK6(encod_src.masklen, s_mask);
		if ((s_flags & USADDR_RP_BIT) && (s_flags & USADDR_WC_BIT))
		{
		    /* (*,G) prune suppression */
		    rpentry_ptr = rp_match(&source);
		    if ((rpentry_ptr == (rpentry_t *) NULL)
			|| (!inet6_equal(&rpentry_ptr->address , &source)))
			continue;	/* No such RP or it is different.
					 * Ignore */
		    mrtentry_ptr = find_route(&sockaddr6_any, &group, MRTF_WC,
					      DONT_CREATE);
		    my_action = join_or_prune(mrtentry_ptr, upstream_router);
		    if (my_action == PIM_ACTION_PRUNE)
		    {
			/* TODO: XXX: TIMER implem. dependency! */
			if ((mrtentry_ptr->jp_timer < holdtime)
			    || ((mrtentry_ptr->jp_timer == holdtime)
		    		&& (inet6_greaterthan(src, &v->uv_linklocal->pa_addr))))
			{
			    /* Suppress the Prune */
			    jp_value = pim_join_prune_period +
				0.5 * (RANDOM() % pim_join_prune_period);
			    if (mrtentry_ptr->jp_timer < jp_value)
				SET_TIMER(mrtentry_ptr->jp_timer, jp_value);
			}
		    }
		    else
			if (my_action == PIM_ACTION_JOIN)
			{
			    /* Override the Prune by scheduling a Join */
			    jp_value = (RANDOM() % 11) / (10 * PIM_RANDOM_DELAY_JOIN_TIMEOUT);
			    /* TODO: XXX: TIMER implem. dependency! */
			    if (mrtentry_ptr->jp_timer > jp_value)
				SET_TIMER(mrtentry_ptr->jp_timer, jp_value);
			}

		    /*
		     * Check all (S,G) entries for this group. If my_action
		     * == JOIN, then send the Join and override the (*,G)
		     * Prune.
		     */
		    for (mrtentry_srcs = mrtentry_ptr->group->mrtlink;
			 mrtentry_srcs != (mrtentry_t *) NULL;
			 mrtentry_srcs = mrtentry_srcs->grpnext)
		    {
			my_action = join_or_prune(mrtentry_srcs,
						  upstream_router);
			if (my_action == PIM_ACTION_JOIN)
			{
			    jp_value = (RANDOM() % 11) / (10 * PIM_RANDOM_DELAY_JOIN_TIMEOUT);
			    /* TODO: XXX: TIMER implem. dependency! */
			    if (mrtentry_ptr->jp_timer > jp_value)
				SET_TIMER(mrtentry_ptr->jp_timer, jp_value);
			}
		    }		/* For all (S,G) */
		    continue;	/* End of (*,G) prune suppression */
		}

		/* (S,G) prune suppression */
		mrtentry_ptr = find_route(&source, &group, MRTF_SG,
					  DONT_CREATE);
		my_action = join_or_prune(mrtentry_ptr, upstream_router);
		if (my_action == PIM_ACTION_PRUNE)
		{
		    /* Suppress the (S,G) Prune */
		    /* TODO: XXX: TIMER implem. dependency! */
		    if ((mrtentry_ptr->jp_timer < holdtime)
			|| ((mrtentry_ptr->jp_timer == holdtime)
		    	&& (inet6_greaterthan(src, &v->uv_linklocal->pa_addr))))
		    {
			jp_value = pim_join_prune_period +
			    0.5 * (RANDOM() % pim_join_prune_period);
			if (mrtentry_ptr->jp_timer < jp_value)
			    SET_TIMER(mrtentry_ptr->jp_timer, jp_value);
		    }
		}
		else
		    if (my_action == PIM_ACTION_JOIN)
		    {
			/* Override the Prune by scheduling a Join */
			jp_value = (RANDOM() % 11) / (10 * PIM_RANDOM_DELAY_JOIN_TIMEOUT);
			/* TODO: XXX: TIMER implem. dependency! */
			if (mrtentry_ptr->jp_timer > jp_value)
			    SET_TIMER(mrtentry_ptr->jp_timer, jp_value);
		    }
	    }			/* while (num_p_srcs--) */
	}			/* while (num_groups--) */
	return (TRUE);
    }				/* End of Join/Prune suppression code */

    /* I am the target of this join, so process the message */

    /*
     * The spec says that if there is (*,G) Join, it has priority over old
     * existing ~(S,G) prunes in the routing table. However, if the (*,G)
     * Join and the ~(S,G) prune are in the same message, ~(S,G) has the
     * priority. The spec doesn't say it, but I think the same is true for
     * (*,*,RP) and ~(S,G) prunes.
     *
     * The code below do: (1) Check the whole message for (*,*,RP) Joins. (1.1)
     * If found, clean all pruned_oifs for all (*,G) and all (S,G) for each
     * RP in the list, but do not update the kernel cache. Then go back to
     * the beginning of the message and start processing for each group: (2)
     * Check for Prunes. If no prunes, process the Joins. (3) If there are
     * Prunes: (3.1) Scan the Join part for existing (*,G) Join. (3.1.1) If
     * there is (*,G) Join, clear join interface from the pruned_oifs for all
     * (S,G), but DO NOT flush the change to the kernel (by using
     * change_interfaces() for example) (3.2) After the pruned_oifs are
     * eventually cleared in (3.1.1), process the Prune part of the message
     * normally (setting the prune_oifs and flashing the changes to the
     * (kernel). (3.3) After the Prune part is processed, process the Join
     * part normally (by applying any changes to the kernel) (4) If there
     * were (*,*,RP) Join/Prune, process them.
     *
     * If the Join/Prune list is too long, it may result in long processing
     * overhead. The idea above is not to place any wrong info in the kernel,
     * because it may result in short-time existing traffic forwarding on
     * wrong interface. Hopefully, in the future will find a better way to
     * implement it.
     */

    IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
	log(LOG_DEBUG,0,"I'm the target of the JOIN/PRUNE message");

    num_groups_tmp = num_groups;
    data_ptr_start = data_ptr;
    star_star_rp_found = FALSE;	/* Indicating whether we have (*,*,RP) join */

    IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
	log(LOG_DEBUG,0,"Number of groups to process : %d",num_groups_tmp);

    while (num_groups_tmp--)
    {
	/* Search for (*,*,RP) Join */
	GET_EGADDR6(&encod_group, data_ptr);
	GET_HOSTSHORT(num_j_srcs, data_ptr);
	GET_HOSTSHORT(num_p_srcs, data_ptr);
	group.sin6_addr = encod_group.mcast_addr;
	group.sin6_scope_id = inet6_uvif2scopeid(&group, v);

   	IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
	{
		log(LOG_DEBUG, 0,
		    "Group to process : %s",inet6_fmt(&encod_group.mcast_addr));
		log(LOG_DEBUG, 0,
		    "Number of join   : %d",num_j_srcs );
		log(LOG_DEBUG, 0,
		    "Number of prune  : %d",num_p_srcs );
	}

	if (!(inet6_equal(&group,&sockaddr6_d))
	    || (encod_src.masklen != STAR_STAR_RP_MSK6LEN))
	{
	    /* This is not (*,*,RP). Jump to the next group. */
	    data_ptr += (num_j_srcs + num_p_srcs) * sizeof(pim6_encod_src_addr_t);
	    IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
		{
		    log(LOG_DEBUG, 0,
			"I'm looking for the (*,*,RP) entry , skip to next entry");
		}
	    continue;
	}

	/*
	 * (*,*,RP) found. For each RP and each (*,G) and each (S,G) clear
	 * the pruned oif, but do not update the kernel.
	 */

	star_star_rp_found = TRUE;
	while (num_j_srcs--)
	{
	    GET_ESADDR6(&encod_src, data_ptr);
	    source.sin6_addr = encod_src.src_addr;
	    rpentry_ptr = rp_find(&source);

	    if (rpentry_ptr == (rpentry_t *) NULL)
		continue;
	    for (rp_grp_entry_ptr = rpentry_ptr->cand_rp->rp_grp_next;
		 rp_grp_entry_ptr != (rp_grp_entry_t *) NULL;
		 rp_grp_entry_ptr = rp_grp_entry_ptr->rp_grp_next)
	    {
		for (grpentry_ptr = rp_grp_entry_ptr->grplink;
		     grpentry_ptr != (grpentry_t *) NULL;
		     grpentry_ptr = grpentry_ptr->rpnext)
		{
		    if (grpentry_ptr->grp_route != (mrtentry_t *) NULL)
			IF_CLR(mifi, &grpentry_ptr->grp_route->pruned_oifs);
		    for (mrtentry_ptr = grpentry_ptr->mrtlink;
			 mrtentry_ptr != (mrtentry_t *) NULL;
			 mrtentry_ptr = mrtentry_ptr->grpnext)
			IF_CLR(mifi, &mrtentry_ptr->pruned_oifs);
		}
	    }
	}
	data_ptr += (num_p_srcs) * sizeof(pim6_encod_src_addr_t);
    }

    /*
     * Start processing the groups. If this is (*,*,RP), skip it, but process
     * it at the end.don't forget to reinit data_ptr!
     */

    data_ptr = data_ptr_start;
    num_groups_tmp = num_groups;

    while (num_groups_tmp--)
    {
	GET_EGADDR6(&encod_group, data_ptr);
	GET_HOSTSHORT(num_j_srcs, data_ptr);
	GET_HOSTSHORT(num_p_srcs, data_ptr);
	group.sin6_addr = encod_group.mcast_addr;
	group.sin6_scope_id = inet6_uvif2scopeid(&group, v);

 	IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
	{
		log(LOG_DEBUG,0,"Group to process : %s",inet6_fmt(&encod_group.mcast_addr));
		log(LOG_DEBUG,0,"Number of join   : %d",num_j_srcs );
		log(LOG_DEBUG,0,"Number of prune  : %d",num_p_srcs );
	}

	if (!IN6_IS_ADDR_MULTICAST(&group.sin6_addr))
	{
	    data_ptr += (num_j_srcs + num_p_srcs) * sizeof(pim6_encod_src_addr_t);
	    continue;		/* Ignore this group and jump to the next one */
	}


	if (inet6_equal(&group, &sockaddr6_d)
	    && (encod_group.masklen == STAR_STAR_RP_MSK6LEN))
	{
	    /* This is (*,*,RP). Jump to the next group. */
	    IF_DEBUG(DEBUG_PIM_JOIN_PRUNE) {
		    log(LOG_DEBUG, 0, "This is (*,*,RP). Jump to next.");
	    }
	    data_ptr += (num_j_srcs + num_p_srcs) * sizeof(pim6_encod_src_addr_t);
	    continue;
	}

	rpentry_ptr = rp_match(&group);
	if (rpentry_ptr == (rpentry_t *) NULL)
	    continue;

	IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
		log(LOG_DEBUG,0,"The rp for this JOIN/PRUNE is %s",inet6_fmt(&rpentry_ptr->address.sin6_addr));

	data_ptr_group_j_start = data_ptr;
	data_ptr_group_p_start = data_ptr + num_j_srcs * sizeof(pim6_encod_src_addr_t);

	/*
	 * Scan the Join part for (*,G) Join and then clear the particular
	 * interface from pruned_oifs for all (S,G). If the RP address in the
	 * Join message is different from the local match, ignore the whole
	 * group.
	 */

	num_j_srcs_tmp = num_j_srcs;
	ignore_group = FALSE;

	while (num_j_srcs_tmp--)
	{
	    GET_ESADDR6(&encod_src, data_ptr);
	    source.sin6_addr=encod_src.src_addr;
	    source.sin6_scope_id = inet6_uvif2scopeid(&source,v);

   	    if ((encod_src.flags & USADDR_RP_BIT)
		&& (encod_src.flags & USADDR_WC_BIT))
	    {
		/*
		 * This is the RP address, i.e. (*,G) Join. Check if the
		 * RP-mapping is consistent and if "yes", then Reset the
		 * pruned_oifs for all (S,G) entries.
		 */

		if(!inet6_equal(&rpentry_ptr->address, &source))
		{
		    ignore_group = TRUE;
		    IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
			log(LOG_DEBUG,0,"And I'm not the RP for this address");
		    break;
		}

		mrtentry_ptr = find_route(&sockaddr6_any, &group,
					  MRTF_WC, DONT_CREATE);

		if (mrtentry_ptr != (mrtentry_t *) NULL)
		{
		    for (mrtentry_srcs = mrtentry_ptr->group->mrtlink;
			 mrtentry_srcs != (mrtentry_t *) NULL;
			 mrtentry_srcs = mrtentry_srcs->grpnext)
			IF_CLR(mifi, &mrtentry_srcs->pruned_oifs);
		}
		break;
	    }
	}

	if (ignore_group == TRUE)
	    continue;

	data_ptr = data_ptr_group_p_start;

	/* Process the Prune part first */

	while (num_p_srcs--)
	{
	    GET_ESADDR6(&encod_src, data_ptr);
	    source.sin6_addr = encod_src.src_addr;
	    source.sin6_scope_id = inet6_uvif2scopeid(&source, v);

	    if (!inet6_valid_host(&source))
		continue;
	    s_flags = encod_src.flags;
	    if (!(s_flags & (USADDR_WC_BIT | USADDR_RP_BIT)))
	    {
		/* (S,G) prune sent toward S */
		mrtentry_ptr = find_route(&source, &group, MRTF_SG,
					  DONT_CREATE);
		if (mrtentry_ptr == (mrtentry_t *) NULL)
		    continue;	/* I don't have (S,G) to prune. Ignore. */
		/*
		 * If the link is point-to-point, timeout the oif
		 * immediately, otherwise decrease the timer to allow other
		 * downstream routers to override the prune.
		 */
		/* TODO: XXX: increase the entry timer? */

		if (v->uv_flags & VIFF_POINT_TO_POINT)
		{
		    FIRE_TIMER(mrtentry_ptr->vif_timers[mifi]);
		}
		else
		{
		    /* TODO: XXX: TIMER implem. dependency! */
		    if (mrtentry_ptr->vif_timers[mifi] >
			mrtentry_ptr->vif_deletion_delay[mifi])
			SET_TIMER(mrtentry_ptr->vif_timers[mifi],
				  mrtentry_ptr->vif_deletion_delay[mifi]);
		}
		IF_TIMER_NOT_SET(mrtentry_ptr->vif_timers[mifi])
		{
		    IF_CLR(mifi, &mrtentry_ptr->joined_oifs);
		    IF_SET(mifi, &mrtentry_ptr->pruned_oifs);
		    change_interfaces(mrtentry_ptr,
				      mrtentry_ptr->incoming,
				      &mrtentry_ptr->joined_oifs,
				      &mrtentry_ptr->pruned_oifs,
				      &mrtentry_ptr->leaves,
				      &mrtentry_ptr->asserted_oifs, 0);
		}
		continue;
	    }

	    if ((s_flags & USADDR_RP_BIT)
		&& (!(s_flags & USADDR_WC_BIT)))
	    {
		/* ~(S,G)RPbit prune sent toward the RP */
		mrtentry_ptr = find_route(&source, &group, MRTF_SG,
					  DONT_CREATE);
		if (mrtentry_ptr != (mrtentry_t *) NULL)
		{
		    SET_TIMER(mrtentry_ptr->timer, holdtime);
		    if (v->uv_flags & VIFF_POINT_TO_POINT)
		    {
			FIRE_TIMER(mrtentry_ptr->vif_timers[mifi]);
		    }
		    else
		    {
			/* TODO: XXX: TIMER implem. dependency! */
			if (mrtentry_ptr->vif_timers[mifi] >
			    mrtentry_ptr->vif_deletion_delay[mifi])
			    SET_TIMER(mrtentry_ptr->vif_timers[mifi],
				    mrtentry_ptr->vif_deletion_delay[mifi]);
		    }
		    IF_TIMER_NOT_SET(mrtentry_ptr->vif_timers[mifi])
		    {
			IF_CLR(mifi, &mrtentry_ptr->joined_oifs);
			IF_SET(mifi, &mrtentry_ptr->pruned_oifs);
			change_interfaces(mrtentry_ptr,
					  mrtentry_ptr->incoming,
					  &mrtentry_ptr->joined_oifs,
					  &mrtentry_ptr->pruned_oifs,
					  &mrtentry_ptr->leaves,
					  &mrtentry_ptr->asserted_oifs, 0);
		    }
		    continue;
		}
		/* There is no (S,G) entry. Check for (*,G) or (*,*,RP) */
		mrtentry_ptr = find_route(NULL, &group,
					  MRTF_WC | MRTF_PMBR,
					  DONT_CREATE);
		if (mrtentry_ptr != (mrtentry_t *) NULL)
		{
		    mrtentry_ptr = find_route(&source, &group,
					      MRTF_SG | MRTF_RP,
					      CREATE);
		    if (mrtentry_ptr == (mrtentry_t *) NULL)
			continue;
		    mrtentry_ptr->flags &= ~MRTF_NEW;
		    RESET_TIMER(mrtentry_ptr->vif_timers[mifi]);

		    /*
		     * TODO: XXX: The spec doens't say what value to use for
		     * the entry time. Use the J/P holdtime.
		     */

		    SET_TIMER(mrtentry_ptr->timer, holdtime);

		    /*
		     * TODO: XXX: The spec says to delete the oif. However,
		     * its timer only should be lowered, so the prune can be
		     * overwritten on multiaccess LAN. Spec BUG.
		     */

		    IF_CLR(mifi, &mrtentry_ptr->joined_oifs);
		    IF_SET(mifi, &mrtentry_ptr->pruned_oifs);
		    change_interfaces(mrtentry_ptr,
				      mrtentry_ptr->incoming,
				      &mrtentry_ptr->joined_oifs,
				      &mrtentry_ptr->pruned_oifs,
				      &mrtentry_ptr->leaves,
				      &mrtentry_ptr->asserted_oifs, 0);
		}
		continue;
	    }

	    if ((s_flags & USADDR_RP_BIT) && (s_flags & USADDR_WC_BIT))
	    {
		/* (*,G) Prune */
		mrtentry_ptr = find_route(NULL, &group,
					  MRTF_WC | MRTF_PMBR,
					  DONT_CREATE);
		if (mrtentry_ptr != (mrtentry_t *) NULL)
		{
		    if (mrtentry_ptr->flags & MRTF_WC)
		    {
			/*
			 * TODO: XXX: Should check the whole Prune list in
			 * advance for (*,G) prune and if the RP address does
			 * not match the local RP-map, then ignore the whole
			 * group, not only this particular (*,G) prune.
			 */
			if (!inet6_equal(&mrtentry_ptr->group->active_rp_grp->rp->rpentry->address, &source ))
			    continue;	/* The RP address doesn't match. */
			if (v->uv_flags & VIFF_POINT_TO_POINT)
			{
			    FIRE_TIMER(mrtentry_ptr->vif_timers[mifi]);
			}
			else
			{
			    /* TODO: XXX: TIMER implem. dependency! */
			    if (mrtentry_ptr->vif_timers[mifi] >
				mrtentry_ptr->vif_deletion_delay[mifi])
				SET_TIMER(mrtentry_ptr->vif_timers[mifi],
				    mrtentry_ptr->vif_deletion_delay[mifi]);
			}
			IF_TIMER_NOT_SET(mrtentry_ptr->vif_timers[mifi])
			{
			    IF_CLR(mifi, &mrtentry_ptr->joined_oifs);
			    IF_SET(mifi, &mrtentry_ptr->pruned_oifs);
			    change_interfaces(mrtentry_ptr,
					      mrtentry_ptr->incoming,
					      &mrtentry_ptr->joined_oifs,
					      &mrtentry_ptr->pruned_oifs,
					      &mrtentry_ptr->leaves,
					      &mrtentry_ptr->asserted_oifs, 0);
			}
			continue;
		    }
		    /* No (*,G) entry, but found (*,*,RP). Create (*,G) */
		    if (!inet6_equal(&mrtentry_ptr->source->address, &source))
			continue;	/* The RP address doesn't match. */
		    mrtentry_ptr = find_route(NULL, &group,
					      MRTF_WC, CREATE);
		    if (mrtentry_ptr == (mrtentry_t *) NULL)
			continue;
		    mrtentry_ptr->flags &= ~MRTF_NEW;
		    RESET_TIMER(mrtentry_ptr->vif_timers[mifi]);

		    /*
		     * TODO: XXX: should only lower the oif timer, so it can
		     * be overwritten on multiaccess LAN. Spec bug.
		     */

		    IF_CLR(mifi, &mrtentry_ptr->joined_oifs);
		    IF_SET(mifi, &mrtentry_ptr->pruned_oifs);
		    change_interfaces(mrtentry_ptr,
				      mrtentry_ptr->incoming,
				      &mrtentry_ptr->joined_oifs,
				      &mrtentry_ptr->pruned_oifs,
				      &mrtentry_ptr->leaves,
				      &mrtentry_ptr->asserted_oifs, 0);
		}		/* (*,G) or (*,*,RP) found */
	    }			/* (*,G) prune */
	}			/* while(num_p_srcs--) */

	/* End of (S,G) and (*,G) Prune handling */

	/* Jump back to the Join part and process it */
	data_ptr = data_ptr_group_j_start;
	while (num_j_srcs--)
	{
	    GET_ESADDR6(&encod_src, data_ptr);
	    source.sin6_addr = encod_src.src_addr;
	    source.sin6_scope_id = inet6_uvif2scopeid(&source, v);

	    if (!inet6_valid_host(&source))
		continue;
	    s_flags = encod_src.flags;
	    MASKLEN_TO_MASK6(encod_src.masklen, s_mask);
	    if ((s_flags & USADDR_WC_BIT)
		&& (s_flags & USADDR_RP_BIT))
	    {
		/* (*,G) Join toward RP */
		/*
		 * It has been checked already that this RP address is the
		 * same as the local RP-maping.
		 */
		mrtentry_ptr = find_route(NULL, &group, MRTF_WC,
					  CREATE);
		if (mrtentry_ptr == (mrtentry_t *) NULL)
		    continue;
		IF_SET(mifi, &mrtentry_ptr->joined_oifs);
		IF_CLR(mifi, &mrtentry_ptr->pruned_oifs);
		IF_CLR(mifi, &mrtentry_ptr->asserted_oifs);
		/* TODO: XXX: TIMER implem. dependency! */
		if (mrtentry_ptr->vif_timers[mifi] < holdtime)
		{
		    SET_TIMER(mrtentry_ptr->vif_timers[mifi], holdtime);
		    mrtentry_ptr->vif_deletion_delay[mifi] = holdtime / 3;
		}
		if (mrtentry_ptr->timer < holdtime)
		    SET_TIMER(mrtentry_ptr->timer, holdtime);
		mrtentry_ptr->flags &= ~MRTF_NEW;
		change_interfaces(mrtentry_ptr,
				  mrtentry_ptr->incoming,
				  &mrtentry_ptr->joined_oifs,
				  &mrtentry_ptr->pruned_oifs,
				  &mrtentry_ptr->leaves,
				  &mrtentry_ptr->asserted_oifs, 0);
		/*
		 * Need to update the (S,G) entries, because of the previous
		 * cleaning of the pruned_oifs. The reason is that if the
		 * oifs for (*,G) weren't changed, the (S,G) entries won't be
		 * updated by change_interfaces()
		 */

		for (mrtentry_srcs = mrtentry_ptr->group->mrtlink;
		     mrtentry_srcs != (mrtentry_t *) NULL;
		     mrtentry_srcs = mrtentry_srcs->grpnext)
		    change_interfaces(mrtentry_srcs,
				      mrtentry_srcs->incoming,
				      &mrtentry_srcs->joined_oifs,
				      &mrtentry_srcs->pruned_oifs,
				      &mrtentry_srcs->leaves,
				      &mrtentry_srcs->asserted_oifs, 0);
		continue;
	    }

	    if (!(s_flags & (USADDR_WC_BIT | USADDR_RP_BIT)))
	    {
		/* (S,G) Join toward S */
		if (mifi == get_iif(&source))
		    continue;	/* Ignore this (S,G) Join */
		mrtentry_ptr = find_route(&source, &group, MRTF_SG, CREATE);
		if (mrtentry_ptr == (mrtentry_t *) NULL)
		    continue;
		IF_SET(mifi, &mrtentry_ptr->joined_oifs);
		IF_CLR(mifi, &mrtentry_ptr->pruned_oifs);
		IF_CLR(mifi, &mrtentry_ptr->asserted_oifs);
		/* TODO: XXX: TIMER implem. dependency! */
		if (mrtentry_ptr->vif_timers[mifi] < holdtime)
		{
		    SET_TIMER(mrtentry_ptr->vif_timers[mifi], holdtime);
		    mrtentry_ptr->vif_deletion_delay[mifi] = holdtime / 3;
		}
		if (mrtentry_ptr->timer < holdtime)
		    SET_TIMER(mrtentry_ptr->timer, holdtime);
		/*
		 * TODO: if this is a new entry, send immediately the Join
		 * message toward S. The Join/Prune timer for new entries is
		 * 0, but it does not means the message will be sent
		 * immediately.
		 */
		mrtentry_ptr->flags &= ~MRTF_NEW;
		/*
		 * Note that we must create (S,G) without the RPbit set. If
		 * we already had such entry, change_interfaces() will reset
		 * the RPbit propertly.
		 */
		change_interfaces(mrtentry_ptr,
				  mrtentry_ptr->source->incoming,
				  &mrtentry_ptr->joined_oifs,
				  &mrtentry_ptr->pruned_oifs,
				  &mrtentry_ptr->leaves,
				  &mrtentry_ptr->asserted_oifs, 0);
		continue;
	    }
	}			/* while(num_j_srcs--) */
    }				/* for all groups */

    /* Now process the (*,*,RP) Join/Prune */

    if (star_star_rp_found == TRUE)
	return (TRUE);
    data_ptr = data_ptr_start;
    while (num_groups--)
    {
	/*
	 * The conservative approach is to scan again the whole message, just
	 * in case if we have more than one (*,*,RP) requests.
	 */
	GET_EGADDR6(&encod_group, data_ptr);
	GET_HOSTSHORT(num_j_srcs, data_ptr);
	GET_HOSTSHORT(num_p_srcs, data_ptr);

	group.sin6_addr = encod_group.mcast_addr;
	group.sin6_scope_id = inet6_uvif2scopeid(&group, v);

	if (!inet6_equal(&group,&sockaddr6_d)
	    || (encod_group.masklen != STAR_STAR_RP_MSK6LEN))
	{
	    /* This is not (*,*,RP). Jump to the next group. */
	    data_ptr +=
		(num_j_srcs + num_p_srcs) * sizeof(pim6_encod_src_addr_t);
	    continue;
	}
	/* (*,*,RP) found */
	while (num_j_srcs--)
	{
	    /* TODO: XXX: check that the iif is different from the Join oifs */
	    GET_ESADDR6(&encod_src, data_ptr);
	    source.sin6_addr = encod_src.src_addr;
	    source.sin6_scope_id = inet6_uvif2scopeid(&source,
                                      v);


	    if (!inet6_valid_host(&source))
		continue;
	    s_flags = encod_src.flags;
	    MASKLEN_TO_MASK6(encod_src.masklen, s_mask);
	    mrtentry_ptr = find_route(&source, NULL, MRTF_PMBR,
				      CREATE);
	    if (mrtentry_ptr == (mrtentry_t *) NULL)
		continue;
	    IF_SET(mifi, &mrtentry_ptr->joined_oifs);
	    IF_CLR(mifi, &mrtentry_ptr->pruned_oifs);
	    IF_CLR(mifi, &mrtentry_ptr->asserted_oifs);
	    /* TODO: XXX: TIMER implem. dependency! */
	    if (mrtentry_ptr->vif_timers[mifi] < holdtime)
	    {
		SET_TIMER(mrtentry_ptr->vif_timers[mifi], holdtime);
		mrtentry_ptr->vif_deletion_delay[mifi] = holdtime / 3;
	    }
	    if (mrtentry_ptr->timer < holdtime)
		SET_TIMER(mrtentry_ptr->timer, holdtime);
	    mrtentry_ptr->flags &= ~MRTF_NEW;
	    change_interfaces(mrtentry_ptr,
			      mrtentry_ptr->incoming,
			      &mrtentry_ptr->joined_oifs,
			      &mrtentry_ptr->pruned_oifs,
			      &mrtentry_ptr->leaves,
			      &mrtentry_ptr->asserted_oifs, 0);

	    /*
	     * Need to update the (S,G) and (*,G) entries, because of the
	     * previous cleaning of the pruned_oifs. The reason is that if
	     * the oifs for (*,*,RP) weren't changed, the (*,G) and (S,G)
	     * entries won't be updated by change_interfaces()
	     */

	    for (rp_grp_entry_ptr = mrtentry_ptr->source->cand_rp->rp_grp_next;
		 rp_grp_entry_ptr != (rp_grp_entry_t *) NULL;
		 rp_grp_entry_ptr = rp_grp_entry_ptr->rp_grp_next)
		for (grpentry_ptr = rp_grp_entry_ptr->grplink;
		     grpentry_ptr != (grpentry_t *) NULL;
		     grpentry_ptr = grpentry_ptr->rpnext)
		{
		    /* Update the (*,G) entry */
		    change_interfaces(grpentry_ptr->grp_route,
				      grpentry_ptr->grp_route->incoming,
				      &grpentry_ptr->grp_route->joined_oifs,
				      &grpentry_ptr->grp_route->pruned_oifs,
				      &grpentry_ptr->grp_route->leaves,
				 	  &grpentry_ptr->grp_route->asserted_oifs, 0);
		    /* Update the (S,G) entries */
		    for (mrtentry_srcs = grpentry_ptr->mrtlink;
			 mrtentry_srcs != (mrtentry_t *) NULL;
			 mrtentry_srcs = mrtentry_srcs->grpnext)
			change_interfaces(mrtentry_srcs,
					  mrtentry_srcs->incoming,
					  &mrtentry_srcs->joined_oifs,
					  &mrtentry_srcs->pruned_oifs,
					  &mrtentry_srcs->leaves,
					  &mrtentry_srcs->asserted_oifs, 0);
		}
	    continue;
	}

	while (num_p_srcs--)
	{
	    /* TODO: XXX: can we have (*,*,RP) Prune? */
	    GET_ESADDR6(&encod_src, data_ptr);
	    source.sin6_addr = encod_src.src_addr;
	    source.sin6_scope_id = inet6_uvif2scopeid(&source,
                                      v);

	    if (!inet6_valid_host(&source))
		continue;
	    s_flags = encod_src.flags;
	    MASKLEN_TO_MASK6(encod_src.masklen, s_mask);
	    mrtentry_ptr = find_route(&source, NULL , MRTF_PMBR,
				      DONT_CREATE);
	    if (mrtentry_ptr == (mrtentry_t *) NULL)
		continue;
	    /*
	     * If the link is point-to-point, timeout the oif immediately,
	     * otherwise decrease the timer to allow other downstream routers
	     * to override the prune.
	     */
	    /* TODO: XXX: increase the entry timer? */
	    if (v->uv_flags & VIFF_POINT_TO_POINT)
	    {
		FIRE_TIMER(mrtentry_ptr->vif_timers[mifi]);
	    }
	    else
	    {
		/* TODO: XXX: TIMER implem. dependency! */
		if (mrtentry_ptr->vif_timers[mifi] >
		    mrtentry_ptr->vif_deletion_delay[mifi])
		    SET_TIMER(mrtentry_ptr->vif_timers[mifi],
			      mrtentry_ptr->vif_deletion_delay[mifi]);
	    }
	    IF_TIMER_NOT_SET(mrtentry_ptr->vif_timers[mifi])
	    {
		IF_CLR(mifi, &mrtentry_ptr->joined_oifs);
		IF_SET(mifi, &mrtentry_ptr->pruned_oifs);
		IF_SET(mifi, &mrtentry_ptr->asserted_oifs);
		change_interfaces(mrtentry_ptr,
				  mrtentry_ptr->incoming,
				  &mrtentry_ptr->joined_oifs,
				  &mrtentry_ptr->pruned_oifs,
				  &mrtentry_ptr->leaves,
				  &mrtentry_ptr->asserted_oifs, 0);
	    }

	}
    }				/* For all groups processing (*,*,R) */

    return (TRUE);
}


/*
 * TODO: NOT USED, probably buggy, but may need it in the future.
 */
/*
 * TODO: create two functions: periodic which timeout the timers and
 * non-periodic which only check but don't timeout the timers.
 */
/*
 * Create and send Join/Prune messages per interface. Only the entries which
 * have the Join/Prune timer expired are included. In the special case when
 * we have ~(S,G)RPbit Prune entry, we must include any (*,G) or (*,*,RP)
 * Currently the whole table is scanned. In the future will have all routing
 * entries linked in a chain with the corresponding upstream pim_nbr_entry.
 *
 * If pim_nbr is not NULL, then send to only this particular PIM neighbor,
 */
int
send_periodic_pim6_join_prune(mifi, pim_nbr, holdtime)
    mifi_t          mifi;
    pim_nbr_entry_t *pim_nbr;
    u_int16         holdtime;
{
    grpentry_t     		*grpentry_ptr;
    mrtentry_t     		*mrtentry_ptr;
    rpentry_t      		*rpentry_ptr;
    struct sockaddr_in6   	src_addr;
    struct uvif    		*v;
    pim_nbr_entry_t 		*pim_nbr_ptr;
    cand_rp_t      		*cand_rp_ptr;

    /*
     * Walk through all routing entries. The iif must match to include the
     * entry. Check first the (*,G) entry and then all associated (S,G). At
     * the end of the message will add any (*,*,RP) entries. TODO: check
     * other PIM-SM implementations and decide the more appropriate place to
     * put the (*,*,RP) entries: in the beginning of the message or at the
     * end.
     */

    v = &uvifs[mifi];

    /* Check the (*,G) and (S,G) entries */
    for (grpentry_ptr = grplist; grpentry_ptr != (grpentry_t *) NULL;
	 grpentry_ptr = grpentry_ptr->next)
    {
	mrtentry_ptr = grpentry_ptr->grp_route;
	/* TODO: XXX: TIMER implem. dependency! */
	if ((mrtentry_ptr != (mrtentry_t *) NULL)
	    && (mrtentry_ptr->incoming == mifi)
	    && (mrtentry_ptr->jp_timer <= timer_interval))
	{

	    /* If join/prune to a particular neighbor only was specified */
	    if ((pim_nbr != (pim_nbr_entry_t *) NULL)
		&& (mrtentry_ptr->upstream != pim_nbr))
		continue;

	    /* TODO: XXX: The J/P suppression timer is not in the spec! */
	    if (!IF_ISEMPTY(&mrtentry_ptr->joined_oifs) ||
		(v->uv_flags & VIFF_DR))
	    {
		add_jp_entry(mrtentry_ptr->upstream, holdtime,
			     &grpentry_ptr->group,
			     SINGLE_GRP_MSK6LEN,
			     &grpentry_ptr->rpaddr,
			     SINGLE_SRC_MSK6LEN, 0, PIM_ACTION_JOIN);
	    }
	    /* TODO: XXX: TIMER implem. dependency! */
	    if (IF_ISEMPTY(&mrtentry_ptr->joined_oifs)
		&& (!(v->uv_flags & VIFF_DR))
		&& (mrtentry_ptr->jp_timer <= timer_interval))
	    {
		add_jp_entry(mrtentry_ptr->upstream, holdtime,
			     &grpentry_ptr->group, SINGLE_GRP_MSK6LEN,
			     &grpentry_ptr->rpaddr,
			     SINGLE_SRC_MSK6LEN, 0, PIM_ACTION_PRUNE);
	    }
	}

	/* Check the (S,G) entries */
	for (mrtentry_ptr = grpentry_ptr->mrtlink;
	     mrtentry_ptr != (mrtentry_t *) NULL;
	     mrtentry_ptr = mrtentry_ptr->grpnext)
	{

	    /* If join/prune to a particular neighbor only was specified */
	    if ((pim_nbr != (pim_nbr_entry_t *) NULL)
		&& (mrtentry_ptr->upstream != pim_nbr))
		continue;

	    if (mrtentry_ptr->flags & MRTF_RP)
	    {
		/* RPbit set */

		src_addr = mrtentry_ptr->source->address;
		if (IF_ISEMPTY(&mrtentry_ptr->joined_oifs)
		    || ((find_vif_direct_local(&src_addr) != NO_VIF)
			&& grpentry_ptr->grp_route != (mrtentry_t *) NULL))
		    /* TODO: XXX: TIMER implem. dependency! */
		    if ((grpentry_ptr->grp_route->incoming == mifi)
			&& (grpentry_ptr->grp_route->jp_timer
			    <= timer_interval))
			/* S is directly connected. Send toward RP */
			add_jp_entry(grpentry_ptr->grp_route->upstream,
				     holdtime,
				     &grpentry_ptr->group, SINGLE_GRP_MSK6LEN,
				     &src_addr, SINGLE_SRC_MSK6LEN,
				     MRTF_RP, PIM_ACTION_PRUNE);
	    }
	    else
	    {
		/* RPbit cleared */
		if (IF_ISEMPTY(&mrtentry_ptr->joined_oifs))
		{
		    /* TODO: XXX: TIMER implem. dependency! */
		    if ((mrtentry_ptr->incoming == mifi)
			&& (mrtentry_ptr->jp_timer <= timer_interval))
			add_jp_entry(mrtentry_ptr->upstream, holdtime,
				     &grpentry_ptr->group, SINGLE_GRP_MSK6LEN,
				     &mrtentry_ptr->source->address,
				     SINGLE_SRC_MSK6LEN, 0, PIM_ACTION_PRUNE);
		}
		else
		{
		    /* TODO: XXX: TIMER implem. dependency! */
		    if ((mrtentry_ptr->incoming == mifi)
			&& (mrtentry_ptr->jp_timer <= timer_interval))
			add_jp_entry(mrtentry_ptr->upstream, holdtime,
				     &grpentry_ptr->group, SINGLE_GRP_MSK6LEN,
				     &mrtentry_ptr->source->address,
				     SINGLE_SRC_MSK6LEN, 0, PIM_ACTION_JOIN);
		}
		/* TODO: XXX: TIMER implem. dependency! */
		if ((mrtentry_ptr->flags & MRTF_SPT)
		    && (grpentry_ptr->grp_route != (mrtentry_t *) NULL)
		    && (mrtentry_ptr->incoming !=
			grpentry_ptr->grp_route->incoming)
		    && (grpentry_ptr->grp_route->incoming == mifi)
		    && (grpentry_ptr->grp_route->jp_timer
			<= timer_interval))
		    add_jp_entry(grpentry_ptr->grp_route->upstream, holdtime,
				 &grpentry_ptr->group, SINGLE_GRP_MSK6LEN,
				 &mrtentry_ptr->source->address,
				 SINGLE_SRC_MSK6LEN, MRTF_RP,
				 PIM_ACTION_PRUNE);
	    }
	}
    }

    /* Check the (*,*,RP) entries */
    for (cand_rp_ptr = cand_rp_list; cand_rp_ptr != (cand_rp_t *) NULL;
	 cand_rp_ptr = cand_rp_ptr->next)
    {
	rpentry_ptr = cand_rp_ptr->rpentry;

	/* If join/prune to a particular neighbor only was specified */
	if ((pim_nbr != (pim_nbr_entry_t *) NULL)
	    && (rpentry_ptr->upstream != pim_nbr))
	    continue;


	/* TODO: XXX: TIMER implem. dependency! */
	if ((rpentry_ptr->mrtlink != (mrtentry_t *) NULL)
	    && (rpentry_ptr->incoming == mifi)
	    && (rpentry_ptr->mrtlink->jp_timer <= timer_interval))
	{
	    add_jp_entry(rpentry_ptr->upstream, holdtime,
			 &sockaddr6_d, STAR_STAR_RP_MSK6LEN,
			 &rpentry_ptr->address,
			 SINGLE_SRC_MSK6LEN, MRTF_RP | MRTF_WC,
			 PIM_ACTION_JOIN);
	}
    }

    /* Send all pending Join/Prune messages */
    for (pim_nbr_ptr = v->uv_pim_neighbors;
	 pim_nbr_ptr != (pim_nbr_entry_t *) NULL;
	 pim_nbr_ptr = pim_nbr->next)
    {

	/* If join/prune to a particular neighbor only was specified */
	if ((pim_nbr != (pim_nbr_entry_t *) NULL)
	    && (pim_nbr_ptr != pim_nbr))
	    continue;

	pack_and_send_jp6_message(pim_nbr_ptr);
    }

    return (TRUE);
}


int
add_jp_entry(pim_nbr, holdtime, group, grp_msklen, source, src_msklen,
	     addr_flags, join_prune)
    pim_nbr_entry_t 		*pim_nbr;
    u_int16         		holdtime;
    struct sockaddr_in6         *group;
    u_int8          		grp_msklen;
    struct sockaddr_in6         *source;
    u_int8          		src_msklen;
    u_int16         		addr_flags;
    u_int8          		join_prune;
{
    build_jp_message_t 		*bjpm;
    u_int8         		*data_ptr;
    u_int8          		flags = 0;
    int             		rp_flag;


    bjpm = pim_nbr->build_jp_message;

    if (bjpm != (build_jp_message_t *) NULL)
    {
	if ((bjpm->jp_message_size + bjpm->join_list_size +
	     bjpm->prune_list_size + bjpm->rp_list_join_size +
	     bjpm->rp_list_prune_size >= MAX_JP_MESSAGE_SIZE)
	    || (bjpm->join_list_size >= MAX_JOIN_LIST_SIZE)
	    || (bjpm->prune_list_size >= MAX_PRUNE_LIST_SIZE)
	    || (bjpm->rp_list_join_size >= MAX_JOIN_LIST_SIZE)
	    || (bjpm->rp_list_prune_size >= MAX_PRUNE_LIST_SIZE))
	{
	    /*
	     * TODO: XXX: BUG: If the list is getting too large, must be
	     * careful with the fragmentation.
	     */
	    pack_and_send_jp6_message(pim_nbr);
	    bjpm = pim_nbr->build_jp_message;	/* The buffer will be freed */
	}
    }

    if (bjpm != (build_jp_message_t *) NULL)
    {
	if ((!inet6_equal(&bjpm->curr_group, group)
	    || (bjpm->curr_group_msklen != grp_msklen)
	    || (bjpm->holdtime != holdtime)))
	{
	    pack_jp6_message(pim_nbr);
	}
    }

    if (bjpm == (build_jp_message_t *) NULL)
    {
	bjpm = get_jp6_working_buff();
	pim_nbr->build_jp_message = bjpm;
	data_ptr = bjpm->jp_message;
	PUT_EUADDR6(pim_nbr->address.sin6_addr, data_ptr);
	PUT_BYTE(0, data_ptr);	/* Reserved */
	bjpm->num_groups_ptr = data_ptr++;	/* The pointer for numgroups */
	*(bjpm->num_groups_ptr) = 0;	/* Zero groups */
	PUT_HOSTSHORT(holdtime, data_ptr);
	bjpm->holdtime = holdtime;
	bjpm->jp_message_size = data_ptr - bjpm->jp_message;
    }

    /* TODO: move somewhere else, only when it is a new group */
    bjpm->curr_group = *group;
    bjpm->curr_group_msklen = grp_msklen;

    if (inet6_equal(group, &sockaddr6_d) &&
	(grp_msklen == STAR_STAR_RP_MSK6LEN))
	rp_flag = TRUE;
    else
	rp_flag = FALSE;

    switch (join_prune)
    {
    case PIM_ACTION_JOIN:
	if (rp_flag == TRUE)
	    data_ptr = bjpm->rp_list_join + bjpm->rp_list_join_size;
	else
	    data_ptr = bjpm->join_list + bjpm->join_list_size;
	break;
    case PIM_ACTION_PRUNE:
	if (rp_flag == TRUE)
	    data_ptr = bjpm->rp_list_join + bjpm->rp_list_join_size;
	else
	    data_ptr = bjpm->prune_list + bjpm->prune_list_size;
	break;
    default:
	return (FALSE);
    }

    flags |= USADDR_S_BIT;	/* Mandatory for PIMv2 */
    if (addr_flags & MRTF_RP)
	flags |= USADDR_RP_BIT;
    if (addr_flags & MRTF_WC)
	flags |= USADDR_WC_BIT;
    PUT_ESADDR6(source->sin6_addr, src_msklen, flags, data_ptr);

    switch (join_prune)
    {
    case PIM_ACTION_JOIN:
	if (rp_flag == TRUE)
	{
	    bjpm->rp_list_join_size = data_ptr - bjpm->rp_list_join;
	    bjpm->rp_list_join_number++;
	}
	else
	{
	    bjpm->join_list_size = data_ptr - bjpm->join_list;
	    bjpm->join_addr_number++;
	}
	break;
    case PIM_ACTION_PRUNE:
	if (rp_flag == TRUE)
	{
	    bjpm->rp_list_prune_size = data_ptr - bjpm->rp_list_prune;
	    bjpm->rp_list_prune_number++;
	}
	else
	{
	    bjpm->prune_list_size = data_ptr - bjpm->prune_list;
	    bjpm->prune_addr_number++;
	}
	break;
    default:
	return (FALSE);
    }

    return (TRUE);
}


/* TODO: check again the size of the buffers */

static build_jp_message_t *
get_jp6_working_buff()
{
    build_jp_message_t *bjpm_ptr;

    if (build_jp_message_pool_counter == 0)
    {
	bjpm_ptr = (build_jp_message_t *) malloc(sizeof(build_jp_message_t));
	bjpm_ptr->next = (build_jp_message_t *) NULL;
	bjpm_ptr->jp_message =
	    (u_int8 *) malloc(MAX_JP_MESSAGE_SIZE +
			      sizeof(pim_jp_encod_grp_t) +
			      2 * sizeof(pim6_encod_src_addr_t));
	bjpm_ptr->jp_message_size = 0;
	bjpm_ptr->join_list_size = 0;
	bjpm_ptr->join_addr_number = 0;
	bjpm_ptr->join_list = (u_int8 *) malloc(MAX_JOIN_LIST_SIZE +
					      sizeof(pim6_encod_src_addr_t));
	bjpm_ptr->prune_list_size = 0;
	bjpm_ptr->prune_addr_number = 0;
	bjpm_ptr->prune_list = (u_int8 *) malloc(MAX_PRUNE_LIST_SIZE +
					      sizeof(pim6_encod_src_addr_t));
	bjpm_ptr->rp_list_join_size = 0;
	bjpm_ptr->rp_list_join_number = 0;
	bjpm_ptr->rp_list_join = (u_int8 *) malloc(MAX_JOIN_LIST_SIZE +
					      sizeof(pim6_encod_src_addr_t));
	bjpm_ptr->rp_list_prune_size = 0;
	bjpm_ptr->rp_list_prune_number = 0;
	bjpm_ptr->rp_list_prune = (u_int8 *) malloc(MAX_PRUNE_LIST_SIZE +
					      sizeof(pim6_encod_src_addr_t));
	bjpm_ptr->curr_group = sockaddr6_any;
	bjpm_ptr->curr_group_msklen = 0;
	bjpm_ptr->holdtime = 0;
	return bjpm_ptr;
    }
    else
    {
	bjpm_ptr = build_jp_message_pool;
	build_jp_message_pool = build_jp_message_pool->next;
	build_jp_message_pool_counter--;
	bjpm_ptr->jp_message_size = 0;
	bjpm_ptr->join_list_size = 0;
	bjpm_ptr->join_addr_number = 0;
	bjpm_ptr->prune_list_size = 0;
	bjpm_ptr->prune_addr_number = 0;
	bjpm_ptr->curr_group = sockaddr6_any;
	bjpm_ptr->curr_group_msklen = 0;
	return (bjpm_ptr);
    }
}


static void
return_jp6_working_buff(pim_nbr)
    pim_nbr_entry_t *pim_nbr;
{
    build_jp_message_t *bjpm_ptr = pim_nbr->build_jp_message;

    if (bjpm_ptr == (build_jp_message_t *) NULL)
	return;
    /* Don't waste memory by keeping too many free buffers */
    /* TODO: check/modify the definitions for POOL_NUMBER and size */
    if (build_jp_message_pool_counter >= MAX_JP_MESSAGE_POOL_NUMBER)
    {
	free((void *) bjpm_ptr->jp_message);
	free((void *) bjpm_ptr->join_list);
	free((void *) bjpm_ptr->prune_list);
	free((void *) bjpm_ptr->rp_list_join);
	free((void *) bjpm_ptr->rp_list_prune);
	free((void *) bjpm_ptr);
    }
    else
    {
	bjpm_ptr->next = build_jp_message_pool;
	build_jp_message_pool = bjpm_ptr;
	build_jp_message_pool_counter++;
    }
    pim_nbr->build_jp_message = (build_jp_message_t *) NULL;
}


/*
 * TODO: XXX: Currently, the (*,*,RP) stuff goes at the end of the Join/Prune
 * message. However, this particular implementation of PIM processes the
 * Join/Prune messages faster if (*,*,RP) is at the beginning. Modify some of
 * the functions below such that the outgoing messages place (*,*,RP) at the
 * beginning, not at the end.
 */

static void
pack_jp6_message(pim_nbr)
    pim_nbr_entry_t *pim_nbr;
{
    build_jp_message_t *bjpm;
    u_int8         *data_ptr;

    bjpm = pim_nbr->build_jp_message;
    if ((bjpm == (build_jp_message_t *) NULL)
	|| (inet6_equal(&bjpm->curr_group,&sockaddr6_any)))
	return;
    data_ptr = bjpm->jp_message + bjpm->jp_message_size;
    PUT_EGADDR6(bjpm->curr_group.sin6_addr, bjpm->curr_group_msklen, 0, data_ptr);
    PUT_HOSTSHORT(bjpm->join_addr_number, data_ptr);
    PUT_HOSTSHORT(bjpm->prune_addr_number, data_ptr);
    bcopy(bjpm->join_list, data_ptr, bjpm->join_list_size);
    data_ptr += bjpm->join_list_size;
    bcopy(bjpm->prune_list, data_ptr, bjpm->prune_list_size);
    data_ptr += bjpm->prune_list_size;
    bjpm->jp_message_size = (data_ptr - bjpm->jp_message);
    bjpm->join_list_size = 0;
    bjpm->join_addr_number = 0;
#if 0				/* isn't this necessary? */
    bjpm->rp_list_join_size = 0;
    bjpm->rp_list_join_number = 0;
#endif
    bjpm->prune_list_size = 0;
    bjpm->prune_addr_number = 0;
#if 0				/* isn't this necessary? */
    bjpm->rp_list_prune_size = 0;
    bjpm->rp_list_prune_number = 0;
#endif
    (*bjpm->num_groups_ptr)++;
    bjpm->curr_group = sockaddr6_any;
    bjpm->curr_group_msklen = 0;
    if (*bjpm->num_groups_ptr == ((u_int8) ~ 0 - 1))
    {
	if (bjpm->rp_list_join_number + bjpm->rp_list_prune_number)
	{
	    /* Add the (*,*,RP) at the end */
	    data_ptr = bjpm->jp_message + bjpm->jp_message_size;
	    PUT_EGADDR6(sockaddr6_d.sin6_addr, STAR_STAR_RP_MSK6LEN, 0, data_ptr);
	    PUT_HOSTSHORT(bjpm->rp_list_join_number, data_ptr);
	    PUT_HOSTSHORT(bjpm->rp_list_prune_number, data_ptr);
	    bcopy(bjpm->rp_list_join, data_ptr, bjpm->rp_list_join_size);
	    data_ptr += bjpm->rp_list_join_size;
	    bcopy(bjpm->rp_list_prune, data_ptr, bjpm->rp_list_prune_size);
	    data_ptr += bjpm->rp_list_prune_size;
	    bjpm->jp_message_size = (data_ptr - bjpm->jp_message);
	    bjpm->rp_list_join_size = 0;
	    bjpm->rp_list_join_number = 0;
	    bjpm->rp_list_prune_size = 0;
	    bjpm->rp_list_prune_number = 0;
	    (*bjpm->num_groups_ptr)++;
	}
	send_jp6_message(pim_nbr);
    }
}

void
pack_and_send_jp6_message(pim_nbr)
    pim_nbr_entry_t *pim_nbr;
{
    u_int8         	*data_ptr;
    build_jp_message_t 	*bjpm;


    if ((pim_nbr == (pim_nbr_entry_t *) NULL)
     || ((bjpm = pim_nbr->build_jp_message) == (build_jp_message_t *) NULL))
	{
		return;
	}
    pack_jp6_message(pim_nbr);


    if (bjpm->rp_list_join_number + bjpm->rp_list_prune_number)
    {
	/* Add the (*,*,RP) at the end */
	data_ptr = bjpm->jp_message + bjpm->jp_message_size;

	PUT_EGADDR6(sockaddr6_d.sin6_addr, STAR_STAR_RP_MSK6LEN, 0, data_ptr);
	PUT_HOSTSHORT(bjpm->rp_list_join_number, data_ptr);
	PUT_HOSTSHORT(bjpm->rp_list_prune_number, data_ptr);
	bcopy(bjpm->rp_list_join, data_ptr, bjpm->rp_list_join_size);
	data_ptr += bjpm->rp_list_join_size;
	bcopy(bjpm->rp_list_prune, data_ptr, bjpm->rp_list_prune_size);
	data_ptr += bjpm->rp_list_prune_size;
	bjpm->jp_message_size = (data_ptr - bjpm->jp_message);
	bjpm->rp_list_join_size = 0;
	bjpm->rp_list_join_number = 0;
	bjpm->rp_list_prune_size = 0;
	bjpm->rp_list_prune_number = 0;
	(*bjpm->num_groups_ptr)++;
    }
    send_jp6_message(pim_nbr);
}

static void
send_jp6_message(pim_nbr)
    pim_nbr_entry_t *pim_nbr;
{
    u_int16         datalen;
    mifi_t          mifi;

    datalen = pim_nbr->build_jp_message->jp_message_size;
    mifi = pim_nbr->vifi;
    bcopy(pim_nbr->build_jp_message->jp_message,
	  pim6_send_buf+sizeof(struct pim), datalen);

    send_pim6(pim6_send_buf, &uvifs[mifi].uv_linklocal->pa_addr,
	      &allpim6routers_group , PIM_JOIN_PRUNE, datalen);
    uvifs[mifi].uv_out_pim6_join_prune++;
    return_jp6_working_buff(pim_nbr);
}

/************************************************************************
 *                        PIM_ASSERT
 ************************************************************************/
int
receive_pim6_assert(src, dst, pim_message, datalen)
    struct sockaddr_in6 	*src,
                    		*dst;
    register char  		*pim_message;
    int             		datalen;
{
    mifi_t          		mifi;
    pim6_encod_uni_addr_t 	eusaddr;
    pim6_encod_grp_addr_t 	egaddr;
    struct sockaddr_in6		source,
                    		group;
    mrtentry_t     		*mrtentry_ptr,
                   		*mrtentry_ptr2;
    u_int8         		*data_ptr;
    struct uvif    		*v;
    u_int32         		assert_preference;
    u_int32         		assert_metric;
    u_int32         		assert_rptbit;
    u_int32         		local_metric;
    u_int32         		local_preference;
    u_int8          		local_rptbit;
    u_int8          		local_wins;
    pim_nbr_entry_t 		*original_upstream_router;


    if ((mifi = find_vif_direct(src)) == NO_VIF)
    {
	/*
	 * Either a local vif or somehow received PIM_ASSERT from
	 * non-directly connected router. Ignore it.
	 */

	if (local_address(src) == NO_VIF)
	    log(LOG_INFO, 0,
		"Ignoring PIM_ASSERT from non-neighbor router %s",
		inet6_fmt(&src->sin6_addr));
	return (FALSE);
    }

    v = &uvifs[mifi];
    v->uv_in_pim6_assert++;
    if (uvifs[mifi].uv_flags &
	(VIFF_DOWN | VIFF_DISABLED | VIFF_NONBRS | MIFF_REGISTER))
	return (FALSE);		/* Shoudn't come on this interface */
    data_ptr = (u_int8 *) (pim_message + sizeof(struct pim));

    /* Get the group and source addresses */
    GET_EGADDR6(&egaddr, data_ptr);
    GET_EUADDR6(&eusaddr, data_ptr);

    /* Get the metric related info */
    GET_HOSTLONG(assert_preference, data_ptr);
    GET_HOSTLONG(assert_metric, data_ptr);
    assert_rptbit = assert_preference & PIM_ASSERT_RPT_BIT;

    source.sin6_addr  = eusaddr.unicast_addr;
    source.sin6_scope_id = inet6_uvif2scopeid(&source, v);

    group.sin6_addr = egaddr.mcast_addr;
    group.sin6_scope_id = inet6_uvif2scopeid(&group, v);

    /* Find the longest "active" entry, i.e. the one with a kernel mirror */
    if (assert_rptbit)
    {
	mrtentry_ptr = find_route(NULL, &group,
				  MRTF_WC | MRTF_PMBR, DONT_CREATE);
	if (mrtentry_ptr != (mrtentry_t *) NULL)
	    if (!(mrtentry_ptr->flags & MRTF_KERNEL_CACHE))
		if (mrtentry_ptr->flags & MRTF_WC)
		{
		    mrtentry_ptr =
			mrtentry_ptr->group->active_rp_grp->rp->rpentry->mrtlink;
		}
    }
    else
    {
	mrtentry_ptr = find_route(&source, &group,
				MRTF_SG | MRTF_WC | MRTF_PMBR, DONT_CREATE);
	if ((mrtentry_ptr != (mrtentry_t *) NULL))
	    if (!(mrtentry_ptr->flags & MRTF_KERNEL_CACHE))
	    {
		if (mrtentry_ptr->flags & MRTF_SG)
		{
		    mrtentry_ptr2 = mrtentry_ptr->group->grp_route;
		    if ((mrtentry_ptr2 != (mrtentry_t *) NULL)
			&& (mrtentry_ptr2->flags & MRTF_KERNEL_CACHE))
			mrtentry_ptr = mrtentry_ptr2;
		    else
			mrtentry_ptr = mrtentry_ptr->group->active_rp_grp->rp->rpentry->mrtlink;
		}
		else
		    if (mrtentry_ptr->flags & MRTF_WC)
			mrtentry_ptr = mrtentry_ptr->group->active_rp_grp->rp->rpentry->mrtlink;
	    }
    }
    if ((mrtentry_ptr == (mrtentry_t *) NULL)
	|| (!(mrtentry_ptr->flags & MRTF_KERNEL_CACHE)))
	/* No routing entry or not "active" entry. Ignore the assert */
	return (FALSE);

    /* Prepare the local preference and metric */
    if ((mrtentry_ptr->flags & MRTF_PMBR)
	|| ((mrtentry_ptr->flags & MRTF_SG)
	    && (!(mrtentry_ptr->flags & MRTF_RP))))
    {
	/* Either (S,G) (toward S) or (*,*,RP). */
	/* TODO: XXX: get the info from mrtentry, or source or from kernel ? */
	/*
	 * local_metric = mrtentry_ptr->source->metric; local_preference =
	 * mrtentry_ptr->source->preference;
	 */
	local_metric = mrtentry_ptr->metric;
	local_preference = mrtentry_ptr->preference;
    }
    else
    {
	/*
	 * Should be (*,G) or (S,G)RPbit entry. Get what we need from the RP
	 * info.
	 */
	/* TODO: get the info from mrtentry, RP-entry or kernel? */
	/*
	 * local_metric =
	 * mrtentry_ptr->group->active_rp_grp->rp->rpentry->metric;
	 * local_preference =
	 * mrtentry_ptr->group->active_rp_grp->rp->rpentry->preference;
	 */

	local_metric = mrtentry_ptr->metric;
	local_preference = mrtentry_ptr->preference;
    }

    local_rptbit = (mrtentry_ptr->flags & MRTF_RP);
    if (local_rptbit)
	/* Make the RPT bit the most significant one */
	local_preference |= PIM_ASSERT_RPT_BIT;


    if (IF_ISSET(mifi, &mrtentry_ptr->oifs))
    {
	/* The ASSERT has arrived on oif */

	/*
	 * TODO: XXX: here the processing order is different from the spec.
	 * The spec requires first eventually to create a routing entry (see
	 * 3.5.2.1(1) and then compare the metrics. Here we compare first the
	 * metrics with the existing longest match entry and if we lose then
	 * create a new entry and compare again. This saves us the
	 * unnecessary creating of a routing entry if we anyway are going to
	 * lose: for example the local (*,*,RP) vs the remote (*,*,RP) or
	 * (*,G)
	 */

	local_wins = compare_metrics(local_preference, local_metric,
				     &v->uv_linklocal->pa_addr, assert_preference,
				     assert_metric, src);

	if (local_wins == TRUE)
	{
	    /* TODO: verify the parameters */
	    send_pim6_assert(&source, &group, mifi, mrtentry_ptr);
	    return (TRUE);
	}

	/* Create a "better" routing entry and try again */

	if ((assert_rptbit) && (mrtentry_ptr->flags & MRTF_PMBR))
	{
	    /* The matching entry was (*,*,RP). Create (*,G) */
	    mrtentry_ptr2 = find_route(NULL, &group, MRTF_WC, CREATE);
	}
	else
	    if ((!assert_rptbit) &&
		(mrtentry_ptr->flags & (MRTF_WC | MRTF_PMBR)))
	    {
		/* create (S,G) */
		mrtentry_ptr2 = find_route(&source, &group, MRTF_SG, CREATE);
	    }
	    else
	    {
		/* We have no chance to win. Give up and prune the oif */
		mrtentry_ptr2 = (mrtentry_t *) NULL;
	    }

	if (mrtentry_ptr2 != (mrtentry_t *) NULL)
	{
	    mrtentry_ptr2->flags &= ~MRTF_NEW;

	    /*
	     * TODO: XXX: The spec doesn't say what entry timer value to use
	     * when the routing entry is created because of asserts.
	     */

	    SET_TIMER(mrtentry_ptr2->timer, pim_data_timeout);
	    if (mrtentry_ptr2->flags & MRTF_RP)
	    {
		/*
		 * Either (*,G) or (S,G)RPbit entry. Get what we need from
		 * the RP info.
		 */
		/* TODO: where to get the metric+preference from? */
		/*
		 * local_metric =
		 * mrtentry_ptr->group->active_rp_grp->rp->rpentry->metric;
		 * local_preference =
		 * mrtentry_ptr->group->active_rp_grp->rp->rpentry->preference
		 * ;
		 */
		local_metric = mrtentry_ptr->metric;
		local_preference = mrtentry_ptr->preference;
		local_preference |= PIM_ASSERT_RPT_BIT;
	    }
	    else
	    {
		/* (S,G) toward the source */
		/* TODO: where to get the metric from ? */
		/*
		 * local_metric = mrtentry_ptr->source->metric;
		 * local_preference = mrtentry_ptr->source->preference;
		 */
		local_metric = mrtentry_ptr->metric;
		local_preference = mrtentry_ptr->preference;
	    }

	    local_wins = compare_metrics(local_preference, local_metric,
					 &v->uv_linklocal->pa_addr, assert_preference,
					 assert_metric, src);

	    if (local_wins == TRUE)
	    {
		/* TODO: verify the parameters */
		send_pim6_assert(&source, &group, mifi, mrtentry_ptr);
		return (TRUE);
	    }
	    /* We lost, but have created the entry which has to be pruned */
	    mrtentry_ptr = mrtentry_ptr2;
	}

	/* Have to remove that outgoing vifi from mrtentry_ptr */
	IF_SET(mifi, &mrtentry_ptr->asserted_oifs);
	/* TODO: XXX: TIMER implem. dependency! */
	if (mrtentry_ptr->timer < pim_assert_timeout)
	    SET_TIMER(mrtentry_ptr->timer, pim_assert_timeout);
	/*
	 * TODO: XXX: check that the timer of all affected routing entries
	 * has been restarted.
	 */
	change_interfaces(mrtentry_ptr,
			  mrtentry_ptr->incoming,
			  &mrtentry_ptr->joined_oifs,
			  &mrtentry_ptr->pruned_oifs,
			  &mrtentry_ptr->leaves,
			  &mrtentry_ptr->asserted_oifs, 0);
	return (FALSE);		/* Doesn't matter the return value */
    }				/* End of assert received on oif */


    if (mrtentry_ptr->incoming == mifi)
    {
	/* Assert received on iif */
	if (assert_rptbit)
	{
	    if (!(mrtentry_ptr->flags & MRTF_RP))
		return (TRUE);	/* The locally used upstream router will win
				 * the assert, so don't change it. */
	}

	/*
	 * TODO: where to get the local metric and preference from? system
	 * call or mrtentry is fine?
	 */
	local_metric = mrtentry_ptr->metric;
	local_preference = mrtentry_ptr->preference;
	if (mrtentry_ptr->flags & MRTF_RP)
	    local_preference |= PIM_ASSERT_RPT_BIT;

	local_wins = compare_metrics(local_preference, local_metric,
				     &mrtentry_ptr->upstream->address,
				     assert_preference, assert_metric, src);

	if (local_wins == TRUE)
	    return (TRUE);	/* return whatever */

	/* The upstream must be changed to the winner */
	mrtentry_ptr->preference = assert_preference;
	mrtentry_ptr->metric = assert_metric;
	mrtentry_ptr->upstream = find_pim6_nbr(src);

	/* Check if the upstream router is different from the original one */
	if (mrtentry_ptr->flags & MRTF_PMBR)
	    original_upstream_router = mrtentry_ptr->source->upstream;
	else
	    if (mrtentry_ptr->flags & MRTF_RP)
		original_upstream_router =
		    mrtentry_ptr->group->active_rp_grp->rp->rpentry->upstream;
	    else
		original_upstream_router = mrtentry_ptr->source->upstream;
	if (mrtentry_ptr->upstream != original_upstream_router)
	{
	    mrtentry_ptr->flags |= MRTF_ASSERTED;
	    SET_TIMER(mrtentry_ptr->assert_timer, pim_assert_timeout);
	}
	else
	    mrtentry_ptr->flags &= ~MRTF_ASSERTED;
    }

    return (TRUE);
}


int
send_pim6_assert(source, group, mifi, mrtentry_ptr)
    struct sockaddr_in6	*source;
    struct sockaddr_in6 *group;
    mifi_t          mifi;
    mrtentry_t     *mrtentry_ptr;
{
    u_int8         *data_ptr;
    u_int8         *data_start_ptr;
    u_int32         local_preference;
    u_int32         local_metric;
    srcentry_t     *srcentry_ptr;

    /* Don't send assert if the outgoing interface a tunnel or register vif */
    if (uvifs[mifi].uv_flags & (MIFF_REGISTER | VIFF_TUNNEL))
	return (FALSE);

    data_ptr = (u_int8 *) (pim6_send_buf + sizeof(struct pim));
    data_start_ptr = data_ptr;
    PUT_EGADDR6(group->sin6_addr, SINGLE_GRP_MSK6LEN, 0, data_ptr);
    PUT_EUADDR6(source->sin6_addr, data_ptr);

    /*
     * TODO: XXX: where to get the metric from: srcentry_ptr or mrtentry_ptr
     * or from the kernel?
     */

    if (mrtentry_ptr->flags & MRTF_PMBR)
    {
	/* (*,*,RP) */
	srcentry_ptr = mrtentry_ptr->source;
	/*
	 * TODO: set_incoming(srcentry_ptr, PIM_IIF_RP);
	 */
    }
    else
	if (mrtentry_ptr->flags & MRTF_RP)
	{
	    /* (*,G) or (S,G)RPbit (iif toward RP) */
	    srcentry_ptr = mrtentry_ptr->group->active_rp_grp->rp->rpentry;
	    /*
	     * TODO: set_incoming(srcentry_ptr, PIM_IIF_RP);
	     */
	}
	else
	{
	    /* (S,G) toward S */
	    srcentry_ptr = mrtentry_ptr->source;
	    /*
	     * TODO: set_incoming(srcentry_ptr, PIM_IIF_SOURCE);
	     */
	}

    /*
     * TODO: check again! local_metric = srcentry_ptr->metric;
     * local_preference = srcentry_ptr->preference;
     */
    local_metric = mrtentry_ptr->metric;
    local_preference = mrtentry_ptr->preference;

    if (mrtentry_ptr->flags & MRTF_RP)
	local_preference |= PIM_ASSERT_RPT_BIT;
    PUT_HOSTLONG(local_preference, data_ptr);
    PUT_HOSTLONG(local_metric, data_ptr);

    send_pim6(pim6_send_buf, &uvifs[mifi].uv_linklocal->pa_addr,
          &allpim6routers_group, PIM_ASSERT,
          data_ptr - data_start_ptr);
    uvifs[mifi].uv_out_pim6_assert++;

    return (TRUE);
}


/* Return TRUE if the local win, otherwise FALSE */
static int
compare_metrics(local_preference, local_metric, local_address,
		remote_preference, remote_metric, remote_address)
    u_int32         		local_preference;
    u_int32         		local_metric;
    struct sockaddr_in6		*local_address;
    u_int32         		remote_preference;
    u_int32         		remote_metric;
    struct sockaddr_in6 	*remote_address;
{
    /* Now lets see who has a smaller gun (aka "asserts war") */
    /*
     * FYI, the smaller gun...err metric wins, but if the same caliber, then
     * the bigger network address wins. The order of threatment is:
     * preference, metric, address.
     */
    /*
     * The RPT bits are already included as the most significant bits of the
     * preferences.
     */
    if (remote_preference > local_preference)
	return TRUE;
    if (remote_preference < local_preference)
	return FALSE;
    if (remote_metric > local_metric)
	return TRUE;
    if (remote_metric < local_metric)
	return FALSE;
    if (inet6_greaterthan(local_address, remote_address))
       return TRUE;

    return FALSE;
}


/************************************************************************
 *                        PIM_BOOTSTRAP
 ************************************************************************/
#define	PIM6_BOOTSTRAP_MINLEN (PIM_MINLEN + PIM6_ENCODE_UNI_ADDR_LEN)

int
receive_pim6_bootstrap(src, dst, pim_message, datalen)
    struct sockaddr_in6		*src,
                    		*dst;
    char           		*pim_message;
    int             		datalen;
{
    u_int8         		*data_ptr;
    u_int8         		*max_data_ptr;
    u_int16         		new_bsr_fragment_tag;
    u_int8          		new_bsr_hash_masklen;
    u_int8          		new_bsr_priority;
    pim6_encod_uni_addr_t 	new_bsr_uni_addr;
    struct sockaddr_in6         new_bsr_address;
    struct rpfctl   		rpfc;
    pim_nbr_entry_t 		*n,
                   		*rpf_neighbor;
    struct sockaddr_in6         neighbor_addr;
    mifi_t          		mifi,
                    		incoming = NO_VIF;
    int             		min_datalen;
    pim6_encod_grp_addr_t 	curr_group_addr;
    pim6_encod_uni_addr_t 	curr_rp_addr;
    u_int8          		curr_rp_count;
    u_int8          		curr_frag_rp_count;
    u_int16         		reserved_short;
    u_int16         		curr_rp_holdtime;
    u_int8          		curr_rp_priority;
    u_int8          		reserved_byte;
    struct in6_addr    		curr_group_mask;
    grp_mask_t     		*grp_mask_ptr;
    grp_mask_t     		*grp_mask_next;
    rp_grp_entry_t 		*grp_rp_entry_ptr;
    rp_grp_entry_t 		*grp_rp_entry_next;
    struct sockaddr_in6		prefix_h,
				prefix_h2,
				group_,
				rpp_;
    int i;
    struct uvif 		*v;


    if ((mifi=find_vif_direct(src)) == NO_VIF)
    {
	/*
	 * Either a local vif or somehow received PIM_BOOTSTRAP from
	 * non-directly connected router. Ignore it.
	 */
	if (local_address(src) == NO_VIF)
	    log(LOG_INFO, 0,
		"Ignoring PIM_BOOTSTRAP from non-neighbor router %s",
		inet6_fmt(&src->sin6_addr));
	return (FALSE);
    }

    /* sanity check for the minimum length */
    if (datalen < PIM6_BOOTSTRAP_MINLEN) {
	    log(LOG_NOTICE, 0,
		"receive_pim6_bootstrap: Bootstrap message size(%u) is"
		" too short from %s",
		datalen, inet6_fmt(&src->sin6_addr));
	    return(FALSE);
    }

    v = &uvifs[mifi];
    v->uv_in_pim6_bootsrap++;
    data_ptr = (u_int8 *) (pim_message + sizeof(struct pim));

    /* Parse the PIM_BOOTSTRAP message */
    GET_HOSTSHORT(new_bsr_fragment_tag, data_ptr);
    GET_BYTE(new_bsr_hash_masklen, data_ptr);
    GET_BYTE(new_bsr_priority, data_ptr);
    GET_EUADDR6(&new_bsr_uni_addr, data_ptr);

    /*
     * BSR address must be a global unicast address.
     * [draft-ietf-pim-ipv6-01.txt sec 4.5]
     */
    if (IN6_IS_ADDR_MULTICAST(&new_bsr_uni_addr.unicast_addr) ||
	IN6_IS_ADDR_LINKLOCAL(&new_bsr_uni_addr.unicast_addr) ||
	IN6_IS_ADDR_SITELOCAL(&new_bsr_uni_addr.unicast_addr)) {
	    log(LOG_WARNING, 0,
		"receive_pim6_bootstrap: invalid BSR address: %s",
		inet6_fmt(&new_bsr_uni_addr.unicast_addr));
	    return(FALSE);
    }

    new_bsr_address.sin6_addr = new_bsr_uni_addr.unicast_addr;
    new_bsr_address.sin6_len = sizeof(new_bsr_address);
    new_bsr_address.sin6_family = AF_INET6;
    new_bsr_address.sin6_scope_id = inet6_uvif2scopeid(&new_bsr_address, v);

    if (local_address(&new_bsr_address) != NO_VIF)
    {
	IF_DEBUG(DEBUG_RPF | DEBUG_PIM_BOOTSTRAP)
	    log(LOG_DEBUG, 0,
		"receive_pim6_bootstrap: Bootstrap from myself(%s), ignored.",
		inet6_fmt(&new_bsr_address.sin6_addr));
	return (FALSE);		/* The new BSR is one of my local addresses */
    }

    /*
     * Compare the current BSR priority with the priority of the BSR included
     * in the message.
     */
    /*
     * TODO: if I am just starting and will become the BSR, I should accept
     * the message coming from the current BSR and get the current
     * Cand-RP-Set.
     */

    if ((curr_bsr_priority > new_bsr_priority) ||
	((curr_bsr_priority == new_bsr_priority)
	&& (inet6_greaterthan(&curr_bsr_address, &new_bsr_address))))
    {
	/* The message's BSR is less preferred than the current BSR */
	log(LOG_DEBUG, 0,
	    "receive_pim6_bootstrap: BSR(%s, prio=%d) is less preferred"
	    " than the current BSR(%s, prio=%d)",
	    inet6_fmt(&new_bsr_address.sin6_addr), new_bsr_priority,
	    inet6_fmt(&curr_bsr_address.sin6_addr), curr_bsr_priority);
	return (FALSE);		/* Ignore the received BSR message */
    }

    /* Check the iif, if this was PIM-ROUTERS multicast */
    if (IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &allpim6routers_group.sin6_addr))
    {
	k_req_incoming(&new_bsr_address, &rpfc);
	if ((rpfc.iif == NO_VIF) ||
	    IN6_IS_ADDR_UNSPECIFIED(&rpfc.rpfneighbor.sin6_addr))
	{
	    /* coudn't find a route to the BSR */
	    log(LOG_NOTICE, 0,
		"receive_pim6_bootstrap: can't find a route to the BSR(%s)",
		inet6_fmt(&new_bsr_address.sin6_addr));
	    return (FALSE);
	}

	neighbor_addr = *src;
	incoming = rpfc.iif;

	if (uvifs[incoming].uv_flags &
	    (VIFF_DISABLED | VIFF_DOWN | MIFF_REGISTER))
	{
	  log(LOG_NOTICE, 0,
	      "receive_pim6_bootstrap: Bootstrap from an invalid interface(%s)",
	      uvifs[incoming].uv_name);
	    return (FALSE);	/* Shoudn't arrive on that interface */
	}

	/* Find the upstream router */

	for (n = uvifs[incoming].uv_pim_neighbors; n != NULL; n = n->next)
	{
	    if (inet6_lessthan(&neighbor_addr, &n->address))
		continue;
	    if (inet6_equal(&neighbor_addr, &n->address))
	    {
		rpf_neighbor = n;
		break;
	    }
	    log(LOG_NOTICE, 0,
		"receive_pim6_bootstrap: Bootstrap from an unrecognized "
		"neighbor(%s) on %s",
		inet6_fmt(&neighbor_addr.sin6_addr), uvifs[incoming].uv_name);
	    return (FALSE);	/* No neighbor toward BSR found */
	}

	/* redundant checks? */
	if ((n == (pim_nbr_entry_t *) NULL ))
	{
	    return (FALSE);	/* Sender of this message is not the RPF*/
	}
				 			/* neighbor */
	if(!(inet6_equal(&n->address, src)))
	{
		return (FALSE);
	}
    }
    else
    {
	if (local_address(dst) == NO_VIF)
	    /*
	     * TODO: XXX: this situation should be handled earlier: The
	     * destination is neither ALL_PIM_ROUTERS nor me
	     */
	    log(LOG_NOTICE, 0,
		"receive_pim6_bootstrap: Bootstrap with an invalid dst(%s)",
		inet6_fmt(&dst->sin6_addr));
	    return (FALSE);

	/* Probably unicasted from the current DR */
	if (cand_rp_list != (cand_rp_t *) NULL)
	{
	    /*
	     * Hmmm, I do have a Cand-RP-list, but some neighbor has a
	     * different opinion and is unicasting it to me. Ignore this guy.
	     */
	    log(LOG_INFO, 0,
		"receive_pim6_bootstrap: Bootstrap received but we already "
		"have RPs. ignored.");
	    return (FALSE);
	}
	for (mifi = 0; mifi < numvifs; mifi++)
	{
	    if (uvifs[mifi].uv_flags & (VIFF_DISABLED | VIFF_DOWN |
					MIFF_REGISTER))
		continue;
	    if (inet6_equal(&uvifs[mifi].uv_linklocal->pa_addr,dst))
	    {
		incoming = mifi;
		break;
	    }
	}
	if (incoming == NO_VIF)
	{
	    /* Cannot find the receiving iif toward that DR */
	    IF_DEBUG(DEBUG_RPF | DEBUG_PIM_BOOTSTRAP)
		log(LOG_DEBUG, 0,
		    "Unicast boostrap message from %s to %s ignored: "
		    "cannot find iif",
		    inet6_fmt(&src->sin6_addr), inet6_fmt(&dst->sin6_addr));
	    return (FALSE);
	}
	/*
	 * TODO: check the sender is directly connected and I am really the
	 * DR.
	 */
    }

    if (cand_rp_flag == TRUE)
    {
	/* If change in the BSR address, send immediately Cand-RP-Adv */
	/* TODO: use some random delay? */

	if (!inet6_equal(&new_bsr_address , &curr_bsr_address))
	{
	    send_pim6_cand_rp_adv();
	    SET_TIMER(pim_cand_rp_adv_timer, my_cand_rp_adv_period);
	}
    }

    /* Forward the BSR Message first and then update the RP-set list */
    /* XXX: should we do sanity checks before forwarding?? */
    /* TODO: if the message was unicasted to me, resend? */

    for (mifi = 0; mifi < numvifs; mifi++)
    {
	if (mifi == incoming)
	    continue;
	if (uvifs[mifi].uv_flags & (VIFF_DISABLED | VIFF_DOWN |
				    MIFF_REGISTER | VIFF_TUNNEL | VIFF_NONBRS))
	    continue;

	bcopy(pim_message, (char *)(pim6_send_buf), datalen);

	send_pim6(pim6_send_buf, &uvifs[mifi].uv_linklocal->pa_addr,
		  &allpim6routers_group, PIM_BOOTSTRAP,
		  datalen - sizeof(struct pim));
    }

    max_data_ptr = (u_int8 *) pim_message + datalen;

    /*
     * TODO: XXX: this 24 is HARDCODING!!! Do a bunch of definitions and make
     * it stylish!
     * 24 = Encoded-Group Address(20) + RP-cound(1) + Frag-RP(1) + Reserved(2)
     */
    min_datalen = 24;

    if ((new_bsr_fragment_tag != curr_bsr_fragment_tag) ||
	(inet6_equal(&new_bsr_address, &curr_bsr_address)))
    {
	/* Throw away the old segment */
	delete_rp_list(&segmented_cand_rp_list, &segmented_grp_mask_list);
    }

    curr_bsr_address = new_bsr_address;
    curr_bsr_priority = new_bsr_priority;
    curr_bsr_fragment_tag = new_bsr_fragment_tag;
    MASKLEN_TO_MASK6(new_bsr_hash_masklen, curr_bsr_hash_mask);
    SET_TIMER(pim_bootstrap_timer, PIM_BOOTSTRAP_TIMEOUT);

    while (data_ptr + min_datalen <= max_data_ptr)
    {
	GET_EGADDR6(&curr_group_addr, data_ptr);
	GET_BYTE(curr_rp_count, data_ptr);
	GET_BYTE(curr_frag_rp_count, data_ptr);
	GET_HOSTSHORT(reserved_short, data_ptr);
	MASKLEN_TO_MASK6(curr_group_addr.masklen, curr_group_mask);
	if (curr_rp_count == 0)
	{
	    group_.sin6_addr = curr_group_addr.mcast_addr;
	    delete_grp_mask(&cand_rp_list, &grp_mask_list,
			    &group_, curr_group_mask);
	    continue;
	}
	if (curr_rp_count == curr_frag_rp_count)
    	{
            /* Add all RPs */
	    while (curr_frag_rp_count--)
	    {
		/*
		 * Sanity for the data length; the data packet must contain
		 * Encoded-Unicast-RP-Address(18) + RP-Holdtime(2) +
		 * RP-Priority(1) + Reserved(1).
		 */
		if (data_ptr + PIM6_ENCODE_UNI_ADDR_LEN + sizeof(u_int32_t)
		    > max_data_ptr) {
		    log(LOG_NOTICE, 0,
			"receive_pim6_bootstrap: Bootstrap from %s on %s "
			"does not have enough length to contatin RP information",
			inet6_fmt(&src->sin6_addr), v->uv_name);

		    /*
		     * Ignore the rest of the message.
		     * XXX: should we discard the entire message?
		     */
		    goto garbage_collect;
		}

		GET_EUADDR6(&curr_rp_addr, data_ptr);
		GET_HOSTSHORT(curr_rp_holdtime, data_ptr);
		GET_BYTE(curr_rp_priority, data_ptr);
		GET_BYTE(reserved_byte, data_ptr);
		MASKLEN_TO_MASK6(curr_group_addr.masklen, curr_group_mask);
		rpp_.sin6_addr = curr_rp_addr.unicast_addr;
    		rpp_.sin6_len = sizeof(rpp_);
    		rpp_.sin6_family = AF_INET6;
		/*
		 * The cand_rp address scope should be global.
		 * XXX: however, is a site-local RP sometimes useful?
		 *      we currently discard such RP...
		 */
		if (IN6_IS_ADDR_LINKLOCAL(&rpp_.sin6_addr) ||
		    IN6_IS_ADDR_SITELOCAL(&rpp_.sin6_addr)) {
			log(LOG_WARNING, 0,
			    "receive_pim6_bootstrap: invalid RP address: %s",
			    inet6_fmt(&rpp_.sin6_addr));
			continue;
		}
    		rpp_.sin6_scope_id = 0;
		group_.sin6_addr = curr_group_addr.mcast_addr;
		group_.sin6_len = sizeof(group_);
		group_.sin6_family = AF_INET6;
		group_.sin6_scope_id = inet6_uvif2scopeid(&group_,v);

		add_rp_grp_entry(&cand_rp_list, &grp_mask_list,
				 &rpp_, curr_rp_priority,
				 curr_rp_holdtime, &group_,
				 curr_group_mask,
				 curr_bsr_hash_mask,
				 curr_bsr_fragment_tag);
	    }
	    continue;
	}
	/*
	 * This is a partial list of the RPs for this group prefix. Save
	 * until all segments arrive.
	 */

	for (i = 0; i < sizeof(struct in6_addr); i++) {
	    prefix_h.sin6_addr.s6_addr[i] =
		curr_group_addr.mcast_addr.s6_addr[i]
		    & curr_group_mask.s6_addr[i];
	}

	for (grp_mask_ptr = segmented_grp_mask_list;
	     grp_mask_ptr != (grp_mask_t *) NULL;
	     grp_mask_ptr = grp_mask_ptr->next)
	{
	    for (i = 0; i < sizeof(struct in6_addr); i++) {
		prefix_h2.sin6_addr.s6_addr[i] =
		    grp_mask_ptr->group_addr.sin6_addr.s6_addr[i]
			& grp_mask_ptr->group_mask.s6_addr[i];
	    }

	    if (inet6_lessthan(&prefix_h2, &prefix_h))
		continue;
	    else
		break;
	}
	if ((grp_mask_ptr != (grp_mask_t *) NULL)
	    && (IN6_ARE_ADDR_EQUAL(&grp_mask_ptr->group_addr.sin6_addr,
				   &curr_group_addr.mcast_addr))
	    && (IN6_ARE_ADDR_EQUAL(&grp_mask_ptr->group_mask, &curr_group_mask))
	    && (grp_mask_ptr->group_rp_number + curr_frag_rp_count
		== curr_rp_count))
	{
	    /* All missing PRs have arrived. Add all RP entries */
	    while (curr_frag_rp_count--)
	    {
		GET_EUADDR6(&curr_rp_addr, data_ptr);
		GET_HOSTSHORT(curr_rp_holdtime, data_ptr);
		GET_BYTE(curr_rp_priority, data_ptr);
		GET_BYTE(reserved_byte, data_ptr);
		MASKLEN_TO_MASK6(curr_group_addr.masklen, curr_group_mask);
		rpp_.sin6_addr = curr_rp_addr.unicast_addr;
		rpp_.sin6_scope_id=0;
		group_.sin6_addr = curr_group_addr.mcast_addr;
		group_.sin6_scope_id = inet6_uvif2scopeid(&group_,v);
		add_rp_grp_entry(&cand_rp_list,
				 &grp_mask_list,
				 &rpp_,
				 curr_rp_priority,
				 curr_rp_holdtime,
				 &group_,
				 curr_group_mask,
				 curr_bsr_hash_mask,
				 curr_bsr_fragment_tag);
	    }
	    /* Add the rest from the previously saved segments */
	    for (grp_rp_entry_ptr = grp_mask_ptr->grp_rp_next;
		 grp_rp_entry_ptr != (rp_grp_entry_t *) NULL;
		 grp_rp_entry_ptr = grp_rp_entry_ptr->grp_rp_next)
	    {
		group_.sin6_addr = curr_group_addr.mcast_addr;
		group_.sin6_scope_id = inet6_uvif2scopeid(&group_,v);
		add_rp_grp_entry(&cand_rp_list,
				 &grp_mask_list,
				 &grp_rp_entry_ptr->rp->rpentry->address,
				 grp_rp_entry_ptr->priority,
				 grp_rp_entry_ptr->holdtime,
				 &group_,
				 curr_group_mask,
				 curr_bsr_hash_mask,
				 curr_bsr_fragment_tag);
	    }
	    group_.sin6_addr = curr_group_addr.mcast_addr;
	    group_.sin6_scope_id = inet6_uvif2scopeid(&group_,v);
		delete_grp_mask(&segmented_cand_rp_list,
			    &segmented_grp_mask_list,
			    &group_,
			    curr_group_mask);
	}
	else
	{
	    /* Add the partially received RP-list to the group of pending RPs */
	    while (curr_frag_rp_count--)
	    {
		GET_EUADDR6(&curr_rp_addr, data_ptr);
		GET_HOSTSHORT(curr_rp_holdtime, data_ptr);
		GET_BYTE(curr_rp_priority, data_ptr);
		GET_BYTE(reserved_byte, data_ptr);
		MASKLEN_TO_MASK6(curr_group_addr.masklen, curr_group_mask);
		rpp_.sin6_addr = curr_rp_addr.unicast_addr;
		group_.sin6_addr = curr_group_addr.mcast_addr;
		group_.sin6_scope_id = inet6_uvif2scopeid(&group_,v);
		add_rp_grp_entry(&segmented_cand_rp_list,
				 &segmented_grp_mask_list,
				 &rpp_,
				 curr_rp_priority,
				 curr_rp_holdtime,
				 &group_,
				 curr_group_mask,
				 curr_bsr_hash_mask,
				 curr_bsr_fragment_tag);
	    }
	}
    }


 garbage_collect:
    /*
     * Garbage collection. Check all group prefixes and if the fragment_tag
     * for a group_prefix is the same as curr_bsr_fragment_tag, then remove
     * all RPs for this group_prefix which have different fragment tag.
     */

    for (grp_mask_ptr = grp_mask_list;
	 grp_mask_ptr != (grp_mask_t *) NULL;
	 grp_mask_ptr = grp_mask_next)
    {
	grp_mask_next = grp_mask_ptr->next;
	if (grp_mask_ptr->fragment_tag == curr_bsr_fragment_tag)
	{
	    for (grp_rp_entry_ptr = grp_mask_ptr->grp_rp_next;
		 grp_rp_entry_ptr != (rp_grp_entry_t *) NULL;
		 grp_rp_entry_ptr = grp_rp_entry_next)
	    {
		grp_rp_entry_next = grp_rp_entry_ptr->grp_rp_next;
		if (grp_rp_entry_ptr->fragment_tag != curr_bsr_fragment_tag)
		    delete_rp_grp_entry(&cand_rp_list, &grp_mask_list,
					grp_rp_entry_ptr);
	    }
	}
    }

    /* Cleanup also the list used by incompleted segments */

    for (grp_mask_ptr = segmented_grp_mask_list;
	 grp_mask_ptr != (grp_mask_t *) NULL;
	 grp_mask_ptr = grp_mask_next)
    {
	grp_mask_next = grp_mask_ptr->next;
	if (grp_mask_ptr->fragment_tag == curr_bsr_fragment_tag)
	{
	    for (grp_rp_entry_ptr = grp_mask_ptr->grp_rp_next;
		 grp_rp_entry_ptr != (rp_grp_entry_t *) NULL;
		 grp_rp_entry_ptr = grp_rp_entry_next)
	    {
		grp_rp_entry_next = grp_rp_entry_ptr->grp_rp_next;
		if (grp_rp_entry_ptr->fragment_tag != curr_bsr_fragment_tag)
		    delete_rp_grp_entry(&segmented_cand_rp_list,
					&segmented_grp_mask_list,
					grp_rp_entry_ptr);
	    }
	}
    }

    return (TRUE);
}

void
send_pim6_bootstrap()
{
    int             datalen;
    mifi_t          mifi;

    if ((datalen = create_pim6_bootstrap_message(pim6_send_buf)))
    {
	for (mifi = 0; mifi < numvifs; mifi++)
	{
	    if (uvifs[mifi].uv_flags & (VIFF_DISABLED | VIFF_DOWN |
					MIFF_REGISTER | VIFF_TUNNEL))
		continue;

   	     send_pim6(pim6_send_buf, &uvifs[mifi].uv_linklocal->pa_addr,
          	       &allpim6routers_group, PIM_BOOTSTRAP, datalen);
	     uvifs[mifi].uv_out_pim6_bootsrap++;
	}
    }
}

/************************************************************************
 *                        PIM_CAND_RP_ADV
 ************************************************************************/
/*
 * minimum length of a cand. RP adv. message;
 * length of PIM header + prefix-cnt(1) + priority(1) + holdtime(2) +
 *                        encoded unicast RP addr(18)
  */
#define	PIM6_CAND_RP_ADV_MINLEN (PIM_MINLEN + PIM6_ENCODE_UNI_ADDR_LEN)

/*
 * If I am the Bootstrap router, process the advertisement, otherwise ignore
 * it.
 */
int
receive_pim6_cand_rp_adv(src, dst, pim_message, datalen)
    struct sockaddr_in6         *src,
                    			*dst;
    char           		*pim_message;
    register int    		datalen;
{
    u_int8          		prefix_cnt;
    u_int8          		priority;
    u_int16         		holdtime;
    pim6_encod_uni_addr_t 	cand_rp_addr;
    pim6_encod_grp_addr_t 	encod_grp_addr;
    u_int8         		*data_ptr;
    struct in6_addr         	grp_mask;
    struct sockaddr_in6		group_, rpp_;

    pim6dstat.in_pim6_cand_rp++;

    /* if I am not the bootstrap RP, then do not accept the message */
    if ((cand_bsr_flag != FALSE) &&
	!inet6_equal(&curr_bsr_address, &my_bsr_address))
    {
	log(LOG_NOTICE, 0,
	    "receive_pim6_cand_rp_adv: receive cand_RP from %s "
	    "but I'm not the BSR",
	    inet6_fmt(&src->sin6_addr));
	return (FALSE);
    }

    /* sanity check for the minimum length */
    if (datalen < PIM6_CAND_RP_ADV_MINLEN) {
	    log(LOG_NOTICE, 0,
		"receive_pim6_cand_rp_adv: cand_RP message size(%u) is"
		" too short from %s",
		datalen, inet6_fmt(&src->sin6_addr));
	    return(FALSE);
    }
    datalen -= PIM6_CAND_RP_ADV_MINLEN;

    data_ptr = (u_int8 *) (pim_message + sizeof(struct pim));
    /* Parse the CAND_RP_ADV message */
    GET_BYTE(prefix_cnt, data_ptr);
    GET_BYTE(priority, data_ptr);
    GET_HOSTSHORT(holdtime, data_ptr);
    GET_EUADDR6(&cand_rp_addr, data_ptr);

    /*
     * The RP Address field is set to the globally reachable IPv6 address
     * [draft-ietf-pim-ipv6].
     */
    if (IN6_IS_ADDR_LINKLOCAL(&cand_rp_addr.unicast_addr)) {
	    /* XXX: prohibit a site-local address as well? */
	    log(LOG_WARNING, 0,
		"receive_pim6_cand_rp_adv: non global address(%s) as RP",
		inet6_fmt(&cand_rp_addr.unicast_addr));
	    return(FALSE);
    }

    memset(&rpp_, 0, sizeof(rpp_));
    if (prefix_cnt == 0)
    {
	/* The default ff:: and masklen of 8 */
	MASKLEN_TO_MASK6(ALL_MCAST_GROUPS_LENGTH, grp_mask);
	rpp_.sin6_addr = cand_rp_addr.unicast_addr;
	/*
	 * note that we don't have to take care of scope id, since
	 * the address should be global(see above).
	 */
	add_rp_grp_entry(&cand_rp_list, &grp_mask_list,
			 &rpp_, priority, holdtime,
			 &sockaddr6_d, grp_mask,
			 my_bsr_hash_mask,
			 curr_bsr_fragment_tag);
	return (TRUE);
    }
    while (prefix_cnt--)
    {
	    /*
	     * Sanity check for the message length.
	     * XXX: do we have to do the check at an earlier stage and
	     *      discard the whole message (instead of adopting a part of it)
	     *      if it's bogus?
	     */
	    if (datalen < PIM6_ENCODE_GRP_ADDR_LEN) {
		    log(LOG_NOTICE, 0,
			"receive_pim6_cand_rp_adv: cand_RP message from %s is"
			" too short to contain enough groups",
			inet6_fmt(&src->sin6_addr));
		    return(FALSE);
	    }
	    datalen -= PIM6_ENCODE_GRP_ADDR_LEN;

	    GET_EGADDR6(&encod_grp_addr, data_ptr);
	    MASKLEN_TO_MASK6(encod_grp_addr.masklen, grp_mask);
	    group_.sin6_addr = encod_grp_addr.mcast_addr;
	    group_.sin6_scope_id = 0; /* XXX: what if for scoped multicast addr? */
	    rpp_.sin6_addr = cand_rp_addr.unicast_addr;
	    /* see above note on scope id */

	    add_rp_grp_entry(&cand_rp_list, &grp_mask_list,
			     &rpp_, priority, holdtime,
			     &group_, grp_mask,
			     my_bsr_hash_mask,
			     curr_bsr_fragment_tag);
    }

    return (TRUE);
}

int
send_pim6_cand_rp_adv()
{
    u_int8          		prefix_cnt;
    struct in6_addr  		grp_mask;
    pim6_encod_grp_addr_t 	encod_grp_addr;
    u_int8         		*data_ptr;
    struct sockaddr_in6		group_;

    if (!inet6_valid_host(&curr_bsr_address))
	return (FALSE);		/* No BSR yet */

    if( inet6_equal(&curr_bsr_address, &my_bsr_address))
    {
	/* I am the BSR and have to include my own group_prefix stuff */
	prefix_cnt = *cand_rp_adv_message.prefix_cnt_ptr;
	if (prefix_cnt == 0)
	{
	    /* The default ff00:: and masklen of 8 */
	    MASKLEN_TO_MASK6(ALL_MCAST_GROUPS_LENGTH, grp_mask);
	    add_rp_grp_entry(&cand_rp_list, &grp_mask_list,
			     &my_cand_rp_address, my_cand_rp_priority,
			     my_cand_rp_holdtime,
			     &sockaddr6_d,
			     grp_mask,
			     my_bsr_hash_mask,
			     curr_bsr_fragment_tag);
	    return (TRUE);
	}
	/* TODO: hardcoding!! */
	/* 18 = sizeof(pim6_encod_uni_addr_t) without padding */
	data_ptr = cand_rp_adv_message.buffer + (4 + 18);

	while (prefix_cnt--)
	{
	    GET_EGADDR6(&encod_grp_addr, data_ptr);
	    MASKLEN_TO_MASK6(encod_grp_addr.masklen, grp_mask);
	    group_.sin6_addr = encod_grp_addr.mcast_addr;
	    group_.sin6_scope_id = 0;			/*XXX */
		add_rp_grp_entry(&cand_rp_list,
			     &grp_mask_list,
			     &my_cand_rp_address, my_cand_rp_priority,
			     my_cand_rp_holdtime,
			     &group_, grp_mask,
			     my_bsr_hash_mask,
			     curr_bsr_fragment_tag);
	}
	return (TRUE);
    }

    data_ptr = (u_int8 *) (pim6_send_buf + sizeof(struct pim));

    bcopy((char *)cand_rp_adv_message.buffer, (char *) data_ptr,
	  cand_rp_adv_message.message_size);

    send_pim6(pim6_send_buf, &my_cand_rp_address, &curr_bsr_address ,
            PIM_CAND_RP_ADV, cand_rp_adv_message.message_size);
    pim6dstat.out_pim6_cand_rp++;

    return TRUE;
}