Skip to content

Commit ef8efe4

Browse files
jhedbergholtmann
authored andcommitted
Bluetooth: Add skeleton for BR/EDR SMP channel
This patch adds the very basic code for creating and destroying SMP L2CAP channels for BR/EDR connections. Signed-off-by: Johan Hedberg <johan.hedberg@intel.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent 858cdc7 commit ef8efe4

3 files changed

Lines changed: 74 additions & 18 deletions

File tree

‎include/net/bluetooth/hci_core.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ struct hci_dev {
306306
__u32 req_result;
307307

308308
void *smp_data;
309+
void *smp_bredr_data;
309310

310311
struct discovery_state discovery;
311312
struct hci_conn_hash conn_hash;

‎include/net/bluetooth/l2cap.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ struct l2cap_conninfo {
141141
#define L2CAP_FC_ATT 0x10
142142
#define L2CAP_FC_SIG_LE 0x20
143143
#define L2CAP_FC_SMP_LE 0x40
144+
#define L2CAP_FC_SMP_BREDR 0x80
144145

145146
/* L2CAP Control Field bit masks */
146147
#define L2CAP_CTRL_SAR 0xC000
@@ -255,6 +256,7 @@ struct l2cap_conn_rsp {
255256
#define L2CAP_CID_ATT 0x0004
256257
#define L2CAP_CID_LE_SIGNALING 0x0005
257258
#define L2CAP_CID_SMP 0x0006
259+
#define L2CAP_CID_SMP_BREDR 0x0007
258260
#define L2CAP_CID_DYN_START 0x0040
259261
#define L2CAP_CID_DYN_END 0xffff
260262
#define L2CAP_CID_LE_DYN_END 0x007f

‎net/bluetooth/smp.c‎

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,6 +2504,9 @@ static void smp_resume_cb(struct l2cap_chan *chan)
25042504

25052505
BT_DBG("chan %p", chan);
25062506

2507+
if (hcon->type == ACL_LINK)
2508+
return;
2509+
25072510
if (!smp)
25082511
return;
25092512

@@ -2527,10 +2530,14 @@ static void smp_ready_cb(struct l2cap_chan *chan)
25272530

25282531
static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
25292532
{
2533+
struct hci_conn *hcon = chan->conn->hcon;
25302534
int err;
25312535

25322536
BT_DBG("chan %p", chan);
25332537

2538+
if (hcon->type == ACL_LINK)
2539+
return -EOPNOTSUPP;
2540+
25342541
err = smp_sig_channel(chan, skb);
25352542
if (err) {
25362543
struct smp_chan *smp = chan->data;
@@ -2627,34 +2634,40 @@ static const struct l2cap_ops smp_root_chan_ops = {
26272634
.memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
26282635
};
26292636

2630-
int smp_register(struct hci_dev *hdev)
2637+
static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
26312638
{
26322639
struct l2cap_chan *chan;
26332640
struct crypto_blkcipher *tfm_aes;
26342641

2635-
BT_DBG("%s", hdev->name);
2642+
if (cid == L2CAP_CID_SMP_BREDR) {
2643+
tfm_aes = NULL;
2644+
goto create_chan;
2645+
}
26362646

26372647
tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
26382648
if (IS_ERR(tfm_aes)) {
2639-
int err = PTR_ERR(tfm_aes);
26402649
BT_ERR("Unable to create crypto context");
2641-
return err;
2650+
return ERR_PTR(PTR_ERR(tfm_aes));
26422651
}
26432652

2653+
create_chan:
26442654
chan = l2cap_chan_create();
26452655
if (!chan) {
26462656
crypto_free_blkcipher(tfm_aes);
2647-
return -ENOMEM;
2657+
return ERR_PTR(-ENOMEM);
26482658
}
26492659

26502660
chan->data = tfm_aes;
26512661

2652-
l2cap_add_scid(chan, L2CAP_CID_SMP);
2662+
l2cap_add_scid(chan, cid);
26532663

26542664
l2cap_chan_set_defaults(chan);
26552665

26562666
bacpy(&chan->src, &hdev->bdaddr);
2657-
chan->src_type = BDADDR_LE_PUBLIC;
2667+
if (cid == L2CAP_CID_SMP)
2668+
chan->src_type = BDADDR_LE_PUBLIC;
2669+
else
2670+
chan->src_type = BDADDR_BREDR;
26582671
chan->state = BT_LISTEN;
26592672
chan->mode = L2CAP_MODE_BASIC;
26602673
chan->imtu = L2CAP_DEFAULT_MTU;
@@ -2663,27 +2676,67 @@ int smp_register(struct hci_dev *hdev)
26632676
/* Set correct nesting level for a parent/listening channel */
26642677
atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
26652678

2666-
hdev->smp_data = chan;
2667-
2668-
return 0;
2679+
return chan;
26692680
}
26702681

2671-
void smp_unregister(struct hci_dev *hdev)
2682+
static void smp_del_chan(struct l2cap_chan *chan)
26722683
{
2673-
struct l2cap_chan *chan = hdev->smp_data;
2674-
struct crypto_blkcipher *tfm_aes;
2675-
2676-
if (!chan)
2677-
return;
2684+
struct crypto_blkcipher *tfm_aes;
26782685

2679-
BT_DBG("%s chan %p", hdev->name, chan);
2686+
BT_DBG("chan %p", chan);
26802687

26812688
tfm_aes = chan->data;
26822689
if (tfm_aes) {
26832690
chan->data = NULL;
26842691
crypto_free_blkcipher(tfm_aes);
26852692
}
26862693

2687-
hdev->smp_data = NULL;
26882694
l2cap_chan_put(chan);
26892695
}
2696+
2697+
int smp_register(struct hci_dev *hdev)
2698+
{
2699+
struct l2cap_chan *chan;
2700+
2701+
BT_DBG("%s", hdev->name);
2702+
2703+
chan = smp_add_cid(hdev, L2CAP_CID_SMP);
2704+
if (IS_ERR(chan))
2705+
return PTR_ERR(chan);
2706+
2707+
hdev->smp_data = chan;
2708+
2709+
if (!lmp_sc_capable(hdev) &&
2710+
!test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2711+
return 0;
2712+
2713+
chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
2714+
if (IS_ERR(chan)) {
2715+
int err = PTR_ERR(chan);
2716+
chan = hdev->smp_data;
2717+
hdev->smp_data = NULL;
2718+
smp_del_chan(chan);
2719+
return err;
2720+
}
2721+
2722+
hdev->smp_bredr_data = chan;
2723+
2724+
return 0;
2725+
}
2726+
2727+
void smp_unregister(struct hci_dev *hdev)
2728+
{
2729+
struct l2cap_chan *chan;
2730+
2731+
if (hdev->smp_bredr_data) {
2732+
chan = hdev->smp_bredr_data;
2733+
hdev->smp_bredr_data = NULL;
2734+
smp_del_chan(chan);
2735+
}
2736+
2737+
if (hdev->smp_data) {
2738+
chan = hdev->smp_data;
2739+
hdev->smp_data = NULL;
2740+
smp_del_chan(chan);
2741+
}
2742+
}

0 commit comments

Comments
 (0)