summaryrefslogtreecommitdiffhomepage
path: root/server/peer.go
blob: 70d464ecffa315010d3106e7b30e1944dd6385a7 (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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
	"encoding/json"
	log "github.com/Sirupsen/logrus"
	"github.com/osrg/gobgp/api"
	"github.com/osrg/gobgp/config"
	"github.com/osrg/gobgp/packet"
	"github.com/osrg/gobgp/policy"
	"github.com/osrg/gobgp/table"
	"net"
	"time"
)

const (
	FLOP_THRESHOLD    = time.Second * 30
	MIN_CONNECT_RETRY = 10
)

type Peer struct {
	gConf                 config.Global
	conf                  config.Neighbor
	fsm                   *FSM
	rfMap                 map[bgp.RouteFamily]bool
	capMap                map[bgp.BGPCapabilityCode][]bgp.ParameterCapabilityInterface
	adjRib                *table.AdjRib
	peerInfo              *table.PeerInfo
	outgoing              chan *bgp.BGPMessage
	inPolicies            []*policy.Policy
	defaultInPolicy       config.DefaultPolicyType
	isConfederationMember bool
	isEBGP                bool
}

func NewPeer(g config.Global, conf config.Neighbor) *Peer {
	peer := &Peer{
		gConf:  g,
		conf:   conf,
		rfMap:  make(map[bgp.RouteFamily]bool),
		capMap: make(map[bgp.BGPCapabilityCode][]bgp.ParameterCapabilityInterface),
	}

	conf.NeighborState.SessionState = uint32(bgp.BGP_FSM_IDLE)
	conf.Timers.TimersState.Downtime = time.Now().Unix()
	for _, rf := range conf.AfiSafis.AfiSafiList {
		k, _ := bgp.GetRouteFamily(rf.AfiSafiName)
		peer.rfMap[k] = true
	}
	peer.peerInfo = &table.PeerInfo{
		AS:      conf.NeighborConfig.PeerAs,
		LocalAS: g.GlobalConfig.As,
		LocalID: g.GlobalConfig.RouterId,
		Address: conf.NeighborConfig.NeighborAddress,
	}
	peer.adjRib = table.NewAdjRib(peer.configuredRFlist())
	peer.fsm = NewFSM(&g, &conf)

	if conf.NeighborConfig.PeerAs != g.GlobalConfig.As {
		peer.isEBGP = true
		for _, member := range g.Confederation.ConfederationConfig.MemberAs {
			if member == conf.NeighborConfig.PeerAs {
				peer.isConfederationMember = true
				break
			}
		}
	}

	return peer
}

func (peer *Peer) isRouteServerClient() bool {
	return peer.conf.RouteServer.RouteServerConfig.RouteServerClient
}

func (peer *Peer) configuredRFlist() []bgp.RouteFamily {
	rfList := []bgp.RouteFamily{}
	for _, rf := range peer.conf.AfiSafis.AfiSafiList {
		k, _ := bgp.GetRouteFamily(rf.AfiSafiName)
		rfList = append(rfList, k)
	}
	return rfList
}

func (peer *Peer) handleBGPmessage(m *bgp.BGPMessage) ([]*table.Path, bool, []*bgp.BGPMessage) {
	bgpMsgList := []*bgp.BGPMessage{}
	pathList := []*table.Path{}
	log.WithFields(log.Fields{
		"Topic": "Peer",
		"Key":   peer.conf.NeighborConfig.NeighborAddress,
		"data":  m,
	}).Debug("received")
	update := false

	switch m.Header.Type {
	case bgp.BGP_MSG_OPEN:
		body := m.Body.(*bgp.BGPOpen)
		peer.peerInfo.ID = m.Body.(*bgp.BGPOpen).ID
		r := make(map[bgp.RouteFamily]bool)
		for _, p := range body.OptParams {
			if paramCap, y := p.(*bgp.OptionParameterCapability); y {
				for _, c := range paramCap.Capability {
					m, ok := peer.capMap[c.Code()]
					if !ok {
						m = make([]bgp.ParameterCapabilityInterface, 0, 1)
					}
					peer.capMap[c.Code()] = append(m, c)

					if c.Code() == bgp.BGP_CAP_MULTIPROTOCOL {
						m := c.(*bgp.CapMultiProtocol)
						r[bgp.AfiSafiToRouteFamily(m.CapValue.AFI, m.CapValue.SAFI)] = true
					}
				}
			}
		}

		for rf, _ := range peer.rfMap {
			if _, y := r[rf]; !y {
				delete(peer.rfMap, rf)
			}
		}

		for _, rf := range peer.configuredRFlist() {
			if _, ok := r[rf]; ok {
				peer.rfMap[rf] = true
			}
		}

		// calculate HoldTime
		// RFC 4271 P.13
		// a BGP speaker MUST calculate the value of the Hold Timer
		// by using the smaller of its configured Hold Time and the Hold Time
		// received in the OPEN message.
		holdTime := float64(body.HoldTime)
		myHoldTime := peer.conf.Timers.TimersConfig.HoldTime
		if holdTime > myHoldTime {
			peer.fsm.negotiatedHoldTime = myHoldTime
		} else {
			peer.fsm.negotiatedHoldTime = holdTime
		}

	case bgp.BGP_MSG_ROUTE_REFRESH:
		rr := m.Body.(*bgp.BGPRouteRefresh)
		rf := bgp.AfiSafiToRouteFamily(rr.AFI, rr.SAFI)
		if _, ok := peer.rfMap[rf]; !ok {
			log.WithFields(log.Fields{
				"Topic": "Peer",
				"Key":   peer.conf.NeighborConfig.NeighborAddress,
				"Data":  rf,
			}).Warn("Route family isn't supported")
			break
		}
		if _, ok := peer.capMap[bgp.BGP_CAP_ROUTE_REFRESH]; ok {
			pathList = peer.adjRib.GetOutPathList(rf)
		} else {
			log.WithFields(log.Fields{
				"Topic": "Peer",
				"Key":   peer.conf.NeighborConfig.NeighborAddress,
			}).Warn("ROUTE_REFRESH received but the capability wasn't advertised")
		}

	case bgp.BGP_MSG_UPDATE:
		update = true
		peer.conf.Timers.TimersState.UpdateRecvTime = time.Now().Unix()
		body := m.Body.(*bgp.BGPUpdate)
		confedCheckRequired := !peer.isConfederationMember && peer.isEBGP
		_, err := bgp.ValidateUpdateMsg(body, peer.rfMap, confedCheckRequired)
		if err != nil {
			log.WithFields(log.Fields{
				"Topic": "Peer",
				"Key":   peer.conf.NeighborConfig.NeighborAddress,
				"error": err,
			}).Warn("malformed BGP update message")
			m := err.(*bgp.MessageError)
			if m.TypeCode != 0 {
				bgpMsgList = append(bgpMsgList, bgp.NewBGPNotificationMessage(m.TypeCode, m.SubTypeCode, m.Data))
			}
			break
		}
		table.UpdatePathAttrs4ByteAs(body)
		pathList = table.ProcessMessage(m, peer.peerInfo)
		peer.adjRib.UpdateIn(pathList)
	}
	return pathList, update, bgpMsgList
}

func (peer *Peer) getBests(loc *LocalRib) []*table.Path {
	pathList := []*table.Path{}
	for _, rf := range peer.configuredRFlist() {
		for _, paths := range loc.rib.GetBestPathList(rf) {
			pathList = append(pathList, paths)
		}
	}
	return pathList
}

func (peer *Peer) startFSMHandler(incoming chan *fsmMsg) {
	peer.fsm.h = NewFSMHandler(peer.fsm, incoming, peer.outgoing)
}

func (peer *Peer) PassConn(conn *net.TCPConn) {
	isEBGP := peer.gConf.GlobalConfig.As != peer.conf.NeighborConfig.PeerAs
	if isEBGP {
		ttl := 1
		SetTcpTTLSockopts(conn, ttl)
	}
	select {
	case peer.fsm.connCh <- conn:
	default:
		conn.Close()
		log.WithFields(log.Fields{
			"Topic": "Peer",
			"Key":   peer.conf.NeighborConfig.NeighborAddress,
		}).Warn("accepted conn is closed to avoid be blocked")
	}
}

func (peer *Peer) MarshalJSON() ([]byte, error) {
	return json.Marshal(peer.ToApiStruct())
}

func (peer *Peer) ToApiStruct() *api.Peer {

	f := peer.fsm
	c := f.pConf

	remoteCap := make([]*api.Capability, 0, len(peer.capMap))
	for _, c := range peer.capMap {
		for _, m := range c {
			remoteCap = append(remoteCap, m.ToApiStruct())
		}
	}

	caps := capabilitiesFromConfig(&peer.gConf, &peer.conf)
	localCap := make([]*api.Capability, 0, len(caps))
	for _, c := range caps {
		localCap = append(localCap, c.ToApiStruct())
	}

	conf := &api.PeerConf{
		RemoteIp:          c.NeighborConfig.NeighborAddress.String(),
		Id:                peer.peerInfo.ID.To4().String(),
		RemoteAs:          c.NeighborConfig.PeerAs,
		RemoteCap:         remoteCap,
		LocalCap:          localCap,
		KeepaliveInterval: uint32(peer.conf.Timers.TimersConfig.KeepaliveInterval),
		Holdtime:          uint32(peer.conf.Timers.TimersConfig.HoldTime),
	}

	s := &c.NeighborState
	timer := &c.Timers

	uptime := int64(0)
	if timer.TimersState.Uptime != 0 {
		uptime = int64(time.Now().Sub(time.Unix(timer.TimersState.Uptime, 0)).Seconds())
	}
	downtime := int64(0)
	if timer.TimersState.Downtime != 0 {
		downtime = int64(time.Now().Sub(time.Unix(timer.TimersState.Downtime, 0)).Seconds())
	}

	advertized := uint32(0)
	received := uint32(0)
	accepted := uint32(0)
	if f.state == bgp.BGP_FSM_ESTABLISHED {
		for _, rf := range peer.configuredRFlist() {
			advertized += uint32(peer.adjRib.GetOutCount(rf))
			received += uint32(peer.adjRib.GetInCount(rf))
			// FIXME: we should store 'accepted' in memory
			for _, p := range peer.adjRib.GetInPathList(rf) {
				applied, path := peer.applyPolicies(POLICY_DIRECTION_IN, p)
				if applied && path == nil || !applied && peer.defaultInPolicy != config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE {
					continue
				}
				accepted += 1
			}
		}
	}

	keepalive := uint32(0)
	if f.negotiatedHoldTime != 0 {
		if f.negotiatedHoldTime < timer.TimersConfig.HoldTime {
			keepalive = uint32(f.negotiatedHoldTime / 3)
		} else {
			keepalive = uint32(timer.TimersConfig.KeepaliveInterval)
		}
	}

	info := &api.PeerInfo{
		BgpState:                  f.state.String(),
		AdminState:                f.adminState.String(),
		FsmEstablishedTransitions: s.EstablishedCount,
		TotalMessageOut:           s.Messages.Sent.Total,
		TotalMessageIn:            s.Messages.Received.Total,
		UpdateMessageOut:          s.Messages.Sent.Update,
		UpdateMessageIn:           s.Messages.Received.Update,
		KeepAliveMessageOut:       s.Messages.Sent.Keepalive,
		KeepAliveMessageIn:        s.Messages.Received.Keepalive,
		OpenMessageOut:            s.Messages.Sent.Open,
		OpenMessageIn:             s.Messages.Received.Open,
		NotificationOut:           s.Messages.Sent.Notification,
		NotificationIn:            s.Messages.Received.Notification,
		RefreshMessageOut:         s.Messages.Sent.Refresh,
		RefreshMessageIn:          s.Messages.Received.Refresh,
		DiscardedOut:              s.Messages.Sent.Discarded,
		DiscardedIn:               s.Messages.Received.Discarded,
		Uptime:                    uptime,
		Downtime:                  downtime,
		Received:                  received,
		Accepted:                  accepted,
		Advertized:                advertized,
		OutQ:                      uint32(len(peer.outgoing)),
		Flops:                     s.Flops,
		NegotiatedHoldtime:        uint32(f.negotiatedHoldTime),
		KeepaliveInterval:         keepalive,
	}

	return &api.Peer{
		Conf: conf,
		Info: info,
	}
}

func (peer *Peer) setPolicy(policyMap map[string]*policy.Policy) {
	// configure distribute policy
	policyConf := peer.conf.ApplyPolicy
	inPolicies := make([]*policy.Policy, 0)
	for _, policyName := range policyConf.ApplyPolicyConfig.InPolicy {
		log.WithFields(log.Fields{
			"Topic":      "Peer",
			"Key":        peer.conf.NeighborConfig.NeighborAddress,
			"PolicyName": policyName,
		}).Info("distribute policy installed")
		if pol, ok := policyMap[policyName]; ok {
			log.Debug("distribute policy : ", pol)
			inPolicies = append(inPolicies, pol)
		}
	}
	peer.inPolicies = inPolicies
	peer.defaultInPolicy = policyConf.ApplyPolicyConfig.DefaultInPolicy
}

func (peer *Peer) applyPolicies(d Direction, original *table.Path) (bool, *table.Path) {
	var policies []*policy.Policy
	switch d {
	case POLICY_DIRECTION_IN:
		policies = peer.inPolicies
	}
	return applyPolicy("Peer", peer.conf.NeighborConfig.NeighborAddress.String(), d, policies, original)
}

type LocalRib struct {
	rib                 *table.TableManager
	importPolicies      []*policy.Policy
	defaultImportPolicy config.DefaultPolicyType
	exportPolicies      []*policy.Policy
	defaultExportPolicy config.DefaultPolicyType
}

func NewLocalRib(owner string, rfList []bgp.RouteFamily, policyMap map[string]*policy.Policy) *LocalRib {
	return &LocalRib{
		rib: table.NewTableManager(owner, rfList),
	}
}

func (loc *LocalRib) OwnerName() string {
	return loc.rib.OwnerName()
}

func (loc *LocalRib) isGlobal() bool {
	return loc.OwnerName() == "global"
}

func (loc *LocalRib) setPolicy(peer *Peer, policyMap map[string]*policy.Policy) {
	// configure import policy
	policyConf := peer.conf.ApplyPolicy
	inPolicies := make([]*policy.Policy, 0)
	for _, policyName := range policyConf.ApplyPolicyConfig.ImportPolicy {
		log.WithFields(log.Fields{
			"Topic":      "Peer",
			"Key":        peer.conf.NeighborConfig.NeighborAddress,
			"PolicyName": policyName,
		}).Info("import policy installed")
		if pol, ok := policyMap[policyName]; ok {
			log.Debug("import policy : ", pol)
			inPolicies = append(inPolicies, pol)
		}
	}
	loc.importPolicies = inPolicies
	loc.defaultImportPolicy = policyConf.ApplyPolicyConfig.DefaultImportPolicy

	// configure export policy
	outPolicies := make([]*policy.Policy, 0)
	for _, policyName := range policyConf.ApplyPolicyConfig.ExportPolicy {
		log.WithFields(log.Fields{
			"Topic":      "Peer",
			"Key":        peer.conf.NeighborConfig.NeighborAddress,
			"PolicyName": policyName,
		}).Info("export policy installed")
		if pol, ok := policyMap[policyName]; ok {
			log.Debug("export policy : ", pol)
			outPolicies = append(outPolicies, pol)
		}
	}
	loc.exportPolicies = outPolicies
	loc.defaultExportPolicy = policyConf.ApplyPolicyConfig.DefaultExportPolicy
}

// apply policies to the path
// if multiple policies are defined,
// this function applies each policy to the path in the order that
// policies are stored in the array passed to this function.
//
// the way of applying statements inside a single policy
//   - apply statement until the condition in the statement matches.
//     if the condition matches the path, apply the action on the statement and
//     return value that indicates 'applied' to caller of this function
//   - if no statement applied, then process the next policy
//
// if no policy applied, return value that indicates 'not applied' to the caller of this function
//
// return values:
//	bool -- indicates that any of policy applied to the path that is passed to this function
//  table.Path -- indicates new path object that is the result of modification according to
//                policy's action.
//                If the applied policy doesn't have a modification action,
//                then return the path itself that is passed to this function, otherwise return
//                modified path.
//                If action of the policy is 'reject', return nil
//
func (loc *LocalRib) applyPolicies(d Direction, original *table.Path) (bool, *table.Path) {
	var policies []*policy.Policy
	switch d {
	case POLICY_DIRECTION_EXPORT:
		policies = loc.exportPolicies
	case POLICY_DIRECTION_IMPORT:
		policies = loc.importPolicies
	}
	return applyPolicy("Loc", loc.OwnerName(), d, policies, original)
}

func applyPolicy(component, owner string, d Direction, policies []*policy.Policy, original *table.Path) (bool, *table.Path) {
	var applied bool = true
	for _, pol := range policies {
		if result, action, newpath := pol.Apply(original); result {
			log.Debug("newpath: ", newpath)
			if action == policy.ROUTE_TYPE_REJECT {
				log.WithFields(log.Fields{
					"Topic": component,
					"Key":   owner,
					"NLRI":  original.GetNlri(),
					"Dir":   d,
				}).Debug("path was rejected")
				// return applied, nil, this means path was rejected
				return applied, nil
			} else {
				// return applied, new path
				return applied, newpath
			}
		}
	}

	log.WithFields(log.Fields{
		"Topic": component,
		"Key":   owner,
		"Len":   len(policies),
		"NLRI":  original,
		"Dir":   d,
	}).Debug("no policy applied")
	return !applied, original
}