aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/smp.c
diff options
context:
space:
mode:
authorJohan Hedberg <johan.hedberg@intel.com>2014-08-08 02:37:18 -0400
committerMarcel Holtmann <marcel@holtmann.org>2014-08-14 02:49:19 -0400
commit5d88cc73dded31a93fcc4821f33a8c3d755bf454 (patch)
treec6599506929f335c954e40b2fbcf13dc7af98a88 /net/bluetooth/smp.c
parentdefce9e83666658d4420d65e45ab1ad190992f72 (diff)
Bluetooth: Convert SMP to use l2cap_chan infrastructure
Now that we have all the necessary pieces in place we can fully convert SMP to use the L2CAP channel infrastructure. This patch adds the necessary callbacks and removes the now unneeded conn->smp_chan pointer. One notable behavioral change in this patch comes from the following code snippet: - case L2CAP_CID_SMP: - if (smp_sig_channel(conn, skb)) - l2cap_conn_del(conn->hcon, EACCES); This piece of code was essentially forcing a disconnection if garbage SMP data was received. The l2cap_conn_del() function is private to l2cap_conn.c so we don't have access to it anymore when using the L2CAP channel callbacks. Therefore, the behavior of the new code is simply to return errors in the recv() callback (which is simply the old smp_sig_channel()), but no disconnection will occur. Signed-off-by: Johan Hedberg <johan.hedberg@intel.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Diffstat (limited to 'net/bluetooth/smp.c')
-rw-r--r--net/bluetooth/smp.c122
1 files changed, 66 insertions, 56 deletions
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index 6925fc4caaee..744f678ac3e8 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -248,44 +248,29 @@ static int smp_s1(struct smp_chan *smp, u8 k[16], u8 r1[16], u8 r2[16],
248 return err; 248 return err;
249} 249}
250 250
251static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code, 251static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
252 u16 dlen, void *data)
253{ 252{
254 struct sk_buff *skb; 253 struct l2cap_chan *chan = conn->smp;
255 struct l2cap_hdr *lh; 254 struct kvec iv[2];
256 int len; 255 struct msghdr msg;
257
258 len = L2CAP_HDR_SIZE + sizeof(code) + dlen;
259
260 if (len > conn->mtu)
261 return NULL;
262 256
263 skb = bt_skb_alloc(len, GFP_ATOMIC); 257 if (!chan)
264 if (!skb) 258 return;
265 return NULL;
266 259
267 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 260 BT_DBG("code 0x%2.2x", code);
268 lh->len = cpu_to_le16(sizeof(code) + dlen);
269 lh->cid = cpu_to_le16(L2CAP_CID_SMP);
270 261
271 memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code)); 262 iv[0].iov_base = &code;
263 iv[0].iov_len = 1;
272 264
273 memcpy(skb_put(skb, dlen), data, dlen); 265 iv[1].iov_base = data;
266 iv[1].iov_len = len;
274 267
275 return skb; 268 memset(&msg, 0, sizeof(msg));
276}
277 269
278static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) 270 msg.msg_iov = (struct iovec *) &iv;
279{ 271 msg.msg_iovlen = 2;
280 struct sk_buff *skb = smp_build_cmd(conn, code, len, data);
281 272
282 BT_DBG("code 0x%2.2x", code); 273 l2cap_chan_send(chan, &msg, 1 + len);
283
284 if (!skb)
285 return;
286
287 skb->priority = HCI_PRIO_MAX;
288 hci_send_acl(conn->hchan, skb, 0);
289 274
290 cancel_delayed_work_sync(&conn->security_timer); 275 cancel_delayed_work_sync(&conn->security_timer);
291 schedule_delayed_work(&conn->security_timer, SMP_TIMEOUT); 276 schedule_delayed_work(&conn->security_timer, SMP_TIMEOUT);
@@ -315,7 +300,8 @@ static void build_pairing_cmd(struct l2cap_conn *conn,
315 struct smp_cmd_pairing *req, 300 struct smp_cmd_pairing *req,
316 struct smp_cmd_pairing *rsp, __u8 authreq) 301 struct smp_cmd_pairing *rsp, __u8 authreq)
317{ 302{
318 struct smp_chan *smp = conn->smp_chan; 303 struct l2cap_chan *chan = conn->smp;
304 struct smp_chan *smp = chan->data;
319 struct hci_conn *hcon = conn->hcon; 305 struct hci_conn *hcon = conn->hcon;
320 struct hci_dev *hdev = hcon->hdev; 306 struct hci_dev *hdev = hcon->hdev;
321 u8 local_dist = 0, remote_dist = 0; 307 u8 local_dist = 0, remote_dist = 0;
@@ -358,7 +344,8 @@ static void build_pairing_cmd(struct l2cap_conn *conn,
358 344
359static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) 345static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
360{ 346{
361 struct smp_chan *smp = conn->smp_chan; 347 struct l2cap_chan *chan = conn->smp;
348 struct smp_chan *smp = chan->data;
362 349
363 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || 350 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
364 (max_key_size < SMP_MIN_ENC_KEY_SIZE)) 351 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
@@ -418,7 +405,8 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
418 u8 local_io, u8 remote_io) 405 u8 local_io, u8 remote_io)
419{ 406{
420 struct hci_conn *hcon = conn->hcon; 407 struct hci_conn *hcon = conn->hcon;
421 struct smp_chan *smp = conn->smp_chan; 408 struct l2cap_chan *chan = conn->smp;
409 struct smp_chan *smp = chan->data;
422 u8 method; 410 u8 method;
423 u32 passkey = 0; 411 u32 passkey = 0;
424 int ret = 0; 412 int ret = 0;
@@ -589,6 +577,7 @@ static u8 smp_random(struct smp_chan *smp)
589 577
590static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) 578static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
591{ 579{
580 struct l2cap_chan *chan = conn->smp;
592 struct smp_chan *smp; 581 struct smp_chan *smp;
593 582
594 smp = kzalloc(sizeof(*smp), GFP_ATOMIC); 583 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
@@ -606,7 +595,7 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
606 } 595 }
607 596
608 smp->conn = conn; 597 smp->conn = conn;
609 conn->smp_chan = smp; 598 chan->data = smp;
610 599
611 hci_conn_hold(conn->hcon); 600 hci_conn_hold(conn->hcon);
612 601
@@ -615,7 +604,8 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
615 604
616void smp_chan_destroy(struct l2cap_conn *conn) 605void smp_chan_destroy(struct l2cap_conn *conn)
617{ 606{
618 struct smp_chan *smp = conn->smp_chan; 607 struct l2cap_chan *chan = conn->smp;
608 struct smp_chan *smp = chan->data;
619 bool complete; 609 bool complete;
620 610
621 BUG_ON(!smp); 611 BUG_ON(!smp);
@@ -646,14 +636,15 @@ void smp_chan_destroy(struct l2cap_conn *conn)
646 } 636 }
647 } 637 }
648 638
639 chan->data = NULL;
649 kfree(smp); 640 kfree(smp);
650 conn->smp_chan = NULL;
651 hci_conn_drop(conn->hcon); 641 hci_conn_drop(conn->hcon);
652} 642}
653 643
654int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) 644int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
655{ 645{
656 struct l2cap_conn *conn = hcon->l2cap_data; 646 struct l2cap_conn *conn = hcon->l2cap_data;
647 struct l2cap_chan *chan;
657 struct smp_chan *smp; 648 struct smp_chan *smp;
658 u32 value; 649 u32 value;
659 650
@@ -662,7 +653,11 @@ int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
662 if (!conn || !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) 653 if (!conn || !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
663 return -ENOTCONN; 654 return -ENOTCONN;
664 655
665 smp = conn->smp_chan; 656 chan = conn->smp;
657 if (!chan)
658 return -ENOTCONN;
659
660 smp = chan->data;
666 661
667 switch (mgmt_op) { 662 switch (mgmt_op) {
668 case MGMT_OP_USER_PASSKEY_REPLY: 663 case MGMT_OP_USER_PASSKEY_REPLY:
@@ -709,10 +704,12 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
709 if (conn->hcon->role != HCI_ROLE_SLAVE) 704 if (conn->hcon->role != HCI_ROLE_SLAVE)
710 return SMP_CMD_NOTSUPP; 705 return SMP_CMD_NOTSUPP;
711 706
712 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) 707 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) {
713 smp = smp_chan_create(conn); 708 smp = smp_chan_create(conn);
714 else 709 } else {
715 smp = conn->smp_chan; 710 struct l2cap_chan *chan = conn->smp;
711 smp = chan->data;
712 }
716 713
717 if (!smp) 714 if (!smp)
718 return SMP_UNSPECIFIED; 715 return SMP_UNSPECIFIED;
@@ -766,7 +763,8 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
766static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) 763static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
767{ 764{
768 struct smp_cmd_pairing *req, *rsp = (void *) skb->data; 765 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
769 struct smp_chan *smp = conn->smp_chan; 766 struct l2cap_chan *chan = conn->smp;
767 struct smp_chan *smp = chan->data;
770 u8 key_size, auth = SMP_AUTH_NONE; 768 u8 key_size, auth = SMP_AUTH_NONE;
771 int ret; 769 int ret;
772 770
@@ -827,7 +825,8 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
827 825
828static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) 826static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
829{ 827{
830 struct smp_chan *smp = conn->smp_chan; 828 struct l2cap_chan *chan = conn->smp;
829 struct smp_chan *smp = chan->data;
831 830
832 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); 831 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
833 832
@@ -850,7 +849,8 @@ static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
850 849
851static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) 850static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
852{ 851{
853 struct smp_chan *smp = conn->smp_chan; 852 struct l2cap_chan *chan = conn->smp;
853 struct smp_chan *smp = chan->data;
854 854
855 BT_DBG("conn %p", conn); 855 BT_DBG("conn %p", conn);
856 856
@@ -1023,7 +1023,8 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
1023static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) 1023static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1024{ 1024{
1025 struct smp_cmd_encrypt_info *rp = (void *) skb->data; 1025 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
1026 struct smp_chan *smp = conn->smp_chan; 1026 struct l2cap_chan *chan = conn->smp;
1027 struct smp_chan *smp = chan->data;
1027 1028
1028 BT_DBG("conn %p", conn); 1029 BT_DBG("conn %p", conn);
1029 1030
@@ -1044,7 +1045,8 @@ static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1044static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) 1045static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1045{ 1046{
1046 struct smp_cmd_master_ident *rp = (void *) skb->data; 1047 struct smp_cmd_master_ident *rp = (void *) skb->data;
1047 struct smp_chan *smp = conn->smp_chan; 1048 struct l2cap_chan *chan = conn->smp;
1049 struct smp_chan *smp = chan->data;
1048 struct hci_dev *hdev = conn->hcon->hdev; 1050 struct hci_dev *hdev = conn->hcon->hdev;
1049 struct hci_conn *hcon = conn->hcon; 1051 struct hci_conn *hcon = conn->hcon;
1050 struct smp_ltk *ltk; 1052 struct smp_ltk *ltk;
@@ -1080,7 +1082,8 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1080static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) 1082static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1081{ 1083{
1082 struct smp_cmd_ident_info *info = (void *) skb->data; 1084 struct smp_cmd_ident_info *info = (void *) skb->data;
1083 struct smp_chan *smp = conn->smp_chan; 1085 struct l2cap_chan *chan = conn->smp;
1086 struct smp_chan *smp = chan->data;
1084 1087
1085 BT_DBG(""); 1088 BT_DBG("");
1086 1089
@@ -1102,7 +1105,8 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1102 struct sk_buff *skb) 1105 struct sk_buff *skb)
1103{ 1106{
1104 struct smp_cmd_ident_addr_info *info = (void *) skb->data; 1107 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
1105 struct smp_chan *smp = conn->smp_chan; 1108 struct l2cap_chan *chan = conn->smp;
1109 struct smp_chan *smp = chan->data;
1106 struct hci_conn *hcon = conn->hcon; 1110 struct hci_conn *hcon = conn->hcon;
1107 bdaddr_t rpa; 1111 bdaddr_t rpa;
1108 1112
@@ -1156,7 +1160,8 @@ distribute:
1156static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) 1160static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1157{ 1161{
1158 struct smp_cmd_sign_info *rp = (void *) skb->data; 1162 struct smp_cmd_sign_info *rp = (void *) skb->data;
1159 struct smp_chan *smp = conn->smp_chan; 1163 struct l2cap_chan *chan = conn->smp;
1164 struct smp_chan *smp = chan->data;
1160 struct hci_dev *hdev = conn->hcon->hdev; 1165 struct hci_dev *hdev = conn->hcon->hdev;
1161 struct smp_csrk *csrk; 1166 struct smp_csrk *csrk;
1162 1167
@@ -1187,8 +1192,9 @@ static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1187 return 0; 1192 return 0;
1188} 1193}
1189 1194
1190int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) 1195static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1191{ 1196{
1197 struct l2cap_conn *conn = chan->conn;
1192 struct hci_conn *hcon = conn->hcon; 1198 struct hci_conn *hcon = conn->hcon;
1193 __u8 code, reason; 1199 __u8 code, reason;
1194 int err = 0; 1200 int err = 0;
@@ -1290,7 +1296,8 @@ done:
1290 1296
1291static void smp_notify_keys(struct l2cap_conn *conn) 1297static void smp_notify_keys(struct l2cap_conn *conn)
1292{ 1298{
1293 struct smp_chan *smp = conn->smp_chan; 1299 struct l2cap_chan *chan = conn->smp;
1300 struct smp_chan *smp = chan->data;
1294 struct hci_conn *hcon = conn->hcon; 1301 struct hci_conn *hcon = conn->hcon;
1295 struct hci_dev *hdev = hcon->hdev; 1302 struct hci_dev *hdev = hcon->hdev;
1296 struct smp_cmd_pairing *req = (void *) &smp->preq[1]; 1303 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
@@ -1357,7 +1364,8 @@ static void smp_notify_keys(struct l2cap_conn *conn)
1357int smp_distribute_keys(struct l2cap_conn *conn) 1364int smp_distribute_keys(struct l2cap_conn *conn)
1358{ 1365{
1359 struct smp_cmd_pairing *req, *rsp; 1366 struct smp_cmd_pairing *req, *rsp;
1360 struct smp_chan *smp = conn->smp_chan; 1367 struct l2cap_chan *chan = conn->smp;
1368 struct smp_chan *smp = chan->data;
1361 struct hci_conn *hcon = conn->hcon; 1369 struct hci_conn *hcon = conn->hcon;
1362 struct hci_dev *hdev = hcon->hdev; 1370 struct hci_dev *hdev = hcon->hdev;
1363 __u8 *keydist; 1371 __u8 *keydist;
@@ -1475,6 +1483,11 @@ static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1475 1483
1476 BT_DBG("chan %p", chan); 1484 BT_DBG("chan %p", chan);
1477 1485
1486 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) {
1487 cancel_delayed_work_sync(&conn->security_timer);
1488 smp_chan_destroy(conn);
1489 }
1490
1478 conn->smp = NULL; 1491 conn->smp = NULL;
1479 l2cap_chan_put(chan); 1492 l2cap_chan_put(chan);
1480} 1493}
@@ -1508,11 +1521,11 @@ static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1508static const struct l2cap_ops smp_chan_ops = { 1521static const struct l2cap_ops smp_chan_ops = {
1509 .name = "Security Manager", 1522 .name = "Security Manager",
1510 .ready = smp_ready_cb, 1523 .ready = smp_ready_cb,
1524 .recv = smp_recv_cb,
1511 .alloc_skb = smp_alloc_skb_cb, 1525 .alloc_skb = smp_alloc_skb_cb,
1512 .teardown = smp_teardown_cb, 1526 .teardown = smp_teardown_cb,
1513 1527
1514 .new_connection = l2cap_chan_no_new_connection, 1528 .new_connection = l2cap_chan_no_new_connection,
1515 .recv = l2cap_chan_no_recv,
1516 .state_change = l2cap_chan_no_state_change, 1529 .state_change = l2cap_chan_no_state_change,
1517 .close = l2cap_chan_no_close, 1530 .close = l2cap_chan_no_close,
1518 .defer = l2cap_chan_no_defer, 1531 .defer = l2cap_chan_no_defer,
@@ -1587,10 +1600,7 @@ int smp_register(struct hci_dev *hdev)
1587 1600
1588 chan->data = tfm_aes; 1601 chan->data = tfm_aes;
1589 1602
1590 /* FIXME: Using reserved 0x1f value for now - to be changed to 1603 l2cap_add_scid(chan, L2CAP_CID_SMP);
1591 * L2CAP_CID_SMP once all functionality is in place.
1592 */
1593 l2cap_add_scid(chan, 0x1f);
1594 1604
1595 l2cap_chan_set_defaults(chan); 1605 l2cap_chan_set_defaults(chan);
1596 1606