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