Don't allow exceeding maximum MTU for a given platform

This commit is contained in:
Neil Alexander 2018-03-03 11:47:14 +00:00
parent 7c0102e43d
commit bec898a326
6 changed files with 46 additions and 5 deletions

View File

@ -27,6 +27,17 @@ type tunDevice struct {
iface tunInterface 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) { func (tun *tunDevice) init(core *Core) {
tun.core = core tun.core = core
tun.icmpv6.init(tun) tun.icmpv6.init(tun)

View File

@ -10,6 +10,12 @@ import "golang.org/x/sys/unix"
import water "github.com/neilalexander/water" 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 { func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
if iftapmode { if iftapmode {
tun.core.log.Printf("TAP mode is not supported on this platform, defaulting to TUN") 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) panic(err)
} }
tun.iface = iface tun.iface = iface
tun.mtu = mtu tun.mtu = getMTUFromMax(mtu)
return tun.setupAddress(addr) return tun.setupAddress(addr)
} }

View File

@ -9,6 +9,12 @@ import "strings"
import water "github.com/neilalexander/water" 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 { func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
var config water.Config var config water.Config
if iftapmode { if iftapmode {
@ -24,7 +30,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
panic(err) panic(err)
} }
tun.iface = iface tun.iface = iface
tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now tun.mtu = getMTUFromMax(mtu)
return tun.setupAddress(addr) return tun.setupAddress(addr)
} }

View File

@ -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/ // to disable the PI header when in TUN mode, so we need to modify the read/
// writes to handle those first four bytes // 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 // 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 // the fields in a different order, and the consts below might also have
// different values // different values
@ -80,7 +86,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
panic(err) panic(err)
} }
tun.iface = iface tun.iface = iface
tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now tun.mtu = getMTUFromMax(mtu)
return tun.setupAddress(addr) return tun.setupAddress(addr)
} }

View File

@ -7,6 +7,12 @@ import water "github.com/neilalexander/water"
// This is to catch unsupported platforms // This is to catch unsupported platforms
// If your platform supports tun devices, you could try configuring it manually // 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 { func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
var config water.Config var config water.Config
if iftapmode { if iftapmode {
@ -19,7 +25,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
panic(err) panic(err)
} }
tun.iface = iface tun.iface = iface
tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now tun.mtu = getMTUFromMax(mtu)
return tun.setupAddress(addr) return tun.setupAddress(addr)
} }

View File

@ -7,6 +7,12 @@ import "fmt"
// This is to catch Windows platforms // 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 { func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
if !iftapmode { if !iftapmode {
tun.core.log.Printf("TUN mode is not supported on this platform, defaulting to TAP") 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) panic(err)
} }
tun.iface = iface tun.iface = iface
tun.mtu = mtu tun.mtu = getMTUFromMax(mtu)
err = tun.setupMTU(tun.mtu) err = tun.setupMTU(tun.mtu)
if err != nil { if err != nil {
panic(err) panic(err)