Bluetooth: Fix Add Device response status
Subsystems (2)
PATCH v2 1/4 Bluetooth: Fix Add Device response status
Make sure that MGMT_STATUS_INVALID_PARAMS is returned when non identity address is passed to ADD_DEVICE. Right now MGMT_STATUS_FAILED is returned, which might be misleading.
--- net/bluetooth/mgmt.c | 7 +++++++ 1 file changed, 7 insertions(+)
net/bluetooth/mgmt.c +7 -0
@@ -6226,6 +6226,13 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,else auto_conn =HCI_AUTO_CONN_REPORT ; +/* Allow only identity addresses */ +if (!hci_is_identity_address (&cp->addr.bdaddr, addr_type)) { + err = cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); +mgmt_pending_remove (cmd); + goto unlock; + } +/* If the connection parameters don't exist for this device, * they will be created and configured with defaults. */
Signature · 1 line
2.1.4
PATCH v2 2/4 Bluetooth: Move irk checking logic in preparation to new connect method
--- net/bluetooth/hci_core.c | 11 ----------- net/bluetooth/mgmt.c | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 12 deletions(-)
net/bluetooth/hci_core.c +0 -11
@@ -2822,10 +2822,6 @@ struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev, {struct hci_conn_params *params; -/* The conn params list only contains identity addresses */ -if (!hci_is_identity_address (addr, addr_type)) -return NULL ; -list_for_each_entry (params, &hdev->le_conn_params, list) {if (bacmp (¶ms->addr, addr) ==0 && params->addr_type == addr_type) {
@@ -2842,10 +2838,6 @@ struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list, {struct hci_conn_params *param; -/* The list only contains identity addresses */ -if (!hci_is_identity_address (addr, addr_type)) -return NULL ; -list_for_each_entry (param, list, action) {if (bacmp (¶m->addr, addr) ==0 && param->addr_type == addr_type)
@@ -2861,9 +2853,6 @@ struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev, {struct hci_conn_params *params; -if (!hci_is_identity_address (addr, addr_type)) -return NULL ; - params =hci_conn_params_lookup (hdev, addr, addr_type);if (params)return params;
net/bluetooth/mgmt.c +16 -1
@@ -6226,7 +6226,11 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,else auto_conn =HCI_AUTO_CONN_REPORT ; -/* Allow only identity addresses */ +/* Kernel internally uses conn_params with resolvable private +* address, but Remove Device allows only identity addresses. +* Make sure it is enforced before calling +* hci_conn_params_lookup. +*/ if (!hci_is_identity_address (&cp->addr.bdaddr, addr_type)) { err = cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS );mgmt_pending_remove (cmd);
@@ -6347,6 +6351,17 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,else addr_type =ADDR_LE_DEV_RANDOM ; +/* Kernel internally uses conn_params with resolvable private +* address, but Remove Device allows only identity addresses. +* Make sure it is enforced before calling +* hci_conn_params_lookup. +*/ +if (!hci_is_identity_address (&cp->addr.bdaddr, addr_type)) { + err = cmd->cmd_complete (cmd, +MGMT_STATUS_INVALID_PARAMS ); +mgmt_pending_remove (cmd); + goto unlock; + } params =hci_conn_params_lookup (hdev, &cp->addr.bdaddr, addr_type);if (!params) {
Signature · 1 line
2.1.4
PATCH v2 3/4 Bluetooth: Fix connection to already paired device.
In reply to Jakub PawlowskiCurrently, when trying to connect to already paired device that just rotated its RPA MAC address, old address would be used and connection would fail. In order to fix that, kernel must scan and receive advertisement with fresh RPA before connecting.
This patch adds infrastructure for future changes in connection estabilishment procedure behaviour for all devices. Instead of just sending HCI_OP_LE_CREATE_CONN to controller, "connect" will add device to kernel whitelist and start scan. If advertisement is received, it'll be compared against whitelist and then trigger connection if it matches. That fixes mentioned reconnect issue for already paired devices. It also make whole connection procedure more robust. We can try to connect to multiple devices at same time now, even though controller allow only one.
pend_le_sock_conn field was added to hci_dev struct. It is list of all devices that we try to connect to. When the connection is succesully estabilished, or after timeout params will be removed from this list or deleted, i.e. if params structure is not used for autoconnect.
HCI_AUTO_CONN_JUST_ONCE value was added to hci_conn_params. It is used to mark hci_conn_params that are to be used to connect to device only once, and be removed after sending connect request.
--- include/net/bluetooth/hci_core.h | 13 ++++ net/bluetooth/hci_conn.c | 147 ++++++++++++++++++++++++++++++++++++--- net/bluetooth/hci_core.c | 30 ++++++++ net/bluetooth/hci_event.c | 70 +++++++++++++------ net/bluetooth/hci_request.c | 32 ++++++++- 5 files changed, 259 insertions(+), 33 deletions(-)
include/net/bluetooth/hci_core.h +13 -0
@@ -357,6 +357,7 @@ struct hci_dev {struct list_head le_conn_params;struct list_head pend_le_conns;struct list_head pend_le_reports; +struct list_head pend_le_sock_conn;struct hci_dev_stats stat;
@@ -497,6 +498,7 @@ struct hci_chan {struct hci_conn_params {struct list_head list;struct list_head action; +struct list_head sock_action;bdaddr_t addr;u8 addr_type;
@@ -512,6 +514,7 @@ struct hci_conn_params {HCI_AUTO_CONN_DIRECT ,HCI_AUTO_CONN_ALWAYS ,HCI_AUTO_CONN_LINK_LOSS , +HCI_AUTO_CONN_SOCK , } auto_connect;struct hci_conn *conn;
@@ -823,6 +826,10 @@ void hci_chan_del(struct hci_chan *chan);void hci_chan_list_flush (struct hci_conn *conn);struct hci_chan *hci_chan_lookup_handle (struct hci_dev *hdev,__u16 handle); +struct hci_conn *hci_add_to_sock_conn_whitelist (struct hci_dev *hdev, +bdaddr_t *dst,u8 dst_type,u8 sec_level, +u16 conn_timeout,u8 role); +void hci_remove_from_sock_conn_whitelist (struct hci_conn *conn);struct hci_conn *hci_connect_le (struct hci_dev *hdev,bdaddr_t *dst,u8 dst_type,u8 sec_level,u16 conn_timeout,u8 role);
@@ -898,6 +905,10 @@ static inline void hci_conn_drop(struct hci_conn *conn) }else { timeo =0 ; } + +if (conn->state ==BT_OPEN ) +hci_remove_from_sock_conn_whitelist (conn); +break ;case AMP_LINK :
@@ -988,6 +999,8 @@ void hci_conn_params_clear_disabled(struct hci_dev *hdev);struct hci_conn_params *hci_pend_le_action_lookup (struct list_head *list,bdaddr_t *addr,u8 addr_type); +struct hci_conn_params *hci_pend_le_sock_action_lookup (struct list_head *list, +bdaddr_t *addr,u8 addr_type);void hci_uuids_clear (struct hci_dev *hdev);
net/bluetooth/hci_conn.c +139 -8
184 lines not on this page · raw message
net/bluetooth/hci_core.c +30 -0
@@ -2848,6 +2848,22 @@ struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list, }/* This function requires the caller holds hdev->lock */ +struct hci_conn_params *hci_pend_le_sock_action_lookup (struct list_head *list, +bdaddr_t *addr, +u8 addr_type) +{ +struct hci_conn_params *param; + +list_for_each_entry (param, list, sock_action) { +if (bacmp (¶m->addr, addr) ==0 && + param->addr_type == addr_type) +return param; + } + +return NULL ; +} + +/* This function requires the caller holds hdev->lock */ struct hci_conn_params *hci_conn_params_add (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type) {
@@ -2868,6 +2884,7 @@ struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,list_add (¶ms->list, &hdev->le_conn_params);INIT_LIST_HEAD (¶ms->action); +INIT_LIST_HEAD (¶ms->sock_action); params->conn_min_interval = hdev->le_conn_min_interval; params->conn_max_interval = hdev->le_conn_max_interval;
@@ -2888,6 +2905,7 @@ static void hci_conn_params_free(struct hci_conn_params *params) }list_del (¶ms->action); +list_del (¶ms->sock_action);list_del (¶ms->list);kfree (params); }
@@ -2916,6 +2934,17 @@ void hci_conn_params_clear_disabled(struct hci_dev *hdev)list_for_each_entry_safe (params, tmp, &hdev->le_conn_params, list) {if (params->auto_connect !=HCI_AUTO_CONN_DISABLED )continue ; + +/* If trying to estabilish one time connection to disabled +* device, leave the params, but mark them as just once. +*/ +if (hci_pend_le_sock_action_lookup (&hdev->pend_le_sock_conn, + ¶ms->addr, + params->addr_type)) { + params->auto_connect =HCI_AUTO_CONN_SOCK ; +continue ; + } +list_del (¶ms->list);kfree (params); }
@@ -3187,6 +3216,7 @@ struct hci_dev *hci_alloc_dev(void)INIT_LIST_HEAD (&hdev->le_conn_params);INIT_LIST_HEAD (&hdev->pend_le_conns);INIT_LIST_HEAD (&hdev->pend_le_reports); +INIT_LIST_HEAD (&hdev->pend_le_sock_conn);INIT_LIST_HEAD (&hdev->conn_hash.list);INIT_LIST_HEAD (&hdev->adv_instances);
net/bluetooth/hci_event.c +47 -23
97 lines not on this page · raw message
net/bluetooth/hci_request.c +30 -2
@@ -180,7 +180,10 @@ static u8 update_white_list(struct hci_request *req)if (hci_pend_le_action_lookup (&hdev->pend_le_conns, &b->bdaddr, b->bdaddr_type) ||hci_pend_le_action_lookup (&hdev->pend_le_reports, - &b->bdaddr, b->bdaddr_type)) { + &b->bdaddr, b->bdaddr_type) || +hci_pend_le_sock_action_lookup (&hdev->pend_le_sock_conn, + &b->bdaddr, + b->bdaddr_type)) { white_list_entries++;continue ; }
@@ -246,6 +249,30 @@ static u8 update_white_list(struct hci_request *req)add_to_white_list (req, params); } +/* After adding all new pending connections, and pending reports walk +* through the list of connection requests and also add these to the +* white list if there is still space. +*/ +list_for_each_entry (params, &hdev->pend_le_sock_conn, sock_action) { +if (hci_bdaddr_list_lookup (&hdev->le_white_list, + ¶ms->addr, params->addr_type)) +continue ; + +if (white_list_entries >= hdev->le_white_list_size) { +/* Select filter policy to accept all advertising */ +return 0x00 ; + } + +if (hci_find_irk_by_addr (hdev, ¶ms->addr, + params->addr_type)) { +/* White list can not be used with RPAs */ +return 0x00 ; + } + + white_list_entries++; +add_to_white_list (req, params); + } +/* Select filter policy to use white list */ return 0x01 ; }
@@ -507,7 +534,8 @@ void __hci_update_background_scan(struct hci_request *req)hci_discovery_filter_clear (hdev);if (list_empty (&hdev->pend_le_conns) && -list_empty (&hdev->pend_le_reports)) { +list_empty (&hdev->pend_le_reports) && +list_empty (&hdev->pend_le_sock_conn)) {/* If there is no pending LE connections or devices * to be scanned for, we should stop the background * scanning.
Signature · 1 line
2.1.4
PATCH v2 4/4 Bluetooth: Enable new connection establishment procedure.
In reply to Jakub PawlowskiCurrently, when trying to connect to already paired device that just rotated its RPA MAC address, old address would be used and connection would fail. In order to fix that, kernel must scan and receive advertisement with fresh RPA before connecting.
This patch enables new connection establishment procedure. Instead of just sending HCI_OP_LE_CREATE_CONN to controller, "connect" will add device to kernel whitelist and start scan. If advertisement is received, it'll be compared against whitelist and then trigger connection if it matches. That fixes mentioned reconnect issue for already paired devices. It also make whole connection procedure more robust. We can try to connect to multiple devices at same time now, even though controller allow only one.
--- net/bluetooth/l2cap_core.c | 6 ++++-- net/bluetooth/mgmt.c | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-)
net/bluetooth/l2cap_core.c +4 -2
@@ -7113,8 +7113,10 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,else role =HCI_ROLE_MASTER ; - hcon =hci_connect_le (hdev, dst, dst_type, chan->sec_level, -HCI_LE_CONN_TIMEOUT , role); + hcon =hci_add_to_sock_conn_whitelist (hdev, dst, dst_type, + chan->sec_level, +HCI_LE_CONN_TIMEOUT , + role); }else {u8 auth_type =l2cap_get_auth_type (chan); hcon =hci_connect_acl (hdev, dst, chan->sec_level, auth_type);
net/bluetooth/mgmt.c +12 -4
@@ -3195,13 +3195,20 @@ static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,else conn =hci_conn_hash_lookup_ba (hdev,LE_LINK , &cp->addr.bdaddr); -if (!conn || conn->state ==BT_OPEN || conn->state ==BT_CLOSED ) { +if (!conn || conn->state ==BT_CLOSED ) { err =mgmt_cmd_complete (sk, hdev->id,MGMT_OP_DISCONNECT ,MGMT_STATUS_NOT_CONNECTED , &rp,sizeof (rp)); goto failed; } +if (conn->state ==BT_OPEN ) { +hci_remove_from_sock_conn_whitelist (conn); + err =mgmt_cmd_complete (sk, hdev->id,MGMT_OP_DISCONNECT , +MGMT_STATUS_SUCCESS , &rp,sizeof (rp)); + goto failed; + } + cmd =mgmt_pending_add (sk,MGMT_OP_DISCONNECT , hdev, data, len);if (!cmd) { err = -ENOMEM ;
@@ -3564,9 +3571,10 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,*/ hci_conn_params_add (hdev, &cp->addr.bdaddr, addr_type); - conn =hci_connect_le (hdev, &cp->addr.bdaddr, addr_type, - sec_level,HCI_LE_CONN_TIMEOUT , -HCI_ROLE_MASTER ); + conn =hci_add_to_sock_conn_whitelist (hdev, &cp->addr.bdaddr, + addr_type, sec_level, +HCI_LE_CONN_TIMEOUT , +HCI_ROLE_MASTER ); }if (IS_ERR (conn)) {
Signature · 1 line
2.1.4