]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_rp.c
pimd: Make msdp `struct pim_instance *` aware
[mirror_frr.git] / pimd / pim_rp.c
CommitLineData
8f5f5e91
DS
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.
896014f4
DL
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
8f5f5e91
DS
19 */
20#include <zebra.h>
21
9bf3c633 22#include "lib/json.h"
54b97c74 23#include "log.h"
8f5f5e91 24#include "network.h"
744d91b3 25#include "if.h"
36d6bd7d
DS
26#include "linklist.h"
27#include "prefix.h"
28#include "memory.h"
00d07c6f
DS
29#include "vty.h"
30#include "vrf.h"
dfe43e25 31#include "plist.h"
cba44481 32#include "nexthop.h"
8f5f5e91
DS
33
34#include "pimd.h"
75a26779 35#include "pim_vty.h"
54b97c74 36#include "pim_str.h"
7176984f 37#include "pim_iface.h"
8f5f5e91 38#include "pim_rp.h"
ed66602c
DS
39#include "pim_str.h"
40#include "pim_rpf.h"
13afbd05 41#include "pim_sock.h"
36d6bd7d 42#include "pim_memory.h"
00d07c6f 43#include "pim_iface.h"
7667c556 44#include "pim_msdp.h"
1bc98276 45#include "pim_nht.h"
8f5f5e91 46
36d6bd7d 47
c2cf4b02
DS
48/* Cleanup pim->rpf_hash each node data */
49void pim_rp_list_hash_clean(void *data)
50{
51 struct pim_nexthop_cache *pnc;
52
53 pnc = (struct pim_nexthop_cache *)data;
54 if (pnc->rp_list->count)
55 list_delete_all_node(pnc->rp_list);
56 if (pnc->upstream_list->count)
57 list_delete_all_node(pnc->upstream_list);
58}
59
d62a17ae 60static void pim_rp_info_free(struct rp_info *rp_info)
36d6bd7d 61{
d62a17ae 62 XFREE(MTYPE_PIM_RP, rp_info);
36d6bd7d
DS
63}
64
d62a17ae 65int pim_rp_list_cmp(void *v1, void *v2)
36d6bd7d 66{
d62a17ae 67 struct rp_info *rp1 = (struct rp_info *)v1;
68 struct rp_info *rp2 = (struct rp_info *)v2;
69
70 /*
71 * Sort by RP IP address
72 */
73 if (rp1->rp.rpf_addr.u.prefix4.s_addr
74 < rp2->rp.rpf_addr.u.prefix4.s_addr)
75 return -1;
76
77 if (rp1->rp.rpf_addr.u.prefix4.s_addr
78 > rp2->rp.rpf_addr.u.prefix4.s_addr)
79 return 1;
80
81 /*
82 * Sort by group IP address
83 */
84 if (rp1->group.u.prefix4.s_addr < rp2->group.u.prefix4.s_addr)
85 return -1;
86
87 if (rp1->group.u.prefix4.s_addr > rp2->group.u.prefix4.s_addr)
88 return 1;
89
90 return 0;
36d6bd7d
DS
91}
92
d9c9a9ee 93void pim_rp_init(struct pim_instance *pim)
36d6bd7d 94{
d62a17ae 95 struct rp_info *rp_info;
36d6bd7d 96
d9c9a9ee
DS
97 pim->rp_list = list_new();
98 pim->rp_list->del = (void (*)(void *))pim_rp_info_free;
99 pim->rp_list->cmp = pim_rp_list_cmp;
36d6bd7d 100
d62a17ae 101 rp_info = XCALLOC(MTYPE_PIM_RP, sizeof(*rp_info));
36d6bd7d 102
d62a17ae 103 if (!rp_info)
104 return;
36d6bd7d 105
2e8345c1
DS
106 if (!str2prefix("224.0.0.0/4", &rp_info->group)) {
107 XFREE(MTYPE_PIM_RP, rp_info);
108 return;
109 }
d62a17ae 110 rp_info->group.family = AF_INET;
111 rp_info->rp.rpf_addr.family = AF_INET;
112 rp_info->rp.rpf_addr.prefixlen = IPV4_MAX_PREFIXLEN;
113 rp_info->rp.rpf_addr.u.prefix4.s_addr = INADDR_NONE;
36d6bd7d 114
d9c9a9ee 115 listnode_add(pim->rp_list, rp_info);
36d6bd7d
DS
116}
117
d9c9a9ee 118void pim_rp_free(struct pim_instance *pim)
36d6bd7d 119{
d9c9a9ee
DS
120 if (pim->rp_list)
121 list_delete(pim->rp_list);
122 pim->rp_list = NULL;
36d6bd7d
DS
123}
124
dfe43e25
DW
125/*
126 * Given an RP's prefix-list, return the RP's rp_info for that prefix-list
127 */
d9c9a9ee
DS
128static struct rp_info *pim_rp_find_prefix_list(struct pim_instance *pim,
129 struct in_addr rp,
d62a17ae 130 const char *plist)
36d6bd7d 131{
d62a17ae 132 struct listnode *node;
133 struct rp_info *rp_info;
134
d9c9a9ee 135 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 136 if (rp.s_addr == rp_info->rp.rpf_addr.u.prefix4.s_addr
137 && rp_info->plist && strcmp(rp_info->plist, plist) == 0) {
138 return rp_info;
139 }
140 }
141
142 return NULL;
36d6bd7d
DS
143}
144
dfe43e25
DW
145/*
146 * Return true if plist is used by any rp_info
147 */
d9c9a9ee 148static int pim_rp_prefix_list_used(struct pim_instance *pim, const char *plist)
dfe43e25 149{
d62a17ae 150 struct listnode *node;
151 struct rp_info *rp_info;
152
d9c9a9ee 153 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 154 if (rp_info->plist && strcmp(rp_info->plist, plist) == 0) {
155 return 1;
156 }
157 }
158
159 return 0;
dfe43e25
DW
160}
161
162/*
d62a17ae 163 * Given an RP's address, return the RP's rp_info that is an exact match for
164 * 'group'
dfe43e25 165 */
d9c9a9ee
DS
166static struct rp_info *pim_rp_find_exact(struct pim_instance *pim,
167 struct in_addr rp,
d62a17ae 168 struct prefix *group)
36d6bd7d 169{
d62a17ae 170 struct listnode *node;
171 struct rp_info *rp_info;
36d6bd7d 172
d9c9a9ee 173 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 174 if (rp.s_addr == rp_info->rp.rpf_addr.u.prefix4.s_addr
175 && prefix_same(&rp_info->group, group))
176 return rp_info;
177 }
54b97c74 178
d62a17ae 179 return NULL;
36d6bd7d
DS
180}
181
dfe43e25
DW
182/*
183 * Given a group, return the rp_info for that group
184 */
d9c9a9ee
DS
185static struct rp_info *pim_rp_find_match_group(struct pim_instance *pim,
186 struct prefix *group)
36d6bd7d 187{
d62a17ae 188 struct listnode *node;
189 struct rp_info *rp_info;
190 struct prefix_list *plist;
191
d9c9a9ee 192 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 193 if (rp_info->plist) {
194 plist = prefix_list_lookup(AFI_IP, rp_info->plist);
195
196 if (plist
197 && prefix_list_apply(plist, group) == PREFIX_PERMIT)
198 return rp_info;
199 } else {
200 if (prefix_match(&rp_info->group, group))
201 return rp_info;
202 }
203 }
204
205 return NULL;
36d6bd7d 206}
75a26779 207
dfe43e25
DW
208/*
209 * When the user makes "ip pim rp" configuration changes or if they change the
210 * prefix-list(s) used by these statements we must tickle the upstream state
211 * for each group to make them re-lookup who their RP should be.
212 *
213 * This is a placeholder function for now.
214 */
472ad383 215static void pim_rp_refresh_group_to_rp_mapping(struct pim_instance *pim)
dfe43e25 216{
472ad383 217 pim_msdp_i_am_rp_changed(pim);
dfe43e25
DW
218}
219
d9c9a9ee
DS
220void pim_rp_prefix_list_update(struct pim_instance *pim,
221 struct prefix_list *plist)
dfe43e25 222{
d62a17ae 223 struct listnode *node;
224 struct rp_info *rp_info;
225 int refresh_needed = 0;
226
d9c9a9ee 227 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 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)
472ad383 236 pim_rp_refresh_group_to_rp_mapping(pim);
dfe43e25
DW
237}
238
d62a17ae 239static int pim_rp_check_interface_addrs(struct rp_info *rp_info,
240 struct pim_interface *pim_ifp)
7176984f 241{
d62a17ae 242 struct listnode *node;
243 struct pim_secondary_addr *sec_addr;
7176984f 244
d62a17ae 245 if (pim_ifp->primary_address.s_addr
246 == rp_info->rp.rpf_addr.u.prefix4.s_addr)
247 return 1;
7176984f 248
d62a17ae 249 if (!pim_ifp->sec_addr_list) {
250 return 0;
251 }
7176984f 252
d62a17ae 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 }
7176984f 258
d62a17ae 259 return 0;
7176984f 260}
261
fec883d9
DS
262static void pim_rp_check_interfaces(struct pim_instance *pim,
263 struct rp_info *rp_info)
00d07c6f 264{
d62a17ae 265 struct listnode *node;
266 struct interface *ifp;
00d07c6f 267
d62a17ae 268 rp_info->i_am_rp = 0;
fec883d9 269 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(pim->vrf_id), node, ifp)) {
d62a17ae 270 struct pim_interface *pim_ifp = ifp->info;
00d07c6f 271
d62a17ae 272 if (!pim_ifp)
273 continue;
00d07c6f 274
d62a17ae 275 if (pim_rp_check_interface_addrs(rp_info, pim_ifp)) {
276 rp_info->i_am_rp = 1;
277 }
278 }
00d07c6f
DS
279}
280
fec883d9
DS
281int pim_rp_new(struct pim_instance *pim, const char *rp,
282 const char *group_range, const char *plist)
75a26779 283{
d62a17ae 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 */
d9c9a9ee 322 if (pim_rp_find_prefix_list(pim, rp_info->rp.rpf_addr.u.prefix4,
d62a17ae 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 */
d9c9a9ee 331 if (pim_rp_prefix_list_used(pim, plist)) {
d62a17ae 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 */
d9c9a9ee 339 for (ALL_LIST_ELEMENTS(pim->rp_list, node, nnode,
d62a17ae 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)
fec883d9 344 pim_rp_del(pim, rp, NULL,
d62a17ae 345 tmp_rp_info->plist);
346 else
347 pim_rp_del(
fec883d9 348 pim, rp,
d62a17ae 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 {
2e8345c1
DS
357 if (!str2prefix("224.0.0.0/4", &group_all)) {
358 XFREE(MTYPE_PIM_RP, rp_info);
359 return PIM_GROUP_BAD_ADDRESS;
360 }
d9c9a9ee 361 rp_all = pim_rp_find_match_group(pim, &group_all);
d62a17ae 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 */
d9c9a9ee 374 for (ALL_LIST_ELEMENTS(pim->rp_list, node, nnode,
d62a17ae 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) {
fec883d9 380 pim_rp_del(pim, rp, NULL, tmp_rp_info->plist);
d62a17ae 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));
fec883d9
DS
407 if (pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_all,
408 &pnc)) {
cb9c7c50 409 if (!pim_ecmp_nexthop_search(
fec883d9 410 pim, &pnc,
25b787a2
DS
411 &rp_all->rp.source_nexthop, &nht_p,
412 &rp_all->group, 1))
d62a17ae 413 return PIM_RP_NO_PATH;
414 } else {
415 if (pim_nexthop_lookup(
d9c9a9ee 416 pim, &rp_all->rp.source_nexthop,
d62a17ae 417 rp_all->rp.rpf_addr.u.prefix4, 1)
418 != 0)
419 return PIM_RP_NO_PATH;
420 }
fec883d9 421 pim_rp_check_interfaces(pim, rp_all);
472ad383 422 pim_rp_refresh_group_to_rp_mapping(pim);
d62a17ae 423 return PIM_SUCCESS;
424 }
425
426 /*
427 * Return if the group is already configured for this RP
428 */
d9c9a9ee 429 if (pim_rp_find_exact(pim, rp_info->rp.rpf_addr.u.prefix4,
d62a17ae 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 */
d9c9a9ee 438 tmp_rp_info = pim_rp_find_match_group(pim, &rp_info->group);
d62a17ae 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
d9c9a9ee 461 listnode_add_sort(pim->rp_list, rp_info);
d62a17ae 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));
fec883d9
DS
477 if (pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, &pnc)) {
478 if (!pim_ecmp_nexthop_search(pim, &pnc,
25b787a2 479 &rp_info->rp.source_nexthop,
cb9c7c50 480 &nht_p, &rp_info->group, 1))
d62a17ae 481 return PIM_RP_NO_PATH;
482 } else {
d9c9a9ee 483 if (pim_nexthop_lookup(pim, &rp_info->rp.source_nexthop,
d62a17ae 484 rp_info->rp.rpf_addr.u.prefix4, 1)
485 != 0)
486 return PIM_RP_NO_PATH;
487 }
488
fec883d9 489 pim_rp_check_interfaces(pim, rp_info);
472ad383 490 pim_rp_refresh_group_to_rp_mapping(pim);
d62a17ae 491 return PIM_SUCCESS;
75a26779
DS
492}
493
fec883d9
DS
494int pim_rp_del(struct pim_instance *pim, const char *rp,
495 const char *group_range, const char *plist)
75a26779 496{
d62a17ae 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)
d9c9a9ee 518 rp_info = pim_rp_find_prefix_list(pim, rp_addr, plist);
d62a17ae 519 else
d9c9a9ee 520 rp_info = pim_rp_find_exact(pim, rp_addr, &group);
d62a17ae 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 }
fec883d9 540 pim_delete_tracked_nexthop(pim, &nht_p, NULL, rp_info);
d62a17ae 541
542 str2prefix("224.0.0.0/4", &g_all);
d9c9a9ee 543 rp_all = pim_rp_find_match_group(pim, &g_all);
d62a17ae 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
d9c9a9ee 552 listnode_delete(pim->rp_list, rp_info);
472ad383 553 pim_rp_refresh_group_to_rp_mapping(pim);
d62a17ae 554 return PIM_SUCCESS;
75a26779 555}
13afbd05 556
fec883d9 557void pim_rp_setup(struct pim_instance *pim)
13afbd05 558{
d62a17ae 559 struct listnode *node;
560 struct rp_info *rp_info;
d62a17ae 561 struct prefix nht_p;
562 struct pim_nexthop_cache pnc;
563
d9c9a9ee 564 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 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));
fec883d9
DS
572 if (pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, &pnc))
573 pim_ecmp_nexthop_search(pim, &pnc,
cb9c7c50
DS
574 &rp_info->rp.source_nexthop,
575 &nht_p, &rp_info->group, 1);
576 else {
d62a17ae 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 }
d9c9a9ee
DS
584 if (!pim_nexthop_lookup(
585 pim, &rp_info->rp.source_nexthop,
586 rp_info->rp.rpf_addr.u.prefix4, 1))
d62a17ae 587 if (PIM_DEBUG_PIM_TRACE)
588 zlog_debug(
589 "Unable to lookup nexthop for rp specified");
d62a17ae 590 }
591 }
13afbd05
DS
592}
593
54b97c74 594/*
7176984f 595 * Checks to see if we should elect ourself the actual RP when new if
596 * addresses are added against an interface.
54b97c74 597 */
d62a17ae 598void pim_rp_check_on_if_add(struct pim_interface *pim_ifp)
54b97c74 599{
d62a17ae 600 struct listnode *node;
601 struct rp_info *rp_info;
602 bool i_am_rp_changed = false;
d9c9a9ee 603 struct pim_instance *pim = pim_ifp->pim;
d62a17ae 604
d9c9a9ee 605 if (pim->rp_list == NULL)
d62a17ae 606 return;
607
d9c9a9ee 608 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 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) {
472ad383 632 pim_msdp_i_am_rp_changed(pim);
d62a17ae 633 }
7176984f 634}
36d6bd7d 635
7176984f 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. */
fec883d9 639void pim_i_am_rp_re_evaluate(struct pim_instance *pim)
7176984f 640{
d62a17ae 641 struct listnode *node;
642 struct rp_info *rp_info;
643 bool i_am_rp_changed = false;
644 int old_i_am_rp;
645
d9c9a9ee 646 if (pim->rp_list == NULL)
d62a17ae 647 return;
648
d9c9a9ee 649 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 650 if (pim_rpf_addr_is_inaddr_none(&rp_info->rp))
651 continue;
652
653 old_i_am_rp = rp_info->i_am_rp;
fec883d9 654 pim_rp_check_interfaces(pim, rp_info);
d62a17ae 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) {
472ad383 674 pim_msdp_i_am_rp_changed(pim);
d62a17ae 675 }
54b97c74
DS
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 */
d9c9a9ee 684int pim_rp_i_am_rp(struct pim_instance *pim, struct in_addr group)
54b97c74 685{
d62a17ae 686 struct prefix g;
687 struct rp_info *rp_info;
36d6bd7d 688
d62a17ae 689 memset(&g, 0, sizeof(g));
690 g.family = AF_INET;
691 g.prefixlen = 32;
692 g.u.prefix4 = group;
36d6bd7d 693
d9c9a9ee 694 rp_info = pim_rp_find_match_group(pim, &g);
36d6bd7d 695
d62a17ae 696 if (rp_info)
697 return rp_info->i_am_rp;
36d6bd7d 698
d62a17ae 699 return 0;
54b97c74
DS
700}
701
71694057
DS
702/*
703 * RP(G)
704 *
705 * Return the RP that the Group belongs too.
706 */
fec883d9 707struct pim_rpf *pim_rp_g(struct pim_instance *pim, struct in_addr group)
71694057 708{
d62a17ae 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
d9c9a9ee 717 rp_info = pim_rp_find_match_group(pim, &g);
d62a17ae 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));
fec883d9
DS
736 if (pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, &pnc))
737 pim_ecmp_nexthop_search(pim, &pnc,
d62a17ae 738 &rp_info->rp.source_nexthop,
739 &nht_p, &rp_info->group, 1);
cb9c7c50 740 else {
d62a17ae 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();
d9c9a9ee 751 pim_nexthop_lookup(pim, &rp_info->rp.source_nexthop,
d62a17ae 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;
71694057
DS
759}
760
8f5f5e91
DS
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 */
d9c9a9ee
DS
769int pim_rp_set_upstream_addr(struct pim_instance *pim, struct in_addr *up,
770 struct in_addr source, struct in_addr group)
8f5f5e91 771{
d62a17ae 772 struct rp_info *rp_info;
773 struct prefix g;
36d6bd7d 774
d62a17ae 775 memset(&g, 0, sizeof(g));
776 g.family = AF_INET;
777 g.prefixlen = 32;
778 g.u.prefix4 = group;
36d6bd7d 779
d9c9a9ee 780 rp_info = pim_rp_find_match_group(pim, &g);
36d6bd7d 781
d62a17ae 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 }
8f5f5e91 789
d62a17ae 790 *up = (source.s_addr == INADDR_ANY) ? rp_info->rp.rpf_addr.u.prefix4
791 : source;
8f5f5e91 792
d62a17ae 793 return 1;
8f5f5e91 794}
75a26779 795
d9c9a9ee 796int pim_rp_config_write(struct pim_instance *pim, struct vty *vty)
75a26779 797{
d62a17ae 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
d9c9a9ee 804 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 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;
75a26779
DS
824}
825
fec883d9
DS
826int pim_rp_check_is_my_ip_address(struct pim_instance *pim,
827 struct in_addr group,
d62a17ae 828 struct in_addr dest_addr)
75a26779 829{
d62a17ae 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
d9c9a9ee 838 rp_info = pim_rp_find_match_group(pim, &g);
d62a17ae 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 */
d9c9a9ee 845 if (I_am_RP(pim, group)) {
d62a17ae 846 if (dest_addr.s_addr == rp_info->rp.rpf_addr.u.prefix4.s_addr)
847 return 1;
848 }
849
fec883d9 850 if (if_lookup_exact_address(&dest_addr, AF_INET, pim->vrf_id))
d62a17ae 851 return 1;
852
853 return 0;
75a26779 854}
00d07c6f 855
d9c9a9ee
DS
856void pim_rp_show_information(struct pim_instance *pim, struct vty *vty,
857 u_char uj)
00d07c6f 858{
d62a17ae 859 struct rp_info *rp_info;
860 struct rp_info *prev_rp_info = NULL;
861 struct listnode *node;
862
863 json_object *json = NULL;
864 json_object *json_rp_rows = NULL;
865 json_object *json_row = NULL;
866
867 if (uj)
868 json = json_object_new_object();
869 else
870 vty_out(vty,
871 "RP address group/prefix-list OIF I am RP\n");
872
d9c9a9ee 873 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 874 if (!pim_rpf_addr_is_inaddr_none(&rp_info->rp)) {
875 char buf[48];
876
877 if (uj) {
878 /*
879 * If we have moved on to a new RP then add the
880 * entry for the previous RP
881 */
882 if (prev_rp_info
883 && prev_rp_info->rp.rpf_addr.u.prefix4
884 .s_addr
885 != rp_info->rp.rpf_addr.u.prefix4
886 .s_addr) {
887 json_object_object_add(
888 json,
889 inet_ntoa(prev_rp_info->rp
890 .rpf_addr.u
891 .prefix4),
892 json_rp_rows);
893 json_rp_rows = NULL;
894 }
895
896 if (!json_rp_rows)
897 json_rp_rows = json_object_new_array();
898
899 json_row = json_object_new_object();
900 if (rp_info->rp.source_nexthop.interface)
901 json_object_string_add(
902 json_row, "outboundInterface",
903 rp_info->rp.source_nexthop
904 .interface->name);
905
906 if (rp_info->i_am_rp)
907 json_object_boolean_true_add(json_row,
908 "iAmRP");
909
910 if (rp_info->plist)
911 json_object_string_add(json_row,
912 "prefixList",
913 rp_info->plist);
914 else
915 json_object_string_add(
916 json_row, "group",
917 prefix2str(&rp_info->group, buf,
918 48));
919
920 json_object_array_add(json_rp_rows, json_row);
921 } else {
922 vty_out(vty, "%-15s ",
923 inet_ntoa(rp_info->rp.rpf_addr.u
924 .prefix4));
925
926 if (rp_info->plist)
927 vty_out(vty, "%-18s ", rp_info->plist);
928 else
929 vty_out(vty, "%-18s ",
930 prefix2str(&rp_info->group, buf,
931 48));
932
933 if (rp_info->rp.source_nexthop.interface)
934 vty_out(vty, "%-10s ",
935 rp_info->rp.source_nexthop
936 .interface->name);
937 else
938 vty_out(vty, "%-10s ", "(Unknown)");
939
940 if (rp_info->i_am_rp)
941 vty_out(vty, "yes\n");
942 else
943 vty_out(vty, "no\n");
944 }
945
946 prev_rp_info = rp_info;
947 }
948 }
949
950 if (uj) {
951 if (prev_rp_info && json_rp_rows)
952 json_object_object_add(
953 json,
954 inet_ntoa(prev_rp_info->rp.rpf_addr.u.prefix4),
955 json_rp_rows);
956
9d303b37
DL
957 vty_out(vty, "%s\n", json_object_to_json_string_ext(
958 json, JSON_C_TO_STRING_PRETTY));
d62a17ae 959 json_object_free(json);
960 }
00d07c6f 961}
cba44481 962
fec883d9 963void pim_resolve_rp_nh(struct pim_instance *pim)
cba44481 964{
d62a17ae 965 struct listnode *node = NULL;
966 struct rp_info *rp_info = NULL;
967 struct nexthop *nh_node = NULL;
968 struct prefix nht_p;
969 struct pim_nexthop_cache pnc;
970 struct pim_neighbor *nbr = NULL;
971
d9c9a9ee 972 for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) {
d62a17ae 973 if (rp_info->rp.rpf_addr.u.prefix4.s_addr == INADDR_NONE)
974 continue;
975
976 nht_p.family = AF_INET;
977 nht_p.prefixlen = IPV4_MAX_BITLEN;
978 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
979 memset(&pnc, 0, sizeof(struct pim_nexthop_cache));
fec883d9 980 if (!pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info,
25bdac42 981 &pnc))
cb9c7c50
DS
982 continue;
983
984 for (nh_node = pnc.nexthop; nh_node; nh_node = nh_node->next) {
985 if (nh_node->gate.ipv4.s_addr != 0)
986 continue;
987
988 struct interface *ifp1 = if_lookup_by_index(
fec883d9 989 nh_node->ifindex, pim->vrf_id);
cb9c7c50
DS
990 nbr = pim_neighbor_find_if(ifp1);
991 if (!nbr)
992 continue;
993
994 nh_node->gate.ipv4 = nbr->source_addr;
995 if (PIM_DEBUG_TRACE) {
996 char str[PREFIX_STRLEN];
997 char str1[INET_ADDRSTRLEN];
998 pim_inet4_dump("<nht_nbr?>", nbr->source_addr,
999 str1, sizeof(str1));
1000 pim_addr_dump("<nht_addr?>", &nht_p, str,
1001 sizeof(str));
1002 zlog_debug(
1003 "%s: addr %s new nexthop addr %s interface %s",
1004 __PRETTY_FUNCTION__, str, str1,
1005 ifp1->name);
d62a17ae 1006 }
1007 }
1008 }
cba44481 1009}