From e2aea648cf2e6c8b11d53d6149f9e9d161da6d3e Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 24 Mar 2023 14:32:39 +0100 Subject: greatly simplify ipv4 and ipv6 configuration --- nix/deuxfleurs.nix | 87 ++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 49 deletions(-) (limited to 'nix/deuxfleurs.nix') diff --git a/nix/deuxfleurs.nix b/nix/deuxfleurs.nix index 245e12c..6d27d5c 100644 --- a/nix/deuxfleurs.nix +++ b/nix/deuxfleurs.nix @@ -36,36 +36,20 @@ in in { # Parameters for individual nodes - network_interface = mkOption { - description = "Network interface name to configure"; - type = types.str; - }; - lan_ip = mkOption { - description = "IP address of this node on the local network interface"; - type = types.str; - }; - lan_ip_prefix_length = mkOption { - description = "Prefix length associated with lan_ip"; - type = types.int; - }; ipv6 = mkOption { - description = "Public IPv6 address of this node"; + description = "Static public IPv6 address of this node"; type = types.str; }; - ipv6_prefix_length = mkOption { - description = "Prefix length associated with ipv6 ip"; - type = types.int; + staticIPv4.address = mkOption { + description = "IP address (with prefix length) of this node on the local network interface"; + type = types.nullOr types.str; + default = null; }; cluster_ip = mkOption { description = "IP address of this node on the Wesher mesh network"; type = types.str; }; - wireguard_port = mkOption { - description = "Port for incoming Wireguard VPN connections"; - type = types.port; - default = 33799; - }; is_raft_server = mkOption { description = "Make this node a RAFT server for the Nomad and Consul deployments"; @@ -74,18 +58,15 @@ in }; # Parameters that generally vary between sites - lan_default_gateway = mkOption { - description = "IPv4 address of the default route on the local network interface"; - type = types.str; - }; - ipv6_default_gateway = mkOption { - description = "IPv6 address of the default IPv6 gateway for the targeted net interface"; - type = types.str; - }; site_name = mkOption { description = "Site (availability zone) on which this node is deployed"; type = types.str; }; + staticIPv4.defaultGateway = mkOption { + description = "IPv4 address of the default route on the local network interface"; + type = types.nullOr types.str; + default = null; + }; public_ipv4 = mkOption { description = "Public IPv4 through which this node is accessible (possibly after port opening using DiploNAT), for domain names that are updated by D53"; type = types.nullOr types.str; @@ -124,6 +105,13 @@ in type = types.bool; default = false; }; + + # Options that generally stay to their default value + wireguardPort = mkOption { + description = "Port for incoming Wireguard VPN connections"; + type = types.port; + default = 33799; + }; }; imports = [ @@ -157,33 +145,33 @@ in systemd.network.networks = { "10-uplink" = { matchConfig = { - # We could preprend "en* eth*" to match all ethernet interfaces - Name = "${cfg.network_interface}"; + Name = "en* eth*"; }; + ipv6AcceptRAConfig = { + Token = "static:${cfg.ipv6}"; + UseDNS = false; + }; + } // (if cfg.staticIPv4.address == null || cfg.staticIPv4.defaultGateway == null then { networkConfig = { - IPv6AcceptRA = false; - LinkLocalAddressing = "no"; + DHCP = "ipv4"; + }; + dhcpV4Config = { + UseDNS = false; }; + } else { address = [ - "${cfg.lan_ip}/${toString cfg.lan_ip_prefix_length}" - "${cfg.ipv6}/${toString cfg.ipv6_prefix_length}" + "${cfg.staticIPv4.address}" ]; routes = [ { routeConfig = { - Gateway = cfg.lan_default_gateway; + Gateway = cfg.staticIPv4.defaultGateway; # GatewayOnLink - Takes a boolean. If set to true, the kernel does not have to check if the gateway is reachable directly by the current machine (i.e., attached to the local network), so that we can insert the route in the kernel table without it being complained about. Defaults to "no". GatewayOnLink = true; }; } - { - routeConfig = { - Gateway = cfg.ipv6_default_gateway; - GatewayOnLink = true; - }; - } ]; - }; + }); }; # Configure Unbound as a central DNS server for everything @@ -221,7 +209,7 @@ in # Configure Wireguard VPN between all nodes networking.wireguard.interfaces.wg0 = { ips = [ "${cfg.cluster_ip}/16" ]; - listenPort = cfg.wireguard_port; + listenPort = cfg.wireguardPort; privateKeyFile = "/var/lib/deuxfleurs/wireguard-keys/private"; mtu = 1420; }; @@ -248,7 +236,7 @@ in # systemd.services."wg-quick-wg0".after = [ "unbound.service" ]; # networking.wg-quick.interfaces.wg0 = { # address = [ "${cfg.cluster_ip}/16" ]; - # listenPort = cfg.wireguard_port; + # listenPort = cfg.wireguardPort; # privateKeyFile = "/var/lib/deuxfleurs/wireguard-keys/private"; # mtu = 1420; # peers = map ({ publicKey, endpoint, IP, ... }: { @@ -384,13 +372,14 @@ in allowedUDPPorts = [ # Allow peers to connect to Wireguard - cfg.wireguard_port + cfg.wireguardPort ]; # Allow specific hosts access to specific things in the cluster extraCommands = '' - # Allow everything from router (usefull for UPnP/IGD) - iptables -A INPUT -s ${cfg.lan_default_gateway} -j ACCEPT + # Allow UDP packets comming from port 1900 from a local address, + # these are necessary for UPnP/IGD + iptables -A INPUT -s 192.168.0.0/16 -p udp --sport 1900 -j ACCEPT # Allow docker containers to access all ports iptables -A INPUT -s 172.17.0.0/16 -j ACCEPT @@ -401,7 +390,7 @@ in # When stopping firewall, delete all rules that were configured manually above extraStopCommands = '' - iptables -D INPUT -s ${cfg.lan_default_gateway} -j ACCEPT + iptables -D INPUT -s 192.168.0.0/16 -p udp --sport 1900 -j ACCEPT iptables -D INPUT -s 172.17.0.0/16 -j ACCEPT iptables -D INPUT -s ${cfg.cluster_prefix}/${toString cfg.cluster_prefix_length} -j ACCEPT ''; -- cgit v1.2.3