From bec898a32669e63a7fbfa52b287c826722faaf4a Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 3 Mar 2018 11:47:14 +0000 Subject: [PATCH] Don't allow exceeding maximum MTU for a given platform --- src/yggdrasil/tun.go | 11 +++++++++++ src/yggdrasil/tun_darwin.go | 8 +++++++- src/yggdrasil/tun_linux.go | 8 +++++++- src/yggdrasil/tun_openbsd.go | 8 +++++++- src/yggdrasil/tun_other.go | 8 +++++++- src/yggdrasil/tun_windows.go | 8 +++++++- 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index b22a341..946467a 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 eb1489f..98abc1b 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 5fdf640..9287ff3 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 c58518e..84ba2d9 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 ec56408..5e33fee 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 a2108fe..9e8dda4 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)