From f6a460f982e9694f4713476d217f0ae810663a4f Mon Sep 17 00:00:00 2001 From: Samanvitha B Bhargav Date: Wed, 21 Sep 2022 00:54:14 -0700 Subject: [PATCH 1/4] bgpd: memory leak issue fix When router id is changed through config, new RD is auto generated. However new export RT was being assigned without freeing the older RT from VRF. Signed-off-by: Samanvitha B Bhargav --- bgpd/bgp_mplsvpn.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bgpd/bgp_mplsvpn.c b/bgpd/bgp_mplsvpn.c index 5a039b25bc..d7fd4bc77e 100644 --- a/bgpd/bgp_mplsvpn.c +++ b/bgpd/bgp_mplsvpn.c @@ -1988,9 +1988,15 @@ void vpn_handle_router_id_update(struct bgp *bgp, bool withdraw, bgp->vpn_policy[afi].tovpn_rd = bgp->vrf_prd_auto; prefix_rd2str(&bgp->vpn_policy[afi].tovpn_rd, buf, sizeof(buf)); - bgp->vpn_policy[afi].rtlist[edir] = - ecommunity_str2com(buf, - ECOMMUNITY_ROUTE_TARGET, 0); + + /* free up pre-existing memory if any and allocate + * the ecommunity attribute with new RD/RT + */ + if (bgp->vpn_policy[afi].rtlist[edir]) + ecommunity_free( + &bgp->vpn_policy[afi].rtlist[edir]); + bgp->vpn_policy[afi].rtlist[edir] = ecommunity_str2com( + buf, ECOMMUNITY_ROUTE_TARGET, 0); /* Update import_vrf rt_list */ ecom = bgp->vpn_policy[afi].rtlist[edir]; From 92d537611b2cd13c51d1d117f25c63f2b4480e50 Mon Sep 17 00:00:00 2001 From: Samanvitha B Bhargav Date: Wed, 21 Sep 2022 01:45:41 -0700 Subject: [PATCH 2/4] bgpd: memory leak issue fix In ecommunity_del_val(), ecommunity was not being freed when the last value in the ecommunity was being deleted. Signed-off-by: Samanvitha B Bhargav --- bgpd/bgp_ecommunity.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bgpd/bgp_ecommunity.c b/bgpd/bgp_ecommunity.c index f57e9ae88b..589d9af1e5 100644 --- a/bgpd/bgp_ecommunity.c +++ b/bgpd/bgp_ecommunity.c @@ -1383,7 +1383,7 @@ bool ecommunity_del_val(struct ecommunity *ecom, struct ecommunity_val *eval) XFREE(MTYPE_ECOMMUNITY_VAL, ecom->val); ecom->val = p; } else - ecom->val = NULL; + XFREE(MTYPE_ECOMMUNITY_VAL, ecom->val); return true; } From 2305e3e46097474246a279c05bd62b8573f83a08 Mon Sep 17 00:00:00 2001 From: Samanvitha B Bhargav Date: Wed, 21 Sep 2022 01:57:09 -0700 Subject: [PATCH 3/4] bgpd: memory leak issue fix Memory allocated when 'import vrf route maps <>' is configured, wasn't being freed when the entire bgp config was deleted through 'no router bgp'. Signed-off-by: Samanvitha B Bhargav --- bgpd/bgpd.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c index 4c151b2d37..919d3f034d 100644 --- a/bgpd/bgpd.c +++ b/bgpd/bgpd.c @@ -3713,6 +3713,17 @@ int bgp_delete(struct bgp *bgp) bgp->vpn_policy[afi].import_redirect_rtlist = NULL; } + /* Free any memory allocated to holding routemap references */ + for (afi = 0; afi < AFI_MAX; ++afi) { + for (enum vpn_policy_direction dir = 0; + dir < BGP_VPN_POLICY_DIR_MAX; ++dir) { + if (bgp->vpn_policy[afi].rmap_name[dir]) + XFREE(MTYPE_ROUTE_MAP_NAME, + bgp->vpn_policy[afi].rmap_name[dir]); + bgp->vpn_policy[afi].rmap[dir] = NULL; + } + } + /* Deregister from Zebra, if needed */ if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) { if (BGP_DEBUG(zebra, ZEBRA)) From b96b4f1c5fa9ad2ba78b3b9f602dcdc794296309 Mon Sep 17 00:00:00 2001 From: Samanvitha B Bhargav Date: Wed, 21 Sep 2022 03:18:06 -0700 Subject: [PATCH 4/4] bgpd: Cleanup memory leaks associated with t_deferral_timer We are allocating temporary memory for information about what to process in this thread, which is not being cleaned up on thread cancelling. Signed-off-by: Samanvitha B Bhargav --- bgpd/bgp_fsm.c | 5 +++++ bgpd/bgp_packet.c | 5 +++++ bgpd/bgpd.c | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/bgpd/bgp_fsm.c b/bgpd/bgp_fsm.c index a8accc25f5..ddda100774 100644 --- a/bgpd/bgp_fsm.c +++ b/bgpd/bgp_fsm.c @@ -1464,6 +1464,11 @@ int bgp_stop(struct peer *peer) /* There is no pending EOR message */ if (gr_info->eor_required == 0) { + if (gr_info->t_select_deferral) { + void *info = THREAD_ARG( + gr_info->t_select_deferral); + XFREE(MTYPE_TMP, info); + } THREAD_OFF(gr_info->t_select_deferral); gr_info->eor_received = 0; } diff --git a/bgpd/bgp_packet.c b/bgpd/bgp_packet.c index cc18808373..8ae31bf2e6 100644 --- a/bgpd/bgp_packet.c +++ b/bgpd/bgp_packet.c @@ -2054,6 +2054,11 @@ static int bgp_update_receive(struct peer *peer, bgp_size_t size) gr_info->eor_required, "EOR RCV", gr_info->eor_received); + if (gr_info->t_select_deferral) { + void *info = THREAD_ARG( + gr_info->t_select_deferral); + XFREE(MTYPE_TMP, info); + } THREAD_OFF(gr_info->t_select_deferral); gr_info->eor_required = 0; gr_info->eor_received = 0; diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c index 919d3f034d..94cf12272a 100644 --- a/bgpd/bgpd.c +++ b/bgpd/bgpd.c @@ -3630,7 +3630,12 @@ int bgp_delete(struct bgp *bgp) gr_info = &bgp->gr_info[afi][safi]; if (!gr_info) continue; + t = gr_info->t_select_deferral; + if (t) { + void *info = THREAD_ARG(t); + XFREE(MTYPE_TMP, info); + } THREAD_OFF(gr_info->t_select_deferral); t = gr_info->t_route_select;