blob: 07ca4ce0943b5b3b6d976c7b2351343e20d83149 [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
Marcel Holtmann17b02e62012-03-01 14:32:37 -080034#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030035
Johan Hedberg065a13e2012-10-11 16:26:06 +020036#define AUTH_REQ_MASK 0x07
37
Johan Hedberg533e35d2014-06-16 19:25:18 +030038enum {
39 SMP_FLAG_TK_VALID,
40 SMP_FLAG_CFM_PENDING,
41 SMP_FLAG_MITM_AUTH,
42 SMP_FLAG_COMPLETE,
43 SMP_FLAG_INITIATOR,
44};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030045
46struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030047 struct l2cap_conn *conn;
48 struct delayed_work security_timer;
Johan Hedberg86d14072014-08-11 22:06:43 +030049 struct work_struct distribute_work;
Johan Hedbergb68fda62014-08-11 22:06:40 +030050
Johan Hedberg4bc58f52014-05-20 09:45:47 +030051 u8 preq[7]; /* SMP Pairing Request */
52 u8 prsp[7]; /* SMP Pairing Response */
53 u8 prnd[16]; /* SMP Pairing Random (local) */
54 u8 rrnd[16]; /* SMP Pairing Random (remote) */
55 u8 pcnf[16]; /* SMP Pairing Confirm */
56 u8 tk[16]; /* SMP Temporary Key */
57 u8 enc_key_size;
58 u8 remote_key_dist;
59 bdaddr_t id_addr;
60 u8 id_addr_type;
61 u8 irk[16];
62 struct smp_csrk *csrk;
63 struct smp_csrk *slave_csrk;
64 struct smp_ltk *ltk;
65 struct smp_ltk *slave_ltk;
66 struct smp_irk *remote_irk;
Johan Hedberg4a74d652014-05-20 09:45:50 +030067 unsigned long flags;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030068
69 struct crypto_blkcipher *tfm_aes;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030070};
71
Johan Hedberg8a2936f2014-06-16 19:25:19 +030072static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030073{
Johan Hedberg8a2936f2014-06-16 19:25:19 +030074 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030075
Johan Hedberg8a2936f2014-06-16 19:25:19 +030076 for (i = 0; i < len; i++)
77 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030078}
79
80static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
81{
82 struct blkcipher_desc desc;
83 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +020084 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +020085 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030086
87 if (tfm == NULL) {
88 BT_ERR("tfm %p", tfm);
89 return -EINVAL;
90 }
91
92 desc.tfm = tfm;
93 desc.flags = 0;
94
Johan Hedberg943a7322014-03-18 12:58:24 +020095 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +030096 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +020097
98 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030099 if (err) {
100 BT_ERR("cipher setkey failed: %d", err);
101 return err;
102 }
103
Johan Hedberg943a7322014-03-18 12:58:24 +0200104 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300105 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200106
107 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300108
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300109 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
110 if (err)
111 BT_ERR("Encrypt data error %d", err);
112
Johan Hedberg943a7322014-03-18 12:58:24 +0200113 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300114 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200115
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300116 return err;
117}
118
Johan Hedberg60478052014-02-18 10:19:31 +0200119static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
120{
Johan Hedberg943a7322014-03-18 12:58:24 +0200121 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200122 int err;
123
124 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200125 memcpy(_res, r, 3);
126 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200127
Johan Hedberg943a7322014-03-18 12:58:24 +0200128 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200129 if (err) {
130 BT_ERR("Encrypt error");
131 return err;
132 }
133
134 /* The output of the random address function ah is:
135 * ah(h, r) = e(k, r') mod 2^24
136 * The output of the security function e is then truncated to 24 bits
137 * by taking the least significant 24 bits of the output of e as the
138 * result of ah.
139 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200140 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200141
142 return 0;
143}
144
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300145bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200146{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300147 struct l2cap_chan *chan = hdev->smp_data;
148 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200149 u8 hash[3];
150 int err;
151
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300152 if (!chan || !chan->data)
153 return false;
154
155 tfm = chan->data;
156
Johan Hedberg60478052014-02-18 10:19:31 +0200157 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
158
159 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
160 if (err)
161 return false;
162
163 return !memcmp(bdaddr->b, hash, 3);
164}
165
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300166int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200167{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300168 struct l2cap_chan *chan = hdev->smp_data;
169 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200170 int err;
171
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300172 if (!chan || !chan->data)
173 return -EOPNOTSUPP;
174
175 tfm = chan->data;
176
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200177 get_random_bytes(&rpa->b[3], 3);
178
179 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
180 rpa->b[5] |= 0x40; /* Set second most significant bit */
181
182 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
183 if (err < 0)
184 return err;
185
186 BT_DBG("RPA %pMR", rpa);
187
188 return 0;
189}
190
Johan Hedbergec70f362014-06-27 14:23:04 +0300191static int smp_c1(struct smp_chan *smp, u8 k[16], u8 r[16], u8 preq[7],
192 u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, bdaddr_t *ra,
193 u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300194{
Johan Hedbergec70f362014-06-27 14:23:04 +0300195 struct hci_dev *hdev = smp->conn->hcon->hdev;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300196 u8 p1[16], p2[16];
197 int err;
198
Johan Hedbergec70f362014-06-27 14:23:04 +0300199 BT_DBG("%s", hdev->name);
200
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300201 memset(p1, 0, 16);
202
203 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200204 p1[0] = _iat;
205 p1[1] = _rat;
206 memcpy(p1 + 2, preq, 7);
207 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300208
209 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200210 memcpy(p2, ra, 6);
211 memcpy(p2 + 6, ia, 6);
212 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300213
214 /* res = r XOR p1 */
215 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
216
217 /* res = e(k, res) */
Johan Hedbergec70f362014-06-27 14:23:04 +0300218 err = smp_e(smp->tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300219 if (err) {
220 BT_ERR("Encrypt data error");
221 return err;
222 }
223
224 /* res = res XOR p2 */
225 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
226
227 /* res = e(k, res) */
Johan Hedbergec70f362014-06-27 14:23:04 +0300228 err = smp_e(smp->tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300229 if (err)
230 BT_ERR("Encrypt data error");
231
232 return err;
233}
234
Johan Hedbergec70f362014-06-27 14:23:04 +0300235static int smp_s1(struct smp_chan *smp, u8 k[16], u8 r1[16], u8 r2[16],
236 u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300237{
Johan Hedbergec70f362014-06-27 14:23:04 +0300238 struct hci_dev *hdev = smp->conn->hcon->hdev;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300239 int err;
240
Johan Hedbergec70f362014-06-27 14:23:04 +0300241 BT_DBG("%s", hdev->name);
242
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300243 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200244 memcpy(_r, r2, 8);
245 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300246
Johan Hedbergec70f362014-06-27 14:23:04 +0300247 err = smp_e(smp->tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300248 if (err)
249 BT_ERR("Encrypt data error");
250
251 return err;
252}
253
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300254static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
255{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300256 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300257 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300258 struct kvec iv[2];
259 struct msghdr msg;
260
261 if (!chan)
262 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300263
264 BT_DBG("code 0x%2.2x", code);
265
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300266 iv[0].iov_base = &code;
267 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300268
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300269 iv[1].iov_base = data;
270 iv[1].iov_len = len;
271
272 memset(&msg, 0, sizeof(msg));
273
274 msg.msg_iov = (struct iovec *) &iv;
275 msg.msg_iovlen = 2;
276
277 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300278
Johan Hedbergb68fda62014-08-11 22:06:40 +0300279 if (!chan->data)
280 return;
281
282 smp = chan->data;
283
284 cancel_delayed_work_sync(&smp->security_timer);
285 if (test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
286 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300287}
288
Brian Gix2b64d152011-12-21 16:12:12 -0800289static __u8 authreq_to_seclevel(__u8 authreq)
290{
291 if (authreq & SMP_AUTH_MITM)
292 return BT_SECURITY_HIGH;
293 else
294 return BT_SECURITY_MEDIUM;
295}
296
297static __u8 seclevel_to_authreq(__u8 sec_level)
298{
299 switch (sec_level) {
300 case BT_SECURITY_HIGH:
301 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
302 case BT_SECURITY_MEDIUM:
303 return SMP_AUTH_BONDING;
304 default:
305 return SMP_AUTH_NONE;
306 }
307}
308
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300309static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700310 struct smp_cmd_pairing *req,
311 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300312{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300313 struct l2cap_chan *chan = conn->smp;
314 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200315 struct hci_conn *hcon = conn->hcon;
316 struct hci_dev *hdev = hcon->hdev;
317 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300318
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300319 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700320 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
321 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300322 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800323 } else {
324 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300325 }
326
Johan Hedbergfd349c02014-02-18 10:19:36 +0200327 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
328 remote_dist |= SMP_DIST_ID_KEY;
329
Johan Hedberg863efaf2014-02-22 19:06:32 +0200330 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
331 local_dist |= SMP_DIST_ID_KEY;
332
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300333 if (rsp == NULL) {
334 req->io_capability = conn->hcon->io_capability;
335 req->oob_flag = SMP_OOB_NOT_PRESENT;
336 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200337 req->init_key_dist = local_dist;
338 req->resp_key_dist = remote_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200339 req->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200340
341 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300342 return;
343 }
344
345 rsp->io_capability = conn->hcon->io_capability;
346 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
347 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200348 rsp->init_key_dist = req->init_key_dist & remote_dist;
349 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200350 rsp->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200351
352 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300353}
354
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300355static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
356{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300357 struct l2cap_chan *chan = conn->smp;
358 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300359
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300360 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700361 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300362 return SMP_ENC_KEY_SIZE;
363
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300364 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300365
366 return 0;
367}
368
Johan Hedberg6f48e262014-08-11 22:06:44 +0300369static void smp_chan_destroy(struct l2cap_conn *conn)
370{
371 struct l2cap_chan *chan = conn->smp;
372 struct smp_chan *smp = chan->data;
373 bool complete;
374
375 BUG_ON(!smp);
376
377 cancel_delayed_work_sync(&smp->security_timer);
378 /* In case the timeout freed the SMP context */
379 if (!chan->data)
380 return;
381
382 if (work_pending(&smp->distribute_work)) {
383 cancel_work_sync(&smp->distribute_work);
384 if (!chan->data)
385 return;
386 }
387
388 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
389 mgmt_smp_complete(conn->hcon, complete);
390
391 kfree(smp->csrk);
392 kfree(smp->slave_csrk);
393
394 crypto_free_blkcipher(smp->tfm_aes);
395
396 /* If pairing failed clean up any keys we might have */
397 if (!complete) {
398 if (smp->ltk) {
399 list_del(&smp->ltk->list);
400 kfree(smp->ltk);
401 }
402
403 if (smp->slave_ltk) {
404 list_del(&smp->slave_ltk->list);
405 kfree(smp->slave_ltk);
406 }
407
408 if (smp->remote_irk) {
409 list_del(&smp->remote_irk->list);
410 kfree(smp->remote_irk);
411 }
412 }
413
414 chan->data = NULL;
415 kfree(smp);
416 hci_conn_drop(conn->hcon);
417}
418
Johan Hedberg84794e12013-11-06 11:24:57 +0200419static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800420{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200421 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300422 struct l2cap_chan *chan = conn->smp;
423 struct smp_chan *smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200424
Johan Hedberg84794e12013-11-06 11:24:57 +0200425 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800426 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700427 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800428
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700429 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
430 mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type,
431 HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300432
Johan Hedbergb68fda62014-08-11 22:06:40 +0300433 if (!chan->data)
434 return;
435
436 smp = chan->data;
437
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700438 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300439 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800440}
441
Brian Gix2b64d152011-12-21 16:12:12 -0800442#define JUST_WORKS 0x00
443#define JUST_CFM 0x01
444#define REQ_PASSKEY 0x02
445#define CFM_PASSKEY 0x03
446#define REQ_OOB 0x04
447#define OVERLAP 0xFF
448
449static const u8 gen_method[5][5] = {
450 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
451 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
452 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
453 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
454 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
455};
456
Johan Hedberg581370c2014-06-17 13:07:38 +0300457static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
458{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300459 /* If either side has unknown io_caps, use JUST_CFM (which gets
460 * converted later to JUST_WORKS if we're initiators.
461 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300462 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
463 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300464 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300465
466 return gen_method[remote_io][local_io];
467}
468
Brian Gix2b64d152011-12-21 16:12:12 -0800469static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
470 u8 local_io, u8 remote_io)
471{
472 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300473 struct l2cap_chan *chan = conn->smp;
474 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800475 u8 method;
476 u32 passkey = 0;
477 int ret = 0;
478
479 /* Initialize key for JUST WORKS */
480 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300481 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800482
483 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
484
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300485 /* If neither side wants MITM, either "just" confirm an incoming
486 * request or use just-works for outgoing ones. The JUST_CFM
487 * will be converted to JUST_WORKS if necessary later in this
488 * function. If either side has MITM look up the method from the
489 * table.
490 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300491 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300492 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800493 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300494 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800495
Johan Hedberga82505c2014-03-24 14:39:07 +0200496 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300497 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200498 method = JUST_WORKS;
499
Johan Hedberg02f3e252014-07-16 15:09:13 +0300500 /* Don't bother user space with no IO capabilities */
501 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
502 method = JUST_WORKS;
503
Brian Gix2b64d152011-12-21 16:12:12 -0800504 /* If Just Works, Continue with Zero TK */
505 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300506 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800507 return 0;
508 }
509
510 /* Not Just Works/Confirm results in MITM Authentication */
511 if (method != JUST_CFM)
Johan Hedberg4a74d652014-05-20 09:45:50 +0300512 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800513
514 /* If both devices have Keyoard-Display I/O, the master
515 * Confirms and the slave Enters the passkey.
516 */
517 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300518 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800519 method = CFM_PASSKEY;
520 else
521 method = REQ_PASSKEY;
522 }
523
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200524 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800525 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200526 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800527 get_random_bytes(&passkey, sizeof(passkey));
528 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200529 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800530 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300531 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800532 }
533
534 hci_dev_lock(hcon->hdev);
535
536 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700537 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200538 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200539 else if (method == JUST_CFM)
540 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
541 hcon->type, hcon->dst_type,
542 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800543 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200544 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200545 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200546 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800547
548 hci_dev_unlock(hcon->hdev);
549
550 return ret;
551}
552
Johan Hedberg1cc61142014-05-20 09:45:52 +0300553static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300554{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300555 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300556 struct smp_cmd_pairing_confirm cp;
557 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300558
559 BT_DBG("conn %p", conn);
560
Johan Hedbergec70f362014-06-27 14:23:04 +0300561 ret = smp_c1(smp, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200562 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200563 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
564 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300565 if (ret)
566 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300567
Johan Hedberg4a74d652014-05-20 09:45:50 +0300568 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800569
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300570 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
571
Johan Hedberg1cc61142014-05-20 09:45:52 +0300572 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300573}
574
Johan Hedberg861580a2014-05-20 09:45:51 +0300575static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300576{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300577 struct l2cap_conn *conn = smp->conn;
578 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300579 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300580 int ret;
581
Johan Hedbergec70f362014-06-27 14:23:04 +0300582 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300583 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300584
585 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
586
Johan Hedbergec70f362014-06-27 14:23:04 +0300587 ret = smp_c1(smp, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200588 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200589 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300590 if (ret)
591 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300592
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300593 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
594 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300595 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300596 }
597
598 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800599 u8 stk[16];
600 __le64 rand = 0;
601 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300602
Johan Hedbergec70f362014-06-27 14:23:04 +0300603 smp_s1(smp, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300604
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300605 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300606 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300607
Johan Hedberg861580a2014-05-20 09:45:51 +0300608 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
609 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300610
611 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300612 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300613 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300614 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300615 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800616 __le64 rand = 0;
617 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300618
Johan Hedberg943a7322014-03-18 12:58:24 +0200619 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
620 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300621
Johan Hedbergec70f362014-06-27 14:23:04 +0300622 smp_s1(smp, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300623
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300624 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700625 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300626
Johan Hedbergfff34902014-06-10 15:19:50 +0300627 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
628 auth = 1;
629 else
630 auth = 0;
631
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300632 /* Even though there's no _SLAVE suffix this is the
633 * slave STK we're adding for later lookup (the master
634 * STK never needs to be stored).
635 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700636 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300637 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300638 }
639
Johan Hedberg861580a2014-05-20 09:45:51 +0300640 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300641}
642
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300643static void smp_notify_keys(struct l2cap_conn *conn)
644{
645 struct l2cap_chan *chan = conn->smp;
646 struct smp_chan *smp = chan->data;
647 struct hci_conn *hcon = conn->hcon;
648 struct hci_dev *hdev = hcon->hdev;
649 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
650 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
651 bool persistent;
652
653 if (smp->remote_irk) {
654 mgmt_new_irk(hdev, smp->remote_irk);
655 /* Now that user space can be considered to know the
656 * identity address track the connection based on it
657 * from now on.
658 */
659 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
660 hcon->dst_type = smp->remote_irk->addr_type;
661 l2cap_conn_update_id_addr(hcon);
662
663 /* When receiving an indentity resolving key for
664 * a remote device that does not use a resolvable
665 * private address, just remove the key so that
666 * it is possible to use the controller white
667 * list for scanning.
668 *
669 * Userspace will have been told to not store
670 * this key at this point. So it is safe to
671 * just remove it.
672 */
673 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
674 list_del(&smp->remote_irk->list);
675 kfree(smp->remote_irk);
676 smp->remote_irk = NULL;
677 }
678 }
679
680 /* The LTKs and CSRKs should be persistent only if both sides
681 * had the bonding bit set in their authentication requests.
682 */
683 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
684
685 if (smp->csrk) {
686 smp->csrk->bdaddr_type = hcon->dst_type;
687 bacpy(&smp->csrk->bdaddr, &hcon->dst);
688 mgmt_new_csrk(hdev, smp->csrk, persistent);
689 }
690
691 if (smp->slave_csrk) {
692 smp->slave_csrk->bdaddr_type = hcon->dst_type;
693 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
694 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
695 }
696
697 if (smp->ltk) {
698 smp->ltk->bdaddr_type = hcon->dst_type;
699 bacpy(&smp->ltk->bdaddr, &hcon->dst);
700 mgmt_new_ltk(hdev, smp->ltk, persistent);
701 }
702
703 if (smp->slave_ltk) {
704 smp->slave_ltk->bdaddr_type = hcon->dst_type;
705 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
706 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
707 }
708}
709
Johan Hedberg86d14072014-08-11 22:06:43 +0300710static void smp_distribute_keys(struct work_struct *work)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300711{
Johan Hedberg86d14072014-08-11 22:06:43 +0300712 struct smp_chan *smp = container_of(work, struct smp_chan,
713 distribute_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300714 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300715 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300716 struct hci_conn *hcon = conn->hcon;
717 struct hci_dev *hdev = hcon->hdev;
718 __u8 *keydist;
719
720 BT_DBG("conn %p", conn);
721
722 if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Johan Hedberg86d14072014-08-11 22:06:43 +0300723 return;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300724
725 rsp = (void *) &smp->prsp[1];
726
727 /* The responder sends its keys first */
728 if (hcon->out && (smp->remote_key_dist & 0x07))
Johan Hedberg86d14072014-08-11 22:06:43 +0300729 return;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300730
731 req = (void *) &smp->preq[1];
732
733 if (hcon->out) {
734 keydist = &rsp->init_key_dist;
735 *keydist &= req->init_key_dist;
736 } else {
737 keydist = &rsp->resp_key_dist;
738 *keydist &= req->resp_key_dist;
739 }
740
741 BT_DBG("keydist 0x%x", *keydist);
742
743 if (*keydist & SMP_DIST_ENC_KEY) {
744 struct smp_cmd_encrypt_info enc;
745 struct smp_cmd_master_ident ident;
746 struct smp_ltk *ltk;
747 u8 authenticated;
748 __le16 ediv;
749 __le64 rand;
750
751 get_random_bytes(enc.ltk, sizeof(enc.ltk));
752 get_random_bytes(&ediv, sizeof(ediv));
753 get_random_bytes(&rand, sizeof(rand));
754
755 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
756
757 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
758 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
759 SMP_LTK_SLAVE, authenticated, enc.ltk,
760 smp->enc_key_size, ediv, rand);
761 smp->slave_ltk = ltk;
762
763 ident.ediv = ediv;
764 ident.rand = rand;
765
766 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
767
768 *keydist &= ~SMP_DIST_ENC_KEY;
769 }
770
771 if (*keydist & SMP_DIST_ID_KEY) {
772 struct smp_cmd_ident_addr_info addrinfo;
773 struct smp_cmd_ident_info idinfo;
774
775 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
776
777 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
778
779 /* The hci_conn contains the local identity address
780 * after the connection has been established.
781 *
782 * This is true even when the connection has been
783 * established using a resolvable random address.
784 */
785 bacpy(&addrinfo.bdaddr, &hcon->src);
786 addrinfo.addr_type = hcon->src_type;
787
788 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
789 &addrinfo);
790
791 *keydist &= ~SMP_DIST_ID_KEY;
792 }
793
794 if (*keydist & SMP_DIST_SIGN) {
795 struct smp_cmd_sign_info sign;
796 struct smp_csrk *csrk;
797
798 /* Generate a new random key */
799 get_random_bytes(sign.csrk, sizeof(sign.csrk));
800
801 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
802 if (csrk) {
803 csrk->master = 0x00;
804 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
805 }
806 smp->slave_csrk = csrk;
807
808 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
809
810 *keydist &= ~SMP_DIST_SIGN;
811 }
812
813 /* If there are still keys to be received wait for them */
814 if ((smp->remote_key_dist & 0x07))
Johan Hedberg86d14072014-08-11 22:06:43 +0300815 return;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300816
817 clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300818 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
819 smp_notify_keys(conn);
820
821 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300822}
823
Johan Hedbergb68fda62014-08-11 22:06:40 +0300824static void smp_timeout(struct work_struct *work)
825{
826 struct smp_chan *smp = container_of(work, struct smp_chan,
827 security_timer.work);
828 struct l2cap_conn *conn = smp->conn;
829
830 BT_DBG("conn %p", conn);
831
832 l2cap_conn_shutdown(conn, ETIMEDOUT);
833}
834
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300835static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
836{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300837 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300838 struct smp_chan *smp;
839
Marcel Holtmannf1560462013-10-13 05:43:25 -0700840 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedberg616d55b2014-07-29 14:18:48 +0300841 if (!smp) {
842 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300843 return NULL;
Johan Hedberg616d55b2014-07-29 14:18:48 +0300844 }
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300845
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300846 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
847 if (IS_ERR(smp->tfm_aes)) {
848 BT_ERR("Unable to create ECB crypto context");
849 kfree(smp);
Johan Hedberg616d55b2014-07-29 14:18:48 +0300850 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300851 return NULL;
852 }
853
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300854 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300855 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300856
Johan Hedberg86d14072014-08-11 22:06:43 +0300857 INIT_WORK(&smp->distribute_work, smp_distribute_keys);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300858 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
859
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300860 hci_conn_hold(conn->hcon);
861
862 return smp;
863}
864
Brian Gix2b64d152011-12-21 16:12:12 -0800865int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
866{
Johan Hedbergb10e8012014-06-27 14:23:07 +0300867 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300868 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -0800869 struct smp_chan *smp;
870 u32 value;
Brian Gix2b64d152011-12-21 16:12:12 -0800871
872 BT_DBG("");
873
Johan Hedberg642ac772014-06-27 14:23:06 +0300874 if (!conn || !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Brian Gix2b64d152011-12-21 16:12:12 -0800875 return -ENOTCONN;
876
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300877 chan = conn->smp;
878 if (!chan)
879 return -ENOTCONN;
880
881 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800882
883 switch (mgmt_op) {
884 case MGMT_OP_USER_PASSKEY_REPLY:
885 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +0200886 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800887 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +0200888 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800889 /* Fall Through */
890 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +0300891 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800892 break;
893 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
894 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +0200895 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Brian Gix2b64d152011-12-21 16:12:12 -0800896 return 0;
897 default:
Johan Hedberg84794e12013-11-06 11:24:57 +0200898 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Brian Gix2b64d152011-12-21 16:12:12 -0800899 return -EOPNOTSUPP;
900 }
901
902 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +0300903 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
904 u8 rsp = smp_confirm(smp);
905 if (rsp)
906 smp_failure(conn, rsp);
907 }
Brian Gix2b64d152011-12-21 16:12:12 -0800908
909 return 0;
910}
911
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300912static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300913{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300914 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergb3c64102014-07-10 11:02:07 +0300915 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300916 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +0300917 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300918 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300919
920 BT_DBG("conn %p", conn);
921
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200922 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300923 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200924
Johan Hedberg40bef302014-07-16 11:42:27 +0300925 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -0800926 return SMP_CMD_NOTSUPP;
927
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300928 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) {
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300929 smp = smp_chan_create(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300930 } else {
931 struct l2cap_chan *chan = conn->smp;
932 smp = chan->data;
933 }
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300934
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +0300935 if (!smp)
936 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300937
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300938 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergb3c64102014-07-10 11:02:07 +0300939 (req->auth_req & SMP_AUTH_BONDING))
940 return SMP_PAIRING_NOTSUPP;
941
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300942 smp->preq[0] = SMP_CMD_PAIRING_REQ;
943 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300944 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300945
Brian Gix2b64d152011-12-21 16:12:12 -0800946 /* We didn't start the pairing, so match remote */
Johan Hedberg1ef35822014-05-20 09:45:48 +0300947 auth = req->auth_req;
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300948
Johan Hedbergc7262e72014-06-17 13:07:37 +0300949 sec_level = authreq_to_seclevel(auth);
950 if (sec_level > conn->hcon->pending_sec_level)
951 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +0200952
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300953 /* If we need MITM check that it can be acheived */
954 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
955 u8 method;
956
957 method = get_auth_method(smp, conn->hcon->io_capability,
958 req->io_capability);
959 if (method == JUST_WORKS || method == JUST_CFM)
960 return SMP_AUTH_REQUIREMENTS;
961 }
962
Brian Gix2b64d152011-12-21 16:12:12 -0800963 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300964
965 key_size = min(req->max_key_size, rsp.max_key_size);
966 if (check_enc_key_size(conn, key_size))
967 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300968
Johan Hedberge84a6b12013-12-02 10:49:03 +0200969 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300970
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300971 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
972 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -0300973
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300974 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300975
Brian Gix2b64d152011-12-21 16:12:12 -0800976 /* Request setup of TK */
977 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
978 if (ret)
979 return SMP_UNSPECIFIED;
980
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300981 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300982}
983
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300984static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300985{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300986 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300987 struct l2cap_chan *chan = conn->smp;
988 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800989 u8 key_size, auth = SMP_AUTH_NONE;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -0300990 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300991
992 BT_DBG("conn %p", conn);
993
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200994 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300995 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200996
Johan Hedberg40bef302014-07-16 11:42:27 +0300997 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800998 return SMP_CMD_NOTSUPP;
999
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001000 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001001
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001002 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001003
1004 key_size = min(req->max_key_size, rsp->max_key_size);
1005 if (check_enc_key_size(conn, key_size))
1006 return SMP_ENC_KEY_SIZE;
1007
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001008 /* If we need MITM check that it can be acheived */
1009 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1010 u8 method;
1011
1012 method = get_auth_method(smp, req->io_capability,
1013 rsp->io_capability);
1014 if (method == JUST_WORKS || method == JUST_CFM)
1015 return SMP_AUTH_REQUIREMENTS;
1016 }
1017
Johan Hedberge84a6b12013-12-02 10:49:03 +02001018 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001019
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001020 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1021 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001022
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001023 /* Update remote key distribution in case the remote cleared
1024 * some bits that we had enabled in our request.
1025 */
1026 smp->remote_key_dist &= rsp->resp_key_dist;
1027
Brian Gix2b64d152011-12-21 16:12:12 -08001028 if ((req->auth_req & SMP_AUTH_BONDING) &&
Marcel Holtmannf1560462013-10-13 05:43:25 -07001029 (rsp->auth_req & SMP_AUTH_BONDING))
Brian Gix2b64d152011-12-21 16:12:12 -08001030 auth = SMP_AUTH_BONDING;
1031
1032 auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM;
1033
Johan Hedberg476585e2012-06-06 18:54:15 +08001034 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001035 if (ret)
1036 return SMP_UNSPECIFIED;
1037
Johan Hedberg4a74d652014-05-20 09:45:50 +03001038 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001039
1040 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001041 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001042 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001043
1044 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001045}
1046
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001047static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001048{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001049 struct l2cap_chan *chan = conn->smp;
1050 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001051
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001052 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1053
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001054 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001055 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001056
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001057 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1058 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001059
Johan Hedberg943a7322014-03-18 12:58:24 +02001060 if (conn->hcon->out)
1061 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1062 smp->prnd);
Johan Hedberg4a74d652014-05-20 09:45:50 +03001063 else if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001064 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001065 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001066 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001067
1068 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001069}
1070
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001071static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001072{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001073 struct l2cap_chan *chan = conn->smp;
1074 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001075
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001076 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001077
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001078 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001079 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001080
Johan Hedberg943a7322014-03-18 12:58:24 +02001081 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001082 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001083
Johan Hedberg861580a2014-05-20 09:45:51 +03001084 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001085}
1086
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001087static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001088{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001089 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001090 struct hci_conn *hcon = conn->hcon;
1091
Johan Hedberg98a0b842014-01-30 19:40:00 -08001092 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001093 hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001094 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001095 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001096
Johan Hedberg4dab7862012-06-07 14:58:37 +08001097 if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001098 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001099
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001100 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001101 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001102
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001103 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1104 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001105
Johan Hedbergfe59a052014-07-01 19:14:12 +03001106 /* We never store STKs for master role, so clear this flag */
1107 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1108
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001109 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001110}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001111
Johan Hedberg854f4722014-07-01 18:40:20 +03001112bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level)
1113{
1114 if (sec_level == BT_SECURITY_LOW)
1115 return true;
1116
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001117 /* If we're encrypted with an STK always claim insufficient
1118 * security. This way we allow the connection to be re-encrypted
1119 * with an LTK, even if the LTK provides the same level of
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001120 * security. Only exception is if we don't have an LTK (e.g.
1121 * because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001122 */
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001123 if (test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
1124 hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001125 hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001126 return false;
1127
Johan Hedberg854f4722014-07-01 18:40:20 +03001128 if (hcon->sec_level >= sec_level)
1129 return true;
1130
1131 return false;
1132}
1133
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001134static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001135{
1136 struct smp_cmd_security_req *rp = (void *) skb->data;
1137 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001138 struct hci_conn *hcon = conn->hcon;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001139 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001140 u8 sec_level;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001141
1142 BT_DBG("conn %p", conn);
1143
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001144 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001145 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001146
Johan Hedberg40bef302014-07-16 11:42:27 +03001147 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001148 return SMP_CMD_NOTSUPP;
1149
Johan Hedbergc7262e72014-06-17 13:07:37 +03001150 sec_level = authreq_to_seclevel(rp->auth_req);
Johan Hedberg854f4722014-07-01 18:40:20 +03001151 if (smp_sufficient_security(hcon, sec_level))
1152 return 0;
1153
Johan Hedbergc7262e72014-06-17 13:07:37 +03001154 if (sec_level > hcon->pending_sec_level)
1155 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001156
Johan Hedberg4dab7862012-06-07 14:58:37 +08001157 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001158 return 0;
1159
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001160 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001161 return 0;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001162
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001163 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001164 if (!smp)
1165 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001166
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001167 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedberg616d55b2014-07-29 14:18:48 +03001168 (rp->auth_req & SMP_AUTH_BONDING))
1169 return SMP_PAIRING_NOTSUPP;
1170
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001171 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001172
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001173 memset(&cp, 0, sizeof(cp));
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -03001174 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001175
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001176 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1177 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001178
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001179 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001180
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001181 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001182}
1183
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001184int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001185{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001186 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001187 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001188 __u8 authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001189
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001190 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1191
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001192 /* This may be NULL if there's an unexpected disconnection */
1193 if (!conn)
1194 return 1;
1195
Johan Hedberg757aee02013-04-24 13:05:32 +03001196 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001197 return 1;
1198
Johan Hedbergad32a2f2013-05-14 18:05:12 +03001199 if (smp_sufficient_security(hcon, sec_level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001200 return 1;
1201
Johan Hedbergc7262e72014-06-17 13:07:37 +03001202 if (sec_level > hcon->pending_sec_level)
1203 hcon->pending_sec_level = sec_level;
1204
Johan Hedberg40bef302014-07-16 11:42:27 +03001205 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001206 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1207 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001208
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001209 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001210 return 0;
1211
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001212 smp = smp_chan_create(conn);
Brian Gix2b64d152011-12-21 16:12:12 -08001213 if (!smp)
1214 return 1;
1215
1216 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001217
Johan Hedberg79897d22014-06-01 09:45:24 +03001218 /* Require MITM if IO Capability allows or the security level
1219 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001220 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001221 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001222 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001223 authreq |= SMP_AUTH_MITM;
1224
Johan Hedberg40bef302014-07-16 11:42:27 +03001225 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001226 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001227
Brian Gix2b64d152011-12-21 16:12:12 -08001228 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001229 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1230 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001231
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001232 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
1233 } else {
1234 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001235 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001236 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
1237 }
1238
Johan Hedberg4a74d652014-05-20 09:45:50 +03001239 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergedca7922014-03-24 15:54:11 +02001240
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001241 return 0;
1242}
1243
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001244static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1245{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001246 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001247 struct l2cap_chan *chan = conn->smp;
1248 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001249
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001250 BT_DBG("conn %p", conn);
1251
1252 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001253 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001254
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001255 /* Ignore this PDU if it wasn't requested */
1256 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
1257 return 0;
1258
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001259 skb_pull(skb, sizeof(*rp));
1260
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001261 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001262
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001263 return 0;
1264}
1265
1266static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1267{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001268 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001269 struct l2cap_chan *chan = conn->smp;
1270 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001271 struct hci_dev *hdev = conn->hcon->hdev;
1272 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001273 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001274 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001275
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001276 BT_DBG("conn %p", conn);
1277
1278 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001279 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001280
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001281 /* Ignore this PDU if it wasn't requested */
1282 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
1283 return 0;
1284
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001285 /* Mark the information as received */
1286 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1287
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001288 skb_pull(skb, sizeof(*rp));
1289
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001290 hci_dev_lock(hdev);
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001291 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001292 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001293 authenticated, smp->tk, smp->enc_key_size,
1294 rp->ediv, rp->rand);
1295 smp->ltk = ltk;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001296 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
Johan Hedberg86d14072014-08-11 22:06:43 +03001297 queue_work(hdev->workqueue, &smp->distribute_work);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001298 hci_dev_unlock(hdev);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001299
1300 return 0;
1301}
1302
Johan Hedbergfd349c02014-02-18 10:19:36 +02001303static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1304{
1305 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001306 struct l2cap_chan *chan = conn->smp;
1307 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001308
1309 BT_DBG("");
1310
1311 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001312 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001313
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001314 /* Ignore this PDU if it wasn't requested */
1315 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1316 return 0;
1317
Johan Hedbergfd349c02014-02-18 10:19:36 +02001318 skb_pull(skb, sizeof(*info));
1319
1320 memcpy(smp->irk, info->irk, 16);
1321
1322 return 0;
1323}
1324
1325static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1326 struct sk_buff *skb)
1327{
1328 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001329 struct l2cap_chan *chan = conn->smp;
1330 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001331 struct hci_conn *hcon = conn->hcon;
Johan Hedberg86d14072014-08-11 22:06:43 +03001332 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001333 bdaddr_t rpa;
1334
1335 BT_DBG("");
1336
1337 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001338 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001339
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001340 /* Ignore this PDU if it wasn't requested */
1341 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1342 return 0;
1343
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001344 /* Mark the information as received */
1345 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1346
Johan Hedbergfd349c02014-02-18 10:19:36 +02001347 skb_pull(skb, sizeof(*info));
1348
Johan Hedberg31dd6242014-06-27 14:23:02 +03001349 hci_dev_lock(hcon->hdev);
1350
Johan Hedberga9a58f82014-02-25 22:24:37 +02001351 /* Strictly speaking the Core Specification (4.1) allows sending
1352 * an empty address which would force us to rely on just the IRK
1353 * as "identity information". However, since such
1354 * implementations are not known of and in order to not over
1355 * complicate our implementation, simply pretend that we never
1356 * received an IRK for such a device.
1357 */
1358 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1359 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001360 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001361 }
1362
Johan Hedbergfd349c02014-02-18 10:19:36 +02001363 bacpy(&smp->id_addr, &info->bdaddr);
1364 smp->id_addr_type = info->addr_type;
1365
1366 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1367 bacpy(&rpa, &hcon->dst);
1368 else
1369 bacpy(&rpa, BDADDR_ANY);
1370
Johan Hedberg23d0e122014-02-19 14:57:46 +02001371 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1372 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001373
Johan Hedberg31dd6242014-06-27 14:23:02 +03001374distribute:
Johan Hedberg86d14072014-08-11 22:06:43 +03001375 queue_work(hdev->workqueue, &smp->distribute_work);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001376
Johan Hedberg31dd6242014-06-27 14:23:02 +03001377 hci_dev_unlock(hcon->hdev);
1378
Johan Hedbergfd349c02014-02-18 10:19:36 +02001379 return 0;
1380}
1381
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001382static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1383{
1384 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001385 struct l2cap_chan *chan = conn->smp;
1386 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001387 struct hci_dev *hdev = conn->hcon->hdev;
1388 struct smp_csrk *csrk;
1389
1390 BT_DBG("conn %p", conn);
1391
1392 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001393 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001394
1395 /* Ignore this PDU if it wasn't requested */
1396 if (!(smp->remote_key_dist & SMP_DIST_SIGN))
1397 return 0;
1398
1399 /* Mark the information as received */
1400 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1401
1402 skb_pull(skb, sizeof(*rp));
1403
1404 hci_dev_lock(hdev);
1405 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1406 if (csrk) {
1407 csrk->master = 0x01;
1408 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1409 }
1410 smp->csrk = csrk;
Johan Hedberg86d14072014-08-11 22:06:43 +03001411 queue_work(hdev->workqueue, &smp->distribute_work);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001412 hci_dev_unlock(hdev);
1413
1414 return 0;
1415}
1416
Johan Hedberg4befb862014-08-11 22:06:38 +03001417static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001418{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001419 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001420 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001421 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001422 int err = 0;
1423
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001424 if (hcon->type != LE_LINK) {
1425 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001426 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001427 }
1428
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001429 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001430 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001431
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001432 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Johan Hedbergbeb19e42014-07-18 11:15:26 +03001433 err = -EOPNOTSUPP;
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001434 reason = SMP_PAIRING_NOTSUPP;
1435 goto done;
1436 }
1437
Marcel Holtmann92381f52013-10-03 01:23:08 -07001438 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001439 skb_pull(skb, sizeof(code));
1440
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001441 /*
1442 * The SMP context must be initialized for all other PDUs except
1443 * pairing and security requests. If we get any other PDU when
1444 * not initialized simply disconnect (done if this function
1445 * returns an error).
1446 */
1447 if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ &&
Johan Hedbergd3368602014-08-08 09:28:05 +03001448 !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) {
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001449 BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001450 reason = SMP_CMD_NOTSUPP;
1451 err = -EOPNOTSUPP;
1452 goto done;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001453 }
1454
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001455 switch (code) {
1456 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001457 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001458 break;
1459
1460 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001461 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001462 reason = 0;
1463 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001464 break;
1465
1466 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001467 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001468 break;
1469
1470 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001471 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001472 break;
1473
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001474 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001475 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001476 break;
1477
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001478 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001479 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001480 break;
1481
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001482 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001483 reason = smp_cmd_encrypt_info(conn, skb);
1484 break;
1485
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001486 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001487 reason = smp_cmd_master_ident(conn, skb);
1488 break;
1489
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001490 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001491 reason = smp_cmd_ident_info(conn, skb);
1492 break;
1493
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001494 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001495 reason = smp_cmd_ident_addr_info(conn, skb);
1496 break;
1497
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001498 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001499 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001500 break;
1501
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001502 default:
1503 BT_DBG("Unknown command code 0x%2.2x", code);
1504
1505 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001506 err = -EOPNOTSUPP;
1507 goto done;
1508 }
1509
1510done:
1511 if (reason)
Johan Hedberg84794e12013-11-06 11:24:57 +02001512 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001513 if (!err)
1514 kfree_skb(skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001515 return err;
1516}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001517
Johan Hedberg70db83c2014-08-08 09:37:16 +03001518static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1519{
1520 struct l2cap_conn *conn = chan->conn;
1521
1522 BT_DBG("chan %p", chan);
1523
Johan Hedberg109ec232014-08-11 22:06:42 +03001524 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001525 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001526
Johan Hedberg70db83c2014-08-08 09:37:16 +03001527 conn->smp = NULL;
1528 l2cap_chan_put(chan);
1529}
1530
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001531static void smp_resume_cb(struct l2cap_chan *chan)
1532{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001533 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001534 struct l2cap_conn *conn = chan->conn;
1535 struct hci_conn *hcon = conn->hcon;
Johan Hedberg86d14072014-08-11 22:06:43 +03001536 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001537
1538 BT_DBG("chan %p", chan);
1539
Johan Hedberg86d14072014-08-11 22:06:43 +03001540 if (!smp)
1541 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001542
Johan Hedberg86d14072014-08-11 22:06:43 +03001543 cancel_delayed_work(&smp->security_timer);
1544
1545 if (test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1546 queue_work(hdev->workqueue, &smp->distribute_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001547}
1548
Johan Hedberg70db83c2014-08-08 09:37:16 +03001549static void smp_ready_cb(struct l2cap_chan *chan)
1550{
1551 struct l2cap_conn *conn = chan->conn;
1552
1553 BT_DBG("chan %p", chan);
1554
1555 conn->smp = chan;
1556 l2cap_chan_hold(chan);
1557}
1558
Johan Hedberg4befb862014-08-11 22:06:38 +03001559static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1560{
1561 int err;
1562
1563 BT_DBG("chan %p", chan);
1564
1565 err = smp_sig_channel(chan, skb);
1566 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001567 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001568
Johan Hedbergb68fda62014-08-11 22:06:40 +03001569 if (smp)
1570 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001571
1572 l2cap_conn_shutdown(chan->conn, -err);
1573 }
1574
1575 return err;
1576}
1577
Johan Hedberg70db83c2014-08-08 09:37:16 +03001578static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1579 unsigned long hdr_len,
1580 unsigned long len, int nb)
1581{
1582 struct sk_buff *skb;
1583
1584 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1585 if (!skb)
1586 return ERR_PTR(-ENOMEM);
1587
1588 skb->priority = HCI_PRIO_MAX;
1589 bt_cb(skb)->chan = chan;
1590
1591 return skb;
1592}
1593
1594static const struct l2cap_ops smp_chan_ops = {
1595 .name = "Security Manager",
1596 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001597 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001598 .alloc_skb = smp_alloc_skb_cb,
1599 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001600 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001601
1602 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001603 .state_change = l2cap_chan_no_state_change,
1604 .close = l2cap_chan_no_close,
1605 .defer = l2cap_chan_no_defer,
1606 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001607 .set_shutdown = l2cap_chan_no_set_shutdown,
1608 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1609 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1610};
1611
1612static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1613{
1614 struct l2cap_chan *chan;
1615
1616 BT_DBG("pchan %p", pchan);
1617
1618 chan = l2cap_chan_create();
1619 if (!chan)
1620 return NULL;
1621
1622 chan->chan_type = pchan->chan_type;
1623 chan->ops = &smp_chan_ops;
1624 chan->scid = pchan->scid;
1625 chan->dcid = chan->scid;
1626 chan->imtu = pchan->imtu;
1627 chan->omtu = pchan->omtu;
1628 chan->mode = pchan->mode;
1629
1630 BT_DBG("created chan %p", chan);
1631
1632 return chan;
1633}
1634
1635static const struct l2cap_ops smp_root_chan_ops = {
1636 .name = "Security Manager Root",
1637 .new_connection = smp_new_conn_cb,
1638
1639 /* None of these are implemented for the root channel */
1640 .close = l2cap_chan_no_close,
1641 .alloc_skb = l2cap_chan_no_alloc_skb,
1642 .recv = l2cap_chan_no_recv,
1643 .state_change = l2cap_chan_no_state_change,
1644 .teardown = l2cap_chan_no_teardown,
1645 .ready = l2cap_chan_no_ready,
1646 .defer = l2cap_chan_no_defer,
1647 .suspend = l2cap_chan_no_suspend,
1648 .resume = l2cap_chan_no_resume,
1649 .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
Johan Hedberg711eafe2014-08-08 09:32:52 +03001654int smp_register(struct hci_dev *hdev)
1655{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001656 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001657 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001658
Johan Hedberg711eafe2014-08-08 09:32:52 +03001659 BT_DBG("%s", hdev->name);
1660
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001661 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1662 if (IS_ERR(tfm_aes)) {
1663 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001664 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03001665 return err;
1666 }
1667
Johan Hedberg70db83c2014-08-08 09:37:16 +03001668 chan = l2cap_chan_create();
1669 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001670 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001671 return -ENOMEM;
1672 }
1673
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001674 chan->data = tfm_aes;
1675
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001676 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001677
1678 l2cap_chan_set_defaults(chan);
1679
1680 bacpy(&chan->src, &hdev->bdaddr);
1681 chan->src_type = BDADDR_LE_PUBLIC;
1682 chan->state = BT_LISTEN;
1683 chan->mode = L2CAP_MODE_BASIC;
1684 chan->imtu = L2CAP_DEFAULT_MTU;
1685 chan->ops = &smp_root_chan_ops;
1686
1687 hdev->smp_data = chan;
1688
Johan Hedberg711eafe2014-08-08 09:32:52 +03001689 return 0;
1690}
1691
1692void smp_unregister(struct hci_dev *hdev)
1693{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001694 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001695 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001696
1697 if (!chan)
1698 return;
1699
1700 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001701
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001702 tfm_aes = chan->data;
1703 if (tfm_aes) {
1704 chan->data = NULL;
1705 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001706 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03001707
1708 hdev->smp_data = NULL;
1709 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001710}