---
name: "nlinline"
section: "3"
platform: "linux"
package: "nlinline-dev"
version: "0.2.2-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/nlinline"
---

# nlinline(3)

configure network interfaces

## 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.

- **nlinline\_if\_nametoindex**

  This function returns the index of the network interface whose name is *ifname*.

- **nlinline\_linksetupdown**

  This function turns the interface *ifindex* up (*updown* == **1**) or down (*updown* == **0**).

- **nlinline\_ipaddr\_add**

  This function adds an IP address to the interface *ifindex*. It supports IPv4 (*family* == **AF\_INET**) and IPv6 (*family* == **AF\_INET6**).

- **nlinline\_ipaddr\_del**

  This function removes the IP address from the interface *ifindex*. It supports IPv4 (*family* == **AF\_INET**) and IPv6 (*family* == **AF\_INET6**).

- **nlinline\_iproute\_add**

  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.

- **nlinline\_iproute\_del**

  This function removes the static route to *dst\_addr*/*dst\_prefixlen* network through the gateway *gw\_addr*.

- **nlinline\_iplink\_add**

  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.

- **nlinline\_iplink\_del**

  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.

- **nlinline\_linksetaddr**

  This functions sets the mac address of the interface *ifindex*.

- **nlinline\_linkgetaddr**

  This functions gets the mac address of the interface *ifindex*.

- **nlinline\_linksetmtu**

  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
