Commit | Line | Data |
---|---|---|
eb492e01 AB |
1 | /* |
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 | ||
8c520a59 GP |
23 | #include <linux/crypto.h> |
24 | #include <linux/scatterlist.h> | |
25 | #include <crypto/b128ops.h> | |
26 | ||
eb492e01 AB |
27 | #include <net/bluetooth/bluetooth.h> |
28 | #include <net/bluetooth/hci_core.h> | |
29 | #include <net/bluetooth/l2cap.h> | |
2b64d153 | 30 | #include <net/bluetooth/mgmt.h> |
ac4b7236 MH |
31 | |
32 | #include "smp.h" | |
d22ef0bc | 33 | |
b28b4943 | 34 | #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd) |
b28b4943 | 35 | |
17b02e62 | 36 | #define SMP_TIMEOUT msecs_to_jiffies(30000) |
5d3de7df | 37 | |
0edb14de JH |
38 | #define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \ |
39 | 0x1f : 0x07) | |
40 | #define KEY_DIST_MASK 0x07 | |
065a13e2 | 41 | |
533e35d4 JH |
42 | enum { |
43 | SMP_FLAG_TK_VALID, | |
44 | SMP_FLAG_CFM_PENDING, | |
45 | SMP_FLAG_MITM_AUTH, | |
46 | SMP_FLAG_COMPLETE, | |
47 | SMP_FLAG_INITIATOR, | |
48 | }; | |
4bc58f51 JH |
49 | |
50 | struct smp_chan { | |
b68fda68 JH |
51 | struct l2cap_conn *conn; |
52 | struct delayed_work security_timer; | |
b28b4943 | 53 | unsigned long allow_cmd; /* Bitmask of allowed commands */ |
b68fda68 | 54 | |
4bc58f51 JH |
55 | u8 preq[7]; /* SMP Pairing Request */ |
56 | u8 prsp[7]; /* SMP Pairing Response */ | |
57 | u8 prnd[16]; /* SMP Pairing Random (local) */ | |
58 | u8 rrnd[16]; /* SMP Pairing Random (remote) */ | |
59 | u8 pcnf[16]; /* SMP Pairing Confirm */ | |
60 | u8 tk[16]; /* SMP Temporary Key */ | |
61 | u8 enc_key_size; | |
62 | u8 remote_key_dist; | |
63 | bdaddr_t id_addr; | |
64 | u8 id_addr_type; | |
65 | u8 irk[16]; | |
66 | struct smp_csrk *csrk; | |
67 | struct smp_csrk *slave_csrk; | |
68 | struct smp_ltk *ltk; | |
69 | struct smp_ltk *slave_ltk; | |
70 | struct smp_irk *remote_irk; | |
4a74d658 | 71 | unsigned long flags; |
6a7bd103 JH |
72 | |
73 | struct crypto_blkcipher *tfm_aes; | |
4bc58f51 JH |
74 | }; |
75 | ||
8a2936f4 | 76 | static inline void swap_buf(const u8 *src, u8 *dst, size_t len) |
d22ef0bc | 77 | { |
8a2936f4 | 78 | size_t i; |
d22ef0bc | 79 | |
8a2936f4 JH |
80 | for (i = 0; i < len; i++) |
81 | dst[len - 1 - i] = src[i]; | |
d22ef0bc AB |
82 | } |
83 | ||
84 | static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) | |
85 | { | |
86 | struct blkcipher_desc desc; | |
87 | struct scatterlist sg; | |
943a732a | 88 | uint8_t tmp[16], data[16]; |
201a5929 | 89 | int err; |
d22ef0bc AB |
90 | |
91 | if (tfm == NULL) { | |
92 | BT_ERR("tfm %p", tfm); | |
93 | return -EINVAL; | |
94 | } | |
95 | ||
96 | desc.tfm = tfm; | |
97 | desc.flags = 0; | |
98 | ||
943a732a | 99 | /* The most significant octet of key corresponds to k[0] */ |
8a2936f4 | 100 | swap_buf(k, tmp, 16); |
943a732a JH |
101 | |
102 | err = crypto_blkcipher_setkey(tfm, tmp, 16); | |
d22ef0bc AB |
103 | if (err) { |
104 | BT_ERR("cipher setkey failed: %d", err); | |
105 | return err; | |
106 | } | |
107 | ||
943a732a | 108 | /* Most significant octet of plaintextData corresponds to data[0] */ |
8a2936f4 | 109 | swap_buf(r, data, 16); |
943a732a JH |
110 | |
111 | sg_init_one(&sg, data, 16); | |
d22ef0bc | 112 | |
d22ef0bc AB |
113 | err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); |
114 | if (err) | |
115 | BT_ERR("Encrypt data error %d", err); | |
116 | ||
943a732a | 117 | /* Most significant octet of encryptedData corresponds to data[0] */ |
8a2936f4 | 118 | swap_buf(data, r, 16); |
943a732a | 119 | |
d22ef0bc AB |
120 | return err; |
121 | } | |
122 | ||
60478054 JH |
123 | static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3]) |
124 | { | |
943a732a | 125 | u8 _res[16]; |
60478054 JH |
126 | int err; |
127 | ||
128 | /* r' = padding || r */ | |
943a732a JH |
129 | memcpy(_res, r, 3); |
130 | memset(_res + 3, 0, 13); | |
60478054 | 131 | |
943a732a | 132 | err = smp_e(tfm, irk, _res); |
60478054 JH |
133 | if (err) { |
134 | BT_ERR("Encrypt error"); | |
135 | return err; | |
136 | } | |
137 | ||
138 | /* The output of the random address function ah is: | |
139 | * ah(h, r) = e(k, r') mod 2^24 | |
140 | * The output of the security function e is then truncated to 24 bits | |
141 | * by taking the least significant 24 bits of the output of e as the | |
142 | * result of ah. | |
143 | */ | |
943a732a | 144 | memcpy(res, _res, 3); |
60478054 JH |
145 | |
146 | return 0; | |
147 | } | |
148 | ||
defce9e8 | 149 | bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr) |
60478054 | 150 | { |
defce9e8 JH |
151 | struct l2cap_chan *chan = hdev->smp_data; |
152 | struct crypto_blkcipher *tfm; | |
60478054 JH |
153 | u8 hash[3]; |
154 | int err; | |
155 | ||
defce9e8 JH |
156 | if (!chan || !chan->data) |
157 | return false; | |
158 | ||
159 | tfm = chan->data; | |
160 | ||
60478054 JH |
161 | BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); |
162 | ||
163 | err = smp_ah(tfm, irk, &bdaddr->b[3], hash); | |
164 | if (err) | |
165 | return false; | |
166 | ||
167 | return !memcmp(bdaddr->b, hash, 3); | |
168 | } | |
169 | ||
defce9e8 | 170 | int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa) |
b1e2b3ae | 171 | { |
defce9e8 JH |
172 | struct l2cap_chan *chan = hdev->smp_data; |
173 | struct crypto_blkcipher *tfm; | |
b1e2b3ae JH |
174 | int err; |
175 | ||
defce9e8 JH |
176 | if (!chan || !chan->data) |
177 | return -EOPNOTSUPP; | |
178 | ||
179 | tfm = chan->data; | |
180 | ||
b1e2b3ae JH |
181 | get_random_bytes(&rpa->b[3], 3); |
182 | ||
183 | rpa->b[5] &= 0x3f; /* Clear two most significant bits */ | |
184 | rpa->b[5] |= 0x40; /* Set second most significant bit */ | |
185 | ||
186 | err = smp_ah(tfm, irk, &rpa->b[3], rpa->b); | |
187 | if (err < 0) | |
188 | return err; | |
189 | ||
190 | BT_DBG("RPA %pMR", rpa); | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
e491eaf3 JH |
195 | static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16], |
196 | u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, | |
197 | bdaddr_t *ra, u8 res[16]) | |
d22ef0bc AB |
198 | { |
199 | u8 p1[16], p2[16]; | |
200 | int err; | |
201 | ||
202 | memset(p1, 0, 16); | |
203 | ||
204 | /* p1 = pres || preq || _rat || _iat */ | |
943a732a JH |
205 | p1[0] = _iat; |
206 | p1[1] = _rat; | |
207 | memcpy(p1 + 2, preq, 7); | |
208 | memcpy(p1 + 9, pres, 7); | |
d22ef0bc AB |
209 | |
210 | /* p2 = padding || ia || ra */ | |
943a732a JH |
211 | memcpy(p2, ra, 6); |
212 | memcpy(p2 + 6, ia, 6); | |
213 | memset(p2 + 12, 0, 4); | |
d22ef0bc AB |
214 | |
215 | /* res = r XOR p1 */ | |
216 | u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); | |
217 | ||
218 | /* res = e(k, res) */ | |
e491eaf3 | 219 | err = smp_e(tfm_aes, k, res); |
d22ef0bc AB |
220 | if (err) { |
221 | BT_ERR("Encrypt data error"); | |
222 | return err; | |
223 | } | |
224 | ||
225 | /* res = res XOR p2 */ | |
226 | u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); | |
227 | ||
228 | /* res = e(k, res) */ | |
e491eaf3 | 229 | err = smp_e(tfm_aes, k, res); |
d22ef0bc AB |
230 | if (err) |
231 | BT_ERR("Encrypt data error"); | |
232 | ||
233 | return err; | |
234 | } | |
235 | ||
e491eaf3 JH |
236 | static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16], |
237 | u8 r2[16], u8 _r[16]) | |
d22ef0bc AB |
238 | { |
239 | int err; | |
240 | ||
241 | /* Just least significant octets from r1 and r2 are considered */ | |
943a732a JH |
242 | memcpy(_r, r2, 8); |
243 | memcpy(_r + 8, r1, 8); | |
d22ef0bc | 244 | |
e491eaf3 | 245 | err = smp_e(tfm_aes, k, _r); |
d22ef0bc AB |
246 | if (err) |
247 | BT_ERR("Encrypt data error"); | |
248 | ||
249 | return err; | |
250 | } | |
251 | ||
5d88cc73 | 252 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) |
eb492e01 | 253 | { |
5d88cc73 | 254 | struct l2cap_chan *chan = conn->smp; |
b68fda68 | 255 | struct smp_chan *smp; |
5d88cc73 JH |
256 | struct kvec iv[2]; |
257 | struct msghdr msg; | |
eb492e01 | 258 | |
5d88cc73 JH |
259 | if (!chan) |
260 | return; | |
eb492e01 | 261 | |
5d88cc73 | 262 | BT_DBG("code 0x%2.2x", code); |
eb492e01 | 263 | |
5d88cc73 JH |
264 | iv[0].iov_base = &code; |
265 | iv[0].iov_len = 1; | |
eb492e01 | 266 | |
5d88cc73 JH |
267 | iv[1].iov_base = data; |
268 | iv[1].iov_len = len; | |
eb492e01 | 269 | |
5d88cc73 | 270 | memset(&msg, 0, sizeof(msg)); |
eb492e01 | 271 | |
5d88cc73 JH |
272 | msg.msg_iov = (struct iovec *) &iv; |
273 | msg.msg_iovlen = 2; | |
eb492e01 | 274 | |
5d88cc73 | 275 | l2cap_chan_send(chan, &msg, 1 + len); |
e2dcd113 | 276 | |
b68fda68 JH |
277 | if (!chan->data) |
278 | return; | |
279 | ||
280 | smp = chan->data; | |
281 | ||
282 | cancel_delayed_work_sync(&smp->security_timer); | |
1b0921d6 | 283 | schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT); |
eb492e01 AB |
284 | } |
285 | ||
2b64d153 BG |
286 | static __u8 authreq_to_seclevel(__u8 authreq) |
287 | { | |
288 | if (authreq & SMP_AUTH_MITM) | |
289 | return BT_SECURITY_HIGH; | |
290 | else | |
291 | return BT_SECURITY_MEDIUM; | |
292 | } | |
293 | ||
294 | static __u8 seclevel_to_authreq(__u8 sec_level) | |
295 | { | |
296 | switch (sec_level) { | |
297 | case BT_SECURITY_HIGH: | |
298 | return SMP_AUTH_MITM | SMP_AUTH_BONDING; | |
299 | case BT_SECURITY_MEDIUM: | |
300 | return SMP_AUTH_BONDING; | |
301 | default: | |
302 | return SMP_AUTH_NONE; | |
303 | } | |
304 | } | |
305 | ||
b8e66eac | 306 | static void build_pairing_cmd(struct l2cap_conn *conn, |
f1560463 MH |
307 | struct smp_cmd_pairing *req, |
308 | struct smp_cmd_pairing *rsp, __u8 authreq) | |
b8e66eac | 309 | { |
5d88cc73 JH |
310 | struct l2cap_chan *chan = conn->smp; |
311 | struct smp_chan *smp = chan->data; | |
fd349c02 JH |
312 | struct hci_conn *hcon = conn->hcon; |
313 | struct hci_dev *hdev = hcon->hdev; | |
314 | u8 local_dist = 0, remote_dist = 0; | |
54790f73 | 315 | |
b6ae8457 | 316 | if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) { |
7ee4ea36 MH |
317 | local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
318 | remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; | |
54790f73 | 319 | authreq |= SMP_AUTH_BONDING; |
2b64d153 BG |
320 | } else { |
321 | authreq &= ~SMP_AUTH_BONDING; | |
54790f73 VCG |
322 | } |
323 | ||
fd349c02 JH |
324 | if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) |
325 | remote_dist |= SMP_DIST_ID_KEY; | |
326 | ||
863efaf2 JH |
327 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
328 | local_dist |= SMP_DIST_ID_KEY; | |
329 | ||
54790f73 VCG |
330 | if (rsp == NULL) { |
331 | req->io_capability = conn->hcon->io_capability; | |
332 | req->oob_flag = SMP_OOB_NOT_PRESENT; | |
333 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
fd349c02 JH |
334 | req->init_key_dist = local_dist; |
335 | req->resp_key_dist = remote_dist; | |
0edb14de | 336 | req->auth_req = (authreq & AUTH_REQ_MASK(hdev)); |
fd349c02 JH |
337 | |
338 | smp->remote_key_dist = remote_dist; | |
54790f73 VCG |
339 | return; |
340 | } | |
341 | ||
342 | rsp->io_capability = conn->hcon->io_capability; | |
343 | rsp->oob_flag = SMP_OOB_NOT_PRESENT; | |
344 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
fd349c02 JH |
345 | rsp->init_key_dist = req->init_key_dist & remote_dist; |
346 | rsp->resp_key_dist = req->resp_key_dist & local_dist; | |
0edb14de | 347 | rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev)); |
fd349c02 JH |
348 | |
349 | smp->remote_key_dist = rsp->init_key_dist; | |
b8e66eac VCG |
350 | } |
351 | ||
3158c50c VCG |
352 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
353 | { | |
5d88cc73 JH |
354 | struct l2cap_chan *chan = conn->smp; |
355 | struct smp_chan *smp = chan->data; | |
1c1def09 | 356 | |
3158c50c | 357 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
f1560463 | 358 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
3158c50c VCG |
359 | return SMP_ENC_KEY_SIZE; |
360 | ||
f7aa611a | 361 | smp->enc_key_size = max_key_size; |
3158c50c VCG |
362 | |
363 | return 0; | |
364 | } | |
365 | ||
6f48e260 JH |
366 | static void smp_chan_destroy(struct l2cap_conn *conn) |
367 | { | |
368 | struct l2cap_chan *chan = conn->smp; | |
369 | struct smp_chan *smp = chan->data; | |
370 | bool complete; | |
371 | ||
372 | BUG_ON(!smp); | |
373 | ||
374 | cancel_delayed_work_sync(&smp->security_timer); | |
6f48e260 | 375 | |
6f48e260 JH |
376 | complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); |
377 | mgmt_smp_complete(conn->hcon, complete); | |
378 | ||
379 | kfree(smp->csrk); | |
380 | kfree(smp->slave_csrk); | |
381 | ||
382 | crypto_free_blkcipher(smp->tfm_aes); | |
383 | ||
384 | /* If pairing failed clean up any keys we might have */ | |
385 | if (!complete) { | |
386 | if (smp->ltk) { | |
970d0f1b JH |
387 | list_del_rcu(&smp->ltk->list); |
388 | kfree_rcu(smp->ltk, rcu); | |
6f48e260 JH |
389 | } |
390 | ||
391 | if (smp->slave_ltk) { | |
970d0f1b JH |
392 | list_del_rcu(&smp->slave_ltk->list); |
393 | kfree_rcu(smp->slave_ltk, rcu); | |
6f48e260 JH |
394 | } |
395 | ||
396 | if (smp->remote_irk) { | |
adae20cb JH |
397 | list_del_rcu(&smp->remote_irk->list); |
398 | kfree_rcu(smp->remote_irk, rcu); | |
6f48e260 JH |
399 | } |
400 | } | |
401 | ||
402 | chan->data = NULL; | |
403 | kfree(smp); | |
404 | hci_conn_drop(conn->hcon); | |
405 | } | |
406 | ||
84794e11 | 407 | static void smp_failure(struct l2cap_conn *conn, u8 reason) |
4f957a76 | 408 | { |
bab73cb6 | 409 | struct hci_conn *hcon = conn->hcon; |
b68fda68 | 410 | struct l2cap_chan *chan = conn->smp; |
bab73cb6 | 411 | |
84794e11 | 412 | if (reason) |
4f957a76 | 413 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
f1560463 | 414 | &reason); |
4f957a76 | 415 | |
ce39fb4e | 416 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags); |
e1e930f5 | 417 | mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); |
f1c09c07 | 418 | |
fc75cc86 | 419 | if (chan->data) |
f1c09c07 | 420 | smp_chan_destroy(conn); |
4f957a76 BG |
421 | } |
422 | ||
2b64d153 BG |
423 | #define JUST_WORKS 0x00 |
424 | #define JUST_CFM 0x01 | |
425 | #define REQ_PASSKEY 0x02 | |
426 | #define CFM_PASSKEY 0x03 | |
427 | #define REQ_OOB 0x04 | |
428 | #define OVERLAP 0xFF | |
429 | ||
430 | static const u8 gen_method[5][5] = { | |
431 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
432 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
433 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, | |
434 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, | |
435 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, | |
436 | }; | |
437 | ||
581370cc JH |
438 | static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io) |
439 | { | |
2bcd4003 JH |
440 | /* If either side has unknown io_caps, use JUST_CFM (which gets |
441 | * converted later to JUST_WORKS if we're initiators. | |
442 | */ | |
581370cc JH |
443 | if (local_io > SMP_IO_KEYBOARD_DISPLAY || |
444 | remote_io > SMP_IO_KEYBOARD_DISPLAY) | |
2bcd4003 | 445 | return JUST_CFM; |
581370cc JH |
446 | |
447 | return gen_method[remote_io][local_io]; | |
448 | } | |
449 | ||
2b64d153 BG |
450 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, |
451 | u8 local_io, u8 remote_io) | |
452 | { | |
453 | struct hci_conn *hcon = conn->hcon; | |
5d88cc73 JH |
454 | struct l2cap_chan *chan = conn->smp; |
455 | struct smp_chan *smp = chan->data; | |
2b64d153 BG |
456 | u8 method; |
457 | u32 passkey = 0; | |
458 | int ret = 0; | |
459 | ||
460 | /* Initialize key for JUST WORKS */ | |
461 | memset(smp->tk, 0, sizeof(smp->tk)); | |
4a74d658 | 462 | clear_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
463 | |
464 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); | |
465 | ||
2bcd4003 JH |
466 | /* If neither side wants MITM, either "just" confirm an incoming |
467 | * request or use just-works for outgoing ones. The JUST_CFM | |
468 | * will be converted to JUST_WORKS if necessary later in this | |
469 | * function. If either side has MITM look up the method from the | |
470 | * table. | |
471 | */ | |
581370cc | 472 | if (!(auth & SMP_AUTH_MITM)) |
2bcd4003 | 473 | method = JUST_CFM; |
2b64d153 | 474 | else |
581370cc | 475 | method = get_auth_method(smp, local_io, remote_io); |
2b64d153 | 476 | |
a82505c7 | 477 | /* Don't confirm locally initiated pairing attempts */ |
4a74d658 | 478 | if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) |
a82505c7 JH |
479 | method = JUST_WORKS; |
480 | ||
02f3e254 JH |
481 | /* Don't bother user space with no IO capabilities */ |
482 | if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) | |
483 | method = JUST_WORKS; | |
484 | ||
2b64d153 BG |
485 | /* If Just Works, Continue with Zero TK */ |
486 | if (method == JUST_WORKS) { | |
4a74d658 | 487 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
488 | return 0; |
489 | } | |
490 | ||
491 | /* Not Just Works/Confirm results in MITM Authentication */ | |
5eb596f5 | 492 | if (method != JUST_CFM) { |
4a74d658 | 493 | set_bit(SMP_FLAG_MITM_AUTH, &smp->flags); |
5eb596f5 JH |
494 | if (hcon->pending_sec_level < BT_SECURITY_HIGH) |
495 | hcon->pending_sec_level = BT_SECURITY_HIGH; | |
496 | } | |
2b64d153 BG |
497 | |
498 | /* If both devices have Keyoard-Display I/O, the master | |
499 | * Confirms and the slave Enters the passkey. | |
500 | */ | |
501 | if (method == OVERLAP) { | |
40bef302 | 502 | if (hcon->role == HCI_ROLE_MASTER) |
2b64d153 BG |
503 | method = CFM_PASSKEY; |
504 | else | |
505 | method = REQ_PASSKEY; | |
506 | } | |
507 | ||
01ad34d2 | 508 | /* Generate random passkey. */ |
2b64d153 | 509 | if (method == CFM_PASSKEY) { |
943a732a | 510 | memset(smp->tk, 0, sizeof(smp->tk)); |
2b64d153 BG |
511 | get_random_bytes(&passkey, sizeof(passkey)); |
512 | passkey %= 1000000; | |
943a732a | 513 | put_unaligned_le32(passkey, smp->tk); |
2b64d153 | 514 | BT_DBG("PassKey: %d", passkey); |
4a74d658 | 515 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
516 | } |
517 | ||
2b64d153 | 518 | if (method == REQ_PASSKEY) |
ce39fb4e | 519 | ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, |
272d90df | 520 | hcon->type, hcon->dst_type); |
4eb65e66 JH |
521 | else if (method == JUST_CFM) |
522 | ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, | |
523 | hcon->type, hcon->dst_type, | |
524 | passkey, 1); | |
2b64d153 | 525 | else |
01ad34d2 | 526 | ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst, |
272d90df | 527 | hcon->type, hcon->dst_type, |
39adbffe | 528 | passkey, 0); |
2b64d153 | 529 | |
2b64d153 BG |
530 | return ret; |
531 | } | |
532 | ||
1cc61144 | 533 | static u8 smp_confirm(struct smp_chan *smp) |
8aab4757 | 534 | { |
8aab4757 | 535 | struct l2cap_conn *conn = smp->conn; |
8aab4757 VCG |
536 | struct smp_cmd_pairing_confirm cp; |
537 | int ret; | |
8aab4757 VCG |
538 | |
539 | BT_DBG("conn %p", conn); | |
540 | ||
e491eaf3 | 541 | ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp, |
b1cd5fd9 | 542 | conn->hcon->init_addr_type, &conn->hcon->init_addr, |
943a732a JH |
543 | conn->hcon->resp_addr_type, &conn->hcon->resp_addr, |
544 | cp.confirm_val); | |
1cc61144 JH |
545 | if (ret) |
546 | return SMP_UNSPECIFIED; | |
8aab4757 | 547 | |
4a74d658 | 548 | clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
2b64d153 | 549 | |
8aab4757 VCG |
550 | smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); |
551 | ||
b28b4943 JH |
552 | if (conn->hcon->out) |
553 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); | |
554 | else | |
555 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); | |
556 | ||
1cc61144 | 557 | return 0; |
8aab4757 VCG |
558 | } |
559 | ||
861580a9 | 560 | static u8 smp_random(struct smp_chan *smp) |
8aab4757 | 561 | { |
8aab4757 VCG |
562 | struct l2cap_conn *conn = smp->conn; |
563 | struct hci_conn *hcon = conn->hcon; | |
861580a9 | 564 | u8 confirm[16]; |
8aab4757 VCG |
565 | int ret; |
566 | ||
ec70f36f | 567 | if (IS_ERR_OR_NULL(smp->tfm_aes)) |
861580a9 | 568 | return SMP_UNSPECIFIED; |
8aab4757 VCG |
569 | |
570 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); | |
571 | ||
e491eaf3 | 572 | ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
b1cd5fd9 | 573 | hcon->init_addr_type, &hcon->init_addr, |
943a732a | 574 | hcon->resp_addr_type, &hcon->resp_addr, confirm); |
861580a9 JH |
575 | if (ret) |
576 | return SMP_UNSPECIFIED; | |
8aab4757 | 577 | |
8aab4757 VCG |
578 | if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { |
579 | BT_ERR("Pairing failed (confirmation values mismatch)"); | |
861580a9 | 580 | return SMP_CONFIRM_FAILED; |
8aab4757 VCG |
581 | } |
582 | ||
583 | if (hcon->out) { | |
fe39c7b2 MH |
584 | u8 stk[16]; |
585 | __le64 rand = 0; | |
586 | __le16 ediv = 0; | |
8aab4757 | 587 | |
e491eaf3 | 588 | smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk); |
8aab4757 | 589 | |
f7aa611a | 590 | memset(stk + smp->enc_key_size, 0, |
04124681 | 591 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 592 | |
861580a9 JH |
593 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
594 | return SMP_UNSPECIFIED; | |
8aab4757 VCG |
595 | |
596 | hci_le_start_enc(hcon, ediv, rand, stk); | |
f7aa611a | 597 | hcon->enc_key_size = smp->enc_key_size; |
fe59a05f | 598 | set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); |
8aab4757 | 599 | } else { |
fff3490f | 600 | u8 stk[16], auth; |
fe39c7b2 MH |
601 | __le64 rand = 0; |
602 | __le16 ediv = 0; | |
8aab4757 | 603 | |
943a732a JH |
604 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
605 | smp->prnd); | |
8aab4757 | 606 | |
e491eaf3 | 607 | smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk); |
8aab4757 | 608 | |
f7aa611a | 609 | memset(stk + smp->enc_key_size, 0, |
f1560463 | 610 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 611 | |
fff3490f JH |
612 | if (hcon->pending_sec_level == BT_SECURITY_HIGH) |
613 | auth = 1; | |
614 | else | |
615 | auth = 0; | |
616 | ||
7d5843b7 JH |
617 | /* Even though there's no _SLAVE suffix this is the |
618 | * slave STK we're adding for later lookup (the master | |
619 | * STK never needs to be stored). | |
620 | */ | |
ce39fb4e | 621 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
2ceba539 | 622 | SMP_STK, auth, stk, smp->enc_key_size, ediv, rand); |
8aab4757 VCG |
623 | } |
624 | ||
861580a9 | 625 | return 0; |
8aab4757 VCG |
626 | } |
627 | ||
44f1a7ab JH |
628 | static void smp_notify_keys(struct l2cap_conn *conn) |
629 | { | |
630 | struct l2cap_chan *chan = conn->smp; | |
631 | struct smp_chan *smp = chan->data; | |
632 | struct hci_conn *hcon = conn->hcon; | |
633 | struct hci_dev *hdev = hcon->hdev; | |
634 | struct smp_cmd_pairing *req = (void *) &smp->preq[1]; | |
635 | struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1]; | |
636 | bool persistent; | |
637 | ||
638 | if (smp->remote_irk) { | |
639 | mgmt_new_irk(hdev, smp->remote_irk); | |
640 | /* Now that user space can be considered to know the | |
641 | * identity address track the connection based on it | |
642 | * from now on. | |
643 | */ | |
644 | bacpy(&hcon->dst, &smp->remote_irk->bdaddr); | |
645 | hcon->dst_type = smp->remote_irk->addr_type; | |
f3d82d0c | 646 | queue_work(hdev->workqueue, &conn->id_addr_update_work); |
44f1a7ab JH |
647 | |
648 | /* When receiving an indentity resolving key for | |
649 | * a remote device that does not use a resolvable | |
650 | * private address, just remove the key so that | |
651 | * it is possible to use the controller white | |
652 | * list for scanning. | |
653 | * | |
654 | * Userspace will have been told to not store | |
655 | * this key at this point. So it is safe to | |
656 | * just remove it. | |
657 | */ | |
658 | if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) { | |
adae20cb JH |
659 | list_del_rcu(&smp->remote_irk->list); |
660 | kfree_rcu(smp->remote_irk, rcu); | |
44f1a7ab JH |
661 | smp->remote_irk = NULL; |
662 | } | |
663 | } | |
664 | ||
665 | /* The LTKs and CSRKs should be persistent only if both sides | |
666 | * had the bonding bit set in their authentication requests. | |
667 | */ | |
668 | persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING); | |
669 | ||
670 | if (smp->csrk) { | |
671 | smp->csrk->bdaddr_type = hcon->dst_type; | |
672 | bacpy(&smp->csrk->bdaddr, &hcon->dst); | |
673 | mgmt_new_csrk(hdev, smp->csrk, persistent); | |
674 | } | |
675 | ||
676 | if (smp->slave_csrk) { | |
677 | smp->slave_csrk->bdaddr_type = hcon->dst_type; | |
678 | bacpy(&smp->slave_csrk->bdaddr, &hcon->dst); | |
679 | mgmt_new_csrk(hdev, smp->slave_csrk, persistent); | |
680 | } | |
681 | ||
682 | if (smp->ltk) { | |
683 | smp->ltk->bdaddr_type = hcon->dst_type; | |
684 | bacpy(&smp->ltk->bdaddr, &hcon->dst); | |
685 | mgmt_new_ltk(hdev, smp->ltk, persistent); | |
686 | } | |
687 | ||
688 | if (smp->slave_ltk) { | |
689 | smp->slave_ltk->bdaddr_type = hcon->dst_type; | |
690 | bacpy(&smp->slave_ltk->bdaddr, &hcon->dst); | |
691 | mgmt_new_ltk(hdev, smp->slave_ltk, persistent); | |
692 | } | |
693 | } | |
694 | ||
b28b4943 JH |
695 | static void smp_allow_key_dist(struct smp_chan *smp) |
696 | { | |
697 | /* Allow the first expected phase 3 PDU. The rest of the PDUs | |
698 | * will be allowed in each PDU handler to ensure we receive | |
699 | * them in the correct order. | |
700 | */ | |
701 | if (smp->remote_key_dist & SMP_DIST_ENC_KEY) | |
702 | SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO); | |
703 | else if (smp->remote_key_dist & SMP_DIST_ID_KEY) | |
704 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); | |
705 | else if (smp->remote_key_dist & SMP_DIST_SIGN) | |
706 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); | |
707 | } | |
708 | ||
d6268e86 | 709 | static void smp_distribute_keys(struct smp_chan *smp) |
44f1a7ab JH |
710 | { |
711 | struct smp_cmd_pairing *req, *rsp; | |
86d1407c | 712 | struct l2cap_conn *conn = smp->conn; |
44f1a7ab JH |
713 | struct hci_conn *hcon = conn->hcon; |
714 | struct hci_dev *hdev = hcon->hdev; | |
715 | __u8 *keydist; | |
716 | ||
717 | BT_DBG("conn %p", conn); | |
718 | ||
44f1a7ab JH |
719 | rsp = (void *) &smp->prsp[1]; |
720 | ||
721 | /* The responder sends its keys first */ | |
b28b4943 JH |
722 | if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) { |
723 | smp_allow_key_dist(smp); | |
86d1407c | 724 | return; |
b28b4943 | 725 | } |
44f1a7ab JH |
726 | |
727 | req = (void *) &smp->preq[1]; | |
728 | ||
729 | if (hcon->out) { | |
730 | keydist = &rsp->init_key_dist; | |
731 | *keydist &= req->init_key_dist; | |
732 | } else { | |
733 | keydist = &rsp->resp_key_dist; | |
734 | *keydist &= req->resp_key_dist; | |
735 | } | |
736 | ||
737 | BT_DBG("keydist 0x%x", *keydist); | |
738 | ||
739 | if (*keydist & SMP_DIST_ENC_KEY) { | |
740 | struct smp_cmd_encrypt_info enc; | |
741 | struct smp_cmd_master_ident ident; | |
742 | struct smp_ltk *ltk; | |
743 | u8 authenticated; | |
744 | __le16 ediv; | |
745 | __le64 rand; | |
746 | ||
747 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); | |
748 | get_random_bytes(&ediv, sizeof(ediv)); | |
749 | get_random_bytes(&rand, sizeof(rand)); | |
750 | ||
751 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); | |
752 | ||
753 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; | |
754 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, | |
755 | SMP_LTK_SLAVE, authenticated, enc.ltk, | |
756 | smp->enc_key_size, ediv, rand); | |
757 | smp->slave_ltk = ltk; | |
758 | ||
759 | ident.ediv = ediv; | |
760 | ident.rand = rand; | |
761 | ||
762 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); | |
763 | ||
764 | *keydist &= ~SMP_DIST_ENC_KEY; | |
765 | } | |
766 | ||
767 | if (*keydist & SMP_DIST_ID_KEY) { | |
768 | struct smp_cmd_ident_addr_info addrinfo; | |
769 | struct smp_cmd_ident_info idinfo; | |
770 | ||
771 | memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk)); | |
772 | ||
773 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); | |
774 | ||
775 | /* The hci_conn contains the local identity address | |
776 | * after the connection has been established. | |
777 | * | |
778 | * This is true even when the connection has been | |
779 | * established using a resolvable random address. | |
780 | */ | |
781 | bacpy(&addrinfo.bdaddr, &hcon->src); | |
782 | addrinfo.addr_type = hcon->src_type; | |
783 | ||
784 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), | |
785 | &addrinfo); | |
786 | ||
787 | *keydist &= ~SMP_DIST_ID_KEY; | |
788 | } | |
789 | ||
790 | if (*keydist & SMP_DIST_SIGN) { | |
791 | struct smp_cmd_sign_info sign; | |
792 | struct smp_csrk *csrk; | |
793 | ||
794 | /* Generate a new random key */ | |
795 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); | |
796 | ||
797 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); | |
798 | if (csrk) { | |
799 | csrk->master = 0x00; | |
800 | memcpy(csrk->val, sign.csrk, sizeof(csrk->val)); | |
801 | } | |
802 | smp->slave_csrk = csrk; | |
803 | ||
804 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); | |
805 | ||
806 | *keydist &= ~SMP_DIST_SIGN; | |
807 | } | |
808 | ||
809 | /* If there are still keys to be received wait for them */ | |
b28b4943 JH |
810 | if (smp->remote_key_dist & KEY_DIST_MASK) { |
811 | smp_allow_key_dist(smp); | |
86d1407c | 812 | return; |
b28b4943 | 813 | } |
44f1a7ab | 814 | |
44f1a7ab JH |
815 | set_bit(SMP_FLAG_COMPLETE, &smp->flags); |
816 | smp_notify_keys(conn); | |
817 | ||
818 | smp_chan_destroy(conn); | |
44f1a7ab JH |
819 | } |
820 | ||
b68fda68 JH |
821 | static void smp_timeout(struct work_struct *work) |
822 | { | |
823 | struct smp_chan *smp = container_of(work, struct smp_chan, | |
824 | security_timer.work); | |
825 | struct l2cap_conn *conn = smp->conn; | |
826 | ||
827 | BT_DBG("conn %p", conn); | |
828 | ||
1e91c29e | 829 | hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM); |
b68fda68 JH |
830 | } |
831 | ||
8aab4757 VCG |
832 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) |
833 | { | |
5d88cc73 | 834 | struct l2cap_chan *chan = conn->smp; |
8aab4757 VCG |
835 | struct smp_chan *smp; |
836 | ||
f1560463 | 837 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); |
fc75cc86 | 838 | if (!smp) |
8aab4757 VCG |
839 | return NULL; |
840 | ||
6a7bd103 JH |
841 | smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); |
842 | if (IS_ERR(smp->tfm_aes)) { | |
843 | BT_ERR("Unable to create ECB crypto context"); | |
844 | kfree(smp); | |
845 | return NULL; | |
846 | } | |
847 | ||
8aab4757 | 848 | smp->conn = conn; |
5d88cc73 | 849 | chan->data = smp; |
8aab4757 | 850 | |
b28b4943 JH |
851 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL); |
852 | ||
b68fda68 JH |
853 | INIT_DELAYED_WORK(&smp->security_timer, smp_timeout); |
854 | ||
8aab4757 VCG |
855 | hci_conn_hold(conn->hcon); |
856 | ||
857 | return smp; | |
858 | } | |
859 | ||
2b64d153 BG |
860 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
861 | { | |
b10e8017 | 862 | struct l2cap_conn *conn = hcon->l2cap_data; |
5d88cc73 | 863 | struct l2cap_chan *chan; |
2b64d153 BG |
864 | struct smp_chan *smp; |
865 | u32 value; | |
fc75cc86 | 866 | int err; |
2b64d153 BG |
867 | |
868 | BT_DBG(""); | |
869 | ||
fc75cc86 | 870 | if (!conn) |
2b64d153 BG |
871 | return -ENOTCONN; |
872 | ||
5d88cc73 JH |
873 | chan = conn->smp; |
874 | if (!chan) | |
875 | return -ENOTCONN; | |
876 | ||
fc75cc86 JH |
877 | l2cap_chan_lock(chan); |
878 | if (!chan->data) { | |
879 | err = -ENOTCONN; | |
880 | goto unlock; | |
881 | } | |
882 | ||
5d88cc73 | 883 | smp = chan->data; |
2b64d153 BG |
884 | |
885 | switch (mgmt_op) { | |
886 | case MGMT_OP_USER_PASSKEY_REPLY: | |
887 | value = le32_to_cpu(passkey); | |
943a732a | 888 | memset(smp->tk, 0, sizeof(smp->tk)); |
2b64d153 | 889 | BT_DBG("PassKey: %d", value); |
943a732a | 890 | put_unaligned_le32(value, smp->tk); |
2b64d153 BG |
891 | /* Fall Through */ |
892 | case MGMT_OP_USER_CONFIRM_REPLY: | |
4a74d658 | 893 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
2b64d153 BG |
894 | break; |
895 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: | |
896 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: | |
84794e11 | 897 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
fc75cc86 JH |
898 | err = 0; |
899 | goto unlock; | |
2b64d153 | 900 | default: |
84794e11 | 901 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
fc75cc86 JH |
902 | err = -EOPNOTSUPP; |
903 | goto unlock; | |
2b64d153 BG |
904 | } |
905 | ||
fc75cc86 JH |
906 | err = 0; |
907 | ||
2b64d153 | 908 | /* If it is our turn to send Pairing Confirm, do so now */ |
1cc61144 JH |
909 | if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) { |
910 | u8 rsp = smp_confirm(smp); | |
911 | if (rsp) | |
912 | smp_failure(conn, rsp); | |
913 | } | |
2b64d153 | 914 | |
fc75cc86 JH |
915 | unlock: |
916 | l2cap_chan_unlock(chan); | |
917 | return err; | |
2b64d153 BG |
918 | } |
919 | ||
da85e5e5 | 920 | static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 921 | { |
3158c50c | 922 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
fc75cc86 | 923 | struct l2cap_chan *chan = conn->smp; |
b3c6410b | 924 | struct hci_dev *hdev = conn->hcon->hdev; |
8aab4757 | 925 | struct smp_chan *smp; |
c7262e71 | 926 | u8 key_size, auth, sec_level; |
8aab4757 | 927 | int ret; |
88ba43b6 AB |
928 | |
929 | BT_DBG("conn %p", conn); | |
930 | ||
c46b98be | 931 | if (skb->len < sizeof(*req)) |
38e4a915 | 932 | return SMP_INVALID_PARAMS; |
c46b98be | 933 | |
40bef302 | 934 | if (conn->hcon->role != HCI_ROLE_SLAVE) |
2b64d153 BG |
935 | return SMP_CMD_NOTSUPP; |
936 | ||
fc75cc86 | 937 | if (!chan->data) |
8aab4757 | 938 | smp = smp_chan_create(conn); |
fc75cc86 | 939 | else |
5d88cc73 | 940 | smp = chan->data; |
8aab4757 | 941 | |
d08fd0e7 AE |
942 | if (!smp) |
943 | return SMP_UNSPECIFIED; | |
d26a2345 | 944 | |
c05b9339 | 945 | /* We didn't start the pairing, so match remote */ |
0edb14de | 946 | auth = req->auth_req & AUTH_REQ_MASK(hdev); |
c05b9339 | 947 | |
b6ae8457 | 948 | if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) && |
c05b9339 | 949 | (auth & SMP_AUTH_BONDING)) |
b3c6410b JH |
950 | return SMP_PAIRING_NOTSUPP; |
951 | ||
1c1def09 VCG |
952 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
953 | memcpy(&smp->preq[1], req, sizeof(*req)); | |
3158c50c | 954 | skb_pull(skb, sizeof(*req)); |
88ba43b6 | 955 | |
5be5e275 | 956 | if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
1afc2a1a JH |
957 | sec_level = BT_SECURITY_MEDIUM; |
958 | else | |
959 | sec_level = authreq_to_seclevel(auth); | |
960 | ||
c7262e71 JH |
961 | if (sec_level > conn->hcon->pending_sec_level) |
962 | conn->hcon->pending_sec_level = sec_level; | |
fdde0a26 | 963 | |
49c922bb | 964 | /* If we need MITM check that it can be achieved */ |
2ed8f65c JH |
965 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { |
966 | u8 method; | |
967 | ||
968 | method = get_auth_method(smp, conn->hcon->io_capability, | |
969 | req->io_capability); | |
970 | if (method == JUST_WORKS || method == JUST_CFM) | |
971 | return SMP_AUTH_REQUIREMENTS; | |
972 | } | |
973 | ||
2b64d153 | 974 | build_pairing_cmd(conn, req, &rsp, auth); |
3158c50c VCG |
975 | |
976 | key_size = min(req->max_key_size, rsp.max_key_size); | |
977 | if (check_enc_key_size(conn, key_size)) | |
978 | return SMP_ENC_KEY_SIZE; | |
88ba43b6 | 979 | |
e84a6b13 | 980 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
8aab4757 | 981 | |
1c1def09 VCG |
982 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
983 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); | |
f01ead31 | 984 | |
3158c50c | 985 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
b28b4943 | 986 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
da85e5e5 | 987 | |
2b64d153 BG |
988 | /* Request setup of TK */ |
989 | ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); | |
990 | if (ret) | |
991 | return SMP_UNSPECIFIED; | |
992 | ||
da85e5e5 | 993 | return 0; |
88ba43b6 AB |
994 | } |
995 | ||
da85e5e5 | 996 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 997 | { |
3158c50c | 998 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
5d88cc73 JH |
999 | struct l2cap_chan *chan = conn->smp; |
1000 | struct smp_chan *smp = chan->data; | |
0edb14de | 1001 | struct hci_dev *hdev = conn->hcon->hdev; |
3a7dbfb8 | 1002 | u8 key_size, auth; |
7d24ddcc | 1003 | int ret; |
88ba43b6 AB |
1004 | |
1005 | BT_DBG("conn %p", conn); | |
1006 | ||
c46b98be | 1007 | if (skb->len < sizeof(*rsp)) |
38e4a915 | 1008 | return SMP_INVALID_PARAMS; |
c46b98be | 1009 | |
40bef302 | 1010 | if (conn->hcon->role != HCI_ROLE_MASTER) |
2b64d153 BG |
1011 | return SMP_CMD_NOTSUPP; |
1012 | ||
3158c50c VCG |
1013 | skb_pull(skb, sizeof(*rsp)); |
1014 | ||
1c1def09 | 1015 | req = (void *) &smp->preq[1]; |
da85e5e5 | 1016 | |
3158c50c VCG |
1017 | key_size = min(req->max_key_size, rsp->max_key_size); |
1018 | if (check_enc_key_size(conn, key_size)) | |
1019 | return SMP_ENC_KEY_SIZE; | |
1020 | ||
0edb14de | 1021 | auth = rsp->auth_req & AUTH_REQ_MASK(hdev); |
c05b9339 | 1022 | |
49c922bb | 1023 | /* If we need MITM check that it can be achieved */ |
2ed8f65c JH |
1024 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { |
1025 | u8 method; | |
1026 | ||
1027 | method = get_auth_method(smp, req->io_capability, | |
1028 | rsp->io_capability); | |
1029 | if (method == JUST_WORKS || method == JUST_CFM) | |
1030 | return SMP_AUTH_REQUIREMENTS; | |
1031 | } | |
1032 | ||
e84a6b13 | 1033 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
7d24ddcc | 1034 | |
8aab4757 VCG |
1035 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
1036 | memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); | |
7d24ddcc | 1037 | |
fdcc4bec JH |
1038 | /* Update remote key distribution in case the remote cleared |
1039 | * some bits that we had enabled in our request. | |
1040 | */ | |
1041 | smp->remote_key_dist &= rsp->resp_key_dist; | |
1042 | ||
c05b9339 | 1043 | auth |= req->auth_req; |
2b64d153 | 1044 | |
476585ec | 1045 | ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); |
2b64d153 BG |
1046 | if (ret) |
1047 | return SMP_UNSPECIFIED; | |
1048 | ||
4a74d658 | 1049 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
2b64d153 BG |
1050 | |
1051 | /* Can't compose response until we have been confirmed */ | |
4a74d658 | 1052 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) |
1cc61144 | 1053 | return smp_confirm(smp); |
da85e5e5 VCG |
1054 | |
1055 | return 0; | |
88ba43b6 AB |
1056 | } |
1057 | ||
da85e5e5 | 1058 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 1059 | { |
5d88cc73 JH |
1060 | struct l2cap_chan *chan = conn->smp; |
1061 | struct smp_chan *smp = chan->data; | |
7d24ddcc | 1062 | |
88ba43b6 AB |
1063 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
1064 | ||
c46b98be | 1065 | if (skb->len < sizeof(smp->pcnf)) |
38e4a915 | 1066 | return SMP_INVALID_PARAMS; |
c46b98be | 1067 | |
1c1def09 VCG |
1068 | memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); |
1069 | skb_pull(skb, sizeof(smp->pcnf)); | |
88ba43b6 | 1070 | |
b28b4943 | 1071 | if (conn->hcon->out) { |
943a732a JH |
1072 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
1073 | smp->prnd); | |
b28b4943 JH |
1074 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
1075 | return 0; | |
1076 | } | |
1077 | ||
1078 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) | |
1cc61144 | 1079 | return smp_confirm(smp); |
943a732a | 1080 | else |
4a74d658 | 1081 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
da85e5e5 VCG |
1082 | |
1083 | return 0; | |
88ba43b6 AB |
1084 | } |
1085 | ||
da85e5e5 | 1086 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 1087 | { |
5d88cc73 JH |
1088 | struct l2cap_chan *chan = conn->smp; |
1089 | struct smp_chan *smp = chan->data; | |
7d24ddcc | 1090 | |
8aab4757 | 1091 | BT_DBG("conn %p", conn); |
3158c50c | 1092 | |
c46b98be | 1093 | if (skb->len < sizeof(smp->rrnd)) |
38e4a915 | 1094 | return SMP_INVALID_PARAMS; |
c46b98be | 1095 | |
943a732a | 1096 | memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd)); |
8aab4757 | 1097 | skb_pull(skb, sizeof(smp->rrnd)); |
e7e62c85 | 1098 | |
861580a9 | 1099 | return smp_random(smp); |
88ba43b6 AB |
1100 | } |
1101 | ||
f81cd823 | 1102 | static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) |
988c5997 | 1103 | { |
c9839a11 | 1104 | struct smp_ltk *key; |
988c5997 VCG |
1105 | struct hci_conn *hcon = conn->hcon; |
1106 | ||
98a0b845 | 1107 | key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type, |
e804d25d | 1108 | hcon->role); |
988c5997 | 1109 | if (!key) |
f81cd823 | 1110 | return false; |
988c5997 | 1111 | |
a6f7833c | 1112 | if (smp_ltk_sec_level(key) < sec_level) |
f81cd823 | 1113 | return false; |
4dab7864 | 1114 | |
51a8efd7 | 1115 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
f81cd823 | 1116 | return true; |
988c5997 | 1117 | |
c9839a11 VCG |
1118 | hci_le_start_enc(hcon, key->ediv, key->rand, key->val); |
1119 | hcon->enc_key_size = key->enc_size; | |
988c5997 | 1120 | |
fe59a05f JH |
1121 | /* We never store STKs for master role, so clear this flag */ |
1122 | clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); | |
1123 | ||
f81cd823 | 1124 | return true; |
988c5997 | 1125 | } |
f1560463 | 1126 | |
35dc6f83 JH |
1127 | bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level, |
1128 | enum smp_key_pref key_pref) | |
854f4727 JH |
1129 | { |
1130 | if (sec_level == BT_SECURITY_LOW) | |
1131 | return true; | |
1132 | ||
35dc6f83 JH |
1133 | /* If we're encrypted with an STK but the caller prefers using |
1134 | * LTK claim insufficient security. This way we allow the | |
1135 | * connection to be re-encrypted with an LTK, even if the LTK | |
1136 | * provides the same level of security. Only exception is if we | |
1137 | * don't have an LTK (e.g. because of key distribution bits). | |
9ab65d60 | 1138 | */ |
35dc6f83 JH |
1139 | if (key_pref == SMP_USE_LTK && |
1140 | test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) && | |
b2d5e254 | 1141 | hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type, |
e804d25d | 1142 | hcon->role)) |
9ab65d60 JH |
1143 | return false; |
1144 | ||
854f4727 JH |
1145 | if (hcon->sec_level >= sec_level) |
1146 | return true; | |
1147 | ||
1148 | return false; | |
1149 | } | |
1150 | ||
da85e5e5 | 1151 | static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 AB |
1152 | { |
1153 | struct smp_cmd_security_req *rp = (void *) skb->data; | |
1154 | struct smp_cmd_pairing cp; | |
f1cb9af5 | 1155 | struct hci_conn *hcon = conn->hcon; |
0edb14de | 1156 | struct hci_dev *hdev = hcon->hdev; |
8aab4757 | 1157 | struct smp_chan *smp; |
c05b9339 | 1158 | u8 sec_level, auth; |
88ba43b6 AB |
1159 | |
1160 | BT_DBG("conn %p", conn); | |
1161 | ||
c46b98be | 1162 | if (skb->len < sizeof(*rp)) |
38e4a915 | 1163 | return SMP_INVALID_PARAMS; |
c46b98be | 1164 | |
40bef302 | 1165 | if (hcon->role != HCI_ROLE_MASTER) |
86ca9eac JH |
1166 | return SMP_CMD_NOTSUPP; |
1167 | ||
0edb14de | 1168 | auth = rp->auth_req & AUTH_REQ_MASK(hdev); |
c05b9339 | 1169 | |
5be5e275 | 1170 | if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
1afc2a1a JH |
1171 | sec_level = BT_SECURITY_MEDIUM; |
1172 | else | |
1173 | sec_level = authreq_to_seclevel(auth); | |
1174 | ||
35dc6f83 | 1175 | if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) |
854f4727 JH |
1176 | return 0; |
1177 | ||
c7262e71 JH |
1178 | if (sec_level > hcon->pending_sec_level) |
1179 | hcon->pending_sec_level = sec_level; | |
feb45eb5 | 1180 | |
4dab7864 | 1181 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
988c5997 VCG |
1182 | return 0; |
1183 | ||
8aab4757 | 1184 | smp = smp_chan_create(conn); |
c29d2444 JH |
1185 | if (!smp) |
1186 | return SMP_UNSPECIFIED; | |
d26a2345 | 1187 | |
b6ae8457 | 1188 | if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) && |
c05b9339 | 1189 | (auth & SMP_AUTH_BONDING)) |
616d55be JH |
1190 | return SMP_PAIRING_NOTSUPP; |
1191 | ||
88ba43b6 | 1192 | skb_pull(skb, sizeof(*rp)); |
88ba43b6 | 1193 | |
da85e5e5 | 1194 | memset(&cp, 0, sizeof(cp)); |
c05b9339 | 1195 | build_pairing_cmd(conn, &cp, NULL, auth); |
88ba43b6 | 1196 | |
1c1def09 VCG |
1197 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
1198 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 1199 | |
88ba43b6 | 1200 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
b28b4943 | 1201 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
f1cb9af5 | 1202 | |
da85e5e5 | 1203 | return 0; |
88ba43b6 AB |
1204 | } |
1205 | ||
cc110922 | 1206 | int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) |
eb492e01 | 1207 | { |
cc110922 | 1208 | struct l2cap_conn *conn = hcon->l2cap_data; |
c68b7f12 | 1209 | struct l2cap_chan *chan; |
0a66cf20 | 1210 | struct smp_chan *smp; |
2b64d153 | 1211 | __u8 authreq; |
fc75cc86 | 1212 | int ret; |
eb492e01 | 1213 | |
3a0259bb VCG |
1214 | BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); |
1215 | ||
0a66cf20 JH |
1216 | /* This may be NULL if there's an unexpected disconnection */ |
1217 | if (!conn) | |
1218 | return 1; | |
1219 | ||
c68b7f12 JH |
1220 | chan = conn->smp; |
1221 | ||
757aee0f | 1222 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) |
2e65c9d2 AG |
1223 | return 1; |
1224 | ||
35dc6f83 | 1225 | if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) |
eb492e01 | 1226 | return 1; |
f1cb9af5 | 1227 | |
c7262e71 JH |
1228 | if (sec_level > hcon->pending_sec_level) |
1229 | hcon->pending_sec_level = sec_level; | |
1230 | ||
40bef302 | 1231 | if (hcon->role == HCI_ROLE_MASTER) |
c7262e71 JH |
1232 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
1233 | return 0; | |
d26a2345 | 1234 | |
fc75cc86 JH |
1235 | l2cap_chan_lock(chan); |
1236 | ||
1237 | /* If SMP is already in progress ignore this request */ | |
1238 | if (chan->data) { | |
1239 | ret = 0; | |
1240 | goto unlock; | |
1241 | } | |
d26a2345 | 1242 | |
8aab4757 | 1243 | smp = smp_chan_create(conn); |
fc75cc86 JH |
1244 | if (!smp) { |
1245 | ret = 1; | |
1246 | goto unlock; | |
1247 | } | |
2b64d153 BG |
1248 | |
1249 | authreq = seclevel_to_authreq(sec_level); | |
d26a2345 | 1250 | |
79897d20 JH |
1251 | /* Require MITM if IO Capability allows or the security level |
1252 | * requires it. | |
2e233644 | 1253 | */ |
79897d20 | 1254 | if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT || |
c7262e71 | 1255 | hcon->pending_sec_level > BT_SECURITY_MEDIUM) |
2e233644 JH |
1256 | authreq |= SMP_AUTH_MITM; |
1257 | ||
40bef302 | 1258 | if (hcon->role == HCI_ROLE_MASTER) { |
d26a2345 | 1259 | struct smp_cmd_pairing cp; |
f01ead31 | 1260 | |
2b64d153 | 1261 | build_pairing_cmd(conn, &cp, NULL, authreq); |
1c1def09 VCG |
1262 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
1263 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 1264 | |
eb492e01 | 1265 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
b28b4943 | 1266 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
eb492e01 AB |
1267 | } else { |
1268 | struct smp_cmd_security_req cp; | |
2b64d153 | 1269 | cp.auth_req = authreq; |
eb492e01 | 1270 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
b28b4943 | 1271 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ); |
eb492e01 AB |
1272 | } |
1273 | ||
4a74d658 | 1274 | set_bit(SMP_FLAG_INITIATOR, &smp->flags); |
fc75cc86 | 1275 | ret = 0; |
edca792c | 1276 | |
fc75cc86 JH |
1277 | unlock: |
1278 | l2cap_chan_unlock(chan); | |
1279 | return ret; | |
eb492e01 AB |
1280 | } |
1281 | ||
7034b911 VCG |
1282 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1283 | { | |
16b90839 | 1284 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
5d88cc73 JH |
1285 | struct l2cap_chan *chan = conn->smp; |
1286 | struct smp_chan *smp = chan->data; | |
16b90839 | 1287 | |
c46b98be JH |
1288 | BT_DBG("conn %p", conn); |
1289 | ||
1290 | if (skb->len < sizeof(*rp)) | |
38e4a915 | 1291 | return SMP_INVALID_PARAMS; |
c46b98be | 1292 | |
b28b4943 | 1293 | SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT); |
6131ddc8 | 1294 | |
16b90839 VCG |
1295 | skb_pull(skb, sizeof(*rp)); |
1296 | ||
1c1def09 | 1297 | memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); |
16b90839 | 1298 | |
7034b911 VCG |
1299 | return 0; |
1300 | } | |
1301 | ||
1302 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) | |
1303 | { | |
16b90839 | 1304 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
5d88cc73 JH |
1305 | struct l2cap_chan *chan = conn->smp; |
1306 | struct smp_chan *smp = chan->data; | |
c9839a11 VCG |
1307 | struct hci_dev *hdev = conn->hcon->hdev; |
1308 | struct hci_conn *hcon = conn->hcon; | |
23d0e128 | 1309 | struct smp_ltk *ltk; |
c9839a11 | 1310 | u8 authenticated; |
16b90839 | 1311 | |
c46b98be JH |
1312 | BT_DBG("conn %p", conn); |
1313 | ||
1314 | if (skb->len < sizeof(*rp)) | |
38e4a915 | 1315 | return SMP_INVALID_PARAMS; |
c46b98be | 1316 | |
9747a9f3 JH |
1317 | /* Mark the information as received */ |
1318 | smp->remote_key_dist &= ~SMP_DIST_ENC_KEY; | |
1319 | ||
b28b4943 JH |
1320 | if (smp->remote_key_dist & SMP_DIST_ID_KEY) |
1321 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); | |
196332f5 JH |
1322 | else if (smp->remote_key_dist & SMP_DIST_SIGN) |
1323 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); | |
b28b4943 | 1324 | |
16b90839 | 1325 | skb_pull(skb, sizeof(*rp)); |
7034b911 | 1326 | |
ce39fb4e | 1327 | authenticated = (hcon->sec_level == BT_SECURITY_HIGH); |
2ceba539 | 1328 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK, |
23d0e128 JH |
1329 | authenticated, smp->tk, smp->enc_key_size, |
1330 | rp->ediv, rp->rand); | |
1331 | smp->ltk = ltk; | |
c6e81e9a | 1332 | if (!(smp->remote_key_dist & KEY_DIST_MASK)) |
d6268e86 | 1333 | smp_distribute_keys(smp); |
7034b911 VCG |
1334 | |
1335 | return 0; | |
1336 | } | |
1337 | ||
fd349c02 JH |
1338 | static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1339 | { | |
1340 | struct smp_cmd_ident_info *info = (void *) skb->data; | |
5d88cc73 JH |
1341 | struct l2cap_chan *chan = conn->smp; |
1342 | struct smp_chan *smp = chan->data; | |
fd349c02 JH |
1343 | |
1344 | BT_DBG(""); | |
1345 | ||
1346 | if (skb->len < sizeof(*info)) | |
38e4a915 | 1347 | return SMP_INVALID_PARAMS; |
fd349c02 | 1348 | |
b28b4943 | 1349 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO); |
6131ddc8 | 1350 | |
fd349c02 JH |
1351 | skb_pull(skb, sizeof(*info)); |
1352 | ||
1353 | memcpy(smp->irk, info->irk, 16); | |
1354 | ||
1355 | return 0; | |
1356 | } | |
1357 | ||
1358 | static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, | |
1359 | struct sk_buff *skb) | |
1360 | { | |
1361 | struct smp_cmd_ident_addr_info *info = (void *) skb->data; | |
5d88cc73 JH |
1362 | struct l2cap_chan *chan = conn->smp; |
1363 | struct smp_chan *smp = chan->data; | |
fd349c02 JH |
1364 | struct hci_conn *hcon = conn->hcon; |
1365 | bdaddr_t rpa; | |
1366 | ||
1367 | BT_DBG(""); | |
1368 | ||
1369 | if (skb->len < sizeof(*info)) | |
38e4a915 | 1370 | return SMP_INVALID_PARAMS; |
fd349c02 | 1371 | |
9747a9f3 JH |
1372 | /* Mark the information as received */ |
1373 | smp->remote_key_dist &= ~SMP_DIST_ID_KEY; | |
1374 | ||
b28b4943 JH |
1375 | if (smp->remote_key_dist & SMP_DIST_SIGN) |
1376 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); | |
1377 | ||
fd349c02 JH |
1378 | skb_pull(skb, sizeof(*info)); |
1379 | ||
a9a58f86 JH |
1380 | /* Strictly speaking the Core Specification (4.1) allows sending |
1381 | * an empty address which would force us to rely on just the IRK | |
1382 | * as "identity information". However, since such | |
1383 | * implementations are not known of and in order to not over | |
1384 | * complicate our implementation, simply pretend that we never | |
1385 | * received an IRK for such a device. | |
1386 | */ | |
1387 | if (!bacmp(&info->bdaddr, BDADDR_ANY)) { | |
1388 | BT_ERR("Ignoring IRK with no identity address"); | |
31dd624e | 1389 | goto distribute; |
a9a58f86 JH |
1390 | } |
1391 | ||
fd349c02 JH |
1392 | bacpy(&smp->id_addr, &info->bdaddr); |
1393 | smp->id_addr_type = info->addr_type; | |
1394 | ||
1395 | if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type)) | |
1396 | bacpy(&rpa, &hcon->dst); | |
1397 | else | |
1398 | bacpy(&rpa, BDADDR_ANY); | |
1399 | ||
23d0e128 JH |
1400 | smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr, |
1401 | smp->id_addr_type, smp->irk, &rpa); | |
fd349c02 | 1402 | |
31dd624e | 1403 | distribute: |
c6e81e9a JH |
1404 | if (!(smp->remote_key_dist & KEY_DIST_MASK)) |
1405 | smp_distribute_keys(smp); | |
fd349c02 JH |
1406 | |
1407 | return 0; | |
1408 | } | |
1409 | ||
7ee4ea36 MH |
1410 | static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) |
1411 | { | |
1412 | struct smp_cmd_sign_info *rp = (void *) skb->data; | |
5d88cc73 JH |
1413 | struct l2cap_chan *chan = conn->smp; |
1414 | struct smp_chan *smp = chan->data; | |
7ee4ea36 MH |
1415 | struct smp_csrk *csrk; |
1416 | ||
1417 | BT_DBG("conn %p", conn); | |
1418 | ||
1419 | if (skb->len < sizeof(*rp)) | |
38e4a915 | 1420 | return SMP_INVALID_PARAMS; |
7ee4ea36 | 1421 | |
7ee4ea36 MH |
1422 | /* Mark the information as received */ |
1423 | smp->remote_key_dist &= ~SMP_DIST_SIGN; | |
1424 | ||
1425 | skb_pull(skb, sizeof(*rp)); | |
1426 | ||
7ee4ea36 MH |
1427 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); |
1428 | if (csrk) { | |
1429 | csrk->master = 0x01; | |
1430 | memcpy(csrk->val, rp->csrk, sizeof(csrk->val)); | |
1431 | } | |
1432 | smp->csrk = csrk; | |
d6268e86 | 1433 | smp_distribute_keys(smp); |
7ee4ea36 MH |
1434 | |
1435 | return 0; | |
1436 | } | |
1437 | ||
4befb867 | 1438 | static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb) |
eb492e01 | 1439 | { |
5d88cc73 | 1440 | struct l2cap_conn *conn = chan->conn; |
7b9899db | 1441 | struct hci_conn *hcon = conn->hcon; |
b28b4943 | 1442 | struct smp_chan *smp; |
92381f5c | 1443 | __u8 code, reason; |
eb492e01 AB |
1444 | int err = 0; |
1445 | ||
7b9899db MH |
1446 | if (hcon->type != LE_LINK) { |
1447 | kfree_skb(skb); | |
3432711f | 1448 | return 0; |
7b9899db MH |
1449 | } |
1450 | ||
8ae9b984 | 1451 | if (skb->len < 1) |
92381f5c | 1452 | return -EILSEQ; |
92381f5c | 1453 | |
06ae3314 | 1454 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { |
2e65c9d2 AG |
1455 | reason = SMP_PAIRING_NOTSUPP; |
1456 | goto done; | |
1457 | } | |
1458 | ||
92381f5c | 1459 | code = skb->data[0]; |
eb492e01 AB |
1460 | skb_pull(skb, sizeof(code)); |
1461 | ||
b28b4943 JH |
1462 | smp = chan->data; |
1463 | ||
1464 | if (code > SMP_CMD_MAX) | |
1465 | goto drop; | |
1466 | ||
24bd0bd9 | 1467 | if (smp && !test_and_clear_bit(code, &smp->allow_cmd)) |
b28b4943 JH |
1468 | goto drop; |
1469 | ||
1470 | /* If we don't have a context the only allowed commands are | |
1471 | * pairing request and security request. | |
8cf9fa12 | 1472 | */ |
b28b4943 JH |
1473 | if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ) |
1474 | goto drop; | |
8cf9fa12 | 1475 | |
eb492e01 AB |
1476 | switch (code) { |
1477 | case SMP_CMD_PAIRING_REQ: | |
da85e5e5 | 1478 | reason = smp_cmd_pairing_req(conn, skb); |
eb492e01 AB |
1479 | break; |
1480 | ||
1481 | case SMP_CMD_PAIRING_FAIL: | |
84794e11 | 1482 | smp_failure(conn, 0); |
da85e5e5 | 1483 | err = -EPERM; |
eb492e01 AB |
1484 | break; |
1485 | ||
1486 | case SMP_CMD_PAIRING_RSP: | |
da85e5e5 | 1487 | reason = smp_cmd_pairing_rsp(conn, skb); |
88ba43b6 AB |
1488 | break; |
1489 | ||
1490 | case SMP_CMD_SECURITY_REQ: | |
da85e5e5 | 1491 | reason = smp_cmd_security_req(conn, skb); |
88ba43b6 AB |
1492 | break; |
1493 | ||
eb492e01 | 1494 | case SMP_CMD_PAIRING_CONFIRM: |
da85e5e5 | 1495 | reason = smp_cmd_pairing_confirm(conn, skb); |
88ba43b6 AB |
1496 | break; |
1497 | ||
eb492e01 | 1498 | case SMP_CMD_PAIRING_RANDOM: |
da85e5e5 | 1499 | reason = smp_cmd_pairing_random(conn, skb); |
88ba43b6 AB |
1500 | break; |
1501 | ||
eb492e01 | 1502 | case SMP_CMD_ENCRYPT_INFO: |
7034b911 VCG |
1503 | reason = smp_cmd_encrypt_info(conn, skb); |
1504 | break; | |
1505 | ||
eb492e01 | 1506 | case SMP_CMD_MASTER_IDENT: |
7034b911 VCG |
1507 | reason = smp_cmd_master_ident(conn, skb); |
1508 | break; | |
1509 | ||
eb492e01 | 1510 | case SMP_CMD_IDENT_INFO: |
fd349c02 JH |
1511 | reason = smp_cmd_ident_info(conn, skb); |
1512 | break; | |
1513 | ||
eb492e01 | 1514 | case SMP_CMD_IDENT_ADDR_INFO: |
fd349c02 JH |
1515 | reason = smp_cmd_ident_addr_info(conn, skb); |
1516 | break; | |
1517 | ||
eb492e01 | 1518 | case SMP_CMD_SIGN_INFO: |
7ee4ea36 | 1519 | reason = smp_cmd_sign_info(conn, skb); |
7034b911 VCG |
1520 | break; |
1521 | ||
eb492e01 AB |
1522 | default: |
1523 | BT_DBG("Unknown command code 0x%2.2x", code); | |
eb492e01 | 1524 | reason = SMP_CMD_NOTSUPP; |
3a0259bb | 1525 | goto done; |
eb492e01 AB |
1526 | } |
1527 | ||
3a0259bb | 1528 | done: |
9b7b18ef JH |
1529 | if (!err) { |
1530 | if (reason) | |
1531 | smp_failure(conn, reason); | |
8ae9b984 | 1532 | kfree_skb(skb); |
9b7b18ef JH |
1533 | } |
1534 | ||
eb492e01 | 1535 | return err; |
b28b4943 JH |
1536 | |
1537 | drop: | |
1538 | BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name, | |
1539 | code, &hcon->dst); | |
1540 | kfree_skb(skb); | |
1541 | return 0; | |
eb492e01 | 1542 | } |
7034b911 | 1543 | |
70db83c4 JH |
1544 | static void smp_teardown_cb(struct l2cap_chan *chan, int err) |
1545 | { | |
1546 | struct l2cap_conn *conn = chan->conn; | |
1547 | ||
1548 | BT_DBG("chan %p", chan); | |
1549 | ||
fc75cc86 | 1550 | if (chan->data) |
5d88cc73 | 1551 | smp_chan_destroy(conn); |
5d88cc73 | 1552 | |
70db83c4 JH |
1553 | conn->smp = NULL; |
1554 | l2cap_chan_put(chan); | |
1555 | } | |
1556 | ||
44f1a7ab JH |
1557 | static void smp_resume_cb(struct l2cap_chan *chan) |
1558 | { | |
b68fda68 | 1559 | struct smp_chan *smp = chan->data; |
44f1a7ab JH |
1560 | struct l2cap_conn *conn = chan->conn; |
1561 | struct hci_conn *hcon = conn->hcon; | |
1562 | ||
1563 | BT_DBG("chan %p", chan); | |
1564 | ||
86d1407c JH |
1565 | if (!smp) |
1566 | return; | |
b68fda68 | 1567 | |
84bc0db5 JH |
1568 | if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags)) |
1569 | return; | |
1570 | ||
86d1407c JH |
1571 | cancel_delayed_work(&smp->security_timer); |
1572 | ||
d6268e86 | 1573 | smp_distribute_keys(smp); |
44f1a7ab JH |
1574 | } |
1575 | ||
70db83c4 JH |
1576 | static void smp_ready_cb(struct l2cap_chan *chan) |
1577 | { | |
1578 | struct l2cap_conn *conn = chan->conn; | |
1579 | ||
1580 | BT_DBG("chan %p", chan); | |
1581 | ||
1582 | conn->smp = chan; | |
1583 | l2cap_chan_hold(chan); | |
1584 | } | |
1585 | ||
4befb867 JH |
1586 | static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
1587 | { | |
1588 | int err; | |
1589 | ||
1590 | BT_DBG("chan %p", chan); | |
1591 | ||
1592 | err = smp_sig_channel(chan, skb); | |
1593 | if (err) { | |
b68fda68 | 1594 | struct smp_chan *smp = chan->data; |
4befb867 | 1595 | |
b68fda68 JH |
1596 | if (smp) |
1597 | cancel_delayed_work_sync(&smp->security_timer); | |
4befb867 | 1598 | |
1e91c29e | 1599 | hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE); |
4befb867 JH |
1600 | } |
1601 | ||
1602 | return err; | |
1603 | } | |
1604 | ||
70db83c4 JH |
1605 | static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan, |
1606 | unsigned long hdr_len, | |
1607 | unsigned long len, int nb) | |
1608 | { | |
1609 | struct sk_buff *skb; | |
1610 | ||
1611 | skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL); | |
1612 | if (!skb) | |
1613 | return ERR_PTR(-ENOMEM); | |
1614 | ||
1615 | skb->priority = HCI_PRIO_MAX; | |
1616 | bt_cb(skb)->chan = chan; | |
1617 | ||
1618 | return skb; | |
1619 | } | |
1620 | ||
1621 | static const struct l2cap_ops smp_chan_ops = { | |
1622 | .name = "Security Manager", | |
1623 | .ready = smp_ready_cb, | |
5d88cc73 | 1624 | .recv = smp_recv_cb, |
70db83c4 JH |
1625 | .alloc_skb = smp_alloc_skb_cb, |
1626 | .teardown = smp_teardown_cb, | |
44f1a7ab | 1627 | .resume = smp_resume_cb, |
70db83c4 JH |
1628 | |
1629 | .new_connection = l2cap_chan_no_new_connection, | |
70db83c4 JH |
1630 | .state_change = l2cap_chan_no_state_change, |
1631 | .close = l2cap_chan_no_close, | |
1632 | .defer = l2cap_chan_no_defer, | |
1633 | .suspend = l2cap_chan_no_suspend, | |
70db83c4 JH |
1634 | .set_shutdown = l2cap_chan_no_set_shutdown, |
1635 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, | |
1636 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, | |
1637 | }; | |
1638 | ||
1639 | static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan) | |
1640 | { | |
1641 | struct l2cap_chan *chan; | |
1642 | ||
1643 | BT_DBG("pchan %p", pchan); | |
1644 | ||
1645 | chan = l2cap_chan_create(); | |
1646 | if (!chan) | |
1647 | return NULL; | |
1648 | ||
1649 | chan->chan_type = pchan->chan_type; | |
1650 | chan->ops = &smp_chan_ops; | |
1651 | chan->scid = pchan->scid; | |
1652 | chan->dcid = chan->scid; | |
1653 | chan->imtu = pchan->imtu; | |
1654 | chan->omtu = pchan->omtu; | |
1655 | chan->mode = pchan->mode; | |
1656 | ||
abe84903 JH |
1657 | /* Other L2CAP channels may request SMP routines in order to |
1658 | * change the security level. This means that the SMP channel | |
1659 | * lock must be considered in its own category to avoid lockdep | |
1660 | * warnings. | |
1661 | */ | |
1662 | atomic_set(&chan->nesting, L2CAP_NESTING_SMP); | |
1663 | ||
70db83c4 JH |
1664 | BT_DBG("created chan %p", chan); |
1665 | ||
1666 | return chan; | |
1667 | } | |
1668 | ||
1669 | static const struct l2cap_ops smp_root_chan_ops = { | |
1670 | .name = "Security Manager Root", | |
1671 | .new_connection = smp_new_conn_cb, | |
1672 | ||
1673 | /* None of these are implemented for the root channel */ | |
1674 | .close = l2cap_chan_no_close, | |
1675 | .alloc_skb = l2cap_chan_no_alloc_skb, | |
1676 | .recv = l2cap_chan_no_recv, | |
1677 | .state_change = l2cap_chan_no_state_change, | |
1678 | .teardown = l2cap_chan_no_teardown, | |
1679 | .ready = l2cap_chan_no_ready, | |
1680 | .defer = l2cap_chan_no_defer, | |
1681 | .suspend = l2cap_chan_no_suspend, | |
1682 | .resume = l2cap_chan_no_resume, | |
1683 | .set_shutdown = l2cap_chan_no_set_shutdown, | |
1684 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, | |
1685 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, | |
1686 | }; | |
1687 | ||
711eafe3 JH |
1688 | int smp_register(struct hci_dev *hdev) |
1689 | { | |
70db83c4 | 1690 | struct l2cap_chan *chan; |
defce9e8 | 1691 | struct crypto_blkcipher *tfm_aes; |
70db83c4 | 1692 | |
711eafe3 JH |
1693 | BT_DBG("%s", hdev->name); |
1694 | ||
adae20cb | 1695 | tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0); |
defce9e8 JH |
1696 | if (IS_ERR(tfm_aes)) { |
1697 | int err = PTR_ERR(tfm_aes); | |
711eafe3 | 1698 | BT_ERR("Unable to create crypto context"); |
711eafe3 JH |
1699 | return err; |
1700 | } | |
1701 | ||
70db83c4 JH |
1702 | chan = l2cap_chan_create(); |
1703 | if (!chan) { | |
defce9e8 | 1704 | crypto_free_blkcipher(tfm_aes); |
70db83c4 JH |
1705 | return -ENOMEM; |
1706 | } | |
1707 | ||
defce9e8 JH |
1708 | chan->data = tfm_aes; |
1709 | ||
5d88cc73 | 1710 | l2cap_add_scid(chan, L2CAP_CID_SMP); |
70db83c4 JH |
1711 | |
1712 | l2cap_chan_set_defaults(chan); | |
1713 | ||
1714 | bacpy(&chan->src, &hdev->bdaddr); | |
1715 | chan->src_type = BDADDR_LE_PUBLIC; | |
1716 | chan->state = BT_LISTEN; | |
1717 | chan->mode = L2CAP_MODE_BASIC; | |
1718 | chan->imtu = L2CAP_DEFAULT_MTU; | |
1719 | chan->ops = &smp_root_chan_ops; | |
1720 | ||
abe84903 JH |
1721 | /* Set correct nesting level for a parent/listening channel */ |
1722 | atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); | |
1723 | ||
70db83c4 JH |
1724 | hdev->smp_data = chan; |
1725 | ||
711eafe3 JH |
1726 | return 0; |
1727 | } | |
1728 | ||
1729 | void smp_unregister(struct hci_dev *hdev) | |
1730 | { | |
70db83c4 | 1731 | struct l2cap_chan *chan = hdev->smp_data; |
defce9e8 | 1732 | struct crypto_blkcipher *tfm_aes; |
70db83c4 JH |
1733 | |
1734 | if (!chan) | |
1735 | return; | |
1736 | ||
1737 | BT_DBG("%s chan %p", hdev->name, chan); | |
711eafe3 | 1738 | |
defce9e8 JH |
1739 | tfm_aes = chan->data; |
1740 | if (tfm_aes) { | |
1741 | chan->data = NULL; | |
1742 | crypto_free_blkcipher(tfm_aes); | |
711eafe3 | 1743 | } |
70db83c4 JH |
1744 | |
1745 | hdev->smp_data = NULL; | |
1746 | l2cap_chan_put(chan); | |
711eafe3 | 1747 | } |