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

Merge pull request #13 from neilalexander/exchangemtu

Include endpoint MTU in session pings
This commit is contained in:
Arceliar 2018-02-14 11:16:09 -06:00 committed by GitHub
commit f6bdbda5c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 1 deletions

View File

@ -223,6 +223,7 @@ func (a *admin) getData_getSessions() []admin_nodeInfo {
info := admin_nodeInfo{ info := admin_nodeInfo{
{"IP", net.IP(sinfo.theirAddr[:]).String()}, {"IP", net.IP(sinfo.theirAddr[:]).String()},
{"coords", fmt.Sprint(sinfo.coords)}, {"coords", fmt.Sprint(sinfo.coords)},
{"MTU", fmt.Sprint(sinfo.getMTU())},
} }
infos = append(infos, info) infos = append(infos, info)
} }

View File

@ -145,6 +145,10 @@ func (r *router) sendPacket(bs []byte) {
fallthrough fallthrough
//default: go func() { sinfo.send<-bs }() //default: go func() { sinfo.send<-bs }()
default: 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 { select {
case sinfo.send <- bs: case sinfo.send <- bs:
default: default:

View File

@ -19,6 +19,8 @@ type sessionInfo struct {
myHandle handle myHandle handle
theirNonce boxNonce theirNonce boxNonce
myNonce boxNonce myNonce boxNonce
theirMTU uint16
myMTU uint16
time time.Time // Time we last received a packet time time.Time // Time we last received a packet
coords []byte // coords of destination coords []byte // coords of destination
packet []byte // a buffered packet, sent immediately on ping/pong packet []byte // a buffered packet, sent immediately on ping/pong
@ -36,6 +38,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
} }
// Returns true if the session was updated, false otherwise // Returns true if the session was updated, false otherwise
@ -56,6 +59,9 @@ func (s *sessionInfo) update(p *sessionPing) bool {
s.theirNonce = boxNonce{} s.theirNonce = boxNonce{}
s.nonceMask = 0 s.nonceMask = 0
} }
if p.mtu >= 1280 {
s.theirMTU = p.mtu
}
s.coords = append([]byte{}, p.coords...) s.coords = append([]byte{}, p.coords...)
s.time = time.Now() s.time = time.Now()
s.tstamp = p.tstamp s.tstamp = p.tstamp
@ -144,6 +150,8 @@ func (ss *sessions) createSession(theirPermKey *boxPubKey) *sessionInfo {
sinfo.mySesPub = *pub sinfo.mySesPub = *pub
sinfo.mySesPriv = *priv sinfo.mySesPriv = *priv
sinfo.myNonce = *newBoxNonce() sinfo.myNonce = *newBoxNonce()
sinfo.theirMTU = 1280
sinfo.myMTU = uint16(ss.core.tun.mtu)
higher := false higher := false
for idx := range ss.core.boxPub { for idx := range ss.core.boxPub {
if ss.core.boxPub[idx] > sinfo.theirPermPub[idx] { if ss.core.boxPub[idx] > sinfo.theirPermPub[idx] {
@ -201,6 +209,7 @@ func (ss *sessions) getPing(sinfo *sessionInfo) sessionPing {
sendSesPub: sinfo.mySesPub, sendSesPub: sinfo.mySesPub,
tstamp: time.Now().Unix(), tstamp: time.Now().Unix(),
coords: coords, coords: coords,
mtu: sinfo.myMTU,
} }
sinfo.myNonce.update() sinfo.myNonce.update()
return ref return ref
@ -289,6 +298,13 @@ func (n *boxNonce) minus(m *boxNonce) int64 {
return diff 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 { func (sinfo *sessionInfo) nonceIsOK(theirNonce *boxNonce) bool {
// The bitmask is to allow for some non-duplicate out-of-order packets // The bitmask is to allow for some non-duplicate out-of-order packets
diff := theirNonce.minus(&sinfo.theirNonce) diff := theirNonce.minus(&sinfo.theirNonce)

View File

@ -429,12 +429,14 @@ func (p *sessionPing) encode() []byte {
bs = append(bs, wire_encode_uint64(wire_intToUint(p.tstamp))...) bs = append(bs, wire_encode_uint64(wire_intToUint(p.tstamp))...)
coords := wire_encode_coords(p.coords) coords := wire_encode_coords(p.coords)
bs = append(bs, coords...) bs = append(bs, coords...)
bs = append(bs, wire_encode_uint64(uint64(p.mtu))...)
return bs return bs
} }
func (p *sessionPing) decode(bs []byte) bool { func (p *sessionPing) decode(bs []byte) bool {
var pType uint64 var pType uint64
var tstamp uint64 var tstamp uint64
var mtu uint64
switch { switch {
case !wire_chop_uint64(&pType, &bs): case !wire_chop_uint64(&pType, &bs):
return false return false
@ -449,11 +451,14 @@ func (p *sessionPing) decode(bs []byte) bool {
return false return false
case !wire_chop_coords(&p.coords, &bs): case !wire_chop_coords(&p.coords, &bs):
return false return false
case !wire_chop_uint64(&mtu, &bs):
mtu = 1280
} }
p.tstamp = wire_intFromUint(tstamp) p.tstamp = wire_intFromUint(tstamp)
if pType == wire_SessionPong { if pType == wire_SessionPong {
p.isPong = true p.isPong = true
} }
p.mtu = uint16(mtu)
return true return true
} }

View File

@ -40,6 +40,7 @@ type nodeConfig struct {
LinkLocal string LinkLocal string
IfName string IfName string
IfTAPMode bool IfTAPMode bool
IfMTU int
} }
type node struct { type node struct {
@ -114,6 +115,7 @@ func generateConfig() *nodeConfig {
cfg.Multicast = true cfg.Multicast = true
cfg.LinkLocal = "" cfg.LinkLocal = ""
cfg.IfName = "auto" cfg.IfName = "auto"
cfg.IfMTU = 65535
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
cfg.IfTAPMode = true cfg.IfTAPMode = true
} else { } else {
@ -264,7 +266,7 @@ func main() {
n.init(cfg, logger) n.init(cfg, logger)
logger.Println("Starting tun...") logger.Println("Starting tun...")
//n.core.DEBUG_startTun(cfg.IfName) // 1280, the smallest supported MTU //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() { defer func() {
logger.Println("Closing...") logger.Println("Closing...")
n.core.DEBUG_stopTun() n.core.DEBUG_stopTun()