]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/netlink.7
bind.2, chmod.2, chown.2, chroot.2, clock_getres.2, clone.2, connect.2, dup.2, falloc...
[thirdparty/man-pages.git] / man7 / netlink.7
1 '\" t
2 .\" This man page is Copyright (c) 1998 by Andi Kleen.
3 .\"
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" Subject to the GPL.
6 .\" %%%LICENSE_END
7 .\"
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"
12 .SH NAME
13 netlink \- communication between kernel and user space (AF_NETLINK)
14 .SH SYNOPSIS
15 .nf
16 .B #include <asm/types.h>
17 .B #include <sys/socket.h>
18 .B #include <linux/netlink.h>
19
20 .BI "netlink_socket = socket(AF_NETLINK, " socket_type ", " netlink_family );
21 .fi
22 .SH DESCRIPTION
23 Netlink is used to transfer information between the kernel and
24 user-space processes.
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.
31
32 Netlink is a datagram-oriented service.
33 Both
34 .B SOCK_RAW
35 and
36 .B SOCK_DGRAM
37 are valid values for
38 .IR socket_type .
39 However, the netlink protocol does not distinguish between datagram
40 and raw sockets.
41
42 .I netlink_family
43 selects the kernel module or netlink group to communicate with.
44 The currently assigned netlink families are:
45 .TP
46 .BR NETLINK_ROUTE
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
51 .BR rtnetlink (7)).
52 .TP
53 .BR NETLINK_W1 " (since Linux 2.6.13)"
54 Messages from 1-wire subsystem.
55 .TP
56 .BR NETLINK_USERSOCK
57 Reserved for user-mode socket protocols.
58 .TP
59 .BR NETLINK_FIREWALL
60 Transport IPv4 packets from netfilter to user space.
61 Used by
62 .I ip_queue
63 kernel module.
64 .TP
65 .BR NETLINK_INET_DIAG " (since Linux 2.6.14)"
66 Query information about sockets of various protocol families from the kernel
67 (see
68 .BR sock_diag (7)).
69 .TP
70 .BR NETLINK_SOCK_DIAG " (since Linux 3.3)"
71 .\" commit 7f1fb60c4fc9fb29fbb406ac8c4cfb4e59e168d6
72 A synonym for
73 .BR NETLINK_INET_DIAG .
74 .TP
75 .BR NETLINK_NFLOG
76 Netfilter/iptables ULOG.
77 .TP
78 .BR NETLINK_XFRM
79 .\" FIXME More details on NETLINK_XFRM needed.
80 IPsec.
81 .TP
82 .BR NETLINK_SELINUX " (since Linux 2.6.4)"
83 SELinux event notifications.
84 .TP
85 .BR NETLINK_ISCSI " (since Linux 2.6.15)"
86 .\" FIXME More details on NETLINK_ISCSI needed.
87 Open-iSCSI.
88 .TP
89 .BR NETLINK_AUDIT " (since Linux 2.6.6)"
90 .\" FIXME More details on NETLINK_AUDIT needed.
91 Auditing.
92 .TP
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.
96 .TP
97 .BR NETLINK_CONNECTOR " (since Linux 2.6.14)"
98 Kernel connector.
99 See
100 .I Documentation/connector/*
101 in the Linux kernel source tree for further information.
102 .TP
103 .BR NETLINK_NETFILTER " (since Linux 2.6.14)"
104 .\" FIXME More details on NETLINK_NETFILTER needed.
105 Netfilter subsystem.
106 .TP
107 .BR NETLINK_IP6_FW
108 Transport IPv6 packets from netfilter to user space.
109 Used by
110 .I ip6_queue
111 kernel module.
112 .TP
113 .B NETLINK_DNRTMSG
114 DECnet routing messages.
115 .TP
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.
119 .TP
120 .BR NETLINK_GENERIC " (since Linux 2.6.15)"
121 Generic netlink family for simplified netlink usage.
122 .TP
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
128 kernel crypto API.
129 .PP
130 Netlink messages consist of a byte stream with one or multiple
131 .I nlmsghdr
132 headers and associated payload.
133 The byte stream should be accessed only with the standard
134 .B NLMSG_*
135 macros.
136 See
137 .BR netlink (3)
138 for further information.
139
140 In multipart messages (multiple
141 .I nlmsghdr
142 headers with associated payload in one byte stream) the first and all
143 following headers have the
144 .B NLM_F_MULTI
145 flag set, except for the last header which has the type
146 .BR NLMSG_DONE .
147
148 After each
149 .I nlmsghdr
150 the payload follows.
151
152 .in +4n
153 .nf
154 struct nlmsghdr {
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 */
160 };
161 .fi
162 .in
163
164 .I nlmsg_type
165 can be one of the standard message types:
166 .B NLMSG_NOOP
167 message is to be ignored,
168 .B NLMSG_ERROR
169 message signals an error and the payload contains an
170 .I nlmsgerr
171 structure,
172 .B NLMSG_DONE
173 message terminates a multipart message.
174
175 .in +4n
176 .nf
177 struct nlmsgerr {
178 int error; /* Negative errno or 0 for acknowledgements */
179 struct nlmsghdr msg; /* Message header that caused the error */
180 };
181 .fi
182 .in
183
184 A netlink family usually specifies more message types, see the
185 appropriate manual pages for that, for example,
186 .BR rtnetlink (7)
187 for
188 .BR NETLINK_ROUTE .
189 .TS
190 tab(:);
191 l s
192 lB l.
193 Standard flag bits in \fInlmsg_flags\fP
194 _
195 NLM_F_REQUEST:Must be set on all request messages.
196 NLM_F_MULTI:T{
197 The message is part of a multipart message terminated by
198 .BR NLMSG_DONE .
199 T}
200 NLM_F_ACK:Request for an acknowledgment on success.
201 NLM_F_ECHO:Echo this request.
202 .TE
203 .\" No right adjustment for text blocks in tables
204 .TS
205 tab(:);
206 l s
207 lB l.
208 Additional flag bits for GET requests
209 _
210 NLM_F_ROOT:Return the complete table instead of a single entry.
211 NLM_F_MATCH:T{
212 Return all entries matching criteria passed in message content.
213 Not implemented yet.
214 T}
215 NLM_F_ATOMIC:Return an atomic snapshot of the table.
216 NLM_F_DUMP:T{
217 Convenience macro; equivalent to
218 .br
219 (NLM_F_ROOT|NLM_F_MATCH).
220 T}
221 .TE
222 .\" FIXME NLM_F_ATOMIC is not used anymore?
223 .sp 1
224 Note that
225 .B NLM_F_ATOMIC
226 requires the
227 .B CAP_NET_ADMIN
228 capability or an effective UID of 0.
229 .TS
230 tab(:);
231 l s
232 lB l.
233 Additional flag bits for NEW requests
234 _
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.
239 .TE
240 .sp 1
241 .I nlmsg_seq
242 and
243 .I nlmsg_pid
244 are used to track messages.
245 .I nlmsg_pid
246 shows the origin of the message.
247 Note that there isn't a 1:1 relationship between
248 .I nlmsg_pid
249 and the PID of the process if the message originated from a netlink
250 socket.
251 See the
252 .B ADDRESS FORMATS
253 section for further information.
254
255 Both
256 .I nlmsg_seq
257 and
258 .I nlmsg_pid
259 .\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
260 are opaque to netlink core.
261
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
265 other error occurs.
266 For reliable transfer the sender can request an
267 acknowledgement from the receiver by setting the
268 .B NLM_F_ACK
269 flag.
270 An acknowledgment is an
271 .B NLMSG_ERROR
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
276 .B NLMSG_ERROR
277 message for every failed packet.
278 A user process should follow this convention too.
279
280 However, reliable transmissions from kernel to user are impossible
281 in any case.
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
286 .B ENOBUFS
287 error returned by
288 .BR recvmsg (2))
289 and resynchronize.
290 .SS Address formats
291 The
292 .I sockaddr_nl
293 structure describes a netlink client in user space or in the kernel.
294 A
295 .I sockaddr_nl
296 can be either unicast (only sent to one peer) or sent to
297 netlink multicast groups
298 .RI ( nl_groups
299 not equal 0).
300
301 .in +4n
302 .nf
303 struct sockaddr_nl {
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 */
308 };
309 .fi
310 .in
311
312 .I nl_pid
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,
316 .I nl_pid
317 is usually the PID of the process owning the destination socket.
318 However,
319 .I nl_pid
320 identifies a netlink socket, not a process.
321 If a process owns several netlink
322 sockets, then
323 .I nl_pid
324 can be equal to the process ID only for at most one socket.
325 There are two ways to assign
326 .I nl_pid
327 to a netlink socket.
328 If the application sets
329 .I nl_pid
330 before calling
331 .BR bind (2),
332 then it is up to the application to make sure that
333 .I nl_pid
334 is unique.
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
338 .I nl_pid
339 to every netlink socket that the process subsequently creates.
340
341 .I nl_groups
342 is a bit mask with every bit representing a netlink group number.
343 Each netlink family has a set of 32 multicast groups.
344 When
345 .BR bind (2)
346 is called on the socket, the
347 .I nl_groups
348 field in the
349 .I sockaddr_nl
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
352 will be received.
353 A socket may multicast messages to any of the multicast groups by setting
354 .I nl_groups
355 to a bit mask of the groups it wishes to send to when it calls
356 .BR sendmsg (2)
357 or does a
358 .BR connect (2).
359 Only processes with an effective UID of 0 or the
360 .B CAP_NET_ADMIN
361 capability may send or listen to a netlink multicast group.
362 Since Linux 2.6.13,
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.
369 As at Linux 3.0, the
370 .BR NETLINK_KOBJECT_UEVENT ,
371 .BR NETLINK_GENERIC ,
372 .BR NETLINK_ROUTE ,
373 and
374 .BR NETLINK_SELINUX
375 groups allow other users to receive messages.
376 No groups allow other users to send messages.
377
378 .SS Socket options
379 To set or get a netlink socket option, call
380 .BR getsockopt (2)
381 to read or
382 .BR setsockopt (2)
383 to write the option with the option level argument set to
384 .BR SOL_NETLINK .
385 Unless otherwise noted,
386 .I optval
387 is a pointer to an
388 .IR int .
389 .TP
390 .BR NETLINK_PKTINFO " (since Linux 2.6.14)"
391 .\" commit 9a4595bc7e67962f13232ee55a64e063062c3a99
392 .\" Author: Patrick McHardy <kaber@trash.net>
393 Enable
394 .B nl_pktinfo
395 control messages for received packets to get the extended
396 destination group number.
397 .TP
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
402 .IR optval .
403 .TP
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.
408 .I optval
409 is a pointer to
410 .B __u32
411 and
412 .I optlen
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
416 .I optlen.
417 .TP
418 .BR NETLINK_BROADCAST_ERROR " (since Linux 2.6.30)"
419 .\" commit be0c22a46cfb79ab2342bb28fde99afa94ef868e
420 .\" Author: Pablo Neira Ayuso <pablo@netfilter.org>
421 When not set,
422 .B netlink_broadcast()
423 only reports
424 .B ESRCH
425 errors and silently ignore
426 .B NOBUFS
427 errors.
428 .TP
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
433 .B ENOBUFS
434 errors.
435 .TP
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
441 .I nsid
442 assigned into the network namespace where the socket has been opened.
443 The
444 .I nsid
445 is sent to user space via an ancillary data.
446 .TP
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.
455 .SH VERSIONS
456 The socket interface to netlink first appeared Linux 2.2.
457
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.
461 .SH NOTES
462 It is often better to use netlink via
463 .I libnetlink
464 or
465 .I libnl
466 than via the low-level kernel interface.
467 .SH BUGS
468 This manual page is not complete.
469 .SH EXAMPLE
470 The following example creates a
471 .B NETLINK_ROUTE
472 netlink socket which will listen to the
473 .B RTMGRP_LINK
474 (network interface create/delete/up/down events) and
475 .B RTMGRP_IPV4_IFADDR
476 (IPv4 addresses add/delete events) multicast groups.
477
478 .in +4n
479 .nf
480 struct sockaddr_nl sa;
481
482 memset(&sa, 0, sizeof(sa));
483 sa.nl_family = AF_NETLINK;
484 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
485
486 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
487 bind(fd, (struct sockaddr *) &sa, sizeof(sa));
488 .fi
489 .in
490
491 The next example demonstrates how to send a netlink message to the
492 kernel (pid 0).
493 Note that the application must take care of message sequence numbers
494 in order to reliably track acknowledgements.
495
496 .in +4n
497 .nf
498 struct nlmsghdr *nh; /* The nlmsghdr with payload to send */
499 struct sockaddr_nl sa;
500 struct iovec iov = { nh, nh\->nlmsg_len };
501 struct msghdr msg;
502
503 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
504 memset(&sa, 0, sizeof(sa));
505 sa.nl_family = AF_NETLINK;
506 nh\->nlmsg_pid = 0;
507 nh\->nlmsg_seq = ++sequence_number;
508 /* Request an ack from kernel by setting NLM_F_ACK */
509 nh\->nlmsg_flags |= NLM_F_ACK;
510
511 sendmsg(fd, &msg, 0);
512 .fi
513 .in
514
515 And the last example is about reading netlink message.
516
517 .in +4n
518 .nf
519 int len;
520 char buf[4096];
521 struct iovec iov = { buf, sizeof(buf) };
522 struct sockaddr_nl sa;
523 struct msghdr msg;
524 struct nlmsghdr *nh;
525
526 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
527 len = recvmsg(fd, &msg, 0);
528
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)
533 return;
534
535 if (nh\->nlmsg_type == NLMSG_ERROR)
536 /* Do some error handling */
537 ...
538
539 /* Continue with parsing payload */
540 ...
541 }
542 .fi
543 .in
544 .SH SEE ALSO
545 .BR cmsg (3),
546 .BR netlink (3),
547 .BR capabilities (7),
548 .BR rtnetlink (7),
549 .BR sock_diag (7)
550
551 .UR ftp://ftp.inr.ac.ru\:/ip-routing\:/iproute2*
552 information about libnetlink
553 .UE
554
555 .UR http://people.suug.ch\:/~tgr\:/libnl/
556 information about libnl
557 .UE
558
559 RFC 3549 "Linux Netlink as an IP Services Protocol"