Policy Based Routing
7,689 Total views across this article’s versions; includes the historical baseline where available.
A routing table describes paths to network destinations. Linux can use several tables, with a Routing Policy Database (RPDB) deciding which table to consult. The policies are expressed as rules; the tables hold the routes.
The RPDB provides a way to choose among several routing tables based on traffic attributes. Rules and routes are separate. Several rules can consult the same table, or different rules can consult different tables. This lets you make routing decisions beyond a single destination-based table.
Routes and policy rules can be changed while Linux is running. That is also true of ordinary destination-based routing. Policy routing provides more control, but a mistaken rule or route can still interrupt service. Keep a working recovery path when changing a remote system.
The active rules and routes live in the kernel. Commands update that live state; the network configuration service may also install or replace it. A reboot does not preserve ad hoc command-line changes unless the configuration reapplies them.
Linux separates policy selection from route selection. The routing policy database (RPDB) holds rules that select an action, often a lookup in a routing table. A routing table holds routes. The ip-rule manual and ip-route manual describe these separately.
- Check rules in order of priority, with lower numbers first. If a selector does not match, continue to the next rule.
- If a matching rule selects a table, look for the best usable route to the destination in that table. Among eligible routes, the longest matching destination prefix is the most specific; for example, a matching
/24is more specific than/0. Metrics or multipath settings matter when applicable, not the order routes appear in a listing. - If a lookup finds no usable route, or hits a
throwroute, policy lookup can continue to another rule. A terminal result such as an explicit rejection is different from an empty lookup.
A valid result from an earlier table is not compared with routes in later tables. That is why an earlier table’s default route can take precedence over a more specific route in another table.
Let’s move on to a discussion of how the main routing process functions and the philosophy behind it. Afterwards, I’ll go over the packet mangling process (netfilter), and wrap it all together for you with a comprehensive explanation of how these pieces fit together to complete the puzzle.
The RPDB Triad: Addresses, Rules, and Routes
Addresses, rules, and routes work together. ip rule manages routing policy, ip route manages routes, and iptables manages Netfilter firewall rules. These are administration commands, not three kernel modules called in sequence for every packet.
Before changing routing policy, distinguish route selection from firewall processing. The Linux Firewall Management Tools Overview introduces the packet paths; the commands configure the kernel mechanisms that handle those paths.
Here’s a brief clarification of each portion of the Linux RPDB system.
Addresses
This is pretty straightforward. An address describes the location of a service on a network. Packets must have both source (origin) and destination addresses. A packet’s addresses are the most important factor in how it is routed.
Rules
A policy rule can select a routing table or another action. It need not point directly to a route.
Manage routing policy with the ip rule command. Each rule contains a selector and an action. A selector matches lookup attributes; it is not a packet filter or firewall verdict.
Selectors may examine one or more of the following parts of the current network packet:
- Source address
- Destination address
- ToS (Type of Service)[1]
- Incoming interface (that which the packet arrived from)
- fwmark (firewall mark)
Current iproute2 also documents protocol and port selectors (ipproto, sport, dport). Availability depends on the installed kernel and tools. A routing selector is not a substitute for a firewall policy.
Common actions look up a table, continue at another rule, or return a terminal result such as blackhole, unreachable, or prohibit. Use explicit, distinct priorities for custom rules. The usual built-in priorities are 0, 32766 and 32767, not a limit on custom rule numbers.
IP Rules are explained in more detail in Follow the Rules.
Type of Service (ToS)
The IPv4 byte originally called Type of Service was redefined as the Differentiated Services field in RFC 2474. Its upper six bits carry DSCP; RFC 3168 assigns the lower two bits to ECN. A six-bit DSCP value is therefore not interchangeable with the complete byte.
The legacy tos/dsfield options and newer DSCP selectors depend on the installed iproute2 and kernel versions. Check their documented value format before writing a rule; do not assume a DSCP number can simply be substituted for a ToS value. Traffic classification and shaping are outside this article’s scope.
Routes and Routing Tables
A route describes how to handle destinations covered by a prefix. A unicast route may select an interface and a next-hop router; a local route delivers to this host. A hop is one step along a packet’s path, not another name for a route.
A routing table is a collection of routes. When several eligible routes in the selected table cover the packet’s destination, the most specific matching destination prefix wins. This comparison applies to routes, not to the text or source prefix of an RPDB rule.
Endnotes
[1] The DS field was defined in 1998, and ECN was specified in 2001. See the RFCs above and Breaking Down IPv4 Address Headers.