Bluetooth: Set link key generation bit if necessary for LE SC
[deliverable/linux.git] / net / bluetooth / smp.c
... / ...
CommitLineData
1/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
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;
8
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.
17
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.
21*/
22
23#include <linux/crypto.h>
24#include <linux/scatterlist.h>
25#include <crypto/b128ops.h>
26
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
32#include "smp.h"
33
34#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
35
36#define SMP_TIMEOUT msecs_to_jiffies(30000)
37
38#define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
39 0x1f : 0x07)
40#define KEY_DIST_MASK 0x07
41
42enum {
43 SMP_FLAG_TK_VALID,
44 SMP_FLAG_CFM_PENDING,
45 SMP_FLAG_MITM_AUTH,
46 SMP_FLAG_COMPLETE,
47 SMP_FLAG_INITIATOR,
48 SMP_FLAG_SC,
49};
50
51struct smp_chan {
52 struct l2cap_conn *conn;
53 struct delayed_work security_timer;
54 unsigned long allow_cmd; /* Bitmask of allowed commands */
55
56 u8 preq[7]; /* SMP Pairing Request */
57 u8 prsp[7]; /* SMP Pairing Response */
58 u8 prnd[16]; /* SMP Pairing Random (local) */
59 u8 rrnd[16]; /* SMP Pairing Random (remote) */
60 u8 pcnf[16]; /* SMP Pairing Confirm */
61 u8 tk[16]; /* SMP Temporary Key */
62 u8 enc_key_size;
63 u8 remote_key_dist;
64 bdaddr_t id_addr;
65 u8 id_addr_type;
66 u8 irk[16];
67 struct smp_csrk *csrk;
68 struct smp_csrk *slave_csrk;
69 struct smp_ltk *ltk;
70 struct smp_ltk *slave_ltk;
71 struct smp_irk *remote_irk;
72 unsigned long flags;
73
74 struct crypto_blkcipher *tfm_aes;
75};
76
77static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
78{
79 size_t i;
80
81 for (i = 0; i < len; i++)
82 dst[len - 1 - i] = src[i];
83}
84
85static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
86{
87 struct blkcipher_desc desc;
88 struct scatterlist sg;
89 uint8_t tmp[16], data[16];
90 int err;
91
92 if (tfm == NULL) {
93 BT_ERR("tfm %p", tfm);
94 return -EINVAL;
95 }
96
97 desc.tfm = tfm;
98 desc.flags = 0;
99
100 /* The most significant octet of key corresponds to k[0] */
101 swap_buf(k, tmp, 16);
102
103 err = crypto_blkcipher_setkey(tfm, tmp, 16);
104 if (err) {
105 BT_ERR("cipher setkey failed: %d", err);
106 return err;
107 }
108
109 /* Most significant octet of plaintextData corresponds to data[0] */
110 swap_buf(r, data, 16);
111
112 sg_init_one(&sg, data, 16);
113
114 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
115 if (err)
116 BT_ERR("Encrypt data error %d", err);
117
118 /* Most significant octet of encryptedData corresponds to data[0] */
119 swap_buf(data, r, 16);
120
121 return err;
122}
123
124static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
125{
126 u8 _res[16];
127 int err;
128
129 /* r' = padding || r */
130 memcpy(_res, r, 3);
131 memset(_res + 3, 0, 13);
132
133 err = smp_e(tfm, irk, _res);
134 if (err) {
135 BT_ERR("Encrypt error");
136 return err;
137 }
138
139 /* The output of the random address function ah is:
140 * ah(h, r) = e(k, r') mod 2^24
141 * The output of the security function e is then truncated to 24 bits
142 * by taking the least significant 24 bits of the output of e as the
143 * result of ah.
144 */
145 memcpy(res, _res, 3);
146
147 return 0;
148}
149
150bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
151{
152 struct l2cap_chan *chan = hdev->smp_data;
153 struct crypto_blkcipher *tfm;
154 u8 hash[3];
155 int err;
156
157 if (!chan || !chan->data)
158 return false;
159
160 tfm = chan->data;
161
162 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
163
164 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
165 if (err)
166 return false;
167
168 return !memcmp(bdaddr->b, hash, 3);
169}
170
171int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
172{
173 struct l2cap_chan *chan = hdev->smp_data;
174 struct crypto_blkcipher *tfm;
175 int err;
176
177 if (!chan || !chan->data)
178 return -EOPNOTSUPP;
179
180 tfm = chan->data;
181
182 get_random_bytes(&rpa->b[3], 3);
183
184 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
185 rpa->b[5] |= 0x40; /* Set second most significant bit */
186
187 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
188 if (err < 0)
189 return err;
190
191 BT_DBG("RPA %pMR", rpa);
192
193 return 0;
194}
195
196static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
197 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
198 bdaddr_t *ra, u8 res[16])
199{
200 u8 p1[16], p2[16];
201 int err;
202
203 memset(p1, 0, 16);
204
205 /* p1 = pres || preq || _rat || _iat */
206 p1[0] = _iat;
207 p1[1] = _rat;
208 memcpy(p1 + 2, preq, 7);
209 memcpy(p1 + 9, pres, 7);
210
211 /* p2 = padding || ia || ra */
212 memcpy(p2, ra, 6);
213 memcpy(p2 + 6, ia, 6);
214 memset(p2 + 12, 0, 4);
215
216 /* res = r XOR p1 */
217 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
218
219 /* res = e(k, res) */
220 err = smp_e(tfm_aes, k, res);
221 if (err) {
222 BT_ERR("Encrypt data error");
223 return err;
224 }
225
226 /* res = res XOR p2 */
227 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
228
229 /* res = e(k, res) */
230 err = smp_e(tfm_aes, k, res);
231 if (err)
232 BT_ERR("Encrypt data error");
233
234 return err;
235}
236
237static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
238 u8 r2[16], u8 _r[16])
239{
240 int err;
241
242 /* Just least significant octets from r1 and r2 are considered */
243 memcpy(_r, r2, 8);
244 memcpy(_r + 8, r1, 8);
245
246 err = smp_e(tfm_aes, k, _r);
247 if (err)
248 BT_ERR("Encrypt data error");
249
250 return err;
251}
252
253static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
254{
255 struct l2cap_chan *chan = conn->smp;
256 struct smp_chan *smp;
257 struct kvec iv[2];
258 struct msghdr msg;
259
260 if (!chan)
261 return;
262
263 BT_DBG("code 0x%2.2x", code);
264
265 iv[0].iov_base = &code;
266 iv[0].iov_len = 1;
267
268 iv[1].iov_base = data;
269 iv[1].iov_len = len;
270
271 memset(&msg, 0, sizeof(msg));
272
273 msg.msg_iov = (struct iovec *) &iv;
274 msg.msg_iovlen = 2;
275
276 l2cap_chan_send(chan, &msg, 1 + len);
277
278 if (!chan->data)
279 return;
280
281 smp = chan->data;
282
283 cancel_delayed_work_sync(&smp->security_timer);
284 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
285}
286
287static u8 authreq_to_seclevel(u8 authreq)
288{
289 if (authreq & SMP_AUTH_MITM) {
290 if (authreq & SMP_AUTH_SC)
291 return BT_SECURITY_FIPS;
292 else
293 return BT_SECURITY_HIGH;
294 } else {
295 return BT_SECURITY_MEDIUM;
296 }
297}
298
299static __u8 seclevel_to_authreq(__u8 sec_level)
300{
301 switch (sec_level) {
302 case BT_SECURITY_FIPS:
303 case BT_SECURITY_HIGH:
304 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
305 case BT_SECURITY_MEDIUM:
306 return SMP_AUTH_BONDING;
307 default:
308 return SMP_AUTH_NONE;
309 }
310}
311
312static void build_pairing_cmd(struct l2cap_conn *conn,
313 struct smp_cmd_pairing *req,
314 struct smp_cmd_pairing *rsp, __u8 authreq)
315{
316 struct l2cap_chan *chan = conn->smp;
317 struct smp_chan *smp = chan->data;
318 struct hci_conn *hcon = conn->hcon;
319 struct hci_dev *hdev = hcon->hdev;
320 u8 local_dist = 0, remote_dist = 0;
321
322 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
323 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
324 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
325 authreq |= SMP_AUTH_BONDING;
326 } else {
327 authreq &= ~SMP_AUTH_BONDING;
328 }
329
330 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
331 remote_dist |= SMP_DIST_ID_KEY;
332
333 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
334 local_dist |= SMP_DIST_ID_KEY;
335
336 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
337 if ((authreq & SMP_AUTH_SC) &&
338 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
339 local_dist |= SMP_DIST_LINK_KEY;
340 remote_dist |= SMP_DIST_LINK_KEY;
341 }
342 } else {
343 authreq &= ~SMP_AUTH_SC;
344 }
345
346 if (rsp == NULL) {
347 req->io_capability = conn->hcon->io_capability;
348 req->oob_flag = SMP_OOB_NOT_PRESENT;
349 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
350 req->init_key_dist = local_dist;
351 req->resp_key_dist = remote_dist;
352 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
353
354 smp->remote_key_dist = remote_dist;
355 return;
356 }
357
358 rsp->io_capability = conn->hcon->io_capability;
359 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
360 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
361 rsp->init_key_dist = req->init_key_dist & remote_dist;
362 rsp->resp_key_dist = req->resp_key_dist & local_dist;
363 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
364
365 smp->remote_key_dist = rsp->init_key_dist;
366}
367
368static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
369{
370 struct l2cap_chan *chan = conn->smp;
371 struct smp_chan *smp = chan->data;
372
373 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
374 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
375 return SMP_ENC_KEY_SIZE;
376
377 smp->enc_key_size = max_key_size;
378
379 return 0;
380}
381
382static void smp_chan_destroy(struct l2cap_conn *conn)
383{
384 struct l2cap_chan *chan = conn->smp;
385 struct smp_chan *smp = chan->data;
386 bool complete;
387
388 BUG_ON(!smp);
389
390 cancel_delayed_work_sync(&smp->security_timer);
391
392 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
393 mgmt_smp_complete(conn->hcon, complete);
394
395 kfree(smp->csrk);
396 kfree(smp->slave_csrk);
397
398 crypto_free_blkcipher(smp->tfm_aes);
399
400 /* If pairing failed clean up any keys we might have */
401 if (!complete) {
402 if (smp->ltk) {
403 list_del_rcu(&smp->ltk->list);
404 kfree_rcu(smp->ltk, rcu);
405 }
406
407 if (smp->slave_ltk) {
408 list_del_rcu(&smp->slave_ltk->list);
409 kfree_rcu(smp->slave_ltk, rcu);
410 }
411
412 if (smp->remote_irk) {
413 list_del_rcu(&smp->remote_irk->list);
414 kfree_rcu(smp->remote_irk, rcu);
415 }
416 }
417
418 chan->data = NULL;
419 kfree(smp);
420 hci_conn_drop(conn->hcon);
421}
422
423static void smp_failure(struct l2cap_conn *conn, u8 reason)
424{
425 struct hci_conn *hcon = conn->hcon;
426 struct l2cap_chan *chan = conn->smp;
427
428 if (reason)
429 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
430 &reason);
431
432 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
433 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
434
435 if (chan->data)
436 smp_chan_destroy(conn);
437}
438
439#define JUST_WORKS 0x00
440#define JUST_CFM 0x01
441#define REQ_PASSKEY 0x02
442#define CFM_PASSKEY 0x03
443#define REQ_OOB 0x04
444#define OVERLAP 0xFF
445
446static const u8 gen_method[5][5] = {
447 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
448 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
449 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
450 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
451 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
452};
453
454static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
455{
456 /* If either side has unknown io_caps, use JUST_CFM (which gets
457 * converted later to JUST_WORKS if we're initiators.
458 */
459 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
460 remote_io > SMP_IO_KEYBOARD_DISPLAY)
461 return JUST_CFM;
462
463 return gen_method[remote_io][local_io];
464}
465
466static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
467 u8 local_io, u8 remote_io)
468{
469 struct hci_conn *hcon = conn->hcon;
470 struct l2cap_chan *chan = conn->smp;
471 struct smp_chan *smp = chan->data;
472 u8 method;
473 u32 passkey = 0;
474 int ret = 0;
475
476 /* Initialize key for JUST WORKS */
477 memset(smp->tk, 0, sizeof(smp->tk));
478 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
479
480 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
481
482 /* If neither side wants MITM, either "just" confirm an incoming
483 * request or use just-works for outgoing ones. The JUST_CFM
484 * will be converted to JUST_WORKS if necessary later in this
485 * function. If either side has MITM look up the method from the
486 * table.
487 */
488 if (!(auth & SMP_AUTH_MITM))
489 method = JUST_CFM;
490 else
491 method = get_auth_method(smp, local_io, remote_io);
492
493 /* Don't confirm locally initiated pairing attempts */
494 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
495 method = JUST_WORKS;
496
497 /* Don't bother user space with no IO capabilities */
498 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
499 method = JUST_WORKS;
500
501 /* If Just Works, Continue with Zero TK */
502 if (method == JUST_WORKS) {
503 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
504 return 0;
505 }
506
507 /* Not Just Works/Confirm results in MITM Authentication */
508 if (method != JUST_CFM) {
509 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
510 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
511 hcon->pending_sec_level = BT_SECURITY_HIGH;
512 }
513
514 /* If both devices have Keyoard-Display I/O, the master
515 * Confirms and the slave Enters the passkey.
516 */
517 if (method == OVERLAP) {
518 if (hcon->role == HCI_ROLE_MASTER)
519 method = CFM_PASSKEY;
520 else
521 method = REQ_PASSKEY;
522 }
523
524 /* Generate random passkey. */
525 if (method == CFM_PASSKEY) {
526 memset(smp->tk, 0, sizeof(smp->tk));
527 get_random_bytes(&passkey, sizeof(passkey));
528 passkey %= 1000000;
529 put_unaligned_le32(passkey, smp->tk);
530 BT_DBG("PassKey: %d", passkey);
531 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
532 }
533
534 if (method == REQ_PASSKEY)
535 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
536 hcon->type, hcon->dst_type);
537 else if (method == JUST_CFM)
538 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
539 hcon->type, hcon->dst_type,
540 passkey, 1);
541 else
542 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
543 hcon->type, hcon->dst_type,
544 passkey, 0);
545
546 return ret;
547}
548
549static u8 smp_confirm(struct smp_chan *smp)
550{
551 struct l2cap_conn *conn = smp->conn;
552 struct smp_cmd_pairing_confirm cp;
553 int ret;
554
555 BT_DBG("conn %p", conn);
556
557 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
558 conn->hcon->init_addr_type, &conn->hcon->init_addr,
559 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
560 cp.confirm_val);
561 if (ret)
562 return SMP_UNSPECIFIED;
563
564 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
565
566 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
567
568 if (conn->hcon->out)
569 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
570 else
571 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
572
573 return 0;
574}
575
576static u8 smp_random(struct smp_chan *smp)
577{
578 struct l2cap_conn *conn = smp->conn;
579 struct hci_conn *hcon = conn->hcon;
580 u8 confirm[16];
581 int ret;
582
583 if (IS_ERR_OR_NULL(smp->tfm_aes))
584 return SMP_UNSPECIFIED;
585
586 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
587
588 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
589 hcon->init_addr_type, &hcon->init_addr,
590 hcon->resp_addr_type, &hcon->resp_addr, confirm);
591 if (ret)
592 return SMP_UNSPECIFIED;
593
594 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
595 BT_ERR("Pairing failed (confirmation values mismatch)");
596 return SMP_CONFIRM_FAILED;
597 }
598
599 if (hcon->out) {
600 u8 stk[16];
601 __le64 rand = 0;
602 __le16 ediv = 0;
603
604 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
605
606 memset(stk + smp->enc_key_size, 0,
607 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
608
609 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
610 return SMP_UNSPECIFIED;
611
612 hci_le_start_enc(hcon, ediv, rand, stk);
613 hcon->enc_key_size = smp->enc_key_size;
614 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
615 } else {
616 u8 stk[16], auth;
617 __le64 rand = 0;
618 __le16 ediv = 0;
619
620 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
621 smp->prnd);
622
623 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
624
625 memset(stk + smp->enc_key_size, 0,
626 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
627
628 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
629 auth = 1;
630 else
631 auth = 0;
632
633 /* Even though there's no _SLAVE suffix this is the
634 * slave STK we're adding for later lookup (the master
635 * STK never needs to be stored).
636 */
637 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
638 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
639 }
640
641 return 0;
642}
643
644static void smp_notify_keys(struct l2cap_conn *conn)
645{
646 struct l2cap_chan *chan = conn->smp;
647 struct smp_chan *smp = chan->data;
648 struct hci_conn *hcon = conn->hcon;
649 struct hci_dev *hdev = hcon->hdev;
650 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
651 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
652 bool persistent;
653
654 if (smp->remote_irk) {
655 mgmt_new_irk(hdev, smp->remote_irk);
656 /* Now that user space can be considered to know the
657 * identity address track the connection based on it
658 * from now on.
659 */
660 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
661 hcon->dst_type = smp->remote_irk->addr_type;
662 queue_work(hdev->workqueue, &conn->id_addr_update_work);
663
664 /* When receiving an indentity resolving key for
665 * a remote device that does not use a resolvable
666 * private address, just remove the key so that
667 * it is possible to use the controller white
668 * list for scanning.
669 *
670 * Userspace will have been told to not store
671 * this key at this point. So it is safe to
672 * just remove it.
673 */
674 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
675 list_del_rcu(&smp->remote_irk->list);
676 kfree_rcu(smp->remote_irk, rcu);
677 smp->remote_irk = NULL;
678 }
679 }
680
681 /* The LTKs and CSRKs should be persistent only if both sides
682 * had the bonding bit set in their authentication requests.
683 */
684 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
685
686 if (smp->csrk) {
687 smp->csrk->bdaddr_type = hcon->dst_type;
688 bacpy(&smp->csrk->bdaddr, &hcon->dst);
689 mgmt_new_csrk(hdev, smp->csrk, persistent);
690 }
691
692 if (smp->slave_csrk) {
693 smp->slave_csrk->bdaddr_type = hcon->dst_type;
694 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
695 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
696 }
697
698 if (smp->ltk) {
699 smp->ltk->bdaddr_type = hcon->dst_type;
700 bacpy(&smp->ltk->bdaddr, &hcon->dst);
701 mgmt_new_ltk(hdev, smp->ltk, persistent);
702 }
703
704 if (smp->slave_ltk) {
705 smp->slave_ltk->bdaddr_type = hcon->dst_type;
706 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
707 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
708 }
709}
710
711static void smp_allow_key_dist(struct smp_chan *smp)
712{
713 /* Allow the first expected phase 3 PDU. The rest of the PDUs
714 * will be allowed in each PDU handler to ensure we receive
715 * them in the correct order.
716 */
717 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
718 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
719 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
720 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
721 else if (smp->remote_key_dist & SMP_DIST_SIGN)
722 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
723}
724
725static void smp_distribute_keys(struct smp_chan *smp)
726{
727 struct smp_cmd_pairing *req, *rsp;
728 struct l2cap_conn *conn = smp->conn;
729 struct hci_conn *hcon = conn->hcon;
730 struct hci_dev *hdev = hcon->hdev;
731 __u8 *keydist;
732
733 BT_DBG("conn %p", conn);
734
735 rsp = (void *) &smp->prsp[1];
736
737 /* The responder sends its keys first */
738 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
739 smp_allow_key_dist(smp);
740 return;
741 }
742
743 req = (void *) &smp->preq[1];
744
745 if (hcon->out) {
746 keydist = &rsp->init_key_dist;
747 *keydist &= req->init_key_dist;
748 } else {
749 keydist = &rsp->resp_key_dist;
750 *keydist &= req->resp_key_dist;
751 }
752
753 BT_DBG("keydist 0x%x", *keydist);
754
755 if (*keydist & SMP_DIST_ENC_KEY) {
756 struct smp_cmd_encrypt_info enc;
757 struct smp_cmd_master_ident ident;
758 struct smp_ltk *ltk;
759 u8 authenticated;
760 __le16 ediv;
761 __le64 rand;
762
763 get_random_bytes(enc.ltk, sizeof(enc.ltk));
764 get_random_bytes(&ediv, sizeof(ediv));
765 get_random_bytes(&rand, sizeof(rand));
766
767 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
768
769 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
770 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
771 SMP_LTK_SLAVE, authenticated, enc.ltk,
772 smp->enc_key_size, ediv, rand);
773 smp->slave_ltk = ltk;
774
775 ident.ediv = ediv;
776 ident.rand = rand;
777
778 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
779
780 *keydist &= ~SMP_DIST_ENC_KEY;
781 }
782
783 if (*keydist & SMP_DIST_ID_KEY) {
784 struct smp_cmd_ident_addr_info addrinfo;
785 struct smp_cmd_ident_info idinfo;
786
787 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
788
789 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
790
791 /* The hci_conn contains the local identity address
792 * after the connection has been established.
793 *
794 * This is true even when the connection has been
795 * established using a resolvable random address.
796 */
797 bacpy(&addrinfo.bdaddr, &hcon->src);
798 addrinfo.addr_type = hcon->src_type;
799
800 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
801 &addrinfo);
802
803 *keydist &= ~SMP_DIST_ID_KEY;
804 }
805
806 if (*keydist & SMP_DIST_SIGN) {
807 struct smp_cmd_sign_info sign;
808 struct smp_csrk *csrk;
809
810 /* Generate a new random key */
811 get_random_bytes(sign.csrk, sizeof(sign.csrk));
812
813 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
814 if (csrk) {
815 csrk->master = 0x00;
816 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
817 }
818 smp->slave_csrk = csrk;
819
820 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
821
822 *keydist &= ~SMP_DIST_SIGN;
823 }
824
825 /* If there are still keys to be received wait for them */
826 if (smp->remote_key_dist & KEY_DIST_MASK) {
827 smp_allow_key_dist(smp);
828 return;
829 }
830
831 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
832 smp_notify_keys(conn);
833
834 smp_chan_destroy(conn);
835}
836
837static void smp_timeout(struct work_struct *work)
838{
839 struct smp_chan *smp = container_of(work, struct smp_chan,
840 security_timer.work);
841 struct l2cap_conn *conn = smp->conn;
842
843 BT_DBG("conn %p", conn);
844
845 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
846}
847
848static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
849{
850 struct l2cap_chan *chan = conn->smp;
851 struct smp_chan *smp;
852
853 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
854 if (!smp)
855 return NULL;
856
857 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
858 if (IS_ERR(smp->tfm_aes)) {
859 BT_ERR("Unable to create ECB crypto context");
860 kfree(smp);
861 return NULL;
862 }
863
864 smp->conn = conn;
865 chan->data = smp;
866
867 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
868
869 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
870
871 hci_conn_hold(conn->hcon);
872
873 return smp;
874}
875
876int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
877{
878 struct l2cap_conn *conn = hcon->l2cap_data;
879 struct l2cap_chan *chan;
880 struct smp_chan *smp;
881 u32 value;
882 int err;
883
884 BT_DBG("");
885
886 if (!conn)
887 return -ENOTCONN;
888
889 chan = conn->smp;
890 if (!chan)
891 return -ENOTCONN;
892
893 l2cap_chan_lock(chan);
894 if (!chan->data) {
895 err = -ENOTCONN;
896 goto unlock;
897 }
898
899 smp = chan->data;
900
901 switch (mgmt_op) {
902 case MGMT_OP_USER_PASSKEY_REPLY:
903 value = le32_to_cpu(passkey);
904 memset(smp->tk, 0, sizeof(smp->tk));
905 BT_DBG("PassKey: %d", value);
906 put_unaligned_le32(value, smp->tk);
907 /* Fall Through */
908 case MGMT_OP_USER_CONFIRM_REPLY:
909 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
910 break;
911 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
912 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
913 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
914 err = 0;
915 goto unlock;
916 default:
917 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
918 err = -EOPNOTSUPP;
919 goto unlock;
920 }
921
922 err = 0;
923
924 /* If it is our turn to send Pairing Confirm, do so now */
925 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
926 u8 rsp = smp_confirm(smp);
927 if (rsp)
928 smp_failure(conn, rsp);
929 }
930
931unlock:
932 l2cap_chan_unlock(chan);
933 return err;
934}
935
936static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
937{
938 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
939 struct l2cap_chan *chan = conn->smp;
940 struct hci_dev *hdev = conn->hcon->hdev;
941 struct smp_chan *smp;
942 u8 key_size, auth, sec_level;
943 int ret;
944
945 BT_DBG("conn %p", conn);
946
947 if (skb->len < sizeof(*req))
948 return SMP_INVALID_PARAMS;
949
950 if (conn->hcon->role != HCI_ROLE_SLAVE)
951 return SMP_CMD_NOTSUPP;
952
953 if (!chan->data)
954 smp = smp_chan_create(conn);
955 else
956 smp = chan->data;
957
958 if (!smp)
959 return SMP_UNSPECIFIED;
960
961 /* We didn't start the pairing, so match remote */
962 auth = req->auth_req & AUTH_REQ_MASK(hdev);
963
964 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
965 (auth & SMP_AUTH_BONDING))
966 return SMP_PAIRING_NOTSUPP;
967
968 smp->preq[0] = SMP_CMD_PAIRING_REQ;
969 memcpy(&smp->preq[1], req, sizeof(*req));
970 skb_pull(skb, sizeof(*req));
971
972 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
973 sec_level = BT_SECURITY_MEDIUM;
974 else
975 sec_level = authreq_to_seclevel(auth);
976
977 if (sec_level > conn->hcon->pending_sec_level)
978 conn->hcon->pending_sec_level = sec_level;
979
980 /* If we need MITM check that it can be achieved */
981 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
982 u8 method;
983
984 method = get_auth_method(smp, conn->hcon->io_capability,
985 req->io_capability);
986 if (method == JUST_WORKS || method == JUST_CFM)
987 return SMP_AUTH_REQUIREMENTS;
988 }
989
990 build_pairing_cmd(conn, req, &rsp, auth);
991
992 if (rsp.auth_req & SMP_AUTH_SC)
993 set_bit(SMP_FLAG_SC, &smp->flags);
994
995 key_size = min(req->max_key_size, rsp.max_key_size);
996 if (check_enc_key_size(conn, key_size))
997 return SMP_ENC_KEY_SIZE;
998
999 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1000
1001 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1002 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1003
1004 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1005 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1006
1007 /* Request setup of TK */
1008 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1009 if (ret)
1010 return SMP_UNSPECIFIED;
1011
1012 return 0;
1013}
1014
1015static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
1016{
1017 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
1018 struct l2cap_chan *chan = conn->smp;
1019 struct smp_chan *smp = chan->data;
1020 struct hci_dev *hdev = conn->hcon->hdev;
1021 u8 key_size, auth;
1022 int ret;
1023
1024 BT_DBG("conn %p", conn);
1025
1026 if (skb->len < sizeof(*rsp))
1027 return SMP_INVALID_PARAMS;
1028
1029 if (conn->hcon->role != HCI_ROLE_MASTER)
1030 return SMP_CMD_NOTSUPP;
1031
1032 skb_pull(skb, sizeof(*rsp));
1033
1034 req = (void *) &smp->preq[1];
1035
1036 key_size = min(req->max_key_size, rsp->max_key_size);
1037 if (check_enc_key_size(conn, key_size))
1038 return SMP_ENC_KEY_SIZE;
1039
1040 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
1041
1042 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1043 set_bit(SMP_FLAG_SC, &smp->flags);
1044 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1045 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
1046
1047 /* If we need MITM check that it can be achieved */
1048 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1049 u8 method;
1050
1051 method = get_auth_method(smp, req->io_capability,
1052 rsp->io_capability);
1053 if (method == JUST_WORKS || method == JUST_CFM)
1054 return SMP_AUTH_REQUIREMENTS;
1055 }
1056
1057 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1058
1059 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1060 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1061
1062 /* Update remote key distribution in case the remote cleared
1063 * some bits that we had enabled in our request.
1064 */
1065 smp->remote_key_dist &= rsp->resp_key_dist;
1066
1067 auth |= req->auth_req;
1068
1069 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
1070 if (ret)
1071 return SMP_UNSPECIFIED;
1072
1073 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1074
1075 /* Can't compose response until we have been confirmed */
1076 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1077 return smp_confirm(smp);
1078
1079 return 0;
1080}
1081
1082static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
1083{
1084 struct l2cap_chan *chan = conn->smp;
1085 struct smp_chan *smp = chan->data;
1086
1087 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1088
1089 if (skb->len < sizeof(smp->pcnf))
1090 return SMP_INVALID_PARAMS;
1091
1092 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1093 skb_pull(skb, sizeof(smp->pcnf));
1094
1095 if (conn->hcon->out) {
1096 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1097 smp->prnd);
1098 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1099 return 0;
1100 }
1101
1102 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1103 return smp_confirm(smp);
1104 else
1105 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1106
1107 return 0;
1108}
1109
1110static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
1111{
1112 struct l2cap_chan *chan = conn->smp;
1113 struct smp_chan *smp = chan->data;
1114
1115 BT_DBG("conn %p", conn);
1116
1117 if (skb->len < sizeof(smp->rrnd))
1118 return SMP_INVALID_PARAMS;
1119
1120 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
1121 skb_pull(skb, sizeof(smp->rrnd));
1122
1123 return smp_random(smp);
1124}
1125
1126static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
1127{
1128 struct smp_ltk *key;
1129 struct hci_conn *hcon = conn->hcon;
1130
1131 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
1132 if (!key)
1133 return false;
1134
1135 if (smp_ltk_sec_level(key) < sec_level)
1136 return false;
1137
1138 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
1139 return true;
1140
1141 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1142 hcon->enc_key_size = key->enc_size;
1143
1144 /* We never store STKs for master role, so clear this flag */
1145 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1146
1147 return true;
1148}
1149
1150bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1151 enum smp_key_pref key_pref)
1152{
1153 if (sec_level == BT_SECURITY_LOW)
1154 return true;
1155
1156 /* If we're encrypted with an STK but the caller prefers using
1157 * LTK claim insufficient security. This way we allow the
1158 * connection to be re-encrypted with an LTK, even if the LTK
1159 * provides the same level of security. Only exception is if we
1160 * don't have an LTK (e.g. because of key distribution bits).
1161 */
1162 if (key_pref == SMP_USE_LTK &&
1163 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
1164 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
1165 return false;
1166
1167 if (hcon->sec_level >= sec_level)
1168 return true;
1169
1170 return false;
1171}
1172
1173static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
1174{
1175 struct smp_cmd_security_req *rp = (void *) skb->data;
1176 struct smp_cmd_pairing cp;
1177 struct hci_conn *hcon = conn->hcon;
1178 struct hci_dev *hdev = hcon->hdev;
1179 struct smp_chan *smp;
1180 u8 sec_level, auth;
1181
1182 BT_DBG("conn %p", conn);
1183
1184 if (skb->len < sizeof(*rp))
1185 return SMP_INVALID_PARAMS;
1186
1187 if (hcon->role != HCI_ROLE_MASTER)
1188 return SMP_CMD_NOTSUPP;
1189
1190 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
1191
1192 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
1193 sec_level = BT_SECURITY_MEDIUM;
1194 else
1195 sec_level = authreq_to_seclevel(auth);
1196
1197 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
1198 return 0;
1199
1200 if (sec_level > hcon->pending_sec_level)
1201 hcon->pending_sec_level = sec_level;
1202
1203 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1204 return 0;
1205
1206 smp = smp_chan_create(conn);
1207 if (!smp)
1208 return SMP_UNSPECIFIED;
1209
1210 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
1211 (auth & SMP_AUTH_BONDING))
1212 return SMP_PAIRING_NOTSUPP;
1213
1214 skb_pull(skb, sizeof(*rp));
1215
1216 memset(&cp, 0, sizeof(cp));
1217 build_pairing_cmd(conn, &cp, NULL, auth);
1218
1219 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1220 memcpy(&smp->preq[1], &cp, sizeof(cp));
1221
1222 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
1223 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
1224
1225 return 0;
1226}
1227
1228int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
1229{
1230 struct l2cap_conn *conn = hcon->l2cap_data;
1231 struct l2cap_chan *chan;
1232 struct smp_chan *smp;
1233 __u8 authreq;
1234 int ret;
1235
1236 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1237
1238 /* This may be NULL if there's an unexpected disconnection */
1239 if (!conn)
1240 return 1;
1241
1242 chan = conn->smp;
1243
1244 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
1245 return 1;
1246
1247 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
1248 return 1;
1249
1250 if (sec_level > hcon->pending_sec_level)
1251 hcon->pending_sec_level = sec_level;
1252
1253 if (hcon->role == HCI_ROLE_MASTER)
1254 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1255 return 0;
1256
1257 l2cap_chan_lock(chan);
1258
1259 /* If SMP is already in progress ignore this request */
1260 if (chan->data) {
1261 ret = 0;
1262 goto unlock;
1263 }
1264
1265 smp = smp_chan_create(conn);
1266 if (!smp) {
1267 ret = 1;
1268 goto unlock;
1269 }
1270
1271 authreq = seclevel_to_authreq(sec_level);
1272
1273 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
1274 authreq |= SMP_AUTH_SC;
1275
1276 /* Require MITM if IO Capability allows or the security level
1277 * requires it.
1278 */
1279 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
1280 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
1281 authreq |= SMP_AUTH_MITM;
1282
1283 if (hcon->role == HCI_ROLE_MASTER) {
1284 struct smp_cmd_pairing cp;
1285
1286 build_pairing_cmd(conn, &cp, NULL, authreq);
1287 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1288 memcpy(&smp->preq[1], &cp, sizeof(cp));
1289
1290 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
1291 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
1292 } else {
1293 struct smp_cmd_security_req cp;
1294 cp.auth_req = authreq;
1295 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
1296 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
1297 }
1298
1299 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
1300 ret = 0;
1301
1302unlock:
1303 l2cap_chan_unlock(chan);
1304 return ret;
1305}
1306
1307static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1308{
1309 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
1310 struct l2cap_chan *chan = conn->smp;
1311 struct smp_chan *smp = chan->data;
1312
1313 BT_DBG("conn %p", conn);
1314
1315 if (skb->len < sizeof(*rp))
1316 return SMP_INVALID_PARAMS;
1317
1318 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
1319
1320 skb_pull(skb, sizeof(*rp));
1321
1322 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
1323
1324 return 0;
1325}
1326
1327static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1328{
1329 struct smp_cmd_master_ident *rp = (void *) skb->data;
1330 struct l2cap_chan *chan = conn->smp;
1331 struct smp_chan *smp = chan->data;
1332 struct hci_dev *hdev = conn->hcon->hdev;
1333 struct hci_conn *hcon = conn->hcon;
1334 struct smp_ltk *ltk;
1335 u8 authenticated;
1336
1337 BT_DBG("conn %p", conn);
1338
1339 if (skb->len < sizeof(*rp))
1340 return SMP_INVALID_PARAMS;
1341
1342 /* Mark the information as received */
1343 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1344
1345 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1346 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1347 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1348 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1349
1350 skb_pull(skb, sizeof(*rp));
1351
1352 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
1353 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
1354 authenticated, smp->tk, smp->enc_key_size,
1355 rp->ediv, rp->rand);
1356 smp->ltk = ltk;
1357 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1358 smp_distribute_keys(smp);
1359
1360 return 0;
1361}
1362
1363static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1364{
1365 struct smp_cmd_ident_info *info = (void *) skb->data;
1366 struct l2cap_chan *chan = conn->smp;
1367 struct smp_chan *smp = chan->data;
1368
1369 BT_DBG("");
1370
1371 if (skb->len < sizeof(*info))
1372 return SMP_INVALID_PARAMS;
1373
1374 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
1375
1376 skb_pull(skb, sizeof(*info));
1377
1378 memcpy(smp->irk, info->irk, 16);
1379
1380 return 0;
1381}
1382
1383static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1384 struct sk_buff *skb)
1385{
1386 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
1387 struct l2cap_chan *chan = conn->smp;
1388 struct smp_chan *smp = chan->data;
1389 struct hci_conn *hcon = conn->hcon;
1390 bdaddr_t rpa;
1391
1392 BT_DBG("");
1393
1394 if (skb->len < sizeof(*info))
1395 return SMP_INVALID_PARAMS;
1396
1397 /* Mark the information as received */
1398 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1399
1400 if (smp->remote_key_dist & SMP_DIST_SIGN)
1401 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1402
1403 skb_pull(skb, sizeof(*info));
1404
1405 /* Strictly speaking the Core Specification (4.1) allows sending
1406 * an empty address which would force us to rely on just the IRK
1407 * as "identity information". However, since such
1408 * implementations are not known of and in order to not over
1409 * complicate our implementation, simply pretend that we never
1410 * received an IRK for such a device.
1411 */
1412 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1413 BT_ERR("Ignoring IRK with no identity address");
1414 goto distribute;
1415 }
1416
1417 bacpy(&smp->id_addr, &info->bdaddr);
1418 smp->id_addr_type = info->addr_type;
1419
1420 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1421 bacpy(&rpa, &hcon->dst);
1422 else
1423 bacpy(&rpa, BDADDR_ANY);
1424
1425 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1426 smp->id_addr_type, smp->irk, &rpa);
1427
1428distribute:
1429 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1430 smp_distribute_keys(smp);
1431
1432 return 0;
1433}
1434
1435static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1436{
1437 struct smp_cmd_sign_info *rp = (void *) skb->data;
1438 struct l2cap_chan *chan = conn->smp;
1439 struct smp_chan *smp = chan->data;
1440 struct smp_csrk *csrk;
1441
1442 BT_DBG("conn %p", conn);
1443
1444 if (skb->len < sizeof(*rp))
1445 return SMP_INVALID_PARAMS;
1446
1447 /* Mark the information as received */
1448 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1449
1450 skb_pull(skb, sizeof(*rp));
1451
1452 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1453 if (csrk) {
1454 csrk->master = 0x01;
1455 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1456 }
1457 smp->csrk = csrk;
1458 smp_distribute_keys(smp);
1459
1460 return 0;
1461}
1462
1463static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
1464{
1465 struct l2cap_conn *conn = chan->conn;
1466 struct hci_conn *hcon = conn->hcon;
1467 struct smp_chan *smp;
1468 __u8 code, reason;
1469 int err = 0;
1470
1471 if (hcon->type != LE_LINK) {
1472 kfree_skb(skb);
1473 return 0;
1474 }
1475
1476 if (skb->len < 1)
1477 return -EILSEQ;
1478
1479 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
1480 reason = SMP_PAIRING_NOTSUPP;
1481 goto done;
1482 }
1483
1484 code = skb->data[0];
1485 skb_pull(skb, sizeof(code));
1486
1487 smp = chan->data;
1488
1489 if (code > SMP_CMD_MAX)
1490 goto drop;
1491
1492 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
1493 goto drop;
1494
1495 /* If we don't have a context the only allowed commands are
1496 * pairing request and security request.
1497 */
1498 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1499 goto drop;
1500
1501 switch (code) {
1502 case SMP_CMD_PAIRING_REQ:
1503 reason = smp_cmd_pairing_req(conn, skb);
1504 break;
1505
1506 case SMP_CMD_PAIRING_FAIL:
1507 smp_failure(conn, 0);
1508 err = -EPERM;
1509 break;
1510
1511 case SMP_CMD_PAIRING_RSP:
1512 reason = smp_cmd_pairing_rsp(conn, skb);
1513 break;
1514
1515 case SMP_CMD_SECURITY_REQ:
1516 reason = smp_cmd_security_req(conn, skb);
1517 break;
1518
1519 case SMP_CMD_PAIRING_CONFIRM:
1520 reason = smp_cmd_pairing_confirm(conn, skb);
1521 break;
1522
1523 case SMP_CMD_PAIRING_RANDOM:
1524 reason = smp_cmd_pairing_random(conn, skb);
1525 break;
1526
1527 case SMP_CMD_ENCRYPT_INFO:
1528 reason = smp_cmd_encrypt_info(conn, skb);
1529 break;
1530
1531 case SMP_CMD_MASTER_IDENT:
1532 reason = smp_cmd_master_ident(conn, skb);
1533 break;
1534
1535 case SMP_CMD_IDENT_INFO:
1536 reason = smp_cmd_ident_info(conn, skb);
1537 break;
1538
1539 case SMP_CMD_IDENT_ADDR_INFO:
1540 reason = smp_cmd_ident_addr_info(conn, skb);
1541 break;
1542
1543 case SMP_CMD_SIGN_INFO:
1544 reason = smp_cmd_sign_info(conn, skb);
1545 break;
1546
1547 default:
1548 BT_DBG("Unknown command code 0x%2.2x", code);
1549 reason = SMP_CMD_NOTSUPP;
1550 goto done;
1551 }
1552
1553done:
1554 if (!err) {
1555 if (reason)
1556 smp_failure(conn, reason);
1557 kfree_skb(skb);
1558 }
1559
1560 return err;
1561
1562drop:
1563 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
1564 code, &hcon->dst);
1565 kfree_skb(skb);
1566 return 0;
1567}
1568
1569static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1570{
1571 struct l2cap_conn *conn = chan->conn;
1572
1573 BT_DBG("chan %p", chan);
1574
1575 if (chan->data)
1576 smp_chan_destroy(conn);
1577
1578 conn->smp = NULL;
1579 l2cap_chan_put(chan);
1580}
1581
1582static void smp_resume_cb(struct l2cap_chan *chan)
1583{
1584 struct smp_chan *smp = chan->data;
1585 struct l2cap_conn *conn = chan->conn;
1586 struct hci_conn *hcon = conn->hcon;
1587
1588 BT_DBG("chan %p", chan);
1589
1590 if (!smp)
1591 return;
1592
1593 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1594 return;
1595
1596 cancel_delayed_work(&smp->security_timer);
1597
1598 smp_distribute_keys(smp);
1599}
1600
1601static void smp_ready_cb(struct l2cap_chan *chan)
1602{
1603 struct l2cap_conn *conn = chan->conn;
1604
1605 BT_DBG("chan %p", chan);
1606
1607 conn->smp = chan;
1608 l2cap_chan_hold(chan);
1609}
1610
1611static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1612{
1613 int err;
1614
1615 BT_DBG("chan %p", chan);
1616
1617 err = smp_sig_channel(chan, skb);
1618 if (err) {
1619 struct smp_chan *smp = chan->data;
1620
1621 if (smp)
1622 cancel_delayed_work_sync(&smp->security_timer);
1623
1624 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
1625 }
1626
1627 return err;
1628}
1629
1630static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1631 unsigned long hdr_len,
1632 unsigned long len, int nb)
1633{
1634 struct sk_buff *skb;
1635
1636 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1637 if (!skb)
1638 return ERR_PTR(-ENOMEM);
1639
1640 skb->priority = HCI_PRIO_MAX;
1641 bt_cb(skb)->chan = chan;
1642
1643 return skb;
1644}
1645
1646static const struct l2cap_ops smp_chan_ops = {
1647 .name = "Security Manager",
1648 .ready = smp_ready_cb,
1649 .recv = smp_recv_cb,
1650 .alloc_skb = smp_alloc_skb_cb,
1651 .teardown = smp_teardown_cb,
1652 .resume = smp_resume_cb,
1653
1654 .new_connection = l2cap_chan_no_new_connection,
1655 .state_change = l2cap_chan_no_state_change,
1656 .close = l2cap_chan_no_close,
1657 .defer = l2cap_chan_no_defer,
1658 .suspend = l2cap_chan_no_suspend,
1659 .set_shutdown = l2cap_chan_no_set_shutdown,
1660 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1661 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1662};
1663
1664static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1665{
1666 struct l2cap_chan *chan;
1667
1668 BT_DBG("pchan %p", pchan);
1669
1670 chan = l2cap_chan_create();
1671 if (!chan)
1672 return NULL;
1673
1674 chan->chan_type = pchan->chan_type;
1675 chan->ops = &smp_chan_ops;
1676 chan->scid = pchan->scid;
1677 chan->dcid = chan->scid;
1678 chan->imtu = pchan->imtu;
1679 chan->omtu = pchan->omtu;
1680 chan->mode = pchan->mode;
1681
1682 /* Other L2CAP channels may request SMP routines in order to
1683 * change the security level. This means that the SMP channel
1684 * lock must be considered in its own category to avoid lockdep
1685 * warnings.
1686 */
1687 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
1688
1689 BT_DBG("created chan %p", chan);
1690
1691 return chan;
1692}
1693
1694static const struct l2cap_ops smp_root_chan_ops = {
1695 .name = "Security Manager Root",
1696 .new_connection = smp_new_conn_cb,
1697
1698 /* None of these are implemented for the root channel */
1699 .close = l2cap_chan_no_close,
1700 .alloc_skb = l2cap_chan_no_alloc_skb,
1701 .recv = l2cap_chan_no_recv,
1702 .state_change = l2cap_chan_no_state_change,
1703 .teardown = l2cap_chan_no_teardown,
1704 .ready = l2cap_chan_no_ready,
1705 .defer = l2cap_chan_no_defer,
1706 .suspend = l2cap_chan_no_suspend,
1707 .resume = l2cap_chan_no_resume,
1708 .set_shutdown = l2cap_chan_no_set_shutdown,
1709 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1710 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1711};
1712
1713int smp_register(struct hci_dev *hdev)
1714{
1715 struct l2cap_chan *chan;
1716 struct crypto_blkcipher *tfm_aes;
1717
1718 BT_DBG("%s", hdev->name);
1719
1720 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
1721 if (IS_ERR(tfm_aes)) {
1722 int err = PTR_ERR(tfm_aes);
1723 BT_ERR("Unable to create crypto context");
1724 return err;
1725 }
1726
1727 chan = l2cap_chan_create();
1728 if (!chan) {
1729 crypto_free_blkcipher(tfm_aes);
1730 return -ENOMEM;
1731 }
1732
1733 chan->data = tfm_aes;
1734
1735 l2cap_add_scid(chan, L2CAP_CID_SMP);
1736
1737 l2cap_chan_set_defaults(chan);
1738
1739 bacpy(&chan->src, &hdev->bdaddr);
1740 chan->src_type = BDADDR_LE_PUBLIC;
1741 chan->state = BT_LISTEN;
1742 chan->mode = L2CAP_MODE_BASIC;
1743 chan->imtu = L2CAP_DEFAULT_MTU;
1744 chan->ops = &smp_root_chan_ops;
1745
1746 /* Set correct nesting level for a parent/listening channel */
1747 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
1748
1749 hdev->smp_data = chan;
1750
1751 return 0;
1752}
1753
1754void smp_unregister(struct hci_dev *hdev)
1755{
1756 struct l2cap_chan *chan = hdev->smp_data;
1757 struct crypto_blkcipher *tfm_aes;
1758
1759 if (!chan)
1760 return;
1761
1762 BT_DBG("%s chan %p", hdev->name, chan);
1763
1764 tfm_aes = chan->data;
1765 if (tfm_aes) {
1766 chan->data = NULL;
1767 crypto_free_blkcipher(tfm_aes);
1768 }
1769
1770 hdev->smp_data = NULL;
1771 l2cap_chan_put(chan);
1772}
This page took 0.029445 seconds and 5 git commands to generate.