這篇文章主要介紹了Linux中netfilter與VRF的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
黃山區(qū)網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)從2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
實驗環(huán)境如下圖所示:
配置如下:
#!/bin/bash sudo ip netns add ns1 sudo ip link add ns1veth2 type veth peer name eth0 netns ns1 sudo ip netns add ns2 sudo ip link add ns2veth2 type veth peer name eth0 netns ns2 sudo ip link set ns1veth2 master vrftest sudo ip link set ns2veth2 master vrftest sudo ip link set ns2veth2 up sudo ip link set ns1veth2 up sudo ip addr add 1.1.1.254/24 dev ns1veth2 sudo ip addr add 2.2.2.254/24 dev ns2veth2 sudo ip netns exec ns2 ip addr add 2.2.2.1/24 dev eth0 sudo ip netns exec ns1 ip addr add 1.1.1.1/24 dev eth0 sudo ip netns exec ns1 ip link set eth0 up sudo ip netns exec ns1 ip link set lo up sudo ip netns exec ns1 ip route add default via 1.1.1.254 dev eth0 sudo ip netns exec ns2 ip link set eth0 up sudo ip netns exec ns2 ip link set lo up sudo ip netns exec ns2 ip route add default via 2.2.2.254 dev eth0 sudo iptables -t mangle -A PREROUTING -s 1.1.1.1 -j LOG --log-prefix="vrf-test-prerouting" sudo iptables -t mangle -A FORWARD -s 1.1.1.1 -j LOG --log-prefix="vrf-test-forward" sudo iptables -t mangle -A POSTROUTING -s 1.1.1.1 -j LOG --log-prefix="vrf-test-postrouting" sudo iptables -t mangle -A PREROUTING -d 1.1.1.1 -j LOG --log-prefix="vrf-test-prerouting" sudo iptables -t mangle -A FORWARD -d 1.1.1.1 -j LOG --log-prefix="vrf-test-forward" sudo iptables -t mangle -A POSTROUTING -d 1.1.1.1 -j LOG --log-prefix="vrf-test-postrouting" sudo iptables -t mangle -A INPUT -d 1.1.1.1 -j LOG --log-prefix="vrf-test-localin" sudo iptables -t mangle -A INPUT -s 1.1.1.1 -j LOG --log-prefix="vrf-test-localin" sudo iptables -t mangle -A OUTPUT -s 1.1.1.1 -j LOG --log-prefix="vrf-test-localout" sudo iptables -t mangle -A OUTPUT -d 1.1.1.1 -j LOG --log-prefix="vrf-test-localout"
ns1 ping 網(wǎng)關(guān)1.1.1.254
admin@ubuntu:~$ sudo ip netns exec ns1 ping 1.1.1.254 -c 1 PING 1.1.1.254 (1.1.1.254) 56(84) bytes of data. 64 bytes from 1.1.1.254: icmp_seq=1 ttl=64 time=0.064 ms --- 1.1.1.254 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.064/0.064/0.064/0.000 ms admin@ubuntu:~$
查看log
Nov 20 20:34:10 ubuntu kernel: [180403.527204] vrf-test-preroutingIN=ns1veth2 OUT= MAC=b2:f8:2a:13:31:75:6e:17:d5:b2:55:14:08:00 SRC=1.1.1.1 DST=1.1.1.254 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=32492 DF PROTO=ICMP TYPE=8 CODE=0 ID=33955 SEQ=1 Nov 20 20:34:10 ubuntu kernel: [180403.527213] vrf-test-preroutingIN=vrftest OUT= MAC=b2:f8:2a:13:31:75:6e:17:d5:b2:55:14:08:00 SRC=1.1.1.1 DST=1.1.1.254 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=32492 DF PROTO=ICMP TYPE=8 CODE=0 ID=33955 SEQ=1 Nov 20 20:34:10 ubuntu kernel: [180403.527220] vrf-test-localinIN=vrftest OUT= MAC=b2:f8:2a:13:31:75:6e:17:d5:b2:55:14:08:00 SRC=1.1.1.1 DST=1.1.1.254 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=32492 DF PROTO=ICMP TYPE=8 CODE=0 ID=33955 SEQ=1 Nov 20 20:34:10 ubuntu kernel: [180403.527231] vrf-test-localoutIN= OUT=vrftest SRC=1.1.1.254 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=54845 PROTO=ICMP TYPE=0 CODE=0 ID=33955 SEQ=1 Nov 20 20:34:10 ubuntu kernel: [180403.527233] vrf-test-postroutingIN= OUT=vrftest SRC=1.1.1.254 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=54845 PROTO=ICMP TYPE=0 CODE=0 ID=33955 SEQ=1 Nov 20 20:34:10 ubuntu kernel: [180403.527235] vrf-test-localoutIN= OUT=ns1veth2 SRC=1.1.1.254 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=54845 PROTO=ICMP TYPE=0 CODE=0 ID=33955 SEQ=1 Nov 20 20:34:10 ubuntu kernel: [180403.527242] vrf-test-postroutingIN= OUT=ns1veth2 SRC=1.1.1.254 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=54845 PROTO=ICMP TYPE=0 CODE=0 ID=33955 SEQ=1
從log可以看出:
請求報文經(jīng)過的hook點順序如下:
序號 | hook點 | 輸入接口 | 輸出接口 |
---|---|---|---|
1 | PREROUTING | ns1veth2 | 無 |
2 | PREROUTING | vrftest | 無 |
3 | INPUT | vrftest | 無 |
應(yīng)答報文經(jīng)過的hook點順序如下:
序號 | hook點 | 輸入接口 | 輸出接口 |
---|---|---|---|
1 | OUTPUT | 無 | vrftest |
2 | POSTROUTING | 無 | vrftest |
3 | OUTPUT | 無 | ns1veth2 |
4 | POSTROUTING | 無 | ns1veth2 |
ns1 ping ns2
admin@ubuntu:~$ sudo ip netns exec ns1 ping 2.2.2.1 -c 1 PING 2.2.2.1 (2.2.2.1) 56(84) bytes of data. 64 bytes from 2.2.2.1: icmp_seq=1 ttl=63 time=0.063 ms --- 2.2.2.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.063/0.063/0.063/0.000 ms admin@ubuntu:~$
查看log
Nov 20 20:28:31 ubuntu kernel: [180065.076713] vrf-test-preroutingIN=ns1veth2 OUT= MAC=b2:f8:2a:13:31:75:6e:17:d5:b2:55:14:08:00 SRC=1.1.1.1 DST=2.2.2.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=38312 DF PROTO=ICMP TYPE=8 CODE=0 ID=33948 SEQ=1 Nov 20 20:28:31 ubuntu kernel: [180065.076722] vrf-test-preroutingIN=vrftest OUT= MAC=b2:f8:2a:13:31:75:6e:17:d5:b2:55:14:08:00 SRC=1.1.1.1 DST=2.2.2.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=38312 DF PROTO=ICMP TYPE=8 CODE=0 ID=33948 SEQ=1 Nov 20 20:28:31 ubuntu kernel: [180065.076730] vrf-test-forwardIN=vrftest OUT=ns2veth2 MAC=b2:f8:2a:13:31:75:6e:17:d5:b2:55:14:08:00 SRC=1.1.1.1 DST=2.2.2.1 LEN=84 TOS=0x00 PREC=0x00 TTL=63 ID=38312 DF PROTO=ICMP TYPE=8 CODE=0 ID=33948 SEQ=1 Nov 20 20:28:31 ubuntu kernel: [180065.076732] vrf-test-postroutingIN= OUT=ns2veth2 SRC=1.1.1.1 DST=2.2.2.1 LEN=84 TOS=0x00 PREC=0x00 TTL=63 ID=38312 DF PROTO=ICMP TYPE=8 CODE=0 ID=33948 SEQ=1 Nov 20 20:28:31 ubuntu kernel: [180065.076746] vrf-test-preroutingIN=ns2veth2 OUT= MAC=02:25:0e:fe:52:35:ba:19:4d:37:ac:8b:08:00 SRC=2.2.2.1 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=47601 PROTO=ICMP TYPE=0 CODE=0 ID=33948 SEQ=1 Nov 20 20:28:31 ubuntu kernel: [180065.076749] vrf-test-preroutingIN=vrftest OUT= MAC=02:25:0e:fe:52:35:ba:19:4d:37:ac:8b:08:00 SRC=2.2.2.1 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=47601 PROTO=ICMP TYPE=0 CODE=0 ID=33948 SEQ=1 Nov 20 20:28:31 ubuntu kernel: [180065.076752] vrf-test-forwardIN=vrftest OUT=ns1veth2 MAC=02:25:0e:fe:52:35:ba:19:4d:37:ac:8b:08:00 SRC=2.2.2.1 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=63 ID=47601 PROTO=ICMP TYPE=0 CODE=0 ID=33948 SEQ=1 Nov 20 20:28:31 ubuntu kernel: [180065.076753] vrf-test-postroutingIN= OUT=ns1veth2 SRC=2.2.2.1 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=63 ID=47601 PROTO=ICMP TYPE=0 CODE=0 ID=33948 SEQ=1
從log可以看出:
請求報文經(jīng)過的hook點順序如下:
序號 | hook點 | 輸入接口 | 輸出接口 |
---|---|---|---|
1 | PREROUTING | ns1veth2 | 無 |
2 | PREROUTING | vrftest | 無 |
3 | FORWARD | vrftest | ns2veth2 |
4 | POSTROUTING | 無 | ns2veth2 |
應(yīng)答報文經(jīng)過的hook點順序如下:
序號 | hook點 | 輸入接口 | 輸出接口 |
---|---|---|---|
1 | PREROUTING | ns2veth2 | |
2 | PREROUTING | vrftest | |
3 | FORWARD | vrftest | ns2veth2 |
4 | POSTROUTING | 無 | ns2veth2 |
vrftest ping 1.1.1.1
admin@ubuntu:~$ sudo ping 1.1.1.1 -I vrftest -c 1 ping: Warning: source address might be selected on device other than vrftest. PING 1.1.1.1 (1.1.1.1) from 1.1.1.254 vrftest: 56(84) bytes of data. 64 bytes from 1.1.1.1: icmp_seq=1 ttl=64 time=0.039 ms --- 1.1.1.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.039/0.039/0.039/0.000 ms admin@ubuntu:~$
查看log
Nov 20 21:21:11 ubuntu kernel: [183224.956734] vrf-test-localoutIN= OUT=vrftest SRC=1.1.1.254 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=35042 DF PROTO=ICMP TYPE=8 CODE=0 ID=34186 SEQ=1 Nov 20 21:21:11 ubuntu kernel: [183224.956740] vrf-test-postroutingIN= OUT=vrftest SRC=1.1.1.254 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=35042 DF PROTO=ICMP TYPE=8 CODE=0 ID=34186 SEQ=1 Nov 20 21:21:11 ubuntu kernel: [183224.956745] vrf-test-localoutIN= OUT=ns1veth2 SRC=1.1.1.254 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=35042 DF PROTO=ICMP TYPE=8 CODE=0 ID=34186 SEQ=1 Nov 20 21:21:11 ubuntu kernel: [183224.956746] vrf-test-postroutingIN= OUT=ns1veth2 SRC=1.1.1.254 DST=1.1.1.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=35042 DF PROTO=ICMP TYPE=8 CODE=0 ID=34186 SEQ=1 Nov 20 21:21:11 ubuntu kernel: [183224.956762] vrf-test-preroutingIN=ns1veth2 OUT= MAC=b2:f8:2a:13:31:75:6e:17:d5:b2:55:14:08:00 SRC=1.1.1.1 DST=1.1.1.254 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=323 PROTO=ICMP TYPE=0 CODE=0 ID=34186 SEQ=1 Nov 20 21:21:11 ubuntu kernel: [183224.956765] vrf-test-preroutingIN=vrftest OUT= MAC=b2:f8:2a:13:31:75:6e:17:d5:b2:55:14:08:00 SRC=1.1.1.1 DST=1.1.1.254 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=323 PROTO=ICMP TYPE=0 CODE=0 ID=34186 SEQ=1 Nov 20 21:21:11 ubuntu kernel: [183224.956769] vrf-test-localinIN=vrftest OUT= MAC=b2:f8:2a:13:31:75:6e:17:d5:b2:55:14:08:00 SRC=1.1.1.1 DST=1.1.1.254 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=323 PROTO=ICMP TYPE=0 CODE=0 ID=34186 SEQ=1
從log可以看出:
請求報文經(jīng)過的hook點順序如下:
序號 | hook點 | 輸入接口 | 輸出接口 |
---|---|---|---|
1 | OUTPUT | 無 | vrftest |
2 | POSTROUTING | 無 | vrftest |
3 | OUTPUT | 無 | ns1veth2 |
4 | POSTROUTING | 無 | ns1veth2 |
應(yīng)答報文經(jīng)過的hook點順序如下:
序號 | hook點 | 輸入接口 | 輸出接口 |
---|---|---|---|
1 | PREROUTING | ns1veth2 | 無 |
2 | PREROUTING | vrftest | 無 |
3 | INPUT | vrftest | 無 |
vrftest ping vrf接口地址9.9.9.9/24
添加如下netfilter規(guī)則:
admin@ubuntu:~/vrftcpdump$ cat netfilter1.sh sudo iptables -t mangle -A PREROUTING -s 9.9.9.9 -j LOG --log-prefix="vrf-test-prerouting" sudo iptables -t mangle -A FORWARD -s 9.9.9.9 -j LOG --log-prefix="vrf-test-forward" sudo iptables -t mangle -A POSTROUTING -s 9.9.9.9 -j LOG --log-prefix="vrf-test-postrouting" sudo iptables -t mangle -A INPUT -s 9.9.9.9 -j LOG --log-prefix="vrf-test-localin" sudo iptables -t mangle -A OUTPUT -s 9.9.9.9 -j LOG --log-prefix="vrf-test-localout" admin@ubuntu:~/vrftcpdump$
配置vrftest接口地址為9.9.9.9/24
admin@ubuntu:~/vrftcpdump$ sudo ip addr add 9.9.9.9/24 dev vrftest admin@ubuntu:~/vrftcpdump$ sudo ping 9.9.9.9 -I vrftest -c 1 PING 9.9.9.9 (9.9.9.9) from 9.9.9.9 vrftest: 56(84) bytes of data. 64 bytes from 9.9.9.9: icmp_seq=1 ttl=64 time=0.050 ms --- 9.9.9.9 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.050/0.050/0.050/0.000 ms admin@ubuntu:~/vrftcpdump$
查看log
Nov 20 22:13:41 ubuntu kernel: [186374.589186] vrf-test-localoutIN= OUT=vrftest SRC=9.9.9.9 DST=9.9.9.9 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=39425 DF PROTO=ICMP TYPE=8 CODE=0 ID=34815 SEQ=1 Nov 20 22:13:41 ubuntu kernel: [186374.589192] vrf-test-postroutingIN= OUT=vrftest SRC=9.9.9.9 DST=9.9.9.9 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=39425 DF PROTO=ICMP TYPE=8 CODE=0 ID=34815 SEQ=1 Nov 20 22:13:41 ubuntu kernel: [186374.589202] vrf-test-preroutingIN=vrftest OUT= MAC=ca:f9:f0:37:4c:6c:ca:f9:f0:37:4c:6c:08:00 SRC=9.9.9.9 DST=9.9.9.9 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=39425 DF PROTO=ICMP TYPE=8 CODE=0 ID=34815 SEQ=1 Nov 20 22:13:41 ubuntu kernel: [186374.589204] vrf-test-localinIN=vrftest OUT= MAC=ca:f9:f0:37:4c:6c:ca:f9:f0:37:4c:6c:08:00 SRC=9.9.9.9 DST=9.9.9.9 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=39425 DF PROTO=ICMP TYPE=8 CODE=0 ID=34815 SEQ=1 Nov 20 22:13:41 ubuntu kernel: [186374.589210] vrf-test-localoutIN= OUT=vrftest SRC=9.9.9.9 DST=9.9.9.9 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=39426 PROTO=ICMP TYPE=0 CODE=0 ID=34815 SEQ=1 Nov 20 22:13:41 ubuntu kernel: [186374.589211] vrf-test-postroutingIN= OUT=vrftest SRC=9.9.9.9 DST=9.9.9.9 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=39426 PROTO=ICMP TYPE=0 CODE=0 ID=34815 SEQ=1 Nov 20 22:13:41 ubuntu kernel: [186374.589215] vrf-test-preroutingIN=vrftest OUT= MAC=ca:f9:f0:37:4c:6c:ca:f9:f0:37:4c:6c:08:00 SRC=9.9.9.9 DST=9.9.9.9 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=39426 PROTO=ICMP TYPE=0 CODE=0 ID=34815 SEQ=1 Nov 20 22:13:41 ubuntu kernel: [186374.589217] vrf-test-localinIN=vrftest OUT= MAC=ca:f9:f0:37:4c:6c:ca:f9:f0:37:4c:6c:08:00 SRC=9.9.9.9 DST=9.9.9.9 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=39426 PROTO=ICMP TYPE=0 CODE=0 ID=34815 SEQ=1
從log可以看出:
請求報文經(jīng)過的hook點順序如下:
序號 | hook點 | 輸入接口 | 輸出接口 |
---|---|---|---|
1 | OUTPUT | 無 | vrftest |
2 | POSTROUTING | 無 | vrftest |
3 | PREROUTING | vrftest | 無 |
4 | INPUT | vrftest | 無 |
應(yīng)答報文經(jīng)過的hook點順序如下:
序號 | hook點 | 輸入接口 | 輸出接口 |
---|---|---|---|
1 | OUTPUT | 無 | vrftest |
2 | POSTROUTING | 無 | vrftest |
3 | PREROUTING | vrftest | 無 |
4 | INPUT | vrftest | 無 |
環(huán)回請求和應(yīng)答是一樣的。
linux內(nèi)核支持VRF后,報文會兩次進入PREROUTING節(jié)點,第一次是原始接口,第二次是原始接口依附的vrf主接口。
linux內(nèi)核支持VRF后,本機輸出的報文,先以vrf接口作為輸出接口經(jīng)過OUTPUT和POSTROUTING節(jié)點,然后再以真實出去的接口經(jīng)過OUPUT和POSTROUTING節(jié)點。
本機報文進行環(huán)回時,與沒有VRF的環(huán)境是一樣的。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Linux中netfilter與VRF的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!