NLINLINE(3)

πŸ’– NLINLINE β€” the nlinline girlies' guide πŸ’–

okay so bestie, we're in section 3 of the Library Functions Manual and the star of the show is NLINLINE, a whole library of functions that let you configure network interfaces. yes, in C. yes, it slays. πŸ¦‹


✨ NAME ✨

nlinline_if_nametoindex, nlinline_linksetupdown, nlinline_ipaddr_add, nlinline_ipaddr_del, nlinline_iproute_add, nlinline_iproute_del, nlinline_iplink_add, nlinline_iplink_del, nlinline_linksetaddr, nlinline_linkgetaddr β€” they literally configure network interfaces. that's the whole vibe. πŸ’…


🌸 SYNOPSIS 🌸

first things first, you gotta pull in the header:

#include <nlinline.h>

and then, the girlies themselves:

int nlinline_if_nametoindex(const char *ifname);

int nlinline_linksetupdown(unsigned int ifindex, int updown);

int nlinline_ipaddr_add(int family, void *addr, int prefixlen, unsigned
int ifindex);

int nlinline_ipaddr_del(int family, void *addr, int prefixlen, unsigned
int ifindex);

int nlinline_iproute_add(int family, void *dst_addr, int dst_prefixlen,
void *gw_addr, unsigned int ifindex);

int nlinline_iproute_del(int family, void *dst_addr, int dst_prefixlen,
void *gw_addr, unsigned int ifindex);

int nlinline_iplink_add(const char *ifname, unsigned int ifindex, const
char *type, const char *data);

int nlinline_iplink_del(const char *ifname, unsigned int ifindex);

int nlinline_linksetaddr(unsigned int ifindex, void *macaddr);

int nlinline_linkgetaddr(unsigned int ifindex, void *macaddr);

int nlinline_linksetmtu(unsigned int ifindex, unsigned int mtu);

πŸ‘‘ DESCRIPTION πŸ‘‘

NLINLINE (short for netlink inline, iykyk) is a library of inline functions that hands C programmers some extremely handy little tools for configuring network stacks. and the best part? it's entirely implemented in a header file, nlinline.h. one file. no drama. no cap. πŸŽ€


🧚 the functions, one by one

  • nlinline_if_nametoindex β€” bestie, this one just gives you the index of the network interface whose name is ifname. that's it, that's the function.
  • nlinline_linksetupdown β€” turns the interface ifindex up (updown == 1) or down (updown == 0). flip the switch, main character energy. πŸ”₯
  • nlinline_ipaddr_add β€” adds an IP address to the interface ifindex. it supports IPv4 (family == AF_INET) and IPv6 (family == AF_INET6). both baddies welcome. ✨
  • nlinline_ipaddr_del β€” removes the IP address from the interface ifindex. same deal, it supports IPv4 (family == AF_INET) and IPv6 (family == AF_INET6). we love a symmetric API. πŸ’œ
  • nlinline_iproute_add β€” adds a static route to the dst_addr/dst_prefixlen network through the gateway gw_addr. if dst_addr == NULL it adds a default route. and heads up: ifindex must be specified when gw_addr is an IPv6 link local address. periodt.
  • nlinline_iproute_del β€” removes the static route to the dst_addr/dst_prefixlen network through the gateway gw_addr. bye girl πŸ’€
  • nlinline_iplink_add β€” adds a new link of type type, named ifname. the value of data depends on the type of link and can be NULL. a default interface name is assigned if name == NULL. and the link gets created with a given index when ifindex is positive. slay.
  • nlinline_iplink_del β€” removes a link. the link to be deleted can be identified by its name (ifname) or by its index (ifindex). either ifindex can be zero or ifname can be NULL. you can even use both ifindex and ifname to identify the link β€” but an error may occur if the parameters are inconsistent. so like, don't be messy. πŸ™ˆ
  • nlinline_linksetaddr β€” this function sets the mac address of the interface ifindex. (yes, the original really does say "This functions". we love her.)
  • nlinline_linkgetaddr β€” this function gets the mac address of the interface ifindex. get you a function that can do both. πŸ’–
  • nlinline_linksetmtu β€” this function sets the MTU (Maximum Transfer Unit) of the interface ifindex. big packets, bigger dreams. 🌈

🫢 the important lore

IP addresses are void * arguments β€” any sequence of 4 or 16 bytes (in network byte order) is a legal IPv4 or IPv6 address respectively. that's the rule, no exceptions, bestie.

nlinline functions do not add dependencies at run-time. this is highkey useful for security critical applications (like PAM modules). these inline functions use netlink only β€” they do not depend on the obsolete netdevice (ioctl) API. and only the code of referenced inline functions enters into the object and executable code. so efficient. so clean. we're obsessed. 😭


⭐ RETURN VALUE ⭐

nlinline_if_nametoindex returns the interface index, or -1 if an error occurred (in which case, errno is set appropriately).

all the other functions return zero in case of success. on error, -1 is returned, and errno is set appropriately.

and a lil plot twist for you: (nlinline_iplink_add can return the (positive) ifindex of the newly created link when the argument ifindex is -1 and the stack supports this feature.) πŸ‘€

πŸ“ EXAMPLE πŸ“

here's a whole little program, bestie. look at her go:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <nlinline.h>

int main(int argc, char *argv[]) {
  uint8_t ipv4addr[] = {192,168,2,2};
  uint8_t ipv4gw[] = {192,168,2,1};
  uint8_t ipv6addr[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x02};
  uint8_t ipv6gw[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x01};

  int ifindex = nlinline_if_nametoindex(argv[1]);
  if (ifindex > 0)
    printf("%d\n", ifindex);
  else {
    perror("nametoindex");
    return 1;
  }

  if (nlinline_linksetupdown(ifindex, 1) < 0)
    perror("link up");
  if (nlinline_ipaddr_add(AF_INET, ipv4addr, 24, ifindex) < 0)
    perror("addr ipv4");
  if (nlinline_iproute_add(AF_INET, NULL, 0, ipv4gw, 0) < 0)
    perror("addr ipv6");
  if (nlinline_ipaddr_add(AF_INET6, ipv6addr, 64, ifindex) < 0)
    perror("route ipv4");
  if (nlinline_iproute_add(AF_INET6, NULL, 0, ipv6gw, 0) < 0)
    perror("route ipv6");
  return 0;
}

so what does she do? this program takes the name of an interface from the command line. it turns that interface up and sets the interface IPv4 and IPv6 addresses and default routes. iconic behaviour, honestly. πŸ’…βœ¨


πŸ’œ AUTHOR πŸ’œ

VirtualSquare. Project leader: Renzo Davoli. legends only. πŸ‘‘


and that's the whole page, bestie β€” brought to you by VirtualSquare, dated December 2020, living in section 3 of the manual. go configure some network stacks, you've got this. mwah πŸ«ΆπŸ’–

πŸ“¦ where this page comes from