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