5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 15:21:37 +00:00

Merge pull request #643 from adamruzicka/mtu

Unify MTU datatypes across the codebase
This commit is contained in:
Neil Alexander 2020-01-07 22:39:38 +00:00 committed by GitHub
commit 2fc6f9a71d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 51 additions and 37 deletions

View File

@ -22,8 +22,11 @@ import (
"github.com/yggdrasil-network/yggdrasil-go/src/crypto" "github.com/yggdrasil-network/yggdrasil-go/src/crypto"
"github.com/yggdrasil-network/yggdrasil-go/src/defaults" "github.com/yggdrasil-network/yggdrasil-go/src/defaults"
"github.com/yggdrasil-network/yggdrasil-go/src/types"
) )
type MTU = types.MTU
// NodeState represents the active and previous configuration of an Yggdrasil // NodeState represents the active and previous configuration of an Yggdrasil
// node. A NodeState object is returned when starting an Yggdrasil node. Note // node. A NodeState object is returned when starting an Yggdrasil node. Note
// that this structure and related functions are likely to disappear soon. // that this structure and related functions are likely to disappear soon.
@ -71,7 +74,7 @@ type NodeConfig struct {
SigningPrivateKey string `comment:"Your private signing key. DO NOT share this with anyone!"` SigningPrivateKey string `comment:"Your private signing key. DO NOT share this with anyone!"`
LinkLocalTCPPort uint16 `comment:"The port number to be used for the link-local TCP listeners for the\nconfigured MulticastInterfaces. This option does not affect listeners\nspecified in the Listen option. Unless you plan to firewall link-local\ntraffic, it is best to leave this as the default value of 0. This\noption cannot currently be changed by reloading config during runtime."` LinkLocalTCPPort uint16 `comment:"The port number to be used for the link-local TCP listeners for the\nconfigured MulticastInterfaces. This option does not affect listeners\nspecified in the Listen option. Unless you plan to firewall link-local\ntraffic, it is best to leave this as the default value of 0. This\noption cannot currently be changed by reloading config during runtime."`
IfName string `comment:"Local network interface name for TUN adapter, or \"auto\" to select\nan interface automatically, or \"none\" to run without TUN."` IfName string `comment:"Local network interface name for TUN adapter, or \"auto\" to select\nan interface automatically, or \"none\" to run without TUN."`
IfMTU int `comment:"Maximum Transmission Unit (MTU) size for your local TUN interface.\nDefault is the largest supported size for your platform. The lowest\npossible value is 1280."` IfMTU MTU `comment:"Maximum Transmission Unit (MTU) size for your local TUN interface.\nDefault is the largest supported size for your platform. The lowest\npossible value is 1280."`
SessionFirewall SessionFirewall `comment:"The session firewall controls who can send/receive network traffic\nto/from. This is useful if you want to protect this node without\nresorting to using a real firewall. This does not affect traffic\nbeing routed via this node to somewhere else. Rules are prioritised as\nfollows: blacklist, whitelist, always allow outgoing, direct, remote."` SessionFirewall SessionFirewall `comment:"The session firewall controls who can send/receive network traffic\nto/from. This is useful if you want to protect this node without\nresorting to using a real firewall. This does not affect traffic\nbeing routed via this node to somewhere else. Rules are prioritised as\nfollows: blacklist, whitelist, always allow outgoing, direct, remote."`
TunnelRouting TunnelRouting `comment:"Allow tunneling non-Yggdrasil traffic over Yggdrasil. This effectively\nallows you to use Yggdrasil to route to, or to bridge other networks,\nsimilar to a VPN tunnel. Tunnelling works between any two nodes and\ndoes not require them to be directly peered."` TunnelRouting TunnelRouting `comment:"Allow tunneling non-Yggdrasil traffic over Yggdrasil. This effectively\nallows you to use Yggdrasil to route to, or to bridge other networks,\nsimilar to a VPN tunnel. Tunnelling works between any two nodes and\ndoes not require them to be directly peered."`
SwitchOptions SwitchOptions `comment:"Advanced options for tuning the switch. Normally you will not need\nto edit these options."` SwitchOptions SwitchOptions `comment:"Advanced options for tuning the switch. Normally you will not need\nto edit these options."`

View File

@ -1,5 +1,7 @@
package defaults package defaults
import "github.com/yggdrasil-network/yggdrasil-go/src/types"
// Defines which parameters are expected by default for configuration on a // Defines which parameters are expected by default for configuration on a
// specific platform. These values are populated in the relevant defaults_*.go // specific platform. These values are populated in the relevant defaults_*.go
// for the platform being targeted. They must be set. // for the platform being targeted. They must be set.
@ -14,7 +16,7 @@ type platformDefaultParameters struct {
DefaultMulticastInterfaces []string DefaultMulticastInterfaces []string
// TUN/TAP // TUN/TAP
MaximumIfMTU int MaximumIfMTU types.MTU
DefaultIfMTU int DefaultIfMTU types.MTU
DefaultIfName string DefaultIfName string
} }

View File

@ -55,7 +55,7 @@ type tunReader struct {
func (r *tunReader) _read() { func (r *tunReader) _read() {
// Get a slice to store the packet in // Get a slice to store the packet in
recvd := util.ResizeBytes(util.GetBytes(), r.tun.mtu+TUN_OFFSET_BYTES) recvd := util.ResizeBytes(util.GetBytes(), int(r.tun.mtu)+TUN_OFFSET_BYTES)
// Wait for a packet to be delivered to us through the TUN adapter // Wait for a packet to be delivered to us through the TUN adapter
n, err := r.tun.iface.Read(recvd, TUN_OFFSET_BYTES) n, err := r.tun.iface.Read(recvd, TUN_OFFSET_BYTES)
if n <= TUN_OFFSET_BYTES || err != nil { if n <= TUN_OFFSET_BYTES || err != nil {

View File

@ -25,8 +25,11 @@ import (
"github.com/yggdrasil-network/yggdrasil-go/src/crypto" "github.com/yggdrasil-network/yggdrasil-go/src/crypto"
"github.com/yggdrasil-network/yggdrasil-go/src/defaults" "github.com/yggdrasil-network/yggdrasil-go/src/defaults"
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil" "github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
"github.com/yggdrasil-network/yggdrasil-go/src/types"
) )
type MTU = types.MTU
const tun_IPv6_HEADER_LENGTH = 40 const tun_IPv6_HEADER_LENGTH = 40
// TunAdapter represents a running TUN interface and extends the // TunAdapter represents a running TUN interface and extends the
@ -46,7 +49,7 @@ type TunAdapter struct {
subnet address.Subnet subnet address.Subnet
ckr cryptokey ckr cryptokey
icmpv6 ICMPv6 icmpv6 ICMPv6
mtu int mtu MTU
iface tun.Device iface tun.Device
phony.Inbox // Currently only used for _handlePacket from the reader, TODO: all the stuff that currently needs a mutex below phony.Inbox // Currently only used for _handlePacket from the reader, TODO: all the stuff that currently needs a mutex below
//mutex sync.RWMutex // Protects the below //mutex sync.RWMutex // Protects the below
@ -63,7 +66,7 @@ type TunOptions struct {
// Gets the maximum supported MTU for the platform based on the defaults in // Gets the maximum supported MTU for the platform based on the defaults in
// defaults.GetDefaults(). // defaults.GetDefaults().
func getSupportedMTU(mtu int) int { func getSupportedMTU(mtu MTU) MTU {
if mtu < 1280 { if mtu < 1280 {
return 1280 return 1280
} }
@ -85,7 +88,7 @@ func (tun *TunAdapter) Name() string {
// MTU gets the adapter's MTU. This can range between 1280 and 65535, although // MTU gets the adapter's MTU. This can range between 1280 and 65535, although
// the maximum value is determined by your platform. The returned value will // the maximum value is determined by your platform. The returned value will
// never exceed that of MaximumMTU(). // never exceed that of MaximumMTU().
func (tun *TunAdapter) MTU() int { func (tun *TunAdapter) MTU() MTU {
return getSupportedMTU(tun.mtu) return getSupportedMTU(tun.mtu)
} }
@ -96,14 +99,14 @@ func DefaultName() string {
// DefaultMTU gets the default TUN interface MTU for your platform. This can // DefaultMTU gets the default TUN interface MTU for your platform. This can
// be as high as MaximumMTU(), depending on platform, but is never lower than 1280. // be as high as MaximumMTU(), depending on platform, but is never lower than 1280.
func DefaultMTU() int { func DefaultMTU() MTU {
return defaults.GetDefaults().DefaultIfMTU return defaults.GetDefaults().DefaultIfMTU
} }
// MaximumMTU returns the maximum supported TUN interface MTU for your // MaximumMTU returns the maximum supported TUN interface MTU for your
// platform. This can be as high as 65535, depending on platform, but is never // platform. This can be as high as 65535, depending on platform, but is never
// lower than 1280. // lower than 1280.
func MaximumMTU() int { func MaximumMTU() MTU {
return defaults.GetDefaults().MaximumIfMTU return defaults.GetDefaults().MaximumIfMTU
} }
@ -165,7 +168,7 @@ func (tun *TunAdapter) _start() error {
if tun.MTU() != current.IfMTU { if tun.MTU() != current.IfMTU {
tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", current.IfMTU, tun.MTU(), MaximumMTU()) tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", current.IfMTU, tun.MTU(), MaximumMTU())
} }
tun.core.SetMaximumSessionMTU(uint16(tun.MTU())) tun.core.SetMaximumSessionMTU(tun.MTU())
tun.isOpen = true tun.isOpen = true
go tun.handler() go tun.handler()
tun.reader.Act(nil, tun.reader._read) // Start the reader tun.reader.Act(nil, tun.reader._read) // Start the reader

View File

@ -73,14 +73,14 @@ type in6_ifreq_lifetime struct {
} }
// Configures the TUN adapter with the correct IPv6 address and MTU. // Configures the TUN adapter with the correct IPv6 address and MTU.
func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error { func (tun *TunAdapter) setup(ifname string, addr string, mtu MTU) error {
iface, err := wgtun.CreateTUN(ifname, mtu) iface, err := wgtun.CreateTUN(ifname, int(mtu))
if err != nil { if err != nil {
panic(err) panic(err)
} }
tun.iface = iface tun.iface = iface
if mtu, err := iface.MTU(); err == nil { if mtu, err := iface.MTU(); err == nil {
tun.mtu = getSupportedMTU(mtu) tun.mtu = getSupportedMTU(MTU(mtu))
} else { } else {
tun.mtu = 0 tun.mtu = 0
} }

View File

@ -16,17 +16,17 @@ import (
) )
// Configures the "utun" adapter with the correct IPv6 address and MTU. // Configures the "utun" adapter with the correct IPv6 address and MTU.
func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error { func (tun *TunAdapter) setup(ifname string, addr string, mtu MTU) error {
if ifname == "auto" { if ifname == "auto" {
ifname = "utun" ifname = "utun"
} }
iface, err := wgtun.CreateTUN(ifname, mtu) iface, err := wgtun.CreateTUN(ifname, int(mtu))
if err != nil { if err != nil {
panic(err) panic(err)
} }
tun.iface = iface tun.iface = iface
if mtu, err := iface.MTU(); err == nil { if mtu, err := iface.MTU(); err == nil {
tun.mtu = getSupportedMTU(mtu) tun.mtu = getSupportedMTU(MTU(mtu))
} else { } else {
tun.mtu = 0 tun.mtu = 0
} }

View File

@ -10,17 +10,17 @@ import (
) )
// Configures the TUN adapter with the correct IPv6 address and MTU. // Configures the TUN adapter with the correct IPv6 address and MTU.
func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error { func (tun *TunAdapter) setup(ifname string, addr string, mtu MTU) error {
if ifname == "auto" { if ifname == "auto" {
ifname = "\000" ifname = "\000"
} }
iface, err := wgtun.CreateTUN(ifname, mtu) iface, err := wgtun.CreateTUN(ifname, int(mtu))
if err != nil { if err != nil {
panic(err) panic(err)
} }
tun.iface = iface tun.iface = iface
if mtu, err := iface.MTU(); err == nil { if mtu, err := iface.MTU(); err == nil {
tun.mtu = getSupportedMTU(mtu) tun.mtu = getSupportedMTU(MTU(mtu))
} else { } else {
tun.mtu = 0 tun.mtu = 0
} }
@ -43,7 +43,7 @@ func (tun *TunAdapter) setupAddress(addr string) error {
if err := netlink.AddrAdd(nlintf, nladdr); err != nil { if err := netlink.AddrAdd(nlintf, nladdr); err != nil {
return err return err
} }
if err := netlink.LinkSetMTU(nlintf, tun.mtu); err != nil { if err := netlink.LinkSetMTU(nlintf, int(tun.mtu)); err != nil {
return err return err
} }
if err := netlink.LinkSetUp(nlintf); err != nil { if err := netlink.LinkSetUp(nlintf); err != nil {

View File

@ -19,7 +19,7 @@ import (
// This is to catch Windows platforms // This is to catch Windows platforms
// Configures the TUN adapter with the correct IPv6 address and MTU. // Configures the TUN adapter with the correct IPv6 address and MTU.
func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error { func (tun *TunAdapter) setup(ifname string, addr string, mtu MTU) error {
if ifname == "auto" { if ifname == "auto" {
ifname = defaults.GetDefaults().DefaultIfName ifname = defaults.GetDefaults().DefaultIfName
} }
@ -30,7 +30,7 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error {
if guid, err = windows.GUIDFromString("{8f59971a-7872-4aa6-b2eb-061fc4e9d0a7}"); err != nil { if guid, err = windows.GUIDFromString("{8f59971a-7872-4aa6-b2eb-061fc4e9d0a7}"); err != nil {
return err return err
} }
if iface, err = wgtun.CreateTUNWithRequestedGUID(ifname, &guid, mtu); err != nil { if iface, err = wgtun.CreateTUNWithRequestedGUID(ifname, &guid, int(mtu)); err != nil {
return err return err
} }
tun.iface = iface tun.iface = iface
@ -42,15 +42,15 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error {
tun.log.Errorln("Failed to set up TUN MTU:", err) tun.log.Errorln("Failed to set up TUN MTU:", err)
return err return err
} }
if mtu, err = iface.MTU(); err == nil { if mtu, err := iface.MTU(); err == nil {
tun.mtu = mtu tun.mtu = MTU(mtu)
} }
return nil return nil
}) })
} }
// Sets the MTU of the TAP adapter. // Sets the MTU of the TAP adapter.
func (tun *TunAdapter) setupMTU(mtu int) error { func (tun *TunAdapter) setupMTU(mtu MTU) error {
if tun.iface == nil || tun.Name() == "" { if tun.iface == nil || tun.Name() == "" {
return errors.New("Can't configure MTU as TUN adapter is not present") return errors.New("Can't configure MTU as TUN adapter is not present")
} }

3
src/types/types.go Normal file
View File

@ -0,0 +1,3 @@
package types
type MTU uint16

View File

@ -99,7 +99,7 @@ type Session struct {
Coords []uint64 // The coordinates of the remote node Coords []uint64 // The coordinates of the remote node
BytesSent uint64 // Bytes sent to the session BytesSent uint64 // Bytes sent to the session
BytesRecvd uint64 // Bytes received from the session BytesRecvd uint64 // Bytes received from the session
MTU uint16 // The maximum supported message size of the session MTU MTU // The maximum supported message size of the session
Uptime time.Duration // How long this session has been active for Uptime time.Duration // How long this session has been active for
WasMTUFixed bool // This field is no longer used WasMTUFixed bool // This field is no longer used
} }
@ -364,8 +364,8 @@ func (c *Core) SetNodeInfo(nodeinfo interface{}, nodeinfoprivacy bool) {
} }
// GetMaximumSessionMTU returns the maximum allowed session MTU size. // GetMaximumSessionMTU returns the maximum allowed session MTU size.
func (c *Core) GetMaximumSessionMTU() uint16 { func (c *Core) GetMaximumSessionMTU() MTU {
var mtu uint16 var mtu MTU
phony.Block(&c.router, func() { phony.Block(&c.router, func() {
mtu = c.router.sessions.myMaximumMTU mtu = c.router.sessions.myMaximumMTU
}) })
@ -375,7 +375,7 @@ func (c *Core) GetMaximumSessionMTU() uint16 {
// SetMaximumSessionMTU sets the maximum allowed session MTU size. The default // SetMaximumSessionMTU sets the maximum allowed session MTU size. The default
// value is 65535 bytes. Session pings will be sent to update all open sessions // value is 65535 bytes. Session pings will be sent to update all open sessions
// if the MTU has changed. // if the MTU has changed.
func (c *Core) SetMaximumSessionMTU(mtu uint16) { func (c *Core) SetMaximumSessionMTU(mtu MTU) {
phony.Block(&c.router, func() { phony.Block(&c.router, func() {
if c.router.sessions.myMaximumMTU != mtu { if c.router.sessions.myMaximumMTU != mtu {
c.router.sessions.myMaximumMTU = mtu c.router.sessions.myMaximumMTU = mtu

View File

@ -8,10 +8,13 @@ import (
"github.com/yggdrasil-network/yggdrasil-go/src/crypto" "github.com/yggdrasil-network/yggdrasil-go/src/crypto"
"github.com/yggdrasil-network/yggdrasil-go/src/util" "github.com/yggdrasil-network/yggdrasil-go/src/util"
"github.com/yggdrasil-network/yggdrasil-go/src/types"
"github.com/Arceliar/phony" "github.com/Arceliar/phony"
) )
type MTU = types.MTU
// ConnError implements the net.Error interface // ConnError implements the net.Error interface
type ConnError struct { type ConnError struct {
error error
@ -65,7 +68,7 @@ type Conn struct {
nodeID *crypto.NodeID nodeID *crypto.NodeID
nodeMask *crypto.NodeID nodeMask *crypto.NodeID
session *sessionInfo session *sessionInfo
mtu uint16 mtu MTU
readCallback func([]byte) readCallback func([]byte)
readBuffer chan []byte readBuffer chan []byte
} }
@ -93,7 +96,7 @@ func (c *Conn) String() string {
return s return s
} }
func (c *Conn) setMTU(from phony.Actor, mtu uint16) { func (c *Conn) setMTU(from phony.Actor, mtu MTU) {
c.Act(from, func() { c.mtu = mtu }) c.Act(from, func() { c.mtu = mtu })
} }

View File

@ -36,8 +36,8 @@ type sessionInfo struct {
myHandle crypto.Handle // myHandle crypto.Handle //
theirNonce crypto.BoxNonce // theirNonce crypto.BoxNonce //
myNonce crypto.BoxNonce // myNonce crypto.BoxNonce //
theirMTU uint16 // theirMTU MTU //
myMTU uint16 // myMTU MTU //
wasMTUFixed bool // Was the MTU fixed by a receive error? wasMTUFixed bool // Was the MTU fixed by a receive error?
timeOpened time.Time // Time the session was opened timeOpened time.Time // Time the session was opened
time time.Time // Time we last received a packet time time.Time // Time we last received a packet
@ -63,7 +63,7 @@ type sessionPing struct {
Coords []byte // Coords []byte //
Tstamp int64 // unix time, but the only real requirement is that it increases Tstamp int64 // unix time, but the only real requirement is that it increases
IsPong bool // IsPong bool //
MTU uint16 // MTU MTU //
} }
// Updates session info in response to a ping, after checking that the ping is OK. // Updates session info in response to a ping, after checking that the ping is OK.
@ -117,7 +117,7 @@ type sessions struct {
lastCleanup time.Time lastCleanup time.Time
isAllowedHandler func(pubkey *crypto.BoxPubKey, initiator bool) bool // Returns true or false if session setup is allowed isAllowedHandler func(pubkey *crypto.BoxPubKey, initiator bool) bool // Returns true or false if session setup is allowed
isAllowedMutex sync.RWMutex // Protects the above isAllowedMutex sync.RWMutex // Protects the above
myMaximumMTU uint16 // Maximum allowed session MTU myMaximumMTU MTU // Maximum allowed session MTU
permShared map[crypto.BoxPubKey]*crypto.BoxSharedKey // Maps known permanent keys to their shared key, used by DHT a lot permShared map[crypto.BoxPubKey]*crypto.BoxSharedKey // Maps known permanent keys to their shared key, used by DHT a lot
sinfos map[crypto.Handle]*sessionInfo // Maps handle onto session info sinfos map[crypto.Handle]*sessionInfo // Maps handle onto session info
byTheirPerm map[crypto.BoxPubKey]*crypto.Handle // Maps theirPermPub onto handle byTheirPerm map[crypto.BoxPubKey]*crypto.Handle // Maps theirPermPub onto handle
@ -385,7 +385,7 @@ func (ss *sessions) handlePing(ping *sessionPing) {
// Get the MTU of the session. // Get the MTU of the session.
// Will be equal to the smaller of this node's MTU or the remote node's MTU. // Will be equal to the smaller of this node's MTU or the remote node's MTU.
// If sending over links with a maximum message size (this was a thing with the old UDP code), it could be further lowered, to a minimum of 1280. // If sending over links with a maximum message size (this was a thing with the old UDP code), it could be further lowered, to a minimum of 1280.
func (sinfo *sessionInfo) _getMTU() uint16 { func (sinfo *sessionInfo) _getMTU() MTU {
if sinfo.theirMTU == 0 || sinfo.myMTU == 0 { if sinfo.theirMTU == 0 || sinfo.myMTU == 0 {
return 0 return 0
} }

View File

@ -380,7 +380,7 @@ func (p *sessionPing) decode(bs []byte) bool {
if pType == wire_SessionPong { if pType == wire_SessionPong {
p.IsPong = true p.IsPong = true
} }
p.MTU = uint16(mtu) p.MTU = MTU(mtu)
return true return true
} }