f-57: subnet: gateway anycast sur macvlan, adresse d'émission sur le bridge, MAC anycast confinée au netns #57

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-13 14:42:01 +02:00
commit 6c35ebdff4
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
7 changed files with 239 additions and 4 deletions

View file

@ -58,3 +58,34 @@ func DeleteDHCP(iface, ip string) error {
"--ip-source", ip,
"-j", "DROP")
}
func confineRules(iface, mac string) [][]string {
return [][]string{
{"-t", "nat", "PREROUTING", "--in-interface", iface, "-s", mac, "-j", "DROP"},
{"-t", "filter", "FORWARD", "--out-interface", iface, "-s", mac, "-j", "DROP"},
{"-t", "filter", "OUTPUT", "--out-interface", iface, "-s", mac, "-j", "DROP"},
}
}
func withAction(action string, rule []string) []string {
return append([]string{rule[0], rule[1], action}, rule[2:]...)
}
func ConfineMAC(iface, mac string) error {
for _, rule := range confineRules(iface, mac) {
if err := exec.Command("ebtables", withAction("-A", rule)...).Run(); err != nil {
return fmt.Errorf("ebtables confine %s on %s (%s %s): %w", mac, iface, rule[1], rule[2], err)
}
}
return nil
}
func DeleteConfineMAC(iface, mac string) error {
var first error
for _, rule := range confineRules(iface, mac) {
if err := exec.Command("ebtables", withAction("-D", rule)...).Run(); err != nil && first == nil {
first = fmt.Errorf("ebtables unconfine %s on %s (%s %s): %w", mac, iface, rule[1], rule[2], err)
}
}
return first
}

View file

@ -0,0 +1,43 @@
package ebtables
import (
"strings"
"testing"
)
func TestConfineRules(t *testing.T) {
want := []string{
"-t nat -A PREROUTING --in-interface v-s9r1-i -s 02:01:00:00:0b:5f -j DROP",
"-t filter -A FORWARD --out-interface v-s9r1-i -s 02:01:00:00:0b:5f -j DROP",
"-t filter -A OUTPUT --out-interface v-s9r1-i -s 02:01:00:00:0b:5f -j DROP",
}
rules := confineRules("v-s9r1-i", "02:01:00:00:0b:5f")
if len(rules) != len(want) {
t.Fatalf("%d règles, %d attendues", len(rules), len(want))
}
for i, rule := range rules {
if got := strings.Join(withAction("-A", rule), " "); got != want[i] {
t.Errorf("règle %d : %q, %q attendu", i, got, want[i])
}
}
}
func TestDeleteUsesTheSameRules(t *testing.T) {
for _, rule := range confineRules("v-x-i", "02:01:00:00:00:01") {
added := withAction("-A", rule)
deleted := withAction("-D", rule)
if strings.Replace(strings.Join(added, " "), " -A ", " -D ", 1) != strings.Join(deleted, " ") {
t.Errorf("ajout %v et suppression %v divergent", added, deleted)
}
}
}
func TestWithActionDoesNotAlterTheRule(t *testing.T) {
rule := confineRules("v-x-i", "02:01:00:00:00:01")[0]
before := strings.Join(rule, " ")
withAction("-A", rule)
withAction("-D", rule)
if strings.Join(rule, " ") != before {
t.Errorf("la règle a été modifiée : %q devenu %q", before, strings.Join(rule, " "))
}
}