Render the packet filter's allowances beside the derived ports

The operator, 2026-11-21: a firewall module has tools to open a port by hand
beside the ports derived from the modules' listens, and the node-engine stays
the one renderer of the filter file. AsNftablesAllowing renders each allowance
the holder answered as one accept rule in the input chain after the derived
ones: its interface by name or a negated match on the private network's
interface, its source by its family, an until as the one element of a timeout
set so the kernel closes the port, and the comment allowance <key> <at>. With
no allowance the file is today's, byte for byte.

Builds: design 08 control-verbs §2, §8; ADR 0683; H-A-305, H-A-308 (1), H-A-315 (4), (5)
This commit is contained in:
2026-11-21 22:32:06 +01:00
parent a2332bc6a5
commit 2a29c458ce
4 changed files with 147 additions and 5 deletions
+78 -3
View File
@@ -2,20 +2,25 @@ package resolution
import (
"fmt"
"math"
"slices"
"sort"
"strconv"
"strings"
"time"
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodepacketfilter"
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
)
// Filtering is the whole rule set for the node this resolved for.
//
// **Derived from what is assigned here, and from the overlay's shape** — a node's open ports are
// a consequence of what runs on it, not a second list kept in step by hand. Nothing else opens a
// port: **what is not declared is closed**, which is the property that makes the derivation worth
// having rather than merely tidy.
// a consequence of what runs on it, not a second list kept in step by hand. Beside them only the
// allowances are rendered, the ports opened by hand through the seat node-packet-filter: nothing else
// writes the chain; the allowances come from the holder (Allowing; design 08 control-verbs §1, §2).
// **What is neither declared nor allowed is closed**, which is the property that makes the derivation
// worth having rather than merely tidy.
func Filtering(r mcat.Resolution, computed map[string]mcat.Generator, ports map[string]map[int]int, exposure map[string]map[int]string) ([]mcat.Rule, error) {
// Keyed by what actually distinguishes an opening. Two modules wanting :443 from the mesh is
// one rule with two sources; one wanting it from the mesh and another from anywhere is two,
@@ -183,6 +188,28 @@ func widest(rules []mcat.Rule) []mcat.Rule {
// anywhere and not dropped silently: the rule set says the port wanted the local network and found none.
func AsNftablesWithLocal(rules []mcat.Rule, mesh []string, outward bool, foundation []int,
outwardLinks []string, tunnel string, local []string) string {
return AsNftablesAllowing(rules, mesh, outward, foundation, outwardLinks, tunnel, local, Allowing{})
}
// Allowing is what the holder of the seat node-packet-filter answered its verb allowed at a compose of the filter, and the
// time of that compose (design 08 control-verbs §1, §2): the allowances rendered beside the derived ports.
type Allowing struct {
Allowances []nodepacketfilter.Allowance
At time.Time
}
// AsNftablesAllowing is AsNftablesWithLocal with the allowances the holder answered (design 08 control-verbs §2): one accept
// rule per allowance in the input chain, after the derived ports' rules and before the chain's drop policy, each with the
// comment `allowance <key> <at>`. Its interface by name, or for the default set a negated match on the private network's
// interface (tunnel), so an interface that comes up later is covered and the tunnel never is; its source when it has one,
// for the family of its CIDR. An allowance with an until matches its port as the one element of a set of the table with
// the timeout flag, the element's timeout the time left to its until at allowing.At, at least 1 s, so the kernel closes the
// port at its until even while the holder is away (H-A-315 (4)). The forward chain takes none: an allowance opens the input
// path only. With no allowance the file is AsNftablesWithLocal's, byte for byte.
func AsNftablesAllowing(rules []mcat.Rule, mesh []string, outward bool, foundation []int,
outwardLinks []string, tunnel string, local []string, allowing Allowing) string {
allowances := slices.Clone(allowing.Allowances)
sort.Slice(allowances, func(a, b int) bool { return allowances[a].Key < allowances[b].Key })
// The links that are not this machine's own: the ones facing outside, and the mesh's tunnel.
// Traffic arriving on any of them is admitted only by a rule below; traffic arriving anywhere
// else is this machine's own guest and is not something the mesh has a position on.
@@ -207,6 +234,14 @@ func AsNftablesWithLocal(rules []mcat.Rule, mesh []string, outward bool, foundat
// runtime writes for its bridges. Reloading the mesh's rules must not stop every container.
b.WriteString("table inet mesh {}\ndelete table inet mesh\n\n")
b.WriteString("table inet mesh {\n")
// The sets of the allowances with an until, declared before the chain that matches them.
for i, a := range allowances {
if a.Until == "" {
continue
}
b.WriteString(fmt.Sprintf("\tset %s {\n\t\ttype inet_service\n\t\tflags timeout\n", allowanceSet(i)))
b.WriteString(fmt.Sprintf("\t\telements = { %d timeout %ds }\n\t}\n", a.Port, timeLeft(a.Until, allowing.At)))
}
b.WriteString("\tchain input {\n")
// Dropping by default is the whole point, and the three lines under it are what keep dropping
// by default from meaning "unreachable": a reply to something this node started, its own
@@ -375,6 +410,33 @@ func AsNftablesWithLocal(rules []mcat.Rule, mesh []string, outward bool, foundat
}
}
// The allowances, set by hand through the seat node-packet-filter and answered by its holder (design 08 control-verbs §2).
if len(allowances) > 0 {
b.WriteString("\n\t\t# allowances, opened by hand through the seat node-packet-filter\n")
}
for i, a := range allowances {
var match []string
switch {
case a.Interface != "":
match = append(match, fmt.Sprintf("iifname %q", a.Interface))
case tunnel != "":
match = append(match, fmt.Sprintf("iifname != %q", tunnel))
}
if a.Source != "" {
family := "ip"
if strings.Contains(a.Source, ":") {
family = "ip6"
}
match = append(match, fmt.Sprintf("%s saddr %s", family, a.Source))
}
port := strconv.FormatInt(a.Port, 10)
if a.Until != "" {
port = "@" + allowanceSet(i)
}
match = append(match, fmt.Sprintf("%s dport %s", a.Protocol, port))
b.WriteString(fmt.Sprintf("\t\t%s accept comment %q\n", strings.Join(match, " "), "allowance "+a.Key+" "+a.At))
}
b.WriteString("\t}\n")
// Nothing this node sends is filtered, and it is stated rather than left to nftables' default
// so that reading this file answers the question instead of requiring the reader to know it.
@@ -499,6 +561,19 @@ func AsNftablesWithLocal(rules []mcat.Rule, mesh []string, outward bool, foundat
return b.String()
}
// allowanceSet is the name of the set of the i-th allowance (by key) with an until.
func allowanceSet(i int) string { return fmt.Sprintf("allowance_%d", i+1) }
// timeLeft is the whole seconds from at to until, at least 1 (design 08 control-verbs §2): an until already passed, or one
// that does not read as RFC 3339 (the ask refuses such an answer before a compose renders it), closes the port at once.
func timeLeft(until string, at time.Time) int64 {
u, err := time.Parse(time.RFC3339, until)
if err != nil {
return 1
}
return max(int64(math.Ceil(u.Sub(at).Seconds())), 1)
}
// byFamily splits addresses into the two nftables understands separately.
//
// `ip saddr` and `ip6 saddr` are different matches, and one set holding both families is a syntax
@@ -5,7 +5,9 @@ import (
"fmt"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodepacketfilter"
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
)
@@ -113,6 +115,70 @@ func TestTheModuleAskingForTheRuleSetGetsEveryModulesPorts(t *testing.T) {
t.Errorf("expose %v: the left-out module's 5100 opens from %v, want %s", row.expose, from, row.from)
}
}
// The allowances the holder of node-packet-filter answered, rendered beside the derived ports (design 08 control-verbs §2,
// §8; H-A-315 (4), (5)).
t.Run("allowances", func(t *testing.T) {
derived := []mcat.Rule{{Port: 443, Protocol: "tcp", From: mcat.FromEverywhere, Because: []string{"web"}}}
at := time.Date(2026, 11, 21, 20, 0, 0, 0, time.UTC)
render := func(allowances ...nodepacketfilter.Allowance) string {
return AsNftablesAllowing(derived, []string{"10.10.0.1"}, false, nil, []string{"eth0"}, "mesh0", nil,
Allowing{Allowances: allowances, At: at})
}
inputOf := func(out string) string {
in, _, _ := strings.Cut(out, "\tchain output")
return in
}
// 08 control-verbs §8: with no allowance the file is byte for byte today's derived file.
if none, today := render(), AsNftablesWithLocal(derived, []string{"10.10.0.1"}, false, nil, []string{"eth0"}, "mesh0", nil); none != today {
t.Errorf("no allowance changed the file:\n%s\nwant\n%s", none, today)
}
// 08 control-verbs §8: one of the default set, a negated match on the private network's interface and no positive one.
one := render(nodepacketfilter.Allowance{Key: "allow.tcp.8000", Port: 8000, Protocol: "tcp", At: "2026-11-21T19:00:00Z"})
if want := "\t\tiifname != \"mesh0\" tcp dport 8000 accept comment \"allowance allow.tcp.8000 2026-11-21T19:00:00Z\"\n"; !strings.Contains(inputOf(one), want) {
t.Errorf("the default set's allowance is not rendered as %q:\n%s", want, one)
}
if strings.Contains(one, "iifname \"mesh0\" tcp dport 8000") || strings.Contains(one, "proto-dst 8000") {
t.Errorf("the allowance opens the tunnel or the forward path:\n%s", one)
}
// 08 control-verbs §8: three, one interface named, one with an IPv4 source, one udp, after the derived ones.
three := inputOf(render(
nodepacketfilter.Allowance{Key: "allow.udp.5353", Port: 5353, Protocol: "udp", At: "a3"},
nodepacketfilter.Allowance{Key: "allow.tcp.8000.enp3s0", Port: 8000, Protocol: "tcp", Interface: "enp3s0", At: "a1"},
nodepacketfilter.Allowance{Key: "allow.tcp.9000", Port: 9000, Protocol: "tcp", Source: "192.168.1.0/24", At: "a2"},
))
derivedAt := strings.Index(three, "tcp dport 443 accept")
last := derivedAt
for _, want := range []string{
"\t\tiifname \"enp3s0\" tcp dport 8000 accept comment \"allowance allow.tcp.8000.enp3s0 a1\"\n",
"\t\tiifname != \"mesh0\" ip saddr 192.168.1.0/24 tcp dport 9000 accept comment \"allowance allow.tcp.9000 a2\"\n",
"\t\tiifname != \"mesh0\" udp dport 5353 accept comment \"allowance allow.udp.5353 a3\"\n",
} {
at := strings.Index(three, want)
if at < 0 || at < last {
t.Errorf("the input chain lacks %q after the derived rules and the one before it:\n%s", want, three)
}
last = at
}
// 08 control-verbs §2, §8: an allowance with an until matches its port as the one element of a timeout set, its timeout
// the time left; one past its until is rendered as answered, at least 1 s.
until := render(
nodepacketfilter.Allowance{Key: "allow.tcp.8000", Port: 8000, Protocol: "tcp", At: "a1", Until: "2026-11-21T21:00:00Z"},
nodepacketfilter.Allowance{Key: "allow.tcp.8001", Port: 8001, Protocol: "tcp", At: "a2", Until: "2026-11-21T19:00:00Z"},
nodepacketfilter.Allowance{Key: "allow.tcp.8002", Port: 8002, Protocol: "tcp", Source: "2001:db8::/32", At: "a3"},
)
for _, want := range []string{
"\tset allowance_1 {\n\t\ttype inet_service\n\t\tflags timeout\n\t\telements = { 8000 timeout 3600s }\n\t}\n",
"\tset allowance_2 {\n\t\ttype inet_service\n\t\tflags timeout\n\t\telements = { 8001 timeout 1s }\n\t}\n",
"\t\tiifname != \"mesh0\" tcp dport @allowance_1 accept comment \"allowance allow.tcp.8000 a1\"\n",
"\t\tiifname != \"mesh0\" tcp dport @allowance_2 accept comment \"allowance allow.tcp.8001 a2\"\n",
"\t\tiifname != \"mesh0\" ip6 saddr 2001:db8::/32 tcp dport 8002 accept comment \"allowance allow.tcp.8002 a3\"\n",
} {
if !strings.Contains(until, want) {
t.Errorf("the rule set lacks %q:\n%s", want, until)
}
}
})
}
// The host does not sort, so the order the mesh writes is the order a machine applies.