fix(wireless): raise when a uci step of an ACL or channel push is refused
driver / standard (pull_request) Successful in 24s
driver / lint (pull_request) Successful in 24s
driver / types (pull_request) Successful in 1m28s
driver / test (3.13) (pull_request) Successful in 1m12s
driver / test (3.12) (pull_request) Successful in 1m21s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 2m38s
driver / publish (pull_request) Skipped

push_mac_acl and push_radio_channel sent each step through the
interactive shell and never looked at the result: a refused uci set,
add_list, commit or wifi reload passed silently, an SSID no wifi-iface
carried did nothing, and wifi was restarted after a refused channel.

Each step now runs on an SSH exec channel of its own, as root, with its
exit status checked (uci_steps.UciStepsMixin). A refused step raises
CommandErrorException naming the command and what the router said; a
refusal before the commit reverts what was staged, so a later commit
does not apply half of it, and wifi is not restarted. push_mac_acl
reads the sections once, raises when none carries the SSID, and deletes
macfilter/maclist only where a section has them instead of hiding each
delete's status behind `|| true`.

Closes #35
This commit is contained in:
Christian Manivong committed 2026-11-12 19:01:07 +01:00
1 parent 522720f818
commit 0552663d53
6 files changed
+516 -164

No files matched your search

+55 -21
View File
@@ -16,10 +16,38 @@ import logging
import re
from typing import Any
from napalm.base.exceptions import CommandErrorException
from napalm_openwrt.uci_steps import UciSection, UciStepsMixin, parse_uci_show, uci
logger = logging.getLogger(__name__)
_WIRELESS = "wireless"
_SHOW_WIRELESS = "uci -q show wireless"
#: Re-applies the config; enough for a MAC filter.
_WIFI_RELOAD = "wifi reload"
#: A full down/up of every radio; a channel needs it (see ``push_radio_channel``).
_WIFI_RESTART = "wifi"
_ACL_OPTIONS = ("macfilter", "maclist")
class OpenWrtWirelessMixin:
def _mac_acl_changes(sec: UciSection, mode: str, macs: list[str]) -> list[str]:
"""The ``uci`` changes that give *sec* the ACL *mode* with *macs*.
"off" deletes both options; there is no value for it. Otherwise the list is
rebuilt: deleted where the section has one, then each MAC added.
"""
present = [opt for opt in _ACL_OPTIONS if opt in sec.options]
if mode == "off":
return [uci("delete", f"{sec.ref}.{opt}") for opt in present]
changes = [uci("set", f"{sec.ref}.macfilter={'allow' if mode == 'whitelist' else 'deny'}")]
if "maclist" in present:
changes.append(uci("delete", f"{sec.ref}.maclist"))
changes += [uci("add_list", f"{sec.ref}.maclist={mac}") for mac in macs]
return changes
class OpenWrtWirelessMixin(UciStepsMixin):
"""Mixin providing wireless/radio NAPALM getters."""
def get_ssids(self) -> dict[str, Any]:
@@ -569,13 +597,17 @@ class OpenWrtWirelessMixin:
the physical channel on many hardware/driver combinations; a full
restart is required for channel changes to take effect.
Each step's exit status is checked (:mod:`napalm_openwrt.uci_steps`): a
refused ``uci set`` (a radio that does not exist) raises, and wifi is not
restarted for it.
:param radio: UCI radio name, e.g. ``"radio0"``.
:param channel: Channel number to set; ``0`` sets UCI ``auto``.
:raises CommandErrorException: when a step is refused, naming it.
"""
ch_val = "auto" if channel == 0 else str(channel)
self._send_command(f"uci set wireless.{radio}.channel={ch_val}")
self._send_command("uci commit wireless")
self._send_command("wifi")
change = uci("set", f"{_WIRELESS}.{radio}.channel={ch_val}")
self._uci_apply(_WIRELESS, [change], _WIFI_RESTART)
def push_mac_acl(self, ssid_name: str, mode: str, macs: list[str]) -> None:
"""Rewrite macfilter mode + maclist entries on every wifi-iface matching *ssid_name*.
@@ -592,31 +624,33 @@ class OpenWrtWirelessMixin:
A whitelist ("allow") with zero MACs blocks every client outright, so
it is treated as equivalent to "off" instead of being pushed as-is.
Each step's exit status is checked (:mod:`napalm_openwrt.uci_steps`): a
refused step raises naming itself, what was staged is reverted, and
nothing is committed or reloaded. An option is deleted only where the
section has it, since deleting a missing one is refused too.
:param ssid_name: SSID name to match against ``option ssid`` on each wifi-iface section.
:param mode: ``"off"`` | ``"whitelist"`` | ``"blacklist"``.
:param macs: MAC addresses to set as the maclist. Only the entries for the
active mode's list are ever passed in — the caller resolves whitelist
vs. blacklist before calling.
:raises CommandErrorException: when no wifi-iface carries *ssid_name*, or
when a step is refused.
"""
effective_mode = "off" if (mode == "whitelist" and not macs) else mode
sections = self._send_command(
"uci show wireless | grep -oE '^wireless\\.[^.]+' | sort -u"
).split()
sections = [
sec
for sec in parse_uci_show(self._uci_step(_SHOW_WIRELESS))
if sec.type == "wifi-iface" and sec.options.get("ssid") == [ssid_name]
]
if not sections:
raise CommandErrorException(
f"{self.hostname}: no wifi-iface carries the SSID {ssid_name!r}"
)
changes: list[str] = []
for sec in sections:
ssid_val = self._send_command(f"uci -q get {sec}.ssid 2>/dev/null || true").strip()
if ssid_val != ssid_name:
continue
if effective_mode == "off":
self._send_command(f"uci -q delete {sec}.macfilter || true")
self._send_command(f"uci -q delete {sec}.maclist || true")
continue
uci_mode = "allow" if effective_mode == "whitelist" else "deny"
self._send_command(f"uci set {sec}.macfilter='{uci_mode}'")
self._send_command(f"uci delete {sec}.maclist 2>/dev/null || true")
for mac in macs:
self._send_command(f"uci add_list {sec}.maclist='{mac}'")
self._send_command("uci commit wireless")
self._send_command("wifi reload")
changes += _mac_acl_changes(sec, effective_mode, macs)
self._uci_apply(_WIRELESS, changes, _WIFI_RELOAD)
def get_radio_status(self) -> dict[str, Any]:
"""Return radio status from UCI and iwinfo.