diff options
Diffstat (limited to 'modules/iptables.nix')
| -rw-r--r-- | modules/iptables.nix | 82 |
1 files changed, 81 insertions, 1 deletions
diff --git a/modules/iptables.nix b/modules/iptables.nix index e2fdcbc..0a8ee20 100644 --- a/modules/iptables.nix +++ b/modules/iptables.nix @@ -3,7 +3,10 @@ body = { options.tv.iptables = api; - config = lib.mkIf cfg.enable imp; + config = lib.mkIf cfg.enable (lib.mkMerge [ + (lib.mkIf (!config.hrm.nftables.enable) imp) + (lib.mkIf config.hrm.nftables.enable nftables) + ]); }; extraTypes = { @@ -204,4 +207,81 @@ )} COMMIT ''; + + # With hrm.nftables enabled, the options configure hrm.nftables instead of + # iptables. Rules in tv.iptables.extra{,4,6} are in iptables syntax, which + # iptables-restore-translate turns into nftables syntax at build time. + nftables = { + hrm.nftables.input = { + internet = { + tcp = cfg.input-internet-accept-tcp; + udp = cfg.input-internet-accept-udp; + echo-request = cfg.accept-echo-request == "internet"; + }; + retiolum = { + tcp = cfg.input-retiolum-accept-tcp; + udp = cfg.input-retiolum-accept-udp; + echo-request = cfg.accept-echo-request == "retiolum"; + }; + wiregrill = { + tcp = cfg.input-wiregrill-accept-tcp; + udp = cfg.input-wiregrill-accept-udp; + }; + }; + + hrm.nftables.tables = { + filter.chains = { + input.rules = extraRules 1500 "filter" "INPUT"; + forward.rules = extraRules 1000 "filter" "FORWARD"; + retiolum.rules = extraRules 900 "filter" "Retiolum"; + wiregrill.rules = extraRules 900 "filter" "Wiregrill"; + }; + nat.chains = { + prerouting.rules = extraRules 1000 "nat" "PREROUTING"; + output.rules = extraRules 1000 "nat" "OUTPUT"; + postrouting.rules = extraRules 1000 "nat" "POSTROUTING"; + }; + }; + }; + + # Rules for an iptables chain, as nft include statements of translated files. + # In one inet table, rules from extra apply to both families, so they keep + # their place relative to extra4 and extra6: after them in nat, before them + # in filter, like in the separate iptables and ip6tables tables. + extraRules = order: table: chain: let + rule = position: tool: rules: { + enable = rules != []; + order = order + position; + text = translate tool table chain rules; + }; + nat = table == "nat"; + in { + tv-iptables-extra = rule (if nat then 2 else 0) "iptables" cfg.extra.${table}.${chain}; + tv-iptables-extra4 = rule (if nat then 0 else 1) "iptables" cfg.extra4.${table}.${chain}; + tv-iptables-extra6 = rule (if nat then 1 else 2) "ip6tables" cfg.extra6.${table}.${chain}; + }; + + translate = tool: table: chain: rules: let + input = pkgs.writeText "tv-iptables-${tool}-${table}-${chain}" '' + *${table} + ${lib.optionalString (table == "filter") '' + :Retiolum - [0:0] + :Wiregrill - [0:0] + ''} + ${lib.concatMapStrings (rule: "-A ${chain} ${rule}\n") rules} + COMMIT + ''; + output = pkgs.runCommand "tv-iptables-${tool}-${table}-${chain}.nft" { + nativeBuildInputs = [ pkgs.iptables ]; + # Service names like "smtp" need the host's /etc/services. + NIX_REDIRECTS = "/etc/services=${config.environment.etc.services.source}"; + LD_PRELOAD = "${pkgs.libredirect}/lib/libredirect.so"; + } '' + ${tool}-restore-translate -f ${input} > translated + sed -n 's/^add rule [a-z0-9]* ${table} ${chain} \(.*\)/\1/p' translated | + sed 's/ counter / /' > $out + test "$(wc -l < $out)" = ${toString (lib.length rules)} + ''; + in + "include \"${output}\""; } |
