libnl 1.1.4
Modules
Generic Netlink

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 | | | |
+----------------------------+- - -+- - - - - - - - - - -+- - -+
Netlink message header.
<-------- GENL_HDRLEN -------> <--- hdrlen -->
<------- genlmsg_len(ghdr) ------>
+------------------------+- - -+---------------+- - -+------------+
| Generic Netlink Header | Pad | Family Header | Pad | Attributes |
| struct genlmsghdr | | | | |
+------------------------+- - -+---------------+- - -+------------+
genlmsg_data(ghdr)--------------^ ^
genlmsg_attrdata(ghdr, hdrlen)-------------------------
struct nlattr * genlmsg_attrdata(const struct genlmsghdr *gnlh, int hdrlen)
Get head of attribute data.
Definition: genl.c:212
int genlmsg_len(const struct genlmsghdr *gnlh)
Get lenght of message payload.
Definition: genl.c:200
void * genlmsg_data(const struct genlmsghdr *gnlh)
Get head of message payload.
Definition: genl.c:191
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();
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;
}
int nla_put_u32(struct nl_msg *n, int attrtype, uint32_t value)
Add a u32 netlink attribute to a netlink message.
Definition: attr.c:569
uint32_t nla_get_u32(struct nlattr *nla)
Return payload of u32 attribute.
Definition: attr.c:693
@ NL_CB_VALID
Message is valid.
Definition: handlers.h:96
@ NL_CB_CUSTOM
Customized handler specified by the user.
Definition: handlers.h:84
int genl_ctrl_resolve(struct nl_handle *handle, const char *name)
Resolve generic netlink family name to its identifier.
Definition: ctrl.c:238
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.
Definition: genl.c:247
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition: msg.c:643
void nlmsg_free(struct nl_msg *n)
Free a netlink message.
Definition: msg.c:656
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition: msg.c:401
#define NLM_F_ECHO
Echo this request.
#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:41
#define NL_AUTO_PID
Will cause the netlink pid to be set to the pid assigned to the netlink handle (socket) just before s...
Definition: msg.h:32
int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
Send netlink message and check & extend header values as needed.
Definition: nl.c:373
int nl_recvmsgs_default(struct nl_handle *handle)
Receive a set of message from a netlink socket using handlers in nl_handle.
Definition: nl.c:782
struct nl_handle * nl_handle_alloc(void)
Allocate new netlink socket handle.
Definition: socket.c:206
int nl_socket_modify_cb(struct nl_handle *handle, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Modify the callback handler associated to the socket.
Definition: socket.c:514

Function Documentation

◆ genl_connect()

int genl_connect ( struct nl_handle *  handle)

Definition at line 103 of file genl.c.

104{
105 return nl_connect(handle, NETLINK_GENERIC);
106}
int nl_connect(struct nl_handle *handle, int protocol)
Create and connect netlink socket.
Definition: nl.c:191

◆ genl_send_simple()

int genl_send_simple ( struct nl_handle *  handle,
int  family,
int  cmd,
int  version,
int  flags 
)
Parameters
handleNetlink handle.
familyGeneric netlink family
cmdCommand
versionVersion
flagsAdditional 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.

130{
131 struct genlmsghdr hdr = {
132 .cmd = cmd,
133 .version = version,
134 };
135
136 return nl_send_simple(handle, family, flags, &hdr, sizeof(hdr));
137}
int nl_send_simple(struct nl_handle *handle, int type, int flags, void *buf, size_t size)
Send simple netlink message using nl_send_auto_complete()
Definition: nl.c:410

References nl_send_simple().

◆ genlmsg_valid_hdr()

int genlmsg_valid_hdr ( struct nlmsghdr nlh,
int  hdrlen 
)

Definition at line 147 of file genl.c.

148{
149 struct genlmsghdr *ghdr;
150
151 if (!nlmsg_valid_hdr(nlh, GENL_HDRLEN))
152 return 0;
153
154 ghdr = nlmsg_data(nlh);
155 if (genlmsg_len(ghdr) < NLMSG_ALIGN(hdrlen))
156 return 0;
157
158 return 1;
159}
void * nlmsg_data(const struct nlmsghdr *nlh)
head of message payload
Definition: msg.c:218

◆ genlmsg_validate()

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

Definition at line 161 of file genl.c.

163{
164 struct genlmsghdr *ghdr;
165
166 if (!genlmsg_valid_hdr(nlh, hdrlen))
167 return nl_errno(EINVAL);
168
169 ghdr = nlmsg_data(nlh);
170 return nla_validate(genlmsg_attrdata(ghdr, hdrlen),
171 genlmsg_attrlen(ghdr, hdrlen), maxtype, policy);
172}
int nla_validate(struct nlattr *head, int len, int maxtype, struct nla_policy *policy)
Validate a stream of attributes.
Definition: attr.c:327
int genlmsg_attrlen(const struct genlmsghdr *gnlh, int hdrlen)
Get length of attribute data.
Definition: genl.c:222

◆ genlmsg_parse()

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

Definition at line 174 of file genl.c.

176{
177 struct genlmsghdr *ghdr;
178
179 if (!genlmsg_valid_hdr(nlh, hdrlen))
180 return nl_errno(EINVAL);
181
182 ghdr = nlmsg_data(nlh);
183 return nla_parse(tb, maxtype, genlmsg_attrdata(ghdr, hdrlen),
184 genlmsg_attrlen(ghdr, hdrlen), policy);
185}
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, struct nla_policy *policy)
Parse a stream of attributes into a tb buffer.
Definition: attr.c:262

◆ genlmsg_data()

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

Definition at line 191 of file genl.c.

192{
193 return ((unsigned char *) gnlh + GENL_HDRLEN);
194}

Referenced by genlmsg_attrdata().

◆ genlmsg_len()

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

Definition at line 200 of file genl.c.

201{
202 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
203 NLMSG_HDRLEN);
204 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
205}
uint32_t nlmsg_len
Length of message including header.

References nlmsghdr::nlmsg_len.

Referenced by genlmsg_attrlen().

◆ genlmsg_attrdata()

struct nlattr * genlmsg_attrdata ( const struct genlmsghdr *  gnlh,
int  hdrlen 
)
Parameters
gnlhgeneric netlink message header
hdrlenlength of family specific header

Definition at line 212 of file genl.c.

213{
214 return genlmsg_data(gnlh) + NLMSG_ALIGN(hdrlen);
215}

References genlmsg_data().

◆ genlmsg_attrlen()

int genlmsg_attrlen ( const struct genlmsghdr *  gnlh,
int  hdrlen 
)
Parameters
gnlhgeneric netlink message header
hdrlenlength of family specific header

Definition at line 222 of file genl.c.

223{
224 return genlmsg_len(gnlh) - NLMSG_ALIGN(hdrlen);
225}

References genlmsg_len().

◆ genlmsg_put()

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
msgnetlink message
pidnetlink process id or NL_AUTO_PID
seqsequence number of message or NL_AUTO_SEQ
familygeneric netlink family
hdrlenlength of user specific header
flagsmessage flags
cmdgeneric netlink command
versionprotocol version

Returns pointer to user specific header.

Definition at line 247 of file genl.c.

249{
250 struct nlmsghdr *nlh;
251 struct genlmsghdr hdr = {
252 .cmd = cmd,
253 .version = version,
254 };
255
256 nlh = nlmsg_put(msg, pid, seq, family, GENL_HDRLEN + hdrlen, flags);
257 if (nlh == NULL)
258 return NULL;
259
260 memcpy(nlmsg_data(nlh), &hdr, sizeof(hdr));
261 NL_DBG(2, "msg %p: Added generic netlink header cmd=%d version=%d\n",
262 msg, cmd, version);
263
264 return nlmsg_data(nlh) + GENL_HDRLEN;
265}
struct nlmsghdr * nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq, int type, int payload, int flags)
Add a netlink message header to a netlink message.
Definition: msg.c:610

References nlmsg_data(), and nlmsg_put().