# phpman > man > netlink(7)

[NETLINK(7)](https://www.chedong.com/phpMan.php/man/NETLINK/7/markdown)                            Linux Programmer's Manual                           [NETLINK(7)](https://www.chedong.com/phpMan.php/man/NETLINK/7/markdown)



## NAME
       netlink - communication between kernel and user space (AF_NETLINK)

## SYNOPSIS
### #include <asm/types.h>
### #include <sys/socket.h>
### #include <linux/netlink.h>

       **netlink**___**socket** **=** **socket(AF**___**NETLINK,** _socket_type_**,** _netlink_family_**);**

## DESCRIPTION
       Netlink is used to transfer information between the kernel and user-space processes.  It con‐
       sists of a standard sockets-based interface for user space processes and an  internal  kernel
       API for kernel modules.  The internal kernel interface is not documented in this manual page.
       There is also an obsolete netlink interface via netlink character devices; this interface  is
       not documented here and is provided only for backward compatibility.

       Netlink  is  a  datagram-oriented service.  Both **SOCK**___**RAW** and **SOCK**___**DGRAM** are valid values for
       _socket_type_.  However, the netlink protocol does not distinguish  between  datagram  and  raw
       sockets.

       _netlink_family_ selects the kernel module or netlink group to communicate with.  The currently
       assigned netlink families are:

       **NETLINK**___**ROUTE**
              Receives routing and link updates and may be used to modify the routing  tables  (both
              IPv4  and IPv6), IP addresses, link parameters, neighbor setups, queueing disciplines,
              traffic classes and packet classifiers (see [**rtnetlink**(7)](https://www.chedong.com/phpMan.php/man/rtnetlink/7/markdown)).

       **NETLINK**___**W1** (Linux 2.6.13 to 2.16.17)
              Messages from 1-wire subsystem.

       **NETLINK**___**USERSOCK**
              Reserved for user-mode socket protocols.

       **NETLINK**___**FIREWALL** (up to and including Linux 3.4)
              Transport IPv4 packets from netfilter to user space.  Used by _ip_queue_ kernel  module.
              After  a  long  period  of  being  declared  obsolete  (in  favor of the more advanced
              _nfnetlink_queue_ feature), **NETLINK**___**FIREWALL** was removed in Linux 3.5.

       **NETLINK**___**SOCK**___**DIAG** (since Linux 3.3)
              Query information about sockets of various protocol  families  from  the  kernel  (see
              **sock**___**[diag**(7)](https://www.chedong.com/phpMan.php/man/diag/7/markdown)).

       **NETLINK**___**INET**___**DIAG** (since Linux 2.6.14)
              An obsolete synonym for **NETLINK**___**SOCK**___**DIAG**.

       **NETLINK**___**NFLOG** (up to and including Linux 3.16)
              Netfilter/iptables ULOG.

       **NETLINK**___**XFRM**
              IPsec.

       **NETLINK**___**SELINUX** (since Linux 2.6.4)
              SELinux event notifications.

       **NETLINK**___**ISCSI** (since Linux 2.6.15)
              Open-iSCSI.

       **NETLINK**___**AUDIT** (since Linux 2.6.6)
              Auditing.

       **NETLINK**___**FIB**___**LOOKUP** (since Linux 2.6.13)
              Access to FIB lookup from user space.

       **NETLINK**___**CONNECTOR** (since Linux 2.6.14)
              Kernel  connector.  See _Documentation/driver-api/connector.rst_ (or _/Documentation/con__‐
              _nector/connector.*_  in kernel 5.2 and earlier) in the Linux  kernel  source  tree  for
              further information.

       **NETLINK**___**NETFILTER** (since Linux 2.6.14)
              Netfilter subsystem.

       **NETLINK**___**SCSITRANSPORT** (since Linux 2.6.19)
              SCSI Transports.

       **NETLINK**___**RDMA** (since Linux 3.0)
              Infiniband RDMA.

       **NETLINK**___**IP6**___**FW** (up to and including Linux 3.4)
              Transport IPv6 packets from netfilter to user space.  Used by _ip6_queue_ kernel module.

       **NETLINK**___**DNRTMSG**
              DECnet routing messages.

       **NETLINK**___**KOBJECT**___**UEVENT** (since Linux 2.6.10)
              Kernel messages to user space.

       **NETLINK**___**GENERIC** (since Linux 2.6.15)
              Generic netlink family for simplified netlink usage.

       **NETLINK**___**CRYPTO** (since Linux 3.2)
              Netlink  interface  to  request  information  about ciphers registered with the kernel
              crypto API as well as allow configuration of the kernel crypto API.

       Netlink messages consist of a byte stream with one or multiple _nlmsghdr_ headers  and  associ‐
       ated payload.  The byte stream should be accessed only with the standard **NLMSG**___***** macros.  See
       [**netlink**(3)](https://www.chedong.com/phpMan.php/man/netlink/3/markdown) for further information.

       In multipart messages (multiple _nlmsghdr_ headers with associated payload in one byte  stream)
       the first and all following headers have the **NLM**___**F**___**MULTI** flag set, except for the last header
       which has the type **NLMSG**___**DONE**.

       After each _nlmsghdr_ the payload follows.

           struct nlmsghdr {
               __u32 nlmsg_len;    /* Length of message including header */
               __u16 nlmsg_type;   /* Type of message content */
               __u16 nlmsg_flags;  /* Additional flags */
               __u32 nlmsg_seq;    /* Sequence number */
               __u32 nlmsg_pid;    /* Sender port ID */
           };

       _nlmsg_type_ can be one of the standard message types: **NLMSG**___**NOOP** message  is  to  be  ignored,
       **NLMSG**___**ERROR**  message  signals  an  error  and  the  payload  contains  an _nlmsgerr_ structure,
       **NLMSG**___**DONE** message terminates a multipart message.

           struct nlmsgerr {
               int error;        /* Negative errno or 0 for acknowledgements */
               struct nlmsghdr msg;  /* Message header that caused the error */
           };

       A netlink family usually specifies more message types, see the appropriate manual  pages  for
       that, for example, [**rtnetlink**(7)](https://www.chedong.com/phpMan.php/man/rtnetlink/7/markdown) for **NETLINK**___**ROUTE**.

       Standard flag bits in _nlmsg_flags_
       ──────────────────────────────────────────────────────────

       **NLM**___**F**___**REQUEST**   Must be set on all request messages.
       **NLM**___**F**___**MULTI**     The  message  is part of a multipart mes‐
                       sage terminated by **NLMSG**___**DONE**.
       **NLM**___**F**___**ACK**       Request for an acknowledgment on success.
       **NLM**___**F**___**ECHO**      Echo this request.

       Additional flag bits for GET requests
       ────────────────────────────────────────────────────────────────────
       **NLM**___**F**___**ROOT**     Return the complete table instead of a single entry.
       **NLM**___**F**___**MATCH**    Return all entries matching criteria passed in  mes‐
                      sage content.  Not implemented yet.
       **NLM**___**F**___**ATOMIC**   Return an atomic snapshot of the table.
       **NLM**___**F**___**DUMP**     Convenience macro; equivalent to
                      (NLM_F_ROOT|NLM_F_MATCH).

       Note that **NLM**___**F**___**ATOMIC** requires the **CAP**___**NET**___**ADMIN** capability or an effective UID of 0.

       Additional flag bits for NEW requests
       ────────────────────────────────────────────────────────────
       **NLM**___**F**___**REPLACE**   Replace existing matching object.
       **NLM**___**F**___**EXCL**      Don't replace if the object already exists.
       **NLM**___**F**___**CREATE**    Create object if it doesn't already exist.
       **NLM**___**F**___**APPEND**    Add to the end of the object list.

       _nlmsg_seq_  and  _nlmsg_pid_ are used to track messages.  _nlmsg_pid_ shows the origin of the mes‐
       sage.  Note that there isn't a 1:1 relationship between _nlmsg_pid_ and the PID of the  process
       if the message originated from a netlink socket.  See the **ADDRESS** **FORMATS** section for further
       information.

       Both _nlmsg_seq_ and _nlmsg_pid_ are opaque to netlink core.

       Netlink is not a reliable protocol.  It tries its best to deliver a message to  its  destina‐
       tion(s),  but  may  drop messages when an out-of-memory condition or other error occurs.  For
       reliable transfer the sender can request an acknowledgement from the receiver by setting  the
       **NLM**___**F**___**ACK**  flag.   An  acknowledgment is an **NLMSG**___**ERROR** packet with the error field set to 0.
       The application must generate acknowledgements for  received  messages  itself.   The  kernel
       tries  to  send an **NLMSG**___**ERROR** message for every failed packet.  A user process should follow
       this convention too.

       However, reliable transmissions from kernel to user are impossible in any case.   The  kernel
       can't  send  a  netlink message if the socket buffer is full: the message will be dropped and
       the kernel and the user-space process will no longer have the same view of kernel state.   It
       is  up  to  the  application  to  detect when this happens (via the **ENOBUFS** error returned by
       [**recvmsg**(2)](https://www.chedong.com/phpMan.php/man/recvmsg/2/markdown)) and resynchronize.

### Address formats
       The _sockaddr_nl_ structure describes a netlink client in user space or in the kernel.  A _sock__‐
       _addr_nl_  can  be  either  unicast (only sent to one peer) or sent to netlink multicast groups
       (_nl_groups_ not equal 0).

           struct sockaddr_nl {
               sa_family_t     nl_family;  /* AF_NETLINK */
               unsigned short  nl_pad;     /* Zero */
               pid_t           nl_pid;     /* Port ID */
               __u32           nl_groups;  /* Multicast groups mask */
           };

       _nl_pid_ is the unicast address of netlink socket.  It's always 0 if the destination is in  the
       kernel.  For a user-space process, _nl_pid_ is usually the PID of the process owning the desti‐
       nation socket.  However, _nl_pid_ identifies a netlink socket, not a  process.   If  a  process
       owns several netlink sockets, then _nl_pid_ can be equal to the process ID only for at most one
       socket.  There are two ways to assign _nl_pid_ to a netlink socket.  If  the  application  sets
       _nl_pid_  before  calling [**bind**(2)](https://www.chedong.com/phpMan.php/man/bind/2/markdown), then it is up to the application to make sure that _nl_pid_ is
       unique.  If the application sets it to 0, the kernel takes care of assigning it.  The  kernel
       assigns  the  process  ID  to the first netlink socket the process opens and assigns a unique
       _nl_pid_ to every netlink socket that the process subsequently creates.

       _nl_groups_ is a bit mask with every bit representing a netlink  group  number.   Each  netlink
       family has a set of 32 multicast groups.  When [**bind**(2)](https://www.chedong.com/phpMan.php/man/bind/2/markdown) is called on the socket, the _nl_groups_
       field in the _sockaddr_nl_ should be set to a bit mask of the groups which it wishes to  listen
       to.   The  default  value  for  this field is zero which means that no multicasts will be re‐
       ceived.  A socket may multicast messages to any of the multicast groups by setting  _nl_groups_
       to  a  bit  mask  of  the groups it wishes to send to when it calls [**sendmsg**(2)](https://www.chedong.com/phpMan.php/man/sendmsg/2/markdown) or does a **con**‐‐
       [**nect**(2)](https://www.chedong.com/phpMan.php/man/nect/2/markdown).  Only processes with an effective UID of 0 or the **CAP**___**NET**___**ADMIN** capability may  send
       or  listen  to a netlink multicast group.  Since Linux 2.6.13, messages can't be broadcast to
       multiple groups.  Any replies to a message received for a multicast group should be sent back
       to  the  sending  PID and the multicast group.  Some Linux kernel subsystems may additionally
       allow other users to send and/or  receive  messages.   As  at  Linux  3.0,  the  **NETLINK**___**KOB**‐‐
       **JECT**___**UEVENT**,  **NETLINK**___**GENERIC**, **NETLINK**___**ROUTE**, and **NETLINK**___**SELINUX** groups allow other users to
       receive messages.  No groups allow other users to send messages.

### Socket options
       To set or get a netlink socket option, call [**getsockopt**(2)](https://www.chedong.com/phpMan.php/man/getsockopt/2/markdown) to read or [**setsockopt**(2)](https://www.chedong.com/phpMan.php/man/setsockopt/2/markdown)  to  write
       the option with the option level argument set to **SOL**___**NETLINK**.  Unless otherwise noted, _optval_
       is a pointer to an _int_.

       **NETLINK**___**PKTINFO** (since Linux 2.6.14)
              Enable **nl**___**pktinfo** control messages for received packets to get the  extended  destina‐
              tion group number.

       **NETLINK**___**ADD**___**MEMBERSHIP**, **NETLINK**___**DROP**___**MEMBERSHIP** (since Linux 2.6.14)
              Join/leave a group specified by _optval_.

       **NETLINK**___**LIST**___**MEMBERSHIPS** (since Linux 4.2)
              Retrieve  all groups a socket is a member of.  _optval_ is a pointer to ____**u32** and _optlen_
              is the size of the array.  The array is filled with the full  membership  set  of  the
              socket, and the required array size is returned in _optlen_.

       **NETLINK**___**BROADCAST**___**ERROR** (since Linux 2.6.30)
              When  not  set,  **netlink**___**broadcast()**  only  reports  **ESRCH**  errors and silently ignore
              **ENOBUFS** errors.

       **NETLINK**___**NO**___**ENOBUFS** (since Linux 2.6.30)
              This flag can be used by unicast and broadcast listeners to  avoid  receiving  **ENOBUFS**
              errors.

       **NETLINK**___**LISTEN**___**ALL**___**NSID** (since Linux 4.2)
              When  set,  this socket will receive netlink notifications from all network namespaces
              that have an _nsid_ assigned into the  network  namespace  where  the  socket  has  been
              opened.  The _nsid_ is sent to user space via an ancillary data.

       **NETLINK**___**CAP**___**ACK** (since Linux 4.2)
              The kernel may fail to allocate the necessary room for the acknowledgment message back
              to user space.  This option trims off the payload of  the  original  netlink  message.
              The  netlink message header is still included, so the user can guess from the sequence
              number which message triggered the acknowledgment.

## VERSIONS
       The socket interface to netlink first appeared Linux 2.2.

       Linux 2.0 supported a more primitive device-based netlink interface (which is still available
       as a compatibility option).  This obsolete interface is not described here.

## NOTES
       It  is  often better to use netlink via _libnetlink_ or _libnl_ than via the low-level kernel in‐
       terface.

## BUGS
       This manual page is not complete.

## EXAMPLES
       The following example creates a **NETLINK**___**ROUTE** netlink socket which will listen  to  the  **RTM**‐‐
       **GRP**___**LINK**  (network  interface  create/delete/up/down events) and **RTMGRP**___**IPV4**___**IFADDR** (IPv4 ad‐
       dresses add/delete events) multicast groups.

           struct sockaddr_nl sa;

           memset(&sa, 0, sizeof(sa));
           sa.nl_family = AF_NETLINK;
           sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;

           fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
           bind(fd, (struct sockaddr *) &sa, sizeof(sa));

       The next example demonstrates how to send a netlink message to the kernel (pid 0).  Note that
       the  application  must  take  care of message sequence numbers in order to reliably track ac‐
       knowledgements.

           struct nlmsghdr *nh;    /* The nlmsghdr with payload to send */
           struct sockaddr_nl sa;
           struct iovec iov = { nh, nh->nlmsg_len };
           struct msghdr msg;

           msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
           memset(&sa, 0, sizeof(sa));
           sa.nl_family = AF_NETLINK;
           nh->nlmsg_pid = 0;
           nh->nlmsg_seq = ++sequence_number;
           /* Request an ack from kernel by setting NLM_F_ACK */
           nh->nlmsg_flags |= NLM_F_ACK;

           sendmsg(fd, &msg, 0);

       And the last example is about reading netlink message.

           int len;
           /* 8192 to avoid message truncation on platforms with
              page size > 4096 */
           struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)];
           struct iovec iov = { buf, sizeof(buf) };
           struct sockaddr_nl sa;
           struct msghdr msg;
           struct nlmsghdr *nh;

           msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
           len = recvmsg(fd, &msg, 0);

           for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
                nh = NLMSG_NEXT (nh, len)) {
               /* The end of multipart message */
               if (nh->nlmsg_type == NLMSG_DONE)
                   return;

               if (nh->nlmsg_type == NLMSG_ERROR)
                   /* Do some error handling */
               ...

               /* Continue with parsing payload */
               ...
           }

## SEE ALSO
       [**cmsg**(3)](https://www.chedong.com/phpMan.php/man/cmsg/3/markdown), [**netlink**(3)](https://www.chedong.com/phpMan.php/man/netlink/3/markdown), [**capabilities**(7)](https://www.chedong.com/phpMan.php/man/capabilities/7/markdown), [**rtnetlink**(7)](https://www.chedong.com/phpMan.php/man/rtnetlink/7/markdown), **sock**___**[diag**(7)](https://www.chedong.com/phpMan.php/man/diag/7/markdown)

       information about libnetlink ⟨ftp://ftp.inr.ac.ru/ip-routing/iproute2*⟩

       information about libnl ⟨<http://www.infradead.org/~tgr/libnl/>⟩

       RFC 3549 "Linux Netlink as an IP Services Protocol"

## COLOPHON
       This page is part of release 5.10 of the Linux  _man-pages_  project.   A  description  of  the
       project,  information about reporting bugs, and the latest version of this page, can be found
       at <https://www.kernel.org/doc/man-pages/>.



Linux                                        2020-06-09                                   [NETLINK(7)](https://www.chedong.com/phpMan.php/man/NETLINK/7/markdown)
