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 | |
17b02e62 | 34 | #define SMP_TIMEOUT msecs_to_jiffies(30000) |
5d3de7df | 35 | |
065a13e2 JH |
36 | #define AUTH_REQ_MASK 0x07 |
37 | ||
d22ef0bc AB |
38 | static inline void swap128(u8 src[16], u8 dst[16]) |
39 | { | |
40 | int i; | |
41 | for (i = 0; i < 16; i++) | |
42 | dst[15 - i] = src[i]; | |
43 | } | |
44 | ||
45 | static inline void swap56(u8 src[7], u8 dst[7]) | |
46 | { | |
47 | int i; | |
48 | for (i = 0; i < 7; i++) | |
49 | dst[6 - i] = src[i]; | |
50 | } | |
51 | ||
52 | static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) | |
53 | { | |
54 | struct blkcipher_desc desc; | |
55 | struct scatterlist sg; | |
201a5929 | 56 | int err; |
d22ef0bc AB |
57 | |
58 | if (tfm == NULL) { | |
59 | BT_ERR("tfm %p", tfm); | |
60 | return -EINVAL; | |
61 | } | |
62 | ||
63 | desc.tfm = tfm; | |
64 | desc.flags = 0; | |
65 | ||
66 | err = crypto_blkcipher_setkey(tfm, k, 16); | |
67 | if (err) { | |
68 | BT_ERR("cipher setkey failed: %d", err); | |
69 | return err; | |
70 | } | |
71 | ||
72 | sg_init_one(&sg, r, 16); | |
73 | ||
d22ef0bc AB |
74 | err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); |
75 | if (err) | |
76 | BT_ERR("Encrypt data error %d", err); | |
77 | ||
78 | return err; | |
79 | } | |
80 | ||
60478054 JH |
81 | static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3]) |
82 | { | |
83 | u8 _res[16], k[16]; | |
84 | int err; | |
85 | ||
86 | /* r' = padding || r */ | |
87 | memset(_res, 0, 13); | |
88 | _res[13] = r[2]; | |
89 | _res[14] = r[1]; | |
90 | _res[15] = r[0]; | |
91 | ||
92 | swap128(irk, k); | |
93 | err = smp_e(tfm, k, _res); | |
94 | if (err) { | |
95 | BT_ERR("Encrypt error"); | |
96 | return err; | |
97 | } | |
98 | ||
99 | /* The output of the random address function ah is: | |
100 | * ah(h, r) = e(k, r') mod 2^24 | |
101 | * The output of the security function e is then truncated to 24 bits | |
102 | * by taking the least significant 24 bits of the output of e as the | |
103 | * result of ah. | |
104 | */ | |
105 | res[0] = _res[15]; | |
106 | res[1] = _res[14]; | |
107 | res[2] = _res[13]; | |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
112 | bool smp_irk_matches(struct crypto_blkcipher *tfm, u8 irk[16], | |
113 | bdaddr_t *bdaddr) | |
114 | { | |
115 | u8 hash[3]; | |
116 | int err; | |
117 | ||
118 | BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); | |
119 | ||
120 | err = smp_ah(tfm, irk, &bdaddr->b[3], hash); | |
121 | if (err) | |
122 | return false; | |
123 | ||
124 | return !memcmp(bdaddr->b, hash, 3); | |
125 | } | |
126 | ||
d22ef0bc | 127 | static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16], |
f1560463 MH |
128 | u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, |
129 | u8 _rat, bdaddr_t *ra, u8 res[16]) | |
d22ef0bc AB |
130 | { |
131 | u8 p1[16], p2[16]; | |
132 | int err; | |
133 | ||
134 | memset(p1, 0, 16); | |
135 | ||
136 | /* p1 = pres || preq || _rat || _iat */ | |
137 | swap56(pres, p1); | |
138 | swap56(preq, p1 + 7); | |
139 | p1[14] = _rat; | |
140 | p1[15] = _iat; | |
141 | ||
142 | memset(p2, 0, 16); | |
143 | ||
144 | /* p2 = padding || ia || ra */ | |
145 | baswap((bdaddr_t *) (p2 + 4), ia); | |
146 | baswap((bdaddr_t *) (p2 + 10), ra); | |
147 | ||
148 | /* res = r XOR p1 */ | |
149 | u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); | |
150 | ||
151 | /* res = e(k, res) */ | |
152 | err = smp_e(tfm, k, res); | |
153 | if (err) { | |
154 | BT_ERR("Encrypt data error"); | |
155 | return err; | |
156 | } | |
157 | ||
158 | /* res = res XOR p2 */ | |
159 | u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); | |
160 | ||
161 | /* res = e(k, res) */ | |
162 | err = smp_e(tfm, k, res); | |
163 | if (err) | |
164 | BT_ERR("Encrypt data error"); | |
165 | ||
166 | return err; | |
167 | } | |
168 | ||
f1560463 MH |
169 | static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], u8 r1[16], |
170 | u8 r2[16], u8 _r[16]) | |
d22ef0bc AB |
171 | { |
172 | int err; | |
173 | ||
174 | /* Just least significant octets from r1 and r2 are considered */ | |
175 | memcpy(_r, r1 + 8, 8); | |
176 | memcpy(_r + 8, r2 + 8, 8); | |
177 | ||
178 | err = smp_e(tfm, k, _r); | |
179 | if (err) | |
180 | BT_ERR("Encrypt data error"); | |
181 | ||
182 | return err; | |
183 | } | |
184 | ||
eb492e01 | 185 | static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code, |
f1560463 | 186 | u16 dlen, void *data) |
eb492e01 AB |
187 | { |
188 | struct sk_buff *skb; | |
189 | struct l2cap_hdr *lh; | |
190 | int len; | |
191 | ||
192 | len = L2CAP_HDR_SIZE + sizeof(code) + dlen; | |
193 | ||
194 | if (len > conn->mtu) | |
195 | return NULL; | |
196 | ||
197 | skb = bt_skb_alloc(len, GFP_ATOMIC); | |
198 | if (!skb) | |
199 | return NULL; | |
200 | ||
201 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); | |
202 | lh->len = cpu_to_le16(sizeof(code) + dlen); | |
d8aece2a | 203 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_SMP); |
eb492e01 AB |
204 | |
205 | memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code)); | |
206 | ||
207 | memcpy(skb_put(skb, dlen), data, dlen); | |
208 | ||
209 | return skb; | |
210 | } | |
211 | ||
212 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) | |
213 | { | |
214 | struct sk_buff *skb = smp_build_cmd(conn, code, len, data); | |
215 | ||
216 | BT_DBG("code 0x%2.2x", code); | |
217 | ||
218 | if (!skb) | |
219 | return; | |
220 | ||
73d80deb LAD |
221 | skb->priority = HCI_PRIO_MAX; |
222 | hci_send_acl(conn->hchan, skb, 0); | |
e2dcd113 | 223 | |
6c9d42a1 | 224 | cancel_delayed_work_sync(&conn->security_timer); |
17b02e62 | 225 | schedule_delayed_work(&conn->security_timer, SMP_TIMEOUT); |
eb492e01 AB |
226 | } |
227 | ||
2b64d153 BG |
228 | static __u8 authreq_to_seclevel(__u8 authreq) |
229 | { | |
230 | if (authreq & SMP_AUTH_MITM) | |
231 | return BT_SECURITY_HIGH; | |
232 | else | |
233 | return BT_SECURITY_MEDIUM; | |
234 | } | |
235 | ||
236 | static __u8 seclevel_to_authreq(__u8 sec_level) | |
237 | { | |
238 | switch (sec_level) { | |
239 | case BT_SECURITY_HIGH: | |
240 | return SMP_AUTH_MITM | SMP_AUTH_BONDING; | |
241 | case BT_SECURITY_MEDIUM: | |
242 | return SMP_AUTH_BONDING; | |
243 | default: | |
244 | return SMP_AUTH_NONE; | |
245 | } | |
246 | } | |
247 | ||
b8e66eac | 248 | static void build_pairing_cmd(struct l2cap_conn *conn, |
f1560463 MH |
249 | struct smp_cmd_pairing *req, |
250 | struct smp_cmd_pairing *rsp, __u8 authreq) | |
b8e66eac | 251 | { |
fd349c02 JH |
252 | struct smp_chan *smp = conn->smp_chan; |
253 | struct hci_conn *hcon = conn->hcon; | |
254 | struct hci_dev *hdev = hcon->hdev; | |
255 | u8 local_dist = 0, remote_dist = 0; | |
54790f73 | 256 | |
a8b2d5c2 | 257 | if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->dev_flags)) { |
fd349c02 JH |
258 | local_dist = SMP_DIST_ENC_KEY; |
259 | remote_dist = SMP_DIST_ENC_KEY; | |
54790f73 | 260 | authreq |= SMP_AUTH_BONDING; |
2b64d153 BG |
261 | } else { |
262 | authreq &= ~SMP_AUTH_BONDING; | |
54790f73 VCG |
263 | } |
264 | ||
fd349c02 JH |
265 | if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) |
266 | remote_dist |= SMP_DIST_ID_KEY; | |
267 | ||
54790f73 VCG |
268 | if (rsp == NULL) { |
269 | req->io_capability = conn->hcon->io_capability; | |
270 | req->oob_flag = SMP_OOB_NOT_PRESENT; | |
271 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
fd349c02 JH |
272 | req->init_key_dist = local_dist; |
273 | req->resp_key_dist = remote_dist; | |
065a13e2 | 274 | req->auth_req = (authreq & AUTH_REQ_MASK); |
fd349c02 JH |
275 | |
276 | smp->remote_key_dist = remote_dist; | |
54790f73 VCG |
277 | return; |
278 | } | |
279 | ||
280 | rsp->io_capability = conn->hcon->io_capability; | |
281 | rsp->oob_flag = SMP_OOB_NOT_PRESENT; | |
282 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
fd349c02 JH |
283 | rsp->init_key_dist = req->init_key_dist & remote_dist; |
284 | rsp->resp_key_dist = req->resp_key_dist & local_dist; | |
065a13e2 | 285 | rsp->auth_req = (authreq & AUTH_REQ_MASK); |
fd349c02 JH |
286 | |
287 | smp->remote_key_dist = rsp->init_key_dist; | |
b8e66eac VCG |
288 | } |
289 | ||
3158c50c VCG |
290 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
291 | { | |
1c1def09 VCG |
292 | struct smp_chan *smp = conn->smp_chan; |
293 | ||
3158c50c | 294 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
f1560463 | 295 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
3158c50c VCG |
296 | return SMP_ENC_KEY_SIZE; |
297 | ||
f7aa611a | 298 | smp->enc_key_size = max_key_size; |
3158c50c VCG |
299 | |
300 | return 0; | |
301 | } | |
302 | ||
84794e11 | 303 | static void smp_failure(struct l2cap_conn *conn, u8 reason) |
4f957a76 | 304 | { |
bab73cb6 JH |
305 | struct hci_conn *hcon = conn->hcon; |
306 | ||
84794e11 | 307 | if (reason) |
4f957a76 | 308 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
f1560463 | 309 | &reason); |
4f957a76 | 310 | |
ce39fb4e MH |
311 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags); |
312 | mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type, | |
313 | HCI_ERROR_AUTH_FAILURE); | |
f1c09c07 | 314 | |
61a0cfb0 AG |
315 | cancel_delayed_work_sync(&conn->security_timer); |
316 | ||
ce39fb4e | 317 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
f1c09c07 | 318 | smp_chan_destroy(conn); |
4f957a76 BG |
319 | } |
320 | ||
2b64d153 BG |
321 | #define JUST_WORKS 0x00 |
322 | #define JUST_CFM 0x01 | |
323 | #define REQ_PASSKEY 0x02 | |
324 | #define CFM_PASSKEY 0x03 | |
325 | #define REQ_OOB 0x04 | |
326 | #define OVERLAP 0xFF | |
327 | ||
328 | static const u8 gen_method[5][5] = { | |
329 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
330 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
331 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, | |
332 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, | |
333 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, | |
334 | }; | |
335 | ||
336 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, | |
337 | u8 local_io, u8 remote_io) | |
338 | { | |
339 | struct hci_conn *hcon = conn->hcon; | |
340 | struct smp_chan *smp = conn->smp_chan; | |
341 | u8 method; | |
342 | u32 passkey = 0; | |
343 | int ret = 0; | |
344 | ||
345 | /* Initialize key for JUST WORKS */ | |
346 | memset(smp->tk, 0, sizeof(smp->tk)); | |
347 | clear_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); | |
348 | ||
349 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); | |
350 | ||
351 | /* If neither side wants MITM, use JUST WORKS */ | |
352 | /* If either side has unknown io_caps, use JUST WORKS */ | |
353 | /* Otherwise, look up method from the table */ | |
354 | if (!(auth & SMP_AUTH_MITM) || | |
f1560463 MH |
355 | local_io > SMP_IO_KEYBOARD_DISPLAY || |
356 | remote_io > SMP_IO_KEYBOARD_DISPLAY) | |
2b64d153 BG |
357 | method = JUST_WORKS; |
358 | else | |
b3ff53ff | 359 | method = gen_method[remote_io][local_io]; |
2b64d153 BG |
360 | |
361 | /* If not bonding, don't ask user to confirm a Zero TK */ | |
362 | if (!(auth & SMP_AUTH_BONDING) && method == JUST_CFM) | |
363 | method = JUST_WORKS; | |
364 | ||
365 | /* If Just Works, Continue with Zero TK */ | |
366 | if (method == JUST_WORKS) { | |
367 | set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); | |
368 | return 0; | |
369 | } | |
370 | ||
371 | /* Not Just Works/Confirm results in MITM Authentication */ | |
372 | if (method != JUST_CFM) | |
373 | set_bit(SMP_FLAG_MITM_AUTH, &smp->smp_flags); | |
374 | ||
375 | /* If both devices have Keyoard-Display I/O, the master | |
376 | * Confirms and the slave Enters the passkey. | |
377 | */ | |
378 | if (method == OVERLAP) { | |
379 | if (hcon->link_mode & HCI_LM_MASTER) | |
380 | method = CFM_PASSKEY; | |
381 | else | |
382 | method = REQ_PASSKEY; | |
383 | } | |
384 | ||
385 | /* Generate random passkey. Not valid until confirmed. */ | |
386 | if (method == CFM_PASSKEY) { | |
387 | u8 key[16]; | |
388 | ||
389 | memset(key, 0, sizeof(key)); | |
390 | get_random_bytes(&passkey, sizeof(passkey)); | |
391 | passkey %= 1000000; | |
392 | put_unaligned_le32(passkey, key); | |
393 | swap128(key, smp->tk); | |
394 | BT_DBG("PassKey: %d", passkey); | |
395 | } | |
396 | ||
397 | hci_dev_lock(hcon->hdev); | |
398 | ||
399 | if (method == REQ_PASSKEY) | |
ce39fb4e | 400 | ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, |
272d90df | 401 | hcon->type, hcon->dst_type); |
2b64d153 | 402 | else |
ce39fb4e | 403 | ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, |
272d90df | 404 | hcon->type, hcon->dst_type, |
2b64d153 BG |
405 | cpu_to_le32(passkey), 0); |
406 | ||
407 | hci_dev_unlock(hcon->hdev); | |
408 | ||
409 | return ret; | |
410 | } | |
411 | ||
8aab4757 VCG |
412 | static void confirm_work(struct work_struct *work) |
413 | { | |
414 | struct smp_chan *smp = container_of(work, struct smp_chan, confirm); | |
415 | struct l2cap_conn *conn = smp->conn; | |
893ce8b1 JH |
416 | struct hci_dev *hdev = conn->hcon->hdev; |
417 | struct crypto_blkcipher *tfm = hdev->tfm_aes; | |
8aab4757 VCG |
418 | struct smp_cmd_pairing_confirm cp; |
419 | int ret; | |
420 | u8 res[16], reason; | |
421 | ||
422 | BT_DBG("conn %p", conn); | |
423 | ||
893ce8b1 JH |
424 | /* Prevent mutual access to hdev->tfm_aes */ |
425 | hci_dev_lock(hdev); | |
8aab4757 VCG |
426 | |
427 | if (conn->hcon->out) | |
c8462ca6 MH |
428 | ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, |
429 | conn->hcon->src_type, &conn->hcon->src, | |
430 | conn->hcon->dst_type, &conn->hcon->dst, res); | |
8aab4757 VCG |
431 | else |
432 | ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, | |
c8462ca6 MH |
433 | conn->hcon->dst_type, &conn->hcon->dst, |
434 | conn->hcon->src_type, &conn->hcon->src, res); | |
893ce8b1 JH |
435 | |
436 | hci_dev_unlock(hdev); | |
437 | ||
8aab4757 VCG |
438 | if (ret) { |
439 | reason = SMP_UNSPECIFIED; | |
440 | goto error; | |
441 | } | |
442 | ||
2b64d153 BG |
443 | clear_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); |
444 | ||
8aab4757 VCG |
445 | swap128(res, cp.confirm_val); |
446 | smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); | |
447 | ||
448 | return; | |
449 | ||
450 | error: | |
84794e11 | 451 | smp_failure(conn, reason); |
8aab4757 VCG |
452 | } |
453 | ||
454 | static void random_work(struct work_struct *work) | |
455 | { | |
456 | struct smp_chan *smp = container_of(work, struct smp_chan, random); | |
457 | struct l2cap_conn *conn = smp->conn; | |
458 | struct hci_conn *hcon = conn->hcon; | |
893ce8b1 JH |
459 | struct hci_dev *hdev = hcon->hdev; |
460 | struct crypto_blkcipher *tfm = hdev->tfm_aes; | |
8aab4757 VCG |
461 | u8 reason, confirm[16], res[16], key[16]; |
462 | int ret; | |
463 | ||
464 | if (IS_ERR_OR_NULL(tfm)) { | |
465 | reason = SMP_UNSPECIFIED; | |
466 | goto error; | |
467 | } | |
468 | ||
469 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); | |
470 | ||
893ce8b1 JH |
471 | /* Prevent mutual access to hdev->tfm_aes */ |
472 | hci_dev_lock(hdev); | |
473 | ||
8aab4757 | 474 | if (hcon->out) |
c8462ca6 MH |
475 | ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
476 | hcon->src_type, &hcon->src, | |
477 | hcon->dst_type, &hcon->dst, res); | |
8aab4757 VCG |
478 | else |
479 | ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, | |
c8462ca6 MH |
480 | hcon->dst_type, &hcon->dst, |
481 | hcon->src_type, &hcon->src, res); | |
893ce8b1 JH |
482 | |
483 | hci_dev_unlock(hdev); | |
484 | ||
8aab4757 VCG |
485 | if (ret) { |
486 | reason = SMP_UNSPECIFIED; | |
487 | goto error; | |
488 | } | |
489 | ||
490 | swap128(res, confirm); | |
491 | ||
492 | if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { | |
493 | BT_ERR("Pairing failed (confirmation values mismatch)"); | |
494 | reason = SMP_CONFIRM_FAILED; | |
495 | goto error; | |
496 | } | |
497 | ||
498 | if (hcon->out) { | |
499 | u8 stk[16], rand[8]; | |
500 | __le16 ediv; | |
501 | ||
502 | memset(rand, 0, sizeof(rand)); | |
503 | ediv = 0; | |
504 | ||
505 | smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, key); | |
506 | swap128(key, stk); | |
507 | ||
f7aa611a | 508 | memset(stk + smp->enc_key_size, 0, |
04124681 | 509 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 510 | |
51a8efd7 | 511 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) { |
8aab4757 VCG |
512 | reason = SMP_UNSPECIFIED; |
513 | goto error; | |
514 | } | |
515 | ||
516 | hci_le_start_enc(hcon, ediv, rand, stk); | |
f7aa611a | 517 | hcon->enc_key_size = smp->enc_key_size; |
8aab4757 VCG |
518 | } else { |
519 | u8 stk[16], r[16], rand[8]; | |
520 | __le16 ediv; | |
521 | ||
522 | memset(rand, 0, sizeof(rand)); | |
523 | ediv = 0; | |
524 | ||
525 | swap128(smp->prnd, r); | |
526 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r); | |
527 | ||
528 | smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, key); | |
529 | swap128(key, stk); | |
530 | ||
f7aa611a | 531 | memset(stk + smp->enc_key_size, 0, |
f1560463 | 532 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 533 | |
ce39fb4e | 534 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
04124681 GP |
535 | HCI_SMP_STK_SLAVE, 0, 0, stk, smp->enc_key_size, |
536 | ediv, rand); | |
8aab4757 VCG |
537 | } |
538 | ||
539 | return; | |
540 | ||
541 | error: | |
84794e11 | 542 | smp_failure(conn, reason); |
8aab4757 VCG |
543 | } |
544 | ||
545 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) | |
546 | { | |
547 | struct smp_chan *smp; | |
548 | ||
f1560463 | 549 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); |
8aab4757 VCG |
550 | if (!smp) |
551 | return NULL; | |
552 | ||
553 | INIT_WORK(&smp->confirm, confirm_work); | |
554 | INIT_WORK(&smp->random, random_work); | |
555 | ||
556 | smp->conn = conn; | |
557 | conn->smp_chan = smp; | |
2b64d153 | 558 | conn->hcon->smp_conn = conn; |
8aab4757 VCG |
559 | |
560 | hci_conn_hold(conn->hcon); | |
561 | ||
562 | return smp; | |
563 | } | |
564 | ||
565 | void smp_chan_destroy(struct l2cap_conn *conn) | |
566 | { | |
c8eb9690 | 567 | struct smp_chan *smp = conn->smp_chan; |
f4a407be | 568 | bool complete; |
c8eb9690 | 569 | |
f1c09c07 | 570 | BUG_ON(!smp); |
c8eb9690 | 571 | |
f4a407be JH |
572 | complete = test_bit(SMP_FLAG_COMPLETE, &smp->smp_flags); |
573 | mgmt_smp_complete(conn->hcon, complete); | |
574 | ||
c8eb9690 BG |
575 | kfree(smp); |
576 | conn->smp_chan = NULL; | |
2b64d153 | 577 | conn->hcon->smp_conn = NULL; |
76a68ba0 | 578 | hci_conn_drop(conn->hcon); |
8aab4757 VCG |
579 | } |
580 | ||
2b64d153 BG |
581 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
582 | { | |
583 | struct l2cap_conn *conn = hcon->smp_conn; | |
584 | struct smp_chan *smp; | |
585 | u32 value; | |
586 | u8 key[16]; | |
587 | ||
588 | BT_DBG(""); | |
589 | ||
590 | if (!conn) | |
591 | return -ENOTCONN; | |
592 | ||
593 | smp = conn->smp_chan; | |
594 | ||
595 | switch (mgmt_op) { | |
596 | case MGMT_OP_USER_PASSKEY_REPLY: | |
597 | value = le32_to_cpu(passkey); | |
598 | memset(key, 0, sizeof(key)); | |
599 | BT_DBG("PassKey: %d", value); | |
600 | put_unaligned_le32(value, key); | |
601 | swap128(key, smp->tk); | |
602 | /* Fall Through */ | |
603 | case MGMT_OP_USER_CONFIRM_REPLY: | |
604 | set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); | |
605 | break; | |
606 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: | |
607 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: | |
84794e11 | 608 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
2b64d153 BG |
609 | return 0; |
610 | default: | |
84794e11 | 611 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
2b64d153 BG |
612 | return -EOPNOTSUPP; |
613 | } | |
614 | ||
615 | /* If it is our turn to send Pairing Confirm, do so now */ | |
616 | if (test_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags)) | |
617 | queue_work(hcon->hdev->workqueue, &smp->confirm); | |
618 | ||
619 | return 0; | |
620 | } | |
621 | ||
da85e5e5 | 622 | static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 623 | { |
3158c50c | 624 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
8aab4757 | 625 | struct smp_chan *smp; |
3158c50c | 626 | u8 key_size; |
2b64d153 | 627 | u8 auth = SMP_AUTH_NONE; |
8aab4757 | 628 | int ret; |
88ba43b6 AB |
629 | |
630 | BT_DBG("conn %p", conn); | |
631 | ||
c46b98be JH |
632 | if (skb->len < sizeof(*req)) |
633 | return SMP_UNSPECIFIED; | |
634 | ||
2b64d153 BG |
635 | if (conn->hcon->link_mode & HCI_LM_MASTER) |
636 | return SMP_CMD_NOTSUPP; | |
637 | ||
51a8efd7 | 638 | if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) |
8aab4757 | 639 | smp = smp_chan_create(conn); |
d08fd0e7 AE |
640 | else |
641 | smp = conn->smp_chan; | |
8aab4757 | 642 | |
d08fd0e7 AE |
643 | if (!smp) |
644 | return SMP_UNSPECIFIED; | |
d26a2345 | 645 | |
1c1def09 VCG |
646 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
647 | memcpy(&smp->preq[1], req, sizeof(*req)); | |
3158c50c | 648 | skb_pull(skb, sizeof(*req)); |
88ba43b6 | 649 | |
2b64d153 BG |
650 | /* We didn't start the pairing, so match remote */ |
651 | if (req->auth_req & SMP_AUTH_BONDING) | |
652 | auth = req->auth_req; | |
da85e5e5 | 653 | |
fdde0a26 IY |
654 | conn->hcon->pending_sec_level = authreq_to_seclevel(auth); |
655 | ||
2b64d153 | 656 | build_pairing_cmd(conn, req, &rsp, auth); |
3158c50c VCG |
657 | |
658 | key_size = min(req->max_key_size, rsp.max_key_size); | |
659 | if (check_enc_key_size(conn, key_size)) | |
660 | return SMP_ENC_KEY_SIZE; | |
88ba43b6 | 661 | |
e84a6b13 | 662 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
8aab4757 | 663 | |
1c1def09 VCG |
664 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
665 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); | |
f01ead31 | 666 | |
3158c50c | 667 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
da85e5e5 | 668 | |
2b64d153 BG |
669 | /* Request setup of TK */ |
670 | ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); | |
671 | if (ret) | |
672 | return SMP_UNSPECIFIED; | |
673 | ||
da85e5e5 | 674 | return 0; |
88ba43b6 AB |
675 | } |
676 | ||
da85e5e5 | 677 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 678 | { |
3158c50c | 679 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
1c1def09 | 680 | struct smp_chan *smp = conn->smp_chan; |
8aab4757 | 681 | struct hci_dev *hdev = conn->hcon->hdev; |
2b64d153 | 682 | u8 key_size, auth = SMP_AUTH_NONE; |
7d24ddcc | 683 | int ret; |
88ba43b6 AB |
684 | |
685 | BT_DBG("conn %p", conn); | |
686 | ||
c46b98be JH |
687 | if (skb->len < sizeof(*rsp)) |
688 | return SMP_UNSPECIFIED; | |
689 | ||
2b64d153 BG |
690 | if (!(conn->hcon->link_mode & HCI_LM_MASTER)) |
691 | return SMP_CMD_NOTSUPP; | |
692 | ||
3158c50c VCG |
693 | skb_pull(skb, sizeof(*rsp)); |
694 | ||
1c1def09 | 695 | req = (void *) &smp->preq[1]; |
da85e5e5 | 696 | |
3158c50c VCG |
697 | key_size = min(req->max_key_size, rsp->max_key_size); |
698 | if (check_enc_key_size(conn, key_size)) | |
699 | return SMP_ENC_KEY_SIZE; | |
700 | ||
e84a6b13 | 701 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
7d24ddcc | 702 | |
8aab4757 VCG |
703 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
704 | memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); | |
7d24ddcc | 705 | |
2b64d153 | 706 | if ((req->auth_req & SMP_AUTH_BONDING) && |
f1560463 | 707 | (rsp->auth_req & SMP_AUTH_BONDING)) |
2b64d153 BG |
708 | auth = SMP_AUTH_BONDING; |
709 | ||
710 | auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM; | |
711 | ||
476585ec | 712 | ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); |
2b64d153 BG |
713 | if (ret) |
714 | return SMP_UNSPECIFIED; | |
715 | ||
716 | set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); | |
717 | ||
718 | /* Can't compose response until we have been confirmed */ | |
719 | if (!test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) | |
720 | return 0; | |
721 | ||
8aab4757 | 722 | queue_work(hdev->workqueue, &smp->confirm); |
da85e5e5 VCG |
723 | |
724 | return 0; | |
88ba43b6 AB |
725 | } |
726 | ||
da85e5e5 | 727 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 728 | { |
1c1def09 | 729 | struct smp_chan *smp = conn->smp_chan; |
8aab4757 | 730 | struct hci_dev *hdev = conn->hcon->hdev; |
7d24ddcc | 731 | |
88ba43b6 AB |
732 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
733 | ||
c46b98be JH |
734 | if (skb->len < sizeof(smp->pcnf)) |
735 | return SMP_UNSPECIFIED; | |
736 | ||
1c1def09 VCG |
737 | memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); |
738 | skb_pull(skb, sizeof(smp->pcnf)); | |
88ba43b6 | 739 | |
7d24ddcc AB |
740 | if (conn->hcon->out) { |
741 | u8 random[16]; | |
88ba43b6 | 742 | |
1c1def09 | 743 | swap128(smp->prnd, random); |
88ba43b6 | 744 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random), |
f1560463 | 745 | random); |
2b64d153 | 746 | } else if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) { |
8aab4757 | 747 | queue_work(hdev->workqueue, &smp->confirm); |
2b64d153 BG |
748 | } else { |
749 | set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); | |
88ba43b6 | 750 | } |
da85e5e5 VCG |
751 | |
752 | return 0; | |
88ba43b6 AB |
753 | } |
754 | ||
da85e5e5 | 755 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 756 | { |
1c1def09 | 757 | struct smp_chan *smp = conn->smp_chan; |
8aab4757 | 758 | struct hci_dev *hdev = conn->hcon->hdev; |
7d24ddcc | 759 | |
8aab4757 | 760 | BT_DBG("conn %p", conn); |
3158c50c | 761 | |
c46b98be JH |
762 | if (skb->len < sizeof(smp->rrnd)) |
763 | return SMP_UNSPECIFIED; | |
764 | ||
8aab4757 VCG |
765 | swap128(skb->data, smp->rrnd); |
766 | skb_pull(skb, sizeof(smp->rrnd)); | |
e7e62c85 | 767 | |
8aab4757 | 768 | queue_work(hdev->workqueue, &smp->random); |
da85e5e5 VCG |
769 | |
770 | return 0; | |
88ba43b6 AB |
771 | } |
772 | ||
4dab7864 | 773 | static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) |
988c5997 | 774 | { |
c9839a11 | 775 | struct smp_ltk *key; |
988c5997 VCG |
776 | struct hci_conn *hcon = conn->hcon; |
777 | ||
98a0b845 JH |
778 | key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type, |
779 | hcon->out); | |
988c5997 VCG |
780 | if (!key) |
781 | return 0; | |
782 | ||
4dab7864 JH |
783 | if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated) |
784 | return 0; | |
785 | ||
51a8efd7 | 786 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
988c5997 VCG |
787 | return 1; |
788 | ||
c9839a11 VCG |
789 | hci_le_start_enc(hcon, key->ediv, key->rand, key->val); |
790 | hcon->enc_key_size = key->enc_size; | |
988c5997 VCG |
791 | |
792 | return 1; | |
988c5997 | 793 | } |
f1560463 | 794 | |
da85e5e5 | 795 | static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 AB |
796 | { |
797 | struct smp_cmd_security_req *rp = (void *) skb->data; | |
798 | struct smp_cmd_pairing cp; | |
f1cb9af5 | 799 | struct hci_conn *hcon = conn->hcon; |
8aab4757 | 800 | struct smp_chan *smp; |
88ba43b6 AB |
801 | |
802 | BT_DBG("conn %p", conn); | |
803 | ||
c46b98be JH |
804 | if (skb->len < sizeof(*rp)) |
805 | return SMP_UNSPECIFIED; | |
806 | ||
86ca9eac JH |
807 | if (!(conn->hcon->link_mode & HCI_LM_MASTER)) |
808 | return SMP_CMD_NOTSUPP; | |
809 | ||
2b64d153 | 810 | hcon->pending_sec_level = authreq_to_seclevel(rp->auth_req); |
feb45eb5 | 811 | |
4dab7864 | 812 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
988c5997 VCG |
813 | return 0; |
814 | ||
51a8efd7 | 815 | if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
da85e5e5 | 816 | return 0; |
f1cb9af5 | 817 | |
8aab4757 | 818 | smp = smp_chan_create(conn); |
d26a2345 | 819 | |
88ba43b6 | 820 | skb_pull(skb, sizeof(*rp)); |
88ba43b6 | 821 | |
da85e5e5 | 822 | memset(&cp, 0, sizeof(cp)); |
54790f73 | 823 | build_pairing_cmd(conn, &cp, NULL, rp->auth_req); |
88ba43b6 | 824 | |
1c1def09 VCG |
825 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
826 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 827 | |
88ba43b6 | 828 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
f1cb9af5 | 829 | |
da85e5e5 | 830 | return 0; |
88ba43b6 AB |
831 | } |
832 | ||
ad32a2f5 JH |
833 | bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level) |
834 | { | |
835 | if (sec_level == BT_SECURITY_LOW) | |
836 | return true; | |
837 | ||
838 | if (hcon->sec_level >= sec_level) | |
839 | return true; | |
840 | ||
841 | return false; | |
842 | } | |
843 | ||
cc110922 | 844 | int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) |
eb492e01 | 845 | { |
cc110922 | 846 | struct l2cap_conn *conn = hcon->l2cap_data; |
1c1def09 | 847 | struct smp_chan *smp = conn->smp_chan; |
2b64d153 | 848 | __u8 authreq; |
eb492e01 | 849 | |
3a0259bb VCG |
850 | BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); |
851 | ||
757aee0f | 852 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) |
2e65c9d2 AG |
853 | return 1; |
854 | ||
ad32a2f5 | 855 | if (smp_sufficient_security(hcon, sec_level)) |
eb492e01 | 856 | return 1; |
f1cb9af5 | 857 | |
988c5997 | 858 | if (hcon->link_mode & HCI_LM_MASTER) |
4dab7864 | 859 | if (smp_ltk_encrypt(conn, sec_level)) |
02bc7455 | 860 | goto done; |
d26a2345 | 861 | |
51a8efd7 | 862 | if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
d26a2345 VCG |
863 | return 0; |
864 | ||
8aab4757 | 865 | smp = smp_chan_create(conn); |
2b64d153 BG |
866 | if (!smp) |
867 | return 1; | |
868 | ||
869 | authreq = seclevel_to_authreq(sec_level); | |
d26a2345 | 870 | |
d26a2345 VCG |
871 | if (hcon->link_mode & HCI_LM_MASTER) { |
872 | struct smp_cmd_pairing cp; | |
f01ead31 | 873 | |
2b64d153 | 874 | build_pairing_cmd(conn, &cp, NULL, authreq); |
1c1def09 VCG |
875 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
876 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 877 | |
eb492e01 AB |
878 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
879 | } else { | |
880 | struct smp_cmd_security_req cp; | |
2b64d153 | 881 | cp.auth_req = authreq; |
eb492e01 AB |
882 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
883 | } | |
884 | ||
02bc7455 | 885 | done: |
f1cb9af5 | 886 | hcon->pending_sec_level = sec_level; |
f1cb9af5 | 887 | |
eb492e01 AB |
888 | return 0; |
889 | } | |
890 | ||
7034b911 VCG |
891 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
892 | { | |
16b90839 | 893 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
1c1def09 | 894 | struct smp_chan *smp = conn->smp_chan; |
16b90839 | 895 | |
c46b98be JH |
896 | BT_DBG("conn %p", conn); |
897 | ||
898 | if (skb->len < sizeof(*rp)) | |
899 | return SMP_UNSPECIFIED; | |
900 | ||
6131ddc8 JH |
901 | /* Ignore this PDU if it wasn't requested */ |
902 | if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY)) | |
903 | return 0; | |
904 | ||
16b90839 VCG |
905 | skb_pull(skb, sizeof(*rp)); |
906 | ||
1c1def09 | 907 | memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); |
16b90839 | 908 | |
7034b911 VCG |
909 | return 0; |
910 | } | |
911 | ||
912 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) | |
913 | { | |
16b90839 | 914 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
1c1def09 | 915 | struct smp_chan *smp = conn->smp_chan; |
c9839a11 VCG |
916 | struct hci_dev *hdev = conn->hcon->hdev; |
917 | struct hci_conn *hcon = conn->hcon; | |
918 | u8 authenticated; | |
16b90839 | 919 | |
c46b98be JH |
920 | BT_DBG("conn %p", conn); |
921 | ||
922 | if (skb->len < sizeof(*rp)) | |
923 | return SMP_UNSPECIFIED; | |
924 | ||
6131ddc8 JH |
925 | /* Ignore this PDU if it wasn't requested */ |
926 | if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY)) | |
927 | return 0; | |
928 | ||
16b90839 | 929 | skb_pull(skb, sizeof(*rp)); |
7034b911 | 930 | |
c9839a11 | 931 | hci_dev_lock(hdev); |
ce39fb4e MH |
932 | authenticated = (hcon->sec_level == BT_SECURITY_HIGH); |
933 | hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, HCI_SMP_LTK, 1, | |
934 | authenticated, smp->tk, smp->enc_key_size, | |
04124681 | 935 | rp->ediv, rp->rand); |
fd349c02 JH |
936 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) |
937 | smp_distribute_keys(conn, 1); | |
c9839a11 | 938 | hci_dev_unlock(hdev); |
7034b911 VCG |
939 | |
940 | return 0; | |
941 | } | |
942 | ||
fd349c02 JH |
943 | static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) |
944 | { | |
945 | struct smp_cmd_ident_info *info = (void *) skb->data; | |
946 | struct smp_chan *smp = conn->smp_chan; | |
947 | ||
948 | BT_DBG(""); | |
949 | ||
950 | if (skb->len < sizeof(*info)) | |
951 | return SMP_UNSPECIFIED; | |
952 | ||
6131ddc8 JH |
953 | /* Ignore this PDU if it wasn't requested */ |
954 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) | |
955 | return 0; | |
956 | ||
fd349c02 JH |
957 | skb_pull(skb, sizeof(*info)); |
958 | ||
959 | memcpy(smp->irk, info->irk, 16); | |
960 | ||
961 | return 0; | |
962 | } | |
963 | ||
964 | static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, | |
965 | struct sk_buff *skb) | |
966 | { | |
967 | struct smp_cmd_ident_addr_info *info = (void *) skb->data; | |
968 | struct smp_chan *smp = conn->smp_chan; | |
969 | struct hci_conn *hcon = conn->hcon; | |
970 | bdaddr_t rpa; | |
971 | ||
972 | BT_DBG(""); | |
973 | ||
974 | if (skb->len < sizeof(*info)) | |
975 | return SMP_UNSPECIFIED; | |
976 | ||
6131ddc8 JH |
977 | /* Ignore this PDU if it wasn't requested */ |
978 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) | |
979 | return 0; | |
980 | ||
fd349c02 JH |
981 | skb_pull(skb, sizeof(*info)); |
982 | ||
983 | bacpy(&smp->id_addr, &info->bdaddr); | |
984 | smp->id_addr_type = info->addr_type; | |
985 | ||
986 | if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type)) | |
987 | bacpy(&rpa, &hcon->dst); | |
988 | else | |
989 | bacpy(&rpa, BDADDR_ANY); | |
990 | ||
991 | hci_add_irk(conn->hcon->hdev, &smp->id_addr, smp->id_addr_type, | |
992 | smp->irk, &rpa); | |
993 | ||
68d6f6de JH |
994 | /* Track the connection based on the Identity Address from now on */ |
995 | bacpy(&hcon->dst, &smp->id_addr); | |
996 | hcon->dst_type = smp->id_addr_type; | |
997 | ||
387a33e3 JH |
998 | l2cap_conn_update_id_addr(hcon); |
999 | ||
fd349c02 JH |
1000 | smp_distribute_keys(conn, 1); |
1001 | ||
1002 | return 0; | |
1003 | } | |
1004 | ||
eb492e01 AB |
1005 | int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) |
1006 | { | |
7b9899db | 1007 | struct hci_conn *hcon = conn->hcon; |
92381f5c | 1008 | __u8 code, reason; |
eb492e01 AB |
1009 | int err = 0; |
1010 | ||
7b9899db MH |
1011 | if (hcon->type != LE_LINK) { |
1012 | kfree_skb(skb); | |
3432711f | 1013 | return 0; |
7b9899db MH |
1014 | } |
1015 | ||
92381f5c MH |
1016 | if (skb->len < 1) { |
1017 | kfree_skb(skb); | |
1018 | return -EILSEQ; | |
1019 | } | |
1020 | ||
06ae3314 | 1021 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { |
2e65c9d2 AG |
1022 | err = -ENOTSUPP; |
1023 | reason = SMP_PAIRING_NOTSUPP; | |
1024 | goto done; | |
1025 | } | |
1026 | ||
92381f5c | 1027 | code = skb->data[0]; |
eb492e01 AB |
1028 | skb_pull(skb, sizeof(code)); |
1029 | ||
8cf9fa12 JH |
1030 | /* |
1031 | * The SMP context must be initialized for all other PDUs except | |
1032 | * pairing and security requests. If we get any other PDU when | |
1033 | * not initialized simply disconnect (done if this function | |
1034 | * returns an error). | |
1035 | */ | |
1036 | if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ && | |
1037 | !conn->smp_chan) { | |
1038 | BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code); | |
1039 | kfree_skb(skb); | |
1040 | return -ENOTSUPP; | |
1041 | } | |
1042 | ||
eb492e01 AB |
1043 | switch (code) { |
1044 | case SMP_CMD_PAIRING_REQ: | |
da85e5e5 | 1045 | reason = smp_cmd_pairing_req(conn, skb); |
eb492e01 AB |
1046 | break; |
1047 | ||
1048 | case SMP_CMD_PAIRING_FAIL: | |
84794e11 | 1049 | smp_failure(conn, 0); |
da85e5e5 VCG |
1050 | reason = 0; |
1051 | err = -EPERM; | |
eb492e01 AB |
1052 | break; |
1053 | ||
1054 | case SMP_CMD_PAIRING_RSP: | |
da85e5e5 | 1055 | reason = smp_cmd_pairing_rsp(conn, skb); |
88ba43b6 AB |
1056 | break; |
1057 | ||
1058 | case SMP_CMD_SECURITY_REQ: | |
da85e5e5 | 1059 | reason = smp_cmd_security_req(conn, skb); |
88ba43b6 AB |
1060 | break; |
1061 | ||
eb492e01 | 1062 | case SMP_CMD_PAIRING_CONFIRM: |
da85e5e5 | 1063 | reason = smp_cmd_pairing_confirm(conn, skb); |
88ba43b6 AB |
1064 | break; |
1065 | ||
eb492e01 | 1066 | case SMP_CMD_PAIRING_RANDOM: |
da85e5e5 | 1067 | reason = smp_cmd_pairing_random(conn, skb); |
88ba43b6 AB |
1068 | break; |
1069 | ||
eb492e01 | 1070 | case SMP_CMD_ENCRYPT_INFO: |
7034b911 VCG |
1071 | reason = smp_cmd_encrypt_info(conn, skb); |
1072 | break; | |
1073 | ||
eb492e01 | 1074 | case SMP_CMD_MASTER_IDENT: |
7034b911 VCG |
1075 | reason = smp_cmd_master_ident(conn, skb); |
1076 | break; | |
1077 | ||
eb492e01 | 1078 | case SMP_CMD_IDENT_INFO: |
fd349c02 JH |
1079 | reason = smp_cmd_ident_info(conn, skb); |
1080 | break; | |
1081 | ||
eb492e01 | 1082 | case SMP_CMD_IDENT_ADDR_INFO: |
fd349c02 JH |
1083 | reason = smp_cmd_ident_addr_info(conn, skb); |
1084 | break; | |
1085 | ||
eb492e01 | 1086 | case SMP_CMD_SIGN_INFO: |
7034b911 VCG |
1087 | /* Just ignored */ |
1088 | reason = 0; | |
1089 | break; | |
1090 | ||
eb492e01 AB |
1091 | default: |
1092 | BT_DBG("Unknown command code 0x%2.2x", code); | |
1093 | ||
1094 | reason = SMP_CMD_NOTSUPP; | |
eb492e01 | 1095 | err = -EOPNOTSUPP; |
3a0259bb | 1096 | goto done; |
eb492e01 AB |
1097 | } |
1098 | ||
3a0259bb VCG |
1099 | done: |
1100 | if (reason) | |
84794e11 | 1101 | smp_failure(conn, reason); |
3a0259bb | 1102 | |
eb492e01 AB |
1103 | kfree_skb(skb); |
1104 | return err; | |
1105 | } | |
7034b911 VCG |
1106 | |
1107 | int smp_distribute_keys(struct l2cap_conn *conn, __u8 force) | |
1108 | { | |
1109 | struct smp_cmd_pairing *req, *rsp; | |
1c1def09 | 1110 | struct smp_chan *smp = conn->smp_chan; |
7034b911 VCG |
1111 | __u8 *keydist; |
1112 | ||
1113 | BT_DBG("conn %p force %d", conn, force); | |
1114 | ||
51a8efd7 | 1115 | if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) |
d26a2345 VCG |
1116 | return 0; |
1117 | ||
1c1def09 | 1118 | rsp = (void *) &smp->prsp[1]; |
7034b911 VCG |
1119 | |
1120 | /* The responder sends its keys first */ | |
1121 | if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07)) | |
1122 | return 0; | |
1123 | ||
1c1def09 | 1124 | req = (void *) &smp->preq[1]; |
7034b911 VCG |
1125 | |
1126 | if (conn->hcon->out) { | |
1127 | keydist = &rsp->init_key_dist; | |
1128 | *keydist &= req->init_key_dist; | |
1129 | } else { | |
1130 | keydist = &rsp->resp_key_dist; | |
1131 | *keydist &= req->resp_key_dist; | |
1132 | } | |
1133 | ||
7034b911 VCG |
1134 | BT_DBG("keydist 0x%x", *keydist); |
1135 | ||
1136 | if (*keydist & SMP_DIST_ENC_KEY) { | |
1137 | struct smp_cmd_encrypt_info enc; | |
1138 | struct smp_cmd_master_ident ident; | |
c9839a11 VCG |
1139 | struct hci_conn *hcon = conn->hcon; |
1140 | u8 authenticated; | |
7034b911 VCG |
1141 | __le16 ediv; |
1142 | ||
1143 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); | |
1144 | get_random_bytes(&ediv, sizeof(ediv)); | |
1145 | get_random_bytes(ident.rand, sizeof(ident.rand)); | |
1146 | ||
1147 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); | |
1148 | ||
c9839a11 | 1149 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; |
ce39fb4e | 1150 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
04124681 GP |
1151 | HCI_SMP_LTK_SLAVE, 1, authenticated, |
1152 | enc.ltk, smp->enc_key_size, ediv, ident.rand); | |
16b90839 | 1153 | |
58115373 | 1154 | ident.ediv = ediv; |
7034b911 VCG |
1155 | |
1156 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); | |
1157 | ||
1158 | *keydist &= ~SMP_DIST_ENC_KEY; | |
1159 | } | |
1160 | ||
1161 | if (*keydist & SMP_DIST_ID_KEY) { | |
1162 | struct smp_cmd_ident_addr_info addrinfo; | |
1163 | struct smp_cmd_ident_info idinfo; | |
1164 | ||
1165 | /* Send a dummy key */ | |
1166 | get_random_bytes(idinfo.irk, sizeof(idinfo.irk)); | |
1167 | ||
1168 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); | |
1169 | ||
1170 | /* Just public address */ | |
1171 | memset(&addrinfo, 0, sizeof(addrinfo)); | |
2b36a562 | 1172 | bacpy(&addrinfo.bdaddr, &conn->hcon->src); |
7034b911 VCG |
1173 | |
1174 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), | |
f1560463 | 1175 | &addrinfo); |
7034b911 VCG |
1176 | |
1177 | *keydist &= ~SMP_DIST_ID_KEY; | |
1178 | } | |
1179 | ||
1180 | if (*keydist & SMP_DIST_SIGN) { | |
1181 | struct smp_cmd_sign_info sign; | |
1182 | ||
1183 | /* Send a dummy key */ | |
1184 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); | |
1185 | ||
1186 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); | |
1187 | ||
1188 | *keydist &= ~SMP_DIST_SIGN; | |
1189 | } | |
1190 | ||
b7d448d7 | 1191 | if (conn->hcon->out || force || !(rsp->init_key_dist & 0x07)) { |
51a8efd7 | 1192 | clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags); |
6c9d42a1 | 1193 | cancel_delayed_work_sync(&conn->security_timer); |
f4a407be | 1194 | set_bit(SMP_FLAG_COMPLETE, &smp->smp_flags); |
8aab4757 | 1195 | smp_chan_destroy(conn); |
d26a2345 VCG |
1196 | } |
1197 | ||
7034b911 VCG |
1198 | return 0; |
1199 | } |