2023-08-08 00:03:49 +02:00
|
|
|
## Firewall
|
|
|
|
## ========
|
|
|
|
##
|
|
|
|
## Provides a service to create an nftables ruleset based on
|
|
|
|
## configuration supplied to it.
|
|
|
|
|
2023-07-16 17:55:50 +02:00
|
|
|
{ lib, pkgs, config, ...}:
|
|
|
|
let
|
|
|
|
inherit (lib) mkOption types;
|
2023-08-05 13:07:35 +02:00
|
|
|
inherit (pkgs) liminix;
|
2023-07-16 17:55:50 +02:00
|
|
|
inherit (pkgs.liminix.services) oneshot;
|
|
|
|
|
|
|
|
kconf = isModule :
|
|
|
|
# setting isModule false is utterly untested and mostly
|
|
|
|
# unimplemented: I say this to preempt any "how on earth is this
|
|
|
|
# even supposed to work?" questions
|
|
|
|
let yes = if isModule then "m" else "y";
|
|
|
|
in {
|
|
|
|
NFT_FIB_IPV4 = yes;
|
|
|
|
NFT_FIB_IPV6 = yes;
|
|
|
|
NF_TABLES = yes;
|
|
|
|
NF_CT_PROTO_DCCP = "y";
|
|
|
|
NF_CT_PROTO_SCTP = "y";
|
|
|
|
NF_CT_PROTO_UDPLITE = "y";
|
|
|
|
# NF_CONNTRACK_FTP = yes;
|
|
|
|
NFT_CT = yes;
|
|
|
|
};
|
|
|
|
kmodules = pkgs.kernel-modules.override {
|
|
|
|
kernelSrc = config.system.outputs.kernel.src;
|
|
|
|
modulesupport = config.system.outputs.kernel.modulesupport;
|
|
|
|
targets = [
|
|
|
|
"nft_fib_ipv4"
|
|
|
|
"nft_fib_ipv6"
|
|
|
|
];
|
|
|
|
kconfig = kconf true;
|
|
|
|
};
|
|
|
|
loadModules = oneshot {
|
|
|
|
name = "firewall-modules";
|
|
|
|
up = "sh ${kmodules}/load.sh";
|
|
|
|
down = "sh ${kmodules}/unload.sh";
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
system.service.firewall = mkOption {
|
2023-08-05 13:07:35 +02:00
|
|
|
type = liminix.lib.types.serviceDefn;
|
2023-07-16 17:55:50 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
config = {
|
2023-08-05 13:07:35 +02:00
|
|
|
system.service.firewall =
|
|
|
|
let svc = liminix.callService ./service.nix {
|
|
|
|
ruleset = mkOption {
|
|
|
|
type = types.attrsOf types.attrs; # we could usefully tighten this a bit :-)
|
|
|
|
description = "firewall ruleset";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in svc // {
|
2024-02-07 17:21:14 +01:00
|
|
|
build = args :
|
|
|
|
let args' = args // {
|
|
|
|
dependencies = (args.dependencies or []) ++ [loadModules];
|
|
|
|
};
|
|
|
|
in svc.build args' ;
|
2023-08-05 13:07:35 +02:00
|
|
|
};
|
2023-07-16 17:55:50 +02:00
|
|
|
|
2023-07-16 18:04:19 +02:00
|
|
|
# For historical reasons the kernel config is split between
|
|
|
|
# monolithic options and modules. TODO: go through this list
|
|
|
|
# and see what can be moved into the "kconf" definiton above
|
2023-07-16 17:55:50 +02:00
|
|
|
kernel.config = {
|
|
|
|
NETFILTER_XT_MATCH_CONNTRACK = "y";
|
|
|
|
|
2023-07-16 18:04:19 +02:00
|
|
|
IP6_NF_IPTABLES= "y";
|
|
|
|
IP_NF_IPTABLES= "y";
|
2023-07-16 17:55:50 +02:00
|
|
|
|
|
|
|
IP_NF_NAT = "y";
|
|
|
|
IP_NF_TARGET_MASQUERADE = "y";
|
|
|
|
NETFILTER = "y";
|
|
|
|
NETFILTER_ADVANCED = "y";
|
|
|
|
NETFILTER_XTABLES = "y";
|
|
|
|
|
|
|
|
NFT_COMPAT = "y";
|
|
|
|
NFT_CT = "y";
|
|
|
|
NFT_LOG = "y";
|
|
|
|
NFT_MASQ = "y";
|
|
|
|
NFT_NAT = "y";
|
|
|
|
NFT_REJECT = "y";
|
|
|
|
NFT_REJECT_INET = "y";
|
|
|
|
|
|
|
|
NF_CONNTRACK = "y";
|
|
|
|
NF_NAT = "y";
|
|
|
|
NF_NAT_MASQUERADE = "y";
|
|
|
|
NF_TABLES= "y";
|
|
|
|
NF_TABLES_INET = "y";
|
|
|
|
NF_TABLES_IPV4 = "y";
|
|
|
|
NF_TABLES_IPV6 = "y";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|