Module netlink

Source
Expand description

§Purpose

This module provides functions to query the Linux kernel’s networking subsystems via netlink sockets. It is used to fetch information such as network interface details, IP addresses, routes, and neighbor (ARP) entries.

§How it works

It communicates with the kernel using a raw NETLINK_ROUTE socket. The netlink-packet crates are used to construct, serialize, and deserialize netlink messages. A generic netlink function handles the common pattern of sending a request and processing a potentially multi-part response. Specific functions like get_ipv4_routes and get_neighbors use this generic handler to fetch and parse different types of data.

§Main components

  • netlink(): A generic function for the netlink request/response message loop.
  • get_links(), get_ipv4_routes(), get_neighbors(), get_ipv4_address(): Public functions for querying specific kernel data.
  • Link, Ipv4Route, Neighbor: Structs that represent the networking objects retrieved from the kernel.

Structs§

Gateway
Represents a default gateway.
Ipv4Route
Represents an IPv4 route.
Link
Represents a network interface link.
Neighbor
Represents a neighbor (ARP) entry.

Functions§

find_default_gateway
Finds the default gateway from a list of IPv4 routes.
get_ipv4_address
Retrieves IPv4 addresses associated with network interfaces.
get_ipv4_routes
Retrieves a list of IPv4 routes from the kernel.
get_links
Retrieves a list of all network interfaces (links) from the kernel.
get_neighbors
Retrieves a list of neighbor (ARP) entries from the kernel.
netlink
A generic function to send a netlink request and parse the response.