mnl_attr_get_len(3)

attr(3) Library Functions Manual attr(3)

NAME

attr - Netlink attribute helpers

SYNOPSIS

Functions


uint16_t mnl_attr_get_type (const struct nlattr *attr)
uint16_t mnl_attr_get_len (const struct nlattr *attr)
uint16_t mnl_attr_get_payload_len (const struct nlattr *attr)
void * mnl_attr_get_payload (const struct nlattr *attr)
bool mnl_attr_ok (const struct nlattr *attr, int len)
struct nlattr * mnl_attr_next (const struct nlattr *attr)
int mnl_attr_type_valid (const struct nlattr *attr, uint16_t max)
int mnl_attr_validate (const struct nlattr *attr, enum mnl_attr_data_type type)
int mnl_attr_validate2 (const struct nlattr *attr, enum mnl_attr_data_type type, size_t exp_len)
int mnl_attr_parse (const struct nlmsghdr *nlh, unsigned int offset, mnl_attr_cb_t cb, void *data)
int mnl_attr_parse_nested (const struct nlattr *nested, mnl_attr_cb_t cb, void *data)
int mnl_attr_parse_payload (const void *payload, size_t payload_len, mnl_attr_cb_t cb, void *data)
uint8_t mnl_attr_get_u8 (const struct nlattr *attr)
uint16_t mnl_attr_get_u16 (const struct nlattr *attr)
uint32_t mnl_attr_get_u32 (const struct nlattr *attr)
uint64_t mnl_attr_get_u64 (const struct nlattr *attr)
uint64_t mnl_attr_get_uint (const struct nlattr *attr)
const char * mnl_attr_get_str (const struct nlattr *attr)
void mnl_attr_put (struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data)
void mnl_attr_put_u8 (struct nlmsghdr *nlh, uint16_t type, uint8_t data)
void mnl_attr_put_u16 (struct nlmsghdr *nlh, uint16_t type, uint16_t data)
void mnl_attr_put_u32 (struct nlmsghdr *nlh, uint16_t type, uint32_t data)
void mnl_attr_put_u64 (struct nlmsghdr *nlh, uint16_t type, uint64_t data)
void mnl_attr_put_str (struct nlmsghdr *nlh, uint16_t type, const char *data)
void mnl_attr_put_strz (struct nlmsghdr *nlh, uint16_t type, const char *data)
struct nlattr * mnl_attr_nest_start (struct nlmsghdr *nlh, uint16_t type)
bool mnl_attr_put_check (struct nlmsghdr *nlh, size_t buflen, uint16_t type, size_t len, const void *data)
bool mnl_attr_put_u8_check (struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint8_t data)
bool mnl_attr_put_u16_check (struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint16_t data)
bool mnl_attr_put_u32_check (struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint32_t data)
bool mnl_attr_put_u64_check (struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint64_t data)
bool mnl_attr_put_str_check (struct nlmsghdr *nlh, size_t buflen, uint16_t type, const char *data)
bool mnl_attr_put_strz_check (struct nlmsghdr *nlh, size_t buflen, uint16_t type, const char *data)
struct nlattr * mnl_attr_nest_start_check (struct nlmsghdr *nlh, size_t buflen, uint16_t type)
void mnl_attr_nest_end (struct nlmsghdr *nlh, struct nlattr *start)
void mnl_attr_nest_cancel (struct nlmsghdr *nlh, struct nlattr *start)

Detailed Description

Netlink Type-Length-Value (TLV) attribute:


|<-- 2 bytes -->|<-- 2 bytes -->|<-- variable -->|
-------------------------------------------------
| length | type | value |
-------------------------------------------------
|<--------- header ------------>|<-- payload --->|


The payload of the Netlink message contains sequences of attributes that are expressed in TLV format.

Function Documentation

uint16_t mnl_attr_get_len (const struct nlattr * attr)

mnl_attr_get_len - get length of netlink attribute

Parameters

attr pointer to netlink attribute

Returns

the attribute length

The attribute length is the length of the attribute header plus the attribute payload.

Definition at line 53 of file attr.c.

void * mnl_attr_get_payload (const struct nlattr * attr)

mnl_attr_get_payload - get pointer to the attribute payload

Parameters

attr pointer to netlink attribute

Returns

pointer to the attribute payload

Definition at line 75 of file attr.c.

uint16_t mnl_attr_get_payload_len (const struct nlattr * attr)

mnl_attr_get_payload_len - get the attribute payload-value length

Parameters

attr pointer to netlink attribute

Returns

the attribute payload-value length

Definition at line 64 of file attr.c.

const char * mnl_attr_get_str (const struct nlattr * attr)

mnl_attr_get_str - get pointer to string attribute

Parameters

attr pointer to netlink attribute

Returns

string pointer of the attribute payload

Definition at line 437 of file attr.c.

uint16_t mnl_attr_get_type (const struct nlattr * attr)

mnl_attr_get_type - get type of netlink attribute

Parameters

attr pointer to netlink attribute

Returns

the attribute type

Definition at line 38 of file attr.c.

uint16_t mnl_attr_get_u16 (const struct nlattr * attr)

mnl_attr_get_u16 - get 16-bit unsigned integer attribute payload

Parameters

attr pointer to netlink attribute

Returns

16-bit value of the attribute payload

Definition at line 361 of file attr.c.

uint32_t mnl_attr_get_u32 (const struct nlattr * attr)

mnl_attr_get_u32 - get 32-bit unsigned integer attribute payload

Parameters

attr pointer to netlink attribute

Returns

32-bit value of the attribute payload

Definition at line 372 of file attr.c.

uint64_t mnl_attr_get_u64 (const struct nlattr * attr)

mnl_attr_get_u64 - get 64-bit unsigned integer attribute

Parameters

attr pointer to netlink attribute

This function reads the 64-bit nlattr payload in an alignment safe manner.

Returns

64-bit value of the attribute payload

Definition at line 385 of file attr.c.

uint8_t mnl_attr_get_u8 (const struct nlattr * attr)

mnl_attr_get_u8 - get 8-bit unsigned integer attribute payload

Parameters

attr pointer to netlink attribute

Returns

8-bit value of the attribute payload

Definition at line 350 of file attr.c.

uint64_t mnl_attr_get_uint (const struct nlattr * attr)

mnl_attr_get_uint - returns 64-bit unsigned integer attribute.

Parameters

attr pointer to netlink attribute

This helper function reads the variable-length netlink attribute NLA_UINT that provides a 32-bit or 64-bit integer payload. Its use is recommended only in these cases.

Recommended validation for NLA_UINT is:


if (!mnl_attr_validate(attr, NLA_U32) &&
!mnl_attr_validate(attr, NLA_U64)) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}

Returns

the 64-bit value of the attribute payload. On error, it returns UINT64_MAX if the length of the netlink attribute is not an 8-bit, 16-bit, 32-bit and 64-bit integer. Therefore, there is no way to distinguish between UINT64_MAX and an error. Also, errno is never set.

Definition at line 415 of file attr.c.

void mnl_attr_nest_cancel (struct nlmsghdr * nlh, struct nlattr * start)

mnl_attr_nest_cancel - cancel an attribute nest

Parameters

nlh pointer to the netlink message
start pointer to the attribute nest returned by mnl_attr_nest_start()

This function updates the attribute header that identifies the nest.

Definition at line 773 of file attr.c.

void mnl_attr_nest_end (struct nlmsghdr * nlh, struct nlattr * start)

mnl_attr_nest_end - end an attribute nest

Parameters

nlh pointer to the netlink message
start pointer to the attribute nest returned by mnl_attr_nest_start()

This function updates the attribute header that identifies the nest.

Definition at line 760 of file attr.c.

struct nlattr * mnl_attr_nest_start (struct nlmsghdr * nlh, uint16_t type)

mnl_attr_nest_start - start an attribute nest

Parameters

nlh pointer to the netlink message
type netlink attribute type

This function adds the attribute header that identifies the beginning of an attribute nest.

Returns

valid pointer to the beginning of the nest

Definition at line 572 of file attr.c.

struct nlattr * mnl_attr_nest_start_check (struct nlmsghdr * nlh, size_t buflen, uint16_t type)

mnl_attr_nest_start_check - start an attribute nest

Parameters

buflen size of buffer which stores the message
nlh pointer to the netlink message
type netlink attribute type

This function adds the attribute header that identifies the beginning of an attribute nest.

Returns

NULL if the attribute cannot be added, otherwise a pointer to the beginning of the nest

Definition at line 744 of file attr.c.

struct nlattr * mnl_attr_next (const struct nlattr * attr)

mnl_attr_next - get the next attribute in the payload of a netlink message

Parameters

attr pointer to the current attribute

Returns

a pointer to the next attribute after the one passed in

You have to use mnl_attr_ok() on the returned attribute to ensure that the next attribute is valid.

Definition at line 115 of file attr.c.

bool mnl_attr_ok (const struct nlattr * attr, int len)

mnl_attr_ok - check if there is room for an attribute in a buffer

Parameters

attr attribute that we want to check if there is room for
len remaining bytes in a buffer that contains the attribute

This function is used to check that a buffer, which is supposed to contain an attribute, has enough room for the attribute that it stores, i.e. this function can be used to verify that an attribute is neither malformed nor truncated.

This function does not set errno in case of error since it is intended for iterations.

The len parameter may be negative in the case of malformed messages during attribute iteration, that is why we use a signed integer.

Returns

true if there is room for the attribute, false otherwise

Definition at line 98 of file attr.c.

int mnl_attr_parse (const struct nlmsghdr * nlh, unsigned int offset, mnl_attr_cb_t cb, void * data)

mnl_attr_parse - parse attributes

Parameters

nlh pointer to netlink message
offset offset to start parsing from (if payload is after any header)
cb callback function that is called for each attribute
data pointer to data that is passed to the callback function

This function allows you to iterate over the sequence of attributes that compose the Netlink message. You can then put the attribute in an array as it usually happens at this stage or you can use any other data structure (such as lists or trees).

Returns

propagated value from callback, one of MNL_CB_ERROR, MNL_CB_STOP or MNL_CB_OK

Definition at line 273 of file attr.c.

int mnl_attr_parse_nested (const struct nlattr * nested, mnl_attr_cb_t cb, void * data)

mnl_attr_parse_nested - parse attributes inside a nest

Parameters

nested pointer to netlink attribute that contains a nest
cb callback function that is called for each attribute in the nest
data pointer to data passed to the callback function

This function allows you to iterate over the sequence of attributes that compose the Netlink message. You can then put the attribute in an array as it usually happens at this stage or you can use any other data structure (such as lists or trees).

Returns

propagated value from callback, one of MNL_CB_ERROR, MNL_CB_STOP or MNL_CB_OK

Definition at line 300 of file attr.c.

int mnl_attr_parse_payload (const void * payload, size_t payload_len, mnl_attr_cb_t cb, void * data)

mnl_attr_parse_payload - parse attributes in payload of Netlink message

Parameters

payload pointer to payload of the Netlink message
payload_len payload length that contains the attributes
cb callback function that is called for each attribute
data pointer to data that is passed to the callback function

This function takes a pointer to the area that contains the attributes, commonly known as the payload of the Netlink message. Thus, you have to pass a pointer to the Netlink message payload, instead of the entire message.

This function allows you to iterate over the sequence of attributes that are located at some payload offset. You can then put the attributes in one array as usual, or you can use any other data structure (such as lists or trees).

Returns

propagated value from callback, one of MNL_CB_ERROR, MNL_CB_STOP or MNL_CB_OK

Definition at line 331 of file attr.c.

void mnl_attr_put (struct nlmsghdr * nlh, uint16_t type, size_t len, const void * data)

mnl_attr_put - add an attribute to netlink message

Parameters

nlh pointer to the netlink message
type netlink attribute type that you want to add
len netlink attribute payload length
data pointer to the data that will be stored by the new attribute

This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Definition at line 452 of file attr.c.

bool mnl_attr_put_check (struct nlmsghdr * nlh, size_t buflen, uint16_t type, size_t len, const void * data)

mnl_attr_put_check - add an attribute to netlink message

Parameters

nlh pointer to the netlink message
buflen size of buffer which stores the message
type netlink attribute type that you want to add
len netlink attribute payload length
data pointer to the data that will be stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Returns

true if the attribute could be added, false otherwise

Definition at line 599 of file attr.c.

void mnl_attr_put_str (struct nlmsghdr * nlh, uint16_t type, const char * data)

mnl_attr_put_str - add string attribute to netlink message

Parameters

nlh pointer to the netlink message
type netlink attribute type
data pointer to string data that is stored by the new attribute

This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Definition at line 538 of file attr.c.

bool mnl_attr_put_str_check (struct nlmsghdr * nlh, size_t buflen, uint16_t type, const char * data)

mnl_attr_put_str_check - add string attribute to netlink message

Parameters

nlh pointer to the netlink message
buflen size of buffer which stores the message
type netlink attribute type
data pointer to string data that is stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Returns

true if the attribute could be added, false otherwise

Definition at line 703 of file attr.c.

void mnl_attr_put_strz (struct nlmsghdr * nlh, uint16_t type, const char * data)

mnl_attr_put_strz - add string attribute to netlink message

Parameters

nlh pointer to the netlink message
type netlink attribute type
data pointer to string data that is stored by the new attribute

This function is similar to mnl_attr_put_str, but it includes the NUL/zero ('\0') terminator at the end of the string.

This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Definition at line 556 of file attr.c.

bool mnl_attr_put_strz_check (struct nlmsghdr * nlh, size_t buflen, uint16_t type, const char * data)

mnl_attr_put_strz_check - add string attribute to netlink message

Parameters

nlh pointer to the netlink message
buflen size of buffer which stores the message
type netlink attribute type
data pointer to string data that is stored by the new attribute

This function is similar to mnl_attr_put_str, but it includes the NUL/zero ('\0') terminator at the end of the string.

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Returns

true if the attribute could be added, false otherwise

Definition at line 726 of file attr.c.

void mnl_attr_put_u16 (struct nlmsghdr * nlh, uint16_t type, uint16_t data)

mnl_attr_put_u16 - add 16-bit unsigned integer attribute to netlink message

Parameters

nlh pointer to the netlink message
type netlink attribute type
data 16-bit unsigned integer data that is stored by the new attribute

This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Definition at line 493 of file attr.c.

bool mnl_attr_put_u16_check (struct nlmsghdr * nlh, size_t buflen, uint16_t type, uint16_t data)

mnl_attr_put_u16_check - add 16-bit unsigned int attribute to netlink message

Parameters

nlh pointer to the netlink message
buflen size of buffer which stores the message
type netlink attribute type
data 16-bit unsigned integer data that is stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Returns

true if the attribute could be added, false otherwise

Definition at line 643 of file attr.c.

void mnl_attr_put_u32 (struct nlmsghdr * nlh, uint16_t type, uint32_t data)

mnl_attr_put_u32 - add 32-bit unsigned integer attribute to netlink message

Parameters

nlh pointer to the netlink message
type netlink attribute type
data 32-bit unsigned integer data that is stored by the new attribute

This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Definition at line 508 of file attr.c.

bool mnl_attr_put_u32_check (struct nlmsghdr * nlh, size_t buflen, uint16_t type, uint32_t data)

mnl_attr_put_u32_check - add 32-bit unsigned int attribute to netlink message

Parameters

nlh pointer to the netlink message
buflen size of buffer which stores the message
type netlink attribute type
data 32-bit unsigned integer data that is stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Returns

true if the attribute could be added, false otherwise

Definition at line 663 of file attr.c.

void mnl_attr_put_u64 (struct nlmsghdr * nlh, uint16_t type, uint64_t data)

mnl_attr_put_u64 - add 64-bit unsigned integer attribute to netlink message

Parameters

nlh pointer to the netlink message
type netlink attribute type
data 64-bit unsigned integer data that is stored by the new attribute

This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Definition at line 523 of file attr.c.

bool mnl_attr_put_u64_check (struct nlmsghdr * nlh, size_t buflen, uint16_t type, uint64_t data)

mnl_attr_put_u64_check - add 64-bit unsigned int attribute to netlink message

Parameters

nlh pointer to the netlink message
buflen size of buffer which stores the message
type netlink attribute type
data 64-bit unsigned integer data that is stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Returns

true if the attribute could be added, false otherwise

Definition at line 683 of file attr.c.

void mnl_attr_put_u8 (struct nlmsghdr * nlh, uint16_t type, uint8_t data)

mnl_attr_put_u8 - add 8-bit unsigned integer attribute to netlink message

Parameters

nlh pointer to the netlink message
type netlink attribute type
data 8-bit unsigned integer data that is stored by the new attribute

This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Definition at line 478 of file attr.c.

bool mnl_attr_put_u8_check (struct nlmsghdr * nlh, size_t buflen, uint16_t type, uint8_t data)

mnl_attr_put_u8_check - add 8-bit unsigned int attribute to netlink message

Parameters

nlh pointer to the netlink message
buflen size of buffer which stores the message
type netlink attribute type
data 8-bit unsigned integer data that is stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

Returns

true if the attribute could be added, false otherwise

Definition at line 623 of file attr.c.

int mnl_attr_type_valid (const struct nlattr * attr, uint16_t max)

mnl_attr_type_valid - check if the attribute type is valid

Parameters

attr pointer to attribute to be checked
max maximum attribute type

This function allows one to check if the attribute type is higher than the maximum supported type.

Strict attribute checking in user-space is not a good idea since you may run an old application with a newer kernel that supports new attributes. This leads to backward compatibility breakages in user-space. Better check if you support an attribute, if not, skip it.

On an error, errno is explicitly set.

Returns

1 if the attribute is valid, -1 otherwise

Definition at line 138 of file attr.c.

int mnl_attr_validate (const struct nlattr * attr, enum mnl_attr_data_type type)

mnl_attr_validate - validate netlink attribute (simplified version)

Parameters

attr pointer to netlink attribute that we want to validate
type data type (see enum mnl_attr_data_type)

The validation is based on the data type. Specifically, it checks that integers (u8, u16, u32 and u64) have enough room for them.

On an error, errno is explicitly set.

Returns

0 on success, -1 on error

Definition at line 221 of file attr.c.

int mnl_attr_validate2 (const struct nlattr * attr, enum mnl_attr_data_type type, size_t exp_len)

mnl_attr_validate2 - validate netlink attribute (extended version)

Parameters

attr pointer to netlink attribute that we want to validate
type attribute type (see enum mnl_attr_data_type)
exp_len expected attribute data size

This function allows one to perform a more accurate validation for attributes whose size is variable.

On an error, errno is explicitly set.

Returns

0 if the attribute is valid and fits within the expected length, -1 otherwise

Definition at line 247 of file attr.c.

Author

Generated automatically by Doxygen for libmnl from the source code.

Version 1.0.5 libmnl