fixing test failures

This commit is contained in:
ari committed 2026-09-23 13:24:07 -04:00
1 parent c8ff8b1f25
commit 42e903909c
14 files changed
+823 -111

No files matched your search

+150 -18
View File
@@ -36,6 +36,13 @@ let
anyUsb = pt.usb != [ ];
earlyIds = lib.filter (i: i != null) (map (d: d.id) pt.pci);
# Netfilter mark tagging guest-originated traffic in bridged mode. The
# guest's only way in is the tap interface, so marking at the bridge
# prerouting hook is as specific as an address match - and it is the only
# way to identify the guest at the ip-family layer, where its DHCP-issued
# LAN address is not statically known. 18501 == 0x4845 ("HE"), for hermenix.
bridgeMark = 18501;
# Graceful ACPI power button via QMP, then wait for QEMU to exit.
stopScript = pkgs.writeShellScript "hermes-vm-stop" ''
set -eu
@@ -330,6 +337,26 @@ in
type = types.int;
default = 24;
};
bridged = mkOption {
type = types.bool;
default = false;
description = ''
Put the guest directly on your LAN instead of behind the host's
NAT. The bridge absorbs `externalInterface`, and the guest takes
an address from your LAN's DHCP, so any device on the LAN can
reach any port the guest exposes.
Inbound is no longer limited to `publish` (which is ignored in
this mode): the LAN may reach every guest port. Outbound is still
governed by `lanAccess` / `allowLan`, and `hostAccess` plus the
host cache still work over the host-guest service net
(`hostAddress` stays configured on the bridge).
Requires a working DHCP server on the LAN, and a guest that runs
a DHCP client on its NIC (see guest/common.nix - this repo's
guest does).
'';
};
externalInterface = mkOption {
type = types.nullOr types.str;
default = null;
@@ -734,11 +761,15 @@ in
systemd.network.networks."10-${net.bridge}" = {
matchConfig.Name = net.bridge;
# hostAddress stays on the bridge in both modes: it is the guest's
# service net (host cache, hostAccess). In bridged mode the LAN DHCP
# lease is added on top of it.
address = [ "${net.hostAddress}/${toString net.prefixLength}" ];
networkConfig = {
ConfigureWithoutCarrier = true;
IPv4Forwarding = true;
};
}
// lib.optionalAttrs net.bridged { DHCP = "ipv4"; };
};
systemd.network.networks."11-${net.tapId}" = {
@@ -746,6 +777,17 @@ in
networkConfig.Bridge = net.bridge;
};
# Bridged mode: the physical uplink becomes a bridge port so the LAN
# shares an L2 domain with the guest and the guest's DHCP is answered
# by the LAN itself. The port must not carry its own address config -
# the bridge owns the LAN address now. Note the bridge takes a fresh
# DHCP lease, and because its MAC can differ from the uplink's, the
# host's LAN IP may change after the first switch to bridged mode.
systemd.network.networks."12-${net.externalInterface}" = lib.mkIf net.bridged {
matchConfig.Name = net.externalInterface;
networkConfig.Bridge = net.bridge;
};
# ip_forward: use the same sysctl keys networking.nat used (mkOverride 99
# so they win over any lower-priority defaults elsewhere in the config).
boot.kernel.sysctl."net.ipv4.conf.all.forwarding" = lib.mkOverride 99 true;
@@ -764,19 +806,86 @@ in
# while covering every legitimate external path.
networking.nftables.tables.hermenix-nat = lib.mkIf (net.externalInterface != null) {
family = "ip";
content =
if net.bridged then
# Bridged mode: no DNAT (the guest is directly reachable on the
# LAN) and no primary masquerade. Keep one scoped masquerade as a
# fallback: if the guest's DHCP lease lapses it falls back to its
# static default route via the host, and this keeps that path
# working instead of blackholing at the uplink. With a healthy
# lease the guest's traffic is L2-bridged and never reaches this
# chain. Only traffic handed up from the bridge (i.e. the guest's
# service-net traffic) is matched, never the LAN itself.
''
chain postrouting {
type nat hook postrouting priority srcnat;
iifname "${net.bridge}" oifname "${net.externalInterface}" masquerade
}
''
else
''
chain prerouting {
type nat hook prerouting priority dstnat;
''
+ lib.concatMapStrings (s: ''
iifname != "${net.bridge}" ${s.proto} dport ${toString s.hostPort} dnat to ${net.guestAddress}:${toString s.guestPort}
'') cfg.publish
+ ''
}
chain postrouting {
type nat hook postrouting priority srcnat;
iifname "${net.bridge}" oifname "${net.externalInterface}" masquerade
}
'';
};
# =====================================================================
# Bridged mode: bridge-family enforcement.
#
# With the uplink bridged, guest <-> LAN traffic is switched at L2 and
# never traverses the ip-family hooks the NAT-mode rules rely on, so
# bridged mode gets its own table where the tap port is unambiguous.
# lanAccess / allowLan still gate what the guest may reach; inbound
# from the LAN to the guest is open on every port.
# =====================================================================
networking.nftables.tables."hermenix-bridge" = lib.mkIf net.bridged {
family = "bridge";
content = ''
chain prerouting {
type nat hook prerouting priority dstnat;
type filter hook prerouting priority filter; policy accept;
# Tag anything entering from the guest. ip-family input rules
# (host cache, hostAccess) match this mark.
iifname "${net.tapId}" meta mark set ${toString bridgeMark}
}
chain forward {
type filter hook forward priority filter; policy accept;
# Inbound from the LAN to the guest: any device, any port.
oifname "${net.tapId}" accept
''
+ lib.concatMapStrings (s: ''
iifname != "${net.bridge}" ${s.proto} dport ${toString s.hostPort} dnat to ${net.guestAddress}:${toString s.guestPort}
'') cfg.publish
iifname "${net.tapId}" ip daddr ${s.address} ${s.proto} dport ${toString s.port} accept
'') cfg.lanAccess
+ (
if cfg.allowLan then
''
iifname "${net.tapId}" accept
''
else
''
iifname "${net.tapId}" ip daddr {
10.0.0.0/8,
172.16.0.0/12,
192.168.0.0/16,
169.254.0.0/16,
127.0.0.0/8,
100.64.0.0/10
} drop
iifname "${net.tapId}" ip6 daddr { fc00::/7, fe80::/10, ::1/128 } drop
iifname "${net.tapId}" accept
''
)
+ ''
}
chain postrouting {
type nat hook postrouting priority srcnat;
iifname "${net.bridge}" oifname "${net.externalInterface}" masquerade
}
'';
};
@@ -792,15 +901,35 @@ in
# The deliberate guest -> host holes: the Nix cache, plus anything listed
# in hostAccess.
#
# NAT mode: the guest always arrives from its fixed service-net address,
# so match address + bridge. Bridged mode: the guest's LAN address comes
# from DHCP and is not known here, so the bridge table above tags guest
# traffic with a netfilter mark as it enters the tap - the rules match
# that mark instead. The tap is exclusive to the guest, so the mark is
# as specific as an address match: LAN devices can never inherit these
# holes.
networking.firewall.extraInputRules =
optionalString cfg.hostCache.enable ''
iifname "${net.bridge}" ip saddr ${net.guestAddress} tcp dport ${toString cfg.hostCache.port} accept
''
+ lib.concatMapStrings (s: ''
iifname "${net.bridge}" ip saddr ${net.guestAddress} ${s.proto} dport ${toString s.port} accept
'') cfg.hostAccess;
if net.bridged then
optionalString cfg.hostCache.enable ''
meta mark ${toString bridgeMark} tcp dport ${toString cfg.hostCache.port} accept
''
+ lib.concatMapStrings (s: ''
meta mark ${toString bridgeMark} ${s.proto} dport ${toString s.port} accept
'') cfg.hostAccess
else
optionalString cfg.hostCache.enable ''
iifname "${net.bridge}" ip saddr ${net.guestAddress} tcp dport ${toString cfg.hostCache.port} accept
''
+ lib.concatMapStrings (s: ''
iifname "${net.bridge}" ip saddr ${net.guestAddress} ${s.proto} dport ${toString s.port} accept
'') cfg.hostAccess;
# Order matters here. Allowlists must precede the RFC1918 drop.
#
# These ip-family rules only ever see routed traffic. They are the whole
# story in NAT mode; in bridged mode the guest's LAN traffic is
# L2-bridged instead and is governed by the hermenix-bridge table.
networking.firewall.extraForwardRules = ''
iifname != "${net.bridge}" accept
ct state established,related accept
@@ -809,10 +938,13 @@ in
+ lib.concatMapStrings (s: ''
iifname "${net.bridge}" ip daddr ${s.address} ${s.proto} dport ${toString s.port} accept
'') cfg.lanAccess
# Inbound to guest services published on the uplink.
+ lib.concatMapStrings (s: ''
oifname "${net.bridge}" ip daddr ${net.guestAddress} ${s.proto} dport ${toString s.guestPort} accept
'') cfg.publish
# Inbound to guest services published on the uplink. NAT mode only; in
# bridged mode the bridge table opens every inbound port instead.
+ lib.optionalString (!net.bridged) (
lib.concatMapStrings (s: ''
oifname "${net.bridge}" ip daddr ${net.guestAddress} ${s.proto} dport ${toString s.guestPort} accept
'') cfg.publish
)
+ (
if cfg.allowLan then
''