sbc-deploys/modules/router/default.nix

210 lines
5.8 KiB
Nix
Raw Normal View History

2024-07-02 05:52:32 +00:00
{ hostName }:
2024-07-03 00:12:41 +00:00
{ pkgs, ... }:
2024-07-01 19:11:08 +00:00
2024-07-02 05:52:32 +00:00
let
wan = "end0";
lan = "enp1s0";
bridge = "br-lan";
2024-07-02 22:38:29 +00:00
devices = "devices-vlan";
wifi = "wifi-vlan";
2024-07-02 05:52:32 +00:00
in
2024-07-01 19:11:08 +00:00
{
boot.kernel = {
sysctl = {
"net.ipv4.conf.all.forwarding" = true;
"net.ipv6.conf.all.forwarding" = false;
"net.ipv4.conf.default.rp_filter" = 1;
2024-07-02 05:52:32 +00:00
"net.ipv4.conf.${wan}.rp_filter" = 1;
"net.ipv4.conf.${bridge}.rp_filter" = 0;
2024-07-01 19:11:08 +00:00
};
};
2024-07-03 00:12:41 +00:00
environment.systemPackages = with pkgs; [
arp-scan
];
2024-07-01 19:11:08 +00:00
systemd.network = {
wait-online.anyInterface = true;
netdevs = {
2024-07-02 22:38:29 +00:00
"00-${devices}" = {
netdevConfig = {
Name = devices;
Kind = "vlan";
};
vlanConfig.Id = 30;
};
"00-${wifi}" = {
netdevConfig = {
Name = wifi;
Kind = "vlan";
};
vlanConfig.Id = 40;
};
2024-07-02 05:52:32 +00:00
"20-${bridge}" = {
netdevConfig = {
2024-07-01 19:11:08 +00:00
Kind = "bridge";
2024-07-02 05:52:32 +00:00
Name = bridge;
2024-07-01 19:11:08 +00:00
};
};
};
networks = {
2024-07-02 05:52:32 +00:00
"10-${wan}" = {
matchConfig.Name = wan;
2024-07-01 19:11:08 +00:00
linkConfig.RequiredForOnline = "routable";
networkConfig = {
DHCP = "ipv4";
2024-07-02 05:52:32 +00:00
IPv6AcceptRA = true;
2024-07-01 19:11:08 +00:00
DNSOverTLS = true;
DNSSEC = true;
IPv6PrivacyExtensions = false;
IPForward = true;
};
};
2024-07-02 05:52:32 +00:00
"30-${lan}" = {
matchConfig.Name = lan;
2024-07-01 19:11:08 +00:00
linkConfig.RequiredForOnline = "enslaved";
networkConfig = {
2024-07-02 05:52:32 +00:00
Bridge = bridge;
2024-07-01 19:11:08 +00:00
ConfigureWithoutCarrier = true;
2024-07-03 00:12:41 +00:00
VLAN = [ devices wifi ];
2024-07-01 19:11:08 +00:00
};
};
2024-07-02 05:52:32 +00:00
"40-${bridge}" = {
matchConfig.Name = bridge;
2024-07-01 19:11:08 +00:00
bridgeConfig = { };
address = [
2024-07-02 05:52:32 +00:00
"192.168.20.1/24"
2024-07-01 19:11:08 +00:00
];
networkConfig = {
ConfigureWithoutCarrier = true;
};
2024-07-02 05:52:32 +00:00
linkConfig.RequiredForOnline = "no";
2024-07-01 19:11:08 +00:00
};
2024-07-02 22:38:29 +00:00
"50-${devices}" = {
matchConfig = {
Name = devices;
Type = "vlan";
};
address = [
"192.168.30.1/24"
];
};
"50-${wifi}" = {
matchConfig = {
Name = wifi;
Type = "vlan";
};
address = [
"192.168.40.1/24"
];
};
2024-07-01 19:11:08 +00:00
};
};
networking = {
2024-07-02 05:52:32 +00:00
inherit hostName;
2024-07-01 19:11:08 +00:00
useNetworkd = true;
useDHCP = false;
nat.enable = false;
firewall.enable = false;
nftables = {
enable = true;
2024-07-02 05:52:32 +00:00
checkRuleset = false;
2024-07-01 19:11:08 +00:00
ruleset = ''
2024-07-02 05:52:32 +00:00
define WAN=${wan}
define LAN=${lan}
define BRIDGE=${bridge}
2024-07-02 22:38:29 +00:00
define DEVICES=${devices}
define WIFI=${wifi}
2024-07-02 05:52:32 +00:00
2024-07-01 19:11:08 +00:00
table inet filter {
2024-07-02 05:52:32 +00:00
2024-07-01 19:11:08 +00:00
chain input {
type filter hook input priority 0; policy drop;
2024-07-02 05:52:32 +00:00
iifname $BRIDGE accept comment "Allow local network to access the router"
2024-07-03 00:12:41 +00:00
iifname { $DEVICES, $WIFI } udp dport { 53, 67, 68 } accept comment "DHCP & DNS for vlans"
iifname { $WAN, $DEVICES, $WIFI } ct state { established, related } accept comment "Allow established traffic"
2024-07-02 05:52:32 +00:00
iifname $WAN icmp type { echo-request, destination-unreachable, time-exceeded } counter accept comment "icmp stuff"
2024-07-02 22:38:29 +00:00
iifname { $WAN, $WIFI } tcp dport 3128 accept comment "Allow SSH in"
2024-07-02 05:52:32 +00:00
iifname $WAN counter drop comment "Drop all other traffic from wan"
2024-07-01 19:11:08 +00:00
iifname "lo" accept comment "Accept everything from loopback"
}
chain forward {
type filter hook forward priority filter; policy drop;
2024-07-02 05:52:32 +00:00
iifname $BRIDGE oifname $BRIDGE accept comment "Allow forwarding internal traffic"
2024-07-02 22:38:29 +00:00
iifname { $WAN, $WIFI } oifname $BRIDGE tcp dport { 22, 80, 443, 27750 } accept comment "Allow forwarding external traffic to services"
2024-07-03 00:12:41 +00:00
iifname $BRIDGE oifname $WIFI ct state { established, related } accept comment "Allow established traffic"
iifname $WIFI oifname $BRIDGE ct state { established, related } accept comment "Allow established traffic"
2024-07-02 22:38:29 +00:00
iifname { $BRIDGE, $DEVICES, $WIFI } oifname $WAN accept comment "Allow trusted LAN to WAN"
iifname $WAN oifname { $BRIDGE, $DEVICES, $WIFI } ct state { established, related } accept comment "Allow established traffic"
2024-07-01 19:11:08 +00:00
}
}
table ip nat {
2024-07-02 05:52:32 +00:00
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
}
2024-07-01 19:11:08 +00:00
chain postrouting {
type nat hook postrouting priority 100; policy accept;
2024-07-02 05:52:32 +00:00
oifname $WAN masquerade
2024-07-02 05:59:24 +00:00
iifname $BRIDGE oifname $BRIDGE masquerade
2024-07-02 22:38:29 +00:00
iifname $WIFI oifname $BRIDGE masquerade
2024-07-01 19:11:08 +00:00
}
}
'';
};
2024-07-02 05:52:32 +00:00
};
2024-07-01 19:11:08 +00:00
2024-07-02 05:52:32 +00:00
services.openssh.ports = [ 22 3128 ];
2024-07-01 19:11:08 +00:00
2024-07-02 05:52:32 +00:00
services.resolved.enable = false;
2024-07-01 19:11:08 +00:00
2024-07-02 05:52:32 +00:00
services.dnsmasq = {
enable = true;
settings = {
server = [ "9.9.9.9" "9.9.9.10" ];
domain-needed = true;
bogus-priv = true;
no-resolv = true;
2024-07-01 19:11:08 +00:00
2024-07-02 05:52:32 +00:00
cache-size = 1000;
2024-07-01 19:11:08 +00:00
2024-07-02 22:38:29 +00:00
dhcp-range = [
2024-07-03 00:12:41 +00:00
# format TAG,START,END,MASK,LEASE
"${bridge},192.168.20.50,192.168.20.90,255.255.255.0,24h"
"${devices},192.168.30.10,192.168.30.240,255.255.255.0,24h"
"${wifi},192.168.40.10,192.168.40.240,255.255.255.0,24h"
2024-07-02 22:38:29 +00:00
];
interface = [ bridge devices wifi ];
dhcp-option = [
2024-07-03 00:12:41 +00:00
"${bridge},option:router,192.168.20.1"
"${bridge},option:dns-server,192.168.20.1"
2024-07-02 22:38:29 +00:00
2024-07-03 00:12:41 +00:00
"${devices},option:router,192.168.30.1"
"${devices},option:dns-server,192.168.30.1"
2024-07-02 22:38:29 +00:00
2024-07-03 00:12:41 +00:00
"${wifi},option:router,192.168.40.1"
"${wifi},option:dns-server,192.168.40.1"
2024-07-02 22:38:29 +00:00
];
2024-07-02 05:52:32 +00:00
local = "/lan/";
domain = "lan";
expand-hosts = true;
no-hosts = true;
address = "/router.lan/192.168.20.1";
2024-07-01 19:11:08 +00:00
};
};
2024-07-02 05:52:32 +00:00
services.irqbalance.enable = false;
2024-07-01 19:11:08 +00:00
}