Skip to contentSkip to conversation

Bluetooth: Fix connection to already paired device.

PATCH Superseded Jakub Pawlowski 2 participants 5 messages Jul 22, 2015 – Jul 23, 2015

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 Bluetooth: Fix connection to already paired device.

Currently, 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.

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.

New flag, HCI_CONN_SCANNING, is added to hci_conn flags. When it's set, connect is scan phase. It gets cleared when advertisement is received, and HCI_OP_LE_CREATE_CONN is sent.

---
 include/net/bluetooth/hci_core.h |  30 ++++++
 net/bluetooth/hci_conn.c         | 216 ++++++++++++++++++++++++++++++++++++---
 net/bluetooth/hci_core.c         |  33 ++++++
 net/bluetooth/hci_event.c        |  62 +++++++----
 net/bluetooth/mgmt.c             |   6 ++
 5 files changed, 312 insertions(+), 35 deletions(-)
include/net/bluetooth/hci_core.h +30 -0
index 2a6b091..b36e598 100644
@@ -512,9 +512,11 @@ 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;
+	bool is_sock_conn;
 };
 
 extern struct list_head hci_dev_list;
@@ -639,6 +641,7 @@ enum {
 	HCI_CONN_DROP,
 	HCI_CONN_PARAM_REMOVAL_PEND,
 	HCI_CONN_NEW_LINK_KEY,
+	HCI_CONN_SCANNING,
 };
 
 static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
@@ -808,6 +811,27 @@ static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
 	return NULL;
 }
 
+static inline struct hci_conn *hci_conn_hash_lookup_state_no_flag(
+			struct hci_dev *hdev, __u8 type, __u16 state, int flag)
+{
+	struct hci_conn_hash *h = &hdev->conn_hash;
+	struct hci_conn  *c;
+
+	rcu_read_lock();
+
+	list_for_each_entry_rcu(c, &h->list, list) {
+		if (c->type == type && c->state == state &&
+		    !test_bit(flag, &c->flags)) {
+			rcu_read_unlock();
+			return c;
+		}
+	}
+
+	rcu_read_unlock();
+
+	return NULL;
+}
+
 int hci_disconnect(struct hci_conn *conn, __u8 reason);
 bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
 void hci_sco_setup(struct hci_conn *conn, __u8 status);
@@ -823,6 +847,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);
@@ -988,6 +1016,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 hci_dev *hdev,
+						  bdaddr_t *addr, u8 addr_type);
 
 void hci_uuids_clear(struct hci_dev *hdev);
 
net/bluetooth/hci_conn.c +203 -13

272 lines not on this page · raw message

net/bluetooth/hci_core.c +33 -0
index bc43b64..8f7902b 100644
@@ -2848,6 +2848,30 @@ 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 hci_dev *hdev,
+						       bdaddr_t *addr,
+						       u8 addr_type)
+{
+	struct hci_conn_params *param;
+
+	list_for_each_entry(param, &hdev->pend_le_conns, action) {
+		if (bacmp(&param->addr, addr) == 0 &&
+		    param->addr_type == addr_type &&
+		    param->is_sock_conn)
+			return param;
+	}
+
+	list_for_each_entry(param, &hdev->pend_le_reports, action) {
+		if (bacmp(&param->addr, addr) == 0 &&
+		    param->addr_type == addr_type &&
+		    param->is_sock_conn)
+			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)
 {
@@ -2916,6 +2940,15 @@ 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 (params->is_sock_conn) {
+			params->auto_connect = HCI_AUTO_CONN_SOCK;
+			continue;
+		}
+
 		list_del(&params->list);
 		kfree(params);
 	}
net/bluetooth/hci_event.c +40 -22

88 lines not on this page · raw message

net/bluetooth/mgmt.c +6 -0
index 7ab1915..ce403ff 100644
@@ -6107,6 +6107,12 @@ static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,
 	switch (auto_connect) {
 	case HCI_AUTO_CONN_DISABLED:
 	case HCI_AUTO_CONN_LINK_LOSS:
+		/* If auto connect is being disabled when we're trying to
+		 * connect to device, keep connecting.
+		 */
+		if (params->is_sock_conn)
+			list_add(&params->action, &hdev->pend_le_conns);
+
 		__hci_update_background_scan(req);
 		break;
 	case HCI_AUTO_CONN_REPORT:
Signature · 1 line
2.1.4
#1 lore raw

PATCH v3 2/2 Bluetooth: Enable new connection establishment procedure.

Currently, 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       | 7 ++++---
 2 files changed, 8 insertions(+), 5 deletions(-)
net/bluetooth/l2cap_core.c +4 -2
index 45fffa4..121d039 100644
@@ -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 +4 -3
index ce403ff..f803abf 100644
@@ -3564,9 +3564,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
#4 lore raw
In reply to Jakub Pawlowski

Hi Jakub,

Quoted 14 lines · Jakub Pawlowski

Currently, 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.

Quoted 1 line

HCI_AUTO_CONN_JUST_ONCE value was added to hci_conn_params. It is used to

I don't see HCI_AUTO_CONN_JUST_ONCE anywhere in the patch. Have you forgotten to update the commit message?

include/net/bluetooth/hci_core.h struct hci_conn_params { · line 520

Quoted 12 lines
include/net/bluetooth/hci_core.h +2 -0
@@ -512,9 +512,11 @@ 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;
+	bool is_sock_conn;
 };

I still don't like having the term "sock" anywhere in the HCI core. The fact that a higher layer is implemented through sockets is something the HCI core shouldn't need to care or know about imo.

Could we perhaps make all of this completely generic and simpler through adding reference counting the hci_conn_params struct? The socket based attempt would own its own reference and drop it after the attempt completes (either in success or failure). Likewise the mgmt based Add Device would add its own reference which would get dropped once Remove Device is called. This way the HCI core wouldn't need to care about any other details of the lifetime of the parameters.

include/net/bluetooth/hci_core.h struct hci_conn_params {

Quoted 9 lines
include/net/bluetooth/hci_core.h +9 -0
+/* This function requires the caller holds hdev->lock */
+void hci_remove_from_sock_conn_whitelist(struct hci_conn *conn)
+{
+	struct hci_request req;
+	struct hci_conn_params *params = hci_pend_le_sock_action_lookup(
+						conn->hdev, &conn->dst,
+						conn->dst_type);
+	if (!params)
+		return;

Call hci_pend_le_sock_action_lookup() here separate from the 'params' variable declaration, and remember that there should be an empty line between the variable declarations and the rest of the function.

include/net/bluetooth/hci_core.h

Quoted 3 lines
include/net/bluetooth/hci_core.h +3 -0
+	hci_req_init(&req, conn->hdev);
+	__hci_update_background_scan(&req);
+	hci_req_run(&req, NULL);

Can't you just use hci_update_background_scan() instead of these three function calls?

As a general comment I think this patch is quite big and it'd be good if you could try to find ways to split it up into smaller pieces.

Johan

#2 lore raw

Re: [PATCH v3 2/2] Bluetooth: Enable new connection establishment procedure.

In reply to Jakub Pawlowski

Hi Jakub,

net/bluetooth/mgmt.c static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data, · line 3570

Quoted 11 lines · Jakub Pawlowski
net/bluetooth/mgmt.c +4 -3
@@ -3564,9 +3564,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);

The purpose we're introducing this passive scanning functionality is so that we have a fresh RPA for the remote device. The way we know that a new RPA matches the remote device is by checking against an IRK. If the user is calling pair_device it means we don't have an IRK, so in this case it seems pointless to me to start passive scanning (not to mention that typically paired device is called straight after stopping an active scan).

Johan

#3 lore raw

Re: [PATCH v3 2/2] Bluetooth: Enable new connection establishment procedure.

Hi Johan,

Quoted 8 lines · Johan Hedberg

Hi Jakub,

Quoted 4 lines · Jakub Pawlowski
net/bluetooth/mgmt.c +0 -0
@@ -3564,9 +3564,10 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
               */
              hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
 
Quoted 15 lines

net/bluetooth/mgmt.c static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data, · line 3570

Quoted 7 lines
net/bluetooth/mgmt.c +4 -3
-             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);

The purpose we're introducing this passive scanning functionality is so that we have a fresh RPA for the remote device. The way we know that a new RPA matches the remote device is by checking against an IRK. If the user is calling pair_device it means we don't have an IRK, so in this case it seems pointless to me to start passive scanning (not to mention that typically paired device is called straight after stopping an active scan).

So right now we start scan no matter what kind of address was provided, it can even be static address, that's how Marcel wanted it to be in one of his comments (
http://article.gmane.org/gmane.linux.bluez.kernel/62704 ) :

" In addition, I do not think it makes a difference if you are an RPA or not. Just run through our background
scanning for all connection attempts. This has the advantage that now simultaneous connection attempts
via L2CAP connect() would not have to wait for each other."

Probably keeping last time when device was seen and choosing what to do might be good option, but maybe for future improvements, not base patch ?

Quoted 2 lines

Johan