f-42: nft: NAT et filtrage entrant de la passerelle, table ip two #42

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-29 19:58:22 +01:00
commit 00617d6e0c
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
2 changed files with 141 additions and 0 deletions

77
internal/nft/nat.go Normal file
View file

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

64
internal/nft/nat_test.go Normal file
View file

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