blob: cdae40869447a815e036a33b44037e3a82558291 [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
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700387 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300388 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800389}
390
Brian Gix2b64d152011-12-21 16:12:12 -0800391#define JUST_WORKS 0x00
392#define JUST_CFM 0x01
393#define REQ_PASSKEY 0x02
394#define CFM_PASSKEY 0x03
395#define REQ_OOB 0x04
396#define OVERLAP 0xFF
397
398static const u8 gen_method[5][5] = {
399 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
400 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
401 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
402 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
403 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
404};
405
Johan Hedberg581370c2014-06-17 13:07:38 +0300406static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
407{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300408 /* If either side has unknown io_caps, use JUST_CFM (which gets
409 * converted later to JUST_WORKS if we're initiators.
410 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300411 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
412 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300413 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300414
415 return gen_method[remote_io][local_io];
416}
417
Brian Gix2b64d152011-12-21 16:12:12 -0800418static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
419 u8 local_io, u8 remote_io)
420{
421 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300422 struct l2cap_chan *chan = conn->smp;
423 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800424 u8 method;
425 u32 passkey = 0;
426 int ret = 0;
427
428 /* Initialize key for JUST WORKS */
429 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300430 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800431
432 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
433
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300434 /* If neither side wants MITM, either "just" confirm an incoming
435 * request or use just-works for outgoing ones. The JUST_CFM
436 * will be converted to JUST_WORKS if necessary later in this
437 * function. If either side has MITM look up the method from the
438 * table.
439 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300440 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300441 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800442 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300443 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800444
Johan Hedberga82505c2014-03-24 14:39:07 +0200445 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300446 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200447 method = JUST_WORKS;
448
Johan Hedberg02f3e252014-07-16 15:09:13 +0300449 /* Don't bother user space with no IO capabilities */
450 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
451 method = JUST_WORKS;
452
Brian Gix2b64d152011-12-21 16:12:12 -0800453 /* If Just Works, Continue with Zero TK */
454 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300455 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800456 return 0;
457 }
458
459 /* Not Just Works/Confirm results in MITM Authentication */
460 if (method != JUST_CFM)
Johan Hedberg4a74d652014-05-20 09:45:50 +0300461 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800462
463 /* If both devices have Keyoard-Display I/O, the master
464 * Confirms and the slave Enters the passkey.
465 */
466 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300467 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800468 method = CFM_PASSKEY;
469 else
470 method = REQ_PASSKEY;
471 }
472
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200473 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800474 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200475 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800476 get_random_bytes(&passkey, sizeof(passkey));
477 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200478 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800479 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300480 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800481 }
482
483 hci_dev_lock(hcon->hdev);
484
485 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700486 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200487 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200488 else if (method == JUST_CFM)
489 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
490 hcon->type, hcon->dst_type,
491 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800492 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200493 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200494 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200495 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800496
497 hci_dev_unlock(hcon->hdev);
498
499 return ret;
500}
501
Johan Hedberg1cc61142014-05-20 09:45:52 +0300502static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300503{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300504 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300505 struct smp_cmd_pairing_confirm cp;
506 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300507
508 BT_DBG("conn %p", conn);
509
Johan Hedbergec70f362014-06-27 14:23:04 +0300510 ret = smp_c1(smp, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200511 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200512 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
513 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300514 if (ret)
515 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300516
Johan Hedberg4a74d652014-05-20 09:45:50 +0300517 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800518
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300519 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
520
Johan Hedberg1cc61142014-05-20 09:45:52 +0300521 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300522}
523
Johan Hedberg861580a2014-05-20 09:45:51 +0300524static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300525{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300526 struct l2cap_conn *conn = smp->conn;
527 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300528 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300529 int ret;
530
Johan Hedbergec70f362014-06-27 14:23:04 +0300531 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300532 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300533
534 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
535
Johan Hedbergec70f362014-06-27 14:23:04 +0300536 ret = smp_c1(smp, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200537 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200538 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300539 if (ret)
540 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300541
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300542 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
543 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300544 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300545 }
546
547 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800548 u8 stk[16];
549 __le64 rand = 0;
550 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300551
Johan Hedbergec70f362014-06-27 14:23:04 +0300552 smp_s1(smp, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300553
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300554 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300555 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300556
Johan Hedberg861580a2014-05-20 09:45:51 +0300557 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
558 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300559
560 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300561 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300562 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300563 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300564 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800565 __le64 rand = 0;
566 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300567
Johan Hedberg943a7322014-03-18 12:58:24 +0200568 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
569 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300570
Johan Hedbergec70f362014-06-27 14:23:04 +0300571 smp_s1(smp, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300572
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300573 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700574 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300575
Johan Hedbergfff34902014-06-10 15:19:50 +0300576 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
577 auth = 1;
578 else
579 auth = 0;
580
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300581 /* Even though there's no _SLAVE suffix this is the
582 * slave STK we're adding for later lookup (the master
583 * STK never needs to be stored).
584 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700585 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300586 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300587 }
588
Johan Hedberg861580a2014-05-20 09:45:51 +0300589 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300590}
591
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300592static void smp_notify_keys(struct l2cap_conn *conn)
593{
594 struct l2cap_chan *chan = conn->smp;
595 struct smp_chan *smp = chan->data;
596 struct hci_conn *hcon = conn->hcon;
597 struct hci_dev *hdev = hcon->hdev;
598 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
599 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
600 bool persistent;
601
602 if (smp->remote_irk) {
603 mgmt_new_irk(hdev, smp->remote_irk);
604 /* Now that user space can be considered to know the
605 * identity address track the connection based on it
606 * from now on.
607 */
608 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
609 hcon->dst_type = smp->remote_irk->addr_type;
610 l2cap_conn_update_id_addr(hcon);
611
612 /* When receiving an indentity resolving key for
613 * a remote device that does not use a resolvable
614 * private address, just remove the key so that
615 * it is possible to use the controller white
616 * list for scanning.
617 *
618 * Userspace will have been told to not store
619 * this key at this point. So it is safe to
620 * just remove it.
621 */
622 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
623 list_del(&smp->remote_irk->list);
624 kfree(smp->remote_irk);
625 smp->remote_irk = NULL;
626 }
627 }
628
629 /* The LTKs and CSRKs should be persistent only if both sides
630 * had the bonding bit set in their authentication requests.
631 */
632 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
633
634 if (smp->csrk) {
635 smp->csrk->bdaddr_type = hcon->dst_type;
636 bacpy(&smp->csrk->bdaddr, &hcon->dst);
637 mgmt_new_csrk(hdev, smp->csrk, persistent);
638 }
639
640 if (smp->slave_csrk) {
641 smp->slave_csrk->bdaddr_type = hcon->dst_type;
642 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
643 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
644 }
645
646 if (smp->ltk) {
647 smp->ltk->bdaddr_type = hcon->dst_type;
648 bacpy(&smp->ltk->bdaddr, &hcon->dst);
649 mgmt_new_ltk(hdev, smp->ltk, persistent);
650 }
651
652 if (smp->slave_ltk) {
653 smp->slave_ltk->bdaddr_type = hcon->dst_type;
654 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
655 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
656 }
657}
658
659static int smp_distribute_keys(struct l2cap_conn *conn)
660{
661 struct smp_cmd_pairing *req, *rsp;
662 struct l2cap_chan *chan = conn->smp;
663 struct smp_chan *smp = chan->data;
664 struct hci_conn *hcon = conn->hcon;
665 struct hci_dev *hdev = hcon->hdev;
666 __u8 *keydist;
667
668 BT_DBG("conn %p", conn);
669
670 if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
671 return 0;
672
673 rsp = (void *) &smp->prsp[1];
674
675 /* The responder sends its keys first */
676 if (hcon->out && (smp->remote_key_dist & 0x07))
677 return 0;
678
679 req = (void *) &smp->preq[1];
680
681 if (hcon->out) {
682 keydist = &rsp->init_key_dist;
683 *keydist &= req->init_key_dist;
684 } else {
685 keydist = &rsp->resp_key_dist;
686 *keydist &= req->resp_key_dist;
687 }
688
689 BT_DBG("keydist 0x%x", *keydist);
690
691 if (*keydist & SMP_DIST_ENC_KEY) {
692 struct smp_cmd_encrypt_info enc;
693 struct smp_cmd_master_ident ident;
694 struct smp_ltk *ltk;
695 u8 authenticated;
696 __le16 ediv;
697 __le64 rand;
698
699 get_random_bytes(enc.ltk, sizeof(enc.ltk));
700 get_random_bytes(&ediv, sizeof(ediv));
701 get_random_bytes(&rand, sizeof(rand));
702
703 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
704
705 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
706 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
707 SMP_LTK_SLAVE, authenticated, enc.ltk,
708 smp->enc_key_size, ediv, rand);
709 smp->slave_ltk = ltk;
710
711 ident.ediv = ediv;
712 ident.rand = rand;
713
714 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
715
716 *keydist &= ~SMP_DIST_ENC_KEY;
717 }
718
719 if (*keydist & SMP_DIST_ID_KEY) {
720 struct smp_cmd_ident_addr_info addrinfo;
721 struct smp_cmd_ident_info idinfo;
722
723 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
724
725 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
726
727 /* The hci_conn contains the local identity address
728 * after the connection has been established.
729 *
730 * This is true even when the connection has been
731 * established using a resolvable random address.
732 */
733 bacpy(&addrinfo.bdaddr, &hcon->src);
734 addrinfo.addr_type = hcon->src_type;
735
736 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
737 &addrinfo);
738
739 *keydist &= ~SMP_DIST_ID_KEY;
740 }
741
742 if (*keydist & SMP_DIST_SIGN) {
743 struct smp_cmd_sign_info sign;
744 struct smp_csrk *csrk;
745
746 /* Generate a new random key */
747 get_random_bytes(sign.csrk, sizeof(sign.csrk));
748
749 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
750 if (csrk) {
751 csrk->master = 0x00;
752 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
753 }
754 smp->slave_csrk = csrk;
755
756 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
757
758 *keydist &= ~SMP_DIST_SIGN;
759 }
760
761 /* If there are still keys to be received wait for them */
762 if ((smp->remote_key_dist & 0x07))
763 return 0;
764
765 clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300766 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
767 smp_notify_keys(conn);
768
769 smp_chan_destroy(conn);
770
771 return 0;
772}
773
Johan Hedbergb68fda62014-08-11 22:06:40 +0300774static void smp_timeout(struct work_struct *work)
775{
776 struct smp_chan *smp = container_of(work, struct smp_chan,
777 security_timer.work);
778 struct l2cap_conn *conn = smp->conn;
779
780 BT_DBG("conn %p", conn);
781
782 l2cap_conn_shutdown(conn, ETIMEDOUT);
783}
784
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300785static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
786{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300787 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300788 struct smp_chan *smp;
789
Marcel Holtmannf1560462013-10-13 05:43:25 -0700790 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedberg616d55b2014-07-29 14:18:48 +0300791 if (!smp) {
792 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300793 return NULL;
Johan Hedberg616d55b2014-07-29 14:18:48 +0300794 }
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300795
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300796 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
797 if (IS_ERR(smp->tfm_aes)) {
798 BT_ERR("Unable to create ECB crypto context");
799 kfree(smp);
Johan Hedberg616d55b2014-07-29 14:18:48 +0300800 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300801 return NULL;
802 }
803
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300804 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300805 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300806
Johan Hedbergb68fda62014-08-11 22:06:40 +0300807 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
808
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300809 hci_conn_hold(conn->hcon);
810
811 return smp;
812}
813
814void smp_chan_destroy(struct l2cap_conn *conn)
815{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300816 struct l2cap_chan *chan = conn->smp;
817 struct smp_chan *smp = chan->data;
Johan Hedbergf4a407be2014-02-18 21:41:34 +0200818 bool complete;
Brian Gixc8eb9692011-11-23 08:28:35 -0800819
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300820 BUG_ON(!smp);
Brian Gixc8eb9692011-11-23 08:28:35 -0800821
Johan Hedberg109ec232014-08-11 22:06:42 +0300822 cancel_delayed_work_sync(&smp->security_timer);
823 /* In case the timeout freed the SMP context */
824 if (!chan->data)
825 return;
826
Johan Hedberg4a74d652014-05-20 09:45:50 +0300827 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedbergf4a407be2014-02-18 21:41:34 +0200828 mgmt_smp_complete(conn->hcon, complete);
829
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700830 kfree(smp->csrk);
831 kfree(smp->slave_csrk);
832
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300833 crypto_free_blkcipher(smp->tfm_aes);
834
Johan Hedberg759331d2014-02-28 10:10:16 +0200835 /* If pairing failed clean up any keys we might have */
836 if (!complete) {
837 if (smp->ltk) {
838 list_del(&smp->ltk->list);
839 kfree(smp->ltk);
840 }
841
842 if (smp->slave_ltk) {
843 list_del(&smp->slave_ltk->list);
844 kfree(smp->slave_ltk);
845 }
846
847 if (smp->remote_irk) {
848 list_del(&smp->remote_irk->list);
849 kfree(smp->remote_irk);
850 }
851 }
852
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300853 chan->data = NULL;
Brian Gixc8eb9692011-11-23 08:28:35 -0800854 kfree(smp);
David Herrmann76a68ba2013-04-06 20:28:37 +0200855 hci_conn_drop(conn->hcon);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300856}
857
Brian Gix2b64d152011-12-21 16:12:12 -0800858int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
859{
Johan Hedbergb10e8012014-06-27 14:23:07 +0300860 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300861 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -0800862 struct smp_chan *smp;
863 u32 value;
Brian Gix2b64d152011-12-21 16:12:12 -0800864
865 BT_DBG("");
866
Johan Hedberg642ac772014-06-27 14:23:06 +0300867 if (!conn || !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Brian Gix2b64d152011-12-21 16:12:12 -0800868 return -ENOTCONN;
869
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300870 chan = conn->smp;
871 if (!chan)
872 return -ENOTCONN;
873
874 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800875
876 switch (mgmt_op) {
877 case MGMT_OP_USER_PASSKEY_REPLY:
878 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +0200879 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800880 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +0200881 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800882 /* Fall Through */
883 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +0300884 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800885 break;
886 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
887 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +0200888 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Brian Gix2b64d152011-12-21 16:12:12 -0800889 return 0;
890 default:
Johan Hedberg84794e12013-11-06 11:24:57 +0200891 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Brian Gix2b64d152011-12-21 16:12:12 -0800892 return -EOPNOTSUPP;
893 }
894
895 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +0300896 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
897 u8 rsp = smp_confirm(smp);
898 if (rsp)
899 smp_failure(conn, rsp);
900 }
Brian Gix2b64d152011-12-21 16:12:12 -0800901
902 return 0;
903}
904
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300905static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300906{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300907 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergb3c64102014-07-10 11:02:07 +0300908 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300909 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +0300910 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300911 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300912
913 BT_DBG("conn %p", conn);
914
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200915 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300916 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200917
Johan Hedberg40bef302014-07-16 11:42:27 +0300918 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -0800919 return SMP_CMD_NOTSUPP;
920
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300921 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) {
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300922 smp = smp_chan_create(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300923 } else {
924 struct l2cap_chan *chan = conn->smp;
925 smp = chan->data;
926 }
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300927
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +0300928 if (!smp)
929 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300930
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300931 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergb3c64102014-07-10 11:02:07 +0300932 (req->auth_req & SMP_AUTH_BONDING))
933 return SMP_PAIRING_NOTSUPP;
934
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300935 smp->preq[0] = SMP_CMD_PAIRING_REQ;
936 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300937 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300938
Brian Gix2b64d152011-12-21 16:12:12 -0800939 /* We didn't start the pairing, so match remote */
Johan Hedberg1ef35822014-05-20 09:45:48 +0300940 auth = req->auth_req;
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300941
Johan Hedbergc7262e72014-06-17 13:07:37 +0300942 sec_level = authreq_to_seclevel(auth);
943 if (sec_level > conn->hcon->pending_sec_level)
944 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +0200945
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300946 /* If we need MITM check that it can be acheived */
947 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
948 u8 method;
949
950 method = get_auth_method(smp, conn->hcon->io_capability,
951 req->io_capability);
952 if (method == JUST_WORKS || method == JUST_CFM)
953 return SMP_AUTH_REQUIREMENTS;
954 }
955
Brian Gix2b64d152011-12-21 16:12:12 -0800956 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300957
958 key_size = min(req->max_key_size, rsp.max_key_size);
959 if (check_enc_key_size(conn, key_size))
960 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300961
Johan Hedberge84a6b12013-12-02 10:49:03 +0200962 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300963
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300964 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
965 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -0300966
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300967 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300968
Brian Gix2b64d152011-12-21 16:12:12 -0800969 /* Request setup of TK */
970 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
971 if (ret)
972 return SMP_UNSPECIFIED;
973
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300974 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300975}
976
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300977static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300978{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300979 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300980 struct l2cap_chan *chan = conn->smp;
981 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800982 u8 key_size, auth = SMP_AUTH_NONE;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -0300983 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300984
985 BT_DBG("conn %p", conn);
986
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200987 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300988 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200989
Johan Hedberg40bef302014-07-16 11:42:27 +0300990 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800991 return SMP_CMD_NOTSUPP;
992
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300993 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300994
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300995 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300996
997 key_size = min(req->max_key_size, rsp->max_key_size);
998 if (check_enc_key_size(conn, key_size))
999 return SMP_ENC_KEY_SIZE;
1000
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001001 /* If we need MITM check that it can be acheived */
1002 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1003 u8 method;
1004
1005 method = get_auth_method(smp, req->io_capability,
1006 rsp->io_capability);
1007 if (method == JUST_WORKS || method == JUST_CFM)
1008 return SMP_AUTH_REQUIREMENTS;
1009 }
1010
Johan Hedberge84a6b12013-12-02 10:49:03 +02001011 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001012
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001013 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1014 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001015
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001016 /* Update remote key distribution in case the remote cleared
1017 * some bits that we had enabled in our request.
1018 */
1019 smp->remote_key_dist &= rsp->resp_key_dist;
1020
Brian Gix2b64d152011-12-21 16:12:12 -08001021 if ((req->auth_req & SMP_AUTH_BONDING) &&
Marcel Holtmannf1560462013-10-13 05:43:25 -07001022 (rsp->auth_req & SMP_AUTH_BONDING))
Brian Gix2b64d152011-12-21 16:12:12 -08001023 auth = SMP_AUTH_BONDING;
1024
1025 auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM;
1026
Johan Hedberg476585e2012-06-06 18:54:15 +08001027 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001028 if (ret)
1029 return SMP_UNSPECIFIED;
1030
Johan Hedberg4a74d652014-05-20 09:45:50 +03001031 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001032
1033 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001034 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001035 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001036
1037 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001038}
1039
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001040static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001041{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001042 struct l2cap_chan *chan = conn->smp;
1043 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001044
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001045 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1046
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001047 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001048 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001049
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001050 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1051 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001052
Johan Hedberg943a7322014-03-18 12:58:24 +02001053 if (conn->hcon->out)
1054 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1055 smp->prnd);
Johan Hedberg4a74d652014-05-20 09:45:50 +03001056 else if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001057 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001058 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001059 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001060
1061 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001062}
1063
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001064static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001065{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001066 struct l2cap_chan *chan = conn->smp;
1067 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001068
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001069 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001070
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001071 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001072 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001073
Johan Hedberg943a7322014-03-18 12:58:24 +02001074 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001075 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001076
Johan Hedberg861580a2014-05-20 09:45:51 +03001077 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001078}
1079
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001080static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001081{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001082 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001083 struct hci_conn *hcon = conn->hcon;
1084
Johan Hedberg98a0b842014-01-30 19:40:00 -08001085 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001086 hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001087 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001088 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001089
Johan Hedberg4dab7862012-06-07 14:58:37 +08001090 if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001091 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001092
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001093 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001094 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001095
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001096 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1097 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001098
Johan Hedbergfe59a052014-07-01 19:14:12 +03001099 /* We never store STKs for master role, so clear this flag */
1100 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1101
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001102 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001103}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001104
Johan Hedberg854f4722014-07-01 18:40:20 +03001105bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level)
1106{
1107 if (sec_level == BT_SECURITY_LOW)
1108 return true;
1109
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001110 /* If we're encrypted with an STK always claim insufficient
1111 * security. This way we allow the connection to be re-encrypted
1112 * with an LTK, even if the LTK provides the same level of
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001113 * security. Only exception is if we don't have an LTK (e.g.
1114 * because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001115 */
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001116 if (test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
1117 hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001118 hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001119 return false;
1120
Johan Hedberg854f4722014-07-01 18:40:20 +03001121 if (hcon->sec_level >= sec_level)
1122 return true;
1123
1124 return false;
1125}
1126
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001127static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001128{
1129 struct smp_cmd_security_req *rp = (void *) skb->data;
1130 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001131 struct hci_conn *hcon = conn->hcon;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001132 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001133 u8 sec_level;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001134
1135 BT_DBG("conn %p", conn);
1136
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001137 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001138 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001139
Johan Hedberg40bef302014-07-16 11:42:27 +03001140 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001141 return SMP_CMD_NOTSUPP;
1142
Johan Hedbergc7262e72014-06-17 13:07:37 +03001143 sec_level = authreq_to_seclevel(rp->auth_req);
Johan Hedberg854f4722014-07-01 18:40:20 +03001144 if (smp_sufficient_security(hcon, sec_level))
1145 return 0;
1146
Johan Hedbergc7262e72014-06-17 13:07:37 +03001147 if (sec_level > hcon->pending_sec_level)
1148 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001149
Johan Hedberg4dab7862012-06-07 14:58:37 +08001150 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001151 return 0;
1152
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001153 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001154 return 0;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001155
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001156 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001157 if (!smp)
1158 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001159
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001160 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedberg616d55b2014-07-29 14:18:48 +03001161 (rp->auth_req & SMP_AUTH_BONDING))
1162 return SMP_PAIRING_NOTSUPP;
1163
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001164 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001165
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001166 memset(&cp, 0, sizeof(cp));
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -03001167 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001168
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001169 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1170 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001171
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001172 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001173
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001174 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001175}
1176
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001177int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001178{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001179 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001180 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001181 __u8 authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001182
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001183 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1184
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001185 /* This may be NULL if there's an unexpected disconnection */
1186 if (!conn)
1187 return 1;
1188
Johan Hedberg757aee02013-04-24 13:05:32 +03001189 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001190 return 1;
1191
Johan Hedbergad32a2f2013-05-14 18:05:12 +03001192 if (smp_sufficient_security(hcon, sec_level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001193 return 1;
1194
Johan Hedbergc7262e72014-06-17 13:07:37 +03001195 if (sec_level > hcon->pending_sec_level)
1196 hcon->pending_sec_level = sec_level;
1197
Johan Hedberg40bef302014-07-16 11:42:27 +03001198 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001199 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1200 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001201
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001202 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001203 return 0;
1204
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001205 smp = smp_chan_create(conn);
Brian Gix2b64d152011-12-21 16:12:12 -08001206 if (!smp)
1207 return 1;
1208
1209 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001210
Johan Hedberg79897d22014-06-01 09:45:24 +03001211 /* Require MITM if IO Capability allows or the security level
1212 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001213 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001214 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001215 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001216 authreq |= SMP_AUTH_MITM;
1217
Johan Hedberg40bef302014-07-16 11:42:27 +03001218 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001219 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001220
Brian Gix2b64d152011-12-21 16:12:12 -08001221 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001222 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1223 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001224
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001225 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
1226 } else {
1227 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001228 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001229 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
1230 }
1231
Johan Hedberg4a74d652014-05-20 09:45:50 +03001232 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergedca7922014-03-24 15:54:11 +02001233
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001234 return 0;
1235}
1236
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001237static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1238{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001239 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001240 struct l2cap_chan *chan = conn->smp;
1241 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001242
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001243 BT_DBG("conn %p", conn);
1244
1245 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001246 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001247
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001248 /* Ignore this PDU if it wasn't requested */
1249 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
1250 return 0;
1251
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001252 skb_pull(skb, sizeof(*rp));
1253
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001254 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001255
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001256 return 0;
1257}
1258
1259static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1260{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001261 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001262 struct l2cap_chan *chan = conn->smp;
1263 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001264 struct hci_dev *hdev = conn->hcon->hdev;
1265 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001266 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001267 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001268
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001269 BT_DBG("conn %p", conn);
1270
1271 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001272 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001273
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001274 /* Ignore this PDU if it wasn't requested */
1275 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
1276 return 0;
1277
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001278 /* Mark the information as received */
1279 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1280
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001281 skb_pull(skb, sizeof(*rp));
1282
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001283 hci_dev_lock(hdev);
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001284 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001285 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001286 authenticated, smp->tk, smp->enc_key_size,
1287 rp->ediv, rp->rand);
1288 smp->ltk = ltk;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001289 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
Johan Hedberg4bd6d382014-02-26 23:33:45 +02001290 smp_distribute_keys(conn);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001291 hci_dev_unlock(hdev);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001292
1293 return 0;
1294}
1295
Johan Hedbergfd349c02014-02-18 10:19:36 +02001296static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1297{
1298 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001299 struct l2cap_chan *chan = conn->smp;
1300 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001301
1302 BT_DBG("");
1303
1304 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001305 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001306
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001307 /* Ignore this PDU if it wasn't requested */
1308 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1309 return 0;
1310
Johan Hedbergfd349c02014-02-18 10:19:36 +02001311 skb_pull(skb, sizeof(*info));
1312
1313 memcpy(smp->irk, info->irk, 16);
1314
1315 return 0;
1316}
1317
1318static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1319 struct sk_buff *skb)
1320{
1321 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001322 struct l2cap_chan *chan = conn->smp;
1323 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001324 struct hci_conn *hcon = conn->hcon;
1325 bdaddr_t rpa;
1326
1327 BT_DBG("");
1328
1329 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001330 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001331
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001332 /* Ignore this PDU if it wasn't requested */
1333 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
1334 return 0;
1335
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001336 /* Mark the information as received */
1337 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1338
Johan Hedbergfd349c02014-02-18 10:19:36 +02001339 skb_pull(skb, sizeof(*info));
1340
Johan Hedberg31dd6242014-06-27 14:23:02 +03001341 hci_dev_lock(hcon->hdev);
1342
Johan Hedberga9a58f82014-02-25 22:24:37 +02001343 /* Strictly speaking the Core Specification (4.1) allows sending
1344 * an empty address which would force us to rely on just the IRK
1345 * as "identity information". However, since such
1346 * implementations are not known of and in order to not over
1347 * complicate our implementation, simply pretend that we never
1348 * received an IRK for such a device.
1349 */
1350 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1351 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001352 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001353 }
1354
Johan Hedbergfd349c02014-02-18 10:19:36 +02001355 bacpy(&smp->id_addr, &info->bdaddr);
1356 smp->id_addr_type = info->addr_type;
1357
1358 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1359 bacpy(&rpa, &hcon->dst);
1360 else
1361 bacpy(&rpa, BDADDR_ANY);
1362
Johan Hedberg23d0e122014-02-19 14:57:46 +02001363 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1364 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001365
Johan Hedberg31dd6242014-06-27 14:23:02 +03001366distribute:
Johan Hedberg4bd6d382014-02-26 23:33:45 +02001367 smp_distribute_keys(conn);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001368
Johan Hedberg31dd6242014-06-27 14:23:02 +03001369 hci_dev_unlock(hcon->hdev);
1370
Johan Hedbergfd349c02014-02-18 10:19:36 +02001371 return 0;
1372}
1373
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001374static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1375{
1376 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001377 struct l2cap_chan *chan = conn->smp;
1378 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001379 struct hci_dev *hdev = conn->hcon->hdev;
1380 struct smp_csrk *csrk;
1381
1382 BT_DBG("conn %p", conn);
1383
1384 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001385 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001386
1387 /* Ignore this PDU if it wasn't requested */
1388 if (!(smp->remote_key_dist & SMP_DIST_SIGN))
1389 return 0;
1390
1391 /* Mark the information as received */
1392 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1393
1394 skb_pull(skb, sizeof(*rp));
1395
1396 hci_dev_lock(hdev);
1397 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1398 if (csrk) {
1399 csrk->master = 0x01;
1400 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1401 }
1402 smp->csrk = csrk;
Johan Hedberg5fcb9342014-08-07 10:03:31 +03001403 smp_distribute_keys(conn);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001404 hci_dev_unlock(hdev);
1405
1406 return 0;
1407}
1408
Johan Hedberg4befb862014-08-11 22:06:38 +03001409static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001410{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001411 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001412 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001413 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001414 int err = 0;
1415
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001416 if (hcon->type != LE_LINK) {
1417 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001418 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001419 }
1420
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001421 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001422 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001423
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001424 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Johan Hedbergbeb19e42014-07-18 11:15:26 +03001425 err = -EOPNOTSUPP;
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001426 reason = SMP_PAIRING_NOTSUPP;
1427 goto done;
1428 }
1429
Marcel Holtmann92381f52013-10-03 01:23:08 -07001430 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001431 skb_pull(skb, sizeof(code));
1432
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001433 /*
1434 * The SMP context must be initialized for all other PDUs except
1435 * pairing and security requests. If we get any other PDU when
1436 * not initialized simply disconnect (done if this function
1437 * returns an error).
1438 */
1439 if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ &&
Johan Hedbergd3368602014-08-08 09:28:05 +03001440 !test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) {
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001441 BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001442 reason = SMP_CMD_NOTSUPP;
1443 err = -EOPNOTSUPP;
1444 goto done;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001445 }
1446
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001447 switch (code) {
1448 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001449 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001450 break;
1451
1452 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001453 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001454 reason = 0;
1455 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001456 break;
1457
1458 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001459 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001460 break;
1461
1462 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001463 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001464 break;
1465
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001466 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001467 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001468 break;
1469
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001470 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001471 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001472 break;
1473
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001474 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001475 reason = smp_cmd_encrypt_info(conn, skb);
1476 break;
1477
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001478 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001479 reason = smp_cmd_master_ident(conn, skb);
1480 break;
1481
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001482 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001483 reason = smp_cmd_ident_info(conn, skb);
1484 break;
1485
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001486 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001487 reason = smp_cmd_ident_addr_info(conn, skb);
1488 break;
1489
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001490 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001491 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001492 break;
1493
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001494 default:
1495 BT_DBG("Unknown command code 0x%2.2x", code);
1496
1497 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001498 err = -EOPNOTSUPP;
1499 goto done;
1500 }
1501
1502done:
1503 if (reason)
Johan Hedberg84794e12013-11-06 11:24:57 +02001504 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001505 if (!err)
1506 kfree_skb(skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001507 return err;
1508}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001509
Johan Hedberg70db83c2014-08-08 09:37:16 +03001510static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1511{
1512 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001513 struct smp_chan *smp = chan->data;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001514
1515 BT_DBG("chan %p", chan);
1516
Johan Hedberg109ec232014-08-11 22:06:42 +03001517 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001518 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001519
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}