Course contentsShow
← Linux
Lesson 1122 of 1,81910. Network Administration and Kernel StacksFree lesson

Source-Specific Routing

Implement complex routing logic to ensure return traffic exits the correct interface.

In this lesson, you will learn how to ensure return traffic exits the correct network interface by binding specific routes to source IP addresses.

The Routing Dilemma

By default, the Linux kernel makes routing decisions solely based on the destination IP address. In multi-homed servers—those connected to two different ISPs or subnets—this can cause "asymmetric routing." If a packet arrives on eth1 but the kernel's main routing table points to eth0 as the default gateway, the return traffic might exit the wrong interface. This causes firewalls to drop your packets and breaks stateful connections.

Implementing Source-Specific Routing

To solve this, we move beyond the main routing table and implement Policy-Based Routing (PBR). The process involves three steps: defining a custom routing table, creating a rule to identify traffic by source IP, and adding a default route to that custom table.

Consider a server with two interfaces: eth0 (192.168.1.10) and eth1 (10.0.0.10). We want traffic originating from 10.0.0.10 to always exit via the 10.0.0.1 gateway.

  1. Create a custom table: Edit /etc/iproute2/rt_tables and add a line: 200 isp2.

  2. Add a routing rule: Tell the kernel to use table isp2 when the source address is 10.0.0.10:

    ip rule add from 10.0.0.10 table isp2
    
  3. Configure the table's default route: Add a specific default gateway for that table so the kernel knows where to send the traffic:

    ip route add default via 10.0.0.1 dev eth1 table isp2
    

Why this works

When a packet is generated by an application bound to 10.0.0.10, the kernel checks the ip rule database before the standard routing table. It sees our custom rule, switches the lookup to table isp2, and finds the specific gateway assigned to that interface. Because the lookup happens before the general routing logic, the kernel ignores the system-wide default gateway and ensures the packet leaves through the interface associated with its source address.

Key Takeaway: Source-specific routing uses independent routing tables and policy rules to guarantee that traffic responses exit through the exact interface that received the initial request, maintaining symmetry in multi-homed environments.