netfilter
firewalling, NAT, and packet mangling for linux
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Generic Netlink
Collaboration diagram for Generic Netlink:

Modules

 Generic Netlink Family
 
 Management
 

Socket Creating

int genl_connect (struct nl_sock *sk)
 

Sending

int genl_send_simple (struct nl_sock *sk, int family, int cmd, int version, int flags)
 Send trivial generic netlink message. More...
 

Message Parsing

int genlmsg_valid_hdr (struct nlmsghdr *nlh, int hdrlen)
 
int genlmsg_validate (struct nlmsghdr *nlh, int hdrlen, int maxtype, struct nla_policy *policy)
 
int genlmsg_parse (struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
 
void * genlmsg_data (const struct genlmsghdr *gnlh)
 Get head of message payload. More...
 
int genlmsg_len (const struct genlmsghdr *gnlh)
 Get lenght of message payload. More...
 
struct nlattrgenlmsg_attrdata (const struct genlmsghdr *gnlh, int hdrlen)
 Get head of attribute data. More...
 
int genlmsg_attrlen (const struct genlmsghdr *gnlh, int hdrlen)
 Get length of attribute data. More...
 

Message Building

void * genlmsg_put (struct nl_msg *msg, uint32_t pid, uint32_t seq, int family, int hdrlen, int flags, uint8_t cmd, uint8_t version)
 Add generic netlink header to netlink message. More...
 

Detailed Description

Message Format
<------- NLMSG_ALIGN(hlen) ------> <---- NLMSG_ALIGN(len) --->
+----------------------------+- - -+- - - - - - - - - - -+- - -+
| Header | Pad | Payload | Pad |
| struct nlmsghdr | | | |
+----------------------------+- - -+- - - - - - - - - - -+- - -+
<-------- GENL_HDRLEN -------> <--- hdrlen -->
<------- genlmsg_len(ghdr) ------>
+------------------------+- - -+---------------+- - -+------------+
| Generic Netlink Header | Pad | Family Header | Pad | Attributes |
| struct genlmsghdr | | | | |
+------------------------+- - -+---------------+- - -+------------+
genlmsg_data(ghdr)--------------^ ^
genlmsg_attrdata(ghdr, hdrlen)-------------------------
Example
struct nl_sock *sock;
struct nl_msg *msg;
int family;
// Allocate a new netlink socket
sock = nl_socket_alloc();
// Connect to generic netlink socket on kernel side
// Ask kernel to resolve family name to family id
family = genl_ctrl_resolve(sock, "generic_netlink_family_name");
// Construct a generic netlink by allocating a new message, fill in
// the header and append a simple integer attribute.
msg = nlmsg_alloc();
CMD_FOO_GET, FOO_VERSION);
nla_put_u32(msg, ATTR_FOO, 123);
// Send message over netlink socket
// Free message
// Prepare socket to receive the answer by specifying the callback
// function to be called for valid messages.
// Wait for the answer and receive it
static int parse_cb(struct nl_msg *msg, void *arg)
{
struct nlmsghdr *nlh = nlmsg_hdr(msg);
struct nlattr *attrs[ATTR_MAX+1];
// Validate message and parse attributes
genlmsg_parse(nlh, 0, attrs, ATTR_MAX, policy);
if (attrs[ATTR_FOO]) {
uint32_t value = nla_get_u32(attrs[ATTR_FOO]);
...
}
return 0;
}

Function Documentation

int genl_connect ( struct nl_sock sk)

References NETLINK_GENERIC, and nl_connect().

Here is the call graph for this function:

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

Send trivial generic netlink message.

  • sk Netlink socket.
  • family Generic netlink family
  • cmd Command
  • version Version
  • flags Additional netlink message flags.

Fills out a routing netlink request message and sends it out using nl_send_simple().

Returns
0 on success or a negative error code.

References genlmsghdr::cmd, nl_send_simple(), and version.

Here is the call graph for this function:

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

Get head of attribute data.

  • gnlh generic netlink message header
  • hdrlen length of family specific header

References genlmsg_data(), and NLMSG_ALIGN.

Referenced by genlmsg_parse(), and genlmsg_validate().

Here is the call graph for this function:

Here is the caller graph for this function:

int genlmsg_attrlen ( const struct genlmsghdr gnlh,
int  hdrlen 
)

Get length of attribute data.

  • gnlh generic netlink message header
  • hdrlen length of family specific header

References genlmsg_len(), and NLMSG_ALIGN.

Referenced by genlmsg_parse(), and genlmsg_validate().

Here is the call graph for this function:

Here is the caller graph for this function:

void* genlmsg_data ( const struct genlmsghdr gnlh)

Get head of message payload.

  • gnlh genetlink messsage header

References GENL_HDRLEN.

Referenced by genlmsg_attrdata().

Here is the caller graph for this function:

int genlmsg_len ( const struct genlmsghdr gnlh)

Get lenght of message payload.

  • gnlh genetlink message header

References GENL_HDRLEN, nlh, NLMSG_HDRLEN, and nlmsghdr::nlmsg_len.

Referenced by genlmsg_attrlen(), and genlmsg_valid_hdr().

Here is the caller graph for this function:

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

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

Here is the call graph for this function:

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

Add generic netlink header to netlink message.

  • msg netlink message
  • pid netlink process id or NL_AUTO_PID
  • seq sequence number of message or NL_AUTO_SEQ
  • family generic netlink family
  • hdrlen length of user specific header
  • flags message flags
  • cmd generic netlink command
  • version protocol version

Returns pointer to user specific header.

References genlmsghdr::cmd, GENL_HDRLEN, NL_DBG, nlh, nlmsg_data(), nlmsg_put(), NULL, and version.

Here is the call graph for this function:

int genlmsg_valid_hdr ( struct nlmsghdr nlh,
int  hdrlen 
)

References GENL_HDRLEN, genlmsg_len(), NLMSG_ALIGN, nlmsg_data(), and nlmsg_valid_hdr().

Referenced by genlmsg_parse(), and genlmsg_validate().

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Here is the call graph for this function: