Logo Search packages:      
Sourcecode: libnl version File versions  Download package

Attributes
[Core Netlink API]


Detailed Description

Netlink Attributes Construction/Parsing Interface
0) Introduction
Netlink attributes are chained together following each other:
    <------- nla_total_size(payload) ------->
    <---- nla_attr_size(payload) ----->
   +----------+- - -+- - - - - - - - - +- - -+-------- - -
   |  Header  | Pad |     Payload      | Pad |  Header
   +----------+- - -+- - - - - - - - - +- - -+-------- - -
                     <- nla_len(nla) ->      ^
   nla_data(nla)----^                        |
   nla_next(nla)-----------------------------'
The attribute header and payload must be aligned properly:
  <------- NLA_HDRLEN ------> <-- NLA_ALIGN(payload)-->
 +---------------------+- - -+- - - - - - - - - -+- - -+
 |        Header       | Pad |     Payload       | Pad |
 |   (struct nlattr)   | ing |                   | ing |
 +---------------------+- - -+- - - - - - - - - -+- - -+
  <-------------- nlattr->nla_len -------------->
Nested TLVs:
Nested TLVs are an array of TLVs nested into another TLV. This can be useful to allow subsystems to have their own formatting rules without the need to make the underlying layer be aware of it. It can also be useful to transfer arrays, lists and flattened trees.
  <-------------------- NLA_ALIGN(...) ------------------->
 +---------------+- - - - - - - - - - - - - - - - - -+- - -+
 |               |+---------+---------+- - -+-------+|     |
 |  TLV Header   ||  TLV 1  |  TLV 2  |     | TLV n || Pad |
 |               |+---------+---------+- - -+-------+|     |
 +---------------+- - - - - - - - - - - - - - - - - -+- - -+
                  <--------- nla_data(nla) --------->
1) Constructing a message with attributes
 int param1 = 10;
 char *param2 = "parameter text";
 struct nlmsghdr hdr = {
      .nlmsg_type = MY_ACTION,
 };
 struct nl_msg *m = nlmsg_build(&hdr);
 nla_put_u32(m, 1, param1);
 nla_put_string(m, 2, param2);
 
 nl_send_auto_complete(handle, nl_msg_get(m));
 nlmsg_free(m);
2) Constructing nested attributes
 struct nl_msg * nested_config(void)
 {
      int a = 5, int b = 10;
      struct nl_msg *n = nlmsg_build(NULL);
      nla_put_u32(n, 10, a);
      nla_put_u32(n, 20, b);
      return n;
 }

 ...
 struct nl_msg *m = nlmsg_build(&hdr);
 struct nl_msg *nest = nested_config();
 nla_put_nested(m, 1, nest);

 nl_send_auto_complete(handle, nl_msg_get(m));
 nlmsg_free(nest);
 nlmsg_free(m);


Classes

struct  nla_policy
struct  nlattr

Validation Policy Types

enum  {
  NLA_UNSPEC, NLA_U8, NLA_U16, NLA_U32,
  NLA_U64, NLA_STRING, NLA_FLAG, NLA_MSECS,
  NLA_NESTED, __NLA_TYPE_MAX
}
#define NLA_TYPE_MAX   (__NLA_TYPE_MAX - 1)

Attribute Parsing

static uint16_t nla_attr_minlen [NLA_TYPE_MAX+1]
struct nlattrnla_find (struct nlattr *head, int len, int attrtype)
struct nlattrnla_next (const struct nlattr *nla, int *remaining)
int nla_ok (const struct nlattr *nla, int remaining)
int nla_parse (struct nlattr *tb[], int maxtype, struct nlattr *head, int len, struct nla_policy *policy)
int nla_parse_nested (struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
int nla_validate (struct nlattr *head, int len, int maxtype, struct nla_policy *policy)
static int validate_nla (struct nlattr *nla, int maxtype, struct nla_policy *policy)

Size Calculations

int nla_attr_size (int payload)
int nla_padlen (int payload)
int nla_total_size (int payload)

Payload Access

void * nla_data (const struct nlattr *nla)
int nla_len (const struct nlattr *nla)

Attribute Reading

struct nl_addr * nla_get_addr (struct nlattr *nla, int family)
struct nl_data * nla_get_data (struct nlattr *nla)
int nla_get_flag (struct nlattr *nla)
unsigned long nla_get_msecs (struct nlattr *nla)
uint16_t nla_get_u16 (struct nlattr *nla)
uint32_t nla_get_u32 (struct nlattr *nla)
uint64_t nla_get_u64 (struct nlattr *nla)
uint8_t nla_get_u8 (struct nlattr *nla)

Utilities

int nla_memcmp (const struct nlattr *nla, const void *data, size_t size)
int nla_memcpy (void *dest, struct nlattr *src, int count)
int nla_strcmp (const struct nlattr *nla, const char *str)
size_t nla_strlcpy (char *dst, const struct nlattr *nla, size_t dstsize)

Attribute Nesting

int nla_nest_end (struct nl_msg *n, struct nlattr *start)
struct nlattrnla_nest_start (struct nl_msg *n, int attrtype)

Attribute Construction

int nla_put (struct nl_msg *n, int attrtype, int attrlen, const void *data)
int nla_put_addr (struct nl_msg *n, int attrtype, struct nl_addr *addr)
int nla_put_data (struct nl_msg *n, int attrtype, struct nl_data *data)
int nla_put_flag (struct nl_msg *n, int attrtype)
int nla_put_msecs (struct nl_msg *n, int attrtype, unsigned long msecs)
int nla_put_nested (struct nl_msg *n, int attrtype, struct nl_msg *nested)
int nla_put_string (struct nl_msg *n, int attrtype, const char *str)
int nla_put_u16 (struct nl_msg *n, int attrtype, uint16_t value)
int nla_put_u32 (struct nl_msg *n, int attrtype, uint32_t value)
int nla_put_u64 (struct nl_msg *n, int attrtype, uint64_t value)
int nla_put_u8 (struct nl_msg *n, int attrtype, uint8_t value)
struct nlattrnla_reserve (struct nl_msg *n, int attrtype, int attrlen)

Iterators

#define nla_for_each_attr(pos, head, len, rem)

Attribute Construction (Exception Based)

All these functions jump to nla_put_failure in case of a failure instead of returning an error code.

#define NLA_PUT(n, attrtype, attrlen, data)
#define NLA_PUT_ADDR(n, attrtype, addr)
#define NLA_PUT_FLAG(n, attrtype)   NLA_PUT(n, attrtype, 0, NULL)
#define NLA_PUT_MSECS(n, attrtype, msecs)   NLA_PUT_U64(n, attrtype, msecs)
#define NLA_PUT_STRING(n, attrtype, value)   NLA_PUT(n, attrtype, strlen(value) + 1, value)
#define NLA_PUT_TYPE(n, type, attrtype, value)
#define NLA_PUT_U16(n, attrtype, value)   NLA_PUT_TYPE(n, uint16_t, attrtype, value)
#define NLA_PUT_U32(n, attrtype, value)   NLA_PUT_TYPE(n, uint32_t, attrtype, value)
#define NLA_PUT_U64(n, attrtype, value)   NLA_PUT_TYPE(n, uint64_t, attrtype, value)
#define NLA_PUT_U8(n, attrtype, value)   NLA_PUT_TYPE(n, uint8_t, attrtype, value)


Generated by  Doxygen 1.6.0   Back to index