gescco.
Networking & proxiesPractical guide

Dual-WAN domain-based routing with iptables and ipset

Combine dnsmasq, ipset, connection marks, policy routing and SNAT for two public uplinks, including return paths, DNS and TTL limits, failure guards, persistence and rollback.

21 min readiptablesipsetdnsmasqDual WANPolicy routingCONNMARK

A Linux gateway with two public uplinks can send connections associated with selected domains through WAN2 and other connections through WAN1. Dnsmasq adds observed DNS answers to ipset, iptables assigns and saves a connection mark, ip rule selects a routing table from that mark, and SNAT uses the chosen uplink.

This is destination-IP steering based on DNS observations. Iptables does not identify the domain inside an HTTPS request; unrelated domains sharing an address can be steered together. Use it where DNS is managed and IP-level classification is acceptable. Strict per-request domain selection needs a different design, such as an application proxy.

The example targets an IPv4-only gateway running Ubuntu Server 24.04 LTS. Sources were checked on 2026-10-07. Private networks, documentation public addresses and reserved domains are illustrative; replace them with your interfaces, addresses and authorized test domains. No commands were applied to a real public gateway and no carrier links were tested.

01 / Topology, traffic scope and mark allocation

text
LAN clients: 10.20.0.0/24
  default gateway + DNS: 10.20.0.1
              |
          lan0: 10.20.0.1/24
          Linux gateway
              |                    |
wan1: 198.51.100.2/24      wan2: 203.0.113.2/24
gateway: 198.51.100.1      gateway: 203.0.113.1
table: 101                table: 102
mark: 0x1/0xff            mark: 0x2/0xff

portal.example.com -> dw_wan1 -> WAN1
cdn.example.net    -> dw_wan2 -> WAN2
other destinations           -> WAN1

All three interfaces already have their addresses, each WAN works independently, and the main table contains the connected LAN route and a WAN1 default. These are distinct upstream gateways, not two interchangeable defaults on one exit.

  • Classify only forwarded connections entering lan0 from 10.20.0.0/24. DNS queries, updates and curl generated by the gateway use OUTPUT and are outside this classifier.
  • Public addresses are static, so use SNAT. Dynamic PPPoE/DHCP needs interface-specific MASQUERADE plus link-event updates to gateways and routing tables.
  • Reserve the low eight mark bits, 0xff, tables 101/102 and priorities 1001/1002. Coordinate with VPNs, containers, QoS and other routing policies before using them.
  • Inbound public DNAT, management services on WAN2 and routed internal networks are outside the example. They need additional ingress connection marking, source routing and ACLs.
  • The baseline has no forwarded IPv6 path for LAN clients. An existing IPv6 exit requires separate IPv6 sets, rules and routes, or an access policy that disables that path. IPv4 rules do not prevent IPv6 bypass.

02 / Inspect software, rule ownership and backups

Prepare iptables, ipset, dnsmasq, iproute2, conntrack, dnsutils and tcpdump on a dedicated lab gateway. Installing Ubuntu's dnsmasq package may start its service, so inspect existing DNS listeners first. These read-only commands establish versions, the firewall backend and current networking:

sh
# Gateway: inspect without changing firewall or routing state.
iptables --version
ipset --version
dnsmasq --version
ip -br address
ip -4 route show table all
ip -4 rule show
sudo iptables-save
sudo ipset list -name
sudo ss -lntup

Dnsmasq's compile options must include ipset, not no-ipset. iptables-nft and iptables-legacy expose different rulesets; use one backend consistently for inspection, saving and restoration. An ipset is not a native nftables set: replacing ipset= with nftset= requires changing the rest of the design. Verify support for the set, MARK, CONNMARK and conntrack extensions on the installed kernel and backend.

If UFW, firewalld, Docker or another tool owns the firewall, integrate with its lifecycle rather than letting managers overwrite each other. This example requires out-of-band access and controlled test clients. Preserve management INPUT rules and inbound default-deny policy. Pause test-client traffic during initial setup so partially applied stages do not affect new connections.

sh
# Gateway: retain this directory and shell variable for rollback.
umask 077
egress_backup_dir=$(mktemp -d "$PWD/domain-egress-before.XXXXXX")
sudo iptables-save > "$egress_backup_dir/iptables.v4"
sudo ipset save > "$egress_backup_dir/ipset.save"
ip -4 rule show > "$egress_backup_dir/rules.txt"
ip -4 route show table all > "$egress_backup_dir/routes.txt"
sysctl net.ipv4.ip_forward \
  net.ipv4.conf.all.rp_filter net.ipv4.conf.lan0.rp_filter \
  net.ipv4.conf.wan1.rp_filter net.ipv4.conf.wan2.rp_filter \
  > "$egress_backup_dir/sysctl.txt"

Also back up dnsmasq configuration, interface configuration and relevant systemd units. The text route listing is for review, not the binary input required by ip route restore. Subsequent commands run on the gateway unless labeled as client commands. Apply these state changes only after replacing addresses and checking ownership conflicts.

03 / Create domain sets and destination exclusions

Confirm that dw_wan1, dw_wan2 and dw_bypass4 do not already exist. The first two hold IPv4 addresses, while the third holds destinations excluded from public-uplink classification. Create sets before dnsmasq and before rules that reference them.

sh
# Gateway: first installation; abort on unexpected existing set names.
sudo ipset create dw_wan1 hash:ip family inet hashsize 1024 maxelem 65536
sudo ipset create dw_wan2 hash:ip family inet hashsize 1024 maxelem 65536
sudo ipset create dw_bypass4 hash:net family inet

for network in \
  0.0.0.0/8 10.0.0.0/8 100.64.0.0/10 127.0.0.0/8 \
  169.254.0.0/16 172.16.0.0/12 192.168.0.0/16 \
  198.18.0.0/15 224.0.0.0/4 240.0.0.0/4 \
  198.51.100.0/24 203.0.113.0/24; do
  sudo ipset add dw_bypass4 "$network"
done

The last two entries are the example WAN connected networks; replace them for the real topology. Add internally routed public ranges, VPN networks and private-circuit routes where appropriate. An exclusion retains existing routing and ACLs; it does not grant access.

The example deliberately omits timeouts to avoid expiring an address while clients still use cached DNS answers. Old addresses therefore accumulate, requiring the cleanup and capacity controls in section 10. This is not a maintenance-free permanent inventory. See the ipset manual for set types, timeouts and swapping.

04 / Policy tables and reverse-path validation

Verify that tables 101/102 and priorities 1001/1002 are unused. Each table receives its own connected WAN route, a LAN return route and a default. These commands add separate tables without replacing the main default; the main table still handles gateway-generated traffic and unmarked replies.

sh
# Gateway: tables and priorities are reserved exclusively for this example.
sudo ip -4 route add table 101 10.20.0.0/24 dev lan0 scope link
sudo ip -4 route add table 101 198.51.100.0/24 dev wan1 scope link src 198.51.100.2
sudo ip -4 route add table 101 default via 198.51.100.1 dev wan1 metric 10
sudo ip -4 route add unreachable default table 101 metric 32760

sudo ip -4 route add table 102 10.20.0.0/24 dev lan0 scope link
sudo ip -4 route add table 102 203.0.113.0/24 dev wan2 scope link src 203.0.113.2
sudo ip -4 route add table 102 default via 203.0.113.1 dev wan2 metric 10
sudo ip -4 route add unreachable default table 102 metric 32760

sudo ip -4 rule add priority 1001 fwmark 0x1/0xff lookup 101
sudo ip -4 rule add priority 1002 fwmark 0x2/0xff lookup 102
ip -4 rule show

The higher-metric unreachable default is a rejecting fallback within each table. If the normal default is removed, it prevents an empty lookup from falling through to WAN1 in the main table. It is not a health probe: a carrier outage beyond a reachable gateway does not automatically trigger failover. The later FORWARD guard also rejects mark/interface mismatches if a table is accidentally removed. See the ip-rule manual and ip-route manual.

Enable forwarding, then use loose rp_filter on the two WAN interfaces for this multihomed router. A WAN2 reply's source may have its best reverse route through WAN1 in the main table, causing strict validation to drop a legitimate reply. Do not globally disable source validation.

sh
# Gateway: record old values first; enable only for this routing role.
sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -w net.ipv4.conf.wan1.rp_filter=2
sudo sysctl -w net.ipv4.conf.wan2.rp_filter=2
sudo sysctl -w net.ipv4.conf.lan0.rp_filter=1
sysctl net.ipv4.conf.all.rp_filter \
  net.ipv4.conf.lan0.rp_filter net.ipv4.conf.wan1.rp_filter \
  net.ipv4.conf.wan2.rp_filter net.ipv4.conf.all.src_valid_mark

Linux uses the larger of the all and per-interface rp_filter values. If all=2, the LAN also effectively uses loose validation; reconcile this with the existing policy. Changing ip_forward can reset host/router parameters, so apply settings in order and inspect them again. This example restores connection marks only in the LAN-to-WAN direction; do not assume that globally enabling src_valid_mark=1 is correct for every multi-uplink design. See the Linux networking sysctls.

05 / Feed DNS results into ipset with dnsmasq

Merge this into /etc/dnsmasq.d/70-domain-egress.conf and verify that the main configuration and systemd arguments actually load that directory. 198.51.100.53 represents a reachable upstream recursive resolver, not a real public DNS service. Preserve existing internal-domain, DNSSEC and DHCP settings instead of replacing the complete configuration.

Bind only loopback and the LAN address. Allow TCP/UDP 53 only from trusted LAN clients; the next section supplies those INPUT rules. Explicit upstreams with no-resolv avoid resolver loops. This example adds no DHCP server: have the existing DHCP service advertise gateway 10.20.0.1 and only the controlled DNS server 10.20.0.1. Do not advertise a fallback resolver that does not update these sets.

ini
# Merge into the effective dnsmasq configuration.
listen-address=127.0.0.1,10.20.0.1
bind-interfaces
no-resolv
server=198.51.100.53
cache-size=1000

ipset=/portal.example.com/dw_wan1
ipset=/cdn.example.net/dw_wan2

Domain rules cover the name and queries for its subdomains; they do not enumerate or prefetch every subdomain. Addresses appear only after relevant DNS answers are observed. Give the reserved example names records in a controlled upstream resolver, or substitute domains you own and may test. Local hosts or address= responses are not a substitute for testing the forwarded DNS path. See the dnsmasq manual's ipset option.

With CNAME chains, multiple CDN levels, additional resource domains, HTTPS/SVCB address hints or application caches, verify that every address actually used by clients enters the intended set. One main-site domain does not cover every website dependency. If uplinks need different DNS views, domain-specific upstreams are possible, but inspect the gateway's own resolver-query routes: choosing a resolver does not choose the application's egress.

sh
# Gateway: check the same effective configuration used by the service.
sudo dnsmasq --test --conf-file=/etc/dnsmasq.conf
# After verifying includes, listeners, existing clients and upstream reachability:
sudo systemctl restart dnsmasq
sudo systemctl status dnsmasq --no-pager
sudo journalctl -u dnsmasq -n 40 --no-pager

If the service supplies additional configuration files or directories, use the same arguments for syntax checking. Look for missing sets, absent ipset support or failed set updates in the journal. A successful DNS answer does not prove successful ipset insertion. Resolve update failures before admitting clients.

06 / Marking, SNAT and egress-guard rules

Save the following as domain-egress.v4 on the gateway. It declares only four dedicated chains, does not declare or flush built-in chains, and does not change INPUT/FORWARD policies. Confirm that DW_CLASS, DW_NAT, DW_INPUT and DW_FORWARD do not already exist.

These lab rules allow LAN-initiated traffic through either WAN. Where production access is restricted by destination, port or identity, narrow the ACCEPT rules or delegate authorization to the existing ACLs. Do not place broad acceptance ahead of organizational policy. Mark/interface mismatch drops must precede broad ESTABLISHED acceptance.

iptables
*mangle
:DW_CLASS - [0:0]
-A DW_CLASS -m addrtype --dst-type LOCAL -j RETURN
-A DW_CLASS -m set --match-set dw_bypass4 dst -j RETURN
-A DW_CLASS -j CONNMARK --restore-mark --nfmask 0xff --ctmask 0xff
-A DW_CLASS -m mark ! --mark 0x0/0xff -j RETURN
-A DW_CLASS -m conntrack ! --ctstate NEW -j RETURN
-A DW_CLASS -m set --match-set dw_wan2 dst -j MARK --set-xmark 0x2/0xff
-A DW_CLASS -m mark --mark 0x0/0xff -m set --match-set dw_wan1 dst -j MARK --set-xmark 0x1/0xff
-A DW_CLASS -m mark --mark 0x0/0xff -j MARK --set-xmark 0x1/0xff
-A DW_CLASS -j CONNMARK --save-mark --nfmask 0xff --ctmask 0xff
COMMIT

*nat
:DW_NAT - [0:0]
-A DW_NAT -s 10.20.0.0/24 -o wan1 -m mark --mark 0x1/0xff -j SNAT --to-source 198.51.100.2
-A DW_NAT -s 10.20.0.0/24 -o wan2 -m mark --mark 0x2/0xff -j SNAT --to-source 203.0.113.2
COMMIT

*filter
:DW_INPUT - [0:0]
:DW_FORWARD - [0:0]
-A DW_INPUT -i lan0 -s 10.20.0.0/24 -d 10.20.0.1 -p udp --dport 53 -j ACCEPT
-A DW_INPUT -i lan0 -s 10.20.0.0/24 -d 10.20.0.1 -p tcp --dport 53 -j ACCEPT

-A DW_FORWARD -i lan0 ! -s 10.20.0.0/24 -j DROP
-A DW_FORWARD -m conntrack --ctstate INVALID -j DROP
-A DW_FORWARD -i lan0 -s 10.20.0.0/24 -m mark --mark 0x1/0xff ! -o wan1 -j DROP
-A DW_FORWARD -i lan0 -s 10.20.0.0/24 -m mark --mark 0x2/0xff ! -o wan2 -j DROP
-A DW_FORWARD -i lan0 -s 10.20.0.0/24 -p udp --dport 53 -j REJECT
-A DW_FORWARD -i lan0 -s 10.20.0.0/24 -p tcp --dport 53 -j REJECT
-A DW_FORWARD -i lan0 -s 10.20.0.0/24 -p tcp --dport 853 -j REJECT
-A DW_FORWARD -i lan0 -s 10.20.0.0/24 -p udp --dport 853 -j REJECT
-A DW_FORWARD -i lan0 -s 10.20.0.0/24 -o wan1 -m mark --mark 0x1/0xff -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j ACCEPT
-A DW_FORWARD -i lan0 -s 10.20.0.0/24 -o wan2 -m mark --mark 0x2/0xff -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j ACCEPT
-A DW_FORWARD -i wan1 -o lan0 -d 10.20.0.0/24 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
-A DW_FORWARD -i wan2 -o lan0 -d 10.20.0.0/24 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
COMMIT

Test on the target gateway, then load the currently unhooked chains with --noflush, after creating the sets. Other chains are retained, but a custom chain declared again in the file is flushed and rebuilt; do not use this to overwrite an unknown chain. Commits across tables are not one transaction covering the entire network. Stop and diagnose a failed load before attaching hooks. See the iptables-restore manual.

sh
# Gateway: validate first, then load dedicated chains without built-in hooks.
sudo iptables-restore --wait 5 --test --noflush < domain-egress.v4
sudo iptables-restore --wait 5 --noflush < domain-egress.v4

# First installation only: inspect positions and avoid duplicate hooks.
sudo iptables -w 5 -t filter -I INPUT 1 -j DW_INPUT
sudo iptables -w 5 -t filter -I FORWARD 1 -j DW_FORWARD
sudo iptables -w 5 -t nat -I POSTROUTING 1 -j DW_NAT
# Activate classification last, after routes, guards and SNAT are ready.
sudo iptables -w 5 -t mangle -I PREROUTING 1 -i lan0 -s 10.20.0.0/24 -j DW_CLASS

Review insertion positions against existing ACLs, NAT exemptions and marking rules; position 1 assumes the controlled lab baseline. These rules reject forwarded access to other DNS services on TCP/UDP 53 and the usual 853 ports rather than redirecting DNS. Queries to the gateway use INPUT and remain allowed. DoH over HTTPS cannot reliably be separated from ordinary sites by port 443 alone; endpoint management, a controlled proxy or application policy is needed.

07 / Why default-path connections also need CONNMARK

MARK changes a packet mark; CONNMARK stores it in the connection-tracking entry. Only the low eight bits are modified, preserving higher bits for coordinated features. This assumes exclusive ownership of the low byte. See the iptables extensions manual for mask and SNAT semantics.

  • New connection: exclude local and internal destinations, restore its connection mark, and classify only an unmarked NEW connection. Check WAN2 first, WAN1 second, then assign 0x1 to default traffic too.
  • Later outbound packets: restore 0x1 or 0x2 from conntrack and stop classification. Set changes do not move a previously classified connection.
  • Address in both sets: WAN2 explicitly wins. This resolves an IP-level conflict; it cannot distinguish two domains sharing that IP. Monitor intersections and obtain a business decision on collisions.
  • WAN replies: they do not enter this classifier, which is hooked only on lan0. Conntrack reverses NAT and the main LAN route returns traffic to the client. Do not reclassify both WAN ingress directions by their destination sets.
  • NAT: translation is chosen when the connection mapping is established, then reused. Editing SNAT does not migrate existing sessions, and NAT counters do not represent every packet in a flow.

If default-path traffic remained unmarked, an address added to the WAN2 set could change its classification mid-session. Saving default mark 0x1 and classifying only NEW prevents that. Connections established before deployment without classification marks are not retroactively marked. Let clients finish and reconnect during maintenance instead of flushing the gateway's entire conntrack table.

08 / Validate DNS, routing and the observed public source

For a controlled lab, have upstream DNS return test server 192.0.2.80 for cdn.example.net and 192.0.2.81 for portal.example.com. Real verification needs reachable targets whose logs you may inspect; documentation addresses are not Internet services. Clear the test client's relevant caches or use a fresh query process, then run from a LAN client:

sh
# LAN client: these names need controlled upstream DNS records.
dig @10.20.0.1 cdn.example.net A +noall +answer
dig @10.20.0.1 portal.example.com A +noall +answer

On the gateway, confirm every returned A address enters the correct set; do not check only the first address of a multi-address response. ip route get with an explicit mark tests routing, not whether iptables classified a real connection. Continue with counters and actual traffic.

sh
# Gateway: use the actual DNS answers if they differ from the lab records.
sudo ipset test dw_wan2 192.0.2.80
sudo ipset test dw_wan1 192.0.2.81
ip -4 route get 192.0.2.80 from 10.20.0.10 iif lan0 mark 0x2
ip -4 route get 192.0.2.81 from 10.20.0.10 iif lan0 mark 0x1
sudo iptables -t mangle -nvL DW_CLASS --line-numbers
sudo iptables -t filter -nvL DW_FORWARD --line-numbers
sudo iptables -t nat -nvL DW_NAT --line-numbers
sudo conntrack -L -f ipv4 --mark 0x2/0xff

Create fresh HTTPS connections from a LAN client configured to use the gateway's DNS. The test service needs a valid certificate for its domain; do not disable TLS validation to claim success. Ordinary curl uses the operating system's resolver and does not inherit the previous dig @... server selection, so verify system DNS first.

sh
# LAN client: use authorized HTTPS services with valid domain certificates.
curl -4 --connect-timeout 5 --max-time 15 https://cdn.example.net/
curl -4 --connect-timeout 5 --max-time 15 https://portal.example.com/

Confirm the second public source address for WAN2 and the first for WAN1 in the controlled server logs. Alternatively, capture a small amount of traffic to the test target on each WAN, checking interface choice and translated source addresses. Avoid unrelated user traffic. A public IP-check website shows its own path, not the path of another domain.

sh
# Gateway: run in separate terminals during a new authorized test connection.
sudo tcpdump -ni wan1 -c 10 'host 192.0.2.80 and tcp port 443'
sudo tcpdump -ni wan2 -c 10 'host 192.0.2.80 and tcp port 443'

Also verify default new connections on WAN1, selected new connections on WAN2, collision precedence, stable sessions across domain-set updates, absence of unintended alternate-WAN fallback, DNS bypass restrictions and the intended IPv6 scope. A count-limited capture waits if too few packets arrive; stop it with Ctrl+C when finished.

09 / Troubleshooting and failover boundaries

  • DNS succeeds but the set is empty: verify that queries traverse this dnsmasq, then inspect compile options, configuration includes, address family, permissions, capacity and logs before changing routes.
  • Correct mark, wrong interface: inspect higher-priority ip rule entries, table contents, later mark changes, offload and firewall managers. Classified traffic must traverse these Netfilter chains; separately validate or exclude flowtable and hardware acceleration paths.
  • WAN2 sends but gets no reply: check the translated source, provider source validation, ARP/next-hop reachability, return ACLs and rp_filter, then MTU and ICMP errors.
  • A changed domain list still uses the old exit: check for a reused TCP/QUIC connection. Domain sets choose new connections, not forced migrations of existing sessions.
  • Two websites sharing an IP cannot be separated: this is a destination-IP limitation, not something fixed by another domain string match. HTTPS, connection reuse and shared CDNs may require a proxy design.

This example keeps selected traffic off alternate uplinks when its assigned path fails. Automatic failover needs probes through each actual service path, a policy defining which domains may change public source addresses, and reclassification only for approved new connections. Existing TCP/QUIC NAT mappings remain tied to the old public address and generally need reconnection; two public IPs do not provide seamless session migration.

10 / TTLs, caches, shared addresses and set maintenance

Dnsmasq's ipset= adds observed addresses; it is not a complete domain-to-address-to-TTL lifecycle database. A set configured with timeout 300 does not automatically follow each DNS record's TTL. Client caches, application caches and resolution paths can outlive or differ from the set's timeout.

The no-timeout baseline avoids a simple expiry gap but retains stale addresses. Moving a domain from WAN2 to WAN1 or removing its configuration does not automatically delete previously inserted addresses, which may later belong to other services. Define acceptable retention, collision handling and capacity alarms before deployment.

  • Small controlled environments: maintain the complete active domain inventory, recent answers and observation times. During maintenance, pause new workloads, refresh and validate answers, handle client caches and rebuild sets. Do not flush the live WAN2 set while admitting traffic.
  • Long-running deployments: use a controller that tracks domains, aliases, address provenance, TTLs and grace periods. An address may have several domain references; remove it only after all references expire.
  • Atomic updates: a validated temporary set of the same type can be exchanged with ipset swap, but concurrent dnsmasq writes still create a lost-update problem. Serialize writers, pause admission or reliably replay updates. Swap alone is not a complete synchronization protocol.
  • Restart recovery: restore a validated recent snapshot and warm critical names before allowing new traffic. Clients may retain pre-reboot DNS answers; starting with empty sets can misroute their new connections through the default uplink.

Monitor set size and overlap, update failures, DNS failures, rule counters, conntrack usage and application success on each WAN. If domain accuracy must exceed these boundaries, change the architecture instead of treating observed DNS as reliable per-request identity.

11 / Persist sets, firewall rules and routing together

iptables-save preserves firewall rules, not ipset contents, policy rules, routing tables, addresses, sysctls or dnsmasq configuration. Running netfilter-persistent save alone does not persist this complete design.

Coordinate startup with the existing network manager rather than copying first-install commands unchanged into rc.local:

  • First: restore interface addresses, main routes and reviewed sysctls. Keep initial forwarding denied and verify actual WAN readiness; network-online.target depends on a functioning wait-online implementation.
  • Second: create sets and restore trusted snapshots. Use ipset -exist only after verifying name, type and ownership. Restore tables 101/102, rejecting fallback routes and unique policy rules.
  • Third: restore the firewall after sets exist. A complete iptables-save snapshot already includes hooks, so do not add them again. With the dedicated chain file, check each hook with iptables -C before insertion.
  • Fourth: start dnsmasq, verify set updates, warm critical names and handle old client caches as planned. Admit new workloads only after all stages succeed.
  • Fifth: after DHCP renewal, PPPoE reconnection, network-manager reload or firewall reload, rebuild actual next hops and inspect hooks, sets and policy rules. Fail closed rather than silently opening a default route.

Systemd oneshot services or distribution network hooks can implement this order, but scripts must validate addresses, rule ownership and every step's exit status. Use replace only for routes owned by this policy. Repeated ip rule add creates duplicates: inspect the reserved priority precisely instead of repeatedly adding rules or flushing the entire RPDB.

Finish with a controlled reboot test, including clients holding pre-reboot DNS answers. Saving configuration and successfully restoring it at boot are separate checks.

12 / Rollback order and impact

In a console maintenance window, stop new connections from test clients while preserving DNS and management access. Removing rules does not automatically remove existing NAT mappings or connection marks. Have affected clients close sessions and reconnect after restoration; do not run a global conntrack -F.

Remove these four exact hooks. If a startup script duplicated them, inspect with iptables -S and remove only instances owned by this policy. Detach firewall hooks before removing policy routes so an active classifier cannot send traffic into the wrong main-table path.

sh
# Gateway: maintenance window with affected client traffic stopped.
sudo iptables -w 5 -t mangle -D PREROUTING -i lan0 -s 10.20.0.0/24 -j DW_CLASS
sudo iptables -w 5 -t nat -D POSTROUTING -j DW_NAT
sudo iptables -w 5 -t filter -D FORWARD -j DW_FORWARD
sudo iptables -w 5 -t filter -D INPUT -j DW_INPUT

sudo ip -4 rule del priority 1001 fwmark 0x1/0xff lookup 101
sudo ip -4 rule del priority 1002 fwmark 0x2/0xff lookup 102
# These tables must still be owned exclusively by this example.
sudo ip -4 route flush table 101
sudo ip -4 route flush table 102

Restore the previous dnsmasq configuration and verify its restart, ensuring no process still references these sets. Restore prior firewall access policy, sysctls and persistence changes, then remove dedicated chains and sets. Delete only resources created for this change; stop if other workloads now own them.

sh
# Gateway: hooks removed and dnsmasq no longer references these sets.
sudo iptables -w 5 -t mangle -F DW_CLASS
sudo iptables -w 5 -t mangle -X DW_CLASS
sudo iptables -w 5 -t nat -F DW_NAT
sudo iptables -w 5 -t nat -X DW_NAT
sudo iptables -w 5 -t filter -F DW_INPUT
sudo iptables -w 5 -t filter -X DW_INPUT
sudo iptables -w 5 -t filter -F DW_FORWARD
sudo iptables -w 5 -t filter -X DW_FORWARD
sudo ipset destroy dw_wan1
sudo ipset destroy dw_wan2
sudo ipset destroy dw_bypass4

Verify the original DNS behavior, default route and public source address before restoring workloads. A complete firewall backup may be restored under an approved change plan when there were no concurrent changes, but do not overwrite another administrator's new rules to undo this policy. Do not flush built-in chains or unrelated routes.

References

END OF NOTE / Networking & proxiesView article source ↗