Skip to contentSkip to conversation

device: Run reverse service discovery on the bonded bearer

PATCH Superseded Simon Mikuda 2 participants 4 messages Nov 24 – Nov 25 Bluetooth drivers

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 device: Run reverse service discovery on the bonded bearer

When a remote bonds with us, reverse service discovery is started after DISCOVERY_TIMER, but it browses SDP whenever the device supports BR/EDR, whichever bearer bonded. A dual-mode phone that connects and bonds over LE is then paged for SDP while only LE is connected. A browse on one bearer also makes a browse on the other fail with -EBUSY, as there is a single request per device.

Keep the discovery timer and the browse request per bearer, and discover SDP on BR/EDR and GATT on LE.

Assisted-by Claude:claude-opus-5-5
---
 src/device.c | 122 +++++++++++++++++++++++++++------------------------
 1 file changed, 65 insertions(+), 57 deletions(-)
src/device.c +65 -57
index 36a980d1e..40d17bae4 100644
@@ -157,6 +157,8 @@ struct bearer_state {
 	bool connectable;
 	time_t last_seen;
 	time_t last_used;
+	unsigned int discov_timer;	/* reverse service discovery timer */
+	struct browse_req *browse;	/* service discovery request */
 };
 
 struct ltk_info {
@@ -246,9 +248,7 @@ struct btd_device {
 	bool		connectable;
 	bool		cable_pairing;
 	unsigned int	disconn_timer;
-	unsigned int	discov_timer;
 	unsigned int	temporary_timer;	/* Temporary/disappear timer */
-	struct browse_req *browse;		/* service discover request */
 	struct bonding_req *bonding;
 	struct authentication_req *authr;	/* authentication request */
 	uint8_t		bonding_status;
@@ -787,8 +787,10 @@ static void browse_request_free(struct browse_req *req)
 {
 	struct btd_device *device = req->device;
 
-	if (device->browse == req)
-		device->browse = NULL;
+	if (device->bredr_state.browse == req)
+		device->bredr_state.browse = NULL;
+	if (device->le_state.browse == req)
+		device->le_state.browse = NULL;
 
 	if (req->listener_id)
 		g_dbus_remove_watch(dbus_conn, req->listener_id);
@@ -928,8 +930,10 @@ static void device_free(gpointer user_data)
 	if (device->disconn_timer)
 		timeout_remove(device->disconn_timer);
 
-	if (device->discov_timer)
-		timeout_remove(device->discov_timer);
+	if (device->bredr_state.discov_timer)
+		timeout_remove(device->bredr_state.discov_timer);
+	if (device->le_state.discov_timer)
+		timeout_remove(device->le_state.discov_timer);
 
 	if (device->temporary_timer)
 		timeout_remove(device->temporary_timer);
@@ -2076,8 +2080,10 @@ void device_request_disconnect(struct btd_device *device, DBusMessage *msg)
 	if (device->bonding)
 		bonding_request_cancel(device->bonding);
 
-	if (device->browse)
-		browse_request_cancel(device->browse);
+	if (device->bredr_state.browse)
+		browse_request_cancel(device->bredr_state.browse);
+	if (device->le_state.browse)
+		browse_request_cancel(device->le_state.browse);
 
 	if (device->att_io) {
 		g_io_channel_shutdown(device->att_io, FALSE, NULL);
@@ -2660,7 +2666,7 @@ void btd_device_update_allowed_services(struct btd_device *dev)
 	/* If service discovery is ongoing, let the service discovery complete
 	 * callback call this function.
 	 */
-	if (dev->browse) {
+	if (dev->bredr_state.browse || dev->le_state.browse) {
 		ba2str(&dev->bdaddr, addr);
 		DBG("service discovery of %s is ongoing. Skip updating allowed "
 							"services", addr);
@@ -2801,7 +2807,7 @@ int btd_device_connect_services(struct btd_device *dev, GSList *services)
 	GSList *l;
 	uint8_t bdaddr_type;
 
-	if (dev->pending || dev->connect || dev->browse)
+	if (dev->pending || dev->connect || dev->bredr_state.browse)
 		return -EBUSY;
 
 	if (!btd_adapter_get_powered(dev->adapter))
@@ -2852,7 +2858,7 @@ DBusMessage *device_connect_profiles(struct btd_device *dev,
 	DBG("%s %s, client %s", dev->path, uuid ? uuid : "(all)",
 						dbus_message_get_sender(msg));
 
-	if (dev->pending || dev->connect || dev->browse)
+	if (dev->pending || dev->connect || dev->bredr_state.browse)
 		return btd_error_in_progress_str(msg, ERR_BREDR_CONN_BUSY);
 
 	if (!btd_adapter_get_powered(dev->adapter)) {
@@ -3206,7 +3212,7 @@ static void browse_request_complete(struct browse_req *req, uint8_t type,
 
 	/* if successfully resolved services we need to free browsing request
 	 * before passing message back to connect functions, otherwise
-	 * device->browse is set and "InProgress" error is returned instead
+	 * the bearer's browse is set and "InProgress" error is returned instead
 	 * of actually connecting services
 	 */
 	msg = dbus_message_ref(req->msg);
@@ -3251,7 +3257,7 @@ static void device_svc_resolved(struct btd_device *dev, uint8_t browse_type,
 						uint8_t bdaddr_type, int err)
 {
 	struct bearer_state *state = get_state(dev, bdaddr_type);
-	struct browse_req *req = dev->browse;
+	struct browse_req *req = state->browse;
 
 	DBG("%s err %d", dev->path, err);
 
@@ -5658,8 +5664,10 @@ void device_remove(struct btd_device *device, gboolean remove_stored)
 		device_cancel_bonding(device, status);
 	}
 
-	if (device->browse)
-		browse_request_cancel(device->browse);
+	if (device->bredr_state.browse)
+		browse_request_cancel(device->bredr_state.browse);
+	if (device->le_state.browse)
+		browse_request_cancel(device->le_state.browse);
 
 	while (device->services != NULL) {
 		struct btd_service *service = device->services->data;
@@ -6364,7 +6372,7 @@ static void att_disconnected_cb(int err, void *user_data)
 
 	DBG("");
 
-	if (device->browse)
+	if (device->le_state.browse)
 		goto done;
 
 	DBG("%s (%d)", strerror(err), err);
@@ -6392,7 +6400,7 @@ done:
 
 static void register_gatt_services(struct btd_device *device)
 {
-	struct browse_req *req = device->browse;
+	struct browse_req *req = device->le_state.browse;
 	GSList *services = NULL;
 
 	if (!bt_gatt_client_is_ready(device->client))
@@ -6797,8 +6805,8 @@ static void att_connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
 			adapter_connect_list_add(device->adapter, device);
 		}
 
-		if (device->browse)
-			browse_request_complete(device->browse,
+		if (device->le_state.browse)
+			browse_request_complete(device->le_state.browse,
 						BROWSE_GATT,
 						device->bdaddr_type,
 						-ECONNABORTED);
@@ -6924,15 +6932,17 @@ static struct browse_req *browse_request_new(struct btd_device *device,
 							DBusMessage *msg)
 {
 	struct browse_req *req;
+	struct bearer_state *state = type == BROWSE_SDP ?
+				&device->bredr_state : &device->le_state;
 
-	if (device->browse)
+	if (state->browse)
 		return NULL;
 
 	req = g_new0(struct browse_req, 1);
 	req->device = device;
 	req->type = type;
 
-	device->browse = req;
+	state->browse = req;
 
 	if (!msg)
 		return req;
@@ -7051,16 +7061,7 @@ static int device_browse_sdp(struct btd_device *device, DBusMessage *msg)
 static gboolean device_is_browsing(struct btd_device *device,
 					uint8_t bdaddr_type)
 {
-	if (!device->browse)
-		return FALSE;
-
-	if (bdaddr_type == BDADDR_BREDR && device->browse->type == BROWSE_SDP)
-		return TRUE;
-
-	if (bdaddr_type != BDADDR_BREDR && device->browse->type == BROWSE_GATT)
-		return TRUE;
-
-	return FALSE;
+	return get_state(device, bdaddr_type)->browse != NULL;
 }
 
 void device_cancel_browse(struct btd_device *device, uint8_t bdaddr_type)
@@ -7078,12 +7079,13 @@ void device_cancel_browse(struct btd_device *device, uint8_t bdaddr_type)
 	else
 		attio_cleanup(device);
 
-	browse_request_free(device->browse);
+	browse_request_free(get_state(device, bdaddr_type)->browse);
 }
 
 int device_discover_services(struct btd_device *device,
 				uint8_t bdaddr_type, DBusMessage *msg)
 {
+	struct bearer_state *state = get_state(device, bdaddr_type);
 	int err;
 
 	if (bdaddr_type == BDADDR_BREDR)
@@ -7091,9 +7093,9 @@ int device_discover_services(struct btd_device *device,
 	else
 		err = device_browse_gatt(device, msg);
 
-	if (err == 0 && device->discov_timer) {
-		timeout_remove(device->discov_timer);
-		device->discov_timer = 0;
+	if (err == 0 && state->discov_timer) {
+		timeout_remove(state->discov_timer);
+		state->discov_timer = 0;
 	}
 
 	return err;
@@ -7435,16 +7437,22 @@ bool device_is_connectable(struct btd_device *device)
 	return state->connectable;
 }
 
-static bool start_discovery_cb(gpointer user_data)
+static bool start_sdp_discovery_cb(gpointer user_data)
 {
 	struct btd_device *device = user_data;
 
-	if (device->bredr)
-		device_browse_sdp(device, NULL);
-	else
-		device_browse_gatt(device, NULL);
+	device->bredr_state.discov_timer = 0;
+	device_browse_sdp(device, NULL);
+
+	return FALSE;
+}
+
+static bool start_gatt_discovery_cb(gpointer user_data)
+{
+	struct btd_device *device = user_data;
 
-	device->discov_timer = 0;
+	device->le_state.discov_timer = 0;
+	device_browse_gatt(device, NULL);
 
 	return FALSE;
 }
@@ -7618,18 +7626,18 @@ void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type,
 						DBUS_TYPE_INVALID);
 		}
 		bonding_request_free(bonding);
-	} else if (!state->svc_resolved) {
-		if (!device->browse && !device->discov_timer &&
-				btd_opts.reverse_discovery) {
-			/* If we are not initiators and there is no currently
-			 * active discovery or discovery timer, set discovery
-			 * timer */
-			DBG("setting timer for reverse service discovery");
-			device->discov_timer = timeout_add_seconds(
-							DISCOVERY_TIMER,
-							start_discovery_cb,
-							device, NULL);
-		}
+	} else if (!state->svc_resolved && !state->browse &&
+			!state->discov_timer && btd_opts.reverse_discovery) {
+		/* If we are not initiators and there is no currently
+		 * active discovery or discovery timer, set discovery
+		 * timer on the bearer that was bonded.
+		 */
+		DBG("setting timer for reverse service discovery");
+		state->discov_timer = timeout_add_seconds(DISCOVERY_TIMER,
+					bdaddr_type == BDADDR_BREDR ?
+					start_sdp_discovery_cb :
+					start_gatt_discovery_cb,
+					device, NULL);
 	}
 }
 
@@ -7667,12 +7675,12 @@ unsigned int device_wait_for_svc_complete(struct btd_device *dev,
 	if (state->svc_resolved || !btd_opts.reverse_discovery)
 		cb->idle_id = g_idle_add(svc_idle_cb, cb);
 	else {
-		if (dev->discov_timer > 0)
-			timeout_remove(dev->discov_timer);
+		if (dev->bredr_state.discov_timer > 0)
+			timeout_remove(dev->bredr_state.discov_timer);
 
-		dev->discov_timer = timeout_add_seconds(
+		dev->bredr_state.discov_timer = timeout_add_seconds(
 						0,
-						start_discovery_cb,
+						start_sdp_discovery_cb,
 						dev, NULL);
 	}
 

base-commit: 1608f836188610aabeff5a3164169ebe0360e443

Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 main.conf: Add 'paired' to ReverseServiceDiscovery

Reverse GATT discovery on a device that connected to us but is not paired reads characteristics that may need encryption. The remote answers those with ATT error Insufficient Authentication (0x05) or Insufficient Encryption (0x0f), and bt_att then raises the link's security level to retry the read, which sends a pairing request to the device although nobody asked for pairing.

Add 'paired' in addition to true and false to reverse discovery conf it waits until the device is paired, and the GATT client of an LE link that connected without one is started once it is.

Set the default ReverseServiceDiscovery to new 'paired' option.

Assisted-by Claude:claude-opus-5-5
---
 src/btd.h     |  8 +++++++-
 src/device.c  | 26 +++++++++++++++++++++++---
 src/main.c    | 29 ++++++++++++++++++++++++++---
 src/main.conf |  7 +++++--
 4 files changed, 61 insertions(+), 9 deletions(-)
src/btd.h +7 -1
index 9bf310271..2c897756c 100644
@@ -48,6 +48,12 @@ enum bt_gatt_export_t {
 	BT_GATT_EXPORT_READ_WRITE,
 };
 
+enum reverse_discovery_t {
+	REVERSE_DISCOVERY_NEVER,
+	REVERSE_DISCOVERY_ALWAYS,
+	REVERSE_DISCOVERY_PAIRED,
+};
+
 struct btd_br_defaults {
 	uint16_t	page_scan_type;
 	uint16_t	page_scan_interval;
@@ -144,7 +150,7 @@ struct btd_opts {
 
 	struct btd_defaults defaults;
 
-	bool		reverse_discovery;
+	uint8_t		reverse_discovery;
 	bool		name_resolv;
 	bool		debug_keys;
 	bool		fast_conn;
src/device.c +23 -3
index 40d17bae4..3ece82c8d 100644
@@ -350,6 +350,18 @@ bool btd_device_is_initiator(struct btd_device *dev)
 	return dev->att_io ? true : false;
 }
 
+static bool reverse_discovery(struct btd_device *dev, uint8_t bdaddr_type)
+{
+	switch (btd_opts.reverse_discovery) {
+	case REVERSE_DISCOVERY_ALWAYS:
+		return true;
+	case REVERSE_DISCOVERY_PAIRED:
+		return device_is_paired(dev, bdaddr_type);
+	default:
+		return false;
+	}
+}
+
 static GSList *find_service_with_profile(GSList *list, struct btd_profile *p)
 {
 	GSList *l;
@@ -6471,7 +6483,8 @@ static void gatt_client_init(struct btd_device *device)
 
 	gatt_client_cleanup(device);
 
-	if (!btd_device_is_initiator(device) && !btd_opts.reverse_discovery) {
+	if (!btd_device_is_initiator(device) &&
+			!reverse_discovery(device, device->bdaddr_type)) {
 		DBG("Reverse service discovery disabled: skipping GATT client");
 		return;
 	}
@@ -6970,6 +6983,12 @@ static int device_browse_gatt(struct btd_device *device, DBusMessage *msg)
 	if (!req)
 		return -EBUSY;
 
+	/* Connected without a client, as reverse discovery waited for the
+	 * device to be paired: start it on the existing link.
+	 */
+	if (!device->client && device->att)
+		gatt_client_init(device);
+
 	if (device->client) {
 		/*
 		 * If discovery has not yet completed, then wait for gatt-client
@@ -7627,7 +7646,8 @@ void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type,
 		}
 		bonding_request_free(bonding);
 	} else if (!state->svc_resolved && !state->browse &&
-			!state->discov_timer && btd_opts.reverse_discovery) {
+			!state->discov_timer &&
+			reverse_discovery(device, bdaddr_type)) {
 		/* If we are not initiators and there is no currently
 		 * active discovery or discovery timer, set discovery
 		 * timer on the bearer that was bonded.
@@ -7672,7 +7692,7 @@ unsigned int device_wait_for_svc_complete(struct btd_device *dev,
 
 	dev->svc_callbacks = g_slist_prepend(dev->svc_callbacks, cb);
 
-	if (state->svc_resolved || !btd_opts.reverse_discovery)
+	if (state->svc_resolved || !reverse_discovery(dev, BDADDR_BREDR))
 		cb->idle_id = g_idle_add(svc_idle_cb, cb);
 	else {
 		if (dev->bredr_state.discov_timer > 0)
src/main.c +26 -3
index 3a051c5a4..4003a9627 100644
@@ -924,6 +924,30 @@ static bool parse_config_bool(GKeyFile *config, const char *group,
 	return true;
 }
 
+static void parse_reverse_discovery(GKeyFile *config)
+{
+	char *str = NULL;
+	bool val;
+
+	if (parse_config_bool(config, "General", "ReverseServiceDiscovery",
+								&val)) {
+		btd_opts.reverse_discovery = val ? REVERSE_DISCOVERY_ALWAYS :
+						REVERSE_DISCOVERY_NEVER;
+		return;
+	}
+
+	if (!parse_config_string(config, "General", "ReverseServiceDiscovery",
+								&str))
+		return;
+
+	if (!strcmp(str, "paired"))
+		btd_opts.reverse_discovery = REVERSE_DISCOVERY_PAIRED;
+	else
+		warn("Invalid value for ReverseServiceDiscovery: %s", str);
+
+	g_free(str);
+}
+
 static void parse_privacy(GKeyFile *config)
 {
 	char *str = NULL;
@@ -1096,8 +1120,7 @@ static void parse_general(GKeyFile *config)
 						&btd_opts.pairto,
 						0, UINT32_MAX);
 	parse_device_id(config);
-	parse_config_bool(config, "General", "ReverseServiceDiscovery",
-						&btd_opts.reverse_discovery);
+	parse_reverse_discovery(config);
 	parse_config_bool(config, "General", "NameResolving",
 						&btd_opts.name_resolv);
 	parse_config_bool(config, "General", "DebugKeys",
@@ -1473,7 +1496,7 @@ static void init_defaults(void)
 	btd_opts.pairto = DEFAULT_PAIRABLE_TIMEOUT;
 	btd_opts.discovto = DEFAULT_DISCOVERABLE_TIMEOUT;
 	btd_opts.tmpto = DEFAULT_TEMPORARY_TIMEOUT;
-	btd_opts.reverse_discovery = TRUE;
+	btd_opts.reverse_discovery = REVERSE_DISCOVERY_PAIRED;
 	btd_opts.name_resolv = TRUE;
 	btd_opts.debug_keys = FALSE;
 	btd_opts.refresh_discovery = TRUE;
src/main.conf +5 -2
index c9906148d..b2c834244 100644
@@ -34,8 +34,11 @@
 # BITE tester doesn't like us doing reverse SDP for some test cases, for LE
 # this disables the GATT client functionally so it can be used in system which
 # can only operate as peripheral (see also GATT Client option).
-# Defaults to 'true'.
-#ReverseServiceDiscovery = true
+# Possible values: 'true', 'false' or 'paired'. With 'paired' it is done only
+# once the device is paired, so that reading the services of a device that
+# connected to us does not start pairing when they need encryption.
+# Defaults to 'paired'.
+#ReverseServiceDiscovery = paired
 
 # Enable name resolving after inquiry. Set it to 'false' if you don't need
 # remote devices name and want shorter discovery cycle. Defaults to 'true'.
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH BlueZ 2/2] main.conf: Add 'paired' to ReverseServiceDiscovery

Hi Simon,

Quoted 11 lines · Simon Mikuda

Reverse GATT discovery on a device that connected to us but is not paired reads characteristics that may need encryption. The remote answers those with ATT error Insufficient Authentication (0x05) or Insufficient Encryption (0x0f), and bt_att then raises the link's security level to retry the read, which sends a pairing request to the device although nobody asked for pairing.

Add 'paired' in addition to true and false to reverse discovery conf it waits until the device is paired, and the GATT client of an LE link that connected without one is started once it is.

Hmm, actually it is not the reverse discovery isn't what triggers the read, etc.; it's the accept callback which causes the profiles to start the 'attach' procedures.

Quoted 160 lines

Set the default ReverseServiceDiscovery to new 'paired' option.

Assisted-by Claude:claude-opus-5-5
---
 src/btd.h     |  8 +++++++-
 src/device.c  | 26 +++++++++++++++++++++++---
 src/main.c    | 29 ++++++++++++++++++++++++++---
 src/main.conf |  7 +++++--
 4 files changed, 61 insertions(+), 9 deletions(-)
src/btd.h +7 -1
index 9bf310271..2c897756c 100644
@@ -48,6 +48,12 @@ enum bt_gatt_export_t {
        BT_GATT_EXPORT_READ_WRITE,
 };
 
+enum reverse_discovery_t {
+       REVERSE_DISCOVERY_NEVER,
+       REVERSE_DISCOVERY_ALWAYS,
+       REVERSE_DISCOVERY_PAIRED,
+};
+
 struct btd_br_defaults {
        uint16_t        page_scan_type;
        uint16_t        page_scan_interval;
@@ -144,7 +150,7 @@ struct btd_opts {
 
        struct btd_defaults defaults;
 
-       bool            reverse_discovery;
+       uint8_t         reverse_discovery;
        bool            name_resolv;
        bool            debug_keys;
        bool            fast_conn;
src/device.c +23 -3
index 40d17bae4..3ece82c8d 100644
@@ -350,6 +350,18 @@ bool btd_device_is_initiator(struct btd_device *dev)
        return dev->att_io ? true : false;
 }
 
+static bool reverse_discovery(struct btd_device *dev, uint8_t bdaddr_type)
+{
+       switch (btd_opts.reverse_discovery) {
+       case REVERSE_DISCOVERY_ALWAYS:
+               return true;
+       case REVERSE_DISCOVERY_PAIRED:
+               return device_is_paired(dev, bdaddr_type);
+       default:
+               return false;
+       }
+}
+
 static GSList *find_service_with_profile(GSList *list, struct btd_profile *p)
 {
        GSList *l;
@@ -6471,7 +6483,8 @@ static void gatt_client_init(struct btd_device *device)
 
        gatt_client_cleanup(device);
 
-       if (!btd_device_is_initiator(device) && !btd_opts.reverse_discovery) {
+       if (!btd_device_is_initiator(device) &&
+                       !reverse_discovery(device, device->bdaddr_type)) {
                DBG("Reverse service discovery disabled: skipping GATT client");
                return;
        }
@@ -6970,6 +6983,12 @@ static int device_browse_gatt(struct btd_device *device, DBusMessage *msg)
        if (!req)
                return -EBUSY;
 
+       /* Connected without a client, as reverse discovery waited for the
+        * device to be paired: start it on the existing link.
+        */
+       if (!device->client && device->att)
+               gatt_client_init(device);
+
        if (device->client) {
                /*
                 * If discovery has not yet completed, then wait for gatt-client
@@ -7627,7 +7646,8 @@ void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type,
                }
                bonding_request_free(bonding);
        } else if (!state->svc_resolved && !state->browse &&
-                       !state->discov_timer && btd_opts.reverse_discovery) {
+                       !state->discov_timer &&
+                       reverse_discovery(device, bdaddr_type)) {
                /* If we are not initiators and there is no currently
                 * active discovery or discovery timer, set discovery
                 * timer on the bearer that was bonded.
@@ -7672,7 +7692,7 @@ unsigned int device_wait_for_svc_complete(struct btd_device *dev,
 
        dev->svc_callbacks = g_slist_prepend(dev->svc_callbacks, cb);
 
-       if (state->svc_resolved || !btd_opts.reverse_discovery)
+       if (state->svc_resolved || !reverse_discovery(dev, BDADDR_BREDR))
                cb->idle_id = g_idle_add(svc_idle_cb, cb);
        else {
                if (dev->bredr_state.discov_timer > 0)
src/main.c +26 -3
index 3a051c5a4..4003a9627 100644
@@ -924,6 +924,30 @@ static bool parse_config_bool(GKeyFile *config, const char *group,
        return true;
 }
 
+static void parse_reverse_discovery(GKeyFile *config)
+{
+       char *str = NULL;
+       bool val;
+
+       if (parse_config_bool(config, "General", "ReverseServiceDiscovery",
+                                                               &val)) {
+               btd_opts.reverse_discovery = val ? REVERSE_DISCOVERY_ALWAYS :
+                                               REVERSE_DISCOVERY_NEVER;
+               return;
+       }
+
+       if (!parse_config_string(config, "General", "ReverseServiceDiscovery",
+                                                               &str))
+               return;
+
+       if (!strcmp(str, "paired"))
+               btd_opts.reverse_discovery = REVERSE_DISCOVERY_PAIRED;
+       else
+               warn("Invalid value for ReverseServiceDiscovery: %s", str);
+
+       g_free(str);
+}
+
 static void parse_privacy(GKeyFile *config)
 {
        char *str = NULL;
@@ -1096,8 +1120,7 @@ static void parse_general(GKeyFile *config)
                                                &btd_opts.pairto,
                                                0, UINT32_MAX);
        parse_device_id(config);
-       parse_config_bool(config, "General", "ReverseServiceDiscovery",
-                                               &btd_opts.reverse_discovery);
+       parse_reverse_discovery(config);
        parse_config_bool(config, "General", "NameResolving",
                                                &btd_opts.name_resolv);
        parse_config_bool(config, "General", "DebugKeys",
@@ -1473,7 +1496,7 @@ static void init_defaults(void)
        btd_opts.pairto = DEFAULT_PAIRABLE_TIMEOUT;
        btd_opts.discovto = DEFAULT_DISCOVERABLE_TIMEOUT;
        btd_opts.tmpto = DEFAULT_TEMPORARY_TIMEOUT;
-       btd_opts.reverse_discovery = TRUE;
+       btd_opts.reverse_discovery = REVERSE_DISCOVERY_PAIRED;
        btd_opts.name_resolv = TRUE;
        btd_opts.debug_keys = FALSE;
        btd_opts.refresh_discovery = TRUE;
src/main.conf +0 -0
index c9906148d..b2c834244 100644

src/main.conf line 41

Quoted 10 lines
src/main.conf +5 -2
@@ -34,8 +34,11 @@
 # BITE tester doesn't like us doing reverse SDP for some test cases, for LE
 # this disables the GATT client functionally so it can be used in system which
 # can only operate as peripheral (see also GATT Client option).
-# Defaults to 'true'.
-#ReverseServiceDiscovery = true
+# Possible values: 'true', 'false' or 'paired'. With 'paired' it is done only
+# once the device is paired, so that reading the services of a device that
+# connected to us does not start pairing when they need encryption.
+# Defaults to 'paired'.
+#ReverseServiceDiscovery = paired

I can leave with it being an optional argument, but not the default, that said this may overlap with the [GATT] client support, although this maybe cleaner, but we can't really anticipate if any profile, etc, is not depending on the auto pairing logic.

Quoted 6 lines
src/main.conf +0 -1
 # Enable name resolving after inquiry. Set it to 'false' if you don't need
 # remote devices name and want shorter discovery cycle. Defaults to 'true'.
--

2.43.0

Signature · 1 line
Luiz Augusto von Dentz
#3 lore raw

Re: [PATCH BlueZ 2/2] main.conf: Add 'paired' to ReverseServiceDiscovery

Hi Luiz

Thanks for comments. I switched default back to true and reworded description.

Thanks!

Quoted 200 lines · Luiz Augusto von Dentz

Hi Simon,

Quoted 10 lines · Simon Mikuda

Reverse GATT discovery on a device that connected to us but is not paired reads characteristics that may need encryption. The remote answers those with ATT error Insufficient Authentication (0x05) or Insufficient Encryption (0x0f), and bt_att then raises the link's security level to retry the read, which sends a pairing request to the device although nobody asked for pairing.

Add 'paired' in addition to true and false to reverse discovery conf it waits until the device is paired, and the GATT client of an LE link that connected without one is started once it is.

Hmm, actually it is not the reverse discovery isn't what triggers the read, etc.; it's the accept callback which causes the profiles to start the 'attach' procedures.

Quoted 160 lines

Set the default ReverseServiceDiscovery to new 'paired' option.

Assisted-by Claude:claude-opus-5-5
---
  src/btd.h     |  8 +++++++-
  src/device.c  | 26 +++++++++++++++++++++++---
  src/main.c    | 29 ++++++++++++++++++++++++++---
  src/main.conf |  7 +++++--
  4 files changed, 61 insertions(+), 9 deletions(-)
src/btd.h +7 -1
index 9bf310271..2c897756c 100644
@@ -48,6 +48,12 @@ enum bt_gatt_export_t {
         BT_GATT_EXPORT_READ_WRITE,
  };
 
+enum reverse_discovery_t {
+       REVERSE_DISCOVERY_NEVER,
+       REVERSE_DISCOVERY_ALWAYS,
+       REVERSE_DISCOVERY_PAIRED,
+};
+
  struct btd_br_defaults {
         uint16_t        page_scan_type;
         uint16_t        page_scan_interval;
@@ -144,7 +150,7 @@ struct btd_opts {
 
         struct btd_defaults defaults;
 
-       bool            reverse_discovery;
+       uint8_t         reverse_discovery;
         bool            name_resolv;
         bool            debug_keys;
         bool            fast_conn;
src/device.c +23 -3
index 40d17bae4..3ece82c8d 100644
@@ -350,6 +350,18 @@ bool btd_device_is_initiator(struct btd_device *dev)
         return dev->att_io ? true : false;
  }
 
+static bool reverse_discovery(struct btd_device *dev, uint8_t bdaddr_type)
+{
+       switch (btd_opts.reverse_discovery) {
+       case REVERSE_DISCOVERY_ALWAYS:
+               return true;
+       case REVERSE_DISCOVERY_PAIRED:
+               return device_is_paired(dev, bdaddr_type);
+       default:
+               return false;
+       }
+}
+
  static GSList *find_service_with_profile(GSList *list, struct btd_profile *p)
  {
         GSList *l;
@@ -6471,7 +6483,8 @@ static void gatt_client_init(struct btd_device *device)
 
         gatt_client_cleanup(device);
 
-       if (!btd_device_is_initiator(device) && !btd_opts.reverse_discovery) {
+       if (!btd_device_is_initiator(device) &&
+                       !reverse_discovery(device, device->bdaddr_type)) {
                 DBG("Reverse service discovery disabled: skipping GATT client");
                 return;
         }
@@ -6970,6 +6983,12 @@ static int device_browse_gatt(struct btd_device *device, DBusMessage *msg)
         if (!req)
                 return -EBUSY;
 
+       /* Connected without a client, as reverse discovery waited for the
+        * device to be paired: start it on the existing link.
+        */
+       if (!device->client && device->att)
+               gatt_client_init(device);
+
         if (device->client) {
                 /*
                  * If discovery has not yet completed, then wait for gatt-client
@@ -7627,7 +7646,8 @@ void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type,
                 }
                 bonding_request_free(bonding);
         } else if (!state->svc_resolved && !state->browse &&
-                       !state->discov_timer && btd_opts.reverse_discovery) {
+                       !state->discov_timer &&
+                       reverse_discovery(device, bdaddr_type)) {
                 /* If we are not initiators and there is no currently
                  * active discovery or discovery timer, set discovery
                  * timer on the bearer that was bonded.
@@ -7672,7 +7692,7 @@ unsigned int device_wait_for_svc_complete(struct btd_device *dev,
 
         dev->svc_callbacks = g_slist_prepend(dev->svc_callbacks, cb);
 
-       if (state->svc_resolved || !btd_opts.reverse_discovery)
+       if (state->svc_resolved || !reverse_discovery(dev, BDADDR_BREDR))
                 cb->idle_id = g_idle_add(svc_idle_cb, cb);
         else {
                 if (dev->bredr_state.discov_timer > 0)
src/main.c +26 -3
index 3a051c5a4..4003a9627 100644
@@ -924,6 +924,30 @@ static bool parse_config_bool(GKeyFile *config, const char *group,
         return true;
  }
 
+static void parse_reverse_discovery(GKeyFile *config)
+{
+       char *str = NULL;
+       bool val;
+
+       if (parse_config_bool(config, "General", "ReverseServiceDiscovery",
+                                                               &val)) {
+               btd_opts.reverse_discovery = val ? REVERSE_DISCOVERY_ALWAYS :
+                                               REVERSE_DISCOVERY_NEVER;
+               return;
+       }
+
+       if (!parse_config_string(config, "General", "ReverseServiceDiscovery",
+                                                               &str))
+               return;
+
+       if (!strcmp(str, "paired"))
+               btd_opts.reverse_discovery = REVERSE_DISCOVERY_PAIRED;
+       else
+               warn("Invalid value for ReverseServiceDiscovery: %s", str);
+
+       g_free(str);
+}
+
  static void parse_privacy(GKeyFile *config)
  {
         char *str = NULL;
@@ -1096,8 +1120,7 @@ static void parse_general(GKeyFile *config)
                                                 &btd_opts.pairto,
                                                 0, UINT32_MAX);
         parse_device_id(config);
-       parse_config_bool(config, "General", "ReverseServiceDiscovery",
-                                               &btd_opts.reverse_discovery);
+       parse_reverse_discovery(config);
         parse_config_bool(config, "General", "NameResolving",
                                                 &btd_opts.name_resolv);
         parse_config_bool(config, "General", "DebugKeys",
@@ -1473,7 +1496,7 @@ static void init_defaults(void)
         btd_opts.pairto = DEFAULT_PAIRABLE_TIMEOUT;
         btd_opts.discovto = DEFAULT_DISCOVERABLE_TIMEOUT;
         btd_opts.tmpto = DEFAULT_TEMPORARY_TIMEOUT;
-       btd_opts.reverse_discovery = TRUE;
+       btd_opts.reverse_discovery = REVERSE_DISCOVERY_PAIRED;
         btd_opts.name_resolv = TRUE;
         btd_opts.debug_keys = FALSE;
         btd_opts.refresh_discovery = TRUE;
src/main.conf +0 -0
index c9906148d..b2c834244 100644

src/main.conf line 41

Quoted 10 lines
src/main.conf +5 -2
@@ -34,8 +34,11 @@
  # BITE tester doesn't like us doing reverse SDP for some test cases, for LE
  # this disables the GATT client functionally so it can be used in system which
  # can only operate as peripheral (see also GATT Client option).
-# Defaults to 'true'.
-#ReverseServiceDiscovery = true
+# Possible values: 'true', 'false' or 'paired'. With 'paired' it is done only
+# once the device is paired, so that reading the services of a device that
+# connected to us does not start pairing when they need encryption.
+# Defaults to 'paired'.
+#ReverseServiceDiscovery = paired

I can leave with it being an optional argument, but not the default, that said this may overlap with the [GATT] client support, although this maybe cleaner, but we can't really anticipate if any profile, etc, is not depending on the auto pairing logic.

Quoted 6 lines
src/main.conf +0 -1
  # Enable name resolving after inquiry. Set it to 'false' if you don't need
  # remote devices name and want shorter discovery cycle. Defaults to 'true'.
--

2.43.0