Skip to content

Instantly share code, notes, and snippets.

@mschmitt
Last active July 30, 2024 06:52
Show Gist options
  • Save mschmitt/42b69caeac71004a48f7ea642b0bcec8 to your computer and use it in GitHub Desktop.
Save mschmitt/42b69caeac71004a48f7ea642b0bcec8 to your computer and use it in GitHub Desktop.
Multi-WAN routing (damit Port Forwards funktionieren) mit iptables/iproute2

Auf Basis der MAC-Adresse der Fritze, über die das Paket reinkam, ein Connection Mark an die Verbindung schreiben:

iptables -t mangle -A INPUT -i eth0 -m mac --mac-source a7:bc:5d:23:53:a3 -j CONNMARK --set-xmark 0x1/0xffffffff
iptables -t mangle -A INPUT -i eth0 -m mac --mac-source e3:c0:f9:f1:8d:76 -j CONNMARK --set-xmark 0x2/0xffffffff

Das Connection Mark in ein Routing Mark umschreiben:

iptables -t mangle -A OUTPUT -m connmark --mark 0x1 -j MARK --set-xmark 0x1/0xffffffff
iptables -t mangle -A OUTPUT -m connmark --mark 0x2 -j MARK --set-xmark 0x2/0xffffffff

Alternative Routingtabellen pro Fritze anlegen (habe ich immer als post-up in /etc/network/interfaces):

ip route add default via 192.168.1.1 table 1
ip route add default via 192.168.1.2 table 2

Routing Policies anlegen, die die Routingtabelle aufgrund des gesetzten Routing Mark auswählen (habe ich ebenfalls immer als post-up in /etc/network/interfaces):

ip rule add fwmark 1 table 1
ip rule add fwmark 2 table 2

(Das wars schon, glaube ich.)

@mschmitt
Copy link
Author

Konfiguration am Interface in /etc/network/interfaces:

        # Alle Pakete bekommen in Abhängigkeit der Fritze, von der sie kamen, ein Connection Mark
        post-up /sbin/iptables -t mangle -A INPUT -m mac --mac-source a7:bc:5d:23:53:a3 -j CONNMARK --set-xmark 0x1/0xffffffff
        post-up /sbin/iptables -t mangle -A INPUT -m mac --mac-source e3:c0:f9:f1:8d:76 -j CONNMARK --set-xmark 0x2/0xffffffff
        # Connmark zum Routing Mark umschreiben
        post-up /sbin/iptables -t mangle -A OUTPUT -m connmark --mark 0x1 -j MARK --set-xmark 0x1/0xffffffff
        post-up /sbin/iptables -t mangle -A OUTPUT -m connmark --mark 0x2 -j MARK --set-xmark 0x2/0xffffffff
        # Routingtabelle anhand des Routing Mark auswählen
        post-up /sbin/ip rule add fwmark 1 table 1
        post-up /sbin/ip rule add fwmark 2 table 2
        # Defaultrouten im regelbasierten Routing pro Fritze setzen
        post-up /sbin/ip route add default via 192.168.1.1 table 1
        post-up /sbin/ip route add default via 192.168.1.2 table 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment