The trick is to combine iptables --mac-source
with CONNMARK
:
- First use
--mac-source
to match packets coming from the mac address you're interested in. It's the wrong direction since you're interested in packets going to this mac address, but now you can - use
CONNMARK
to mark the whole connection, ie both directions (!) and - set the mark from the connection mark with
--restore-mark
# lan interface if_lan=eth0 # create 'mark_mac' table for marking connections: iptables -t mangle -N mark_mac iptables -t mangle -A mark_mac -j MARK --set-mark 1234 iptables -t mangle -A mark_mac -j CONNMARK --save-mark # mark connections involving mac address: iptables -t mangle -A PREROUTING -i $if_lan -m state --state NEW -m mac --mac-source 9c:4e:36:aa:bb:cc -j mark_mac # mark packets going to mac: iptables -t mangle -A POSTROUTING -o $if_lan -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark
Initially i thought this would only work for tcp connections originating from the lan, but given the definition of --state NEW
it should work in both directions for both tcp and udp (!)
See also Policy Routing on Linux based on Sender MAC Address which was the inspiration for this answer.