forked from msgboxio/ike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
349 lines (340 loc) · 8.7 KB
/
run.go
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package ike
import (
"time"
"github.com/go-kit/kit/log/level"
"github.com/msgboxio/ike/protocol"
"github.com/pkg/errors"
)
var (
initJitter = 2 * time.Second
jitterFactor = -0.5
)
func runInitiator(sess *Session) (err error) {
// send initiator INIT after jittered wait and wait for reply
time.Sleep(Jitter(initJitter, jitterFactor))
var msg *Message
for {
msg, err = sess.SendMsgGetReply(sess.InitMsg)
if err != nil {
return
}
// TODO - check if we already have a connection to this host
// check if incoming message is an acceptable Init Response
if err = checkInitResponseForSession(sess, msg); err != nil {
if ce, ok := err.(peerRequestsCookieError); ok {
sess.SetCookie(ce.Cookie)
sess.msgIDReq.reset(0)
// This will keep going if peer keeps sending COOKIE
// TODO -fix
continue
}
// return error
return
}
break
}
init, ok := msg.Params.(*initParams)
if !ok {
return errors.New("missing init parameters")
}
if err = sess.CreateIkeSa(init); err != nil {
return
}
if init.hasNat {
sess.Logger.Log("NAT detected")
}
// TODO
// If there is NAT , then all the further communication is performed over port 4500 instead of the default port 500
// also, periodically send keepalive packets in order for NAT to keep it’s bindings alive.
// save message
sess.initRb = msg.Data
// start auth
sess.EspSpiI = MakeSpi()[:4]
// send AUTH and wait for reply
if msg, err = sess.SendMsgGetReply(sess.AuthMsg); err != nil {
return
}
// is it an AUTH response, and can we proceed
if err = checkAuthResponseForSession(sess, msg); err != nil {
return
}
// can we authenticate ?
espSpiR, lifetime, err := handleAuthForSession(sess, msg)
if err != nil {
// send notification in INFORMATIONAL request to peer & end IKE SA
sess.CheckError(err, false)
return
}
// replace espSpiI & lifetime : MUTATION
sess.EspSpiR = espSpiR
if lifetime != 0 {
sess.cfg.Lifetime = lifetime
}
return
}
// got new INIT
func runResponder(sess *Session) (err error) {
// wait for INIT
// send COOKIE, wait - handled by cmd:newSession
// get INIT
msg, ok := <-sess.incoming
if !ok {
return errorSessionClosed
}
// fetch params already attached from prior parsing
init, ok := msg.Params.(*initParams)
if !ok {
return errors.New("missing init parameters")
}
if err = sess.CreateIkeSa(init); err != nil {
return
}
if init.hasNat {
sess.Logger.Log("NAT detected")
}
// TODO - NAT
// save message
sess.initIb = msg.Data
// send INIT_reply & wait for AUTH
msg, err = sess.SendMsgGetReply(sess.InitMsg)
if err != nil {
return
}
// is it an AUTH request
if err = checkAuthRequestForSession(sess, msg); err != nil {
return
}
// can we authenticate ?
espSpiI, lifetime, err := handleAuthForSession(sess, msg)
if err != nil {
sess.AuthReply(err)
return
}
// replace espSpiI & espSpiR : MUTATION
sess.EspSpiI = espSpiI
sess.EspSpiR = MakeSpi()[:4]
if lifetime != 0 {
sess.cfg.Lifetime = lifetime
}
// send AUTH_reply
if err = sess.sendMsg(sess.AuthMsg()); err != nil {
return
}
return
}
func runIpsecRekey(sess *Session) (err error) {
// if REKEY timeout
// create new tkm, send REKEY, wait for REKEY_reply,
// retry on timeout
newTkm, err := NewTkm(&sess.cfg, nil)
if err != nil {
return
}
espSpiI := MakeSpi()[:4]
// closure with parameters for new SA
rekeyFn := func() (*OutgoingMessage, error) {
return sess.RekeyMsg(ChildSaFromSession(sess, newTkm, true, espSpiI))
}
msg, err := sess.SendMsgGetReply(rekeyFn)
if err != nil {
return
}
params, err := parseChildSa(msg, true)
if err != nil {
return
}
for _, n := range msg.Payloads.GetNotifications() {
if nErr, ok := protocol.GetIkeErrorCode(n.NotificationType); ok {
// for example, due to FAILED_CP_REQUIRED, NO_PROPOSAL_CHOSEN, TS_UNACCEPTABLE etc
// TODO - for now, we should simply end the IKE_SA
err = errors.Wrap(nErr, "peer notified")
return
}
}
espSpiR, err := checkIpsecRekeyResponse(sess, params)
if err != nil {
// send notification in INFORMATIONAL request to peer & end IKE SA
sess.CheckError(err, false)
return
}
if params.dhPublic != nil {
if err = newTkm.DhGenerateKey(params.dhPublic); err != nil {
return
}
}
newTkm.Nr = params.nonce
// install new SA - [espSpiI, espSpiR, nI, nR & dhShared]
err = sess.AddSa(addSaParams(sess.tkm,
newTkm.Ni, newTkm.Nr, newTkm.DhShared,
espSpiI, espSpiR,
&sess.cfg))
if err != nil {
return
}
// remove old sa
sess.RemoveSa()
// replace espSpiI & espSpiR : MUTATION
sess.EspSpiI = espSpiI
sess.EspSpiR = espSpiR
if params.lifetime != 0 {
sess.cfg.Lifetime = params.lifetime
}
return
}
func onRekeyRequest(sess *Session, msg *Message) (err error) {
// if REKEY rx :
params, err := parseChildSa(msg, false)
if err != nil {
return
}
espSpiI, err := checkIpsecRekeyRequest(sess, params)
if err != nil {
// send notification to peer & end IKE SA
sess.CheckError(err, true)
return
}
// create tkm with new Nonce
newTkm, err := NewTkm(&sess.cfg, params.nonce)
if err != nil {
return
}
if params.dhPublic != nil {
if err = newTkm.DhGenerateKey(params.dhPublic); err != nil {
return
}
}
espSpiR := MakeSpi()[:4]
// send REKEY_reply
// closure with parameters for new SA
err = sess.sendMsg(sess.RekeyMsg(ChildSaFromSession(sess, newTkm, false, espSpiR)))
if err != nil {
return
}
// install new SA - [espSpiI, espSpiR, nI, nR & dhShared]
err = sess.AddSa(addSaParams(sess.tkm,
newTkm.Ni, newTkm.Nr, newTkm.DhShared,
espSpiI, espSpiR,
&sess.cfg))
if err != nil {
return
}
// remove old sa
sess.RemoveSa()
// replace espSpiI & espSpiR : MUTATION
sess.EspSpiI = espSpiI
sess.EspSpiR = espSpiR
if params.lifetime != 0 {
sess.cfg.Lifetime = params.lifetime
}
// send INFORMATIONAL, wait for INFORMATIONAL_reply
// if timeout, send REKEY_reply again
return
}
func monitorSa(sess *Session) (err error) {
// install policy
err = sess.installPolicy()
if err != nil {
return
}
// add INITIAL sa
err = sess.AddSa(addSaParams(sess.tkm,
sess.tkm.Ni, sess.tkm.Nr, nil, // NOTE : use the original SA
sess.EspSpiI, sess.EspSpiR,
&sess.cfg))
if err != nil {
return
}
if sess.isInitiator {
// send INFORMATIONAL, wait for INFORMATIONAL_reply
// if timeout, send AUTH_reply again
// monitor SA
if err = sess.SendEmptyInformational(false); err != nil {
return
}
}
// check for duplicate SA, if found remove one with smaller nonce
// setup SA REKEY timeout (jittered) & monitoring
rekeyDelay := sess.cfg.Lifetime
saRekeyDeadline := time.NewTimer(rekeyDelay)
rekeyTimeout := Jitter(rekeyDelay, jitterFactor)
saRekeyTimer := time.NewTimer(rekeyTimeout)
sess.Logger.Log("RekeyDeadline", rekeyDelay, "RekeyTimeout", rekeyTimeout)
for {
select {
// TODO - use a timeout here
case msg, ok := <-sess.incoming:
if !ok {
return errorSessionClosed
}
switch msg.IkeHeader.ExchangeType {
// if INFORMATIONAL, send INFORMATIONAL_reply
case protocol.INFORMATIONAL:
evt := HandleInformationalForSession(sess, msg)
if evt == nil {
continue
}
switch evt.SessionNotificationType {
case MSG_EMPTY_RESPONSE:
break
case MSG_EMPTY_REQUEST:
if iErr := sess.SendEmptyInformational(true); iErr != nil {
return iErr
}
case MSG_ERROR:
iErr := evt.Message.(error)
sess.Logger.Log("INFORMATIONAL", iErr)
return iErr
default:
level.Warn(sess.Logger).Log("INFORMATIONAL", "unhandled", "NOTIFICATION", evt.Message)
}
case protocol.CREATE_CHILD_SA:
// ONLY :
// Accept SA rekey if responder
if sess.isInitiator {
level.Warn(sess.Logger).Log("CREATE_CHILD_SA", "Currently only supported for responder")
// send notification
sess.Notify(protocol.ERR_NO_ADDITIONAL_SAS, true)
continue
}
if err = onRekeyRequest(sess, msg); err != nil {
return err
}
// reset timers
saRekeyTimer.Reset(rekeyTimeout)
saRekeyDeadline.Reset(rekeyDelay)
}
case <-saRekeyDeadline.C:
return errorRekeyDeadlineExceeded
case <-saRekeyTimer.C:
// ONLY :
// Initiate SA rekey if initiator
if !sess.isInitiator {
level.Warn(sess.Logger).Log("RekeyTimeout", "Currently only supported for initiator")
continue
}
sess.Logger.Log("Rekey", "Timeout")
if err = runIpsecRekey(sess); err != nil {
sess.Logger.Log("RekeyError", err)
return
}
// reset timers
saRekeyTimer.Reset(rekeyTimeout)
saRekeyDeadline.Reset(rekeyDelay)
} // select
} // for
}
// RunSession starts and monitors the session returning when the session ends
func RunSession(sess *Session) error {
var err error
if sess.isInitiator {
err = runInitiator(sess)
} else {
err = runResponder(sess)
}
if err == nil {
err = monitorSa(sess)
}
sess.cancel()
// err must not be nil
return err
}