iptables 智能路由问题,感谢大佬

2022-06-21 11:01:35 +08:00
 MuskLee
ipset 设置:

ipset create non-vpn hash:net
ipset add non-vpn 1.0.1.0/24
ipset add non-vpn 1.0.2.0/23
ipset add non-vpn 1.0.8.0/21
ipset add non-vpn 1.0.32.0/19
ipset add non-vpn 1.1.0.0/24
ipset add non-vpn 1.1.2.0/23
ipset add non-vpn 1.1.4.0/22
...



iptables 设置:

iptables -F
iptables -X
iptables -Z
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -t nat -A POSTROUTING -o eth0 -m set --match-set non-vpn dst -j MASQUERADE
iptables -t nat -A POSTROUTING -o tun0 -m set ! --match-set non-vpn dst -j MASQUERADE



route 信息:

Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.8.0.1 128.0.0.0 UG 0 0 0 tun0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
10.8.0.0 * 255.255.255.0 U 0 0 0 tun0
外网 IP 192.168.1.1 255.255.255.255 UGH 0 0 0 eth0
127.0.0.1 192.168.1.1 255.255.255.255 UGH 0 0 0 eth0
128.0.0.0 10.8.0.1 128.0.0.0 UG 0 0 0 tun0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
192.168.6.0 * 255.255.255.0 U 0 0 0 br-lan



ifconfig 网卡信息:

br-lan Link encap:Ethernet HWaddr ********
inet addr:192.168.6.1 Bcast:192.168.6.255 Mask:255.255.255.0
inet6 addr: fd74:b959:13de::1/60 Scope:Global
inet6 addr: fe80::457:2ff:fe85:67ce/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:30679 errors:0 dropped:0 overruns:0 frame:0
TX packets:40267 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2791569 (2.6 MiB) TX bytes:37067870 (35.3 MiB)

eth0 Link encap:Ethernet HWaddr ********
inet addr:192.168.1.30 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::4cac:92ff:fe35:c480/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:42954 errors:0 dropped:1830 overruns:0 frame:0
TX packets:31589 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:40242889 (38.3 MiB) TX bytes:5667545 (5.4 MiB)
Interrupt:31

eth1 Link encap:Ethernet HWaddr********
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:30751 errors:0 dropped:0 overruns:0 frame:0
TX packets:40261 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3225443 (3.0 MiB) TX bytes:37067141 (35.3 MiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:43483 errors:0 dropped:0 overruns:0 frame:0
TX packets:43483 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:42440596 (40.4 MiB) TX bytes:42440596 (40.4 MiB)

tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:10.8.0.2 P-t-P:10.8.0.2 Mask:255.255.255.0
inet6 addr: fe80::ae94:5ef8:ae8e:4cea/64 Scope:Link
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:14631 errors:0 dropped:0 overruns:0 frame:0
TX packets:9002 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:15496526 (14.7 MiB) TX bytes:829920 (810.4 KiB)

================================================================================

想通过 iptables 实现 ipset 列表的 ip(强内)走 eth0 出去
其余 ip(强外)走 tun0 实现饭强。

现在是非 ipset 的 ip 列表可以走 tun0 成功饭强,但是 ip 列表走 eth0 失败,不通,请问一下问题出在哪里?
1992 次点击
所在节点    Linux
7 条回复
defunct9
2022-06-21 11:10:52 +08:00
试试 snat

iptables -t nat -A POSTROUTING -s 10.10.0.0/16 -j SNAT --to 172.16.8.1
sujin190
2022-06-21 11:52:10 +08:00
nat 是 route 完成后才走这个,iptables 的 nat 表改不了 route 路由出口信息的,走 tun0 成功是因为这个默认路由优先级高,想要走不同路由叫策略路由 route rule ,iptables 配合策略路由的是 mangle 链表
0bing
2022-06-21 12:29:42 +08:00
建议做一个路由规则 table xx
然后 mangle 给需要去往天国的打个标签
然后把打了标签的丢给这个路由规则
lazywen
2022-06-21 13:25:06 +08:00
原因 2 楼解释了,你也可以这样解决:
1.先把 tun0 的 default 去掉,只留一个 eth0 defalut
2.第一条规则改成 --match-set non-vpn dst -j RETUEN
qbqbqbqb
2022-06-21 13:26:13 +08:00
其实这个就是常见的一种对 iptables 的误解:很多人认为 iptables 里的 NAT 规则( SNAT 和 MASQUERADE ;这里先不讨论 DNAT )是负责“转发”的。

实际上不是这样的,SNAT 和 MASQUERADE 规则,功能就仅仅是做 NAT ,也就是对从指定出口转发出去的数据包修改源 IP ,并且建立临时映射对返程数据包做对应的处理使其能被正确收到,仅此而已。
至于这个数据包会不会从“指定出口”出去,这个不归 iptables 管,归路由表管。

想实现这个需求需要用到“策略路由”,流程上大概是在 iptables 的 mangle 表里给数据包用 MARK 操作打上标签,然后再在 ip route 这边新建一个路由表 table 并且设定相应的路由,和主路由表区分开来(比如说你现在主路由表是默认走 tun0 ,就新建一张默认走 eth0 的路由表),最后再用 ip rule 里面提供的 fwmark 规则指定打了特定标签的数据包走新建的路由表。
yaott2020
2022-06-22 08:34:22 +08:00
iptables -t mangle -I OUTPUT -m set --match-set proxylist dst -j MARK 0x400

ip rule add fwmark 0x400 table 1000

ip route add 0.0.0.0/0 dev tun0 table 1000

差不多像这样吧
yaott2020
2022-06-22 08:36:04 +08:00
第一条打错了,应该是 -j MARK --set-mark 0x400

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

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

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

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

© 2021 V2EX