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