OPNsense with Two Internet Connections: Failover and Load Balancing
Two WAN interfaces and their gateways, monitoring with address, latency and loss, gateway groups with tiers for failover and load balancing, policy routing in the LAN rules, the default gateway, open connections, DNS, outbound NAT, notifications, a step-by-step example and common mistakes.
The office has a fibre line and, just in case, a second one: a fixed wireless access (FWA) link or a 4G/5G router. One afternoon the fibre goes down because of a fault in the street. E-mails stop leaving, the cloud management software stops answering and someone has to rush over and move a cable. Having two lines is not enough: you need a firewall that notices the fault by itself, moves the traffic to the second line and goes back to the fibre when it recovers. Let's see how to do it with OPNsense: gateway monitoring, groups with tiers for failover and for load balancing, rules, DNS, NAT and the mistakes that make the second line useless exactly when it is needed.
Two WAN interfaces, two gateways
The starting point is a firewall with two interfaces towards the Internet. In our example, with addresses reserved for documentation:
- WAN1, fibre: the WAN interface is connected to the ONT and receives the public address 203.0.113.25 from the provider; the provider's gateway is 203.0.113.1;
- WAN2, 4G line: a free port on the firewall (OPT1, renamed WAN2) is connected to a LAN port of the 4G router; the firewall has the address 192.168.8.2/24 and the gateway is the router, 192.168.8.1;
- LAN: the office network, 192.168.1.0/24, with the firewall as the PCs' default gateway.
On WAN2 the traffic goes through NAT twice: on the firewall and on the 4G router. This is not a problem for browsing, but it is for services you want to publish, because many mobile providers use CGNAT and do not offer a public address reachable from outside. If the router allows it, set it to bridge or IP passthrough mode, so that the firewall receives the mobile line's address directly.
In OPNsense every interface towards the Internet has its own gateway, an object that represents the provider's router reachable through that interface. For OPNsense to treat WAN2 as a real exit to the Internet, the interface configuration must specify the upstream gateway (IPv4 Upstream Gateway). This way automatic outbound NAT and replies to inbound connections also work on WAN2.
Gateway monitoring
The firewall cannot tell by itself whether a line works: the link to the ONT may stay up, with its LED on, even when the fibre is broken further upstream. This is why OPNsense checks every gateway with a small service, dpinger, which sends continuous pings to a monitor address and measures two values:
- latency, the average round-trip time of the pings;
- packet loss, the percentage of pings that get no reply.
Averages are calculated over a configurable time window, usually a few tens of seconds, so a single lost ping does not trigger anything. Each value has two thresholds: above the low threshold the gateway is reported as degraded, above the high one it is considered down. These are the default values, as an indication: they may change with the version and should be adapted to the line.
| Value | Low threshold (warning) | High threshold (gateway down) | Notes for a 4G line |
|---|---|---|---|
| Latency | 200 ms | 500 ms | latency varies a lot with cell load: wider thresholds avoid false alarms |
| Packet loss | 10 % | 20 % | occasional loss is normal; total loss means a fault |
Choosing the monitor address is the most important decision. It must be beyond the line being checked, on the Internet, and reliable: for example the public DNS resolvers of two different providers, one per line. In the examples we use 192.0.2.53 for WAN1 and 198.51.100.53 for WAN2. Different addresses are required, because OPNsense adds a static route to each monitor address through its gateway: the ping to 192.0.2.53 always leaves through WAN1, even when the rest of the traffic goes elsewhere. For the same reason, a line's monitor address must not be a server that the other line also needs, such as the DNS server used by the firewall.
From the firewall shell you can check by hand that WAN2's monitor address answers, using WAN2's address as the source. A plausible output, as an example:
root@opnsense:~ # ping -S 192.168.8.2 -c 3 198.51.100.53
PING 198.51.100.53 (198.51.100.53) from 192.168.8.2: 56 data bytes
64 bytes from 198.51.100.53: icmp_seq=0 ttl=57 time=41.2 ms
64 bytes from 198.51.100.53: icmp_seq=1 ttl=57 time=36.8 ms
64 bytes from 198.51.100.53: icmp_seq=2 ttl=57 time=40.1 ms
--- 198.51.100.53 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 36.8/39.4/41.2/1.9 msTo read latency, loss and paths correctly, see the article on ping and traceroute.
Gateway groups: tiers for failover, the same tier for load balancing
Two monitored gateways are not enough: you have to tell the firewall how to use them. The tool is the gateway group, in which each member is given a tier, from 1 to 5:
- different tiers → failover. Traffic uses the gateways of the lowest available tier. With WAN1 on tier 1 and WAN2 on tier 2, everything goes through the fibre; WAN2 comes into play only when WAN1 is down, and the fibre takes the traffic back when it becomes available again;
- the same tier → load balancing. New connections are distributed in turn among the gateways of the same tier, in proportion to their weight. With weights 3 and 1, about three connections out of four leave through WAN1.
The group also has a trigger level: with the default setting a member leaves the group when it is down, that is when it exceeds the high thresholds; alternatively it can be excluded as soon as it exceeds the packet loss or the latency threshold.
For a small office with a mobile backup line the typical choice is failover: a 4G line often has more variable latency, an address behind CGNAT and sometimes a data cap. Using it all the time, with load balancing, would consume data and make video calls worse without a real benefit.
Using the group: LAN rules and the default gateway
A gateway group does nothing until something uses it. OPNsense applies it in two different places, and both are needed.
LAN rules (policy routing). In the advanced options of a Pass rule you can choose the gateway: if you select the group, traffic matching the rule follows the group instead of the routing table. This is called policy routing. Beware of one consequence: a rule with a gateway sends everything it matches to the Internet, including traffic to other local networks, VLANs or VPNs. This is why a rule without a gateway for internal destinations goes above it, easy to write with OPNsense aliases. As always, rules are read from top to bottom, as explained in the article on how a firewall rule works.
| Order | Action | Source | Destination | Gateway | Purpose |
|---|---|---|---|---|---|
| 1 | Pass | LAN net | alias LOCAL_NETS | default | traffic to the firewall, VLANs and VPNs: stays in the network |
| 2 | Pass | LAN net | any | WAN_FAILOVER | everything else to the Internet, with failover |
The LOCAL_NETS alias contains the company's private networks, for example 192.168.0.0/16 and 10.0.0.0/8.
The firewall's default gateway. LAN rules do not apply to traffic generated by the firewall itself: the DNS resolver, updates, VPNs started from the firewall, notifications. This traffic follows the system's default route. In OPNsense you enable automatic default gateway switching (Allow default gateway switching, in the general system settings) and give WAN1 a better priority than WAN2: when WAN1 goes down, the default route moves to WAN2 and returns to WAN1 when it recovers. Other firewalls let you select the group directly as the default gateway; the result is the same.
Open connections when the line fails
Failover applies to new connections. Those already open do not move: as explained in the article on the stateful firewall, every connection has an entry in the state table, and that entry is tied to the WAN it left through and to its NAT translation. A video call started from the public address 203.0.113.25 cannot continue from another address: for the server on the other side it would be a different connection.
In practice, when the fibre fails, connections open on WAN1 stay stuck until the application gives up on them and opens new ones, which leave through WAN2. To speed up the switch, the firewall's advanced settings have a gateway monitoring section that decides whether to kill the states tied to a gateway when it goes down. Killing them makes applications restart on the new line immediately; keeping them avoids needless interruptions when the gateway is down only because of a false alarm. Option names and locations vary with the version.
The return works the same way: when the fibre recovers, new connections leave through WAN1 again, while those open on WAN2 carry on there until they close.
DNS must work on both lines
A perfect failover is useless if names can no longer be resolved. DNS typically breaks in three ways:
- the firewall only uses the fibre provider's DNS servers, which often answer only their own customers: when traffic leaves through WAN2, with another address, the queries are refused;
- a DNS server is bound to the WAN1 gateway in the general settings: OPNsense lets you assign a gateway to each DNS server, and that server will be reachable only through the fibre;
- WAN1's monitor address is also a DNS server: because of the monitoring static route, queries to that address always leave through WAN1, even when it is down.
The solutions are simple: use the firewall's resolver (Unbound) in recursive mode, which follows the default route and therefore the automatic gateway switch, or configure at least one public DNS server for each WAN, bound to its gateway. LAN PCs keep using the firewall as their DNS server and notice nothing.
Outbound NAT for each WAN
Each line has its own address, so each line needs its own outbound NAT rule, as explained in the article on NAT and port forwarding. In automatic mode OPNsense creates rules for every interface with an upstream gateway: one more reason to set it in the WAN2 configuration. In hybrid or manual mode, however, the rules must be written by hand: if only the WAN rule exists, packets leave WAN2 with the private LAN address and no reply comes back.
For published services, port forwarding is defined on the WAN the requests arrive on. Replies leave through the same WAN the connection came in on, thanks to the reply-to option that OPNsense applies to interfaces with a gateway. The service's DNS name, however, points to a single address: if it must stay reachable during the fault, you need dynamic DNS or a service that updates the record.
The limits of load balancing
Load balancing distributes connections, not packets. This brings some limits worth knowing before enabling it:
- a single connection is no faster: a download over a single TCP connection stays on the line chosen when it opened. With two 100 Mbit/s lines you can get 200 Mbit/s in total, but only by adding up many connections from several users;
- some sites dislike a change of public address: online banking, government portals and management panels may tie the session to the IP address and end it, or ask you to sign in again, when requests arrive from two different addresses;
- different lines have different latencies: voice and video calls suffer from the jumps.
For the second problem OPNsense offers sticky connections, in the firewall's advanced settings: for a configurable time, connections from the same PC always use the same line. Alternatively, above the rule with the group, you add a rule that always sends sensitive destinations, collected in an alias, through WAN1.
Notifications: knowing that the line has failed
A well-made failover is silent: users barely notice it. That is why you need a notification, otherwise the office may work for days on the 4G line, consuming data, without anyone calling the fibre provider. OPNsense records every state change in the gateway log and shows latency, loss and status on the dashboard. To receive an e-mail you can use the built-in Monit service, with a check on the gateway status and messages sent to a company address. It is also useful to log every fault, with times and duration, to use with the provider.
Step-by-step example on OPNsense
Here is the complete sequence for the office in our example. Menus may vary with the version; before starting, back up the configuration, as explained in the article on OPNsense backups and updates.
- Interfaces › Assignments: assign the port connected to the 4G router (OPT1), enable it, rename it WAN2 and give it the address 192.168.8.2/24 with the upstream gateway 192.168.8.1.
- System › Gateways › Configuration: open the two gateways and set the monitor address (192.0.2.53 for WAN1, 198.51.100.53 for WAN2), the thresholds and the priority, better for WAN1.
- System › Gateways › Group: create the WAN_FAILOVER group with WAN1 on tier 1 and WAN2 on tier 2; trigger level: member down.
- System › Settings › General: configure DNS servers for both lines, or use the recursive resolver, and enable automatic default gateway switching.
- Firewall › NAT › Outbound: check that a rule for WAN2 exists; in hybrid or manual mode, add it.
- Firewall › Rules › LAN: add the rule for local networks without a gateway and, below it, the rule to the Internet with the WAN_FAILOVER gateway.
- Firewall › Settings › Advanced: decide whether to kill states when a gateway goes down and, only for load balancing, whether to enable sticky connections.
- Test: unplug the cable between the ONT and the firewall, or better the ONT's fibre cable, and check the status under System › Gateways, then browsing and video calls from a PC.
During the test, a tracert from a LAN PC immediately shows which line is in use: the second hop is the provider's gateway. Excerpt, with indicative values; before and after unplugging the fibre:
C:\> tracert -d -h 2 192.0.2.80
1 <1 ms <1 ms <1 ms 192.168.1.1
2 3 ms 2 ms 3 ms 203.0.113.1
C:\> tracert -d -h 2 192.0.2.80
1 <1 ms <1 ms <1 ms 192.168.1.1
2 28 ms 31 ms 26 ms 192.168.8.1With IPv6 you need separate gateways and groups, because groups combine gateways of the same address family; this article is limited to IPv4.
Common mistakes
- Monitoring the provider's router. If WAN2's monitor address is 192.168.8.1, the 4G router answers even when there is no mobile network: the gateway always looks online and failover never happens. The same applies to the address of the ONT or of a provider router on the fibre. The address to monitor is on the Internet, beyond the line.
- LAN rules without the group. If the rule to the Internet uses the default gateway and automatic gateway switching is not enabled, traffic stays on WAN1 even when it is down. The group exists, but nothing uses it.
- The group rule at the top of the list. Without a rule for local destinations above the one with the group, traffic to VLANs, VPNs or the firewall itself is pushed to the Internet and lost.
- DNS bound to a single WAN. DNS servers bound to the WAN1 gateway, provider DNS servers that answer only their own customers, or a DNS server also used as a monitor address: traffic moves to WAN2, but names are not resolved and “the Internet is down”.
- Outbound NAT only for WAN1. In manual mode, without a rule for WAN2, packets leave with private addresses and never come back.
- Never testing the failover. The configuration is verified by actually disconnecting the main line, at a quiet time, and checking browsing, DNS, VPNs and telephony.
Conclusions
Two lines become real service continuity only if the firewall knows when one is not working and what to do. In practice:
- give each WAN its own gateway and a monitor address on the Internet, different for each line and never on the provider's router;
- adapt the latency and loss thresholds, especially for the mobile line;
- create a group with different tiers for failover; use the same tier only if you really need load balancing;
- use the group in the LAN rules, below a rule for local networks, and enable automatic default gateway switching;
- check DNS and outbound NAT on both lines;
- decide how to handle the states of open connections and set up a notification of line changes;
- test the failover by unplugging the fibre, and repeat the test after every major update.
To learn more about OPNsense, routing, NAT and network security with complete examples, the books and the course listed below are useful.
The video
Further reading
- Book: OPNsense® in Practice — Volume 1: The Foundations of a Professional Journey (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 4: Routing, NAT, and Internet Access (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 5: LAN Security and Integration (English edition)
- Video course: Capire i Firewall con OPNsense (video course in Italian)
- Related article: NAT and Port Forwarding: How a Private Network Talks to the Internet
- Related article: Stateful Firewalls: Why Replies Get Through Without a Rule
- Related article: Aliases in OPNsense: Clearer Firewall Rules That Are Easier to Maintain
- Related article: DNS: How a Name Becomes an IP Address
- Related article: Ping and Traceroute: How to Really Read the Results
- Related article: How a Firewall Rule Works
- Related article: OPNsense: Configuration Backups and Risk-Free Updates
- Related article: Default Gateway: How a Device Gets Out of Its Own Network
- Specialization: Firewalls & Network Security
Technical sources
- Documentazione ufficiale di OPNsense — Gateways e Multi WAN — Gateways with monitor address and latency and loss thresholds, gateway groups with tiers, weight and trigger level, policy routing in the rules, automatic default gateway switching, DNS servers bound to gateways, sticky connections; menus and options depend on the version.
- dpinger — monitoraggio continuo con ICMP echo — Service used by OPNsense to send periodic pings to the monitor address and compute average latency and loss over a time window.
- Manuale di pf.conf(5) — Packet Filter — route-to and reply-to options for policy routing, address pools with round-robin and sticky-address, the state table and the NAT translation stored in the state.
- RFC 792 — Internet Control Message Protocol — Echo request and echo reply messages used by the ping command and by gateway monitoring.
- RFC 1918 e RFC 5737 — Addresses used in the examples: private networks 192.168.1.0/24 and 192.168.8.0/24, documentation addresses 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24.
Standards and official documentation available from the respective organisations.