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 $
7 .TH NETLINK 7 2008-11-11 "Linux" "Linux Programmer's Manual"
9 netlink \- Communication between kernel and userspace (AF_NETLINK)
12 .B #include <asm/types.h>
13 .B #include <sys/socket.h>
14 .B #include <linux/netlink.h>
16 .BI "netlink_socket = socket(AF_NETLINK, " socket_type ", " netlink_family );
19 Netlink is used to transfer information between kernel and
21 It consists of a standard sockets-based interface for userspace
22 processes and an internal kernel API for kernel modules.
23 The internal kernel interface is not documented in this manual page.
24 There is also an obsolete netlink interface
25 via netlink character devices; this interface is not documented here
26 and is only provided for backwards compatibility.
28 Netlink is a datagram-oriented service.
35 However, the netlink protocol does not distinguish between datagram
39 selects the kernel module or netlink group to communicate with.
40 The currently assigned netlink families are:
43 Receives routing and link updates and may be used to modify the routing
44 tables (both IPv4 and IPv6), IP addresses, link parameters,
45 neighbor setups, queueing disciplines, traffic classes and
46 packet classifiers (see
50 Messages from 1-wire subsystem.
53 Reserved for user-mode socket protocols.
56 Transport IPv4 packets from netfilter to userspace.
62 .\" FIXME More details on NETLINK_INET_DIAG needed.
63 INET socket monitoring.
66 Netfilter/iptables ULOG.
69 .\" FIXME More details on NETLINK_XFRM needed.
73 SELinux event notifications.
76 .\" FIXME More details on NETLINK_ISCSI needed.
80 .\" FIXME More details on NETLINK_AUDIT needed.
84 .\" FIXME More details on NETLINK_FIB_LOOKUP needed.
85 Access to FIB lookup from userspace.
90 .I Documentation/connector/*
91 in the kernel source for further information.
94 .\" FIXME More details on NETLINK_NETFILTER needed.
98 Transport IPv6 packets from netfilter to userspace.
104 DECnet routing messages.
106 .B NETLINK_KOBJECT_UEVENT
107 .\" FIXME More details on NETLINK_KOBJECT_UEVENT needed.
108 Kernel messages to userspace.
111 Generic netlink family for simplified netlink usage.
113 Netlink messages consist of a byte stream with one or multiple
115 headers and associated payload.
116 The byte stream should only be accessed with the standard
121 for further information.
123 In multipart messages (multiple
125 headers with associated payload in one byte stream) the first and all
126 following headers have the
128 flag set, except for the last header which has the type
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. */
142 __u32 nlmsg_pid; /* PID of the sending process. */
148 can be one of the standard message types:
150 message is to be ignored,
152 message signals an error and the payload contains an
156 message terminates a multipart message.
161 int error; /* Negative errno or 0 for acknowledgements */
162 struct nlmsghdr msg; /* Message header that caused the error */
167 A netlink family usually specifies more message types, see the
168 appropriate manual pages for that, for example,
173 Standard flag bits in
176 ---------------------------------
180 NLM_F_REQUEST:Must be set on all request messages.
182 The message is part of a multipart message terminated by
185 NLM_F_ACK:Request for an acknowledgment on success.
186 NLM_F_ECHO:Echo this request.
189 Additional flag bits for GET requests
191 -------------------------------------
195 NLM_F_ROOT:Return the complete table instead of a single entry.
197 Return all entries matching criteria passed in message content.
200 .\" FIXME NLM_F_ATOMIC is not used any more?
201 NLM_F_ATOMIC:Return an atomic snapshot of the table.
202 NLM_F_DUMP:Convenience macro; equivalent to (NLM_F_ROOT|NLM_F_MATCH).
209 capability or an effective UID of 0.
211 Additional flag bits for NEW requests
213 -------------------------------------
217 NLM_F_REPLACE:Replace existing matching object.
218 NLM_F_EXCL:Don't replace if the object already exists.
219 NLM_F_CREATE:Create object if it doesn't already exist.
220 NLM_F_APPEND:Add to the end of the object list.
226 are used to track messages.
228 shows the origin of the message.
229 Note that there isn't a 1:1 relationship between
231 and the PID of the process if the message originated from a netlink
235 section for further information.
241 .\" FIXME Explain more about nlmsg_seq and nlmsg_pid.
242 are opaque to netlink core.
244 Netlink is not a reliable protocol.
245 It tries its best to deliver a message to its destination(s),
246 but may drop messages when an out-of-memory condition or
248 For reliable transfer the sender can request an
249 acknowledgement from the receiver by setting the
252 An acknowledgment is an
254 packet with the error field set to 0.
255 The application must generate acknowledgements for
256 received messages itself.
257 The kernel tries to send an
259 message for every failed packet.
260 A user process should follow this convention too.
262 However, reliable transmissions from kernel to user are impossible
264 The kernel can't send a netlink message if the socket buffer is full:
265 the message will be dropped and the kernel and the userspace process will
266 no longer have the same view of kernel state.
267 It is up to the application to detect when this happens (via the
275 structure describes a netlink client in user space or in the kernel.
278 can be either unicast (only sent to one peer) or sent to
279 netlink multicast groups
286 sa_family_t nl_family; /* AF_NETLINK */
287 unsigned short nl_pad; /* Zero. */
288 pid_t nl_pid; /* Process ID. */
289 __u32 nl_groups; /* Multicast groups mask. */
295 is the unicast address of netlink socket.
296 It's always 0 if the destination is in the kernel.
297 For a userspace process,
299 is usually the PID of the process owning the destination socket.
302 identifies a netlink socket, not a process.
303 If a process owns several netlink
306 can only be equal to the process ID for at most one socket.
307 There are two ways to assign
310 If the application sets
314 then it is up to the application to make sure that
317 If the application sets it to 0, the kernel takes care of assigning it.
318 The kernel assigns the process ID to the first netlink socket the process
319 opens and assigns a unique
321 to every netlink socket that the process subsequently creates.
324 is a bit mask with every bit representing a netlink group number.
325 Each netlink family has a set of 32 multicast groups.
328 is called on the socket, the
332 should be set to a bit mask of the groups which it wishes to listen to.
333 The default value for this field is zero which means that no multicasts
335 A socket may multicast messages to any of the multicast groups by setting
337 to a bit mask of the groups it wishes to send to when it calls
341 Only processes with an effective UID of 0 or the
343 capability may send or listen to a netlink multicast group.
344 Any replies to a message received for a multicast group should be
345 sent back to the sending PID and the multicast group.
347 The socket interface to netlink is a new feature of Linux 2.2.
349 Linux 2.0 supported a more primitive device based netlink interface
350 (which is still available as a compatibility option).
351 This obsolete interface is not described here.
353 NETLINK_SELINUX appeared in Linux 2.6.4.
355 NETLINK_AUDIT appeared in Linux 2.6.6.
357 NETLINK_KOBJECT_UEVENT appeared in Linux 2.6.10.
359 NETLINK_W1 and NETLINK_FIB_LOOKUP appeared in Linux 2.6.13.
361 NETLINK_INET_DIAG, NETLINK_CONNECTOR and NETLINK_NETFILTER appeared in
364 NETLINK_GENERIC and NETLINK_ISCSI appeared in Linux 2.6.15.
366 It is often better to use netlink via
370 than via the low-level kernel interface.
372 This manual page is not complete.
374 The following example creates a
376 netlink socket which will listen to the
378 (network interface create/delete/up/down events) and
379 .B RTMGRP_IPV4_IFADDR
380 (IPv4 addresses add/delete events) multicast groups.
384 struct sockaddr_nl sa;
386 memset(&sa, 0, sizeof(sa));
387 sa.nl_family = AF_NETLINK;
388 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
390 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
391 bind(fd, (struct sockaddr *) &sa, sizeof(sa));
395 The next example demonstrates how to send a netlink message to the
397 Note that application must take care of message sequence numbers
398 in order to reliably track acknowledgements.
402 struct nlmsghdr *nh; /* The nlmsghdr with payload to send. */
403 struct sockaddr_nl sa;
404 struct iovec iov = { (void *) nh, nh\->nlmsg_len };
407 msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
408 memset(&sa, 0, sizeof(sa));
409 sa.nl_family = AF_NETLINK;
411 nh\->nlmsg_seq = ++sequence_number;
412 /* Request an ack from kernel by setting NLM_F_ACK. */
413 nh\->nlmsg_flags |= NLM_F_ACK;
415 sendmsg(fd, &msg, 0);
419 And the last example is about reading netlink message.
425 struct iovec iov = { buf, sizeof(buf) };
426 struct sockaddr_nl sa;
430 msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
431 len = recvmsg(fd, &msg, 0);
433 for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
434 nh = NLMSG_NEXT (nh, len)) {
435 /* The end of multipart message. */
436 if (nh\->nlmsg_type == NLMSG_DONE)
439 if (nh\->nlmsg_type == NLMSG_ERROR)
440 /* Do some error handling. */
443 /* Continue with parsing payload. */
451 .BR capabilities (7),
454 ftp://ftp.inr.ac.ru/ip-routing/iproute2*
455 for information about libnetlink.
457 http://people.suug.ch/~tgr/libnl/
458 for information about libnl.
460 RFC 3549 "Linux Netlink as an IP Services Protocol"