博客 / 詳情

返回

Openwrt2203_雙wan口_簡單的策略路由

Openwrt2203_雙wan口_簡單的策略路由_DMZ設置

轉載註明來源: 本文鏈接 來自osnosn的博客,寫於 2025-11-20.

環境

  • 使用openwrt2203。 op23,op24的配置方法相同, 因為都是使用的fw4。
  • 有一個lan,兩個wan,分別是wan1wan2
    lan:192.168.1.1/24, wan1:10.100.9.20/gw 10.100.9.1, wan2: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 使用特殊的路由表。
    有 mark標記的數據包,走路由表100,走wan2.
    其他所有數據包 使用默認路由表,走 wan1.

策略路由配置

  • op中,主路由表的缺省路由是: default via 10.100.9.1
  • 測試 (2025-12測)
    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
    - 其他內容: 默認/留空/不改變
    

    Firewall mark 支持 "值/掩碼"格式,比如 luci中寫 "0x64/0xff" 表示 "fwmark 0x64/0xff"

  • 測試, (沒有變化) (2025-12測)
    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
    - 其他內容: 默認/留空/不改變
    

    XOR mark 支持 "值/掩碼"格式,比如 luci中寫 "0x64/0xff" 表示 "meta mark set meta mark & 0xffffff64 | 0x64"

  • 測試, (op自身走wan2) (2025-12測)
    win中使用 tracert -d -w 1 202.12.12.12測試,數據走的是wan1
    op 中使用 traceroute -n -w 1 202.12.12.12測試,數據走的是wan2
    如果 "Destination address" 留空,那麼op中的所有訪問都走wan2,win依然走wan1
  • 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
    - 其他內容: 默認/留空/不改變
    

    XOR mark 支持 "值/掩碼"格式,比如 luci中寫 "0x64/0xff" 表示 "meta mark set meta mark & 0xffffff64 | 0x64"

  • 測試, (lan口電腦走wan2) (2025-12測)
    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
    - 其他內容: 默認/留空/不改變
    

    XOR mark 支持 "值/掩碼"格式,比如 luci中寫 "0x64/0xff" 表示 "meta mark set meta mark & 0xffffff64 | 0x64"

  • op配置 wg連接外部的兩台機器。wg通過 wan2 連接成功。
    win電腦走 wan1.
    (2025-12測試成功)
  • 如果是debian,wg連接的配置中,可能需要在 [Interface] 中用 PostUp/PreDown, 把 wg 的路由寫入到 table 100 中。
    也可以指定 wg自動添加的路由寫入指定的table,比如在 [Interface] 中加一行 Table = 100
  • op中沒找到配置PostUp/PreDown的方法,可能是通過"Import configuration"導入配置的辦法,把PostUp/PreDown添加進去。
    op中可以指定 wg自動添加的路由指定寫到 table 100。配置在"Advanced Settings->Override IPv4 routing table"。
  • 上面的"set mark"防火牆規則,只限定了兩個udp口,影響不大,所以沒有添加這個table,或PostUp/PreDown設置。
  • 如果wg對端是 ddns,記得在crontab中加入
    */3 * * * * /usr/bin/wireguard_watchdog

wireguard FwMark

此方法,未做測試 (2025-12)

  • wireguard 本身就可以給 wg 的加密數據包打上 fwmark。
    如需配置,在 [Interface] 中加一行 FwMark = 0x64
    可以配置 wireguard 連接的 fwmark,而無需單獨設置 "nft mangle set mark" 的防火牆規則。
    配合 ip route 和 ip rule 就可以實現,wg 加密數據包走 wan2,其他數據包走 wan1.
  • op中 luci界面 wireguard 配置的 fwmark 的位置在:
    Network -> Interfaces -> wg0的配置 -> Advanced Settings -> Firewall Mark。
    只是,僅支持固定值,比如 0x64。不支持 "值/掩碼" 格式。
  • 如果使用 wireguard本身的 fwmark,就不需要在 PostUp/PreDown 中設置路由規則了。

DMZ設置

  • 【OpenWRT配置完全開放的DMZ主機】
  • 【OpenWrt開啓DMZ】
  • luci操作
    Network -> Firewall -> Port Forwards -> 添加規則
    - General Settings
      - Restrict to address family: Auto 或者 ipv4 only
      - protocol: Any 或者 按需選擇
      - Source Zone: wan
      - External port: 留空 或 1-65535
      - Destination Zone: lan
      - Internal IP address: <內網的dmz主機IP>
      - Internal port: 留空 或 1-65535
    - Advanced Settings
      - 保留默認值
    - 其他內容: 默認/留空/不改變
    
    這條dmz規則,必須是最後一條規則。否則它後面的規則都不生效。

其他

  • 關於"前綴抑制器 (Prefix suppressor)",【OpenWrt 下的策略路由】
    • 前綴抑制器: 拒絕前綴長度小於或等於指定值的路由決策。
    • 比如,新建一個規則,讓指定的主機先匹配 main 路由表,然後設置前綴抑制為 0,那麼其意思就是不匹配 main 表中默認路由,但是匹配其他路由條目。因為默認路由的前綴長度(掩碼長度)=0。
  • 如果有線網絡是 IEEE802.1X 認證。
    需要設置 認證機 為DMZ主機,並且 wan口 mac地址要 copy 認證機的mac。

--end----


轉載註明來源: 本文鏈接 https://www.cnblogs.com/osnosn/p/19309459.html
來自 osnosn的博客 https://www.cnblogs.com/osnosn/ .


user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.