From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v1 1/4] shared/hci: Add BPF filter for registered events
Date: Fri, 1 May 2026 10:38:44 -0400 [thread overview]
Message-ID: <20260501143847.724150-1-luiz.dentz@gmail.com> (raw)
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Implement a BPF socket filter in bt_hci_register/bt_hci_unregister that
uses setsockopt(SO_ATTACH_FILTER) to only accept HCI events that have
been registered, plus BT_HCI_EVT_CMD_COMPLETE and BT_HCI_EVT_CMD_STATUS
which are always needed for command response processing.
The filter is rebuilt each time an event is registered or unregistered,
and only applies to non-stream (raw socket) connections.
Assisted-by: Claude:claude-opus-4.6
---
src/shared/hci.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 84 insertions(+)
diff --git a/src/shared/hci.c b/src/shared/hci.c
index 0faa6dea555d..5105b0b2f320 100644
--- a/src/shared/hci.c
+++ b/src/shared/hci.c
@@ -23,6 +23,7 @@
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
+#include <linux/filter.h>
#include "bluetooth/hci.h"
#include "monitor/bt.h"
@@ -565,6 +566,85 @@ bool bt_hci_flush(struct bt_hci *hci)
return true;
}
+static void update_evt_filter(struct bt_hci *hci)
+{
+ const struct queue_entry *entry;
+ struct sock_filter *filters;
+ struct sock_fprog fprog;
+ unsigned int count, i;
+ int fd;
+
+ fd = io_get_fd(hci->io);
+ if (fd < 0)
+ return;
+
+ /* If stream-based (not a raw socket), no BPF filtering needed */
+ if (hci->is_stream)
+ return;
+
+ count = queue_length(hci->evt_list);
+
+ /* Build filter: load event code, check defaults + registered events.
+ * Packet layout for HCI_CHANNEL_RAW: [H4 type (1 byte)][evt code (1)]
+ * So event code is at offset 1.
+ *
+ * Filter structure:
+ * [0] Load byte at offset 1 (event code)
+ * [1] JEQ BT_HCI_EVT_CMD_COMPLETE -> accept
+ * [2] JEQ BT_HCI_EVT_CMD_STATUS -> accept
+ * [3..3+count-1] JEQ registered_event -> accept
+ * [3+count] reject
+ * [4+count] accept
+ */
+ filters = malloc(sizeof(*filters) * (count + 5));
+ if (!filters)
+ return;
+
+ i = 0;
+
+ /* Load event code byte */
+ filters[i++] = (struct sock_filter)
+ BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 1);
+
+ /* Check BT_HCI_EVT_CMD_COMPLETE (0x0e) */
+ filters[i++] = (struct sock_filter)
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, BT_HCI_EVT_CMD_COMPLETE,
+ count + 2, 0);
+
+ /* Check BT_HCI_EVT_CMD_STATUS (0x0f) */
+ filters[i++] = (struct sock_filter)
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, BT_HCI_EVT_CMD_STATUS,
+ count + 1, 0);
+
+ /* Check each registered event */
+ entry = queue_get_entries(hci->evt_list);
+ while (entry) {
+ const struct evt *evt = entry->data;
+ unsigned int jump = count - (i - 3);
+
+ filters[i] = (struct sock_filter)
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, evt->event,
+ jump, 0);
+ i++;
+ entry = entry->next;
+ }
+
+ /* Reject */
+ filters[i++] = (struct sock_filter)
+ BPF_STMT(BPF_RET | BPF_K, 0);
+
+ /* Accept */
+ filters[i++] = (struct sock_filter)
+ BPF_STMT(BPF_RET | BPF_K, 0x0fffffff);
+
+ fprog.len = i;
+ fprog.filter = filters;
+
+ setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
+
+ free(filters);
+}
+
unsigned int bt_hci_register(struct bt_hci *hci, uint8_t event,
bt_hci_callback_func_t callback,
void *user_data, bt_hci_destroy_func_t destroy)
@@ -591,6 +671,8 @@ unsigned int bt_hci_register(struct bt_hci *hci, uint8_t event,
return 0;
}
+ update_evt_filter(hci);
+
return evt->id;
}
@@ -657,6 +739,8 @@ bool bt_hci_unregister(struct bt_hci *hci, unsigned int id)
evt_free(evt);
+ update_evt_filter(hci);
+
return true;
}
--
2.53.0
next reply other threads:[~2026-05-01 14:38 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-01 14:38 Luiz Augusto von Dentz [this message]
2026-05-01 14:38 ` [PATCH BlueZ v1 2/4] shared/hci: Add bt_hci_register_subevent for LE Meta events Luiz Augusto von Dentz
2026-05-01 14:38 ` [PATCH BlueZ v1 3/4] ranging/rap_hci: Use bt_hci_register_subevent for LE CS events Luiz Augusto von Dentz
2026-05-01 14:38 ` [PATCH BlueZ v1 4/4] hci-tester: Use bt_hci_register_subevent for LE Meta events Luiz Augusto von Dentz
2026-05-01 15:56 ` [BlueZ,v1,1/4] shared/hci: Add BPF filter for registered events bluez.test.bot
2026-05-06 13:30 ` [PATCH BlueZ v1 1/4] " patchwork-bot+bluetooth
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260501143847.724150-1-luiz.dentz@gmail.com \
--to=luiz.dentz@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox