From 699436a3b6ef415e4b1b3b88fccddc666f24dd12 Mon Sep 17 00:00:00 2001 From: Joakim Andersson Date: Tue, 6 Aug 2019 12:45:31 +0200 Subject: [PATCH] Bluetooth: Host: Fix bug in creating connection with wrong own address. Fix bug introduced by: 45da629b24f720019355fca8e1ddddb798ac409f Mistakenly set the own address type to the destination address type. Also this uses the RPA_OR_RANDOM in case local IRKs exists in the controller. Signed-off-by: Joakim Andersson --- subsys/bluetooth/host/hci_core.c | 45 ++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index a0922dd9941..9c741ceb724 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -690,6 +690,33 @@ static int hci_le_create_conn(const struct bt_conn *conn) { struct net_buf *buf; struct bt_hci_cp_le_create_conn *cp; + u8_t own_addr_type; + int err; + + if (IS_ENABLED(CONFIG_BT_PRIVACY)) { + err = le_set_private_addr(conn->id); + if (err) { + return err; + } + + if (BT_FEAT_LE_PRIVACY(bt_dev.le.features)) { + own_addr_type = BT_HCI_OWN_ADDR_RPA_OR_RANDOM; + } else { + own_addr_type = BT_ADDR_LE_RANDOM; + } + } else { + /* If Static Random address is used as Identity address we + * need to restore it before creating connection. Otherwise + * NRPA used for active scan could be used for connection. + */ + const bt_addr_le_t *own_addr = &bt_dev.id_addr[conn->id]; + + if (own_addr->type == BT_ADDR_LE_RANDOM) { + set_random_address(&own_addr->a); + } + + own_addr_type = own_addr->type; + } buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_CONN, sizeof(*cp)); if (!buf) { @@ -704,7 +731,7 @@ static int hci_le_create_conn(const struct bt_conn *conn) cp->scan_window = cp->scan_interval; bt_addr_le_copy(&cp->peer_addr, &conn->le.dst); - cp->own_addr_type = conn->le.dst.type; + cp->own_addr_type = own_addr_type; cp->conn_interval_min = sys_cpu_to_le16(conn->le.interval_min); cp->conn_interval_max = sys_cpu_to_le16(conn->le.interval_max); cp->conn_latency = sys_cpu_to_le16(conn->le.latency); @@ -1428,22 +1455,6 @@ static void check_pending_conn(const bt_addr_le_t *id_addr, goto failed; } - if (IS_ENABLED(CONFIG_BT_PRIVACY)) { - if (le_set_private_addr(BT_ID_DEFAULT)) { - goto failed; - } - } else { - const bt_addr_le_t *own_addr = &bt_dev.id_addr[conn->id]; - - /* If Static Random address is used as Identity address we - * need to restore it before creating connection. Otherwise - * NRPA used for active scan could be used for connection. - */ - if (own_addr->type == BT_ADDR_LE_RANDOM) { - set_random_address(&own_addr->a); - } - } - if (hci_le_create_conn(conn)) { goto failed; }