From 536974f20c4dded53387808e0b26c2c9ffc06806 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 11 Feb 2018 23:09:05 +0000 Subject: [PATCH 1/5] Try to exchange MTUs when creating sessions --- src/yggdrasil/admin.go | 1 + src/yggdrasil/session.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index 63b6d60..ab0e33e 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -223,6 +223,7 @@ func (a *admin) getData_getSessions() []admin_nodeInfo { info := admin_nodeInfo{ {"IP", net.IP(sinfo.theirAddr[:]).String()}, {"coords", fmt.Sprint(sinfo.coords)}, + {"MTU", fmt.Sprint(sinfo.getMTU())}, } infos = append(infos, info) } diff --git a/src/yggdrasil/session.go b/src/yggdrasil/session.go index 06061bc..585dd5d 100644 --- a/src/yggdrasil/session.go +++ b/src/yggdrasil/session.go @@ -5,6 +5,7 @@ package yggdrasil // The session information consists of crypto keys and coords import "time" +import "net" type sessionInfo struct { core *Core @@ -19,6 +20,8 @@ type sessionInfo struct { myHandle handle theirNonce boxNonce myNonce boxNonce + theirMTU uint16 + myMTU uint16 time time.Time // Time we last received a packet coords []byte // coords of destination packet []byte // a buffered packet, sent immediately on ping/pong @@ -36,6 +39,7 @@ type sessionPing struct { coords []byte tstamp int64 // unix time, but the only real requirement is that it increases isPong bool + mtu uint16 } // Returns true if the session was updated, false otherwise @@ -56,6 +60,10 @@ func (s *sessionInfo) update(p *sessionPing) bool { s.theirNonce = boxNonce{} s.nonceMask = 0 } + s.core.log.Printf("Received MTU %d from %s", p.mtu, net.IP(s.theirAddr[:]).String()) + if p.mtu >= 1280 { + s.theirMTU = p.mtu + } s.coords = append([]byte{}, p.coords...) s.time = time.Now() s.tstamp = p.tstamp @@ -144,6 +152,8 @@ func (ss *sessions) createSession(theirPermKey *boxPubKey) *sessionInfo { sinfo.mySesPub = *pub sinfo.mySesPriv = *priv sinfo.myNonce = *newBoxNonce() + sinfo.theirMTU = 1280 + sinfo.myMTU = uint16(ss.core.tun.mtu) higher := false for idx := range ss.core.boxPub { if ss.core.boxPub[idx] > sinfo.theirPermPub[idx] { @@ -195,12 +205,14 @@ func (sinfo *sessionInfo) close() { func (ss *sessions) getPing(sinfo *sessionInfo) sessionPing { loc := ss.core.switchTable.getLocator() coords := loc.getCoords() + sinfo.core.log.Printf("Sending MTU %d to %s", sinfo.myMTU, net.IP(sinfo.theirAddr[:]).String()) ref := sessionPing{ sendPermPub: ss.core.boxPub, handle: sinfo.myHandle, sendSesPub: sinfo.mySesPub, tstamp: time.Now().Unix(), coords: coords, + mtu: sinfo.myMTU, } sinfo.myNonce.update() return ref @@ -289,6 +301,13 @@ func (n *boxNonce) minus(m *boxNonce) int64 { return diff } +func (sinfo *sessionInfo) getMTU() uint16 { + if sinfo.theirMTU < sinfo.myMTU { + return sinfo.theirMTU + } + return sinfo.myMTU +} + func (sinfo *sessionInfo) nonceIsOK(theirNonce *boxNonce) bool { // The bitmask is to allow for some non-duplicate out-of-order packets diff := theirNonce.minus(&sinfo.theirNonce) From 11a7c5c458514062f540b3954b0d8507cb9b0643 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 11 Feb 2018 23:58:30 +0000 Subject: [PATCH 2/5] Exchange MTU on wire --- src/yggdrasil/wire.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/yggdrasil/wire.go b/src/yggdrasil/wire.go index 7dbd7b8..6d57454 100644 --- a/src/yggdrasil/wire.go +++ b/src/yggdrasil/wire.go @@ -429,12 +429,14 @@ func (p *sessionPing) encode() []byte { bs = append(bs, wire_encode_uint64(wire_intToUint(p.tstamp))...) coords := wire_encode_coords(p.coords) bs = append(bs, coords...) + bs = append(bs, wire_encode_uint64(uint64(p.mtu))...) return bs } func (p *sessionPing) decode(bs []byte) bool { var pType uint64 var tstamp uint64 + var mtu uint64 switch { case !wire_chop_uint64(&pType, &bs): return false @@ -449,11 +451,14 @@ func (p *sessionPing) decode(bs []byte) bool { return false case !wire_chop_coords(&p.coords, &bs): return false + case !wire_chop_uint64(&mtu, &bs): + mtu = 1280 } p.tstamp = wire_intFromUint(tstamp) if pType == wire_SessionPong { p.isPong = true } + p.mtu = uint16(mtu) return true } From 7a0b48ffb3bc69964dca9b67ec1038e4e3665f1c Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 12 Feb 2018 00:01:15 +0000 Subject: [PATCH 3/5] Clean up session.go --- src/yggdrasil/session.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/yggdrasil/session.go b/src/yggdrasil/session.go index 585dd5d..681a194 100644 --- a/src/yggdrasil/session.go +++ b/src/yggdrasil/session.go @@ -5,7 +5,6 @@ package yggdrasil // The session information consists of crypto keys and coords import "time" -import "net" type sessionInfo struct { core *Core @@ -60,7 +59,6 @@ func (s *sessionInfo) update(p *sessionPing) bool { s.theirNonce = boxNonce{} s.nonceMask = 0 } - s.core.log.Printf("Received MTU %d from %s", p.mtu, net.IP(s.theirAddr[:]).String()) if p.mtu >= 1280 { s.theirMTU = p.mtu } @@ -205,7 +203,6 @@ func (sinfo *sessionInfo) close() { func (ss *sessions) getPing(sinfo *sessionInfo) sessionPing { loc := ss.core.switchTable.getLocator() coords := loc.getCoords() - sinfo.core.log.Printf("Sending MTU %d to %s", sinfo.myMTU, net.IP(sinfo.theirAddr[:]).String()) ref := sessionPing{ sendPermPub: ss.core.boxPub, handle: sinfo.myHandle, From 9fb45d77fc3944f8d158852c3863cab519bc41c6 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 14 Feb 2018 12:53:32 +0000 Subject: [PATCH 4/5] Add "IfMTU" configuration option to allow selecting TUN/TAP MTU --- yggdrasil.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/yggdrasil.go b/yggdrasil.go index 9e6cc86..35d8768 100644 --- a/yggdrasil.go +++ b/yggdrasil.go @@ -40,6 +40,7 @@ type nodeConfig struct { LinkLocal string IfName string IfTAPMode bool + IfMTU int } type node struct { @@ -114,6 +115,7 @@ func generateConfig() *nodeConfig { cfg.Multicast = true cfg.LinkLocal = "" cfg.IfName = "auto" + cfg.IfMTU = 65535 if runtime.GOOS == "windows" { cfg.IfTAPMode = true } else { @@ -264,7 +266,7 @@ func main() { n.init(cfg, logger) logger.Println("Starting tun...") //n.core.DEBUG_startTun(cfg.IfName) // 1280, the smallest supported MTU - n.core.DEBUG_startTunWithMTU(cfg.IfName, cfg.IfTAPMode, 65535) // Largest supported MTU + n.core.DEBUG_startTunWithMTU(cfg.IfName, cfg.IfTAPMode, cfg.IfMTU) // Largest supported MTU defer func() { logger.Println("Closing...") n.core.DEBUG_stopTun() From 844212e95b2956049846052aeca651d73d316e48 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 14 Feb 2018 14:08:40 +0000 Subject: [PATCH 5/5] Highlight packets that exceed the agreed session MTU --- src/yggdrasil/router.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/yggdrasil/router.go b/src/yggdrasil/router.go index 15e2ff3..e0884ef 100644 --- a/src/yggdrasil/router.go +++ b/src/yggdrasil/router.go @@ -145,6 +145,10 @@ func (r *router) sendPacket(bs []byte) { fallthrough //default: go func() { sinfo.send<-bs }() default: + if len(bs) > int(sinfo.getMTU()) { + // TODO: Send ICMPv6 Packet Too Big back to the TUN/TAP adapter + sinfo.core.log.Printf("Packet length %d exceeds session MTU %d", len(bs), sinfo.getMTU()) + } select { case sinfo.send <- bs: default: