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