git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/firewall: per-interface port options
commitc60c8aa759b7b3b396a709effefb879ceed96fa6 authorgnidorah <gnidorah@users.noreply.github.com> dateTue 29 May 2018, 22:10 (8 years ago) parent0450c7f
1 file changed+230−209
nixos/modules/services/networking/firewall.nix+230−209
nixos/modules/services/networking/firewall.nix+230−209
@@ -148,38 +148,42 @@ let
148148 ip46tables -A nixos-fw -m conntrack --ctstate ESTABLISHED,RELATED -j nixos-fw-accept
149149
150150 # Accept connections to the allowed TCP ports.
151− ${concatMapStrings (port:
151+ ${concatStrings (mapAttrsToList (iface: cfg:
152+ concatMapStrings (port:
152153 ''
153− ip46tables -A nixos-fw -p tcp --dport ${toString port} -j nixos-fw-accept
154+ ip46tables -A nixos-fw -p tcp --dport ${toString port} -j nixos-fw-accept ${optionalString (iface != "default") "-i ${iface}"}
154155 ''
155156 ) cfg.allowedTCPPorts
156− }
157+ ) cfg.interfaces)}
157158
158159 # Accept connections to the allowed TCP port ranges.
159− ${concatMapStrings (rangeAttr:
160+ ${concatStrings (mapAttrsToList (iface: cfg:
161+ concatMapStrings (rangeAttr:
160162 let range = toString rangeAttr.from + ":" + toString rangeAttr.to; in
161163 ''
162− ip46tables -A nixos-fw -p tcp --dport ${range} -j nixos-fw-accept
164+ ip46tables -A nixos-fw -p tcp --dport ${range} -j nixos-fw-accept ${optionalString (iface != "default") "-i ${iface}"}
163165 ''
164166 ) cfg.allowedTCPPortRanges
165− }
167+ ) cfg.interfaces)}
166168
167169 # Accept packets on the allowed UDP ports.
168− ${concatMapStrings (port:
170+ ${concatStrings (mapAttrsToList (iface: cfg:
171+ concatMapStrings (port:
169172 ''
170− ip46tables -A nixos-fw -p udp --dport ${toString port} -j nixos-fw-accept
173+ ip46tables -A nixos-fw -p udp --dport ${toString port} -j nixos-fw-accept ${optionalString (iface != "default") "-i ${iface}"}
171174 ''
172175 ) cfg.allowedUDPPorts
173− }
176+ ) cfg.interfaces)}
174177
175178 # Accept packets on the allowed UDP port ranges.
176− ${concatMapStrings (rangeAttr:
179+ ${concatStrings (mapAttrsToList (iface: cfg:
180+ concatMapStrings (rangeAttr:
177181 let range = toString rangeAttr.from + ":" + toString rangeAttr.to; in
178182 ''
179− ip46tables -A nixos-fw -p udp --dport ${range} -j nixos-fw-accept
183+ ip46tables -A nixos-fw -p udp --dport ${range} -j nixos-fw-accept ${optionalString (iface != "default") "-i ${iface}"}
180184 ''
181185 ) cfg.allowedUDPPortRanges
182− }
186+ ) cfg.interfaces)}
183187
184188 # Accept IPv4 multicast. Not a big security risk since
185189 # probably nobody is listening anyway.
@@ -254,106 +258,30 @@ let
254258 fi
255259 '';
256260
257−in
258−
259−{
260−
261− ###### interface
262−
263− options = {
264−
265− networking.firewall.enable = mkOption {
266− type = types.bool;
267− default = true;
268− description =
269− ''
270− Whether to enable the firewall. This is a simple stateful
271− firewall that blocks connection attempts to unauthorised TCP
272− or UDP ports on this machine. It does not affect packet
273− forwarding.
274− '';
275− };
276−
277− networking.firewall.logRefusedConnections = mkOption {
278− type = types.bool;
279− default = true;
280− description =
281− ''
282− Whether to log rejected or dropped incoming connections.
283− '';
284− };
285−
286− networking.firewall.logRefusedPackets = mkOption {
287− type = types.bool;
288− default = false;
289− description =
290− ''
291− Whether to log all rejected or dropped incoming packets.
292− This tends to give a lot of log messages, so it's mostly
293− useful for debugging.
294− '';
295− };
296−
297− networking.firewall.logRefusedUnicastsOnly = mkOption {
298− type = types.bool;
299− default = true;
300− description =
301− ''
302− If <option>networking.firewall.logRefusedPackets</option>
303− and this option are enabled, then only log packets
304− specifically directed at this machine, i.e., not broadcasts
305− or multicasts.
306− '';
307− };
308−
309− networking.firewall.rejectPackets = mkOption {
310− type = types.bool;
311− default = false;
312− description =
313− ''
314− If set, refused packets are rejected rather than dropped
315− (ignored). This means that an ICMP "port unreachable" error
316− message is sent back to the client (or a TCP RST packet in
317− case of an existing connection). Rejecting packets makes
318− port scanning somewhat easier.
319− '';
320− };
321−
322− networking.firewall.trustedInterfaces = mkOption {
323− type = types.listOf types.str;
324− default = [ ];
325− example = [ "enp0s2" ];
326− description =
327− ''
328− Traffic coming in from these interfaces will be accepted
329− unconditionally. Traffic from the loopback (lo) interface
330− will always be accepted.
331− '';
332− };
333−
334− networking.firewall.allowedTCPPorts = mkOption {
261+ commonOptions = {
262+ allowedTCPPorts = mkOption {
335263 type = types.listOf types.int;
336264 default = [ ];
337265 example = [ 22 80 ];
338266 description =
339− ''
267+ ''
340268 List of TCP ports on which incoming connections are
341269 accepted.
342270 '';
343271 };
344272
345− networking.firewall.allowedTCPPortRanges = mkOption {
273+ allowedTCPPortRanges = mkOption {
346274 type = types.listOf (types.attrsOf types.int);
347275 default = [ ];
348276 example = [ { from = 8999; to = 9003; } ];
349277 description =
350− ''
278+ ''
351279 A range of TCP ports on which incoming connections are
352280 accepted.
353281 '';
354282 };
355283
356− networking.firewall.allowedUDPPorts = mkOption {
284+ allowedUDPPorts = mkOption {
357285 type = types.listOf types.int;
358286 default = [ ];
359287 example = [ 53 ];
@@ -363,7 +291,7 @@ in
363291 '';
364292 };
365293
366− networking.firewall.allowedUDPPortRanges = mkOption {
294+ allowedUDPPortRanges = mkOption {
367295 type = types.listOf (types.attrsOf types.int);
368296 default = [ ];
369297 example = [ { from = 60000; to = 61000; } ];
@@ -372,133 +300,226 @@ in
372300 Range of open UDP ports.
373301 '';
374302 };
303+ };
375304
376− networking.firewall.allowPing = mkOption {
377− type = types.bool;
378− default = true;
379− description =
380− ''
381− Whether to respond to incoming ICMPv4 echo requests
382− ("pings"). ICMPv6 pings are always allowed because the
383− larger address space of IPv6 makes network scanning much
384− less effective.
385− '';
386− };
305+in
387306
388− networking.firewall.pingLimit = mkOption {
389− type = types.nullOr (types.separatedString " ");
390− default = null;
391− example = "--limit 1/minute --limit-burst 5";
392− description =
393− ''
394− If pings are allowed, this allows setting rate limits
395− on them. If non-null, this option should be in the form of
396− flags like "--limit 1/minute --limit-burst 5"
397− '';
398− };
307+{
399308
400− networking.firewall.checkReversePath = mkOption {
401− type = types.either types.bool (types.enum ["strict" "loose"]);
402− default = kernelHasRPFilter;
403− example = "loose";
404− description =
405− ''
406− Performs a reverse path filter test on a packet. If a reply
407− to the packet would not be sent via the same interface that
408− the packet arrived on, it is refused.
309+ ###### interface
409310
410− If using asymmetric routing or other complicated routing, set
411− this option to loose mode or disable it and setup your own
412− counter-measures.
311+ options = {
413312
414− This option can be either true (or "strict"), "loose" (only
415− drop the packet if the source address is not reachable via any
416− interface) or false. Defaults to the value of
417− kernelHasRPFilter.
313+ networking.firewall = {
314+ enable = mkOption {
315+ type = types.bool;
316+ default = true;
317+ description =
318+ ''
319+ Whether to enable the firewall. This is a simple stateful
320+ firewall that blocks connection attempts to unauthorised TCP
321+ or UDP ports on this machine. It does not affect packet
322+ forwarding.
323+ '';
324+ };
418325
419− (needs kernel 3.3+)
420− '';
421− };
326+ logRefusedConnections = mkOption {
327+ type = types.bool;
328+ default = true;
329+ description =
330+ ''
331+ Whether to log rejected or dropped incoming connections.
332+ '';
333+ };
422334
423− networking.firewall.logReversePathDrops = mkOption {
424− type = types.bool;
425− default = false;
426− description =
427− ''
428− Logs dropped packets failing the reverse path filter test if
429− the option networking.firewall.checkReversePath is enabled.
430− '';
431− };
335+ logRefusedPackets = mkOption {
336+ type = types.bool;
337+ default = false;
338+ description =
339+ ''
340+ Whether to log all rejected or dropped incoming packets.
341+ This tends to give a lot of log messages, so it's mostly
342+ useful for debugging.
343+ '';
344+ };
432345
433− networking.firewall.connectionTrackingModules = mkOption {
434− type = types.listOf types.str;
435− default = [ ];
436− example = [ "ftp" "irc" "sane" "sip" "tftp" "amanda" "h323" "netbios_sn" "pptp" "snmp" ];
437− description =
438− ''
439− List of connection-tracking helpers that are auto-loaded.
440− The complete list of possible values is given in the example.
441−
442− As helpers can pose as a security risk, it is advised to
443− set this to an empty list and disable the setting
444− networking.firewall.autoLoadConntrackHelpers unless you
445− know what you are doing. Connection tracking is disabled
446− by default.
447−
448− Loading of helpers is recommended to be done through the
449− CT target. More info:
450− https://home.regit.org/netfilter-en/secure-use-of-helpers/
451− '';
452− };
346+ logRefusedUnicastsOnly = mkOption {
347+ type = types.bool;
348+ default = true;
349+ description =
350+ ''
351+ If <option>networking.firewall.logRefusedPackets</option>
352+ and this option are enabled, then only log packets
353+ specifically directed at this machine, i.e., not broadcasts
354+ or multicasts.
355+ '';
356+ };
453357
454− networking.firewall.autoLoadConntrackHelpers = mkOption {
455− type = types.bool;
456− default = false;
457− description =
458− ''
459− Whether to auto-load connection-tracking helpers.
460− See the description at networking.firewall.connectionTrackingModules
358+ rejectPackets = mkOption {
359+ type = types.bool;
360+ default = false;
361+ description =
362+ ''
363+ If set, refused packets are rejected rather than dropped
364+ (ignored). This means that an ICMP "port unreachable" error
365+ message is sent back to the client (or a TCP RST packet in
366+ case of an existing connection). Rejecting packets makes
367+ port scanning somewhat easier.
368+ '';
369+ };
461370
462− (needs kernel 3.5+)
463− '';
464− };
371+ trustedInterfaces = mkOption {
372+ type = types.listOf types.str;
373+ default = [ ];
374+ example = [ "enp0s2" ];
375+ description =
376+ ''
377+ Traffic coming in from these interfaces will be accepted
378+ unconditionally. Traffic from the loopback (lo) interface
379+ will always be accepted.
380+ '';
381+ };
465382
466− networking.firewall.extraCommands = mkOption {
467− type = types.lines;
468− default = "";
469− example = "iptables -A INPUT -p icmp -j ACCEPT";
470− description =
471− ''
472− Additional shell commands executed as part of the firewall
473− initialisation script. These are executed just before the
474− final "reject" firewall rule is added, so they can be used
475− to allow packets that would otherwise be refused.
476− '';
477− };
383+ allowPing = mkOption {
384+ type = types.bool;
385+ default = true;
386+ description =
387+ ''
388+ Whether to respond to incoming ICMPv4 echo requests
389+ ("pings"). ICMPv6 pings are always allowed because the
390+ larger address space of IPv6 makes network scanning much
391+ less effective.
392+ '';
393+ };
478394
479− networking.firewall.extraPackages = mkOption {
480− type = types.listOf types.package;
481− default = [ ];
482− example = literalExample "[ pkgs.ipset ]";
483− description =
484− ''
485− Additional packages to be included in the environment of the system
486− as well as the path of networking.firewall.extraCommands.
487− '';
488− };
395+ pingLimit = mkOption {
396+ type = types.nullOr (types.separatedString " ");
397+ default = null;
398+ example = "--limit 1/minute --limit-burst 5";
399+ description =
400+ ''
401+ If pings are allowed, this allows setting rate limits
402+ on them. If non-null, this option should be in the form of
403+ flags like "--limit 1/minute --limit-burst 5"
404+ '';
405+ };
489406
490− networking.firewall.extraStopCommands = mkOption {
491− type = types.lines;
492− default = "";
493− example = "iptables -P INPUT ACCEPT";
494− description =
495− ''
496− Additional shell commands executed as part of the firewall
497− shutdown script. These are executed just after the removal
498− of the NixOS input rule, or if the service enters a failed
499− state.
500− '';
501− };
407+ checkReversePath = mkOption {
408+ type = types.either types.bool (types.enum ["strict" "loose"]);
409+ default = kernelHasRPFilter;
410+ example = "loose";
411+ description =
412+ ''
413+ Performs a reverse path filter test on a packet. If a reply
414+ to the packet would not be sent via the same interface that
415+ the packet arrived on, it is refused.
416+
417+ If using asymmetric routing or other complicated routing, set
418+ this option to loose mode or disable it and setup your own
419+ counter-measures.
420+
421+ This option can be either true (or "strict"), "loose" (only
422+ drop the packet if the source address is not reachable via any
423+ interface) or false. Defaults to the value of
424+ kernelHasRPFilter.
425+
426+ (needs kernel 3.3+)
427+ '';
428+ };
429+
430+ logReversePathDrops = mkOption {
431+ type = types.bool;
432+ default = false;
433+ description =
434+ ''
435+ Logs dropped packets failing the reverse path filter test if
436+ the option networking.firewall.checkReversePath is enabled.
437+ '';
438+ };
439+
440+ connectionTrackingModules = mkOption {
441+ type = types.listOf types.str;
442+ default = [ ];
443+ example = [ "ftp" "irc" "sane" "sip" "tftp" "amanda" "h323" "netbios_sn" "pptp" "snmp" ];
444+ description =
445+ ''
446+ List of connection-tracking helpers that are auto-loaded.
447+ The complete list of possible values is given in the example.
448+
449+ As helpers can pose as a security risk, it is advised to
450+ set this to an empty list and disable the setting
451+ networking.firewall.autoLoadConntrackHelpers unless you
452+ know what you are doing. Connection tracking is disabled
453+ by default.
454+
455+ Loading of helpers is recommended to be done through the
456+ CT target. More info:
457+ https://home.regit.org/netfilter-en/secure-use-of-helpers/
458+ '';
459+ };
460+
461+ autoLoadConntrackHelpers = mkOption {
462+ type = types.bool;
463+ default = false;
464+ description =
465+ ''
466+ Whether to auto-load connection-tracking helpers.
467+ See the description at networking.firewall.connectionTrackingModules
468+
469+ (needs kernel 3.5+)
470+ '';
471+ };
472+
473+ extraCommands = mkOption {
474+ type = types.lines;
475+ default = "";
476+ example = "iptables -A INPUT -p icmp -j ACCEPT";
477+ description =
478+ ''
479+ Additional shell commands executed as part of the firewall
480+ initialisation script. These are executed just before the
481+ final "reject" firewall rule is added, so they can be used
482+ to allow packets that would otherwise be refused.
483+ '';
484+ };
485+
486+ extraPackages = mkOption {
487+ type = types.listOf types.package;
488+ default = [ ];
489+ example = literalExample "[ pkgs.ipset ]";
490+ description =
491+ ''
492+ Additional packages to be included in the environment of the system
493+ as well as the path of networking.firewall.extraCommands.
494+ '';
495+ };
496+
497+ extraStopCommands = mkOption {
498+ type = types.lines;
499+ default = "";
500+ example = "iptables -P INPUT ACCEPT";
501+ description =
502+ ''
503+ Additional shell commands executed as part of the firewall
504+ shutdown script. These are executed just after the removal
505+ of the NixOS input rule, or if the service enters a failed
506+ state.
507+ '';
508+ };
509+
510+ interfaces = mkOption {
511+ default = {
512+ default = mapAttrs (name: value: cfg."${name}") commonOptions;
513+ };
514+ type = with types; attrsOf (submodule [ { options = commonOptions; } ]);
515+ description =
516+ ''
517+ Interface-specific open ports. Setting this value will override
518+ all values of the <literal>networking.firewall.allowed*</literal>
519+ options.
520+ '';
521+ };
522+ } // commonOptions;
502523
503524 };
504525