rtnetlink (7) Linux Manual Page
rtnetlink – Linux IPv4 routing socket
Synopsis
#include <asm/types.h>
#include <linux/if_link.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 user-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: 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 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 theRTM_NEWLINKmessage, thus creating a link with the given ifindex); ifi_change is reserved for future use and should be always set to 0xFFFFFFFF.Routing attributes rta_typeValue type Description
IFLA_UNSPEC– unspecified IFLA_ADDRESShardware address interface L2 address IFLA_BROADCASThardware address L2 broadcast address IFLA_IFNAMEasciiz string Device name IFLA_MTUunsigned int MTU of the device IFLA_LINKint Link type IFLA_QDISCasciiz string Queueing discipline IFLA_STATSsee below Interface Statistics - The value type for
IFLA_STATSis 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 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_INETorAF_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 ofIFA_F_SECONDARYfor secondary address (old alias interface),IFA_F_PERMANENTfor a permanent address set by the user and other undocumented flags.Attributes rta_typeValue type Description
IFA_UNSPEC– unspecified IFA_ADDRESSraw protocol address interface address IFA_LOCALraw protocol address local address IFA_LABELasciiz string name of the interface IFA_BROADCASTraw protocol address broadcast address IFA_ANYCASTraw protocol address anycast address IFA_CACHEINFOstruct ifa_cacheinfo Address information 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 administratorValues larger than
RTPROT_STATICare 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_SITEinterior 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_UNIVERSEandRT_SCOPE_SITEare available to the user.- The rtm_flags have the following meanings:
RTM_F_NOTIFYif 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_UNSPECandRT_TABLE_DEFAULT.Attributes rta_typeValue type Description
RTA_UNSPEC– ignored RTA_DSTprotocol address Route destination address RTA_SRCprotocol address Route source address RTA_IIFint Input interface index RTA_OIFint Output interface index RTA_GATEWAYprotocol address The gateway of the route RTA_PRIORITYint Priority of route RTA_PREFSRCprotocol address Preferred source address RTA_METRICSint Route metric RTA_MULTIPATHMultipath nexthop data br (see below). RTA_PROTOINFONo longer used RTA_FLOWint Route realm RTA_CACHEINFOstruct rta_cacheinfo (see linux/rtnetlink.h) RTA_SESSIONNo longer used RTA_MP_ALGONo longer used RTA_TABLEint Routing table ID; if set,
rtm_table is ignoredRTA_MARKint RTA_MFC_STATSstruct rta_mfc_stats (see linux/rtnetlink.h) RTA_VIAstruct rtvia Gateway in different AF (see below) RTA_NEWDSTprotocol address Change packet destination address RTA_PREFchar RFC4191 IPv6 router preference (see below) RTA_ENCAP_TYPEshort Encapsulation type for
lwtunnels (see below)RTA_ENCAPDefined by RTA_ENCAP_TYPE RTA_EXPIRESint Expire time for IPv6 routes (in seconds) RTA_MULTIPATHcontains 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 */
- struct ifinfomsg {
