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