Commit c27346dbc1 for frr

commit c27346dbc14f1a4550751c1f2a6476d2b64e97f7
Author: Philippe Guibert <philippe.guibert@6wind.com>
Date:   Tue Nov 10 17:56:07 2026 +0100

    bfdd: refresh the sessions of a removed keychain

    A BFD peer whose keychain is removed keeps a pointer to the freed
    keychain, and its next authenticated packet uses it. The YANG reference
    of the peer "authentication key-chain" leaf refuses to remove a keychain
    a peer uses, so this is not reachable yet, but it becomes so as soon as
    the peer accepts a keychain that does not exist:

    > bfd
    >  peer 192.168.1.2
    >   authentication key-chain AAEE
    > exit
    > no key chain AAEE

    keychain_delete() unlinks the keychain before calling the
    keychain_removed hook, so that keychain_lookup() fails from there on.
    __bfd_process_keychain_updated() selects the sessions to refresh with
    bfd_session_auth_config_takes_precedence_over_profile(), which relies on
    that lookup: the sessions of the removed keychain are skipped, and their
    bs->kc is left pointing to the keychain that is freed right after. It
    also only walks the BFD_MODE_TYPE_BFD sessions, while the S-BFD echo and
    initiator sessions take the same "authentication key-chain" leaf: an
    S-BFD peer without a profile keeps the freed keychain too, and does not
    get a keychain created after it.

    Select the sessions of every mode by the keychain name they are
    configured with, and let bfd_session_apply() resolve bs->kc again, as
    it already does for the other changes. A removed keychain then falls
    back to the profile one, like a peer configured with a keychain that
    does not exist.

    Fixes: 61cc79e8b4 ("bfdd: add bfd_process_keychain_remove")
    Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>

diff --git a/bfdd/bfdd.c b/bfdd/bfdd.c
index 25ae881148..6126be7710 100644
--- a/bfdd/bfdd.c
+++ b/bfdd/bfdd.c
@@ -197,31 +197,23 @@ static void __bfd_process_keychain_updated(const char *keychain_name, bool is_mh
 					   bool remove_event)
 {
 	struct bfd_session *bs;
-	const struct bfd_session *iter = NULL;
-	struct keychain *kc;
+	const struct bfd_session *iter;
+	uint32_t mode;

-	kc = keychain_lookup(keychain_name);
+	for (mode = BFD_MODE_TYPE_BFD; mode <= BFD_MODE_TYPE_SBFD_INIT; mode++) {
+		iter = NULL;
+		while ((iter = bfd_session_next(iter, is_mhop, mode)) != NULL) {
+			bs = (struct bfd_session *)iter;

-	while ((iter = bfd_session_next(iter, is_mhop, BFD_MODE_TYPE_BFD)) != NULL) {
-		bs = (struct bfd_session *)iter;
+			if (strcmp(keychain_name, bs->peer_profile.auth_config.key_chain_name) != 0)
+				/* peer profile keychain name does not match */
+				continue;

-		if (!bfd_session_auth_config_takes_precedence_over_profile(bs))
-			/* Peer-specific config takes precedence */
-			continue;
-
-		if (strcmp(keychain_name, bs->peer_profile.auth_config.key_chain_name) != 0)
-			/* peer profile keychain name does not match */
-			continue;
-
-		if (kc && remove_event == false)
-			bs->kc = kc;
-		else
-			bs->kc = NULL;
+			zlog_info("BFD: session [%s], keychain %s %s", bs_to_string(bs),
+				  keychain_name, remove_event ? "removed" : "updated");

-		zlog_info("BFD: session [%s], keychain %s %s", bs_to_string(bs), keychain_name,
-			  remove_event ? "removed" : "updated");
-
-		bfd_session_apply(bs);
+			bfd_session_apply(bs);
+		}
 	}
 }