Scroll to navigation

NLINLINE(3) Library Functions Manual NLINLINE(3)

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 - configure network interfaces

SYNOPSIS

#include <nlinline.h>

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 (netlink inline) is a library of inline functions providing C programmers with very handy functions to configure network stacks. NLINLINE is entirely implemented in a header file, nlinline.h.

This function returns the index of the network interface whose name is ifname.
This function turns the interface ifindex up (updown == 1) or down (updown == 0).
This function adds an IP address to the interface ifindex. It supports IPv4 (family == AF_INET) and IPv6 (family == AF_INET6).
This function removes the IP address from the interface ifindex. It supports IPv4 (family == AF_INET) and IPv6 (family == AF_INET6).
This function adds a static route to dst_addr/dst_prefixlen network through the gateway gw_addr. If dst_addr == NULL it adds a default route. ifindex must be specified when gw_addr is an IPv6 link local address.
This function removes the static route to dst_addr/dst_prefixlen network through the gateway gw_addr.
This function 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. The link is created with a given index when ifindex is positive.
This function 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. It is possible to use both ifindex and ifname to identify the link. An error may occur if the parameters are inconsistent.
This functions sets the mac address of the interface ifindex.
This functions gets the mac address of the interface ifindex.
This functions sets the MTU (Maximum Transfer Unit) of the interface ifindex.

IP addresses are void * arguments, any sequence of 4 or 16 bytes (in network byte order) is a legal IPv4 or IPv6 address respectively.

nlinline functions do not add dependencies at run-time. This is useful for security critical applications (like PAM modules) These inline functions use netlink only, they do not depend on the obsolete netdevice (ioctl) API. Only the code of referenced inline functions enters in the object and executable code.

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.

(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

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

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.

AUTHOR

VirtualSquare. Project leader: Renzo Davoli

December 2020 VirtualSquare