blob: de7dc7581ff072f5e2361c3506bddaa68d121b6b [file] [log] [blame]
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
8
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
21*/
22
Gustavo Padovan8c520a52012-05-23 04:04:22 -030023#include <linux/crypto.h>
24#include <linux/scatterlist.h>
25#include <crypto/b128ops.h>
26
Anderson Brigliaeb492e02011-06-09 18:50:40 -030027#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080030#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070031
32#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030033
Johan Hedbergb28b4942014-09-05 22:19:55 +030034#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030035
Marcel Holtmann17b02e62012-03-01 14:32:37 -080036#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030037
Johan Hedberg065a13e2012-10-11 16:26:06 +020038#define AUTH_REQ_MASK 0x07
Johan Hedberg88d3a8a2014-09-05 22:19:53 +030039#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020040
Johan Hedberg533e35d2014-06-16 19:25:18 +030041enum {
42 SMP_FLAG_TK_VALID,
43 SMP_FLAG_CFM_PENDING,
44 SMP_FLAG_MITM_AUTH,
45 SMP_FLAG_COMPLETE,
46 SMP_FLAG_INITIATOR,
47};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030048
49struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030050 struct l2cap_conn *conn;
51 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030052 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030053
Johan Hedberg4bc58f52014-05-20 09:45:47 +030054 u8 preq[7]; /* SMP Pairing Request */
55 u8 prsp[7]; /* SMP Pairing Response */
56 u8 prnd[16]; /* SMP Pairing Random (local) */
57 u8 rrnd[16]; /* SMP Pairing Random (remote) */
58 u8 pcnf[16]; /* SMP Pairing Confirm */
59 u8 tk[16]; /* SMP Temporary Key */
60 u8 enc_key_size;
61 u8 remote_key_dist;
62 bdaddr_t id_addr;
63 u8 id_addr_type;
64 u8 irk[16];
65 struct smp_csrk *csrk;
66 struct smp_csrk *slave_csrk;
67 struct smp_ltk *ltk;
68 struct smp_ltk *slave_ltk;
69 struct smp_irk *remote_irk;
Johan Hedberg4a74d652014-05-20 09:45:50 +030070 unsigned long flags;
Johan Hedberg6a7bd102014-06-27 14:23:03 +030071
72 struct crypto_blkcipher *tfm_aes;
Johan Hedberg4bc58f52014-05-20 09:45:47 +030073};
74
Johan Hedberg8a2936f2014-06-16 19:25:19 +030075static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030076{
Johan Hedberg8a2936f2014-06-16 19:25:19 +030077 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030078
Johan Hedberg8a2936f2014-06-16 19:25:19 +030079 for (i = 0; i < len; i++)
80 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030081}
82
83static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
84{
85 struct blkcipher_desc desc;
86 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +020087 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +020088 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030089
90 if (tfm == NULL) {
91 BT_ERR("tfm %p", tfm);
92 return -EINVAL;
93 }
94
95 desc.tfm = tfm;
96 desc.flags = 0;
97
Johan Hedberg943a7322014-03-18 12:58:24 +020098 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +030099 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200100
101 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300102 if (err) {
103 BT_ERR("cipher setkey failed: %d", err);
104 return err;
105 }
106
Johan Hedberg943a7322014-03-18 12:58:24 +0200107 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300108 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200109
110 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300111
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300112 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
113 if (err)
114 BT_ERR("Encrypt data error %d", err);
115
Johan Hedberg943a7322014-03-18 12:58:24 +0200116 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300117 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200118
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300119 return err;
120}
121
Johan Hedberg60478052014-02-18 10:19:31 +0200122static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
123{
Johan Hedberg943a7322014-03-18 12:58:24 +0200124 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200125 int err;
126
127 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200128 memcpy(_res, r, 3);
129 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200130
Johan Hedberg943a7322014-03-18 12:58:24 +0200131 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200132 if (err) {
133 BT_ERR("Encrypt error");
134 return err;
135 }
136
137 /* The output of the random address function ah is:
138 * ah(h, r) = e(k, r') mod 2^24
139 * The output of the security function e is then truncated to 24 bits
140 * by taking the least significant 24 bits of the output of e as the
141 * result of ah.
142 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200143 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200144
145 return 0;
146}
147
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300148bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200149{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300150 struct l2cap_chan *chan = hdev->smp_data;
151 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200152 u8 hash[3];
153 int err;
154
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300155 if (!chan || !chan->data)
156 return false;
157
158 tfm = chan->data;
159
Johan Hedberg60478052014-02-18 10:19:31 +0200160 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
161
162 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
163 if (err)
164 return false;
165
166 return !memcmp(bdaddr->b, hash, 3);
167}
168
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300169int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200170{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300171 struct l2cap_chan *chan = hdev->smp_data;
172 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200173 int err;
174
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300175 if (!chan || !chan->data)
176 return -EOPNOTSUPP;
177
178 tfm = chan->data;
179
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200180 get_random_bytes(&rpa->b[3], 3);
181
182 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
183 rpa->b[5] |= 0x40; /* Set second most significant bit */
184
185 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
186 if (err < 0)
187 return err;
188
189 BT_DBG("RPA %pMR", rpa);
190
191 return 0;
192}
193
Johan Hedberge491eaf2014-10-25 21:15:37 +0200194static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
195 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
196 bdaddr_t *ra, u8 res[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300197{
198 u8 p1[16], p2[16];
199 int err;
200
201 memset(p1, 0, 16);
202
203 /* p1 = pres || preq || _rat || _iat */
Johan Hedberg943a7322014-03-18 12:58:24 +0200204 p1[0] = _iat;
205 p1[1] = _rat;
206 memcpy(p1 + 2, preq, 7);
207 memcpy(p1 + 9, pres, 7);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300208
209 /* p2 = padding || ia || ra */
Johan Hedberg943a7322014-03-18 12:58:24 +0200210 memcpy(p2, ra, 6);
211 memcpy(p2 + 6, ia, 6);
212 memset(p2 + 12, 0, 4);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300213
214 /* res = r XOR p1 */
215 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
216
217 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200218 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300219 if (err) {
220 BT_ERR("Encrypt data error");
221 return err;
222 }
223
224 /* res = res XOR p2 */
225 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
226
227 /* res = e(k, res) */
Johan Hedberge491eaf2014-10-25 21:15:37 +0200228 err = smp_e(tfm_aes, k, res);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300229 if (err)
230 BT_ERR("Encrypt data error");
231
232 return err;
233}
234
Johan Hedberge491eaf2014-10-25 21:15:37 +0200235static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
236 u8 r2[16], u8 _r[16])
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300237{
238 int err;
239
240 /* Just least significant octets from r1 and r2 are considered */
Johan Hedberg943a7322014-03-18 12:58:24 +0200241 memcpy(_r, r2, 8);
242 memcpy(_r + 8, r1, 8);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300243
Johan Hedberge491eaf2014-10-25 21:15:37 +0200244 err = smp_e(tfm_aes, k, _r);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300245 if (err)
246 BT_ERR("Encrypt data error");
247
248 return err;
249}
250
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300251static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
252{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300253 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300254 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300255 struct kvec iv[2];
256 struct msghdr msg;
257
258 if (!chan)
259 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300260
261 BT_DBG("code 0x%2.2x", code);
262
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300263 iv[0].iov_base = &code;
264 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300265
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300266 iv[1].iov_base = data;
267 iv[1].iov_len = len;
268
269 memset(&msg, 0, sizeof(msg));
270
Al Viro17836392014-11-24 17:07:38 -0500271 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300272
273 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300274
Johan Hedbergb68fda62014-08-11 22:06:40 +0300275 if (!chan->data)
276 return;
277
278 smp = chan->data;
279
280 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300281 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300282}
283
Brian Gix2b64d152011-12-21 16:12:12 -0800284static __u8 authreq_to_seclevel(__u8 authreq)
285{
286 if (authreq & SMP_AUTH_MITM)
287 return BT_SECURITY_HIGH;
288 else
289 return BT_SECURITY_MEDIUM;
290}
291
292static __u8 seclevel_to_authreq(__u8 sec_level)
293{
294 switch (sec_level) {
295 case BT_SECURITY_HIGH:
296 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
297 case BT_SECURITY_MEDIUM:
298 return SMP_AUTH_BONDING;
299 default:
300 return SMP_AUTH_NONE;
301 }
302}
303
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300304static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700305 struct smp_cmd_pairing *req,
306 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300307{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300308 struct l2cap_chan *chan = conn->smp;
309 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200310 struct hci_conn *hcon = conn->hcon;
311 struct hci_dev *hdev = hcon->hdev;
312 u8 local_dist = 0, remote_dist = 0;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300313
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300314 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700315 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
316 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300317 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800318 } else {
319 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300320 }
321
Johan Hedbergfd349c02014-02-18 10:19:36 +0200322 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
323 remote_dist |= SMP_DIST_ID_KEY;
324
Johan Hedberg863efaf2014-02-22 19:06:32 +0200325 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
326 local_dist |= SMP_DIST_ID_KEY;
327
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300328 if (rsp == NULL) {
329 req->io_capability = conn->hcon->io_capability;
330 req->oob_flag = SMP_OOB_NOT_PRESENT;
331 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200332 req->init_key_dist = local_dist;
333 req->resp_key_dist = remote_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200334 req->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200335
336 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300337 return;
338 }
339
340 rsp->io_capability = conn->hcon->io_capability;
341 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
342 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200343 rsp->init_key_dist = req->init_key_dist & remote_dist;
344 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg065a13e2012-10-11 16:26:06 +0200345 rsp->auth_req = (authreq & AUTH_REQ_MASK);
Johan Hedbergfd349c02014-02-18 10:19:36 +0200346
347 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300348}
349
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300350static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
351{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300352 struct l2cap_chan *chan = conn->smp;
353 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300354
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300355 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700356 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300357 return SMP_ENC_KEY_SIZE;
358
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300359 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300360
361 return 0;
362}
363
Johan Hedberg6f48e262014-08-11 22:06:44 +0300364static void smp_chan_destroy(struct l2cap_conn *conn)
365{
366 struct l2cap_chan *chan = conn->smp;
367 struct smp_chan *smp = chan->data;
368 bool complete;
369
370 BUG_ON(!smp);
371
372 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300373
Johan Hedberg6f48e262014-08-11 22:06:44 +0300374 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
375 mgmt_smp_complete(conn->hcon, complete);
376
377 kfree(smp->csrk);
378 kfree(smp->slave_csrk);
379
380 crypto_free_blkcipher(smp->tfm_aes);
381
382 /* If pairing failed clean up any keys we might have */
383 if (!complete) {
384 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200385 list_del_rcu(&smp->ltk->list);
386 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300387 }
388
389 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200390 list_del_rcu(&smp->slave_ltk->list);
391 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300392 }
393
394 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200395 list_del_rcu(&smp->remote_irk->list);
396 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300397 }
398 }
399
400 chan->data = NULL;
401 kfree(smp);
402 hci_conn_drop(conn->hcon);
403}
404
Johan Hedberg84794e12013-11-06 11:24:57 +0200405static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800406{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200407 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300408 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200409
Johan Hedberg84794e12013-11-06 11:24:57 +0200410 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800411 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700412 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800413
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700414 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700415 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300416
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300417 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300418 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800419}
420
Brian Gix2b64d152011-12-21 16:12:12 -0800421#define JUST_WORKS 0x00
422#define JUST_CFM 0x01
423#define REQ_PASSKEY 0x02
424#define CFM_PASSKEY 0x03
425#define REQ_OOB 0x04
426#define OVERLAP 0xFF
427
428static const u8 gen_method[5][5] = {
429 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
430 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
431 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
432 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
433 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
434};
435
Johan Hedberg581370c2014-06-17 13:07:38 +0300436static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
437{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300438 /* If either side has unknown io_caps, use JUST_CFM (which gets
439 * converted later to JUST_WORKS if we're initiators.
440 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300441 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
442 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300443 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300444
445 return gen_method[remote_io][local_io];
446}
447
Brian Gix2b64d152011-12-21 16:12:12 -0800448static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
449 u8 local_io, u8 remote_io)
450{
451 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300452 struct l2cap_chan *chan = conn->smp;
453 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800454 u8 method;
455 u32 passkey = 0;
456 int ret = 0;
457
458 /* Initialize key for JUST WORKS */
459 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300460 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800461
462 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
463
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300464 /* If neither side wants MITM, either "just" confirm an incoming
465 * request or use just-works for outgoing ones. The JUST_CFM
466 * will be converted to JUST_WORKS if necessary later in this
467 * function. If either side has MITM look up the method from the
468 * table.
469 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300470 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300471 method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800472 else
Johan Hedberg581370c2014-06-17 13:07:38 +0300473 method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800474
Johan Hedberga82505c2014-03-24 14:39:07 +0200475 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg4a74d652014-05-20 09:45:50 +0300476 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
Johan Hedberga82505c2014-03-24 14:39:07 +0200477 method = JUST_WORKS;
478
Johan Hedberg02f3e252014-07-16 15:09:13 +0300479 /* Don't bother user space with no IO capabilities */
480 if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
481 method = JUST_WORKS;
482
Brian Gix2b64d152011-12-21 16:12:12 -0800483 /* If Just Works, Continue with Zero TK */
484 if (method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300485 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800486 return 0;
487 }
488
489 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300490 if (method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300491 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300492 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
493 hcon->pending_sec_level = BT_SECURITY_HIGH;
494 }
Brian Gix2b64d152011-12-21 16:12:12 -0800495
496 /* If both devices have Keyoard-Display I/O, the master
497 * Confirms and the slave Enters the passkey.
498 */
499 if (method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300500 if (hcon->role == HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -0800501 method = CFM_PASSKEY;
502 else
503 method = REQ_PASSKEY;
504 }
505
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200506 /* Generate random passkey. */
Brian Gix2b64d152011-12-21 16:12:12 -0800507 if (method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200508 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800509 get_random_bytes(&passkey, sizeof(passkey));
510 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200511 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800512 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300513 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800514 }
515
Brian Gix2b64d152011-12-21 16:12:12 -0800516 if (method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700517 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200518 hcon->type, hcon->dst_type);
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200519 else if (method == JUST_CFM)
520 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
521 hcon->type, hcon->dst_type,
522 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800523 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200524 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200525 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200526 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800527
Brian Gix2b64d152011-12-21 16:12:12 -0800528 return ret;
529}
530
Johan Hedberg1cc61142014-05-20 09:45:52 +0300531static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300532{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300533 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300534 struct smp_cmd_pairing_confirm cp;
535 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300536
537 BT_DBG("conn %p", conn);
538
Johan Hedberge491eaf2014-10-25 21:15:37 +0200539 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200540 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200541 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
542 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300543 if (ret)
544 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300545
Johan Hedberg4a74d652014-05-20 09:45:50 +0300546 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800547
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300548 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
549
Johan Hedbergb28b4942014-09-05 22:19:55 +0300550 if (conn->hcon->out)
551 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
552 else
553 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
554
Johan Hedberg1cc61142014-05-20 09:45:52 +0300555 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300556}
557
Johan Hedberg861580a2014-05-20 09:45:51 +0300558static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300559{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300560 struct l2cap_conn *conn = smp->conn;
561 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300562 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300563 int ret;
564
Johan Hedbergec70f362014-06-27 14:23:04 +0300565 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300566 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300567
568 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
569
Johan Hedberge491eaf2014-10-25 21:15:37 +0200570 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200571 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200572 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300573 if (ret)
574 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300575
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300576 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
577 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300578 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300579 }
580
581 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800582 u8 stk[16];
583 __le64 rand = 0;
584 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300585
Johan Hedberge491eaf2014-10-25 21:15:37 +0200586 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300587
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300588 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300589 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300590
Johan Hedberg861580a2014-05-20 09:45:51 +0300591 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
592 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300593
594 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300595 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300596 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300597 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300598 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800599 __le64 rand = 0;
600 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300601
Johan Hedberg943a7322014-03-18 12:58:24 +0200602 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
603 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300604
Johan Hedberge491eaf2014-10-25 21:15:37 +0200605 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300606
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300607 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700608 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300609
Johan Hedbergfff34902014-06-10 15:19:50 +0300610 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
611 auth = 1;
612 else
613 auth = 0;
614
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300615 /* Even though there's no _SLAVE suffix this is the
616 * slave STK we're adding for later lookup (the master
617 * STK never needs to be stored).
618 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700619 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300620 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300621 }
622
Johan Hedberg861580a2014-05-20 09:45:51 +0300623 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300624}
625
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300626static void smp_notify_keys(struct l2cap_conn *conn)
627{
628 struct l2cap_chan *chan = conn->smp;
629 struct smp_chan *smp = chan->data;
630 struct hci_conn *hcon = conn->hcon;
631 struct hci_dev *hdev = hcon->hdev;
632 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
633 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
634 bool persistent;
635
636 if (smp->remote_irk) {
637 mgmt_new_irk(hdev, smp->remote_irk);
638 /* Now that user space can be considered to know the
639 * identity address track the connection based on it
640 * from now on.
641 */
642 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
643 hcon->dst_type = smp->remote_irk->addr_type;
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300644 queue_work(hdev->workqueue, &conn->id_addr_update_work);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300645
646 /* When receiving an indentity resolving key for
647 * a remote device that does not use a resolvable
648 * private address, just remove the key so that
649 * it is possible to use the controller white
650 * list for scanning.
651 *
652 * Userspace will have been told to not store
653 * this key at this point. So it is safe to
654 * just remove it.
655 */
656 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200657 list_del_rcu(&smp->remote_irk->list);
658 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300659 smp->remote_irk = NULL;
660 }
661 }
662
663 /* The LTKs and CSRKs should be persistent only if both sides
664 * had the bonding bit set in their authentication requests.
665 */
666 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
667
668 if (smp->csrk) {
669 smp->csrk->bdaddr_type = hcon->dst_type;
670 bacpy(&smp->csrk->bdaddr, &hcon->dst);
671 mgmt_new_csrk(hdev, smp->csrk, persistent);
672 }
673
674 if (smp->slave_csrk) {
675 smp->slave_csrk->bdaddr_type = hcon->dst_type;
676 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
677 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
678 }
679
680 if (smp->ltk) {
681 smp->ltk->bdaddr_type = hcon->dst_type;
682 bacpy(&smp->ltk->bdaddr, &hcon->dst);
683 mgmt_new_ltk(hdev, smp->ltk, persistent);
684 }
685
686 if (smp->slave_ltk) {
687 smp->slave_ltk->bdaddr_type = hcon->dst_type;
688 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
689 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
690 }
691}
692
Johan Hedbergb28b4942014-09-05 22:19:55 +0300693static void smp_allow_key_dist(struct smp_chan *smp)
694{
695 /* Allow the first expected phase 3 PDU. The rest of the PDUs
696 * will be allowed in each PDU handler to ensure we receive
697 * them in the correct order.
698 */
699 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
700 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
701 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
702 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
703 else if (smp->remote_key_dist & SMP_DIST_SIGN)
704 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
705}
706
Johan Hedbergd6268e82014-09-05 22:19:51 +0300707static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300708{
709 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +0300710 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300711 struct hci_conn *hcon = conn->hcon;
712 struct hci_dev *hdev = hcon->hdev;
713 __u8 *keydist;
714
715 BT_DBG("conn %p", conn);
716
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300717 rsp = (void *) &smp->prsp[1];
718
719 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300720 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
721 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300722 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300723 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300724
725 req = (void *) &smp->preq[1];
726
727 if (hcon->out) {
728 keydist = &rsp->init_key_dist;
729 *keydist &= req->init_key_dist;
730 } else {
731 keydist = &rsp->resp_key_dist;
732 *keydist &= req->resp_key_dist;
733 }
734
735 BT_DBG("keydist 0x%x", *keydist);
736
737 if (*keydist & SMP_DIST_ENC_KEY) {
738 struct smp_cmd_encrypt_info enc;
739 struct smp_cmd_master_ident ident;
740 struct smp_ltk *ltk;
741 u8 authenticated;
742 __le16 ediv;
743 __le64 rand;
744
745 get_random_bytes(enc.ltk, sizeof(enc.ltk));
746 get_random_bytes(&ediv, sizeof(ediv));
747 get_random_bytes(&rand, sizeof(rand));
748
749 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
750
751 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
752 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
753 SMP_LTK_SLAVE, authenticated, enc.ltk,
754 smp->enc_key_size, ediv, rand);
755 smp->slave_ltk = ltk;
756
757 ident.ediv = ediv;
758 ident.rand = rand;
759
760 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
761
762 *keydist &= ~SMP_DIST_ENC_KEY;
763 }
764
765 if (*keydist & SMP_DIST_ID_KEY) {
766 struct smp_cmd_ident_addr_info addrinfo;
767 struct smp_cmd_ident_info idinfo;
768
769 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
770
771 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
772
773 /* The hci_conn contains the local identity address
774 * after the connection has been established.
775 *
776 * This is true even when the connection has been
777 * established using a resolvable random address.
778 */
779 bacpy(&addrinfo.bdaddr, &hcon->src);
780 addrinfo.addr_type = hcon->src_type;
781
782 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
783 &addrinfo);
784
785 *keydist &= ~SMP_DIST_ID_KEY;
786 }
787
788 if (*keydist & SMP_DIST_SIGN) {
789 struct smp_cmd_sign_info sign;
790 struct smp_csrk *csrk;
791
792 /* Generate a new random key */
793 get_random_bytes(sign.csrk, sizeof(sign.csrk));
794
795 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
796 if (csrk) {
797 csrk->master = 0x00;
798 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
799 }
800 smp->slave_csrk = csrk;
801
802 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
803
804 *keydist &= ~SMP_DIST_SIGN;
805 }
806
807 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +0300808 if (smp->remote_key_dist & KEY_DIST_MASK) {
809 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +0300810 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300811 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300812
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300813 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
814 smp_notify_keys(conn);
815
816 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300817}
818
Johan Hedbergb68fda62014-08-11 22:06:40 +0300819static void smp_timeout(struct work_struct *work)
820{
821 struct smp_chan *smp = container_of(work, struct smp_chan,
822 security_timer.work);
823 struct l2cap_conn *conn = smp->conn;
824
825 BT_DBG("conn %p", conn);
826
Johan Hedberg1e91c292014-08-18 20:33:29 +0300827 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +0300828}
829
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300830static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
831{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300832 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300833 struct smp_chan *smp;
834
Marcel Holtmannf1560462013-10-13 05:43:25 -0700835 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300836 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300837 return NULL;
838
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300839 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
840 if (IS_ERR(smp->tfm_aes)) {
841 BT_ERR("Unable to create ECB crypto context");
842 kfree(smp);
843 return NULL;
844 }
845
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300846 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300847 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300848
Johan Hedbergb28b4942014-09-05 22:19:55 +0300849 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
850
Johan Hedbergb68fda62014-08-11 22:06:40 +0300851 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
852
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300853 hci_conn_hold(conn->hcon);
854
855 return smp;
856}
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;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300864 int err;
Brian Gix2b64d152011-12-21 16:12:12 -0800865
866 BT_DBG("");
867
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300868 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -0800869 return -ENOTCONN;
870
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300871 chan = conn->smp;
872 if (!chan)
873 return -ENOTCONN;
874
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300875 l2cap_chan_lock(chan);
876 if (!chan->data) {
877 err = -ENOTCONN;
878 goto unlock;
879 }
880
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300881 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800882
883 switch (mgmt_op) {
884 case MGMT_OP_USER_PASSKEY_REPLY:
885 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +0200886 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800887 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +0200888 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800889 /* Fall Through */
890 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +0300891 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800892 break;
893 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
894 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +0200895 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300896 err = 0;
897 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800898 default:
Johan Hedberg84794e12013-11-06 11:24:57 +0200899 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300900 err = -EOPNOTSUPP;
901 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -0800902 }
903
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300904 err = 0;
905
Brian Gix2b64d152011-12-21 16:12:12 -0800906 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +0300907 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
908 u8 rsp = smp_confirm(smp);
909 if (rsp)
910 smp_failure(conn, rsp);
911 }
Brian Gix2b64d152011-12-21 16:12:12 -0800912
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300913unlock:
914 l2cap_chan_unlock(chan);
915 return err;
Brian Gix2b64d152011-12-21 16:12:12 -0800916}
917
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300918static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300919{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300920 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300921 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +0300922 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300923 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +0300924 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300925 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300926
927 BT_DBG("conn %p", conn);
928
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200929 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +0300930 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +0200931
Johan Hedberg40bef302014-07-16 11:42:27 +0300932 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -0800933 return SMP_CMD_NOTSUPP;
934
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300935 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300936 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300937 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300938 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300939
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +0300940 if (!smp)
941 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300942
Johan Hedbergc05b9332014-09-10 17:37:42 -0700943 /* We didn't start the pairing, so match remote */
944 auth = req->auth_req & AUTH_REQ_MASK;
945
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300946 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -0700947 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +0300948 return SMP_PAIRING_NOTSUPP;
949
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300950 smp->preq[0] = SMP_CMD_PAIRING_REQ;
951 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300952 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300953
Johan Hedberg5be5e272014-09-10 17:58:54 -0700954 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -0700955 sec_level = BT_SECURITY_MEDIUM;
956 else
957 sec_level = authreq_to_seclevel(auth);
958
Johan Hedbergc7262e72014-06-17 13:07:37 +0300959 if (sec_level > conn->hcon->pending_sec_level)
960 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +0200961
Stephen Hemminger49c922b2014-10-27 21:12:20 -0700962 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +0300963 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
964 u8 method;
965
966 method = get_auth_method(smp, conn->hcon->io_capability,
967 req->io_capability);
968 if (method == JUST_WORKS || method == JUST_CFM)
969 return SMP_AUTH_REQUIREMENTS;
970 }
971
Brian Gix2b64d152011-12-21 16:12:12 -0800972 build_pairing_cmd(conn, req, &rsp, auth);
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300973
974 key_size = min(req->max_key_size, rsp.max_key_size);
975 if (check_enc_key_size(conn, key_size))
976 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300977
Johan Hedberge84a6b12013-12-02 10:49:03 +0200978 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300979
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300980 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
981 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -0300982
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300983 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedbergb28b4942014-09-05 22:19:55 +0300984 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300985
Brian Gix2b64d152011-12-21 16:12:12 -0800986 /* Request setup of TK */
987 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
988 if (ret)
989 return SMP_UNSPECIFIED;
990
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300991 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300992}
993
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -0300994static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -0300995{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300996 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300997 struct l2cap_chan *chan = conn->smp;
998 struct smp_chan *smp = chan->data;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -0700999 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001000 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001001
1002 BT_DBG("conn %p", conn);
1003
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001004 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001005 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001006
Johan Hedberg40bef302014-07-16 11:42:27 +03001007 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001008 return SMP_CMD_NOTSUPP;
1009
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001010 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001011
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001012 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001013
1014 key_size = min(req->max_key_size, rsp->max_key_size);
1015 if (check_enc_key_size(conn, key_size))
1016 return SMP_ENC_KEY_SIZE;
1017
Johan Hedbergc05b9332014-09-10 17:37:42 -07001018 auth = rsp->auth_req & AUTH_REQ_MASK;
1019
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001020 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001021 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1022 u8 method;
1023
1024 method = get_auth_method(smp, req->io_capability,
1025 rsp->io_capability);
1026 if (method == JUST_WORKS || method == JUST_CFM)
1027 return SMP_AUTH_REQUIREMENTS;
1028 }
1029
Johan Hedberge84a6b12013-12-02 10:49:03 +02001030 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001031
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001032 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1033 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001034
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001035 /* Update remote key distribution in case the remote cleared
1036 * some bits that we had enabled in our request.
1037 */
1038 smp->remote_key_dist &= rsp->resp_key_dist;
1039
Johan Hedbergc05b9332014-09-10 17:37:42 -07001040 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001041
Johan Hedberg476585e2012-06-06 18:54:15 +08001042 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001043 if (ret)
1044 return SMP_UNSPECIFIED;
1045
Johan Hedberg4a74d652014-05-20 09:45:50 +03001046 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001047
1048 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001049 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001050 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001051
1052 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001053}
1054
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001055static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001056{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001057 struct l2cap_chan *chan = conn->smp;
1058 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001059
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001060 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1061
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001062 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001063 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001064
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001065 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1066 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001067
Johan Hedbergb28b4942014-09-05 22:19:55 +03001068 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001069 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1070 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001071 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1072 return 0;
1073 }
1074
1075 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001076 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001077 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001078 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001079
1080 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001081}
1082
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001083static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001084{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001085 struct l2cap_chan *chan = conn->smp;
1086 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001087
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001088 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001089
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001090 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001091 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001092
Johan Hedberg943a7322014-03-18 12:58:24 +02001093 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001094 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001095
Johan Hedberg861580a2014-05-20 09:45:51 +03001096 return smp_random(smp);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001097}
1098
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001099static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001100{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001101 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001102 struct hci_conn *hcon = conn->hcon;
1103
Johan Hedberg98a0b842014-01-30 19:40:00 -08001104 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001105 hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001106 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001107 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001108
Johan Hedberga6f78332014-09-10 17:37:45 -07001109 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001110 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08001111
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001112 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001113 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001114
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001115 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1116 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001117
Johan Hedbergfe59a052014-07-01 19:14:12 +03001118 /* We never store STKs for master role, so clear this flag */
1119 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1120
Marcel Holtmannf81cd822014-07-01 10:59:24 +02001121 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001122}
Marcel Holtmannf1560462013-10-13 05:43:25 -07001123
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001124bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1125 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03001126{
1127 if (sec_level == BT_SECURITY_LOW)
1128 return true;
1129
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001130 /* If we're encrypted with an STK but the caller prefers using
1131 * LTK claim insufficient security. This way we allow the
1132 * connection to be re-encrypted with an LTK, even if the LTK
1133 * provides the same level of security. Only exception is if we
1134 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001135 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001136 if (key_pref == SMP_USE_LTK &&
1137 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergb2d5e252014-07-14 14:34:55 +03001138 hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberge804d252014-07-16 11:42:28 +03001139 hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03001140 return false;
1141
Johan Hedberg854f4722014-07-01 18:40:20 +03001142 if (hcon->sec_level >= sec_level)
1143 return true;
1144
1145 return false;
1146}
1147
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001148static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001149{
1150 struct smp_cmd_security_req *rp = (void *) skb->data;
1151 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001152 struct hci_conn *hcon = conn->hcon;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001153 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07001154 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001155
1156 BT_DBG("conn %p", conn);
1157
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001158 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001159 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001160
Johan Hedberg40bef302014-07-16 11:42:27 +03001161 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02001162 return SMP_CMD_NOTSUPP;
1163
Johan Hedbergc05b9332014-09-10 17:37:42 -07001164 auth = rp->auth_req & AUTH_REQ_MASK;
1165
Johan Hedberg5be5e272014-09-10 17:58:54 -07001166 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001167 sec_level = BT_SECURITY_MEDIUM;
1168 else
1169 sec_level = authreq_to_seclevel(auth);
1170
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001171 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03001172 return 0;
1173
Johan Hedbergc7262e72014-06-17 13:07:37 +03001174 if (sec_level > hcon->pending_sec_level)
1175 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03001176
Johan Hedberg4dab7862012-06-07 14:58:37 +08001177 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03001178 return 0;
1179
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001180 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03001181 if (!smp)
1182 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001183
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001184 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001185 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03001186 return SMP_PAIRING_NOTSUPP;
1187
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001188 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001189
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001190 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07001191 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001192
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001193 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1194 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001195
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001196 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001197 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001198
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001199 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001200}
1201
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001202int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001203{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001204 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001205 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001206 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08001207 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001208 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001209
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001210 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1211
Johan Hedberg0a66cf22014-03-24 14:39:03 +02001212 /* This may be NULL if there's an unexpected disconnection */
1213 if (!conn)
1214 return 1;
1215
Johan Hedbergc68b7f12014-09-06 06:59:10 +03001216 chan = conn->smp;
1217
Johan Hedberg757aee02013-04-24 13:05:32 +03001218 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001219 return 1;
1220
Johan Hedberg35dc6f82014-11-13 10:55:18 +02001221 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03001222 return 1;
1223
Johan Hedbergc7262e72014-06-17 13:07:37 +03001224 if (sec_level > hcon->pending_sec_level)
1225 hcon->pending_sec_level = sec_level;
1226
Johan Hedberg40bef302014-07-16 11:42:27 +03001227 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03001228 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1229 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001230
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001231 l2cap_chan_lock(chan);
1232
1233 /* If SMP is already in progress ignore this request */
1234 if (chan->data) {
1235 ret = 0;
1236 goto unlock;
1237 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001238
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001239 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001240 if (!smp) {
1241 ret = 1;
1242 goto unlock;
1243 }
Brian Gix2b64d152011-12-21 16:12:12 -08001244
1245 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001246
Johan Hedberg79897d22014-06-01 09:45:24 +03001247 /* Require MITM if IO Capability allows or the security level
1248 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02001249 */
Johan Hedberg79897d22014-06-01 09:45:24 +03001250 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03001251 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02001252 authreq |= SMP_AUTH_MITM;
1253
Johan Hedberg40bef302014-07-16 11:42:27 +03001254 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001255 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001256
Brian Gix2b64d152011-12-21 16:12:12 -08001257 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001258 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1259 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001260
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001261 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001262 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001263 } else {
1264 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08001265 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001266 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001267 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001268 }
1269
Johan Hedberg4a74d652014-05-20 09:45:50 +03001270 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001271 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02001272
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001273unlock:
1274 l2cap_chan_unlock(chan);
1275 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001276}
1277
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001278static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1279{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001280 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001281 struct l2cap_chan *chan = conn->smp;
1282 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001283
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001284 BT_DBG("conn %p", conn);
1285
1286 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001287 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001288
Johan Hedbergb28b4942014-09-05 22:19:55 +03001289 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001290
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001291 skb_pull(skb, sizeof(*rp));
1292
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001293 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001294
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001295 return 0;
1296}
1297
1298static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1299{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001300 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001301 struct l2cap_chan *chan = conn->smp;
1302 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001303 struct hci_dev *hdev = conn->hcon->hdev;
1304 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02001305 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001306 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001307
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001308 BT_DBG("conn %p", conn);
1309
1310 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001311 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001312
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001313 /* Mark the information as received */
1314 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1315
Johan Hedbergb28b4942014-09-05 22:19:55 +03001316 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1317 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07001318 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1319 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001320
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03001321 skb_pull(skb, sizeof(*rp));
1322
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001323 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03001324 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02001325 authenticated, smp->tk, smp->enc_key_size,
1326 rp->ediv, rp->rand);
1327 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001328 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03001329 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001330
1331 return 0;
1332}
1333
Johan Hedbergfd349c02014-02-18 10:19:36 +02001334static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1335{
1336 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001337 struct l2cap_chan *chan = conn->smp;
1338 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001339
1340 BT_DBG("");
1341
1342 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001343 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001344
Johan Hedbergb28b4942014-09-05 22:19:55 +03001345 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02001346
Johan Hedbergfd349c02014-02-18 10:19:36 +02001347 skb_pull(skb, sizeof(*info));
1348
1349 memcpy(smp->irk, info->irk, 16);
1350
1351 return 0;
1352}
1353
1354static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1355 struct sk_buff *skb)
1356{
1357 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001358 struct l2cap_chan *chan = conn->smp;
1359 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001360 struct hci_conn *hcon = conn->hcon;
1361 bdaddr_t rpa;
1362
1363 BT_DBG("");
1364
1365 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001366 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02001367
Johan Hedberg9747a9f2014-02-26 23:33:43 +02001368 /* Mark the information as received */
1369 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1370
Johan Hedbergb28b4942014-09-05 22:19:55 +03001371 if (smp->remote_key_dist & SMP_DIST_SIGN)
1372 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1373
Johan Hedbergfd349c02014-02-18 10:19:36 +02001374 skb_pull(skb, sizeof(*info));
1375
Johan Hedberga9a58f82014-02-25 22:24:37 +02001376 /* Strictly speaking the Core Specification (4.1) allows sending
1377 * an empty address which would force us to rely on just the IRK
1378 * as "identity information". However, since such
1379 * implementations are not known of and in order to not over
1380 * complicate our implementation, simply pretend that we never
1381 * received an IRK for such a device.
1382 */
1383 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1384 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03001385 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02001386 }
1387
Johan Hedbergfd349c02014-02-18 10:19:36 +02001388 bacpy(&smp->id_addr, &info->bdaddr);
1389 smp->id_addr_type = info->addr_type;
1390
1391 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1392 bacpy(&rpa, &hcon->dst);
1393 else
1394 bacpy(&rpa, BDADDR_ANY);
1395
Johan Hedberg23d0e122014-02-19 14:57:46 +02001396 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1397 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001398
Johan Hedberg31dd6242014-06-27 14:23:02 +03001399distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03001400 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1401 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02001402
1403 return 0;
1404}
1405
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001406static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1407{
1408 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001409 struct l2cap_chan *chan = conn->smp;
1410 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001411 struct smp_csrk *csrk;
1412
1413 BT_DBG("conn %p", conn);
1414
1415 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001416 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001417
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001418 /* Mark the information as received */
1419 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1420
1421 skb_pull(skb, sizeof(*rp));
1422
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001423 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1424 if (csrk) {
1425 csrk->master = 0x01;
1426 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1427 }
1428 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03001429 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001430
1431 return 0;
1432}
1433
Johan Hedberg4befb862014-08-11 22:06:38 +03001434static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001435{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001436 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001437 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001438 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001439 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001440 int err = 0;
1441
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001442 if (hcon->type != LE_LINK) {
1443 kfree_skb(skb);
Johan Hedberg34327112013-10-16 11:37:01 +03001444 return 0;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07001445 }
1446
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001447 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07001448 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07001449
Marcel Holtmann06ae3312013-10-18 03:43:00 -07001450 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03001451 reason = SMP_PAIRING_NOTSUPP;
1452 goto done;
1453 }
1454
Marcel Holtmann92381f52013-10-03 01:23:08 -07001455 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001456 skb_pull(skb, sizeof(code));
1457
Johan Hedbergb28b4942014-09-05 22:19:55 +03001458 smp = chan->data;
1459
1460 if (code > SMP_CMD_MAX)
1461 goto drop;
1462
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07001463 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03001464 goto drop;
1465
1466 /* If we don't have a context the only allowed commands are
1467 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001468 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001469 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
1470 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06001471
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001472 switch (code) {
1473 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001474 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001475 break;
1476
1477 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02001478 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001479 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001480 break;
1481
1482 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001483 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001484 break;
1485
1486 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001487 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001488 break;
1489
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001490 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001491 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001492 break;
1493
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001494 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001495 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001496 break;
1497
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001498 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001499 reason = smp_cmd_encrypt_info(conn, skb);
1500 break;
1501
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001502 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001503 reason = smp_cmd_master_ident(conn, skb);
1504 break;
1505
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001506 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001507 reason = smp_cmd_ident_info(conn, skb);
1508 break;
1509
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001510 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02001511 reason = smp_cmd_ident_addr_info(conn, skb);
1512 break;
1513
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001514 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07001515 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001516 break;
1517
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001518 default:
1519 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001520 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001521 goto done;
1522 }
1523
1524done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001525 if (!err) {
1526 if (reason)
1527 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03001528 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03001529 }
1530
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001531 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001532
1533drop:
1534 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
1535 code, &hcon->dst);
1536 kfree_skb(skb);
1537 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001538}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03001539
Johan Hedberg70db83c2014-08-08 09:37:16 +03001540static void smp_teardown_cb(struct l2cap_chan *chan, int err)
1541{
1542 struct l2cap_conn *conn = chan->conn;
1543
1544 BT_DBG("chan %p", chan);
1545
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001546 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001547 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001548
Johan Hedberg70db83c2014-08-08 09:37:16 +03001549 conn->smp = NULL;
1550 l2cap_chan_put(chan);
1551}
1552
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001553static void smp_resume_cb(struct l2cap_chan *chan)
1554{
Johan Hedbergb68fda62014-08-11 22:06:40 +03001555 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001556 struct l2cap_conn *conn = chan->conn;
1557 struct hci_conn *hcon = conn->hcon;
1558
1559 BT_DBG("chan %p", chan);
1560
Johan Hedberg86d14072014-08-11 22:06:43 +03001561 if (!smp)
1562 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03001563
Johan Hedberg84bc0db2014-09-05 22:19:49 +03001564 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
1565 return;
1566
Johan Hedberg86d14072014-08-11 22:06:43 +03001567 cancel_delayed_work(&smp->security_timer);
1568
Johan Hedbergd6268e82014-09-05 22:19:51 +03001569 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001570}
1571
Johan Hedberg70db83c2014-08-08 09:37:16 +03001572static void smp_ready_cb(struct l2cap_chan *chan)
1573{
1574 struct l2cap_conn *conn = chan->conn;
1575
1576 BT_DBG("chan %p", chan);
1577
1578 conn->smp = chan;
1579 l2cap_chan_hold(chan);
1580}
1581
Johan Hedberg4befb862014-08-11 22:06:38 +03001582static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
1583{
1584 int err;
1585
1586 BT_DBG("chan %p", chan);
1587
1588 err = smp_sig_channel(chan, skb);
1589 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03001590 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03001591
Johan Hedbergb68fda62014-08-11 22:06:40 +03001592 if (smp)
1593 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03001594
Johan Hedberg1e91c292014-08-18 20:33:29 +03001595 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03001596 }
1597
1598 return err;
1599}
1600
Johan Hedberg70db83c2014-08-08 09:37:16 +03001601static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
1602 unsigned long hdr_len,
1603 unsigned long len, int nb)
1604{
1605 struct sk_buff *skb;
1606
1607 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
1608 if (!skb)
1609 return ERR_PTR(-ENOMEM);
1610
1611 skb->priority = HCI_PRIO_MAX;
1612 bt_cb(skb)->chan = chan;
1613
1614 return skb;
1615}
1616
1617static const struct l2cap_ops smp_chan_ops = {
1618 .name = "Security Manager",
1619 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001620 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001621 .alloc_skb = smp_alloc_skb_cb,
1622 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001623 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001624
1625 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001626 .state_change = l2cap_chan_no_state_change,
1627 .close = l2cap_chan_no_close,
1628 .defer = l2cap_chan_no_defer,
1629 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001630 .set_shutdown = l2cap_chan_no_set_shutdown,
1631 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001632};
1633
1634static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
1635{
1636 struct l2cap_chan *chan;
1637
1638 BT_DBG("pchan %p", pchan);
1639
1640 chan = l2cap_chan_create();
1641 if (!chan)
1642 return NULL;
1643
1644 chan->chan_type = pchan->chan_type;
1645 chan->ops = &smp_chan_ops;
1646 chan->scid = pchan->scid;
1647 chan->dcid = chan->scid;
1648 chan->imtu = pchan->imtu;
1649 chan->omtu = pchan->omtu;
1650 chan->mode = pchan->mode;
1651
Johan Hedbergabe84902014-11-12 22:22:21 +02001652 /* Other L2CAP channels may request SMP routines in order to
1653 * change the security level. This means that the SMP channel
1654 * lock must be considered in its own category to avoid lockdep
1655 * warnings.
1656 */
1657 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
1658
Johan Hedberg70db83c2014-08-08 09:37:16 +03001659 BT_DBG("created chan %p", chan);
1660
1661 return chan;
1662}
1663
1664static const struct l2cap_ops smp_root_chan_ops = {
1665 .name = "Security Manager Root",
1666 .new_connection = smp_new_conn_cb,
1667
1668 /* None of these are implemented for the root channel */
1669 .close = l2cap_chan_no_close,
1670 .alloc_skb = l2cap_chan_no_alloc_skb,
1671 .recv = l2cap_chan_no_recv,
1672 .state_change = l2cap_chan_no_state_change,
1673 .teardown = l2cap_chan_no_teardown,
1674 .ready = l2cap_chan_no_ready,
1675 .defer = l2cap_chan_no_defer,
1676 .suspend = l2cap_chan_no_suspend,
1677 .resume = l2cap_chan_no_resume,
1678 .set_shutdown = l2cap_chan_no_set_shutdown,
1679 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03001680};
1681
Johan Hedberg711eafe2014-08-08 09:32:52 +03001682int smp_register(struct hci_dev *hdev)
1683{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001684 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001685 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001686
Johan Hedberg711eafe2014-08-08 09:32:52 +03001687 BT_DBG("%s", hdev->name);
1688
Johan Hedbergadae20c2014-11-13 14:37:48 +02001689 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001690 if (IS_ERR(tfm_aes)) {
1691 int err = PTR_ERR(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001692 BT_ERR("Unable to create crypto context");
Johan Hedberg711eafe2014-08-08 09:32:52 +03001693 return err;
1694 }
1695
Johan Hedberg70db83c2014-08-08 09:37:16 +03001696 chan = l2cap_chan_create();
1697 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001698 crypto_free_blkcipher(tfm_aes);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001699 return -ENOMEM;
1700 }
1701
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001702 chan->data = tfm_aes;
1703
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001704 l2cap_add_scid(chan, L2CAP_CID_SMP);
Johan Hedberg70db83c2014-08-08 09:37:16 +03001705
1706 l2cap_chan_set_defaults(chan);
1707
1708 bacpy(&chan->src, &hdev->bdaddr);
1709 chan->src_type = BDADDR_LE_PUBLIC;
1710 chan->state = BT_LISTEN;
1711 chan->mode = L2CAP_MODE_BASIC;
1712 chan->imtu = L2CAP_DEFAULT_MTU;
1713 chan->ops = &smp_root_chan_ops;
1714
Johan Hedbergabe84902014-11-12 22:22:21 +02001715 /* Set correct nesting level for a parent/listening channel */
1716 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
1717
Johan Hedberg70db83c2014-08-08 09:37:16 +03001718 hdev->smp_data = chan;
1719
Johan Hedberg711eafe2014-08-08 09:32:52 +03001720 return 0;
1721}
1722
1723void smp_unregister(struct hci_dev *hdev)
1724{
Johan Hedberg70db83c2014-08-08 09:37:16 +03001725 struct l2cap_chan *chan = hdev->smp_data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001726 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03001727
1728 if (!chan)
1729 return;
1730
1731 BT_DBG("%s chan %p", hdev->name, chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001732
Johan Hedbergdefce9e2014-08-08 09:37:17 +03001733 tfm_aes = chan->data;
1734 if (tfm_aes) {
1735 chan->data = NULL;
1736 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001737 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03001738
1739 hdev->smp_data = NULL;
1740 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03001741}