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 <net/bluetooth/bluetooth.h>
24 #include <net/bluetooth/hci_core.h>
25 #include <net/bluetooth/l2cap.h>
26 #include <net/bluetooth/mgmt.h>
27 #include <net/bluetooth/smp.h>
28 #include <linux/crypto.h>
29 #include <linux/scatterlist.h>
30 #include <crypto/b128ops.h>
32 #define SMP_TIMEOUT 30000 /* 30 seconds */
34 static inline void swap128(u8 src
[16], u8 dst
[16])
37 for (i
= 0; i
< 16; i
++)
41 static inline void swap56(u8 src
[7], u8 dst
[7])
44 for (i
= 0; i
< 7; i
++)
48 static int smp_e(struct crypto_blkcipher
*tfm
, const u8
*k
, u8
*r
)
50 struct blkcipher_desc desc
;
51 struct scatterlist sg
;
53 unsigned char iv
[128];
56 BT_ERR("tfm %p", tfm
);
63 err
= crypto_blkcipher_setkey(tfm
, k
, 16);
65 BT_ERR("cipher setkey failed: %d", err
);
69 sg_init_one(&sg
, r
, 16);
71 iv_len
= crypto_blkcipher_ivsize(tfm
);
73 memset(&iv
, 0xff, iv_len
);
74 crypto_blkcipher_set_iv(tfm
, iv
, iv_len
);
77 err
= crypto_blkcipher_encrypt(&desc
, &sg
, &sg
, 16);
79 BT_ERR("Encrypt data error %d", err
);
84 static int smp_c1(struct crypto_blkcipher
*tfm
, u8 k
[16], u8 r
[16],
85 u8 preq
[7], u8 pres
[7], u8 _iat
, bdaddr_t
*ia
,
86 u8 _rat
, bdaddr_t
*ra
, u8 res
[16])
93 /* p1 = pres || preq || _rat || _iat */
101 /* p2 = padding || ia || ra */
102 baswap((bdaddr_t
*) (p2
+ 4), ia
);
103 baswap((bdaddr_t
*) (p2
+ 10), ra
);
106 u128_xor((u128
*) res
, (u128
*) r
, (u128
*) p1
);
108 /* res = e(k, res) */
109 err
= smp_e(tfm
, k
, res
);
111 BT_ERR("Encrypt data error");
115 /* res = res XOR p2 */
116 u128_xor((u128
*) res
, (u128
*) res
, (u128
*) p2
);
118 /* res = e(k, res) */
119 err
= smp_e(tfm
, k
, res
);
121 BT_ERR("Encrypt data error");
126 static int smp_s1(struct crypto_blkcipher
*tfm
, u8 k
[16],
127 u8 r1
[16], u8 r2
[16], u8 _r
[16])
131 /* Just least significant octets from r1 and r2 are considered */
132 memcpy(_r
, r1
+ 8, 8);
133 memcpy(_r
+ 8, r2
+ 8, 8);
135 err
= smp_e(tfm
, k
, _r
);
137 BT_ERR("Encrypt data error");
142 static int smp_rand(u8
*buf
)
144 get_random_bytes(buf
, 16);
149 static struct sk_buff
*smp_build_cmd(struct l2cap_conn
*conn
, u8 code
,
150 u16 dlen
, void *data
)
153 struct l2cap_hdr
*lh
;
156 len
= L2CAP_HDR_SIZE
+ sizeof(code
) + dlen
;
161 skb
= bt_skb_alloc(len
, GFP_ATOMIC
);
165 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
166 lh
->len
= cpu_to_le16(sizeof(code
) + dlen
);
167 lh
->cid
= cpu_to_le16(L2CAP_CID_SMP
);
169 memcpy(skb_put(skb
, sizeof(code
)), &code
, sizeof(code
));
171 memcpy(skb_put(skb
, dlen
), data
, dlen
);
176 static void smp_send_cmd(struct l2cap_conn
*conn
, u8 code
, u16 len
, void *data
)
178 struct sk_buff
*skb
= smp_build_cmd(conn
, code
, len
, data
);
180 BT_DBG("code 0x%2.2x", code
);
185 skb
->priority
= HCI_PRIO_MAX
;
186 hci_send_acl(conn
->hchan
, skb
, 0);
188 cancel_delayed_work_sync(&conn
->security_timer
);
189 schedule_delayed_work(&conn
->security_timer
,
190 msecs_to_jiffies(SMP_TIMEOUT
));
193 static __u8
authreq_to_seclevel(__u8 authreq
)
195 if (authreq
& SMP_AUTH_MITM
)
196 return BT_SECURITY_HIGH
;
198 return BT_SECURITY_MEDIUM
;
201 static __u8
seclevel_to_authreq(__u8 sec_level
)
204 case BT_SECURITY_HIGH
:
205 return SMP_AUTH_MITM
| SMP_AUTH_BONDING
;
206 case BT_SECURITY_MEDIUM
:
207 return SMP_AUTH_BONDING
;
209 return SMP_AUTH_NONE
;
213 static void build_pairing_cmd(struct l2cap_conn
*conn
,
214 struct smp_cmd_pairing
*req
,
215 struct smp_cmd_pairing
*rsp
,
220 if (test_bit(HCI_PAIRABLE
, &conn
->hcon
->hdev
->dev_flags
)) {
221 dist_keys
= SMP_DIST_ENC_KEY
;
222 authreq
|= SMP_AUTH_BONDING
;
224 authreq
&= ~SMP_AUTH_BONDING
;
228 req
->io_capability
= conn
->hcon
->io_capability
;
229 req
->oob_flag
= SMP_OOB_NOT_PRESENT
;
230 req
->max_key_size
= SMP_MAX_ENC_KEY_SIZE
;
231 req
->init_key_dist
= 0;
232 req
->resp_key_dist
= dist_keys
;
233 req
->auth_req
= authreq
;
237 rsp
->io_capability
= conn
->hcon
->io_capability
;
238 rsp
->oob_flag
= SMP_OOB_NOT_PRESENT
;
239 rsp
->max_key_size
= SMP_MAX_ENC_KEY_SIZE
;
240 rsp
->init_key_dist
= 0;
241 rsp
->resp_key_dist
= req
->resp_key_dist
& dist_keys
;
242 rsp
->auth_req
= authreq
;
245 static u8
check_enc_key_size(struct l2cap_conn
*conn
, __u8 max_key_size
)
247 struct smp_chan
*smp
= conn
->smp_chan
;
249 if ((max_key_size
> SMP_MAX_ENC_KEY_SIZE
) ||
250 (max_key_size
< SMP_MIN_ENC_KEY_SIZE
))
251 return SMP_ENC_KEY_SIZE
;
253 smp
->enc_key_size
= max_key_size
;
258 static void smp_failure(struct l2cap_conn
*conn
, u8 reason
, u8 send
)
261 smp_send_cmd(conn
, SMP_CMD_PAIRING_FAIL
, sizeof(reason
),
264 clear_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->hcon
->flags
);
265 mgmt_auth_failed(conn
->hcon
->hdev
, conn
->dst
, reason
);
267 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND
, &conn
->hcon
->flags
)) {
268 cancel_delayed_work_sync(&conn
->security_timer
);
269 smp_chan_destroy(conn
);
273 #define JUST_WORKS 0x00
274 #define JUST_CFM 0x01
275 #define REQ_PASSKEY 0x02
276 #define CFM_PASSKEY 0x03
280 static const u8 gen_method
[5][5] = {
281 { JUST_WORKS
, JUST_CFM
, REQ_PASSKEY
, JUST_WORKS
, REQ_PASSKEY
},
282 { JUST_WORKS
, JUST_CFM
, REQ_PASSKEY
, JUST_WORKS
, REQ_PASSKEY
},
283 { CFM_PASSKEY
, CFM_PASSKEY
, REQ_PASSKEY
, JUST_WORKS
, CFM_PASSKEY
},
284 { JUST_WORKS
, JUST_CFM
, JUST_WORKS
, JUST_WORKS
, JUST_CFM
},
285 { CFM_PASSKEY
, CFM_PASSKEY
, REQ_PASSKEY
, JUST_WORKS
, OVERLAP
},
288 static int tk_request(struct l2cap_conn
*conn
, u8 remote_oob
, u8 auth
,
289 u8 local_io
, u8 remote_io
)
291 struct hci_conn
*hcon
= conn
->hcon
;
292 struct smp_chan
*smp
= conn
->smp_chan
;
297 /* Initialize key for JUST WORKS */
298 memset(smp
->tk
, 0, sizeof(smp
->tk
));
299 clear_bit(SMP_FLAG_TK_VALID
, &smp
->smp_flags
);
301 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth
, local_io
, remote_io
);
303 /* If neither side wants MITM, use JUST WORKS */
304 /* If either side has unknown io_caps, use JUST WORKS */
305 /* Otherwise, look up method from the table */
306 if (!(auth
& SMP_AUTH_MITM
) ||
307 local_io
> SMP_IO_KEYBOARD_DISPLAY
||
308 remote_io
> SMP_IO_KEYBOARD_DISPLAY
)
311 method
= gen_method
[local_io
][remote_io
];
313 /* If not bonding, don't ask user to confirm a Zero TK */
314 if (!(auth
& SMP_AUTH_BONDING
) && method
== JUST_CFM
)
317 /* If Just Works, Continue with Zero TK */
318 if (method
== JUST_WORKS
) {
319 set_bit(SMP_FLAG_TK_VALID
, &smp
->smp_flags
);
323 /* Not Just Works/Confirm results in MITM Authentication */
324 if (method
!= JUST_CFM
)
325 set_bit(SMP_FLAG_MITM_AUTH
, &smp
->smp_flags
);
327 /* If both devices have Keyoard-Display I/O, the master
328 * Confirms and the slave Enters the passkey.
330 if (method
== OVERLAP
) {
331 if (hcon
->link_mode
& HCI_LM_MASTER
)
332 method
= CFM_PASSKEY
;
334 method
= REQ_PASSKEY
;
337 /* Generate random passkey. Not valid until confirmed. */
338 if (method
== CFM_PASSKEY
) {
341 memset(key
, 0, sizeof(key
));
342 get_random_bytes(&passkey
, sizeof(passkey
));
344 put_unaligned_le32(passkey
, key
);
345 swap128(key
, smp
->tk
);
346 BT_DBG("PassKey: %d", passkey
);
349 hci_dev_lock(hcon
->hdev
);
351 if (method
== REQ_PASSKEY
)
352 ret
= mgmt_user_passkey_request(hcon
->hdev
, conn
->dst
);
354 ret
= mgmt_user_confirm_request(hcon
->hdev
, conn
->dst
,
355 cpu_to_le32(passkey
), 0);
357 hci_dev_unlock(hcon
->hdev
);
362 static void confirm_work(struct work_struct
*work
)
364 struct smp_chan
*smp
= container_of(work
, struct smp_chan
, confirm
);
365 struct l2cap_conn
*conn
= smp
->conn
;
366 struct crypto_blkcipher
*tfm
;
367 struct smp_cmd_pairing_confirm cp
;
371 BT_DBG("conn %p", conn
);
373 tfm
= crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC
);
375 reason
= SMP_UNSPECIFIED
;
382 ret
= smp_c1(tfm
, smp
->tk
, smp
->prnd
, smp
->preq
, smp
->prsp
, 0,
383 conn
->src
, conn
->hcon
->dst_type
, conn
->dst
,
386 ret
= smp_c1(tfm
, smp
->tk
, smp
->prnd
, smp
->preq
, smp
->prsp
,
387 conn
->hcon
->dst_type
, conn
->dst
, 0, conn
->src
,
390 reason
= SMP_UNSPECIFIED
;
394 clear_bit(SMP_FLAG_CFM_PENDING
, &smp
->smp_flags
);
396 swap128(res
, cp
.confirm_val
);
397 smp_send_cmd(smp
->conn
, SMP_CMD_PAIRING_CONFIRM
, sizeof(cp
), &cp
);
402 smp_failure(conn
, reason
, 1);
405 static void random_work(struct work_struct
*work
)
407 struct smp_chan
*smp
= container_of(work
, struct smp_chan
, random
);
408 struct l2cap_conn
*conn
= smp
->conn
;
409 struct hci_conn
*hcon
= conn
->hcon
;
410 struct crypto_blkcipher
*tfm
= smp
->tfm
;
411 u8 reason
, confirm
[16], res
[16], key
[16];
414 if (IS_ERR_OR_NULL(tfm
)) {
415 reason
= SMP_UNSPECIFIED
;
419 BT_DBG("conn %p %s", conn
, conn
->hcon
->out
? "master" : "slave");
422 ret
= smp_c1(tfm
, smp
->tk
, smp
->rrnd
, smp
->preq
, smp
->prsp
, 0,
423 conn
->src
, hcon
->dst_type
, conn
->dst
,
426 ret
= smp_c1(tfm
, smp
->tk
, smp
->rrnd
, smp
->preq
, smp
->prsp
,
427 hcon
->dst_type
, conn
->dst
, 0, conn
->src
,
430 reason
= SMP_UNSPECIFIED
;
434 swap128(res
, confirm
);
436 if (memcmp(smp
->pcnf
, confirm
, sizeof(smp
->pcnf
)) != 0) {
437 BT_ERR("Pairing failed (confirmation values mismatch)");
438 reason
= SMP_CONFIRM_FAILED
;
446 memset(rand
, 0, sizeof(rand
));
449 smp_s1(tfm
, smp
->tk
, smp
->rrnd
, smp
->prnd
, key
);
452 memset(stk
+ smp
->enc_key_size
, 0,
453 SMP_MAX_ENC_KEY_SIZE
- smp
->enc_key_size
);
455 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND
, &hcon
->flags
)) {
456 reason
= SMP_UNSPECIFIED
;
460 hci_le_start_enc(hcon
, ediv
, rand
, stk
);
461 hcon
->enc_key_size
= smp
->enc_key_size
;
463 u8 stk
[16], r
[16], rand
[8];
466 memset(rand
, 0, sizeof(rand
));
469 swap128(smp
->prnd
, r
);
470 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
, sizeof(r
), r
);
472 smp_s1(tfm
, smp
->tk
, smp
->prnd
, smp
->rrnd
, key
);
475 memset(stk
+ smp
->enc_key_size
, 0,
476 SMP_MAX_ENC_KEY_SIZE
- smp
->enc_key_size
);
478 hci_add_ltk(hcon
->hdev
, conn
->dst
, hcon
->dst_type
,
479 HCI_SMP_STK_SLAVE
, 0, 0, stk
,
480 smp
->enc_key_size
, ediv
, rand
);
486 smp_failure(conn
, reason
, 1);
489 static struct smp_chan
*smp_chan_create(struct l2cap_conn
*conn
)
491 struct smp_chan
*smp
;
493 smp
= kzalloc(sizeof(struct smp_chan
), GFP_ATOMIC
);
497 INIT_WORK(&smp
->confirm
, confirm_work
);
498 INIT_WORK(&smp
->random
, random_work
);
501 conn
->smp_chan
= smp
;
502 conn
->hcon
->smp_conn
= conn
;
504 hci_conn_hold(conn
->hcon
);
509 void smp_chan_destroy(struct l2cap_conn
*conn
)
511 struct smp_chan
*smp
= conn
->smp_chan
;
516 crypto_free_blkcipher(smp
->tfm
);
519 conn
->smp_chan
= NULL
;
520 conn
->hcon
->smp_conn
= NULL
;
521 hci_conn_put(conn
->hcon
);
524 int smp_user_confirm_reply(struct hci_conn
*hcon
, u16 mgmt_op
, __le32 passkey
)
526 struct l2cap_conn
*conn
= hcon
->smp_conn
;
527 struct smp_chan
*smp
;
536 smp
= conn
->smp_chan
;
539 case MGMT_OP_USER_PASSKEY_REPLY
:
540 value
= le32_to_cpu(passkey
);
541 memset(key
, 0, sizeof(key
));
542 BT_DBG("PassKey: %d", value
);
543 put_unaligned_le32(value
, key
);
544 swap128(key
, smp
->tk
);
546 case MGMT_OP_USER_CONFIRM_REPLY
:
547 set_bit(SMP_FLAG_TK_VALID
, &smp
->smp_flags
);
549 case MGMT_OP_USER_PASSKEY_NEG_REPLY
:
550 case MGMT_OP_USER_CONFIRM_NEG_REPLY
:
551 smp_failure(conn
, SMP_PASSKEY_ENTRY_FAILED
, 1);
554 smp_failure(conn
, SMP_PASSKEY_ENTRY_FAILED
, 1);
558 /* If it is our turn to send Pairing Confirm, do so now */
559 if (test_bit(SMP_FLAG_CFM_PENDING
, &smp
->smp_flags
))
560 queue_work(hcon
->hdev
->workqueue
, &smp
->confirm
);
565 static u8
smp_cmd_pairing_req(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
567 struct smp_cmd_pairing rsp
, *req
= (void *) skb
->data
;
568 struct smp_chan
*smp
;
570 u8 auth
= SMP_AUTH_NONE
;
573 BT_DBG("conn %p", conn
);
575 if (conn
->hcon
->link_mode
& HCI_LM_MASTER
)
576 return SMP_CMD_NOTSUPP
;
578 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND
, &conn
->hcon
->flags
))
579 smp
= smp_chan_create(conn
);
581 smp
= conn
->smp_chan
;
583 smp
->preq
[0] = SMP_CMD_PAIRING_REQ
;
584 memcpy(&smp
->preq
[1], req
, sizeof(*req
));
585 skb_pull(skb
, sizeof(*req
));
587 /* We didn't start the pairing, so match remote */
588 if (req
->auth_req
& SMP_AUTH_BONDING
)
589 auth
= req
->auth_req
;
591 build_pairing_cmd(conn
, req
, &rsp
, auth
);
593 key_size
= min(req
->max_key_size
, rsp
.max_key_size
);
594 if (check_enc_key_size(conn
, key_size
))
595 return SMP_ENC_KEY_SIZE
;
597 ret
= smp_rand(smp
->prnd
);
599 return SMP_UNSPECIFIED
;
601 smp
->prsp
[0] = SMP_CMD_PAIRING_RSP
;
602 memcpy(&smp
->prsp
[1], &rsp
, sizeof(rsp
));
604 smp_send_cmd(conn
, SMP_CMD_PAIRING_RSP
, sizeof(rsp
), &rsp
);
606 /* Request setup of TK */
607 ret
= tk_request(conn
, 0, auth
, rsp
.io_capability
, req
->io_capability
);
609 return SMP_UNSPECIFIED
;
614 static u8
smp_cmd_pairing_rsp(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
616 struct smp_cmd_pairing
*req
, *rsp
= (void *) skb
->data
;
617 struct smp_chan
*smp
= conn
->smp_chan
;
618 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
619 u8 key_size
, auth
= SMP_AUTH_NONE
;
622 BT_DBG("conn %p", conn
);
624 if (!(conn
->hcon
->link_mode
& HCI_LM_MASTER
))
625 return SMP_CMD_NOTSUPP
;
627 skb_pull(skb
, sizeof(*rsp
));
629 req
= (void *) &smp
->preq
[1];
631 key_size
= min(req
->max_key_size
, rsp
->max_key_size
);
632 if (check_enc_key_size(conn
, key_size
))
633 return SMP_ENC_KEY_SIZE
;
635 ret
= smp_rand(smp
->prnd
);
637 return SMP_UNSPECIFIED
;
639 smp
->prsp
[0] = SMP_CMD_PAIRING_RSP
;
640 memcpy(&smp
->prsp
[1], rsp
, sizeof(*rsp
));
642 if ((req
->auth_req
& SMP_AUTH_BONDING
) &&
643 (rsp
->auth_req
& SMP_AUTH_BONDING
))
644 auth
= SMP_AUTH_BONDING
;
646 auth
|= (req
->auth_req
| rsp
->auth_req
) & SMP_AUTH_MITM
;
648 ret
= tk_request(conn
, 0, auth
, rsp
->io_capability
, req
->io_capability
);
650 return SMP_UNSPECIFIED
;
652 set_bit(SMP_FLAG_CFM_PENDING
, &smp
->smp_flags
);
654 /* Can't compose response until we have been confirmed */
655 if (!test_bit(SMP_FLAG_TK_VALID
, &smp
->smp_flags
))
658 queue_work(hdev
->workqueue
, &smp
->confirm
);
663 static u8
smp_cmd_pairing_confirm(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
665 struct smp_chan
*smp
= conn
->smp_chan
;
666 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
668 BT_DBG("conn %p %s", conn
, conn
->hcon
->out
? "master" : "slave");
670 memcpy(smp
->pcnf
, skb
->data
, sizeof(smp
->pcnf
));
671 skb_pull(skb
, sizeof(smp
->pcnf
));
673 if (conn
->hcon
->out
) {
676 swap128(smp
->prnd
, random
);
677 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
, sizeof(random
),
679 } else if (test_bit(SMP_FLAG_TK_VALID
, &smp
->smp_flags
)) {
680 queue_work(hdev
->workqueue
, &smp
->confirm
);
682 set_bit(SMP_FLAG_CFM_PENDING
, &smp
->smp_flags
);
688 static u8
smp_cmd_pairing_random(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
690 struct smp_chan
*smp
= conn
->smp_chan
;
691 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
693 BT_DBG("conn %p", conn
);
695 swap128(skb
->data
, smp
->rrnd
);
696 skb_pull(skb
, sizeof(smp
->rrnd
));
698 queue_work(hdev
->workqueue
, &smp
->random
);
703 static u8
smp_ltk_encrypt(struct l2cap_conn
*conn
)
706 struct hci_conn
*hcon
= conn
->hcon
;
708 key
= hci_find_ltk_by_addr(hcon
->hdev
, conn
->dst
, hcon
->dst_type
);
712 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND
, &hcon
->flags
))
715 hci_le_start_enc(hcon
, key
->ediv
, key
->rand
, key
->val
);
716 hcon
->enc_key_size
= key
->enc_size
;
721 static u8
smp_cmd_security_req(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
723 struct smp_cmd_security_req
*rp
= (void *) skb
->data
;
724 struct smp_cmd_pairing cp
;
725 struct hci_conn
*hcon
= conn
->hcon
;
726 struct smp_chan
*smp
;
728 BT_DBG("conn %p", conn
);
730 hcon
->pending_sec_level
= authreq_to_seclevel(rp
->auth_req
);
732 if (smp_ltk_encrypt(conn
))
735 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND
, &hcon
->flags
))
738 smp
= smp_chan_create(conn
);
740 skb_pull(skb
, sizeof(*rp
));
742 memset(&cp
, 0, sizeof(cp
));
743 build_pairing_cmd(conn
, &cp
, NULL
, rp
->auth_req
);
745 smp
->preq
[0] = SMP_CMD_PAIRING_REQ
;
746 memcpy(&smp
->preq
[1], &cp
, sizeof(cp
));
748 smp_send_cmd(conn
, SMP_CMD_PAIRING_REQ
, sizeof(cp
), &cp
);
753 int smp_conn_security(struct l2cap_conn
*conn
, __u8 sec_level
)
755 struct hci_conn
*hcon
= conn
->hcon
;
756 struct smp_chan
*smp
= conn
->smp_chan
;
759 BT_DBG("conn %p hcon %p level 0x%2.2x", conn
, hcon
, sec_level
);
761 if (!lmp_host_le_capable(hcon
->hdev
))
764 if (sec_level
== BT_SECURITY_LOW
)
767 if (hcon
->sec_level
>= sec_level
)
770 if (hcon
->link_mode
& HCI_LM_MASTER
)
771 if (smp_ltk_encrypt(conn
))
774 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND
, &hcon
->flags
))
777 smp
= smp_chan_create(conn
);
781 authreq
= seclevel_to_authreq(sec_level
);
783 if (hcon
->link_mode
& HCI_LM_MASTER
) {
784 struct smp_cmd_pairing cp
;
786 build_pairing_cmd(conn
, &cp
, NULL
, authreq
);
787 smp
->preq
[0] = SMP_CMD_PAIRING_REQ
;
788 memcpy(&smp
->preq
[1], &cp
, sizeof(cp
));
790 smp_send_cmd(conn
, SMP_CMD_PAIRING_REQ
, sizeof(cp
), &cp
);
792 struct smp_cmd_security_req cp
;
793 cp
.auth_req
= authreq
;
794 smp_send_cmd(conn
, SMP_CMD_SECURITY_REQ
, sizeof(cp
), &cp
);
798 hcon
->pending_sec_level
= sec_level
;
803 static int smp_cmd_encrypt_info(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
805 struct smp_cmd_encrypt_info
*rp
= (void *) skb
->data
;
806 struct smp_chan
*smp
= conn
->smp_chan
;
808 skb_pull(skb
, sizeof(*rp
));
810 memcpy(smp
->tk
, rp
->ltk
, sizeof(smp
->tk
));
815 static int smp_cmd_master_ident(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
817 struct smp_cmd_master_ident
*rp
= (void *) skb
->data
;
818 struct smp_chan
*smp
= conn
->smp_chan
;
819 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
820 struct hci_conn
*hcon
= conn
->hcon
;
823 skb_pull(skb
, sizeof(*rp
));
826 authenticated
= (conn
->hcon
->sec_level
== BT_SECURITY_HIGH
);
827 hci_add_ltk(conn
->hcon
->hdev
, conn
->dst
, hcon
->dst_type
,
828 HCI_SMP_LTK
, 1, authenticated
, smp
->tk
,
829 smp
->enc_key_size
, rp
->ediv
, rp
->rand
);
830 smp_distribute_keys(conn
, 1);
831 hci_dev_unlock(hdev
);
836 int smp_sig_channel(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
838 __u8 code
= skb
->data
[0];
842 if (!lmp_host_le_capable(conn
->hcon
->hdev
)) {
844 reason
= SMP_PAIRING_NOTSUPP
;
848 skb_pull(skb
, sizeof(code
));
851 case SMP_CMD_PAIRING_REQ
:
852 reason
= smp_cmd_pairing_req(conn
, skb
);
855 case SMP_CMD_PAIRING_FAIL
:
856 smp_failure(conn
, skb
->data
[0], 0);
861 case SMP_CMD_PAIRING_RSP
:
862 reason
= smp_cmd_pairing_rsp(conn
, skb
);
865 case SMP_CMD_SECURITY_REQ
:
866 reason
= smp_cmd_security_req(conn
, skb
);
869 case SMP_CMD_PAIRING_CONFIRM
:
870 reason
= smp_cmd_pairing_confirm(conn
, skb
);
873 case SMP_CMD_PAIRING_RANDOM
:
874 reason
= smp_cmd_pairing_random(conn
, skb
);
877 case SMP_CMD_ENCRYPT_INFO
:
878 reason
= smp_cmd_encrypt_info(conn
, skb
);
881 case SMP_CMD_MASTER_IDENT
:
882 reason
= smp_cmd_master_ident(conn
, skb
);
885 case SMP_CMD_IDENT_INFO
:
886 case SMP_CMD_IDENT_ADDR_INFO
:
887 case SMP_CMD_SIGN_INFO
:
893 BT_DBG("Unknown command code 0x%2.2x", code
);
895 reason
= SMP_CMD_NOTSUPP
;
902 smp_failure(conn
, reason
, 1);
908 int smp_distribute_keys(struct l2cap_conn
*conn
, __u8 force
)
910 struct smp_cmd_pairing
*req
, *rsp
;
911 struct smp_chan
*smp
= conn
->smp_chan
;
914 BT_DBG("conn %p force %d", conn
, force
);
916 if (!test_bit(HCI_CONN_LE_SMP_PEND
, &conn
->hcon
->flags
))
919 rsp
= (void *) &smp
->prsp
[1];
921 /* The responder sends its keys first */
922 if (!force
&& conn
->hcon
->out
&& (rsp
->resp_key_dist
& 0x07))
925 req
= (void *) &smp
->preq
[1];
927 if (conn
->hcon
->out
) {
928 keydist
= &rsp
->init_key_dist
;
929 *keydist
&= req
->init_key_dist
;
931 keydist
= &rsp
->resp_key_dist
;
932 *keydist
&= req
->resp_key_dist
;
936 BT_DBG("keydist 0x%x", *keydist
);
938 if (*keydist
& SMP_DIST_ENC_KEY
) {
939 struct smp_cmd_encrypt_info enc
;
940 struct smp_cmd_master_ident ident
;
941 struct hci_conn
*hcon
= conn
->hcon
;
945 get_random_bytes(enc
.ltk
, sizeof(enc
.ltk
));
946 get_random_bytes(&ediv
, sizeof(ediv
));
947 get_random_bytes(ident
.rand
, sizeof(ident
.rand
));
949 smp_send_cmd(conn
, SMP_CMD_ENCRYPT_INFO
, sizeof(enc
), &enc
);
951 authenticated
= hcon
->sec_level
== BT_SECURITY_HIGH
;
952 hci_add_ltk(conn
->hcon
->hdev
, conn
->dst
, hcon
->dst_type
,
953 HCI_SMP_LTK_SLAVE
, 1, authenticated
,
954 enc
.ltk
, smp
->enc_key_size
,
957 ident
.ediv
= cpu_to_le16(ediv
);
959 smp_send_cmd(conn
, SMP_CMD_MASTER_IDENT
, sizeof(ident
), &ident
);
961 *keydist
&= ~SMP_DIST_ENC_KEY
;
964 if (*keydist
& SMP_DIST_ID_KEY
) {
965 struct smp_cmd_ident_addr_info addrinfo
;
966 struct smp_cmd_ident_info idinfo
;
968 /* Send a dummy key */
969 get_random_bytes(idinfo
.irk
, sizeof(idinfo
.irk
));
971 smp_send_cmd(conn
, SMP_CMD_IDENT_INFO
, sizeof(idinfo
), &idinfo
);
973 /* Just public address */
974 memset(&addrinfo
, 0, sizeof(addrinfo
));
975 bacpy(&addrinfo
.bdaddr
, conn
->src
);
977 smp_send_cmd(conn
, SMP_CMD_IDENT_ADDR_INFO
, sizeof(addrinfo
),
980 *keydist
&= ~SMP_DIST_ID_KEY
;
983 if (*keydist
& SMP_DIST_SIGN
) {
984 struct smp_cmd_sign_info sign
;
986 /* Send a dummy key */
987 get_random_bytes(sign
.csrk
, sizeof(sign
.csrk
));
989 smp_send_cmd(conn
, SMP_CMD_SIGN_INFO
, sizeof(sign
), &sign
);
991 *keydist
&= ~SMP_DIST_SIGN
;
994 if (conn
->hcon
->out
|| force
) {
995 clear_bit(HCI_CONN_LE_SMP_PEND
, &conn
->hcon
->flags
);
996 cancel_delayed_work_sync(&conn
->security_timer
);
997 smp_chan_destroy(conn
);