]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/netlink.7
sched.7: Note error that occurs when writing invalid value to /proc/PID/autogroup
[thirdparty/man-pages.git] / man7 / netlink.7
CommitLineData
77117f4f 1'\" t
8f0aff2a 2.\" This man page is Copyright (c) 1998 by Andi Kleen.
2297bf0e 3.\"
b55e2bb3 4.\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
8f0aff2a 5.\" Subject to the GPL.
b55e2bb3 6.\" %%%LICENSE_END
6a717e5e 7.\"
77117f4f
MK
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 $
3df541c0 11.TH NETLINK 7 2016-07-17 "Linux" "Linux Programmer's Manual"
77117f4f 12.SH NAME
f68512e9 13netlink \- communication between kernel and user space (AF_NETLINK)
77117f4f
MK
14.SH SYNOPSIS
15.nf
16.B #include <asm/types.h>
17.B #include <sys/socket.h>
18.B #include <linux/netlink.h>
19
d4c8c97c 20.BI "netlink_socket = socket(AF_NETLINK, " socket_type ", " netlink_family );
77117f4f
MK
21.fi
22.SH DESCRIPTION
0a66259f 23Netlink is used to transfer information between the kernel and
7fac88a9
MK
24user-space processes.
25It consists of a standard sockets-based interface for user space
77117f4f
MK
26processes and an internal kernel API for kernel modules.
27The internal kernel interface is not documented in this manual page.
28There is also an obsolete netlink interface
29via netlink character devices; this interface is not documented here
33a0ccb2 30and is provided only for backward compatibility.
77117f4f
MK
31
32Netlink is a datagram-oriented service.
33Both
34.B SOCK_RAW
35and
36.B SOCK_DGRAM
37are valid values for
38.IR socket_type .
39However, the netlink protocol does not distinguish between datagram
40and raw sockets.
41
42.I netlink_family
43selects the kernel module or netlink group to communicate with.
44The currently assigned netlink families are:
45.TP
6cab1c1f 46.BR NETLINK_ROUTE
77117f4f
MK
47Receives routing and link updates and may be used to modify the routing
48tables (both IPv4 and IPv6), IP addresses, link parameters,
49neighbor setups, queueing disciplines, traffic classes and
50packet classifiers (see
51.BR rtnetlink (7)).
52.TP
6cab1c1f 53.BR NETLINK_W1 " (since Linux 2.6.13)"
77117f4f
MK
54Messages from 1-wire subsystem.
55.TP
6cab1c1f 56.BR NETLINK_USERSOCK
77117f4f
MK
57Reserved for user-mode socket protocols.
58.TP
6cab1c1f 59.BR NETLINK_FIREWALL
7fac88a9 60Transport IPv4 packets from netfilter to user space.
77117f4f
MK
61Used by
62.I ip_queue
63kernel module.
64.TP
6cab1c1f 65.BR NETLINK_INET_DIAG " (since Linux 2.6.14)"
77117f4f
MK
66.\" FIXME More details on NETLINK_INET_DIAG needed.
67INET socket monitoring.
68.TP
6cab1c1f 69.BR NETLINK_NFLOG
77117f4f
MK
70Netfilter/iptables ULOG.
71.TP
6cab1c1f 72.BR NETLINK_XFRM
77117f4f
MK
73.\" FIXME More details on NETLINK_XFRM needed.
74IPsec.
75.TP
6cab1c1f 76.BR NETLINK_SELINUX " (since Linux 2.6.4)"
77117f4f
MK
77SELinux event notifications.
78.TP
6cab1c1f 79.BR NETLINK_ISCSI " (since Linux 2.6.15)"
77117f4f
MK
80.\" FIXME More details on NETLINK_ISCSI needed.
81Open-iSCSI.
82.TP
6cab1c1f 83.BR NETLINK_AUDIT " (since Linux 2.6.6)"
77117f4f
MK
84.\" FIXME More details on NETLINK_AUDIT needed.
85Auditing.
86.TP
6cab1c1f 87.BR NETLINK_FIB_LOOKUP " (since Linux 2.6.13)"
77117f4f 88.\" FIXME More details on NETLINK_FIB_LOOKUP needed.
7fac88a9 89Access to FIB lookup from user space.
77117f4f 90.TP
6cab1c1f 91.BR NETLINK_CONNECTOR " (since Linux 2.6.14)"
77117f4f
MK
92Kernel connector.
93See
94.I Documentation/connector/*
66a9882e 95in the Linux kernel source tree for further information.
77117f4f 96.TP
6cab1c1f 97.B NETLINK_NETFILTER " (since Linux 2.6.14)"
77117f4f
MK
98.\" FIXME More details on NETLINK_NETFILTER needed.
99Netfilter subsystem.
100.TP
6cab1c1f 101.BR NETLINK_IP6_FW
7fac88a9 102Transport IPv6 packets from netfilter to user space.
77117f4f
MK
103Used by
104.I ip6_queue
105kernel module.
106.TP
107.B NETLINK_DNRTMSG
108DECnet routing messages.
109.TP
6cab1c1f 110.BR NETLINK_KOBJECT_UEVENT " (since Linux 2.6.10)"
77117f4f 111.\" FIXME More details on NETLINK_KOBJECT_UEVENT needed.
7fac88a9 112Kernel messages to user space.
77117f4f 113.TP
6cab1c1f 114.BR NETLINK_GENERIC " (since Linux 2.6.15)"
77117f4f 115Generic netlink family for simplified netlink usage.
7b429332 116.TP
bbb4532b
MK
117.BR NETLINK_CRYPTO " (since Linux 3.2)"
118.\" commit a38f7907b926e4c6c7d389ad96cc38cec2e5a9e9
119.\" Author: Steffen Klassert <steffen.klassert@secunet.com>
7b429332
SM
120Netlink interface to request information about ciphers registered
121with the kernel crypto API as well as allow configuration of the
122kernel crypto API.
77117f4f
MK
123.PP
124Netlink messages consist of a byte stream with one or multiple
125.I nlmsghdr
126headers and associated payload.
33a0ccb2 127The byte stream should be accessed only with the standard
77117f4f
MK
128.B NLMSG_*
129macros.
130See
131.BR netlink (3)
132for further information.
133
134In multipart messages (multiple
135.I nlmsghdr
136headers with associated payload in one byte stream) the first and all
137following headers have the
138.B NLM_F_MULTI
139flag set, except for the last header which has the type
140.BR NLMSG_DONE .
141
142After each
143.I nlmsghdr
144the payload follows.
145
146.in +4n
147.nf
148struct nlmsghdr {
6c5a6b2c
MK
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 */
77117f4f
MK
154};
155.fi
156.in
157
158.I nlmsg_type
159can be one of the standard message types:
160.B NLMSG_NOOP
161message is to be ignored,
162.B NLMSG_ERROR
163message signals an error and the payload contains an
164.I nlmsgerr
165structure,
166.B NLMSG_DONE
167message terminates a multipart message.
168
169.in +4n
170.nf
171struct nlmsgerr {
172 int error; /* Negative errno or 0 for acknowledgements */
173 struct nlmsghdr msg; /* Message header that caused the error */
174};
175.fi
176.in
177
178A netlink family usually specifies more message types, see the
179appropriate manual pages for that, for example,
180.BR rtnetlink (7)
181for
182.BR NETLINK_ROUTE .
77117f4f
MK
183.TS
184tab(:);
3421b826 185l s
77117f4f 186lB l.
3421b826
BIG
187Standard flag bits in \fInlmsg_flags\fP
188_
77117f4f
MK
189NLM_F_REQUEST:Must be set on all request messages.
190NLM_F_MULTI:T{
191The message is part of a multipart message terminated by
192.BR NLMSG_DONE .
193T}
194NLM_F_ACK:Request for an acknowledgment on success.
195NLM_F_ECHO:Echo this request.
196.TE
d33aa381 197.\" No right adjustment for text blocks in tables
77117f4f
MK
198.TS
199tab(:);
3421b826 200l s
77117f4f 201lB l.
3421b826
BIG
202Additional flag bits for GET requests
203_
77117f4f
MK
204NLM_F_ROOT:Return the complete table instead of a single entry.
205NLM_F_MATCH:T{
206Return all entries matching criteria passed in message content.
207Not implemented yet.
208T}
77117f4f 209NLM_F_ATOMIC:Return an atomic snapshot of the table.
3421b826 210NLM_F_DUMP:T{
0eb65d3a
MK
211Convenience macro; equivalent to
212.br
213(NLM_F_ROOT|NLM_F_MATCH).
3421b826 214T}
77117f4f 215.TE
922077e0 216.\" FIXME NLM_F_ATOMIC is not used anymore?
3421b826 217.sp 1
77117f4f
MK
218Note that
219.B NLM_F_ATOMIC
220requires the
221.B CAP_NET_ADMIN
222capability or an effective UID of 0.
77117f4f
MK
223.TS
224tab(:);
3421b826 225l s
77117f4f 226lB l.
3421b826
BIG
227Additional flag bits for NEW requests
228_
77117f4f
MK
229NLM_F_REPLACE:Replace existing matching object.
230NLM_F_EXCL:Don't replace if the object already exists.
231NLM_F_CREATE:Create object if it doesn't already exist.
232NLM_F_APPEND:Add to the end of the object list.
233.TE
3421b826 234.sp 1
77117f4f
MK
235.I nlmsg_seq
236and
237.I nlmsg_pid
238are used to track messages.
239.I nlmsg_pid
240shows the origin of the message.
241Note that there isn't a 1:1 relationship between
242.I nlmsg_pid
243and the PID of the process if the message originated from a netlink
244socket.
245See the
246.B ADDRESS FORMATS
247section for further information.
248
249Both
250.I nlmsg_seq
251and
252.I nlmsg_pid
253.\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
254are opaque to netlink core.
255
256Netlink is not a reliable protocol.
257It tries its best to deliver a message to its destination(s),
258but may drop messages when an out-of-memory condition or
259other error occurs.
260For reliable transfer the sender can request an
261acknowledgement from the receiver by setting the
262.B NLM_F_ACK
263flag.
264An acknowledgment is an
265.B NLMSG_ERROR
266packet with the error field set to 0.
267The application must generate acknowledgements for
268received messages itself.
269The kernel tries to send an
270.B NLMSG_ERROR
271message for every failed packet.
272A user process should follow this convention too.
273
274However, reliable transmissions from kernel to user are impossible
275in any case.
276The kernel can't send a netlink message if the socket buffer is full:
7fac88a9 277the message will be dropped and the kernel and the user-space process will
77117f4f
MK
278no longer have the same view of kernel state.
279It is up to the application to detect when this happens (via the
280.B ENOBUFS
281error returned by
282.BR recvmsg (2))
283and resynchronize.
c634028a 284.SS Address formats
77117f4f
MK
285The
286.I sockaddr_nl
287structure describes a netlink client in user space or in the kernel.
288A
289.I sockaddr_nl
290can be either unicast (only sent to one peer) or sent to
291netlink multicast groups
292.RI ( nl_groups
293not equal 0).
294
295.in +4n
296.nf
297struct sockaddr_nl {
298 sa_family_t nl_family; /* AF_NETLINK */
6c5a6b2c
MK
299 unsigned short nl_pad; /* Zero */
300 pid_t nl_pid; /* Port ID */
301 __u32 nl_groups; /* Multicast groups mask */
77117f4f
MK
302};
303.fi
304.in
305
306.I nl_pid
307is the unicast address of netlink socket.
308It's always 0 if the destination is in the kernel.
7fac88a9 309For a user-space process,
77117f4f
MK
310.I nl_pid
311is usually the PID of the process owning the destination socket.
312However,
313.I nl_pid
314identifies a netlink socket, not a process.
315If a process owns several netlink
316sockets, then
317.I nl_pid
33a0ccb2 318can be equal to the process ID only for at most one socket.
77117f4f
MK
319There are two ways to assign
320.I nl_pid
321to a netlink socket.
322If the application sets
323.I nl_pid
324before calling
325.BR bind (2),
326then it is up to the application to make sure that
327.I nl_pid
328is unique.
329If the application sets it to 0, the kernel takes care of assigning it.
330The kernel assigns the process ID to the first netlink socket the process
331opens and assigns a unique
332.I nl_pid
333to every netlink socket that the process subsequently creates.
334
335.I nl_groups
336is a bit mask with every bit representing a netlink group number.
337Each netlink family has a set of 32 multicast groups.
338When
339.BR bind (2)
340is called on the socket, the
341.I nl_groups
342field in the
343.I sockaddr_nl
344should be set to a bit mask of the groups which it wishes to listen to.
345The default value for this field is zero which means that no multicasts
346will be received.
347A socket may multicast messages to any of the multicast groups by setting
348.I nl_groups
349to a bit mask of the groups it wishes to send to when it calls
350.BR sendmsg (2)
351or does a
352.BR connect (2).
353Only processes with an effective UID of 0 or the
354.B CAP_NET_ADMIN
355capability may send or listen to a netlink multicast group.
b96a8bc5
AV
356Since Linux 2.6.13,
357.\" commit d629b836d151d43332492651dd841d32e57ebe3b
358messages can't be broadcast to multiple groups.
77117f4f
MK
359Any replies to a message received for a multicast group should be
360sent back to the sending PID and the multicast group.
dfad7db9
JM
361Some Linux kernel subsystems may additionally allow other users
362to send and/or receive messages.
50b57418
MK
363As at Linux 3.0, the
364.BR NETLINK_KOBJECT_UEVENT ,
dfad7db9 365.BR NETLINK_GENERIC ,
50b57418
MK
366.BR NETLINK_ROUTE ,
367and
368.BR NETLINK_SELINUX
dfad7db9
JM
369groups allow other users to receive messages.
370No groups allow other users to send messages.
67d0dc13
AV
371
372.SS Socket options
373To set or get a netlink socket option, call
374.BR getsockopt (2)
375to read or
376.BR setsockopt (2)
377to write the option with the option level argument set to
378.BR SOL_NETLINK .
379Unless otherwise noted,
380.I optval
381is a pointer to an
382.IR int .
383.TP
384.BR NETLINK_PKTINFO " (since Linux 2.6.14)"
dd4d8039
MK
385.\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
386.\" Author: Patrick McHardy <kaber@trash.net>
67d0dc13
AV
387Enable
388.B nl_pktinfo
389control messages for received packets to get the extended
390destination group number.
391.TP
392.BR NETLINK_ADD_MEMBERSHIP ,\ NETLINK_DROP_MEMBERSHIP " (since Linux 2.6.14)"
67d0dc13
AV
393.\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
394.\" Author: Patrick McHardy <kaber@trash.net>
376db9a1
MK
395Join/leave a group specified by
396.IR optval .
67d0dc13
AV
397.TP
398.BR NETLINK_LIST_MEMBERSHIPS " (since Linux 4.2)"
376db9a1
MK
399.\" commit b42be38b2778eda2237fc759e55e3b698b05b315
400.\" Author: David Herrmann <dh.herrmann@gmail.com>
67d0dc13
AV
401Retrieve all groups a socket is a member of.
402.I optval
403is a pointer to
404.B __u32
405and
406.I optlen
319a5cb8
MK
407is the size of the array.
408The array is filled with the full membership set of the
67d0dc13
AV
409socket, and the required array size is returned in
410.I optlen.
67d0dc13
AV
411.TP
412.BR NETLINK_BROADCAST_ERROR " (since Linux 2.6.30)"
376db9a1
MK
413.\" commit be0c22a46cfb79ab2342bb28fde99afa94ef868e
414.\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
67d0dc13
AV
415When not set,
416.B netlink_broadcast()
417only reports
418.B ESRCH
419errors and silently ignore
420.B NOBUFS
421errors.
67d0dc13
AV
422.TP
423.BR NETLINK_NO_ENOBUFS " (since Linux 2.6.30)"
376db9a1
MK
424.\" commit 38938bfe3489394e2eed5e40c9bb8f66a2ce1405
425.\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
67d0dc13
AV
426This flag can be used by unicast and broadcast listeners to avoid receiving
427.B ENOBUFS
428errors.
67d0dc13
AV
429.TP
430.BR NETLINK_LISTEN_ALL_NSID " (since Linux 4.2)"
376db9a1
MK
431.\" commit 59324cf35aba5336b611074028777838a963d03b
432.\" Author: Nicolas Dichtel <nicolas.dichtel@6wind.com>
433When set, this socket will receive netlink notifications from
434all network namespaces that have an
67d0dc13 435.I nsid
319a5cb8
MK
436assigned into the network namespace where the socket has been opened.
437The
67d0dc13
AV
438.I nsid
439is sent to user space via an ancillary data.
67d0dc13
AV
440.TP
441.BR NETLINK_CAP_ACK " (since Linux 4.2)"
376db9a1
MK
442.\" commit 0a6a3a23ea6efde079a5b77688541a98bf202721
443.\" Author: Christophe Ricard <christophe.ricard@gmail.com>
67d0dc13 444The kernel may fail to allocate the necessary room for the acknowledgment
6f858d5c 445message back to user space.
319a5cb8 446This option trims off the payload of the original netlink message.
67d0dc13 447The netlink message header is still included, so the user can guess from the
dd4d8039 448sequence number which message triggered the acknowledgment.
77117f4f 449.SH VERSIONS
6cab1c1f 450The socket interface to netlink first appeared Linux 2.2.
77117f4f 451
e929e68b 452Linux 2.0 supported a more primitive device-based netlink interface
77117f4f
MK
453(which is still available as a compatibility option).
454This obsolete interface is not described here.
77117f4f
MK
455.SH NOTES
456It is often better to use netlink via
457.I libnetlink
458or
459.I libnl
460than via the low-level kernel interface.
461.SH BUGS
462This manual page is not complete.
463.SH EXAMPLE
464The following example creates a
465.B NETLINK_ROUTE
466netlink socket which will listen to the
467.B RTMGRP_LINK
468(network interface create/delete/up/down events) and
469.B RTMGRP_IPV4_IFADDR
470(IPv4 addresses add/delete events) multicast groups.
471
472.in +4n
473.nf
474struct sockaddr_nl sa;
475
476memset(&sa, 0, sizeof(sa));
63042553
VN
477sa.nl_family = AF_NETLINK;
478sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
77117f4f
MK
479
480fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
48011c24 481bind(fd, (struct sockaddr *) &sa, sizeof(sa));
77117f4f
MK
482.fi
483.in
484
485The next example demonstrates how to send a netlink message to the
486kernel (pid 0).
69b77259 487Note that the application must take care of message sequence numbers
77117f4f
MK
488in order to reliably track acknowledgements.
489
490.in +4n
491.nf
6c5a6b2c 492struct nlmsghdr *nh; /* The nlmsghdr with payload to send */
77117f4f 493struct sockaddr_nl sa;
13f78d96 494struct iovec iov = { nh, nh\->nlmsg_len };
77117f4f
MK
495struct msghdr msg;
496
13f78d96 497msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
77117f4f
MK
498memset(&sa, 0, sizeof(sa));
499sa.nl_family = AF_NETLINK;
500nh\->nlmsg_pid = 0;
501nh\->nlmsg_seq = ++sequence_number;
6c5a6b2c 502/* Request an ack from kernel by setting NLM_F_ACK */
77117f4f
MK
503nh\->nlmsg_flags |= NLM_F_ACK;
504
505sendmsg(fd, &msg, 0);
506.fi
507.in
508
509And the last example is about reading netlink message.
510
511.in +4n
512.nf
513int len;
514char buf[4096];
515struct iovec iov = { buf, sizeof(buf) };
516struct sockaddr_nl sa;
517struct msghdr msg;
518struct nlmsghdr *nh;
519
13f78d96 520msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
77117f4f
MK
521len = recvmsg(fd, &msg, 0);
522
523for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
524 nh = NLMSG_NEXT (nh, len)) {
6c5a6b2c 525 /* The end of multipart message */
77117f4f
MK
526 if (nh\->nlmsg_type == NLMSG_DONE)
527 return;
528
529 if (nh\->nlmsg_type == NLMSG_ERROR)
6c5a6b2c 530 /* Do some error handling */
77117f4f
MK
531 ...
532
6c5a6b2c 533 /* Continue with parsing payload */
77117f4f
MK
534 ...
535}
536.fi
537.in
47297adb 538.SH SEE ALSO
77117f4f
MK
539.BR cmsg (3),
540.BR netlink (3),
541.BR capabilities (7),
542.BR rtnetlink (7)
173fe7e7 543
608bf950
SK
544.UR ftp://ftp.inr.ac.ru\:/ip-routing\:/iproute2*
545information about libnetlink
546.UE
173fe7e7 547
608bf950
SK
548.UR http://people.suug.ch\:/~tgr\:/libnl/
549information about libnl
550.UE
173fe7e7 551
77117f4f 552RFC 3549 "Linux Netlink as an IP Services Protocol"