f-81: l3vni: route unreachable default dans chaque VRF de VPC #81
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
dd9844284d
commit
cc8ddfe882
5 changed files with 115 additions and 1 deletions
|
|
@ -3,11 +3,16 @@
|
|||
package netif
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"syscall"
|
||||
|
||||
"github.com/vishvananda/netlink"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var defaultIPv4 = &net.IPNet{IP: net.IPv4zero.To4(), Mask: net.CIDRMask(0, 32)}
|
||||
|
||||
func RouteAdd(iface string, subnet *net.IPNet) error {
|
||||
return RouteAddFrom(iface, subnet, nil)
|
||||
}
|
||||
|
|
@ -45,3 +50,44 @@ func TableRoutesVia(table int, iface string) ([]*net.IPNet, error) {
|
|||
}
|
||||
return dsts, nil
|
||||
}
|
||||
|
||||
func unreachableDefault(table, metric int) *netlink.Route {
|
||||
return &netlink.Route{
|
||||
Dst: defaultIPv4,
|
||||
Table: table,
|
||||
Type: unix.RTN_UNREACHABLE,
|
||||
Priority: metric,
|
||||
}
|
||||
}
|
||||
|
||||
func RouteReplaceUnreachableDefault(table, metric int) error {
|
||||
return netlink.RouteReplace(unreachableDefault(table, metric))
|
||||
}
|
||||
|
||||
func RouteDelUnreachableDefaultIfExists(table, metric int) error {
|
||||
if err := netlink.RouteDel(unreachableDefault(table, metric)); err != nil && !errors.Is(err, syscall.ESRCH) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func HasUnreachableDefault(table, metric int) (bool, error) {
|
||||
routes, err := netlink.RouteListFiltered(netlink.FAMILY_V4, &netlink.Route{
|
||||
Table: table,
|
||||
Type: unix.RTN_UNREACHABLE,
|
||||
}, netlink.RT_FILTER_TABLE|netlink.RT_FILTER_TYPE)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, r := range routes {
|
||||
if r.Priority == metric && (r.Dst == nil || isDefault(r.Dst)) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func isDefault(dst *net.IPNet) bool {
|
||||
ones, _ := dst.Mask.Size()
|
||||
return ones == 0
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue