]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_rp.c
zebra: move rtadv service from zrouter to zvrf
[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 route_node *rn;
422 struct pim_upstream *up;
423 struct listnode *upnode;
424
425 rp_info = XCALLOC(MTYPE_PIM_RP, sizeof(*rp_info));
426
427 if (group_range == NULL)
428 result = str2prefix("224.0.0.0/4", &rp_info->group);
429 else {
430 result = str2prefix(group_range, &rp_info->group);
431 if (result) {
432 prefix_copy(&temp, &rp_info->group);
433 apply_mask(&temp);
434 if (!prefix_same(&rp_info->group, &temp)) {
435 XFREE(MTYPE_PIM_RP, rp_info);
436 return PIM_GROUP_BAD_ADDR_MASK_COMBO;
437 }
438 }
439 }
440
441 if (!result) {
442 XFREE(MTYPE_PIM_RP, rp_info);
443 return PIM_GROUP_BAD_ADDRESS;
444 }
445
446 rp_info->rp.rpf_addr.family = AF_INET;
447 rp_info->rp.rpf_addr.prefixlen = IPV4_MAX_PREFIXLEN;
448 result = inet_pton(rp_info->rp.rpf_addr.family, rp,
449 &rp_info->rp.rpf_addr.u.prefix4);
450
451 if (result <= 0) {
452 XFREE(MTYPE_PIM_RP, rp_info);
453 return PIM_RP_BAD_ADDRESS;
454 }
455
456 if (plist) {
457 /*
458 * Return if the prefix-list is already configured for this RP
459 */
460 if (pim_rp_find_prefix_list(pim, rp_info->rp.rpf_addr.u.prefix4,
461 plist)) {
462 XFREE(MTYPE_PIM_RP, rp_info);
463 return PIM_SUCCESS;
464 }
465
466 /*
467 * Barf if the prefix-list is already configured for an RP
468 */
469 if (pim_rp_prefix_list_used(pim, plist)) {
470 XFREE(MTYPE_PIM_RP, rp_info);
471 return PIM_RP_PFXLIST_IN_USE;
472 }
473
474 /*
475 * Free any existing rp_info entries for this RP
476 */
477 for (ALL_LIST_ELEMENTS(pim->rp_list, node, nnode,
478 tmp_rp_info)) {
479 if (rp_info->rp.rpf_addr.u.prefix4.s_addr
480 == tmp_rp_info->rp.rpf_addr.u.prefix4.s_addr) {
481 if (tmp_rp_info->plist)
482 pim_rp_del(pim, rp, NULL,
483 tmp_rp_info->plist);
484 else
485 pim_rp_del(
486 pim, rp,
487 prefix2str(&tmp_rp_info->group,
488 buffer, BUFSIZ),
489 NULL);
490 }
491 }
492
493 rp_info->plist = XSTRDUP(MTYPE_PIM_FILTER_NAME, plist);
494 } else {
495
496 if (!str2prefix("224.0.0.0/4", &group_all)) {
497 XFREE(MTYPE_PIM_RP, rp_info);
498 return PIM_GROUP_BAD_ADDRESS;
499 }
500 rp_all = pim_rp_find_match_group(pim, &group_all);
501
502 /*
503 * Barf if group is a non-multicast subnet
504 */
505 if (!prefix_match(&rp_all->group, &rp_info->group)) {
506 XFREE(MTYPE_PIM_RP, rp_info);
507 return PIM_GROUP_BAD_ADDRESS;
508 }
509
510 /*
511 * Remove any prefix-list rp_info entries for this RP
512 */
513 for (ALL_LIST_ELEMENTS(pim->rp_list, node, nnode,
514 tmp_rp_info)) {
515 if (tmp_rp_info->plist
516 && rp_info->rp.rpf_addr.u.prefix4.s_addr
517 == tmp_rp_info->rp.rpf_addr.u.prefix4
518 .s_addr) {
519 pim_rp_del(pim, rp, NULL, tmp_rp_info->plist);
520 }
521 }
522
523 /*
524 * Take over the 224.0.0.0/4 group if the rp is INADDR_NONE
525 */
526 if (prefix_same(&rp_all->group, &rp_info->group)
527 && pim_rpf_addr_is_inaddr_none(&rp_all->rp)) {
528 rp_all->rp.rpf_addr = rp_info->rp.rpf_addr;
529 XFREE(MTYPE_PIM_RP, rp_info);
530
531 /* Register addr with Zebra NHT */
532 nht_p.family = AF_INET;
533 nht_p.prefixlen = IPV4_MAX_BITLEN;
534 nht_p.u.prefix4 =
535 rp_all->rp.rpf_addr.u.prefix4; // RP address
536 if (PIM_DEBUG_PIM_NHT_RP) {
537 char buf[PREFIX2STR_BUFFER];
538 char buf1[PREFIX2STR_BUFFER];
539 prefix2str(&nht_p, buf, sizeof(buf));
540 prefix2str(&rp_all->group, buf1, sizeof(buf1));
541 zlog_debug(
542 "%s: NHT Register rp_all addr %s grp %s ",
543 __PRETTY_FUNCTION__, buf, buf1);
544 }
545
546 for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode,
547 up)) {
548 /* Find (*, G) upstream whose RP is not
549 * configured yet
550 */
551 if ((up->upstream_addr.s_addr == INADDR_ANY)
552 && (up->sg.src.s_addr == INADDR_ANY)) {
553 struct prefix grp;
554 struct rp_info *trp_info;
555
556 grp.family = AF_INET;
557 grp.prefixlen = IPV4_MAX_BITLEN;
558 grp.u.prefix4 = up->sg.grp;
559 trp_info = pim_rp_find_match_group(pim,
560 &grp);
561 if (trp_info == rp_all)
562 pim_upstream_update(pim, up);
563 }
564 }
565
566 pim_rp_check_interfaces(pim, rp_all);
567 pim_rp_refresh_group_to_rp_mapping(pim);
568
569 pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_all,
570 NULL);
571 if (!pim_ecmp_nexthop_lookup(pim,
572 &rp_all->rp.source_nexthop,
573 &nht_p, &rp_all->group, 1))
574 return PIM_RP_NO_PATH;
575
576 return PIM_SUCCESS;
577 }
578
579 /*
580 * Return if the group is already configured for this RP
581 */
582 if (pim_rp_find_exact(pim, rp_info->rp.rpf_addr.u.prefix4,
583 &rp_info->group)) {
584 XFREE(MTYPE_PIM_RP, rp_info);
585 return PIM_SUCCESS;
586 }
587
588 /*
589 * Barf if this group is already covered by some other RP
590 */
591 tmp_rp_info = pim_rp_find_match_group(pim, &rp_info->group);
592
593 if (tmp_rp_info) {
594 if (tmp_rp_info->plist) {
595 XFREE(MTYPE_PIM_RP, rp_info);
596 return PIM_GROUP_PFXLIST_OVERLAP;
597 } else {
598 /*
599 * If the only RP that covers this group is an
600 * RP configured for
601 * 224.0.0.0/4 that is fine, ignore that one.
602 * For all others
603 * though we must return PIM_GROUP_OVERLAP
604 */
605 if (prefix_same(&rp_info->group,
606 &tmp_rp_info->group)) {
607 XFREE(MTYPE_PIM_RP, rp_info);
608 return PIM_GROUP_OVERLAP;
609 }
610 }
611 }
612 }
613
614 listnode_add_sort(pim->rp_list, rp_info);
615 rn = route_node_get(pim->rp_table, &rp_info->group);
616 rn->info = rp_info;
617
618 if (PIM_DEBUG_TRACE) {
619 char buf[PREFIX_STRLEN];
620
621 zlog_debug("Allocated: %p for rp_info: %p(%s) Lock: %d", rn,
622 rp_info,
623 prefix2str(&rp_info->group, buf, sizeof(buf)),
624 rn->lock);
625 }
626
627 for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, up)) {
628 if (up->sg.src.s_addr == INADDR_ANY) {
629 struct prefix grp;
630 struct rp_info *trp_info;
631
632 grp.family = AF_INET;
633 grp.prefixlen = IPV4_MAX_BITLEN;
634 grp.u.prefix4 = up->sg.grp;
635 trp_info = pim_rp_find_match_group(pim, &grp);
636
637 if (trp_info == rp_info)
638 pim_upstream_update(pim, up);
639 }
640 }
641
642 pim_rp_check_interfaces(pim, rp_info);
643 pim_rp_refresh_group_to_rp_mapping(pim);
644
645 /* Register addr with Zebra NHT */
646 nht_p.family = AF_INET;
647 nht_p.prefixlen = IPV4_MAX_BITLEN;
648 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
649 if (PIM_DEBUG_PIM_NHT_RP) {
650 char buf[PREFIX2STR_BUFFER];
651 char buf1[PREFIX2STR_BUFFER];
652 prefix2str(&nht_p, buf, sizeof(buf));
653 prefix2str(&rp_info->group, buf1, sizeof(buf1));
654 zlog_debug("%s: NHT Register RP addr %s grp %s with Zebra ",
655 __PRETTY_FUNCTION__, buf, buf1);
656 }
657
658 pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, NULL);
659 if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop, &nht_p,
660 &rp_info->group, 1))
661 return PIM_RP_NO_PATH;
662
663 return PIM_SUCCESS;
664 }
665
666 int pim_rp_del(struct pim_instance *pim, const char *rp,
667 const char *group_range, const char *plist)
668 {
669 struct prefix group;
670 struct in_addr rp_addr;
671 struct prefix g_all;
672 struct rp_info *rp_info;
673 struct rp_info *rp_all;
674 int result;
675 struct prefix nht_p;
676 struct route_node *rn;
677 bool was_plist = false;
678 struct rp_info *trp_info;
679 struct pim_upstream *up;
680 struct listnode *upnode;
681
682 if (group_range == NULL)
683 result = str2prefix("224.0.0.0/4", &group);
684 else
685 result = str2prefix(group_range, &group);
686
687 if (!result)
688 return PIM_GROUP_BAD_ADDRESS;
689
690 result = inet_pton(AF_INET, rp, &rp_addr);
691 if (result <= 0)
692 return PIM_RP_BAD_ADDRESS;
693
694 if (plist)
695 rp_info = pim_rp_find_prefix_list(pim, rp_addr, plist);
696 else
697 rp_info = pim_rp_find_exact(pim, rp_addr, &group);
698
699 if (!rp_info)
700 return PIM_RP_NOT_FOUND;
701
702 if (rp_info->plist) {
703 XFREE(MTYPE_PIM_FILTER_NAME, rp_info->plist);
704 was_plist = true;
705 }
706
707 /* Deregister addr with Zebra NHT */
708 nht_p.family = AF_INET;
709 nht_p.prefixlen = IPV4_MAX_BITLEN;
710 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
711 if (PIM_DEBUG_PIM_NHT_RP) {
712 char buf[PREFIX2STR_BUFFER];
713 prefix2str(&nht_p, buf, sizeof(buf));
714 zlog_debug("%s: Deregister RP addr %s with Zebra ",
715 __PRETTY_FUNCTION__, buf);
716 }
717 pim_delete_tracked_nexthop(pim, &nht_p, NULL, rp_info);
718
719 if (!str2prefix("224.0.0.0/4", &g_all))
720 return PIM_RP_BAD_ADDRESS;
721
722 rp_all = pim_rp_find_match_group(pim, &g_all);
723
724 if (rp_all == rp_info) {
725 for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, up)) {
726 /* Find the upstream (*, G) whose upstream address is
727 * same as the deleted RP
728 */
729 if ((up->upstream_addr.s_addr == rp_addr.s_addr) &&
730 (up->sg.src.s_addr == INADDR_ANY)) {
731 struct prefix grp;
732 grp.family = AF_INET;
733 grp.prefixlen = IPV4_MAX_BITLEN;
734 grp.u.prefix4 = up->sg.grp;
735 trp_info = pim_rp_find_match_group(pim, &grp);
736 if (trp_info == rp_all) {
737 pim_upstream_rpf_clear(pim, up);
738 up->upstream_addr.s_addr = INADDR_ANY;
739 }
740 }
741 }
742 rp_all->rp.rpf_addr.family = AF_INET;
743 rp_all->rp.rpf_addr.u.prefix4.s_addr = INADDR_NONE;
744 rp_all->i_am_rp = 0;
745 return PIM_SUCCESS;
746 }
747
748 listnode_delete(pim->rp_list, rp_info);
749
750 if (!was_plist) {
751 rn = route_node_get(pim->rp_table, &rp_info->group);
752 if (rn) {
753 if (rn->info != rp_info)
754 flog_err(
755 EC_LIB_DEVELOPMENT,
756 "Expected rn->info to be equal to rp_info");
757
758 if (PIM_DEBUG_TRACE) {
759 char buf[PREFIX_STRLEN];
760
761 zlog_debug(
762 "%s:Found for Freeing: %p for rp_info: %p(%s) Lock: %d",
763 __PRETTY_FUNCTION__, rn, rp_info,
764 prefix2str(&rp_info->group, buf,
765 sizeof(buf)),
766 rn->lock);
767 }
768 rn->info = NULL;
769 route_unlock_node(rn);
770 route_unlock_node(rn);
771 }
772 }
773
774 pim_rp_refresh_group_to_rp_mapping(pim);
775
776 for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, up)) {
777 /* Find the upstream (*, G) whose upstream address is same as
778 * the deleted RP
779 */
780 if ((up->upstream_addr.s_addr == rp_addr.s_addr) &&
781 (up->sg.src.s_addr == INADDR_ANY)) {
782 struct prefix grp;
783
784 grp.family = AF_INET;
785 grp.prefixlen = IPV4_MAX_BITLEN;
786 grp.u.prefix4 = up->sg.grp;
787
788 trp_info = pim_rp_find_match_group(pim, &grp);
789
790 /* RP not found for the group grp */
791 if (pim_rpf_addr_is_inaddr_none(&trp_info->rp)) {
792 pim_upstream_rpf_clear(pim, up);
793 pim_rp_set_upstream_addr(pim,
794 &up->upstream_addr,
795 up->sg.src, up->sg.grp);
796 }
797
798 /* RP found for the group grp */
799 else
800 pim_upstream_update(pim, up);
801 }
802 }
803
804 XFREE(MTYPE_PIM_RP, rp_info);
805 return PIM_SUCCESS;
806 }
807
808 void pim_rp_setup(struct pim_instance *pim)
809 {
810 struct listnode *node;
811 struct rp_info *rp_info;
812 struct prefix nht_p;
813
814 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
815 if (rp_info->rp.rpf_addr.u.prefix4.s_addr == INADDR_NONE)
816 continue;
817
818 nht_p.family = AF_INET;
819 nht_p.prefixlen = IPV4_MAX_BITLEN;
820 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
821
822 pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, NULL);
823 if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop,
824 &nht_p, &rp_info->group, 1))
825 if (PIM_DEBUG_PIM_NHT_RP)
826 zlog_debug(
827 "Unable to lookup nexthop for rp specified");
828 }
829 }
830
831 /*
832 * Checks to see if we should elect ourself the actual RP when new if
833 * addresses are added against an interface.
834 */
835 void pim_rp_check_on_if_add(struct pim_interface *pim_ifp)
836 {
837 struct listnode *node;
838 struct rp_info *rp_info;
839 bool i_am_rp_changed = false;
840 struct pim_instance *pim = pim_ifp->pim;
841
842 if (pim->rp_list == NULL)
843 return;
844
845 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
846 if (pim_rpf_addr_is_inaddr_none(&rp_info->rp))
847 continue;
848
849 /* if i_am_rp is already set nothing to be done (adding new
850 * addresses
851 * is not going to make a difference). */
852 if (rp_info->i_am_rp) {
853 continue;
854 }
855
856 if (pim_rp_check_interface_addrs(rp_info, pim_ifp)) {
857 i_am_rp_changed = true;
858 rp_info->i_am_rp = 1;
859 if (PIM_DEBUG_PIM_NHT_RP) {
860 char rp[PREFIX_STRLEN];
861 pim_addr_dump("<rp?>", &rp_info->rp.rpf_addr,
862 rp, sizeof(rp));
863 zlog_debug("%s: %s: i am rp", __func__, rp);
864 }
865 }
866 }
867
868 if (i_am_rp_changed) {
869 pim_msdp_i_am_rp_changed(pim);
870 }
871 }
872
873 /* up-optimized re-evaluation of "i_am_rp". this is used when ifaddresses
874 * are removed. Removing numbers is an uncommon event in an active network
875 * so I have made no attempt to optimize it. */
876 void pim_i_am_rp_re_evaluate(struct pim_instance *pim)
877 {
878 struct listnode *node;
879 struct rp_info *rp_info;
880 bool i_am_rp_changed = false;
881 int old_i_am_rp;
882
883 if (pim->rp_list == NULL)
884 return;
885
886 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
887 if (pim_rpf_addr_is_inaddr_none(&rp_info->rp))
888 continue;
889
890 old_i_am_rp = rp_info->i_am_rp;
891 pim_rp_check_interfaces(pim, rp_info);
892
893 if (old_i_am_rp != rp_info->i_am_rp) {
894 i_am_rp_changed = true;
895 if (PIM_DEBUG_PIM_NHT_RP) {
896 char rp[PREFIX_STRLEN];
897 pim_addr_dump("<rp?>", &rp_info->rp.rpf_addr,
898 rp, sizeof(rp));
899 if (rp_info->i_am_rp) {
900 zlog_debug("%s: %s: i am rp", __func__,
901 rp);
902 } else {
903 zlog_debug("%s: %s: i am no longer rp",
904 __func__, rp);
905 }
906 }
907 }
908 }
909
910 if (i_am_rp_changed) {
911 pim_msdp_i_am_rp_changed(pim);
912 }
913 }
914
915 /*
916 * I_am_RP(G) is true if the group-to-RP mapping indicates that
917 * this router is the RP for the group.
918 *
919 * Since we only have static RP, all groups are part of this RP
920 */
921 int pim_rp_i_am_rp(struct pim_instance *pim, struct in_addr group)
922 {
923 struct prefix g;
924 struct rp_info *rp_info;
925
926 memset(&g, 0, sizeof(g));
927 g.family = AF_INET;
928 g.prefixlen = 32;
929 g.u.prefix4 = group;
930
931 rp_info = pim_rp_find_match_group(pim, &g);
932
933 if (rp_info)
934 return rp_info->i_am_rp;
935
936 return 0;
937 }
938
939 /*
940 * RP(G)
941 *
942 * Return the RP that the Group belongs too.
943 */
944 struct pim_rpf *pim_rp_g(struct pim_instance *pim, struct in_addr group)
945 {
946 struct prefix g;
947 struct rp_info *rp_info;
948
949 memset(&g, 0, sizeof(g));
950 g.family = AF_INET;
951 g.prefixlen = 32;
952 g.u.prefix4 = group;
953
954 rp_info = pim_rp_find_match_group(pim, &g);
955
956 if (rp_info) {
957 struct prefix nht_p;
958
959 /* Register addr with Zebra NHT */
960 nht_p.family = AF_INET;
961 nht_p.prefixlen = IPV4_MAX_BITLEN;
962 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
963 if (PIM_DEBUG_PIM_NHT_RP) {
964 char buf[PREFIX2STR_BUFFER];
965 char buf1[PREFIX2STR_BUFFER];
966 prefix2str(&nht_p, buf, sizeof(buf));
967 prefix2str(&rp_info->group, buf1, sizeof(buf1));
968 zlog_debug(
969 "%s: NHT Register RP addr %s grp %s with Zebra",
970 __PRETTY_FUNCTION__, buf, buf1);
971 }
972
973 pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, NULL);
974 pim_rpf_set_refresh_time(pim);
975 (void)pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop,
976 &nht_p, &rp_info->group, 1);
977 return (&rp_info->rp);
978 }
979
980 // About to Go Down
981 return NULL;
982 }
983
984 /*
985 * Set the upstream IP address we want to talk to based upon
986 * the rp configured and the source address
987 *
988 * If we have don't have a RP configured and the source address is *
989 * then set the upstream addr as INADDR_ANY and return failure.
990 *
991 */
992 int pim_rp_set_upstream_addr(struct pim_instance *pim, struct in_addr *up,
993 struct in_addr source, struct in_addr group)
994 {
995 struct rp_info *rp_info;
996 struct prefix g;
997
998 memset(&g, 0, sizeof(g));
999 g.family = AF_INET;
1000 g.prefixlen = 32;
1001 g.u.prefix4 = group;
1002
1003 rp_info = pim_rp_find_match_group(pim, &g);
1004
1005 if ((pim_rpf_addr_is_inaddr_none(&rp_info->rp))
1006 && (source.s_addr == INADDR_ANY)) {
1007 if (PIM_DEBUG_PIM_NHT_RP)
1008 zlog_debug("%s: Received a (*,G) with no RP configured",
1009 __PRETTY_FUNCTION__);
1010 up->s_addr = INADDR_ANY;
1011 return 0;
1012 }
1013
1014 *up = (source.s_addr == INADDR_ANY) ? rp_info->rp.rpf_addr.u.prefix4
1015 : source;
1016
1017 return 1;
1018 }
1019
1020 int pim_rp_config_write(struct pim_instance *pim, struct vty *vty,
1021 const char *spaces)
1022 {
1023 struct listnode *node;
1024 struct rp_info *rp_info;
1025 char rp_buffer[32];
1026 char group_buffer[32];
1027 int count = 0;
1028
1029 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
1030 if (pim_rpf_addr_is_inaddr_none(&rp_info->rp))
1031 continue;
1032
1033 if (rp_info->plist)
1034 vty_out(vty, "%sip pim rp %s prefix-list %s\n", spaces,
1035 inet_ntop(AF_INET,
1036 &rp_info->rp.rpf_addr.u.prefix4,
1037 rp_buffer, 32),
1038 rp_info->plist);
1039 else
1040 vty_out(vty, "%sip pim rp %s %s\n", spaces,
1041 inet_ntop(AF_INET,
1042 &rp_info->rp.rpf_addr.u.prefix4,
1043 rp_buffer, 32),
1044 prefix2str(&rp_info->group, group_buffer, 32));
1045 count++;
1046 }
1047
1048 return count;
1049 }
1050
1051 bool pim_rp_check_is_my_ip_address(struct pim_instance *pim,
1052 struct in_addr dest_addr)
1053 {
1054 if (if_lookup_exact_address(&dest_addr, AF_INET, pim->vrf_id))
1055 return true;
1056
1057 return false;
1058 }
1059
1060 void pim_rp_show_information(struct pim_instance *pim, struct vty *vty, bool uj)
1061 {
1062 struct rp_info *rp_info;
1063 struct rp_info *prev_rp_info = NULL;
1064 struct listnode *node;
1065
1066 json_object *json = NULL;
1067 json_object *json_rp_rows = NULL;
1068 json_object *json_row = NULL;
1069
1070 if (uj)
1071 json = json_object_new_object();
1072 else
1073 vty_out(vty,
1074 "RP address group/prefix-list OIF I am RP\n");
1075
1076 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
1077 if (!pim_rpf_addr_is_inaddr_none(&rp_info->rp)) {
1078 char buf[48];
1079
1080 if (uj) {
1081 /*
1082 * If we have moved on to a new RP then add the
1083 * entry for the previous RP
1084 */
1085 if (prev_rp_info
1086 && prev_rp_info->rp.rpf_addr.u.prefix4
1087 .s_addr
1088 != rp_info->rp.rpf_addr.u.prefix4
1089 .s_addr) {
1090 json_object_object_add(
1091 json,
1092 inet_ntoa(prev_rp_info->rp
1093 .rpf_addr.u
1094 .prefix4),
1095 json_rp_rows);
1096 json_rp_rows = NULL;
1097 }
1098
1099 if (!json_rp_rows)
1100 json_rp_rows = json_object_new_array();
1101
1102 json_row = json_object_new_object();
1103 if (rp_info->rp.source_nexthop.interface)
1104 json_object_string_add(
1105 json_row, "outboundInterface",
1106 rp_info->rp.source_nexthop
1107 .interface->name);
1108
1109 if (rp_info->i_am_rp)
1110 json_object_boolean_true_add(json_row,
1111 "iAmRP");
1112
1113 if (rp_info->plist)
1114 json_object_string_add(json_row,
1115 "prefixList",
1116 rp_info->plist);
1117 else
1118 json_object_string_add(
1119 json_row, "group",
1120 prefix2str(&rp_info->group, buf,
1121 48));
1122
1123 json_object_array_add(json_rp_rows, json_row);
1124 } else {
1125 vty_out(vty, "%-15s ",
1126 inet_ntoa(rp_info->rp.rpf_addr.u
1127 .prefix4));
1128
1129 if (rp_info->plist)
1130 vty_out(vty, "%-18s ", rp_info->plist);
1131 else
1132 vty_out(vty, "%-18s ",
1133 prefix2str(&rp_info->group, buf,
1134 48));
1135
1136 if (rp_info->rp.source_nexthop.interface)
1137 vty_out(vty, "%-16s ",
1138 rp_info->rp.source_nexthop
1139 .interface->name);
1140 else
1141 vty_out(vty, "%-16s ", "(Unknown)");
1142
1143 if (rp_info->i_am_rp)
1144 vty_out(vty, "yes\n");
1145 else
1146 vty_out(vty, "no\n");
1147 }
1148
1149 prev_rp_info = rp_info;
1150 }
1151 }
1152
1153 if (uj) {
1154 if (prev_rp_info && json_rp_rows)
1155 json_object_object_add(
1156 json,
1157 inet_ntoa(prev_rp_info->rp.rpf_addr.u.prefix4),
1158 json_rp_rows);
1159
1160 vty_out(vty, "%s\n", json_object_to_json_string_ext(
1161 json, JSON_C_TO_STRING_PRETTY));
1162 json_object_free(json);
1163 }
1164 }
1165
1166 void pim_resolve_rp_nh(struct pim_instance *pim, struct pim_neighbor *nbr)
1167 {
1168 struct listnode *node = NULL;
1169 struct rp_info *rp_info = NULL;
1170 struct nexthop *nh_node = NULL;
1171 struct prefix nht_p;
1172 struct pim_nexthop_cache pnc;
1173
1174 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
1175 if (rp_info->rp.rpf_addr.u.prefix4.s_addr == INADDR_NONE)
1176 continue;
1177
1178 nht_p.family = AF_INET;
1179 nht_p.prefixlen = IPV4_MAX_BITLEN;
1180 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
1181 memset(&pnc, 0, sizeof(struct pim_nexthop_cache));
1182 if (!pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info,
1183 &pnc))
1184 continue;
1185
1186 for (nh_node = pnc.nexthop; nh_node; nh_node = nh_node->next) {
1187 if (nh_node->gate.ipv4.s_addr != 0)
1188 continue;
1189
1190 struct interface *ifp1 = if_lookup_by_index(
1191 nh_node->ifindex, pim->vrf_id);
1192
1193 if (nbr->interface != ifp1)
1194 continue;
1195
1196 nh_node->gate.ipv4 = nbr->source_addr;
1197 if (PIM_DEBUG_PIM_NHT_RP) {
1198 char str[PREFIX_STRLEN];
1199 char str1[INET_ADDRSTRLEN];
1200 pim_inet4_dump("<nht_nbr?>", nbr->source_addr,
1201 str1, sizeof(str1));
1202 pim_addr_dump("<nht_addr?>", &nht_p, str,
1203 sizeof(str));
1204 zlog_debug(
1205 "%s: addr %s new nexthop addr %s interface %s",
1206 __PRETTY_FUNCTION__, str, str1,
1207 ifp1->name);
1208 }
1209 }
1210 }
1211 }