blob: 0980b86574bd444531e67999ef7c1371c3ce3769 [file] [log] [blame]
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001/*
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
Gustavo Padovan8c520a52012-05-23 04:04:22 -030023#include <linux/crypto.h>
24#include <linux/scatterlist.h>
25#include <crypto/b128ops.h>
26
Anderson Brigliaeb492e02011-06-09 18:50:40 -030027#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080030#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070031
Johan Hedberg3b191462014-06-06 10:50:15 +030032#include "ecc.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070033#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030034
Johan Hedbergb28b4942014-09-05 22:19:55 +030035#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030036
Johan Hedberg3b191462014-06-06 10:50:15 +030037/* Keys which are not distributed with Secure Connections */
38#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
39
Marcel Holtmann17b02e62012-03-01 14:32:37 -080040#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030041
Johan Hedberg0edb14d2014-05-26 13:29:28 +030042#define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
43 0x1f : 0x07)
44#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020045
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030046/* Maximum message length that can be passed to aes_cmac */
47#define CMAC_MSG_MAX 80
48
Johan Hedberg533e35d2014-06-16 19:25:18 +030049enum {
50 SMP_FLAG_TK_VALID,
51 SMP_FLAG_CFM_PENDING,
52 SMP_FLAG_MITM_AUTH,
53 SMP_FLAG_COMPLETE,
54 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030055 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030056 SMP_FLAG_REMOTE_PK,
Johan Hedberg533e35d2014-06-16 19:25:18 +030057};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030058
59struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030060 struct l2cap_conn *conn;
61 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030062 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030063
Johan Hedberg4bc58f52014-05-20 09:45:47 +030064 u8 preq[7]; /* SMP Pairing Request */
65 u8 prsp[7]; /* SMP Pairing Response */
66 u8 prnd[16]; /* SMP Pairing Random (local) */
67 u8 rrnd[16]; /* SMP Pairing Random (remote) */
68 u8 pcnf[16]; /* SMP Pairing Confirm */
69 u8 tk[16]; /* SMP Temporary Key */
70 u8 enc_key_size;
71 u8 remote_key_dist;
72 bdaddr_t id_addr;
73 u8 id_addr_type;
74 u8 irk[16];
75 struct smp_csrk *csrk;
76 struct smp_csrk *slave_csrk;
77 struct smp_ltk *ltk;
78 struct smp_ltk *slave_ltk;
79 struct smp_irk *remote_irk;
Johan Hedberg4a74d652014-05-20 09:45:50 +030080 unsigned long flags;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030081
Johan Hedberg3b191462014-06-06 10:50:15 +030082 /* Secure Connections variables */
83 u8 local_pk[64];
84 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030085 u8 remote_pk[64];
86 u8 dhkey[32];
Johan Hedberg3b191462014-06-06 10:50:15 +030087
Johan Hedberg6a7bd102014-06-27 14:23:03 +030088 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +030089 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030090};
91
Johan Hedberg8a2936f2014-06-16 19:25:19 +030092static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030093{
Johan Hedberg8a2936f2014-06-16 19:25:19 +030094 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030095
Johan Hedberg8a2936f2014-06-16 19:25:19 +030096 for (i = 0; i < len; i++)
97 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030098}
99
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300100static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
101 size_t len, u8 mac[16])
102{
103 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
104 struct hash_desc desc;
105 struct scatterlist sg;
106 int err;
107
108 if (len > CMAC_MSG_MAX)
109 return -EFBIG;
110
111 if (!tfm) {
112 BT_ERR("tfm %p", tfm);
113 return -EINVAL;
114 }
115
116 desc.tfm = tfm;
117 desc.flags = 0;
118
119 crypto_hash_init(&desc);
120
121 /* Swap key and message from LSB to MSB */
122 swap_buf(k, tmp, 16);
123 swap_buf(m, msg_msb, len);
124
125 BT_DBG("msg (len %zu) %*phN", len, (int) len, m);
126 BT_DBG("key %16phN", k);
127
128 err = crypto_hash_setkey(tfm, tmp, 16);
129 if (err) {
130 BT_ERR("cipher setkey failed: %d", err);
131 return err;
132 }
133
134 sg_init_one(&sg, msg_msb, len);
135
136 err = crypto_hash_update(&desc, &sg, len);
137 if (err) {
138 BT_ERR("Hash update error %d", err);
139 return err;
140 }
141
142 err = crypto_hash_final(&desc, mac_msb);
143 if (err) {
144 BT_ERR("Hash final error %d", err);
145 return err;
146 }
147
148 swap_buf(mac_msb, mac, 16);
149
150 BT_DBG("mac %16phN", mac);
151
152 return 0;
153}
154
155static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
156 const u8 x[16], u8 z, u8 res[16])
157{
158 u8 m[65];
159 int err;
160
161 BT_DBG("u %32phN", u);
162 BT_DBG("v %32phN", v);
163 BT_DBG("x %16phN z %02x", x, z);
164
165 m[0] = z;
166 memcpy(m + 1, v, 32);
167 memcpy(m + 33, u, 32);
168
169 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
170 if (err)
171 return err;
172
173 BT_DBG("res %16phN", res);
174
175 return err;
176}
177
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300178static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
179{
180 struct blkcipher_desc desc;
181 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200182 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200183 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300184
185 if (tfm == NULL) {
186 BT_ERR("tfm %p", tfm);
187 return -EINVAL;
188 }
189
190 desc.tfm = tfm;
191 desc.flags = 0;
192
Johan Hedberg943a7322014-03-18 12:58:24 +0200193 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300194 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200195
196 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300197 if (err) {
198 BT_ERR("cipher setkey failed: %d", err);
199 return err;
200 }
201
Johan Hedberg943a7322014-03-18 12:58:24 +0200202 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300203 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200204
205 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300206
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300207 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
208 if (err)
209 BT_ERR("Encrypt data error %d", err);
210
Johan Hedberg943a7322014-03-18 12:58:24 +0200211 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300212 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200213
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300214 return err;
215}
216
Johan Hedberg60478052014-02-18 10:19:31 +0200217static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
218{
Johan Hedberg943a7322014-03-18 12:58:24 +0200219 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200220 int err;
221
222 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200223 memcpy(_res, r, 3);
224 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200225
Johan Hedberg943a7322014-03-18 12:58:24 +0200226 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200227 if (err) {
228 BT_ERR("Encrypt error");
229 return err;
230 }
231
232 /* The output of the random address function ah is:
233 * ah(h, r) = e(k, r') mod 2^24
234 * The output of the security function e is then truncated to 24 bits
235 * by taking the least significant 24 bits of the output of e as the
236 * result of ah.
237 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200238 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200239
240 return 0;
241}
242
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300243bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200244{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300245 struct l2cap_chan *chan = hdev->smp_data;
246 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200247 u8 hash[3];
248 int err;
249
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300250 if (!chan || !chan->data)
251 return false;
252
253 tfm = chan->data;
254
Johan Hedberg60478052014-02-18 10:19:31 +0200255 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
256
257 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
258 if (err)
259 return false;
260
261 return !memcmp(bdaddr->b, hash, 3);
262}
263
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300264int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200265{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300266 struct l2cap_chan *chan = hdev->smp_data;
267 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200268 int err;
269
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300270 if (!chan || !chan->data)
271 return -EOPNOTSUPP;
272
273 tfm = chan->data;
274
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200275 get_random_bytes(&rpa->b[3], 3);
276
277 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
278 rpa->b[5] |= 0x40; /* Set second most significant bit */
279
280 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
281 if (err < 0)
282 return err;
283
284 BT_DBG("RPA %pMR", rpa);
285
286 return 0;
287}
288
Johan Hedberge491eaf2014-10-25 21:15:37 +0200289static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
290 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
291 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300292{
293 u8 p1[16], p2[16];
294 int err;
295
296 memset(p1, 0, 16);
297
298 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200299 p1[0] = _iat;
300 p1[1] = _rat;
301 memcpy(p1 + 2, preq, 7);
302 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300303
304 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200305 memcpy(p2, ra, 6);
306 memcpy(p2 + 6, ia, 6);
307 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300308
309 /* res = r XOR p1 */
310 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
311
312 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200313 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300314 if (err) {
315 BT_ERR("Encrypt data error");
316 return err;
317 }
318
319 /* res = res XOR p2 */
320 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
321
322 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200323 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300324 if (err)
325 BT_ERR("Encrypt data error");
326
327 return err;
328}
329
Johan Hedberge491eaf2014-10-25 21:15:37 +0200330static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
331 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300332{
333 int err;
334
335 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200336 memcpy(_r, r2, 8);
337 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300338
Johan Hedberge491eaf2014-10-25 21:15:37 +0200339 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300340 if (err)
341 BT_ERR("Encrypt data error");
342
343 return err;
344}
345
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300346static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
347{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300348 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300349 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300350 struct kvec iv[2];
351 struct msghdr msg;
352
353 if (!chan)
354 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300355
356 BT_DBG("code 0x%2.2x", code);
357
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300358 iv[0].iov_base = &code;
359 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300360
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300361 iv[1].iov_base = data;
362 iv[1].iov_len = len;
363
364 memset(&msg, 0, sizeof(msg));
365
366 msg.msg_iov = (struct iovec *) &iv;
367 msg.msg_iovlen = 2;
368
369 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300370
Johan Hedbergb68fda62014-08-11 22:06:40 +0300371 if (!chan->data)
372 return;
373
374 smp = chan->data;
375
376 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300377 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300378}
379
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300380static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800381{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300382 if (authreq & SMP_AUTH_MITM) {
383 if (authreq & SMP_AUTH_SC)
384 return BT_SECURITY_FIPS;
385 else
386 return BT_SECURITY_HIGH;
387 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800388 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300389 }
Brian Gix2b64d152011-12-21 16:12:12 -0800390}
391
392static __u8 seclevel_to_authreq(__u8 sec_level)
393{
394 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300395 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800396 case BT_SECURITY_HIGH:
397 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
398 case BT_SECURITY_MEDIUM:
399 return SMP_AUTH_BONDING;
400 default:
401 return SMP_AUTH_NONE;
402 }
403}
404
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300405static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700406 struct smp_cmd_pairing *req,
407 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300408{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300409 struct l2cap_chan *chan = conn->smp;
410 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200411 struct hci_conn *hcon = conn->hcon;
412 struct hci_dev *hdev = hcon->hdev;
413 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300414
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300415 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700416 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
417 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300418 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800419 } else {
420 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300421 }
422
Johan Hedbergfd349c02014-02-18 10:19:36 +0200423 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
424 remote_dist |= SMP_DIST_ID_KEY;
425
Johan Hedberg863efaf2014-02-22 19:06:32 +0200426 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
427 local_dist |= SMP_DIST_ID_KEY;
428
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300429 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
430 if ((authreq & SMP_AUTH_SC) &&
431 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
432 local_dist |= SMP_DIST_LINK_KEY;
433 remote_dist |= SMP_DIST_LINK_KEY;
434 }
435 } else {
436 authreq &= ~SMP_AUTH_SC;
437 }
438
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300439 if (rsp == NULL) {
440 req->io_capability = conn->hcon->io_capability;
441 req->oob_flag = SMP_OOB_NOT_PRESENT;
442 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200443 req->init_key_dist = local_dist;
444 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300445 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200446
447 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300448 return;
449 }
450
451 rsp->io_capability = conn->hcon->io_capability;
452 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
453 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200454 rsp->init_key_dist = req->init_key_dist & remote_dist;
455 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300456 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200457
458 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300459}
460
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300461static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
462{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300463 struct l2cap_chan *chan = conn->smp;
464 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300465
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300466 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700467 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300468 return SMP_ENC_KEY_SIZE;
469
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300470 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300471
472 return 0;
473}
474
Johan Hedberg6f48e262014-08-11 22:06:44 +0300475static void smp_chan_destroy(struct l2cap_conn *conn)
476{
477 struct l2cap_chan *chan = conn->smp;
478 struct smp_chan *smp = chan->data;
479 bool complete;
480
481 BUG_ON(!smp);
482
483 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300484
Johan Hedberg6f48e262014-08-11 22:06:44 +0300485 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
486 mgmt_smp_complete(conn->hcon, complete);
487
488 kfree(smp->csrk);
489 kfree(smp->slave_csrk);
490
491 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300492 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300493
494 /* If pairing failed clean up any keys we might have */
495 if (!complete) {
496 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200497 list_del_rcu(&smp->ltk->list);
498 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300499 }
500
501 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200502 list_del_rcu(&smp->slave_ltk->list);
503 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300504 }
505
506 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200507 list_del_rcu(&smp->remote_irk->list);
508 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300509 }
510 }
511
512 chan->data = NULL;
513 kfree(smp);
514 hci_conn_drop(conn->hcon);
515}
516
Johan Hedberg84794e12013-11-06 11:24:57 +0200517static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800518{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200519 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300520 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200521
Johan Hedberg84794e12013-11-06 11:24:57 +0200522 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800523 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700524 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800525
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700526 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700527 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300528
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300529 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300530 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800531}
532
Brian Gix2b64d152011-12-21 16:12:12 -0800533#define JUST_WORKS 0x00
534#define JUST_CFM 0x01
535#define REQ_PASSKEY 0x02
536#define CFM_PASSKEY 0x03
537#define REQ_OOB 0x04
538#define OVERLAP 0xFF
539
540static const u8 gen_method[5][5] = {
541 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
542 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
543 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
544 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
545 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
546};
547
Johan Hedberg581370c2014-06-17 13:07:38 +0300548static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
549{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300550 /* If either side has unknown io_caps, use JUST_CFM (which gets
551 * converted later to JUST_WORKS if we're initiators.
552 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300553 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
554 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300555 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300556
557 return gen_method[remote_io][local_io];
558}
559
Brian Gix2b64d152011-12-21 16:12:12 -0800560static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
561 u8 local_io, u8 remote_io)
562{
563 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300564 struct l2cap_chan *chan = conn->smp;
565 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800566 u8 method;
567 u32 passkey = 0;
568 int ret = 0;
569
570 /* Initialize key for JUST WORKS */
571 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300572 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800573
574 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
575
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300576 /* If neither side wants MITM, either "just" confirm an incoming
577 * request or use just-works for outgoing ones. The JUST_CFM
578 * will be converted to JUST_WORKS if necessary later in this
579 * function. If either side has MITM look up the method from the
580 * table.
581 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300582 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300583 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800584 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300585 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800586
Johan Hedberga82505c2014-03-24 14:39:07 +0200587 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300588 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200589 method = JUST_WORKS;
590
Johan Hedberg02f3e252014-07-16 15:09:13 +0300591 /* Don't bother user space with no IO capabilities */
592 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
593 method = JUST_WORKS;
594
Brian Gix2b64d152011-12-21 16:12:12 -0800595 /* If Just Works, Continue with Zero TK */
596 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300597 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800598 return 0;
599 }
600
601 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300602 if (method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300603 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300604 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
605 hcon->pending_sec_level = BT_SECURITY_HIGH;
606 }
Brian Gix2b64d152011-12-21 16:12:12 -0800607
608 /* If both devices have Keyoard-Display I/O, the master
609 * Confirms and the slave Enters the passkey.
610 */
611 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300612 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800613 method = CFM_PASSKEY;
614 else
615 method = REQ_PASSKEY;
616 }
617
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200618 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800619 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200620 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800621 get_random_bytes(&passkey, sizeof(passkey));
622 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200623 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800624 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300625 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800626 }
627
Brian Gix2b64d152011-12-21 16:12:12 -0800628 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700629 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200630 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200631 else if (method == JUST_CFM)
632 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
633 hcon->type, hcon->dst_type,
634 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800635 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200636 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200637 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200638 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800639
Brian Gix2b64d152011-12-21 16:12:12 -0800640 return ret;
641}
642
Johan Hedberg1cc61142014-05-20 09:45:52 +0300643static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300644{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300645 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300646 struct smp_cmd_pairing_confirm cp;
647 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300648
649 BT_DBG("conn %p", conn);
650
Johan Hedberge491eaf2014-10-25 21:15:37 +0200651 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200652 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200653 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
654 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300655 if (ret)
656 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300657
Johan Hedberg4a74d652014-05-20 09:45:50 +0300658 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800659
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300660 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
661
Johan Hedbergb28b4942014-09-05 22:19:55 +0300662 if (conn->hcon->out)
663 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
664 else
665 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
666
Johan Hedberg1cc61142014-05-20 09:45:52 +0300667 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300668}
669
Johan Hedberg861580a2014-05-20 09:45:51 +0300670static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300671{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300672 struct l2cap_conn *conn = smp->conn;
673 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300674 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300675 int ret;
676
Johan Hedbergec70f362014-06-27 14:23:04 +0300677 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300678 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300679
680 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
681
Johan Hedberge491eaf2014-10-25 21:15:37 +0200682 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200683 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200684 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300685 if (ret)
686 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300687
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300688 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
689 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300690 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300691 }
692
693 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800694 u8 stk[16];
695 __le64 rand = 0;
696 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300697
Johan Hedberge491eaf2014-10-25 21:15:37 +0200698 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300699
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300700 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300701 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300702
Johan Hedberg861580a2014-05-20 09:45:51 +0300703 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
704 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300705
706 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300707 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300708 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300709 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300710 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800711 __le64 rand = 0;
712 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300713
Johan Hedberg943a7322014-03-18 12:58:24 +0200714 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
715 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300716
Johan Hedberge491eaf2014-10-25 21:15:37 +0200717 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300718
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300719 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700720 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300721
Johan Hedbergfff34902014-06-10 15:19:50 +0300722 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
723 auth = 1;
724 else
725 auth = 0;
726
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300727 /* Even though there's no _SLAVE suffix this is the
728 * slave STK we're adding for later lookup (the master
729 * STK never needs to be stored).
730 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700731 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300732 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300733 }
734
Johan Hedberg861580a2014-05-20 09:45:51 +0300735 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300736}
737
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300738static void smp_notify_keys(struct l2cap_conn *conn)
739{
740 struct l2cap_chan *chan = conn->smp;
741 struct smp_chan *smp = chan->data;
742 struct hci_conn *hcon = conn->hcon;
743 struct hci_dev *hdev = hcon->hdev;
744 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
745 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
746 bool persistent;
747
748 if (smp->remote_irk) {
749 mgmt_new_irk(hdev, smp->remote_irk);
750 /* Now that user space can be considered to know the
751 * identity address track the connection based on it
752 * from now on.
753 */
754 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
755 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300756 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300757
758 /* When receiving an indentity resolving key for
759 * a remote device that does not use a resolvable
760 * private address, just remove the key so that
761 * it is possible to use the controller white
762 * list for scanning.
763 *
764 * Userspace will have been told to not store
765 * this key at this point. So it is safe to
766 * just remove it.
767 */
768 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200769 list_del_rcu(&smp->remote_irk->list);
770 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300771 smp->remote_irk = NULL;
772 }
773 }
774
775 /* The LTKs and CSRKs should be persistent only if both sides
776 * had the bonding bit set in their authentication requests.
777 */
778 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
779
780 if (smp->csrk) {
781 smp->csrk->bdaddr_type = hcon->dst_type;
782 bacpy(&smp->csrk->bdaddr, &hcon->dst);
783 mgmt_new_csrk(hdev, smp->csrk, persistent);
784 }
785
786 if (smp->slave_csrk) {
787 smp->slave_csrk->bdaddr_type = hcon->dst_type;
788 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
789 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
790 }
791
792 if (smp->ltk) {
793 smp->ltk->bdaddr_type = hcon->dst_type;
794 bacpy(&smp->ltk->bdaddr, &hcon->dst);
795 mgmt_new_ltk(hdev, smp->ltk, persistent);
796 }
797
798 if (smp->slave_ltk) {
799 smp->slave_ltk->bdaddr_type = hcon->dst_type;
800 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
801 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
802 }
803}
804
Johan Hedbergb28b4942014-09-05 22:19:55 +0300805static void smp_allow_key_dist(struct smp_chan *smp)
806{
807 /* Allow the first expected phase 3 PDU. The rest of the PDUs
808 * will be allowed in each PDU handler to ensure we receive
809 * them in the correct order.
810 */
811 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
812 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
813 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
814 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
815 else if (smp->remote_key_dist & SMP_DIST_SIGN)
816 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
817}
818
Johan Hedbergd6268e82014-09-05 22:19:51 +0300819static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300820{
821 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300822 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300823 struct hci_conn *hcon = conn->hcon;
824 struct hci_dev *hdev = hcon->hdev;
825 __u8 *keydist;
826
827 BT_DBG("conn %p", conn);
828
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300829 rsp = (void *) &smp->prsp[1];
830
831 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300832 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
833 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300834 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300835 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300836
837 req = (void *) &smp->preq[1];
838
839 if (hcon->out) {
840 keydist = &rsp->init_key_dist;
841 *keydist &= req->init_key_dist;
842 } else {
843 keydist = &rsp->resp_key_dist;
844 *keydist &= req->resp_key_dist;
845 }
846
847 BT_DBG("keydist 0x%x", *keydist);
848
849 if (*keydist & SMP_DIST_ENC_KEY) {
850 struct smp_cmd_encrypt_info enc;
851 struct smp_cmd_master_ident ident;
852 struct smp_ltk *ltk;
853 u8 authenticated;
854 __le16 ediv;
855 __le64 rand;
856
857 get_random_bytes(enc.ltk, sizeof(enc.ltk));
858 get_random_bytes(&ediv, sizeof(ediv));
859 get_random_bytes(&rand, sizeof(rand));
860
861 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
862
863 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
864 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
865 SMP_LTK_SLAVE, authenticated, enc.ltk,
866 smp->enc_key_size, ediv, rand);
867 smp->slave_ltk = ltk;
868
869 ident.ediv = ediv;
870 ident.rand = rand;
871
872 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
873
874 *keydist &= ~SMP_DIST_ENC_KEY;
875 }
876
877 if (*keydist & SMP_DIST_ID_KEY) {
878 struct smp_cmd_ident_addr_info addrinfo;
879 struct smp_cmd_ident_info idinfo;
880
881 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
882
883 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
884
885 /* The hci_conn contains the local identity address
886 * after the connection has been established.
887 *
888 * This is true even when the connection has been
889 * established using a resolvable random address.
890 */
891 bacpy(&addrinfo.bdaddr, &hcon->src);
892 addrinfo.addr_type = hcon->src_type;
893
894 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
895 &addrinfo);
896
897 *keydist &= ~SMP_DIST_ID_KEY;
898 }
899
900 if (*keydist & SMP_DIST_SIGN) {
901 struct smp_cmd_sign_info sign;
902 struct smp_csrk *csrk;
903
904 /* Generate a new random key */
905 get_random_bytes(sign.csrk, sizeof(sign.csrk));
906
907 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
908 if (csrk) {
909 csrk->master = 0x00;
910 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
911 }
912 smp->slave_csrk = csrk;
913
914 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
915
916 *keydist &= ~SMP_DIST_SIGN;
917 }
918
919 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300920 if (smp->remote_key_dist & KEY_DIST_MASK) {
921 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300922 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300923 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300924
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300925 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
926 smp_notify_keys(conn);
927
928 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300929}
930
Johan Hedbergb68fda62014-08-11 22:06:40 +0300931static void smp_timeout(struct work_struct *work)
932{
933 struct smp_chan *smp = container_of(work, struct smp_chan,
934 security_timer.work);
935 struct l2cap_conn *conn = smp->conn;
936
937 BT_DBG("conn %p", conn);
938
Johan Hedberg1e91c292014-08-18 20:33:29 +0300939 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300940}
941
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300942static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
943{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300944 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300945 struct smp_chan *smp;
946
Marcel Holtmannf1560462013-10-13 05:43:25 -0700947 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300948 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300949 return NULL;
950
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300951 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
952 if (IS_ERR(smp->tfm_aes)) {
953 BT_ERR("Unable to create ECB crypto context");
954 kfree(smp);
955 return NULL;
956 }
957
Johan Hedberg407cecf2014-05-02 14:19:47 +0300958 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
959 if (IS_ERR(smp->tfm_cmac)) {
960 BT_ERR("Unable to create CMAC crypto context");
961 crypto_free_blkcipher(smp->tfm_aes);
962 kfree(smp);
963 return NULL;
964 }
965
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300966 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300967 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300968
Johan Hedbergb28b4942014-09-05 22:19:55 +0300969 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
970
Johan Hedbergb68fda62014-08-11 22:06:40 +0300971 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
972
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300973 hci_conn_hold(conn->hcon);
974
975 return smp;
976}
977
Brian Gix2b64d152011-12-21 16:12:12 -0800978int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
979{
Johan Hedbergb10e8012014-06-27 14:23:07 +0300980 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300981 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -0800982 struct smp_chan *smp;
983 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300984 int err;
Brian Gix2b64d152011-12-21 16:12:12 -0800985
986 BT_DBG("");
987
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300988 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -0800989 return -ENOTCONN;
990
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300991 chan = conn->smp;
992 if (!chan)
993 return -ENOTCONN;
994
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300995 l2cap_chan_lock(chan);
996 if (!chan->data) {
997 err = -ENOTCONN;
998 goto unlock;
999 }
1000
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001001 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001002
1003 switch (mgmt_op) {
1004 case MGMT_OP_USER_PASSKEY_REPLY:
1005 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001006 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001007 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001008 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001009 /* Fall Through */
1010 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001011 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001012 break;
1013 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1014 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001015 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001016 err = 0;
1017 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001018 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001019 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001020 err = -EOPNOTSUPP;
1021 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001022 }
1023
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001024 err = 0;
1025
Brian Gix2b64d152011-12-21 16:12:12 -08001026 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001027 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1028 u8 rsp = smp_confirm(smp);
1029 if (rsp)
1030 smp_failure(conn, rsp);
1031 }
Brian Gix2b64d152011-12-21 16:12:12 -08001032
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001033unlock:
1034 l2cap_chan_unlock(chan);
1035 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001036}
1037
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001038static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001039{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001040 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001041 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001042 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001043 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001044 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001045 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001046
1047 BT_DBG("conn %p", conn);
1048
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001049 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001050 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001051
Johan Hedberg40bef302014-07-16 11:42:27 +03001052 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001053 return SMP_CMD_NOTSUPP;
1054
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001055 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001056 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001057 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001058 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001059
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001060 if (!smp)
1061 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001062
Johan Hedbergc05b9332014-09-10 17:37:42 -07001063 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001064 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001065
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001066 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001067 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001068 return SMP_PAIRING_NOTSUPP;
1069
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001070 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1071 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001072 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001073
Johan Hedberg5be5e272014-09-10 17:58:54 -07001074 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001075 sec_level = BT_SECURITY_MEDIUM;
1076 else
1077 sec_level = authreq_to_seclevel(auth);
1078
Johan Hedbergc7262e72014-06-17 13:07:37 +03001079 if (sec_level > conn->hcon->pending_sec_level)
1080 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001081
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001082 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001083 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1084 u8 method;
1085
1086 method = get_auth_method(smp, conn->hcon->io_capability,
1087 req->io_capability);
1088 if (method == JUST_WORKS || method == JUST_CFM)
1089 return SMP_AUTH_REQUIREMENTS;
1090 }
1091
Brian Gix2b64d152011-12-21 16:12:12 -08001092 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001093
Johan Hedberg65668772014-05-16 11:03:34 +03001094 if (rsp.auth_req & SMP_AUTH_SC)
1095 set_bit(SMP_FLAG_SC, &smp->flags);
1096
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001097 key_size = min(req->max_key_size, rsp.max_key_size);
1098 if (check_enc_key_size(conn, key_size))
1099 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001100
Johan Hedberge84a6b12013-12-02 10:49:03 +02001101 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001102
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001103 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1104 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001105
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001106 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001107
1108 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1109
1110 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1111 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1112 /* Clear bits which are generated but not distributed */
1113 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1114 /* Wait for Public Key from Initiating Device */
1115 return 0;
1116 } else {
1117 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1118 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001119
Brian Gix2b64d152011-12-21 16:12:12 -08001120 /* Request setup of TK */
1121 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1122 if (ret)
1123 return SMP_UNSPECIFIED;
1124
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001125 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001126}
1127
Johan Hedberg3b191462014-06-06 10:50:15 +03001128static u8 sc_send_public_key(struct smp_chan *smp)
1129{
1130 BT_DBG("");
1131
1132 /* Generate local key pair for Secure Connections */
1133 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1134 return SMP_UNSPECIFIED;
1135
1136 BT_DBG("Local Public Key X: %32phN", smp->local_pk);
1137 BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1138 BT_DBG("Local Private Key: %32phN", smp->local_sk);
1139
1140 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1141
1142 return 0;
1143}
1144
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001145static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001146{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001147 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001148 struct l2cap_chan *chan = conn->smp;
1149 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001150 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001151 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001152 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001153
1154 BT_DBG("conn %p", conn);
1155
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001156 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001157 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001158
Johan Hedberg40bef302014-07-16 11:42:27 +03001159 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001160 return SMP_CMD_NOTSUPP;
1161
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001162 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001163
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001164 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001165
1166 key_size = min(req->max_key_size, rsp->max_key_size);
1167 if (check_enc_key_size(conn, key_size))
1168 return SMP_ENC_KEY_SIZE;
1169
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001170 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001171
Johan Hedberg65668772014-05-16 11:03:34 +03001172 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1173 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001174 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1175 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001176
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001177 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001178 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1179 u8 method;
1180
1181 method = get_auth_method(smp, req->io_capability,
1182 rsp->io_capability);
1183 if (method == JUST_WORKS || method == JUST_CFM)
1184 return SMP_AUTH_REQUIREMENTS;
1185 }
1186
Johan Hedberge84a6b12013-12-02 10:49:03 +02001187 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001188
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001189 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1190 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001191
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001192 /* Update remote key distribution in case the remote cleared
1193 * some bits that we had enabled in our request.
1194 */
1195 smp->remote_key_dist &= rsp->resp_key_dist;
1196
Johan Hedberg3b191462014-06-06 10:50:15 +03001197 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1198 /* Clear bits which are generated but not distributed */
1199 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1200 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1201 return sc_send_public_key(smp);
1202 }
1203
Johan Hedbergc05b9332014-09-10 17:37:42 -07001204 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001205
Johan Hedberg476585e2012-06-06 18:54:15 +08001206 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001207 if (ret)
1208 return SMP_UNSPECIFIED;
1209
Johan Hedberg4a74d652014-05-20 09:45:50 +03001210 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001211
1212 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001213 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001214 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001215
1216 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001217}
1218
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001219static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001220{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001221 struct l2cap_chan *chan = conn->smp;
1222 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001223
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001224 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1225
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001226 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001227 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001228
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001229 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1230 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001231
Johan Hedbergb28b4942014-09-05 22:19:55 +03001232 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001233 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1234 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001235 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1236 return 0;
1237 }
1238
1239 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001240 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001241 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001242 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001243
1244 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001245}
1246
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001247static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001248{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001249 struct l2cap_chan *chan = conn->smp;
1250 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001251
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001252 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001253
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001254 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001255 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001256
Johan Hedberg943a7322014-03-18 12:58:24 +02001257 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001258 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001259
Johan Hedberg861580a2014-05-20 09:45:51 +03001260 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001261}
1262
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001263static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001264{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001265 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001266 struct hci_conn *hcon = conn->hcon;
1267
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001268 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001269 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001270 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001271
Johan Hedberga6f78332014-09-10 17:37:45 -07001272 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001273 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001274
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001275 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001276 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001277
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001278 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1279 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001280
Johan Hedbergfe59a052014-07-01 19:14:12 +03001281 /* We never store STKs for master role, so clear this flag */
1282 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1283
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001284 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001285}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001286
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001287bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1288 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03001289{
1290 if (sec_level == BT_SECURITY_LOW)
1291 return true;
1292
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001293 /* If we're encrypted with an STK but the caller prefers using
1294 * LTK claim insufficient security. This way we allow the
1295 * connection to be re-encrypted with an LTK, even if the LTK
1296 * provides the same level of security. Only exception is if we
1297 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001298 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001299 if (key_pref == SMP_USE_LTK &&
1300 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001301 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001302 return false;
1303
Johan Hedberg854f4722014-07-01 18:40:20 +03001304 if (hcon->sec_level >= sec_level)
1305 return true;
1306
1307 return false;
1308}
1309
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001310static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001311{
1312 struct smp_cmd_security_req *rp = (void *) skb->data;
1313 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001314 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001315 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001316 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07001317 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001318
1319 BT_DBG("conn %p", conn);
1320
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001321 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001322 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001323
Johan Hedberg40bef302014-07-16 11:42:27 +03001324 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001325 return SMP_CMD_NOTSUPP;
1326
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001327 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001328
Johan Hedberg5be5e272014-09-10 17:58:54 -07001329 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001330 sec_level = BT_SECURITY_MEDIUM;
1331 else
1332 sec_level = authreq_to_seclevel(auth);
1333
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001334 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03001335 return 0;
1336
Johan Hedbergc7262e72014-06-17 13:07:37 +03001337 if (sec_level > hcon->pending_sec_level)
1338 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001339
Johan Hedberg4dab7862012-06-07 14:58:37 +08001340 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001341 return 0;
1342
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001343 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001344 if (!smp)
1345 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001346
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001347 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001348 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03001349 return SMP_PAIRING_NOTSUPP;
1350
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001351 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001352
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001353 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07001354 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001355
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001356 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1357 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001358
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001359 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001360 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001361
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001362 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001363}
1364
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001365int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001366{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001367 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001368 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001369 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001370 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001371 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001372
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001373 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1374
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001375 /* This may be NULL if there's an unexpected disconnection */
1376 if (!conn)
1377 return 1;
1378
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001379 chan = conn->smp;
1380
Johan Hedberg757aee02013-04-24 13:05:32 +03001381 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001382 return 1;
1383
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001384 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001385 return 1;
1386
Johan Hedbergc7262e72014-06-17 13:07:37 +03001387 if (sec_level > hcon->pending_sec_level)
1388 hcon->pending_sec_level = sec_level;
1389
Johan Hedberg40bef302014-07-16 11:42:27 +03001390 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001391 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1392 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001393
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001394 l2cap_chan_lock(chan);
1395
1396 /* If SMP is already in progress ignore this request */
1397 if (chan->data) {
1398 ret = 0;
1399 goto unlock;
1400 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001401
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001402 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001403 if (!smp) {
1404 ret = 1;
1405 goto unlock;
1406 }
Brian Gix2b64d152011-12-21 16:12:12 -08001407
1408 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001409
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001410 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
1411 authreq |= SMP_AUTH_SC;
1412
Johan Hedberg79897d22014-06-01 09:45:24 +03001413 /* Require MITM if IO Capability allows or the security level
1414 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001415 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001416 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001417 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001418 authreq |= SMP_AUTH_MITM;
1419
Johan Hedberg40bef302014-07-16 11:42:27 +03001420 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001421 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001422
Brian Gix2b64d152011-12-21 16:12:12 -08001423 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001424 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1425 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001426
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001427 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001428 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001429 } else {
1430 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001431 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001432 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001433 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001434 }
1435
Johan Hedberg4a74d652014-05-20 09:45:50 +03001436 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001437 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001438
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001439unlock:
1440 l2cap_chan_unlock(chan);
1441 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001442}
1443
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001444static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1445{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001446 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001447 struct l2cap_chan *chan = conn->smp;
1448 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001449
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001450 BT_DBG("conn %p", conn);
1451
1452 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001453 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001454
Johan Hedbergb28b4942014-09-05 22:19:55 +03001455 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001456
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001457 skb_pull(skb, sizeof(*rp));
1458
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001459 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001460
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001461 return 0;
1462}
1463
1464static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1465{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001466 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001467 struct l2cap_chan *chan = conn->smp;
1468 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001469 struct hci_dev *hdev = conn->hcon->hdev;
1470 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001471 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001472 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001473
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001474 BT_DBG("conn %p", conn);
1475
1476 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001477 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001478
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001479 /* Mark the information as received */
1480 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1481
Johan Hedbergb28b4942014-09-05 22:19:55 +03001482 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1483 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07001484 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1485 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001486
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001487 skb_pull(skb, sizeof(*rp));
1488
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001489 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001490 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001491 authenticated, smp->tk, smp->enc_key_size,
1492 rp->ediv, rp->rand);
1493 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001494 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001495 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001496
1497 return 0;
1498}
1499
Johan Hedbergfd349c02014-02-18 10:19:36 +02001500static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1501{
1502 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001503 struct l2cap_chan *chan = conn->smp;
1504 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001505
1506 BT_DBG("");
1507
1508 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001509 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001510
Johan Hedbergb28b4942014-09-05 22:19:55 +03001511 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001512
Johan Hedbergfd349c02014-02-18 10:19:36 +02001513 skb_pull(skb, sizeof(*info));
1514
1515 memcpy(smp->irk, info->irk, 16);
1516
1517 return 0;
1518}
1519
1520static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1521 struct sk_buff *skb)
1522{
1523 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001524 struct l2cap_chan *chan = conn->smp;
1525 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001526 struct hci_conn *hcon = conn->hcon;
1527 bdaddr_t rpa;
1528
1529 BT_DBG("");
1530
1531 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001532 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001533
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001534 /* Mark the information as received */
1535 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1536
Johan Hedbergb28b4942014-09-05 22:19:55 +03001537 if (smp->remote_key_dist & SMP_DIST_SIGN)
1538 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1539
Johan Hedbergfd349c02014-02-18 10:19:36 +02001540 skb_pull(skb, sizeof(*info));
1541
Johan Hedberga9a58f82014-02-25 22:24:37 +02001542 /* Strictly speaking the Core Specification (4.1) allows sending
1543 * an empty address which would force us to rely on just the IRK
1544 * as "identity information". However, since such
1545 * implementations are not known of and in order to not over
1546 * complicate our implementation, simply pretend that we never
1547 * received an IRK for such a device.
1548 */
1549 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1550 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001551 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001552 }
1553
Johan Hedbergfd349c02014-02-18 10:19:36 +02001554 bacpy(&smp->id_addr, &info->bdaddr);
1555 smp->id_addr_type = info->addr_type;
1556
1557 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1558 bacpy(&rpa, &hcon->dst);
1559 else
1560 bacpy(&rpa, BDADDR_ANY);
1561
Johan Hedberg23d0e122014-02-19 14:57:46 +02001562 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1563 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001564
Johan Hedberg31dd6242014-06-27 14:23:02 +03001565distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001566 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1567 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001568
1569 return 0;
1570}
1571
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001572static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1573{
1574 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001575 struct l2cap_chan *chan = conn->smp;
1576 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001577 struct smp_csrk *csrk;
1578
1579 BT_DBG("conn %p", conn);
1580
1581 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001582 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001583
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001584 /* Mark the information as received */
1585 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1586
1587 skb_pull(skb, sizeof(*rp));
1588
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001589 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1590 if (csrk) {
1591 csrk->master = 0x01;
1592 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1593 }
1594 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001595 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001596
1597 return 0;
1598}
1599
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001600static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
1601{
1602 struct smp_cmd_public_key *key = (void *) skb->data;
1603 struct hci_conn *hcon = conn->hcon;
1604 struct l2cap_chan *chan = conn->smp;
1605 struct smp_chan *smp = chan->data;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03001606 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001607 int err;
1608
1609 BT_DBG("conn %p", conn);
1610
1611 if (skb->len < sizeof(*key))
1612 return SMP_INVALID_PARAMS;
1613
1614 memcpy(smp->remote_pk, key, 64);
1615
1616 /* Non-initiating device sends its public key after receiving
1617 * the key from the initiating device.
1618 */
1619 if (!hcon->out) {
1620 err = sc_send_public_key(smp);
1621 if (err)
1622 return err;
1623 }
1624
1625 BT_DBG("Remote Public Key X: %32phN", smp->remote_pk);
1626 BT_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
1627
1628 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
1629 return SMP_UNSPECIFIED;
1630
1631 BT_DBG("DHKey %32phN", smp->dhkey);
1632
1633 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
1634
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03001635 /* The Initiating device waits for the non-initiating device to
1636 * send the confirm value.
1637 */
1638 if (conn->hcon->out)
1639 return 0;
1640
1641 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
1642 0, cfm.confirm_val);
1643 if (err)
1644 return SMP_UNSPECIFIED;
1645
1646 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1647 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1648
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001649 return 0;
1650}
1651
Johan Hedberg4befb862014-08-11 22:06:38 +03001652static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001653{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001654 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001655 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001656 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001657 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001658 int err = 0;
1659
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001660 if (hcon->type != LE_LINK) {
1661 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001662 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001663 }
1664
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001665 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001666 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001667
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001668 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001669 reason = SMP_PAIRING_NOTSUPP;
1670 goto done;
1671 }
1672
Marcel Holtmann92381f52013-10-03 01:23:08 -07001673 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001674 skb_pull(skb, sizeof(code));
1675
Johan Hedbergb28b4942014-09-05 22:19:55 +03001676 smp = chan->data;
1677
1678 if (code > SMP_CMD_MAX)
1679 goto drop;
1680
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07001681 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03001682 goto drop;
1683
1684 /* If we don't have a context the only allowed commands are
1685 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001686 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001687 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1688 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001689
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001690 switch (code) {
1691 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001692 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001693 break;
1694
1695 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001696 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001697 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001698 break;
1699
1700 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001701 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001702 break;
1703
1704 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001705 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001706 break;
1707
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001708 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001709 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001710 break;
1711
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001712 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001713 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001714 break;
1715
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001716 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001717 reason = smp_cmd_encrypt_info(conn, skb);
1718 break;
1719
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001720 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001721 reason = smp_cmd_master_ident(conn, skb);
1722 break;
1723
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001724 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001725 reason = smp_cmd_ident_info(conn, skb);
1726 break;
1727
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001728 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001729 reason = smp_cmd_ident_addr_info(conn, skb);
1730 break;
1731
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001732 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001733 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001734 break;
1735
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001736 case SMP_CMD_PUBLIC_KEY:
1737 reason = smp_cmd_public_key(conn, skb);
1738 break;
1739
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001740 default:
1741 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001742 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001743 goto done;
1744 }
1745
1746done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001747 if (!err) {
1748 if (reason)
1749 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001750 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001751 }
1752
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001753 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001754
1755drop:
1756 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
1757 code, &hcon->dst);
1758 kfree_skb(skb);
1759 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001760}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001761
Johan Hedberg70db83c2014-08-08 09:37:16 +03001762static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1763{
1764 struct l2cap_conn *conn = chan->conn;
1765
1766 BT_DBG("chan %p", chan);
1767
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001768 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001769 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001770
Johan Hedberg70db83c2014-08-08 09:37:16 +03001771 conn->smp = NULL;
1772 l2cap_chan_put(chan);
1773}
1774
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001775static void smp_resume_cb(struct l2cap_chan *chan)
1776{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001777 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001778 struct l2cap_conn *conn = chan->conn;
1779 struct hci_conn *hcon = conn->hcon;
1780
1781 BT_DBG("chan %p", chan);
1782
Johan Hedberg86d14072014-08-11 22:06:43 +03001783 if (!smp)
1784 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001785
Johan Hedberg84bc0db2014-09-05 22:19:49 +03001786 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1787 return;
1788
Johan Hedberg86d14072014-08-11 22:06:43 +03001789 cancel_delayed_work(&smp->security_timer);
1790
Johan Hedbergd6268e82014-09-05 22:19:51 +03001791 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001792}
1793
Johan Hedberg70db83c2014-08-08 09:37:16 +03001794static void smp_ready_cb(struct l2cap_chan *chan)
1795{
1796 struct l2cap_conn *conn = chan->conn;
1797
1798 BT_DBG("chan %p", chan);
1799
1800 conn->smp = chan;
1801 l2cap_chan_hold(chan);
1802}
1803
Johan Hedberg4befb862014-08-11 22:06:38 +03001804static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1805{
1806 int err;
1807
1808 BT_DBG("chan %p", chan);
1809
1810 err = smp_sig_channel(chan, skb);
1811 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001812 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001813
Johan Hedbergb68fda62014-08-11 22:06:40 +03001814 if (smp)
1815 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001816
Johan Hedberg1e91c292014-08-18 20:33:29 +03001817 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03001818 }
1819
1820 return err;
1821}
1822
Johan Hedberg70db83c2014-08-08 09:37:16 +03001823static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1824 unsigned long hdr_len,
1825 unsigned long len, int nb)
1826{
1827 struct sk_buff *skb;
1828
1829 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1830 if (!skb)
1831 return ERR_PTR(-ENOMEM);
1832
1833 skb->priority = HCI_PRIO_MAX;
1834 bt_cb(skb)->chan = chan;
1835
1836 return skb;
1837}
1838
1839static const struct l2cap_ops smp_chan_ops = {
1840 .name = "Security Manager",
1841 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001842 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001843 .alloc_skb = smp_alloc_skb_cb,
1844 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001845 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001846
1847 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001848 .state_change = l2cap_chan_no_state_change,
1849 .close = l2cap_chan_no_close,
1850 .defer = l2cap_chan_no_defer,
1851 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001852 .set_shutdown = l2cap_chan_no_set_shutdown,
1853 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1854 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1855};
1856
1857static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1858{
1859 struct l2cap_chan *chan;
1860
1861 BT_DBG("pchan %p", pchan);
1862
1863 chan = l2cap_chan_create();
1864 if (!chan)
1865 return NULL;
1866
1867 chan->chan_type = pchan->chan_type;
1868 chan->ops = &smp_chan_ops;
1869 chan->scid = pchan->scid;
1870 chan->dcid = chan->scid;
1871 chan->imtu = pchan->imtu;
1872 chan->omtu = pchan->omtu;
1873 chan->mode = pchan->mode;
1874
Johan Hedbergabe84902014-11-12 22:22:21 +02001875 /* Other L2CAP channels may request SMP routines in order to
1876 * change the security level. This means that the SMP channel
1877 * lock must be considered in its own category to avoid lockdep
1878 * warnings.
1879 */
1880 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
1881
Johan Hedberg70db83c2014-08-08 09:37:16 +03001882 BT_DBG("created chan %p", chan);
1883
1884 return chan;
1885}
1886
1887static const struct l2cap_ops smp_root_chan_ops = {
1888 .name = "Security Manager Root",
1889 .new_connection = smp_new_conn_cb,
1890
1891 /* None of these are implemented for the root channel */
1892 .close = l2cap_chan_no_close,
1893 .alloc_skb = l2cap_chan_no_alloc_skb,
1894 .recv = l2cap_chan_no_recv,
1895 .state_change = l2cap_chan_no_state_change,
1896 .teardown = l2cap_chan_no_teardown,
1897 .ready = l2cap_chan_no_ready,
1898 .defer = l2cap_chan_no_defer,
1899 .suspend = l2cap_chan_no_suspend,
1900 .resume = l2cap_chan_no_resume,
1901 .set_shutdown = l2cap_chan_no_set_shutdown,
1902 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1903 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1904};
1905
Johan Hedberg711eafe2014-08-08 09:32:52 +03001906int smp_register(struct hci_dev *hdev)
1907{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001908 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001909 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001910
Johan Hedberg711eafe2014-08-08 09:32:52 +03001911 BT_DBG("%s", hdev->name);
1912
Johan Hedbergadae20c2014-11-13 14:37:48 +02001913 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001914 if (IS_ERR(tfm_aes)) {
1915 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001916 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03001917 return err;
1918 }
1919
Johan Hedberg70db83c2014-08-08 09:37:16 +03001920 chan = l2cap_chan_create();
1921 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001922 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001923 return -ENOMEM;
1924 }
1925
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001926 chan->data = tfm_aes;
1927
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001928 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001929
1930 l2cap_chan_set_defaults(chan);
1931
1932 bacpy(&chan->src, &hdev->bdaddr);
1933 chan->src_type = BDADDR_LE_PUBLIC;
1934 chan->state = BT_LISTEN;
1935 chan->mode = L2CAP_MODE_BASIC;
1936 chan->imtu = L2CAP_DEFAULT_MTU;
1937 chan->ops = &smp_root_chan_ops;
1938
Johan Hedbergabe84902014-11-12 22:22:21 +02001939 /* Set correct nesting level for a parent/listening channel */
1940 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
1941
Johan Hedberg70db83c2014-08-08 09:37:16 +03001942 hdev->smp_data = chan;
1943
Johan Hedberg711eafe2014-08-08 09:32:52 +03001944 return 0;
1945}
1946
1947void smp_unregister(struct hci_dev *hdev)
1948{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001949 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001950 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001951
1952 if (!chan)
1953 return;
1954
1955 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001956
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001957 tfm_aes = chan->data;
1958 if (tfm_aes) {
1959 chan->data = NULL;
1960 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001961 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03001962
1963 hdev->smp_data = NULL;
1964 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001965}