Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two Handhake initiation releated race-conditions fixed #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions device/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (peer *Peer) SendKeepalive() {

func (peer *Peer) SendHandshakeInitiation(isRetry bool) error {
if !isRetry {
atomic.StoreUint32(&peer.timers.handshakeAttempts, 0)
atomic.StoreUint32(&peer.timers.handshakeAttempts, 1)
}

peer.handshake.mutex.RLock()
Expand Down Expand Up @@ -193,7 +193,10 @@ func (peer *Peer) keepKeyFreshSending() {
}
nonce := atomic.LoadUint64(&keypair.sendNonce)
if nonce > RekeyAfterMessages || (keypair.isInitiator && time.Since(keypair.created) > RekeyAfterTime) {
peer.SendHandshakeInitiation(false)
// Do not send new HandshakeInitiation, if handshake retry is already in progress
if !peer.handshakeInProgress() {
peer.SendHandshakeInitiation(false)
}
}
}

Expand Down Expand Up @@ -298,7 +301,10 @@ top:

keypair := peer.keypairs.Current()
if keypair == nil || atomic.LoadUint64(&keypair.sendNonce) >= RejectAfterMessages || time.Since(keypair.created) >= RejectAfterTime {
peer.SendHandshakeInitiation(false)
// Do not send new HandshakeInitiation, if handshake retry is already in progress
if !peer.handshakeInProgress() {
peer.SendHandshakeInitiation(false)
}
return
}

Expand Down
15 changes: 13 additions & 2 deletions device/timers.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func expiredRetransmitHandshake(peer *Peer) {
if atomic.LoadUint32(&peer.timers.handshakeAttempts) > MaxTimerHandshakes {
peer.device.log.Verbosef("%s - Handshake did not complete after %d attempts, giving up", peer, MaxTimerHandshakes+2)

// Reset attempts count
atomic.StoreUint32(&peer.timers.handshakeAttempts, 0)

if peer.timersActive() {
peer.timers.sendKeepalive.Del()
}
Expand All @@ -95,7 +98,7 @@ func expiredRetransmitHandshake(peer *Peer) {
}
} else {
atomic.AddUint32(&peer.timers.handshakeAttempts, 1)
peer.device.log.Verbosef("%s - Handshake did not complete after %d seconds, retrying (try %d)", peer, int(RekeyTimeout.Seconds()), atomic.LoadUint32(&peer.timers.handshakeAttempts)+1)
peer.device.log.Verbosef("%s - Handshake did not complete after %d seconds, retrying (try %d)", peer, int(RekeyTimeout.Seconds()), atomic.LoadUint32(&peer.timers.handshakeAttempts))

/* We clear the endpoint address src address, in case this is the cause of trouble. */
peer.Lock()
Expand Down Expand Up @@ -141,10 +144,18 @@ func expiredPersistentKeepalive(peer *Peer) {
}
}

/* Returns true if HandshakeInitiation is already started (and not yet finished) */
func (peer *Peer) handshakeInProgress() bool {
return atomic.LoadUint32(&peer.timers.handshakeAttempts) > 0
}

/* Should be called after an authenticated data packet is sent. */
func (peer *Peer) timersDataSent() {
if peer.timersActive() && !peer.timers.newHandshake.IsPending() {
peer.timers.newHandshake.Mod(KeepaliveTimeout + RekeyTimeout + time.Millisecond*time.Duration(rand.Int31n(RekeyTimeoutJitterMaxMs)))
// Do not restart handshake because of staled data traffic, if it is already in progress
if !peer.handshakeInProgress() {
peer.timers.newHandshake.Mod(KeepaliveTimeout + RekeyTimeout + time.Millisecond*time.Duration(rand.Int31n(RekeyTimeoutJitterMaxMs)))
}
}
}

Expand Down