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