fix(netlink): RouteList list routes from all tables

- Do not filter by link anymore
- IPv6 detection simplified
This commit is contained in:
Quentin McGaw
2023-06-08 09:12:46 +00:00
parent c58d6d4de2
commit 40cdb4f662
12 changed files with 43 additions and 48 deletions

View File

@@ -5,35 +5,28 @@ import (
)
func (n *NetLink) IsIPv6Supported() (supported bool, err error) {
links, err := n.LinkList()
routes, err := n.RouteList(FamilyV6)
if err != nil {
return false, fmt.Errorf("listing links: %w", err)
return false, fmt.Errorf("listing IPv6 routes: %w", err)
}
var totalRoutes uint
for _, link := range links {
link := link
routes, err := n.RouteList(&link, FamilyV6)
if err != nil {
return false, fmt.Errorf("listing IPv6 routes for link %s: %w",
link.Name, err)
}
// Check each route for IPv6 due to Podman bug listing IPv4 routes
// as IPv6 routes at container start, see:
// https://github.com/qdm12/gluetun/issues/1241#issuecomment-1333405949
for _, route := range routes {
sourceIsIPv6 := route.Src.IsValid() && route.Src.Is6()
destinationIsIPv6 := route.Dst.IsValid() && route.Dst.Addr().Is6()
if sourceIsIPv6 || destinationIsIPv6 {
n.debugLogger.Debugf("IPv6 is supported by link %s", link.Name)
return true, nil
// Check each route for IPv6 due to Podman bug listing IPv4 routes
// as IPv6 routes at container start, see:
// https://github.com/qdm12/gluetun/issues/1241#issuecomment-1333405949
for _, route := range routes {
sourceIsIPv6 := route.Src.IsValid() && route.Src.Is6()
destinationIsIPv6 := route.Dst.IsValid() && route.Dst.Addr().Is6()
if sourceIsIPv6 || destinationIsIPv6 {
link, err := n.LinkByIndex(route.LinkIndex)
if err != nil {
return false, fmt.Errorf("finding IPv6 supported link: %w", err)
}
totalRoutes++
n.debugLogger.Debugf("IPv6 is supported by link %s", link.Name)
return true, nil
}
}
n.debugLogger.Debugf("IPv6 is not supported after searching %d links and %d routes",
len(links), totalRoutes)
n.debugLogger.Debugf("IPv6 is not supported after searching %d routes",
len(routes))
return false, nil
}

View File

@@ -6,10 +6,16 @@ import (
"github.com/vishvananda/netlink"
)
func (n *NetLink) RouteList(link *Link, family int) (
routes []Route, err error) {
netlinkLink := linkToNetlinkLink(link)
netlinkRoutes, err := netlink.RouteList(netlinkLink, family)
func (n *NetLink) RouteList(family int) (routes []Route, err error) {
// We set the filter to netlink.RT_FILTER_TABLE so that
// routes from all tables are listed, as long as the filter
// table is set to 0.
const filterMask = netlink.RT_FILTER_TABLE
// The filter is not left to `nil` otherwise non-main tables
// are ignored.
filter := &netlink.Route{}
netlinkRoutes, err := netlink.RouteListFiltered(family, filter, filterMask)
if err != nil {
return nil, err
}

View File

@@ -2,7 +2,7 @@
package netlink
func (n *NetLink) RouteList(link *Link, family int) (
func (n *NetLink) RouteList(family int) (
routes []Route, err error) {
panic("not implemented")
}