f-88: netif: MAC d'IP flottante, règles de routage par source #88
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
ee7ecbf69f
commit
d51359d4f2
5 changed files with 110 additions and 0 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
34
internal/netif/rule_linux.go
Normal file
34
internal/netif/rule_linux.go
Normal file
|
|
@ -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
|
||||
}
|
||||
24
internal/netif/rule_linux_test.go
Normal file
24
internal/netif/rule_linux_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
16
internal/netif/rule_other.go
Normal file
16
internal/netif/rule_other.go
Normal file
|
|
@ -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")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue