Netlink Attributes Construction/Parsing Interface. More...
Data Structures | |
struct | nla_policy |
Attribute validation policy. More... | |
Basic Attribute Data Types | |
| |
enum | { NLA_UNSPEC, NLA_U8, NLA_U16, NLA_U32, NLA_U64, NLA_STRING, NLA_FLAG, NLA_MSECS, NLA_NESTED, __NLA_TYPE_MAX } |
Basic attribute data types. More... | |
Parsing Attributes | |
| |
int | nla_type (const struct nlattr *nla) |
Return type of the attribute. | |
void * | nla_data (const struct nlattr *nla) |
Return pointer to the payload section. | |
int | nla_len (const struct nlattr *nla) |
Return length of the payload . | |
int | nla_ok (const struct nlattr *nla, int remaining) |
Check if the attribute header and payload can be accessed safely. | |
struct nlattr * | nla_next (const struct nlattr *nla, int *remaining) |
Return next attribute in a stream of attributes. | |
int | nla_parse (struct nlattr *tb[], int maxtype, struct nlattr *head, int len, struct nla_policy *policy) |
Create attribute index based on a stream of attributes. | |
int | nla_validate (struct nlattr *head, int len, int maxtype, struct nla_policy *policy) |
Validate a stream of attributes. | |
struct nlattr * | nla_find (struct nlattr *head, int len, int attrtype) |
Find a single attribute in a stream of attributes. | |
Attribute Size Calculation | |
| |
int | nla_attr_size (int payload) |
Return size of attribute whithout padding. | |
int | nla_total_size (int payload) |
Return size of attribute including padding. | |
int | nla_padlen (int payload) |
Return length of padding at the tail of the attribute. | |
Helper Functions | |
| |
int | nla_memcpy (void *dest, struct nlattr *src, int count) |
Copy attribute payload to another memory area. | |
size_t | nla_strlcpy (char *dst, const struct nlattr *nla, size_t dstsize) |
Copy string attribute payload to a buffer. | |
int | nla_memcmp (const struct nlattr *nla, const void *data, size_t size) |
Compare attribute payload with memory area. | |
int | nla_strcmp (const struct nlattr *nla, const char *str) |
Compare string attribute payload with string. | |
Unspecific Attribute | |
| |
struct nlattr * | nla_reserve (struct nl_msg *msg, int attrtype, int attrlen) |
Reserve space for a attribute. | |
int | nla_put (struct nl_msg *msg, int attrtype, int datalen, const void *data) |
Add a unspecific attribute to netlink message. | |
int | nla_put_data (struct nl_msg *msg, int attrtype, struct nl_data *data) |
Add abstract data as unspecific attribute to netlink message. | |
int | nla_put_addr (struct nl_msg *msg, int attrtype, struct nl_addr *addr) |
Add abstract address as unspecific attribute to netlink message. | |
Integer Attributes | |
| |
int | nla_put_u8 (struct nl_msg *msg, int attrtype, uint8_t value) |
Add 8 bit integer attribute to netlink message. | |
uint8_t | nla_get_u8 (struct nlattr *nla) |
Return value of 8 bit integer attribute. | |
int | nla_put_u16 (struct nl_msg *msg, int attrtype, uint16_t value) |
Add 16 bit integer attribute to netlink message. | |
uint16_t | nla_get_u16 (struct nlattr *nla) |
Return payload of 16 bit integer attribute. | |
int | nla_put_u32 (struct nl_msg *msg, int attrtype, uint32_t value) |
Add 32 bit integer attribute to netlink message. | |
uint32_t | nla_get_u32 (struct nlattr *nla) |
Return payload of 32 bit integer attribute. | |
int | nla_put_u64 (struct nl_msg *msg, int attrtype, uint64_t value) |
Add 64 bit integer attribute to netlink message. | |
uint64_t | nla_get_u64 (struct nlattr *nla) |
Return payload of u64 attribute. | |
String Attribute | |
| |
int | nla_put_string (struct nl_msg *msg, int attrtype, const char *str) |
Add string attribute to netlink message. | |
char * | nla_get_string (struct nlattr *nla) |
Return payload of string attribute. | |
char * | nla_strdup (struct nlattr *nla) |
Flag Attribute | |
| |
int | nla_put_flag (struct nl_msg *msg, int attrtype) |
Add flag netlink attribute to netlink message. | |
int | nla_get_flag (struct nlattr *nla) |
Return true if flag attribute is set. | |
Microseconds Attribute | |
| |
int | nla_put_msecs (struct nl_msg *n, int attrtype, unsigned long msecs) |
Add a msecs netlink attribute to a netlink message. | |
unsigned long | nla_get_msecs (struct nlattr *nla) |
Return payload of msecs attribute. | |
Nested Attribute | |
| |
int | nla_put_nested (struct nl_msg *msg, int attrtype, struct nl_msg *nested) |
Add nested attributes to netlink message. | |
struct nlattr * | nla_nest_start (struct nl_msg *msg, int attrtype) |
Start a new level of nested attributes. | |
int | nla_nest_end (struct nl_msg *msg, struct nlattr *start) |
Finalize nesting of attributes. | |
int | nla_parse_nested (struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy) |
Create attribute index based on nested attribute. | |
Attribute Construction (Exception Based) | |
| |
#define | NLA_PUT(msg, attrtype, attrlen, data) |
Add unspecific attribute to netlink message. | |
#define | NLA_PUT_TYPE(msg, type, attrtype, value) |
Add atomic type attribute to netlink message. | |
#define | NLA_PUT_U8(msg, attrtype, value) NLA_PUT_TYPE(msg, uint8_t, attrtype, value) |
Add 8 bit integer attribute to netlink message. | |
#define | NLA_PUT_U16(msg, attrtype, value) NLA_PUT_TYPE(msg, uint16_t, attrtype, value) |
Add 16 bit integer attribute to netlink message. | |
#define | NLA_PUT_U32(msg, attrtype, value) NLA_PUT_TYPE(msg, uint32_t, attrtype, value) |
Add 32 bit integer attribute to netlink message. | |
#define | NLA_PUT_U64(msg, attrtype, value) NLA_PUT_TYPE(msg, uint64_t, attrtype, value) |
Add 64 bit integer attribute to netlink message. | |
#define | NLA_PUT_STRING(msg, attrtype, value) NLA_PUT(msg, attrtype, strlen(value) + 1, value) |
Add string attribute to netlink message. | |
#define | NLA_PUT_FLAG(msg, attrtype) NLA_PUT(msg, attrtype, 0, NULL) |
Add flag attribute to netlink message. | |
#define | NLA_PUT_MSECS(msg, attrtype, msecs) NLA_PUT_U64(msg, attrtype, msecs) |
Add msecs attribute to netlink message. | |
#define | NLA_PUT_ADDR(msg, attrtype, addr) |
Add address attribute to netlink message. | |
#define | NLA_PUT_DATA(msg, attrtype, data) |
Add abstract data attribute to netlink message. | |
Iterators | |
| |
#define | nla_for_each_attr(pos, head, len, rem) |
Iterate over a stream of attributes. | |
#define | nla_for_each_nested(pos, nla, rem) |
Iterate over a stream of nested attributes. |
Netlink Attributes Construction/Parsing Interface.
Netlink attributes allow for data chunks of arbitary length to be attached to a netlink message. Each attribute is encoded with a type and length field, both 16 bits, stored in the attribute header preceding the attribute data. The main advantage of using attributes over packing everything into the family header is that the interface stays extendable as new attributes can supersede old attributes while remaining backwards compatible. Also attributes can be defined optional thus avoiding the transmission of unnecessary empty data blocks. Special nested attributes allow for more complex data structures to be transmitted, e.g. trees, lists, etc.
While not required, netlink attributes typically follow the family header of a netlink message and must be properly aligned to NLA_ALIGNTO:
+----------------+- - -+---------------+- - -+------------+- - -+ | Netlink Header | Pad | Family Header | Pad | Attributes | Pad | +----------------+- - -+---------------+- - -+------------+- - -+
The actual attributes are chained together each separately aligned to NLA_ALIGNTO. The position of an attribute is defined based on the length field of the preceding attributes:
+-------------+- - -+-------------+- - -+------ | Attribute 1 | Pad | Attribute 2 | Pad | ... +-------------+- - -+-------------+- - -+------ nla_next(attr1)------^
The attribute itself consists of the attribute header followed by the actual payload also aligned to NLA_ALIGNTO. The function nla_data() returns a pointer to the start of the payload while nla_len() returns the length of the payload in bytes.
Note: Be aware, NLA_ALIGNTO equals to 4 bytes, therefore it is not safe to dereference any 64 bit data types directly.
<----------- nla_total_size(payload) -----------> <-------- nla_attr_size(payload) ---------> +------------------+- - -+- - - - - - - - - +- - -+ | Attribute Header | Pad | Payload | Pad | +------------------+- - -+- - - - - - - - - +- - -+ nla_data(nla)-------------^ <- nla_len(nla) ->
A number of basic data types are supported to simplify access and validation of netlink attributes. This data type information is not encoded in the attribute, both the kernel and userspace part are required to share this information on their own.
One of the major advantages of these basic types is the automatic validation of each attribute based on an attribute policy. The validation covers most of the checks required to safely use attributes and thus keeps the individual sanity check to a minimum.
Never access attribute payload without ensuring basic validation first, attributes may:
Policies are defined as array of the struct nla_policy. The array is indexed with the attribute type, therefore the array must be sized accordingly.
static struct nla_policy my_policy[ATTR_MAX+1] = { [ATTR_FOO] = { .type = ..., .minlen = ..., .maxlen = ... }, }; err = nla_validate(attrs, attrlen, ATTR_MAX, &my_policy);
Some basic validations are performed on every attribute, regardless of type.
// In this example, we will assume a binary structure requires to // be transmitted. The definition of the structure will typically // go into a header file available to both the kernel and userspace // side. // // Note: Be careful when putting 64 bit data types into a structure. // The attribute payload is only aligned to 4 bytes, dereferencing // the member may fail. struct my_struct { int a; int b; }; // The validation function will not enforce an exact length match to // allow structures to grow as required. Note: While it is allowed // to add members to the end of the structure, changing the order or // inserting members in the middle of the structure will break your // binary interface. static struct nla_policy my_policy[ATTR_MAX+1] = { [ATTR_MY_STRICT] = { .type = NLA_UNSPEC, .minlen = sizeof(struct my_struct) }, // The binary structure is appened to the message using nla_put() struct my_struct foo = { .a = 1, .b = 2 }; nla_put(msg, ATTR_MY_STRUCT, sizeof(foo), &foo); // On the receiving side, a pointer to the structure pointing inside // the message payload is returned by nla_get(). if (attrs[ATTR_MY_STRUCT]) struct my_struct *foo = nla_get(attrs[ATTR_MY_STRUCT]);
// Even though possible, it does not make sense to specify .minlen or // .maxlen for integer types. The data types implies the corresponding // minimal payload length. static struct nla_policy my_policy[ATTR_MAX+1] = { [ATTR_FOO] = { .type = NLA_U32 }, // Numeric values can be appended directly using the respective // nla_put_uxxx() function nla_put_u32(msg, ATTR_FOO, 123); // Same for the receiving side. if (attrs[ATTR_FOO]) uint32_t foo = nla_get_u32(attrs[ATTR_FOO]);
// Enforce a NUL terminated character string of at most 4 characters // including the NUL termination. static struct nla_policy my_policy[ATTR_MAX+1] = { [ATTR_BAR] = { .type = NLA_STRING, maxlen = 4 }, // nla_put_string() creates a string attribute of the necessary length // and appends it to the message including the NUL termination. nla_put_string(msg, ATTR_BAR, "some text"); // It is safe to use the returned character string directly if the // attribute has been validated as the validation enforces the proper // termination of the string. if (attrs[ATTR_BAR]) char *text = nla_get_string(attrs[ATTR_BAR]);
// This attribute type is special as .minlen and .maxlen have no effect. static struct nla_policy my_policy[ATTR_MAX+1] = { [ATTR_FLAG] = { .type = NLA_FLAG }, // nla_put_flag() appends a zero sized attribute to the message. nla_put_flag(msg, ATTR_FLAG); // There is no need for a receival function, the presence is the value. if (attrs[ATTR_FLAG]) // flag is present
// The minimal length policy may be used to enforce the presence of at // least one attribute. static struct nla_policy my_policy[ATTR_MAX+1] = { [ATTR_OPTS] = { .type = NLA_NESTED, minlen = NLA_HDRLEN }, // Nested attributes are constructed by enclosing the attributes // to be nested with calls to nla_nest_start() respetively nla_nest_end(). struct nlattr *opts = nla_nest_start(msg, ATTR_OPTS); nla_put_u32(msg, ATTR_FOO, 123); nla_put_string(msg, ATTR_BAR, "some text"); nla_nest_end(msg, opts); // Various methods exist to parse nested attributes, the easiest being // nla_parse_nested() which also allows validation in the same step. if (attrs[ATTR_OPTS]) { struct nlattr *nested[ATTR_MAX+1]; nla_parse_nested(nested, ATTR_MAX, attrs[ATTR_OPTS], &policy); if (nested[ATTR_FOO]) uint32_t foo = nla_get_u32(nested[ATTR_FOO]); }
Often a large number of attributes are added to a message in a single function. In order to simplify error handling, a second set of construction functions exist which jump to a error label when they fail instead of returning an error code. This second set consists of macros which are named after their error code based counterpart except that the name is written all uppercase.
All of the macros jump to the target nla_put_failure
if they fail.
void my_func(struct nl_msg *msg) { NLA_PUT_U32(msg, ATTR_FOO, 10); NLA_PUT_STRING(msg, ATTR_BAR, "bar"); return 0; nla_put_failure: return -NLE_NOMEM; }
struct nl_msg *build_msg(int ifindex, struct nl_addr *lladdr, int mtu) { struct nl_msg *msg; struct nlattr *info, *vlan; struct ifinfomsg ifi = { .ifi_family = AF_INET, .ifi_index = ifindex, }; // Allocate a new netlink message, type=RTM_SETLINK, flags=NLM_F_ECHO if (!(msg = nlmsg_alloc_simple(RTM_SETLINK, NLM_F_ECHO))) return NULL; // Append the family specific header (struct ifinfomsg) if (nlmsg_append(msg, &ifi, sizeof(ifi), NLMSG_ALIGNTO) < 0) goto nla_put_failure // Append a 32 bit integer attribute to carry the MTU NLA_PUT_U32(msg, IFLA_MTU, mtu); // Append a unspecific attribute to carry the link layer address NLA_PUT_ADDR(msg, IFLA_ADDRESS, lladdr); // Append a container for nested attributes to carry link information if (!(info = nla_nest_start(msg, IFLA_LINKINFO))) goto nla_put_failure; // Put a string attribute into the container NLA_PUT_STRING(msg, IFLA_INFO_KIND, "vlan"); // Append another container inside the open container to carry // vlan specific attributes if (!(vlan = nla_nest_start(msg, IFLA_INFO_DATA))) goto nla_put_failure; // add vlan specific info attributes here... // Finish nesting the vlan attributes and close the second container. nla_nest_end(msg, vlan); // Finish nesting the link info attribute and close the first container. nla_nest_end(msg, info); return msg; // If any of the construction macros fails, we end up here. nla_put_failure: nlmsg_free(msg); return NULL; }
int parse_message(struct nl_msg *msg) { // The policy defines two attributes: a 32 bit integer and a container // for nested attributes. struct nla_policy attr_policy[ATTR_MAX+1] = { [ATTR_FOO] = { .type = NLA_U32 }, [ATTR_BAR] = { .type = NLA_NESTED }, }; struct nlattr *attrs[ATTR_MAX+1]; int err; // The nlmsg_parse() function will make sure that the message contains // enough payload to hold the header (struct my_hdr), validates any // attributes attached to the messages and stores a pointer to each // attribute in the attrs[] array accessable by attribute type. if ((err = nlmsg_parse(nlmsg_hdr(msg), sizeof(struct my_hdr), attrs, ATTR_MAX, attr_policy)) < 0) goto errout; if (attrs[ATTR_FOO]) { // It is safe to directly access the attribute payload without // any further checks since nlmsg_parse() enforced the policy. uint32_t foo = nla_get_u32(attrs[ATTR_FOO]); } if (attrs[ATTR_BAR]) { struct nlattr *nested[NESTED_MAX+1]; // Attributes nested in a container can be parsed the same way // as top level attributes. if ((err = nla_parse_nested(nested, NESTED_MAX, attrs[ATTR_BAR], nested_policy)) < 0) goto errout; // Process nested attributes here. } err = 0; errout: return err; }
#define NLA_PUT | ( | msg, | ||
attrtype, | ||||
attrlen, | ||||
data | ||||
) |
do { \ if (nla_put(msg, attrtype, attrlen, data) < 0) \ goto nla_put_failure; \ } while(0)
Add unspecific attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
attrlen | Length of attribute payload. | |
data | Head of attribute payload. |
#define NLA_PUT_TYPE | ( | msg, | ||
type, | ||||
attrtype, | ||||
value | ||||
) |
#define nla_for_each_attr | ( | pos, | ||
head, | ||||
len, | ||||
rem | ||||
) |
Iterate over a stream of attributes.
pos | loop counter, set to current attribute | |
head | head of attribute stream | |
len | length of attribute stream | |
rem | initialized to len, holds bytes currently remaining in stream |
Definition at line 260 of file attr.h.
Referenced by nla_find(), nla_parse(), and nla_validate().
#define nla_for_each_nested | ( | pos, | ||
nla, | ||||
rem | ||||
) |
Iterate over a stream of nested attributes.
pos | loop counter, set to current attribute | |
nla | attribute containing the nested attributes | |
rem | initialized to len, holds bytes currently remaining in stream |
Definition at line 272 of file attr.h.
Referenced by rtnl_ematch_parse_attr().
#define NLA_PUT_U8 | ( | msg, | ||
attrtype, | ||||
value | ||||
) | NLA_PUT_TYPE(msg, uint8_t, attrtype, value) |
Add 8 bit integer attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
value | Numeric value. |
Definition at line 169 of file attr.h.
Referenced by rtnl_link_build_change_request().
#define NLA_PUT_U16 | ( | msg, | ||
attrtype, | ||||
value | ||||
) | NLA_PUT_TYPE(msg, uint16_t, attrtype, value) |
#define NLA_PUT_U32 | ( | msg, | ||
attrtype, | ||||
value | ||||
) | NLA_PUT_TYPE(msg, uint32_t, attrtype, value) |
Add 32 bit integer attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
value | Numeric value. |
Definition at line 187 of file attr.h.
Referenced by rtnl_link_build_change_request(), and rtnl_neightbl_build_change_request().
#define NLA_PUT_U64 | ( | msg, | ||
attrtype, | ||||
value | ||||
) | NLA_PUT_TYPE(msg, uint64_t, attrtype, value) |
Add 64 bit integer attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
value | Numeric value. |
Definition at line 196 of file attr.h.
Referenced by rtnl_neightbl_build_change_request().
#define NLA_PUT_STRING | ( | msg, | ||
attrtype, | ||||
value | ||||
) | NLA_PUT(msg, attrtype, strlen(value) + 1, value) |
Add string attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
value | NUL terminated character string. |
Definition at line 205 of file attr.h.
Referenced by rtnl_link_build_change_request(), and rtnl_neightbl_build_change_request().
#define NLA_PUT_FLAG | ( | msg, | ||
attrtype | ||||
) | NLA_PUT(msg, attrtype, 0, NULL) |
#define NLA_PUT_MSECS | ( | msg, | ||
attrtype, | ||||
msecs | ||||
) | NLA_PUT_U64(msg, attrtype, msecs) |
#define NLA_PUT_ADDR | ( | msg, | ||
attrtype, | ||||
addr | ||||
) |
NLA_PUT(msg, attrtype, nl_addr_get_len(addr), \ nl_addr_get_binary_addr(addr))
Add address attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
addr | Abstract address object. |
Definition at line 231 of file attr.h.
Referenced by rtnl_link_build_change_request().
#define NLA_PUT_DATA | ( | msg, | ||
attrtype, | ||||
data | ||||
) |
NLA_PUT(msg, attrtype, nl_data_get_size(data), \ nl_data_get(data))
Add abstract data attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
data | Abstract data object. |
anonymous enum |
Basic attribute data types.
See Attribute Data Types for more details.
int nla_attr_size | ( | int | payload | ) |
Return size of attribute whithout padding.
payload | Payload length of attribute. |
<-------- nla_attr_size(payload) ---------> +------------------+- - -+- - - - - - - - - +- - -+ | Attribute Header | Pad | Payload | Pad | +------------------+- - -+- - - - - - - - - +- - -+
Definition at line 395 of file attr.c.
Referenced by nla_padlen(), nla_reserve(), and nla_total_size().
int nla_total_size | ( | int | payload | ) |
Return size of attribute including padding.
payload | Payload length of attribute. |
<----------- nla_total_size(payload) -----------> +------------------+- - -+- - - - - - - - - +- - -+ | Attribute Header | Pad | Payload | Pad | +------------------+- - -+- - - - - - - - - +- - -+
Definition at line 413 of file attr.c.
References nla_attr_size().
Referenced by nla_padlen(), nla_reserve(), and rtnl_ematch_parse_attr().
int nla_padlen | ( | int | payload | ) |
Return length of padding at the tail of the attribute.
payload | Payload length of attribute. |
+------------------+- - -+- - - - - - - - - +- - -+ | Attribute Header | Pad | Payload | Pad | +------------------+- - -+- - - - - - - - - +- - -+ <--->
Definition at line 431 of file attr.c.
References nla_attr_size(), and nla_total_size().
Referenced by nla_reserve().
int nla_type | ( | const struct nlattr * | nla | ) |
Return type of the attribute.
nla | Attribute. |
Definition at line 449 of file attr.c.
Referenced by nla_find(), and nla_parse().
void* nla_data | ( | const struct nlattr * | nla | ) |
Return pointer to the payload section.
nla | Attribute. |
Definition at line 460 of file attr.c.
Referenced by nl_addr_alloc_attr(), nl_data_alloc_attr(), nla_get_string(), nla_get_u16(), nla_get_u32(), nla_get_u8(), nla_memcmp(), nla_memcpy(), nla_parse_nested(), nla_put(), nla_strcmp(), nla_strlcpy(), and rtnl_ematch_parse_attr().
int nla_len | ( | const struct nlattr * | nla | ) |
Return length of the payload .
nla | Attribute |
Definition at line 471 of file attr.c.
Referenced by nl_addr_alloc_attr(), nl_data_alloc_attr(), nla_memcmp(), nla_memcpy(), nla_parse_nested(), nla_strcmp(), nla_strlcpy(), and rtnl_ematch_parse_attr().
int nla_ok | ( | const struct nlattr * | nla, | |
int | remaining | |||
) |
Check if the attribute header and payload can be accessed safely.
nla | Attribute of any kind. | |
remaining | Number of bytes remaining in attribute stream. |
Verifies that the header and payload do not exceed the number of bytes left in the attribute stream. This function must be called before access the attribute header or payload when iterating over the attribute stream using nla_next().
struct nlattr* nla_next | ( | const struct nlattr * | nla, | |
int * | remaining | |||
) | [read] |
Return next attribute in a stream of attributes.
nla | Attribute of any kind. | |
remaining | Variable to count remaining bytes in stream. |
Calculates the offset to the next attribute based on the attribute given. The attribute provided is assumed to be accessible, the caller is responsible to use nla_ok() beforehand. The offset (length of specified attribute including padding) is then subtracted from the remaining bytes variable and a pointer to the next attribute is returned.
nla_next() can be called as long as remainig is >0.
int nla_parse | ( | struct nlattr * | tb[], | |
int | maxtype, | |||
struct nlattr * | head, | |||
int | len, | |||
struct nla_policy * | policy | |||
) |
Create attribute index based on a stream of attributes.
tb | Index array to be filled (maxtype+1 elements). | |
maxtype | Maximum attribute type expected and accepted. | |
head | Head of attribute stream. | |
len | Length of attribute stream. | |
policy | Attribute validation policy. |
Iterates over the stream of attributes and stores a pointer to each attribute in the index array using the attribute type as index to the array. Attribute with a type greater than the maximum type specified will be silently ignored in order to maintain backwards compatibility. If policy is not NULL, the attribute will be validated using the specified policy.
Definition at line 583 of file attr.c.
References nla_for_each_attr, and nla_type().
Referenced by nla_parse_nested(), and nlmsg_parse().
int nla_validate | ( | struct nlattr * | head, | |
int | len, | |||
int | maxtype, | |||
struct nla_policy * | policy | |||
) |
Validate a stream of attributes.
head | Head of attributes stream. | |
len | Length of attributes stream. | |
maxtype | Maximum attribute type expected and accepted. | |
policy | Validation policy. |
Iterates over the stream of attributes and validates each attribute one by one using the specified policy. Attributes with a type greater than the maximum type specified will be silently ignored in order to maintain backwards compatibility.
See Attribute Data Types for more details on what kind of validation checks are performed on each attribute data type.
Definition at line 636 of file attr.c.
References nla_for_each_attr.
Referenced by nlmsg_validate().
struct nlattr* nla_find | ( | struct nlattr * | head, | |
int | len, | |||
int | attrtype | |||
) | [read] |
Find a single attribute in a stream of attributes.
head | Head of attributes stream. | |
len | Length of attributes stream. | |
attrtype | Attribute type to look for. |
Iterates over the stream of attributes and compares each type with the type specified. Returns the first attribute which matches the type.
Definition at line 665 of file attr.c.
References nla_for_each_attr, and nla_type().
Referenced by nlmsg_find_attr().
int nla_memcpy | ( | void * | dest, | |
struct nlattr * | src, | |||
int | count | |||
) |
Copy attribute payload to another memory area.
dest | Pointer to destination memory area. | |
src | Attribute | |
count | Number of bytes to copy at most. |
Note: The number of bytes copied is limited by the length of the attribute payload.
Definition at line 695 of file attr.c.
References nla_data(), and nla_len().
Referenced by nla_get_u64().
size_t nla_strlcpy | ( | char * | dst, | |
const struct nlattr * | nla, | |||
size_t | dstsize | |||
) |
Copy string attribute payload to a buffer.
dst | Pointer to destination buffer. | |
nla | Attribute of type NLA_STRING. | |
dstsize | Size of destination buffer in bytes. |
Copies at most dstsize - 1 bytes to the destination buffer. The result is always a valid NUL terminated string. Unlike strlcpy the destination buffer is always padded out.
Definition at line 720 of file attr.c.
References nla_data(), and nla_len().
int nla_memcmp | ( | const struct nlattr * | nla, | |
const void * | data, | |||
size_t | size | |||
) |
Compare attribute payload with memory area.
nla | Attribute. | |
data | Memory area to compare to. | |
size | Number of bytes to compare. |
Definition at line 747 of file attr.c.
References nla_data(), and nla_len().
int nla_strcmp | ( | const struct nlattr * | nla, | |
const char * | str | |||
) |
Compare string attribute payload with string.
nla | Attribute of type NLA_STRING. | |
str | NUL terminated string. |
Definition at line 765 of file attr.c.
References nla_data(), and nla_len().
struct nlattr* nla_reserve | ( | struct nl_msg * | msg, | |
int | attrtype, | |||
int | attrlen | |||
) | [read] |
Reserve space for a attribute.
msg | Netlink Message. | |
attrtype | Attribute Type. | |
attrlen | Length of payload. |
Reserves room for a attribute in the specified netlink message and fills in the attribute header (type, length). Returns NULL if there is unsuficient space for the attribute.
Any padding between payload and the start of the next attribute is zeroed out.
Definition at line 798 of file attr.c.
References nla_attr_size(), nla_padlen(), nla_total_size(), and nlmsg_data().
Referenced by nla_put().
int nla_put | ( | struct nl_msg * | msg, | |
int | attrtype, | |||
int | datalen, | |||
const void * | data | |||
) |
Add a unspecific attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
datalen | Length of data to be used as payload. | |
data | Pointer to data to be used as attribute payload. |
Reserves room for a unspecific attribute and copies the provided data into the message as payload of the attribute. Returns an error if there is insufficient space for the attribute.
Definition at line 839 of file attr.c.
References nla_data(), nla_reserve(), and nlmsg_data().
Referenced by nla_nest_start(), nla_put_addr(), nla_put_data(), nla_put_flag(), nla_put_nested(), nla_put_string(), nla_put_u16(), nla_put_u32(), nla_put_u64(), and nla_put_u8().
int nla_put_data | ( | struct nl_msg * | msg, | |
int | attrtype, | |||
struct nl_data * | data | |||
) |
Add abstract data as unspecific attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
data | Abstract data object. |
Equivalent to nla_put() except that the length of the payload is derived from the abstract data object.
Definition at line 869 of file attr.c.
References nl_data_get(), nl_data_get_size(), and nla_put().
int nla_put_addr | ( | struct nl_msg * | msg, | |
int | attrtype, | |||
struct nl_addr * | addr | |||
) |
Add abstract address as unspecific attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
addr | Abstract address object. |
Definition at line 884 of file attr.c.
References nl_addr_get_binary_addr(), nl_addr_get_len(), and nla_put().
int nla_put_u8 | ( | struct nl_msg * | msg, | |
int | attrtype, | |||
uint8_t | value | |||
) |
Add 8 bit integer attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
value | Numeric value to store as payload. |
Definition at line 905 of file attr.c.
References nla_put().
uint8_t nla_get_u8 | ( | struct nlattr * | nla | ) |
Return value of 8 bit integer attribute.
nla | 8 bit integer attribute |
Definition at line 916 of file attr.c.
References nla_data().
int nla_put_u16 | ( | struct nl_msg * | msg, | |
int | attrtype, | |||
uint16_t | value | |||
) |
Add 16 bit integer attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
value | Numeric value to store as payload. |
Definition at line 930 of file attr.c.
References nla_put().
uint16_t nla_get_u16 | ( | struct nlattr * | nla | ) |
Return payload of 16 bit integer attribute.
nla | 16 bit integer attribute |
Definition at line 941 of file attr.c.
References nla_data().
int nla_put_u32 | ( | struct nl_msg * | msg, | |
int | attrtype, | |||
uint32_t | value | |||
) |
Add 32 bit integer attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
value | Numeric value to store as payload. |
Definition at line 955 of file attr.c.
References nla_put().
uint32_t nla_get_u32 | ( | struct nlattr * | nla | ) |
Return payload of 32 bit integer attribute.
nla | 32 bit integer attribute. |
Definition at line 966 of file attr.c.
References nla_data().
int nla_put_u64 | ( | struct nl_msg * | msg, | |
int | attrtype, | |||
uint64_t | value | |||
) |
Add 64 bit integer attribute to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
value | Numeric value to store as payload. |
Definition at line 980 of file attr.c.
References nla_put().
Referenced by nla_put_msecs().
uint64_t nla_get_u64 | ( | struct nlattr * | nla | ) |
Return payload of u64 attribute.
nla | u64 netlink attribute |
Definition at line 991 of file attr.c.
References nla_memcpy().
Referenced by nla_get_msecs().
int nla_put_string | ( | struct nl_msg * | msg, | |
int | attrtype, | |||
const char * | str | |||
) |
char* nla_get_string | ( | struct nlattr * | nla | ) |
Return payload of string attribute.
nla | String attribute. |
Definition at line 1026 of file attr.c.
References nla_data().
int nla_put_flag | ( | struct nl_msg * | msg, | |
int | attrtype | |||
) |
int nla_get_flag | ( | struct nlattr * | nla | ) |
int nla_put_msecs | ( | struct nl_msg * | n, | |
int | attrtype, | |||
unsigned long | msecs | |||
) |
Add a msecs netlink attribute to a netlink message.
n | netlink message | |
attrtype | attribute type | |
msecs | number of msecs |
Definition at line 1078 of file attr.c.
References nla_put_u64().
unsigned long nla_get_msecs | ( | struct nlattr * | nla | ) |
Return payload of msecs attribute.
nla | msecs netlink attribute |
Definition at line 1089 of file attr.c.
References nla_get_u64().
int nla_put_nested | ( | struct nl_msg * | msg, | |
int | attrtype, | |||
struct nl_msg * | nested | |||
) |
Add nested attributes to netlink message.
msg | Netlink message. | |
attrtype | Attribute type. | |
nested | Message containing attributes to be nested. |
Takes the attributes found in the nested message and appends them to the message msg nested in a container of the type attrtype. The nested message may not have a family specific header.
Definition at line 1113 of file attr.c.
References nla_put(), nlmsg_data(), and nlmsg_datalen().
Referenced by rtnl_neightbl_build_change_request().
struct nlattr* nla_nest_start | ( | struct nl_msg * | msg, | |
int | attrtype | |||
) | [read] |
Start a new level of nested attributes.
msg | Netlink message. | |
attrtype | Attribute type of container. |
Definition at line 1130 of file attr.c.
References nla_put().
Referenced by rtnl_link_build_change_request().
int nla_nest_end | ( | struct nl_msg * | msg, | |
struct nlattr * | start | |||
) |
Finalize nesting of attributes.
msg | Netlink message. | |
start | Container attribute as returned from nla_nest_start(). |
Corrects the container attribute header to include the appeneded attributes.
Definition at line 1152 of file attr.c.
References nlmsg_reserve().
Referenced by rtnl_link_build_change_request().
int nla_parse_nested | ( | struct nlattr * | tb[], | |
int | maxtype, | |||
struct nlattr * | nla, | |||
struct nla_policy * | policy | |||
) |
Create attribute index based on nested attribute.
tb | Index array to be filled (maxtype+1 elements). | |
maxtype | Maximum attribute type expected and accepted. | |
nla | Nested Attribute. | |
policy | Attribute validation policy. |
Feeds the stream of attributes nested into the specified attribute to nla_parse().
Definition at line 1193 of file attr.c.
References nla_data(), nla_len(), and nla_parse().
Referenced by rtnl_ematch_parse_attr().