libnetlink(3)
π§ libnetlink(3) π§
β¨ NAME β¨
libnetlink π okay so this is a library for talking to the netlink service π¦
π SYNOPSIS π
#include <asm/types.h>
#include <libnetlink.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
int rtnl_send(struct rtnl_handle *rth, char *buf, int len)
int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
int rtnl_dump_filter(struct rtnl_handle *rth,
int (*filter)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
void *arg1,
int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
void *arg2)
int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
unsigned groups, struct nlmsghdr *answer,
int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
void *jarg)
int rtnl_listen(struct rtnl_handle *rtnl,
int (*handler)(struct sockaddr_nl *, struct rtnl_ctrl_data *,
struct nlmsghdr *n, void *),
void *jarg)
int rtnl_from_file(FILE *rtnl,
int (*handler)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
void *jarg)
int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
int rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
π DESCRIPTION π
okay so libnetlink gives you a higher level interface to rtnetlink(7) π«Ά bestie, the read functions return 0 on success and a negative errno on failure π and the send functions return the amount of data sent, or -1 on error β¨
rtnl_openβ opens a rtnetlink socket and saves the state into therthhandle π¦ that handle gets passed to every call after this one.subscriptionsis a bitmap of the rtnetlink multicast groups the socket will be a member of πrtnl_wilddump_requestβ requests a full dump of the type database for family addresses πΈtypeis a rtnetlink message type.rtnl_dump_requestβ requests a full dump of the type data intobuf, with a max length oflenπtypeis a rtnetlink message type.rtnl_dump_filterβ receives netlink data after a request and filters it π₯ thefiltercallback checks whether the received message is wanted. It gets the source address of the message, the message itself, andarg1as arguments. A return of0means the filter passed, and a negative value is returned byrtnl_dump_filterif something goes wrong. PassingNULLforfiltermeans no filter at all πjunkis used to filter out messages that aren't meant for the local socket. Only one message bundle gets received, and if a message is pending, this function does not block π«Άrtnl_listenβ receives netlink data after a request and passes it tohandlerπ the handler is a callback that gets the message source address, the ancillary data, the message itself, and thejargcookie as arguments. It gets called for all received messages β¨ Only one message bundle is received, and if a message is pending this function does not block.rtnl_from_fileβ works likertnl_listen, but reads a netlink message bundle from the filefileand passes the messages tohandlerfor parsing π the file should contain raw data as received from a rtnetlink socket.
π¨ bestie, the next bit is for when you want to build your own rtnetlink messages. For simple database dumping with filtering, just use the higher level functions above π Seertnetlink(3)andnetlink(3)to learn how to generate a rtnetlink message. These utility functions need a continuous buffer that already holds a netlink message header and a rtnetlink request.
rtnl_sendβ sends the rtnetlink message inbuf(lengthlen) to the handlerthπ§addattr32β adds a__u32attribute of typetypewith the valuedatato the netlink messagen, which lives in a buffer of lengthmaxlenπaddattr_lβ adds a variable length attribute of typetypewith the valuedataand lengthalento the netlink messagen, which lives in a buffer of lengthmaxlenπdatagets copied.rta_addattr32β initializes the rtnetlink attributertawith a__u32data value π¦rta_addattr32β initializes the rtnetlink attributertawith a variable length data value πΈ
π BUGS π
Warning, bestie: this library is meant for internal use only π use libmnl for new programs.
The functions sometimes use fprintf and exit when a fatal error happens π and this library should honestly be named librtnetlink π₯Ί
π«Ά AUTHORS π«Ά
netlink/rtnetlink was designed and written by Alexey Kuznetsov β¨ Andi Kleen wrote the man page π
π SEE ALSO π
netlink(7),rtnetlink(7)/usr/include/linux/rtnetlink.h
okay bestie, that's the whole tea π΅ this was libnetlink(3) from the Library Functions Manual, and that's a wrap π π
π¦ where this page comes from
- alpine 3.22: iproute2-doc 6.15.0-r0 Β· GPL-2.0-or-later
- alpine 3.23: iproute2-doc 6.17.0-r0 Β· GPL-2.0-or-later
- alpine 3.24: iproute2-doc 7.0.0-r0 Β· GPL-2.0-or-later
- centos-stream 10: iproute-devel 7.0.0-2.el10 Β· GPL-2.0-or-later
- centos-stream 9: iproute-devel 6.17.0-3.el9 Β· GPL-2.0-or-later
- centos-stream 9: iproute-devel 6.17.0-3.el9 Β· GPL-2.0-or-later
- debian bookworm: iproute2 6.1.0-3 Β· GPL-2.0-only
- debian trixie: iproute2 6.15.0-1 Β· GPL-2.0-only
- fedora 43: iproute-devel 6.14.0-2.fc43 Β· GPL-2.0-or-later
- fedora 43: iproute-devel 6.14.0-2.fc43 Β· GPL-2.0-or-later
- fedora 44: iproute-devel 6.17.0-2.fc44 Β· GPL-2.0-or-later
- fedora 44: iproute-devel 6.17.0-2.fc44 Β· GPL-2.0-or-later
- nixos 26.05: iproute2 7.0.0
- opensuse-leap 16.0: libnetlink-devel 6.12-160000.4.1 Β· GPL-2.0-or-later
- opensuse-leap 16.0: libnetlink-devel 6.12-160000.4.1 Β· GPL-2.0-or-later
- opensuse-leap 16.0: libnetlink-devel 6.12-160000.4.1 Β· GPL-2.0-or-later
- opensuse-leap 16.0: libnetlink-devel 6.12-160000.4.1 Β· GPL-2.0-or-later
- ubuntu jammy: iproute2 5.15.0-1ubuntu2 Β· GPL-2.0-only
- ubuntu jammy: iproute2 5.15.0-1ubuntu2.2 Β· GPL-2.0-only
- ubuntu noble: iproute2 6.1.0-1ubuntu6 Β· GPL-2.0-only
- ubuntu noble: iproute2 6.1.0-1ubuntu6.4 Β· GPL-2.0-only
- ubuntu questing: iproute2 6.16.0-1ubuntu2
- ubuntu questing: iproute2 6.16.0-1ubuntu2.1 Β· GPL-2.0-only
- ubuntu questing: iproute2 6.16.0-1ubuntu2.2
- ubuntu resolute: iproute2 6.19.0-1ubuntu1 Β· GPL-2.0-only
- ubuntu resolute: iproute2 6.19.0-1ubuntu1.1 Β· GPL-2.0-only
- π girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms