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.
192 .\" No right adjustment for text blocks in tables
198 Additional flag bits for GET requests
200 NLM_F_ROOT:Return the complete table instead of a single entry.
202 Return all entries matching criteria passed in message content.
205 .\" FIXME NLM_F_ATOMIC is not used any more?
206 NLM_F_ATOMIC:Return an atomic snapshot of the table.
208 Convenience macro; equivalent to (NLM_F_ROOT|NLM_F_MATCH).
217 capability or an effective UID of 0.
223 Additional flag bits for NEW requests
225 NLM_F_REPLACE:Replace existing matching object.
226 NLM_F_EXCL:Don't replace if the object already exists.
227 NLM_F_CREATE:Create object if it doesn't already exist.
228 NLM_F_APPEND:Add to the end of the object list.
235 are used to track messages.
237 shows the origin of the message.
238 Note that there isn't a 1:1 relationship between
240 and the PID of the process if the message originated from a netlink
244 section for further information.
250 .\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
251 are opaque to netlink core.
253 Netlink is not a reliable protocol.
254 It tries its best to deliver a message to its destination(s),
255 but may drop messages when an out-of-memory condition or
257 For reliable transfer the sender can request an
258 acknowledgement from the receiver by setting the
261 An acknowledgment is an
263 packet with the error field set to 0.
264 The application must generate acknowledgements for
265 received messages itself.
266 The kernel tries to send an
268 message for every failed packet.
269 A user process should follow this convention too.
271 However, reliable transmissions from kernel to user are impossible
273 The kernel can't send a netlink message if the socket buffer is full:
274 the message will be dropped and the kernel and the user-space process will
275 no longer have the same view of kernel state.
276 It is up to the application to detect when this happens (via the
284 structure describes a netlink client in user space or in the kernel.
287 can be either unicast (only sent to one peer) or sent to
288 netlink multicast groups
295 sa_family_t nl_family; /* AF_NETLINK */
296 unsigned short nl_pad; /* Zero. */
297 pid_t nl_pid; /* Port ID. */
298 __u32 nl_groups; /* Multicast groups mask. */
304 is the unicast address of netlink socket.
305 It's always 0 if the destination is in the kernel.
306 For a user-space process,
308 is usually the PID of the process owning the destination socket.
311 identifies a netlink socket, not a process.
312 If a process owns several netlink
315 can be equal to the process ID only for at most one socket.
316 There are two ways to assign
319 If the application sets
323 then it is up to the application to make sure that
326 If the application sets it to 0, the kernel takes care of assigning it.
327 The kernel assigns the process ID to the first netlink socket the process
328 opens and assigns a unique
330 to every netlink socket that the process subsequently creates.
333 is a bit mask with every bit representing a netlink group number.
334 Each netlink family has a set of 32 multicast groups.
337 is called on the socket, the
341 should be set to a bit mask of the groups which it wishes to listen to.
342 The default value for this field is zero which means that no multicasts
344 A socket may multicast messages to any of the multicast groups by setting
346 to a bit mask of the groups it wishes to send to when it calls
350 Only processes with an effective UID of 0 or the
352 capability may send or listen to a netlink multicast group.
354 .\" commit d629b836d151d43332492651dd841d32e57ebe3b
355 messages can't be broadcast to multiple groups.
356 Any replies to a message received for a multicast group should be
357 sent back to the sending PID and the multicast group.
358 Some Linux kernel subsystems may additionally allow other users
359 to send and/or receive messages.
361 .BR NETLINK_KOBJECT_UEVENT ,
362 .BR NETLINK_GENERIC ,
366 groups allow other users to receive messages.
367 No groups allow other users to send messages.
369 The socket interface to netlink is a new feature of Linux 2.2.
371 Linux 2.0 supported a more primitive device-based netlink interface
372 (which is still available as a compatibility option).
373 This obsolete interface is not described here.
375 NETLINK_SELINUX appeared in Linux 2.6.4.
377 NETLINK_AUDIT appeared in Linux 2.6.6.
379 NETLINK_KOBJECT_UEVENT appeared in Linux 2.6.10.
381 NETLINK_W1 and NETLINK_FIB_LOOKUP appeared in Linux 2.6.13.
383 NETLINK_INET_DIAG, NETLINK_CONNECTOR and NETLINK_NETFILTER appeared in
386 NETLINK_GENERIC and NETLINK_ISCSI appeared in Linux 2.6.15.
388 It is often better to use netlink via
392 than via the low-level kernel interface.
394 This manual page is not complete.
396 The following example creates a
398 netlink socket which will listen to the
400 (network interface create/delete/up/down events) and
401 .B RTMGRP_IPV4_IFADDR
402 (IPv4 addresses add/delete events) multicast groups.
406 struct sockaddr_nl sa;
408 memset(&sa, 0, sizeof(sa));
409 sa.nl_family = AF_NETLINK;
410 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
412 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
413 bind(fd, (struct sockaddr *) &sa, sizeof(sa));
417 The next example demonstrates how to send a netlink message to the
419 Note that application must take care of message sequence numbers
420 in order to reliably track acknowledgements.
424 struct nlmsghdr *nh; /* The nlmsghdr with payload to send. */
425 struct sockaddr_nl sa;
426 struct iovec iov = { nh, nh\->nlmsg_len };
429 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
430 memset(&sa, 0, sizeof(sa));
431 sa.nl_family = AF_NETLINK;
433 nh\->nlmsg_seq = ++sequence_number;
434 /* Request an ack from kernel by setting NLM_F_ACK. */
435 nh\->nlmsg_flags |= NLM_F_ACK;
437 sendmsg(fd, &msg, 0);
441 And the last example is about reading netlink message.
447 struct iovec iov = { buf, sizeof(buf) };
448 struct sockaddr_nl sa;
452 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
453 len = recvmsg(fd, &msg, 0);
455 for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
456 nh = NLMSG_NEXT (nh, len)) {
457 /* The end of multipart message. */
458 if (nh\->nlmsg_type == NLMSG_DONE)
461 if (nh\->nlmsg_type == NLMSG_ERROR)
462 /* Do some error handling. */
465 /* Continue with parsing payload. */
473 .BR capabilities (7),
476 .UR ftp://ftp.inr.ac.ru\:/ip-routing\:/iproute2*
477 information about libnetlink
480 .UR http://people.suug.ch\:/~tgr\:/libnl/
481 information about libnl
484 RFC 3549 "Linux Netlink as an IP Services Protocol"