BGP协议格式记录
[toc]
BGP协议主体格式
BGP协议主要由Header和BGP Message Types组成
Message Header Format
Header结构为Marker(16字节)、Length(2字节)、Type(1字节)组成
=> RFC4271 Sec 4.1
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ +
| |
+ +
| Marker |
+ +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length | Type |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Type存在以下状态:
1 - OPEN
2 - UPDATE
3 - NOTIFICATION
4 - KEEPALIVE
5 - ROUTE-REFRESH
6 - DYNAMIC CAPABILITY
P.S Type: 5 - ROUTE-REFRESH出自 RFC2918 Sec 3.Type: 6 - DYNAMIC CAPABILITY出自 draft-ietf-idr-dynamic-cap-16 Sec 3.
OPEN Message Format
Open结构为Version(1字节)、My Autonomous System(2字节)、Hold Time(2字节)、BGP Identifier(4字节,BGP IP)、Optional Parameters Length(1字节)、Optional Parameters(变长)
=> RFC4271 Sec 4.2
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+
| Version |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| My Autonomous System |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Hold Time |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| BGP Identifier |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Opt Parm Len |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Optional Parameters (variable) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-...
| Parm. Type | Parm. Length | Parameter Value (variable)
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-...
Optional Parameters内部结构为1个或多个<Parameter Type, Parameter Length, Parameter Value>三元组(1字节,1字节,x字节)
This field contains a list of optional parameters, in which each parameter is encoded as a <Parameter Type, Parameter Length, Parameter Value> triplet.
Parameter Type可以为Reserved(Parameter Type 0)、Authentication (deprecated)(Parameter Type 1)、Capability Codes(Parameter Type 2)
最常用的还是Capability Codes,其内部格式为1个或多个<Capability Code, Capability Length, Capability Value>三元组
=> RFC3392 Sec 4.
+------------------------------+
| Capability Code (1 octet) |
+------------------------------+
| Capability Length (1 octet) |
+------------------------------+
| Capability Value (variable) |
+------------------------------+
其中Capability-Code可为如下的值
整个结构如下
graph LR
X(Optional Parameters)--> T1(Opt Param tuple)
T1(Opt Param tuple)--> A("Parameter Type(if Capability Codes)")
T1(Opt Param tuple)--> B(Parameter Length)
T1(Opt Param tuple)--> C(Parameter Value)
C(Parameter Value)--> CT1(Cap Code tuple)
CT1(Cap Code tuple)--> CTA(Capability Code)
CT1(Cap Code tuple)--> CTB(Capability Length)
CT1(Cap Code tuple)--> CTC(Capability Value)
C(Parameter Value)--> CT2(...)
X(Optional Parameters)--> T2(...)
(1) Multiprotocol Extensions for BGP-4
Capability Code字段为1Capability Length字段为变长Capability Value如下:Address Family Identifier(2字节)、Reserved(1字节,默认为0)、Subsequent Address Family Identifier(1字节)
0 7 15 23 31
+-------+-------+-------+-------+
| AFI | Res. | SAFI |
+-------+-------+-------+-------+
(2) Route Refresh Capability for BGP-4
Capability Code字段为2Capability Length字段为0
=> RFC2918 Sec 2.
(3) Outbound Route Filtering Capability
Capability Code字段为3Capability Length字段为变长Capability Value字段为一个或者多个如下结构
+--------------------------------------------------+
| Address Family Identifier (2 octets) |
+--------------------------------------------------+
| Reserved (1 octet) |
+--------------------------------------------------+
| Subsequent Address Family Identifier (1 octet) |
+--------------------------------------------------+
| Number of ORFs (1 octet) |
+--------------------------------------------------+
| ORF Type (1 octet) |
+--------------------------------------------------+
| Send/Receive (1 octet) |
+--------------------------------------------------+
| ... |
+--------------------------------------------------+
| ORF Type (1 octet) |
+--------------------------------------------------+
| Send/Receive (1 octet) |
+--------------------------------------------------+
其中Send/Receive值只能为1、2、3
=> RFC5291 Sec 5.
(4) Multiple routes to a destination capability (deprecated)
Capability Code字段为4
但是被弃用
=> RFC8277 Sec 6.
(5) Extended Next Hop Encoding
Capability Code字段为5Capability Length字段为变长
+-----------------------------------------------------+
| NLRI AFI - 1 (2 octets) |
+-----------------------------------------------------+
| NLRI SAFI - 1 (2 octets) |
+-----------------------------------------------------+
| Nexthop AFI - 1 (2 octets) |
+-----------------------------------------------------+
| ..... |
+-----------------------------------------------------+
| NLRI AFI - N (2 octets) |
+-----------------------------------------------------+
| NLRI SAFI - N (2 octets) |
+-----------------------------------------------------+
| Nexthop AFI - N (2 octets) |
+-----------------------------------------------------+
(6) BGP Extended Message
Capability Code字段为6Capability Length字段为0
=> RFC8654 Sec 3.
(7) BGPsec Capability
Capability Code字段为7Capability Length字段为3Capability Value字段如下
0 1 2 3 4 5 6 7
+---------------------------------------+
| Version | Dir | Unassigned |
+---------------------------------------+
| |
+------ AFI -----+
| |
+---------------------------------------+
(8) Multiple Labels Capability
Capability Code字段为8Capability Length字段为变长Capability Value字段为一个或多个<AFI, SAFI, Count>三元组
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| AFI | SAFI | Count ~
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
~ AFI | SAFI | Count |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
(9) BGP Role
Capability Code字段为9Capability Length字段为1Capability Value字段如下
Value Role name (for the local AS)
0 Provider
1 RS
2 RS-Client
3 Customer
4 Peer (i.e., Lateral Peer)
5-255 Unassigned
(64) Graceful Restart Capability
Capability Code字段为64Capability Length字段为变长Capability Value字段为ReStart Flags、Restart Time in seconds和一个或多个<AFI, SAFI, Flags for Address Family>三元组
+--------------------------------------------------+
| Restart Flags (4 bits) |
+--------------------------------------------------+
| Restart Time in seconds (12 bits) |
+--------------------------------------------------+
| Address Family Identifier (16 bits) |
+--------------------------------------------------+
| Subsequent Address Family Identifier (8 bits) |
+--------------------------------------------------+
| Flags for Address Family (8 bits) |
+--------------------------------------------------+
| ... |
+--------------------------------------------------+
| Address Family Identifier (16 bits) |
+--------------------------------------------------+
| Subsequent Address Family Identifier (8 bits) |
+--------------------------------------------------+
| Flags for Address Family (8 bits) |
+--------------------------------------------------+
Restart Flags:
0 1 2 3
+-+-+-+-+
|R|Resv.|
+-+-+-+-+
Flags for Address Family:
0 1 2 3 4 5 6 7
+-+-+-+-+-+-+-+-+
|F| Reserved |
+-+-+-+-+-+-+-+-+
(65) Support for 4-octet AS number capability
Capability Code字段为65Capability Length字段为4Capability Value字段为4字节的AS number
=> RFC6793 Sec 3.
(67) Support for Dynamic Capability (capability specific)
Capability Code字段为67Capability Length字段为变长Capability Value字段是一串capability codes,每个一字节
The Capability Value field consists of a list of capability codes (one-octet for each) that specify the capabilities that MAY be revised dynamically by the remote speaker.
=> draft-ietf-idr-dynamic-cap-16 Sec 2.
(68) Multisession BGP Capability
Capability Code字段为68Capability Length字段为变长Capability Value字段是一个Flags(G+Reserved)和0个或多个capability codes用于区别不同的组
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|G| Reserved | Session Id ~
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
=> draft-ietf-idr-bgp-multisession-07 Sec 4.
(69) ADD-PATH Capability
Capability Code字段为69Capability Length字段为变长Capability Value字段为一个或多个<AFI, SAFI, Send/Receive>三元组
+------------------------------------------------+
| Address Family Identifier (2 octets) |
+------------------------------------------------+
| Subsequent Address Family Identifier (1 octet) |
+------------------------------------------------+
| Send/Receive (1 octet) |
+------------------------------------------------+
(70) Enhanced Route Refresh Capability
Capability Code字段为70Capability Length字段为0
=> RFC7313 Sec 3.1.
(71) Long-Lived Graceful Restart (LLGR) Capability
Capability Code字段为71Capability Length字段为变长Capability Value字段为0个或多个<AFI, SAFI, Flags, Long-lived Stale Time>四元组
+--------------------------------------------------+
| Address Family Identifier (16 bits) |
+--------------------------------------------------+
| Subsequent Address Family Identifier (8 bits) |
+--------------------------------------------------+
| Flags for Address Family (8 bits) |
+--------------------------------------------------+
| Long-lived Stale Time (24 bits) |
+--------------------------------------------------+
| ... |
+--------------------------------------------------+
| Address Family Identifier (16 bits) |
+--------------------------------------------------+
| Subsequent Address Family Identifier (8 bits) |
+--------------------------------------------------+
| Flags for Address Family (8 bits) |
+--------------------------------------------------+
| Long-lived Stale Time (24 bits) |
+--------------------------------------------------+
=> RFC-ietf-idr-long-lived-gr-06 Sec 3.1
(72) Routing Policy Distribution
Capability Code字段为72Capability Length字段为变长Capability Value字段为1个或多个<AFI, SAFI, Send/Receive>三元组
+--------------------------------------------------+
| Address Family Identifier (2 octets) |
+--------------------------------------------------+
| Subsequent Address Family Identifier (1 octet) |
+--------------------------------------------------+
| Send/Receive (1 octet) |
+--------------------------------------------------+
=> draft-ietf-idr-rpd-04 Sec 4.3
(128) Prestandard Route Refresh (deprecated)
同 (2) Route Refresh Capability for BGP-4
(130) Prestandard Outbound Route Filtering (deprecated)
同 (3) Outbound Route Filtering Capability
UPDATE Message Format
UPDATE Message主要用来构建描述各种自治域的关系图,用于向peer通告共享公共路径属性的可行路由或者撤回多个不可行路由
update结构为Withdrawn Routes Length(2字节)、Withdrawn Routes(变长)、Total Path Attribute Length(2字节)、Path Attributes(变长,其长度值为前一个字段)、Network Layer Reachability Information(变长,其长度值为Header长度减去Total Path Attribute Length)
+-----------------------------------------------------+
| Withdrawn Routes Length (2 octets) |
+-----------------------------------------------------+
| Withdrawn Routes (variable) |
+-----------------------------------------------------+
| Total Path Attribute Length (2 octets) |
+-----------------------------------------------------+
| Path Attributes (variable) |
+-----------------------------------------------------+
| Network Layer Reachability Information (variable) |
+-----------------------------------------------------+
Withdrawn Routes中是一些IP address prefix,每个IP address prefix都是<length, prefix>的二元组
+---------------------------+
| Length (1 octet) |
+---------------------------+
| Prefix (variable) |
+---------------------------+
每个Path Attributes都是一组<attribute type, attribute length, attribute value>三元组
Attribute Type是二个字节的字段。Attr. Flags、Attr. Type Code均一个字节
0 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Attr. Flags |Attr. Type Code|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Attr. Flags每个bits的0/1均有不同的含义,例如第四个bit(bit 3)表示是否是一个Extended长度的比特位,如果为0,则Attribute Length是1个字节,如果为1,则是2个字节
Attr. Type Code值如下

整个结构如下
graph LR
X(Path Attributes)--> T1(tuple)
T1(tuple)--> A(attribute type)
A-->A1(Attr. Flags)
A-->A2(Attr. Type Code)
T1(tuple)--> B(attribute length)
T1(tuple)--> C(attribute value)
X(Path Attributes)--> T2(...)
(1) ORIGIN
Type Code为1Attribute Len为1Attribute Value可以为IGP(0),EGP(1),INCOMPLETE(2)
(2) AS_PATH
Type Code为2Attribute Len为变长Attribute Value为一组AS path segments,每一个AS path segments表示为<path segment type(1字节), path segment length(1字节), path segment value(变长)>三元组,
path segment type可以为AS_SET(1),AS_SEQUENCE(2)path segment length为ASes的个数(不是path segment value的字节长度)path segment value为一个或多个AS编号,每个编号2个字节
整个结构如下
graph LR
X(Attribute Value)--> T1(AS path segments)
T1(AS path segments)--> A(path segment type)
T1(AS path segments)--> B(path segment length)
T1(AS path segments)--> C(path segment value)
C(path segment value)--> C1(AS number)
C(path segment value)--> C2(...)
X(Path Attributes)--> T2(...)
(3) NEXT_HOP
Type Code为3Attribute Len为4Attribute Value为IP address
(4) MULTI_EXIT_DISC
Type Code为4Attribute Len为4Attribute Value为4个字节的无符号整数,用于区别邻居自治域的入口点
(5) LOCAL_PREF
Type Code为5Attribute Len为4Attribute Value为4个字节的无符号整数,用于通知内部对等节点,广告路由的偏好程度(不是很理解)
(6) ATOMIC_AGGREGATE
Type Code为6Attribute Len为0
(7) AGGREGATOR
Type Code为7Attribute Len为6(某些实现下为8,即AS number(4字节))Attribute Value为the last AS number(2字节),the IP address(4字节)
(8) COMMUNITIES
Type Code为8Attribute Len为变长Attribute Value为一组4字节的community
(9) ORIGINATOR_ID
Type Code为9Attribute Len为4Attribute Value为本地自治域的BGP Identifier
ORIGINATOR_ID和CLUSTER_LIST一起是防止路由环路的
(10) CLUSTER_LIST
Type Code为10Attribute Len为变长Attribute Value为一组4字节的CLUSTER_ID
(14) MP_REACH_NLRI
Type Code为14Attribute Len为变长Attribute Value为AFI(2字节)、SAFI(1字节)、Length of Next Hop Network Address(1字节)、Network Address of Next Hop(变长)、Reserved(1字节)、NLRI(变长)
Network Address of Next Hop和NLRI均由<AFI, SAFI>标识
+---------------------------------------------------------+
| Address Family Identifier (2 octets) |
+---------------------------------------------------------+
| Subsequent Address Family Identifier (1 octet) |
+---------------------------------------------------------+
| Length of Next Hop Network Address (1 octet) |
+---------------------------------------------------------+
| Network Address of Next Hop (variable) |
+---------------------------------------------------------+
| Reserved (1 octet) |
+---------------------------------------------------------+
| Network Layer Reachability Information (variable) |
+---------------------------------------------------------+
(15) MP_UNREACH_NLRI
Type Code为15Attribute Len为变长Attribute Value为AFI(2字节)、SAFI(1字节)、Withdrawn Routes(变长)
+---------------------------------------------------------+
| Address Family Identifier (2 octets) |
+---------------------------------------------------------+
| Subsequent Address Family Identifier (1 octet) |
+---------------------------------------------------------+
| Withdrawn Routes (variable) |
+---------------------------------------------------------+
Withdrawn Routes由<AFI, SAFI>标识
(16) EXTENDED COMMUNITIES
Type Code为16Attribute Len为变长Attribute Value为一系列extended communities,每个Extended Community均是8个字节的如下结构
如果是Regular type,则是1个字节,如果是Extended type,则是2个字节
- Type Field : 1 or 2 octets
- Value Field : Remaining octets
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type high | Type low(*) | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Value |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
(17) AS4_PATH
Type Code为17Attribute Len为4Attribute Value为4字节的AS4_PATH
(18) AS4_AGGREGATOR
Type Code为18Attribute Len为8Attribute Value为the last AS number(4字节),the IP address(4字节)
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!
