V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
MuskLee
V2EX  ›  Linux

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

  •  
  •   MuskLee · 2022-06-21 11:01:35 +08:00 · 1957 次点击
    这是一个创建于 646 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 失败,不通,请问一下问题出在哪里?
    7 条回复    2022-06-22 08:36:04 +08:00
    defunct9
        1
    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
        2
    sujin190  
       2022-06-21 11:52:10 +08:00   ❤️ 2
    nat 是 route 完成后才走这个,iptables 的 nat 表改不了 route 路由出口信息的,走 tun0 成功是因为这个默认路由优先级高,想要走不同路由叫策略路由 route rule ,iptables 配合策略路由的是 mangle 链表
    0bing
        3
    0bing  
       2022-06-21 12:29:42 +08:00
    建议做一个路由规则 table xx
    然后 mangle 给需要去往天国的打个标签
    然后把打了标签的丢给这个路由规则
    lazywen
        4
    lazywen  
       2022-06-21 13:25:06 +08:00 via Android
    原因 2 楼解释了,你也可以这样解决:
    1.先把 tun0 的 default 去掉,只留一个 eth0 defalut
    2.第一条规则改成 --match-set non-vpn dst -j RETUEN
    qbqbqbqb
        5
    qbqbqbqb  
       2022-06-21 13:26:13 +08:00   ❤️ 2
    其实这个就是常见的一种对 iptables 的误解:很多人认为 iptables 里的 NAT 规则( SNAT 和 MASQUERADE ;这里先不讨论 DNAT )是负责“转发”的。

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

    想实现这个需求需要用到“策略路由”,流程上大概是在 iptables 的 mangle 表里给数据包用 MARK 操作打上标签,然后再在 ip route 这边新建一个路由表 table 并且设定相应的路由,和主路由表区分开来(比如说你现在主路由表是默认走 tun0 ,就新建一张默认走 eth0 的路由表),最后再用 ip rule 里面提供的 fwmark 规则指定打了特定标签的数据包走新建的路由表。
    yaott2020
        6
    yaott2020  
       2022-06-22 08:34:22 +08:00 via Android
    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
        7
    yaott2020  
       2022-06-22 08:36:04 +08:00 via Android
    第一条打错了,应该是 -j MARK --set-mark 0x400
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3274 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 14:04 · PVG 22:04 · LAX 07:04 · JFK 10:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.