From f1f4c2b62934f2829d0580f27d4a6c946a2e24a6 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Thu, 29 Oct 2026 21:17:14 +0100 Subject: [PATCH] =?UTF-8?q?f-88:=20nft:=20traduction=201:1=20sans=20=C3=A9?= =?UTF-8?q?tat=20de=20l'IP=20flottante,=20table=20ip=20two=20#88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GnomeZworc --- internal/nft/fip.go | 58 ++++++++++++++++++++++++++++++++++++++++ internal/nft/fip_test.go | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 internal/nft/fip.go create mode 100644 internal/nft/fip_test.go diff --git a/internal/nft/fip.go b/internal/nft/fip.go new file mode 100644 index 0000000..33e0859 --- /dev/null +++ b/internal/nft/fip.go @@ -0,0 +1,58 @@ +package nft + +import ( + "fmt" + "strings" +) + +type FloatingIP struct { + Iface string + Public string + Private string +} + +var fipChains = []natChain{ + {"pre-", "filter", "prerouting", "raw"}, + {"post-", "filter", "postrouting", "srcnat"}, +} + +func (f FloatingIP) rule(c natChain) string { + if c.hook == "prerouting" { + return fmt.Sprintf("iifname %q ip daddr %s ip daddr set %s", f.Iface, f.Public, f.Private) + } + return fmt.Sprintf("oifname %q ip saddr %s ip saddr set %s", f.Iface, f.Private, f.Public) +} + +func fipSetScript(f FloatingIP) string { + var b strings.Builder + fmt.Fprintf(&b, "add table ip %s\n", Table) + for _, c := range fipChains { + name := c.declare(&b, f.Iface) + fmt.Fprintf(&b, "add rule ip %s %s %s\n", Table, name, f.rule(c)) + } + return b.String() +} + +func fipDeleteScript(iface string) string { + var b strings.Builder + fmt.Fprintf(&b, "add table ip %s\n", Table) + for _, c := range fipChains { + name := c.declare(&b, iface) + fmt.Fprintf(&b, "delete chain ip %s %s\n", Table, name) + } + return b.String() +} + +func SetFloatingIP(f FloatingIP) error { + if err := apply(fipSetScript(f)); err != nil { + return fmt.Errorf("set floating ip %s: %w", f.Iface, err) + } + return nil +} + +func DeleteFloatingIP(iface string) error { + if err := apply(fipDeleteScript(iface)); err != nil { + return fmt.Errorf("delete floating ip %s: %w", iface, err) + } + return nil +} diff --git a/internal/nft/fip_test.go b/internal/nft/fip_test.go new file mode 100644 index 0000000..1dee79a --- /dev/null +++ b/internal/nft/fip_test.go @@ -0,0 +1,53 @@ +package nft + +import ( + "strings" + "testing" +) + +func labFloatingIP() FloatingIP { + return FloatingIP{Iface: "fip-f000002f", Public: "240.0.0.47", Private: "10.46.1.10"} +} + +func TestFloatingIPSetScript(t *testing.T) { + want := `add table ip two +add chain ip two pre-fip-f000002f { type filter hook prerouting priority raw; policy accept; } +flush chain ip two pre-fip-f000002f +add rule ip two pre-fip-f000002f iifname "fip-f000002f" ip daddr 240.0.0.47 ip daddr set 10.46.1.10 +add chain ip two post-fip-f000002f { type filter hook postrouting priority srcnat; policy accept; } +flush chain ip two post-fip-f000002f +add rule ip two post-fip-f000002f oifname "fip-f000002f" ip saddr 10.46.1.10 ip saddr set 240.0.0.47 +` + if got := fipSetScript(labFloatingIP()); got != want { + t.Errorf("script :\n%s\nattendu :\n%s", got, want) + } +} + +func TestFloatingIPDeleteScript(t *testing.T) { + want := `add table ip two +add chain ip two pre-fip-f000002f { type filter hook prerouting priority raw; policy accept; } +flush chain ip two pre-fip-f000002f +delete chain ip two pre-fip-f000002f +add chain ip two post-fip-f000002f { type filter hook postrouting priority srcnat; policy accept; } +flush chain ip two post-fip-f000002f +delete chain ip two post-fip-f000002f +` + if got := fipDeleteScript("fip-f000002f"); got != want { + t.Errorf("script :\n%s\nattendu :\n%s", got, want) + } +} + +func TestFloatingIPScriptsDoNotTrackNorTouchOtherInterfaces(t *testing.T) { + script := fipSetScript(labFloatingIP()) + fipDeleteScript("fip-f000002f") + for _, line := range strings.Split(strings.TrimSpace(script), "\n") { + if !strings.Contains(line, " ip two") { + t.Errorf("ligne hors de la table ip two : %q", line) + } + if strings.Contains(line, "notrack") { + t.Errorf("notrack présent : %q", line) + } + if strings.HasPrefix(line, "add rule") && !strings.Contains(line, `ifname "fip-f000002f"`) { + t.Errorf("règle qui ne vise pas fip-f000002f : %q", line) + } + } +}