wait.2: Add ESRCH for when pid == INT_MIN
[man-pages.git] / man7 / netlink.7
blob6537e6fab9ab8cffe2e2016a5293e3f534d81bf2
1 .\" This man page is Copyright (c) 1998 by Andi Kleen.
2 .\"
3 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
4 .\" Subject to the GPL.
5 .\" %%%LICENSE_END
6 .\"
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"
11 .SH NAME
12 netlink \- communication between kernel and user space (AF_NETLINK)
13 .SH SYNOPSIS
14 .nf
15 .B #include <asm/types.h>
16 .B #include <sys/socket.h>
17 .B #include <linux/netlink.h>
18 .PP
19 .BI "netlink_socket = socket(AF_NETLINK, " socket_type ", " netlink_family );
20 .fi
21 .SH DESCRIPTION
22 Netlink is used to transfer information between the kernel and
23 user-space processes.
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.
30 .PP
31 Netlink is a datagram-oriented service.
32 Both
33 .B SOCK_RAW
34 and
35 .B SOCK_DGRAM
36 are valid values for
37 .IR socket_type .
38 However, the netlink protocol does not distinguish between datagram
39 and raw sockets.
40 .PP
41 .I netlink_family
42 selects the kernel module or netlink group to communicate with.
43 The currently assigned netlink families are:
44 .TP
45 .BR NETLINK_ROUTE
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
50 .BR rtnetlink (7)).
51 .TP
52 .BR NETLINK_W1 " (Linux 2.6.13 to 2.16.17)"
53 Messages from 1-wire subsystem.
54 .TP
55 .BR NETLINK_USERSOCK
56 Reserved for user-mode socket protocols.
57 .TP
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.
61 Used by
62 .I ip_queue
63 kernel module.
64 After a long period of being declared obsolete (in favor of the more advanced
65 .I nfnetlink_queue
66 feature),
67 .BR NETLINK_FIREWALL
68 was removed in Linux 3.5.
69 .TP
70 .BR NETLINK_SOCK_DIAG " (since Linux 3.3)"
71 .\" commit 7f1fb60c4fc9fb29fbb406ac8c4cfb4e59e168d6
72 Query information about sockets of various protocol families from the kernel
73 (see
74 .BR sock_diag (7)).
75 .TP
76 .BR NETLINK_INET_DIAG " (since Linux 2.6.14)"
77 An obsolete synonym for
78 .BR NETLINK_SOCK_DIAG .
79 .TP
80 .BR NETLINK_NFLOG " (up to and including Linux 3.16)"
81 Netfilter/iptables ULOG.
82 .TP
83 .BR NETLINK_XFRM
84 .\" FIXME More details on NETLINK_XFRM needed.
85 IPsec.
86 .TP
87 .BR NETLINK_SELINUX " (since Linux 2.6.4)"
88 SELinux event notifications.
89 .TP
90 .BR NETLINK_ISCSI " (since Linux 2.6.15)"
91 .\" FIXME More details on NETLINK_ISCSI needed.
92 Open-iSCSI.
93 .TP
94 .BR NETLINK_AUDIT " (since Linux 2.6.6)"
95 .\" FIXME More details on NETLINK_AUDIT needed.
96 Auditing.
97 .TP
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)"
103 Kernel connector.
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.
114 Netfilter subsystem.
116 .BR NETLINK_SCSITRANSPORT " (since Linux 2.6.19)"
117 .\" commit 84314fd4740ad73550c76dee4a9578979d84af48
118 .\" FIXME More details on NETLINK_SCSITRANSPORT needed.
119 SCSI Transports.
121 .BR NETLINK_RDMA " (since Linux 3.0)"
122 .\" commit b2cbae2c248776d81cc265ff7d48405b6a4cc463
123 .\" FIXME More details on NETLINK_RDMA needed.
124 Infiniband RDMA.
126 .BR NETLINK_IP6_FW " (up to and including Linux 3.4)"
127 Transport IPv6 packets from netfilter to user space.
128 Used by
129 .I ip6_queue
130 kernel module.
132 .B NETLINK_DNRTMSG
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
147 kernel crypto API.
149 Netlink messages consist of a byte stream with one or multiple
150 .I nlmsghdr
151 headers and associated payload.
152 The byte stream should be accessed only with the standard
153 .B NLMSG_*
154 macros.
156 .BR netlink (3)
157 for further information.
159 In multipart messages (multiple
160 .I nlmsghdr
161 headers with associated payload in one byte stream) the first and all
162 following headers have the
163 .B NLM_F_MULTI
164 flag set, except for the last header which has the type
165 .BR NLMSG_DONE .
167 After each
168 .I nlmsghdr
169 the payload follows.
171 .in +4n
173 struct nlmsghdr {
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 */
183 .I nlmsg_type
184 can be one of the standard message types:
185 .B NLMSG_NOOP
186 message is to be ignored,
187 .B NLMSG_ERROR
188 message signals an error and the payload contains an
189 .I nlmsgerr
190 structure,
191 .B NLMSG_DONE
192 message terminates a multipart message. Error messages get the
193 original request appened, unless the user requests to cap the
194 error message, and get extra error data if requested.
196 .in +4n
198 struct nlmsgerr {
199     int error;        /* Negative errno or 0 for acknowledgements */
200     struct nlmsghdr msg;  /* Message header that caused the error */
201     /*
202      * followed by the message contents unless NETLINK_CAP_ACK was set
203      * or the ACK indicates success (error == 0).
204      * For example Generic Netlink message with attributes.
205      * message length is aligned with NLMSG_ALIGN()
206      */
207     /*
208      * followed by TLVs defined in enum nlmsgerr_attrs
209      * if NETLINK_EXT_ACK was set
210      */
215 A netlink family usually specifies more message types, see the
216 appropriate manual pages for that, for example,
217 .BR rtnetlink (7)
219 .BR NETLINK_ROUTE .
221 .ad l
223 tab(:);
224 l s
225 lB lx.
226 Standard flag bits in \fInlmsg_flags\fP
228 NLM_F_REQUEST:T{
229 Must be set on all request messages.
231 NLM_F_MULTI:T{
232 The message is part of a multipart message terminated by
233 .BR NLMSG_DONE .
235 NLM_F_ACK:T{
236 Request for an acknowledgement on success.
238 NLM_F_ECHO:T{
239 Echo this request.
244 .\" No right adjustment for text blocks in tables
246 .ad l
248 tab(:);
249 l s
250 lB lx.
251 Additional flag bits for GET requests
253 NLM_F_ROOT:T{
254 Return the complete table instead of a single entry.
256 NLM_F_MATCH:T{
257 Return all entries matching criteria passed in message content.
258 Not implemented yet.
260 NLM_F_ATOMIC:T{
261 Return an atomic snapshot of the table.
263 NLM_F_DUMP:T{
264 Convenience macro; equivalent to
265 (NLM_F_ROOT|NLM_F_MATCH).
270 .\" FIXME NLM_F_ATOMIC is not used anymore?
272 Note that
273 .B NLM_F_ATOMIC
274 requires the
275 .B CAP_NET_ADMIN
276 capability or an effective UID of 0.
278 .ad l
280 tab(:);
281 l s
282 lB lx.
283 Additional flag bits for NEW requests
285 NLM_F_REPLACE:T{
286 Replace existing matching object.
288 NLM_F_EXCL:T{
289 Don't replace if the object already exists.
291 NLM_F_CREATE:T{
292 Create object if it doesn't already exist.
294 NLM_F_APPEND:T{
295 Add to the end of the object list.
301 .I nlmsg_seq
303 .I nlmsg_pid
304 are used to track messages.
305 .I nlmsg_pid
306 shows the origin of the message.
307 Note that there isn't a 1:1 relationship between
308 .I nlmsg_pid
309 and the PID of the process if the message originated from a netlink
310 socket.
311 See the
312 .B ADDRESS FORMATS
313 section for further information.
315 Both
316 .I nlmsg_seq
318 .I nlmsg_pid
319 .\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
320 are opaque to netlink core.
322 Netlink is not a reliable protocol.
323 It tries its best to deliver a message to its destination(s),
324 but may drop messages when an out-of-memory condition or
325 other error occurs.
326 For reliable transfer the sender can request an
327 acknowledgement from the receiver by setting the
328 .B NLM_F_ACK
329 flag.
330 An acknowledgement is an
331 .B NLMSG_ERROR
332 packet with the error field set to 0.
333 The application must generate acknowledgements for
334 received messages itself.
335 The kernel tries to send an
336 .B NLMSG_ERROR
337 message for every failed packet.
338 A user process should follow this convention too.
340 However, reliable transmissions from kernel to user are impossible
341 in any case.
342 The kernel can't send a netlink message if the socket buffer is full:
343 the message will be dropped and the kernel and the user-space process will
344 no longer have the same view of kernel state.
345 It is up to the application to detect when this happens (via the
346 .B ENOBUFS
347 error returned by
348 .BR recvmsg (2))
349 and resynchronize.
350 .SS Address formats
352 .I sockaddr_nl
353 structure describes a netlink client in user space or in the kernel.
355 .I sockaddr_nl
356 can be either unicast (only sent to one peer) or sent to
357 netlink multicast groups
358 .RI ( nl_groups
359 not equal 0).
361 .in +4n
363 struct sockaddr_nl {
364     sa_family_t     nl_family;  /* AF_NETLINK */
365     unsigned short  nl_pad;     /* Zero */
366     pid_t           nl_pid;     /* Port ID */
367     __u32           nl_groups;  /* Multicast groups mask */
372 .I nl_pid
373 is the unicast address of netlink socket.
374 It's always 0 if the destination is in the kernel.
375 For a user-space process,
376 .I nl_pid
377 is usually the PID of the process owning the destination socket.
378 However,
379 .I nl_pid
380 identifies a netlink socket, not a process.
381 If a process owns several netlink
382 sockets, then
383 .I nl_pid
384 can be equal to the process ID only for at most one socket.
385 There are two ways to assign
386 .I nl_pid
387 to a netlink socket.
388 If the application sets
389 .I nl_pid
390 before calling
391 .BR bind (2),
392 then it is up to the application to make sure that
393 .I nl_pid
394 is unique.
395 If the application sets it to 0, the kernel takes care of assigning it.
396 The kernel assigns the process ID to the first netlink socket the process
397 opens and assigns a unique
398 .I nl_pid
399 to every netlink socket that the process subsequently creates.
401 .I nl_groups
402 is a bit mask with every bit representing a netlink group number.
403 Each netlink family has a set of 32 multicast groups.
404 When
405 .BR bind (2)
406 is called on the socket, the
407 .I nl_groups
408 field in the
409 .I sockaddr_nl
410 should be set to a bit mask of the groups which it wishes to listen to.
411 The default value for this field is zero which means that no multicasts
412 will be received.
413 A socket may multicast messages to any of the multicast groups by setting
414 .I nl_groups
415 to a bit mask of the groups it wishes to send to when it calls
416 .BR sendmsg (2)
417 or does a
418 .BR connect (2).
419 Only processes with an effective UID of 0 or the
420 .B CAP_NET_ADMIN
421 capability may send or listen to a netlink multicast group.
422 Since Linux 2.6.13,
423 .\" commit d629b836d151d43332492651dd841d32e57ebe3b
424 messages can't be broadcast to multiple groups.
425 Any replies to a message received for a multicast group should be
426 sent back to the sending PID and the multicast group.
427 Some Linux kernel subsystems may additionally allow other users
428 to send and/or receive messages.
429 As at Linux 3.0, the
430 .BR NETLINK_KOBJECT_UEVENT ,
431 .BR NETLINK_GENERIC ,
432 .BR NETLINK_ROUTE ,
434 .BR NETLINK_SELINUX
435 groups allow other users to receive messages.
436 No groups allow other users to send messages.
437 .SS Socket options
438 To set or get a netlink socket option, call
439 .BR getsockopt (2)
440 to read or
441 .BR setsockopt (2)
442 to write the option with the option level argument set to
443 .BR SOL_NETLINK .
444 Unless otherwise noted,
445 .I optval
446 is a pointer to an
447 .IR int .
449 .BR NETLINK_PKTINFO " (since Linux 2.6.14)"
450 .\"     commit 9a4595bc7e67962f13232ee55a64e063062c3a99
451 .\"     Author: Patrick McHardy <kaber@trash.net>
452 Enable
453 .B nl_pktinfo
454 control messages for received packets to get the extended
455 destination group number.
457 .BR NETLINK_ADD_MEMBERSHIP ,\  NETLINK_DROP_MEMBERSHIP " (since Linux 2.6.14)"
458 .\"     commit 9a4595bc7e67962f13232ee55a64e063062c3a99
459 .\"     Author: Patrick McHardy <kaber@trash.net>
460 Join/leave a group specified by
461 .IR optval .
463 .BR NETLINK_LIST_MEMBERSHIPS " (since Linux 4.2)"
464 .\"     commit b42be38b2778eda2237fc759e55e3b698b05b315
465 .\"     Author: David Herrmann <dh.herrmann@gmail.com>
466 Retrieve all groups a socket is a member of.
467 .I optval
468 is a pointer to
469 .B __u32
471 .I optlen
472 is the size of the array.
473 The array is filled with the full membership set of the
474 socket, and the required array size is returned in
475 .IR optlen .
477 .BR NETLINK_BROADCAST_ERROR " (since Linux 2.6.30)"
478 .\"     commit be0c22a46cfb79ab2342bb28fde99afa94ef868e
479 .\"     Author: Pablo Neira Ayuso <pablo@netfilter.org>
480 When not set,
481 .B netlink_broadcast()
482 only reports
483 .B ESRCH
484 errors and silently ignore
485 .B ENOBUFS
486 errors.
488 .BR NETLINK_NO_ENOBUFS " (since Linux 2.6.30)"
489 .\"     commit 38938bfe3489394e2eed5e40c9bb8f66a2ce1405
490 .\"     Author: Pablo Neira Ayuso <pablo@netfilter.org>
491 This flag can be used by unicast and broadcast listeners to avoid receiving
492 .B ENOBUFS
493 errors.
495 .BR NETLINK_LISTEN_ALL_NSID " (since Linux 4.2)"
496 .\"     commit 59324cf35aba5336b611074028777838a963d03b
497 .\"     Author: Nicolas Dichtel <nicolas.dichtel@6wind.com>
498 When set, this socket will receive netlink notifications from
499 all network namespaces that have an
500 .I nsid
501 assigned into the network namespace where the socket has been opened.
503 .I nsid
504 is sent to user space via an ancillary data.
506 .BR NETLINK_CAP_ACK " (since Linux 4.3)"
507 .\"     commit 0a6a3a23ea6efde079a5b77688541a98bf202721
508 .\"     Author: Christophe Ricard <christophe.ricard@gmail.com>
509 The kernel may fail to allocate the necessary room for the acknowledgement
510 message back to user space.
511 This option trims off the payload of the original netlink message.
512 The netlink message header is still included, so the user can guess from the
513 sequence number which message triggered the acknowledgement.
514 .SH VERSIONS
515 The socket interface to netlink first appeared Linux 2.2.
517 Linux 2.0 supported a more primitive device-based netlink interface
518 (which is still available as a compatibility option).
519 This obsolete interface is not described here.
520 .SH NOTES
521 It is often better to use netlink via
522 .I libnetlink
524 .I libnl
525 than via the low-level kernel interface.
526 .SH BUGS
527 This manual page is not complete.
528 .SH EXAMPLES
529 The following example creates a
530 .B NETLINK_ROUTE
531 netlink socket which will listen to the
532 .B RTMGRP_LINK
533 (network interface create/delete/up/down events) and
534 .B RTMGRP_IPV4_IFADDR
535 (IPv4 addresses add/delete events) multicast groups.
537 .in +4n
539 struct sockaddr_nl sa;
541 memset(&sa, 0, sizeof(sa));
542 sa.nl_family = AF_NETLINK;
543 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
545 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
546 bind(fd, (struct sockaddr *) &sa, sizeof(sa));
550 The next example demonstrates how to send a netlink message to the
551 kernel (pid 0).
552 Note that the application must take care of message sequence numbers
553 in order to reliably track acknowledgements.
555 .in +4n
557 struct nlmsghdr *nh;    /* The nlmsghdr with payload to send */
558 struct sockaddr_nl sa;
559 struct iovec iov = { nh, nh\->nlmsg_len };
560 struct msghdr msg;
562 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
563 memset(&sa, 0, sizeof(sa));
564 sa.nl_family = AF_NETLINK;
565 nh\->nlmsg_pid = 0;
566 nh\->nlmsg_seq = ++sequence_number;
567 /* Request an ack from kernel by setting NLM_F_ACK */
568 nh\->nlmsg_flags |= NLM_F_ACK;
570 sendmsg(fd, &msg, 0);
574 And the last example is about reading netlink message.
576 .in +4n
578 int len;
579 /* 8192 to avoid message truncation on platforms with
580    page size > 4096 */
581 struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)];
582 struct iovec iov = { buf, sizeof(buf) };
583 struct sockaddr_nl sa;
584 struct msghdr msg;
585 struct nlmsghdr *nh;
587 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
588 len = recvmsg(fd, &msg, 0);
590 for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
591      nh = NLMSG_NEXT (nh, len)) {
592     /* The end of multipart message */
593     if (nh\->nlmsg_type == NLMSG_DONE)
594         return;
596     if (nh\->nlmsg_type == NLMSG_ERROR)
597         /* Do some error handling */
598     ...
600     /* Continue with parsing payload */
601     ...
605 .SH SEE ALSO
606 .BR cmsg (3),
607 .BR netlink (3),
608 .BR capabilities (7),
609 .BR rtnetlink (7),
610 .BR sock_diag (7)
612 .UR ftp://ftp.inr.ac.ru\:/ip\-routing\:/iproute2*
613 information about libnetlink
616 .UR http://www.infradead.org\:/\(titgr\:/libnl/
617 information about libnl
620 RFC 3549 "Linux Netlink as an IP Services Protocol"