How can I route network traffic such that the packets go out via the same interface they came in?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux
  • System connected to Internet via multiple network interfaces on different networks/ISPs
  • System connected to the same subnet via multiple network interfaces

Issue

  • How can I forward network packets to the same network interface from where it came from?
  • Two interfaces are both connected to internet which is provided by different ISPs. How can I make the network packets go back via the interface where they came from?
  • How to assign two IP for two NIC with two diffrent subnet (two different gateway). How to add two gateway on a single machine.

Resolution

  • Note: This below steps would help you achieve the desired results but this is only shared for informational and reference purposes . Designing and implementing custom policy based routing" is out of scope

This feature can be implemented by using policy-based routing (source routing). The iproute package provides the tools (/sbin/ip) to configure this.

  1. Example : server has 2 interfaces with IP address.
    eth0 - > inet addr:10.66.1.51  Bcast:10.66.255.255
    eth1 - > inet addr:10.67.1.51  Bcast:10.67.255.255
    
  2. Setup 2 more routing tables with different table IDs; 10.66.255.254 is the gateway for eth0 and 10.67.255.254 is the gateway for eth1. We also add routes for the directly connected networks:
    
    ip route add 10.66.0.0/16 dev eth0 table 1
    ip route add default via 10.66.255.254 dev eth0 table 1
    ip route add 10.67.0.0/16 dev eth1 table 2
    ip route add default via 10.67.255.254 dev eth1 table 2
    
    Above command creates 2 routing tables.
  3. Create rules to forward all the packets entering via a particular NIC to go out the appropriate routing table.
    ip rule add iif eth0 table 1
    ip rule add iif eth1 table 2
    
  4. Create rules to forward packets to go out the specific routing table.
    ip rule add from 10.66.1.51 table 1
    ip rule add from 10.67.1.51 table 2
    
  5. If you want to add more routing records, following is the command format
    ip route add to 192.168.100.0 via 10.66.0.203 dev eth0 table 1
    

For instructions on how to make this persistent across reboots, please refer to the following article. How to make routing rules persistent, when I want packets to leave the same interface they came in?

If the interface is managed by NetworkManager you also need to enable and start the NetworkManager-dispatcher service. Please refer to Is it possible to set up Policy Based Routing with NetworkManager in RHEL 7?

The same applies to servers with multiple NICs at same subnet. You just need to adjust the addresses.

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.