hci_conn.c (37b16beaa92860c378273ccdcc2ccb22c6cee047) hci_conn.c (5a134faeef82b46ff4ad244d11d8c6be41679834)
1/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
4
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as

--- 353 unchanged lines hidden (view full) ---

362 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
363 &conn->dst);
364}
365
366static void le_conn_timeout(struct work_struct *work)
367{
368 struct hci_conn *conn = container_of(work, struct hci_conn,
369 le_conn_timeout.work);
1/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
4
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as

--- 353 unchanged lines hidden (view full) ---

362 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
363 &conn->dst);
364}
365
366static void le_conn_timeout(struct work_struct *work)
367{
368 struct hci_conn *conn = container_of(work, struct hci_conn,
369 le_conn_timeout.work);
370 struct hci_dev *hdev = conn->hdev;
370
371 BT_DBG("");
372
371
372 BT_DBG("");
373
374 /* We could end up here due to having done directed advertising,
375 * so clean up the state if necessary. This should however only
376 * happen with broken hardware or if low duty cycle was used
377 * (which doesn't have a timeout of its own).
378 */
379 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
380 u8 enable = 0x00;
381 hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
382 &enable);
383 hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
384 return;
385 }
386
373 hci_le_create_connection_cancel(conn);
374}
375
376struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
377{
378 struct hci_conn *conn;
379
380 BT_DBG("%s dst %pMR", hdev->name, dst);

--- 7 unchanged lines hidden (view full) ---

388 conn->hdev = hdev;
389 conn->type = type;
390 conn->mode = HCI_CM_ACTIVE;
391 conn->state = BT_OPEN;
392 conn->auth_type = HCI_AT_GENERAL_BONDING;
393 conn->io_capability = hdev->io_capability;
394 conn->remote_auth = 0xff;
395 conn->key_type = 0xff;
387 hci_le_create_connection_cancel(conn);
388}
389
390struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
391{
392 struct hci_conn *conn;
393
394 BT_DBG("%s dst %pMR", hdev->name, dst);

--- 7 unchanged lines hidden (view full) ---

402 conn->hdev = hdev;
403 conn->type = type;
404 conn->mode = HCI_CM_ACTIVE;
405 conn->state = BT_OPEN;
406 conn->auth_type = HCI_AT_GENERAL_BONDING;
407 conn->io_capability = hdev->io_capability;
408 conn->remote_auth = 0xff;
409 conn->key_type = 0xff;
410 conn->tx_power = HCI_TX_POWER_INVALID;
396
397 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
398 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
399
400 switch (type) {
401 case ACL_LINK:
402 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
403 break;
411
412 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
413 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
414
415 switch (type) {
416 case ACL_LINK:
417 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
418 break;
419 case LE_LINK:
420 /* conn->src should reflect the local identity address */
421 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
422 break;
404 case SCO_LINK:
405 if (lmp_esco_capable(hdev))
406 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
407 (hdev->esco_type & EDR_ESCO_MASK);
408 else
409 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
410 break;
411 case ESCO_LINK:

--- 128 unchanged lines hidden (view full) ---

540 hci_proto_connect_cfm(conn, status);
541
542 hci_conn_del(conn);
543
544 /* Since we may have temporarily stopped the background scanning in
545 * favor of connection establishment, we should restart it.
546 */
547 hci_update_background_scan(hdev);
423 case SCO_LINK:
424 if (lmp_esco_capable(hdev))
425 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
426 (hdev->esco_type & EDR_ESCO_MASK);
427 else
428 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
429 break;
430 case ESCO_LINK:

--- 128 unchanged lines hidden (view full) ---

559 hci_proto_connect_cfm(conn, status);
560
561 hci_conn_del(conn);
562
563 /* Since we may have temporarily stopped the background scanning in
564 * favor of connection establishment, we should restart it.
565 */
566 hci_update_background_scan(hdev);
567
568 /* Re-enable advertising in case this was a failed connection
569 * attempt as a peripheral.
570 */
571 mgmt_reenable_advertising(hdev);
548}
549
550static void create_le_conn_complete(struct hci_dev *hdev, u8 status)
551{
552 struct hci_conn *conn;
553
554 if (status == 0)
555 return;

--- 44 unchanged lines hidden (view full) ---

600 cp.min_ce_len = cpu_to_le16(0x0000);
601 cp.max_ce_len = cpu_to_le16(0x0000);
602
603 hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
604
605 conn->state = BT_CONNECT;
606}
607
572}
573
574static void create_le_conn_complete(struct hci_dev *hdev, u8 status)
575{
576 struct hci_conn *conn;
577
578 if (status == 0)
579 return;

--- 44 unchanged lines hidden (view full) ---

624 cp.min_ce_len = cpu_to_le16(0x0000);
625 cp.max_ce_len = cpu_to_le16(0x0000);
626
627 hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
628
629 conn->state = BT_CONNECT;
630}
631
632static void hci_req_directed_advertising(struct hci_request *req,
633 struct hci_conn *conn)
634{
635 struct hci_dev *hdev = req->hdev;
636 struct hci_cp_le_set_adv_param cp;
637 u8 own_addr_type;
638 u8 enable;
639
640 enable = 0x00;
641 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
642
643 /* Clear the HCI_ADVERTISING bit temporarily so that the
644 * hci_update_random_address knows that it's safe to go ahead
645 * and write a new random address. The flag will be set back on
646 * as soon as the SET_ADV_ENABLE HCI command completes.
647 */
648 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
649
650 /* Set require_privacy to false so that the remote device has a
651 * chance of identifying us.
652 */
653 if (hci_update_random_address(req, false, &own_addr_type) < 0)
654 return;
655
656 memset(&cp, 0, sizeof(cp));
657 cp.type = LE_ADV_DIRECT_IND;
658 cp.own_address_type = own_addr_type;
659 cp.direct_addr_type = conn->dst_type;
660 bacpy(&cp.direct_addr, &conn->dst);
661 cp.channel_map = hdev->le_adv_channel_map;
662
663 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
664
665 enable = 0x01;
666 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
667
668 conn->state = BT_CONNECT;
669}
670
608struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
609 u8 dst_type, u8 sec_level, u8 auth_type)
610{
611 struct hci_conn_params *params;
612 struct hci_conn *conn;
613 struct smp_irk *irk;
614 struct hci_request req;
615 int err;
616
671struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
672 u8 dst_type, u8 sec_level, u8 auth_type)
673{
674 struct hci_conn_params *params;
675 struct hci_conn *conn;
676 struct smp_irk *irk;
677 struct hci_request req;
678 int err;
679
617 if (test_bit(HCI_ADVERTISING, &hdev->flags))
618 return ERR_PTR(-ENOTSUPP);
619
620 /* Some devices send ATT messages as soon as the physical link is
621 * established. To be able to handle these ATT messages, the user-
622 * space first establishes the connection and then starts the pairing
623 * process.
624 *
625 * So if a hci_conn object already exists for the following connection
626 * attempt, we simply update pending_sec_level and auth_type fields
627 * and return the object found.

--- 31 unchanged lines hidden (view full) ---

659 dst_type = ADDR_LE_DEV_RANDOM;
660 }
661
662 conn = hci_conn_add(hdev, LE_LINK, dst);
663 if (!conn)
664 return ERR_PTR(-ENOMEM);
665
666 conn->dst_type = dst_type;
680 /* Some devices send ATT messages as soon as the physical link is
681 * established. To be able to handle these ATT messages, the user-
682 * space first establishes the connection and then starts the pairing
683 * process.
684 *
685 * So if a hci_conn object already exists for the following connection
686 * attempt, we simply update pending_sec_level and auth_type fields
687 * and return the object found.

--- 31 unchanged lines hidden (view full) ---

719 dst_type = ADDR_LE_DEV_RANDOM;
720 }
721
722 conn = hci_conn_add(hdev, LE_LINK, dst);
723 if (!conn)
724 return ERR_PTR(-ENOMEM);
725
726 conn->dst_type = dst_type;
667
668 conn->out = true;
669 conn->link_mode |= HCI_LM_MASTER;
670 conn->sec_level = BT_SECURITY_LOW;
671 conn->pending_sec_level = sec_level;
672 conn->auth_type = auth_type;
673
727 conn->sec_level = BT_SECURITY_LOW;
728 conn->pending_sec_level = sec_level;
729 conn->auth_type = auth_type;
730
731 hci_req_init(&req, hdev);
732
733 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
734 hci_req_directed_advertising(&req, conn);
735 goto create_conn;
736 }
737
738 conn->out = true;
739 conn->link_mode |= HCI_LM_MASTER;
740
674 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
675 if (params) {
676 conn->le_conn_min_interval = params->conn_min_interval;
677 conn->le_conn_max_interval = params->conn_max_interval;
678 } else {
679 conn->le_conn_min_interval = hdev->le_conn_min_interval;
680 conn->le_conn_max_interval = hdev->le_conn_max_interval;
681 }
682
741 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
742 if (params) {
743 conn->le_conn_min_interval = params->conn_min_interval;
744 conn->le_conn_max_interval = params->conn_max_interval;
745 } else {
746 conn->le_conn_min_interval = hdev->le_conn_min_interval;
747 conn->le_conn_max_interval = hdev->le_conn_max_interval;
748 }
749
683 hci_req_init(&req, hdev);
684
685 /* If controller is scanning, we stop it since some controllers are
686 * not able to scan and connect at the same time. Also set the
687 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
688 * handler for scan disabling knows to set the correct discovery
689 * state.
690 */
691 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {
692 hci_req_add_le_scan_disable(&req);
693 set_bit(HCI_LE_SCAN_INTERRUPTED, &hdev->dev_flags);
694 }
695
696 hci_req_add_le_create_conn(&req, conn);
697
750 /* If controller is scanning, we stop it since some controllers are
751 * not able to scan and connect at the same time. Also set the
752 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
753 * handler for scan disabling knows to set the correct discovery
754 * state.
755 */
756 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {
757 hci_req_add_le_scan_disable(&req);
758 set_bit(HCI_LE_SCAN_INTERRUPTED, &hdev->dev_flags);
759 }
760
761 hci_req_add_le_create_conn(&req, conn);
762
763create_conn:
698 err = hci_req_run(&req, create_le_conn_complete);
699 if (err) {
700 hci_conn_del(conn);
701 return ERR_PTR(err);
702 }
703
704done:
705 hci_conn_hold(conn);

--- 108 unchanged lines hidden (view full) ---

814 /* Make sure we preserve an existing MITM requirement*/
815 auth_type |= (conn->auth_type & 0x01);
816
817 conn->auth_type = auth_type;
818
819 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
820 struct hci_cp_auth_requested cp;
821
764 err = hci_req_run(&req, create_le_conn_complete);
765 if (err) {
766 hci_conn_del(conn);
767 return ERR_PTR(err);
768 }
769
770done:
771 hci_conn_hold(conn);

--- 108 unchanged lines hidden (view full) ---

880 /* Make sure we preserve an existing MITM requirement*/
881 auth_type |= (conn->auth_type & 0x01);
882
883 conn->auth_type = auth_type;
884
885 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
886 struct hci_cp_auth_requested cp;
887
888 /* encrypt must be pending if auth is also pending */
889 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
890
822 cp.handle = cpu_to_le16(conn->handle);
823 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
824 sizeof(cp), &cp);
891 cp.handle = cpu_to_le16(conn->handle);
892 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
893 sizeof(cp), &cp);
825
826 /* If we're already encrypted set the REAUTH_PEND flag,
827 * otherwise set the ENCRYPT_PEND.
828 */
829 if (conn->key_type != 0xff)
830 set_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
894 if (conn->key_type != 0xff)
895 set_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
831 else
832 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
833 }
834
835 return 0;
836}
837
838/* Encrypt the the link */
839static void hci_conn_encrypt(struct hci_conn *conn)
840{

--- 369 unchanged lines hidden ---
896 }
897
898 return 0;
899}
900
901/* Encrypt the the link */
902static void hci_conn_encrypt(struct hci_conn *conn)
903{

--- 369 unchanged lines hidden ---