2015-07-16 13:48:56 +00:00
|
|
|
arg@{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (pkgs) writeScript writeText;
|
2015-07-18 11:29:07 +00:00
|
|
|
|
2015-10-01 20:10:21 +00:00
|
|
|
inherit (builtins)
|
|
|
|
elem
|
|
|
|
;
|
|
|
|
|
2015-07-18 11:29:07 +00:00
|
|
|
inherit (lib)
|
|
|
|
concatMapStringsSep
|
|
|
|
concatStringsSep
|
|
|
|
attrNames
|
|
|
|
unique
|
|
|
|
fold
|
|
|
|
any
|
|
|
|
attrValues
|
|
|
|
catAttrs
|
|
|
|
filter
|
|
|
|
flatten
|
|
|
|
length
|
|
|
|
hasAttr
|
2016-04-12 13:09:08 +00:00
|
|
|
hasPrefix
|
2015-07-18 11:29:07 +00:00
|
|
|
mkEnableOption
|
|
|
|
mkOption
|
|
|
|
mkIf
|
|
|
|
types
|
2015-10-01 20:10:21 +00:00
|
|
|
sort
|
|
|
|
;
|
2015-07-16 13:48:56 +00:00
|
|
|
|
2015-10-01 20:10:21 +00:00
|
|
|
cfg = config.krebs.iptables;
|
2015-07-16 13:48:56 +00:00
|
|
|
|
|
|
|
out = {
|
2015-10-01 20:10:21 +00:00
|
|
|
options.krebs.iptables = api;
|
2015-07-16 13:48:56 +00:00
|
|
|
config = mkIf cfg.enable imp;
|
|
|
|
};
|
|
|
|
|
|
|
|
api = {
|
|
|
|
enable = mkEnableOption "iptables";
|
|
|
|
|
|
|
|
#tables.filter.INPUT = {
|
|
|
|
# policy = "DROP";
|
|
|
|
# rules = [
|
|
|
|
# { predicate = "-i retiolum"; target = "ACCEPT"; priority = -10; }
|
|
|
|
# ];
|
|
|
|
#};
|
|
|
|
#new api
|
|
|
|
tables = mkOption {
|
|
|
|
type = with types; attrsOf (attrsOf (submodule ({
|
|
|
|
options = {
|
|
|
|
policy = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "-";
|
|
|
|
};
|
|
|
|
rules = mkOption {
|
|
|
|
type = nullOr (listOf (submodule ({
|
|
|
|
options = {
|
|
|
|
predicate = mkOption {
|
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
target = mkOption {
|
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
precedence = mkOption {
|
|
|
|
type = int;
|
|
|
|
default = 0;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})));
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
imp = {
|
|
|
|
networking.firewall.enable = false;
|
|
|
|
|
2015-10-01 20:10:21 +00:00
|
|
|
systemd.services.krebs-iptables = {
|
|
|
|
description = "krebs-iptables";
|
2015-07-16 13:48:56 +00:00
|
|
|
wantedBy = [ "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";
|
2015-10-01 20:10:21 +00:00
|
|
|
ExecStart = "@${startScript} krebs-iptables_start";
|
2015-07-16 13:48:56 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-07-18 11:55:17 +00:00
|
|
|
#buildTable :: iptablesVersion -> iptablesAttrSet` -> str
|
2015-07-16 13:48:56 +00:00
|
|
|
#todo: differentiate by iptables-version
|
2015-07-18 11:55:17 +00:00
|
|
|
buildTables = v: ts:
|
2015-07-16 13:48:56 +00:00
|
|
|
let
|
2015-07-18 11:55:17 +00:00
|
|
|
|
2015-07-16 13:48:56 +00:00
|
|
|
declareChain = t: cn:
|
|
|
|
#TODO: find out what to do whit these count numbers
|
|
|
|
":${cn} ${t."${cn}".policy} [0:0]";
|
|
|
|
|
|
|
|
buildChain = tn: cn:
|
2015-07-18 12:30:29 +00:00
|
|
|
let
|
2015-07-23 00:18:45 +00:00
|
|
|
sortedRules = sort (a: b: a.precedence > b.precedence) ts."${tn}"."${cn}".rules;
|
2015-07-18 12:30:29 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
#TODO: double check should be unneccessary, refactor!
|
2015-10-01 20:10:21 +00:00
|
|
|
if ts.${tn}.${cn}.rules or null != null then
|
|
|
|
concatMapStringsSep "\n" (rule: "\n-A ${cn} ${rule}") ([]
|
|
|
|
++ map (buildRule tn cn) sortedRules
|
|
|
|
)
|
2015-07-16 13:48:56 +00:00
|
|
|
else
|
2015-07-18 12:30:29 +00:00
|
|
|
""
|
|
|
|
;
|
2015-07-16 13:48:56 +00:00
|
|
|
|
|
|
|
|
2015-07-18 11:55:56 +00:00
|
|
|
buildRule = tn: cn: rule:
|
|
|
|
#target validation test:
|
2016-04-12 13:09:08 +00:00
|
|
|
assert (elem rule.target ([ "ACCEPT" "REJECT" "DROP" "QUEUE" "LOG" "RETURN" ] ++ (attrNames ts."${tn}"))) || hasPrefix "REDIRECT" rule.target;
|
2015-07-16 13:48:56 +00:00
|
|
|
|
2015-07-18 11:55:56 +00:00
|
|
|
#predicate validation test:
|
2015-07-16 13:48:56 +00:00
|
|
|
#maybe use iptables-test
|
|
|
|
#TODO: howto exit with evaluation error by shellscript?
|
|
|
|
#apperantly not possible from nix because evalatution wouldn't be deterministic.
|
|
|
|
"${rule.predicate} -j ${rule.target}";
|
|
|
|
|
|
|
|
buildTable = tn:
|
|
|
|
"*${tn}\n" +
|
|
|
|
concatStringsSep "\n" ([]
|
|
|
|
++ map (declareChain ts."${tn}") (attrNames ts."${tn}")
|
|
|
|
) +
|
|
|
|
#this looks dirty, find a better way to do this (maybe optionalString)
|
|
|
|
concatStringsSep "" ([]
|
|
|
|
++ map (buildChain tn) (attrNames ts."${tn}")
|
|
|
|
) +
|
|
|
|
"\nCOMMIT";
|
|
|
|
in
|
|
|
|
concatStringsSep "\n" ([]
|
2015-07-18 12:30:29 +00:00
|
|
|
++ map buildTable (attrNames ts)
|
2015-07-16 13:48:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
#=====
|
|
|
|
|
|
|
|
rules4 = iptables-version:
|
|
|
|
let
|
|
|
|
#TODO: find out good defaults.
|
|
|
|
tables-defaults = {
|
|
|
|
nat.PREROUTING.policy = "ACCEPT";
|
|
|
|
nat.INPUT.policy = "ACCEPT";
|
|
|
|
nat.OUTPUT.policy = "ACCEPT";
|
|
|
|
nat.POSTROUTING.policy = "ACCEPT";
|
|
|
|
filter.INPUT.policy = "ACCEPT";
|
|
|
|
filter.FORWARD.policy = "ACCEPT";
|
|
|
|
filter.OUTPUT.policy = "ACCEPT";
|
|
|
|
|
|
|
|
#if someone specifies any other rules on this chain, the default rules get lost.
|
|
|
|
#is this wanted beahiviour or a bug?
|
|
|
|
#TODO: implement abstraction of rules
|
|
|
|
filter.INPUT.rules = [
|
|
|
|
{ predicate = "-m conntrack --ctstate RELATED,ESTABLISHED"; target = "ACCEPT"; }
|
|
|
|
];
|
|
|
|
};
|
|
|
|
tables = tables-defaults // cfg.tables;
|
|
|
|
|
|
|
|
in
|
2015-10-01 20:10:21 +00:00
|
|
|
writeText "krebs-iptables-rules${toString iptables-version}" ''
|
2015-07-16 13:48:56 +00:00
|
|
|
${buildTables iptables-version tables}
|
|
|
|
'';
|
|
|
|
|
2015-10-01 20:10:21 +00:00
|
|
|
startScript = writeScript "krebs-iptables_start" ''
|
2015-07-16 13:48:56 +00:00
|
|
|
#! /bin/sh
|
|
|
|
set -euf
|
|
|
|
iptables-restore < ${rules4 4}
|
|
|
|
ip6tables-restore < ${rules4 6}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
out
|
|
|
|
|