sbc-deploys/modules/router/default.nix
2024-07-02 00:52:32 -05:00

150 lines
4 KiB
Nix

{ hostName }:
{ ... }:
let
wan = "end0";
lan = "enp1s0";
bridge = "br-lan";
in
{
boot.kernel = {
sysctl = {
"net.ipv4.conf.all.forwarding" = true;
"net.ipv6.conf.all.forwarding" = false;
"net.ipv4.conf.default.rp_filter" = 1;
"net.ipv4.conf.${wan}.rp_filter" = 1;
"net.ipv4.conf.${bridge}.rp_filter" = 0;
};
};
systemd.network = {
wait-online.anyInterface = true;
netdevs = {
"20-${bridge}" = {
netdevConfig = {
Kind = "bridge";
Name = bridge;
};
};
};
networks = {
"10-${wan}" = {
matchConfig.Name = wan;
linkConfig.RequiredForOnline = "routable";
networkConfig = {
DHCP = "ipv4";
IPv6AcceptRA = true;
DNSOverTLS = true;
DNSSEC = true;
IPv6PrivacyExtensions = false;
IPForward = true;
};
};
"30-${lan}" = {
matchConfig.Name = lan;
linkConfig.RequiredForOnline = "enslaved";
networkConfig = {
Bridge = bridge;
ConfigureWithoutCarrier = true;
};
};
"40-${bridge}" = {
matchConfig.Name = bridge;
bridgeConfig = { };
address = [
"192.168.20.1/24"
];
networkConfig = {
ConfigureWithoutCarrier = true;
};
linkConfig.RequiredForOnline = "no";
};
};
};
networking = {
inherit hostName;
useNetworkd = true;
useDHCP = false;
nat.enable = false;
firewall.enable = false;
nftables = {
enable = true;
checkRuleset = false;
ruleset = ''
define WAN=${wan}
define LAN=${lan}
define BRIDGE=${bridge}
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
iifname $BRIDGE accept comment "Allow local network to access the router"
iifname $WAN ct state { established, related } accept comment "Allow established traffic"
iifname $WAN icmp type { echo-request, destination-unreachable, time-exceeded } counter accept comment "icmp stuff"
iifname $WAN tcp dport 3128 accept comment "Allow SSH in"
iifname $WAN counter drop comment "Drop all other traffic from wan"
iifname "lo" accept comment "Accept everything from loopback"
}
chain forward {
type filter hook forward priority filter; policy drop;
iifname $BRIDGE oifname $BRIDGE accept comment "Allow forwarding internal traffic"
iifname $WAN oifname $BRIDGE tcp dport { 22, 80, 443, 27750 } accept comment "Allow forwarding external traffic to services"
iifname $BRIDGE oifname $WAN accept comment "Allow trusted LAN to WAN"
iifname $WAN oifname $BRIDGE ct state { established, related } accept comment "Allow established traffic"
}
}
table ip nat {
chain prerouting {
type nat hook prerouting priority -100; policy accept;
fib daddr type local tcp dport { 80, 443 } dnat to 192.168.20.200
fib daddr type local tcp dport 22 dnat to 192.168.20.201:2222
fib daddr type local tcp dport 27750 dnat to 192.168.20.202:27750
}
chain postrouting {
type nat hook postrouting priority 100; policy accept;
oifname $WAN masquerade
ip saddr 192.168.20.0/24 masquerade
}
}
'';
};
};
services.openssh.ports = [ 22 3128 ];
services.resolved.enable = false;
services.dnsmasq = {
enable = true;
settings = {
server = [ "9.9.9.9" "9.9.9.10" ];
domain-needed = true;
bogus-priv = true;
no-resolv = true;
cache-size = 1000;
dhcp-range = [ "br-lan,192.168.20.50,192.168.20.90,24h" ];
interface = "br-lan";
dhcp-host = "192.168.20.1";
local = "/lan/";
domain = "lan";
expand-hosts = true;
no-hosts = true;
address = "/router.lan/192.168.20.1";
};
};
services.irqbalance.enable = false;
}