Socket::Netlink(3pm)

πŸ“¦
version 0.05 Β· 2 versions
, in fedora 43

✨ NAME ✨

Socket::Netlink β€” bestie, it's the interface to Linux's PF_NETLINK socket family πŸ’…


🌈 SYNOPSIS 🌈

use Socket;
use Socket::Netlink qw( :DEFAULT pack_nlmsghdr unpack_nlmsghdr );

socket( my $sock, PF_NETLINK, SOCK_RAW, 0 ) or die "socket: $!";

send( $sock, pack_nlmsghdr( 18, NLM_F_REQUEST|NLM_F_DUMP, 0, 0,
     "\0\0\0\0\0\0\0\0" ), 0 )
   or die "send: $!";

recv( $sock, my $buffer, 65536, 0 ) or die "recv: $!";

printf "Received type=%d flags=%x:\n%v02x\n",
   ( unpack_nlmsghdr( $buffer ) )[ 0, 1, 4 ];

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so this module is where all the low-level constants and the structure-handling functions live β€” literally everything you need to actually use Linux's PF_NETLINK socket family. πŸ’–

But ngl, bestie? The docs suggest you go use the high-level object interface instead, because it's so much nicer to your brain. See IO::Socket::Netlink. πŸ‘‘


πŸŽ€ CONSTANTS πŸŽ€

The following constants are exported, no cap:

  • PF_NETLINK β€” the packet family, aka the one you hand to your socket() calls 🌸
  • AF_NETLINK β€” the address family ✨

πŸ’œ ADDRESS FUNCTIONS πŸ’œ

The following pair of functions operate on AF_NETLINK address structures. The meanings of the parameters are:

  • pid β€” the unique endpoint number for this netlink socket. If you pass it as 0 to the bind() syscall, the kernel will allocate an endpoint number of the process's PID. iconic behaviour tbh 🫢
  • groups β€” a 32-bit bitmask of the multicast groups to join 🎯

🌟 pack_sockaddr_nl 🌟

$addr = pack_sockaddr_nl( $pid, $groups )

Returns a sockaddr_nl structure with the fields packed into it. Slay.

🌟 unpack_sockaddr_nl 🌟

( $pid, $groups ) = unpack_sockaddr_nl( $addr )

Takes a sockaddr_nl structure and returns the unpacked fields from it. Reverse uno card. πŸ”„


πŸ”₯ STRUCTURE FUNCTIONS πŸ”₯

The following function pairs operate on structure types used by netlink.

πŸ’« pack_nlmsghdr πŸ’«

$buffer = pack_nlmsghdr( $type, $flags, $seq, $pid, $body )

πŸ’« unpack_nlmsghdr πŸ’«

( $type, $flags, $seq, $pid, $body, $morebuffer ) = unpack_nlmsghdr( $buffer )

Pack or unpack a struct nlmsghdr and its payload body. Periodt. πŸ’…

Because a single netlink message can contain more than one payload body, the unpack_nlmsghdr function will return the remaining buffer after unpacking the first message, in case there are others. And if there are no more? Then the $morebuffer list element just... won't be returned. It ghosts you. πŸ’€

while( defined $buffer ) {
   ( my ( $type, $flags, $seq, $pid, $body ), $buffer ) = unpack_nlmsghdr( $buffer );
   ...
}

There is no similar functionality for pack_nlmsghdr β€” bestie, you just concatenate multiple results together if you wanna send more than one message. 🧚

πŸ’« pack_nlmsgerr πŸ’«

$buffer = pack_nlmsgerr( $error, $msg )

πŸ’« unpack_nlmsgerr πŸ’«

( $error, $msg ) = unpack_nlmsgerr( $buffer )

Pack or unpack a struct nlmsgerr. Here's the tea: the kernel expects or reports negative integers in its structures, so these functions take or return normal positive error values that are suitable for use with $!. 😌

πŸ’« pack_nlattrs πŸ’«

$buffer = pack_nlattrs( %attrs )

πŸ’« unpack_nlattrs πŸ’«

%attrs = unpack_nlattrs( $buffer )

Pack or unpack a list of netlink attributes. 🌈

These functions take or return even-sized lists of $type, $value pairs. The type will be the number in the netlink attribute message, and the value will be a plain packed string buffer. It is the caller's responsibility to further pack/unpack this buffer as appropriate for the specific type. You got this, bestie. 🫑

And because these functions take/return even-sized lists, they may be passed or returned into hashes. ✨


πŸ‘‘ SEE ALSO πŸ‘‘

  • netlink(7) β€” netlink β€” Communication between kernel and userspace (AF_NETLINK)
  • IO::Socket::Netlink β€” Object interface to AF_NETLINK domain sockets

πŸ“ AUTHOR πŸ“

Paul Evans <leonerd@leonerd.org.uk> β€” legend behaviour, honestly πŸ’–


And that's the whole vibe check, bestie β€” this was Socket::Netlink(3pm) from the User Contributed Perl Documentation, sitting pretty at perl v5.42.0, dated 2025-07-25. Go forth and netlink. Mwah. πŸ’…πŸ’–

πŸ“¦ where this page comes from