From 039d467435e4390fc41e8265da7f34a0caf2443c Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 3 Dec 2026 03:08:53 +0000 Subject: tv.iptables: drop --- modules/iptables.nix | 301 --------------------------------------------------- 1 file changed, 301 deletions(-) delete mode 100644 modules/iptables.nix (limited to 'modules') diff --git a/modules/iptables.nix b/modules/iptables.nix deleted file mode 100644 index f2a719bb..00000000 --- a/modules/iptables.nix +++ /dev/null @@ -1,301 +0,0 @@ -{ buildSystem, config, lib, pkgs, ... }: let { - cfg = config.tv.iptables; - - body = { - options.tv.iptables = api; - config = lib.mkIf cfg.enable (lib.mkMerge [ - (lib.mkIf (!config.hrm.nftables.enable) imp) - (lib.mkIf config.hrm.nftables.enable nftables) - ]); - }; - - extraTypes = { - rules = lib.types.submodule { - options = { - nat.OUTPUT = lib.mkOption { - type = with lib.types; listOf str; - default = []; - }; - nat.PREROUTING = lib.mkOption { - type = with lib.types; listOf str; - default = []; - }; - nat.POSTROUTING = lib.mkOption { - type = with lib.types; listOf str; - default = []; - }; - filter.FORWARD = lib.mkOption { - type = with lib.types; listOf str; - default = []; - }; - filter.INPUT = lib.mkOption { - type = with lib.types; listOf str; - default = []; - }; - filter.Retiolum = lib.mkOption { - type = with lib.types; listOf str; - default = []; - }; - filter.Wiregrill = lib.mkOption { - type = with lib.types; listOf str; - default = []; - }; - }; - }; - }; - - api = { - enable = lib.mkEnableOption "tv.iptables"; - - accept-echo-request = lib.mkOption { - type = with lib.types; nullOr (enum ["internet" "retiolum"]); - default = "retiolum"; - }; - - input-internet-accept-tcp = lib.mkOption { - type = with lib.types; listOf (either int str); - default = []; - }; - - input-internet-accept-udp = lib.mkOption { - type = with lib.types; listOf (either int str); - default = []; - }; - - input-retiolum-accept-tcp = lib.mkOption { - type = with lib.types; listOf (either int str); - default = []; - }; - - input-retiolum-accept-udp = lib.mkOption { - type = with lib.types; listOf (either int str); - default = []; - }; - - input-wiregrill-accept-tcp = lib.mkOption { - type = with lib.types; listOf (either int str); - default = []; - }; - - input-wiregrill-accept-udp = lib.mkOption { - type = with lib.types; listOf (either int str); - default = []; - }; - - extra = lib.mkOption { - default = {}; - type = extraTypes.rules; - }; - - extra4 = lib.mkOption { - default = {}; - type = extraTypes.rules; - }; - - extra6 = lib.mkOption { - default = {}; - type = extraTypes.rules; - }; - }; - - imp = { - networking.firewall.enable = false; - - systemd.services.tv-iptables = { - wantedBy = [ "sysinit.target" ]; - wants = [ "network-pre.target" ]; - before = [ "network-pre.target" ]; - after = [ "systemd-modules-load.service" ]; - - path = with pkgs; [ - iptables - ]; - - restartIfChanged = true; - - serviceConfig = { - Type = "simple"; - RemainAfterExit = true; - Restart = "always"; - SyslogIdentifier = "tv-iptables_start"; - ExecStart = pkgs.writeDash "tv-iptables_start" '' - set -euf - iptables-restore < ${rules 4} - ip6tables-restore < ${rules 6} - ''; - }; - - unitConfig.DefaultDependencies = false; - }; - }; - - formatTable = table: - (lib.concatStringsSep "\n" - (lib.mapAttrsToList - (chain: lib.concatMapStringsSep "\n" (rule: "-A ${chain} ${rule}")) - table)); - - rules = iptables-version: let - accept-echo-request = { - ip4tables = "-p icmp -m icmp --icmp-type echo-request -j ACCEPT"; - ip6tables = "-p ipv6-icmp -m icmp6 --icmpv6-type echo-request -j ACCEPT"; - }."ip${toString iptables-version}tables"; - accept-tcp = port: "-p tcp -m tcp --dport ${port} -j ACCEPT"; - accept-udp = port: "-p udp -m udp --dport ${port} -j ACCEPT"; - in - pkgs.writeText "tv-iptables-rules${toString iptables-version}" '' - *nat - :PREROUTING ACCEPT [0:0] - :INPUT ACCEPT [0:0] - :OUTPUT ACCEPT [0:0] - :POSTROUTING ACCEPT [0:0] - ${formatTable cfg."extra${toString iptables-version}".nat} - ${formatTable cfg.extra.nat} - COMMIT - *filter - :INPUT DROP [0:0] - :FORWARD DROP [0:0] - :OUTPUT ACCEPT [0:0] - :Retiolum - [0:0] - :Wiregrill - [0:0] - ${lib.concatMapStringsSep "\n" (rule: "-A INPUT ${rule}") ([] - ++ [ - "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT" - "-i lo -j ACCEPT" - ] - ++ lib.optional (cfg.accept-echo-request == "internet") accept-echo-request - ++ map accept-tcp (lib.unique (map toString cfg.input-internet-accept-tcp)) - ++ map accept-udp (lib.unique (map toString cfg.input-internet-accept-udp)) - ++ ["-i retiolum -j Retiolum"] - ++ ["-i wiregrill -j Wiregrill"] - )} - ${formatTable cfg.extra.filter} - ${formatTable cfg."extra${toString iptables-version}".filter} - ${lib.concatMapStringsSep "\n" (rule: "-A Retiolum ${rule}") ([] - ++ lib.optional (cfg.accept-echo-request == "retiolum") accept-echo-request - ++ map accept-tcp (lib.unique (map toString cfg.input-retiolum-accept-tcp)) - ++ map accept-udp (lib.unique (map toString cfg.input-retiolum-accept-udp)) - ++ { - ip4tables = [ - "-p tcp -j REJECT --reject-with tcp-reset" - "-p udp -j REJECT --reject-with icmp-port-unreachable" - "-j REJECT --reject-with icmp-proto-unreachable" - ]; - ip6tables = [ - "-p tcp -j REJECT --reject-with tcp-reset" - "-p udp -j REJECT --reject-with icmp6-port-unreachable" - "-j REJECT" - ]; - }."ip${toString iptables-version}tables" - )} - ${lib.concatMapStringsSep "\n" (rule: "-A Wiregrill ${rule}") ([] - ++ lib.optional (cfg.accept-echo-request == "wiregrill") accept-echo-request - ++ map accept-tcp (lib.unique (map toString cfg.input-wiregrill-accept-tcp)) - ++ map accept-udp (lib.unique (map toString cfg.input-wiregrill-accept-udp)) - ++ { - ip4tables = [ - "-p tcp -j REJECT --reject-with tcp-reset" - "-p udp -j REJECT --reject-with icmp-port-unreachable" - "-j REJECT --reject-with icmp-proto-unreachable" - ]; - ip6tables = [ - "-p tcp -j REJECT --reject-with tcp-reset" - "-p udp -j REJECT --reject-with icmp6-port-unreachable" - "-j REJECT" - ]; - }."ip${toString iptables-version}tables" - )} - 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. - # iptables applies extra to both families and extra4 or extra6 to one. In - # one inet table, each family gets its own copy, restricted by meta nfproto, - # in iptables' order: extra before the family's rules in filter, after them - # in nat. - extraRules = order: table: chain: let - rule = family: tool: let - common = cfg.extra.${table}.${chain}; - specific = cfg."extra${family}".${table}.${chain}; - rules = if table == "nat" then specific ++ common else common ++ specific; - in { - enable = rules != []; - inherit order; - text = translate tool "ipv${family}" table chain rules; - }; - in { - tv-iptables-ipv4 = rule "4" "iptables"; - tv-iptables-ipv6 = rule "6" "ip6tables"; - }; - - # The translation is text, so the system that builds can make it with its - # own iptables. Emulated, the tool crashes for lack of netlink. - buildPkgs = - if buildSystem == pkgs.stdenv.hostPlatform.system - then pkgs - else import pkgs.path { system = buildSystem; }; - - translate = tool: nfproto: 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 = buildPkgs.runCommand "tv-iptables-${tool}-${table}-${chain}.nft" { - nativeBuildInputs = [ buildPkgs.iptables ]; - # Service names like "smtp" and host names like "leg.r" need the - # host's /etc/services and /etc/hosts. - NIX_REDIRECTS = lib.concatStringsSep ":" [ - "/etc/hosts=${config.environment.etc.hosts.source}" - "/etc/services=${config.environment.etc.services.source}" - ]; - LD_PRELOAD = "${buildPkgs.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 / /; s/^/meta nfproto ${nfproto} /" > $out - test "$(wc -l < $out)" = ${toString (lib.length rules)} - ''; - in - "include \"${output}\""; -} -- cgit v1.3.1 [cgit] Unable to lock slot /tmp/cgit/66100000.lock: Permission denied (13)