aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/smp.c
diff options
context:
space:
mode:
authorJohan Hedberg <johan.hedberg@intel.com>2014-08-11 15:06:36 -0400
committerMarcel Holtmann <marcel@holtmann.org>2014-08-14 02:49:21 -0400
commit44f1a7ab51ebe1ca189445837e0599a5edc6efb1 (patch)
treed9955a785be9fa69fa7f16c98195ff95b231c838 /net/bluetooth/smp.c
parent89d2975fa06e66ea0d3665d91f799fb1ce4b8bad (diff)
Bluetooth: Use L2CAP resume callback to call smp_distribute_keys
There's no need to export the smp_distribute_keys() function since the resume callback is called in the same scenario. This patch makes the smp_notify_keys function private (at the same time moving it higher up in smp.c to avoid forward declarations) and adds a resume callback for SMP to call it from there instead. 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.c380
1 files changed, 196 insertions, 184 deletions
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index 744f678ac3e8..28014ad3d2d3 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -575,6 +575,189 @@ static u8 smp_random(struct smp_chan *smp)
575 return 0; 575 return 0;
576} 576}
577 577
578static void smp_notify_keys(struct l2cap_conn *conn)
579{
580 struct l2cap_chan *chan = conn->smp;
581 struct smp_chan *smp = chan->data;
582 struct hci_conn *hcon = conn->hcon;
583 struct hci_dev *hdev = hcon->hdev;
584 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
585 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
586 bool persistent;
587
588 if (smp->remote_irk) {
589 mgmt_new_irk(hdev, smp->remote_irk);
590 /* Now that user space can be considered to know the
591 * identity address track the connection based on it
592 * from now on.
593 */
594 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
595 hcon->dst_type = smp->remote_irk->addr_type;
596 l2cap_conn_update_id_addr(hcon);
597
598 /* When receiving an indentity resolving key for
599 * a remote device that does not use a resolvable
600 * private address, just remove the key so that
601 * it is possible to use the controller white
602 * list for scanning.
603 *
604 * Userspace will have been told to not store
605 * this key at this point. So it is safe to
606 * just remove it.
607 */
608 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
609 list_del(&smp->remote_irk->list);
610 kfree(smp->remote_irk);
611 smp->remote_irk = NULL;
612 }
613 }
614
615 /* The LTKs and CSRKs should be persistent only if both sides
616 * had the bonding bit set in their authentication requests.
617 */
618 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
619
620 if (smp->csrk) {
621 smp->csrk->bdaddr_type = hcon->dst_type;
622 bacpy(&smp->csrk->bdaddr, &hcon->dst);
623 mgmt_new_csrk(hdev, smp->csrk, persistent);
624 }
625
626 if (smp->slave_csrk) {
627 smp->slave_csrk->bdaddr_type = hcon->dst_type;
628 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
629 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
630 }
631
632 if (smp->ltk) {
633 smp->ltk->bdaddr_type = hcon->dst_type;
634 bacpy(&smp->ltk->bdaddr, &hcon->dst);
635 mgmt_new_ltk(hdev, smp->ltk, persistent);
636 }
637
638 if (smp->slave_ltk) {
639 smp->slave_ltk->bdaddr_type = hcon->dst_type;
640 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
641 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
642 }
643}
644
645static int smp_distribute_keys(struct l2cap_conn *conn)
646{
647 struct smp_cmd_pairing *req, *rsp;
648 struct l2cap_chan *chan = conn->smp;
649 struct smp_chan *smp = chan->data;
650 struct hci_conn *hcon = conn->hcon;
651 struct hci_dev *hdev = hcon->hdev;
652 __u8 *keydist;
653
654 BT_DBG("conn %p", conn);
655
656 if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
657 return 0;
658
659 rsp = (void *) &smp->prsp[1];
660
661 /* The responder sends its keys first */
662 if (hcon->out && (smp->remote_key_dist & 0x07))
663 return 0;
664
665 req = (void *) &smp->preq[1];
666
667 if (hcon->out) {
668 keydist = &rsp->init_key_dist;
669 *keydist &= req->init_key_dist;
670 } else {
671 keydist = &rsp->resp_key_dist;
672 *keydist &= req->resp_key_dist;
673 }
674
675 BT_DBG("keydist 0x%x", *keydist);
676
677 if (*keydist & SMP_DIST_ENC_KEY) {
678 struct smp_cmd_encrypt_info enc;
679 struct smp_cmd_master_ident ident;
680 struct smp_ltk *ltk;
681 u8 authenticated;
682 __le16 ediv;
683 __le64 rand;
684
685 get_random_bytes(enc.ltk, sizeof(enc.ltk));
686 get_random_bytes(&ediv, sizeof(ediv));
687 get_random_bytes(&rand, sizeof(rand));
688
689 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
690
691 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
692 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
693 SMP_LTK_SLAVE, authenticated, enc.ltk,
694 smp->enc_key_size, ediv, rand);
695 smp->slave_ltk = ltk;
696
697 ident.ediv = ediv;
698 ident.rand = rand;
699
700 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
701
702 *keydist &= ~SMP_DIST_ENC_KEY;
703 }
704
705 if (*keydist & SMP_DIST_ID_KEY) {
706 struct smp_cmd_ident_addr_info addrinfo;
707 struct smp_cmd_ident_info idinfo;
708
709 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
710
711 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
712
713 /* The hci_conn contains the local identity address
714 * after the connection has been established.
715 *
716 * This is true even when the connection has been
717 * established using a resolvable random address.
718 */
719 bacpy(&addrinfo.bdaddr, &hcon->src);
720 addrinfo.addr_type = hcon->src_type;
721
722 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
723 &addrinfo);
724
725 *keydist &= ~SMP_DIST_ID_KEY;
726 }
727
728 if (*keydist & SMP_DIST_SIGN) {
729 struct smp_cmd_sign_info sign;
730 struct smp_csrk *csrk;
731
732 /* Generate a new random key */
733 get_random_bytes(sign.csrk, sizeof(sign.csrk));
734
735 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
736 if (csrk) {
737 csrk->master = 0x00;
738 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
739 }
740 smp->slave_csrk = csrk;
741
742 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
743
744 *keydist &= ~SMP_DIST_SIGN;
745 }
746
747 /* If there are still keys to be received wait for them */
748 if ((smp->remote_key_dist & 0x07))
749 return 0;
750
751 clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);
752 cancel_delayed_work_sync(&conn->security_timer);
753 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
754 smp_notify_keys(conn);
755
756 smp_chan_destroy(conn);
757
758 return 0;
759}
760
578static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) 761static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
579{ 762{
580 struct l2cap_chan *chan = conn->smp; 763 struct l2cap_chan *chan = conn->smp;
@@ -1294,189 +1477,6 @@ done:
1294 return err; 1477 return err;
1295} 1478}
1296 1479
1297static void smp_notify_keys(struct l2cap_conn *conn)
1298{
1299 struct l2cap_chan *chan = conn->smp;
1300 struct smp_chan *smp = chan->data;
1301 struct hci_conn *hcon = conn->hcon;
1302 struct hci_dev *hdev = hcon->hdev;
1303 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1304 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1305 bool persistent;
1306
1307 if (smp->remote_irk) {
1308 mgmt_new_irk(hdev, smp->remote_irk);
1309 /* Now that user space can be considered to know the
1310 * identity address track the connection based on it
1311 * from now on.
1312 */
1313 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1314 hcon->dst_type = smp->remote_irk->addr_type;
1315 l2cap_conn_update_id_addr(hcon);
1316
1317 /* When receiving an indentity resolving key for
1318 * a remote device that does not use a resolvable
1319 * private address, just remove the key so that
1320 * it is possible to use the controller white
1321 * list for scanning.
1322 *
1323 * Userspace will have been told to not store
1324 * this key at this point. So it is safe to
1325 * just remove it.
1326 */
1327 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
1328 list_del(&smp->remote_irk->list);
1329 kfree(smp->remote_irk);
1330 smp->remote_irk = NULL;
1331 }
1332 }
1333
1334 /* The LTKs and CSRKs should be persistent only if both sides
1335 * had the bonding bit set in their authentication requests.
1336 */
1337 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
1338
1339 if (smp->csrk) {
1340 smp->csrk->bdaddr_type = hcon->dst_type;
1341 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1342 mgmt_new_csrk(hdev, smp->csrk, persistent);
1343 }
1344
1345 if (smp->slave_csrk) {
1346 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1347 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1348 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1349 }
1350
1351 if (smp->ltk) {
1352 smp->ltk->bdaddr_type = hcon->dst_type;
1353 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1354 mgmt_new_ltk(hdev, smp->ltk, persistent);
1355 }
1356
1357 if (smp->slave_ltk) {
1358 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1359 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1360 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1361 }
1362}
1363
1364int smp_distribute_keys(struct l2cap_conn *conn)
1365{
1366 struct smp_cmd_pairing *req, *rsp;
1367 struct l2cap_chan *chan = conn->smp;
1368 struct smp_chan *smp = chan->data;
1369 struct hci_conn *hcon = conn->hcon;
1370 struct hci_dev *hdev = hcon->hdev;
1371 __u8 *keydist;
1372
1373 BT_DBG("conn %p", conn);
1374
1375 if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
1376 return 0;
1377
1378 rsp = (void *) &smp->prsp[1];
1379
1380 /* The responder sends its keys first */
1381 if (hcon->out && (smp->remote_key_dist & 0x07))
1382 return 0;
1383
1384 req = (void *) &smp->preq[1];
1385
1386 if (hcon->out) {
1387 keydist = &rsp->init_key_dist;
1388 *keydist &= req->init_key_dist;
1389 } else {
1390 keydist = &rsp->resp_key_dist;
1391 *keydist &= req->resp_key_dist;
1392 }
1393
1394 BT_DBG("keydist 0x%x", *keydist);
1395
1396 if (*keydist & SMP_DIST_ENC_KEY) {
1397 struct smp_cmd_encrypt_info enc;
1398 struct smp_cmd_master_ident ident;
1399 struct smp_ltk *ltk;
1400 u8 authenticated;
1401 __le16 ediv;
1402 __le64 rand;
1403
1404 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1405 get_random_bytes(&ediv, sizeof(ediv));
1406 get_random_bytes(&rand, sizeof(rand));
1407
1408 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1409
1410 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1411 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1412 SMP_LTK_SLAVE, authenticated, enc.ltk,
1413 smp->enc_key_size, ediv, rand);
1414 smp->slave_ltk = ltk;
1415
1416 ident.ediv = ediv;
1417 ident.rand = rand;
1418
1419 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1420
1421 *keydist &= ~SMP_DIST_ENC_KEY;
1422 }
1423
1424 if (*keydist & SMP_DIST_ID_KEY) {
1425 struct smp_cmd_ident_addr_info addrinfo;
1426 struct smp_cmd_ident_info idinfo;
1427
1428 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1429
1430 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1431
1432 /* The hci_conn contains the local identity address
1433 * after the connection has been established.
1434 *
1435 * This is true even when the connection has been
1436 * established using a resolvable random address.
1437 */
1438 bacpy(&addrinfo.bdaddr, &hcon->src);
1439 addrinfo.addr_type = hcon->src_type;
1440
1441 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1442 &addrinfo);
1443
1444 *keydist &= ~SMP_DIST_ID_KEY;
1445 }
1446
1447 if (*keydist & SMP_DIST_SIGN) {
1448 struct smp_cmd_sign_info sign;
1449 struct smp_csrk *csrk;
1450
1451 /* Generate a new random key */
1452 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1453
1454 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1455 if (csrk) {
1456 csrk->master = 0x00;
1457 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1458 }
1459 smp->slave_csrk = csrk;
1460
1461 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1462
1463 *keydist &= ~SMP_DIST_SIGN;
1464 }
1465
1466 /* If there are still keys to be received wait for them */
1467 if ((smp->remote_key_dist & 0x07))
1468 return 0;
1469
1470 clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);
1471 cancel_delayed_work_sync(&conn->security_timer);
1472 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1473 smp_notify_keys(conn);
1474
1475 smp_chan_destroy(conn);
1476
1477 return 0;
1478}
1479
1480static void smp_teardown_cb(struct l2cap_chan *chan, int err) 1480static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1481{ 1481{
1482 struct l2cap_conn *conn = chan->conn; 1482 struct l2cap_conn *conn = chan->conn;
@@ -1492,6 +1492,18 @@ static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1492 l2cap_chan_put(chan); 1492 l2cap_chan_put(chan);
1493} 1493}
1494 1494
1495static void smp_resume_cb(struct l2cap_chan *chan)
1496{
1497 struct l2cap_conn *conn = chan->conn;
1498 struct hci_conn *hcon = conn->hcon;
1499
1500 BT_DBG("chan %p", chan);
1501
1502 if (test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1503 smp_distribute_keys(conn);
1504 cancel_delayed_work(&conn->security_timer);
1505}
1506
1495static void smp_ready_cb(struct l2cap_chan *chan) 1507static void smp_ready_cb(struct l2cap_chan *chan)
1496{ 1508{
1497 struct l2cap_conn *conn = chan->conn; 1509 struct l2cap_conn *conn = chan->conn;
@@ -1524,13 +1536,13 @@ static const struct l2cap_ops smp_chan_ops = {
1524 .recv = smp_recv_cb, 1536 .recv = smp_recv_cb,
1525 .alloc_skb = smp_alloc_skb_cb, 1537 .alloc_skb = smp_alloc_skb_cb,
1526 .teardown = smp_teardown_cb, 1538 .teardown = smp_teardown_cb,
1539 .resume = smp_resume_cb,
1527 1540
1528 .new_connection = l2cap_chan_no_new_connection, 1541 .new_connection = l2cap_chan_no_new_connection,
1529 .state_change = l2cap_chan_no_state_change, 1542 .state_change = l2cap_chan_no_state_change,
1530 .close = l2cap_chan_no_close, 1543 .close = l2cap_chan_no_close,
1531 .defer = l2cap_chan_no_defer, 1544 .defer = l2cap_chan_no_defer,
1532 .suspend = l2cap_chan_no_suspend, 1545 .suspend = l2cap_chan_no_suspend,
1533 .resume = l2cap_chan_no_resume,
1534 .set_shutdown = l2cap_chan_no_set_shutdown, 1546 .set_shutdown = l2cap_chan_no_set_shutdown,
1535 .get_sndtimeo = l2cap_chan_no_get_sndtimeo, 1547 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1536 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, 1548 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,