# RTNETLINK(7) - man - phpMan

[RTNETLINK(7)](https://www.chedong.com/phpMan.php/man/RTNETLINK/7/markdown)                          Linux Programmer's Manual                         [RTNETLINK(7)](https://www.chedong.com/phpMan.php/man/RTNETLINK/7/markdown)



## NAME
       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 parame‐
       ters, 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)](https://www.chedong.com/phpMan.php/man/netlink/7/markdown)
       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  **rt**‐‐
       [**netlink**(3)](https://www.chedong.com/phpMan.php/man/netlink/3/markdown).

### 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)](https://www.chedong.com/phpMan.php/man/netdevice/7/markdown); _ifi_index_ is the unique  inter‐
              face  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  re‐
              served for future use and should be always set to 0xFFFFFFFF.

                                 Routing attributes
              **rta**___**type**         Value type         Description
              ─────────────────────────────────────────────────────────
              **IFLA**___**UNSPEC**      -                  unspecified
              **IFLA**___**ADDRESS**     hardware address   interface L2 address
              **IFLA**___**BROADCAST**   hardware address   L2 broadcast address
              **IFLA**___**IFNAME**      asciiz string      Device name
              **IFLA**___**MTU**         unsigned int       MTU of the device
              **IFLA**___**LINK**        int                Link type

              **IFLA**___**QDISC**       asciiz string      Queueing discipline
              **IFLA**___**STATS**       see below          Interface Statistics

              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 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 inter‐
              face 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**___**type**        Value type             Description
              ─────────────────────────────────────────────────────────────
              **IFA**___**UNSPEC**      -                      unspecified
              **IFA**___**ADDRESS**     raw protocol address   interface address
              **IFA**___**LOCAL**       raw protocol address   local address
              **IFA**___**LABEL**       asciiz string          name of the interface
              **IFA**___**BROADCAST**   raw protocol address   broadcast address
              **IFA**___**ANYCAST**     raw protocol address   anycast address
              **IFA**___**CACHEINFO**   struct 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**___**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 uni‐
                                cast)
              **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  im‐
                                plemented)

              **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 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**___**UNIVERSE**   global route
              **RT**___**SCOPE**___**SITE**       interior  route  in the local au‐
                                  tonomous 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

              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**     int                   Route metric
              **RTA**___**MULTIPATH**                         Multipath nexthop data br
                                                    (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 pref‐
                                                    erence (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**___**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**___**EN**‐‐
              **CAP**___**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**___**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

              Valid _ndm_flags_ are:

              **NTF**___**PROXY**    a proxy arp entry
              **NTF**___**ROUTER**   an IPv6 router

              The _rtattr_ struct has the following meanings for the _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

              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        Qdisc-specific options follow
              **TCA**___**STATS**    struct tc_stats      Qdisc statistics
              **TCA**___**XSTATS**   qdisc-specific       Module-specific statistics
              **TCA**___**RATE**     struct tc_estimator  Rate limit

              In addition, various other qdisc-module-specific attributes are allowed.  For more in‐
              formation 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 de‐
              scribed 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)](https://www.chedong.com/phpMan.php/man/cmsg/3/markdown), [**rtnetlink**(3)](https://www.chedong.com/phpMan.php/man/rtnetlink/3/markdown), [**ip**(7)](https://www.chedong.com/phpMan.php/man/ip/7/markdown), [**netlink**(7)](https://www.chedong.com/phpMan.php/man/netlink/7/markdown)

## COLOPHON
       This  page  is  part  of  release  5.10 of the Linux _man-pages_ project.  A description of the
       project, information about reporting bugs, and the latest version of this page, can be  found
       at <https://www.kernel.org/doc/man-pages/>.



Linux                                        2020-06-09                                 [RTNETLINK(7)](https://www.chedong.com/phpMan.php/man/RTNETLINK/7/markdown)
