---
name: "rtnetlink"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/rtnetlink"
---

# rtnetlink(3)

macros to manipulate rtnetlink messages

## NAME

rtnetlink - macros to manipulate rtnetlink messages

## LIBRARY

Standard C library (*libc*, *-lc*)

## SYNOPSIS

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

**rtnetlink\_socket = socket(AF\_NETLINK, int** *socket\_type***, NETLINK\_ROUTE);**

**int RTA\_OK(struct rtattr \****rta***, int** *size***);**

**void \*RTA\_DATA(struct rtattr \****rta***);** **unsigned int RTA\_PAYLOAD(struct rtattr \****rta***);**

**struct rtattr \*RTA\_NEXT(struct rtattr \****rta***, unsigned int** *size***);**

**unsigned int RTA\_LENGTH(unsigned int** *size***);** **unsigned int RTA\_SPACE(unsigned int** *size***);**

## DESCRIPTION

All [**rtnetlink**(7)](/rtnetlink/7.md) messages consist of a [**netlink**(7)](/netlink/7.md) message header and appended attributes. The attributes should be manipulated only using the macros provided here.

**RTA\_OK(***rta***,** *size***)** returns true if *rta* points to a valid routing attribute; *size* is the running size of the attribute buffer. When not true then you must assume there are no more attributes in the message, even if *size* is nonzero.

**RTA\_DATA(***rta***)** returns a pointer to the start of this attribute's data.

**RTA\_PAYLOAD(***rta***)** returns the size of this attribute's data.

**RTA\_NEXT(***rta***,** *size***)** gets the next attribute after *rta*. Calling this macro will update *size*. You should use **RTA\_OK** to check the validity of the returned pointer.

**RTA\_LENGTH(***size***)** returns the size which is required for *size* bytes of data plus the header.

**RTA\_SPACE(***size***)** returns the amount of space which will be needed in a message with *size* bytes of data.

## STANDARDS

Linux.

## BUGS

This manual page is incomplete.

## EXAMPLES

Creating a rtnetlink message to set the MTU of a device:

```
#include <linux/rtnetlink.h>
...
struct {
    struct nlmsghdr  nh;
    struct ifinfomsg if;
    char             attrbuf[512];
} req;
struct rtattr *rta;
unsigned int mtu = 1000;
int rtnetlink_sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
memset(&req, 0, sizeof(req));
req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.if));
req.nh.nlmsg_flags = NLM_F_REQUEST;
req.nh.nlmsg_type = RTM_NEWLINK;
req.if.ifi_family = AF_UNSPEC;
req.if.ifi_index = INTERFACE_INDEX;
req.if.ifi_change = 0xffffffff; /* ??? */
rta = (struct rtattr *) ((char *) &req +
                         NLMSG_ALIGN(req.nh.nlmsg_len));
rta->rta_type = IFLA_MTU;
rta->rta_len = RTA_LENGTH(sizeof(mtu));
req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) +
                              RTA_LENGTH(sizeof(mtu));
memcpy(RTA_DATA(rta), &mtu, sizeof(mtu));
send(rtnetlink_sk, &req, req.nh.nlmsg_len, 0);
```

## SEE ALSO

[**netlink**(3)](/netlink/3.md), [**netlink**(7)](/netlink/7.md), [**rtnetlink**(7)](/rtnetlink/7.md)
