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