1 .\" This man page is Copyright (c) 1998 by Andi Kleen.
3 .\" SPDX-License-Identifier: GPL-1.0-or-later
5 .\" Based on the original comments from Alexey Kuznetsov
6 .\" Modified 2005-12-27 by Hasso Tepper <hasso@estpak.ee>
7 .\" $Id: netlink.7,v 1.8 2000/06/22 13:23:00 ak Exp $
8 .TH NETLINK 7 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
10 netlink \- communication between kernel and user space (AF_NETLINK)
13 .B #include <asm/types.h>
14 .B #include <sys/socket.h>
15 .B #include <linux/netlink.h>
17 .BI "netlink_socket = socket(AF_NETLINK, " socket_type ", " netlink_family );
20 Netlink is used to transfer information between the kernel and
22 It consists of a standard sockets-based interface for user space
23 processes and an internal kernel API for kernel modules.
24 The internal kernel interface is not documented in this manual page.
25 There is also an obsolete netlink interface
26 via netlink character devices; this interface is not documented here
27 and is provided only for backward compatibility.
29 Netlink is a datagram-oriented service.
36 However, the netlink protocol does not distinguish between datagram
40 selects the kernel module or netlink group to communicate with.
41 The currently assigned netlink families are:
44 Receives routing and link updates and may be used to modify the routing
45 tables (both IPv4 and IPv6), IP addresses, link parameters,
46 neighbor setups, queueing disciplines, traffic classes, and
47 packet classifiers (see
50 .BR NETLINK_W1 " (Linux 2.6.13 to 2.16.17)"
51 Messages from 1-wire subsystem.
54 Reserved for user-mode socket protocols.
56 .BR NETLINK_FIREWALL " (up to and including Linux 3.4)"
57 .\" removed by commit d16cf20e2f2f13411eece7f7fb72c17d141c4a84
58 Transport IPv4 packets from netfilter to user space.
62 After a long period of being declared obsolete (in favor of the more advanced
66 was removed in Linux 3.5.
68 .BR NETLINK_SOCK_DIAG " (since Linux 3.3)"
69 .\" commit 7f1fb60c4fc9fb29fbb406ac8c4cfb4e59e168d6
70 Query information about sockets of various protocol families from the kernel
74 .BR NETLINK_INET_DIAG " (since Linux 2.6.14)"
75 An obsolete synonym for
76 .BR NETLINK_SOCK_DIAG .
78 .BR NETLINK_NFLOG " (up to and including Linux 3.16)"
79 Netfilter/iptables ULOG.
82 .\" FIXME More details on NETLINK_XFRM needed.
85 .BR NETLINK_SELINUX " (since Linux 2.6.4)"
86 SELinux event notifications.
88 .BR NETLINK_ISCSI " (since Linux 2.6.15)"
89 .\" FIXME More details on NETLINK_ISCSI needed.
92 .BR NETLINK_AUDIT " (since Linux 2.6.6)"
93 .\" FIXME More details on NETLINK_AUDIT needed.
96 .BR NETLINK_FIB_LOOKUP " (since Linux 2.6.13)"
97 .\" FIXME More details on NETLINK_FIB_LOOKUP needed.
98 Access to FIB lookup from user space.
100 .BR NETLINK_CONNECTOR " (since Linux 2.6.14)"
103 .I Documentation/driver\-api/connector.rst
105 .I /Documentation/connector/connector.*
106 .\" commit baa293e9544bea71361950d071579f0e4d5713ed
107 in kernel 5.2 and earlier)
108 in the Linux kernel source tree for further information.
110 .BR NETLINK_NETFILTER " (since Linux 2.6.14)"
111 .\" FIXME More details on NETLINK_NETFILTER needed.
114 .BR NETLINK_SCSITRANSPORT " (since Linux 2.6.19)"
115 .\" commit 84314fd4740ad73550c76dee4a9578979d84af48
116 .\" FIXME More details on NETLINK_SCSITRANSPORT needed.
119 .BR NETLINK_RDMA " (since Linux 3.0)"
120 .\" commit b2cbae2c248776d81cc265ff7d48405b6a4cc463
121 .\" FIXME More details on NETLINK_RDMA needed.
124 .BR NETLINK_IP6_FW " (up to and including Linux 3.4)"
125 Transport IPv6 packets from netfilter to user space.
131 DECnet routing messages.
133 .BR NETLINK_KOBJECT_UEVENT " (since Linux 2.6.10)"
134 .\" FIXME More details on NETLINK_KOBJECT_UEVENT needed.
135 Kernel messages to user space.
137 .BR NETLINK_GENERIC " (since Linux 2.6.15)"
138 Generic netlink family for simplified netlink usage.
140 .BR NETLINK_CRYPTO " (since Linux 3.2)"
141 .\" commit a38f7907b926e4c6c7d389ad96cc38cec2e5a9e9
142 .\" Author: Steffen Klassert <steffen.klassert@secunet.com>
143 Netlink interface to request information about ciphers registered
144 with the kernel crypto API as well as allow configuration of the
147 Netlink messages consist of a byte stream with one or multiple
149 headers and associated payload.
150 The byte stream should be accessed only with the standard
155 for further information.
157 In multipart messages (multiple
159 headers with associated payload in one byte stream) the first and all
160 following headers have the
162 flag set, except for the last header which has the type
172 __u32 nlmsg_len; /* Length of message including header */
173 __u16 nlmsg_type; /* Type of message content */
174 __u16 nlmsg_flags; /* Additional flags */
175 __u32 nlmsg_seq; /* Sequence number */
176 __u32 nlmsg_pid; /* Sender port ID */
182 can be one of the standard message types:
184 message is to be ignored,
186 message signals an error and the payload contains an
190 message terminates a multipart message.
191 Error messages get the
192 original request appended, unless the user requests to cap the
193 error message, and get extra error data if requested.
198 int error; /* Negative errno or 0 for acknowledgements */
199 struct nlmsghdr msg; /* Message header that caused the error */
201 * followed by the message contents unless NETLINK_CAP_ACK was set
202 * or the ACK indicates success (error == 0).
203 * For example Generic Netlink message with attributes.
204 * message length is aligned with NLMSG_ALIGN()
207 * followed by TLVs defined in enum nlmsgerr_attrs
208 * if NETLINK_EXT_ACK was set
214 A netlink family usually specifies more message types, see the
215 appropriate manual pages for that, for example,
225 Standard flag bits in \fInlmsg_flags\fP
228 Must be set on all request messages.
231 The message is part of a multipart message terminated by
235 Request for an acknowledgement on success.
243 .\" No right adjustment for text blocks in tables
250 Additional flag bits for GET requests
253 Return the complete table instead of a single entry.
256 Return all entries matching criteria passed in message content.
260 Return an atomic snapshot of the table.
263 Convenience macro; equivalent to
264 (NLM_F_ROOT|NLM_F_MATCH).
269 .\" FIXME NLM_F_ATOMIC is not used anymore?
275 capability or an effective UID of 0.
282 Additional flag bits for NEW requests
285 Replace existing matching object.
288 Don't replace if the object already exists.
291 Create object if it doesn't already exist.
294 Add to the end of the object list.
303 are used to track messages.
305 shows the origin of the message.
306 Note that there isn't a 1:1 relationship between
308 and the PID of the process if the message originated from a netlink
312 section for further information.
318 .\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
319 are opaque to netlink core.
321 Netlink is not a reliable protocol.
322 It tries its best to deliver a message to its destination(s),
323 but may drop messages when an out-of-memory condition or
325 For reliable transfer the sender can request an
326 acknowledgement from the receiver by setting the
329 An acknowledgement is an
331 packet with the error field set to 0.
332 The application must generate acknowledgements for
333 received messages itself.
334 The kernel tries to send an
336 message for every failed packet.
337 A user process should follow this convention too.
339 However, reliable transmissions from kernel to user are impossible
341 The kernel can't send a netlink message if the socket buffer is full:
342 the message will be dropped and the kernel and the user-space process will
343 no longer have the same view of kernel state.
344 It is up to the application to detect when this happens (via the
352 structure describes a netlink client in user space or in the kernel.
355 can be either unicast (only sent to one peer) or sent to
356 netlink multicast groups
363 sa_family_t nl_family; /* AF_NETLINK */
364 unsigned short nl_pad; /* Zero */
365 pid_t nl_pid; /* Port ID */
366 __u32 nl_groups; /* Multicast groups mask */
372 is the unicast address of netlink socket.
373 It's always 0 if the destination is in the kernel.
374 For a user-space process,
376 is usually the PID of the process owning the destination socket.
379 identifies a netlink socket, not a process.
380 If a process owns several netlink
383 can be equal to the process ID only for at most one socket.
384 There are two ways to assign
387 If the application sets
391 then it is up to the application to make sure that
394 If the application sets it to 0, the kernel takes care of assigning it.
395 The kernel assigns the process ID to the first netlink socket the process
396 opens and assigns a unique
398 to every netlink socket that the process subsequently creates.
401 is a bit mask with every bit representing a netlink group number.
402 Each netlink family has a set of 32 multicast groups.
405 is called on the socket, the
409 should be set to a bit mask of the groups which it wishes to listen to.
410 The default value for this field is zero which means that no multicasts
412 A socket may multicast messages to any of the multicast groups by setting
414 to a bit mask of the groups it wishes to send to when it calls
418 Only processes with an effective UID of 0 or the
420 capability may send or listen to a netlink multicast group.
422 .\" commit d629b836d151d43332492651dd841d32e57ebe3b
423 messages can't be broadcast to multiple groups.
424 Any replies to a message received for a multicast group should be
425 sent back to the sending PID and the multicast group.
426 Some Linux kernel subsystems may additionally allow other users
427 to send and/or receive messages.
429 .BR NETLINK_KOBJECT_UEVENT ,
430 .BR NETLINK_GENERIC ,
434 groups allow other users to receive messages.
435 No groups allow other users to send messages.
437 To set or get a netlink socket option, call
441 to write the option with the option level argument set to
443 Unless otherwise noted,
448 .BR NETLINK_PKTINFO " (since Linux 2.6.14)"
449 .\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
450 .\" Author: Patrick McHardy <kaber@trash.net>
453 control messages for received packets to get the extended
454 destination group number.
456 .BR NETLINK_ADD_MEMBERSHIP ,\ NETLINK_DROP_MEMBERSHIP " (since Linux 2.6.14)"
457 .\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
458 .\" Author: Patrick McHardy <kaber@trash.net>
459 Join/leave a group specified by
462 .BR NETLINK_LIST_MEMBERSHIPS " (since Linux 4.2)"
463 .\" commit b42be38b2778eda2237fc759e55e3b698b05b315
464 .\" Author: David Herrmann <dh.herrmann@gmail.com>
465 Retrieve all groups a socket is a member of.
471 is the size of the array.
472 The array is filled with the full membership set of the
473 socket, and the required array size is returned in
476 .BR NETLINK_BROADCAST_ERROR " (since Linux 2.6.30)"
477 .\" commit be0c22a46cfb79ab2342bb28fde99afa94ef868e
478 .\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
480 .B netlink_broadcast()
483 errors and silently ignore
487 .BR NETLINK_NO_ENOBUFS " (since Linux 2.6.30)"
488 .\" commit 38938bfe3489394e2eed5e40c9bb8f66a2ce1405
489 .\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
490 This flag can be used by unicast and broadcast listeners to avoid receiving
494 .BR NETLINK_LISTEN_ALL_NSID " (since Linux 4.2)"
495 .\" commit 59324cf35aba5336b611074028777838a963d03b
496 .\" Author: Nicolas Dichtel <nicolas.dichtel@6wind.com>
497 When set, this socket will receive netlink notifications from
498 all network namespaces that have an
500 assigned into the network namespace where the socket has been opened.
503 is sent to user space via an ancillary data.
505 .BR NETLINK_CAP_ACK " (since Linux 4.3)"
506 .\" commit 0a6a3a23ea6efde079a5b77688541a98bf202721
507 .\" Author: Christophe Ricard <christophe.ricard@gmail.com>
508 The kernel may fail to allocate the necessary room for the acknowledgement
509 message back to user space.
510 This option trims off the payload of the original netlink message.
511 The netlink message header is still included, so the user can guess from the
512 sequence number which message triggered the acknowledgement.
514 The socket interface to netlink first appeared Linux 2.2.
516 Linux 2.0 supported a more primitive device-based netlink interface
517 (which is still available as a compatibility option).
518 This obsolete interface is not described here.
520 It is often better to use netlink via
524 than via the low-level kernel interface.
526 This manual page is not complete.
528 The following example creates a
530 netlink socket which will listen to the
532 (network interface create/delete/up/down events) and
533 .B RTMGRP_IPV4_IFADDR
534 (IPv4 addresses add/delete events) multicast groups.
538 struct sockaddr_nl sa;
540 memset(&sa, 0, sizeof(sa));
541 sa.nl_family = AF_NETLINK;
542 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
544 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
545 bind(fd, (struct sockaddr *) &sa, sizeof(sa));
549 The next example demonstrates how to send a netlink message to the
551 Note that the application must take care of message sequence numbers
552 in order to reliably track acknowledgements.
556 struct nlmsghdr *nh; /* The nlmsghdr with payload to send */
557 struct sockaddr_nl sa;
558 struct iovec iov = { nh, nh\->nlmsg_len };
561 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
562 memset(&sa, 0, sizeof(sa));
563 sa.nl_family = AF_NETLINK;
565 nh\->nlmsg_seq = ++sequence_number;
566 /* Request an ack from kernel by setting NLM_F_ACK */
567 nh\->nlmsg_flags |= NLM_F_ACK;
569 sendmsg(fd, &msg, 0);
573 And the last example is about reading netlink message.
578 /* 8192 to avoid message truncation on platforms with
580 struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)];
581 struct iovec iov = { buf, sizeof(buf) };
582 struct sockaddr_nl sa;
586 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
587 len = recvmsg(fd, &msg, 0);
589 for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
590 nh = NLMSG_NEXT (nh, len)) {
591 /* The end of multipart message */
592 if (nh\->nlmsg_type == NLMSG_DONE)
595 if (nh\->nlmsg_type == NLMSG_ERROR)
596 /* Do some error handling */
599 /* Continue with parsing payload */
607 .BR capabilities (7),
611 .UR ftp://ftp.inr.ac.ru\:/ip\-routing\:/iproute2*
612 information about libnetlink
615 .UR http://www.infradead.org\:/\(titgr\:/libnl/
616 information about libnl
619 RFC 3549 "Linux Netlink as an IP Services Protocol"