找回密码
 立即注册
首页 业界区 安全 Openwrt2203_双wan口_简单的策略路由

Openwrt2203_双wan口_简单的策略路由

呵烘稿 2025-12-5 04:10:08
Openwrt2203_双wan口_简单的策略路由

转载注明来源: 本文链接 来自osnosn的博客,写于 2025-11-20.
环境


  • 使用openwrt2203。 op23,op24的配置方法相同, 因为都是使用的fw4。
  • 有一个lan,两个wan,分别是wan1和wan2。
    lan:192.168.1.1/24, wan1:10.100.9.20/gw 10.100.9.1, wan1:192.168.8.8/gw 192.168.8.1,
  • 仅 ipv4环境,无ipv6。
  • lan口连接一台windows电脑,设置为DMZ主机。win电脑上网,所有数据包走wan1。
    win: 192.168.1.55
  • op配置了 wireguard,但udp走wan1干扰很大,不稳定。
    需要让 wg的 udp包走 wan2。
配置方案


  • 用nft防火墙规则对特定的数据包, 打上标记 (mark 100)
  • 根据 mark 使用特殊的路由表。
    所有数据包 使用默认路由表,走 wan1.
    有 mark标记的数据包,走路由表100,走wan2.
策略路由配置


  • op中,主路由表的缺省路由是: default via 10.100.9.1
  • 测试
    win中使用 tracert -d -w 1 202.12.12.12测试,数据走的是wan1
    op 中使用 traceroute -n -w 1 202.12.12.12测试,数据走的是wan1
  • op中,路由表 100中,添加路由记录
    1. ip route add 10.100.0.0/16 via 10.100.9.1 table 100 metric 10   ## 10.100网段还是走wan1
    2. ip route add 0.0.0.0/0 via 192.168.8.1 table 100 metric 50    ## 其他数据走wan2
    复制代码
    对应的luci操作,
    1. Network -> Routing -> Static IPv4 Routes -> 添加路由
    2. ---- 1 ----
    3. - General Settings
    4.   - Interface: wan1
    5.   - Route type: unicast   #默认值
    6.   - Target: 10.100.0.0/16
    7.   - Gateway: 10.100.9.1
    8. - Advanced Settings
    9.   - Metric: 10
    10.   - Table: 100
    11. - 其他内容: 默认/留空/不改变
    12. ---- 2 ----
    13. - General Settings
    14.   - Interface: wan2
    15.   - Route type: unicast   #默认值
    16.   - Target: 0.0.0.0/0
    17.   - Gateway: 192.168.8.1
    18. - Advanced Settings
    19.   - Metric: 50
    20.   - Table: 100
    21. - 其他内容: 默认/留空/不改变
    复制代码
  • op中,添加路由规则
    ip rule add from all fwmark 100 lookup 100 pref 3000
    对应的luci操作,
    1. Network -> Routing -> IPv4 Rules -> 添加规则
    2. - General Settings
    3.   - Priority: 3000
    4.   - Route type: unicast   #默认值
    5.   - Table: 100
    6. - Advanced Settings
    7.   - Firewall mark: 100
    8. - 其他内容: 默认/留空/不改变
    复制代码
  • 测试, 没有变化
    win中使用 tracert -d -w 1 202.12.12.12测试,数据走的是wan1
    op 中使用 traceroute -n -w 1 202.12.12.12测试,数据走的是wan1
  • op中,添加防火墙规则, 0x64=100
    1. nft add rule inet fw4 mangle_output ip daddr 202.12.12.12 counter meta mark set 0x64
    复制代码
    对应的luci操作,
    1. Network -> Firewall -> Traffic Rules -> 添加规则
    2. - General Settings
    3.   - protocol: Any
    4.   - Source Zone: Device (output)
    5.   - Output Zone: Any zone
    6.   - Destination address: 202.12.12.12
    7.   - Action: XOR firewall mark
    8.   - XOR mark: 100
    9. - Advanced Settings
    10.   - Restrict to address family: IPv4 only
    11. - 其他内容: 默认/留空/不改变
    复制代码
  • 测试, op自身走 wan2
    win中使用 tracert -d -w 1 202.12.12.12测试,数据走的是wan1
    op 中使用 traceroute -n -w 1 202.12.12.12测试,数据走的是wan2
  • op中,添加防火墙规则, 0x64=100
    1. nft add rule inet fw4 mangle_prerouting iifname "br-lan" ip daddr 202.12.12.12 counter meta mark set 0x64
    复制代码
    对应的luci操作,
    1. Network -> Firewall -> Traffic Rules -> 添加规则
    2. - General Settings
    3.   - protocol: Any
    4.   - Source Zone: LAN
    5.   - Destination Zone: Any zone
    6.   - Destination address: 202.12.12.12
    7.   - Action: XOR firewall mark
    8.   - XOR mark: 100
    9. - Advanced Settings
    10.   - Restrict to address family: IPv4 only
    11. - 其他内容: 默认/留空/不改变
    复制代码
    win中使用 tracert -d -w 1 202.12.12.12测试,数据走的是wan2
  • op中, 删除上面两条防火墙规则,只添加下面这条, 对端 wg的端口是 34567 和 34568,
    1. nft add rule inet fw4 mangle_output meta nfproto ipv4 udp dport { 34567, 34568 } counter meta mark set 0x64
    复制代码
    对应的luci操作,
    1. Network -> Firewall -> Traffic Rules -> 添加规则
    2. - General Settings
    3.   - protocol: UDP
    4.   - Source Zone: Device (output)
    5.   - Output Zone: Any zone
    6.   - Destination port: 34567 34568   #空格隔开的两个端口号
    7.   - Action: XOR firewall mark
    8.   - XOR mark: 100
    9. - Advanced Settings
    10.   - Restrict to address family: IPv4 only
    11. - 其他内容: 默认/留空/不改变
    复制代码
  • op配置 wg连接外部的两台机器。wg通过 wan2 连接成功。
--end----
转载注明来源: 本文链接 https://www.cnblogs.com/osnosn/p/19309459.html
来自 osnosn的博客 https://www.cnblogs.com/osnosn/ .

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册