www.LinuxHowtos.org





rtnetlink

Section: Environments, Tables, and Troff Macros (7)
Updated: 202-0-08
Index Return to Main Contents
 

NAME

rtnetlink - Linux routing socket  

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

Rtnetlink allows the kernel's routing tables to be read and altered. It is used within the kernel to communicate between various subsystems, though this usage is not documented here, and for communication with use-space programs. Network routes, IP addresses, link parameters, neighbor setups, queueing disciplines, traffic classes and packet classifiers may all be controlled through NETLINK_ROUTE sockets. It is based on netlink messages; see netlink(7) for more information.  

Routing attributes

Some rtnetlink messages have optional attributes after the initial header: struct rtattr {
    unsigned short rta_len;    /* Length of option */
    unsigned short rta_type;   /* Type of option */
    /* Data follows */ }; These attributes should be manipulated using only the RTA_* macros or libnetlink, see rtnetlink(3).  

Messages

Rtnetlink consists of these message types (in addition to standard netlink messages):
RTM_NEWLINK
RTM_DELLINK RTM_GETLINK Create, remove, or get information 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 contains the device flags, see netdevice(7); ifi_index is the unique interface index (since Linux 3.7, it is possible to feed a nonzero value with the RTM_NEWLINK message, thus creating a link with the given ifindex); ifi_change is a mask specifying which bits of ifi_flags should be applied to the device.
Routing attributes
rta_typeValue typeDescription

IFLA_UNSPEC-unspecified
IFLA_ADDRESShardware addressinterface L2 address
IFLA_BROADCASThardware addressL2 broadcast address
IFLA_IFNAMEasciiz stringDevice name
IFLA_MTUunsigned intMTU of the device
IFLA_LINKintLink type
IFLA_QDISCasciiz stringQueueing discipline
IFLA_STATS see below Interface Statistics
IFLA_PERM_ADDRESShardware address hardware address provided by device (since Linux 5.5)
The value type for IFLA_STATS is struct rtnl_link_stats (struct net_device_stats in Linux 2.4 and earlier).
RTM_NEWADDR
RTM_DELADDR RTM_GETADDR Add, remove, or receive information about an IP address associated with an interface. In Linux 2.2, an interface can carry multiple IP addresses, this replaces the alias device concept in Linux 2.0. In Linux 2.2, these messages support 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 */ };
ifa_family is the address family type (currently AF_INET or AF_INET6), ifa_prefixlen is the length of the address mask of the address if defined for the family (like for IPv4), ifa_scope is the address scope, ifa_index is the interface index of the interface the address is associated with. ifa_flags is a flag word of IFA_F_SECONDARY for secondary address (old alias interface), IFA_F_PERMANENT for a permanent address set by the user and other undocumented flags.
Attributes
rta_typeValue typeDescription

IFA_UNSPEC-unspecified
IFA_ADDRESSraw protocol addressinterface address
IFA_LOCALraw protocol addresslocal address
IFA_LABELasciiz stringname of the interface
IFA_BROADCASTraw protocol addressbroadcast address
IFA_ANYCASTraw protocol addressanycast address
IFA_CACHEINFOstruct ifa_cacheinfoAddress 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 };
ifa_valid cannot be zero, and ifa_prefered cannot be greater than ifa_valid. A value of UINT32_MAX represents an infinite lifetime.
RTM_NEWROUTE
RTM_DELROUTE RTM_GETROUTE Create, remove, or receive information about a network route. These messages contain an rtmsg structure with an optional sequence of rtattr structures following. 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_typeRoute type

RTN_UNSPECunknown route
RTN_UNICASTa gateway or direct route
RTN_LOCALa local interface route
RTN_BROADCAST a local broadcast route (sent as a broadcast)
RTN_ANYCAST a local broadcast route (sent as a unicast)
RTN_MULTICASTa multicast route
RTN_BLACKHOLEa packet dropping route
RTN_UNREACHABLEan unreachable destination
RTN_PROHIBITa packet rejection route
RTN_THROWcontinue routing lookup in another table
RTN_NATa network address translation rule
RTN_XRESOLVE refer to an external resolver (not implemented)
rtm_protocolRoute origin

RTPROT_UNSPECunknown
RTPROT_REDIRECT by an ICMP redirect (currently unused)
RTPROT_KERNELby the kernel
RTPROT_BOOTduring boot
RTPROT_STATICby the administrator
Values larger than RTPROT_STATIC are not interpreted by the kernel, they are just for user information. They may be used to tag the source of a routing information or to distinguish between multiple routing daemons. See <linux/rtnetlink.h> for the routing daemon identifiers which are already assigned.
rtm_scope is the distance to the destination:
RT_SCOPE_UNIVERSEglobal route
RT_SCOPE_SITE interior route in the local autonomous system
RT_SCOPE_LINKroute on this link
RT_SCOPE_HOSTroute on the local host
RT_SCOPE_NOWHEREdestination 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_CLONEDroute is cloned from another route
RTM_F_EQUALIZEa multipath equalizer (not yet implemented)
rtm_table specifies the routing table
RT_TABLE_UNSPECan unspecified routing table
RT_TABLE_DEFAULTthe default table
RT_TABLE_MAINthe main table
RT_TABLE_LOCALthe local table
The user may assign arbitrary values between RT_TABLE_UNSPEC and RT_TABLE_DEFAULT.
Attributes
rta_type  Value type  Description

RTA_UNSPEC  -  ignored
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  (see linux/rtnetlink.h)
RTA_SESSION    No longer used
RTA_MP_ALGO    No longer used
RTA_TABLE  int   Routing table ID; if set,
rtm_table is ignored
RTA_MARK  int  
RTA_MFC_STATS  struct rta_mfc_stats  (see linux/rtnetlink.h)
RTA_VIA  struct rtvia   Gateway in 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)
RTA_METRICS contains an array of struct rtattr with their corresponding attributes:
Attributes
rta_typeValue typeDescription

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 roun-trip time for this route.
RTAX_RTTVAR__u32 Estimated roun-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_ALGOasciiz string Congestion Control algorithm to use for this route.
RTAX_FASTOPEN_NO_COOKIE__u32 Allow TCP Fast Open without cookie.
Metrics that are locked with RTAX_LOCK take precedence over the values normally used by the kernel (computed or assigned by a sysctl or setsockopt(2)). Therefore, some metrics, like RTAX_RTO_MIN, have no effect unless their bit is set in RTAX_LOCK.
RTA_MULTIPATH contains several packed instances of struct rtnexthop together with nested RTAs (RTA_GATEWAY):
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 exist a bunch of RTNH_* macros similar to RTA_* and NLHDR_* macros useful to handle these structures.
struct rtvia {
    unsigned short rtvia_family;
    unsigned char  rtvia_addr[0]; };
rtvia_addr is the address, rtvia_family is its family type.
RTA_PREF may contain values ICMPV6_ROUTER_PREF_LOW, ICMPV6_ROUTER_PREF_MEDIUM, and ICMPV6_ROUTER_PREF_HIGH defined incw <linux/icmpv6.h>.
RTA_ENCAP_TYPE may contain 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 Add, remove, or receive information about a neighbor table entry (e.g., an ARP entry). The message contains 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; };
ndm_state is a bit mask of the following states:
NUD_INCOMPLETEa currently resolving cache entry
NUD_REACHABLEa confirmed working cache entry
NUD_STALEan expired cache entry
NUD_DELAYan entry waiting for a timer
NUD_PROBEa cache entry that is currently reprobed
NUD_FAILEDan invalid cache entry
NUD_NOARPa device with no destination cache
NUD_PERMANENTa static entry
Valid ndm_flags are:
NTF_PROXYa proxy arp entry
NTF_ROUTERan IPv6 router
The rtattr struct has the following meanings for the rta_type field:
NDA_UNSPECunknown type
NDA_DSTa neighbor cache n/w layer destination address
NDA_LLADDRa neighbor cache link layer address
NDA_CACHEINFOcache statistics
If the rta_type field is NDA_CACHEINFO, then a struct nda_cacheinfo header follows.
RTM_NEWRULE
RTM_DELRULE RTM_GETRULE Add, delete, or retrieve a routing rule. Carries a struct rtmsg
RTM_NEWQDISC
RTM_DELQDISC RTM_GETQDISC Add, remove, or get a queueing discipline. The message contains a struct tcmsg and may be followed by a series of attributes.
struct tcmsg {
    unsigned char    tcm_family;
    int              tcm_ifindex;   /* interface index */
    __u32            tcm_handle;    /* Qdisc handle */
    __u32            tcm_parent;    /* Parent qdisc */
    __u32            tcm_info; };
Attributes
rta_type  Value type  Description

TCA_UNSPEC  -  unspecified
TCA_KIND  asciiz string  Name of queueing discipline
TCA_OPTIONS  byte sequence  Qdis-specific options follow
TCA_STATS  struct tc_stats  Qdisc statistics
TCA_XSTATS  qdis-specific  Modul-specific statistics
TCA_RATE  struct tc_estimator  Rate limit
In addition, various other qdis-modul-specific attributes are allowed. For more information see the appropriate include files.
RTM_NEWTCLASS
RTM_DELTCLASS RTM_GETTCLASS Add, remove, or get a traffic class. These messages contain a struct tcmsg as described above.
RTM_NEWTFILTER
RTM_DELTFILTER RTM_GETTFILTER Add, remove, or receive information about a traffic filter. These messages contain a struct tcmsg as described above.
 

VERSIONS

rtnetlink is a new feature of Linux 2.2.  

BUGS

This manual page is incomplete.  

SEE ALSO

cmsg(3), rtnetlink(3), ip(7), netlink(7)


 

Index

NAME
SYNOPSIS
DESCRIPTION
Routing attributes
Messages
VERSIONS
BUGS
SEE ALSO