2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-09-29 14:21:56 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2009-07-24 23:12:52 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2009-09-29 14:21:56 +00:00
|
|
|
cfg = config.networking.firewall;
|
|
|
|
|
2019-03-09 18:59:01 +00:00
|
|
|
canonicalizePortList =
|
|
|
|
ports: lib.unique (builtins.sort builtins.lessThan ports);
|
|
|
|
|
2018-05-29 19:10:25 +00:00
|
|
|
commonOptions = {
|
|
|
|
allowedTCPPorts = mkOption {
|
2019-03-09 18:45:11 +00:00
|
|
|
type = types.listOf types.port;
|
2017-01-18 16:18:11 +00:00
|
|
|
default = [ ];
|
2019-03-09 18:59:01 +00:00
|
|
|
apply = canonicalizePortList;
|
2017-01-18 16:18:11 +00:00
|
|
|
example = [ 22 80 ];
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
List of TCP ports on which incoming connections are
|
|
|
|
accepted.
|
|
|
|
'';
|
2009-07-24 23:12:52 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2018-05-29 19:10:25 +00:00
|
|
|
allowedTCPPortRanges = mkOption {
|
2019-03-09 18:45:11 +00:00
|
|
|
type = types.listOf (types.attrsOf types.port);
|
2017-01-18 16:18:11 +00:00
|
|
|
default = [ ];
|
2022-12-22 16:23:23 +00:00
|
|
|
example = [{ from = 8999; to = 9003; }];
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
A range of TCP ports on which incoming connections are
|
|
|
|
accepted.
|
|
|
|
'';
|
2014-02-19 11:19:19 +00:00
|
|
|
};
|
|
|
|
|
2018-05-29 19:10:25 +00:00
|
|
|
allowedUDPPorts = mkOption {
|
2019-03-09 18:45:11 +00:00
|
|
|
type = types.listOf types.port;
|
2017-01-18 16:18:11 +00:00
|
|
|
default = [ ];
|
2019-03-09 18:59:01 +00:00
|
|
|
apply = canonicalizePortList;
|
2017-01-18 16:18:11 +00:00
|
|
|
example = [ 53 ];
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
List of open UDP ports.
|
|
|
|
'';
|
2011-03-09 16:37:16 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2018-05-29 19:10:25 +00:00
|
|
|
allowedUDPPortRanges = mkOption {
|
2019-03-09 18:45:11 +00:00
|
|
|
type = types.listOf (types.attrsOf types.port);
|
2017-01-18 16:18:11 +00:00
|
|
|
default = [ ];
|
2022-12-22 16:23:23 +00:00
|
|
|
example = [{ from = 60000; to = 61000; }];
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Range of open UDP ports.
|
|
|
|
'';
|
2014-02-19 11:19:19 +00:00
|
|
|
};
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
2014-02-19 11:19:19 +00:00
|
|
|
|
2018-05-29 19:10:25 +00:00
|
|
|
in
|
2012-10-12 11:16:33 +00:00
|
|
|
|
2018-05-29 19:10:25 +00:00
|
|
|
{
|
2012-10-12 11:16:33 +00:00
|
|
|
|
2018-05-29 19:10:25 +00:00
|
|
|
options = {
|
2015-07-26 23:32:59 +00:00
|
|
|
|
2018-05-29 19:10:25 +00:00
|
|
|
networking.firewall = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to enable the firewall. This is a simple stateful
|
|
|
|
firewall that blocks connection attempts to unauthorised TCP
|
|
|
|
or UDP ports on this machine.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
2019-08-19 17:21:24 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
2022-12-22 16:23:23 +00:00
|
|
|
default = if config.networking.nftables.enable then pkgs.nftables else pkgs.iptables;
|
|
|
|
defaultText = literalExpression ''if config.networking.nftables.enable then "pkgs.nftables" else "pkgs.iptables"'';
|
2022-02-08 09:31:09 +00:00
|
|
|
example = literalExpression "pkgs.iptables-legacy";
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The package to use for running the firewall service.
|
|
|
|
'';
|
2019-08-19 17:21:24 +00:00
|
|
|
};
|
|
|
|
|
2018-05-29 19:10:25 +00:00
|
|
|
logRefusedConnections = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to log rejected or dropped incoming connections.
|
|
|
|
Note: The logs are found in the kernel logs, i.e. dmesg
|
|
|
|
or journalctl -k.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
logRefusedPackets = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to log all rejected or dropped incoming packets.
|
|
|
|
This tends to give a lot of log messages, so it's mostly
|
|
|
|
useful for debugging.
|
|
|
|
Note: The logs are found in the kernel logs, i.e. dmesg
|
|
|
|
or journalctl -k.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
logRefusedUnicastsOnly = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
If {option}`networking.firewall.logRefusedPackets`
|
|
|
|
and this option are enabled, then only log packets
|
|
|
|
specifically directed at this machine, i.e., not broadcasts
|
|
|
|
or multicasts.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
rejectPackets = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
If set, refused packets are rejected rather than dropped
|
|
|
|
(ignored). This means that an ICMP "port unreachable" error
|
|
|
|
message is sent back to the client (or a TCP RST packet in
|
|
|
|
case of an existing connection). Rejecting packets makes
|
|
|
|
port scanning somewhat easier.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
trustedInterfaces = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
example = [ "enp0s2" ];
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Traffic coming in from these interfaces will be accepted
|
|
|
|
unconditionally. Traffic from the loopback (lo) interface
|
|
|
|
will always be accepted.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
allowPing = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to respond to incoming ICMPv4 echo requests
|
|
|
|
("pings"). ICMPv6 pings are always allowed because the
|
|
|
|
larger address space of IPv6 makes network scanning much
|
|
|
|
less effective.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pingLimit = mkOption {
|
|
|
|
type = types.nullOr (types.separatedString " ");
|
|
|
|
default = null;
|
|
|
|
example = "--limit 1/minute --limit-burst 5";
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
If pings are allowed, this allows setting rate limits on them.
|
|
|
|
|
|
|
|
For the iptables based firewall, it should be set like
|
|
|
|
"--limit 1/minute --limit-burst 5".
|
|
|
|
|
|
|
|
For the nftables based firewall, it should be set like
|
|
|
|
"2/second" or "1/minute burst 5 packets".
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
checkReversePath = mkOption {
|
2022-12-22 16:23:23 +00:00
|
|
|
type = types.either types.bool (types.enum [ "strict" "loose" ]);
|
|
|
|
default = true;
|
|
|
|
defaultText = literalMD "`true` except if the iptables based firewall is in use and the kernel lacks rpfilter support";
|
2018-05-29 19:10:25 +00:00
|
|
|
example = "loose";
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Performs a reverse path filter test on a packet. If a reply
|
|
|
|
to the packet would not be sent via the same interface that
|
|
|
|
the packet arrived on, it is refused.
|
|
|
|
|
|
|
|
If using asymmetric routing or other complicated routing, set
|
|
|
|
this option to loose mode or disable it and setup your own
|
|
|
|
counter-measures.
|
|
|
|
|
|
|
|
This option can be either true (or "strict"), "loose" (only
|
|
|
|
drop the packet if the source address is not reachable via any
|
|
|
|
interface) or false.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
logReversePathDrops = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Logs dropped packets failing the reverse path filter test if
|
|
|
|
the option networking.firewall.checkReversePath is enabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
filterForward = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Enable filtering in IP forwarding.
|
|
|
|
|
|
|
|
This option only works with the nftables based firewall.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
connectionTrackingModules = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
example = [ "ftp" "irc" "sane" "sip" "tftp" "amanda" "h323" "netbios_sn" "pptp" "snmp" ];
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
List of connection-tracking helpers that are auto-loaded.
|
|
|
|
The complete list of possible values is given in the example.
|
|
|
|
|
|
|
|
As helpers can pose as a security risk, it is advised to
|
|
|
|
set this to an empty list and disable the setting
|
|
|
|
networking.firewall.autoLoadConntrackHelpers unless you
|
|
|
|
know what you are doing. Connection tracking is disabled
|
|
|
|
by default.
|
|
|
|
|
|
|
|
Loading of helpers is recommended to be done through the
|
|
|
|
CT target. More info:
|
|
|
|
https://home.regit.org/netfilter-en/secure-use-of-helpers/
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
autoLoadConntrackHelpers = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to auto-load connection-tracking helpers.
|
|
|
|
See the description at networking.firewall.connectionTrackingModules
|
2018-05-29 19:10:25 +00:00
|
|
|
|
2022-12-22 16:23:23 +00:00
|
|
|
(needs kernel 3.5+)
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extraPackages = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [ ];
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression "[ pkgs.ipset ]";
|
2022-12-22 16:23:23 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Additional packages to be included in the environment of the system
|
|
|
|
as well as the path of networking.firewall.extraCommands.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
interfaces = mkOption {
|
2018-11-18 20:03:42 +00:00
|
|
|
default = { };
|
2022-12-22 16:23:23 +00:00
|
|
|
type = with types; attrsOf (submodule [{ options = commonOptions; }]);
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Interface-specific open ports.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
allInterfaces = mkOption {
|
|
|
|
internal = true;
|
|
|
|
visible = false;
|
|
|
|
default = { default = mapAttrs (name: value: cfg.${name}) commonOptions; } // cfg.interfaces;
|
|
|
|
type = with types; attrsOf (submodule [{ options = commonOptions; }]);
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
All open ports.
|
|
|
|
'';
|
2018-05-29 19:10:25 +00:00
|
|
|
};
|
|
|
|
} // commonOptions;
|
2014-09-16 04:29:46 +00:00
|
|
|
|
2009-07-24 23:12:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-03-10 09:39:17 +00:00
|
|
|
config = mkIf cfg.enable {
|
2009-07-24 23:12:52 +00:00
|
|
|
|
2022-12-22 16:23:23 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.filterForward -> config.networking.nftables.enable;
|
|
|
|
message = "filterForward only works with the nftables based firewall";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2012-09-20 21:51:44 +00:00
|
|
|
networking.firewall.trustedInterfaces = [ "lo" ];
|
|
|
|
|
2019-08-19 17:21:24 +00:00
|
|
|
environment.systemPackages = [ cfg.package ] ++ cfg.extraPackages;
|
2009-07-24 23:12:52 +00:00
|
|
|
|
2017-01-22 16:29:38 +00:00
|
|
|
boot.kernelModules = (optional cfg.autoLoadConntrackHelpers "nf_conntrack")
|
|
|
|
++ map (x: "nf_conntrack_${x}") cfg.connectionTrackingModules;
|
|
|
|
boot.extraModprobeConfig = optionalString cfg.autoLoadConntrackHelpers ''
|
|
|
|
options nf_conntrack nf_conntrack_helper=1
|
2012-10-12 11:16:33 +00:00
|
|
|
'';
|
2011-03-11 13:34:17 +00:00
|
|
|
|
2009-07-24 23:12:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|