Skip to contentSkip to conversation

Bluetooth: Add dedicated SMP (de)initialization functions

PATCH In review [email protected] 2 participants 7 messages Aug 8, 2014

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

cover Bluetooth: Add dedicated SMP (de)initialization functions

Hi,

For us to have a clean way to create the root (per-adapter) SMP L2CAP channel it's best to move the code responsible for this to specialized functions in smp.c. For now this only involves the AES crypto context creation and destruction.

Johan

----------------------------------------------------------------
Johan Hedberg (5):
Bluetooth: Fix hci_update_random_address() error return for no crypto
Bluetooth: Fix IRK lookup when tfm_aes is not available
Bluetooth: Refactor SMP (de)initialization into separate functions
Bluetooth: Move SMP initialization after HCI init
Bluetooth: Move SMP (de)initialization to smp.c

 net/bluetooth/hci_core.c | 27 +++++++++++++--------------
 net/bluetooth/smp.c      | 26 ++++++++++++++++++++++++++
 net/bluetooth/smp.h      |  3 +++
 3 files changed, 42 insertions(+), 14 deletions(-)
#1 lore raw

PATCH 1/5 Bluetooth: Fix hci_update_random_address() error return for no crypto

From: Johan Hedberg <[email protected]>

If the AES crypto context is not available we cannot generate new RPAs. We should therefore cleanly return an error from the function responsible for updating the random address.

---
 net/bluetooth/hci_core.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/bluetooth/hci_core.c +6 -0
index 217ef83fb541..860477090d78 100644
@@ -3882,6 +3882,12 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,
 		    !bacmp(&hdev->random_addr, &hdev->rpa))
 			return 0;
 
+		if (!hdev->tfm_aes) {
+			BT_ERR("%s crypto not available to generate RPA",
+			       hdev->name);
+			return -EOPNOTSUPP;
+		}
+
 		err = smp_generate_rpa(hdev->tfm_aes, hdev->irk, &hdev->rpa);
 		if (err < 0) {
 			BT_ERR("%s failed to generate new RPA", hdev->name);
Signature · 1 line
1.9.3
#2 lore raw

PATCH 2/5 Bluetooth: Fix IRK lookup when tfm_aes is not available

In reply to [email protected]

From: Johan Hedberg <[email protected]>

If the AES crypto has not been initialized properly we should cleanly return from the hci_find_irk_by_rpa() function. Right now this will not happen in practice, but once (in subsequent patches) SMP init is moved to after the HCI init procedure it is possible that the pointer is NULL.

---
 net/bluetooth/hci_core.c | 3 +++
 1 file changed, 3 insertions(+)
net/bluetooth/hci_core.c +3 -0
index 860477090d78..4a1ec259099e 100644
@@ -3232,6 +3232,9 @@ struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa)
 			return irk;
 	}
 
+	if (!hdev->tfm_aes)
+		return NULL;
+
 	list_for_each_entry(irk, &hdev->identity_resolving_keys, list) {
 		if (smp_irk_matches(hdev->tfm_aes, irk->val, rpa)) {
 			bacpy(&irk->rpa, rpa);
Signature · 1 line
1.9.3
#3 lore raw

PATCH 3/5 Bluetooth: Refactor SMP (de)initialization into separate functions

In reply to [email protected]

From: Johan Hedberg <[email protected]>

As preparation for converting SMP to use the l2cap_chan infrastructure refactor the (de)initialization into separate functions.

---
 net/bluetooth/hci_core.c | 46 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 12 deletions(-)
net/bluetooth/hci_core.c +34 -12
index 4a1ec259099e..1f691c50abbc 100644
@@ -1764,6 +1764,34 @@ static void hci_init4_req(struct hci_request *req, unsigned long opt)
 	}
 }
 
+static int hci_register_smp(struct hci_dev *hdev)
+{
+	int err;
+
+	BT_DBG("%s", hdev->name);
+
+	hdev->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0,
+					       CRYPTO_ALG_ASYNC);
+	if (IS_ERR(hdev->tfm_aes)) {
+		BT_ERR("Unable to create crypto context");
+		err = PTR_ERR(hdev->tfm_aes);
+		hdev->tfm_aes = NULL;
+		return err;
+	}
+
+	return 0;
+}
+
+static void hci_unregister_smp(struct hci_dev *hdev)
+{
+	BT_DBG("%s", hdev->name);
+
+	if (hdev->tfm_aes) {
+		crypto_free_blkcipher(hdev->tfm_aes);
+		hdev->tfm_aes = NULL;
+	}
+}
+
 static int __hci_init(struct hci_dev *hdev)
 {
 	int err;
@@ -4099,18 +4127,13 @@ int hci_register_dev(struct hci_dev *hdev)
 
 	dev_set_name(&hdev->dev, "%s", hdev->name);
 
-	hdev->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0,
-					       CRYPTO_ALG_ASYNC);
-	if (IS_ERR(hdev->tfm_aes)) {
-		BT_ERR("Unable to create crypto context");
-		error = PTR_ERR(hdev->tfm_aes);
-		hdev->tfm_aes = NULL;
+	error = hci_register_smp(hdev);
+	if (error)
 		goto err_wqueue;
-	}
 
 	error = device_add(&hdev->dev);
 	if (error < 0)
-		goto err_tfm;
+		goto err_smp;
 
 	hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
 				    RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops,
@@ -4152,8 +4175,8 @@ int hci_register_dev(struct hci_dev *hdev)
 
 	return id;
 
-err_tfm:
-	crypto_free_blkcipher(hdev->tfm_aes);
+err_smp:
+	hci_unregister_smp(hdev);
 err_wqueue:
 	destroy_workqueue(hdev->workqueue);
 	destroy_workqueue(hdev->req_workqueue);
@@ -4205,8 +4228,7 @@ void hci_unregister_dev(struct hci_dev *hdev)
 		rfkill_destroy(hdev->rfkill);
 	}
 
-	if (hdev->tfm_aes)
-		crypto_free_blkcipher(hdev->tfm_aes);
+	hci_unregister_smp(hdev);
 
 	device_del(&hdev->dev);
 
Signature · 1 line
1.9.3
#4 lore raw

PATCH 4/5 Bluetooth: Move SMP initialization after HCI init

In reply to [email protected]

From: Johan Hedberg <[email protected]>

First of all, it's wasteful to initialize SMP if it's never going to be used (e.g. on non-LE controllers). Second of all, when we move to use l2cap_chan we need to know the real local address, meaning we must have completed at least part of the HCI init. This patch moves the SMP initialization to after the HCI init procedure and makes it depend on whether the controller actually supports LE.

---
 net/bluetooth/hci_core.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
net/bluetooth/hci_core.c +3 -7
index 1f691c50abbc..9eb2869b183b 100644
@@ -1926,6 +1926,8 @@ static int __hci_init(struct hci_dev *hdev)
 		debugfs_create_u16("discov_interleaved_timeout", 0644,
 				   hdev->debugfs,
 				   &hdev->discov_interleaved_timeout);
+
+		hci_register_smp(hdev);
 	}
 
 	return 0;
@@ -4127,13 +4129,9 @@ int hci_register_dev(struct hci_dev *hdev)
 
 	dev_set_name(&hdev->dev, "%s", hdev->name);
 
-	error = hci_register_smp(hdev);
-	if (error)
-		goto err_wqueue;
-
 	error = device_add(&hdev->dev);
 	if (error < 0)
-		goto err_smp;
+		goto err_wqueue;
 
 	hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
 				    RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops,
@@ -4175,8 +4173,6 @@ int hci_register_dev(struct hci_dev *hdev)
 
 	return id;
 
-err_smp:
-	hci_unregister_smp(hdev);
 err_wqueue:
 	destroy_workqueue(hdev->workqueue);
 	destroy_workqueue(hdev->req_workqueue);
Signature · 1 line
1.9.3
#5 lore raw

PATCH 5/5 Bluetooth: Move SMP (de)initialization to smp.c

In reply to [email protected]

From: Johan Hedberg <[email protected]>

As preparation for moving SMP to use l2cap_chan infrastructure we need to move the (de)initialization functions to smp.c (where they'll eventually need access to the local L2CAP channel callbacks).

---
 net/bluetooth/hci_core.c | 32 ++------------------------------
 net/bluetooth/smp.c      | 26 ++++++++++++++++++++++++++
 net/bluetooth/smp.h      |  3 +++
 3 files changed, 31 insertions(+), 30 deletions(-)
net/bluetooth/hci_core.c +2 -30
index 9eb2869b183b..88575a633601 100644
@@ -1764,34 +1764,6 @@ static void hci_init4_req(struct hci_request *req, unsigned long opt)
 	}
 }
 
-static int hci_register_smp(struct hci_dev *hdev)
-{
-	int err;
-
-	BT_DBG("%s", hdev->name);
-
-	hdev->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0,
-					       CRYPTO_ALG_ASYNC);
-	if (IS_ERR(hdev->tfm_aes)) {
-		BT_ERR("Unable to create crypto context");
-		err = PTR_ERR(hdev->tfm_aes);
-		hdev->tfm_aes = NULL;
-		return err;
-	}
-
-	return 0;
-}
-
-static void hci_unregister_smp(struct hci_dev *hdev)
-{
-	BT_DBG("%s", hdev->name);
-
-	if (hdev->tfm_aes) {
-		crypto_free_blkcipher(hdev->tfm_aes);
-		hdev->tfm_aes = NULL;
-	}
-}
-
 static int __hci_init(struct hci_dev *hdev)
 {
 	int err;
@@ -1927,7 +1899,7 @@ static int __hci_init(struct hci_dev *hdev)
 				   hdev->debugfs,
 				   &hdev->discov_interleaved_timeout);
 
-		hci_register_smp(hdev);
+		smp_register(hdev);
 	}
 
 	return 0;
@@ -4224,7 +4196,7 @@ void hci_unregister_dev(struct hci_dev *hdev)
 		rfkill_destroy(hdev->rfkill);
 	}
 
-	hci_unregister_smp(hdev);
+	smp_unregister(hdev);
 
 	device_del(&hdev->dev);
 
net/bluetooth/smp.c +26 -0
index 33016ec9b247..ab07649ecc77 100644
@@ -1455,3 +1455,29 @@ int smp_distribute_keys(struct l2cap_conn *conn)
 
 	return 0;
 }
+
+int smp_register(struct hci_dev *hdev)
+{
+	BT_DBG("%s", hdev->name);
+
+	hdev->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0,
+					       CRYPTO_ALG_ASYNC);
+	if (IS_ERR(hdev->tfm_aes)) {
+		int err = PTR_ERR(hdev->tfm_aes);
+		BT_ERR("Unable to create crypto context");
+		hdev->tfm_aes = NULL;
+		return err;
+	}
+
+	return 0;
+}
+
+void smp_unregister(struct hci_dev *hdev)
+{
+	BT_DBG("%s", hdev->name);
+
+	if (hdev->tfm_aes) {
+		crypto_free_blkcipher(hdev->tfm_aes);
+		hdev->tfm_aes = NULL;
+	}
+}
net/bluetooth/smp.h +3 -0
index 796f4f45f92f..6e29359f60a3 100644
@@ -136,4 +136,7 @@ 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);
 
+int smp_register(struct hci_dev *hdev);
+void smp_unregister(struct hci_dev *hdev);
+
 #endif /* __SMP_H */
Signature · 1 line
1.9.3
maintainer
#6 lore raw
In reply to [email protected]

Hi Johan,

Quoted 15 lines

For us to have a clean way to create the root (per-adapter) SMP L2CAP channel it's best to move the code responsible for this to specialized functions in smp.c. For now this only involves the AES crypto context creation and destruction.

Johan

----------------------------------------------------------------
Johan Hedberg (5):
Bluetooth: Fix hci_update_random_address() error return for no crypto
Bluetooth: Fix IRK lookup when tfm_aes is not available
Bluetooth: Refactor SMP (de)initialization into separate functions
Bluetooth: Move SMP initialization after HCI init
Bluetooth: Move SMP (de)initialization to smp.c

Quoted 4 lines
net/bluetooth/hci_core.c | 27 +++++++++++++--------------
net/bluetooth/smp.c      | 26 ++++++++++++++++++++++++++
net/bluetooth/smp.h      |  3 +++
3 files changed, 42 insertions(+), 14 deletions(-)

all 5 patches have been applied to bluetooth-next tree.

Regards

Marcel