]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/bluetooth/smp.c
Merge git://git.kvack.org/~bcrl/aio-next
[mirror_ubuntu-bionic-kernel.git] / net / bluetooth / smp.c
CommitLineData
eb492e01
AB
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
8c520a59
GP
23#include <linux/crypto.h>
24#include <linux/scatterlist.h>
25#include <crypto/b128ops.h>
26
eb492e01
AB
27#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29#include <net/bluetooth/l2cap.h>
2b64d153 30#include <net/bluetooth/mgmt.h>
ac4b7236 31
3b19146d 32#include "ecc.h"
ac4b7236 33#include "smp.h"
d22ef0bc 34
c7a3d57d
JH
35/* Low-level debug macros to be used for stuff that we don't want
36 * accidentially in dmesg, i.e. the values of the various crypto keys
37 * and the inputs & outputs of crypto functions.
38 */
39#ifdef DEBUG
40#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
41 ##__VA_ARGS__)
42#else
43#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
44 ##__VA_ARGS__)
45#endif
46
b28b4943 47#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
b28b4943 48
3b19146d
JH
49/* Keys which are not distributed with Secure Connections */
50#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
51
17b02e62 52#define SMP_TIMEOUT msecs_to_jiffies(30000)
5d3de7df 53
0edb14de
JH
54#define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
55 0x1f : 0x07)
56#define KEY_DIST_MASK 0x07
065a13e2 57
cbbbe3e2
JH
58/* Maximum message length that can be passed to aes_cmac */
59#define CMAC_MSG_MAX 80
60
533e35d4
JH
61enum {
62 SMP_FLAG_TK_VALID,
63 SMP_FLAG_CFM_PENDING,
64 SMP_FLAG_MITM_AUTH,
65 SMP_FLAG_COMPLETE,
66 SMP_FLAG_INITIATOR,
65668776 67 SMP_FLAG_SC,
d8f8edbe 68 SMP_FLAG_REMOTE_PK,
aeb7d461 69 SMP_FLAG_DEBUG_KEY,
38606f14 70 SMP_FLAG_WAIT_USER,
d3e54a87 71 SMP_FLAG_DHKEY_PENDING,
02b05bd8 72 SMP_FLAG_OOB,
533e35d4 73};
4bc58f51
JH
74
75struct smp_chan {
b68fda68
JH
76 struct l2cap_conn *conn;
77 struct delayed_work security_timer;
b28b4943 78 unsigned long allow_cmd; /* Bitmask of allowed commands */
b68fda68 79
4bc58f51
JH
80 u8 preq[7]; /* SMP Pairing Request */
81 u8 prsp[7]; /* SMP Pairing Response */
82 u8 prnd[16]; /* SMP Pairing Random (local) */
83 u8 rrnd[16]; /* SMP Pairing Random (remote) */
84 u8 pcnf[16]; /* SMP Pairing Confirm */
85 u8 tk[16]; /* SMP Temporary Key */
a29b0733 86 u8 rr[16];
4bc58f51
JH
87 u8 enc_key_size;
88 u8 remote_key_dist;
89 bdaddr_t id_addr;
90 u8 id_addr_type;
91 u8 irk[16];
92 struct smp_csrk *csrk;
93 struct smp_csrk *slave_csrk;
94 struct smp_ltk *ltk;
95 struct smp_ltk *slave_ltk;
96 struct smp_irk *remote_irk;
6a77083a 97 u8 *link_key;
4a74d658 98 unsigned long flags;
783e0574 99 u8 method;
38606f14 100 u8 passkey_round;
6a7bd103 101
3b19146d
JH
102 /* Secure Connections variables */
103 u8 local_pk[64];
104 u8 local_sk[32];
d8f8edbe
JH
105 u8 remote_pk[64];
106 u8 dhkey[32];
760b018b 107 u8 mackey[16];
3b19146d 108
6a7bd103 109 struct crypto_blkcipher *tfm_aes;
407cecf6 110 struct crypto_hash *tfm_cmac;
4bc58f51
JH
111};
112
aeb7d461
JH
113/* These debug key values are defined in the SMP section of the core
114 * specification. debug_pk is the public debug key and debug_sk the
115 * private debug key.
116 */
117static const u8 debug_pk[64] = {
118 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
119 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
120 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
121 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
122
123 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
124 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
125 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
126 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
127};
128
129static const u8 debug_sk[32] = {
130 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
131 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
132 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
133 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
134};
135
8a2936f4 136static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
d22ef0bc 137{
8a2936f4 138 size_t i;
d22ef0bc 139
8a2936f4
JH
140 for (i = 0; i < len; i++)
141 dst[len - 1 - i] = src[i];
d22ef0bc
AB
142}
143
06edf8de
JH
144/* The following functions map to the LE SC SMP crypto functions
145 * AES-CMAC, f4, f5, f6, g2 and h6.
146 */
147
cbbbe3e2
JH
148static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
149 size_t len, u8 mac[16])
150{
151 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
152 struct hash_desc desc;
153 struct scatterlist sg;
154 int err;
155
156 if (len > CMAC_MSG_MAX)
157 return -EFBIG;
158
159 if (!tfm) {
160 BT_ERR("tfm %p", tfm);
161 return -EINVAL;
162 }
163
164 desc.tfm = tfm;
165 desc.flags = 0;
166
167 crypto_hash_init(&desc);
168
169 /* Swap key and message from LSB to MSB */
170 swap_buf(k, tmp, 16);
171 swap_buf(m, msg_msb, len);
172
c7a3d57d
JH
173 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
174 SMP_DBG("key %16phN", k);
cbbbe3e2
JH
175
176 err = crypto_hash_setkey(tfm, tmp, 16);
177 if (err) {
178 BT_ERR("cipher setkey failed: %d", err);
179 return err;
180 }
181
182 sg_init_one(&sg, msg_msb, len);
183
184 err = crypto_hash_update(&desc, &sg, len);
185 if (err) {
186 BT_ERR("Hash update error %d", err);
187 return err;
188 }
189
190 err = crypto_hash_final(&desc, mac_msb);
191 if (err) {
192 BT_ERR("Hash final error %d", err);
193 return err;
194 }
195
196 swap_buf(mac_msb, mac, 16);
197
c7a3d57d 198 SMP_DBG("mac %16phN", mac);
cbbbe3e2
JH
199
200 return 0;
201}
202
203static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
204 const u8 x[16], u8 z, u8 res[16])
205{
206 u8 m[65];
207 int err;
208
c7a3d57d
JH
209 SMP_DBG("u %32phN", u);
210 SMP_DBG("v %32phN", v);
211 SMP_DBG("x %16phN z %02x", x, z);
cbbbe3e2
JH
212
213 m[0] = z;
214 memcpy(m + 1, v, 32);
215 memcpy(m + 33, u, 32);
216
217 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
218 if (err)
219 return err;
220
c7a3d57d 221 SMP_DBG("res %16phN", res);
cbbbe3e2
JH
222
223 return err;
224}
225
760b018b
JH
226static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
227 u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16])
228{
229 /* The btle, salt and length "magic" values are as defined in
230 * the SMP section of the Bluetooth core specification. In ASCII
231 * the btle value ends up being 'btle'. The salt is just a
232 * random number whereas length is the value 256 in little
233 * endian format.
234 */
235 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
236 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
237 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
238 const u8 length[2] = { 0x00, 0x01 };
239 u8 m[53], t[16];
240 int err;
241
c7a3d57d
JH
242 SMP_DBG("w %32phN", w);
243 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
244 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
760b018b
JH
245
246 err = aes_cmac(tfm_cmac, salt, w, 32, t);
247 if (err)
248 return err;
249
c7a3d57d 250 SMP_DBG("t %16phN", t);
760b018b
JH
251
252 memcpy(m, length, 2);
253 memcpy(m + 2, a2, 7);
254 memcpy(m + 9, a1, 7);
255 memcpy(m + 16, n2, 16);
256 memcpy(m + 32, n1, 16);
257 memcpy(m + 48, btle, 4);
258
259 m[52] = 0; /* Counter */
260
261 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
262 if (err)
263 return err;
264
c7a3d57d 265 SMP_DBG("mackey %16phN", mackey);
760b018b
JH
266
267 m[52] = 1; /* Counter */
268
269 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
270 if (err)
271 return err;
272
c7a3d57d 273 SMP_DBG("ltk %16phN", ltk);
760b018b
JH
274
275 return 0;
276}
277
278static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
279 const u8 n1[16], u8 n2[16], const u8 r[16],
280 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
281 u8 res[16])
282{
283 u8 m[65];
284 int err;
285
c7a3d57d
JH
286 SMP_DBG("w %16phN", w);
287 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
288 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
760b018b
JH
289
290 memcpy(m, a2, 7);
291 memcpy(m + 7, a1, 7);
292 memcpy(m + 14, io_cap, 3);
293 memcpy(m + 17, r, 16);
294 memcpy(m + 33, n2, 16);
295 memcpy(m + 49, n1, 16);
296
297 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
298 if (err)
299 return err;
300
301 BT_DBG("res %16phN", res);
302
303 return err;
304}
305
191dc7fe
JH
306static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
307 const u8 x[16], const u8 y[16], u32 *val)
308{
309 u8 m[80], tmp[16];
310 int err;
311
c7a3d57d
JH
312 SMP_DBG("u %32phN", u);
313 SMP_DBG("v %32phN", v);
314 SMP_DBG("x %16phN y %16phN", x, y);
191dc7fe
JH
315
316 memcpy(m, y, 16);
317 memcpy(m + 16, v, 32);
318 memcpy(m + 48, u, 32);
319
320 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
321 if (err)
322 return err;
323
324 *val = get_unaligned_le32(tmp);
325 *val %= 1000000;
326
c7a3d57d 327 SMP_DBG("val %06u", *val);
191dc7fe
JH
328
329 return 0;
330}
331
06edf8de
JH
332static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
333 const u8 key_id[4], u8 res[16])
334{
335 int err;
336
337 SMP_DBG("w %16phN key_id %4phN", w, key_id);
338
339 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
340 if (err)
341 return err;
342
343 SMP_DBG("res %16phN", res);
344
345 return err;
346}
347
348/* The following functions map to the legacy SMP crypto functions e, c1,
349 * s1 and ah.
350 */
351
d22ef0bc
AB
352static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
353{
354 struct blkcipher_desc desc;
355 struct scatterlist sg;
943a732a 356 uint8_t tmp[16], data[16];
201a5929 357 int err;
d22ef0bc 358
7f376cd6 359 if (!tfm) {
d22ef0bc
AB
360 BT_ERR("tfm %p", tfm);
361 return -EINVAL;
362 }
363
364 desc.tfm = tfm;
365 desc.flags = 0;
366
943a732a 367 /* The most significant octet of key corresponds to k[0] */
8a2936f4 368 swap_buf(k, tmp, 16);
943a732a
JH
369
370 err = crypto_blkcipher_setkey(tfm, tmp, 16);
d22ef0bc
AB
371 if (err) {
372 BT_ERR("cipher setkey failed: %d", err);
373 return err;
374 }
375
943a732a 376 /* Most significant octet of plaintextData corresponds to data[0] */
8a2936f4 377 swap_buf(r, data, 16);
943a732a
JH
378
379 sg_init_one(&sg, data, 16);
d22ef0bc 380
d22ef0bc
AB
381 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
382 if (err)
383 BT_ERR("Encrypt data error %d", err);
384
943a732a 385 /* Most significant octet of encryptedData corresponds to data[0] */
8a2936f4 386 swap_buf(data, r, 16);
943a732a 387
d22ef0bc
AB
388 return err;
389}
390
06edf8de
JH
391static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
392 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
393 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
6a77083a 394{
06edf8de 395 u8 p1[16], p2[16];
6a77083a
JH
396 int err;
397
06edf8de 398 memset(p1, 0, 16);
6a77083a 399
06edf8de
JH
400 /* p1 = pres || preq || _rat || _iat */
401 p1[0] = _iat;
402 p1[1] = _rat;
403 memcpy(p1 + 2, preq, 7);
404 memcpy(p1 + 9, pres, 7);
405
406 /* p2 = padding || ia || ra */
407 memcpy(p2, ra, 6);
408 memcpy(p2 + 6, ia, 6);
409 memset(p2 + 12, 0, 4);
410
411 /* res = r XOR p1 */
412 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
413
414 /* res = e(k, res) */
415 err = smp_e(tfm_aes, k, res);
416 if (err) {
417 BT_ERR("Encrypt data error");
6a77083a 418 return err;
06edf8de 419 }
6a77083a 420
06edf8de
JH
421 /* res = res XOR p2 */
422 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
423
424 /* res = e(k, res) */
425 err = smp_e(tfm_aes, k, res);
426 if (err)
427 BT_ERR("Encrypt data error");
428
429 return err;
430}
431
432static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
433 const u8 r1[16], const u8 r2[16], u8 _r[16])
434{
435 int err;
436
437 /* Just least significant octets from r1 and r2 are considered */
438 memcpy(_r, r2, 8);
439 memcpy(_r + 8, r1, 8);
440
441 err = smp_e(tfm_aes, k, _r);
442 if (err)
443 BT_ERR("Encrypt data error");
6a77083a
JH
444
445 return err;
446}
447
cd082797
JH
448static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
449 const u8 r[3], u8 res[3])
60478054 450{
943a732a 451 u8 _res[16];
60478054
JH
452 int err;
453
454 /* r' = padding || r */
943a732a
JH
455 memcpy(_res, r, 3);
456 memset(_res + 3, 0, 13);
60478054 457
943a732a 458 err = smp_e(tfm, irk, _res);
60478054
JH
459 if (err) {
460 BT_ERR("Encrypt error");
461 return err;
462 }
463
464 /* The output of the random address function ah is:
465 * ah(h, r) = e(k, r') mod 2^24
466 * The output of the security function e is then truncated to 24 bits
467 * by taking the least significant 24 bits of the output of e as the
468 * result of ah.
469 */
943a732a 470 memcpy(res, _res, 3);
60478054
JH
471
472 return 0;
473}
474
cd082797
JH
475bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
476 const bdaddr_t *bdaddr)
60478054 477{
defce9e8
JH
478 struct l2cap_chan *chan = hdev->smp_data;
479 struct crypto_blkcipher *tfm;
60478054
JH
480 u8 hash[3];
481 int err;
482
defce9e8
JH
483 if (!chan || !chan->data)
484 return false;
485
486 tfm = chan->data;
487
60478054
JH
488 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
489
490 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
491 if (err)
492 return false;
493
494 return !memcmp(bdaddr->b, hash, 3);
495}
496
cd082797 497int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
b1e2b3ae 498{
defce9e8
JH
499 struct l2cap_chan *chan = hdev->smp_data;
500 struct crypto_blkcipher *tfm;
b1e2b3ae
JH
501 int err;
502
defce9e8
JH
503 if (!chan || !chan->data)
504 return -EOPNOTSUPP;
505
506 tfm = chan->data;
507
b1e2b3ae
JH
508 get_random_bytes(&rpa->b[3], 3);
509
510 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
511 rpa->b[5] |= 0x40; /* Set second most significant bit */
512
513 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
514 if (err < 0)
515 return err;
516
517 BT_DBG("RPA %pMR", rpa);
518
519 return 0;
520}
521
5d88cc73 522static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
eb492e01 523{
5d88cc73 524 struct l2cap_chan *chan = conn->smp;
b68fda68 525 struct smp_chan *smp;
5d88cc73
JH
526 struct kvec iv[2];
527 struct msghdr msg;
eb492e01 528
5d88cc73
JH
529 if (!chan)
530 return;
eb492e01 531
5d88cc73 532 BT_DBG("code 0x%2.2x", code);
eb492e01 533
5d88cc73
JH
534 iv[0].iov_base = &code;
535 iv[0].iov_len = 1;
eb492e01 536
5d88cc73
JH
537 iv[1].iov_base = data;
538 iv[1].iov_len = len;
eb492e01 539
5d88cc73 540 memset(&msg, 0, sizeof(msg));
eb492e01 541
17836394 542 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
eb492e01 543
5d88cc73 544 l2cap_chan_send(chan, &msg, 1 + len);
e2dcd113 545
b68fda68
JH
546 if (!chan->data)
547 return;
548
549 smp = chan->data;
550
551 cancel_delayed_work_sync(&smp->security_timer);
1b0921d6 552 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
eb492e01
AB
553}
554
d2eb9e10 555static u8 authreq_to_seclevel(u8 authreq)
2b64d153 556{
d2eb9e10
JH
557 if (authreq & SMP_AUTH_MITM) {
558 if (authreq & SMP_AUTH_SC)
559 return BT_SECURITY_FIPS;
560 else
561 return BT_SECURITY_HIGH;
562 } else {
2b64d153 563 return BT_SECURITY_MEDIUM;
d2eb9e10 564 }
2b64d153
BG
565}
566
567static __u8 seclevel_to_authreq(__u8 sec_level)
568{
569 switch (sec_level) {
d2eb9e10 570 case BT_SECURITY_FIPS:
2b64d153
BG
571 case BT_SECURITY_HIGH:
572 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
573 case BT_SECURITY_MEDIUM:
574 return SMP_AUTH_BONDING;
575 default:
576 return SMP_AUTH_NONE;
577 }
578}
579
b8e66eac 580static void build_pairing_cmd(struct l2cap_conn *conn,
f1560463
MH
581 struct smp_cmd_pairing *req,
582 struct smp_cmd_pairing *rsp, __u8 authreq)
b8e66eac 583{
5d88cc73
JH
584 struct l2cap_chan *chan = conn->smp;
585 struct smp_chan *smp = chan->data;
fd349c02
JH
586 struct hci_conn *hcon = conn->hcon;
587 struct hci_dev *hdev = hcon->hdev;
02b05bd8 588 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
54790f73 589
b6ae8457 590 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
7ee4ea36
MH
591 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
592 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
54790f73 593 authreq |= SMP_AUTH_BONDING;
2b64d153
BG
594 } else {
595 authreq &= ~SMP_AUTH_BONDING;
54790f73
VCG
596 }
597
fd349c02
JH
598 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
599 remote_dist |= SMP_DIST_ID_KEY;
600
863efaf2
JH
601 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
602 local_dist |= SMP_DIST_ID_KEY;
603
02b05bd8
JH
604 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
605 (authreq & SMP_AUTH_SC)) {
606 struct oob_data *oob_data;
607 u8 bdaddr_type;
608
609 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
df8e1a4c
JH
610 local_dist |= SMP_DIST_LINK_KEY;
611 remote_dist |= SMP_DIST_LINK_KEY;
612 }
02b05bd8
JH
613
614 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
615 bdaddr_type = BDADDR_LE_PUBLIC;
616 else
617 bdaddr_type = BDADDR_LE_RANDOM;
618
619 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
620 bdaddr_type);
621 if (oob_data) {
622 set_bit(SMP_FLAG_OOB, &smp->flags);
623 oob_flag = SMP_OOB_PRESENT;
a29b0733 624 memcpy(smp->rr, oob_data->rand256, 16);
02b05bd8
JH
625 memcpy(smp->pcnf, oob_data->hash256, 16);
626 }
627
df8e1a4c
JH
628 } else {
629 authreq &= ~SMP_AUTH_SC;
630 }
631
54790f73
VCG
632 if (rsp == NULL) {
633 req->io_capability = conn->hcon->io_capability;
02b05bd8 634 req->oob_flag = oob_flag;
54790f73 635 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
fd349c02
JH
636 req->init_key_dist = local_dist;
637 req->resp_key_dist = remote_dist;
0edb14de 638 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
fd349c02
JH
639
640 smp->remote_key_dist = remote_dist;
54790f73
VCG
641 return;
642 }
643
644 rsp->io_capability = conn->hcon->io_capability;
02b05bd8 645 rsp->oob_flag = oob_flag;
54790f73 646 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
fd349c02
JH
647 rsp->init_key_dist = req->init_key_dist & remote_dist;
648 rsp->resp_key_dist = req->resp_key_dist & local_dist;
0edb14de 649 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
fd349c02
JH
650
651 smp->remote_key_dist = rsp->init_key_dist;
b8e66eac
VCG
652}
653
3158c50c
VCG
654static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
655{
5d88cc73
JH
656 struct l2cap_chan *chan = conn->smp;
657 struct smp_chan *smp = chan->data;
1c1def09 658
3158c50c 659 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
f1560463 660 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
3158c50c
VCG
661 return SMP_ENC_KEY_SIZE;
662
f7aa611a 663 smp->enc_key_size = max_key_size;
3158c50c
VCG
664
665 return 0;
666}
667
6f48e260
JH
668static void smp_chan_destroy(struct l2cap_conn *conn)
669{
670 struct l2cap_chan *chan = conn->smp;
671 struct smp_chan *smp = chan->data;
923e2414 672 struct hci_conn *hcon = conn->hcon;
6f48e260
JH
673 bool complete;
674
675 BUG_ON(!smp);
676
677 cancel_delayed_work_sync(&smp->security_timer);
6f48e260 678
6f48e260 679 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
923e2414 680 mgmt_smp_complete(hcon, complete);
6f48e260
JH
681
682 kfree(smp->csrk);
683 kfree(smp->slave_csrk);
6a77083a 684 kfree(smp->link_key);
6f48e260
JH
685
686 crypto_free_blkcipher(smp->tfm_aes);
407cecf6 687 crypto_free_hash(smp->tfm_cmac);
6f48e260 688
923e2414
JH
689 /* Ensure that we don't leave any debug key around if debug key
690 * support hasn't been explicitly enabled.
691 */
692 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
693 !test_bit(HCI_KEEP_DEBUG_KEYS, &hcon->hdev->dev_flags)) {
694 list_del_rcu(&smp->ltk->list);
695 kfree_rcu(smp->ltk, rcu);
696 smp->ltk = NULL;
697 }
698
6f48e260
JH
699 /* If pairing failed clean up any keys we might have */
700 if (!complete) {
701 if (smp->ltk) {
970d0f1b
JH
702 list_del_rcu(&smp->ltk->list);
703 kfree_rcu(smp->ltk, rcu);
6f48e260
JH
704 }
705
706 if (smp->slave_ltk) {
970d0f1b
JH
707 list_del_rcu(&smp->slave_ltk->list);
708 kfree_rcu(smp->slave_ltk, rcu);
6f48e260
JH
709 }
710
711 if (smp->remote_irk) {
adae20cb
JH
712 list_del_rcu(&smp->remote_irk->list);
713 kfree_rcu(smp->remote_irk, rcu);
6f48e260
JH
714 }
715 }
716
717 chan->data = NULL;
718 kfree(smp);
923e2414 719 hci_conn_drop(hcon);
6f48e260
JH
720}
721
84794e11 722static void smp_failure(struct l2cap_conn *conn, u8 reason)
4f957a76 723{
bab73cb6 724 struct hci_conn *hcon = conn->hcon;
b68fda68 725 struct l2cap_chan *chan = conn->smp;
bab73cb6 726
84794e11 727 if (reason)
4f957a76 728 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
f1560463 729 &reason);
4f957a76 730
ce39fb4e 731 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
e1e930f5 732 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
f1c09c07 733
fc75cc86 734 if (chan->data)
f1c09c07 735 smp_chan_destroy(conn);
4f957a76
BG
736}
737
2b64d153
BG
738#define JUST_WORKS 0x00
739#define JUST_CFM 0x01
740#define REQ_PASSKEY 0x02
741#define CFM_PASSKEY 0x03
742#define REQ_OOB 0x04
5e3d3d9b 743#define DSP_PASSKEY 0x05
2b64d153
BG
744#define OVERLAP 0xFF
745
746static const u8 gen_method[5][5] = {
747 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
748 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
749 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
750 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
751 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
752};
753
5e3d3d9b
JH
754static const u8 sc_method[5][5] = {
755 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
756 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
757 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
758 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
759 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
760};
761
581370cc
JH
762static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
763{
2bcd4003
JH
764 /* If either side has unknown io_caps, use JUST_CFM (which gets
765 * converted later to JUST_WORKS if we're initiators.
766 */
581370cc
JH
767 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
768 remote_io > SMP_IO_KEYBOARD_DISPLAY)
2bcd4003 769 return JUST_CFM;
581370cc 770
5e3d3d9b
JH
771 if (test_bit(SMP_FLAG_SC, &smp->flags))
772 return sc_method[remote_io][local_io];
773
581370cc
JH
774 return gen_method[remote_io][local_io];
775}
776
2b64d153
BG
777static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
778 u8 local_io, u8 remote_io)
779{
780 struct hci_conn *hcon = conn->hcon;
5d88cc73
JH
781 struct l2cap_chan *chan = conn->smp;
782 struct smp_chan *smp = chan->data;
2b64d153
BG
783 u32 passkey = 0;
784 int ret = 0;
785
786 /* Initialize key for JUST WORKS */
787 memset(smp->tk, 0, sizeof(smp->tk));
4a74d658 788 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
789
790 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
791
2bcd4003
JH
792 /* If neither side wants MITM, either "just" confirm an incoming
793 * request or use just-works for outgoing ones. The JUST_CFM
794 * will be converted to JUST_WORKS if necessary later in this
795 * function. If either side has MITM look up the method from the
796 * table.
797 */
581370cc 798 if (!(auth & SMP_AUTH_MITM))
783e0574 799 smp->method = JUST_CFM;
2b64d153 800 else
783e0574 801 smp->method = get_auth_method(smp, local_io, remote_io);
2b64d153 802
a82505c7 803 /* Don't confirm locally initiated pairing attempts */
783e0574
JH
804 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
805 &smp->flags))
806 smp->method = JUST_WORKS;
a82505c7 807
02f3e254 808 /* Don't bother user space with no IO capabilities */
783e0574
JH
809 if (smp->method == JUST_CFM &&
810 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
811 smp->method = JUST_WORKS;
02f3e254 812
2b64d153 813 /* If Just Works, Continue with Zero TK */
783e0574 814 if (smp->method == JUST_WORKS) {
4a74d658 815 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
816 return 0;
817 }
818
819 /* Not Just Works/Confirm results in MITM Authentication */
783e0574 820 if (smp->method != JUST_CFM) {
4a74d658 821 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
5eb596f5
JH
822 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
823 hcon->pending_sec_level = BT_SECURITY_HIGH;
824 }
2b64d153
BG
825
826 /* If both devices have Keyoard-Display I/O, the master
827 * Confirms and the slave Enters the passkey.
828 */
783e0574 829 if (smp->method == OVERLAP) {
40bef302 830 if (hcon->role == HCI_ROLE_MASTER)
783e0574 831 smp->method = CFM_PASSKEY;
2b64d153 832 else
783e0574 833 smp->method = REQ_PASSKEY;
2b64d153
BG
834 }
835
01ad34d2 836 /* Generate random passkey. */
783e0574 837 if (smp->method == CFM_PASSKEY) {
943a732a 838 memset(smp->tk, 0, sizeof(smp->tk));
2b64d153
BG
839 get_random_bytes(&passkey, sizeof(passkey));
840 passkey %= 1000000;
943a732a 841 put_unaligned_le32(passkey, smp->tk);
2b64d153 842 BT_DBG("PassKey: %d", passkey);
4a74d658 843 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
844 }
845
783e0574 846 if (smp->method == REQ_PASSKEY)
ce39fb4e 847 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
272d90df 848 hcon->type, hcon->dst_type);
783e0574 849 else if (smp->method == JUST_CFM)
4eb65e66
JH
850 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
851 hcon->type, hcon->dst_type,
852 passkey, 1);
2b64d153 853 else
01ad34d2 854 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
272d90df 855 hcon->type, hcon->dst_type,
39adbffe 856 passkey, 0);
2b64d153 857
2b64d153
BG
858 return ret;
859}
860
1cc61144 861static u8 smp_confirm(struct smp_chan *smp)
8aab4757 862{
8aab4757 863 struct l2cap_conn *conn = smp->conn;
8aab4757
VCG
864 struct smp_cmd_pairing_confirm cp;
865 int ret;
8aab4757
VCG
866
867 BT_DBG("conn %p", conn);
868
e491eaf3 869 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
b1cd5fd9 870 conn->hcon->init_addr_type, &conn->hcon->init_addr,
943a732a
JH
871 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
872 cp.confirm_val);
1cc61144
JH
873 if (ret)
874 return SMP_UNSPECIFIED;
8aab4757 875
4a74d658 876 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
2b64d153 877
8aab4757
VCG
878 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
879
b28b4943
JH
880 if (conn->hcon->out)
881 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
882 else
883 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
884
1cc61144 885 return 0;
8aab4757
VCG
886}
887
861580a9 888static u8 smp_random(struct smp_chan *smp)
8aab4757 889{
8aab4757
VCG
890 struct l2cap_conn *conn = smp->conn;
891 struct hci_conn *hcon = conn->hcon;
861580a9 892 u8 confirm[16];
8aab4757
VCG
893 int ret;
894
ec70f36f 895 if (IS_ERR_OR_NULL(smp->tfm_aes))
861580a9 896 return SMP_UNSPECIFIED;
8aab4757
VCG
897
898 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
899
e491eaf3 900 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
b1cd5fd9 901 hcon->init_addr_type, &hcon->init_addr,
943a732a 902 hcon->resp_addr_type, &hcon->resp_addr, confirm);
861580a9
JH
903 if (ret)
904 return SMP_UNSPECIFIED;
8aab4757 905
8aab4757
VCG
906 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
907 BT_ERR("Pairing failed (confirmation values mismatch)");
861580a9 908 return SMP_CONFIRM_FAILED;
8aab4757
VCG
909 }
910
911 if (hcon->out) {
fe39c7b2
MH
912 u8 stk[16];
913 __le64 rand = 0;
914 __le16 ediv = 0;
8aab4757 915
e491eaf3 916 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
8aab4757 917
f7aa611a 918 memset(stk + smp->enc_key_size, 0,
04124681 919 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
8aab4757 920
861580a9
JH
921 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
922 return SMP_UNSPECIFIED;
8aab4757
VCG
923
924 hci_le_start_enc(hcon, ediv, rand, stk);
f7aa611a 925 hcon->enc_key_size = smp->enc_key_size;
fe59a05f 926 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
8aab4757 927 } else {
fff3490f 928 u8 stk[16], auth;
fe39c7b2
MH
929 __le64 rand = 0;
930 __le16 ediv = 0;
8aab4757 931
943a732a
JH
932 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
933 smp->prnd);
8aab4757 934
e491eaf3 935 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
8aab4757 936
f7aa611a 937 memset(stk + smp->enc_key_size, 0,
f1560463 938 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
8aab4757 939
fff3490f
JH
940 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
941 auth = 1;
942 else
943 auth = 0;
944
7d5843b7
JH
945 /* Even though there's no _SLAVE suffix this is the
946 * slave STK we're adding for later lookup (the master
947 * STK never needs to be stored).
948 */
ce39fb4e 949 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
2ceba539 950 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
8aab4757
VCG
951 }
952
861580a9 953 return 0;
8aab4757
VCG
954}
955
44f1a7ab
JH
956static void smp_notify_keys(struct l2cap_conn *conn)
957{
958 struct l2cap_chan *chan = conn->smp;
959 struct smp_chan *smp = chan->data;
960 struct hci_conn *hcon = conn->hcon;
961 struct hci_dev *hdev = hcon->hdev;
962 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
963 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
964 bool persistent;
965
966 if (smp->remote_irk) {
967 mgmt_new_irk(hdev, smp->remote_irk);
968 /* Now that user space can be considered to know the
969 * identity address track the connection based on it
b5ae344d 970 * from now on (assuming this is an LE link).
44f1a7ab 971 */
b5ae344d
JH
972 if (hcon->type == LE_LINK) {
973 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
974 hcon->dst_type = smp->remote_irk->addr_type;
975 queue_work(hdev->workqueue, &conn->id_addr_update_work);
976 }
44f1a7ab
JH
977
978 /* When receiving an indentity resolving key for
979 * a remote device that does not use a resolvable
980 * private address, just remove the key so that
981 * it is possible to use the controller white
982 * list for scanning.
983 *
984 * Userspace will have been told to not store
985 * this key at this point. So it is safe to
986 * just remove it.
987 */
988 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
adae20cb
JH
989 list_del_rcu(&smp->remote_irk->list);
990 kfree_rcu(smp->remote_irk, rcu);
44f1a7ab
JH
991 smp->remote_irk = NULL;
992 }
993 }
994
b5ae344d
JH
995 if (hcon->type == ACL_LINK) {
996 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
997 persistent = false;
998 else
999 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1000 &hcon->flags);
1001 } else {
1002 /* The LTKs and CSRKs should be persistent only if both sides
1003 * had the bonding bit set in their authentication requests.
1004 */
1005 persistent = !!((req->auth_req & rsp->auth_req) &
1006 SMP_AUTH_BONDING);
1007 }
1008
44f1a7ab
JH
1009
1010 if (smp->csrk) {
1011 smp->csrk->bdaddr_type = hcon->dst_type;
1012 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1013 mgmt_new_csrk(hdev, smp->csrk, persistent);
1014 }
1015
1016 if (smp->slave_csrk) {
1017 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1018 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1019 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1020 }
1021
1022 if (smp->ltk) {
1023 smp->ltk->bdaddr_type = hcon->dst_type;
1024 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1025 mgmt_new_ltk(hdev, smp->ltk, persistent);
1026 }
1027
1028 if (smp->slave_ltk) {
1029 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1030 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1031 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1032 }
6a77083a
JH
1033
1034 if (smp->link_key) {
e3befab9
JH
1035 struct link_key *key;
1036 u8 type;
1037
1038 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1039 type = HCI_LK_DEBUG_COMBINATION;
1040 else if (hcon->sec_level == BT_SECURITY_FIPS)
1041 type = HCI_LK_AUTH_COMBINATION_P256;
1042 else
1043 type = HCI_LK_UNAUTH_COMBINATION_P256;
1044
1045 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1046 smp->link_key, type, 0, &persistent);
1047 if (key) {
1048 mgmt_new_link_key(hdev, key, persistent);
1049
1050 /* Don't keep debug keys around if the relevant
1051 * flag is not set.
1052 */
1053 if (!test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags) &&
1054 key->type == HCI_LK_DEBUG_COMBINATION) {
1055 list_del_rcu(&key->list);
1056 kfree_rcu(key, rcu);
1057 }
1058 }
6a77083a
JH
1059 }
1060}
1061
d3e54a87
JH
1062static void sc_add_ltk(struct smp_chan *smp)
1063{
1064 struct hci_conn *hcon = smp->conn->hcon;
1065 u8 key_type, auth;
1066
1067 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1068 key_type = SMP_LTK_P256_DEBUG;
1069 else
1070 key_type = SMP_LTK_P256;
1071
1072 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1073 auth = 1;
1074 else
1075 auth = 0;
1076
1077 memset(smp->tk + smp->enc_key_size, 0,
1078 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
1079
1080 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1081 key_type, auth, smp->tk, smp->enc_key_size,
1082 0, 0);
1083}
1084
6a77083a
JH
1085static void sc_generate_link_key(struct smp_chan *smp)
1086{
1087 /* These constants are as specified in the core specification.
1088 * In ASCII they spell out to 'tmp1' and 'lebr'.
1089 */
1090 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1091 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1092
1093 smp->link_key = kzalloc(16, GFP_KERNEL);
1094 if (!smp->link_key)
1095 return;
1096
1097 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1098 kfree(smp->link_key);
1099 smp->link_key = NULL;
1100 return;
1101 }
1102
1103 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
1104 kfree(smp->link_key);
1105 smp->link_key = NULL;
1106 return;
1107 }
44f1a7ab
JH
1108}
1109
b28b4943
JH
1110static void smp_allow_key_dist(struct smp_chan *smp)
1111{
1112 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1113 * will be allowed in each PDU handler to ensure we receive
1114 * them in the correct order.
1115 */
1116 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1117 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1118 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1119 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1120 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1121 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1122}
1123
b5ae344d
JH
1124static void sc_generate_ltk(struct smp_chan *smp)
1125{
1126 /* These constants are as specified in the core specification.
1127 * In ASCII they spell out to 'tmp2' and 'brle'.
1128 */
1129 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1130 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1131 struct hci_conn *hcon = smp->conn->hcon;
1132 struct hci_dev *hdev = hcon->hdev;
1133 struct link_key *key;
1134
1135 key = hci_find_link_key(hdev, &hcon->dst);
1136 if (!key) {
1137 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1138 return;
1139 }
1140
1141 if (key->type == HCI_LK_DEBUG_COMBINATION)
1142 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1143
1144 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1145 return;
1146
1147 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1148 return;
1149
1150 sc_add_ltk(smp);
1151}
1152
d6268e86 1153static void smp_distribute_keys(struct smp_chan *smp)
44f1a7ab
JH
1154{
1155 struct smp_cmd_pairing *req, *rsp;
86d1407c 1156 struct l2cap_conn *conn = smp->conn;
44f1a7ab
JH
1157 struct hci_conn *hcon = conn->hcon;
1158 struct hci_dev *hdev = hcon->hdev;
1159 __u8 *keydist;
1160
1161 BT_DBG("conn %p", conn);
1162
44f1a7ab
JH
1163 rsp = (void *) &smp->prsp[1];
1164
1165 /* The responder sends its keys first */
b28b4943
JH
1166 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1167 smp_allow_key_dist(smp);
86d1407c 1168 return;
b28b4943 1169 }
44f1a7ab
JH
1170
1171 req = (void *) &smp->preq[1];
1172
1173 if (hcon->out) {
1174 keydist = &rsp->init_key_dist;
1175 *keydist &= req->init_key_dist;
1176 } else {
1177 keydist = &rsp->resp_key_dist;
1178 *keydist &= req->resp_key_dist;
1179 }
1180
6a77083a 1181 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
b5ae344d 1182 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
6a77083a 1183 sc_generate_link_key(smp);
b5ae344d
JH
1184 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1185 sc_generate_ltk(smp);
6a77083a
JH
1186
1187 /* Clear the keys which are generated but not distributed */
1188 *keydist &= ~SMP_SC_NO_DIST;
1189 }
1190
44f1a7ab
JH
1191 BT_DBG("keydist 0x%x", *keydist);
1192
1193 if (*keydist & SMP_DIST_ENC_KEY) {
1194 struct smp_cmd_encrypt_info enc;
1195 struct smp_cmd_master_ident ident;
1196 struct smp_ltk *ltk;
1197 u8 authenticated;
1198 __le16 ediv;
1199 __le64 rand;
1200
1201 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1202 get_random_bytes(&ediv, sizeof(ediv));
1203 get_random_bytes(&rand, sizeof(rand));
1204
1205 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1206
1207 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1208 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1209 SMP_LTK_SLAVE, authenticated, enc.ltk,
1210 smp->enc_key_size, ediv, rand);
1211 smp->slave_ltk = ltk;
1212
1213 ident.ediv = ediv;
1214 ident.rand = rand;
1215
1216 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1217
1218 *keydist &= ~SMP_DIST_ENC_KEY;
1219 }
1220
1221 if (*keydist & SMP_DIST_ID_KEY) {
1222 struct smp_cmd_ident_addr_info addrinfo;
1223 struct smp_cmd_ident_info idinfo;
1224
1225 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1226
1227 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1228
1229 /* The hci_conn contains the local identity address
1230 * after the connection has been established.
1231 *
1232 * This is true even when the connection has been
1233 * established using a resolvable random address.
1234 */
1235 bacpy(&addrinfo.bdaddr, &hcon->src);
1236 addrinfo.addr_type = hcon->src_type;
1237
1238 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1239 &addrinfo);
1240
1241 *keydist &= ~SMP_DIST_ID_KEY;
1242 }
1243
1244 if (*keydist & SMP_DIST_SIGN) {
1245 struct smp_cmd_sign_info sign;
1246 struct smp_csrk *csrk;
1247
1248 /* Generate a new random key */
1249 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1250
1251 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1252 if (csrk) {
1253 csrk->master = 0x00;
1254 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1255 }
1256 smp->slave_csrk = csrk;
1257
1258 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1259
1260 *keydist &= ~SMP_DIST_SIGN;
1261 }
1262
1263 /* If there are still keys to be received wait for them */
b28b4943
JH
1264 if (smp->remote_key_dist & KEY_DIST_MASK) {
1265 smp_allow_key_dist(smp);
86d1407c 1266 return;
b28b4943 1267 }
44f1a7ab 1268
44f1a7ab
JH
1269 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1270 smp_notify_keys(conn);
1271
1272 smp_chan_destroy(conn);
44f1a7ab
JH
1273}
1274
b68fda68
JH
1275static void smp_timeout(struct work_struct *work)
1276{
1277 struct smp_chan *smp = container_of(work, struct smp_chan,
1278 security_timer.work);
1279 struct l2cap_conn *conn = smp->conn;
1280
1281 BT_DBG("conn %p", conn);
1282
1e91c29e 1283 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
b68fda68
JH
1284}
1285
8aab4757
VCG
1286static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1287{
5d88cc73 1288 struct l2cap_chan *chan = conn->smp;
8aab4757
VCG
1289 struct smp_chan *smp;
1290
f1560463 1291 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
fc75cc86 1292 if (!smp)
8aab4757
VCG
1293 return NULL;
1294
6a7bd103
JH
1295 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1296 if (IS_ERR(smp->tfm_aes)) {
1297 BT_ERR("Unable to create ECB crypto context");
1298 kfree(smp);
1299 return NULL;
1300 }
1301
407cecf6
JH
1302 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1303 if (IS_ERR(smp->tfm_cmac)) {
1304 BT_ERR("Unable to create CMAC crypto context");
1305 crypto_free_blkcipher(smp->tfm_aes);
1306 kfree(smp);
1307 return NULL;
1308 }
1309
8aab4757 1310 smp->conn = conn;
5d88cc73 1311 chan->data = smp;
8aab4757 1312
b28b4943
JH
1313 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1314
b68fda68
JH
1315 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1316
8aab4757
VCG
1317 hci_conn_hold(conn->hcon);
1318
1319 return smp;
1320}
1321
760b018b
JH
1322static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1323{
1324 struct hci_conn *hcon = smp->conn->hcon;
1325 u8 *na, *nb, a[7], b[7];
1326
1327 if (hcon->out) {
1328 na = smp->prnd;
1329 nb = smp->rrnd;
1330 } else {
1331 na = smp->rrnd;
1332 nb = smp->prnd;
1333 }
1334
1335 memcpy(a, &hcon->init_addr, 6);
1336 memcpy(b, &hcon->resp_addr, 6);
1337 a[6] = hcon->init_addr_type;
1338 b[6] = hcon->resp_addr_type;
1339
1340 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1341}
1342
38606f14 1343static void sc_dhkey_check(struct smp_chan *smp)
760b018b
JH
1344{
1345 struct hci_conn *hcon = smp->conn->hcon;
1346 struct smp_cmd_dhkey_check check;
1347 u8 a[7], b[7], *local_addr, *remote_addr;
1348 u8 io_cap[3], r[16];
1349
760b018b
JH
1350 memcpy(a, &hcon->init_addr, 6);
1351 memcpy(b, &hcon->resp_addr, 6);
1352 a[6] = hcon->init_addr_type;
1353 b[6] = hcon->resp_addr_type;
1354
1355 if (hcon->out) {
1356 local_addr = a;
1357 remote_addr = b;
1358 memcpy(io_cap, &smp->preq[1], 3);
1359 } else {
1360 local_addr = b;
1361 remote_addr = a;
1362 memcpy(io_cap, &smp->prsp[1], 3);
1363 }
1364
dddd3059
JH
1365 memset(r, 0, sizeof(r));
1366
1367 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
38606f14 1368 put_unaligned_le32(hcon->passkey_notify, r);
760b018b 1369
a29b0733
JH
1370 if (smp->method == REQ_OOB)
1371 memcpy(r, smp->rr, 16);
1372
760b018b
JH
1373 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1374 local_addr, remote_addr, check.e);
1375
1376 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
dddd3059
JH
1377}
1378
38606f14
JH
1379static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1380{
1381 struct l2cap_conn *conn = smp->conn;
1382 struct hci_conn *hcon = conn->hcon;
1383 struct smp_cmd_pairing_confirm cfm;
1384 u8 r;
1385
1386 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1387 r |= 0x80;
1388
1389 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1390
1391 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1392 cfm.confirm_val))
1393 return SMP_UNSPECIFIED;
1394
1395 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1396
1397 return 0;
1398}
1399
1400static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1401{
1402 struct l2cap_conn *conn = smp->conn;
1403 struct hci_conn *hcon = conn->hcon;
1404 struct hci_dev *hdev = hcon->hdev;
1405 u8 cfm[16], r;
1406
1407 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1408 if (smp->passkey_round >= 20)
1409 return 0;
1410
1411 switch (smp_op) {
1412 case SMP_CMD_PAIRING_RANDOM:
1413 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1414 r |= 0x80;
1415
1416 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1417 smp->rrnd, r, cfm))
1418 return SMP_UNSPECIFIED;
1419
1420 if (memcmp(smp->pcnf, cfm, 16))
1421 return SMP_CONFIRM_FAILED;
1422
1423 smp->passkey_round++;
1424
1425 if (smp->passkey_round == 20) {
1426 /* Generate MacKey and LTK */
1427 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1428 return SMP_UNSPECIFIED;
1429 }
1430
1431 /* The round is only complete when the initiator
1432 * receives pairing random.
1433 */
1434 if (!hcon->out) {
1435 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1436 sizeof(smp->prnd), smp->prnd);
d3e54a87 1437 if (smp->passkey_round == 20)
38606f14 1438 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
d3e54a87 1439 else
38606f14 1440 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
38606f14
JH
1441 return 0;
1442 }
1443
1444 /* Start the next round */
1445 if (smp->passkey_round != 20)
1446 return sc_passkey_round(smp, 0);
1447
1448 /* Passkey rounds are complete - start DHKey Check */
1449 sc_dhkey_check(smp);
1450 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1451
1452 break;
1453
1454 case SMP_CMD_PAIRING_CONFIRM:
1455 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1456 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1457 return 0;
1458 }
1459
1460 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1461
1462 if (hcon->out) {
1463 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1464 sizeof(smp->prnd), smp->prnd);
1465 return 0;
1466 }
1467
1468 return sc_passkey_send_confirm(smp);
1469
1470 case SMP_CMD_PUBLIC_KEY:
1471 default:
1472 /* Initiating device starts the round */
1473 if (!hcon->out)
1474 return 0;
1475
1476 BT_DBG("%s Starting passkey round %u", hdev->name,
1477 smp->passkey_round + 1);
1478
1479 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1480
1481 return sc_passkey_send_confirm(smp);
1482 }
1483
1484 return 0;
1485}
1486
dddd3059
JH
1487static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1488{
38606f14
JH
1489 struct l2cap_conn *conn = smp->conn;
1490 struct hci_conn *hcon = conn->hcon;
1491 u8 smp_op;
1492
1493 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1494
dddd3059
JH
1495 switch (mgmt_op) {
1496 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1497 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1498 return 0;
1499 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1500 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1501 return 0;
38606f14
JH
1502 case MGMT_OP_USER_PASSKEY_REPLY:
1503 hcon->passkey_notify = le32_to_cpu(passkey);
1504 smp->passkey_round = 0;
1505
1506 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1507 smp_op = SMP_CMD_PAIRING_CONFIRM;
1508 else
1509 smp_op = 0;
1510
1511 if (sc_passkey_round(smp, smp_op))
1512 return -EIO;
1513
1514 return 0;
dddd3059
JH
1515 }
1516
d3e54a87
JH
1517 /* Initiator sends DHKey check first */
1518 if (hcon->out) {
1519 sc_dhkey_check(smp);
1520 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1521 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1522 sc_dhkey_check(smp);
1523 sc_add_ltk(smp);
1524 }
760b018b
JH
1525
1526 return 0;
1527}
1528
2b64d153
BG
1529int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1530{
b10e8017 1531 struct l2cap_conn *conn = hcon->l2cap_data;
5d88cc73 1532 struct l2cap_chan *chan;
2b64d153
BG
1533 struct smp_chan *smp;
1534 u32 value;
fc75cc86 1535 int err;
2b64d153
BG
1536
1537 BT_DBG("");
1538
fc75cc86 1539 if (!conn)
2b64d153
BG
1540 return -ENOTCONN;
1541
5d88cc73
JH
1542 chan = conn->smp;
1543 if (!chan)
1544 return -ENOTCONN;
1545
fc75cc86
JH
1546 l2cap_chan_lock(chan);
1547 if (!chan->data) {
1548 err = -ENOTCONN;
1549 goto unlock;
1550 }
1551
5d88cc73 1552 smp = chan->data;
2b64d153 1553
760b018b
JH
1554 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1555 err = sc_user_reply(smp, mgmt_op, passkey);
1556 goto unlock;
1557 }
1558
2b64d153
BG
1559 switch (mgmt_op) {
1560 case MGMT_OP_USER_PASSKEY_REPLY:
1561 value = le32_to_cpu(passkey);
943a732a 1562 memset(smp->tk, 0, sizeof(smp->tk));
2b64d153 1563 BT_DBG("PassKey: %d", value);
943a732a 1564 put_unaligned_le32(value, smp->tk);
2b64d153
BG
1565 /* Fall Through */
1566 case MGMT_OP_USER_CONFIRM_REPLY:
4a74d658 1567 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
1568 break;
1569 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1570 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
84794e11 1571 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
fc75cc86
JH
1572 err = 0;
1573 goto unlock;
2b64d153 1574 default:
84794e11 1575 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
fc75cc86
JH
1576 err = -EOPNOTSUPP;
1577 goto unlock;
2b64d153
BG
1578 }
1579
fc75cc86
JH
1580 err = 0;
1581
2b64d153 1582 /* If it is our turn to send Pairing Confirm, do so now */
1cc61144
JH
1583 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1584 u8 rsp = smp_confirm(smp);
1585 if (rsp)
1586 smp_failure(conn, rsp);
1587 }
2b64d153 1588
fc75cc86
JH
1589unlock:
1590 l2cap_chan_unlock(chan);
1591 return err;
2b64d153
BG
1592}
1593
b5ae344d
JH
1594static void build_bredr_pairing_cmd(struct smp_chan *smp,
1595 struct smp_cmd_pairing *req,
1596 struct smp_cmd_pairing *rsp)
1597{
1598 struct l2cap_conn *conn = smp->conn;
1599 struct hci_dev *hdev = conn->hcon->hdev;
1600 u8 local_dist = 0, remote_dist = 0;
1601
1602 if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) {
1603 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1604 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1605 }
1606
1607 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
1608 remote_dist |= SMP_DIST_ID_KEY;
1609
1610 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
1611 local_dist |= SMP_DIST_ID_KEY;
1612
1613 if (!rsp) {
1614 memset(req, 0, sizeof(*req));
1615
1616 req->init_key_dist = local_dist;
1617 req->resp_key_dist = remote_dist;
1618 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1619
1620 smp->remote_key_dist = remote_dist;
1621
1622 return;
1623 }
1624
1625 memset(rsp, 0, sizeof(*rsp));
1626
1627 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1628 rsp->init_key_dist = req->init_key_dist & remote_dist;
1629 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1630
1631 smp->remote_key_dist = rsp->init_key_dist;
1632}
1633
da85e5e5 1634static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1635{
3158c50c 1636 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
fc75cc86 1637 struct l2cap_chan *chan = conn->smp;
b3c6410b 1638 struct hci_dev *hdev = conn->hcon->hdev;
8aab4757 1639 struct smp_chan *smp;
c7262e71 1640 u8 key_size, auth, sec_level;
8aab4757 1641 int ret;
88ba43b6
AB
1642
1643 BT_DBG("conn %p", conn);
1644
c46b98be 1645 if (skb->len < sizeof(*req))
38e4a915 1646 return SMP_INVALID_PARAMS;
c46b98be 1647
40bef302 1648 if (conn->hcon->role != HCI_ROLE_SLAVE)
2b64d153
BG
1649 return SMP_CMD_NOTSUPP;
1650
fc75cc86 1651 if (!chan->data)
8aab4757 1652 smp = smp_chan_create(conn);
fc75cc86 1653 else
5d88cc73 1654 smp = chan->data;
8aab4757 1655
d08fd0e7
AE
1656 if (!smp)
1657 return SMP_UNSPECIFIED;
d26a2345 1658
c05b9339 1659 /* We didn't start the pairing, so match remote */
0edb14de 1660 auth = req->auth_req & AUTH_REQ_MASK(hdev);
c05b9339 1661
b6ae8457 1662 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
c05b9339 1663 (auth & SMP_AUTH_BONDING))
b3c6410b
JH
1664 return SMP_PAIRING_NOTSUPP;
1665
903b71c7
JH
1666 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1667 return SMP_AUTH_REQUIREMENTS;
1668
1c1def09
VCG
1669 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1670 memcpy(&smp->preq[1], req, sizeof(*req));
3158c50c 1671 skb_pull(skb, sizeof(*req));
88ba43b6 1672
b5ae344d
JH
1673 /* SMP over BR/EDR requires special treatment */
1674 if (conn->hcon->type == ACL_LINK) {
1675 /* We must have a BR/EDR SC link */
1676 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags))
1677 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1678
1679 set_bit(SMP_FLAG_SC, &smp->flags);
1680
1681 build_bredr_pairing_cmd(smp, req, &rsp);
1682
1683 key_size = min(req->max_key_size, rsp.max_key_size);
1684 if (check_enc_key_size(conn, key_size))
1685 return SMP_ENC_KEY_SIZE;
1686
1687 /* Clear bits which are generated but not distributed */
1688 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1689
1690 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1691 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1692 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1693
1694 smp_distribute_keys(smp);
1695 return 0;
1696 }
1697
5e3d3d9b
JH
1698 build_pairing_cmd(conn, req, &rsp, auth);
1699
1700 if (rsp.auth_req & SMP_AUTH_SC)
1701 set_bit(SMP_FLAG_SC, &smp->flags);
1702
5be5e275 1703 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
1afc2a1a
JH
1704 sec_level = BT_SECURITY_MEDIUM;
1705 else
1706 sec_level = authreq_to_seclevel(auth);
1707
c7262e71
JH
1708 if (sec_level > conn->hcon->pending_sec_level)
1709 conn->hcon->pending_sec_level = sec_level;
fdde0a26 1710
49c922bb 1711 /* If we need MITM check that it can be achieved */
2ed8f65c
JH
1712 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1713 u8 method;
1714
1715 method = get_auth_method(smp, conn->hcon->io_capability,
1716 req->io_capability);
1717 if (method == JUST_WORKS || method == JUST_CFM)
1718 return SMP_AUTH_REQUIREMENTS;
1719 }
1720
3158c50c
VCG
1721 key_size = min(req->max_key_size, rsp.max_key_size);
1722 if (check_enc_key_size(conn, key_size))
1723 return SMP_ENC_KEY_SIZE;
88ba43b6 1724
e84a6b13 1725 get_random_bytes(smp->prnd, sizeof(smp->prnd));
8aab4757 1726
1c1def09
VCG
1727 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1728 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
f01ead31 1729
3158c50c 1730 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
3b19146d
JH
1731
1732 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1733
1734 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1735 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1736 /* Clear bits which are generated but not distributed */
1737 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1738 /* Wait for Public Key from Initiating Device */
1739 return 0;
1740 } else {
1741 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1742 }
da85e5e5 1743
2b64d153
BG
1744 /* Request setup of TK */
1745 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1746 if (ret)
1747 return SMP_UNSPECIFIED;
1748
da85e5e5 1749 return 0;
88ba43b6
AB
1750}
1751
3b19146d
JH
1752static u8 sc_send_public_key(struct smp_chan *smp)
1753{
70157ef5
JH
1754 struct hci_dev *hdev = smp->conn->hcon->hdev;
1755
3b19146d
JH
1756 BT_DBG("");
1757
70157ef5
JH
1758 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) {
1759 BT_DBG("Using debug keys");
1760 memcpy(smp->local_pk, debug_pk, 64);
1761 memcpy(smp->local_sk, debug_sk, 32);
1762 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1763 } else {
1764 while (true) {
1765 /* Generate local key pair for Secure Connections */
1766 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1767 return SMP_UNSPECIFIED;
6c0dcc50 1768
70157ef5
JH
1769 /* This is unlikely, but we need to check that
1770 * we didn't accidentially generate a debug key.
1771 */
1772 if (memcmp(smp->local_sk, debug_sk, 32))
1773 break;
1774 }
6c0dcc50 1775 }
3b19146d 1776
c7a3d57d
JH
1777 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
1778 SMP_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1779 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
3b19146d
JH
1780
1781 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1782
1783 return 0;
1784}
1785
da85e5e5 1786static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1787{
3158c50c 1788 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
5d88cc73
JH
1789 struct l2cap_chan *chan = conn->smp;
1790 struct smp_chan *smp = chan->data;
0edb14de 1791 struct hci_dev *hdev = conn->hcon->hdev;
3a7dbfb8 1792 u8 key_size, auth;
7d24ddcc 1793 int ret;
88ba43b6
AB
1794
1795 BT_DBG("conn %p", conn);
1796
c46b98be 1797 if (skb->len < sizeof(*rsp))
38e4a915 1798 return SMP_INVALID_PARAMS;
c46b98be 1799
40bef302 1800 if (conn->hcon->role != HCI_ROLE_MASTER)
2b64d153
BG
1801 return SMP_CMD_NOTSUPP;
1802
3158c50c
VCG
1803 skb_pull(skb, sizeof(*rsp));
1804
1c1def09 1805 req = (void *) &smp->preq[1];
da85e5e5 1806
3158c50c
VCG
1807 key_size = min(req->max_key_size, rsp->max_key_size);
1808 if (check_enc_key_size(conn, key_size))
1809 return SMP_ENC_KEY_SIZE;
1810
0edb14de 1811 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
c05b9339 1812
903b71c7
JH
1813 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1814 return SMP_AUTH_REQUIREMENTS;
1815
b5ae344d
JH
1816 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1817 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1818
1819 /* Update remote key distribution in case the remote cleared
1820 * some bits that we had enabled in our request.
1821 */
1822 smp->remote_key_dist &= rsp->resp_key_dist;
1823
1824 /* For BR/EDR this means we're done and can start phase 3 */
1825 if (conn->hcon->type == ACL_LINK) {
1826 /* Clear bits which are generated but not distributed */
1827 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1828 smp_distribute_keys(smp);
1829 return 0;
1830 }
1831
65668776
JH
1832 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1833 set_bit(SMP_FLAG_SC, &smp->flags);
d2eb9e10
JH
1834 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1835 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
65668776 1836
49c922bb 1837 /* If we need MITM check that it can be achieved */
2ed8f65c
JH
1838 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1839 u8 method;
1840
1841 method = get_auth_method(smp, req->io_capability,
1842 rsp->io_capability);
1843 if (method == JUST_WORKS || method == JUST_CFM)
1844 return SMP_AUTH_REQUIREMENTS;
1845 }
1846
e84a6b13 1847 get_random_bytes(smp->prnd, sizeof(smp->prnd));
7d24ddcc 1848
fdcc4bec
JH
1849 /* Update remote key distribution in case the remote cleared
1850 * some bits that we had enabled in our request.
1851 */
1852 smp->remote_key_dist &= rsp->resp_key_dist;
1853
3b19146d
JH
1854 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1855 /* Clear bits which are generated but not distributed */
1856 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1857 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1858 return sc_send_public_key(smp);
1859 }
1860
c05b9339 1861 auth |= req->auth_req;
2b64d153 1862
476585ec 1863 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
2b64d153
BG
1864 if (ret)
1865 return SMP_UNSPECIFIED;
1866
4a74d658 1867 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
2b64d153
BG
1868
1869 /* Can't compose response until we have been confirmed */
4a74d658 1870 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1cc61144 1871 return smp_confirm(smp);
da85e5e5
VCG
1872
1873 return 0;
88ba43b6
AB
1874}
1875
dcee2b32
JH
1876static u8 sc_check_confirm(struct smp_chan *smp)
1877{
1878 struct l2cap_conn *conn = smp->conn;
1879
1880 BT_DBG("");
1881
1882 /* Public Key exchange must happen before any other steps */
1883 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1884 return SMP_UNSPECIFIED;
1885
38606f14
JH
1886 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1887 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1888
dcee2b32
JH
1889 if (conn->hcon->out) {
1890 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1891 smp->prnd);
1892 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1893 }
1894
1895 return 0;
1896}
1897
da85e5e5 1898static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1899{
5d88cc73
JH
1900 struct l2cap_chan *chan = conn->smp;
1901 struct smp_chan *smp = chan->data;
7d24ddcc 1902
88ba43b6
AB
1903 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1904
c46b98be 1905 if (skb->len < sizeof(smp->pcnf))
38e4a915 1906 return SMP_INVALID_PARAMS;
c46b98be 1907
1c1def09
VCG
1908 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1909 skb_pull(skb, sizeof(smp->pcnf));
88ba43b6 1910
dcee2b32
JH
1911 if (test_bit(SMP_FLAG_SC, &smp->flags))
1912 return sc_check_confirm(smp);
1913
b28b4943 1914 if (conn->hcon->out) {
943a732a
JH
1915 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1916 smp->prnd);
b28b4943
JH
1917 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1918 return 0;
1919 }
1920
1921 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1cc61144 1922 return smp_confirm(smp);
943a732a 1923 else
4a74d658 1924 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
da85e5e5
VCG
1925
1926 return 0;
88ba43b6
AB
1927}
1928
da85e5e5 1929static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1930{
5d88cc73
JH
1931 struct l2cap_chan *chan = conn->smp;
1932 struct smp_chan *smp = chan->data;
191dc7fe
JH
1933 struct hci_conn *hcon = conn->hcon;
1934 u8 *pkax, *pkbx, *na, *nb;
1935 u32 passkey;
1936 int err;
7d24ddcc 1937
8aab4757 1938 BT_DBG("conn %p", conn);
3158c50c 1939
c46b98be 1940 if (skb->len < sizeof(smp->rrnd))
38e4a915 1941 return SMP_INVALID_PARAMS;
c46b98be 1942
943a732a 1943 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
8aab4757 1944 skb_pull(skb, sizeof(smp->rrnd));
e7e62c85 1945
191dc7fe
JH
1946 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1947 return smp_random(smp);
1948
580039e8
JH
1949 if (hcon->out) {
1950 pkax = smp->local_pk;
1951 pkbx = smp->remote_pk;
1952 na = smp->prnd;
1953 nb = smp->rrnd;
1954 } else {
1955 pkax = smp->remote_pk;
1956 pkbx = smp->local_pk;
1957 na = smp->rrnd;
1958 nb = smp->prnd;
1959 }
1960
a29b0733
JH
1961 if (smp->method == REQ_OOB) {
1962 if (!hcon->out)
1963 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1964 sizeof(smp->prnd), smp->prnd);
1965 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1966 goto mackey_and_ltk;
1967 }
1968
38606f14
JH
1969 /* Passkey entry has special treatment */
1970 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1971 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
1972
191dc7fe
JH
1973 if (hcon->out) {
1974 u8 cfm[16];
1975
1976 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1977 smp->rrnd, 0, cfm);
1978 if (err)
1979 return SMP_UNSPECIFIED;
1980
1981 if (memcmp(smp->pcnf, cfm, 16))
1982 return SMP_CONFIRM_FAILED;
191dc7fe
JH
1983 } else {
1984 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1985 smp->prnd);
1986 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
191dc7fe
JH
1987 }
1988
a29b0733 1989mackey_and_ltk:
760b018b
JH
1990 /* Generate MacKey and LTK */
1991 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1992 if (err)
1993 return SMP_UNSPECIFIED;
1994
a29b0733 1995 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
dddd3059 1996 if (hcon->out) {
38606f14 1997 sc_dhkey_check(smp);
dddd3059
JH
1998 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1999 }
2000 return 0;
2001 }
2002
38606f14
JH
2003 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
2004 if (err)
2005 return SMP_UNSPECIFIED;
2006
2007 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2008 hcon->dst_type, passkey, 0);
191dc7fe
JH
2009 if (err)
2010 return SMP_UNSPECIFIED;
2011
38606f14
JH
2012 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2013
191dc7fe 2014 return 0;
88ba43b6
AB
2015}
2016
f81cd823 2017static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
988c5997 2018{
c9839a11 2019 struct smp_ltk *key;
988c5997
VCG
2020 struct hci_conn *hcon = conn->hcon;
2021
f3a73d97 2022 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
988c5997 2023 if (!key)
f81cd823 2024 return false;
988c5997 2025
a6f7833c 2026 if (smp_ltk_sec_level(key) < sec_level)
f81cd823 2027 return false;
4dab7864 2028
51a8efd7 2029 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
f81cd823 2030 return true;
988c5997 2031
c9839a11
VCG
2032 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
2033 hcon->enc_key_size = key->enc_size;
988c5997 2034
fe59a05f
JH
2035 /* We never store STKs for master role, so clear this flag */
2036 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2037
f81cd823 2038 return true;
988c5997 2039}
f1560463 2040
35dc6f83
JH
2041bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2042 enum smp_key_pref key_pref)
854f4727
JH
2043{
2044 if (sec_level == BT_SECURITY_LOW)
2045 return true;
2046
35dc6f83
JH
2047 /* If we're encrypted with an STK but the caller prefers using
2048 * LTK claim insufficient security. This way we allow the
2049 * connection to be re-encrypted with an LTK, even if the LTK
2050 * provides the same level of security. Only exception is if we
2051 * don't have an LTK (e.g. because of key distribution bits).
9ab65d60 2052 */
35dc6f83
JH
2053 if (key_pref == SMP_USE_LTK &&
2054 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
f3a73d97 2055 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
9ab65d60
JH
2056 return false;
2057
854f4727
JH
2058 if (hcon->sec_level >= sec_level)
2059 return true;
2060
2061 return false;
2062}
2063
da85e5e5 2064static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6
AB
2065{
2066 struct smp_cmd_security_req *rp = (void *) skb->data;
2067 struct smp_cmd_pairing cp;
f1cb9af5 2068 struct hci_conn *hcon = conn->hcon;
0edb14de 2069 struct hci_dev *hdev = hcon->hdev;
8aab4757 2070 struct smp_chan *smp;
c05b9339 2071 u8 sec_level, auth;
88ba43b6
AB
2072
2073 BT_DBG("conn %p", conn);
2074
c46b98be 2075 if (skb->len < sizeof(*rp))
38e4a915 2076 return SMP_INVALID_PARAMS;
c46b98be 2077
40bef302 2078 if (hcon->role != HCI_ROLE_MASTER)
86ca9eac
JH
2079 return SMP_CMD_NOTSUPP;
2080
0edb14de 2081 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
c05b9339 2082
903b71c7
JH
2083 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
2084 return SMP_AUTH_REQUIREMENTS;
2085
5be5e275 2086 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
1afc2a1a
JH
2087 sec_level = BT_SECURITY_MEDIUM;
2088 else
2089 sec_level = authreq_to_seclevel(auth);
2090
35dc6f83 2091 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
854f4727
JH
2092 return 0;
2093
c7262e71
JH
2094 if (sec_level > hcon->pending_sec_level)
2095 hcon->pending_sec_level = sec_level;
feb45eb5 2096
4dab7864 2097 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
988c5997
VCG
2098 return 0;
2099
8aab4757 2100 smp = smp_chan_create(conn);
c29d2444
JH
2101 if (!smp)
2102 return SMP_UNSPECIFIED;
d26a2345 2103
b6ae8457 2104 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
c05b9339 2105 (auth & SMP_AUTH_BONDING))
616d55be
JH
2106 return SMP_PAIRING_NOTSUPP;
2107
88ba43b6 2108 skb_pull(skb, sizeof(*rp));
88ba43b6 2109
da85e5e5 2110 memset(&cp, 0, sizeof(cp));
c05b9339 2111 build_pairing_cmd(conn, &cp, NULL, auth);
88ba43b6 2112
1c1def09
VCG
2113 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2114 memcpy(&smp->preq[1], &cp, sizeof(cp));
f01ead31 2115
88ba43b6 2116 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
b28b4943 2117 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
f1cb9af5 2118
da85e5e5 2119 return 0;
88ba43b6
AB
2120}
2121
cc110922 2122int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
eb492e01 2123{
cc110922 2124 struct l2cap_conn *conn = hcon->l2cap_data;
c68b7f12 2125 struct l2cap_chan *chan;
0a66cf20 2126 struct smp_chan *smp;
2b64d153 2127 __u8 authreq;
fc75cc86 2128 int ret;
eb492e01 2129
3a0259bb
VCG
2130 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2131
0a66cf20
JH
2132 /* This may be NULL if there's an unexpected disconnection */
2133 if (!conn)
2134 return 1;
2135
c68b7f12
JH
2136 chan = conn->smp;
2137
757aee0f 2138 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
2e65c9d2
AG
2139 return 1;
2140
35dc6f83 2141 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
eb492e01 2142 return 1;
f1cb9af5 2143
c7262e71
JH
2144 if (sec_level > hcon->pending_sec_level)
2145 hcon->pending_sec_level = sec_level;
2146
40bef302 2147 if (hcon->role == HCI_ROLE_MASTER)
c7262e71
JH
2148 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2149 return 0;
d26a2345 2150
fc75cc86
JH
2151 l2cap_chan_lock(chan);
2152
2153 /* If SMP is already in progress ignore this request */
2154 if (chan->data) {
2155 ret = 0;
2156 goto unlock;
2157 }
d26a2345 2158
8aab4757 2159 smp = smp_chan_create(conn);
fc75cc86
JH
2160 if (!smp) {
2161 ret = 1;
2162 goto unlock;
2163 }
2b64d153
BG
2164
2165 authreq = seclevel_to_authreq(sec_level);
d26a2345 2166
d2eb9e10
JH
2167 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
2168 authreq |= SMP_AUTH_SC;
2169
79897d20
JH
2170 /* Require MITM if IO Capability allows or the security level
2171 * requires it.
2e233644 2172 */
79897d20 2173 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
c7262e71 2174 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
2e233644
JH
2175 authreq |= SMP_AUTH_MITM;
2176
40bef302 2177 if (hcon->role == HCI_ROLE_MASTER) {
d26a2345 2178 struct smp_cmd_pairing cp;
f01ead31 2179
2b64d153 2180 build_pairing_cmd(conn, &cp, NULL, authreq);
1c1def09
VCG
2181 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2182 memcpy(&smp->preq[1], &cp, sizeof(cp));
f01ead31 2183
eb492e01 2184 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
b28b4943 2185 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
eb492e01
AB
2186 } else {
2187 struct smp_cmd_security_req cp;
2b64d153 2188 cp.auth_req = authreq;
eb492e01 2189 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
b28b4943 2190 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
eb492e01
AB
2191 }
2192
4a74d658 2193 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
fc75cc86 2194 ret = 0;
edca792c 2195
fc75cc86
JH
2196unlock:
2197 l2cap_chan_unlock(chan);
2198 return ret;
eb492e01
AB
2199}
2200
7034b911
VCG
2201static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2202{
16b90839 2203 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
5d88cc73
JH
2204 struct l2cap_chan *chan = conn->smp;
2205 struct smp_chan *smp = chan->data;
16b90839 2206
c46b98be
JH
2207 BT_DBG("conn %p", conn);
2208
2209 if (skb->len < sizeof(*rp))
38e4a915 2210 return SMP_INVALID_PARAMS;
c46b98be 2211
b28b4943 2212 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
6131ddc8 2213
16b90839
VCG
2214 skb_pull(skb, sizeof(*rp));
2215
1c1def09 2216 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
16b90839 2217
7034b911
VCG
2218 return 0;
2219}
2220
2221static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2222{
16b90839 2223 struct smp_cmd_master_ident *rp = (void *) skb->data;
5d88cc73
JH
2224 struct l2cap_chan *chan = conn->smp;
2225 struct smp_chan *smp = chan->data;
c9839a11
VCG
2226 struct hci_dev *hdev = conn->hcon->hdev;
2227 struct hci_conn *hcon = conn->hcon;
23d0e128 2228 struct smp_ltk *ltk;
c9839a11 2229 u8 authenticated;
16b90839 2230
c46b98be
JH
2231 BT_DBG("conn %p", conn);
2232
2233 if (skb->len < sizeof(*rp))
38e4a915 2234 return SMP_INVALID_PARAMS;
c46b98be 2235
9747a9f3
JH
2236 /* Mark the information as received */
2237 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2238
b28b4943
JH
2239 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2240 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
196332f5
JH
2241 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2242 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
b28b4943 2243
16b90839 2244 skb_pull(skb, sizeof(*rp));
7034b911 2245
ce39fb4e 2246 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
2ceba539 2247 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
23d0e128
JH
2248 authenticated, smp->tk, smp->enc_key_size,
2249 rp->ediv, rp->rand);
2250 smp->ltk = ltk;
c6e81e9a 2251 if (!(smp->remote_key_dist & KEY_DIST_MASK))
d6268e86 2252 smp_distribute_keys(smp);
7034b911
VCG
2253
2254 return 0;
2255}
2256
fd349c02
JH
2257static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2258{
2259 struct smp_cmd_ident_info *info = (void *) skb->data;
5d88cc73
JH
2260 struct l2cap_chan *chan = conn->smp;
2261 struct smp_chan *smp = chan->data;
fd349c02
JH
2262
2263 BT_DBG("");
2264
2265 if (skb->len < sizeof(*info))
38e4a915 2266 return SMP_INVALID_PARAMS;
fd349c02 2267
b28b4943 2268 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
6131ddc8 2269
fd349c02
JH
2270 skb_pull(skb, sizeof(*info));
2271
2272 memcpy(smp->irk, info->irk, 16);
2273
2274 return 0;
2275}
2276
2277static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2278 struct sk_buff *skb)
2279{
2280 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
5d88cc73
JH
2281 struct l2cap_chan *chan = conn->smp;
2282 struct smp_chan *smp = chan->data;
fd349c02
JH
2283 struct hci_conn *hcon = conn->hcon;
2284 bdaddr_t rpa;
2285
2286 BT_DBG("");
2287
2288 if (skb->len < sizeof(*info))
38e4a915 2289 return SMP_INVALID_PARAMS;
fd349c02 2290
9747a9f3
JH
2291 /* Mark the information as received */
2292 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2293
b28b4943
JH
2294 if (smp->remote_key_dist & SMP_DIST_SIGN)
2295 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2296
fd349c02
JH
2297 skb_pull(skb, sizeof(*info));
2298
a9a58f86
JH
2299 /* Strictly speaking the Core Specification (4.1) allows sending
2300 * an empty address which would force us to rely on just the IRK
2301 * as "identity information". However, since such
2302 * implementations are not known of and in order to not over
2303 * complicate our implementation, simply pretend that we never
2304 * received an IRK for such a device.
2305 */
2306 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
2307 BT_ERR("Ignoring IRK with no identity address");
31dd624e 2308 goto distribute;
a9a58f86
JH
2309 }
2310
fd349c02
JH
2311 bacpy(&smp->id_addr, &info->bdaddr);
2312 smp->id_addr_type = info->addr_type;
2313
2314 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2315 bacpy(&rpa, &hcon->dst);
2316 else
2317 bacpy(&rpa, BDADDR_ANY);
2318
23d0e128
JH
2319 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2320 smp->id_addr_type, smp->irk, &rpa);
fd349c02 2321
31dd624e 2322distribute:
c6e81e9a
JH
2323 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2324 smp_distribute_keys(smp);
fd349c02
JH
2325
2326 return 0;
2327}
2328
7ee4ea36
MH
2329static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2330{
2331 struct smp_cmd_sign_info *rp = (void *) skb->data;
5d88cc73
JH
2332 struct l2cap_chan *chan = conn->smp;
2333 struct smp_chan *smp = chan->data;
7ee4ea36
MH
2334 struct smp_csrk *csrk;
2335
2336 BT_DBG("conn %p", conn);
2337
2338 if (skb->len < sizeof(*rp))
38e4a915 2339 return SMP_INVALID_PARAMS;
7ee4ea36 2340
7ee4ea36
MH
2341 /* Mark the information as received */
2342 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2343
2344 skb_pull(skb, sizeof(*rp));
2345
7ee4ea36
MH
2346 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2347 if (csrk) {
2348 csrk->master = 0x01;
2349 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2350 }
2351 smp->csrk = csrk;
d6268e86 2352 smp_distribute_keys(smp);
7ee4ea36
MH
2353
2354 return 0;
2355}
2356
5e3d3d9b
JH
2357static u8 sc_select_method(struct smp_chan *smp)
2358{
2359 struct l2cap_conn *conn = smp->conn;
2360 struct hci_conn *hcon = conn->hcon;
2361 struct smp_cmd_pairing *local, *remote;
2362 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2363
a29b0733
JH
2364 if (test_bit(SMP_FLAG_OOB, &smp->flags))
2365 return REQ_OOB;
2366
5e3d3d9b
JH
2367 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2368 * which are needed as inputs to some crypto functions. To get
2369 * the "struct smp_cmd_pairing" from them we need to skip the
2370 * first byte which contains the opcode.
2371 */
2372 if (hcon->out) {
2373 local = (void *) &smp->preq[1];
2374 remote = (void *) &smp->prsp[1];
2375 } else {
2376 local = (void *) &smp->prsp[1];
2377 remote = (void *) &smp->preq[1];
2378 }
2379
2380 local_io = local->io_capability;
2381 remote_io = remote->io_capability;
2382
2383 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2384 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2385
2386 /* If either side wants MITM, look up the method from the table,
2387 * otherwise use JUST WORKS.
2388 */
2389 if (local_mitm || remote_mitm)
2390 method = get_auth_method(smp, local_io, remote_io);
2391 else
2392 method = JUST_WORKS;
2393
2394 /* Don't confirm locally initiated pairing attempts */
2395 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2396 method = JUST_WORKS;
2397
2398 return method;
2399}
2400
d8f8edbe
JH
2401static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2402{
2403 struct smp_cmd_public_key *key = (void *) skb->data;
2404 struct hci_conn *hcon = conn->hcon;
2405 struct l2cap_chan *chan = conn->smp;
2406 struct smp_chan *smp = chan->data;
5e3d3d9b 2407 struct hci_dev *hdev = hcon->hdev;
cbbbe3e2 2408 struct smp_cmd_pairing_confirm cfm;
d8f8edbe
JH
2409 int err;
2410
2411 BT_DBG("conn %p", conn);
2412
2413 if (skb->len < sizeof(*key))
2414 return SMP_INVALID_PARAMS;
2415
2416 memcpy(smp->remote_pk, key, 64);
2417
2418 /* Non-initiating device sends its public key after receiving
2419 * the key from the initiating device.
2420 */
2421 if (!hcon->out) {
2422 err = sc_send_public_key(smp);
2423 if (err)
2424 return err;
2425 }
2426
c7a3d57d
JH
2427 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2428 SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
d8f8edbe
JH
2429
2430 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2431 return SMP_UNSPECIFIED;
2432
c7a3d57d 2433 SMP_DBG("DHKey %32phN", smp->dhkey);
d8f8edbe
JH
2434
2435 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2436
5e3d3d9b
JH
2437 smp->method = sc_select_method(smp);
2438
2439 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2440
2441 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2442 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2443 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2444 else
2445 hcon->pending_sec_level = BT_SECURITY_FIPS;
2446
aeb7d461
JH
2447 if (!memcmp(debug_pk, smp->remote_pk, 64))
2448 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2449
38606f14
JH
2450 if (smp->method == DSP_PASSKEY) {
2451 get_random_bytes(&hcon->passkey_notify,
2452 sizeof(hcon->passkey_notify));
2453 hcon->passkey_notify %= 1000000;
2454 hcon->passkey_entered = 0;
2455 smp->passkey_round = 0;
2456 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2457 hcon->dst_type,
2458 hcon->passkey_notify,
2459 hcon->passkey_entered))
2460 return SMP_UNSPECIFIED;
2461 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2462 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2463 }
2464
a29b0733
JH
2465 if (smp->method == REQ_OOB) {
2466 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2467 smp->rr, 0, cfm.confirm_val);
2468 if (err)
2469 return SMP_UNSPECIFIED;
2470
2471 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2472 return SMP_CONFIRM_FAILED;
2473
2474 if (hcon->out)
2475 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2476 sizeof(smp->prnd), smp->prnd);
2477
2478 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2479
2480 return 0;
2481 }
2482
38606f14
JH
2483 if (hcon->out)
2484 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2485
2486 if (smp->method == REQ_PASSKEY) {
2487 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2488 hcon->dst_type))
2489 return SMP_UNSPECIFIED;
2490 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2491 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2492 return 0;
2493 }
2494
cbbbe3e2
JH
2495 /* The Initiating device waits for the non-initiating device to
2496 * send the confirm value.
2497 */
2498 if (conn->hcon->out)
2499 return 0;
2500
2501 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2502 0, cfm.confirm_val);
2503 if (err)
2504 return SMP_UNSPECIFIED;
2505
2506 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2507 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2508
d8f8edbe
JH
2509 return 0;
2510}
2511
6433a9a2
JH
2512static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2513{
2514 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2515 struct l2cap_chan *chan = conn->smp;
2516 struct hci_conn *hcon = conn->hcon;
2517 struct smp_chan *smp = chan->data;
2518 u8 a[7], b[7], *local_addr, *remote_addr;
2519 u8 io_cap[3], r[16], e[16];
2520 int err;
2521
2522 BT_DBG("conn %p", conn);
2523
2524 if (skb->len < sizeof(*check))
2525 return SMP_INVALID_PARAMS;
2526
2527 memcpy(a, &hcon->init_addr, 6);
2528 memcpy(b, &hcon->resp_addr, 6);
2529 a[6] = hcon->init_addr_type;
2530 b[6] = hcon->resp_addr_type;
2531
2532 if (hcon->out) {
2533 local_addr = a;
2534 remote_addr = b;
2535 memcpy(io_cap, &smp->prsp[1], 3);
2536 } else {
2537 local_addr = b;
2538 remote_addr = a;
2539 memcpy(io_cap, &smp->preq[1], 3);
2540 }
2541
2542 memset(r, 0, sizeof(r));
2543
38606f14
JH
2544 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2545 put_unaligned_le32(hcon->passkey_notify, r);
2546
6433a9a2
JH
2547 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2548 io_cap, remote_addr, local_addr, e);
2549 if (err)
2550 return SMP_UNSPECIFIED;
2551
2552 if (memcmp(check->e, e, 16))
2553 return SMP_DHKEY_CHECK_FAILED;
2554
d3e54a87
JH
2555 if (!hcon->out) {
2556 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2557 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2558 return 0;
2559 }
d378a2d7 2560
d3e54a87
JH
2561 /* Slave sends DHKey check as response to master */
2562 sc_dhkey_check(smp);
2563 }
d378a2d7 2564
d3e54a87 2565 sc_add_ltk(smp);
6433a9a2
JH
2566
2567 if (hcon->out) {
2568 hci_le_start_enc(hcon, 0, 0, smp->tk);
2569 hcon->enc_key_size = smp->enc_key_size;
2570 }
2571
2572 return 0;
2573}
2574
1408bb6e
JH
2575static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2576 struct sk_buff *skb)
2577{
2578 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2579
2580 BT_DBG("value 0x%02x", kp->value);
2581
2582 return 0;
2583}
2584
4befb867 2585static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
eb492e01 2586{
5d88cc73 2587 struct l2cap_conn *conn = chan->conn;
7b9899db 2588 struct hci_conn *hcon = conn->hcon;
b28b4943 2589 struct smp_chan *smp;
92381f5c 2590 __u8 code, reason;
eb492e01
AB
2591 int err = 0;
2592
8ae9b984 2593 if (skb->len < 1)
92381f5c 2594 return -EILSEQ;
92381f5c 2595
06ae3314 2596 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
2e65c9d2
AG
2597 reason = SMP_PAIRING_NOTSUPP;
2598 goto done;
2599 }
2600
92381f5c 2601 code = skb->data[0];
eb492e01
AB
2602 skb_pull(skb, sizeof(code));
2603
b28b4943
JH
2604 smp = chan->data;
2605
2606 if (code > SMP_CMD_MAX)
2607 goto drop;
2608
24bd0bd9 2609 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
b28b4943
JH
2610 goto drop;
2611
2612 /* If we don't have a context the only allowed commands are
2613 * pairing request and security request.
8cf9fa12 2614 */
b28b4943
JH
2615 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2616 goto drop;
8cf9fa12 2617
eb492e01
AB
2618 switch (code) {
2619 case SMP_CMD_PAIRING_REQ:
da85e5e5 2620 reason = smp_cmd_pairing_req(conn, skb);
eb492e01
AB
2621 break;
2622
2623 case SMP_CMD_PAIRING_FAIL:
84794e11 2624 smp_failure(conn, 0);
da85e5e5 2625 err = -EPERM;
eb492e01
AB
2626 break;
2627
2628 case SMP_CMD_PAIRING_RSP:
da85e5e5 2629 reason = smp_cmd_pairing_rsp(conn, skb);
88ba43b6
AB
2630 break;
2631
2632 case SMP_CMD_SECURITY_REQ:
da85e5e5 2633 reason = smp_cmd_security_req(conn, skb);
88ba43b6
AB
2634 break;
2635
eb492e01 2636 case SMP_CMD_PAIRING_CONFIRM:
da85e5e5 2637 reason = smp_cmd_pairing_confirm(conn, skb);
88ba43b6
AB
2638 break;
2639
eb492e01 2640 case SMP_CMD_PAIRING_RANDOM:
da85e5e5 2641 reason = smp_cmd_pairing_random(conn, skb);
88ba43b6
AB
2642 break;
2643
eb492e01 2644 case SMP_CMD_ENCRYPT_INFO:
7034b911
VCG
2645 reason = smp_cmd_encrypt_info(conn, skb);
2646 break;
2647
eb492e01 2648 case SMP_CMD_MASTER_IDENT:
7034b911
VCG
2649 reason = smp_cmd_master_ident(conn, skb);
2650 break;
2651
eb492e01 2652 case SMP_CMD_IDENT_INFO:
fd349c02
JH
2653 reason = smp_cmd_ident_info(conn, skb);
2654 break;
2655
eb492e01 2656 case SMP_CMD_IDENT_ADDR_INFO:
fd349c02
JH
2657 reason = smp_cmd_ident_addr_info(conn, skb);
2658 break;
2659
eb492e01 2660 case SMP_CMD_SIGN_INFO:
7ee4ea36 2661 reason = smp_cmd_sign_info(conn, skb);
7034b911
VCG
2662 break;
2663
d8f8edbe
JH
2664 case SMP_CMD_PUBLIC_KEY:
2665 reason = smp_cmd_public_key(conn, skb);
2666 break;
2667
6433a9a2
JH
2668 case SMP_CMD_DHKEY_CHECK:
2669 reason = smp_cmd_dhkey_check(conn, skb);
2670 break;
2671
1408bb6e
JH
2672 case SMP_CMD_KEYPRESS_NOTIFY:
2673 reason = smp_cmd_keypress_notify(conn, skb);
2674 break;
2675
eb492e01
AB
2676 default:
2677 BT_DBG("Unknown command code 0x%2.2x", code);
eb492e01 2678 reason = SMP_CMD_NOTSUPP;
3a0259bb 2679 goto done;
eb492e01
AB
2680 }
2681
3a0259bb 2682done:
9b7b18ef
JH
2683 if (!err) {
2684 if (reason)
2685 smp_failure(conn, reason);
8ae9b984 2686 kfree_skb(skb);
9b7b18ef
JH
2687 }
2688
eb492e01 2689 return err;
b28b4943
JH
2690
2691drop:
2692 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2693 code, &hcon->dst);
2694 kfree_skb(skb);
2695 return 0;
eb492e01 2696}
7034b911 2697
70db83c4
JH
2698static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2699{
2700 struct l2cap_conn *conn = chan->conn;
2701
2702 BT_DBG("chan %p", chan);
2703
fc75cc86 2704 if (chan->data)
5d88cc73 2705 smp_chan_destroy(conn);
5d88cc73 2706
70db83c4
JH
2707 conn->smp = NULL;
2708 l2cap_chan_put(chan);
2709}
2710
b5ae344d
JH
2711static void bredr_pairing(struct l2cap_chan *chan)
2712{
2713 struct l2cap_conn *conn = chan->conn;
2714 struct hci_conn *hcon = conn->hcon;
2715 struct hci_dev *hdev = hcon->hdev;
2716 struct smp_cmd_pairing req;
2717 struct smp_chan *smp;
2718
2719 BT_DBG("chan %p", chan);
2720
2721 /* Only new pairings are interesting */
2722 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2723 return;
2724
2725 /* Don't bother if we're not encrypted */
2726 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2727 return;
2728
2729 /* Only master may initiate SMP over BR/EDR */
2730 if (hcon->role != HCI_ROLE_MASTER)
2731 return;
2732
2733 /* Secure Connections support must be enabled */
2734 if (!test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
2735 return;
2736
2737 /* BR/EDR must use Secure Connections for SMP */
2738 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
2739 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2740 return;
2741
2742 /* If our LE support is not enabled don't do anything */
2743 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
2744 return;
2745
2746 /* Don't bother if remote LE support is not enabled */
2747 if (!lmp_host_le_capable(hcon))
2748 return;
2749
2750 /* Remote must support SMP fixed chan for BR/EDR */
2751 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2752 return;
2753
2754 /* Don't bother if SMP is already ongoing */
2755 if (chan->data)
2756 return;
2757
2758 smp = smp_chan_create(conn);
2759 if (!smp) {
2760 BT_ERR("%s unable to create SMP context for BR/EDR",
2761 hdev->name);
2762 return;
2763 }
2764
2765 set_bit(SMP_FLAG_SC, &smp->flags);
2766
2767 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2768
2769 /* Prepare and send the BR/EDR SMP Pairing Request */
2770 build_bredr_pairing_cmd(smp, &req, NULL);
2771
2772 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2773 memcpy(&smp->preq[1], &req, sizeof(req));
2774
2775 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2776 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2777}
2778
44f1a7ab
JH
2779static void smp_resume_cb(struct l2cap_chan *chan)
2780{
b68fda68 2781 struct smp_chan *smp = chan->data;
44f1a7ab
JH
2782 struct l2cap_conn *conn = chan->conn;
2783 struct hci_conn *hcon = conn->hcon;
2784
2785 BT_DBG("chan %p", chan);
2786
b5ae344d
JH
2787 if (hcon->type == ACL_LINK) {
2788 bredr_pairing(chan);
ef8efe4b 2789 return;
b5ae344d 2790 }
ef8efe4b 2791
86d1407c
JH
2792 if (!smp)
2793 return;
b68fda68 2794
84bc0db5
JH
2795 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2796 return;
2797
86d1407c
JH
2798 cancel_delayed_work(&smp->security_timer);
2799
d6268e86 2800 smp_distribute_keys(smp);
44f1a7ab
JH
2801}
2802
70db83c4
JH
2803static void smp_ready_cb(struct l2cap_chan *chan)
2804{
2805 struct l2cap_conn *conn = chan->conn;
b5ae344d 2806 struct hci_conn *hcon = conn->hcon;
70db83c4
JH
2807
2808 BT_DBG("chan %p", chan);
2809
2810 conn->smp = chan;
2811 l2cap_chan_hold(chan);
b5ae344d
JH
2812
2813 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2814 bredr_pairing(chan);
70db83c4
JH
2815}
2816
4befb867
JH
2817static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2818{
2819 int err;
2820
2821 BT_DBG("chan %p", chan);
2822
2823 err = smp_sig_channel(chan, skb);
2824 if (err) {
b68fda68 2825 struct smp_chan *smp = chan->data;
4befb867 2826
b68fda68
JH
2827 if (smp)
2828 cancel_delayed_work_sync(&smp->security_timer);
4befb867 2829
1e91c29e 2830 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
4befb867
JH
2831 }
2832
2833 return err;
2834}
2835
70db83c4
JH
2836static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2837 unsigned long hdr_len,
2838 unsigned long len, int nb)
2839{
2840 struct sk_buff *skb;
2841
2842 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2843 if (!skb)
2844 return ERR_PTR(-ENOMEM);
2845
2846 skb->priority = HCI_PRIO_MAX;
2847 bt_cb(skb)->chan = chan;
2848
2849 return skb;
2850}
2851
2852static const struct l2cap_ops smp_chan_ops = {
2853 .name = "Security Manager",
2854 .ready = smp_ready_cb,
5d88cc73 2855 .recv = smp_recv_cb,
70db83c4
JH
2856 .alloc_skb = smp_alloc_skb_cb,
2857 .teardown = smp_teardown_cb,
44f1a7ab 2858 .resume = smp_resume_cb,
70db83c4
JH
2859
2860 .new_connection = l2cap_chan_no_new_connection,
70db83c4
JH
2861 .state_change = l2cap_chan_no_state_change,
2862 .close = l2cap_chan_no_close,
2863 .defer = l2cap_chan_no_defer,
2864 .suspend = l2cap_chan_no_suspend,
70db83c4
JH
2865 .set_shutdown = l2cap_chan_no_set_shutdown,
2866 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
70db83c4
JH
2867};
2868
2869static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2870{
2871 struct l2cap_chan *chan;
2872
2873 BT_DBG("pchan %p", pchan);
2874
2875 chan = l2cap_chan_create();
2876 if (!chan)
2877 return NULL;
2878
2879 chan->chan_type = pchan->chan_type;
2880 chan->ops = &smp_chan_ops;
2881 chan->scid = pchan->scid;
2882 chan->dcid = chan->scid;
2883 chan->imtu = pchan->imtu;
2884 chan->omtu = pchan->omtu;
2885 chan->mode = pchan->mode;
2886
abe84903
JH
2887 /* Other L2CAP channels may request SMP routines in order to
2888 * change the security level. This means that the SMP channel
2889 * lock must be considered in its own category to avoid lockdep
2890 * warnings.
2891 */
2892 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2893
70db83c4
JH
2894 BT_DBG("created chan %p", chan);
2895
2896 return chan;
2897}
2898
2899static const struct l2cap_ops smp_root_chan_ops = {
2900 .name = "Security Manager Root",
2901 .new_connection = smp_new_conn_cb,
2902
2903 /* None of these are implemented for the root channel */
2904 .close = l2cap_chan_no_close,
2905 .alloc_skb = l2cap_chan_no_alloc_skb,
2906 .recv = l2cap_chan_no_recv,
2907 .state_change = l2cap_chan_no_state_change,
2908 .teardown = l2cap_chan_no_teardown,
2909 .ready = l2cap_chan_no_ready,
2910 .defer = l2cap_chan_no_defer,
2911 .suspend = l2cap_chan_no_suspend,
2912 .resume = l2cap_chan_no_resume,
2913 .set_shutdown = l2cap_chan_no_set_shutdown,
2914 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
70db83c4
JH
2915};
2916
ef8efe4b 2917static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
711eafe3 2918{
70db83c4 2919 struct l2cap_chan *chan;
defce9e8 2920 struct crypto_blkcipher *tfm_aes;
70db83c4 2921
ef8efe4b
JH
2922 if (cid == L2CAP_CID_SMP_BREDR) {
2923 tfm_aes = NULL;
2924 goto create_chan;
2925 }
711eafe3 2926
adae20cb 2927 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
defce9e8 2928 if (IS_ERR(tfm_aes)) {
711eafe3 2929 BT_ERR("Unable to create crypto context");
ef8efe4b 2930 return ERR_PTR(PTR_ERR(tfm_aes));
711eafe3
JH
2931 }
2932
ef8efe4b 2933create_chan:
70db83c4
JH
2934 chan = l2cap_chan_create();
2935 if (!chan) {
defce9e8 2936 crypto_free_blkcipher(tfm_aes);
ef8efe4b 2937 return ERR_PTR(-ENOMEM);
70db83c4
JH
2938 }
2939
defce9e8
JH
2940 chan->data = tfm_aes;
2941
ef8efe4b 2942 l2cap_add_scid(chan, cid);
70db83c4
JH
2943
2944 l2cap_chan_set_defaults(chan);
2945
2946 bacpy(&chan->src, &hdev->bdaddr);
ef8efe4b
JH
2947 if (cid == L2CAP_CID_SMP)
2948 chan->src_type = BDADDR_LE_PUBLIC;
2949 else
2950 chan->src_type = BDADDR_BREDR;
70db83c4
JH
2951 chan->state = BT_LISTEN;
2952 chan->mode = L2CAP_MODE_BASIC;
2953 chan->imtu = L2CAP_DEFAULT_MTU;
2954 chan->ops = &smp_root_chan_ops;
2955
abe84903
JH
2956 /* Set correct nesting level for a parent/listening channel */
2957 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2958
ef8efe4b 2959 return chan;
711eafe3
JH
2960}
2961
ef8efe4b 2962static void smp_del_chan(struct l2cap_chan *chan)
711eafe3 2963{
ef8efe4b 2964 struct crypto_blkcipher *tfm_aes;
70db83c4 2965
ef8efe4b 2966 BT_DBG("chan %p", chan);
711eafe3 2967
defce9e8
JH
2968 tfm_aes = chan->data;
2969 if (tfm_aes) {
2970 chan->data = NULL;
2971 crypto_free_blkcipher(tfm_aes);
711eafe3 2972 }
70db83c4 2973
70db83c4 2974 l2cap_chan_put(chan);
711eafe3 2975}
ef8efe4b
JH
2976
2977int smp_register(struct hci_dev *hdev)
2978{
2979 struct l2cap_chan *chan;
2980
2981 BT_DBG("%s", hdev->name);
2982
2983 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
2984 if (IS_ERR(chan))
2985 return PTR_ERR(chan);
2986
2987 hdev->smp_data = chan;
2988
2989 if (!lmp_sc_capable(hdev) &&
2990 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2991 return 0;
2992
2993 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
2994 if (IS_ERR(chan)) {
2995 int err = PTR_ERR(chan);
2996 chan = hdev->smp_data;
2997 hdev->smp_data = NULL;
2998 smp_del_chan(chan);
2999 return err;
3000 }
3001
3002 hdev->smp_bredr_data = chan;
3003
3004 return 0;
3005}
3006
3007void smp_unregister(struct hci_dev *hdev)
3008{
3009 struct l2cap_chan *chan;
3010
3011 if (hdev->smp_bredr_data) {
3012 chan = hdev->smp_bredr_data;
3013 hdev->smp_bredr_data = NULL;
3014 smp_del_chan(chan);
3015 }
3016
3017 if (hdev->smp_data) {
3018 chan = hdev->smp_data;
3019 hdev->smp_data = NULL;
3020 smp_del_chan(chan);
3021 }
3022}