aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/host/default.nix116
1 files changed, 105 insertions, 11 deletions
diff --git a/modules/host/default.nix b/modules/host/default.nix
index 87a32d3..94a44b4 100644
--- a/modules/host/default.nix
+++ b/modules/host/default.nix
@@ -22,6 +22,12 @@
# at any time with fc-egress (see fc-egress help); they apply to the next
# connection.
#
+# Users can also allow plain TCP or UDP to given addresses and ports, like
+# ssh to some host: rules like tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT] in the
+# same file. fcbr0-firewall puts them into the user's nftables set
+# allow_USER whenever the file changes, and those connections get
+# forwarded (and NATed), or let in, if they're for the host.
+#
# usage (in the host's configuration.nix), users and their number of taps:
# imports = [ (import ./modules/host { users = { tv = 8; }; }) ];
# The host's firewall has to let the guests in on fcbr0: DHCP (udp 67),
@@ -43,6 +49,31 @@
++ go (i + n) (lib.tail names);
in go 0 (lib.attrNames users);
dir = "/var/lib/fc-egress";
+ userNames = lib.attrNames users;
+ ipsOf = user: map (t: t.ip) (lib.filter (t: t.user == user) taps);
+
+ # tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT] or udp://... (nft checks the
+ # numbers.)
+ portRule = "(tcp|udp)://[0-9]+(\\.[0-9]+){3}(/[0-9]+)?:[0-9]+(-[0-9]+)?";
+
+ # Puts each user's tcp:// and udp:// rules into their nftables set, one
+ # user at a time, so one user's bad rule doesn't keep the others' out.
+ firewallUpdate = pkgs.writeShellScript "fcbr0-firewall-update" ''
+ set -fu
+ export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.gnugrep pkgs.gnused pkgs.nftables ]}
+ status=0
+ for user in ${lib.escapeShellArgs userNames}; do
+ elements=$(grep -xE '${portRule}' ${dir}/$user/allowed 2>/dev/null |
+ sed -E 's#^(tcp|udp)://([^:]+):(.*)$#\2 . \1 . \3#' | paste -sd,)
+ {
+ echo "flush set inet firecracker allow_$user"
+ if test -n "$elements"; then
+ echo "add element inet firecracker allow_$user { $elements }"
+ fi
+ } | nft -f - || { echo "fcbr0-firewall: rules of $user not applied" >&2; status=1; }
+ done
+ exit $status
+ '';
# Where nginx keeps the CA: ca.key, and ca.pem for the guests (see
# caCert in pkgs/run-firecracker-vm).
@@ -51,7 +82,7 @@
fc-egress = pkgs.writeShellScriptBin "fc-egress" ''
set -efu
- export PATH=${lib.makeBinPath [ pkgs.bash pkgs.coreutils pkgs.gnugrep pkgs.systemd ]}
+ export PATH=${lib.makeBinPath [ pkgs.bash pkgs.coreutils pkgs.gawk pkgs.gnugrep pkgs.systemd ]}
user=$(id -un)
list=${dir}/$user/allowed
if ! test -e "$list"; then
@@ -63,11 +94,28 @@
echo " fc-egress allow RULE..."
echo " fc-egress deny RULE..."
echo " fc-egress log [JOURNALCTL-OPTION...]"
- echo "RULE is DOMAIN (with subdomains, not decrypted)"
- echo " or DOMAIN/PATH-PREFIX (decrypted to check the path)"
+ echo "RULE is DOMAIN (HTTPS, with subdomains, not decrypted)"
+ echo " or DOMAIN/PATH-PREFIX (HTTPS, decrypted to check the path)"
+ echo " or tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT] (likewise udp://...)"
}
check() {
for r; do
+ if printf %s "$r" | grep -qxE '${portRule}'; then
+ # numbers in range: address bytes, prefix length, ports
+ if printf %s "$r" | awk '{
+ s = $0; sub(/^[a-z]+:\/\//, "", s)
+ split(s, hp, ":"); split(hp[1], a, "/"); split(a[1], b, ".")
+ for (i = 1; i <= 4; i++) if (b[i] > 255) exit 1
+ if (a[2] != "" && a[2] > 32) exit 1
+ n = split(hp[2], p, "-")
+ for (i = 1; i <= n; i++) if (p[i] < 1 || p[i] > 65535) exit 1
+ if (n == 2 && p[1] > p[2]) exit 1
+ }'; then
+ continue
+ fi
+ echo "fc-egress: bad rule: $r" >&2
+ exit 1
+ fi
if ! printf %s "$r" | grep -qxE '[a-z0-9-]+(\.[a-z0-9-]+)*(/[A-Za-z0-9._~!$&()*+,;=:@%/-]*)?' ||
printf %s "$r" | grep -qE '/\.\.?(/|$)'; then
echo "fc-egress: bad rule: $r" >&2
@@ -244,22 +292,34 @@ in {
systemd.network.networks."40-fcbr0".networkConfig.ConfigureWithoutCarrier = true;
# What the guests may do on the host's network: DHCP and DNS with our
- # dnsmasq, and TLS to nginx (port 443 of any address, redirected to
- # 172.16.0.1:proxyPort). Nothing else, neither to the host (whose own
- # services, like ssh, the guests would otherwise reach at 172.16.0.1,
- # where DNS sends every name, or at any other of its addresses) nor
- # through it (nothing gets forwarded), nor to each other (bridge).
- # These chains only reject; for what they let through, the host's own
- # chains decide, too.
+ # dnsmasq, TLS to nginx (port 443 of any address, redirected to
+ # 172.16.0.1:proxyPort), and what their user's tcp:// and udp:// rules
+ # allow (set allow_USER, see firewallUpdate). Nothing else, neither to
+ # the host (whose own services, like ssh, the guests would otherwise
+ # reach at 172.16.0.1, where DNS sends every name, or at any other of
+ # its addresses) nor through it, nor to each other (bridge). These
+ # chains only reject; for what they let through, the host's own chains
+ # decide, too. Forwarded connections get NATed to the host's address.
networking.nftables.enable = true;
networking.nftables.tables.firecracker = {
family = "inet";
content = ''
+${lib.concatMapStrings (u: ''
+ set allow_${u} {
+ type ipv4_addr . inet_proto . inet_service
+ flags interval
+ }
+'') userNames}
chain prerouting {
type nat hook prerouting priority dstnat;
iifname "fcbr0" tcp dport 443 redirect to :${toString proxyPort}
}
+ chain postrouting {
+ type nat hook postrouting priority srcnat;
+ iifname "fcbr0" masquerade
+ }
+
chain input {
type filter hook input priority filter; policy accept;
iifname != "fcbr0" accept
@@ -267,13 +327,20 @@ in {
ip daddr { 172.16.0.1, 255.255.255.255 } udp dport 67 accept
ip daddr 172.16.0.1 udp dport 53 accept
ip daddr 172.16.0.1 tcp dport { 53, ${toString proxyPort} } accept
+ jump users
reject with icmpx admin-prohibited
}
chain forward {
type filter hook forward priority filter; policy accept;
- iifname "fcbr0" reject with icmpx admin-prohibited
+ iifname != "fcbr0" accept
+ ct state { established, related } accept
+ jump users
+ reject with icmpx admin-prohibited
}
+
+ chain users {
+${lib.concatMapStrings (u: " ip saddr { ${lib.concatStringsSep ", " (ipsOf u)} } ip daddr . meta l4proto . th dport @allow_${u} accept\n") userNames} }
'';
};
# On the bridge: each tap may only send from its own address (or
@@ -343,6 +410,33 @@ in {
"f ${dir}/${u}/allowed 0644 ${u} - -"
]) (lib.attrNames users);
+ # Forwarding, for the tcp:// and udp:// rules.
+ boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
+
+ # Fills the allow_USER sets: at start, whenever nftables gets reloaded
+ # (which empties them), and whenever a user's rules change.
+ systemd.services.fcbr0-firewall = {
+ wantedBy = [ "multi-user.target" ];
+ after = [ "nftables.service" "systemd-tmpfiles-setup.service" ];
+ partOf = [ "nftables.service" ];
+ unitConfig.ReloadPropagatedFrom = "nftables.service";
+ serviceConfig = {
+ Type = "oneshot";
+ RemainAfterExit = true;
+ ExecStart = firewallUpdate;
+ ExecReload = firewallUpdate;
+ };
+ };
+ systemd.paths.fcbr0-firewall = {
+ wantedBy = [ "multi-user.target" ];
+ pathConfig.PathChanged = map (u: "${dir}/${u}/allowed") userNames;
+ pathConfig.Unit = "fcbr0-firewall-reload.service";
+ };
+ systemd.services.fcbr0-firewall-reload = {
+ serviceConfig.Type = "oneshot";
+ serviceConfig.ExecStart = "${pkgs.systemd}/bin/systemctl try-reload-or-restart fcbr0-firewall.service";
+ };
+
# A user of its own rather than DynamicUser, as ca.pem has to be readable
# by anyone (DynamicUser state lives under /var/lib/private, mode 0700).
# nginx binds to 172.16.0.1, so it has to wait for fcbr0's address