![]() |
![]()
| ![]() |
![]()
NAME
SYNOPSIS
int
DESCRIPTIONNetlink is a user-kernel message-based communication protocol primarily used for network stack configuration. Netlink is easily extendable and supports large dumps and event notifications, all via a single socket. The protocol is fully asynchronous, allowing one to issue and track multiple requests at once. Netlink consists of multiple families, which commonly group the commands belonging to the particular kernel subsystem. Currently, the supported families are: NETLINK_ROUTE network configuration, NETLINK_GENERIC "container" family The Netlink has its own sockaddr structure: struct sockaddr_nl { uint8_t nl_len; /* sizeof(sockaddr_nl) */ sa_family_t nl_family; /* netlink family */ uint16_t nl_pad; /* reserved, set to 0 */ uint32_t nl_pid; /* automatically selected, set to 0 */ uint32_t nl_groups; /* multicast groups mask to bind to */ }; Typically, filling this structure is not required for socket operations. It is presented here for completeness. PROTOCOL DESCRIPTIONThe protocol is message-based. Each message starts with the mandatory nlmsghdr header, followed by the family-specific header and the list of type-length-value pairs (TLVs). TLVs can be nested. All headers and TLVS are padded to 4-byte boundaries. Each send(2) or recv(2) system call may contain multiple messages. BASE HEADERstruct nlmsghdr { uint32_t nlmsg_len; /* Length of message including header */ uint16_t nlmsg_type; /* Message type identifier */ uint16_t nlmsg_flags; /* Flags (NLM_F_) */ uint32_t nlmsg_seq; /* Sequence number */ uint32_t nlmsg_pid; /* Sending process port ID */ }; The nlmsg_len field stores the whole message
length, in bytes, including the header. This length has to be rounded up to
the nearest 4-byte boundary when iterating over messages. The
nlmsg_type field represents the command/request type.
This value is family-specific. The list of supported commands can be found
in the relevant family header file. nlmsg_seq is a
user-provided request identifier. An application can track the operation
result using the NLM_F_REQUEST Indicates that the message is an actual request to the kernel NLM_F_ACK Request an explicit ACK message with an operation result The following generic flags are defined for the "GET" request types: NLM_F_ROOT Return the whole dataset NLM_F_MATCH Return all entries matching the criteria NLM_F_DUMP
The following generic flags are defined for the "NEW" request types: NLM_F_CREATE Create an object if none exists NLM_F_EXCL Don't replace an object if it exists NLM_F_REPLACE Replace an existing matching object NLM_F_APPEND Append to an existing object The following generic flags are defined for the replies: NLM_F_MULTI Indicates that the message is part of the message group NLM_F_DUMP_INTR Indicates that the state dump was not completed NLM_F_DUMP_FILTERED Indicates that the dump was filtered per request NLM_F_CAPPED Indicates the original message was capped to its header NLM_F_ACK_TLVS Indicates that extended ACK TLVs were included TLVsMost messages encode their attributes as type-length-value pairs (TLVs). The base TLV header: struct nlattr { uint16_t nla_len; /* Total attribute length */ uint16_t nla_type; /* Attribute type */ }; RTN_MULTICAST type value is only valid for
RTM_NEWROUTE , RTM_DELROUTE
and RTM_GETROUTE messages. TLVs can be nested; in that
case internal TLVs may have their own sub-types. All TLVs are packed with
4-byte padding.
CONTROL MESSAGESA number of generic control messages are reserved in each family.
struct nlmsgerr { int error; /* Standard errno */ struct nlmsghdr msg; /* Original message header */ }; NETLINK_CAP_ACK socket option is not set, the
remainder of the original message will follow. If the
NETLINK_EXT_ACK socket option is set, the kernel may
add a NLMSGERR_ATTR_MSG string TLV with the textual
error description, optionally followed by the
NLMSGERR_ATTR_OFFS TLV, indicating the offset from the
message start that triggered an error. Some operations may return additional
metadata encapsulated in the NLMSGERR_ATTR_COOKIE TLV.
The metadata format is specific to the operation. If the operation reply is a
multipart message, then no NLMSG_ERROR reply is
generated, only a NLMSG_DONE message, closing
multipart sequence.
SOCKET OPTIONSNetlink supports a number of custom socket options, which can be
set with
setsockopt(2)
with the
Additionally, netlink overrides the following socket options from
the
SYSCTL VARIABLESA set of sysctl(8) variables is available to tweak run-time parameters:
DEBUGGINGNetlink implements per-functional-unit debugging, with different severities controllable via the net.netlink.debug branch. These messages are logged in the kernel message buffer and can be seen in dmesg(8) The following severity levels are defined:
ERRORSNetlink reports operation results, including errors and error
metadata, by sending a
SEE ALSOJ. Salim, H. Khosravi, A. Kleen, and A. Kuznetsov, Linux Netlink as an IP Services Protocol, RFC 3549. HISTORYThe netlink protocol appeared in FreeBSD 13.2. AUTHORSThe 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.
|