]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_rp.c
Merge pull request #3863 from patrasar/RP_addition_deletion_changes
[mirror_frr.git] / pimd / pim_rp.c
1 /*
2 * PIM for Quagga
3 * Copyright (C) 2015 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include <zebra.h>
21
22 #include "lib/json.h"
23 #include "log.h"
24 #include "network.h"
25 #include "if.h"
26 #include "linklist.h"
27 #include "prefix.h"
28 #include "memory.h"
29 #include "vty.h"
30 #include "vrf.h"
31 #include "plist.h"
32 #include "nexthop.h"
33 #include "table.h"
34 #include "lib_errors.h"
35
36 #include "pimd.h"
37 #include "pim_vty.h"
38 #include "pim_str.h"
39 #include "pim_iface.h"
40 #include "pim_rp.h"
41 #include "pim_str.h"
42 #include "pim_rpf.h"
43 #include "pim_sock.h"
44 #include "pim_memory.h"
45 #include "pim_iface.h"
46 #include "pim_msdp.h"
47 #include "pim_nht.h"
48 #include "pim_mroute.h"
49 #include "pim_oil.h"
50 #include "pim_zebra.h"
51
52 /* Cleanup pim->rpf_hash each node data */
53 void pim_rp_list_hash_clean(void *data)
54 {
55 struct pim_nexthop_cache *pnc = (struct pim_nexthop_cache *)data;
56
57 list_delete(&pnc->rp_list);
58
59 hash_clean(pnc->upstream_hash, NULL);
60 hash_free(pnc->upstream_hash);
61 pnc->upstream_hash = NULL;
62 if (pnc->nexthop)
63 nexthops_free(pnc->nexthop);
64
65 XFREE(MTYPE_PIM_NEXTHOP_CACHE, pnc);
66 }
67
68 static void pim_rp_info_free(struct rp_info *rp_info)
69 {
70 XFREE(MTYPE_PIM_FILTER_NAME, rp_info->plist);
71
72 XFREE(MTYPE_PIM_RP, rp_info);
73 }
74
75 int pim_rp_list_cmp(void *v1, void *v2)
76 {
77 struct rp_info *rp1 = (struct rp_info *)v1;
78 struct rp_info *rp2 = (struct rp_info *)v2;
79
80 /*
81 * Sort by RP IP address
82 */
83 if (rp1->rp.rpf_addr.u.prefix4.s_addr
84 < rp2->rp.rpf_addr.u.prefix4.s_addr)
85 return -1;
86
87 if (rp1->rp.rpf_addr.u.prefix4.s_addr
88 > rp2->rp.rpf_addr.u.prefix4.s_addr)
89 return 1;
90
91 /*
92 * Sort by group IP address
93 */
94 if (rp1->group.u.prefix4.s_addr < rp2->group.u.prefix4.s_addr)
95 return -1;
96
97 if (rp1->group.u.prefix4.s_addr > rp2->group.u.prefix4.s_addr)
98 return 1;
99
100 return 0;
101 }
102
103 void pim_rp_init(struct pim_instance *pim)
104 {
105 struct rp_info *rp_info;
106 struct route_node *rn;
107
108 pim->rp_list = list_new();
109 pim->rp_list->del = (void (*)(void *))pim_rp_info_free;
110 pim->rp_list->cmp = pim_rp_list_cmp;
111
112 pim->rp_table = route_table_init();
113
114 rp_info = XCALLOC(MTYPE_PIM_RP, sizeof(*rp_info));
115
116 if (!str2prefix("224.0.0.0/4", &rp_info->group)) {
117 flog_err(EC_LIB_DEVELOPMENT,
118 "Unable to convert 224.0.0.0/4 to prefix");
119 list_delete(&pim->rp_list);
120 route_table_finish(pim->rp_table);
121 XFREE(MTYPE_PIM_RP, rp_info);
122 return;
123 }
124 rp_info->group.family = AF_INET;
125 rp_info->rp.rpf_addr.family = AF_INET;
126 rp_info->rp.rpf_addr.prefixlen = IPV4_MAX_PREFIXLEN;
127 rp_info->rp.rpf_addr.u.prefix4.s_addr = INADDR_NONE;
128
129 listnode_add(pim->rp_list, rp_info);
130
131 rn = route_node_get(pim->rp_table, &rp_info->group);
132 rn->info = rp_info;
133 if (PIM_DEBUG_TRACE)
134 zlog_debug(
135 "Allocated: %p for rp_info: %p(224.0.0.0/4) Lock: %d",
136 rn, rp_info, rn->lock);
137 }
138
139 void pim_rp_free(struct pim_instance *pim)
140 {
141 if (pim->rp_list)
142 list_delete(&pim->rp_list);
143 }
144
145 /*
146 * Given an RP's prefix-list, return the RP's rp_info for that prefix-list
147 */
148 static struct rp_info *pim_rp_find_prefix_list(struct pim_instance *pim,
149 struct in_addr rp,
150 const char *plist)
151 {
152 struct listnode *node;
153 struct rp_info *rp_info;
154
155 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
156 if (rp.s_addr == rp_info->rp.rpf_addr.u.prefix4.s_addr
157 && rp_info->plist && strcmp(rp_info->plist, plist) == 0) {
158 return rp_info;
159 }
160 }
161
162 return NULL;
163 }
164
165 /*
166 * Return true if plist is used by any rp_info
167 */
168 static int pim_rp_prefix_list_used(struct pim_instance *pim, const char *plist)
169 {
170 struct listnode *node;
171 struct rp_info *rp_info;
172
173 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
174 if (rp_info->plist && strcmp(rp_info->plist, plist) == 0) {
175 return 1;
176 }
177 }
178
179 return 0;
180 }
181
182 /*
183 * Given an RP's address, return the RP's rp_info that is an exact match for
184 * 'group'
185 */
186 static struct rp_info *pim_rp_find_exact(struct pim_instance *pim,
187 struct in_addr rp,
188 const struct prefix *group)
189 {
190 struct listnode *node;
191 struct rp_info *rp_info;
192
193 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
194 if (rp.s_addr == rp_info->rp.rpf_addr.u.prefix4.s_addr
195 && prefix_same(&rp_info->group, group))
196 return rp_info;
197 }
198
199 return NULL;
200 }
201
202 /*
203 * Given a group, return the rp_info for that group
204 */
205 struct rp_info *pim_rp_find_match_group(struct pim_instance *pim,
206 const struct prefix *group)
207 {
208 struct listnode *node;
209 struct rp_info *best = NULL;
210 struct rp_info *rp_info;
211 struct prefix_list *plist;
212 const struct prefix *p, *bp;
213 struct route_node *rn;
214
215 bp = NULL;
216 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
217 if (rp_info->plist) {
218 plist = prefix_list_lookup(AFI_IP, rp_info->plist);
219
220 if (prefix_list_apply_which_prefix(plist, &p, group)
221 == PREFIX_DENY)
222 continue;
223
224 if (!best) {
225 best = rp_info;
226 bp = p;
227 continue;
228 }
229
230 if (bp && bp->prefixlen < p->prefixlen) {
231 best = rp_info;
232 bp = p;
233 }
234 }
235 }
236
237 rn = route_node_match(pim->rp_table, group);
238 if (!rn) {
239 flog_err(
240 EC_LIB_DEVELOPMENT,
241 "%s: BUG We should have found default group information\n",
242 __PRETTY_FUNCTION__);
243 return best;
244 }
245
246 rp_info = rn->info;
247 if (PIM_DEBUG_TRACE) {
248 char buf[PREFIX_STRLEN];
249
250 route_unlock_node(rn);
251 zlog_debug("Lookedup: %p for rp_info: %p(%s) Lock: %d", rn,
252 rp_info,
253 prefix2str(&rp_info->group, buf, sizeof(buf)),
254 rn->lock);
255 }
256
257 if (!best)
258 return rp_info;
259
260 if (rp_info->group.prefixlen < best->group.prefixlen)
261 best = rp_info;
262
263 return best;
264 }
265
266 /*
267 * When the user makes "ip pim rp" configuration changes or if they change the
268 * prefix-list(s) used by these statements we must tickle the upstream state
269 * for each group to make them re-lookup who their RP should be.
270 *
271 * This is a placeholder function for now.
272 */
273 static void pim_rp_refresh_group_to_rp_mapping(struct pim_instance *pim)
274 {
275 pim_msdp_i_am_rp_changed(pim);
276 }
277
278 void pim_rp_prefix_list_update(struct pim_instance *pim,
279 struct prefix_list *plist)
280 {
281 struct listnode *node;
282 struct rp_info *rp_info;
283 int refresh_needed = 0;
284
285 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
286 if (rp_info->plist
287 && strcmp(rp_info->plist, prefix_list_name(plist)) == 0) {
288 refresh_needed = 1;
289 break;
290 }
291 }
292
293 if (refresh_needed)
294 pim_rp_refresh_group_to_rp_mapping(pim);
295 }
296
297 static int pim_rp_check_interface_addrs(struct rp_info *rp_info,
298 struct pim_interface *pim_ifp)
299 {
300 struct listnode *node;
301 struct pim_secondary_addr *sec_addr;
302
303 if (pim_ifp->primary_address.s_addr
304 == rp_info->rp.rpf_addr.u.prefix4.s_addr)
305 return 1;
306
307 if (!pim_ifp->sec_addr_list) {
308 return 0;
309 }
310
311 for (ALL_LIST_ELEMENTS_RO(pim_ifp->sec_addr_list, node, sec_addr)) {
312 if (prefix_same(&sec_addr->addr, &rp_info->rp.rpf_addr)) {
313 return 1;
314 }
315 }
316
317 return 0;
318 }
319
320 static void pim_rp_check_interfaces(struct pim_instance *pim,
321 struct rp_info *rp_info)
322 {
323 struct interface *ifp;
324
325 rp_info->i_am_rp = 0;
326 FOR_ALL_INTERFACES (pim->vrf, ifp) {
327 struct pim_interface *pim_ifp = ifp->info;
328
329 if (!pim_ifp)
330 continue;
331
332 if (pim_rp_check_interface_addrs(rp_info, pim_ifp)) {
333 rp_info->i_am_rp = 1;
334 }
335 }
336 }
337
338 void pim_upstream_update(struct pim_instance *pim, struct pim_upstream *up)
339 {
340 struct pim_rpf old_rpf;
341 enum pim_rpf_result rpf_result;
342 struct in_addr old_upstream_addr;
343 struct in_addr new_upstream_addr;
344 struct prefix nht_p;
345
346 old_upstream_addr = up->upstream_addr;
347 pim_rp_set_upstream_addr(pim, &new_upstream_addr, up->sg.src,
348 up->sg.grp);
349
350 if (PIM_DEBUG_TRACE)
351 zlog_debug("%s: pim upstream update for old upstream %s",
352 __PRETTY_FUNCTION__,
353 inet_ntoa(old_upstream_addr));
354
355 if (old_upstream_addr.s_addr == new_upstream_addr.s_addr)
356 return;
357
358 /* Lets consider a case, where a PIM upstream has a better RP as a
359 * result of a new RP configuration with more precise group range.
360 * This upstream has to be added to the upstream hash of new RP's
361 * NHT(pnc) and has to be removed from old RP's NHT upstream hash
362 */
363 if (old_upstream_addr.s_addr != INADDR_ANY) {
364 /* Deregister addr with Zebra NHT */
365 nht_p.family = AF_INET;
366 nht_p.prefixlen = IPV4_MAX_BITLEN;
367 nht_p.u.prefix4 = old_upstream_addr;
368 if (PIM_DEBUG_TRACE) {
369 char buf[PREFIX2STR_BUFFER];
370
371 prefix2str(&nht_p, buf, sizeof(buf));
372 zlog_debug("%s: Deregister upstream %s addr %s with Zebra NHT",
373 __PRETTY_FUNCTION__, up->sg_str, buf);
374 }
375 pim_delete_tracked_nexthop(pim, &nht_p, up, NULL);
376 }
377
378 /* Update the upstream address */
379 up->upstream_addr = new_upstream_addr;
380
381 old_rpf.source_nexthop.interface = up->rpf.source_nexthop.interface;
382
383 rpf_result = pim_rpf_update(pim, up, &old_rpf, 1);
384 if (rpf_result == PIM_RPF_FAILURE)
385 pim_mroute_del(up->channel_oil, __PRETTY_FUNCTION__);
386
387 /* update kernel multicast forwarding cache (MFC) */
388 if (up->rpf.source_nexthop.interface && up->channel_oil) {
389 ifindex_t ifindex = up->rpf.source_nexthop.interface->ifindex;
390 int vif_index = pim_if_find_vifindex_by_ifindex(pim, ifindex);
391 /* Pass Current selected NH vif index to mroute download */
392 if (vif_index)
393 pim_scan_individual_oil(up->channel_oil, vif_index);
394 else {
395 if (PIM_DEBUG_PIM_NHT)
396 zlog_debug(
397 "%s: NHT upstream %s channel_oil IIF %s vif_index is not valid",
398 __PRETTY_FUNCTION__, up->sg_str,
399 up->rpf.source_nexthop.interface->name);
400 }
401 }
402
403 if (rpf_result == PIM_RPF_CHANGED)
404 pim_zebra_upstream_rpf_changed(pim, up, &old_rpf);
405
406 pim_zebra_update_all_interfaces(pim);
407 }
408
409 int pim_rp_new(struct pim_instance *pim, const char *rp,
410 const char *group_range, const char *plist)
411 {
412 int result = 0;
413 struct rp_info *rp_info;
414 struct rp_info *rp_all;
415 struct prefix group_all;
416 struct listnode *node, *nnode;
417 struct rp_info *tmp_rp_info;
418 char buffer[BUFSIZ];
419 struct prefix nht_p;
420 struct prefix temp;
421 struct pim_nexthop_cache pnc;
422 struct route_node *rn;
423 struct pim_upstream *up;
424 struct listnode *upnode;
425
426 rp_info = XCALLOC(MTYPE_PIM_RP, sizeof(*rp_info));
427
428 if (group_range == NULL)
429 result = str2prefix("224.0.0.0/4", &rp_info->group);
430 else {
431 result = str2prefix(group_range, &rp_info->group);
432 if (result) {
433 prefix_copy(&temp, &rp_info->group);
434 apply_mask(&temp);
435 if (!prefix_same(&rp_info->group, &temp)) {
436 XFREE(MTYPE_PIM_RP, rp_info);
437 return PIM_GROUP_BAD_ADDR_MASK_COMBO;
438 }
439 }
440 }
441
442 if (!result) {
443 XFREE(MTYPE_PIM_RP, rp_info);
444 return PIM_GROUP_BAD_ADDRESS;
445 }
446
447 rp_info->rp.rpf_addr.family = AF_INET;
448 rp_info->rp.rpf_addr.prefixlen = IPV4_MAX_PREFIXLEN;
449 result = inet_pton(rp_info->rp.rpf_addr.family, rp,
450 &rp_info->rp.rpf_addr.u.prefix4);
451
452 if (result <= 0) {
453 XFREE(MTYPE_PIM_RP, rp_info);
454 return PIM_RP_BAD_ADDRESS;
455 }
456
457 if (plist) {
458 /*
459 * Return if the prefix-list is already configured for this RP
460 */
461 if (pim_rp_find_prefix_list(pim, rp_info->rp.rpf_addr.u.prefix4,
462 plist)) {
463 XFREE(MTYPE_PIM_RP, rp_info);
464 return PIM_SUCCESS;
465 }
466
467 /*
468 * Barf if the prefix-list is already configured for an RP
469 */
470 if (pim_rp_prefix_list_used(pim, plist)) {
471 XFREE(MTYPE_PIM_RP, rp_info);
472 return PIM_RP_PFXLIST_IN_USE;
473 }
474
475 /*
476 * Free any existing rp_info entries for this RP
477 */
478 for (ALL_LIST_ELEMENTS(pim->rp_list, node, nnode,
479 tmp_rp_info)) {
480 if (rp_info->rp.rpf_addr.u.prefix4.s_addr
481 == tmp_rp_info->rp.rpf_addr.u.prefix4.s_addr) {
482 if (tmp_rp_info->plist)
483 pim_rp_del(pim, rp, NULL,
484 tmp_rp_info->plist);
485 else
486 pim_rp_del(
487 pim, rp,
488 prefix2str(&tmp_rp_info->group,
489 buffer, BUFSIZ),
490 NULL);
491 }
492 }
493
494 rp_info->plist = XSTRDUP(MTYPE_PIM_FILTER_NAME, plist);
495 } else {
496
497 if (!str2prefix("224.0.0.0/4", &group_all)) {
498 XFREE(MTYPE_PIM_RP, rp_info);
499 return PIM_GROUP_BAD_ADDRESS;
500 }
501 rp_all = pim_rp_find_match_group(pim, &group_all);
502
503 /*
504 * Barf if group is a non-multicast subnet
505 */
506 if (!prefix_match(&rp_all->group, &rp_info->group)) {
507 XFREE(MTYPE_PIM_RP, rp_info);
508 return PIM_GROUP_BAD_ADDRESS;
509 }
510
511 /*
512 * Remove any prefix-list rp_info entries for this RP
513 */
514 for (ALL_LIST_ELEMENTS(pim->rp_list, node, nnode,
515 tmp_rp_info)) {
516 if (tmp_rp_info->plist
517 && rp_info->rp.rpf_addr.u.prefix4.s_addr
518 == tmp_rp_info->rp.rpf_addr.u.prefix4
519 .s_addr) {
520 pim_rp_del(pim, rp, NULL, tmp_rp_info->plist);
521 }
522 }
523
524 /*
525 * Take over the 224.0.0.0/4 group if the rp is INADDR_NONE
526 */
527 if (prefix_same(&rp_all->group, &rp_info->group)
528 && pim_rpf_addr_is_inaddr_none(&rp_all->rp)) {
529 rp_all->rp.rpf_addr = rp_info->rp.rpf_addr;
530 XFREE(MTYPE_PIM_RP, rp_info);
531
532 /* Register addr with Zebra NHT */
533 nht_p.family = AF_INET;
534 nht_p.prefixlen = IPV4_MAX_BITLEN;
535 nht_p.u.prefix4 =
536 rp_all->rp.rpf_addr.u.prefix4; // RP address
537 if (PIM_DEBUG_PIM_NHT_RP) {
538 char buf[PREFIX2STR_BUFFER];
539 char buf1[PREFIX2STR_BUFFER];
540 prefix2str(&nht_p, buf, sizeof(buf));
541 prefix2str(&rp_all->group, buf1, sizeof(buf1));
542 zlog_debug(
543 "%s: NHT Register rp_all addr %s grp %s ",
544 __PRETTY_FUNCTION__, buf, buf1);
545 }
546
547 for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode,
548 up)) {
549 /* Find (*, G) upstream whose RP is not
550 * configured yet
551 */
552 if ((up->upstream_addr.s_addr == INADDR_ANY)
553 && (up->sg.src.s_addr == INADDR_ANY)) {
554 struct prefix grp;
555 struct rp_info *trp_info;
556
557 grp.family = AF_INET;
558 grp.prefixlen = IPV4_MAX_BITLEN;
559 grp.u.prefix4 = up->sg.grp;
560 trp_info = pim_rp_find_match_group(pim,
561 &grp);
562 if (trp_info == rp_all)
563 pim_upstream_update(pim, up);
564 }
565 }
566
567 memset(&pnc, 0, sizeof(struct pim_nexthop_cache));
568 if (pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_all,
569 &pnc)) {
570 if (!pim_ecmp_nexthop_search(
571 pim, &pnc,
572 &rp_all->rp.source_nexthop, &nht_p,
573 &rp_all->group, 1))
574 return PIM_RP_NO_PATH;
575 } else {
576 if (!pim_ecmp_nexthop_lookup(
577 pim, &rp_all->rp.source_nexthop,
578 &nht_p, &rp_all->group, 1))
579 return PIM_RP_NO_PATH;
580 }
581 pim_rp_check_interfaces(pim, rp_all);
582 pim_rp_refresh_group_to_rp_mapping(pim);
583 return PIM_SUCCESS;
584 }
585
586 /*
587 * Return if the group is already configured for this RP
588 */
589 if (pim_rp_find_exact(pim, rp_info->rp.rpf_addr.u.prefix4,
590 &rp_info->group)) {
591 XFREE(MTYPE_PIM_RP, rp_info);
592 return PIM_SUCCESS;
593 }
594
595 /*
596 * Barf if this group is already covered by some other RP
597 */
598 tmp_rp_info = pim_rp_find_match_group(pim, &rp_info->group);
599
600 if (tmp_rp_info) {
601 if (tmp_rp_info->plist) {
602 XFREE(MTYPE_PIM_RP, rp_info);
603 return PIM_GROUP_PFXLIST_OVERLAP;
604 } else {
605 /*
606 * If the only RP that covers this group is an
607 * RP configured for
608 * 224.0.0.0/4 that is fine, ignore that one.
609 * For all others
610 * though we must return PIM_GROUP_OVERLAP
611 */
612 if (prefix_same(&rp_info->group,
613 &tmp_rp_info->group)) {
614 XFREE(MTYPE_PIM_RP, rp_info);
615 return PIM_GROUP_OVERLAP;
616 }
617 }
618 }
619 }
620
621 listnode_add_sort(pim->rp_list, rp_info);
622 rn = route_node_get(pim->rp_table, &rp_info->group);
623 rn->info = rp_info;
624
625 if (PIM_DEBUG_TRACE) {
626 char buf[PREFIX_STRLEN];
627
628 zlog_debug("Allocated: %p for rp_info: %p(%s) Lock: %d", rn,
629 rp_info,
630 prefix2str(&rp_info->group, buf, sizeof(buf)),
631 rn->lock);
632 }
633
634 for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, up)) {
635 if (up->sg.src.s_addr == INADDR_ANY) {
636 struct prefix grp;
637 struct rp_info *trp_info;
638
639 grp.family = AF_INET;
640 grp.prefixlen = IPV4_MAX_BITLEN;
641 grp.u.prefix4 = up->sg.grp;
642 trp_info = pim_rp_find_match_group(pim, &grp);
643
644 if (trp_info == rp_info)
645 pim_upstream_update(pim, up);
646 }
647 }
648
649 /* Register addr with Zebra NHT */
650 nht_p.family = AF_INET;
651 nht_p.prefixlen = IPV4_MAX_BITLEN;
652 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
653 if (PIM_DEBUG_PIM_NHT_RP) {
654 char buf[PREFIX2STR_BUFFER];
655 char buf1[PREFIX2STR_BUFFER];
656 prefix2str(&nht_p, buf, sizeof(buf));
657 prefix2str(&rp_info->group, buf1, sizeof(buf1));
658 zlog_debug("%s: NHT Register RP addr %s grp %s with Zebra ",
659 __PRETTY_FUNCTION__, buf, buf1);
660 }
661
662 memset(&pnc, 0, sizeof(struct pim_nexthop_cache));
663 if (pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, &pnc)) {
664 if (!pim_ecmp_nexthop_search(pim, &pnc,
665 &rp_info->rp.source_nexthop,
666 &nht_p, &rp_info->group, 1))
667 return PIM_RP_NO_PATH;
668 } else {
669 if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop,
670 &nht_p, &rp_info->group, 1))
671 return PIM_RP_NO_PATH;
672 }
673
674 pim_rp_check_interfaces(pim, rp_info);
675 pim_rp_refresh_group_to_rp_mapping(pim);
676 return PIM_SUCCESS;
677 }
678
679 int pim_rp_del(struct pim_instance *pim, const char *rp,
680 const char *group_range, const char *plist)
681 {
682 struct prefix group;
683 struct in_addr rp_addr;
684 struct prefix g_all;
685 struct rp_info *rp_info;
686 struct rp_info *rp_all;
687 int result;
688 struct prefix nht_p;
689 struct route_node *rn;
690 bool was_plist = false;
691 struct rp_info *trp_info;
692 struct pim_upstream *up;
693 struct listnode *upnode;
694
695 if (group_range == NULL)
696 result = str2prefix("224.0.0.0/4", &group);
697 else
698 result = str2prefix(group_range, &group);
699
700 if (!result)
701 return PIM_GROUP_BAD_ADDRESS;
702
703 result = inet_pton(AF_INET, rp, &rp_addr);
704 if (result <= 0)
705 return PIM_RP_BAD_ADDRESS;
706
707 if (plist)
708 rp_info = pim_rp_find_prefix_list(pim, rp_addr, plist);
709 else
710 rp_info = pim_rp_find_exact(pim, rp_addr, &group);
711
712 if (!rp_info)
713 return PIM_RP_NOT_FOUND;
714
715 if (rp_info->plist) {
716 XFREE(MTYPE_PIM_FILTER_NAME, rp_info->plist);
717 was_plist = true;
718 }
719
720 /* Deregister addr with Zebra NHT */
721 nht_p.family = AF_INET;
722 nht_p.prefixlen = IPV4_MAX_BITLEN;
723 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
724 if (PIM_DEBUG_PIM_NHT_RP) {
725 char buf[PREFIX2STR_BUFFER];
726 prefix2str(&nht_p, buf, sizeof(buf));
727 zlog_debug("%s: Deregister RP addr %s with Zebra ",
728 __PRETTY_FUNCTION__, buf);
729 }
730 pim_delete_tracked_nexthop(pim, &nht_p, NULL, rp_info);
731
732 if (!str2prefix("224.0.0.0/4", &g_all))
733 return PIM_RP_BAD_ADDRESS;
734
735 rp_all = pim_rp_find_match_group(pim, &g_all);
736
737 if (rp_all == rp_info) {
738 for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, up)) {
739 /* Find the upstream (*, G) whose upstream address is
740 * same as the deleted RP
741 */
742 if ((up->upstream_addr.s_addr == rp_addr.s_addr) &&
743 (up->sg.src.s_addr == INADDR_ANY)) {
744 struct prefix grp;
745 grp.family = AF_INET;
746 grp.prefixlen = IPV4_MAX_BITLEN;
747 grp.u.prefix4 = up->sg.grp;
748 trp_info = pim_rp_find_match_group(pim, &grp);
749 if (trp_info == rp_all) {
750 pim_upstream_rpf_clear(pim, up);
751 up->upstream_addr.s_addr = INADDR_ANY;
752 }
753 }
754 }
755 rp_all->rp.rpf_addr.family = AF_INET;
756 rp_all->rp.rpf_addr.u.prefix4.s_addr = INADDR_NONE;
757 rp_all->i_am_rp = 0;
758 return PIM_SUCCESS;
759 }
760
761 listnode_delete(pim->rp_list, rp_info);
762
763 if (!was_plist) {
764 rn = route_node_get(pim->rp_table, &rp_info->group);
765 if (rn) {
766 if (rn->info != rp_info)
767 flog_err(
768 EC_LIB_DEVELOPMENT,
769 "Expected rn->info to be equal to rp_info");
770
771 if (PIM_DEBUG_TRACE) {
772 char buf[PREFIX_STRLEN];
773
774 zlog_debug(
775 "%s:Found for Freeing: %p for rp_info: %p(%s) Lock: %d",
776 __PRETTY_FUNCTION__, rn, rp_info,
777 prefix2str(&rp_info->group, buf,
778 sizeof(buf)),
779 rn->lock);
780 }
781 rn->info = NULL;
782 route_unlock_node(rn);
783 route_unlock_node(rn);
784 }
785 }
786
787 pim_rp_refresh_group_to_rp_mapping(pim);
788
789 for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, up)) {
790 /* Find the upstream (*, G) whose upstream address is same as
791 * the deleted RP
792 */
793 if ((up->upstream_addr.s_addr == rp_addr.s_addr) &&
794 (up->sg.src.s_addr == INADDR_ANY)) {
795 struct prefix grp;
796
797 grp.family = AF_INET;
798 grp.prefixlen = IPV4_MAX_BITLEN;
799 grp.u.prefix4 = up->sg.grp;
800
801 trp_info = pim_rp_find_match_group(pim, &grp);
802
803 /* RP not found for the group grp */
804 if (pim_rpf_addr_is_inaddr_none(&trp_info->rp)) {
805 pim_upstream_rpf_clear(pim, up);
806 pim_rp_set_upstream_addr(pim,
807 &up->upstream_addr,
808 up->sg.src, up->sg.grp);
809 }
810
811 /* RP found for the group grp */
812 else
813 pim_upstream_update(pim, up);
814 }
815 }
816
817 XFREE(MTYPE_PIM_RP, rp_info);
818 return PIM_SUCCESS;
819 }
820
821 void pim_rp_setup(struct pim_instance *pim)
822 {
823 struct listnode *node;
824 struct rp_info *rp_info;
825 struct prefix nht_p;
826 struct pim_nexthop_cache pnc;
827
828 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
829 if (rp_info->rp.rpf_addr.u.prefix4.s_addr == INADDR_NONE)
830 continue;
831
832 nht_p.family = AF_INET;
833 nht_p.prefixlen = IPV4_MAX_BITLEN;
834 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
835 memset(&pnc, 0, sizeof(struct pim_nexthop_cache));
836 if (pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, &pnc))
837 pim_ecmp_nexthop_search(pim, &pnc,
838 &rp_info->rp.source_nexthop,
839 &nht_p, &rp_info->group, 1);
840 else {
841 if (PIM_DEBUG_PIM_NHT_RP) {
842 char buf[PREFIX2STR_BUFFER];
843
844 prefix2str(&nht_p, buf, sizeof(buf));
845 zlog_debug(
846 "%s: NHT Local Nexthop not found for RP %s ",
847 __PRETTY_FUNCTION__, buf);
848 }
849 if (!pim_ecmp_nexthop_lookup(pim,
850 &rp_info->rp.source_nexthop,
851 &nht_p, &rp_info->group, 1))
852 if (PIM_DEBUG_PIM_NHT_RP)
853 zlog_debug(
854 "Unable to lookup nexthop for rp specified");
855 }
856 }
857 }
858
859 /*
860 * Checks to see if we should elect ourself the actual RP when new if
861 * addresses are added against an interface.
862 */
863 void pim_rp_check_on_if_add(struct pim_interface *pim_ifp)
864 {
865 struct listnode *node;
866 struct rp_info *rp_info;
867 bool i_am_rp_changed = false;
868 struct pim_instance *pim = pim_ifp->pim;
869
870 if (pim->rp_list == NULL)
871 return;
872
873 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
874 if (pim_rpf_addr_is_inaddr_none(&rp_info->rp))
875 continue;
876
877 /* if i_am_rp is already set nothing to be done (adding new
878 * addresses
879 * is not going to make a difference). */
880 if (rp_info->i_am_rp) {
881 continue;
882 }
883
884 if (pim_rp_check_interface_addrs(rp_info, pim_ifp)) {
885 i_am_rp_changed = true;
886 rp_info->i_am_rp = 1;
887 if (PIM_DEBUG_PIM_NHT_RP) {
888 char rp[PREFIX_STRLEN];
889 pim_addr_dump("<rp?>", &rp_info->rp.rpf_addr,
890 rp, sizeof(rp));
891 zlog_debug("%s: %s: i am rp", __func__, rp);
892 }
893 }
894 }
895
896 if (i_am_rp_changed) {
897 pim_msdp_i_am_rp_changed(pim);
898 }
899 }
900
901 /* up-optimized re-evaluation of "i_am_rp". this is used when ifaddresses
902 * are removed. Removing numbers is an uncommon event in an active network
903 * so I have made no attempt to optimize it. */
904 void pim_i_am_rp_re_evaluate(struct pim_instance *pim)
905 {
906 struct listnode *node;
907 struct rp_info *rp_info;
908 bool i_am_rp_changed = false;
909 int old_i_am_rp;
910
911 if (pim->rp_list == NULL)
912 return;
913
914 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
915 if (pim_rpf_addr_is_inaddr_none(&rp_info->rp))
916 continue;
917
918 old_i_am_rp = rp_info->i_am_rp;
919 pim_rp_check_interfaces(pim, rp_info);
920
921 if (old_i_am_rp != rp_info->i_am_rp) {
922 i_am_rp_changed = true;
923 if (PIM_DEBUG_PIM_NHT_RP) {
924 char rp[PREFIX_STRLEN];
925 pim_addr_dump("<rp?>", &rp_info->rp.rpf_addr,
926 rp, sizeof(rp));
927 if (rp_info->i_am_rp) {
928 zlog_debug("%s: %s: i am rp", __func__,
929 rp);
930 } else {
931 zlog_debug("%s: %s: i am no longer rp",
932 __func__, rp);
933 }
934 }
935 }
936 }
937
938 if (i_am_rp_changed) {
939 pim_msdp_i_am_rp_changed(pim);
940 }
941 }
942
943 /*
944 * I_am_RP(G) is true if the group-to-RP mapping indicates that
945 * this router is the RP for the group.
946 *
947 * Since we only have static RP, all groups are part of this RP
948 */
949 int pim_rp_i_am_rp(struct pim_instance *pim, struct in_addr group)
950 {
951 struct prefix g;
952 struct rp_info *rp_info;
953
954 memset(&g, 0, sizeof(g));
955 g.family = AF_INET;
956 g.prefixlen = 32;
957 g.u.prefix4 = group;
958
959 rp_info = pim_rp_find_match_group(pim, &g);
960
961 if (rp_info)
962 return rp_info->i_am_rp;
963
964 return 0;
965 }
966
967 /*
968 * RP(G)
969 *
970 * Return the RP that the Group belongs too.
971 */
972 struct pim_rpf *pim_rp_g(struct pim_instance *pim, struct in_addr group)
973 {
974 struct prefix g;
975 struct rp_info *rp_info;
976
977 memset(&g, 0, sizeof(g));
978 g.family = AF_INET;
979 g.prefixlen = 32;
980 g.u.prefix4 = group;
981
982 rp_info = pim_rp_find_match_group(pim, &g);
983
984 if (rp_info) {
985 struct prefix nht_p;
986 struct pim_nexthop_cache pnc;
987 /* Register addr with Zebra NHT */
988 nht_p.family = AF_INET;
989 nht_p.prefixlen = IPV4_MAX_BITLEN;
990 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
991 if (PIM_DEBUG_PIM_NHT_RP) {
992 char buf[PREFIX2STR_BUFFER];
993 char buf1[PREFIX2STR_BUFFER];
994 prefix2str(&nht_p, buf, sizeof(buf));
995 prefix2str(&rp_info->group, buf1, sizeof(buf1));
996 zlog_debug(
997 "%s: NHT Register RP addr %s grp %s with Zebra",
998 __PRETTY_FUNCTION__, buf, buf1);
999 }
1000 memset(&pnc, 0, sizeof(struct pim_nexthop_cache));
1001 if (pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, &pnc))
1002 pim_ecmp_nexthop_search(pim, &pnc,
1003 &rp_info->rp.source_nexthop,
1004 &nht_p, &rp_info->group, 1);
1005 else {
1006 if (PIM_DEBUG_PIM_NHT_RP) {
1007 char buf[PREFIX2STR_BUFFER];
1008 char buf1[PREFIX2STR_BUFFER];
1009 prefix2str(&nht_p, buf, sizeof(buf));
1010 prefix2str(&g, buf1, sizeof(buf1));
1011 zlog_debug(
1012 "%s: Nexthop cache not found for RP %s grp %s register with Zebra",
1013 __PRETTY_FUNCTION__, buf, buf1);
1014 }
1015 pim_rpf_set_refresh_time(pim);
1016 (void)pim_ecmp_nexthop_lookup(
1017 pim, &rp_info->rp.source_nexthop, &nht_p,
1018 &rp_info->group, 1);
1019 }
1020 return (&rp_info->rp);
1021 }
1022
1023 // About to Go Down
1024 return NULL;
1025 }
1026
1027 /*
1028 * Set the upstream IP address we want to talk to based upon
1029 * the rp configured and the source address
1030 *
1031 * If we have don't have a RP configured and the source address is *
1032 * then set the upstream addr as INADDR_ANY and return failure.
1033 *
1034 */
1035 int pim_rp_set_upstream_addr(struct pim_instance *pim, struct in_addr *up,
1036 struct in_addr source, struct in_addr group)
1037 {
1038 struct rp_info *rp_info;
1039 struct prefix g;
1040
1041 memset(&g, 0, sizeof(g));
1042 g.family = AF_INET;
1043 g.prefixlen = 32;
1044 g.u.prefix4 = group;
1045
1046 rp_info = pim_rp_find_match_group(pim, &g);
1047
1048 if ((pim_rpf_addr_is_inaddr_none(&rp_info->rp))
1049 && (source.s_addr == INADDR_ANY)) {
1050 if (PIM_DEBUG_PIM_NHT_RP)
1051 zlog_debug("%s: Received a (*,G) with no RP configured",
1052 __PRETTY_FUNCTION__);
1053 up->s_addr = INADDR_ANY;
1054 return 0;
1055 }
1056
1057 *up = (source.s_addr == INADDR_ANY) ? rp_info->rp.rpf_addr.u.prefix4
1058 : source;
1059
1060 return 1;
1061 }
1062
1063 int pim_rp_config_write(struct pim_instance *pim, struct vty *vty,
1064 const char *spaces)
1065 {
1066 struct listnode *node;
1067 struct rp_info *rp_info;
1068 char rp_buffer[32];
1069 char group_buffer[32];
1070 int count = 0;
1071
1072 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
1073 if (pim_rpf_addr_is_inaddr_none(&rp_info->rp))
1074 continue;
1075
1076 if (rp_info->plist)
1077 vty_out(vty, "%sip pim rp %s prefix-list %s\n", spaces,
1078 inet_ntop(AF_INET,
1079 &rp_info->rp.rpf_addr.u.prefix4,
1080 rp_buffer, 32),
1081 rp_info->plist);
1082 else
1083 vty_out(vty, "%sip pim rp %s %s\n", spaces,
1084 inet_ntop(AF_INET,
1085 &rp_info->rp.rpf_addr.u.prefix4,
1086 rp_buffer, 32),
1087 prefix2str(&rp_info->group, group_buffer, 32));
1088 count++;
1089 }
1090
1091 return count;
1092 }
1093
1094 int pim_rp_check_is_my_ip_address(struct pim_instance *pim,
1095 struct in_addr group,
1096 struct in_addr dest_addr)
1097 {
1098 struct rp_info *rp_info;
1099 struct prefix g;
1100
1101 memset(&g, 0, sizeof(g));
1102 g.family = AF_INET;
1103 g.prefixlen = 32;
1104 g.u.prefix4 = group;
1105
1106 rp_info = pim_rp_find_match_group(pim, &g);
1107 /*
1108 * See if we can short-cut some?
1109 * This might not make sense if we ever leave a static RP
1110 * type of configuration.
1111 * Note - Premature optimization might bite our patooeys' here.
1112 */
1113 if (I_am_RP(pim, group)) {
1114 if (dest_addr.s_addr == rp_info->rp.rpf_addr.u.prefix4.s_addr)
1115 return 1;
1116 }
1117
1118 if (if_lookup_exact_address(&dest_addr, AF_INET, pim->vrf_id))
1119 return 1;
1120
1121 return 0;
1122 }
1123
1124 void pim_rp_show_information(struct pim_instance *pim, struct vty *vty, bool uj)
1125 {
1126 struct rp_info *rp_info;
1127 struct rp_info *prev_rp_info = NULL;
1128 struct listnode *node;
1129
1130 json_object *json = NULL;
1131 json_object *json_rp_rows = NULL;
1132 json_object *json_row = NULL;
1133
1134 if (uj)
1135 json = json_object_new_object();
1136 else
1137 vty_out(vty,
1138 "RP address group/prefix-list OIF I am RP\n");
1139
1140 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
1141 if (!pim_rpf_addr_is_inaddr_none(&rp_info->rp)) {
1142 char buf[48];
1143
1144 if (uj) {
1145 /*
1146 * If we have moved on to a new RP then add the
1147 * entry for the previous RP
1148 */
1149 if (prev_rp_info
1150 && prev_rp_info->rp.rpf_addr.u.prefix4
1151 .s_addr
1152 != rp_info->rp.rpf_addr.u.prefix4
1153 .s_addr) {
1154 json_object_object_add(
1155 json,
1156 inet_ntoa(prev_rp_info->rp
1157 .rpf_addr.u
1158 .prefix4),
1159 json_rp_rows);
1160 json_rp_rows = NULL;
1161 }
1162
1163 if (!json_rp_rows)
1164 json_rp_rows = json_object_new_array();
1165
1166 json_row = json_object_new_object();
1167 if (rp_info->rp.source_nexthop.interface)
1168 json_object_string_add(
1169 json_row, "outboundInterface",
1170 rp_info->rp.source_nexthop
1171 .interface->name);
1172
1173 if (rp_info->i_am_rp)
1174 json_object_boolean_true_add(json_row,
1175 "iAmRP");
1176
1177 if (rp_info->plist)
1178 json_object_string_add(json_row,
1179 "prefixList",
1180 rp_info->plist);
1181 else
1182 json_object_string_add(
1183 json_row, "group",
1184 prefix2str(&rp_info->group, buf,
1185 48));
1186
1187 json_object_array_add(json_rp_rows, json_row);
1188 } else {
1189 vty_out(vty, "%-15s ",
1190 inet_ntoa(rp_info->rp.rpf_addr.u
1191 .prefix4));
1192
1193 if (rp_info->plist)
1194 vty_out(vty, "%-18s ", rp_info->plist);
1195 else
1196 vty_out(vty, "%-18s ",
1197 prefix2str(&rp_info->group, buf,
1198 48));
1199
1200 if (rp_info->rp.source_nexthop.interface)
1201 vty_out(vty, "%-10s ",
1202 rp_info->rp.source_nexthop
1203 .interface->name);
1204 else
1205 vty_out(vty, "%-10s ", "(Unknown)");
1206
1207 if (rp_info->i_am_rp)
1208 vty_out(vty, "yes\n");
1209 else
1210 vty_out(vty, "no\n");
1211 }
1212
1213 prev_rp_info = rp_info;
1214 }
1215 }
1216
1217 if (uj) {
1218 if (prev_rp_info && json_rp_rows)
1219 json_object_object_add(
1220 json,
1221 inet_ntoa(prev_rp_info->rp.rpf_addr.u.prefix4),
1222 json_rp_rows);
1223
1224 vty_out(vty, "%s\n", json_object_to_json_string_ext(
1225 json, JSON_C_TO_STRING_PRETTY));
1226 json_object_free(json);
1227 }
1228 }
1229
1230 void pim_resolve_rp_nh(struct pim_instance *pim)
1231 {
1232 struct listnode *node = NULL;
1233 struct rp_info *rp_info = NULL;
1234 struct nexthop *nh_node = NULL;
1235 struct prefix nht_p;
1236 struct pim_nexthop_cache pnc;
1237 struct pim_neighbor *nbr = NULL;
1238
1239 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
1240 if (rp_info->rp.rpf_addr.u.prefix4.s_addr == INADDR_NONE)
1241 continue;
1242
1243 nht_p.family = AF_INET;
1244 nht_p.prefixlen = IPV4_MAX_BITLEN;
1245 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
1246 memset(&pnc, 0, sizeof(struct pim_nexthop_cache));
1247 if (!pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info,
1248 &pnc))
1249 continue;
1250
1251 for (nh_node = pnc.nexthop; nh_node; nh_node = nh_node->next) {
1252 if (nh_node->gate.ipv4.s_addr != 0)
1253 continue;
1254
1255 struct interface *ifp1 = if_lookup_by_index(
1256 nh_node->ifindex, pim->vrf_id);
1257 nbr = pim_neighbor_find_if(ifp1);
1258 if (!nbr)
1259 continue;
1260
1261 nh_node->gate.ipv4 = nbr->source_addr;
1262 if (PIM_DEBUG_PIM_NHT_RP) {
1263 char str[PREFIX_STRLEN];
1264 char str1[INET_ADDRSTRLEN];
1265 pim_inet4_dump("<nht_nbr?>", nbr->source_addr,
1266 str1, sizeof(str1));
1267 pim_addr_dump("<nht_addr?>", &nht_p, str,
1268 sizeof(str));
1269 zlog_debug(
1270 "%s: addr %s new nexthop addr %s interface %s",
1271 __PRETTY_FUNCTION__, str, str1,
1272 ifp1->name);
1273 }
1274 }
1275 }
1276 }