# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """The wireless parts of an AP configuration: ``wireless//``. Each writes every wireless section that carries the SSID. A keyed mode is never written without its key: the SSID would go off the air, and ``none`` instead would put it on the air unprotected; such a part is left unwritten. """ from __future__ import annotations from collections.abc import Callable, Mapping from napalm_device_types import ApConfigDict, ApSsidConfigDict from napalm_openwrt.ap_config._common import ( Plan, Radios, keyed_without_key, sq, ssid_loop, ssid_of, ) _ALL_BANDS = ["2.4 GHz", "5 GHz"] def _net(ssid: ApSsidConfigDict) -> str: return f"ap_vlan{ssid['vlan_id']}" def _vlan(ssid: ApSsidConfigDict, config: ApConfigDict, radios: Radios, plan: Plan) -> None: if not ssid.get("vlan_id"): return plan.commands.append(ssid_loop(ssid["name"], [f"uci set $_s.network={sq(_net(ssid))}"])) plan.commits.add("wireless") def _new_iface(ssid: ApSsidConfigDict, radio: str, indent: str = "") -> list[str]: """A new wifi-iface on *radio* for *ssid*, in its VLAN, with its key and hidden flag.""" enc = ssid.get("encryption") or "none" lines = [ "uci add wireless wifi-iface", f"uci set wireless.@wifi-iface[-1].device={sq(radio)}", "uci set wireless.@wifi-iface[-1].mode='ap'", f"uci set wireless.@wifi-iface[-1].ssid={sq(ssid['name'])}", f"uci set wireless.@wifi-iface[-1].network={sq(_net(ssid))}", f"uci set wireless.@wifi-iface[-1].encryption={sq(enc)}", ] if ssid.get("key") and enc not in ("none", ""): lines.append(f"uci set wireless.@wifi-iface[-1].key={sq(ssid['key'])}") if ssid.get("hidden"): lines.append("uci set wireless.@wifi-iface[-1].hidden='1'") return [f"{indent}{line}" for line in lines] def _wanted_radios(ssid: ApSsidConfigDict, radios: Radios) -> list[str]: bands = ssid.get("bands") or _ALL_BANDS wanted = [] if any("2.4" in b for b in bands) and radios.r2g: wanted.append(radios.r2g) if any("5" in b and "2.4" not in b for b in bands) and radios.r5g: wanted.append(radios.r5g) return wanted def _bands(ssid: ApSsidConfigDict, config: ApConfigDict, radios: Radios, plan: Plan) -> None: """The SSID on the radios of its bands: taken off the others, added where missing.""" if not ssid.get("vlan_id") or keyed_without_key(ssid): return name = ssid["name"] wanted = _wanted_radios(ssid, radios) for radio in (r for r in radios.all if r not in wanted): plan.commands.append( ssid_loop( name, [ f'[ "$(uci -q get $_s.device 2>/dev/null)" = {sq(radio)} ] || continue', "uci delete $_s", ], ) ) for radio in wanted: plan.commands.append( "\n".join( [ "_found=0", ssid_loop( name, [ f'[ "$(uci -q get $_s.device 2>/dev/null)" = {sq(radio)} ] || continue', "_found=1; break", ], ), "if [ $_found = 0 ]; then", *_new_iface(ssid, radio, indent=" "), "fi", ] ) ) plan.commits.add("wireless") def _missing(ssid: ApSsidConfigDict, config: ApConfigDict, radios: Radios, plan: Plan) -> None: """A wifi-iface for each band of an SSID the AP does not serve at all.""" if not ssid.get("vlan_id") or keyed_without_key(ssid): return for band in ssid.get("bands") or _ALL_BANDS: radio = radios.r2g if "2.4" in band else radios.r5g if radio: plan.commands += _new_iface(ssid, radio) plan.commits.add("wireless") def _encryption(ssid: ApSsidConfigDict, config: ApConfigDict, radios: Radios, plan: Plan) -> None: if keyed_without_key(ssid): return enc = ssid.get("encryption") or "none" body = [f"uci set $_s.encryption={sq(enc)}"] if ssid.get("key") and enc not in ("none", ""): body.append(f"uci set $_s.key={sq(ssid['key'])}") plan.commands.append(ssid_loop(ssid["name"], body)) plan.commits.add("wireless") def _passphrase(ssid: ApSsidConfigDict, config: ApConfigDict, radios: Radios, plan: Plan) -> None: """The key alone, for an SSID whose mode is right and whose key is not.""" if not ssid.get("key") or (ssid.get("encryption") or "none") == "none": return plan.commands.append(ssid_loop(ssid["name"], [f"uci set $_s.key={sq(ssid['key'])}"])) plan.commits.add("wireless") def _flag(option: str, field: str, invert: bool = False) -> _Builder: """A boolean of the SSID as a ``0``/``1`` option; *invert* for ``disabled``.""" def build(ssid: ApSsidConfigDict, config: ApConfigDict, radios: Radios, plan: Plan) -> None: values: Mapping[str, object] = ssid if values.get(field) is None: return on = bool(values[field]) != invert plan.commands.append(ssid_loop(ssid["name"], [f"uci set $_s.{option}={sq(int(on))}"])) plan.commits.add("wireless") return build def _max_clients(ssid: ApSsidConfigDict, config: ApConfigDict, radios: Radios, plan: Plan) -> None: if "max_clients" not in ssid: return limit = ssid.get("max_clients") line = ( f"uci set $_s.maxassoc={sq(limit)}" if limit is not None else "uci -q delete $_s.maxassoc 2>/dev/null; true" ) plan.commands.append(ssid_loop(ssid["name"], [line])) plan.commits.add("wireless") def _max_inactivity( ssid: ApSsidConfigDict, config: ApConfigDict, radios: Radios, plan: Plan ) -> None: if ssid.get("max_inactivity") is None: return seconds = ssid.get("max_inactivity") plan.commands.append(ssid_loop(ssid["name"], [f"uci set $_s.max_inactivity={sq(seconds)}"])) plan.commits.add("wireless") def _acl(ssid: ApSsidConfigDict, config: ApConfigDict, radios: Radios, plan: Plan) -> None: """The MAC filter and its list, rebuilt whole. The wifi-scripts validator takes no ``macfilter`` but ``allow`` and ``deny``: ``disable`` puts netifd in a restart loop with the radios down. Off is the option left out. A whitelist without MACs would lock every client out, so it is written as off. """ mode = ssid.get("acl_mode") or "off" macs = sorted(set(ssid.get("acl_macs") or [])) if mode != "off" else [] if mode == "whitelist" and not macs: mode = "off" if mode == "off": body = ["uci -q delete $_s.macfilter", "uci -q delete $_s.maclist"] else: body = [ f"uci set $_s.macfilter={sq('allow' if mode == 'whitelist' else 'deny')}", "uci -q delete $_s.maclist 2>/dev/null", *(f"uci add_list $_s.maclist={sq(mac)}" for mac in macs), ] plan.commands.append(ssid_loop(ssid["name"], body)) plan.commits.add("wireless") def _ft(ssid: ApSsidConfigDict, config: ApConfigDict, radios: Radios, plan: Plan) -> None: """802.11r on, in the configuration's mobility domain; or off, which needs none.""" if not ssid.get("fast_transition"): body = [ "uci -q delete $_s.ieee80211r", "uci -q delete $_s.mobility_domain", "uci -q delete $_s.ft_over_ds", "uci -q delete $_s.ft_psk_generate_local", ] elif config.get("mobility_domain"): over_ds = "1" if config.get("ft_over_ds", True) else "0" body = [ "uci set $_s.ieee80211r='1'", f"uci set $_s.mobility_domain={sq(config['mobility_domain'])}", f"uci set $_s.ft_over_ds={sq(over_ds)}", ] else: return plan.commands.append(ssid_loop(ssid["name"], body)) plan.commits.add("wireless") _Builder = Callable[[ApSsidConfigDict, ApConfigDict, Radios, Plan], None] _SSID_PARTS: dict[str, _Builder] = { "vlan": _vlan, "bands": _bands, "missing": _missing, "encryption": _encryption, "passphrase": _passphrase, "hidden": _flag("hidden", "hidden"), "enabled": _flag("disabled", "enabled", invert=True), "client_isolation": _flag("isolate", "client_isolation"), "disassoc_low_ack": _flag("disassoc_low_ack", "disassoc_low_ack"), "max_clients": _max_clients, "max_inactivity": _max_inactivity, "acl": _acl, "ft": _ft, } def wireless_part( ssid_name: str, attribute: str, config: ApConfigDict, radios: Radios, plan: Plan ) -> None: """The *attribute* of the SSID *ssid_name*; nothing for an SSID *config* lacks.""" ssid = ssid_of(config, ssid_name) build = _SSID_PARTS.get(attribute) if ssid is not None and build is not None: build(ssid, config, radios, plan)