device: Limit the number of retries on auth failures
PATCH v2 1/1 device: Limit the number of retries on auth failures
Currently BlueZ has no retry limit on reconnection attempts in case of authentication failures. This leads to a very unpleasant user experince with, for example, XBox controller. The scenario is as follows:
1. Host A is running BlueZ and XBox controller is paired and connected to it.
2. Host A shuts down (for example)
3. XBox controller is put into pairing mode and is paired to a Host B
4. Host B is powered down
5. Host A is powered on
6. XBox controller is powered on
7. Host A/BlueZ will start spamming XBox controller with connection attempts in an endless loop
What happens under the hood is:
1. BlueZ starts up, sees that there's LTK saved for XBox controller, so it adds it to kernels autoconnect list
2. Once XBox controller is on and advertising, kernel connects to it
3. BlueZ tries to elevate the security level, but is rejected by the controller with "Key or PIN missing" because the controller is already paired to another host.
4. Controller disconnects, controller goes back to advertising, goto 2
To avoid this, let's add a simple auth failure counter and disable autoconnect for the device once we go past the failure threshold. Let's also implement an expoenential backoff in our retry logic to avoid blasting our retires in a single burst.
---
Changes since [v1]
- Auth failure detection logic is moved to device_remove_connection() which seems like a much better place to make this decision and it also has access to low level disconnect reason that allows us to detect Auth failures
- Added exponential backoff timer to space the retry attempts out
Luis, you mentioned wanting to have some kind of signal to users about this event. Do you want a new kind of event or do you think that
string "org.bluez.Reason.Authentication"
string "Connection terminated due to authentication failure"
that's already emitted in this case is enough?
[v1] https://lore.kernel.org/linux-bluetooth/[email protected]
src/device.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
src/device.c +39 -0
@@ -77,6 +77,7 @@#endif #define RSSI_THRESHOLD 8 +#define AUTH_FAILURES_THRESHOLD 3static DBusConnection *dbus_conn =NULL ;static unsigned service_state_cb_id;
@@ -306,6 +307,9 @@ struct btd_device {time_t name_resolve_failed_time;int8_t volume; + +uint32_t auth_failures; +guint auth_retry_timer; };static const uint16_t uuid_list[] = {
@@ -2824,6 +2828,8 @@ static DBusMessage *dev_connect(DBusConnection *conn, DBusMessage *msg,else bdaddr_type =select_conn_bearer (dev); + dev->auth_failures =0 ; +if (bdaddr_type !=BDADDR_BREDR ) {int err;
@@ -3898,6 +3904,13 @@ static void device_disconnected(struct btd_device *device, uint8_t reason) DBUS_TYPE_INVALID); } +static bool device_auth_failure_retry (gpointer user_data) +{ +struct btd_device *device = user_data; +device_set_auto_connect (device, TRUE); +return false; +} +void device_remove_connection (struct btd_device *device,uint8_t bdaddr_type,bool *remove,uint8_t reason)
@@ -3978,6 +3991,27 @@ void device_remove_connection(struct btd_device *device, uint8_t bdaddr_type,g_slist_free_full (device->eir_uuids, g_free); device->eir_uuids =NULL ; +if (reason ==MGMT_DEV_DISCONN_AUTH_FAILURE ) { +/* +* In case of an auth failure, implement an +* exponential backoff retry logic. We disable auto +* connect immediately to prevent us from retrying to +* connect to this device again and start an 1, 2, 4 +* second timers to re-enable that knob in hopes that +* subsequent retires will be more succesful +*/ +device_set_auto_connect (device, FALSE); +if (device->auth_failures <AUTH_FAILURES_THRESHOLD ) { + device->auth_retry_timer =timeout_add_seconds (1 << device->auth_failures, + device_auth_failure_retry, + device,NULL ); +DBG ("Auth failure, retrying in %d seconds" ,1 << device->auth_failures); + device->auth_failures++; + }else { +DBG ("Disabling auto connect due to too many auth failures" ); + } + } +device_disconnected (device, reason); g_dbus_emit_property_changed(dbus_conn, device->path,
@@ -5408,6 +5442,11 @@ void device_remove(struct btd_device *device, gboolean remove_stored) {DBG ("Removing device %s" , device->path); +if (device->auth_retry_timer) { + device->auth_retry_timer =0 ; +timeout_remove (device->auth_retry_timer); + } +if (device->auto_connect) { device->disable_auto_connect = TRUE;device_set_auto_connect (device, FALSE);
--
2.43.0