The Netlink Socket Structures – Part 1

The Socket Address structure for Netlink sockets is provided below (linux/netlink.h).

struct sockaddr_nl {

        sa_family_t nl_family;

        unsigned short nl_pad;

        __u32 nl_pid;

        __u32 nl_groups;

};

The socket structure uses the AF_NETLINK socket family. The nl_pad bytes are zero filled pad-bytes.

The nl_pid is the unicast address of the netlink socket and usually obtained via the getpid () API if a single process is creating the socket. The requirement for nl_pid parameter is that it should be unique to the caller of the API. In case multiple threads invoke the socket API, a mechanism which generates an unique ID can be used to fill the nl_pid parameter. The Linux kernel provides two mechanisms to fill the nl_pid parameter.

  • If a single socket is to be opened – the application can use any means to fill a unique ID in the nl_pid parameter.
  • If the application has to open multiple netlink sockets, then the nl_pid parameter is set to 0. The Linux Kernel then assigns a unique ID t each netlink socket opened by the application.

The nl_group parameter is a bit mask and each bit represents a netlink group that the application might be interested in and wishes to listen to. For example,  if the application wishes to listen to network interface up/down/create/delete events – the RTMGRP_LINK can be added to the nl_group bitmask. Each subsequent group to listen to needs to be OR’ed with RTMGRP_LINK and assigned to nl_group bitmask. The default value of n_group bitmask is zero.

Netlink Socket Structures – Part 2

Reference: https://linux.die.net/man/7/netlink

Comments

  1. Pingback: The Netlink Socket | Hitch Hiker's Guide to Learning

  2. Pingback: Netlink Message Macros | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *