summaryrefslogtreecommitdiffhomepage
path: root/src/timers.go
blob: 24544147e7730e2cbd1d27f4d8487e19b8a2d6b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main

import (
	"bytes"
	"encoding/binary"
	"golang.org/x/crypto/blake2s"
	"sync/atomic"
	"time"
)

/* Called when a new authenticated message has been send
 *
 */
func (peer *Peer) KeepKeyFreshSending() {
	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) > RekeyAfterTime
	if send {
		signalSend(peer.signal.handshakeBegin)
	}
}

/* Called when a new authenticated message has been recevied
 *
 */
func (peer *Peer) KeepKeyFreshReceiving() {
	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 {
		signalSend(peer.signal.handshakeBegin)
	}
}

/* Called after succesfully completing a handshake.
 * i.e. after:
 * - Valid handshake response
 * - First transport message under the "next" key
 */
func (peer *Peer) EventHandshakeComplete() {
	peer.device.log.Info.Println("Negotiated new handshake for", peer.String())
	peer.timer.zeroAllKeys.Reset(RejectAfterTime * 3)
	atomic.StoreInt64(
		&peer.stats.lastHandshakeNano,
		time.Now().UnixNano(),
	)
	signalSend(peer.signal.handshakeCompleted)
}

/* Queues a keep-alive if no packets are queued for peer
 */
func (peer *Peer) SendKeepAlive() bool {
	elem := peer.device.NewOutboundElement()
	elem.packet = nil
	if len(peer.queue.nonce) == 0 {
		select {
		case peer.queue.nonce <- elem:
			return true
		default:
			return false
		}
	}
	return true
}

/* Starts the "keep-alive" timer
 * (if not already running),
 * in response to incomming messages
 */
func (peer *Peer) TimerStartKeepalive() {

	// check if acknowledgement timer set yet

	var waiting int32 = AtomicTrue
	waiting = atomic.SwapInt32(&peer.flags.keepaliveWaiting, waiting)
	if waiting == AtomicTrue {
		return
	}

	// timer not yet set, start it

	wait := KeepaliveTimeout
	interval := atomic.LoadUint64(&peer.persistentKeepaliveInterval)
	if interval > 0 {
		duration := time.Duration(interval) * time.Second
		if duration < wait {
			wait = duration
		}
	}
}

/* Resets both keep-alive timers
 */
func (peer *Peer) TimerResetKeepalive() {

	// reset persistent timer

	interval := atomic.LoadUint64(&peer.persistentKeepaliveInterval)
	if interval > 0 {
		peer.timer.keepalivePersistent.Reset(
			time.Duration(interval) * time.Second,
		)
	}

	// stop acknowledgement timer

	timerStop(peer.timer.keepalivePassive)
	atomic.StoreInt32(&peer.flags.keepaliveWaiting, AtomicFalse)
}

func (peer *Peer) BeginHandshakeInitiation() (*QueueOutboundElement, error) {

	// create initiation

	elem := peer.device.NewOutboundElement()
	msg, err := peer.device.CreateMessageInitiation(peer)
	if err != nil {
		return nil, err
	}

	// marshal & schedule for sending

	writer := bytes.NewBuffer(elem.buffer[:0])
	binary.Write(writer, binary.LittleEndian, msg)
	elem.packet = writer.Bytes()
	peer.mac.AddMacs(elem.packet)
	addToOutboundQueue(peer.queue.outbound, elem)
	return elem, err
}

func (peer *Peer) RoutineTimerHandler() {
	device := peer.device
	indices := &device.indices

	logDebug := device.log.Debug
	logDebug.Println("Routine, timer handler, started for peer", peer.String())

	for {
		select {

		case <-peer.signal.stop:
			return

		// keep-alives

		case <-peer.timer.keepalivePersistent.C:

			logDebug.Println("Sending persistent keep-alive to", peer.String())

			peer.SendKeepAlive()
			peer.TimerResetKeepalive()

		case <-peer.timer.keepalivePassive.C:

			logDebug.Println("Sending passive persistent keep-alive to", peer.String())

			peer.SendKeepAlive()
			peer.TimerResetKeepalive()

		// clear key material

		case <-peer.timer.zeroAllKeys.C:

			logDebug.Println("Clearing all key material for", peer.String())

			kp := &peer.keyPairs
			kp.mutex.Lock()

			hs := &peer.handshake
			hs.mutex.Lock()

			// unmap local indecies

			indices.mutex.Lock()
			if kp.previous != nil {
				delete(indices.table, kp.previous.localIndex)
			}
			if kp.current != nil {
				delete(indices.table, kp.current.localIndex)
			}
			if kp.next != nil {
				delete(indices.table, kp.next.localIndex)
			}
			delete(indices.table, hs.localIndex)
			indices.mutex.Unlock()

			// zero out key pairs (TODO: better than wait for GC)

			kp.current = nil
			kp.previous = nil
			kp.next = nil
			kp.mutex.Unlock()

			// zero out handshake

			hs.localIndex = 0
			hs.localEphemeral = NoisePrivateKey{}
			hs.remoteEphemeral = NoisePublicKey{}
			hs.chainKey = [blake2s.Size]byte{}
			hs.hash = [blake2s.Size]byte{}
			hs.mutex.Unlock()
		}
	}
}

/* This is the state machine for handshake initiation
 *
 * Associated with this routine is the signal "handshakeBegin"
 * The routine will read from the "handshakeBegin" channel
 * at most every RekeyTimeout seconds
 */
func (peer *Peer) RoutineHandshakeInitiator() {
	device := peer.device

	var elem *QueueOutboundElement

	logInfo := device.log.Info
	logError := device.log.Error
	logDebug := device.log.Debug
	logDebug.Println("Routine, handshake initator, started for", peer.String())

	for {

		// wait for signal

		select {
		case <-peer.signal.handshakeBegin:
		case <-peer.signal.stop:
			return
		}

		// wait for handshake

		func() {
			var err error
			var deadline time.Time
			for attempts := uint(1); ; attempts++ {

				// clear completed signal

				select {
				case <-peer.signal.handshakeCompleted:
				case <-peer.signal.stop:
					return
				default:
				}

				// create initiation

				if elem != nil {
					elem.Drop()
				}
				elem, err = peer.BeginHandshakeInitiation()
				if err != nil {
					logError.Println("Failed to create initiation message", err, "for", peer.String())
					return
				}

				// set timeout

				if attempts == 1 {
					deadline = time.Now().Add(MaxHandshakeAttemptTime)
				}
				timeout := time.NewTimer(RekeyTimeout)
				logDebug.Println("Handshake initiation attempt", attempts, "queued for", peer.String())

				// wait for handshake or timeout

				select {

				case <-peer.signal.stop:
					return

				case <-peer.signal.handshakeCompleted:
					<-timeout.C
					return

				case <-timeout.C:
					if deadline.Before(time.Now().Add(RekeyTimeout)) {
						logInfo.Println("Handshake negotiation timed out for", peer.String())
						signalSend(peer.signal.flushNonceQueue)
						timerStop(peer.timer.keepalivePersistent)
						timerStop(peer.timer.keepalivePassive)
						return
					}
				}
			}
		}()

		signalClear(peer.signal.handshakeBegin)
	}
}