我查了一下百度百科和维基百科,两者关于 AS(autonomous system)的解释,我不敢说他写得不对,我只能说,写得有点晦涩难懂。(关于 AS, 百度百科实际上是翻译的维基百科)。笔者结合各种资料,给出一个 AS 的解释吧(是解释,不是定义):
l 在一个 AS 内,只运行一种 IGP(如果说,就是要运行两种 IGP,这属于抬杠。如果一个 AS 内运行两种 IGP,并且相安无事,那是 IGP 的本事,如果出现了问题,那也是 IGP 的问题。抛开抬杠这一想法,那么,一个 AS 内,就是只运行一种 IGP)
l 在一个 AS 内运行的 IGP,与其他 AS 内的路由器无关。也就是说,在 IGP 这个层面,不同 AS 的路由器,老死不相往来。
l 在一个 AS 内,除了有相同的 IGP(因为只有一种,所以相同)外,还可以制定自己独立的路由策略和安全策略,与其他 AS 无关。
换个角度说,如果没有 BGP(后面我们会讲述 BGP),那么一个个 AS,就是一个个网络孤岛:内部有自己独立的 IGP 和路由策略、安全策略,外部不与其他 AS 交往。
关于 BGP 如何解决 AS 孤岛问题,我们会在下面讲述,这里暂时不要着急,先看看 AS 有关的信息。
区分不同 AS 的是 AS ID(或者叫 AS 号码)。AS 号码的分配,是由 Internet Assigned Number Authority (IANA) 机构来统一规划和分配。在 2009 年 1 月之前,只能使用最多 2 字节 长度的 AS 号码,即 1-65535,在 2009 年 1 月之后,(IANA)决定使用 4 字节长度 AS, 范围是 65536 -4294967295。当前,通常还是使用 2 字节长度的 AS,也就是 1-65535。
AS 号码划分为公有 AS 和私有 AS,公有 AS 的范围是 1-64511,私有 AS 范围是 64512-65534;公有 AS 只能 用于互联网,并且全球唯一,不可重复,而私有 AS 可以在得不到合法 AS 的企业网 络使用,可以重复。很显然,因为私有 AS 可以被多个企业网络重复使用,所以这 些私有 AS 不允许传入互联网,ISP 在企业用户边缘,需要过滤掉带有私有 AS 号码 的路由条目。
IANA 分配给中国的 AS 号有一百多个(117个?),我们随便举几个例子:
3.4.3 BGP 的报文格式
上一节介绍了 BGP 的基本机制,这一节我们看看 BGP 的报文格式。本小节如无特别说明,所有内容都是覆盖 eBGP 和 iBGP。
BGP 一共有四种消息类型:
l Open: 用于建立BGP对等体之间的连接关系
l Keepalive: 周期性地向BGP对等体发出Keepalive消息, 用来保持连接的有效性
l Update: 携带的是路由更新(删减、增加)信息
l Notification: 当BGP检测到错误状态时, 就向对等体发出Notification消息, 之后BGP连接会立即被关闭
我们在前面章节的介绍中,涉及到了两种消息:Open 与 Update,只不过当时我们是从基本运行机制的角度所做的讲述。
BGP 的报文,承载在 TCP 连接上,其端口号是 179,如下图所示:
图16 BGP 承载在 TCP 上
3.4.3.1 BGP 报文头格式
BGP 所有消息的报文头,都是一样的格式,如下图所示:
图17 BGP Header 报文格式
关于这些字段的解释,我犹豫了一下,还是决定,直接 copy RFC 4271,必要的地方我加些注释吧。
l Marker: This 16-octet field is included for compatibility; it MUST be set to all ones.(全设置为1,其余不用管)
l Length: This 2-octet unsigned integer indicates the total length of the message, including the header in octets. Thus, it allows one to locate the (Marker field of the) next message in the TCP stream. The value of the Length field MUST always be at least 19 and no greater than 4096, and MAY be further constrained, depending on the message type. "padding" of extra data after the message is not allowed. Therefore, the Length field MUST have the smallest value required, given the rest of the message.(整个的报文长度,包括报文头长度在内。)
l Type: This 1-octet unsigned integer indicates the type code of the message. This document defines the following type codes:(报文类型,一共四种)
1 - OPEN
2 - UPDATE
3 - NOTIFICATION
4 - KEEPALIVE
3.4.3.2 BGP Update消息报文格式
BGP 四种消息,笔者以为,其余三种都不是重点,我们只须关注 Update 消息。因为这个就是著名的路由消息通告的消息。
BGP 将自身所知道的路由信息(存放在 BGP 路由表中),通过 Update 消息,通告给它的邻居(peer)。值得一提的是,由于 BGP 是基于 TCP 协议,所以它每次只是通告“增量”路由消息,而不是全量。这意味着:第一次将自己所有 BGP 路由信息告知对端以后,以后再通告,只须通告“变化”的路由信息即可。
笔者在网上看到一道题,我们一起来做一下:
在 BGP 协议中,每隔()时间向邻居发送路由更新信息
A. 30 秒
B. 60 秒
C. 120 秒
D. 无固定周期
BGP 是在什么时机向邻居发送路由更新信息的呢?
......
这个报文结构,分为三部分:
l 撤销的路由信息。包含两个字段:Withdrawn Routes Length,Withdrawn Routes。当上次通告的路由,有些路由无效时,需要靠这两个字段撤销(取消)。(已经无效的路由,得通知别人啊)
l 新增路由的属性。包含两个字段:Total Path Attribute Length,Path Attributes。新增的路由,除了目的网段以外,还有许多其他相关的信息。(这个我们会在下一节专门描述)
l 新增路由的信息。包含一个字段:Network Layer Reachability Information。表达的是目的网段信息。意思是,这些网段的路由是可达的。
路由属性比较复杂,我们放在下一小节专门描述。本小节,只讲述另外两个。
3.4.3.2.2 撤销路由信息
前面说过,路由通告,通告的是变化信息。消息类型(Update)本身,也明确了这一点。既然是变化信息,就有可能是增加,也有可能是删减。当有些路由无效时,那么告知对方的变化信息就是“删减”。BGP 使用“Withdrawn Routes Length,Withdrawn Routes”这两个字段告知邻居路由的删减信息。
我们先贴一下 RFC 4271 的官方说明文字:
l Withdrawn Routes Length:
Ø This 2-octets unsigned integer indicates the total length of the Withdrawn Routes field in octets. Its value allows the length of the Network Layer Reachability Information field to be determined, as specified below.
Ø A value of 0 indicates that no routes are being withdrawn from service, and that the WITHDRAWN ROUTES field is not present in this UPDATE message.
l Withdrawn Routes:
Ø This is a variable-length field that contains a list of IP address prefixes for the routes that are being withdrawn from service. Each IP address prefix is encoded as a 2-tuple of the form <length, prefix>, whose fields are described below:(图不贴了,我下面会另外贴图)
Ø The use and the meaning of these fields are as follows:
ü a) Length: The Length field indicates the length in bits of the IP address prefix. A length of zero indicates a prefix that matches all IP addresses (with prefix, itself, of zero octets).
ü b) Prefix: The Prefix field contains an IP address prefix, followed by the minimum number of trailing bits needed to make the end of the field fall on an octet boundary. Note that the value of trailing bits is irrelevant.
因为 Withdrawn Routes 是个变长的字段,所以首先需要一个字段“Withdrawn Routes Length”来表示 Withdrawn Routes 字段的长度。如果长度为0,则表示没有需要撤销的路由。
而 Withdrawn Routes Length,是一个列表,包含 N 个二元组(Length, Prefix),它的报文结构如下图所示: