From 00617d6e0c7cf91963e0c175c8d35303e6eb7f3c Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Thu, 29 Oct 2026 19:58:22 +0100 Subject: [PATCH] f-42: nft: NAT et filtrage entrant de la passerelle, table ip two #42 Signed-off-by: GnomeZworc --- internal/nft/nat.go | 77 ++++++++++++++++++++++++++++++++++++++++ internal/nft/nat_test.go | 64 +++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 internal/nft/nat.go create mode 100644 internal/nft/nat_test.go diff --git a/internal/nft/nat.go b/internal/nft/nat.go new file mode 100644 index 0000000..308b804 --- /dev/null +++ b/internal/nft/nat.go @@ -0,0 +1,77 @@ +package nft + +import ( + "fmt" + "strings" +) + +type NATGateway struct { + Iface string + IP string +} + +type natChain struct { + prefix string + kind string + hook string + priority string +} + +var natChains = []natChain{ + {"snat-", "nat", "postrouting", "srcnat"}, + {"fwd-", "filter", "forward", "filter"}, + {"in-", "filter", "input", "filter"}, +} + +func (c natChain) declare(b *strings.Builder, iface string) string { + name := c.prefix + iface + fmt.Fprintf(b, "add chain ip %s %s { type %s hook %s priority %s; policy accept; }\n", Table, name, c.kind, c.hook, c.priority) + fmt.Fprintf(b, "flush chain ip %s %s\n", Table, name) + return name +} + +func (g NATGateway) rules(c natChain) []string { + if c.kind == "nat" { + return []string{fmt.Sprintf("oifname %q snat to %s", g.Iface, g.IP)} + } + return []string{ + fmt.Sprintf("iifname %q ct state established,related accept", g.Iface), + fmt.Sprintf("iifname %q drop", g.Iface), + } +} + +func natSetScript(g NATGateway) string { + var b strings.Builder + fmt.Fprintf(&b, "add table ip %s\n", Table) + for _, c := range natChains { + name := c.declare(&b, g.Iface) + for _, r := range g.rules(c) { + fmt.Fprintf(&b, "add rule ip %s %s %s\n", Table, name, r) + } + } + return b.String() +} + +func natDeleteScript(iface string) string { + var b strings.Builder + fmt.Fprintf(&b, "add table ip %s\n", Table) + for _, c := range natChains { + name := c.declare(&b, iface) + fmt.Fprintf(&b, "delete chain ip %s %s\n", Table, name) + } + return b.String() +} + +func SetNATGateway(g NATGateway) error { + if err := apply(natSetScript(g)); err != nil { + return fmt.Errorf("set nat gateway %s: %w", g.Iface, err) + } + return nil +} + +func DeleteNATGateway(iface string) error { + if err := apply(natDeleteScript(iface)); err != nil { + return fmt.Errorf("delete nat gateway %s: %w", iface, err) + } + return nil +} diff --git a/internal/nft/nat_test.go b/internal/nft/nat_test.go new file mode 100644 index 0000000..43a3b0e --- /dev/null +++ b/internal/nft/nat_test.go @@ -0,0 +1,64 @@ +package nft + +import ( + "strings" + "testing" +) + +func TestNATSetScript(t *testing.T) { + want := `add table ip two +add chain ip two snat-ng-42 { type nat hook postrouting priority srcnat; policy accept; } +flush chain ip two snat-ng-42 +add rule ip two snat-ng-42 oifname "ng-42" snat to 198.51.100.50 +add chain ip two fwd-ng-42 { type filter hook forward priority filter; policy accept; } +flush chain ip two fwd-ng-42 +add rule ip two fwd-ng-42 iifname "ng-42" ct state established,related accept +add rule ip two fwd-ng-42 iifname "ng-42" drop +add chain ip two in-ng-42 { type filter hook input priority filter; policy accept; } +flush chain ip two in-ng-42 +add rule ip two in-ng-42 iifname "ng-42" ct state established,related accept +add rule ip two in-ng-42 iifname "ng-42" drop +` + if got := natSetScript(NATGateway{Iface: "ng-42", IP: "198.51.100.50"}); got != want { + t.Errorf("script :\n%s\nattendu :\n%s", got, want) + } +} + +func TestNATDeleteScript(t *testing.T) { + want := `add table ip two +add chain ip two snat-ng-42 { type nat hook postrouting priority srcnat; policy accept; } +flush chain ip two snat-ng-42 +delete chain ip two snat-ng-42 +add chain ip two fwd-ng-42 { type filter hook forward priority filter; policy accept; } +flush chain ip two fwd-ng-42 +delete chain ip two fwd-ng-42 +add chain ip two in-ng-42 { type filter hook input priority filter; policy accept; } +flush chain ip two in-ng-42 +delete chain ip two in-ng-42 +` + if got := natDeleteScript("ng-42"); got != want { + t.Errorf("script :\n%s\nattendu :\n%s", got, want) + } +} + +func TestNATScriptsLeaveTheBridgeTableAlone(t *testing.T) { + for label, script := range map[string]string{ + "set": natSetScript(NATGateway{Iface: "ng-42", IP: "198.51.100.50"}), + "delete": natDeleteScript("ng-42"), + } { + for _, line := range strings.Split(strings.TrimSpace(script), "\n") { + if !strings.Contains(line, " ip two") { + t.Errorf("%s : ligne hors de la table ip two : %q", label, line) + } + } + } +} + +func TestNATScriptsOnlyTouchTheirInterface(t *testing.T) { + script := natSetScript(NATGateway{Iface: "ng-42", IP: "198.51.100.50"}) + natDeleteScript("ng-42") + for _, line := range strings.Split(strings.TrimSpace(script), "\n") { + if strings.HasPrefix(line, "add rule") && !strings.Contains(line, `ifname "ng-42"`) { + t.Errorf("règle qui ne vise pas ng-42 : %q", line) + } + } +}