2 * pim_bsm.c: PIM BSM handling routines
4 * Copyright (C) 2018-19 Vmware, Inc.
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.
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.
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,
29 #include "pim_iface.h"
30 #include "pim_instance.h"
32 #include "pim_hello.h"
38 /* Functions forward declaration */
39 static void pim_bs_timer_start(struct bsm_scope
*scope
, int bs_timeout
);
40 static void pim_g2rp_timer_start(struct bsm_rpinfo
*bsrp
, int hold_time
);
41 static inline void pim_g2rp_timer_restart(struct bsm_rpinfo
*bsrp
,
45 DEFINE_MTYPE_STATIC(PIMD
, PIM_BSGRP_NODE
, "PIM BSR advertised grp info")
46 DEFINE_MTYPE_STATIC(PIMD
, PIM_BSRP_NODE
, "PIM BSR advertised RP info")
47 DEFINE_MTYPE_STATIC(PIMD
, PIM_BSM_INFO
, "PIM BSM Info")
48 DEFINE_MTYPE_STATIC(PIMD
, PIM_BSM_PKT_VAR_MEM
, "PIM BSM Packet")
50 /* All bsm packets forwarded shall be fit within ip mtu less iphdr(max) */
51 #define MAX_IP_HDR_LEN 24
53 /* pim_bsm_write_config - Write the interface pim bsm configuration.*/
54 void pim_bsm_write_config(struct vty
*vty
, struct interface
*ifp
)
56 struct pim_interface
*pim_ifp
= ifp
->info
;
59 if (!pim_ifp
->bsm_enable
)
60 vty_out(vty
, " no ip pim bsm\n");
61 if (!pim_ifp
->ucast_bsm_accept
)
62 vty_out(vty
, " no ip pim unicast-bsm\n");
66 static void pim_free_bsgrp_data(struct bsgrp_node
*bsgrp_node
)
68 if (bsgrp_node
->bsrp_list
)
69 list_delete(&bsgrp_node
->bsrp_list
);
70 if (bsgrp_node
->partial_bsrp_list
)
71 list_delete(&bsgrp_node
->partial_bsrp_list
);
72 XFREE(MTYPE_PIM_BSGRP_NODE
, bsgrp_node
);
75 static void pim_free_bsgrp_node(struct route_table
*rt
, struct prefix
*grp
)
77 struct route_node
*rn
;
79 rn
= route_node_lookup(rt
, grp
);
82 route_unlock_node(rn
);
83 route_unlock_node(rn
);
87 static void pim_bsm_node_free(struct bsm_info
*bsm
)
89 XFREE(MTYPE_PIM_BSM_PKT_VAR_MEM
, bsm
->bsm
);
90 XFREE(MTYPE_PIM_BSM_INFO
, bsm
);
93 static int pim_g2rp_list_compare(struct bsm_rpinfo
*node1
,
94 struct bsm_rpinfo
*node2
)
97 * Step-1 : Loweset Rp priority will have higher precedance.
98 * Step-2 : If priority same then higher hash val will have
100 * Step-3 : If Hash val is same then highest rp address will
103 if (node1
->rp_prio
< node2
->rp_prio
)
105 if (node1
->rp_prio
> node2
->rp_prio
)
107 if (node1
->hash
< node2
->hash
)
109 if (node1
->hash
> node2
->hash
)
111 if (node1
->rp_address
.s_addr
< node2
->rp_address
.s_addr
)
113 if (node1
->rp_address
.s_addr
> node2
->rp_address
.s_addr
)
118 static void pim_free_bsrp_node(struct bsm_rpinfo
*bsrp_info
)
120 if (bsrp_info
->g2rp_timer
)
121 THREAD_OFF(bsrp_info
->g2rp_timer
);
122 XFREE(MTYPE_PIM_BSRP_NODE
, bsrp_info
);
125 static struct list
*pim_alloc_bsrp_list(void)
127 struct list
*new_list
= NULL
;
129 new_list
= list_new();
134 new_list
->cmp
= (int (*)(void *, void *))pim_g2rp_list_compare
;
135 new_list
->del
= (void (*)(void *))pim_free_bsrp_node
;
140 static struct bsgrp_node
*pim_bsm_new_bsgrp_node(struct route_table
*rt
,
143 struct route_node
*rn
;
144 struct bsgrp_node
*bsgrp
;
146 rn
= route_node_get(rt
, grp
);
148 zlog_warn("%s: route node creation failed", __func__
);
151 bsgrp
= XCALLOC(MTYPE_PIM_BSGRP_NODE
, sizeof(struct bsgrp_node
));
154 bsgrp
->bsrp_list
= pim_alloc_bsrp_list();
155 bsgrp
->partial_bsrp_list
= pim_alloc_bsrp_list();
157 if ((!bsgrp
->bsrp_list
) || (!bsgrp
->partial_bsrp_list
)) {
158 route_unlock_node(rn
);
159 pim_free_bsgrp_data(bsgrp
);
163 prefix_copy(&bsgrp
->group
, grp
);
167 static int pim_on_bs_timer(struct thread
*t
)
169 struct route_node
*rn
;
170 struct bsm_scope
*scope
;
171 struct bsgrp_node
*bsgrp_node
;
172 struct bsm_rpinfo
*bsrp
;
174 char buf
[PREFIX2STR_BUFFER
];
175 bool is_bsr_tracking
= true;
177 scope
= THREAD_ARG(t
);
178 THREAD_OFF(scope
->bs_timer
);
181 zlog_debug("%s: Bootstrap Timer expired for scope: %d",
182 __func__
, scope
->sz_id
);
184 /* Remove next hop tracking for the bsr */
185 nht_p
.family
= AF_INET
;
186 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
187 nht_p
.u
.prefix4
= scope
->current_bsr
;
189 prefix2str(&nht_p
, buf
, sizeof(buf
));
190 zlog_debug("%s: Deregister BSR addr %s with Zebra NHT",
193 pim_delete_tracked_nexthop(scope
->pim
, &nht_p
, NULL
, NULL
,
196 /* Reset scope zone data */
197 scope
->accept_nofwd_bsm
= false;
198 scope
->state
= ACCEPT_ANY
;
199 scope
->current_bsr
.s_addr
= INADDR_ANY
;
200 scope
->current_bsr_prio
= 0;
201 scope
->current_bsr_first_ts
= 0;
202 scope
->current_bsr_last_ts
= 0;
203 scope
->bsm_frag_tag
= 0;
204 list_delete_all_node(scope
->bsm_list
);
206 for (rn
= route_top(scope
->bsrp_table
); rn
; rn
= route_next(rn
)) {
208 bsgrp_node
= (struct bsgrp_node
*)rn
->info
;
211 zlog_debug("%s: bsgrp_node is null", __func__
);
214 /* Give grace time for rp to continue for another hold time */
215 if ((bsgrp_node
->bsrp_list
) && (bsgrp_node
->bsrp_list
->count
)) {
216 bsrp
= listnode_head(bsgrp_node
->bsrp_list
);
217 pim_g2rp_timer_restart(bsrp
, bsrp
->rp_holdtime
);
219 /* clear pending list */
220 if ((bsgrp_node
->partial_bsrp_list
)
221 && (bsgrp_node
->partial_bsrp_list
->count
)) {
222 list_delete_all_node(bsgrp_node
->partial_bsrp_list
);
223 bsgrp_node
->pend_rp_cnt
= 0;
229 static void pim_bs_timer_stop(struct bsm_scope
*scope
)
232 zlog_debug("%s : BS timer being stopped of sz: %d", __func__
,
234 THREAD_OFF(scope
->bs_timer
);
237 static void pim_bs_timer_start(struct bsm_scope
*scope
, int bs_timeout
)
241 zlog_debug("%s : Invalid scope(NULL).", __func__
);
244 THREAD_OFF(scope
->bs_timer
);
247 "%s : starting bs timer for scope %d with timeout %d secs",
248 __func__
, scope
->sz_id
, bs_timeout
);
249 thread_add_timer(router
->master
, pim_on_bs_timer
, scope
, bs_timeout
,
253 static inline void pim_bs_timer_restart(struct bsm_scope
*scope
, int bs_timeout
)
255 pim_bs_timer_start(scope
, bs_timeout
);
258 void pim_bsm_proc_init(struct pim_instance
*pim
)
260 memset(&pim
->global_scope
, 0, sizeof(struct bsm_scope
));
262 pim
->global_scope
.sz_id
= PIM_GBL_SZ_ID
;
263 pim
->global_scope
.bsrp_table
= route_table_init();
264 pim
->global_scope
.accept_nofwd_bsm
= true;
265 pim
->global_scope
.state
= NO_INFO
;
266 pim
->global_scope
.pim
= pim
;
267 pim
->global_scope
.bsm_list
= list_new();
268 pim
->global_scope
.bsm_list
->del
= (void (*)(void *))pim_bsm_node_free
;
269 pim_bs_timer_start(&pim
->global_scope
, PIM_BS_TIME
);
272 void pim_bsm_proc_free(struct pim_instance
*pim
)
274 struct route_node
*rn
;
275 struct bsgrp_node
*bsgrp
;
277 pim_bs_timer_stop(&pim
->global_scope
);
279 if (pim
->global_scope
.bsm_list
)
280 list_delete(&pim
->global_scope
.bsm_list
);
282 for (rn
= route_top(pim
->global_scope
.bsrp_table
); rn
;
283 rn
= route_next(rn
)) {
287 pim_free_bsgrp_data(bsgrp
);
290 route_table_finish(pim
->global_scope
.bsrp_table
);
293 static bool is_hold_time_elapsed(void *data
)
295 struct bsm_rpinfo
*bsrp
;
299 if (bsrp
->elapse_time
< bsrp
->rp_holdtime
)
305 static int pim_on_g2rp_timer(struct thread
*t
)
307 struct bsm_rpinfo
*bsrp
;
308 struct bsm_rpinfo
*bsrp_node
;
309 struct bsgrp_node
*bsgrp_node
;
310 struct listnode
*bsrp_ln
;
311 struct pim_instance
*pim
;
312 struct rp_info
*rp_info
;
313 struct route_node
*rn
;
315 struct in_addr bsrp_addr
;
317 bsrp
= THREAD_ARG(t
);
318 THREAD_OFF(bsrp
->g2rp_timer
);
319 bsgrp_node
= bsrp
->bsgrp_node
;
321 /* elapse time is the hold time of expired node */
322 elapse
= bsrp
->rp_holdtime
;
323 bsrp_addr
= bsrp
->rp_address
;
325 /* update elapse for all bsrp nodes */
326 for (ALL_LIST_ELEMENTS_RO(bsgrp_node
->bsrp_list
, bsrp_ln
, bsrp_node
))
327 bsrp_node
->elapse_time
+= elapse
;
329 /* remove the expired nodes from the list */
330 list_filter_out_nodes(bsgrp_node
->bsrp_list
, is_hold_time_elapsed
);
332 /* Get the next elected rp node */
333 bsrp
= listnode_head(bsgrp_node
->bsrp_list
);
334 pim
= bsgrp_node
->scope
->pim
;
335 rn
= route_node_lookup(pim
->rp_table
, &bsgrp_node
->group
);
338 zlog_warn("%s: Route node doesn't exist", __func__
);
342 rp_info
= (struct rp_info
*)rn
->info
;
345 route_unlock_node(rn
);
349 if (rp_info
->rp_src
!= RP_SRC_STATIC
) {
350 /* If new rp available, change it else delete the existing */
352 bsrp_addr
= bsrp
->rp_address
;
353 pim_g2rp_timer_start(
354 bsrp
, (bsrp
->rp_holdtime
- bsrp
->elapse_time
));
355 pim_rp_change(pim
, bsrp_addr
, bsgrp_node
->group
,
358 pim_rp_del(pim
, bsrp_addr
, bsgrp_node
->group
, NULL
,
363 if ((!bsgrp_node
->bsrp_list
->count
)
364 && (!bsgrp_node
->partial_bsrp_list
->count
)) {
365 pim_free_bsgrp_node(pim
->global_scope
.bsrp_table
,
367 pim_free_bsgrp_data(bsgrp_node
);
373 static void pim_g2rp_timer_start(struct bsm_rpinfo
*bsrp
, int hold_time
)
377 zlog_debug("%s : Invalid brsp(NULL).", __func__
);
380 THREAD_OFF(bsrp
->g2rp_timer
);
385 "%s : starting g2rp timer for grp: %s - rp: %s with timeout %d secs(Actual Hold time : %d secs)",
386 __func__
, prefix2str(&bsrp
->bsgrp_node
->group
, buf
, 48),
387 inet_ntoa(bsrp
->rp_address
), hold_time
,
391 thread_add_timer(router
->master
, pim_on_g2rp_timer
, bsrp
, hold_time
,
395 static inline void pim_g2rp_timer_restart(struct bsm_rpinfo
*bsrp
,
398 pim_g2rp_timer_start(bsrp
, hold_time
);
401 static void pim_g2rp_timer_stop(struct bsm_rpinfo
*bsrp
)
409 zlog_debug("%s : stopping g2rp timer for grp: %s - rp: %s",
411 prefix2str(&bsrp
->bsgrp_node
->group
, buf
, 48),
412 inet_ntoa(bsrp
->rp_address
));
415 THREAD_OFF(bsrp
->g2rp_timer
);
418 static bool is_hold_time_zero(void *data
)
420 struct bsm_rpinfo
*bsrp
;
424 if (bsrp
->rp_holdtime
)
430 static void pim_instate_pend_list(struct bsgrp_node
*bsgrp_node
)
432 struct bsm_rpinfo
*active
;
433 struct bsm_rpinfo
*pend
;
435 struct rp_info
*rp_info
;
436 struct route_node
*rn
;
437 struct pim_instance
*pim
;
438 struct rp_info
*rp_all
;
439 struct prefix group_all
;
440 bool had_rp_node
= true;
442 pim
= bsgrp_node
->scope
->pim
;
443 active
= listnode_head(bsgrp_node
->bsrp_list
);
445 /* Remove nodes with hold time 0 & check if list still has a head */
446 list_filter_out_nodes(bsgrp_node
->partial_bsrp_list
, is_hold_time_zero
);
447 pend
= listnode_head(bsgrp_node
->partial_bsrp_list
);
449 if (!str2prefix("224.0.0.0/4", &group_all
))
452 rp_all
= pim_rp_find_match_group(pim
, &group_all
);
453 rn
= route_node_lookup(pim
->rp_table
, &bsgrp_node
->group
);
456 pim_g2rp_timer_start(pend
, pend
->rp_holdtime
);
458 /* if rp node doesn't exist or exist but not configured(rp_all),
459 * install the rp from head(if exists) of partial list. List is
460 * is sorted such that head is the elected RP for the group.
462 if (!rn
|| (prefix_same(&rp_all
->group
, &bsgrp_node
->group
)
463 && pim_rpf_addr_is_inaddr_none(&rp_all
->rp
))) {
465 zlog_debug("%s: Route node doesn't exist", __func__
);
467 pim_rp_new(pim
, pend
->rp_address
, bsgrp_node
->group
,
471 rp_info
= (struct rp_info
*)rn
->info
;
473 route_unlock_node(rn
);
475 pim_rp_new(pim
, pend
->rp_address
,
476 bsgrp_node
->group
, NULL
, RP_SRC_BSR
);
481 /* We didn't have rp node and pending list is empty(unlikely), cleanup*/
482 if ((!had_rp_node
) && (!pend
)) {
483 pim_free_bsgrp_node(bsgrp_node
->scope
->bsrp_table
,
485 pim_free_bsgrp_data(bsgrp_node
);
489 if ((had_rp_node
) && (rp_info
->rp_src
!= RP_SRC_STATIC
)) {
490 /* This means we searched and got rp node, needs unlock */
491 route_unlock_node(rn
);
493 if (active
&& pend
) {
494 if ((active
->rp_address
.s_addr
495 != pend
->rp_address
.s_addr
))
496 pim_rp_change(pim
, pend
->rp_address
,
497 bsgrp_node
->group
, RP_SRC_BSR
);
500 /* Possible when the first BSM has group with 0 rp count */
501 if ((!active
) && (!pend
)) {
504 "%s: Both bsrp and partial list are empty",
507 pim_free_bsgrp_node(bsgrp_node
->scope
->bsrp_table
,
509 pim_free_bsgrp_data(bsgrp_node
);
513 /* Possible when a group with 0 rp count received in BSM */
514 if ((active
) && (!pend
)) {
515 pim_rp_del(pim
, active
->rp_address
, bsgrp_node
->group
,
517 pim_free_bsgrp_node(bsgrp_node
->scope
->bsrp_table
,
520 zlog_debug("%s:Pend List is null,del grp node",
523 pim_free_bsgrp_data(bsgrp_node
);
528 if ((had_rp_node
) && (rp_info
->rp_src
== RP_SRC_STATIC
)) {
529 /* We need to unlock rn this case */
530 route_unlock_node(rn
);
531 /* there is a chance that static rp exist and bsrp cleaned
532 * so clean bsgrp node if pending list empty
537 "%s: Partial list is empty, static rp exists",
539 pim_free_bsgrp_node(bsgrp_node
->scope
->bsrp_table
,
541 pim_free_bsgrp_data(bsgrp_node
);
546 /* swap the list & delete all nodes in partial list (old bsrp_list)
548 * active is head of bsrp list
549 * pend is head of partial list
551 * active is head of partial list
552 * pend is head of bsrp list
553 * So check appriate head after swap and clean the new partial list
555 temp
= bsgrp_node
->bsrp_list
;
556 bsgrp_node
->bsrp_list
= bsgrp_node
->partial_bsrp_list
;
557 bsgrp_node
->partial_bsrp_list
= temp
;
560 pim_g2rp_timer_stop(active
);
561 list_delete_all_node(bsgrp_node
->partial_bsrp_list
);
565 static bool pim_bsr_rpf_check(struct pim_instance
*pim
, struct in_addr bsr
,
566 struct in_addr ip_src_addr
)
568 struct pim_nexthop nexthop
;
571 memset(&nexthop
, 0, sizeof(nexthop
));
573 /* New BSR recived */
574 if (bsr
.s_addr
!= pim
->global_scope
.current_bsr
.s_addr
) {
575 result
= pim_nexthop_match(pim
, bsr
, ip_src_addr
);
577 /* Nexthop lookup pass for the new BSR address */
582 char bsr_str
[INET_ADDRSTRLEN
];
584 pim_inet4_dump("<bsr?>", bsr
, bsr_str
, sizeof(bsr_str
));
585 zlog_debug("%s : No route to BSR address %s", __func__
,
591 return pim_nexthop_match_nht_cache(pim
, bsr
, ip_src_addr
);
594 static bool is_preferred_bsr(struct pim_instance
*pim
, struct in_addr bsr
,
597 if (bsr
.s_addr
== pim
->global_scope
.current_bsr
.s_addr
)
600 if (bsr_prio
> pim
->global_scope
.current_bsr_prio
)
603 else if (bsr_prio
== pim
->global_scope
.current_bsr_prio
) {
604 if (bsr
.s_addr
>= pim
->global_scope
.current_bsr
.s_addr
)
612 static void pim_bsm_update(struct pim_instance
*pim
, struct in_addr bsr
,
615 struct pim_nexthop_cache pnc
;
617 if (bsr
.s_addr
!= pim
->global_scope
.current_bsr
.s_addr
) {
619 char buf
[PREFIX2STR_BUFFER
];
620 bool is_bsr_tracking
= true;
622 /* De-register old BSR and register new BSR with Zebra NHT */
623 nht_p
.family
= AF_INET
;
624 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
626 if (pim
->global_scope
.current_bsr
.s_addr
!= INADDR_ANY
) {
627 nht_p
.u
.prefix4
= pim
->global_scope
.current_bsr
;
629 prefix2str(&nht_p
, buf
, sizeof(buf
));
631 "%s: Deregister BSR addr %s with Zebra NHT",
634 pim_delete_tracked_nexthop(pim
, &nht_p
, NULL
, NULL
,
638 nht_p
.u
.prefix4
= bsr
;
640 prefix2str(&nht_p
, buf
, sizeof(buf
));
642 "%s: NHT Register BSR addr %s with Zebra NHT",
646 memset(&pnc
, 0, sizeof(struct pim_nexthop_cache
));
647 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, NULL
,
648 is_bsr_tracking
, &pnc
);
649 pim
->global_scope
.current_bsr
= bsr
;
650 pim
->global_scope
.current_bsr_first_ts
=
651 pim_time_monotonic_sec();
652 pim
->global_scope
.state
= ACCEPT_PREFERRED
;
654 pim
->global_scope
.current_bsr_prio
= bsr_prio
;
655 pim
->global_scope
.current_bsr_last_ts
= pim_time_monotonic_sec();
658 static bool pim_bsm_send_intf(uint8_t *buf
, int len
, struct interface
*ifp
,
659 struct in_addr dst_addr
)
661 struct pim_interface
*pim_ifp
;
667 zlog_debug("%s: Pim interface not available for %s",
668 __func__
, ifp
->name
);
672 if (pim_ifp
->pim_sock_fd
== -1) {
674 zlog_debug("%s: Pim sock not available for %s",
675 __func__
, ifp
->name
);
679 if (pim_msg_send(pim_ifp
->pim_sock_fd
, pim_ifp
->primary_address
,
680 dst_addr
, buf
, len
, ifp
->name
)) {
681 zlog_warn("%s: Could not send BSM message on interface: %s",
682 __func__
, ifp
->name
);
686 pim_ifp
->pim_ifstat_bsm_tx
++;
687 pim_ifp
->pim
->bsm_sent
++;
691 static bool pim_bsm_frag_send(uint8_t *buf
, uint32_t len
, struct interface
*ifp
,
692 uint32_t pim_mtu
, struct in_addr dst_addr
,
695 struct bsmmsg_grpinfo
*grpinfo
, *curgrp
;
696 uint8_t *firstgrp_ptr
;
699 uint32_t parsed_len
= 0;
700 uint32_t this_pkt_rem
;
701 uint32_t copy_byte_count
;
702 uint32_t this_pkt_len
;
703 uint8_t total_rp_cnt
;
707 bool pak_pending
= false;
709 /* MTU passed here is PIM MTU (IP MTU less IP Hdr) */
710 if (pim_mtu
< (PIM_MIN_BSM_LEN
)) {
712 "%s: mtu(pim mtu: %d) size less than minimum bootstrap len",
716 "%s: mtu (pim mtu:%d) less than minimum bootstrap len",
721 pak_start
= XCALLOC(MTYPE_PIM_BSM_PKT_VAR_MEM
, pim_mtu
);
725 /* Fill PIM header later before sending packet to calc checksum */
726 pkt
+= PIM_MSG_HEADER_LEN
;
727 buf
+= PIM_MSG_HEADER_LEN
;
729 /* copy bsm header to new packet at offset of pim hdr */
730 memcpy(pkt
, buf
, PIM_BSM_HDR_LEN
);
731 pkt
+= PIM_BSM_HDR_LEN
;
732 buf
+= PIM_BSM_HDR_LEN
;
733 parsed_len
+= (PIM_MSG_HEADER_LEN
+ PIM_BSM_HDR_LEN
);
735 /* Store the position of first grp ptr, which can be reused for
736 * next packet to start filling group. old bsm header and pim hdr
737 * remains. So need not be filled again for next packet onwards.
741 /* we received mtu excluding IP hdr len as param
742 * now this_pkt_rem is mtu excluding
743 * PIM_BSM_HDR_LEN + PIM_MSG_HEADER_LEN
745 this_pkt_rem
= pim_mtu
- (PIM_BSM_HDR_LEN
+ PIM_MSG_HEADER_LEN
);
747 /* For each group till the packet length parsed */
748 while (parsed_len
< len
) {
749 /* pkt ---> fragment's current pointer
750 * buf ---> input buffer's current pointer
751 * mtu ---> size of the pim packet - PIM header
752 * curgrp ---> current group on the fragment
753 * grpinfo ---> current group on the input buffer
754 * this_pkt_rem ---> bytes remaing on the current fragment
755 * rp_fit_cnt ---> num of rp for current grp that
757 * total_rp_cnt ---> total rp present for the group in the buf
758 * frag_rp_cnt ---> no of rp for the group to be fit in
760 * this_rp_cnt ---> how many rp have we parsed
762 grpinfo
= (struct bsmmsg_grpinfo
*)buf
;
763 memcpy(pkt
, buf
, PIM_BSM_GRP_LEN
);
764 curgrp
= (struct bsmmsg_grpinfo
*)pkt
;
765 parsed_len
+= PIM_BSM_GRP_LEN
;
766 pkt
+= PIM_BSM_GRP_LEN
;
767 buf
+= PIM_BSM_GRP_LEN
;
768 this_pkt_rem
-= PIM_BSM_GRP_LEN
;
770 /* initialize rp count and total_rp_cnt before the rp loop */
772 total_rp_cnt
= grpinfo
->frag_rp_count
;
774 /* Loop till all RPs for the group parsed */
775 while (this_rp_cnt
< total_rp_cnt
) {
776 /* All RP from a group processed here.
777 * group is pointed by grpinfo.
778 * At this point make sure buf pointing to a RP
781 rp_fit_cnt
= this_pkt_rem
/ PIM_BSM_RP_LEN
;
783 /* calculate how many rp am i going to copy in
786 if (rp_fit_cnt
> (total_rp_cnt
- this_rp_cnt
))
787 frag_rp_cnt
= total_rp_cnt
- this_rp_cnt
;
789 frag_rp_cnt
= rp_fit_cnt
;
791 /* populate the frag rp count for the current grp */
792 curgrp
->frag_rp_count
= frag_rp_cnt
;
793 copy_byte_count
= frag_rp_cnt
* PIM_BSM_RP_LEN
;
795 /* copy all the rp that we are fitting in this
798 memcpy(pkt
, buf
, copy_byte_count
);
799 this_rp_cnt
+= frag_rp_cnt
;
800 buf
+= copy_byte_count
;
801 pkt
+= copy_byte_count
;
802 parsed_len
+= copy_byte_count
;
803 this_pkt_rem
-= copy_byte_count
;
805 /* Either we couldn't fit all rp for the group or the
808 if ((this_rp_cnt
< total_rp_cnt
)
810 < (PIM_BSM_GRP_LEN
+ PIM_BSM_RP_LEN
))) {
811 /* No space to fit in more rp, send this pkt */
812 this_pkt_len
= pim_mtu
- this_pkt_rem
;
813 pim_msg_build_header(pak_start
, this_pkt_len
,
814 PIM_MSG_TYPE_BOOTSTRAP
,
816 pim_bsm_send_intf(pak_start
, this_pkt_len
, ifp
,
819 /* Construct next fragment. Reuse old packet */
821 this_pkt_rem
= pim_mtu
- (PIM_BSM_HDR_LEN
822 + PIM_MSG_HEADER_LEN
);
824 /* If pkt can't accomodate next group + atleast
825 * one rp, we must break out of this inner loop
826 * and process next RP
828 if (total_rp_cnt
== this_rp_cnt
)
831 /* If some more RPs for the same group pending,
834 memcpy(pkt
, (uint8_t *)grpinfo
,
836 curgrp
= (struct bsmmsg_grpinfo
*)pkt
;
837 pkt
+= PIM_BSM_GRP_LEN
;
838 this_pkt_rem
-= PIM_BSM_GRP_LEN
;
841 /* We filled something but not yet sent out */
844 } /* while RP count */
845 } /*while parsed len */
847 /* Send if we have any unsent packet */
849 this_pkt_len
= pim_mtu
- this_pkt_rem
;
850 pim_msg_build_header(pak_start
, this_pkt_len
,
851 PIM_MSG_TYPE_BOOTSTRAP
, no_fwd
);
852 pim_bsm_send_intf(pak_start
, (pim_mtu
- this_pkt_rem
), ifp
,
855 XFREE(MTYPE_PIM_BSM_PKT_VAR_MEM
, pak_start
);
859 static void pim_bsm_fwd_whole_sz(struct pim_instance
*pim
, uint8_t *buf
,
860 uint32_t len
, int sz
)
862 struct interface
*ifp
;
863 struct pim_interface
*pim_ifp
;
864 struct in_addr dst_addr
;
869 /* For now only global scope zone is supported, so send on all
870 * pim interfaces in the vrf
872 dst_addr
= qpim_all_pim_routers_addr
;
873 FOR_ALL_INTERFACES (pim
->vrf
, ifp
) {
875 if ((!pim_ifp
) || (!pim_ifp
->bsm_enable
))
877 pim_hello_require(ifp
);
878 pim_mtu
= ifp
->mtu
- MAX_IP_HDR_LEN
;
880 ret
= pim_bsm_frag_send(buf
, len
, ifp
, pim_mtu
,
883 zlog_debug("%s: pim_bsm_frag_send returned %s",
884 __func__
, ret
? "TRUE" : "FALSE");
886 pim_msg_build_header(buf
, len
, PIM_MSG_TYPE_BOOTSTRAP
,
888 if (!pim_bsm_send_intf(buf
, len
, ifp
, dst_addr
)) {
891 "%s: pim_bsm_send_intf returned false",
898 bool pim_bsm_new_nbr_fwd(struct pim_neighbor
*neigh
, struct interface
*ifp
)
900 struct in_addr dst_addr
;
901 struct pim_interface
*pim_ifp
;
902 struct bsm_scope
*scope
;
903 struct listnode
*bsm_ln
;
904 struct bsm_info
*bsminfo
;
905 char neigh_src_str
[INET_ADDRSTRLEN
];
911 pim_inet4_dump("<src?>", neigh
->source_addr
, neigh_src_str
,
912 sizeof(neigh_src_str
));
913 zlog_debug("%s: New neighbor %s seen on %s", __func__
,
914 neigh_src_str
, ifp
->name
);
919 /* DR only forwards BSM packet */
920 if (pim_ifp
->pim_dr_addr
.s_addr
== pim_ifp
->primary_address
.s_addr
) {
923 "%s: It is not DR, so don't forward BSM packet",
927 if (!pim_ifp
->bsm_enable
) {
929 zlog_debug("%s: BSM proc not enabled on %s", __func__
,
934 scope
= &pim_ifp
->pim
->global_scope
;
936 if (!scope
->bsm_list
->count
) {
938 zlog_debug("%s: BSM list for the scope is empty",
943 if (!pim_ifp
->ucast_bsm_accept
) {
944 dst_addr
= qpim_all_pim_routers_addr
;
946 zlog_debug("%s: Sending BSM mcast to %s", __func__
,
949 dst_addr
= neigh
->source_addr
;
951 zlog_debug("%s: Sending BSM ucast to %s", __func__
,
954 pim_mtu
= ifp
->mtu
- MAX_IP_HDR_LEN
;
955 pim_hello_require(ifp
);
957 for (ALL_LIST_ELEMENTS_RO(scope
->bsm_list
, bsm_ln
, bsminfo
)) {
958 if (pim_mtu
< bsminfo
->size
) {
959 ret
= pim_bsm_frag_send(bsminfo
->bsm
, bsminfo
->size
,
960 ifp
, pim_mtu
, dst_addr
, no_fwd
);
964 "%s: pim_bsm_frag_send failed",
968 /* Pim header needs to be constructed */
969 pim_msg_build_header(bsminfo
->bsm
, bsminfo
->size
,
970 PIM_MSG_TYPE_BOOTSTRAP
, no_fwd
);
971 ret
= pim_bsm_send_intf(bsminfo
->bsm
, bsminfo
->size
,
976 "%s: pim_bsm_frag_send failed",
984 struct bsgrp_node
*pim_bsm_get_bsgrp_node(struct bsm_scope
*scope
,
987 struct route_node
*rn
;
988 struct bsgrp_node
*bsgrp
;
990 rn
= route_node_lookup(scope
->bsrp_table
, grp
);
993 zlog_debug("%s: Route node doesn't exist for the group",
998 route_unlock_node(rn
);
1003 static uint32_t hash_calc_on_grp_rp(struct prefix group
, struct in_addr rp
,
1004 uint8_t hashmasklen
)
1010 uint32_t mask
= 0xffffffff;
1012 /* mask to be made zero if hashmasklen is 0 because mask << 32
1013 * may not give 0. hashmasklen can be 0 to 32.
1015 if (hashmasklen
== 0)
1018 /* in_addr stores ip in big endian, hence network byte order
1019 * convert to uint32 before processing hash
1021 grpaddr
= ntohl(group
.u
.prefix4
.s_addr
);
1022 /* Avoid shifting by 32 bit on a 32 bit register */
1024 grpaddr
= grpaddr
& ((mask
<< (32 - hashmasklen
)));
1026 grpaddr
= grpaddr
& mask
;
1027 rp_add
= ntohl(rp
.s_addr
);
1028 temp
= 1103515245 * ((1103515245 * (uint64_t)grpaddr
+ 12345) ^ rp_add
)
1030 hash
= temp
& (0x7fffffff);
1034 static bool pim_install_bsm_grp_rp(struct pim_instance
*pim
,
1035 struct bsgrp_node
*grpnode
,
1036 struct bsmmsg_rpinfo
*rp
)
1038 struct bsm_rpinfo
*bsm_rpinfo
;
1039 uint8_t hashMask_len
= pim
->global_scope
.hashMasklen
;
1041 /*memory allocation for bsm_rpinfo */
1042 bsm_rpinfo
= XCALLOC(MTYPE_PIM_BSRP_NODE
, sizeof(*bsm_rpinfo
));
1044 bsm_rpinfo
->rp_prio
= rp
->rp_pri
;
1045 bsm_rpinfo
->rp_holdtime
= rp
->rp_holdtime
;
1046 memcpy(&bsm_rpinfo
->rp_address
, &rp
->rpaddr
.addr
,
1047 sizeof(struct in_addr
));
1048 bsm_rpinfo
->elapse_time
= 0;
1050 /* Back pointer to the group node. */
1051 bsm_rpinfo
->bsgrp_node
= grpnode
;
1053 /* update hash for this rp node */
1054 bsm_rpinfo
->hash
= hash_calc_on_grp_rp(grpnode
->group
, rp
->rpaddr
.addr
,
1056 if (listnode_add_sort_nodup(grpnode
->partial_bsrp_list
, bsm_rpinfo
)) {
1059 "%s, bs_rpinfo node added to the partial bs_rplist.\r\n",
1065 zlog_debug("%s: list node not added\n", __func__
);
1067 XFREE(MTYPE_PIM_BSRP_NODE
, bsm_rpinfo
);
1071 static void pim_update_pending_rp_cnt(struct bsm_scope
*sz
,
1072 struct bsgrp_node
*bsgrp
,
1073 uint16_t bsm_frag_tag
,
1074 uint32_t total_rp_count
)
1076 if (bsgrp
->pend_rp_cnt
) {
1077 /* received bsm is different packet ,
1078 * it is not same fragment.
1080 if (bsm_frag_tag
!= bsgrp
->frag_tag
) {
1083 "%s,Received a new BSM ,so clear the pending bs_rpinfo list.\r\n",
1085 list_delete_all_node(bsgrp
->partial_bsrp_list
);
1086 bsgrp
->pend_rp_cnt
= total_rp_count
;
1089 bsgrp
->pend_rp_cnt
= total_rp_count
;
1091 bsgrp
->frag_tag
= bsm_frag_tag
;
1094 /* Parsing BSR packet and adding to partial list of corresponding bsgrp node */
1095 static bool pim_bsm_parse_install_g2rp(struct bsm_scope
*scope
, uint8_t *buf
,
1096 int buflen
, uint16_t bsm_frag_tag
)
1098 struct bsmmsg_grpinfo grpinfo
;
1099 struct bsmmsg_rpinfo rpinfo
;
1100 struct prefix group
;
1101 struct bsgrp_node
*bsgrp
= NULL
;
1102 int frag_rp_cnt
= 0;
1106 while (buflen
> offset
) {
1107 if (offset
+ (int)sizeof(struct bsmmsg_grpinfo
) > buflen
) {
1110 "%s: buflen received %d is less than the internal data structure of the packet would suggest",
1114 /* Extract Group tlv from BSM */
1115 memcpy(&grpinfo
, buf
, sizeof(struct bsmmsg_grpinfo
));
1117 if (PIM_DEBUG_BSM
) {
1118 char grp_str
[INET_ADDRSTRLEN
];
1120 pim_inet4_dump("<Group?>", grpinfo
.group
.addr
, grp_str
,
1123 "%s, Group %s Rpcount:%d Fragment-Rp-count:%d\r\n",
1124 __func__
, grp_str
, grpinfo
.rp_count
,
1125 grpinfo
.frag_rp_count
);
1128 buf
+= sizeof(struct bsmmsg_grpinfo
);
1129 offset
+= sizeof(struct bsmmsg_grpinfo
);
1131 if (grpinfo
.rp_count
== 0) {
1132 if (PIM_DEBUG_BSM
) {
1133 char grp_str
[INET_ADDRSTRLEN
];
1135 pim_inet4_dump("<Group?>", grpinfo
.group
.addr
,
1136 grp_str
, sizeof(grp_str
));
1138 "%s, Rp count is zero for group: %s\r\n",
1144 group
.family
= AF_INET
;
1145 if (grpinfo
.group
.mask
> IPV4_MAX_BITLEN
) {
1148 "%s, v4 prefix length specified: %d is too long",
1149 __func__
, grpinfo
.group
.mask
);
1152 group
.prefixlen
= grpinfo
.group
.mask
;
1153 group
.u
.prefix4
.s_addr
= grpinfo
.group
.addr
.s_addr
;
1155 /* Get the Group node for the BSM rp table */
1156 bsgrp
= pim_bsm_get_bsgrp_node(scope
, &group
);
1161 "%s, Create new BSM Group node.\r\n",
1164 /* create a new node to be added to the tree. */
1165 bsgrp
= pim_bsm_new_bsgrp_node(scope
->bsrp_table
,
1170 "%s, Failed to get the BSM group node.\r\n",
1175 bsgrp
->scope
= scope
;
1178 pim_update_pending_rp_cnt(scope
, bsgrp
, bsm_frag_tag
,
1180 frag_rp_cnt
= grpinfo
.frag_rp_count
;
1183 while (frag_rp_cnt
--) {
1184 if (offset
+ (int)sizeof(struct bsmmsg_rpinfo
)
1188 "%s, buflen received: %u is less than the internal data structure of the packet would suggest",
1193 /* Extract RP address tlv from BSM */
1194 memcpy(&rpinfo
, buf
, sizeof(struct bsmmsg_rpinfo
));
1195 rpinfo
.rp_holdtime
= ntohs(rpinfo
.rp_holdtime
);
1196 buf
+= sizeof(struct bsmmsg_rpinfo
);
1197 offset
+= sizeof(struct bsmmsg_rpinfo
);
1199 if (PIM_DEBUG_BSM
) {
1200 char rp_str
[INET_ADDRSTRLEN
];
1202 pim_inet4_dump("<Rpaddr?>", rpinfo
.rpaddr
.addr
,
1203 rp_str
, sizeof(rp_str
));
1205 "%s, Rp address - %s; pri:%d hold:%d\r\n",
1206 __func__
, rp_str
, rpinfo
.rp_pri
,
1207 rpinfo
.rp_holdtime
);
1210 /* Call Install api to update grp-rp mappings */
1211 if (pim_install_bsm_grp_rp(scope
->pim
, bsgrp
, &rpinfo
))
1215 bsgrp
->pend_rp_cnt
-= ins_count
;
1217 if (!bsgrp
->pend_rp_cnt
) {
1220 "%s, Recvd all the rps for this group, so bsrp list with penidng rp list.",
1222 /* replace the bsrp_list with pending list */
1223 pim_instate_pend_list(bsgrp
);
1229 int pim_bsm_process(struct interface
*ifp
, struct ip
*ip_hdr
, uint8_t *buf
,
1230 uint32_t buf_size
, bool no_fwd
)
1232 struct bsm_hdr
*bshdr
;
1233 int sz
= PIM_GBL_SZ_ID
;
1234 struct bsmmsg_grpinfo
*msg_grp
;
1235 struct pim_interface
*pim_ifp
= NULL
;
1236 struct bsm_info
*bsminfo
;
1237 struct pim_instance
*pim
;
1238 char bsr_str
[INET_ADDRSTRLEN
];
1240 bool empty_bsm
= false;
1242 /* BSM Packet acceptance validation */
1243 pim_ifp
= ifp
->info
;
1246 zlog_debug("%s: multicast not enabled on interface %s",
1247 __func__
, ifp
->name
);
1251 pim_ifp
->pim_ifstat_bsm_rx
++;
1255 /* Drop if bsm processing is disabled on interface */
1256 if (!pim_ifp
->bsm_enable
) {
1257 zlog_warn("%s: BSM not enabled on interface %s", __func__
,
1259 pim_ifp
->pim_ifstat_bsm_cfg_miss
++;
1264 if (buf_size
< (PIM_MSG_HEADER_LEN
+ sizeof(struct bsm_hdr
))) {
1267 "%s: received buffer length of %d which is too small to properly decode",
1268 __func__
, buf_size
);
1272 bshdr
= (struct bsm_hdr
*)(buf
+ PIM_MSG_HEADER_LEN
);
1273 pim_inet4_dump("<bsr?>", bshdr
->bsr_addr
.addr
, bsr_str
,
1275 pim
->global_scope
.hashMasklen
= bshdr
->hm_len
;
1276 frag_tag
= ntohs(bshdr
->frag_tag
);
1278 /* Identify empty BSM */
1279 if ((buf_size
- PIM_BSM_HDR_LEN
- PIM_MSG_HEADER_LEN
) < PIM_BSM_GRP_LEN
)
1283 msg_grp
= (struct bsmmsg_grpinfo
*)(buf
+ PIM_MSG_HEADER_LEN
1285 /* Currently we don't support scope zoned BSM */
1286 if (msg_grp
->group
.sz
) {
1289 "%s : Administratively scoped range BSM received",
1291 pim_ifp
->pim_ifstat_bsm_invalid_sz
++;
1297 /* Drop if bsr is not preferred bsr */
1298 if (!is_preferred_bsr(pim
, bshdr
->bsr_addr
.addr
, bshdr
->bsr_prio
)) {
1300 zlog_debug("%s : Received a non-preferred BSM",
1307 /* only accept no-forward BSM if quick refresh on startup */
1308 if ((pim
->global_scope
.accept_nofwd_bsm
)
1309 || (frag_tag
== pim
->global_scope
.bsm_frag_tag
)) {
1310 pim
->global_scope
.accept_nofwd_bsm
= false;
1314 "%s : nofwd_bsm received on %s when accpt_nofwd_bsm false",
1317 pim_ifp
->pim_ifstat_ucast_bsm_cfg_miss
++;
1322 /* Mulicast BSM received */
1323 if (ip_hdr
->ip_dst
.s_addr
== qpim_all_pim_routers_addr
.s_addr
) {
1325 if (!pim_bsr_rpf_check(pim
, bshdr
->bsr_addr
.addr
,
1329 "%s : RPF check fail for BSR address %s",
1335 } else if (if_lookup_exact_address(&ip_hdr
->ip_dst
, AF_INET
,
1337 /* Unicast BSM received - if ucast bsm not enabled on
1338 * the interface, drop it
1340 if (!pim_ifp
->ucast_bsm_accept
) {
1343 "%s : Unicast BSM not enabled on interface %s",
1344 __func__
, ifp
->name
);
1345 pim_ifp
->pim_ifstat_ucast_bsm_cfg_miss
++;
1352 zlog_debug("%s : Invalid destination address",
1360 zlog_debug("%s : Empty Pref BSM received", __func__
);
1362 /* Parse Update bsm rp table and install/uninstall rp if required */
1363 if (!pim_bsm_parse_install_g2rp(
1364 &pim_ifp
->pim
->global_scope
,
1365 (buf
+ PIM_BSM_HDR_LEN
+ PIM_MSG_HEADER_LEN
),
1366 (buf_size
- PIM_BSM_HDR_LEN
- PIM_MSG_HEADER_LEN
),
1368 if (PIM_DEBUG_BSM
) {
1369 zlog_debug("%s, Parsing BSM failed.\r\n", __func__
);
1374 /* Restart the bootstrap timer */
1375 pim_bs_timer_restart(&pim_ifp
->pim
->global_scope
,
1376 PIM_BSR_DEFAULT_TIMEOUT
);
1378 /* If new BSM received, clear the old bsm database */
1379 if (pim_ifp
->pim
->global_scope
.bsm_frag_tag
!= frag_tag
) {
1380 if (PIM_DEBUG_BSM
) {
1381 zlog_debug("%s: Current frag tag: %d Frag teg rcvd: %d",
1383 pim_ifp
->pim
->global_scope
.bsm_frag_tag
,
1386 list_delete_all_node(pim_ifp
->pim
->global_scope
.bsm_list
);
1387 pim_ifp
->pim
->global_scope
.bsm_frag_tag
= frag_tag
;
1390 /* update the scope information from bsm */
1391 pim_bsm_update(pim
, bshdr
->bsr_addr
.addr
, bshdr
->bsr_prio
);
1394 pim_bsm_fwd_whole_sz(pim_ifp
->pim
, buf
, buf_size
, sz
);
1395 bsminfo
= XCALLOC(MTYPE_PIM_BSM_INFO
, sizeof(struct bsm_info
));
1397 bsminfo
->bsm
= XCALLOC(MTYPE_PIM_BSM_PKT_VAR_MEM
, buf_size
);
1399 bsminfo
->size
= buf_size
;
1400 memcpy(bsminfo
->bsm
, buf
, buf_size
);
1401 listnode_add(pim_ifp
->pim
->global_scope
.bsm_list
, bsminfo
);