diff options
| author | tv <tv@krebsco.de> | 2026-11-16 15:41:00 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-16 16:42:36 +0100 |
| commit | 0eab39238ffa04d16c1fab1013e6c4463910121a (patch) | |
| tree | d152d69d412972e56f3a0a08e8d34de297b2068e /modules | |
| parent | 9c0420ad860abef5285b2b099815b8dcd3c8bf5e (diff) | |
hrm.nftables: init
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/nftables.nix | 336 |
1 files changed, 336 insertions, 0 deletions
diff --git a/modules/nftables.nix b/modules/nftables.nix new file mode 100644 index 0000000..1eda00a --- /dev/null +++ b/modules/nftables.nix @@ -0,0 +1,336 @@ +{ config, lib, ... }: let + cfg = config.hrm.nftables; + + # A port is a number, a service name from /etc/services, or a range like + # "60000-61000". + port = lib.types.either lib.types.port lib.types.str; + + ruleType = lib.types.submodule ({ name, ... }: { + options = { + enable = lib.mkOption { + type = lib.types.bool; + default = true; + }; + order = lib.mkOption { + type = lib.types.int; + default = 1000; + description = "Rules are sorted by order, then by name."; + }; + text = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = "A rule in nft syntax, instead of the fields below."; + }; + iifname = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + }; + oifname = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + }; + saddr = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + description = "IPv4 and IPv6 addresses; each family gets its own nft rule."; + }; + daddr = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + description = "IPv4 and IPv6 addresses; each family gets its own nft rule."; + }; + ct.state = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + example = [ "established" "related" ]; + }; + icmp.type = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + }; + icmpv6.type = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + }; + tcp.dport = lib.mkOption { + type = lib.types.listOf port; + default = []; + }; + udp.dport = lib.mkOption { + type = lib.types.listOf port; + default = []; + }; + verdict = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + example = "jump reject-all"; + description = '' + The statement at the end of the rule, like accept, drop, reject, + masquerade, "jump reject-all" or "redirect to :22". + ''; + }; + }; + }); + + chainType = lib.types.submodule { + options = { + type = lib.mkOption { + type = lib.types.nullOr (lib.types.enum [ "filter" "nat" "route" ]); + default = null; + description = "Type of a base chain; null for a regular chain."; + }; + hook = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + example = "input"; + }; + priority = lib.mkOption { + type = lib.types.nullOr (lib.types.either lib.types.int lib.types.str); + default = null; + example = "filter"; + }; + policy = lib.mkOption { + type = lib.types.nullOr (lib.types.enum [ "accept" "drop" ]); + default = null; + }; + rules = lib.mkOption { + type = lib.types.attrsOf ruleType; + default = {}; + }; + }; + }; + + tableType = lib.types.submodule { + options = { + family = lib.mkOption { + type = lib.types.enum [ "ip" "ip6" "inet" "arp" "bridge" "netdev" ]; + default = "inet"; + }; + chains = lib.mkOption { + type = lib.types.attrsOf chainType; + default = {}; + }; + }; + }; + + zoneType = lib.types.submodule { + options = { + tcp = lib.mkOption { + type = lib.types.listOf port; + default = []; + description = "TCP ports to accept."; + }; + udp = lib.mkOption { + type = lib.types.listOf port; + default = []; + description = "UDP ports to accept."; + }; + echo-request = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Whether to accept ICMP and ICMPv6 echo requests."; + }; + }; + }; + + renderSet = xs: let + xs' = lib.unique (map toString xs); + in + if lib.length xs' == 1 + then lib.head xs' + else "{ ${lib.concatStringsSep ", " xs'} }"; + + isIPv6 = lib.hasInfix ":"; + + # A rule becomes one nft rule per address family and protocol it needs: + # addresses and ICMP types are family-specific, ports protocol-specific. + renderRule = rule: let + familySpecific = + rule.saddr != [] || rule.daddr != [] || + rule.icmp.type != [] || rule.icmpv6.type != []; + families = + if familySpecific then [ + { name = "ip"; isMember = x: !isIPv6 x; icmp = [ "icmp" rule.icmp.type ]; } + { name = "ip6"; isMember = isIPv6; icmp = [ "icmpv6" rule.icmpv6.type ]; } + ] else [ + null + ]; + protocols = + lib.optional (rule.tcp.dport != []) [ "tcp" rule.tcp.dport ] + ++ lib.optional (rule.udp.dport != []) [ "udp" rule.udp.dport ]; + render = family: protocol: let + saddr = lib.filter family.isMember rule.saddr; + daddr = lib.filter family.isMember rule.daddr; + icmp = lib.elemAt family.icmp 1; + hasICMP = rule.icmp.type != [] || rule.icmpv6.type != []; + applies = + family == null || ( + (rule.saddr == [] || saddr != []) && + (rule.daddr == [] || daddr != []) && + (!hasICMP || icmp != []) + ); + in + lib.optional applies (lib.concatStringsSep " " ( + lib.optional (rule.iifname != null) "iifname \"${rule.iifname}\"" + ++ lib.optional (rule.oifname != null) "oifname \"${rule.oifname}\"" + ++ lib.optional (family != null && saddr != []) "${family.name} saddr ${renderSet saddr}" + ++ lib.optional (family != null && daddr != []) "${family.name} daddr ${renderSet daddr}" + ++ lib.optional (rule.ct.state != []) "ct state ${renderSet rule.ct.state}" + ++ lib.optional (family != null && icmp != []) "${lib.head family.icmp} type ${renderSet icmp}" + ++ lib.optional (protocol != null) "${lib.head protocol} dport ${renderSet (lib.elemAt protocol 1)}" + ++ lib.optional (rule.verdict != null) rule.verdict + )); + in + if rule.text != null + then [ rule.text ] + else lib.concatMap (family: + lib.concatMap (render family) (if protocols == [] then [ null ] else protocols) + ) families; + + renderChain = name: chain: let + rules = + lib.sort + (a: b: if a.order != b.order then a.order < b.order else a.name < b.name) + (lib.filter (rule: rule.enable) + (lib.mapAttrsToList (name: rule: rule // { inherit name; }) chain.rules)); + in + lib.concatStringsSep "\n" ( + [ "chain ${name} {" ] + ++ lib.optional (chain.hook != null) " ${lib.concatStringsSep " " ( + [ "type ${chain.type} hook ${chain.hook} priority ${toString chain.priority};" ] + ++ lib.optional (chain.policy != null) "policy ${chain.policy};" + )}" + ++ map (line: " ${line}") (lib.concatMap renderRule rules) + ++ [ "}" ] + ); + + # Rules accepting what a zone accepts. + zoneRules = prefix: zone: { + "${prefix}echo-request" = { + enable = zone.echo-request; + icmp.type = [ "echo-request" ]; + icmpv6.type = [ "echo-request" ]; + verdict = "accept"; + }; + "${prefix}tcp" = { + enable = zone.tcp != []; + tcp.dport = zone.tcp; + verdict = "accept"; + }; + "${prefix}udp" = { + enable = zone.udp != []; + udp.dport = zone.udp; + verdict = "accept"; + }; + }; + + # Interface zones, i.e. all zones but "internet", get their own chain. + interfaceZones = lib.removeAttrs cfg.input [ "internet" ]; +in { + options.hrm.nftables = { + enable = lib.mkEnableOption "hrm.nftables"; + + tables = lib.mkOption { + type = lib.types.attrsOf tableType; + default = {}; + description = '' + nftables tables, rendered to networking.nftables.tables. + ''; + }; + + input = lib.mkOption { + type = lib.types.attrsOf zoneType; + default = {}; + example = { + internet.tcp = [ "ssh" ]; + retiolum.tcp = [ "http" ]; + }; + description = '' + What to accept on input. "internet" applies to every interface; + any other name is an interface, whose traffic goes to a chain of the + same name, which rejects what it doesn't accept. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + networking.firewall.enable = false; + networking.nftables.enable = true; + + networking.nftables.tables = + lib.mapAttrs (_: table: { + inherit (table) family; + content = lib.concatStringsSep "\n" (lib.mapAttrsToList renderChain table.chains); + }) cfg.tables; + + hrm.nftables.tables.filter.chains = { + input = { + type = "filter"; + hook = "input"; + priority = "filter"; + policy = "drop"; + rules = { + established = { + order = 100; + ct.state = [ "established" "related" ]; + verdict = "accept"; + }; + loopback = { + order = 100; + iifname = "lo"; + verdict = "accept"; + }; + # IPv6 needs neighbor discovery. + neighbor-discovery = { + order = 100; + icmpv6.type = [ "nd-neighbor-solicit" "nd-neighbor-advert" "nd-router-advert" ]; + verdict = "accept"; + }; + } + // zoneRules "internet-" (cfg.input.internet or { tcp = []; udp = []; echo-request = false; }) + // lib.mapAttrs' (name: _: lib.nameValuePair "jump-${name}" { + order = 1400; + iifname = name; + verdict = "jump ${name}"; + }) interfaceZones; + }; + forward = { + type = "filter"; + hook = "forward"; + priority = "filter"; + policy = "drop"; + }; + reject-all.rules = { + tcp = { order = 100; text = "meta l4proto tcp reject with tcp reset"; }; + udp = { order = 200; text = "meta l4proto udp reject with icmpx type port-unreachable"; }; + ipv4 = { order = 300; text = "meta nfproto ipv4 reject with icmp type prot-unreachable"; }; + other = { order = 400; verdict = "reject"; }; + }; + } // lib.mapAttrs (_: zone: { + rules = zoneRules "" zone // { + reject = { + order = 2000; + verdict = "jump reject-all"; + }; + }; + }) interfaceZones; + + hrm.nftables.tables.nat.chains = { + prerouting = { + type = "nat"; + hook = "prerouting"; + priority = "dstnat"; + }; + output = { + type = "nat"; + hook = "output"; + priority = "dstnat"; + }; + postrouting = { + type = "nat"; + hook = "postrouting"; + priority = "srcnat"; + }; + }; + }; +} |
