f-88: fip: face publique, traduction et routage par source sur l'hôte #88

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-29 21:32:29 +01:00
commit 350a1dd82f
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
8 changed files with 538 additions and 18 deletions

View file

@ -51,6 +51,19 @@ func TableRoutesVia(table int, iface string) ([]*net.IPNet, error) {
return dsts, nil
}
func RouteReplaceDev(table int, dst *net.IPNet, iface string) error {
link, err := netlink.LinkByName(iface)
if err != nil {
return err
}
return netlink.RouteReplace(&netlink.Route{
LinkIndex: link.Attrs().Index,
Dst: dst,
Table: table,
Scope: netlink.SCOPE_LINK,
})
}
func RouteDelViaIfExists(table int, dst *net.IPNet, gw net.IP, iface string) error {
link, err := netlink.LinkByName(iface)
if err != nil {

View file

@ -34,3 +34,7 @@ func HasUnreachableDefault(_, _ int) (bool, error) {
func RouteDelViaIfExists(_ int, _ *net.IPNet, _ net.IP, _ string) error {
return errors.New("not supported on this platform")
}
func RouteReplaceDev(_ int, _ *net.IPNet, _ string) error {
return errors.New("not supported on this platform")
}

View file

@ -10,24 +10,22 @@ import (
"github.com/vishvananda/netlink"
)
func sourceRule(src net.IP, table, priority int, suppressDefault bool) *netlink.Rule {
func sourceRule(src net.IP, dst *net.IPNet, table, priority int) *netlink.Rule {
r := netlink.NewRule()
r.Family = netlink.FAMILY_V4
r.Src = &net.IPNet{IP: src.To4(), Mask: net.CIDRMask(32, 32)}
r.Dst = dst
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 RuleAddFrom(src net.IP, dst *net.IPNet, table, priority int) error {
return netlink.RuleAdd(sourceRule(src, dst, table, priority))
}
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) {
func RuleDelFromIfExists(src net.IP, dst *net.IPNet, table, priority int) error {
if err := netlink.RuleDel(sourceRule(src, dst, table, priority)); err != nil && !errors.Is(err, syscall.ENOENT) {
return err
}
return nil

View file

@ -10,15 +10,13 @@ import (
)
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 {
r := sourceRule(net.ParseIP("10.46.1.10"), nil, 170786058, 101)
if r.Family != netlink.FAMILY_V4 || r.Src.String() != "10.46.1.10/32" || r.Dst != nil || 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)
_, vpc, _ := net.ParseCIDR("10.46.0.0/16")
toVPC := sourceRule(net.ParseIP("10.46.1.10"), vpc, 254, 100)
if toVPC.Dst.String() != "10.46.0.0/16" || toVPC.Table != 254 || toVPC.SuppressPrefixlen != -1 {
t.Errorf("règle vers le VPC %+v", toVPC)
}
}

View file

@ -7,10 +7,10 @@ import (
"net"
)
func RuleAddFrom(_ net.IP, _, _ int, _ bool) error {
func RuleAddFrom(_ net.IP, _ *net.IPNet, _, _ int) error {
return errors.New("not supported on this platform")
}
func RuleDelFromIfExists(_ net.IP, _, _ int, _ bool) error {
func RuleDelFromIfExists(_ net.IP, _ *net.IPNet, _, _ int) error {
return errors.New("not supported on this platform")
}