标签归档:rp_filter

rp_filter及Linux下多网卡接收多播的问题

有一台双网卡的机器,上面装有Fedora8,运行一个程序。该程序分别在两个网口上都接收多播数据,程序运行是正常的。但是,后来升级系统到Fedora13,发现就出问题了:在运行几秒钟后,第2个网口上就接收不到多播数据了。

能不能收到多播,取决于交换机是不是往这个网口上转发多播数据。程序在起动的时候,会发一个IGMP的AddMembership的消息,交换机将把这个网口加入多播组。当在其他网口上收到该地址的多播包后,会转至这个网口。其后,为了确认该接收者一直在线,交换机会发送一个IGMPQuery消息,接收者反馈一个IGMP Report消息,以确认自己的存在。如果交换机没有收到IGMPReport,则认为该接收者已经断线,就不再往该网口上转发多播包了。

用抓包工具定位了一下,发现程序在启动时确实发了AddMembership消息,这是正常的。在接收下来的5秒时间内,程序能够收到多播数据。接着,交换机发来了一条IGMPQuery,问题来了,这个Fedora13系统却没有反馈Report。这是很奇怪的。按理说,IGMP属于系统自动完成的协议,无需用户干预;那么按照预期,Linux会自动反馈IGMPReport的。事实上,Feodra8和WinXP系统就是这么做的,都很正常。为什么到了Fedora13反而不正常了呢?

在调查“为什么不反馈IGMPReport”的事情上,花了一周时间都没有进展,后来发现其实不至Fedora13,其他的主流linux如Ubuntu10,SUSE14也存在同样的问题。

查了众多论坛都没有一点提示信息。后来,终于在一个英文网站上扫到了一个信息:rp_filter。后来证明,这个关键词是解决问题的关键。reverse-pathfiltering,反向过滤技术,系统在接收到一个IP包后,检查该IP是不是合乎要求,不合要求的IP包会被系统丢弃。该技术就称为rpfilter。怎么样的包才算不合要求呢?例如,用户在A网口上收到一个IP包,检查其IP为B。然后考查:对于B这个IP,在发送时应该用哪个网口,“如果在不应该接收到该包的网口上接收到该IP包,则认为该IP包是hacker行为”。

例如:

A: 192.168.8.100

B: (IGMP Query) 10.0.0.1 来自路由器

查找路由表

网卡1为默认路由: 172.17.5.100  172.17.5.1

网卡2          192.168.8.100  192.168.8.1

系统根据路由表,认为10.0.0.1这个IP应该在第一个网卡172.17.5.100上收到,现实的情况是在第二张网卡192.168.8.100上收到了。认为这是不合理的,丢弃该包。致命的问题的,该包是来自路由器的IGMPQuery包。

The rp_filter can reject incoming packets if their sourceaddress doesn’t match the network interface that they’re arrivingon, which helps to prevent IP spoofing. Turning this on, however,has its consequences: If your host has several IP addresses ondifferent interfaces, or if your single interface has multiple IPaddresses on it, you’ll find that your kernel may end up rejectingvalid traffic. It’s also important to note that even if you do notenable the rp_filter, protection against broadcast spoofing isalways on. Also, the protection it provides is only against spoofedinternal addresses; external addresses can still be spoofed.. Bydefault, it is disabled.

解决方法:

系统配置文件
1. /etc/sysctl.conf
把 net.ipv4.conf.all.rp_filter和net.ipv4.conf.default.rp_filter设为0即可
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.all.rp_filter = 0
系统启动后,会自动加载这个配置文件,内核会使用这个变量

2. 命令行
显示一个内核变量 sysctl net.ipv4.conf.all.rp_filter
设置一个内核变量 sysctl -w net.ipv4.conf.all.rp_filter=0
设置完后,会更新内核(实时的内存)中的变量的值,但不会修改sysctl.conf的值

3. 使用/proc文件系统
查看 cat /proc/sys/net/ipv4/conf/all/rp_filter
设置 echo “0”>/proc/sys/net/ipv4/conf/all/rp_filter

sysctl.conf文件参数rp_filter

系统:Centos6

影响:

路径:/etc/sysctl.conf

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 theinterface 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 theFIB and if the source address is not reachable via any interface the packet check will fail.Current recommended practice in RFC3704 is to enable strictmode to prevent IP spoofing from DDos attacks. If using asymmetricrouting 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 it in startup scripts.
—–

Red Hat are (correctly) setting rp_filter to 1, strictmode.  In this case a packet coming in eth0 willhave its source address routed out on the same interface that itcame in on (because that’s the default route). However, a packet coming in on eth1 will have it source addressrouted out on a different interface to the one it came in on and itwill be discarded.  Silently.

This is basically asymmetric routing and is quite possibly not whatyou want anyway (it messes up TCP flow control) so there are twoways to fix this: stick with asymmetric routing and permit it orfix the asymmetric routing.

The first one is easiest: in /etc/sysctl.conf change rp_filter=1 torp_filter=2).  You’ll need to load that andrestart the network.  It’s probably easiest toreboot 🙂 to be sure.  I suspect that it was notrestarting enough things that prevented this change from workingbefore.

The second one may be simple as simple as adding those routes thatshould go out on eth1 to the routing table or running some routingdaemon.  It depends on your network topology,basically.  This would be the preferred solutionif it’s practicable.

reverse-pathfiltering,反向过滤技术,系统在接收到一个IP包后,检查该IP是不是合乎要求,不合要求的IP包会被系统丢弃。该技术就称为rpfilter。怎么样的包才算不合要求呢?例如,用户在A网口上收到一个IP包,检查其IP为B。然后考查:对于B这个IP,在发送时应该用哪个网口,“如果在不应该接收到该包的网口上接收到该IP包,则认为该IP包是hacker行为”。

解决方法:

系统配置文件
1. /etc/sysctl.conf
把 net.ipv4.conf.all.rp_filter和net.ipv4.conf.default.rp_filter设为0即可
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.all.rp_filter = 0

net.ipv4.conf.eth0.rp_filter = 0
net.ipv4.conf.eth1.rp_filter = 0

net.ipv4.conf.lo.rp_filter = 0

系统启动后,会自动加载这个配置文件,内核会使用这个变量

2. 命令行
显示一个内核变量 sysctl net.ipv4.conf.all.rp_filter
设置一个内核变量 sysctl -w net.ipv4.conf.all.rp_filter=0
设置完后,会更新内核(实时的内存)中的变量的值,但不会修改sysctl.conf的值

3. 使用/proc文件系统
查看 cat /proc/sys/net/ipv4/conf/all/rp_filter
设置 echo “0”>/proc/sys/net/ipv4/conf/all/rp_filter

sysctl常用命令

sysctl -a 查看所有参数

sysctl -p 加载配置文件

Suse linux 11配置多网卡后网络不通的解决办法

问题描述

1.RH2288-1、RH2288-2安装的是SUSE11操作系统。
2.服务器采的是双网卡绑定,bond0在VLAN100、bond1在VLAN200。
3.VLAN100的网关为172.16.0.1、VLAN200的网关为192.168.0.1。
RH2288-1的默认网关为192.168.0.1,RH2288-2的默认网关为172.16.0.1。
4.RH2288-2 ping RH2288-1的bond1网卡 192.168.0.187不通。

告警信息
FY-NMS:~ # ping -c 1 192.168.0.187
PING 192.168.0.187 (192.168.0.187) 56(84) bytes of data.
— 192.168.0.187 ping statistics —
1 packets transmitted, 0 received, 100% packet loss, time 0ms

处理过程
1.在RH2288-1、RH2288-2 ping 各自默认网关是否正常。
结果正常,无丢包。
2.检查RH2288-1、RH2288-1默认网关配置正确。
RH2288-1配置正确,检查结果如下:
FY-HIS:/etc # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 bond1
RH2288-2配置正确,检查结果如下:
FY-NMS ~ # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 172.16.0.1 0.0.0.0 UG 0 0 0 bond0
3.在RH2288-1抓取网络数据包,是否能收到RH2288-2请求的ping请求。
FY-HIS:~ # tcpdump -ni bond1 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on bond1, link-type EN10MB (Ethernet), capture size 96 bytes
09:45:38.900680 IP 172.16.0.188 > 192.168.0.188: ICMP echo request, id 24523, seq 54, length 64
09:45:39.908686 IP 172.16.0.188 > 192.168.0.188: ICMP echo request, id 24523, seq 55, length 64
09:45:40.916731 IP 172.16.0.188 > 192.168.0.188: ICMP echo request, id 24523, seq 56, length 64
09:45:41.924674 IP 172.16.0.188 > 192.168.0.188: ICMP echo request, id 24523, seq 57, length 64
4.根据抓包显示,从RH2288-2 ping RH2288-1 192.168.0.188不通,是因为在RH2288-1上只能抓到RH2288-2的request报文,但没有抓到RH2288-1 的reply的报文。

原因
问题产生的根因应该是SUSE 系统路由表策略,优先级低的路由,它不回复对端的arp请求。

解决方案
1.sysctl -a |grep rp_fi,查看返回值是否都为0。
2.将不为0的值改为0。
如net.ipv4.conf.all.rp_filter = 1,则修改sysctl -w net.ipv4.conf.all.rp_filter=0
3.将该修改值写入配置文件:
vi /etc/sysctl.conf
在该文件中将需要修改为0的值进行更改,需要注意格式与文件中其他配置一致。

建议与总结
在处理服务器侧网络不通时,建议多使用抓包的方法,通过分析网络报文快速定位问题。