2018-05-03 15:04:00 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2018-02-11 19:02:50 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"math/rand"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* NOTE:
|
|
|
|
* Notion of validity
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Called when a new authenticated message has been send
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
func (peer *Peer) KeepKeyFreshSending() {
|
|
|
|
kp := peer.keyPairs.Current()
|
|
|
|
if kp == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nonce := atomic.LoadUint64(&kp.sendNonce)
|
|
|
|
if nonce > RekeyAfterMessages {
|
2018-05-05 04:15:07 +02:00
|
|
|
peer.event.handshakeBegin.Fire()
|
2018-02-11 19:02:50 +01:00
|
|
|
}
|
|
|
|
if kp.isInitiator && time.Now().Sub(kp.created) > RekeyAfterTime {
|
2018-05-05 04:15:07 +02:00
|
|
|
peer.event.handshakeBegin.Fire()
|
2018-02-11 19:02:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called when a new authenticated message has been received
|
|
|
|
*
|
|
|
|
* NOTE: Not thread safe, but called by sequential receiver!
|
|
|
|
*/
|
|
|
|
func (peer *Peer) KeepKeyFreshReceiving() {
|
2018-02-11 23:26:54 +01:00
|
|
|
if peer.timer.sendLastMinuteHandshake.Get() {
|
2018-02-11 19:02:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
kp := peer.keyPairs.Current()
|
|
|
|
if kp == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !kp.isInitiator {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nonce := atomic.LoadUint64(&kp.sendNonce)
|
|
|
|
send := nonce > RekeyAfterMessages || time.Now().Sub(kp.created) > RekeyAfterTimeReceiving
|
|
|
|
if send {
|
|
|
|
// do a last minute attempt at initiating a new handshake
|
2018-02-11 23:26:54 +01:00
|
|
|
peer.timer.sendLastMinuteHandshake.Set(true)
|
2018-05-05 04:15:07 +02:00
|
|
|
peer.event.handshakeBegin.Fire()
|
2018-02-11 19:02:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Queues a keep-alive if no packets are queued for peer
|
|
|
|
*/
|
|
|
|
func (peer *Peer) SendKeepAlive() bool {
|
|
|
|
if len(peer.queue.nonce) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
elem := peer.device.NewOutboundElement()
|
|
|
|
elem.packet = nil
|
|
|
|
select {
|
|
|
|
case peer.queue.nonce <- elem:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called after successfully completing a handshake.
|
|
|
|
* i.e. after:
|
|
|
|
*
|
|
|
|
* - Valid handshake response
|
|
|
|
* - First transport message under the "next" key
|
|
|
|
*/
|
2018-05-05 04:15:07 +02:00
|
|
|
// peer.device.log.Info.Println(peer, ": New handshake completed")
|
2018-02-11 19:02:50 +01:00
|
|
|
|
|
|
|
/* Event:
|
|
|
|
* An ephemeral key is generated
|
|
|
|
*
|
|
|
|
* i.e. after:
|
|
|
|
*
|
|
|
|
* CreateMessageInitiation
|
|
|
|
* CreateMessageResponse
|
|
|
|
*
|
|
|
|
* Action:
|
|
|
|
* Schedule the deletion of all key material
|
|
|
|
* upon failure to complete a handshake
|
|
|
|
*/
|
|
|
|
func (peer *Peer) TimerEphemeralKeyCreated() {
|
2018-05-05 02:20:52 +02:00
|
|
|
peer.event.ephemeralKeyCreated.Fire()
|
|
|
|
// peer.timer.zeroAllKeys.Reset(RejectAfterTime * 3)
|
2018-02-11 19:02:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sends a new handshake initiation message to the peer (endpoint)
|
|
|
|
*/
|
|
|
|
func (peer *Peer) sendNewHandshake() error {
|
|
|
|
|
|
|
|
// create initiation message
|
|
|
|
|
|
|
|
msg, err := peer.device.CreateMessageInitiation(peer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal handshake message
|
|
|
|
|
|
|
|
var buff [MessageInitiationSize]byte
|
|
|
|
writer := bytes.NewBuffer(buff[:0])
|
|
|
|
binary.Write(writer, binary.LittleEndian, msg)
|
|
|
|
packet := writer.Bytes()
|
|
|
|
peer.mac.AddMacs(packet)
|
|
|
|
|
|
|
|
// send to endpoint
|
|
|
|
|
2018-05-05 04:15:07 +02:00
|
|
|
peer.event.anyAuthenticatedPacketTraversal.Fire()
|
2018-02-11 19:02:50 +01:00
|
|
|
|
2018-05-05 04:15:07 +02:00
|
|
|
return peer.SendBuffer(packet)
|
2018-02-11 19:02:50 +01:00
|
|
|
}
|
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
func newTimer() *time.Timer {
|
|
|
|
timer := time.NewTimer(time.Hour)
|
|
|
|
timer.Stop()
|
|
|
|
return timer
|
|
|
|
}
|
|
|
|
|
2018-02-11 19:02:50 +01:00
|
|
|
func (peer *Peer) RoutineTimerHandler() {
|
|
|
|
|
|
|
|
device := peer.device
|
|
|
|
|
|
|
|
logInfo := device.log.Info
|
|
|
|
logDebug := device.log.Debug
|
2018-04-18 20:29:48 +02:00
|
|
|
|
|
|
|
defer func() {
|
2018-05-05 02:20:52 +02:00
|
|
|
logDebug.Println(peer, ": Routine: timer handler - stopped")
|
2018-04-18 20:29:48 +02:00
|
|
|
peer.routines.stopping.Done()
|
|
|
|
}()
|
2018-02-11 19:02:50 +01:00
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
logDebug.Println(peer, ": Routine: timer handler - started")
|
2018-05-01 16:59:13 +02:00
|
|
|
|
2018-02-11 19:02:50 +01:00
|
|
|
// reset all timers
|
|
|
|
|
2018-05-05 04:15:07 +02:00
|
|
|
enableHandshake := true
|
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
pendingHandshakeNew := false
|
|
|
|
pendingKeepalivePassive := false
|
|
|
|
|
|
|
|
timerKeepalivePassive := newTimer()
|
|
|
|
timerHandshakeDeadline := newTimer()
|
|
|
|
timerHandshakeTimeout := newTimer()
|
|
|
|
timerHandshakeNew := newTimer()
|
|
|
|
timerZeroAllKeys := newTimer()
|
|
|
|
timerKeepalivePersistent := newTimer()
|
2018-02-11 19:02:50 +01:00
|
|
|
|
2018-04-18 07:24:33 +02:00
|
|
|
interval := peer.persistentKeepaliveInterval
|
2018-02-11 19:02:50 +01:00
|
|
|
if interval > 0 {
|
|
|
|
duration := time.Duration(interval) * time.Second
|
2018-05-05 02:20:52 +02:00
|
|
|
timerKeepalivePersistent.Reset(duration)
|
2018-02-11 19:02:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// signal synchronised setup complete
|
|
|
|
|
|
|
|
peer.routines.starting.Done()
|
|
|
|
|
|
|
|
// handle timer events
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
/* stopping */
|
|
|
|
|
|
|
|
case <-peer.routines.stop.Wait():
|
|
|
|
return
|
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
/* events */
|
|
|
|
|
|
|
|
case <-peer.event.dataSent.C:
|
|
|
|
timerKeepalivePassive.Stop()
|
|
|
|
if !pendingHandshakeNew {
|
|
|
|
timerHandshakeNew.Reset(NewHandshakeTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-peer.event.dataReceived.C:
|
|
|
|
if pendingKeepalivePassive {
|
|
|
|
peer.timer.needAnotherKeepalive.Set(true) // TODO: make local
|
|
|
|
} else {
|
|
|
|
timerKeepalivePassive.Reset(KeepaliveTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-peer.event.anyAuthenticatedPacketTraversal.C:
|
|
|
|
interval := peer.persistentKeepaliveInterval
|
|
|
|
if interval > 0 {
|
|
|
|
duration := time.Duration(interval) * time.Second
|
|
|
|
timerKeepalivePersistent.Reset(duration)
|
|
|
|
}
|
|
|
|
|
2018-05-05 04:42:17 +02:00
|
|
|
case <-peer.event.handshakeBegin.C:
|
|
|
|
|
|
|
|
if !enableHandshake {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logDebug.Println(peer, ": Event, Handshake Begin")
|
|
|
|
|
|
|
|
err := peer.sendNewHandshake()
|
|
|
|
|
|
|
|
// set timeout
|
|
|
|
|
|
|
|
jitter := time.Millisecond * time.Duration(rand.Int31n(334))
|
|
|
|
timerKeepalivePassive.Stop()
|
|
|
|
timerHandshakeTimeout.Reset(RekeyTimeout + jitter)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logInfo.Println(peer, ": Failed to send handshake initiation", err)
|
|
|
|
} else {
|
|
|
|
logDebug.Println(peer, ": Send handshake initiation (initial)")
|
|
|
|
}
|
|
|
|
|
|
|
|
timerHandshakeDeadline.Reset(RekeyAttemptTime)
|
|
|
|
|
|
|
|
// disable further handshakes
|
|
|
|
|
|
|
|
peer.event.handshakeBegin.Clear()
|
|
|
|
enableHandshake = false
|
|
|
|
|
|
|
|
case <-peer.event.handshakeCompleted.C:
|
|
|
|
|
|
|
|
logInfo.Println(peer, ": Handshake completed")
|
|
|
|
|
|
|
|
atomic.StoreInt64(
|
|
|
|
&peer.stats.lastHandshakeNano,
|
|
|
|
time.Now().UnixNano(),
|
|
|
|
)
|
|
|
|
|
|
|
|
timerHandshakeTimeout.Stop()
|
|
|
|
timerHandshakeDeadline.Stop()
|
|
|
|
peer.timer.sendLastMinuteHandshake.Set(false)
|
|
|
|
|
|
|
|
// allow further handshakes
|
|
|
|
|
|
|
|
peer.event.handshakeBegin.Clear()
|
|
|
|
enableHandshake = true
|
|
|
|
|
2018-02-11 19:02:50 +01:00
|
|
|
/* timers */
|
|
|
|
|
|
|
|
// keep-alive
|
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
case <-timerKeepalivePersistent.C:
|
2018-02-11 19:02:50 +01:00
|
|
|
|
2018-04-18 07:24:33 +02:00
|
|
|
interval := peer.persistentKeepaliveInterval
|
2018-02-11 19:02:50 +01:00
|
|
|
if interval > 0 {
|
2018-05-05 02:20:52 +02:00
|
|
|
logDebug.Println(peer, ": Send keep-alive (persistent)")
|
|
|
|
timerKeepalivePassive.Stop()
|
2018-02-11 19:02:50 +01:00
|
|
|
peer.SendKeepAlive()
|
|
|
|
}
|
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
case <-timerKeepalivePassive.C:
|
2018-02-11 19:02:50 +01:00
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
logDebug.Println(peer, ": Send keep-alive (passive)")
|
2018-02-11 19:02:50 +01:00
|
|
|
|
|
|
|
peer.SendKeepAlive()
|
|
|
|
|
2018-02-11 23:26:54 +01:00
|
|
|
if peer.timer.needAnotherKeepalive.Swap(false) {
|
2018-05-05 02:20:52 +02:00
|
|
|
timerKeepalivePassive.Reset(KeepaliveTimeout)
|
2018-02-11 19:02:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear key material timer
|
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
case <-timerZeroAllKeys.C:
|
2018-02-11 19:02:50 +01:00
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
logDebug.Println(peer, ": Clear all key-material (timer event)")
|
2018-02-11 19:02:50 +01:00
|
|
|
|
|
|
|
hs := &peer.handshake
|
|
|
|
hs.mutex.Lock()
|
|
|
|
|
|
|
|
kp := &peer.keyPairs
|
|
|
|
kp.mutex.Lock()
|
|
|
|
|
|
|
|
// remove key-pairs
|
|
|
|
|
|
|
|
if kp.previous != nil {
|
|
|
|
device.DeleteKeyPair(kp.previous)
|
|
|
|
kp.previous = nil
|
|
|
|
}
|
|
|
|
if kp.current != nil {
|
|
|
|
device.DeleteKeyPair(kp.current)
|
|
|
|
kp.current = nil
|
|
|
|
}
|
|
|
|
if kp.next != nil {
|
|
|
|
device.DeleteKeyPair(kp.next)
|
|
|
|
kp.next = nil
|
|
|
|
}
|
|
|
|
kp.mutex.Unlock()
|
|
|
|
|
|
|
|
// zero out handshake
|
|
|
|
|
|
|
|
device.indices.Delete(hs.localIndex)
|
|
|
|
hs.Clear()
|
|
|
|
hs.mutex.Unlock()
|
|
|
|
|
|
|
|
// handshake timers
|
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
case <-timerHandshakeTimeout.C:
|
2018-02-11 19:02:50 +01:00
|
|
|
|
2018-05-05 04:15:07 +02:00
|
|
|
// allow new handshake to be send
|
|
|
|
|
|
|
|
enableHandshake = true
|
|
|
|
|
2018-02-11 19:02:50 +01:00
|
|
|
// clear source (in case this is causing problems)
|
|
|
|
|
|
|
|
peer.mutex.Lock()
|
|
|
|
if peer.endpoint != nil {
|
|
|
|
peer.endpoint.ClearSrc()
|
|
|
|
}
|
|
|
|
peer.mutex.Unlock()
|
|
|
|
|
|
|
|
// send new handshake
|
|
|
|
|
|
|
|
err := peer.sendNewHandshake()
|
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
// set timeout
|
|
|
|
|
2018-05-05 04:42:17 +02:00
|
|
|
jitter := time.Millisecond * time.Duration(rand.Int31n(334))
|
2018-05-05 02:20:52 +02:00
|
|
|
timerKeepalivePassive.Stop()
|
|
|
|
timerHandshakeTimeout.Reset(RekeyTimeout + jitter)
|
|
|
|
|
2018-02-11 19:02:50 +01:00
|
|
|
if err != nil {
|
2018-05-05 02:20:52 +02:00
|
|
|
logInfo.Println(peer, ": Failed to send handshake initiation", err)
|
2018-02-11 19:02:50 +01:00
|
|
|
} else {
|
2018-05-05 02:20:52 +02:00
|
|
|
logDebug.Println(peer, ": Send handshake initiation (subsequent)")
|
2018-02-11 19:02:50 +01:00
|
|
|
}
|
|
|
|
|
2018-05-05 04:15:07 +02:00
|
|
|
// disable further handshakes
|
|
|
|
|
|
|
|
peer.event.handshakeBegin.Clear()
|
|
|
|
enableHandshake = false
|
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
case <-timerHandshakeDeadline.C:
|
2018-02-11 19:02:50 +01:00
|
|
|
|
|
|
|
// clear all queued packets and stop keep-alive
|
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
logInfo.Println(peer, ": Handshake negotiation timed-out")
|
2018-02-11 19:02:50 +01:00
|
|
|
|
2018-05-05 02:20:52 +02:00
|
|
|
peer.flushNonceQueue()
|
|
|
|
signalSend(peer.signal.flushNonceQueue)
|
|
|
|
timerKeepalivePersistent.Stop()
|
2018-02-11 19:02:50 +01:00
|
|
|
|
2018-05-05 04:15:07 +02:00
|
|
|
// disable further handshakes
|
|
|
|
|
|
|
|
peer.event.handshakeBegin.Clear()
|
|
|
|
enableHandshake = true
|
2018-02-11 19:02:50 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|