Skip to contentSkip to conversation

monitor: Use BPF to filter packets by index

PATCH Luiz Augusto von Dentz 2 participants 3 messages Jun 20, 2018 – Jun 21, 2018 1 message not in the archive Bluetooth drivers

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Luiz Augusto von Dentz

author maintainer
#1 lore raw

Hi Marcel,

Quoted 142 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This uses a BPF filter to filter packets to specific index.

---
 monitor/control.c | 36 ++++++++++++++++++++++++++++++++++++
 monitor/control.h |  1 +
 monitor/packet.c  |  9 ++-------
 3 files changed, 39 insertions(+), 7 deletions(-)
monitor/control.c +36 -0
index 9bbdc37dc..05cfc3aaa 100644
@@ -28,6 +28,7 @@
 
 #include <stdio.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -39,6 +40,7 @@
 #include <sys/stat.h>
 #include <termios.h>
 #include <fcntl.h>
+#include <linux/filter.h>
 
 #include "lib/bluetooth.h"
 #include "lib/hci.h"
@@ -58,6 +60,7 @@
 static struct btsnoop *btsnoop_file = NULL;
 static bool hcidump_fallback = false;
 static bool decode_control = true;
+static uint16_t filter_index = HCI_DEV_NONE;
 
 struct control_data {
        uint16_t channel;
@@ -1028,6 +1031,31 @@ static int open_socket(uint16_t channel)
        return fd;
 }
 
+static void attach_index_filter(int fd, uint16_t index)
+{
+       struct sock_filter filters[] = {
+               /* A <- MGMT index */
+               BPF_STMT(BPF_LD + BPF_B + BPF_ABS,
+                                       offsetof(struct mgmt_hdr, index)),
+               /* A == HCI_DEV_NONE */
+               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, HCI_DEV_NONE, 0, 1),
+               /* return */
+               BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* pass */
+               /* A ==  index */
+               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, index, 0, 1),
+               /* returns */
+               BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* pass */
+               BPF_STMT(BPF_RET|BPF_K, 0), /* reject */
+       };
+       struct sock_fprog fprog = {
+               .len = sizeof(filters) / sizeof(filters[0]),
+               /* casting const away: */
+               .filter = filters,
+       };
+
+       setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
+}
+
 static int open_channel(uint16_t channel)
 {
        struct control_data *data;
@@ -1045,6 +1073,9 @@ static int open_channel(uint16_t channel)
                return -1;
        }
 
+       if (filter_index != HCI_DEV_NONE)
+               attach_index_filter(data->fd, filter_index);
+
        mainloop_add_fd(data->fd, EPOLLIN, data_callback, data, free_data);
 
        return 0;
@@ -1459,3 +1490,8 @@ void control_disable_decoding(void)
 {
        decode_control = false;
 }
+
+void control_filter_index(uint16_t index)
+{
+       filter_index = index;
+}
monitor/control.h +1 -0
index 630a852e4..dc655a45d 100644
@@ -30,5 +30,6 @@ void control_server(const char *path);
 int control_tty(const char *path, unsigned int speed);
 int control_tracing(void);
 void control_disable_decoding(void);
+void control_filter_index(uint16_t index);
 
 void control_message(uint16_t opcode, const void *data, uint16_t size);
monitor/packet.c +2 -8
index 387be1dcb..cedbef1f7 100644
@@ -104,7 +104,6 @@ static time_t time_offset = ((time_t) -1);
 static int priority_level = BTSNOOP_PRIORITY_INFO;
 static unsigned long filter_mask = 0;
 static bool index_filter = false;
-static uint16_t index_number = 0;
 static uint16_t index_current = 0;
 
 #define UNKNOWN_MANUFACTURER 0xffff
@@ -259,8 +258,9 @@ void packet_select_index(uint16_t index)
 {
        filter_mask &= ~PACKET_FILTER_SHOW_INDEX;
 
+       control_filter_index(index);
+
        index_filter = true;
-       index_number = index;
 }
 
 #define print_space(x) printf("%*c", (x), ' ');
@@ -4039,9 +4039,6 @@ void packet_control(struct timeval *tv, struct ucred *cred,
                                        uint16_t index, uint16_t opcode,
                                        const void *data, uint16_t size)
 {
-       if (index_filter && index_number != index)
-               return;
-
        control_message(opcode, data, size);
 }
 
@@ -4063,8 +4060,6 @@ void packet_monitor(struct timeval *tv, struct ucred *cred,
        const char *ident;
 
        if (index != HCI_DEV_NONE) {
-               if (index_filter && index_number != index)
-                       return;
                index_current = index;
        }
 
--
Quoted 1 line

2.13.3

ping

Signature · 1 line
Luiz Augusto von Dentz
maintainer
#2 lore raw

Hi Luiz,

Quoted 56 lines
Quoted 56 lines

This uses a BPF filter to filter packets to specific index.
---

monitor/control.c | 36 ++++++++++++++++++++++++++++++++++++
monitor/control.h |  1 +
monitor/packet.c  |  9 ++-------
3 files changed, 39 insertions(+), 7 deletions(-)
monitor/control.c +20 -0
index 9bbdc37dc..05cfc3aaa 100644
@@ -28,6 +28,7 @@
 
 #include <stdio.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -39,6 +40,7 @@
 #include <sys/stat.h>
 #include <termios.h>
 #include <fcntl.h>
+#include <linux/filter.h>
 
 #include "lib/bluetooth.h"
 #include "lib/hci.h"
@@ -58,6 +60,7 @@
 static struct btsnoop *btsnoop_file = NULL;
 static bool hcidump_fallback = false;
 static bool decode_control = true;
+static uint16_t filter_index = HCI_DEV_NONE;
 
 struct control_data {
       uint16_t channel;
@@ -1028,6 +1031,31 @@ static int open_socket(uint16_t channel)
       return fd;
 }
 
+static void attach_index_filter(int fd, uint16_t index)
+{
+       struct sock_filter filters[] = {
+               /* A <- MGMT index */
+               BPF_STMT(BPF_LD + BPF_B + BPF_ABS,
+                                       offsetof(struct mgmt_hdr, index)),
+               /* A == HCI_DEV_NONE */
+               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, HCI_DEV_NONE, 0, 1),
+               /* return */
+               BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* pass */
+               /* A ==  index */
+               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, index, 0, 1),
+               /* returns */
+               BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* pass */
+               BPF_STMT(BPF_RET|BPF_K, 0), /* reject */
+       };
+       struct sock_fprog fprog = {
Quoted 8 lines

monitor/control.c static int open_socket(uint16_t channel) · line 1058

Quoted 8 lines
monitor/control.c +8 -0
+               .len = sizeof(filters) / sizeof(filters[0]),
+               /* casting const away: */
+               .filter = filters,
+       };
+
+       setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
+}
+

I would prefer a bit more comments on what the program is doing. So allow packets from HCI_DEV_NONE, filter for index etc.

Regards

Marcel

author maintainer
#3 lore raw

Hi Marcel,

Quoted 60 lines · Marcel Holtmann

Hi Luiz,

Quoted 58 lines
Quoted 58 lines

This uses a BPF filter to filter packets to specific index.
---

monitor/control.c | 36 ++++++++++++++++++++++++++++++++++++
monitor/control.h |  1 +
monitor/packet.c  |  9 ++-------
3 files changed, 39 insertions(+), 7 deletions(-)
monitor/control.c +22 -0
index 9bbdc37dc..05cfc3aaa 100644
@@ -28,6 +28,7 @@
 
 #include <stdio.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -39,6 +40,7 @@
 #include <sys/stat.h>
 #include <termios.h>
 #include <fcntl.h>
+#include <linux/filter.h>
 
 #include "lib/bluetooth.h"
 #include "lib/hci.h"
@@ -58,6 +60,7 @@
 static struct btsnoop *btsnoop_file = NULL;
 static bool hcidump_fallback = false;
 static bool decode_control = true;
+static uint16_t filter_index = HCI_DEV_NONE;
 
 struct control_data {
       uint16_t channel;
@@ -1028,6 +1031,31 @@ static int open_socket(uint16_t channel)
       return fd;
 }
 
+static void attach_index_filter(int fd, uint16_t index)
+{
+       struct sock_filter filters[] = {
+               /* A <- MGMT index */
+               BPF_STMT(BPF_LD + BPF_B + BPF_ABS,
+                                       offsetof(struct mgmt_hdr, index)),
+               /* A == HCI_DEV_NONE */
+               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, HCI_DEV_NONE, 0, 1),
+               /* return */
+               BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* pass */
+               /* A ==  index */
+               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, index, 0, 1),
+               /* returns */
+               BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* pass */
+               BPF_STMT(BPF_RET|BPF_K, 0), /* reject */
+       };
+       struct sock_fprog fprog = {
+               .len = sizeof(filters) / sizeof(filters[0]),
+               /* casting const away: */
Quoted 8 lines
Quoted 6 lines

monitor/control.c static int open_socket(uint16_t channel) · line 1058

Quoted 6 lines
monitor/control.c +6 -0
+               .filter = filters,
+       };
+
+       setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
+}
+

I would prefer a bit more comments on what the program is doing. So allow packets from HCI_DEV_NONE, filter for index etc.

Sure I will update with what each instruction is doing.

Quoted 4 lines

Regards

Marcel

Signature · 1 line
Luiz Augusto von Dentz