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 | { |
2b64d153 | 252 | u8 dist_keys = 0; |
54790f73 | 253 | |
a8b2d5c2 | 254 | if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->dev_flags)) { |
ca10b5ee | 255 | dist_keys = SMP_DIST_ENC_KEY; |
54790f73 | 256 | authreq |= SMP_AUTH_BONDING; |
2b64d153 BG |
257 | } else { |
258 | authreq &= ~SMP_AUTH_BONDING; | |
54790f73 VCG |
259 | } |
260 | ||
261 | if (rsp == NULL) { | |
262 | req->io_capability = conn->hcon->io_capability; | |
263 | req->oob_flag = SMP_OOB_NOT_PRESENT; | |
264 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
0cf73b9f | 265 | req->init_key_dist = dist_keys; |
54790f73 | 266 | req->resp_key_dist = dist_keys; |
065a13e2 | 267 | req->auth_req = (authreq & AUTH_REQ_MASK); |
54790f73 VCG |
268 | return; |
269 | } | |
270 | ||
271 | rsp->io_capability = conn->hcon->io_capability; | |
272 | rsp->oob_flag = SMP_OOB_NOT_PRESENT; | |
273 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; | |
0cf73b9f | 274 | rsp->init_key_dist = req->init_key_dist & dist_keys; |
54790f73 | 275 | rsp->resp_key_dist = req->resp_key_dist & dist_keys; |
065a13e2 | 276 | rsp->auth_req = (authreq & AUTH_REQ_MASK); |
b8e66eac VCG |
277 | } |
278 | ||
3158c50c VCG |
279 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
280 | { | |
1c1def09 VCG |
281 | struct smp_chan *smp = conn->smp_chan; |
282 | ||
3158c50c | 283 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
f1560463 | 284 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
3158c50c VCG |
285 | return SMP_ENC_KEY_SIZE; |
286 | ||
f7aa611a | 287 | smp->enc_key_size = max_key_size; |
3158c50c VCG |
288 | |
289 | return 0; | |
290 | } | |
291 | ||
84794e11 | 292 | static void smp_failure(struct l2cap_conn *conn, u8 reason) |
4f957a76 | 293 | { |
bab73cb6 JH |
294 | struct hci_conn *hcon = conn->hcon; |
295 | ||
84794e11 | 296 | if (reason) |
4f957a76 | 297 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
f1560463 | 298 | &reason); |
4f957a76 | 299 | |
ce39fb4e MH |
300 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags); |
301 | mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type, | |
302 | HCI_ERROR_AUTH_FAILURE); | |
f1c09c07 | 303 | |
61a0cfb0 AG |
304 | cancel_delayed_work_sync(&conn->security_timer); |
305 | ||
ce39fb4e | 306 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
f1c09c07 | 307 | smp_chan_destroy(conn); |
4f957a76 BG |
308 | } |
309 | ||
2b64d153 BG |
310 | #define JUST_WORKS 0x00 |
311 | #define JUST_CFM 0x01 | |
312 | #define REQ_PASSKEY 0x02 | |
313 | #define CFM_PASSKEY 0x03 | |
314 | #define REQ_OOB 0x04 | |
315 | #define OVERLAP 0xFF | |
316 | ||
317 | static const u8 gen_method[5][5] = { | |
318 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
319 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, | |
320 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, | |
321 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, | |
322 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, | |
323 | }; | |
324 | ||
325 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, | |
326 | u8 local_io, u8 remote_io) | |
327 | { | |
328 | struct hci_conn *hcon = conn->hcon; | |
329 | struct smp_chan *smp = conn->smp_chan; | |
330 | u8 method; | |
331 | u32 passkey = 0; | |
332 | int ret = 0; | |
333 | ||
334 | /* Initialize key for JUST WORKS */ | |
335 | memset(smp->tk, 0, sizeof(smp->tk)); | |
336 | clear_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); | |
337 | ||
338 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); | |
339 | ||
340 | /* If neither side wants MITM, use JUST WORKS */ | |
341 | /* If either side has unknown io_caps, use JUST WORKS */ | |
342 | /* Otherwise, look up method from the table */ | |
343 | if (!(auth & SMP_AUTH_MITM) || | |
f1560463 MH |
344 | local_io > SMP_IO_KEYBOARD_DISPLAY || |
345 | remote_io > SMP_IO_KEYBOARD_DISPLAY) | |
2b64d153 BG |
346 | method = JUST_WORKS; |
347 | else | |
b3ff53ff | 348 | method = gen_method[remote_io][local_io]; |
2b64d153 BG |
349 | |
350 | /* If not bonding, don't ask user to confirm a Zero TK */ | |
351 | if (!(auth & SMP_AUTH_BONDING) && method == JUST_CFM) | |
352 | method = JUST_WORKS; | |
353 | ||
354 | /* If Just Works, Continue with Zero TK */ | |
355 | if (method == JUST_WORKS) { | |
356 | set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); | |
357 | return 0; | |
358 | } | |
359 | ||
360 | /* Not Just Works/Confirm results in MITM Authentication */ | |
361 | if (method != JUST_CFM) | |
362 | set_bit(SMP_FLAG_MITM_AUTH, &smp->smp_flags); | |
363 | ||
364 | /* If both devices have Keyoard-Display I/O, the master | |
365 | * Confirms and the slave Enters the passkey. | |
366 | */ | |
367 | if (method == OVERLAP) { | |
368 | if (hcon->link_mode & HCI_LM_MASTER) | |
369 | method = CFM_PASSKEY; | |
370 | else | |
371 | method = REQ_PASSKEY; | |
372 | } | |
373 | ||
374 | /* Generate random passkey. Not valid until confirmed. */ | |
375 | if (method == CFM_PASSKEY) { | |
376 | u8 key[16]; | |
377 | ||
378 | memset(key, 0, sizeof(key)); | |
379 | get_random_bytes(&passkey, sizeof(passkey)); | |
380 | passkey %= 1000000; | |
381 | put_unaligned_le32(passkey, key); | |
382 | swap128(key, smp->tk); | |
383 | BT_DBG("PassKey: %d", passkey); | |
384 | } | |
385 | ||
386 | hci_dev_lock(hcon->hdev); | |
387 | ||
388 | if (method == REQ_PASSKEY) | |
ce39fb4e | 389 | ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, |
272d90df | 390 | hcon->type, hcon->dst_type); |
2b64d153 | 391 | else |
ce39fb4e | 392 | ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, |
272d90df | 393 | hcon->type, hcon->dst_type, |
2b64d153 BG |
394 | cpu_to_le32(passkey), 0); |
395 | ||
396 | hci_dev_unlock(hcon->hdev); | |
397 | ||
398 | return ret; | |
399 | } | |
400 | ||
8aab4757 VCG |
401 | static void confirm_work(struct work_struct *work) |
402 | { | |
403 | struct smp_chan *smp = container_of(work, struct smp_chan, confirm); | |
404 | struct l2cap_conn *conn = smp->conn; | |
405 | struct crypto_blkcipher *tfm; | |
406 | struct smp_cmd_pairing_confirm cp; | |
407 | int ret; | |
408 | u8 res[16], reason; | |
409 | ||
410 | BT_DBG("conn %p", conn); | |
411 | ||
412 | tfm = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); | |
413 | if (IS_ERR(tfm)) { | |
414 | reason = SMP_UNSPECIFIED; | |
415 | goto error; | |
416 | } | |
417 | ||
418 | smp->tfm = tfm; | |
419 | ||
420 | if (conn->hcon->out) | |
c8462ca6 MH |
421 | ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, |
422 | conn->hcon->src_type, &conn->hcon->src, | |
423 | conn->hcon->dst_type, &conn->hcon->dst, res); | |
8aab4757 VCG |
424 | else |
425 | ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, | |
c8462ca6 MH |
426 | conn->hcon->dst_type, &conn->hcon->dst, |
427 | conn->hcon->src_type, &conn->hcon->src, res); | |
8aab4757 VCG |
428 | if (ret) { |
429 | reason = SMP_UNSPECIFIED; | |
430 | goto error; | |
431 | } | |
432 | ||
2b64d153 BG |
433 | clear_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); |
434 | ||
8aab4757 VCG |
435 | swap128(res, cp.confirm_val); |
436 | smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); | |
437 | ||
438 | return; | |
439 | ||
440 | error: | |
84794e11 | 441 | smp_failure(conn, reason); |
8aab4757 VCG |
442 | } |
443 | ||
444 | static void random_work(struct work_struct *work) | |
445 | { | |
446 | struct smp_chan *smp = container_of(work, struct smp_chan, random); | |
447 | struct l2cap_conn *conn = smp->conn; | |
448 | struct hci_conn *hcon = conn->hcon; | |
449 | struct crypto_blkcipher *tfm = smp->tfm; | |
450 | u8 reason, confirm[16], res[16], key[16]; | |
451 | int ret; | |
452 | ||
453 | if (IS_ERR_OR_NULL(tfm)) { | |
454 | reason = SMP_UNSPECIFIED; | |
455 | goto error; | |
456 | } | |
457 | ||
458 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); | |
459 | ||
460 | if (hcon->out) | |
c8462ca6 MH |
461 | ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
462 | hcon->src_type, &hcon->src, | |
463 | hcon->dst_type, &hcon->dst, res); | |
8aab4757 VCG |
464 | else |
465 | ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, | |
c8462ca6 MH |
466 | hcon->dst_type, &hcon->dst, |
467 | hcon->src_type, &hcon->src, res); | |
8aab4757 VCG |
468 | if (ret) { |
469 | reason = SMP_UNSPECIFIED; | |
470 | goto error; | |
471 | } | |
472 | ||
473 | swap128(res, confirm); | |
474 | ||
475 | if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { | |
476 | BT_ERR("Pairing failed (confirmation values mismatch)"); | |
477 | reason = SMP_CONFIRM_FAILED; | |
478 | goto error; | |
479 | } | |
480 | ||
481 | if (hcon->out) { | |
482 | u8 stk[16], rand[8]; | |
483 | __le16 ediv; | |
484 | ||
485 | memset(rand, 0, sizeof(rand)); | |
486 | ediv = 0; | |
487 | ||
488 | smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, key); | |
489 | swap128(key, stk); | |
490 | ||
f7aa611a | 491 | memset(stk + smp->enc_key_size, 0, |
04124681 | 492 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 493 | |
51a8efd7 | 494 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) { |
8aab4757 VCG |
495 | reason = SMP_UNSPECIFIED; |
496 | goto error; | |
497 | } | |
498 | ||
499 | hci_le_start_enc(hcon, ediv, rand, stk); | |
f7aa611a | 500 | hcon->enc_key_size = smp->enc_key_size; |
8aab4757 VCG |
501 | } else { |
502 | u8 stk[16], r[16], rand[8]; | |
503 | __le16 ediv; | |
504 | ||
505 | memset(rand, 0, sizeof(rand)); | |
506 | ediv = 0; | |
507 | ||
508 | swap128(smp->prnd, r); | |
509 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r); | |
510 | ||
511 | smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, key); | |
512 | swap128(key, stk); | |
513 | ||
f7aa611a | 514 | memset(stk + smp->enc_key_size, 0, |
f1560463 | 515 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
8aab4757 | 516 | |
ce39fb4e | 517 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
04124681 GP |
518 | HCI_SMP_STK_SLAVE, 0, 0, stk, smp->enc_key_size, |
519 | ediv, rand); | |
8aab4757 VCG |
520 | } |
521 | ||
522 | return; | |
523 | ||
524 | error: | |
84794e11 | 525 | smp_failure(conn, reason); |
8aab4757 VCG |
526 | } |
527 | ||
528 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) | |
529 | { | |
530 | struct smp_chan *smp; | |
531 | ||
f1560463 | 532 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); |
8aab4757 VCG |
533 | if (!smp) |
534 | return NULL; | |
535 | ||
536 | INIT_WORK(&smp->confirm, confirm_work); | |
537 | INIT_WORK(&smp->random, random_work); | |
538 | ||
539 | smp->conn = conn; | |
540 | conn->smp_chan = smp; | |
2b64d153 | 541 | conn->hcon->smp_conn = conn; |
8aab4757 VCG |
542 | |
543 | hci_conn_hold(conn->hcon); | |
544 | ||
545 | return smp; | |
546 | } | |
547 | ||
548 | void smp_chan_destroy(struct l2cap_conn *conn) | |
549 | { | |
c8eb9690 BG |
550 | struct smp_chan *smp = conn->smp_chan; |
551 | ||
f1c09c07 | 552 | BUG_ON(!smp); |
c8eb9690 BG |
553 | |
554 | if (smp->tfm) | |
555 | crypto_free_blkcipher(smp->tfm); | |
556 | ||
557 | kfree(smp); | |
558 | conn->smp_chan = NULL; | |
2b64d153 | 559 | conn->hcon->smp_conn = NULL; |
76a68ba0 | 560 | hci_conn_drop(conn->hcon); |
8aab4757 VCG |
561 | } |
562 | ||
2b64d153 BG |
563 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
564 | { | |
565 | struct l2cap_conn *conn = hcon->smp_conn; | |
566 | struct smp_chan *smp; | |
567 | u32 value; | |
568 | u8 key[16]; | |
569 | ||
570 | BT_DBG(""); | |
571 | ||
572 | if (!conn) | |
573 | return -ENOTCONN; | |
574 | ||
575 | smp = conn->smp_chan; | |
576 | ||
577 | switch (mgmt_op) { | |
578 | case MGMT_OP_USER_PASSKEY_REPLY: | |
579 | value = le32_to_cpu(passkey); | |
580 | memset(key, 0, sizeof(key)); | |
581 | BT_DBG("PassKey: %d", value); | |
582 | put_unaligned_le32(value, key); | |
583 | swap128(key, smp->tk); | |
584 | /* Fall Through */ | |
585 | case MGMT_OP_USER_CONFIRM_REPLY: | |
586 | set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); | |
587 | break; | |
588 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: | |
589 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: | |
84794e11 | 590 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
2b64d153 BG |
591 | return 0; |
592 | default: | |
84794e11 | 593 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
2b64d153 BG |
594 | return -EOPNOTSUPP; |
595 | } | |
596 | ||
597 | /* If it is our turn to send Pairing Confirm, do so now */ | |
598 | if (test_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags)) | |
599 | queue_work(hcon->hdev->workqueue, &smp->confirm); | |
600 | ||
601 | return 0; | |
602 | } | |
603 | ||
da85e5e5 | 604 | static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 605 | { |
3158c50c | 606 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
8aab4757 | 607 | struct smp_chan *smp; |
3158c50c | 608 | u8 key_size; |
2b64d153 | 609 | u8 auth = SMP_AUTH_NONE; |
8aab4757 | 610 | int ret; |
88ba43b6 AB |
611 | |
612 | BT_DBG("conn %p", conn); | |
613 | ||
c46b98be JH |
614 | if (skb->len < sizeof(*req)) |
615 | return SMP_UNSPECIFIED; | |
616 | ||
2b64d153 BG |
617 | if (conn->hcon->link_mode & HCI_LM_MASTER) |
618 | return SMP_CMD_NOTSUPP; | |
619 | ||
51a8efd7 | 620 | if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) |
8aab4757 | 621 | smp = smp_chan_create(conn); |
d08fd0e7 AE |
622 | else |
623 | smp = conn->smp_chan; | |
8aab4757 | 624 | |
d08fd0e7 AE |
625 | if (!smp) |
626 | return SMP_UNSPECIFIED; | |
d26a2345 | 627 | |
1c1def09 VCG |
628 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
629 | memcpy(&smp->preq[1], req, sizeof(*req)); | |
3158c50c | 630 | skb_pull(skb, sizeof(*req)); |
88ba43b6 | 631 | |
2b64d153 BG |
632 | /* We didn't start the pairing, so match remote */ |
633 | if (req->auth_req & SMP_AUTH_BONDING) | |
634 | auth = req->auth_req; | |
da85e5e5 | 635 | |
fdde0a26 IY |
636 | conn->hcon->pending_sec_level = authreq_to_seclevel(auth); |
637 | ||
2b64d153 | 638 | build_pairing_cmd(conn, req, &rsp, auth); |
3158c50c VCG |
639 | |
640 | key_size = min(req->max_key_size, rsp.max_key_size); | |
641 | if (check_enc_key_size(conn, key_size)) | |
642 | return SMP_ENC_KEY_SIZE; | |
88ba43b6 | 643 | |
e84a6b13 | 644 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
8aab4757 | 645 | |
1c1def09 VCG |
646 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
647 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); | |
f01ead31 | 648 | |
3158c50c | 649 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
da85e5e5 | 650 | |
2b64d153 BG |
651 | /* Request setup of TK */ |
652 | ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); | |
653 | if (ret) | |
654 | return SMP_UNSPECIFIED; | |
655 | ||
da85e5e5 | 656 | return 0; |
88ba43b6 AB |
657 | } |
658 | ||
da85e5e5 | 659 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 660 | { |
3158c50c | 661 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
1c1def09 | 662 | struct smp_chan *smp = conn->smp_chan; |
8aab4757 | 663 | struct hci_dev *hdev = conn->hcon->hdev; |
2b64d153 | 664 | u8 key_size, auth = SMP_AUTH_NONE; |
7d24ddcc | 665 | int ret; |
88ba43b6 AB |
666 | |
667 | BT_DBG("conn %p", conn); | |
668 | ||
c46b98be JH |
669 | if (skb->len < sizeof(*rsp)) |
670 | return SMP_UNSPECIFIED; | |
671 | ||
2b64d153 BG |
672 | if (!(conn->hcon->link_mode & HCI_LM_MASTER)) |
673 | return SMP_CMD_NOTSUPP; | |
674 | ||
3158c50c VCG |
675 | skb_pull(skb, sizeof(*rsp)); |
676 | ||
1c1def09 | 677 | req = (void *) &smp->preq[1]; |
da85e5e5 | 678 | |
3158c50c VCG |
679 | key_size = min(req->max_key_size, rsp->max_key_size); |
680 | if (check_enc_key_size(conn, key_size)) | |
681 | return SMP_ENC_KEY_SIZE; | |
682 | ||
e84a6b13 | 683 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
7d24ddcc | 684 | |
8aab4757 VCG |
685 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
686 | memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); | |
7d24ddcc | 687 | |
2b64d153 | 688 | if ((req->auth_req & SMP_AUTH_BONDING) && |
f1560463 | 689 | (rsp->auth_req & SMP_AUTH_BONDING)) |
2b64d153 BG |
690 | auth = SMP_AUTH_BONDING; |
691 | ||
692 | auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM; | |
693 | ||
476585ec | 694 | ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); |
2b64d153 BG |
695 | if (ret) |
696 | return SMP_UNSPECIFIED; | |
697 | ||
698 | set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); | |
699 | ||
700 | /* Can't compose response until we have been confirmed */ | |
701 | if (!test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) | |
702 | return 0; | |
703 | ||
8aab4757 | 704 | queue_work(hdev->workqueue, &smp->confirm); |
da85e5e5 VCG |
705 | |
706 | return 0; | |
88ba43b6 AB |
707 | } |
708 | ||
da85e5e5 | 709 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 710 | { |
1c1def09 | 711 | struct smp_chan *smp = conn->smp_chan; |
8aab4757 | 712 | struct hci_dev *hdev = conn->hcon->hdev; |
7d24ddcc | 713 | |
88ba43b6 AB |
714 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
715 | ||
c46b98be JH |
716 | if (skb->len < sizeof(smp->pcnf)) |
717 | return SMP_UNSPECIFIED; | |
718 | ||
1c1def09 VCG |
719 | memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); |
720 | skb_pull(skb, sizeof(smp->pcnf)); | |
88ba43b6 | 721 | |
7d24ddcc AB |
722 | if (conn->hcon->out) { |
723 | u8 random[16]; | |
88ba43b6 | 724 | |
1c1def09 | 725 | swap128(smp->prnd, random); |
88ba43b6 | 726 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random), |
f1560463 | 727 | random); |
2b64d153 | 728 | } else if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) { |
8aab4757 | 729 | queue_work(hdev->workqueue, &smp->confirm); |
2b64d153 BG |
730 | } else { |
731 | set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); | |
88ba43b6 | 732 | } |
da85e5e5 VCG |
733 | |
734 | return 0; | |
88ba43b6 AB |
735 | } |
736 | ||
da85e5e5 | 737 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 | 738 | { |
1c1def09 | 739 | struct smp_chan *smp = conn->smp_chan; |
8aab4757 | 740 | struct hci_dev *hdev = conn->hcon->hdev; |
7d24ddcc | 741 | |
8aab4757 | 742 | BT_DBG("conn %p", conn); |
3158c50c | 743 | |
c46b98be JH |
744 | if (skb->len < sizeof(smp->rrnd)) |
745 | return SMP_UNSPECIFIED; | |
746 | ||
8aab4757 VCG |
747 | swap128(skb->data, smp->rrnd); |
748 | skb_pull(skb, sizeof(smp->rrnd)); | |
e7e62c85 | 749 | |
8aab4757 | 750 | queue_work(hdev->workqueue, &smp->random); |
da85e5e5 VCG |
751 | |
752 | return 0; | |
88ba43b6 AB |
753 | } |
754 | ||
4dab7864 | 755 | static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) |
988c5997 | 756 | { |
c9839a11 | 757 | struct smp_ltk *key; |
988c5997 VCG |
758 | struct hci_conn *hcon = conn->hcon; |
759 | ||
98a0b845 JH |
760 | key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type, |
761 | hcon->out); | |
988c5997 VCG |
762 | if (!key) |
763 | return 0; | |
764 | ||
4dab7864 JH |
765 | if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated) |
766 | return 0; | |
767 | ||
51a8efd7 | 768 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
988c5997 VCG |
769 | return 1; |
770 | ||
c9839a11 VCG |
771 | hci_le_start_enc(hcon, key->ediv, key->rand, key->val); |
772 | hcon->enc_key_size = key->enc_size; | |
988c5997 VCG |
773 | |
774 | return 1; | |
988c5997 | 775 | } |
f1560463 | 776 | |
da85e5e5 | 777 | static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) |
88ba43b6 AB |
778 | { |
779 | struct smp_cmd_security_req *rp = (void *) skb->data; | |
780 | struct smp_cmd_pairing cp; | |
f1cb9af5 | 781 | struct hci_conn *hcon = conn->hcon; |
8aab4757 | 782 | struct smp_chan *smp; |
88ba43b6 AB |
783 | |
784 | BT_DBG("conn %p", conn); | |
785 | ||
c46b98be JH |
786 | if (skb->len < sizeof(*rp)) |
787 | return SMP_UNSPECIFIED; | |
788 | ||
86ca9eac JH |
789 | if (!(conn->hcon->link_mode & HCI_LM_MASTER)) |
790 | return SMP_CMD_NOTSUPP; | |
791 | ||
2b64d153 | 792 | hcon->pending_sec_level = authreq_to_seclevel(rp->auth_req); |
feb45eb5 | 793 | |
4dab7864 | 794 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
988c5997 VCG |
795 | return 0; |
796 | ||
51a8efd7 | 797 | if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
da85e5e5 | 798 | return 0; |
f1cb9af5 | 799 | |
8aab4757 | 800 | smp = smp_chan_create(conn); |
d26a2345 | 801 | |
88ba43b6 | 802 | skb_pull(skb, sizeof(*rp)); |
88ba43b6 | 803 | |
da85e5e5 | 804 | memset(&cp, 0, sizeof(cp)); |
54790f73 | 805 | build_pairing_cmd(conn, &cp, NULL, rp->auth_req); |
88ba43b6 | 806 | |
1c1def09 VCG |
807 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
808 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 809 | |
88ba43b6 | 810 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
f1cb9af5 | 811 | |
da85e5e5 | 812 | return 0; |
88ba43b6 AB |
813 | } |
814 | ||
ad32a2f5 JH |
815 | bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level) |
816 | { | |
817 | if (sec_level == BT_SECURITY_LOW) | |
818 | return true; | |
819 | ||
820 | if (hcon->sec_level >= sec_level) | |
821 | return true; | |
822 | ||
823 | return false; | |
824 | } | |
825 | ||
cc110922 | 826 | int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) |
eb492e01 | 827 | { |
cc110922 | 828 | struct l2cap_conn *conn = hcon->l2cap_data; |
1c1def09 | 829 | struct smp_chan *smp = conn->smp_chan; |
2b64d153 | 830 | __u8 authreq; |
eb492e01 | 831 | |
3a0259bb VCG |
832 | BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); |
833 | ||
757aee0f | 834 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) |
2e65c9d2 AG |
835 | return 1; |
836 | ||
ad32a2f5 | 837 | if (smp_sufficient_security(hcon, sec_level)) |
eb492e01 | 838 | return 1; |
f1cb9af5 | 839 | |
988c5997 | 840 | if (hcon->link_mode & HCI_LM_MASTER) |
4dab7864 | 841 | if (smp_ltk_encrypt(conn, sec_level)) |
02bc7455 | 842 | goto done; |
d26a2345 | 843 | |
51a8efd7 | 844 | if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
d26a2345 VCG |
845 | return 0; |
846 | ||
8aab4757 | 847 | smp = smp_chan_create(conn); |
2b64d153 BG |
848 | if (!smp) |
849 | return 1; | |
850 | ||
851 | authreq = seclevel_to_authreq(sec_level); | |
d26a2345 | 852 | |
d26a2345 VCG |
853 | if (hcon->link_mode & HCI_LM_MASTER) { |
854 | struct smp_cmd_pairing cp; | |
f01ead31 | 855 | |
2b64d153 | 856 | build_pairing_cmd(conn, &cp, NULL, authreq); |
1c1def09 VCG |
857 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
858 | memcpy(&smp->preq[1], &cp, sizeof(cp)); | |
f01ead31 | 859 | |
eb492e01 AB |
860 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
861 | } else { | |
862 | struct smp_cmd_security_req cp; | |
2b64d153 | 863 | cp.auth_req = authreq; |
eb492e01 AB |
864 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
865 | } | |
866 | ||
02bc7455 | 867 | done: |
f1cb9af5 | 868 | hcon->pending_sec_level = sec_level; |
f1cb9af5 | 869 | |
eb492e01 AB |
870 | return 0; |
871 | } | |
872 | ||
7034b911 VCG |
873 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
874 | { | |
16b90839 | 875 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
1c1def09 | 876 | struct smp_chan *smp = conn->smp_chan; |
16b90839 | 877 | |
c46b98be JH |
878 | BT_DBG("conn %p", conn); |
879 | ||
880 | if (skb->len < sizeof(*rp)) | |
881 | return SMP_UNSPECIFIED; | |
882 | ||
16b90839 VCG |
883 | skb_pull(skb, sizeof(*rp)); |
884 | ||
1c1def09 | 885 | memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); |
16b90839 | 886 | |
7034b911 VCG |
887 | return 0; |
888 | } | |
889 | ||
890 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) | |
891 | { | |
16b90839 | 892 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
1c1def09 | 893 | struct smp_chan *smp = conn->smp_chan; |
c9839a11 VCG |
894 | struct hci_dev *hdev = conn->hcon->hdev; |
895 | struct hci_conn *hcon = conn->hcon; | |
896 | u8 authenticated; | |
16b90839 | 897 | |
c46b98be JH |
898 | BT_DBG("conn %p", conn); |
899 | ||
900 | if (skb->len < sizeof(*rp)) | |
901 | return SMP_UNSPECIFIED; | |
902 | ||
16b90839 | 903 | skb_pull(skb, sizeof(*rp)); |
7034b911 | 904 | |
c9839a11 | 905 | hci_dev_lock(hdev); |
ce39fb4e MH |
906 | authenticated = (hcon->sec_level == BT_SECURITY_HIGH); |
907 | hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, HCI_SMP_LTK, 1, | |
908 | authenticated, smp->tk, smp->enc_key_size, | |
04124681 | 909 | rp->ediv, rp->rand); |
7034b911 | 910 | smp_distribute_keys(conn, 1); |
c9839a11 | 911 | hci_dev_unlock(hdev); |
7034b911 VCG |
912 | |
913 | return 0; | |
914 | } | |
915 | ||
eb492e01 AB |
916 | int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) |
917 | { | |
7b9899db | 918 | struct hci_conn *hcon = conn->hcon; |
92381f5c | 919 | __u8 code, reason; |
eb492e01 AB |
920 | int err = 0; |
921 | ||
7b9899db MH |
922 | if (hcon->type != LE_LINK) { |
923 | kfree_skb(skb); | |
3432711f | 924 | return 0; |
7b9899db MH |
925 | } |
926 | ||
92381f5c MH |
927 | if (skb->len < 1) { |
928 | kfree_skb(skb); | |
929 | return -EILSEQ; | |
930 | } | |
931 | ||
06ae3314 | 932 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { |
2e65c9d2 AG |
933 | err = -ENOTSUPP; |
934 | reason = SMP_PAIRING_NOTSUPP; | |
935 | goto done; | |
936 | } | |
937 | ||
92381f5c | 938 | code = skb->data[0]; |
eb492e01 AB |
939 | skb_pull(skb, sizeof(code)); |
940 | ||
8cf9fa12 JH |
941 | /* |
942 | * The SMP context must be initialized for all other PDUs except | |
943 | * pairing and security requests. If we get any other PDU when | |
944 | * not initialized simply disconnect (done if this function | |
945 | * returns an error). | |
946 | */ | |
947 | if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ && | |
948 | !conn->smp_chan) { | |
949 | BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code); | |
950 | kfree_skb(skb); | |
951 | return -ENOTSUPP; | |
952 | } | |
953 | ||
eb492e01 AB |
954 | switch (code) { |
955 | case SMP_CMD_PAIRING_REQ: | |
da85e5e5 | 956 | reason = smp_cmd_pairing_req(conn, skb); |
eb492e01 AB |
957 | break; |
958 | ||
959 | case SMP_CMD_PAIRING_FAIL: | |
84794e11 | 960 | smp_failure(conn, 0); |
da85e5e5 VCG |
961 | reason = 0; |
962 | err = -EPERM; | |
eb492e01 AB |
963 | break; |
964 | ||
965 | case SMP_CMD_PAIRING_RSP: | |
da85e5e5 | 966 | reason = smp_cmd_pairing_rsp(conn, skb); |
88ba43b6 AB |
967 | break; |
968 | ||
969 | case SMP_CMD_SECURITY_REQ: | |
da85e5e5 | 970 | reason = smp_cmd_security_req(conn, skb); |
88ba43b6 AB |
971 | break; |
972 | ||
eb492e01 | 973 | case SMP_CMD_PAIRING_CONFIRM: |
da85e5e5 | 974 | reason = smp_cmd_pairing_confirm(conn, skb); |
88ba43b6 AB |
975 | break; |
976 | ||
eb492e01 | 977 | case SMP_CMD_PAIRING_RANDOM: |
da85e5e5 | 978 | reason = smp_cmd_pairing_random(conn, skb); |
88ba43b6 AB |
979 | break; |
980 | ||
eb492e01 | 981 | case SMP_CMD_ENCRYPT_INFO: |
7034b911 VCG |
982 | reason = smp_cmd_encrypt_info(conn, skb); |
983 | break; | |
984 | ||
eb492e01 | 985 | case SMP_CMD_MASTER_IDENT: |
7034b911 VCG |
986 | reason = smp_cmd_master_ident(conn, skb); |
987 | break; | |
988 | ||
eb492e01 AB |
989 | case SMP_CMD_IDENT_INFO: |
990 | case SMP_CMD_IDENT_ADDR_INFO: | |
991 | case SMP_CMD_SIGN_INFO: | |
7034b911 VCG |
992 | /* Just ignored */ |
993 | reason = 0; | |
994 | break; | |
995 | ||
eb492e01 AB |
996 | default: |
997 | BT_DBG("Unknown command code 0x%2.2x", code); | |
998 | ||
999 | reason = SMP_CMD_NOTSUPP; | |
eb492e01 | 1000 | err = -EOPNOTSUPP; |
3a0259bb | 1001 | goto done; |
eb492e01 AB |
1002 | } |
1003 | ||
3a0259bb VCG |
1004 | done: |
1005 | if (reason) | |
84794e11 | 1006 | smp_failure(conn, reason); |
3a0259bb | 1007 | |
eb492e01 AB |
1008 | kfree_skb(skb); |
1009 | return err; | |
1010 | } | |
7034b911 VCG |
1011 | |
1012 | int smp_distribute_keys(struct l2cap_conn *conn, __u8 force) | |
1013 | { | |
1014 | struct smp_cmd_pairing *req, *rsp; | |
1c1def09 | 1015 | struct smp_chan *smp = conn->smp_chan; |
7034b911 VCG |
1016 | __u8 *keydist; |
1017 | ||
1018 | BT_DBG("conn %p force %d", conn, force); | |
1019 | ||
51a8efd7 | 1020 | if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) |
d26a2345 VCG |
1021 | return 0; |
1022 | ||
1c1def09 | 1023 | rsp = (void *) &smp->prsp[1]; |
7034b911 VCG |
1024 | |
1025 | /* The responder sends its keys first */ | |
1026 | if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07)) | |
1027 | return 0; | |
1028 | ||
1c1def09 | 1029 | req = (void *) &smp->preq[1]; |
7034b911 VCG |
1030 | |
1031 | if (conn->hcon->out) { | |
1032 | keydist = &rsp->init_key_dist; | |
1033 | *keydist &= req->init_key_dist; | |
1034 | } else { | |
1035 | keydist = &rsp->resp_key_dist; | |
1036 | *keydist &= req->resp_key_dist; | |
1037 | } | |
1038 | ||
7034b911 VCG |
1039 | BT_DBG("keydist 0x%x", *keydist); |
1040 | ||
1041 | if (*keydist & SMP_DIST_ENC_KEY) { | |
1042 | struct smp_cmd_encrypt_info enc; | |
1043 | struct smp_cmd_master_ident ident; | |
c9839a11 VCG |
1044 | struct hci_conn *hcon = conn->hcon; |
1045 | u8 authenticated; | |
7034b911 VCG |
1046 | __le16 ediv; |
1047 | ||
1048 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); | |
1049 | get_random_bytes(&ediv, sizeof(ediv)); | |
1050 | get_random_bytes(ident.rand, sizeof(ident.rand)); | |
1051 | ||
1052 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); | |
1053 | ||
c9839a11 | 1054 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; |
ce39fb4e | 1055 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
04124681 GP |
1056 | HCI_SMP_LTK_SLAVE, 1, authenticated, |
1057 | enc.ltk, smp->enc_key_size, ediv, ident.rand); | |
16b90839 | 1058 | |
58115373 | 1059 | ident.ediv = ediv; |
7034b911 VCG |
1060 | |
1061 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); | |
1062 | ||
1063 | *keydist &= ~SMP_DIST_ENC_KEY; | |
1064 | } | |
1065 | ||
1066 | if (*keydist & SMP_DIST_ID_KEY) { | |
1067 | struct smp_cmd_ident_addr_info addrinfo; | |
1068 | struct smp_cmd_ident_info idinfo; | |
1069 | ||
1070 | /* Send a dummy key */ | |
1071 | get_random_bytes(idinfo.irk, sizeof(idinfo.irk)); | |
1072 | ||
1073 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); | |
1074 | ||
1075 | /* Just public address */ | |
1076 | memset(&addrinfo, 0, sizeof(addrinfo)); | |
2b36a562 | 1077 | bacpy(&addrinfo.bdaddr, &conn->hcon->src); |
7034b911 VCG |
1078 | |
1079 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), | |
f1560463 | 1080 | &addrinfo); |
7034b911 VCG |
1081 | |
1082 | *keydist &= ~SMP_DIST_ID_KEY; | |
1083 | } | |
1084 | ||
1085 | if (*keydist & SMP_DIST_SIGN) { | |
1086 | struct smp_cmd_sign_info sign; | |
1087 | ||
1088 | /* Send a dummy key */ | |
1089 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); | |
1090 | ||
1091 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); | |
1092 | ||
1093 | *keydist &= ~SMP_DIST_SIGN; | |
1094 | } | |
1095 | ||
d26a2345 | 1096 | if (conn->hcon->out || force) { |
51a8efd7 | 1097 | clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags); |
6c9d42a1 | 1098 | cancel_delayed_work_sync(&conn->security_timer); |
8aab4757 | 1099 | smp_chan_destroy(conn); |
d26a2345 VCG |
1100 | } |
1101 | ||
7034b911 VCG |
1102 | return 0; |
1103 | } |