feat(access-point): SSID settings, radio channels, VLAN names, the AP configuration and DHCP leases through declared methods (NetOrk/netork#695)
push_ssid_config, set_radio_channels and set_vlan_name run as checked uci steps (#35): a refused step raises naming itself and reverts what was staged. set_radio_channels replaces the undeclared push_radio_channel. apply_ap_config takes napalm-device-types' ApConfigDict and the parts the caller found different and writes them as one detached script: the builder netOrk had in _uci_builders.py, moved here. It answers which parts it wrote and which it could not, whether the AP restarts, and the script with every passphrase masked. A passphrase part now writes the key. get_dhcp_leases reads dnsmasq's lease file. Needs napalm-device-types 4.54.0; CI pre-installs it from its branch until it is on PyPI.
This commit is contained in:
1 parent
dd3e3f1543
commit
634bc9c793
18 files changed
+2108
-40
No files matched your search
@@ -0,0 +1,228 @@
|
||||
# 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 network parts of an AP configuration: the ``br-ap`` bridge and its VLANs.
|
||||
|
||||
The uplink ``eth0`` is a trunk; each VLAN is a ``bridge-vlan`` of ``br-ap``, and an
|
||||
SSID's clients reach theirs through an interface ``ap_vlan<vid>`` on ``br-ap.<vid>``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from napalm_device_types import ApConfigDict
|
||||
|
||||
from napalm_openwrt.ap_config._common import AP_UPLINK_PORT, Plan, sq
|
||||
|
||||
# Drop every bridge-vlan section on br-ap before writing our own. Sections may carry
|
||||
# any name -- a hand-built bridge commonly uses `apbr_vlanN` without the separator
|
||||
# used here -- so setting our names on top would leave the old sections in place and
|
||||
# define the same VLAN twice with conflicting port membership.
|
||||
_PURGE_BRIDGE_VLANS = "\n".join(
|
||||
[
|
||||
"n=0",
|
||||
"while uci -q get network.@bridge-vlan[$n] >/dev/null 2>&1; do",
|
||||
" if [ \"$(uci -q get network.@bridge-vlan[$n].device 2>/dev/null)\" = 'br-ap' ]; then",
|
||||
" uci delete network.@bridge-vlan[$n]",
|
||||
" else",
|
||||
" n=$((n+1))",
|
||||
" fi",
|
||||
"done; true",
|
||||
]
|
||||
)
|
||||
|
||||
# Every wireless AP interface onto the ap_vlan<vid> of the VLAN its network's device names.
|
||||
_ATTACH_APS = (
|
||||
"for iface in $(uci show wireless | grep -oE '^wireless\\.[^.]+' | sort -u); do "
|
||||
" mode=$(uci -q get $iface.mode 2>/dev/null); [ \"$mode\" != 'ap' ] && continue; "
|
||||
" net=$(uci -q get $iface.network 2>/dev/null); "
|
||||
" dev=$(uci -q get network.$net.device 2>/dev/null); "
|
||||
" vid=$(echo \"$dev\" | grep -oE '[0-9]+$'); "
|
||||
' [ -n "$vid" ] && uci set $iface.network="ap_vlan$vid"; '
|
||||
"done; true"
|
||||
)
|
||||
|
||||
# Every device section but br-ap goes.
|
||||
_DROP_OTHER_DEVICES = "\n".join(
|
||||
[
|
||||
"n=0",
|
||||
"while uci show network.@device[$n] >/dev/null 2>&1; do",
|
||||
" _nm=$(uci -q get network.@device[$n].name 2>/dev/null)",
|
||||
" if [ \"$_nm\" = 'br-ap' ]; then",
|
||||
" n=$((n+1))",
|
||||
" else",
|
||||
" uci delete network.@device[$n]",
|
||||
" fi",
|
||||
"done; true",
|
||||
]
|
||||
)
|
||||
|
||||
# The interfaces of the old bridges and 802.1q devices go.
|
||||
_DROP_OLD_INTERFACES = (
|
||||
"for iface in $(uci show network | grep -oE 'network\\.[a-z0-9_]+\\.device' | "
|
||||
"cut -d. -f2); do "
|
||||
" dev=$(uci -q get network.$iface.device 2>/dev/null); "
|
||||
" case $dev in br-wlan*|eth0.[0-9]*) uci delete network.$iface;; esac; "
|
||||
"done; true"
|
||||
)
|
||||
|
||||
|
||||
def _stp(config: ApConfigDict) -> str:
|
||||
return "1" if config.get("bridge_stp", True) else "0"
|
||||
|
||||
|
||||
def vlan_port_spec(config: ApConfigDict, vid: int) -> str:
|
||||
"""The uplink's membership in *vid*: ``u*`` (untagged, PVID) for the management
|
||||
VLAN when it is to be untagged -- only one VLAN of a port may be -- else ``t``."""
|
||||
if config.get("management_untagged") and vid == config.get("management_vlan_id"):
|
||||
return f"{AP_UPLINK_PORT}:u*"
|
||||
return f"{AP_UPLINK_PORT}:t"
|
||||
|
||||
|
||||
def stp_part(config: ApConfigDict, plan: Plan) -> None:
|
||||
"""``network/bridge_stp``: on the section whose ``name`` is br-ap, whatever its own
|
||||
name -- setting it on a section that does not exist would create a second one."""
|
||||
plan.commands.append(
|
||||
"\n".join(
|
||||
[
|
||||
"for _s in $(uci show network | grep -oE '^network\\.[a-z0-9_]+' "
|
||||
"| cut -d. -f2 | sort -u); do",
|
||||
" [ \"$(uci -q get network.$_s.name 2>/dev/null)\" = 'br-ap' ] || continue",
|
||||
f" uci set network.$_s.stp={sq(_stp(config))}",
|
||||
"done",
|
||||
]
|
||||
)
|
||||
)
|
||||
plan.commits.add("network")
|
||||
|
||||
|
||||
def _ssid_vlans(config: ApConfigDict) -> list[int]:
|
||||
return [vid for s in config.get("ssids") or [] if (vid := s.get("vlan_id"))]
|
||||
|
||||
|
||||
def _management_interface(mvid: int) -> str:
|
||||
return (
|
||||
f"_mgmt_sec=''; "
|
||||
f"for _sec in $(uci show network | grep -oE "
|
||||
f"'network\\.[a-z0-9_]+\\.device' | cut -d. -f2); do "
|
||||
f" _d=$(uci -q get network.$_sec.device 2>/dev/null); "
|
||||
f' [ "$_d" = \'eth0.{mvid}\' ] && _mgmt_sec="$_sec" && break; '
|
||||
f"done; "
|
||||
f'if [ -z "$_mgmt_sec" ]; then '
|
||||
f" _mgmt_sec='mgmt'; uci set network.mgmt=interface; "
|
||||
f" uci set network.mgmt.proto='dhcp'; "
|
||||
f"fi; "
|
||||
f"uci set network.$_mgmt_sec.device='br-ap.{mvid}'; "
|
||||
f"_proto=$(uci -q get network.$_mgmt_sec.proto 2>/dev/null); "
|
||||
f"if [ -z \"$_proto\" ]; then uci set network.$_mgmt_sec.proto='dhcp'; fi; "
|
||||
f"true"
|
||||
)
|
||||
|
||||
|
||||
def _ap_vlan_interface(vid: int) -> list[str]:
|
||||
return [
|
||||
f"uci set network.ap_vlan{vid}=interface",
|
||||
f"uci set network.ap_vlan{vid}.device='br-ap.{vid}'",
|
||||
f"uci set network.ap_vlan{vid}.proto='none'",
|
||||
]
|
||||
|
||||
|
||||
def _lan_zone(networks: list[str]) -> str:
|
||||
"""The management and SSID networks into the firewall zone ``lan``."""
|
||||
nets = " ".join(networks)
|
||||
return (
|
||||
f"for i in 0 1 2 3 4 5; do "
|
||||
f" zone=$(uci -q get firewall.@zone[$i].name 2>/dev/null); "
|
||||
f" [ \"$zone\" != 'lan' ] && continue; "
|
||||
f" for net in {nets}; do "
|
||||
f" cur=$(uci -q get firewall.@zone[$i].network 2>/dev/null || true); "
|
||||
f' echo "$cur" | grep -qw "$net" || '
|
||||
f' uci add_list firewall.@zone[$i].network="$net"; '
|
||||
f" done; break; "
|
||||
f"done; true"
|
||||
)
|
||||
|
||||
|
||||
def bridge_part(config: ApConfigDict, plan: Plan) -> None:
|
||||
"""``network/bridge``: br-ap built anew, with every VLAN of management and SSIDs."""
|
||||
mvid = config.get("management_vlan_id")
|
||||
ssid_vlans = _ssid_vlans(config)
|
||||
vlans = sorted({*ssid_vlans, *([mvid] if mvid else [])})
|
||||
plan.commands += [
|
||||
"uci set network.apbr=device",
|
||||
"uci set network.apbr.name='br-ap'",
|
||||
"uci set network.apbr.type='bridge'",
|
||||
"uci set network.apbr.vlan_filtering='1'",
|
||||
f"uci set network.apbr.stp={sq(_stp(config))}",
|
||||
"uci -q delete network.apbr.ports 2>/dev/null; true",
|
||||
"uci add_list network.apbr.ports='eth0'",
|
||||
_PURGE_BRIDGE_VLANS,
|
||||
]
|
||||
for vid in vlans:
|
||||
sec = f"apbr_vlan_{vid}"
|
||||
plan.commands += [
|
||||
f"uci set network.{sec}=bridge-vlan",
|
||||
f"uci set network.{sec}.device='br-ap'",
|
||||
f"uci set network.{sec}.vlan={sq(vid)}",
|
||||
f"uci set network.{sec}.ports={sq(vlan_port_spec(config, vid))}",
|
||||
]
|
||||
if mvid:
|
||||
plan.commands.append(_management_interface(mvid))
|
||||
for vid in ssid_vlans:
|
||||
if vid != mvid:
|
||||
plan.commands += _ap_vlan_interface(vid)
|
||||
plan.commands += [_ATTACH_APS, _DROP_OTHER_DEVICES, _DROP_OLD_INTERFACES]
|
||||
plan.commands.append(
|
||||
_lan_zone(["$_mgmt_sec", *sorted({f"ap_vlan{v}" for v in ssid_vlans if v != mvid})])
|
||||
)
|
||||
plan.commits.update({"firewall", "network", "wireless"})
|
||||
|
||||
|
||||
def vlan_part(vid: int, config: ApConfigDict, plan: Plan) -> None:
|
||||
"""``network/vlan/<vid>``: the VLAN on br-ap and its interface.
|
||||
|
||||
Reuses whatever section already defines the VLAN -- its name is not ours to
|
||||
assume -- and creates one only where none does. Unlike the bridge, nothing is
|
||||
purged: the other VLANs are not being rewritten.
|
||||
"""
|
||||
sec = f"apbr_vlan_{vid}"
|
||||
ports = vlan_port_spec(config, vid)
|
||||
plan.commands.append(
|
||||
"\n".join(
|
||||
[
|
||||
"_i=0; _found=''",
|
||||
"while uci -q get network.@bridge-vlan[$_i] >/dev/null 2>&1; do",
|
||||
' if [ "$(uci -q get network.@bridge-vlan[$_i].device 2>/dev/null)" ='
|
||||
" 'br-ap' ] &&"
|
||||
' [ "$(uci -q get network.@bridge-vlan[$_i].vlan 2>/dev/null)" ='
|
||||
f" {sq(vid)} ]; then",
|
||||
" _found=$_i; break",
|
||||
" fi",
|
||||
" _i=$((_i+1))",
|
||||
"done",
|
||||
'if [ -n "$_found" ]; then',
|
||||
f" uci set network.@bridge-vlan[$_found].ports={sq(ports)}",
|
||||
"else",
|
||||
f" uci set network.{sec}=bridge-vlan",
|
||||
f" uci set network.{sec}.device='br-ap'",
|
||||
f" uci set network.{sec}.vlan={sq(vid)}",
|
||||
f" uci set network.{sec}.ports={sq(ports)}",
|
||||
"fi",
|
||||
"true",
|
||||
]
|
||||
)
|
||||
)
|
||||
if vid == (config.get("management_vlan_id") or 0):
|
||||
plan.commands.append(f"uci set network.mgmt.device='br-ap.{vid}'")
|
||||
else:
|
||||
plan.commands += _ap_vlan_interface(vid)
|
||||
plan.commits.add("network")
|
||||
Reference in new issue
Block a user