EtherGuard-VPN/device/timers.go

229 lines
6.9 KiB
Go
Raw Normal View History

2019-01-02 01:55:51 +01:00
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
*
* This is based heavily on timers.c from the kernel implementation.
*/
2019-03-03 04:04:41 +01:00
package device
2018-02-11 19:02:50 +01:00
import (
"math/rand"
2018-05-15 18:38:18 +02:00
"sync"
2018-02-11 19:02:50 +01:00
"sync/atomic"
"time"
)
// A Timer manages time-based aspects of the WireGuard protocol.
// Timer roughly copies the interface of the Linux kernel's struct timer_list.
type Timer struct {
*time.Timer
2018-05-20 03:31:27 +02:00
modifyingLock sync.RWMutex
2018-05-15 18:38:18 +02:00
runningLock sync.Mutex
isPending bool
2018-02-11 19:02:50 +01:00
}
func (peer *Peer) NewTimer(expirationFunction func(*Peer)) *Timer {
timer := &Timer{}
timer.Timer = time.AfterFunc(time.Hour, func() {
2018-05-15 18:38:18 +02:00
timer.runningLock.Lock()
defer timer.runningLock.Unlock()
2018-05-15 18:38:18 +02:00
timer.modifyingLock.Lock()
if !timer.isPending {
timer.modifyingLock.Unlock()
return
}
timer.isPending = false
2018-05-15 18:38:18 +02:00
timer.modifyingLock.Unlock()
expirationFunction(peer)
})
timer.Stop()
return timer
2018-02-11 19:02:50 +01:00
}
func (timer *Timer) Mod(d time.Duration) {
2018-05-15 18:38:18 +02:00
timer.modifyingLock.Lock()
timer.isPending = true
timer.Reset(d)
2018-05-15 18:38:18 +02:00
timer.modifyingLock.Unlock()
2018-02-11 19:02:50 +01:00
}
func (timer *Timer) Del() {
2018-05-15 18:38:18 +02:00
timer.modifyingLock.Lock()
timer.isPending = false
timer.Stop()
2018-05-15 18:38:18 +02:00
timer.modifyingLock.Unlock()
}
func (timer *Timer) DelSync() {
timer.Del()
timer.runningLock.Lock()
timer.Del()
timer.runningLock.Unlock()
2018-02-11 19:02:50 +01:00
}
2018-05-20 03:31:27 +02:00
func (timer *Timer) IsPending() bool {
timer.modifyingLock.RLock()
defer timer.modifyingLock.RUnlock()
return timer.isPending
}
func (peer *Peer) timersActive() bool {
return peer.isRunning.Get() && peer.device != nil && peer.device.isUp() && !peer.device.peers.empty.Get()
}
2018-02-11 19:02:50 +01:00
func expiredRetransmitHandshake(peer *Peer) {
2018-05-20 06:50:07 +02:00
if atomic.LoadUint32(&peer.timers.handshakeAttempts) > MaxTimerHandshakes {
peer.device.log.Verbosef("%s - Handshake did not complete after %d attempts, giving up", peer, MaxTimerHandshakes+2)
2018-02-11 19:02:50 +01:00
if peer.timersActive() {
peer.timers.sendKeepalive.Del()
}
2018-02-11 19:02:50 +01:00
/* We drop all packets without a keypair and don't try again,
* if we try unsuccessfully for too long to make a handshake.
*/
peer.FlushStagedPackets()
2018-02-11 19:02:50 +01:00
/* We set a timer for destroying any residue that might be left
* of a partial exchange.
*/
2018-05-20 03:31:27 +02:00
if peer.timersActive() && !peer.timers.zeroKeyMaterial.IsPending() {
peer.timers.zeroKeyMaterial.Mod(RejectAfterTime * 3)
}
} else {
2018-05-20 06:50:07 +02:00
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)
/* We clear the endpoint address src address, in case this is the cause of trouble. */
peer.Lock()
if peer.endpoint != nil {
peer.endpoint.ClearSrc()
}
peer.Unlock()
2018-02-11 19:02:50 +01:00
peer.SendHandshakeInitiation(true)
}
2018-02-11 19:02:50 +01:00
}
func expiredSendKeepalive(peer *Peer) {
peer.SendKeepalive()
2018-05-20 06:50:07 +02:00
if peer.timers.needAnotherKeepalive.Get() {
peer.timers.needAnotherKeepalive.Set(false)
if peer.timersActive() {
peer.timers.sendKeepalive.Mod(KeepaliveTimeout)
}
}
}
func expiredNewHandshake(peer *Peer) {
peer.device.log.Verbosef("%s - Retrying handshake because we stopped hearing back after %d seconds", peer, int((KeepaliveTimeout + RekeyTimeout).Seconds()))
/* We clear the endpoint address src address, in case this is the cause of trouble. */
peer.Lock()
if peer.endpoint != nil {
peer.endpoint.ClearSrc()
2018-02-11 19:02:50 +01:00
}
peer.Unlock()
peer.SendHandshakeInitiation(false)
2018-02-11 19:02:50 +01:00
}
2018-05-05 04:15:07 +02:00
func expiredZeroKeyMaterial(peer *Peer) {
peer.device.log.Verbosef("%s - Removing all keys, since we haven't received a new one in %d seconds", peer, int((RejectAfterTime * 3).Seconds()))
2018-05-13 23:14:43 +02:00
peer.ZeroAndFlushAll()
}
2018-02-11 19:02:50 +01:00
func expiredPersistentKeepalive(peer *Peer) {
if atomic.LoadUint32(&peer.persistentKeepaliveInterval) > 0 {
peer.SendKeepalive()
}
}
/* Should be called after an authenticated data packet is sent. */
func (peer *Peer) timersDataSent() {
2018-05-20 03:31:27 +02:00
if peer.timersActive() && !peer.timers.newHandshake.IsPending() {
peer.timers.newHandshake.Mod(KeepaliveTimeout + RekeyTimeout + time.Millisecond*time.Duration(rand.Int31n(RekeyTimeoutJitterMaxMs)))
}
}
2018-02-11 19:02:50 +01:00
/* Should be called after an authenticated data packet is received. */
func (peer *Peer) timersDataReceived() {
if peer.timersActive() {
2018-05-20 03:31:27 +02:00
if !peer.timers.sendKeepalive.IsPending() {
peer.timers.sendKeepalive.Mod(KeepaliveTimeout)
} else {
2018-05-20 06:50:07 +02:00
peer.timers.needAnotherKeepalive.Set(true)
}
}
}
2018-05-05 04:15:07 +02:00
/* Should be called after any type of authenticated packet is sent -- keepalive, data, or handshake. */
func (peer *Peer) timersAnyAuthenticatedPacketSent() {
if peer.timersActive() {
peer.timers.sendKeepalive.Del()
}
}
/* Should be called after any type of authenticated packet is received -- keepalive, data, or handshake. */
func (peer *Peer) timersAnyAuthenticatedPacketReceived() {
if peer.timersActive() {
peer.timers.newHandshake.Del()
}
}
2018-05-05 04:15:07 +02:00
/* Should be called after a handshake initiation message is sent. */
func (peer *Peer) timersHandshakeInitiated() {
if peer.timersActive() {
peer.timers.retransmitHandshake.Mod(RekeyTimeout + time.Millisecond*time.Duration(rand.Int31n(RekeyTimeoutJitterMaxMs)))
}
}
2018-02-11 19:02:50 +01:00
/* Should be called after a handshake response message is received and processed or when getting key confirmation via the first data message. */
func (peer *Peer) timersHandshakeComplete() {
if peer.timersActive() {
peer.timers.retransmitHandshake.Del()
}
2018-05-20 06:50:07 +02:00
atomic.StoreUint32(&peer.timers.handshakeAttempts, 0)
peer.timers.sentLastMinuteHandshake.Set(false)
atomic.StoreInt64(&peer.stats.lastHandshakeNano, time.Now().UnixNano())
}
2018-02-11 19:02:50 +01:00
/* Should be called after an ephemeral key is created, which is before sending a handshake response or after receiving a handshake response. */
func (peer *Peer) timersSessionDerived() {
if peer.timersActive() {
peer.timers.zeroKeyMaterial.Mod(RejectAfterTime * 3)
}
}
2018-02-11 19:02:50 +01:00
/* Should be called before a packet with authentication -- keepalive, data, or handshake -- is sent, or after one is received. */
func (peer *Peer) timersAnyAuthenticatedPacketTraversal() {
keepalive := atomic.LoadUint32(&peer.persistentKeepaliveInterval)
if keepalive > 0 && peer.timersActive() {
peer.timers.persistentKeepalive.Mod(time.Duration(keepalive) * time.Second)
}
}
2018-02-11 19:02:50 +01:00
func (peer *Peer) timersInit() {
peer.timers.retransmitHandshake = peer.NewTimer(expiredRetransmitHandshake)
peer.timers.sendKeepalive = peer.NewTimer(expiredSendKeepalive)
peer.timers.newHandshake = peer.NewTimer(expiredNewHandshake)
peer.timers.zeroKeyMaterial = peer.NewTimer(expiredZeroKeyMaterial)
peer.timers.persistentKeepalive = peer.NewTimer(expiredPersistentKeepalive)
}
func (peer *Peer) timersStart() {
2018-05-20 06:50:07 +02:00
atomic.StoreUint32(&peer.timers.handshakeAttempts, 0)
peer.timers.sentLastMinuteHandshake.Set(false)
peer.timers.needAnotherKeepalive.Set(false)
}
2018-05-05 04:15:07 +02:00
func (peer *Peer) timersStop() {
2018-05-15 18:38:18 +02:00
peer.timers.retransmitHandshake.DelSync()
peer.timers.sendKeepalive.DelSync()
peer.timers.newHandshake.DelSync()
peer.timers.zeroKeyMaterial.DelSync()
peer.timers.persistentKeepalive.DelSync()
2018-02-11 19:02:50 +01:00
}