2022-11-24 18:30:37 +01:00
|
|
|
import json
|
|
|
|
import sys
|
2022-11-27 17:06:17 +01:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any
|
2022-11-24 18:30:37 +01:00
|
|
|
|
|
|
|
|
2022-11-27 17:06:17 +01:00
|
|
|
def filter_interfaces(network: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
2022-11-24 18:30:37 +01:00
|
|
|
output = []
|
|
|
|
for net in network:
|
2022-11-27 17:06:17 +01:00
|
|
|
if net.get("link_type") == "loopback":
|
|
|
|
continue
|
|
|
|
if not net.get("address"):
|
|
|
|
# We need a mac address to match devices reliable
|
2022-11-24 18:30:37 +01:00
|
|
|
continue
|
|
|
|
addr_info = []
|
2022-11-27 17:06:17 +01:00
|
|
|
has_dynamic_address = False
|
2023-03-23 09:37:04 +01:00
|
|
|
for addr in net.get("addr_info", []):
|
2022-11-27 17:06:17 +01:00
|
|
|
# no link-local ipv4/ipv6
|
|
|
|
if addr.get("scope") == "link":
|
|
|
|
continue
|
|
|
|
# do not explicitly configure addresses from dhcp or router advertisment
|
2022-11-24 18:30:37 +01:00
|
|
|
if addr.get("dynamic", False):
|
2022-11-27 17:06:17 +01:00
|
|
|
has_dynamic_address = True
|
|
|
|
continue
|
2022-11-24 18:30:37 +01:00
|
|
|
else:
|
|
|
|
addr_info.append(addr)
|
2022-11-27 17:06:17 +01:00
|
|
|
if addr_info != [] or has_dynamic_address:
|
2022-11-24 18:30:37 +01:00
|
|
|
net["addr_info"] = addr_info
|
|
|
|
output.append(net)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2022-11-27 17:06:17 +01:00
|
|
|
def filter_routes(routes: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
|
|
filtered = []
|
|
|
|
for route in routes:
|
|
|
|
# Filter out routes set by addresses with subnets, dhcp and router advertisment
|
|
|
|
if route.get("protocol") in ["dhcp", "kernel", "ra"]:
|
|
|
|
continue
|
|
|
|
filtered.append(route)
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
|
|
|
|
|
|
|
|
def generate_networkd_units(
|
|
|
|
interfaces: list[dict[str, Any]], routes: list[dict[str, Any]], directory: Path
|
|
|
|
) -> None:
|
|
|
|
directory.mkdir(exist_ok=True)
|
|
|
|
for interface in interfaces:
|
|
|
|
name = f"{interface['ifname']}.network"
|
|
|
|
addresses = [
|
|
|
|
f"Address = {addr['local']}/{addr['prefixlen']}"
|
2023-03-23 09:37:04 +01:00
|
|
|
for addr in interface.get("addr_info", [])
|
2022-11-27 17:06:17 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
route_sections = []
|
|
|
|
for route in routes:
|
2023-03-23 09:37:04 +01:00
|
|
|
if route.get("dev", "nodev") != interface.get("ifname", "noif"):
|
2022-11-27 17:06:17 +01:00
|
|
|
continue
|
|
|
|
|
2022-11-27 19:56:24 +01:00
|
|
|
route_section = "[Route]\n"
|
2023-03-23 09:37:04 +01:00
|
|
|
if route.get("dst") != "default":
|
2022-11-27 17:06:17 +01:00
|
|
|
# can be skipped for default routes
|
|
|
|
route_section += f"Destination = {route['dst']}\n"
|
|
|
|
gateway = route.get("gateway")
|
|
|
|
if gateway:
|
|
|
|
route_section += f"Gateway = {gateway}\n"
|
|
|
|
|
|
|
|
# we may ignore on-link default routes here, but I don't see how
|
|
|
|
# they would be useful for internet connectivity anyway
|
|
|
|
route_sections.append(route_section)
|
|
|
|
|
|
|
|
# FIXME in some networks we might not want to trust dhcp or router advertisments
|
|
|
|
unit = f"""
|
|
|
|
[Match]
|
|
|
|
MACAddress = {interface["address"]}
|
|
|
|
|
|
|
|
[Network]
|
|
|
|
DHCP = yes
|
|
|
|
IPv6AcceptRA = yes
|
|
|
|
"""
|
|
|
|
unit += "\n".join(addresses)
|
|
|
|
unit += "\n" + "\n".join(route_sections)
|
|
|
|
(directory / name).write_text(unit)
|
|
|
|
|
|
|
|
|
2022-11-25 09:50:03 +01:00
|
|
|
def main() -> None:
|
2022-11-27 19:56:24 +01:00
|
|
|
if len(sys.argv) < 5:
|
2022-11-27 17:06:17 +01:00
|
|
|
print(
|
2022-11-27 19:56:24 +01:00
|
|
|
f"USAGE: {sys.argv[0]} addresses routes-v4 routes-v6 networkd-directory",
|
2022-11-27 17:06:17 +01:00
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2022-11-25 09:50:03 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2022-11-24 18:30:37 +01:00
|
|
|
with open(sys.argv[1]) as f:
|
2022-11-27 17:55:14 +01:00
|
|
|
addresses = json.load(f)
|
2022-11-24 18:30:37 +01:00
|
|
|
with open(sys.argv[2]) as f:
|
2022-11-27 17:06:17 +01:00
|
|
|
v4_routes = json.load(f)
|
2022-11-27 17:55:14 +01:00
|
|
|
with open(sys.argv[3]) as f:
|
2022-11-27 17:06:17 +01:00
|
|
|
v6_routes = json.load(f)
|
|
|
|
|
2022-11-27 19:56:24 +01:00
|
|
|
networkd_directory = Path(sys.argv[4])
|
2022-11-27 17:06:17 +01:00
|
|
|
|
2022-11-24 18:30:37 +01:00
|
|
|
relevant_interfaces = filter_interfaces(addresses)
|
2022-11-27 17:06:17 +01:00
|
|
|
relevant_routes = filter_routes(v4_routes) + filter_routes(v6_routes)
|
|
|
|
|
|
|
|
generate_networkd_units(relevant_interfaces, relevant_routes, networkd_directory)
|
2022-11-24 18:30:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|