天天看點

基于iptables實作的跨網絡通信

作者:運維老男孩

描述

在很多業務場景下,會遇上很多詭異的需求,不僅限于文章提及的需求,還有各種五花八門的需求,大部份的這些需求的産生都是來源于以前設計、規劃上導緻的問題。是以我們都會想盡辦法為客戶解決問題,維護好客戶的關系。

環境資訊

OS: Centos7及以上
VM 主機A IP 網卡 網卡通途 預設路由
10.0.43.15 eth0 管理網 yes
VM 主機B IP 網卡用途
10.0.44.63 業務網
10.0.43.101 eth1 no

需求

因特殊原因。使用者需要在主機A通路到10.0.44.0/24的業務網段,主機A 又不能直接使用到業務網。 是以隻能利用主機B,采用nat的方式進行轉發主機A業務請求通路到業務網。

實作方法

# 在主機A添加靜态路由,通路10.0.44.0/24流量都從eth0出去,并且下一跳位址是10.0.43.101

$ cat >  /etc/sysconfig/network-scripts/route-eth0 << EOF
10.0.44.0/24 via 10.0.43.101 dev eth0
EOF           

# 在主機B的eth1網卡進行抓包檢視,發現icmp包已經過來了。

$ tcpdump -i eth1 host 10.0.44.1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 262144 bytes
08:40:09.351088 IP host-10-0-43-15 > gateway: ICMP echo request, id 21356, seq 229, length 64
08:40:10.351100 IP host-10-0-43-15 > gateway: ICMP echo request, id 21356, seq 230, length 64
08:40:11.351091 IP host-10-0-43-15 > gateway: ICMP echo request, id 21356, seq 231, length 64
08:40:12.351090 IP host-10-0-43-15 > gateway: ICMP echo request, id 21356, seq 232, length 64
08:40:13.351060 IP host-10-0-43-15 > gateway: ICMP echo request, id 21356, seq 233, length 64           

# 但在主機B的eth1的網卡并沒有發現icmp包, 這是什麼原因?懷疑是port_security的問題和ip_forward。

$ tcpdump -i eth0 host 10.0.44.1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes           

# 關閉主機B eth0和eth1 的port_security, 在openstack 環境下的配置。

# 找到port id
$ neutron port-list | grep 10.0.43.101
neutron CLI is deprecated and will be removed in the future. Use openstack CLI instead.
| 648c8165-f3e6-42a4-b6cc-0a38cecc5bec |                     | 363b136093524567863320fa0c95b069 | fa:16:3e:8a:63:c9 | {"subnet_id": "9ff8ad43-7b53-4cc2-acbd-74ec6fed3adf", "ip_address": "10.0.43.101"}   |
$ neutron port-list | grep 10.0.44.63
neutron CLI is deprecated and will be removed in the future. Use openstack CLI instead.
| 0b5db09f-3699-4df5-8517-01e7ff665468 |                     | 363b136093524567863320fa0c95b069 | fa:16:3e:f3:1e:42 | {"subnet_id": "97800cd2-06f7-4c9a-aa3b-f9b7a6fe6419", "ip_address": "10.0.44.63"}    |

# 關閉端口的安全組
$ openstack port set --disable-port-security --no-security-group 0b5db09f-3699-4df5-8517-01e7ff665468
$ openstack port set --disable-port-security --no-security-group 648c8165-f3e6-42a4-b6cc-0a38cecc5bec           

# 主機B配置ip_forward

出于安全考慮,Linux系統預設是禁止資料包轉發的。所謂轉發就是當主機擁有多塊網卡時,其中一塊收到資料包,根據資料包的目的ip位址将資料包發往本機另一塊網卡,該網卡根據路由表繼續發送資料包(通常這是需要路由器來實作的功能)。

$ echo 1 > /proc/sys/net/ipv4/ip_forward
$ cat >> /etc/sysctl.conf << EOF
net.ipv4.ip_forward = 1
EOF
$ sysctl -p           

# 在主機B eth0口抓包發現包已經過來了,但是發現隻有request包沒有replay響應的包。仔細分析發現源IP是10.0.43.15去通路10.0.44.1肯定是不通。

$ tcpdump -i eth0 host 10.0.44.1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
09:16:20.099852 IP host-10-0-43-15 > gateway: ICMP echo request, id 21418, seq 385, length 64
09:16:20.230831 IP host-10-0-43-15 > gateway: ICMP echo request, id 21449, seq 51, length 64
09:16:21.099843 IP host-10-0-43-15 > gateway: ICMP echo request, id 21418, seq 386, length 64
09:16:24.099845 IP host-10-0-43-15 > gateway: ICMP echo request, id 21418, seq 389, length 64
           

# 在主機B iptables配置源10.0.44.0/24位址轉換成10.0.44.63出去

對于不是很熟iptables的同學了解此文章[淺聊iptables]

$ iptables -t nat -A POSTROUTING   -d 10.0.44.0/24  -o eth0 -j SNAT --to 10.0.44.63
$ iptables -t nat -L -n -v
Chain PREROUTING (policy ACCEPT 4 packets, 336 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 6 packets, 470 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 6 packets, 470 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    4   336 SNAT       all  --  *      eth0    0.0.0.0/0            10.0.44.0/24         to:10.0.44.63           

# 在主機A發現去通路10.0.44.1的icmp通了。

$ ping 10.0.44.1
PING 10.0.44.1 (10.0.44.1) 56(84) bytes of data.
64 bytes from 10.0.44.1: icmp_seq=1 ttl=253 time=1.19 ms
64 bytes from 10.0.44.1: icmp_seq=2 ttl=253 time=0.852 ms
64 bytes from 10.0.44.1: icmp_seq=3 ttl=253 time=0.809 ms
64 bytes from 10.0.44.1: icmp_seq=4 ttl=253 time=0.886 ms           
原文位址:https://www.cnblogs.com/acommoners/p/15963002.html