]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_bsm.c
pimd: Moving igmp "show running-config" to a function
[mirror_frr.git] / pimd / pim_bsm.c
CommitLineData
361b5843 1/*
2 * pim_bsm.c: PIM BSM handling routines
3 *
4 * Copyright (C) 2018-19 Vmware, Inc.
5 * Saravanan K
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301 USA
21 */
2618a52e
DL
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
361b5843 27#include "if.h"
28#include "pimd.h"
29#include "pim_iface.h"
361b5843 30#include "pim_instance.h"
e34e07e6 31#include "pim_neighbor.h"
361b5843 32#include "pim_rpf.h"
33#include "pim_hello.h"
34#include "pim_pim.h"
35#include "pim_nht.h"
36#include "pim_bsm.h"
37#include "pim_time.h"
62596d9a 38#include "pim_zebra.h"
361b5843 39
40/* Functions forward declaration */
41static void pim_bs_timer_start(struct bsm_scope *scope, int bs_timeout);
a5164e97 42static void pim_g2rp_timer_start(struct bsm_rpinfo *bsrp, int hold_time);
43static inline void pim_g2rp_timer_restart(struct bsm_rpinfo *bsrp,
44 int hold_time);
23255dfd 45
46/* Memory Types */
bf8d3d6a 47DEFINE_MTYPE_STATIC(PIMD, PIM_BSGRP_NODE, "PIM BSR advertised grp info");
fdab2940 48DEFINE_MTYPE_STATIC(PIMD, PIM_BSRP_INFO, "PIM BSR advertised RP info");
d4a4525c 49DEFINE_MTYPE_STATIC(PIMD, PIM_BSM_FRAG, "PIM BSM fragment");
bf8d3d6a 50DEFINE_MTYPE_STATIC(PIMD, PIM_BSM_PKT_VAR_MEM, "PIM BSM Packet");
361b5843 51
79d97386 52/* All bsm packets forwarded shall be fit within ip mtu less iphdr(max) */
53#define MAX_IP_HDR_LEN 24
54
16c926c8 55/* pim_bsm_write_config - Write the interface pim bsm configuration.*/
d0e418b4 56void pim_bsm_write_config(struct vty *vty, struct interface *ifp)
16c926c8 57{
58 struct pim_interface *pim_ifp = ifp->info;
59
60 if (pim_ifp) {
61 if (!pim_ifp->bsm_enable)
62 vty_out(vty, " no ip pim bsm\n");
63 if (!pim_ifp->ucast_bsm_accept)
64 vty_out(vty, " no ip pim unicast-bsm\n");
65 }
66}
67
fdab2940
DL
68static void pim_bsm_rpinfo_free(struct bsm_rpinfo *bsrp_info)
69{
70 THREAD_OFF(bsrp_info->g2rp_timer);
71 XFREE(MTYPE_PIM_BSRP_INFO, bsrp_info);
72}
73
b09bd804 74static void pim_bsm_rpinfos_free(struct bsm_rpinfos_head *head)
fdab2940
DL
75{
76 struct bsm_rpinfo *bsrp_info;
77
78 while ((bsrp_info = bsm_rpinfos_pop(head)))
79 pim_bsm_rpinfo_free(bsrp_info);
80}
81
b09bd804 82static void pim_free_bsgrp_data(struct bsgrp_node *bsgrp_node)
361b5843 83{
fdab2940
DL
84 pim_bsm_rpinfos_free(bsgrp_node->bsrp_list);
85 pim_bsm_rpinfos_free(bsgrp_node->partial_bsrp_list);
361b5843 86 XFREE(MTYPE_PIM_BSGRP_NODE, bsgrp_node);
87}
88
b09bd804 89static void pim_free_bsgrp_node(struct route_table *rt, struct prefix *grp)
c295e391 90{
91 struct route_node *rn;
92
93 rn = route_node_lookup(rt, grp);
94 if (rn) {
95 rn->info = NULL;
96 route_unlock_node(rn);
97 route_unlock_node(rn);
98 }
99}
100
d4a4525c 101static void pim_bsm_frag_free(struct bsm_frag *bsfrag)
361b5843 102{
d4a4525c
DL
103 XFREE(MTYPE_PIM_BSM_FRAG, bsfrag);
104}
105
b09bd804 106static void pim_bsm_frags_free(struct bsm_scope *scope)
d4a4525c
DL
107{
108 struct bsm_frag *bsfrag;
109
110 while ((bsfrag = bsm_frags_pop(scope->bsm_frags)))
111 pim_bsm_frag_free(bsfrag);
361b5843 112}
113
fdab2940
DL
114int pim_bsm_rpinfo_cmp(const struct bsm_rpinfo *node1,
115 const struct bsm_rpinfo *node2)
7b3e6ba1 116{
117 /* RP election Algo :
118 * Step-1 : Loweset Rp priority will have higher precedance.
119 * Step-2 : If priority same then higher hash val will have
120 * higher precedance.
121 * Step-3 : If Hash val is same then highest rp address will
122 * become elected RP.
123 */
124 if (node1->rp_prio < node2->rp_prio)
125 return -1;
126 if (node1->rp_prio > node2->rp_prio)
127 return 1;
128 if (node1->hash < node2->hash)
129 return 1;
130 if (node1->hash > node2->hash)
131 return -1;
132 if (node1->rp_address.s_addr < node2->rp_address.s_addr)
133 return 1;
134 if (node1->rp_address.s_addr > node2->rp_address.s_addr)
135 return -1;
136 return 0;
137}
138
7b3e6ba1 139static struct bsgrp_node *pim_bsm_new_bsgrp_node(struct route_table *rt,
140 struct prefix *grp)
141{
142 struct route_node *rn;
143 struct bsgrp_node *bsgrp;
144
145 rn = route_node_get(rt, grp);
146 if (!rn) {
15569c58 147 zlog_warn("%s: route node creation failed", __func__);
7b3e6ba1 148 return NULL;
149 }
150 bsgrp = XCALLOC(MTYPE_PIM_BSGRP_NODE, sizeof(struct bsgrp_node));
151
7b3e6ba1 152 rn->info = bsgrp;
fdab2940
DL
153 bsm_rpinfos_init(bsgrp->bsrp_list);
154 bsm_rpinfos_init(bsgrp->partial_bsrp_list);
7b3e6ba1 155
156 prefix_copy(&bsgrp->group, grp);
157 return bsgrp;
158}
159
cc9f21da 160static void pim_on_bs_timer(struct thread *t)
23255dfd 161{
c843f56d 162 struct route_node *rn;
163 struct bsm_scope *scope;
164 struct bsgrp_node *bsgrp_node;
165 struct bsm_rpinfo *bsrp;
c843f56d 166
167 scope = THREAD_ARG(t);
168 THREAD_OFF(scope->bs_timer);
169
170 if (PIM_DEBUG_BSM)
171 zlog_debug("%s: Bootstrap Timer expired for scope: %d",
15569c58 172 __func__, scope->sz_id);
c843f56d 173
5f010b12 174 pim_nht_bsr_del(scope->pim, scope->current_bsr);
c843f56d 175
176 /* Reset scope zone data */
177 scope->accept_nofwd_bsm = false;
178 scope->state = ACCEPT_ANY;
179 scope->current_bsr.s_addr = INADDR_ANY;
180 scope->current_bsr_prio = 0;
181 scope->current_bsr_first_ts = 0;
182 scope->current_bsr_last_ts = 0;
183 scope->bsm_frag_tag = 0;
d4a4525c 184 pim_bsm_frags_free(scope);
c843f56d 185
186 for (rn = route_top(scope->bsrp_table); rn; rn = route_next(rn)) {
187
188 bsgrp_node = (struct bsgrp_node *)rn->info;
189 if (!bsgrp_node) {
190 if (PIM_DEBUG_BSM)
15569c58 191 zlog_debug("%s: bsgrp_node is null", __func__);
c843f56d 192 continue;
193 }
194 /* Give grace time for rp to continue for another hold time */
fdab2940
DL
195 bsrp = bsm_rpinfos_first(bsgrp_node->bsrp_list);
196 if (bsrp)
c843f56d 197 pim_g2rp_timer_restart(bsrp, bsrp->rp_holdtime);
fdab2940 198
c843f56d 199 /* clear pending list */
fdab2940
DL
200 pim_bsm_rpinfos_free(bsgrp_node->partial_bsrp_list);
201 bsgrp_node->pend_rp_cnt = 0;
c843f56d 202 }
23255dfd 203}
204
b09bd804 205static void pim_bs_timer_stop(struct bsm_scope *scope)
23255dfd 206{
207 if (PIM_DEBUG_BSM)
15569c58
DA
208 zlog_debug("%s : BS timer being stopped of sz: %d", __func__,
209 scope->sz_id);
23255dfd 210 THREAD_OFF(scope->bs_timer);
211}
212
213static void pim_bs_timer_start(struct bsm_scope *scope, int bs_timeout)
214{
215 if (!scope) {
216 if (PIM_DEBUG_BSM)
15569c58 217 zlog_debug("%s : Invalid scope(NULL).", __func__);
23255dfd 218 return;
219 }
220 THREAD_OFF(scope->bs_timer);
221 if (PIM_DEBUG_BSM)
15569c58
DA
222 zlog_debug(
223 "%s : starting bs timer for scope %d with timeout %d secs",
224 __func__, scope->sz_id, bs_timeout);
23255dfd 225 thread_add_timer(router->master, pim_on_bs_timer, scope, bs_timeout,
226 &scope->bs_timer);
227}
228
5acde1cf 229static inline void pim_bs_timer_restart(struct bsm_scope *scope, int bs_timeout)
230{
231 pim_bs_timer_start(scope, bs_timeout);
232}
233
361b5843 234void pim_bsm_proc_init(struct pim_instance *pim)
235{
236 memset(&pim->global_scope, 0, sizeof(struct bsm_scope));
237
238 pim->global_scope.sz_id = PIM_GBL_SZ_ID;
239 pim->global_scope.bsrp_table = route_table_init();
240 pim->global_scope.accept_nofwd_bsm = true;
241 pim->global_scope.state = NO_INFO;
242 pim->global_scope.pim = pim;
d4a4525c 243 bsm_frags_init(pim->global_scope.bsm_frags);
361b5843 244 pim_bs_timer_start(&pim->global_scope, PIM_BS_TIME);
245}
246
247void pim_bsm_proc_free(struct pim_instance *pim)
248{
249 struct route_node *rn;
250 struct bsgrp_node *bsgrp;
251
252 pim_bs_timer_stop(&pim->global_scope);
d4a4525c 253 pim_bsm_frags_free(&pim->global_scope);
361b5843 254
d0e418b4 255 for (rn = route_top(pim->global_scope.bsrp_table); rn;
256 rn = route_next(rn)) {
361b5843 257 bsgrp = rn->info;
258 if (!bsgrp)
259 continue;
260 pim_free_bsgrp_data(bsgrp);
261 }
262
4d19a911 263 route_table_finish(pim->global_scope.bsrp_table);
361b5843 264}
265
c295e391 266static bool is_hold_time_elapsed(void *data)
267{
268 struct bsm_rpinfo *bsrp;
269
270 bsrp = data;
271
272 if (bsrp->elapse_time < bsrp->rp_holdtime)
273 return false;
274 else
275 return true;
276}
277
cc9f21da 278static void pim_on_g2rp_timer(struct thread *t)
a5164e97 279{
c295e391 280 struct bsm_rpinfo *bsrp;
281 struct bsm_rpinfo *bsrp_node;
282 struct bsgrp_node *bsgrp_node;
c295e391 283 struct pim_instance *pim;
284 struct rp_info *rp_info;
285 struct route_node *rn;
286 uint16_t elapse;
287 struct in_addr bsrp_addr;
288
289 bsrp = THREAD_ARG(t);
290 THREAD_OFF(bsrp->g2rp_timer);
291 bsgrp_node = bsrp->bsgrp_node;
292
293 /* elapse time is the hold time of expired node */
294 elapse = bsrp->rp_holdtime;
295 bsrp_addr = bsrp->rp_address;
296
297 /* update elapse for all bsrp nodes */
fdab2940 298 frr_each_safe (bsm_rpinfos, bsgrp_node->bsrp_list, bsrp_node) {
c295e391 299 bsrp_node->elapse_time += elapse;
300
fdab2940
DL
301 if (is_hold_time_elapsed(bsrp_node)) {
302 bsm_rpinfos_del(bsgrp_node->bsrp_list, bsrp_node);
303 pim_bsm_rpinfo_free(bsrp_node);
304 }
305 }
c295e391 306
307 /* Get the next elected rp node */
fdab2940 308 bsrp = bsm_rpinfos_first(bsgrp_node->bsrp_list);
c295e391 309 pim = bsgrp_node->scope->pim;
310 rn = route_node_lookup(pim->rp_table, &bsgrp_node->group);
311
312 if (!rn) {
5e81f5dd 313 zlog_warn("%s: Route node doesn't exist", __func__);
cc9f21da 314 return;
c295e391 315 }
316
317 rp_info = (struct rp_info *)rn->info;
318
319 if (!rp_info) {
320 route_unlock_node(rn);
cc9f21da 321 return;
c295e391 322 }
323
324 if (rp_info->rp_src != RP_SRC_STATIC) {
325 /* If new rp available, change it else delete the existing */
326 if (bsrp) {
327 bsrp_addr = bsrp->rp_address;
328 pim_g2rp_timer_start(
329 bsrp, (bsrp->rp_holdtime - bsrp->elapse_time));
330 pim_rp_change(pim, bsrp_addr, bsgrp_node->group,
331 RP_SRC_BSR);
332 } else {
333 pim_rp_del(pim, bsrp_addr, bsgrp_node->group, NULL,
334 RP_SRC_BSR);
335 }
336 }
337
fdab2940
DL
338 if (!bsm_rpinfos_count(bsgrp_node->bsrp_list)
339 && !bsm_rpinfos_count(bsgrp_node->partial_bsrp_list)) {
c295e391 340 pim_free_bsgrp_node(pim->global_scope.bsrp_table,
341 &bsgrp_node->group);
342 pim_free_bsgrp_data(bsgrp_node);
343 }
a5164e97 344}
345
346static void pim_g2rp_timer_start(struct bsm_rpinfo *bsrp, int hold_time)
347{
348 if (!bsrp) {
349 if (PIM_DEBUG_BSM)
15569c58 350 zlog_debug("%s : Invalid brsp(NULL).", __func__);
a5164e97 351 return;
352 }
353 THREAD_OFF(bsrp->g2rp_timer);
2dbe669b 354 if (PIM_DEBUG_BSM)
a5164e97 355 zlog_debug(
ee2bbf7c 356 "%s : starting g2rp timer for grp: %pFX - rp: %pI4 with timeout %d secs(Actual Hold time : %d secs)",
2dbe669b 357 __func__, &bsrp->bsgrp_node->group,
ee2bbf7c 358 &bsrp->rp_address, hold_time,
a5164e97 359 bsrp->rp_holdtime);
a5164e97 360
361 thread_add_timer(router->master, pim_on_g2rp_timer, bsrp, hold_time,
362 &bsrp->g2rp_timer);
363}
364
365static inline void pim_g2rp_timer_restart(struct bsm_rpinfo *bsrp,
366 int hold_time)
367{
368 pim_g2rp_timer_start(bsrp, hold_time);
369}
370
20fd30e5 371static void pim_g2rp_timer_stop(struct bsm_rpinfo *bsrp)
372{
373 if (!bsrp)
374 return;
375
2dbe669b 376 if (PIM_DEBUG_BSM)
ee2bbf7c 377 zlog_debug("%s : stopping g2rp timer for grp: %pFX - rp: %pI4",
2dbe669b 378 __func__, &bsrp->bsgrp_node->group,
ee2bbf7c 379 &bsrp->rp_address);
20fd30e5 380
381 THREAD_OFF(bsrp->g2rp_timer);
382}
383
384static bool is_hold_time_zero(void *data)
385{
386 struct bsm_rpinfo *bsrp;
387
388 bsrp = data;
389
390 if (bsrp->rp_holdtime)
391 return false;
392 else
393 return true;
394}
395
396static void pim_instate_pend_list(struct bsgrp_node *bsgrp_node)
397{
398 struct bsm_rpinfo *active;
399 struct bsm_rpinfo *pend;
20fd30e5 400 struct rp_info *rp_info;
401 struct route_node *rn;
402 struct pim_instance *pim;
403 struct rp_info *rp_all;
404 struct prefix group_all;
405 bool had_rp_node = true;
406
407 pim = bsgrp_node->scope->pim;
fdab2940 408 active = bsm_rpinfos_first(bsgrp_node->bsrp_list);
20fd30e5 409
410 /* Remove nodes with hold time 0 & check if list still has a head */
5f1808ac
DS
411 frr_each_safe (bsm_rpinfos, bsgrp_node->partial_bsrp_list, pend) {
412 if (is_hold_time_zero(pend)) {
fdab2940 413 bsm_rpinfos_del(bsgrp_node->partial_bsrp_list, pend);
5f1808ac
DS
414 pim_bsm_rpinfo_free(pend);
415 }
416 }
fdab2940
DL
417
418 pend = bsm_rpinfos_first(bsgrp_node->partial_bsrp_list);
20fd30e5 419
420 if (!str2prefix("224.0.0.0/4", &group_all))
421 return;
422
423 rp_all = pim_rp_find_match_group(pim, &group_all);
424 rn = route_node_lookup(pim->rp_table, &bsgrp_node->group);
425
426 if (pend)
427 pim_g2rp_timer_start(pend, pend->rp_holdtime);
428
429 /* if rp node doesn't exist or exist but not configured(rp_all),
430 * install the rp from head(if exists) of partial list. List is
431 * is sorted such that head is the elected RP for the group.
432 */
433 if (!rn || (prefix_same(&rp_all->group, &bsgrp_node->group)
434 && pim_rpf_addr_is_inaddr_none(&rp_all->rp))) {
435 if (PIM_DEBUG_BSM)
15569c58 436 zlog_debug("%s: Route node doesn't exist", __func__);
20fd30e5 437 if (pend)
438 pim_rp_new(pim, pend->rp_address, bsgrp_node->group,
439 NULL, RP_SRC_BSR);
440 had_rp_node = false;
441 } else {
442 rp_info = (struct rp_info *)rn->info;
443 if (!rp_info) {
444 route_unlock_node(rn);
445 if (pend)
446 pim_rp_new(pim, pend->rp_address,
447 bsgrp_node->group, NULL, RP_SRC_BSR);
448 had_rp_node = false;
449 }
450 }
451
452 /* We didn't have rp node and pending list is empty(unlikely), cleanup*/
453 if ((!had_rp_node) && (!pend)) {
454 pim_free_bsgrp_node(bsgrp_node->scope->bsrp_table,
455 &bsgrp_node->group);
456 pim_free_bsgrp_data(bsgrp_node);
457 return;
458 }
459
460 if ((had_rp_node) && (rp_info->rp_src != RP_SRC_STATIC)) {
461 /* This means we searched and got rp node, needs unlock */
462 route_unlock_node(rn);
463
464 if (active && pend) {
465 if ((active->rp_address.s_addr
466 != pend->rp_address.s_addr))
467 pim_rp_change(pim, pend->rp_address,
468 bsgrp_node->group, RP_SRC_BSR);
469 }
470
471 /* Possible when the first BSM has group with 0 rp count */
472 if ((!active) && (!pend)) {
473 if (PIM_DEBUG_BSM) {
474 zlog_debug(
475 "%s: Both bsrp and partial list are empty",
15569c58 476 __func__);
20fd30e5 477 }
478 pim_free_bsgrp_node(bsgrp_node->scope->bsrp_table,
479 &bsgrp_node->group);
480 pim_free_bsgrp_data(bsgrp_node);
481 return;
482 }
483
484 /* Possible when a group with 0 rp count received in BSM */
485 if ((active) && (!pend)) {
486 pim_rp_del(pim, active->rp_address, bsgrp_node->group,
487 NULL, RP_SRC_BSR);
488 pim_free_bsgrp_node(bsgrp_node->scope->bsrp_table,
489 &bsgrp_node->group);
490 if (PIM_DEBUG_BSM) {
491 zlog_debug("%s:Pend List is null,del grp node",
15569c58 492 __func__);
20fd30e5 493 }
494 pim_free_bsgrp_data(bsgrp_node);
495 return;
496 }
497 }
498
499 if ((had_rp_node) && (rp_info->rp_src == RP_SRC_STATIC)) {
500 /* We need to unlock rn this case */
501 route_unlock_node(rn);
502 /* there is a chance that static rp exist and bsrp cleaned
503 * so clean bsgrp node if pending list empty
504 */
505 if (!pend) {
506 if (PIM_DEBUG_BSM)
507 zlog_debug(
508 "%s: Partial list is empty, static rp exists",
15569c58 509 __func__);
20fd30e5 510 pim_free_bsgrp_node(bsgrp_node->scope->bsrp_table,
511 &bsgrp_node->group);
512 pim_free_bsgrp_data(bsgrp_node);
513 return;
514 }
515 }
516
517 /* swap the list & delete all nodes in partial list (old bsrp_list)
518 * before swap
519 * active is head of bsrp list
520 * pend is head of partial list
521 * After swap
522 * active is head of partial list
523 * pend is head of bsrp list
524 * So check appriate head after swap and clean the new partial list
525 */
fdab2940
DL
526 bsm_rpinfos_swap_all(bsgrp_node->bsrp_list,
527 bsgrp_node->partial_bsrp_list);
20fd30e5 528
fdab2940 529 if (active)
20fd30e5 530 pim_g2rp_timer_stop(active);
fdab2940 531 pim_bsm_rpinfos_free(bsgrp_node->partial_bsrp_list);
20fd30e5 532}
533
5acde1cf 534static bool is_preferred_bsr(struct pim_instance *pim, struct in_addr bsr,
535 uint32_t bsr_prio)
536{
537 if (bsr.s_addr == pim->global_scope.current_bsr.s_addr)
538 return true;
539
540 if (bsr_prio > pim->global_scope.current_bsr_prio)
541 return true;
542
543 else if (bsr_prio == pim->global_scope.current_bsr_prio) {
e34c4619 544 if (ntohl(bsr.s_addr)
545 >= ntohl(pim->global_scope.current_bsr.s_addr))
5acde1cf 546 return true;
547 else
548 return false;
549 } else
550 return false;
551}
552
553static void pim_bsm_update(struct pim_instance *pim, struct in_addr bsr,
554 uint32_t bsr_prio)
555{
5acde1cf 556 if (bsr.s_addr != pim->global_scope.current_bsr.s_addr) {
5f010b12 557 pim_nht_bsr_del(pim, pim->global_scope.current_bsr);
4efdb9c6 558 pim_nht_bsr_add(pim, bsr);
5acde1cf 559
5acde1cf 560 pim->global_scope.current_bsr = bsr;
561 pim->global_scope.current_bsr_first_ts =
562 pim_time_monotonic_sec();
563 pim->global_scope.state = ACCEPT_PREFERRED;
564 }
565 pim->global_scope.current_bsr_prio = bsr_prio;
566 pim->global_scope.current_bsr_last_ts = pim_time_monotonic_sec();
567}
568
b09bd804
DL
569void pim_bsm_clear(struct pim_instance *pim)
570{
571 struct route_node *rn;
572 struct route_node *rpnode;
573 struct bsgrp_node *bsgrp;
574 struct prefix nht_p;
575 struct prefix g_all;
576 struct rp_info *rp_all;
577 struct pim_upstream *up;
578 struct rp_info *rp_info;
62596d9a 579 bool upstream_updated = false;
b09bd804 580
5f010b12 581 pim_nht_bsr_del(pim, pim->global_scope.current_bsr);
b09bd804
DL
582
583 /* Reset scope zone data */
584 pim->global_scope.accept_nofwd_bsm = false;
585 pim->global_scope.state = ACCEPT_ANY;
586 pim->global_scope.current_bsr.s_addr = INADDR_ANY;
587 pim->global_scope.current_bsr_prio = 0;
588 pim->global_scope.current_bsr_first_ts = 0;
589 pim->global_scope.current_bsr_last_ts = 0;
590 pim->global_scope.bsm_frag_tag = 0;
591 pim_bsm_frags_free(&pim->global_scope);
592
593 pim_bs_timer_stop(&pim->global_scope);
594
595 for (rn = route_top(pim->global_scope.bsrp_table); rn;
596 rn = route_next(rn)) {
597 bsgrp = rn->info;
598 if (!bsgrp)
599 continue;
600
601 rpnode = route_node_lookup(pim->rp_table, &bsgrp->group);
602
603 if (!rpnode) {
604 pim_free_bsgrp_node(bsgrp->scope->bsrp_table,
605 &bsgrp->group);
606 pim_free_bsgrp_data(bsgrp);
607 continue;
608 }
609
610 rp_info = (struct rp_info *)rpnode->info;
611
612 if ((!rp_info) || (rp_info->rp_src != RP_SRC_BSR)) {
613 pim_free_bsgrp_node(bsgrp->scope->bsrp_table,
614 &bsgrp->group);
615 pim_free_bsgrp_data(bsgrp);
616 continue;
617 }
618
619 /* Deregister addr with Zebra NHT */
620 nht_p.family = AF_INET;
621 nht_p.prefixlen = IPV4_MAX_BITLEN;
622 nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4;
623
624 if (PIM_DEBUG_PIM_NHT_RP) {
625 zlog_debug("%s: Deregister RP addr %pFX with Zebra ",
626 __func__, &nht_p);
627 }
628
629 pim_delete_tracked_nexthop(pim, &nht_p, NULL, rp_info);
630
631 if (!str2prefix("224.0.0.0/4", &g_all))
632 return;
633
634 rp_all = pim_rp_find_match_group(pim, &g_all);
635
636 if (rp_all == rp_info) {
637 rp_all->rp.rpf_addr.family = AF_INET;
638 rp_all->rp.rpf_addr.u.prefix4.s_addr = INADDR_NONE;
639 rp_all->i_am_rp = 0;
640 } else {
641 /* Delete the rp_info from rp-list */
642 listnode_delete(pim->rp_list, rp_info);
643
644 /* Delete the rp node from rp_table */
645 rpnode->info = NULL;
646 route_unlock_node(rpnode);
647 route_unlock_node(rpnode);
648 XFREE(MTYPE_PIM_RP, rp_info);
649 }
650
651 pim_free_bsgrp_node(bsgrp->scope->bsrp_table, &bsgrp->group);
652 pim_free_bsgrp_data(bsgrp);
653 }
654 pim_rp_refresh_group_to_rp_mapping(pim);
655
656
657 frr_each (rb_pim_upstream, &pim->upstream_head, up) {
658 /* Find the upstream (*, G) whose upstream address is same as
659 * the RP
660 */
2a27f13b 661 if (!pim_addr_is_any(up->sg.src))
b09bd804
DL
662 continue;
663
664 struct prefix grp;
665 struct rp_info *trp_info;
666
c631920c 667 pim_addr_to_prefix(&grp, up->sg.grp);
b09bd804
DL
668 trp_info = pim_rp_find_match_group(pim, &grp);
669
670 /* RP not found for the group grp */
671 if (pim_rpf_addr_is_inaddr_none(&trp_info->rp)) {
672 pim_upstream_rpf_clear(pim, up);
673 pim_rp_set_upstream_addr(pim, &up->upstream_addr,
674 up->sg.src, up->sg.grp);
675 } else {
676 /* RP found for the group grp */
677 pim_upstream_update(pim, up);
62596d9a 678 upstream_updated = true;
b09bd804
DL
679 }
680 }
62596d9a
MR
681
682 if (upstream_updated)
683 pim_zebra_update_all_interfaces(pim);
b09bd804
DL
684}
685
79d97386 686static bool pim_bsm_send_intf(uint8_t *buf, int len, struct interface *ifp,
29fd9fca 687 pim_addr dst_addr)
79d97386 688{
689 struct pim_interface *pim_ifp;
690
691 pim_ifp = ifp->info;
692
693 if (!pim_ifp) {
694 if (PIM_DEBUG_BSM)
695 zlog_debug("%s: Pim interface not available for %s",
15569c58 696 __func__, ifp->name);
79d97386 697 return false;
698 }
699
700 if (pim_ifp->pim_sock_fd == -1) {
701 if (PIM_DEBUG_BSM)
702 zlog_debug("%s: Pim sock not available for %s",
15569c58 703 __func__, ifp->name);
79d97386 704 return false;
705 }
706
3bb29c4e
DS
707 if (pim_msg_send(pim_ifp->pim_sock_fd, pim_ifp->primary_address,
708 dst_addr, buf, len, ifp->name)) {
709 zlog_warn("%s: Could not send BSM message on interface: %s",
15569c58 710 __func__, ifp->name);
3bb29c4e
DS
711 return false;
712 }
713
79d97386 714 pim_ifp->pim_ifstat_bsm_tx++;
715 pim_ifp->pim->bsm_sent++;
716 return true;
717}
718
40270c35 719static bool pim_bsm_frag_send(uint8_t *buf, uint32_t len, struct interface *ifp,
29fd9fca 720 uint32_t pim_mtu, pim_addr dst_addr, bool no_fwd)
40270c35 721{
722 struct bsmmsg_grpinfo *grpinfo, *curgrp;
723 uint8_t *firstgrp_ptr;
724 uint8_t *pkt;
725 uint8_t *pak_start;
726 uint32_t parsed_len = 0;
727 uint32_t this_pkt_rem;
728 uint32_t copy_byte_count;
729 uint32_t this_pkt_len;
730 uint8_t total_rp_cnt;
731 uint8_t this_rp_cnt;
732 uint8_t frag_rp_cnt;
733 uint8_t rp_fit_cnt;
734 bool pak_pending = false;
735
736 /* MTU passed here is PIM MTU (IP MTU less IP Hdr) */
737 if (pim_mtu < (PIM_MIN_BSM_LEN)) {
738 zlog_warn(
f79f7a7b 739 "%s: mtu(pim mtu: %d) size less than minimum bootstrap len",
15569c58 740 __func__, pim_mtu);
40270c35 741 if (PIM_DEBUG_BSM)
742 zlog_debug(
f79f7a7b 743 "%s: mtu (pim mtu:%d) less than minimum bootstrap len",
15569c58 744 __func__, pim_mtu);
40270c35 745 return false;
746 }
747
748 pak_start = XCALLOC(MTYPE_PIM_BSM_PKT_VAR_MEM, pim_mtu);
749
40270c35 750 pkt = pak_start;
751
752 /* Fill PIM header later before sending packet to calc checksum */
753 pkt += PIM_MSG_HEADER_LEN;
754 buf += PIM_MSG_HEADER_LEN;
755
756 /* copy bsm header to new packet at offset of pim hdr */
757 memcpy(pkt, buf, PIM_BSM_HDR_LEN);
758 pkt += PIM_BSM_HDR_LEN;
759 buf += PIM_BSM_HDR_LEN;
760 parsed_len += (PIM_MSG_HEADER_LEN + PIM_BSM_HDR_LEN);
761
762 /* Store the position of first grp ptr, which can be reused for
763 * next packet to start filling group. old bsm header and pim hdr
764 * remains. So need not be filled again for next packet onwards.
765 */
766 firstgrp_ptr = pkt;
767
768 /* we received mtu excluding IP hdr len as param
769 * now this_pkt_rem is mtu excluding
770 * PIM_BSM_HDR_LEN + PIM_MSG_HEADER_LEN
771 */
772 this_pkt_rem = pim_mtu - (PIM_BSM_HDR_LEN + PIM_MSG_HEADER_LEN);
773
774 /* For each group till the packet length parsed */
775 while (parsed_len < len) {
776 /* pkt ---> fragment's current pointer
777 * buf ---> input buffer's current pointer
778 * mtu ---> size of the pim packet - PIM header
779 * curgrp ---> current group on the fragment
780 * grpinfo ---> current group on the input buffer
781 * this_pkt_rem ---> bytes remaing on the current fragment
782 * rp_fit_cnt ---> num of rp for current grp that
783 * fits this frag
784 * total_rp_cnt ---> total rp present for the group in the buf
785 * frag_rp_cnt ---> no of rp for the group to be fit in
786 * the frag
787 * this_rp_cnt ---> how many rp have we parsed
788 */
789 grpinfo = (struct bsmmsg_grpinfo *)buf;
790 memcpy(pkt, buf, PIM_BSM_GRP_LEN);
791 curgrp = (struct bsmmsg_grpinfo *)pkt;
792 parsed_len += PIM_BSM_GRP_LEN;
793 pkt += PIM_BSM_GRP_LEN;
794 buf += PIM_BSM_GRP_LEN;
795 this_pkt_rem -= PIM_BSM_GRP_LEN;
796
797 /* initialize rp count and total_rp_cnt before the rp loop */
798 this_rp_cnt = 0;
799 total_rp_cnt = grpinfo->frag_rp_count;
800
801 /* Loop till all RPs for the group parsed */
802 while (this_rp_cnt < total_rp_cnt) {
803 /* All RP from a group processed here.
804 * group is pointed by grpinfo.
805 * At this point make sure buf pointing to a RP
806 * within a group
807 */
808 rp_fit_cnt = this_pkt_rem / PIM_BSM_RP_LEN;
809
810 /* calculate how many rp am i going to copy in
811 * this frag
812 */
813 if (rp_fit_cnt > (total_rp_cnt - this_rp_cnt))
814 frag_rp_cnt = total_rp_cnt - this_rp_cnt;
815 else
816 frag_rp_cnt = rp_fit_cnt;
817
818 /* populate the frag rp count for the current grp */
819 curgrp->frag_rp_count = frag_rp_cnt;
820 copy_byte_count = frag_rp_cnt * PIM_BSM_RP_LEN;
821
822 /* copy all the rp that we are fitting in this
823 * frag for the grp
824 */
825 memcpy(pkt, buf, copy_byte_count);
826 this_rp_cnt += frag_rp_cnt;
827 buf += copy_byte_count;
828 pkt += copy_byte_count;
829 parsed_len += copy_byte_count;
830 this_pkt_rem -= copy_byte_count;
831
832 /* Either we couldn't fit all rp for the group or the
833 * mtu reached
834 */
835 if ((this_rp_cnt < total_rp_cnt)
836 || (this_pkt_rem
837 < (PIM_BSM_GRP_LEN + PIM_BSM_RP_LEN))) {
838 /* No space to fit in more rp, send this pkt */
839 this_pkt_len = pim_mtu - this_pkt_rem;
840 pim_msg_build_header(pak_start, this_pkt_len,
841 PIM_MSG_TYPE_BOOTSTRAP,
842 no_fwd);
843 pim_bsm_send_intf(pak_start, this_pkt_len, ifp,
844 dst_addr);
845
846 /* Construct next fragment. Reuse old packet */
847 pkt = firstgrp_ptr;
848 this_pkt_rem = pim_mtu - (PIM_BSM_HDR_LEN
849 + PIM_MSG_HEADER_LEN);
850
851 /* If pkt can't accomodate next group + atleast
852 * one rp, we must break out of this inner loop
853 * and process next RP
854 */
855 if (total_rp_cnt == this_rp_cnt)
856 break;
857
858 /* If some more RPs for the same group pending,
859 * fill grp hdr
860 */
861 memcpy(pkt, (uint8_t *)grpinfo,
862 PIM_BSM_GRP_LEN);
863 curgrp = (struct bsmmsg_grpinfo *)pkt;
864 pkt += PIM_BSM_GRP_LEN;
865 this_pkt_rem -= PIM_BSM_GRP_LEN;
866 pak_pending = false;
867 } else {
868 /* We filled something but not yet sent out */
869 pak_pending = true;
870 }
871 } /* while RP count */
872 } /*while parsed len */
873
874 /* Send if we have any unsent packet */
875 if (pak_pending) {
876 this_pkt_len = pim_mtu - this_pkt_rem;
877 pim_msg_build_header(pak_start, this_pkt_len,
878 PIM_MSG_TYPE_BOOTSTRAP, no_fwd);
879 pim_bsm_send_intf(pak_start, (pim_mtu - this_pkt_rem), ifp,
880 dst_addr);
881 }
882 XFREE(MTYPE_PIM_BSM_PKT_VAR_MEM, pak_start);
883 return true;
884}
885
79d97386 886static void pim_bsm_fwd_whole_sz(struct pim_instance *pim, uint8_t *buf,
887 uint32_t len, int sz)
888{
889 struct interface *ifp;
890 struct pim_interface *pim_ifp;
29fd9fca 891 pim_addr dst_addr;
79d97386 892 uint32_t pim_mtu;
2951a7a4
QY
893 bool no_fwd = false;
894 bool ret = false;
79d97386 895
896 /* For now only global scope zone is supported, so send on all
897 * pim interfaces in the vrf
898 */
899 dst_addr = qpim_all_pim_routers_addr;
900 FOR_ALL_INTERFACES (pim->vrf, ifp) {
901 pim_ifp = ifp->info;
902 if ((!pim_ifp) || (!pim_ifp->bsm_enable))
903 continue;
9255f21e 904
905 /*
906 * RFC 5059 Sec 3.4:
907 * When a Bootstrap message is forwarded, it is forwarded out
908 * of every multicast-capable interface that has PIM neighbors.
909 *
910 * So skipping pim interfaces with no neighbors.
911 */
912 if (listcount(pim_ifp->pim_neighbor_list) == 0)
913 continue;
914
79d97386 915 pim_hello_require(ifp);
916 pim_mtu = ifp->mtu - MAX_IP_HDR_LEN;
917 if (pim_mtu < len) {
40270c35 918 ret = pim_bsm_frag_send(buf, len, ifp, pim_mtu,
919 dst_addr, no_fwd);
920 if (PIM_DEBUG_BSM)
921 zlog_debug("%s: pim_bsm_frag_send returned %s",
15569c58 922 __func__, ret ? "TRUE" : "FALSE");
79d97386 923 } else {
924 pim_msg_build_header(buf, len, PIM_MSG_TYPE_BOOTSTRAP,
925 no_fwd);
926 if (!pim_bsm_send_intf(buf, len, ifp, dst_addr)) {
927 if (PIM_DEBUG_BSM)
928 zlog_debug(
2951a7a4 929 "%s: pim_bsm_send_intf returned false",
15569c58 930 __func__);
79d97386 931 }
932 }
933 }
934}
935
6bb2ef35 936bool pim_bsm_new_nbr_fwd(struct pim_neighbor *neigh, struct interface *ifp)
937{
29fd9fca 938 pim_addr dst_addr;
6bb2ef35 939 struct pim_interface *pim_ifp;
940 struct bsm_scope *scope;
d4a4525c 941 struct bsm_frag *bsfrag;
6bb2ef35 942 char neigh_src_str[INET_ADDRSTRLEN];
943 uint32_t pim_mtu;
944 bool no_fwd = true;
945 bool ret = false;
946
9bb93fa0
DL
947 if (PIM_DEBUG_BSM)
948 zlog_debug("%s: New neighbor %pPA seen on %s", __func__,
949 &neigh->source_addr, ifp->name);
6bb2ef35 950
951 pim_ifp = ifp->info;
952
953 /* DR only forwards BSM packet */
034db86b 954 if (!pim_addr_cmp(pim_ifp->pim_dr_addr, pim_ifp->primary_address)) {
6bb2ef35 955 if (PIM_DEBUG_BSM)
956 zlog_debug(
957 "%s: It is not DR, so don't forward BSM packet",
5e81f5dd 958 __func__);
6bb2ef35 959 }
960
961 if (!pim_ifp->bsm_enable) {
962 if (PIM_DEBUG_BSM)
5e81f5dd
DS
963 zlog_debug("%s: BSM proc not enabled on %s", __func__,
964 ifp->name);
6bb2ef35 965 return ret;
966 }
967
968 scope = &pim_ifp->pim->global_scope;
969
d4a4525c 970 if (!bsm_frags_count(scope->bsm_frags)) {
6bb2ef35 971 if (PIM_DEBUG_BSM)
972 zlog_debug("%s: BSM list for the scope is empty",
5e81f5dd 973 __func__);
6bb2ef35 974 return ret;
975 }
976
977 if (!pim_ifp->ucast_bsm_accept) {
978 dst_addr = qpim_all_pim_routers_addr;
979 if (PIM_DEBUG_BSM)
5e81f5dd
DS
980 zlog_debug("%s: Sending BSM mcast to %s", __func__,
981 neigh_src_str);
6bb2ef35 982 } else {
983 dst_addr = neigh->source_addr;
984 if (PIM_DEBUG_BSM)
5e81f5dd
DS
985 zlog_debug("%s: Sending BSM ucast to %s", __func__,
986 neigh_src_str);
6bb2ef35 987 }
988 pim_mtu = ifp->mtu - MAX_IP_HDR_LEN;
989 pim_hello_require(ifp);
990
d4a4525c
DL
991 frr_each (bsm_frags, scope->bsm_frags, bsfrag) {
992 if (pim_mtu < bsfrag->size) {
993 ret = pim_bsm_frag_send(bsfrag->data, bsfrag->size, ifp,
994 pim_mtu, dst_addr, no_fwd);
6bb2ef35 995 if (!ret) {
996 if (PIM_DEBUG_BSM)
997 zlog_debug(
998 "%s: pim_bsm_frag_send failed",
5e81f5dd 999 __func__);
6bb2ef35 1000 }
1001 } else {
1002 /* Pim header needs to be constructed */
d4a4525c 1003 pim_msg_build_header(bsfrag->data, bsfrag->size,
6bb2ef35 1004 PIM_MSG_TYPE_BOOTSTRAP, no_fwd);
d4a4525c
DL
1005 ret = pim_bsm_send_intf(bsfrag->data, bsfrag->size, ifp,
1006 dst_addr);
6bb2ef35 1007 if (!ret) {
1008 if (PIM_DEBUG_BSM)
1009 zlog_debug(
1010 "%s: pim_bsm_frag_send failed",
5e81f5dd 1011 __func__);
6bb2ef35 1012 }
1013 }
1014 }
1015 return ret;
1016}
1017
d0e418b4 1018struct bsgrp_node *pim_bsm_get_bsgrp_node(struct bsm_scope *scope,
1019 struct prefix *grp)
1020{
1021 struct route_node *rn;
1022 struct bsgrp_node *bsgrp;
1023
1024 rn = route_node_lookup(scope->bsrp_table, grp);
1025 if (!rn) {
1026 if (PIM_DEBUG_BSM)
1027 zlog_debug("%s: Route node doesn't exist for the group",
15569c58 1028 __func__);
d0e418b4 1029 return NULL;
1030 }
1031 bsgrp = rn->info;
1032 route_unlock_node(rn);
1033
1034 return bsgrp;
1035}
5acde1cf 1036
7b3e6ba1 1037static uint32_t hash_calc_on_grp_rp(struct prefix group, struct in_addr rp,
1038 uint8_t hashmasklen)
1039{
1040 uint64_t temp;
1041 uint32_t hash;
1042 uint32_t grpaddr;
1043 uint32_t rp_add;
1044 uint32_t mask = 0xffffffff;
1045
1046 /* mask to be made zero if hashmasklen is 0 because mask << 32
1047 * may not give 0. hashmasklen can be 0 to 32.
1048 */
1049 if (hashmasklen == 0)
1050 mask = 0;
1051
1052 /* in_addr stores ip in big endian, hence network byte order
1053 * convert to uint32 before processing hash
1054 */
1055 grpaddr = ntohl(group.u.prefix4.s_addr);
1056 /* Avoid shifting by 32 bit on a 32 bit register */
1057 if (hashmasklen)
1058 grpaddr = grpaddr & ((mask << (32 - hashmasklen)));
1059 else
1060 grpaddr = grpaddr & mask;
1061 rp_add = ntohl(rp.s_addr);
29f7fc87
DS
1062 temp = 1103515245 * ((1103515245 * (uint64_t)grpaddr + 12345) ^ rp_add)
1063 + 12345;
7b3e6ba1 1064 hash = temp & (0x7fffffff);
1065 return hash;
1066}
1067
1068static bool pim_install_bsm_grp_rp(struct pim_instance *pim,
1069 struct bsgrp_node *grpnode,
1070 struct bsmmsg_rpinfo *rp)
1071{
1072 struct bsm_rpinfo *bsm_rpinfo;
1073 uint8_t hashMask_len = pim->global_scope.hashMasklen;
1074
1075 /*memory allocation for bsm_rpinfo */
fdab2940 1076 bsm_rpinfo = XCALLOC(MTYPE_PIM_BSRP_INFO, sizeof(*bsm_rpinfo));
7b3e6ba1 1077
7b3e6ba1 1078 bsm_rpinfo->rp_prio = rp->rp_pri;
1079 bsm_rpinfo->rp_holdtime = rp->rp_holdtime;
1080 memcpy(&bsm_rpinfo->rp_address, &rp->rpaddr.addr,
1081 sizeof(struct in_addr));
1082 bsm_rpinfo->elapse_time = 0;
1083
1084 /* Back pointer to the group node. */
1085 bsm_rpinfo->bsgrp_node = grpnode;
1086
1087 /* update hash for this rp node */
1088 bsm_rpinfo->hash = hash_calc_on_grp_rp(grpnode->group, rp->rpaddr.addr,
1089 hashMask_len);
fdab2940 1090 if (bsm_rpinfos_add(grpnode->partial_bsrp_list, bsm_rpinfo) == NULL) {
7b3e6ba1 1091 if (PIM_DEBUG_BSM)
1092 zlog_debug(
63efca0e 1093 "%s, bs_rpinfo node added to the partial bs_rplist.",
15569c58 1094 __func__);
7b3e6ba1 1095 return true;
1096 }
1097
1098 if (PIM_DEBUG_BSM)
63efca0e 1099 zlog_debug("%s: list node not added", __func__);
7b3e6ba1 1100
fdab2940 1101 XFREE(MTYPE_PIM_BSRP_INFO, bsm_rpinfo);
7b3e6ba1 1102 return false;
1103}
1104
1105static void pim_update_pending_rp_cnt(struct bsm_scope *sz,
1106 struct bsgrp_node *bsgrp,
1107 uint16_t bsm_frag_tag,
1108 uint32_t total_rp_count)
1109{
1110 if (bsgrp->pend_rp_cnt) {
1111 /* received bsm is different packet ,
1112 * it is not same fragment.
1113 */
1114 if (bsm_frag_tag != bsgrp->frag_tag) {
1115 if (PIM_DEBUG_BSM)
1116 zlog_debug(
63efca0e 1117 "%s,Received a new BSM ,so clear the pending bs_rpinfo list.",
15569c58 1118 __func__);
fdab2940 1119 pim_bsm_rpinfos_free(bsgrp->partial_bsrp_list);
7b3e6ba1 1120 bsgrp->pend_rp_cnt = total_rp_count;
1121 }
1122 } else
1123 bsgrp->pend_rp_cnt = total_rp_count;
1124
1125 bsgrp->frag_tag = bsm_frag_tag;
1126}
1127
1128/* Parsing BSR packet and adding to partial list of corresponding bsgrp node */
1129static bool pim_bsm_parse_install_g2rp(struct bsm_scope *scope, uint8_t *buf,
1130 int buflen, uint16_t bsm_frag_tag)
1131{
1132 struct bsmmsg_grpinfo grpinfo;
1133 struct bsmmsg_rpinfo rpinfo;
1134 struct prefix group;
1135 struct bsgrp_node *bsgrp = NULL;
1136 int frag_rp_cnt = 0;
1137 int offset = 0;
1138 int ins_count = 0;
1139
1140 while (buflen > offset) {
b1945363
DS
1141 if (offset + (int)sizeof(struct bsmmsg_grpinfo) > buflen) {
1142 if (PIM_DEBUG_BSM)
1143 zlog_debug(
1144 "%s: buflen received %d is less than the internal data structure of the packet would suggest",
15569c58 1145 __func__, buflen);
b1945363
DS
1146 return false;
1147 }
7b3e6ba1 1148 /* Extract Group tlv from BSM */
1149 memcpy(&grpinfo, buf, sizeof(struct bsmmsg_grpinfo));
1150
1151 if (PIM_DEBUG_BSM) {
1152 char grp_str[INET_ADDRSTRLEN];
1153
1154 pim_inet4_dump("<Group?>", grpinfo.group.addr, grp_str,
1155 sizeof(grp_str));
1156 zlog_debug(
63efca0e 1157 "%s, Group %s Rpcount:%d Fragment-Rp-count:%d",
15569c58 1158 __func__, grp_str, grpinfo.rp_count,
7b3e6ba1 1159 grpinfo.frag_rp_count);
1160 }
1161
1162 buf += sizeof(struct bsmmsg_grpinfo);
1163 offset += sizeof(struct bsmmsg_grpinfo);
1164
7b3e6ba1 1165 group.family = AF_INET;
b1945363
DS
1166 if (grpinfo.group.mask > IPV4_MAX_BITLEN) {
1167 if (PIM_DEBUG_BSM)
15569c58
DA
1168 zlog_debug(
1169 "%s, v4 prefix length specified: %d is too long",
1170 __func__, grpinfo.group.mask);
b1945363
DS
1171 return false;
1172 }
7b3e6ba1 1173 group.prefixlen = grpinfo.group.mask;
1174 group.u.prefix4.s_addr = grpinfo.group.addr.s_addr;
1175
1176 /* Get the Group node for the BSM rp table */
1177 bsgrp = pim_bsm_get_bsgrp_node(scope, &group);
1178
43038bd5
DL
1179 if (grpinfo.rp_count == 0) {
1180 struct bsm_rpinfo *old_rpinfo;
1181
1182 /* BSR explicitly no longer has RPs for this group */
1183 if (!bsgrp)
1184 continue;
1185
1186 if (PIM_DEBUG_BSM) {
1187 char grp_str[INET_ADDRSTRLEN];
1188
1189 pim_inet4_dump("<Group?>", grpinfo.group.addr,
1190 grp_str, sizeof(grp_str));
1191 zlog_debug("%s, Rp count is zero for group: %s",
1192 __func__, grp_str);
1193 }
1194
1195 old_rpinfo = bsm_rpinfos_first(bsgrp->bsrp_list);
1196 if (old_rpinfo)
1197 pim_rp_del(scope->pim, old_rpinfo->rp_address,
1198 group, NULL, RP_SRC_BSR);
1199
1200 pim_free_bsgrp_node(scope->bsrp_table, &bsgrp->group);
1201 pim_free_bsgrp_data(bsgrp);
1202 continue;
1203 }
1204
7b3e6ba1 1205 if (!bsgrp) {
1206 if (PIM_DEBUG_BSM)
63efca0e
DL
1207 zlog_debug("%s, Create new BSM Group node.",
1208 __func__);
7b3e6ba1 1209
1210 /* create a new node to be added to the tree. */
1211 bsgrp = pim_bsm_new_bsgrp_node(scope->bsrp_table,
1212 &group);
1213
1214 if (!bsgrp) {
1215 zlog_debug(
63efca0e 1216 "%s, Failed to get the BSM group node.",
15569c58 1217 __func__);
7b3e6ba1 1218 continue;
1219 }
1220
1221 bsgrp->scope = scope;
1222 }
1223
1224 pim_update_pending_rp_cnt(scope, bsgrp, bsm_frag_tag,
1225 grpinfo.rp_count);
1226 frag_rp_cnt = grpinfo.frag_rp_count;
1227 ins_count = 0;
1228
1229 while (frag_rp_cnt--) {
b1945363
DS
1230 if (offset + (int)sizeof(struct bsmmsg_rpinfo)
1231 > buflen) {
1232 if (PIM_DEBUG_BSM)
1233 zlog_debug(
1234 "%s, buflen received: %u is less than the internal data structure of the packet would suggest",
15569c58 1235 __func__, buflen);
b1945363
DS
1236 return false;
1237 }
1238
7b3e6ba1 1239 /* Extract RP address tlv from BSM */
1240 memcpy(&rpinfo, buf, sizeof(struct bsmmsg_rpinfo));
1241 rpinfo.rp_holdtime = ntohs(rpinfo.rp_holdtime);
1242 buf += sizeof(struct bsmmsg_rpinfo);
1243 offset += sizeof(struct bsmmsg_rpinfo);
1244
1245 if (PIM_DEBUG_BSM) {
1246 char rp_str[INET_ADDRSTRLEN];
1247
1248 pim_inet4_dump("<Rpaddr?>", rpinfo.rpaddr.addr,
1249 rp_str, sizeof(rp_str));
1250 zlog_debug(
63efca0e 1251 "%s, Rp address - %s; pri:%d hold:%d",
15569c58
DA
1252 __func__, rp_str, rpinfo.rp_pri,
1253 rpinfo.rp_holdtime);
7b3e6ba1 1254 }
1255
1256 /* Call Install api to update grp-rp mappings */
1257 if (pim_install_bsm_grp_rp(scope->pim, bsgrp, &rpinfo))
1258 ins_count++;
1259 }
1260
1261 bsgrp->pend_rp_cnt -= ins_count;
1262
1263 if (!bsgrp->pend_rp_cnt) {
1264 if (PIM_DEBUG_BSM)
1265 zlog_debug(
1266 "%s, Recvd all the rps for this group, so bsrp list with penidng rp list.",
15569c58 1267 __func__);
20fd30e5 1268 /* replace the bsrp_list with pending list */
1269 pim_instate_pend_list(bsgrp);
7b3e6ba1 1270 }
1271 }
1272 return true;
1273}
1274
5acde1cf 1275int pim_bsm_process(struct interface *ifp, struct ip *ip_hdr, uint8_t *buf,
1276 uint32_t buf_size, bool no_fwd)
1277{
1278 struct bsm_hdr *bshdr;
79d97386 1279 int sz = PIM_GBL_SZ_ID;
5acde1cf 1280 struct bsmmsg_grpinfo *msg_grp;
1281 struct pim_interface *pim_ifp = NULL;
d4a4525c 1282 struct bsm_frag *bsfrag;
5acde1cf 1283 struct pim_instance *pim;
1284 char bsr_str[INET_ADDRSTRLEN];
1285 uint16_t frag_tag;
2951a7a4 1286 bool empty_bsm = false;
5acde1cf 1287
1288 /* BSM Packet acceptance validation */
1289 pim_ifp = ifp->info;
1290 if (!pim_ifp) {
1291 if (PIM_DEBUG_BSM)
1292 zlog_debug("%s: multicast not enabled on interface %s",
15569c58 1293 __func__, ifp->name);
5acde1cf 1294 return -1;
1295 }
1296
1297 pim_ifp->pim_ifstat_bsm_rx++;
1298 pim = pim_ifp->pim;
1299 pim->bsm_rcvd++;
1300
1301 /* Drop if bsm processing is disabled on interface */
1302 if (!pim_ifp->bsm_enable) {
15569c58
DA
1303 zlog_warn("%s: BSM not enabled on interface %s", __func__,
1304 ifp->name);
5acde1cf 1305 pim_ifp->pim_ifstat_bsm_cfg_miss++;
1306 pim->bsm_dropped++;
1307 return -1;
1308 }
1309
d83a854b 1310 if (buf_size < (PIM_MSG_HEADER_LEN + sizeof(struct bsm_hdr))) {
b1945363 1311 if (PIM_DEBUG_BSM)
15569c58
DA
1312 zlog_debug(
1313 "%s: received buffer length of %d which is too small to properly decode",
1314 __func__, buf_size);
b1945363
DS
1315 return -1;
1316 }
1317
5acde1cf 1318 bshdr = (struct bsm_hdr *)(buf + PIM_MSG_HEADER_LEN);
1319 pim_inet4_dump("<bsr?>", bshdr->bsr_addr.addr, bsr_str,
1320 sizeof(bsr_str));
12256b84 1321 if (bshdr->hm_len > IPV4_MAX_BITLEN) {
6cde4b45 1322 zlog_warn("Bad hashmask length for IPv4; got %hhu, expected value in range 0-32",
817f8933
QY
1323 bshdr->hm_len);
1324 pim->bsm_dropped++;
1325 return -1;
1326 }
5acde1cf 1327 pim->global_scope.hashMasklen = bshdr->hm_len;
1328 frag_tag = ntohs(bshdr->frag_tag);
1329
1330 /* Identify empty BSM */
1331 if ((buf_size - PIM_BSM_HDR_LEN - PIM_MSG_HEADER_LEN) < PIM_BSM_GRP_LEN)
1332 empty_bsm = true;
1333
1334 if (!empty_bsm) {
1335 msg_grp = (struct bsmmsg_grpinfo *)(buf + PIM_MSG_HEADER_LEN
1336 + PIM_BSM_HDR_LEN);
1337 /* Currently we don't support scope zoned BSM */
1338 if (msg_grp->group.sz) {
1339 if (PIM_DEBUG_BSM)
1340 zlog_debug(
1341 "%s : Administratively scoped range BSM received",
15569c58 1342 __func__);
5acde1cf 1343 pim_ifp->pim_ifstat_bsm_invalid_sz++;
1344 pim->bsm_dropped++;
1345 return -1;
1346 }
1347 }
1348
1349 /* Drop if bsr is not preferred bsr */
1350 if (!is_preferred_bsr(pim, bshdr->bsr_addr.addr, bshdr->bsr_prio)) {
1351 if (PIM_DEBUG_BSM)
1352 zlog_debug("%s : Received a non-preferred BSM",
15569c58 1353 __func__);
5acde1cf 1354 pim->bsm_dropped++;
1355 return -1;
1356 }
1357
1358 if (no_fwd) {
1359 /* only accept no-forward BSM if quick refresh on startup */
1360 if ((pim->global_scope.accept_nofwd_bsm)
1361 || (frag_tag == pim->global_scope.bsm_frag_tag)) {
1362 pim->global_scope.accept_nofwd_bsm = false;
1363 } else {
1364 if (PIM_DEBUG_BSM)
1365 zlog_debug(
1366 "%s : nofwd_bsm received on %s when accpt_nofwd_bsm false",
15569c58 1367 __func__, bsr_str);
5acde1cf 1368 pim->bsm_dropped++;
1369 pim_ifp->pim_ifstat_ucast_bsm_cfg_miss++;
1370 return -1;
1371 }
1372 }
1373
809c11a9
DL
1374#if PIM_IPV == 4
1375 if (ip_hdr->ip_dst.s_addr == qpim_all_pim_routers_addr.s_addr)
1376#else
1377 if (0)
1378#endif
1379 {
4efdb9c6
DL
1380 /* Multicast BSMs are only accepted if source interface & IP
1381 * match RPF towards the BSR's IP address, or they have
1382 * no-forward set
1383 */
1384 if (!no_fwd
1385 && !pim_nht_bsr_rpf_check(pim, bshdr->bsr_addr.addr, ifp,
1386 ip_hdr->ip_src)) {
1387 if (PIM_DEBUG_BSM)
1388 zlog_debug(
1389 "BSM check: RPF to BSR %s is not %pI4%%%s",
1390 bsr_str, &ip_hdr->ip_src, ifp->name);
1391 pim->bsm_dropped++;
1392 return -1;
5acde1cf 1393 }
1e9044be
DL
1394 } else if (if_address_is_local(&ip_hdr->ip_dst, AF_INET,
1395 pim->vrf->vrf_id)) {
5acde1cf 1396 /* Unicast BSM received - if ucast bsm not enabled on
1397 * the interface, drop it
1398 */
1399 if (!pim_ifp->ucast_bsm_accept) {
1400 if (PIM_DEBUG_BSM)
1401 zlog_debug(
1402 "%s : Unicast BSM not enabled on interface %s",
15569c58 1403 __func__, ifp->name);
5acde1cf 1404 pim_ifp->pim_ifstat_ucast_bsm_cfg_miss++;
1405 pim->bsm_dropped++;
1406 return -1;
1407 }
1408
1409 } else {
1410 if (PIM_DEBUG_BSM)
1411 zlog_debug("%s : Invalid destination address",
15569c58 1412 __func__);
5acde1cf 1413 pim->bsm_dropped++;
1414 return -1;
1415 }
1416
1417 if (empty_bsm) {
1418 if (PIM_DEBUG_BSM)
15569c58 1419 zlog_debug("%s : Empty Pref BSM received", __func__);
5acde1cf 1420 }
7b3e6ba1 1421 /* Parse Update bsm rp table and install/uninstall rp if required */
1422 if (!pim_bsm_parse_install_g2rp(
1423 &pim_ifp->pim->global_scope,
1424 (buf + PIM_BSM_HDR_LEN + PIM_MSG_HEADER_LEN),
1425 (buf_size - PIM_BSM_HDR_LEN - PIM_MSG_HEADER_LEN),
1426 frag_tag)) {
1427 if (PIM_DEBUG_BSM) {
63efca0e 1428 zlog_debug("%s, Parsing BSM failed.", __func__);
7b3e6ba1 1429 }
1430 pim->bsm_dropped++;
1431 return -1;
1432 }
5acde1cf 1433 /* Restart the bootstrap timer */
1434 pim_bs_timer_restart(&pim_ifp->pim->global_scope,
1435 PIM_BSR_DEFAULT_TIMEOUT);
1436
1437 /* If new BSM received, clear the old bsm database */
1438 if (pim_ifp->pim->global_scope.bsm_frag_tag != frag_tag) {
1439 if (PIM_DEBUG_BSM) {
1440 zlog_debug("%s: Current frag tag: %d Frag teg rcvd: %d",
15569c58 1441 __func__,
5acde1cf 1442 pim_ifp->pim->global_scope.bsm_frag_tag,
1443 frag_tag);
1444 }
d4a4525c 1445 pim_bsm_frags_free(&pim_ifp->pim->global_scope);
5acde1cf 1446 pim_ifp->pim->global_scope.bsm_frag_tag = frag_tag;
1447 }
1448
1449 /* update the scope information from bsm */
1450 pim_bsm_update(pim, bshdr->bsr_addr.addr, bshdr->bsr_prio);
79d97386 1451
1452 if (!no_fwd) {
1453 pim_bsm_fwd_whole_sz(pim_ifp->pim, buf, buf_size, sz);
d4a4525c
DL
1454 bsfrag = XCALLOC(MTYPE_PIM_BSM_FRAG,
1455 sizeof(struct bsm_frag) + buf_size);
79d97386 1456
d4a4525c
DL
1457 bsfrag->size = buf_size;
1458 memcpy(bsfrag->data, buf, buf_size);
1459 bsm_frags_add_tail(pim_ifp->pim->global_scope.bsm_frags,
1460 bsfrag);
79d97386 1461 }
1462
5acde1cf 1463 return 0;
1464}