---
name: "PF_NETLINK"
section: "7"
platform: "linux"
package: "manpages"
version: "3.27-1"
release: "Debian 6.0 (Squeeze)"
platforms: ["linux"]
url: "https://new.carta.tech/netlink/7"
---

# PF\_NETLINK(7)

Communication between kernel and userspace (AF\_NETLINK)

Part of [netlink(7)](/netlink/7.md).

## NAME

netlink - Communication between kernel and userspace (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 kernel and userspace processes. It consists of a standard sockets-based interface for userspace 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 only provided for backwards 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)](/rtnetlink/7.md)).

- **NETLINK\_W1**

  Messages from 1-wire subsystem.

- **NETLINK\_USERSOCK**

  Reserved for user-mode socket protocols.

- **NETLINK\_FIREWALL**

  Transport IPv4 packets from netfilter to userspace. Used by *ip\_queue* kernel module.

- **NETLINK\_INET\_DIAG**

  INET socket monitoring.

- **NETLINK\_NFLOG**

  Netfilter/iptables ULOG.

- **NETLINK\_XFRM**

  IPsec.

- **NETLINK\_SELINUX**

  SELinux event notifications.

- **NETLINK\_ISCSI**

  Open-iSCSI.

- **NETLINK\_AUDIT**

  Auditing.

- **NETLINK\_FIB\_LOOKUP**

  Access to FIB lookup from userspace.

- **NETLINK\_CONNECTOR**

  Kernel connector. See *Documentation/connector/\** in the kernel source for further information.

- **NETLINK\_NETFILTER**

  Netfilter subsystem.

- **NETLINK\_IP6\_FW**

  Transport IPv6 packets from netfilter to userspace. Used by *ip6\_queue* kernel module.

- **NETLINK\_DNRTMSG**

  DECnet routing messages.

- **NETLINK\_KOBJECT\_UEVENT**

  Kernel messages to userspace.

- **NETLINK\_GENERIC**

  Generic netlink family for simplified netlink usage.

Netlink messages consist of a byte stream with one or multiple *nlmsghdr* headers and associated payload. The byte stream should only be accessed with the standard **NLMSG\_\*** macros. See [**netlink**(3)](/netlink/3.md) 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;    /* PID of the sending process. */
};
```

*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)](/rtnetlink/7.md) 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 message 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 message 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 message. 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 destination(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 userspace 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)](/recvmsg/2.md)) and resynchronize.

### Address Formats

The *sockaddr\_nl* structure describes a netlink client in user space or in the kernel. A *sockaddr\_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;     /* Process 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 userspace process, *nl\_pid* is usually the PID of the process owning the destination socket. However, *nl\_pid* identifies a netlink socket, not a process. If a process owns several netlink sockets, then *nl\_pid* can only be equal to the process ID 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)](/bind/2.md), 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)](/bind/2.md) 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 received. 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)](/sendmsg/2.md) or does a [**connect**(2)](/connect/2.md). Only processes with an effective UID of 0 or the **CAP\_NET\_ADMIN** capability may send or listen to a netlink multicast group. Any replies to a message received for a multicast group should be sent back to the sending PID and the multicast group.

## VERSIONS

The socket interface to netlink is a new feature of 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.

NETLINK\_SELINUX appeared in Linux 2.6.4.

NETLINK\_AUDIT appeared in Linux 2.6.6.

NETLINK\_KOBJECT\_UEVENT appeared in Linux 2.6.10.

NETLINK\_W1 and NETLINK\_FIB\_LOOKUP appeared in Linux 2.6.13.

NETLINK\_INET\_DIAG, NETLINK\_CONNECTOR and NETLINK\_NETFILTER appeared in Linux 2.6.14.

NETLINK\_GENERIC and NETLINK\_ISCSI appeared in Linux 2.6.15.

## NOTES

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

## BUGS

This manual page is not complete.

## EXAMPLE

The following example creates a **NETLINK\_ROUTE** netlink socket which will listen to the **RTMGRP\_LINK** (network interface create/delete/up/down events) and **RTMGRP\_IPV4\_IFADDR** (IPv4 addresses 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 application must take care of message sequence numbers in order to reliably track acknowledgements.

```
struct nlmsghdr *nh;    /* The nlmsghdr with payload to send. */
struct sockaddr_nl sa;
struct iovec iov = { (void *) nh, nh->nlmsg_len };
struct msghdr msg;
msg = { (void *)&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;
char buf[4096];
struct iovec iov = { buf, sizeof(buf) };
struct sockaddr_nl sa;
struct msghdr msg;
struct nlmsghdr *nh;
msg = { (void *)&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)](/cmsg/3.md), [**netlink**(3)](/netlink/3.md), [**capabilities**(7)](/capabilities/7.md), [**rtnetlink**(7)](/rtnetlink/7.md)

ftp://ftp.inr.ac.ru/ip-routing/iproute2\* for information about libnetlink.

http://people.suug.ch/~tgr/libnl/ for information about libnl.

RFC 3549 "Linux Netlink as an IP Services Protocol"

## COLOPHON

This page is part of release 3.27 of the Linux *man-pages* project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.
