Special Routing Use-Case Scenarios (iproute2)

Tutorials · Published 2019-07-20 · Revised 2026-11-24
Reviewed corrections applied locally; pre-review version preserved separately · Joomla source

7,094 Total views across this article’s versions; includes the historical baseline where available.

Linux routes can select a path, return a terminal error, or supply a preferred source address for locally generated traffic. These functions are different from firewall filtering and Network Address Translation (NAT).

Dropping Packets

A route can reject or discard traffic without specifying an output device. This affects packets whose policy lookup selects that route; an earlier matching policy rule or a more specific route can change the result.

The examples below are syntax templates. Replace DESTINATION/PREFIX_LENGTH with the intended IPv4 prefix and specify a table if main is not appropriate. Inspect current routing and keep recovery access available before applying a discard or rejection route. Use ip -4 route del with the same type, prefix, and table to undo a test.

Silently Drop Packets.

ip -4 route add blackhole DESTINATION/PREFIX_LENGTH

A selected blackhole route discards the packet without sending an ICMP rejection. A local sender receives an error.

Reject Packets with ICMP / “Host unreachable” Response.

ip -4 route add unreachable DESTINATION/PREFIX_LENGTH

A selected unreachable route discards the packet and reports host unreachable, subject to normal ICMP error rules. Local senders receive EHOSTUNREACH.

Reject Packets with ICMP / “Administratively prohibited” Response.

ip -4 route add prohibit DESTINATION/PREFIX_LENGTH

A selected prohibit route discards the packet and reports administratively prohibited, subject to normal ICMP error rules. Local senders receive EACCES.

Continue Policy Lookup with throw

ip -4 route add throw DESTINATION/PREFIX_LENGTH table TABLE_ID

A throw route is a control result: it ends the current table lookup as though no route were found. Later policy rules may still supply a route. Only if no later lookup succeeds does the overall routing attempt fail; throw is not an unconditional drop or a VPN fallback guard.

Selecting a Preferred Source Address

The src attribute names a preferred local source address for a route. It is used when selecting a source for locally generated traffic whose application has not already chosen one. It does not overwrite a source explicitly bound by an application or rewrite transit packets.

Here is a template for a direct route:

ip -4 route add DESTINATION/PREFIX_LENGTH dev INTERFACE table TABLE_ID src LOCAL_ADDRESS

For an isolated example, suppose eth0 already has local address 10.10.10.11/24, the destination network is directly connected, and table 200 is unused:

ip -4 route add 10.10.10.0/24 dev eth0 table 200 src 10.10.10.11
ip -4 route show table 200

This adds a route to table 200; it does not add a policy rule selecting the table. The preferred source must be a suitable address on the host. To undo this route:

ip -4 route del 10.10.10.0/24 dev eth0 table 200 src 10.10.10.11

See iproute and Routing Tables for a complete bounded table-and-rule example.

Source NAT (or Not)

Source selection chooses an address for a new local packet. Source NAT rewrites a packet’s source address, with stateful NAT also keeping a translation binding for the connection. They are different operations.

The route’s src attribute is not a NAT backdoor. Similarly, ip rule selects routing actions; it does not automatically understand connection state. Netfilter connection tracking supplies that state to firewall rules.

For ordinary stateful NAT, the first packet selects the translation binding. Subsequent packets still undergo the applicable translation, but use that binding rather than independently evaluating the NAT rules again. This applies to both SNAT and MASQUERADE; it is incorrect to say only the first packet is translated. See Netfilter’s NAT explanation.

The packet-path overview places initial route/source selection before OUTPUT and separates it from later NAT processing.

When a Preferred Source Makes Sense

A preferred source can be useful when a host has multiple local addresses, whether on one interface or several. It expresses which address should normally be used for destinations reached by that route when the application leaves the choice to the kernel.

That can help the receiving host use an appropriate return path, but it does not guarantee delivery. Remote routes, firewall policy, link state, and application binding still matter. Inspect the selected route with ip route get, then test a real connection.

A Brief History of iproute NAT

The ip route manual still describes the historical nat route type, but explicitly says route NAT is no longer supported in Linux 2.6. That historical facility is distinct from the current src attribute.

Use the current route manual for supported behavior. Preferred source selection remains available; it does not reintroduce the obsolete route NAT facility.

Stateful inspection associates packets with tracked flows and their state. Stateless inspection evaluates packets without that connection-tracking context. Those terms describe how rules use state; they do not turn source-address selection into NAT.

When You Must Have Stateless NAT

Stateless NAT is available through mechanisms other than the old route NAT type, including the tc nat action. Choosing it requires a design for translation in both directions and an understanding of its protocol limitations. The existence of that action does not mean old ip route ... nat commands are supported.

tc

tc, part of iproute2, configures Linux traffic control. Its nat action performs stateless IPv4 address translation: the action’s ingress direction translates destinations, while egress translates sources. Those are parameters of the action and must be placed in an appropriate traffic-control configuration.

Traffic-control filters, actions, attachment points, and reverse-path handling require more than the action alone. This article does not provide a complete tc NAT configuration. Start with the tc manual and tc-nat manual rather than treating a route’s src value as translation.

Single Network Connection, Multiple LANs

Suppose eth0 has both 192.168.10.11/24 and 10.10.10.11/24, and both subnets are available on that link. The first subnet also has a gateway at 192.168.10.10. An illustrative route listing is:

default via 192.168.10.10 dev eth0
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.11
10.10.10.0/24 dev eth0 proto kernel scope link src 10.10.10.11

The connected prefixes often appear automatically when the addresses are configured; their presence does not prove someone manually supplied src. For an unbound local connection to 10.10.10.14, the more specific connected route provides 10.10.10.11 as its preferred source.

Inspect the actual address and route selection:

ip -4 address show dev eth0
ip -4 route get 10.10.10.14

A shorter legacy route listing does not show all these attributes. Even the detailed listing establishes only local routing state, not guaranteed replies from the remote host.

Multiple Network Connections, Multiple LANs

For two separate interfaces, suppose eth0 has 192.168.10.11/24 and eth1 has 10.10.10.11/24. The second connected route would use eth1:

default via 192.168.10.10 dev eth0
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.11
10.10.10.0/24 dev eth1 proto kernel scope link src 10.10.10.11

These preferred sources guide local source selection; they do not force every packet to use those addresses or guarantee a return path. An application can bind a particular local source, and forwarded traffic retains its existing source unless a separate translation rule changes it.

If placing these routes in a custom policy table, include the required connected routes, select the table with an appropriate rule, and verify the exact selectors. See the table example.

Endnotes

1 Netfilter NAT HOWTO: Controlling What To NAT explains connection-based translation. Consult the current extensions manual for supported targets and chains.

2 ip-route(8) documents the preferred source attribute, route types, and obsolete route NAT.