2009-09-29 14:21:56 +00:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
2009-07-24 23:12:52 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
iptables = "${pkgs.iptables}/sbin/iptables";
|
|
|
|
|
2009-09-29 14:21:56 +00:00
|
|
|
cfg = config.networking.firewall;
|
|
|
|
|
2009-07-24 23:12:52 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2009-09-29 14:21:56 +00:00
|
|
|
networking.firewall.enable = mkOption {
|
2009-07-26 21:27:35 +00:00
|
|
|
default = false;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Whether to enable the firewall.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-09-29 14:21:56 +00:00
|
|
|
networking.firewall.logRefusedConnections = mkOption {
|
|
|
|
default = true;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Whether to log rejected or dropped incoming connections.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.firewall.logRefusedPackets = mkOption {
|
|
|
|
default = false;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.firewall.rejectPackets = mkOption {
|
|
|
|
default = false;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
If set, forbidden packets are rejected rather than dropped
|
|
|
|
(ignored). This means that a ICMP "port unreachable" error
|
|
|
|
message is sent back to the client. Rejecting packets makes
|
|
|
|
port scanning somewhat easier.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.firewall.allowedTCPPorts = mkOption {
|
2009-07-24 23:12:52 +00:00
|
|
|
default = [];
|
|
|
|
example = [22 80];
|
2009-09-29 14:21:56 +00:00
|
|
|
type = types.list types.int;
|
2009-07-24 23:12:52 +00:00
|
|
|
description =
|
|
|
|
''
|
|
|
|
List of TCP ports on which incoming connections are
|
|
|
|
accepted.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
2009-07-26 21:27:35 +00:00
|
|
|
|
|
|
|
# !!! Maybe if `enable' is false, the firewall should still be built
|
|
|
|
# but not started by default. However, currently nixos-rebuild
|
|
|
|
# doesn't deal with such Upstart jobs properly (it starts them if
|
|
|
|
# they are changed, regardless of whether the start condition
|
|
|
|
# holds).
|
2009-09-29 14:21:56 +00:00
|
|
|
config = mkIf config.networking.firewall.enable {
|
2009-07-24 23:12:52 +00:00
|
|
|
|
|
|
|
environment.systemPackages = [pkgs.iptables];
|
|
|
|
|
2009-10-12 18:09:34 +00:00
|
|
|
jobs.firewall =
|
2009-11-06 22:19:17 +00:00
|
|
|
{ startOn = "started network-interfaces";
|
2009-07-26 21:27:35 +00:00
|
|
|
|
2009-07-24 23:12:52 +00:00
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
${iptables} -F
|
|
|
|
|
|
|
|
# Accept all traffic on the loopback interface.
|
|
|
|
${iptables} -A INPUT -i lo -j ACCEPT
|
|
|
|
|
|
|
|
# Accept packets from established or related connections.
|
|
|
|
${iptables} -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
|
|
|
|
|
|
|
|
# Accept connections to the allowed TCP ports.
|
2009-09-29 14:21:56 +00:00
|
|
|
${concatMapStrings (port:
|
2009-07-24 23:12:52 +00:00
|
|
|
''
|
|
|
|
${iptables} -A INPUT -p tcp --dport ${toString port} -j ACCEPT
|
|
|
|
''
|
|
|
|
) config.networking.firewall.allowedTCPPorts
|
|
|
|
}
|
|
|
|
|
2009-08-10 18:25:09 +00:00
|
|
|
# Accept multicast. Not a big security risk since
|
|
|
|
# probably nobody is listening anyway.
|
|
|
|
${iptables} -A INPUT -d 224.0.0.0/4 -j ACCEPT
|
|
|
|
|
|
|
|
# Drop everything else.
|
2009-09-29 14:21:56 +00:00
|
|
|
${optionalString cfg.logRefusedConnections ''
|
|
|
|
${iptables} -A INPUT -p tcp --syn -j LOG --log-level info --log-prefix "rejected connection: "
|
|
|
|
''}
|
|
|
|
${optionalString cfg.logRefusedPackets ''
|
|
|
|
${iptables} -A INPUT -j LOG --log-level info --log-prefix "rejected packet: "
|
|
|
|
''}
|
|
|
|
${iptables} -A INPUT -j ${if cfg.rejectPackets then "REJECT" else "DROP"}
|
2009-07-24 23:12:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
postStop =
|
|
|
|
''
|
|
|
|
${iptables} -F
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|