mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-30 00:51:36 +00:00
keep track of recent nonces with a heap and a map instead of a fixed-size bitmask
This commit is contained in:
parent
fdac8932a8
commit
03b8af9f1a
@ -6,6 +6,7 @@ package yggdrasil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"container/heap"
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -15,6 +16,25 @@ import (
|
|||||||
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Duration that we keep track of old nonces per session, to allow some out-of-order packet delivery
|
||||||
|
const nonceWindow = time.Second
|
||||||
|
|
||||||
|
// A heap of nonces, used with a map[nonce]time to allow out-of-order packets a little time to arrive without rejecting them
|
||||||
|
// Less is backwards so the oldest node is the highest priority for Pop
|
||||||
|
type nonceHeap []crypto.BoxNonce
|
||||||
|
|
||||||
|
func (h nonceHeap) Len() int { return len(h) }
|
||||||
|
func (h nonceHeap) Less(i, j int) bool { return h[i].Minus(&h[j]) > 0 }
|
||||||
|
func (h nonceHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
|
||||||
|
func (h *nonceHeap) Push(x interface{}) { *h = append(*h, x.(crypto.BoxNonce)) }
|
||||||
|
func (h *nonceHeap) Pop() interface{} {
|
||||||
|
l := len(*h)
|
||||||
|
var n crypto.BoxNonce
|
||||||
|
n, *h = (*h)[l-1], (*h)[:l-1]
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
func (h nonceHeap) peek() *crypto.BoxNonce { return &h[len(h)-1] }
|
||||||
|
|
||||||
// All the information we know about an active session.
|
// All the information we know about an active session.
|
||||||
// This includes coords, permanent and ephemeral keys, handles and nonces, various sorts of timing information for timeout and maintenance, and some metadata for the admin API.
|
// This includes coords, permanent and ephemeral keys, handles and nonces, various sorts of timing information for timeout and maintenance, and some metadata for the admin API.
|
||||||
type sessionInfo struct {
|
type sessionInfo struct {
|
||||||
@ -31,7 +51,8 @@ type sessionInfo struct {
|
|||||||
theirHandle crypto.Handle //
|
theirHandle crypto.Handle //
|
||||||
myHandle crypto.Handle //
|
myHandle crypto.Handle //
|
||||||
theirNonce crypto.BoxNonce //
|
theirNonce crypto.BoxNonce //
|
||||||
theirNonceMask uint64 //
|
theirNonceHeap nonceHeap // priority queue to keep track of the lowest nonce we recently accepted
|
||||||
|
theirNonceMap map[crypto.BoxNonce]time.Time // time we added each nonce to the heap
|
||||||
myNonce crypto.BoxNonce //
|
myNonce crypto.BoxNonce //
|
||||||
theirMTU uint16 //
|
theirMTU uint16 //
|
||||||
myMTU uint16 //
|
myMTU uint16 //
|
||||||
@ -87,7 +108,8 @@ func (s *sessionInfo) update(p *sessionPing) bool {
|
|||||||
s.theirHandle = p.Handle
|
s.theirHandle = p.Handle
|
||||||
s.sharedSesKey = *crypto.GetSharedKey(&s.mySesPriv, &s.theirSesPub)
|
s.sharedSesKey = *crypto.GetSharedKey(&s.mySesPriv, &s.theirSesPub)
|
||||||
s.theirNonce = crypto.BoxNonce{}
|
s.theirNonce = crypto.BoxNonce{}
|
||||||
s.theirNonceMask = 0
|
s.theirNonceHeap = nil
|
||||||
|
s.theirNonceMap = make(map[crypto.BoxNonce]time.Time)
|
||||||
}
|
}
|
||||||
if p.MTU >= 1280 || p.MTU == 0 {
|
if p.MTU >= 1280 || p.MTU == 0 {
|
||||||
s.theirMTU = p.MTU
|
s.theirMTU = p.MTU
|
||||||
@ -400,27 +422,40 @@ func (sinfo *sessionInfo) getMTU() uint16 {
|
|||||||
// Checks if a packet's nonce is recent enough to fall within the window of allowed packets, and not already received.
|
// Checks if a packet's nonce is recent enough to fall within the window of allowed packets, and not already received.
|
||||||
func (sinfo *sessionInfo) nonceIsOK(theirNonce *crypto.BoxNonce) bool {
|
func (sinfo *sessionInfo) nonceIsOK(theirNonce *crypto.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)
|
if theirNonce.Minus(&sinfo.theirNonce) > 0 {
|
||||||
if diff > 0 {
|
// This is newer than the newest nonce we've seen
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return ^sinfo.theirNonceMask&(0x01<<uint64(-diff)) != 0
|
if len(sinfo.theirNonceHeap) > 0 {
|
||||||
|
if theirNonce.Minus(sinfo.theirNonceHeap.peek()) > 0 {
|
||||||
|
if _, isIn := sinfo.theirNonceMap[*theirNonce]; !isIn {
|
||||||
|
// This nonce is recent enough that we keep track of older nonces, but it's not one we've seen yet
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates the nonce mask by (possibly) shifting the bitmask and setting the bit corresponding to this nonce to 1, and then updating the most recent nonce
|
// Updates the nonce mask by (possibly) shifting the bitmask and setting the bit corresponding to this nonce to 1, and then updating the most recent nonce
|
||||||
func (sinfo *sessionInfo) updateNonce(theirNonce *crypto.BoxNonce) {
|
func (sinfo *sessionInfo) updateNonce(theirNonce *crypto.BoxNonce) {
|
||||||
// Shift nonce mask if needed
|
// Start with some cleanup
|
||||||
// Set bit
|
for len(sinfo.theirNonceHeap) > 64 {
|
||||||
diff := theirNonce.Minus(&sinfo.theirNonce)
|
if time.Since(sinfo.theirNonceMap[*sinfo.theirNonceHeap.peek()]) < nonceWindow {
|
||||||
if diff > 0 {
|
// This nonce is still fairly new, so keep it around
|
||||||
// This nonce is newer, so shift the window before setting the bit, and update theirNonce in the session info.
|
break
|
||||||
sinfo.theirNonceMask <<= uint64(diff)
|
|
||||||
sinfo.theirNonceMask &= 0x01
|
|
||||||
sinfo.theirNonce = *theirNonce
|
|
||||||
} else {
|
|
||||||
// This nonce is older, so set the bit but do not shift the window.
|
|
||||||
sinfo.theirNonceMask &= 0x01 << uint64(-diff)
|
|
||||||
}
|
}
|
||||||
|
// TODO? reallocate the map in some cases, to free unused map space?
|
||||||
|
delete(sinfo.theirNonceMap, *sinfo.theirNonceHeap.peek())
|
||||||
|
heap.Pop(&sinfo.theirNonceHeap)
|
||||||
|
}
|
||||||
|
if theirNonce.Minus(&sinfo.theirNonce) > 0 {
|
||||||
|
// This nonce is the newest we've seen, so make a note of that
|
||||||
|
sinfo.theirNonce = *theirNonce
|
||||||
|
}
|
||||||
|
// Add it to the heap/map so we know not to allow it again
|
||||||
|
heap.Push(&sinfo.theirNonceHeap, *theirNonce)
|
||||||
|
sinfo.theirNonceMap[*theirNonce] = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resets all sessions to an uninitialized state.
|
// Resets all sessions to an uninitialized state.
|
||||||
|
Loading…
Reference in New Issue
Block a user