-
Notifications
You must be signed in to change notification settings - Fork 44
/
bgp.go
172 lines (151 loc) · 3.56 KB
/
bgp.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
package packet
const (
OctetLen = 8
MaxASNsSegment = 255
BGP4Version = 4
MinOpenLen = 29
MarkerLen = 16
HeaderLen = 19
MinLen = 19
MaxLen = 4096
MinUpdateLen = 4
NLRIMaxLen = 5
AFILen = 2
SAFILen = 1
CommunityLen = 4
LargeCommunityLen = 12
IPv4Len = 4
IPv6Len = 16
ClusterIDLen = 4
OpenMsg = 1
UpdateMsg = 2
NotificationMsg = 3
KeepaliveMsg = 4
MessageHeaderError = 1
OpenMessageError = 2
UpdateMessageError = 3
HoldTimeExpired = 4
FiniteStateMachineError = 5
Cease = 6
// Msg Header Errors
ConnectionNotSync = 1
BadMessageLength = 2
BadMessageType = 3
// Open Msg Errors
UnsupportedVersionNumber = 1
BadPeerAS = 2
BadBGPIdentifier = 3
UnsupportedOptionalParameter = 4
DeprecatedOpenMsgError5 = 5
UnacceptableHoldTime = 6
// Update Msg Errors
MalformedAttributeList = 1
UnrecognizedWellKnownAttr = 2
MissingWellKnownAttr = 3
AttrFlagsError = 4
AttrLengthError = 5
InvalidOriginAttr = 6
DeprecatedUpdateMsgError7 = 7
InvalidNextHopAttr = 8
OptionalAttrError = 9
InvalidNetworkField = 10
MalformedASPath = 11
// Notification Msg Subcodes
AdministrativeShutdown = 2
AdministrativeReset = 4
// Attribute Type Codes
OriginAttr = 1
ASPathAttr = 2
NextHopAttr = 3
MEDAttr = 4
LocalPrefAttr = 5
AtomicAggrAttr = 6
AggregatorAttr = 7
CommunitiesAttr = 8
OriginatorIDAttr = 9
ClusterListAttr = 10
AS4PathAttr = 17
AS4AggregatorAttr = 18
LargeCommunitiesAttr = 32
// ORIGIN values
IGP = 0
EGP = 1
INCOMPLETE = 2
// NOTIFICATION Cease error SubCodes (RFC4486)
MaxPrefReached = 1
AdminShut = 2
PeerDeconfigured = 3
AdminReset = 4
ConnectionRejected = 5
OtherConfigChange = 8
ConnectionCollisionResolution = 7
OutOfResources = 8
IPv4AFI = 1
IPv6AFI = 2
UnicastSAFI = 1
CapabilitiesParamType = 2
MultiProtocolCapabilityCode = 1
MultiProtocolReachNLRICode = 14
MultiProtocolUnreachNLRICode = 15
AddPathCapabilityCode = 69
ASN4CapabilityCode = 65
AddPathReceive = 1
AddPathSend = 2
AddPathSendReceive = 3
ASTransASN = 23456
)
var (
afiAddrLenBytes = map[uint16]uint8{
1: 4,
2: 16,
}
)
type BGPError struct {
ErrorCode uint8
ErrorSubCode uint8
ErrorStr string
}
func (b BGPError) Error() string {
return b.ErrorStr
}
type BGPMessage struct {
Header *BGPHeader
Body interface{}
}
type BGPHeader struct {
Length uint16
Type uint8
}
type BGPOpen struct {
Version uint8
ASN uint16
HoldTime uint16
BGPIdentifier uint32
OptParmLen uint8
OptParams []OptParam
}
type BGPNotification struct {
ErrorCode uint8
ErrorSubcode uint8
}
type PathAttribute struct {
Length uint16
Optional bool
Transitive bool
Partial bool
ExtendedLength bool
TypeCode uint8
Value interface{}
Next *PathAttribute
}
// AFIName returns the name of an address family
func AFIName(afi uint16) string {
switch afi {
case IPv4AFI:
return "IPv4"
case IPv6AFI:
return "IPv6"
default:
return "Unknown AFI"
}
}