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-07-17 "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 .\" FIXME More details on NETLINK_INET_DIAG needed.
67 INET socket monitoring.
70 Netfilter/iptables ULOG.
73 .\" FIXME More details on NETLINK_XFRM needed.
76 .BR NETLINK_SELINUX " (since Linux 2.6.4)"
77 SELinux event notifications.
79 .BR NETLINK_ISCSI " (since Linux 2.6.15)"
80 .\" FIXME More details on NETLINK_ISCSI needed.
83 .BR NETLINK_AUDIT " (since Linux 2.6.6)"
84 .\" FIXME More details on NETLINK_AUDIT needed.
87 .BR NETLINK_FIB_LOOKUP " (since Linux 2.6.13)"
88 .\" FIXME More details on NETLINK_FIB_LOOKUP needed.
89 Access to FIB lookup from user space.
91 .BR NETLINK_CONNECTOR " (since Linux 2.6.14)"
94 .I Documentation/connector/*
95 in the Linux kernel source tree for further information.
97 .B NETLINK_NETFILTER " (since Linux 2.6.14)"
98 .\" FIXME More details on NETLINK_NETFILTER needed.
102 Transport IPv6 packets from netfilter to user space.
108 DECnet routing messages.
110 .BR NETLINK_KOBJECT_UEVENT " (since Linux 2.6.10)"
111 .\" FIXME More details on NETLINK_KOBJECT_UEVENT needed.
112 Kernel messages to user space.
114 .BR NETLINK_GENERIC " (since Linux 2.6.15)"
115 Generic netlink family for simplified netlink usage.
117 .BR NETLINK_CRYPTO " (since Linux 3.2)"
118 .\" commit a38f7907b926e4c6c7d389ad96cc38cec2e5a9e9
119 .\" Author: Steffen Klassert <steffen.klassert@secunet.com>
120 Netlink interface to request information about ciphers registered
121 with the kernel crypto API as well as allow configuration of the
124 Netlink messages consist of a byte stream with one or multiple
126 headers and associated payload.
127 The byte stream should be accessed only with the standard
132 for further information.
134 In multipart messages (multiple
136 headers with associated payload in one byte stream) the first and all
137 following headers have the
139 flag set, except for the last header which has the type
149 __u32 nlmsg_len; /* Length of message including header */
150 __u16 nlmsg_type; /* Type of message content */
151 __u16 nlmsg_flags; /* Additional flags */
152 __u32 nlmsg_seq; /* Sequence number */
153 __u32 nlmsg_pid; /* Sender port ID */
159 can be one of the standard message types:
161 message is to be ignored,
163 message signals an error and the payload contains an
167 message terminates a multipart message.
172 int error; /* Negative errno or 0 for acknowledgements */
173 struct nlmsghdr msg; /* Message header that caused the error */
178 A netlink family usually specifies more message types, see the
179 appropriate manual pages for that, for example,
187 Standard flag bits in \fInlmsg_flags\fP
189 NLM_F_REQUEST:Must be set on all request messages.
191 The message is part of a multipart message terminated by
194 NLM_F_ACK:Request for an acknowledgment on success.
195 NLM_F_ECHO:Echo this request.
197 .\" No right adjustment for text blocks in tables
202 Additional flag bits for GET requests
204 NLM_F_ROOT:Return the complete table instead of a single entry.
206 Return all entries matching criteria passed in message content.
209 NLM_F_ATOMIC:Return an atomic snapshot of the table.
211 Convenience macro; equivalent to
213 (NLM_F_ROOT|NLM_F_MATCH).
216 .\" FIXME NLM_F_ATOMIC is not used anymore?
222 capability or an effective UID of 0.
227 Additional flag bits for NEW requests
229 NLM_F_REPLACE:Replace existing matching object.
230 NLM_F_EXCL:Don't replace if the object already exists.
231 NLM_F_CREATE:Create object if it doesn't already exist.
232 NLM_F_APPEND:Add to the end of the object list.
238 are used to track messages.
240 shows the origin of the message.
241 Note that there isn't a 1:1 relationship between
243 and the PID of the process if the message originated from a netlink
247 section for further information.
253 .\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
254 are opaque to netlink core.
256 Netlink is not a reliable protocol.
257 It tries its best to deliver a message to its destination(s),
258 but may drop messages when an out-of-memory condition or
260 For reliable transfer the sender can request an
261 acknowledgement from the receiver by setting the
264 An acknowledgment is an
266 packet with the error field set to 0.
267 The application must generate acknowledgements for
268 received messages itself.
269 The kernel tries to send an
271 message for every failed packet.
272 A user process should follow this convention too.
274 However, reliable transmissions from kernel to user are impossible
276 The kernel can't send a netlink message if the socket buffer is full:
277 the message will be dropped and the kernel and the user-space process will
278 no longer have the same view of kernel state.
279 It is up to the application to detect when this happens (via the
287 structure describes a netlink client in user space or in the kernel.
290 can be either unicast (only sent to one peer) or sent to
291 netlink multicast groups
298 sa_family_t nl_family; /* AF_NETLINK */
299 unsigned short nl_pad; /* Zero */
300 pid_t nl_pid; /* Port ID */
301 __u32 nl_groups; /* Multicast groups mask */
307 is the unicast address of netlink socket.
308 It's always 0 if the destination is in the kernel.
309 For a user-space process,
311 is usually the PID of the process owning the destination socket.
314 identifies a netlink socket, not a process.
315 If a process owns several netlink
318 can be equal to the process ID only for at most one socket.
319 There are two ways to assign
322 If the application sets
326 then it is up to the application to make sure that
329 If the application sets it to 0, the kernel takes care of assigning it.
330 The kernel assigns the process ID to the first netlink socket the process
331 opens and assigns a unique
333 to every netlink socket that the process subsequently creates.
336 is a bit mask with every bit representing a netlink group number.
337 Each netlink family has a set of 32 multicast groups.
340 is called on the socket, the
344 should be set to a bit mask of the groups which it wishes to listen to.
345 The default value for this field is zero which means that no multicasts
347 A socket may multicast messages to any of the multicast groups by setting
349 to a bit mask of the groups it wishes to send to when it calls
353 Only processes with an effective UID of 0 or the
355 capability may send or listen to a netlink multicast group.
357 .\" commit d629b836d151d43332492651dd841d32e57ebe3b
358 messages can't be broadcast to multiple groups.
359 Any replies to a message received for a multicast group should be
360 sent back to the sending PID and the multicast group.
361 Some Linux kernel subsystems may additionally allow other users
362 to send and/or receive messages.
364 .BR NETLINK_KOBJECT_UEVENT ,
365 .BR NETLINK_GENERIC ,
369 groups allow other users to receive messages.
370 No groups allow other users to send messages.
373 To set or get a netlink socket option, call
377 to write the option with the option level argument set to
379 Unless otherwise noted,
384 .BR NETLINK_PKTINFO " (since Linux 2.6.14)"
385 .\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
386 .\" Author: Patrick McHardy <kaber@trash.net>
389 control messages for received packets to get the extended
390 destination group number.
392 .BR NETLINK_ADD_MEMBERSHIP ,\ NETLINK_DROP_MEMBERSHIP " (since Linux 2.6.14)"
393 .\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
394 .\" Author: Patrick McHardy <kaber@trash.net>
395 Join/leave a group specified by
398 .BR NETLINK_LIST_MEMBERSHIPS " (since Linux 4.2)"
399 .\" commit b42be38b2778eda2237fc759e55e3b698b05b315
400 .\" Author: David Herrmann <dh.herrmann@gmail.com>
401 Retrieve all groups a socket is a member of.
407 is the size of the array.
408 The array is filled with the full membership set of the
409 socket, and the required array size is returned in
412 .BR NETLINK_BROADCAST_ERROR " (since Linux 2.6.30)"
413 .\" commit be0c22a46cfb79ab2342bb28fde99afa94ef868e
414 .\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
416 .B netlink_broadcast()
419 errors and silently ignore
423 .BR NETLINK_NO_ENOBUFS " (since Linux 2.6.30)"
424 .\" commit 38938bfe3489394e2eed5e40c9bb8f66a2ce1405
425 .\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
426 This flag can be used by unicast and broadcast listeners to avoid receiving
430 .BR NETLINK_LISTEN_ALL_NSID " (since Linux 4.2)"
431 .\" commit 59324cf35aba5336b611074028777838a963d03b
432 .\" Author: Nicolas Dichtel <nicolas.dichtel@6wind.com>
433 When set, this socket will receive netlink notifications from
434 all network namespaces that have an
436 assigned into the network namespace where the socket has been opened.
439 is sent to user space via an ancillary data.
441 .BR NETLINK_CAP_ACK " (since Linux 4.2)"
442 .\" commit 0a6a3a23ea6efde079a5b77688541a98bf202721
443 .\" Author: Christophe Ricard <christophe.ricard@gmail.com>
444 The kernel may fail to allocate the necessary room for the acknowledgment
445 message back to user space.
446 This option trims off the payload of the original netlink message.
447 The netlink message header is still included, so the user can guess from the
448 sequence number which message triggered the acknowledgment.
450 The socket interface to netlink first appeared Linux 2.2.
452 Linux 2.0 supported a more primitive device-based netlink interface
453 (which is still available as a compatibility option).
454 This obsolete interface is not described here.
456 It is often better to use netlink via
460 than via the low-level kernel interface.
462 This manual page is not complete.
464 The following example creates a
466 netlink socket which will listen to the
468 (network interface create/delete/up/down events) and
469 .B RTMGRP_IPV4_IFADDR
470 (IPv4 addresses add/delete events) multicast groups.
474 struct sockaddr_nl sa;
476 memset(&sa, 0, sizeof(sa));
477 sa.nl_family = AF_NETLINK;
478 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
480 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
481 bind(fd, (struct sockaddr *) &sa, sizeof(sa));
485 The next example demonstrates how to send a netlink message to the
487 Note that the application must take care of message sequence numbers
488 in order to reliably track acknowledgements.
492 struct nlmsghdr *nh; /* The nlmsghdr with payload to send */
493 struct sockaddr_nl sa;
494 struct iovec iov = { nh, nh\->nlmsg_len };
497 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
498 memset(&sa, 0, sizeof(sa));
499 sa.nl_family = AF_NETLINK;
501 nh\->nlmsg_seq = ++sequence_number;
502 /* Request an ack from kernel by setting NLM_F_ACK */
503 nh\->nlmsg_flags |= NLM_F_ACK;
505 sendmsg(fd, &msg, 0);
509 And the last example is about reading netlink message.
515 struct iovec iov = { buf, sizeof(buf) };
516 struct sockaddr_nl sa;
520 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
521 len = recvmsg(fd, &msg, 0);
523 for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
524 nh = NLMSG_NEXT (nh, len)) {
525 /* The end of multipart message */
526 if (nh\->nlmsg_type == NLMSG_DONE)
529 if (nh\->nlmsg_type == NLMSG_ERROR)
530 /* Do some error handling */
533 /* Continue with parsing payload */
541 .BR capabilities (7),
544 .UR ftp://ftp.inr.ac.ru\:/ip-routing\:/iproute2*
545 information about libnetlink
548 .UR http://people.suug.ch\:/~tgr\:/libnl/
549 information about libnl
552 RFC 3549 "Linux Netlink as an IP Services Protocol"