Netlink and rtnetlink message codec (RFC 3549)
README.md

nox-netlink #

Netlink and rtnetlink message codec (RFC 3549), built on the wire codec library.

Linux sets up network interfaces, addresses and routes through netlink messages on an AF_NETLINK socket, and the ip command is a front end to those messages. A program that shells out to ip ends up parsing its text output and guessing at its exit codes. With nox-netlink the program builds and parses the messages itself and talks to the kernel in typed OCaml values.

The core library is a pure codec and never opens a socket. It converts the message header (nlmsghdr) and the rtnetlink link, address and route payloads to and from bytes. Netlink uses host byte order. Every Linux architecture in common use is little-endian, so the codec encodes little-endian.

Two sublibraries do the I/O. nox-netlink.eio (module Rtnl) opens the AF_NETLINK socket with Eio and runs typed operations such as link_add_bridge, link_set_master, addr_add and route_add. Each request goes out with NLM_F_ACK and is checked against the kernel's reply. This module works on Linux only, and Rtnl.connect returns an error on any other system. nox-netlink.uevent (module Uevent) reads kernel object events from a NETLINK_KOBJECT_UEVENT socket; it accepts a datagram only if the kernel itself sent it to the multicast group.

Installation #

Install with opam:

$ opam install nox-netlink

If opam cannot find the package, it has probably not reached the public opam-repository yet. Add the overlay repository and install from there:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-netlink

Features #

The codec covers the nlmsghdr header, whose length field it computes on encode, and the three rtnetlink payloads: ifinfomsg for links, ifaddrmsg for addresses and rtmsg for routes. Attributes (rtattr) are padded to 4 bytes as RTA_ALIGN requires, with helpers to write and read u8, u16, u32 and string values.

On the reply side it decodes the nlmsgerr payload the kernel sends to acknowledge or refuse a request, and list_of_string splits a multipart dump into messages by stepping over each one's 4-byte-aligned length. Message types, flags, address families, interface flags and the usual IFLA_* and IFA_* attribute codes all have named constants.

Usage #

(* a "dump all links" request *)
let dump_links ~seq =
  Netlink.to_string
    {
      Netlink.header =
        {
          length = 0 (* filled in by to_string *);
          typ = Netlink.Type.getlink;
          flags = Netlink.Flags.(request lor dump);
          seq;
          pid = 0;
        };
      payload =
        Netlink.Ifinfomsg.to_string
          { family = Netlink.Af.unspec; typ = 0; index = 0; flags = 0; change = 0 };
    }
  • netlink binds the C libnl library. nox-netlink writes the wire format in OCaml and does not depend on libnl.
  • libmnl is the minimal C userspace netlink library whose framing this codec mirrors.
  • The Go vishvananda/netlink and Rust rtnetlink crates also build rtnetlink messages by hand without libnl.

License #

ISC License. See LICENSE.md for details.