-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nixos-firewall-tool: add nftables support #275126
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6375083
nixos/firewall-nftables: don't use `with lib`
duament 95ad743
nixos/firewall-nftables: use named set for runtime manipulation
duament c40d55f
maintainers: add rvfg
duament ff0ce05
nixos-firewall-tool: add nftables support
duament 8da2338
nixosTests.firewall: add tests for nixos-firewall-tool
duament File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15962,6 +15962,12 @@ | |
githubId = 141248; | ||
name = "Ramses"; | ||
}; | ||
rvfg = { | ||
email = "[email protected]"; | ||
github = "duament"; | ||
githubId = 30264485; | ||
name = "Rvfg"; | ||
}; | ||
rvl = { | ||
email = "[email protected]"; | ||
github = "rvl"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,31 @@ | |
import ./make-test-python.nix ( { pkgs, nftables, ... } : { | ||
name = "firewall" + pkgs.lib.optionalString nftables "-nftables"; | ||
meta = with pkgs.lib.maintainers; { | ||
maintainers = [ eelco ]; | ||
maintainers = [ eelco rvfg ]; | ||
}; | ||
|
||
nodes = | ||
{ walled = | ||
{ ... }: | ||
{ networking.firewall.enable = true; | ||
networking.firewall.logRefusedPackets = true; | ||
{ networking.firewall = { | ||
enable = true; | ||
logRefusedPackets = true; | ||
# Syntax smoke test, not actually verified otherwise | ||
allowedTCPPorts = [ 25 993 8005 ]; | ||
allowedTCPPortRanges = [ | ||
{ from = 980; to = 1000; } | ||
{ from = 990; to = 1010; } | ||
{ from = 8000; to = 8010; } | ||
]; | ||
interfaces.eth0 = { | ||
allowedTCPPorts = [ 10003 ]; | ||
allowedTCPPortRanges = [ { from = 10000; to = 10005; } ]; | ||
}; | ||
interfaces.eth3 = { | ||
allowedUDPPorts = [ 10003 ]; | ||
allowedUDPPortRanges = [ { from = 10000; to = 10005; } ]; | ||
}; | ||
}; | ||
duament marked this conversation as resolved.
Show resolved
Hide resolved
|
||
networking.nftables.enable = nftables; | ||
services.httpd.enable = true; | ||
services.httpd.adminAddr = "[email protected]"; | ||
|
@@ -36,7 +53,7 @@ import ./make-test-python.nix ( { pkgs, nftables, ... } : { | |
}; | ||
|
||
testScript = { nodes, ... }: let | ||
newSystem = nodes.walled2.config.system.build.toplevel; | ||
newSystem = nodes.walled2.system.build.toplevel; | ||
unit = if nftables then "nftables" else "firewall"; | ||
in '' | ||
start_all() | ||
|
@@ -56,6 +73,14 @@ import ./make-test-python.nix ( { pkgs, nftables, ... } : { | |
walled.succeed("curl -v http://attacker/ >&2") | ||
walled.succeed("ping -c 1 attacker >&2") | ||
# Open tcp port 80 at runtime | ||
walled.succeed("nixos-firewall-tool open tcp 80") | ||
attacker.succeed("curl -v http://walled/ >&2") | ||
# Reset the firewall | ||
walled.succeed("nixos-firewall-tool reset") | ||
attacker.fail("curl --fail --connect-timeout 2 http://walled/ >&2") | ||
# If we stop the firewall, then connections should succeed. | ||
walled.stop_job("${unit}") | ||
attacker.succeed("curl -v http://walled/ >&2") | ||
|
49 changes: 49 additions & 0 deletions
49
pkgs/by-name/ni/nixos-firewall-tool/nixos-firewall-tool-nftables.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
show_help() { | ||
echo "nixos-firewall-tool" | ||
echo "" | ||
echo "Can temporarily manipulate the NixOS firewall" | ||
echo "" | ||
echo "Open TCP port:" | ||
echo " nixos-firewall-tool open tcp 8888" | ||
echo "" | ||
echo "Show all firewall rules:" | ||
echo " nixos-firewall-tool show" | ||
echo "" | ||
echo "Open UDP port:" | ||
echo " nixos-firewall-tool open udp 51820" | ||
echo "" | ||
echo "Reset firewall configuration to system settings:" | ||
echo " nixos-firewall-tool reset" | ||
} | ||
|
||
if [[ -z ${1+x} ]]; then | ||
show_help | ||
exit 1 | ||
fi | ||
|
||
case $1 in | ||
"open") | ||
protocol="$2" | ||
port="$3" | ||
|
||
nft add element inet nixos-fw "$protocol-ports" "{ $port }" | ||
;; | ||
"show") | ||
nft list table inet nixos-fw | ||
;; | ||
"reset") | ||
systemctl reload nftables.service | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reloading nftables for undoing port changes is a bit excessive, see: #286584. Instead of the current approach, I would suggest add a set of type |
||
;; | ||
-h|--help|help) | ||
show_help | ||
exit 0 | ||
;; | ||
*) | ||
show_help | ||
exit 1 | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
{ writeShellApplication, iptables, lib }: | ||
{ writeShellApplication, iptables, lib | ||
, nftables, backend ? "iptables" | ||
}: | ||
|
||
writeShellApplication { | ||
writeShellApplication ({ | ||
name = "nixos-firewall-tool"; | ||
text = builtins.readFile ./nixos-firewall-tool.sh; | ||
runtimeInputs = [ | ||
iptables | ||
]; | ||
|
||
meta = with lib; { | ||
description = "Temporarily manipulate the NixOS firewall"; | ||
license = licenses.mit; | ||
maintainers = with maintainers; [ clerie ]; | ||
maintainers = with maintainers; [ clerie rvfg ]; | ||
}; | ||
} | ||
} // ( | ||
if backend == "iptables" then { | ||
text = builtins.readFile ./nixos-firewall-tool.sh; | ||
runtimeInputs = [ iptables ]; | ||
|
||
} else if backend == "nftables" then { | ||
text = builtins.readFile ./nixos-firewall-tool-nftables.sh; | ||
runtimeInputs = [ nftables ]; | ||
|
||
} else | ||
throw "Unknown firewall backend." | ||
)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make the iptables backend selection also explicit? There might be more than two firewall implementations in the future and I'd rather throw here than install the iptables firewall tool in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, we only have a boolean option
networking.nftables.enable
for firewall backends. Do you mean we add a new option to set the backend?