Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle edge cases in interface and routing config #1136

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions routing/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// that can be found in the LICENSE file in the root of the source
// tree.

//go:build !linux
// +build !linux

// Package routing is currently only supported in Linux, but the build system requires a valid go file for all architectures.
Expand Down
32 changes: 26 additions & 6 deletions routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// that can be found in the LICENSE file in the root of the source
// tree.

//go:build linux
// +build linux

// Package routing provides a very basic but mostly functional implementation of
Expand Down Expand Up @@ -134,8 +135,11 @@ func (r *router) route(routes routeSlice, input net.HardwareAddr, src, dst net.I
if rt.InputIface != 0 && rt.InputIface != inputIndex {
continue
}
// use the default gateway with lowest metric
if rt.Src == nil && rt.Dst == nil {
defaultGateway = rt
if defaultGateway == nil && rt.Gateway != nil {
defaultGateway = rt
}
continue
}
if rt.Src != nil && !rt.Src.Contains(src) {
Expand Down Expand Up @@ -194,19 +198,31 @@ loop:
for _, attr := range attrs {
switch attr.Attr.Type {
case syscall.RTA_DST:
routeInfo.Dst = &net.IPNet{
inet := net.IPNet{
IP: net.IP(attr.Value),
Mask: net.CIDRMask(int(rt.DstLen), len(attr.Value)*8),
}
if inet.IP.IsMulticast() || inet.IP.IsInterfaceLocalMulticast() || inet.IP.IsLinkLocalMulticast() || inet.IP.IsUnspecified() || inet.IP.IsLinkLocalUnicast() {
continue
}
routeInfo.Dst = &inet
case syscall.RTA_SRC:
routeInfo.Src = &net.IPNet{
inet := net.IPNet{
IP: net.IP(attr.Value),
Mask: net.CIDRMask(int(rt.SrcLen), len(attr.Value)*8),
Mask: net.CIDRMask(int(rt.DstLen), len(attr.Value)*8),
}
if inet.IP.IsMulticast() || inet.IP.IsInterfaceLocalMulticast() || inet.IP.IsLinkLocalMulticast() || inet.IP.IsUnspecified() || inet.IP.IsLinkLocalUnicast() {
continue
}
routeInfo.Src = &inet
case syscall.RTA_GATEWAY:
routeInfo.Gateway = net.IP(attr.Value)
case syscall.RTA_PREFSRC:
routeInfo.PrefSrc = net.IP(attr.Value)
ip := net.IP(attr.Value)
if ip.IsLoopback() || ip.IsMulticast() || ip.IsInterfaceLocalMulticast() || ip.IsLinkLocalMulticast() || ip.IsUnspecified() || ip.IsLinkLocalUnicast() {
continue
}
routeInfo.PrefSrc = ip
case syscall.RTA_IIF:
routeInfo.InputIface = *(*uint32)(unsafe.Pointer(&attr.Value[0]))
case syscall.RTA_OIF:
Expand All @@ -231,8 +247,12 @@ loop:
if err != nil {
return nil, err
}
for _, addr := range ifaceAddrs {
for i, addr := range ifaceAddrs {
if inet, ok := addr.(*net.IPNet); ok {
// filter out any non routable addresses
if i < len(ifaceAddrs)-1 && (inet.IP.IsMulticast() || inet.IP.IsInterfaceLocalMulticast() || inet.IP.IsLinkLocalMulticast() || inet.IP.IsUnspecified() || inet.IP.IsLinkLocalUnicast()) {
continue
}
// Go has a nasty habit of giving you IPv4s as ::ffff:1.2.3.4 instead of 1.2.3.4.
// We want to use mapped v4 addresses as v4 preferred addresses, never as v6
// preferred addresses.
Expand Down
3 changes: 3 additions & 0 deletions routing/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// that can be found in the LICENSE file in the root of the source
// tree.

//go:build linux
// +build linux

package routing
Expand Down Expand Up @@ -533,6 +534,8 @@ func init() {
testRouter.addrs[2] = ipAddrs{v4: net.IPv4(192, 168, 1, 2)}
defaultRoute := &rtInfo{Gateway: net.IPv4(192, 168, 1, 1), InputIface: 0, OutputIface: 2, Priority: 600}
testRouter.v4 = append(testRouter.v4, defaultRoute)
defaultRouteWithHighPriority := &rtInfo{Gateway: net.IPv4(172, 0, 0, 1), InputIface: 0, OutputIface: 2, Priority: 1000}
testRouter.v4 = append(testRouter.v4, defaultRouteWithHighPriority)
// Configure local route
localHW, _ := net.ParseMAC("01:23:45:67:89:ac")
localInterface := net.Interface{Index: 1, MTU: 1500, Name: "Local", HardwareAddr: localHW, Flags: 1}
Expand Down