本文将在两台openwrt系统上安装配置quagga, 搭建一套完整环境,来验证OSPF动态路由的基本功能和实际效果,验证过程中详细展示了OSPF控制台基本命令使用和配置前后路由信息、OSPF控制台信息的变化,另外对OSPF协议进行了简单分析。
前言
最短路径协议是计算网络中数据包传输的最短路径的一种算法。它用于在网络中选择最佳路径来传输数据,使得数据包能够以最快的速度到达目的地。
最短路径协议在网络中起到了非常重要的作用,它能够提高网络的性能和可靠性,减少数据传输的延迟和丢失。常见的最短路径协议有RIP(Routing Information Protocol)、OSPF(Open Shortest Path First)和BGP(Border Gateway Protocol)等。
在OSPF出现前,网络上广泛使用RIP(Routing Information Protocol)作为内部网关协议。RIP,即路由信息协议,是一个很老的协议,RIP 路由器向网络中周期性多播它的整个路由表,而不是像 OSPF 那样只多播网络的变化。RIP 通过跳数来测量路由,任何超过 15 跳的路由它均视为不可到达。RIP 设置很简单,但是 OSPF 在速度、效率以及弹性方面更佳。由于RIP是基于距离矢量算法的路由协议,存在着收敛慢、路由环路、可扩展性差等问题,所以逐渐被OSPF取代。
OSPF 的意思是 最短路径优先(Open Shortest Path First)。OSPF 是一个内部网关协议(IGP);它可以用在局域网和跨因特网的局域网互联中。在你的网络中的每个 OSPF 路由器都包含整个网络的拓扑,并计算通过网络的最短路径。OSPF 会通过多播的方式自动对外传播它检测到的网络变化。你可以将你的网络分割为区域,以保持路由表的可管理性;每个区域的路由器只需要知道离开它的区域的下一跳接口地址,而不用记录你的网络的整个路由表。
Quagga 是一个支持 OSPFv2、OSPFv3、RIP v1 和 v2、RIPng、以及 BGP-4 的路由协议套件,并全部由 zebra 守护程序管理。OSPF作为基于链路状态的协议,能够解决RIP所面临的诸多问题。此外,OSPF还有以下优点:
- OSPF采用组播形式收发报文,这样可以减少对其它不运行OSPF路由器的影响。
- OSPF支持无类型域间选路(CIDR)。
- OSPF支持对等价路由进行负载分担。
- OSPF支持报文加密。
网络拓扑
本实验涉及四台设备,2台路由器R1、R2, 2台主机,R1/R2路由器分别使用两台嵌入式ARM板来模拟, Host1为一台ubuntu嵌入式板,host2为windows PC。 各网元网口及地址信息如下图所示。以下将分别采用静态路由和动态路由两种方式验证如何打通三个网段。
静态路由方式
为与动态路由方式进行对照,先采用静态路由方式实现跨网段互通。然后再用动态路由的方式达到相同的效果。
各网元IP及初始路由表信息如下:
#R1 IP信息
eth1 UP 192.168.2.2/24
br-lan UP 192.168.1.1/24 #eth0
#R1 router信息
192.168.1.0/24 dev br-lan proto kernel scope link src 192.168.1.1
192.168.2.0/24 dev eth1 proto kernel scope link src 192.168.2.2
# R2 IP信息
# ip -br a
eth0 UP 192.168.3.1/24
eth1 UP 192.168.1.104/24
# R2 router信息
# ip r
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.104
192.168.3.0/24 dev eth0 proto kernel scope link src 192.168.3.1
# host1 IP信息
# ip -br a
eth0 UP 192.168.2.1/24
#host1 router 信息
192.168.2.0/24 dev eth0 proto kernel scope link src 192.168.2.1 metric 100
在各个设备上配置静态路由,打通各网段
#R1
ip r a 192.168.3.0/24 via 192.168.1.104 dev br-lan
ip r
192.168.1.0/24 dev br-lan proto kernel scope link src 192.168.1.1
192.168.2.0/24 dev eth1 proto kernel scope link src 192.168.2.2
192.168.3.0/24 via 192.168.1.104 dev br-lan
#R2
ip r a 192.168.2.0/24 via 192.168.1.1 dev eth1
ip r
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.104
192.168.2.0/24 via 192.168.1.1 dev eth1
192.168.3.0/24 dev eth0 proto kernel scope link src 192.168.3.1
#host1
ip r a 192.168.1.0/24 via 192.168.2.2 dev eth0
ip r a 192.168.3.0/24 via 192.168.2.2 dev eth0
# ip r
192.168.1.0/24 via 192.168.2.2 dev eth0
192.168.2.0/24 dev eth0 proto kernel scope link src 192.168.2.1 metric 100
192.168.3.0/24 via 192.168.2.2 dev eth0
#host2
以太网适配器 以太网 2:
子网掩码 . . . . . . . . . . . . : 255.255.255.0
IPv4 地址 . . . . . . . . . . . . : 192.168.3.2
ROUTE.EXE add 192.168.1.0/24 192.168.3.1 IF 9
ROUTE.EXE add 192.168.2.0/24 192.168.3.1 IF 9
ROUTE.EXE print
192.168.1.0 255.255.255.0 192.168.3.1 172.17.10.223 26
192.168.2.0 255.255.255.0 192.168.3.1 172.17.10.223 26
#完成以上配置后,可在Host1上验证以下ping,检查各网段之间互通情况
#验证以下ping
ping 192.168.1.1
ping 192.168.1.104
ping 192.168.3.1
ping 192.168.3.2
#完成以上配置后,可在Host2上验证以下ping,检查各网段之间互通情况
C:\Windows\System32>ping 192.168.1.1
正在 Ping 192.168.1.1 具有 32 字节的数据:
来自 192.168.1.1 的回复: 字节=32 时间=2ms TTL=63
来自 192.168.1.1 的回复: 字节=32 时间=2ms TTL=63
C:\Windows\System32>ping 192.168.2.1
正在 Ping 192.168.2.1 具有 32 字节的数据:
来自 192.168.2.1 的回复: 字节=32 时间=3ms TTL=62
C:\Windows\System32>ping 192.168.2.2
正在 Ping 192.168.2.2 具有 32 字节的数据:
来自 192.168.2.2 的回复: 字节=32 时间=2ms TTL=63
C:\Windows\System32>ping 192.168.1.104
正在 Ping 192.168.1.104 具有 32 字节的数据:
来自 192.168.1.104 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.1.104 的回复: 字节=32 时间=2ms TTL=64
动态路由方式
Openwrt下 Quagga 安装
openwrt 下的Quaaga软件包有多种路由协议软件。验证OSPF需要安装 Quaaga基础模块、ospf 路由协议(quagga-ospfd)、quagga控制台的工具(quagga-vtysh)和维护路由表工具(quagga-zebra)三个组件。
opkg install quagga quagga-ospfd quagga-vtysh quagga-zebra
对于在内网部署,无法连接公网的设备,也可以下载安装包进行离线安装,
opkg download quagga quagga-ospfd quagga-vtysh quagga-zebra libzebra libospf
# ls -lrt *.ipk
-rw-r--r-- 1 root root 3203 Jan 8 2024 quagga_1.2.4-2_aarch64_generic.ipk
-rw-r--r-- 1 root root 99497 Jan 8 2024 quagga-zebra_1.2.4-2_aarch64_generic.ipk
-rw-r--r-- 1 root root 4471 Jan 8 2024 quagga-ospfd_1.2.4-2_aarch64_generic.ipk
-rw-r--r-- 1 root root 127568 Jan 8 2024 quagga-vtysh_1.2.4-2_aarch64_generic.ipk
-rw-r--r-- 1 root root 145428 Jan 12 2024 quagga-libzebra_1.2.4-2_aarch64_cortex-a53.ipk
-rw-r--r-- 1 root root 201491 Jan 12 2024 quagga-libospf_1.2.4-2_aarch64_generic.ipk
#copy 到目标设备,并按顺序安装
opkg install ./quagga-libzebra_1.2.4-2_aarch64_cortex-a53.ipk
opkg install ./quagga-libospf_1.2.4-2_aarch64_generic.ipk
opkg install ./quagga_1.2.4-2_aarch64_generic.ipk
opkg install ./quagga-zebra_1.2.4-2_aarch64_generic.ipk
opkg install ./quagga-ospfd_1.2.4-2_aarch64_generic.ipk
opkg install ./quagga-vtysh_1.2.4-2_aarch64_generic.ipk
Quagga 配置
首先清空R1/R2上的静态路由,恢复到如下的初始状态。
#R1 router信息
192.168.1.0/24 dev br-lan proto kernel scope link src 192.168.1.1
192.168.2.0/24 dev eth1 proto kernel scope link src 192.168.2.2
# R2 router信息
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.104
192.168.3.0/24 dev eth0 proto kernel scope link src 192.168.3.1
R1路由器zebra配置
#配置zebra并启动
echo > /etc/quagga/zebra.conf << "EOF"
hostname test
password zebra
!
access-list vty permit 127.0.0.0/8
access-list vty deny any
!
line vty
access-class vty
EOF
#启动zebra
zebra -d
netstat -tnupl | grep zebra
#登陆zebra后台
telnet 127.0.0.1 2601
router> show ip route
Codes: K - kernel route, C - connected, S - static, R - RIP,
O - OSPF, I - IS-IS, B - BGP, P - PIM, A - Babel,
> - selected route, * - FIB route
C>* 127.0.0.0/8 is directly connected, lo
O 192.168.1.0/24 [110/10] is directly connected, br-lan, 00:14:01
C>* 192.168.1.0/24 is directly connected, br-lan
O 192.168.2.0/24 [110/10] is directly connected, eth1, 00:14:01
C>* 192.168.2.0/24 is directly connected, eth1
#C:代表由网络接口所设置的IP而产生的相关的路由规则
#O: 代表由OSPF学习到的路由
R1路由器ospf配置
echo > /etc/quagga/ospfd.conf << "EOF"
!
!
router ospf
ospf router-id 2.2.2.2
network 192.168.1.0/24 area 2
network 192.168.2.0/24 area 2
!
access-list vty permit 127.0.0.0/8
access-list vty deny any
!
line vty
access-class vty
EOF
#启动ospf
ospfd -d
netstat -tnupl | grep ospf
~# ps |grep ospf
10965 network 2616 S /usr/sbin/ospfd -d
11068 root 1236 R grep ospf
~# netstat -tnupl |grep ospf
tcp 0 0 0.0.0.0:2604 0.0.0.0:* LISTEN 10965/ospfd
tcp 0 0 :::2604 :::* LISTEN 10965/ospfd
#登陆 ospf
vtysh
Hello, this is Quagga (version 1.1.1).
Copyright 1996-2005 Kunihiro Ishiguro, et al.
test#
test# show ip os
OSPF Routing Process, Router ID: 2.2.2.2
Supports only single TOS (TOS0) routes
This implementation conforms to RFC2328
RFC1583Compatibility flag is disabled
OpaqueCapability flag is disabled
Initial SPF scheduling delay 0 millisec(s)
Minimum hold time between consecutive SPFs 50 millisec(s)
Maximum hold time between consecutive SPFs 5000 millisec(s)
Hold time multiplier is currently 2
SPF algorithm last executed 1h45m10s ago
Last SPF duration 109 usecs
SPF timer is inactive
Refresh timer 10 secs
Number of external LSA 0. Checksum Sum 0x00000000
Number of opaque AS LSA 0. Checksum Sum 0x00000000
Number of areas attached to this router: 1
Area ID: 0.0.0.2
Shortcutting mode: Default, S-bit consensus: ok
Number of interfaces in this area: Total: 2, Active: 2
Number of fully adjacent neighbors in this area: 0
Area has no authentication
Number of full virtual adjacencies going through this area: 0
SPF algorithm executed 2 times
Number of LSA 1
Number of router LSA 1. Checksum Sum 0x0000b092
Number of network LSA 0. Checksum Sum 0x00000000
Number of summary LSA 0. Checksum Sum 0x00000000
Number of ASBR summary LSA 0. Checksum Sum 0x00000000
Number of NSSA LSA 0. Checksum Sum 0x00000000
Number of opaque link LSA 0. Checksum Sum 0x00000000
Number of opaque area LSA 0. Checksum Sum 0x00000000
test# show ip ospf route
============ OSPF network routing table ============
N 192.168.1.0/24 [10] area: 0.0.0.2
directly attached to br-lan
N 192.168.2.0/24 [10] area: 0.0.0.2
directly attached to eth1
============ OSPF router routing table =============
============ OSPF external routing table ===========
test# show ip ospf interface
br-lan is up
ifindex 11, MTU 1500 bytes, BW 0 Kbit <UP,BROADCAST,RUNNING,MULTICAST>
Internet Address 192.168.1.1/24, Broadcast 192.168.1.255, Area 0.0.0.2
MTU mismatch detection:enabled
Router ID 2.2.2.2, Network Type BROADCAST, Cost: 10
..
Neighbor Count is 0, Adjacent neighbor count is 0
eth1 is up
ifindex 3, MTU 1500 bytes, BW 0 Kbit <UP,BROADCAST,RUNNING,MULTICAST>
Internet Address 192.168.2.2/24, Broadcast 192.168.2.255, Area 0.0.0.2
MTU mismatch detection:enabled
Router ID 2.2.2.2, Network Type BROADCAST, Cost: 10
...
Neighbor Count is 0, Adjacent neighbor count is 0
test# show ip ospf database
OSPF Router with ID (2.2.2.2)
Router Link States (Area 0.0.0.2)
Link ID ADV Router Age Seq# CkSum Link count
2.2.2.2 2.2.2.2 1138 0x80000007 0xb092 2
R2路由器zebra配置
zebra配置与R1一样。
~# telnet localhost 2601
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Hello, this is Quagga (version 1.2.4).
Copyright 1996-2005 Kunihiro Ishiguro, et al.
User Access Verification
Password:
test>
test>
test> show ip route
Codes: K - kernel route, C - connected, S - static, R - RIP,
O - OSPF, I - IS-IS, B - BGP, P - PIM, A - Babel, N - NHRP,
> - selected route, * - FIB route
O 192.168.1.0/24 [110/10] is directly connected, eth1, 21:14:14
C>* 192.168.1.0/24 is directly connected, eth1
C>* 192.168.3.0/24 is directly connected, eth0
R2路由器ospf配置
先启动ospf, 并查看 ospf的默认配置
#启动ospf
/etc/init.d/quagga start
/etc/init.d/quagga enable
root@test:~# netstat -tnupl | grep ospf
tcp 0 0 0.0.0.0:2604 0.0.0.0:* LISTEN 2959/ospfd
tcp 0 0 :::2604 :::* LISTEN 2959/ospfd
#进入 ospf后台,也可以使用vtysh 命令, 查看 OSPF的默认配置
root@test:~# telnet localhost 2604
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Hello, this is Quagga (version 1.2.4).
Copyright 1996-2005 Kunihiro Ishiguro, et al.
User Access Verification
Password:
test> show ip route
% [OSPF] Unknown command: show ip route
test> show ip ospf route
N 192.168.1.0/24 [10] area: 0.0.0.0
directly attached to eth1
============ OSPF router routing table =============
============ OSPF external routing table ===========
test> show ip ospf neighbor
Neighbor ID Pri State Dead Time Address Interface RXmtL RqstL DBsmL
test> show ip ospf bo
test> show ip ospf border-routers
============ OSPF router routing table =============
可以看到,eth0,eth1的OSPF已经使能,但是没有发现邻居。修改ospf配置如下,增加2个网口的配置
#vim /etc/quagga/ospfd.conf
router ospf
ospf router-id 1.1.1.1
network 192.168.1.0/24 area 2
network 192.168.3.0/24 area 2
default-information originate
修改后重启quagga服务生效。
/etc/init.d/quagga restart
生效后,两次进入 quagga控制台,查看路由器发现结果:
5GCPE# show ip ospf route
============ OSPF network routing table ============
N 192.168.1.0/24 [10] area: 0.0.0.2
directly attached to eth1
N 192.168.3.0/24 [10] area: 0.0.0.2
directly attached to eth0
============ OSPF router routing table =============
============ OSPF external routing table ===========
#已经发现2.2.2.2(R2)
test# show ip ospf database
OSPF Router with ID (1.1.1.1)
Router Link States (Area 0.0.0.0)
Link ID ADV Router Age Seq# CkSum Link count
1.1.1.1 1.1.1.1 11 0x80000003 0xd3a0 1
Summary Link States (Area 0.0.0.0)
Link ID ADV Router Age Seq# CkSum Route
192.168.1.0 1.1.1.1 1 0x80000003 0xf0f1 192.168.1.0/24
192.168.2.0 1.1.1.1 2 0x80000001 0x4e8b 192.168.2.0/24
Router Link States (Area 0.0.0.2)
Link ID ADV Router Age Seq# CkSum Link count
1.1.1.1 1.1.1.1 11 0x80000006 0xd4a0 2
2.2.2.2 2.2.2.2 12 0x80000032 0xa00c 2
Net Link States (Area 0.0.0.2)
Link ID ADV Router Age Seq# CkSum
192.168.1.1 2.2.2.2 13 0x80000001 0xf4e5
#已经发现2.2.2.2(R2)
test# show ip ospf neighbor
Neighbor ID Pri State Dead Time Address Interface RXmtL RqstL DBsmL
2.2.2.2 1 Full/DR 37.051s 192.168.1.1 eth1:192.168.1.104 0 0 0
test# show ip ospf border-routers
============ OSPF router routing table =============
test# show ip ospf interface
eth1 is up
ifindex 3, MTU 1500 bytes, BW 0 Kbit <UP,BROADCAST,RUNNING,MULTICAST>
Internet Address 192.168.1.104/24, Broadcast 192.168.1.255, Area 0.0.0.2
MTU mismatch detection:enabled
Router ID 1.1.1.1, Network Type BROADCAST, Cost: 10
Transmit Delay is 1 sec, State Backup, Priority 1
Designated Router (ID) 2.2.2.2, Interface Address 192.168.1.1
Backup Designated Router (ID) 1.1.1.1, Interface Address 192.168.1.104
Multicast group memberships: OSPFAllRouters OSPFDesignatedRouters
Timer intervals configured, Hello 10s, Dead 40s, Wait 40s, Retransmit 5
Hello due in 5.376s
Neighbor Count is 1, Adjacent neighbor count is 1
此时,查看R1、R2时的路由表,发现OSPF已经为系统添加动态路由,
#R1路由表, 第三条192.168.3.0/24 为ospf自动发现并动态添加
192.168.1.0/24 dev br-lan proto kernel scope link src 192.168.1.1
192.168.2.0/24 dev eth1 proto kernel scope link src 192.168.2.2
192.168.3.0/24 via 192.168.1.104 dev br-lan proto zebra metric 20
#R2路由表, 第二条192.168.2.0/24为ospf自动发现并动态添加
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.104
192.168.2.0/24 via 192.168.1.1 dev eth1 proto zebra metric 20
192.168.3.0/24 dev eth0 proto kernel scope link src 192.168.3.1
在host1/host2上互ping进行验证。
OSPF协议分析
先停止ospf,打开一个抓 包命令,然后再开始 ospf,记录ospf邻居发现过程。
tcpdump -nn -p -i eth1 ip[9] == 89 or arp -w ospf1.pcap -v
当路由器R2配置好OSPF后,就会每10s向网络中广播一个OSPF hello 包, hello包中包含了Deignated router :192.168.3.1
R1也配置好后,R2上会收到其回复的hello包, designated Router 为192.168.1.1, 并且显示neighbor发现结果,更交互流程如下图所示。
REF
openwrt ospf 路由协议支持 | sagiri's blog
Linux CentOS 7的quagga配置动态路由ospf - 掘金 (juejin.cn)
更新ing---Linux利用quagga组建OSPF网络_quagga zebra ospf bgp代码分析-CSDN博客
Linux环境中静态路由、quagga、netmap、docker等实验整理 - 知乎 (zhihu.com)
Linux下使用Quagga(Zebra)搭建路由器记录 第6页_服务器应用_Linux公社-Linux系统门户网站 (linuxidc.com)
The End.