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