Search code examples
defaultroutesgatewayicmp

Redirecting icmp traffic through one gateway and tcp through another


I have two devices connected to a network, but I am trying to create my own little network. I want to route my icmp traffic of client, say 10.10.10.99 through 10.10.10.50 and all the other traffic of 10.10.10.99 should remain unaffected and pass via default gateway.

I was able to route the whole traffic by changing the default gateway, but I no longer want that.


Solution

  • This should be possible in Linux through the usage of ip route add for adding a new table with the new gateway and iptables to mark certain packets destined for a specific port. The new table can then act on the marked packets.

    There is a good example for how to achieve this at this link on tldp

    For example you can do the following for marking/routing ICMP traffic :

    iptables -A PREROUTING -i eth0 -t mangle -p icmp -j MARK --set-mark 1
    # create a table icmp.out to handle all icmp traffic
    echo 201 icmp.out >> /etc/iproute2/rt_tables
    ip rule add fwmark 1 table icmp.out
    ip route add default via <gateway> dev <dev> table icmp.out