Linux核心參數之rp_filter 一、rp_filter參數介紹 rp_filter參數用於控制系統是否開啟對資料包源地址的校正。 首先看一下Linux核心文檔documentation/networking/ip-sysctl.txt中的描述: 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.
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 it in 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網卡出,即請求資料包進的網卡和響應資料包出的網卡不是同一個網卡,這時候系統會判斷該反向路徑不是最佳路徑,而直接丟棄該請求資料包。(業務進程也收不到該請求資料包) 解決辦法: 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,冒充另外一個用戶端與目標服務進行通訊,從而達到某些不可告人的秘密。
出處:https://www.cnblogs.com/lipengxiang2009/p/7446388.html
補充:
https://my.oschina.net/7shell/blog/308887
公司業務需求,一台伺服器,雙網卡,需要同時訪問外網和內網。在設定過程中,同事反映原本好用的內網地址(192.168.1.100)在設定上外網地址後變的不好用,只要停止外網那個網卡內網就可以正常訪問了。
其實是因為,你先設定了內網之後,又設定外網,在給外網設定了網關之後,由於沒有設定預設閘道,導致,內網的IP也會走外網的網關,肯定是不可以的。我們要做的僅僅是讓內網走自己的網卡
內網網卡:eth0 192.168.1.100 網關:192.168.1.100
外網網卡 : eth1 119.222.222.222 網關:119.222.222.1
首先設定預設閘道,讓所有IP包預設情況下均通過 119.222.222.1 進行轉寄:
route add default gw 119.222.222.1
然後,單獨為內網設定轉寄特例,所有192.168開頭的,全部走eth0
route add -net 192.168.0.0 netmask 255.255.0.0 dev eth0
路由添加的最好是要加到開機啟動上
vi /etc/rc.local
另:刪除預設閘道的命令如下:
route del default