前缀 172.16.12.0 通配符 0.0.0.0 通配符bits位为0==match
172.16.13.0           0.0.0.0 bits位为1==ignore
172.16.14.0          0.0.0.0
172.16.15.0          0.0.0.0
 
匹配上面的四条路由用访问控制列表是 172.16.x.x 0.0.x.x
                     前缀     通配符
前缀:相同是几就是几,不同为0
通配符:相同为0,不同为1
172.16.12.0==172.16.0000 1100.0
172.16.13.0==172.16.0000 1101.0
172.16.14.0==172.16.0000 1110.0
172.16.15.0==172.16.0000 1111.0
前缀172.16.x.0
通配符0.0.x.0
蓝色标记的两个1bits是相同的前缀相同是几就是几所以这两个bits==12,后面的两位不同为0,所以前缀就是12.0
绿色标记的两个bits位是不同的,所以通配符不同为1,这两个bits==3,这两个bits前面的bits和后面的bits都相同,所以为0
前缀:172.16.12.0
通配符:0.0.3.0
所以访问控制列表:access-list 1 permit 172.16.12.0 0.0.3.0
 
//路由条目验算:匹配路由条目是n=2 m,m=通配符中的bits位1。
 
//access-list 1 permit 172.16.12.0 0.0.0.0是正确的
Access-list 1 permit 172.16.12.0 0.0.0.255 是错误的
通配符和前缀是一体的,匹配的是前缀,而不是掩码
 
//access-list 1 permit 3.3.3.0 0.0.0.0
标准列表匹配的是前缀,所以匹配的是3.3.3.0这个前缀。
对于/25、26、27、28等掩码包含的IP范围,都包含在3.3.3.0这个前缀里的,是无法区分/25、26、27、28等掩码的IP;所以说标准列表匹配的只是前缀,是不能匹配掩码的。
比如:3.3.3.0 0.0.0.0,现在只是匹配3.3.3.0这个前缀,在3.3.3.0网段里的3.3.3.10/25或3.3.3.234/26,是无法区分的,标准列表无法匹配这两个路由,只能匹配3.3.3.0整个前缀网段的路由。
 
//如果要在匹配前缀的情况下,还要匹配掩码/24、25、26、27等来区分掩码的话,就要使用前缀列表。
 
Ip prefix-list A permit 172.16.12.0/24 ge xx le xx
Ge 24 le 24就==掩码长度/24,但是ge 24和le 24是无法执行的(ge与le后面的数字不能相同)。
 
如果要执行ge 24和le 24的话,不写ge和le命令即可:ip prefix-list A permit 172.16.12.0/24。
如果只写一个ge 25不写le的话,默认le就等于32。
如果只写le 26的话,不写ge,ge默认=前最长度/24,就是ge 24 le 26的概念。
 
// 匹配的路由条目数==当ge=le时,用ge或le减去length==x,2的x次方==匹配的路由条目数。
Ip prefix-list 172.16.12.0/24的条目数为1条;因为ge=le==24,24减去length24==0,路由条目数==2的0次方==1,所以说现在只匹配了一条路由172.16.12.0/24(现在不只匹配了前缀,还匹配了172.16.12.0的/24掩码路由,就是只匹配了172.16.12.0路由掩码为/24的,如果是172.16.12.0中掩码为/25、26等路由是没有被匹配住的)
 
//172.16.12.0/22的匹配
172.16.12.0/24
172.16.13.0/24
172.16.14.0/24
172.16.15.0/24
172.16.00001100.0 根据前缀原则,相同是几就是几,不同为0,所以前缀是172.16.12.0
10101100.00010000.00001100.00000000它们的公共长度是/22(公共长度就是就是前缀长度)
前缀加上公共长度就是172.16.12.0/22
Ip prefix-list A permit 172.16.12.0/22 ge 24 le 24 //ge或le==24减去length/22==2,2的2次方==4,匹配4条路由条目;匹配了12.0,13.0,14.0,15.0掩码为24的4跳路由。
/22匹配的是前缀,Ge和le匹配的是掩码。
 
//172.16.12.0/22 ge 25 le 25
172.16.12.0/25
172.16.13.0/25
172.16.14.0/25
172.16.15.0/25
前缀:172.16.12.0
前缀长度:/22
匹配掩码为25的路由
Ip prefix-list A permit 172.16.12.0/22 ge 25 le 25 //匹配了前缀为172.16.12.0/22,掩码为25的路由;/24、/26等是不会被匹配住的。
 
 
//如果是标准列表access-list 1 permit 172.16.12.0 0.0.3.0的话,会将172.16.12.0路由,掩码为/24、/25、/26等都匹配住,不会区分掩码的
 
 
//之前介绍过匹配掩码为/24的和/25的,要写两条前缀列表ge24,le24和一个ge25,le25的前缀列表;现在可以通过写一条ge24 le25的前缀列表来匹配/24和/25的掩码路由。
Ip prefix-list A permit 172.16.12.0/22 ge 24 le 25
可是这样验算匹配路由条目的方法就发生了变化,因为现在ge≠le, 就不能用ge=le时,用le或ge减去length=x,2的x次方=匹配的路由条目。
 
当ge不等于le时:a=le减length,z=ge减length
公式:条目数=2的a次方+····+2的z次方
172.16.12.0/22 ge 24 le 25就是2 2+2 3=12 //匹配的路由条目数为12个。
可是公式里的"···"代表的是什么呢?
比如所ge 24 le 26,就不是2 2+2 4了,它们之间有一个25,所以是2 2+2 3+2 4=匹配的路由条目数28
 
//在IGP中是无法使用ge 24 le 25的,只有在BGP中才可以使用
172.16.12.0/24
172.16.13.0/24
172.16.14.0/24
172.16.15.0/24
172.16.12.0/25
172.16.13.0/25
172.16.14.0/25
172.16.15.0/25
现在是8条路由,要匹配的/24和/25的话就要用172.16.12.0/22 ge 24 le 25
可是ge-22=2,le-22=3;2 2+2 3=12,匹配了12个路由条目
多出了172.16.12.128/25,172.16.13.128/25,172.16.14.128/25,172.16.15.128/25这4条路由条目;
至于为什么多出的是/25的这几条路由路由条目,那是因为ge 24匹配的是12.0掩码为/24的路由,2的2次方正好4条路由;le 25匹配的是12.0掩码为25的路由,2的3次方=8,匹配8条路由,分别是网络号为12.0/25的4条,还有网络号为12.128的4条。
 
 
针对掩码为/24(ge24)的路由前缀进行匹配: 172.16.00001100.00000000 /22 被蓝色标记的两个bits是变动值,因为ge24减length22=2,所以是两个变动值,变动值前面红色标记的bits会被锁死为172.16.12的。对于掩码为/24的路由可以匹配12.0—15.0这4条路由,匹配的条目数正好,不多也不少;
针对掩码为/25(le25)的路由前缀进行匹配: 172.16.00001100.00000000 /22 被蓝色标记的三个bits是变动值,因为le25减length22=3,所以是三个变动值,变动值前面红色标记的bits会被锁死为172.16.12的。对于掩码为/25的路由可以匹配12.0—15.0这4条,还有12.128—15.128这4条路由,共8条,所以在匹配172.16.12.0/25路由时会多出12.128—15.128这4条路由的。
在IGP中是无法将 172.16.00000011. 00000000 标记为红色的两个部分同时锁死的,就是说前缀是不能被跳着匹配的,只能匹配172.16.000000这个部分。由于掩码是25,第4个byte的第一个0是会被借去的,所以是2的3次方==8条路由条目,多出了4条。
 
 
在BGP中的扩展列表
Access-list 100 permit ip x.x.x.x a.a.a.a y.y.y.y b.b.b.b
            前缀 通配符 掩码 通配符
在BGP中扩展列表是用通配符匹配前缀和掩码
通配符是可以跳着匹配前缀的:
172.16.00001100. 00000000 /22前缀可以被掩码跳着匹配(锁死),而源和目的是不会跳着匹配的。第4个byte的第一个0被锁死后是无法被借走的。
例如:access-list 100 permit ip 172.16.12.0 0.0.3.0 255.255.255.0 0.0.0.127
172.16.12.0 0.0.3.0==172.16.000000 11.0根据通配符0.0.000000 11.0,为1的部分会被放开,其他的会被锁死
255.255.255.0 用来匹配掩码为/24的路由条目
0.0.0.128 用来匹配掩码为/25的路由条目
 
 
IGP与BGP的列表不同
在IGP中的扩展列表
Access-list 100 permit ip x.x.x.x a.a.a.a y.y.y.y b.b.b.b
            源     通配符 目的 通配符
在IGP中扩展列表是用通配符匹配源和目的,所以无法向BGP一样精确匹配前缀,和匹配掩码
 
 
//标准列表、前缀列表、和BGP的扩展列表区别
标准列表不能匹配掩码,只能匹配前缀
 
前缀列表可以匹配掩码,但是length控制不严格,不能精确匹配下面这样的前缀
172.16.12.0/24
172.16.13.0/24
172.16.14.0/24
172.16.15.0/24
172.16.12.0/25
172.16.13.0/25
172.16.14.0/25
172.16.15.0/25
 
在BGP中的扩展列表既可以精确匹配前缀,又可以匹配掩码

不同于用于匹配流量的IP访问列表,IP前缀列表主要是用来指定具体的网络可达的。前缀列表用来匹配前缀(网段)和前缀长度(子网掩码)。前缀列表有两个参数很难理解。
下面是普通的前缀列表的参数:
ip prefix-list [name] [permit | deny] [prefix]/[len]
name为任意的名字或者数字,prefix是指定的路由前缀(网段),len是指定的前缀长度(子网掩码)。例子如下:
ip prefix-list LIST permit 1.2.3.0/24
上面的例子中指定匹配网段1.2.3.0,并且指定子网掩码为255.255.255.0,这个列表不匹配1.2.0.0/24,也不匹配1.2.3.4/32
ip prefix-list LIST permit 0.0.0.0/0
上面的例子指定匹配网段0.0.0.0和子网掩码0.0.0.0。这个列表用来匹配默认路由。
通常情况下,在使用前缀列表的时候加上“GE”(大于或等于)和“LE”(小于或等于)时比较容易发生混淆。这是因为当使用“GE”和“LE”时,列表的长度(len)发生了改变。
另外一种前缀列表的参数:
ip prefix-list [name] [permit | deny] [prefix]/[len] ge [min_length] le [max_length]
name为任意的名字或者数字,prefix是将要进行比较的路由前缀(网段),len是指从最左边开始的比特位,min_length为最小的子网掩码的值,max_length为最大的子网掩码的值
使用GE和LE,必须满足下面的条件:
len < GE <= LE
上面的参数很容易混淆,简单的说就是一个匹配前缀或子网的地址的范围。
看下面的例子:
ip prefix-list LIST permit 1.2.3.0/24 le 32
上面的例子表示前缀1.2.3.0前面的24位必须匹配。此外,子网掩码必须小于或等于32位
ip prefix-list LIST permit 0.0.0.0/0 le 32
上面的例子意味着0位需要匹配,此外子网掩码必须小于或等于32位。一位所有的网段的掩码都小于或等于32位,并且一位都不用匹配,所以这句话等于permit any
ip prefix-list LIST permit 10.0.0.0/8 ge 21 le 29
上面的例子说明网段10.0.0.0的前8位必须匹配,此外子网掩码必须在21位和29位之间。
注意:
使用前缀列表不能像访问列表那样匹配具体的应用流。
前缀列表也不能用来具体匹配奇数或偶数的前缀,或什么可以被15整除的前缀
在前缀列表中,比特位必须是连续的,并且从左边开始
ip prefix-list fuck permit 0.0.0.0/0 ge 1            表示除了默认路由外的所有路由
ip prefix-list test16 seq 5 permit 0.0.0.0/1 ge 8 le 8                配置A类地址
ip prefix-list test16 seq 10 permit 128.0.0.0/2 ge 16 le 16      配置B类地址
ip prefix-list test16 seq 15 permit 192.0.0.0/3 ge 24 le 24      配置C类地址
---------------------------------------------------------------------------------------------
Exercises:
1. Construct a prefix list that permits only the 192.168.1.0/24 network.
ip prefix-list test1 seq 5 permit 192.168.1.0/24
2. Construct a prefix list that denies network 119.0.0.0, and permits all other prefixes (including all subnets of 119.0.0.0).
ip prefix-list test2 seq 5 deny 119.0.0.0/8
ip prefix-list test2 seq 10 permit 0.0.0.0/0 le 32
3. Construct a prefix list that permits only the default route.
ip prefix-list test3 seq 5 permit 0.0.0.0/0
4. Construct a prefix list the permits everything except the default route.
ip prefix-list test4 seq 5 deny 0.0.0.0/0
ip prefix-list test4 seq 10 permit 0.0.0.0/0 le 32
5. Construct a prefix list that permits network 172.16.0.0 and any of its subnets, and denies all other prefixes.
ip prefix-list test5 seq 5 permit 172.16.0.0/16 le 32
6. Construct a prefix list that permits only the following prefixes: 
10.2.8.32/27 
10.2.8.32/28 
10.2.8.32/29 
10.2.8.32/30
ip prefix-list test6 seq 5 permit 10.2.8.32/27 le 30
7. Construct a prefix list that:
Permits 197.25.94.128/25 
Denies 197.25.94.192/26 
Permits 197.25.94.224/27 
Denies 197.25.94.240/28 
Permits 197.25.94.248/29 
Denies 197.25.94.252/30 
Permits all other prefixes, except for 198.82.0.0/16
ip prefix-list test7 seq 5 deny 197.25.94.192/26
ip prefix-list test7 seq 10 deny 197.25.94.240/28
ip prefix-list test7 seq 15 deny 197.25.94.252/30
ip prefix-list test7 seq 20 deny 198.82.0.0/16
ip prefix-list test7 seq 25 permit 0.0.0.0/0 le 32
8. Construct a prefix list that permits any prefix matching the first 20 bits of 175.29.64.0 which has a mask of at least /26 but not exceeding /29, and denies all other prefixes.
ip prefix-list test8 seq 5 permit 175.29.64.0/20 ge 26 le 29
9. Construct a prefix list that denies any prefix matching the first 19 bits of 15.26.96.0 with any mask up to and including /32, and permits any other prefix.
ip prefix-list test9 seq 5 deny 15.26.96.0/19 le 32
ip prefix-list test9 seq 10 permit 0.0.0.0/0 le 32
10. Construct a prefix list that denies the RFC 1918 private networks and any of their subnets, and permits everything else.
ip prefix-list test10 seq 5 deny 10.0.0.0/8 le 32
ip prefix-list test10 seq 10 deny 172.16.0.0/12 le 32
ip prefix-list test10 seq 15 deny 192.168.0.0/16 le 32
ip prefix-list test10 seq 20 permit 0.0.0.0/0 le 32
11. Construct a prefix list that permits any subnet of network 15.0.0.0 (but not the network), and denies everything else. Your router lies within AS 65011. Place the prefix list in service in the inbound direction with BGP neighbor 1.2.3.4.
ip prefix-list test11 seq 5 permit 15.0.0.0/8 ge 9
To place it in service: 
router bgp 65011
neighbor 1.2.3.4 prefix-list test11 in
12. Construct a prefix list that denies 162.56.0.0/16 and all of its subnets (with the exception of 162.56.209.208/29, which is permitted), and permits all other prefixes. Your router lies within AS 65012. Place the prefix list in service in the outbound direction with its BGP neighbor having address 5.6.7.8.
ip prefix-list test12 seq 5 permit 162.56.209.208/29
ip prefix-list test12 seq 10 deny 162.56.0.0/16 le 32
ip prefix-list test12 seq 15 permit 0.0.0.0/0 le 32
To place it in service: 
router bgp 65012
neighbor 5.6.7.8 prefix-list test12 out
13. Construct a prefix list that permits the CIDR block containing the thirty-two class C networks beginning with 200.202.160.0/24, and denies everything else. Your router is within AS 65013. Place the prefix list in service in the inbound direction with BGP peer-group "Lucky_13".
ip prefix-list test13 seq 5 permit 200.202.160.0/19
To place it in service: 
router bgp 65013
neighbor Lucky_13 prefix-list test13 in
14. Construct a prefix list that denies any prefix for which the most-significant four bits are "0110", and permits everything else.
ip prefix-list test14 seq 5 deny 96.0.0.0/4 le 32
ip prefix-list test14 seq 10 permit 0.0.0.0/0 le 32
15. Construct a prefix list that permits the host address of "CatSpace", and denies everything else.
ip prefix-list test15 seq 5 permit 64.82.100.67/32
16. Construct a prefix list that permits only classful networks, and denies everything else.
ip prefix-list test16 seq 5 permit 0.0.0.0/1 ge 8 le 8
ip prefix-list test16 seq 10 permit 128.0.0.0/2 ge 16 le 16
ip prefix-list test16 seq 15 permit 192.0.0.0/3 ge 24 le 24
17. Construct a prefix list that denies only supernets, and permits everything else.
ip prefix-list test17 seq 5 deny 0.0.0.0/1 le 7
ip prefix-list test17 seq 10 deny 128.0.0.0/2 le 15
ip prefix-list test17 seq 15 deny 192.0.0.0/3 le 23
ip prefix-list test17 seq 20 permit 0.0.0.0/0 le 32
18. Construct a prefix list that permits only subnets, and denies everything else.
ip prefix-list test18 seq 5 permit 0.0.0.0/1 ge 9
ip prefix-list test18 seq 10 permit 128.0.0.0/2 ge 17
ip prefix-list test18 seq 15 permit 192.0.0.0/3 ge 25
19. Construct a prefix list that permits only CIDR blocks encompassing at least 32 class-C equivalents.
ip prefix-list test19 seq 5 deny 0.0.0.0/0
ip prefix-list test19 seq 10 permit 0.0.0.0/0 le 19
20. Construct a prefix list that permits only the RFC 1918 private networks and their subnets, and configure RIP to use this prefix list for outbound routing advertisements.
ip prefix-list test20 seq 5 permit 10.0.0.0/8 le 32
ip prefix-list test20 seq 10 permit 172.16.0.0/12 le 32
ip prefix-list test20 seq 15 permit 192.168.0.0/16 le 32
To place it in effect for outbound RIP updates: 
router rip
distribute-list prefix test20 out