blob: 0c2214a418163da58fb7e610a1bdcce39e47b16f [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 Hedbergaeb7d462014-05-31 18:52:28 +030057 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030058 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030059 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg02b05bd2014-10-26 21:19:10 +010060 SMP_FLAG_OOB,
Johan Hedberg533e35d2014-06-16 19:25:18 +030061};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030062
63struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030064 struct l2cap_conn *conn;
65 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030066 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030067
Johan Hedberg4bc58f52014-05-20 09:45:47 +030068 u8 preq[7]; /* SMP Pairing Request */
69 u8 prsp[7]; /* SMP Pairing Response */
70 u8 prnd[16]; /* SMP Pairing Random (local) */
71 u8 rrnd[16]; /* SMP Pairing Random (remote) */
72 u8 pcnf[16]; /* SMP Pairing Confirm */
73 u8 tk[16]; /* SMP Temporary Key */
74 u8 enc_key_size;
75 u8 remote_key_dist;
76 bdaddr_t id_addr;
77 u8 id_addr_type;
78 u8 irk[16];
79 struct smp_csrk *csrk;
80 struct smp_csrk *slave_csrk;
81 struct smp_ltk *ltk;
82 struct smp_ltk *slave_ltk;
83 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +030084 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +030085 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +030086 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +030087 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030088
Johan Hedberg3b191462014-06-06 10:50:15 +030089 /* Secure Connections variables */
90 u8 local_pk[64];
91 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030092 u8 remote_pk[64];
93 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +030094 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +030095
Johan Hedberg6a7bd102014-06-27 14:23:03 +030096 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +030097 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030098};
99
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300100/* These debug key values are defined in the SMP section of the core
101 * specification. debug_pk is the public debug key and debug_sk the
102 * private debug key.
103 */
104static const u8 debug_pk[64] = {
105 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
106 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
107 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
108 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
109
110 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
111 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
112 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
113 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
114};
115
116static const u8 debug_sk[32] = {
117 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
118 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
119 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
120 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
121};
122
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300123static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300124{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300125 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300126
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300127 for (i = 0; i < len; i++)
128 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300129}
130
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300131static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
132 size_t len, u8 mac[16])
133{
134 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
135 struct hash_desc desc;
136 struct scatterlist sg;
137 int err;
138
139 if (len > CMAC_MSG_MAX)
140 return -EFBIG;
141
142 if (!tfm) {
143 BT_ERR("tfm %p", tfm);
144 return -EINVAL;
145 }
146
147 desc.tfm = tfm;
148 desc.flags = 0;
149
150 crypto_hash_init(&desc);
151
152 /* Swap key and message from LSB to MSB */
153 swap_buf(k, tmp, 16);
154 swap_buf(m, msg_msb, len);
155
156 BT_DBG("msg (len %zu) %*phN", len, (int) len, m);
157 BT_DBG("key %16phN", k);
158
159 err = crypto_hash_setkey(tfm, tmp, 16);
160 if (err) {
161 BT_ERR("cipher setkey failed: %d", err);
162 return err;
163 }
164
165 sg_init_one(&sg, msg_msb, len);
166
167 err = crypto_hash_update(&desc, &sg, len);
168 if (err) {
169 BT_ERR("Hash update error %d", err);
170 return err;
171 }
172
173 err = crypto_hash_final(&desc, mac_msb);
174 if (err) {
175 BT_ERR("Hash final error %d", err);
176 return err;
177 }
178
179 swap_buf(mac_msb, mac, 16);
180
181 BT_DBG("mac %16phN", mac);
182
183 return 0;
184}
185
186static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
187 const u8 x[16], u8 z, u8 res[16])
188{
189 u8 m[65];
190 int err;
191
192 BT_DBG("u %32phN", u);
193 BT_DBG("v %32phN", v);
194 BT_DBG("x %16phN z %02x", x, z);
195
196 m[0] = z;
197 memcpy(m + 1, v, 32);
198 memcpy(m + 33, u, 32);
199
200 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
201 if (err)
202 return err;
203
204 BT_DBG("res %16phN", res);
205
206 return err;
207}
208
Johan Hedberg760b0182014-06-06 11:44:05 +0300209static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
210 u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16])
211{
212 /* The btle, salt and length "magic" values are as defined in
213 * the SMP section of the Bluetooth core specification. In ASCII
214 * the btle value ends up being 'btle'. The salt is just a
215 * random number whereas length is the value 256 in little
216 * endian format.
217 */
218 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
219 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
220 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
221 const u8 length[2] = { 0x00, 0x01 };
222 u8 m[53], t[16];
223 int err;
224
225 BT_DBG("w %32phN", w);
226 BT_DBG("n1 %16phN n2 %16phN", n1, n2);
227 BT_DBG("a1 %7phN a2 %7phN", a1, a2);
228
229 err = aes_cmac(tfm_cmac, salt, w, 32, t);
230 if (err)
231 return err;
232
233 BT_DBG("t %16phN", t);
234
235 memcpy(m, length, 2);
236 memcpy(m + 2, a2, 7);
237 memcpy(m + 9, a1, 7);
238 memcpy(m + 16, n2, 16);
239 memcpy(m + 32, n1, 16);
240 memcpy(m + 48, btle, 4);
241
242 m[52] = 0; /* Counter */
243
244 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
245 if (err)
246 return err;
247
248 BT_DBG("mackey %16phN", mackey);
249
250 m[52] = 1; /* Counter */
251
252 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
253 if (err)
254 return err;
255
256 BT_DBG("ltk %16phN", ltk);
257
258 return 0;
259}
260
261static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
262 const u8 n1[16], u8 n2[16], const u8 r[16],
263 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
264 u8 res[16])
265{
266 u8 m[65];
267 int err;
268
269 BT_DBG("w %16phN", w);
270 BT_DBG("n1 %16phN n2 %16phN", n1, n2);
271 BT_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
272
273 memcpy(m, a2, 7);
274 memcpy(m + 7, a1, 7);
275 memcpy(m + 14, io_cap, 3);
276 memcpy(m + 17, r, 16);
277 memcpy(m + 33, n2, 16);
278 memcpy(m + 49, n1, 16);
279
280 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
281 if (err)
282 return err;
283
284 BT_DBG("res %16phN", res);
285
286 return err;
287}
288
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300289static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
290 const u8 x[16], const u8 y[16], u32 *val)
291{
292 u8 m[80], tmp[16];
293 int err;
294
295 BT_DBG("u %32phN", u);
296 BT_DBG("v %32phN", v);
297 BT_DBG("x %16phN y %16phN", x, y);
298
299 memcpy(m, y, 16);
300 memcpy(m + 16, v, 32);
301 memcpy(m + 48, u, 32);
302
303 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
304 if (err)
305 return err;
306
307 *val = get_unaligned_le32(tmp);
308 *val %= 1000000;
309
310 BT_DBG("val %06u", *val);
311
312 return 0;
313}
314
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300315static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
316{
317 struct blkcipher_desc desc;
318 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200319 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200320 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300321
322 if (tfm == NULL) {
323 BT_ERR("tfm %p", tfm);
324 return -EINVAL;
325 }
326
327 desc.tfm = tfm;
328 desc.flags = 0;
329
Johan Hedberg943a7322014-03-18 12:58:24 +0200330 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300331 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200332
333 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300334 if (err) {
335 BT_ERR("cipher setkey failed: %d", err);
336 return err;
337 }
338
Johan Hedberg943a7322014-03-18 12:58:24 +0200339 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300340 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200341
342 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300343
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300344 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
345 if (err)
346 BT_ERR("Encrypt data error %d", err);
347
Johan Hedberg943a7322014-03-18 12:58:24 +0200348 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300349 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200350
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300351 return err;
352}
353
Johan Hedberg6a770832014-06-06 11:54:04 +0300354static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
355 const u8 key_id[4], u8 res[16])
356{
357 int err;
358
359 BT_DBG("w %16phN key_id %4phN", w, key_id);
360
361 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
362 if (err)
363 return err;
364
365 BT_DBG("res %16phN", res);
366
367 return err;
368}
369
Johan Hedberg60478052014-02-18 10:19:31 +0200370static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
371{
Johan Hedberg943a7322014-03-18 12:58:24 +0200372 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200373 int err;
374
375 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200376 memcpy(_res, r, 3);
377 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200378
Johan Hedberg943a7322014-03-18 12:58:24 +0200379 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200380 if (err) {
381 BT_ERR("Encrypt error");
382 return err;
383 }
384
385 /* The output of the random address function ah is:
386 * ah(h, r) = e(k, r') mod 2^24
387 * The output of the security function e is then truncated to 24 bits
388 * by taking the least significant 24 bits of the output of e as the
389 * result of ah.
390 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200391 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200392
393 return 0;
394}
395
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300396bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200397{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300398 struct l2cap_chan *chan = hdev->smp_data;
399 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200400 u8 hash[3];
401 int err;
402
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300403 if (!chan || !chan->data)
404 return false;
405
406 tfm = chan->data;
407
Johan Hedberg60478052014-02-18 10:19:31 +0200408 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
409
410 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
411 if (err)
412 return false;
413
414 return !memcmp(bdaddr->b, hash, 3);
415}
416
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300417int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200418{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300419 struct l2cap_chan *chan = hdev->smp_data;
420 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200421 int err;
422
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300423 if (!chan || !chan->data)
424 return -EOPNOTSUPP;
425
426 tfm = chan->data;
427
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200428 get_random_bytes(&rpa->b[3], 3);
429
430 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
431 rpa->b[5] |= 0x40; /* Set second most significant bit */
432
433 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
434 if (err < 0)
435 return err;
436
437 BT_DBG("RPA %pMR", rpa);
438
439 return 0;
440}
441
Johan Hedberge491eaf2014-10-25 21:15:37 +0200442static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
443 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
444 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300445{
446 u8 p1[16], p2[16];
447 int err;
448
449 memset(p1, 0, 16);
450
451 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200452 p1[0] = _iat;
453 p1[1] = _rat;
454 memcpy(p1 + 2, preq, 7);
455 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300456
457 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200458 memcpy(p2, ra, 6);
459 memcpy(p2 + 6, ia, 6);
460 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300461
462 /* res = r XOR p1 */
463 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
464
465 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200466 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300467 if (err) {
468 BT_ERR("Encrypt data error");
469 return err;
470 }
471
472 /* res = res XOR p2 */
473 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
474
475 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200476 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300477 if (err)
478 BT_ERR("Encrypt data error");
479
480 return err;
481}
482
Johan Hedberge491eaf2014-10-25 21:15:37 +0200483static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
484 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300485{
486 int err;
487
488 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200489 memcpy(_r, r2, 8);
490 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300491
Johan Hedberge491eaf2014-10-25 21:15:37 +0200492 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300493 if (err)
494 BT_ERR("Encrypt data error");
495
496 return err;
497}
498
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300499static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
500{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300501 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300502 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300503 struct kvec iv[2];
504 struct msghdr msg;
505
506 if (!chan)
507 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300508
509 BT_DBG("code 0x%2.2x", code);
510
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300511 iv[0].iov_base = &code;
512 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300513
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300514 iv[1].iov_base = data;
515 iv[1].iov_len = len;
516
517 memset(&msg, 0, sizeof(msg));
518
519 msg.msg_iov = (struct iovec *) &iv;
520 msg.msg_iovlen = 2;
521
522 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300523
Johan Hedbergb68fda62014-08-11 22:06:40 +0300524 if (!chan->data)
525 return;
526
527 smp = chan->data;
528
529 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300530 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300531}
532
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300533static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800534{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300535 if (authreq & SMP_AUTH_MITM) {
536 if (authreq & SMP_AUTH_SC)
537 return BT_SECURITY_FIPS;
538 else
539 return BT_SECURITY_HIGH;
540 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800541 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300542 }
Brian Gix2b64d152011-12-21 16:12:12 -0800543}
544
545static __u8 seclevel_to_authreq(__u8 sec_level)
546{
547 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300548 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800549 case BT_SECURITY_HIGH:
550 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
551 case BT_SECURITY_MEDIUM:
552 return SMP_AUTH_BONDING;
553 default:
554 return SMP_AUTH_NONE;
555 }
556}
557
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300558static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700559 struct smp_cmd_pairing *req,
560 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300561{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300562 struct l2cap_chan *chan = conn->smp;
563 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200564 struct hci_conn *hcon = conn->hcon;
565 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100566 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300567
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300568 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700569 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
570 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300571 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800572 } else {
573 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300574 }
575
Johan Hedbergfd349c02014-02-18 10:19:36 +0200576 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
577 remote_dist |= SMP_DIST_ID_KEY;
578
Johan Hedberg863efaf2014-02-22 19:06:32 +0200579 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
580 local_dist |= SMP_DIST_ID_KEY;
581
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100582 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
583 (authreq & SMP_AUTH_SC)) {
584 struct oob_data *oob_data;
585 u8 bdaddr_type;
586
587 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300588 local_dist |= SMP_DIST_LINK_KEY;
589 remote_dist |= SMP_DIST_LINK_KEY;
590 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100591
592 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
593 bdaddr_type = BDADDR_LE_PUBLIC;
594 else
595 bdaddr_type = BDADDR_LE_RANDOM;
596
597 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
598 bdaddr_type);
599 if (oob_data) {
600 set_bit(SMP_FLAG_OOB, &smp->flags);
601 oob_flag = SMP_OOB_PRESENT;
602 memcpy(smp->rrnd, oob_data->rand256, 16);
603 memcpy(smp->pcnf, oob_data->hash256, 16);
604 }
605
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300606 } else {
607 authreq &= ~SMP_AUTH_SC;
608 }
609
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300610 if (rsp == NULL) {
611 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100612 req->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300613 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200614 req->init_key_dist = local_dist;
615 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300616 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200617
618 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300619 return;
620 }
621
622 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100623 rsp->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300624 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200625 rsp->init_key_dist = req->init_key_dist & remote_dist;
626 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300627 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200628
629 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300630}
631
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300632static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
633{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300634 struct l2cap_chan *chan = conn->smp;
635 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300636
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300637 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700638 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300639 return SMP_ENC_KEY_SIZE;
640
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300641 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300642
643 return 0;
644}
645
Johan Hedberg6f48e262014-08-11 22:06:44 +0300646static void smp_chan_destroy(struct l2cap_conn *conn)
647{
648 struct l2cap_chan *chan = conn->smp;
649 struct smp_chan *smp = chan->data;
650 bool complete;
651
652 BUG_ON(!smp);
653
654 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300655
Johan Hedberg6f48e262014-08-11 22:06:44 +0300656 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
657 mgmt_smp_complete(conn->hcon, complete);
658
659 kfree(smp->csrk);
660 kfree(smp->slave_csrk);
Johan Hedberg6a770832014-06-06 11:54:04 +0300661 kfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300662
663 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300664 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300665
666 /* If pairing failed clean up any keys we might have */
667 if (!complete) {
668 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200669 list_del_rcu(&smp->ltk->list);
670 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300671 }
672
673 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200674 list_del_rcu(&smp->slave_ltk->list);
675 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300676 }
677
678 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200679 list_del_rcu(&smp->remote_irk->list);
680 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300681 }
682 }
683
684 chan->data = NULL;
685 kfree(smp);
686 hci_conn_drop(conn->hcon);
687}
688
Johan Hedberg84794e12013-11-06 11:24:57 +0200689static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800690{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200691 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300692 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200693
Johan Hedberg84794e12013-11-06 11:24:57 +0200694 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800695 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700696 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800697
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700698 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700699 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300700
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300701 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300702 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800703}
704
Brian Gix2b64d152011-12-21 16:12:12 -0800705#define JUST_WORKS 0x00
706#define JUST_CFM 0x01
707#define REQ_PASSKEY 0x02
708#define CFM_PASSKEY 0x03
709#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300710#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800711#define OVERLAP 0xFF
712
713static const u8 gen_method[5][5] = {
714 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
715 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
716 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
717 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
718 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
719};
720
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300721static const u8 sc_method[5][5] = {
722 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
723 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
724 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
725 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
726 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
727};
728
Johan Hedberg581370c2014-06-17 13:07:38 +0300729static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
730{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300731 /* If either side has unknown io_caps, use JUST_CFM (which gets
732 * converted later to JUST_WORKS if we're initiators.
733 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300734 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
735 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300736 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300737
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300738 if (test_bit(SMP_FLAG_SC, &smp->flags))
739 return sc_method[remote_io][local_io];
740
Johan Hedberg581370c2014-06-17 13:07:38 +0300741 return gen_method[remote_io][local_io];
742}
743
Brian Gix2b64d152011-12-21 16:12:12 -0800744static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
745 u8 local_io, u8 remote_io)
746{
747 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300748 struct l2cap_chan *chan = conn->smp;
749 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800750 u32 passkey = 0;
751 int ret = 0;
752
753 /* Initialize key for JUST WORKS */
754 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300755 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800756
757 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
758
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300759 /* If neither side wants MITM, either "just" confirm an incoming
760 * request or use just-works for outgoing ones. The JUST_CFM
761 * will be converted to JUST_WORKS if necessary later in this
762 * function. If either side has MITM look up the method from the
763 * table.
764 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300765 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300766 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800767 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300768 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800769
Johan Hedberga82505c2014-03-24 14:39:07 +0200770 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300771 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
772 &smp->flags))
773 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200774
Johan Hedberg02f3e252014-07-16 15:09:13 +0300775 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300776 if (smp->method == JUST_CFM &&
777 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
778 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300779
Brian Gix2b64d152011-12-21 16:12:12 -0800780 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300781 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300782 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800783 return 0;
784 }
785
786 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300787 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300788 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300789 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
790 hcon->pending_sec_level = BT_SECURITY_HIGH;
791 }
Brian Gix2b64d152011-12-21 16:12:12 -0800792
793 /* If both devices have Keyoard-Display I/O, the master
794 * Confirms and the slave Enters the passkey.
795 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300796 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300797 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300798 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800799 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300800 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800801 }
802
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200803 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300804 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200805 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800806 get_random_bytes(&passkey, sizeof(passkey));
807 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200808 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800809 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300810 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800811 }
812
Johan Hedberg783e0572014-05-31 18:48:26 +0300813 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700814 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200815 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300816 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200817 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
818 hcon->type, hcon->dst_type,
819 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800820 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200821 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200822 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200823 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800824
Brian Gix2b64d152011-12-21 16:12:12 -0800825 return ret;
826}
827
Johan Hedberg1cc61142014-05-20 09:45:52 +0300828static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300829{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300830 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300831 struct smp_cmd_pairing_confirm cp;
832 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300833
834 BT_DBG("conn %p", conn);
835
Johan Hedberge491eaf2014-10-25 21:15:37 +0200836 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200837 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200838 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
839 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300840 if (ret)
841 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300842
Johan Hedberg4a74d652014-05-20 09:45:50 +0300843 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800844
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300845 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
846
Johan Hedbergb28b4942014-09-05 22:19:55 +0300847 if (conn->hcon->out)
848 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
849 else
850 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
851
Johan Hedberg1cc61142014-05-20 09:45:52 +0300852 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300853}
854
Johan Hedberg861580a2014-05-20 09:45:51 +0300855static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300856{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300857 struct l2cap_conn *conn = smp->conn;
858 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300859 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300860 int ret;
861
Johan Hedbergec70f362014-06-27 14:23:04 +0300862 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300863 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300864
865 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
866
Johan Hedberge491eaf2014-10-25 21:15:37 +0200867 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200868 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200869 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300870 if (ret)
871 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300872
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300873 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
874 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300875 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300876 }
877
878 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800879 u8 stk[16];
880 __le64 rand = 0;
881 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300882
Johan Hedberge491eaf2014-10-25 21:15:37 +0200883 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300884
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300885 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300886 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300887
Johan Hedberg861580a2014-05-20 09:45:51 +0300888 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
889 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300890
891 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300892 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300893 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300894 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300895 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800896 __le64 rand = 0;
897 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300898
Johan Hedberg943a7322014-03-18 12:58:24 +0200899 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
900 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300901
Johan Hedberge491eaf2014-10-25 21:15:37 +0200902 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300903
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300904 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700905 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300906
Johan Hedbergfff34902014-06-10 15:19:50 +0300907 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
908 auth = 1;
909 else
910 auth = 0;
911
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300912 /* Even though there's no _SLAVE suffix this is the
913 * slave STK we're adding for later lookup (the master
914 * STK never needs to be stored).
915 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700916 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300917 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300918 }
919
Johan Hedberg861580a2014-05-20 09:45:51 +0300920 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300921}
922
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300923static void smp_notify_keys(struct l2cap_conn *conn)
924{
925 struct l2cap_chan *chan = conn->smp;
926 struct smp_chan *smp = chan->data;
927 struct hci_conn *hcon = conn->hcon;
928 struct hci_dev *hdev = hcon->hdev;
929 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
930 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
931 bool persistent;
932
933 if (smp->remote_irk) {
934 mgmt_new_irk(hdev, smp->remote_irk);
935 /* Now that user space can be considered to know the
936 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300937 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300938 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300939 if (hcon->type == LE_LINK) {
940 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
941 hcon->dst_type = smp->remote_irk->addr_type;
942 queue_work(hdev->workqueue, &conn->id_addr_update_work);
943 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300944
945 /* When receiving an indentity resolving key for
946 * a remote device that does not use a resolvable
947 * private address, just remove the key so that
948 * it is possible to use the controller white
949 * list for scanning.
950 *
951 * Userspace will have been told to not store
952 * this key at this point. So it is safe to
953 * just remove it.
954 */
955 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200956 list_del_rcu(&smp->remote_irk->list);
957 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300958 smp->remote_irk = NULL;
959 }
960 }
961
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300962 if (hcon->type == ACL_LINK) {
963 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
964 persistent = false;
965 else
966 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
967 &hcon->flags);
968 } else {
969 /* The LTKs and CSRKs should be persistent only if both sides
970 * had the bonding bit set in their authentication requests.
971 */
972 persistent = !!((req->auth_req & rsp->auth_req) &
973 SMP_AUTH_BONDING);
974 }
975
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300976
977 if (smp->csrk) {
978 smp->csrk->bdaddr_type = hcon->dst_type;
979 bacpy(&smp->csrk->bdaddr, &hcon->dst);
980 mgmt_new_csrk(hdev, smp->csrk, persistent);
981 }
982
983 if (smp->slave_csrk) {
984 smp->slave_csrk->bdaddr_type = hcon->dst_type;
985 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
986 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
987 }
988
989 if (smp->ltk) {
990 smp->ltk->bdaddr_type = hcon->dst_type;
991 bacpy(&smp->ltk->bdaddr, &hcon->dst);
992 mgmt_new_ltk(hdev, smp->ltk, persistent);
993 }
994
995 if (smp->slave_ltk) {
996 smp->slave_ltk->bdaddr_type = hcon->dst_type;
997 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
998 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
999 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001000
1001 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001002 struct link_key *key;
1003 u8 type;
1004
1005 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1006 type = HCI_LK_DEBUG_COMBINATION;
1007 else if (hcon->sec_level == BT_SECURITY_FIPS)
1008 type = HCI_LK_AUTH_COMBINATION_P256;
1009 else
1010 type = HCI_LK_UNAUTH_COMBINATION_P256;
1011
1012 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1013 smp->link_key, type, 0, &persistent);
1014 if (key) {
1015 mgmt_new_link_key(hdev, key, persistent);
1016
1017 /* Don't keep debug keys around if the relevant
1018 * flag is not set.
1019 */
1020 if (!test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags) &&
1021 key->type == HCI_LK_DEBUG_COMBINATION) {
1022 list_del_rcu(&key->list);
1023 kfree_rcu(key, rcu);
1024 }
1025 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001026 }
1027}
1028
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001029static void sc_add_ltk(struct smp_chan *smp)
1030{
1031 struct hci_conn *hcon = smp->conn->hcon;
1032 u8 key_type, auth;
1033
1034 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1035 key_type = SMP_LTK_P256_DEBUG;
1036 else
1037 key_type = SMP_LTK_P256;
1038
1039 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1040 auth = 1;
1041 else
1042 auth = 0;
1043
1044 memset(smp->tk + smp->enc_key_size, 0,
1045 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
1046
1047 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1048 key_type, auth, smp->tk, smp->enc_key_size,
1049 0, 0);
1050}
1051
Johan Hedberg6a770832014-06-06 11:54:04 +03001052static void sc_generate_link_key(struct smp_chan *smp)
1053{
1054 /* These constants are as specified in the core specification.
1055 * In ASCII they spell out to 'tmp1' and 'lebr'.
1056 */
1057 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1058 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1059
1060 smp->link_key = kzalloc(16, GFP_KERNEL);
1061 if (!smp->link_key)
1062 return;
1063
1064 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1065 kfree(smp->link_key);
1066 smp->link_key = NULL;
1067 return;
1068 }
1069
1070 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
1071 kfree(smp->link_key);
1072 smp->link_key = NULL;
1073 return;
1074 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001075}
1076
Johan Hedbergb28b4942014-09-05 22:19:55 +03001077static void smp_allow_key_dist(struct smp_chan *smp)
1078{
1079 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1080 * will be allowed in each PDU handler to ensure we receive
1081 * them in the correct order.
1082 */
1083 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1084 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1085 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1086 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1087 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1088 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1089}
1090
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001091static void sc_generate_ltk(struct smp_chan *smp)
1092{
1093 /* These constants are as specified in the core specification.
1094 * In ASCII they spell out to 'tmp2' and 'brle'.
1095 */
1096 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1097 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1098 struct hci_conn *hcon = smp->conn->hcon;
1099 struct hci_dev *hdev = hcon->hdev;
1100 struct link_key *key;
1101
1102 key = hci_find_link_key(hdev, &hcon->dst);
1103 if (!key) {
1104 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1105 return;
1106 }
1107
1108 if (key->type == HCI_LK_DEBUG_COMBINATION)
1109 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1110
1111 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1112 return;
1113
1114 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1115 return;
1116
1117 sc_add_ltk(smp);
1118}
1119
Johan Hedbergd6268e82014-09-05 22:19:51 +03001120static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001121{
1122 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001123 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001124 struct hci_conn *hcon = conn->hcon;
1125 struct hci_dev *hdev = hcon->hdev;
1126 __u8 *keydist;
1127
1128 BT_DBG("conn %p", conn);
1129
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001130 rsp = (void *) &smp->prsp[1];
1131
1132 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001133 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1134 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001135 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001136 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001137
1138 req = (void *) &smp->preq[1];
1139
1140 if (hcon->out) {
1141 keydist = &rsp->init_key_dist;
1142 *keydist &= req->init_key_dist;
1143 } else {
1144 keydist = &rsp->resp_key_dist;
1145 *keydist &= req->resp_key_dist;
1146 }
1147
Johan Hedberg6a770832014-06-06 11:54:04 +03001148 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001149 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001150 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001151 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1152 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001153
1154 /* Clear the keys which are generated but not distributed */
1155 *keydist &= ~SMP_SC_NO_DIST;
1156 }
1157
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001158 BT_DBG("keydist 0x%x", *keydist);
1159
1160 if (*keydist & SMP_DIST_ENC_KEY) {
1161 struct smp_cmd_encrypt_info enc;
1162 struct smp_cmd_master_ident ident;
1163 struct smp_ltk *ltk;
1164 u8 authenticated;
1165 __le16 ediv;
1166 __le64 rand;
1167
1168 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1169 get_random_bytes(&ediv, sizeof(ediv));
1170 get_random_bytes(&rand, sizeof(rand));
1171
1172 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1173
1174 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1175 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1176 SMP_LTK_SLAVE, authenticated, enc.ltk,
1177 smp->enc_key_size, ediv, rand);
1178 smp->slave_ltk = ltk;
1179
1180 ident.ediv = ediv;
1181 ident.rand = rand;
1182
1183 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1184
1185 *keydist &= ~SMP_DIST_ENC_KEY;
1186 }
1187
1188 if (*keydist & SMP_DIST_ID_KEY) {
1189 struct smp_cmd_ident_addr_info addrinfo;
1190 struct smp_cmd_ident_info idinfo;
1191
1192 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1193
1194 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1195
1196 /* The hci_conn contains the local identity address
1197 * after the connection has been established.
1198 *
1199 * This is true even when the connection has been
1200 * established using a resolvable random address.
1201 */
1202 bacpy(&addrinfo.bdaddr, &hcon->src);
1203 addrinfo.addr_type = hcon->src_type;
1204
1205 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1206 &addrinfo);
1207
1208 *keydist &= ~SMP_DIST_ID_KEY;
1209 }
1210
1211 if (*keydist & SMP_DIST_SIGN) {
1212 struct smp_cmd_sign_info sign;
1213 struct smp_csrk *csrk;
1214
1215 /* Generate a new random key */
1216 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1217
1218 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1219 if (csrk) {
1220 csrk->master = 0x00;
1221 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1222 }
1223 smp->slave_csrk = csrk;
1224
1225 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1226
1227 *keydist &= ~SMP_DIST_SIGN;
1228 }
1229
1230 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001231 if (smp->remote_key_dist & KEY_DIST_MASK) {
1232 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001233 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001234 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001235
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001236 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1237 smp_notify_keys(conn);
1238
1239 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001240}
1241
Johan Hedbergb68fda62014-08-11 22:06:40 +03001242static void smp_timeout(struct work_struct *work)
1243{
1244 struct smp_chan *smp = container_of(work, struct smp_chan,
1245 security_timer.work);
1246 struct l2cap_conn *conn = smp->conn;
1247
1248 BT_DBG("conn %p", conn);
1249
Johan Hedberg1e91c292014-08-18 20:33:29 +03001250 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001251}
1252
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001253static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1254{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001255 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001256 struct smp_chan *smp;
1257
Marcel Holtmannf1560462013-10-13 05:43:25 -07001258 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001259 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001260 return NULL;
1261
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001262 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1263 if (IS_ERR(smp->tfm_aes)) {
1264 BT_ERR("Unable to create ECB crypto context");
1265 kfree(smp);
1266 return NULL;
1267 }
1268
Johan Hedberg407cecf2014-05-02 14:19:47 +03001269 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1270 if (IS_ERR(smp->tfm_cmac)) {
1271 BT_ERR("Unable to create CMAC crypto context");
1272 crypto_free_blkcipher(smp->tfm_aes);
1273 kfree(smp);
1274 return NULL;
1275 }
1276
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001277 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001278 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001279
Johan Hedbergb28b4942014-09-05 22:19:55 +03001280 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1281
Johan Hedbergb68fda62014-08-11 22:06:40 +03001282 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1283
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001284 hci_conn_hold(conn->hcon);
1285
1286 return smp;
1287}
1288
Johan Hedberg760b0182014-06-06 11:44:05 +03001289static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1290{
1291 struct hci_conn *hcon = smp->conn->hcon;
1292 u8 *na, *nb, a[7], b[7];
1293
1294 if (hcon->out) {
1295 na = smp->prnd;
1296 nb = smp->rrnd;
1297 } else {
1298 na = smp->rrnd;
1299 nb = smp->prnd;
1300 }
1301
1302 memcpy(a, &hcon->init_addr, 6);
1303 memcpy(b, &hcon->resp_addr, 6);
1304 a[6] = hcon->init_addr_type;
1305 b[6] = hcon->resp_addr_type;
1306
1307 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1308}
1309
Johan Hedberg38606f12014-06-25 11:10:28 +03001310static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001311{
1312 struct hci_conn *hcon = smp->conn->hcon;
1313 struct smp_cmd_dhkey_check check;
1314 u8 a[7], b[7], *local_addr, *remote_addr;
1315 u8 io_cap[3], r[16];
1316
Johan Hedberg760b0182014-06-06 11:44:05 +03001317 memcpy(a, &hcon->init_addr, 6);
1318 memcpy(b, &hcon->resp_addr, 6);
1319 a[6] = hcon->init_addr_type;
1320 b[6] = hcon->resp_addr_type;
1321
1322 if (hcon->out) {
1323 local_addr = a;
1324 remote_addr = b;
1325 memcpy(io_cap, &smp->preq[1], 3);
1326 } else {
1327 local_addr = b;
1328 remote_addr = a;
1329 memcpy(io_cap, &smp->prsp[1], 3);
1330 }
1331
Johan Hedbergdddd3052014-06-01 15:38:09 +03001332 memset(r, 0, sizeof(r));
1333
1334 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001335 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001336
1337 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1338 local_addr, remote_addr, check.e);
1339
1340 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001341}
1342
Johan Hedberg38606f12014-06-25 11:10:28 +03001343static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1344{
1345 struct l2cap_conn *conn = smp->conn;
1346 struct hci_conn *hcon = conn->hcon;
1347 struct smp_cmd_pairing_confirm cfm;
1348 u8 r;
1349
1350 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1351 r |= 0x80;
1352
1353 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1354
1355 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1356 cfm.confirm_val))
1357 return SMP_UNSPECIFIED;
1358
1359 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1360
1361 return 0;
1362}
1363
1364static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1365{
1366 struct l2cap_conn *conn = smp->conn;
1367 struct hci_conn *hcon = conn->hcon;
1368 struct hci_dev *hdev = hcon->hdev;
1369 u8 cfm[16], r;
1370
1371 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1372 if (smp->passkey_round >= 20)
1373 return 0;
1374
1375 switch (smp_op) {
1376 case SMP_CMD_PAIRING_RANDOM:
1377 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1378 r |= 0x80;
1379
1380 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1381 smp->rrnd, r, cfm))
1382 return SMP_UNSPECIFIED;
1383
1384 if (memcmp(smp->pcnf, cfm, 16))
1385 return SMP_CONFIRM_FAILED;
1386
1387 smp->passkey_round++;
1388
1389 if (smp->passkey_round == 20) {
1390 /* Generate MacKey and LTK */
1391 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1392 return SMP_UNSPECIFIED;
1393 }
1394
1395 /* The round is only complete when the initiator
1396 * receives pairing random.
1397 */
1398 if (!hcon->out) {
1399 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1400 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001401 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001402 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001403 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001404 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001405 return 0;
1406 }
1407
1408 /* Start the next round */
1409 if (smp->passkey_round != 20)
1410 return sc_passkey_round(smp, 0);
1411
1412 /* Passkey rounds are complete - start DHKey Check */
1413 sc_dhkey_check(smp);
1414 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1415
1416 break;
1417
1418 case SMP_CMD_PAIRING_CONFIRM:
1419 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1420 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1421 return 0;
1422 }
1423
1424 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1425
1426 if (hcon->out) {
1427 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1428 sizeof(smp->prnd), smp->prnd);
1429 return 0;
1430 }
1431
1432 return sc_passkey_send_confirm(smp);
1433
1434 case SMP_CMD_PUBLIC_KEY:
1435 default:
1436 /* Initiating device starts the round */
1437 if (!hcon->out)
1438 return 0;
1439
1440 BT_DBG("%s Starting passkey round %u", hdev->name,
1441 smp->passkey_round + 1);
1442
1443 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1444
1445 return sc_passkey_send_confirm(smp);
1446 }
1447
1448 return 0;
1449}
1450
Johan Hedbergdddd3052014-06-01 15:38:09 +03001451static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1452{
Johan Hedberg38606f12014-06-25 11:10:28 +03001453 struct l2cap_conn *conn = smp->conn;
1454 struct hci_conn *hcon = conn->hcon;
1455 u8 smp_op;
1456
1457 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1458
Johan Hedbergdddd3052014-06-01 15:38:09 +03001459 switch (mgmt_op) {
1460 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1461 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1462 return 0;
1463 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1464 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1465 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001466 case MGMT_OP_USER_PASSKEY_REPLY:
1467 hcon->passkey_notify = le32_to_cpu(passkey);
1468 smp->passkey_round = 0;
1469
1470 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1471 smp_op = SMP_CMD_PAIRING_CONFIRM;
1472 else
1473 smp_op = 0;
1474
1475 if (sc_passkey_round(smp, smp_op))
1476 return -EIO;
1477
1478 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001479 }
1480
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001481 /* Initiator sends DHKey check first */
1482 if (hcon->out) {
1483 sc_dhkey_check(smp);
1484 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1485 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1486 sc_dhkey_check(smp);
1487 sc_add_ltk(smp);
1488 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001489
1490 return 0;
1491}
1492
Brian Gix2b64d152011-12-21 16:12:12 -08001493int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1494{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001495 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001496 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001497 struct smp_chan *smp;
1498 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001499 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001500
1501 BT_DBG("");
1502
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001503 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001504 return -ENOTCONN;
1505
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001506 chan = conn->smp;
1507 if (!chan)
1508 return -ENOTCONN;
1509
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001510 l2cap_chan_lock(chan);
1511 if (!chan->data) {
1512 err = -ENOTCONN;
1513 goto unlock;
1514 }
1515
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001516 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001517
Johan Hedberg760b0182014-06-06 11:44:05 +03001518 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1519 err = sc_user_reply(smp, mgmt_op, passkey);
1520 goto unlock;
1521 }
1522
Brian Gix2b64d152011-12-21 16:12:12 -08001523 switch (mgmt_op) {
1524 case MGMT_OP_USER_PASSKEY_REPLY:
1525 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001526 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001527 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001528 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001529 /* Fall Through */
1530 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001531 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001532 break;
1533 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1534 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001535 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001536 err = 0;
1537 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001538 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001539 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001540 err = -EOPNOTSUPP;
1541 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001542 }
1543
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001544 err = 0;
1545
Brian Gix2b64d152011-12-21 16:12:12 -08001546 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001547 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1548 u8 rsp = smp_confirm(smp);
1549 if (rsp)
1550 smp_failure(conn, rsp);
1551 }
Brian Gix2b64d152011-12-21 16:12:12 -08001552
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001553unlock:
1554 l2cap_chan_unlock(chan);
1555 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001556}
1557
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001558static void build_bredr_pairing_cmd(struct smp_chan *smp,
1559 struct smp_cmd_pairing *req,
1560 struct smp_cmd_pairing *rsp)
1561{
1562 struct l2cap_conn *conn = smp->conn;
1563 struct hci_dev *hdev = conn->hcon->hdev;
1564 u8 local_dist = 0, remote_dist = 0;
1565
1566 if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) {
1567 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1568 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1569 }
1570
1571 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
1572 remote_dist |= SMP_DIST_ID_KEY;
1573
1574 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
1575 local_dist |= SMP_DIST_ID_KEY;
1576
1577 if (!rsp) {
1578 memset(req, 0, sizeof(*req));
1579
1580 req->init_key_dist = local_dist;
1581 req->resp_key_dist = remote_dist;
1582 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1583
1584 smp->remote_key_dist = remote_dist;
1585
1586 return;
1587 }
1588
1589 memset(rsp, 0, sizeof(*rsp));
1590
1591 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1592 rsp->init_key_dist = req->init_key_dist & remote_dist;
1593 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1594
1595 smp->remote_key_dist = rsp->init_key_dist;
1596}
1597
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001598static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001599{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001600 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001601 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001602 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001603 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001604 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001605 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001606
1607 BT_DBG("conn %p", conn);
1608
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001609 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001610 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001611
Johan Hedberg40bef302014-07-16 11:42:27 +03001612 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001613 return SMP_CMD_NOTSUPP;
1614
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001615 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001616 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001617 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001618 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001619
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001620 if (!smp)
1621 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001622
Johan Hedbergc05b9332014-09-10 17:37:42 -07001623 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001624 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001625
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001626 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001627 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001628 return SMP_PAIRING_NOTSUPP;
1629
Johan Hedberg903b71c2014-09-08 16:59:18 -07001630 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1631 return SMP_AUTH_REQUIREMENTS;
1632
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001633 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1634 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001635 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001636
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001637 /* SMP over BR/EDR requires special treatment */
1638 if (conn->hcon->type == ACL_LINK) {
1639 /* We must have a BR/EDR SC link */
1640 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags))
1641 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1642
1643 set_bit(SMP_FLAG_SC, &smp->flags);
1644
1645 build_bredr_pairing_cmd(smp, req, &rsp);
1646
1647 key_size = min(req->max_key_size, rsp.max_key_size);
1648 if (check_enc_key_size(conn, key_size))
1649 return SMP_ENC_KEY_SIZE;
1650
1651 /* Clear bits which are generated but not distributed */
1652 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1653
1654 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1655 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1656 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1657
1658 smp_distribute_keys(smp);
1659 return 0;
1660 }
1661
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001662 build_pairing_cmd(conn, req, &rsp, auth);
1663
1664 if (rsp.auth_req & SMP_AUTH_SC)
1665 set_bit(SMP_FLAG_SC, &smp->flags);
1666
Johan Hedberg5be5e272014-09-10 17:58:54 -07001667 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001668 sec_level = BT_SECURITY_MEDIUM;
1669 else
1670 sec_level = authreq_to_seclevel(auth);
1671
Johan Hedbergc7262e72014-06-17 13:07:37 +03001672 if (sec_level > conn->hcon->pending_sec_level)
1673 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001674
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001675 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001676 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1677 u8 method;
1678
1679 method = get_auth_method(smp, conn->hcon->io_capability,
1680 req->io_capability);
1681 if (method == JUST_WORKS || method == JUST_CFM)
1682 return SMP_AUTH_REQUIREMENTS;
1683 }
1684
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001685 key_size = min(req->max_key_size, rsp.max_key_size);
1686 if (check_enc_key_size(conn, key_size))
1687 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001688
Johan Hedberge84a6b12013-12-02 10:49:03 +02001689 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001690
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001691 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1692 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001693
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001694 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001695
1696 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1697
1698 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1699 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1700 /* Clear bits which are generated but not distributed */
1701 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1702 /* Wait for Public Key from Initiating Device */
1703 return 0;
1704 } else {
1705 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1706 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001707
Brian Gix2b64d152011-12-21 16:12:12 -08001708 /* Request setup of TK */
1709 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1710 if (ret)
1711 return SMP_UNSPECIFIED;
1712
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001713 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001714}
1715
Johan Hedberg3b191462014-06-06 10:50:15 +03001716static u8 sc_send_public_key(struct smp_chan *smp)
1717{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001718 struct hci_dev *hdev = smp->conn->hcon->hdev;
1719
Johan Hedberg3b191462014-06-06 10:50:15 +03001720 BT_DBG("");
1721
Johan Hedberg70157ef2014-06-24 15:22:59 +03001722 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) {
1723 BT_DBG("Using debug keys");
1724 memcpy(smp->local_pk, debug_pk, 64);
1725 memcpy(smp->local_sk, debug_sk, 32);
1726 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1727 } else {
1728 while (true) {
1729 /* Generate local key pair for Secure Connections */
1730 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1731 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001732
Johan Hedberg70157ef2014-06-24 15:22:59 +03001733 /* This is unlikely, but we need to check that
1734 * we didn't accidentially generate a debug key.
1735 */
1736 if (memcmp(smp->local_sk, debug_sk, 32))
1737 break;
1738 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001739 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001740
1741 BT_DBG("Local Public Key X: %32phN", smp->local_pk);
1742 BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1743 BT_DBG("Local Private Key: %32phN", smp->local_sk);
1744
1745 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1746
1747 return 0;
1748}
1749
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001750static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001751{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001752 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001753 struct l2cap_chan *chan = conn->smp;
1754 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001755 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001756 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001757 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001758
1759 BT_DBG("conn %p", conn);
1760
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001761 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001762 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001763
Johan Hedberg40bef302014-07-16 11:42:27 +03001764 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001765 return SMP_CMD_NOTSUPP;
1766
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001767 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001768
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001769 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001770
1771 key_size = min(req->max_key_size, rsp->max_key_size);
1772 if (check_enc_key_size(conn, key_size))
1773 return SMP_ENC_KEY_SIZE;
1774
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001775 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001776
Johan Hedberg903b71c2014-09-08 16:59:18 -07001777 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1778 return SMP_AUTH_REQUIREMENTS;
1779
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001780 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1781 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1782
1783 /* Update remote key distribution in case the remote cleared
1784 * some bits that we had enabled in our request.
1785 */
1786 smp->remote_key_dist &= rsp->resp_key_dist;
1787
1788 /* For BR/EDR this means we're done and can start phase 3 */
1789 if (conn->hcon->type == ACL_LINK) {
1790 /* Clear bits which are generated but not distributed */
1791 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1792 smp_distribute_keys(smp);
1793 return 0;
1794 }
1795
Johan Hedberg65668772014-05-16 11:03:34 +03001796 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1797 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001798 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1799 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001800
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001801 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001802 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1803 u8 method;
1804
1805 method = get_auth_method(smp, req->io_capability,
1806 rsp->io_capability);
1807 if (method == JUST_WORKS || method == JUST_CFM)
1808 return SMP_AUTH_REQUIREMENTS;
1809 }
1810
Johan Hedberge84a6b12013-12-02 10:49:03 +02001811 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001812
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001813 /* Update remote key distribution in case the remote cleared
1814 * some bits that we had enabled in our request.
1815 */
1816 smp->remote_key_dist &= rsp->resp_key_dist;
1817
Johan Hedberg3b191462014-06-06 10:50:15 +03001818 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1819 /* Clear bits which are generated but not distributed */
1820 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1821 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1822 return sc_send_public_key(smp);
1823 }
1824
Johan Hedbergc05b9332014-09-10 17:37:42 -07001825 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001826
Johan Hedberg476585e2012-06-06 18:54:15 +08001827 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001828 if (ret)
1829 return SMP_UNSPECIFIED;
1830
Johan Hedberg4a74d652014-05-20 09:45:50 +03001831 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001832
1833 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001834 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001835 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001836
1837 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001838}
1839
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001840static u8 sc_check_confirm(struct smp_chan *smp)
1841{
1842 struct l2cap_conn *conn = smp->conn;
1843
1844 BT_DBG("");
1845
1846 /* Public Key exchange must happen before any other steps */
1847 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1848 return SMP_UNSPECIFIED;
1849
Johan Hedberg38606f12014-06-25 11:10:28 +03001850 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1851 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1852
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001853 if (conn->hcon->out) {
1854 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1855 smp->prnd);
1856 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1857 }
1858
1859 return 0;
1860}
1861
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001862static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001863{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001864 struct l2cap_chan *chan = conn->smp;
1865 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001866
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001867 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1868
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001869 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001870 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001871
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001872 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1873 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001874
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001875 if (test_bit(SMP_FLAG_SC, &smp->flags))
1876 return sc_check_confirm(smp);
1877
Johan Hedbergb28b4942014-09-05 22:19:55 +03001878 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001879 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1880 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001881 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1882 return 0;
1883 }
1884
1885 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001886 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001887 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001888 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001889
1890 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001891}
1892
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001893static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001894{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001895 struct l2cap_chan *chan = conn->smp;
1896 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03001897 struct hci_conn *hcon = conn->hcon;
1898 u8 *pkax, *pkbx, *na, *nb;
1899 u32 passkey;
1900 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001901
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001902 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001903
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001904 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001905 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001906
Johan Hedberg943a7322014-03-18 12:58:24 +02001907 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001908 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001909
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03001910 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1911 return smp_random(smp);
1912
Johan Hedberg38606f12014-06-25 11:10:28 +03001913 /* Passkey entry has special treatment */
1914 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1915 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
1916
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03001917 if (hcon->out) {
1918 u8 cfm[16];
1919
1920 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1921 smp->rrnd, 0, cfm);
1922 if (err)
1923 return SMP_UNSPECIFIED;
1924
1925 if (memcmp(smp->pcnf, cfm, 16))
1926 return SMP_CONFIRM_FAILED;
1927
1928 pkax = smp->local_pk;
1929 pkbx = smp->remote_pk;
1930 na = smp->prnd;
1931 nb = smp->rrnd;
1932 } else {
1933 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1934 smp->prnd);
1935 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1936
1937 pkax = smp->remote_pk;
1938 pkbx = smp->local_pk;
1939 na = smp->rrnd;
1940 nb = smp->prnd;
1941 }
1942
Johan Hedberg760b0182014-06-06 11:44:05 +03001943 /* Generate MacKey and LTK */
1944 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1945 if (err)
1946 return SMP_UNSPECIFIED;
1947
Johan Hedbergdddd3052014-06-01 15:38:09 +03001948 if (smp->method == JUST_WORKS) {
1949 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03001950 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001951 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1952 }
1953 return 0;
1954 }
1955
Johan Hedberg38606f12014-06-25 11:10:28 +03001956 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03001957 if (err)
1958 return SMP_UNSPECIFIED;
1959
Johan Hedberg38606f12014-06-25 11:10:28 +03001960 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
1961 hcon->dst_type, passkey, 0);
1962 if (err)
1963 return SMP_UNSPECIFIED;
1964
1965 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1966
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03001967 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001968}
1969
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001970static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001971{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001972 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001973 struct hci_conn *hcon = conn->hcon;
1974
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001975 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001976 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001977 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001978
Johan Hedberga6f78332014-09-10 17:37:45 -07001979 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001980 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001981
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001982 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001983 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001984
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001985 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1986 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001987
Johan Hedbergfe59a052014-07-01 19:14:12 +03001988 /* We never store STKs for master role, so clear this flag */
1989 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1990
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001991 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001992}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001993
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001994bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1995 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03001996{
1997 if (sec_level == BT_SECURITY_LOW)
1998 return true;
1999
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002000 /* If we're encrypted with an STK but the caller prefers using
2001 * LTK claim insufficient security. This way we allow the
2002 * connection to be re-encrypted with an LTK, even if the LTK
2003 * provides the same level of security. Only exception is if we
2004 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002005 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002006 if (key_pref == SMP_USE_LTK &&
2007 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002008 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002009 return false;
2010
Johan Hedberg854f4722014-07-01 18:40:20 +03002011 if (hcon->sec_level >= sec_level)
2012 return true;
2013
2014 return false;
2015}
2016
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002017static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002018{
2019 struct smp_cmd_security_req *rp = (void *) skb->data;
2020 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002021 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002022 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002023 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002024 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002025
2026 BT_DBG("conn %p", conn);
2027
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002028 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002029 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002030
Johan Hedberg40bef302014-07-16 11:42:27 +03002031 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002032 return SMP_CMD_NOTSUPP;
2033
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002034 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002035
Johan Hedberg903b71c2014-09-08 16:59:18 -07002036 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
2037 return SMP_AUTH_REQUIREMENTS;
2038
Johan Hedberg5be5e272014-09-10 17:58:54 -07002039 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002040 sec_level = BT_SECURITY_MEDIUM;
2041 else
2042 sec_level = authreq_to_seclevel(auth);
2043
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002044 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002045 return 0;
2046
Johan Hedbergc7262e72014-06-17 13:07:37 +03002047 if (sec_level > hcon->pending_sec_level)
2048 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002049
Johan Hedberg4dab7862012-06-07 14:58:37 +08002050 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002051 return 0;
2052
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002053 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002054 if (!smp)
2055 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002056
Johan Hedbergb6ae8452014-07-30 09:22:22 +03002057 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002058 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55be42014-07-29 14:18:48 +03002059 return SMP_PAIRING_NOTSUPP;
2060
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002061 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002062
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002063 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002064 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002065
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002066 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2067 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002068
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002069 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002070 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002071
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002072 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002073}
2074
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002075int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002076{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002077 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002078 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002079 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002080 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002081 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002082
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002083 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2084
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002085 /* This may be NULL if there's an unexpected disconnection */
2086 if (!conn)
2087 return 1;
2088
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002089 chan = conn->smp;
2090
Johan Hedberg757aee02013-04-24 13:05:32 +03002091 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002092 return 1;
2093
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002094 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002095 return 1;
2096
Johan Hedbergc7262e72014-06-17 13:07:37 +03002097 if (sec_level > hcon->pending_sec_level)
2098 hcon->pending_sec_level = sec_level;
2099
Johan Hedberg40bef302014-07-16 11:42:27 +03002100 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002101 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2102 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002103
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002104 l2cap_chan_lock(chan);
2105
2106 /* If SMP is already in progress ignore this request */
2107 if (chan->data) {
2108 ret = 0;
2109 goto unlock;
2110 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002111
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002112 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002113 if (!smp) {
2114 ret = 1;
2115 goto unlock;
2116 }
Brian Gix2b64d152011-12-21 16:12:12 -08002117
2118 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002119
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002120 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
2121 authreq |= SMP_AUTH_SC;
2122
Johan Hedberg79897d22014-06-01 09:45:24 +03002123 /* Require MITM if IO Capability allows or the security level
2124 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002125 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002126 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002127 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002128 authreq |= SMP_AUTH_MITM;
2129
Johan Hedberg40bef302014-07-16 11:42:27 +03002130 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002131 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002132
Brian Gix2b64d152011-12-21 16:12:12 -08002133 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002134 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2135 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002136
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002137 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002138 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002139 } else {
2140 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002141 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002142 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002143 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002144 }
2145
Johan Hedberg4a74d652014-05-20 09:45:50 +03002146 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002147 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002148
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002149unlock:
2150 l2cap_chan_unlock(chan);
2151 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002152}
2153
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002154static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2155{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002156 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002157 struct l2cap_chan *chan = conn->smp;
2158 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002159
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002160 BT_DBG("conn %p", conn);
2161
2162 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002163 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002164
Johan Hedbergb28b4942014-09-05 22:19:55 +03002165 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002166
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002167 skb_pull(skb, sizeof(*rp));
2168
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002169 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002170
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002171 return 0;
2172}
2173
2174static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2175{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002176 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002177 struct l2cap_chan *chan = conn->smp;
2178 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002179 struct hci_dev *hdev = conn->hcon->hdev;
2180 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002181 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002182 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002183
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002184 BT_DBG("conn %p", conn);
2185
2186 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002187 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002188
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002189 /* Mark the information as received */
2190 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2191
Johan Hedbergb28b4942014-09-05 22:19:55 +03002192 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2193 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002194 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2195 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002196
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002197 skb_pull(skb, sizeof(*rp));
2198
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002199 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002200 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002201 authenticated, smp->tk, smp->enc_key_size,
2202 rp->ediv, rp->rand);
2203 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002204 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002205 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002206
2207 return 0;
2208}
2209
Johan Hedbergfd349c02014-02-18 10:19:36 +02002210static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2211{
2212 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002213 struct l2cap_chan *chan = conn->smp;
2214 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002215
2216 BT_DBG("");
2217
2218 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002219 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002220
Johan Hedbergb28b4942014-09-05 22:19:55 +03002221 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002222
Johan Hedbergfd349c02014-02-18 10:19:36 +02002223 skb_pull(skb, sizeof(*info));
2224
2225 memcpy(smp->irk, info->irk, 16);
2226
2227 return 0;
2228}
2229
2230static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2231 struct sk_buff *skb)
2232{
2233 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002234 struct l2cap_chan *chan = conn->smp;
2235 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002236 struct hci_conn *hcon = conn->hcon;
2237 bdaddr_t rpa;
2238
2239 BT_DBG("");
2240
2241 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002242 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002243
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002244 /* Mark the information as received */
2245 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2246
Johan Hedbergb28b4942014-09-05 22:19:55 +03002247 if (smp->remote_key_dist & SMP_DIST_SIGN)
2248 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2249
Johan Hedbergfd349c02014-02-18 10:19:36 +02002250 skb_pull(skb, sizeof(*info));
2251
Johan Hedberga9a58f82014-02-25 22:24:37 +02002252 /* Strictly speaking the Core Specification (4.1) allows sending
2253 * an empty address which would force us to rely on just the IRK
2254 * as "identity information". However, since such
2255 * implementations are not known of and in order to not over
2256 * complicate our implementation, simply pretend that we never
2257 * received an IRK for such a device.
2258 */
2259 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
2260 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002261 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002262 }
2263
Johan Hedbergfd349c02014-02-18 10:19:36 +02002264 bacpy(&smp->id_addr, &info->bdaddr);
2265 smp->id_addr_type = info->addr_type;
2266
2267 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2268 bacpy(&rpa, &hcon->dst);
2269 else
2270 bacpy(&rpa, BDADDR_ANY);
2271
Johan Hedberg23d0e122014-02-19 14:57:46 +02002272 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2273 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002274
Johan Hedberg31dd6242014-06-27 14:23:02 +03002275distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002276 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2277 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002278
2279 return 0;
2280}
2281
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002282static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2283{
2284 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002285 struct l2cap_chan *chan = conn->smp;
2286 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002287 struct smp_csrk *csrk;
2288
2289 BT_DBG("conn %p", conn);
2290
2291 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002292 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002293
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002294 /* Mark the information as received */
2295 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2296
2297 skb_pull(skb, sizeof(*rp));
2298
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002299 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2300 if (csrk) {
2301 csrk->master = 0x01;
2302 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2303 }
2304 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002305 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002306
2307 return 0;
2308}
2309
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002310static u8 sc_select_method(struct smp_chan *smp)
2311{
2312 struct l2cap_conn *conn = smp->conn;
2313 struct hci_conn *hcon = conn->hcon;
2314 struct smp_cmd_pairing *local, *remote;
2315 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2316
2317 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2318 * which are needed as inputs to some crypto functions. To get
2319 * the "struct smp_cmd_pairing" from them we need to skip the
2320 * first byte which contains the opcode.
2321 */
2322 if (hcon->out) {
2323 local = (void *) &smp->preq[1];
2324 remote = (void *) &smp->prsp[1];
2325 } else {
2326 local = (void *) &smp->prsp[1];
2327 remote = (void *) &smp->preq[1];
2328 }
2329
2330 local_io = local->io_capability;
2331 remote_io = remote->io_capability;
2332
2333 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2334 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2335
2336 /* If either side wants MITM, look up the method from the table,
2337 * otherwise use JUST WORKS.
2338 */
2339 if (local_mitm || remote_mitm)
2340 method = get_auth_method(smp, local_io, remote_io);
2341 else
2342 method = JUST_WORKS;
2343
2344 /* Don't confirm locally initiated pairing attempts */
2345 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2346 method = JUST_WORKS;
2347
2348 return method;
2349}
2350
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002351static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2352{
2353 struct smp_cmd_public_key *key = (void *) skb->data;
2354 struct hci_conn *hcon = conn->hcon;
2355 struct l2cap_chan *chan = conn->smp;
2356 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002357 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002358 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002359 int err;
2360
2361 BT_DBG("conn %p", conn);
2362
2363 if (skb->len < sizeof(*key))
2364 return SMP_INVALID_PARAMS;
2365
2366 memcpy(smp->remote_pk, key, 64);
2367
2368 /* Non-initiating device sends its public key after receiving
2369 * the key from the initiating device.
2370 */
2371 if (!hcon->out) {
2372 err = sc_send_public_key(smp);
2373 if (err)
2374 return err;
2375 }
2376
2377 BT_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2378 BT_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
2379
2380 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2381 return SMP_UNSPECIFIED;
2382
2383 BT_DBG("DHKey %32phN", smp->dhkey);
2384
2385 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2386
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002387 smp->method = sc_select_method(smp);
2388
2389 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2390
2391 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2392 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2393 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2394 else
2395 hcon->pending_sec_level = BT_SECURITY_FIPS;
2396
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002397 if (!memcmp(debug_pk, smp->remote_pk, 64))
2398 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2399
Johan Hedberg38606f12014-06-25 11:10:28 +03002400 if (smp->method == DSP_PASSKEY) {
2401 get_random_bytes(&hcon->passkey_notify,
2402 sizeof(hcon->passkey_notify));
2403 hcon->passkey_notify %= 1000000;
2404 hcon->passkey_entered = 0;
2405 smp->passkey_round = 0;
2406 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2407 hcon->dst_type,
2408 hcon->passkey_notify,
2409 hcon->passkey_entered))
2410 return SMP_UNSPECIFIED;
2411 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2412 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2413 }
2414
2415 if (hcon->out)
2416 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2417
2418 if (smp->method == REQ_PASSKEY) {
2419 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2420 hcon->dst_type))
2421 return SMP_UNSPECIFIED;
2422 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2423 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2424 return 0;
2425 }
2426
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002427 /* The Initiating device waits for the non-initiating device to
2428 * send the confirm value.
2429 */
2430 if (conn->hcon->out)
2431 return 0;
2432
2433 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2434 0, cfm.confirm_val);
2435 if (err)
2436 return SMP_UNSPECIFIED;
2437
2438 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2439 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2440
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002441 return 0;
2442}
2443
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002444static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2445{
2446 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2447 struct l2cap_chan *chan = conn->smp;
2448 struct hci_conn *hcon = conn->hcon;
2449 struct smp_chan *smp = chan->data;
2450 u8 a[7], b[7], *local_addr, *remote_addr;
2451 u8 io_cap[3], r[16], e[16];
2452 int err;
2453
2454 BT_DBG("conn %p", conn);
2455
2456 if (skb->len < sizeof(*check))
2457 return SMP_INVALID_PARAMS;
2458
2459 memcpy(a, &hcon->init_addr, 6);
2460 memcpy(b, &hcon->resp_addr, 6);
2461 a[6] = hcon->init_addr_type;
2462 b[6] = hcon->resp_addr_type;
2463
2464 if (hcon->out) {
2465 local_addr = a;
2466 remote_addr = b;
2467 memcpy(io_cap, &smp->prsp[1], 3);
2468 } else {
2469 local_addr = b;
2470 remote_addr = a;
2471 memcpy(io_cap, &smp->preq[1], 3);
2472 }
2473
2474 memset(r, 0, sizeof(r));
2475
Johan Hedberg38606f12014-06-25 11:10:28 +03002476 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2477 put_unaligned_le32(hcon->passkey_notify, r);
2478
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002479 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2480 io_cap, remote_addr, local_addr, e);
2481 if (err)
2482 return SMP_UNSPECIFIED;
2483
2484 if (memcmp(check->e, e, 16))
2485 return SMP_DHKEY_CHECK_FAILED;
2486
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002487 if (!hcon->out) {
2488 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2489 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2490 return 0;
2491 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002492
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002493 /* Slave sends DHKey check as response to master */
2494 sc_dhkey_check(smp);
2495 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002496
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002497 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002498
2499 if (hcon->out) {
2500 hci_le_start_enc(hcon, 0, 0, smp->tk);
2501 hcon->enc_key_size = smp->enc_key_size;
2502 }
2503
2504 return 0;
2505}
2506
Johan Hedberg1408bb62014-06-04 22:45:57 +03002507static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2508 struct sk_buff *skb)
2509{
2510 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2511
2512 BT_DBG("value 0x%02x", kp->value);
2513
2514 return 0;
2515}
2516
Johan Hedberg4befb862014-08-11 22:06:38 +03002517static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002518{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002519 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002520 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002521 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002522 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002523 int err = 0;
2524
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002525 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002526 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002527
Marcel Holtmann06ae3312013-10-18 03:43:00 -07002528 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002529 reason = SMP_PAIRING_NOTSUPP;
2530 goto done;
2531 }
2532
Marcel Holtmann92381f52013-10-03 01:23:08 -07002533 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002534 skb_pull(skb, sizeof(code));
2535
Johan Hedbergb28b4942014-09-05 22:19:55 +03002536 smp = chan->data;
2537
2538 if (code > SMP_CMD_MAX)
2539 goto drop;
2540
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002541 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002542 goto drop;
2543
2544 /* If we don't have a context the only allowed commands are
2545 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002546 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002547 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2548 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002549
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002550 switch (code) {
2551 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002552 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002553 break;
2554
2555 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002556 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002557 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002558 break;
2559
2560 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002561 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002562 break;
2563
2564 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002565 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002566 break;
2567
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002568 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002569 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002570 break;
2571
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002572 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002573 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002574 break;
2575
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002576 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002577 reason = smp_cmd_encrypt_info(conn, skb);
2578 break;
2579
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002580 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002581 reason = smp_cmd_master_ident(conn, skb);
2582 break;
2583
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002584 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002585 reason = smp_cmd_ident_info(conn, skb);
2586 break;
2587
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002588 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002589 reason = smp_cmd_ident_addr_info(conn, skb);
2590 break;
2591
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002592 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002593 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002594 break;
2595
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002596 case SMP_CMD_PUBLIC_KEY:
2597 reason = smp_cmd_public_key(conn, skb);
2598 break;
2599
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002600 case SMP_CMD_DHKEY_CHECK:
2601 reason = smp_cmd_dhkey_check(conn, skb);
2602 break;
2603
Johan Hedberg1408bb62014-06-04 22:45:57 +03002604 case SMP_CMD_KEYPRESS_NOTIFY:
2605 reason = smp_cmd_keypress_notify(conn, skb);
2606 break;
2607
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002608 default:
2609 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002610 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002611 goto done;
2612 }
2613
2614done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002615 if (!err) {
2616 if (reason)
2617 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002618 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002619 }
2620
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002621 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002622
2623drop:
2624 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2625 code, &hcon->dst);
2626 kfree_skb(skb);
2627 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002628}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002629
Johan Hedberg70db83c2014-08-08 09:37:16 +03002630static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2631{
2632 struct l2cap_conn *conn = chan->conn;
2633
2634 BT_DBG("chan %p", chan);
2635
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002636 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002637 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002638
Johan Hedberg70db83c2014-08-08 09:37:16 +03002639 conn->smp = NULL;
2640 l2cap_chan_put(chan);
2641}
2642
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002643static void bredr_pairing(struct l2cap_chan *chan)
2644{
2645 struct l2cap_conn *conn = chan->conn;
2646 struct hci_conn *hcon = conn->hcon;
2647 struct hci_dev *hdev = hcon->hdev;
2648 struct smp_cmd_pairing req;
2649 struct smp_chan *smp;
2650
2651 BT_DBG("chan %p", chan);
2652
2653 /* Only new pairings are interesting */
2654 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2655 return;
2656
2657 /* Don't bother if we're not encrypted */
2658 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2659 return;
2660
2661 /* Only master may initiate SMP over BR/EDR */
2662 if (hcon->role != HCI_ROLE_MASTER)
2663 return;
2664
2665 /* Secure Connections support must be enabled */
2666 if (!test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
2667 return;
2668
2669 /* BR/EDR must use Secure Connections for SMP */
2670 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
2671 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2672 return;
2673
2674 /* If our LE support is not enabled don't do anything */
2675 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
2676 return;
2677
2678 /* Don't bother if remote LE support is not enabled */
2679 if (!lmp_host_le_capable(hcon))
2680 return;
2681
2682 /* Remote must support SMP fixed chan for BR/EDR */
2683 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2684 return;
2685
2686 /* Don't bother if SMP is already ongoing */
2687 if (chan->data)
2688 return;
2689
2690 smp = smp_chan_create(conn);
2691 if (!smp) {
2692 BT_ERR("%s unable to create SMP context for BR/EDR",
2693 hdev->name);
2694 return;
2695 }
2696
2697 set_bit(SMP_FLAG_SC, &smp->flags);
2698
2699 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2700
2701 /* Prepare and send the BR/EDR SMP Pairing Request */
2702 build_bredr_pairing_cmd(smp, &req, NULL);
2703
2704 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2705 memcpy(&smp->preq[1], &req, sizeof(req));
2706
2707 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2708 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2709}
2710
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002711static void smp_resume_cb(struct l2cap_chan *chan)
2712{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002713 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002714 struct l2cap_conn *conn = chan->conn;
2715 struct hci_conn *hcon = conn->hcon;
2716
2717 BT_DBG("chan %p", chan);
2718
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002719 if (hcon->type == ACL_LINK) {
2720 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002721 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002722 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002723
Johan Hedberg86d14072014-08-11 22:06:43 +03002724 if (!smp)
2725 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002726
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002727 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2728 return;
2729
Johan Hedberg86d14072014-08-11 22:06:43 +03002730 cancel_delayed_work(&smp->security_timer);
2731
Johan Hedbergd6268e82014-09-05 22:19:51 +03002732 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002733}
2734
Johan Hedberg70db83c2014-08-08 09:37:16 +03002735static void smp_ready_cb(struct l2cap_chan *chan)
2736{
2737 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002738 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002739
2740 BT_DBG("chan %p", chan);
2741
2742 conn->smp = chan;
2743 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002744
2745 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2746 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002747}
2748
Johan Hedberg4befb862014-08-11 22:06:38 +03002749static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2750{
2751 int err;
2752
2753 BT_DBG("chan %p", chan);
2754
2755 err = smp_sig_channel(chan, skb);
2756 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03002757 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03002758
Johan Hedbergb68fda62014-08-11 22:06:40 +03002759 if (smp)
2760 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03002761
Johan Hedberg1e91c292014-08-18 20:33:29 +03002762 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03002763 }
2764
2765 return err;
2766}
2767
Johan Hedberg70db83c2014-08-08 09:37:16 +03002768static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2769 unsigned long hdr_len,
2770 unsigned long len, int nb)
2771{
2772 struct sk_buff *skb;
2773
2774 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2775 if (!skb)
2776 return ERR_PTR(-ENOMEM);
2777
2778 skb->priority = HCI_PRIO_MAX;
2779 bt_cb(skb)->chan = chan;
2780
2781 return skb;
2782}
2783
2784static const struct l2cap_ops smp_chan_ops = {
2785 .name = "Security Manager",
2786 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002787 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002788 .alloc_skb = smp_alloc_skb_cb,
2789 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002790 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002791
2792 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002793 .state_change = l2cap_chan_no_state_change,
2794 .close = l2cap_chan_no_close,
2795 .defer = l2cap_chan_no_defer,
2796 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002797 .set_shutdown = l2cap_chan_no_set_shutdown,
2798 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2799 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2800};
2801
2802static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2803{
2804 struct l2cap_chan *chan;
2805
2806 BT_DBG("pchan %p", pchan);
2807
2808 chan = l2cap_chan_create();
2809 if (!chan)
2810 return NULL;
2811
2812 chan->chan_type = pchan->chan_type;
2813 chan->ops = &smp_chan_ops;
2814 chan->scid = pchan->scid;
2815 chan->dcid = chan->scid;
2816 chan->imtu = pchan->imtu;
2817 chan->omtu = pchan->omtu;
2818 chan->mode = pchan->mode;
2819
Johan Hedbergabe84902014-11-12 22:22:21 +02002820 /* Other L2CAP channels may request SMP routines in order to
2821 * change the security level. This means that the SMP channel
2822 * lock must be considered in its own category to avoid lockdep
2823 * warnings.
2824 */
2825 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2826
Johan Hedberg70db83c2014-08-08 09:37:16 +03002827 BT_DBG("created chan %p", chan);
2828
2829 return chan;
2830}
2831
2832static const struct l2cap_ops smp_root_chan_ops = {
2833 .name = "Security Manager Root",
2834 .new_connection = smp_new_conn_cb,
2835
2836 /* None of these are implemented for the root channel */
2837 .close = l2cap_chan_no_close,
2838 .alloc_skb = l2cap_chan_no_alloc_skb,
2839 .recv = l2cap_chan_no_recv,
2840 .state_change = l2cap_chan_no_state_change,
2841 .teardown = l2cap_chan_no_teardown,
2842 .ready = l2cap_chan_no_ready,
2843 .defer = l2cap_chan_no_defer,
2844 .suspend = l2cap_chan_no_suspend,
2845 .resume = l2cap_chan_no_resume,
2846 .set_shutdown = l2cap_chan_no_set_shutdown,
2847 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2848 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2849};
2850
Johan Hedbergef8efe42014-08-13 15:12:32 +03002851static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002852{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002853 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002854 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002855
Johan Hedbergef8efe42014-08-13 15:12:32 +03002856 if (cid == L2CAP_CID_SMP_BREDR) {
2857 tfm_aes = NULL;
2858 goto create_chan;
2859 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03002860
Johan Hedbergadae20c2014-11-13 14:37:48 +02002861 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002862 if (IS_ERR(tfm_aes)) {
Johan Hedberg711eafe2014-08-08 09:32:52 +03002863 BT_ERR("Unable to create crypto context");
Johan Hedbergef8efe42014-08-13 15:12:32 +03002864 return ERR_PTR(PTR_ERR(tfm_aes));
Johan Hedberg711eafe2014-08-08 09:32:52 +03002865 }
2866
Johan Hedbergef8efe42014-08-13 15:12:32 +03002867create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03002868 chan = l2cap_chan_create();
2869 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002870 crypto_free_blkcipher(tfm_aes);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002871 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002872 }
2873
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002874 chan->data = tfm_aes;
2875
Johan Hedbergef8efe42014-08-13 15:12:32 +03002876 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002877
2878 l2cap_chan_set_defaults(chan);
2879
2880 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002881 if (cid == L2CAP_CID_SMP)
2882 chan->src_type = BDADDR_LE_PUBLIC;
2883 else
2884 chan->src_type = BDADDR_BREDR;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002885 chan->state = BT_LISTEN;
2886 chan->mode = L2CAP_MODE_BASIC;
2887 chan->imtu = L2CAP_DEFAULT_MTU;
2888 chan->ops = &smp_root_chan_ops;
2889
Johan Hedbergabe84902014-11-12 22:22:21 +02002890 /* Set correct nesting level for a parent/listening channel */
2891 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2892
Johan Hedbergef8efe42014-08-13 15:12:32 +03002893 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03002894}
2895
Johan Hedbergef8efe42014-08-13 15:12:32 +03002896static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002897{
Johan Hedbergef8efe42014-08-13 15:12:32 +03002898 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002899
Johan Hedbergef8efe42014-08-13 15:12:32 +03002900 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002901
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002902 tfm_aes = chan->data;
2903 if (tfm_aes) {
2904 chan->data = NULL;
2905 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002906 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03002907
Johan Hedberg70db83c2014-08-08 09:37:16 +03002908 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002909}
Johan Hedbergef8efe42014-08-13 15:12:32 +03002910
2911int smp_register(struct hci_dev *hdev)
2912{
2913 struct l2cap_chan *chan;
2914
2915 BT_DBG("%s", hdev->name);
2916
2917 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
2918 if (IS_ERR(chan))
2919 return PTR_ERR(chan);
2920
2921 hdev->smp_data = chan;
2922
2923 if (!lmp_sc_capable(hdev) &&
2924 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2925 return 0;
2926
2927 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
2928 if (IS_ERR(chan)) {
2929 int err = PTR_ERR(chan);
2930 chan = hdev->smp_data;
2931 hdev->smp_data = NULL;
2932 smp_del_chan(chan);
2933 return err;
2934 }
2935
2936 hdev->smp_bredr_data = chan;
2937
2938 return 0;
2939}
2940
2941void smp_unregister(struct hci_dev *hdev)
2942{
2943 struct l2cap_chan *chan;
2944
2945 if (hdev->smp_bredr_data) {
2946 chan = hdev->smp_bredr_data;
2947 hdev->smp_bredr_data = NULL;
2948 smp_del_chan(chan);
2949 }
2950
2951 if (hdev->smp_data) {
2952 chan = hdev->smp_data;
2953 hdev->smp_data = NULL;
2954 smp_del_chan(chan);
2955 }
2956}