Multiple interfaces: route packets to specific interface

10,846

Routing decisions are based on destination addresses. If you want to route traffic based on the source address you need to configure policy-based routing. Have a look at:

http://www.linuxhorizon.ro/iproute2.html

I answered these similar questions a few weeks ago:

How can I route different traffic over different interfaces (routes) by protocol?

Using iptables to forward traffic destined for specific ip via specific interface

Share:
10,846

Related videos on Youtube

Maarten
Author by

Maarten

Updated on September 18, 2022

Comments

  • Maarten
    Maarten over 1 year

    I have 2 internet connections on my network. My router is running Debian. eth0 is my network (192.168.0.0/16). eth1 and eth2 are connected to 2 different internet providers (with dynamic IP).

    Here is a small drawing of my configuration

    PC1 ---|                  |-eth1- DynIP provider 1
    PC2 ---|-eth0- Router --- |
    PCn ---|                  |-eth2- DynIP provider 2
    

    I'm trying to configure it as follow: I want all packets by default to go via eth1. All packets coming from a PC having address 192.168.2.0/24 to go via eth2.

    After looking for a while and posting on other forums I already have the following (in my rc.local file):

    echo "1" > /proc/sys/net/ipv4/ip_forward
    
    iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
    iptables -t nat -A POSTROUTING -o eth2 -j MASQUERADE
    iptables -A FORWARD -i eth1 -j ACCEPT
    iptables -A FORWARD -i eth2 -j ACCEPT
    

    with route table:

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    10.0.4.0        0.0.0.0         255.255.255.0   U     0      0        0 eth2
    172.16.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth1
    192.168.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
    0.0.0.0         172.16.1.50     0.0.0.0         UG    0      0        0 eth1
    0.0.0.0         10.0.4.2        0.0.0.0         UG    0      0        0 eth2
    0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
    

    I tried to play with route and ip route but I cannot find it to work...