Skip to content

Commit

Permalink
Merge pull request #11855 from cscarpitta/fix-srv6-memleaks
Browse files Browse the repository at this point in the history
*: Fix several memory leaks in SRv6 implementation
  • Loading branch information
ton31337 authored Aug 29, 2022
2 parents ad70aa8 + 877682e commit c29b1ce
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 deletions.
22 changes: 20 additions & 2 deletions bgpd/bgp_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,16 @@ static int bgp_srv6_locator_unset(struct bgp *bgp)
return -1;

/* refresh chunks */
for (ALL_LIST_ELEMENTS(bgp->srv6_locator_chunks, node, nnode, chunk))
for (ALL_LIST_ELEMENTS(bgp->srv6_locator_chunks, node, nnode, chunk)) {
listnode_delete(bgp->srv6_locator_chunks, chunk);
srv6_locator_chunk_free(chunk);
}

/* refresh functions */
for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func))
for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func)) {
listnode_delete(bgp->srv6_functions, func);
XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
}

/* refresh tovpn_sid */
for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
Expand All @@ -334,6 +338,20 @@ static int bgp_srv6_locator_unset(struct bgp *bgp)
/* update vpn bgp processes */
vpn_leak_postchange_all();

/* refresh tovpn_sid_locator */
for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF)
continue;

/* refresh vpnv4 tovpn_sid_locator */
XFREE(MTYPE_BGP_SRV6_SID,
bgp_vrf->vpn_policy[AFI_IP].tovpn_sid_locator);

/* refresh vpnv6 tovpn_sid_locator */
XFREE(MTYPE_BGP_SRV6_SID,
bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid_locator);
}

/* clear locator name */
memset(bgp->srv6_locator_name, 0, sizeof(bgp->srv6_locator_name));

Expand Down
41 changes: 38 additions & 3 deletions bgpd/bgp_zebra.c
Original file line number Diff line number Diff line change
Expand Up @@ -3209,7 +3209,7 @@ static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS)
struct srv6_locator_chunk *chunk;
struct bgp_srv6_function *func;
struct bgp *bgp_vrf;
struct in6_addr *tovpn_sid;
struct in6_addr *tovpn_sid, *tovpn_sid_locator;
struct prefix_ipv6 tmp_prefi;

if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
Expand All @@ -3218,17 +3218,21 @@ static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS)
// refresh chunks
for (ALL_LIST_ELEMENTS(bgp->srv6_locator_chunks, node, nnode, chunk))
if (prefix_match((struct prefix *)&loc.prefix,
(struct prefix *)&chunk->prefix))
(struct prefix *)&chunk->prefix)) {
listnode_delete(bgp->srv6_locator_chunks, chunk);
srv6_locator_chunk_free(chunk);
}

// refresh functions
for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func)) {
tmp_prefi.family = AF_INET6;
tmp_prefi.prefixlen = 128;
tmp_prefi.prefix = func->sid;
if (prefix_match((struct prefix *)&loc.prefix,
(struct prefix *)&tmp_prefi))
(struct prefix *)&tmp_prefi)) {
listnode_delete(bgp->srv6_functions, func);
XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
}
}

// refresh tovpn_sid
Expand Down Expand Up @@ -3262,6 +3266,37 @@ static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS)
}

vpn_leak_postchange_all();

/* refresh tovpn_sid_locator */
for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF)
continue;

/* refresh vpnv4 tovpn_sid_locator */
tovpn_sid_locator =
bgp_vrf->vpn_policy[AFI_IP].tovpn_sid_locator;
if (tovpn_sid_locator) {
tmp_prefi.family = AF_INET6;
tmp_prefi.prefixlen = IPV6_MAX_BITLEN;
tmp_prefi.prefix = *tovpn_sid_locator;
if (prefix_match((struct prefix *)&loc.prefix,
(struct prefix *)&tmp_prefi))
XFREE(MTYPE_BGP_SRV6_SID, tovpn_sid_locator);
}

/* refresh vpnv6 tovpn_sid_locator */
tovpn_sid_locator =
bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid_locator;
if (tovpn_sid_locator) {
tmp_prefi.family = AF_INET6;
tmp_prefi.prefixlen = IPV6_MAX_BITLEN;
tmp_prefi.prefix = *tovpn_sid_locator;
if (prefix_match((struct prefix *)&loc.prefix,
(struct prefix *)&tmp_prefi))
XFREE(MTYPE_BGP_SRV6_SID, tovpn_sid_locator);
}
}

return 0;
}

Expand Down
16 changes: 10 additions & 6 deletions lib/zclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ enum zclient_send_status zclient_send_localsid(struct zclient *zclient,
{
struct prefix_ipv6 p = {};
struct zapi_route api = {};
struct nexthop nh = {};
struct zapi_nexthop *znh;

p.family = AF_INET6;
p.prefixlen = IPV6_MAX_BITLEN;
Expand All @@ -465,12 +465,16 @@ enum zclient_send_status zclient_send_localsid(struct zclient *zclient,
SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);

nh.type = NEXTHOP_TYPE_IFINDEX;
nh.ifindex = oif;
SET_FLAG(nh.flags, ZAPI_NEXTHOP_FLAG_SEG6LOCAL);
nexthop_add_srv6_seg6local(&nh, action, context);
znh = &api.nexthops[0];

memset(znh, 0, sizeof(*znh));

znh->type = NEXTHOP_TYPE_IFINDEX;
znh->ifindex = oif;
SET_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_SEG6LOCAL);
znh->seg6local_action = action;
memcpy(&znh->seg6local_ctx, context, sizeof(struct seg6local_context));

zapi_nexthop_from_nexthop(&api.nexthops[0], &nh);
api.nexthop_num = 1;

return zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
Expand Down
14 changes: 14 additions & 0 deletions sharpd/sharp_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ DEFPY (install_routes,

memset(&prefix, 0, sizeof(prefix));
memset(&sg.r.orig_prefix, 0, sizeof(sg.r.orig_prefix));
nexthop_del_srv6_seg6local(&sg.r.nhop);
nexthop_del_srv6_seg6(&sg.r.nhop);
memset(&sg.r.nhop, 0, sizeof(sg.r.nhop));
memset(&sg.r.nhop_group, 0, sizeof(sg.r.nhop_group));
memset(&sg.r.backup_nhop, 0, sizeof(sg.r.nhop));
Expand Down Expand Up @@ -376,6 +378,8 @@ DEFPY (install_seg6_routes,

memset(&prefix, 0, sizeof(prefix));
memset(&sg.r.orig_prefix, 0, sizeof(sg.r.orig_prefix));
nexthop_del_srv6_seg6local(&sg.r.nhop);
nexthop_del_srv6_seg6(&sg.r.nhop);
memset(&sg.r.nhop, 0, sizeof(sg.r.nhop));
memset(&sg.r.nhop_group, 0, sizeof(sg.r.nhop_group));
memset(&sg.r.backup_nhop, 0, sizeof(sg.r.nhop));
Expand Down Expand Up @@ -467,6 +471,8 @@ DEFPY (install_seg6local_routes,
sg.r.repeat = 0;

memset(&sg.r.orig_prefix, 0, sizeof(sg.r.orig_prefix));
nexthop_del_srv6_seg6local(&sg.r.nhop);
nexthop_del_srv6_seg6(&sg.r.nhop);
memset(&sg.r.nhop, 0, sizeof(sg.r.nhop));
memset(&sg.r.nhop_group, 0, sizeof(sg.r.nhop_group));
memset(&sg.r.backup_nhop, 0, sizeof(sg.r.nhop));
Expand Down Expand Up @@ -924,6 +930,11 @@ DEFPY (import_te,
return CMD_SUCCESS;
}

static void sharp_srv6_locator_chunk_free(struct prefix_ipv6 *chunk)
{
prefix_ipv6_free((struct prefix_ipv6 **)&chunk);
}

DEFPY (sharp_srv6_manager_get_locator_chunk,
sharp_srv6_manager_get_locator_chunk_cmd,
"sharp srv6-manager get-locator-chunk NAME$locator_name",
Expand All @@ -947,6 +958,8 @@ DEFPY (sharp_srv6_manager_get_locator_chunk,
loc = XCALLOC(MTYPE_SRV6_LOCATOR,
sizeof(struct sharp_srv6_locator));
loc->chunks = list_new();
loc->chunks->del =
(void (*)(void *))sharp_srv6_locator_chunk_free;
snprintf(loc->name, SRV6_LOCNAME_SIZE, "%s", locator_name);
listnode_add(sg.srv6_locators, loc);
}
Expand Down Expand Up @@ -1096,6 +1109,7 @@ DEFPY (sharp_srv6_manager_release_locator_chunk,
list_delete_all_node(loc->chunks);
list_delete(&loc->chunks);
listnode_delete(sg.srv6_locators, loc);
XFREE(MTYPE_SRV6_LOCATOR, loc);
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions zebra/zebra_srv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ void zebra_srv6_locator_delete(struct srv6_locator *locator)
}

listnode_delete(srv6->locators, locator);
srv6_locator_free(locator);
}

struct srv6_locator *zebra_srv6_locator_lookup(const char *name)
Expand Down

0 comments on commit c29b1ce

Please sign in to comment.