aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-20 11:04:12 +0000
committertv <tv@krebsco.de>2026-11-20 11:04:12 +0000
commitc12cc5a78f0b4b153fbe71c2ea3d5ff06d8cde2d (patch)
tree751b7a4a86c65ac43526b4d11094663b719c6a9d
parenta623b0d5de4db7d484371ad5bc6af7d4a0a93aa6 (diff)
host: IPv6 for the guests' tcp:// and udp:// rules
The guests get IPv6: fcbr0 has fdfc:fc::1/64 (module argument prefix6), the host's dnsmasq sends router advertisements, and each guest takes the address from its MAC (SLAAC with EUI-64; the guest's networkd is set up for that, without privacy addresses), so the host knows each tap's address, as with IPv4. The rules take IPv6 addresses in brackets, tcp://[42::1]:22 or udp://[42::/16]:5000-5100, and names now resolve to both families (and get AAAA records for the guests). Forwarded connections get NATed to the host's address, like IPv4. Like IPv4 everything else is refused: the bridge only lets a tap send from its own addresses (or :: for duplicate address detection), the host only answers neighbour and router discovery, denyDestinations and the per-guest limits apply. HTTPS stays IPv4, through the proxy: IPv6 port 443 is refused.
-rw-r--r--README.md16
-rw-r--r--modules/host/default.nix115
-rw-r--r--pkgs/run-firecracker-vm/default.nix6
3 files changed, 110 insertions, 27 deletions
diff --git a/README.md b/README.md
index e2c809f..0c47dff 100644
--- a/README.md
+++ b/README.md
@@ -26,8 +26,8 @@ imports = [
```
`users` says who may run VMs, and how many at the same time: each gets
-that many tap devices. This sets up a bridge `fcbr0` (172.16.0.1/24)
-with the taps, DHCP and DNS for the guests, the egress proxy, and
+that many tap devices. This sets up a bridge `fcbr0` (172.16.0.1/24,
+and fdfc:fc::1/64) with the taps, DHCP and DNS for the guests, the egress proxy, and
nftables rules that keep the guests from reaching anything else, on the
host, beyond, or each other. Each tap has a fixed address, and the bridge
drops anything a guest sends from another, so the proxy knows whose a
@@ -37,6 +37,10 @@ Optional arguments:
- `proxyPort` (default 10443): where the proxy listens; the guests' port
443 gets redirected there.
+- `prefix6` (default `fdfc:fc::`): the guests' IPv6 /64. Each guest
+ gets the address from its MAC (SLAAC, from router advertisements by
+ the host's dnsmasq), for tcp:// and udp:// rules; HTTPS stays IPv4,
+ through the proxy.
- `denyDestinations`: addresses no whitelist can open, neither for
tcp:// and udp:// rules nor through the proxy, whatever a name resolves
to. Default: loopback, private, link-local, CGNAT, multicast and the
@@ -145,9 +149,11 @@ when the guest gets built, so build the guest on the host after the proxy
has started once. Everything else, and anything that isn't HTTPS, gets
refused.
-tcp:// and udp:// rules take IPv4 addresses or names. The guests
-resolve every name to the host, except the names in such rules: the host
-resolves those (IPv4) and tells the guests the same addresses, so
+tcp:// and udp:// rules take IPv4 addresses, IPv6 addresses in brackets
+(`tcp://[42::1]:22`, `udp://[42::/16]:5000-5100`; not port 443, HTTPS
+goes through the proxy, over IPv4), or names. The guests resolve every
+name to the host, except the names in such rules: the host resolves
+those (IPv4 and IPv6) and tells the guests the same addresses, so
`ssh git.example.org` works. They get resolved again every 5 minutes.
(Every guest gets those addresses for those names, but only the VMs
whose list has the rule can connect.) Those connections get forwarded, NATed to the host's address, and logged
diff --git a/modules/host/default.nix b/modules/host/default.nix
index 9a40f77..5df9a7c 100644
--- a/modules/host/default.nix
+++ b/modules/host/default.nix
@@ -42,6 +42,11 @@
# DNS (udp and tcp 53) and the proxy (tcp proxyPort, after the redirect).
{ users
, proxyPort ? 10443
+ # The guests' IPv6 /64 (on fcbr0, the host is ::1): each guest gets the
+ # address from its MAC (SLAAC with EUI-64, see taps), for the tcp:// and
+ # udp:// rules. Unique local, so forwarded connections get NATed, like
+ # IPv4.
+, prefix6 ? "fdfc:fc::"
# Where no whitelist can let guests go, neither via tcp:// and udp://
# rules nor via the HTTPS proxy, whatever a name resolves to. The host's
# own addresses are always out of reach (but for DHCP, DNS and the proxy).
@@ -54,13 +59,19 @@
, maxConnections ? 256
, maxNewConnections ? 50
}: { lib, pkgs, ... }: let
- # [ { name = "fctap0"; user = ...; ip = "172.16.0.2"; mac = ...; } ... ]
+ # [ { name = "fctap0"; user = ...; ip = "172.16.0.2"; ip6 = ...;
+ # ll6 = ...; mac = ...; } ... ]
taps = let
hex = n: lib.fixedWidthString 2 "0" (lib.toLower (lib.toHexString n));
+ # The EUI-64 interface ID of the MAC below (in the canonical form, as
+ # nft and nginx print addresses).
+ eui64 = i: "400:acff:fe10:${lib.toLower (lib.toHexString i)}";
tap = i: user: {
name = "fctap${toString i}";
inherit user;
ip = "172.16.0.${toString (i + 2)}";
+ ip6 = "${prefix6}${eui64 i}";
+ ll6 = "fe80::${eui64 i}";
mac = "06:00:ac:10:${hex (i / 256)}:${hex (lib.mod i 256)}"; # as the run script
};
go = i: names:
@@ -85,11 +96,12 @@
tapsOf = user: lib.filter (t: t.user == user) taps;
ipsOf = user: map (t: t.ip) (tapsOf user);
- # tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT], tcp://NAME:PORT[-PORT], or
- # udp://... (nft checks the numbers.)
+ # tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT], tcp://[ADDRESS6[/PREFIXLEN]]:...,
+ # tcp://NAME:PORT[-PORT], or udp://... (nft checks the numbers.)
addressRule = "[0-9]+(\\.[0-9]+){3}(/[0-9]+)?";
+ address6Rule = "\\[[0-9A-Fa-f:]+(/[0-9]+)?\\]";
hostName = "[A-Za-z0-9][A-Za-z0-9-]*(\\.[A-Za-z0-9-]+)*";
- portRule = "(tcp|udp)://(${addressRule}|${hostName}):[0-9]+(-[0-9]+)?";
+ portRule = "(tcp|udp)://(${addressRule}|${address6Rule}|${hostName}):[0-9]+(-[0-9]+)?";
# The addresses of the names in the tcp:// and udp:// rules, as the
# firewall update resolves them, a hosts file per tap, for the guests'
@@ -101,8 +113,8 @@
# Puts the tcp:// and udp:// rules of the given taps' VMs (default: all;
# a user's name means all of their taps) into the taps' nftables sets,
# one tap at a time, so one bad rule doesn't keep the others' out, with
- # names resolved by the host (IPv4; a name that doesn't resolve allows
- # nothing), and those names' addresses into the taps' hosts files.
+ # names resolved by the host (IPv4 and IPv6; a name that doesn't resolve
+ # allows nothing), and those names' addresses into the taps' hosts files.
# Doesn't follow symlinks (it runs with CAP_NET_ADMIN, though not as
# root, see fcbr0-firewall).
firewallUpdate = pkgs.writeShellScript "fcbr0-firewall-update" ''
@@ -129,29 +141,37 @@ ${lib.concatMapStrings (t: " ${t.name}) user=${t.user} ;;\n") taps}
rules=$(head -c ${toString maxRulesSize} "$file" | grep -xE '${portRule}') || :
fi
elements=
+ elements6=
hosts=
for rule in $rules; do
proto=''${rule%%://*}
rest=''${rule#*://}
host=''${rest%:*}
ports=''${rest##*:}
- if printf %s "$host" | grep -qxE '${addressRule}'; then
+ if printf %s "$host" | grep -qxE '${addressRule}|${address6Rule}'; then
addresses=$host
else
- addresses=$(timeout 5 getent ahostsv4 "$host" | awk '{ print $1 }' | sort -u) || :
+ addresses=$(timeout 5 getent ahosts "$host" | awk '{ print $1 }' | sort -u) || :
for a in $addresses; do
hosts="$hosts$a $host"$'\n'
done
fi
for a in $addresses; do
- elements="$elements''${elements:+, }$a . $proto . $ports"
+ case $a in
+ *:*) a=''${a#[}; elements6="$elements6''${elements6:+, }''${a%]} . $proto . $ports" ;;
+ *) elements="$elements''${elements:+, }$a . $proto . $ports"
+ esac
done
done
{
echo "flush set inet firecracker allow_$tap"
+ echo "flush set inet firecracker allow6_$tap"
if test -n "$elements"; then
echo "add element inet firecracker allow_$tap { $elements }"
fi
+ if test -n "$elements6"; then
+ echo "add element inet firecracker allow6_$tap { $elements6 }"
+ fi
} | nft -f - || { echo "fcbr0-firewall: rules of $tap ($user: $list) not applied" >&2; status=1; continue; }
# dnsmasq reads new and changed files, but not dot files.
printf %s "$hosts" | sort -u > ${hostsDir}/.$tap
@@ -210,7 +230,8 @@ ${lib.concatMapStrings (t: " ${t.name}) user=${t.user} ;;\n") taps}
echo "RULE is https://DOMAIN (with subdomains, not decrypted)"
echo " or https://DOMAIN/PATH-PREFIX (decrypted to check the path)"
echo " or tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT] (likewise udp://...)"
- echo " or tcp://NAME:PORT[-PORT] (resolved by the host, IPv4)"
+ echo " or tcp://[ADDRESS6[/PREFIXLEN]]:PORT[-PORT] (IPv6, not port 443)"
+ echo " or tcp://NAME:PORT[-PORT] (resolved by the host)"
echo "vms shows the running VMs' taps (as in the log), names and lists"
echo "use switches a running VM to another list"
}
@@ -244,15 +265,20 @@ ${lib.concatMapStrings (t: " ${t.name}) user=${t.user} ;;\n") taps}
check() {
for r; do
if printf %s "$r" | grep -qxE '${portRule}'; then
- # numbers in range: address bytes, prefix length, ports
+ # numbers in range: address bytes, prefix length, ports (IPv6
+ # addresses: nft)
if printf %s "$r" | awk '{
s = $0; sub(/^[a-z]+:\/\//, "", s)
- split(s, hp, ":"); split(hp[1], a, "/"); split(a[1], b, ".")
- if (hp[1] ~ /^[0-9.\/]+$/) {
+ match(s, /:[0-9-]+$/); host = substr(s, 1, RSTART - 1); ports = substr(s, RSTART + 1)
+ split(host, a, "/"); split(a[1], b, ".")
+ if (host ~ /^\[/) {
+ sub(/\]$/, "", a[2])
+ if (a[2] != "" && a[2] > 128) exit 1
+ } else if (host ~ /^[0-9.\/]+$/) {
for (i = 1; i <= 4; i++) if (b[i] > 255) exit 1
if (a[2] != "" && a[2] > 32) exit 1
- } else if (length(hp[1]) > 253) exit 1
- n = split(hp[2], p, "-")
+ } else if (length(host) > 253) exit 1
+ n = split(ports, 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
@@ -323,7 +349,9 @@ ${lib.concatMapStrings (t: " ${t.name}) user=${t.user} ;;\n") taps}
# the packet. The URIs are as rules take them, for copying.
journalctl -o short-iso _SYSTEMD_UNIT=fcbr0-egress.service + _TRANSPORT=kernel "$@" |
grep --line-buffered -E " (ALLOW|DENY) $user " |
- sed -u -E 's/( fc-egress (ALLOW|DENY) [^ ]+ [^ ]+ )(.* DST=([0-9.]+) .*PROTO=(TCP|UDP) .*DPT=([0-9]+).*)$/\1\L\5\E:\/\/\4:\6 \3/'
+ sed -u -E \
+ -e 's/( fc-egress (ALLOW|DENY) [^ ]+ [^ ]+ )(.* DST=([0-9.]+) .*PROTO=(TCP|UDP) .*DPT=([0-9]+).*)$/\1\L\5\E:\/\/\4:\6 \3/' \
+ -e 's/( fc-egress (ALLOW|DENY) [^ ]+ [^ ]+ )(.* DST=([0-9a-f:]+) .*PROTO=(TCP|UDP) .*DPT=([0-9]+).*)$/\1\L\5\E:\/\/[\4]:\6 \3/'
;;
help|-h|--help)
usage
@@ -474,6 +502,7 @@ in {
virtualOwner = t.user;
}) taps) // {
fcbr0.ipv4.addresses = [{ address = "172.16.0.1"; prefixLength = 24; }];
+ fcbr0.ipv6.addresses = [{ address = "${prefix6}1"; prefixLength = 64; }];
};
networking.bridges.fcbr0.interfaces = map (t: t.name) taps;
# With networkd, fcbr0 would only get its address while it has carrier,
@@ -509,6 +538,11 @@ ${lib.concatMapStrings (t: ''
type ipv4_addr . inet_proto . inet_service
flags interval
}
+
+ set allow6_${t.name} {
+ type ipv6_addr . inet_proto . inet_service
+ flags interval
+ }
'') taps}
set deny4 {
type ipv4_addr
@@ -535,6 +569,19 @@ ${lib.concatMapStrings (t: ''
size 1024
}
+ set guest_conns6 {
+ type ipv6_addr
+ flags dynamic
+ size 1024
+ }
+
+ set guest_rate6 {
+ type ipv6_addr
+ flags dynamic, timeout
+ timeout 1m
+ size 1024
+ }
+
# Before the host's own NAT rules (which are for connections to the
# host, like a redirect of some port to its sshd), so they don't get
# applied to the guests' connections: the first NAT a connection gets
@@ -542,8 +589,9 @@ ${lib.concatMapStrings (t: ''
# nothing.
chain prerouting {
type nat hook prerouting priority dstnat - 1;
- iifname "fcbr0" tcp dport 443 redirect to :${toString proxyPort}
+ iifname "fcbr0" meta nfproto ipv4 tcp dport 443 redirect to :${toString proxyPort}
iifname "fcbr0" meta nfproto ipv4 meta l4proto { tcp, udp } dnat ip to ip daddr
+ iifname "fcbr0" meta nfproto ipv6 meta l4proto { tcp, udp } dnat ip6 to ip6 daddr
}
chain postrouting {
@@ -555,6 +603,8 @@ ${lib.concatMapStrings (t: ''
type filter hook input priority filter; policy accept;
iifname != "fcbr0" accept
ct state { established, related } accept
+ # IPv6 neighbour and router discovery (dnsmasq sends the RAs)
+ icmpv6 type { nd-router-solicit, nd-neighbor-solicit, nd-neighbor-advert, mld2-listener-report } accept
jump limits
ip daddr { 172.16.0.1, 255.255.255.255 } udp dport 67 accept
ip daddr 172.16.0.1 udp dport 53 accept
@@ -568,6 +618,9 @@ ${lib.concatMapStrings (t: ''
ct state { established, related } accept
jump limits
ip daddr @deny4 jump refused
+ ip6 daddr @deny6 jump refused
+ # HTTPS goes via nginx, which is IPv4 only (see prerouting).
+ meta nfproto ipv6 tcp dport 443 jump refused
jump vms
jump refused
}
@@ -575,16 +628,20 @@ ${lib.concatMapStrings (t: ''
chain limits {
ct state new add @guest_conns { ip saddr ct count over ${toString maxConnections} } reject with icmpx admin-prohibited
ct state new update @guest_rate { ip saddr limit rate over ${toString maxNewConnections}/second burst ${toString (2 * maxNewConnections)} packets } reject with icmpx admin-prohibited
+ ct state new add @guest_conns6 { ip6 saddr ct count over ${toString maxConnections} } reject with icmpx admin-prohibited
+ ct state new update @guest_rate6 { ip6 saddr limit rate over ${toString maxNewConnections}/second burst ${toString (2 * maxNewConnections)} packets } reject with icmpx admin-prohibited
}
chain vms {
${lib.concatMapStrings (t: ''
ip saddr ${t.ip} ip daddr . meta l4proto . th dport @allow_${t.name} limit rate 10/second burst 50 packets log prefix "fc-egress ALLOW ${t.user} ${t.name} " level info
ip saddr ${t.ip} ip daddr . meta l4proto . th dport @allow_${t.name} accept
+ ip6 saddr ${t.ip6} ip6 daddr . meta l4proto . th dport @allow6_${t.name} limit rate 10/second burst 50 packets log prefix "fc-egress ALLOW ${t.user} ${t.name} " level info
+ ip6 saddr ${t.ip6} ip6 daddr . meta l4proto . th dport @allow6_${t.name} accept
'') taps} }
chain refused {
-${lib.concatMapStrings (t: " ip saddr ${t.ip} limit rate 10/second burst 50 packets log prefix \"fc-egress DENY ${t.user} ${t.name} \" level info\n") taps} reject with icmpx admin-prohibited
+${lib.concatMapStrings (t: " ip saddr ${t.ip} limit rate 10/second burst 50 packets log prefix \"fc-egress DENY ${t.user} ${t.name} \" level info\n") taps}${lib.concatMapStrings (t: " ip6 saddr ${t.ip6} limit rate 10/second burst 50 packets log prefix \"fc-egress DENY ${t.user} ${t.name} \" level info\n") taps} reject with icmpx admin-prohibited
}
# Only nginx's packets: others, including those the kernel sends on
@@ -605,9 +662,9 @@ ${lib.concatMapStrings (t: " ip saddr ${t.ip} limit rate 10/second burst
}
'';
};
- # On the bridge: each tap may only send from its own address (or
- # 0.0.0.0, for DHCP and ARP probes), IPv4 and ARP only, and nothing to
- # other taps.
+ # On the bridge: each tap may only send from its own addresses (or
+ # 0.0.0.0, for DHCP and ARP probes, or ::, for IPv6 duplicate address
+ # detection), IPv4, ARP and IPv6 only, and nothing to other taps.
networking.nftables.tables.firecracker-bridge = {
family = "bridge";
content = ''
@@ -616,6 +673,11 @@ ${lib.concatMapStrings (t: " ip saddr ${t.ip} limit rate 10/second burst
elements = { ${lib.concatMapStringsSep ", " (t: "\"${t.name}\" . ${t.ip}") taps} }
}
+ set guests6 {
+ type ifname . ipv6_addr
+ elements = { ${lib.concatMapStringsSep ", " (t: "\"${t.name}\" . ${t.ip6}, \"${t.name}\" . ${t.ll6}") taps} }
+ }
+
chain prerouting {
type filter hook prerouting priority filter; policy accept;
iifname != "fctap*" accept
@@ -623,6 +685,8 @@ ${lib.concatMapStrings (t: " ip saddr ${t.ip} limit rate 10/second burst
ether type ip ip saddr 0.0.0.0 udp dport 67 accept
ether type arp iifname . arp saddr ip @guests accept
ether type arp arp saddr ip 0.0.0.0 accept
+ ether type ip6 iifname . ip6 saddr @guests6 accept
+ ether type ip6 ip6 saddr :: icmpv6 type { nd-neighbor-solicit, mld2-listener-report } accept
drop
}
@@ -659,6 +723,10 @@ ${lib.concatMapStrings (t: " ip saddr ${t.ip} limit rate 10/second burst
"--except-interface=lo"
"--dhcp-authoritative"
"--dhcp-range=172.16.0.0,static"
+ # IPv6: router advertisements only, the guests take their
+ # addresses from their MACs.
+ "--enable-ra"
+ "--dhcp-range=${prefix6},ra-only"
"--dhcp-leasefile=/run/fcbr0-dnsmasq/leases"
"--no-resolv"
"--no-hosts"
@@ -686,8 +754,11 @@ ${lib.concatMapStrings (t: " ip saddr ${t.ip} limit rate 10/second burst
"f ${vmDir}/${t.name}.name 0644 ${t.user} - -"
]) taps;
- # Forwarding, for the tcp:// and udp:// rules.
+ # Forwarding, for the tcp:// and udp:// rules. (With IPv6 forwarding,
+ # the kernel ignores router advertisements on interfaces with accept_ra
+ # = 1; networkd, which handles them itself, doesn't mind.)
boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
+ boot.kernel.sysctl."net.ipv6.conf.all.forwarding" = 1;
# Fills the allow_TAP sets: all of them at start and whenever nftables
# gets reloaded (which empties them), a tap's (fcbr0-firewall@TAP)
diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix
index 855b43c..6df6222 100644
--- a/pkgs/run-firecracker-vm/default.nix
+++ b/pkgs/run-firecracker-vm/default.nix
@@ -217,6 +217,12 @@ let
# The default client identifier is derived from the machine-id,
# which is the same in every VM, so they'd all get the same lease.
dhcpV4Config.ClientIdentifier = "mac";
+ # IPv6: the address from the MAC, which the host expects (see
+ # modules/host), and DNS stays the host's IPv4 address.
+ networkConfig.IPv6AcceptRA = true;
+ networkConfig.IPv6PrivacyExtensions = "no";
+ ipv6AcceptRAConfig.Token = "eui64";
+ ipv6AcceptRAConfig.UseDNS = false;
};
boot.bcache.enable = false;