Commit 916fb3e701 for frr

commit 916fb3e701a83a3b1e2fd436afe494c7f3520e58
Author: Donatas Abraitis <donatas@opensourcerouting.org>
Date:   Wed Dec 2 14:21:09 2026 +0200

    bgpd: Apply Role checks only if the Role capability is advertised

    RFC 9234, 4.2 says the Role correctness check and the strict mode apply
    if the Role capability is advertised (sent). bgp_role_violation() checked
    only whether the Role is configured, but our OPEN does not carry any
    capability with `neighbor PEER dont-capability-negotiate`, or after the
    neighbor rejected the OPEN with Unsupported Optional Parameter.

    Add bgp_role_advertise() and use it for both sending the Role capability
    and checking the received one, so they cannot diverge. PEER_CAP_ROLE_ADV
    can't be used for that, because with DelayOpen the received OPEN is
    checked before our OPEN is sent, so strict-mode would be bypassed.

    Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>

diff --git a/bgpd/bgp_open.c b/bgpd/bgp_open.c
index 833a2d0a96..3477d55ab0 100644
--- a/bgpd/bgp_open.c
+++ b/bgpd/bgp_open.c
@@ -1369,21 +1369,32 @@ static bool strict_capability_same(struct peer *peer)
 	return true;
 }

+static bool bgp_open_capability_send(const struct peer *peer)
+{
+	return CHECK_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN) &&
+	       !CHECK_FLAG(peer->flags, PEER_FLAG_DONT_CAPABILITY);
+}
+
+static bool bgp_role_advertise(const struct peer *peer)
+{
+	return bgp_open_capability_send(peer) && CHECK_FLAG(peer->flags, PEER_FLAG_ROLE);
+}

 bool bgp_role_violation(struct peer_connection *connection)
 {
 	struct peer *peer = connection->peer;
 	uint8_t local_role = peer->local_role;
 	uint8_t remote_role = peer->remote_role;
-	bool role_cfgd = CHECK_FLAG(peer->flags, PEER_FLAG_ROLE);
 	bool role_rcvd = CHECK_FLAG(peer->cap, PEER_CAP_ROLE_RCV);

-	if (role_cfgd && role_rcvd &&
-	    !((local_role == ROLE_PEER && remote_role == ROLE_PEER) ||
-	      (local_role == ROLE_PROVIDER && remote_role == ROLE_CUSTOMER) ||
-	      (local_role == ROLE_CUSTOMER && remote_role == ROLE_PROVIDER) ||
-	      (local_role == ROLE_RS_SERVER && remote_role == ROLE_RS_CLIENT) ||
-	      (local_role == ROLE_RS_CLIENT && remote_role == ROLE_RS_SERVER))) {
+	if (!bgp_role_advertise(peer))
+		return false;
+
+	if (role_rcvd && !((local_role == ROLE_PEER && remote_role == ROLE_PEER) ||
+			   (local_role == ROLE_PROVIDER && remote_role == ROLE_CUSTOMER) ||
+			   (local_role == ROLE_CUSTOMER && remote_role == ROLE_PROVIDER) ||
+			   (local_role == ROLE_RS_SERVER && remote_role == ROLE_RS_CLIENT) ||
+			   (local_role == ROLE_RS_CLIENT && remote_role == ROLE_RS_SERVER))) {
 		bgp_notify_send(connection, BGP_NOTIFY_OPEN_ERR, BGP_NOTIFY_OPEN_ROLE_MISMATCH);
 		return true;
 	}
@@ -1911,8 +1922,7 @@ uint16_t bgp_open_capability(struct stream *s, struct peer_connection *connectio
 	}

 	/* Do not send capability. */
-	if (!CHECK_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN)
-	    || CHECK_FLAG(peer->flags, PEER_FLAG_DONT_CAPABILITY))
+	if (!bgp_open_capability_send(peer))
 		return 0;

 	/* MP capability for configured AFI, SAFI */
@@ -2015,7 +2025,7 @@ uint16_t bgp_open_capability(struct stream *s, struct peer_connection *connectio
 	stream_putc(s, CAPABILITY_CODE_EXT_MESSAGE_LEN);

 	/* Role*/
-	if (CHECK_FLAG(peer->flags, PEER_FLAG_ROLE)) {
+	if (bgp_role_advertise(peer)) {
 		SET_FLAG(peer->cap, PEER_CAP_ROLE_ADV);
 		stream_putc(s, BGP_OPEN_OPT_CAP);
 		stream_putc(s, CAPABILITY_CODE_ROLE_LEN + 2);
diff --git a/doc/user/bgp.rst b/doc/user/bgp.rst
index 6a3df0fe40..142ebc8598 100644
--- a/doc/user/bgp.rst
+++ b/doc/user/bgp.rst
@@ -3825,6 +3825,12 @@ happened automatically if local-role is set.
    value of his role (by setting local-role on his side). Otherwise, a Role
    Mismatch Notification will be sent.

+   As defined in :rfc:`9234`, the Role correctness check and ``strict-mode``
+   apply only when the Role capability is advertised. It is not advertised
+   if no capabilities are sent in the OPEN message, e.g. with
+   ``neighbor PEER dont-capability-negotiate`` or after the neighbor rejected
+   the OPEN message with an Unsupported Optional Parameter Notification.
+
 Labeled unicast
 ---------------

diff --git a/tests/topotests/bgp_roles_capability/r1/bgpd.conf b/tests/topotests/bgp_roles_capability/r1/bgpd.conf
index f736448b28..e96894a561 100644
--- a/tests/topotests/bgp_roles_capability/r1/bgpd.conf
+++ b/tests/topotests/bgp_roles_capability/r1/bgpd.conf
@@ -27,3 +27,13 @@ router bgp 64501
   neighbor 192.168.7.2 remote-as external
   neighbor 192.168.7.2 local-role provider strict-mode
   neighbor 192.168.7.2 timers 3 10
+! Missed neighbor role with strict-mode, neighbor OPEN received before ours
+  neighbor 192.168.8.2 remote-as external
+  neighbor 192.168.8.2 local-role provider strict-mode
+  neighbor 192.168.8.2 timers 3 10
+  neighbor 192.168.8.2 timers delayopen 10
+! Missed neighbor role with strict-mode, but our Role is not advertised
+  neighbor 192.168.9.2 remote-as external
+  neighbor 192.168.9.2 local-role provider strict-mode
+  neighbor 192.168.9.2 dont-capability-negotiate
+  neighbor 192.168.9.2 timers 3 10
diff --git a/tests/topotests/bgp_roles_capability/r1/zebra.conf b/tests/topotests/bgp_roles_capability/r1/zebra.conf
index d56b350b37..e9fe0951c6 100644
--- a/tests/topotests/bgp_roles_capability/r1/zebra.conf
+++ b/tests/topotests/bgp_roles_capability/r1/zebra.conf
@@ -17,5 +17,11 @@ interface r1-eth4
 interface r1-eth5
  ip address 192.168.7.1/24
 !
+interface r1-eth6
+ ip address 192.168.8.1/24
+!
+interface r1-eth7
+ ip address 192.168.9.1/24
+!
 ip forwarding
 !
diff --git a/tests/topotests/bgp_roles_capability/r8/bgpd.conf b/tests/topotests/bgp_roles_capability/r8/bgpd.conf
new file mode 100644
index 0000000000..30e435d905
--- /dev/null
+++ b/tests/topotests/bgp_roles_capability/r8/bgpd.conf
@@ -0,0 +1,3 @@
+router bgp 64508
+  neighbor 192.168.8.1 remote-as external
+  neighbor 192.168.8.1 timers 3 10
diff --git a/tests/topotests/bgp_roles_capability/r8/zebra.conf b/tests/topotests/bgp_roles_capability/r8/zebra.conf
new file mode 100644
index 0000000000..688a652d24
--- /dev/null
+++ b/tests/topotests/bgp_roles_capability/r8/zebra.conf
@@ -0,0 +1,6 @@
+!
+interface r8-eth0
+ ip address 192.168.8.2/24
+!
+ip forwarding
+!
diff --git a/tests/topotests/bgp_roles_capability/r9/bgpd.conf b/tests/topotests/bgp_roles_capability/r9/bgpd.conf
new file mode 100644
index 0000000000..67cd8abc23
--- /dev/null
+++ b/tests/topotests/bgp_roles_capability/r9/bgpd.conf
@@ -0,0 +1,3 @@
+router bgp 64509
+  neighbor 192.168.9.1 remote-as external
+  neighbor 192.168.9.1 timers 3 10
diff --git a/tests/topotests/bgp_roles_capability/r9/zebra.conf b/tests/topotests/bgp_roles_capability/r9/zebra.conf
new file mode 100644
index 0000000000..0bbe45ef7e
--- /dev/null
+++ b/tests/topotests/bgp_roles_capability/r9/zebra.conf
@@ -0,0 +1,6 @@
+!
+interface r9-eth0
+ ip address 192.168.9.2/24
+!
+ip forwarding
+!
diff --git a/tests/topotests/bgp_roles_capability/test_bgp_roles_capability.py b/tests/topotests/bgp_roles_capability/test_bgp_roles_capability.py
index 542718c025..e60d3e9dd7 100644
--- a/tests/topotests/bgp_roles_capability/test_bgp_roles_capability.py
+++ b/tests/topotests/bgp_roles_capability/test_bgp_roles_capability.py
@@ -29,7 +29,7 @@ from lib.topogen import Topogen, TopoRouter
 pytestmark = [pytest.mark.bgpd]


-topodef = {f"s{i}": ("r1", f"r{i}") for i in range(2, 8)}
+topodef = {f"s{i}": ("r1", f"r{i}") for i in range(2, 10)}


 @pytest.fixture(scope="module")
@@ -152,6 +152,35 @@ def test_role_strict_mode_empty_open(tgen):
     assert success, "Session between r1 and r7 was not correctly closed"


+def test_role_strict_mode_delayopen(tgen):
+    # provider-undefined pair with strict-mode, r1 uses DelayOpen, so it
+    # checks the OPEN from r8 before sending its own OPEN. Check it on r1,
+    # because r8 gets the NOTIFICATION in OpenSent state, which is an FSM
+    # error for r8 (RFC 4271, 8.2.2).
+    router = tgen.gears["r1"]
+    neighbor_ip = "192.168.8.2"
+    check_r8_mismatch = functools.partial(check_role_mismatch, router, neighbor_ip)
+    success, _ = topotest.run_and_expect(check_r8_mismatch, True, count=20, wait=3)
+    assert success, "Session between r1 and r8 was not correctly closed"
+
+
+def test_role_strict_mode_not_advertised(tgen):
+    # provider-undefined pair with strict-mode, but r1 does not advertise
+    # its Role (dont-capability-negotiate), so strict-mode does not apply
+    router = tgen.gears["r1"]
+    neighbor_ip = "192.168.9.2"
+    check_r9_established = functools.partial(
+        check_session_established, router, neighbor_ip
+    )
+    success, _ = topotest.run_and_expect(check_r9_established, True, count=20, wait=3)
+    assert success, "Session with r9 is not Established"
+
+    neighbor_status = find_neighbor_status(router, neighbor_ip)
+    assert neighbor_status["localRole"] == "provider"
+    assert neighbor_status["remoteRole"] == "undefined"
+    assert "role" not in neighbor_status.get("neighborCapabilities", {})
+
+
 def test_correct_pair_peer_group(tgen):
     # provider-customer pair (using peer-groups)
     router = tgen.gears["r1"]