Skip to contentSkip to conversation

station: Fix autoconnect loops

PATCH In review Andrew Zaborowski 3 participants 9 messages May 10, 2021 – May 11, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 station: Fix autoconnect loops

Make sure we process the result of a connect attempt both in a D-Bus triggered connection and during autoconnect. Until now we'd only call station_connect_cb() on NETDEV_EVENT_DISCONNECT_BY_{AP,SME} if station->connect_pending was non-NULL, i.e. only in the D-Bus method. As a result we were never blacklisting BSSes and never calling network_connect_failed() (to set ask_passphrase) during autoconnect.

Use station->netdev_connected to keep track of whether the event actually happens in the handshake (as opposed to netconfig for example) to avoid calling network_connect_failed() with the "in_handshake" parameter set to true. Arguably we might want to call netdev_connect_failed() or at least temporarily blacklist/downrank the bss if the connection breaks during netconfig but I kept the current logic in this commit.

We might also want to call station_reassociate_cb() if we're in a reassociation but this wouldn't currently make much difference (and we don't seem to have any flag to know that we're in a reassociation right now.)

---
 src/station.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
src/station.c +6 -1
index 479f81f5..e503f636 100644
@@ -113,6 +113,7 @@ struct station {
 	bool ap_directed_roaming : 1;
 	bool scanning : 1;
 	bool autoconnect : 1;
+	bool netdev_connected : 1;
 };
 
 struct anqp_entry {
@@ -1339,6 +1340,7 @@ static void station_reset_connection_state(struct station *station)
 	l_queue_insert(station->networks_sorted, station->connected_network,
 				network_rank_compare, NULL);
 
+	station->netdev_connected = false;
 	station->connected_bss = NULL;
 	station->connected_network = NULL;
 
@@ -1370,7 +1372,9 @@ static void station_disconnect_event(struct station *station, void *event_data)
 {
 	l_debug("%u", netdev_get_ifindex(station->netdev));
 
-	if (station->connect_pending)
+	if (station->connect_pending ||
+			(station->state == STATION_STATE_CONNECTING &&
+			 !station->netdev_connected))
 		station_connect_cb(station->netdev,
 					NETDEV_RESULT_HANDSHAKE_FAILED,
 					event_data, station);
@@ -2554,6 +2558,7 @@ static void station_connect_cb(struct netdev *netdev, enum netdev_result result,
 			l_warn("Could not request neighbor report");
 	}
 
+	station->netdev_connected = true;
 	network_connected(station->connected_network);
 
 	if (station->netconfig)
Signature · 1 line
2.27.0
#1 lore raw

PATCH 2/2 netdev: Pass a reason code with NETDEV_EVENT_DISCONNECT_BY_*

Station callbacks expect a reason code (as opposed to status codes) with this event type. This shouldn't matter a lot because station_disconnect_event() only actually looks at the value during connection establishment so usually the "result" parameter is going to be HANDSHAKE_FAILED meaning that we'd already be getting a reason code in status_or_reason, but there could be corner cases where we got a different "result".

---
 src/netdev.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
src/netdev.c +6 -1
index 4fbe813a..6c063df5 100644
@@ -771,10 +771,15 @@ static void netdev_connect_failed(struct netdev *netdev,
 
 	if (connect_cb)
 		connect_cb(netdev, result, &status_or_reason, connect_data);
-	else if (event_filter)
+	else if (event_filter) {
+		/* NETDEV_EVENT_DISCONNECT_BY_SME expects a reason code */
+		if (result != NETDEV_RESULT_HANDSHAKE_FAILED)
+			status_or_reason = MMPDU_REASON_CODE_UNSPECIFIED;
+
 		event_filter(netdev, NETDEV_EVENT_DISCONNECT_BY_SME,
 				&status_or_reason,
 				connect_data);
+	}
 }
 
 static void netdev_disconnect_cb(struct l_genl_msg *msg, void *user_data)
Signature · 1 line
2.27.0
#2 lore raw

Re: [PATCH 2/2] netdev: Pass a reason code with NETDEV_EVENT_DISCONNECT_BY_*

Hi Andrew,

Quoted 3 lines · Andrew Zaborowski

Station callbacks expect a reason code (as opposed to status codes) with this event type. This shouldn't matter a lot because station_disconnect_event() only actually looks at the value during

I'm not sure why station is written this way. I'm pretty sure that calling station_disconnect_event() for NETDEV_EVENT_DISCONNECT_BY_SME() is the wrong thing to do.

Quoted 4 lines

connection establishment so usually the "result" parameter is going to be HANDSHAKE_FAILED meaning that we'd already be getting a reason code in status_or_reason, but there could be corner cases where we got a different "result".

That is correct for AP disconnections during handshaking, but that is signaled by NETDEV_EVENT_DISCONNECT_BY_AP...

Quoted 9 lines
---
  src/netdev.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
src/netdev.c +0 -0
index 4fbe813a..6c063df5 100644

src/netdev.c static void netdev_connect_failed(struct netdev *netdev, · line 778

Quoted 9 lines
src/netdev.c +5 -1
@@ -771,10 +771,15 @@ static void netdev_connect_failed(struct netdev *netdev,
 
  	if (connect_cb)
  		connect_cb(netdev, result, &status_or_reason, connect_data);
-	else if (event_filter)
+	else if (event_filter) {
+		/* NETDEV_EVENT_DISCONNECT_BY_SME expects a reason code */
+		if (result != NETDEV_RESULT_HANDSHAKE_FAILED)
+			status_or_reason = MMPDU_REASON_CODE_UNSPECIFIED;
+

From what I remember, the only time we can get here is if a rekey somehow
fails, or setting keys during a rekey fails. Are there other cases?

src/netdev.c static void netdev_connect_failed(struct netdev *netdev, · line 785

Quoted 8 lines
src/netdev.c +1 -0
  		event_filter(netdev, NETDEV_EVENT_DISCONNECT_BY_SME,
  				&status_or_reason,
  				connect_data);
+	}
  }
 
  static void netdev_disconnect_cb(struct l_genl_msg *msg, void *user_data)

Anyway, I applied this with a modified commit description.

Regards,
-Denis

#3 lore raw

Re: [PATCH 2/2] netdev: Pass a reason code with NETDEV_EVENT_DISCONNECT_BY_*

Quoted 8 lines · Denis Kenzior
Quoted 3 lines · Andrew Zaborowski

Station callbacks expect a reason code (as opposed to status codes) with this event type. This shouldn't matter a lot because station_disconnect_event() only actually looks at the value during

I'm not sure why station is written this way. I'm pretty sure that calling station_disconnect_event() for NETDEV_EVENT_DISCONNECT_BY_SME() is the wrong thing to do.

That is currently the only use of that function though.

Quoted 19 lines
Quoted 5 lines

connection establishment so usually the "result" parameter is going to be HANDSHAKE_FAILED meaning that we'd already be getting a reason code in status_or_reason, but there could be corner cases where we got a different "result".

That is correct for AP disconnections during handshaking, but that is signaled by NETDEV_EVENT_DISCONNECT_BY_AP...

Quoted 10 lines
---
  src/netdev.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
src/netdev.c +0 -0
index 4fbe813a..6c063df5 100644
@@ -771,10 +771,15 @@ static void netdev_connect_failed(struct netdev *netdev,
 
Quoted 11 lines

src/netdev.c static void netdev_connect_failed(struct netdev *netdev, · line 778

Quoted 8 lines
src/netdev.c +5 -1
      if (connect_cb)
              connect_cb(netdev, result, &status_or_reason, connect_data);
-     else if (event_filter)
+     else if (event_filter) {
+             /* NETDEV_EVENT_DISCONNECT_BY_SME expects a reason code */
+             if (result != NETDEV_RESULT_HANDSHAKE_FAILED)
+                     status_or_reason = MMPDU_REASON_CODE_UNSPECIFIED;
+

From what I remember, the only time we can get here is if a rekey somehow
fails, or setting keys during a rekey fails. Are there other cases?

In theory you can pass a null connect_cb to netdev_connect_common(), but in practice we don't do this so you could trigger this only for:

NETDEV_RESULT_HANDSHAKE_FAILED,
NETDEV_RESULT_KEY_SETTING_FAILED,
NETDEV_RESULT_ABORTED.

I realize I was confused, I thought that netdev_connect_failed() was actually sending NETDEV_EVENT_DISCONNECT_BY_SME to station and triggering my autoconnect loop, but actually it was netdev_disconnect_event() which always calls netdev->event_filter and never uses netdev->connect_cb. Perhaps netdev_disconnect_event() should use netdev_connect_failed() instead.

Quoted 11 lines

src/netdev.c static void netdev_connect_failed(struct netdev *netdev, · line 785

Quoted 9 lines
src/netdev.c +1 -0
              event_filter(netdev, NETDEV_EVENT_DISCONNECT_BY_SME,
                              &status_or_reason,
                              connect_data);
+     }
  }
 
  static void netdev_disconnect_cb(struct l_genl_msg *msg, void *user_data)

Anyway, I applied this with a modified commit description.

Thanks.

Best regards

#4 lore raw

Re: [PATCH 2/2] netdev: Pass a reason code with NETDEV_EVENT_DISCONNECT_BY_*

Quoted 11 lines · Andrew Zaborowski
Quoted 8 lines · Denis Kenzior
Quoted 3 lines · Andrew Zaborowski

Station callbacks expect a reason code (as opposed to status codes) with this event type. This shouldn't matter a lot because station_disconnect_event() only actually looks at the value during

I'm not sure why station is written this way. I'm pretty sure that calling station_disconnect_event() for NETDEV_EVENT_DISCONNECT_BY_SME() is the wrong thing to do.

That is currently the only use of that function though.

No, we have DISCONNECT_BY_AP and DISCONNECT_BY_SME. DISCONNECT_BY_SME should pretty much imply station_disassociated() should be called since we should never receive this event when we're in the connecting state.

Receiving DISCONNECT_BY_AP needs to be treated differently as well, depending on whether we're connected or connecting, regardless of connect_pending state (i.e. during autoconnect). Or have netdev.c invoke connect_cb if we're still connecting instead of sending this event.

Quoted 32 lines
Quoted 20 lines
Quoted 6 lines

connection establishment so usually the "result" parameter is going to be HANDSHAKE_FAILED meaning that we'd already be getting a reason code in status_or_reason, but there could be corner cases where we got a different "result".

That is correct for AP disconnections during handshaking, but that is signaled by NETDEV_EVENT_DISCONNECT_BY_AP...

Quoted 10 lines
---
   src/netdev.c | 7 ++++++-
   1 file changed, 6 insertions(+), 1 deletion(-)
src/netdev.c +0 -0
index 4fbe813a..6c063df5 100644
@@ -771,10 +771,15 @@ static void netdev_connect_failed(struct netdev *netdev,
 
Quoted 11 lines

src/netdev.c static void netdev_connect_failed(struct netdev *netdev, · line 778

Quoted 8 lines
src/netdev.c +5 -1
       if (connect_cb)
               connect_cb(netdev, result, &status_or_reason, connect_data);
-     else if (event_filter)
+     else if (event_filter) {
+             /* NETDEV_EVENT_DISCONNECT_BY_SME expects a reason code */
+             if (result != NETDEV_RESULT_HANDSHAKE_FAILED)
+                     status_or_reason = MMPDU_REASON_CODE_UNSPECIFIED;
+

From what I remember, the only time we can get here is if a rekey somehow
fails, or setting keys during a rekey fails. Are there other cases?

Quoted 1 line

In theory you can pass a null connect_cb to netdev_connect_common(),

Yes, so either netdev should handle this specially, or we should make it an error condition during netdev_connect().

Quoted 5 lines

but in practice we don't do this so you could trigger this only for:

NETDEV_RESULT_HANDSHAKE_FAILED,
NETDEV_RESULT_KEY_SETTING_FAILED,
NETDEV_RESULT_ABORTED.

NETDEV_ABORTED should only be invoked in the connecting phase, no? I suppose there may be some bizarre situations, like we're rekeying and the device goes down.

Quoted 5 lines

I realize I was confused, I thought that netdev_connect_failed() was actually sending NETDEV_EVENT_DISCONNECT_BY_SME to station and triggering my autoconnect loop, but actually it was netdev_disconnect_event() which always calls netdev->event_filter and

This confirms my suspicions that we're not handling DISCONNECT_BY_AP completely correctly.

Quoted 2 lines

never uses netdev->connect_cb. Perhaps netdev_disconnect_event() should use netdev_connect_failed() instead.

They're different conditions though. DISCONNECT_BY_AP tells us the AP sent a Deauthenticate. DISCONNECT_BY_SME says that we did (actual or implied).

Regards,
-Denis

#5 lore raw

Re: [PATCH 2/2] netdev: Pass a reason code with NETDEV_EVENT_DISCONNECT_BY_*

Quoted 11 lines · Denis Kenzior
Quoted 10 lines · Andrew Zaborowski
Quoted 8 lines · Denis Kenzior
Quoted 3 lines · Andrew Zaborowski

Station callbacks expect a reason code (as opposed to status codes) with this event type. This shouldn't matter a lot because station_disconnect_event() only actually looks at the value during

I'm not sure why station is written this way. I'm pretty sure that calling station_disconnect_event() for NETDEV_EVENT_DISCONNECT_BY_SME() is the wrong thing to do.

Quoted 5 lines
Quoted 1 line

That is currently the only use of that function though.

No, we have DISCONNECT_BY_AP and DISCONNECT_BY_SME. DISCONNECT_BY_SME should pretty much imply station_disassociated() should be called since we should never receive this event when we're in the connecting state.

I wouldn't rely on that, if the kernel doesn't pass NL80211_ATTR_DISCONNECTED_BY_AP it could mean that the AP sent something that triggered the kernel to disconnect (possibly before handshake complete). For us we mainly care that we can't use this BSS and should try a different one next time.

Best regards

#6 lore raw

Re: [PATCH 2/2] netdev: Pass a reason code with NETDEV_EVENT_DISCONNECT_BY_*

Quoted 14 lines · Andrew Zaborowski
Quoted 13 lines · Denis Kenzior
Quoted 11 lines · Andrew Zaborowski
Quoted 8 lines · Denis Kenzior
Quoted 3 lines · Andrew Zaborowski

Station callbacks expect a reason code (as opposed to status codes) with this event type. This shouldn't matter a lot because station_disconnect_event() only actually looks at the value during

I'm not sure why station is written this way. I'm pretty sure that calling station_disconnect_event() for NETDEV_EVENT_DISCONNECT_BY_SME() is the wrong thing to do.

That is currently the only use of that function though.

Quoted 7 lines
Quoted 3 lines

No, we have DISCONNECT_BY_AP and DISCONNECT_BY_SME. DISCONNECT_BY_SME should pretty much imply station_disassociated() should be called since we should never receive this event when we're in the connecting state.

I wouldn't rely on that, if the kernel doesn't pass NL80211_ATTR_DISCONNECTED_BY_AP it could mean that the AP sent something that triggered the kernel to disconnect (possibly before

I suppose it might be possible, but wouldn't you treat it as a connect failed and not a disconnect_by_sme? That is what we do if Auth/Assoc times out for example.

Quoted 2 lines

handshake complete). For us we mainly care that we can't use this BSS and should try a different one next time.

But we need to make a best attempt to guess as to why it failed.

From what I gather, in this particular case, the AP just sends a Deauth after
PTK2/4 packet doesn't pass the MIC check.

Regards,
-Denis

#7 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 7 lines · Andrew Zaborowski

Make sure we process the result of a connect attempt both in a D-Bus triggered connection and during autoconnect.  Until now we'd only call
station_connect_cb() on NETDEV_EVENT_DISCONNECT_BY_{AP,SME} if station->connect_pending was non-NULL, i.e. only in the D-Bus method. As a result we were never blacklisting BSSes and never calling network_connect_failed() (to set ask_passphrase) during autoconnect.

Is the purpose of this to avoid re-trying the same network over and over? Relying on network_autoconnect() to fail due to ask_passphrase being true?

Seems like it would be better to re-work autoconnect to actually pop the list when autoconnect fails, and immediately move onto the next (no scanning).

Quoted 65 lines

Use station->netdev_connected to keep track of whether the event actually happens in the handshake (as opposed to netconfig for example)
to avoid calling network_connect_failed() with the "in_handshake" parameter set to true.  Arguably we might want to call netdev_connect_failed() or at least temporarily blacklist/downrank the bss if the connection breaks during netconfig but I kept the current logic in this commit.

We might also want to call station_reassociate_cb() if we're in a reassociation but this wouldn't currently make much difference (and we
don't seem to have any flag to know that we're in a reassociation right
now.)
---
 src/station.c | 7 ++++++-

 1 file changed, 6 insertions(+), 1 deletion(-)
src/station.c +1 -0
index 479f81f5..e503f636 100644
@@ -113,6 +113,7 @@ struct station {
         bool ap_directed_roaming : 1;
         bool scanning : 1;
         bool autoconnect : 1;
+       bool netdev_connected : 1;
  };


 struct anqp_entry {

src/station.c +0 -0
@@ -1339,6 +1340,7 @@ static void
 station_reset_connection_state(struct station *station)

        l_queue_insert(station->networks_sorted, station-

Quoted 1 line

connected_network,

                                network_rank_compare, NULL);
+       station->netdev_connected = false; station->connected_bss = NULL; station->connected_network = NULL;

src/station.c +0 -0
@@ -1370,7 +1372,9 @@ static void station_disconnect_event(struct
 station *station, void *event_data)
  {
         l_debug("%u", netdev_get_ifindex(station->netdev));


-       if (station->connect_pending)
+       if (station->connect_pending ||
+                       (station->state == STATION_STATE_CONNECTING &&
+                        !station->netdev_connected)) station_connect_cb(station->netdev, NETDEV_RESULT_HANDSHAKE_FAILE D,
                                        event_data, station);

src/station.c +0 -0
@@ -2554,6 +2558,7 @@ static void station_connect_cb(struct netdev
 *netdev, enum netdev_result result,
                         l_warn("Could not request neighbor report");
         }


+       station->netdev_connected = true; network_connected(station->connected_network);
        if (station->netconfig)

#8 lore raw

Hi James,

Quoted 9 lines · James Prestwood
Quoted 7 lines · Andrew Zaborowski

Make sure we process the result of a connect attempt both in a D-Bus triggered connection and during autoconnect. Until now we'd only call
station_connect_cb() on NETDEV_EVENT_DISCONNECT_BY_{AP,SME} if station->connect_pending was non-NULL, i.e. only in the D-Bus method. As a result we were never blacklisting BSSes and never calling network_connect_failed() (to set ask_passphrase) during autoconnect.

Quoted 3 lines

Is the purpose of this to avoid re-trying the same network over and over? Relying on network_autoconnect() to fail due to ask_passphrase being true?

Either this, or getting the BSS blacklisted. Oherwise autoconnect is working around the blacklist logic and has no chance to try another BSS or another network.

I don't think the current blacklist and ask_passphrase logic is very good, but it could be misleading that station_connect_cb is called only in D-Bus triggered connections. station_connect_cb is already handling NETDEV_RESULT_HANDSHAKE_FAILED for both manual- and auto-connect, it should also handle NETDEV_EVENT_DISCONNECT_BY_* for both cases. Perhaps station_connect_cb should then implement a better policy that looks at whether we're in autoconnect or not, and handles NETDEV_RESULT_HANDSHAKE_FAILED and NETDEV_EVENT_DISCONNECT_BY_* in the same way.

Quoted 4 lines

Seems like it would be better to re-work autoconnect to actually pop the list when autoconnect fails, and immediately move onto the next (no scanning).

Yep, using autoconnect_list would at least give justification for having a list, as it is now we could just as well select a single network/bss (if ask_passphrase is true there's no point adding it to the list in the first place).

Ideally I think this should happen:

* the blacklist can be dropped completely,

* a connection failure at a given timestamp causes the BSS rank and the network's rank to be penalized and recover to its normal value with time, independent of auto- vs. manual- connect.

* ask_password should be set based on the failure reason, either always or only for manual connect (IIRC Denis thinks it should be only for manual connect)

Best regards