简介
Null 接口通常用于防止路由环路。例如,增强型内部网关路由协议 (EIGRP) 总是在汇总一组路由时创建一个通往 Null0 接口的路由。当路由协议进行汇总时,就意味着路由器可能会收到针对该汇总内任何 IP 地址的流量。由于并非所有 IP 地址都在使用中,所以如果收到汇总流量的路由器使用默认路由,就可能造成数据包循环。
先决条件
要求
本文档没有任何特定的前提条件。
使用的组件
本文档中的信息基于以下软件和硬件版本:
本文档中的信息都是基于特定实验室环境中的设备创建的。本文档中使用的所有设备最初均采用原始(默认)配置。如果您是在真实网络上操作,请确保您在使用任何命令前已经了解其潜在影响。
规则
有关文档规则的详细信息,请参阅 Cisco 技术提示规则。
命令语法
指向 Null0 的静态路由是普通的静态路由,但是它指向的 Null0 接口是一个虚拟 IOS 接口。请参阅本章的ip route部分:IP Routing Protocol-Independent Commands A至R(IP路由协议无关命令A至R),了解有关ip route命令的详细信息。下一部分举例说明如何使用 ip route 命令来创建指向 Null0 的静态路由。
示例
最常见的情况是,有很多客户端拨入同一个接入服务器,这时就需要添加一个指向 Null0 的静态路由。这种情况会导致主机路由被安装到接入服务器的路由表中。为确保客户端的可达性,同时又不至于造成整个网络充斥着主机路由,网络中的其他路由器通常会有指向接入服务器的汇总路由。在这种配置下,接入服务器应该具有指向接入服务器 Null0 接口的相同汇总路由。否则,当外部主机试图访问目前尚未分配给拨入客户端、但已经包含在汇总路由中的 IP 地址时,就会出现路由环路。这是因为接入服务器由于缺少通往目标的主机路由,而通过接入服务器的默认路由将数据包反弹回核心网络。
请考虑以下示例:
一个小型ISP(ISP-R1)为其客户的一个网络块192.168.0.0/16。在本例中,客户将192.168.0.0/16划分到/24网络中,目前仅使用192.168.1.0/24和192.168.2.0/24。在路由器 ISP-R1 上,ISP 为 192.168.0.0/16 配置了一个通往客户路由器 (cust-R2) 的静态路由。 然后,ISP连接到由路由器BB-R3表示的主干ISP。路由器BB-R3向ISP-R1发送默认路由,并通过BGP从ISP-R1接收网络192.168.0.0/16。
由于cust-R2配置了指向ISP-R1的默认路由,因此现在可以保证从Internet(主干ISP路由器BB-R3)到客户路由器cust-R2的可达性。但是,如果数据包的目的地是未在192.168.0.0/16范围之外使用的网络块,则cust-R2路由器使用到ISP-Rr1转发这些数据包。然后,数据包在 ISP-R1 和 cust-R2 之间形成环路,直到 TTL 超时。这会对路由器的 CPU 和链路利用率造成巨大的影响。例如,对于未使用的 IP 地址,它的流量可能会来源于拒绝服务攻击或者扫描 IP 地址块以便查找易受攻击主机等。
相关配置:
cust-R2 |
version 12.3
!
hostname cust-R2
!
ip subnet-zero
!
interface Loopback0
ip address 10.2.2.2 255.255.255.255
!
interface Ethernet0/0
ip address 192.168.1.1 255.255.255.0
!
interface Ethernet1/0
ip address 192.168.2.1 255.255.255.0
!
interface Serial2/0
ip address 10.0.0.2 255.255.255.252
!--- This interface leads to ISP-R1.
!
ip classless
ip route 0.0.0.0 0.0.0.0 10.0.0.1
!--- Default route going to ISP-R1.
!
end |
ISP-R1 |
version 12.3
!
hostname ISP-R1
!
ip subnet-zero
!
interface Loopback0
ip address 10.1.1.1 255.255.255.255
!
interface Serial0/0
ip address 10.0.0.1 255.255.255.252
!--- Interface to cust-R2.
!
interface Serial1/0
ip unnumbered Loopback0
!--- Interface going to BB-R3.
!
router bgp 65501
no synchronization
network 192.168.0.0 mask 255.255.0.0
!--- ISP-R1 injects 192.168.0.0/16 into BGP to !--- advertise it to BB-R3.
neighbor 10.3.3.3 remote-as 65503
neighbor 10.3.3.3 ebgp-multihop 255
no auto-summary
!
ip classless
ip route 10.3.3.3 255.255.255.255 Serial1/0
ip route 192.168.0.0 255.255.0.0 Serial0/0
!--- The first route is necessary for the eBGP !--- session to BB-R3 to come up.
!--- The route to 192.168.0.0/16 points towards cust-R2.
!
!
end |
BB-R3 |
version 12.3
!
hostname BB-R3
!
ip subnet-zero
!
!
interface Loopback0
ip address 10.3.3.3 255.255.255.255
!
interface Serial2/0
ip unnumbered Loopback0
!--- This interface goes to ISP-R1.
!
router bgp 65503
no synchronization
bgp log-neighbor-changes
neighbor 10.1.1.1 remote-as 65501
neighbor 10.1.1.1 ebgp-multihop 255
neighbor 10.1.1.1 default-originate
!--- BB-R3 injects a default route into BGP and !--- sends it to ISP-R1.
no auto-summary
!
ip classless
ip route 10.1.1.1 255.255.255.255 Serial2/0
!--- This route points to ISP-R1 and is !--- used to establish the eBGP peering.
!
end |
数据包流:
注:我们在路由器上启用了一些debug命令,以更好地说明数据包流,特别是debug ip packet和debug ip icmp。除非已经充分了解后果,否则切勿在生产环境中启用这些命令。
BB-R3# ping ip 192.168.20.1 repeat 1
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 192.168.20.1, timeout is 2 seconds:
*Oct 6 09:36:45.355: IP: tableid=0, s=10.3.3.3 (local), d=192.168.20.1 (Serial2/0), routed via FIB
*Oct 6 09:36:45.355: IP: s=10.3.3.3 (local), d=192.168.20.1 (Serial2/0), len 100, sending.
Success rate is 0 percent (0/1)
BB-R3#
*Oct 6 09:36:50.943: ICMP: time exceeded rcvd from 10.0.0.1
BB-R3向192.168.0.0/16地址块内未在cust-R2上使用的IP地址发送单个ICMP请求。BB-R3从ISP-R1接收ICMP超时返回。
在 ISP-R1 上:
18:50:22: IP: tableid=0, s=10.3.3.3 (Serial1/0), d=192.168.20.1 (Serial0/0), routed via RIB
18:50:22: IP: s=10.3.3.3 (Serial1/0), d=192.168.20.1 (Serial0/0), g=192.168.20.1, len 100, forward
18:50:22: IP: tableid=0, s=10.3.3.3 (Serial0/0), d=192.168.20.1 (Serial0/0), routed via RIB
18:50:22: IP: s=10.3.3.3 (Serial0/0), d=192.168.20.1 (Serial0/0), g=192.168.20.1, len 100, forward
18:50:22: IP: tableid=0, s=10.3.3.3 (Serial0/0), d=192.168.20.1 (Serial0/0), routed via RIB
18:50:22: IP: s=10.3.3.3 (Serial0/0), d=192.168.20.1 (Serial0/0), g=192.168.20.1, len 100, forward
18:50:22: IP: tableid=0, s=10.3.3.3 (Serial0/0), d=192.168.20.1 (Serial0/0), routed via RIB
ISP-R1上的serial1/0收到来自BB-R3的初始数据包,并按预期转发到serial0/0上的cust-R2。由于以下路由,同一数据包返回到 ISP-R1 的 serial0/0 上,并立即通过同一接口发送到 cust-R2。
ISP-R1# show ip route 192.168.20.1
Routing entry for 192.168.0.0/16, supernet
Known via "static", distance 1, metric 0 (connected)
Advertised by bgp 65501
Routing Descriptor Blocks:
* directly connected, via Serial0/0
Route metric is 0, traffic share count is 1
是什么导致 cust-R2 把流量发送回 ISP-R1 呢?
在 cust-R2 上:
*Oct 6 09:41:43.495: IP: s=10.3.3.3 (Serial2/0), d=192.168.20.1 (Serial2/0), g=10.0.0.1, len 100, forward
*Oct 6 09:41:43.515: IP: tableid=0, s=10.3.3.3 (Serial2/0), d=192.168.20.1 (Serial2/0), routed via RIB
*Oct 6 09:41:43.515: IP: s=10.3.3.3 (Serial2/0), d=192.168.20.1 (Serial2/0), g=10.0.0.1, len 100, forward
*Oct 6 09:41:43.555: IP: tableid=0, s=10.3.3.3 (Serial2/0), d=192.168.20.1 (Serial2/0), routed via RIB
我们看到,由于以下路由,cust-R2 将这些数据包发送回 ISP-R1:
cust-R2# show ip route 192.168.20.1 longer-prefixes
Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2
i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
ia - IS-IS inter area, * - candidate default, U - per-user static route
o - ODR, P - periodic downloaded static route
Gateway of last resort is 10.0.0.1 to network 0.0.0.0
cust-R2#
由于客户网络中没有使用此网络,路由器 cust-R2 没有指向 192.168.20.1 的路由,因此指向 192.168.20.1 的最佳路由将采用默认路由,而默认路由则指向 ISP-R1。
结果是,数据包在 ISP-R1 和 cust-R2 之间循环,直到 TTL 超时。
注意,如果 ICMP 请求的是网络中已经使用的 IP 地址,就不会出现此结果。例如,如果 ICMP 请求的是 192.168.1.x,而该地址直接连接到 cust-R2,则不会出现环路:
cust-R2# show ip rou 192.168.1.1
Routing entry for 192.168.1.0/24
Known via "connected", distance 0, metric 0 (connected, via interface)
Routing Descriptor Blocks:
* directly connected, via Ethernet0/0
Route metric is 0, traffic share count is 1
此问题的解决方案是,在 cust-R2 上为 192.168.0.0/16 配置指向 Null0 的静态路由。
cust-R2# conf t
Enter configuration commands, one per line. End with CNTL/Z.
cust-R2(config)# ip route 192.168.0.0 255.255.0.0 Null0
cust-R2(config)# end
cust-R2#
*Oct 6 09:53:18.015: %SYS-5-CONFIG_I: Configured from console by console
cust-R2# show ip route 192.168.20.1
Routing entry for 192.168.0.0/16, supernet
Known via "static", distance 1, metric 0 (connected)
Routing Descriptor Blocks:
* directly connected, via Null0
Route metric is 0, traffic share count is 1
现在,如果我们再从 BB-R3 向 192.168.20.1 发出 ICMP 请求,cust-R2 会将此流量发送到 Null0,并因此生成 ICMP 不可达。
BB-R3# p ip 192.168.20.1 repeat 1
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 192.168.20.1, timeout is 2 seconds:
U
Success rate is 0 percent (0/1)
BB-R3#
*Oct 6 09:54:33.051: ICMP: dst (10.3.3.3) host unreachable rcv from 10.0.0.2
注意:在某些情况下,使用到Null0的总结静态路由是不可行的。例如,如果在上一个示例中:
注:结果是,尝试到达此IP地址块的传输中的数据包或应用程序会创建前面所述的相同路由环路。
注意:要修复此路由环路,必须使用ip route 192.168.1.0 255.255.255.0 Null0 200 命令为192.168.1.0/24配置到Null0的浮动静态路由。命令中的200距离为管理距离。请参考什么是管理距离?。
注意:由于我们使用的管理距离比任何路由协议都高,因此,如果通过ISDN链路到达192.168.1.0/24的路由变为非活动状态,cust-R2会安装浮动静态路由。随后,数据包将被发送到 Null0,直到 ISDN 链路变为活动状态为止。
相关信息