From d51359d4f235b269d3bb9ad634073cb34e954646 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Thu, 29 Oct 2026 21:14:39 +0100 Subject: [PATCH] =?UTF-8?q?f-88:=20netif:=20MAC=20d'IP=20flottante,=20r?= =?UTF-8?q?=C3=A8gles=20de=20routage=20par=20source=20#88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GnomeZworc --- internal/netif/mac.go | 5 +++++ internal/netif/mac_test.go | 31 ++++++++++++++++++++++++++++ internal/netif/rule_linux.go | 34 +++++++++++++++++++++++++++++++ internal/netif/rule_linux_test.go | 24 ++++++++++++++++++++++ internal/netif/rule_other.go | 16 +++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 internal/netif/rule_linux.go create mode 100644 internal/netif/rule_linux_test.go create mode 100644 internal/netif/rule_other.go diff --git a/internal/netif/mac.go b/internal/netif/mac.go index 7dd4559..7fe73c0 100644 --- a/internal/netif/mac.go +++ b/internal/netif/mac.go @@ -12,6 +12,7 @@ const ( RoleRouter byte = 0x02 RoleHostGateway byte = 0x03 RoleNATGateway byte = 0x04 + RoleFloatingIP byte = 0x05 MaxVNI = 1<<24 - 1 ) @@ -28,6 +29,10 @@ func AnycastMAC(vni int) (net.HardwareAddr, error) { return vniMAC(RoleAnycastGateway, vni) } +func FloatingIPMAC(public net.IP) (net.HardwareAddr, error) { + return HostMAC(RoleFloatingIP, public) +} + func NATGatewayMAC(l3vni int) (net.HardwareAddr, error) { return vniMAC(RoleNATGateway, l3vni) } diff --git a/internal/netif/mac_test.go b/internal/netif/mac_test.go index b302bff..88fab93 100644 --- a/internal/netif/mac_test.go +++ b/internal/netif/mac_test.go @@ -141,3 +141,34 @@ func TestNATGatewayMACDiffersFromOtherRoles(t *testing.T) { } } } + +func TestFloatingIPMAC(t *testing.T) { + cases := map[string]string{ + "203.0.113.20": "02:05:cb:00:71:14", + "240.0.0.47": "02:05:f0:00:00:2f", + } + for ip, want := range cases { + got, err := FloatingIPMAC(net.ParseIP(ip)) + if err != nil { + t.Fatalf("%s : erreur inattendue %v", ip, err) + } + if got.String() != want { + t.Errorf("%s : %s, %s attendu", ip, got, want) + } + } + if mac, err := FloatingIPMAC(net.ParseIP("2001:db8::20")); err == nil { + t.Errorf("IPv6 acceptée : %s", mac) + } +} + +func TestFloatingIPMACDiffersFromOtherRoles(t *testing.T) { + fip, _ := FloatingIPMAC(net.ParseIP("192.168.14.11")) + for _, role := range []byte{RoleAnycastGateway, RoleRouter, RoleHostGateway, RoleNATGateway} { + if fip[1] == role { + t.Errorf("rôle %#x : même octet de rôle que l'IP flottante (%s)", role, fip) + } + } + if fip[1] != 0x05 { + t.Errorf("%s : octet de rôle 0x05 attendu (valeur publiée dans la doc)", fip) + } +} diff --git a/internal/netif/rule_linux.go b/internal/netif/rule_linux.go new file mode 100644 index 0000000..fcc2538 --- /dev/null +++ b/internal/netif/rule_linux.go @@ -0,0 +1,34 @@ +//go:build linux + +package netif + +import ( + "errors" + "net" + "syscall" + + "github.com/vishvananda/netlink" +) + +func sourceRule(src net.IP, table, priority int, suppressDefault bool) *netlink.Rule { + r := netlink.NewRule() + r.Family = netlink.FAMILY_V4 + r.Src = &net.IPNet{IP: src.To4(), Mask: net.CIDRMask(32, 32)} + r.Table = table + r.Priority = priority + if suppressDefault { + r.SuppressPrefixlen = 0 + } + return r +} + +func RuleAddFrom(src net.IP, table, priority int, suppressDefault bool) error { + return netlink.RuleAdd(sourceRule(src, table, priority, suppressDefault)) +} + +func RuleDelFromIfExists(src net.IP, table, priority int, suppressDefault bool) error { + if err := netlink.RuleDel(sourceRule(src, table, priority, suppressDefault)); err != nil && !errors.Is(err, syscall.ENOENT) { + return err + } + return nil +} diff --git a/internal/netif/rule_linux_test.go b/internal/netif/rule_linux_test.go new file mode 100644 index 0000000..846c247 --- /dev/null +++ b/internal/netif/rule_linux_test.go @@ -0,0 +1,24 @@ +//go:build linux + +package netif + +import ( + "net" + "testing" + + "github.com/vishvananda/netlink" +) + +func TestSourceRule(t *testing.T) { + r := sourceRule(net.ParseIP("10.46.1.10"), 170786058, 101, false) + if r.Family != netlink.FAMILY_V4 || r.Src.String() != "10.46.1.10/32" || r.Table != 170786058 || r.Priority != 101 { + t.Errorf("règle %+v", r) + } + if r.SuppressPrefixlen != -1 { + t.Errorf("suppress_prefixlength %d sans suppression demandée, -1 attendu", r.SuppressPrefixlen) + } + main := sourceRule(net.ParseIP("10.46.1.10"), 254, 100, true) + if main.SuppressPrefixlen != 0 || main.Table != 254 { + t.Errorf("règle de la table main %+v", main) + } +} diff --git a/internal/netif/rule_other.go b/internal/netif/rule_other.go new file mode 100644 index 0000000..f3bb34b --- /dev/null +++ b/internal/netif/rule_other.go @@ -0,0 +1,16 @@ +//go:build !linux + +package netif + +import ( + "errors" + "net" +) + +func RuleAddFrom(_ net.IP, _, _ int, _ bool) error { + return errors.New("not supported on this platform") +} + +func RuleDelFromIfExists(_ net.IP, _, _ int, _ bool) error { + return errors.New("not supported on this platform") +}