这是我所做的。
服务器(公共互联网是222.xxx):
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf sysctl -p iptunnel add gre1 mode gre local 222.xxx remote 115.xxx ttl 255 ip add add 192.168.168.1/30 dev gre1 ip link set gre1 up iptables -t nat -A POSTROUTING -s 192.168.168.0/30 -j SNAT --to-source 222.xxx iptables -t nat -A PREROUTING -d 222.xxx -j DNAT --to-destination 192.168.168.2
客户端(公共互联网是115.xxx):
iptunnel add gre1 mode gre local 115.xxx remote 222.xxx ttl 255 ip add add 192.168.168.2/30 dev gre1 ip link set gre1 up echo '100 tunnel' >> /etc/iproute2/rt_tables ip rule add from 192.168.168.0/30 table tunnel ip route add default via 192.168.168.1 table tunnel
直到这里,一切似乎都是正确的。 但是第一个问题,如何使用GRE隧道作为默认路由? 客户端计算机仍然使用115.xxx接口作为默认值。
第二个问题,如何强制只有ICMPstream量通过隧道,其他一切去默认界面? 我试图在客户端计算机上做这个:
ip rule add fwmark 200 table tunnel iptables -t mangle -A OUTPUT -p udp -j MARK --set-mark 200
但是这样做后,我的ping程序将超时(如果我没有做上面的2命令,并使用ping -I gre1 ip代替,它将工作)。 后来我还想做其他的事情,比如只有UDP端口53通过隧道等。
第三个问题,在客户端计算机上,我强制一个mysql程序在gre1接口上侦听192.168.168.2。 在客户端计算机中,还有一个公共接口(IP 114.xxx)…如何使用iptables和路由正确转发stream量,以便mysql也响应来自此114.xxx公共接口的请求?
问题1
作为默认路由使用gre隧道检出。
问题2所有icmp通过隧道
iptables -t nat -A POSTROUTING -o gre1 -p icmp -j SNAT --to-source 192.168.168.2
问题3
在客户机上,使用DNAT进行从外部端口到服务器端口的端口转发。
方法2 – 端口reflection/端口镜像
iptables -t nat -A PREROUTING -p tcp -m tcp -m multiport -d 114.xxx --dports 3306 -j DNAT --to-destination 192.168.168.1 iptables -t nat -A POSTROUTING -o gre1 -p tcp -m tcp -m multiport -d 192.168.168.1 --dports 3306 -j SNAT --to-source 192.168.168.2