feat(nftables): lan rules

This commit is contained in:
arcnmx 2024-01-24 12:59:28 -08:00
parent 59c61d021e
commit c321f3a502
6 changed files with 64 additions and 19 deletions

View file

@ -6,6 +6,7 @@
inherit (lib.modules) mkIf mkMerge mkDefault mkOptionDefault; inherit (lib.modules) mkIf mkMerge mkDefault mkOptionDefault;
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.lists) optionals; inherit (lib.lists) optionals;
inherit (lib.strings) concatStringsSep;
inherit (config.services) tailscale avahi; inherit (config.services) tailscale avahi;
inherit (config) networking; inherit (config) networking;
inherit (networking) hostName; inherit (networking) hostName;
@ -83,4 +84,15 @@ in {
}; };
}; };
}; };
config.networking.firewall = {
interfaces.local = {
nftables.conditions = [
"ip saddr { ${concatStringsSep ", " networking.access.cidrForNetwork.local.v4} }"
(mkIf networking.enableIPv6
"ip6 saddr { ${concatStringsSep ", " networking.access.cidrForNetwork.local.v6} }"
)
];
};
};
} }

View file

@ -1,6 +1,11 @@
{ pkgs, lib, config, modulesPath, ... }: { lib, config, ... }:
let let
inherit (lib) types;
inherit (lib.options) mkOption;
inherit (lib.modules) mkIf;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.strings) optionalString concatStringsSep concatMapStringsSep;
fwcfg = config.networking.firewall; fwcfg = config.networking.firewall;
cfg = config.networking.nftables; cfg = config.networking.nftables;
@ -9,8 +14,8 @@ let
mkPorts = cond: ports: ranges: action: let mkPorts = cond: ports: ranges: action: let
portStrings = (map (range: "${toString range.from}-${toString range.to}") ranges) portStrings = (map (range: "${toString range.from}-${toString range.to}") ranges)
++ (map toString ports); ++ (map toString ports);
in lib.optionalString (portStrings != []) '' in optionalString (portStrings != []) ''
${cond} dport { ${lib.concatStringsSep ", " portStrings} } ${action} ${cond} dport { ${concatStringsSep "," portStrings} } ${action}
''; '';
ruleset = '' ruleset = ''
@ -26,17 +31,17 @@ let
ct state established,related accept ct state established,related accept
iifname { ${ iifname { ${
lib.concatStringsSep "," (["lo"] ++ fwcfg.trustedInterfaces) concatStringsSep "," (["lo"] ++ fwcfg.trustedInterfaces)
} } accept } } accept
${mkPorts "tcp" fwcfg.allowedTCPPorts fwcfg.allowedTCPPortRanges "accept"} ${mkPorts "tcp" fwcfg.allowedTCPPorts fwcfg.allowedTCPPortRanges "accept"}
${mkPorts "udp" fwcfg.allowedUDPPorts fwcfg.allowedUDPPortRanges "accept"} ${mkPorts "udp" fwcfg.allowedUDPPorts fwcfg.allowedUDPPortRanges "accept"}
${ ${
lib.concatStringsSep "\n" (lib.mapAttrsToList (name: ifcfg: concatStringsSep "\n" (mapAttrsToList (name: ifcfg: concatMapStringsSep "\n" (cond:
mkPorts "iifname ${name} tcp" ifcfg.allowedTCPPorts ifcfg.allowedTCPPortRanges "accept" mkPorts "${cond} tcp" ifcfg.allowedTCPPorts ifcfg.allowedTCPPortRanges "accept"
+ mkPorts "iifname ${name} udp" ifcfg.allowedUDPPorts ifcfg.allowedUDPPortRanges "accept" + mkPorts "${cond} udp" ifcfg.allowedUDPPorts ifcfg.allowedUDPPortRanges "accept"
) fwcfg.interfaces) ) ifcfg.nftables.conditions) fwcfg.interfaces)
} }
# DHCPv6 # DHCPv6
@ -58,7 +63,7 @@ let
type filter hook forward priority filter type filter hook forward priority filter
policy ${cfg.forwardPolicy} policy ${cfg.forwardPolicy}
${lib.optionalString doDocker '' ${optionalString doDocker ''
oifname docker0 ct state invalid drop oifname docker0 ct state invalid drop
oifname docker0 ct state established,related accept oifname docker0 ct state established,related accept
iifname docker0 accept iifname docker0 accept
@ -69,7 +74,7 @@ let
counter counter
} }
} }
${lib.optionalString doDocker '' ${optionalString doDocker ''
table ip nat { table ip nat {
chain docker-postrouting { chain docker-postrouting {
type nat hook postrouting priority 10 type nat hook postrouting priority 10
@ -79,9 +84,17 @@ let
''} ''}
${cfg.extraConfig} ${cfg.extraConfig}
''; '';
interfaceModule = { config, name, ... }: {
options = {
nftables.conditions = mkOption {
type = types.listOf types.str;
default = "iifname ${name}";
};
};
};
in { in {
options = with lib; { options = {
networking.nftables = { networking.nftables = {
extraConfig = mkOption { extraConfig = mkOption {
type = types.lines; type = types.lines;
@ -116,15 +129,18 @@ in {
default = true; default = true;
}; };
}; };
networking.firewall.interfaces = mkOption {
type = types.attrsOf (types.submodule interfaceModule);
};
}; };
config = lib.mkIf cfg.enable { config = mkIf cfg.enable {
networking.firewall.enable = false; networking.firewall.enable = false;
networking.nftables = { networking.nftables = {
inherit ruleset; inherit ruleset;
}; };
virtualisation.docker = lib.mkIf doDocker { virtualisation.docker = mkIf doDocker {
extraOptions = "--iptables=false"; extraOptions = "--iptables=false";
}; };
}; };

View file

@ -26,7 +26,10 @@ in with lib; {
LogLevel = "VERBOSE"; LogLevel = "VERBOSE";
}; };
}; };
networking.firewall.allowedTCPPorts = [publicPort]; networking.firewall = {
allowedTCPPorts = [publicPort];
interfaces.local.allowedTCPPorts = [ 22 ];
};
programs.mosh.enable = true; programs.mosh.enable = true;
} }

View file

@ -22,7 +22,6 @@ in {
services.home-assistant = { services.home-assistant = {
enable = mkDefault true; enable = mkDefault true;
openFirewall = mkDefault true;
mutableUiConfig = mkDefault true; mutableUiConfig = mkDefault true;
domain = mkDefault "home.${config.networking.domain}"; domain = mkDefault "home.${config.networking.domain}";
config = { config = {

View file

@ -30,7 +30,6 @@ in {
persistence = mkDefault true; persistence = mkDefault true;
listeners = [ listeners = [
{ {
openFirewall = mkDefault true;
acl = [ acl = [
"pattern readwrite #" "pattern readwrite #"
]; ];

View file

@ -1,7 +1,12 @@
{ {
config,
lib,
meta, meta,
... ...
}: { }: let
inherit (lib.modules) mkIf mkMerge;
inherit (config.services) kanidm mosquitto home-assistant;
in {
imports = let imports = let
inherit (meta) nixos; inherit (meta) nixos;
in [ in [
@ -24,8 +29,19 @@
sops.defaultSopsFile = ./secrets.yaml; sops.defaultSopsFile = ./secrets.yaml;
services.kanidm = { networking.firewall = {
server.openFirewall = true; interfaces.local.allowedTCPPorts = mkMerge [
(mkIf kanidm.enableServer [
kanidm.server.frontend.port
(mkIf kanidm.server.ldap.enable kanidm.server.ldap.port)
])
(mkIf home-assistant.enable [
home-assistant.config.http.server_port
])
(mkIf mosquitto.enable (map (listener:
listener.port
) mosquitto.listeners))
];
}; };
systemd.network.networks.eth0 = { systemd.network.networks.eth0 = {