From 6a3914f33c73f5c86856b12b2d4d21dec727e518 Mon Sep 17 00:00:00 2001 From: zhou-run Date: Fri, 13 Sep 2024 18:01:43 +0800 Subject: [PATCH] zebra: Fix NHLFE entry memory leaks Before adding an NHLFE entry, zebra calls nhlfe_nexthop_active_ipv4()/nhlfe_nexthop_active_ipv6() to check if there is a corresponding directly connected route for the next hop prefix of the NHLFE. If such a route exists, it directly overwrites the output interface of the NHLFE's next hop with the output interface of the directly connected route. If the output interface of the directly connected route is different from the output interface of the NHLFE's next hop, deleting the NHLFE entry later will fail because nhlfe_nhop_match() cannot find the same next hop address and output interface, leading to an NHLFE entry leak. If the outbound interface of the directly connected route differs from the outbound interface of the NHLFE's next hop, we should retain the outbound interface without activating the next hop, rather than directly overwriting it. Taking ISIS as an example, establishing neighbors in ISIS only requires the existence of the neighbor's IP address and does not need the neighbor's IP address to be in the same subnet as the local IP address. When zebra creates an adjacency label entry, it uses the neighbor's IP address as the next hop. If the neighbor's IP address is not in the same subnet as the local IP address and there is a local directly connected route with the neighbor's IP address prefix, it will result in an issue where the corresponding adjacency label entry in zebra cannot be deleted. Signed-off-by: zhou-run --- zebra/zebra_mpls.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/zebra/zebra_mpls.c b/zebra/zebra_mpls.c index 9549af5f14bc..54d737457ab3 100644 --- a/zebra/zebra_mpls.c +++ b/zebra/zebra_mpls.c @@ -663,7 +663,7 @@ static int nhlfe_nexthop_active_ipv4(struct zebra_nhlfe *nhlfe, route_unlock_node(rn); - /* Locate a valid connected route. */ + /* Locate a valid route. */ RNODE_FOREACH_RE (rn, match) { if (CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED) || !CHECK_FLAG(match->flags, ZEBRA_FLAG_SELECTED)) @@ -671,12 +671,8 @@ static int nhlfe_nexthop_active_ipv4(struct zebra_nhlfe *nhlfe, for (match_nh = match->nhe->nhg.nexthop; match_nh; match_nh = match_nh->next) { - if ((match->type == ZEBRA_ROUTE_CONNECT || - match->type == ZEBRA_ROUTE_LOCAL) || - nexthop->ifindex == match_nh->ifindex) { - nexthop->ifindex = match_nh->ifindex; + if (nexthop->ifindex == match_nh->ifindex) return 1; - } } } @@ -713,11 +709,9 @@ static int nhlfe_nexthop_active_ipv6(struct zebra_nhlfe *nhlfe, route_unlock_node(rn); - /* Locate a valid connected route. */ + /* Locate a valid route. */ RNODE_FOREACH_RE (rn, match) { - if (((match->type == ZEBRA_ROUTE_CONNECT || - match->type == ZEBRA_ROUTE_LOCAL)) && - !CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED) && + if (!CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED) && CHECK_FLAG(match->flags, ZEBRA_FLAG_SELECTED)) break; } @@ -725,7 +719,9 @@ static int nhlfe_nexthop_active_ipv6(struct zebra_nhlfe *nhlfe, if (!match || !match->nhe->nhg.nexthop) return 0; - nexthop->ifindex = match->nhe->nhg.nexthop->ifindex; + if (nexthop->ifindex != match->nhe->nhg.nexthop->ifindex) + return 0; + return 1; }