f-76: nft: règles de pont en nftables, ebtables retiré #76

internal/nft pose les règles d'un port en une transaction, dans une table
bridge two par netns de VPC. Subnets, gateway anycast et watchdog l'utilisent,
même filtrage qu'avec ebtables. bootstrap_kvm.sh et le lab installent
nftables ; helpers e2e, doc et notes 0.3.0 suivent.

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-25 22:39:36 +01:00
commit d98e42e0a8
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
27 changed files with 823 additions and 341 deletions

353
internal/nft/nft.go Normal file
View file

@ -0,0 +1,353 @@
package nft
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"strings"
)
const Table = "two"
type hook struct {
name string
chain string
prefix string
ports string
key string
priority string
}
var hooks = []hook{
{"prerouting", "prerouting", "pre-", "pre_ports", "iifname", "dstnat"},
{"forward", "forward", "fwd-", "fwd_ports", "oifname", "filter"},
{"output", "output", "out-", "out_ports", "oifname", "filter"},
}
type Port struct {
Name string
Prerouting []string
Forward []string
Output []string
}
func (p Port) rules(h hook) []string {
switch h.name {
case "prerouting":
return p.Prerouting
case "forward":
return p.Forward
default:
return p.Output
}
}
func DropSourceMAC(mac string) string {
return "ether saddr " + mac + " drop"
}
func DropARPRequestTo(ip string) string {
return "arp operation request arp daddr ip " + ip + " drop"
}
func DropARPFrom(ip string) string {
return "arp saddr ip " + ip + " drop"
}
func DropDHCPFrom(ip string) string {
return "ip saddr " + ip + " udp sport 67-68 udp dport 67-68 drop"
}
func run(args []string, script string) ([]byte, error) {
cmd := exec.Command("nft", args...)
if script != "" {
cmd.Stdin = strings.NewReader(script)
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("nft %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(stderr.String()))
}
return out, nil
}
func apply(script string) error {
_, err := run([]string{"-f", "-"}, script)
return err
}
func baseScript() string {
var b strings.Builder
fmt.Fprintf(&b, "add table bridge %s\n", Table)
for _, h := range hooks {
fmt.Fprintf(&b, "add map bridge %s %s { type ifname : verdict; }\n", Table, h.ports)
}
for _, h := range hooks {
fmt.Fprintf(&b, "add chain bridge %s %s { type filter hook %s priority %s; policy accept; }\n", Table, h.chain, h.name, h.priority)
}
return b.String()
}
func setScript(p Port, base table) string {
var b strings.Builder
b.WriteString(baseScript())
for _, h := range hooks {
if !base.hasDispatch(h) {
fmt.Fprintf(&b, "add rule bridge %s %s %s vmap @%s\n", Table, h.chain, h.key, h.ports)
}
}
for _, h := range hooks {
rules := p.rules(h)
chain := h.prefix + p.Name
if len(rules) == 0 {
if _, ok := base.elements[h.ports][p.Name]; ok {
fmt.Fprintf(&b, "delete element bridge %s %s { %q }\n", Table, h.ports, p.Name)
}
if base.chains[chain] {
fmt.Fprintf(&b, "delete chain bridge %s %s\n", Table, chain)
}
continue
}
fmt.Fprintf(&b, "add chain bridge %s %s\n", Table, chain)
fmt.Fprintf(&b, "flush chain bridge %s %s\n", Table, chain)
for _, r := range rules {
fmt.Fprintf(&b, "add rule bridge %s %s %s\n", Table, chain, r)
}
target, ok := base.elements[h.ports][p.Name]
if ok && target != chain {
fmt.Fprintf(&b, "delete element bridge %s %s { %q }\n", Table, h.ports, p.Name)
}
if target != chain {
fmt.Fprintf(&b, "add element bridge %s %s { %q : jump %s }\n", Table, h.ports, p.Name, chain)
}
}
return b.String()
}
func deleteScript(name string, t table) string {
var b strings.Builder
for _, h := range hooks {
if _, ok := t.elements[h.ports][name]; ok {
fmt.Fprintf(&b, "delete element bridge %s %s { %q }\n", Table, h.ports, name)
}
}
for _, h := range hooks {
if chain := h.prefix + name; t.chains[chain] {
fmt.Fprintf(&b, "delete chain bridge %s %s\n", Table, chain)
}
}
return b.String()
}
func SetPort(p Port) error {
t, err := readTable()
if err != nil {
return err
}
if err := apply(setScript(p, t)); err != nil {
return fmt.Errorf("set port %s: %w", p.Name, err)
}
return nil
}
func DeletePort(name string) error {
t, err := readTable()
if err != nil {
return err
}
script := deleteScript(name, t)
if script == "" {
return nil
}
if err := apply(script); err != nil {
return fmt.Errorf("delete port %s: %w", name, err)
}
return nil
}
func MissingRules(p Port) ([]string, error) {
t, err := readTable()
if err != nil {
return nil, err
}
listings := map[string][]string{}
for _, h := range hooks {
chain := h.prefix + p.Name
if len(p.rules(h)) == 0 || !t.chains[chain] {
continue
}
out, err := run([]string{"list", "chain", "bridge", Table, chain}, "")
if err != nil {
return nil, err
}
listings[chain] = chainRules(string(out))
}
return missing(p, t, listings), nil
}
func missing(p Port, t table, listings map[string][]string) []string {
var out []string
for _, h := range hooks {
rules := p.rules(h)
if len(rules) == 0 {
continue
}
chain := h.prefix + p.Name
if !t.hasDispatch(h) {
out = append(out, fmt.Sprintf("%s: %s vmap @%s", h.chain, h.key, h.ports))
}
if t.elements[h.ports][p.Name] != chain {
out = append(out, fmt.Sprintf("%s: %q : jump %s", h.ports, p.Name, chain))
}
want := map[string]bool{}
for _, r := range rules {
want[r] = true
}
have := map[string]bool{}
for _, r := range listings[chain] {
have[r] = true
if !want[r] {
out = append(out, chain+": unexpected "+r)
}
}
for _, r := range rules {
if !have[r] {
out = append(out, chain+": "+r)
}
}
}
return out
}
func chainRules(listing string) []string {
var rules []string
depth := 0
for _, line := range strings.Split(listing, "\n") {
line = strings.TrimSpace(line)
switch {
case strings.HasSuffix(line, "{"):
depth++
case line == "}":
depth--
case depth == 2 && line != "":
rules = append(rules, line)
}
}
return rules
}
type table struct {
exists bool
chains map[string]bool
elements map[string]map[string]string
dispatch map[string]string
}
func (t table) hasDispatch(h hook) bool {
return t.dispatch[h.chain] == h.key+" @"+h.ports
}
type listing struct {
Nftables []struct {
Table *struct {
Family string `json:"family"`
Name string `json:"name"`
} `json:"table"`
Chain *struct {
Name string `json:"name"`
} `json:"chain"`
Map *struct {
Name string `json:"name"`
Elem []json.RawMessage `json:"elem"`
} `json:"map"`
Rule *struct {
Chain string `json:"chain"`
Expr []struct {
Vmap *struct {
Key struct {
Meta struct {
Key string `json:"key"`
} `json:"meta"`
} `json:"key"`
Data string `json:"data"`
} `json:"vmap"`
} `json:"expr"`
} `json:"rule"`
} `json:"nftables"`
}
func readTable() (table, error) {
out, err := run([]string{"-j", "list", "tables", "bridge"}, "")
if err != nil {
return table{}, err
}
tables, err := parseListing(out)
if err != nil || !tables.exists {
return tables, err
}
out, err = run([]string{"-j", "list", "table", "bridge", Table}, "")
if err != nil {
return table{}, err
}
return parseListing(out)
}
func parseListing(data []byte) (table, error) {
var l listing
if err := json.Unmarshal(data, &l); err != nil {
return table{}, fmt.Errorf("nft json: %w", err)
}
t := table{chains: map[string]bool{}, elements: map[string]map[string]string{}, dispatch: map[string]string{}}
for _, o := range l.Nftables {
switch {
case o.Table != nil:
if o.Table.Family == "bridge" && o.Table.Name == Table {
t.exists = true
}
case o.Chain != nil:
t.chains[o.Chain.Name] = true
case o.Map != nil:
elems := map[string]string{}
for _, raw := range o.Map.Elem {
name, target, err := parseElem(raw)
if err != nil {
return table{}, fmt.Errorf("nft json: map %s: %w", o.Map.Name, err)
}
elems[name] = target
}
t.elements[o.Map.Name] = elems
case o.Rule != nil:
for _, e := range o.Rule.Expr {
if e.Vmap != nil {
t.dispatch[o.Rule.Chain] = e.Vmap.Key.Meta.Key + " " + e.Vmap.Data
}
}
}
}
return t, nil
}
func parseElem(raw json.RawMessage) (string, string, error) {
var pair []json.RawMessage
if err := json.Unmarshal(raw, &pair); err != nil || len(pair) != 2 {
return "", "", fmt.Errorf("element %s: not a key and verdict pair", raw)
}
var name string
if err := json.Unmarshal(pair[0], &name); err != nil {
return "", "", fmt.Errorf("element %s: key: %w", raw, err)
}
var verdict struct {
Jump *struct {
Target string `json:"target"`
} `json:"jump"`
}
if err := json.Unmarshal(pair[1], &verdict); err != nil {
return "", "", fmt.Errorf("element %s: verdict: %w", raw, err)
}
if verdict.Jump == nil {
return name, "", nil
}
return name, verdict.Jump.Target, nil
}

217
internal/nft/nft_test.go Normal file
View file

@ -0,0 +1,217 @@
package nft
import (
"strings"
"testing"
)
const listedTable = `{"nftables": [{"metainfo": {"version": "1.0.6", "release_name": "Lester Gooch #5", "json_schema_version": 1}}, {"table": {"family": "bridge", "name": "two", "handle": 1}}, {"map": {"family": "bridge", "name": "pre_ports", "table": "two", "type": "ifname", "handle": 1, "map": "verdict"}}, {"map": {"family": "bridge", "name": "fwd_ports", "table": "two", "type": "ifname", "handle": 2, "map": "verdict", "elem": [["v-t33a-i", {"jump": {"target": "fwd-v-t33a-i"}}], ["v-other-i", {"jump": {"target": "fwd-v-other-i"}}]]}}, {"map": {"family": "bridge", "name": "out_ports", "table": "two", "type": "ifname", "handle": 3, "map": "verdict", "elem": [["v-t33a-i", {"jump": {"target": "out-v-t33a-i"}}]]}}, {"chain": {"family": "bridge", "table": "two", "name": "prerouting", "handle": 4, "type": "filter", "hook": "prerouting", "prio": -300, "policy": "accept"}}, {"chain": {"family": "bridge", "table": "two", "name": "forward", "handle": 5, "type": "filter", "hook": "forward", "prio": -200, "policy": "accept"}}, {"chain": {"family": "bridge", "table": "two", "name": "output", "handle": 6, "type": "filter", "hook": "output", "prio": -200, "policy": "accept"}}, {"chain": {"family": "bridge", "table": "two", "name": "fwd-v-t33a-i", "handle": 10}}, {"chain": {"family": "bridge", "table": "two", "name": "out-v-t33a-i", "handle": 11}}, {"chain": {"family": "bridge", "table": "two", "name": "fwd-v-other-i", "handle": 12}}, {"rule": {"family": "bridge", "table": "two", "chain": "prerouting", "handle": 7, "expr": [{"vmap": {"key": {"meta": {"key": "iifname"}}, "data": "@pre_ports"}}]}}, {"rule": {"family": "bridge", "table": "two", "chain": "forward", "handle": 8, "expr": [{"vmap": {"key": {"meta": {"key": "oifname"}}, "data": "@fwd_ports"}}]}}, {"rule": {"family": "bridge", "table": "two", "chain": "output", "handle": 9, "expr": [{"vmap": {"key": {"meta": {"key": "oifname"}}, "data": "@out_ports"}}]}}]}`
const noTable = `{"nftables": [{"metainfo": {"version": "1.0.6", "release_name": "Lester Gooch #5", "json_schema_version": 1}}]}`
const listedChain = `table bridge two {
chain fwd-v-t33a-i {
arp operation request arp daddr ip 10.33.0.1 drop
ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop
}
}
`
func bridgePort() Port {
return Port{
Name: "v-t33a-i",
Forward: []string{"arp operation request arp daddr ip 10.33.0.1 drop", "ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop"},
Output: []string{"arp saddr ip 10.33.0.1 drop", "ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop"},
}
}
func mustParse(t *testing.T, data string) table {
t.Helper()
tb, err := parseListing([]byte(data))
if err != nil {
t.Fatalf("parseListing: %v", err)
}
return tb
}
func TestRuleTexts(t *testing.T) {
for got, want := range map[string]string{
DropSourceMAC("02:01:00:00:0b:5f"): "ether saddr 02:01:00:00:0b:5f drop",
DropARPRequestTo("10.33.0.1"): "arp operation request arp daddr ip 10.33.0.1 drop",
DropARPFrom("10.33.0.1"): "arp saddr ip 10.33.0.1 drop",
DropDHCPFrom("10.33.0.1"): "ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop",
} {
if got != want {
t.Errorf("%q, %q attendu", got, want)
}
}
}
func TestSetScriptOnAnEmptyNetns(t *testing.T) {
want := `add table bridge two
add map bridge two pre_ports { type ifname : verdict; }
add map bridge two fwd_ports { type ifname : verdict; }
add map bridge two out_ports { type ifname : verdict; }
add chain bridge two prerouting { type filter hook prerouting priority dstnat; policy accept; }
add chain bridge two forward { type filter hook forward priority filter; policy accept; }
add chain bridge two output { type filter hook output priority filter; policy accept; }
add rule bridge two prerouting iifname vmap @pre_ports
add rule bridge two forward oifname vmap @fwd_ports
add rule bridge two output oifname vmap @out_ports
add chain bridge two fwd-v-t33a-i
flush chain bridge two fwd-v-t33a-i
add rule bridge two fwd-v-t33a-i arp operation request arp daddr ip 10.33.0.1 drop
add rule bridge two fwd-v-t33a-i ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop
add element bridge two fwd_ports { "v-t33a-i" : jump fwd-v-t33a-i }
add chain bridge two out-v-t33a-i
flush chain bridge two out-v-t33a-i
add rule bridge two out-v-t33a-i arp saddr ip 10.33.0.1 drop
add rule bridge two out-v-t33a-i ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop
add element bridge two out_ports { "v-t33a-i" : jump out-v-t33a-i }
`
if got := setScript(bridgePort(), mustParse(t, noTable)); got != want {
t.Errorf("script :\n%s\nattendu :\n%s", got, want)
}
}
func TestSetScriptAgainKeepsDispatchAndElements(t *testing.T) {
got := setScript(bridgePort(), mustParse(t, listedTable))
for _, absent := range []string{"vmap", "add element", "delete"} {
if strings.Contains(got, absent) {
t.Errorf("%q présent alors que la table est déjà en place :\n%s", absent, got)
}
}
if !strings.Contains(got, "flush chain bridge two fwd-v-t33a-i\n") {
t.Errorf("la chaîne n'est pas vidée avant d'être réécrite :\n%s", got)
}
}
func TestSetScriptReplacesAnElementPointingElsewhere(t *testing.T) {
data := strings.Replace(listedTable, `["v-t33a-i", {"jump": {"target": "fwd-v-t33a-i"}}]`, `["v-t33a-i", {"jump": {"target": "fwd-v-other-i"}}]`, 1)
got := setScript(bridgePort(), mustParse(t, data))
want := "delete element bridge two fwd_ports { \"v-t33a-i\" }\nadd element bridge two fwd_ports { \"v-t33a-i\" : jump fwd-v-t33a-i }\n"
if !strings.Contains(got, want) {
t.Errorf("élément non remplacé :\n%s", got)
}
}
func TestSetScriptRemovesAHookThePortNoLongerUses(t *testing.T) {
p := bridgePort()
p.Output = nil
got := setScript(p, mustParse(t, listedTable))
want := "delete element bridge two out_ports { \"v-t33a-i\" }\ndelete chain bridge two out-v-t33a-i\n"
if !strings.Contains(got, want) {
t.Errorf("crochet output non retiré :\n%s", got)
}
if strings.Contains(got, "pre-v-t33a-i") {
t.Errorf("chaîne prerouting touchée alors qu'elle n'existe pas :\n%s", got)
}
}
func TestDeleteScriptRemovesElementsBeforeChains(t *testing.T) {
want := `delete element bridge two fwd_ports { "v-t33a-i" }
delete element bridge two out_ports { "v-t33a-i" }
delete chain bridge two fwd-v-t33a-i
delete chain bridge two out-v-t33a-i
`
if got := deleteScript("v-t33a-i", mustParse(t, listedTable)); got != want {
t.Errorf("script :\n%s\nattendu :\n%s", got, want)
}
}
func TestDeleteScriptLeavesOtherPorts(t *testing.T) {
got := deleteScript("v-t33a-i", mustParse(t, listedTable))
if strings.Contains(got, "v-other-i") {
t.Errorf("un autre port est touché :\n%s", got)
}
}
func TestDeleteScriptIsEmptyForAnAbsentPort(t *testing.T) {
for name, data := range map[string]string{"sans table": noTable, "port inconnu": listedTable} {
if got := deleteScript("v-gone-i", mustParse(t, data)); got != "" {
t.Errorf("%s : script %q, vide attendu", name, got)
}
}
}
func TestParseListing(t *testing.T) {
tb := mustParse(t, listedTable)
if !tb.exists {
t.Errorf("table two non vue")
}
if got := tb.elements["fwd_ports"]["v-t33a-i"]; got != "fwd-v-t33a-i" {
t.Errorf("fwd_ports[v-t33a-i] = %q", got)
}
if got := len(tb.elements["pre_ports"]); got != 0 {
t.Errorf("pre_ports : %d éléments, 0 attendu", got)
}
for _, c := range []string{"prerouting", "forward", "output", "fwd-v-t33a-i", "out-v-t33a-i", "fwd-v-other-i"} {
if !tb.chains[c] {
t.Errorf("chaîne %s non vue", c)
}
}
for _, h := range hooks {
if !tb.hasDispatch(h) {
t.Errorf("aiguillage de %s non vu : %q", h.chain, tb.dispatch[h.chain])
}
}
if mustParse(t, noTable).exists {
t.Errorf("table vue alors qu'absente")
}
}
func TestParseListingRejectsAMalformedElement(t *testing.T) {
data := strings.Replace(listedTable, `["v-other-i", {"jump": {"target": "fwd-v-other-i"}}]`, `"v-other-i"`, 1)
if _, err := parseListing([]byte(data)); err == nil || !strings.Contains(err.Error(), "map fwd_ports") {
t.Errorf("erreur = %v, erreur sur fwd_ports attendue", err)
}
}
func TestChainRules(t *testing.T) {
got := chainRules(listedChain)
want := []string{"arp operation request arp daddr ip 10.33.0.1 drop", "ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop"}
if strings.Join(got, "|") != strings.Join(want, "|") {
t.Errorf("règles %q, %q attendues", got, want)
}
if got := chainRules("table bridge two {\n\tchain fwd-b {\n\t}\n}\n"); len(got) != 0 {
t.Errorf("chaîne vide : %q", got)
}
}
func healthyListings() map[string][]string {
return map[string][]string{
"fwd-v-t33a-i": chainRules(listedChain),
"out-v-t33a-i": {"arp saddr ip 10.33.0.1 drop", "ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop"},
}
}
func TestMissingIsEmptyWhenEverythingIsInPlace(t *testing.T) {
if got := missing(bridgePort(), mustParse(t, listedTable), healthyListings()); len(got) != 0 {
t.Errorf("manques %q, aucun attendu", got)
}
}
func TestMissingReportsEachDefect(t *testing.T) {
data := strings.Replace(listedTable, `, "elem": [["v-t33a-i", {"jump": {"target": "out-v-t33a-i"}}]]`, "", 1)
data = strings.Replace(data, `"data": "@fwd_ports"`, `"data": "@pre_ports"`, 1)
listings := healthyListings()
listings["fwd-v-t33a-i"] = []string{"arp operation request arp daddr ip 10.33.0.1 drop", "accept"}
got := strings.Join(missing(bridgePort(), mustParse(t, data), listings), "\n")
for _, want := range []string{
"forward: oifname vmap @fwd_ports",
`out_ports: "v-t33a-i" : jump out-v-t33a-i`,
"fwd-v-t33a-i: ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop",
"fwd-v-t33a-i: unexpected accept",
} {
if !strings.Contains(got, want) {
t.Errorf("%q absent de :\n%s", want, got)
}
}
}
func TestMissingReportsEverythingWithoutTable(t *testing.T) {
got := missing(bridgePort(), mustParse(t, noTable), nil)
if len(got) != 8 {
t.Errorf("%d manques, 8 attendus (2 aiguillages, 2 éléments, 4 règles) : %q", len(got), got)
}
}