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
|
2024-09-03 08:57:21 +02:00
|
|
|
from dataclasses import dataclass
|
2022-11-24 18:30:37 +01:00
|
|
|
|
|
|
|
|
2024-09-03 08:57:21 +02:00
|
|
|
@dataclass
|
|
|
|
class Interface:
|
|
|
|
name: str
|
|
|
|
ifname: str | None
|
|
|
|
mac_address: str
|
|
|
|
dynamic_addresses: list[str]
|
|
|
|
static_addresses: list[dict[str, Any]]
|
|
|
|
static_routes: list[dict[str, Any]]
|
|
|
|
|
|
|
|
|
|
|
|
def filter_interfaces(network: list[dict[str, Any]]) -> list[Interface]:
|
|
|
|
interfaces = []
|
2022-11-24 18:30:37 +01:00
|
|
|
for net in network:
|
2022-11-27 17:06:17 +01:00
|
|
|
if net.get("link_type") == "loopback":
|
|
|
|
continue
|
2024-09-03 08:57:21 +02:00
|
|
|
if not (mac_address := net.get("address")):
|
2022-11-27 17:06:17 +01:00
|
|
|
# We need a mac address to match devices reliable
|
2022-11-24 18:30:37 +01:00
|
|
|
continue
|
2024-09-03 08:57:21 +02:00
|
|
|
static_addresses = []
|
|
|
|
dynamic_addresses = []
|
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
|
2022-11-24 18:30:37 +01:00
|
|
|
if addr.get("dynamic", False):
|
2024-09-03 08:57:21 +02:00
|
|
|
dynamic_addresses.append(addr["local"])
|
2022-11-24 18:30:37 +01:00
|
|
|
else:
|
2024-09-03 08:57:21 +02:00
|
|
|
static_addresses.append(addr)
|
|
|
|
interfaces.append(
|
|
|
|
Interface(
|
|
|
|
name=net.get("ifname", mac_address.replace(":", "-")),
|
|
|
|
ifname=net.get("ifname"),
|
|
|
|
mac_address=mac_address,
|
|
|
|
dynamic_addresses=dynamic_addresses,
|
|
|
|
static_addresses=static_addresses,
|
|
|
|
static_routes=[],
|
|
|
|
)
|
|
|
|
)
|
2022-11-24 18:30:37 +01:00
|
|
|
|
2024-09-03 08:57:21 +02:00
|
|
|
return interfaces
|
2022-11-24 18:30:37 +01:00
|
|
|
|
|
|
|
|
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:
|
2023-10-16 23:22:54 +02:00
|
|
|
# Filter out routes set by addresses with subnets, dhcp and router advertisement
|
2022-11-27 17:06:17 +01:00
|
|
|
if route.get("protocol") in ["dhcp", "kernel", "ra"]:
|
|
|
|
continue
|
|
|
|
filtered.append(route)
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
|
|
|
|
|
2024-09-03 08:57:21 +02:00
|
|
|
def generate_routes(interface: Interface, routes: list[dict[str, Any]]) -> list[str]:
|
|
|
|
route_sections = []
|
|
|
|
for route in routes:
|
|
|
|
if interface.ifname is None or route.get("dev") != interface.ifname:
|
|
|
|
continue
|
|
|
|
|
|
|
|
route_section = "[Route]\n"
|
|
|
|
if route.get("dst") != "default":
|
|
|
|
# can be skipped for default routes
|
|
|
|
route_section += f"Destination = {route['dst']}\n"
|
|
|
|
gateway = route.get("gateway")
|
|
|
|
# route v4 via v6
|
|
|
|
route_via = route.get("via")
|
|
|
|
if route_via and route_via.get("family") == "inet6":
|
|
|
|
gateway = route_via.get("host")
|
|
|
|
if route.get("dst") == "default":
|
|
|
|
route_section += "Destination = 0.0.0.0/0\n"
|
|
|
|
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)
|
|
|
|
return route_sections
|
|
|
|
|
|
|
|
|
2022-11-27 17:06:17 +01:00
|
|
|
def generate_networkd_units(
|
2024-09-03 08:57:21 +02:00
|
|
|
interfaces: list[Interface], routes: list[dict[str, Any]], directory: Path
|
2022-11-27 17:06:17 +01:00
|
|
|
) -> None:
|
|
|
|
directory.mkdir(exist_ok=True)
|
|
|
|
for interface in interfaces:
|
2024-09-03 08:57:21 +02:00
|
|
|
name = f"00-{interface.name}.network"
|
2022-11-27 17:06:17 +01:00
|
|
|
|
2023-10-16 23:22:54 +02:00
|
|
|
# FIXME in some networks we might not want to trust dhcp or router advertisements
|
2022-11-27 17:06:17 +01:00
|
|
|
unit = f"""
|
|
|
|
[Match]
|
2024-09-03 08:57:21 +02:00
|
|
|
MACAddress = {interface.mac_address}
|
2022-11-27 17:06:17 +01:00
|
|
|
|
|
|
|
[Network]
|
2023-11-02 10:43:08 +01:00
|
|
|
# both ipv4 and ipv6
|
2022-11-27 17:06:17 +01:00
|
|
|
DHCP = yes
|
2023-11-02 10:43:08 +01:00
|
|
|
# lets us discover the switch port we're connected to
|
|
|
|
LLDP = yes
|
|
|
|
# ipv6 router advertisements
|
2022-11-27 17:06:17 +01:00
|
|
|
IPv6AcceptRA = yes
|
2023-11-02 10:43:08 +01:00
|
|
|
# allows us to ping "nixos.local"
|
|
|
|
MulticastDNS = yes
|
|
|
|
|
2022-11-27 17:06:17 +01:00
|
|
|
"""
|
2024-09-03 08:57:21 +02:00
|
|
|
unit += "\n".join(
|
|
|
|
[
|
|
|
|
f"Address = {addr['local']}/{addr['prefixlen']}"
|
|
|
|
for addr in interface.static_addresses
|
|
|
|
]
|
|
|
|
)
|
|
|
|
unit += "\n" + "\n".join(generate_routes(interface, routes))
|
2022-11-27 17:06:17 +01:00
|
|
|
(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()
|