rtnetlink(3)

okay so this is rtnetlink, a page from section 3 of the manual (Library Functions Manual, if we're being fancy), and it's literally just the little helper macros you use to poke at rtnetlink messages without losing your entire mind. it's giving "tiny tools, huge consequences" and I'm obsessed 🫢


🌸 NAME 🌸

rtnetlink β€” macros to manipulate rtnetlink messages

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc) β€” the usual girlies πŸ’–

✨ 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 πŸ¦‹

so every single rtnetlink(7) message is built out of two things: a netlink(7) message header, and then a whole lil stack of attributes tacked onto the end. and here's the thing, bestie β€” you do NOT touch those attributes by hand. the macros on this page are the only correct way to do it, periodt.

  • RTA_OK(rta, size) β€” returns true if rta actually points to a valid routing attribute. size is the running size of the attribute buffer. and if it's NOT true?? then you have to assume there are no more attributes left in the message, even if size is nonzero. like, no more, done, go home. 😭
  • RTA_DATA(rta) β€” hands you a pointer to the start of this attribute's data.
  • RTA_PAYLOAD(rta) β€” tells you how big this attribute's data is.
  • RTA_NEXT(rta, size) β€” grabs the next attribute after rta. heads up though, calling this macro updates size, so don't be weird about it. and you should still run RTA_OK on the pointer it gives back to check it's actually valid, iykyk.
  • RTA_LENGTH(size) β€” returns the size you need for size bytes of data plus the header. (the header is not free, bestie, nothing ever is πŸ’€)
  • RTA_SPACE(size) β€” returns the amount of space that'll be needed in a message with size bytes of data.

πŸ‘‘ STANDARDS πŸ‘‘

Linux. that's it, that's the standard. πŸ’…

πŸ’€ BUGS πŸ’€

This manual page is incomplete. yeah. it said what it said. πŸ™ˆ

🌈 EXAMPLES 🌈

okay so here's a whole example where we create an rtnetlink message to set the MTU of a device. watch and learn ✨

#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);

(yeah the /* ??? */ is in the original, I'm not gonna lie to you and pretend I know better than the source. it's iconic actually. main character energy.)

πŸ”₯ SEE ALSO πŸ”₯


and that's the whole page, bestie πŸŽ€ this one came from Linux man-pages 6.20, dated 2026-02-08, and it lives at rtnetlink(3). go forth and manipulate your netlink attributes responsibly, mwah πŸ’–πŸ¦‹βœ¨

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-1-para
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms