libnl 3.7.0
Generic Netlink Library (libnl-genl)

Modules

 Controller (Resolver)
 Resolves Generic Netlink family names to numeric identifiers.
 
 Family and Command Registration
 Registering Generic Netlink Families and Commands.
 

Generic Netlink Socket

int genl_connect (struct nl_sock *sk)
 Connect a Generic Netlink socket. More...
 

Sending Data

int genl_send_simple (struct nl_sock *sk, int family, int cmd, int version, int flags)
 Send a Generic Netlink message consisting only of a header. More...
 

Message Parsing

int genlmsg_valid_hdr (struct nlmsghdr *nlh, int hdrlen)
 Validate Generic Netlink message headers. More...
 
int genlmsg_validate (struct nlmsghdr *nlh, int hdrlen, int maxtype, const struct nla_policy *policy)
 Validate Generic Netlink message including attributes. More...
 
int genlmsg_parse (struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
 Parse Generic Netlink message including attributes. More...
 
struct genlmsghdr * genlmsg_hdr (struct nlmsghdr *nlh)
 Return pointer to Generic Netlink header. More...
 
int genlmsg_len (const struct genlmsghdr *gnlh)
 Return length of message payload including user header. More...
 
void * genlmsg_user_hdr (const struct genlmsghdr *gnlh)
 Return pointer to user header. More...
 
void * genlmsg_user_data (const struct genlmsghdr *gnlh, const int hdrlen)
 Return pointer to user data. More...
 
int genlmsg_user_datalen (const struct genlmsghdr *gnlh, const int hdrlen)
 Return length of user data. More...
 
struct nlattr * genlmsg_attrdata (const struct genlmsghdr *gnlh, int hdrlen)
 Return pointer to message attributes. More...
 
int genlmsg_attrlen (const struct genlmsghdr *gnlh, int hdrlen)
 Return length of message attributes. More...
 

Message Construction

void * genlmsg_put (struct nl_msg *msg, uint32_t port, uint32_t seq, int family, int hdrlen, int flags, uint8_t cmd, uint8_t version)
 Add Generic Netlink headers to Netlink message. More...
 

Deprecated

void * genlmsg_data (const struct genlmsghdr *gnlh)
 Return pointer to message payload. More...
 

Detailed Description

Function Documentation

◆ genl_connect()

int genl_connect ( struct nl_sock *  sk)

Connect a Generic Netlink socket.

Parameters
skUnconnected Netlink socket

This function expects a struct nl_socket object previously allocated via nl_socket_alloc(). It calls nl_connect() to create the local socket file descriptor and binds the socket to the NETLINK_GENERIC Netlink protocol.

Using this function is equivalent to:

nl_connect(sk, NETLINK_GENERIC);
int nl_connect(struct nl_sock *sk, int protocol)
Create file descriptor and bind socket.
Definition: nl.c:97
See also
nl_connect()
Returns
0 on success or a negative error code.

Definition at line 39 of file genl.c.

References nl_connect().

+ Here is the call graph for this function:

◆ genl_send_simple()

int genl_send_simple ( struct nl_sock *  sk,
int  family,
int  cmd,
int  version,
int  flags 
)

Send a Generic Netlink message consisting only of a header.

Parameters
skGeneric Netlink socket
familyNumeric family identifier
cmdNumeric command identifier
versionInterface version
flagsAdditional Netlink message flags (optional)

This function is a shortcut for sending a Generic Netlink message without any message payload. The message will only consist of the Netlink and Generic Netlink headers. The header is constructed based on the specified parameters and passed on to nl_send_simple() to send it on the specified socket.

Example:
#include <netlink/genl/genl.h>
#include <linux/genetlink.h>
err = genl_send_simple(sk, GENL_ID_CTRL, CTRL_CMD_GETFAMILY, CTRL_VERSION,
NLM_F_DUMP);
int genl_send_simple(struct nl_sock *sk, int family, int cmd, int version, int flags)
Send a Generic Netlink message consisting only of a header.
Definition: genl.c:79
See also
nl_send_simple()
Returns
0 on success or a negative error code. Due to a bug, this function returns the number of bytes sent. Treat any non-negative number as success.

Definition at line 79 of file genl.c.

References nl_send_simple().

+ Here is the call graph for this function:

◆ genlmsg_valid_hdr()

int genlmsg_valid_hdr ( struct nlmsghdr *  nlh,
int  hdrlen 
)

Validate Generic Netlink message headers.

Parameters
nlhPointer to Netlink message header
hdrlenLength of user header

Verifies the integrity of the Netlink and Generic Netlink headers by enforcing the following requirements:

  • Valid Netlink message header (nlmsg_valid_hdr())
  • Presence of a complete Generic Netlink header
  • At least hdrlen bytes of payload included after the generic netlink header.
Returns
A positive integer (true) if the headers are valid or 0 (false) if not.

Definition at line 112 of file genl.c.

References genlmsg_len(), and nlmsg_data().

Referenced by genl_handle_msg(), genlmsg_parse(), and genlmsg_validate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ genlmsg_validate()

int genlmsg_validate ( struct nlmsghdr *  nlh,
int  hdrlen,
int  maxtype,
const struct nla_policy policy 
)

Validate Generic Netlink message including attributes.

Parameters
nlhPointer to Netlink message header
hdrlenLength of user header
maxtypeMaximum attribtue id expected
policyAttribute validation policy

Verifies the validity of the Netlink and Generic Netlink headers using genlmsg_valid_hdr() and calls nla_validate() on the message payload to verify the integrity of eventual attributes.

Note
You may call genlmsg_parse() directly to perform validation and parsing in a single step.
See also
genlmsg_valid_hdr()
nla_validate()
genlmsg_parse()
Returns
0 on success or a negative error code.

Definition at line 146 of file genl.c.

References genlmsg_attrdata(), genlmsg_attrlen(), genlmsg_valid_hdr(), nla_validate(), and nlmsg_data().

+ Here is the call graph for this function:

◆ genlmsg_parse()

int genlmsg_parse ( struct nlmsghdr *  nlh,
int  hdrlen,
struct nlattr *  tb[],
int  maxtype,
const struct nla_policy policy 
)

Parse Generic Netlink message including attributes.

Parameters
nlhPointer to Netlink message header
hdrlenLength of user header
tbArray to store parsed attributes
maxtypeMaximum attribute id expected
policyAttribute validation policy

Verifies the validity of the Netlink and Generic Netlink headers using genlmsg_valid_hdr() and calls nla_parse() on the message payload to parse eventual attributes.

Example:
struct nlattr *attrs[MY_TYPE_MAX+1];
if ((err = genlmsg_parse(nlmsg_hdr(msg), sizeof(struct my_hdr), attrs,
MY_TYPE_MAX, attr_policy)) < 0)
// ERROR
int genlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
Parse Generic Netlink message including attributes.
Definition: genl.c:186
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition: msg.c:536
See also
genlmsg_valid_hdr()
genlmsg_validate()
nla_parse()
Returns
0 on success or a negative error code.

Definition at line 186 of file genl.c.

References genlmsg_attrdata(), genlmsg_attrlen(), genlmsg_valid_hdr(), nla_parse(), and nlmsg_data().

+ Here is the call graph for this function:

◆ genlmsg_hdr()

struct genlmsghdr * genlmsg_hdr ( struct nlmsghdr *  nlh)

Return pointer to Generic Netlink header.

Parameters
nlhNetlink message header
Returns
Pointer to Generic Netlink message header

Definition at line 205 of file genl.c.

References nlmsg_data().

+ Here is the call graph for this function:

◆ genlmsg_len()

int genlmsg_len ( const struct genlmsghdr *  gnlh)

Return length of message payload including user header.

Parameters
gnlhGeneric Netlink message header
See also
genlmsg_data()
Returns
Length of user payload including an eventual user header in number of bytes.

Definition at line 219 of file genl.c.

Referenced by genlmsg_attrlen(), genlmsg_user_datalen(), and genlmsg_valid_hdr().

+ Here is the caller graph for this function:

◆ genlmsg_user_hdr()

void * genlmsg_user_hdr ( const struct genlmsghdr *  gnlh)

Return pointer to user header.

Parameters
gnlhGeneric Netlink message header

Calculates the pointer to the user header based on the pointer to the Generic Netlink message header.

Returns
Pointer to the user header

Definition at line 237 of file genl.c.

References genlmsg_data().

Referenced by genlmsg_user_data().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ genlmsg_user_data()

void * genlmsg_user_data ( const struct genlmsghdr *  gnlh,
const int  hdrlen 
)

Return pointer to user data.

Parameters
gnlhGeneric netlink message header
hdrlenLength of user header

Calculates the pointer to the user data based on the pointer to the Generic Netlink message header.

See also
genlmsg_user_datalen()
Returns
Pointer to the user data

Definition at line 254 of file genl.c.

References genlmsg_user_hdr().

Referenced by genlmsg_attrdata().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ genlmsg_user_datalen()

int genlmsg_user_datalen ( const struct genlmsghdr *  gnlh,
const int  hdrlen 
)

Return length of user data.

Parameters
gnlhGeneric Netlink message header
hdrlenLength of user header
See also
genlmsg_user_data()
Returns
Length of user data in bytes

Definition at line 268 of file genl.c.

References genlmsg_len().

+ Here is the call graph for this function:

◆ genlmsg_attrdata()

struct nlattr * genlmsg_attrdata ( const struct genlmsghdr *  gnlh,
int  hdrlen 
)

Return pointer to message attributes.

Parameters
gnlhGeneric Netlink message header
hdrlenLength of user header
See also
genlmsg_attrlen()
Returns
Pointer to the start of the message's attributes section.

Definition at line 282 of file genl.c.

References genlmsg_user_data().

Referenced by genlmsg_parse(), and genlmsg_validate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ genlmsg_attrlen()

int genlmsg_attrlen ( const struct genlmsghdr *  gnlh,
int  hdrlen 
)

Return length of message attributes.

Parameters
gnlhGeneric Netlink message header
hdrlenLength of user header
See also
genlmsg_attrdata()
Returns
Length of the message section containing attributes in number of bytes.

Definition at line 297 of file genl.c.

References genlmsg_len().

Referenced by genlmsg_parse(), and genlmsg_validate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ genlmsg_put()

void * genlmsg_put ( struct nl_msg *  msg,
uint32_t  port,
uint32_t  seq,
int  family,
int  hdrlen,
int  flags,
uint8_t  cmd,
uint8_t  version 
)

Add Generic Netlink headers to Netlink message.

Parameters
msgNetlink message object
portNetlink port or NL_AUTO_PORT
seqSequence number of message or NL_AUTO_SEQ
familyNumeric family identifier
hdrlenLength of user header
flagsAdditional Netlink message flags (optional)
cmdNumeric command identifier
versionInterface version

Calls nlmsg_put() on the specified message object to reserve space for the Netlink header, the Generic Netlink header, and a user header of specified length. Fills out the header fields with the specified parameters.

Example:
struct nl_msg *msg;
struct my_hdr *user_hdr;
if (!(msg = nlmsg_alloc()))
// ERROR
user_hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, family_id,
sizeof(struct my_hdr), 0, MY_CMD_FOO, 0);
if (!user_hdr)
// ERROR
void * genlmsg_put(struct nl_msg *msg, uint32_t port, uint32_t seq, int family, int hdrlen, int flags, uint8_t cmd, uint8_t version)
Add Generic Netlink headers to Netlink message.
Definition: genl.c:343
#define NL_AUTO_PORT
Will cause the netlink port to be set to the port assigned to the netlink icoket ust before sending t...
Definition: msg.h:29
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition: msg.c:294
#define NL_AUTO_SEQ
May be used to refer to a sequence number which should be automatically set just before sending the m...
Definition: msg.h:40
See also
nlmsg_put()

Returns Pointer to user header or NULL if an error occurred.

Definition at line 343 of file genl.c.

References nlmsg_data(), and nlmsg_put().

+ Here is the call graph for this function:

◆ genlmsg_data()

void * genlmsg_data ( const struct genlmsghdr *  gnlh)

Return pointer to message payload.

Parameters
gnlhGeneric Netlink message header
Deprecated:
This function has been deprecated due to inability to specify the length of the user header. Use genlmsg_user_hdr() respectively genlmsg_user_data().
Returns
Pointer to payload section

Definition at line 380 of file genl.c.

Referenced by genlmsg_user_hdr().

+ Here is the caller graph for this function: