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