Commit 7e3e9fd462 for frr

commit 7e3e9fd4624580842f40270e2f29545ebf74d889
Author: Donatas Abraitis <donatas@opensourcerouting.org>
Date:   Wed Nov 18 14:10:21 2026 +0200

    bgpd: Enforce maximum-prefix for labeled-unicast neighbors

    Pass orig_safi to bgp_maximum_prefix_overflow() (as the inbound
    policy, filter and route-map lookups already do), and let
    bgp_maximum_prefix_overflow()/bgp_filtered_routes_count() read pcount
    and walk the RIB under the unicast safi for labeled-unicast, the same
    way `show` code does via pfx_rcd_safi.

    Fixes: ANT-2026-VN6L1NXR
    Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>

diff --git a/bgpd/bgp_label.c b/bgpd/bgp_label.c
index 6b9844a812..60aca032fa 100644
--- a/bgpd/bgp_label.c
+++ b/bgpd/bgp_label.c
@@ -613,6 +613,12 @@ int bgp_nlri_parse_label(struct peer *peer, struct attr *attr,
 			bgp_withdraw(peer, &p, addpath_id, packet->afi, SAFI_UNICAST,
 				     ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, NULL, labels, num_labels);
 		}
+
+		/* Do not send BGP notification twice when maximum-prefix count
+		 * overflow.
+		 */
+		if (CHECK_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW))
+			return BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW;
 	}

 	/* Packet length consistency check. */
diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index bcefcd4245..c718116f31 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -5652,7 +5652,9 @@ static uint32_t bgp_filtered_routes_count(struct peer *peer, afi_t afi,
 	struct bgp_dest *dest;
 	struct bgp_adj_in *ain;
 	struct attr attr = {};
-	struct bgp_table *table = peer->bgp->rib[afi][safi];
+	/* labeled-unicast routes are installed in the unicast table */
+	safi_t pfx_rcd_safi = (safi == SAFI_LABELED_UNICAST) ? SAFI_UNICAST : safi;
+	struct bgp_table *table = peer->bgp->rib[afi][pfx_rcd_safi];

 	for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
 		for (ain = dest->adj_in; ain; ain = ain->next) {
@@ -5690,11 +5692,12 @@ bool bgp_maximum_prefix_overflow(struct peer *peer, afi_t afi, safi_t safi,
 {
 	iana_afi_t pkt_afi;
 	iana_safi_t pkt_safi;
-	uint32_t pcount = (CHECK_FLAG(peer->af_flags[afi][safi],
-				      PEER_FLAG_MAX_PREFIX_FORCE))
-				  ? bgp_filtered_routes_count(peer, afi, safi)
-					    + peer->pcount[afi][safi]
-				  : peer->pcount[afi][safi];
+	/* labeled-unicast routes are installed in the unicast table */
+	safi_t pfx_rcd_safi = (safi == SAFI_LABELED_UNICAST) ? SAFI_UNICAST : safi;
+	uint32_t pcount = (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_FORCE))
+				  ? bgp_filtered_routes_count(peer, afi, safi) +
+					    peer->pcount[afi][pfx_rcd_safi]
+				  : peer->pcount[afi][pfx_rcd_safi];
 	struct peer_connection *connection = peer->connection;

 	if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX))
@@ -7108,9 +7111,10 @@ void bgp_update(struct peer *peer, const struct prefix *p, uint32_t addpath_id,
 	bgp_update_check_valid_flags(bgp, peer, dest, p, afi, safi, new, attr_new,
 				     bgp_nht_param_prefix, accept_own);
 	/* If maximum prefix count is configured and current prefix
-	 * count exceed it.
+	 * count exceed it. The limit is configured under the peer's own
+	 * safi (labeled-unicast included), not the table's.
 	 */
-	if (bgp_maximum_prefix_overflow(peer, afi, safi, 0)) {
+	if (bgp_maximum_prefix_overflow(peer, afi, orig_safi, 0)) {
 		reason = "maximum-prefix overflow";
 		bgp_attr_flush(&new_attr);
 		goto filtered;