iptables 转发的问题

2014-11-16 11:32:28 +08:00
 Saay
最近在研究@lifetyper FreeRouter_V2的翻墙方案,再本地机器中执行如下命令:
#1创建ipset 模块
ipset create ssh iphash -exist
#2目的地址匹配ipset给数据包打标记
iptables -t mangle -I PREROUTING -m set --match-set ssh dst -j MARK --set-mark 10
#3:添加策略路由(ip rule), 让它们走ssh 路由表
ip rule add fwmark 1 table ssh priority 1984
#4设置默认路由
ip route add default dev vpn0 table ssh

iptables -t nat -A POSTROUTING -o vpn0 -j MASQUERADE

173.194.127.76这个ip地址在ssh ipset模块中,但是:
mtr --report 173.194.127.76
HOST: doenx Loss% Snt Last Avg Best Wrst StDev
1.|-- 192.168.10.190 0.0% 10 170.7 145.7 126.8 170.7 13.4
2.|-- ??? 100.0 10 0.0 0.0 0.0 0.0 0.0
数据包只到达vpn服务器的网关,然后就无法转发了!!

vps中的iptables表:
[root@vps ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere

Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere

[root@vps ~]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 10.8.0.0/24 anywhere
MASQUERADE all -- anywhere anywhere
MASQUERADE all -- anywhere anywhere
MASQUERADE all -- anywhere anywhere
MASQUERADE all -- 10.0.0.0/24 anywhere
MASQUERADE all -- anywhere anywhere
MASQUERADE all -- anywhere anywhere
MASQUERADE all -- anywhere anywhere
MASQUERADE all -- anywhere anywhere
MASQUERADE all -- anywhere anywhere

Chain OUTPUT (policy ACCEPT)
target prot opt source destination


[root@vps ~]# ip route list
192.168.10.191 dev vpn0 proto kernel scope link src 192.168.10.190
128.199.128.0/18 dev eth0 proto kernel scope link src 128.199.189.120
169.254.0.0/16 dev eth0 scope link metric 1002
default via 128.199.128.1 dev eth0

@neroanelli @pheyx
1259 次点击
所在节点    问与答
9 条回复
neroanelli
2014-11-16 16:17:43 +08:00
mtr是在路由器中执行的吗?
试试添加iptables -t mangle -I OUTPUT-m set --match-set ssh dst -j MARK --set-mark 10
Saay
2014-11-16 20:33:43 +08:00
@neroanelli 不是路由,是在本地机器,
添加iptables -t mangle -I OUTPUT -m set --match-set ssh dst -j MARK --set-mark 10依然不行
neroanelli
2014-11-17 09:17:46 +08:00
你的远程vps配置正确吗?其他客户端能正常不?你首先得确定远程vps配置没问题。
然后请提供一下以下几个命令的输出(本地机器):
ip ro ls
ip ru ls
ip ro ls table ssh
你所说的3#中,fwmark应该是10吧,手误?
Saay
2014-11-17 10:52:35 +08:00
@neroanelli你好,3#中的fwmark是10,上面的手误写错了
默认情况下,vpn可以正常工作。但是这时所有的数据都走vpn通道,所以我又添加了一个默认路由:
ip route add default via 192.168.1.1 dev wlp9s0
本地机器的命令输出:
# ip route list
default via 192.168.1.1 dev wlp9s0
default dev ppp0 proto static scope link metric 1024
10.0.0.1 dev ppp0 proto kernel scope link src 10.0.0.100
128.199.189.120 via 192.168.1.1 dev wlp9s0 src 192.168.1.254
192.168.1.0/24 dev wlp9s0 proto kernel scope link src 192.168.1.254
192.168.1.0/24 dev wlp9s0 proto kernel scope link src 192.168.1.254 metric 302

#ip rule list
0: from all lookup local
32765: from all fwmark 0xa lookup ssh
32766: from all lookup main
32767: from all lookup default

# ip route list table ssh
default dev ppp0 scope link
Saay
2014-11-17 10:56:17 +08:00
vps工作应该没问题,我在上面配置了pptp服务器和cisco anyconnect vpn服务器。
对应本地的接口分别是vpn0和ppp0
neroanelli
2014-11-17 13:15:34 +08:00
@Saay
#1 ip route中,添加ip route add default via 192.168.1.1 dev wlp9s0,删掉default dev ppp0 proto static scope link metric 1024 ,路由应该只有一条默认路由。
#2 ssh表中,应该还有192.168.1.0/24 dev wlp9s0 scope link 这条路由。
#3 iptables中,应该是iptables -t mangle -I OUTPUT-m set --match-set ssh dst -j MARK --set-mark 10,而不是PREROUTING。
Saay
2014-11-17 15:41:12 +08:00
@neroanelli
还是不行,我感觉是vps的问题,因为抓包看数据是到vpn网关了,数据包就是死在那!
但是如果client端不做任何设置,vpn是能用的。奇怪了!!!!!!!
neroanelli
2014-11-17 18:09:06 +08:00
@Saay
那就不知道了。应该是没配置好。
vimin
2015-08-11 17:58:31 +08:00
@Saay 请问你后来解决了吗?现在我也遇到了这个问题。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/146876

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX