Bluetooth: Fix initiator/responder addresses for SMP
Subsystems (2)
v2 cover Bluetooth: Fix initiator/responder addresses for SMP
Hi,
Here's a second attempt to fix the initiator/responder addresses when using privacy. The main difference to the previous attempt is an added protection to HCI_Set_Random_Address so we don't do that while connecting or advertising, and a clarification for the white list corner case in aptch 3/4 that we're taking a "best effort" approach while lacking full tracking of white list initiated connections.
Johan
----------------------------------------------------------------
Johan Hedberg (4):
Bluetooth: Add protections for updating local random address
Bluetooth: Fix updating connection state to BT_CONNECT too early
Bluetooth: Track LE initiator and responder address information
Bluetooth: Use hdev->init/resp_addr values for smp_c1 function
include/net/bluetooth/hci_core.h | 4 ++ net/bluetooth/hci_conn.c | 3 +- net/bluetooth/hci_core.c | 27 ++++++++++++- net/bluetooth/hci_event.c | 78 ++++++++++++++++++++++++++++++++++++ net/bluetooth/mgmt.c | 7 ++++ net/bluetooth/smp.c | 22 +++------- 6 files changed, 122 insertions(+), 19 deletions(-)
PATCH v2 1/4 Bluetooth: Add protections for updating local random address
From: Johan Hedberg <[email protected]>
Different controllers behave differently when HCI_Set_Random_Address is called while they are advertising or have a HCI_LE_Create_Connection in progress. Some take the newly written address into use for the pending operation while others use the random address that we had at the time that the operation started.
Due to this undefined behavior and for the fact that we want to reliably determine the initiator address of all connections for the sake of SMP it's best to simply prevent the random address update if we have these problematic operations in progress.
This patch adds a set_random_addr() helper function for the use of hci_update_random_address which contains the necessary checks for advertising and ongoing LE connections.
One extra thing we need to do is to clear the HCI_ADVERTISING flag in the enable_advertising() function before sending any commands. Since re-enabling advertising happens by calling first disable_advertising() and then enable_advertising() all while having the HCI_ADVERTISING flag set. Clearing the flag lets the set_random_addr() function know that it's safe to write a new address at least as far as advertising is concerned.
--- net/bluetooth/hci_core.c | 27 +++++++++++++++++++++++++-- net/bluetooth/mgmt.c | 7 +++++++ 2 files changed, 32 insertions(+), 2 deletions(-)
net/bluetooth/hci_core.c +25 -2
@@ -3649,6 +3649,29 @@ static void le_scan_disable_work(struct work_struct *work)BT_ERR ("Disable LE scanning request failed: err %d" , err); } +static void set_random_addr (struct hci_request *req,bdaddr_t *rpa) +{ +struct hci_dev *hdev = req->hdev; + +/* If we're advertising or initiating an LE connection we can't +* go ahead and change the random address at this time. This is +* because the eventual initiator address used for the +* subsequently created connection will be undefined (some +* controllers use the new address and others the one we had +* when the operation started). +* +* In this kind of scenario skip the update and let the random +* address be updated at the next cycle. +*/ +if (test_bit (HCI_ADVERTISING , &hdev->dev_flags) || +hci_conn_hash_lookup_state (hdev,LE_LINK ,BT_CONNECT )) { +BT_DBG ("Deferring random address update" ); +return ; + } + +hci_req_add (req,HCI_OP_LE_SET_RANDOM_ADDR ,6 , rpa); +} +int hci_update_random_address (struct hci_request *req,bool require_privacy,u8 *own_addr_type) {
@@ -3674,7 +3697,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,return err; } -hci_req_add (req,HCI_OP_LE_SET_RANDOM_ADDR ,6 , &hdev->rpa); +set_random_addr (req, &hdev->rpa); to =msecs_to_jiffies (hdev->rpa_timeout *1000 );queue_delayed_work (hdev->workqueue, &hdev->rpa_expired, to);
@@ -3693,7 +3716,7 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy, urpa.b[5 ] &=0x3f ;/* Clear two most significant bits */ *own_addr_type =ADDR_LE_DEV_RANDOM ; -hci_req_add (req,HCI_OP_LE_SET_RANDOM_ADDR ,6 , &urpa); +set_random_addr (req, &urpa);return 0 ; }
net/bluetooth/mgmt.c +7 -0
@@ -840,6 +840,13 @@ static void enable_advertising(struct hci_request *req)u8 own_addr_type, enable =0x01 ;bool connectable; +/* Clear the HCI_ADVERTISING bit temporarily so that the +* hci_update_random_address knows that it's safe to go ahead +* and write a new random address. The flag will be set back on +* as soon as the SET_ADV_ENABLE HCI command completes. +*/ +clear_bit (HCI_ADVERTISING , &hdev->dev_flags); + connectable =get_connectable (hdev);/* Set require_privacy to true only when non-connectable
Signature · 1 line
1.8.5.3
PATCH v2 2/4 Bluetooth: Fix updating connection state to BT_CONNECT too early
In reply to [email protected]From: Johan Hedberg <[email protected]>
We shouldn't update the hci_conn state to BT_CONNECT until the moment that we're ready to send the initiating HCI command for it. If the connection has the BT_CONNECT state too early the code responsible for updating the local random address may incorrectly think there's a pending connection in progress and refuse to update the address.
--- net/bluetooth/hci_conn.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/bluetooth/hci_conn.c +2 -1
@@ -588,6 +588,8 @@ static void hci_req_add_le_create_conn(struct hci_request *req, cp.max_ce_len =__constant_cpu_to_le16 (0x0000 );hci_req_add (req,HCI_OP_LE_CREATE_CONN ,sizeof (cp), &cp); + + conn->state =BT_CONNECT ; }static void stop_scan_complete (struct hci_dev *hdev,u8 status)
@@ -689,7 +691,6 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, conn->dst_type = dst_type; - conn->state =BT_CONNECT ; conn->out = true; conn->link_mode |=HCI_LM_MASTER ; conn->sec_level =BT_SECURITY_LOW ;
Signature · 1 line
1.8.5.3
PATCH v2 3/4 Bluetooth: Track LE initiator and responder address information
In reply to [email protected]From: Johan Hedberg <[email protected]>
For SMP we need the local and remote addresses (and their types) that were used to establish the connection. These may be different from the Identity Addresses or even the current RPA. To guarantee that we have this information available and it is correct track these values separately from the very beginning of the connection.
For outgoing connections we set the values as soon as we get a successful command status for HCI_LE_Create_Connection (for which the patch adds a command status handler function) and for incoming connections as soon as we get a LE Connection Complete HCI event.
--- include/net/bluetooth/hci_core.h | 4 +++ net/bluetooth/hci_event.c | 78 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+)
include/net/bluetooth/hci_core.h +4 -0
@@ -332,6 +332,10 @@ struct hci_conn {__u8 dst_type;bdaddr_t src;__u8 src_type; +bdaddr_t init_addr; +__u8 init_addr_type; +bdaddr_t resp_addr; +__u8 resp_addr_type;__u16 handle;__u16 state;__u8 mode;
net/bluetooth/hci_event.c +78 -0
96 lines not on this page · raw message
Signature · 1 line
1.8.5.3
PATCH v2 4/4 Bluetooth: Use hdev->init/resp_addr values for smp_c1 function
In reply to [email protected]From: Johan Hedberg <[email protected]>
Now that we have nicely tracked values of the initiator and responder address information we can pass that directly to the smp_c1 function without worrying e.g. about who initiated the connection. This patch updates the two places in smp.c to use the new variables.
--- net/bluetooth/smp.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-)
net/bluetooth/smp.c +6 -16
@@ -445,14 +445,9 @@ static void confirm_work(struct work_struct *work)/* Prevent mutual access to hdev->tfm_aes */ hci_dev_lock (hdev); -if (conn->hcon->out) - ret =smp_c1 (tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, - conn->hcon->src_type, &conn->hcon->src, - conn->hcon->dst_type, &conn->hcon->dst, res); -else - ret =smp_c1 (tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, - conn->hcon->dst_type, &conn->hcon->dst, - conn->hcon->src_type, &conn->hcon->src, res); + ret =smp_c1 (tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, + conn->hcon->init_addr_type, &conn->hcon->init_addr, + conn->hcon->resp_addr_type, &conn->hcon->resp_addr, res);hci_dev_unlock (hdev);
@@ -492,14 +487,9 @@ static void random_work(struct work_struct *work)/* Prevent mutual access to hdev->tfm_aes */ hci_dev_lock (hdev); -if (hcon->out) - ret =smp_c1 (tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, - hcon->src_type, &hcon->src, - hcon->dst_type, &hcon->dst, res); -else - ret =smp_c1 (tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, - hcon->dst_type, &hcon->dst, - hcon->src_type, &hcon->src, res); + ret =smp_c1 (tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, + hcon->init_addr_type, &hcon->init_addr, + hcon->resp_addr_type, &hcon->resp_addr, res);hci_dev_unlock (hdev);
Signature · 1 line
1.8.5.3
Marcel Holtmann
maintainerHi Johan,
Quoted 16 lines
Here's a second attempt to fix the initiator/responder addresses when using privacy. The main difference to the previous attempt is an added protection to
HCI_Set_Random_Addressso we don't do that while connecting or advertising, and a clarification for the white list corner case in aptch 3/4 that we're taking a "best effort" approach while lacking full tracking of white list initiated connections.Johan
----------------------------------------------------------------
Johan Hedberg (4):
Bluetooth: Add protections for updating local random address
Bluetooth: Fix updating connection state toBT_CONNECTtoo early
Bluetooth: Track LE initiator and responder address information
Bluetooth: Use hdev->init/resp_addr values forsmp_c1function
Quoted 7 lines
include/net/bluetooth/hci_core.h | 4 ++ net/bluetooth/hci_conn.c | 3 +- net/bluetooth/hci_core.c | 27 ++++++++++++- net/bluetooth/hci_event.c | 78 ++++++++++++++++++++++++++++++++++++ net/bluetooth/mgmt.c | 7 ++++ net/bluetooth/smp.c | 22 +++------- 6 files changed, 122 insertions(+), 19 deletions(-)
all 4 patches have been applied to bluetooth-next tree.
Regards
Marcel