aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/smp.c
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
commit8dea78da5cee153b8af9c07a2745f6c55057fe12 (patch)
treea8f4d49d63b1ecc92f2fddceba0655b2472c5bd9 /net/bluetooth/smp.c
parent406089d01562f1e2bf9f089fd7637009ebaad589 (diff)
Patched in Tegra support.
Diffstat (limited to 'net/bluetooth/smp.c')
-rw-r--r--net/bluetooth/smp.c675
1 files changed, 184 insertions, 491 deletions
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index 68a9587c969..c45dd737cdd 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -20,19 +20,15 @@
20 SOFTWARE IS DISCLAIMED. 20 SOFTWARE IS DISCLAIMED.
21*/ 21*/
22 22
23#include <linux/crypto.h>
24#include <linux/scatterlist.h>
25#include <crypto/b128ops.h>
26
27#include <net/bluetooth/bluetooth.h> 23#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h> 24#include <net/bluetooth/hci_core.h>
29#include <net/bluetooth/l2cap.h> 25#include <net/bluetooth/l2cap.h>
30#include <net/bluetooth/mgmt.h>
31#include <net/bluetooth/smp.h> 26#include <net/bluetooth/smp.h>
27#include <linux/crypto.h>
28#include <linux/scatterlist.h>
29#include <crypto/b128ops.h>
32 30
33#define SMP_TIMEOUT msecs_to_jiffies(30000) 31#define SMP_TIMEOUT 30000 /* 30 seconds */
34
35#define AUTH_REQ_MASK 0x07
36 32
37static inline void swap128(u8 src[16], u8 dst[16]) 33static inline void swap128(u8 src[16], u8 dst[16])
38{ 34{
@@ -167,7 +163,7 @@ static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code,
167 163
168 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 164 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
169 lh->len = cpu_to_le16(sizeof(code) + dlen); 165 lh->len = cpu_to_le16(sizeof(code) + dlen);
170 lh->cid = __constant_cpu_to_le16(L2CAP_CID_SMP); 166 lh->cid = cpu_to_le16(L2CAP_CID_SMP);
171 167
172 memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code)); 168 memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code));
173 169
@@ -185,28 +181,16 @@ static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
185 if (!skb) 181 if (!skb)
186 return; 182 return;
187 183
188 skb->priority = HCI_PRIO_MAX; 184 hci_send_acl(conn->hcon, skb, 0);
189 hci_send_acl(conn->hchan, skb, 0);
190
191 cancel_delayed_work_sync(&conn->security_timer);
192 schedule_delayed_work(&conn->security_timer, SMP_TIMEOUT);
193} 185}
194 186
195static __u8 authreq_to_seclevel(__u8 authreq) 187static __u8 seclevel_to_authreq(__u8 level)
196{ 188{
197 if (authreq & SMP_AUTH_MITM) 189 switch (level) {
198 return BT_SECURITY_HIGH;
199 else
200 return BT_SECURITY_MEDIUM;
201}
202
203static __u8 seclevel_to_authreq(__u8 sec_level)
204{
205 switch (sec_level) {
206 case BT_SECURITY_HIGH: 190 case BT_SECURITY_HIGH:
207 return SMP_AUTH_MITM | SMP_AUTH_BONDING; 191 /* Right now we don't support bonding */
208 case BT_SECURITY_MEDIUM: 192 return SMP_AUTH_MITM;
209 return SMP_AUTH_BONDING; 193
210 default: 194 default:
211 return SMP_AUTH_NONE; 195 return SMP_AUTH_NONE;
212 } 196 }
@@ -217,351 +201,39 @@ static void build_pairing_cmd(struct l2cap_conn *conn,
217 struct smp_cmd_pairing *rsp, 201 struct smp_cmd_pairing *rsp,
218 __u8 authreq) 202 __u8 authreq)
219{ 203{
220 u8 dist_keys = 0; 204 u8 dist_keys;
221 205
222 if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->dev_flags)) { 206 dist_keys = 0;
223 dist_keys = SMP_DIST_ENC_KEY; 207 if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->flags)) {
208 dist_keys = SMP_DIST_ENC_KEY | SMP_DIST_ID_KEY | SMP_DIST_SIGN;
224 authreq |= SMP_AUTH_BONDING; 209 authreq |= SMP_AUTH_BONDING;
225 } else {
226 authreq &= ~SMP_AUTH_BONDING;
227 } 210 }
228 211
229 if (rsp == NULL) { 212 if (rsp == NULL) {
230 req->io_capability = conn->hcon->io_capability; 213 req->io_capability = conn->hcon->io_capability;
231 req->oob_flag = SMP_OOB_NOT_PRESENT; 214 req->oob_flag = SMP_OOB_NOT_PRESENT;
232 req->max_key_size = SMP_MAX_ENC_KEY_SIZE; 215 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
233 req->init_key_dist = 0; 216 req->init_key_dist = dist_keys;
234 req->resp_key_dist = dist_keys; 217 req->resp_key_dist = dist_keys;
235 req->auth_req = (authreq & AUTH_REQ_MASK); 218 req->auth_req = authreq;
236 return; 219 return;
237 } 220 }
238 221
239 rsp->io_capability = conn->hcon->io_capability; 222 rsp->io_capability = conn->hcon->io_capability;
240 rsp->oob_flag = SMP_OOB_NOT_PRESENT; 223 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
241 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; 224 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
242 rsp->init_key_dist = 0; 225 rsp->init_key_dist = req->init_key_dist & dist_keys;
243 rsp->resp_key_dist = req->resp_key_dist & dist_keys; 226 rsp->resp_key_dist = req->resp_key_dist & dist_keys;
244 rsp->auth_req = (authreq & AUTH_REQ_MASK); 227 rsp->auth_req = authreq;
245} 228}
246 229
247static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) 230static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
248{ 231{
249 struct smp_chan *smp = conn->smp_chan;
250
251 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || 232 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
252 (max_key_size < SMP_MIN_ENC_KEY_SIZE)) 233 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
253 return SMP_ENC_KEY_SIZE; 234 return SMP_ENC_KEY_SIZE;
254 235
255 smp->enc_key_size = max_key_size; 236 conn->smp_key_size = max_key_size;
256
257 return 0;
258}
259
260static void smp_failure(struct l2cap_conn *conn, u8 reason, u8 send)
261{
262 struct hci_conn *hcon = conn->hcon;
263
264 if (send)
265 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
266 &reason);
267
268 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->hcon->flags);
269 mgmt_auth_failed(conn->hcon->hdev, conn->dst, hcon->type,
270 hcon->dst_type, HCI_ERROR_AUTH_FAILURE);
271
272 cancel_delayed_work_sync(&conn->security_timer);
273
274 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
275 smp_chan_destroy(conn);
276}
277
278#define JUST_WORKS 0x00
279#define JUST_CFM 0x01
280#define REQ_PASSKEY 0x02
281#define CFM_PASSKEY 0x03
282#define REQ_OOB 0x04
283#define OVERLAP 0xFF
284
285static const u8 gen_method[5][5] = {
286 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
287 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
288 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
289 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
290 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
291};
292
293static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
294 u8 local_io, u8 remote_io)
295{
296 struct hci_conn *hcon = conn->hcon;
297 struct smp_chan *smp = conn->smp_chan;
298 u8 method;
299 u32 passkey = 0;
300 int ret = 0;
301
302 /* Initialize key for JUST WORKS */
303 memset(smp->tk, 0, sizeof(smp->tk));
304 clear_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
305
306 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
307
308 /* If neither side wants MITM, use JUST WORKS */
309 /* If either side has unknown io_caps, use JUST WORKS */
310 /* Otherwise, look up method from the table */
311 if (!(auth & SMP_AUTH_MITM) ||
312 local_io > SMP_IO_KEYBOARD_DISPLAY ||
313 remote_io > SMP_IO_KEYBOARD_DISPLAY)
314 method = JUST_WORKS;
315 else
316 method = gen_method[remote_io][local_io];
317
318 /* If not bonding, don't ask user to confirm a Zero TK */
319 if (!(auth & SMP_AUTH_BONDING) && method == JUST_CFM)
320 method = JUST_WORKS;
321
322 /* If Just Works, Continue with Zero TK */
323 if (method == JUST_WORKS) {
324 set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
325 return 0;
326 }
327
328 /* Not Just Works/Confirm results in MITM Authentication */
329 if (method != JUST_CFM)
330 set_bit(SMP_FLAG_MITM_AUTH, &smp->smp_flags);
331
332 /* If both devices have Keyoard-Display I/O, the master
333 * Confirms and the slave Enters the passkey.
334 */
335 if (method == OVERLAP) {
336 if (hcon->link_mode & HCI_LM_MASTER)
337 method = CFM_PASSKEY;
338 else
339 method = REQ_PASSKEY;
340 }
341
342 /* Generate random passkey. Not valid until confirmed. */
343 if (method == CFM_PASSKEY) {
344 u8 key[16];
345
346 memset(key, 0, sizeof(key));
347 get_random_bytes(&passkey, sizeof(passkey));
348 passkey %= 1000000;
349 put_unaligned_le32(passkey, key);
350 swap128(key, smp->tk);
351 BT_DBG("PassKey: %d", passkey);
352 }
353
354 hci_dev_lock(hcon->hdev);
355
356 if (method == REQ_PASSKEY)
357 ret = mgmt_user_passkey_request(hcon->hdev, conn->dst,
358 hcon->type, hcon->dst_type);
359 else
360 ret = mgmt_user_confirm_request(hcon->hdev, conn->dst,
361 hcon->type, hcon->dst_type,
362 cpu_to_le32(passkey), 0);
363
364 hci_dev_unlock(hcon->hdev);
365
366 return ret;
367}
368
369static void confirm_work(struct work_struct *work)
370{
371 struct smp_chan *smp = container_of(work, struct smp_chan, confirm);
372 struct l2cap_conn *conn = smp->conn;
373 struct crypto_blkcipher *tfm;
374 struct smp_cmd_pairing_confirm cp;
375 int ret;
376 u8 res[16], reason;
377
378 BT_DBG("conn %p", conn);
379
380 tfm = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
381 if (IS_ERR(tfm)) {
382 reason = SMP_UNSPECIFIED;
383 goto error;
384 }
385
386 smp->tfm = tfm;
387
388 if (conn->hcon->out)
389 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, 0,
390 conn->src, conn->hcon->dst_type, conn->dst, res);
391 else
392 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,
393 conn->hcon->dst_type, conn->dst, 0, conn->src,
394 res);
395 if (ret) {
396 reason = SMP_UNSPECIFIED;
397 goto error;
398 }
399
400 clear_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
401
402 swap128(res, cp.confirm_val);
403 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
404
405 return;
406
407error:
408 smp_failure(conn, reason, 1);
409}
410
411static void random_work(struct work_struct *work)
412{
413 struct smp_chan *smp = container_of(work, struct smp_chan, random);
414 struct l2cap_conn *conn = smp->conn;
415 struct hci_conn *hcon = conn->hcon;
416 struct crypto_blkcipher *tfm = smp->tfm;
417 u8 reason, confirm[16], res[16], key[16];
418 int ret;
419
420 if (IS_ERR_OR_NULL(tfm)) {
421 reason = SMP_UNSPECIFIED;
422 goto error;
423 }
424
425 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
426
427 if (hcon->out)
428 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, 0,
429 conn->src, hcon->dst_type, conn->dst, res);
430 else
431 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,
432 hcon->dst_type, conn->dst, 0, conn->src, res);
433 if (ret) {
434 reason = SMP_UNSPECIFIED;
435 goto error;
436 }
437
438 swap128(res, confirm);
439
440 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
441 BT_ERR("Pairing failed (confirmation values mismatch)");
442 reason = SMP_CONFIRM_FAILED;
443 goto error;
444 }
445
446 if (hcon->out) {
447 u8 stk[16], rand[8];
448 __le16 ediv;
449
450 memset(rand, 0, sizeof(rand));
451 ediv = 0;
452
453 smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, key);
454 swap128(key, stk);
455
456 memset(stk + smp->enc_key_size, 0,
457 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
458
459 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) {
460 reason = SMP_UNSPECIFIED;
461 goto error;
462 }
463
464 hci_le_start_enc(hcon, ediv, rand, stk);
465 hcon->enc_key_size = smp->enc_key_size;
466 } else {
467 u8 stk[16], r[16], rand[8];
468 __le16 ediv;
469
470 memset(rand, 0, sizeof(rand));
471 ediv = 0;
472
473 swap128(smp->prnd, r);
474 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r);
475
476 smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, key);
477 swap128(key, stk);
478
479 memset(stk + smp->enc_key_size, 0,
480 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
481
482 hci_add_ltk(hcon->hdev, conn->dst, hcon->dst_type,
483 HCI_SMP_STK_SLAVE, 0, 0, stk, smp->enc_key_size,
484 ediv, rand);
485 }
486
487 return;
488
489error:
490 smp_failure(conn, reason, 1);
491}
492
493static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
494{
495 struct smp_chan *smp;
496
497 smp = kzalloc(sizeof(struct smp_chan), GFP_ATOMIC);
498 if (!smp)
499 return NULL;
500
501 INIT_WORK(&smp->confirm, confirm_work);
502 INIT_WORK(&smp->random, random_work);
503
504 smp->conn = conn;
505 conn->smp_chan = smp;
506 conn->hcon->smp_conn = conn;
507
508 hci_conn_hold(conn->hcon);
509
510 return smp;
511}
512
513void smp_chan_destroy(struct l2cap_conn *conn)
514{
515 struct smp_chan *smp = conn->smp_chan;
516
517 BUG_ON(!smp);
518
519 if (smp->tfm)
520 crypto_free_blkcipher(smp->tfm);
521
522 kfree(smp);
523 conn->smp_chan = NULL;
524 conn->hcon->smp_conn = NULL;
525 hci_conn_put(conn->hcon);
526}
527
528int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
529{
530 struct l2cap_conn *conn = hcon->smp_conn;
531 struct smp_chan *smp;
532 u32 value;
533 u8 key[16];
534
535 BT_DBG("");
536
537 if (!conn)
538 return -ENOTCONN;
539
540 smp = conn->smp_chan;
541
542 switch (mgmt_op) {
543 case MGMT_OP_USER_PASSKEY_REPLY:
544 value = le32_to_cpu(passkey);
545 memset(key, 0, sizeof(key));
546 BT_DBG("PassKey: %d", value);
547 put_unaligned_le32(value, key);
548 swap128(key, smp->tk);
549 /* Fall Through */
550 case MGMT_OP_USER_CONFIRM_REPLY:
551 set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
552 break;
553 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
554 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
555 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED, 1);
556 return 0;
557 default:
558 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED, 1);
559 return -EOPNOTSUPP;
560 }
561
562 /* If it is our turn to send Pairing Confirm, do so now */
563 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags))
564 queue_work(hcon->hdev->workqueue, &smp->confirm);
565 237
566 return 0; 238 return 0;
567} 239}
@@ -569,53 +241,37 @@ int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
569static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) 241static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
570{ 242{
571 struct smp_cmd_pairing rsp, *req = (void *) skb->data; 243 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
572 struct smp_chan *smp;
573 u8 key_size; 244 u8 key_size;
574 u8 auth = SMP_AUTH_NONE;
575 int ret;
576 245
577 BT_DBG("conn %p", conn); 246 BT_DBG("conn %p", conn);
578 247
579 if (conn->hcon->link_mode & HCI_LM_MASTER) 248 conn->preq[0] = SMP_CMD_PAIRING_REQ;
580 return SMP_CMD_NOTSUPP; 249 memcpy(&conn->preq[1], req, sizeof(*req));
581
582 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))
583 smp = smp_chan_create(conn);
584 else
585 smp = conn->smp_chan;
586
587 if (!smp)
588 return SMP_UNSPECIFIED;
589
590 smp->preq[0] = SMP_CMD_PAIRING_REQ;
591 memcpy(&smp->preq[1], req, sizeof(*req));
592 skb_pull(skb, sizeof(*req)); 250 skb_pull(skb, sizeof(*req));
593 251
594 /* We didn't start the pairing, so match remote */ 252 if (req->oob_flag)
595 if (req->auth_req & SMP_AUTH_BONDING) 253 return SMP_OOB_NOT_AVAIL;
596 auth = req->auth_req;
597 254
598 conn->hcon->pending_sec_level = authreq_to_seclevel(auth); 255 /* We didn't start the pairing, so no requirements */
599 256 build_pairing_cmd(conn, req, &rsp, SMP_AUTH_NONE);
600 build_pairing_cmd(conn, req, &rsp, auth);
601 257
602 key_size = min(req->max_key_size, rsp.max_key_size); 258 key_size = min(req->max_key_size, rsp.max_key_size);
603 if (check_enc_key_size(conn, key_size)) 259 if (check_enc_key_size(conn, key_size))
604 return SMP_ENC_KEY_SIZE; 260 return SMP_ENC_KEY_SIZE;
605 261
606 ret = smp_rand(smp->prnd); 262 /* Just works */
607 if (ret) 263 memset(conn->tk, 0, sizeof(conn->tk));
608 return SMP_UNSPECIFIED;
609 264
610 smp->prsp[0] = SMP_CMD_PAIRING_RSP; 265 conn->prsp[0] = SMP_CMD_PAIRING_RSP;
611 memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); 266 memcpy(&conn->prsp[1], &rsp, sizeof(rsp));
612 267
613 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); 268 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
614 269
615 /* Request setup of TK */ 270 mod_timer(&conn->security_timer, jiffies +
616 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); 271 msecs_to_jiffies(SMP_TIMEOUT));
617 if (ret) 272
618 return SMP_UNSPECIFIED; 273 mod_timer(&conn->security_timer, jiffies +
274 msecs_to_jiffies(SMP_TIMEOUT));
619 275
620 return 0; 276 return 0;
621} 277}
@@ -623,149 +279,187 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
623static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) 279static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
624{ 280{
625 struct smp_cmd_pairing *req, *rsp = (void *) skb->data; 281 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
626 struct smp_chan *smp = conn->smp_chan; 282 struct smp_cmd_pairing_confirm cp;
627 struct hci_dev *hdev = conn->hcon->hdev; 283 struct crypto_blkcipher *tfm = conn->hcon->hdev->tfm;
628 u8 key_size, auth = SMP_AUTH_NONE;
629 int ret; 284 int ret;
285 u8 res[16], key_size;
630 286
631 BT_DBG("conn %p", conn); 287 BT_DBG("conn %p", conn);
632 288
633 if (!(conn->hcon->link_mode & HCI_LM_MASTER))
634 return SMP_CMD_NOTSUPP;
635
636 skb_pull(skb, sizeof(*rsp)); 289 skb_pull(skb, sizeof(*rsp));
637 290
638 req = (void *) &smp->preq[1]; 291 req = (void *) &conn->preq[1];
639 292
640 key_size = min(req->max_key_size, rsp->max_key_size); 293 key_size = min(req->max_key_size, rsp->max_key_size);
641 if (check_enc_key_size(conn, key_size)) 294 if (check_enc_key_size(conn, key_size))
642 return SMP_ENC_KEY_SIZE; 295 return SMP_ENC_KEY_SIZE;
643 296
644 ret = smp_rand(smp->prnd); 297 if (rsp->oob_flag)
645 if (ret) 298 return SMP_OOB_NOT_AVAIL;
646 return SMP_UNSPECIFIED;
647
648 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
649 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
650 299
651 if ((req->auth_req & SMP_AUTH_BONDING) && 300 /* Just works */
652 (rsp->auth_req & SMP_AUTH_BONDING)) 301 memset(conn->tk, 0, sizeof(conn->tk));
653 auth = SMP_AUTH_BONDING;
654 302
655 auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM; 303 conn->prsp[0] = SMP_CMD_PAIRING_RSP;
304 memcpy(&conn->prsp[1], rsp, sizeof(*rsp));
656 305
657 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); 306 ret = smp_rand(conn->prnd);
658 if (ret) 307 if (ret)
659 return SMP_UNSPECIFIED; 308 return SMP_UNSPECIFIED;
660 309
661 set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); 310 ret = smp_c1(tfm, conn->tk, conn->prnd, conn->preq, conn->prsp, 0,
311 conn->src, conn->hcon->dst_type, conn->dst, res);
312 if (ret)
313 return SMP_UNSPECIFIED;
662 314
663 /* Can't compose response until we have been confirmed */ 315 swap128(res, cp.confirm_val);
664 if (!test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags))
665 return 0;
666 316
667 queue_work(hdev->workqueue, &smp->confirm); 317 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
668 318
669 return 0; 319 return 0;
670} 320}
671 321
672static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) 322static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
673{ 323{
674 struct smp_chan *smp = conn->smp_chan; 324 struct crypto_blkcipher *tfm = conn->hcon->hdev->tfm;
675 struct hci_dev *hdev = conn->hcon->hdev;
676 325
677 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); 326 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
678 327
679 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); 328 memcpy(conn->pcnf, skb->data, sizeof(conn->pcnf));
680 skb_pull(skb, sizeof(smp->pcnf)); 329 skb_pull(skb, sizeof(conn->pcnf));
681 330
682 if (conn->hcon->out) { 331 if (conn->hcon->out) {
683 u8 random[16]; 332 u8 random[16];
684 333
685 swap128(smp->prnd, random); 334 swap128(conn->prnd, random);
686 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random), 335 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random),
687 random); 336 random);
688 } else if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) {
689 queue_work(hdev->workqueue, &smp->confirm);
690 } else { 337 } else {
691 set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); 338 struct smp_cmd_pairing_confirm cp;
339 int ret;
340 u8 res[16];
341
342 ret = smp_rand(conn->prnd);
343 if (ret)
344 return SMP_UNSPECIFIED;
345
346 ret = smp_c1(tfm, conn->tk, conn->prnd, conn->preq, conn->prsp,
347 conn->hcon->dst_type, conn->dst,
348 0, conn->src, res);
349 if (ret)
350 return SMP_CONFIRM_FAILED;
351
352 swap128(res, cp.confirm_val);
353
354 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
692 } 355 }
693 356
357 mod_timer(&conn->security_timer, jiffies +
358 msecs_to_jiffies(SMP_TIMEOUT));
359
694 return 0; 360 return 0;
695} 361}
696 362
697static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) 363static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
698{ 364{
699 struct smp_chan *smp = conn->smp_chan; 365 struct hci_conn *hcon = conn->hcon;
700 struct hci_dev *hdev = conn->hcon->hdev; 366 struct crypto_blkcipher *tfm = hcon->hdev->tfm;
367 int ret;
368 u8 key[16], res[16], random[16], confirm[16];
701 369
702 BT_DBG("conn %p", conn); 370 swap128(skb->data, random);
371 skb_pull(skb, sizeof(random));
703 372
704 swap128(skb->data, smp->rrnd); 373 if (conn->hcon->out)
705 skb_pull(skb, sizeof(smp->rrnd)); 374 ret = smp_c1(tfm, conn->tk, random, conn->preq, conn->prsp, 0,
375 conn->src, conn->hcon->dst_type, conn->dst,
376 res);
377 else
378 ret = smp_c1(tfm, conn->tk, random, conn->preq, conn->prsp,
379 conn->hcon->dst_type, conn->dst, 0, conn->src,
380 res);
381 if (ret)
382 return SMP_UNSPECIFIED;
706 383
707 queue_work(hdev->workqueue, &smp->random); 384 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
708 385
709 return 0; 386 swap128(res, confirm);
710}
711 387
712static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) 388 if (memcmp(conn->pcnf, confirm, sizeof(conn->pcnf)) != 0) {
713{ 389 BT_ERR("Pairing failed (confirmation values mismatch)");
714 struct smp_ltk *key; 390 return SMP_CONFIRM_FAILED;
715 struct hci_conn *hcon = conn->hcon; 391 }
716 392
717 key = hci_find_ltk_by_addr(hcon->hdev, conn->dst, hcon->dst_type); 393 if (conn->hcon->out) {
718 if (!key) 394 u8 stk[16], rand[8];
719 return 0; 395 __le16 ediv;
720 396
721 if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated) 397 memset(rand, 0, sizeof(rand));
722 return 0; 398 ediv = 0;
723 399
724 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) 400 smp_s1(tfm, conn->tk, random, conn->prnd, key);
725 return 1; 401 swap128(key, stk);
726 402
727 hci_le_start_enc(hcon, key->ediv, key->rand, key->val); 403 memset(stk + conn->smp_key_size, 0,
728 hcon->enc_key_size = key->enc_size; 404 SMP_MAX_ENC_KEY_SIZE - conn->smp_key_size);
729 405
730 return 1; 406 hci_le_start_enc(hcon, ediv, rand, stk);
407 hcon->enc_key_size = conn->smp_key_size;
408 } else {
409 u8 stk[16], r[16], rand[8];
410 __le16 ediv;
411
412 memset(rand, 0, sizeof(rand));
413 ediv = 0;
731 414
415 swap128(conn->prnd, r);
416 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r);
417
418 smp_s1(tfm, conn->tk, conn->prnd, random, key);
419 swap128(key, stk);
420
421 memset(stk + conn->smp_key_size, 0,
422 SMP_MAX_ENC_KEY_SIZE - conn->smp_key_size);
423
424 hci_add_ltk(conn->hcon->hdev, 0, conn->dst, conn->smp_key_size,
425 ediv, rand, stk);
426 }
427
428 return 0;
732} 429}
430
733static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) 431static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
734{ 432{
735 struct smp_cmd_security_req *rp = (void *) skb->data; 433 struct smp_cmd_security_req *rp = (void *) skb->data;
736 struct smp_cmd_pairing cp; 434 struct smp_cmd_pairing cp;
737 struct hci_conn *hcon = conn->hcon; 435 struct hci_conn *hcon = conn->hcon;
738 struct smp_chan *smp;
739 436
740 BT_DBG("conn %p", conn); 437 BT_DBG("conn %p", conn);
741 438
742 hcon->pending_sec_level = authreq_to_seclevel(rp->auth_req); 439 if (test_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend))
743
744 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
745 return 0; 440 return 0;
746 441
747 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
748 return 0;
749
750 smp = smp_chan_create(conn);
751
752 skb_pull(skb, sizeof(*rp)); 442 skb_pull(skb, sizeof(*rp));
753 443
754 memset(&cp, 0, sizeof(cp)); 444 memset(&cp, 0, sizeof(cp));
755 build_pairing_cmd(conn, &cp, NULL, rp->auth_req); 445 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
756 446
757 smp->preq[0] = SMP_CMD_PAIRING_REQ; 447 conn->preq[0] = SMP_CMD_PAIRING_REQ;
758 memcpy(&smp->preq[1], &cp, sizeof(cp)); 448 memcpy(&conn->preq[1], &cp, sizeof(cp));
759 449
760 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); 450 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
761 451
452 mod_timer(&conn->security_timer, jiffies +
453 msecs_to_jiffies(SMP_TIMEOUT));
454
455 set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend);
456
762 return 0; 457 return 0;
763} 458}
764 459
765int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) 460int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level)
766{ 461{
767 struct l2cap_conn *conn = hcon->l2cap_data; 462 struct hci_conn *hcon = conn->hcon;
768 struct smp_chan *smp = conn->smp_chan;
769 __u8 authreq; 463 __u8 authreq;
770 464
771 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); 465 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
@@ -773,31 +467,42 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
773 if (!lmp_host_le_capable(hcon->hdev)) 467 if (!lmp_host_le_capable(hcon->hdev))
774 return 1; 468 return 1;
775 469
776 if (sec_level == BT_SECURITY_LOW) 470 if (IS_ERR(hcon->hdev->tfm))
777 return 1;
778
779 if (hcon->sec_level >= sec_level)
780 return 1; 471 return 1;
781 472
782 if (hcon->link_mode & HCI_LM_MASTER) 473 if (test_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend))
783 if (smp_ltk_encrypt(conn, sec_level))
784 goto done;
785
786 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
787 return 0; 474 return 0;
788 475
789 smp = smp_chan_create(conn); 476 if (sec_level == BT_SECURITY_LOW)
790 if (!smp) 477 return 1;
478
479 if (hcon->sec_level >= sec_level)
791 return 1; 480 return 1;
792 481
793 authreq = seclevel_to_authreq(sec_level); 482 authreq = seclevel_to_authreq(sec_level);
794 483
795 if (hcon->link_mode & HCI_LM_MASTER) { 484 if (hcon->link_mode & HCI_LM_MASTER) {
796 struct smp_cmd_pairing cp; 485 struct smp_cmd_pairing cp;
486 struct link_key *key;
487
488 key = hci_find_link_key_type(hcon->hdev, conn->dst,
489 HCI_LK_SMP_LTK);
490 if (key) {
491 struct key_master_id *master = (void *) key->data;
492
493 hci_le_start_enc(hcon, master->ediv, master->rand,
494 key->val);
495 hcon->enc_key_size = key->pin_len;
496
497 goto done;
498 }
797 499
798 build_pairing_cmd(conn, &cp, NULL, authreq); 500 build_pairing_cmd(conn, &cp, NULL, authreq);
799 smp->preq[0] = SMP_CMD_PAIRING_REQ; 501 conn->preq[0] = SMP_CMD_PAIRING_REQ;
800 memcpy(&smp->preq[1], &cp, sizeof(cp)); 502 memcpy(&conn->preq[1], &cp, sizeof(cp));
503
504 mod_timer(&conn->security_timer, jiffies +
505 msecs_to_jiffies(SMP_TIMEOUT));
801 506
802 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); 507 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
803 } else { 508 } else {
@@ -808,6 +513,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
808 513
809done: 514done:
810 hcon->pending_sec_level = sec_level; 515 hcon->pending_sec_level = sec_level;
516 set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend);
811 517
812 return 0; 518 return 0;
813} 519}
@@ -815,11 +521,10 @@ done:
815static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) 521static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
816{ 522{
817 struct smp_cmd_encrypt_info *rp = (void *) skb->data; 523 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
818 struct smp_chan *smp = conn->smp_chan;
819 524
820 skb_pull(skb, sizeof(*rp)); 525 skb_pull(skb, sizeof(*rp));
821 526
822 memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); 527 memcpy(conn->tk, rp->ltk, sizeof(conn->tk));
823 528
824 return 0; 529 return 0;
825} 530}
@@ -827,20 +532,13 @@ static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
827static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) 532static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
828{ 533{
829 struct smp_cmd_master_ident *rp = (void *) skb->data; 534 struct smp_cmd_master_ident *rp = (void *) skb->data;
830 struct smp_chan *smp = conn->smp_chan;
831 struct hci_dev *hdev = conn->hcon->hdev;
832 struct hci_conn *hcon = conn->hcon;
833 u8 authenticated;
834 535
835 skb_pull(skb, sizeof(*rp)); 536 skb_pull(skb, sizeof(*rp));
836 537
837 hci_dev_lock(hdev); 538 hci_add_ltk(conn->hcon->hdev, 1, conn->src, conn->smp_key_size,
838 authenticated = (conn->hcon->sec_level == BT_SECURITY_HIGH); 539 rp->ediv, rp->rand, conn->tk);
839 hci_add_ltk(conn->hcon->hdev, conn->dst, hcon->dst_type, 540
840 HCI_SMP_LTK, 1, authenticated, smp->tk, smp->enc_key_size,
841 rp->ediv, rp->rand);
842 smp_distribute_keys(conn, 1); 541 smp_distribute_keys(conn, 1);
843 hci_dev_unlock(hdev);
844 542
845 return 0; 543 return 0;
846} 544}
@@ -857,6 +555,12 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
857 goto done; 555 goto done;
858 } 556 }
859 557
558 if (IS_ERR(conn->hcon->hdev->tfm)) {
559 err = PTR_ERR(conn->hcon->hdev->tfm);
560 reason = SMP_PAIRING_NOTSUPP;
561 goto done;
562 }
563
860 skb_pull(skb, sizeof(code)); 564 skb_pull(skb, sizeof(code));
861 565
862 switch (code) { 566 switch (code) {
@@ -865,7 +569,6 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
865 break; 569 break;
866 570
867 case SMP_CMD_PAIRING_FAIL: 571 case SMP_CMD_PAIRING_FAIL:
868 smp_failure(conn, skb->data[0], 0);
869 reason = 0; 572 reason = 0;
870 err = -EPERM; 573 err = -EPERM;
871 break; 574 break;
@@ -911,7 +614,8 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
911 614
912done: 615done:
913 if (reason) 616 if (reason)
914 smp_failure(conn, reason, 1); 617 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
618 &reason);
915 619
916 kfree_skb(skb); 620 kfree_skb(skb);
917 return err; 621 return err;
@@ -920,21 +624,20 @@ done:
920int smp_distribute_keys(struct l2cap_conn *conn, __u8 force) 624int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
921{ 625{
922 struct smp_cmd_pairing *req, *rsp; 626 struct smp_cmd_pairing *req, *rsp;
923 struct smp_chan *smp = conn->smp_chan;
924 __u8 *keydist; 627 __u8 *keydist;
925 628
926 BT_DBG("conn %p force %d", conn, force); 629 BT_DBG("conn %p force %d", conn, force);
927 630
928 if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) 631 if (IS_ERR(conn->hcon->hdev->tfm))
929 return 0; 632 return PTR_ERR(conn->hcon->hdev->tfm);
930 633
931 rsp = (void *) &smp->prsp[1]; 634 rsp = (void *) &conn->prsp[1];
932 635
933 /* The responder sends its keys first */ 636 /* The responder sends its keys first */
934 if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07)) 637 if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07))
935 return 0; 638 return 0;
936 639
937 req = (void *) &smp->preq[1]; 640 req = (void *) &conn->preq[1];
938 641
939 if (conn->hcon->out) { 642 if (conn->hcon->out) {
940 keydist = &rsp->init_key_dist; 643 keydist = &rsp->init_key_dist;
@@ -950,8 +653,6 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
950 if (*keydist & SMP_DIST_ENC_KEY) { 653 if (*keydist & SMP_DIST_ENC_KEY) {
951 struct smp_cmd_encrypt_info enc; 654 struct smp_cmd_encrypt_info enc;
952 struct smp_cmd_master_ident ident; 655 struct smp_cmd_master_ident ident;
953 struct hci_conn *hcon = conn->hcon;
954 u8 authenticated;
955 __le16 ediv; 656 __le16 ediv;
956 657
957 get_random_bytes(enc.ltk, sizeof(enc.ltk)); 658 get_random_bytes(enc.ltk, sizeof(enc.ltk));
@@ -960,12 +661,10 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
960 661
961 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); 662 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
962 663
963 authenticated = hcon->sec_level == BT_SECURITY_HIGH; 664 hci_add_ltk(conn->hcon->hdev, 1, conn->dst, conn->smp_key_size,
964 hci_add_ltk(conn->hcon->hdev, conn->dst, hcon->dst_type, 665 ediv, ident.rand, enc.ltk);
965 HCI_SMP_LTK_SLAVE, 1, authenticated,
966 enc.ltk, smp->enc_key_size, ediv, ident.rand);
967 666
968 ident.ediv = ediv; 667 ident.ediv = cpu_to_le16(ediv);
969 668
970 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); 669 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
971 670
@@ -1002,11 +701,5 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
1002 *keydist &= ~SMP_DIST_SIGN; 701 *keydist &= ~SMP_DIST_SIGN;
1003 } 702 }
1004 703
1005 if (conn->hcon->out || force) {
1006 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);
1007 cancel_delayed_work_sync(&conn->security_timer);
1008 smp_chan_destroy(conn);
1009 }
1010
1011 return 0; 704 return 0;
1012} 705}