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;
|
|
|
|
|
2024-02-12 00:47:11 +01:00
|
|
|
kmodules = pkgs.kmodloader.override {
|
|
|
|
inherit (config.system.outputs) kernel;
|
2023-07-16 17:55:50 +02:00
|
|
|
targets = [
|
|
|
|
"nft_fib_ipv4"
|
|
|
|
"nft_fib_ipv6"
|
2024-02-08 00:43:41 +01:00
|
|
|
"nf_log_syslog"
|
|
|
|
|
|
|
|
"ip6_tables"
|
|
|
|
"ip_tables"
|
|
|
|
"iptable_nat"
|
|
|
|
"nf_conntrack"
|
|
|
|
"nf_defrag_ipv4"
|
|
|
|
"nf_defrag_ipv6"
|
|
|
|
"nf_log_syslog"
|
|
|
|
"nf_nat"
|
|
|
|
"nf_reject_ipv4"
|
|
|
|
"nf_reject_ipv6"
|
|
|
|
"nf_tables"
|
|
|
|
"nft_chain_nat"
|
|
|
|
"nft_ct"
|
|
|
|
"nft_fib"
|
|
|
|
"nft_fib_ipv4"
|
|
|
|
"nft_fib_ipv6"
|
|
|
|
"nft_log"
|
|
|
|
"nft_masq"
|
|
|
|
"nft_nat"
|
|
|
|
"nft_reject"
|
|
|
|
"nft_reject_inet"
|
|
|
|
"nft_reject_ipv4"
|
|
|
|
"nft_reject_ipv6"
|
|
|
|
"x_tables"
|
|
|
|
"xt_MASQUERADE"
|
|
|
|
"xt_nat"
|
|
|
|
"xt_tcpudp"
|
2023-07-16 17:55:50 +02:00
|
|
|
];
|
|
|
|
};
|
|
|
|
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 {
|
2024-03-21 13:00:34 +01:00
|
|
|
extraRules = mkOption {
|
|
|
|
type = types.attrsOf types.attrs;
|
|
|
|
description = "firewall ruleset";
|
|
|
|
};
|
|
|
|
rules = mkOption {
|
2023-08-05 13:07:35 +02:00
|
|
|
type = types.attrsOf types.attrs; # we could usefully tighten this a bit :-)
|
2024-03-21 13:00:34 +01:00
|
|
|
default = import ./default-rules.nix;
|
2023-08-05 13:07:35 +02:00
|
|
|
description = "firewall ruleset";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in svc // {
|
2024-02-07 17:21:14 +01:00
|
|
|
build = args :
|
|
|
|
let args' = args // {
|
2024-02-12 00:47:11 +01:00
|
|
|
dependencies = (args.dependencies or []) ++ [kmodules];
|
2024-02-07 17:21:14 +01:00
|
|
|
};
|
|
|
|
in svc.build args' ;
|
2023-08-05 13:07:35 +02:00
|
|
|
};
|
2024-05-02 00:06:12 +02:00
|
|
|
programs.busybox.applets = [
|
|
|
|
"insmod" "rmmod"
|
|
|
|
];
|
2024-02-12 00:47:11 +01:00
|
|
|
kernel.config = {
|
|
|
|
NETFILTER = "y";
|
|
|
|
NETFILTER_ADVANCED = "y";
|
|
|
|
NETFILTER_NETLINK = "m";
|
|
|
|
NF_CONNTRACK = "m";
|
|
|
|
|
2024-05-02 00:06:12 +02:00
|
|
|
NETLINK_DIAG = "y";
|
|
|
|
|
2024-02-12 00:47:11 +01:00
|
|
|
IP6_NF_IPTABLES= "m";
|
|
|
|
IP_NF_IPTABLES = "m";
|
|
|
|
IP_NF_NAT = "m";
|
|
|
|
IP_NF_TARGET_MASQUERADE = "m";
|
|
|
|
|
|
|
|
NFT_CT = "m";
|
|
|
|
NFT_FIB_IPV4 = "m";
|
|
|
|
NFT_FIB_IPV6 = "m";
|
|
|
|
NFT_LOG = "m";
|
|
|
|
NFT_MASQ = "m";
|
|
|
|
NFT_NAT = "m";
|
|
|
|
NFT_REJECT = "m";
|
|
|
|
NFT_REJECT_INET = "m";
|
|
|
|
|
|
|
|
NF_CT_PROTO_DCCP = "y";
|
|
|
|
NF_CT_PROTO_SCTP = "y";
|
|
|
|
NF_CT_PROTO_UDPLITE = "y";
|
|
|
|
NF_LOG_SYSLOG = "m";
|
|
|
|
NF_NAT = "m";
|
|
|
|
NF_NAT_MASQUERADE = "y";
|
|
|
|
NF_TABLES = "m";
|
|
|
|
NF_TABLES_INET = "y";
|
|
|
|
NF_TABLES_IPV4 = "y";
|
|
|
|
NF_TABLES_IPV6 = "y";
|
|
|
|
};
|
2023-07-16 17:55:50 +02:00
|
|
|
};
|
|
|
|
}
|