bonding: rlb mode of bond should not alter ARP originating via bridge
[linux-2.6/cjktty.git] / net / bluetooth / smp.c
blob68a9587c96945cdd3264cc310b68b7be95cfd953
1 /*
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>
31 #include <net/bluetooth/smp.h>
33 #define SMP_TIMEOUT msecs_to_jiffies(30000)
35 #define AUTH_REQ_MASK 0x07
37 static inline void swap128(u8 src[16], u8 dst[16])
39 int i;
40 for (i = 0; i < 16; i++)
41 dst[15 - i] = src[i];
44 static inline void swap56(u8 src[7], u8 dst[7])
46 int i;
47 for (i = 0; i < 7; i++)
48 dst[6 - i] = src[i];
51 static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
53 struct blkcipher_desc desc;
54 struct scatterlist sg;
55 int err, iv_len;
56 unsigned char iv[128];
58 if (tfm == NULL) {
59 BT_ERR("tfm %p", tfm);
60 return -EINVAL;
63 desc.tfm = tfm;
64 desc.flags = 0;
66 err = crypto_blkcipher_setkey(tfm, k, 16);
67 if (err) {
68 BT_ERR("cipher setkey failed: %d", err);
69 return err;
72 sg_init_one(&sg, r, 16);
74 iv_len = crypto_blkcipher_ivsize(tfm);
75 if (iv_len) {
76 memset(&iv, 0xff, iv_len);
77 crypto_blkcipher_set_iv(tfm, iv, iv_len);
80 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
81 if (err)
82 BT_ERR("Encrypt data error %d", err);
84 return err;
87 static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],
88 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia,
89 u8 _rat, bdaddr_t *ra, u8 res[16])
91 u8 p1[16], p2[16];
92 int err;
94 memset(p1, 0, 16);
96 /* p1 = pres || preq || _rat || _iat */
97 swap56(pres, p1);
98 swap56(preq, p1 + 7);
99 p1[14] = _rat;
100 p1[15] = _iat;
102 memset(p2, 0, 16);
104 /* p2 = padding || ia || ra */
105 baswap((bdaddr_t *) (p2 + 4), ia);
106 baswap((bdaddr_t *) (p2 + 10), ra);
108 /* res = r XOR p1 */
109 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
111 /* res = e(k, res) */
112 err = smp_e(tfm, k, res);
113 if (err) {
114 BT_ERR("Encrypt data error");
115 return err;
118 /* res = res XOR p2 */
119 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
121 /* res = e(k, res) */
122 err = smp_e(tfm, k, res);
123 if (err)
124 BT_ERR("Encrypt data error");
126 return err;
129 static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16],
130 u8 r1[16], u8 r2[16], u8 _r[16])
132 int err;
134 /* Just least significant octets from r1 and r2 are considered */
135 memcpy(_r, r1 + 8, 8);
136 memcpy(_r + 8, r2 + 8, 8);
138 err = smp_e(tfm, k, _r);
139 if (err)
140 BT_ERR("Encrypt data error");
142 return err;
145 static int smp_rand(u8 *buf)
147 get_random_bytes(buf, 16);
149 return 0;
152 static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code,
153 u16 dlen, void *data)
155 struct sk_buff *skb;
156 struct l2cap_hdr *lh;
157 int len;
159 len = L2CAP_HDR_SIZE + sizeof(code) + dlen;
161 if (len > conn->mtu)
162 return NULL;
164 skb = bt_skb_alloc(len, GFP_ATOMIC);
165 if (!skb)
166 return NULL;
168 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
169 lh->len = cpu_to_le16(sizeof(code) + dlen);
170 lh->cid = __constant_cpu_to_le16(L2CAP_CID_SMP);
172 memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code));
174 memcpy(skb_put(skb, dlen), data, dlen);
176 return skb;
179 static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
181 struct sk_buff *skb = smp_build_cmd(conn, code, len, data);
183 BT_DBG("code 0x%2.2x", code);
185 if (!skb)
186 return;
188 skb->priority = HCI_PRIO_MAX;
189 hci_send_acl(conn->hchan, skb, 0);
191 cancel_delayed_work_sync(&conn->security_timer);
192 schedule_delayed_work(&conn->security_timer, SMP_TIMEOUT);
195 static __u8 authreq_to_seclevel(__u8 authreq)
197 if (authreq & SMP_AUTH_MITM)
198 return BT_SECURITY_HIGH;
199 else
200 return BT_SECURITY_MEDIUM;
203 static __u8 seclevel_to_authreq(__u8 sec_level)
205 switch (sec_level) {
206 case BT_SECURITY_HIGH:
207 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
208 case BT_SECURITY_MEDIUM:
209 return SMP_AUTH_BONDING;
210 default:
211 return SMP_AUTH_NONE;
215 static void build_pairing_cmd(struct l2cap_conn *conn,
216 struct smp_cmd_pairing *req,
217 struct smp_cmd_pairing *rsp,
218 __u8 authreq)
220 u8 dist_keys = 0;
222 if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->dev_flags)) {
223 dist_keys = SMP_DIST_ENC_KEY;
224 authreq |= SMP_AUTH_BONDING;
225 } else {
226 authreq &= ~SMP_AUTH_BONDING;
229 if (rsp == NULL) {
230 req->io_capability = conn->hcon->io_capability;
231 req->oob_flag = SMP_OOB_NOT_PRESENT;
232 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
233 req->init_key_dist = 0;
234 req->resp_key_dist = dist_keys;
235 req->auth_req = (authreq & AUTH_REQ_MASK);
236 return;
239 rsp->io_capability = conn->hcon->io_capability;
240 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
241 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
242 rsp->init_key_dist = 0;
243 rsp->resp_key_dist = req->resp_key_dist & dist_keys;
244 rsp->auth_req = (authreq & AUTH_REQ_MASK);
247 static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
249 struct smp_chan *smp = conn->smp_chan;
251 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
252 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
253 return SMP_ENC_KEY_SIZE;
255 smp->enc_key_size = max_key_size;
257 return 0;
260 static void smp_failure(struct l2cap_conn *conn, u8 reason, u8 send)
262 struct hci_conn *hcon = conn->hcon;
264 if (send)
265 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
266 &reason);
268 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->hcon->flags);
269 mgmt_auth_failed(conn->hcon->hdev, conn->dst, hcon->type,
270 hcon->dst_type, HCI_ERROR_AUTH_FAILURE);
272 cancel_delayed_work_sync(&conn->security_timer);
274 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
275 smp_chan_destroy(conn);
278 #define JUST_WORKS 0x00
279 #define JUST_CFM 0x01
280 #define REQ_PASSKEY 0x02
281 #define CFM_PASSKEY 0x03
282 #define REQ_OOB 0x04
283 #define OVERLAP 0xFF
285 static const u8 gen_method[5][5] = {
286 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
287 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
288 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
289 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
290 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
293 static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
294 u8 local_io, u8 remote_io)
296 struct hci_conn *hcon = conn->hcon;
297 struct smp_chan *smp = conn->smp_chan;
298 u8 method;
299 u32 passkey = 0;
300 int ret = 0;
302 /* Initialize key for JUST WORKS */
303 memset(smp->tk, 0, sizeof(smp->tk));
304 clear_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
306 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
308 /* If neither side wants MITM, use JUST WORKS */
309 /* If either side has unknown io_caps, use JUST WORKS */
310 /* Otherwise, look up method from the table */
311 if (!(auth & SMP_AUTH_MITM) ||
312 local_io > SMP_IO_KEYBOARD_DISPLAY ||
313 remote_io > SMP_IO_KEYBOARD_DISPLAY)
314 method = JUST_WORKS;
315 else
316 method = gen_method[remote_io][local_io];
318 /* If not bonding, don't ask user to confirm a Zero TK */
319 if (!(auth & SMP_AUTH_BONDING) && method == JUST_CFM)
320 method = JUST_WORKS;
322 /* If Just Works, Continue with Zero TK */
323 if (method == JUST_WORKS) {
324 set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
325 return 0;
328 /* Not Just Works/Confirm results in MITM Authentication */
329 if (method != JUST_CFM)
330 set_bit(SMP_FLAG_MITM_AUTH, &smp->smp_flags);
332 /* If both devices have Keyoard-Display I/O, the master
333 * Confirms and the slave Enters the passkey.
335 if (method == OVERLAP) {
336 if (hcon->link_mode & HCI_LM_MASTER)
337 method = CFM_PASSKEY;
338 else
339 method = REQ_PASSKEY;
342 /* Generate random passkey. Not valid until confirmed. */
343 if (method == CFM_PASSKEY) {
344 u8 key[16];
346 memset(key, 0, sizeof(key));
347 get_random_bytes(&passkey, sizeof(passkey));
348 passkey %= 1000000;
349 put_unaligned_le32(passkey, key);
350 swap128(key, smp->tk);
351 BT_DBG("PassKey: %d", passkey);
354 hci_dev_lock(hcon->hdev);
356 if (method == REQ_PASSKEY)
357 ret = mgmt_user_passkey_request(hcon->hdev, conn->dst,
358 hcon->type, hcon->dst_type);
359 else
360 ret = mgmt_user_confirm_request(hcon->hdev, conn->dst,
361 hcon->type, hcon->dst_type,
362 cpu_to_le32(passkey), 0);
364 hci_dev_unlock(hcon->hdev);
366 return ret;
369 static void confirm_work(struct work_struct *work)
371 struct smp_chan *smp = container_of(work, struct smp_chan, confirm);
372 struct l2cap_conn *conn = smp->conn;
373 struct crypto_blkcipher *tfm;
374 struct smp_cmd_pairing_confirm cp;
375 int ret;
376 u8 res[16], reason;
378 BT_DBG("conn %p", conn);
380 tfm = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
381 if (IS_ERR(tfm)) {
382 reason = SMP_UNSPECIFIED;
383 goto error;
386 smp->tfm = tfm;
388 if (conn->hcon->out)
389 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, 0,
390 conn->src, conn->hcon->dst_type, conn->dst, res);
391 else
392 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,
393 conn->hcon->dst_type, conn->dst, 0, conn->src,
394 res);
395 if (ret) {
396 reason = SMP_UNSPECIFIED;
397 goto error;
400 clear_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
402 swap128(res, cp.confirm_val);
403 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
405 return;
407 error:
408 smp_failure(conn, reason, 1);
411 static void random_work(struct work_struct *work)
413 struct smp_chan *smp = container_of(work, struct smp_chan, random);
414 struct l2cap_conn *conn = smp->conn;
415 struct hci_conn *hcon = conn->hcon;
416 struct crypto_blkcipher *tfm = smp->tfm;
417 u8 reason, confirm[16], res[16], key[16];
418 int ret;
420 if (IS_ERR_OR_NULL(tfm)) {
421 reason = SMP_UNSPECIFIED;
422 goto error;
425 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
427 if (hcon->out)
428 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, 0,
429 conn->src, hcon->dst_type, conn->dst, res);
430 else
431 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,
432 hcon->dst_type, conn->dst, 0, conn->src, res);
433 if (ret) {
434 reason = SMP_UNSPECIFIED;
435 goto error;
438 swap128(res, confirm);
440 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
441 BT_ERR("Pairing failed (confirmation values mismatch)");
442 reason = SMP_CONFIRM_FAILED;
443 goto error;
446 if (hcon->out) {
447 u8 stk[16], rand[8];
448 __le16 ediv;
450 memset(rand, 0, sizeof(rand));
451 ediv = 0;
453 smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, key);
454 swap128(key, stk);
456 memset(stk + smp->enc_key_size, 0,
457 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
459 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) {
460 reason = SMP_UNSPECIFIED;
461 goto error;
464 hci_le_start_enc(hcon, ediv, rand, stk);
465 hcon->enc_key_size = smp->enc_key_size;
466 } else {
467 u8 stk[16], r[16], rand[8];
468 __le16 ediv;
470 memset(rand, 0, sizeof(rand));
471 ediv = 0;
473 swap128(smp->prnd, r);
474 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r);
476 smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, key);
477 swap128(key, stk);
479 memset(stk + smp->enc_key_size, 0,
480 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
482 hci_add_ltk(hcon->hdev, conn->dst, hcon->dst_type,
483 HCI_SMP_STK_SLAVE, 0, 0, stk, smp->enc_key_size,
484 ediv, rand);
487 return;
489 error:
490 smp_failure(conn, reason, 1);
493 static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
495 struct smp_chan *smp;
497 smp = kzalloc(sizeof(struct smp_chan), GFP_ATOMIC);
498 if (!smp)
499 return NULL;
501 INIT_WORK(&smp->confirm, confirm_work);
502 INIT_WORK(&smp->random, random_work);
504 smp->conn = conn;
505 conn->smp_chan = smp;
506 conn->hcon->smp_conn = conn;
508 hci_conn_hold(conn->hcon);
510 return smp;
513 void smp_chan_destroy(struct l2cap_conn *conn)
515 struct smp_chan *smp = conn->smp_chan;
517 BUG_ON(!smp);
519 if (smp->tfm)
520 crypto_free_blkcipher(smp->tfm);
522 kfree(smp);
523 conn->smp_chan = NULL;
524 conn->hcon->smp_conn = NULL;
525 hci_conn_put(conn->hcon);
528 int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
530 struct l2cap_conn *conn = hcon->smp_conn;
531 struct smp_chan *smp;
532 u32 value;
533 u8 key[16];
535 BT_DBG("");
537 if (!conn)
538 return -ENOTCONN;
540 smp = conn->smp_chan;
542 switch (mgmt_op) {
543 case MGMT_OP_USER_PASSKEY_REPLY:
544 value = le32_to_cpu(passkey);
545 memset(key, 0, sizeof(key));
546 BT_DBG("PassKey: %d", value);
547 put_unaligned_le32(value, key);
548 swap128(key, smp->tk);
549 /* Fall Through */
550 case MGMT_OP_USER_CONFIRM_REPLY:
551 set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
552 break;
553 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
554 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
555 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED, 1);
556 return 0;
557 default:
558 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED, 1);
559 return -EOPNOTSUPP;
562 /* If it is our turn to send Pairing Confirm, do so now */
563 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags))
564 queue_work(hcon->hdev->workqueue, &smp->confirm);
566 return 0;
569 static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
571 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
572 struct smp_chan *smp;
573 u8 key_size;
574 u8 auth = SMP_AUTH_NONE;
575 int ret;
577 BT_DBG("conn %p", conn);
579 if (conn->hcon->link_mode & HCI_LM_MASTER)
580 return SMP_CMD_NOTSUPP;
582 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
583 smp = smp_chan_create(conn);
584 else
585 smp = conn->smp_chan;
587 if (!smp)
588 return SMP_UNSPECIFIED;
590 smp->preq[0] = SMP_CMD_PAIRING_REQ;
591 memcpy(&smp->preq[1], req, sizeof(*req));
592 skb_pull(skb, sizeof(*req));
594 /* We didn't start the pairing, so match remote */
595 if (req->auth_req & SMP_AUTH_BONDING)
596 auth = req->auth_req;
598 conn->hcon->pending_sec_level = authreq_to_seclevel(auth);
600 build_pairing_cmd(conn, req, &rsp, auth);
602 key_size = min(req->max_key_size, rsp.max_key_size);
603 if (check_enc_key_size(conn, key_size))
604 return SMP_ENC_KEY_SIZE;
606 ret = smp_rand(smp->prnd);
607 if (ret)
608 return SMP_UNSPECIFIED;
610 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
611 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
613 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
615 /* Request setup of TK */
616 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
617 if (ret)
618 return SMP_UNSPECIFIED;
620 return 0;
623 static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
625 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
626 struct smp_chan *smp = conn->smp_chan;
627 struct hci_dev *hdev = conn->hcon->hdev;
628 u8 key_size, auth = SMP_AUTH_NONE;
629 int ret;
631 BT_DBG("conn %p", conn);
633 if (!(conn->hcon->link_mode & HCI_LM_MASTER))
634 return SMP_CMD_NOTSUPP;
636 skb_pull(skb, sizeof(*rsp));
638 req = (void *) &smp->preq[1];
640 key_size = min(req->max_key_size, rsp->max_key_size);
641 if (check_enc_key_size(conn, key_size))
642 return SMP_ENC_KEY_SIZE;
644 ret = smp_rand(smp->prnd);
645 if (ret)
646 return SMP_UNSPECIFIED;
648 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
649 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
651 if ((req->auth_req & SMP_AUTH_BONDING) &&
652 (rsp->auth_req & SMP_AUTH_BONDING))
653 auth = SMP_AUTH_BONDING;
655 auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM;
657 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
658 if (ret)
659 return SMP_UNSPECIFIED;
661 set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
663 /* Can't compose response until we have been confirmed */
664 if (!test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags))
665 return 0;
667 queue_work(hdev->workqueue, &smp->confirm);
669 return 0;
672 static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
674 struct smp_chan *smp = conn->smp_chan;
675 struct hci_dev *hdev = conn->hcon->hdev;
677 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
679 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
680 skb_pull(skb, sizeof(smp->pcnf));
682 if (conn->hcon->out) {
683 u8 random[16];
685 swap128(smp->prnd, random);
686 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random),
687 random);
688 } else if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) {
689 queue_work(hdev->workqueue, &smp->confirm);
690 } else {
691 set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
694 return 0;
697 static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
699 struct smp_chan *smp = conn->smp_chan;
700 struct hci_dev *hdev = conn->hcon->hdev;
702 BT_DBG("conn %p", conn);
704 swap128(skb->data, smp->rrnd);
705 skb_pull(skb, sizeof(smp->rrnd));
707 queue_work(hdev->workqueue, &smp->random);
709 return 0;
712 static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
714 struct smp_ltk *key;
715 struct hci_conn *hcon = conn->hcon;
717 key = hci_find_ltk_by_addr(hcon->hdev, conn->dst, hcon->dst_type);
718 if (!key)
719 return 0;
721 if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated)
722 return 0;
724 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
725 return 1;
727 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
728 hcon->enc_key_size = key->enc_size;
730 return 1;
733 static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
735 struct smp_cmd_security_req *rp = (void *) skb->data;
736 struct smp_cmd_pairing cp;
737 struct hci_conn *hcon = conn->hcon;
738 struct smp_chan *smp;
740 BT_DBG("conn %p", conn);
742 hcon->pending_sec_level = authreq_to_seclevel(rp->auth_req);
744 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
745 return 0;
747 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
748 return 0;
750 smp = smp_chan_create(conn);
752 skb_pull(skb, sizeof(*rp));
754 memset(&cp, 0, sizeof(cp));
755 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
757 smp->preq[0] = SMP_CMD_PAIRING_REQ;
758 memcpy(&smp->preq[1], &cp, sizeof(cp));
760 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
762 return 0;
765 int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
767 struct l2cap_conn *conn = hcon->l2cap_data;
768 struct smp_chan *smp = conn->smp_chan;
769 __u8 authreq;
771 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
773 if (!lmp_host_le_capable(hcon->hdev))
774 return 1;
776 if (sec_level == BT_SECURITY_LOW)
777 return 1;
779 if (hcon->sec_level >= sec_level)
780 return 1;
782 if (hcon->link_mode & HCI_LM_MASTER)
783 if (smp_ltk_encrypt(conn, sec_level))
784 goto done;
786 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
787 return 0;
789 smp = smp_chan_create(conn);
790 if (!smp)
791 return 1;
793 authreq = seclevel_to_authreq(sec_level);
795 if (hcon->link_mode & HCI_LM_MASTER) {
796 struct smp_cmd_pairing cp;
798 build_pairing_cmd(conn, &cp, NULL, authreq);
799 smp->preq[0] = SMP_CMD_PAIRING_REQ;
800 memcpy(&smp->preq[1], &cp, sizeof(cp));
802 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
803 } else {
804 struct smp_cmd_security_req cp;
805 cp.auth_req = authreq;
806 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
809 done:
810 hcon->pending_sec_level = sec_level;
812 return 0;
815 static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
817 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
818 struct smp_chan *smp = conn->smp_chan;
820 skb_pull(skb, sizeof(*rp));
822 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
824 return 0;
827 static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
829 struct smp_cmd_master_ident *rp = (void *) skb->data;
830 struct smp_chan *smp = conn->smp_chan;
831 struct hci_dev *hdev = conn->hcon->hdev;
832 struct hci_conn *hcon = conn->hcon;
833 u8 authenticated;
835 skb_pull(skb, sizeof(*rp));
837 hci_dev_lock(hdev);
838 authenticated = (conn->hcon->sec_level == BT_SECURITY_HIGH);
839 hci_add_ltk(conn->hcon->hdev, conn->dst, hcon->dst_type,
840 HCI_SMP_LTK, 1, authenticated, smp->tk, smp->enc_key_size,
841 rp->ediv, rp->rand);
842 smp_distribute_keys(conn, 1);
843 hci_dev_unlock(hdev);
845 return 0;
848 int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
850 __u8 code = skb->data[0];
851 __u8 reason;
852 int err = 0;
854 if (!lmp_host_le_capable(conn->hcon->hdev)) {
855 err = -ENOTSUPP;
856 reason = SMP_PAIRING_NOTSUPP;
857 goto done;
860 skb_pull(skb, sizeof(code));
862 switch (code) {
863 case SMP_CMD_PAIRING_REQ:
864 reason = smp_cmd_pairing_req(conn, skb);
865 break;
867 case SMP_CMD_PAIRING_FAIL:
868 smp_failure(conn, skb->data[0], 0);
869 reason = 0;
870 err = -EPERM;
871 break;
873 case SMP_CMD_PAIRING_RSP:
874 reason = smp_cmd_pairing_rsp(conn, skb);
875 break;
877 case SMP_CMD_SECURITY_REQ:
878 reason = smp_cmd_security_req(conn, skb);
879 break;
881 case SMP_CMD_PAIRING_CONFIRM:
882 reason = smp_cmd_pairing_confirm(conn, skb);
883 break;
885 case SMP_CMD_PAIRING_RANDOM:
886 reason = smp_cmd_pairing_random(conn, skb);
887 break;
889 case SMP_CMD_ENCRYPT_INFO:
890 reason = smp_cmd_encrypt_info(conn, skb);
891 break;
893 case SMP_CMD_MASTER_IDENT:
894 reason = smp_cmd_master_ident(conn, skb);
895 break;
897 case SMP_CMD_IDENT_INFO:
898 case SMP_CMD_IDENT_ADDR_INFO:
899 case SMP_CMD_SIGN_INFO:
900 /* Just ignored */
901 reason = 0;
902 break;
904 default:
905 BT_DBG("Unknown command code 0x%2.2x", code);
907 reason = SMP_CMD_NOTSUPP;
908 err = -EOPNOTSUPP;
909 goto done;
912 done:
913 if (reason)
914 smp_failure(conn, reason, 1);
916 kfree_skb(skb);
917 return err;
920 int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
922 struct smp_cmd_pairing *req, *rsp;
923 struct smp_chan *smp = conn->smp_chan;
924 __u8 *keydist;
926 BT_DBG("conn %p force %d", conn, force);
928 if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
929 return 0;
931 rsp = (void *) &smp->prsp[1];
933 /* The responder sends its keys first */
934 if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07))
935 return 0;
937 req = (void *) &smp->preq[1];
939 if (conn->hcon->out) {
940 keydist = &rsp->init_key_dist;
941 *keydist &= req->init_key_dist;
942 } else {
943 keydist = &rsp->resp_key_dist;
944 *keydist &= req->resp_key_dist;
948 BT_DBG("keydist 0x%x", *keydist);
950 if (*keydist & SMP_DIST_ENC_KEY) {
951 struct smp_cmd_encrypt_info enc;
952 struct smp_cmd_master_ident ident;
953 struct hci_conn *hcon = conn->hcon;
954 u8 authenticated;
955 __le16 ediv;
957 get_random_bytes(enc.ltk, sizeof(enc.ltk));
958 get_random_bytes(&ediv, sizeof(ediv));
959 get_random_bytes(ident.rand, sizeof(ident.rand));
961 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
963 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
964 hci_add_ltk(conn->hcon->hdev, conn->dst, hcon->dst_type,
965 HCI_SMP_LTK_SLAVE, 1, authenticated,
966 enc.ltk, smp->enc_key_size, ediv, ident.rand);
968 ident.ediv = ediv;
970 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
972 *keydist &= ~SMP_DIST_ENC_KEY;
975 if (*keydist & SMP_DIST_ID_KEY) {
976 struct smp_cmd_ident_addr_info addrinfo;
977 struct smp_cmd_ident_info idinfo;
979 /* Send a dummy key */
980 get_random_bytes(idinfo.irk, sizeof(idinfo.irk));
982 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
984 /* Just public address */
985 memset(&addrinfo, 0, sizeof(addrinfo));
986 bacpy(&addrinfo.bdaddr, conn->src);
988 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
989 &addrinfo);
991 *keydist &= ~SMP_DIST_ID_KEY;
994 if (*keydist & SMP_DIST_SIGN) {
995 struct smp_cmd_sign_info sign;
997 /* Send a dummy key */
998 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1000 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1002 *keydist &= ~SMP_DIST_SIGN;
1005 if (conn->hcon->out || force) {
1006 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
1007 cancel_delayed_work_sync(&conn->security_timer);
1008 smp_chan_destroy(conn);
1011 return 0;