1netlink(7)             Miscellaneous Information Manual             netlink(7)
2
3
4

NAME

6       netlink - communication between kernel and user space (AF_NETLINK)
7

SYNOPSIS

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

DESCRIPTION

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,
35              and packet classifiers (see rtnetlink(7)).
36
37       NETLINK_W1 (Linux 2.6.13 to Linux 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 Linux 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.  Error messages get the original request appended, unless
130       the user requests to cap the error message, and get extra error data if
131       requested.
132
133           struct nlmsgerr {
134               int error;        /* Negative errno or 0 for acknowledgements */
135               struct nlmsghdr msg;  /* Message header that caused the error */
136               /*
137                * followed by the message contents
138                * unless NETLINK_CAP_ACK was set
139                * or the ACK indicates success (error == 0).
140                * For example Generic Netlink message with attributes.
141                * message length is aligned with NLMSG_ALIGN()
142                */
143               /*
144                * followed by TLVs defined in enum nlmsgerr_attrs
145                * if NETLINK_EXT_ACK was set
146                */
147           };
148
149       A  netlink  family usually specifies more message types, see the appro‐
150       priate  manual  pages  for  that,   for   example,   rtnetlink(7)   for
151       NETLINK_ROUTE.
152
153       Standard flag bits in nlmsg_flags
154       ────────────────────────────────────────────────────────────────────────
155       NLM_F_REQUEST           Must be set on all request messages.
156       NLM_F_MULTI             The message is part of a multipart message
157                               terminated by NLMSG_DONE.
158       NLM_F_ACK               Request for an acknowledgement on success.
159       NLM_F_ECHO              Echo this request.
160
161       Additional flag bits for GET requests
162       ────────────────────────────────────────────────────────────────────────
163       NLM_F_ROOT               Return the complete table instead of a single
164                                entry.
165       NLM_F_MATCH              Return all entries matching criteria passed
166                                in message content.  Not implemented yet.
167       NLM_F_ATOMIC             Return an atomic snapshot of the table.
168       NLM_F_DUMP               Convenience macro; equivalent to
169                                (NLM_F_ROOT|NLM_F_MATCH).
170
171       Note  that NLM_F_ATOMIC requires the CAP_NET_ADMIN capability or an ef‐
172       fective UID of 0.
173
174       Additional flag bits for NEW requests
175       ────────────────────────────────────────────────────────────────────────
176       NLM_F_REPLACE             Replace existing matching object.
177       NLM_F_EXCL                Don't replace if the object already exists.
178       NLM_F_CREATE              Create object if it doesn't already exist.
179       NLM_F_APPEND              Add to the end of the object list.
180
181       nlmsg_seq and nlmsg_pid are used to track  messages.   nlmsg_pid  shows
182       the  origin  of  the message.  Note that there isn't a 1:1 relationship
183       between nlmsg_pid and the PID of the process if the message  originated
184       from a netlink socket.  See the ADDRESS FORMATS section for further in‐
185       formation.
186
187       Both nlmsg_seq and nlmsg_pid are opaque to netlink core.
188
189       Netlink is not a reliable protocol.  It tries its  best  to  deliver  a
190       message  to  its  destination(s), but may drop messages when an out-of-
191       memory condition or other error  occurs.   For  reliable  transfer  the
192       sender  can request an acknowledgement from the receiver by setting the
193       NLM_F_ACK flag.  An acknowledgement is an NLMSG_ERROR packet  with  the
194       error  field  set to 0.  The application must generate acknowledgements
195       for received messages itself.  The kernel tries to send an  NLMSG_ERROR
196       message  for  every  failed  packet.  A user process should follow this
197       convention too.
198
199       However, reliable transmissions from kernel to user are  impossible  in
200       any case.  The kernel can't send a netlink message if the socket buffer
201       is full: the message will be dropped and the kernel and the  user-space
202       process will no longer have the same view of kernel state.  It is up to
203       the application to detect when this happens (via the ENOBUFS error  re‐
204       turned by recvmsg(2)) and resynchronize.
205
206   Address formats
207       The  sockaddr_nl  structure describes a netlink client in user space or
208       in the kernel.  A sockaddr_nl can be either unicast (only sent  to  one
209       peer) or sent to netlink multicast groups (nl_groups not equal 0).
210
211           struct sockaddr_nl {
212               sa_family_t     nl_family;  /* AF_NETLINK */
213               unsigned short  nl_pad;     /* Zero */
214               pid_t           nl_pid;     /* Port ID */
215               __u32           nl_groups;  /* Multicast groups mask */
216           };
217
218       nl_pid  is the unicast address of netlink socket.  It's always 0 if the
219       destination is in the kernel.  For a user-space process, nl_pid is usu‐
220       ally  the  PID  of the process owning the destination socket.  However,
221       nl_pid identifies a netlink socket, not a process.  If a  process  owns
222       several  netlink  sockets,  then  nl_pid can be equal to the process ID
223       only for at most one socket.  There are two ways to assign nl_pid to  a
224       netlink socket.  If the application sets nl_pid before calling bind(2),
225       then it is up to the application to make sure that  nl_pid  is  unique.
226       If the application sets it to 0, the kernel takes care of assigning it.
227       The kernel assigns the process ID  to  the  first  netlink  socket  the
228       process  opens and assigns a unique nl_pid to every netlink socket that
229       the process subsequently creates.
230
231       nl_groups is a bit mask with every bit  representing  a  netlink  group
232       number.   Each  netlink  family has a set of 32 multicast groups.  When
233       bind(2) is called on the socket, the nl_groups field in the sockaddr_nl
234       should be set to a bit mask of the groups which it wishes to listen to.
235       The default value for this field is zero which means that no multicasts
236       will be received.  A socket may multicast messages to any of the multi‐
237       cast groups by setting nl_groups to a bit mask of the groups it  wishes
238       to  send  to  when it calls sendmsg(2) or does a connect(2).  Only pro‐
239       cesses with an effective UID of 0 or the CAP_NET_ADMIN  capability  may
240       send  or listen to a netlink multicast group.  Since Linux 2.6.13, mes‐
241       sages can't be broadcast to multiple groups.  Any replies to a  message
242       received  for  a multicast group should be sent back to the sending PID
243       and the multicast group.  Some Linux kernel subsystems may additionally
244       allow  other  users  to send and/or receive messages.  As at Linux 3.0,
245       the   NETLINK_KOBJECT_UEVENT,   NETLINK_GENERIC,   NETLINK_ROUTE,   and
246       NETLINK_SELINUX  groups  allow  other  users  to  receive messages.  No
247       groups allow other users to send messages.
248
249   Socket options
250       To set or get a netlink socket option, call getsockopt(2)  to  read  or
251       setsockopt(2) to write the option with the option level argument set to
252       SOL_NETLINK.  Unless otherwise noted, optval is a pointer to an int.
253
254       NETLINK_PKTINFO (since Linux 2.6.14)
255              Enable nl_pktinfo control messages for received packets  to  get
256              the extended destination group number.
257
258       NETLINK_ADD_MEMBERSHIP, NETLINK_DROP_MEMBERSHIP (since Linux 2.6.14)
259              Join/leave a group specified by optval.
260
261       NETLINK_LIST_MEMBERSHIPS (since Linux 4.2)
262              Retrieve  all  groups  a  socket  is  a  member of.  optval is a
263              pointer to __u32 and optlen is the size of the array.  The array
264              is  filled  with  the full membership set of the socket, and the
265              required array size is returned in optlen.
266
267       NETLINK_BROADCAST_ERROR (since Linux 2.6.30)
268              When not set, netlink_broadcast() only reports ESRCH errors  and
269              silently ignore ENOBUFS errors.
270
271       NETLINK_NO_ENOBUFS (since Linux 2.6.30)
272              This  flag  can  be  used  by unicast and broadcast listeners to
273              avoid receiving ENOBUFS errors.
274
275       NETLINK_LISTEN_ALL_NSID (since Linux 4.2)
276              When set, this socket will receive  netlink  notifications  from
277              all  network namespaces that have an nsid assigned into the net‐
278              work namespace where the socket has been opened.   The  nsid  is
279              sent to user space via an ancillary data.
280
281       NETLINK_CAP_ACK (since Linux 4.3)
282              The  kernel  may fail to allocate the necessary room for the ac‐
283              knowledgement message back to user space.  This option trims off
284              the  payload  of the original netlink message.  The netlink mes‐
285              sage header is still included, so the user can  guess  from  the
286              sequence number which message triggered the acknowledgement.
287

VERSIONS

289       The socket interface to netlink first appeared Linux 2.2.
290
291       Linux  2.0  supported  a  more primitive device-based netlink interface
292       (which is still available as a compatibility  option).   This  obsolete
293       interface is not described here.
294

NOTES

296       It  is often better to use netlink via libnetlink or libnl than via the
297       low-level kernel interface.
298

BUGS

300       This manual page is not complete.
301

EXAMPLES

303       The following example creates a NETLINK_ROUTE netlink socket which will
304       listen  to  the  RTMGRP_LINK  (network  interface create/delete/up/down
305       events) and RTMGRP_IPV4_IFADDR (IPv4 addresses add/delete events)  mul‐
306       ticast groups.
307
308           struct sockaddr_nl sa;
309
310           memset(&sa, 0, sizeof(sa));
311           sa.nl_family = AF_NETLINK;
312           sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
313
314           fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
315           bind(fd, (struct sockaddr *) &sa, sizeof(sa));
316
317       The next example demonstrates how to send a netlink message to the ker‐
318       nel (pid 0).  Note that the application must take care of  message  se‐
319       quence numbers in order to reliably track acknowledgements.
320
321           struct nlmsghdr *nh;    /* The nlmsghdr with payload to send */
322           struct sockaddr_nl sa;
323           struct iovec iov = { nh, nh->nlmsg_len };
324           struct msghdr msg;
325
326           msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
327           memset(&sa, 0, sizeof(sa));
328           sa.nl_family = AF_NETLINK;
329           nh->nlmsg_pid = 0;
330           nh->nlmsg_seq = ++sequence_number;
331           /* Request an ack from kernel by setting NLM_F_ACK */
332           nh->nlmsg_flags |= NLM_F_ACK;
333
334           sendmsg(fd, &msg, 0);
335
336       And the last example is about reading netlink message.
337
338           int len;
339           /* 8192 to avoid message truncation on platforms with
340              page size > 4096 */
341           struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)];
342           struct iovec iov = { buf, sizeof(buf) };
343           struct sockaddr_nl sa;
344           struct msghdr msg;
345           struct nlmsghdr *nh;
346
347           msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
348           len = recvmsg(fd, &msg, 0);
349
350           for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
351                nh = NLMSG_NEXT (nh, len)) {
352               /* The end of multipart message */
353               if (nh->nlmsg_type == NLMSG_DONE)
354                   return;
355
356               if (nh->nlmsg_type == NLMSG_ERROR)
357                   /* Do some error handling */
358               ...
359
360               /* Continue with parsing payload */
361               ...
362           }
363

SEE ALSO

365       cmsg(3), netlink(3), capabilities(7), rtnetlink(7), sock_diag(7)
366
367       information about libnetlink ⟨ftp://ftp.inr.ac.ru/ip-routing/iproute2*⟩
368
369       information about libnl ⟨http://www.infradead.org/~tgr/libnl/⟩
370
371       RFC 3549 "Linux Netlink as an IP Services Protocol"
372
373
374
375Linux man-pages 6.04              2023-02-05                        netlink(7)
Impressum