blob: bfa839eed89bb32d0d48d478a81fe882e2991c47 [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)
Johan Hedbergb28b4942014-09-05 22:19:55 +030035
Marcel Holtmann17b02e62012-03-01 14:32:37 -080036#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030037
Johan Hedberg0edb14d2014-05-26 13:29:28 +030038#define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
39 0x1f : 0x07)
40#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 Hedberge491eaf2014-10-25 21:15:37 +0200195static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
196 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
197 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300198{
199 u8 p1[16], p2[16];
200 int err;
201
202 memset(p1, 0, 16);
203
204 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200205 p1[0] = _iat;
206 p1[1] = _rat;
207 memcpy(p1 + 2, preq, 7);
208 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300209
210 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200211 memcpy(p2, ra, 6);
212 memcpy(p2 + 6, ia, 6);
213 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300214
215 /* res = r XOR p1 */
216 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
217
218 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200219 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300220 if (err) {
221 BT_ERR("Encrypt data error");
222 return err;
223 }
224
225 /* res = res XOR p2 */
226 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
227
228 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200229 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300230 if (err)
231 BT_ERR("Encrypt data error");
232
233 return err;
234}
235
Johan Hedberge491eaf2014-10-25 21:15:37 +0200236static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
237 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300238{
239 int err;
240
241 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200242 memcpy(_r, r2, 8);
243 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300244
Johan Hedberge491eaf2014-10-25 21:15:37 +0200245 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300246 if (err)
247 BT_ERR("Encrypt data error");
248
249 return err;
250}
251
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300252static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
253{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300254 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300255 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300256 struct kvec iv[2];
257 struct msghdr msg;
258
259 if (!chan)
260 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300261
262 BT_DBG("code 0x%2.2x", code);
263
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300264 iv[0].iov_base = &code;
265 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300266
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300267 iv[1].iov_base = data;
268 iv[1].iov_len = len;
269
270 memset(&msg, 0, sizeof(msg));
271
272 msg.msg_iov = (struct iovec *) &iv;
273 msg.msg_iovlen = 2;
274
275 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300276
Johan Hedbergb68fda62014-08-11 22:06:40 +0300277 if (!chan->data)
278 return;
279
280 smp = chan->data;
281
282 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300283 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300284}
285
Brian Gix2b64d152011-12-21 16:12:12 -0800286static __u8 authreq_to_seclevel(__u8 authreq)
287{
288 if (authreq & SMP_AUTH_MITM)
289 return BT_SECURITY_HIGH;
290 else
291 return BT_SECURITY_MEDIUM;
292}
293
294static __u8 seclevel_to_authreq(__u8 sec_level)
295{
296 switch (sec_level) {
297 case BT_SECURITY_HIGH:
298 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
299 case BT_SECURITY_MEDIUM:
300 return SMP_AUTH_BONDING;
301 default:
302 return SMP_AUTH_NONE;
303 }
304}
305
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300306static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700307 struct smp_cmd_pairing *req,
308 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300309{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300310 struct l2cap_chan *chan = conn->smp;
311 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200312 struct hci_conn *hcon = conn->hcon;
313 struct hci_dev *hdev = hcon->hdev;
314 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300315
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300316 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700317 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
318 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300319 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800320 } else {
321 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300322 }
323
Johan Hedbergfd349c02014-02-18 10:19:36 +0200324 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
325 remote_dist |= SMP_DIST_ID_KEY;
326
Johan Hedberg863efaf2014-02-22 19:06:32 +0200327 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
328 local_dist |= SMP_DIST_ID_KEY;
329
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300330 if (rsp == NULL) {
331 req->io_capability = conn->hcon->io_capability;
332 req->oob_flag = SMP_OOB_NOT_PRESENT;
333 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200334 req->init_key_dist = local_dist;
335 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300336 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200337
338 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300339 return;
340 }
341
342 rsp->io_capability = conn->hcon->io_capability;
343 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
344 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200345 rsp->init_key_dist = req->init_key_dist & remote_dist;
346 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300347 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200348
349 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300350}
351
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300352static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
353{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300354 struct l2cap_chan *chan = conn->smp;
355 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300356
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300357 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700358 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300359 return SMP_ENC_KEY_SIZE;
360
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300361 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300362
363 return 0;
364}
365
Johan Hedberg6f48e262014-08-11 22:06:44 +0300366static void smp_chan_destroy(struct l2cap_conn *conn)
367{
368 struct l2cap_chan *chan = conn->smp;
369 struct smp_chan *smp = chan->data;
370 bool complete;
371
372 BUG_ON(!smp);
373
374 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300375
Johan Hedberg6f48e262014-08-11 22:06:44 +0300376 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
377 mgmt_smp_complete(conn->hcon, complete);
378
379 kfree(smp->csrk);
380 kfree(smp->slave_csrk);
381
382 crypto_free_blkcipher(smp->tfm_aes);
383
384 /* If pairing failed clean up any keys we might have */
385 if (!complete) {
386 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200387 list_del_rcu(&smp->ltk->list);
388 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300389 }
390
391 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200392 list_del_rcu(&smp->slave_ltk->list);
393 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300394 }
395
396 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200397 list_del_rcu(&smp->remote_irk->list);
398 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300399 }
400 }
401
402 chan->data = NULL;
403 kfree(smp);
404 hci_conn_drop(conn->hcon);
405}
406
Johan Hedberg84794e12013-11-06 11:24:57 +0200407static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800408{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200409 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300410 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200411
Johan Hedberg84794e12013-11-06 11:24:57 +0200412 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800413 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700414 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800415
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700416 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700417 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300418
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300419 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300420 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800421}
422
Brian Gix2b64d152011-12-21 16:12:12 -0800423#define JUST_WORKS 0x00
424#define JUST_CFM 0x01
425#define REQ_PASSKEY 0x02
426#define CFM_PASSKEY 0x03
427#define REQ_OOB 0x04
428#define OVERLAP 0xFF
429
430static const u8 gen_method[5][5] = {
431 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
432 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
433 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
434 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
435 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
436};
437
Johan Hedberg581370c2014-06-17 13:07:38 +0300438static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
439{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300440 /* If either side has unknown io_caps, use JUST_CFM (which gets
441 * converted later to JUST_WORKS if we're initiators.
442 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300443 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
444 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300445 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300446
447 return gen_method[remote_io][local_io];
448}
449
Brian Gix2b64d152011-12-21 16:12:12 -0800450static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
451 u8 local_io, u8 remote_io)
452{
453 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300454 struct l2cap_chan *chan = conn->smp;
455 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800456 u8 method;
457 u32 passkey = 0;
458 int ret = 0;
459
460 /* Initialize key for JUST WORKS */
461 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300462 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800463
464 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
465
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300466 /* If neither side wants MITM, either "just" confirm an incoming
467 * request or use just-works for outgoing ones. The JUST_CFM
468 * will be converted to JUST_WORKS if necessary later in this
469 * function. If either side has MITM look up the method from the
470 * table.
471 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300472 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300473 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800474 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300475 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800476
Johan Hedberga82505c2014-03-24 14:39:07 +0200477 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300478 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200479 method = JUST_WORKS;
480
Johan Hedberg02f3e252014-07-16 15:09:13 +0300481 /* Don't bother user space with no IO capabilities */
482 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
483 method = JUST_WORKS;
484
Brian Gix2b64d152011-12-21 16:12:12 -0800485 /* If Just Works, Continue with Zero TK */
486 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300487 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800488 return 0;
489 }
490
491 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300492 if (method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300493 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300494 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
495 hcon->pending_sec_level = BT_SECURITY_HIGH;
496 }
Brian Gix2b64d152011-12-21 16:12:12 -0800497
498 /* If both devices have Keyoard-Display I/O, the master
499 * Confirms and the slave Enters the passkey.
500 */
501 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300502 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800503 method = CFM_PASSKEY;
504 else
505 method = REQ_PASSKEY;
506 }
507
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200508 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800509 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200510 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800511 get_random_bytes(&passkey, sizeof(passkey));
512 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200513 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800514 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300515 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800516 }
517
Brian Gix2b64d152011-12-21 16:12:12 -0800518 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700519 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200520 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200521 else if (method == JUST_CFM)
522 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
523 hcon->type, hcon->dst_type,
524 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800525 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200526 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200527 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200528 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800529
Brian Gix2b64d152011-12-21 16:12:12 -0800530 return ret;
531}
532
Johan Hedberg1cc61142014-05-20 09:45:52 +0300533static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300534{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300535 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300536 struct smp_cmd_pairing_confirm cp;
537 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300538
539 BT_DBG("conn %p", conn);
540
Johan Hedberge491eaf2014-10-25 21:15:37 +0200541 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200542 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200543 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
544 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300545 if (ret)
546 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300547
Johan Hedberg4a74d652014-05-20 09:45:50 +0300548 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800549
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300550 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
551
Johan Hedbergb28b4942014-09-05 22:19:55 +0300552 if (conn->hcon->out)
553 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
554 else
555 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
556
Johan Hedberg1cc61142014-05-20 09:45:52 +0300557 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300558}
559
Johan Hedberg861580a2014-05-20 09:45:51 +0300560static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300561{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300562 struct l2cap_conn *conn = smp->conn;
563 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300564 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300565 int ret;
566
Johan Hedbergec70f362014-06-27 14:23:04 +0300567 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300568 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300569
570 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
571
Johan Hedberge491eaf2014-10-25 21:15:37 +0200572 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200573 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200574 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300575 if (ret)
576 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300577
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300578 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
579 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300580 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300581 }
582
583 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800584 u8 stk[16];
585 __le64 rand = 0;
586 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300587
Johan Hedberge491eaf2014-10-25 21:15:37 +0200588 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300589
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300590 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300591 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300592
Johan Hedberg861580a2014-05-20 09:45:51 +0300593 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
594 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300595
596 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300597 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300598 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300599 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300600 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800601 __le64 rand = 0;
602 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300603
Johan Hedberg943a7322014-03-18 12:58:24 +0200604 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
605 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300606
Johan Hedberge491eaf2014-10-25 21:15:37 +0200607 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300608
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300609 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700610 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300611
Johan Hedbergfff34902014-06-10 15:19:50 +0300612 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
613 auth = 1;
614 else
615 auth = 0;
616
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300617 /* Even though there's no _SLAVE suffix this is the
618 * slave STK we're adding for later lookup (the master
619 * STK never needs to be stored).
620 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700621 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300622 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300623 }
624
Johan Hedberg861580a2014-05-20 09:45:51 +0300625 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300626}
627
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300628static void smp_notify_keys(struct l2cap_conn *conn)
629{
630 struct l2cap_chan *chan = conn->smp;
631 struct smp_chan *smp = chan->data;
632 struct hci_conn *hcon = conn->hcon;
633 struct hci_dev *hdev = hcon->hdev;
634 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
635 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
636 bool persistent;
637
638 if (smp->remote_irk) {
639 mgmt_new_irk(hdev, smp->remote_irk);
640 /* Now that user space can be considered to know the
641 * identity address track the connection based on it
642 * from now on.
643 */
644 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
645 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300646 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300647
648 /* When receiving an indentity resolving key for
649 * a remote device that does not use a resolvable
650 * private address, just remove the key so that
651 * it is possible to use the controller white
652 * list for scanning.
653 *
654 * Userspace will have been told to not store
655 * this key at this point. So it is safe to
656 * just remove it.
657 */
658 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200659 list_del_rcu(&smp->remote_irk->list);
660 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300661 smp->remote_irk = NULL;
662 }
663 }
664
665 /* The LTKs and CSRKs should be persistent only if both sides
666 * had the bonding bit set in their authentication requests.
667 */
668 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
669
670 if (smp->csrk) {
671 smp->csrk->bdaddr_type = hcon->dst_type;
672 bacpy(&smp->csrk->bdaddr, &hcon->dst);
673 mgmt_new_csrk(hdev, smp->csrk, persistent);
674 }
675
676 if (smp->slave_csrk) {
677 smp->slave_csrk->bdaddr_type = hcon->dst_type;
678 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
679 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
680 }
681
682 if (smp->ltk) {
683 smp->ltk->bdaddr_type = hcon->dst_type;
684 bacpy(&smp->ltk->bdaddr, &hcon->dst);
685 mgmt_new_ltk(hdev, smp->ltk, persistent);
686 }
687
688 if (smp->slave_ltk) {
689 smp->slave_ltk->bdaddr_type = hcon->dst_type;
690 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
691 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
692 }
693}
694
Johan Hedbergb28b4942014-09-05 22:19:55 +0300695static void smp_allow_key_dist(struct smp_chan *smp)
696{
697 /* Allow the first expected phase 3 PDU. The rest of the PDUs
698 * will be allowed in each PDU handler to ensure we receive
699 * them in the correct order.
700 */
701 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
702 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
703 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
704 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
705 else if (smp->remote_key_dist & SMP_DIST_SIGN)
706 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
707}
708
Johan Hedbergd6268e82014-09-05 22:19:51 +0300709static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300710{
711 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300712 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300713 struct hci_conn *hcon = conn->hcon;
714 struct hci_dev *hdev = hcon->hdev;
715 __u8 *keydist;
716
717 BT_DBG("conn %p", conn);
718
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300719 rsp = (void *) &smp->prsp[1];
720
721 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300722 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
723 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300724 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300725 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300726
727 req = (void *) &smp->preq[1];
728
729 if (hcon->out) {
730 keydist = &rsp->init_key_dist;
731 *keydist &= req->init_key_dist;
732 } else {
733 keydist = &rsp->resp_key_dist;
734 *keydist &= req->resp_key_dist;
735 }
736
737 BT_DBG("keydist 0x%x", *keydist);
738
739 if (*keydist & SMP_DIST_ENC_KEY) {
740 struct smp_cmd_encrypt_info enc;
741 struct smp_cmd_master_ident ident;
742 struct smp_ltk *ltk;
743 u8 authenticated;
744 __le16 ediv;
745 __le64 rand;
746
747 get_random_bytes(enc.ltk, sizeof(enc.ltk));
748 get_random_bytes(&ediv, sizeof(ediv));
749 get_random_bytes(&rand, sizeof(rand));
750
751 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
752
753 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
754 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
755 SMP_LTK_SLAVE, authenticated, enc.ltk,
756 smp->enc_key_size, ediv, rand);
757 smp->slave_ltk = ltk;
758
759 ident.ediv = ediv;
760 ident.rand = rand;
761
762 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
763
764 *keydist &= ~SMP_DIST_ENC_KEY;
765 }
766
767 if (*keydist & SMP_DIST_ID_KEY) {
768 struct smp_cmd_ident_addr_info addrinfo;
769 struct smp_cmd_ident_info idinfo;
770
771 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
772
773 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
774
775 /* The hci_conn contains the local identity address
776 * after the connection has been established.
777 *
778 * This is true even when the connection has been
779 * established using a resolvable random address.
780 */
781 bacpy(&addrinfo.bdaddr, &hcon->src);
782 addrinfo.addr_type = hcon->src_type;
783
784 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
785 &addrinfo);
786
787 *keydist &= ~SMP_DIST_ID_KEY;
788 }
789
790 if (*keydist & SMP_DIST_SIGN) {
791 struct smp_cmd_sign_info sign;
792 struct smp_csrk *csrk;
793
794 /* Generate a new random key */
795 get_random_bytes(sign.csrk, sizeof(sign.csrk));
796
797 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
798 if (csrk) {
799 csrk->master = 0x00;
800 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
801 }
802 smp->slave_csrk = csrk;
803
804 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
805
806 *keydist &= ~SMP_DIST_SIGN;
807 }
808
809 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300810 if (smp->remote_key_dist & KEY_DIST_MASK) {
811 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300812 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300813 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300814
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300815 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
816 smp_notify_keys(conn);
817
818 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300819}
820
Johan Hedbergb68fda62014-08-11 22:06:40 +0300821static void smp_timeout(struct work_struct *work)
822{
823 struct smp_chan *smp = container_of(work, struct smp_chan,
824 security_timer.work);
825 struct l2cap_conn *conn = smp->conn;
826
827 BT_DBG("conn %p", conn);
828
Johan Hedberg1e91c292014-08-18 20:33:29 +0300829 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300830}
831
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300832static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
833{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300834 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300835 struct smp_chan *smp;
836
Marcel Holtmannf1560462013-10-13 05:43:25 -0700837 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300838 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300839 return NULL;
840
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300841 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
842 if (IS_ERR(smp->tfm_aes)) {
843 BT_ERR("Unable to create ECB crypto context");
844 kfree(smp);
845 return NULL;
846 }
847
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300848 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300849 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300850
Johan Hedbergb28b4942014-09-05 22:19:55 +0300851 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
852
Johan Hedbergb68fda62014-08-11 22:06:40 +0300853 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
854
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300855 hci_conn_hold(conn->hcon);
856
857 return smp;
858}
859
Brian Gix2b64d152011-12-21 16:12:12 -0800860int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
861{
Johan Hedbergb10e8012014-06-27 14:23:07 +0300862 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300863 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -0800864 struct smp_chan *smp;
865 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300866 int err;
Brian Gix2b64d152011-12-21 16:12:12 -0800867
868 BT_DBG("");
869
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300870 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -0800871 return -ENOTCONN;
872
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300873 chan = conn->smp;
874 if (!chan)
875 return -ENOTCONN;
876
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300877 l2cap_chan_lock(chan);
878 if (!chan->data) {
879 err = -ENOTCONN;
880 goto unlock;
881 }
882
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300883 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800884
885 switch (mgmt_op) {
886 case MGMT_OP_USER_PASSKEY_REPLY:
887 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +0200888 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800889 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +0200890 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800891 /* Fall Through */
892 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +0300893 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800894 break;
895 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
896 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +0200897 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300898 err = 0;
899 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800900 default:
Johan Hedberg84794e12013-11-06 11:24:57 +0200901 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300902 err = -EOPNOTSUPP;
903 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800904 }
905
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300906 err = 0;
907
Brian Gix2b64d152011-12-21 16:12:12 -0800908 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +0300909 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
910 u8 rsp = smp_confirm(smp);
911 if (rsp)
912 smp_failure(conn, rsp);
913 }
Brian Gix2b64d152011-12-21 16:12:12 -0800914
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300915unlock:
916 l2cap_chan_unlock(chan);
917 return err;
Brian Gix2b64d152011-12-21 16:12:12 -0800918}
919
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300920static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300921{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300922 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300923 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +0300924 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300925 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +0300926 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300927 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300928
929 BT_DBG("conn %p", conn);
930
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200931 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300932 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200933
Johan Hedberg40bef302014-07-16 11:42:27 +0300934 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -0800935 return SMP_CMD_NOTSUPP;
936
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300937 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300938 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300939 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300940 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300941
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +0300942 if (!smp)
943 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300944
Johan Hedbergc05b9332014-09-10 17:37:42 -0700945 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300946 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -0700947
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300948 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -0700949 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +0300950 return SMP_PAIRING_NOTSUPP;
951
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300952 smp->preq[0] = SMP_CMD_PAIRING_REQ;
953 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300954 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300955
Johan Hedberg5be5e272014-09-10 17:58:54 -0700956 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -0700957 sec_level = BT_SECURITY_MEDIUM;
958 else
959 sec_level = authreq_to_seclevel(auth);
960
Johan Hedbergc7262e72014-06-17 13:07:37 +0300961 if (sec_level > conn->hcon->pending_sec_level)
962 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +0200963
Stephen Hemminger49c922b2014-10-27 21:12:20 -0700964 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300965 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
966 u8 method;
967
968 method = get_auth_method(smp, conn->hcon->io_capability,
969 req->io_capability);
970 if (method == JUST_WORKS || method == JUST_CFM)
971 return SMP_AUTH_REQUIREMENTS;
972 }
973
Brian Gix2b64d152011-12-21 16:12:12 -0800974 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300975
976 key_size = min(req->max_key_size, rsp.max_key_size);
977 if (check_enc_key_size(conn, key_size))
978 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300979
Johan Hedberge84a6b12013-12-02 10:49:03 +0200980 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300981
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300982 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
983 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -0300984
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300985 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedbergb28b4942014-09-05 22:19:55 +0300986 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300987
Brian Gix2b64d152011-12-21 16:12:12 -0800988 /* Request setup of TK */
989 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
990 if (ret)
991 return SMP_UNSPECIFIED;
992
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300993 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300994}
995
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300996static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300997{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300998 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300999 struct l2cap_chan *chan = conn->smp;
1000 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001001 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001002 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001003 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001004
1005 BT_DBG("conn %p", conn);
1006
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001007 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001008 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001009
Johan Hedberg40bef302014-07-16 11:42:27 +03001010 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001011 return SMP_CMD_NOTSUPP;
1012
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001013 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001014
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001015 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001016
1017 key_size = min(req->max_key_size, rsp->max_key_size);
1018 if (check_enc_key_size(conn, key_size))
1019 return SMP_ENC_KEY_SIZE;
1020
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001021 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001022
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001023 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001024 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1025 u8 method;
1026
1027 method = get_auth_method(smp, req->io_capability,
1028 rsp->io_capability);
1029 if (method == JUST_WORKS || method == JUST_CFM)
1030 return SMP_AUTH_REQUIREMENTS;
1031 }
1032
Johan Hedberge84a6b12013-12-02 10:49:03 +02001033 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001034
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001035 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1036 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001037
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001038 /* Update remote key distribution in case the remote cleared
1039 * some bits that we had enabled in our request.
1040 */
1041 smp->remote_key_dist &= rsp->resp_key_dist;
1042
Johan Hedbergc05b9332014-09-10 17:37:42 -07001043 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001044
Johan Hedberg476585e2012-06-06 18:54:15 +08001045 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001046 if (ret)
1047 return SMP_UNSPECIFIED;
1048
Johan Hedberg4a74d652014-05-20 09:45:50 +03001049 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001050
1051 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001052 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001053 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001054
1055 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001056}
1057
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001058static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001059{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001060 struct l2cap_chan *chan = conn->smp;
1061 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001062
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001063 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1064
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001065 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001066 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001067
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001068 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1069 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001070
Johan Hedbergb28b4942014-09-05 22:19:55 +03001071 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001072 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1073 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001074 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1075 return 0;
1076 }
1077
1078 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001079 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001080 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001081 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001082
1083 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001084}
1085
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001086static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001087{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001088 struct l2cap_chan *chan = conn->smp;
1089 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001090
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001091 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001092
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001093 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001094 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001095
Johan Hedberg943a7322014-03-18 12:58:24 +02001096 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001097 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001098
Johan Hedberg861580a2014-05-20 09:45:51 +03001099 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001100}
1101
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001102static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001103{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001104 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001105 struct hci_conn *hcon = conn->hcon;
1106
Johan Hedberg98a0b842014-01-30 19:40:00 -08001107 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001108 hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001109 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001110 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001111
Johan Hedberga6f78332014-09-10 17:37:45 -07001112 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001113 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001114
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001115 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001116 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001117
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001118 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1119 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001120
Johan Hedbergfe59a052014-07-01 19:14:12 +03001121 /* We never store STKs for master role, so clear this flag */
1122 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1123
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001124 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001125}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001126
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001127bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1128 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03001129{
1130 if (sec_level == BT_SECURITY_LOW)
1131 return true;
1132
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001133 /* If we're encrypted with an STK but the caller prefers using
1134 * LTK claim insufficient security. This way we allow the
1135 * connection to be re-encrypted with an LTK, even if the LTK
1136 * provides the same level of security. Only exception is if we
1137 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001138 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001139 if (key_pref == SMP_USE_LTK &&
1140 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001141 hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001142 hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001143 return false;
1144
Johan Hedberg854f4722014-07-01 18:40:20 +03001145 if (hcon->sec_level >= sec_level)
1146 return true;
1147
1148 return false;
1149}
1150
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001151static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001152{
1153 struct smp_cmd_security_req *rp = (void *) skb->data;
1154 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001155 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001156 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001157 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07001158 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001159
1160 BT_DBG("conn %p", conn);
1161
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001162 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001163 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001164
Johan Hedberg40bef302014-07-16 11:42:27 +03001165 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001166 return SMP_CMD_NOTSUPP;
1167
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001168 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001169
Johan Hedberg5be5e272014-09-10 17:58:54 -07001170 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001171 sec_level = BT_SECURITY_MEDIUM;
1172 else
1173 sec_level = authreq_to_seclevel(auth);
1174
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001175 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03001176 return 0;
1177
Johan Hedbergc7262e72014-06-17 13:07:37 +03001178 if (sec_level > hcon->pending_sec_level)
1179 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001180
Johan Hedberg4dab7862012-06-07 14:58:37 +08001181 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001182 return 0;
1183
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001184 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001185 if (!smp)
1186 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001187
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001188 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001189 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03001190 return SMP_PAIRING_NOTSUPP;
1191
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001192 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001193
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001194 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07001195 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001196
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001197 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1198 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001199
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001200 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001201 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001202
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001203 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001204}
1205
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001206int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001207{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001208 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001209 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001210 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001211 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001212 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001213
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001214 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1215
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001216 /* This may be NULL if there's an unexpected disconnection */
1217 if (!conn)
1218 return 1;
1219
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001220 chan = conn->smp;
1221
Johan Hedberg757aee02013-04-24 13:05:32 +03001222 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001223 return 1;
1224
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001225 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001226 return 1;
1227
Johan Hedbergc7262e72014-06-17 13:07:37 +03001228 if (sec_level > hcon->pending_sec_level)
1229 hcon->pending_sec_level = sec_level;
1230
Johan Hedberg40bef302014-07-16 11:42:27 +03001231 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001232 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1233 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001234
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001235 l2cap_chan_lock(chan);
1236
1237 /* If SMP is already in progress ignore this request */
1238 if (chan->data) {
1239 ret = 0;
1240 goto unlock;
1241 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001242
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001243 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001244 if (!smp) {
1245 ret = 1;
1246 goto unlock;
1247 }
Brian Gix2b64d152011-12-21 16:12:12 -08001248
1249 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001250
Johan Hedberg79897d22014-06-01 09:45:24 +03001251 /* Require MITM if IO Capability allows or the security level
1252 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001253 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001254 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001255 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001256 authreq |= SMP_AUTH_MITM;
1257
Johan Hedberg40bef302014-07-16 11:42:27 +03001258 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001259 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001260
Brian Gix2b64d152011-12-21 16:12:12 -08001261 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001262 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1263 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001264
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001265 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001266 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001267 } else {
1268 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001269 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001270 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001271 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001272 }
1273
Johan Hedberg4a74d652014-05-20 09:45:50 +03001274 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001275 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001276
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001277unlock:
1278 l2cap_chan_unlock(chan);
1279 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001280}
1281
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001282static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1283{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001284 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001285 struct l2cap_chan *chan = conn->smp;
1286 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001287
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001288 BT_DBG("conn %p", conn);
1289
1290 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001291 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001292
Johan Hedbergb28b4942014-09-05 22:19:55 +03001293 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001294
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001295 skb_pull(skb, sizeof(*rp));
1296
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001297 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001298
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001299 return 0;
1300}
1301
1302static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1303{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001304 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001305 struct l2cap_chan *chan = conn->smp;
1306 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001307 struct hci_dev *hdev = conn->hcon->hdev;
1308 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001309 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001310 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001311
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001312 BT_DBG("conn %p", conn);
1313
1314 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001315 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001316
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001317 /* Mark the information as received */
1318 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1319
Johan Hedbergb28b4942014-09-05 22:19:55 +03001320 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1321 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07001322 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1323 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001324
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001325 skb_pull(skb, sizeof(*rp));
1326
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001327 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001328 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001329 authenticated, smp->tk, smp->enc_key_size,
1330 rp->ediv, rp->rand);
1331 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001332 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001333 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001334
1335 return 0;
1336}
1337
Johan Hedbergfd349c02014-02-18 10:19:36 +02001338static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1339{
1340 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001341 struct l2cap_chan *chan = conn->smp;
1342 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001343
1344 BT_DBG("");
1345
1346 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001347 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001348
Johan Hedbergb28b4942014-09-05 22:19:55 +03001349 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001350
Johan Hedbergfd349c02014-02-18 10:19:36 +02001351 skb_pull(skb, sizeof(*info));
1352
1353 memcpy(smp->irk, info->irk, 16);
1354
1355 return 0;
1356}
1357
1358static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1359 struct sk_buff *skb)
1360{
1361 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001362 struct l2cap_chan *chan = conn->smp;
1363 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001364 struct hci_conn *hcon = conn->hcon;
1365 bdaddr_t rpa;
1366
1367 BT_DBG("");
1368
1369 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001370 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001371
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001372 /* Mark the information as received */
1373 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1374
Johan Hedbergb28b4942014-09-05 22:19:55 +03001375 if (smp->remote_key_dist & SMP_DIST_SIGN)
1376 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1377
Johan Hedbergfd349c02014-02-18 10:19:36 +02001378 skb_pull(skb, sizeof(*info));
1379
Johan Hedberga9a58f82014-02-25 22:24:37 +02001380 /* Strictly speaking the Core Specification (4.1) allows sending
1381 * an empty address which would force us to rely on just the IRK
1382 * as "identity information". However, since such
1383 * implementations are not known of and in order to not over
1384 * complicate our implementation, simply pretend that we never
1385 * received an IRK for such a device.
1386 */
1387 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1388 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001389 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001390 }
1391
Johan Hedbergfd349c02014-02-18 10:19:36 +02001392 bacpy(&smp->id_addr, &info->bdaddr);
1393 smp->id_addr_type = info->addr_type;
1394
1395 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1396 bacpy(&rpa, &hcon->dst);
1397 else
1398 bacpy(&rpa, BDADDR_ANY);
1399
Johan Hedberg23d0e122014-02-19 14:57:46 +02001400 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1401 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001402
Johan Hedberg31dd6242014-06-27 14:23:02 +03001403distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001404 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1405 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001406
1407 return 0;
1408}
1409
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001410static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1411{
1412 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001413 struct l2cap_chan *chan = conn->smp;
1414 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001415 struct smp_csrk *csrk;
1416
1417 BT_DBG("conn %p", conn);
1418
1419 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001420 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001421
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001422 /* Mark the information as received */
1423 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1424
1425 skb_pull(skb, sizeof(*rp));
1426
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001427 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1428 if (csrk) {
1429 csrk->master = 0x01;
1430 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1431 }
1432 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001433 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001434
1435 return 0;
1436}
1437
Johan Hedberg4befb862014-08-11 22:06:38 +03001438static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001439{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001440 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001441 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001442 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001443 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001444 int err = 0;
1445
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001446 if (hcon->type != LE_LINK) {
1447 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001448 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001449 }
1450
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001451 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001452 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001453
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001454 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001455 reason = SMP_PAIRING_NOTSUPP;
1456 goto done;
1457 }
1458
Marcel Holtmann92381f52013-10-03 01:23:08 -07001459 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001460 skb_pull(skb, sizeof(code));
1461
Johan Hedbergb28b4942014-09-05 22:19:55 +03001462 smp = chan->data;
1463
1464 if (code > SMP_CMD_MAX)
1465 goto drop;
1466
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07001467 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03001468 goto drop;
1469
1470 /* If we don't have a context the only allowed commands are
1471 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001472 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001473 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1474 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001475
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001476 switch (code) {
1477 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001478 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001479 break;
1480
1481 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001482 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001483 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001484 break;
1485
1486 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001487 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001488 break;
1489
1490 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001491 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001492 break;
1493
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001494 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001495 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001496 break;
1497
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001498 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001499 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001500 break;
1501
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001502 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001503 reason = smp_cmd_encrypt_info(conn, skb);
1504 break;
1505
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001506 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001507 reason = smp_cmd_master_ident(conn, skb);
1508 break;
1509
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001510 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001511 reason = smp_cmd_ident_info(conn, skb);
1512 break;
1513
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001514 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001515 reason = smp_cmd_ident_addr_info(conn, skb);
1516 break;
1517
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001518 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001519 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001520 break;
1521
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001522 default:
1523 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001524 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001525 goto done;
1526 }
1527
1528done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001529 if (!err) {
1530 if (reason)
1531 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001532 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001533 }
1534
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001535 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001536
1537drop:
1538 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
1539 code, &hcon->dst);
1540 kfree_skb(skb);
1541 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001542}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001543
Johan Hedberg70db83c2014-08-08 09:37:16 +03001544static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1545{
1546 struct l2cap_conn *conn = chan->conn;
1547
1548 BT_DBG("chan %p", chan);
1549
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001550 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001551 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001552
Johan Hedberg70db83c2014-08-08 09:37:16 +03001553 conn->smp = NULL;
1554 l2cap_chan_put(chan);
1555}
1556
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001557static void smp_resume_cb(struct l2cap_chan *chan)
1558{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001559 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001560 struct l2cap_conn *conn = chan->conn;
1561 struct hci_conn *hcon = conn->hcon;
1562
1563 BT_DBG("chan %p", chan);
1564
Johan Hedberg86d14072014-08-11 22:06:43 +03001565 if (!smp)
1566 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001567
Johan Hedberg84bc0db2014-09-05 22:19:49 +03001568 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1569 return;
1570
Johan Hedberg86d14072014-08-11 22:06:43 +03001571 cancel_delayed_work(&smp->security_timer);
1572
Johan Hedbergd6268e82014-09-05 22:19:51 +03001573 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001574}
1575
Johan Hedberg70db83c2014-08-08 09:37:16 +03001576static void smp_ready_cb(struct l2cap_chan *chan)
1577{
1578 struct l2cap_conn *conn = chan->conn;
1579
1580 BT_DBG("chan %p", chan);
1581
1582 conn->smp = chan;
1583 l2cap_chan_hold(chan);
1584}
1585
Johan Hedberg4befb862014-08-11 22:06:38 +03001586static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1587{
1588 int err;
1589
1590 BT_DBG("chan %p", chan);
1591
1592 err = smp_sig_channel(chan, skb);
1593 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001594 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001595
Johan Hedbergb68fda62014-08-11 22:06:40 +03001596 if (smp)
1597 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001598
Johan Hedberg1e91c292014-08-18 20:33:29 +03001599 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03001600 }
1601
1602 return err;
1603}
1604
Johan Hedberg70db83c2014-08-08 09:37:16 +03001605static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1606 unsigned long hdr_len,
1607 unsigned long len, int nb)
1608{
1609 struct sk_buff *skb;
1610
1611 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1612 if (!skb)
1613 return ERR_PTR(-ENOMEM);
1614
1615 skb->priority = HCI_PRIO_MAX;
1616 bt_cb(skb)->chan = chan;
1617
1618 return skb;
1619}
1620
1621static const struct l2cap_ops smp_chan_ops = {
1622 .name = "Security Manager",
1623 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001624 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001625 .alloc_skb = smp_alloc_skb_cb,
1626 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001627 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001628
1629 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001630 .state_change = l2cap_chan_no_state_change,
1631 .close = l2cap_chan_no_close,
1632 .defer = l2cap_chan_no_defer,
1633 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001634 .set_shutdown = l2cap_chan_no_set_shutdown,
1635 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1636 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1637};
1638
1639static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1640{
1641 struct l2cap_chan *chan;
1642
1643 BT_DBG("pchan %p", pchan);
1644
1645 chan = l2cap_chan_create();
1646 if (!chan)
1647 return NULL;
1648
1649 chan->chan_type = pchan->chan_type;
1650 chan->ops = &smp_chan_ops;
1651 chan->scid = pchan->scid;
1652 chan->dcid = chan->scid;
1653 chan->imtu = pchan->imtu;
1654 chan->omtu = pchan->omtu;
1655 chan->mode = pchan->mode;
1656
Johan Hedbergabe84902014-11-12 22:22:21 +02001657 /* Other L2CAP channels may request SMP routines in order to
1658 * change the security level. This means that the SMP channel
1659 * lock must be considered in its own category to avoid lockdep
1660 * warnings.
1661 */
1662 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
1663
Johan Hedberg70db83c2014-08-08 09:37:16 +03001664 BT_DBG("created chan %p", chan);
1665
1666 return chan;
1667}
1668
1669static const struct l2cap_ops smp_root_chan_ops = {
1670 .name = "Security Manager Root",
1671 .new_connection = smp_new_conn_cb,
1672
1673 /* None of these are implemented for the root channel */
1674 .close = l2cap_chan_no_close,
1675 .alloc_skb = l2cap_chan_no_alloc_skb,
1676 .recv = l2cap_chan_no_recv,
1677 .state_change = l2cap_chan_no_state_change,
1678 .teardown = l2cap_chan_no_teardown,
1679 .ready = l2cap_chan_no_ready,
1680 .defer = l2cap_chan_no_defer,
1681 .suspend = l2cap_chan_no_suspend,
1682 .resume = l2cap_chan_no_resume,
1683 .set_shutdown = l2cap_chan_no_set_shutdown,
1684 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1685 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1686};
1687
Johan Hedberg711eafe2014-08-08 09:32:52 +03001688int smp_register(struct hci_dev *hdev)
1689{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001690 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001691 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001692
Johan Hedberg711eafe2014-08-08 09:32:52 +03001693 BT_DBG("%s", hdev->name);
1694
Johan Hedbergadae20c2014-11-13 14:37:48 +02001695 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001696 if (IS_ERR(tfm_aes)) {
1697 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001698 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03001699 return err;
1700 }
1701
Johan Hedberg70db83c2014-08-08 09:37:16 +03001702 chan = l2cap_chan_create();
1703 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001704 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001705 return -ENOMEM;
1706 }
1707
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001708 chan->data = tfm_aes;
1709
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001710 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001711
1712 l2cap_chan_set_defaults(chan);
1713
1714 bacpy(&chan->src, &hdev->bdaddr);
1715 chan->src_type = BDADDR_LE_PUBLIC;
1716 chan->state = BT_LISTEN;
1717 chan->mode = L2CAP_MODE_BASIC;
1718 chan->imtu = L2CAP_DEFAULT_MTU;
1719 chan->ops = &smp_root_chan_ops;
1720
Johan Hedbergabe84902014-11-12 22:22:21 +02001721 /* Set correct nesting level for a parent/listening channel */
1722 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
1723
Johan Hedberg70db83c2014-08-08 09:37:16 +03001724 hdev->smp_data = chan;
1725
Johan Hedberg711eafe2014-08-08 09:32:52 +03001726 return 0;
1727}
1728
1729void smp_unregister(struct hci_dev *hdev)
1730{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001731 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001732 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001733
1734 if (!chan)
1735 return;
1736
1737 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001738
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001739 tfm_aes = chan->data;
1740 if (tfm_aes) {
1741 chan->data = NULL;
1742 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001743 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03001744
1745 hdev->smp_data = NULL;
1746 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001747}