netfilter
firewalling, NAT, and packet mangling for linux
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Netlink attribute helpers

Netlink Type-Length-Value (TLV) attribute: More...

Collaboration diagram for Netlink attribute helpers:

Data Structures

struct  nla_policy
 Attribute validation policy. More...
 

Functions

uint16_t mnl_attr_get_type (const struct nlattr *attr)
 mnl_attr_get_type - get type of netlink attribute More...
 
 EXPORT_SYMBOL (mnl_attr_get_type)
 
uint16_t mnl_attr_get_len (const struct nlattr *attr)
 mnl_attr_get_len - get length of netlink attribute More...
 
 EXPORT_SYMBOL (mnl_attr_get_len)
 
uint16_t mnl_attr_get_payload_len (const struct nlattr *attr)
 mnl_attr_get_payload_len - get the attribute payload-value length More...
 
 EXPORT_SYMBOL (mnl_attr_get_payload_len)
 
void * mnl_attr_get_payload (const struct nlattr *attr)
 mnl_attr_get_payload - get pointer to the attribute payload More...
 
 EXPORT_SYMBOL (mnl_attr_get_payload)
 
bool mnl_attr_ok (const struct nlattr *attr, int len)
 mnl_attr_ok - check if there is room for an attribute in a buffer More...
 
 EXPORT_SYMBOL (mnl_attr_ok)
 
struct nlattrmnl_attr_next (const struct nlattr *attr)
 mnl_attr_next - get the next attribute in the payload of a netlink message More...
 
 EXPORT_SYMBOL (mnl_attr_next)
 
int mnl_attr_type_valid (const struct nlattr *attr, uint16_t max)
 mnl_attr_type_valid - check if the attribute type is valid More...
 
 EXPORT_SYMBOL (mnl_attr_type_valid)
 
int mnl_attr_validate (const struct nlattr *attr, enum mnl_attr_data_type type)
 mnl_attr_validate - validate netlink attribute (simplified version) More...
 
 EXPORT_SYMBOL (mnl_attr_validate)
 
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) More...
 
 EXPORT_SYMBOL (mnl_attr_validate2)
 
int mnl_attr_parse (const struct nlmsghdr *nlh, unsigned int offset, mnl_attr_cb_t cb, void *data)
 mnl_attr_parse - parse attributes More...
 
 EXPORT_SYMBOL (mnl_attr_parse)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_parse_nested)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_parse_payload)
 
uint8_t mnl_attr_get_u8 (const struct nlattr *attr)
 mnl_attr_get_u8 - returns 8-bit unsigned integer attribute payload More...
 
 EXPORT_SYMBOL (mnl_attr_get_u8)
 
uint16_t mnl_attr_get_u16 (const struct nlattr *attr)
 mnl_attr_get_u16 - returns 16-bit unsigned integer attribute payload More...
 
 EXPORT_SYMBOL (mnl_attr_get_u16)
 
uint32_t mnl_attr_get_u32 (const struct nlattr *attr)
 mnl_attr_get_u32 - returns 32-bit unsigned integer attribute payload More...
 
 EXPORT_SYMBOL (mnl_attr_get_u32)
 
uint64_t mnl_attr_get_u64 (const struct nlattr *attr)
 mnl_attr_get_u64 - returns 64-bit unsigned integer attribute. More...
 
 EXPORT_SYMBOL (mnl_attr_get_u64)
 
const char * mnl_attr_get_str (const struct nlattr *attr)
 mnl_attr_get_str - returns pointer to string attribute. More...
 
 EXPORT_SYMBOL (mnl_attr_get_str)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_u8)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_u16)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_u32)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_u64)
 
void mnl_attr_put_str (struct nlmsghdr *nlh, uint16_t type, const char *data)
 mnl_attr_put_str - add string attribute to netlink message More...
 
 EXPORT_SYMBOL (mnl_attr_put_str)
 
void mnl_attr_put_strz (struct nlmsghdr *nlh, uint16_t type, const char *data)
 mnl_attr_put_strz - add string attribute to netlink message More...
 
 EXPORT_SYMBOL (mnl_attr_put_strz)
 
struct nlattrmnl_attr_nest_start (struct nlmsghdr *nlh, uint16_t type)
 mnl_attr_nest_start - start an attribute nest More...
 
 EXPORT_SYMBOL (mnl_attr_nest_start)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_check)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_u8_check)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_u16_check)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_u32_check)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_u64_check)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_str_check)
 
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 More...
 
 EXPORT_SYMBOL (mnl_attr_put_strz_check)
 
struct nlattrmnl_attr_nest_start_check (struct nlmsghdr *nlh, size_t buflen, uint16_t type)
 mnl_attr_nest_start_check - start an attribute nest More...
 
 EXPORT_SYMBOL (mnl_attr_nest_start_check)
 
void mnl_attr_nest_end (struct nlmsghdr *nlh, struct nlattr *start)
 mnl_attr_nest_end - end an attribute nest More...
 
 EXPORT_SYMBOL (mnl_attr_nest_end)
 
void mnl_attr_nest_cancel (struct nlmsghdr *nlh, struct nlattr *start)
 mnl_attr_nest_cancel - cancel an attribute nest More...
 
 EXPORT_SYMBOL (mnl_attr_nest_cancel)
 

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...
 

Attribute Construction (Exception Based)

#define NLA_PUT(msg, attrtype, attrlen, data)
 Add unspecific attribute to netlink message. More...
 
#define NLA_PUT_TYPE(msg, type, attrtype, value)
 Add atomic type attribute to netlink message. More...
 
#define NLA_PUT_U8(msg, attrtype, value)   NLA_PUT_TYPE(msg, uint8_t, attrtype, value)
 Add 8 bit integer attribute to netlink message. More...
 
#define NLA_PUT_U16(msg, attrtype, value)   NLA_PUT_TYPE(msg, uint16_t, attrtype, value)
 Add 16 bit integer attribute to netlink message. More...
 
#define NLA_PUT_U32(msg, attrtype, value)   NLA_PUT_TYPE(msg, uint32_t, attrtype, value)
 Add 32 bit integer attribute to netlink message. More...
 
#define NLA_PUT_U64(msg, attrtype, value)   NLA_PUT_TYPE(msg, uint64_t, attrtype, value)
 Add 64 bit integer attribute to netlink message. More...
 
#define NLA_PUT_STRING(msg, attrtype, value)   NLA_PUT(msg, attrtype, strlen(value) + 1, value)
 Add string attribute to netlink message. More...
 
#define NLA_PUT_FLAG(msg, attrtype)   NLA_PUT(msg, attrtype, 0, NULL)
 Add flag attribute to netlink message. More...
 
#define NLA_PUT_MSECS(msg, attrtype, msecs)   NLA_PUT_U64(msg, attrtype, msecs)
 Add msecs attribute to netlink message. More...
 
#define NLA_PUT_ADDR(msg, attrtype, addr)
 Add address attribute to netlink message. More...
 
#define NLA_PUT_DATA(msg, attrtype, data)
 Add abstract data attribute to netlink message. More...
 

Iterators

#define nla_for_each_attr(pos, head, len, rem)
 Iterate over a stream of attributes. More...
 
#define nla_for_each_nested(pos, nla, rem)
 Iterate over a stream of nested attributes. More...
 

Attribute Size Calculation

int nla_attr_size (int payload)
 Return size of attribute whithout padding. More...
 
int nla_total_size (int payload)
 Return size of attribute including padding. More...
 
int nla_padlen (int payload)
 Return length of padding at the tail of the attribute. More...
 

Parsing Attributes

int nla_type (const struct nlattr *nla)
 Return type of the attribute. More...
 
void * nla_data (const struct nlattr *nla)
 Return pointer to the payload section. More...
 
int nla_len (const struct nlattr *nla)
 Return length of the payload . More...
 
int nla_ok (const struct nlattr *nla, int remaining)
 Check if the attribute header and payload can be accessed safely. More...
 
struct nlattrnla_next (const struct nlattr *nla, int *remaining)
 Return next attribute in a stream of attributes. More...
 
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. More...
 
int nla_validate (struct nlattr *head, int len, int maxtype, struct nla_policy *policy)
 Validate a stream of attributes. More...
 
struct nlattrnla_find (struct nlattr *head, int len, int attrtype)
 Find a single attribute in a stream of attributes. More...
 

Helper Functions

int nla_memcpy (void *dest, struct nlattr *src, int count)
 Copy attribute payload to another memory area. More...
 
size_t nla_strlcpy (char *dst, const struct nlattr *nla, size_t dstsize)
 Copy string attribute payload to a buffer. More...
 
int nla_memcmp (const struct nlattr *nla, const void *data, size_t size)
 Compare attribute payload with memory area. More...
 
int nla_strcmp (const struct nlattr *nla, const char *str)
 Compare string attribute payload with string. More...
 

Unspecific Attribute

struct nlattrnla_reserve (struct nl_msg *msg, int attrtype, int attrlen)
 Reserve space for a attribute. More...
 
int nla_put (struct nl_msg *msg, int attrtype, int datalen, const void *data)
 Add a unspecific attribute to netlink message. More...
 
int nla_put_data (struct nl_msg *msg, int attrtype, struct nl_data *data)
 Add abstract data as unspecific attribute to netlink message. More...
 
int nla_put_addr (struct nl_msg *msg, int attrtype, struct nl_addr *addr)
 Add abstract address as unspecific attribute to netlink message. More...
 

Integer Attributes

int nla_put_u8 (struct nl_msg *msg, int attrtype, uint8_t value)
 Add 8 bit integer attribute to netlink message. More...
 
uint8_t nla_get_u8 (struct nlattr *nla)
 Return value of 8 bit integer attribute. More...
 
int nla_put_u16 (struct nl_msg *msg, int attrtype, uint16_t value)
 Add 16 bit integer attribute to netlink message. More...
 
uint16_t nla_get_u16 (struct nlattr *nla)
 Return payload of 16 bit integer attribute. More...
 
int nla_put_u32 (struct nl_msg *msg, int attrtype, uint32_t value)
 Add 32 bit integer attribute to netlink message. More...
 
uint32_t nla_get_u32 (struct nlattr *nla)
 Return payload of 32 bit integer attribute. More...
 
int nla_put_u64 (struct nl_msg *msg, int attrtype, uint64_t value)
 Add 64 bit integer attribute to netlink message. More...
 
uint64_t nla_get_u64 (struct nlattr *nla)
 Return payload of u64 attribute. More...
 

String Attribute

int nla_put_string (struct nl_msg *msg, int attrtype, const char *str)
 Add string attribute to netlink message. More...
 
char * nla_get_string (struct nlattr *nla)
 Return payload of string attribute. More...
 
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. More...
 
int nla_get_flag (struct nlattr *nla)
 Return true if flag attribute is set. More...
 

Microseconds Attribute

int nla_put_msecs (struct nl_msg *n, int attrtype, unsigned long msecs)
 Add a msecs netlink attribute to a netlink message. More...
 
unsigned long nla_get_msecs (struct nlattr *nla)
 Return payload of msecs attribute. More...
 

Nested Attribute

int nla_put_nested (struct nl_msg *msg, int attrtype, struct nl_msg *nested)
 Add nested attributes to netlink message. More...
 
struct nlattrnla_nest_start (struct nl_msg *msg, int attrtype)
 Start a new level of nested attributes. More...
 
int nla_nest_end (struct nl_msg *msg, struct nlattr *start)
 Finalize nesting of attributes. More...
 
int nla_parse_nested (struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
 Create attribute index based on nested attribute. More...
 

Detailed Description

Netlink Type-Length-Value (TLV) attribute:

Netlink Attributes Construction/Parsing Interface.

    |<-- 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.

Netlink Attributes

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) ->

Attribute Data Types

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.

Unspecific Attribute (NLA_UNSPEC)
This is the standard type if no type is specified. It is used for binary data of arbitary length. Typically this attribute carries a binary structure or a stream of bytes.
// 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]);
Integers (NLA_U8, NLA_U16, NLA_U32, NLA_U64)
Integers come in different sizes from 8 bit to 64 bit. However, since the payload length is aligned to 4 bytes, integers smaller than 32 bit are only useful to enforce the maximum range of values.
Note: There is no difference made between signed and unsigned integers. The validation only enforces the minimal payload length required to store an integer of specified type.
// 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]);
Character string (NLA_STRING)
This data type represents a NUL terminated character string of variable length. For binary data streams the type NLA_UNSPEC is recommended.
// 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]);
Flag (NLA_FLAG)
This attribute type may be used to indicate the presence of a flag. The attribute is only valid if the payload length is zero. The presence of the attribute header indicates the presence of the flag.
// 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
Micro Seconds (NLA_MSECS)
Nested Attribute (NLA_NESTED)
Attributes can be nested and put into a container to create groups, lists or to construct trees of attributes. Nested attributes are often used to pass attributes to a subsystem where the top layer has no knowledge of the configuration possibilities of each subsystem.
Note: When validating the attributes using nlmsg_validate() or nlmsg_parse() it will only affect the top level attributes. Each level of nested attributes must be validated seperately using nla_parse_nested() or nla_validate().
// 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]);
}

Exception Based Attribute Construction

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;
}

Examples

Example 1.1 Constructing a netlink message with attributes.
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
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
// 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;
}
Example 2.1 Parsing a netlink message with attributes.
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;
}

Macro Definition Documentation

#define nla_for_each_attr (   pos,
  head,
  len,
  rem 
)
Value:
for (pos = head, rem = len; \
nla_ok(pos, rem); \
pos = nla_next(pos, &(rem)))
int nla_ok(const struct nlattr *, int)
Check if the attribute header and payload can be accessed safely.
Definition: attr.c:488
struct nlattr * nla_next(const struct nlattr *, int *)
Return next attribute in a stream of attributes.
Definition: attr.c:511
u8 pos
Definition: ip_set_hash_gen.h:187
int len
Definition: utils.c:56

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

Referenced by nla_find(), nla_parse(), and nla_validate().

#define nla_for_each_nested (   pos,
  nla,
  rem 
)
Value:
for (pos = nla_data(nla), rem = nla_len(nla); \
nla_ok(pos, rem); \
pos = nla_next(pos, &(rem)))
int nla_ok(const struct nlattr *, int)
Check if the attribute header and payload can be accessed safely.
Definition: attr.c:488
struct nlattr * nla_next(const struct nlattr *, int *)
Return next attribute in a stream of attributes.
Definition: attr.c:511
void * nla_data(const struct nlattr *)
Return pointer to the payload section.
Definition: attr.c:460
u8 pos
Definition: ip_set_hash_gen.h:187
int nla_len(const struct nlattr *)
Return length of the payload .
Definition: attr.c:471

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

Referenced by nfnlmsg_nft_rule_parse(), and rtnl_ematch_parse().

#define NLA_PUT (   msg,
  attrtype,
  attrlen,
  data 
)
Value:
do { \
if (nla_put(msg, attrtype, attrlen, data) < 0) \
goto nla_put_failure; \
} while(0)
enum nf_conntrack_query query const void * data
Definition: libnetfilter_conntrack.h:570
int nla_put(struct nl_msg *, int, int, const void *)
Add a unspecific attribute to netlink message.
Definition: attr.c:837

Add unspecific attribute to netlink message.

  • msg Netlink message.
  • attrtype Attribute type.
  • attrlen Length of attribute payload.
  • data Head of attribute payload.

Referenced by netem_build_msg(), and nfnl_queue_build_create_request().

#define NLA_PUT_ADDR (   msg,
  attrtype,
  addr 
)
Value:
NLA_PUT(msg, attrtype, nl_addr_get_len(addr), \
unsigned int nl_addr_get_len(struct nl_addr *)
Get length of binary address of abstract address object.
Definition: addr.c:793
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition: attr.h:143
void * nl_addr_get_binary_addr(struct nl_addr *)
Get binary address of abstract address object.
Definition: addr.c:784

Add address attribute to netlink message.

  • msg Netlink message.
  • attrtype Attribute type.
  • addr Abstract address object.

Referenced by rtnl_link_build_change_request(), and rtnl_route_build_msg().

#define NLA_PUT_DATA (   msg,
  attrtype,
  data 
)
Value:
NLA_PUT(msg, attrtype, nl_data_get_size(data), \
void * nl_data_get(const struct nl_data *)
Get data buffer of abstract data object.
Definition: data.c:144
size_t nl_data_get_size(const struct nl_data *)
Get size of data buffer of abstract data object.
Definition: data.c:154
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition: attr.h:143
enum nf_conntrack_query query const void * data
Definition: libnetfilter_conntrack.h:570

Add abstract data attribute to netlink message.

  • msg Netlink message.
  • attrtype Attribute type.
  • data Abstract data object.
#define NLA_PUT_FLAG (   msg,
  attrtype 
)    NLA_PUT(msg, attrtype, 0, NULL)

Add flag attribute to netlink message.

  • msg Netlink message.
  • attrtype Attribute type.
#define NLA_PUT_MSECS (   msg,
  attrtype,
  msecs 
)    NLA_PUT_U64(msg, attrtype, msecs)

Add msecs attribute to netlink message.

  • msg Netlink message.
  • attrtype Attribute type.
  • msecs Numeric value in micro seconds.
#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.

Referenced by nfnl_nft_set_dump_request(), nfnl_nft_setelem_dump_request(), rtnl_link_build_change_request(), and rtnl_neightbl_build_change_request().

#define NLA_PUT_TYPE (   msg,
  type,
  attrtype,
  value 
)
Value:
do { \
type __tmp = value; \
NLA_PUT(msg, attrtype, sizeof(type), &__tmp); \
} while(0)
unsigned char value[0]
Definition: ip_set_hash_gen.h:188
struct expr
void size_t u_int16_t type
Definition: libnetfilter_conntrack.h:557
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition: attr.h:143

Add atomic type attribute to netlink message.

  • msg Netlink message.
  • type Atomic type.
  • attrtype Attribute type.
  • value Head of attribute payload.
#define NLA_PUT_U16 (   msg,
  attrtype,
  value 
)    NLA_PUT_TYPE(msg, uint16_t, attrtype, value)

Add 16 bit integer attribute to netlink message.

  • msg Netlink message.
  • attrtype Attribute type.
  • value Numeric 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.

Referenced by rtnl_link_build_change_request(), rtnl_neightbl_build_change_request(), and rtnl_route_build_msg().

#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.

Referenced by rtnl_neightbl_build_change_request().

#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.

Referenced by rtnl_link_build_change_request().

Enumeration Type Documentation

anonymous enum

Basic attribute data types.

See Attribute Data Types for more details.

Enumerator
NLA_UNSPEC 

Unspecified type, binary data chunk.

NLA_U8 

8 bit integer

NLA_U16 

16 bit integer

NLA_U32 

32 bit integer

NLA_U64 

64 bit integer

NLA_STRING 

NUL terminated character string.

NLA_FLAG 

Flag.

NLA_MSECS 

Micro seconds (64bit)

NLA_NESTED 

Nested attributes.

__NLA_TYPE_MAX 

Function Documentation

EXPORT_SYMBOL ( mnl_attr_get_type  )
EXPORT_SYMBOL ( mnl_attr_get_len  )
EXPORT_SYMBOL ( mnl_attr_get_payload_len  )
EXPORT_SYMBOL ( mnl_attr_get_payload  )
EXPORT_SYMBOL ( mnl_attr_ok  )
EXPORT_SYMBOL ( mnl_attr_next  )
EXPORT_SYMBOL ( mnl_attr_type_valid  )
EXPORT_SYMBOL ( mnl_attr_validate  )
EXPORT_SYMBOL ( mnl_attr_validate2  )
EXPORT_SYMBOL ( mnl_attr_parse  )
EXPORT_SYMBOL ( mnl_attr_parse_nested  )
EXPORT_SYMBOL ( mnl_attr_parse_payload  )
EXPORT_SYMBOL ( mnl_attr_get_u8  )
EXPORT_SYMBOL ( mnl_attr_get_u16  )
EXPORT_SYMBOL ( mnl_attr_get_u32  )
EXPORT_SYMBOL ( mnl_attr_get_u64  )
EXPORT_SYMBOL ( mnl_attr_get_str  )
EXPORT_SYMBOL ( mnl_attr_put  )
EXPORT_SYMBOL ( mnl_attr_put_u8  )
EXPORT_SYMBOL ( mnl_attr_put_u16  )
EXPORT_SYMBOL ( mnl_attr_put_u32  )
EXPORT_SYMBOL ( mnl_attr_put_u64  )
EXPORT_SYMBOL ( mnl_attr_put_str  )
EXPORT_SYMBOL ( mnl_attr_put_strz  )
EXPORT_SYMBOL ( mnl_attr_nest_start  )
EXPORT_SYMBOL ( mnl_attr_put_check  )
EXPORT_SYMBOL ( mnl_attr_put_u8_check  )
EXPORT_SYMBOL ( mnl_attr_put_u16_check  )
EXPORT_SYMBOL ( mnl_attr_put_u32_check  )
EXPORT_SYMBOL ( mnl_attr_put_u64_check  )
EXPORT_SYMBOL ( mnl_attr_put_str_check  )
EXPORT_SYMBOL ( mnl_attr_put_strz_check  )
EXPORT_SYMBOL ( mnl_attr_nest_start_check  )
EXPORT_SYMBOL ( mnl_attr_nest_end  )
EXPORT_SYMBOL ( mnl_attr_nest_cancel  )
uint16_t mnl_attr_get_len ( const struct nlattr attr)

mnl_attr_get_len - get length of netlink attribute

Parameters
attrpointer to netlink attribute

This function returns the attribute length that is the attribute header plus the attribute payload.

References nlattr::nla_len.

void* mnl_attr_get_payload ( const struct nlattr attr)

mnl_attr_get_payload - get pointer to the attribute payload

Parameters
attrpointer to netlink attribute

This function return a pointer to the attribute payload.

References MNL_ATTR_HDRLEN.

Referenced by mnl_attr_get_str(), mnl_attr_get_u16(), mnl_attr_get_u32(), mnl_attr_get_u64(), mnl_attr_get_u8(), mnl_attr_put(), and nft_rule_nlmsg_parse().

Here is the caller graph for this function:

uint16_t mnl_attr_get_payload_len ( const struct nlattr attr)

mnl_attr_get_payload_len - get the attribute payload-value length

Parameters
attrpointer to netlink attribute

This function returns the attribute payload-value length.

References MNL_ATTR_HDRLEN, and nlattr::nla_len.

Referenced by nft_rule_nlmsg_parse().

Here is the caller graph for this function:

const char* mnl_attr_get_str ( const struct nlattr attr)

mnl_attr_get_str - returns pointer to string attribute.

Parameters
attrpointer to netlink attribute

This function returns the payload of string attribute value.

References mnl_attr_get_payload().

Referenced by nfacct_nlmsg_parse_payload(), nfct_helper_nlmsg_parse_payload(), nfct_timeout_nlmsg_parse_payload(), nfexp_nlmsg_parse(), nft_chain_nlmsg_parse(), nft_rule_nlmsg_parse(), nft_set_elems_nlmsg_parse(), nft_set_nlmsg_parse(), and nft_table_nlmsg_parse().

Here is the call graph for this function:

Here is the caller graph for this function:

uint16_t mnl_attr_get_type ( const struct nlattr attr)

mnl_attr_get_type - get type of netlink attribute

Parameters
attrpointer to netlink attribute

This function returns the attribute type.

References nlattr::nla_type, and NLA_TYPE_MASK.

Referenced by mnl_attr_type_valid().

Here is the caller graph for this function:

uint16_t mnl_attr_get_u16 ( const struct nlattr attr)

mnl_attr_get_u16 - returns 16-bit unsigned integer attribute payload

Parameters
attrpointer to netlink attribute

This function returns the 16-bit value of the attribute payload.

References mnl_attr_get_payload().

Referenced by nfct_payload_parse(), nfct_timeout_nlmsg_parse_payload(), and nfexp_nlmsg_parse().

Here is the call graph for this function:

Here is the caller graph for this function:

uint32_t mnl_attr_get_u32 ( const struct nlattr attr)

mnl_attr_get_u32 - returns 32-bit unsigned integer attribute payload

Parameters
attrpointer to netlink attribute

This function returns the 32-bit value of the attribute payload.

References mnl_attr_get_payload().

Referenced by nfacct_nlmsg_parse_payload(), nfct_helper_nlmsg_parse_payload(), nfct_payload_parse(), nfexp_nlmsg_parse(), nft_chain_nlmsg_parse(), nft_gen_nlmsg_parse(), nft_set_elems_nlmsg_parse(), nft_set_nlmsg_parse(), and nft_table_nlmsg_parse().

Here is the call graph for this function:

Here is the caller graph for this function:

uint64_t mnl_attr_get_u64 ( const struct nlattr attr)

mnl_attr_get_u64 - returns 64-bit unsigned integer attribute.

Parameters
attrpointer to netlink attribute

This function returns the 64-bit value of the attribute payload. This function is align-safe, since accessing 64-bit Netlink attributes is a common source of alignment issues.

References mnl_attr_get_payload().

Referenced by nfacct_nlmsg_parse_payload(), nft_chain_nlmsg_parse(), and nft_rule_nlmsg_parse().

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t mnl_attr_get_u8 ( const struct nlattr attr)

mnl_attr_get_u8 - returns 8-bit unsigned integer attribute payload

Parameters
attrpointer to netlink attribute

This function returns the 8-bit value of the attribute payload.

References mnl_attr_get_payload().

Referenced by nfct_timeout_nlmsg_parse_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

void mnl_attr_nest_cancel ( struct nlmsghdr nlh,
struct nlattr start 
)

mnl_attr_nest_cancel - cancel an attribute nest

Parameters
nlhpointer to the netlink message
startpointer to the attribute nest returned by mnl_attr_nest_start()

This function updates the attribute header that identifies the nest.

References mnl_nlmsg_get_payload_tail(), and nlmsghdr::nlmsg_len.

Referenced by nfct_build_tuple().

Here is the call graph for this function:

Here is the caller graph for this function:

void mnl_attr_nest_end ( struct nlmsghdr nlh,
struct nlattr start 
)

mnl_attr_nest_end - end an attribute nest

Parameters
nlhpointer to the netlink message
startpointer to the attribute nest returned by mnl_attr_nest_start()

This function updates the attribute header that identifies the nest.

References mnl_nlmsg_get_payload_tail(), and nlattr::nla_len.

Referenced by nfct_build_tuple(), nfct_helper_nlmsg_build_payload(), nfct_timeout_nlmsg_build_payload(), nft_chain_nlmsg_build_payload(), nft_rule_expr_build_payload(), nft_rule_nlmsg_build_payload(), nft_set_elem_nlmsg_build_payload(), nft_set_elems_nlmsg_build_payload(), and nft_set_elems_nlmsg_build_payload_iter().

Here is the call graph for this function:

Here is the caller graph for this function:

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

mnl_attr_nest_start - start an attribute nest

Parameters
nlhpointer to the netlink message
typenetlink attribute type

This function adds the attribute header that identifies the beginning of an attribute nest. This function always returns a valid pointer to the beginning of the nest.

References MNL_ALIGN, mnl_nlmsg_get_payload_tail(), NLA_F_NESTED, nlattr::nla_type, nlmsghdr::nlmsg_len, and type.

Referenced by mnl_attr_nest_start_check(), nfct_build_tuple(), nfct_helper_nlmsg_build_payload(), nfct_timeout_nlmsg_build_payload(), nft_chain_nlmsg_build_payload(), nft_rule_expr_build_payload(), nft_rule_nlmsg_build_payload(), nft_set_elem_nlmsg_build_payload(), nft_set_elems_nlmsg_build_payload(), and nft_set_elems_nlmsg_build_payload_iter().

Here is the call graph for this function:

Here is the caller graph for this function:

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
buflensize of buffer which stores the message
nlhpointer to the netlink message
typenetlink attribute type

This function adds the attribute header that identifies the beginning of an attribute nest. If the nested attribute cannot be added then NULL, otherwise valid pointer to the beginning of the nest is returned.

References MNL_ATTR_HDRLEN, mnl_attr_nest_start(), nlmsghdr::nlmsg_len, and NULL.

Here is the call graph for this function:

struct nlattr* mnl_attr_next ( const struct nlattr attr)

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

Parameters
attrpointer to the current attribute

This function returns a pointer to the next attribute after the one passed as parameter. You have to use mnl_attr_ok() to ensure that the next attribute is valid.

References MNL_ALIGN, and nlattr::nla_len.

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
attrattribute that we want to check if there is room for
lenremaining 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. Thus, it returns true on success and false on error.

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

References nlattr::nla_len.

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

mnl_attr_parse - parse attributes

Parameters
nlhpointer to netlink message
offsetoffset to start parsing from (if payload is after any header)
cbcallback function that is called for each attribute
datapointer to data that is passed to the callback function

This function allows 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).

This function propagates the return value of the callback, which can be MNL_CB_ERROR, MNL_CB_OK or MNL_CB_STOP.

References mnl_attr_for_each, MNL_CB_OK, and MNL_CB_STOP.

Referenced by ipset_debug_msg(), nfacct_nlmsg_parse_payload(), nfct_helper_nlmsg_parse_payload(), nfct_timeout_nlmsg_parse_payload(), nfexp_nlmsg_parse(), nfq_nlmsg_parse(), nft_chain_nlmsg_parse(), nft_gen_nlmsg_parse(), nft_rule_nlmsg_parse(), nft_set_elems_nlmsg_parse(), nft_set_nlmsg_parse(), and nft_table_nlmsg_parse().

Here is the caller graph for this function:

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
nestedpointer to netlink attribute that contains a nest
cbcallback function that is called for each attribute in the nest
datapointer to data passed to the callback function

This function allows 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).

This function propagates the return value of the callback, which can be MNL_CB_ERROR, MNL_CB_OK or MNL_CB_STOP.

References mnl_attr_for_each_nested, MNL_CB_OK, and MNL_CB_STOP.

Referenced by nfct_parse_tuple(), and nft_parse_data().

Here is the caller graph for this function:

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
payloadpointer to payload of the Netlink message
payload_lenpayload length that contains the attributes
cbcallback function that is called for each attribute
datapointer 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).

This function propagates the return value of the callback, which can be MNL_CB_ERROR, MNL_CB_OK or MNL_CB_STOP.

References mnl_attr_for_each_payload, MNL_CB_OK, and MNL_CB_STOP.

Referenced by nfct_payload_parse().

Here is the caller graph for this function:

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
nlhpointer to the netlink message
typenetlink attribute type that you want to add
lennetlink attribute payload length
datapointer 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.

References len, MNL_ALIGN, mnl_attr_get_payload(), mnl_nlmsg_get_payload_tail(), nlattr::nla_len, nlattr::nla_type, nlmsghdr::nlmsg_len, and type.

Referenced by mnl_attr_put_check(), mnl_attr_put_str(), mnl_attr_put_strz(), mnl_attr_put_u16(), mnl_attr_put_u32(), mnl_attr_put_u64(), mnl_attr_put_u8(), nfq_nlmsg_cfg_put_cmd(), nfq_nlmsg_cfg_put_params(), nfq_nlmsg_verdict_put(), nfq_nlmsg_verdict_put_pkt(), nft_rule_nlmsg_build_payload(), and nft_set_elem_nlmsg_build_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

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
nlhpointer to the netlink message
buflensize of buffer which stores the message
typenetlink attribute type that you want to add
lennetlink attribute payload length
datapointer 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. The function returns true if the attribute could be added to the message, otherwise false is returned.

References MNL_ALIGN, MNL_ATTR_HDRLEN, mnl_attr_put(), and nlmsghdr::nlmsg_len.

Referenced by mnl_attr_put_str_check(), mnl_attr_put_strz_check(), mnl_attr_put_u16_check(), mnl_attr_put_u32_check(), mnl_attr_put_u64_check(), and mnl_attr_put_u8_check().

Here is the call graph for this function:

Here is the caller graph for this function:

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
nlhpointer to the netlink message
typenetlink attribute type
datapointer 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.

References mnl_attr_put().

Here is the call graph for this function:

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
nlhpointer to the netlink message
buflensize of buffer which stores the message
typenetlink attribute type
datapointer 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. The function returns true if the attribute could be added to the message, otherwise false is returned. This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

References mnl_attr_put_check().

Here is the call graph for this function:

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
nlhpointer to the netlink message
typenetlink attribute type
datapointer 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.

References mnl_attr_put().

Referenced by nfacct_nlmsg_build_payload(), nfct_helper_nlmsg_build_payload(), nfct_timeout_nlmsg_build_payload(), nfexp_nlmsg_build(), nft_chain_nlmsg_build_payload(), nft_compatible_revision(), nft_rule_expr_build_payload(), nft_rule_nlmsg_build_payload(), nft_set_elem_nlmsg_build_payload(), nft_set_nlmsg_build_payload(), and nft_table_nlmsg_build_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

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
nlhpointer to the netlink message
buflensize of buffer which stores the message
typenetlink attribute type
datapointer 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. The function returns true if the attribute could be added to the message, otherwise false is returned.

References mnl_attr_put_check().

Here is the call graph for this function:

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
nlhpointer to the netlink message
typenetlink attribute type
data16-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.

References mnl_attr_put().

Referenced by nfct_helper_nlmsg_build_payload(), nfct_timeout_nlmsg_build_payload(), and nfexp_nlmsg_build().

Here is the call graph for this function:

Here is the caller graph for this function:

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
nlhpointer to the netlink message
buflensize of buffer which stores the message
typenetlink attribute type
data16-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. The function returns true if the attribute could be added to the message, otherwise false is returned. This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

References mnl_attr_put_check().

Here is the call graph for this function:

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
nlhpointer to the netlink message
typenetlink attribute type
data32-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.

References mnl_attr_put().

Referenced by nfacct_nlmsg_build_payload(), nfct_helper_nlmsg_build_payload(), nfct_timeout_nlmsg_build_payload(), nfexp_nlmsg_build(), nfq_nlmsg_cfg_put_qmaxlen(), nfq_nlmsg_verdict_put_mark(), nft_chain_nlmsg_build_payload(), nft_compatible_revision(), nft_rule_nlmsg_build_payload(), nft_set_elem_nlmsg_build_payload(), nft_set_nlmsg_build_payload(), and nft_table_nlmsg_build_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

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
nlhpointer to the netlink message
buflensize of buffer which stores the message
typenetlink attribute type
data32-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. The function returns true if the attribute could be added to the message, otherwise false is returned. This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

References mnl_attr_put_check().

Here is the call graph for this function:

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
nlhpointer to the netlink message
typenetlink attribute type
data64-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.

References mnl_attr_put().

Referenced by nfacct_nlmsg_build_payload(), nft_chain_nlmsg_build_payload(), and nft_rule_nlmsg_build_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

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
nlhpointer to the netlink message
buflensize of buffer which stores the message
typenetlink attribute type
data64-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. The function returns true if the attribute could be added to the message, otherwise false is returned. This function updates the length field of the Netlink message (nlmsg_len) by adding the size (header + payload) of the new attribute.

References mnl_attr_put_check().

Here is the call graph for this function:

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
nlhpointer to the netlink message
typenetlink attribute type
data8-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.

References mnl_attr_put().

Referenced by nfct_helper_nlmsg_build_payload(), and nfct_timeout_nlmsg_build_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

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
nlhpointer to the netlink message
buflensize of buffer which stores the message
typenetlink attribute type
data8-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. The function returns true if the attribute could be added to the message, otherwise false is returned.

References mnl_attr_put_check().

Here is the call graph for this function:

int mnl_attr_type_valid ( const struct nlattr attr,
uint16_t  max 
)

mnl_attr_type_valid - check if the attribute type is valid

Parameters
attrpointer to attribute to be checked
maxmaximum attribute type

This function allows to check if the attribute type is higher than the maximum supported type. If the attribute type is invalid, this function returns -1 and errno is explicitly set. On success, this function returns 1.

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.

References mnl_attr_get_type().

Here is the call graph for this function:

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

mnl_attr_validate - validate netlink attribute (simplified version)

Parameters
attrpointer to netlink attribute that we want to validate
typedata 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. This function returns -1 in case of error, and errno is explicitly set.

References MNL_TYPE_MAX, and type.

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
attrpointer to netlink attribute that we want to validate
typeattribute type (see enum mnl_attr_data_type)
exp_lenexpected attribute data size

This function allows to perform a more accurate validation for attributes whose size is variable. If the size of the attribute is not what we expect, this functions returns -1 and errno is explicitly set.

References MNL_TYPE_MAX.

int nla_attr_size ( int  payload)

Return size of attribute whithout padding.

  • payload Payload length of attribute.
1  <-------- nla_attr_size(payload) --------->
2 +------------------+- - -+- - - - - - - - - +- - -+
3 | Attribute Header | Pad | Payload | Pad |
4 +------------------+- - -+- - - - - - - - - +- - -+
Returns
Size of attribute in bytes without padding.

References NLA_HDRLEN, and payload.

Referenced by nla_padlen(), nla_reserve(), and nla_total_size().

Here is the caller graph for this function:

struct nlattr* nla_find ( struct nlattr head,
int  len,
int  attrtype 
)

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.

Returns
Pointer to attribute found or NULL.

References nla_for_each_attr, nla_type(), and NULL.

Referenced by nlmsg_find_attr().

Here is the call graph for this function:

Here is the caller graph for this function:

int nla_get_flag ( struct nlattr nla)

Return true if flag attribute is set.

  • nla Flag netlink attribute.
Returns
True if flag is set, otherwise false.
unsigned long nla_get_msecs ( struct nlattr nla)

Return payload of msecs attribute.

  • nla msecs netlink attribute
Returns
the number of milliseconds.

References nla_get_u64().

Here is the call graph for this function:

char* nla_get_string ( struct nlattr nla)

Return payload of string attribute.

  • nla String attribute.
Returns
Pointer to attribute payload.

References nla_data().

Referenced by nla_strdup().

Here is the call graph for this function:

Here is the caller graph for this function:

uint16_t nla_get_u16 ( struct nlattr nla)

Return payload of 16 bit integer attribute.

  • nla 16 bit integer attribute
Returns
Payload as 16 bit integer.

References nla_data().

Here is the call graph for this function:

uint32_t nla_get_u32 ( struct nlattr nla)

Return payload of 32 bit integer attribute.

  • nla 32 bit integer attribute.
Returns
Payload as 32 bit integer.

References nla_data().

Referenced by nfnl_nft_data_parse(), nfnlmsg_ct_parse(), nfnlmsg_log_msg_parse(), nfnlmsg_nft_chain_parse(), nfnlmsg_nft_set_parse(), nfnlmsg_queue_msg_parse(), and rtnl_route_parse().

Here is the call graph for this function:

Here is the caller graph for this function:

uint64_t nla_get_u64 ( struct nlattr nla)

Return payload of u64 attribute.

  • nla u64 netlink attribute
Returns
Payload as 64 bit integer.

References nla_memcpy().

Referenced by nfnlmsg_nft_chain_parse(), nfnlmsg_nft_rule_parse(), and nla_get_msecs().

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t nla_get_u8 ( struct nlattr nla)

Return value of 8 bit integer attribute.

  • nla 8 bit integer attribute
Returns
Payload as 8 bit integer.

References nla_data().

Here is the call graph for this function:

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.
See also
memcmp(3)
Returns
An integer less than, equal to, or greater than zero.

References nla_data(), nla_len(), and size.

Here is the call graph for this function:

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.

Returns
The number of bytes copied to dest.

References min_t, nla_data(), and nla_len().

Referenced by nla_get_u64(), and rtnl_route_parse().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Returns
0

References nlattr::nla_len, nlmsg_tail(), and nl_msg::nm_nlh.

Referenced by nfnl_nft_data_put(), nfnl_nft_expr_build_message(), rtnl_link_build_change_request(), and rtnl_route_build_msg().

Here is the call graph for this function:

Here is the caller graph for this function:

struct nlattr* nla_nest_start ( struct nl_msg msg,
int  attrtype 
)

Start a new level of nested attributes.

  • msg Netlink message.
  • attrtype Attribute type of container.
Returns
Pointer to container attribute.

References nla_put(), nlmsg_tail(), nl_msg::nm_nlh, and NULL.

Referenced by nfnl_nft_data_put(), nfnl_nft_expr_build_message(), rtnl_link_build_change_request(), and rtnl_route_build_msg().

Here is the call graph for this function:

Here is the caller graph for this function:

struct nlattr* nla_next ( const struct nlattr nla,
int *  remaining 
)

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.

Returns
Pointer to next attribute.

References NLA_ALIGN, and nlattr::nla_len.

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().

Returns
True if the attribute can be accessed safely, false otherwise.

References nlattr::nla_len.

int nla_padlen ( int  payload)

Return length of padding at the tail of the attribute.

  • payload Payload length of attribute.
1 +------------------+- - -+- - - - - - - - - +- - -+
2 | Attribute Header | Pad | Payload | Pad |
3 +------------------+- - -+- - - - - - - - - +- - -+
4  <--->
Returns
Length of padding in bytes.

References nla_attr_size(), and nla_total_size().

Referenced by nla_reserve().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

See also
nla_validate
Returns
0 on success or a negative error code.

References nla_for_each_attr, nla_type(), and type.

Referenced by nla_parse_nested().

Here is the call graph for this function:

Here is the caller graph for this function:

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().

See also
nla_parse
Returns
0 on success or a negative error code.

References nla_data(), nla_len(), and nla_parse().

Here is the call graph for this function:

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.

See also
nla_reserve
Returns
0 on success or a negative error code.

References NL_DBG, nla_data(), nla_reserve(), NLE_NOMEM, nlmsg_data(), and nl_msg::nm_nlh.

Referenced by nfnl_log_build_create_request(), nfnl_nft_data_put(), nfnl_queue_msg_build_verdict(), 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().

Here is the call graph for this function:

Here is the caller graph for this function:

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.
See also
nla_put
Returns
0 on success or a negative error code.

References nl_addr_get_binary_addr(), nl_addr_get_len(), and nla_put().

Referenced by main().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

See also
nla_put
Returns
0 on success or a negative error code.

References nl_data_get(), nl_data_get_size(), and nla_put().

Here is the call graph for this function:

int nla_put_flag ( struct nl_msg msg,
int  attrtype 
)

Add flag netlink attribute to netlink message.

  • msg Netlink message.
  • attrtype Attribute type.
See also
nla_put
Returns
0 on success or a negative error code.

References nla_put(), and NULL.

Here is the call graph for this function:

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

References nla_put_u64().

Here is the call graph for this function:

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.

See also
nla_put
Returns
0 on success or a negative error code.

References nla_put(), nlmsg_data(), nlmsg_len(), and nl_msg::nm_nlh.

Referenced by rtnl_neightbl_build_change_request().

Here is the call graph for this function:

Here is the caller graph for this function:

int nla_put_string ( struct nl_msg msg,
int  attrtype,
const char *  str 
)

Add string attribute to netlink message.

  • msg Netlink message.
  • attrtype Attribute type.
  • str NUL terminated string.
See also
nla_put
Returns
0 on success or a negative error code.

References nla_put().

Referenced by nfnl_nft_data_put(), and nfnl_nft_expr_build_message().

Here is the call graph for this function:

Here is the caller graph for this function:

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.
See also
nla_put
Returns
0 on success or a negative error code.

References nla_put().

Here is the call graph for this function:

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.
See also
nla_put
Returns
0 on success or a negative error code.

References nla_put().

Referenced by nfnl_nft_data_put(), and nfnl_queue_msg_build_verdict().

Here is the call graph for this function:

Here is the caller graph for this function:

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.
See also
nla_put
Returns
0 on success or a negative error code.

References nla_put().

Referenced by nla_put_msecs().

Here is the call graph for this function:

Here is the caller graph for this function:

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.
See also
nla_put
Returns
0 on success or a negative error code.

References nla_put().

Here is the call graph for this function:

struct nlattr* nla_reserve ( struct nl_msg msg,
int  attrtype,
int  attrlen 
)

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.

Returns
Pointer to start of attribute or NULL on failure.

References NL_DBG, nla_attr_size(), nlattr::nla_len, nla_padlen(), nla_total_size(), nlattr::nla_type, NLMSG_ALIGN, nlmsg_data(), nlmsghdr::nlmsg_len, nlmsg_tail(), nl_msg::nm_nlh, nl_msg::nm_size, and NULL.

Referenced by nla_put().

Here is the call graph for this function:

Here is the caller graph for this function:

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.
See also
strcmp(3)
Returns
An integer less than, equal to, or greater than zero.

References len, nla_data(), and nla_len().

Here is the call graph for this function:

char* nla_strdup ( struct nlattr nla)

References nla_get_string().

Referenced by nfnl_nft_data_parse().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Returns
The length of string attribute without the terminating NUL.

References len, nla_data(), and nla_len().

Here is the call graph for this function:

int nla_total_size ( int  payload)

Return size of attribute including padding.

  • payload Payload length of attribute.
1  <----------- nla_total_size(payload) ----------->
2 +------------------+- - -+- - - - - - - - - +- - -+
3 | Attribute Header | Pad | Payload | Pad |
4 +------------------+- - -+- - - - - - - - - +- - -+
Returns
Size of attribute in bytes.

References NLA_ALIGN, and nla_attr_size().

Referenced by nla_padlen(), nla_reserve(), and rtnl_ematch_parse().

Here is the call graph for this function:

Here is the caller graph for this function:

int nla_type ( const struct nlattr nla)

Return type of the attribute.

  • nla Attribute.
Returns
Type of attribute.

References nlattr::nla_type, and NLA_TYPE_MASK.

Referenced by nfnlmsg_nft_rule_parse(), nla_find(), and nla_parse().

Here is the caller graph for this function:

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.

Returns
0 on success or a negative error code.

References nla_for_each_attr.

Referenced by genlmsg_validate(), and nlmsg_validate().

Here is the caller graph for this function: