Module: netlink.generic

Provides an implementation of the generic netlink protocol.

class GenericNetlinkMessage
A simple class that holds a generic netlink message.

class GenericNetlinkSocket
Base class for generic netlink families.

class GenericNetlinkController(GenericNetlinkSocket)
Implements the nlctrl family. Can be used to access other families.

class Family
class CommandPolicy
class Policy
These classes contain the response of netlink controller requests.

async with connect() -> GenericNetlinkController
Creates a generic nelink socket. Returns a generic netlink controller that can be used to instantiate other families.

Command Flags

GENL_ADMIN_PERM = 1
GENL_CMD_CAP_DO = 2
GENL_CMD_CAP_DUMP = 4
GENL_CMD_CAP_HASPOL = 8
GENL_UNS_ADMIN_PERM = 16

GenericNetlinkMessage

family: int
flags: int
type: int
version: int
header: bytes
attributes: dict[int, Any]

GenericNetlinkSocket

This class and its subclasses should not be instantiated directly. Instead, one should obtain an instance from GenericNetlinkController.get() or another function.

def add_membership(name: str) -> None
Adds the underlying netlink socket to a multicast group.

async def receive() -> GenericNetlinkMessage
Receives a netlink message from the kernel for the netlink family that belongs to this socket.

async def request(cmd: int, attrs: dict[int, Any], flags: int = 0, header: bytes = b"") -> list[GenericNetlinkMessage]
Sends a generic netlink request to the kernel and waits for an acknowledgement. The flags argument can be used to specify additional flags (e.g. NLM_F_DUMP). The flags NLM_F_REQUEST and NLM_F_ACK are always added to the request automatically. Returns the messages that were received from the kernel with a matching sequence id. Raises OSError if the kernel returns an error code.

GenericNetlinkController

This class inherits GenericNetlinkSocket. It provides a simple interface for nlctrl and can also be used to instantiate other netlink families.

async def get(name: str, cls: type[GenericNetlinkSocket]) -> GenericNetlinkSocket
Creates an instance of the given class and connects it to the given netlink family.

async def get_families() -> list[Family]
Requests the list of generic netlink families that are provided by the kernel.

async def get_family_by_id(id: int) -> Family
Requests information about a specific family by id.

async def get_family_by_name(name: str) -> Family
Requests information about a specific family by name.

async def get_policy_by_id(id: int, cmd: int | None = None) -> Policy | None
Requests the policy for all commands of the given family id, or a specific command if cmd is given.

async def get_policy_by_name(name: str, cmd: int | None = None) -> Policy | None
Requests the policy for all commands of the given family name, or a specific command if cmd is given.

Family

id: int
name: str
version: int
hdrsize: int
maxattr: int
commands: dict[int, int]
mcast_groups: dict[str, int]

CommandPolicy

do: int | None
dump: int | None

Policy

policies: dict[int, dict[int, attributes.Policy]]
commands: dict[int, CommandPolicy]