blob: 4fed367da380789424bbf7c5bef1f8571242e376 [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,
Johan Hedberg65668772014-05-16 11:03:34 +030048 SMP_FLAG_SC,
Johan Hedberg533e35d2014-06-16 19:25:18 +030049};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030050
51struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030052 struct l2cap_conn *conn;
53 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030054 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030055
Johan Hedberg4bc58f52014-05-20 09:45:47 +030056 u8 preq[7]; /* SMP Pairing Request */
57 u8 prsp[7]; /* SMP Pairing Response */
58 u8 prnd[16]; /* SMP Pairing Random (local) */
59 u8 rrnd[16]; /* SMP Pairing Random (remote) */
60 u8 pcnf[16]; /* SMP Pairing Confirm */
61 u8 tk[16]; /* SMP Temporary Key */
62 u8 enc_key_size;
63 u8 remote_key_dist;
64 bdaddr_t id_addr;
65 u8 id_addr_type;
66 u8 irk[16];
67 struct smp_csrk *csrk;
68 struct smp_csrk *slave_csrk;
69 struct smp_ltk *ltk;
70 struct smp_ltk *slave_ltk;
71 struct smp_irk *remote_irk;
Johan Hedberg4a74d652014-05-20 09:45:50 +030072 unsigned long flags;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030073
74 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +030075 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030076};
77
Johan Hedberg8a2936f2014-06-16 19:25:19 +030078static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030079{
Johan Hedberg8a2936f2014-06-16 19:25:19 +030080 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030081
Johan Hedberg8a2936f2014-06-16 19:25:19 +030082 for (i = 0; i < len; i++)
83 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030084}
85
86static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
87{
88 struct blkcipher_desc desc;
89 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +020090 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +020091 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030092
93 if (tfm == NULL) {
94 BT_ERR("tfm %p", tfm);
95 return -EINVAL;
96 }
97
98 desc.tfm = tfm;
99 desc.flags = 0;
100
Johan Hedberg943a7322014-03-18 12:58:24 +0200101 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300102 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200103
104 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300105 if (err) {
106 BT_ERR("cipher setkey failed: %d", err);
107 return err;
108 }
109
Johan Hedberg943a7322014-03-18 12:58:24 +0200110 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300111 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200112
113 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300114
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300115 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
116 if (err)
117 BT_ERR("Encrypt data error %d", err);
118
Johan Hedberg943a7322014-03-18 12:58:24 +0200119 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300120 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200121
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300122 return err;
123}
124
Johan Hedberg60478052014-02-18 10:19:31 +0200125static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
126{
Johan Hedberg943a7322014-03-18 12:58:24 +0200127 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200128 int err;
129
130 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200131 memcpy(_res, r, 3);
132 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200133
Johan Hedberg943a7322014-03-18 12:58:24 +0200134 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200135 if (err) {
136 BT_ERR("Encrypt error");
137 return err;
138 }
139
140 /* The output of the random address function ah is:
141 * ah(h, r) = e(k, r') mod 2^24
142 * The output of the security function e is then truncated to 24 bits
143 * by taking the least significant 24 bits of the output of e as the
144 * result of ah.
145 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200146 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200147
148 return 0;
149}
150
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300151bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200152{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300153 struct l2cap_chan *chan = hdev->smp_data;
154 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200155 u8 hash[3];
156 int err;
157
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300158 if (!chan || !chan->data)
159 return false;
160
161 tfm = chan->data;
162
Johan Hedberg60478052014-02-18 10:19:31 +0200163 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
164
165 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
166 if (err)
167 return false;
168
169 return !memcmp(bdaddr->b, hash, 3);
170}
171
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300172int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200173{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300174 struct l2cap_chan *chan = hdev->smp_data;
175 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200176 int err;
177
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300178 if (!chan || !chan->data)
179 return -EOPNOTSUPP;
180
181 tfm = chan->data;
182
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200183 get_random_bytes(&rpa->b[3], 3);
184
185 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
186 rpa->b[5] |= 0x40; /* Set second most significant bit */
187
188 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
189 if (err < 0)
190 return err;
191
192 BT_DBG("RPA %pMR", rpa);
193
194 return 0;
195}
196
Johan Hedberge491eaf2014-10-25 21:15:37 +0200197static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
198 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
199 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300200{
201 u8 p1[16], p2[16];
202 int err;
203
204 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 Hedberge491eaf2014-10-25 21:15:37 +0200221 err = smp_e(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 Hedberge491eaf2014-10-25 21:15:37 +0200231 err = smp_e(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 Hedberge491eaf2014-10-25 21:15:37 +0200238static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
239 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300240{
241 int err;
242
243 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200244 memcpy(_r, r2, 8);
245 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300246
Johan Hedberge491eaf2014-10-25 21:15:37 +0200247 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300248 if (err)
249 BT_ERR("Encrypt data error");
250
251 return err;
252}
253
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300254static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
255{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300256 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300257 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300258 struct kvec iv[2];
259 struct msghdr msg;
260
261 if (!chan)
262 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300263
264 BT_DBG("code 0x%2.2x", code);
265
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300266 iv[0].iov_base = &code;
267 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300268
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300269 iv[1].iov_base = data;
270 iv[1].iov_len = len;
271
272 memset(&msg, 0, sizeof(msg));
273
274 msg.msg_iov = (struct iovec *) &iv;
275 msg.msg_iovlen = 2;
276
277 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300278
Johan Hedbergb68fda62014-08-11 22:06:40 +0300279 if (!chan->data)
280 return;
281
282 smp = chan->data;
283
284 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300285 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300286}
287
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300288static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800289{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300290 if (authreq & SMP_AUTH_MITM) {
291 if (authreq & SMP_AUTH_SC)
292 return BT_SECURITY_FIPS;
293 else
294 return BT_SECURITY_HIGH;
295 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800296 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300297 }
Brian Gix2b64d152011-12-21 16:12:12 -0800298}
299
300static __u8 seclevel_to_authreq(__u8 sec_level)
301{
302 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300303 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800304 case BT_SECURITY_HIGH:
305 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
306 case BT_SECURITY_MEDIUM:
307 return SMP_AUTH_BONDING;
308 default:
309 return SMP_AUTH_NONE;
310 }
311}
312
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300313static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700314 struct smp_cmd_pairing *req,
315 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300316{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300317 struct l2cap_chan *chan = conn->smp;
318 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200319 struct hci_conn *hcon = conn->hcon;
320 struct hci_dev *hdev = hcon->hdev;
321 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300322
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300323 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700324 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
325 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300326 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800327 } else {
328 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300329 }
330
Johan Hedbergfd349c02014-02-18 10:19:36 +0200331 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
332 remote_dist |= SMP_DIST_ID_KEY;
333
Johan Hedberg863efaf2014-02-22 19:06:32 +0200334 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
335 local_dist |= SMP_DIST_ID_KEY;
336
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300337 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
338 if ((authreq & SMP_AUTH_SC) &&
339 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
340 local_dist |= SMP_DIST_LINK_KEY;
341 remote_dist |= SMP_DIST_LINK_KEY;
342 }
343 } else {
344 authreq &= ~SMP_AUTH_SC;
345 }
346
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300347 if (rsp == NULL) {
348 req->io_capability = conn->hcon->io_capability;
349 req->oob_flag = SMP_OOB_NOT_PRESENT;
350 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200351 req->init_key_dist = local_dist;
352 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300353 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200354
355 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300356 return;
357 }
358
359 rsp->io_capability = conn->hcon->io_capability;
360 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
361 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200362 rsp->init_key_dist = req->init_key_dist & remote_dist;
363 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300364 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200365
366 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300367}
368
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300369static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
370{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300371 struct l2cap_chan *chan = conn->smp;
372 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300373
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300374 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700375 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300376 return SMP_ENC_KEY_SIZE;
377
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300378 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300379
380 return 0;
381}
382
Johan Hedberg6f48e262014-08-11 22:06:44 +0300383static void smp_chan_destroy(struct l2cap_conn *conn)
384{
385 struct l2cap_chan *chan = conn->smp;
386 struct smp_chan *smp = chan->data;
387 bool complete;
388
389 BUG_ON(!smp);
390
391 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300392
Johan Hedberg6f48e262014-08-11 22:06:44 +0300393 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
394 mgmt_smp_complete(conn->hcon, complete);
395
396 kfree(smp->csrk);
397 kfree(smp->slave_csrk);
398
399 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300400 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300401
402 /* If pairing failed clean up any keys we might have */
403 if (!complete) {
404 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200405 list_del_rcu(&smp->ltk->list);
406 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300407 }
408
409 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200410 list_del_rcu(&smp->slave_ltk->list);
411 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300412 }
413
414 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200415 list_del_rcu(&smp->remote_irk->list);
416 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300417 }
418 }
419
420 chan->data = NULL;
421 kfree(smp);
422 hci_conn_drop(conn->hcon);
423}
424
Johan Hedberg84794e12013-11-06 11:24:57 +0200425static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800426{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200427 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300428 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200429
Johan Hedberg84794e12013-11-06 11:24:57 +0200430 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800431 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700432 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800433
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700434 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700435 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300436
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300437 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300438 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800439}
440
Brian Gix2b64d152011-12-21 16:12:12 -0800441#define JUST_WORKS 0x00
442#define JUST_CFM 0x01
443#define REQ_PASSKEY 0x02
444#define CFM_PASSKEY 0x03
445#define REQ_OOB 0x04
446#define OVERLAP 0xFF
447
448static const u8 gen_method[5][5] = {
449 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
450 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
451 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
452 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
453 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
454};
455
Johan Hedberg581370c2014-06-17 13:07:38 +0300456static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
457{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300458 /* If either side has unknown io_caps, use JUST_CFM (which gets
459 * converted later to JUST_WORKS if we're initiators.
460 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300461 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
462 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300463 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300464
465 return gen_method[remote_io][local_io];
466}
467
Brian Gix2b64d152011-12-21 16:12:12 -0800468static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
469 u8 local_io, u8 remote_io)
470{
471 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300472 struct l2cap_chan *chan = conn->smp;
473 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800474 u8 method;
475 u32 passkey = 0;
476 int ret = 0;
477
478 /* Initialize key for JUST WORKS */
479 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300480 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800481
482 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
483
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300484 /* If neither side wants MITM, either "just" confirm an incoming
485 * request or use just-works for outgoing ones. The JUST_CFM
486 * will be converted to JUST_WORKS if necessary later in this
487 * function. If either side has MITM look up the method from the
488 * table.
489 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300490 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300491 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800492 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300493 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800494
Johan Hedberga82505c2014-03-24 14:39:07 +0200495 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300496 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200497 method = JUST_WORKS;
498
Johan Hedberg02f3e252014-07-16 15:09:13 +0300499 /* Don't bother user space with no IO capabilities */
500 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
501 method = JUST_WORKS;
502
Brian Gix2b64d152011-12-21 16:12:12 -0800503 /* If Just Works, Continue with Zero TK */
504 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300505 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800506 return 0;
507 }
508
509 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300510 if (method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300511 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300512 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
513 hcon->pending_sec_level = BT_SECURITY_HIGH;
514 }
Brian Gix2b64d152011-12-21 16:12:12 -0800515
516 /* If both devices have Keyoard-Display I/O, the master
517 * Confirms and the slave Enters the passkey.
518 */
519 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300520 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800521 method = CFM_PASSKEY;
522 else
523 method = REQ_PASSKEY;
524 }
525
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200526 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800527 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200528 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800529 get_random_bytes(&passkey, sizeof(passkey));
530 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200531 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800532 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300533 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800534 }
535
Brian Gix2b64d152011-12-21 16:12:12 -0800536 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700537 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200538 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200539 else if (method == JUST_CFM)
540 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
541 hcon->type, hcon->dst_type,
542 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800543 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200544 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200545 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200546 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800547
Brian Gix2b64d152011-12-21 16:12:12 -0800548 return ret;
549}
550
Johan Hedberg1cc61142014-05-20 09:45:52 +0300551static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300552{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300553 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300554 struct smp_cmd_pairing_confirm cp;
555 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300556
557 BT_DBG("conn %p", conn);
558
Johan Hedberge491eaf2014-10-25 21:15:37 +0200559 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200560 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200561 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
562 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300563 if (ret)
564 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300565
Johan Hedberg4a74d652014-05-20 09:45:50 +0300566 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800567
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300568 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
569
Johan Hedbergb28b4942014-09-05 22:19:55 +0300570 if (conn->hcon->out)
571 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
572 else
573 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
574
Johan Hedberg1cc61142014-05-20 09:45:52 +0300575 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300576}
577
Johan Hedberg861580a2014-05-20 09:45:51 +0300578static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300579{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300580 struct l2cap_conn *conn = smp->conn;
581 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300582 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300583 int ret;
584
Johan Hedbergec70f362014-06-27 14:23:04 +0300585 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300586 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300587
588 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
589
Johan Hedberge491eaf2014-10-25 21:15:37 +0200590 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200591 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200592 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300593 if (ret)
594 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300595
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300596 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
597 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300598 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300599 }
600
601 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800602 u8 stk[16];
603 __le64 rand = 0;
604 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300605
Johan Hedberge491eaf2014-10-25 21:15:37 +0200606 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300607
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300608 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300609 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300610
Johan Hedberg861580a2014-05-20 09:45:51 +0300611 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
612 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300613
614 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300615 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300616 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300617 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300618 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800619 __le64 rand = 0;
620 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300621
Johan Hedberg943a7322014-03-18 12:58:24 +0200622 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
623 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300624
Johan Hedberge491eaf2014-10-25 21:15:37 +0200625 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300626
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300627 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700628 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300629
Johan Hedbergfff34902014-06-10 15:19:50 +0300630 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
631 auth = 1;
632 else
633 auth = 0;
634
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300635 /* Even though there's no _SLAVE suffix this is the
636 * slave STK we're adding for later lookup (the master
637 * STK never needs to be stored).
638 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700639 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300640 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300641 }
642
Johan Hedberg861580a2014-05-20 09:45:51 +0300643 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300644}
645
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300646static void smp_notify_keys(struct l2cap_conn *conn)
647{
648 struct l2cap_chan *chan = conn->smp;
649 struct smp_chan *smp = chan->data;
650 struct hci_conn *hcon = conn->hcon;
651 struct hci_dev *hdev = hcon->hdev;
652 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
653 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
654 bool persistent;
655
656 if (smp->remote_irk) {
657 mgmt_new_irk(hdev, smp->remote_irk);
658 /* Now that user space can be considered to know the
659 * identity address track the connection based on it
660 * from now on.
661 */
662 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
663 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300664 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300665
666 /* When receiving an indentity resolving key for
667 * a remote device that does not use a resolvable
668 * private address, just remove the key so that
669 * it is possible to use the controller white
670 * list for scanning.
671 *
672 * Userspace will have been told to not store
673 * this key at this point. So it is safe to
674 * just remove it.
675 */
676 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200677 list_del_rcu(&smp->remote_irk->list);
678 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300679 smp->remote_irk = NULL;
680 }
681 }
682
683 /* The LTKs and CSRKs should be persistent only if both sides
684 * had the bonding bit set in their authentication requests.
685 */
686 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
687
688 if (smp->csrk) {
689 smp->csrk->bdaddr_type = hcon->dst_type;
690 bacpy(&smp->csrk->bdaddr, &hcon->dst);
691 mgmt_new_csrk(hdev, smp->csrk, persistent);
692 }
693
694 if (smp->slave_csrk) {
695 smp->slave_csrk->bdaddr_type = hcon->dst_type;
696 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
697 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
698 }
699
700 if (smp->ltk) {
701 smp->ltk->bdaddr_type = hcon->dst_type;
702 bacpy(&smp->ltk->bdaddr, &hcon->dst);
703 mgmt_new_ltk(hdev, smp->ltk, persistent);
704 }
705
706 if (smp->slave_ltk) {
707 smp->slave_ltk->bdaddr_type = hcon->dst_type;
708 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
709 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
710 }
711}
712
Johan Hedbergb28b4942014-09-05 22:19:55 +0300713static void smp_allow_key_dist(struct smp_chan *smp)
714{
715 /* Allow the first expected phase 3 PDU. The rest of the PDUs
716 * will be allowed in each PDU handler to ensure we receive
717 * them in the correct order.
718 */
719 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
720 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
721 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
722 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
723 else if (smp->remote_key_dist & SMP_DIST_SIGN)
724 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
725}
726
Johan Hedbergd6268e82014-09-05 22:19:51 +0300727static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300728{
729 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300730 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300731 struct hci_conn *hcon = conn->hcon;
732 struct hci_dev *hdev = hcon->hdev;
733 __u8 *keydist;
734
735 BT_DBG("conn %p", conn);
736
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300737 rsp = (void *) &smp->prsp[1];
738
739 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300740 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
741 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300742 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300743 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300744
745 req = (void *) &smp->preq[1];
746
747 if (hcon->out) {
748 keydist = &rsp->init_key_dist;
749 *keydist &= req->init_key_dist;
750 } else {
751 keydist = &rsp->resp_key_dist;
752 *keydist &= req->resp_key_dist;
753 }
754
755 BT_DBG("keydist 0x%x", *keydist);
756
757 if (*keydist & SMP_DIST_ENC_KEY) {
758 struct smp_cmd_encrypt_info enc;
759 struct smp_cmd_master_ident ident;
760 struct smp_ltk *ltk;
761 u8 authenticated;
762 __le16 ediv;
763 __le64 rand;
764
765 get_random_bytes(enc.ltk, sizeof(enc.ltk));
766 get_random_bytes(&ediv, sizeof(ediv));
767 get_random_bytes(&rand, sizeof(rand));
768
769 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
770
771 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
772 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
773 SMP_LTK_SLAVE, authenticated, enc.ltk,
774 smp->enc_key_size, ediv, rand);
775 smp->slave_ltk = ltk;
776
777 ident.ediv = ediv;
778 ident.rand = rand;
779
780 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
781
782 *keydist &= ~SMP_DIST_ENC_KEY;
783 }
784
785 if (*keydist & SMP_DIST_ID_KEY) {
786 struct smp_cmd_ident_addr_info addrinfo;
787 struct smp_cmd_ident_info idinfo;
788
789 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
790
791 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
792
793 /* The hci_conn contains the local identity address
794 * after the connection has been established.
795 *
796 * This is true even when the connection has been
797 * established using a resolvable random address.
798 */
799 bacpy(&addrinfo.bdaddr, &hcon->src);
800 addrinfo.addr_type = hcon->src_type;
801
802 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
803 &addrinfo);
804
805 *keydist &= ~SMP_DIST_ID_KEY;
806 }
807
808 if (*keydist & SMP_DIST_SIGN) {
809 struct smp_cmd_sign_info sign;
810 struct smp_csrk *csrk;
811
812 /* Generate a new random key */
813 get_random_bytes(sign.csrk, sizeof(sign.csrk));
814
815 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
816 if (csrk) {
817 csrk->master = 0x00;
818 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
819 }
820 smp->slave_csrk = csrk;
821
822 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
823
824 *keydist &= ~SMP_DIST_SIGN;
825 }
826
827 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300828 if (smp->remote_key_dist & KEY_DIST_MASK) {
829 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300830 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300831 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300832
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300833 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
834 smp_notify_keys(conn);
835
836 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300837}
838
Johan Hedbergb68fda62014-08-11 22:06:40 +0300839static void smp_timeout(struct work_struct *work)
840{
841 struct smp_chan *smp = container_of(work, struct smp_chan,
842 security_timer.work);
843 struct l2cap_conn *conn = smp->conn;
844
845 BT_DBG("conn %p", conn);
846
Johan Hedberg1e91c292014-08-18 20:33:29 +0300847 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300848}
849
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300850static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
851{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300852 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300853 struct smp_chan *smp;
854
Marcel Holtmannf1560462013-10-13 05:43:25 -0700855 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300856 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300857 return NULL;
858
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300859 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
860 if (IS_ERR(smp->tfm_aes)) {
861 BT_ERR("Unable to create ECB crypto context");
862 kfree(smp);
863 return NULL;
864 }
865
Johan Hedberg407cecf2014-05-02 14:19:47 +0300866 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
867 if (IS_ERR(smp->tfm_cmac)) {
868 BT_ERR("Unable to create CMAC crypto context");
869 crypto_free_blkcipher(smp->tfm_aes);
870 kfree(smp);
871 return NULL;
872 }
873
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300874 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300875 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300876
Johan Hedbergb28b4942014-09-05 22:19:55 +0300877 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
878
Johan Hedbergb68fda62014-08-11 22:06:40 +0300879 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
880
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300881 hci_conn_hold(conn->hcon);
882
883 return smp;
884}
885
Brian Gix2b64d152011-12-21 16:12:12 -0800886int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
887{
Johan Hedbergb10e8012014-06-27 14:23:07 +0300888 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300889 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -0800890 struct smp_chan *smp;
891 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300892 int err;
Brian Gix2b64d152011-12-21 16:12:12 -0800893
894 BT_DBG("");
895
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300896 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -0800897 return -ENOTCONN;
898
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300899 chan = conn->smp;
900 if (!chan)
901 return -ENOTCONN;
902
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300903 l2cap_chan_lock(chan);
904 if (!chan->data) {
905 err = -ENOTCONN;
906 goto unlock;
907 }
908
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300909 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800910
911 switch (mgmt_op) {
912 case MGMT_OP_USER_PASSKEY_REPLY:
913 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +0200914 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800915 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +0200916 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800917 /* Fall Through */
918 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +0300919 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800920 break;
921 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
922 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +0200923 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300924 err = 0;
925 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800926 default:
Johan Hedberg84794e12013-11-06 11:24:57 +0200927 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300928 err = -EOPNOTSUPP;
929 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800930 }
931
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300932 err = 0;
933
Brian Gix2b64d152011-12-21 16:12:12 -0800934 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +0300935 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
936 u8 rsp = smp_confirm(smp);
937 if (rsp)
938 smp_failure(conn, rsp);
939 }
Brian Gix2b64d152011-12-21 16:12:12 -0800940
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300941unlock:
942 l2cap_chan_unlock(chan);
943 return err;
Brian Gix2b64d152011-12-21 16:12:12 -0800944}
945
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300946static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300947{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300948 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300949 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +0300950 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300951 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +0300952 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300953 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300954
955 BT_DBG("conn %p", conn);
956
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200957 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300958 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200959
Johan Hedberg40bef302014-07-16 11:42:27 +0300960 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -0800961 return SMP_CMD_NOTSUPP;
962
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300963 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300964 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300965 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300966 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300967
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +0300968 if (!smp)
969 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300970
Johan Hedbergc05b9332014-09-10 17:37:42 -0700971 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300972 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -0700973
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300974 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -0700975 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +0300976 return SMP_PAIRING_NOTSUPP;
977
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300978 smp->preq[0] = SMP_CMD_PAIRING_REQ;
979 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300980 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300981
Johan Hedberg5be5e272014-09-10 17:58:54 -0700982 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -0700983 sec_level = BT_SECURITY_MEDIUM;
984 else
985 sec_level = authreq_to_seclevel(auth);
986
Johan Hedbergc7262e72014-06-17 13:07:37 +0300987 if (sec_level > conn->hcon->pending_sec_level)
988 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +0200989
Stephen Hemminger49c922b2014-10-27 21:12:20 -0700990 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300991 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
992 u8 method;
993
994 method = get_auth_method(smp, conn->hcon->io_capability,
995 req->io_capability);
996 if (method == JUST_WORKS || method == JUST_CFM)
997 return SMP_AUTH_REQUIREMENTS;
998 }
999
Brian Gix2b64d152011-12-21 16:12:12 -08001000 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001001
Johan Hedberg65668772014-05-16 11:03:34 +03001002 if (rsp.auth_req & SMP_AUTH_SC)
1003 set_bit(SMP_FLAG_SC, &smp->flags);
1004
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001005 key_size = min(req->max_key_size, rsp.max_key_size);
1006 if (check_enc_key_size(conn, key_size))
1007 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001008
Johan Hedberge84a6b12013-12-02 10:49:03 +02001009 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001010
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001011 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1012 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001013
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001014 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001015 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001016
Brian Gix2b64d152011-12-21 16:12:12 -08001017 /* Request setup of TK */
1018 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1019 if (ret)
1020 return SMP_UNSPECIFIED;
1021
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001022 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001023}
1024
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001025static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001026{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001027 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001028 struct l2cap_chan *chan = conn->smp;
1029 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001030 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001031 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001032 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001033
1034 BT_DBG("conn %p", conn);
1035
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001036 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001037 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001038
Johan Hedberg40bef302014-07-16 11:42:27 +03001039 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001040 return SMP_CMD_NOTSUPP;
1041
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001042 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001043
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001044 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001045
1046 key_size = min(req->max_key_size, rsp->max_key_size);
1047 if (check_enc_key_size(conn, key_size))
1048 return SMP_ENC_KEY_SIZE;
1049
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001050 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001051
Johan Hedberg65668772014-05-16 11:03:34 +03001052 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1053 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001054 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1055 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001056
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001057 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001058 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1059 u8 method;
1060
1061 method = get_auth_method(smp, req->io_capability,
1062 rsp->io_capability);
1063 if (method == JUST_WORKS || method == JUST_CFM)
1064 return SMP_AUTH_REQUIREMENTS;
1065 }
1066
Johan Hedberge84a6b12013-12-02 10:49:03 +02001067 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001068
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001069 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1070 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001071
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001072 /* Update remote key distribution in case the remote cleared
1073 * some bits that we had enabled in our request.
1074 */
1075 smp->remote_key_dist &= rsp->resp_key_dist;
1076
Johan Hedbergc05b9332014-09-10 17:37:42 -07001077 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001078
Johan Hedberg476585e2012-06-06 18:54:15 +08001079 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001080 if (ret)
1081 return SMP_UNSPECIFIED;
1082
Johan Hedberg4a74d652014-05-20 09:45:50 +03001083 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001084
1085 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001086 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001087 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001088
1089 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001090}
1091
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001092static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001093{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001094 struct l2cap_chan *chan = conn->smp;
1095 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001096
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001097 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1098
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001099 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001100 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001101
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001102 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1103 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001104
Johan Hedbergb28b4942014-09-05 22:19:55 +03001105 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001106 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1107 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001108 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1109 return 0;
1110 }
1111
1112 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001113 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001114 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001115 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001116
1117 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001118}
1119
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001120static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001121{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001122 struct l2cap_chan *chan = conn->smp;
1123 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001124
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001125 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001126
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001127 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001128 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001129
Johan Hedberg943a7322014-03-18 12:58:24 +02001130 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001131 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001132
Johan Hedberg861580a2014-05-20 09:45:51 +03001133 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001134}
1135
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001136static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001137{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001138 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001139 struct hci_conn *hcon = conn->hcon;
1140
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001141 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001142 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001143 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001144
Johan Hedberga6f78332014-09-10 17:37:45 -07001145 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001146 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001147
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001148 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001149 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001150
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001151 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1152 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001153
Johan Hedbergfe59a052014-07-01 19:14:12 +03001154 /* We never store STKs for master role, so clear this flag */
1155 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1156
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001157 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001158}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001159
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001160bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1161 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03001162{
1163 if (sec_level == BT_SECURITY_LOW)
1164 return true;
1165
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001166 /* If we're encrypted with an STK but the caller prefers using
1167 * LTK claim insufficient security. This way we allow the
1168 * connection to be re-encrypted with an LTK, even if the LTK
1169 * provides the same level of security. Only exception is if we
1170 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001171 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001172 if (key_pref == SMP_USE_LTK &&
1173 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03001174 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001175 return false;
1176
Johan Hedberg854f4722014-07-01 18:40:20 +03001177 if (hcon->sec_level >= sec_level)
1178 return true;
1179
1180 return false;
1181}
1182
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001183static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001184{
1185 struct smp_cmd_security_req *rp = (void *) skb->data;
1186 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001187 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001188 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001189 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07001190 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001191
1192 BT_DBG("conn %p", conn);
1193
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001194 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001195 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001196
Johan Hedberg40bef302014-07-16 11:42:27 +03001197 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001198 return SMP_CMD_NOTSUPP;
1199
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001200 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001201
Johan Hedberg5be5e272014-09-10 17:58:54 -07001202 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001203 sec_level = BT_SECURITY_MEDIUM;
1204 else
1205 sec_level = authreq_to_seclevel(auth);
1206
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001207 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03001208 return 0;
1209
Johan Hedbergc7262e72014-06-17 13:07:37 +03001210 if (sec_level > hcon->pending_sec_level)
1211 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001212
Johan Hedberg4dab7862012-06-07 14:58:37 +08001213 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001214 return 0;
1215
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001216 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001217 if (!smp)
1218 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001219
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001220 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001221 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03001222 return SMP_PAIRING_NOTSUPP;
1223
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001224 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001225
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001226 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07001227 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001228
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001229 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1230 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001231
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001232 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001233 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001234
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001235 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001236}
1237
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001238int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001239{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001240 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001241 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001242 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001243 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001244 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001245
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001246 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1247
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001248 /* This may be NULL if there's an unexpected disconnection */
1249 if (!conn)
1250 return 1;
1251
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001252 chan = conn->smp;
1253
Johan Hedberg757aee02013-04-24 13:05:32 +03001254 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001255 return 1;
1256
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001257 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001258 return 1;
1259
Johan Hedbergc7262e72014-06-17 13:07:37 +03001260 if (sec_level > hcon->pending_sec_level)
1261 hcon->pending_sec_level = sec_level;
1262
Johan Hedberg40bef302014-07-16 11:42:27 +03001263 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001264 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1265 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001266
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001267 l2cap_chan_lock(chan);
1268
1269 /* If SMP is already in progress ignore this request */
1270 if (chan->data) {
1271 ret = 0;
1272 goto unlock;
1273 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001274
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001275 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001276 if (!smp) {
1277 ret = 1;
1278 goto unlock;
1279 }
Brian Gix2b64d152011-12-21 16:12:12 -08001280
1281 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001282
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001283 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
1284 authreq |= SMP_AUTH_SC;
1285
Johan Hedberg79897d22014-06-01 09:45:24 +03001286 /* Require MITM if IO Capability allows or the security level
1287 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001288 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001289 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001290 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001291 authreq |= SMP_AUTH_MITM;
1292
Johan Hedberg40bef302014-07-16 11:42:27 +03001293 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001294 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001295
Brian Gix2b64d152011-12-21 16:12:12 -08001296 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001297 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1298 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001299
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001300 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001301 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001302 } else {
1303 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001304 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001305 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001306 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001307 }
1308
Johan Hedberg4a74d652014-05-20 09:45:50 +03001309 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001310 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001311
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001312unlock:
1313 l2cap_chan_unlock(chan);
1314 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001315}
1316
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001317static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1318{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001319 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001320 struct l2cap_chan *chan = conn->smp;
1321 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001322
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001323 BT_DBG("conn %p", conn);
1324
1325 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001326 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001327
Johan Hedbergb28b4942014-09-05 22:19:55 +03001328 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001329
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001330 skb_pull(skb, sizeof(*rp));
1331
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001332 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001333
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001334 return 0;
1335}
1336
1337static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1338{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001339 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001340 struct l2cap_chan *chan = conn->smp;
1341 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001342 struct hci_dev *hdev = conn->hcon->hdev;
1343 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001344 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001345 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001346
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001347 BT_DBG("conn %p", conn);
1348
1349 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001350 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001351
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001352 /* Mark the information as received */
1353 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1354
Johan Hedbergb28b4942014-09-05 22:19:55 +03001355 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1356 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07001357 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1358 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001359
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001360 skb_pull(skb, sizeof(*rp));
1361
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001362 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001363 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001364 authenticated, smp->tk, smp->enc_key_size,
1365 rp->ediv, rp->rand);
1366 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001367 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001368 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001369
1370 return 0;
1371}
1372
Johan Hedbergfd349c02014-02-18 10:19:36 +02001373static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1374{
1375 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001376 struct l2cap_chan *chan = conn->smp;
1377 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001378
1379 BT_DBG("");
1380
1381 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001382 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001383
Johan Hedbergb28b4942014-09-05 22:19:55 +03001384 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001385
Johan Hedbergfd349c02014-02-18 10:19:36 +02001386 skb_pull(skb, sizeof(*info));
1387
1388 memcpy(smp->irk, info->irk, 16);
1389
1390 return 0;
1391}
1392
1393static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1394 struct sk_buff *skb)
1395{
1396 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001397 struct l2cap_chan *chan = conn->smp;
1398 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001399 struct hci_conn *hcon = conn->hcon;
1400 bdaddr_t rpa;
1401
1402 BT_DBG("");
1403
1404 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001405 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001406
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001407 /* Mark the information as received */
1408 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1409
Johan Hedbergb28b4942014-09-05 22:19:55 +03001410 if (smp->remote_key_dist & SMP_DIST_SIGN)
1411 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1412
Johan Hedbergfd349c02014-02-18 10:19:36 +02001413 skb_pull(skb, sizeof(*info));
1414
Johan Hedberga9a58f82014-02-25 22:24:37 +02001415 /* Strictly speaking the Core Specification (4.1) allows sending
1416 * an empty address which would force us to rely on just the IRK
1417 * as "identity information". However, since such
1418 * implementations are not known of and in order to not over
1419 * complicate our implementation, simply pretend that we never
1420 * received an IRK for such a device.
1421 */
1422 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1423 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001424 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001425 }
1426
Johan Hedbergfd349c02014-02-18 10:19:36 +02001427 bacpy(&smp->id_addr, &info->bdaddr);
1428 smp->id_addr_type = info->addr_type;
1429
1430 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1431 bacpy(&rpa, &hcon->dst);
1432 else
1433 bacpy(&rpa, BDADDR_ANY);
1434
Johan Hedberg23d0e122014-02-19 14:57:46 +02001435 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1436 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001437
Johan Hedberg31dd6242014-06-27 14:23:02 +03001438distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001439 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1440 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001441
1442 return 0;
1443}
1444
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001445static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1446{
1447 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001448 struct l2cap_chan *chan = conn->smp;
1449 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001450 struct smp_csrk *csrk;
1451
1452 BT_DBG("conn %p", conn);
1453
1454 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001455 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001456
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001457 /* Mark the information as received */
1458 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1459
1460 skb_pull(skb, sizeof(*rp));
1461
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001462 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1463 if (csrk) {
1464 csrk->master = 0x01;
1465 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1466 }
1467 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001468 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001469
1470 return 0;
1471}
1472
Johan Hedberg4befb862014-08-11 22:06:38 +03001473static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001474{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001475 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001476 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001477 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001478 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001479 int err = 0;
1480
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001481 if (hcon->type != LE_LINK) {
1482 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001483 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001484 }
1485
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001486 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001487 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001488
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001489 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001490 reason = SMP_PAIRING_NOTSUPP;
1491 goto done;
1492 }
1493
Marcel Holtmann92381f52013-10-03 01:23:08 -07001494 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001495 skb_pull(skb, sizeof(code));
1496
Johan Hedbergb28b4942014-09-05 22:19:55 +03001497 smp = chan->data;
1498
1499 if (code > SMP_CMD_MAX)
1500 goto drop;
1501
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07001502 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03001503 goto drop;
1504
1505 /* If we don't have a context the only allowed commands are
1506 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001507 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001508 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1509 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001510
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001511 switch (code) {
1512 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001513 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001514 break;
1515
1516 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001517 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001518 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001519 break;
1520
1521 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001522 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001523 break;
1524
1525 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001526 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001527 break;
1528
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001529 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001530 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001531 break;
1532
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001533 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001534 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001535 break;
1536
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001537 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001538 reason = smp_cmd_encrypt_info(conn, skb);
1539 break;
1540
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001541 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001542 reason = smp_cmd_master_ident(conn, skb);
1543 break;
1544
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001545 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001546 reason = smp_cmd_ident_info(conn, skb);
1547 break;
1548
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001549 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001550 reason = smp_cmd_ident_addr_info(conn, skb);
1551 break;
1552
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001553 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001554 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001555 break;
1556
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001557 default:
1558 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001559 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001560 goto done;
1561 }
1562
1563done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001564 if (!err) {
1565 if (reason)
1566 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001567 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001568 }
1569
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001570 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001571
1572drop:
1573 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
1574 code, &hcon->dst);
1575 kfree_skb(skb);
1576 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001577}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001578
Johan Hedberg70db83c2014-08-08 09:37:16 +03001579static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1580{
1581 struct l2cap_conn *conn = chan->conn;
1582
1583 BT_DBG("chan %p", chan);
1584
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001585 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001586 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001587
Johan Hedberg70db83c2014-08-08 09:37:16 +03001588 conn->smp = NULL;
1589 l2cap_chan_put(chan);
1590}
1591
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001592static void smp_resume_cb(struct l2cap_chan *chan)
1593{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001594 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001595 struct l2cap_conn *conn = chan->conn;
1596 struct hci_conn *hcon = conn->hcon;
1597
1598 BT_DBG("chan %p", chan);
1599
Johan Hedberg86d14072014-08-11 22:06:43 +03001600 if (!smp)
1601 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001602
Johan Hedberg84bc0db2014-09-05 22:19:49 +03001603 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1604 return;
1605
Johan Hedberg86d14072014-08-11 22:06:43 +03001606 cancel_delayed_work(&smp->security_timer);
1607
Johan Hedbergd6268e82014-09-05 22:19:51 +03001608 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001609}
1610
Johan Hedberg70db83c2014-08-08 09:37:16 +03001611static void smp_ready_cb(struct l2cap_chan *chan)
1612{
1613 struct l2cap_conn *conn = chan->conn;
1614
1615 BT_DBG("chan %p", chan);
1616
1617 conn->smp = chan;
1618 l2cap_chan_hold(chan);
1619}
1620
Johan Hedberg4befb862014-08-11 22:06:38 +03001621static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1622{
1623 int err;
1624
1625 BT_DBG("chan %p", chan);
1626
1627 err = smp_sig_channel(chan, skb);
1628 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001629 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001630
Johan Hedbergb68fda62014-08-11 22:06:40 +03001631 if (smp)
1632 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001633
Johan Hedberg1e91c292014-08-18 20:33:29 +03001634 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03001635 }
1636
1637 return err;
1638}
1639
Johan Hedberg70db83c2014-08-08 09:37:16 +03001640static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1641 unsigned long hdr_len,
1642 unsigned long len, int nb)
1643{
1644 struct sk_buff *skb;
1645
1646 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1647 if (!skb)
1648 return ERR_PTR(-ENOMEM);
1649
1650 skb->priority = HCI_PRIO_MAX;
1651 bt_cb(skb)->chan = chan;
1652
1653 return skb;
1654}
1655
1656static const struct l2cap_ops smp_chan_ops = {
1657 .name = "Security Manager",
1658 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001659 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001660 .alloc_skb = smp_alloc_skb_cb,
1661 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001662 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001663
1664 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001665 .state_change = l2cap_chan_no_state_change,
1666 .close = l2cap_chan_no_close,
1667 .defer = l2cap_chan_no_defer,
1668 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001669 .set_shutdown = l2cap_chan_no_set_shutdown,
1670 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1671 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1672};
1673
1674static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1675{
1676 struct l2cap_chan *chan;
1677
1678 BT_DBG("pchan %p", pchan);
1679
1680 chan = l2cap_chan_create();
1681 if (!chan)
1682 return NULL;
1683
1684 chan->chan_type = pchan->chan_type;
1685 chan->ops = &smp_chan_ops;
1686 chan->scid = pchan->scid;
1687 chan->dcid = chan->scid;
1688 chan->imtu = pchan->imtu;
1689 chan->omtu = pchan->omtu;
1690 chan->mode = pchan->mode;
1691
Johan Hedbergabe84902014-11-12 22:22:21 +02001692 /* Other L2CAP channels may request SMP routines in order to
1693 * change the security level. This means that the SMP channel
1694 * lock must be considered in its own category to avoid lockdep
1695 * warnings.
1696 */
1697 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
1698
Johan Hedberg70db83c2014-08-08 09:37:16 +03001699 BT_DBG("created chan %p", chan);
1700
1701 return chan;
1702}
1703
1704static const struct l2cap_ops smp_root_chan_ops = {
1705 .name = "Security Manager Root",
1706 .new_connection = smp_new_conn_cb,
1707
1708 /* None of these are implemented for the root channel */
1709 .close = l2cap_chan_no_close,
1710 .alloc_skb = l2cap_chan_no_alloc_skb,
1711 .recv = l2cap_chan_no_recv,
1712 .state_change = l2cap_chan_no_state_change,
1713 .teardown = l2cap_chan_no_teardown,
1714 .ready = l2cap_chan_no_ready,
1715 .defer = l2cap_chan_no_defer,
1716 .suspend = l2cap_chan_no_suspend,
1717 .resume = l2cap_chan_no_resume,
1718 .set_shutdown = l2cap_chan_no_set_shutdown,
1719 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1720 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1721};
1722
Johan Hedberg711eafe2014-08-08 09:32:52 +03001723int smp_register(struct hci_dev *hdev)
1724{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001725 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001726 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001727
Johan Hedberg711eafe2014-08-08 09:32:52 +03001728 BT_DBG("%s", hdev->name);
1729
Johan Hedbergadae20c2014-11-13 14:37:48 +02001730 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001731 if (IS_ERR(tfm_aes)) {
1732 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001733 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03001734 return err;
1735 }
1736
Johan Hedberg70db83c2014-08-08 09:37:16 +03001737 chan = l2cap_chan_create();
1738 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001739 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001740 return -ENOMEM;
1741 }
1742
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001743 chan->data = tfm_aes;
1744
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001745 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001746
1747 l2cap_chan_set_defaults(chan);
1748
1749 bacpy(&chan->src, &hdev->bdaddr);
1750 chan->src_type = BDADDR_LE_PUBLIC;
1751 chan->state = BT_LISTEN;
1752 chan->mode = L2CAP_MODE_BASIC;
1753 chan->imtu = L2CAP_DEFAULT_MTU;
1754 chan->ops = &smp_root_chan_ops;
1755
Johan Hedbergabe84902014-11-12 22:22:21 +02001756 /* Set correct nesting level for a parent/listening channel */
1757 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
1758
Johan Hedberg70db83c2014-08-08 09:37:16 +03001759 hdev->smp_data = chan;
1760
Johan Hedberg711eafe2014-08-08 09:32:52 +03001761 return 0;
1762}
1763
1764void smp_unregister(struct hci_dev *hdev)
1765{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001766 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001767 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001768
1769 if (!chan)
1770 return;
1771
1772 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001773
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001774 tfm_aes = chan->data;
1775 if (tfm_aes) {
1776 chan->data = NULL;
1777 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001778 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03001779
1780 hdev->smp_data = NULL;
1781 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001782}