f-88: nft: traduction 1:1 sans état de l'IP flottante, table ip two #88

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-29 21:17:14 +01:00
commit 4effb5028a
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
2 changed files with 111 additions and 0 deletions

58
internal/nft/fip.go Normal file
View file

@ -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
}

53
internal/nft/fip_test.go Normal file
View file

@ -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)
}
}
}