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 the rth handle πŸ¦‹ that handle gets passed to every call after this one. subscriptions is 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 🌸 type is a rtnetlink message type.
  • rtnl_dump_request β€” requests a full dump of the type data into buf, with a max length of len πŸ“ type is a rtnetlink message type.
  • rtnl_dump_filter β€” receives netlink data after a request and filters it πŸ”₯ the filter callback checks whether the received message is wanted. It gets the source address of the message, the message itself, and arg1 as arguments. A return of 0 means the filter passed, and a negative value is returned by rtnl_dump_filter if something goes wrong. Passing NULL for filter means no filter at all πŸ™ˆ junk is 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 to handler πŸ‘‘ the handler is a callback that gets the message source address, the ancillary data, the message itself, and the jarg cookie 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 like rtnl_listen, but reads a netlink message bundle from the file file and passes the messages to handler for 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 πŸ’€ See rtnetlink(3) and netlink(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 in buf (length len) to the handle rth 🧚
  • addattr32 β€” adds a __u32 attribute of type type with the value data to the netlink message n, which lives in a buffer of length maxlen πŸ’œ
  • addattr_l β€” adds a variable length attribute of type type with the value data and length alen to the netlink message n, which lives in a buffer of length maxlen πŸŽ€ data gets copied.
  • rta_addattr32 β€” initializes the rtnetlink attribute rta with a __u32 data value πŸ¦‹
  • rta_addattr32 β€” initializes the rtnetlink attribute rta with 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 πŸ”—


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