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中,添加路由记录
- ip route add 10.100.0.0/16 via 10.100.9.1 table 100 metric 10 ## 10.100网段还是走wan1
- ip route add 0.0.0.0/0 via 192.168.8.1 table 100 metric 50 ## 其他数据走wan2
复制代码 对应的luci操作,- Network -> Routing -> Static IPv4 Routes -> 添加路由
- ---- 1 ----
- - General Settings
- - Interface: wan1
- - Route type: unicast #默认值
- - Target: 10.100.0.0/16
- - Gateway: 10.100.9.1
- - Advanced Settings
- - Metric: 10
- - Table: 100
- - 其他内容: 默认/留空/不改变
- ---- 2 ----
- - General Settings
- - Interface: wan2
- - Route type: unicast #默认值
- - Target: 0.0.0.0/0
- - Gateway: 192.168.8.1
- - Advanced Settings
- - Metric: 50
- - Table: 100
- - 其他内容: 默认/留空/不改变
复制代码 - op中,添加路由规则
ip rule add from all fwmark 100 lookup 100 pref 3000
对应的luci操作,- Network -> Routing -> IPv4 Rules -> 添加规则
- - General Settings
- - Priority: 3000
- - Route type: unicast #默认值
- - Table: 100
- - Advanced Settings
- - Firewall mark: 100
- - 其他内容: 默认/留空/不改变
复制代码 - 测试, 没有变化
win中使用 tracert -d -w 1 202.12.12.12测试,数据走的是wan1
op 中使用 traceroute -n -w 1 202.12.12.12测试,数据走的是wan1
- op中,添加防火墙规则, 0x64=100
- nft add rule inet fw4 mangle_output ip daddr 202.12.12.12 counter meta mark set 0x64
复制代码 对应的luci操作,- Network -> Firewall -> Traffic Rules -> 添加规则
- - General Settings
- - protocol: Any
- - Source Zone: Device (output)
- - Output Zone: Any zone
- - Destination address: 202.12.12.12
- - Action: XOR firewall mark
- - XOR mark: 100
- - Advanced Settings
- - Restrict to address family: IPv4 only
- - 其他内容: 默认/留空/不改变
复制代码 - 测试, 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
- nft add rule inet fw4 mangle_prerouting iifname "br-lan" ip daddr 202.12.12.12 counter meta mark set 0x64
复制代码 对应的luci操作,- Network -> Firewall -> Traffic Rules -> 添加规则
- - General Settings
- - protocol: Any
- - Source Zone: LAN
- - Destination Zone: Any zone
- - Destination address: 202.12.12.12
- - Action: XOR firewall mark
- - XOR mark: 100
- - Advanced Settings
- - Restrict to address family: IPv4 only
- - 其他内容: 默认/留空/不改变
复制代码 win中使用 tracert -d -w 1 202.12.12.12测试,数据走的是wan2
- op中, 删除上面两条防火墙规则,只添加下面这条, 对端 wg的端口是 34567 和 34568,
- nft add rule inet fw4 mangle_output meta nfproto ipv4 udp dport { 34567, 34568 } counter meta mark set 0x64
复制代码 对应的luci操作,- Network -> Firewall -> Traffic Rules -> 添加规则
- - General Settings
- - protocol: UDP
- - Source Zone: Device (output)
- - Output Zone: Any zone
- - Destination port: 34567 34568 #空格隔开的两个端口号
- - Action: XOR firewall mark
- - XOR mark: 100
- - Advanced Settings
- - Restrict to address family: IPv4 only
- - 其他内容: 默认/留空/不改变
复制代码 - op配置 wg连接外部的两台机器。wg通过 wan2 连接成功。
--end----
转载注明来源: 本文链接 https://www.cnblogs.com/osnosn/p/19309459.html
来自 osnosn的博客 https://www.cnblogs.com/osnosn/ .
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |