1 .\" This man page is Copyright (c) 1998 by Andi Kleen.
3 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
4 .\" Subject to the GPL.
7 .\" Based on the original comments from Alexey Kuznetsov
8 .\" Modified 2005-12-27 by Hasso Tepper <hasso@estpak.ee>
9 .\" $Id: netlink.7,v 1.8 2000/06/22 13:23:00 ak Exp $
10 .TH NETLINK 7 2021-03-22 "Linux" "Linux Programmer's Manual"
12 netlink \- communication between kernel and user space (AF_NETLINK)
15 .B #include <asm/types.h>
16 .B #include <sys/socket.h>
17 .B #include <linux/netlink.h>
19 .BI "netlink_socket = socket(AF_NETLINK, " socket_type ", " netlink_family );
22 Netlink is used to transfer information between the kernel and
24 It consists of a standard sockets-based interface for user space
25 processes and an internal kernel API for kernel modules.
26 The internal kernel interface is not documented in this manual page.
27 There is also an obsolete netlink interface
28 via netlink character devices; this interface is not documented here
29 and is provided only for backward compatibility.
31 Netlink is a datagram-oriented service.
38 However, the netlink protocol does not distinguish between datagram
42 selects the kernel module or netlink group to communicate with.
43 The currently assigned netlink families are:
46 Receives routing and link updates and may be used to modify the routing
47 tables (both IPv4 and IPv6), IP addresses, link parameters,
48 neighbor setups, queueing disciplines, traffic classes, and
49 packet classifiers (see
52 .BR NETLINK_W1 " (Linux 2.6.13 to 2.16.17)"
53 Messages from 1-wire subsystem.
56 Reserved for user-mode socket protocols.
58 .BR NETLINK_FIREWALL " (up to and including Linux 3.4)"
59 .\" removed by commit d16cf20e2f2f13411eece7f7fb72c17d141c4a84
60 Transport IPv4 packets from netfilter to user space.
64 After a long period of being declared obsolete (in favor of the more advanced
68 was removed in Linux 3.5.
70 .BR NETLINK_SOCK_DIAG " (since Linux 3.3)"
71 .\" commit 7f1fb60c4fc9fb29fbb406ac8c4cfb4e59e168d6
72 Query information about sockets of various protocol families from the kernel
76 .BR NETLINK_INET_DIAG " (since Linux 2.6.14)"
77 An obsolete synonym for
78 .BR NETLINK_SOCK_DIAG .
80 .BR NETLINK_NFLOG " (up to and including Linux 3.16)"
81 Netfilter/iptables ULOG.
84 .\" FIXME More details on NETLINK_XFRM needed.
87 .BR NETLINK_SELINUX " (since Linux 2.6.4)"
88 SELinux event notifications.
90 .BR NETLINK_ISCSI " (since Linux 2.6.15)"
91 .\" FIXME More details on NETLINK_ISCSI needed.
94 .BR NETLINK_AUDIT " (since Linux 2.6.6)"
95 .\" FIXME More details on NETLINK_AUDIT needed.
98 .BR NETLINK_FIB_LOOKUP " (since Linux 2.6.13)"
99 .\" FIXME More details on NETLINK_FIB_LOOKUP needed.
100 Access to FIB lookup from user space.
102 .BR NETLINK_CONNECTOR " (since Linux 2.6.14)"
105 .I Documentation/driver\-api/connector.rst
107 .IR /Documentation/connector/connector.*
108 .\" commit baa293e9544bea71361950d071579f0e4d5713ed
109 in kernel 5.2 and earlier)
110 in the Linux kernel source tree for further information.
112 .BR NETLINK_NETFILTER " (since Linux 2.6.14)"
113 .\" FIXME More details on NETLINK_NETFILTER needed.
116 .BR NETLINK_SCSITRANSPORT " (since Linux 2.6.19)"
117 .\" commit 84314fd4740ad73550c76dee4a9578979d84af48
118 .\" FIXME More details on NETLINK_SCSITRANSPORT needed.
121 .BR NETLINK_RDMA " (since Linux 3.0)"
122 .\" commit b2cbae2c248776d81cc265ff7d48405b6a4cc463
123 .\" FIXME More details on NETLINK_RDMA needed.
126 .BR NETLINK_IP6_FW " (up to and including Linux 3.4)"
127 Transport IPv6 packets from netfilter to user space.
133 DECnet routing messages.
135 .BR NETLINK_KOBJECT_UEVENT " (since Linux 2.6.10)"
136 .\" FIXME More details on NETLINK_KOBJECT_UEVENT needed.
137 Kernel messages to user space.
139 .BR NETLINK_GENERIC " (since Linux 2.6.15)"
140 Generic netlink family for simplified netlink usage.
142 .BR NETLINK_CRYPTO " (since Linux 3.2)"
143 .\" commit a38f7907b926e4c6c7d389ad96cc38cec2e5a9e9
144 .\" Author: Steffen Klassert <steffen.klassert@secunet.com>
145 Netlink interface to request information about ciphers registered
146 with the kernel crypto API as well as allow configuration of the
149 Netlink messages consist of a byte stream with one or multiple
151 headers and associated payload.
152 The byte stream should be accessed only with the standard
157 for further information.
159 In multipart messages (multiple
161 headers with associated payload in one byte stream) the first and all
162 following headers have the
164 flag set, except for the last header which has the type
174 __u32 nlmsg_len; /* Length of message including header */
175 __u16 nlmsg_type; /* Type of message content */
176 __u16 nlmsg_flags; /* Additional flags */
177 __u32 nlmsg_seq; /* Sequence number */
178 __u32 nlmsg_pid; /* Sender port ID */
184 can be one of the standard message types:
186 message is to be ignored,
188 message signals an error and the payload contains an
192 message terminates a multipart message.
193 Error messages get the
194 original request appened, unless the user requests to cap the
195 error message, and get extra error data if requested.
200 int error; /* Negative errno or 0 for acknowledgements */
201 struct nlmsghdr msg; /* Message header that caused the error */
203 * followed by the message contents unless NETLINK_CAP_ACK was set
204 * or the ACK indicates success (error == 0).
205 * For example Generic Netlink message with attributes.
206 * message length is aligned with NLMSG_ALIGN()
209 * followed by TLVs defined in enum nlmsgerr_attrs
210 * if NETLINK_EXT_ACK was set
216 A netlink family usually specifies more message types, see the
217 appropriate manual pages for that, for example,
227 Standard flag bits in \fInlmsg_flags\fP
230 Must be set on all request messages.
233 The message is part of a multipart message terminated by
237 Request for an acknowledgement on success.
245 .\" No right adjustment for text blocks in tables
252 Additional flag bits for GET requests
255 Return the complete table instead of a single entry.
258 Return all entries matching criteria passed in message content.
262 Return an atomic snapshot of the table.
265 Convenience macro; equivalent to
266 (NLM_F_ROOT|NLM_F_MATCH).
271 .\" FIXME NLM_F_ATOMIC is not used anymore?
277 capability or an effective UID of 0.
284 Additional flag bits for NEW requests
287 Replace existing matching object.
290 Don't replace if the object already exists.
293 Create object if it doesn't already exist.
296 Add to the end of the object list.
305 are used to track messages.
307 shows the origin of the message.
308 Note that there isn't a 1:1 relationship between
310 and the PID of the process if the message originated from a netlink
314 section for further information.
320 .\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
321 are opaque to netlink core.
323 Netlink is not a reliable protocol.
324 It tries its best to deliver a message to its destination(s),
325 but may drop messages when an out-of-memory condition or
327 For reliable transfer the sender can request an
328 acknowledgement from the receiver by setting the
331 An acknowledgement is an
333 packet with the error field set to 0.
334 The application must generate acknowledgements for
335 received messages itself.
336 The kernel tries to send an
338 message for every failed packet.
339 A user process should follow this convention too.
341 However, reliable transmissions from kernel to user are impossible
343 The kernel can't send a netlink message if the socket buffer is full:
344 the message will be dropped and the kernel and the user-space process will
345 no longer have the same view of kernel state.
346 It is up to the application to detect when this happens (via the
354 structure describes a netlink client in user space or in the kernel.
357 can be either unicast (only sent to one peer) or sent to
358 netlink multicast groups
365 sa_family_t nl_family; /* AF_NETLINK */
366 unsigned short nl_pad; /* Zero */
367 pid_t nl_pid; /* Port ID */
368 __u32 nl_groups; /* Multicast groups mask */
374 is the unicast address of netlink socket.
375 It's always 0 if the destination is in the kernel.
376 For a user-space process,
378 is usually the PID of the process owning the destination socket.
381 identifies a netlink socket, not a process.
382 If a process owns several netlink
385 can be equal to the process ID only for at most one socket.
386 There are two ways to assign
389 If the application sets
393 then it is up to the application to make sure that
396 If the application sets it to 0, the kernel takes care of assigning it.
397 The kernel assigns the process ID to the first netlink socket the process
398 opens and assigns a unique
400 to every netlink socket that the process subsequently creates.
403 is a bit mask with every bit representing a netlink group number.
404 Each netlink family has a set of 32 multicast groups.
407 is called on the socket, the
411 should be set to a bit mask of the groups which it wishes to listen to.
412 The default value for this field is zero which means that no multicasts
414 A socket may multicast messages to any of the multicast groups by setting
416 to a bit mask of the groups it wishes to send to when it calls
420 Only processes with an effective UID of 0 or the
422 capability may send or listen to a netlink multicast group.
424 .\" commit d629b836d151d43332492651dd841d32e57ebe3b
425 messages can't be broadcast to multiple groups.
426 Any replies to a message received for a multicast group should be
427 sent back to the sending PID and the multicast group.
428 Some Linux kernel subsystems may additionally allow other users
429 to send and/or receive messages.
431 .BR NETLINK_KOBJECT_UEVENT ,
432 .BR NETLINK_GENERIC ,
436 groups allow other users to receive messages.
437 No groups allow other users to send messages.
439 To set or get a netlink socket option, call
443 to write the option with the option level argument set to
445 Unless otherwise noted,
450 .BR NETLINK_PKTINFO " (since Linux 2.6.14)"
451 .\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
452 .\" Author: Patrick McHardy <kaber@trash.net>
455 control messages for received packets to get the extended
456 destination group number.
458 .BR NETLINK_ADD_MEMBERSHIP ,\ NETLINK_DROP_MEMBERSHIP " (since Linux 2.6.14)"
459 .\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
460 .\" Author: Patrick McHardy <kaber@trash.net>
461 Join/leave a group specified by
464 .BR NETLINK_LIST_MEMBERSHIPS " (since Linux 4.2)"
465 .\" commit b42be38b2778eda2237fc759e55e3b698b05b315
466 .\" Author: David Herrmann <dh.herrmann@gmail.com>
467 Retrieve all groups a socket is a member of.
473 is the size of the array.
474 The array is filled with the full membership set of the
475 socket, and the required array size is returned in
478 .BR NETLINK_BROADCAST_ERROR " (since Linux 2.6.30)"
479 .\" commit be0c22a46cfb79ab2342bb28fde99afa94ef868e
480 .\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
482 .B netlink_broadcast()
485 errors and silently ignore
489 .BR NETLINK_NO_ENOBUFS " (since Linux 2.6.30)"
490 .\" commit 38938bfe3489394e2eed5e40c9bb8f66a2ce1405
491 .\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
492 This flag can be used by unicast and broadcast listeners to avoid receiving
496 .BR NETLINK_LISTEN_ALL_NSID " (since Linux 4.2)"
497 .\" commit 59324cf35aba5336b611074028777838a963d03b
498 .\" Author: Nicolas Dichtel <nicolas.dichtel@6wind.com>
499 When set, this socket will receive netlink notifications from
500 all network namespaces that have an
502 assigned into the network namespace where the socket has been opened.
505 is sent to user space via an ancillary data.
507 .BR NETLINK_CAP_ACK " (since Linux 4.3)"
508 .\" commit 0a6a3a23ea6efde079a5b77688541a98bf202721
509 .\" Author: Christophe Ricard <christophe.ricard@gmail.com>
510 The kernel may fail to allocate the necessary room for the acknowledgement
511 message back to user space.
512 This option trims off the payload of the original netlink message.
513 The netlink message header is still included, so the user can guess from the
514 sequence number which message triggered the acknowledgement.
516 The socket interface to netlink first appeared Linux 2.2.
518 Linux 2.0 supported a more primitive device-based netlink interface
519 (which is still available as a compatibility option).
520 This obsolete interface is not described here.
522 It is often better to use netlink via
526 than via the low-level kernel interface.
528 This manual page is not complete.
530 The following example creates a
532 netlink socket which will listen to the
534 (network interface create/delete/up/down events) and
535 .B RTMGRP_IPV4_IFADDR
536 (IPv4 addresses add/delete events) multicast groups.
540 struct sockaddr_nl sa;
542 memset(&sa, 0, sizeof(sa));
543 sa.nl_family = AF_NETLINK;
544 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
546 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
547 bind(fd, (struct sockaddr *) &sa, sizeof(sa));
551 The next example demonstrates how to send a netlink message to the
553 Note that the application must take care of message sequence numbers
554 in order to reliably track acknowledgements.
558 struct nlmsghdr *nh; /* The nlmsghdr with payload to send */
559 struct sockaddr_nl sa;
560 struct iovec iov = { nh, nh\->nlmsg_len };
563 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
564 memset(&sa, 0, sizeof(sa));
565 sa.nl_family = AF_NETLINK;
567 nh\->nlmsg_seq = ++sequence_number;
568 /* Request an ack from kernel by setting NLM_F_ACK */
569 nh\->nlmsg_flags |= NLM_F_ACK;
571 sendmsg(fd, &msg, 0);
575 And the last example is about reading netlink message.
580 /* 8192 to avoid message truncation on platforms with
582 struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)];
583 struct iovec iov = { buf, sizeof(buf) };
584 struct sockaddr_nl sa;
588 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
589 len = recvmsg(fd, &msg, 0);
591 for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
592 nh = NLMSG_NEXT (nh, len)) {
593 /* The end of multipart message */
594 if (nh\->nlmsg_type == NLMSG_DONE)
597 if (nh\->nlmsg_type == NLMSG_ERROR)
598 /* Do some error handling */
601 /* Continue with parsing payload */
609 .BR capabilities (7),
613 .UR ftp://ftp.inr.ac.ru\:/ip\-routing\:/iproute2*
614 information about libnetlink
617 .UR http://www.infradead.org\:/\(titgr\:/libnl/
618 information about libnl
621 RFC 3549 "Linux Netlink as an IP Services Protocol"