blob: 7ab5f52955cb85e010328be6a82bc1053436c8b0 [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);
284 if (test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
285 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300286}
287
Brian Gix2b64d152011-12-21 16:12:12 -0800288static __u8 authreq_to_seclevel(__u8 authreq)
289{
290 if (authreq & SMP_AUTH_MITM)
291 return BT_SECURITY_HIGH;
292 else
293 return BT_SECURITY_MEDIUM;
294}
295
296static __u8 seclevel_to_authreq(__u8 sec_level)
297{
298 switch (sec_level) {
299 case BT_SECURITY_HIGH:
300 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
301 case BT_SECURITY_MEDIUM:
302 return SMP_AUTH_BONDING;
303 default:
304 return SMP_AUTH_NONE;
305 }
306}
307
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300308static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700309 struct smp_cmd_pairing *req,
310 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300311{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300312 struct l2cap_chan *chan = conn->smp;
313 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200314 struct hci_conn *hcon = conn->hcon;
315 struct hci_dev *hdev = hcon->hdev;
316 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300317
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300318 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700319 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
320 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300321 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800322 } else {
323 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300324 }
325
Johan Hedbergfd349c02014-02-18 10:19:36 +0200326 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
327 remote_dist |= SMP_DIST_ID_KEY;
328
Johan Hedberg863efaf2014-02-22 19:06:32 +0200329 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
330 local_dist |= SMP_DIST_ID_KEY;
331
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300332 if (rsp == NULL) {
333 req->io_capability = conn->hcon->io_capability;
334 req->oob_flag = SMP_OOB_NOT_PRESENT;
335 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200336 req->init_key_dist = local_dist;
337 req->resp_key_dist = remote_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200338 req->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200339
340 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300341 return;
342 }
343
344 rsp->io_capability = conn->hcon->io_capability;
345 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
346 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200347 rsp->init_key_dist = req->init_key_dist & remote_dist;
348 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200349 rsp->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200350
351 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300352}
353
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300354static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
355{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300356 struct l2cap_chan *chan = conn->smp;
357 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300358
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300359 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700360 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300361 return SMP_ENC_KEY_SIZE;
362
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300363 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300364
365 return 0;
366}
367
Johan Hedberg84794e12013-11-06 11:24:57 +0200368static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800369{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200370 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300371 struct l2cap_chan *chan = conn->smp;
372 struct smp_chan *smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200373
Johan Hedberg84794e12013-11-06 11:24:57 +0200374 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800375 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700376 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800377
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700378 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
379 mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type,
380 HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300381
Johan Hedbergb68fda62014-08-11 22:06:40 +0300382 if (!chan->data)
383 return;
384
385 smp = chan->data;
386
387 cancel_delayed_work_sync(&smp->security_timer);
Andre Guedes61a0cfb2012-08-01 20:34:15 -0300388
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700389 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300390 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800391}
392
Brian Gix2b64d152011-12-21 16:12:12 -0800393#define JUST_WORKS 0x00
394#define JUST_CFM 0x01
395#define REQ_PASSKEY 0x02
396#define CFM_PASSKEY 0x03
397#define REQ_OOB 0x04
398#define OVERLAP 0xFF
399
400static const u8 gen_method[5][5] = {
401 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
402 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
403 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
404 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
405 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
406};
407
Johan Hedberg581370c2014-06-17 13:07:38 +0300408static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
409{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300410 /* If either side has unknown io_caps, use JUST_CFM (which gets
411 * converted later to JUST_WORKS if we're initiators.
412 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300413 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
414 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300415 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300416
417 return gen_method[remote_io][local_io];
418}
419
Brian Gix2b64d152011-12-21 16:12:12 -0800420static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
421 u8 local_io, u8 remote_io)
422{
423 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300424 struct l2cap_chan *chan = conn->smp;
425 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800426 u8 method;
427 u32 passkey = 0;
428 int ret = 0;
429
430 /* Initialize key for JUST WORKS */
431 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300432 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800433
434 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
435
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300436 /* If neither side wants MITM, either "just" confirm an incoming
437 * request or use just-works for outgoing ones. The JUST_CFM
438 * will be converted to JUST_WORKS if necessary later in this
439 * function. If either side has MITM look up the method from the
440 * table.
441 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300442 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300443 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800444 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300445 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800446
Johan Hedberga82505c2014-03-24 14:39:07 +0200447 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300448 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200449 method = JUST_WORKS;
450
Johan Hedberg02f3e252014-07-16 15:09:13 +0300451 /* Don't bother user space with no IO capabilities */
452 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
453 method = JUST_WORKS;
454
Brian Gix2b64d152011-12-21 16:12:12 -0800455 /* If Just Works, Continue with Zero TK */
456 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300457 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800458 return 0;
459 }
460
461 /* Not Just Works/Confirm results in MITM Authentication */
462 if (method != JUST_CFM)
Johan Hedberg4a74d652014-05-20 09:45:50 +0300463 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800464
465 /* If both devices have Keyoard-Display I/O, the master
466 * Confirms and the slave Enters the passkey.
467 */
468 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300469 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800470 method = CFM_PASSKEY;
471 else
472 method = REQ_PASSKEY;
473 }
474
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200475 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800476 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200477 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800478 get_random_bytes(&passkey, sizeof(passkey));
479 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200480 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800481 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300482 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800483 }
484
485 hci_dev_lock(hcon->hdev);
486
487 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700488 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200489 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200490 else if (method == JUST_CFM)
491 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
492 hcon->type, hcon->dst_type,
493 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800494 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200495 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200496 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200497 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800498
499 hci_dev_unlock(hcon->hdev);
500
501 return ret;
502}
503
Johan Hedberg1cc61142014-05-20 09:45:52 +0300504static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300505{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300506 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300507 struct smp_cmd_pairing_confirm cp;
508 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300509
510 BT_DBG("conn %p", conn);
511
Johan Hedbergec70f362014-06-27 14:23:04 +0300512 ret = smp_c1(smp, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200513 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200514 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
515 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300516 if (ret)
517 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300518
Johan Hedberg4a74d652014-05-20 09:45:50 +0300519 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800520
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300521 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
522
Johan Hedberg1cc61142014-05-20 09:45:52 +0300523 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300524}
525
Johan Hedberg861580a2014-05-20 09:45:51 +0300526static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300527{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300528 struct l2cap_conn *conn = smp->conn;
529 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300530 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300531 int ret;
532
Johan Hedbergec70f362014-06-27 14:23:04 +0300533 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300534 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300535
536 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
537
Johan Hedbergec70f362014-06-27 14:23:04 +0300538 ret = smp_c1(smp, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200539 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200540 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300541 if (ret)
542 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300543
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300544 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
545 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300546 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300547 }
548
549 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800550 u8 stk[16];
551 __le64 rand = 0;
552 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300553
Johan Hedbergec70f362014-06-27 14:23:04 +0300554 smp_s1(smp, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300555
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300556 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300557 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300558
Johan Hedberg861580a2014-05-20 09:45:51 +0300559 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
560 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300561
562 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300563 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300564 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300565 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300566 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800567 __le64 rand = 0;
568 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300569
Johan Hedberg943a7322014-03-18 12:58:24 +0200570 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
571 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300572
Johan Hedbergec70f362014-06-27 14:23:04 +0300573 smp_s1(smp, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300574
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300575 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700576 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300577
Johan Hedbergfff34902014-06-10 15:19:50 +0300578 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
579 auth = 1;
580 else
581 auth = 0;
582
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300583 /* Even though there's no _SLAVE suffix this is the
584 * slave STK we're adding for later lookup (the master
585 * STK never needs to be stored).
586 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700587 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300588 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300589 }
590
Johan Hedberg861580a2014-05-20 09:45:51 +0300591 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300592}
593
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300594static void smp_notify_keys(struct l2cap_conn *conn)
595{
596 struct l2cap_chan *chan = conn->smp;
597 struct smp_chan *smp = chan->data;
598 struct hci_conn *hcon = conn->hcon;
599 struct hci_dev *hdev = hcon->hdev;
600 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
601 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
602 bool persistent;
603
604 if (smp->remote_irk) {
605 mgmt_new_irk(hdev, smp->remote_irk);
606 /* Now that user space can be considered to know the
607 * identity address track the connection based on it
608 * from now on.
609 */
610 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
611 hcon->dst_type = smp->remote_irk->addr_type;
612 l2cap_conn_update_id_addr(hcon);
613
614 /* When receiving an indentity resolving key for
615 * a remote device that does not use a resolvable
616 * private address, just remove the key so that
617 * it is possible to use the controller white
618 * list for scanning.
619 *
620 * Userspace will have been told to not store
621 * this key at this point. So it is safe to
622 * just remove it.
623 */
624 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
625 list_del(&smp->remote_irk->list);
626 kfree(smp->remote_irk);
627 smp->remote_irk = NULL;
628 }
629 }
630
631 /* The LTKs and CSRKs should be persistent only if both sides
632 * had the bonding bit set in their authentication requests.
633 */
634 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
635
636 if (smp->csrk) {
637 smp->csrk->bdaddr_type = hcon->dst_type;
638 bacpy(&smp->csrk->bdaddr, &hcon->dst);
639 mgmt_new_csrk(hdev, smp->csrk, persistent);
640 }
641
642 if (smp->slave_csrk) {
643 smp->slave_csrk->bdaddr_type = hcon->dst_type;
644 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
645 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
646 }
647
648 if (smp->ltk) {
649 smp->ltk->bdaddr_type = hcon->dst_type;
650 bacpy(&smp->ltk->bdaddr, &hcon->dst);
651 mgmt_new_ltk(hdev, smp->ltk, persistent);
652 }
653
654 if (smp->slave_ltk) {
655 smp->slave_ltk->bdaddr_type = hcon->dst_type;
656 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
657 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
658 }
659}
660
661static int smp_distribute_keys(struct l2cap_conn *conn)
662{
663 struct smp_cmd_pairing *req, *rsp;
664 struct l2cap_chan *chan = conn->smp;
665 struct smp_chan *smp = chan->data;
666 struct hci_conn *hcon = conn->hcon;
667 struct hci_dev *hdev = hcon->hdev;
668 __u8 *keydist;
669
670 BT_DBG("conn %p", conn);
671
672 if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
673 return 0;
674
675 rsp = (void *) &smp->prsp[1];
676
677 /* The responder sends its keys first */
678 if (hcon->out && (smp->remote_key_dist & 0x07))
679 return 0;
680
681 req = (void *) &smp->preq[1];
682
683 if (hcon->out) {
684 keydist = &rsp->init_key_dist;
685 *keydist &= req->init_key_dist;
686 } else {
687 keydist = &rsp->resp_key_dist;
688 *keydist &= req->resp_key_dist;
689 }
690
691 BT_DBG("keydist 0x%x", *keydist);
692
693 if (*keydist & SMP_DIST_ENC_KEY) {
694 struct smp_cmd_encrypt_info enc;
695 struct smp_cmd_master_ident ident;
696 struct smp_ltk *ltk;
697 u8 authenticated;
698 __le16 ediv;
699 __le64 rand;
700
701 get_random_bytes(enc.ltk, sizeof(enc.ltk));
702 get_random_bytes(&ediv, sizeof(ediv));
703 get_random_bytes(&rand, sizeof(rand));
704
705 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
706
707 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
708 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
709 SMP_LTK_SLAVE, authenticated, enc.ltk,
710 smp->enc_key_size, ediv, rand);
711 smp->slave_ltk = ltk;
712
713 ident.ediv = ediv;
714 ident.rand = rand;
715
716 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
717
718 *keydist &= ~SMP_DIST_ENC_KEY;
719 }
720
721 if (*keydist & SMP_DIST_ID_KEY) {
722 struct smp_cmd_ident_addr_info addrinfo;
723 struct smp_cmd_ident_info idinfo;
724
725 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
726
727 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
728
729 /* The hci_conn contains the local identity address
730 * after the connection has been established.
731 *
732 * This is true even when the connection has been
733 * established using a resolvable random address.
734 */
735 bacpy(&addrinfo.bdaddr, &hcon->src);
736 addrinfo.addr_type = hcon->src_type;
737
738 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
739 &addrinfo);
740
741 *keydist &= ~SMP_DIST_ID_KEY;
742 }
743
744 if (*keydist & SMP_DIST_SIGN) {
745 struct smp_cmd_sign_info sign;
746 struct smp_csrk *csrk;
747
748 /* Generate a new random key */
749 get_random_bytes(sign.csrk, sizeof(sign.csrk));
750
751 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
752 if (csrk) {
753 csrk->master = 0x00;
754 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
755 }
756 smp->slave_csrk = csrk;
757
758 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
759
760 *keydist &= ~SMP_DIST_SIGN;
761 }
762
763 /* If there are still keys to be received wait for them */
764 if ((smp->remote_key_dist & 0x07))
765 return 0;
766
767 clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300768 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300769 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
770 smp_notify_keys(conn);
771
772 smp_chan_destroy(conn);
773
774 return 0;
775}
776
Johan Hedbergb68fda62014-08-11 22:06:40 +0300777static void smp_timeout(struct work_struct *work)
778{
779 struct smp_chan *smp = container_of(work, struct smp_chan,
780 security_timer.work);
781 struct l2cap_conn *conn = smp->conn;
782
783 BT_DBG("conn %p", conn);
784
785 l2cap_conn_shutdown(conn, ETIMEDOUT);
786}
787
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300788static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
789{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300790 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300791 struct smp_chan *smp;
792
Marcel Holtmannf1560462013-10-13 05:43:25 -0700793 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedberg616d55b2014-07-29 14:18:48 +0300794 if (!smp) {
795 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300796 return NULL;
Johan Hedberg616d55b2014-07-29 14:18:48 +0300797 }
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300798
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300799 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
800 if (IS_ERR(smp->tfm_aes)) {
801 BT_ERR("Unable to create ECB crypto context");
802 kfree(smp);
Johan Hedberg616d55b2014-07-29 14:18:48 +0300803 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300804 return NULL;
805 }
806
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300807 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300808 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300809
Johan Hedbergb68fda62014-08-11 22:06:40 +0300810 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
811
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300812 hci_conn_hold(conn->hcon);
813
814 return smp;
815}
816
817void smp_chan_destroy(struct l2cap_conn *conn)
818{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300819 struct l2cap_chan *chan = conn->smp;
820 struct smp_chan *smp = chan->data;
Johan Hedbergf4a407be2014-02-18 21:41:34 +0200821 bool complete;
Brian Gixc8eb9692011-11-23 08:28:35 -0800822
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300823 BUG_ON(!smp);
Brian Gixc8eb9692011-11-23 08:28:35 -0800824
Johan Hedberg4a74d652014-05-20 09:45:50 +0300825 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedbergf4a407be2014-02-18 21:41:34 +0200826 mgmt_smp_complete(conn->hcon, complete);
827
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700828 kfree(smp->csrk);
829 kfree(smp->slave_csrk);
830
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300831 crypto_free_blkcipher(smp->tfm_aes);
832
Johan Hedberg759331d2014-02-28 10:10:16 +0200833 /* If pairing failed clean up any keys we might have */
834 if (!complete) {
835 if (smp->ltk) {
836 list_del(&smp->ltk->list);
837 kfree(smp->ltk);
838 }
839
840 if (smp->slave_ltk) {
841 list_del(&smp->slave_ltk->list);
842 kfree(smp->slave_ltk);
843 }
844
845 if (smp->remote_irk) {
846 list_del(&smp->remote_irk->list);
847 kfree(smp->remote_irk);
848 }
849 }
850
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300851 chan->data = NULL;
Brian Gixc8eb9692011-11-23 08:28:35 -0800852 kfree(smp);
David Herrmann76a68ba2013-04-06 20:28:37 +0200853 hci_conn_drop(conn->hcon);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300854}
855
Brian Gix2b64d152011-12-21 16:12:12 -0800856int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
857{
Johan Hedbergb10e8012014-06-27 14:23:07 +0300858 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300859 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -0800860 struct smp_chan *smp;
861 u32 value;
Brian Gix2b64d152011-12-21 16:12:12 -0800862
863 BT_DBG("");
864
Johan Hedberg642ac772014-06-27 14:23:06 +0300865 if (!conn || !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Brian Gix2b64d152011-12-21 16:12:12 -0800866 return -ENOTCONN;
867
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300868 chan = conn->smp;
869 if (!chan)
870 return -ENOTCONN;
871
872 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800873
874 switch (mgmt_op) {
875 case MGMT_OP_USER_PASSKEY_REPLY:
876 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +0200877 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800878 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +0200879 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800880 /* Fall Through */
881 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +0300882 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800883 break;
884 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
885 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +0200886 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Brian Gix2b64d152011-12-21 16:12:12 -0800887 return 0;
888 default:
Johan Hedberg84794e12013-11-06 11:24:57 +0200889 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Brian Gix2b64d152011-12-21 16:12:12 -0800890 return -EOPNOTSUPP;
891 }
892
893 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +0300894 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
895 u8 rsp = smp_confirm(smp);
896 if (rsp)
897 smp_failure(conn, rsp);
898 }
Brian Gix2b64d152011-12-21 16:12:12 -0800899
900 return 0;
901}
902
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300903static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300904{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300905 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergb3c64102014-07-10 11:02:07 +0300906 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300907 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +0300908 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300909 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300910
911 BT_DBG("conn %p", conn);
912
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200913 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300914 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200915
Johan Hedberg40bef302014-07-16 11:42:27 +0300916 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -0800917 return SMP_CMD_NOTSUPP;
918
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300919 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) {
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300920 smp = smp_chan_create(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300921 } else {
922 struct l2cap_chan *chan = conn->smp;
923 smp = chan->data;
924 }
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300925
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +0300926 if (!smp)
927 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300928
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300929 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergb3c64102014-07-10 11:02:07 +0300930 (req->auth_req & SMP_AUTH_BONDING))
931 return SMP_PAIRING_NOTSUPP;
932
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300933 smp->preq[0] = SMP_CMD_PAIRING_REQ;
934 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300935 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300936
Brian Gix2b64d152011-12-21 16:12:12 -0800937 /* We didn't start the pairing, so match remote */
Johan Hedberg1ef35822014-05-20 09:45:48 +0300938 auth = req->auth_req;
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300939
Johan Hedbergc7262e72014-06-17 13:07:37 +0300940 sec_level = authreq_to_seclevel(auth);
941 if (sec_level > conn->hcon->pending_sec_level)
942 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +0200943
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300944 /* If we need MITM check that it can be acheived */
945 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
946 u8 method;
947
948 method = get_auth_method(smp, conn->hcon->io_capability,
949 req->io_capability);
950 if (method == JUST_WORKS || method == JUST_CFM)
951 return SMP_AUTH_REQUIREMENTS;
952 }
953
Brian Gix2b64d152011-12-21 16:12:12 -0800954 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300955
956 key_size = min(req->max_key_size, rsp.max_key_size);
957 if (check_enc_key_size(conn, key_size))
958 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300959
Johan Hedberge84a6b12013-12-02 10:49:03 +0200960 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300961
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300962 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
963 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -0300964
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300965 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300966
Brian Gix2b64d152011-12-21 16:12:12 -0800967 /* Request setup of TK */
968 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
969 if (ret)
970 return SMP_UNSPECIFIED;
971
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300972 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300973}
974
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300975static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300976{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300977 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300978 struct l2cap_chan *chan = conn->smp;
979 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800980 u8 key_size, auth = SMP_AUTH_NONE;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -0300981 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300982
983 BT_DBG("conn %p", conn);
984
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200985 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300986 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200987
Johan Hedberg40bef302014-07-16 11:42:27 +0300988 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800989 return SMP_CMD_NOTSUPP;
990
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300991 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300992
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300993 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300994
995 key_size = min(req->max_key_size, rsp->max_key_size);
996 if (check_enc_key_size(conn, key_size))
997 return SMP_ENC_KEY_SIZE;
998
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300999 /* If we need MITM check that it can be acheived */
1000 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1001 u8 method;
1002
1003 method = get_auth_method(smp, req->io_capability,
1004 rsp->io_capability);
1005 if (method == JUST_WORKS || method == JUST_CFM)
1006 return SMP_AUTH_REQUIREMENTS;
1007 }
1008
Johan Hedberge84a6b12013-12-02 10:49:03 +02001009 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001010
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001011 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1012 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001013
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001014 /* Update remote key distribution in case the remote cleared
1015 * some bits that we had enabled in our request.
1016 */
1017 smp->remote_key_dist &= rsp->resp_key_dist;
1018
Brian Gix2b64d152011-12-21 16:12:12 -08001019 if ((req->auth_req & SMP_AUTH_BONDING) &&
Marcel Holtmannf1560462013-10-13 05:43:25 -07001020 (rsp->auth_req & SMP_AUTH_BONDING))
Brian Gix2b64d152011-12-21 16:12:12 -08001021 auth = SMP_AUTH_BONDING;
1022
1023 auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM;
1024
Johan Hedberg476585e2012-06-06 18:54:15 +08001025 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001026 if (ret)
1027 return SMP_UNSPECIFIED;
1028
Johan Hedberg4a74d652014-05-20 09:45:50 +03001029 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001030
1031 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001032 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001033 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001034
1035 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001036}
1037
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001038static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001039{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001040 struct l2cap_chan *chan = conn->smp;
1041 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001042
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001043 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1044
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001045 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001046 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001047
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001048 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1049 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001050
Johan Hedberg943a7322014-03-18 12:58:24 +02001051 if (conn->hcon->out)
1052 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1053 smp->prnd);
Johan Hedberg4a74d652014-05-20 09:45:50 +03001054 else if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001055 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001056 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001057 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001058
1059 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001060}
1061
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001062static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001063{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001064 struct l2cap_chan *chan = conn->smp;
1065 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001066
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001067 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001068
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001069 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001070 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001071
Johan Hedberg943a7322014-03-18 12:58:24 +02001072 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001073 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001074
Johan Hedberg861580a2014-05-20 09:45:51 +03001075 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001076}
1077
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001078static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001079{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001080 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001081 struct hci_conn *hcon = conn->hcon;
1082
Johan Hedberg98a0b842014-01-30 19:40:00 -08001083 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001084 hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001085 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001086 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001087
Johan Hedberg4dab7862012-06-07 14:58:37 +08001088 if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001089 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001090
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001091 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001092 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001093
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001094 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1095 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001096
Johan Hedbergfe59a052014-07-01 19:14:12 +03001097 /* We never store STKs for master role, so clear this flag */
1098 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1099
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001100 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001101}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001102
Johan Hedberg854f4722014-07-01 18:40:20 +03001103bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level)
1104{
1105 if (sec_level == BT_SECURITY_LOW)
1106 return true;
1107
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001108 /* If we're encrypted with an STK always claim insufficient
1109 * security. This way we allow the connection to be re-encrypted
1110 * with an LTK, even if the LTK provides the same level of
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001111 * security. Only exception is if we don't have an LTK (e.g.
1112 * because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001113 */
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001114 if (test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
1115 hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001116 hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001117 return false;
1118
Johan Hedberg854f4722014-07-01 18:40:20 +03001119 if (hcon->sec_level >= sec_level)
1120 return true;
1121
1122 return false;
1123}
1124
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001125static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001126{
1127 struct smp_cmd_security_req *rp = (void *) skb->data;
1128 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001129 struct hci_conn *hcon = conn->hcon;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001130 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001131 u8 sec_level;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001132
1133 BT_DBG("conn %p", conn);
1134
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001135 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001136 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001137
Johan Hedberg40bef302014-07-16 11:42:27 +03001138 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001139 return SMP_CMD_NOTSUPP;
1140
Johan Hedbergc7262e72014-06-17 13:07:37 +03001141 sec_level = authreq_to_seclevel(rp->auth_req);
Johan Hedberg854f4722014-07-01 18:40:20 +03001142 if (smp_sufficient_security(hcon, sec_level))
1143 return 0;
1144
Johan Hedbergc7262e72014-06-17 13:07:37 +03001145 if (sec_level > hcon->pending_sec_level)
1146 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001147
Johan Hedberg4dab7862012-06-07 14:58:37 +08001148 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001149 return 0;
1150
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001151 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001152 return 0;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001153
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001154 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001155 if (!smp)
1156 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001157
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001158 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedberg616d55b2014-07-29 14:18:48 +03001159 (rp->auth_req & SMP_AUTH_BONDING))
1160 return SMP_PAIRING_NOTSUPP;
1161
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001162 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001163
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001164 memset(&cp, 0, sizeof(cp));
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -03001165 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001166
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001167 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1168 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001169
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001170 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001171
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001172 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001173}
1174
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001175int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001176{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001177 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001178 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001179 __u8 authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001180
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001181 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1182
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001183 /* This may be NULL if there's an unexpected disconnection */
1184 if (!conn)
1185 return 1;
1186
Johan Hedberg757aee02013-04-24 13:05:32 +03001187 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001188 return 1;
1189
Johan Hedbergad32a2f2013-05-14 18:05:12 +03001190 if (smp_sufficient_security(hcon, sec_level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001191 return 1;
1192
Johan Hedbergc7262e72014-06-17 13:07:37 +03001193 if (sec_level > hcon->pending_sec_level)
1194 hcon->pending_sec_level = sec_level;
1195
Johan Hedberg40bef302014-07-16 11:42:27 +03001196 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001197 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1198 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001199
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001200 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001201 return 0;
1202
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001203 smp = smp_chan_create(conn);
Brian Gix2b64d152011-12-21 16:12:12 -08001204 if (!smp)
1205 return 1;
1206
1207 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001208
Johan Hedberg79897d22014-06-01 09:45:24 +03001209 /* Require MITM if IO Capability allows or the security level
1210 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001211 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001212 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001213 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001214 authreq |= SMP_AUTH_MITM;
1215
Johan Hedberg40bef302014-07-16 11:42:27 +03001216 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001217 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001218
Brian Gix2b64d152011-12-21 16:12:12 -08001219 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001220 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1221 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001222
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001223 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
1224 } else {
1225 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001226 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001227 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
1228 }
1229
Johan Hedberg4a74d652014-05-20 09:45:50 +03001230 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergedca7922014-03-24 15:54:11 +02001231
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001232 return 0;
1233}
1234
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001235static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1236{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001237 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001238 struct l2cap_chan *chan = conn->smp;
1239 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001240
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001241 BT_DBG("conn %p", conn);
1242
1243 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001244 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001245
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001246 /* Ignore this PDU if it wasn't requested */
1247 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
1248 return 0;
1249
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001250 skb_pull(skb, sizeof(*rp));
1251
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001252 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001253
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001254 return 0;
1255}
1256
1257static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1258{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001259 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001260 struct l2cap_chan *chan = conn->smp;
1261 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001262 struct hci_dev *hdev = conn->hcon->hdev;
1263 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001264 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001265 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001266
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001267 BT_DBG("conn %p", conn);
1268
1269 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001270 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001271
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001272 /* Ignore this PDU if it wasn't requested */
1273 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
1274 return 0;
1275
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001276 /* Mark the information as received */
1277 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1278
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001279 skb_pull(skb, sizeof(*rp));
1280
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001281 hci_dev_lock(hdev);
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001282 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001283 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001284 authenticated, smp->tk, smp->enc_key_size,
1285 rp->ediv, rp->rand);
1286 smp->ltk = ltk;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001287 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
Johan Hedberg4bd6d382014-02-26 23:33:45 +02001288 smp_distribute_keys(conn);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001289 hci_dev_unlock(hdev);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001290
1291 return 0;
1292}
1293
Johan Hedbergfd349c02014-02-18 10:19:36 +02001294static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1295{
1296 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001297 struct l2cap_chan *chan = conn->smp;
1298 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001299
1300 BT_DBG("");
1301
1302 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001303 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001304
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001305 /* Ignore this PDU if it wasn't requested */
1306 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1307 return 0;
1308
Johan Hedbergfd349c02014-02-18 10:19:36 +02001309 skb_pull(skb, sizeof(*info));
1310
1311 memcpy(smp->irk, info->irk, 16);
1312
1313 return 0;
1314}
1315
1316static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1317 struct sk_buff *skb)
1318{
1319 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001320 struct l2cap_chan *chan = conn->smp;
1321 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001322 struct hci_conn *hcon = conn->hcon;
1323 bdaddr_t rpa;
1324
1325 BT_DBG("");
1326
1327 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001328 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001329
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001330 /* Ignore this PDU if it wasn't requested */
1331 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1332 return 0;
1333
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001334 /* Mark the information as received */
1335 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1336
Johan Hedbergfd349c02014-02-18 10:19:36 +02001337 skb_pull(skb, sizeof(*info));
1338
Johan Hedberg31dd6242014-06-27 14:23:02 +03001339 hci_dev_lock(hcon->hdev);
1340
Johan Hedberga9a58f82014-02-25 22:24:37 +02001341 /* Strictly speaking the Core Specification (4.1) allows sending
1342 * an empty address which would force us to rely on just the IRK
1343 * as "identity information". However, since such
1344 * implementations are not known of and in order to not over
1345 * complicate our implementation, simply pretend that we never
1346 * received an IRK for such a device.
1347 */
1348 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1349 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001350 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001351 }
1352
Johan Hedbergfd349c02014-02-18 10:19:36 +02001353 bacpy(&smp->id_addr, &info->bdaddr);
1354 smp->id_addr_type = info->addr_type;
1355
1356 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1357 bacpy(&rpa, &hcon->dst);
1358 else
1359 bacpy(&rpa, BDADDR_ANY);
1360
Johan Hedberg23d0e122014-02-19 14:57:46 +02001361 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1362 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001363
Johan Hedberg31dd6242014-06-27 14:23:02 +03001364distribute:
Johan Hedberg4bd6d382014-02-26 23:33:45 +02001365 smp_distribute_keys(conn);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001366
Johan Hedberg31dd6242014-06-27 14:23:02 +03001367 hci_dev_unlock(hcon->hdev);
1368
Johan Hedbergfd349c02014-02-18 10:19:36 +02001369 return 0;
1370}
1371
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001372static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1373{
1374 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001375 struct l2cap_chan *chan = conn->smp;
1376 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001377 struct hci_dev *hdev = conn->hcon->hdev;
1378 struct smp_csrk *csrk;
1379
1380 BT_DBG("conn %p", conn);
1381
1382 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001383 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001384
1385 /* Ignore this PDU if it wasn't requested */
1386 if (!(smp->remote_key_dist & SMP_DIST_SIGN))
1387 return 0;
1388
1389 /* Mark the information as received */
1390 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1391
1392 skb_pull(skb, sizeof(*rp));
1393
1394 hci_dev_lock(hdev);
1395 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1396 if (csrk) {
1397 csrk->master = 0x01;
1398 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1399 }
1400 smp->csrk = csrk;
Johan Hedberg5fcb9342014-08-07 10:03:31 +03001401 smp_distribute_keys(conn);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001402 hci_dev_unlock(hdev);
1403
1404 return 0;
1405}
1406
Johan Hedberg4befb862014-08-11 22:06:38 +03001407static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001408{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001409 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001410 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001411 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001412 int err = 0;
1413
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001414 if (hcon->type != LE_LINK) {
1415 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001416 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001417 }
1418
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001419 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001420 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001421
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001422 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Johan Hedbergbeb19e42014-07-18 11:15:26 +03001423 err = -EOPNOTSUPP;
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001424 reason = SMP_PAIRING_NOTSUPP;
1425 goto done;
1426 }
1427
Marcel Holtmann92381f52013-10-03 01:23:08 -07001428 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001429 skb_pull(skb, sizeof(code));
1430
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001431 /*
1432 * The SMP context must be initialized for all other PDUs except
1433 * pairing and security requests. If we get any other PDU when
1434 * not initialized simply disconnect (done if this function
1435 * returns an error).
1436 */
1437 if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ &&
Johan Hedbergd3368602014-08-08 09:28:05 +03001438 !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) {
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001439 BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001440 reason = SMP_CMD_NOTSUPP;
1441 err = -EOPNOTSUPP;
1442 goto done;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001443 }
1444
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001445 switch (code) {
1446 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001447 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001448 break;
1449
1450 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001451 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001452 reason = 0;
1453 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001454 break;
1455
1456 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001457 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001458 break;
1459
1460 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001461 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001462 break;
1463
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001464 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001465 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001466 break;
1467
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001468 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001469 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001470 break;
1471
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001472 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001473 reason = smp_cmd_encrypt_info(conn, skb);
1474 break;
1475
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001476 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001477 reason = smp_cmd_master_ident(conn, skb);
1478 break;
1479
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001480 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001481 reason = smp_cmd_ident_info(conn, skb);
1482 break;
1483
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001484 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001485 reason = smp_cmd_ident_addr_info(conn, skb);
1486 break;
1487
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001488 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001489 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001490 break;
1491
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001492 default:
1493 BT_DBG("Unknown command code 0x%2.2x", code);
1494
1495 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001496 err = -EOPNOTSUPP;
1497 goto done;
1498 }
1499
1500done:
1501 if (reason)
Johan Hedberg84794e12013-11-06 11:24:57 +02001502 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001503 if (!err)
1504 kfree_skb(skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001505 return err;
1506}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001507
Johan Hedberg70db83c2014-08-08 09:37:16 +03001508static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1509{
1510 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001511 struct smp_chan *smp = chan->data;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001512
1513 BT_DBG("chan %p", chan);
1514
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001515 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001516 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001517 smp_chan_destroy(conn);
1518 }
1519
Johan Hedberg70db83c2014-08-08 09:37:16 +03001520 conn->smp = NULL;
1521 l2cap_chan_put(chan);
1522}
1523
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001524static void smp_resume_cb(struct l2cap_chan *chan)
1525{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001526 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001527 struct l2cap_conn *conn = chan->conn;
1528 struct hci_conn *hcon = conn->hcon;
1529
1530 BT_DBG("chan %p", chan);
1531
1532 if (test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1533 smp_distribute_keys(conn);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001534
1535 if (smp)
1536 cancel_delayed_work(&smp->security_timer);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001537}
1538
Johan Hedberg70db83c2014-08-08 09:37:16 +03001539static void smp_ready_cb(struct l2cap_chan *chan)
1540{
1541 struct l2cap_conn *conn = chan->conn;
1542
1543 BT_DBG("chan %p", chan);
1544
1545 conn->smp = chan;
1546 l2cap_chan_hold(chan);
1547}
1548
Johan Hedberg4befb862014-08-11 22:06:38 +03001549static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1550{
1551 int err;
1552
1553 BT_DBG("chan %p", chan);
1554
1555 err = smp_sig_channel(chan, skb);
1556 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001557 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001558
Johan Hedbergb68fda62014-08-11 22:06:40 +03001559 if (smp)
1560 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001561
1562 l2cap_conn_shutdown(chan->conn, -err);
1563 }
1564
1565 return err;
1566}
1567
Johan Hedberg70db83c2014-08-08 09:37:16 +03001568static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1569 unsigned long hdr_len,
1570 unsigned long len, int nb)
1571{
1572 struct sk_buff *skb;
1573
1574 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1575 if (!skb)
1576 return ERR_PTR(-ENOMEM);
1577
1578 skb->priority = HCI_PRIO_MAX;
1579 bt_cb(skb)->chan = chan;
1580
1581 return skb;
1582}
1583
1584static const struct l2cap_ops smp_chan_ops = {
1585 .name = "Security Manager",
1586 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001587 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001588 .alloc_skb = smp_alloc_skb_cb,
1589 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001590 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001591
1592 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001593 .state_change = l2cap_chan_no_state_change,
1594 .close = l2cap_chan_no_close,
1595 .defer = l2cap_chan_no_defer,
1596 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001597 .set_shutdown = l2cap_chan_no_set_shutdown,
1598 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1599 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1600};
1601
1602static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1603{
1604 struct l2cap_chan *chan;
1605
1606 BT_DBG("pchan %p", pchan);
1607
1608 chan = l2cap_chan_create();
1609 if (!chan)
1610 return NULL;
1611
1612 chan->chan_type = pchan->chan_type;
1613 chan->ops = &smp_chan_ops;
1614 chan->scid = pchan->scid;
1615 chan->dcid = chan->scid;
1616 chan->imtu = pchan->imtu;
1617 chan->omtu = pchan->omtu;
1618 chan->mode = pchan->mode;
1619
1620 BT_DBG("created chan %p", chan);
1621
1622 return chan;
1623}
1624
1625static const struct l2cap_ops smp_root_chan_ops = {
1626 .name = "Security Manager Root",
1627 .new_connection = smp_new_conn_cb,
1628
1629 /* None of these are implemented for the root channel */
1630 .close = l2cap_chan_no_close,
1631 .alloc_skb = l2cap_chan_no_alloc_skb,
1632 .recv = l2cap_chan_no_recv,
1633 .state_change = l2cap_chan_no_state_change,
1634 .teardown = l2cap_chan_no_teardown,
1635 .ready = l2cap_chan_no_ready,
1636 .defer = l2cap_chan_no_defer,
1637 .suspend = l2cap_chan_no_suspend,
1638 .resume = l2cap_chan_no_resume,
1639 .set_shutdown = l2cap_chan_no_set_shutdown,
1640 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
1641 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1642};
1643
Johan Hedberg711eafe2014-08-08 09:32:52 +03001644int smp_register(struct hci_dev *hdev)
1645{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001646 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001647 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001648
Johan Hedberg711eafe2014-08-08 09:32:52 +03001649 BT_DBG("%s", hdev->name);
1650
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001651 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1652 if (IS_ERR(tfm_aes)) {
1653 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001654 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03001655 return err;
1656 }
1657
Johan Hedberg70db83c2014-08-08 09:37:16 +03001658 chan = l2cap_chan_create();
1659 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001660 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001661 return -ENOMEM;
1662 }
1663
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001664 chan->data = tfm_aes;
1665
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001666 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001667
1668 l2cap_chan_set_defaults(chan);
1669
1670 bacpy(&chan->src, &hdev->bdaddr);
1671 chan->src_type = BDADDR_LE_PUBLIC;
1672 chan->state = BT_LISTEN;
1673 chan->mode = L2CAP_MODE_BASIC;
1674 chan->imtu = L2CAP_DEFAULT_MTU;
1675 chan->ops = &smp_root_chan_ops;
1676
1677 hdev->smp_data = chan;
1678
Johan Hedberg711eafe2014-08-08 09:32:52 +03001679 return 0;
1680}
1681
1682void smp_unregister(struct hci_dev *hdev)
1683{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001684 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001685 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001686
1687 if (!chan)
1688 return;
1689
1690 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001691
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001692 tfm_aes = chan->data;
1693 if (tfm_aes) {
1694 chan->data = NULL;
1695 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001696 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03001697
1698 hdev->smp_data = NULL;
1699 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001700}