1NETLINK(7) Linux Programmer's Manual NETLINK(7)
2
3
4
6 netlink - communication between kernel and user space (AF_NETLINK)
7
9 #include <asm/types.h>
10 #include <sys/socket.h>
11 #include <linux/netlink.h>
12
13 netlink_socket = socket(AF_NETLINK, socket_type, netlink_family);
14
16 Netlink is used to transfer information between the kernel and user-
17 space processes. It consists of a standard sockets-based interface for
18 user space processes and an internal kernel API for kernel modules.
19 The internal kernel interface is not documented in this manual page.
20 There is also an obsolete netlink interface via netlink character de‐
21 vices; this interface is not documented here and is provided only for
22 backward compatibility.
23
24 Netlink is a datagram-oriented service. Both SOCK_RAW and SOCK_DGRAM
25 are valid values for socket_type. However, the netlink protocol does
26 not distinguish between datagram and raw sockets.
27
28 netlink_family selects the kernel module or netlink group to communi‐
29 cate with. The currently assigned netlink families are:
30
31 NETLINK_ROUTE
32 Receives routing and link updates and may be used to modify the
33 routing tables (both IPv4 and IPv6), IP addresses, link parame‐
34 ters, neighbor setups, queueing disciplines, traffic classes and
35 packet classifiers (see rtnetlink(7)).
36
37 NETLINK_W1 (Linux 2.6.13 to 2.16.17)
38 Messages from 1-wire subsystem.
39
40 NETLINK_USERSOCK
41 Reserved for user-mode socket protocols.
42
43 NETLINK_FIREWALL (up to and including Linux 3.4)
44 Transport IPv4 packets from netfilter to user space. Used by
45 ip_queue kernel module. After a long period of being declared
46 obsolete (in favor of the more advanced nfnetlink_queue fea‐
47 ture), NETLINK_FIREWALL was removed in Linux 3.5.
48
49 NETLINK_SOCK_DIAG (since Linux 3.3)
50 Query information about sockets of various protocol families
51 from the kernel (see sock_diag(7)).
52
53 NETLINK_INET_DIAG (since Linux 2.6.14)
54 An obsolete synonym for NETLINK_SOCK_DIAG.
55
56 NETLINK_NFLOG (up to and including Linux 3.16)
57 Netfilter/iptables ULOG.
58
59 NETLINK_XFRM
60 IPsec.
61
62 NETLINK_SELINUX (since Linux 2.6.4)
63 SELinux event notifications.
64
65 NETLINK_ISCSI (since Linux 2.6.15)
66 Open-iSCSI.
67
68 NETLINK_AUDIT (since Linux 2.6.6)
69 Auditing.
70
71 NETLINK_FIB_LOOKUP (since Linux 2.6.13)
72 Access to FIB lookup from user space.
73
74 NETLINK_CONNECTOR (since Linux 2.6.14)
75 Kernel connector. See Documentation/driver-api/connector.rst
76 (or /Documentation/connector/connector.* in kernel 5.2 and ear‐
77 lier) in the Linux kernel source tree for further information.
78
79 NETLINK_NETFILTER (since Linux 2.6.14)
80 Netfilter subsystem.
81
82 NETLINK_SCSITRANSPORT (since Linux 2.6.19)
83 SCSI Transports.
84
85 NETLINK_RDMA (since Linux 3.0)
86 Infiniband RDMA.
87
88 NETLINK_IP6_FW (up to and including Linux 3.4)
89 Transport IPv6 packets from netfilter to user space. Used by
90 ip6_queue kernel module.
91
92 NETLINK_DNRTMSG
93 DECnet routing messages.
94
95 NETLINK_KOBJECT_UEVENT (since Linux 2.6.10)
96 Kernel messages to user space.
97
98 NETLINK_GENERIC (since Linux 2.6.15)
99 Generic netlink family for simplified netlink usage.
100
101 NETLINK_CRYPTO (since Linux 3.2)
102 Netlink interface to request information about ciphers regis‐
103 tered with the kernel crypto API as well as allow configuration
104 of the kernel crypto API.
105
106 Netlink messages consist of a byte stream with one or multiple nlmsghdr
107 headers and associated payload. The byte stream should be accessed
108 only with the standard NLMSG_* macros. See netlink(3) for further in‐
109 formation.
110
111 In multipart messages (multiple nlmsghdr headers with associated pay‐
112 load in one byte stream) the first and all following headers have the
113 NLM_F_MULTI flag set, except for the last header which has the type
114 NLMSG_DONE.
115
116 After each nlmsghdr the payload follows.
117
118 struct nlmsghdr {
119 __u32 nlmsg_len; /* Length of message including header */
120 __u16 nlmsg_type; /* Type of message content */
121 __u16 nlmsg_flags; /* Additional flags */
122 __u32 nlmsg_seq; /* Sequence number */
123 __u32 nlmsg_pid; /* Sender port ID */
124 };
125
126 nlmsg_type can be one of the standard message types: NLMSG_NOOP message
127 is to be ignored, NLMSG_ERROR message signals an error and the payload
128 contains an nlmsgerr structure, NLMSG_DONE message terminates a multi‐
129 part message.
130
131 struct nlmsgerr {
132 int error; /* Negative errno or 0 for acknowledgements */
133 struct nlmsghdr msg; /* Message header that caused the error */
134 };
135
136 A netlink family usually specifies more message types, see the appro‐
137 priate manual pages for that, for example, rtnetlink(7) for
138 NETLINK_ROUTE.
139
140 Standard flag bits in nlmsg_flags
141 ──────────────────────────────────────────────────────────
142 NLM_F_REQUEST Must be set on all request messages.
143 NLM_F_MULTI The message is part of a multipart mes‐
144 sage terminated by NLMSG_DONE.
145 NLM_F_ACK Request for an acknowledgment on success.
146 NLM_F_ECHO Echo this request.
147
148 Additional flag bits for GET requests
149 ────────────────────────────────────────────────────────────────────
150 NLM_F_ROOT Return the complete table instead of a single entry.
151 NLM_F_MATCH Return all entries matching criteria passed in mes‐
152 sage content. Not implemented yet.
153 NLM_F_ATOMIC Return an atomic snapshot of the table.
154 NLM_F_DUMP Convenience macro; equivalent to
155 (NLM_F_ROOT|NLM_F_MATCH).
156
157 Note that NLM_F_ATOMIC requires the CAP_NET_ADMIN capability or an ef‐
158 fective UID of 0.
159
160 Additional flag bits for NEW requests
161 ────────────────────────────────────────────────────────────
162 NLM_F_REPLACE Replace existing matching object.
163 NLM_F_EXCL Don't replace if the object already exists.
164 NLM_F_CREATE Create object if it doesn't already exist.
165 NLM_F_APPEND Add to the end of the object list.
166
167 nlmsg_seq and nlmsg_pid are used to track messages. nlmsg_pid shows
168 the origin of the message. Note that there isn't a 1:1 relationship
169 between nlmsg_pid and the PID of the process if the message originated
170 from a netlink socket. See the ADDRESS FORMATS section for further in‐
171 formation.
172
173 Both nlmsg_seq and nlmsg_pid are opaque to netlink core.
174
175 Netlink is not a reliable protocol. It tries its best to deliver a
176 message to its destination(s), but may drop messages when an out-of-
177 memory condition or other error occurs. For reliable transfer the
178 sender can request an acknowledgement from the receiver by setting the
179 NLM_F_ACK flag. An acknowledgment is an NLMSG_ERROR packet with the
180 error field set to 0. The application must generate acknowledgements
181 for received messages itself. The kernel tries to send an NLMSG_ERROR
182 message for every failed packet. A user process should follow this
183 convention too.
184
185 However, reliable transmissions from kernel to user are impossible in
186 any case. The kernel can't send a netlink message if the socket buffer
187 is full: the message will be dropped and the kernel and the user-space
188 process will no longer have the same view of kernel state. It is up to
189 the application to detect when this happens (via the ENOBUFS error re‐
190 turned by recvmsg(2)) and resynchronize.
191
192 Address formats
193 The sockaddr_nl structure describes a netlink client in user space or
194 in the kernel. A sockaddr_nl can be either unicast (only sent to one
195 peer) or sent to netlink multicast groups (nl_groups not equal 0).
196
197 struct sockaddr_nl {
198 sa_family_t nl_family; /* AF_NETLINK */
199 unsigned short nl_pad; /* Zero */
200 pid_t nl_pid; /* Port ID */
201 __u32 nl_groups; /* Multicast groups mask */
202 };
203
204 nl_pid is the unicast address of netlink socket. It's always 0 if the
205 destination is in the kernel. For a user-space process, nl_pid is usu‐
206 ally the PID of the process owning the destination socket. However,
207 nl_pid identifies a netlink socket, not a process. If a process owns
208 several netlink sockets, then nl_pid can be equal to the process ID
209 only for at most one socket. There are two ways to assign nl_pid to a
210 netlink socket. If the application sets nl_pid before calling bind(2),
211 then it is up to the application to make sure that nl_pid is unique.
212 If the application sets it to 0, the kernel takes care of assigning it.
213 The kernel assigns the process ID to the first netlink socket the
214 process opens and assigns a unique nl_pid to every netlink socket that
215 the process subsequently creates.
216
217 nl_groups is a bit mask with every bit representing a netlink group
218 number. Each netlink family has a set of 32 multicast groups. When
219 bind(2) is called on the socket, the nl_groups field in the sockaddr_nl
220 should be set to a bit mask of the groups which it wishes to listen to.
221 The default value for this field is zero which means that no multicasts
222 will be received. A socket may multicast messages to any of the multi‐
223 cast groups by setting nl_groups to a bit mask of the groups it wishes
224 to send to when it calls sendmsg(2) or does a connect(2). Only pro‐
225 cesses with an effective UID of 0 or the CAP_NET_ADMIN capability may
226 send or listen to a netlink multicast group. Since Linux 2.6.13, mes‐
227 sages can't be broadcast to multiple groups. Any replies to a message
228 received for a multicast group should be sent back to the sending PID
229 and the multicast group. Some Linux kernel subsystems may additionally
230 allow other users to send and/or receive messages. As at Linux 3.0,
231 the NETLINK_KOBJECT_UEVENT, NETLINK_GENERIC, NETLINK_ROUTE, and
232 NETLINK_SELINUX groups allow other users to receive messages. No
233 groups allow other users to send messages.
234
235 Socket options
236 To set or get a netlink socket option, call getsockopt(2) to read or
237 setsockopt(2) to write the option with the option level argument set to
238 SOL_NETLINK. Unless otherwise noted, optval is a pointer to an int.
239
240 NETLINK_PKTINFO (since Linux 2.6.14)
241 Enable nl_pktinfo control messages for received packets to get
242 the extended destination group number.
243
244 NETLINK_ADD_MEMBERSHIP, NETLINK_DROP_MEMBERSHIP (since Linux 2.6.14)
245 Join/leave a group specified by optval.
246
247 NETLINK_LIST_MEMBERSHIPS (since Linux 4.2)
248 Retrieve all groups a socket is a member of. optval is a
249 pointer to __u32 and optlen is the size of the array. The array
250 is filled with the full membership set of the socket, and the
251 required array size is returned in optlen.
252
253 NETLINK_BROADCAST_ERROR (since Linux 2.6.30)
254 When not set, netlink_broadcast() only reports ESRCH errors and
255 silently ignore ENOBUFS errors.
256
257 NETLINK_NO_ENOBUFS (since Linux 2.6.30)
258 This flag can be used by unicast and broadcast listeners to
259 avoid receiving ENOBUFS errors.
260
261 NETLINK_LISTEN_ALL_NSID (since Linux 4.2)
262 When set, this socket will receive netlink notifications from
263 all network namespaces that have an nsid assigned into the net‐
264 work namespace where the socket has been opened. The nsid is
265 sent to user space via an ancillary data.
266
267 NETLINK_CAP_ACK (since Linux 4.2)
268 The kernel may fail to allocate the necessary room for the ac‐
269 knowledgment message back to user space. This option trims off
270 the payload of the original netlink message. The netlink mes‐
271 sage header is still included, so the user can guess from the
272 sequence number which message triggered the acknowledgment.
273
275 The socket interface to netlink first appeared Linux 2.2.
276
277 Linux 2.0 supported a more primitive device-based netlink interface
278 (which is still available as a compatibility option). This obsolete
279 interface is not described here.
280
282 It is often better to use netlink via libnetlink or libnl than via the
283 low-level kernel interface.
284
286 This manual page is not complete.
287
289 The following example creates a NETLINK_ROUTE netlink socket which will
290 listen to the RTMGRP_LINK (network interface create/delete/up/down
291 events) and RTMGRP_IPV4_IFADDR (IPv4 addresses add/delete events) mul‐
292 ticast groups.
293
294 struct sockaddr_nl sa;
295
296 memset(&sa, 0, sizeof(sa));
297 sa.nl_family = AF_NETLINK;
298 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
299
300 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
301 bind(fd, (struct sockaddr *) &sa, sizeof(sa));
302
303 The next example demonstrates how to send a netlink message to the ker‐
304 nel (pid 0). Note that the application must take care of message se‐
305 quence numbers in order to reliably track acknowledgements.
306
307 struct nlmsghdr *nh; /* The nlmsghdr with payload to send */
308 struct sockaddr_nl sa;
309 struct iovec iov = { nh, nh->nlmsg_len };
310 struct msghdr msg;
311
312 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
313 memset(&sa, 0, sizeof(sa));
314 sa.nl_family = AF_NETLINK;
315 nh->nlmsg_pid = 0;
316 nh->nlmsg_seq = ++sequence_number;
317 /* Request an ack from kernel by setting NLM_F_ACK */
318 nh->nlmsg_flags |= NLM_F_ACK;
319
320 sendmsg(fd, &msg, 0);
321
322 And the last example is about reading netlink message.
323
324 int len;
325 /* 8192 to avoid message truncation on platforms with
326 page size > 4096 */
327 struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)];
328 struct iovec iov = { buf, sizeof(buf) };
329 struct sockaddr_nl sa;
330 struct msghdr msg;
331 struct nlmsghdr *nh;
332
333 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
334 len = recvmsg(fd, &msg, 0);
335
336 for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
337 nh = NLMSG_NEXT (nh, len)) {
338 /* The end of multipart message */
339 if (nh->nlmsg_type == NLMSG_DONE)
340 return;
341
342 if (nh->nlmsg_type == NLMSG_ERROR)
343 /* Do some error handling */
344 ...
345
346 /* Continue with parsing payload */
347 ...
348 }
349
351 cmsg(3), netlink(3), capabilities(7), rtnetlink(7), sock_diag(7)
352
353 information about libnetlink ⟨ftp://ftp.inr.ac.ru/ip-routing/iproute2*⟩
354
355 information about libnl ⟨http://www.infradead.org/~tgr/libnl/⟩
356
357 RFC 3549 "Linux Netlink as an IP Services Protocol"
358
360 This page is part of release 5.10 of the Linux man-pages project. A
361 description of the project, information about reporting bugs, and the
362 latest version of this page, can be found at
363 https://www.kernel.org/doc/man-pages/.
364
365
366
367Linux 2020-06-09 NETLINK(7)