Generic Netlink
[Netlink Families]

Modules

 Generic Netlink Family
 Management

Socket Creating



int genl_connect (struct nl_handle *handle)

Sending



int genl_send_simple (struct nl_handle *handle, int family, int cmd, int version, int flags)
 Send trivial generic netlink message.

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.
int genlmsg_len (const struct genlmsghdr *gnlh)
 Get lenght of message payload.
struct nlattr * genlmsg_attrdata (const struct genlmsghdr *gnlh, int hdrlen)
 Get head of attribute data.
int genlmsg_attrlen (const struct genlmsghdr *gnlh, int hdrlen)
 Get length of attribute data.

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.

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
 #include <netlink/netlink.h>
 #include <netlink/genl/genl.h>
 #include <netlink/genl/ctrl.h>

 struct nl_handle *sock;
 struct nl_msg *msg;
 int family;

 // Allocate a new netlink socket
 sock = nl_handle_alloc();

 // Connect to generic netlink socket on kernel side
 genl_connect(sock);

 // 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();
 genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_ECHO,
             CMD_FOO_GET, FOO_VERSION);
 nla_put_u32(msg, ATTR_FOO, 123);

 // Send message over netlink socket
 nl_send_auto_complete(sock, msg);

 // Free message
 nlmsg_free(msg);

 // Prepare socket to receive the answer by specifying the callback
 // function to be called for valid messages.
 nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, parse_cb, NULL);

 // Wait for the answer and receive it
 nl_recvmsgs_default(sock);

 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_send_simple ( struct nl_handle *  handle,
int  family,
int  cmd,
int  version,
int  flags 
)
Parameters:
handle Netlink handle.
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.

Definition at line 128 of file genl.c.

References nl_send_simple().

00130 {
00131         struct genlmsghdr hdr = {
00132                 .cmd = cmd,
00133                 .version = version,
00134         };
00135 
00136         return nl_send_simple(handle, family, flags, &hdr, sizeof(hdr));
00137 }

void* genlmsg_data ( const struct genlmsghdr *  gnlh  ) 
Parameters:
gnlh genetlink messsage header

Definition at line 191 of file genl.c.

Referenced by genlmsg_attrdata().

00192 {
00193         return ((unsigned char *) gnlh + GENL_HDRLEN);
00194 }

int genlmsg_len ( const struct genlmsghdr *  gnlh  ) 
Parameters:
gnlh genetlink message header

Definition at line 200 of file genl.c.

References nlmsghdr::nlmsg_len.

Referenced by genlmsg_attrlen().

00201 {
00202         struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
00203                                                         NLMSG_HDRLEN);
00204         return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
00205 }

struct nlattr* genlmsg_attrdata ( const struct genlmsghdr *  gnlh,
int  hdrlen 
) [read]
Parameters:
gnlh generic netlink message header
hdrlen length of family specific header

Definition at line 212 of file genl.c.

References genlmsg_data().

00213 {
00214         return genlmsg_data(gnlh) + NLMSG_ALIGN(hdrlen);
00215 }

int genlmsg_attrlen ( const struct genlmsghdr *  gnlh,
int  hdrlen 
)
Parameters:
gnlh generic netlink message header
hdrlen length of family specific header

Definition at line 222 of file genl.c.

References genlmsg_len().

00223 {
00224         return genlmsg_len(gnlh) - NLMSG_ALIGN(hdrlen);
00225 }

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 
)
Parameters:
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.

Definition at line 247 of file genl.c.

References nlmsg_data(), and nlmsg_put().

00249 {
00250         struct nlmsghdr *nlh;
00251         struct genlmsghdr hdr = {
00252                 .cmd = cmd,
00253                 .version = version,
00254         };
00255 
00256         nlh = nlmsg_put(msg, pid, seq, family, GENL_HDRLEN + hdrlen, flags);
00257         if (nlh == NULL)
00258                 return NULL;
00259 
00260         memcpy(nlmsg_data(nlh), &hdr, sizeof(hdr));
00261         NL_DBG(2, "msg %p: Added generic netlink header cmd=%d version=%d\n",
00262                msg, cmd, version);
00263 
00264         return nlmsg_data(nlh) + GENL_HDRLEN;
00265 }


Generated on 30 Oct 2009 for libnl by  doxygen 1.6.1