2 .\" This man page is Copyright (c) 1998 by Andi Kleen.
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" Subject to the GPL.
8 .\" Based on the original comments from Alexey Kuznetsov
9 .\" Modified 2005-12-27 by Hasso Tepper <hasso@estpak.ee>
10 .\" $Id: netlink.7,v 1.8 2000/06/22 13:23:00 ak Exp $
11 .TH NETLINK 7 2016-12-12 "Linux" "Linux Programmer's Manual"
13 netlink \- communication between kernel and user space (AF_NETLINK)
16 .B #include <asm/types.h>
17 .B #include <sys/socket.h>
18 .B #include <linux/netlink.h>
20 .BI "netlink_socket = socket(AF_NETLINK, " socket_type ", " netlink_family );
23 Netlink is used to transfer information between the kernel and
25 It consists of a standard sockets-based interface for user space
26 processes and an internal kernel API for kernel modules.
27 The internal kernel interface is not documented in this manual page.
28 There is also an obsolete netlink interface
29 via netlink character devices; this interface is not documented here
30 and is provided only for backward compatibility.
32 Netlink is a datagram-oriented service.
39 However, the netlink protocol does not distinguish between datagram
43 selects the kernel module or netlink group to communicate with.
44 The currently assigned netlink families are:
47 Receives routing and link updates and may be used to modify the routing
48 tables (both IPv4 and IPv6), IP addresses, link parameters,
49 neighbor setups, queueing disciplines, traffic classes and
50 packet classifiers (see
53 .BR NETLINK_W1 " (since Linux 2.6.13)"
54 Messages from 1-wire subsystem.
57 Reserved for user-mode socket protocols.
60 Transport IPv4 packets from netfilter to user space.
65 .BR NETLINK_INET_DIAG " (since Linux 2.6.14)"
66 Query information about sockets of various protocol families from the kernel
70 .BR NETLINK_SOCK_DIAG " (since Linux 3.3)"
71 .\" commit 7f1fb60c4fc9fb29fbb406ac8c4cfb4e59e168d6
73 .BR NETLINK_INET_DIAG .
76 Netfilter/iptables ULOG.
79 .\" FIXME More details on NETLINK_XFRM needed.
82 .BR NETLINK_SELINUX " (since Linux 2.6.4)"
83 SELinux event notifications.
85 .BR NETLINK_ISCSI " (since Linux 2.6.15)"
86 .\" FIXME More details on NETLINK_ISCSI needed.
89 .BR NETLINK_AUDIT " (since Linux 2.6.6)"
90 .\" FIXME More details on NETLINK_AUDIT needed.
93 .BR NETLINK_FIB_LOOKUP " (since Linux 2.6.13)"
94 .\" FIXME More details on NETLINK_FIB_LOOKUP needed.
95 Access to FIB lookup from user space.
97 .BR NETLINK_CONNECTOR " (since Linux 2.6.14)"
100 .I Documentation/connector/*
101 in the Linux kernel source tree for further information.
103 .BR NETLINK_NETFILTER " (since Linux 2.6.14)"
104 .\" FIXME More details on NETLINK_NETFILTER needed.
108 Transport IPv6 packets from netfilter to user space.
114 DECnet routing messages.
116 .BR NETLINK_KOBJECT_UEVENT " (since Linux 2.6.10)"
117 .\" FIXME More details on NETLINK_KOBJECT_UEVENT needed.
118 Kernel messages to user space.
120 .BR NETLINK_GENERIC " (since Linux 2.6.15)"
121 Generic netlink family for simplified netlink usage.
123 .BR NETLINK_CRYPTO " (since Linux 3.2)"
124 .\" commit a38f7907b926e4c6c7d389ad96cc38cec2e5a9e9
125 .\" Author: Steffen Klassert <steffen.klassert@secunet.com>
126 Netlink interface to request information about ciphers registered
127 with the kernel crypto API as well as allow configuration of the
130 Netlink messages consist of a byte stream with one or multiple
132 headers and associated payload.
133 The byte stream should be accessed only with the standard
138 for further information.
140 In multipart messages (multiple
142 headers with associated payload in one byte stream) the first and all
143 following headers have the
145 flag set, except for the last header which has the type
155 __u32 nlmsg_len; /* Length of message including header */
156 __u16 nlmsg_type; /* Type of message content */
157 __u16 nlmsg_flags; /* Additional flags */
158 __u32 nlmsg_seq; /* Sequence number */
159 __u32 nlmsg_pid; /* Sender port ID */
165 can be one of the standard message types:
167 message is to be ignored,
169 message signals an error and the payload contains an
173 message terminates a multipart message.
178 int error; /* Negative errno or 0 for acknowledgements */
179 struct nlmsghdr msg; /* Message header that caused the error */
184 A netlink family usually specifies more message types, see the
185 appropriate manual pages for that, for example,
193 Standard flag bits in \fInlmsg_flags\fP
195 NLM_F_REQUEST:Must be set on all request messages.
197 The message is part of a multipart message terminated by
200 NLM_F_ACK:Request for an acknowledgment on success.
201 NLM_F_ECHO:Echo this request.
203 .\" No right adjustment for text blocks in tables
208 Additional flag bits for GET requests
210 NLM_F_ROOT:Return the complete table instead of a single entry.
212 Return all entries matching criteria passed in message content.
215 NLM_F_ATOMIC:Return an atomic snapshot of the table.
217 Convenience macro; equivalent to
219 (NLM_F_ROOT|NLM_F_MATCH).
222 .\" FIXME NLM_F_ATOMIC is not used anymore?
228 capability or an effective UID of 0.
233 Additional flag bits for NEW requests
235 NLM_F_REPLACE:Replace existing matching object.
236 NLM_F_EXCL:Don't replace if the object already exists.
237 NLM_F_CREATE:Create object if it doesn't already exist.
238 NLM_F_APPEND:Add to the end of the object list.
244 are used to track messages.
246 shows the origin of the message.
247 Note that there isn't a 1:1 relationship between
249 and the PID of the process if the message originated from a netlink
253 section for further information.
259 .\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
260 are opaque to netlink core.
262 Netlink is not a reliable protocol.
263 It tries its best to deliver a message to its destination(s),
264 but may drop messages when an out-of-memory condition or
266 For reliable transfer the sender can request an
267 acknowledgement from the receiver by setting the
270 An acknowledgment is an
272 packet with the error field set to 0.
273 The application must generate acknowledgements for
274 received messages itself.
275 The kernel tries to send an
277 message for every failed packet.
278 A user process should follow this convention too.
280 However, reliable transmissions from kernel to user are impossible
282 The kernel can't send a netlink message if the socket buffer is full:
283 the message will be dropped and the kernel and the user-space process will
284 no longer have the same view of kernel state.
285 It is up to the application to detect when this happens (via the
293 structure describes a netlink client in user space or in the kernel.
296 can be either unicast (only sent to one peer) or sent to
297 netlink multicast groups
304 sa_family_t nl_family; /* AF_NETLINK */
305 unsigned short nl_pad; /* Zero */
306 pid_t nl_pid; /* Port ID */
307 __u32 nl_groups; /* Multicast groups mask */
313 is the unicast address of netlink socket.
314 It's always 0 if the destination is in the kernel.
315 For a user-space process,
317 is usually the PID of the process owning the destination socket.
320 identifies a netlink socket, not a process.
321 If a process owns several netlink
324 can be equal to the process ID only for at most one socket.
325 There are two ways to assign
328 If the application sets
332 then it is up to the application to make sure that
335 If the application sets it to 0, the kernel takes care of assigning it.
336 The kernel assigns the process ID to the first netlink socket the process
337 opens and assigns a unique
339 to every netlink socket that the process subsequently creates.
342 is a bit mask with every bit representing a netlink group number.
343 Each netlink family has a set of 32 multicast groups.
346 is called on the socket, the
350 should be set to a bit mask of the groups which it wishes to listen to.
351 The default value for this field is zero which means that no multicasts
353 A socket may multicast messages to any of the multicast groups by setting
355 to a bit mask of the groups it wishes to send to when it calls
359 Only processes with an effective UID of 0 or the
361 capability may send or listen to a netlink multicast group.
363 .\" commit d629b836d151d43332492651dd841d32e57ebe3b
364 messages can't be broadcast to multiple groups.
365 Any replies to a message received for a multicast group should be
366 sent back to the sending PID and the multicast group.
367 Some Linux kernel subsystems may additionally allow other users
368 to send and/or receive messages.
370 .BR NETLINK_KOBJECT_UEVENT ,
371 .BR NETLINK_GENERIC ,
375 groups allow other users to receive messages.
376 No groups allow other users to send messages.
379 To set or get a netlink socket option, call
383 to write the option with the option level argument set to
385 Unless otherwise noted,
390 .BR NETLINK_PKTINFO " (since Linux 2.6.14)"
391 .\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
392 .\" Author: Patrick McHardy <kaber@trash.net>
395 control messages for received packets to get the extended
396 destination group number.
398 .BR NETLINK_ADD_MEMBERSHIP ,\ NETLINK_DROP_MEMBERSHIP " (since Linux 2.6.14)"
399 .\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
400 .\" Author: Patrick McHardy <kaber@trash.net>
401 Join/leave a group specified by
404 .BR NETLINK_LIST_MEMBERSHIPS " (since Linux 4.2)"
405 .\" commit b42be38b2778eda2237fc759e55e3b698b05b315
406 .\" Author: David Herrmann <dh.herrmann@gmail.com>
407 Retrieve all groups a socket is a member of.
413 is the size of the array.
414 The array is filled with the full membership set of the
415 socket, and the required array size is returned in
418 .BR NETLINK_BROADCAST_ERROR " (since Linux 2.6.30)"
419 .\" commit be0c22a46cfb79ab2342bb28fde99afa94ef868e
420 .\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
422 .B netlink_broadcast()
425 errors and silently ignore
429 .BR NETLINK_NO_ENOBUFS " (since Linux 2.6.30)"
430 .\" commit 38938bfe3489394e2eed5e40c9bb8f66a2ce1405
431 .\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
432 This flag can be used by unicast and broadcast listeners to avoid receiving
436 .BR NETLINK_LISTEN_ALL_NSID " (since Linux 4.2)"
437 .\" commit 59324cf35aba5336b611074028777838a963d03b
438 .\" Author: Nicolas Dichtel <nicolas.dichtel@6wind.com>
439 When set, this socket will receive netlink notifications from
440 all network namespaces that have an
442 assigned into the network namespace where the socket has been opened.
445 is sent to user space via an ancillary data.
447 .BR NETLINK_CAP_ACK " (since Linux 4.2)"
448 .\" commit 0a6a3a23ea6efde079a5b77688541a98bf202721
449 .\" Author: Christophe Ricard <christophe.ricard@gmail.com>
450 The kernel may fail to allocate the necessary room for the acknowledgment
451 message back to user space.
452 This option trims off the payload of the original netlink message.
453 The netlink message header is still included, so the user can guess from the
454 sequence number which message triggered the acknowledgment.
456 The socket interface to netlink first appeared Linux 2.2.
458 Linux 2.0 supported a more primitive device-based netlink interface
459 (which is still available as a compatibility option).
460 This obsolete interface is not described here.
462 It is often better to use netlink via
466 than via the low-level kernel interface.
468 This manual page is not complete.
470 The following example creates a
472 netlink socket which will listen to the
474 (network interface create/delete/up/down events) and
475 .B RTMGRP_IPV4_IFADDR
476 (IPv4 addresses add/delete events) multicast groups.
480 struct sockaddr_nl sa;
482 memset(&sa, 0, sizeof(sa));
483 sa.nl_family = AF_NETLINK;
484 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
486 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
487 bind(fd, (struct sockaddr *) &sa, sizeof(sa));
491 The next example demonstrates how to send a netlink message to the
493 Note that the application must take care of message sequence numbers
494 in order to reliably track acknowledgements.
498 struct nlmsghdr *nh; /* The nlmsghdr with payload to send */
499 struct sockaddr_nl sa;
500 struct iovec iov = { nh, nh\->nlmsg_len };
503 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
504 memset(&sa, 0, sizeof(sa));
505 sa.nl_family = AF_NETLINK;
507 nh\->nlmsg_seq = ++sequence_number;
508 /* Request an ack from kernel by setting NLM_F_ACK */
509 nh\->nlmsg_flags |= NLM_F_ACK;
511 sendmsg(fd, &msg, 0);
515 And the last example is about reading netlink message.
521 struct iovec iov = { buf, sizeof(buf) };
522 struct sockaddr_nl sa;
526 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
527 len = recvmsg(fd, &msg, 0);
529 for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
530 nh = NLMSG_NEXT (nh, len)) {
531 /* The end of multipart message */
532 if (nh\->nlmsg_type == NLMSG_DONE)
535 if (nh\->nlmsg_type == NLMSG_ERROR)
536 /* Do some error handling */
539 /* Continue with parsing payload */
547 .BR capabilities (7),
551 .UR ftp://ftp.inr.ac.ru\:/ip-routing\:/iproute2*
552 information about libnetlink
555 .UR http://people.suug.ch\:/~tgr\:/libnl/
556 information about libnl
559 RFC 3549 "Linux Netlink as an IP Services Protocol"