b4 icon indicating copy to clipboard operation
b4 copied to clipboard

[Feature Request] Multi-WAN Failover with Per-Device Routing Priorities

Open CptVinyl opened this issue 3 weeks ago • 9 comments

Hi Daniel!

First of all, thank you for developing B4! I've been experimenting with its routing capabilities and would like to suggest a possible improvement.

In my case things looks the next. A home network with two internet connections:

WAN 1: Primary cable ISP, used by most devices. WAN 2: LTE modem, used as a backup connection but also preferred by certain devices (such as smart home equipment). <- IOT should go there by priority. WireGuard VPN: Used only for specific domains defined in a bypass list.

The goal is to use both connections simultaneously while maintaining automatic failover.

When both connections are available: Regular devices → WAN 1. Selected devices (e.g., IoT) → WAN 2. Domains matching the bypass list → WireGuard VPN, regardless of which WAN a device normally uses. All other traffic → directly through the device's preferred WAN.

If either ISP goes down: Devices using the failed connection automatically switch to the remaining WAN. VPN routing rules continue working whenever the VPN is available. When the failed connection recovers, devices return to their preferred WAN.

Currently B4 already supports routing sets through specific network interfaces, which is great.

However, each set can only specify one output interface. With the Kill Switch disabled, traffic can fall back to the system's default route if that interface disappears, but this doesn't provide configurable per-device failover or detect situations where the interface remains up while internet connectivity is lost.

Some routers already support Multi-WAN policies, but their native policy-routing rules can take precedence over B4's domain-based VPN routing, making it difficult to combine both features.

Suggestion

Would it be possible to add: Primary and fallback output interfaces for routing sets. Automatic connectivity checks (ICMP/TCP) and failover/failback. Compatibility with existing per-device and domain-based routing, so VPN rules can still take priority over general WAN routing. This would allow B4 to handle both selective VPN routing and Multi-WAN redundancy without relying on potentially conflicting routing policies from the router's firmware.

Would you consider implementing something like this?

Thanks again for your work!

CptVinyl avatar Nov 07 '26 17:11 CptVinyl

Both directions are possible here, and the choice changes the size a lot, so I would rather hear which one fits b4.

Where the conflict comes from. b4 installs a set's rule at 10000 + table, so any policy rule the router already has, usually 100-2000, is consulted first. That is why a domain set can lose to the router's own multi-WAN choice.

Option A, small. Let a routing set place its rule ahead of the router's policy rules, through a setting or by finding the lowest foreign priority. The set then always leaves by its interface, and everything else stays on the router's multi-WAN together with the failover the firmware already does. This needs a config field, the three places that install the rule, docs and a netns test. It fixes the "VPN rules must win" half and leaves failover where it already works.

Option B, large. b4 owns the failover: a fallback egress per set, connectivity probes per interface (ICMP or TCP through SO_BINDTODEVICE), switching and failing back with hysteresis, route and source rewrite on transition, plus UI, docs and netns tests on two dummy uplinks. This covers a link that stays up without internet, which the firmware's own rules miss. It is a much bigger change, and it makes b4 responsible for connection tracking and NAT across a switch.

Worth noting: per-device priorities already work today, with two sets that use source_devices and their own egress_interface, so "IoT on WAN2, the rest on WAN1" needs no new code. What is missing is health and failback.

A is a small PR I can send. B is fine too, but as a separate agreement. Which one do you want in b4?

asovetov avatar Nov 09 '26 11:11 asovetov

@CptVinyl Thanks for the write-up. Most of this layout can be built today, and one part is a real gap.

What works now

  • IoT on WAN 2: a set with those devices under Source Devices, Match any IP address on, and WAN 2 as Output Interface.
  • Bypass domains through WireGuard: a set with the domains and the WireGuard interface. A set limited to devices is evaluated before the others, so for the IoT devices add a copy of that set limited to them, above the IoT set.
  • Everything else stays on the router's default uplink, and the firmware's own failover keeps moving it between WAN 1 and WAN 2.
  • With Kill switch off, a set whose output interface disappears or is set down leaves by the normal uplink and returns when the interface is back.

What is missing

b4 does not test whether an output interface still has connectivity, so an LTE modem or a tunnel that stays up without internet keeps receiving the set's traffic. An optional connectivity check that treats a dead interface like a missing one is the part I would add. b4 will not take over WAN failover itself: the firmware and mwan3 do that.

To go further I need the router model and firmware, the b4 version, the System Info JSON, how the LTE modem is attached, and the output of ip rule from the setup where the firmware policy won. See https://docs.b4core.app/docs/troubleshooting/reporting.

@asovetov thanks for offering. On A: KeeneticOS writes a whole mark for devices in a policy, and mwan3 adds its interface bits under b4's mask, so in both the set's rule stops matching whatever its priority. Let's see the reporter's ip rule first. B as a whole is too large; the narrow part above is what I would take, design first.

DanielLavrushin avatar Nov 12 '26 11:11 DanielLavrushin

Second data point, from our own router (same Keenetic family as the reporter's, so the mark layout should be comparable).

Setup: Netcraze Ultra (NC-1812, Keenetic Ultra equivalent), KeeneticOS 4.x line — kernel 4.9-ndm-5, arm64, 3 cores / 991 MB, b4 1.90.0 (c120603) from Entware, iptables 1.4.21, NFQUEUE engine, three routing sets (one of them a domain set going out through a local proxy). Right now the WAN is a modem on the LAN port; a USB modem is on hand, so a SIM can go into it in a couple of days and we can test both attachment paths.

Our ip rule — and yes, it matches what you described: the firmware's own policy rule is consulted before b4's, exactly as you said about the whole mark vs the interface bits:

2154:  from 192.168.1.57 lookup 16386              # firmware policy for one device
10125: from all fwmark 0x6d73/0x27fff lookup 125   # b4 set
32766: from all lookup main

b4's set rule is at priority 10125 with the interface bits under its mask (0x27fff), while the firmware's rule at 2154 selects by source address into its own table. So raising b4's rule priority cannot win against it — the packets are already routed by the firmware rule. Agreed that A is not worth pursuing on this platform.

On the narrow part of B (treating an interface that is up but has no connectivity like a missing one): that is the gap we can exercise here. Our failure mode would be exactly that — pull the SIM out of the USB modem, or drop the uplink behind the LAN modem while the interface keeps carrier — and the set keeps sending, because the interface is still up. Happy to run it when you have a design; tell us what you want observed (b4_recent_connections output, conntrack marks, output interface of the packets) and we will report back with logs. No urgency from our side: our current sets route into a local proxy and a tunnel, and we have a rollback binary on the box.

asovetov avatar Nov 13 '26 17:11 asovetov

Hi @DanielLavrushin and @asovetov!

First of all, apologies for the late reply, and thank you both for the detailed explanations and the offer to help investigate this.

I've reproduced the issue and collected the requested diagnostics.

Setup

  • Router: Keenetic Giga (KN-1011)
  • Firmware: KeeneticOS 5.1.8
  • B4: 1.92.0 (2e6916a), installed through Entware
  • Packet processing: NFQUEUE / iptables
  • WAN 1: Primary wired internet connection
  • WAN 2: Secondary internet connection provided over Ethernet by a separate LTE router/modem
  • VPN: WireGuard managed by KeeneticOS (nwg1 is the active tunnel)

KeeneticOS sees the two WAN links as independent internet connections and provides its own priorities and failover.

B4 routing sets

These are generic descriptions rather than the literal local set names:

  1. Sites via VPN — enabled. Selected domain categories are routed to nwg1, regardless of the source device.
  2. Main PC — Direct WAN — disabled. An all-destinations (0.0.0.0/0) set for the main PC that previously selected WAN 2.
  3. Test Set — disabled. Previously used to test VPN routing.

Only the first B4 routing set is currently enabled.

Steps and observations

  1. Assign the Main PC to the WAN 2 internet-access policy in KeeneticOS, while keeping the B4 device-specific direct-WAN set disabled.
  2. Access a domain matched by Sites via VPN. B4 Traffic associates the connection with the VPN set, but the traffic does not consistently use the VPN egress. At one point an external IP check showed the WAN 2 IP instead of the VPN IP. Later, requests to a locally blocked destination failed with ERR_CONNECTION_RESET.
  3. Move the same PC back to the default KeeneticOS policy. The domain then works through WireGuard, and the external IP check shows the VPN exit IP.
  4. Other devices using the default policy also successfully use the B4 VPN set.

Local ISPs restrict access to the destination used in the test, so ERR_CONNECTION_RESET by itself is not evidence of a B4 bug. The relevant observation is that the routing outcome changes when the KeeneticOS policy assigned to the PC changes.

IPv4 policy routing rules

Output of ip -4 rule show, with addresses consistently replaced by documentation/private example addresses (the table numbers, priorities, marks and masks are unchanged):

0:	from all lookup local
10:	from all fwmark 0xffffa00 lookup main
100:	from all fwmark 0xffffaaa lookup 4096
101:	from all fwmark 0xffffaaa lookup unspec blackhole
102:	from all fwmark 0xffffaab lookup 4098
103:	from all fwmark 0xffffaab lookup unspec blackhole
104:	from all fwmark 0xffffaac lookup 4100
105:	from all fwmark 0xffffaac lookup unspec blackhole
106:	from all fwmark 0xffffaad lookup 4102
107:	from all fwmark 0xffffaad lookup unspec blackhole
108:	from all fwmark 0xffffaae lookup 4104
109:	from all fwmark 0xffffaae lookup unspec blackhole
1154:	from 198.51.100.10 lookup 16386
1156:	from 203.0.113.10 lookup 16387
1172:	from 10.250.20.2 lookup 16395
10104:	from all fwmark 0x67a0/0x27fff lookup 104
32766:	from all lookup main
32767:	from all lookup default

System Info JSON

I have also attached an anonymized B4 System Info JSON (b4_system_info_redacted.json), collected after updating the routing set to use the active nwg1 tunnel.

For privacy, IP addresses, MAC addresses, the router hostname, storage UUID, and autogenerated chain identifier have been replaced consistently. Routing priorities, marks, masks, table IDs, interface names and rule structure are preserved. The addresses in the file are examples, not usable device configuration.

Questions

  • Is a device-scoped VPN set above a device-scoped direct-WAN set expected to work even when the device is assigned to a separate native KeeneticOS internet policy? Or should the device first be returned to KeeneticOS's default policy?
  • Would any additional targeted logs or tests help identify where KeeneticOS's marking conflicts with B4's routing rules?

I understand that full Multi-WAN management should remain with the router firmware. The proposed optional connectivity checks for B4 output interfaces would still be very useful.

Thanks again for your work and for taking the time to look into this!

b4_system_info_redacted.json

CptVinyl avatar Nov 17 '26 16:11 CptVinyl

@CptVinyl thanks, the ip rule output answers it.

Your first question: for b4's routing, the PC has to be in the default policy. KeeneticOS gives every packet of a device in a non-default policy that policy's own mark, and its rules 100-109 (fwmark 0xffffaaa ... 0xffffaae, each with a blackhole after it) route by that mark before b4's rule (10104). b4's rule never matches those packets, so no set with an Output Interface routes such a device, whether the set is limited to devices or not. The PC's connections to the VPN sites therefore left by WAN 2 in the clear, which fits the WAN 2 address and the reset. Rules 1154, 1156 and 1172 (from <address> lookup ...) are KeeneticOS's rules for the router's own WAN and nwg1 addresses, not device policies.

What works for your layout today

  • Devices that never need the VPN sites, such as most IoT devices, can stay in your KeeneticOS WAN 2 policy. KeeneticOS keeps their failover.
  • Devices that need both WAN 2 and the VPN sites, such as the PC, go back to the default policy and get two b4 sets:
    • a set with the device under Source Devices, Match any IP address on, the WAN 2 interface as Output Interface, and the LTE router's LAN address as Gateway. b4 looks for an interface's gateway only in the main routing table, and KeeneticOS keeps WAN 2's default route in its own tables, so with Gateway empty the set's route has no next hop and its connections fail;
    • a copy of Sites via VPN limited to the same device, placed above that set. A set limited to devices takes their traffic before the sets that are not, so without the copy their VPN sites would go to WAN 2. That is the case the editor's warning about Match any IP address is about.
  • Sites via VPN covers every other device in the default policy.

The limit, as far as I can see: a b4 set does not follow failover. b4 reacts only when the output interface goes down or disappears. WAN 2 is Ethernet to the LTE router, so unless KeeneticOS takes that interface down when LTE loses service, b4 keeps sending the set's traffic into it, with Kill switch on or off. That is the case the connectivity check is for: an interface that fails the check will be treated like a missing one, so with Kill switch off its traffic goes by the router's normal routing until the check passes again. I will post the design here before writing it.

To confirm this on 5.1.8, with the PC in the WAN 2 policy, please send:

  • iptables -t mangle -L -n -v --line-numbers, the whole table, with MAC addresses replaced
  • cat /sys/class/net/nwg1/uevent
  • ip -4 route show table all, with addresses replaced the same way as before
  • the line on the Logs page for a connection to one of the VPN sites (the log level has to be Info or more detailed)
  • whether the dashboard shows "IPv6 traffic skips all sets": b4 has no IPv6 rule on your router while your LAN has global IPv6

@asovetov, rule 2154 in your listing is most likely the same kind of rule for your WAN address; device policies appear as fwmark rules like 100-109 above. For the connectivity check, the case to reproduce is the one you described: the SIM out, or the uplink behind the LAN modem cut, while the interface keeps carrier. During the outage, please note whether KeeneticOS sets the WAN interface down (ip link show <iface>) and what the set's table holds (ip route show table <N>, N from b4's fwmark ... lookup N rule).

DanielLavrushin avatar Nov 19 '26 06:11 DanielLavrushin

Hi @DanielLavrushin

My apologize for LLMs since Im not good at English 😥

Thank you for the detailed diagnosis and the working configuration you outlined. That explains why the Main PC's native KeeneticOS WAN 2 policy overrides b4's VPN routing. I appreciate the clarification about the gateway requirement too.

I've now collected the five items you requested with the Main PC still assigned to the non-default WAN 2 policy in KeeneticOS. During this test, only the general Sites via VPN b4 set was enabled (nwg1); the b4 device-specific WAN 2 set and the old test set remained disabled.

Diagnostics attached

All requested material is in b4_issue_526_diagnostics_redacted.zip:

  1. 01_iptables_mangle_redacted.txt — complete output of iptables -t mangle -L -n -v --line-numbers (including counters).
  2. 02_nwg1_uevent.txt — output of cat /sys/class/net/nwg1/uevent.
  3. 03_ipv4_routes_all_redacted.txt — complete output of ip -4 route show table all.
  4. 04_b4_info_log_excerpt_redacted.txt — selected Info log entries for a connection from the Main PC to yandex.ru.
  5. 05_ipv6_dashboard_and_test_conditions.txt — IPv6 dashboard warning and the test conditions.

All router/household IP addresses, MAC addresses, personal router hostnames and the per-installation b4 chain identifier have been anonymized consistently. Routing-table numbers, marks, masks, interfaces, rule order, counters and diagnostic flags are unchanged. Public destination IPs in the small log excerpt are kept for context.

What the captures show

In _NDM_HOTSPOT_PREROUTING_MANGL, KeeneticOS marks the Main PC's traffic using 0xffffaad (the MARK rule had 2,726 packets in this snapshot):

21    2726  773K MARK ... MAC 02:00:00:00:00:01 MARK set 0xffffaad
22    2726  773K CONNMARK ... MAC 02:00:00:00:00:01 CONNMARK save

The firmware's fwmark 0xffffaad rule directs it to table 4102, whose default route goes via WAN 2 (eth2.5). Meanwhile the b4 Info log shows the VPN-domain match and [routed->nwg1] for this device; I understand this is b4's routing decision, not proof that the actual packets egressed via the tunnel.

106:   from all fwmark 0xffffaad lookup 4102
10104: from all fwmark 0x67a0/0x27fff lookup 104

nwg1 is present as a WireGuard interface (DEVTYPE=wireguard, INTERFACE=nwg1, IFINDEX=43). The WAN 2 route's next hop is visible as via 198.51.100.1 dev eth2.5 in the redacted capture (the actual gateway differs).

IPv6: yes, the Dashboard shows exactly this warning:

IPv6 traffic skips all sets: the router has IPv6, b4's IPv6 is off

I haven't tested the suggested three-set configuration yet. I'll try it separately with the Main PC returned to KeeneticOS's default policy and report back if I encounter any problems. I also understand the current failover limitation when the Ethernet interface stays up but its upstream connectivity fails, and look forward to your proposed connectivity-check design.

Please let me know if any more targeted output would be helpful. Thanks again for your help and for your work on b4!

b4_issue_526_diagnostics_redacted.zip

CptVinyl avatar Nov 19 '26 19:11 CptVinyl

Measured on our box: Keenetic Ultra class (Netcraze NC-1812, KeeneticOS 4.9-ndm-5, arm64), b4 1.92.0 (2e6916a), NFQUEUE, iptables 1.4.21. The uplink is an LTE modem on its LAN port (eth3, our address 192.168.1.57, gateway 192.168.1.1); the modem runs KeeneticOS and raises PPPoE itself. The set that uses it as Output Interface has kill_switch: false, so this is the case you asked about.

We cut LTE on the modem (mobile connection off in its UI) and left the Ethernet link alone. A loop on the router logged every 5 s: operstate, ip link show eth3, ip route show table 125, ip neigh show 192.168.1.1, ping 8.8.8.8. Two runs, 105 s and 133 s of outage, same result:

== 11:17:43
operstate: up
11: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel qlen 2000
default via 192.168.1.1 dev eth3  src 192.168.1.57
192.168.1.1 dev eth3 lladdr 36:d7:77:d8:68:ec ref 1 used 0/0/0 probes 1 REACHABLE
ping: FAIL
  1. KeeneticOS does not set the interface down. operstate stays up, the flags keep LOWER_UP, the address stays, and the ARP entry for the modem stays REACHABLE, so the modem itself is alive and only its mobile link is gone.
  2. The set's table does not change. table 125 keeps default via 192.168.1.1 dev eth3 src 192.168.1.57 for the whole outage, and ip route get <target> keeps resolving through it.

Recovery is as quiet as the failure: the route never moved, so the first ping: ok after the outage comes with the same table. A check would have to flip state in both directions on its own.

So b4 has no event to react to here: neither the interface state nor the routing table ever changes. With kill_switch off the set keeps sending its traffic into an interface that has carrier and no internet, which is what you predicted. A connectivity check on the egress interface, treating a failed check like a missing interface, is the only way to notice it.

The box is live, so any variant you want is a few minutes away: SIM out instead of the mobile link, kill switch on, a different check target or interval.

asovetov avatar Nov 22 '26 08:11 asovetov

Adding one more piece of evidence, and I want to be clear that this one is from memory rather than from a fresh measurement, so treat it as an observation and not as a test result.

Before the current setup I had two uplinks in KeeneticOS with balancing between them, and one of the two was a USB LTE modem. When that modem ran out of data or lost its session, the whole house stalled instead of degrading: the firmware kept sending roughly half of the new connections into the dead modem, because its interface was still up and registered, and those connections hung until their timeouts instead of failing fast.

So the signal that is missing here is not the network interface at all. A USB stick keeps its interface up for as long as it is plugged in and attached to the network, even with no data session behind it, and the LAN modem we measured behaves the same way. A balancer that looks only at interface state cannot tell a live channel from a live interface.

That is the reason the second modem is out of the balancing on this box. The only thing that separates the two situations is an active check through the uplink, which is the check being discussed in this thread. It would help the sets that b4 routes, but it would not fix the firmware's own balancing, so for an uplink whose health cannot be observed the safer answer is not to balance across it.

asovetov avatar Nov 22 '26 08:11 asovetov

@CptVinyl this was implemented in https://github.com/DanielLavrushin/b4/releases/tag/v1.93.0

DanielLavrushin avatar Nov 25 '26 15:11 DanielLavrushin