diff options
Diffstat (limited to 'net/bluetooth/smp.c')
-rw-r--r-- | net/bluetooth/smp.c | 726 |
1 files changed, 489 insertions, 237 deletions
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index fd3294300803..07ca4ce0943b 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c | |||
@@ -44,7 +44,10 @@ enum { | |||
44 | }; | 44 | }; |
45 | 45 | ||
46 | struct smp_chan { | 46 | struct smp_chan { |
47 | struct l2cap_conn *conn; | 47 | struct l2cap_conn *conn; |
48 | struct delayed_work security_timer; | ||
49 | struct work_struct distribute_work; | ||
50 | |||
48 | u8 preq[7]; /* SMP Pairing Request */ | 51 | u8 preq[7]; /* SMP Pairing Request */ |
49 | u8 prsp[7]; /* SMP Pairing Response */ | 52 | u8 prsp[7]; /* SMP Pairing Response */ |
50 | u8 prnd[16]; /* SMP Pairing Random (local) */ | 53 | u8 prnd[16]; /* SMP Pairing Random (local) */ |
@@ -139,12 +142,18 @@ static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3]) | |||
139 | return 0; | 142 | return 0; |
140 | } | 143 | } |
141 | 144 | ||
142 | bool smp_irk_matches(struct crypto_blkcipher *tfm, u8 irk[16], | 145 | bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr) |
143 | bdaddr_t *bdaddr) | ||
144 | { | 146 | { |
147 | struct l2cap_chan *chan = hdev->smp_data; | ||
148 | struct crypto_blkcipher *tfm; | ||
145 | u8 hash[3]; | 149 | u8 hash[3]; |
146 | int err; | 150 | int err; |
147 | 151 | ||
152 | if (!chan || !chan->data) | ||
153 | return false; | ||
154 | |||
155 | tfm = chan->data; | ||
156 | |||
148 | BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); | 157 | BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); |
149 | 158 | ||
150 | err = smp_ah(tfm, irk, &bdaddr->b[3], hash); | 159 | err = smp_ah(tfm, irk, &bdaddr->b[3], hash); |
@@ -154,10 +163,17 @@ bool smp_irk_matches(struct crypto_blkcipher *tfm, u8 irk[16], | |||
154 | return !memcmp(bdaddr->b, hash, 3); | 163 | return !memcmp(bdaddr->b, hash, 3); |
155 | } | 164 | } |
156 | 165 | ||
157 | int smp_generate_rpa(struct crypto_blkcipher *tfm, u8 irk[16], bdaddr_t *rpa) | 166 | int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa) |
158 | { | 167 | { |
168 | struct l2cap_chan *chan = hdev->smp_data; | ||
169 | struct crypto_blkcipher *tfm; | ||
159 | int err; | 170 | int err; |
160 | 171 | ||
172 | if (!chan || !chan->data) | ||
173 | return -EOPNOTSUPP; | ||
174 | |||
175 | tfm = chan->data; | ||
176 | |||
161 | get_random_bytes(&rpa->b[3], 3); | 177 | get_random_bytes(&rpa->b[3], 3); |
162 | 178 | ||
163 | rpa->b[5] &= 0x3f; /* Clear two most significant bits */ | 179 | rpa->b[5] &= 0x3f; /* Clear two most significant bits */ |
@@ -235,47 +251,39 @@ static int smp_s1(struct smp_chan *smp, u8 k[16], u8 r1[16], u8 r2[16], | |||
235 | return err; | 251 | return err; |
236 | } | 252 | } |
237 | 253 | ||
238 | static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code, | 254 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) |
239 | u16 dlen, void *data) | ||
240 | { | 255 | { |
241 | struct sk_buff *skb; | 256 | struct l2cap_chan *chan = conn->smp; |
242 | struct l2cap_hdr *lh; | 257 | struct smp_chan *smp; |
243 | int len; | 258 | struct kvec iv[2]; |
244 | 259 | struct msghdr msg; | |
245 | len = L2CAP_HDR_SIZE + sizeof(code) + dlen; | ||
246 | |||
247 | if (len > conn->mtu) | ||
248 | return NULL; | ||
249 | 260 | ||
250 | skb = bt_skb_alloc(len, GFP_ATOMIC); | 261 | if (!chan) |
251 | if (!skb) | 262 | return; |
252 | return NULL; | ||
253 | 263 | ||
254 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); | 264 | BT_DBG("code 0x%2.2x", code); |
255 | lh->len = cpu_to_le16(sizeof(code) + dlen); | ||
256 | lh->cid = cpu_to_le16(L2CAP_CID_SMP); | ||
257 | 265 | ||
258 | memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code)); | 266 | iv[0].iov_base = &code; |
267 | iv[0].iov_len = 1; | ||
259 | 268 | ||
260 | memcpy(skb_put(skb, dlen), data, dlen); | 269 | iv[1].iov_base = data; |
270 | iv[1].iov_len = len; | ||
261 | 271 | ||
262 | return skb; | 272 | memset(&msg, 0, sizeof(msg)); |
263 | } | ||
264 | 273 | ||
265 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) | 274 | msg.msg_iov = (struct iovec *) &iv; |
266 | { | 275 | msg.msg_iovlen = 2; |
267 | struct sk_buff *skb = smp_build_cmd(conn, code, len, data); | ||
268 | 276 | ||
269 | BT_DBG("code 0x%2.2x", code); | 277 | l2cap_chan_send(chan, &msg, 1 + len); |
270 | 278 | ||
271 | if (!skb) | 279 | if (!chan->data) |
272 | return; | 280 | return; |
273 | 281 | ||
274 | skb->priority = HCI_PRIO_MAX; | 282 | smp = chan->data; |
275 | hci_send_acl(conn->hchan, skb, 0); | ||
276 | 283 | ||
277 | cancel_delayed_work_sync(&conn->security_timer); | 284 | cancel_delayed_work_sync(&smp->security_timer); |
278 | schedule_delayed_work(&conn->security_timer, SMP_TIMEOUT); | 285 | if (test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) |
286 | schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT); | ||
279 | } | 287 | } |
280 | 288 | ||
281 | static __u8 authreq_to_seclevel(__u8 authreq) | 289 | static __u8 authreq_to_seclevel(__u8 authreq) |
@@ -302,7 +310,8 @@ static void build_pairing_cmd(struct l2cap_conn *conn, | |||
302 | struct smp_cmd_pairing *req, | 310 | struct smp_cmd_pairing *req, |
303 | struct smp_cmd_pairing *rsp, __u8 authreq) | 311 | struct smp_cmd_pairing *rsp, __u8 authreq) |
304 | { | 312 | { |
305 | struct smp_chan *smp = conn->smp_chan; | 313 | struct l2cap_chan *chan = conn->smp; |
314 | struct smp_chan *smp = chan->data; | ||
306 | struct hci_conn *hcon = conn->hcon; | 315 | struct hci_conn *hcon = conn->hcon; |
307 | struct hci_dev *hdev = hcon->hdev; | 316 | struct hci_dev *hdev = hcon->hdev; |
308 | u8 local_dist = 0, remote_dist = 0; | 317 | u8 local_dist = 0, remote_dist = 0; |
@@ -345,7 +354,8 @@ static void build_pairing_cmd(struct l2cap_conn *conn, | |||
345 | 354 | ||
346 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) | 355 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
347 | { | 356 | { |
348 | struct smp_chan *smp = conn->smp_chan; | 357 | struct l2cap_chan *chan = conn->smp; |
358 | struct smp_chan *smp = chan->data; | ||
349 | 359 | ||
350 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || | 360 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
351 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) | 361 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
@@ -356,9 +366,61 @@ static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) | |||
356 | return 0; | 366 | return 0; |
357 | } | 367 | } |
358 | 368 | ||
369 | static void smp_chan_destroy(struct l2cap_conn *conn) | ||
370 | { | ||
371 | struct l2cap_chan *chan = conn->smp; | ||
372 | struct smp_chan *smp = chan->data; | ||
373 | bool complete; | ||
374 | |||
375 | BUG_ON(!smp); | ||
376 | |||
377 | cancel_delayed_work_sync(&smp->security_timer); | ||
378 | /* In case the timeout freed the SMP context */ | ||
379 | if (!chan->data) | ||
380 | return; | ||
381 | |||
382 | if (work_pending(&smp->distribute_work)) { | ||
383 | cancel_work_sync(&smp->distribute_work); | ||
384 | if (!chan->data) | ||
385 | return; | ||
386 | } | ||
387 | |||
388 | complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); | ||
389 | mgmt_smp_complete(conn->hcon, complete); | ||
390 | |||
391 | kfree(smp->csrk); | ||
392 | kfree(smp->slave_csrk); | ||
393 | |||
394 | crypto_free_blkcipher(smp->tfm_aes); | ||
395 | |||
396 | /* If pairing failed clean up any keys we might have */ | ||
397 | if (!complete) { | ||
398 | if (smp->ltk) { | ||
399 | list_del(&smp->ltk->list); | ||
400 | kfree(smp->ltk); | ||
401 | } | ||
402 | |||
403 | if (smp->slave_ltk) { | ||
404 | list_del(&smp->slave_ltk->list); | ||
405 | kfree(smp->slave_ltk); | ||
406 | } | ||
407 | |||
408 | if (smp->remote_irk) { | ||
409 | list_del(&smp->remote_irk->list); | ||
410 | kfree(smp->remote_irk); | ||
411 | } | ||
412 | } | ||
413 | |||
414 | chan->data = NULL; | ||
415 | kfree(smp); | ||
416 | hci_conn_drop(conn->hcon); | ||
417 | } | ||
418 | |||
359 | static void smp_failure(struct l2cap_conn *conn, u8 reason) | 419 | static void smp_failure(struct l2cap_conn *conn, u8 reason) |
360 | { | 420 | { |
361 | struct hci_conn *hcon = conn->hcon; | 421 | struct hci_conn *hcon = conn->hcon; |
422 | struct l2cap_chan *chan = conn->smp; | ||
423 | struct smp_chan *smp; | ||
362 | 424 | ||
363 | if (reason) | 425 | if (reason) |
364 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), | 426 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
@@ -368,7 +430,10 @@ static void smp_failure(struct l2cap_conn *conn, u8 reason) | |||
368 | mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type, | 430 | mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type, |
369 | HCI_ERROR_AUTH_FAILURE); | 431 | HCI_ERROR_AUTH_FAILURE); |
370 | 432 | ||
371 | cancel_delayed_work_sync(&conn->security_timer); | 433 | if (!chan->data) |
434 | return; | ||
435 | |||
436 | smp = chan->data; | ||
372 | 437 | ||
373 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) | 438 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
374 | smp_chan_destroy(conn); | 439 | smp_chan_destroy(conn); |
@@ -405,7 +470,8 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, | |||
405 | u8 local_io, u8 remote_io) | 470 | u8 local_io, u8 remote_io) |
406 | { | 471 | { |
407 | struct hci_conn *hcon = conn->hcon; | 472 | struct hci_conn *hcon = conn->hcon; |
408 | struct smp_chan *smp = conn->smp_chan; | 473 | struct l2cap_chan *chan = conn->smp; |
474 | struct smp_chan *smp = chan->data; | ||
409 | u8 method; | 475 | u8 method; |
410 | u32 passkey = 0; | 476 | u32 passkey = 0; |
411 | int ret = 0; | 477 | int ret = 0; |
@@ -574,8 +640,201 @@ static u8 smp_random(struct smp_chan *smp) | |||
574 | return 0; | 640 | return 0; |
575 | } | 641 | } |
576 | 642 | ||
643 | static void smp_notify_keys(struct l2cap_conn *conn) | ||
644 | { | ||
645 | struct l2cap_chan *chan = conn->smp; | ||
646 | struct smp_chan *smp = chan->data; | ||
647 | struct hci_conn *hcon = conn->hcon; | ||
648 | struct hci_dev *hdev = hcon->hdev; | ||
649 | struct smp_cmd_pairing *req = (void *) &smp->preq[1]; | ||
650 | struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1]; | ||
651 | bool persistent; | ||
652 | |||
653 | if (smp->remote_irk) { | ||
654 | mgmt_new_irk(hdev, smp->remote_irk); | ||
655 | /* Now that user space can be considered to know the | ||
656 | * identity address track the connection based on it | ||
657 | * from now on. | ||
658 | */ | ||
659 | bacpy(&hcon->dst, &smp->remote_irk->bdaddr); | ||
660 | hcon->dst_type = smp->remote_irk->addr_type; | ||
661 | l2cap_conn_update_id_addr(hcon); | ||
662 | |||
663 | /* When receiving an indentity resolving key for | ||
664 | * a remote device that does not use a resolvable | ||
665 | * private address, just remove the key so that | ||
666 | * it is possible to use the controller white | ||
667 | * list for scanning. | ||
668 | * | ||
669 | * Userspace will have been told to not store | ||
670 | * this key at this point. So it is safe to | ||
671 | * just remove it. | ||
672 | */ | ||
673 | if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) { | ||
674 | list_del(&smp->remote_irk->list); | ||
675 | kfree(smp->remote_irk); | ||
676 | smp->remote_irk = NULL; | ||
677 | } | ||
678 | } | ||
679 | |||
680 | /* The LTKs and CSRKs should be persistent only if both sides | ||
681 | * had the bonding bit set in their authentication requests. | ||
682 | */ | ||
683 | persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING); | ||
684 | |||
685 | if (smp->csrk) { | ||
686 | smp->csrk->bdaddr_type = hcon->dst_type; | ||
687 | bacpy(&smp->csrk->bdaddr, &hcon->dst); | ||
688 | mgmt_new_csrk(hdev, smp->csrk, persistent); | ||
689 | } | ||
690 | |||
691 | if (smp->slave_csrk) { | ||
692 | smp->slave_csrk->bdaddr_type = hcon->dst_type; | ||
693 | bacpy(&smp->slave_csrk->bdaddr, &hcon->dst); | ||
694 | mgmt_new_csrk(hdev, smp->slave_csrk, persistent); | ||
695 | } | ||
696 | |||
697 | if (smp->ltk) { | ||
698 | smp->ltk->bdaddr_type = hcon->dst_type; | ||
699 | bacpy(&smp->ltk->bdaddr, &hcon->dst); | ||
700 | mgmt_new_ltk(hdev, smp->ltk, persistent); | ||
701 | } | ||
702 | |||
703 | if (smp->slave_ltk) { | ||
704 | smp->slave_ltk->bdaddr_type = hcon->dst_type; | ||
705 | bacpy(&smp->slave_ltk->bdaddr, &hcon->dst); | ||
706 | mgmt_new_ltk(hdev, smp->slave_ltk, persistent); | ||
707 | } | ||
708 | } | ||
709 | |||
710 | static void smp_distribute_keys(struct work_struct *work) | ||
711 | { | ||
712 | struct smp_chan *smp = container_of(work, struct smp_chan, | ||
713 | distribute_work); | ||
714 | struct smp_cmd_pairing *req, *rsp; | ||
715 | struct l2cap_conn *conn = smp->conn; | ||
716 | struct hci_conn *hcon = conn->hcon; | ||
717 | struct hci_dev *hdev = hcon->hdev; | ||
718 | __u8 *keydist; | ||
719 | |||
720 | BT_DBG("conn %p", conn); | ||
721 | |||
722 | if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) | ||
723 | return; | ||
724 | |||
725 | rsp = (void *) &smp->prsp[1]; | ||
726 | |||
727 | /* The responder sends its keys first */ | ||
728 | if (hcon->out && (smp->remote_key_dist & 0x07)) | ||
729 | return; | ||
730 | |||
731 | req = (void *) &smp->preq[1]; | ||
732 | |||
733 | if (hcon->out) { | ||
734 | keydist = &rsp->init_key_dist; | ||
735 | *keydist &= req->init_key_dist; | ||
736 | } else { | ||
737 | keydist = &rsp->resp_key_dist; | ||
738 | *keydist &= req->resp_key_dist; | ||
739 | } | ||
740 | |||
741 | BT_DBG("keydist 0x%x", *keydist); | ||
742 | |||
743 | if (*keydist & SMP_DIST_ENC_KEY) { | ||
744 | struct smp_cmd_encrypt_info enc; | ||
745 | struct smp_cmd_master_ident ident; | ||
746 | struct smp_ltk *ltk; | ||
747 | u8 authenticated; | ||
748 | __le16 ediv; | ||
749 | __le64 rand; | ||
750 | |||
751 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); | ||
752 | get_random_bytes(&ediv, sizeof(ediv)); | ||
753 | get_random_bytes(&rand, sizeof(rand)); | ||
754 | |||
755 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); | ||
756 | |||
757 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; | ||
758 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, | ||
759 | SMP_LTK_SLAVE, authenticated, enc.ltk, | ||
760 | smp->enc_key_size, ediv, rand); | ||
761 | smp->slave_ltk = ltk; | ||
762 | |||
763 | ident.ediv = ediv; | ||
764 | ident.rand = rand; | ||
765 | |||
766 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); | ||
767 | |||
768 | *keydist &= ~SMP_DIST_ENC_KEY; | ||
769 | } | ||
770 | |||
771 | if (*keydist & SMP_DIST_ID_KEY) { | ||
772 | struct smp_cmd_ident_addr_info addrinfo; | ||
773 | struct smp_cmd_ident_info idinfo; | ||
774 | |||
775 | memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk)); | ||
776 | |||
777 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); | ||
778 | |||
779 | /* The hci_conn contains the local identity address | ||
780 | * after the connection has been established. | ||
781 | * | ||
782 | * This is true even when the connection has been | ||
783 | * established using a resolvable random address. | ||
784 | */ | ||
785 | bacpy(&addrinfo.bdaddr, &hcon->src); | ||
786 | addrinfo.addr_type = hcon->src_type; | ||
787 | |||
788 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), | ||
789 | &addrinfo); | ||
790 | |||
791 | *keydist &= ~SMP_DIST_ID_KEY; | ||
792 | } | ||
793 | |||
794 | if (*keydist & SMP_DIST_SIGN) { | ||
795 | struct smp_cmd_sign_info sign; | ||
796 | struct smp_csrk *csrk; | ||
797 | |||
798 | /* Generate a new random key */ | ||
799 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); | ||
800 | |||
801 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); | ||
802 | if (csrk) { | ||
803 | csrk->master = 0x00; | ||
804 | memcpy(csrk->val, sign.csrk, sizeof(csrk->val)); | ||
805 | } | ||
806 | smp->slave_csrk = csrk; | ||
807 | |||
808 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); | ||
809 | |||
810 | *keydist &= ~SMP_DIST_SIGN; | ||
811 | } | ||
812 | |||
813 | /* If there are still keys to be received wait for them */ | ||
814 | if ((smp->remote_key_dist & 0x07)) | ||
815 | return; | ||
816 | |||
817 | clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags); | ||
818 | set_bit(SMP_FLAG_COMPLETE, &smp->flags); | ||
819 | smp_notify_keys(conn); | ||
820 | |||
821 | smp_chan_destroy(conn); | ||
822 | } | ||
823 | |||
824 | static void smp_timeout(struct work_struct *work) | ||
825 | { | ||
826 | struct smp_chan *smp = container_of(work, struct smp_chan, | ||
827 | security_timer.work); | ||
828 | struct l2cap_conn *conn = smp->conn; | ||
829 | |||
830 | BT_DBG("conn %p", conn); | ||
831 | |||
832 | l2cap_conn_shutdown(conn, ETIMEDOUT); | ||
833 | } | ||
834 | |||
577 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) | 835 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) |
578 | { | 836 | { |
837 | struct l2cap_chan *chan = conn->smp; | ||
579 | struct smp_chan *smp; | 838 | struct smp_chan *smp; |
580 | 839 | ||
581 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); | 840 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); |
@@ -593,54 +852,20 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) | |||
593 | } | 852 | } |
594 | 853 | ||
595 | smp->conn = conn; | 854 | smp->conn = conn; |
596 | conn->smp_chan = smp; | 855 | chan->data = smp; |
856 | |||
857 | INIT_WORK(&smp->distribute_work, smp_distribute_keys); | ||
858 | INIT_DELAYED_WORK(&smp->security_timer, smp_timeout); | ||
597 | 859 | ||
598 | hci_conn_hold(conn->hcon); | 860 | hci_conn_hold(conn->hcon); |
599 | 861 | ||
600 | return smp; | 862 | return smp; |
601 | } | 863 | } |
602 | 864 | ||
603 | void smp_chan_destroy(struct l2cap_conn *conn) | ||
604 | { | ||
605 | struct smp_chan *smp = conn->smp_chan; | ||
606 | bool complete; | ||
607 | |||
608 | BUG_ON(!smp); | ||
609 | |||
610 | complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); | ||
611 | mgmt_smp_complete(conn->hcon, complete); | ||
612 | |||
613 | kfree(smp->csrk); | ||
614 | kfree(smp->slave_csrk); | ||
615 | |||
616 | crypto_free_blkcipher(smp->tfm_aes); | ||
617 | |||
618 | /* If pairing failed clean up any keys we might have */ | ||
619 | if (!complete) { | ||
620 | if (smp->ltk) { | ||
621 | list_del(&smp->ltk->list); | ||
622 | kfree(smp->ltk); | ||
623 | } | ||
624 | |||
625 | if (smp->slave_ltk) { | ||
626 | list_del(&smp->slave_ltk->list); | ||
627 | kfree(smp->slave_ltk); | ||
628 | } | ||
629 | |||
630 | if (smp->remote_irk) { | ||
631 | list_del(&smp->remote_irk->list); | ||
632 | kfree(smp->remote_irk); | ||
633 | } | ||
634 | } | ||
635 | |||
636 | kfree(smp); | ||
637 | conn->smp_chan = NULL; | ||
638 | hci_conn_drop(conn->hcon); | ||
639 | } | ||
640 | |||
641 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) | 865 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
642 | { | 866 | { |
643 | struct l2cap_conn *conn = hcon->l2cap_data; | 867 | struct l2cap_conn *conn = hcon->l2cap_data; |
868 | struct l2cap_chan *chan; | ||
644 | struct smp_chan *smp; | 869 | struct smp_chan *smp; |
645 | u32 value; | 870 | u32 value; |
646 | 871 | ||
@@ -649,7 +874,11 @@ int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) | |||
649 | if (!conn || !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) | 874 | if (!conn || !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
650 | return -ENOTCONN; | 875 | return -ENOTCONN; |
651 | 876 | ||
652 | smp = conn->smp_chan; | 877 | chan = conn->smp; |
878 | if (!chan) | ||
879 | return -ENOTCONN; | ||
880 | |||
881 | smp = chan->data; | ||
653 | 882 | ||
654 | switch (mgmt_op) { | 883 | switch (mgmt_op) { |
655 | case MGMT_OP_USER_PASSKEY_REPLY: | 884 | case MGMT_OP_USER_PASSKEY_REPLY: |
@@ -696,10 +925,12 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) | |||
696 | if (conn->hcon->role != HCI_ROLE_SLAVE) | 925 | if (conn->hcon->role != HCI_ROLE_SLAVE) |
697 | return SMP_CMD_NOTSUPP; | 926 | return SMP_CMD_NOTSUPP; |
698 | 927 | ||
699 | if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) | 928 | if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) { |
700 | smp = smp_chan_create(conn); | 929 | smp = smp_chan_create(conn); |
701 | else | 930 | } else { |
702 | smp = conn->smp_chan; | 931 | struct l2cap_chan *chan = conn->smp; |
932 | smp = chan->data; | ||
933 | } | ||
703 | 934 | ||
704 | if (!smp) | 935 | if (!smp) |
705 | return SMP_UNSPECIFIED; | 936 | return SMP_UNSPECIFIED; |
@@ -753,7 +984,8 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) | |||
753 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) | 984 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) |
754 | { | 985 | { |
755 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; | 986 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
756 | struct smp_chan *smp = conn->smp_chan; | 987 | struct l2cap_chan *chan = conn->smp; |
988 | struct smp_chan *smp = chan->data; | ||
757 | u8 key_size, auth = SMP_AUTH_NONE; | 989 | u8 key_size, auth = SMP_AUTH_NONE; |
758 | int ret; | 990 | int ret; |
759 | 991 | ||
@@ -814,7 +1046,8 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) | |||
814 | 1046 | ||
815 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) | 1047 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) |
816 | { | 1048 | { |
817 | struct smp_chan *smp = conn->smp_chan; | 1049 | struct l2cap_chan *chan = conn->smp; |
1050 | struct smp_chan *smp = chan->data; | ||
818 | 1051 | ||
819 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); | 1052 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
820 | 1053 | ||
@@ -837,7 +1070,8 @@ static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) | |||
837 | 1070 | ||
838 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) | 1071 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) |
839 | { | 1072 | { |
840 | struct smp_chan *smp = conn->smp_chan; | 1073 | struct l2cap_chan *chan = conn->smp; |
1074 | struct smp_chan *smp = chan->data; | ||
841 | 1075 | ||
842 | BT_DBG("conn %p", conn); | 1076 | BT_DBG("conn %p", conn); |
843 | 1077 | ||
@@ -1010,7 +1244,8 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) | |||
1010 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) | 1244 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1011 | { | 1245 | { |
1012 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; | 1246 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
1013 | struct smp_chan *smp = conn->smp_chan; | 1247 | struct l2cap_chan *chan = conn->smp; |
1248 | struct smp_chan *smp = chan->data; | ||
1014 | 1249 | ||
1015 | BT_DBG("conn %p", conn); | 1250 | BT_DBG("conn %p", conn); |
1016 | 1251 | ||
@@ -1031,7 +1266,8 @@ static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) | |||
1031 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) | 1266 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) |
1032 | { | 1267 | { |
1033 | struct smp_cmd_master_ident *rp = (void *) skb->data; | 1268 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
1034 | struct smp_chan *smp = conn->smp_chan; | 1269 | struct l2cap_chan *chan = conn->smp; |
1270 | struct smp_chan *smp = chan->data; | ||
1035 | struct hci_dev *hdev = conn->hcon->hdev; | 1271 | struct hci_dev *hdev = conn->hcon->hdev; |
1036 | struct hci_conn *hcon = conn->hcon; | 1272 | struct hci_conn *hcon = conn->hcon; |
1037 | struct smp_ltk *ltk; | 1273 | struct smp_ltk *ltk; |
@@ -1058,7 +1294,7 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) | |||
1058 | rp->ediv, rp->rand); | 1294 | rp->ediv, rp->rand); |
1059 | smp->ltk = ltk; | 1295 | smp->ltk = ltk; |
1060 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) | 1296 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) |
1061 | smp_distribute_keys(conn); | 1297 | queue_work(hdev->workqueue, &smp->distribute_work); |
1062 | hci_dev_unlock(hdev); | 1298 | hci_dev_unlock(hdev); |
1063 | 1299 | ||
1064 | return 0; | 1300 | return 0; |
@@ -1067,7 +1303,8 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) | |||
1067 | static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) | 1303 | static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1068 | { | 1304 | { |
1069 | struct smp_cmd_ident_info *info = (void *) skb->data; | 1305 | struct smp_cmd_ident_info *info = (void *) skb->data; |
1070 | struct smp_chan *smp = conn->smp_chan; | 1306 | struct l2cap_chan *chan = conn->smp; |
1307 | struct smp_chan *smp = chan->data; | ||
1071 | 1308 | ||
1072 | BT_DBG(""); | 1309 | BT_DBG(""); |
1073 | 1310 | ||
@@ -1089,8 +1326,10 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, | |||
1089 | struct sk_buff *skb) | 1326 | struct sk_buff *skb) |
1090 | { | 1327 | { |
1091 | struct smp_cmd_ident_addr_info *info = (void *) skb->data; | 1328 | struct smp_cmd_ident_addr_info *info = (void *) skb->data; |
1092 | struct smp_chan *smp = conn->smp_chan; | 1329 | struct l2cap_chan *chan = conn->smp; |
1330 | struct smp_chan *smp = chan->data; | ||
1093 | struct hci_conn *hcon = conn->hcon; | 1331 | struct hci_conn *hcon = conn->hcon; |
1332 | struct hci_dev *hdev = hcon->hdev; | ||
1094 | bdaddr_t rpa; | 1333 | bdaddr_t rpa; |
1095 | 1334 | ||
1096 | BT_DBG(""); | 1335 | BT_DBG(""); |
@@ -1133,7 +1372,7 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, | |||
1133 | smp->id_addr_type, smp->irk, &rpa); | 1372 | smp->id_addr_type, smp->irk, &rpa); |
1134 | 1373 | ||
1135 | distribute: | 1374 | distribute: |
1136 | smp_distribute_keys(conn); | 1375 | queue_work(hdev->workqueue, &smp->distribute_work); |
1137 | 1376 | ||
1138 | hci_dev_unlock(hcon->hdev); | 1377 | hci_dev_unlock(hcon->hdev); |
1139 | 1378 | ||
@@ -1143,7 +1382,8 @@ distribute: | |||
1143 | static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) | 1382 | static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1144 | { | 1383 | { |
1145 | struct smp_cmd_sign_info *rp = (void *) skb->data; | 1384 | struct smp_cmd_sign_info *rp = (void *) skb->data; |
1146 | struct smp_chan *smp = conn->smp_chan; | 1385 | struct l2cap_chan *chan = conn->smp; |
1386 | struct smp_chan *smp = chan->data; | ||
1147 | struct hci_dev *hdev = conn->hcon->hdev; | 1387 | struct hci_dev *hdev = conn->hcon->hdev; |
1148 | struct smp_csrk *csrk; | 1388 | struct smp_csrk *csrk; |
1149 | 1389 | ||
@@ -1168,15 +1408,15 @@ static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) | |||
1168 | memcpy(csrk->val, rp->csrk, sizeof(csrk->val)); | 1408 | memcpy(csrk->val, rp->csrk, sizeof(csrk->val)); |
1169 | } | 1409 | } |
1170 | smp->csrk = csrk; | 1410 | smp->csrk = csrk; |
1171 | if (!(smp->remote_key_dist & SMP_DIST_SIGN)) | 1411 | queue_work(hdev->workqueue, &smp->distribute_work); |
1172 | smp_distribute_keys(conn); | ||
1173 | hci_dev_unlock(hdev); | 1412 | hci_dev_unlock(hdev); |
1174 | 1413 | ||
1175 | return 0; | 1414 | return 0; |
1176 | } | 1415 | } |
1177 | 1416 | ||
1178 | int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) | 1417 | static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb) |
1179 | { | 1418 | { |
1419 | struct l2cap_conn *conn = chan->conn; | ||
1180 | struct hci_conn *hcon = conn->hcon; | 1420 | struct hci_conn *hcon = conn->hcon; |
1181 | __u8 code, reason; | 1421 | __u8 code, reason; |
1182 | int err = 0; | 1422 | int err = 0; |
@@ -1186,10 +1426,8 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) | |||
1186 | return 0; | 1426 | return 0; |
1187 | } | 1427 | } |
1188 | 1428 | ||
1189 | if (skb->len < 1) { | 1429 | if (skb->len < 1) |
1190 | kfree_skb(skb); | ||
1191 | return -EILSEQ; | 1430 | return -EILSEQ; |
1192 | } | ||
1193 | 1431 | ||
1194 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { | 1432 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { |
1195 | err = -EOPNOTSUPP; | 1433 | err = -EOPNOTSUPP; |
@@ -1207,10 +1445,11 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) | |||
1207 | * returns an error). | 1445 | * returns an error). |
1208 | */ | 1446 | */ |
1209 | if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ && | 1447 | if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ && |
1210 | !conn->smp_chan) { | 1448 | !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { |
1211 | BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code); | 1449 | BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code); |
1212 | kfree_skb(skb); | 1450 | reason = SMP_CMD_NOTSUPP; |
1213 | return -EOPNOTSUPP; | 1451 | err = -EOPNOTSUPP; |
1452 | goto done; | ||
1214 | } | 1453 | } |
1215 | 1454 | ||
1216 | switch (code) { | 1455 | switch (code) { |
@@ -1271,188 +1510,201 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) | |||
1271 | done: | 1510 | done: |
1272 | if (reason) | 1511 | if (reason) |
1273 | smp_failure(conn, reason); | 1512 | smp_failure(conn, reason); |
1274 | 1513 | if (!err) | |
1275 | kfree_skb(skb); | 1514 | kfree_skb(skb); |
1276 | return err; | 1515 | return err; |
1277 | } | 1516 | } |
1278 | 1517 | ||
1279 | static void smp_notify_keys(struct l2cap_conn *conn) | 1518 | static void smp_teardown_cb(struct l2cap_chan *chan, int err) |
1280 | { | 1519 | { |
1281 | struct smp_chan *smp = conn->smp_chan; | 1520 | struct l2cap_conn *conn = chan->conn; |
1521 | |||
1522 | BT_DBG("chan %p", chan); | ||
1523 | |||
1524 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) | ||
1525 | smp_chan_destroy(conn); | ||
1526 | |||
1527 | conn->smp = NULL; | ||
1528 | l2cap_chan_put(chan); | ||
1529 | } | ||
1530 | |||
1531 | static void smp_resume_cb(struct l2cap_chan *chan) | ||
1532 | { | ||
1533 | struct smp_chan *smp = chan->data; | ||
1534 | struct l2cap_conn *conn = chan->conn; | ||
1282 | struct hci_conn *hcon = conn->hcon; | 1535 | struct hci_conn *hcon = conn->hcon; |
1283 | struct hci_dev *hdev = hcon->hdev; | 1536 | struct hci_dev *hdev = hcon->hdev; |
1284 | struct smp_cmd_pairing *req = (void *) &smp->preq[1]; | ||
1285 | struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1]; | ||
1286 | bool persistent; | ||
1287 | 1537 | ||
1288 | if (smp->remote_irk) { | 1538 | BT_DBG("chan %p", chan); |
1289 | mgmt_new_irk(hdev, smp->remote_irk); | ||
1290 | /* Now that user space can be considered to know the | ||
1291 | * identity address track the connection based on it | ||
1292 | * from now on. | ||
1293 | */ | ||
1294 | bacpy(&hcon->dst, &smp->remote_irk->bdaddr); | ||
1295 | hcon->dst_type = smp->remote_irk->addr_type; | ||
1296 | l2cap_conn_update_id_addr(hcon); | ||
1297 | 1539 | ||
1298 | /* When receiving an indentity resolving key for | 1540 | if (!smp) |
1299 | * a remote device that does not use a resolvable | 1541 | return; |
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 | } | ||
1313 | } | ||
1314 | 1542 | ||
1315 | /* The LTKs and CSRKs should be persistent only if both sides | 1543 | cancel_delayed_work(&smp->security_timer); |
1316 | * had the bonding bit set in their authentication requests. | ||
1317 | */ | ||
1318 | persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING); | ||
1319 | 1544 | ||
1320 | if (smp->csrk) { | 1545 | if (test_bit(HCI_CONN_ENCRYPT, &hcon->flags)) |
1321 | smp->csrk->bdaddr_type = hcon->dst_type; | 1546 | queue_work(hdev->workqueue, &smp->distribute_work); |
1322 | bacpy(&smp->csrk->bdaddr, &hcon->dst); | 1547 | } |
1323 | mgmt_new_csrk(hdev, smp->csrk, persistent); | ||
1324 | } | ||
1325 | 1548 | ||
1326 | if (smp->slave_csrk) { | 1549 | static void smp_ready_cb(struct l2cap_chan *chan) |
1327 | smp->slave_csrk->bdaddr_type = hcon->dst_type; | 1550 | { |
1328 | bacpy(&smp->slave_csrk->bdaddr, &hcon->dst); | 1551 | struct l2cap_conn *conn = chan->conn; |
1329 | mgmt_new_csrk(hdev, smp->slave_csrk, persistent); | ||
1330 | } | ||
1331 | 1552 | ||
1332 | if (smp->ltk) { | 1553 | BT_DBG("chan %p", chan); |
1333 | smp->ltk->bdaddr_type = hcon->dst_type; | ||
1334 | bacpy(&smp->ltk->bdaddr, &hcon->dst); | ||
1335 | mgmt_new_ltk(hdev, smp->ltk, persistent); | ||
1336 | } | ||
1337 | 1554 | ||
1338 | if (smp->slave_ltk) { | 1555 | conn->smp = chan; |
1339 | smp->slave_ltk->bdaddr_type = hcon->dst_type; | 1556 | l2cap_chan_hold(chan); |
1340 | bacpy(&smp->slave_ltk->bdaddr, &hcon->dst); | ||
1341 | mgmt_new_ltk(hdev, smp->slave_ltk, persistent); | ||
1342 | } | ||
1343 | } | 1557 | } |
1344 | 1558 | ||
1345 | int smp_distribute_keys(struct l2cap_conn *conn) | 1559 | static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
1346 | { | 1560 | { |
1347 | struct smp_cmd_pairing *req, *rsp; | 1561 | int err; |
1348 | struct smp_chan *smp = conn->smp_chan; | ||
1349 | struct hci_conn *hcon = conn->hcon; | ||
1350 | struct hci_dev *hdev = hcon->hdev; | ||
1351 | __u8 *keydist; | ||
1352 | 1562 | ||
1353 | BT_DBG("conn %p", conn); | 1563 | BT_DBG("chan %p", chan); |
1354 | 1564 | ||
1355 | if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) | 1565 | err = smp_sig_channel(chan, skb); |
1356 | return 0; | 1566 | if (err) { |
1567 | struct smp_chan *smp = chan->data; | ||
1357 | 1568 | ||
1358 | rsp = (void *) &smp->prsp[1]; | 1569 | if (smp) |
1570 | cancel_delayed_work_sync(&smp->security_timer); | ||
1359 | 1571 | ||
1360 | /* The responder sends its keys first */ | 1572 | l2cap_conn_shutdown(chan->conn, -err); |
1361 | if (hcon->out && (smp->remote_key_dist & 0x07)) | 1573 | } |
1362 | return 0; | ||
1363 | 1574 | ||
1364 | req = (void *) &smp->preq[1]; | 1575 | return err; |
1576 | } | ||
1365 | 1577 | ||
1366 | if (hcon->out) { | 1578 | static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan, |
1367 | keydist = &rsp->init_key_dist; | 1579 | unsigned long hdr_len, |
1368 | *keydist &= req->init_key_dist; | 1580 | unsigned long len, int nb) |
1369 | } else { | 1581 | { |
1370 | keydist = &rsp->resp_key_dist; | 1582 | struct sk_buff *skb; |
1371 | *keydist &= req->resp_key_dist; | ||
1372 | } | ||
1373 | 1583 | ||
1374 | BT_DBG("keydist 0x%x", *keydist); | 1584 | skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL); |
1585 | if (!skb) | ||
1586 | return ERR_PTR(-ENOMEM); | ||
1375 | 1587 | ||
1376 | if (*keydist & SMP_DIST_ENC_KEY) { | 1588 | skb->priority = HCI_PRIO_MAX; |
1377 | struct smp_cmd_encrypt_info enc; | 1589 | bt_cb(skb)->chan = chan; |
1378 | struct smp_cmd_master_ident ident; | ||
1379 | struct smp_ltk *ltk; | ||
1380 | u8 authenticated; | ||
1381 | __le16 ediv; | ||
1382 | __le64 rand; | ||
1383 | 1590 | ||
1384 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); | 1591 | return skb; |
1385 | get_random_bytes(&ediv, sizeof(ediv)); | 1592 | } |
1386 | get_random_bytes(&rand, sizeof(rand)); | ||
1387 | 1593 | ||
1388 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); | 1594 | static const struct l2cap_ops smp_chan_ops = { |
1595 | .name = "Security Manager", | ||
1596 | .ready = smp_ready_cb, | ||
1597 | .recv = smp_recv_cb, | ||
1598 | .alloc_skb = smp_alloc_skb_cb, | ||
1599 | .teardown = smp_teardown_cb, | ||
1600 | .resume = smp_resume_cb, | ||
1601 | |||
1602 | .new_connection = l2cap_chan_no_new_connection, | ||
1603 | .state_change = l2cap_chan_no_state_change, | ||
1604 | .close = l2cap_chan_no_close, | ||
1605 | .defer = l2cap_chan_no_defer, | ||
1606 | .suspend = l2cap_chan_no_suspend, | ||
1607 | .set_shutdown = l2cap_chan_no_set_shutdown, | ||
1608 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, | ||
1609 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, | ||
1610 | }; | ||
1389 | 1611 | ||
1390 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; | 1612 | static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan) |
1391 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, | 1613 | { |
1392 | SMP_LTK_SLAVE, authenticated, enc.ltk, | 1614 | struct l2cap_chan *chan; |
1393 | smp->enc_key_size, ediv, rand); | ||
1394 | smp->slave_ltk = ltk; | ||
1395 | 1615 | ||
1396 | ident.ediv = ediv; | 1616 | BT_DBG("pchan %p", pchan); |
1397 | ident.rand = rand; | ||
1398 | 1617 | ||
1399 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); | 1618 | chan = l2cap_chan_create(); |
1619 | if (!chan) | ||
1620 | return NULL; | ||
1400 | 1621 | ||
1401 | *keydist &= ~SMP_DIST_ENC_KEY; | 1622 | chan->chan_type = pchan->chan_type; |
1402 | } | 1623 | chan->ops = &smp_chan_ops; |
1624 | chan->scid = pchan->scid; | ||
1625 | chan->dcid = chan->scid; | ||
1626 | chan->imtu = pchan->imtu; | ||
1627 | chan->omtu = pchan->omtu; | ||
1628 | chan->mode = pchan->mode; | ||
1403 | 1629 | ||
1404 | if (*keydist & SMP_DIST_ID_KEY) { | 1630 | BT_DBG("created chan %p", chan); |
1405 | struct smp_cmd_ident_addr_info addrinfo; | ||
1406 | struct smp_cmd_ident_info idinfo; | ||
1407 | 1631 | ||
1408 | memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk)); | 1632 | return chan; |
1633 | } | ||
1409 | 1634 | ||
1410 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); | 1635 | static const struct l2cap_ops smp_root_chan_ops = { |
1636 | .name = "Security Manager Root", | ||
1637 | .new_connection = smp_new_conn_cb, | ||
1638 | |||
1639 | /* None of these are implemented for the root channel */ | ||
1640 | .close = l2cap_chan_no_close, | ||
1641 | .alloc_skb = l2cap_chan_no_alloc_skb, | ||
1642 | .recv = l2cap_chan_no_recv, | ||
1643 | .state_change = l2cap_chan_no_state_change, | ||
1644 | .teardown = l2cap_chan_no_teardown, | ||
1645 | .ready = l2cap_chan_no_ready, | ||
1646 | .defer = l2cap_chan_no_defer, | ||
1647 | .suspend = l2cap_chan_no_suspend, | ||
1648 | .resume = l2cap_chan_no_resume, | ||
1649 | .set_shutdown = l2cap_chan_no_set_shutdown, | ||
1650 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, | ||
1651 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, | ||
1652 | }; | ||
1411 | 1653 | ||
1412 | /* The hci_conn contains the local identity address | 1654 | int smp_register(struct hci_dev *hdev) |
1413 | * after the connection has been established. | 1655 | { |
1414 | * | 1656 | struct l2cap_chan *chan; |
1415 | * This is true even when the connection has been | 1657 | struct crypto_blkcipher *tfm_aes; |
1416 | * established using a resolvable random address. | ||
1417 | */ | ||
1418 | bacpy(&addrinfo.bdaddr, &hcon->src); | ||
1419 | addrinfo.addr_type = hcon->src_type; | ||
1420 | 1658 | ||
1421 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), | 1659 | BT_DBG("%s", hdev->name); |
1422 | &addrinfo); | ||
1423 | 1660 | ||
1424 | *keydist &= ~SMP_DIST_ID_KEY; | 1661 | tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); |
1662 | if (IS_ERR(tfm_aes)) { | ||
1663 | int err = PTR_ERR(tfm_aes); | ||
1664 | BT_ERR("Unable to create crypto context"); | ||
1665 | return err; | ||
1425 | } | 1666 | } |
1426 | 1667 | ||
1427 | if (*keydist & SMP_DIST_SIGN) { | 1668 | chan = l2cap_chan_create(); |
1428 | struct smp_cmd_sign_info sign; | 1669 | if (!chan) { |
1429 | struct smp_csrk *csrk; | 1670 | crypto_free_blkcipher(tfm_aes); |
1671 | return -ENOMEM; | ||
1672 | } | ||
1430 | 1673 | ||
1431 | /* Generate a new random key */ | 1674 | chan->data = tfm_aes; |
1432 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); | ||
1433 | 1675 | ||
1434 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); | 1676 | l2cap_add_scid(chan, L2CAP_CID_SMP); |
1435 | if (csrk) { | ||
1436 | csrk->master = 0x00; | ||
1437 | memcpy(csrk->val, sign.csrk, sizeof(csrk->val)); | ||
1438 | } | ||
1439 | smp->slave_csrk = csrk; | ||
1440 | 1677 | ||
1441 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); | 1678 | l2cap_chan_set_defaults(chan); |
1442 | 1679 | ||
1443 | *keydist &= ~SMP_DIST_SIGN; | 1680 | bacpy(&chan->src, &hdev->bdaddr); |
1444 | } | 1681 | chan->src_type = BDADDR_LE_PUBLIC; |
1682 | chan->state = BT_LISTEN; | ||
1683 | chan->mode = L2CAP_MODE_BASIC; | ||
1684 | chan->imtu = L2CAP_DEFAULT_MTU; | ||
1685 | chan->ops = &smp_root_chan_ops; | ||
1445 | 1686 | ||
1446 | /* If there are still keys to be received wait for them */ | 1687 | hdev->smp_data = chan; |
1447 | if ((smp->remote_key_dist & 0x07)) | ||
1448 | return 0; | ||
1449 | 1688 | ||
1450 | clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags); | 1689 | return 0; |
1451 | cancel_delayed_work_sync(&conn->security_timer); | 1690 | } |
1452 | set_bit(SMP_FLAG_COMPLETE, &smp->flags); | ||
1453 | smp_notify_keys(conn); | ||
1454 | 1691 | ||
1455 | smp_chan_destroy(conn); | 1692 | void smp_unregister(struct hci_dev *hdev) |
1693 | { | ||
1694 | struct l2cap_chan *chan = hdev->smp_data; | ||
1695 | struct crypto_blkcipher *tfm_aes; | ||
1456 | 1696 | ||
1457 | return 0; | 1697 | if (!chan) |
1698 | return; | ||
1699 | |||
1700 | BT_DBG("%s chan %p", hdev->name, chan); | ||
1701 | |||
1702 | tfm_aes = chan->data; | ||
1703 | if (tfm_aes) { | ||
1704 | chan->data = NULL; | ||
1705 | crypto_free_blkcipher(tfm_aes); | ||
1706 | } | ||
1707 | |||
1708 | hdev->smp_data = NULL; | ||
1709 | l2cap_chan_put(chan); | ||
1458 | } | 1710 | } |