Here's my network diagram:
56.56.56.56 192.168.0.1/24
MAC:AA:BB:CC:DD:EE:01
___________
---| Modem 1 |-------
| ___________ | ___________
Internet ---| | Switch |--------| Machine |
| ___________ | ___________
---| Modem 2 |------- 192.168.0.3/24
___________
67.67.67.67 192.168.0.2/24
MAC:AA:BB:CC:DD:EE:02
- Both modems forward the same ports from the internet to the machine.
- The machine behind the modems should respond appropriately to any request coming from the internet. e.g. Modem 1 packets return via Modem 1, Modem 2 packets return via Modem 2.
- The machine has only one network port and the switch is unmanaged.
- The machine uses
Netplan
, iptables, and iproute2 for network configuration.
I eventually got my solution from article and comments in Policy Routing on Linux based on Sender MAC Address and the Netplan.io reference on Policy-Routing.
The trick is to mark and CONNTRACK incoming packets by source MAC address to a separate routing table via
iptables -t mangle
, and then tell Netplan to use the table to route outgoing packets appropriately.First, we need tables for our packets to be herded into:
Append the following to the file
/etc/iproute2/rt_tables
:Then, tell Netplan about the tables, routes, and marks:
This first part tells
netplan
that packets in these different tables need different default routes. The second part says that some packets will have anfwmark
from iptables, and these packets should be herded into those tables.Then, tell
iptables
to mark packets by their originating MAC address, but only when it's not from the local network (a little script):Then, tell netplan to generate and apply:
e voila!
BONUS ANSWER
If you have more than one internal network (e.g. a VPN via a non-local IP), use
ipset
andiptables
-m set ! -match-set alias
, e.g.then in the iptables script....
Verification
Verify fwmark rules to route tables:
Verify iptables mangle routing:
Verify outgoing table routes: