stockholm/tv/3modules/iptables.nix

126 lines
3.6 KiB
Nix
Raw Normal View History

2015-07-11 14:55:22 +00:00
{ config, lib, pkgs, ... }:
2016-02-14 15:43:44 +00:00
with config.krebs.lib;
2015-07-11 14:55:22 +00:00
let
cfg = config.tv.iptables;
out = {
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
};
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";
};
2015-07-11 14:55:22 +00:00
input-internet-accept-new-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 = [];
};
input-retiolum-accept-new-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 = [];
};
};
imp = {
networking.firewall.enable = false;
systemd.services.tv-iptables = {
description = "tv-iptables";
wantedBy = [ "network-pre.target" ];
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
};
};
};
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";
accept-new-tcp = port:
"-p tcp -m tcp --dport ${port} -m conntrack --ctstate NEW -j ACCEPT";
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]
${concatMapStringsSep "\n" (rule: "-A PREROUTING ${rule}") [
"! -i retiolum -p tcp -m tcp --dport 22 -j REDIRECT --to-ports 0"
"-p tcp -m tcp --dport 11423 -j REDIRECT --to-ports 22"
]}
${concatMapStringsSep "\n" (rule: "-A OUTPUT ${rule}") [
"-o lo -p tcp -m tcp --dport 11423 -j REDIRECT --to-ports 22"
]}
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]
${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
2015-07-19 14:12:21 +00:00
++ map accept-new-tcp (unique (map toString cfg.input-internet-accept-new-tcp))
2015-07-11 14:55:22 +00:00
++ ["-i retiolum -j Retiolum"]
)}
${concatMapStringsSep "\n" (rule: "-A Retiolum ${rule}") ([]
2016-02-07 02:09:14 +00:00
++ optional (cfg.accept-echo-request == "retiolum") accept-echo-request
2015-07-19 14:12:21 +00:00
++ map accept-new-tcp (unique (map toString cfg.input-retiolum-accept-new-tcp))
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"
)}
COMMIT
'';
in out
2015-07-11 14:55:22 +00:00
#let
# cfg = config.tv.iptables;
# arg' = arg // { inherit cfg; };
#in
#
#{
# options.tv.iptables = import ./options.nix arg';
# config = lib.mkIf cfg.enable (import ./config.nix arg');
#}