blob: 103f05aff7e9c2f3a841fd6ef0c4559018188c17 [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
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300178static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
179 const u8 x[16], const u8 y[16], u32 *val)
180{
181 u8 m[80], tmp[16];
182 int err;
183
184 BT_DBG("u %32phN", u);
185 BT_DBG("v %32phN", v);
186 BT_DBG("x %16phN y %16phN", x, y);
187
188 memcpy(m, y, 16);
189 memcpy(m + 16, v, 32);
190 memcpy(m + 48, u, 32);
191
192 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
193 if (err)
194 return err;
195
196 *val = get_unaligned_le32(tmp);
197 *val %= 1000000;
198
199 BT_DBG("val %06u", *val);
200
201 return 0;
202}
203
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300204static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
205{
206 struct blkcipher_desc desc;
207 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200208 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200209 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300210
211 if (tfm == NULL) {
212 BT_ERR("tfm %p", tfm);
213 return -EINVAL;
214 }
215
216 desc.tfm = tfm;
217 desc.flags = 0;
218
Johan Hedberg943a7322014-03-18 12:58:24 +0200219 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300220 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200221
222 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300223 if (err) {
224 BT_ERR("cipher setkey failed: %d", err);
225 return err;
226 }
227
Johan Hedberg943a7322014-03-18 12:58:24 +0200228 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300229 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200230
231 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300232
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300233 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
234 if (err)
235 BT_ERR("Encrypt data error %d", err);
236
Johan Hedberg943a7322014-03-18 12:58:24 +0200237 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300238 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200239
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300240 return err;
241}
242
Johan Hedberg60478052014-02-18 10:19:31 +0200243static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
244{
Johan Hedberg943a7322014-03-18 12:58:24 +0200245 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200246 int err;
247
248 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200249 memcpy(_res, r, 3);
250 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200251
Johan Hedberg943a7322014-03-18 12:58:24 +0200252 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200253 if (err) {
254 BT_ERR("Encrypt error");
255 return err;
256 }
257
258 /* The output of the random address function ah is:
259 * ah(h, r) = e(k, r') mod 2^24
260 * The output of the security function e is then truncated to 24 bits
261 * by taking the least significant 24 bits of the output of e as the
262 * result of ah.
263 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200264 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200265
266 return 0;
267}
268
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300269bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200270{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300271 struct l2cap_chan *chan = hdev->smp_data;
272 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200273 u8 hash[3];
274 int err;
275
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300276 if (!chan || !chan->data)
277 return false;
278
279 tfm = chan->data;
280
Johan Hedberg60478052014-02-18 10:19:31 +0200281 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
282
283 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
284 if (err)
285 return false;
286
287 return !memcmp(bdaddr->b, hash, 3);
288}
289
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300290int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200291{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300292 struct l2cap_chan *chan = hdev->smp_data;
293 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200294 int err;
295
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300296 if (!chan || !chan->data)
297 return -EOPNOTSUPP;
298
299 tfm = chan->data;
300
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200301 get_random_bytes(&rpa->b[3], 3);
302
303 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
304 rpa->b[5] |= 0x40; /* Set second most significant bit */
305
306 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
307 if (err < 0)
308 return err;
309
310 BT_DBG("RPA %pMR", rpa);
311
312 return 0;
313}
314
Johan Hedberge491eaf2014-10-25 21:15:37 +0200315static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
316 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
317 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300318{
319 u8 p1[16], p2[16];
320 int err;
321
322 memset(p1, 0, 16);
323
324 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200325 p1[0] = _iat;
326 p1[1] = _rat;
327 memcpy(p1 + 2, preq, 7);
328 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300329
330 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200331 memcpy(p2, ra, 6);
332 memcpy(p2 + 6, ia, 6);
333 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300334
335 /* res = r XOR p1 */
336 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
337
338 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200339 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300340 if (err) {
341 BT_ERR("Encrypt data error");
342 return err;
343 }
344
345 /* res = res XOR p2 */
346 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
347
348 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200349 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300350 if (err)
351 BT_ERR("Encrypt data error");
352
353 return err;
354}
355
Johan Hedberge491eaf2014-10-25 21:15:37 +0200356static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
357 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300358{
359 int err;
360
361 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200362 memcpy(_r, r2, 8);
363 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300364
Johan Hedberge491eaf2014-10-25 21:15:37 +0200365 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300366 if (err)
367 BT_ERR("Encrypt data error");
368
369 return err;
370}
371
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300372static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
373{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300374 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300375 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300376 struct kvec iv[2];
377 struct msghdr msg;
378
379 if (!chan)
380 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300381
382 BT_DBG("code 0x%2.2x", code);
383
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300384 iv[0].iov_base = &code;
385 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300386
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300387 iv[1].iov_base = data;
388 iv[1].iov_len = len;
389
390 memset(&msg, 0, sizeof(msg));
391
392 msg.msg_iov = (struct iovec *) &iv;
393 msg.msg_iovlen = 2;
394
395 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300396
Johan Hedbergb68fda62014-08-11 22:06:40 +0300397 if (!chan->data)
398 return;
399
400 smp = chan->data;
401
402 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300403 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300404}
405
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300406static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800407{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300408 if (authreq & SMP_AUTH_MITM) {
409 if (authreq & SMP_AUTH_SC)
410 return BT_SECURITY_FIPS;
411 else
412 return BT_SECURITY_HIGH;
413 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800414 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300415 }
Brian Gix2b64d152011-12-21 16:12:12 -0800416}
417
418static __u8 seclevel_to_authreq(__u8 sec_level)
419{
420 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300421 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800422 case BT_SECURITY_HIGH:
423 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
424 case BT_SECURITY_MEDIUM:
425 return SMP_AUTH_BONDING;
426 default:
427 return SMP_AUTH_NONE;
428 }
429}
430
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300431static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700432 struct smp_cmd_pairing *req,
433 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300434{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300435 struct l2cap_chan *chan = conn->smp;
436 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200437 struct hci_conn *hcon = conn->hcon;
438 struct hci_dev *hdev = hcon->hdev;
439 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300440
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300441 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700442 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
443 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300444 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800445 } else {
446 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300447 }
448
Johan Hedbergfd349c02014-02-18 10:19:36 +0200449 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
450 remote_dist |= SMP_DIST_ID_KEY;
451
Johan Hedberg863efaf2014-02-22 19:06:32 +0200452 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
453 local_dist |= SMP_DIST_ID_KEY;
454
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300455 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
456 if ((authreq & SMP_AUTH_SC) &&
457 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
458 local_dist |= SMP_DIST_LINK_KEY;
459 remote_dist |= SMP_DIST_LINK_KEY;
460 }
461 } else {
462 authreq &= ~SMP_AUTH_SC;
463 }
464
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300465 if (rsp == NULL) {
466 req->io_capability = conn->hcon->io_capability;
467 req->oob_flag = SMP_OOB_NOT_PRESENT;
468 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200469 req->init_key_dist = local_dist;
470 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300471 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200472
473 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300474 return;
475 }
476
477 rsp->io_capability = conn->hcon->io_capability;
478 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
479 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200480 rsp->init_key_dist = req->init_key_dist & remote_dist;
481 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300482 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200483
484 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300485}
486
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300487static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
488{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300489 struct l2cap_chan *chan = conn->smp;
490 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300491
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300492 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700493 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300494 return SMP_ENC_KEY_SIZE;
495
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300496 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300497
498 return 0;
499}
500
Johan Hedberg6f48e262014-08-11 22:06:44 +0300501static void smp_chan_destroy(struct l2cap_conn *conn)
502{
503 struct l2cap_chan *chan = conn->smp;
504 struct smp_chan *smp = chan->data;
505 bool complete;
506
507 BUG_ON(!smp);
508
509 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300510
Johan Hedberg6f48e262014-08-11 22:06:44 +0300511 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
512 mgmt_smp_complete(conn->hcon, complete);
513
514 kfree(smp->csrk);
515 kfree(smp->slave_csrk);
516
517 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300518 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300519
520 /* If pairing failed clean up any keys we might have */
521 if (!complete) {
522 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200523 list_del_rcu(&smp->ltk->list);
524 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300525 }
526
527 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200528 list_del_rcu(&smp->slave_ltk->list);
529 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300530 }
531
532 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200533 list_del_rcu(&smp->remote_irk->list);
534 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300535 }
536 }
537
538 chan->data = NULL;
539 kfree(smp);
540 hci_conn_drop(conn->hcon);
541}
542
Johan Hedberg84794e12013-11-06 11:24:57 +0200543static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800544{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200545 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300546 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200547
Johan Hedberg84794e12013-11-06 11:24:57 +0200548 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800549 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700550 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800551
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700552 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700553 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300554
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300555 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300556 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800557}
558
Brian Gix2b64d152011-12-21 16:12:12 -0800559#define JUST_WORKS 0x00
560#define JUST_CFM 0x01
561#define REQ_PASSKEY 0x02
562#define CFM_PASSKEY 0x03
563#define REQ_OOB 0x04
564#define OVERLAP 0xFF
565
566static const u8 gen_method[5][5] = {
567 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
568 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
569 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
570 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
571 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
572};
573
Johan Hedberg581370c2014-06-17 13:07:38 +0300574static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
575{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300576 /* If either side has unknown io_caps, use JUST_CFM (which gets
577 * converted later to JUST_WORKS if we're initiators.
578 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300579 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
580 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300581 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300582
583 return gen_method[remote_io][local_io];
584}
585
Brian Gix2b64d152011-12-21 16:12:12 -0800586static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
587 u8 local_io, u8 remote_io)
588{
589 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300590 struct l2cap_chan *chan = conn->smp;
591 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800592 u8 method;
593 u32 passkey = 0;
594 int ret = 0;
595
596 /* Initialize key for JUST WORKS */
597 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300598 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800599
600 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
601
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300602 /* If neither side wants MITM, either "just" confirm an incoming
603 * request or use just-works for outgoing ones. The JUST_CFM
604 * will be converted to JUST_WORKS if necessary later in this
605 * function. If either side has MITM look up the method from the
606 * table.
607 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300608 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300609 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800610 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300611 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800612
Johan Hedberga82505c2014-03-24 14:39:07 +0200613 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300614 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200615 method = JUST_WORKS;
616
Johan Hedberg02f3e252014-07-16 15:09:13 +0300617 /* Don't bother user space with no IO capabilities */
618 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
619 method = JUST_WORKS;
620
Brian Gix2b64d152011-12-21 16:12:12 -0800621 /* If Just Works, Continue with Zero TK */
622 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300623 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800624 return 0;
625 }
626
627 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300628 if (method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300629 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300630 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
631 hcon->pending_sec_level = BT_SECURITY_HIGH;
632 }
Brian Gix2b64d152011-12-21 16:12:12 -0800633
634 /* If both devices have Keyoard-Display I/O, the master
635 * Confirms and the slave Enters the passkey.
636 */
637 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300638 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800639 method = CFM_PASSKEY;
640 else
641 method = REQ_PASSKEY;
642 }
643
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200644 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800645 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200646 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800647 get_random_bytes(&passkey, sizeof(passkey));
648 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200649 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800650 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300651 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800652 }
653
Brian Gix2b64d152011-12-21 16:12:12 -0800654 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700655 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200656 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200657 else if (method == JUST_CFM)
658 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
659 hcon->type, hcon->dst_type,
660 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800661 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200662 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200663 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200664 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800665
Brian Gix2b64d152011-12-21 16:12:12 -0800666 return ret;
667}
668
Johan Hedberg1cc61142014-05-20 09:45:52 +0300669static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300670{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300671 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300672 struct smp_cmd_pairing_confirm cp;
673 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300674
675 BT_DBG("conn %p", conn);
676
Johan Hedberge491eaf2014-10-25 21:15:37 +0200677 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200678 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200679 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
680 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300681 if (ret)
682 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300683
Johan Hedberg4a74d652014-05-20 09:45:50 +0300684 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800685
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300686 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
687
Johan Hedbergb28b4942014-09-05 22:19:55 +0300688 if (conn->hcon->out)
689 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
690 else
691 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
692
Johan Hedberg1cc61142014-05-20 09:45:52 +0300693 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300694}
695
Johan Hedberg861580a2014-05-20 09:45:51 +0300696static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300697{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300698 struct l2cap_conn *conn = smp->conn;
699 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300700 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300701 int ret;
702
Johan Hedbergec70f362014-06-27 14:23:04 +0300703 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300704 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300705
706 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
707
Johan Hedberge491eaf2014-10-25 21:15:37 +0200708 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200709 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200710 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300711 if (ret)
712 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300713
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300714 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
715 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300716 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300717 }
718
719 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800720 u8 stk[16];
721 __le64 rand = 0;
722 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300723
Johan Hedberge491eaf2014-10-25 21:15:37 +0200724 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300725
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300726 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300727 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300728
Johan Hedberg861580a2014-05-20 09:45:51 +0300729 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
730 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300731
732 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300733 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300734 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300735 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300736 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800737 __le64 rand = 0;
738 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300739
Johan Hedberg943a7322014-03-18 12:58:24 +0200740 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
741 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300742
Johan Hedberge491eaf2014-10-25 21:15:37 +0200743 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300744
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300745 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700746 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300747
Johan Hedbergfff34902014-06-10 15:19:50 +0300748 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
749 auth = 1;
750 else
751 auth = 0;
752
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300753 /* Even though there's no _SLAVE suffix this is the
754 * slave STK we're adding for later lookup (the master
755 * STK never needs to be stored).
756 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700757 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300758 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300759 }
760
Johan Hedberg861580a2014-05-20 09:45:51 +0300761 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300762}
763
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300764static void smp_notify_keys(struct l2cap_conn *conn)
765{
766 struct l2cap_chan *chan = conn->smp;
767 struct smp_chan *smp = chan->data;
768 struct hci_conn *hcon = conn->hcon;
769 struct hci_dev *hdev = hcon->hdev;
770 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
771 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
772 bool persistent;
773
774 if (smp->remote_irk) {
775 mgmt_new_irk(hdev, smp->remote_irk);
776 /* Now that user space can be considered to know the
777 * identity address track the connection based on it
778 * from now on.
779 */
780 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
781 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300782 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300783
784 /* When receiving an indentity resolving key for
785 * a remote device that does not use a resolvable
786 * private address, just remove the key so that
787 * it is possible to use the controller white
788 * list for scanning.
789 *
790 * Userspace will have been told to not store
791 * this key at this point. So it is safe to
792 * just remove it.
793 */
794 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200795 list_del_rcu(&smp->remote_irk->list);
796 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300797 smp->remote_irk = NULL;
798 }
799 }
800
801 /* The LTKs and CSRKs should be persistent only if both sides
802 * had the bonding bit set in their authentication requests.
803 */
804 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
805
806 if (smp->csrk) {
807 smp->csrk->bdaddr_type = hcon->dst_type;
808 bacpy(&smp->csrk->bdaddr, &hcon->dst);
809 mgmt_new_csrk(hdev, smp->csrk, persistent);
810 }
811
812 if (smp->slave_csrk) {
813 smp->slave_csrk->bdaddr_type = hcon->dst_type;
814 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
815 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
816 }
817
818 if (smp->ltk) {
819 smp->ltk->bdaddr_type = hcon->dst_type;
820 bacpy(&smp->ltk->bdaddr, &hcon->dst);
821 mgmt_new_ltk(hdev, smp->ltk, persistent);
822 }
823
824 if (smp->slave_ltk) {
825 smp->slave_ltk->bdaddr_type = hcon->dst_type;
826 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
827 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
828 }
829}
830
Johan Hedbergb28b4942014-09-05 22:19:55 +0300831static void smp_allow_key_dist(struct smp_chan *smp)
832{
833 /* Allow the first expected phase 3 PDU. The rest of the PDUs
834 * will be allowed in each PDU handler to ensure we receive
835 * them in the correct order.
836 */
837 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
838 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
839 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
840 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
841 else if (smp->remote_key_dist & SMP_DIST_SIGN)
842 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
843}
844
Johan Hedbergd6268e82014-09-05 22:19:51 +0300845static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300846{
847 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300848 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300849 struct hci_conn *hcon = conn->hcon;
850 struct hci_dev *hdev = hcon->hdev;
851 __u8 *keydist;
852
853 BT_DBG("conn %p", conn);
854
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300855 rsp = (void *) &smp->prsp[1];
856
857 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300858 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
859 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300860 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300861 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300862
863 req = (void *) &smp->preq[1];
864
865 if (hcon->out) {
866 keydist = &rsp->init_key_dist;
867 *keydist &= req->init_key_dist;
868 } else {
869 keydist = &rsp->resp_key_dist;
870 *keydist &= req->resp_key_dist;
871 }
872
873 BT_DBG("keydist 0x%x", *keydist);
874
875 if (*keydist & SMP_DIST_ENC_KEY) {
876 struct smp_cmd_encrypt_info enc;
877 struct smp_cmd_master_ident ident;
878 struct smp_ltk *ltk;
879 u8 authenticated;
880 __le16 ediv;
881 __le64 rand;
882
883 get_random_bytes(enc.ltk, sizeof(enc.ltk));
884 get_random_bytes(&ediv, sizeof(ediv));
885 get_random_bytes(&rand, sizeof(rand));
886
887 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
888
889 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
890 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
891 SMP_LTK_SLAVE, authenticated, enc.ltk,
892 smp->enc_key_size, ediv, rand);
893 smp->slave_ltk = ltk;
894
895 ident.ediv = ediv;
896 ident.rand = rand;
897
898 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
899
900 *keydist &= ~SMP_DIST_ENC_KEY;
901 }
902
903 if (*keydist & SMP_DIST_ID_KEY) {
904 struct smp_cmd_ident_addr_info addrinfo;
905 struct smp_cmd_ident_info idinfo;
906
907 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
908
909 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
910
911 /* The hci_conn contains the local identity address
912 * after the connection has been established.
913 *
914 * This is true even when the connection has been
915 * established using a resolvable random address.
916 */
917 bacpy(&addrinfo.bdaddr, &hcon->src);
918 addrinfo.addr_type = hcon->src_type;
919
920 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
921 &addrinfo);
922
923 *keydist &= ~SMP_DIST_ID_KEY;
924 }
925
926 if (*keydist & SMP_DIST_SIGN) {
927 struct smp_cmd_sign_info sign;
928 struct smp_csrk *csrk;
929
930 /* Generate a new random key */
931 get_random_bytes(sign.csrk, sizeof(sign.csrk));
932
933 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
934 if (csrk) {
935 csrk->master = 0x00;
936 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
937 }
938 smp->slave_csrk = csrk;
939
940 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
941
942 *keydist &= ~SMP_DIST_SIGN;
943 }
944
945 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300946 if (smp->remote_key_dist & KEY_DIST_MASK) {
947 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300948 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300949 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300950
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300951 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
952 smp_notify_keys(conn);
953
954 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300955}
956
Johan Hedbergb68fda62014-08-11 22:06:40 +0300957static void smp_timeout(struct work_struct *work)
958{
959 struct smp_chan *smp = container_of(work, struct smp_chan,
960 security_timer.work);
961 struct l2cap_conn *conn = smp->conn;
962
963 BT_DBG("conn %p", conn);
964
Johan Hedberg1e91c292014-08-18 20:33:29 +0300965 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300966}
967
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300968static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
969{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300970 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300971 struct smp_chan *smp;
972
Marcel Holtmannf1560462013-10-13 05:43:25 -0700973 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300974 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300975 return NULL;
976
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300977 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
978 if (IS_ERR(smp->tfm_aes)) {
979 BT_ERR("Unable to create ECB crypto context");
980 kfree(smp);
981 return NULL;
982 }
983
Johan Hedberg407cecf2014-05-02 14:19:47 +0300984 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
985 if (IS_ERR(smp->tfm_cmac)) {
986 BT_ERR("Unable to create CMAC crypto context");
987 crypto_free_blkcipher(smp->tfm_aes);
988 kfree(smp);
989 return NULL;
990 }
991
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300992 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300993 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300994
Johan Hedbergb28b4942014-09-05 22:19:55 +0300995 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
996
Johan Hedbergb68fda62014-08-11 22:06:40 +0300997 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
998
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300999 hci_conn_hold(conn->hcon);
1000
1001 return smp;
1002}
1003
Brian Gix2b64d152011-12-21 16:12:12 -08001004int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1005{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001006 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001007 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001008 struct smp_chan *smp;
1009 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001010 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001011
1012 BT_DBG("");
1013
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001014 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001015 return -ENOTCONN;
1016
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001017 chan = conn->smp;
1018 if (!chan)
1019 return -ENOTCONN;
1020
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001021 l2cap_chan_lock(chan);
1022 if (!chan->data) {
1023 err = -ENOTCONN;
1024 goto unlock;
1025 }
1026
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001027 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001028
1029 switch (mgmt_op) {
1030 case MGMT_OP_USER_PASSKEY_REPLY:
1031 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001032 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001033 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001034 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001035 /* Fall Through */
1036 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001037 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001038 break;
1039 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1040 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001041 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001042 err = 0;
1043 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001044 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001045 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001046 err = -EOPNOTSUPP;
1047 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001048 }
1049
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001050 err = 0;
1051
Brian Gix2b64d152011-12-21 16:12:12 -08001052 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001053 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1054 u8 rsp = smp_confirm(smp);
1055 if (rsp)
1056 smp_failure(conn, rsp);
1057 }
Brian Gix2b64d152011-12-21 16:12:12 -08001058
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001059unlock:
1060 l2cap_chan_unlock(chan);
1061 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001062}
1063
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001064static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001065{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001066 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001067 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001068 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001069 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001070 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001071 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001072
1073 BT_DBG("conn %p", conn);
1074
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001075 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001076 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001077
Johan Hedberg40bef302014-07-16 11:42:27 +03001078 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001079 return SMP_CMD_NOTSUPP;
1080
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001081 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001082 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001083 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001084 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001085
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001086 if (!smp)
1087 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001088
Johan Hedbergc05b9332014-09-10 17:37:42 -07001089 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001090 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001091
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001092 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001093 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001094 return SMP_PAIRING_NOTSUPP;
1095
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001096 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1097 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001098 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001099
Johan Hedberg5be5e272014-09-10 17:58:54 -07001100 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001101 sec_level = BT_SECURITY_MEDIUM;
1102 else
1103 sec_level = authreq_to_seclevel(auth);
1104
Johan Hedbergc7262e72014-06-17 13:07:37 +03001105 if (sec_level > conn->hcon->pending_sec_level)
1106 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001107
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001108 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001109 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1110 u8 method;
1111
1112 method = get_auth_method(smp, conn->hcon->io_capability,
1113 req->io_capability);
1114 if (method == JUST_WORKS || method == JUST_CFM)
1115 return SMP_AUTH_REQUIREMENTS;
1116 }
1117
Brian Gix2b64d152011-12-21 16:12:12 -08001118 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001119
Johan Hedberg65668772014-05-16 11:03:34 +03001120 if (rsp.auth_req & SMP_AUTH_SC)
1121 set_bit(SMP_FLAG_SC, &smp->flags);
1122
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001123 key_size = min(req->max_key_size, rsp.max_key_size);
1124 if (check_enc_key_size(conn, key_size))
1125 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001126
Johan Hedberge84a6b12013-12-02 10:49:03 +02001127 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001128
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001129 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1130 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001131
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001132 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001133
1134 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1135
1136 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1137 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1138 /* Clear bits which are generated but not distributed */
1139 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1140 /* Wait for Public Key from Initiating Device */
1141 return 0;
1142 } else {
1143 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1144 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001145
Brian Gix2b64d152011-12-21 16:12:12 -08001146 /* Request setup of TK */
1147 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1148 if (ret)
1149 return SMP_UNSPECIFIED;
1150
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001151 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001152}
1153
Johan Hedberg3b191462014-06-06 10:50:15 +03001154static u8 sc_send_public_key(struct smp_chan *smp)
1155{
1156 BT_DBG("");
1157
1158 /* Generate local key pair for Secure Connections */
1159 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1160 return SMP_UNSPECIFIED;
1161
1162 BT_DBG("Local Public Key X: %32phN", smp->local_pk);
1163 BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1164 BT_DBG("Local Private Key: %32phN", smp->local_sk);
1165
1166 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1167
1168 return 0;
1169}
1170
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001171static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001172{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001173 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001174 struct l2cap_chan *chan = conn->smp;
1175 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001176 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001177 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001178 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001179
1180 BT_DBG("conn %p", conn);
1181
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001182 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001183 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001184
Johan Hedberg40bef302014-07-16 11:42:27 +03001185 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001186 return SMP_CMD_NOTSUPP;
1187
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001188 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001189
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001190 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001191
1192 key_size = min(req->max_key_size, rsp->max_key_size);
1193 if (check_enc_key_size(conn, key_size))
1194 return SMP_ENC_KEY_SIZE;
1195
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001196 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001197
Johan Hedberg65668772014-05-16 11:03:34 +03001198 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1199 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001200 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1201 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001202
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001203 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001204 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1205 u8 method;
1206
1207 method = get_auth_method(smp, req->io_capability,
1208 rsp->io_capability);
1209 if (method == JUST_WORKS || method == JUST_CFM)
1210 return SMP_AUTH_REQUIREMENTS;
1211 }
1212
Johan Hedberge84a6b12013-12-02 10:49:03 +02001213 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001214
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001215 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1216 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001217
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001218 /* Update remote key distribution in case the remote cleared
1219 * some bits that we had enabled in our request.
1220 */
1221 smp->remote_key_dist &= rsp->resp_key_dist;
1222
Johan Hedberg3b191462014-06-06 10:50:15 +03001223 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1224 /* Clear bits which are generated but not distributed */
1225 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1226 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1227 return sc_send_public_key(smp);
1228 }
1229
Johan Hedbergc05b9332014-09-10 17:37:42 -07001230 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001231
Johan Hedberg476585e2012-06-06 18:54:15 +08001232 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001233 if (ret)
1234 return SMP_UNSPECIFIED;
1235
Johan Hedberg4a74d652014-05-20 09:45:50 +03001236 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001237
1238 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001239 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001240 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001241
1242 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001243}
1244
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001245static u8 sc_check_confirm(struct smp_chan *smp)
1246{
1247 struct l2cap_conn *conn = smp->conn;
1248
1249 BT_DBG("");
1250
1251 /* Public Key exchange must happen before any other steps */
1252 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1253 return SMP_UNSPECIFIED;
1254
1255 if (conn->hcon->out) {
1256 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1257 smp->prnd);
1258 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1259 }
1260
1261 return 0;
1262}
1263
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001264static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001265{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001266 struct l2cap_chan *chan = conn->smp;
1267 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001268
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001269 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1270
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001271 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001272 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001273
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001274 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1275 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001276
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001277 if (test_bit(SMP_FLAG_SC, &smp->flags))
1278 return sc_check_confirm(smp);
1279
Johan Hedbergb28b4942014-09-05 22:19:55 +03001280 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001281 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1282 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001283 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1284 return 0;
1285 }
1286
1287 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001288 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001289 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001290 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001291
1292 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001293}
1294
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001295static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001296{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001297 struct l2cap_chan *chan = conn->smp;
1298 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001299 struct hci_conn *hcon = conn->hcon;
1300 u8 *pkax, *pkbx, *na, *nb;
1301 u32 passkey;
1302 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001303
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001304 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001305
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001306 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001307 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001308
Johan Hedberg943a7322014-03-18 12:58:24 +02001309 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001310 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001311
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001312 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1313 return smp_random(smp);
1314
1315 if (hcon->out) {
1316 u8 cfm[16];
1317
1318 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1319 smp->rrnd, 0, cfm);
1320 if (err)
1321 return SMP_UNSPECIFIED;
1322
1323 if (memcmp(smp->pcnf, cfm, 16))
1324 return SMP_CONFIRM_FAILED;
1325
1326 pkax = smp->local_pk;
1327 pkbx = smp->remote_pk;
1328 na = smp->prnd;
1329 nb = smp->rrnd;
1330 } else {
1331 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1332 smp->prnd);
1333 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1334
1335 pkax = smp->remote_pk;
1336 pkbx = smp->local_pk;
1337 na = smp->rrnd;
1338 nb = smp->prnd;
1339 }
1340
1341 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
1342 if (err)
1343 return SMP_UNSPECIFIED;
1344
1345 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
1346 hcon->type, hcon->dst_type,
1347 passkey, 0);
1348 if (err)
1349 return SMP_UNSPECIFIED;
1350
1351 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001352}
1353
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001354static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001355{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001356 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001357 struct hci_conn *hcon = conn->hcon;
1358
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001359 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001360 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001361 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001362
Johan Hedberga6f78332014-09-10 17:37:45 -07001363 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001364 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001365
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001366 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001367 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001368
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001369 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1370 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001371
Johan Hedbergfe59a052014-07-01 19:14:12 +03001372 /* We never store STKs for master role, so clear this flag */
1373 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1374
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001375 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001376}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001377
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001378bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1379 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03001380{
1381 if (sec_level == BT_SECURITY_LOW)
1382 return true;
1383
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001384 /* If we're encrypted with an STK but the caller prefers using
1385 * LTK claim insufficient security. This way we allow the
1386 * connection to be re-encrypted with an LTK, even if the LTK
1387 * provides the same level of security. Only exception is if we
1388 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001389 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001390 if (key_pref == SMP_USE_LTK &&
1391 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001392 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001393 return false;
1394
Johan Hedberg854f4722014-07-01 18:40:20 +03001395 if (hcon->sec_level >= sec_level)
1396 return true;
1397
1398 return false;
1399}
1400
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001401static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001402{
1403 struct smp_cmd_security_req *rp = (void *) skb->data;
1404 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001405 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001406 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001407 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07001408 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001409
1410 BT_DBG("conn %p", conn);
1411
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001412 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001413 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001414
Johan Hedberg40bef302014-07-16 11:42:27 +03001415 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001416 return SMP_CMD_NOTSUPP;
1417
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001418 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001419
Johan Hedberg5be5e272014-09-10 17:58:54 -07001420 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001421 sec_level = BT_SECURITY_MEDIUM;
1422 else
1423 sec_level = authreq_to_seclevel(auth);
1424
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001425 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03001426 return 0;
1427
Johan Hedbergc7262e72014-06-17 13:07:37 +03001428 if (sec_level > hcon->pending_sec_level)
1429 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001430
Johan Hedberg4dab7862012-06-07 14:58:37 +08001431 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001432 return 0;
1433
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001434 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001435 if (!smp)
1436 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001437
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001438 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001439 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03001440 return SMP_PAIRING_NOTSUPP;
1441
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001442 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001443
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001444 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07001445 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001446
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001447 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1448 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001449
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001450 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001451 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001452
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001453 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001454}
1455
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001456int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001457{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001458 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001459 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001460 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001461 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001462 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001463
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001464 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1465
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001466 /* This may be NULL if there's an unexpected disconnection */
1467 if (!conn)
1468 return 1;
1469
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001470 chan = conn->smp;
1471
Johan Hedberg757aee02013-04-24 13:05:32 +03001472 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001473 return 1;
1474
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001475 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001476 return 1;
1477
Johan Hedbergc7262e72014-06-17 13:07:37 +03001478 if (sec_level > hcon->pending_sec_level)
1479 hcon->pending_sec_level = sec_level;
1480
Johan Hedberg40bef302014-07-16 11:42:27 +03001481 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001482 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1483 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001484
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001485 l2cap_chan_lock(chan);
1486
1487 /* If SMP is already in progress ignore this request */
1488 if (chan->data) {
1489 ret = 0;
1490 goto unlock;
1491 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001492
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001493 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001494 if (!smp) {
1495 ret = 1;
1496 goto unlock;
1497 }
Brian Gix2b64d152011-12-21 16:12:12 -08001498
1499 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001500
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001501 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
1502 authreq |= SMP_AUTH_SC;
1503
Johan Hedberg79897d22014-06-01 09:45:24 +03001504 /* Require MITM if IO Capability allows or the security level
1505 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001506 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001507 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001508 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001509 authreq |= SMP_AUTH_MITM;
1510
Johan Hedberg40bef302014-07-16 11:42:27 +03001511 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001512 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001513
Brian Gix2b64d152011-12-21 16:12:12 -08001514 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001515 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1516 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001517
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001518 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001519 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001520 } else {
1521 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001522 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001523 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001524 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001525 }
1526
Johan Hedberg4a74d652014-05-20 09:45:50 +03001527 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001528 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001529
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001530unlock:
1531 l2cap_chan_unlock(chan);
1532 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001533}
1534
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001535static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1536{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001537 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001538 struct l2cap_chan *chan = conn->smp;
1539 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001540
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001541 BT_DBG("conn %p", conn);
1542
1543 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001544 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001545
Johan Hedbergb28b4942014-09-05 22:19:55 +03001546 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001547
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001548 skb_pull(skb, sizeof(*rp));
1549
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001550 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001551
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001552 return 0;
1553}
1554
1555static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1556{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001557 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001558 struct l2cap_chan *chan = conn->smp;
1559 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001560 struct hci_dev *hdev = conn->hcon->hdev;
1561 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001562 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001563 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001564
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001565 BT_DBG("conn %p", conn);
1566
1567 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001568 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001569
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001570 /* Mark the information as received */
1571 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1572
Johan Hedbergb28b4942014-09-05 22:19:55 +03001573 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1574 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07001575 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1576 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001577
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001578 skb_pull(skb, sizeof(*rp));
1579
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001580 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001581 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001582 authenticated, smp->tk, smp->enc_key_size,
1583 rp->ediv, rp->rand);
1584 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001585 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001586 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001587
1588 return 0;
1589}
1590
Johan Hedbergfd349c02014-02-18 10:19:36 +02001591static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1592{
1593 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001594 struct l2cap_chan *chan = conn->smp;
1595 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001596
1597 BT_DBG("");
1598
1599 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001600 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001601
Johan Hedbergb28b4942014-09-05 22:19:55 +03001602 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001603
Johan Hedbergfd349c02014-02-18 10:19:36 +02001604 skb_pull(skb, sizeof(*info));
1605
1606 memcpy(smp->irk, info->irk, 16);
1607
1608 return 0;
1609}
1610
1611static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1612 struct sk_buff *skb)
1613{
1614 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001615 struct l2cap_chan *chan = conn->smp;
1616 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001617 struct hci_conn *hcon = conn->hcon;
1618 bdaddr_t rpa;
1619
1620 BT_DBG("");
1621
1622 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001623 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001624
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001625 /* Mark the information as received */
1626 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1627
Johan Hedbergb28b4942014-09-05 22:19:55 +03001628 if (smp->remote_key_dist & SMP_DIST_SIGN)
1629 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1630
Johan Hedbergfd349c02014-02-18 10:19:36 +02001631 skb_pull(skb, sizeof(*info));
1632
Johan Hedberga9a58f82014-02-25 22:24:37 +02001633 /* Strictly speaking the Core Specification (4.1) allows sending
1634 * an empty address which would force us to rely on just the IRK
1635 * as "identity information". However, since such
1636 * implementations are not known of and in order to not over
1637 * complicate our implementation, simply pretend that we never
1638 * received an IRK for such a device.
1639 */
1640 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1641 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001642 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001643 }
1644
Johan Hedbergfd349c02014-02-18 10:19:36 +02001645 bacpy(&smp->id_addr, &info->bdaddr);
1646 smp->id_addr_type = info->addr_type;
1647
1648 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1649 bacpy(&rpa, &hcon->dst);
1650 else
1651 bacpy(&rpa, BDADDR_ANY);
1652
Johan Hedberg23d0e122014-02-19 14:57:46 +02001653 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1654 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001655
Johan Hedberg31dd6242014-06-27 14:23:02 +03001656distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001657 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1658 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001659
1660 return 0;
1661}
1662
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001663static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1664{
1665 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001666 struct l2cap_chan *chan = conn->smp;
1667 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001668 struct smp_csrk *csrk;
1669
1670 BT_DBG("conn %p", conn);
1671
1672 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001673 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001674
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001675 /* Mark the information as received */
1676 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1677
1678 skb_pull(skb, sizeof(*rp));
1679
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001680 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1681 if (csrk) {
1682 csrk->master = 0x01;
1683 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1684 }
1685 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001686 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001687
1688 return 0;
1689}
1690
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001691static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
1692{
1693 struct smp_cmd_public_key *key = (void *) skb->data;
1694 struct hci_conn *hcon = conn->hcon;
1695 struct l2cap_chan *chan = conn->smp;
1696 struct smp_chan *smp = chan->data;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03001697 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001698 int err;
1699
1700 BT_DBG("conn %p", conn);
1701
1702 if (skb->len < sizeof(*key))
1703 return SMP_INVALID_PARAMS;
1704
1705 memcpy(smp->remote_pk, key, 64);
1706
1707 /* Non-initiating device sends its public key after receiving
1708 * the key from the initiating device.
1709 */
1710 if (!hcon->out) {
1711 err = sc_send_public_key(smp);
1712 if (err)
1713 return err;
1714 }
1715
1716 BT_DBG("Remote Public Key X: %32phN", smp->remote_pk);
1717 BT_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
1718
1719 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
1720 return SMP_UNSPECIFIED;
1721
1722 BT_DBG("DHKey %32phN", smp->dhkey);
1723
1724 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
1725
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03001726 /* The Initiating device waits for the non-initiating device to
1727 * send the confirm value.
1728 */
1729 if (conn->hcon->out)
1730 return 0;
1731
1732 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
1733 0, cfm.confirm_val);
1734 if (err)
1735 return SMP_UNSPECIFIED;
1736
1737 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1738 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1739
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001740 return 0;
1741}
1742
Johan Hedberg4befb862014-08-11 22:06:38 +03001743static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001744{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001745 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001746 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001747 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001748 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001749 int err = 0;
1750
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001751 if (hcon->type != LE_LINK) {
1752 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001753 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001754 }
1755
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001756 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001757 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001758
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001759 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001760 reason = SMP_PAIRING_NOTSUPP;
1761 goto done;
1762 }
1763
Marcel Holtmann92381f52013-10-03 01:23:08 -07001764 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001765 skb_pull(skb, sizeof(code));
1766
Johan Hedbergb28b4942014-09-05 22:19:55 +03001767 smp = chan->data;
1768
1769 if (code > SMP_CMD_MAX)
1770 goto drop;
1771
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07001772 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03001773 goto drop;
1774
1775 /* If we don't have a context the only allowed commands are
1776 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001777 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001778 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1779 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001780
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001781 switch (code) {
1782 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001783 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001784 break;
1785
1786 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001787 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001788 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001789 break;
1790
1791 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001792 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001793 break;
1794
1795 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001796 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001797 break;
1798
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001799 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001800 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001801 break;
1802
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001803 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001804 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001805 break;
1806
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001807 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001808 reason = smp_cmd_encrypt_info(conn, skb);
1809 break;
1810
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001811 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001812 reason = smp_cmd_master_ident(conn, skb);
1813 break;
1814
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001815 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001816 reason = smp_cmd_ident_info(conn, skb);
1817 break;
1818
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001819 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001820 reason = smp_cmd_ident_addr_info(conn, skb);
1821 break;
1822
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001823 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001824 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001825 break;
1826
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001827 case SMP_CMD_PUBLIC_KEY:
1828 reason = smp_cmd_public_key(conn, skb);
1829 break;
1830
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001831 default:
1832 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001833 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001834 goto done;
1835 }
1836
1837done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001838 if (!err) {
1839 if (reason)
1840 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001841 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001842 }
1843
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001844 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001845
1846drop:
1847 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
1848 code, &hcon->dst);
1849 kfree_skb(skb);
1850 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001851}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001852
Johan Hedberg70db83c2014-08-08 09:37:16 +03001853static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1854{
1855 struct l2cap_conn *conn = chan->conn;
1856
1857 BT_DBG("chan %p", chan);
1858
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001859 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001860 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001861
Johan Hedberg70db83c2014-08-08 09:37:16 +03001862 conn->smp = NULL;
1863 l2cap_chan_put(chan);
1864}
1865
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001866static void smp_resume_cb(struct l2cap_chan *chan)
1867{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001868 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001869 struct l2cap_conn *conn = chan->conn;
1870 struct hci_conn *hcon = conn->hcon;
1871
1872 BT_DBG("chan %p", chan);
1873
Johan Hedberg86d14072014-08-11 22:06:43 +03001874 if (!smp)
1875 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001876
Johan Hedberg84bc0db2014-09-05 22:19:49 +03001877 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1878 return;
1879
Johan Hedberg86d14072014-08-11 22:06:43 +03001880 cancel_delayed_work(&smp->security_timer);
1881
Johan Hedbergd6268e82014-09-05 22:19:51 +03001882 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001883}
1884
Johan Hedberg70db83c2014-08-08 09:37:16 +03001885static void smp_ready_cb(struct l2cap_chan *chan)
1886{
1887 struct l2cap_conn *conn = chan->conn;
1888
1889 BT_DBG("chan %p", chan);
1890
1891 conn->smp = chan;
1892 l2cap_chan_hold(chan);
1893}
1894
Johan Hedberg4befb862014-08-11 22:06:38 +03001895static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1896{
1897 int err;
1898
1899 BT_DBG("chan %p", chan);
1900
1901 err = smp_sig_channel(chan, skb);
1902 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001903 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001904
Johan Hedbergb68fda62014-08-11 22:06:40 +03001905 if (smp)
1906 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001907
Johan Hedberg1e91c292014-08-18 20:33:29 +03001908 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03001909 }
1910
1911 return err;
1912}
1913
Johan Hedberg70db83c2014-08-08 09:37:16 +03001914static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1915 unsigned long hdr_len,
1916 unsigned long len, int nb)
1917{
1918 struct sk_buff *skb;
1919
1920 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1921 if (!skb)
1922 return ERR_PTR(-ENOMEM);
1923
1924 skb->priority = HCI_PRIO_MAX;
1925 bt_cb(skb)->chan = chan;
1926
1927 return skb;
1928}
1929
1930static const struct l2cap_ops smp_chan_ops = {
1931 .name = "Security Manager",
1932 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001933 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001934 .alloc_skb = smp_alloc_skb_cb,
1935 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001936 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001937
1938 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001939 .state_change = l2cap_chan_no_state_change,
1940 .close = l2cap_chan_no_close,
1941 .defer = l2cap_chan_no_defer,
1942 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001943 .set_shutdown = l2cap_chan_no_set_shutdown,
1944 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1945 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1946};
1947
1948static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1949{
1950 struct l2cap_chan *chan;
1951
1952 BT_DBG("pchan %p", pchan);
1953
1954 chan = l2cap_chan_create();
1955 if (!chan)
1956 return NULL;
1957
1958 chan->chan_type = pchan->chan_type;
1959 chan->ops = &smp_chan_ops;
1960 chan->scid = pchan->scid;
1961 chan->dcid = chan->scid;
1962 chan->imtu = pchan->imtu;
1963 chan->omtu = pchan->omtu;
1964 chan->mode = pchan->mode;
1965
Johan Hedbergabe84902014-11-12 22:22:21 +02001966 /* Other L2CAP channels may request SMP routines in order to
1967 * change the security level. This means that the SMP channel
1968 * lock must be considered in its own category to avoid lockdep
1969 * warnings.
1970 */
1971 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
1972
Johan Hedberg70db83c2014-08-08 09:37:16 +03001973 BT_DBG("created chan %p", chan);
1974
1975 return chan;
1976}
1977
1978static const struct l2cap_ops smp_root_chan_ops = {
1979 .name = "Security Manager Root",
1980 .new_connection = smp_new_conn_cb,
1981
1982 /* None of these are implemented for the root channel */
1983 .close = l2cap_chan_no_close,
1984 .alloc_skb = l2cap_chan_no_alloc_skb,
1985 .recv = l2cap_chan_no_recv,
1986 .state_change = l2cap_chan_no_state_change,
1987 .teardown = l2cap_chan_no_teardown,
1988 .ready = l2cap_chan_no_ready,
1989 .defer = l2cap_chan_no_defer,
1990 .suspend = l2cap_chan_no_suspend,
1991 .resume = l2cap_chan_no_resume,
1992 .set_shutdown = l2cap_chan_no_set_shutdown,
1993 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1994 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1995};
1996
Johan Hedberg711eafe2014-08-08 09:32:52 +03001997int smp_register(struct hci_dev *hdev)
1998{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001999 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002000 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002001
Johan Hedberg711eafe2014-08-08 09:32:52 +03002002 BT_DBG("%s", hdev->name);
2003
Johan Hedbergadae20c2014-11-13 14:37:48 +02002004 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002005 if (IS_ERR(tfm_aes)) {
2006 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002007 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03002008 return err;
2009 }
2010
Johan Hedberg70db83c2014-08-08 09:37:16 +03002011 chan = l2cap_chan_create();
2012 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002013 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002014 return -ENOMEM;
2015 }
2016
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002017 chan->data = tfm_aes;
2018
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002019 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002020
2021 l2cap_chan_set_defaults(chan);
2022
2023 bacpy(&chan->src, &hdev->bdaddr);
2024 chan->src_type = BDADDR_LE_PUBLIC;
2025 chan->state = BT_LISTEN;
2026 chan->mode = L2CAP_MODE_BASIC;
2027 chan->imtu = L2CAP_DEFAULT_MTU;
2028 chan->ops = &smp_root_chan_ops;
2029
Johan Hedbergabe84902014-11-12 22:22:21 +02002030 /* Set correct nesting level for a parent/listening channel */
2031 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2032
Johan Hedberg70db83c2014-08-08 09:37:16 +03002033 hdev->smp_data = chan;
2034
Johan Hedberg711eafe2014-08-08 09:32:52 +03002035 return 0;
2036}
2037
2038void smp_unregister(struct hci_dev *hdev)
2039{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002040 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002041 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002042
2043 if (!chan)
2044 return;
2045
2046 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002047
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002048 tfm_aes = chan->data;
2049 if (tfm_aes) {
2050 chan->data = NULL;
2051 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002052 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03002053
2054 hdev->smp_data = NULL;
2055 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002056}