Skip to contentSkip to conversation

Bluetooth: Move SMP fields to a separate structure

PATCH Superseded Vinicius Costa Gomes 2 participants 8 messages Aug 20, 2011 – Sep 2, 2011

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 Bluetooth: Move SMP fields to a separate structure

The objective is to make the core to have as little as possible information about SMP procedures and logic. Now, all the SMP specific information is hidden from the core.

---
 include/net/bluetooth/hci_core.h |    2 -
 include/net/bluetooth/l2cap.h    |   10 +--
 net/bluetooth/hci_core.c         |    8 ---
 net/bluetooth/smp.c              |  118 ++++++++++++++++++++-----------------
 4 files changed, 67 insertions(+), 71 deletions(-)
include/net/bluetooth/hci_core.h +0 -2
index 6e68e43..cc1ea91 100644
@@ -195,8 +195,6 @@ struct hci_dev {
 
 	__u16			init_last_cmd;
 
-	struct crypto_blkcipher	*tfm;
-
 	struct inquiry_cache	inq_cache;
 	struct hci_conn_hash	conn_hash;
 	struct list_head	blacklist;
include/net/bluetooth/l2cap.h +3 -7
index 6fa1140..00e42aa 100644
@@ -386,6 +386,8 @@ struct l2cap_ops {
 	void			(*state_change) (void *data, int state);
 };
 
+struct smp_chan;
+
 struct l2cap_conn {
 	struct hci_conn	*hcon;
 
@@ -409,14 +411,8 @@ struct l2cap_conn {
 
 	__u8		disc_reason;
 
-	__u8		preq[7]; /* SMP Pairing Request */
-	__u8		prsp[7]; /* SMP Pairing Response */
-	__u8		prnd[16]; /* SMP Pairing Random */
-	__u8		pcnf[16]; /* SMP Pairing Confirm */
-	__u8		tk[16]; /* SMP Temporary Key */
-	__u8		smp_key_size;
-
 	struct timer_list security_timer;
+	struct smp_chan *smp_chan;
 
 	struct list_head chan_l;
 	rwlock_t	chan_lock;
net/bluetooth/hci_core.c +0 -8
index 56943ad..534a271 100644
@@ -1523,11 +1523,6 @@ int hci_register_dev(struct hci_dev *hdev)
 	if (!hdev->workqueue)
 		goto nomem;
 
-	hdev->tfm = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
-	if (IS_ERR(hdev->tfm))
-		BT_INFO("Failed to load transform for ecb(aes): %ld",
-							PTR_ERR(hdev->tfm));
-
 	hci_register_sysfs(hdev);
 
 	hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
@@ -1576,9 +1571,6 @@ int hci_unregister_dev(struct hci_dev *hdev)
 					!test_bit(HCI_SETUP, &hdev->flags))
 		mgmt_index_removed(hdev->id);
 
-	if (!IS_ERR(hdev->tfm))
-		crypto_free_blkcipher(hdev->tfm);
-
 	hci_notify(hdev, HCI_DEV_UNREG);
 
 	if (hdev->rfkill) {
net/bluetooth/smp.c +64 -54

321 lines not on this page · raw message

Signature · 1 line
1.7.6
#1 lore raw

PATCH 2/4 Bluetooth: Add functions to initialize the SMP workqueue

SMP crypto function crypto_blkcypher_setkey() may sleep, so we need to move that part of the SMP procedure inside a workqueue.

---
 include/net/bluetooth/smp.h |    3 +++
 net/bluetooth/smp.c         |   24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 0 deletions(-)
include/net/bluetooth/smp.h +3 -0
index 46c4576..884ed41 100644
@@ -120,4 +120,7 @@ int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level);
 int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb);
 int smp_distribute_keys(struct l2cap_conn *conn, __u8 force);
 
+int smp_crypto_init(void);
+void smp_crypto_exit(void);
+
 #endif /* __SMP_H */
net/bluetooth/smp.c +24 -0
index f911930..9edd317 100644
@@ -20,6 +20,7 @@
    SOFTWARE IS DISCLAIMED.
 */
 
+#include <linux/errno.h>
 #include <net/bluetooth/bluetooth.h>
 #include <net/bluetooth/hci_core.h>
 #include <net/bluetooth/l2cap.h>
@@ -30,6 +31,8 @@
 
 #define SMP_TIMEOUT 30000 /* 30 seconds */
 
+static struct workqueue_struct *crypto_wq;
+
 struct smp_chan {
 	u8		preq[7]; /* SMP Pairing Request */
 	u8		prsp[7]; /* SMP Pairing Response */
@@ -644,6 +647,9 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
 
 	BT_DBG("conn %p force %d", conn, force);
 
+	if (!crypto_wq)
+		return -ENOTSUPP;
+
 	if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend))
 		return 0;
 
@@ -725,3 +731,21 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
 
 	return 0;
 }
+
+int smp_crypto_init(void)
+{
+	crypto_wq = create_singlethread_workqueue("smp");
+	if (!crypto_wq)
+		return -ENOMEM;
+
+	return 0;
+}
+
+void smp_crypto_exit(void)
+{
+	if (!crypto_wq)
+		return;
+
+	flush_workqueue(crypto_wq);
+	destroy_workqueue(crypto_wq);
+}
Signature · 1 line
1.7.6
#2 lore raw

Re: [PATCH 2/4] Bluetooth: Add functions to initialize the SMP workqueue

Hi Vinicius,

Quoted 34 lines · Vinicius Costa Gomes

SMP crypto function crypto_blkcypher_setkey() may sleep, so we need to move that part of the SMP procedure inside a workqueue.

---
 include/net/bluetooth/smp.h |    3 +++
 net/bluetooth/smp.c         |   24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 0 deletions(-)
include/net/bluetooth/smp.h +3 -0
index 46c4576..884ed41 100644
@@ -120,4 +120,7 @@ int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level);
 int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb);
 int smp_distribute_keys(struct l2cap_conn *conn, __u8 force);
 
+int smp_crypto_init(void);
+void smp_crypto_exit(void);
+
 #endif /* __SMP_H */
net/bluetooth/smp.c +1 -0
index f911930..9edd317 100644
@@ -20,6 +20,7 @@
    SOFTWARE IS DISCLAIMED.
 */
 
+#include <linux/errno.h>
 #include <net/bluetooth/bluetooth.h>
 #include <net/bluetooth/hci_core.h>
 #include <net/bluetooth/l2cap.h>

net/bluetooth/smp.c line 35

Quoted 5 lines
net/bluetooth/smp.c +2 -0
@@ -30,6 +31,8 @@
 
 #define SMP_TIMEOUT 30000 /* 30 seconds */
 
+static struct workqueue_struct *crypto_wq;
+

Let's use hdev->workqueue instead of creating a new one.

Gustavo

#3 lore raw

PATCH 3/4 Bluetooth: Initialize the SMP workqueue

In reply to Vinicius Costa Gomes

The SMP workqueue is initialized along with the L2CAP module if the enable_smp parameter is enabled, and destroyed along the L2CAP module.

---
 net/bluetooth/l2cap_core.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
net/bluetooth/l2cap_core.c +7 -0
index 00e93d1..3082a0e 100644
@@ -4293,6 +4293,10 @@ int __init l2cap_init(void)
 	if (err < 0)
 		return err;
 
+	err = smp_crypto_init();
+	if (err < 0)
+		BT_DBG("SMP Initialization falied");
+
 	err = hci_register_proto(&l2cap_hci_proto);
 	if (err < 0) {
 		BT_ERR("L2CAP protocol registration failed");
@@ -4310,6 +4314,7 @@ int __init l2cap_init(void)
 	return 0;
 
 error:
+	smp_crypto_exit();
 	l2cap_cleanup_sockets();
 	return err;
 }
@@ -4318,6 +4323,8 @@ void l2cap_exit(void)
 {
 	debugfs_remove(l2cap_debugfs);
 
+	smp_crypto_exit();
+
 	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
 		BT_ERR("L2CAP protocol unregistration failed");
 
Signature · 1 line
1.7.6
#4 lore raw

Re: [PATCH 3/4] Bluetooth: Initialize the SMP workqueue

Quoted 3 lines · Vinicius Costa Gomes

The SMP workqueue is initialized along with the L2CAP module if the enable_smp parameter is enabled, and destroyed along the L2CAP module.

The commit message is wrong. Please disconsider it, will fix it before sending a new version.

Quoted 40 lines
---
 net/bluetooth/l2cap_core.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
net/bluetooth/l2cap_core.c +7 -0
index 00e93d1..3082a0e 100644
@@ -4293,6 +4293,10 @@ int __init l2cap_init(void)
 	if (err < 0)
 		return err;
 
+	err = smp_crypto_init();
+	if (err < 0)
+		BT_DBG("SMP Initialization falied");
+
 	err = hci_register_proto(&l2cap_hci_proto);
 	if (err < 0) {
 		BT_ERR("L2CAP protocol registration failed");
@@ -4310,6 +4314,7 @@ int __init l2cap_init(void)
 	return 0;
 
 error:
+	smp_crypto_exit();
 	l2cap_cleanup_sockets();
 	return err;
 }
@@ -4318,6 +4323,8 @@ void l2cap_exit(void)
 {
 	debugfs_remove(l2cap_debugfs);
 
+	smp_crypto_exit();
+
 	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
 		BT_ERR("L2CAP protocol unregistration failed");
 
Signature · 1 line
1.7.6

Cheers,

Signature · 1 line
Vinicius
#5 lore raw

PATCH 1/1 Bluetooth: Initialize the SMP workqueue

In reply to Vinicius Costa Gomes

From: Vinicius Costa Gomes <[email protected]>

The SMP workqueue is initialized unconditionally because it needs to be done during module initialization. And during this time we can't be sure that LE support is available.

---
 net/bluetooth/l2cap_core.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
net/bluetooth/l2cap_core.c +7 -0
index 09ca51e..b721f16 100644
@@ -4290,6 +4290,10 @@ int __init l2cap_init(void)
 	if (err < 0)
 		return err;
 
+	err = smp_crypto_init();
+	if (err < 0)
+		BT_DBG("SMP Initialization falied");
+
 	err = hci_register_proto(&l2cap_hci_proto);
 	if (err < 0) {
 		BT_ERR("L2CAP protocol registration failed");
@@ -4307,6 +4311,7 @@ int __init l2cap_init(void)
 	return 0;
 
 error:
+	smp_crypto_exit();
 	l2cap_cleanup_sockets();
 	return err;
 }
@@ -4315,6 +4320,8 @@ void l2cap_exit(void)
 {
 	debugfs_remove(l2cap_debugfs);
 
+	smp_crypto_exit();
+
 	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
 		BT_ERR("L2CAP protocol unregistration failed");
 
Signature · 1 line
1.7.6
#6 lore raw

PATCH 4/4 Bluetooth: Move SMP crypto functions to a workqueue

In reply to Vinicius Costa Gomes

The function crypto_blkcipher_setkey() called by smp_e() can sleep, so all the crypto work has to be moved to a workqueue.

---
 include/net/bluetooth/smp.h |    2 +
 net/bluetooth/l2cap_core.c  |    2 +-
 net/bluetooth/smp.c         |  284 +++++++++++++++++++++++++++----------------
 3 files changed, 183 insertions(+), 105 deletions(-)
include/net/bluetooth/smp.h +2 -0
index 884ed41..0991154 100644
@@ -120,6 +120,8 @@ int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level);
 int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb);
 int smp_distribute_keys(struct l2cap_conn *conn, __u8 force);
 
+void smp_chan_destroy(struct l2cap_conn *conn);
+
 int smp_crypto_init(void);
 void smp_crypto_exit(void);
 
net/bluetooth/l2cap_core.c +1 -1
index 3082a0e..5b63381 100644
@@ -996,7 +996,7 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)
 
 	if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend)) {
 		del_timer(&conn->security_timer);
-		hci_conn_put(hcon);
+		smp_chan_destroy(conn);
 	}
 
 	hcon->l2cap_data = NULL;
net/bluetooth/smp.c +180 -104

393 lines not on this page · raw message

Signature · 1 line
1.7.6
#7 lore raw
In reply to Vinicius Costa Gomes

Hi Vinicius,

Quoted 30 lines · Vinicius Costa Gomes

The objective is to make the core to have as little as possible information about SMP procedures and logic. Now, all the SMP specific information is hidden from the core.

---
 include/net/bluetooth/hci_core.h |    2 -
 include/net/bluetooth/l2cap.h    |   10 +--
 net/bluetooth/hci_core.c         |    8 ---
 net/bluetooth/smp.c              |  118 ++++++++++++++++++++-----------------
 4 files changed, 67 insertions(+), 71 deletions(-)
include/net/bluetooth/hci_core.h +0 -2
index 6e68e43..cc1ea91 100644
@@ -195,8 +195,6 @@ struct hci_dev {
 
 	__u16			init_last_cmd;
 
-	struct crypto_blkcipher	*tfm;
-
 	struct inquiry_cache	inq_cache;
 	struct hci_conn_hash	conn_hash;
 	struct list_head	blacklist;
include/net/bluetooth/l2cap.h +0 -0
index 6fa1140..00e42aa 100644

include/net/bluetooth/l2cap.h struct l2cap_ops { · line 389

Quoted 4 lines
include/net/bluetooth/l2cap.h +1 -0
@@ -386,6 +386,8 @@ struct l2cap_ops {
 	void			(*state_change) (void *data, int state);
 };
 
+struct smp_chan;

Just define this on smp.h, it's easy.

Gustavo