From 4917ea3dd2ba06de2a4d42c3dca87c8f4c13ff3a Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 3 Mar 2018 12:30:54 +0000 Subject: [PATCH] Per-platform TUN defaults --- src/yggdrasil/debug.go | 14 ++++++++++++++ src/yggdrasil/tun.go | 13 +++++++++---- src/yggdrasil/tun_darwin.go | 9 ++++++--- src/yggdrasil/tun_linux.go | 9 ++++++--- src/yggdrasil/tun_openbsd.go | 9 ++++++--- src/yggdrasil/tun_other.go | 9 ++++++--- src/yggdrasil/tun_windows.go | 9 ++++++--- yggdrasil.go | 10 +++------- 8 files changed, 56 insertions(+), 26 deletions(-) diff --git a/src/yggdrasil/debug.go b/src/yggdrasil/debug.go index 79fa0596..a93b367f 100644 --- a/src/yggdrasil/debug.go +++ b/src/yggdrasil/debug.go @@ -159,6 +159,20 @@ func (c *Core) DEBUG_getDHTSize() int { return total } +// TUN defaults + +func (c *Core) DEBUG_GetTUNDefaultIfName() string { + return getDefaults().defaultIfName +} + +func (c *Core) DEBUG_GetTUNDefaultIfMTU() int { + return getDefaults().defaultIfMTU +} + +func (c *Core) DEBUG_GetTUNDefaultIfTAPMode() bool { + return getDefaults().defaultIfTAPMode +} + // udpInterface // FIXME udpInterface isn't exported // So debug functions need to work differently... diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index 946467ad..db65338d 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -5,6 +5,8 @@ package yggdrasil import "os" import ethernet "github.com/songgao/packets/ethernet" +const DEFAULT_MTU = 65535 + const IPv6_HEADER_LENGTH = 40 const ETHER_HEADER_LENGTH = 14 @@ -28,12 +30,15 @@ type tunDevice struct { } type tunDefaultParameters struct { - maxMTU int + maximumIfMTU int + defaultIfMTU int + defaultIfName string + defaultIfTAPMode bool } -func getMTUFromMax(mtu int) int { - if mtu > defaultTUNParameters().maxMTU { - return defaultTUNParameters().maxMTU +func getSupportedMTU(mtu int) int { + if mtu > getDefaults().maximumIfMTU { + return getDefaults().maximumIfMTU } return mtu } diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index 98abc1bf..9a0c7127 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -10,9 +10,12 @@ import "golang.org/x/sys/unix" import water "github.com/neilalexander/water" -func defaultTUNParameters() tunDefaultParameters { +func getDefaults() tunDefaultParameters { return tunDefaultParameters{ - maxMTU: 65535, + maximumIfMTU: 65535, + defaultIfMTU: DEFAULT_MTU, + defaultIfName: "auto", + defaultIfTAPMode: false, } } @@ -26,7 +29,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = getMTUFromMax(mtu) + tun.mtu = getSupportedMTU(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_linux.go b/src/yggdrasil/tun_linux.go index 9287ff37..be231719 100644 --- a/src/yggdrasil/tun_linux.go +++ b/src/yggdrasil/tun_linux.go @@ -9,9 +9,12 @@ import "strings" import water "github.com/neilalexander/water" -func defaultTUNParameters() tunDefaultParameters { +func getDefaults() tunDefaultParameters { return tunDefaultParameters{ - maxMTU: 65535, + maximumIfMTU: 65535, + defaultIfMTU: DEFAULT_MTU, + defaultIfName: "auto", + defaultIfTAPMode: false, } } @@ -30,7 +33,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = getMTUFromMax(mtu) + tun.mtu = getSupportedMTU(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_openbsd.go b/src/yggdrasil/tun_openbsd.go index 84ba2d9b..24f0f709 100644 --- a/src/yggdrasil/tun_openbsd.go +++ b/src/yggdrasil/tun_openbsd.go @@ -15,9 +15,12 @@ import water "github.com/neilalexander/water" // to disable the PI header when in TUN mode, so we need to modify the read/ // writes to handle those first four bytes -func defaultTUNParameters() tunDefaultParameters { +func getDefaults() tunDefaultParameters { return tunDefaultParameters{ - maxMTU: 16384, + maximumIfMTU: 16384, + defaultIfMTU: DEFAULT_MTU, + defaultIfName: "/dev/tap0", + defaultIfTAPMode: true, } } @@ -86,7 +89,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = getMTUFromMax(mtu) + tun.mtu = getSupportedMTU(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_other.go b/src/yggdrasil/tun_other.go index 5e33feec..0f4ded6f 100644 --- a/src/yggdrasil/tun_other.go +++ b/src/yggdrasil/tun_other.go @@ -7,9 +7,12 @@ import water "github.com/neilalexander/water" // This is to catch unsupported platforms // If your platform supports tun devices, you could try configuring it manually -func defaultTUNParameters() tunDefaultParameters { +func getDefaults() tunDefaultParameters { return tunDefaultParameters{ - maxMTU: 65535, + maximumIfMTU: 65535, + defaultIfMTU: DEFAULT_MTU, + defaultIfName: "none", + defaultIfTAPMode: false, } } @@ -25,7 +28,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = getMTUFromMax(mtu) + tun.mtu = getSupportedMTU(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_windows.go b/src/yggdrasil/tun_windows.go index 9e8dda43..d23b201e 100644 --- a/src/yggdrasil/tun_windows.go +++ b/src/yggdrasil/tun_windows.go @@ -7,9 +7,12 @@ import "fmt" // This is to catch Windows platforms -func defaultTUNParameters() tunDefaultParameters { +func getDefaults() tunDefaultParameters { return tunDefaultParameters{ - maxMTU: 65535, + maximumIfMTU: 65535, + defaultIfMTU: DEFAULT_MTU, + defaultIfName: "auto", + defaultIfTAPMode: true, } } @@ -47,7 +50,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = getMTUFromMax(mtu) + tun.mtu = getSupportedMTU(mtu) err = tun.setupMTU(tun.mtu) if err != nil { panic(err) diff --git a/yggdrasil.go b/yggdrasil.go index 50a9c63c..0184dede 100644 --- a/yggdrasil.go +++ b/yggdrasil.go @@ -117,13 +117,9 @@ func generateConfig() *nodeConfig { cfg.Peers = []string{} cfg.Multicast = true cfg.LinkLocal = "" - cfg.IfName = "auto" - cfg.IfMTU = 1280 - if runtime.GOOS == "windows" { - cfg.IfTAPMode = true - } else { - cfg.IfTAPMode = false - } + cfg.IfName = core.DEBUG_GetTUNDefaultIfName() + cfg.IfMTU = core.DEBUG_GetTUNDefaultIfMTU() + cfg.IfTAPMode = core.DEBUG_GetTUNDefaultIfTAPMode() return &cfg }