Skip to contentSkip to conversation

LE Privacy support

PATCH In review [email protected] 2 participants 12 messages Feb 23, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover LE Privacy support

Hi,

Here's a new set of patches for the ones that didn't go through from the initial set.

Johan

----------------------------------------------------------------
Johan Hedberg (10):
Bluetooth: Set the correct address type in Identity Address Information
Bluetooth: Add SMP function for generating RPAs
Bluetooth: Add timer for regenerating local RPA
Bluetooth: Add hci_get_own_address_type() convenience function
Bluetooth: Use hci_get_own_address_type() when connecting LE
Bluetooth: Use hci_get_own_address_type() for enabling advertising
Bluetooth: Use hci_get_own_address_type() for initiating LE scan
Bluetooth: Don't write static address during power on
Bluetooth: Add debugfs entry for RPA regeneration timeout
Bluetooth: Add support for Set Privacy command

 include/net/bluetooth/hci.h      |  1 +
 include/net/bluetooth/hci_core.h |  7 ++++
 net/bluetooth/hci_conn.c         |  2 +-
 net/bluetooth/hci_core.c         | 75 +++++++++++++++++++++++++++++++++
 net/bluetooth/mgmt.c             | 80 ++++++++++++++++++++++++++++++++----
 net/bluetooth/smp.c              | 21 +++++++++-
 net/bluetooth/smp.h              |  1 +
 7 files changed, 176 insertions(+), 11 deletions(-)
#1 lore raw

PATCH v2 1/10 Bluetooth: Set the correct address type in Identity Address Information

From: Johan Hedberg <[email protected]>

The SMP Identity Address Information PDU contains our Identity Address. The address type can either be static random or public and it is tracked in the hdev->own_address_type variable.

---
 net/bluetooth/smp.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/bluetooth/smp.c +1 -2
index 8ef50c790b96..ce2c461d07e3 100644
@@ -1196,8 +1196,7 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
 
 		smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
 
-		/* Just public address */
-		memset(&addrinfo, 0, sizeof(addrinfo));
+		addrinfo.addr_type = hdev->own_addr_type;
 		bacpy(&addrinfo.bdaddr, &hcon->src);
 
 		smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
Signature · 1 line
1.8.5.3
maintainer
#2 lore raw

Re: [PATCH v2 01/10] Bluetooth: Set the correct address type in Identity Address Information

Hi Johan,

Quoted 14 lines

The SMP Identity Address Information PDU contains our Identity Address. The address type can either be static random or public and it is tracked in the hdev->own_address_type variable.

---
net/bluetooth/smp.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
net/bluetooth/smp.c +0 -0
index 8ef50c790b96..ce2c461d07e3 100644

net/bluetooth/smp.c int smp_distribute_keys(struct l2cap_conn *conn, __u8 force) · line 1200

Quoted 7 lines
net/bluetooth/smp.c +1 -2
@@ -1196,8 +1196,7 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
 
 		smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
 
-		/* Just public address */
-		memset(&addrinfo, 0, sizeof(addrinfo));
+		addrinfo.addr_type = hdev->own_addr_type;
 		bacpy(&addrinfo.bdaddr, &hcon->src);

why not use hcon->src_type here. Otherwise you have to track the difference between when the public address is used vs the static address and force_static_address debugfs.

Regards

Marcel

#3 lore raw

PATCH v2 2/10 Bluetooth: Add SMP function for generating RPAs

In reply to [email protected]

From: Johan Hedberg <[email protected]>

We need a function in smp.c to generate Resolvable Random Addresses in order to support privacy. The local RPA will need to be generated before advertising, scanning or connecting and regenerated at periodic intervals. This patch adds the necessary function for RPA generation.

---
 net/bluetooth/smp.c | 18 ++++++++++++++++++
 net/bluetooth/smp.h |  1 +
 2 files changed, 19 insertions(+)
net/bluetooth/smp.c +18 -0
index ce2c461d07e3..71a15233d1b6 100644
@@ -124,6 +124,24 @@ bool smp_irk_matches(struct crypto_blkcipher *tfm, u8 irk[16],
 	return !memcmp(bdaddr->b, hash, 3);
 }
 
+int smp_generate_rpa(struct crypto_blkcipher *tfm, u8 irk[16], bdaddr_t *rpa)
+{
+	int err;
+
+	get_random_bytes(&rpa->b[3], 3);
+
+	rpa->b[5] &= 0x3f;
+	rpa->b[5] |= 0x40;
+
+	err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
+	if (err < 0)
+		return err;
+
+	BT_DBG("RPA %pMR", rpa);
+
+	return 0;
+}
+
 static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],
 		  u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia,
 		  u8 _rat, bdaddr_t *ra, u8 res[16])
net/bluetooth/smp.h +1 -0
index d8cc543f523c..f32f1212f650 100644
@@ -152,5 +152,6 @@ void smp_chan_destroy(struct l2cap_conn *conn);
 
 bool smp_irk_matches(struct crypto_blkcipher *tfm, u8 irk[16],
 		     bdaddr_t *bdaddr);
+int smp_generate_rpa(struct crypto_blkcipher *tfm, u8 irk[16], bdaddr_t *rpa);
 
 #endif /* __SMP_H */
Signature · 1 line
1.8.5.3
#4 lore raw

PATCH v2 3/10 Bluetooth: Add timer for regenerating local RPA

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This patch adds a timer for updating the local RPA periodically. The default timeout is set to 15 minutes.

---
 include/net/bluetooth/hci.h      |  1 +
 include/net/bluetooth/hci_core.h |  5 +++++
 net/bluetooth/hci_core.c         |  4 ++++
 net/bluetooth/mgmt.c             | 23 +++++++++++++++++++++++
 4 files changed, 33 insertions(+)
include/net/bluetooth/hci.h +1 -0
index 5ff885ff29df..1bb45a47a78a 100644
@@ -127,6 +127,7 @@ enum {
 	HCI_SC_ENABLED,
 	HCI_SC_ONLY,
 	HCI_PRIVACY,
+	HCI_RPA_EXPIRED,
 	HCI_RPA_RESOLVING,
 	HCI_HS_ENABLED,
 	HCI_LE_ENABLED,
include/net/bluetooth/hci_core.h +5 -0
index 68bbcabdd9fd..6415514e4f17 100644
@@ -130,6 +130,9 @@ struct oob_data {
 
 #define HCI_MAX_SHORT_NAME_LENGTH	10
 
+/* Default LE RPA expiry time, 15 minutes */
+#define HCI_DEFAULT_RPA_TIMEOUT		(15 * 60)
+
 struct amp_assoc {
 	__u16	len;
 	__u16	offset;
@@ -304,6 +307,8 @@ struct hci_dev {
 	__u8			scan_rsp_data_len;
 
 	__u8			irk[16];
+	__u32			rpa_timeout;
+	struct delayed_work	rpa_expired;
 
 	int (*open)(struct hci_dev *hdev);
 	int (*close)(struct hci_dev *hdev);
net/bluetooth/hci_core.c +4 -0
index 964aa8deb009..92d35811b61e 100644
@@ -2102,6 +2102,7 @@ static int hci_dev_do_open(struct hci_dev *hdev)
 
 	if (!ret) {
 		hci_dev_hold(hdev);
+		set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
 		set_bit(HCI_UP, &hdev->flags);
 		hci_notify(hdev, HCI_DEV_UP);
 		if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
@@ -2199,6 +2200,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 		cancel_delayed_work(&hdev->service_cache);
 
 	cancel_delayed_work_sync(&hdev->le_scan_disable);
+	cancel_delayed_work_sync(&hdev->rpa_expired);
 
 	hci_dev_lock(hdev);
 	hci_inquiry_cache_flush(hdev);
@@ -3300,6 +3302,8 @@ struct hci_dev *hci_alloc_dev(void)
 	hdev->le_conn_min_interval = 0x0028;
 	hdev->le_conn_max_interval = 0x0038;
 
+	hdev->rpa_timeout = HCI_DEFAULT_RPA_TIMEOUT;
+
 	mutex_init(&hdev->lock);
 	mutex_init(&hdev->req_lock);
 
net/bluetooth/mgmt.c +23 -0
index 301b18a1c6a0..09316dd2cce1 100644
@@ -881,12 +881,35 @@ static void service_cache_off(struct work_struct *work)
 	hci_req_run(&req, NULL);
 }
 
+static void rpa_expired(struct work_struct *work)
+{
+	struct hci_dev *hdev = container_of(work, struct hci_dev,
+					    rpa_expired.work);
+	struct hci_request req;
+
+	BT_DBG("");
+
+	set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
+
+	if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) ||
+	    hci_conn_num(hdev, LE_LINK) > 0)
+		return;
+
+	hci_req_init(&req, hdev);
+
+	disable_advertising(&req);
+	enable_advertising(&req);
+
+	hci_req_run(&req, NULL);
+}
+
 static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
 {
 	if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags))
 		return;
 
 	INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
+	INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
 
 	/* Non-mgmt controlled devices get this bit set
 	 * implicitly so that pairing works for them, however
Signature · 1 line
1.8.5.3
#5 lore raw

PATCH v2 4/10 Bluetooth: Add hci_get_own_address_type() convenience function

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This patch adds a convenience function for fetching the local address type used for advertising, scanning and initiating LE connections as well as updating the local random address if necessary.

---
 include/net/bluetooth/hci_core.h |  2 ++
 net/bluetooth/hci_core.c         | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
include/net/bluetooth/hci_core.h +2 -0
index 6415514e4f17..996887793d4b 100644
@@ -1257,6 +1257,8 @@ void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
 							__u8 ltk[16]);
 
+uint8_t hci_get_own_address_type(struct hci_request *req);
+
 #define SCO_AIRMODE_MASK       0x0003
 #define SCO_AIRMODE_CVSD       0x0000
 #define SCO_AIRMODE_TRANSP     0x0003
net/bluetooth/hci_core.c +38 -0
index 92d35811b61e..973145ad8bf0 100644
@@ -3276,6 +3276,44 @@ static void le_scan_disable_work(struct work_struct *work)
 		BT_ERR("Disable LE scanning request failed: err %d", err);
 }
 
+uint8_t hci_get_own_address_type(struct hci_request *req)
+{
+	struct hci_dev *hdev = req->hdev;
+
+	if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) {
+		bdaddr_t rpa;
+
+		if (!test_and_clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags))
+			return ADDR_LE_DEV_RANDOM;
+
+		if (smp_generate_rpa(hdev->tfm_aes, hdev->irk, &rpa) < 0) {
+			BT_ERR("%s failed to generate new RPA", hdev->name);
+			return hdev->own_addr_type;
+		}
+
+		hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, &rpa);
+
+		if (hdev->rpa_timeout) {
+			int timeout;
+
+			timeout = msecs_to_jiffies(hdev->rpa_timeout * 1000);
+			queue_delayed_work(hdev->workqueue, &hdev->rpa_expired,
+					   timeout);
+		}
+
+		return ADDR_LE_DEV_RANDOM;
+	}
+
+	if (bacmp(&hdev->static_addr, BDADDR_ANY) &&
+	    bacmp(&hdev->static_addr, &hdev->random_addr)) {
+		hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
+			    &hdev->static_addr);
+		return ADDR_LE_DEV_RANDOM;
+	}
+
+	return hdev->own_addr_type;
+}
+
 /* Alloc HCI device */
 struct hci_dev *hci_alloc_dev(void)
 {
Signature · 1 line
1.8.5.3
#6 lore raw

PATCH v2 5/10 Bluetooth: Use hci_get_own_address_type() when connecting LE

In reply to [email protected]

From: Johan Hedberg <[email protected]>

When we initiate LE connections we need to update the local random address if necessary. This patch updates the LE connection creation mechanism to use the new hci_get_own_address_type() function to set the own_address_type parameter and to update the local random address if necessary.

---
 net/bluetooth/hci_conn.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/hci_conn.c +1 -1
index bd66c52eff95..5be2a05f2795 100644
@@ -564,7 +564,7 @@ static int hci_create_le_conn(struct hci_conn *conn)
 	cp.scan_interval = cpu_to_le16(hdev->le_scan_interval);
 	cp.scan_window = cpu_to_le16(hdev->le_scan_window);
 	bacpy(&cp.peer_addr, &conn->dst);
-	cp.peer_addr_type = conn->dst_type;
+	cp.peer_addr_type = hci_get_own_address_type(&req);
 	cp.own_address_type = conn->src_type;
 	cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
 	cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
Signature · 1 line
1.8.5.3
#7 lore raw

PATCH v2 6/10 Bluetooth: Use hci_get_own_address_type() for enabling advertising

In reply to [email protected]

From: Johan Hedberg <[email protected]>

When we enable advertising we need to update the local random address if necessary. This patch takes advantage of the hci_get_own_address_type() function to set the own_address_type variable and to update the local random address if necessary.

---
 net/bluetooth/mgmt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/mgmt.c +1 -1
index 09316dd2cce1..e57dc93af331 100644
@@ -845,7 +845,7 @@ static void enable_advertising(struct hci_request *req)
 	cp.min_interval = __constant_cpu_to_le16(0x0800);
 	cp.max_interval = __constant_cpu_to_le16(0x0800);
 	cp.type = get_adv_type(hdev);
-	cp.own_address_type = hdev->own_addr_type;
+	cp.own_address_type = hci_get_own_address_type(req);
 	cp.channel_map = hdev->le_adv_channel_map;
 
 	hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
Signature · 1 line
1.8.5.3
#8 lore raw

PATCH v2 7/10 Bluetooth: Use hci_get_own_address_type() for initiating LE scan

In reply to [email protected]

From: Johan Hedberg <[email protected]>

When we start LE scanning we need to update the local random address if necessary. This patch updates the code to use hci_get_own_address_type() for setting the own_address_type scan parameter and updating the local random address if necessary.

---
 net/bluetooth/mgmt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/mgmt.c +1 -1
index e57dc93af331..467ca21e04d6 100644
@@ -3382,7 +3382,7 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
 		param_cp.type = LE_SCAN_ACTIVE;
 		param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT);
 		param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
-		param_cp.own_address_type = hdev->own_addr_type;
+		param_cp.own_address_type = hci_get_own_address_type(&req);
 		hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
 			    &param_cp);
 
Signature · 1 line
1.8.5.3
#9 lore raw

PATCH v2 8/10 Bluetooth: Don't write static address during power on

In reply to [email protected]

From: Johan Hedberg <[email protected]>

Since we always update the random address before enabling advertising, scanning and initiating LE connections there is no need to write the random address add power on.

---
 net/bluetooth/mgmt.c | 5 -----
 1 file changed, 5 deletions(-)
net/bluetooth/mgmt.c +0 -5
index 467ca21e04d6..9934441f7cf8 100644
@@ -4591,11 +4591,6 @@ static int powered_update_hci(struct hci_dev *hdev)
 	}
 
 	if (lmp_le_capable(hdev)) {
-		/* Set random address to static address if configured */
-		if (bacmp(&hdev->static_addr, BDADDR_ANY))
-			hci_req_add(&req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
-				    &hdev->static_addr);
-
 		/* Make sure the controller has a good default for
 		 * advertising data. This also applies to the case
 		 * where BR/EDR was toggled during the AUTO_OFF phase.
Signature · 1 line
1.8.5.3
#10 lore raw

PATCH v2 9/10 Bluetooth: Add debugfs entry for RPA regeneration timeout

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This patch adds a rpa_timeout debugfs entry which can be used to set the RPA regeneration timeout to something else than the default 15 minutes.

---
 net/bluetooth/hci_core.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
net/bluetooth/hci_core.c +33 -0
index 973145ad8bf0..73fe644949d5 100644
@@ -492,6 +492,37 @@ static int idle_timeout_get(void *data, u64 *val)
 DEFINE_SIMPLE_ATTRIBUTE(idle_timeout_fops, idle_timeout_get,
 			idle_timeout_set, "%llu\n");
 
+static int rpa_timeout_set(void *data, u64 val)
+{
+	struct hci_dev *hdev = data;
+
+	/* Require the RPA timeout to be at least 30 seconds and at most
+	 * 24 hours.
+	 */
+	if (val < 30 || val > (60 * 60 * 24))
+		return -EINVAL;
+
+	hci_dev_lock(hdev);
+	hdev->rpa_timeout = val;
+	hci_dev_unlock(hdev);
+
+	return 0;
+}
+
+static int rpa_timeout_get(void *data, u64 *val)
+{
+	struct hci_dev *hdev = data;
+
+	hci_dev_lock(hdev);
+	*val = hdev->rpa_timeout;
+	hci_dev_unlock(hdev);
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(rpa_timeout_fops, rpa_timeout_get,
+			rpa_timeout_set, "%llu\n");
+
 static int sniff_min_interval_set(void *data, u64 val)
 {
 	struct hci_dev *hdev = data;
@@ -1612,6 +1643,8 @@ static int __hci_init(struct hci_dev *hdev)
 				    hdev, &random_address_fops);
 		debugfs_create_file("static_address", 0444, hdev->debugfs,
 				    hdev, &static_address_fops);
+		debugfs_create_file("rpa_timeout", 0644, hdev->debugfs,
+				    hdev, &rpa_timeout_fops);
 
 		/* For controllers with a public address, provide a debug
 		 * option to force the usage of the configured static
Signature · 1 line
1.8.5.3
#11 lore raw

PATCH v2 10/10 Bluetooth: Add support for Set Privacy command

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This patch adds support for handing the Set Privacy mgmt command, including copying the value to hdev->irk and toggling the HCI_PRIVACY flag.

---
 net/bluetooth/mgmt.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)
net/bluetooth/mgmt.c +47 -1
index 9934441f7cf8..657ef654cfd9 100644
@@ -81,6 +81,7 @@ static const u16 mgmt_commands[] = {
 	MGMT_OP_SET_SCAN_PARAMS,
 	MGMT_OP_SET_SECURE_CONN,
 	MGMT_OP_SET_DEBUG_KEYS,
+	MGMT_OP_SET_PRIVACY,
 	MGMT_OP_LOAD_IRKS,
 };
 
@@ -4210,6 +4211,51 @@ unlock:
 	return err;
 }
 
+static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
+		       u16 len)
+{
+	struct mgmt_cp_set_privacy *cp = cp_data;
+	bool changed;
+	int err;
+
+	BT_DBG("request for %s", hdev->name);
+
+	if (!lmp_le_capable(hdev))
+		return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
+				  MGMT_STATUS_NOT_SUPPORTED);
+
+	if (cp->privacy != 0x00 && cp->privacy != 0x01)
+		return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
+				  MGMT_STATUS_INVALID_PARAMS);
+
+	if (hdev_is_powered(hdev))
+		return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
+				  MGMT_STATUS_REJECTED);
+
+	hci_dev_lock(hdev);
+
+	if (cp->privacy) {
+		changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags);
+		memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
+		set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
+	} else {
+		changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags);
+		memset(hdev->irk, 0, sizeof(hdev->irk));
+		clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
+	}
+
+	err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
+	if (err < 0)
+		goto unlock;
+
+	if (changed)
+		err = new_settings(hdev, sk);
+
+unlock:
+	hci_dev_unlock(hdev);
+	return err;
+}
+
 static bool irk_is_valid(struct mgmt_irk_info *irk)
 {
 	switch (irk->addr.type) {
@@ -4424,7 +4470,7 @@ static const struct mgmt_handler {
 	{ set_scan_params,        false, MGMT_SET_SCAN_PARAMS_SIZE },
 	{ set_secure_conn,        false, MGMT_SETTING_SIZE },
 	{ set_debug_keys,         false, MGMT_SETTING_SIZE },
-	{ },
+	{ set_privacy,            false, MGMT_SET_PRIVACY_SIZE },
 	{ load_irks,              true,  MGMT_LOAD_IRKS_SIZE },
 };
 
Signature · 1 line
1.8.5.3