blob: 0b4403f3dce1f7a3585965492a5882b64698f689 [file] [log] [blame]
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
8
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
21*/
22
Gustavo Padovan8c520a52012-05-23 04:04:22 -030023#include <linux/crypto.h>
24#include <linux/scatterlist.h>
25#include <crypto/b128ops.h>
26
Anderson Brigliaeb492e02011-06-09 18:50:40 -030027#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080030#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070031
32#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030033
Marcel Holtmann17b02e62012-03-01 14:32:37 -080034#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030035
Johan Hedberg065a13e2012-10-11 16:26:06 +020036#define AUTH_REQ_MASK 0x07
37
Johan Hedberg533e35d2014-06-16 19:25:18 +030038enum {
39 SMP_FLAG_TK_VALID,
40 SMP_FLAG_CFM_PENDING,
41 SMP_FLAG_MITM_AUTH,
42 SMP_FLAG_COMPLETE,
43 SMP_FLAG_INITIATOR,
44};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030045
46struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030047 struct l2cap_conn *conn;
48 struct delayed_work security_timer;
49
Johan Hedberg4bc58f52014-05-20 09:45:47 +030050 u8 preq[7]; /* SMP Pairing Request */
51 u8 prsp[7]; /* SMP Pairing Response */
52 u8 prnd[16]; /* SMP Pairing Random (local) */
53 u8 rrnd[16]; /* SMP Pairing Random (remote) */
54 u8 pcnf[16]; /* SMP Pairing Confirm */
55 u8 tk[16]; /* SMP Temporary Key */
56 u8 enc_key_size;
57 u8 remote_key_dist;
58 bdaddr_t id_addr;
59 u8 id_addr_type;
60 u8 irk[16];
61 struct smp_csrk *csrk;
62 struct smp_csrk *slave_csrk;
63 struct smp_ltk *ltk;
64 struct smp_ltk *slave_ltk;
65 struct smp_irk *remote_irk;
Johan Hedberg4a74d652014-05-20 09:45:50 +030066 unsigned long flags;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030067
68 struct crypto_blkcipher *tfm_aes;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030069};
70
Johan Hedberg8a2936f2014-06-16 19:25:19 +030071static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030072{
Johan Hedberg8a2936f2014-06-16 19:25:19 +030073 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030074
Johan Hedberg8a2936f2014-06-16 19:25:19 +030075 for (i = 0; i < len; i++)
76 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030077}
78
79static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
80{
81 struct blkcipher_desc desc;
82 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +020083 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +020084 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030085
86 if (tfm == NULL) {
87 BT_ERR("tfm %p", tfm);
88 return -EINVAL;
89 }
90
91 desc.tfm = tfm;
92 desc.flags = 0;
93
Johan Hedberg943a7322014-03-18 12:58:24 +020094 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +030095 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +020096
97 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030098 if (err) {
99 BT_ERR("cipher setkey failed: %d", err);
100 return err;
101 }
102
Johan Hedberg943a7322014-03-18 12:58:24 +0200103 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300104 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200105
106 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300107
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300108 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
109 if (err)
110 BT_ERR("Encrypt data error %d", err);
111
Johan Hedberg943a7322014-03-18 12:58:24 +0200112 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300113 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200114
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300115 return err;
116}
117
Johan Hedberg60478052014-02-18 10:19:31 +0200118static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
119{
Johan Hedberg943a7322014-03-18 12:58:24 +0200120 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200121 int err;
122
123 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200124 memcpy(_res, r, 3);
125 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200126
Johan Hedberg943a7322014-03-18 12:58:24 +0200127 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200128 if (err) {
129 BT_ERR("Encrypt error");
130 return err;
131 }
132
133 /* The output of the random address function ah is:
134 * ah(h, r) = e(k, r') mod 2^24
135 * The output of the security function e is then truncated to 24 bits
136 * by taking the least significant 24 bits of the output of e as the
137 * result of ah.
138 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200139 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200140
141 return 0;
142}
143
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300144bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200145{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300146 struct l2cap_chan *chan = hdev->smp_data;
147 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200148 u8 hash[3];
149 int err;
150
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300151 if (!chan || !chan->data)
152 return false;
153
154 tfm = chan->data;
155
Johan Hedberg60478052014-02-18 10:19:31 +0200156 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
157
158 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
159 if (err)
160 return false;
161
162 return !memcmp(bdaddr->b, hash, 3);
163}
164
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300165int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200166{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300167 struct l2cap_chan *chan = hdev->smp_data;
168 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200169 int err;
170
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300171 if (!chan || !chan->data)
172 return -EOPNOTSUPP;
173
174 tfm = chan->data;
175
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200176 get_random_bytes(&rpa->b[3], 3);
177
178 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
179 rpa->b[5] |= 0x40; /* Set second most significant bit */
180
181 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
182 if (err < 0)
183 return err;
184
185 BT_DBG("RPA %pMR", rpa);
186
187 return 0;
188}
189
Johan Hedbergec70f362014-06-27 14:23:04 +0300190static int smp_c1(struct smp_chan *smp, u8 k[16], u8 r[16], u8 preq[7],
191 u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, bdaddr_t *ra,
192 u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300193{
Johan Hedbergec70f362014-06-27 14:23:04 +0300194 struct hci_dev *hdev = smp->conn->hcon->hdev;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300195 u8 p1[16], p2[16];
196 int err;
197
Johan Hedbergec70f362014-06-27 14:23:04 +0300198 BT_DBG("%s", hdev->name);
199
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300200 memset(p1, 0, 16);
201
202 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200203 p1[0] = _iat;
204 p1[1] = _rat;
205 memcpy(p1 + 2, preq, 7);
206 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300207
208 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200209 memcpy(p2, ra, 6);
210 memcpy(p2 + 6, ia, 6);
211 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300212
213 /* res = r XOR p1 */
214 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
215
216 /* res = e(k, res) */
Johan Hedbergec70f362014-06-27 14:23:04 +0300217 err = smp_e(smp->tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300218 if (err) {
219 BT_ERR("Encrypt data error");
220 return err;
221 }
222
223 /* res = res XOR p2 */
224 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
225
226 /* res = e(k, res) */
Johan Hedbergec70f362014-06-27 14:23:04 +0300227 err = smp_e(smp->tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300228 if (err)
229 BT_ERR("Encrypt data error");
230
231 return err;
232}
233
Johan Hedbergec70f362014-06-27 14:23:04 +0300234static int smp_s1(struct smp_chan *smp, u8 k[16], u8 r1[16], u8 r2[16],
235 u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300236{
Johan Hedbergec70f362014-06-27 14:23:04 +0300237 struct hci_dev *hdev = smp->conn->hcon->hdev;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300238 int err;
239
Johan Hedbergec70f362014-06-27 14:23:04 +0300240 BT_DBG("%s", hdev->name);
241
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300242 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200243 memcpy(_r, r2, 8);
244 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300245
Johan Hedbergec70f362014-06-27 14:23:04 +0300246 err = smp_e(smp->tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300247 if (err)
248 BT_ERR("Encrypt data error");
249
250 return err;
251}
252
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300253static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
254{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300255 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300256 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300257 struct kvec iv[2];
258 struct msghdr msg;
259
260 if (!chan)
261 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300262
263 BT_DBG("code 0x%2.2x", code);
264
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300265 iv[0].iov_base = &code;
266 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300267
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300268 iv[1].iov_base = data;
269 iv[1].iov_len = len;
270
271 memset(&msg, 0, sizeof(msg));
272
273 msg.msg_iov = (struct iovec *) &iv;
274 msg.msg_iovlen = 2;
275
276 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300277
Johan Hedbergb68fda62014-08-11 22:06:40 +0300278 if (!chan->data)
279 return;
280
281 smp = chan->data;
282
283 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300284 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300285}
286
Brian Gix2b64d152011-12-21 16:12:12 -0800287static __u8 authreq_to_seclevel(__u8 authreq)
288{
289 if (authreq & SMP_AUTH_MITM)
290 return BT_SECURITY_HIGH;
291 else
292 return BT_SECURITY_MEDIUM;
293}
294
295static __u8 seclevel_to_authreq(__u8 sec_level)
296{
297 switch (sec_level) {
298 case BT_SECURITY_HIGH:
299 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
300 case BT_SECURITY_MEDIUM:
301 return SMP_AUTH_BONDING;
302 default:
303 return SMP_AUTH_NONE;
304 }
305}
306
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300307static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700308 struct smp_cmd_pairing *req,
309 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300310{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300311 struct l2cap_chan *chan = conn->smp;
312 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200313 struct hci_conn *hcon = conn->hcon;
314 struct hci_dev *hdev = hcon->hdev;
315 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300316
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300317 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700318 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
319 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300320 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800321 } else {
322 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300323 }
324
Johan Hedbergfd349c02014-02-18 10:19:36 +0200325 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
326 remote_dist |= SMP_DIST_ID_KEY;
327
Johan Hedberg863efaf2014-02-22 19:06:32 +0200328 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
329 local_dist |= SMP_DIST_ID_KEY;
330
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300331 if (rsp == NULL) {
332 req->io_capability = conn->hcon->io_capability;
333 req->oob_flag = SMP_OOB_NOT_PRESENT;
334 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200335 req->init_key_dist = local_dist;
336 req->resp_key_dist = remote_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200337 req->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200338
339 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300340 return;
341 }
342
343 rsp->io_capability = conn->hcon->io_capability;
344 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
345 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200346 rsp->init_key_dist = req->init_key_dist & remote_dist;
347 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200348 rsp->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200349
350 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300351}
352
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300353static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
354{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300355 struct l2cap_chan *chan = conn->smp;
356 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300357
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300358 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700359 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300360 return SMP_ENC_KEY_SIZE;
361
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300362 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300363
364 return 0;
365}
366
Johan Hedberg6f48e262014-08-11 22:06:44 +0300367static void smp_chan_destroy(struct l2cap_conn *conn)
368{
369 struct l2cap_chan *chan = conn->smp;
370 struct smp_chan *smp = chan->data;
371 bool complete;
372
373 BUG_ON(!smp);
374
375 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300376
Johan Hedberg6f48e262014-08-11 22:06:44 +0300377 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
378 mgmt_smp_complete(conn->hcon, complete);
379
380 kfree(smp->csrk);
381 kfree(smp->slave_csrk);
382
383 crypto_free_blkcipher(smp->tfm_aes);
384
385 /* If pairing failed clean up any keys we might have */
386 if (!complete) {
387 if (smp->ltk) {
388 list_del(&smp->ltk->list);
389 kfree(smp->ltk);
390 }
391
392 if (smp->slave_ltk) {
393 list_del(&smp->slave_ltk->list);
394 kfree(smp->slave_ltk);
395 }
396
397 if (smp->remote_irk) {
398 list_del(&smp->remote_irk->list);
399 kfree(smp->remote_irk);
400 }
401 }
402
403 chan->data = NULL;
404 kfree(smp);
405 hci_conn_drop(conn->hcon);
406}
407
Johan Hedberg84794e12013-11-06 11:24:57 +0200408static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800409{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200410 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300411 struct l2cap_chan *chan = conn->smp;
412 struct smp_chan *smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200413
Johan Hedberg84794e12013-11-06 11:24:57 +0200414 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800415 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700416 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800417
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700418 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
419 mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type,
420 HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300421
Johan Hedbergb68fda62014-08-11 22:06:40 +0300422 if (!chan->data)
423 return;
424
425 smp = chan->data;
426
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700427 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300428 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800429}
430
Brian Gix2b64d152011-12-21 16:12:12 -0800431#define JUST_WORKS 0x00
432#define JUST_CFM 0x01
433#define REQ_PASSKEY 0x02
434#define CFM_PASSKEY 0x03
435#define REQ_OOB 0x04
436#define OVERLAP 0xFF
437
438static const u8 gen_method[5][5] = {
439 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
440 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
441 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
442 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
443 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
444};
445
Johan Hedberg581370c2014-06-17 13:07:38 +0300446static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
447{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300448 /* If either side has unknown io_caps, use JUST_CFM (which gets
449 * converted later to JUST_WORKS if we're initiators.
450 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300451 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
452 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300453 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300454
455 return gen_method[remote_io][local_io];
456}
457
Brian Gix2b64d152011-12-21 16:12:12 -0800458static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
459 u8 local_io, u8 remote_io)
460{
461 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300462 struct l2cap_chan *chan = conn->smp;
463 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800464 u8 method;
465 u32 passkey = 0;
466 int ret = 0;
467
468 /* Initialize key for JUST WORKS */
469 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300470 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800471
472 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
473
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300474 /* If neither side wants MITM, either "just" confirm an incoming
475 * request or use just-works for outgoing ones. The JUST_CFM
476 * will be converted to JUST_WORKS if necessary later in this
477 * function. If either side has MITM look up the method from the
478 * table.
479 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300480 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300481 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800482 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300483 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800484
Johan Hedberga82505c2014-03-24 14:39:07 +0200485 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300486 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200487 method = JUST_WORKS;
488
Johan Hedberg02f3e252014-07-16 15:09:13 +0300489 /* Don't bother user space with no IO capabilities */
490 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
491 method = JUST_WORKS;
492
Brian Gix2b64d152011-12-21 16:12:12 -0800493 /* If Just Works, Continue with Zero TK */
494 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300495 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800496 return 0;
497 }
498
499 /* Not Just Works/Confirm results in MITM Authentication */
500 if (method != JUST_CFM)
Johan Hedberg4a74d652014-05-20 09:45:50 +0300501 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
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 Hedberg1cc61142014-05-20 09:45:52 +0300561 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300562}
563
Johan Hedberg861580a2014-05-20 09:45:51 +0300564static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300565{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300566 struct l2cap_conn *conn = smp->conn;
567 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300568 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300569 int ret;
570
Johan Hedbergec70f362014-06-27 14:23:04 +0300571 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300572 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300573
574 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
575
Johan Hedbergec70f362014-06-27 14:23:04 +0300576 ret = smp_c1(smp, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200577 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200578 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300579 if (ret)
580 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300581
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300582 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
583 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300584 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300585 }
586
587 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800588 u8 stk[16];
589 __le64 rand = 0;
590 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300591
Johan Hedbergec70f362014-06-27 14:23:04 +0300592 smp_s1(smp, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300593
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300594 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300595 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300596
Johan Hedberg861580a2014-05-20 09:45:51 +0300597 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
598 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300599
600 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300601 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300602 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300603 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300604 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800605 __le64 rand = 0;
606 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300607
Johan Hedberg943a7322014-03-18 12:58:24 +0200608 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
609 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300610
Johan Hedbergec70f362014-06-27 14:23:04 +0300611 smp_s1(smp, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300612
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300613 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700614 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300615
Johan Hedbergfff34902014-06-10 15:19:50 +0300616 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
617 auth = 1;
618 else
619 auth = 0;
620
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300621 /* Even though there's no _SLAVE suffix this is the
622 * slave STK we're adding for later lookup (the master
623 * STK never needs to be stored).
624 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700625 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300626 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300627 }
628
Johan Hedberg861580a2014-05-20 09:45:51 +0300629 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300630}
631
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300632static void smp_notify_keys(struct l2cap_conn *conn)
633{
634 struct l2cap_chan *chan = conn->smp;
635 struct smp_chan *smp = chan->data;
636 struct hci_conn *hcon = conn->hcon;
637 struct hci_dev *hdev = hcon->hdev;
638 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
639 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
640 bool persistent;
641
642 if (smp->remote_irk) {
643 mgmt_new_irk(hdev, smp->remote_irk);
644 /* Now that user space can be considered to know the
645 * identity address track the connection based on it
646 * from now on.
647 */
648 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
649 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300650 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300651
652 /* When receiving an indentity resolving key for
653 * a remote device that does not use a resolvable
654 * private address, just remove the key so that
655 * it is possible to use the controller white
656 * list for scanning.
657 *
658 * Userspace will have been told to not store
659 * this key at this point. So it is safe to
660 * just remove it.
661 */
662 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
663 list_del(&smp->remote_irk->list);
664 kfree(smp->remote_irk);
665 smp->remote_irk = NULL;
666 }
667 }
668
669 /* The LTKs and CSRKs should be persistent only if both sides
670 * had the bonding bit set in their authentication requests.
671 */
672 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
673
674 if (smp->csrk) {
675 smp->csrk->bdaddr_type = hcon->dst_type;
676 bacpy(&smp->csrk->bdaddr, &hcon->dst);
677 mgmt_new_csrk(hdev, smp->csrk, persistent);
678 }
679
680 if (smp->slave_csrk) {
681 smp->slave_csrk->bdaddr_type = hcon->dst_type;
682 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
683 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
684 }
685
686 if (smp->ltk) {
687 smp->ltk->bdaddr_type = hcon->dst_type;
688 bacpy(&smp->ltk->bdaddr, &hcon->dst);
689 mgmt_new_ltk(hdev, smp->ltk, persistent);
690 }
691
692 if (smp->slave_ltk) {
693 smp->slave_ltk->bdaddr_type = hcon->dst_type;
694 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
695 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
696 }
697}
698
Johan Hedbergd6268e82014-09-05 22:19:51 +0300699static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300700{
701 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300702 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300703 struct hci_conn *hcon = conn->hcon;
704 struct hci_dev *hdev = hcon->hdev;
705 __u8 *keydist;
706
707 BT_DBG("conn %p", conn);
708
709 if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Johan Hedberg86d14072014-08-11 22:06:43 +0300710 return;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300711
712 rsp = (void *) &smp->prsp[1];
713
714 /* The responder sends its keys first */
715 if (hcon->out && (smp->remote_key_dist & 0x07))
Johan Hedberg86d14072014-08-11 22:06:43 +0300716 return;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300717
718 req = (void *) &smp->preq[1];
719
720 if (hcon->out) {
721 keydist = &rsp->init_key_dist;
722 *keydist &= req->init_key_dist;
723 } else {
724 keydist = &rsp->resp_key_dist;
725 *keydist &= req->resp_key_dist;
726 }
727
728 BT_DBG("keydist 0x%x", *keydist);
729
730 if (*keydist & SMP_DIST_ENC_KEY) {
731 struct smp_cmd_encrypt_info enc;
732 struct smp_cmd_master_ident ident;
733 struct smp_ltk *ltk;
734 u8 authenticated;
735 __le16 ediv;
736 __le64 rand;
737
738 get_random_bytes(enc.ltk, sizeof(enc.ltk));
739 get_random_bytes(&ediv, sizeof(ediv));
740 get_random_bytes(&rand, sizeof(rand));
741
742 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
743
744 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
745 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
746 SMP_LTK_SLAVE, authenticated, enc.ltk,
747 smp->enc_key_size, ediv, rand);
748 smp->slave_ltk = ltk;
749
750 ident.ediv = ediv;
751 ident.rand = rand;
752
753 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
754
755 *keydist &= ~SMP_DIST_ENC_KEY;
756 }
757
758 if (*keydist & SMP_DIST_ID_KEY) {
759 struct smp_cmd_ident_addr_info addrinfo;
760 struct smp_cmd_ident_info idinfo;
761
762 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
763
764 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
765
766 /* The hci_conn contains the local identity address
767 * after the connection has been established.
768 *
769 * This is true even when the connection has been
770 * established using a resolvable random address.
771 */
772 bacpy(&addrinfo.bdaddr, &hcon->src);
773 addrinfo.addr_type = hcon->src_type;
774
775 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
776 &addrinfo);
777
778 *keydist &= ~SMP_DIST_ID_KEY;
779 }
780
781 if (*keydist & SMP_DIST_SIGN) {
782 struct smp_cmd_sign_info sign;
783 struct smp_csrk *csrk;
784
785 /* Generate a new random key */
786 get_random_bytes(sign.csrk, sizeof(sign.csrk));
787
788 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
789 if (csrk) {
790 csrk->master = 0x00;
791 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
792 }
793 smp->slave_csrk = csrk;
794
795 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
796
797 *keydist &= ~SMP_DIST_SIGN;
798 }
799
800 /* If there are still keys to be received wait for them */
801 if ((smp->remote_key_dist & 0x07))
Johan Hedberg86d14072014-08-11 22:06:43 +0300802 return;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300803
804 clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300805 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
806 smp_notify_keys(conn);
807
808 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300809}
810
Johan Hedbergb68fda62014-08-11 22:06:40 +0300811static void smp_timeout(struct work_struct *work)
812{
813 struct smp_chan *smp = container_of(work, struct smp_chan,
814 security_timer.work);
815 struct l2cap_conn *conn = smp->conn;
816
817 BT_DBG("conn %p", conn);
818
Johan Hedberg1e91c292014-08-18 20:33:29 +0300819 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300820}
821
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300822static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
823{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300824 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300825 struct smp_chan *smp;
826
Marcel Holtmannf1560462013-10-13 05:43:25 -0700827 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedberg616d55b2014-07-29 14:18:48 +0300828 if (!smp) {
829 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300830 return NULL;
Johan Hedberg616d55b2014-07-29 14:18:48 +0300831 }
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300832
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300833 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
834 if (IS_ERR(smp->tfm_aes)) {
835 BT_ERR("Unable to create ECB crypto context");
836 kfree(smp);
Johan Hedberg616d55b2014-07-29 14:18:48 +0300837 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300838 return NULL;
839 }
840
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300841 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300842 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300843
Johan Hedbergb68fda62014-08-11 22:06:40 +0300844 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
845
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300846 hci_conn_hold(conn->hcon);
847
848 return smp;
849}
850
Brian Gix2b64d152011-12-21 16:12:12 -0800851int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
852{
Johan Hedbergb10e8012014-06-27 14:23:07 +0300853 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300854 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -0800855 struct smp_chan *smp;
856 u32 value;
Brian Gix2b64d152011-12-21 16:12:12 -0800857
858 BT_DBG("");
859
Johan Hedberg642ac772014-06-27 14:23:06 +0300860 if (!conn || !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Brian Gix2b64d152011-12-21 16:12:12 -0800861 return -ENOTCONN;
862
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300863 chan = conn->smp;
864 if (!chan)
865 return -ENOTCONN;
866
867 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800868
869 switch (mgmt_op) {
870 case MGMT_OP_USER_PASSKEY_REPLY:
871 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +0200872 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800873 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +0200874 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800875 /* Fall Through */
876 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +0300877 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800878 break;
879 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
880 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +0200881 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Brian Gix2b64d152011-12-21 16:12:12 -0800882 return 0;
883 default:
Johan Hedberg84794e12013-11-06 11:24:57 +0200884 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Brian Gix2b64d152011-12-21 16:12:12 -0800885 return -EOPNOTSUPP;
886 }
887
888 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +0300889 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
890 u8 rsp = smp_confirm(smp);
891 if (rsp)
892 smp_failure(conn, rsp);
893 }
Brian Gix2b64d152011-12-21 16:12:12 -0800894
895 return 0;
896}
897
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300898static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300899{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300900 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergb3c64102014-07-10 11:02:07 +0300901 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300902 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +0300903 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300904 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300905
906 BT_DBG("conn %p", conn);
907
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200908 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300909 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200910
Johan Hedberg40bef302014-07-16 11:42:27 +0300911 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -0800912 return SMP_CMD_NOTSUPP;
913
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300914 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) {
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300915 smp = smp_chan_create(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300916 } else {
917 struct l2cap_chan *chan = conn->smp;
918 smp = chan->data;
919 }
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300920
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +0300921 if (!smp)
922 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300923
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300924 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergb3c64102014-07-10 11:02:07 +0300925 (req->auth_req & SMP_AUTH_BONDING))
926 return SMP_PAIRING_NOTSUPP;
927
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300928 smp->preq[0] = SMP_CMD_PAIRING_REQ;
929 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300930 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300931
Brian Gix2b64d152011-12-21 16:12:12 -0800932 /* We didn't start the pairing, so match remote */
Johan Hedberg1ef35822014-05-20 09:45:48 +0300933 auth = req->auth_req;
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300934
Johan Hedbergc7262e72014-06-17 13:07:37 +0300935 sec_level = authreq_to_seclevel(auth);
936 if (sec_level > conn->hcon->pending_sec_level)
937 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +0200938
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300939 /* If we need MITM check that it can be acheived */
940 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
941 u8 method;
942
943 method = get_auth_method(smp, conn->hcon->io_capability,
944 req->io_capability);
945 if (method == JUST_WORKS || method == JUST_CFM)
946 return SMP_AUTH_REQUIREMENTS;
947 }
948
Brian Gix2b64d152011-12-21 16:12:12 -0800949 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300950
951 key_size = min(req->max_key_size, rsp.max_key_size);
952 if (check_enc_key_size(conn, key_size))
953 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300954
Johan Hedberge84a6b12013-12-02 10:49:03 +0200955 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300956
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300957 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
958 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -0300959
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300960 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300961
Brian Gix2b64d152011-12-21 16:12:12 -0800962 /* Request setup of TK */
963 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
964 if (ret)
965 return SMP_UNSPECIFIED;
966
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300967 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300968}
969
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300970static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300971{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300972 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300973 struct l2cap_chan *chan = conn->smp;
974 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800975 u8 key_size, auth = SMP_AUTH_NONE;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -0300976 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300977
978 BT_DBG("conn %p", conn);
979
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200980 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300981 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200982
Johan Hedberg40bef302014-07-16 11:42:27 +0300983 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800984 return SMP_CMD_NOTSUPP;
985
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300986 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300987
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300988 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300989
990 key_size = min(req->max_key_size, rsp->max_key_size);
991 if (check_enc_key_size(conn, key_size))
992 return SMP_ENC_KEY_SIZE;
993
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300994 /* If we need MITM check that it can be acheived */
995 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
996 u8 method;
997
998 method = get_auth_method(smp, req->io_capability,
999 rsp->io_capability);
1000 if (method == JUST_WORKS || method == JUST_CFM)
1001 return SMP_AUTH_REQUIREMENTS;
1002 }
1003
Johan Hedberge84a6b12013-12-02 10:49:03 +02001004 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001005
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001006 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1007 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001008
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001009 /* Update remote key distribution in case the remote cleared
1010 * some bits that we had enabled in our request.
1011 */
1012 smp->remote_key_dist &= rsp->resp_key_dist;
1013
Brian Gix2b64d152011-12-21 16:12:12 -08001014 if ((req->auth_req & SMP_AUTH_BONDING) &&
Marcel Holtmannf1560462013-10-13 05:43:25 -07001015 (rsp->auth_req & SMP_AUTH_BONDING))
Brian Gix2b64d152011-12-21 16:12:12 -08001016 auth = SMP_AUTH_BONDING;
1017
1018 auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM;
1019
Johan Hedberg476585e2012-06-06 18:54:15 +08001020 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001021 if (ret)
1022 return SMP_UNSPECIFIED;
1023
Johan Hedberg4a74d652014-05-20 09:45:50 +03001024 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001025
1026 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001027 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001028 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001029
1030 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001031}
1032
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001033static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001034{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001035 struct l2cap_chan *chan = conn->smp;
1036 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001037
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001038 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1039
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001040 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001041 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001042
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001043 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1044 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001045
Johan Hedberg943a7322014-03-18 12:58:24 +02001046 if (conn->hcon->out)
1047 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1048 smp->prnd);
Johan Hedberg4a74d652014-05-20 09:45:50 +03001049 else if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001050 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001051 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001052 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001053
1054 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001055}
1056
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001057static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001058{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001059 struct l2cap_chan *chan = conn->smp;
1060 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001061
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001062 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001063
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001064 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001065 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001066
Johan Hedberg943a7322014-03-18 12:58:24 +02001067 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001068 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001069
Johan Hedberg861580a2014-05-20 09:45:51 +03001070 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001071}
1072
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001073static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001074{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001075 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001076 struct hci_conn *hcon = conn->hcon;
1077
Johan Hedberg98a0b842014-01-30 19:40:00 -08001078 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001079 hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001080 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001081 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001082
Johan Hedberg4dab7862012-06-07 14:58:37 +08001083 if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001084 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001085
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001086 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001087 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001088
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001089 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1090 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001091
Johan Hedbergfe59a052014-07-01 19:14:12 +03001092 /* We never store STKs for master role, so clear this flag */
1093 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1094
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001095 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001096}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001097
Johan Hedberg854f4722014-07-01 18:40:20 +03001098bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level)
1099{
1100 if (sec_level == BT_SECURITY_LOW)
1101 return true;
1102
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001103 /* If we're encrypted with an STK always claim insufficient
1104 * security. This way we allow the connection to be re-encrypted
1105 * with an LTK, even if the LTK provides the same level of
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001106 * security. Only exception is if we don't have an LTK (e.g.
1107 * because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001108 */
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001109 if (test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
1110 hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001111 hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001112 return false;
1113
Johan Hedberg854f4722014-07-01 18:40:20 +03001114 if (hcon->sec_level >= sec_level)
1115 return true;
1116
1117 return false;
1118}
1119
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001120static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001121{
1122 struct smp_cmd_security_req *rp = (void *) skb->data;
1123 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001124 struct hci_conn *hcon = conn->hcon;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001125 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001126 u8 sec_level;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001127
1128 BT_DBG("conn %p", conn);
1129
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001130 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001131 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001132
Johan Hedberg40bef302014-07-16 11:42:27 +03001133 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001134 return SMP_CMD_NOTSUPP;
1135
Johan Hedbergc7262e72014-06-17 13:07:37 +03001136 sec_level = authreq_to_seclevel(rp->auth_req);
Johan Hedberg854f4722014-07-01 18:40:20 +03001137 if (smp_sufficient_security(hcon, sec_level))
1138 return 0;
1139
Johan Hedbergc7262e72014-06-17 13:07:37 +03001140 if (sec_level > hcon->pending_sec_level)
1141 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001142
Johan Hedberg4dab7862012-06-07 14:58:37 +08001143 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001144 return 0;
1145
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001146 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001147 return 0;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001148
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001149 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001150 if (!smp)
1151 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001152
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001153 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedberg616d55b2014-07-29 14:18:48 +03001154 (rp->auth_req & SMP_AUTH_BONDING))
1155 return SMP_PAIRING_NOTSUPP;
1156
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001157 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001158
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001159 memset(&cp, 0, sizeof(cp));
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -03001160 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001161
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001162 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1163 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001164
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001165 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001166
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001167 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001168}
1169
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001170int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001171{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001172 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001173 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001174 __u8 authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001175
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001176 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1177
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001178 /* This may be NULL if there's an unexpected disconnection */
1179 if (!conn)
1180 return 1;
1181
Johan Hedberg757aee02013-04-24 13:05:32 +03001182 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001183 return 1;
1184
Johan Hedbergad32a2f2013-05-14 18:05:12 +03001185 if (smp_sufficient_security(hcon, sec_level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001186 return 1;
1187
Johan Hedbergc7262e72014-06-17 13:07:37 +03001188 if (sec_level > hcon->pending_sec_level)
1189 hcon->pending_sec_level = sec_level;
1190
Johan Hedberg40bef302014-07-16 11:42:27 +03001191 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001192 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1193 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001194
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001195 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001196 return 0;
1197
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001198 smp = smp_chan_create(conn);
Brian Gix2b64d152011-12-21 16:12:12 -08001199 if (!smp)
1200 return 1;
1201
1202 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001203
Johan Hedberg79897d22014-06-01 09:45:24 +03001204 /* Require MITM if IO Capability allows or the security level
1205 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001206 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001207 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001208 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001209 authreq |= SMP_AUTH_MITM;
1210
Johan Hedberg40bef302014-07-16 11:42:27 +03001211 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001212 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001213
Brian Gix2b64d152011-12-21 16:12:12 -08001214 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001215 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1216 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001217
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001218 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
1219 } else {
1220 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001221 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001222 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
1223 }
1224
Johan Hedberg4a74d652014-05-20 09:45:50 +03001225 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergedca7922014-03-24 15:54:11 +02001226
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001227 return 0;
1228}
1229
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001230static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1231{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001232 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001233 struct l2cap_chan *chan = conn->smp;
1234 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001235
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001236 BT_DBG("conn %p", conn);
1237
1238 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001239 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001240
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001241 /* Ignore this PDU if it wasn't requested */
1242 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
1243 return 0;
1244
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001245 skb_pull(skb, sizeof(*rp));
1246
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001247 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001248
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001249 return 0;
1250}
1251
1252static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1253{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001254 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001255 struct l2cap_chan *chan = conn->smp;
1256 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001257 struct hci_dev *hdev = conn->hcon->hdev;
1258 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001259 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001260 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001261
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001262 BT_DBG("conn %p", conn);
1263
1264 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001265 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001266
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001267 /* Ignore this PDU if it wasn't requested */
1268 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
1269 return 0;
1270
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001271 /* Mark the information as received */
1272 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1273
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001274 skb_pull(skb, sizeof(*rp));
1275
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001276 hci_dev_lock(hdev);
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001277 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001278 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001279 authenticated, smp->tk, smp->enc_key_size,
1280 rp->ediv, rp->rand);
1281 smp->ltk = ltk;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001282 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001283 smp_distribute_keys(smp);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001284 hci_dev_unlock(hdev);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001285
1286 return 0;
1287}
1288
Johan Hedbergfd349c02014-02-18 10:19:36 +02001289static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1290{
1291 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001292 struct l2cap_chan *chan = conn->smp;
1293 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001294
1295 BT_DBG("");
1296
1297 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001298 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001299
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001300 /* Ignore this PDU if it wasn't requested */
1301 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1302 return 0;
1303
Johan Hedbergfd349c02014-02-18 10:19:36 +02001304 skb_pull(skb, sizeof(*info));
1305
1306 memcpy(smp->irk, info->irk, 16);
1307
1308 return 0;
1309}
1310
1311static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1312 struct sk_buff *skb)
1313{
1314 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001315 struct l2cap_chan *chan = conn->smp;
1316 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001317 struct hci_conn *hcon = conn->hcon;
Johan Hedberg86d14072014-08-11 22:06:43 +03001318 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001319 bdaddr_t rpa;
1320
1321 BT_DBG("");
1322
1323 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001324 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001325
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001326 /* Ignore this PDU if it wasn't requested */
1327 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1328 return 0;
1329
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001330 /* Mark the information as received */
1331 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1332
Johan Hedbergfd349c02014-02-18 10:19:36 +02001333 skb_pull(skb, sizeof(*info));
1334
Johan Hedberg31dd6242014-06-27 14:23:02 +03001335 hci_dev_lock(hcon->hdev);
1336
Johan Hedberga9a58f82014-02-25 22:24:37 +02001337 /* Strictly speaking the Core Specification (4.1) allows sending
1338 * an empty address which would force us to rely on just the IRK
1339 * as "identity information". However, since such
1340 * implementations are not known of and in order to not over
1341 * complicate our implementation, simply pretend that we never
1342 * received an IRK for such a device.
1343 */
1344 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1345 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001346 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001347 }
1348
Johan Hedbergfd349c02014-02-18 10:19:36 +02001349 bacpy(&smp->id_addr, &info->bdaddr);
1350 smp->id_addr_type = info->addr_type;
1351
1352 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1353 bacpy(&rpa, &hcon->dst);
1354 else
1355 bacpy(&rpa, BDADDR_ANY);
1356
Johan Hedberg23d0e122014-02-19 14:57:46 +02001357 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1358 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001359
Johan Hedberg31dd6242014-06-27 14:23:02 +03001360distribute:
Johan Hedbergd6268e82014-09-05 22:19:51 +03001361 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001362
Johan Hedberg31dd6242014-06-27 14:23:02 +03001363 hci_dev_unlock(hcon->hdev);
1364
Johan Hedbergfd349c02014-02-18 10:19:36 +02001365 return 0;
1366}
1367
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001368static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1369{
1370 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001371 struct l2cap_chan *chan = conn->smp;
1372 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001373 struct hci_dev *hdev = conn->hcon->hdev;
1374 struct smp_csrk *csrk;
1375
1376 BT_DBG("conn %p", conn);
1377
1378 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001379 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001380
1381 /* Ignore this PDU if it wasn't requested */
1382 if (!(smp->remote_key_dist & SMP_DIST_SIGN))
1383 return 0;
1384
1385 /* Mark the information as received */
1386 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1387
1388 skb_pull(skb, sizeof(*rp));
1389
1390 hci_dev_lock(hdev);
1391 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1392 if (csrk) {
1393 csrk->master = 0x01;
1394 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1395 }
1396 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001397 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001398 hci_dev_unlock(hdev);
1399
1400 return 0;
1401}
1402
Johan Hedberg4befb862014-08-11 22:06:38 +03001403static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001404{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001405 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001406 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001407 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001408 int err = 0;
1409
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001410 if (hcon->type != LE_LINK) {
1411 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001412 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001413 }
1414
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001415 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001416 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001417
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001418 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001419 reason = SMP_PAIRING_NOTSUPP;
1420 goto done;
1421 }
1422
Marcel Holtmann92381f52013-10-03 01:23:08 -07001423 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001424 skb_pull(skb, sizeof(code));
1425
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001426 /*
1427 * The SMP context must be initialized for all other PDUs except
1428 * pairing and security requests. If we get any other PDU when
1429 * not initialized simply disconnect (done if this function
1430 * returns an error).
1431 */
1432 if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ &&
Johan Hedbergd3368602014-08-08 09:28:05 +03001433 !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) {
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001434 BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001435 err = -EOPNOTSUPP;
1436 goto done;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001437 }
1438
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001439 switch (code) {
1440 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001441 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001442 break;
1443
1444 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001445 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001446 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001447 break;
1448
1449 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001450 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001451 break;
1452
1453 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001454 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001455 break;
1456
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001457 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001458 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001459 break;
1460
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001461 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001462 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001463 break;
1464
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001465 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001466 reason = smp_cmd_encrypt_info(conn, skb);
1467 break;
1468
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001469 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001470 reason = smp_cmd_master_ident(conn, skb);
1471 break;
1472
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001473 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001474 reason = smp_cmd_ident_info(conn, skb);
1475 break;
1476
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001477 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001478 reason = smp_cmd_ident_addr_info(conn, skb);
1479 break;
1480
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001481 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001482 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001483 break;
1484
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001485 default:
1486 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001487 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001488 goto done;
1489 }
1490
1491done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001492 if (!err) {
1493 if (reason)
1494 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001495 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001496 }
1497
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001498 return err;
1499}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001500
Johan Hedberg70db83c2014-08-08 09:37:16 +03001501static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1502{
1503 struct l2cap_conn *conn = chan->conn;
1504
1505 BT_DBG("chan %p", chan);
1506
Johan Hedberg109ec232014-08-11 22:06:42 +03001507 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001508 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001509
Johan Hedberg70db83c2014-08-08 09:37:16 +03001510 conn->smp = NULL;
1511 l2cap_chan_put(chan);
1512}
1513
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001514static void smp_resume_cb(struct l2cap_chan *chan)
1515{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001516 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001517 struct l2cap_conn *conn = chan->conn;
1518 struct hci_conn *hcon = conn->hcon;
1519
1520 BT_DBG("chan %p", chan);
1521
Johan Hedberg86d14072014-08-11 22:06:43 +03001522 if (!smp)
1523 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001524
Johan Hedberg84bc0db2014-09-05 22:19:49 +03001525 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1526 return;
1527
Johan Hedberg86d14072014-08-11 22:06:43 +03001528 cancel_delayed_work(&smp->security_timer);
1529
Johan Hedbergd6268e82014-09-05 22:19:51 +03001530 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001531}
1532
Johan Hedberg70db83c2014-08-08 09:37:16 +03001533static void smp_ready_cb(struct l2cap_chan *chan)
1534{
1535 struct l2cap_conn *conn = chan->conn;
1536
1537 BT_DBG("chan %p", chan);
1538
1539 conn->smp = chan;
1540 l2cap_chan_hold(chan);
1541}
1542
Johan Hedberg4befb862014-08-11 22:06:38 +03001543static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1544{
1545 int err;
1546
1547 BT_DBG("chan %p", chan);
1548
1549 err = smp_sig_channel(chan, skb);
1550 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001551 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001552
Johan Hedbergb68fda62014-08-11 22:06:40 +03001553 if (smp)
1554 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001555
Johan Hedberg1e91c292014-08-18 20:33:29 +03001556 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03001557 }
1558
1559 return err;
1560}
1561
Johan Hedberg70db83c2014-08-08 09:37:16 +03001562static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1563 unsigned long hdr_len,
1564 unsigned long len, int nb)
1565{
1566 struct sk_buff *skb;
1567
1568 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1569 if (!skb)
1570 return ERR_PTR(-ENOMEM);
1571
1572 skb->priority = HCI_PRIO_MAX;
1573 bt_cb(skb)->chan = chan;
1574
1575 return skb;
1576}
1577
1578static const struct l2cap_ops smp_chan_ops = {
1579 .name = "Security Manager",
1580 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001581 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001582 .alloc_skb = smp_alloc_skb_cb,
1583 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001584 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001585
1586 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001587 .state_change = l2cap_chan_no_state_change,
1588 .close = l2cap_chan_no_close,
1589 .defer = l2cap_chan_no_defer,
1590 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001591 .set_shutdown = l2cap_chan_no_set_shutdown,
1592 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1593 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1594};
1595
1596static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1597{
1598 struct l2cap_chan *chan;
1599
1600 BT_DBG("pchan %p", pchan);
1601
1602 chan = l2cap_chan_create();
1603 if (!chan)
1604 return NULL;
1605
1606 chan->chan_type = pchan->chan_type;
1607 chan->ops = &smp_chan_ops;
1608 chan->scid = pchan->scid;
1609 chan->dcid = chan->scid;
1610 chan->imtu = pchan->imtu;
1611 chan->omtu = pchan->omtu;
1612 chan->mode = pchan->mode;
1613
1614 BT_DBG("created chan %p", chan);
1615
1616 return chan;
1617}
1618
1619static const struct l2cap_ops smp_root_chan_ops = {
1620 .name = "Security Manager Root",
1621 .new_connection = smp_new_conn_cb,
1622
1623 /* None of these are implemented for the root channel */
1624 .close = l2cap_chan_no_close,
1625 .alloc_skb = l2cap_chan_no_alloc_skb,
1626 .recv = l2cap_chan_no_recv,
1627 .state_change = l2cap_chan_no_state_change,
1628 .teardown = l2cap_chan_no_teardown,
1629 .ready = l2cap_chan_no_ready,
1630 .defer = l2cap_chan_no_defer,
1631 .suspend = l2cap_chan_no_suspend,
1632 .resume = l2cap_chan_no_resume,
1633 .set_shutdown = l2cap_chan_no_set_shutdown,
1634 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1635 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1636};
1637
Johan Hedberg711eafe2014-08-08 09:32:52 +03001638int smp_register(struct hci_dev *hdev)
1639{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001640 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001641 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001642
Johan Hedberg711eafe2014-08-08 09:32:52 +03001643 BT_DBG("%s", hdev->name);
1644
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001645 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1646 if (IS_ERR(tfm_aes)) {
1647 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001648 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03001649 return err;
1650 }
1651
Johan Hedberg70db83c2014-08-08 09:37:16 +03001652 chan = l2cap_chan_create();
1653 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001654 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001655 return -ENOMEM;
1656 }
1657
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001658 chan->data = tfm_aes;
1659
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001660 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001661
1662 l2cap_chan_set_defaults(chan);
1663
1664 bacpy(&chan->src, &hdev->bdaddr);
1665 chan->src_type = BDADDR_LE_PUBLIC;
1666 chan->state = BT_LISTEN;
1667 chan->mode = L2CAP_MODE_BASIC;
1668 chan->imtu = L2CAP_DEFAULT_MTU;
1669 chan->ops = &smp_root_chan_ops;
1670
1671 hdev->smp_data = chan;
1672
Johan Hedberg711eafe2014-08-08 09:32:52 +03001673 return 0;
1674}
1675
1676void smp_unregister(struct hci_dev *hdev)
1677{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001678 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001679 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001680
1681 if (!chan)
1682 return;
1683
1684 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001685
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001686 tfm_aes = chan->data;
1687 if (tfm_aes) {
1688 chan->data = NULL;
1689 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001690 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03001691
1692 hdev->smp_data = NULL;
1693 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001694}