From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=109.224.244.122 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796323293; b=5XriL4o7bDtoUCluDb99MEh+YavPnGUnEIm93RgalRZH8p8nArnYhRo+SbZcw49NxPfK qPFPwNOzbRUJ14k83qwGeOIbya2tgKkPXxbvqn+EvJcaocMEkHxai0DtkFKUQKS714Ed4 uCOiXoKmqBtto5VT6F9u2z17JIOEP2QY7dp071JopyOUU7IUgciKRR5UxybBtuOJSQChN yqRfD8Eqje7EyirueUodPF7764Q2BdgH4O2aQ/miDVdFuWwagDSxh0X6ISMUBEFyi+K9M mEICxSDwlp6TEqVj8hYBjYsrwUz9v5szceWvyo5IF2bievX4VZQMN7uuT/um1WMRcAw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796323293; h=DMARC-Filter:DKIM-Signature:Date:To:From:Cc:Subject:Message-ID: In-Reply-To:References:Feedback-ID:X-Pm-Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=K+cHwVPmXdiEaGVwy8K/5Vwl7ddriVZcw6G4CkL5vYU=; b=MotEzPuf8GkeqYIZDrNcwJTuW5V0sp3n0v7kcgaEpT4bfG3HQIoD3NBkk4axU+PduC7C o+iuuZuSSiTwbkYK7br4MnLTcuJgg2FKHREyP8yAtBLcZ36nK33bliALYa08dRedy9COk Dmr4uYhMKThFA0nt9CdcdSY6qVMDNAWiwMix8CiR9VzB5Q7VvAHYA677FmqeqK5LeH+ez NC+2sqaBuSJQKcQncI0y8zAldtmD8Qul1NqeuT9GA7dkqwmcSF8XsFwsybOG7igf4fnPV 4Guz9zC+MLgEamJt1MxQODnT7BD5tYWwuzzasL+RT51Vhzw65Uoa4NJgLURTtIL7Pyg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass header.d=proton.me header.i=@proton.me header.a=rsa-sha256 header.s=ffjxtajxsrarzokgpqrdhmkyru.protonmail2 header.b=mKgf5LsM; arc=none smtp.remote-ip=109.224.244.122 Received: from mail-244122.protonmail.ch (mail-244122.protonmail.ch [109.224.244.122]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82C48606A32F for ; Thu, 3 Dec 2026 18:41:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 82C48606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=proton.me header.i=@proton.me header.a=rsa-sha256 header.s=ffjxtajxsrarzokgpqrdhmkyru.protonmail2 header.b=mKgf5LsM DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=ffjxtajxsrarzokgpqrdhmkyru.protonmail2; t=1796323290; x=1796582490; bh=K+cHwVPmXdiEaGVwy8K/5Vwl7ddriVZcw6G4CkL5vYU=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=mKgf5LsMd18K3s1Pk+BWrOv4zTyuze/as8HG+n2lW4BshaKTUMjdNzPVqg0e34ODN 8v3hikHCzJosyPwYiALj14GlLDZPd29a9wGi6nqCexwe4GRlefOd2EMT5A8Wb0Iq1+ 8em70CTFMEZqATdlye3O6I/zp40Xp7hn1M/L9Fso6GY5YOsjFSvB/oWR5aikc997Rr D7UtxGkv92HeZw05FUEmfu/gi5OMRjvtMz0xg5Lk+Ndw0pAOX2TOr4CsBy5BsxRwsT uGDg3Y3VCUf8KGx3I1hAIIjjYb+F/MIOAvtGwWAchuyK+6d/oQJemAer4RKahL/okQ 6kO5JxjmDLoiA== Date: Thu, 03 Dec 2026 18:41:27 +0000 To: linux-bluetooth@vger.kernel.org From: Kyle Pepper Cc: luiz.dentz@gmail.com, marcel@holtmann.org Subject: [RFC PATCH v3 1/2] Bluetooth: Allow custom matching of synchronous LE events Message-ID: <20261203184044.134357-2-cochcoder@proton.me> In-Reply-To: <20261203184044.134357-1-cochcoder@proton.me> References: <20261203184044.134357-1-cochcoder@proton.me> Feedback-ID: 97716137:user:proton X-Pm-Message-ID: ccc04b100d9f03e1e45ab18c01d4179af31d440f X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable LE Connection Update Complete includes a connection handle, but the synchronous HCI request can complete on the event code alone. An update for a different connection can therefore complete the wrong request. Allow a synchronous LE event request to install an optional matcher. Use it for LE Connection Update Complete to check the command and event payload lengths, match the connection handle, and return the controller status. Events that do not match still go through normal dispatch. Callers without a matcher keep event-only completion. AI assistance: OpenAI GPT-6 (via Codex). Signed-off-by: Kyle Pepper --- diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/blue= tooth.h index 777bc1a..cc0a931 100644 --- a/include/net/bluetooth/bluetooth.h +++ b/include/net/bluetooth/bluetooth.h @@ -572,8 +572,12 @@ int hci_ethtool_ts_info(unsigned int index, int sk_pro= to, #define HCI_REQ_START=09BIT(0) #define HCI_REQ_SKB=09BIT(1) =20 +typedef bool (*hci_req_match_t)(const struct sk_buff *cmd, +=09=09=09 const struct sk_buff *event, u8 *status); + struct hci_ctrl { =09struct sock *sk; +=09hci_req_match_t req_match; =09u16 opcode; =09u8 req_flags; =09u8 req_event; diff --git a/include/net/bluetooth/hci_sync.h b/include/net/bluetooth/hci_s= ync.h index eab827d..24cd69d 100644 --- a/include/net/bluetooth/hci_sync.h +++ b/include/net/bluetooth/hci_sync.h @@ -37,6 +37,14 @@ struct hci_cmd_sync_work_entry { }; =20 struct adv_info; +struct hci_cp_le_conn_update; + +int __hci_cmd_sync_le_match(struct hci_dev *hdev, u16 opcode, u32 plen, +=09=09=09 const void *param, u8 event, u32 timeout, +=09=09=09 hci_req_match_t match); +int hci_le_conn_update_cmd_sync(struct hci_dev *hdev, +=09=09=09=09const struct hci_cp_le_conn_update *cp, +=09=09=09=09u32 timeout); =20 struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode, u32 p= len, =09=09=09=09 const void *param, struct sock *sk); diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index 5b7526a..cbad116 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -542,10 +542,7 @@ static int le_conn_update_sync(struct hci_dev *hdev, v= oid *data) =20 =09hci_dev_unlock(hdev); =20 -=09err =3D __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CONN_UPDATE, -=09=09=09=09 sizeof(cp), &cp, -=09=09=09=09 HCI_EV_LE_CONN_UPDATE_COMPLETE, -=09=09=09=09 timeout, NULL); +=09err =3D hci_le_conn_update_cmd_sync(hdev, &cp, timeout); =09if (err) =09=09return err; =20 diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c index 749963d..67405de 100644 --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -8486,18 +8486,28 @@ static void hci_le_meta_evt(struct hci_dev *hdev, v= oid *data, { =09struct hci_ev_le_meta *ev =3D data; =09const struct hci_le_ev *subev; +=09struct sk_buff *cmd; +=09hci_req_match_t match; +=09u8 result =3D 0; =20 =09bt_dev_dbg(hdev, "subevent 0x%2.2x", ev->subevent); =20 =09/* Only match event if command OGF is for LE */ -=09if (hdev->req_skb && -=09 (hci_opcode_ogf(hci_skb_opcode(hdev->req_skb)) =3D=3D 0x08 || -=09 hci_skb_opcode(hdev->req_skb) =3D=3D HCI_OP_NOP) && -=09 hci_skb_event(hdev->req_skb) =3D=3D ev->subevent) { -=09=09*opcode =3D hci_skb_opcode(hdev->req_skb); -=09=09hci_req_cmd_complete(hdev, *opcode, 0x00, req_complete, -=09=09=09=09 req_complete_skb); +=09hci_dev_lock(hdev); +=09cmd =3D hdev->req_skb; +=09if (cmd && (hci_opcode_ogf(hci_skb_opcode(cmd)) =3D=3D 0x08 || +=09=09 hci_skb_opcode(cmd) =3D=3D HCI_OP_NOP) && +=09 hci_skb_event(cmd) =3D=3D ev->subevent) { +=09=09match =3D bt_cb(cmd)->hci.req_match; +=09=09if (!match || (READ_ONCE(hdev->req_status) =3D=3D HCI_REQ_PEND && +=09=09=09 match(cmd, skb, &result))) { +=09=09=09*opcode =3D hci_skb_opcode(cmd); +=09=09=09*status =3D result; +=09=09=09hci_req_cmd_complete(hdev, *opcode, result, req_complete, +=09=09=09=09=09 req_complete_skb); +=09=09} =09} +=09hci_dev_unlock(hdev); =20 =09subev =3D &hci_le_ev_table[ev->subevent]; =09if (!subev->func) diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c index ee426ff..26d438f 100644 --- a/net/bluetooth/hci_sync.c +++ b/net/bluetooth/hci_sync.c @@ -74,6 +74,7 @@ struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, = u16 opcode, u32 plen, =20 =09hci_skb_pkt_type(skb) =3D HCI_COMMAND_PKT; =09hci_skb_opcode(skb) =3D opcode; +=09bt_cb(skb)->hci.req_match =3D NULL; =20 =09/* Grab a reference if command needs to be associated with a sock (e.g. =09 * likely mgmt socket that initiated the command). @@ -87,7 +88,8 @@ struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, = u16 opcode, u32 plen, } =20 static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen= , -=09=09=09 const void *param, u8 event, struct sock *sk) +=09=09=09 const void *param, u8 event, struct sock *sk, +=09=09=09 hci_req_match_t match) { =09struct hci_dev *hdev =3D req->hdev; =09struct sk_buff *skb; @@ -112,6 +114,7 @@ static void hci_cmd_sync_add(struct hci_request *req, u= 16 opcode, u32 plen, =09=09bt_cb(skb)->hci.req_flags |=3D HCI_REQ_START; =20 =09hci_skb_event(skb) =3D event; +=09bt_cb(skb)->hci.req_match =3D match; =20 =09skb_queue_tail(&req->cmd_q, skb); } @@ -157,9 +160,10 @@ static void hci_request_init(struct hci_request *req, = struct hci_dev *hdev) } =20 /* This function requires the caller holds hdev->req_lock. */ -struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 pl= en, -=09=09=09=09 const void *param, u8 event, u32 timeout, -=09=09=09=09 struct sock *sk) +static struct sk_buff *hci_cmd_sync_match(struct hci_dev *hdev, u16 opcode= , +=09=09=09=09=09 u32 plen, const void *param, +=09=09=09=09=09 u8 event, u32 timeout, struct sock *sk, +=09=09=09=09=09 hci_req_match_t match) { =09struct hci_request req; =09struct sk_buff *skb; @@ -169,7 +173,7 @@ struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev,= u16 opcode, u32 plen, =20 =09hci_request_init(&req, hdev); =20 -=09hci_cmd_sync_add(&req, opcode, plen, param, event, sk); +=09hci_cmd_sync_add(&req, opcode, plen, param, event, sk, match); =20 =09WRITE_ONCE(hdev->req_status, HCI_REQ_PEND); =20 @@ -218,8 +222,58 @@ struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev= , u16 opcode, u32 plen, =20 =09return skb; } + +struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 pl= en, +=09=09=09=09 const void *param, u8 event, u32 timeout, +=09=09=09=09 struct sock *sk) +{ +=09return hci_cmd_sync_match(hdev, opcode, plen, param, event, timeout, sk= , +=09=09=09=09 NULL); +} EXPORT_SYMBOL(__hci_cmd_sync_sk); =20 +int __hci_cmd_sync_le_match(struct hci_dev *hdev, u16 opcode, u32 plen, +=09=09=09 const void *param, u8 event, u32 timeout, +=09=09=09 hci_req_match_t match) +{ +=09struct sk_buff *skb; + +=09skb =3D hci_cmd_sync_match(hdev, opcode, plen, param, event, timeout, N= ULL, +=09=09=09=09 match); +=09if (IS_ERR(skb)) +=09=09return PTR_ERR(skb) =3D=3D -ENODATA ? 0 : PTR_ERR(skb); + +=09kfree_skb(skb); +=09return 0; +} + +static bool le_conn_update_match(const struct sk_buff *cmd, +=09=09=09=09 const struct sk_buff *event, u8 *status) +{ +=09struct hci_ev_le_conn_update_complete ev_buf; +=09struct hci_cp_le_conn_update cp_buf; +=09const struct hci_ev_le_conn_update_complete *ev; +=09const struct hci_cp_le_conn_update *cp; + +=09cp =3D skb_header_pointer(cmd, HCI_COMMAND_HDR_SIZE, sizeof(cp_buf), +=09=09=09=09&cp_buf); +=09ev =3D skb_header_pointer(event, 0, sizeof(ev_buf), &ev_buf); +=09if (!cp || !ev || cp->handle !=3D ev->handle) +=09=09return false; + +=09*status =3D ev->status; +=09return true; +} + +int hci_le_conn_update_cmd_sync(struct hci_dev *hdev, +=09=09=09=09const struct hci_cp_le_conn_update *cp, +=09=09=09=09u32 timeout) +{ +=09return __hci_cmd_sync_le_match(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(*cp)= , +=09=09=09=09 cp, HCI_EV_LE_CONN_UPDATE_COMPLETE, +=09=09=09=09 timeout, le_conn_update_match); +} + /* This function requires the caller holds hdev->req_lock. */ struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, =09=09=09 const void *param, u32 timeout)