blob: f09b6b65cf6b22079d6ce60d53f2613ccf6a7745 [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 Hedberg065a13e2012-10-11 16:26:06 +020038#define AUTH_REQ_MASK 0x07
Johan Hedberg88d3a8a2014-09-05 22:19:53 +030039#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020040
Johan Hedberg533e35d2014-06-16 19:25:18 +030041enum {
42 SMP_FLAG_TK_VALID,
43 SMP_FLAG_CFM_PENDING,
44 SMP_FLAG_MITM_AUTH,
45 SMP_FLAG_COMPLETE,
46 SMP_FLAG_INITIATOR,
47};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030048
49struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030050 struct l2cap_conn *conn;
51 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030052 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030053
Johan Hedberg4bc58f52014-05-20 09:45:47 +030054 u8 preq[7]; /* SMP Pairing Request */
55 u8 prsp[7]; /* SMP Pairing Response */
56 u8 prnd[16]; /* SMP Pairing Random (local) */
57 u8 rrnd[16]; /* SMP Pairing Random (remote) */
58 u8 pcnf[16]; /* SMP Pairing Confirm */
59 u8 tk[16]; /* SMP Temporary Key */
60 u8 enc_key_size;
61 u8 remote_key_dist;
62 bdaddr_t id_addr;
63 u8 id_addr_type;
64 u8 irk[16];
65 struct smp_csrk *csrk;
66 struct smp_csrk *slave_csrk;
67 struct smp_ltk *ltk;
68 struct smp_ltk *slave_ltk;
69 struct smp_irk *remote_irk;
Johan Hedberg4a74d652014-05-20 09:45:50 +030070 unsigned long flags;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030071
72 struct crypto_blkcipher *tfm_aes;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030073};
74
Johan Hedberg8a2936f2014-06-16 19:25:19 +030075static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030076{
Johan Hedberg8a2936f2014-06-16 19:25:19 +030077 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030078
Johan Hedberg8a2936f2014-06-16 19:25:19 +030079 for (i = 0; i < len; i++)
80 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030081}
82
83static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
84{
85 struct blkcipher_desc desc;
86 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +020087 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +020088 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030089
90 if (tfm == NULL) {
91 BT_ERR("tfm %p", tfm);
92 return -EINVAL;
93 }
94
95 desc.tfm = tfm;
96 desc.flags = 0;
97
Johan Hedberg943a7322014-03-18 12:58:24 +020098 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +030099 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200100
101 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300102 if (err) {
103 BT_ERR("cipher setkey failed: %d", err);
104 return err;
105 }
106
Johan Hedberg943a7322014-03-18 12:58:24 +0200107 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300108 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200109
110 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300111
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300112 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
113 if (err)
114 BT_ERR("Encrypt data error %d", err);
115
Johan Hedberg943a7322014-03-18 12:58:24 +0200116 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300117 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200118
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300119 return err;
120}
121
Johan Hedberg60478052014-02-18 10:19:31 +0200122static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
123{
Johan Hedberg943a7322014-03-18 12:58:24 +0200124 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200125 int err;
126
127 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200128 memcpy(_res, r, 3);
129 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200130
Johan Hedberg943a7322014-03-18 12:58:24 +0200131 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200132 if (err) {
133 BT_ERR("Encrypt error");
134 return err;
135 }
136
137 /* The output of the random address function ah is:
138 * ah(h, r) = e(k, r') mod 2^24
139 * The output of the security function e is then truncated to 24 bits
140 * by taking the least significant 24 bits of the output of e as the
141 * result of ah.
142 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200143 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200144
145 return 0;
146}
147
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300148bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200149{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300150 struct l2cap_chan *chan = hdev->smp_data;
151 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200152 u8 hash[3];
153 int err;
154
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300155 if (!chan || !chan->data)
156 return false;
157
158 tfm = chan->data;
159
Johan Hedberg60478052014-02-18 10:19:31 +0200160 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
161
162 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
163 if (err)
164 return false;
165
166 return !memcmp(bdaddr->b, hash, 3);
167}
168
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300169int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200170{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300171 struct l2cap_chan *chan = hdev->smp_data;
172 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200173 int err;
174
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300175 if (!chan || !chan->data)
176 return -EOPNOTSUPP;
177
178 tfm = chan->data;
179
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200180 get_random_bytes(&rpa->b[3], 3);
181
182 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
183 rpa->b[5] |= 0x40; /* Set second most significant bit */
184
185 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
186 if (err < 0)
187 return err;
188
189 BT_DBG("RPA %pMR", rpa);
190
191 return 0;
192}
193
Johan Hedbergec70f362014-06-27 14:23:04 +0300194static int smp_c1(struct smp_chan *smp, u8 k[16], u8 r[16], u8 preq[7],
195 u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, bdaddr_t *ra,
196 u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300197{
Johan Hedbergec70f362014-06-27 14:23:04 +0300198 struct hci_dev *hdev = smp->conn->hcon->hdev;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300199 u8 p1[16], p2[16];
200 int err;
201
Johan Hedbergec70f362014-06-27 14:23:04 +0300202 BT_DBG("%s", hdev->name);
203
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300204 memset(p1, 0, 16);
205
206 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200207 p1[0] = _iat;
208 p1[1] = _rat;
209 memcpy(p1 + 2, preq, 7);
210 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300211
212 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200213 memcpy(p2, ra, 6);
214 memcpy(p2 + 6, ia, 6);
215 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300216
217 /* res = r XOR p1 */
218 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
219
220 /* res = e(k, res) */
Johan Hedbergec70f362014-06-27 14:23:04 +0300221 err = smp_e(smp->tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300222 if (err) {
223 BT_ERR("Encrypt data error");
224 return err;
225 }
226
227 /* res = res XOR p2 */
228 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
229
230 /* res = e(k, res) */
Johan Hedbergec70f362014-06-27 14:23:04 +0300231 err = smp_e(smp->tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300232 if (err)
233 BT_ERR("Encrypt data error");
234
235 return err;
236}
237
Johan Hedbergec70f362014-06-27 14:23:04 +0300238static int smp_s1(struct smp_chan *smp, u8 k[16], u8 r1[16], u8 r2[16],
239 u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300240{
Johan Hedbergec70f362014-06-27 14:23:04 +0300241 struct hci_dev *hdev = smp->conn->hcon->hdev;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300242 int err;
243
Johan Hedbergec70f362014-06-27 14:23:04 +0300244 BT_DBG("%s", hdev->name);
245
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300246 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200247 memcpy(_r, r2, 8);
248 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300249
Johan Hedbergec70f362014-06-27 14:23:04 +0300250 err = smp_e(smp->tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300251 if (err)
252 BT_ERR("Encrypt data error");
253
254 return err;
255}
256
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300257static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
258{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300259 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300260 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300261 struct kvec iv[2];
262 struct msghdr msg;
263
264 if (!chan)
265 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300266
267 BT_DBG("code 0x%2.2x", code);
268
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300269 iv[0].iov_base = &code;
270 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300271
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300272 iv[1].iov_base = data;
273 iv[1].iov_len = len;
274
275 memset(&msg, 0, sizeof(msg));
276
277 msg.msg_iov = (struct iovec *) &iv;
278 msg.msg_iovlen = 2;
279
280 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300281
Johan Hedbergb68fda62014-08-11 22:06:40 +0300282 if (!chan->data)
283 return;
284
285 smp = chan->data;
286
287 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300288 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300289}
290
Brian Gix2b64d152011-12-21 16:12:12 -0800291static __u8 authreq_to_seclevel(__u8 authreq)
292{
293 if (authreq & SMP_AUTH_MITM)
294 return BT_SECURITY_HIGH;
295 else
296 return BT_SECURITY_MEDIUM;
297}
298
299static __u8 seclevel_to_authreq(__u8 sec_level)
300{
301 switch (sec_level) {
302 case BT_SECURITY_HIGH:
303 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
304 case BT_SECURITY_MEDIUM:
305 return SMP_AUTH_BONDING;
306 default:
307 return SMP_AUTH_NONE;
308 }
309}
310
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300311static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700312 struct smp_cmd_pairing *req,
313 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300314{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300315 struct l2cap_chan *chan = conn->smp;
316 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200317 struct hci_conn *hcon = conn->hcon;
318 struct hci_dev *hdev = hcon->hdev;
319 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300320
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300321 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700322 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
323 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300324 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800325 } else {
326 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300327 }
328
Johan Hedbergfd349c02014-02-18 10:19:36 +0200329 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
330 remote_dist |= SMP_DIST_ID_KEY;
331
Johan Hedberg863efaf2014-02-22 19:06:32 +0200332 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
333 local_dist |= SMP_DIST_ID_KEY;
334
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300335 if (rsp == NULL) {
336 req->io_capability = conn->hcon->io_capability;
337 req->oob_flag = SMP_OOB_NOT_PRESENT;
338 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200339 req->init_key_dist = local_dist;
340 req->resp_key_dist = remote_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200341 req->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200342
343 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300344 return;
345 }
346
347 rsp->io_capability = conn->hcon->io_capability;
348 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
349 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200350 rsp->init_key_dist = req->init_key_dist & remote_dist;
351 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200352 rsp->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200353
354 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300355}
356
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300357static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
358{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300359 struct l2cap_chan *chan = conn->smp;
360 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300361
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300362 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700363 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300364 return SMP_ENC_KEY_SIZE;
365
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300366 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300367
368 return 0;
369}
370
Johan Hedberg6f48e262014-08-11 22:06:44 +0300371static void smp_chan_destroy(struct l2cap_conn *conn)
372{
373 struct l2cap_chan *chan = conn->smp;
374 struct smp_chan *smp = chan->data;
375 bool complete;
376
377 BUG_ON(!smp);
378
379 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300380
Johan Hedberg6f48e262014-08-11 22:06:44 +0300381 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
382 mgmt_smp_complete(conn->hcon, complete);
383
384 kfree(smp->csrk);
385 kfree(smp->slave_csrk);
386
387 crypto_free_blkcipher(smp->tfm_aes);
388
389 /* If pairing failed clean up any keys we might have */
390 if (!complete) {
391 if (smp->ltk) {
392 list_del(&smp->ltk->list);
393 kfree(smp->ltk);
394 }
395
396 if (smp->slave_ltk) {
397 list_del(&smp->slave_ltk->list);
398 kfree(smp->slave_ltk);
399 }
400
401 if (smp->remote_irk) {
402 list_del(&smp->remote_irk->list);
403 kfree(smp->remote_irk);
404 }
405 }
406
407 chan->data = NULL;
408 kfree(smp);
409 hci_conn_drop(conn->hcon);
410}
411
Johan Hedberg84794e12013-11-06 11:24:57 +0200412static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800413{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200414 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300415 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200416
Johan Hedberg84794e12013-11-06 11:24:57 +0200417 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800418 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700419 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800420
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700421 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700422 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300423
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300424 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300425 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800426}
427
Brian Gix2b64d152011-12-21 16:12:12 -0800428#define JUST_WORKS 0x00
429#define JUST_CFM 0x01
430#define REQ_PASSKEY 0x02
431#define CFM_PASSKEY 0x03
432#define REQ_OOB 0x04
433#define OVERLAP 0xFF
434
435static const u8 gen_method[5][5] = {
436 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
437 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
438 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
439 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
440 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
441};
442
Johan Hedberg581370c2014-06-17 13:07:38 +0300443static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
444{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300445 /* If either side has unknown io_caps, use JUST_CFM (which gets
446 * converted later to JUST_WORKS if we're initiators.
447 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300448 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
449 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300450 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300451
452 return gen_method[remote_io][local_io];
453}
454
Brian Gix2b64d152011-12-21 16:12:12 -0800455static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
456 u8 local_io, u8 remote_io)
457{
458 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300459 struct l2cap_chan *chan = conn->smp;
460 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800461 u8 method;
462 u32 passkey = 0;
463 int ret = 0;
464
465 /* Initialize key for JUST WORKS */
466 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300467 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800468
469 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
470
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300471 /* If neither side wants MITM, either "just" confirm an incoming
472 * request or use just-works for outgoing ones. The JUST_CFM
473 * will be converted to JUST_WORKS if necessary later in this
474 * function. If either side has MITM look up the method from the
475 * table.
476 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300477 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300478 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800479 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300480 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800481
Johan Hedberga82505c2014-03-24 14:39:07 +0200482 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300483 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200484 method = JUST_WORKS;
485
Johan Hedberg02f3e252014-07-16 15:09:13 +0300486 /* Don't bother user space with no IO capabilities */
487 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
488 method = JUST_WORKS;
489
Brian Gix2b64d152011-12-21 16:12:12 -0800490 /* If Just Works, Continue with Zero TK */
491 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300492 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800493 return 0;
494 }
495
496 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300497 if (method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300498 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300499 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
500 hcon->pending_sec_level = BT_SECURITY_HIGH;
501 }
Brian Gix2b64d152011-12-21 16:12:12 -0800502
503 /* If both devices have Keyoard-Display I/O, the master
504 * Confirms and the slave Enters the passkey.
505 */
506 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300507 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800508 method = CFM_PASSKEY;
509 else
510 method = REQ_PASSKEY;
511 }
512
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200513 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800514 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200515 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800516 get_random_bytes(&passkey, sizeof(passkey));
517 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200518 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800519 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300520 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800521 }
522
523 hci_dev_lock(hcon->hdev);
524
525 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700526 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200527 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200528 else if (method == JUST_CFM)
529 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
530 hcon->type, hcon->dst_type,
531 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800532 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200533 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200534 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200535 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800536
537 hci_dev_unlock(hcon->hdev);
538
539 return ret;
540}
541
Johan Hedberg1cc61142014-05-20 09:45:52 +0300542static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300543{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300544 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300545 struct smp_cmd_pairing_confirm cp;
546 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300547
548 BT_DBG("conn %p", conn);
549
Johan Hedbergec70f362014-06-27 14:23:04 +0300550 ret = smp_c1(smp, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200551 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200552 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
553 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300554 if (ret)
555 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300556
Johan Hedberg4a74d652014-05-20 09:45:50 +0300557 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800558
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300559 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
560
Johan Hedbergb28b4942014-09-05 22:19:55 +0300561 if (conn->hcon->out)
562 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
563 else
564 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
565
Johan Hedberg1cc61142014-05-20 09:45:52 +0300566 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300567}
568
Johan Hedberg861580a2014-05-20 09:45:51 +0300569static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300570{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300571 struct l2cap_conn *conn = smp->conn;
572 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300573 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300574 int ret;
575
Johan Hedbergec70f362014-06-27 14:23:04 +0300576 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300577 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300578
579 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
580
Johan Hedbergec70f362014-06-27 14:23:04 +0300581 ret = smp_c1(smp, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200582 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200583 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300584 if (ret)
585 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300586
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300587 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
588 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300589 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300590 }
591
592 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800593 u8 stk[16];
594 __le64 rand = 0;
595 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300596
Johan Hedbergec70f362014-06-27 14:23:04 +0300597 smp_s1(smp, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300598
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300599 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300600 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300601
Johan Hedberg861580a2014-05-20 09:45:51 +0300602 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
603 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300604
605 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300606 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300607 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300608 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300609 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800610 __le64 rand = 0;
611 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300612
Johan Hedberg943a7322014-03-18 12:58:24 +0200613 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
614 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300615
Johan Hedbergec70f362014-06-27 14:23:04 +0300616 smp_s1(smp, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300617
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300618 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700619 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300620
Johan Hedbergfff34902014-06-10 15:19:50 +0300621 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
622 auth = 1;
623 else
624 auth = 0;
625
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300626 /* Even though there's no _SLAVE suffix this is the
627 * slave STK we're adding for later lookup (the master
628 * STK never needs to be stored).
629 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700630 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300631 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300632 }
633
Johan Hedberg861580a2014-05-20 09:45:51 +0300634 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300635}
636
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300637static void smp_notify_keys(struct l2cap_conn *conn)
638{
639 struct l2cap_chan *chan = conn->smp;
640 struct smp_chan *smp = chan->data;
641 struct hci_conn *hcon = conn->hcon;
642 struct hci_dev *hdev = hcon->hdev;
643 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
644 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
645 bool persistent;
646
647 if (smp->remote_irk) {
648 mgmt_new_irk(hdev, smp->remote_irk);
649 /* Now that user space can be considered to know the
650 * identity address track the connection based on it
651 * from now on.
652 */
653 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
654 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300655 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300656
657 /* When receiving an indentity resolving key for
658 * a remote device that does not use a resolvable
659 * private address, just remove the key so that
660 * it is possible to use the controller white
661 * list for scanning.
662 *
663 * Userspace will have been told to not store
664 * this key at this point. So it is safe to
665 * just remove it.
666 */
667 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
668 list_del(&smp->remote_irk->list);
669 kfree(smp->remote_irk);
670 smp->remote_irk = NULL;
671 }
672 }
673
674 /* The LTKs and CSRKs should be persistent only if both sides
675 * had the bonding bit set in their authentication requests.
676 */
677 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
678
679 if (smp->csrk) {
680 smp->csrk->bdaddr_type = hcon->dst_type;
681 bacpy(&smp->csrk->bdaddr, &hcon->dst);
682 mgmt_new_csrk(hdev, smp->csrk, persistent);
683 }
684
685 if (smp->slave_csrk) {
686 smp->slave_csrk->bdaddr_type = hcon->dst_type;
687 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
688 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
689 }
690
691 if (smp->ltk) {
692 smp->ltk->bdaddr_type = hcon->dst_type;
693 bacpy(&smp->ltk->bdaddr, &hcon->dst);
694 mgmt_new_ltk(hdev, smp->ltk, persistent);
695 }
696
697 if (smp->slave_ltk) {
698 smp->slave_ltk->bdaddr_type = hcon->dst_type;
699 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
700 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
701 }
702}
703
Johan Hedbergb28b4942014-09-05 22:19:55 +0300704static void smp_allow_key_dist(struct smp_chan *smp)
705{
706 /* Allow the first expected phase 3 PDU. The rest of the PDUs
707 * will be allowed in each PDU handler to ensure we receive
708 * them in the correct order.
709 */
710 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
711 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
712 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
713 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
714 else if (smp->remote_key_dist & SMP_DIST_SIGN)
715 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
716}
717
Johan Hedbergd6268e82014-09-05 22:19:51 +0300718static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300719{
720 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300721 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300722 struct hci_conn *hcon = conn->hcon;
723 struct hci_dev *hdev = hcon->hdev;
724 __u8 *keydist;
725
726 BT_DBG("conn %p", conn);
727
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300728 rsp = (void *) &smp->prsp[1];
729
730 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300731 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
732 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300733 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300734 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300735
736 req = (void *) &smp->preq[1];
737
738 if (hcon->out) {
739 keydist = &rsp->init_key_dist;
740 *keydist &= req->init_key_dist;
741 } else {
742 keydist = &rsp->resp_key_dist;
743 *keydist &= req->resp_key_dist;
744 }
745
746 BT_DBG("keydist 0x%x", *keydist);
747
748 if (*keydist & SMP_DIST_ENC_KEY) {
749 struct smp_cmd_encrypt_info enc;
750 struct smp_cmd_master_ident ident;
751 struct smp_ltk *ltk;
752 u8 authenticated;
753 __le16 ediv;
754 __le64 rand;
755
756 get_random_bytes(enc.ltk, sizeof(enc.ltk));
757 get_random_bytes(&ediv, sizeof(ediv));
758 get_random_bytes(&rand, sizeof(rand));
759
760 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
761
762 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
763 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
764 SMP_LTK_SLAVE, authenticated, enc.ltk,
765 smp->enc_key_size, ediv, rand);
766 smp->slave_ltk = ltk;
767
768 ident.ediv = ediv;
769 ident.rand = rand;
770
771 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
772
773 *keydist &= ~SMP_DIST_ENC_KEY;
774 }
775
776 if (*keydist & SMP_DIST_ID_KEY) {
777 struct smp_cmd_ident_addr_info addrinfo;
778 struct smp_cmd_ident_info idinfo;
779
780 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
781
782 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
783
784 /* The hci_conn contains the local identity address
785 * after the connection has been established.
786 *
787 * This is true even when the connection has been
788 * established using a resolvable random address.
789 */
790 bacpy(&addrinfo.bdaddr, &hcon->src);
791 addrinfo.addr_type = hcon->src_type;
792
793 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
794 &addrinfo);
795
796 *keydist &= ~SMP_DIST_ID_KEY;
797 }
798
799 if (*keydist & SMP_DIST_SIGN) {
800 struct smp_cmd_sign_info sign;
801 struct smp_csrk *csrk;
802
803 /* Generate a new random key */
804 get_random_bytes(sign.csrk, sizeof(sign.csrk));
805
806 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
807 if (csrk) {
808 csrk->master = 0x00;
809 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
810 }
811 smp->slave_csrk = csrk;
812
813 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
814
815 *keydist &= ~SMP_DIST_SIGN;
816 }
817
818 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300819 if (smp->remote_key_dist & KEY_DIST_MASK) {
820 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300821 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300822 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300823
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300824 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
825 smp_notify_keys(conn);
826
827 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300828}
829
Johan Hedbergb68fda62014-08-11 22:06:40 +0300830static void smp_timeout(struct work_struct *work)
831{
832 struct smp_chan *smp = container_of(work, struct smp_chan,
833 security_timer.work);
834 struct l2cap_conn *conn = smp->conn;
835
836 BT_DBG("conn %p", conn);
837
Johan Hedberg1e91c292014-08-18 20:33:29 +0300838 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300839}
840
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300841static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
842{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300843 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300844 struct smp_chan *smp;
845
Marcel Holtmannf1560462013-10-13 05:43:25 -0700846 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300847 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300848 return NULL;
849
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300850 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
851 if (IS_ERR(smp->tfm_aes)) {
852 BT_ERR("Unable to create ECB crypto context");
853 kfree(smp);
854 return NULL;
855 }
856
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300857 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300858 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300859
Johan Hedbergb28b4942014-09-05 22:19:55 +0300860 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
861
Johan Hedbergb68fda62014-08-11 22:06:40 +0300862 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
863
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300864 hci_conn_hold(conn->hcon);
865
866 return smp;
867}
868
Brian Gix2b64d152011-12-21 16:12:12 -0800869int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
870{
Johan Hedbergb10e8012014-06-27 14:23:07 +0300871 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300872 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -0800873 struct smp_chan *smp;
874 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300875 int err;
Brian Gix2b64d152011-12-21 16:12:12 -0800876
877 BT_DBG("");
878
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300879 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -0800880 return -ENOTCONN;
881
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300882 chan = conn->smp;
883 if (!chan)
884 return -ENOTCONN;
885
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300886 l2cap_chan_lock(chan);
887 if (!chan->data) {
888 err = -ENOTCONN;
889 goto unlock;
890 }
891
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300892 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800893
894 switch (mgmt_op) {
895 case MGMT_OP_USER_PASSKEY_REPLY:
896 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +0200897 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800898 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +0200899 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800900 /* Fall Through */
901 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +0300902 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800903 break;
904 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
905 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +0200906 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300907 err = 0;
908 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800909 default:
Johan Hedberg84794e12013-11-06 11:24:57 +0200910 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300911 err = -EOPNOTSUPP;
912 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800913 }
914
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300915 err = 0;
916
Brian Gix2b64d152011-12-21 16:12:12 -0800917 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +0300918 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
919 u8 rsp = smp_confirm(smp);
920 if (rsp)
921 smp_failure(conn, rsp);
922 }
Brian Gix2b64d152011-12-21 16:12:12 -0800923
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300924unlock:
925 l2cap_chan_unlock(chan);
926 return err;
Brian Gix2b64d152011-12-21 16:12:12 -0800927}
928
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300929static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300930{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300931 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300932 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +0300933 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300934 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +0300935 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300936 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300937
938 BT_DBG("conn %p", conn);
939
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200940 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300941 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200942
Johan Hedberg40bef302014-07-16 11:42:27 +0300943 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -0800944 return SMP_CMD_NOTSUPP;
945
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300946 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300947 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300948 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300949 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300950
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +0300951 if (!smp)
952 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300953
Johan Hedbergc05b9332014-09-10 17:37:42 -0700954 /* We didn't start the pairing, so match remote */
955 auth = req->auth_req & AUTH_REQ_MASK;
956
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300957 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -0700958 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +0300959 return SMP_PAIRING_NOTSUPP;
960
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300961 smp->preq[0] = SMP_CMD_PAIRING_REQ;
962 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300963 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300964
Johan Hedberg5be5e272014-09-10 17:58:54 -0700965 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -0700966 sec_level = BT_SECURITY_MEDIUM;
967 else
968 sec_level = authreq_to_seclevel(auth);
969
Johan Hedbergc7262e72014-06-17 13:07:37 +0300970 if (sec_level > conn->hcon->pending_sec_level)
971 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +0200972
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300973 /* If we need MITM check that it can be acheived */
974 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
975 u8 method;
976
977 method = get_auth_method(smp, conn->hcon->io_capability,
978 req->io_capability);
979 if (method == JUST_WORKS || method == JUST_CFM)
980 return SMP_AUTH_REQUIREMENTS;
981 }
982
Brian Gix2b64d152011-12-21 16:12:12 -0800983 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300984
985 key_size = min(req->max_key_size, rsp.max_key_size);
986 if (check_enc_key_size(conn, key_size))
987 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300988
Johan Hedberge84a6b12013-12-02 10:49:03 +0200989 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300990
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300991 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
992 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -0300993
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300994 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedbergb28b4942014-09-05 22:19:55 +0300995 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300996
Brian Gix2b64d152011-12-21 16:12:12 -0800997 /* Request setup of TK */
998 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
999 if (ret)
1000 return SMP_UNSPECIFIED;
1001
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001002 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001003}
1004
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001005static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001006{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001007 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001008 struct l2cap_chan *chan = conn->smp;
1009 struct smp_chan *smp = chan->data;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001010 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001011 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001012
1013 BT_DBG("conn %p", conn);
1014
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001015 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001016 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001017
Johan Hedberg40bef302014-07-16 11:42:27 +03001018 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001019 return SMP_CMD_NOTSUPP;
1020
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001021 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001022
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001023 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001024
1025 key_size = min(req->max_key_size, rsp->max_key_size);
1026 if (check_enc_key_size(conn, key_size))
1027 return SMP_ENC_KEY_SIZE;
1028
Johan Hedbergc05b9332014-09-10 17:37:42 -07001029 auth = rsp->auth_req & AUTH_REQ_MASK;
1030
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001031 /* If we need MITM check that it can be acheived */
1032 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1033 u8 method;
1034
1035 method = get_auth_method(smp, req->io_capability,
1036 rsp->io_capability);
1037 if (method == JUST_WORKS || method == JUST_CFM)
1038 return SMP_AUTH_REQUIREMENTS;
1039 }
1040
Johan Hedberge84a6b12013-12-02 10:49:03 +02001041 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001042
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001043 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1044 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001045
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001046 /* Update remote key distribution in case the remote cleared
1047 * some bits that we had enabled in our request.
1048 */
1049 smp->remote_key_dist &= rsp->resp_key_dist;
1050
Johan Hedbergc05b9332014-09-10 17:37:42 -07001051 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001052
Johan Hedberg476585e2012-06-06 18:54:15 +08001053 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001054 if (ret)
1055 return SMP_UNSPECIFIED;
1056
Johan Hedberg4a74d652014-05-20 09:45:50 +03001057 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001058
1059 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001060 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001061 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001062
1063 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001064}
1065
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001066static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001067{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001068 struct l2cap_chan *chan = conn->smp;
1069 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001070
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001071 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1072
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001073 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001074 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001075
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001076 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1077 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001078
Johan Hedbergb28b4942014-09-05 22:19:55 +03001079 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001080 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1081 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001082 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1083 return 0;
1084 }
1085
1086 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001087 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001088 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001089 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001090
1091 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001092}
1093
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001094static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001095{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001096 struct l2cap_chan *chan = conn->smp;
1097 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001098
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001099 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001100
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001101 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001102 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001103
Johan Hedberg943a7322014-03-18 12:58:24 +02001104 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001105 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001106
Johan Hedberg861580a2014-05-20 09:45:51 +03001107 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001108}
1109
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001110static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001111{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001112 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001113 struct hci_conn *hcon = conn->hcon;
1114
Johan Hedberg98a0b842014-01-30 19:40:00 -08001115 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001116 hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001117 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001118 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001119
Johan Hedberga6f78332014-09-10 17:37:45 -07001120 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001121 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001122
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001123 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001124 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001125
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001126 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1127 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001128
Johan Hedbergfe59a052014-07-01 19:14:12 +03001129 /* We never store STKs for master role, so clear this flag */
1130 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1131
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001132 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001133}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001134
Johan Hedberg854f4722014-07-01 18:40:20 +03001135bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level)
1136{
1137 if (sec_level == BT_SECURITY_LOW)
1138 return true;
1139
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001140 /* If we're encrypted with an STK always claim insufficient
1141 * security. This way we allow the connection to be re-encrypted
1142 * with an LTK, even if the LTK provides the same level of
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001143 * security. Only exception is if we don't have an LTK (e.g.
1144 * because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001145 */
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001146 if (test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
1147 hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001148 hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001149 return false;
1150
Johan Hedberg854f4722014-07-01 18:40:20 +03001151 if (hcon->sec_level >= sec_level)
1152 return true;
1153
1154 return false;
1155}
1156
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001157static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001158{
1159 struct smp_cmd_security_req *rp = (void *) skb->data;
1160 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001161 struct hci_conn *hcon = conn->hcon;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001162 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07001163 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001164
1165 BT_DBG("conn %p", conn);
1166
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001167 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001168 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001169
Johan Hedberg40bef302014-07-16 11:42:27 +03001170 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001171 return SMP_CMD_NOTSUPP;
1172
Johan Hedbergc05b9332014-09-10 17:37:42 -07001173 auth = rp->auth_req & AUTH_REQ_MASK;
1174
Johan Hedberg5be5e272014-09-10 17:58:54 -07001175 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001176 sec_level = BT_SECURITY_MEDIUM;
1177 else
1178 sec_level = authreq_to_seclevel(auth);
1179
Johan Hedberg854f4722014-07-01 18:40:20 +03001180 if (smp_sufficient_security(hcon, sec_level))
1181 return 0;
1182
Johan Hedbergc7262e72014-06-17 13:07:37 +03001183 if (sec_level > hcon->pending_sec_level)
1184 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001185
Johan Hedberg4dab7862012-06-07 14:58:37 +08001186 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001187 return 0;
1188
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001189 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001190 if (!smp)
1191 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001192
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001193 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001194 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03001195 return SMP_PAIRING_NOTSUPP;
1196
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001197 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001198
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001199 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07001200 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001201
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001202 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1203 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001204
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001205 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001206 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001207
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001208 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001209}
1210
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001211int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001212{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001213 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001214 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001215 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001216 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001217 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001218
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001219 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1220
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001221 /* This may be NULL if there's an unexpected disconnection */
1222 if (!conn)
1223 return 1;
1224
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001225 chan = conn->smp;
1226
Johan Hedberg757aee02013-04-24 13:05:32 +03001227 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001228 return 1;
1229
Johan Hedbergad32a2f2013-05-14 18:05:12 +03001230 if (smp_sufficient_security(hcon, sec_level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001231 return 1;
1232
Johan Hedbergc7262e72014-06-17 13:07:37 +03001233 if (sec_level > hcon->pending_sec_level)
1234 hcon->pending_sec_level = sec_level;
1235
Johan Hedberg40bef302014-07-16 11:42:27 +03001236 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001237 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1238 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001239
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001240 l2cap_chan_lock(chan);
1241
1242 /* If SMP is already in progress ignore this request */
1243 if (chan->data) {
1244 ret = 0;
1245 goto unlock;
1246 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001247
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001248 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001249 if (!smp) {
1250 ret = 1;
1251 goto unlock;
1252 }
Brian Gix2b64d152011-12-21 16:12:12 -08001253
1254 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001255
Johan Hedberg79897d22014-06-01 09:45:24 +03001256 /* Require MITM if IO Capability allows or the security level
1257 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001258 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001259 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001260 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001261 authreq |= SMP_AUTH_MITM;
1262
Johan Hedberg40bef302014-07-16 11:42:27 +03001263 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001264 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001265
Brian Gix2b64d152011-12-21 16:12:12 -08001266 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001267 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1268 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001269
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001270 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001271 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001272 } else {
1273 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001274 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001275 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001276 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001277 }
1278
Johan Hedberg4a74d652014-05-20 09:45:50 +03001279 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001280 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001281
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001282unlock:
1283 l2cap_chan_unlock(chan);
1284 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001285}
1286
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001287static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1288{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001289 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001290 struct l2cap_chan *chan = conn->smp;
1291 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001292
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001293 BT_DBG("conn %p", conn);
1294
1295 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001296 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001297
Johan Hedbergb28b4942014-09-05 22:19:55 +03001298 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001299
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001300 skb_pull(skb, sizeof(*rp));
1301
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001302 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001303
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001304 return 0;
1305}
1306
1307static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1308{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001309 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001310 struct l2cap_chan *chan = conn->smp;
1311 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001312 struct hci_dev *hdev = conn->hcon->hdev;
1313 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001314 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001315 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001316
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001317 BT_DBG("conn %p", conn);
1318
1319 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001320 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001321
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001322 /* Mark the information as received */
1323 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1324
Johan Hedbergb28b4942014-09-05 22:19:55 +03001325 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1326 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07001327 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1328 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001329
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001330 skb_pull(skb, sizeof(*rp));
1331
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001332 hci_dev_lock(hdev);
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001333 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001334 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001335 authenticated, smp->tk, smp->enc_key_size,
1336 rp->ediv, rp->rand);
1337 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001338 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001339 smp_distribute_keys(smp);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001340 hci_dev_unlock(hdev);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001341
1342 return 0;
1343}
1344
Johan Hedbergfd349c02014-02-18 10:19:36 +02001345static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1346{
1347 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001348 struct l2cap_chan *chan = conn->smp;
1349 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001350
1351 BT_DBG("");
1352
1353 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001354 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001355
Johan Hedbergb28b4942014-09-05 22:19:55 +03001356 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001357
Johan Hedbergfd349c02014-02-18 10:19:36 +02001358 skb_pull(skb, sizeof(*info));
1359
1360 memcpy(smp->irk, info->irk, 16);
1361
1362 return 0;
1363}
1364
1365static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1366 struct sk_buff *skb)
1367{
1368 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001369 struct l2cap_chan *chan = conn->smp;
1370 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001371 struct hci_conn *hcon = conn->hcon;
1372 bdaddr_t rpa;
1373
1374 BT_DBG("");
1375
1376 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001377 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001378
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001379 /* Mark the information as received */
1380 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1381
Johan Hedbergb28b4942014-09-05 22:19:55 +03001382 if (smp->remote_key_dist & SMP_DIST_SIGN)
1383 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1384
Johan Hedbergfd349c02014-02-18 10:19:36 +02001385 skb_pull(skb, sizeof(*info));
1386
Johan Hedberg31dd6242014-06-27 14:23:02 +03001387 hci_dev_lock(hcon->hdev);
1388
Johan Hedberga9a58f82014-02-25 22:24:37 +02001389 /* Strictly speaking the Core Specification (4.1) allows sending
1390 * an empty address which would force us to rely on just the IRK
1391 * as "identity information". However, since such
1392 * implementations are not known of and in order to not over
1393 * complicate our implementation, simply pretend that we never
1394 * received an IRK for such a device.
1395 */
1396 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1397 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001398 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001399 }
1400
Johan Hedbergfd349c02014-02-18 10:19:36 +02001401 bacpy(&smp->id_addr, &info->bdaddr);
1402 smp->id_addr_type = info->addr_type;
1403
1404 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1405 bacpy(&rpa, &hcon->dst);
1406 else
1407 bacpy(&rpa, BDADDR_ANY);
1408
Johan Hedberg23d0e122014-02-19 14:57:46 +02001409 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1410 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001411
Johan Hedberg31dd6242014-06-27 14:23:02 +03001412distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001413 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1414 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001415
Johan Hedberg31dd6242014-06-27 14:23:02 +03001416 hci_dev_unlock(hcon->hdev);
1417
Johan Hedbergfd349c02014-02-18 10:19:36 +02001418 return 0;
1419}
1420
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001421static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1422{
1423 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001424 struct l2cap_chan *chan = conn->smp;
1425 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001426 struct hci_dev *hdev = conn->hcon->hdev;
1427 struct smp_csrk *csrk;
1428
1429 BT_DBG("conn %p", conn);
1430
1431 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001432 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001433
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001434 /* Mark the information as received */
1435 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1436
1437 skb_pull(skb, sizeof(*rp));
1438
1439 hci_dev_lock(hdev);
1440 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1441 if (csrk) {
1442 csrk->master = 0x01;
1443 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1444 }
1445 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001446 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001447 hci_dev_unlock(hdev);
1448
1449 return 0;
1450}
1451
Johan Hedberg4befb862014-08-11 22:06:38 +03001452static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001453{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001454 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001455 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001456 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001457 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001458 int err = 0;
1459
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001460 if (hcon->type != LE_LINK) {
1461 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001462 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001463 }
1464
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001465 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001466 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001467
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001468 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001469 reason = SMP_PAIRING_NOTSUPP;
1470 goto done;
1471 }
1472
Marcel Holtmann92381f52013-10-03 01:23:08 -07001473 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001474 skb_pull(skb, sizeof(code));
1475
Johan Hedbergb28b4942014-09-05 22:19:55 +03001476 smp = chan->data;
1477
1478 if (code > SMP_CMD_MAX)
1479 goto drop;
1480
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07001481 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03001482 goto drop;
1483
1484 /* If we don't have a context the only allowed commands are
1485 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001486 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001487 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1488 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001489
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001490 switch (code) {
1491 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001492 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001493 break;
1494
1495 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001496 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001497 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001498 break;
1499
1500 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001501 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001502 break;
1503
1504 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001505 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001506 break;
1507
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001508 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001509 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001510 break;
1511
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001512 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001513 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001514 break;
1515
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001516 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001517 reason = smp_cmd_encrypt_info(conn, skb);
1518 break;
1519
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001520 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001521 reason = smp_cmd_master_ident(conn, skb);
1522 break;
1523
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001524 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001525 reason = smp_cmd_ident_info(conn, skb);
1526 break;
1527
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001528 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001529 reason = smp_cmd_ident_addr_info(conn, skb);
1530 break;
1531
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001532 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001533 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001534 break;
1535
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001536 default:
1537 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001538 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001539 goto done;
1540 }
1541
1542done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001543 if (!err) {
1544 if (reason)
1545 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001546 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001547 }
1548
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001549 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001550
1551drop:
1552 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
1553 code, &hcon->dst);
1554 kfree_skb(skb);
1555 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001556}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001557
Johan Hedberg70db83c2014-08-08 09:37:16 +03001558static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1559{
1560 struct l2cap_conn *conn = chan->conn;
1561
1562 BT_DBG("chan %p", chan);
1563
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001564 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001565 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001566
Johan Hedberg70db83c2014-08-08 09:37:16 +03001567 conn->smp = NULL;
1568 l2cap_chan_put(chan);
1569}
1570
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001571static void smp_resume_cb(struct l2cap_chan *chan)
1572{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001573 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001574 struct l2cap_conn *conn = chan->conn;
1575 struct hci_conn *hcon = conn->hcon;
1576
1577 BT_DBG("chan %p", chan);
1578
Johan Hedberg86d14072014-08-11 22:06:43 +03001579 if (!smp)
1580 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001581
Johan Hedberg84bc0db2014-09-05 22:19:49 +03001582 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1583 return;
1584
Johan Hedberg86d14072014-08-11 22:06:43 +03001585 cancel_delayed_work(&smp->security_timer);
1586
Johan Hedbergd6268e82014-09-05 22:19:51 +03001587 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001588}
1589
Johan Hedberg70db83c2014-08-08 09:37:16 +03001590static void smp_ready_cb(struct l2cap_chan *chan)
1591{
1592 struct l2cap_conn *conn = chan->conn;
1593
1594 BT_DBG("chan %p", chan);
1595
1596 conn->smp = chan;
1597 l2cap_chan_hold(chan);
1598}
1599
Johan Hedberg4befb862014-08-11 22:06:38 +03001600static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1601{
1602 int err;
1603
1604 BT_DBG("chan %p", chan);
1605
1606 err = smp_sig_channel(chan, skb);
1607 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001608 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001609
Johan Hedbergb68fda62014-08-11 22:06:40 +03001610 if (smp)
1611 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001612
Johan Hedberg1e91c292014-08-18 20:33:29 +03001613 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03001614 }
1615
1616 return err;
1617}
1618
Johan Hedberg70db83c2014-08-08 09:37:16 +03001619static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1620 unsigned long hdr_len,
1621 unsigned long len, int nb)
1622{
1623 struct sk_buff *skb;
1624
1625 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1626 if (!skb)
1627 return ERR_PTR(-ENOMEM);
1628
1629 skb->priority = HCI_PRIO_MAX;
1630 bt_cb(skb)->chan = chan;
1631
1632 return skb;
1633}
1634
1635static const struct l2cap_ops smp_chan_ops = {
1636 .name = "Security Manager",
1637 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001638 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001639 .alloc_skb = smp_alloc_skb_cb,
1640 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001641 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001642
1643 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001644 .state_change = l2cap_chan_no_state_change,
1645 .close = l2cap_chan_no_close,
1646 .defer = l2cap_chan_no_defer,
1647 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001648 .set_shutdown = l2cap_chan_no_set_shutdown,
1649 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1650 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1651};
1652
1653static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1654{
1655 struct l2cap_chan *chan;
1656
1657 BT_DBG("pchan %p", pchan);
1658
1659 chan = l2cap_chan_create();
1660 if (!chan)
1661 return NULL;
1662
1663 chan->chan_type = pchan->chan_type;
1664 chan->ops = &smp_chan_ops;
1665 chan->scid = pchan->scid;
1666 chan->dcid = chan->scid;
1667 chan->imtu = pchan->imtu;
1668 chan->omtu = pchan->omtu;
1669 chan->mode = pchan->mode;
1670
1671 BT_DBG("created chan %p", chan);
1672
1673 return chan;
1674}
1675
1676static const struct l2cap_ops smp_root_chan_ops = {
1677 .name = "Security Manager Root",
1678 .new_connection = smp_new_conn_cb,
1679
1680 /* None of these are implemented for the root channel */
1681 .close = l2cap_chan_no_close,
1682 .alloc_skb = l2cap_chan_no_alloc_skb,
1683 .recv = l2cap_chan_no_recv,
1684 .state_change = l2cap_chan_no_state_change,
1685 .teardown = l2cap_chan_no_teardown,
1686 .ready = l2cap_chan_no_ready,
1687 .defer = l2cap_chan_no_defer,
1688 .suspend = l2cap_chan_no_suspend,
1689 .resume = l2cap_chan_no_resume,
1690 .set_shutdown = l2cap_chan_no_set_shutdown,
1691 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1692 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1693};
1694
Johan Hedberg711eafe2014-08-08 09:32:52 +03001695int smp_register(struct hci_dev *hdev)
1696{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001697 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001698 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001699
Johan Hedberg711eafe2014-08-08 09:32:52 +03001700 BT_DBG("%s", hdev->name);
1701
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001702 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1703 if (IS_ERR(tfm_aes)) {
1704 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001705 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03001706 return err;
1707 }
1708
Johan Hedberg70db83c2014-08-08 09:37:16 +03001709 chan = l2cap_chan_create();
1710 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001711 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001712 return -ENOMEM;
1713 }
1714
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001715 chan->data = tfm_aes;
1716
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001717 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001718
1719 l2cap_chan_set_defaults(chan);
1720
1721 bacpy(&chan->src, &hdev->bdaddr);
1722 chan->src_type = BDADDR_LE_PUBLIC;
1723 chan->state = BT_LISTEN;
1724 chan->mode = L2CAP_MODE_BASIC;
1725 chan->imtu = L2CAP_DEFAULT_MTU;
1726 chan->ops = &smp_root_chan_ops;
1727
1728 hdev->smp_data = chan;
1729
Johan Hedberg711eafe2014-08-08 09:32:52 +03001730 return 0;
1731}
1732
1733void smp_unregister(struct hci_dev *hdev)
1734{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001735 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001736 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001737
1738 if (!chan)
1739 return;
1740
1741 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001742
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001743 tfm_aes = chan->data;
1744 if (tfm_aes) {
1745 chan->data = NULL;
1746 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001747 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03001748
1749 hdev->smp_data = NULL;
1750 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001751}