Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Bluetooth: Move SMP fields to a separate structure

The objective is to make the core to have as little as possible
information about SMP procedures and logic. Now, all the SMP
specific information is hidden from the core.

Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@openbossa.org>
Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>

authored by

Vinicius Costa Gomes and committed by
Gustavo F. Padovan
1c1def09 142c69c6

+65 -71
-2
include/net/bluetooth/hci_core.h
··· 195 195 196 196 __u16 init_last_cmd; 197 197 198 - struct crypto_blkcipher *tfm; 199 - 200 198 struct inquiry_cache inq_cache; 201 199 struct hci_conn_hash conn_hash; 202 200 struct list_head blacklist;
+1 -7
include/net/bluetooth/l2cap.h
··· 409 409 410 410 __u8 disc_reason; 411 411 412 - __u8 preq[7]; /* SMP Pairing Request */ 413 - __u8 prsp[7]; /* SMP Pairing Response */ 414 - __u8 prnd[16]; /* SMP Pairing Random */ 415 - __u8 pcnf[16]; /* SMP Pairing Confirm */ 416 - __u8 tk[16]; /* SMP Temporary Key */ 417 - __u8 smp_key_size; 418 - 419 412 struct timer_list security_timer; 413 + struct smp_chan *smp_chan; 420 414 421 415 struct list_head chan_l; 422 416 rwlock_t chan_lock;
+10
include/net/bluetooth/smp.h
··· 115 115 #define SMP_MIN_ENC_KEY_SIZE 7 116 116 #define SMP_MAX_ENC_KEY_SIZE 16 117 117 118 + struct smp_chan { 119 + u8 preq[7]; /* SMP Pairing Request */ 120 + u8 prsp[7]; /* SMP Pairing Response */ 121 + u8 prnd[16]; /* SMP Pairing Random */ 122 + u8 pcnf[16]; /* SMP Pairing Confirm */ 123 + u8 tk[16]; /* SMP Temporary Key */ 124 + u8 smp_key_size; 125 + struct crypto_blkcipher *tfm; 126 + }; 127 + 118 128 /* SMP Commands */ 119 129 int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level); 120 130 int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb);
-8
net/bluetooth/hci_core.c
··· 1523 1523 if (!hdev->workqueue) 1524 1524 goto nomem; 1525 1525 1526 - hdev->tfm = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); 1527 - if (IS_ERR(hdev->tfm)) 1528 - BT_INFO("Failed to load transform for ecb(aes): %ld", 1529 - PTR_ERR(hdev->tfm)); 1530 - 1531 1526 hci_register_sysfs(hdev); 1532 1527 1533 1528 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, ··· 1570 1575 if (!test_bit(HCI_INIT, &hdev->flags) && 1571 1576 !test_bit(HCI_SETUP, &hdev->flags)) 1572 1577 mgmt_index_removed(hdev->id); 1573 - 1574 - if (!IS_ERR(hdev->tfm)) 1575 - crypto_free_blkcipher(hdev->tfm); 1576 1578 1577 1579 hci_notify(hdev, HCI_DEV_UNREG); 1578 1580
+54 -54
net/bluetooth/smp.c
··· 232 232 233 233 static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) 234 234 { 235 + struct smp_chan *smp = conn->smp_chan; 236 + 235 237 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || 236 238 (max_key_size < SMP_MIN_ENC_KEY_SIZE)) 237 239 return SMP_ENC_KEY_SIZE; 238 240 239 - conn->smp_key_size = max_key_size; 241 + smp->smp_key_size = max_key_size; 240 242 241 243 return 0; 242 244 } ··· 246 244 static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) 247 245 { 248 246 struct smp_cmd_pairing rsp, *req = (void *) skb->data; 247 + struct smp_chan *smp = conn->smp_chan; 249 248 u8 key_size; 250 249 251 250 BT_DBG("conn %p", conn); ··· 254 251 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend)) 255 252 hci_conn_hold(conn->hcon); 256 253 257 - conn->preq[0] = SMP_CMD_PAIRING_REQ; 258 - memcpy(&conn->preq[1], req, sizeof(*req)); 254 + smp->preq[0] = SMP_CMD_PAIRING_REQ; 255 + memcpy(&smp->preq[1], req, sizeof(*req)); 259 256 skb_pull(skb, sizeof(*req)); 260 257 261 258 if (req->oob_flag) ··· 269 266 return SMP_ENC_KEY_SIZE; 270 267 271 268 /* Just works */ 272 - memset(conn->tk, 0, sizeof(conn->tk)); 269 + memset(smp->tk, 0, sizeof(smp->tk)); 273 270 274 - conn->prsp[0] = SMP_CMD_PAIRING_RSP; 275 - memcpy(&conn->prsp[1], &rsp, sizeof(rsp)); 271 + smp->prsp[0] = SMP_CMD_PAIRING_RSP; 272 + memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); 276 273 277 274 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); 278 275 ··· 283 280 { 284 281 struct smp_cmd_pairing *req, *rsp = (void *) skb->data; 285 282 struct smp_cmd_pairing_confirm cp; 286 - struct crypto_blkcipher *tfm = conn->hcon->hdev->tfm; 283 + struct smp_chan *smp = conn->smp_chan; 284 + struct crypto_blkcipher *tfm = smp->tfm; 285 + 287 286 int ret; 288 287 u8 res[16], key_size; 289 288 ··· 293 288 294 289 skb_pull(skb, sizeof(*rsp)); 295 290 296 - req = (void *) &conn->preq[1]; 291 + req = (void *) &smp->preq[1]; 297 292 298 293 key_size = min(req->max_key_size, rsp->max_key_size); 299 294 if (check_enc_key_size(conn, key_size)) ··· 303 298 return SMP_OOB_NOT_AVAIL; 304 299 305 300 /* Just works */ 306 - memset(conn->tk, 0, sizeof(conn->tk)); 301 + memset(smp->tk, 0, sizeof(smp->tk)); 307 302 308 - conn->prsp[0] = SMP_CMD_PAIRING_RSP; 309 - memcpy(&conn->prsp[1], rsp, sizeof(*rsp)); 303 + smp->prsp[0] = SMP_CMD_PAIRING_RSP; 304 + memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); 310 305 311 - ret = smp_rand(conn->prnd); 306 + ret = smp_rand(smp->prnd); 312 307 if (ret) 313 308 return SMP_UNSPECIFIED; 314 309 315 - ret = smp_c1(tfm, conn->tk, conn->prnd, conn->preq, conn->prsp, 0, 310 + ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, 0, 316 311 conn->src, conn->hcon->dst_type, conn->dst, res); 317 312 if (ret) 318 313 return SMP_UNSPECIFIED; ··· 326 321 327 322 static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) 328 323 { 329 - struct crypto_blkcipher *tfm = conn->hcon->hdev->tfm; 324 + struct smp_chan *smp = conn->smp_chan; 325 + struct crypto_blkcipher *tfm = smp->tfm; 330 326 331 327 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); 332 328 333 - memcpy(conn->pcnf, skb->data, sizeof(conn->pcnf)); 334 - skb_pull(skb, sizeof(conn->pcnf)); 329 + memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); 330 + skb_pull(skb, sizeof(smp->pcnf)); 335 331 336 332 if (conn->hcon->out) { 337 333 u8 random[16]; 338 334 339 - swap128(conn->prnd, random); 335 + swap128(smp->prnd, random); 340 336 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random), 341 337 random); 342 338 } else { ··· 345 339 int ret; 346 340 u8 res[16]; 347 341 348 - ret = smp_rand(conn->prnd); 342 + ret = smp_rand(smp->prnd); 349 343 if (ret) 350 344 return SMP_UNSPECIFIED; 351 345 352 - ret = smp_c1(tfm, conn->tk, conn->prnd, conn->preq, conn->prsp, 346 + ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, 353 347 conn->hcon->dst_type, conn->dst, 354 348 0, conn->src, res); 355 349 if (ret) ··· 366 360 static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) 367 361 { 368 362 struct hci_conn *hcon = conn->hcon; 369 - struct crypto_blkcipher *tfm = hcon->hdev->tfm; 363 + struct smp_chan *smp = conn->smp_chan; 364 + struct crypto_blkcipher *tfm = smp->tfm; 370 365 int ret; 371 366 u8 key[16], res[16], random[16], confirm[16]; 372 367 ··· 375 368 skb_pull(skb, sizeof(random)); 376 369 377 370 if (conn->hcon->out) 378 - ret = smp_c1(tfm, conn->tk, random, conn->preq, conn->prsp, 0, 371 + ret = smp_c1(tfm, smp->tk, random, smp->preq, smp->prsp, 0, 379 372 conn->src, conn->hcon->dst_type, conn->dst, 380 373 res); 381 374 else 382 - ret = smp_c1(tfm, conn->tk, random, conn->preq, conn->prsp, 375 + ret = smp_c1(tfm, smp->tk, random, smp->preq, smp->prsp, 383 376 conn->hcon->dst_type, conn->dst, 0, conn->src, 384 377 res); 385 378 if (ret) ··· 389 382 390 383 swap128(res, confirm); 391 384 392 - if (memcmp(conn->pcnf, confirm, sizeof(conn->pcnf)) != 0) { 385 + if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { 393 386 BT_ERR("Pairing failed (confirmation values mismatch)"); 394 387 return SMP_CONFIRM_FAILED; 395 388 } ··· 401 394 memset(rand, 0, sizeof(rand)); 402 395 ediv = 0; 403 396 404 - smp_s1(tfm, conn->tk, random, conn->prnd, key); 397 + smp_s1(tfm, smp->tk, random, smp->prnd, key); 405 398 swap128(key, stk); 406 399 407 - memset(stk + conn->smp_key_size, 0, 408 - SMP_MAX_ENC_KEY_SIZE - conn->smp_key_size); 400 + memset(stk + smp->smp_key_size, 0, 401 + SMP_MAX_ENC_KEY_SIZE - smp->smp_key_size); 409 402 410 403 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend)) 411 404 return SMP_UNSPECIFIED; 412 405 413 406 hci_le_start_enc(hcon, ediv, rand, stk); 414 - hcon->enc_key_size = conn->smp_key_size; 407 + hcon->enc_key_size = smp->smp_key_size; 415 408 } else { 416 409 u8 stk[16], r[16], rand[8]; 417 410 __le16 ediv; ··· 419 412 memset(rand, 0, sizeof(rand)); 420 413 ediv = 0; 421 414 422 - swap128(conn->prnd, r); 415 + swap128(smp->prnd, r); 423 416 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r); 424 417 425 - smp_s1(tfm, conn->tk, conn->prnd, random, key); 418 + smp_s1(tfm, smp->tk, smp->prnd, random, key); 426 419 swap128(key, stk); 427 420 428 - memset(stk + conn->smp_key_size, 0, 429 - SMP_MAX_ENC_KEY_SIZE - conn->smp_key_size); 421 + memset(stk + smp->smp_key_size, 0, 422 + SMP_MAX_ENC_KEY_SIZE - smp->smp_key_size); 430 423 431 - hci_add_ltk(conn->hcon->hdev, 0, conn->dst, conn->smp_key_size, 424 + hci_add_ltk(conn->hcon->hdev, 0, conn->dst, smp->smp_key_size, 432 425 ediv, rand, stk); 433 426 } 434 427 ··· 440 433 struct smp_cmd_security_req *rp = (void *) skb->data; 441 434 struct smp_cmd_pairing cp; 442 435 struct hci_conn *hcon = conn->hcon; 436 + struct smp_chan *smp = conn->smp_chan; 443 437 444 438 BT_DBG("conn %p", conn); 445 439 ··· 454 446 memset(&cp, 0, sizeof(cp)); 455 447 build_pairing_cmd(conn, &cp, NULL, rp->auth_req); 456 448 457 - conn->preq[0] = SMP_CMD_PAIRING_REQ; 458 - memcpy(&conn->preq[1], &cp, sizeof(cp)); 449 + smp->preq[0] = SMP_CMD_PAIRING_REQ; 450 + memcpy(&smp->preq[1], &cp, sizeof(cp)); 459 451 460 452 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); 461 453 ··· 465 457 int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level) 466 458 { 467 459 struct hci_conn *hcon = conn->hcon; 460 + struct smp_chan *smp = conn->smp_chan; 468 461 __u8 authreq; 469 462 470 463 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); 471 464 472 465 if (!lmp_host_le_capable(hcon->hdev)) 473 - return 1; 474 - 475 - if (IS_ERR(hcon->hdev->tfm)) 476 466 return 1; 477 467 478 468 if (sec_level == BT_SECURITY_LOW) ··· 511 505 struct smp_cmd_pairing cp; 512 506 513 507 build_pairing_cmd(conn, &cp, NULL, authreq); 514 - conn->preq[0] = SMP_CMD_PAIRING_REQ; 515 - memcpy(&conn->preq[1], &cp, sizeof(cp)); 508 + smp->preq[0] = SMP_CMD_PAIRING_REQ; 509 + memcpy(&smp->preq[1], &cp, sizeof(cp)); 516 510 517 511 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); 518 512 } else { ··· 530 524 static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) 531 525 { 532 526 struct smp_cmd_encrypt_info *rp = (void *) skb->data; 527 + struct smp_chan *smp = conn->smp_chan; 533 528 534 529 skb_pull(skb, sizeof(*rp)); 535 530 536 - memcpy(conn->tk, rp->ltk, sizeof(conn->tk)); 531 + memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); 537 532 538 533 return 0; 539 534 } ··· 542 535 static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) 543 536 { 544 537 struct smp_cmd_master_ident *rp = (void *) skb->data; 538 + struct smp_chan *smp = conn->smp_chan; 545 539 546 540 skb_pull(skb, sizeof(*rp)); 547 541 548 - hci_add_ltk(conn->hcon->hdev, 1, conn->src, conn->smp_key_size, 549 - rp->ediv, rp->rand, conn->tk); 542 + hci_add_ltk(conn->hcon->hdev, 1, conn->src, smp->smp_key_size, 543 + rp->ediv, rp->rand, smp->tk); 550 544 551 545 smp_distribute_keys(conn, 1); 552 546 ··· 562 554 563 555 if (!lmp_host_le_capable(conn->hcon->hdev)) { 564 556 err = -ENOTSUPP; 565 - reason = SMP_PAIRING_NOTSUPP; 566 - goto done; 567 - } 568 - 569 - if (IS_ERR(conn->hcon->hdev->tfm)) { 570 - err = PTR_ERR(conn->hcon->hdev->tfm); 571 557 reason = SMP_PAIRING_NOTSUPP; 572 558 goto done; 573 559 } ··· 629 627 int smp_distribute_keys(struct l2cap_conn *conn, __u8 force) 630 628 { 631 629 struct smp_cmd_pairing *req, *rsp; 630 + struct smp_chan *smp = conn->smp_chan; 632 631 __u8 *keydist; 633 632 634 633 BT_DBG("conn %p force %d", conn, force); 635 634 636 - if (IS_ERR(conn->hcon->hdev->tfm)) 637 - return PTR_ERR(conn->hcon->hdev->tfm); 638 - 639 635 if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend)) 640 636 return 0; 641 637 642 - rsp = (void *) &conn->prsp[1]; 638 + rsp = (void *) &smp->prsp[1]; 643 639 644 640 /* The responder sends its keys first */ 645 641 if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07)) 646 642 return 0; 647 643 648 - req = (void *) &conn->preq[1]; 644 + req = (void *) &smp->preq[1]; 649 645 650 646 if (conn->hcon->out) { 651 647 keydist = &rsp->init_key_dist; ··· 667 667 668 668 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); 669 669 670 - hci_add_ltk(conn->hcon->hdev, 1, conn->dst, conn->smp_key_size, 670 + hci_add_ltk(conn->hcon->hdev, 1, conn->dst, smp->smp_key_size, 671 671 ediv, ident.rand, enc.ltk); 672 672 673 673 ident.ediv = cpu_to_le16(ediv);