]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/netlink.7
eventfd.2, futex.2, mmap2.2, open.2, pciconfig_read.2, ptrace.2, reboot.2, request_ke...
[thirdparty/man-pages.git] / man7 / netlink.7
CommitLineData
77117f4f
MK
1'\" t
2.\" Don't change the first line, it tells man that tbl is needed.
3.\" This man page is Copyright (c) 1998 by Andi Kleen. Subject to the GPL.
4.\" Based on the original comments from Alexey Kuznetsov
5.\" Modified 2005-12-27 by Hasso Tepper <hasso@estpak.ee>
6.\" $Id: netlink.7,v 1.8 2000/06/22 13:23:00 ak Exp $
608bf950 7.TH NETLINK 7 2012-08-05 "Linux" "Linux Programmer's Manual"
77117f4f 8.SH NAME
7fac88a9 9netlink \- Communication between kernel and user space (AF_NETLINK)
77117f4f
MK
10.SH SYNOPSIS
11.nf
12.B #include <asm/types.h>
13.B #include <sys/socket.h>
14.B #include <linux/netlink.h>
15
d4c8c97c 16.BI "netlink_socket = socket(AF_NETLINK, " socket_type ", " netlink_family );
77117f4f
MK
17.fi
18.SH DESCRIPTION
19Netlink is used to transfer information between kernel and
7fac88a9
MK
20user-space processes.
21It consists of a standard sockets-based interface for user space
77117f4f
MK
22processes and an internal kernel API for kernel modules.
23The internal kernel interface is not documented in this manual page.
24There is also an obsolete netlink interface
25via netlink character devices; this interface is not documented here
5fab2e7c 26and is only provided for backward compatibility.
77117f4f
MK
27
28Netlink is a datagram-oriented service.
29Both
30.B SOCK_RAW
31and
32.B SOCK_DGRAM
33are valid values for
34.IR socket_type .
35However, the netlink protocol does not distinguish between datagram
36and raw sockets.
37
38.I netlink_family
39selects the kernel module or netlink group to communicate with.
40The currently assigned netlink families are:
41.TP
42.B NETLINK_ROUTE
43Receives routing and link updates and may be used to modify the routing
44tables (both IPv4 and IPv6), IP addresses, link parameters,
45neighbor setups, queueing disciplines, traffic classes and
46packet classifiers (see
47.BR rtnetlink (7)).
48.TP
49.B NETLINK_W1
50Messages from 1-wire subsystem.
51.TP
52.B NETLINK_USERSOCK
53Reserved for user-mode socket protocols.
54.TP
55.B NETLINK_FIREWALL
7fac88a9 56Transport IPv4 packets from netfilter to user space.
77117f4f
MK
57Used by
58.I ip_queue
59kernel module.
60.TP
61.B NETLINK_INET_DIAG
62.\" FIXME More details on NETLINK_INET_DIAG needed.
63INET socket monitoring.
64.TP
65.B NETLINK_NFLOG
66Netfilter/iptables ULOG.
67.TP
68.B NETLINK_XFRM
69.\" FIXME More details on NETLINK_XFRM needed.
70IPsec.
71.TP
72.B NETLINK_SELINUX
73SELinux event notifications.
74.TP
75.B NETLINK_ISCSI
76.\" FIXME More details on NETLINK_ISCSI needed.
77Open-iSCSI.
78.TP
79.B NETLINK_AUDIT
80.\" FIXME More details on NETLINK_AUDIT needed.
81Auditing.
82.TP
83.B NETLINK_FIB_LOOKUP
84.\" FIXME More details on NETLINK_FIB_LOOKUP needed.
7fac88a9 85Access to FIB lookup from user space.
77117f4f
MK
86.TP
87.B NETLINK_CONNECTOR
88Kernel connector.
89See
90.I Documentation/connector/*
66a9882e 91in the Linux kernel source tree for further information.
77117f4f
MK
92.TP
93.B NETLINK_NETFILTER
94.\" FIXME More details on NETLINK_NETFILTER needed.
95Netfilter subsystem.
96.TP
97.B NETLINK_IP6_FW
7fac88a9 98Transport IPv6 packets from netfilter to user space.
77117f4f
MK
99Used by
100.I ip6_queue
101kernel module.
102.TP
103.B NETLINK_DNRTMSG
104DECnet routing messages.
105.TP
106.B NETLINK_KOBJECT_UEVENT
107.\" FIXME More details on NETLINK_KOBJECT_UEVENT needed.
7fac88a9 108Kernel messages to user space.
77117f4f
MK
109.TP
110.B NETLINK_GENERIC
111Generic netlink family for simplified netlink usage.
112.PP
113Netlink messages consist of a byte stream with one or multiple
114.I nlmsghdr
115headers and associated payload.
116The byte stream should only be accessed with the standard
117.B NLMSG_*
118macros.
119See
120.BR netlink (3)
121for further information.
122
123In multipart messages (multiple
124.I nlmsghdr
125headers with associated payload in one byte stream) the first and all
126following headers have the
127.B NLM_F_MULTI
128flag set, except for the last header which has the type
129.BR NLMSG_DONE .
130
131After each
132.I nlmsghdr
133the payload follows.
134
135.in +4n
136.nf
137struct nlmsghdr {
138 __u32 nlmsg_len; /* Length of message including header. */
139 __u16 nlmsg_type; /* Type of message content. */
140 __u16 nlmsg_flags; /* Additional flags. */
141 __u32 nlmsg_seq; /* Sequence number. */
b8896b6e 142 __u32 nlmsg_pid; /* Sender port ID. */
77117f4f
MK
143};
144.fi
145.in
146
147.I nlmsg_type
148can be one of the standard message types:
149.B NLMSG_NOOP
150message is to be ignored,
151.B NLMSG_ERROR
152message signals an error and the payload contains an
153.I nlmsgerr
154structure,
155.B NLMSG_DONE
156message terminates a multipart message.
157
158.in +4n
159.nf
160struct nlmsgerr {
161 int error; /* Negative errno or 0 for acknowledgements */
162 struct nlmsghdr msg; /* Message header that caused the error */
163};
164.fi
165.in
166
167A netlink family usually specifies more message types, see the
168appropriate manual pages for that, for example,
169.BR rtnetlink (7)
170for
171.BR NETLINK_ROUTE .
77117f4f
MK
172.TS
173tab(:);
3421b826 174l s
77117f4f 175lB l.
3421b826
BIG
176Standard flag bits in \fInlmsg_flags\fP
177_
77117f4f
MK
178NLM_F_REQUEST:Must be set on all request messages.
179NLM_F_MULTI:T{
180The message is part of a multipart message terminated by
181.BR NLMSG_DONE .
182T}
183NLM_F_ACK:Request for an acknowledgment on success.
184NLM_F_ECHO:Echo this request.
185.TE
3421b826
BIG
186.ad
187.sp 1
188.\" No right adustment for text blocks in tables
189.na
77117f4f
MK
190.TS
191tab(:);
3421b826 192l s
77117f4f 193lB l.
3421b826
BIG
194Additional flag bits for GET requests
195_
77117f4f
MK
196NLM_F_ROOT:Return the complete table instead of a single entry.
197NLM_F_MATCH:T{
198Return all entries matching criteria passed in message content.
199Not implemented yet.
200T}
201.\" FIXME NLM_F_ATOMIC is not used any more?
202NLM_F_ATOMIC:Return an atomic snapshot of the table.
3421b826
BIG
203NLM_F_DUMP:T{
204Convenience macro; equivalent to (NLM_F_ROOT|NLM_F_MATCH).
205T}
77117f4f 206.TE
3421b826
BIG
207.ad
208.sp 1
77117f4f
MK
209Note that
210.B NLM_F_ATOMIC
211requires the
212.B CAP_NET_ADMIN
213capability or an effective UID of 0.
214
3421b826 215.na
77117f4f
MK
216.TS
217tab(:);
3421b826 218l s
77117f4f 219lB l.
3421b826
BIG
220Additional flag bits for NEW requests
221_
77117f4f
MK
222NLM_F_REPLACE:Replace existing matching object.
223NLM_F_EXCL:Don't replace if the object already exists.
224NLM_F_CREATE:Create object if it doesn't already exist.
225NLM_F_APPEND:Add to the end of the object list.
226.TE
3421b826
BIG
227.ad
228.sp 1
77117f4f
MK
229.I nlmsg_seq
230and
231.I nlmsg_pid
232are used to track messages.
233.I nlmsg_pid
234shows the origin of the message.
235Note that there isn't a 1:1 relationship between
236.I nlmsg_pid
237and the PID of the process if the message originated from a netlink
238socket.
239See the
240.B ADDRESS FORMATS
241section for further information.
242
243Both
244.I nlmsg_seq
245and
246.I nlmsg_pid
247.\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
248are opaque to netlink core.
249
250Netlink is not a reliable protocol.
251It tries its best to deliver a message to its destination(s),
252but may drop messages when an out-of-memory condition or
253other error occurs.
254For reliable transfer the sender can request an
255acknowledgement from the receiver by setting the
256.B NLM_F_ACK
257flag.
258An acknowledgment is an
259.B NLMSG_ERROR
260packet with the error field set to 0.
261The application must generate acknowledgements for
262received messages itself.
263The kernel tries to send an
264.B NLMSG_ERROR
265message for every failed packet.
266A user process should follow this convention too.
267
268However, reliable transmissions from kernel to user are impossible
269in any case.
270The kernel can't send a netlink message if the socket buffer is full:
7fac88a9 271the message will be dropped and the kernel and the user-space process will
77117f4f
MK
272no longer have the same view of kernel state.
273It is up to the application to detect when this happens (via the
274.B ENOBUFS
275error returned by
276.BR recvmsg (2))
277and resynchronize.
278.SS Address Formats
279The
280.I sockaddr_nl
281structure describes a netlink client in user space or in the kernel.
282A
283.I sockaddr_nl
284can be either unicast (only sent to one peer) or sent to
285netlink multicast groups
286.RI ( nl_groups
287not equal 0).
288
289.in +4n
290.nf
291struct sockaddr_nl {
292 sa_family_t nl_family; /* AF_NETLINK */
293 unsigned short nl_pad; /* Zero. */
b8896b6e 294 pid_t nl_pid; /* Port ID. */
77117f4f
MK
295 __u32 nl_groups; /* Multicast groups mask. */
296};
297.fi
298.in
299
300.I nl_pid
301is the unicast address of netlink socket.
302It's always 0 if the destination is in the kernel.
7fac88a9 303For a user-space process,
77117f4f
MK
304.I nl_pid
305is usually the PID of the process owning the destination socket.
306However,
307.I nl_pid
308identifies a netlink socket, not a process.
309If a process owns several netlink
310sockets, then
311.I nl_pid
312can only be equal to the process ID for at most one socket.
313There are two ways to assign
314.I nl_pid
315to a netlink socket.
316If the application sets
317.I nl_pid
318before calling
319.BR bind (2),
320then it is up to the application to make sure that
321.I nl_pid
322is unique.
323If the application sets it to 0, the kernel takes care of assigning it.
324The kernel assigns the process ID to the first netlink socket the process
325opens and assigns a unique
326.I nl_pid
327to every netlink socket that the process subsequently creates.
328
329.I nl_groups
330is a bit mask with every bit representing a netlink group number.
331Each netlink family has a set of 32 multicast groups.
332When
333.BR bind (2)
334is called on the socket, the
335.I nl_groups
336field in the
337.I sockaddr_nl
338should be set to a bit mask of the groups which it wishes to listen to.
339The default value for this field is zero which means that no multicasts
340will be received.
341A socket may multicast messages to any of the multicast groups by setting
342.I nl_groups
343to a bit mask of the groups it wishes to send to when it calls
344.BR sendmsg (2)
345or does a
346.BR connect (2).
347Only processes with an effective UID of 0 or the
348.B CAP_NET_ADMIN
349capability may send or listen to a netlink multicast group.
350Any replies to a message received for a multicast group should be
351sent back to the sending PID and the multicast group.
dfad7db9
JM
352Some Linux kernel subsystems may additionally allow other users
353to send and/or receive messages.
50b57418
MK
354As at Linux 3.0, the
355.BR NETLINK_KOBJECT_UEVENT ,
dfad7db9 356.BR NETLINK_GENERIC ,
50b57418
MK
357.BR NETLINK_ROUTE ,
358and
359.BR NETLINK_SELINUX
dfad7db9
JM
360groups allow other users to receive messages.
361No groups allow other users to send messages.
77117f4f
MK
362.SH VERSIONS
363The socket interface to netlink is a new feature of Linux 2.2.
364
e929e68b 365Linux 2.0 supported a more primitive device-based netlink interface
77117f4f
MK
366(which is still available as a compatibility option).
367This obsolete interface is not described here.
368
369NETLINK_SELINUX appeared in Linux 2.6.4.
370
371NETLINK_AUDIT appeared in Linux 2.6.6.
372
373NETLINK_KOBJECT_UEVENT appeared in Linux 2.6.10.
374
375NETLINK_W1 and NETLINK_FIB_LOOKUP appeared in Linux 2.6.13.
376
377NETLINK_INET_DIAG, NETLINK_CONNECTOR and NETLINK_NETFILTER appeared in
378Linux 2.6.14.
379
380NETLINK_GENERIC and NETLINK_ISCSI appeared in Linux 2.6.15.
381.SH NOTES
382It is often better to use netlink via
383.I libnetlink
384or
385.I libnl
386than via the low-level kernel interface.
387.SH BUGS
388This manual page is not complete.
389.SH EXAMPLE
390The following example creates a
391.B NETLINK_ROUTE
392netlink socket which will listen to the
393.B RTMGRP_LINK
394(network interface create/delete/up/down events) and
395.B RTMGRP_IPV4_IFADDR
396(IPv4 addresses add/delete events) multicast groups.
397
398.in +4n
399.nf
400struct sockaddr_nl sa;
401
402memset(&sa, 0, sizeof(sa));
63042553
VN
403sa.nl_family = AF_NETLINK;
404sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
77117f4f
MK
405
406fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
48011c24 407bind(fd, (struct sockaddr *) &sa, sizeof(sa));
77117f4f
MK
408.fi
409.in
410
411The next example demonstrates how to send a netlink message to the
412kernel (pid 0).
413Note that application must take care of message sequence numbers
414in order to reliably track acknowledgements.
415
416.in +4n
417.nf
418struct nlmsghdr *nh; /* The nlmsghdr with payload to send. */
419struct sockaddr_nl sa;
13f78d96 420struct iovec iov = { nh, nh\->nlmsg_len };
77117f4f
MK
421struct msghdr msg;
422
13f78d96 423msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
77117f4f
MK
424memset(&sa, 0, sizeof(sa));
425sa.nl_family = AF_NETLINK;
426nh\->nlmsg_pid = 0;
427nh\->nlmsg_seq = ++sequence_number;
428/* Request an ack from kernel by setting NLM_F_ACK. */
429nh\->nlmsg_flags |= NLM_F_ACK;
430
431sendmsg(fd, &msg, 0);
432.fi
433.in
434
435And the last example is about reading netlink message.
436
437.in +4n
438.nf
439int len;
440char buf[4096];
441struct iovec iov = { buf, sizeof(buf) };
442struct sockaddr_nl sa;
443struct msghdr msg;
444struct nlmsghdr *nh;
445
13f78d96 446msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
77117f4f
MK
447len = recvmsg(fd, &msg, 0);
448
449for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
450 nh = NLMSG_NEXT (nh, len)) {
451 /* The end of multipart message. */
452 if (nh\->nlmsg_type == NLMSG_DONE)
453 return;
454
455 if (nh\->nlmsg_type == NLMSG_ERROR)
456 /* Do some error handling. */
457 ...
458
459 /* Continue with parsing payload. */
460 ...
461}
462.fi
463.in
464.SH "SEE ALSO"
465.BR cmsg (3),
466.BR netlink (3),
467.BR capabilities (7),
468.BR rtnetlink (7)
173fe7e7 469
608bf950
SK
470.UR ftp://ftp.inr.ac.ru\:/ip-routing\:/iproute2*
471information about libnetlink
472.UE
173fe7e7 473
608bf950
SK
474.UR http://people.suug.ch\:/~tgr\:/libnl/
475information about libnl
476.UE
173fe7e7 477
77117f4f 478RFC 3549 "Linux Netlink as an IP Services Protocol"