diff --git a/rootfs/overlay/opt/mu300/bin/wifi-client b/rootfs/overlay/opt/mu300/bin/wifi-client index 30f1640f..0e6c81d8 100755 --- a/rootfs/overlay/opt/mu300/bin/wifi-client +++ b/rootfs/overlay/opt/mu300/bin/wifi-client @@ -85,7 +85,9 @@ case ${1:-} in [ -s "$LEASE" ] || exit 0 ;; bound|renew) [ "$(cat "$LEASE" 2>/dev/null)" = "$ip/${mask:-24}" ] && exit 0 - [ -s "$LEASE" ] || { apply_lease; exit 0; } ;; + # only the lease a join (or the event handler) is waiting for, marked by dhcp_run: any later one - also one + # after a lost lease - goes through lease_changed's checks + if [ -e "$LEASE.first" ]; then rm -f "$LEASE.first"; apply_lease; exit 0; fi ;; leasefail|nak) exit 0 ;; esac @@ -238,7 +240,8 @@ forwarding_off() { # state asks for; anything else - a rule missing, one added (an accept before the drops), another order - is not that # state, and counts as a failure, which ends closed or, if even that cannot be confirmed, with nothing routed to wlan0. fw_normal() { # the filter table as nft lists it (or as rules_text writes it), without indentation or counters - awk '{ sub(/^[ \t]+/, ""); sub(/[ \t]+$/, ""); gsub(/counter packets [0-9]+ bytes [0-9]+/, "counter") } + awk '{ sub(/^[ \t]+/, ""); sub(/[ \t]+$/, ""); gsub(/counter packets [0-9]+ bytes [0-9]+/, "counter") + sub(/limit rate 5\/second burst 5 packets/, "limit rate 5/second") } $0 == "table inet '"$FILTER"' {" { p = 1 } p && $0 != ""' } @@ -271,7 +274,10 @@ fail_closed() { put_rules closed && return 0 drop_lease forwarding_off - log "the firewall for $IFACE could not be confirmed (nft): its address and routes are gone, nothing is shared" + # and off the network: with no filter it confirms, wlan0 must not stay on it (mobile data may keep forwarding on, + # and IPv6 link-local needs no address) + leave_network + log "the firewall for $IFACE could not be confirmed (nft): it has left the network, nothing is shared" return 1 } @@ -381,7 +387,7 @@ wan_zone() { drop_lease() { [ -s "$LEASE.pid" ] && kill "$(cat "$LEASE.pid")" 2>/dev/null pkill -f "^(busybox )?udhcpc -i $IFACE " 2>/dev/null || true - rm -f "$LEASE" "$LEASE.pid" + rm -f "$LEASE" "$LEASE.pid" "$LEASE.first" ip route flush dev "$IFACE" 2>/dev/null || true ip -4 addr flush dev "$IFACE" 2>/dev/null || true ip -6 addr flush dev "$IFACE" scope global 2>/dev/null || true @@ -458,7 +464,7 @@ event() { # event CONNECTED|DISCONNECTED [ -e "$ACTIVE" ] && [ -s "$EVENTS_PID" ] && [ "$PPID" = "$(cat "$EVENTS_PID" 2>/dev/null)" ] || exit 0 case $1 in DISCONNECTED) - close_uplink || log "could not close the firewall for $IFACE (nft): its lease and routes go all the same" + fail_closed || exit 1 drop_lease forwarding_off log "link lost: $IFACE not shared until it is back" ;; @@ -490,14 +496,8 @@ lease_changed() { # lease_changed deconfig|bound|renew if command -v flock >/dev/null 2>&1; then exec 9>"$R/run/mu300-wifi-client.lock" && flock -n 9 || exit 0 fi - if ! close_uplink; then - # not even closed: the old address goes too (and the new one is not applied), so nothing is routed to wlan0 - ip -4 addr flush dev "$IFACE" 2>/dev/null - rm -f "$LEASE" - forwarding_off - log "could not close the firewall for $IFACE (nft): no address kept" - exit 1 - fi + # not even closed: no address kept (the new one is not applied) and the radio off the network + fail_closed || exit 1 if [ "$1" = deconfig ]; then ip -4 addr flush dev "$IFACE" 2>/dev/null rm -f "$LEASE" @@ -668,6 +668,12 @@ unescape() { } dhcp_run() { # Ubuntu ships busybox without the applet symlinks, so udhcpc may only exist as "busybox udhcpc" + : > "$LEASE.first" + dhcp_client; rc=$? + rm -f "$LEASE.first" + return "$rc" +} +dhcp_client() { if command -v udhcpc >/dev/null 2>&1; then udhcpc -i "$IFACE" -n -t 8 -s "$SELF" -p "$LEASE.pid" /dev/null 2>&1 3>&- 4>&- 5>&- 6>&- 7>&- 8>&- 9>&- elif command -v busybox >/dev/null 2>&1; then @@ -860,6 +866,7 @@ status() { shared) printf 'firewall shared with the LAN (internet only)\n' ;; closed) printf 'firewall closed (not shared)\n' ;; unknown) printf 'firewall %s is not as wifi-client puts it: wifi-client disconnect, or reconnect\n' "$FILTER" ;; + none) [ -e "$ACTIVE" ] && printf 'firewall OPEN: the client is up but %s is gone (nft flush?): wifi-client reconnect\n' "$FILTER" ;; esac [ "$fw" = none ] || [ -e "$ACTIVE" ] || printf 'firewall left over from a client that is gone: wifi-client disconnect\n' [ -e "$SCANMODE" ] && printf 'scan mode armed: the hotspot stays off at the next boot\n' diff --git a/tests/test_wifi_client.py b/tests/test_wifi_client.py index e6ae4064..873456c4 100644 --- a/tests/test_wifi_client.py +++ b/tests/test_wifi_client.py @@ -946,14 +946,18 @@ class WifiClient(ShellTest): self.assertEqual(self.applied(), [self.closed()]) self.assertFalse((self.tmp / 'addr').exists()) self.assertIn('ip_forward=0', self.events()) - # the first lease of a join is applied only: connect (or the event handler) checks it itself + # the first lease of a join (dhcp_run marks it) is applied only: connect (or the event handler) checks it self.fresh() + (self.root / 'run/mu300-wifi-client.dhcp.first').touch() r = self.lease(shell, 'bound') self.assertEqual(r.returncode, 0, r.stderr) self.assertEqual(self.rulesets(), []) self.assertTrue((self.tmp / 'addr').exists()) + self.assertFalse((self.root / 'run/mu300-wifi-client.dhcp.first').exists()) # and outside a session nothing at all happens to a renewal (self.tmp / 'addr').unlink() + self.assertEqual(self.lease(shell, 'bound').returncode, 0) + self.assertFalse((self.tmp / 'addr').exists()) self.assertEqual(self.lease(shell, 'deconfig').returncode, 0) self.assertEqual(self.rulesets(), []) @@ -1046,6 +1050,12 @@ class WifiClient(ShellTest): self.assertNotIn('ip daddr {', self.filt()) self.assertNotIn('ip_forward=1', self.events()) self.assertIn('closed (not shared)', self.run_wc(shell, 'status').stdout) + # a lying nft on a lost link: off the network + self.joined(shell) + (self.tmp / 'nftlie').touch() + (self.tmp / 'filter').unlink() + self.run_wc(shell, 'wlan0', 'DISCONNECTED') + self.assertFalse((self.tmp / 'supplicant').exists()) def test_a_join_whose_sharing_fails_ends_with_nothing(self): for shell in self.each_shell(): @@ -1094,12 +1104,37 @@ class WifiClient(ShellTest): (self.root / 'run/mu300-wifi-client.active').unlink() self.assertIn('left over from a client that is gone', self.run_wc(shell, 'status').stdout) + def test_a_lease_after_a_lost_one_is_checked(self): + # the lease lost and a new one later on the same link (no link event): checked, not just applied + for shell in self.each_shell(): + self.joined(shell) + (self.root / 'run/mu300-wifi-client.dhcp').write_text('192.168.2.248/24\n') + self.lease(shell, 'deconfig') + self.ev.unlink() + (self.tmp / 'subnet').write_text('192.168.79.0/24') + r = self.lease(shell, 'bound', ip='192.168.79.9') + self.assertEqual(self.applied(), [self.closed()]) + self.assertFalse((self.tmp / 'addr').exists()) + self.ev.unlink() + (self.tmp / 'subnet').write_text('203.0.113.0/24') + r = self.lease(shell, 'bound', ip='203.0.113.7') + self.assertEqual(r.returncode, 0, r.stderr) + self.assertEqual(self.applied(), [self.closed(), self.expected('203.0.113.0/24')]) + + def test_tables_flushed_under_a_running_client_show_as_open(self): + for shell in self.each_shell(): + self.joined(shell) + (self.tmp / 'filter').unlink() + self.assertIn('firewall OPEN', self.run_wc(shell, 'status').stdout) + def test_nft_failing_on_a_lost_link_or_lease_still_takes_the_address(self): for shell in self.each_shell(): self.joined(shell) (self.tmp / 'nftfail').touch() r = self.run_wc(shell, 'wlan0', 'DISCONNECTED') self.assertFalse((self.tmp / 'addr').exists()) + # and the radio leaves the network: unconfirmed means not on it + self.assertFalse((self.tmp / 'supplicant').exists()) self.assertIn('ip route flush dev wlan0', self.events()) self.assertIn('ip_forward=0', self.events()) self.joined(shell) @@ -1108,6 +1143,7 @@ class WifiClient(ShellTest): r = self.lease(shell, 'bound', ip='203.0.113.7') self.assertEqual(r.returncode, 1) self.assertFalse((self.tmp / 'addr').exists()) + self.assertFalse((self.tmp / 'supplicant').exists()) self.assertFalse((self.root / 'run/mu300-wifi-client.dhcp').exists()) self.assertIn('ip_forward=0', self.events())