diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index b22a341a..946467ad 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -27,6 +27,17 @@ type tunDevice struct { iface tunInterface } +type tunDefaultParameters struct { + maxMTU int +} + +func getMTUFromMax(mtu int) int { + if mtu > defaultTUNParameters().maxMTU { + return defaultTUNParameters().maxMTU + } + return mtu +} + func (tun *tunDevice) init(core *Core) { tun.core = core tun.icmpv6.init(tun) diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index eb1489f3..98abc1bf 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -10,6 +10,12 @@ import "golang.org/x/sys/unix" import water "github.com/neilalexander/water" +func defaultTUNParameters() tunDefaultParameters { + return tunDefaultParameters{ + maxMTU: 65535, + } +} + func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { if iftapmode { tun.core.log.Printf("TAP mode is not supported on this platform, defaulting to TUN") @@ -20,7 +26,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = mtu + tun.mtu = getMTUFromMax(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_linux.go b/src/yggdrasil/tun_linux.go index 5fdf6409..9287ff37 100644 --- a/src/yggdrasil/tun_linux.go +++ b/src/yggdrasil/tun_linux.go @@ -9,6 +9,12 @@ import "strings" import water "github.com/neilalexander/water" +func defaultTUNParameters() tunDefaultParameters { + return tunDefaultParameters{ + maxMTU: 65535, + } +} + func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { var config water.Config if iftapmode { @@ -24,7 +30,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now + tun.mtu = getMTUFromMax(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_openbsd.go b/src/yggdrasil/tun_openbsd.go index c58518e6..84ba2d9b 100644 --- a/src/yggdrasil/tun_openbsd.go +++ b/src/yggdrasil/tun_openbsd.go @@ -15,6 +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 { + return tunDefaultParameters{ + maxMTU: 16384, + } +} + // Warning! When porting this to other BSDs, the tuninfo struct can appear with // the fields in a different order, and the consts below might also have // different values @@ -80,7 +86,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now + tun.mtu = getMTUFromMax(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_other.go b/src/yggdrasil/tun_other.go index ec56408e..5e33feec 100644 --- a/src/yggdrasil/tun_other.go +++ b/src/yggdrasil/tun_other.go @@ -7,6 +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 { + return tunDefaultParameters{ + maxMTU: 65535, + } +} + func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { var config water.Config if iftapmode { @@ -19,7 +25,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now + tun.mtu = getMTUFromMax(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_windows.go b/src/yggdrasil/tun_windows.go index a2108fe9..9e8dda43 100644 --- a/src/yggdrasil/tun_windows.go +++ b/src/yggdrasil/tun_windows.go @@ -7,6 +7,12 @@ import "fmt" // This is to catch Windows platforms +func defaultTUNParameters() tunDefaultParameters { + return tunDefaultParameters{ + maxMTU: 65535, + } +} + func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { if !iftapmode { tun.core.log.Printf("TUN mode is not supported on this platform, defaulting to TAP") @@ -41,7 +47,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = mtu + tun.mtu = getMTUFromMax(mtu) err = tun.setupMTU(tun.mtu) if err != nil { panic(err)