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

VERSIONS

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

NOTES

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

BUGS

299       This manual page is not complete.
300

EXAMPLES

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

SEE ALSO

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

COLOPHON

373       This  page  is  part of release 5.13 of the Linux man-pages project.  A
374       description of the project, information about reporting bugs,  and  the
375       latest     version     of     this    page,    can    be    found    at
376       https://www.kernel.org/doc/man-pages/.
377
378
379
380Linux                             2021-03-22                        NETLINK(7)
Impressum