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