OSDN Git Service

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