2 .\" This man page is Copyright (c) 1998 by Andi Kleen.
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" Subject to the GPL.
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"
13 netlink \- communication between kernel and user space (AF_NETLINK)
16 .B #include <asm/types.h>
17 .B #include <sys/socket.h>
18 .B #include <linux/netlink.h>
20 .BI "netlink_socket = socket(AF_NETLINK, " socket_type ", " netlink_family );
23 Netlink is used to transfer information between kernel and
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.
32 Netlink is a datagram-oriented service.
39 However, the netlink protocol does not distinguish between datagram
43 selects the kernel module or netlink group to communicate with.
44 The currently assigned netlink families are:
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
54 Messages from 1-wire subsystem.
57 Reserved for user-mode socket protocols.
60 Transport IPv4 packets from netfilter to user space.
66 .\" FIXME More details on NETLINK_INET_DIAG needed.
67 INET socket monitoring.
70 Netfilter/iptables ULOG.
73 .\" FIXME More details on NETLINK_XFRM needed.
77 SELinux event notifications.
80 .\" FIXME More details on NETLINK_ISCSI needed.
84 .\" FIXME More details on NETLINK_AUDIT needed.
88 .\" FIXME More details on NETLINK_FIB_LOOKUP needed.
89 Access to FIB lookup from user space.
94 .I Documentation/connector/*
95 in the Linux kernel source tree for further information.
98 .\" FIXME More details on NETLINK_NETFILTER needed.
102 Transport IPv6 packets from netfilter to user space.
108 DECnet routing messages.
110 .B NETLINK_KOBJECT_UEVENT
111 .\" FIXME More details on NETLINK_KOBJECT_UEVENT needed.
112 Kernel messages to user space.
115 Generic netlink family for simplified netlink usage.
117 Netlink messages consist of a byte stream with one or multiple
119 headers and associated payload.
120 The byte stream should be accessed only with the standard
125 for further information.
127 In multipart messages (multiple
129 headers with associated payload in one byte stream) the first and all
130 following headers have the
132 flag set, except for the last header which has the type
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. */
152 can be one of the standard message types:
154 message is to be ignored,
156 message signals an error and the payload contains an
160 message terminates a multipart message.
165 int error; /* Negative errno or 0 for acknowledgements */
166 struct nlmsghdr msg; /* Message header that caused the error */
171 A netlink family usually specifies more message types, see the
172 appropriate manual pages for that, for example,
180 Standard flag bits in \fInlmsg_flags\fP
182 NLM_F_REQUEST:Must be set on all request messages.
184 The message is part of a multipart message terminated by
187 NLM_F_ACK:Request for an acknowledgment on success.
188 NLM_F_ECHO:Echo this request.
191 .\" No right adjustment for text blocks in tables
196 Additional flag bits for GET requests
198 NLM_F_ROOT:Return the complete table instead of a single entry.
200 Return all entries matching criteria passed in message content.
203 .\" FIXME NLM_F_ATOMIC is not used any more?
204 NLM_F_ATOMIC:Return an atomic snapshot of the table.
206 Convenience macro; equivalent to
208 (NLM_F_ROOT|NLM_F_MATCH).
216 capability or an effective UID of 0.
221 Additional flag bits for NEW requests
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.
232 are used to track messages.
234 shows the origin of the message.
235 Note that there isn't a 1:1 relationship between
237 and the PID of the process if the message originated from a netlink
241 section for further information.
247 .\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
248 are opaque to netlink core.
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
254 For reliable transfer the sender can request an
255 acknowledgement from the receiver by setting the
258 An acknowledgment is an
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
265 message for every failed packet.
266 A user process should follow this convention too.
268 However, reliable transmissions from kernel to user are impossible
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
281 structure describes a netlink client in user space or in the kernel.
284 can be either unicast (only sent to one peer) or sent to
285 netlink multicast groups
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. */
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,
305 is usually the PID of the process owning the destination socket.
308 identifies a netlink socket, not a process.
309 If a process owns several netlink
312 can be equal to the process ID only for at most one socket.
313 There are two ways to assign
316 If the application sets
320 then it is up to the application to make sure that
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
327 to every netlink socket that the process subsequently creates.
330 is a bit mask with every bit representing a netlink group number.
331 Each netlink family has a set of 32 multicast groups.
334 is called on the socket, the
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
341 A socket may multicast messages to any of the multicast groups by setting
343 to a bit mask of the groups it wishes to send to when it calls
347 Only processes with an effective UID of 0 or the
349 capability may send or listen to a netlink multicast group.
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.
358 .BR NETLINK_KOBJECT_UEVENT ,
359 .BR NETLINK_GENERIC ,
363 groups allow other users to receive messages.
364 No groups allow other users to send messages.
366 The socket interface to netlink is a new feature of Linux 2.2.
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.
372 NETLINK_SELINUX appeared in Linux 2.6.4.
374 NETLINK_AUDIT appeared in Linux 2.6.6.
376 NETLINK_KOBJECT_UEVENT appeared in Linux 2.6.10.
378 NETLINK_W1 and NETLINK_FIB_LOOKUP appeared in Linux 2.6.13.
380 NETLINK_INET_DIAG, NETLINK_CONNECTOR and NETLINK_NETFILTER appeared in
383 NETLINK_GENERIC and NETLINK_ISCSI appeared in Linux 2.6.15.
385 It is often better to use netlink via
389 than via the low-level kernel interface.
391 This manual page is not complete.
393 The following example creates a
395 netlink socket which will listen to the
397 (network interface create/delete/up/down events) and
398 .B RTMGRP_IPV4_IFADDR
399 (IPv4 addresses add/delete events) multicast groups.
403 struct sockaddr_nl sa;
405 memset(&sa, 0, sizeof(sa));
406 sa.nl_family = AF_NETLINK;
407 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
409 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
410 bind(fd, (struct sockaddr *) &sa, sizeof(sa));
414 The next example demonstrates how to send a netlink message to the
416 Note that the application must take care of message sequence numbers
417 in order to reliably track acknowledgements.
421 struct nlmsghdr *nh; /* The nlmsghdr with payload to send. */
422 struct sockaddr_nl sa;
423 struct iovec iov = { nh, nh\->nlmsg_len };
426 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
427 memset(&sa, 0, sizeof(sa));
428 sa.nl_family = AF_NETLINK;
430 nh\->nlmsg_seq = ++sequence_number;
431 /* Request an ack from kernel by setting NLM_F_ACK. */
432 nh\->nlmsg_flags |= NLM_F_ACK;
434 sendmsg(fd, &msg, 0);
438 And the last example is about reading netlink message.
444 struct iovec iov = { buf, sizeof(buf) };
445 struct sockaddr_nl sa;
449 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
450 len = recvmsg(fd, &msg, 0);
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)
458 if (nh\->nlmsg_type == NLMSG_ERROR)
459 /* Do some error handling. */
462 /* Continue with parsing payload. */
470 .BR capabilities (7),
473 .UR ftp://ftp.inr.ac.ru\:/ip-routing\:/iproute2*
474 information about libnetlink
477 .UR http://people.suug.ch\:/~tgr\:/libnl/
478 information about libnl
481 RFC 3549 "Linux Netlink as an IP Services Protocol"
483 This page is part of release 3.68 of the Linux
486 A description of the project,
487 information about reporting bugs,
488 and the latest version of this page,
490 \%http://www.kernel.org/doc/man\-pages/.