rtnetlink(3)
π rtnetlink(3) β bestie edition β¨
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 ifrtaactually points to a valid routing attribute.sizeis 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 ifsizeis 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 afterrta. heads up though, calling this macro updatessize, so don't be weird about it. and you should still runRTA_OKon the pointer it gives back to check it's actually valid, iykyk.RTA_LENGTH(size)β returns the size you need forsizebytes 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 withsizebytes 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
- Copyright 1999, Andi Kleen <andi@firstfloor.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms