{ hostName, config, lib, ... }: let hostConf = config.site.hosts.${hostName}; upstreamInterfaces = lib.filterAttrs (_: { upstream, ... }: upstream != null) hostConf.interfaces; firstUpstreamInterface = if builtins.length (builtins.attrNames upstreamInterfaces) > 0 then builtins.head ( builtins.attrNames upstreamInterfaces ) else null; enabled = firstUpstreamInterface != null; in { systemd.network.networks = { core = { # systemd-networkd only requests Prefix Delegation via DHCPv6 on # the upstream interface if another interface is configured for it. # without this, the static ipv6 subnet won't be routed to us. networkConfig.DHCPv6PrefixDelegation = true; dhcpV6PrefixDelegationConfig = { SubnetId = "81"; # because we have static addresses, we don't actually use this Assign = false; }; }; } // builtins.mapAttrs (_: { upstream, ... }: { DHCP = "yes"; networkConfig.IPv6AcceptRA = true; dhcpV6Config.PrefixDelegationHint = "::/56"; # Traffic Shaping extraConfig = '' [CAKE] Parent = root ${lib.optionalString (upstream.provider == "vodafone") '' # DOCSIS overhead OverheadBytes = 18 ''} ${lib.optionalString (upstream.provider == "dsi") '' # PPPoE overhead OverheadBytes = 18 ''} ${lib.optionalString (upstream.upBandwidth != null) '' Bandwidth = ${toString upstream.upBandwidth}K ''} ''; }) upstreamInterfaces; networking.nat = lib.optionalAttrs enabled { enable = true; internalInterfaces = [ "core" ]; externalInterface = firstUpstreamInterface; extraCommands = # Provide IPv6 upstream for everyone, using NAT66 when not from # our static prefixes builtins.concatStringsSep "\n" ( map (net: '' ip6tables -t nat -N ${net}_nat || \ ip6tables -t nat -F ${net}_nat ${builtins.concatStringsSep "\n" ( map (subnet: '' ip6tables -t nat -A ${net}_nat \ -s ${subnet} \ -j RETURN '') upstreamInterfaces.${net}.upstream.noNat.subnets6 )} ip6tables -t nat -A ${net}_nat -j MASQUERADE ip6tables -t nat -A POSTROUTING \ -o ${net} \ -j ${net}_nat '') (builtins.attrNames upstreamInterfaces) ) + # Do SNAT on connection attempts so that the actual return path # won't matter. Forwarded ports will work from internal networks # and on services that have Internet through another upstream routers. builtins.concatStringsSep "\n" ( map ({ proto, destination, sourcePort, ... }: let ds = builtins.split ":" destination; ds' = if builtins.length ds == 3 then { dest = lib.elemAt ds 0; port = lib.elemAt ds 2; } else if builtins.length ds == 1 then { dest = lib.elemAt ds 0; port = toString sourcePort; } else throw "Too many colons in a forwardPorts destination"; inherit (ds') dest port; in '' iptables -t nat -A nixos-nat-post \ -p ${proto} --dest ${dest} --dport ${port} \ -j SNAT --to-source ${config.site.net.core.hosts4.${hostName}} '') hostConf.forwardPorts ); extraStopCommands = builtins.concatStringsSep "\n" ( map (net: '' ip6tables -t nat -F POSTROUTING 2>/dev/null || true ip6tables -t nat -F ${net}_nat 2>/dev/null || true ip6tables -t nat -X ${net}_nat 2>/dev/null || true '') (builtins.attrNames upstreamInterfaces) ); inherit (hostConf) forwardPorts; }; }