mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-10 08:40:28 +00:00
Merge pull request #82 from neilalexander/admin
Track TX/RX bytes over session and if MTU was adjusted, adjust MTU exchange behaviour, send session pings on TUN/TAP change
This commit is contained in:
commit
fdb826578f
@ -280,6 +280,17 @@ func (a *admin) startTunWithMTU(ifname string, iftapmode bool, ifmtu int) error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// If we have open sessions then we need to notify them
|
||||||
|
// that our MTU has now changed
|
||||||
|
for _, sinfo := range a.core.sessions.sinfos {
|
||||||
|
if ifname == "none" {
|
||||||
|
sinfo.myMTU = 0
|
||||||
|
} else {
|
||||||
|
sinfo.myMTU = uint16(ifmtu)
|
||||||
|
}
|
||||||
|
a.core.sessions.sendPingPong(sinfo, false)
|
||||||
|
}
|
||||||
|
// Aaaaand... go!
|
||||||
go a.core.tun.read()
|
go a.core.tun.read()
|
||||||
}
|
}
|
||||||
go a.core.tun.write()
|
go a.core.tun.write()
|
||||||
@ -374,6 +385,9 @@ func (a *admin) getData_getSessions() []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())},
|
{"MTU", fmt.Sprint(sinfo.getMTU())},
|
||||||
|
{"wasMTUFixed", fmt.Sprint(sinfo.wasMTUFixed)},
|
||||||
|
{"bytesSent", fmt.Sprint(sinfo.bytesSent)},
|
||||||
|
{"bytesRecvd", fmt.Sprint(sinfo.bytesRecvd)},
|
||||||
}
|
}
|
||||||
infos = append(infos, info)
|
infos = append(infos, info)
|
||||||
}
|
}
|
||||||
|
@ -164,6 +164,31 @@ func (r *router) sendPacket(bs []byte) {
|
|||||||
}
|
}
|
||||||
fallthrough // Also send the packet
|
fallthrough // Also send the packet
|
||||||
default:
|
default:
|
||||||
|
// Drop packets if the session MTU is 0 - this means that one or other
|
||||||
|
// side probably has their TUN adapter disabled
|
||||||
|
if sinfo.getMTU() == 0 {
|
||||||
|
// Get the size of the oversized payload, up to a max of 900 bytes
|
||||||
|
window := 900
|
||||||
|
if len(bs) < window {
|
||||||
|
window = len(bs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the Destination Unreachable response
|
||||||
|
ptb := &icmp.DstUnreach{
|
||||||
|
Data: bs[:window],
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the ICMPv6 response from it
|
||||||
|
icmpv6Buf, err := r.core.tun.icmpv6.create_icmpv6_tun(
|
||||||
|
bs[8:24], bs[24:40],
|
||||||
|
ipv6.ICMPTypeDestinationUnreachable, 1, ptb)
|
||||||
|
if err == nil {
|
||||||
|
r.recv <- icmpv6Buf
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't continue - drop the packet
|
||||||
|
return
|
||||||
|
}
|
||||||
// Generate an ICMPv6 Packet Too Big for packets larger than session MTU
|
// Generate an ICMPv6 Packet Too Big for packets larger than session MTU
|
||||||
if len(bs) > int(sinfo.getMTU()) {
|
if len(bs) > int(sinfo.getMTU()) {
|
||||||
// Get the size of the oversized payload, up to a max of 900 bytes
|
// Get the size of the oversized payload, up to a max of 900 bytes
|
||||||
|
@ -21,6 +21,7 @@ type sessionInfo struct {
|
|||||||
myNonce boxNonce
|
myNonce boxNonce
|
||||||
theirMTU uint16
|
theirMTU uint16
|
||||||
myMTU uint16
|
myMTU uint16
|
||||||
|
wasMTUFixed bool // Was the MTU fixed by a receive error?
|
||||||
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
|
||||||
@ -32,6 +33,8 @@ type sessionInfo struct {
|
|||||||
mtuTime time.Time // time myMTU was last changed
|
mtuTime time.Time // time myMTU was last changed
|
||||||
pingTime time.Time // time the first ping was sent since the last received packet
|
pingTime time.Time // time the first ping was sent since the last received packet
|
||||||
pingSend time.Time // time the last ping was sent
|
pingSend time.Time // time the last ping was sent
|
||||||
|
bytesSent uint64 // Bytes of real traffic sent in this session
|
||||||
|
bytesRecvd uint64 // Bytes of real traffic received in this session
|
||||||
}
|
}
|
||||||
|
|
||||||
type sessionPing struct {
|
type sessionPing struct {
|
||||||
@ -62,7 +65,7 @@ func (s *sessionInfo) update(p *sessionPing) bool {
|
|||||||
s.theirNonce = boxNonce{}
|
s.theirNonce = boxNonce{}
|
||||||
s.nonceMask = 0
|
s.nonceMask = 0
|
||||||
}
|
}
|
||||||
if p.mtu >= 1280 {
|
if p.mtu >= 1280 || p.mtu == 0 {
|
||||||
s.theirMTU = p.mtu
|
s.theirMTU = p.mtu
|
||||||
}
|
}
|
||||||
s.coords = append([]byte{}, p.coords...)
|
s.coords = append([]byte{}, p.coords...)
|
||||||
@ -310,6 +313,9 @@ func (n *boxNonce) minus(m *boxNonce) int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (sinfo *sessionInfo) getMTU() uint16 {
|
func (sinfo *sessionInfo) getMTU() uint16 {
|
||||||
|
if sinfo.theirMTU == 0 || sinfo.myMTU == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
if sinfo.theirMTU < sinfo.myMTU {
|
if sinfo.theirMTU < sinfo.myMTU {
|
||||||
return sinfo.theirMTU
|
return sinfo.theirMTU
|
||||||
}
|
}
|
||||||
@ -384,6 +390,7 @@ func (sinfo *sessionInfo) doSend(bs []byte) {
|
|||||||
payload: payload,
|
payload: payload,
|
||||||
}
|
}
|
||||||
packet := p.encode()
|
packet := p.encode()
|
||||||
|
sinfo.bytesSent += uint64(len(bs))
|
||||||
sinfo.core.router.out(packet)
|
sinfo.core.router.out(packet)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -411,6 +418,7 @@ func (sinfo *sessionInfo) doRecv(p *wire_trafficPacket) {
|
|||||||
//sinfo.core.log.Println("DEBUG set MTU to:", sinfo.myMTU)
|
//sinfo.core.log.Println("DEBUG set MTU to:", sinfo.myMTU)
|
||||||
sinfo.core.sessions.sendPingPong(sinfo, false)
|
sinfo.core.sessions.sendPingPong(sinfo, false)
|
||||||
sinfo.mtuTime = time.Now()
|
sinfo.mtuTime = time.Now()
|
||||||
|
sinfo.wasMTUFixed = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
go func() { sinfo.core.router.admin <- fixSessionMTU }()
|
go func() { sinfo.core.router.admin <- fixSessionMTU }()
|
||||||
@ -427,5 +435,6 @@ func (sinfo *sessionInfo) doRecv(p *wire_trafficPacket) {
|
|||||||
go func() { sinfo.core.router.admin <- fixSessionMTU }()
|
go func() { sinfo.core.router.admin <- fixSessionMTU }()
|
||||||
sinfo.updateNonce(&p.nonce)
|
sinfo.updateNonce(&p.nonce)
|
||||||
sinfo.time = time.Now()
|
sinfo.time = time.Now()
|
||||||
|
sinfo.bytesRecvd += uint64(len(bs))
|
||||||
sinfo.core.router.recvPacket(bs, &sinfo.theirAddr, &sinfo.theirSubnet)
|
sinfo.core.router.recvPacket(bs, &sinfo.theirAddr, &sinfo.theirSubnet)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user