netlink(7)
๐ netlink(7) ๐
okay so real talk, this page lives in the Miscellaneous Information Manual, and it's the literal blueprint for netlink โ the secret group chat between your kernel and user space. it's long af, so we're doing it in two parts. buckle up bestie, let's get into it ๐ฆ
โจ NAME โจ
netlink โ communication between kernel and user space (AF_NETLINK)
๐ธ SYNOPSIS ๐ธ
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
netlink_socket = socket(AF_NETLINK, socket_type, netlink_family);
๐ DESCRIPTION ๐
so basically, netlink is the messenger service between the kernel and user-space processes. it's got two sides, and they're lowkey different creatures:
- a standard sockets-based interface for user-space processes
- an internal kernel API for kernel modules
heads up though โ that internal kernel interface? not documented on this page, periodt. there's also an obsolete netlink interface that goes through netlink character devices โ that one is also not documented here, and it only sticks around for backward compatibility. like, it's giving "still invited to the party but nobody talks to it" energy ๐
Netlink is a datagram-oriented service. both SOCK_RAW and SOCK_DGRAM are valid values for socket_type. and here's the quirky bit โ the netlink protocol does not distinguish between datagram and raw sockets. so don't stress about picking the "right" one, they're the same vibe here ๐
netlink_family is the one that picks which kernel module or netlink group you're talking to. here are all the currently assigned netlink families, and yes, we're listing every single one because we don't skip:
NETLINK_ROUTEโ receives routing and link updates, and can be used to modify the routing tables (both IPv4 and IPv6), IP addresses, link parameters, neighbor setups, queueing disciplines, traffic classes, and packet classifiers. seertnetlink(7)for the deets.NETLINK_W1(Linux 2.6.13 to Linux 2.16.17) โ messages from the 1-wire subsystem.NETLINK_USERSOCKโ reserved for user-mode socket protocols.NETLINK_FIREWALL(up to and including Linux 3.4) โ transports IPv4 packets from netfilter to user space. used by theip_queuekernel module. and ngl, after a long, long period of being declared obsolete (in favor of the more advancednfnetlink_queuefeature),NETLINK_FIREWALLwas removed in Linux 3.5. rip ๐NETLINK_SOCK_DIAG(since Linux 3.3) โ queries info about sockets of various protocol families from the kernel. seesock_diag(7).NETLINK_INET_DIAG(since Linux 2.6.14) โ an obsolete synonym forNETLINK_SOCK_DIAG.NETLINK_NFLOG(up to and including Linux 3.16) โ Netfilter/iptables ULOG.NETLINK_XFRMโ IPsec.NETLINK_SELINUX(since Linux 2.6.4) โ SELinux event notifications.NETLINK_ISCSI(since Linux 2.6.15) โ Open-iSCSI.NETLINK_AUDIT(since Linux 2.6.6) โ auditing.NETLINK_FIB_LOOKUP(since Linux 2.6.13) โ access to FIB lookup from user space.NETLINK_CONNECTOR(since Linux 2.6.14) โ kernel connector. peepDocumentation/driver-api/connector.rst(or/Documentation/connector/connector.*in Linux 5.2 and earlier) in the Linux kernel source tree for more.NETLINK_NETFILTER(since Linux 2.6.14) โ Netfilter subsystem.NETLINK_SCSITRANSPORT(since Linux 2.6.19) โ SCSI Transports.NETLINK_RDMA(since Linux 3.0) โ Infiniband RDMA.NETLINK_IP6_FW(up to and including Linux 3.4) โ transports IPv6 packets from netfilter to user space. used by theip6_queuekernel module.NETLINK_DNRTMSGโ DECnet routing messages.NETLINK_KOBJECT_UEVENT(since Linux 2.6.10) โ kernel messages to user space.NETLINK_GENERIC(since Linux 2.6.15) โ generic netlink family for simplified netlink usage.NETLINK_CRYPTO(since Linux 3.2) โ netlink interface to request information about ciphers registered with the kernel crypto API, and also to allow configuration of the kernel crypto API.
okay so now we get to the actual message shape, and this is where it gets โจstructuralโจ
Netlink messages are a byte stream made of one or more nlmsghdr headers plus their associated payload. and the rule is: you access that byte stream only with the standard NLMSG_* macros. see netlink(3) for more on that.
in multipart messages (multiple nlmsghdr headers with their associated payload all in one byte stream), the first header and all the following ones have the NLM_F_MULTI flag set โ except for the last header, which has type NLMSG_DONE. it's giving "everyone hold hands except the last one who closes the door" ๐ช
after each nlmsghdr comes the payload:
struct nlmsghdr {
__u32 nlmsg_len; /* Size of message including header */
__u16 nlmsg_type; /* Type of message content */
__u16 nlmsg_flags; /* Additional flags */
__u32 nlmsg_seq; /* Sequence number */
__u32 nlmsg_pid; /* Sender port ID */
};
nlmsg_type can be one of the standard message types:
NLMSG_NOOPโ this message is meant to be ignored.NLMSG_ERRORโ this message signals an error, and the payload contains annlmsgerrstructure.NLMSG_DONEโ this message terminates a multipart message.
error messages get the original request appended โ unless the user requests to cap the error message โ and they get extra error data if requested. (iykyk, it's the ack flags doing the work ๐ )
struct nlmsgerr {
int error; /* Negative errno or 0 for acknowledgements */
struct nlmsghdr msg; /* Message header that caused the error */
/*
* followed by the message contents
* unless NETLINK_CAP_ACK was set
* or the ACK indicates success (error == 0).
* For example Generic Netlink message with attributes.
* message size is aligned with NLMSG_ALIGN()
*/
/*
* followed by TLVs defined in enum nlmsgerr_attrs
* if NETLINK_EXT_ACK was set
*/
};
a netlink family usually specifies more message types than these โ check the appropriate manual pages for that. for example, rtnetlink(7) for NETLINK_ROUTE. ๐
๐ Standard flag bits in nlmsg_flags ๐
NLM_F_REQUESTโ Must be set on all request messages.NLM_F_MULTIโ The message is part of a multipart message terminated byNLMSG_DONE.NLM_F_ACKโ Request for an acknowledgement on success.NLM_F_ECHOโ Echo this request.
๐ Additional flag bits for GET requests ๐
NLM_F_ROOTโ Return the complete table instead of a single entry.NLM_F_MATCHโ Return all entries matching criteria passed in message content. Not implemented yet.NLM_F_ATOMICโ Return an atomic snapshot of the table.NLM_F_DUMPโ Convenience macro; equivalent to(NLM_F_ROOT|NLM_F_MATCH).
โ ๏ธ note thatNLM_F_ATOMICrequires theCAP_NET_ADMINcapability, or an effective UID of 0. no shortcuts bestie, it's a permissions check ๐
๐ Additional flag bits for NEW requests ๐
NLM_F_REPLACEโ Replace existing matching object.NLM_F_EXCLโ Don't replace if the object already exists.NLM_F_CREATEโ Create object if it doesn't already exist.NLM_F_APPENDโ Add to the end of the object list.
nlmsg_seq and nlmsg_pid are there to track messages. nlmsg_pid shows the origin of the message. but here's the catch โ there is no 1:1 relationship between nlmsg_pid and the PID of the process when the message originated from a netlink socket. see the ADDRESS FORMATS section below for more on that one. ๐
both nlmsg_seq and nlmsg_pid are opaque to netlink core. it just carries them, it doesn't read them.
now, the big mood shift: netlink is not a reliable protocol. it genuinely tries its best to deliver a message to its destination(s), but it may drop messages when an out-of-memory condition or some other error happens. for reliable transfer, the sender can request an acknowledgement from the receiver by setting the NLM_F_ACK flag. an acknowledgement is an NLMSG_ERROR packet with the error field set to 0. and the application has to generate acknowledgements for received messages itself. the kernel tries to send an NLMSG_ERROR message for every failed packet, and a user process should follow that same convention too. consistency is the vibe ๐ซถ
however โ and this is the part nobody wants to hear โ reliable transmissions from kernel to user are impossible in any case ๐ญ. the kernel can't send a netlink message if the socket buffer is full: the message gets dropped, and the kernel and the user-space process no longer have the same view of kernel state. it is up to the application to detect when that happens (via the ENOBUFS error returned by recvmsg(2)) and resynchronize. no cap, that's on you.
๐ฆ Address formats ๐ฆ
the sockaddr_nl structure describes a netlink client in user space or in the kernel. and a sockaddr_nl can be either:
- unicast โ only sent to one peer, or
- sent to netlink multicast groups โ when
nl_groupsis not equal to 0.
struct sockaddr_nl {
sa_family_t nl_family; /* AF_NETLINK */
unsigned short nl_pad; /* Zero */
pid_t nl_pid; /* Port ID */
__u32 nl_groups; /* Multicast groups mask */
};
nl_pid is the unicast address of the netlink socket. it's always 0 if the destination is in the kernel. for a user-space process, nl_pid is usually the PID of the process owning the destination socket โ but (and this is the plot twist) nl_pid identifies a netlink socket, not a process. so if a process owns several netlink sockets, then nl_pid can equal the process ID for at most one socket. main character energy for exactly one socket only ๐
there are two ways to assign nl_pid to a netlink socket:
- if the application sets
nl_pidbefore callingbind(2), then it is up to the application to make sure thatnl_pidis unique. that's on you, bestie. - if the application sets it to
0, then the kernel takes care of assigning it. the kernel assigns the process ID to the first netlink socket the process opens, and assigns a uniquenl_pidto every netlink socket that the process subsequently creates. the kernel is genuinely so thoughtful for that one ๐ธ
okay so nl_groups? it's literally a bit mask where every single bit is a netlink group number. each netlink family has like a set of 32 multicast groups. when you call bind(2) on the socket, the nl_groups field in sockaddr_nl should be set to a bit mask of the groups you wanna listen to. default value is zero, which means no multicasts will be received, periodt. ๐
but bestie, you can also multicast messages to any of the multicast groups by setting nl_groups to a bit mask of the groups you wanna send to when you call sendmsg(2) or do a connect(2). ๐
ngl, only processes with an effective UID of 0 or the CAP_NET_ADMIN capability may send or listen to a netlink multicast group. and since Linux 2.6.13, messages can't be broadcast to multiple groups โ that's just the way it is. any replies to a message received for a multicast group should be sent back to the sending PID and the multicast group. some Linux kernel subsystems may additionally allow other users to send and/or receive messages. as at Linux 3.0, the NETLINK_KOBJECT_UEVENT, NETLINK_GENERIC, NETLINK_ROUTE, and NETLINK_SELINUX groups allow other users to receive messages. no groups allow other users to send messages. ๐
๐งฆ Socket options ๐งฆ
to set or get a netlink socket option, call getsockopt(2) to read or setsockopt(2) to write the option with the option level argument set to SOL_NETLINK. unless otherwise noted, optval is a pointer to an int. so basically, you're passing a pointer to an int. easy. ๐
NETLINK_PKTINFO(since Linux 2.6.14) โ enablenl_pktinfocontrol messages for received packets to get the extended destination group number.NETLINK_ADD_MEMBERSHIP,NETLINK_DROP_MEMBERSHIP(since Linux 2.6.14) โ join/leave a group specified byoptval.NETLINK_LIST_MEMBERSHIPS(since Linux 4.2) โ retrieve all groups a socket is a member of.optvalis a pointer to__u32andoptlenis the size of the array. the array is filled with the full membership set of the socket, and the required array size is returned inoptlen.NETLINK_BROADCAST_ERROR(since Linux 2.6.30) โ when not set,netlink_broadcast()only reportsESRCHerrors and silently ignoreENOBUFSerrors.NETLINK_NO_ENOBUFS(since Linux 2.6.30) โ this flag can be used by unicast and broadcast listeners to avoid receivingENOBUFSerrors.NETLINK_LISTEN_ALL_NSID(since Linux 4.2) โ when set, this socket will receive netlink notifications from all network namespaces that have an nsid assigned into the network namespace where the socket has been opened. the nsid is sent to user space via an ancillary data.NETLINK_CAP_ACK(since Linux 4.3) โ the kernel may fail to allocate the necessary room for the acknowledgement message back to user space. this option trims off the payload of the original netlink message. the netlink message header is still included, so the user can guess from the sequence number which message triggered the acknowledgement.
โจ VERSIONS โจ
the socket interface to netlink first appeared Linux 2.2. Linux 2.0 supported a more primitive device-based netlink interface (which is still available as a compatibility option). this obsolete interface is not described here. bye. ๐
๐ NOTES ๐
it's often better to use netlink via libnetlink or libnl than via the low-level kernel interface. iykyk. ๐
๐ BUGS ๐
this manual page is not complete. ๐ญ (not me leaving y'all hanging but it's true.)
๐ธ EXAMPLES ๐ธ
the following example creates a NETLINK_ROUTE netlink socket which will listen to the RTMGRP_LINK (network interface create/delete/up/down events) and RTMGRP_IPV4_IFADDR (IPv4 addresses add/delete events) multicast groups.
struct sockaddr_nl sa;
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
bind(fd, (struct sockaddr *) &sa, sizeof(sa));
the next example demonstrates how to send a netlink message to the kernel (pid 0). note that the application must take care of message sequence numbers in order to reliably track acknowledgements.
struct nlmsghdr *nh; /* The nlmsghdr with payload to send */
struct sockaddr_nl sa;
struct iovec iov = { nh, nh->nlmsg_len };
struct msghdr msg;
msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
nh->nlmsg_pid = 0;
nh->nlmsg_seq = ++sequence_number;
/* Request an ack from kernel by setting NLM_F_ACK */
nh->nlmsg_flags |= NLM_F_ACK;
sendmsg(fd, &msg, 0);
and the last example is about reading netlink message.
int size;
/* 8192 to avoid message truncation on platforms with
page size > 4096 */
struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)];
struct iovec iov = { buf, sizeof(buf) };
struct sockaddr_nl sa;
struct msghdr msg;
struct nlmsghdr *nh;
msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
size = recvmsg(fd, &msg, 0);
for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, size);
nh = NLMSG_NEXT (nh, size)) {
/* The end of multipart message */
if (nh->nlmsg_type == NLMSG_DONE)
return;
if (nh->nlmsg_type == NLMSG_ERROR)
/* Do some error handling */
...
/* Continue with parsing payload */
...
}
๐ SEE ALSO ๐
cmsg(3),netlink(3),capabilities(7),rtnetlink(7),sock_diag(7)- information about
libnetlink - information about
libnl - RFC 3549 "Linux Netlink as an IP Services Protocol"
and that's the tea, bestie! this page is from Linux man-pages 6.20, dated 2026-02-08, for netlink(7). stay slay. ๐
โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: GPL-1.0-or-later
- Copyright 1998, Andi Kleen <andi@firstfloor.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms