stockholm/tv/3modules/iptables.nix

209 lines
6.0 KiB
Nix
Raw Normal View History

2022-12-09 00:31:56 +00:00
with import ./lib;
{ config, pkgs, ... }: let {
2015-07-11 14:55:22 +00:00
cfg = config.tv.iptables;
2017-01-05 20:03:23 +00:00
body = {
2015-07-11 14:55:22 +00:00
options.tv.iptables = api;
2016-02-14 15:43:44 +00:00
config = lib.mkIf cfg.enable imp;
2015-07-11 14:55:22 +00:00
};
2019-02-10 13:22:54 +00:00
extraTypes = {
rules = types.submodule {
options = {
nat.OUTPUT = mkOption {
type = with types; listOf str;
default = [];
};
nat.PREROUTING = mkOption {
type = with types; listOf str;
default = [];
};
nat.POSTROUTING = mkOption {
type = with types; listOf str;
default = [];
};
filter.FORWARD = mkOption {
type = with types; listOf str;
default = [];
};
filter.INPUT = mkOption {
type = with types; listOf str;
default = [];
};
2019-02-10 13:36:31 +00:00
filter.Retiolum = mkOption {
type = with types; listOf str;
default = [];
};
2022-11-09 14:44:06 +00:00
filter.Wiregrill = mkOption {
type = with types; listOf str;
default = [];
};
2019-02-10 13:22:54 +00:00
};
};
};
2015-07-11 14:55:22 +00:00
api = {
2015-07-13 15:36:31 +00:00
enable = mkEnableOption "tv.iptables";
2015-07-11 14:55:22 +00:00
2016-02-07 02:09:14 +00:00
accept-echo-request = mkOption {
type = with types; nullOr (enum ["internet" "retiolum"]);
default = "retiolum";
};
2016-06-29 22:52:35 +00:00
input-internet-accept-tcp = mkOption {
2015-07-19 14:12:21 +00:00
type = with types; listOf (either int str);
2015-07-11 14:55:22 +00:00
default = [];
};
2016-06-29 22:52:35 +00:00
input-internet-accept-udp = mkOption {
type = with types; listOf (either int str);
default = [];
};
input-retiolum-accept-tcp = mkOption {
type = with types; listOf (either int str);
default = [];
};
input-retiolum-accept-udp = mkOption {
2015-07-19 14:12:21 +00:00
type = with types; listOf (either int str);
2015-07-11 14:55:22 +00:00
default = [];
};
2016-02-17 23:50:10 +00:00
2022-11-09 14:44:06 +00:00
input-wiregrill-accept-tcp = mkOption {
type = with types; listOf (either int str);
default = [];
};
input-wiregrill-accept-udp = mkOption {
type = with types; listOf (either int str);
default = [];
};
2019-02-10 13:22:54 +00:00
extra = mkOption {
default = {};
type = extraTypes.rules;
};
extra4 = mkOption {
default = {};
type = extraTypes.rules;
};
extra6 = mkOption {
default = {};
type = extraTypes.rules;
2016-02-17 23:50:10 +00:00
};
2015-07-11 14:55:22 +00:00
};
imp = {
networking.firewall.enable = false;
systemd.services.tv-iptables = {
wantedBy = [ "sysinit.target" ];
wants = [ "network-pre.target" ];
2015-07-11 14:55:22 +00:00
before = [ "network-pre.target" ];
after = [ "systemd-modules-load.service" ];
path = with pkgs; [
iptables
];
2015-07-27 00:02:34 +00:00
2015-07-11 14:55:22 +00:00
restartIfChanged = true;
2015-07-27 00:02:34 +00:00
2015-07-11 14:55:22 +00:00
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}
'';
2015-07-11 14:55:22 +00:00
};
unitConfig.DefaultDependencies = false;
2015-07-11 14:55:22 +00:00
};
};
2016-02-17 23:50:10 +00:00
formatTable = table:
(concatStringsSep "\n"
(mapAttrsToList
(chain: concatMapStringsSep "\n" (rule: "-A ${chain} ${rule}"))
table));
2015-07-11 14:55:22 +00:00
2016-02-07 02:09:14 +00:00
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";
2016-06-29 22:52:35 +00:00
accept-tcp = port: "-p tcp -m tcp --dport ${port} -j ACCEPT";
accept-udp = port: "-p udp -m udp --dport ${port} -j ACCEPT";
2016-02-07 02:09:14 +00:00
in
2015-07-11 14:55:22 +00:00
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]
2019-02-10 13:22:54 +00:00
${formatTable cfg."extra${toString iptables-version}".nat}
${formatTable cfg.extra.nat}
2015-07-11 14:55:22 +00:00
COMMIT
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
:Retiolum - [0:0]
2022-11-09 14:44:06 +00:00
:Wiregrill - [0:0]
2015-07-11 14:55:22 +00:00
${concatMapStringsSep "\n" (rule: "-A INPUT ${rule}") ([]
++ [
"-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"
"-i lo -j ACCEPT"
]
2016-02-07 02:09:14 +00:00
++ optional (cfg.accept-echo-request == "internet") accept-echo-request
2016-06-29 22:52:35 +00:00
++ map accept-tcp (unique (map toString cfg.input-internet-accept-tcp))
++ map accept-udp (unique (map toString cfg.input-internet-accept-udp))
2015-07-11 14:55:22 +00:00
++ ["-i retiolum -j Retiolum"]
2022-11-09 14:44:06 +00:00
++ ["-i wiregrill -j Wiregrill"]
2015-07-11 14:55:22 +00:00
)}
2016-02-17 23:50:10 +00:00
${formatTable cfg.extra.filter}
2019-02-10 13:22:54 +00:00
${formatTable cfg."extra${toString iptables-version}".filter}
2015-07-11 14:55:22 +00:00
${concatMapStringsSep "\n" (rule: "-A Retiolum ${rule}") ([]
2016-02-07 02:09:14 +00:00
++ optional (cfg.accept-echo-request == "retiolum") accept-echo-request
2016-06-29 22:52:35 +00:00
++ map accept-tcp (unique (map toString cfg.input-retiolum-accept-tcp))
++ map accept-udp (unique (map toString cfg.input-retiolum-accept-udp))
2015-07-11 14:55:22 +00:00
++ {
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"
)}
2022-11-09 14:44:06 +00:00
${concatMapStringsSep "\n" (rule: "-A Wiregrill ${rule}") ([]
++ optional (cfg.accept-echo-request == "wiregrill") accept-echo-request
++ map accept-tcp (unique (map toString cfg.input-wiregrill-accept-tcp))
++ map accept-udp (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"
)}
2015-07-11 14:55:22 +00:00
COMMIT
'';
2017-01-05 20:03:23 +00:00
}