rtnetlink(7)

๐ŸŒธ NAME ๐ŸŒธ

rtnetlink โ€” the Linux routing socket, aka rtnetlink(7), aka the girl who runs the whole network stack's group chat ๐Ÿ’…


โœจ SYNOPSIS โœจ

#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/socket.h>

rtnetlink_socket = socket(AF_NETLINK, int socket_type, NETLINK_ROUTE);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

okay so BESTIE. rtnetlink is the thing that lets you read the kernel's routing tables and also alter them. main character behavior honestly. It gets used inside the kernel itself so all the different subsystems can gossip with each other (that internal usage is not documented here, so don't @ me), and it's also how user-space programs get to talk to the kernel.

What can you control through NETLINK_ROUTE sockets? Literally all of this:

  • network routes
  • IP addresses
  • link parameters
  • neighbor setups
  • queueing disciplines
  • traffic classes
  • packet classifiers

It's built on top of netlink messages, so if you want the full lore go read netlink(7).

๐ŸŒˆ Routing attributes

Some rtnetlink messages carry optional attributes chilling right after the initial header:

struct rtattr {
    unsigned short rta_len;    /* Length of option */
    unsigned short rta_type;   /* Type of option */
    /* Data follows */
};
โš ๏ธ Bestie, listen: you should manipulate these attributes only with the RTA_* macros or with libnetlink. See rtnetlink(3). No freestyling, no touching the bytes yourself. ๐Ÿ™ˆ

๐Ÿ’ซ Messages

Rtnetlink is made up of these message types (on top of the standard netlink messages, obviously):

  • RTM_NEWLINK
  • RTM_DELLINK
  • RTM_GETLINK โ€” create, remove, or get info about a specific network interface. These messages contain an ifinfomsg structure, followed by a series of rtattr structures.
struct ifinfomsg {
    unsigned char  ifi_family; /* AF_UNSPEC */
    unsigned short ifi_type;   /* Device type */
    int            ifi_index;  /* Interface index */
    unsigned int   ifi_flags;  /* Device flags  */
    unsigned int   ifi_change; /* change mask */
};

ifi_flags holds the device flags โ€” go look at netdevice(7) for those. ifi_index is the unique interface index (and since Linux 3.7 you can feed a nonzero value along with the RTM_NEWLINK message to create a link with that given ifindex, which is lowkey iconic). ifi_change is a mask that says which bits of ifi_flags should actually be applied to the device. ๐ŸŒŸ

Routing attributes:

  • IFLA_UNSPEC โ€” value type - โ€” unspecified
  • IFLA_ADDRESS โ€” value type hardware address โ€” interface L2 address
  • IFLA_BROADCAST โ€” value type hardware address โ€” L2 broadcast address
  • IFLA_IFNAME โ€” value type asciiz string โ€” Device name
  • IFLA_MTU โ€” value type unsigned int โ€” MTU of the device
  • IFLA_LINK โ€” value type int โ€” Link type
  • IFLA_QDISC โ€” value type asciiz string โ€” Queueing discipline
  • IFLA_STATS โ€” value type see below โ€” Interface Statistics
  • IFLA_PERM_ADDRESS โ€” value type hardware address โ€” hardware address provided by device (since Linux 5.5)

The value type for IFLA_STATS is struct rtnl_link_stats (which was struct net_device_stats back in Linux 2.4 and earlier, if you're feeling vintage ๐ŸŽ€).


  • RTM_NEWADDR
  • RTM_DELADDR
  • RTM_GETADDR โ€” add, remove, or receive info about an IP address tied to an interface.

History lesson real quick: in Linux 2.2 an interface can carry multiple IP addresses, which replaces the alias device concept from Linux 2.0. And in Linux 2.2 these messages support both IPv4 and IPv6 addresses. They contain an ifaddrmsg structure, optionally followed by rtattr routing attributes.

struct ifaddrmsg {
    unsigned char ifa_family;    /* Address type */
    unsigned char ifa_prefixlen; /* Prefixlength of address */
    unsigned char ifa_flags;     /* Address flags */
    unsigned char ifa_scope;     /* Address scope */
    unsigned int  ifa_index;     /* Interface index */
};

So: ifa_family is the address family type (right now that's AF_INET or AF_INET6). ifa_prefixlen is the length of the address mask of the address, if one is defined for that family (like for IPv4). ifa_scope is the address scope. ifa_index is the interface index of the interface the address is associated with. And ifa_flags is a flag word containing IFA_F_SECONDARY for a secondary address (that's the old alias interface) and IFA_F_PERMANENT for a permanent address set by the user โ€” plus other undocumented flags, which, sure. ๐Ÿ’€

Attributes:

  • IFA_UNSPEC โ€” value type - โ€” unspecified
  • IFA_ADDRESS โ€” value type raw protocol address โ€” interface address
  • IFA_LOCAL โ€” value type raw protocol address โ€” local address
  • IFA_LABEL โ€” value type asciiz string โ€” name of the interface
  • IFA_BROADCAST โ€” value type raw protocol address โ€” broadcast address
  • IFA_ANYCAST โ€” value type raw protocol address โ€” anycast address
  • IFA_CACHEINFO โ€” value type struct ifa_cacheinfo โ€” Address information
struct ifa_cacheinfo {
    __u32 ifa_prefered; // Preferred lifetime remaining, in seconds
    __u32 ifa_valid;    // Valid lifetime remaining, in seconds
    __u32 cstamp;       // Creation timestamp, in centiseconds
    __u32 tstamp;       // Update timestamp, in centiseconds
};
๐Ÿšจ Rules of the cacheinfo house: ifa_valid cannot be zero, and ifa_prefered cannot be greater than ifa_valid. A value of UINT32_MAX means an infinite lifetime. Periodt.

  • RTM_NEWROUTE
  • RTM_DELROUTE
  • RTM_GETROUTE โ€” create, remove, or receive info about a network route. These messages contain an rtmsg structure with an optional sequence of rtattr structures following it.

For RTM_GETROUTE: setting rtm_dst_len and rtm_src_len to 0 means you get all entries for the specified routing table. For the other fields โ€” except rtm_table and rtm_protocol โ€” 0 is the wildcard. โœจ

struct rtmsg {
    unsigned char rtm_family;   /* Address family of route */
    unsigned char rtm_dst_len;  /* Length of destination */
    unsigned char rtm_src_len;  /* Length of source */
    unsigned char rtm_tos;      /* TOS filter */
    unsigned char rtm_table;    /* Routing table ID;
                                   see RTA_TABLE below */
    unsigned char rtm_protocol; /* Routing protocol; see below */
    unsigned char rtm_scope;    /* See below */
    unsigned char rtm_type;     /* See below */

    unsigned int  rtm_flags;
};

rtm_type โ€” Route type:

  • RTN_UNSPEC โ€” unknown route
  • RTN_UNICAST โ€” a gateway or direct route
  • RTN_LOCAL โ€” a local interface route
  • RTN_BROADCAST โ€” a local broadcast route (sent as a broadcast)
  • RTN_ANYCAST โ€” a local broadcast route (sent as a unicast)
  • RTN_MULTICAST โ€” a multicast route
  • RTN_BLACKHOLE โ€” a packet dropping route
  • RTN_UNREACHABLE โ€” an unreachable destination
  • RTN_PROHIBIT โ€” a packet rejection route
  • RTN_THROW โ€” continue routing lookup in another table
  • RTN_NAT โ€” a network address translation rule
  • RTN_XRESOLVE โ€” refer to an external resolver (not implemented)

rtm_protocol โ€” Route origin:

  • RTPROT_UNSPEC โ€” unknown
  • RTPROT_REDIRECT โ€” by an ICMP redirect (currently unused)
  • RTPROT_KERNEL โ€” by the kernel
  • RTPROT_BOOT โ€” during boot
  • RTPROT_STATIC โ€” by the administrator

Values larger than RTPROT_STATIC are not interpreted by the kernel โ€” they're just there for your user-space info. You can use them to tag where a piece of routing info came from, or to tell multiple routing daemons apart. Check <linux/rtnetlink.h> for the routing daemon identifiers that are already assigned. ๐Ÿซถ

rtm_scope is the distance to the destination:

  • RT_SCOPE_UNIVERSE โ€” global route
  • RT_SCOPE_SITE โ€” interior route in the local autonomous system
  • RT_SCOPE_LINK โ€” route on this link
  • RT_SCOPE_HOST โ€” route on the local host
  • RT_SCOPE_NOWHERE โ€” destination doesn't exist

The values between RT_SCOPE_UNIVERSE and RT_SCOPE_SITE are available to the user.

The rtm_flags have the following meanings:

  • RTM_F_NOTIFY โ€” if the route changes, notify the user via rtnetlink
  • RTM_F_CLONED โ€” route is cloned from another route
  • RTM_F_EQUALIZE โ€” a multipath equalizer (not yet implemented)

rtm_table specifies the routing table:

  • RT_TABLE_UNSPEC โ€” an unspecified routing table
  • RT_TABLE_DEFAULT โ€” the default table
  • RT_TABLE_MAIN โ€” the main table
  • RT_TABLE_LOCAL โ€” the local table

...you can legitimately drop any arbitrary value you want in there, anywhere between RT_TABLE_UNSPEC and RT_TABLE_DEFAULT ๐Ÿ’… the kernel is not gonna police that, bestie.

๐ŸŽ€ Attributes ๐ŸŽ€

okay so here's the whole roster of rta_type values you might see riding along in the message, with what they hold and what they mean:

  • RTA_UNSPEC โ€” - โ€” literally just ignored, it's giving placeholder energy ๐Ÿ™ˆ
  • RTA_DST โ€” protocol address โ€” Route destination address
  • RTA_SRC โ€” protocol address โ€” Route source address
  • RTA_IIF โ€” int โ€” Input interface index
  • RTA_OIF โ€” int โ€” Output interface index
  • RTA_GATEWAY โ€” protocol address โ€” The gateway of the route ๐Ÿ‘‘
  • RTA_PRIORITY โ€” int โ€” Priority of route
  • RTA_PREFSRC โ€” protocol address โ€” Preferred source address
  • RTA_METRICS โ€” Route metrics (see below ๐Ÿ‘‡)
  • RTA_MULTIPATH โ€” Multipath nexthop data (see below ๐Ÿ‘‡)
  • RTA_PROTOINFO โ€” No longer used ๐Ÿ’€
  • RTA_FLOW โ€” int โ€” Route realm
  • RTA_CACHEINFO โ€” struct rta_cacheinfo (peep linux/rtnetlink.h)
  • RTA_SESSION โ€” No longer used ๐Ÿ’€
  • RTA_MP_ALGO โ€” No longer used ๐Ÿ’€
  • RTA_TABLE โ€” int โ€” Routing table ID; if you set this one, rtm_table gets straight up ignored, no cap
  • RTA_MARK โ€” int
  • RTA_MFC_STATS โ€” struct rta_mfc_stats (peep linux/rtnetlink.h)
  • RTA_VIA โ€” struct rtvia โ€” Gateway in a different AF (see below ๐Ÿ‘‡)
  • RTA_NEWDST โ€” protocol address โ€” Change packet destination address
  • RTA_PREF โ€” char โ€” RFC4191 IPv6 router preference (see below ๐Ÿ‘‡)
  • RTA_ENCAP_TYPE โ€” short โ€” Encapsulation type for lwtunnels (see below ๐Ÿ‘‡)
  • RTA_ENCAP โ€” Defined by RTA_ENCAP_TYPE
  • RTA_EXPIRES โ€” int โ€” Expire time for IPv6 routes (in seconds)

so RTA_METRICS is not just one little value, bestie โ€” it's an array of struct rtattr, each one carrying its own attribute, and they go a lil something like this:

  • RTAX_UNSPEC โ€” - โ€” unspecified ๐Ÿคท
  • RTAX_LOCK โ€” __u32 โ€” Bit field indicating which RTAX_* attributes are locked.
  • RTAX_MTU โ€” __u32 โ€” Maximum Transmission Unit for this route.
  • RTAX_WINDOW โ€” __u32 โ€” Maximum size of the receive window for this route.
  • RTAX_RTT โ€” __u32 โ€” Estimated round-trip time for this route.
  • RTAX_RTTVAR โ€” __u32 โ€” Estimated round-trip time variation for this route.
  • RTAX_SSTHRESH โ€” __u32 โ€” Slow start threshold to use for this route.
  • RTAX_CWND โ€” __u32 โ€” Maximum size of the congestion window for this route.
  • RTAX_ADVMSS โ€” __u32 โ€” Maximum Segment Size to advertise for this route.
  • RTAX_REORDERING โ€” __u32 โ€” Initial reordering level of packets for this route.
  • RTAX_HOPLIMIT โ€” __u32 โ€” Hop limit (TTL) to use for this route.
  • RTAX_INITCWND โ€” __u32 โ€” Initial congestion window to use for this route.
  • RTAX_FEATURES โ€” __u32 โ€” Features to enable for this route specifically.
  • RTAX_RTO_MIN โ€” __u32 โ€” Minimum Retransmission TimeOut to use for this route.
  • RTAX_INITRWND โ€” __u32 โ€” Initial size of the receive window for this route.
  • RTAX_QUICKACK โ€” __u32 โ€” Use quick ack for this route.
  • RTAX_CC_ALGO โ€” asciiz string โ€” Congestion Control algorithm to use for this route.
  • RTAX_FASTOPEN_NO_COOKIE โ€” __u32 โ€” Allow TCP Fast Open without cookie.
โš ๏ธ okay important one: metrics that are locked with RTAX_LOCK take precedence over the values the kernel would normally use (the ones it computed, or that something set via a sysctl or setsockopt(2)). So lowkey some metrics โ€” RTAX_RTO_MIN is the classic example โ€” do absolutely nothing unless their bit is set in RTAX_LOCK. Set the bit or it's not happening. ๐Ÿ’…

and now for RTA_MULTIPATH โœจ it's a bunch of packed instances of struct rtnexthop hanging out together, plus nested RTAs (RTA_GATEWAY) tucked inside:

struct rtnexthop {
    unsigned short rtnh_len;     /* Length of struct + length
                                    of RTAs */
    unsigned char  rtnh_flags;   /* Flags (see
                                    linux/rtnetlink.h) */
    unsigned char  rtnh_hops;    /* Nexthop priority */
    int            rtnh_ifindex; /* Interface index for this
                                    nexthop */
};

there's a whole squad of RTNH_* macros that look just like the RTA_* and NLHDR_* macros, and they're the ones you want for wrangling these structures ๐Ÿซถ

struct rtvia {
    unsigned short rtvia_family;
    unsigned char  rtvia_addr[0];
};

rtvia_addr is the address itself, and rtvia_family is its family type. periodt.

RTA_PREF can hold the values ICMPV6_ROUTER_PREF_LOW, ICMPV6_ROUTER_PREF_MEDIUM, and ICMPV6_ROUTER_PREF_HIGH, all defined in <linux/icmpv6.h>.

RTA_ENCAP_TYPE can hold the values LWTUNNEL_ENCAP_MPLS, LWTUNNEL_ENCAP_IP, LWTUNNEL_ENCAP_ILA, or LWTUNNEL_ENCAP_IP6, defined in <linux/lwtunnel.h>.

Fill these values in! ๐Ÿซก

๐Ÿ’– RTM_NEWNEIGH ๐Ÿ’–

๐Ÿ’– RTM_DELNEIGH ๐Ÿ’–

๐Ÿ’– RTM_GETNEIGH ๐Ÿ’–

These three add, remove, or hand you info about a neighbor table entry (think: an ARP entry, the classic). The message is packing an ndmsg structure:

struct ndmsg {
    unsigned char ndm_family;
    int           ndm_ifindex;  /* Interface index */
    __u16         ndm_state;    /* State */
    __u8          ndm_flags;    /* Flags */
    __u8          ndm_type;
};
struct nda_cacheinfo {
    __u32         ndm_confirmed;
    __u32         ndm_used;
    __u32         ndm_updated;
    __u32         ndm_refcnt;
};

so ndm_state is a bit mask, and here's everything it can be masking, bestie:

  • NUD_INCOMPLETE โ€” a currently resolving cache entry
  • NUD_REACHABLE โ€” a confirmed working cache entry ๐Ÿ‘‘
  • NUD_STALE โ€” an expired cache entry
  • NUD_DELAY โ€” an entry waiting for a timer โณ
  • NUD_PROBE โ€” a cache entry that is currently reprobed
  • NUD_FAILED โ€” an invalid cache entry ๐Ÿ’€
  • NUD_NOARP โ€” a device with no destination cache
  • NUD_PERMANENT โ€” a static entry

and the valid ndm_flags are:

  • NTF_PROXY โ€” a proxy arp entry
  • NTF_ROUTER โ€” an IPv6 router

meanwhile the rtattr struct has these meanings for its rta_type field:

  • NDA_UNSPEC โ€” unknown type ๐Ÿ™ˆ
  • NDA_DST โ€” a neighbor cache n/w layer destination address
  • NDA_LLADDR โ€” a neighbor cache link layer address
  • NDA_CACHEINFO โ€” cache statistics

and FYI: if that rta_type field is NDA_CACHEINFO, then a struct nda_cacheinfo header follows it. ๐Ÿซถ


๐ŸŒˆ RTM_NEWRULE ๐ŸŒˆ

๐ŸŒˆ RTM_DELRULE ๐ŸŒˆ

๐ŸŒˆ RTM_GETRULE ๐ŸŒˆ

Add, delete, or retrieve a routing rule. Carries a struct rtmsg

๐Ÿ’… the qdisc ops, bestie

RTM_NEWQDISC, RTM_DELQDISC, RTM_GETQDISC โ€” these three are literally the add / remove / get trio for a queueing discipline. So if you wanna slide a new qdisc in, yeet one out, or just peek at what's there? Yeah. These.

Every one of these messages carries a struct tcmsg, and it may have a whole parade of attributes tagging along after it. Observe, bestie:

struct tcmsg {
    unsigned char    tcm_family;
    int              tcm_ifindex;   /* interface index */
    __u32            tcm_handle;    /* Qdisc handle */
    __u32            tcm_parent;    /* Parent qdisc */
    __u32            tcm_info;
};

โœจ now the attributes, slay:

  • TCA_UNSPEC โ€” value type: - โ€” it's justโ€ฆ unspecified. vibes only, no meaning ๐Ÿ’€
  • TCA_KIND โ€” an asciiz string โ€” the name of the queueing discipline ๐ŸŽ€
  • TCA_OPTIONS โ€” a byte sequence โ€” the qdisc-specific options come right after this one
  • TCA_STATS โ€” a struct tc_stats โ€” the qdisc's statistics, the receipts, the math ๐Ÿงฎ
  • TCA_XSTATS โ€” qdisc-specific โ€” module-specific statistics, iykyk
  • TCA_RATE โ€” a struct tc_estimator โ€” rate limit, keep it cute, keep it capped ๐ŸŒธ

and ALSO, on top of all that: a whole bunch of other qdisc-module-specific attributes are allowed too. Wanna know what those are? Go read the appropriate include files, bestie, I can't read them for you ๐Ÿซถ


๐Ÿšฆ the class ops

RTM_NEWTCLASS, RTM_DELTCLASS, RTM_GETTCLASS โ€” add, remove, or get a traffic class. Same energy, class edition. These messages carry a struct tcmsg exactly like the one I described up above, no cap. ๐Ÿ‘‘


๐Ÿ” the filter ops

RTM_NEWTFILTER, RTM_DELTFILTER, RTM_GETTFILTER โ€” add, remove, or receive info about a traffic filter. And you already know what's inside: a struct tcmsg, as described above. consistent queen โœจ


๐ŸŒˆ VERSIONS

rtnetlink is a new feature of Linux 2.2. baby of the family fr fr ๐Ÿ‘ถ


๐Ÿ’€ BUGS

This manual page is incomplete. Not me being honest โ€” the page itself says so. The audacity. ๐Ÿ˜ญ


๐Ÿฆ‹ SEE ALSO


okay that's a wrap on rtnetlink(7), hope u learned something bestie ๐Ÿ’– This lil page came from Linux man-pages 6.20, dated 2026-02-08, and yeah โ€” that's the whole rtnetlink(7) saga, all three parts, finished. go forth and route some packets, main character ๐ŸŒŸ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-1-para
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms