Files
nixos/modules-linux/router.nix
T

90 lines
2.9 KiB
Nix
Raw Normal View History

2023-05-05 15:59:59 -05:00
{ config, lib, pkgs, ... }:
let
names = mylist: (lib.strings.concatMapStringsSep "," (x: ''"${x}"'') mylist);
# Pass the names of the wan/lan ports
2023-05-05 19:21:11 -05:00
nftConfig = {
wan,
lan,
limitedLan ? [],
openPorts ? [ "ssh" "67" "53" ], # ssh, dhcpd, dns
openUDPPorts ? [ "67" "53" ] # dhcpd, dns
2023-05-05 19:21:11 -05:00
}: let
2023-05-05 15:59:59 -05:00
lanList = names lan;
allLan = names (lan ++ limitedLan);
2023-05-05 17:30:12 -05:00
wanName = names wan;
portsString = lib.strings.concatMapStringsSep "\n" (x: "iifname { ${lanList}, \"tailscale0\" } tcp dport ${toString x} accept") openPorts;
udpPortsString = lib.strings.concatMapStringsSep "\n" (x: "iifname { ${lanList}, \"tailscale0\" } udp dport ${toString x} accept") openUDPPorts;
2023-05-05 19:21:11 -05:00
in lib.strings.concatStringsSep "\n" [
"table ip filter {"
" chain input {"
" type filter hook input priority 0; policy drop;"
2023-05-05 15:59:59 -05:00
2023-05-05 19:21:11 -05:00
" iifname lo accept"
portsString
udpPortsString
2023-05-05 19:21:11 -05:00
" iifname { ${lanList} } accept comment \"Allows LAN traffic and outgoing\""
" iifname { ${wanName} } ct state { established, related } accept comment \"Allows existing connections\""
" iifname { ${wanName} } icmp type { echo-request, destination-unreachable, time-exceeded } counter accept comment \"Allow some ICMP traffic\""
" iifname { ${wanName} } counter drop comment \"Drop other incoming traffic, and count how much\""
" }"
" chain forward {"
" type filter hook forward priority 0; policy drop;"
" iifname { ${allLan} } oifname { ${wanName} } accept comment \"Forward LAN to WAN\""
" iifname { ${wanName} } oifname { ${allLan} } ct state established, related accept comment \"Allow incoming established traffic\""
" }"
"}"
2023-05-05 15:59:59 -05:00
2023-05-05 19:21:11 -05:00
"table ip nat {"
" chain postrouting {"
" type nat hook postrouting priority 100; policy accept;"
" oifname { ${wanName} } masquerade"
" }"
"}"
2023-05-05 15:59:59 -05:00
2023-05-05 19:21:11 -05:00
"table ip6 filter {"
" chain input {"
" type filter hook input priority 0; policy drop;"
" }"
" chain forward {"
" type filter hook forward priority 0; policy drop;"
" }"
"}"
];
2023-05-05 15:59:59 -05:00
cfg = config.greg.router;
in with lib; {
options.greg.router = {
enable = mkEnableOption "Enable NFTables and routing";
wan = mkOption {
2023-05-05 17:30:12 -05:00
type = (types.listOf types.str);
2023-05-05 15:59:59 -05:00
description = "The name of the network interface that is the WAN connection";
};
lan = mkOption {
type = (types.listOf types.str);
description = "A list of all network interfaces that are considered LAN connections";
};
limited = mkOption {
type = (types.listOf types.str);
description = "A list of limited access LAN connections - such as IOT connections and similar.";
default = [];
};
};
config = mkIf cfg.enable {
networking.nftables = {
enable = true;
ruleset = (nftConfig {
inherit (cfg) lan wan;
openPorts = config.networking.firewall.allowedTCPPorts;
openUDPPorts = config.networking.firewall.allowedUDPPorts;
});
2023-05-05 15:59:59 -05:00
};
environment.systemPackages = [
pkgs.pciutils
pkgs.tcpdump
];
};
}