RTNETLINK(4)
β¨ NAME β¨
RTNetlink β Network configuration-specific Netlink family π
πΈ SYNOPSIS πΈ
#include <netlink/netlink.h>
#include <netlink/netlink_route.h>
int
socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
π¦ DESCRIPTION π¦
okay so the NETLINK_ROUTE family is literally out here aiming to be the primary configuration mechanism for all network-related tasks. β¨ Currently it supports configuring interfaces, interface addresses, routes, nexthops and arp/ndp neighbors. periodt. π«Ά
π₯ ROUTES π₯
All route configuration messages share the common header:
struct rtmsg {
unsigned char rtm_family; /* address family */
unsigned char rtm_dst_len; /* Prefix length */
unsigned char rtm_src_len; /* Deprecated, set to 0 */
unsigned char rtm_tos; /* Type of service (not used) */
unsigned char rtm_table; /* deprecated, set to 0 */
unsigned char rtm_protocol; /* Routing protocol id (RTPROT_) */
unsigned char rtm_scope; /* Route distance (RT_SCOPE_) */
unsigned char rtm_type; /* Route type (RTN_) */
unsigned rtm_flags; /* Route flags (not supported) */
};
The rtm_family specifies the route family to be operated on. Currently, AF_INET6 and AF_INET are the only supported families. π The route prefix length is stored in rtm_dst_len. The caller should set the originator identity (one of the RTPROT_ values) in rtm_protocol. It is useful for users and for the application itself, allowing for easy identification of self-originated routes. The route scope has to be set via rtm_scope field. The supported values are:
RT_SCOPE_UNIVERSEβ Global scopeRT_SCOPE_LINKβ Link scope
Route type needs to be set. The defined values are:
RTN_UNICASTβ Unicast routeRTN_MULTICASTβ Multicast routeRTN_BLACKHOLEβ Drops traffic towards destinationRTN_PROHIBITβ Drops traffic and sends reject
The following messages are supported:
π RTM_NEWROUTE π
Adds a new route. All NL flags are supported. Extending a multipath route requires NLM_F_APPEND flag. π₯
π RTM_DELROUTE π
Tries to delete a route. The route is specified using a combination of RTA_DST TLV and rtm_dst_len.
π RTM_GETROUTE π
Fetches a single route or all routes in the current VNET, depending on the NLM_F_DUMP flag. Each route is reported as RTM_NEWROUTE message. The following filters are recognised by the kernel:
rtm_familyβ required family orAF_UNSPECRTA_TABLEβ fib number orRT_TABLE_UNSPECto return all fibs
π§ TLVs π§
RTA_DSTβ (binary) IPv4/IPv6 address, depending on thertm_family.RTA_OIFβ (uint32_t) transmit interface index.RTA_GATEWAYβ (binary) IPv4/IPv6 gateway address, depending on thertm_family.RTA_METRICSβ (nested) Container attribute, listing route properties. The only supported sub-attribute isRTAX_MTU, which stores path MTU asuint32_t.RTA_MULTIPATHβ This attribute contains multipath route nexthops with their weights. These nexthops are represented as a sequence ofrtnexthopstructures, each followed byRTA_GATEWAYorRTA_VIAattributes.
struct rtnexthop {
unsigned short rtnh_len;
unsigned char rtnh_flags;
unsigned char rtnh_hops; /* nexthop weight */
int rtnh_ifindex;
};
The rtnh_len field specifies the total nexthop info length, including both struct rtnexthop and the following TLVs. The rtnh_hops field stores relative nexthop weight, used for load balancing between group members. The rtnh_ifindex field contains the index of the transmit interface. π
The following TLVs can follow the structure:
RTA_GATEWAYβ IPv4/IPv6 nexthop address of the gatewayRTA_VIAβ IPv6 nexthop address for IPv4 routeRTA_KNH_IDβ Kernel-specific index of the nexthopRTA_KNH_IDβ (uint32_t) (FreeBSD-specific) Auto-allocated kernel index of the nexthop.RTA_RTFLAGSβ (uint32_t) (FreeBSD-specific) rtsock route flags.RTA_TABLEβ (uint32_t) Fib number of the route. Default route table isRT_TABLE_MAIN. To explicitly specify "all tables" one needs to set the value toRT_TABLE_UNSPEC.RTA_EXPIRESβ (uint32_t) seconds till path expiration.RTA_NH_IDβ (uint32_t) userland nexthop or nexthop group index.
π Groups π
The following groups are defined:
RTNLGRP_IPV4_ROUTEβ Notifies on IPv4 route arrival/removal/changeRTNLGRP_IPV6_ROUTEβ Notifies on IPv6 route arrival/removal/change
π NEXTHOPS π
All nexthop/nexthop group configuration messages share the common header:
struct nhmsg {
unsigned char nh_family; /* transport family */
unsigned char nh_scope; /* ignored on RX, filled by kernel */
unsigned char nh_protocol; /* Routing protocol that installed nh */
unsigned char resvd;
unsigned int nh_flags; /* RTNH_F_* flags from route.h */
};
The nh_family specifies the gateway address family. It can be different from route address family for IPv4 routes with IPv6 nexthops. The nh_protocol is similar to rtm_protocol field, which designates originator application identity. π¦
The following messages are supported:
β RTM_NEWNEXTHOP β
Creates a new nexthop or nexthop group.
β RTM_DELNEXTHOP β
Deletes nexthop or nexthop group. The required object is specified by the RTA_NH_ID attribute.
β RTM_GETNEXTHOP β
Fetches a single nexthop or all nexthops/nexthop groups, depending on the NLM_F_DUMP flag. The following filters are recognised by the kernel:
RTA_NH_IDβ nexthop or nexthop group idNHA_GROUPSβ match only nexthop groups
π TLVs π
RTA_NH_IDβ (uint32_t) Nexthop index used to identify particular nexthop or nexthop group. Should be provided by userland at the nexthop creation time.NHA_GROUPβ This attribute designates the nexthop group and contains all of its nexthops and their relative weights. The attribute consists of a list ofnexthop_grpstructures:
struct nexthop_grp {
uint32_t id; /* nexhop userland index */
uint8_t weight; /* weight of this nexthop */
uint8_t resvd1;
uint16_t resvd2;
};
NHA_GROUP_TYPEβ (uint16_t) Nexthop group type, set to one of the following types:NEXTHOP_GRP_TYPE_MPATHβ default multipath group
NHA_BLACKHOLEβ (flag) Marks the nexthop as blackhole.NHA_OIFβ (uint32_t) Transmit interface index of the nexthop.NHA_GATEWAYβ (binary) IPv4/IPv6 gateway addressNHA_GROUPSβ (flag) Matches nexthop groups during dump.
π§ Groups π§
The following groups are defined:
RTNLGRP_NEXTHOPβ Notifies on nexthop/groups arrival/removal/change
πΈ INTERFACES πΈ
Okay bestie, every single interface config message shares this common header β like, all of them, no cap:
struct ifinfomsg {
unsigned char ifi_family; /* not used, set to 0 */
unsigned char __ifi_pad;
unsigned short ifi_type; /* ARPHRD_* */
int ifi_index; /* Interface index */
unsigned ifi_flags; /* IFF_* flags */
unsigned ifi_change; /* IFF_* change mask */
};
π RTM_NEWLINK π
Creates a new interface. The only mandatory TLV is IFLA_IFNAME. The following attributes are returned inside the nested NLMSGERR_ATTR_COOKIE:
IFLA_NEW_IFINDEXβ(uint32)created interface indexIFLA_IFNAMEβ(string)created interface name
π RTM_DELLINK π
Deletes the interface specified by IFLA_IFNAME.
π RTM_GETLINK π
Fetches a single interface or all interfaces in the current VNET, depending on the NLM_F_DUMP flag. Each interface is reported as a RTM_NEWLINK message. The following filters are recognised by the kernel:
ifi_indexβ interface indexIFLA_IFNAMEβ interface nameIFLA_ALT_IFNAMEβ interface name
β¨ TLVs β¨
IFLA_ADDRESSβ(binary)Llink-level interface address (MAC).IFLA_BROADCASTβ(binary)(readonly)Link-level broadcast address.IFLA_IFNAMEβ(string)New interface name.IFLA_IFALIASβ(string)Interface description.IFLA_LINKβ(uint32_t)(readonly)Interface index.IFLA_MASTERβ(uint32_t)Parent interface index.IFLA_LINKINFOβ(nested)Interface type-specific attributes:IFLA_INFO_KINDβ(string)interface type ("vlan")IFLA_INFO_DATAβ(nested)custom attributes
The following types and attributes are supported:
vlanβ your 802.1Q bestie, with these attributes:IFLA_VLAN_IDβ(uint16_t)802.1Q vlan idIFLA_VLAN_PROTOCOLβ(uint16_t)Protocol:ETHERTYPE_VLANorETHERTYPE_QINQ
IFLA_OPERSTATEβ(uint8_t)Interface operational state per RFC 2863. Can be one of the following:IF_OPER_UNKNOWNβ status can not be determinedIF_OPER_NOTPRESENTβ some (hardware) component not presentIF_OPER_DOWNβ downIF_OPER_LOWERLAYERDOWNβ some lower-level interface is downIF_OPER_TESTINGβ in some test modeIF_OPER_DORMANTβ "up" but waiting for some condition (802.1X)IF_OPER_UPβ ready to pass packets
IFLA_STATS64β(readonly)Consists of the following 64-bit counters structure:
struct rtnl_link_stats64 {
uint64_t rx_packets; /* total RX packets (IFCOUNTER_IPACKETS) */
uint64_t tx_packets; /* total TX packets (IFCOUNTER_OPACKETS) */
uint64_t rx_bytes; /* total RX bytes (IFCOUNTER_IBYTES) */
uint64_t tx_bytes; /* total TX bytes (IFCOUNTER_OBYTES) */
uint64_t rx_errors; /* RX errors (IFCOUNTER_IERRORS) */
uint64_t tx_errors; /* RX errors (IFCOUNTER_OERRORS) */
uint64_t rx_dropped; /* RX drop (no space in ring/no bufs) (IFCOUNTER_IQDROPS) */
uint64_t tx_dropped; /* TX drop (IFCOUNTER_OQDROPS) */
uint64_t multicast; /* RX multicast packets (IFCOUNTER_IMCASTS) */
uint64_t collisions; /* not supported */
uint64_t rx_length_errors; /* not supported */
uint64_t rx_over_errors; /* not supported */
uint64_t rx_crc_errors; /* not supported */
uint64_t rx_frame_errors; /* not supported */
uint64_t rx_fifo_errors; /* not supported */
uint64_t rx_missed_errors; /* not supported */
uint64_t tx_aborted_errors; /* not supported */
uint64_t tx_carrier_errors; /* not supported */
uint64_t tx_fifo_errors; /* not supported */
uint64_t tx_heartbeat_errors; /* not supported */
uint64_t tx_window_errors; /* not supported */
uint64_t rx_compressed; /* not supported */
uint64_t tx_compressed; /* not supported */
uint64_t rx_nohandler; /* dropped due to no proto handler (IFCOUNTER_NOPROTO) */
};
π Groups π
The following groups are defined:
RTNLGRP_LINKβ Notifies on interface arrival/removal/change
π¦ INTERFACE ADDRESSES π¦
All interface address configuration messages share the common header:
struct ifaddrmsg {
uint8_t ifa_family; /* Address family */
uint8_t ifa_prefixlen; /* Prefix length */
uint8_t ifa_flags; /* Address-specific flags */
uint8_t ifa_scope; /* Address scope */
uint32_t ifa_index; /* Link ifindex */
};
The ifa_family specifies the address family of the interface address. The ifa_prefixlen specifies the prefix length if applicable for the address family. The ifa_index specifies the interface index of the target interface.
π RTM_NEWADDR π
Not supported. That's the whole mood, bestie. π
π RTM_DELADDR π
Not supported. Again: nope.
π RTM_GETADDR π
Fetches interface addresses in the current VNET matching conditions. Each address is reported as a RTM_NEWADDR message. The following filters are recognised by the kernel:
ifa_familyβ required family orAF_UNSPECifa_indexβ matching interface index or0
β¨ TLVs β¨
IFA_ADDRESSβ(binary)masked interface address or destination address for p2p interfaces.IFA_LOCALβ(binary)local interface address. Set for IPv4 and p2p addresses.IFA_LABELβ(string)interface name.IFA_BROADCASTβ(binary)broadcast interface address.
π Groups π
The following groups are defined:
RTNLGRP_IPV4_IFADDRβ Notifies on IPv4 ifaddr arrival/removal/changeRTNLGRP_IPV6_IFADDRβ Notifies on IPv6 ifaddr arrival/removal/change
π NEIGHBORS π
All neighbor configuration messages share the common header:
struct ndmsg {
uint8_t ndm_family;
uint8_t ndm_pad1;
uint16_t ndm_pad2;
int32_t ndm_ifindex;
uint16_t ndm_state;
uint8_t ndm_flags;
uint8_t ndm_type;
};
The ndm_family field specifies the address family (IPv4 or IPv6) of the neighbor. The ndm_ifindex specifies the interface to operate on. The ndm_state represents the entry state according to the neighbor model. The state can be one of the following:
NUD_INCOMPLETEβ No lladdr, address resolution in progressNUD_REACHABLEβ reachable & recently resolvedNUD_STALEβ has lladdr but it's staleNUD_DELAYβ has lladdr, is stale, probes delayedNUD_PROBEβ has lladdr, is stale, probes sentNUD_FAILEDβ unused
The ndm_flags field stores the options specific to this entry. Available flags:
NTF_SELFβ local station (LLE_IFADDR)NTF_PROXYβ proxy entry (LLE_PUB)NTF_STICKYβ permanent entry (LLE_STATIC)NTF_ROUTERβ dst indicated itself as a router
π RTM_NEWNEIGH π
Creates new neighbor entry. The mandatory options are NDA_DST, NDA_LLADDR and NDA_IFINDEX.
π RTM_DELNEIGH π
Deletes the neighbor entry. The entry is specified by the combination of NDA_DST and NDA_IFINDEX.
π RTM_GETNEIGH π
Fetches a single neighbor or all neighbors in the current VNET, depending on the NLM_F_DUMP flag. Each entry is reported as RTM_NEWNEIGH message. The following filters are recognised by the kernel:
ndm_familyβ required family orAF_UNSPECndm_ifindexβ target ifindexNDA_IFINDEXβ target ifindex
β¨ TLVs β¨
NDA_DSTβ(binary)neighbor IPv4/IPv6 address.NDA_LLADDRβ(binary)neighbor link-level address.NDA_IFINDEXβ(uint32_t)interface index.NDA_FLAGS_EXTβ(uint32_t)extended version of ndm_flags.
π Groups π
The following groups are defined:
RTNLGRP_NEIGHβ Notifies on ARP/NDP neighbor arrival/removal/change
π SEE ALSO π
netlink(4), route(4) β that's the whole list, bestie. Go read those too. π«Ά
β³ HISTORY β³
The NETLINK_ROUTE protocol family appeared in FreeBSD 13.2.
π AUTHORS π
The netlink was implemented by Alexander Chernikov <melifaro@FreeBSD.org>. It was derived from the Google Summer of Code 2021 project by Ng Peng Nam Sean.
This page's source/version is Debian, it's dated November 1, 2022, and the page is RTNETLINK(4). Stay slay, bestie! π
β¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (C) 2022 Alexander Chernikov <melifaro@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms