blob: 1201670afe381ac7e637137963dfd78e708623bb [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
32#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030033
Johan Hedbergb28b4942014-09-05 22:19:55 +030034#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
35#define SMP_DISALLOW_CMD(smp, code) clear_bit(code, &smp->allow_cmd)
36
Marcel Holtmann17b02e62012-03-01 14:32:37 -080037#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030038
Johan Hedberg065a13e2012-10-11 16:26:06 +020039#define AUTH_REQ_MASK 0x07
Johan Hedberg88d3a8a2014-09-05 22:19:53 +030040#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020041
Johan Hedberg533e35d2014-06-16 19:25:18 +030042enum {
43 SMP_FLAG_TK_VALID,
44 SMP_FLAG_CFM_PENDING,
45 SMP_FLAG_MITM_AUTH,
46 SMP_FLAG_COMPLETE,
47 SMP_FLAG_INITIATOR,
48};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030049
50struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030051 struct l2cap_conn *conn;
52 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030053 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030054
Johan Hedberg4bc58f52014-05-20 09:45:47 +030055 u8 preq[7]; /* SMP Pairing Request */
56 u8 prsp[7]; /* SMP Pairing Response */
57 u8 prnd[16]; /* SMP Pairing Random (local) */
58 u8 rrnd[16]; /* SMP Pairing Random (remote) */
59 u8 pcnf[16]; /* SMP Pairing Confirm */
60 u8 tk[16]; /* SMP Temporary Key */
61 u8 enc_key_size;
62 u8 remote_key_dist;
63 bdaddr_t id_addr;
64 u8 id_addr_type;
65 u8 irk[16];
66 struct smp_csrk *csrk;
67 struct smp_csrk *slave_csrk;
68 struct smp_ltk *ltk;
69 struct smp_ltk *slave_ltk;
70 struct smp_irk *remote_irk;
Johan Hedberg4a74d652014-05-20 09:45:50 +030071 unsigned long flags;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030072
73 struct crypto_blkcipher *tfm_aes;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030074};
75
Johan Hedberg8a2936f2014-06-16 19:25:19 +030076static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030077{
Johan Hedberg8a2936f2014-06-16 19:25:19 +030078 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030079
Johan Hedberg8a2936f2014-06-16 19:25:19 +030080 for (i = 0; i < len; i++)
81 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030082}
83
84static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
85{
86 struct blkcipher_desc desc;
87 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +020088 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +020089 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030090
91 if (tfm == NULL) {
92 BT_ERR("tfm %p", tfm);
93 return -EINVAL;
94 }
95
96 desc.tfm = tfm;
97 desc.flags = 0;
98
Johan Hedberg943a7322014-03-18 12:58:24 +020099 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300100 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200101
102 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300103 if (err) {
104 BT_ERR("cipher setkey failed: %d", err);
105 return err;
106 }
107
Johan Hedberg943a7322014-03-18 12:58:24 +0200108 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300109 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200110
111 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300112
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300113 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
114 if (err)
115 BT_ERR("Encrypt data error %d", err);
116
Johan Hedberg943a7322014-03-18 12:58:24 +0200117 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300118 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200119
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300120 return err;
121}
122
Johan Hedberg60478052014-02-18 10:19:31 +0200123static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
124{
Johan Hedberg943a7322014-03-18 12:58:24 +0200125 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200126 int err;
127
128 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200129 memcpy(_res, r, 3);
130 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200131
Johan Hedberg943a7322014-03-18 12:58:24 +0200132 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200133 if (err) {
134 BT_ERR("Encrypt error");
135 return err;
136 }
137
138 /* The output of the random address function ah is:
139 * ah(h, r) = e(k, r') mod 2^24
140 * The output of the security function e is then truncated to 24 bits
141 * by taking the least significant 24 bits of the output of e as the
142 * result of ah.
143 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200144 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200145
146 return 0;
147}
148
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300149bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200150{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300151 struct l2cap_chan *chan = hdev->smp_data;
152 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200153 u8 hash[3];
154 int err;
155
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300156 if (!chan || !chan->data)
157 return false;
158
159 tfm = chan->data;
160
Johan Hedberg60478052014-02-18 10:19:31 +0200161 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
162
163 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
164 if (err)
165 return false;
166
167 return !memcmp(bdaddr->b, hash, 3);
168}
169
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300170int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200171{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300172 struct l2cap_chan *chan = hdev->smp_data;
173 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200174 int err;
175
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300176 if (!chan || !chan->data)
177 return -EOPNOTSUPP;
178
179 tfm = chan->data;
180
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200181 get_random_bytes(&rpa->b[3], 3);
182
183 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
184 rpa->b[5] |= 0x40; /* Set second most significant bit */
185
186 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
187 if (err < 0)
188 return err;
189
190 BT_DBG("RPA %pMR", rpa);
191
192 return 0;
193}
194
Johan Hedbergec70f362014-06-27 14:23:04 +0300195static int smp_c1(struct smp_chan *smp, u8 k[16], u8 r[16], u8 preq[7],
196 u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, bdaddr_t *ra,
197 u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300198{
Johan Hedbergec70f362014-06-27 14:23:04 +0300199 struct hci_dev *hdev = smp->conn->hcon->hdev;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300200 u8 p1[16], p2[16];
201 int err;
202
Johan Hedbergec70f362014-06-27 14:23:04 +0300203 BT_DBG("%s", hdev->name);
204
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300205 memset(p1, 0, 16);
206
207 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200208 p1[0] = _iat;
209 p1[1] = _rat;
210 memcpy(p1 + 2, preq, 7);
211 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300212
213 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200214 memcpy(p2, ra, 6);
215 memcpy(p2 + 6, ia, 6);
216 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300217
218 /* res = r XOR p1 */
219 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
220
221 /* res = e(k, res) */
Johan Hedbergec70f362014-06-27 14:23:04 +0300222 err = smp_e(smp->tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300223 if (err) {
224 BT_ERR("Encrypt data error");
225 return err;
226 }
227
228 /* res = res XOR p2 */
229 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
230
231 /* res = e(k, res) */
Johan Hedbergec70f362014-06-27 14:23:04 +0300232 err = smp_e(smp->tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300233 if (err)
234 BT_ERR("Encrypt data error");
235
236 return err;
237}
238
Johan Hedbergec70f362014-06-27 14:23:04 +0300239static int smp_s1(struct smp_chan *smp, u8 k[16], u8 r1[16], u8 r2[16],
240 u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300241{
Johan Hedbergec70f362014-06-27 14:23:04 +0300242 struct hci_dev *hdev = smp->conn->hcon->hdev;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300243 int err;
244
Johan Hedbergec70f362014-06-27 14:23:04 +0300245 BT_DBG("%s", hdev->name);
246
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300247 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200248 memcpy(_r, r2, 8);
249 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300250
Johan Hedbergec70f362014-06-27 14:23:04 +0300251 err = smp_e(smp->tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300252 if (err)
253 BT_ERR("Encrypt data error");
254
255 return err;
256}
257
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300258static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
259{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300260 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300261 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300262 struct kvec iv[2];
263 struct msghdr msg;
264
265 if (!chan)
266 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300267
268 BT_DBG("code 0x%2.2x", code);
269
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300270 iv[0].iov_base = &code;
271 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300272
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300273 iv[1].iov_base = data;
274 iv[1].iov_len = len;
275
276 memset(&msg, 0, sizeof(msg));
277
278 msg.msg_iov = (struct iovec *) &iv;
279 msg.msg_iovlen = 2;
280
281 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300282
Johan Hedbergb68fda62014-08-11 22:06:40 +0300283 if (!chan->data)
284 return;
285
286 smp = chan->data;
287
288 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300289 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300290}
291
Brian Gix2b64d152011-12-21 16:12:12 -0800292static __u8 authreq_to_seclevel(__u8 authreq)
293{
294 if (authreq & SMP_AUTH_MITM)
295 return BT_SECURITY_HIGH;
296 else
297 return BT_SECURITY_MEDIUM;
298}
299
300static __u8 seclevel_to_authreq(__u8 sec_level)
301{
302 switch (sec_level) {
303 case BT_SECURITY_HIGH:
304 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
305 case BT_SECURITY_MEDIUM:
306 return SMP_AUTH_BONDING;
307 default:
308 return SMP_AUTH_NONE;
309 }
310}
311
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300312static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700313 struct smp_cmd_pairing *req,
314 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300315{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300316 struct l2cap_chan *chan = conn->smp;
317 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200318 struct hci_conn *hcon = conn->hcon;
319 struct hci_dev *hdev = hcon->hdev;
320 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300321
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300322 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700323 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
324 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300325 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800326 } else {
327 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300328 }
329
Johan Hedbergfd349c02014-02-18 10:19:36 +0200330 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
331 remote_dist |= SMP_DIST_ID_KEY;
332
Johan Hedberg863efaf2014-02-22 19:06:32 +0200333 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
334 local_dist |= SMP_DIST_ID_KEY;
335
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300336 if (rsp == NULL) {
337 req->io_capability = conn->hcon->io_capability;
338 req->oob_flag = SMP_OOB_NOT_PRESENT;
339 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200340 req->init_key_dist = local_dist;
341 req->resp_key_dist = remote_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200342 req->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200343
344 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300345 return;
346 }
347
348 rsp->io_capability = conn->hcon->io_capability;
349 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
350 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200351 rsp->init_key_dist = req->init_key_dist & remote_dist;
352 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200353 rsp->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200354
355 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300356}
357
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300358static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
359{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300360 struct l2cap_chan *chan = conn->smp;
361 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300362
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300363 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700364 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300365 return SMP_ENC_KEY_SIZE;
366
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300367 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300368
369 return 0;
370}
371
Johan Hedberg6f48e262014-08-11 22:06:44 +0300372static void smp_chan_destroy(struct l2cap_conn *conn)
373{
374 struct l2cap_chan *chan = conn->smp;
375 struct smp_chan *smp = chan->data;
376 bool complete;
377
378 BUG_ON(!smp);
379
380 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300381
Johan Hedberg6f48e262014-08-11 22:06:44 +0300382 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
383 mgmt_smp_complete(conn->hcon, complete);
384
385 kfree(smp->csrk);
386 kfree(smp->slave_csrk);
387
388 crypto_free_blkcipher(smp->tfm_aes);
389
390 /* If pairing failed clean up any keys we might have */
391 if (!complete) {
392 if (smp->ltk) {
393 list_del(&smp->ltk->list);
394 kfree(smp->ltk);
395 }
396
397 if (smp->slave_ltk) {
398 list_del(&smp->slave_ltk->list);
399 kfree(smp->slave_ltk);
400 }
401
402 if (smp->remote_irk) {
403 list_del(&smp->remote_irk->list);
404 kfree(smp->remote_irk);
405 }
406 }
407
408 chan->data = NULL;
409 kfree(smp);
410 hci_conn_drop(conn->hcon);
411}
412
Johan Hedberg84794e12013-11-06 11:24:57 +0200413static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800414{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200415 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300416 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200417
Johan Hedberg84794e12013-11-06 11:24:57 +0200418 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800419 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700420 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800421
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700422 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
423 mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type,
424 HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300425
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300426 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300427 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800428}
429
Brian Gix2b64d152011-12-21 16:12:12 -0800430#define JUST_WORKS 0x00
431#define JUST_CFM 0x01
432#define REQ_PASSKEY 0x02
433#define CFM_PASSKEY 0x03
434#define REQ_OOB 0x04
435#define OVERLAP 0xFF
436
437static const u8 gen_method[5][5] = {
438 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
439 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
440 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
441 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
442 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
443};
444
Johan Hedberg581370c2014-06-17 13:07:38 +0300445static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
446{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300447 /* If either side has unknown io_caps, use JUST_CFM (which gets
448 * converted later to JUST_WORKS if we're initiators.
449 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300450 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
451 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300452 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300453
454 return gen_method[remote_io][local_io];
455}
456
Brian Gix2b64d152011-12-21 16:12:12 -0800457static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
458 u8 local_io, u8 remote_io)
459{
460 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300461 struct l2cap_chan *chan = conn->smp;
462 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800463 u8 method;
464 u32 passkey = 0;
465 int ret = 0;
466
467 /* Initialize key for JUST WORKS */
468 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300469 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800470
471 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
472
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300473 /* If neither side wants MITM, either "just" confirm an incoming
474 * request or use just-works for outgoing ones. The JUST_CFM
475 * will be converted to JUST_WORKS if necessary later in this
476 * function. If either side has MITM look up the method from the
477 * table.
478 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300479 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300480 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800481 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300482 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800483
Johan Hedberga82505c2014-03-24 14:39:07 +0200484 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300485 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200486 method = JUST_WORKS;
487
Johan Hedberg02f3e252014-07-16 15:09:13 +0300488 /* Don't bother user space with no IO capabilities */
489 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
490 method = JUST_WORKS;
491
Brian Gix2b64d152011-12-21 16:12:12 -0800492 /* If Just Works, Continue with Zero TK */
493 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300494 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800495 return 0;
496 }
497
498 /* Not Just Works/Confirm results in MITM Authentication */
499 if (method != JUST_CFM)
Johan Hedberg4a74d652014-05-20 09:45:50 +0300500 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800501
502 /* If both devices have Keyoard-Display I/O, the master
503 * Confirms and the slave Enters the passkey.
504 */
505 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300506 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800507 method = CFM_PASSKEY;
508 else
509 method = REQ_PASSKEY;
510 }
511
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200512 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800513 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200514 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800515 get_random_bytes(&passkey, sizeof(passkey));
516 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200517 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800518 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300519 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800520 }
521
522 hci_dev_lock(hcon->hdev);
523
524 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700525 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200526 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200527 else if (method == JUST_CFM)
528 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
529 hcon->type, hcon->dst_type,
530 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800531 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200532 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200533 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200534 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800535
536 hci_dev_unlock(hcon->hdev);
537
538 return ret;
539}
540
Johan Hedberg1cc61142014-05-20 09:45:52 +0300541static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300542{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300543 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300544 struct smp_cmd_pairing_confirm cp;
545 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300546
547 BT_DBG("conn %p", conn);
548
Johan Hedbergec70f362014-06-27 14:23:04 +0300549 ret = smp_c1(smp, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200550 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200551 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
552 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300553 if (ret)
554 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300555
Johan Hedberg4a74d652014-05-20 09:45:50 +0300556 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800557
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300558 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
559
Johan Hedbergb28b4942014-09-05 22:19:55 +0300560 if (conn->hcon->out)
561 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
562 else
563 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
564
Johan Hedberg1cc61142014-05-20 09:45:52 +0300565 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300566}
567
Johan Hedberg861580a2014-05-20 09:45:51 +0300568static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300569{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300570 struct l2cap_conn *conn = smp->conn;
571 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300572 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300573 int ret;
574
Johan Hedbergec70f362014-06-27 14:23:04 +0300575 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300576 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300577
578 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
579
Johan Hedbergec70f362014-06-27 14:23:04 +0300580 ret = smp_c1(smp, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200581 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200582 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300583 if (ret)
584 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300585
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300586 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
587 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300588 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300589 }
590
591 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800592 u8 stk[16];
593 __le64 rand = 0;
594 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300595
Johan Hedbergec70f362014-06-27 14:23:04 +0300596 smp_s1(smp, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300597
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300598 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300599 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300600
Johan Hedberg861580a2014-05-20 09:45:51 +0300601 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
602 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300603
604 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300605 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300606 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300607 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300608 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800609 __le64 rand = 0;
610 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300611
Johan Hedberg943a7322014-03-18 12:58:24 +0200612 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
613 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300614
Johan Hedbergec70f362014-06-27 14:23:04 +0300615 smp_s1(smp, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300616
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300617 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700618 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300619
Johan Hedbergfff34902014-06-10 15:19:50 +0300620 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
621 auth = 1;
622 else
623 auth = 0;
624
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300625 /* Even though there's no _SLAVE suffix this is the
626 * slave STK we're adding for later lookup (the master
627 * STK never needs to be stored).
628 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700629 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300630 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300631 }
632
Johan Hedberg861580a2014-05-20 09:45:51 +0300633 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300634}
635
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300636static void smp_notify_keys(struct l2cap_conn *conn)
637{
638 struct l2cap_chan *chan = conn->smp;
639 struct smp_chan *smp = chan->data;
640 struct hci_conn *hcon = conn->hcon;
641 struct hci_dev *hdev = hcon->hdev;
642 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
643 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
644 bool persistent;
645
646 if (smp->remote_irk) {
647 mgmt_new_irk(hdev, smp->remote_irk);
648 /* Now that user space can be considered to know the
649 * identity address track the connection based on it
650 * from now on.
651 */
652 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
653 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300654 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300655
656 /* When receiving an indentity resolving key for
657 * a remote device that does not use a resolvable
658 * private address, just remove the key so that
659 * it is possible to use the controller white
660 * list for scanning.
661 *
662 * Userspace will have been told to not store
663 * this key at this point. So it is safe to
664 * just remove it.
665 */
666 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
667 list_del(&smp->remote_irk->list);
668 kfree(smp->remote_irk);
669 smp->remote_irk = NULL;
670 }
671 }
672
673 /* The LTKs and CSRKs should be persistent only if both sides
674 * had the bonding bit set in their authentication requests.
675 */
676 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
677
678 if (smp->csrk) {
679 smp->csrk->bdaddr_type = hcon->dst_type;
680 bacpy(&smp->csrk->bdaddr, &hcon->dst);
681 mgmt_new_csrk(hdev, smp->csrk, persistent);
682 }
683
684 if (smp->slave_csrk) {
685 smp->slave_csrk->bdaddr_type = hcon->dst_type;
686 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
687 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
688 }
689
690 if (smp->ltk) {
691 smp->ltk->bdaddr_type = hcon->dst_type;
692 bacpy(&smp->ltk->bdaddr, &hcon->dst);
693 mgmt_new_ltk(hdev, smp->ltk, persistent);
694 }
695
696 if (smp->slave_ltk) {
697 smp->slave_ltk->bdaddr_type = hcon->dst_type;
698 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
699 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
700 }
701}
702
Johan Hedbergb28b4942014-09-05 22:19:55 +0300703static void smp_allow_key_dist(struct smp_chan *smp)
704{
705 /* Allow the first expected phase 3 PDU. The rest of the PDUs
706 * will be allowed in each PDU handler to ensure we receive
707 * them in the correct order.
708 */
709 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
710 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
711 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
712 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
713 else if (smp->remote_key_dist & SMP_DIST_SIGN)
714 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
715}
716
Johan Hedbergd6268e82014-09-05 22:19:51 +0300717static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300718{
719 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300720 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300721 struct hci_conn *hcon = conn->hcon;
722 struct hci_dev *hdev = hcon->hdev;
723 __u8 *keydist;
724
725 BT_DBG("conn %p", conn);
726
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300727 rsp = (void *) &smp->prsp[1];
728
729 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300730 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
731 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300732 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300733 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300734
735 req = (void *) &smp->preq[1];
736
737 if (hcon->out) {
738 keydist = &rsp->init_key_dist;
739 *keydist &= req->init_key_dist;
740 } else {
741 keydist = &rsp->resp_key_dist;
742 *keydist &= req->resp_key_dist;
743 }
744
745 BT_DBG("keydist 0x%x", *keydist);
746
747 if (*keydist & SMP_DIST_ENC_KEY) {
748 struct smp_cmd_encrypt_info enc;
749 struct smp_cmd_master_ident ident;
750 struct smp_ltk *ltk;
751 u8 authenticated;
752 __le16 ediv;
753 __le64 rand;
754
755 get_random_bytes(enc.ltk, sizeof(enc.ltk));
756 get_random_bytes(&ediv, sizeof(ediv));
757 get_random_bytes(&rand, sizeof(rand));
758
759 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
760
761 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
762 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
763 SMP_LTK_SLAVE, authenticated, enc.ltk,
764 smp->enc_key_size, ediv, rand);
765 smp->slave_ltk = ltk;
766
767 ident.ediv = ediv;
768 ident.rand = rand;
769
770 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
771
772 *keydist &= ~SMP_DIST_ENC_KEY;
773 }
774
775 if (*keydist & SMP_DIST_ID_KEY) {
776 struct smp_cmd_ident_addr_info addrinfo;
777 struct smp_cmd_ident_info idinfo;
778
779 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
780
781 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
782
783 /* The hci_conn contains the local identity address
784 * after the connection has been established.
785 *
786 * This is true even when the connection has been
787 * established using a resolvable random address.
788 */
789 bacpy(&addrinfo.bdaddr, &hcon->src);
790 addrinfo.addr_type = hcon->src_type;
791
792 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
793 &addrinfo);
794
795 *keydist &= ~SMP_DIST_ID_KEY;
796 }
797
798 if (*keydist & SMP_DIST_SIGN) {
799 struct smp_cmd_sign_info sign;
800 struct smp_csrk *csrk;
801
802 /* Generate a new random key */
803 get_random_bytes(sign.csrk, sizeof(sign.csrk));
804
805 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
806 if (csrk) {
807 csrk->master = 0x00;
808 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
809 }
810 smp->slave_csrk = csrk;
811
812 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
813
814 *keydist &= ~SMP_DIST_SIGN;
815 }
816
817 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300818 if (smp->remote_key_dist & KEY_DIST_MASK) {
819 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300820 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300821 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300822
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300823 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
824 smp_notify_keys(conn);
825
826 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300827}
828
Johan Hedbergb68fda62014-08-11 22:06:40 +0300829static void smp_timeout(struct work_struct *work)
830{
831 struct smp_chan *smp = container_of(work, struct smp_chan,
832 security_timer.work);
833 struct l2cap_conn *conn = smp->conn;
834
835 BT_DBG("conn %p", conn);
836
Johan Hedberg1e91c292014-08-18 20:33:29 +0300837 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300838}
839
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300840static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
841{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300842 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300843 struct smp_chan *smp;
844
Marcel Holtmannf1560462013-10-13 05:43:25 -0700845 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300846 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300847 return NULL;
848
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300849 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
850 if (IS_ERR(smp->tfm_aes)) {
851 BT_ERR("Unable to create ECB crypto context");
852 kfree(smp);
853 return NULL;
854 }
855
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300856 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300857 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300858
Johan Hedbergb28b4942014-09-05 22:19:55 +0300859 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
860
Johan Hedbergb68fda62014-08-11 22:06:40 +0300861 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
862
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300863 hci_conn_hold(conn->hcon);
864
865 return smp;
866}
867
Brian Gix2b64d152011-12-21 16:12:12 -0800868int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
869{
Johan Hedbergb10e8012014-06-27 14:23:07 +0300870 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300871 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -0800872 struct smp_chan *smp;
873 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300874 int err;
Brian Gix2b64d152011-12-21 16:12:12 -0800875
876 BT_DBG("");
877
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300878 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -0800879 return -ENOTCONN;
880
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300881 chan = conn->smp;
882 if (!chan)
883 return -ENOTCONN;
884
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300885 l2cap_chan_lock(chan);
886 if (!chan->data) {
887 err = -ENOTCONN;
888 goto unlock;
889 }
890
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300891 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800892
893 switch (mgmt_op) {
894 case MGMT_OP_USER_PASSKEY_REPLY:
895 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +0200896 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800897 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +0200898 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800899 /* Fall Through */
900 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +0300901 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800902 break;
903 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
904 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +0200905 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300906 err = 0;
907 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800908 default:
Johan Hedberg84794e12013-11-06 11:24:57 +0200909 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300910 err = -EOPNOTSUPP;
911 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800912 }
913
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300914 err = 0;
915
Brian Gix2b64d152011-12-21 16:12:12 -0800916 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +0300917 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
918 u8 rsp = smp_confirm(smp);
919 if (rsp)
920 smp_failure(conn, rsp);
921 }
Brian Gix2b64d152011-12-21 16:12:12 -0800922
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300923unlock:
924 l2cap_chan_unlock(chan);
925 return err;
Brian Gix2b64d152011-12-21 16:12:12 -0800926}
927
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300928static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300929{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300930 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300931 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +0300932 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300933 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +0300934 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300935 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300936
937 BT_DBG("conn %p", conn);
938
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200939 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300940 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200941
Johan Hedberg40bef302014-07-16 11:42:27 +0300942 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -0800943 return SMP_CMD_NOTSUPP;
944
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300945 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300946 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300947 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300948 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300949
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +0300950 if (!smp)
951 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300952
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300953 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergb3c64102014-07-10 11:02:07 +0300954 (req->auth_req & SMP_AUTH_BONDING))
955 return SMP_PAIRING_NOTSUPP;
956
Johan Hedbergb28b4942014-09-05 22:19:55 +0300957 SMP_DISALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
958
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300959 smp->preq[0] = SMP_CMD_PAIRING_REQ;
960 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300961 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300962
Brian Gix2b64d152011-12-21 16:12:12 -0800963 /* We didn't start the pairing, so match remote */
Johan Hedberg1ef35822014-05-20 09:45:48 +0300964 auth = req->auth_req;
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300965
Johan Hedbergc7262e72014-06-17 13:07:37 +0300966 sec_level = authreq_to_seclevel(auth);
967 if (sec_level > conn->hcon->pending_sec_level)
968 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +0200969
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300970 /* If we need MITM check that it can be acheived */
971 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
972 u8 method;
973
974 method = get_auth_method(smp, conn->hcon->io_capability,
975 req->io_capability);
976 if (method == JUST_WORKS || method == JUST_CFM)
977 return SMP_AUTH_REQUIREMENTS;
978 }
979
Brian Gix2b64d152011-12-21 16:12:12 -0800980 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300981
982 key_size = min(req->max_key_size, rsp.max_key_size);
983 if (check_enc_key_size(conn, key_size))
984 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300985
Johan Hedberge84a6b12013-12-02 10:49:03 +0200986 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300987
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300988 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
989 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -0300990
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300991 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedbergb28b4942014-09-05 22:19:55 +0300992 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300993
Brian Gix2b64d152011-12-21 16:12:12 -0800994 /* Request setup of TK */
995 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
996 if (ret)
997 return SMP_UNSPECIFIED;
998
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300999 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001000}
1001
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001002static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001003{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001004 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001005 struct l2cap_chan *chan = conn->smp;
1006 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001007 u8 key_size, auth = SMP_AUTH_NONE;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001008 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001009
1010 BT_DBG("conn %p", conn);
1011
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001012 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001013 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001014
Johan Hedberg40bef302014-07-16 11:42:27 +03001015 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001016 return SMP_CMD_NOTSUPP;
1017
Johan Hedbergb28b4942014-09-05 22:19:55 +03001018 SMP_DISALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
1019
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001020 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001021
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001022 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001023
1024 key_size = min(req->max_key_size, rsp->max_key_size);
1025 if (check_enc_key_size(conn, key_size))
1026 return SMP_ENC_KEY_SIZE;
1027
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001028 /* If we need MITM check that it can be acheived */
1029 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1030 u8 method;
1031
1032 method = get_auth_method(smp, req->io_capability,
1033 rsp->io_capability);
1034 if (method == JUST_WORKS || method == JUST_CFM)
1035 return SMP_AUTH_REQUIREMENTS;
1036 }
1037
Johan Hedberge84a6b12013-12-02 10:49:03 +02001038 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001039
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001040 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1041 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001042
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001043 /* Update remote key distribution in case the remote cleared
1044 * some bits that we had enabled in our request.
1045 */
1046 smp->remote_key_dist &= rsp->resp_key_dist;
1047
Brian Gix2b64d152011-12-21 16:12:12 -08001048 if ((req->auth_req & SMP_AUTH_BONDING) &&
Marcel Holtmannf1560462013-10-13 05:43:25 -07001049 (rsp->auth_req & SMP_AUTH_BONDING))
Brian Gix2b64d152011-12-21 16:12:12 -08001050 auth = SMP_AUTH_BONDING;
1051
1052 auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM;
1053
Johan Hedberg476585e2012-06-06 18:54:15 +08001054 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001055 if (ret)
1056 return SMP_UNSPECIFIED;
1057
Johan Hedberg4a74d652014-05-20 09:45:50 +03001058 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001059
1060 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001061 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001062 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001063
1064 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001065}
1066
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001067static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001068{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001069 struct l2cap_chan *chan = conn->smp;
1070 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001071
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001072 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1073
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001074 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001075 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001076
Johan Hedbergb28b4942014-09-05 22:19:55 +03001077 SMP_DISALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1078
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001079 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1080 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001081
Johan Hedbergb28b4942014-09-05 22:19:55 +03001082 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001083 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1084 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001085 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1086 return 0;
1087 }
1088
1089 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001090 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001091 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001092 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001093
1094 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001095}
1096
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001097static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001098{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001099 struct l2cap_chan *chan = conn->smp;
1100 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001101
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001102 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001103
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001104 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001105 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001106
Johan Hedbergb28b4942014-09-05 22:19:55 +03001107 SMP_DISALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1108
Johan Hedberg943a7322014-03-18 12:58:24 +02001109 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001110 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001111
Johan Hedberg861580a2014-05-20 09:45:51 +03001112 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001113}
1114
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001115static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001116{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001117 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001118 struct hci_conn *hcon = conn->hcon;
1119
Johan Hedberg98a0b842014-01-30 19:40:00 -08001120 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001121 hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001122 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001123 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001124
Johan Hedberg4dab7862012-06-07 14:58:37 +08001125 if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001126 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001127
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001128 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001129 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001130
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001131 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1132 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001133
Johan Hedbergfe59a052014-07-01 19:14:12 +03001134 /* We never store STKs for master role, so clear this flag */
1135 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1136
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001137 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001138}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001139
Johan Hedberg854f4722014-07-01 18:40:20 +03001140bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level)
1141{
1142 if (sec_level == BT_SECURITY_LOW)
1143 return true;
1144
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001145 /* If we're encrypted with an STK always claim insufficient
1146 * security. This way we allow the connection to be re-encrypted
1147 * with an LTK, even if the LTK provides the same level of
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001148 * security. Only exception is if we don't have an LTK (e.g.
1149 * because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001150 */
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001151 if (test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
1152 hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001153 hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001154 return false;
1155
Johan Hedberg854f4722014-07-01 18:40:20 +03001156 if (hcon->sec_level >= sec_level)
1157 return true;
1158
1159 return false;
1160}
1161
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001162static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001163{
1164 struct smp_cmd_security_req *rp = (void *) skb->data;
1165 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001166 struct hci_conn *hcon = conn->hcon;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001167 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001168 u8 sec_level;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001169
1170 BT_DBG("conn %p", conn);
1171
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001172 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001173 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001174
Johan Hedberg40bef302014-07-16 11:42:27 +03001175 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001176 return SMP_CMD_NOTSUPP;
1177
Johan Hedbergc7262e72014-06-17 13:07:37 +03001178 sec_level = authreq_to_seclevel(rp->auth_req);
Johan Hedberg854f4722014-07-01 18:40:20 +03001179 if (smp_sufficient_security(hcon, sec_level))
1180 return 0;
1181
Johan Hedbergc7262e72014-06-17 13:07:37 +03001182 if (sec_level > hcon->pending_sec_level)
1183 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001184
Johan Hedberg4dab7862012-06-07 14:58:37 +08001185 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001186 return 0;
1187
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001188 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001189 if (!smp)
1190 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001191
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001192 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedberg616d55b2014-07-29 14:18:48 +03001193 (rp->auth_req & SMP_AUTH_BONDING))
1194 return SMP_PAIRING_NOTSUPP;
1195
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001196 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001197
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001198 memset(&cp, 0, sizeof(cp));
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -03001199 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001200
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001201 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1202 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001203
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001204 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001205 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001206
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001207 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001208}
1209
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001210int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001211{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001212 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001213 struct l2cap_chan *chan = conn->smp;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001214 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001215 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001216 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001217
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001218 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1219
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001220 /* This may be NULL if there's an unexpected disconnection */
1221 if (!conn)
1222 return 1;
1223
Johan Hedberg757aee02013-04-24 13:05:32 +03001224 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001225 return 1;
1226
Johan Hedbergad32a2f2013-05-14 18:05:12 +03001227 if (smp_sufficient_security(hcon, sec_level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001228 return 1;
1229
Johan Hedbergc7262e72014-06-17 13:07:37 +03001230 if (sec_level > hcon->pending_sec_level)
1231 hcon->pending_sec_level = sec_level;
1232
Johan Hedberg40bef302014-07-16 11:42:27 +03001233 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001234 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1235 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001236
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001237 l2cap_chan_lock(chan);
1238
1239 /* If SMP is already in progress ignore this request */
1240 if (chan->data) {
1241 ret = 0;
1242 goto unlock;
1243 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001244
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001245 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001246 if (!smp) {
1247 ret = 1;
1248 goto unlock;
1249 }
Brian Gix2b64d152011-12-21 16:12:12 -08001250
1251 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001252
Johan Hedberg79897d22014-06-01 09:45:24 +03001253 /* Require MITM if IO Capability allows or the security level
1254 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001255 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001256 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001257 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001258 authreq |= SMP_AUTH_MITM;
1259
Johan Hedberg40bef302014-07-16 11:42:27 +03001260 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001261 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001262
Brian Gix2b64d152011-12-21 16:12:12 -08001263 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001264 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1265 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001266
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001267 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001268 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001269 } else {
1270 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001271 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001272 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001273 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001274 }
1275
Johan Hedberg4a74d652014-05-20 09:45:50 +03001276 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001277 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001278
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001279unlock:
1280 l2cap_chan_unlock(chan);
1281 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001282}
1283
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001284static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1285{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001286 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001287 struct l2cap_chan *chan = conn->smp;
1288 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001289
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001290 BT_DBG("conn %p", conn);
1291
1292 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001293 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001294
Johan Hedbergb28b4942014-09-05 22:19:55 +03001295 SMP_DISALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1296 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001297
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001298 skb_pull(skb, sizeof(*rp));
1299
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001300 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001301
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001302 return 0;
1303}
1304
1305static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1306{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001307 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001308 struct l2cap_chan *chan = conn->smp;
1309 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001310 struct hci_dev *hdev = conn->hcon->hdev;
1311 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001312 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001313 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001314
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001315 BT_DBG("conn %p", conn);
1316
1317 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001318 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001319
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001320 /* Mark the information as received */
1321 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1322
Johan Hedbergb28b4942014-09-05 22:19:55 +03001323 SMP_DISALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
1324 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1325 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1326
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001327 skb_pull(skb, sizeof(*rp));
1328
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001329 hci_dev_lock(hdev);
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001330 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001331 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001332 authenticated, smp->tk, smp->enc_key_size,
1333 rp->ediv, rp->rand);
1334 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001335 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001336 smp_distribute_keys(smp);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001337 hci_dev_unlock(hdev);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001338
1339 return 0;
1340}
1341
Johan Hedbergfd349c02014-02-18 10:19:36 +02001342static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1343{
1344 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001345 struct l2cap_chan *chan = conn->smp;
1346 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001347
1348 BT_DBG("");
1349
1350 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001351 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001352
Johan Hedbergb28b4942014-09-05 22:19:55 +03001353 SMP_DISALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1354 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001355
Johan Hedbergfd349c02014-02-18 10:19:36 +02001356 skb_pull(skb, sizeof(*info));
1357
1358 memcpy(smp->irk, info->irk, 16);
1359
1360 return 0;
1361}
1362
1363static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1364 struct sk_buff *skb)
1365{
1366 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001367 struct l2cap_chan *chan = conn->smp;
1368 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001369 struct hci_conn *hcon = conn->hcon;
1370 bdaddr_t rpa;
1371
1372 BT_DBG("");
1373
1374 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001375 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001376
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001377 /* Mark the information as received */
1378 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1379
Johan Hedbergb28b4942014-09-05 22:19:55 +03001380 SMP_DISALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
1381 if (smp->remote_key_dist & SMP_DIST_SIGN)
1382 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1383
Johan Hedbergfd349c02014-02-18 10:19:36 +02001384 skb_pull(skb, sizeof(*info));
1385
Johan Hedberg31dd6242014-06-27 14:23:02 +03001386 hci_dev_lock(hcon->hdev);
1387
Johan Hedberga9a58f82014-02-25 22:24:37 +02001388 /* Strictly speaking the Core Specification (4.1) allows sending
1389 * an empty address which would force us to rely on just the IRK
1390 * as "identity information". However, since such
1391 * implementations are not known of and in order to not over
1392 * complicate our implementation, simply pretend that we never
1393 * received an IRK for such a device.
1394 */
1395 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1396 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001397 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001398 }
1399
Johan Hedbergfd349c02014-02-18 10:19:36 +02001400 bacpy(&smp->id_addr, &info->bdaddr);
1401 smp->id_addr_type = info->addr_type;
1402
1403 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1404 bacpy(&rpa, &hcon->dst);
1405 else
1406 bacpy(&rpa, BDADDR_ANY);
1407
Johan Hedberg23d0e122014-02-19 14:57:46 +02001408 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1409 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001410
Johan Hedberg31dd6242014-06-27 14:23:02 +03001411distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001412 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1413 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001414
Johan Hedberg31dd6242014-06-27 14:23:02 +03001415 hci_dev_unlock(hcon->hdev);
1416
Johan Hedbergfd349c02014-02-18 10:19:36 +02001417 return 0;
1418}
1419
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001420static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1421{
1422 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001423 struct l2cap_chan *chan = conn->smp;
1424 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001425 struct hci_dev *hdev = conn->hcon->hdev;
1426 struct smp_csrk *csrk;
1427
1428 BT_DBG("conn %p", conn);
1429
1430 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001431 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001432
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001433 /* Mark the information as received */
1434 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1435
Johan Hedbergb28b4942014-09-05 22:19:55 +03001436 SMP_DISALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1437
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001438 skb_pull(skb, sizeof(*rp));
1439
1440 hci_dev_lock(hdev);
1441 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1442 if (csrk) {
1443 csrk->master = 0x01;
1444 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1445 }
1446 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001447 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001448 hci_dev_unlock(hdev);
1449
1450 return 0;
1451}
1452
Johan Hedberg4befb862014-08-11 22:06:38 +03001453static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001454{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001455 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001456 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001457 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001458 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001459 int err = 0;
1460
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001461 if (hcon->type != LE_LINK) {
1462 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001463 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001464 }
1465
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001466 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001467 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001468
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001469 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001470 reason = SMP_PAIRING_NOTSUPP;
1471 goto done;
1472 }
1473
Marcel Holtmann92381f52013-10-03 01:23:08 -07001474 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001475 skb_pull(skb, sizeof(code));
1476
Johan Hedbergb28b4942014-09-05 22:19:55 +03001477 smp = chan->data;
1478
1479 if (code > SMP_CMD_MAX)
1480 goto drop;
1481
1482 if (smp && !test_bit(code, &smp->allow_cmd))
1483 goto drop;
1484
1485 /* If we don't have a context the only allowed commands are
1486 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001487 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001488 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1489 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001490
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001491 switch (code) {
1492 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001493 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001494 break;
1495
1496 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001497 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001498 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001499 break;
1500
1501 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001502 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001503 break;
1504
1505 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001506 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001507 break;
1508
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001509 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001510 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001511 break;
1512
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001513 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001514 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001515 break;
1516
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001517 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001518 reason = smp_cmd_encrypt_info(conn, skb);
1519 break;
1520
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001521 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001522 reason = smp_cmd_master_ident(conn, skb);
1523 break;
1524
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001525 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001526 reason = smp_cmd_ident_info(conn, skb);
1527 break;
1528
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001529 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001530 reason = smp_cmd_ident_addr_info(conn, skb);
1531 break;
1532
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001533 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001534 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001535 break;
1536
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001537 default:
1538 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001539 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001540 goto done;
1541 }
1542
1543done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001544 if (!err) {
1545 if (reason)
1546 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001547 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001548 }
1549
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001550 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001551
1552drop:
1553 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
1554 code, &hcon->dst);
1555 kfree_skb(skb);
1556 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001557}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001558
Johan Hedberg70db83c2014-08-08 09:37:16 +03001559static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1560{
1561 struct l2cap_conn *conn = chan->conn;
1562
1563 BT_DBG("chan %p", chan);
1564
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001565 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001566 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001567
Johan Hedberg70db83c2014-08-08 09:37:16 +03001568 conn->smp = NULL;
1569 l2cap_chan_put(chan);
1570}
1571
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001572static void smp_resume_cb(struct l2cap_chan *chan)
1573{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001574 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001575 struct l2cap_conn *conn = chan->conn;
1576 struct hci_conn *hcon = conn->hcon;
1577
1578 BT_DBG("chan %p", chan);
1579
Johan Hedberg86d14072014-08-11 22:06:43 +03001580 if (!smp)
1581 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001582
Johan Hedberg84bc0db2014-09-05 22:19:49 +03001583 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1584 return;
1585
Johan Hedberg86d14072014-08-11 22:06:43 +03001586 cancel_delayed_work(&smp->security_timer);
1587
Johan Hedbergd6268e82014-09-05 22:19:51 +03001588 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001589}
1590
Johan Hedberg70db83c2014-08-08 09:37:16 +03001591static void smp_ready_cb(struct l2cap_chan *chan)
1592{
1593 struct l2cap_conn *conn = chan->conn;
1594
1595 BT_DBG("chan %p", chan);
1596
1597 conn->smp = chan;
1598 l2cap_chan_hold(chan);
1599}
1600
Johan Hedberg4befb862014-08-11 22:06:38 +03001601static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1602{
1603 int err;
1604
1605 BT_DBG("chan %p", chan);
1606
1607 err = smp_sig_channel(chan, skb);
1608 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001609 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001610
Johan Hedbergb68fda62014-08-11 22:06:40 +03001611 if (smp)
1612 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001613
Johan Hedberg1e91c292014-08-18 20:33:29 +03001614 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03001615 }
1616
1617 return err;
1618}
1619
Johan Hedberg70db83c2014-08-08 09:37:16 +03001620static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1621 unsigned long hdr_len,
1622 unsigned long len, int nb)
1623{
1624 struct sk_buff *skb;
1625
1626 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1627 if (!skb)
1628 return ERR_PTR(-ENOMEM);
1629
1630 skb->priority = HCI_PRIO_MAX;
1631 bt_cb(skb)->chan = chan;
1632
1633 return skb;
1634}
1635
1636static const struct l2cap_ops smp_chan_ops = {
1637 .name = "Security Manager",
1638 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001639 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001640 .alloc_skb = smp_alloc_skb_cb,
1641 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001642 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001643
1644 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001645 .state_change = l2cap_chan_no_state_change,
1646 .close = l2cap_chan_no_close,
1647 .defer = l2cap_chan_no_defer,
1648 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001649 .set_shutdown = l2cap_chan_no_set_shutdown,
1650 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1651 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1652};
1653
1654static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1655{
1656 struct l2cap_chan *chan;
1657
1658 BT_DBG("pchan %p", pchan);
1659
1660 chan = l2cap_chan_create();
1661 if (!chan)
1662 return NULL;
1663
1664 chan->chan_type = pchan->chan_type;
1665 chan->ops = &smp_chan_ops;
1666 chan->scid = pchan->scid;
1667 chan->dcid = chan->scid;
1668 chan->imtu = pchan->imtu;
1669 chan->omtu = pchan->omtu;
1670 chan->mode = pchan->mode;
1671
1672 BT_DBG("created chan %p", chan);
1673
1674 return chan;
1675}
1676
1677static const struct l2cap_ops smp_root_chan_ops = {
1678 .name = "Security Manager Root",
1679 .new_connection = smp_new_conn_cb,
1680
1681 /* None of these are implemented for the root channel */
1682 .close = l2cap_chan_no_close,
1683 .alloc_skb = l2cap_chan_no_alloc_skb,
1684 .recv = l2cap_chan_no_recv,
1685 .state_change = l2cap_chan_no_state_change,
1686 .teardown = l2cap_chan_no_teardown,
1687 .ready = l2cap_chan_no_ready,
1688 .defer = l2cap_chan_no_defer,
1689 .suspend = l2cap_chan_no_suspend,
1690 .resume = l2cap_chan_no_resume,
1691 .set_shutdown = l2cap_chan_no_set_shutdown,
1692 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1693 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1694};
1695
Johan Hedberg711eafe2014-08-08 09:32:52 +03001696int smp_register(struct hci_dev *hdev)
1697{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001698 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001699 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001700
Johan Hedberg711eafe2014-08-08 09:32:52 +03001701 BT_DBG("%s", hdev->name);
1702
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001703 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1704 if (IS_ERR(tfm_aes)) {
1705 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001706 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03001707 return err;
1708 }
1709
Johan Hedberg70db83c2014-08-08 09:37:16 +03001710 chan = l2cap_chan_create();
1711 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001712 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001713 return -ENOMEM;
1714 }
1715
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001716 chan->data = tfm_aes;
1717
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001718 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001719
1720 l2cap_chan_set_defaults(chan);
1721
1722 bacpy(&chan->src, &hdev->bdaddr);
1723 chan->src_type = BDADDR_LE_PUBLIC;
1724 chan->state = BT_LISTEN;
1725 chan->mode = L2CAP_MODE_BASIC;
1726 chan->imtu = L2CAP_DEFAULT_MTU;
1727 chan->ops = &smp_root_chan_ops;
1728
1729 hdev->smp_data = chan;
1730
Johan Hedberg711eafe2014-08-08 09:32:52 +03001731 return 0;
1732}
1733
1734void smp_unregister(struct hci_dev *hdev)
1735{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001736 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001737 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001738
1739 if (!chan)
1740 return;
1741
1742 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001743
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001744 tfm_aes = chan->data;
1745 if (tfm_aes) {
1746 chan->data = NULL;
1747 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001748 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03001749
1750 hdev->smp_data = NULL;
1751 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001752}