mirror of
https://github.com/slackhq/nebula.git
synced 2025-11-12 07:53:58 +01:00
fix the tun_linux.go bug where we would crash if setting default mtu didn't work on the first try (#1268)
This commit is contained in:
parent
2b1a59c779
commit
602dca8508
@ -11,6 +11,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
@ -367,12 +368,6 @@ func (t *tun) Activate() error {
|
|||||||
return fmt.Errorf("failed to bring the tun device up: %s", err)
|
return fmt.Errorf("failed to bring the tun device up: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the interface
|
|
||||||
ifrf.Flags = ifrf.Flags | unix.IFF_UP | unix.IFF_RUNNING
|
|
||||||
if err = ioctl(t.ioctlFd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
|
|
||||||
return fmt.Errorf("failed to run tun device: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//set route MTU
|
//set route MTU
|
||||||
for i := range t.vpnNetworks {
|
for i := range t.vpnNetworks {
|
||||||
if err = t.setDefaultRoute(t.vpnNetworks[i]); err != nil {
|
if err = t.setDefaultRoute(t.vpnNetworks[i]); err != nil {
|
||||||
@ -385,7 +380,11 @@ func (t *tun) Activate() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//todo do we want to keep the link-local address?
|
// Run the interface
|
||||||
|
ifrf.Flags = ifrf.Flags | unix.IFF_UP | unix.IFF_RUNNING
|
||||||
|
if err = ioctl(t.ioctlFd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
|
||||||
|
return fmt.Errorf("failed to run tun device: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -400,8 +399,6 @@ func (t *tun) setMTU() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
||||||
// Default route
|
|
||||||
|
|
||||||
dr := &net.IPNet{
|
dr := &net.IPNet{
|
||||||
IP: cidr.Masked().Addr().AsSlice(),
|
IP: cidr.Masked().Addr().AsSlice(),
|
||||||
Mask: net.CIDRMask(cidr.Bits(), cidr.Addr().BitLen()),
|
Mask: net.CIDRMask(cidr.Bits(), cidr.Addr().BitLen()),
|
||||||
@ -420,7 +417,20 @@ func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
|||||||
}
|
}
|
||||||
err := netlink.RouteReplace(&nr)
|
err := netlink.RouteReplace(&nr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to set mtu %v on the default route %v; %v", t.DefaultMTU, dr, err)
|
t.l.WithError(err).WithField("cidr", cidr).Warn("Failed to set default route MTU, retrying")
|
||||||
|
//retry twice more -- on some systems there appears to be a race condition where if we set routes too soon, netlink says `invalid argument`
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
err = netlink.RouteReplace(&nr)
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
t.l.WithError(err).WithField("cidr", cidr).WithField("mtu", t.DefaultMTU).Warn("Failed to set default route MTU, retrying")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set mtu %v on the default route %v; %v", t.DefaultMTU, dr, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -547,7 +557,6 @@ func (t *tun) updateRoutes(r netlink.RouteUpdate) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: IPV6-WORK what if not ok?
|
|
||||||
gwAddr, ok := netip.AddrFromSlice(r.Gw)
|
gwAddr, ok := netip.AddrFromSlice(r.Gw)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.l.WithField("route", r).Debug("Ignoring route update, invalid gateway address")
|
t.l.WithField("route", r).Debug("Ignoring route update, invalid gateway address")
|
||||||
@ -568,12 +577,6 @@ func (t *tun) updateRoutes(r netlink.RouteUpdate) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if x := r.Dst.IP.To4(); x == nil {
|
|
||||||
// Nebula only handles ipv4 on the overlay currently
|
|
||||||
t.l.WithField("route", r).Debug("Ignoring route update, destination is not ipv4")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
dstAddr, ok := netip.AddrFromSlice(r.Dst.IP)
|
dstAddr, ok := netip.AddrFromSlice(r.Dst.IP)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.l.WithField("route", r).Debug("Ignoring route update, invalid destination address")
|
t.l.WithField("route", r).Debug("Ignoring route update, invalid destination address")
|
||||||
@ -602,11 +605,11 @@ func (t *tun) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if t.ReadWriteCloser != nil {
|
if t.ReadWriteCloser != nil {
|
||||||
t.ReadWriteCloser.Close()
|
_ = t.ReadWriteCloser.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.ioctlFd > 0 {
|
if t.ioctlFd > 0 {
|
||||||
os.NewFile(t.ioctlFd, "ioctlFd").Close()
|
_ = os.NewFile(t.ioctlFd, "ioctlFd").Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user