diff mbox series

[BlueZ,2/2] gatt: Fix creating duplicated objects

Message ID 20230308005158.2661414-2-luiz.dentz@gmail.com (mailing list archive)
State Accepted
Commit 3842320f450e0b40b205b8fd0ce13b8821d49b51
Headers show
Series [BlueZ,1/2] device: Fix not always storing device info | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/CheckPatch success CheckPatch PASS
tedd_an/GitLint success Gitlint PASS
tedd_an/IncrementalBuild success Incremental Build PASS

Commit Message

Luiz Augusto von Dentz March 8, 2023, 12:51 a.m. UTC
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This checks cid before attempting to create device, if the device is
using an RPA it could be that the MGMT event has not been processed yet
which would lead to create a second copy of the same device using its
identity address.
---
 src/gatt-database.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/src/gatt-database.c b/src/gatt-database.c
index ea282d4bc193..3b53bf2a3c84 100644
--- a/src/gatt-database.c
+++ b/src/gatt-database.c
@@ -632,6 +632,7 @@  static void connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
 	struct btd_device *device;
 	uint8_t dst_type;
 	bdaddr_t src, dst;
+	uint16_t cid;
 
 	if (gerr) {
 		error("%s", gerr->message);
@@ -641,6 +642,7 @@  static void connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
 	bt_io_get(io, &gerr, BT_IO_OPT_SOURCE_BDADDR, &src,
 						BT_IO_OPT_DEST_BDADDR, &dst,
 						BT_IO_OPT_DEST_TYPE, &dst_type,
+						BT_IO_OPT_CID, &cid,
 						BT_IO_OPT_INVALID);
 	if (gerr) {
 		error("bt_io_get: %s", gerr->message);
@@ -655,9 +657,21 @@  static void connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
 	if (!adapter)
 		return;
 
-	device = btd_adapter_get_device(adapter, &dst, dst_type);
-	if (!device)
+	/* Check cid before attempting to create device, if the device is using
+	 * an RPA it could be that the MGMT event has not been processed yet
+	 * which would lead to create a second copy of the same device using its
+	 * identity address.
+	 */
+	if (cid == BT_ATT_CID)
+		device = btd_adapter_get_device(adapter, &dst, dst_type);
+	else
+		device = btd_adapter_find_device(adapter, &dst, dst_type);
+
+	if (!device) {
+		error("Unable to find device, dropping connection attempt");
+		g_io_channel_shutdown(io, FALSE, NULL);
 		return;
+	}
 
 	device_attach_att(device, io);
 }