2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
23 #include <linux/crypto.h>
24 #include <linux/scatterlist.h>
25 #include <crypto/b128ops.h>
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
29 #include <net/bluetooth/l2cap.h>
30 #include <net/bluetooth/mgmt.h>
34 #define SMP_TIMEOUT msecs_to_jiffies(30000)
36 #define AUTH_REQ_MASK 0x07
38 #define SMP_FLAG_TK_VALID 1
39 #define SMP_FLAG_CFM_PENDING 2
40 #define SMP_FLAG_MITM_AUTH 3
41 #define SMP_FLAG_COMPLETE 4
42 #define SMP_FLAG_INITIATOR 5
45 struct l2cap_conn *conn;
46 u8 preq[7]; /* SMP Pairing Request */
47 u8 prsp[7]; /* SMP Pairing Response */
48 u8 prnd[16]; /* SMP Pairing Random (local) */
49 u8 rrnd[16]; /* SMP Pairing Random (remote) */
50 u8 pcnf[16]; /* SMP Pairing Confirm */
51 u8 tk[16]; /* SMP Temporary Key */
57 struct smp_csrk *csrk;
58 struct smp_csrk *slave_csrk;
60 struct smp_ltk *slave_ltk;
61 struct smp_irk *remote_irk;
62 unsigned long smp_flags;
63 struct work_struct confirm;
64 struct work_struct random;
67 static inline void swap128(const u8 src[16], u8 dst[16])
70 for (i = 0; i < 16; i++)
74 static inline void swap56(const u8 src[7], u8 dst[7])
77 for (i = 0; i < 7; i++)
81 static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
83 struct blkcipher_desc desc;
84 struct scatterlist sg;
85 uint8_t tmp[16], data[16];
89 BT_ERR("tfm %p", tfm);
96 /* The most significant octet of key corresponds to k[0] */
99 err = crypto_blkcipher_setkey(tfm, tmp, 16);
101 BT_ERR("cipher setkey failed: %d", err);
105 /* Most significant octet of plaintextData corresponds to data[0] */
108 sg_init_one(&sg, data, 16);
110 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
112 BT_ERR("Encrypt data error %d", err);
114 /* Most significant octet of encryptedData corresponds to data[0] */
120 static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
125 /* r' = padding || r */
127 memset(_res + 3, 0, 13);
129 err = smp_e(tfm, irk, _res);
131 BT_ERR("Encrypt error");
135 /* The output of the random address function ah is:
136 * ah(h, r) = e(k, r') mod 2^24
137 * The output of the security function e is then truncated to 24 bits
138 * by taking the least significant 24 bits of the output of e as the
141 memcpy(res, _res, 3);
146 bool smp_irk_matches(struct crypto_blkcipher *tfm, u8 irk[16],
152 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
154 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
158 return !memcmp(bdaddr->b, hash, 3);
161 int smp_generate_rpa(struct crypto_blkcipher *tfm, u8 irk[16], bdaddr_t *rpa)
165 get_random_bytes(&rpa->b[3], 3);
167 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
168 rpa->b[5] |= 0x40; /* Set second most significant bit */
170 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
174 BT_DBG("RPA %pMR", rpa);
179 static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],
180 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia,
181 u8 _rat, bdaddr_t *ra, u8 res[16])
188 /* p1 = pres || preq || _rat || _iat */
191 memcpy(p1 + 2, preq, 7);
192 memcpy(p1 + 9, pres, 7);
194 /* p2 = padding || ia || ra */
196 memcpy(p2 + 6, ia, 6);
197 memset(p2 + 12, 0, 4);
200 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
202 /* res = e(k, res) */
203 err = smp_e(tfm, k, res);
205 BT_ERR("Encrypt data error");
209 /* res = res XOR p2 */
210 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
212 /* res = e(k, res) */
213 err = smp_e(tfm, k, res);
215 BT_ERR("Encrypt data error");
220 static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], u8 r1[16],
221 u8 r2[16], u8 _r[16])
225 /* Just least significant octets from r1 and r2 are considered */
227 memcpy(_r + 8, r1, 8);
229 err = smp_e(tfm, k, _r);
231 BT_ERR("Encrypt data error");
236 static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code,
237 u16 dlen, void *data)
240 struct l2cap_hdr *lh;
243 len = L2CAP_HDR_SIZE + sizeof(code) + dlen;
248 skb = bt_skb_alloc(len, GFP_ATOMIC);
252 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
253 lh->len = cpu_to_le16(sizeof(code) + dlen);
254 lh->cid = cpu_to_le16(L2CAP_CID_SMP);
256 memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code));
258 memcpy(skb_put(skb, dlen), data, dlen);
263 static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
265 struct sk_buff *skb = smp_build_cmd(conn, code, len, data);
267 BT_DBG("code 0x%2.2x", code);
272 skb->priority = HCI_PRIO_MAX;
273 hci_send_acl(conn->hchan, skb, 0);
275 cancel_delayed_work_sync(&conn->security_timer);
276 schedule_delayed_work(&conn->security_timer, SMP_TIMEOUT);
279 static __u8 authreq_to_seclevel(__u8 authreq)
281 if (authreq & SMP_AUTH_MITM)
282 return BT_SECURITY_HIGH;
284 return BT_SECURITY_MEDIUM;
287 static __u8 seclevel_to_authreq(__u8 sec_level)
290 case BT_SECURITY_HIGH:
291 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
292 case BT_SECURITY_MEDIUM:
293 return SMP_AUTH_BONDING;
295 return SMP_AUTH_NONE;
299 static void build_pairing_cmd(struct l2cap_conn *conn,
300 struct smp_cmd_pairing *req,
301 struct smp_cmd_pairing *rsp, __u8 authreq)
303 struct smp_chan *smp = conn->smp_chan;
304 struct hci_conn *hcon = conn->hcon;
305 struct hci_dev *hdev = hcon->hdev;
306 u8 local_dist = 0, remote_dist = 0;
308 if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->dev_flags)) {
309 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
310 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
311 authreq |= SMP_AUTH_BONDING;
313 authreq &= ~SMP_AUTH_BONDING;
316 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
317 remote_dist |= SMP_DIST_ID_KEY;
319 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
320 local_dist |= SMP_DIST_ID_KEY;
323 req->io_capability = conn->hcon->io_capability;
324 req->oob_flag = SMP_OOB_NOT_PRESENT;
325 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
326 req->init_key_dist = local_dist;
327 req->resp_key_dist = remote_dist;
328 req->auth_req = (authreq & AUTH_REQ_MASK);
330 smp->remote_key_dist = remote_dist;
334 rsp->io_capability = conn->hcon->io_capability;
335 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
336 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
337 rsp->init_key_dist = req->init_key_dist & remote_dist;
338 rsp->resp_key_dist = req->resp_key_dist & local_dist;
339 rsp->auth_req = (authreq & AUTH_REQ_MASK);
341 smp->remote_key_dist = rsp->init_key_dist;
344 static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
346 struct smp_chan *smp = conn->smp_chan;
348 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
349 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
350 return SMP_ENC_KEY_SIZE;
352 smp->enc_key_size = max_key_size;
357 static void smp_failure(struct l2cap_conn *conn, u8 reason)
359 struct hci_conn *hcon = conn->hcon;
362 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
365 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
366 mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type,
367 HCI_ERROR_AUTH_FAILURE);
369 cancel_delayed_work_sync(&conn->security_timer);
371 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
372 smp_chan_destroy(conn);
375 #define JUST_WORKS 0x00
376 #define JUST_CFM 0x01
377 #define REQ_PASSKEY 0x02
378 #define CFM_PASSKEY 0x03
382 static const u8 gen_method[5][5] = {
383 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
384 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
385 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
386 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
387 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
390 static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
391 u8 local_io, u8 remote_io)
393 struct hci_conn *hcon = conn->hcon;
394 struct smp_chan *smp = conn->smp_chan;
399 /* Initialize key for JUST WORKS */
400 memset(smp->tk, 0, sizeof(smp->tk));
401 clear_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
403 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
405 /* If neither side wants MITM, use JUST WORKS */
406 /* If either side has unknown io_caps, use JUST WORKS */
407 /* Otherwise, look up method from the table */
408 if (!(auth & SMP_AUTH_MITM) ||
409 local_io > SMP_IO_KEYBOARD_DISPLAY ||
410 remote_io > SMP_IO_KEYBOARD_DISPLAY)
413 method = gen_method[remote_io][local_io];
415 /* If not bonding, don't ask user to confirm a Zero TK */
416 if (!(auth & SMP_AUTH_BONDING) && method == JUST_CFM)
419 /* Don't confirm locally initiated pairing attempts */
420 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
424 /* If Just Works, Continue with Zero TK */
425 if (method == JUST_WORKS) {
426 set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
430 /* Not Just Works/Confirm results in MITM Authentication */
431 if (method != JUST_CFM)
432 set_bit(SMP_FLAG_MITM_AUTH, &smp->smp_flags);
434 /* If both devices have Keyoard-Display I/O, the master
435 * Confirms and the slave Enters the passkey.
437 if (method == OVERLAP) {
438 if (hcon->link_mode & HCI_LM_MASTER)
439 method = CFM_PASSKEY;
441 method = REQ_PASSKEY;
444 /* Generate random passkey. */
445 if (method == CFM_PASSKEY) {
446 memset(smp->tk, 0, sizeof(smp->tk));
447 get_random_bytes(&passkey, sizeof(passkey));
449 put_unaligned_le32(passkey, smp->tk);
450 BT_DBG("PassKey: %d", passkey);
451 set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
454 hci_dev_lock(hcon->hdev);
456 if (method == REQ_PASSKEY)
457 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
458 hcon->type, hcon->dst_type);
459 else if (method == JUST_CFM)
460 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
461 hcon->type, hcon->dst_type,
464 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
465 hcon->type, hcon->dst_type,
468 hci_dev_unlock(hcon->hdev);
473 static void confirm_work(struct work_struct *work)
475 struct smp_chan *smp = container_of(work, struct smp_chan, confirm);
476 struct l2cap_conn *conn = smp->conn;
477 struct hci_dev *hdev = conn->hcon->hdev;
478 struct crypto_blkcipher *tfm = hdev->tfm_aes;
479 struct smp_cmd_pairing_confirm cp;
483 BT_DBG("conn %p", conn);
485 /* Prevent mutual access to hdev->tfm_aes */
488 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,
489 conn->hcon->init_addr_type, &conn->hcon->init_addr,
490 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
493 hci_dev_unlock(hdev);
496 reason = SMP_UNSPECIFIED;
500 clear_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
502 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
507 smp_failure(conn, reason);
510 static void random_work(struct work_struct *work)
512 struct smp_chan *smp = container_of(work, struct smp_chan, random);
513 struct l2cap_conn *conn = smp->conn;
514 struct hci_conn *hcon = conn->hcon;
515 struct hci_dev *hdev = hcon->hdev;
516 struct crypto_blkcipher *tfm = hdev->tfm_aes;
517 u8 reason, confirm[16];
520 if (IS_ERR_OR_NULL(tfm)) {
521 reason = SMP_UNSPECIFIED;
525 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
527 /* Prevent mutual access to hdev->tfm_aes */
530 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,
531 hcon->init_addr_type, &hcon->init_addr,
532 hcon->resp_addr_type, &hcon->resp_addr, confirm);
534 hci_dev_unlock(hdev);
537 reason = SMP_UNSPECIFIED;
541 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
542 BT_ERR("Pairing failed (confirmation values mismatch)");
543 reason = SMP_CONFIRM_FAILED;
552 smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, stk);
554 memset(stk + smp->enc_key_size, 0,
555 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
557 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) {
558 reason = SMP_UNSPECIFIED;
562 hci_le_start_enc(hcon, ediv, rand, stk);
563 hcon->enc_key_size = smp->enc_key_size;
569 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
572 smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, stk);
574 memset(stk + smp->enc_key_size, 0,
575 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
577 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
578 HCI_SMP_STK_SLAVE, 0, stk, smp->enc_key_size,
585 smp_failure(conn, reason);
588 static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
590 struct smp_chan *smp;
592 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
596 INIT_WORK(&smp->confirm, confirm_work);
597 INIT_WORK(&smp->random, random_work);
600 conn->smp_chan = smp;
601 conn->hcon->smp_conn = conn;
603 hci_conn_hold(conn->hcon);
608 void smp_chan_destroy(struct l2cap_conn *conn)
610 struct smp_chan *smp = conn->smp_chan;
615 complete = test_bit(SMP_FLAG_COMPLETE, &smp->smp_flags);
616 mgmt_smp_complete(conn->hcon, complete);
619 kfree(smp->slave_csrk);
621 /* If pairing failed clean up any keys we might have */
624 list_del(&smp->ltk->list);
628 if (smp->slave_ltk) {
629 list_del(&smp->slave_ltk->list);
630 kfree(smp->slave_ltk);
633 if (smp->remote_irk) {
634 list_del(&smp->remote_irk->list);
635 kfree(smp->remote_irk);
640 conn->smp_chan = NULL;
641 conn->hcon->smp_conn = NULL;
642 hci_conn_drop(conn->hcon);
645 int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
647 struct l2cap_conn *conn = hcon->smp_conn;
648 struct smp_chan *smp;
656 smp = conn->smp_chan;
659 case MGMT_OP_USER_PASSKEY_REPLY:
660 value = le32_to_cpu(passkey);
661 memset(smp->tk, 0, sizeof(smp->tk));
662 BT_DBG("PassKey: %d", value);
663 put_unaligned_le32(value, smp->tk);
665 case MGMT_OP_USER_CONFIRM_REPLY:
666 set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
668 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
669 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
670 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
673 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
677 /* If it is our turn to send Pairing Confirm, do so now */
678 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags))
679 queue_work(hcon->hdev->workqueue, &smp->confirm);
684 static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
686 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
687 struct smp_chan *smp;
691 BT_DBG("conn %p", conn);
693 if (skb->len < sizeof(*req))
694 return SMP_INVALID_PARAMS;
696 if (conn->hcon->link_mode & HCI_LM_MASTER)
697 return SMP_CMD_NOTSUPP;
699 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
700 smp = smp_chan_create(conn);
702 smp = conn->smp_chan;
705 return SMP_UNSPECIFIED;
707 smp->preq[0] = SMP_CMD_PAIRING_REQ;
708 memcpy(&smp->preq[1], req, sizeof(*req));
709 skb_pull(skb, sizeof(*req));
711 /* We didn't start the pairing, so match remote */
712 auth = req->auth_req;
714 conn->hcon->pending_sec_level = authreq_to_seclevel(auth);
716 build_pairing_cmd(conn, req, &rsp, auth);
718 key_size = min(req->max_key_size, rsp.max_key_size);
719 if (check_enc_key_size(conn, key_size))
720 return SMP_ENC_KEY_SIZE;
722 get_random_bytes(smp->prnd, sizeof(smp->prnd));
724 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
725 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
727 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
729 /* Request setup of TK */
730 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
732 return SMP_UNSPECIFIED;
734 clear_bit(SMP_FLAG_INITIATOR, &smp->smp_flags);
739 static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
741 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
742 struct smp_chan *smp = conn->smp_chan;
743 struct hci_dev *hdev = conn->hcon->hdev;
744 u8 key_size, auth = SMP_AUTH_NONE;
747 BT_DBG("conn %p", conn);
749 if (skb->len < sizeof(*rsp))
750 return SMP_INVALID_PARAMS;
752 if (!(conn->hcon->link_mode & HCI_LM_MASTER))
753 return SMP_CMD_NOTSUPP;
755 skb_pull(skb, sizeof(*rsp));
757 req = (void *) &smp->preq[1];
759 key_size = min(req->max_key_size, rsp->max_key_size);
760 if (check_enc_key_size(conn, key_size))
761 return SMP_ENC_KEY_SIZE;
763 get_random_bytes(smp->prnd, sizeof(smp->prnd));
765 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
766 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
768 /* Update remote key distribution in case the remote cleared
769 * some bits that we had enabled in our request.
771 smp->remote_key_dist &= rsp->resp_key_dist;
773 if ((req->auth_req & SMP_AUTH_BONDING) &&
774 (rsp->auth_req & SMP_AUTH_BONDING))
775 auth = SMP_AUTH_BONDING;
777 auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM;
779 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
781 return SMP_UNSPECIFIED;
783 set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
785 /* Can't compose response until we have been confirmed */
786 if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags))
787 queue_work(hdev->workqueue, &smp->confirm);
792 static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
794 struct smp_chan *smp = conn->smp_chan;
795 struct hci_dev *hdev = conn->hcon->hdev;
797 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
799 if (skb->len < sizeof(smp->pcnf))
800 return SMP_INVALID_PARAMS;
802 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
803 skb_pull(skb, sizeof(smp->pcnf));
806 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
808 else if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags))
809 queue_work(hdev->workqueue, &smp->confirm);
811 set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
816 static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
818 struct smp_chan *smp = conn->smp_chan;
819 struct hci_dev *hdev = conn->hcon->hdev;
821 BT_DBG("conn %p", conn);
823 if (skb->len < sizeof(smp->rrnd))
824 return SMP_INVALID_PARAMS;
826 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
827 skb_pull(skb, sizeof(smp->rrnd));
829 queue_work(hdev->workqueue, &smp->random);
834 static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
837 struct hci_conn *hcon = conn->hcon;
839 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
844 if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated)
847 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
850 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
851 hcon->enc_key_size = key->enc_size;
856 static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
858 struct smp_cmd_security_req *rp = (void *) skb->data;
859 struct smp_cmd_pairing cp;
860 struct hci_conn *hcon = conn->hcon;
861 struct smp_chan *smp;
863 BT_DBG("conn %p", conn);
865 if (skb->len < sizeof(*rp))
866 return SMP_INVALID_PARAMS;
868 if (!(conn->hcon->link_mode & HCI_LM_MASTER))
869 return SMP_CMD_NOTSUPP;
871 hcon->pending_sec_level = authreq_to_seclevel(rp->auth_req);
873 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
876 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
879 smp = smp_chan_create(conn);
881 skb_pull(skb, sizeof(*rp));
883 memset(&cp, 0, sizeof(cp));
884 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
886 smp->preq[0] = SMP_CMD_PAIRING_REQ;
887 memcpy(&smp->preq[1], &cp, sizeof(cp));
889 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
891 clear_bit(SMP_FLAG_INITIATOR, &smp->smp_flags);
896 bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level)
898 if (sec_level == BT_SECURITY_LOW)
901 if (hcon->sec_level >= sec_level)
907 int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
909 struct l2cap_conn *conn = hcon->l2cap_data;
910 struct smp_chan *smp;
913 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
915 /* This may be NULL if there's an unexpected disconnection */
919 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
922 if (smp_sufficient_security(hcon, sec_level))
925 if (hcon->link_mode & HCI_LM_MASTER)
926 if (smp_ltk_encrypt(conn, sec_level))
929 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
932 smp = smp_chan_create(conn);
936 authreq = seclevel_to_authreq(sec_level);
938 /* hcon->auth_type is set by pair_device in mgmt.c. If the MITM
939 * flag is set we should also set it for the SMP request.
941 if ((hcon->auth_type & 0x01))
942 authreq |= SMP_AUTH_MITM;
944 if (hcon->link_mode & HCI_LM_MASTER) {
945 struct smp_cmd_pairing cp;
947 build_pairing_cmd(conn, &cp, NULL, authreq);
948 smp->preq[0] = SMP_CMD_PAIRING_REQ;
949 memcpy(&smp->preq[1], &cp, sizeof(cp));
951 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
953 struct smp_cmd_security_req cp;
954 cp.auth_req = authreq;
955 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
958 set_bit(SMP_FLAG_INITIATOR, &smp->smp_flags);
961 hcon->pending_sec_level = sec_level;
966 static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
968 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
969 struct smp_chan *smp = conn->smp_chan;
971 BT_DBG("conn %p", conn);
973 if (skb->len < sizeof(*rp))
974 return SMP_INVALID_PARAMS;
976 /* Ignore this PDU if it wasn't requested */
977 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
980 skb_pull(skb, sizeof(*rp));
982 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
987 static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
989 struct smp_cmd_master_ident *rp = (void *) skb->data;
990 struct smp_chan *smp = conn->smp_chan;
991 struct hci_dev *hdev = conn->hcon->hdev;
992 struct hci_conn *hcon = conn->hcon;
996 BT_DBG("conn %p", conn);
998 if (skb->len < sizeof(*rp))
999 return SMP_INVALID_PARAMS;
1001 /* Ignore this PDU if it wasn't requested */
1002 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
1005 /* Mark the information as received */
1006 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1008 skb_pull(skb, sizeof(*rp));
1011 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
1012 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, HCI_SMP_LTK,
1013 authenticated, smp->tk, smp->enc_key_size,
1014 rp->ediv, rp->rand);
1016 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1017 smp_distribute_keys(conn);
1018 hci_dev_unlock(hdev);
1023 static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1025 struct smp_cmd_ident_info *info = (void *) skb->data;
1026 struct smp_chan *smp = conn->smp_chan;
1030 if (skb->len < sizeof(*info))
1031 return SMP_INVALID_PARAMS;
1033 /* Ignore this PDU if it wasn't requested */
1034 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1037 skb_pull(skb, sizeof(*info));
1039 memcpy(smp->irk, info->irk, 16);
1044 static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1045 struct sk_buff *skb)
1047 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
1048 struct smp_chan *smp = conn->smp_chan;
1049 struct hci_conn *hcon = conn->hcon;
1054 if (skb->len < sizeof(*info))
1055 return SMP_INVALID_PARAMS;
1057 /* Ignore this PDU if it wasn't requested */
1058 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1061 /* Mark the information as received */
1062 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1064 skb_pull(skb, sizeof(*info));
1066 /* Strictly speaking the Core Specification (4.1) allows sending
1067 * an empty address which would force us to rely on just the IRK
1068 * as "identity information". However, since such
1069 * implementations are not known of and in order to not over
1070 * complicate our implementation, simply pretend that we never
1071 * received an IRK for such a device.
1073 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1074 BT_ERR("Ignoring IRK with no identity address");
1075 smp_distribute_keys(conn);
1079 bacpy(&smp->id_addr, &info->bdaddr);
1080 smp->id_addr_type = info->addr_type;
1082 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1083 bacpy(&rpa, &hcon->dst);
1085 bacpy(&rpa, BDADDR_ANY);
1087 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1088 smp->id_addr_type, smp->irk, &rpa);
1090 smp_distribute_keys(conn);
1095 static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1097 struct smp_cmd_sign_info *rp = (void *) skb->data;
1098 struct smp_chan *smp = conn->smp_chan;
1099 struct hci_dev *hdev = conn->hcon->hdev;
1100 struct smp_csrk *csrk;
1102 BT_DBG("conn %p", conn);
1104 if (skb->len < sizeof(*rp))
1105 return SMP_INVALID_PARAMS;
1107 /* Ignore this PDU if it wasn't requested */
1108 if (!(smp->remote_key_dist & SMP_DIST_SIGN))
1111 /* Mark the information as received */
1112 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1114 skb_pull(skb, sizeof(*rp));
1117 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1119 csrk->master = 0x01;
1120 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1123 if (!(smp->remote_key_dist & SMP_DIST_SIGN))
1124 smp_distribute_keys(conn);
1125 hci_dev_unlock(hdev);
1130 int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
1132 struct hci_conn *hcon = conn->hcon;
1136 if (hcon->type != LE_LINK) {
1146 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
1148 reason = SMP_PAIRING_NOTSUPP;
1152 code = skb->data[0];
1153 skb_pull(skb, sizeof(code));
1156 * The SMP context must be initialized for all other PDUs except
1157 * pairing and security requests. If we get any other PDU when
1158 * not initialized simply disconnect (done if this function
1159 * returns an error).
1161 if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ &&
1163 BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code);
1169 case SMP_CMD_PAIRING_REQ:
1170 reason = smp_cmd_pairing_req(conn, skb);
1173 case SMP_CMD_PAIRING_FAIL:
1174 smp_failure(conn, 0);
1179 case SMP_CMD_PAIRING_RSP:
1180 reason = smp_cmd_pairing_rsp(conn, skb);
1183 case SMP_CMD_SECURITY_REQ:
1184 reason = smp_cmd_security_req(conn, skb);
1187 case SMP_CMD_PAIRING_CONFIRM:
1188 reason = smp_cmd_pairing_confirm(conn, skb);
1191 case SMP_CMD_PAIRING_RANDOM:
1192 reason = smp_cmd_pairing_random(conn, skb);
1195 case SMP_CMD_ENCRYPT_INFO:
1196 reason = smp_cmd_encrypt_info(conn, skb);
1199 case SMP_CMD_MASTER_IDENT:
1200 reason = smp_cmd_master_ident(conn, skb);
1203 case SMP_CMD_IDENT_INFO:
1204 reason = smp_cmd_ident_info(conn, skb);
1207 case SMP_CMD_IDENT_ADDR_INFO:
1208 reason = smp_cmd_ident_addr_info(conn, skb);
1211 case SMP_CMD_SIGN_INFO:
1212 reason = smp_cmd_sign_info(conn, skb);
1216 BT_DBG("Unknown command code 0x%2.2x", code);
1218 reason = SMP_CMD_NOTSUPP;
1225 smp_failure(conn, reason);
1231 static void smp_notify_keys(struct l2cap_conn *conn)
1233 struct smp_chan *smp = conn->smp_chan;
1234 struct hci_conn *hcon = conn->hcon;
1235 struct hci_dev *hdev = hcon->hdev;
1236 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1237 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1240 if (smp->remote_irk) {
1241 mgmt_new_irk(hdev, smp->remote_irk);
1242 /* Now that user space can be considered to know the
1243 * identity address track the connection based on it
1246 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1247 hcon->dst_type = smp->remote_irk->addr_type;
1248 l2cap_conn_update_id_addr(hcon);
1251 /* The LTKs and CSRKs should be persistent only if both sides
1252 * had the bonding bit set in their authentication requests.
1254 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
1257 smp->csrk->bdaddr_type = hcon->dst_type;
1258 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1259 mgmt_new_csrk(hdev, smp->csrk, persistent);
1262 if (smp->slave_csrk) {
1263 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1264 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1265 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1269 smp->ltk->bdaddr_type = hcon->dst_type;
1270 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1271 mgmt_new_ltk(hdev, smp->ltk, persistent);
1274 if (smp->slave_ltk) {
1275 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1276 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1277 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1281 int smp_distribute_keys(struct l2cap_conn *conn)
1283 struct smp_cmd_pairing *req, *rsp;
1284 struct smp_chan *smp = conn->smp_chan;
1285 struct hci_conn *hcon = conn->hcon;
1286 struct hci_dev *hdev = hcon->hdev;
1289 BT_DBG("conn %p", conn);
1291 if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
1294 rsp = (void *) &smp->prsp[1];
1296 /* The responder sends its keys first */
1297 if (hcon->out && (smp->remote_key_dist & 0x07))
1300 req = (void *) &smp->preq[1];
1303 keydist = &rsp->init_key_dist;
1304 *keydist &= req->init_key_dist;
1306 keydist = &rsp->resp_key_dist;
1307 *keydist &= req->resp_key_dist;
1310 BT_DBG("keydist 0x%x", *keydist);
1312 if (*keydist & SMP_DIST_ENC_KEY) {
1313 struct smp_cmd_encrypt_info enc;
1314 struct smp_cmd_master_ident ident;
1315 struct smp_ltk *ltk;
1320 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1321 get_random_bytes(&ediv, sizeof(ediv));
1322 get_random_bytes(&rand, sizeof(rand));
1324 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1326 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1327 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1328 HCI_SMP_LTK_SLAVE, authenticated, enc.ltk,
1329 smp->enc_key_size, ediv, rand);
1330 smp->slave_ltk = ltk;
1335 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1337 *keydist &= ~SMP_DIST_ENC_KEY;
1340 if (*keydist & SMP_DIST_ID_KEY) {
1341 struct smp_cmd_ident_addr_info addrinfo;
1342 struct smp_cmd_ident_info idinfo;
1344 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1346 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1348 /* The hci_conn contains the local identity address
1349 * after the connection has been established.
1351 * This is true even when the connection has been
1352 * established using a resolvable random address.
1354 bacpy(&addrinfo.bdaddr, &hcon->src);
1355 addrinfo.addr_type = hcon->src_type;
1357 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1360 *keydist &= ~SMP_DIST_ID_KEY;
1363 if (*keydist & SMP_DIST_SIGN) {
1364 struct smp_cmd_sign_info sign;
1365 struct smp_csrk *csrk;
1367 /* Generate a new random key */
1368 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1370 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1372 csrk->master = 0x00;
1373 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1375 smp->slave_csrk = csrk;
1377 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1379 *keydist &= ~SMP_DIST_SIGN;
1382 /* If there are still keys to be received wait for them */
1383 if ((smp->remote_key_dist & 0x07))
1386 clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);
1387 cancel_delayed_work_sync(&conn->security_timer);
1388 set_bit(SMP_FLAG_COMPLETE, &smp->smp_flags);
1389 smp_notify_keys(conn);
1391 smp_chan_destroy(conn);