blob: ddc76cf45cfbd9ebd22c2decd5d654f92cfbbcc2 [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 Hedberg533e35d2014-06-16 19:25:18 +030058};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030059
60struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030061 struct l2cap_conn *conn;
62 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030063 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030064
Johan Hedberg4bc58f52014-05-20 09:45:47 +030065 u8 preq[7]; /* SMP Pairing Request */
66 u8 prsp[7]; /* SMP Pairing Response */
67 u8 prnd[16]; /* SMP Pairing Random (local) */
68 u8 rrnd[16]; /* SMP Pairing Random (remote) */
69 u8 pcnf[16]; /* SMP Pairing Confirm */
70 u8 tk[16]; /* SMP Temporary Key */
71 u8 enc_key_size;
72 u8 remote_key_dist;
73 bdaddr_t id_addr;
74 u8 id_addr_type;
75 u8 irk[16];
76 struct smp_csrk *csrk;
77 struct smp_csrk *slave_csrk;
78 struct smp_ltk *ltk;
79 struct smp_ltk *slave_ltk;
80 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +030081 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +030082 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +030083 u8 method;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030084
Johan Hedberg3b191462014-06-06 10:50:15 +030085 /* Secure Connections variables */
86 u8 local_pk[64];
87 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030088 u8 remote_pk[64];
89 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +030090 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +030091
Johan Hedberg6a7bd102014-06-27 14:23:03 +030092 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +030093 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030094};
95
Johan Hedbergaeb7d462014-05-31 18:52:28 +030096/* These debug key values are defined in the SMP section of the core
97 * specification. debug_pk is the public debug key and debug_sk the
98 * private debug key.
99 */
100static const u8 debug_pk[64] = {
101 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
102 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
103 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
104 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
105
106 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
107 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
108 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
109 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
110};
111
112static const u8 debug_sk[32] = {
113 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
114 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
115 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
116 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
117};
118
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300119static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300120{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300121 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300122
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300123 for (i = 0; i < len; i++)
124 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300125}
126
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300127static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
128 size_t len, u8 mac[16])
129{
130 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
131 struct hash_desc desc;
132 struct scatterlist sg;
133 int err;
134
135 if (len > CMAC_MSG_MAX)
136 return -EFBIG;
137
138 if (!tfm) {
139 BT_ERR("tfm %p", tfm);
140 return -EINVAL;
141 }
142
143 desc.tfm = tfm;
144 desc.flags = 0;
145
146 crypto_hash_init(&desc);
147
148 /* Swap key and message from LSB to MSB */
149 swap_buf(k, tmp, 16);
150 swap_buf(m, msg_msb, len);
151
152 BT_DBG("msg (len %zu) %*phN", len, (int) len, m);
153 BT_DBG("key %16phN", k);
154
155 err = crypto_hash_setkey(tfm, tmp, 16);
156 if (err) {
157 BT_ERR("cipher setkey failed: %d", err);
158 return err;
159 }
160
161 sg_init_one(&sg, msg_msb, len);
162
163 err = crypto_hash_update(&desc, &sg, len);
164 if (err) {
165 BT_ERR("Hash update error %d", err);
166 return err;
167 }
168
169 err = crypto_hash_final(&desc, mac_msb);
170 if (err) {
171 BT_ERR("Hash final error %d", err);
172 return err;
173 }
174
175 swap_buf(mac_msb, mac, 16);
176
177 BT_DBG("mac %16phN", mac);
178
179 return 0;
180}
181
182static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
183 const u8 x[16], u8 z, u8 res[16])
184{
185 u8 m[65];
186 int err;
187
188 BT_DBG("u %32phN", u);
189 BT_DBG("v %32phN", v);
190 BT_DBG("x %16phN z %02x", x, z);
191
192 m[0] = z;
193 memcpy(m + 1, v, 32);
194 memcpy(m + 33, u, 32);
195
196 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
197 if (err)
198 return err;
199
200 BT_DBG("res %16phN", res);
201
202 return err;
203}
204
Johan Hedberg760b0182014-06-06 11:44:05 +0300205static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
206 u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16])
207{
208 /* The btle, salt and length "magic" values are as defined in
209 * the SMP section of the Bluetooth core specification. In ASCII
210 * the btle value ends up being 'btle'. The salt is just a
211 * random number whereas length is the value 256 in little
212 * endian format.
213 */
214 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
215 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
216 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
217 const u8 length[2] = { 0x00, 0x01 };
218 u8 m[53], t[16];
219 int err;
220
221 BT_DBG("w %32phN", w);
222 BT_DBG("n1 %16phN n2 %16phN", n1, n2);
223 BT_DBG("a1 %7phN a2 %7phN", a1, a2);
224
225 err = aes_cmac(tfm_cmac, salt, w, 32, t);
226 if (err)
227 return err;
228
229 BT_DBG("t %16phN", t);
230
231 memcpy(m, length, 2);
232 memcpy(m + 2, a2, 7);
233 memcpy(m + 9, a1, 7);
234 memcpy(m + 16, n2, 16);
235 memcpy(m + 32, n1, 16);
236 memcpy(m + 48, btle, 4);
237
238 m[52] = 0; /* Counter */
239
240 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
241 if (err)
242 return err;
243
244 BT_DBG("mackey %16phN", mackey);
245
246 m[52] = 1; /* Counter */
247
248 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
249 if (err)
250 return err;
251
252 BT_DBG("ltk %16phN", ltk);
253
254 return 0;
255}
256
257static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
258 const u8 n1[16], u8 n2[16], const u8 r[16],
259 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
260 u8 res[16])
261{
262 u8 m[65];
263 int err;
264
265 BT_DBG("w %16phN", w);
266 BT_DBG("n1 %16phN n2 %16phN", n1, n2);
267 BT_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
268
269 memcpy(m, a2, 7);
270 memcpy(m + 7, a1, 7);
271 memcpy(m + 14, io_cap, 3);
272 memcpy(m + 17, r, 16);
273 memcpy(m + 33, n2, 16);
274 memcpy(m + 49, n1, 16);
275
276 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
277 if (err)
278 return err;
279
280 BT_DBG("res %16phN", res);
281
282 return err;
283}
284
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300285static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
286 const u8 x[16], const u8 y[16], u32 *val)
287{
288 u8 m[80], tmp[16];
289 int err;
290
291 BT_DBG("u %32phN", u);
292 BT_DBG("v %32phN", v);
293 BT_DBG("x %16phN y %16phN", x, y);
294
295 memcpy(m, y, 16);
296 memcpy(m + 16, v, 32);
297 memcpy(m + 48, u, 32);
298
299 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
300 if (err)
301 return err;
302
303 *val = get_unaligned_le32(tmp);
304 *val %= 1000000;
305
306 BT_DBG("val %06u", *val);
307
308 return 0;
309}
310
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300311static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
312{
313 struct blkcipher_desc desc;
314 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200315 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200316 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300317
318 if (tfm == NULL) {
319 BT_ERR("tfm %p", tfm);
320 return -EINVAL;
321 }
322
323 desc.tfm = tfm;
324 desc.flags = 0;
325
Johan Hedberg943a7322014-03-18 12:58:24 +0200326 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300327 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200328
329 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300330 if (err) {
331 BT_ERR("cipher setkey failed: %d", err);
332 return err;
333 }
334
Johan Hedberg943a7322014-03-18 12:58:24 +0200335 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300336 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200337
338 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300339
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300340 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
341 if (err)
342 BT_ERR("Encrypt data error %d", err);
343
Johan Hedberg943a7322014-03-18 12:58:24 +0200344 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300345 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200346
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300347 return err;
348}
349
Johan Hedberg6a770832014-06-06 11:54:04 +0300350static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
351 const u8 key_id[4], u8 res[16])
352{
353 int err;
354
355 BT_DBG("w %16phN key_id %4phN", w, key_id);
356
357 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
358 if (err)
359 return err;
360
361 BT_DBG("res %16phN", res);
362
363 return err;
364}
365
Johan Hedberg60478052014-02-18 10:19:31 +0200366static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
367{
Johan Hedberg943a7322014-03-18 12:58:24 +0200368 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200369 int err;
370
371 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200372 memcpy(_res, r, 3);
373 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200374
Johan Hedberg943a7322014-03-18 12:58:24 +0200375 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200376 if (err) {
377 BT_ERR("Encrypt error");
378 return err;
379 }
380
381 /* The output of the random address function ah is:
382 * ah(h, r) = e(k, r') mod 2^24
383 * The output of the security function e is then truncated to 24 bits
384 * by taking the least significant 24 bits of the output of e as the
385 * result of ah.
386 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200387 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200388
389 return 0;
390}
391
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300392bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200393{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300394 struct l2cap_chan *chan = hdev->smp_data;
395 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200396 u8 hash[3];
397 int err;
398
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300399 if (!chan || !chan->data)
400 return false;
401
402 tfm = chan->data;
403
Johan Hedberg60478052014-02-18 10:19:31 +0200404 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
405
406 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
407 if (err)
408 return false;
409
410 return !memcmp(bdaddr->b, hash, 3);
411}
412
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300413int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200414{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300415 struct l2cap_chan *chan = hdev->smp_data;
416 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200417 int err;
418
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300419 if (!chan || !chan->data)
420 return -EOPNOTSUPP;
421
422 tfm = chan->data;
423
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200424 get_random_bytes(&rpa->b[3], 3);
425
426 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
427 rpa->b[5] |= 0x40; /* Set second most significant bit */
428
429 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
430 if (err < 0)
431 return err;
432
433 BT_DBG("RPA %pMR", rpa);
434
435 return 0;
436}
437
Johan Hedberge491eaf2014-10-25 21:15:37 +0200438static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
439 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
440 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300441{
442 u8 p1[16], p2[16];
443 int err;
444
445 memset(p1, 0, 16);
446
447 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200448 p1[0] = _iat;
449 p1[1] = _rat;
450 memcpy(p1 + 2, preq, 7);
451 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300452
453 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200454 memcpy(p2, ra, 6);
455 memcpy(p2 + 6, ia, 6);
456 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300457
458 /* res = r XOR p1 */
459 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
460
461 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200462 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300463 if (err) {
464 BT_ERR("Encrypt data error");
465 return err;
466 }
467
468 /* res = res XOR p2 */
469 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
470
471 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200472 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300473 if (err)
474 BT_ERR("Encrypt data error");
475
476 return err;
477}
478
Johan Hedberge491eaf2014-10-25 21:15:37 +0200479static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
480 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300481{
482 int err;
483
484 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200485 memcpy(_r, r2, 8);
486 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300487
Johan Hedberge491eaf2014-10-25 21:15:37 +0200488 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300489 if (err)
490 BT_ERR("Encrypt data error");
491
492 return err;
493}
494
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300495static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
496{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300497 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300498 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300499 struct kvec iv[2];
500 struct msghdr msg;
501
502 if (!chan)
503 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300504
505 BT_DBG("code 0x%2.2x", code);
506
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300507 iv[0].iov_base = &code;
508 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300509
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300510 iv[1].iov_base = data;
511 iv[1].iov_len = len;
512
513 memset(&msg, 0, sizeof(msg));
514
515 msg.msg_iov = (struct iovec *) &iv;
516 msg.msg_iovlen = 2;
517
518 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300519
Johan Hedbergb68fda62014-08-11 22:06:40 +0300520 if (!chan->data)
521 return;
522
523 smp = chan->data;
524
525 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300526 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300527}
528
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300529static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800530{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300531 if (authreq & SMP_AUTH_MITM) {
532 if (authreq & SMP_AUTH_SC)
533 return BT_SECURITY_FIPS;
534 else
535 return BT_SECURITY_HIGH;
536 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800537 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300538 }
Brian Gix2b64d152011-12-21 16:12:12 -0800539}
540
541static __u8 seclevel_to_authreq(__u8 sec_level)
542{
543 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300544 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800545 case BT_SECURITY_HIGH:
546 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
547 case BT_SECURITY_MEDIUM:
548 return SMP_AUTH_BONDING;
549 default:
550 return SMP_AUTH_NONE;
551 }
552}
553
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300554static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700555 struct smp_cmd_pairing *req,
556 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300557{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300558 struct l2cap_chan *chan = conn->smp;
559 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200560 struct hci_conn *hcon = conn->hcon;
561 struct hci_dev *hdev = hcon->hdev;
562 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300563
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300564 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700565 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
566 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300567 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800568 } else {
569 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300570 }
571
Johan Hedbergfd349c02014-02-18 10:19:36 +0200572 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
573 remote_dist |= SMP_DIST_ID_KEY;
574
Johan Hedberg863efaf2014-02-22 19:06:32 +0200575 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
576 local_dist |= SMP_DIST_ID_KEY;
577
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300578 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
579 if ((authreq & SMP_AUTH_SC) &&
580 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
581 local_dist |= SMP_DIST_LINK_KEY;
582 remote_dist |= SMP_DIST_LINK_KEY;
583 }
584 } else {
585 authreq &= ~SMP_AUTH_SC;
586 }
587
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300588 if (rsp == NULL) {
589 req->io_capability = conn->hcon->io_capability;
590 req->oob_flag = SMP_OOB_NOT_PRESENT;
591 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200592 req->init_key_dist = local_dist;
593 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300594 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200595
596 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300597 return;
598 }
599
600 rsp->io_capability = conn->hcon->io_capability;
601 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
602 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200603 rsp->init_key_dist = req->init_key_dist & remote_dist;
604 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300605 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200606
607 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300608}
609
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300610static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
611{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300612 struct l2cap_chan *chan = conn->smp;
613 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300614
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300615 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700616 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300617 return SMP_ENC_KEY_SIZE;
618
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300619 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300620
621 return 0;
622}
623
Johan Hedberg6f48e262014-08-11 22:06:44 +0300624static void smp_chan_destroy(struct l2cap_conn *conn)
625{
626 struct l2cap_chan *chan = conn->smp;
627 struct smp_chan *smp = chan->data;
628 bool complete;
629
630 BUG_ON(!smp);
631
632 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300633
Johan Hedberg6f48e262014-08-11 22:06:44 +0300634 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
635 mgmt_smp_complete(conn->hcon, complete);
636
637 kfree(smp->csrk);
638 kfree(smp->slave_csrk);
Johan Hedberg6a770832014-06-06 11:54:04 +0300639 kfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300640
641 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300642 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300643
644 /* If pairing failed clean up any keys we might have */
645 if (!complete) {
646 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200647 list_del_rcu(&smp->ltk->list);
648 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300649 }
650
651 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200652 list_del_rcu(&smp->slave_ltk->list);
653 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300654 }
655
656 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200657 list_del_rcu(&smp->remote_irk->list);
658 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300659 }
660 }
661
662 chan->data = NULL;
663 kfree(smp);
664 hci_conn_drop(conn->hcon);
665}
666
Johan Hedberg84794e12013-11-06 11:24:57 +0200667static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800668{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200669 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300670 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200671
Johan Hedberg84794e12013-11-06 11:24:57 +0200672 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800673 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700674 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800675
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700676 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700677 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300678
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300679 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300680 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800681}
682
Brian Gix2b64d152011-12-21 16:12:12 -0800683#define JUST_WORKS 0x00
684#define JUST_CFM 0x01
685#define REQ_PASSKEY 0x02
686#define CFM_PASSKEY 0x03
687#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300688#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800689#define OVERLAP 0xFF
690
691static const u8 gen_method[5][5] = {
692 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
693 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
694 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
695 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
696 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
697};
698
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300699static const u8 sc_method[5][5] = {
700 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
701 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
702 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
703 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
704 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
705};
706
Johan Hedberg581370c2014-06-17 13:07:38 +0300707static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
708{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300709 /* If either side has unknown io_caps, use JUST_CFM (which gets
710 * converted later to JUST_WORKS if we're initiators.
711 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300712 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
713 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300714 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300715
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300716 if (test_bit(SMP_FLAG_SC, &smp->flags))
717 return sc_method[remote_io][local_io];
718
Johan Hedberg581370c2014-06-17 13:07:38 +0300719 return gen_method[remote_io][local_io];
720}
721
Brian Gix2b64d152011-12-21 16:12:12 -0800722static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
723 u8 local_io, u8 remote_io)
724{
725 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300726 struct l2cap_chan *chan = conn->smp;
727 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800728 u32 passkey = 0;
729 int ret = 0;
730
731 /* Initialize key for JUST WORKS */
732 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300733 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800734
735 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
736
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300737 /* If neither side wants MITM, either "just" confirm an incoming
738 * request or use just-works for outgoing ones. The JUST_CFM
739 * will be converted to JUST_WORKS if necessary later in this
740 * function. If either side has MITM look up the method from the
741 * table.
742 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300743 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300744 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800745 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300746 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800747
Johan Hedberga82505c2014-03-24 14:39:07 +0200748 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300749 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
750 &smp->flags))
751 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200752
Johan Hedberg02f3e252014-07-16 15:09:13 +0300753 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300754 if (smp->method == JUST_CFM &&
755 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
756 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300757
Brian Gix2b64d152011-12-21 16:12:12 -0800758 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300759 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300760 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800761 return 0;
762 }
763
764 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300765 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300766 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300767 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
768 hcon->pending_sec_level = BT_SECURITY_HIGH;
769 }
Brian Gix2b64d152011-12-21 16:12:12 -0800770
771 /* If both devices have Keyoard-Display I/O, the master
772 * Confirms and the slave Enters the passkey.
773 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300774 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300775 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300776 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800777 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300778 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800779 }
780
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200781 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300782 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200783 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800784 get_random_bytes(&passkey, sizeof(passkey));
785 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200786 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800787 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300788 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800789 }
790
Johan Hedberg783e0572014-05-31 18:48:26 +0300791 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700792 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200793 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300794 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200795 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
796 hcon->type, hcon->dst_type,
797 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800798 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200799 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200800 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200801 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800802
Brian Gix2b64d152011-12-21 16:12:12 -0800803 return ret;
804}
805
Johan Hedberg1cc61142014-05-20 09:45:52 +0300806static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300807{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300808 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300809 struct smp_cmd_pairing_confirm cp;
810 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300811
812 BT_DBG("conn %p", conn);
813
Johan Hedberge491eaf2014-10-25 21:15:37 +0200814 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200815 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200816 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
817 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300818 if (ret)
819 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300820
Johan Hedberg4a74d652014-05-20 09:45:50 +0300821 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800822
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300823 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
824
Johan Hedbergb28b4942014-09-05 22:19:55 +0300825 if (conn->hcon->out)
826 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
827 else
828 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
829
Johan Hedberg1cc61142014-05-20 09:45:52 +0300830 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300831}
832
Johan Hedberg861580a2014-05-20 09:45:51 +0300833static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300834{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300835 struct l2cap_conn *conn = smp->conn;
836 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300837 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300838 int ret;
839
Johan Hedbergec70f362014-06-27 14:23:04 +0300840 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300841 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300842
843 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
844
Johan Hedberge491eaf2014-10-25 21:15:37 +0200845 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200846 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200847 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300848 if (ret)
849 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300850
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300851 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
852 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300853 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300854 }
855
856 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800857 u8 stk[16];
858 __le64 rand = 0;
859 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300860
Johan Hedberge491eaf2014-10-25 21:15:37 +0200861 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300862
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300863 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300864 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300865
Johan Hedberg861580a2014-05-20 09:45:51 +0300866 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
867 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300868
869 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300870 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300871 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300872 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300873 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800874 __le64 rand = 0;
875 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300876
Johan Hedberg943a7322014-03-18 12:58:24 +0200877 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
878 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300879
Johan Hedberge491eaf2014-10-25 21:15:37 +0200880 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300881
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300882 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700883 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300884
Johan Hedbergfff34902014-06-10 15:19:50 +0300885 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
886 auth = 1;
887 else
888 auth = 0;
889
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300890 /* Even though there's no _SLAVE suffix this is the
891 * slave STK we're adding for later lookup (the master
892 * STK never needs to be stored).
893 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700894 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300895 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300896 }
897
Johan Hedberg861580a2014-05-20 09:45:51 +0300898 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300899}
900
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300901static void smp_notify_keys(struct l2cap_conn *conn)
902{
903 struct l2cap_chan *chan = conn->smp;
904 struct smp_chan *smp = chan->data;
905 struct hci_conn *hcon = conn->hcon;
906 struct hci_dev *hdev = hcon->hdev;
907 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
908 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
909 bool persistent;
910
911 if (smp->remote_irk) {
912 mgmt_new_irk(hdev, smp->remote_irk);
913 /* Now that user space can be considered to know the
914 * identity address track the connection based on it
915 * from now on.
916 */
917 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
918 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300919 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300920
921 /* When receiving an indentity resolving key for
922 * a remote device that does not use a resolvable
923 * private address, just remove the key so that
924 * it is possible to use the controller white
925 * list for scanning.
926 *
927 * Userspace will have been told to not store
928 * this key at this point. So it is safe to
929 * just remove it.
930 */
931 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200932 list_del_rcu(&smp->remote_irk->list);
933 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300934 smp->remote_irk = NULL;
935 }
936 }
937
938 /* The LTKs and CSRKs should be persistent only if both sides
939 * had the bonding bit set in their authentication requests.
940 */
941 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
942
943 if (smp->csrk) {
944 smp->csrk->bdaddr_type = hcon->dst_type;
945 bacpy(&smp->csrk->bdaddr, &hcon->dst);
946 mgmt_new_csrk(hdev, smp->csrk, persistent);
947 }
948
949 if (smp->slave_csrk) {
950 smp->slave_csrk->bdaddr_type = hcon->dst_type;
951 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
952 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
953 }
954
955 if (smp->ltk) {
956 smp->ltk->bdaddr_type = hcon->dst_type;
957 bacpy(&smp->ltk->bdaddr, &hcon->dst);
958 mgmt_new_ltk(hdev, smp->ltk, persistent);
959 }
960
961 if (smp->slave_ltk) {
962 smp->slave_ltk->bdaddr_type = hcon->dst_type;
963 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
964 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
965 }
Johan Hedberg6a770832014-06-06 11:54:04 +0300966
967 if (smp->link_key) {
968 hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
969 smp->link_key, HCI_LK_AUTH_COMBINATION_P256,
970 0, NULL);
971 }
972}
973
974static void sc_generate_link_key(struct smp_chan *smp)
975{
976 /* These constants are as specified in the core specification.
977 * In ASCII they spell out to 'tmp1' and 'lebr'.
978 */
979 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
980 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
981
982 smp->link_key = kzalloc(16, GFP_KERNEL);
983 if (!smp->link_key)
984 return;
985
986 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
987 kfree(smp->link_key);
988 smp->link_key = NULL;
989 return;
990 }
991
992 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
993 kfree(smp->link_key);
994 smp->link_key = NULL;
995 return;
996 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300997}
998
Johan Hedbergb28b4942014-09-05 22:19:55 +0300999static void smp_allow_key_dist(struct smp_chan *smp)
1000{
1001 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1002 * will be allowed in each PDU handler to ensure we receive
1003 * them in the correct order.
1004 */
1005 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1006 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1007 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1008 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1009 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1010 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1011}
1012
Johan Hedbergd6268e82014-09-05 22:19:51 +03001013static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001014{
1015 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001016 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001017 struct hci_conn *hcon = conn->hcon;
1018 struct hci_dev *hdev = hcon->hdev;
1019 __u8 *keydist;
1020
1021 BT_DBG("conn %p", conn);
1022
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001023 rsp = (void *) &smp->prsp[1];
1024
1025 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001026 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1027 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001028 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001029 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001030
1031 req = (void *) &smp->preq[1];
1032
1033 if (hcon->out) {
1034 keydist = &rsp->init_key_dist;
1035 *keydist &= req->init_key_dist;
1036 } else {
1037 keydist = &rsp->resp_key_dist;
1038 *keydist &= req->resp_key_dist;
1039 }
1040
Johan Hedberg6a770832014-06-06 11:54:04 +03001041 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1042 if (*keydist & SMP_DIST_LINK_KEY)
1043 sc_generate_link_key(smp);
1044
1045 /* Clear the keys which are generated but not distributed */
1046 *keydist &= ~SMP_SC_NO_DIST;
1047 }
1048
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001049 BT_DBG("keydist 0x%x", *keydist);
1050
1051 if (*keydist & SMP_DIST_ENC_KEY) {
1052 struct smp_cmd_encrypt_info enc;
1053 struct smp_cmd_master_ident ident;
1054 struct smp_ltk *ltk;
1055 u8 authenticated;
1056 __le16 ediv;
1057 __le64 rand;
1058
1059 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1060 get_random_bytes(&ediv, sizeof(ediv));
1061 get_random_bytes(&rand, sizeof(rand));
1062
1063 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1064
1065 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1066 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1067 SMP_LTK_SLAVE, authenticated, enc.ltk,
1068 smp->enc_key_size, ediv, rand);
1069 smp->slave_ltk = ltk;
1070
1071 ident.ediv = ediv;
1072 ident.rand = rand;
1073
1074 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1075
1076 *keydist &= ~SMP_DIST_ENC_KEY;
1077 }
1078
1079 if (*keydist & SMP_DIST_ID_KEY) {
1080 struct smp_cmd_ident_addr_info addrinfo;
1081 struct smp_cmd_ident_info idinfo;
1082
1083 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1084
1085 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1086
1087 /* The hci_conn contains the local identity address
1088 * after the connection has been established.
1089 *
1090 * This is true even when the connection has been
1091 * established using a resolvable random address.
1092 */
1093 bacpy(&addrinfo.bdaddr, &hcon->src);
1094 addrinfo.addr_type = hcon->src_type;
1095
1096 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1097 &addrinfo);
1098
1099 *keydist &= ~SMP_DIST_ID_KEY;
1100 }
1101
1102 if (*keydist & SMP_DIST_SIGN) {
1103 struct smp_cmd_sign_info sign;
1104 struct smp_csrk *csrk;
1105
1106 /* Generate a new random key */
1107 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1108
1109 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1110 if (csrk) {
1111 csrk->master = 0x00;
1112 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1113 }
1114 smp->slave_csrk = csrk;
1115
1116 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1117
1118 *keydist &= ~SMP_DIST_SIGN;
1119 }
1120
1121 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001122 if (smp->remote_key_dist & KEY_DIST_MASK) {
1123 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001124 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001125 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001126
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001127 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1128 smp_notify_keys(conn);
1129
1130 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001131}
1132
Johan Hedbergb68fda62014-08-11 22:06:40 +03001133static void smp_timeout(struct work_struct *work)
1134{
1135 struct smp_chan *smp = container_of(work, struct smp_chan,
1136 security_timer.work);
1137 struct l2cap_conn *conn = smp->conn;
1138
1139 BT_DBG("conn %p", conn);
1140
Johan Hedberg1e91c292014-08-18 20:33:29 +03001141 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001142}
1143
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001144static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1145{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001146 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001147 struct smp_chan *smp;
1148
Marcel Holtmannf1560462013-10-13 05:43:25 -07001149 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001150 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001151 return NULL;
1152
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001153 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1154 if (IS_ERR(smp->tfm_aes)) {
1155 BT_ERR("Unable to create ECB crypto context");
1156 kfree(smp);
1157 return NULL;
1158 }
1159
Johan Hedberg407cecf2014-05-02 14:19:47 +03001160 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1161 if (IS_ERR(smp->tfm_cmac)) {
1162 BT_ERR("Unable to create CMAC crypto context");
1163 crypto_free_blkcipher(smp->tfm_aes);
1164 kfree(smp);
1165 return NULL;
1166 }
1167
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001168 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001169 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001170
Johan Hedbergb28b4942014-09-05 22:19:55 +03001171 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1172
Johan Hedbergb68fda62014-08-11 22:06:40 +03001173 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1174
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001175 hci_conn_hold(conn->hcon);
1176
1177 return smp;
1178}
1179
Johan Hedberg760b0182014-06-06 11:44:05 +03001180static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1181{
1182 struct hci_conn *hcon = smp->conn->hcon;
1183 u8 *na, *nb, a[7], b[7];
1184
1185 if (hcon->out) {
1186 na = smp->prnd;
1187 nb = smp->rrnd;
1188 } else {
1189 na = smp->rrnd;
1190 nb = smp->prnd;
1191 }
1192
1193 memcpy(a, &hcon->init_addr, 6);
1194 memcpy(b, &hcon->resp_addr, 6);
1195 a[6] = hcon->init_addr_type;
1196 b[6] = hcon->resp_addr_type;
1197
1198 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1199}
1200
1201static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1202{
1203 struct hci_conn *hcon = smp->conn->hcon;
1204 struct smp_cmd_dhkey_check check;
1205 u8 a[7], b[7], *local_addr, *remote_addr;
1206 u8 io_cap[3], r[16];
1207
1208 switch (mgmt_op) {
1209 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1210 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1211 return 0;
1212 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1213 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1214 return 0;
1215 }
1216
1217 memcpy(a, &hcon->init_addr, 6);
1218 memcpy(b, &hcon->resp_addr, 6);
1219 a[6] = hcon->init_addr_type;
1220 b[6] = hcon->resp_addr_type;
1221
1222 if (hcon->out) {
1223 local_addr = a;
1224 remote_addr = b;
1225 memcpy(io_cap, &smp->preq[1], 3);
1226 } else {
1227 local_addr = b;
1228 remote_addr = a;
1229 memcpy(io_cap, &smp->prsp[1], 3);
1230 }
1231
1232 memcpy(r, &passkey, sizeof(passkey));
1233 memset(r + sizeof(passkey), 0, sizeof(r) - sizeof(passkey));
1234
1235 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1236 local_addr, remote_addr, check.e);
1237
1238 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
1239
1240 return 0;
1241}
1242
Brian Gix2b64d152011-12-21 16:12:12 -08001243int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1244{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001245 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001246 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001247 struct smp_chan *smp;
1248 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001249 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001250
1251 BT_DBG("");
1252
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001253 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001254 return -ENOTCONN;
1255
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001256 chan = conn->smp;
1257 if (!chan)
1258 return -ENOTCONN;
1259
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001260 l2cap_chan_lock(chan);
1261 if (!chan->data) {
1262 err = -ENOTCONN;
1263 goto unlock;
1264 }
1265
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001266 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001267
Johan Hedberg760b0182014-06-06 11:44:05 +03001268 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1269 err = sc_user_reply(smp, mgmt_op, passkey);
1270 goto unlock;
1271 }
1272
Brian Gix2b64d152011-12-21 16:12:12 -08001273 switch (mgmt_op) {
1274 case MGMT_OP_USER_PASSKEY_REPLY:
1275 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001276 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001277 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001278 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001279 /* Fall Through */
1280 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001281 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001282 break;
1283 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1284 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001285 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001286 err = 0;
1287 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001288 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001289 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001290 err = -EOPNOTSUPP;
1291 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001292 }
1293
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001294 err = 0;
1295
Brian Gix2b64d152011-12-21 16:12:12 -08001296 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001297 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1298 u8 rsp = smp_confirm(smp);
1299 if (rsp)
1300 smp_failure(conn, rsp);
1301 }
Brian Gix2b64d152011-12-21 16:12:12 -08001302
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001303unlock:
1304 l2cap_chan_unlock(chan);
1305 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001306}
1307
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001308static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001309{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001310 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001311 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001312 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001313 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001314 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001315 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001316
1317 BT_DBG("conn %p", conn);
1318
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001319 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001320 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001321
Johan Hedberg40bef302014-07-16 11:42:27 +03001322 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001323 return SMP_CMD_NOTSUPP;
1324
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001325 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001326 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001327 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001328 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001329
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001330 if (!smp)
1331 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001332
Johan Hedbergc05b9332014-09-10 17:37:42 -07001333 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001334 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001335
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001336 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001337 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001338 return SMP_PAIRING_NOTSUPP;
1339
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001340 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1341 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001342 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001343
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001344 build_pairing_cmd(conn, req, &rsp, auth);
1345
1346 if (rsp.auth_req & SMP_AUTH_SC)
1347 set_bit(SMP_FLAG_SC, &smp->flags);
1348
Johan Hedberg5be5e272014-09-10 17:58:54 -07001349 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001350 sec_level = BT_SECURITY_MEDIUM;
1351 else
1352 sec_level = authreq_to_seclevel(auth);
1353
Johan Hedbergc7262e72014-06-17 13:07:37 +03001354 if (sec_level > conn->hcon->pending_sec_level)
1355 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001356
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001357 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001358 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1359 u8 method;
1360
1361 method = get_auth_method(smp, conn->hcon->io_capability,
1362 req->io_capability);
1363 if (method == JUST_WORKS || method == JUST_CFM)
1364 return SMP_AUTH_REQUIREMENTS;
1365 }
1366
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001367 key_size = min(req->max_key_size, rsp.max_key_size);
1368 if (check_enc_key_size(conn, key_size))
1369 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001370
Johan Hedberge84a6b12013-12-02 10:49:03 +02001371 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001372
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001373 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1374 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001375
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001376 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001377
1378 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1379
1380 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1381 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1382 /* Clear bits which are generated but not distributed */
1383 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1384 /* Wait for Public Key from Initiating Device */
1385 return 0;
1386 } else {
1387 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1388 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001389
Brian Gix2b64d152011-12-21 16:12:12 -08001390 /* Request setup of TK */
1391 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1392 if (ret)
1393 return SMP_UNSPECIFIED;
1394
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001395 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001396}
1397
Johan Hedberg3b191462014-06-06 10:50:15 +03001398static u8 sc_send_public_key(struct smp_chan *smp)
1399{
1400 BT_DBG("");
1401
1402 /* Generate local key pair for Secure Connections */
1403 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1404 return SMP_UNSPECIFIED;
1405
1406 BT_DBG("Local Public Key X: %32phN", smp->local_pk);
1407 BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1408 BT_DBG("Local Private Key: %32phN", smp->local_sk);
1409
1410 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1411
1412 return 0;
1413}
1414
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001415static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001416{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001417 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001418 struct l2cap_chan *chan = conn->smp;
1419 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001420 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001421 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001422 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001423
1424 BT_DBG("conn %p", conn);
1425
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001426 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001427 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001428
Johan Hedberg40bef302014-07-16 11:42:27 +03001429 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001430 return SMP_CMD_NOTSUPP;
1431
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001432 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001433
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001434 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001435
1436 key_size = min(req->max_key_size, rsp->max_key_size);
1437 if (check_enc_key_size(conn, key_size))
1438 return SMP_ENC_KEY_SIZE;
1439
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001440 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001441
Johan Hedberg65668772014-05-16 11:03:34 +03001442 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1443 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001444 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1445 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001446
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001447 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001448 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1449 u8 method;
1450
1451 method = get_auth_method(smp, req->io_capability,
1452 rsp->io_capability);
1453 if (method == JUST_WORKS || method == JUST_CFM)
1454 return SMP_AUTH_REQUIREMENTS;
1455 }
1456
Johan Hedberge84a6b12013-12-02 10:49:03 +02001457 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001458
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001459 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1460 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001461
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001462 /* Update remote key distribution in case the remote cleared
1463 * some bits that we had enabled in our request.
1464 */
1465 smp->remote_key_dist &= rsp->resp_key_dist;
1466
Johan Hedberg3b191462014-06-06 10:50:15 +03001467 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1468 /* Clear bits which are generated but not distributed */
1469 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1470 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1471 return sc_send_public_key(smp);
1472 }
1473
Johan Hedbergc05b9332014-09-10 17:37:42 -07001474 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001475
Johan Hedberg476585e2012-06-06 18:54:15 +08001476 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001477 if (ret)
1478 return SMP_UNSPECIFIED;
1479
Johan Hedberg4a74d652014-05-20 09:45:50 +03001480 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001481
1482 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001483 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001484 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001485
1486 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001487}
1488
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001489static u8 sc_check_confirm(struct smp_chan *smp)
1490{
1491 struct l2cap_conn *conn = smp->conn;
1492
1493 BT_DBG("");
1494
1495 /* Public Key exchange must happen before any other steps */
1496 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1497 return SMP_UNSPECIFIED;
1498
1499 if (conn->hcon->out) {
1500 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1501 smp->prnd);
1502 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1503 }
1504
1505 return 0;
1506}
1507
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001508static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001509{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001510 struct l2cap_chan *chan = conn->smp;
1511 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001512
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001513 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1514
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001515 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001516 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001517
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001518 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1519 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001520
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001521 if (test_bit(SMP_FLAG_SC, &smp->flags))
1522 return sc_check_confirm(smp);
1523
Johan Hedbergb28b4942014-09-05 22:19:55 +03001524 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001525 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1526 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001527 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1528 return 0;
1529 }
1530
1531 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001532 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001533 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001534 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001535
1536 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001537}
1538
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001539static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001540{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001541 struct l2cap_chan *chan = conn->smp;
1542 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001543 struct hci_conn *hcon = conn->hcon;
1544 u8 *pkax, *pkbx, *na, *nb;
1545 u32 passkey;
1546 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001547
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001548 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001549
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001550 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001551 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001552
Johan Hedberg943a7322014-03-18 12:58:24 +02001553 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001554 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001555
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001556 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1557 return smp_random(smp);
1558
1559 if (hcon->out) {
1560 u8 cfm[16];
1561
1562 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1563 smp->rrnd, 0, cfm);
1564 if (err)
1565 return SMP_UNSPECIFIED;
1566
1567 if (memcmp(smp->pcnf, cfm, 16))
1568 return SMP_CONFIRM_FAILED;
1569
1570 pkax = smp->local_pk;
1571 pkbx = smp->remote_pk;
1572 na = smp->prnd;
1573 nb = smp->rrnd;
1574 } else {
1575 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1576 smp->prnd);
1577 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1578
1579 pkax = smp->remote_pk;
1580 pkbx = smp->local_pk;
1581 na = smp->rrnd;
1582 nb = smp->prnd;
1583 }
1584
Johan Hedberg760b0182014-06-06 11:44:05 +03001585 /* Generate MacKey and LTK */
1586 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1587 if (err)
1588 return SMP_UNSPECIFIED;
1589
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001590 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
1591 if (err)
1592 return SMP_UNSPECIFIED;
1593
1594 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
1595 hcon->type, hcon->dst_type,
1596 passkey, 0);
1597 if (err)
1598 return SMP_UNSPECIFIED;
1599
1600 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001601}
1602
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001603static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001604{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001605 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001606 struct hci_conn *hcon = conn->hcon;
1607
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001608 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001609 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001610 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001611
Johan Hedberga6f78332014-09-10 17:37:45 -07001612 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001613 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001614
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001615 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001616 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001617
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001618 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1619 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001620
Johan Hedbergfe59a052014-07-01 19:14:12 +03001621 /* We never store STKs for master role, so clear this flag */
1622 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1623
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001624 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001625}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001626
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001627bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1628 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03001629{
1630 if (sec_level == BT_SECURITY_LOW)
1631 return true;
1632
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001633 /* If we're encrypted with an STK but the caller prefers using
1634 * LTK claim insufficient security. This way we allow the
1635 * connection to be re-encrypted with an LTK, even if the LTK
1636 * provides the same level of security. Only exception is if we
1637 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001638 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001639 if (key_pref == SMP_USE_LTK &&
1640 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001641 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001642 return false;
1643
Johan Hedberg854f4722014-07-01 18:40:20 +03001644 if (hcon->sec_level >= sec_level)
1645 return true;
1646
1647 return false;
1648}
1649
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001650static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001651{
1652 struct smp_cmd_security_req *rp = (void *) skb->data;
1653 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001654 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001655 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001656 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07001657 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001658
1659 BT_DBG("conn %p", conn);
1660
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001661 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001662 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001663
Johan Hedberg40bef302014-07-16 11:42:27 +03001664 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001665 return SMP_CMD_NOTSUPP;
1666
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001667 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001668
Johan Hedberg5be5e272014-09-10 17:58:54 -07001669 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001670 sec_level = BT_SECURITY_MEDIUM;
1671 else
1672 sec_level = authreq_to_seclevel(auth);
1673
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001674 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03001675 return 0;
1676
Johan Hedbergc7262e72014-06-17 13:07:37 +03001677 if (sec_level > hcon->pending_sec_level)
1678 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001679
Johan Hedberg4dab7862012-06-07 14:58:37 +08001680 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001681 return 0;
1682
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001683 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001684 if (!smp)
1685 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001686
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001687 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001688 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03001689 return SMP_PAIRING_NOTSUPP;
1690
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001691 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001692
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001693 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07001694 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001695
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001696 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1697 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001698
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001699 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001700 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001701
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001702 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001703}
1704
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001705int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001706{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001707 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001708 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001709 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001710 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001711 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001712
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001713 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1714
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001715 /* This may be NULL if there's an unexpected disconnection */
1716 if (!conn)
1717 return 1;
1718
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001719 chan = conn->smp;
1720
Johan Hedberg757aee02013-04-24 13:05:32 +03001721 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001722 return 1;
1723
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001724 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001725 return 1;
1726
Johan Hedbergc7262e72014-06-17 13:07:37 +03001727 if (sec_level > hcon->pending_sec_level)
1728 hcon->pending_sec_level = sec_level;
1729
Johan Hedberg40bef302014-07-16 11:42:27 +03001730 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001731 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1732 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001733
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001734 l2cap_chan_lock(chan);
1735
1736 /* If SMP is already in progress ignore this request */
1737 if (chan->data) {
1738 ret = 0;
1739 goto unlock;
1740 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001741
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001742 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001743 if (!smp) {
1744 ret = 1;
1745 goto unlock;
1746 }
Brian Gix2b64d152011-12-21 16:12:12 -08001747
1748 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001749
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001750 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
1751 authreq |= SMP_AUTH_SC;
1752
Johan Hedberg79897d22014-06-01 09:45:24 +03001753 /* Require MITM if IO Capability allows or the security level
1754 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001755 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001756 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001757 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001758 authreq |= SMP_AUTH_MITM;
1759
Johan Hedberg40bef302014-07-16 11:42:27 +03001760 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001761 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001762
Brian Gix2b64d152011-12-21 16:12:12 -08001763 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001764 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1765 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001766
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001767 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001768 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001769 } else {
1770 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001771 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001772 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001773 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001774 }
1775
Johan Hedberg4a74d652014-05-20 09:45:50 +03001776 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001777 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001778
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001779unlock:
1780 l2cap_chan_unlock(chan);
1781 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001782}
1783
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001784static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1785{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001786 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001787 struct l2cap_chan *chan = conn->smp;
1788 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001789
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001790 BT_DBG("conn %p", conn);
1791
1792 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001793 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001794
Johan Hedbergb28b4942014-09-05 22:19:55 +03001795 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001796
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001797 skb_pull(skb, sizeof(*rp));
1798
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001799 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001800
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001801 return 0;
1802}
1803
1804static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1805{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001806 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001807 struct l2cap_chan *chan = conn->smp;
1808 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001809 struct hci_dev *hdev = conn->hcon->hdev;
1810 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001811 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001812 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001813
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001814 BT_DBG("conn %p", conn);
1815
1816 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001817 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001818
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001819 /* Mark the information as received */
1820 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1821
Johan Hedbergb28b4942014-09-05 22:19:55 +03001822 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1823 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07001824 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1825 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001826
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001827 skb_pull(skb, sizeof(*rp));
1828
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001829 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001830 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001831 authenticated, smp->tk, smp->enc_key_size,
1832 rp->ediv, rp->rand);
1833 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001834 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001835 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001836
1837 return 0;
1838}
1839
Johan Hedbergfd349c02014-02-18 10:19:36 +02001840static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1841{
1842 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001843 struct l2cap_chan *chan = conn->smp;
1844 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001845
1846 BT_DBG("");
1847
1848 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001849 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001850
Johan Hedbergb28b4942014-09-05 22:19:55 +03001851 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001852
Johan Hedbergfd349c02014-02-18 10:19:36 +02001853 skb_pull(skb, sizeof(*info));
1854
1855 memcpy(smp->irk, info->irk, 16);
1856
1857 return 0;
1858}
1859
1860static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1861 struct sk_buff *skb)
1862{
1863 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001864 struct l2cap_chan *chan = conn->smp;
1865 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001866 struct hci_conn *hcon = conn->hcon;
1867 bdaddr_t rpa;
1868
1869 BT_DBG("");
1870
1871 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001872 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001873
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001874 /* Mark the information as received */
1875 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1876
Johan Hedbergb28b4942014-09-05 22:19:55 +03001877 if (smp->remote_key_dist & SMP_DIST_SIGN)
1878 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1879
Johan Hedbergfd349c02014-02-18 10:19:36 +02001880 skb_pull(skb, sizeof(*info));
1881
Johan Hedberga9a58f82014-02-25 22:24:37 +02001882 /* Strictly speaking the Core Specification (4.1) allows sending
1883 * an empty address which would force us to rely on just the IRK
1884 * as "identity information". However, since such
1885 * implementations are not known of and in order to not over
1886 * complicate our implementation, simply pretend that we never
1887 * received an IRK for such a device.
1888 */
1889 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1890 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001891 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001892 }
1893
Johan Hedbergfd349c02014-02-18 10:19:36 +02001894 bacpy(&smp->id_addr, &info->bdaddr);
1895 smp->id_addr_type = info->addr_type;
1896
1897 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1898 bacpy(&rpa, &hcon->dst);
1899 else
1900 bacpy(&rpa, BDADDR_ANY);
1901
Johan Hedberg23d0e122014-02-19 14:57:46 +02001902 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1903 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001904
Johan Hedberg31dd6242014-06-27 14:23:02 +03001905distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001906 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1907 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001908
1909 return 0;
1910}
1911
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001912static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1913{
1914 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001915 struct l2cap_chan *chan = conn->smp;
1916 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001917 struct smp_csrk *csrk;
1918
1919 BT_DBG("conn %p", conn);
1920
1921 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001922 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001923
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001924 /* Mark the information as received */
1925 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1926
1927 skb_pull(skb, sizeof(*rp));
1928
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001929 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1930 if (csrk) {
1931 csrk->master = 0x01;
1932 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1933 }
1934 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001935 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001936
1937 return 0;
1938}
1939
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001940static u8 sc_select_method(struct smp_chan *smp)
1941{
1942 struct l2cap_conn *conn = smp->conn;
1943 struct hci_conn *hcon = conn->hcon;
1944 struct smp_cmd_pairing *local, *remote;
1945 u8 local_mitm, remote_mitm, local_io, remote_io, method;
1946
1947 /* The preq/prsp contain the raw Pairing Request/Response PDUs
1948 * which are needed as inputs to some crypto functions. To get
1949 * the "struct smp_cmd_pairing" from them we need to skip the
1950 * first byte which contains the opcode.
1951 */
1952 if (hcon->out) {
1953 local = (void *) &smp->preq[1];
1954 remote = (void *) &smp->prsp[1];
1955 } else {
1956 local = (void *) &smp->prsp[1];
1957 remote = (void *) &smp->preq[1];
1958 }
1959
1960 local_io = local->io_capability;
1961 remote_io = remote->io_capability;
1962
1963 local_mitm = (local->auth_req & SMP_AUTH_MITM);
1964 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
1965
1966 /* If either side wants MITM, look up the method from the table,
1967 * otherwise use JUST WORKS.
1968 */
1969 if (local_mitm || remote_mitm)
1970 method = get_auth_method(smp, local_io, remote_io);
1971 else
1972 method = JUST_WORKS;
1973
1974 /* Don't confirm locally initiated pairing attempts */
1975 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
1976 method = JUST_WORKS;
1977
1978 return method;
1979}
1980
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001981static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
1982{
1983 struct smp_cmd_public_key *key = (void *) skb->data;
1984 struct hci_conn *hcon = conn->hcon;
1985 struct l2cap_chan *chan = conn->smp;
1986 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001987 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03001988 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03001989 int err;
1990
1991 BT_DBG("conn %p", conn);
1992
1993 if (skb->len < sizeof(*key))
1994 return SMP_INVALID_PARAMS;
1995
1996 memcpy(smp->remote_pk, key, 64);
1997
1998 /* Non-initiating device sends its public key after receiving
1999 * the key from the initiating device.
2000 */
2001 if (!hcon->out) {
2002 err = sc_send_public_key(smp);
2003 if (err)
2004 return err;
2005 }
2006
2007 BT_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2008 BT_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
2009
2010 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2011 return SMP_UNSPECIFIED;
2012
2013 BT_DBG("DHKey %32phN", smp->dhkey);
2014
2015 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2016
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002017 smp->method = sc_select_method(smp);
2018
2019 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2020
2021 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2022 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2023 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2024 else
2025 hcon->pending_sec_level = BT_SECURITY_FIPS;
2026
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002027 if (!memcmp(debug_pk, smp->remote_pk, 64))
2028 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2029
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002030 /* The Initiating device waits for the non-initiating device to
2031 * send the confirm value.
2032 */
2033 if (conn->hcon->out)
2034 return 0;
2035
2036 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2037 0, cfm.confirm_val);
2038 if (err)
2039 return SMP_UNSPECIFIED;
2040
2041 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2042 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2043
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002044 return 0;
2045}
2046
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002047static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2048{
2049 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2050 struct l2cap_chan *chan = conn->smp;
2051 struct hci_conn *hcon = conn->hcon;
2052 struct smp_chan *smp = chan->data;
2053 u8 a[7], b[7], *local_addr, *remote_addr;
2054 u8 io_cap[3], r[16], e[16];
2055 int err;
2056
2057 BT_DBG("conn %p", conn);
2058
2059 if (skb->len < sizeof(*check))
2060 return SMP_INVALID_PARAMS;
2061
2062 memcpy(a, &hcon->init_addr, 6);
2063 memcpy(b, &hcon->resp_addr, 6);
2064 a[6] = hcon->init_addr_type;
2065 b[6] = hcon->resp_addr_type;
2066
2067 if (hcon->out) {
2068 local_addr = a;
2069 remote_addr = b;
2070 memcpy(io_cap, &smp->prsp[1], 3);
2071 } else {
2072 local_addr = b;
2073 remote_addr = a;
2074 memcpy(io_cap, &smp->preq[1], 3);
2075 }
2076
2077 memset(r, 0, sizeof(r));
2078
2079 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2080 io_cap, remote_addr, local_addr, e);
2081 if (err)
2082 return SMP_UNSPECIFIED;
2083
2084 if (memcmp(check->e, e, 16))
2085 return SMP_DHKEY_CHECK_FAILED;
2086
2087 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
2088 SMP_LTK_P256, 0, smp->tk, smp->enc_key_size,
2089 0, 0);
2090
2091 if (hcon->out) {
2092 hci_le_start_enc(hcon, 0, 0, smp->tk);
2093 hcon->enc_key_size = smp->enc_key_size;
2094 }
2095
2096 return 0;
2097}
2098
Johan Hedberg4befb862014-08-11 22:06:38 +03002099static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002100{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002101 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002102 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002103 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002104 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002105 int err = 0;
2106
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002107 if (hcon->type != LE_LINK) {
2108 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03002109 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002110 }
2111
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002112 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002113 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002114
Marcel Holtmann06ae3312013-10-18 03:43:00 -07002115 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002116 reason = SMP_PAIRING_NOTSUPP;
2117 goto done;
2118 }
2119
Marcel Holtmann92381f52013-10-03 01:23:08 -07002120 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002121 skb_pull(skb, sizeof(code));
2122
Johan Hedbergb28b4942014-09-05 22:19:55 +03002123 smp = chan->data;
2124
2125 if (code > SMP_CMD_MAX)
2126 goto drop;
2127
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002128 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002129 goto drop;
2130
2131 /* If we don't have a context the only allowed commands are
2132 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002133 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002134 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2135 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002136
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002137 switch (code) {
2138 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002139 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002140 break;
2141
2142 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002143 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002144 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002145 break;
2146
2147 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002148 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002149 break;
2150
2151 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002152 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002153 break;
2154
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002155 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002156 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002157 break;
2158
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002159 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002160 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002161 break;
2162
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002163 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002164 reason = smp_cmd_encrypt_info(conn, skb);
2165 break;
2166
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002167 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002168 reason = smp_cmd_master_ident(conn, skb);
2169 break;
2170
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002171 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002172 reason = smp_cmd_ident_info(conn, skb);
2173 break;
2174
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002175 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002176 reason = smp_cmd_ident_addr_info(conn, skb);
2177 break;
2178
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002179 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002180 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002181 break;
2182
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002183 case SMP_CMD_PUBLIC_KEY:
2184 reason = smp_cmd_public_key(conn, skb);
2185 break;
2186
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002187 case SMP_CMD_DHKEY_CHECK:
2188 reason = smp_cmd_dhkey_check(conn, skb);
2189 break;
2190
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002191 default:
2192 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002193 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002194 goto done;
2195 }
2196
2197done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002198 if (!err) {
2199 if (reason)
2200 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002201 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002202 }
2203
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002204 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002205
2206drop:
2207 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2208 code, &hcon->dst);
2209 kfree_skb(skb);
2210 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002211}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002212
Johan Hedberg70db83c2014-08-08 09:37:16 +03002213static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2214{
2215 struct l2cap_conn *conn = chan->conn;
2216
2217 BT_DBG("chan %p", chan);
2218
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002219 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002220 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002221
Johan Hedberg70db83c2014-08-08 09:37:16 +03002222 conn->smp = NULL;
2223 l2cap_chan_put(chan);
2224}
2225
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002226static void smp_resume_cb(struct l2cap_chan *chan)
2227{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002228 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002229 struct l2cap_conn *conn = chan->conn;
2230 struct hci_conn *hcon = conn->hcon;
2231
2232 BT_DBG("chan %p", chan);
2233
Johan Hedberg86d14072014-08-11 22:06:43 +03002234 if (!smp)
2235 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002236
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002237 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2238 return;
2239
Johan Hedberg86d14072014-08-11 22:06:43 +03002240 cancel_delayed_work(&smp->security_timer);
2241
Johan Hedbergd6268e82014-09-05 22:19:51 +03002242 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002243}
2244
Johan Hedberg70db83c2014-08-08 09:37:16 +03002245static void smp_ready_cb(struct l2cap_chan *chan)
2246{
2247 struct l2cap_conn *conn = chan->conn;
2248
2249 BT_DBG("chan %p", chan);
2250
2251 conn->smp = chan;
2252 l2cap_chan_hold(chan);
2253}
2254
Johan Hedberg4befb862014-08-11 22:06:38 +03002255static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2256{
2257 int err;
2258
2259 BT_DBG("chan %p", chan);
2260
2261 err = smp_sig_channel(chan, skb);
2262 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03002263 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03002264
Johan Hedbergb68fda62014-08-11 22:06:40 +03002265 if (smp)
2266 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03002267
Johan Hedberg1e91c292014-08-18 20:33:29 +03002268 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03002269 }
2270
2271 return err;
2272}
2273
Johan Hedberg70db83c2014-08-08 09:37:16 +03002274static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2275 unsigned long hdr_len,
2276 unsigned long len, int nb)
2277{
2278 struct sk_buff *skb;
2279
2280 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2281 if (!skb)
2282 return ERR_PTR(-ENOMEM);
2283
2284 skb->priority = HCI_PRIO_MAX;
2285 bt_cb(skb)->chan = chan;
2286
2287 return skb;
2288}
2289
2290static const struct l2cap_ops smp_chan_ops = {
2291 .name = "Security Manager",
2292 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002293 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002294 .alloc_skb = smp_alloc_skb_cb,
2295 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002296 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002297
2298 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002299 .state_change = l2cap_chan_no_state_change,
2300 .close = l2cap_chan_no_close,
2301 .defer = l2cap_chan_no_defer,
2302 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002303 .set_shutdown = l2cap_chan_no_set_shutdown,
2304 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2305 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2306};
2307
2308static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2309{
2310 struct l2cap_chan *chan;
2311
2312 BT_DBG("pchan %p", pchan);
2313
2314 chan = l2cap_chan_create();
2315 if (!chan)
2316 return NULL;
2317
2318 chan->chan_type = pchan->chan_type;
2319 chan->ops = &smp_chan_ops;
2320 chan->scid = pchan->scid;
2321 chan->dcid = chan->scid;
2322 chan->imtu = pchan->imtu;
2323 chan->omtu = pchan->omtu;
2324 chan->mode = pchan->mode;
2325
Johan Hedbergabe84902014-11-12 22:22:21 +02002326 /* Other L2CAP channels may request SMP routines in order to
2327 * change the security level. This means that the SMP channel
2328 * lock must be considered in its own category to avoid lockdep
2329 * warnings.
2330 */
2331 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2332
Johan Hedberg70db83c2014-08-08 09:37:16 +03002333 BT_DBG("created chan %p", chan);
2334
2335 return chan;
2336}
2337
2338static const struct l2cap_ops smp_root_chan_ops = {
2339 .name = "Security Manager Root",
2340 .new_connection = smp_new_conn_cb,
2341
2342 /* None of these are implemented for the root channel */
2343 .close = l2cap_chan_no_close,
2344 .alloc_skb = l2cap_chan_no_alloc_skb,
2345 .recv = l2cap_chan_no_recv,
2346 .state_change = l2cap_chan_no_state_change,
2347 .teardown = l2cap_chan_no_teardown,
2348 .ready = l2cap_chan_no_ready,
2349 .defer = l2cap_chan_no_defer,
2350 .suspend = l2cap_chan_no_suspend,
2351 .resume = l2cap_chan_no_resume,
2352 .set_shutdown = l2cap_chan_no_set_shutdown,
2353 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2354 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2355};
2356
Johan Hedberg711eafe2014-08-08 09:32:52 +03002357int smp_register(struct hci_dev *hdev)
2358{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002359 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002360 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002361
Johan Hedberg711eafe2014-08-08 09:32:52 +03002362 BT_DBG("%s", hdev->name);
2363
Johan Hedbergadae20c2014-11-13 14:37:48 +02002364 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002365 if (IS_ERR(tfm_aes)) {
2366 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002367 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03002368 return err;
2369 }
2370
Johan Hedberg70db83c2014-08-08 09:37:16 +03002371 chan = l2cap_chan_create();
2372 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002373 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002374 return -ENOMEM;
2375 }
2376
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002377 chan->data = tfm_aes;
2378
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002379 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002380
2381 l2cap_chan_set_defaults(chan);
2382
2383 bacpy(&chan->src, &hdev->bdaddr);
2384 chan->src_type = BDADDR_LE_PUBLIC;
2385 chan->state = BT_LISTEN;
2386 chan->mode = L2CAP_MODE_BASIC;
2387 chan->imtu = L2CAP_DEFAULT_MTU;
2388 chan->ops = &smp_root_chan_ops;
2389
Johan Hedbergabe84902014-11-12 22:22:21 +02002390 /* Set correct nesting level for a parent/listening channel */
2391 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2392
Johan Hedberg70db83c2014-08-08 09:37:16 +03002393 hdev->smp_data = chan;
2394
Johan Hedberg711eafe2014-08-08 09:32:52 +03002395 return 0;
2396}
2397
2398void smp_unregister(struct hci_dev *hdev)
2399{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002400 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002401 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002402
2403 if (!chan)
2404 return;
2405
2406 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002407
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002408 tfm_aes = chan->data;
2409 if (tfm_aes) {
2410 chan->data = NULL;
2411 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002412 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03002413
2414 hdev->smp_data = NULL;
2415 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002416}