From 86d6f7341fedd5e0e78969d2c37829a0012dc3ae Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 21 Nov 2026 04:10:08 +0000 Subject: host: static addresses for VMs, by name, on whichever tap they run A VM got the first free tap of its user's, and a tap's address came from its number, so a VM's address depended on what else ran, and on the users before it, and a reverse proxy on the host could only guess it (172.16.0.2, i.e. fctap0, for hermes). Now addresses are the VMs': the host module takes vms, VM name -> user and address (vms = { hermes = { user = "tv"; ip = "172.16.0.2"; }; }), any number of them, and a VM takes any free tap of its user's, which gets its address while it runs, so at most as many run at the same time as the user has taps. Taps have addresses of their own, for VMs without one: from 172.16.0.200 on, so they stand out (but for VMs' there). The MAC address, which dnsmasq hands out every address by, is 06:00 and the address (06:00:ac:10:00:02 for 172.16.0.2), and with it the IPv6 addresses; the run script takes the VM's from /etc/fc-vms, or the tap's own from /etc/fc-taps. The VMs' addresses are also in config.fc.vms.NAME (user, ip, ip6), for the host's configuration, e.g. proxyPass = "http://${config.fc.vms.hermes.ip}:9120"; Which address a tap has is up to fcbr0-firewall, which now also runs when a tap's VM's name (/run/fc-egress/TAP.name, which the run script writes) changes: the VM's, if it's one of the tap's user's in vms and on none of their other taps, too, else the tap's own. It puts it into the tap's new nftables sets addr_TAP and addr6_TAP, in the bridge's table, which lets the tap send from nothing else (instead of the static set guests), and in the other, whose per-tap rules go by them (instead of the tap's fixed addresses); and into /run/fcbr0-addr, ADDRESS: TAP and TAP: ADDRESS, where egress.lua finds a guest's tap (as long as both agree, and the address and the tap are the same user's), and the run script waits for its address before starting the guest. fc-egress vms shows the taps' addresses, too. --- modules/host/egress.lua | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'modules/host/egress.lua') diff --git a/modules/host/egress.lua b/modules/host/egress.lua index 172805a..d276e05 100644 --- a/modules/host/egress.lua +++ b/modules/host/egress.lua @@ -1,7 +1,8 @@ -- The guests' egress policy, for nginx (OpenResty), see default.nix. -- --- Per VM: which tap (and so whose) a guest is, egress_taps.lua says by its --- address (which the bridge makes sure of, see default.nix). VMS/TAP has +-- Per VM: whose a guest is, egress_guests.lua says by its address (which +-- the bridge makes sure of, see default.nix), and which tap it's on, +-- ADDR/ADDRESS and ADDR/TAP, as long as they agree. VMS/TAP has -- the name of the VM's list, RULES/USER/LIST its rules, one per line, both -- read on every connection and request: -- https://example.com example.com and its subdomains, passed @@ -21,10 +22,31 @@ local RULES = os.getenv("FC_EGRESS_RULES") -- RULES/USER/LIST local VMS = os.getenv("FC_EGRESS_VMS") -- VMS/TAP: LIST local STATE = os.getenv("FC_EGRESS_STATE") -- ca.key, ca.pem local RUN = os.getenv("FC_EGRESS_RUN") -- RUN/decrypt-TAP.sock: the http server -local guests = require("egress_taps") -- guest address -> { tap, user } +local ADDR = os.getenv("FC_EGRESS_ADDR") -- ADDR/TAP: ADDRESS, ADDR/ADDRESS: TAP +-- { addresses = { [ADDRESS] = USER }, taps = { [TAP] = USER } } +local owners = require("egress_guests") local by_tap = {} -for _, g in pairs(guests) do - by_tap[g.tap] = g +for tap, user in pairs(owners.taps) do + by_tap[tap] = { tap = tap, user = user } +end + +local function first_line(path) + local f = io.open(path) + local s = f and f:read("*l") + if f then + f:close() + end + return s +end + +-- The guest at an address: { tap, user }, or nil. +local function guest_at(address) + local user = owners.addresses[address] + local tap = user and first_line(ADDR .. "/" .. address) + local guest = tap and by_tap[tap] + if guest and guest.user == user and first_line(ADDR .. "/" .. tap) == address then + return guest + end end local function valid_name(s) @@ -93,7 +115,7 @@ end -- stream, preread: pass through, decrypt, or close function _M.preread() local sni = ngx.var.ssl_preread_server_name - local guest = guests[ngx.var.remote_addr] + local guest = guest_at(ngx.var.remote_addr) local name, who if guest then name, who = list(guest) -- cgit v1.3.1