Skip to contentSkip to conversation

Bluetooth: Introduce HCI_CONN_FLAG_DEVICE_PRIVACY device flag

PATCH Superseded Luiz Augusto von Dentz 2 participants 9 messages Nov 5, 2021 – Nov 18, 2021

Subsystems (2)
lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v2 1/2 Bluetooth: Introduce HCI_CONN_FLAG_DEVICE_PRIVACY device flag

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

This introduces HCI_CONN_FLAG_DEVICE_PRIVACY which can be used by userspace to indicate to the controller to use Device Privacy Mode to a specific device.

---
v2: Fix marking Device Privacy Flag even when adapter is not capable of handling Set Privacy Mode.

 include/net/bluetooth/hci_core.h |  4 ++++
 net/bluetooth/mgmt.c             | 24 ++++++++++++++++++++----
 2 files changed, 24 insertions(+), 4 deletions(-)
include/net/bluetooth/hci_core.h +4 -0
index b5f061882c10..07d2d099dc2a 100644
@@ -160,6 +160,7 @@ struct bdaddr_list_with_flags {
 
 enum hci_conn_flags {
 	HCI_CONN_FLAG_REMOTE_WAKEUP,
+	HCI_CONN_FLAG_DEVICE_PRIVACY,
 	HCI_CONN_FLAG_MAX
 };
 
@@ -1468,6 +1469,9 @@ void hci_conn_del_sysfs(struct hci_conn *conn);
 #define use_ll_privacy(dev) (ll_privacy_capable(dev) && \
 			     hci_dev_test_flag(dev, HCI_ENABLE_LL_PRIVACY))
 
+#define privacy_mode_capable(dev) (use_ll_privacy(dev) && \
+				   (hdev->commands[39] & 0x04))
+
 /* Use enhanced synchronous connection if command is supported */
 #define enhanced_sco_capable(dev) ((dev)->commands[29] & 0x08)
 
net/bluetooth/mgmt.c +20 -4
index 06384d761928..8a8376d32be3 100644
@@ -4350,7 +4350,16 @@ static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
 			       MGMT_STATUS_NOT_SUPPORTED);
 }
 
-#define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1)
+static u32 supported_device_flags(struct hci_dev *hdev)
+{
+	u32 flags = BIT(HCI_CONN_FLAG_MAX) - 1;
+
+	/* Check if Privacy Mode can be set */
+	if (!privacy_mode_capable(hdev))
+		flags &= ~BIT(HCI_CONN_FLAG_DEVICE_PRIVACY);
+
+	return flags;
+}
 
 static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
 			    u16 data_len)
@@ -4359,7 +4368,7 @@ static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
 	struct mgmt_rp_get_device_flags rp;
 	struct bdaddr_list_with_flags *br_params;
 	struct hci_conn_params *params;
-	u32 supported_flags = SUPPORTED_DEVICE_FLAGS();
+	u32 supported_flags = supported_device_flags(hdev);
 	u32 current_flags = 0;
 	u8 status = MGMT_STATUS_INVALID_PARAMS;
 
@@ -4423,7 +4432,7 @@ static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
 	struct bdaddr_list_with_flags *br_params;
 	struct hci_conn_params *params;
 	u8 status = MGMT_STATUS_INVALID_PARAMS;
-	u32 supported_flags = SUPPORTED_DEVICE_FLAGS();
+	u32 supported_flags = supported_device_flags(hdev);
 	u32 current_flags = __le32_to_cpu(cp->current_flags);
 
 	bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x",
@@ -4456,6 +4465,13 @@ static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
 		if (params) {
 			params->current_flags = current_flags;
 			status = MGMT_STATUS_SUCCESS;
+
+			/* Update passive scan if HCI_CONN_FLAG_DEVICE_PRIVACY
+			 * has been set.
+			 */
+			if (hci_conn_test_flag(HCI_CONN_FLAG_DEVICE_PRIVACY,
+					       params->current_flags))
+				hci_update_passive_scan(hdev);
 		} else {
 			bt_dev_warn(hdev, "No such LE device %pMR (0x%x)",
 				    &cp->addr.bdaddr,
@@ -7061,7 +7077,7 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,
 added:
 	device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
 	device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type,
-			     SUPPORTED_DEVICE_FLAGS(), current_flags);
+			     supported_device_flags(hdev), current_flags);
 
 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
 				MGMT_STATUS_SUCCESS, &cp->addr,
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH v2 2/2 Bluetooth: hci_sync: Set Privacy Mode when updating the resolving list

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

This adds support for Set Privacy Mode when updating the resolving list when HCI_CONN_FLAG_DEVICE_PRIVACY so the controller shall use Device Mode for devices programmed in the resolving list, Device Mode is actually required when the remote device are not able to use RPA as otherwise the default mode is Network Privacy Mode in which only allows RPAs thus the controller would filter out advertisement using identity addresses for which there is an IRK.

---
 include/net/bluetooth/hci.h      | 10 ++++++
 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_event.c        | 29 +++++++++++++++++
 net/bluetooth/hci_sync.c         | 53 ++++++++++++++++++++++++++++----
 4 files changed, 87 insertions(+), 6 deletions(-)
include/net/bluetooth/hci.h +10 -0
index 63065bc01b76..979da5179ff4 100644
@@ -1930,6 +1930,16 @@ struct hci_rp_le_read_transmit_power {
 	__s8  max_le_tx_power;
 } __packed;
 
+#define HCI_NETWORK_PRIVACY		0x00
+#define HCI_DEVICE_PRIVACY		0x01
+
+#define HCI_OP_LE_SET_PRIVACY_MODE	0x204e
+struct hci_cp_le_set_privacy_mode {
+	__u8  bdaddr_type;
+	bdaddr_t  bdaddr;
+	__u8  mode;
+} __packed;
+
 #define HCI_OP_LE_READ_BUFFER_SIZE_V2	0x2060
 struct hci_rp_le_read_buffer_size_v2 {
 	__u8    status;
include/net/bluetooth/hci_core.h +1 -0
index 07d2d099dc2a..cb5684da3ed4 100644
@@ -758,6 +758,7 @@ struct hci_conn_params {
 
 	struct hci_conn *conn;
 	bool explicit_connect;
+	uint8_t privacy_mode;
 	u32 current_flags;
 };
 
net/bluetooth/hci_event.c +29 -0
index d4b75a6cfeee..9cadc543abcb 100644
@@ -1300,6 +1300,31 @@ static void hci_cc_le_read_transmit_power(struct hci_dev *hdev,
 	hdev->max_le_tx_power = rp->max_le_tx_power;
 }
 
+static void hci_cc_le_set_privacy_mode(struct hci_dev *hdev,
+				       struct sk_buff *skb)
+{
+	__u8 status = *((__u8 *)skb->data);
+	struct hci_cp_le_set_privacy_mode *cp;
+	struct hci_conn_params *params;
+
+	bt_dev_dbg(hdev, "status 0x%2.2x", status);
+
+	if (status)
+		return;
+
+	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PRIVACY_MODE);
+	if (!cp)
+		return;
+
+	hci_dev_lock(hdev);
+
+	params = hci_conn_params_lookup(hdev, &cp->bdaddr, cp->bdaddr_type);
+	if (params)
+		params->privacy_mode = cp->mode;
+
+	hci_dev_unlock(hdev);
+}
+
 static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
 {
 	__u8 *sent, status = *((__u8 *) skb->data);
@@ -3812,6 +3837,10 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
 		hci_cc_le_read_transmit_power(hdev, skb);
 		break;
 
+	case HCI_OP_LE_SET_PRIVACY_MODE:
+		hci_cc_le_set_privacy_mode(hdev, skb);
+		break;
+
 	default:
 		BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
 		break;
net/bluetooth/hci_sync.c +47 -6

72 lines not on this page · raw message

Signature · 1 line
2.31.1
maintainer
#2 lore raw

Re: [PATCH v2 2/2] Bluetooth: hci_sync: Set Privacy Mode when updating the resolving list

Hi Luiz,

Quoted 42 lines

This adds support for Set Privacy Mode when updating the resolving list when HCI_CONN_FLAG_DEVICE_PRIVACY so the controller shall use Device Mode for devices programmed in the resolving list, Device Mode is actually required when the remote device are not able to use RPA as otherwise the default mode is Network Privacy Mode in which only allows RPAs thus the controller would filter out advertisement using identity addresses for which there is an IRK.

---

include/net/bluetooth/hci.h      | 10 ++++++
include/net/bluetooth/hci_core.h |  1 +
net/bluetooth/hci_event.c        | 29 +++++++++++++++++
net/bluetooth/hci_sync.c         | 53 ++++++++++++++++++++++++++++----
4 files changed, 87 insertions(+), 6 deletions(-)
include/net/bluetooth/hci.h +10 -0
index 63065bc01b76..979da5179ff4 100644
@@ -1930,6 +1930,16 @@ struct hci_rp_le_read_transmit_power {
 	__s8  max_le_tx_power;
 } __packed;
 
+#define HCI_NETWORK_PRIVACY		0x00
+#define HCI_DEVICE_PRIVACY		0x01
+
+#define HCI_OP_LE_SET_PRIVACY_MODE	0x204e
+struct hci_cp_le_set_privacy_mode {
+	__u8  bdaddr_type;
+	bdaddr_t  bdaddr;
+	__u8  mode;
+} __packed;
+
 #define HCI_OP_LE_READ_BUFFER_SIZE_V2	0x2060
 struct hci_rp_le_read_buffer_size_v2 {
 	__u8    status;
include/net/bluetooth/hci_core.h +0 -0
index 07d2d099dc2a..cb5684da3ed4 100644

include/net/bluetooth/hci_core.h struct hci_conn_params { · line 761

Quoted 4 lines
include/net/bluetooth/hci_core.h +1 -0
@@ -758,6 +758,7 @@ struct hci_conn_params {
 
 	struct hci_conn *conn;
 	bool explicit_connect;
+	uint8_t privacy_mode;

actually u8 please.

Quoted 119 lines
include/net/bluetooth/hci_core.h +0 -0
 	u32 current_flags;
 };
 
net/bluetooth/hci_event.c +29 -0
index d4b75a6cfeee..9cadc543abcb 100644
@@ -1300,6 +1300,31 @@ static void hci_cc_le_read_transmit_power(struct hci_dev *hdev,
 	hdev->max_le_tx_power = rp->max_le_tx_power;
 }
 
+static void hci_cc_le_set_privacy_mode(struct hci_dev *hdev,
+				       struct sk_buff *skb)
+{
+	__u8 status = *((__u8 *)skb->data);
+	struct hci_cp_le_set_privacy_mode *cp;
+	struct hci_conn_params *params;
+
+	bt_dev_dbg(hdev, "status 0x%2.2x", status);
+
+	if (status)
+		return;
+
+	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PRIVACY_MODE);
+	if (!cp)
+		return;
+
+	hci_dev_lock(hdev);
+
+	params = hci_conn_params_lookup(hdev, &cp->bdaddr, cp->bdaddr_type);
+	if (params)
+		params->privacy_mode = cp->mode;
+
+	hci_dev_unlock(hdev);
+}
+
 static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
 {
 	__u8 *sent, status = *((__u8 *) skb->data);
@@ -3812,6 +3837,10 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
 		hci_cc_le_read_transmit_power(hdev, skb);
 		break;
 
+	case HCI_OP_LE_SET_PRIVACY_MODE:
+		hci_cc_le_set_privacy_mode(hdev, skb);
+		break;
+
 	default:
 		BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
 		break;
net/bluetooth/hci_sync.c +40 -6
index b794605dc882..32ed7da3b6dd 100644
@@ -1580,8 +1580,42 @@ static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
 }
 
+/* Set Device Privacy Mode. */
+static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
+					struct hci_conn_params *params)
+{
+	struct hci_cp_le_set_privacy_mode cp;
+	struct smp_irk *irk;
+
+	/* If device privacy mode has already been set there is nothing to do */
+	if (params->privacy_mode == HCI_DEVICE_PRIVACY)
+		return 0;
+
+	/* Set Privacy Mode requires the use of resolving list (aka. LL Privacy)
+	 * by default Network Mode is used so only really send the command if
+	 * Device Mode is required (HCI_CONN_FLAG_DEVICE_PRIVACY).
+	 */
+	if (!privacy_mode_capable(hdev) ||
+	    !hci_conn_test_flag(HCI_CONN_FLAG_DEVICE_PRIVACY,
+				params->current_flags))
+		return 0;
+
+	irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
+	if (!irk)
+		return 0;
+
+	memset(&cp, 0, sizeof(cp));
+	cp.bdaddr_type = irk->addr_type;
+	bacpy(&cp.bdaddr, &irk->bdaddr);
+	cp.mode = HCI_DEVICE_PRIVACY;
+
+	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
+				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
+}
+
 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
- * this attempts to program the device in the resolving list as well.
+ * this attempts to program the device in the resolving list as well and
+ * properly set the privacy mode.
 */
 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
 				       struct hci_conn_params *params,
@@ -1590,11 +1624,6 @@ static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
 	struct hci_cp_le_add_to_accept_list cp;
 	int err;
 
-	/* Already in accept list */
-	if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
-				   params->addr_type))
-		return 0;
-
 	/* Select filter policy to accept all advertising */
 	if (*num_entries >= hdev->le_accept_list_size)
 		return -ENOSPC;
@@ -1620,6 +1649,18 @@ static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
 		return err;
 	}
 
+	/* Set Privacy Mode */
+	err = hci_le_set_privacy_mode_sync(hdev, params);
+	if (err) {
+		bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
+		return err;

net/bluetooth/hci_sync.c static int hci_le_add_accept_list_sync(struct hci_dev *hdev, · line 1666

Quoted 10 lines
net/bluetooth/hci_sync.c +7 -0
+	}
+
+	/* Check if already in accept list */
+	if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
+				   params->addr_type))
+		return 0;
+
 	*num_entries += 1;
 	cp.bdaddr_type = params->addr_type;
 	bacpy(&cp.bdaddr, &params->addr);

Regards

Marcel

author maintainer
#3 lore raw

Re: [PATCH v2 2/2] Bluetooth: hci_sync: Set Privacy Mode when updating the resolving list

Hi Marcel,

Quoted 46 lines · Marcel Holtmann

Hi Luiz,

Quoted 43 lines

This adds support for Set Privacy Mode when updating the resolving list when HCI_CONN_FLAG_DEVICE_PRIVACY so the controller shall use Device Mode for devices programmed in the resolving list, Device Mode is actually required when the remote device are not able to use RPA as otherwise the default mode is Network Privacy Mode in which only allows RPAs thus the controller would filter out advertisement using identity addresses for which there is an IRK.

---

include/net/bluetooth/hci.h      | 10 ++++++
include/net/bluetooth/hci_core.h |  1 +
net/bluetooth/hci_event.c        | 29 +++++++++++++++++
net/bluetooth/hci_sync.c         | 53 ++++++++++++++++++++++++++++----
4 files changed, 87 insertions(+), 6 deletions(-)
include/net/bluetooth/hci.h +10 -0
index 63065bc01b76..979da5179ff4 100644
@@ -1930,6 +1930,16 @@ struct hci_rp_le_read_transmit_power {
      __s8  max_le_tx_power;
 } __packed;
 
+#define HCI_NETWORK_PRIVACY          0x00
+#define HCI_DEVICE_PRIVACY           0x01
+
+#define HCI_OP_LE_SET_PRIVACY_MODE   0x204e
+struct hci_cp_le_set_privacy_mode {
+     __u8  bdaddr_type;
+     bdaddr_t  bdaddr;
+     __u8  mode;
+} __packed;
+
 #define HCI_OP_LE_READ_BUFFER_SIZE_V2 0x2060
 struct hci_rp_le_read_buffer_size_v2 {
      __u8    status;
include/net/bluetooth/hci_core.h +0 -0
index 07d2d099dc2a..cb5684da3ed4 100644
@@ -758,6 +758,7 @@ struct hci_conn_params {
 
Quoted 5 lines

include/net/bluetooth/hci_core.h struct hci_conn_params { · line 761

Quoted 3 lines
include/net/bluetooth/hci_core.h +1 -0
      struct hci_conn *conn;
      bool explicit_connect;
+     uint8_t privacy_mode;

actually u8 please.

Ah nice catch, I really wish that was a way to catch this sort of mistake with CI but checkpatch doesn't seem to have a rule for it.

Quoted 134 lines
Quoted 119 lines
include/net/bluetooth/hci_core.h +0 -0
      u32 current_flags;
 };
 
net/bluetooth/hci_event.c +29 -0
index d4b75a6cfeee..9cadc543abcb 100644
@@ -1300,6 +1300,31 @@ static void hci_cc_le_read_transmit_power(struct hci_dev *hdev,
      hdev->max_le_tx_power = rp->max_le_tx_power;
 }
 
+static void hci_cc_le_set_privacy_mode(struct hci_dev *hdev,
+                                    struct sk_buff *skb)
+{
+     __u8 status = *((__u8 *)skb->data);
+     struct hci_cp_le_set_privacy_mode *cp;
+     struct hci_conn_params *params;
+
+     bt_dev_dbg(hdev, "status 0x%2.2x", status);
+
+     if (status)
+             return;
+
+     cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PRIVACY_MODE);
+     if (!cp)
+             return;
+
+     hci_dev_lock(hdev);
+
+     params = hci_conn_params_lookup(hdev, &cp->bdaddr, cp->bdaddr_type);
+     if (params)
+             params->privacy_mode = cp->mode;
+
+     hci_dev_unlock(hdev);
+}
+
 static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
 {
      __u8 *sent, status = *((__u8 *) skb->data);
@@ -3812,6 +3837,10 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
              hci_cc_le_read_transmit_power(hdev, skb);
              break;
 
+     case HCI_OP_LE_SET_PRIVACY_MODE:
+             hci_cc_le_set_privacy_mode(hdev, skb);
+             break;
+
      default:
              BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
              break;
net/bluetooth/hci_sync.c +40 -6
index b794605dc882..32ed7da3b6dd 100644
@@ -1580,8 +1580,42 @@ static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
                                   sizeof(cp), &cp, HCI_CMD_TIMEOUT);
 }
 
+/* Set Device Privacy Mode. */
+static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
+                                     struct hci_conn_params *params)
+{
+     struct hci_cp_le_set_privacy_mode cp;
+     struct smp_irk *irk;
+
+     /* If device privacy mode has already been set there is nothing to do */
+     if (params->privacy_mode == HCI_DEVICE_PRIVACY)
+             return 0;
+
+     /* Set Privacy Mode requires the use of resolving list (aka. LL Privacy)
+      * by default Network Mode is used so only really send the command if
+      * Device Mode is required (HCI_CONN_FLAG_DEVICE_PRIVACY).
+      */
+     if (!privacy_mode_capable(hdev) ||
+         !hci_conn_test_flag(HCI_CONN_FLAG_DEVICE_PRIVACY,
+                             params->current_flags))
+             return 0;
+
+     irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
+     if (!irk)
+             return 0;
+
+     memset(&cp, 0, sizeof(cp));
+     cp.bdaddr_type = irk->addr_type;
+     bacpy(&cp.bdaddr, &irk->bdaddr);
+     cp.mode = HCI_DEVICE_PRIVACY;
+
+     return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
+                                  sizeof(cp), &cp, HCI_CMD_TIMEOUT);
+}
+
 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
- * this attempts to program the device in the resolving list as well.
+ * this attempts to program the device in the resolving list as well and
+ * properly set the privacy mode.
 */
 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
                                     struct hci_conn_params *params,
@@ -1590,11 +1624,6 @@ static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
      struct hci_cp_le_add_to_accept_list cp;
      int err;
 
-     /* Already in accept list */
-     if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
-                                params->addr_type))
-             return 0;
-
      /* Select filter policy to accept all advertising */
      if (*num_entries >= hdev->le_accept_list_size)
              return -ENOSPC;
@@ -1620,6 +1649,18 @@ static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
              return err;
      }
 
+     /* Set Privacy Mode */
+     err = hci_le_set_privacy_mode_sync(hdev, params);
+     if (err) {
+             bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
+             return err;

net/bluetooth/hci_sync.c static int hci_le_add_accept_list_sync(struct hci_dev *hdev, · line 1666

Quoted 10 lines
net/bluetooth/hci_sync.c +7 -0
+     }
+
+     /* Check if already in accept list */
+     if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
+                                params->addr_type))
+             return 0;
+
      *num_entries += 1;
      cp.bdaddr_type = params->addr_type;
      bacpy(&cp.bdaddr, &params->addr);

Regards

Marcel

Signature · 1 line
Luiz Augusto von Dentz
maintainer
#4 lore raw
In reply to Luiz Augusto von Dentz

Hi Luiz,

Quoted 44 lines

This introduces HCI_CONN_FLAG_DEVICE_PRIVACY which can be used by userspace to indicate to the controller to use Device Privacy Mode to a specific device.

---
v2: Fix marking Device Privacy Flag even when adapter is not capable of handling Set Privacy Mode.

include/net/bluetooth/hci_core.h | 4 ++++
net/bluetooth/mgmt.c | 24 ++++++++++++++++++++----

2 files changed, 24 insertions(+), 4 deletions(-)
include/net/bluetooth/hci_core.h +4 -0
index b5f061882c10..07d2d099dc2a 100644
@@ -160,6 +160,7 @@ struct bdaddr_list_with_flags {
 
 enum hci_conn_flags {
 	HCI_CONN_FLAG_REMOTE_WAKEUP,
+	HCI_CONN_FLAG_DEVICE_PRIVACY,
 	HCI_CONN_FLAG_MAX
 };
 
@@ -1468,6 +1469,9 @@ void hci_conn_del_sysfs(struct hci_conn *conn);
 #define use_ll_privacy(dev) (ll_privacy_capable(dev) && \
 			     hci_dev_test_flag(dev, HCI_ENABLE_LL_PRIVACY))
 
+#define privacy_mode_capable(dev) (use_ll_privacy(dev) && \
+				   (hdev->commands[39] & 0x04))
+
 /* Use enhanced synchronous connection if command is supported */
 #define enhanced_sco_capable(dev) ((dev)->commands[29] & 0x08)
 
net/bluetooth/mgmt.c +0 -1
index 06384d761928..8a8376d32be3 100644
@@ -4350,7 +4350,16 @@ static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
 			       MGMT_STATUS_NOT_SUPPORTED);
 }
 
-#define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1)

net/bluetooth/mgmt.c static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, · line 4362

Quoted 10 lines
net/bluetooth/mgmt.c +10 -0
+static u32 supported_device_flags(struct hci_dev *hdev)
+{
+	u32 flags = BIT(HCI_CONN_FLAG_MAX) - 1;
+
+	/* Check if Privacy Mode can be set */
+	if (!privacy_mode_capable(hdev))
+		flags &= ~BIT(HCI_CONN_FLAG_DEVICE_PRIVACY);
+
+	return flags;
+}

I am lost on what we are doing, I know that SUPPORTED_DEVICE_FLAGS was introduced by 4c54bf2b093bb from Abhishek, but I fail to reason now why it is correct.

If we really set all bits on the supported device flags, then that is a bug. Or is it too early for me to grok this code?

Quoted 39 lines
net/bluetooth/mgmt.c +9 -2
 static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
 			    u16 data_len)
@@ -4359,7 +4368,7 @@ static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
 	struct mgmt_rp_get_device_flags rp;
 	struct bdaddr_list_with_flags *br_params;
 	struct hci_conn_params *params;
-	u32 supported_flags = SUPPORTED_DEVICE_FLAGS();
+	u32 supported_flags = supported_device_flags(hdev);
 	u32 current_flags = 0;
 	u8 status = MGMT_STATUS_INVALID_PARAMS;
 
@@ -4423,7 +4432,7 @@ static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
 	struct bdaddr_list_with_flags *br_params;
 	struct hci_conn_params *params;
 	u8 status = MGMT_STATUS_INVALID_PARAMS;
-	u32 supported_flags = SUPPORTED_DEVICE_FLAGS();
+	u32 supported_flags = supported_device_flags(hdev);
 	u32 current_flags = __le32_to_cpu(cp->current_flags);
 
 	bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x",
@@ -4456,6 +4465,13 @@ static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
 		if (params) {
 			params->current_flags = current_flags;
 			status = MGMT_STATUS_SUCCESS;
+
+			/* Update passive scan if HCI_CONN_FLAG_DEVICE_PRIVACY
+			 * has been set.
+			 */
+			if (hci_conn_test_flag(HCI_CONN_FLAG_DEVICE_PRIVACY,
+					       params->current_flags))
+				hci_update_passive_scan(hdev);
 		} else {
 			bt_dev_warn(hdev, "No such LE device %pMR (0x%x)",
 				    &cp->addr.bdaddr,
@@ -7061,7 +7077,7 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,

added:

	device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
	device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type,

net/bluetooth/mgmt.c static int add_device(struct sock *sk, struct hci_dev *hdev, · line 7080

Quoted 2 lines
net/bluetooth/mgmt.c +1 -1
-			     SUPPORTED_DEVICE_FLAGS(), current_flags);
+			     supported_device_flags(hdev), current_flags);

Regards

Marcel

author maintainer
#5 lore raw

Hi Marcel,

Quoted 46 lines · Marcel Holtmann

Hi Luiz,

Quoted 43 lines

This introduces HCI_CONN_FLAG_DEVICE_PRIVACY which can be used by userspace to indicate to the controller to use Device Privacy Mode to a specific device.

---
v2: Fix marking Device Privacy Flag even when adapter is not capable of handling Set Privacy Mode.

include/net/bluetooth/hci_core.h | 4 ++++
net/bluetooth/mgmt.c | 24 ++++++++++++++++++++----

2 files changed, 24 insertions(+), 4 deletions(-)
include/net/bluetooth/hci_core.h +4 -0
index b5f061882c10..07d2d099dc2a 100644
@@ -160,6 +160,7 @@ struct bdaddr_list_with_flags {
 
 enum hci_conn_flags {
      HCI_CONN_FLAG_REMOTE_WAKEUP,
+     HCI_CONN_FLAG_DEVICE_PRIVACY,
      HCI_CONN_FLAG_MAX
 };
 
@@ -1468,6 +1469,9 @@ void hci_conn_del_sysfs(struct hci_conn *conn);
 #define use_ll_privacy(dev) (ll_privacy_capable(dev) && \
                           hci_dev_test_flag(dev, HCI_ENABLE_LL_PRIVACY))
 
+#define privacy_mode_capable(dev) (use_ll_privacy(dev) && \
+                                (hdev->commands[39] & 0x04))
+
 /* Use enhanced synchronous connection if command is supported */
 #define enhanced_sco_capable(dev) ((dev)->commands[29] & 0x08)
 
net/bluetooth/mgmt.c +0 -0
index 06384d761928..8a8376d32be3 100644
@@ -4350,7 +4350,16 @@ static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
                             MGMT_STATUS_NOT_SUPPORTED);
 }
 
Quoted 13 lines

net/bluetooth/mgmt.c static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, · line 4362

Quoted 11 lines
net/bluetooth/mgmt.c +10 -1
-#define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1)
+static u32 supported_device_flags(struct hci_dev *hdev)
+{
+     u32 flags = BIT(HCI_CONN_FLAG_MAX) - 1;
+
+     /* Check if Privacy Mode can be set */
+     if (!privacy_mode_capable(hdev))
+             flags &= ~BIT(HCI_CONN_FLAG_DEVICE_PRIVACY);
+
+     return flags;
+}

I am lost on what we are doing, I know that SUPPORTED_DEVICE_FLAGS was introduced by 4c54bf2b093bb from Abhishek, but I fail to reason now why it is correct.

But we don't set the HCI_CONN_FLAG_DEVICE_PRIVACY is hdev is not capable of setting it, anyway the general idea of the supported_flags is to indicate to userspace what flags the kernel is capable of settings, so yeah I would expected it to be capable of setting every flag except for those the controller don't have proper support for. Maybe you got confused by the logic of first enabling everything and then toggle back the bits that are not supported by the hdev.

Quoted 48 lines

If we really set all bits on the supported device flags, then that is a bug. Or is it too early for me to grok this code?

Quoted 39 lines
net/bluetooth/mgmt.c +9 -2
 static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
                          u16 data_len)
@@ -4359,7 +4368,7 @@ static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
      struct mgmt_rp_get_device_flags rp;
      struct bdaddr_list_with_flags *br_params;
      struct hci_conn_params *params;
-     u32 supported_flags = SUPPORTED_DEVICE_FLAGS();
+     u32 supported_flags = supported_device_flags(hdev);
      u32 current_flags = 0;
      u8 status = MGMT_STATUS_INVALID_PARAMS;
 
@@ -4423,7 +4432,7 @@ static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
      struct bdaddr_list_with_flags *br_params;
      struct hci_conn_params *params;
      u8 status = MGMT_STATUS_INVALID_PARAMS;
-     u32 supported_flags = SUPPORTED_DEVICE_FLAGS();
+     u32 supported_flags = supported_device_flags(hdev);
      u32 current_flags = __le32_to_cpu(cp->current_flags);
 
      bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x",
@@ -4456,6 +4465,13 @@ static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
              if (params) {
                      params->current_flags = current_flags;
                      status = MGMT_STATUS_SUCCESS;
+
+                     /* Update passive scan if HCI_CONN_FLAG_DEVICE_PRIVACY
+                      * has been set.
+                      */
+                     if (hci_conn_test_flag(HCI_CONN_FLAG_DEVICE_PRIVACY,
+                                            params->current_flags))
+                             hci_update_passive_scan(hdev);
              } else {
                      bt_dev_warn(hdev, "No such LE device %pMR (0x%x)",
                                  &cp->addr.bdaddr,
@@ -7061,7 +7077,7 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,

added:

      device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
      device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type,

net/bluetooth/mgmt.c static int add_device(struct sock *sk, struct hci_dev *hdev, · line 7080

Quoted 2 lines
net/bluetooth/mgmt.c +1 -1
-                          SUPPORTED_DEVICE_FLAGS(), current_flags);
+                          supported_device_flags(hdev), current_flags);

Regards

Marcel

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

Hi Luiz,

Quoted 57 lines
Quoted 43 lines
Quoted 43 lines

This introduces HCI_CONN_FLAG_DEVICE_PRIVACY which can be used by userspace to indicate to the controller to use Device Privacy Mode to a specific device.

---
v2: Fix marking Device Privacy Flag even when adapter is not capable of handling Set Privacy Mode.

include/net/bluetooth/hci_core.h | 4 ++++
net/bluetooth/mgmt.c | 24 ++++++++++++++++++++----

2 files changed, 24 insertions(+), 4 deletions(-)
include/net/bluetooth/hci_core.h +4 -0
index b5f061882c10..07d2d099dc2a 100644
@@ -160,6 +160,7 @@ struct bdaddr_list_with_flags {
 
 enum hci_conn_flags {
     HCI_CONN_FLAG_REMOTE_WAKEUP,
+     HCI_CONN_FLAG_DEVICE_PRIVACY,
     HCI_CONN_FLAG_MAX
 };
 
@@ -1468,6 +1469,9 @@ void hci_conn_del_sysfs(struct hci_conn *conn);
 #define use_ll_privacy(dev) (ll_privacy_capable(dev) && \
                          hci_dev_test_flag(dev, HCI_ENABLE_LL_PRIVACY))
 
+#define privacy_mode_capable(dev) (use_ll_privacy(dev) && \
+                                (hdev->commands[39] & 0x04))
+
 /* Use enhanced synchronous connection if command is supported */
 #define enhanced_sco_capable(dev) ((dev)->commands[29] & 0x08)
 
net/bluetooth/mgmt.c +0 -0
index 06384d761928..8a8376d32be3 100644
@@ -4350,7 +4350,16 @@ static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
                            MGMT_STATUS_NOT_SUPPORTED);
 }
 
Quoted 13 lines

net/bluetooth/mgmt.c static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, · line 4362

Quoted 11 lines
net/bluetooth/mgmt.c +10 -1
-#define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1)
+static u32 supported_device_flags(struct hci_dev *hdev)
+{
+     u32 flags = BIT(HCI_CONN_FLAG_MAX) - 1;
+
+     /* Check if Privacy Mode can be set */
+     if (!privacy_mode_capable(hdev))
+             flags &= ~BIT(HCI_CONN_FLAG_DEVICE_PRIVACY);
+
+     return flags;
+}

I am lost on what we are doing, I know that SUPPORTED_DEVICE_FLAGS was introduced by 4c54bf2b093bb from Abhishek, but I fail to reason now why it is correct.

Quoted 7 lines

But we don't set the HCI_CONN_FLAG_DEVICE_PRIVACY is hdev is not capable of setting it, anyway the general idea of the supported_flags is to indicate to userspace what flags the kernel is capable of settings, so yeah I would expected it to be capable of setting every flag except for those the controller don't have proper support for. Maybe you got confused by the logic of first enabling everything and then toggle back the bits that are not supported by the hdev.

and I think that is wrong. We should only set the bits that the hardware and/or kernel supports. I have no idea on how I missed this in the review back then. I am trying to figure it out, but I am failing to follow this logic.

Regards

Marcel

author maintainer
#7 lore raw

Hi Marcel,

Quoted 68 lines · Marcel Holtmann

Hi Luiz,

Quoted 57 lines
Quoted 43 lines
Quoted 43 lines

This introduces HCI_CONN_FLAG_DEVICE_PRIVACY which can be used by userspace to indicate to the controller to use Device Privacy Mode to a specific device.

---
v2: Fix marking Device Privacy Flag even when adapter is not capable of handling Set Privacy Mode.

include/net/bluetooth/hci_core.h | 4 ++++
net/bluetooth/mgmt.c | 24 ++++++++++++++++++++----

2 files changed, 24 insertions(+), 4 deletions(-)
include/net/bluetooth/hci_core.h +4 -0
index b5f061882c10..07d2d099dc2a 100644
@@ -160,6 +160,7 @@ struct bdaddr_list_with_flags {
 
 enum hci_conn_flags {
     HCI_CONN_FLAG_REMOTE_WAKEUP,
+     HCI_CONN_FLAG_DEVICE_PRIVACY,
     HCI_CONN_FLAG_MAX
 };
 
@@ -1468,6 +1469,9 @@ void hci_conn_del_sysfs(struct hci_conn *conn);
 #define use_ll_privacy(dev) (ll_privacy_capable(dev) && \
                          hci_dev_test_flag(dev, HCI_ENABLE_LL_PRIVACY))
 
+#define privacy_mode_capable(dev) (use_ll_privacy(dev) && \
+                                (hdev->commands[39] & 0x04))
+
 /* Use enhanced synchronous connection if command is supported */
 #define enhanced_sco_capable(dev) ((dev)->commands[29] & 0x08)
 
net/bluetooth/mgmt.c +0 -0
index 06384d761928..8a8376d32be3 100644
@@ -4350,7 +4350,16 @@ static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
                            MGMT_STATUS_NOT_SUPPORTED);
 }
 
Quoted 13 lines

net/bluetooth/mgmt.c static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, · line 4362

Quoted 11 lines
net/bluetooth/mgmt.c +10 -1
-#define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1)
+static u32 supported_device_flags(struct hci_dev *hdev)
+{
+     u32 flags = BIT(HCI_CONN_FLAG_MAX) - 1;
+
+     /* Check if Privacy Mode can be set */
+     if (!privacy_mode_capable(hdev))
+             flags &= ~BIT(HCI_CONN_FLAG_DEVICE_PRIVACY);
+
+     return flags;
+}

I am lost on what we are doing, I know that SUPPORTED_DEVICE_FLAGS was introduced by 4c54bf2b093bb from Abhishek, but I fail to reason now why it is correct.

Quoted 7 lines

But we don't set the HCI_CONN_FLAG_DEVICE_PRIVACY is hdev is not capable of setting it, anyway the general idea of the supported_flags is to indicate to userspace what flags the kernel is capable of settings, so yeah I would expected it to be capable of setting every flag except for those the controller don't have proper support for. Maybe you got confused by the logic of first enabling everything and then toggle back the bits that are not supported by the hdev.

Quoted 1 line

and I think that is wrong. We should only set the bits that the hardware and/or kernel supports. I have no idea on how I missed this in the review back then. I am trying to figure it out, but I am failing to follow this logic.

There is only 1 flag currently though, HCI_CONN_FLAG_REMOTE_WAKEUP, so I wonder if that really requires a check? Or perhaps you want to change the way we supported_device_flags works to:

index fe52b7eefb56..3ad09ce6eaeb 100644

net/bluetooth/mgmt.c +0 -0
@@ -4351,11 +4351,15 @@ static int set_exp_feature(struct sock *sk,

struct hci_dev *hdev,

static u32 supported_device_flags(struct hci_dev *hdev) {
- u32 flags = BIT(HCI_CONN_FLAG_MAX) - 1;
+ u32 flags = 0;
+
+ /* Check if adapter can wakeup the system */
+ if (hdev->wakeup && hdev->wakeup(hdev))
+ flags |= HCI_CONN_FLAG_REMOTE_WAKEUP;

        /* Check if Privacy Mode can be set */

- if (!privacy_mode_capable(hdev))
- flags &= ~BIT(HCI_CONN_FLAG_DEVICE_PRIVACY);
+ if (privacy_mode_capable(hdev))
+ flags |= HCI_CONN_FLAG_DEVICE_PRIVACY;

        return flags;

}

We will need to adjust some of the tests though to reflect this change in behavior:

et Device Flags - Success Failed 0.016 seconds
Set Device Flags - Success Not Run

maintainer
#8 lore raw

Hi Luiz,

Quoted 93 lines
Quoted 65 lines
Quoted 57 lines
Quoted 43 lines
Quoted 43 lines

This introduces HCI_CONN_FLAG_DEVICE_PRIVACY which can be used by userspace to indicate to the controller to use Device Privacy Mode to a specific device.

---
v2: Fix marking Device Privacy Flag even when adapter is not capable of handling Set Privacy Mode.

include/net/bluetooth/hci_core.h | 4 ++++
net/bluetooth/mgmt.c | 24 ++++++++++++++++++++----

2 files changed, 24 insertions(+), 4 deletions(-)
include/net/bluetooth/hci_core.h +4 -0
index b5f061882c10..07d2d099dc2a 100644
@@ -160,6 +160,7 @@ struct bdaddr_list_with_flags {
 
 enum hci_conn_flags {
    HCI_CONN_FLAG_REMOTE_WAKEUP,
+     HCI_CONN_FLAG_DEVICE_PRIVACY,
    HCI_CONN_FLAG_MAX
 };
 
@@ -1468,6 +1469,9 @@ void hci_conn_del_sysfs(struct hci_conn *conn);
 #define use_ll_privacy(dev) (ll_privacy_capable(dev) && \
                         hci_dev_test_flag(dev, HCI_ENABLE_LL_PRIVACY))
 
+#define privacy_mode_capable(dev) (use_ll_privacy(dev) && \
+                                (hdev->commands[39] & 0x04))
+
 /* Use enhanced synchronous connection if command is supported */
 #define enhanced_sco_capable(dev) ((dev)->commands[29] & 0x08)
 
net/bluetooth/mgmt.c +0 -0
index 06384d761928..8a8376d32be3 100644
@@ -4350,7 +4350,16 @@ static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
                           MGMT_STATUS_NOT_SUPPORTED);
 }
 
Quoted 13 lines

net/bluetooth/mgmt.c static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, · line 4362

Quoted 11 lines
net/bluetooth/mgmt.c +10 -1
-#define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1)
+static u32 supported_device_flags(struct hci_dev *hdev)
+{
+     u32 flags = BIT(HCI_CONN_FLAG_MAX) - 1;
+
+     /* Check if Privacy Mode can be set */
+     if (!privacy_mode_capable(hdev))
+             flags &= ~BIT(HCI_CONN_FLAG_DEVICE_PRIVACY);
+
+     return flags;
+}

I am lost on what we are doing, I know that SUPPORTED_DEVICE_FLAGS was introduced by 4c54bf2b093bb from Abhishek, but I fail to reason now why it is correct.

Quoted 7 lines

But we don't set the HCI_CONN_FLAG_DEVICE_PRIVACY is hdev is not capable of setting it, anyway the general idea of the supported_flags is to indicate to userspace what flags the kernel is capable of settings, so yeah I would expected it to be capable of setting every flag except for those the controller don't have proper support for. Maybe you got confused by the logic of first enabling everything and then toggle back the bits that are not supported by the hdev.

Quoted 1 line

and I think that is wrong. We should only set the bits that the hardware and/or kernel supports. I have no idea on how I missed this in the review back then. I am trying to figure it out, but I am failing to follow this logic.

There is only 1 flag currently though, HCI_CONN_FLAG_REMOTE_WAKEUP, so I wonder if that really requires a check? Or perhaps you want to change the way we supported_device_flags works to:

index fe52b7eefb56..3ad09ce6eaeb 100644

net/bluetooth/mgmt.c +7 -3
@@ -4351,11 +4351,15 @@ static int set_exp_feature(struct sock *sk,
 struct hci_dev *hdev,
 
 static u32 supported_device_flags(struct hci_dev *hdev)
 {
-       u32 flags = BIT(HCI_CONN_FLAG_MAX) - 1;
+       u32 flags = 0;
+
+       /* Check if adapter can wakeup the system */
+       if (hdev->wakeup && hdev->wakeup(hdev))
+               flags |= HCI_CONN_FLAG_REMOTE_WAKEUP;
 
       /* Check if Privacy Mode can be set */
-       if (!privacy_mode_capable(hdev))
-               flags &= ~BIT(HCI_CONN_FLAG_DEVICE_PRIVACY);
+       if (privacy_mode_capable(hdev))
+               flags |= HCI_CONN_FLAG_DEVICE_PRIVACY;
 
       return flags;
Quoted 1 line

}

yes, that is how I would expected it to be.

Regards

Marcel