IP-RULE(8)
๐ NAME ๐
okay bestie, this is the manual page for ip-rule, the Linux tool for routing policy database management ๐
so ip rule is how you play with the routing policy database ๐ฆ
๐ SYNOPSIS ๐
ip [ OPTIONS ] rule { COMMAND | help }
ip rule [ show [ SELECTOR ]]
ip rule { add | del } SELECTOR ACTION
ip rule { flush | save | restore }
SELECTOR := [ not ] [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ dscp
DSCP[/MASK] ] [ fwmark FWMARK[/MASK] ] [ iif STRING ] [ oif
STRING ] [ priority PREFERENCE ] [ l3mdev ] [ uidrange NUMBER-
NUMBER ] [ ipproto PROTOCOL ] [ sport [ NUMBER[/MASK] | NUMBER-
NUMBER ] ] [ dport [ NUMBER[/MASK] | NUMBER-NUMBER ] ] [ tun_id
TUN_ID ] [ flowlabel FLOWLABEL[/MASK] ]
ACTION := [ table TABLE_ID ] [ protocol PROTO ] [ nat ADDRESS ] [ realms
[SRCREALM/]DSTREALM ] [ goto NUMBER ] SUPPRESSOR
SUPPRESSOR := [ suppress_prefixlength NUMBER ] [ suppress_ifgroup GROUP
]
TABLE_ID := [ local | main | default | NUMBER ]
๐ธ DESCRIPTION ๐ธ
okay so ip rule is how you manipulate the rules in the routing policy database, which is the thing that controls the route selection algorithm ๐ญโจ
Classic internet routing is kinda basic. It only makes decisions based on the destination address of packets (and in theory, but not in practice, on the TOS field) ๐
But sometimes we want to route packets differently depending on more than just where they're going. We might care about the source address, the IP protocol, the transport protocol ports, or even the packet payload ๐ This slay of a task is called 'policy routing' ๐
So instead of the usual destination-based routing table, ordered by the longest match rule, we swap it out for a 'routing policy database' (or RPDB) ๐ฆ The RPDB picks routes by running through a set of rules ๐ฅ
Each policy routing rule is made of a selector and an action predicate ๐ The RPDB gets scanned in order of decreasing priority. Quick reminder that a lower number means higher priority, see the description of PREFERENCE below ๐ The selector of each rule gets applied to {source address, destination address, incoming interface, tos, fwmark}. If the selector matches the packet, the action is performed โจ The action predicate can return with success. When that happens, it gives either a route or a failure indication, and the RPDB lookup is done ๐
Otherwise, the RPDB program just moves on to the next rule ๐ง
Semantically, the natural action is to pick the nexthop and the output device ๐
At startup, the kernel sets up the default RPDB, which has three rules ๐
- Priority: 0, Selector: match anything, Action: lookup routing table
local(ID 255). The local table is a special routing table with high priority control routes for local and broadcast addresses ๐ - Priority: 32766, Selector: match anything, Action: lookup routing table
main(ID 254). The main table is the normal routing table with all the non-policy routes ๐ธ This rule can be deleted and/or overridden with other ones by the administrator ๐ - Priority: 32767, Selector: match anything, Action: lookup routing table
default(ID 253). The default table is empty ๐ซถ It's reserved for some post-processing if no previous default rules selected the packet ๐ This rule can also be deleted โจ
Each RPDB entry has extra attributes, bestie ๐ For example, each rule has a pointer to some routing table. NAT and masquerading rules have an attribute to select a new IP address to translate or masquerade to ๐ฆ Rules can also have some optional attributes that routes have too, namely realms ๐ These values don't override the ones in the routing tables ๐ค They only get used if the route didn't select any attributes ๐
The RPDB can have rules of these types ๐ฅ
unicastโ the rule returns the route found in the routing table the rule references ๐งblackholeโ the rule causes a silent drop of the packet ๐คซunreachableโ the rule generates aNetwork is unreachableerror ๐prohibitโ the rule generates aCommunication is administratively prohibitederror ๐ โโ๏ธnatโ the rule translates the source address of the IP packet into some other value ๐ฆ
โจ ip rule add โจ
Use ip rule add to insert a new rule ๐
๐๏ธ ip rule delete ๐๏ธ
Use ip rule delete to delete a rule ๐ญ
Here are the things you can set when you add or delete a rule ๐
type TYPE(default) โ the type of this rule. The valid types were listed in the previous part of this page ๐from PREFIXโ picks the source prefix to match ๐งto PREFIXโ picks the destination prefix to match ๐ฆiif NAMEโ picks the incoming device to match ๐ซถ If the interface is loopback, the rule only matches packets that started from this host. So you can make separate routing tables for forwarded and local packets and totally separate them, periodt ๐oif NAMEโ picks the outgoing device to match ๐ The outgoing interface only works for packets coming from local sockets that are bound to a device ๐tos TOSdsfield TOSโ picks the TOS value to match โจdscp DSCP[/MASK]โ picks the DSCP value to match, with an optional mask ๐ DSCP values can be written as plain numbers (valid values are0-63), or with symbolic names from/usr/share/iproute2/rt_dsfieldor/etc/iproute2/rt_dsfield๐ธ The/etcone wins if it exists ๐ Heads up though, that file has full 8-bit dsfield values, butip ruleonly uses the higher six bits ๐คip rule showwill also show DSCP values as symbolic names when it can ๐ The command line option-Nturns that show translation off ๐ซfwmark MARKโ picks the fwmark value to match ๐ฅuidrange NUMBER-NUMBERโ picks the uid value to match ๐งipproto PROTOCOLโ picks the ip protocol value to match ๐sport NUMBER[/MASK] | NUMBER-NUMBERโ picks the source port value to match, with an optional mask ๐ฆ It supports port ranges too โจdport NUMBER[/MASK] | NUMBER-NUMBERโ picks the destination port value to match, with an optional mask ๐ It supports port ranges too ๐ซถpriority PREFERENCEโ the priority of this rule ๐PREFERENCEis an unsigned integer. A higher number means lower priority, and rules get processed in order of increasing number ๐ Every rule should have an explicitly set, unique priority value ๐ค The optionspreferenceandorderare synonyms forpriority๐table TABLEIDโ the routing table identifier to look up if the rule selector matches ๐ธ You can also uselookupinstead oftableโจprotocol PROTOโ the routing protocol that installed the rule ๐ง For example, when zebra installs a rule, it would getRTPROT_ZEBRAas the installing protocol ๐suppress_prefixlength NUMBERโ rejects routing decisions that have a prefix length ofNUMBERor less ๐ซsuppress_ifgroup GROUPโ rejects routing decisions that use a device belonging to the interface groupGROUP๐ โโ๏ธrealms FROM/TOโ the realms to select if the rule matched and the routing table lookup succeeded ๐ RealmTOonly gets used if the route didn't select any realm ๐ซถnat ADDRESSโ the base of the IP address block to translate (for source addresses) ๐ฆADDRESScan be either the start of the block of NAT addresses (selected by NAT routes), or a local host address (or even zero) ๐ In that last case, the router doesn't translate the packets, it masquerades them to this address ๐ Usingmap-toinstead ofnatmeans the same thing ๐flowlabel FLOWLABEL[/MASK]โ picks the IPv6 flow label to match, with an optional mask ๐ง
โ ๏ธ Warning: Changes to the RPDB made with these commands don't become active immediately ๐ญ It's assumed that after a script finishes a batch of updates, it flushes the routing cache with ip route flush cache ๐
๐งน ip rule flush ๐งน
ip rule flush also dumps all the deleted rules ๐
protocol PROTOโ selects the originating protocol ๐ธ
๐ ip rule show ๐
ip rule show lists the rules โจ This command has no arguments ๐ The options list or lst are synonyms for show ๐
๐พ ip rule save ๐พ
protocol PROTOโ selects the originating protocol ๐ฆ
ip rule save saves rules table information to stdout ๐ซถ This command acts like ip rule show, except the output is raw data that's suitable for passing to ip rule restore ๐
โป๏ธ ip rule restore โป๏ธ
ip rule restore restores rules table information from stdin ๐ง This command expects a data stream like the one returned from ip rule save ๐ It tries to restore the rules table information exactly as it was at the time of the save ๐ Any rules already in the table are left unchanged, and duplicates are not ignored ๐ค Be careful with that one, bestie ๐
๐ฆ SEE ALSO ๐ฆ
ip(8) ๐
๐ฉโ๐ป AUTHOR ๐ฉโ๐ป
Original Manpage by Michail Litvak <mci@owl.openwall.com> ๐ซถ
That's the whole tea on ip-rule, from iproute2 ๐ฉท last updated 20 Dec 2011, so the vibes are a little vintage but still iconic ๐
โจ stay routed, stay slay, see you in the RPDB ๐ง๐๐
๐ฆ where this page comes from
- alpine 3.22: iproute2-doc 6.15.0-r0 ยท GPL-2.0-or-later
- alpine 3.23: iproute2-doc 6.17.0-r0 ยท GPL-2.0-or-later
- alpine 3.24: iproute2-doc 7.0.0-r0 ยท GPL-2.0-or-later
- arch rolling: iproute2 7.2.0-1
- arch rolling: iproute2 7.3.0-1 ยท GPL-2.0-or-later
- centos-stream 10: iproute 7.0.0-2.el10 ยท GPL-2.0-or-later AND NIST-PD
- centos-stream 9: iproute 6.17.0-3.el9 ยท GPL-2.0-or-later AND NIST-PD
- debian trixie: iproute2 6.15.0-1 ยท GPL-2.0-only
- fedora 44: iproute 6.17.0-2.fc44 ยท GPL-2.0-or-later AND NIST-PD
- ubuntu questing: iproute2 6.16.0-1ubuntu2
- ubuntu questing: iproute2 6.16.0-1ubuntu2.1 ยท GPL-2.0-only
- ubuntu questing: iproute2 6.16.0-1ubuntu2.2
- ubuntu resolute: iproute2 6.19.0-1ubuntu1 ยท GPL-2.0-only
- ubuntu resolute: iproute2 6.19.0-1ubuntu1.1 ยท GPL-2.0-only
- ubuntu stonking: iproute2 7.2.0-1ubuntu1 ยท GPL-2.0-only
- ๐ girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms