libnl 1.1.4
Modules | Data Structures | Macros

Netlink Message Construction/Parsing Interface. More...

Modules

 Attributes
 Netlink Attributes Construction/Parsing Interface.
 

Data Structures

struct  nlmsghdr
 Netlink message header. More...
 
struct  nlmsgerr
 Netlink error message. More...
 

Macros

#define NL_AUTO_PID   0
 Will cause the netlink pid to be set to the pid assigned to the netlink handle (socket) just before sending the message off.
 
#define NL_AUTO_SEQ   0
 May be used to refer to a sequence number which should be automatically set just before sending the message off.
 

Netlink Message Type Translations

char * nl_nlmsgtype2str (int type, char *buf, size_t size)
 
int nl_str2nlmsgtype (const char *name)
 

Size Calculations

int nlmsg_msg_size (int payload)
 length of netlink message not including padding
 
int nlmsg_total_size (int payload)
 length of netlink message including padding
 
int nlmsg_padlen (int payload)
 length of padding at the message's tail
 

Payload Access

void * nlmsg_data (const struct nlmsghdr *nlh)
 head of message payload
 
void * nlmsg_tail (const struct nlmsghdr *nlh)
 
int nlmsg_len (const struct nlmsghdr *nlh)
 length of message payload
 

Attribute Access

struct nlattr * nlmsg_attrdata (const struct nlmsghdr *nlh, int hdrlen)
 head of attributes data
 
int nlmsg_attrlen (const struct nlmsghdr *nlh, int hdrlen)
 length of attributes data
 

Message Parsing

int nlmsg_valid_hdr (const struct nlmsghdr *nlh, int hdrlen)
 
int nlmsg_ok (const struct nlmsghdr *nlh, int remaining)
 check if the netlink message fits into the remaining bytes
 
struct nlmsghdrnlmsg_next (struct nlmsghdr *nlh, int *remaining)
 next netlink message in message stream
 
int nlmsg_parse (struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
 parse attributes of a netlink message
 
struct nlattr * nlmsg_find_attr (struct nlmsghdr *nlh, int hdrlen, int attrtype)
 nlmsg_find_attr - find a specific attribute in a netlink message
 
int nlmsg_validate (struct nlmsghdr *nlh, int hdrlen, int maxtype, struct nla_policy *policy)
 nlmsg_validate - validate a netlink message including attributes
 

Message Building/Access

struct nl_msg * nlmsg_alloc (void)
 Allocate a new netlink message with the default maximum payload size.
 
struct nl_msg * nlmsg_alloc_size (size_t max)
 Allocate a new netlink message with maximum payload size specified.
 
struct nl_msg * nlmsg_inherit (struct nlmsghdr *hdr)
 Allocate a new netlink message and inherit netlink message header.
 
struct nl_msg * nlmsg_alloc_simple (int nlmsgtype, int flags)
 Allocate a new netlink message.
 
void nlmsg_set_default_size (size_t max)
 Set the default maximum message payload size for allocated messages.
 
struct nl_msg * nlmsg_convert (struct nlmsghdr *hdr)
 Convert a netlink message received from a netlink socket to a nl_msg.
 
void * nlmsg_reserve (struct nl_msg *n, size_t len, int pad)
 Reserve room for additional data in a netlink message.
 
int nlmsg_append (struct nl_msg *n, void *data, size_t len, int pad)
 Append data to tail of a netlink message.
 
int nlmsg_expand (struct nl_msg *n, size_t newlen)
 Expand maximum payload size of a netlink message.
 
struct nlmsghdrnlmsg_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.
 
struct nlmsghdrnlmsg_hdr (struct nl_msg *n)
 Return actual netlink message.
 
void nlmsg_free (struct nl_msg *n)
 Free a netlink message.
 

Attributes

void nlmsg_set_proto (struct nl_msg *msg, int protocol)
 
int nlmsg_get_proto (struct nl_msg *msg)
 
size_t nlmsg_get_max_size (struct nl_msg *msg)
 
void nlmsg_set_src (struct nl_msg *msg, struct sockaddr_nl *addr)
 
struct sockaddr_nlnlmsg_get_src (struct nl_msg *msg)
 
void nlmsg_set_dst (struct nl_msg *msg, struct sockaddr_nl *addr)
 
struct sockaddr_nlnlmsg_get_dst (struct nl_msg *msg)
 
void nlmsg_set_creds (struct nl_msg *msg, struct ucred *creds)
 
struct ucred * nlmsg_get_creds (struct nl_msg *msg)
 

Netlink Message Flags Translations

char * nl_nlmsg_flags2str (int flags, char *buf, size_t len)
 

Direct Parsing

int nl_msg_parse (struct nl_msg *msg, void(*cb)(struct nl_object *, void *), void *arg)
 

Dumping

void nl_msg_dump (struct nl_msg *msg, FILE *ofd)
 Dump message in human readable format to file descriptor.
 

Iterators

#define nlmsg_for_each_attr(pos, nlh, hdrlen, rem)
 Iterate over a stream of attributes in a message.
 
#define nlmsg_for_each_msg(pos, head, len, rem)
 Iterate over a stream of messages.
 

Standard message flags

#define NLM_F_REQUEST   1
 Must be set on all request messages (typically from user space to kernel space).
 
#define NLM_F_MULTI   2
 Indicates the message is part of a multipart message terminated by NLMSG_DONE.
 
#define NLM_F_ACK   4
 Request for an acknowledgment on success.
 
#define NLM_F_ECHO   8
 Echo this request.
 

Additional message flags for GET requests

#define NLM_F_ROOT   0x100
 Return the complete table instead of a single entry.
 
#define NLM_F_MATCH   0x200
 Return all entries matching criteria passed in message content.
 
#define NLM_F_ATOMIC   0x400
 Return an atomic snapshot of the table being referenced.
 
#define NLM_F_DUMP   (NLM_F_ROOT|NLM_F_MATCH)
 Dump all entries.
 

Additional messsage flags for NEW requests

#define NLM_F_REPLACE   0x100
 Replace existing matching config object with this request.
 
#define NLM_F_EXCL   0x200
 Don't replace the config object if it already exists.
 
#define NLM_F_CREATE   0x400
 Create config object if it doesn't already exist.
 
#define NLM_F_APPEND   0x800
 Add to the end of the object list.
 

Standard Message types

#define NLMSG_NOOP   0x1
 No operation, message must be ignored.
 
#define NLMSG_ERROR   0x2
 The message signals an error and the payload contains a nlmsgerr structure.
 
#define NLMSG_DONE   0x3
 Message terminates a multipart message.
 
#define NLMSG_OVERRUN   0x4
 The message signals that data got lost.
 
#define NLMSG_MIN_TYPE   0x10
 Lower limit of reserved message types.
 

Detailed Description

The following information is partly extracted from RFC3549 (ftp://ftp.rfc-editor.org/in-notes/rfc3549.txt)

Message Format
Netlink messages consist of a byte stream with one or multiple Netlink headers and an associated payload. If the payload is too big to fit into a single message it, can be split over multiple Netlink messages, collectively called a multipart message. For multipart messages, the first and all following headers have the NLM_F_MULTI Netlink header flag set, except for the last header which has the Netlink header type NLMSG_DONE.
The Netlink message header (struct nlmsghdr) is shown below.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Flags |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Process ID (PID) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
The netlink message header and payload must be aligned properly:
<------- NLMSG_ALIGN(hlen) ------> <---- NLMSG_ALIGN(len) --->
+----------------------------+- - -+- - - - - - - - - - -+- - -+
| Header | Pad | Payload | Pad |
| struct nlmsghdr | | | |
+----------------------------+- - -+- - - - - - - - - - -+- - -+
Netlink message header.
Message Format:
<--- nlmsg_total_size(payload) --->
<-- nlmsg_msg_size(payload) ->
+----------+- - -+-------------+- - -+-------- - -
| nlmsghdr | Pad | Payload | Pad | nlmsghdr
+----------+- - -+-------------+- - -+-------- - -
nlmsg_data(nlh)---^ ^
nlmsg_next(nlh)-----------------------+
int nlmsg_total_size(int payload)
length of netlink message including padding
Definition: msg.c:193
void * nlmsg_data(const struct nlmsghdr *nlh)
head of message payload
Definition: msg.c:218
int nlmsg_msg_size(int payload)
length of netlink message not including padding
Definition: msg.c:184
struct nlmsghdr * nlmsg_next(struct nlmsghdr *nlh, int *remaining)
next netlink message in message stream
Definition: msg.c:300
The payload may consist of arbitary data but may have strict alignment and formatting rules depening on the specific netlink families.
<---------------------- nlmsg_len(nlh) --------------------->
<------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) ->
+----------------------+- - -+--------------------------------+
| Family Header | Pad | Attributes |
+----------------------+- - -+--------------------------------+
nlmsg_attrdata(nlh, hdrlen)---^
int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
length of attributes data
Definition: msg.c:260
struct nlattr * nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
head of attributes data
Definition: msg.c:249
int nlmsg_len(const struct nlmsghdr *nlh)
length of message payload
Definition: msg.c:232
The ACK Netlink Message
This message is actually used to denote both an ACK and a NACK. Typically, the direction is from FEC to CPC (in response to an ACK request message). However, the CPC should be able to send ACKs back to FEC when requested.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Netlink message header |
| type = NLMSG_ERROR |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Error code |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| OLD Netlink message header |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#define NLMSG_ERROR
The message signals an error and the payload contains a nlmsgerr structure.
Example
// Various methods exist to create/allocate a new netlink
// message.
//
// nlmsg_alloc() will allocate an empty netlink message with
// a maximum payload size which defaults to the page size of
// the system. This default size can be modified using the
// function nlmsg_set_default_size().
struct nl_msg *msg = nlmsg_alloc();
// Very often, the message type and message flags are known
// at allocation time while the other fields are auto generated:
struct nl_msg *msg = nlmsg_alloc_simple(MY_TYPE, MY_FLAGS);
// Alternatively an existing netlink message header can be used
// to inherit the header values:
struct nlmsghdr hdr = {
.nlmsg_type = MY_TYPE,
.nlmsg_flags = MY_FLAGS,
};
struct nl_msg *msg = nlmsg_inherit(&hdr);
// Last but not least, netlink messages received from netlink sockets
// can be converted into nl_msg objects using nlmsg_convert(). This
// will create a message with a maximum payload size which equals the
// length of the existing netlink message, therefore no more data can
// be appened without calling nlmsg_expand() first.
struct nl_msg *msg = nlmsg_convert(nlh_from_nl_sock);
// Payload may be added to the message via nlmsg_append(). The fourth
// parameter specifies the number of alignment bytes the data should
// be padding with at the end. Common values are 0 to disable it or
// NLMSG_ALIGNTO to ensure proper netlink message padding.
nlmsg_append(msg, &mydata, sizeof(mydata), 0);
// Sometimes it may be necessary to reserve room for data but defer
// the actual copying to a later point, nlmsg_reserve() can be used
// for this purpose:
void *data = nlmsg_reserve(msg, sizeof(mydata), NLMSG_ALIGNTO);
// Attributes may be added using the attributes interface.
// After successful use of the message, the memory must be freed
// using nlmsg_free()
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition: msg.c:448
void nlmsg_free(struct nl_msg *n)
Free a netlink message.
Definition: msg.c:656
void * nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
Reserve room for additional data in a netlink message.
Definition: msg.c:512
struct nl_msg * nlmsg_convert(struct nlmsghdr *hdr)
Convert a netlink message received from a netlink socket to a nl_msg.
Definition: msg.c:484
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition: msg.c:401
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition: msg.c:549
struct nl_msg * nlmsg_inherit(struct nlmsghdr *hdr)
Allocate a new netlink message and inherit netlink message header.
Definition: msg.c:424
uint16_t nlmsg_type
Message type (content type)
4) Parsing messages
int n;
unsigned char *buf;
struct nlmsghdr *hdr;
n = nl_recv(handle, NULL, &buf);
hdr = (struct nlmsghdr *) buf;
while (nlmsg_ok(hdr, n)) {
// Process message here...
hdr = nlmsg_next(hdr, &n);
}
int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
check if the netlink message fits into the remaining bytes
Definition: msg.c:285
int nl_recv(struct nl_handle *handle, struct sockaddr_nl *nla, unsigned char **buf, struct ucred **creds)
Receive data from netlink socket.
Definition: nl.c:460

Macro Definition Documentation

◆ NL_AUTO_PID

#define NL_AUTO_PID   0
Note
Requires the use of nl_send_auto_complete()!

Definition at line 32 of file msg.h.

◆ NL_AUTO_SEQ

#define NL_AUTO_SEQ   0
Note
Requires the use of nl_send_auto_complete()!

Definition at line 41 of file msg.h.

◆ nlmsg_for_each_attr

#define nlmsg_for_each_attr (   pos,
  nlh,
  hdrlen,
  rem 
)
Value:
nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
nlmsg_attrlen(nlh, hdrlen), rem)
#define nla_for_each_attr(pos, head, len, rem)
iterate over a stream of attributes
Definition: attr.h:264
Parameters
posloop counter, set to current attribute
nlhnetlink message header
hdrlenlength of family header
reminitialized to len, holds bytes currently remaining in stream

Definition at line 126 of file msg.h.

◆ NLM_F_REQUEST

#define NLM_F_REQUEST   1

Definition at line 65 of file netlink-kernel.h.

◆ NLM_F_ROOT

#define NLM_F_ROOT   0x100

Definition at line 94 of file netlink-kernel.h.

◆ NLM_F_REPLACE

#define NLM_F_REPLACE   0x100

Definition at line 124 of file netlink-kernel.h.

◆ NLMSG_NOOP

#define NLMSG_NOOP   0x1

Definition at line 152 of file netlink-kernel.h.

◆ nlmsg_for_each_msg

#define nlmsg_for_each_msg (   pos,
  head,
  len,
  rem 
)
Value:
for (pos = head, rem = len; \
nlmsg_ok(pos, rem); \
pos = nlmsg_next(pos, &(rem)))
Parameters
posloop counter, set to current message
headhead of message stream
lenlength of message stream
reminitialized to len, holds bytes currently remaining in stream

Definition at line 137 of file msg.h.

◆ NLM_F_MULTI

#define NLM_F_MULTI   2

Definition at line 71 of file netlink-kernel.h.

◆ NLM_F_ACK

#define NLM_F_ACK   4

Definition at line 76 of file netlink-kernel.h.

◆ NLM_F_ECHO

#define NLM_F_ECHO   8

Definition at line 81 of file netlink-kernel.h.

◆ NLM_F_MATCH

#define NLM_F_MATCH   0x200

Definition at line 99 of file netlink-kernel.h.

◆ NLM_F_ATOMIC

#define NLM_F_ATOMIC   0x400

This may require special privileges because it has the potential to interrupt service in the FE for a longer time.

Definition at line 106 of file netlink-kernel.h.

◆ NLM_F_DUMP

#define NLM_F_DUMP   (NLM_F_ROOT|NLM_F_MATCH)

Definition at line 111 of file netlink-kernel.h.

◆ NLM_F_EXCL

#define NLM_F_EXCL   0x200

Definition at line 129 of file netlink-kernel.h.

◆ NLM_F_CREATE

#define NLM_F_CREATE   0x400

Definition at line 134 of file netlink-kernel.h.

◆ NLM_F_APPEND

#define NLM_F_APPEND   0x800

Definition at line 139 of file netlink-kernel.h.

◆ NLMSG_ERROR

#define NLMSG_ERROR   0x2

This can be looked at as a NACK and typically it is from FEC to CPC.

Definition at line 159 of file netlink-kernel.h.

◆ NLMSG_DONE

#define NLMSG_DONE   0x3

Definition at line 164 of file netlink-kernel.h.

◆ NLMSG_OVERRUN

#define NLMSG_OVERRUN   0x4

Definition at line 169 of file netlink-kernel.h.

◆ NLMSG_MIN_TYPE

#define NLMSG_MIN_TYPE   0x10

Definition at line 174 of file netlink-kernel.h.

Function Documentation

◆ nlmsg_msg_size()

int nlmsg_msg_size ( int  payload)
Parameters
payloadlength of message payload

Definition at line 184 of file msg.c.

185{
186 return NLMSG_HDRLEN + payload;
187}

Referenced by nl_msg_dump(), nlmsg_padlen(), and nlmsg_total_size().

◆ nlmsg_total_size()

int nlmsg_total_size ( int  payload)
Parameters
payloadlength of message payload

Definition at line 193 of file msg.c.

194{
195 return NLMSG_ALIGN(nlmsg_msg_size(payload));
196}

References nlmsg_msg_size().

Referenced by nlmsg_padlen(), and nlmsg_set_default_size().

◆ nlmsg_padlen()

int nlmsg_padlen ( int  payload)
Parameters
payloadlength of message payload

Definition at line 202 of file msg.c.

203{
204 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
205}

References nlmsg_msg_size(), and nlmsg_total_size().

◆ nlmsg_data()

void * nlmsg_data ( const struct nlmsghdr nlh)
Parameters
nlhnetlink messsage header

Definition at line 218 of file msg.c.

219{
220 return (unsigned char *) nlh + NLMSG_HDRLEN;
221}

Referenced by genlmsg_put(), nfnlmsg_family(), nfnlmsg_res_id(), nl_msg_dump(), nla_put(), nla_put_nested(), nla_reserve(), and nlmsg_attrdata().

◆ nlmsg_tail()

void * nlmsg_tail ( const struct nlmsghdr nlh)

Definition at line 223 of file msg.c.

224{
225 return (unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
226}
uint32_t nlmsg_len
Length of message including header.

◆ nlmsg_len()

int nlmsg_len ( const struct nlmsghdr nlh)
Parameters
nlhnetlink message header

Definition at line 232 of file msg.c.

233{
234 return nlh->nlmsg_len - NLMSG_HDRLEN;
235}

References nlmsghdr::nlmsg_len.

Referenced by nl_msg_dump(), nla_put_nested(), nlmsg_attrlen(), and nlmsg_reserve().

◆ nlmsg_attrdata()

struct nlattr * nlmsg_attrdata ( const struct nlmsghdr nlh,
int  hdrlen 
)
Parameters
nlhnetlink message header
hdrlenlength of family specific header

Definition at line 249 of file msg.c.

250{
251 unsigned char *data = nlmsg_data(nlh);
252 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
253}

References nlmsg_data().

Referenced by nl_msg_dump(), nlmsg_find_attr(), nlmsg_parse(), and nlmsg_validate().

◆ nlmsg_attrlen()

int nlmsg_attrlen ( const struct nlmsghdr nlh,
int  hdrlen 
)
Parameters
nlhnetlink message header
hdrlenlength of family specific header

Definition at line 260 of file msg.c.

261{
262 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
263}

References nlmsg_len().

Referenced by nl_msg_dump(), nlmsg_find_attr(), nlmsg_parse(), and nlmsg_validate().

◆ nlmsg_valid_hdr()

int nlmsg_valid_hdr ( const struct nlmsghdr nlh,
int  hdrlen 
)

Definition at line 272 of file msg.c.

273{
274 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
275 return 0;
276
277 return 1;
278}

◆ nlmsg_ok()

int nlmsg_ok ( const struct nlmsghdr nlh,
int  remaining 
)
Parameters
nlhnetlink message header
remainingnumber of bytes remaining in message stream

Definition at line 285 of file msg.c.

286{
287 return (remaining >= (int)sizeof(struct nlmsghdr) &&
288 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
289 nlh->nlmsg_len <= remaining);
290}

References nlmsghdr::nlmsg_len.

◆ nlmsg_next()

struct nlmsghdr * nlmsg_next ( struct nlmsghdr nlh,
int *  remaining 
)
Parameters
nlhnetlink message header
remainingnumber of bytes remaining in message stream
Returns
the next netlink message in the message stream and decrements remaining by the size of the current message.

Definition at line 300 of file msg.c.

301{
302 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
303
304 *remaining -= totlen;
305
306 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
307}

References nlmsghdr::nlmsg_len.

◆ nlmsg_parse()

int nlmsg_parse ( struct nlmsghdr nlh,
int  hdrlen,
struct nlattr *  tb[],
int  maxtype,
struct nla_policy policy 
)
Parameters
nlhnetlink message header
hdrlenlength of family specific header
tbdestination array with maxtype+1 elements
maxtypemaximum attribute type to be expected
policyvalidation policy

See nla_parse()

Definition at line 319 of file msg.c.

321{
322 if (!nlmsg_valid_hdr(nlh, hdrlen))
323 return nl_errno(EINVAL);
324
325 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
326 nlmsg_attrlen(nlh, hdrlen), policy);
327}
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

References nla_parse(), nlmsg_attrdata(), and nlmsg_attrlen().

◆ nlmsg_find_attr()

struct nlattr * nlmsg_find_attr ( struct nlmsghdr nlh,
int  hdrlen,
int  attrtype 
)
Parameters
nlhnetlink message header
hdrlenlength of familiy specific header
attrtypetype of attribute to look for

Returns the first attribute which matches the specified type.

Definition at line 337 of file msg.c.

338{
339 return nla_find(nlmsg_attrdata(nlh, hdrlen),
340 nlmsg_attrlen(nlh, hdrlen), attrtype);
341}
struct nlattr * nla_find(struct nlattr *head, int len, int attrtype)
Find a specific attribute in a stream of attributes.
Definition: attr.c:352

References nla_find(), nlmsg_attrdata(), and nlmsg_attrlen().

◆ nlmsg_validate()

int nlmsg_validate ( struct nlmsghdr nlh,
int  hdrlen,
int  maxtype,
struct nla_policy policy 
)
Parameters
nlhnetlinket message header
hdrlenlength of familiy specific header
maxtypemaximum attribute type to be expected
policyvalidation policy

Definition at line 350 of file msg.c.

352{
353 if (!nlmsg_valid_hdr(nlh, hdrlen))
354 return nl_errno(EINVAL);
355
356 return nla_validate(nlmsg_attrdata(nlh, hdrlen),
357 nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
358}
int nla_validate(struct nlattr *head, int len, int maxtype, struct nla_policy *policy)
Validate a stream of attributes.
Definition: attr.c:327

References nla_validate(), nlmsg_attrdata(), and nlmsg_attrlen().

◆ nlmsg_alloc()

struct nl_msg * nlmsg_alloc ( void  )

Allocates a new netlink message without any further payload. The maximum payload size defaults to PAGESIZE or as otherwise specified with nlmsg_set_default_size().

Returns
Newly allocated netlink message or NULL.

Definition at line 401 of file msg.c.

402{
403 return __nlmsg_alloc(default_msg_size);
404}

Referenced by nlmsg_inherit(), and rtnl_neightbl_build_change_request().

◆ nlmsg_alloc_size()

struct nl_msg * nlmsg_alloc_size ( size_t  max)

Definition at line 409 of file msg.c.

410{
411 return __nlmsg_alloc(max);
412}

◆ nlmsg_inherit()

struct nl_msg * nlmsg_inherit ( struct nlmsghdr hdr)
Parameters
hdrNetlink message header template

Allocates a new netlink message and inherits the original message header. If hdr is not NULL it will be used as a template for the netlink message header, otherwise the header is left blank.

Returns
Newly allocated netlink message or NULL

Definition at line 424 of file msg.c.

425{
426 struct nl_msg *nm;
427
428 nm = nlmsg_alloc();
429 if (nm && hdr) {
430 struct nlmsghdr *new = nm->nm_nlh;
431
432 new->nlmsg_type = hdr->nlmsg_type;
433 new->nlmsg_flags = hdr->nlmsg_flags;
434 new->nlmsg_seq = hdr->nlmsg_seq;
435 new->nlmsg_pid = hdr->nlmsg_pid;
436 }
437
438 return nm;
439}
uint32_t nlmsg_pid
Netlink PID of the proccess sending the message.
uint16_t nlmsg_flags
Message flags.
uint32_t nlmsg_seq
Sequence number.

References nlmsg_alloc(), nlmsghdr::nlmsg_flags, nlmsghdr::nlmsg_pid, nlmsghdr::nlmsg_seq, and nlmsghdr::nlmsg_type.

Referenced by nl_msg_dump(), and nlmsg_alloc_simple().

◆ nlmsg_alloc_simple()

struct nl_msg * nlmsg_alloc_simple ( int  nlmsgtype,
int  flags 
)
Parameters
nlmsgtypeNetlink message type
flagsMessage flags.
Returns
Newly allocated netlink message or NULL.

Definition at line 448 of file msg.c.

449{
450 struct nl_msg *msg;
451 struct nlmsghdr nlh = {
452 .nlmsg_type = nlmsgtype,
453 .nlmsg_flags = flags,
454 };
455
456 msg = nlmsg_inherit(&nlh);
457 if (msg)
458 NL_DBG(2, "msg %p: Allocated new simple message\n", msg);
459
460 return msg;
461}

References nlmsg_inherit(), and nlmsghdr::nlmsg_type.

Referenced by flnl_lookup_build_request(), nfnlmsg_alloc_simple(), nl_send_simple(), rtnl_link_build_change_request(), rtnl_neightbl_build_change_request(), and rtnl_qdisc_build_delete_request().

◆ nlmsg_set_default_size()

void nlmsg_set_default_size ( size_t  max)
Parameters
maxSize of payload in bytes.

Definition at line 467 of file msg.c.

468{
469 if (max < nlmsg_total_size(0))
470 max = nlmsg_total_size(0);
471
472 default_msg_size = max;
473}

References nlmsg_total_size().

◆ nlmsg_convert()

struct nl_msg * nlmsg_convert ( struct nlmsghdr hdr)
Parameters
hdrNetlink message received from netlink socket.

Allocates a new netlink message and copies all of the data pointed to by hdr into the new message object.

Returns
Newly allocated netlink message or NULL.

Definition at line 484 of file msg.c.

485{
486 struct nl_msg *nm;
487
488 nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
489 if (!nm)
490 goto errout;
491
492 memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
493
494 return nm;
495errout:
496 nlmsg_free(nm);
497 return NULL;
498}

References nlmsg_free(), and nlmsghdr::nlmsg_len.

◆ nlmsg_reserve()

void * nlmsg_reserve ( struct nl_msg *  n,
size_t  len,
int  pad 
)
Parameters
nnetlink message
lenlength of additional data to reserve room for
padnumber of bytes to align data to

Reserves room for additional data at the tail of the an existing netlink message. Eventual padding required will be zeroed out.

Returns
Pointer to start of additional data tailroom or NULL.

Definition at line 512 of file msg.c.

513{
514 void *buf = n->nm_nlh;
515 size_t nlmsg_len = n->nm_nlh->nlmsg_len;
516 size_t tlen;
517
518 tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
519
520 if ((tlen + nlmsg_len) > n->nm_size) {
521 nl_errno(ENOBUFS);
522 return NULL;
523 }
524
525 buf += nlmsg_len;
526 n->nm_nlh->nlmsg_len += tlen;
527
528 if (tlen > len)
529 memset(buf + len, 0, tlen - len);
530
531 NL_DBG(2, "msg %p: Reserved %zu bytes, pad=%d, nlmsg_len=%d\n",
532 n, len, pad, n->nm_nlh->nlmsg_len);
533
534 return buf;
535}

References nlmsg_len().

Referenced by nlmsg_append(), and nlmsg_put().

◆ nlmsg_append()

int nlmsg_append ( struct nl_msg *  n,
void *  data,
size_t  len,
int  pad 
)
Parameters
nnetlink message
datadata to add
lenlength of data
padNumber of bytes to align data to.

Extends the netlink message as needed and appends the data of given length to the message.

Returns
0 on success or a negative error code

Definition at line 549 of file msg.c.

550{
551 void *tmp;
552
553 tmp = nlmsg_reserve(n, len, pad);
554 if (tmp == NULL)
555 return nl_errno(ENOMEM);
556
557 memcpy(tmp, data, len);
558 NL_DBG(2, "msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
559
560 return 0;
561}

References nlmsg_reserve().

Referenced by flnl_lookup_build_request(), nl_send_simple(), rtnl_link_build_change_request(), rtnl_neightbl_build_change_request(), and rtnl_qdisc_build_delete_request().

◆ nlmsg_expand()

int nlmsg_expand ( struct nl_msg *  n,
size_t  newlen 
)
Parameters
nNetlink message.
newlenNew maximum payload size.

Reallocates the payload section of a netlink message and increases the maximum payload size of the message.

Note
Any pointers pointing to old payload block will be stale and need to be refetched. Therfore, do not expand while constructing nested attributes or while reserved data blocks are held.
Returns
0 on success or a negative error code.

Definition at line 577 of file msg.c.

578{
579 void *tmp;
580
581 if (newlen <= n->nm_size)
582 return nl_errno(EINVAL);
583
584 tmp = realloc(n->nm_nlh, newlen);
585 if (tmp == NULL)
586 return nl_errno(ENOMEM);
587
588 n->nm_nlh = tmp;
589 n->nm_size = newlen;
590
591 return 0;
592}

◆ nlmsg_put()

struct nlmsghdr * nlmsg_put ( struct nl_msg *  n,
uint32_t  pid,
uint32_t  seq,
int  type,
int  payload,
int  flags 
)
Parameters
nnetlink message
pidnetlink process id or NL_AUTO_PID
seqsequence number of message or NL_AUTO_SEQ
typemessage type
payloadlength of message payload
flagsmessage flags

Adds or overwrites the netlink message header in an existing message object. If payload is greater-than zero additional room will be reserved, f.e. for family specific headers. It can be accesed via nlmsg_data().

Returns
A pointer to the netlink message header or NULL.

Definition at line 610 of file msg.c.

612{
613 struct nlmsghdr *nlh;
614
615 if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
616 BUG();
617
618 nlh = (struct nlmsghdr *) n->nm_nlh;
619 nlh->nlmsg_type = type;
620 nlh->nlmsg_flags = flags;
621 nlh->nlmsg_pid = pid;
622 nlh->nlmsg_seq = seq;
623
624 NL_DBG(2, "msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
625 "seq=%d\n", n, type, flags, pid, seq);
626
627 if (payload > 0 &&
628 nlmsg_reserve(n, payload, NLMSG_ALIGNTO) == NULL)
629 return NULL;
630
631 return nlh;
632}

References nlmsghdr::nlmsg_flags, nlmsghdr::nlmsg_pid, nlmsg_reserve(), nlmsghdr::nlmsg_seq, and nlmsghdr::nlmsg_type.

Referenced by genlmsg_put(), and nfnlmsg_put().

◆ nlmsg_hdr()

struct nlmsghdr * nlmsg_hdr ( struct nl_msg *  n)
Parameters
nnetlink message

Returns the actual netlink message casted to the type of the netlink message header.

Returns
A pointer to the netlink message.

Definition at line 643 of file msg.c.

644{
645 return n->nm_nlh;
646}

Referenced by nl_cache_parse_and_add(), nl_msg_dump(), nl_send_auto_complete(), and nl_sendmsg().

◆ nlmsg_free()

void nlmsg_free ( struct nl_msg *  n)
Parameters
nnetlink message

Destroys a netlink message and frees up all used memory.

Precondition
The message must be unused.

Definition at line 656 of file msg.c.

657{
658 if (!n)
659 return;
660
661 free(n->nm_nlh);
662 free(n);
663 NL_DBG(2, "msg %p: Freed\n", n);
664}

Referenced by flnl_lookup(), flnl_lookup_build_request(), nfnlmsg_alloc_simple(), nl_msg_dump(), nl_send_simple(), nlmsg_convert(), rtnl_addr_add(), rtnl_addr_delete(), rtnl_class_add(), rtnl_cls_add(), rtnl_cls_change(), rtnl_cls_delete(), rtnl_link_build_change_request(), rtnl_link_change(), rtnl_neigh_add(), rtnl_neigh_change(), rtnl_neigh_delete(), rtnl_neightbl_build_change_request(), rtnl_neightbl_change(), rtnl_qdisc_add(), rtnl_qdisc_change(), rtnl_qdisc_delete(), rtnl_rule_add(), and rtnl_rule_delete().

◆ nlmsg_set_proto()

void nlmsg_set_proto ( struct nl_msg *  msg,
int  protocol 
)

Definition at line 673 of file msg.c.

674{
675 msg->nm_protocol = protocol;
676}

◆ nlmsg_get_proto()

int nlmsg_get_proto ( struct nl_msg *  msg)

Definition at line 678 of file msg.c.

679{
680 return msg->nm_protocol;
681}

◆ nlmsg_get_max_size()

size_t nlmsg_get_max_size ( struct nl_msg *  msg)

Definition at line 683 of file msg.c.

684{
685 return msg->nm_size;
686}

◆ nlmsg_set_src()

void nlmsg_set_src ( struct nl_msg *  msg,
struct sockaddr_nl addr 
)

Definition at line 688 of file msg.c.

689{
690 memcpy(&msg->nm_src, addr, sizeof(*addr));
691}

◆ nlmsg_get_src()

struct sockaddr_nl * nlmsg_get_src ( struct nl_msg *  msg)

Definition at line 693 of file msg.c.

694{
695 return &msg->nm_src;
696}

◆ nlmsg_set_dst()

void nlmsg_set_dst ( struct nl_msg *  msg,
struct sockaddr_nl addr 
)

Definition at line 698 of file msg.c.

699{
700 memcpy(&msg->nm_dst, addr, sizeof(*addr));
701}

◆ nlmsg_get_dst()

struct sockaddr_nl * nlmsg_get_dst ( struct nl_msg *  msg)

Definition at line 703 of file msg.c.

704{
705 return &msg->nm_dst;
706}

◆ nlmsg_set_creds()

void nlmsg_set_creds ( struct nl_msg *  msg,
struct ucred *  creds 
)

Definition at line 708 of file msg.c.

709{
710 memcpy(&msg->nm_creds, creds, sizeof(*creds));
711 msg->nm_flags |= NL_MSG_CRED_PRESENT;
712}

◆ nlmsg_get_creds()

struct ucred * nlmsg_get_creds ( struct nl_msg *  msg)

Definition at line 714 of file msg.c.

715{
716 if (msg->nm_flags & NL_MSG_CRED_PRESENT)
717 return &msg->nm_creds;
718 return NULL;
719}

◆ nl_nlmsgtype2str()

char * nl_nlmsgtype2str ( int  type,
char *  buf,
size_t  size 
)

Definition at line 735 of file msg.c.

736{
737 return __type2str(type, buf, size, nl_msgtypes,
738 ARRAY_SIZE(nl_msgtypes));
739}

◆ nl_str2nlmsgtype()

int nl_str2nlmsgtype ( const char *  name)

Definition at line 741 of file msg.c.

742{
743 return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
744}

◆ nl_nlmsg_flags2str()

char * nl_nlmsg_flags2str ( int  flags,
char *  buf,
size_t  len 
)

Definition at line 753 of file msg.c.

754{
755 memset(buf, 0, len);
756
757#define PRINT_FLAG(f) \
758 if (flags & NLM_F_##f) { \
759 flags &= ~NLM_F_##f; \
760 strncat(buf, #f, len - strlen(buf) - 1); \
761 if (flags) \
762 strncat(buf, ",", len - strlen(buf) - 1); \
763 }
764
765 PRINT_FLAG(REQUEST);
766 PRINT_FLAG(MULTI);
767 PRINT_FLAG(ACK);
768 PRINT_FLAG(ECHO);
769 PRINT_FLAG(ROOT);
770 PRINT_FLAG(MATCH);
771 PRINT_FLAG(ATOMIC);
772 PRINT_FLAG(REPLACE);
773 PRINT_FLAG(EXCL);
774 PRINT_FLAG(CREATE);
775 PRINT_FLAG(APPEND);
776
777 if (flags) {
778 char s[32];
779 snprintf(s, sizeof(s), "0x%x", flags);
780 strncat(buf, s, len - strlen(buf) - 1);
781 }
782#undef PRINT_FLAG
783
784 return buf;
785}

◆ nl_msg_parse()

int nl_msg_parse ( struct nl_msg *  msg,
void(*)(struct nl_object *, void *)  cb,
void *  arg 
)

Definition at line 809 of file msg.c.

811{
812 struct nl_cache_ops *ops;
813 struct nl_parser_param p = {
814 .pp_cb = parse_cb
815 };
816 struct dp_xdata x = {
817 .cb = cb,
818 .arg = arg,
819 };
820
821 ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
822 nlmsg_hdr(msg)->nlmsg_type);
823 if (ops == NULL)
824 return nl_error(ENOENT, "Unknown message type %d",
825 nlmsg_hdr(msg)->nlmsg_type);
826 p.pp_arg = &x;
827
828 return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
829}
struct nl_cache_ops * nl_cache_ops_associate(int protocol, int msgtype)
Associate protocol and message type to cache operations.
Definition: cache_mngt.c:129
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition: msg.c:643
Cache Operations.
Definition: cache-api.h:164

◆ nl_msg_dump()

void nl_msg_dump ( struct nl_msg *  msg,
FILE *  ofd 
)
Parameters
msgMessage to print
ofdFile descriptor.

Definition at line 947 of file msg.c.

948{
949 struct nlmsghdr *hdr = nlmsg_hdr(msg);
950
951 fprintf(ofd,
952 "-------------------------- BEGIN NETLINK MESSAGE "
953 "---------------------------\n");
954
955 fprintf(ofd, " [HEADER] %Zu octets\n", sizeof(struct nlmsghdr));
956 print_hdr(ofd, msg);
957
958 if (hdr->nlmsg_type == NLMSG_ERROR &&
959 hdr->nlmsg_len >= nlmsg_msg_size(sizeof(struct nlmsgerr))) {
960 struct nl_msg *errmsg;
961 struct nlmsgerr *err = nlmsg_data(hdr);
962 char buf[256];
963
964 fprintf(ofd, " [ERRORMSG] %Zu octets\n", sizeof(*err));
965 fprintf(ofd, " .error = %d \"%s\"\n", err->error,
966 strerror_r(-err->error, buf, sizeof(buf)));
967 fprintf(ofd, " [ORIGINAL MESSAGE] %Zu octets\n", sizeof(*hdr));
968
969 errmsg = nlmsg_inherit(&err->msg);
970 print_hdr(ofd, errmsg);
971 nlmsg_free(errmsg);
972 } else if (nlmsg_len(hdr) > 0) {
973 struct nl_cache_ops *ops;
974 int payloadlen = nlmsg_len(hdr);
975 int attrlen = 0;
976
977 ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
978 hdr->nlmsg_type);
979 if (ops) {
980 attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
981 payloadlen -= attrlen;
982 }
983
984 fprintf(ofd, " [PAYLOAD] %d octets\n", payloadlen);
985 dump_hex(ofd, nlmsg_data(hdr), payloadlen, 0);
986
987 if (attrlen) {
988 struct nlattr *attrs;
989 int attrlen;
990
991 attrs = nlmsg_attrdata(hdr, ops->co_hdrsize);
992 attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
993 dump_attrs(ofd, attrs, attrlen, 0);
994 }
995 }
996
997 fprintf(ofd,
998 "--------------------------- END NETLINK MESSAGE "
999 "---------------------------\n");
1000}
Netlink error message.
struct nlmsghdr msg
Original netlink message causing the error.
int error
Error code (errno number)

References nlmsgerr::error, nlmsgerr::msg, nl_cache_ops_associate(), nlmsg_attrdata(), nlmsg_attrlen(), nlmsg_data(), NLMSG_ERROR, nlmsg_free(), nlmsg_hdr(), nlmsg_inherit(), nlmsghdr::nlmsg_len, nlmsg_len(), nlmsg_msg_size(), and nlmsghdr::nlmsg_type.