Commit 1cc6dbf8bb for frr

commit 1cc6dbf8bb4b6a69c45909e29a04bcd6e9835b9d
Author: Donatas Abraitis <donatas@opensourcerouting.org>
Date:   Wed Dec 2 13:54:23 2026 +0200

    bgpd: Check Role correctness also for OPEN without optional parameters

    bgp_role_violation() was called only from bgp_open_option_parse(), and
    bgp_open_receive() skips that function when the Optional Parameters
    Length is 0. An OPEN without optional parameters, which cannot carry
    the Role capability, therefore bypassed local-role strict-mode and the
    session was established.

    Move the check out of bgp_open_option_parse() and do it once in
    bgp_open_receive(), after the optional parameters were handled, so it
    runs regardless of whether the OPEN has optional parameters or not.

    Fixes: https://github.com/FRRouting/frr/issues/24655
    Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>

diff --git a/bgpd/bgp_open.c b/bgpd/bgp_open.c
index a49edabcf4..833a2d0a96 100644
--- a/bgpd/bgp_open.c
+++ b/bgpd/bgp_open.c
@@ -1631,10 +1631,6 @@ int bgp_open_option_parse(struct peer_connection *connection, uint16_t length, i
 	/* Extended Message Support */
 	bgp_peer_set_max_packet_size(peer);

-	/* Check that roles are corresponding to each other */
-	if (bgp_role_violation(connection))
-		return -1;
-
 	/* Check there are no common AFI/SAFIs and send Unsupported Capability
 	   error. */
 	if (*mp_capability
diff --git a/bgpd/bgp_packet.c b/bgpd/bgp_packet.c
index bab78c9618..1f6766ad71 100644
--- a/bgpd/bgp_packet.c
+++ b/bgpd/bgp_packet.c
@@ -2277,6 +2277,14 @@ static int bgp_open_receive(struct peer_connection *connection, bgp_size_t size)
 				   peer->host);
 	}

+	/*
+	 * RFC 9234: check that roles are corresponding to each other. This
+	 * must be done also for an OPEN without optional parameters, which
+	 * cannot carry the Role capability.
+	 */
+	if (bgp_role_violation(connection))
+		return BGP_Stop;
+
 	/*
 	 * Assume that the peer supports the locally configured set of
 	 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
diff --git a/tests/topotests/bgp_roles_capability/r1/bgpd.conf b/tests/topotests/bgp_roles_capability/r1/bgpd.conf
index 273f933d6e..f736448b28 100644
--- a/tests/topotests/bgp_roles_capability/r1/bgpd.conf
+++ b/tests/topotests/bgp_roles_capability/r1/bgpd.conf
@@ -23,3 +23,7 @@ router bgp 64501
   neighbor PG local-role provider
   neighbor PG timers 3 10
   neighbor 192.168.6.2 peer-group PG
+! Missed neighbor role with strict-mode, OPEN without optional parameters
+  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
diff --git a/tests/topotests/bgp_roles_capability/r1/zebra.conf b/tests/topotests/bgp_roles_capability/r1/zebra.conf
index 301a1e8890..d56b350b37 100644
--- a/tests/topotests/bgp_roles_capability/r1/zebra.conf
+++ b/tests/topotests/bgp_roles_capability/r1/zebra.conf
@@ -14,5 +14,8 @@ interface r1-eth3
 interface r1-eth4
  ip address 192.168.6.1/24
 !
+interface r1-eth5
+ ip address 192.168.7.1/24
+!
 ip forwarding
 !
diff --git a/tests/topotests/bgp_roles_capability/r7/bgpd.conf b/tests/topotests/bgp_roles_capability/r7/bgpd.conf
new file mode 100644
index 0000000000..4272f7b9ab
--- /dev/null
+++ b/tests/topotests/bgp_roles_capability/r7/bgpd.conf
@@ -0,0 +1,4 @@
+router bgp 64507
+  neighbor 192.168.7.1 remote-as external
+  neighbor 192.168.7.1 dont-capability-negotiate
+  neighbor 192.168.7.1 timers 3 10
diff --git a/tests/topotests/bgp_roles_capability/r7/zebra.conf b/tests/topotests/bgp_roles_capability/r7/zebra.conf
new file mode 100644
index 0000000000..9fefaf428c
--- /dev/null
+++ b/tests/topotests/bgp_roles_capability/r7/zebra.conf
@@ -0,0 +1,6 @@
+!
+interface r7-eth0
+ ip address 192.168.7.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 f04c882bbb..542718c025 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, 7)}
+topodef = {f"s{i}": ("r1", f"r{i}") for i in range(2, 8)}


 @pytest.fixture(scope="module")
@@ -142,6 +142,16 @@ def test_role_strict_mode(tgen):
     assert success, "Session between r1 and r5 was not correctly closed"


+def test_role_strict_mode_empty_open(tgen):
+    # provider-undefined pair with strict-mode, r7 sends an OPEN without
+    # optional parameters (dont-capability-negotiate)
+    router = tgen.gears["r7"]
+    neighbor_ip = "192.168.7.1"
+    check_r7_mismatch = functools.partial(check_role_mismatch, router, neighbor_ip)
+    success, _ = topotest.run_and_expect(check_r7_mismatch, True, count=20, wait=3)
+    assert success, "Session between r1 and r7 was not correctly closed"
+
+
 def test_correct_pair_peer_group(tgen):
     # provider-customer pair (using peer-groups)
     router = tgen.gears["r1"]