Linux 内核参数之 rp_filter
原文链接:https://www.cnblogs.com/lipengxiang2009/p/7446388.html
一、rp_filter参数介绍
rp_filter - INTEGER
0 - No source validation.
1 - Strict mode as defined in RFC3704 Strict Reverse Path
Each incoming packet is tested against the FIB and if the interface
is not the best reverse path the packet check will fail.
By default failed packets are discarded.
2 - Loose mode as defined in RFC3704 Loose Reverse Path
Each incoming packet's source address is also tested against the FIB
and if the source address is not reachable via any interface
the packet check will fail.
0 - No source validation.
1 - Strict mode as defined in RFC3704 Strict Reverse Path
Each incoming packet is tested against the FIB and if the interface
is not the best reverse path the packet check will fail.
By default failed packets are discarded.
2 - Loose mode as defined in RFC3704 Loose Reverse Path
Each incoming packet's source address is also tested against the FIB
and if the source address is not reachable via any interface
the packet check will fail.
Current recommended practice in RFC3704 is to enable strict mode
to prevent IP spoofing from DDos attacks. If using asymmetric routing
or other complicated routing, then loose mode is recommended.
The max value from conf/{all,interface}/rp_filter is used
when doing source validation on the {interface}.
Default value is 0. Note that some distributions enable itin startup scripts.
即rp_filter参数有三个值,0、1、2,具体含义:
0:不开启源地址校验。
1:开启严格的反向路径校验。对每个进来的数据包,校验其反向路径是否是最佳路径。如果反向路径不是最佳路径,则直接丢弃该数据包。
2:开启松散的反向路径校验。对每个进来的数据包,校验其源地址是否可达,即反向路径是否能通(通过任意网口),如果反向路径不同,则直接丢弃该数据包。
二、rp_filter参数示例
假设机器有2个网口:
eth0: 192.168.1.100
eth1:200.153.1.122
数据包源IP:10.75.153.98,目的IP:200.153.1.122
系统路由表配置为:
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.234 0.0.0.0 UG 0 0 0 eth0
192.168.120.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.75.153.98 0.0.0.0 255.255.255.0 U 0 0 0 eth0
系统rp_filter参数的配置为:
[root@localhost ~]# sysctl -a | grep rp_filter
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.default.rp_filter=1
如上所示,数据包发到了eth1网卡,如果这时候开启了rp_filter参数,并配置为1,则系统会严格校验数据包的反向路径。从路由表中可以看出,返回响应时数据包要从eth0网卡出,即请求数据包进的网卡和响应数据包出的网卡不是同一个网卡,这时候系统会判断该反向路径不是最佳路径,而直接丢弃该请求数据包。(业务进程也收不到该请求数据包)
![](https://images2017.cnblogs.com/blog/1218087/201708/1218087-20170829200628405-1029691481.png)
解决办法:
1.修改路由表,使响应数据包从eth1出,即保证请求数据包进的网卡和响应数据包出的网卡为同一个网卡。
2.关闭rp_filter参数。(注意all和default的参数都要改)
1)修改/etc/sysctl.conf文件,然后sysctl -p刷新到内存。
2)使用sysctl -w直接写入内存:sysctl -w net.ipv4.conf.all.rp_filter=0
3)修改/proc文件系统: echo "0">/proc/sys/net/ipv4/conf/all/rp_filter
三、开启rp_filter参数的作用
1. 减少DDoS攻击
校验数据包的反向路径,如果反向路径不合适,则直接丢弃数据包,避免过多的无效连接消耗系统资源。
2. 防止IP Spoofing
校验数据包的反向路径,如果客户端伪造的源IP地址对应的反向路径不在路由表中,或者反向路径不是最佳路径,则直接丢弃数据包,不会向伪造IP的客户端回复响应。
Ps:两种常见的非法攻击手段:
1. DDos攻击(Distribute Deny of Service)
分布式拒绝服务攻击。通过构造大量的无用数据包向目标服务发起请求,占用目标服务主机大量的资源,还可能造成网络拥塞,进而影响到正常用户的访问。
2. IP Spoofing(IP欺骗)
IP Spoofing指一个客户端通过伪造源IP,冒充另外一个客户端与目标服务进行通信,从而达到某些不可告人的秘密。