summaryrefslogtreecommitdiffstats
path: root/pimd/pim_rp.c
blob: d8d25712a30ffefb44b6392a93d6ef95e06d6732 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * PIM for Quagga
 * Copyright (C) 2015 Cumulus Networks, Inc.
 * Donald Sharp
 */
#include <zebra.h>

#include "lib/json.h"
#include "log.h"
#include "network.h"
#include "if.h"
#include "linklist.h"
#include "prefix.h"
#include "memory.h"
#include "vty.h"
#include "vrf.h"
#include "plist.h"
#include "nexthop.h"
#include "table.h"
#include "lib_errors.h"

#include "pimd.h"
#include "pim_instance.h"
#include "pim_vty.h"
#include "pim_str.h"
#include "pim_iface.h"
#include "pim_rp.h"
#include "pim_rpf.h"
#include "pim_sock.h"
#include "pim_memory.h"
#include "pim_neighbor.h"
#include "pim_msdp.h"
#include "pim_nht.h"
#include "pim_mroute.h"
#include "pim_oil.h"
#include "pim_zebra.h"
#include "pim_bsm.h"
#include "pim_util.h"
#include "pim_ssm.h"
#include "termtable.h"

/* Cleanup pim->rpf_hash each node data */
void pim_rp_list_hash_clean(void *data)
{
	struct pim_nexthop_cache *pnc = (struct pim_nexthop_cache *)data;

	list_delete(&pnc->rp_list);

	hash_clean_and_free(&pnc->upstream_hash, NULL);
	if (pnc->nexthop)
		nexthops_free(pnc->nexthop);

	XFREE(MTYPE_PIM_NEXTHOP_CACHE, pnc);
}

static void pim_rp_info_free(struct rp_info *rp_info)
{
	XFREE(MTYPE_PIM_FILTER_NAME, rp_info->plist);

	XFREE(MTYPE_PIM_RP, rp_info);
}

int pim_rp_list_cmp(void *v1, void *v2)
{
	struct rp_info *rp1 = (struct rp_info *)v1;
	struct rp_info *rp2 = (struct rp_info *)v2;
	int ret;

	/*
	 * Sort by RP IP address
	 */
	ret = pim_addr_cmp(rp1->rp.rpf_addr, rp2->rp.rpf_addr);
	if (ret)
		return ret;

	/*
	 * Sort by group IP address
	 */
	ret = prefix_cmp(&rp1->group, &rp2->group);
	if (ret)
		return ret;

	return 0;
}

void pim_rp_init(struct pim_instance *pim)
{
	struct rp_info *rp_info;
	struct route_node *rn;

	pim->rp_list = list_new();
	pim->rp_list->del = (void (*)(void *))pim_rp_info_free;
	pim->rp_list->cmp = pim_rp_list_cmp;

	pim->rp_table = route_table_init();

	rp_info = XCALLOC(MTYPE_PIM_RP, sizeof(*rp_info));

	if (!pim_get_all_mcast_group(&rp_info->group)) {
		flog_err(EC_LIB_DEVELOPMENT,
			 "Unable to convert all-multicast prefix");
		list_delete(&pim->rp_list);
		route_table_finish(pim->rp_table);
		XFREE(MTYPE_PIM_RP, rp_info);
		return;
	}
	rp_info->rp.rpf_addr = PIMADDR_ANY;

	listnode_add(pim->rp_list, rp_info);

	rn = route_node_get(pim->rp_table, &rp_info->group);
	rn->info = rp_info;
	if (PIM_DEBUG_PIM_TRACE)
		zlog_debug("Allocated: %p for rp_info: %p(%pFX) Lock: %d", rn,
			   rp_info, &rp_info->group,
			   route_node_get_lock_count(rn));
}

void pim_rp_free(struct pim_instance *pim)
{
	if (pim->rp_table)
		route_table_finish(pim->rp_table);
	pim->rp_table = NULL;

	if (pim->rp_list)
		list_delete(&pim->rp_list);
}

/*
 * Given an RP's prefix-list, return the RP's rp_info for that prefix-list
 */
static struct rp_info *pim_rp_find_prefix_list(struct pim_instance *pim,
					       pim_addr rp, const char *plist)
{
	struct listnode *node;
	struct rp_info *rp_info;

	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if ((!pim_addr_cmp(rp, rp_info->rp.rpf_addr)) &&
		    rp_info->plist && strcmp(rp_info->plist, plist) == 0) {
			return rp_info;
		}
	}

	return NULL;
}

/*
 * Return true if plist is used by any rp_info
 */
static int pim_rp_prefix_list_used(struct pim_instance *pim, const char *plist)
{
	struct listnode *node;
	struct rp_info *rp_info;

	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if (rp_info->plist && strcmp(rp_info->plist, plist) == 0) {
			return 1;
		}
	}

	return 0;
}

/*
 * Given an RP's address, return the RP's rp_info that is an exact match for
 * 'group'
 */
static struct rp_info *pim_rp_find_exact(struct pim_instance *pim, pim_addr rp,
					 const struct prefix *group)
{
	struct listnode *node;
	struct rp_info *rp_info;

	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if ((!pim_addr_cmp(rp, rp_info->rp.rpf_addr)) &&
		    prefix_same(&rp_info->group, group))
			return rp_info;
	}

	return NULL;
}

/*
 * XXX: long-term issue:  we don't actually have a good "ip address-list"
 * implementation.  ("access-list XYZ" is the closest but honestly it's
 * kinda garbage.)
 *
 * So it's using a prefix-list to match an address here, which causes very
 * unexpected results for the user since prefix-lists by default only match
 * when the prefix length is an exact match too.  i.e. you'd have to add the
 * "le 32" and do "ip prefix-list foo permit 10.0.0.0/24 le 32"
 *
 * To avoid this pitfall, this code uses "address_mode = true" for the prefix
 * list match (this is the only user for that.)
 *
 * In the long run, we need to add a "ip address-list", but that's a wholly
 * separate bag of worms, and existing configs using ip prefix-list would
 * drop into the UX pitfall.
 */

#include "lib/plist_int.h"

/*
 * Given a group, return the rp_info for that group
 */
struct rp_info *pim_rp_find_match_group(struct pim_instance *pim,
					       const struct prefix *group)
{
	struct listnode *node;
	struct rp_info *best = NULL;
	struct rp_info *rp_info;
	struct prefix_list *plist;
	const struct prefix *bp;
	const struct prefix_list_entry *entry;
	struct route_node *rn;

	bp = NULL;
	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if (rp_info->plist) {
			plist = prefix_list_lookup(PIM_AFI, rp_info->plist);

			if (prefix_list_apply_ext(plist, &entry, group, true)
			    == PREFIX_DENY || !entry)
				continue;

			if (!best) {
				best = rp_info;
				bp = &entry->prefix;
				continue;
			}

			if (bp && bp->prefixlen < entry->prefix.prefixlen) {
				best = rp_info;
				bp = &entry->prefix;
			}
		}
	}

	rn = route_node_match(pim->rp_table, group);
	if (!rn) {
		flog_err(
			EC_LIB_DEVELOPMENT,
			"%s: BUG We should have found default group information",
			__func__);
		return best;
	}

	rp_info = rn->info;
	if (PIM_DEBUG_PIM_TRACE_DETAIL) {
		if (best)
			zlog_debug(
				"Lookedup(%pFX): prefix_list match %s, rn %p found: %pFX",
				group, best->plist, rn, &rp_info->group);
		else
			zlog_debug("Lookedup(%pFX): rn %p found:%pFX", group,
				   rn, &rp_info->group);
	}

	route_unlock_node(rn);

	/*
	 * rp's with prefix lists have the group as 224.0.0.0/4 which will
	 * match anything.  So if we have a rp_info that should match a prefix
	 * list then if we do match then best should be the answer( even
	 * if it is NULL )
	 */
	if (!rp_info || (rp_info && rp_info->plist))
		return best;

	/*
	 * So we have a non plist rp_info found in the lookup and no plists
	 * at all to be choosen, return it!
	 */
	if (!best)
		return rp_info;

	/*
	 * If we have a matching non prefix list and a matching prefix
	 * list we should return the actual rp_info that has the LPM
	 * If they are equal, use the prefix-list( but let's hope
	 * the end-operator doesn't do this )
	 */
	if (rp_info->group.prefixlen > bp->prefixlen)
		best = rp_info;

	return best;
}

/*
 * When the user makes "ip pim rp" configuration changes or if they change the
 * prefix-list(s) used by these statements we must tickle the upstream state
 * for each group to make them re-lookup who their RP should be.
 *
 * This is a placeholder function for now.
 */
void pim_rp_refresh_group_to_rp_mapping(struct pim_instance *pim)
{
	pim_msdp_i_am_rp_changed(pim);
	pim_upstream_reeval_use_rpt(pim);
}

void pim_rp_prefix_list_update(struct pim_instance *pim,
			       struct prefix_list *plist)
{
	struct listnode *node;
	struct rp_info *rp_info;
	int refresh_needed = 0;

	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if (rp_info->plist
		    && strcmp(rp_info->plist, prefix_list_name(plist)) == 0) {
			refresh_needed = 1;
			break;
		}
	}

	if (refresh_needed)
		pim_rp_refresh_group_to_rp_mapping(pim);
}

static int pim_rp_check_interface_addrs(struct rp_info *rp_info,
					struct pim_interface *pim_ifp)
{
	struct listnode *node;
	struct pim_secondary_addr *sec_addr;
	pim_addr sec_paddr;

	if (!pim_addr_cmp(pim_ifp->primary_address, rp_info->rp.rpf_addr))
		return 1;

	if (!pim_ifp->sec_addr_list) {
		return 0;
	}

	for (ALL_LIST_ELEMENTS_RO(pim_ifp->sec_addr_list, node, sec_addr)) {
		sec_paddr = pim_addr_from_prefix(&sec_addr->addr);
		/* If an RP-address is self, It should be enough to say
		 * I am RP the prefix-length should not matter here */
		if (!pim_addr_cmp(sec_paddr, rp_info->rp.rpf_addr))
			return 1;
	}

	return 0;
}

static void pim_rp_check_interfaces(struct pim_instance *pim,
				    struct rp_info *rp_info)
{
	struct interface *ifp;

	rp_info->i_am_rp = 0;
	FOR_ALL_INTERFACES (pim->vrf, ifp) {
		struct pim_interface *pim_ifp = ifp->info;

		if (!pim_ifp)
			continue;

		if (pim_rp_check_interface_addrs(rp_info, pim_ifp)) {
			rp_info->i_am_rp = 1;
		}
	}
}

void pim_upstream_update(struct pim_instance *pim, struct pim_upstream *up)
{
	struct pim_rpf old_rpf;
	enum pim_rpf_result rpf_result;
	pim_addr old_upstream_addr;
	pim_addr new_upstream_addr;

	old_upstream_addr = up->upstream_addr;
	pim_rp_set_upstream_addr(pim, &new_upstream_addr, up->sg.src,
				 up->sg.grp);

	if (PIM_DEBUG_PIM_TRACE)
		zlog_debug("%s: pim upstream update for old upstream %pPA",
			   __func__, &old_upstream_addr);

	if (!pim_addr_cmp(old_upstream_addr, new_upstream_addr))
		return;

	/* Lets consider a case, where a PIM upstream has a better RP as a
	 * result of a new RP configuration with more precise group range.
	 * This upstream has to be added to the upstream hash of new RP's
	 * NHT(pnc) and has to be removed from old RP's NHT upstream hash
	 */
	if (!pim_addr_is_any(old_upstream_addr)) {
		/* Deregister addr with Zebra NHT */
		if (PIM_DEBUG_PIM_TRACE)
			zlog_debug(
				"%s: Deregister upstream %s addr %pPA with Zebra NHT",
				__func__, up->sg_str, &old_upstream_addr);
		pim_delete_tracked_nexthop(pim, old_upstream_addr, up, NULL);
	}

	/* Update the upstream address */
	up->upstream_addr = new_upstream_addr;

	old_rpf.source_nexthop.interface = up->rpf.source_nexthop.interface;

	rpf_result = pim_rpf_update(pim, up, &old_rpf, __func__);
	if (rpf_result == PIM_RPF_FAILURE)
		pim_mroute_del(up->channel_oil, __func__);

	/* update kernel multicast forwarding cache (MFC) */
	if (up->rpf.source_nexthop.interface && up->channel_oil)
		pim_upstream_mroute_iif_update(up->channel_oil, __func__);

	if (rpf_result == PIM_RPF_CHANGED ||
			(rpf_result == PIM_RPF_FAILURE &&
			 old_rpf.source_nexthop.interface))
		pim_zebra_upstream_rpf_changed(pim, up, &old_rpf);

}

int pim_rp_new(struct pim_instance *pim, pim_addr rp_addr, struct prefix group,
	       const char *plist, enum rp_source rp_src_flag)
{
	int result = 0;
	struct rp_info *rp_info;
	struct rp_info *rp_all;
	struct prefix group_all;
	struct listnode *node, *nnode;
	struct rp_info *tmp_rp_info;
	char buffer[BUFSIZ];
	pim_addr nht_p;
	struct route_node *rn = NULL;
	struct pim_upstream *up;
	bool upstream_updated = false;

	if (pim_addr_is_any(rp_addr))
		return PIM_RP_BAD_ADDRESS;

	rp_info = XCALLOC(MTYPE_PIM_RP, sizeof(*rp_info));

	rp_info->rp.rpf_addr = rp_addr;
	prefix_copy(&rp_info->group, &group);
	rp_info->rp_src = rp_src_flag;

	if (plist) {
		/*
		 * Return if the prefix-list is already configured for this RP
		 */
		if (pim_rp_find_prefix_list(pim, rp_addr, plist)) {
			XFREE(MTYPE_PIM_RP, rp_info);
			return PIM_SUCCESS;
		}

		/*
		 * Barf if the prefix-list is already configured for an RP
		 */
		if (pim_rp_prefix_list_used(pim, plist)) {
			XFREE(MTYPE_PIM_RP, rp_info);
			return PIM_RP_PFXLIST_IN_USE;
		}

		/*
		 * Free any existing rp_info entries for this RP
		 */
		for (ALL_LIST_ELEMENTS(pim->rp_list, node, nnode,
				       tmp_rp_info)) {
			if (!pim_addr_cmp(rp_info->rp.rpf_addr,
					  tmp_rp_info->rp.rpf_addr)) {
				if (tmp_rp_info->plist)
					pim_rp_del_config(pim, rp_addr, NULL,
							  tmp_rp_info->plist);
				else
					pim_rp_del_config(
						pim, rp_addr,
						prefix2str(&tmp_rp_info->group,
							   buffer, BUFSIZ),
						NULL);
			}
		}

		rp_info->plist = XSTRDUP(MTYPE_PIM_FILTER_NAME, plist);
	} else {

		if (!pim_get_all_mcast_group(&group_all)) {
			XFREE(MTYPE_PIM_RP, rp_info);
			return PIM_GROUP_BAD_ADDRESS;
		}
		rp_all = pim_rp_find_match_group(pim, &group_all);

		/*
		 * Barf if group is a non-multicast subnet
		 */
		if (!prefix_match(&rp_all->group, &rp_info->group)) {
			XFREE(MTYPE_PIM_RP, rp_info);
			return PIM_GROUP_BAD_ADDRESS;
		}

		/*
		 * Remove any prefix-list rp_info entries for this RP
		 */
		for (ALL_LIST_ELEMENTS(pim->rp_list, node, nnode,
				       tmp_rp_info)) {
			if (tmp_rp_info->plist &&
			    (!pim_addr_cmp(rp_info->rp.rpf_addr,
					   tmp_rp_info->rp.rpf_addr))) {
				pim_rp_del_config(pim, rp_addr, NULL,
						  tmp_rp_info->plist);
			}
		}

		/*
		 * Take over the 224.0.0.0/4 group if the rp is INADDR_ANY
		 */
		if (prefix_same(&rp_all->group, &rp_info->group) &&
		    pim_rpf_addr_is_inaddr_any(&rp_all->rp)) {
			rp_all->rp.rpf_addr = rp_info->rp.rpf_addr;
			rp_all->rp_src = rp_src_flag;
			XFREE(MTYPE_PIM_RP, rp_info);

			/* Register addr with Zebra NHT */
			nht_p = rp_all->rp.rpf_addr;
			if (PIM_DEBUG_PIM_NHT_RP)
				zlog_debug(
					"%s: NHT Register rp_all addr %pPA grp %pFX ",
					__func__, &nht_p, &rp_all->group);

			frr_each (rb_pim_upstream, &pim->upstream_head, up) {
				/* Find (*, G) upstream whose RP is not
				 * configured yet
				 */
				if (pim_addr_is_any(up->upstream_addr) &&
				    pim_addr_is_any(up->sg.src)) {
					struct prefix grp;
					struct rp_info *trp_info;

					pim_addr_to_prefix(&grp, up->sg.grp);
					trp_info = pim_rp_find_match_group(
						pim, &grp);
					if (trp_info == rp_all) {
						pim_upstream_update(pim, up);
						upstream_updated = true;
					}
				}
			}
			if (upstream_updated)
				pim_zebra_update_all_interfaces(pim);

			pim_rp_check_interfaces(pim, rp_all);
			pim_rp_refresh_group_to_rp_mapping(pim);
			pim_find_or_track_nexthop(pim, nht_p, NULL, rp_all,
						  NULL);

			if (!pim_ecmp_nexthop_lookup(pim,
						     &rp_all->rp.source_nexthop,
						     nht_p, &rp_all->group, 1))
				return PIM_RP_NO_PATH;
			return PIM_SUCCESS;
		}

		/*
		 * Return if the group is already configured for this RP
		 */
		tmp_rp_info = pim_rp_find_exact(pim, rp_addr, &rp_info->group);
		if (tmp_rp_info) {
			if ((tmp_rp_info->rp_src != rp_src_flag)
			    && (rp_src_flag == RP_SRC_STATIC))
				tmp_rp_info->rp_src = rp_src_flag;
			XFREE(MTYPE_PIM_RP, rp_info);
			return result;
		}

		/*
		 * Barf if this group is already covered by some other RP
		 */
		tmp_rp_info = pim_rp_find_match_group(pim, &rp_info->group);

		if (tmp_rp_info) {
			if (tmp_rp_info->plist) {
				XFREE(MTYPE_PIM_RP, rp_info);
				return PIM_GROUP_PFXLIST_OVERLAP;
			} else {
				/*
				 * If the only RP that covers this group is an
				 * RP configured for
				 * 224.0.0.0/4 that is fine, ignore that one.
				 * For all others
				 * though we must return PIM_GROUP_OVERLAP
				 */
				if (prefix_same(&rp_info->group,
						&tmp_rp_info->group)) {
					if ((rp_src_flag == RP_SRC_STATIC)
					    && (tmp_rp_info->rp_src
						== RP_SRC_STATIC)) {
						XFREE(MTYPE_PIM_RP, rp_info);
						return PIM_GROUP_OVERLAP;
					}

					result = pim_rp_change(
						pim, rp_addr,
						tmp_rp_info->group,
						rp_src_flag);
					XFREE(MTYPE_PIM_RP, rp_info);
					return result;
				}
			}
		}
	}

	listnode_add_sort(pim->rp_list, rp_info);

	if (!rp_info->plist) {
		rn = route_node_get(pim->rp_table, &rp_info->group);
		rn->info = rp_info;
	}

	if (PIM_DEBUG_PIM_TRACE)
		zlog_debug("Allocated: %p for rp_info: %p(%pFX) Lock: %d", rn,
			   rp_info, &rp_info->group,
			   rn ? route_node_get_lock_count(rn) : 0);

	frr_each (rb_pim_upstream, &pim->upstream_head, up) {
		if (pim_addr_is_any(up->sg.src)) {
			struct prefix grp;
			struct rp_info *trp_info;

			pim_addr_to_prefix(&grp, up->sg.grp);
			trp_info = pim_rp_find_match_group(pim, &grp);

			if (trp_info == rp_info) {
				pim_upstream_update(pim, up);
				upstream_updated = true;
			}
		}
	}

	if (upstream_updated)
		pim_zebra_update_all_interfaces(pim);

	pim_rp_check_interfaces(pim, rp_info);
	pim_rp_refresh_group_to_rp_mapping(pim);

	/* Register addr with Zebra NHT */
	nht_p = rp_info->rp.rpf_addr;
	if (PIM_DEBUG_PIM_NHT_RP)
		zlog_debug("%s: NHT Register RP addr %pPA grp %pFX with Zebra ",
			   __func__, &nht_p, &rp_info->group);
	pim_find_or_track_nexthop(pim, nht_p, NULL, rp_info, NULL);
	if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop, nht_p,
				     &rp_info->group, 1))
		return PIM_RP_NO_PATH;

	return PIM_SUCCESS;
}

void pim_rp_del_config(struct pim_instance *pim, pim_addr rp_addr,
		       const char *group_range, const char *plist)
{
	struct prefix group;
	int result;

	if (group_range == NULL)
		result = pim_get_all_mcast_group(&group);
	else
		result = str2prefix(group_range, &group);

	if (!result) {
		if (PIM_DEBUG_PIM_TRACE)
			zlog_debug(
				"%s: String to prefix failed for %pPAs group",
				__func__, &rp_addr);
		return;
	}

	pim_rp_del(pim, rp_addr, group, plist, RP_SRC_STATIC);
}

int pim_rp_del(struct pim_instance *pim, pim_addr rp_addr, struct prefix group,
	       const char *plist, enum rp_source rp_src_flag)
{
	struct prefix g_all;
	struct rp_info *rp_info;
	struct rp_info *rp_all;
	pim_addr nht_p;
	struct route_node *rn;
	bool was_plist = false;
	struct rp_info *trp_info;
	struct pim_upstream *up;
	struct bsgrp_node *bsgrp = NULL;
	struct bsm_rpinfo *bsrp = NULL;
	bool upstream_updated = false;

	if (plist)
		rp_info = pim_rp_find_prefix_list(pim, rp_addr, plist);
	else
		rp_info = pim_rp_find_exact(pim, rp_addr, &group);

	if (!rp_info)
		return PIM_RP_NOT_FOUND;

	if (rp_info->plist) {
		XFREE(MTYPE_PIM_FILTER_NAME, rp_info->plist);
		was_plist = true;
	}

	if (PIM_DEBUG_PIM_TRACE)
		zlog_debug("%s: Delete RP %pPA for the group %pFX", __func__,
			   &rp_addr, &group);

	/* While static RP is getting deleted, we need to check if dynamic RP
	 * present for the same group in BSM RP table, then install the dynamic
	 * RP for the group node into the main rp table
	 */
	if (rp_src_flag == RP_SRC_STATIC) {
		bsgrp = pim_bsm_get_bsgrp_node(&pim->global_scope, &group);

		if (bsgrp) {
			bsrp = bsm_rpinfos_first(bsgrp->bsrp_list);
			if (bsrp) {
				if (PIM_DEBUG_PIM_TRACE)
					zlog_debug(
						"%s: BSM RP %pPA found for the group %pFX",
						__func__, &bsrp->rp_address,
						&group);
				return pim_rp_change(pim, bsrp->rp_address,
						     group, RP_SRC_BSR);
			}
		} else {
			if (PIM_DEBUG_PIM_TRACE)
				zlog_debug(
					"%s: BSM RP not found for the group %pFX",
					__func__, &group);
		}
	}

	/* Deregister addr with Zebra NHT */
	nht_p = rp_info->rp.rpf_addr;
	if (PIM_DEBUG_PIM_NHT_RP)
		zlog_debug("%s: Deregister RP addr %pPA with Zebra ", __func__,
			   &nht_p);
	pim_delete_tracked_nexthop(pim, nht_p, NULL, rp_info);

	if (!pim_get_all_mcast_group(&g_all))
		return PIM_RP_BAD_ADDRESS;

	rp_all = pim_rp_find_match_group(pim, &g_all);

	if (rp_all == rp_info) {
		frr_each (rb_pim_upstream, &pim->upstream_head, up) {
			/* Find the upstream (*, G) whose upstream address is
			 * same as the deleted RP
			 */
			pim_addr rpf_addr;

			rpf_addr = rp_info->rp.rpf_addr;
			if (!pim_addr_cmp(up->upstream_addr, rpf_addr) &&
			    pim_addr_is_any(up->sg.src)) {
				struct prefix grp;

				pim_addr_to_prefix(&grp, up->sg.grp);
				trp_info = pim_rp_find_match_group(pim, &grp);
				if (trp_info == rp_all) {
					pim_upstream_rpf_clear(pim, up);
					up->upstream_addr = PIMADDR_ANY;
				}
			}
		}
		rp_all->rp.rpf_addr = PIMADDR_ANY;
		rp_all->i_am_rp = 0;
		return PIM_SUCCESS;
	}

	listnode_delete(pim->rp_list, rp_info);

	if (!was_plist) {
		rn = route_node_get(pim->rp_table, &rp_info->group);
		if (rn) {
			if (rn->info != rp_info)
				flog_err(
					EC_LIB_DEVELOPMENT,
					"Expected rn->info to be equal to rp_info");

			if (PIM_DEBUG_PIM_TRACE)
				zlog_debug(
					"%s:Found for Freeing: %p for rp_info: %p(%pFX) Lock: %d",
					__func__, rn, rp_info, &rp_info->group,
					route_node_get_lock_count(rn));

			rn->info = NULL;
			route_unlock_node(rn);
			route_unlock_node(rn);
		}
	}

	pim_rp_refresh_group_to_rp_mapping(pim);

	frr_each (rb_pim_upstream, &pim->upstream_head, up) {
		/* Find the upstream (*, G) whose upstream address is same as
		 * the deleted RP
		 */
		pim_addr rpf_addr;

		rpf_addr = rp_info->rp.rpf_addr;
		if (!pim_addr_cmp(up->upstream_addr, rpf_addr) &&
		    pim_addr_is_any(up->sg.src)) {
			struct prefix grp;

			pim_addr_to_prefix(&grp, up->sg.grp);
			trp_info = pim_rp_find_match_group(pim, &grp);

			/* RP not found for the group grp */
			if (pim_rpf_addr_is_inaddr_any(&trp_info->rp)) {
				pim_upstream_rpf_clear(pim, up);
				pim_rp_set_upstream_addr(
					pim, &up->upstream_addr, up->sg.src,
					up->sg.grp);
			}

			/* RP found for the group grp */
			else {
				pim_upstream_update(pim, up);
				upstream_updated = true;
			}
		}
	}

	if (upstream_updated)
		pim_zebra_update_all_interfaces(pim);

	XFREE(MTYPE_PIM_RP, rp_info);
	return PIM_SUCCESS;
}

int pim_rp_change(struct pim_instance *pim, pim_addr new_rp_addr,
		  struct prefix group, enum rp_source rp_src_flag)
{
	pim_addr nht_p;
	struct route_node *rn;
	int result = 0;
	struct rp_info *rp_info = NULL;
	struct pim_upstream *up;
	bool upstream_updated = false;
	pim_addr old_rp_addr;

	rn = route_node_lookup(pim->rp_table, &group);
	if (!rn) {
		result = pim_rp_new(pim, new_rp_addr, group, NULL, rp_src_flag);
		return result;
	}

	rp_info = rn->info;

	if (!rp_info) {
		route_unlock_node(rn);
		result = pim_rp_new(pim, new_rp_addr, group, NULL, rp_src_flag);
		return result;
	}

	old_rp_addr = rp_info->rp.rpf_addr;
	if (!pim_addr_cmp(new_rp_addr, old_rp_addr)) {
		if (rp_info->rp_src != rp_src_flag) {
			rp_info->rp_src = rp_src_flag;
			route_unlock_node(rn);
			return PIM_SUCCESS;
		}
	}

	/* Deregister old RP addr with Zebra NHT */

	if (!pim_addr_is_any(old_rp_addr)) {
		nht_p = rp_info->rp.rpf_addr;
		if (PIM_DEBUG_PIM_NHT_RP)
			zlog_debug("%s: Deregister RP addr %pPA with Zebra ",
				   __func__, &nht_p);
		pim_delete_tracked_nexthop(pim, nht_p, NULL, rp_info);
	}

	pim_rp_nexthop_del(rp_info);
	listnode_delete(pim->rp_list, rp_info);
	/* Update the new RP address*/

	rp_info->rp.rpf_addr = new_rp_addr;
	rp_info->rp_src = rp_src_flag;
	rp_info->i_am_rp = 0;

	listnode_add_sort(pim->rp_list, rp_info);

	frr_each (rb_pim_upstream, &pim->upstream_head, up) {
		if (pim_addr_is_any(up->sg.src)) {
			struct prefix grp;
			struct rp_info *trp_info;

			pim_addr_to_prefix(&grp, up->sg.grp);
			trp_info = pim_rp_find_match_group(pim, &grp);

			if (trp_info == rp_info) {
				pim_upstream_update(pim, up);
				upstream_updated = true;
			}
		}
	}

	if (upstream_updated)
		pim_zebra_update_all_interfaces(pim);

	/* Register new RP addr with Zebra NHT */
	nht_p = rp_info->rp.rpf_addr;
	if (PIM_DEBUG_PIM_NHT_RP)
		zlog_debug("%s: NHT Register RP addr %pPA grp %pFX with Zebra ",
			   __func__, &nht_p, &rp_info->group);

	pim_find_or_track_nexthop(pim, nht_p, NULL, rp_info, NULL);
	if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop, nht_p,
				     &rp_info->group, 1)) {
		route_unlock_node(rn);
		return PIM_RP_NO_PATH;
	}

	pim_rp_check_interfaces(pim, rp_info);

	route_unlock_node(rn);

	pim_rp_refresh_group_to_rp_mapping(pim);

	return result;
}

void pim_rp_setup(struct pim_instance *pim)
{
	struct listnode *node;
	struct rp_info *rp_info;
	pim_addr nht_p;

	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if (pim_rpf_addr_is_inaddr_any(&rp_info->rp))
			continue;

		nht_p = rp_info->rp.rpf_addr;

		pim_find_or_track_nexthop(pim, nht_p, NULL, rp_info, NULL);
		if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop,
					     nht_p, &rp_info->group, 1)) {
			if (PIM_DEBUG_PIM_NHT_RP)
				zlog_debug(
					"Unable to lookup nexthop for rp specified");
			pim_rp_nexthop_del(rp_info);
		}
	}
}

/*
 * Checks to see if we should elect ourself the actual RP when new if
 * addresses are added against an interface.
 */
void pim_rp_check_on_if_add(struct pim_interface *pim_ifp)
{
	struct listnode *node;
	struct rp_info *rp_info;
	bool i_am_rp_changed = false;
	struct pim_instance *pim = pim_ifp->pim;

	if (pim->rp_list == NULL)
		return;

	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if (pim_rpf_addr_is_inaddr_any(&rp_info->rp))
			continue;

		/* if i_am_rp is already set nothing to be done (adding new
		 * addresses
		 * is not going to make a difference). */
		if (rp_info->i_am_rp) {
			continue;
		}

		if (pim_rp_check_interface_addrs(rp_info, pim_ifp)) {
			i_am_rp_changed = true;
			rp_info->i_am_rp = 1;
			if (PIM_DEBUG_PIM_NHT_RP)
				zlog_debug("%s: %pPA: i am rp", __func__,
					   &rp_info->rp.rpf_addr);
		}
	}

	if (i_am_rp_changed) {
		pim_msdp_i_am_rp_changed(pim);
		pim_upstream_reeval_use_rpt(pim);
	}
}

/* up-optimized re-evaluation of "i_am_rp". this is used when ifaddresses
 * are removed. Removing numbers is an uncommon event in an active network
 * so I have made no attempt to optimize it. */
void pim_i_am_rp_re_evaluate(struct pim_instance *pim)
{
	struct listnode *node;
	struct rp_info *rp_info;
	bool i_am_rp_changed = false;
	int old_i_am_rp;

	if (pim->rp_list == NULL)
		return;

	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if (pim_rpf_addr_is_inaddr_any(&rp_info->rp))
			continue;

		old_i_am_rp = rp_info->i_am_rp;
		pim_rp_check_interfaces(pim, rp_info);

		if (old_i_am_rp != rp_info->i_am_rp) {
			i_am_rp_changed = true;
			if (PIM_DEBUG_PIM_NHT_RP) {
				if (rp_info->i_am_rp)
					zlog_debug("%s: %pPA: i am rp",
						   __func__,
						   &rp_info->rp.rpf_addr);
				else
					zlog_debug(
						"%s: %pPA: i am no longer rp",
						__func__,
						&rp_info->rp.rpf_addr);
			}
		}
	}

	if (i_am_rp_changed) {
		pim_msdp_i_am_rp_changed(pim);
		pim_upstream_reeval_use_rpt(pim);
	}
}

/*
 * I_am_RP(G) is true if the group-to-RP mapping indicates that
 * this router is the RP for the group.
 *
 * Since we only have static RP, all groups are part of this RP
 */
int pim_rp_i_am_rp(struct pim_instance *pim, pim_addr group)
{
	struct prefix g;
	struct rp_info *rp_info;

	memset(&g, 0, sizeof(g));
	pim_addr_to_prefix(&g, group);
	rp_info = pim_rp_find_match_group(pim, &g);

	if (rp_info)
		return rp_info->i_am_rp;
	return 0;
}

/*
 * RP(G)
 *
 * Return the RP that the Group belongs too.
 */
struct pim_rpf *pim_rp_g(struct pim_instance *pim, pim_addr group)
{
	struct prefix g;
	struct rp_info *rp_info;

	memset(&g, 0, sizeof(g));
	pim_addr_to_prefix(&g, group);

	rp_info = pim_rp_find_match_group(pim, &g);

	if (rp_info) {
		pim_addr nht_p;

		if (pim_addr_is_any(rp_info->rp.rpf_addr)) {
			if (PIM_DEBUG_PIM_NHT_RP)
				zlog_debug(
					"%s: Skipping NHT Register since RP is not configured for the group %pPA",
					__func__, &group);
			return &rp_info->rp;
		}

		/* Register addr with Zebra NHT */
		nht_p = rp_info->rp.rpf_addr;
		if (PIM_DEBUG_PIM_NHT_RP)
			zlog_debug(
				"%s: NHT Register RP addr %pPA grp %pFX with Zebra",
				__func__, &nht_p, &rp_info->group);
		pim_find_or_track_nexthop(pim, nht_p, NULL, rp_info, NULL);
		pim_rpf_set_refresh_time(pim);
		(void)pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop,
					      nht_p, &rp_info->group, 1);
		return (&rp_info->rp);
	}

	// About to Go Down
	return NULL;
}

/*
 * Set the upstream IP address we want to talk to based upon
 * the rp configured and the source address
 *
 * If we have don't have a RP configured and the source address is *
 * then set the upstream addr as INADDR_ANY and return failure.
 *
 */
int pim_rp_set_upstream_addr(struct pim_instance *pim, pim_addr *up,
			     pim_addr source, pim_addr group)
{
	struct rp_info *rp_info;
	struct prefix g;

	memset(&g, 0, sizeof(g));

	pim_addr_to_prefix(&g, group);

	rp_info = pim_rp_find_match_group(pim, &g);

	if (!rp_info || ((pim_rpf_addr_is_inaddr_any(&rp_info->rp)) &&
			 (pim_addr_is_any(source)))) {
		if (PIM_DEBUG_PIM_NHT_RP)
			zlog_debug("%s: Received a (*,G) with no RP configured",
				   __func__);
		*up = PIMADDR_ANY;
		return 0;
	}

	if (pim_addr_is_any(source))
		*up = rp_info->rp.rpf_addr;
	else
		*up = source;

	return 1;
}

int pim_rp_config_write(struct pim_instance *pim, struct vty *vty,
			const char *spaces)
{
	struct listnode *node;
	struct rp_info *rp_info;
	int count = 0;
	pim_addr rp_addr;

	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if (pim_rpf_addr_is_inaddr_any(&rp_info->rp))
			continue;

		if (rp_info->rp_src == RP_SRC_BSR)
			continue;

		rp_addr = rp_info->rp.rpf_addr;
		if (rp_info->plist)
			vty_out(vty,
				"%s" PIM_AF_NAME
				" pim rp %pPA prefix-list %s\n",
				spaces, &rp_addr, rp_info->plist);
		else
			vty_out(vty, "%s" PIM_AF_NAME " pim rp %pPA %pFX\n",
				spaces, &rp_addr, &rp_info->group);
		count++;
	}

	return count;
}

void pim_rp_show_information(struct pim_instance *pim, struct prefix *range,
			     struct vty *vty, json_object *json)
{
	struct rp_info *rp_info;
	struct rp_info *prev_rp_info = NULL;
	struct listnode *node;
	struct ttable *tt = NULL;
	char *table = NULL;
	char source[7];
	char grp[INET6_ADDRSTRLEN];

	json_object *json_rp_rows = NULL;
	json_object *json_row = NULL;

	if (!json) {
		/* Prepare table. */
		tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
		ttable_add_row(
			tt,
			"RP address|group/prefix-list|OIF|I am RP|Source|Group-Type");
		tt->style.cell.rpad = 2;
		tt->style.corner = '+';
		ttable_restyle(tt);
	}

	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if (pim_rpf_addr_is_inaddr_any(&rp_info->rp))
			continue;

#if PIM_IPV == 4
		pim_addr group = rp_info->group.u.prefix4;
#else
		pim_addr group = rp_info->group.u.prefix6;
#endif
		const char *group_type =
			pim_is_grp_ssm(pim, group) ? "SSM" : "ASM";

		if (range && !prefix_match(&rp_info->group, range))
			continue;

		if (rp_info->rp_src == RP_SRC_STATIC)
			strlcpy(source, "Static", sizeof(source));
		else if (rp_info->rp_src == RP_SRC_BSR)
			strlcpy(source, "BSR", sizeof(source));
		else
			strlcpy(source, "None", sizeof(source));
		if (json) {
			/*
			 * If we have moved on to a new RP then add the
			 * entry for the previous RP
			 */
			if (prev_rp_info &&
			    (pim_addr_cmp(prev_rp_info->rp.rpf_addr,
					  rp_info->rp.rpf_addr))) {
				json_object_object_addf(
					json, json_rp_rows, "%pPA",
					&prev_rp_info->rp.rpf_addr);
				json_rp_rows = NULL;
			}

			if (!json_rp_rows)
				json_rp_rows = json_object_new_array();

			json_row = json_object_new_object();
			json_object_string_addf(json_row, "rpAddress", "%pPA",
						&rp_info->rp.rpf_addr);
			if (rp_info->rp.source_nexthop.interface)
				json_object_string_add(
					json_row, "outboundInterface",
					rp_info->rp.source_nexthop
						.interface->name);
			else
				json_object_string_add(json_row,
						       "outboundInterface",
						       "Unknown");
			if (rp_info->i_am_rp)
				json_object_boolean_true_add(json_row, "iAmRP");
			else
				json_object_boolean_false_add(json_row,
							      "iAmRP");

			if (rp_info->plist)
				json_object_string_add(json_row, "prefixList",
						       rp_info->plist);
			else
				json_object_string_addf(json_row, "group",
							"%pFX",
							&rp_info->group);
			json_object_string_add(json_row, "source", source);
			json_object_string_add(json_row, "groupType",
					       group_type);

			json_object_array_add(json_rp_rows, json_row);
		} else {
			prefix2str(&rp_info->group, grp, sizeof(grp));
			ttable_add_row(tt, "%pPA|%s|%s|%s|%s|%s",
					  &rp_info->rp.rpf_addr,
					  rp_info->plist
					  ? rp_info->plist
					  : grp,
					  rp_info->rp.source_nexthop.interface
					  ? rp_info->rp.source_nexthop
						.interface->name
						: "Unknown",
					  rp_info->i_am_rp
					  ? "yes"
					  : "no",
					  source, group_type);
		}
		prev_rp_info = rp_info;
	}

	/* Dump the generated table. */
	if (!json) {
		table = ttable_dump(tt, "\n");
		vty_out(vty, "%s\n", table);
		XFREE(MTYPE_TMP, table);
		ttable_del(tt);
	} else {
		if (prev_rp_info && json_rp_rows)
			json_object_object_addf(json, json_rp_rows, "%pPA",
						&prev_rp_info->rp.rpf_addr);
	}
}

void pim_resolve_rp_nh(struct pim_instance *pim, struct pim_neighbor *nbr)
{
	struct listnode *node = NULL;
	struct rp_info *rp_info = NULL;
	struct nexthop *nh_node = NULL;
	pim_addr nht_p;
	struct pim_nexthop_cache pnc;

	for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
		if (pim_rpf_addr_is_inaddr_any(&rp_info->rp))
			continue;

		nht_p = rp_info->rp.rpf_addr;
		memset(&pnc, 0, sizeof(struct pim_nexthop_cache));
		if (!pim_find_or_track_nexthop(pim, nht_p, NULL, rp_info, &pnc))
			continue;

		for (nh_node = pnc.nexthop; nh_node; nh_node = nh_node->next) {
#if PIM_IPV == 4
			if (!pim_addr_is_any(nh_node->gate.ipv4))
				continue;
#else
			if (!pim_addr_is_any(nh_node->gate.ipv6))
				continue;
#endif

			struct interface *ifp1 = if_lookup_by_index(
				nh_node->ifindex, pim->vrf->vrf_id);

			if (nbr->interface != ifp1)
				continue;

#if PIM_IPV == 4
			nh_node->gate.ipv4 = nbr->source_addr;
#else
			nh_node->gate.ipv6 = nbr->source_addr;
#endif
			if (PIM_DEBUG_PIM_NHT_RP)
				zlog_debug(
					"%s: addr %pPA new nexthop addr %pPAs interface %s",
					__func__, &nht_p, &nbr->source_addr,
					ifp1->name);
		}
	}
}