summaryrefslogtreecommitdiffstats
path: root/usr.sbin/pim6dd/pim6_proto.c
blob: 8a39a5e18d48cdd3bd27e887c4aeba3fe545ebf2 (plain)
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
/*
 * 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 Oregon.
 *  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 Oregon.
 *  The name of the University of Oregon may not be used to endorse or 
 *  promote products derived from this software without specific prior 
 *  written permission.
 *
 *  THE UNIVERSITY OF OREGON 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 UO, 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.
 */
/*
 *  Questions concerning this software should be directed to 
 *  Kurt Windisch (kurtw@antc.uoregon.edu)
 *
 *  $Id: pim6_proto.c,v 1.6 2000/10/05 22:20:38 itojun Exp $
 */
/*
 * Part of this program has been derived from PIM sparse-mode pimd.
 * The pimd program is covered by the license in the accompanying file
 * named "LICENSE.pimd".
 *  
 * The pimd program is COPYRIGHT 1998 by University of Southern California.
 *
 * Part of this program has been derived from mrouted.
 * The mrouted program is covered by the license in the accompanying file
 * named "LICENSE.mrouted".
 * 
 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
 * Leland Stanford Junior University.
 *
 * $FreeBSD$
 */

#include "defs.h"

/*
 * Local functions definitions.
 */
static int parse_pim6_hello __P((char *pktPtr, int datalen,
				 struct sockaddr_in6 *src,
				 u_int16 *holdtime));
static void delayed_join_job __P((void *));
static void schedule_delayed_join __P((mrtentry_t *, struct sockaddr_in6 *));
static void delayed_prune_job __P((void *));
static void schedule_delayed_prune __P((mrtentry_t *, mifi_t, u_int16));
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));
static int retransmit_pim6_graft __P((mrtentry_t *));
static void retransmit_all_pim6_grafts __P((void *));

if_set nbr_mifs;    /* Mifs that have one or more neighbors attached */

/************************************************************************
 *                        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;
	u_int8  *data_ptr;
	int state_change;
	srcentry_t *srcentry_ptr;
	srcentry_t *srcentry_ptr_next;
	mrtentry_t *mrtentry_ptr;
	u_long random_delay;

	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];
	if (v->uv_flags & (VIFF_DOWN | VIFF_DISABLED))
		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 the 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;
	new_nbr->timer            = holdtime;
	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;
	IF_SET(mifi, &nbr_mifs);

	/* Elect a new DR */
	if (inet6_lessthan(&v->uv_linklocal->pa_addr,
			   &v->uv_pim_neighbors->address)) {
		/* The first address is the new potential remote
		 * DR address and it wins (is >) over the local address.
		 */
		v->uv_flags &= ~VIFF_DR;
	}

	/*
	 * Since a new neighbour has come up, let it know your existence ASAP;
	 * compute a random value, and reset the value to the hello timer
	 * if it's smaller than the rest of the timer.
	 * XXX: not in the spec...
	 */
	random_delay = 1 + (random() % (long)(PIM_TIMER_HELLO_PERIOD - 1));
	if (random_delay < v->uv_pim_hello_timer)
		v->uv_pim_hello_timer = random_delay;

	/* 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->incoming == mifi)
			continue;

		for (mrtentry_ptr = srcentry_ptr->mrtlink;
		     mrtentry_ptr != (mrtentry_t *)NULL;
		     mrtentry_ptr = mrtentry_ptr->srcnext) {

			if(!(IF_ISSET(mifi, &mrtentry_ptr->oifs))) {
				state_change = 
					change_interfaces(mrtentry_ptr,
							  srcentry_ptr->incoming,
							  &mrtentry_ptr->pruned_oifs,
							  &mrtentry_ptr->leaves,
							  &mrtentry_ptr->asserted_oifs);
				if(state_change == 1)
					trigger_join_alert(mrtentry_ptr);
			}
		}
	}
    
	IF_DEBUG(DEBUG_PIM_HELLO)
		dump_vifs(stderr);     	/* Show we got a new neighbor */
	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;
	struct uvif *v;
	int state_change;

	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;
    
	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 | VIFF_QUERIER);
		IF_CLR(nbr_delete->vifi, &nbr_mifs);
	}
	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;
		}
	}

	/* Update the source entries:
	 * If the deleted nbr was my upstream, then reset incoming and
	 * update all (S,G) entries for sources reachable through it.
	 * If the deleted nbr was the last on a non-iif vif, then recalcuate
	 * outgoing interfaces.
	 */
	for (srcentry_ptr = srclist; srcentry_ptr != (srcentry_t *)NULL;
	     srcentry_ptr = srcentry_ptr_next) {
		srcentry_ptr_next = srcentry_ptr->next;
	
		/* The only time we don't need to scan all mrtentries is
		 * when the nbr was on the iif, but not the upstream nbr! 
		 */
		if (nbr_delete->vifi == srcentry_ptr->incoming &&
		    srcentry_ptr->upstream != nbr_delete)
			continue;

		/* Reset the next hop (PIM) router */
		if(srcentry_ptr->upstream == nbr_delete) 
			if (set_incoming(srcentry_ptr, PIM_IIF_SOURCE) == FALSE) {
				/*
				 * Couldn't reset it. Sorry, the next 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);
				free((char *)nbr_delete);
				return;
			}

		for (mrtentry_ptr = srcentry_ptr->mrtlink;
		     mrtentry_ptr != (mrtentry_t *)NULL;
		     mrtentry_ptr = mrtentry_ptr->srcnext) {
			mrtentry_ptr->incoming   = srcentry_ptr->incoming;
			mrtentry_ptr->upstream   = srcentry_ptr->upstream;
			mrtentry_ptr->metric     = srcentry_ptr->metric;
			mrtentry_ptr->preference = srcentry_ptr->preference;
			state_change = 
				change_interfaces(mrtentry_ptr,
						  srcentry_ptr->incoming,
						  &mrtentry_ptr->pruned_oifs,
						  &mrtentry_ptr->leaves,
						  &mrtentry_ptr->asserted_oifs);
			if(state_change == -1) {
				trigger_prune_alert(mrtentry_ptr);
			} else if(state_change == 1) {
				trigger_join_alert(mrtentry_ptr);
			}
		}
	}
    
	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);
	v->uv_pim_hello_timer = PIM_TIMER_HELLO_PERIOD;
	return(TRUE);
}


/************************************************************************
 *                        PIM_JOIN_PRUNE
 ************************************************************************/

typedef struct {
	struct sockaddr_in6 source;
	struct sockaddr_in6 group;
	struct sockaddr_in6 target;
} join_delay_cbk_t;

typedef struct {
	mifi_t mifi;
	struct sockaddr_in6 source;
	struct sockaddr_in6 group;
	u_int16 holdtime;
} prune_delay_cbk_t;
    
static void 
delayed_join_job(arg)
	void *arg;
{
	mrtentry_t *mrtentry_ptr;

	join_delay_cbk_t *cbk = (join_delay_cbk_t *)arg;

	mrtentry_ptr = find_route(&cbk->source, &cbk->group,
				  MRTF_SG, DONT_CREATE);
	if(mrtentry_ptr == (mrtentry_t *)NULL) 
		return;

	if(mrtentry_ptr->join_delay_timerid)
		timer_clearTimer(mrtentry_ptr->join_delay_timerid);

	if(mrtentry_ptr->upstream)
		send_pim6_jp(mrtentry_ptr, PIM_ACTION_JOIN,
			     mrtentry_ptr->incoming,
			     &mrtentry_ptr->upstream->address, 0, 0);

	free(cbk);
}

static void 
schedule_delayed_join(mrtentry_ptr, target) 
	mrtentry_t *mrtentry_ptr;
	struct sockaddr_in6 *target;
{
	u_long random_delay;
	join_delay_cbk_t *cbk;
    
	/* Delete existing timer */
	if(mrtentry_ptr->join_delay_timerid) 
		timer_clearTimer(mrtentry_ptr->join_delay_timerid);

#ifdef SYSV
	random_delay = lrand48() % (long)PIM_RANDOM_DELAY_JOIN_TIMEOUT;
#else
	random_delay = random() % (long)PIM_RANDOM_DELAY_JOIN_TIMEOUT;
#endif
    
	IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
		log(LOG_DEBUG, 0, "Scheduling join for src %s, grp %s, delay %ld",
		    inet6_fmt(&mrtentry_ptr->source->address.sin6_addr),
		    inet6_fmt(&mrtentry_ptr->group->group.sin6_addr),
		    random_delay);

	if(random_delay == 0 && mrtentry_ptr->upstream) {
		send_pim6_jp(mrtentry_ptr, PIM_ACTION_JOIN,
			     mrtentry_ptr->incoming,
			     &mrtentry_ptr->upstream->address, 0, 0);
		return;
	}

	cbk = (join_delay_cbk_t *)malloc(sizeof(join_delay_cbk_t));
	cbk->source = mrtentry_ptr->source->address;
	cbk->group = mrtentry_ptr->group->group;
	cbk->target = *target;

	mrtentry_ptr->join_delay_timerid = 
		timer_setTimer(random_delay, delayed_join_job, cbk);
}


static void 
delayed_prune_job(arg)
	void *arg;
{
	mrtentry_t *mrtentry_ptr;
	if_set new_pruned_oifs;
	int state_change;

	prune_delay_cbk_t *cbk = (prune_delay_cbk_t *)arg;

	mrtentry_ptr = find_route(&cbk->source, &cbk->group,
				  MRTF_SG, DONT_CREATE);
	if(mrtentry_ptr == (mrtentry_t *)NULL) 
		return;
    
	if(mrtentry_ptr->prune_delay_timerids[cbk->mifi])
		timer_clearTimer(mrtentry_ptr->prune_delay_timerids[cbk->mifi]);

	if(IF_ISSET(cbk->mifi, &mrtentry_ptr->oifs)) {
		IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
			log(LOG_DEBUG, 0,
			    "Deleting pruned mif %d for src %s, grp %s",
			    cbk->mifi, 
			    inet6_fmt(&cbk->source.sin6_addr),
			    inet6_fmt(&cbk->group.sin6_addr));

		IF_COPY(&mrtentry_ptr->pruned_oifs, &new_pruned_oifs);
		IF_SET(cbk->mifi, &new_pruned_oifs);
		SET_TIMER(mrtentry_ptr->prune_timers[cbk->mifi], cbk->holdtime);

		state_change = 
			change_interfaces(mrtentry_ptr,
					  mrtentry_ptr->incoming,
					  &new_pruned_oifs,
					  &mrtentry_ptr->leaves,
					  &mrtentry_ptr->asserted_oifs);

		/* Handle transition to negative cache */
		if(state_change == -1)
			trigger_prune_alert(mrtentry_ptr);
	}

	free(cbk);
}

static void 
schedule_delayed_prune(mrtentry_ptr, mifi, holdtime) 
	mrtentry_t *mrtentry_ptr;
	mifi_t mifi;
	u_int16 holdtime;
{
	prune_delay_cbk_t *cbk;
    
	/* Delete existing timer */
	if(mrtentry_ptr->prune_delay_timerids[mifi]) 
		timer_clearTimer(mrtentry_ptr->prune_delay_timerids[mifi]);

	cbk = (prune_delay_cbk_t *)malloc(sizeof(prune_delay_cbk_t));
	cbk->mifi = mifi;
	cbk->source = mrtentry_ptr->source->address;
	cbk->group = mrtentry_ptr->group->group;
	cbk->holdtime = holdtime;

	mrtentry_ptr->prune_delay_timerids[mifi] = 
		timer_setTimer((u_int16)PIM_RANDOM_DELAY_JOIN_TIMEOUT, 
			       delayed_prune_job, cbk);
}


/* TODO: when parsing, check if we go beyong message size */
int
receive_pim6_join_prune(src, pim_message, datalen)
	struct sockaddr_in6 *src;
	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 num_groups;
	u_int16 holdtime;
	u_int16 num_j_srcs, num_p_srcs;
	struct sockaddr_in6 source, group, target;
	struct in6_addr s_mask, g_mask;
	u_int8 s_flags;
	u_int8 reserved;
	mrtentry_t *mrtentry_ptr;
	pim_nbr_entry_t *upstream_router;
	if_set new_pruned_oifs;
	int state_change;

	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];
	if (uvifs[mifi].uv_flags & (VIFF_DOWN | VIFF_DISABLED | VIFF_NONBRS))
		return(FALSE);    /* Shoudn't come on this interface */
	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);

	IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
		log(LOG_DEBUG, 0,
		    "PIM Join/Prune received from %s : target %s, holdtime %d",
		    inet6_fmt(&src->sin6_addr),
		    inet6_fmt(&target.sin6_addr),
		    holdtime);
    
	if (!inet6_localif_address(&target, v) &&
	    !IN6_IS_ADDR_UNSPECIFIED(&uni_target_addr.unicast_addr)) {
		/* if I am not the target of the join or prune message */
		/*
		 * Join Suppression: when receiving a join not addressed to me,
		 * if I am delaying a join for this (S,G) then cancel the delayed 
		 * join.
		 * Prune Soliticiting Joins: when receiving a prune not
		 * addressed to me on a LAN, schedule delayed join if I have
		 * downstream receivers.
		 */
		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 */
			}

			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);

				/* (S,G) Join suppresion */
				mrtentry_ptr = find_route(&source, &group,
							   MRTF_SG, DONT_CREATE);
				if(mrtentry_ptr == (mrtentry_t *)NULL)
					continue;

				IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
					log(LOG_DEBUG, 0,
					    "\tJOIN src %s, group %s - canceling "
					    "delayed join",
					    inet6_fmt(&source.sin6_addr),
					    inet6_fmt(&group.sin6_addr));

				/* Cancel the delayed join */
				if(mrtentry_ptr->join_delay_timerid) {
					timer_clearTimer(mrtentry_ptr->join_delay_timerid);
					mrtentry_ptr->join_delay_timerid = 0;
				}
			}

			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);
				/* sanity checks */
				if (!inet6_valid_host(&source))
					continue;
				if (encod_src.masklen >
				    (sizeof(struct in6_addr) << 3))
					continue;

				s_flags = encod_src.flags;

				/* if P2P link (not addressed to me) ignore 
				 */
				if(uvifs[mifi].uv_flags & VIFF_POINT_TO_POINT) 
					continue;

				/*
				 * if non-null oiflist then schedule delayed join.
				 */
				mrtentry_ptr = find_route(&source, &group,
							  MRTF_SG, DONT_CREATE);
				if(mrtentry_ptr == (mrtentry_t *)NULL) 
					continue;

				if(!(IF_ISEMPTY(&mrtentry_ptr->oifs))) {
					IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
						log(LOG_DEBUG, 0,
						    "\tPRUNE src %s, group %s "
						    "- scheduling delayed join",
						    inet6_fmt(&source.sin6_addr),
						    inet6_fmt(&group.sin6_addr));
		    
					schedule_delayed_join(mrtentry_ptr,
							      &target);
				}
			}

		} /* while groups */

		return(TRUE);
	} /* if not unicast target */

	/* I am the target of this join/prune:
	 * For joins, cancel delayed prunes that I have scheduled.
	 * For prunes, echo the prune and schedule delayed prunes on LAN or 
	 * prune immediately on point-to-point links.
	 */
	else {
		while (num_groups--) {
			GET_EGADDR6(&encod_group, data_ptr);
			GET_HOSTSHORT(num_j_srcs, data_ptr);
			GET_HOSTSHORT(num_p_srcs, data_ptr);
			IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
				log(LOG_DEBUG, 0,
				    "PIM Join/Prune received: grp: %s plen: %d, "
				    "%d jsrc, %d psrc",
				    inet6_fmt(&encod_group.mcast_addr),
				    encod_group.masklen, num_j_srcs, num_p_srcs);
			if (encod_group.masklen > (sizeof(struct in6_addr) << 3))
				continue; /* Ignore this group */
			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 */
			}

			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);
	    
				mrtentry_ptr = find_route(&source, &group,
							  MRTF_SG, DONT_CREATE);
				if(mrtentry_ptr == (mrtentry_t *)NULL) 
					continue;

				IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
					log(LOG_DEBUG, 0,
					    "\tJOIN src %s, group %s - canceling "
					    "delayed prune",
					    inet6_fmt(&source.sin6_addr),
					    inet6_fmt(&group.sin6_addr));

				/* Cancel the delayed prune */
				if(mrtentry_ptr->prune_delay_timerids[mifi]) {
					timer_clearTimer(mrtentry_ptr->prune_delay_timerids[mifi]);
					mrtentry_ptr->prune_delay_timerids[mifi] = 0;
				}	    
			}

			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;


				mrtentry_ptr = find_route(&source, &group,
							  MRTF_SG, DONT_CREATE);
				if(mrtentry_ptr == (mrtentry_t *)NULL) 
					continue;

				/* if P2P link (addressed to me) prune immediately
				 */
				if(uvifs[mifi].uv_flags & VIFF_POINT_TO_POINT) {
					if(IF_ISSET(mifi, &mrtentry_ptr->oifs)) {

						IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
							log(LOG_DEBUG, 0,
							    "\tPRUNE(P2P) src %s,"
							    "group %s - pruning "
							    "mif",
							    inet6_fmt(&source.sin6_addr),
							    inet6_fmt(&group.sin6_addr));
		
						IF_DEBUG(DEBUG_MRT)
							log(LOG_DEBUG, 0, "Deleting pruned mif %d for src %s, grp %s",
							    mifi, 
							    inet6_fmt(&source.sin6_addr),
							    inet6_fmt(&group.sin6_addr));

						IF_COPY(&mrtentry_ptr->pruned_oifs, &new_pruned_oifs);
						IF_SET(mifi, &new_pruned_oifs);
						SET_TIMER(mrtentry_ptr->prune_timers[mifi], holdtime);

						state_change = 
							change_interfaces(mrtentry_ptr,
									  mrtentry_ptr->incoming,
									  &new_pruned_oifs,
									  &mrtentry_ptr->leaves,
									  &mrtentry_ptr->asserted_oifs);
			
						/* Handle transition to negative cache */
						if(state_change == -1)
							trigger_prune_alert(mrtentry_ptr);
					} /* if is pruned */
				} /* if p2p */

				/* if LAN link, echo the prune and schedule delayed
				 * oif deletion
				 */
				else {
					IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
						log(LOG_DEBUG, 0,
						    "\tPRUNE(LAN) src %s, group "
						    "%s - scheduling delayed prune",
						    inet6_fmt(&source.sin6_addr),
						    inet6_fmt(&group.sin6_addr));

					send_pim6_jp(mrtentry_ptr,
						     PIM_ACTION_PRUNE, mifi,
						     &target, holdtime, 1);
					schedule_delayed_prune(mrtentry_ptr,
							       mifi, holdtime);
				}
			}
		} /* while groups */
	} /* else I am unicast target */

	return(TRUE);
}


int
send_pim6_jp(mrtentry_ptr, action, mifi, target_addr, holdtime, echo)
	mrtentry_t *mrtentry_ptr;
	int action;           /* PIM_ACTION_JOIN or PIM_ACTION_PRUNE */
	mifi_t mifi;          /* vif to send join/prune on */
	struct sockaddr_in6 *target_addr;  /* encoded unicast target neighbor */
	u_int16 holdtime;     /* holdtime */
	int echo;
{
	u_int8 *data_ptr, *data_start_ptr;
	
	data_ptr = (u_int8 *)(pim6_send_buf + sizeof(struct pim));
	data_start_ptr = data_ptr;

	if(echo == 0 && mrtentry_ptr->upstream == (pim_nbr_entry_t *)NULL) {
		/* No upstream neighbor - don't send */
		return(FALSE);
	}
    
	IF_DEBUG(DEBUG_PIM_JOIN_PRUNE)
		log(LOG_DEBUG, 0,
		    "Sending %s:  vif %s, src %s, group %s, "
		    "target %s, holdtime %d",
		    action==PIM_ACTION_JOIN ? "JOIN" : "PRUNE",
		    inet6_fmt(&uvifs[mifi].uv_linklocal->pa_addr.sin6_addr),
		    inet6_fmt(&mrtentry_ptr->source->address.sin6_addr),
		    inet6_fmt(&mrtentry_ptr->group->group.sin6_addr),
		    inet6_fmt(&target_addr->sin6_addr), holdtime);

	PUT_EUADDR6(target_addr->sin6_addr, data_ptr);   /* encoded unicast target addr */
	PUT_BYTE(0, data_ptr);		     /* Reserved */
	*data_ptr++ = (u_int8)1;             /* number of groups */
	PUT_HOSTSHORT(holdtime, data_ptr);   /* holdtime */

	/* data_ptr points at the first, and only encoded mcast group */
	PUT_EGADDR6(mrtentry_ptr->group->group.sin6_addr,
		    SINGLE_GRP_MSK6LEN, 0, data_ptr);

	/* set the number of join and prune sources */
	if(action == PIM_ACTION_JOIN) {
		PUT_HOSTSHORT(1, data_ptr);
		PUT_HOSTSHORT(0, data_ptr);
	} else if(action == PIM_ACTION_PRUNE) {
		PUT_HOSTSHORT(0, data_ptr);
		PUT_HOSTSHORT(1, data_ptr);
	}	
    
	PUT_ESADDR6(mrtentry_ptr->source->address.sin6_addr, SINGLE_SRC_MSK6LEN,
		    0, data_ptr);

	/* Cancel active graft */
	if (echo == 0)
		delete_pim6_graft_entry(mrtentry_ptr);
    
	send_pim6(pim6_send_buf, &uvifs[mifi].uv_linklocal->pa_addr,
		  &allpim6routers_group, PIM_JOIN_PRUNE,
		  data_ptr - data_start_ptr);
    
	return(TRUE);
}


/************************************************************************
 *                        PIM_ASSERT
 ************************************************************************/

/* Notes on assert prefs/metrics
 *   - For downstream routers, compare pref/metric previously received from
 *     winner against those in message.
 *     ==> store assert winner's pref/metric in mrtentry
 *   - For upstream router compare my actualy pref/metric for the source
 *     against those received in message.
 *     ==> store my actual pref/metric in srcentry
 */

int
receive_pim6_assert(src, pim_message, datalen)
	struct sockaddr_in6 *src;
	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;
	u_int8 *data_ptr;
	struct uvif *v;
	u_int32 assert_preference;
	u_int32 assert_metric;
	u_int32 local_metric;
	u_int32 local_preference;
	u_int8  local_wins;
	if_set new_pruned_oifs, new_leaves;
	int state_change;
    
	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];
	if (uvifs[mifi].uv_flags & (VIFF_DOWN | VIFF_DISABLED | VIFF_NONBRS))
		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);

	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);
 
	IF_DEBUG(DEBUG_PIM_ASSERT)
		log(LOG_DEBUG, 0,
		    "PIM Assert received from %s: src %s, grp %s, "
		    "pref %d, metric %d",
		    inet6_fmt(&src->sin6_addr),
		    inet6_fmt(&source.sin6_addr),
		    inet6_fmt(&group.sin6_addr),
		    assert_preference, assert_metric);
 
	if ((mrtentry_ptr = find_route(&source, &group, MRTF_SG, CREATE))
	    == NULL) {
		IF_DEBUG(DEBUG_PIM_ASSERT)
			log(LOG_INFO, 0,
			    "\tFailed to create a mrtentry src:%s grp:%s",
			    inet6_fmt(&source.sin6_addr),
			    inet6_fmt(&group.sin6_addr));
		return(FALSE);
	}
	if(mrtentry_ptr->flags & MRTF_NEW) {
		/* For some reason, it's possible for asserts to be processed
		 * before the data alerts a cache miss.  Therefore, when an
		 * assert is received, create (S,G) state and continue, since
		 * we know by the assert that there are upstream forwarders. 
		 */
		IF_DEBUG(DEBUG_PIM_ASSERT)
			log(LOG_DEBUG, 0, "\tNo MRT entry - creating...");

		mrtentry_ptr->flags &= ~MRTF_NEW;

		/* Set oifs */	
		set_leaves(mrtentry_ptr);
		calc_oifs(mrtentry_ptr, &(mrtentry_ptr->oifs));
	
		/* Add it to the kernel */
		k_chg_mfc(mld6_socket, &source, &group, mrtentry_ptr->incoming,
			  &mrtentry_ptr->oifs);

#ifdef RSRR
		rsrr_cache_send(mrtentry_ptr, RSRR_NOTIFICATION_OK);
#endif /* RSRR */

		/* No need to call change_interfaces, but check for NULL oiflist */
		if(IF_ISEMPTY(&mrtentry_ptr->oifs))
			trigger_prune_alert(mrtentry_ptr);	
	}

	/* If arrived on iif, I'm downstream of the asserted LAN.
	 * If arrived on oif, I'm upstream of the asserted LAN.
	 */
	if (mifi == mrtentry_ptr->incoming) {
		/* assert arrived on iif ==> I'm a downstream router */

		/* Determine local (really that of upstream nbr!) pref/metric */
		local_metric = mrtentry_ptr->metric;
		local_preference = mrtentry_ptr->preference;
 
		if(mrtentry_ptr->upstream &&
		   inet6_equal(&mrtentry_ptr->upstream->address, src) &&
		   assert_preference == local_preference &&
		   assert_metric == local_metric)

			/* if assert from previous winner w/ same pref/metric, 
			 * then assert sender wins again */
			local_wins = FALSE;

		else
			/* assert from someone else or something changed */
			local_wins = compare_metrics(local_preference,
						     local_metric,
						     &mrtentry_ptr->upstream->address,
						     assert_preference,
						     assert_metric, src);

		/*
		 * This is between the assert sender and previous winner or rpf 
		 * (who is the "local" in this case).
		 */
		if(local_wins == TRUE) {
			/* the assert-sender loses, so discard the assert */
			IF_DEBUG(DEBUG_PIM_ASSERT)
				log(LOG_DEBUG, 0,
				    "\tAssert sender %s loses",
				    inet6_fmt(&src->sin6_addr));
			return(TRUE);
		}

		/* The assert sender wins: upstream must be changed to the winner */
		IF_DEBUG(DEBUG_PIM_ASSERT)
			log(LOG_DEBUG, 0, "\tAssert sender %s wins",
			    inet6_fmt(&src->sin6_addr));
		if(inet6_equal(&mrtentry_ptr->upstream->address, src)) {
			IF_DEBUG(DEBUG_PIM_ASSERT)
				log(LOG_DEBUG, 0,
				    "\tChanging upstream nbr to %s",
				    inet6_fmt(&src->sin6_addr));
			mrtentry_ptr->preference = assert_preference;
			mrtentry_ptr->metric = assert_metric;
			mrtentry_ptr->upstream = find_pim6_nbr(src);
		}
		SET_TIMER(mrtentry_ptr->assert_timer, PIM_ASSERT_TIMEOUT);
		mrtentry_ptr->flags |= MRTF_ASSERTED;

		/* Send a join for the S,G if oiflist is non-empty */
		if(!(IF_ISEMPTY(&mrtentry_ptr->oifs))) 
			send_pim6_jp(mrtentry_ptr, PIM_ACTION_JOIN,
				     mrtentry_ptr->incoming, src, 0, 0);

	} /* if assert on iif */

	/* If the assert arrived on an oif: */
	else {
		if(!(IF_ISSET(mifi, &mrtentry_ptr->oifs))) 
			return(FALSE);
		/* assert arrived on oif ==> I'm a upstream router */

		/* Determine local pref/metric */
		local_metric = mrtentry_ptr->source->metric;
		local_preference = mrtentry_ptr->source->preference;

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

		if(local_wins == FALSE) {

			/* Assert sender wins - prune the interface */

			IF_DEBUG(DEBUG_PIM_ASSERT)
				log(LOG_DEBUG, 0,
				    "\tAssert sender %s wins - pruning...",
				    inet6_fmt(&src->sin6_addr));

			IF_COPY(&mrtentry_ptr->pruned_oifs, &new_pruned_oifs);
			IF_SET(mifi, &new_pruned_oifs);
			IF_SET(mifi, &mrtentry_ptr->asserted_oifs);
			SET_TIMER(mrtentry_ptr->prune_timers[mifi], 
				  PIM_JOIN_PRUNE_HOLDTIME);

			if (IF_ISSET(mifi, &mrtentry_ptr->leaves)) {
				IF_COPY(&mrtentry_ptr->leaves, &new_leaves);
				IF_CLR(mifi, &new_leaves);
				state_change =
					change_interfaces(mrtentry_ptr,
							  mrtentry_ptr->incoming,
							  &new_pruned_oifs,
							  &mrtentry_ptr->leaves,
							  &new_leaves);
			}
			else {
				state_change =
					change_interfaces(mrtentry_ptr,
							  mrtentry_ptr->incoming,
							  &new_pruned_oifs,
							  &mrtentry_ptr->leaves,
							  &mrtentry_ptr->asserted_oifs);
			}

			/* Handle transition to negative cache */
			if(state_change == -1)
				trigger_prune_alert(mrtentry_ptr);

		} /* assert sender wins */

		else {

			/* Local wins (assert sender loses):
			 * send assert and schedule prune
			 */

			IF_DEBUG(DEBUG_PIM_ASSERT)
				log(LOG_DEBUG, 0,
				    "\tAssert sender %s loses - "
				    "sending assert and scheuling prune", 
				    inet6_fmt(&src->sin6_addr));

			if(!(IF_ISSET(mifi, &mrtentry_ptr->leaves))) {
				/*
				 * No directly connected receivers - delay prune
				 */
				send_pim6_jp(mrtentry_ptr, PIM_ACTION_PRUNE,
					     mifi, &v->uv_linklocal->pa_addr,
					     PIM_JOIN_PRUNE_HOLDTIME, 0);
				schedule_delayed_prune(mrtentry_ptr, mifi, 
						       PIM_JOIN_PRUNE_HOLDTIME);
			}
			send_pim6_assert(&source, &group, mifi, mrtentry_ptr);
		}

	} /* if assert on oif */
	
	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;

	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);

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

	PUT_HOSTLONG(local_preference, data_ptr);
	PUT_HOSTLONG(local_metric, data_ptr);

	IF_DEBUG(DEBUG_PIM_ASSERT)
		log(LOG_DEBUG, 0,
		    "PIM Assert sending: src %s, grp %s, "
		    "pref %d, metric %d",
		    inet6_fmt(&source->sin6_addr),
		    inet6_fmt(&group->sin6_addr),
		    local_metric, local_preference);

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

	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
	 * treatment 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_GRAFT
 ************************************************************************/


u_long              graft_retrans_timer;   /* Graft retransmission timer */
pim_graft_entry_t   *graft_list;           /* Active grafting entries */


void
delete_pim6_graft_entry(mrtentry_ptr)
	mrtentry_t *mrtentry_ptr;
{
	pim_graft_entry_t *graft_entry;

	if(mrtentry_ptr->graft == (pim_graft_entry_t *)NULL)
		return;
	graft_entry = mrtentry_ptr->graft;
    
	if(graft_entry->prev)
		graft_entry->prev->next = graft_entry->next;
	else 
		graft_list = graft_entry->next;
	if(graft_entry->next)
		graft_entry->next->prev = graft_entry->prev;
	mrtentry_ptr->graft = (pim_graft_entry_t *)NULL;
	free(graft_entry);

	/* Stop the timer if there are no more entries */
	if(!graft_list) {
		timer_clearTimer(graft_retrans_timer);
		graft_retrans_timer = 0;
	}
}


static int
retransmit_pim6_graft(mrtentry_ptr)
	mrtentry_t *mrtentry_ptr;
{
	u_int8 *data_ptr, *data_start_ptr;
	
	data_ptr = (u_int8 *)(pim6_send_buf + sizeof(struct pim));
	data_start_ptr = data_ptr;

	if (mrtentry_ptr->upstream == (pim_nbr_entry_t *)NULL) {
		/* No upstream neighbor - don't send */
		return(FALSE);
	}

	IF_DEBUG(DEBUG_PIM_GRAFT)
		log(LOG_DEBUG, 0,
		    "Sending GRAFT:  vif %s, src %s, grp %s, dst %s",
		    inet6_fmt(&uvifs[mrtentry_ptr->incoming].uv_linklocal->pa_addr.sin6_addr),
		    inet6_fmt(&mrtentry_ptr->source->address.sin6_addr),
		    inet6_fmt(&mrtentry_ptr->group->group.sin6_addr),
		    inet6_fmt(&mrtentry_ptr->upstream->address.sin6_addr));


	/* unicast target */
	PUT_EUADDR6(mrtentry_ptr->upstream->address.sin6_addr, data_ptr);
	PUT_BYTE(0, data_ptr);		 /* Reserved */
	*data_ptr++ = (u_int8)1;             /* number of groups */
	PUT_HOSTSHORT(0, data_ptr);          /* no holdtime */

	/* data_ptr points at the first, and only encoded mcast group */
	PUT_EGADDR6(mrtentry_ptr->group->group.sin6_addr,
		    SINGLE_GRP_MSK6LEN, 0, data_ptr);

	/* set the number of join(graft) and prune sources */
	PUT_HOSTSHORT(1, data_ptr);
	PUT_HOSTSHORT(0, data_ptr);

	PUT_ESADDR6(mrtentry_ptr->source->address.sin6_addr, SINGLE_SRC_MSK6LEN,
		    0, data_ptr);

	send_pim6(pim6_send_buf,
		  &uvifs[mrtentry_ptr->incoming].uv_linklocal->pa_addr,
		  &mrtentry_ptr->upstream->address,
		  PIM_GRAFT, data_ptr - data_start_ptr);

	return(TRUE);
}


static void
retransmit_all_pim6_grafts(arg)
	void *arg; /* UNUSED */
{
	pim_graft_entry_t *graft_ptr;

	IF_DEBUG(DEBUG_PIM_GRAFT)
		log(LOG_DEBUG, 0, "Retransmitting all pending PIM-Grafts");
  

	for(graft_ptr = graft_list; 
	    graft_ptr != NULL; 
	    graft_ptr = graft_ptr->next) {

		IF_DEBUG(DEBUG_PIM_GRAFT)
			log(LOG_DEBUG, 0, "\tGRAFT src %s, grp %s",
			    inet6_fmt(&graft_ptr->mrtlink->source->address.sin6_addr),
			    inet6_fmt(&graft_ptr->mrtlink->group->group.sin6_addr));

		retransmit_pim6_graft(graft_ptr->mrtlink);
	}

	if (graft_list)
		timer_setTimer(PIM_GRAFT_RETRANS_PERIOD,
			       retransmit_all_pim6_grafts, (void *)NULL);
}


int
receive_pim6_graft(src, pim_message, datalen, pimtype)
	struct sockaddr_in6 *src;
	register char *pim_message;
	int datalen;
	int pimtype;
{
	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 num_groups;
	u_int16 holdtime;
	u_int16 num_j_srcs;
	u_int16 num_p_srcs;
	struct sockaddr_in6 source, group;
	struct in6_addr s_mask, g_mask;
	u_int8 s_flags;
	u_int8 reserved;
	mrtentry_t *mrtentry_ptr;
	int state_change;

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

	v = &uvifs[mifi];
	if (uvifs[mifi].uv_flags & (VIFF_DOWN | VIFF_DISABLED | VIFF_NONBRS))
		return(FALSE);    /* Shoudn't come on this interface */
	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);

	IF_DEBUG(DEBUG_PIM_GRAFT)
		log(LOG_DEBUG, 0,
		    "PIM %s received from %s on mif %d, grps: %d",
		    pimtype == PIM_GRAFT ? "GRAFT" : "GRAFT-ACK",
		    inet6_fmt(&src->sin6_addr), mifi, num_groups);
    
	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_DEBUG(DEBUG_PIM_GRAFT)
			log(LOG_DEBUG, 0,
			    "  PIM graft: grp: %s, plen: %d, %d jsrcs, %d psrcs",
			    inet6_fmt(&encod_group.mcast_addr),
			    encod_group.masklen, num_j_srcs, num_p_srcs);
		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 */
		}
	
		while (num_j_srcs--) {
			GET_ESADDR6(&encod_src, data_ptr);
			if (encod_src.masklen > (sizeof(struct in6_addr) << 3))
				continue;
			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, &group, MRTF_SG,
						  DONT_CREATE);
			if(mrtentry_ptr == (mrtentry_t *)NULL) 
				continue;

			if(pimtype == PIM_GRAFT) {
				/* Graft */
				IF_DEBUG(DEBUG_PIM_GRAFT)
					log(LOG_DEBUG, 0,
					    "\tGRAFT src %s, group %s - "
					    "forward data on mif %d",
					    inet6_fmt(&source.sin6_addr),
					    inet6_fmt(&group.sin6_addr),
					    mifi);

				/* Cancel any delayed prune */
				if(mrtentry_ptr->prune_delay_timerids[mifi]) {
					timer_clearTimer(mrtentry_ptr->prune_delay_timerids[mifi]);
					mrtentry_ptr->prune_delay_timerids[mifi] = 0;
				}	    
		
				/* Add to oiflist (unprune) */
				if (IF_ISSET(mifi, &mrtentry_ptr->pruned_oifs)) {
					IF_CLR(mifi, &mrtentry_ptr->pruned_oifs);
					IF_CLR(mifi, &mrtentry_ptr->asserted_oifs);
					SET_TIMER(mrtentry_ptr->prune_timers[mifi], 0);
					state_change = 
						change_interfaces(mrtentry_ptr,
								  mrtentry_ptr->incoming,
								  &mrtentry_ptr->pruned_oifs,
								  &mrtentry_ptr->leaves,
								  &mrtentry_ptr->asserted_oifs);
					if(state_change == 1)
						trigger_join_alert(mrtentry_ptr);
				}
			}  /* Graft */
			else {
				/* Graft-Ack */
				IF_DEBUG(DEBUG_PIM_GRAFT)
					log(LOG_DEBUG, 0,
					    "\tGRAFT-ACK src %s, group %s - "
					    "forward data on mif %d",
					    inet6_fmt(&source.sin6_addr),
					    inet6_fmt(&group.sin6_addr),
					    mifi);
				if(mrtentry_ptr->graft)
					delete_pim6_graft_entry(mrtentry_ptr);
			}
		}
		/* Ignore anything in the prune portion of the message! */
	}

	/* Respond to graft with a graft-ack */
	if(pimtype == PIM_GRAFT) {
		IF_DEBUG(DEBUG_PIM_GRAFT)
			log(LOG_DEBUG, 0, "Sending GRAFT-ACK: mif %s, dst %s",
			    inet6_fmt(&uvifs[mifi].uv_linklocal->pa_addr.sin6_addr),
			    inet6_fmt(&src->sin6_addr));
		bcopy(pim_message, pim6_send_buf, datalen);
		send_pim6(pim6_send_buf, &uvifs[mifi].uv_linklocal->pa_addr,
			  src, PIM_GRAFT_ACK, datalen - sizeof(struct pim));
	}

	return(TRUE);
}

int 
send_pim6_graft(mrtentry_ptr)
	mrtentry_t *mrtentry_ptr;
{
	pim_graft_entry_t *new_graft;
	int was_sent = 0;

	if(mrtentry_ptr->graft != (pim_graft_entry_t *)NULL)
		/* Already sending grafts */
		return(FALSE);

	/* Send the first graft */
	was_sent = retransmit_pim6_graft(mrtentry_ptr);
	if(!was_sent)
		return(FALSE);

	/* Set up retransmission */
	new_graft = (pim_graft_entry_t *)malloc(sizeof(pim_graft_entry_t));
	if (new_graft == (pim_graft_entry_t *)NULL) {
		log(LOG_WARNING, 0, 
		    "Memory allocation error for graft entry src %s, grp %s",
		    inet6_fmt(&mrtentry_ptr->source->address.sin6_addr),
		    inet6_fmt(&mrtentry_ptr->group->group.sin6_addr));
		return(FALSE);
	}
	new_graft->next = graft_list;
	new_graft->prev = (pim_graft_entry_t *)NULL;
	new_graft->mrtlink = mrtentry_ptr;
	if(graft_list)
		graft_list->prev = new_graft;
	graft_list = new_graft;
	mrtentry_ptr->graft = new_graft;

	/* Set up timer if not running */
	if(!graft_retrans_timer) 
		graft_retrans_timer = timer_setTimer(PIM_GRAFT_RETRANS_PERIOD,
						     retransmit_all_pim6_grafts, 
						     (void *)NULL);

	return(TRUE);
}
OpenPOWER on IntegriCloud