diff --git a/internal/frr/config.go b/internal/frr/config.go index 2ebad5e..e63df1e 100644 --- a/internal/frr/config.go +++ b/internal/frr/config.go @@ -4,9 +4,9 @@ import ( "fmt" "net" "strings" -) -const maxVNI = 1<<24 - 1 + "git.g3e.fr/syonad/two/internal/netif" +) type Base struct { ASN uint32 @@ -116,7 +116,7 @@ func (v VRF) validate() error { if err := v.validateName(); err != nil { return err } - if v.L3VNI == 0 || v.L3VNI > maxVNI { + if v.L3VNI == 0 || v.L3VNI > netif.MaxVNI { return fmt.Errorf("frr: l3vni %d does not fit in 24 bits", v.L3VNI) } if v.VTEP.To4() == nil { diff --git a/internal/l3vni/l3.go b/internal/l3vni/l3.go index 3c12707..a36f423 100644 --- a/internal/l3vni/l3.go +++ b/internal/l3vni/l3.go @@ -7,6 +7,7 @@ import ( configuration "git.g3e.fr/syonad/two/internal/config/agent" "git.g3e.fr/syonad/two/internal/frr" + "git.g3e.fr/syonad/two/internal/netif" "git.g3e.fr/syonad/two/pkg/db/kv" "github.com/dgraph-io/badger/v4" @@ -55,14 +56,6 @@ func (v l3VPC) vxlan() string { return "vxl3-" + v.id } func (v l3VPC) vethHost() string { return "l3-" + v.id + "-o" } func (v l3VPC) vethNetns() string { return "l3-" + v.id + "-i" } -func routerMAC(vtep net.IP) (net.HardwareAddr, error) { - ip := vtep.To4() - if ip == nil { - return nil, fmt.Errorf("vtep %v is not an IPv4 address", vtep) - } - return net.HardwareAddr{0x02, 0x02, ip[0], ip[1], ip[2], ip[3]}, nil -} - func bgpASN(cfg *configuration.Config) (uint32, error) { if cfg == nil || !cfg.BGP.Enabled() { return 0, fmt.Errorf("the vpc has an l3vni but the agent configuration has no bgp section") @@ -79,7 +72,7 @@ func createL3(h host, cfg *configuration.Config, v l3VPC) error { if err != nil { return fmt.Errorf("vtep: %w", err) } - rmac, err := routerMAC(vtep) + rmac, err := RouterMAC(vtep) if err != nil { return err } @@ -181,5 +174,5 @@ func LoadRouting(db *badger.DB, vpcName string) (Routing, bool, error) { } func RouterMAC(vtep net.IP) (net.HardwareAddr, error) { - return routerMAC(vtep) + return netif.HostMAC(netif.RoleRouter, vtep) } diff --git a/internal/l3vni/l3_test.go b/internal/l3vni/l3_test.go index 745d697..9838a09 100644 --- a/internal/l3vni/l3_test.go +++ b/internal/l3vni/l3_test.go @@ -220,11 +220,11 @@ func TestDeleteL3RequiresTheBGPSection(t *testing.T) { } func TestRouterMAC(t *testing.T) { - mac, err := routerMAC(net.ParseIP("192.168.14.11")) + mac, err := RouterMAC(net.ParseIP("192.168.14.11")) if err != nil || mac.String() != "02:02:c0:a8:0e:0b" { - t.Errorf("routerMAC = %v, %v", mac, err) + t.Errorf("RouterMAC = %v, %v", mac, err) } - if _, err := routerMAC(net.ParseIP("fd00::1")); err == nil { + if _, err := RouterMAC(net.ParseIP("fd00::1")); err == nil { t.Error("VTEP IPv6 accepté") } } diff --git a/internal/l3vni/l3vni.go b/internal/l3vni/l3vni.go index 211af33..60b3164 100644 --- a/internal/l3vni/l3vni.go +++ b/internal/l3vni/l3vni.go @@ -1,12 +1,14 @@ package l3vni -import "fmt" +import ( + "fmt" -const maxL3VNI = 1<<24 - 1 + "git.g3e.fr/syonad/two/internal/netif" +) func Valid(id uint32) error { - if id == 0 || id > maxL3VNI { - return fmt.Errorf("l3vni %d does not fit in 24 bits (1-%d)", id, maxL3VNI) + if id == 0 || id > netif.MaxVNI { + return fmt.Errorf("l3vni %d does not fit in 24 bits (1-%d)", id, netif.MaxVNI) } if id >= 253 && id <= 255 { return fmt.Errorf("l3vni %d is reserved: it is also the routing table id, and tables 253-255 belong to the kernel", id) diff --git a/internal/netif/mac.go b/internal/netif/mac.go new file mode 100644 index 0000000..ba8300c --- /dev/null +++ b/internal/netif/mac.go @@ -0,0 +1,21 @@ +package netif + +import ( + "fmt" + "net" +) + +const ( + locallyAdministered byte = 0x02 + + RoleRouter byte = 0x02 + RoleHostGateway byte = 0x03 +) + +func HostMAC(role byte, vtep net.IP) (net.HardwareAddr, error) { + ip := vtep.To4() + if ip == nil { + return nil, fmt.Errorf("vtep %v is not an IPv4 address", vtep) + } + return net.HardwareAddr{locallyAdministered, role, ip[0], ip[1], ip[2], ip[3]}, nil +} diff --git a/internal/netif/mac_test.go b/internal/netif/mac_test.go new file mode 100644 index 0000000..5230513 --- /dev/null +++ b/internal/netif/mac_test.go @@ -0,0 +1,60 @@ +package netif + +import ( + "net" + "testing" +) + +func TestHostMAC(t *testing.T) { + cases := []struct { + role byte + vtep string + want string + }{ + {RoleHostGateway, "192.168.14.11", "02:03:c0:a8:0e:0b"}, + {RoleHostGateway, "192.168.14.12", "02:03:c0:a8:0e:0c"}, + {RoleHostGateway, "10.0.0.1", "02:03:0a:00:00:01"}, + {RoleRouter, "192.168.14.11", "02:02:c0:a8:0e:0b"}, + } + for _, c := range cases { + got, err := HostMAC(c.role, net.ParseIP(c.vtep)) + if err != nil { + t.Fatalf("rôle %#x, VTEP %s : erreur inattendue %v", c.role, c.vtep, err) + } + if got.String() != c.want { + t.Errorf("rôle %#x, VTEP %s : %s, %s attendu", c.role, c.vtep, got, c.want) + } + } +} + +func TestHostMACIsLocalUnicast(t *testing.T) { + for _, role := range []byte{RoleRouter, RoleHostGateway} { + got, err := HostMAC(role, net.ParseIP("192.168.14.11")) + if err != nil { + t.Fatal(err) + } + if got[0]&0x02 == 0 { + t.Errorf("%s : bit « administrée localement » absent", got) + } + if got[0]&0x01 != 0 { + t.Errorf("%s : MAC de groupe (multicast)", got) + } + } +} + +func TestHostMACRolesDifferOnTheSameHost(t *testing.T) { + if RoleRouter == RoleHostGateway { + t.Fatalf("rôles identiques : %#x", RoleRouter) + } + if RoleRouter != 0x02 || RoleHostGateway != 0x03 { + t.Errorf("rôles %#x / %#x, 0x02 / 0x03 attendus (valeurs publiées dans la doc)", RoleRouter, RoleHostGateway) + } +} + +func TestHostMACRejectsANonIPv4VTEP(t *testing.T) { + for _, vtep := range []net.IP{nil, net.ParseIP("fd00::1")} { + if mac, err := HostMAC(RoleHostGateway, vtep); err == nil { + t.Errorf("VTEP %v accepté : %s", vtep, mac) + } + } +} diff --git a/internal/netif/vxlan.go b/internal/netif/vxlan.go index 81ec3a2..3455aae 100644 --- a/internal/netif/vxlan.go +++ b/internal/netif/vxlan.go @@ -8,6 +8,8 @@ import ( "github.com/vishvananda/netlink" ) +const MaxVNI = 1<<24 - 1 + const ( scopeUniverse = 0 addressSecondary = 0x01 diff --git a/internal/subnet/create.go b/internal/subnet/create.go index 1630d9f..4c7d79b 100644 --- a/internal/subnet/create.go +++ b/internal/subnet/create.go @@ -80,7 +80,7 @@ func createSubnet(db *badger.DB, subnetName string, d subnetData, backend dhcpba if err != nil { return fmt.Errorf("vtep of %s: %w", d.localIface, err) } - if gatewayMAC, err = hostGatewayMAC(vtep); err != nil { + if gatewayMAC, err = netif.HostMAC(netif.RoleHostGateway, vtep); err != nil { return err } } diff --git a/internal/subnet/gateway.go b/internal/subnet/gateway.go deleted file mode 100644 index 4d3c37a..0000000 --- a/internal/subnet/gateway.go +++ /dev/null @@ -1,19 +0,0 @@ -package subnet - -import ( - "fmt" - "net" -) - -const ( - roleHostGateway = 0x03 - locallyAdministered = 0x02 -) - -func hostGatewayMAC(vtep net.IP) (net.HardwareAddr, error) { - ip := vtep.To4() - if ip == nil { - return nil, fmt.Errorf("vtep %v is not an IPv4 address", vtep) - } - return net.HardwareAddr{locallyAdministered, roleHostGateway, ip[0], ip[1], ip[2], ip[3]}, nil -} diff --git a/internal/subnet/gateway_test.go b/internal/subnet/gateway_test.go deleted file mode 100644 index 55f7c8b..0000000 --- a/internal/subnet/gateway_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package subnet - -import ( - "net" - "testing" -) - -func TestHostGatewayMAC(t *testing.T) { - cases := map[string]string{ - "192.168.14.11": "02:03:c0:a8:0e:0b", - "192.168.14.12": "02:03:c0:a8:0e:0c", - "10.0.0.1": "02:03:0a:00:00:01", - } - for vtep, want := range cases { - got, err := hostGatewayMAC(net.ParseIP(vtep)) - if err != nil { - t.Fatalf("VTEP %s : erreur inattendue %v", vtep, err) - } - if got.String() != want { - t.Errorf("VTEP %s : %s, %s attendu", vtep, got, want) - } - } -} - -func TestHostGatewayMACIsLocalUnicast(t *testing.T) { - got, err := hostGatewayMAC(net.ParseIP("192.168.14.11")) - if err != nil { - t.Fatal(err) - } - if got[0]&0x02 == 0 { - t.Errorf("%s : bit « administrée localement » absent", got) - } - if got[0]&0x01 != 0 { - t.Errorf("%s : MAC de groupe (multicast)", got) - } -} - -func TestHostGatewayMACDiffersFromTheRouterMACOfTheSameHost(t *testing.T) { - got, _ := hostGatewayMAC(net.ParseIP("192.168.14.11")) - if got.String() == "02:02:c0:a8:0e:0b" { - t.Errorf("%s : même MAC que la RMAC du L3VNI de cet hôte", got) - } -} - -func TestHostGatewayMACRejectsANonIPv4VTEP(t *testing.T) { - for _, vtep := range []net.IP{nil, net.ParseIP("fd00::1")} { - if mac, err := hostGatewayMAC(vtep); err == nil { - t.Errorf("VTEP %v accepté : %s", vtep, mac) - } - } -}