-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
409 lines (330 loc) · 9.71 KB
/
session.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* VC5 load balancer. Copyright (C) 2021-present David Coles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package bgp
import (
"fmt"
"sync"
"time"
)
const (
IDLE = "IDLE"
ACTIVE = "ACTIVE"
CONNECT = "CONNECT"
OPEN_SENT = "OPEN_SENT"
OPEN_CONFIRM = "OPEN_CONFIRM"
ESTABLISHED = "ESTABLISHED"
)
type Status struct {
State string `json:"state"`
When time.Time `json:"when"`
Duration time.Duration `json:"duration_s"`
UpdateCalculation time.Duration `json:"update_calculation_ms"`
Advertised uint64 `json:"advertised_routes"`
Withdrawn uint64 `json:"withdrawn_routes"`
Prefixes int `json:"current_routes"`
Attempts uint64 `json:"connection_attempts"`
Connections uint64 `json:"successful_connections"`
Established uint64 `json:"established_sessions"`
LastError string `json:"last_error"`
HoldTime uint16 `json:"hold_time"`
LocalASN uint16 `json:"local_asn"`
RemoteASN uint16 `json:"remote_asn"`
AdjRIBOut []string `json:"adj_rib_out"`
LocalIP string `json:"local_ip"`
//EBGP bool `json:""`
}
const (
CONNECTION_FAILED = iota
REMOTE_SHUTDOWN
LOCAL_SHUTDOWN
INVALID_LOCALIP
)
type Session struct {
c chan Update
p Parameters
r []IP
status Status
mutex sync.Mutex
update Update
log BGPNotify
}
func NewSession(id IP, peer string, p Parameters, r []IP, l BGPNotify) *Session {
s := &Session{p: p, r: r, log: l, status: Status{State: IDLE}, update: Update{RIB: r, Parameters: p}}
s.c = s.session(id, peer)
return s
}
func (s *Session) Status() Status {
s.mutex.Lock()
defer s.mutex.Unlock()
s.status.Duration = time.Now().Sub(s.status.When) / time.Second
return s.status
}
func (s *Session) RIB(r []IP) {
s.r = r
s.c <- Update{RIB: s.r, Parameters: s.p}
}
func (s *Session) Configure(p Parameters) {
s.p = p
s.c <- Update{RIB: s.r, Parameters: s.p}
}
func (s *Session) Close() {
close(s.c)
}
func (s *Session) state2(state string) {
s.status.State = state
s.status.When = time.Now().Round(time.Second)
}
func (s *Session) state(state string) {
s.mutex.Lock()
defer s.mutex.Unlock()
//s.status.State = state
s.state2(state)
}
func (s *Session) error(error string) string {
s.mutex.Lock()
defer s.mutex.Unlock()
s.status.LastError = error
return error
}
func (s *Session) established(ht uint16, local, remote uint16) {
s.mutex.Lock()
defer s.mutex.Unlock()
//s.status.State = ESTABLISHED
s.state2(ESTABLISHED)
s.status.Established++
s.status.LastError = ""
s.status.HoldTime = ht
s.status.LocalASN = local
s.status.RemoteASN = remote
//s.status.EBGP = local != remote
}
func (s *Session) active(ht uint16, local uint16, ip [4]byte) {
s.mutex.Lock()
defer s.mutex.Unlock()
//s.status.State = ACTIVE
s.state2(ACTIVE)
s.status.Attempts++
s.status.AdjRIBOut = nil
s.status.Prefixes = 0
s.status.Advertised = 0
s.status.Withdrawn = 0
s.status.HoldTime = ht
s.status.LocalASN = local
s.status.RemoteASN = 0
//s.status.EBGP = false
s.status.LocalIP = ip_string(ip)
}
func (s *Session) connect() {
s.mutex.Lock()
defer s.mutex.Unlock()
//s.status.State = CONNECT
s.state2(CONNECT)
s.status.Connections++
}
func (s *Session) update_stats(a, w uint64, d time.Duration, r []string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.status.Advertised += a
s.status.Withdrawn += w
s.status.UpdateCalculation = d / time.Millisecond
s.status.AdjRIBOut = r
s.status.Prefixes = len(r)
}
func (s *Session) session(id IP, peer string) chan Update {
const F = "session"
updates := make(chan Update, 10)
go func() {
retry_time := 30 * time.Second
timer := time.NewTimer(1) // fires immediately
defer timer.Stop()
var ok bool
for {
select {
case <-timer.C:
s.log.BGPSession(peer, true, "Connecting ...")
b, n := s.try(id, peer, updates)
var e string
if b {
e = fmt.Sprintf("Received notification[%d:%d]: %s", n.code, n.sub, note(n.code, n.sub))
if len(n.data) > 0 {
e += " (" + string(n.data) + ")"
}
s.log.BGPSession(peer, false, e)
} else {
if n.code == 0 {
e = note(n.code, n.sub)
} else {
e = fmt.Sprintf("Sent notification[%d:%d]: %s", n.code, n.sub, note(n.code, n.sub))
}
if len(n.data) > 0 {
e += " (" + string(n.data) + ")"
}
if n.code == 0 && n.sub == LOCAL_SHUTDOWN {
s.log.BGPSession(peer, true, e)
} else {
s.log.BGPSession(peer, false, e) // treat as "remote" as it was a failed connection, not a local shutdown
}
}
s.error(e)
s.idle()
timer.Reset(retry_time)
case s.update, ok = <-updates: // stores last update
if !ok {
return
}
}
}
}()
return updates
}
func (s *Session) idle() {
s.mutex.Lock()
defer s.mutex.Unlock()
//s.status.State = IDLE
s.state2(IDLE)
}
func (s *Session) try(routerid IP, peer string, updates chan Update) (bool, notification) {
asnumber := s.update.Parameters.ASNumber
holdtime := s.update.Parameters.HoldTime
sourceip := s.update.Parameters.SourceIP
localip := sourceip // may be 0.0.0.0 - in which case network stack chooses address/interface
var external bool
if holdtime < 3 {
holdtime = 10
}
s.active(holdtime, asnumber, localip)
conn, err := new_connection(localip, peer)
if err != nil {
return false, local(CONNECTION_FAILED, err.Error())
}
defer conn.Close()
var nul [4]byte
localip = conn.Local()
if localip == nul {
return false, local(INVALID_LOCALIP, err.Error())
}
//s.active(holdtime, asnumber, localip) // locks mutex
s.mutex.Lock()
s.status.HoldTime = holdtime
s.status.LocalIP = ip_string(localip)
s.mutex.Unlock()
nexthop := localip
s.connect()
conn.write(openMessage(asnumber, holdtime, routerid))
s.state(OPEN_SENT)
hold_time_ns := time.Duration(holdtime) * time.Second
hold_timer := time.NewTimer(hold_time_ns)
defer hold_timer.Stop()
keepalive_time_ns := hold_time_ns / 3
keepalive_timer := time.NewTicker(keepalive_time_ns)
defer keepalive_timer.Stop()
for {
select {
case m, ok := <-conn.C:
if !ok {
return false, local(REMOTE_SHUTDOWN, conn.Error)
}
hold_timer.Reset(hold_time_ns)
switch m.mtype {
case M_NOTIFICATION:
return true, m.notification
case M_KEEPALIVE:
if s.status.State == OPEN_SENT {
n := notificationMessage(FSM_ERROR, 0)
conn.write(n)
return false, n.notification
}
case M_OPEN:
if s.status.State != OPEN_SENT {
n := notificationMessage(FSM_ERROR, 0)
conn.write(notificationMessage(FSM_ERROR, 0))
return false, n.notification
}
if m.open.version != 4 {
n := notificationMessage(OPEN_ERROR, UNSUPPORTED_VERSION_NUMBER)
conn.write(n)
return false, n.notification
}
if m.open.ht < 3 {
n := notificationMessage(OPEN_ERROR, UNNACEPTABLE_HOLD_TIME)
conn.write(n)
return false, n.notification
}
if m.open.id == routerid {
n := notificationMessage(OPEN_ERROR, BAD_BGP_ID)
conn.write(n)
return false, n.notification
}
if m.open.ht < holdtime {
holdtime = m.open.ht
hold_time_ns = time.Duration(holdtime) * time.Second
keepalive_time_ns = hold_time_ns / 3
}
hold_timer.Reset(hold_time_ns)
keepalive_timer.Reset(keepalive_time_ns)
external = m.open.as != asnumber
s.established(holdtime, asnumber, m.open.as)
conn.write(keepaliveMessage())
t := time.Now()
aro, p := s.update.adjRIBOutP()
//conn.write(updateMessage(nexthop, asnumber, p, external, advertise(aro)))
conn.write(_updateMessage(nexthop, asnumber, p.LocalPref, p.MED, p.Communities, external, advertise(aro)))
s.update_stats(uint64(len(aro)), 0, time.Now().Sub(t), to_string(aro))
case M_UPDATE:
if s.status.State != ESTABLISHED {
n := notificationMessage(FSM_ERROR, 0)
conn.write(n)
return false, n.notification
}
// we just ignore updates!
default:
n := notificationMessage(MESSAGE_HEADER_ERROR, BAD_MESSAGE_TYPE)
conn.write(n)
return false, n.notification
}
case r, ok := <-updates:
if !ok {
//conn.write(notificationMessage(CEASE, ADMINISTRATIVE_SHUTDOWN))
conn.write(shutdownMessage("That's all, folks!"))
return false, local(LOCAL_SHUTDOWN, "")
}
if s.status.State == ESTABLISHED {
t := time.Now()
a, w, nlris := r.updates(s.update)
if len(nlris) != 0 {
//conn.write(updateMessage(nexthop, asnumber, r.Parameters, external, nlris))
p := r.Parameters
conn.write(_updateMessage(nexthop, asnumber, p.LocalPref, p.MED, p.Communities, external, nlris))
}
s.update_stats(a, w, time.Now().Sub(t), r.adjRIBOutString())
}
s.update = r
case <-keepalive_timer.C:
if s.status.State == ESTABLISHED {
conn.write(keepaliveMessage())
}
case <-hold_timer.C:
n := notificationMessage(HOLD_TIMER_EXPIRED, 0)
conn.write(n)
return false, n.notification
}
}
}
func local(s uint8, d string) notification {
return notification{code: 0, sub: s, data: []byte(d)}
}