一、简介¶
本章给出了常用过滤工具、基本的访问控制列表、高级的访问控制及前缀列表的配置实例。
二、配置基本的访问控制列表¶
2.1 原理概述¶
1、访问控制列表ACL(Access Control List)是由permit或deny语句组成的一系列有顺序的规则集合,这些规则根据数据包的源地址、目的地址、源端口、目的端口等信息来描述。ACL规则通过匹配报文中的信息对数据包进行分类,路由设备根据这些规则判断哪些数据包可以通过,哪些数据包需要拒绝。 2、按照访问控制列表的用途,可以分为基本的访问控制列表和高级的访问控制列表,基本ACL可使用报文的源IP地址、时间段信息来定义规则,编号范围为2000-2999。 3、一个ACL可以由多条deny/permit语句组成,每一条语句描述一条规则,每条规则有一个Rule-ID。Rule-ID可以由用户进行配置,也可以由系统自动根据步长生成,默认步长为5,Rule-ID默认按照配置先后顺序分配0、5、10、15等,匹配顺序按照ACL的Rule-ID的顺序,从小到大进行匹配。 4、ACL的执行是有顺序性的,如果规则ID小的规则已经被命中,并且执行了允许或者拒绝的动作,那么后续的规则就不再继续匹配。
2.2 基本的访问控制列表配置实验¶
2.2.1 实验目的¶
1、理解基本访问控制列表的应用场景 2、掌握配置基本访问控制列表的方法
2.2.2 实验内容¶
本实验模拟企业网络环境,R1为分支机构A管理员所在IT部门的网关,R2为分支机构A用户部门的网关,R3为分支机构A去往总部出口的网关设备,R4为总部核心路由器设备。整网运行OSPF协议,并在区域0内。企业设计通过远程方式管理核心网路由器R4,要求只能由R1所连的PC(本实验使用环回接口模拟)访问R4,其他设备均不能访问。
2.2.3 实验拓扑¶

2.2.4 实验编址¶
| 设备 | 接口 | IP地址 | 子网掩码 | 默认网关 |
|---|---|---|---|---|
| R1(AR2220) | GE0/0/0 | 10.0.13.1 | 255.255.255.0 | N/A |
| R1(AR2220) | Loopback0 | 1.1.1.1 | 255.255.255.255 | N/A |
| R2(AR2220) | GE0/0/0 | 10.0.23.2 | 255.255.255.0 | N/A |
| R3(AR2220) | GE0/0/0 | 10.0.13.3 | 255.255.255.0 | N/A |
| R3(AR2220) | GE0/0/1 | 10.0.23.3 | 255.255.255.0 | N/A |
| R3(AR2220) | GE0/0/2 | 10.0.34.3 | 255.255.255.0 | N/A |
| R3(AR2220) | Loopback0 | 3.3.3.3 | 255.255.255.255 | N/A |
| R4(AR2220) | GE0/0/0 | 10.0.34.4 | 255.255.255.0 | N/A |
| R4(AR2220) | Loopback0 | 4.4.4.4 | 255.255.255.255 | N/A |
| ### 2.2.5 实验步骤 | ||||
| #### 2.2.5.1 基本配置 | ||||
| 根据实验编制表进行相应的基本配置,并使用Ping命令检测直连链路的连通性。 | ||||
| #### 2.2.5.2 搭建OSPF网络 | ||||
| 1、在所有路由器上运行OSPF协议,通告相应网段至区域0中。 |
[R1]ospf
[R1-ospf-1]area 0
[R1-ospf-1-area-0.0.0.0]network 1.1.1.1 0.0.0.0
[R1-ospf-1-area-0.0.0.0]network 10.0.13.0 0.0.0.255
[R2]ospf
[R2-ospf-1]area 0
[R2-ospf-1-area-0.0.0.0]network 10.0.23.0 0.0.0.255
[R3]ospf
[R3-ospf-1]area 0
[R3-ospf-1-area-0.0.0.0]network 10.0.13.0 0.0.0.255
[R3-ospf-1-area-0.0.0.0]network 10.0.23.0 0.0.0.255
[R3-ospf-1-area-0.0.0.0]network 10.0.34.0 0.0.0.255
[R3-ospf-1-area-0.0.0.0]network 3.3.3.3 0.0.0.0
[R4]ospf
[R4-ospf-1]area 0
[R4-ospf-1-area-0.0.0.0]network 10.0.34.0 0.0.0.255
[R4-ospf-1-area-0.0.0.0]network 4.4.4.4 0.0.0.0
2、配置完成后,在R1的路由表上查看OSPF路由信息。观察到路由器R1已经学到了相关网段的路由条目。
[R1]dis ip routing-table protocol ospf
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Public routing table : OSPF
Destinations : 4 Routes : 4
OSPF routing table status : <Active>
Destinations : 4 Routes : 4
Destination/Mask Proto Pre Cost Flags NextHop Interface
3.3.3.3/32 OSPF 10 1 D 10.0.13.3 GigabitEthernet
0/0/0
4.4.4.4/32 OSPF 10 2 D 10.0.13.3 GigabitEthernet
0/0/0
10.0.23.0/24 OSPF 10 2 D 10.0.13.3 GigabitEthernet
0/0/0
10.0.34.0/24 OSPF 10 2 D 10.0.13.3 GigabitEthernet
0/0/0
OSPF routing table status : <Inactive>
Destinations : 0 Routes : 0
3、测试R1的环回口和R4的环回口间的连通性。观察到通信正常。其他路由器间测试略。
[R1]ping -a 1.1.1.1 4.4.4.4
PING 4.4.4.4: 56 data bytes, press CTRL_C to break
Reply from 4.4.4.4: bytes=56 Sequence=1 ttl=254 time=50 ms
Reply from 4.4.4.4: bytes=56 Sequence=2 ttl=254 time=40 ms
Reply from 4.4.4.4: bytes=56 Sequence=3 ttl=254 time=50 ms
Reply from 4.4.4.4: bytes=56 Sequence=4 ttl=254 time=30 ms
Reply from 4.4.4.4: bytes=56 Sequence=5 ttl=254 time=20 ms
--- 4.4.4.4 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 20/38/50 ms
2.2.5.3 配置基本ACL控制访问¶
1、在总部核心路由器R4上配置SSH相关配置,配置用户名和密码为huawei。
[R4]stelnet server enable
[R4]aaa
[R4-aaa]local-user huawei privilege level 3 password cipher huawei
[R4-aaa]local-user huawei service-type ssh
[R4]user-interface vty 0 4
[R4-ui-vty0-4]authentication-mode aaa
[R4-ui-vty0-4]protocol inbound ssh
[R1]ssh client first-time enable
2、配置完成后,尝试在IT部门网关设备R1上建立SSH连接,观察到,R1可以成功登录R4。
[R1]stelnet 10.0.34.4
Please input the username:huawei
Trying 10.0.34.4 ...
Press CTRL+K to abort
Connected to 10.0.34.4 ...
The server is not authenticated. Continue to access it? (y/n)[n]:y
Feb 12 2022 15:50:01-08:00 R1 %%01SSH/4/CONTINUE_KEYEXCHANGE(l)[0]:The server ha
d not been authenticated in the process of exchanging keys. When deciding whethe
r to continue, the user chose Y.
[R1]
Save the server's public key? (y/n)[n]:y
The server's public key will be saved with the name 10.0.34.4. Please wait...
Feb 12 2022 15:50:05-08:00 R1 %%01SSH/4/SAVE_PUBLICKEY(l)[1]:When deciding wheth
er to save the server's public key 10.0.34.4, the user chose Y.
[R1]
Enter password:
<R4>
3、再尝试在普通员工部门网关设备R2上建立连接。发现,只要是路由可达的设备,并且拥有SSH登录密码,都可以成功访问核心设备R4。这显然是极为不安全的。网络管理员通过配置标准ACL来实现访问过滤,禁止普通员工设备登录。
[R2]stelnet 10.0.34.4
Please input the username:huawei
Trying 10.0.34.4 ...
Press CTRL+K to abort
Connected to 10.0.34.4 ...
The server is not authenticated. Continue to access it? (y/n)[n]:y
Feb 12 2022 15:52:14-08:00 R2 %%01SSH/4/CONTINUE_KEYEXCHANGE(l)[0]:The server ha
d not been authenticated in the process of exchanging keys. When deciding whethe
r to continue, the user chose Y.
[R2]
Save the server's public key? (y/n)[n]:y
The server's public key will be saved with the name 10.0.34.4. Please wait...
Feb 12 2022 15:52:15-08:00 R2 %%01SSH/4/SAVE_PUBLICKEY(l)[1]:When deciding wheth
er to save the server's public key 10.0.34.4, the user chose Y.
[R2]
Enter password:
-----------------------------------------------------------------------------
User last login information:
-----------------------------------------------------------------------------
Access Type: SSH
IP-Address : 10.0.13.1 ssh
Time : 2022-02-12 15:50:10-08:00
-----------------------------------------------------------------------------
<R4>
4、基本ACL可以针对数据包的源IP地址进行过滤,在R4上使用acl命令创建一个编号型ACL,基本ACL的范围是2000-2999.并在ACL视图下,使用rule命令配置ACL规则,指定规则ID为5,允许数据包源地址为1.1.1.1的报文通过,反掩码全为0,即精确匹配。再使用rule命令配置第二条规则,指定规则ID为10,拒绝任意源地址的数据包通过。
[R4]acl 2000
[R4-acl-basic-2000]rule 5 permit source 1.1.1.1 0.0.0.0
[R4-acl-basic-2000]rule 10 deny source any
5、配置完成后,在VTY中调用。使用inbound参数,即在R4的数据入方向上调用。(也可以在R4的GE0/0/0使用traffic-filter inbound acl 2000)
[R4]user-interface vty 0 4
[R4-ui-vty0-4]acl 2000 inbound
6、配置完成后,使用R1的环回口地址1.1.1.1测试访问4.4.4.4的连通性。发现没有问题。
[R1]stelnet -a 1.1.1.1 4.4.4.4
Please input the username:huawei
Trying 4.4.4.4 ...
Press CTRL+K to abort
Connected to 4.4.4.4 ...
The server is not authenticated. Continue to access it? (y/n)[n]:y
Feb 12 2022 16:06:02-08:00 R1 %%01SSH/4/CONTINUE_KEYEXCHANGE(l)[0]:The server ha
d not been authenticated in the process of exchanging keys. When deciding whethe
r to continue, the user chose Y.
[R1]
Save the server's public key? (y/n)[n]:y
The server's public key will be saved with the name 4.4.4.4. Please wait...
Feb 12 2022 16:06:04-08:00 R1 %%01SSH/4/SAVE_PUBLICKEY(l)[1]:When deciding wheth
er to save the server's public key 4.4.4.4, the user chose Y.
[R1]
Enter password:
-----------------------------------------------------------------------------
User last login information:
-----------------------------------------------------------------------------
Access Type: SSH
IP-Address : 10.0.23.2 ssh
Time : 2022-02-12 15:52:19-08:00
-----------------------------------------------------------------------------
<R4>
7、在R2上尝试访问4.4.4.4的连通性。此时R2已经无法访问4.4.4.4,说明上述ACL配置已经生效。
[R2]stelnet 4.4.4.4
Please input the username:huawei
Trying 4.4.4.4 ...
Press CTRL+K to abort
Error: Failed to connect to the remote host.
2.2.5.4 基本ACL的语法规则¶
1、ACL的执行是有顺序性的,如果规则ID小的规则已经被命中,并且执行了允许或者拒绝的动作,那么后续的规则就不再继续匹配。在R4上使用dis acl all命令查看设备上的所有访问控制列表。
[R4]dis acl all
Total quantity of nonempty ACL number is 1
Basic ACL 2000, 2 rules
Acl's step is 5
rule 5 permit source 1.1.1.1 0 (3 matches)
rule 10 deny (1 matches)
2、现出现新的需求,需要R3能够使用其环回接口3.3.3.3访问R4。首先尝试使用规则ID 15来添加允许3.3.3.3访问的规则。
[R4]acl 2000
[R4-acl-basic-2000]rule 15 permit source 3.3.3.3 0.0.0.0
3、配置完成后,尝试使用R3的3.3.3.3访问R4.发现无法访问。按照ACL匹配顺序,由于规则为10的条目是拒绝所有行为,后续所有的允许规则都不会匹配。若要此规则生效,必须添加在拒绝所有的规则ID之前。
[R3]stelnet -a 3.3.3.3 4.4.4.4
Please input the username:huawei
Trying 4.4.4.4 ...
Press CTRL+K to abort
Error: Failed to connect to the remote host.
4、在R4上修改ACL2000,将规则改为8.
[R4]acl 2000
[R4-acl-basic-2000]undo rule 15
[R4-acl-basic-2000]rule 8 permit source 3.3.3.3 0.0.0.0
5、配置完成后,再次尝试使用R3的环回口访问R4。此时访问成功,证明配置已经生效。
[R3]stelnet -a 3.3.3.3 4.4.4.4
Please input the username:huawei
Trying 4.4.4.4 ...
Press CTRL+K to abort
Connected to 4.4.4.4 ...
The server is not authenticated. Continue to access it? (y/n)[n]:y
Feb 12 2022 16:22:09-08:00 R3 %%01SSH/4/CONTINUE_KEYEXCHANGE(l)[0]:The server ha
d not been authenticated in the process of exchanging keys. When deciding whethe
r to continue, the user chose Y.
[R3]
Save the server's public key? (y/n)[n]:y
The server's public key will be saved with the name 4.4.4.4. Please wait...
Feb 12 2022 16:22:10-08:00 R3 %%01SSH/4/SAVE_PUBLICKEY(l)[1]:When deciding wheth
er to save the server's public key 4.4.4.4, the user chose Y.
[R3]
Enter password:
-----------------------------------------------------------------------------
User last login information:
-----------------------------------------------------------------------------
Access Type: SSH
IP-Address : 1.1.1.1 ssh
Time : 2022-02-12 16:06:08-08:00
-----------------------------------------------------------------------------
<R4>
2.3 思考¶
在本实验中,如果ACL不配置在R4上,那么该如何配置?有什么优缺点? 答:可以在R3上的入方向和出方向上配置ACL,优点是阻止R2访问R4;缺点是R3本身可以访问R4,和实际需求不一致。
三、配置高级的访问控制列表¶
3.1 原理概述¶
1、基本ACL只能用于匹配源IP地址,而在实际应用中往往需要针对数据包的其他参数进行匹配,比如目的IP地址、协议号、端口号等,所以基本的ACL由于匹配的局限性而无法实现更多的功能,所以就需要使用高级的访问控制列表。 2、高级的访问控制列表在匹配项上做了扩展,编号范围为3000-3999,既可使用报文的源IP地址,也可使用目的地址、IP优先级、IP协议类型、ICMP类型、TCP源端口/目的端口、UDP源端口/目的端口等信息来定义规则。 3、高级的访问控制列表可以定义比基本访问控制列表更准确、更丰富、更灵活的规则。
3.2 高级的访问控制列表配置实验¶
3.2.1 实验目的¶
1、理解高级的访问控制列表的应用场景 2、掌握配置高级的访问控制列表的方法 3、理解高级的访问控制列表与基本访问控制列表的区别
3.2.2 实验内容¶
本实验模拟企业网络环境,R1为分支机构A管理员所在IT部门的网关,R2为分支机构A用户部门的网关,R3为分支机构A去往总部出口的网关设备,R4为总部核心路由器设备。企业原始设计思路想要通过远程方式管理核心网路由器R4,要求R1所连的PC可以访问R4,其他设备均不能访问。同时要求只能管理R4的4.4.4.4这台服务器,另一台同样直连R4的服务器40.40.40.40不能被管理(本实验PC使用环回接口模拟)
3.2.3 实验拓扑¶

3.2.4 实验编址¶
| 设备 | 接口 | IP地址 | 子网掩码 | 默认网关 |
|---|---|---|---|---|
| R1(AR2220) | GE0/0/0 | 10.0.13.1 | 255.255.255.0 | N/A |
| R1(AR2220) | Loopback0 | 1.1.1.1 | 255.255.255.255 | N/A |
| R2(AR2220) | GE0/0/0 | 10.0.23.2 | 255.255.255.0 | N/A |
| R3(AR2220) | GE0/0/0 | 10.0.13.3 | 255.255.255.0 | N/A |
| R3(AR2220) | GE0/0/1 | 10.0.23.3 | 255.255.255.0 | N/A |
| R3(AR2220) | GE0/0/2 | 10.0.34.3 | 255.255.255.0 | N/A |
| R3(AR2220) | Loopback0 | 3.3.3.3 | 255.255.255.255 | N/A |
| R4(AR2220) | GE0/0/0 | 10.0.34.4 | 255.255.255.0 | N/A |
| R4(AR2220) | Loopback0 | 4.4.4.4 | 255.255.255.255 | N/A |
| R4(AR2220) | Loopback1 | 40.40.40.40 | 255.255.255.255 | N/A |
| ### 3.2.5 实验步骤 | ||||
| #### 3.2.5.1 基本配置 | ||||
| 根据实验编制表进行相应的基本配置,并使用Ping命令检测直连链路的连通性。 | ||||
| #### 3.2.5.2 搭建OSPF网络 | ||||
| 1、在所有路由器上运行OSPF协议,通告相应网段至区域0中。 |
[R1]ospf
[R1-ospf-1]area 0
[R1-ospf-1-area-0.0.0.0]network 1.1.1.1 0.0.0.0
[R1-ospf-1-area-0.0.0.0]network 10.0.13.0 0.0.0.255
[R2]ospf
[R2-ospf-1]area 0
[R2-ospf-1-area-0.0.0.0]network 10.0.23.0 0.0.0.255
[R3]ospf
[R3-ospf-1]area 0
[R3-ospf-1-area-0.0.0.0]network 10.0.13.0 0.0.0.255
[R3-ospf-1-area-0.0.0.0]network 10.0.23.0 0.0.0.255
[R3-ospf-1-area-0.0.0.0]network 10.0.34.0 0.0.0.255
[R3-ospf-1-area-0.0.0.0]network 3.3.3.3 0.0.0.0
[R4]ospf
[R4-ospf-1]area 0
[R4-ospf-1-area-0.0.0.0]network 10.0.34.0 0.0.0.255
[R4-ospf-1-area-0.0.0.0]network 4.4.4.4 0.0.0.0
[R4-ospf-1-area-0.0.0.0]network 40.40.40.40 0.0.0.0
2、配置完成后,在R1的路由表上查看OSPF路由信息。观察到路由器R1已经学到了相关网段的路由条目。
[R1]dis ip routing-table protocol ospf
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Public routing table : OSPF
Destinations : 4 Routes : 4
OSPF routing table status : <Active>
Destinations : 4 Routes : 4
Destination/Mask Proto Pre Cost Flags NextHop Interface
4.4.4.4/32 OSPF 10 2 D 10.0.13.3 GigabitEthernet
0/0/0
10.0.23.0/24 OSPF 10 2 D 10.0.13.3 GigabitEthernet
0/0/0
10.0.34.0/24 OSPF 10 2 D 10.0.13.3 GigabitEthernet
0/0/0
40.40.40.40/32 OSPF 10 2 D 10.0.13.3 GigabitEthernet
0/0/0
OSPF routing table status : <Inactive>
Destinations : 0 Routes : 0
3、测试R1的环回口和R4的环回口间的连通性。观察到通信正常。其他路由器间测试略。
[R1]ping -a 1.1.1.1 4.4.4.4
PING 4.4.4.4: 56 data bytes, press CTRL_C to break
Reply from 4.4.4.4: bytes=56 Sequence=1 ttl=254 time=50 ms
Reply from 4.4.4.4: bytes=56 Sequence=2 ttl=254 time=40 ms
Reply from 4.4.4.4: bytes=56 Sequence=3 ttl=254 time=50 ms
Reply from 4.4.4.4: bytes=56 Sequence=4 ttl=254 time=30 ms
Reply from 4.4.4.4: bytes=56 Sequence=5 ttl=254 time=20 ms
--- 4.4.4.4 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 20/38/50 ms
[R1]ping -a 1.1.1.1 40.40.40.40
PING 40.40.40.40: 56 data bytes, press CTRL_C to break
Reply from 40.40.40.40: bytes=56 Sequence=1 ttl=254 time=30 ms
Reply from 40.40.40.40: bytes=56 Sequence=2 ttl=254 time=30 ms
Reply from 40.40.40.40: bytes=56 Sequence=3 ttl=254 time=40 ms
Reply from 40.40.40.40: bytes=56 Sequence=4 ttl=254 time=50 ms
Reply from 40.40.40.40: bytes=56 Sequence=5 ttl=254 time=30 ms
--- 40.40.40.40 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 30/36/50 ms
3.2.5.3 配置SSH¶
1、在总部核心路由器R4上配置SSH相关配置,配置用户名和密码为huawei。
[R4]stelnet server enable
[R4]aaa
[R4-aaa]local-user huawei privilege level 3 password cipher huawei
[R4-aaa]local-user huawei service-type ssh
[R4]user-interface vty 0 4
[R4-ui-vty0-4]authentication-mode aaa
[R4-ui-vty0-4]protocol inbound ssh
[R1]ssh client first-time enable
2、配置完成后,尝试在IT部门网关设备R1上建立与R4的环回接口0的IP地址的SSH连接,观察到,R1可以成功登录R4。
[R1]stelnet -a 1.1.1.1 4.4.4.4
Please input the username:huawei
Trying 4.4.4.4 ...
Press CTRL+K to abort
Connected to 4.4.4.4 ...
The server is not authenticated. Continue to access it? (y/n)[n]:y
Feb 13 2022 09:53:29-08:00 R1 %%01SSH/4/CONTINUE_KEYEXCHANGE(l)[6]:The server ha
d not been authenticated in the process of exchanging keys. When deciding whethe
r to continue, the user chose Y.
[R1]
Save the server's public key? (y/n)[n]:y
The server's public key will be saved with the name 4.4.4.4. Please wait...
Feb 13 2022 09:53:30-08:00 R1 %%01SSH/4/SAVE_PUBLICKEY(l)[7]:When deciding wheth
er to save the server's public key 4.4.4.4, the user chose Y.
[R1]
Enter password:
<R4>
3、再尝试在IT部门网关设备R1上建立与R4的环回接口1的IP地址的SSH连接。发现,只要是路由可达的设备,并且拥有SSH登录密码,都可以成功访问核心设备R4。
[R1]stelnet -a 1.1.1.1 40.40.40.40
Please input the username:huawei
Trying 40.40.40.40 ...
Press CTRL+K to abort
Connected to 40.40.40.40 ...
The server is not authenticated. Continue to access it? (y/n)[n]:y
Feb 13 2022 09:56:33-08:00 R1 %%01SSH/4/CONTINUE_KEYEXCHANGE(l)[8]:The server ha
d not been authenticated in the process of exchanging keys. When deciding whethe
r to continue, the user chose Y.
[R1]
Save the server's public key? (y/n)[n]:y
The server's public key will be saved with the name 40.40.40.40. Please wait...
Feb 13 2022 09:56:34-08:00 R1 %%01SSH/4/SAVE_PUBLICKEY(l)[9]:When deciding wheth
er to save the server's public key 40.40.40.40, the user chose Y.
[R1]
Enter password:
-----------------------------------------------------------------------------
User last login information:
-----------------------------------------------------------------------------
Access Type: SSH
IP-Address : 1.1.1.1 ssh
Time : 2022-02-13 09:53:35-08:00
-----------------------------------------------------------------------------
<R4>
3.2.5.4 配置高级ACL控制访问¶
1、在R4上使用acl命令创建一个高级ACL3000.
[R4]acl 3000
2、在高级ACL视图中,使用rule命令配置ACL规则,ip为协议类型,允许源地址为1.1.1.1、目的地址为4.4.4.4的数据包通过。
[R4-acl-adv-3000]rule 5 permit ip source 1.1.1.1 0.0.0.0 destination 4.4.4.4 0.0
.0.0
3、配置完成后,查看ACL配置信息
[R4]dis acl all
Total quantity of nonempty ACL number is 1
Advanced ACL 3000, 1 rule
Acl's step is 5
rule 5 permit ip source 1.1.1.1 0 destination 4.4.4.4 0
4、将ACL3000调用在VTY视图下,使用inbound参数,即在R4的数据入方向上调用。
[R4]user-interface vty 0 4
[R4-ui-vty0-4]acl 3000 inbound
5、配置完成后,在R1上使用环回接口地址尝试访问40.40.40.40.观察到,此时过滤已经实现,R1不能使用环回接口地址访问40.40.40.40。
[R1]stelnet -a 1.1.1.1 40.40.40.40
Please input the username:huawei
Trying 40.40.40.40 ...
Press CTRL+K to abort
Error: Failed to connect to the remote host.
3.3 思考¶
路由器能否通过ACL过滤自身产生的数据包? 答:ACL只能过滤穿过路由器的数据流量,不能过滤由本路由器上发出的数据包,且OSPF在区域内传递链路状态信息,链路状态信息无法过滤。
四、配置前缀列表¶
4.1 原理概述¶
1、前缀列表即IP-Prefix List,它可以将与所定义的前缀列表相匹配的路由,根据定义的匹配模式进行过滤。前缀列表中的匹配条目由IP地址和掩码组成,IP地址可以是网段地址或者主机地址,掩码长度的配置范围为0-32,可以进行精确匹配或者在一定掩码长度范围内匹配,也可以通过配置关键字greater-equal和less-equal指定匹配的前缀掩码长度范围。 2、前缀列表能同时匹配前缀号和前缀长度,主要用于路由的匹配和控制,不能用于数据包的过滤。
4.2 前缀列表配置实验¶
4.2.1 实验目的¶
1、理解前缀列表的应用场景 2、掌握前缀列表的配置方法 3、理解前缀列表与ACL的区别
4.2.2 实验内容¶
本实验模拟企业网络场景。公司分部A网络使用11.1.1.0/24网段,通过路由器R2和骨干路由器R1相连,网络运行RIPv2协议。现在公司新成立一个分部B,新分部B的路由器R3连接R1加入到该RIPv2网络。由于分部B的网络管理员不熟悉公司内部IP地址规划,在新分部B中使用了11.1.1.0/25网段,这样导致从总部发往分部A的部分数据包在R1上都会由于路由掩码最长匹配从而错误地发往分部B。而整个新分部B整改IP地址需要一定时间,公司当务之急是需要恢复总部与分部A的通信,可以通过在R1上使用前缀列表过滤掉这些错误的路由。
4.2.3 实验拓扑¶

4.2.4 实验编址¶
| 设备 | 接口 | IP地址 | 子网掩码 | 默认网关 |
|---|---|---|---|---|
| R1(AR2220) | GE0/0/0 | 40.1.1.1 | 255.255.255.0 | N/A |
| R1(AR2220) | GE0/0/1 | 20.1.1.1 | 255.255.255.0 | N/A |
| R1(AR2220) | GE0/0/2 | 30.1.1.1 | 255.255.255.0 | N/A |
| R2(AR2220) | GE0/0/0 | 11.1.1.2 | 255.255.255.0 | N/A |
| R2(AR2220) | GE0/0/1 | 20.1.1.2 | 255.255.255.0 | N/A |
| R3(AR2220) | GE0/0/0 | 11.1.1.11 | 255.255.255.128 | N/A |
| R3(AR2220) | GE0/0/2 | 30.1.1.3 | 255.255.255.0 | N/A |
| R4(AR2220) | GE0/0/0 | 40.1.1.4 | 255.255.255.0 | N/A |
| PC-1 | E0/0/1 | 11.1.1.1 | 255.255.255.0 | 11.1.1.2 |
| PC-2 | E0/0/1 | 11.1.1.10 | 255.255.255.128 | 11.1.1.11 |
| ### 4.2.5 实验步骤 | ||||
| #### 4.2.5.1 基本配置 | ||||
| 根据实验编制表进行相应的基本配置,并使用Ping命令检测直连链路的连通性。 | ||||
| #### 4.2.5.2 搭建RIP网络 | ||||
| 1、首先配置R1、R2和R4运行RIPv2协议,在总部路由器R4上访问分部A的PC。 |
[R1]rip 1
[R1-rip-1]undo summary
[R1-rip-1]version 2
[R1-rip-1]network 20.0.0.0
[R1-rip-1]network 30.0.0.0
[R1-rip-1]network 40.0.0.0
[R2]rip
[R2-rip-1]undo summary
[R2-rip-1]version 2
[R2-rip-1]network 11.0.0.0
[R2-rip-1]network 20.0.0.0
[R4]rip
[R4-rip-1]undo summary
[R4-rip-1]version 2
[R4-rip-1]network 40.0.0.0
2、配置完成后,查看总部R4的路由表。观察到,R4的路由表中已经获得了11.1.1.0/24的路由。
[R4]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 10 Routes : 10
Destination/Mask Proto Pre Cost Flags NextHop Interface
11.1.1.0/24 RIP 100 2 D 40.1.1.1 GigabitEthernet
0/0/0
20.1.1.0/24 RIP 100 1 D 40.1.1.1 GigabitEthernet
0/0/0
30.1.1.0/24 RIP 100 1 D 40.1.1.1 GigabitEthernet
0/0/0
40.1.1.0/24 Direct 0 0 D 40.1.1.4 GigabitEthernet
0/0/0
40.1.1.4/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
40.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
3、测试R4与公司总部A中的PC-1间的连通性。观察到连通性正常。
[R4]ping 11.1.1.1
PING 11.1.1.1: 56 data bytes, press CTRL_C to break
Reply from 11.1.1.1: bytes=56 Sequence=1 ttl=126 time=60 ms
Reply from 11.1.1.1: bytes=56 Sequence=2 ttl=126 time=60 ms
Reply from 11.1.1.1: bytes=56 Sequence=3 ttl=126 time=60 ms
Reply from 11.1.1.1: bytes=56 Sequence=4 ttl=126 time=60 ms
Reply from 11.1.1.1: bytes=56 Sequence=5 ttl=126 time=80 ms
--- 11.1.1.1 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 60/64/80 ms
4、现在将新分部B加入到公司网络中,在R3上配置RIPv2协议。
[R3]rip
[R3-rip-1]undo summary
[R3-rip-1]version 2
[R3-rip-1]network 11.0.0.0
[R3-rip-1]network 30.0.0.0
5、配置完成后,再一次查看R4的路由表。观察到此时R4收到了公司分部A的11.1.1.0/24路由条目和新分布B的11.1.1.0/25路由条目。同样R1也会接收到这两条路由条目。
[R4]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 11 Routes : 11
Destination/Mask Proto Pre Cost Flags NextHop Interface
11.1.1.0/24 RIP 100 2 D 40.1.1.1 GigabitEthernet
0/0/0
11.1.1.0/25 RIP 100 2 D 40.1.1.1 GigabitEthernet
0/0/0
20.1.1.0/24 RIP 100 1 D 40.1.1.1 GigabitEthernet
0/0/0
30.1.1.0/24 RIP 100 1 D 40.1.1.1 GigabitEthernet
0/0/0
40.1.1.0/24 Direct 0 0 D 40.1.1.4 GigabitEthernet
0/0/0
40.1.1.4/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
40.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
6、在R4上测试与PC-1的连通性。发现此时无法正常通信。根据路由器转发数据的原理,在转发数据包时路由器会根据最长匹配的原则去匹配路由条目,即R4向分部A的终端PC-1发送数据时,当数据包到达R1后,根据包头的目的IP地址与路由表中的路由条目进行匹配,发现11.1.1.0/25条目匹配更准确,这会使得数据包都根据这条路由条目来转发,即将原本要发往PC-1的数据包都错误地发往R3,造成总部与分部A异常通信。
[R4]ping 11.1.1.1
PING 11.1.1.1: 56 data bytes, press CTRL_C to break
Request time out
Request time out
Request time out
Request time out
Request time out
--- 11.1.1.1 ping statistics ---
5 packet(s) transmitted
0 packet(s) received
100.00% packet loss
7、在R4上使用tracert命令测试发往PC-1的数据包所经过的网关。观察到,此时R4发往分公司A的PC-1的数据包确实都已错误地发往R3.
[R4]tracert 11.1.1.1
traceroute to 11.1.1.1(11.1.1.1), max hops: 30 ,packet length: 40,press CTRL_C
to break
1 40.1.1.1 40 ms 10 ms 20 ms
2 30.1.1.3 20 ms 20 ms 20 ms
3 * * *
4 * * *
4.2.5.3 配置ACL过滤路由(测试)¶
1、在R1上创建基本的ACL,拒绝11.1.1.0这个目的网段的路由。
[R1]acl 2000
[R1-acl-basic-2000]rule 5 deny source 11.1.1.0 0.0.0.0
[R1-acl-basic-2000]rule 10 permit source any
2、在RIP视图下,配置过滤策略(filter-policy),该策略通过调用之前配置好的ACL来达到过滤路由的目的,并且在R1的RIP路由进程中的接收方向应用此路由过滤策略。
[R1]rip 1
[R1-rip-1]filter-policy 2000 import
3、配置完成后,查看R1的路由表。观察到R1的路由表中11.1.1.0/24、11.1.1.0/25路由被过滤。这是因为ACL无法对掩码长度进行精确匹配,而分部A的网络位和分部B的网络位相同,都是11.1.1.0,就会导致把分部A的路由也过滤掉。
[R1]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 13 Routes : 13
Destination/Mask Proto Pre Cost Flags NextHop Interface
20.1.1.0/24 Direct 0 0 D 20.1.1.1 GigabitEthernet
0/0/1
20.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
20.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
30.1.1.0/24 Direct 0 0 D 30.1.1.1 GigabitEthernet
0/0/2
30.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/2
30.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/2
40.1.1.0/24 Direct 0 0 D 40.1.1.1 GigabitEthernet
0/0/0
40.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
40.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
4.2.5.4 配置前缀列表过滤路由¶
1、为了精确匹配掩码长度,仅过滤掉11.1.1.0/25路由,在R1上配置前缀列表,同时精确匹配网络位和掩码长度。
[R1]ip ip-prefix 1 deny 11.1.1.0 25 greater-equal 25 less-equal 25
[R1]ip ip-prefix 1 permit 0.0.0.0 0 less-equal 32
注意:前缀列表会有一条隐含的拒绝所有的规则,所以如果要放行其他路由的话,一定要显示增加一条允许所有的规则!!!
2、将前缀列表应用到过滤策略下。
[R1]rip
[R1-rip-1]filter-policy ip-prefix 1 import
3、配置完成后,查看R1的路由表。观察到,此时R1的路由表中仅存在11.1.1.0/24分部A的路由条目。
[R1]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 14 Routes : 14
Destination/Mask Proto Pre Cost Flags NextHop Interface
11.1.1.0/24 RIP 100 1 D 20.1.1.2 GigabitEthernet
0/0/1
20.1.1.0/24 Direct 0 0 D 20.1.1.1 GigabitEthernet
0/0/1
20.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
20.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
30.1.1.0/24 Direct 0 0 D 30.1.1.1 GigabitEthernet
0/0/2
30.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/2
30.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/2
40.1.1.0/24 Direct 0 0 D 40.1.1.1 GigabitEthernet
0/0/0
40.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
40.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
4、测试总部与分部A中PC-1的连通性。观察到,此时恢复正常。
[R4]ping 11.1.1.1
PING 11.1.1.1: 56 data bytes, press CTRL_C to break
Request time out
Reply from 11.1.1.1: bytes=56 Sequence=2 ttl=126 time=70 ms
Reply from 11.1.1.1: bytes=56 Sequence=3 ttl=126 time=50 ms
Reply from 11.1.1.1: bytes=56 Sequence=4 ttl=126 time=60 ms
Reply from 11.1.1.1: bytes=56 Sequence=5 ttl=126 time=50 ms
--- 11.1.1.1 ping statistics ---
5 packet(s) transmitted
4 packet(s) received
20.00% packet loss
round-trip min/avg/max = 50/57/70 ms
4.2.5.5 恢复新分部网络¶
1、规划分部B的网络使用11.2.2.0/24网段,更改PC-2的IP地址为11.2.2.1/24,R3的GE0/0/0接口IP地址为11.2.2.3/24.
[R3]int g0/0/0
[R3-GigabitEthernet0/0/0]ip add 11.2.2.3 24
2、配置完成后,查看R1的路由表。观察到,此时R1的路由表上存在现有新分部B所在11.2.2.0/24网段的路由条目,也有分部A的路由。
[R1]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 15 Routes : 15
Destination/Mask Proto Pre Cost Flags NextHop Interface
11.1.1.0/24 RIP 100 1 D 20.1.1.2 GigabitEthernet
0/0/1
11.2.2.0/24 RIP 100 1 D 30.1.1.3 GigabitEthernet
0/0/2
20.1.1.0/24 Direct 0 0 D 20.1.1.1 GigabitEthernet
0/0/1
20.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
20.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
30.1.1.0/24 Direct 0 0 D 30.1.1.1 GigabitEthernet
0/0/2
30.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/2
30.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/2
40.1.1.0/24 Direct 0 0 D 40.1.1.1 GigabitEthernet
0/0/0
40.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
40.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
3、使用Ping命令测试总部与分部A、新分部B的连通性。观察到,通信正常。
[R4]ping 11.1.1.1
PING 11.1.1.1: 56 data bytes, press CTRL_C to break
Request time out
Reply from 11.1.1.1: bytes=56 Sequence=2 ttl=126 time=50 ms
Reply from 11.1.1.1: bytes=56 Sequence=3 ttl=126 time=60 ms
Reply from 11.1.1.1: bytes=56 Sequence=4 ttl=126 time=70 ms
Reply from 11.1.1.1: bytes=56 Sequence=5 ttl=126 time=50 ms
--- 11.1.1.1 ping statistics ---
5 packet(s) transmitted
4 packet(s) received
20.00% packet loss
round-trip min/avg/max = 50/57/70 ms
[R4]ping 11.2.2.1
PING 11.2.2.1: 56 data bytes, press CTRL_C to break
Request time out
Reply from 11.2.2.1: bytes=56 Sequence=2 ttl=126 time=70 ms
Reply from 11.2.2.1: bytes=56 Sequence=3 ttl=126 time=50 ms
Reply from 11.2.2.1: bytes=56 Sequence=4 ttl=126 time=60 ms
Reply from 11.2.2.1: bytes=56 Sequence=5 ttl=126 time=60 ms
--- 11.2.2.1 ping statistics ---
5 packet(s) transmitted
4 packet(s) received
20.00% packet loss
round-trip min/avg/max = 50/60/70 ms
4.3 思考¶
如果将前缀列表中已配置好的语句顺序打乱会对实验结果产生影响吗? 答:会的,前缀列表条目范围匹配有冲突,范围小的必须放在上面也就是序列号要小,不然会被范围大的行为覆盖导致不生效。