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