aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/smp.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/bluetooth/smp.c')
-rw-r--r--net/bluetooth/smp.c246
1 files changed, 149 insertions, 97 deletions
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index e33a982161c1..fd3294300803 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -35,11 +35,13 @@
35 35
36#define AUTH_REQ_MASK 0x07 36#define AUTH_REQ_MASK 0x07
37 37
38#define SMP_FLAG_TK_VALID 1 38enum {
39#define SMP_FLAG_CFM_PENDING 2 39 SMP_FLAG_TK_VALID,
40#define SMP_FLAG_MITM_AUTH 3 40 SMP_FLAG_CFM_PENDING,
41#define SMP_FLAG_COMPLETE 4 41 SMP_FLAG_MITM_AUTH,
42#define SMP_FLAG_INITIATOR 5 42 SMP_FLAG_COMPLETE,
43 SMP_FLAG_INITIATOR,
44};
43 45
44struct smp_chan { 46struct smp_chan {
45 struct l2cap_conn *conn; 47 struct l2cap_conn *conn;
@@ -60,20 +62,16 @@ struct smp_chan {
60 struct smp_ltk *slave_ltk; 62 struct smp_ltk *slave_ltk;
61 struct smp_irk *remote_irk; 63 struct smp_irk *remote_irk;
62 unsigned long flags; 64 unsigned long flags;
65
66 struct crypto_blkcipher *tfm_aes;
63}; 67};
64 68
65static inline void swap128(const u8 src[16], u8 dst[16]) 69static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
66{ 70{
67 int i; 71 size_t i;
68 for (i = 0; i < 16; i++)
69 dst[15 - i] = src[i];
70}
71 72
72static inline void swap56(const u8 src[7], u8 dst[7]) 73 for (i = 0; i < len; i++)
73{ 74 dst[len - 1 - i] = src[i];
74 int i;
75 for (i = 0; i < 7; i++)
76 dst[6 - i] = src[i];
77} 75}
78 76
79static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) 77static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
@@ -92,7 +90,7 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
92 desc.flags = 0; 90 desc.flags = 0;
93 91
94 /* The most significant octet of key corresponds to k[0] */ 92 /* The most significant octet of key corresponds to k[0] */
95 swap128(k, tmp); 93 swap_buf(k, tmp, 16);
96 94
97 err = crypto_blkcipher_setkey(tfm, tmp, 16); 95 err = crypto_blkcipher_setkey(tfm, tmp, 16);
98 if (err) { 96 if (err) {
@@ -101,7 +99,7 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
101 } 99 }
102 100
103 /* Most significant octet of plaintextData corresponds to data[0] */ 101 /* Most significant octet of plaintextData corresponds to data[0] */
104 swap128(r, data); 102 swap_buf(r, data, 16);
105 103
106 sg_init_one(&sg, data, 16); 104 sg_init_one(&sg, data, 16);
107 105
@@ -110,7 +108,7 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
110 BT_ERR("Encrypt data error %d", err); 108 BT_ERR("Encrypt data error %d", err);
111 109
112 /* Most significant octet of encryptedData corresponds to data[0] */ 110 /* Most significant octet of encryptedData corresponds to data[0] */
113 swap128(data, r); 111 swap_buf(data, r, 16);
114 112
115 return err; 113 return err;
116} 114}
@@ -174,13 +172,16 @@ int smp_generate_rpa(struct crypto_blkcipher *tfm, u8 irk[16], bdaddr_t *rpa)
174 return 0; 172 return 0;
175} 173}
176 174
177static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16], 175static int smp_c1(struct smp_chan *smp, u8 k[16], u8 r[16], u8 preq[7],
178 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, 176 u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, bdaddr_t *ra,
179 u8 _rat, bdaddr_t *ra, u8 res[16]) 177 u8 res[16])
180{ 178{
179 struct hci_dev *hdev = smp->conn->hcon->hdev;
181 u8 p1[16], p2[16]; 180 u8 p1[16], p2[16];
182 int err; 181 int err;
183 182
183 BT_DBG("%s", hdev->name);
184
184 memset(p1, 0, 16); 185 memset(p1, 0, 16);
185 186
186 /* p1 = pres || preq || _rat || _iat */ 187 /* p1 = pres || preq || _rat || _iat */
@@ -198,7 +199,7 @@ static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],
198 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); 199 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
199 200
200 /* res = e(k, res) */ 201 /* res = e(k, res) */
201 err = smp_e(tfm, k, res); 202 err = smp_e(smp->tfm_aes, k, res);
202 if (err) { 203 if (err) {
203 BT_ERR("Encrypt data error"); 204 BT_ERR("Encrypt data error");
204 return err; 205 return err;
@@ -208,23 +209,26 @@ static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],
208 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); 209 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
209 210
210 /* res = e(k, res) */ 211 /* res = e(k, res) */
211 err = smp_e(tfm, k, res); 212 err = smp_e(smp->tfm_aes, k, res);
212 if (err) 213 if (err)
213 BT_ERR("Encrypt data error"); 214 BT_ERR("Encrypt data error");
214 215
215 return err; 216 return err;
216} 217}
217 218
218static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], u8 r1[16], 219static int smp_s1(struct smp_chan *smp, u8 k[16], u8 r1[16], u8 r2[16],
219 u8 r2[16], u8 _r[16]) 220 u8 _r[16])
220{ 221{
222 struct hci_dev *hdev = smp->conn->hcon->hdev;
221 int err; 223 int err;
222 224
225 BT_DBG("%s", hdev->name);
226
223 /* Just least significant octets from r1 and r2 are considered */ 227 /* Just least significant octets from r1 and r2 are considered */
224 memcpy(_r, r2, 8); 228 memcpy(_r, r2, 8);
225 memcpy(_r + 8, r1, 8); 229 memcpy(_r + 8, r1, 8);
226 230
227 err = smp_e(tfm, k, _r); 231 err = smp_e(smp->tfm_aes, k, _r);
228 if (err) 232 if (err)
229 BT_ERR("Encrypt data error"); 233 BT_ERR("Encrypt data error");
230 234
@@ -303,7 +307,7 @@ static void build_pairing_cmd(struct l2cap_conn *conn,
303 struct hci_dev *hdev = hcon->hdev; 307 struct hci_dev *hdev = hcon->hdev;
304 u8 local_dist = 0, remote_dist = 0; 308 u8 local_dist = 0, remote_dist = 0;
305 309
306 if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->dev_flags)) { 310 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
307 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; 311 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
308 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; 312 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
309 authreq |= SMP_AUTH_BONDING; 313 authreq |= SMP_AUTH_BONDING;
@@ -387,10 +391,12 @@ static const u8 gen_method[5][5] = {
387 391
388static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io) 392static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
389{ 393{
390 /* If either side has unknown io_caps, use JUST WORKS */ 394 /* If either side has unknown io_caps, use JUST_CFM (which gets
395 * converted later to JUST_WORKS if we're initiators.
396 */
391 if (local_io > SMP_IO_KEYBOARD_DISPLAY || 397 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
392 remote_io > SMP_IO_KEYBOARD_DISPLAY) 398 remote_io > SMP_IO_KEYBOARD_DISPLAY)
393 return JUST_WORKS; 399 return JUST_CFM;
394 400
395 return gen_method[remote_io][local_io]; 401 return gen_method[remote_io][local_io];
396} 402}
@@ -410,21 +416,25 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
410 416
411 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); 417 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
412 418
413 /* If neither side wants MITM, use JUST WORKS */ 419 /* If neither side wants MITM, either "just" confirm an incoming
414 /* Otherwise, look up method from the table */ 420 * request or use just-works for outgoing ones. The JUST_CFM
421 * will be converted to JUST_WORKS if necessary later in this
422 * function. If either side has MITM look up the method from the
423 * table.
424 */
415 if (!(auth & SMP_AUTH_MITM)) 425 if (!(auth & SMP_AUTH_MITM))
416 method = JUST_WORKS; 426 method = JUST_CFM;
417 else 427 else
418 method = get_auth_method(smp, local_io, remote_io); 428 method = get_auth_method(smp, local_io, remote_io);
419 429
420 /* If not bonding, don't ask user to confirm a Zero TK */
421 if (!(auth & SMP_AUTH_BONDING) && method == JUST_CFM)
422 method = JUST_WORKS;
423
424 /* Don't confirm locally initiated pairing attempts */ 430 /* Don't confirm locally initiated pairing attempts */
425 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) 431 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
426 method = JUST_WORKS; 432 method = JUST_WORKS;
427 433
434 /* Don't bother user space with no IO capabilities */
435 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
436 method = JUST_WORKS;
437
428 /* If Just Works, Continue with Zero TK */ 438 /* If Just Works, Continue with Zero TK */
429 if (method == JUST_WORKS) { 439 if (method == JUST_WORKS) {
430 set_bit(SMP_FLAG_TK_VALID, &smp->flags); 440 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
@@ -439,7 +449,7 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
439 * Confirms and the slave Enters the passkey. 449 * Confirms and the slave Enters the passkey.
440 */ 450 */
441 if (method == OVERLAP) { 451 if (method == OVERLAP) {
442 if (hcon->link_mode & HCI_LM_MASTER) 452 if (hcon->role == HCI_ROLE_MASTER)
443 method = CFM_PASSKEY; 453 method = CFM_PASSKEY;
444 else 454 else
445 method = REQ_PASSKEY; 455 method = REQ_PASSKEY;
@@ -477,23 +487,15 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
477static u8 smp_confirm(struct smp_chan *smp) 487static u8 smp_confirm(struct smp_chan *smp)
478{ 488{
479 struct l2cap_conn *conn = smp->conn; 489 struct l2cap_conn *conn = smp->conn;
480 struct hci_dev *hdev = conn->hcon->hdev;
481 struct crypto_blkcipher *tfm = hdev->tfm_aes;
482 struct smp_cmd_pairing_confirm cp; 490 struct smp_cmd_pairing_confirm cp;
483 int ret; 491 int ret;
484 492
485 BT_DBG("conn %p", conn); 493 BT_DBG("conn %p", conn);
486 494
487 /* Prevent mutual access to hdev->tfm_aes */ 495 ret = smp_c1(smp, smp->tk, smp->prnd, smp->preq, smp->prsp,
488 hci_dev_lock(hdev);
489
490 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,
491 conn->hcon->init_addr_type, &conn->hcon->init_addr, 496 conn->hcon->init_addr_type, &conn->hcon->init_addr,
492 conn->hcon->resp_addr_type, &conn->hcon->resp_addr, 497 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
493 cp.confirm_val); 498 cp.confirm_val);
494
495 hci_dev_unlock(hdev);
496
497 if (ret) 499 if (ret)
498 return SMP_UNSPECIFIED; 500 return SMP_UNSPECIFIED;
499 501
@@ -508,25 +510,17 @@ static u8 smp_random(struct smp_chan *smp)
508{ 510{
509 struct l2cap_conn *conn = smp->conn; 511 struct l2cap_conn *conn = smp->conn;
510 struct hci_conn *hcon = conn->hcon; 512 struct hci_conn *hcon = conn->hcon;
511 struct hci_dev *hdev = hcon->hdev;
512 struct crypto_blkcipher *tfm = hdev->tfm_aes;
513 u8 confirm[16]; 513 u8 confirm[16];
514 int ret; 514 int ret;
515 515
516 if (IS_ERR_OR_NULL(tfm)) 516 if (IS_ERR_OR_NULL(smp->tfm_aes))
517 return SMP_UNSPECIFIED; 517 return SMP_UNSPECIFIED;
518 518
519 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); 519 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
520 520
521 /* Prevent mutual access to hdev->tfm_aes */ 521 ret = smp_c1(smp, smp->tk, smp->rrnd, smp->preq, smp->prsp,
522 hci_dev_lock(hdev);
523
524 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,
525 hcon->init_addr_type, &hcon->init_addr, 522 hcon->init_addr_type, &hcon->init_addr,
526 hcon->resp_addr_type, &hcon->resp_addr, confirm); 523 hcon->resp_addr_type, &hcon->resp_addr, confirm);
527
528 hci_dev_unlock(hdev);
529
530 if (ret) 524 if (ret)
531 return SMP_UNSPECIFIED; 525 return SMP_UNSPECIFIED;
532 526
@@ -540,7 +534,7 @@ static u8 smp_random(struct smp_chan *smp)
540 __le64 rand = 0; 534 __le64 rand = 0;
541 __le16 ediv = 0; 535 __le16 ediv = 0;
542 536
543 smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, stk); 537 smp_s1(smp, smp->tk, smp->rrnd, smp->prnd, stk);
544 538
545 memset(stk + smp->enc_key_size, 0, 539 memset(stk + smp->enc_key_size, 0,
546 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); 540 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
@@ -550,6 +544,7 @@ static u8 smp_random(struct smp_chan *smp)
550 544
551 hci_le_start_enc(hcon, ediv, rand, stk); 545 hci_le_start_enc(hcon, ediv, rand, stk);
552 hcon->enc_key_size = smp->enc_key_size; 546 hcon->enc_key_size = smp->enc_key_size;
547 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
553 } else { 548 } else {
554 u8 stk[16], auth; 549 u8 stk[16], auth;
555 __le64 rand = 0; 550 __le64 rand = 0;
@@ -558,7 +553,7 @@ static u8 smp_random(struct smp_chan *smp)
558 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), 553 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
559 smp->prnd); 554 smp->prnd);
560 555
561 smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, stk); 556 smp_s1(smp, smp->tk, smp->prnd, smp->rrnd, stk);
562 557
563 memset(stk + smp->enc_key_size, 0, 558 memset(stk + smp->enc_key_size, 0,
564 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); 559 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
@@ -568,9 +563,12 @@ static u8 smp_random(struct smp_chan *smp)
568 else 563 else
569 auth = 0; 564 auth = 0;
570 565
566 /* Even though there's no _SLAVE suffix this is the
567 * slave STK we're adding for later lookup (the master
568 * STK never needs to be stored).
569 */
571 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, 570 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
572 HCI_SMP_STK_SLAVE, auth, stk, smp->enc_key_size, 571 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
573 ediv, rand);
574 } 572 }
575 573
576 return 0; 574 return 0;
@@ -581,12 +579,21 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
581 struct smp_chan *smp; 579 struct smp_chan *smp;
582 580
583 smp = kzalloc(sizeof(*smp), GFP_ATOMIC); 581 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
584 if (!smp) 582 if (!smp) {
583 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
585 return NULL; 584 return NULL;
585 }
586
587 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
588 if (IS_ERR(smp->tfm_aes)) {
589 BT_ERR("Unable to create ECB crypto context");
590 kfree(smp);
591 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
592 return NULL;
593 }
586 594
587 smp->conn = conn; 595 smp->conn = conn;
588 conn->smp_chan = smp; 596 conn->smp_chan = smp;
589 conn->hcon->smp_conn = conn;
590 597
591 hci_conn_hold(conn->hcon); 598 hci_conn_hold(conn->hcon);
592 599
@@ -606,6 +613,8 @@ void smp_chan_destroy(struct l2cap_conn *conn)
606 kfree(smp->csrk); 613 kfree(smp->csrk);
607 kfree(smp->slave_csrk); 614 kfree(smp->slave_csrk);
608 615
616 crypto_free_blkcipher(smp->tfm_aes);
617
609 /* If pairing failed clean up any keys we might have */ 618 /* If pairing failed clean up any keys we might have */
610 if (!complete) { 619 if (!complete) {
611 if (smp->ltk) { 620 if (smp->ltk) {
@@ -626,19 +635,18 @@ void smp_chan_destroy(struct l2cap_conn *conn)
626 635
627 kfree(smp); 636 kfree(smp);
628 conn->smp_chan = NULL; 637 conn->smp_chan = NULL;
629 conn->hcon->smp_conn = NULL;
630 hci_conn_drop(conn->hcon); 638 hci_conn_drop(conn->hcon);
631} 639}
632 640
633int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) 641int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
634{ 642{
635 struct l2cap_conn *conn = hcon->smp_conn; 643 struct l2cap_conn *conn = hcon->l2cap_data;
636 struct smp_chan *smp; 644 struct smp_chan *smp;
637 u32 value; 645 u32 value;
638 646
639 BT_DBG(""); 647 BT_DBG("");
640 648
641 if (!conn) 649 if (!conn || !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
642 return -ENOTCONN; 650 return -ENOTCONN;
643 651
644 smp = conn->smp_chan; 652 smp = conn->smp_chan;
@@ -675,6 +683,7 @@ int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
675static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) 683static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
676{ 684{
677 struct smp_cmd_pairing rsp, *req = (void *) skb->data; 685 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
686 struct hci_dev *hdev = conn->hcon->hdev;
678 struct smp_chan *smp; 687 struct smp_chan *smp;
679 u8 key_size, auth, sec_level; 688 u8 key_size, auth, sec_level;
680 int ret; 689 int ret;
@@ -684,7 +693,7 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
684 if (skb->len < sizeof(*req)) 693 if (skb->len < sizeof(*req))
685 return SMP_INVALID_PARAMS; 694 return SMP_INVALID_PARAMS;
686 695
687 if (conn->hcon->link_mode & HCI_LM_MASTER) 696 if (conn->hcon->role != HCI_ROLE_SLAVE)
688 return SMP_CMD_NOTSUPP; 697 return SMP_CMD_NOTSUPP;
689 698
690 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) 699 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
@@ -695,6 +704,10 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
695 if (!smp) 704 if (!smp)
696 return SMP_UNSPECIFIED; 705 return SMP_UNSPECIFIED;
697 706
707 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
708 (req->auth_req & SMP_AUTH_BONDING))
709 return SMP_PAIRING_NOTSUPP;
710
698 smp->preq[0] = SMP_CMD_PAIRING_REQ; 711 smp->preq[0] = SMP_CMD_PAIRING_REQ;
699 memcpy(&smp->preq[1], req, sizeof(*req)); 712 memcpy(&smp->preq[1], req, sizeof(*req));
700 skb_pull(skb, sizeof(*req)); 713 skb_pull(skb, sizeof(*req));
@@ -734,8 +747,6 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
734 if (ret) 747 if (ret)
735 return SMP_UNSPECIFIED; 748 return SMP_UNSPECIFIED;
736 749
737 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
738
739 return 0; 750 return 0;
740} 751}
741 752
@@ -751,7 +762,7 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
751 if (skb->len < sizeof(*rsp)) 762 if (skb->len < sizeof(*rsp))
752 return SMP_INVALID_PARAMS; 763 return SMP_INVALID_PARAMS;
753 764
754 if (!(conn->hcon->link_mode & HCI_LM_MASTER)) 765 if (conn->hcon->role != HCI_ROLE_MASTER)
755 return SMP_CMD_NOTSUPP; 766 return SMP_CMD_NOTSUPP;
756 767
757 skb_pull(skb, sizeof(*rsp)); 768 skb_pull(skb, sizeof(*rsp));
@@ -839,26 +850,51 @@ static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
839 return smp_random(smp); 850 return smp_random(smp);
840} 851}
841 852
842static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) 853static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
843{ 854{
844 struct smp_ltk *key; 855 struct smp_ltk *key;
845 struct hci_conn *hcon = conn->hcon; 856 struct hci_conn *hcon = conn->hcon;
846 857
847 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type, 858 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
848 hcon->out); 859 hcon->role);
849 if (!key) 860 if (!key)
850 return 0; 861 return false;
851 862
852 if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated) 863 if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated)
853 return 0; 864 return false;
854 865
855 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) 866 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
856 return 1; 867 return true;
857 868
858 hci_le_start_enc(hcon, key->ediv, key->rand, key->val); 869 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
859 hcon->enc_key_size = key->enc_size; 870 hcon->enc_key_size = key->enc_size;
860 871
861 return 1; 872 /* We never store STKs for master role, so clear this flag */
873 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
874
875 return true;
876}
877
878bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level)
879{
880 if (sec_level == BT_SECURITY_LOW)
881 return true;
882
883 /* If we're encrypted with an STK always claim insufficient
884 * security. This way we allow the connection to be re-encrypted
885 * with an LTK, even if the LTK provides the same level of
886 * security. Only exception is if we don't have an LTK (e.g.
887 * because of key distribution bits).
888 */
889 if (test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
890 hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
891 hcon->role))
892 return false;
893
894 if (hcon->sec_level >= sec_level)
895 return true;
896
897 return false;
862} 898}
863 899
864static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) 900static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
@@ -874,10 +910,13 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
874 if (skb->len < sizeof(*rp)) 910 if (skb->len < sizeof(*rp))
875 return SMP_INVALID_PARAMS; 911 return SMP_INVALID_PARAMS;
876 912
877 if (!(conn->hcon->link_mode & HCI_LM_MASTER)) 913 if (hcon->role != HCI_ROLE_MASTER)
878 return SMP_CMD_NOTSUPP; 914 return SMP_CMD_NOTSUPP;
879 915
880 sec_level = authreq_to_seclevel(rp->auth_req); 916 sec_level = authreq_to_seclevel(rp->auth_req);
917 if (smp_sufficient_security(hcon, sec_level))
918 return 0;
919
881 if (sec_level > hcon->pending_sec_level) 920 if (sec_level > hcon->pending_sec_level)
882 hcon->pending_sec_level = sec_level; 921 hcon->pending_sec_level = sec_level;
883 922
@@ -888,6 +927,12 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
888 return 0; 927 return 0;
889 928
890 smp = smp_chan_create(conn); 929 smp = smp_chan_create(conn);
930 if (!smp)
931 return SMP_UNSPECIFIED;
932
933 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
934 (rp->auth_req & SMP_AUTH_BONDING))
935 return SMP_PAIRING_NOTSUPP;
891 936
892 skb_pull(skb, sizeof(*rp)); 937 skb_pull(skb, sizeof(*rp));
893 938
@@ -899,22 +944,9 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
899 944
900 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); 945 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
901 946
902 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
903
904 return 0; 947 return 0;
905} 948}
906 949
907bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level)
908{
909 if (sec_level == BT_SECURITY_LOW)
910 return true;
911
912 if (hcon->sec_level >= sec_level)
913 return true;
914
915 return false;
916}
917
918int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) 950int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
919{ 951{
920 struct l2cap_conn *conn = hcon->l2cap_data; 952 struct l2cap_conn *conn = hcon->l2cap_data;
@@ -936,7 +968,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
936 if (sec_level > hcon->pending_sec_level) 968 if (sec_level > hcon->pending_sec_level)
937 hcon->pending_sec_level = sec_level; 969 hcon->pending_sec_level = sec_level;
938 970
939 if (hcon->link_mode & HCI_LM_MASTER) 971 if (hcon->role == HCI_ROLE_MASTER)
940 if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) 972 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
941 return 0; 973 return 0;
942 974
@@ -956,7 +988,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
956 hcon->pending_sec_level > BT_SECURITY_MEDIUM) 988 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
957 authreq |= SMP_AUTH_MITM; 989 authreq |= SMP_AUTH_MITM;
958 990
959 if (hcon->link_mode & HCI_LM_MASTER) { 991 if (hcon->role == HCI_ROLE_MASTER) {
960 struct smp_cmd_pairing cp; 992 struct smp_cmd_pairing cp;
961 993
962 build_pairing_cmd(conn, &cp, NULL, authreq); 994 build_pairing_cmd(conn, &cp, NULL, authreq);
@@ -1021,7 +1053,7 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1021 1053
1022 hci_dev_lock(hdev); 1054 hci_dev_lock(hdev);
1023 authenticated = (hcon->sec_level == BT_SECURITY_HIGH); 1055 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
1024 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, HCI_SMP_LTK, 1056 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
1025 authenticated, smp->tk, smp->enc_key_size, 1057 authenticated, smp->tk, smp->enc_key_size,
1026 rp->ediv, rp->rand); 1058 rp->ediv, rp->rand);
1027 smp->ltk = ltk; 1059 smp->ltk = ltk;
@@ -1075,6 +1107,8 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1075 1107
1076 skb_pull(skb, sizeof(*info)); 1108 skb_pull(skb, sizeof(*info));
1077 1109
1110 hci_dev_lock(hcon->hdev);
1111
1078 /* Strictly speaking the Core Specification (4.1) allows sending 1112 /* Strictly speaking the Core Specification (4.1) allows sending
1079 * an empty address which would force us to rely on just the IRK 1113 * an empty address which would force us to rely on just the IRK
1080 * as "identity information". However, since such 1114 * as "identity information". However, since such
@@ -1084,8 +1118,7 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1084 */ 1118 */
1085 if (!bacmp(&info->bdaddr, BDADDR_ANY)) { 1119 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1086 BT_ERR("Ignoring IRK with no identity address"); 1120 BT_ERR("Ignoring IRK with no identity address");
1087 smp_distribute_keys(conn); 1121 goto distribute;
1088 return 0;
1089 } 1122 }
1090 1123
1091 bacpy(&smp->id_addr, &info->bdaddr); 1124 bacpy(&smp->id_addr, &info->bdaddr);
@@ -1099,8 +1132,11 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1099 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr, 1132 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1100 smp->id_addr_type, smp->irk, &rpa); 1133 smp->id_addr_type, smp->irk, &rpa);
1101 1134
1135distribute:
1102 smp_distribute_keys(conn); 1136 smp_distribute_keys(conn);
1103 1137
1138 hci_dev_unlock(hcon->hdev);
1139
1104 return 0; 1140 return 0;
1105} 1141}
1106 1142
@@ -1156,7 +1192,7 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
1156 } 1192 }
1157 1193
1158 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { 1194 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
1159 err = -ENOTSUPP; 1195 err = -EOPNOTSUPP;
1160 reason = SMP_PAIRING_NOTSUPP; 1196 reason = SMP_PAIRING_NOTSUPP;
1161 goto done; 1197 goto done;
1162 } 1198 }
@@ -1174,7 +1210,7 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
1174 !conn->smp_chan) { 1210 !conn->smp_chan) {
1175 BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code); 1211 BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code);
1176 kfree_skb(skb); 1212 kfree_skb(skb);
1177 return -ENOTSUPP; 1213 return -EOPNOTSUPP;
1178 } 1214 }
1179 1215
1180 switch (code) { 1216 switch (code) {
@@ -1258,6 +1294,22 @@ static void smp_notify_keys(struct l2cap_conn *conn)
1258 bacpy(&hcon->dst, &smp->remote_irk->bdaddr); 1294 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1259 hcon->dst_type = smp->remote_irk->addr_type; 1295 hcon->dst_type = smp->remote_irk->addr_type;
1260 l2cap_conn_update_id_addr(hcon); 1296 l2cap_conn_update_id_addr(hcon);
1297
1298 /* When receiving an indentity resolving key for
1299 * a remote device that does not use a resolvable
1300 * private address, just remove the key so that
1301 * it is possible to use the controller white
1302 * list for scanning.
1303 *
1304 * Userspace will have been told to not store
1305 * this key at this point. So it is safe to
1306 * just remove it.
1307 */
1308 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
1309 list_del(&smp->remote_irk->list);
1310 kfree(smp->remote_irk);
1311 smp->remote_irk = NULL;
1312 }
1261 } 1313 }
1262 1314
1263 /* The LTKs and CSRKs should be persistent only if both sides 1315 /* The LTKs and CSRKs should be persistent only if both sides
@@ -1337,7 +1389,7 @@ int smp_distribute_keys(struct l2cap_conn *conn)
1337 1389
1338 authenticated = hcon->sec_level == BT_SECURITY_HIGH; 1390 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1339 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, 1391 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1340 HCI_SMP_LTK_SLAVE, authenticated, enc.ltk, 1392 SMP_LTK_SLAVE, authenticated, enc.ltk,
1341 smp->enc_key_size, ediv, rand); 1393 smp->enc_key_size, ediv, rand);
1342 smp->slave_ltk = ltk; 1394 smp->slave_ltk = ltk;
1343 1395