libnl  1.1.4
Data Structures

Netlink Attributes Construction/Parsing Interface. More...

Data Structures

struct  nla_policy
 attribute validation policy More...
 

Size Calculations

int nla_attr_size (int payload)
 length of attribute not including padding More...
 
int nla_total_size (int payload)
 total length of attribute including padding More...
 
int nla_padlen (int payload)
 length of padding at the tail of the attribute More...
 

Payload Access

int nla_type (const struct nlattr *nla)
 attribute type More...
 
void * nla_data (const struct nlattr *nla)
 head of payload More...
 
int nla_len (const struct nlattr *nla)
 length of payload More...
 

Attribute Parsing

int nla_ok (const struct nlattr *nla, int remaining)
 check if the netlink attribute fits into the remaining bytes More...
 
struct nlattr * nla_next (const struct nlattr *nla, int *remaining)
 next netlink attribte in attribute stream More...
 
int nla_parse (struct nlattr *tb[], int maxtype, struct nlattr *head, int len, struct nla_policy *policy)
 Parse a stream of attributes into a tb buffer. More...
 
int nla_parse_nested (struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
 parse nested attributes More...
 
int nla_validate (struct nlattr *head, int len, int maxtype, struct nla_policy *policy)
 Validate a stream of attributes. More...
 
struct nlattr * nla_find (struct nlattr *head, int len, int attrtype)
 Find a specific attribute in a stream of attributes. More...
 

Utilities

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

Attribute Construction

struct nlattr * nla_reserve (struct nl_msg *n, int attrtype, int attrlen)
 reserve room for attribute on the skb More...
 
int nla_put (struct nl_msg *n, int attrtype, int attrlen, const void *data)
 Add a netlink attribute to a netlink message. More...
 
int nla_put_nested (struct nl_msg *n, int attrtype, struct nl_msg *nested)
 Add a nested netlink attribute to a netlink message. More...
 
int nla_put_u8 (struct nl_msg *n, int attrtype, uint8_t value)
 Add a u16 netlink attribute to a netlink message. More...
 
int nla_put_u16 (struct nl_msg *n, int attrtype, uint16_t value)
 Add a u16 netlink attribute to a netlink message. More...
 
int nla_put_u32 (struct nl_msg *n, int attrtype, uint32_t value)
 Add a u32 netlink attribute to a netlink message. More...
 
int nla_put_u64 (struct nl_msg *n, int attrtype, uint64_t value)
 Add a u64 netlink attribute to a netlink message. More...
 
int nla_put_string (struct nl_msg *n, int attrtype, const char *str)
 Add a string netlink attribute to a netlink message. More...
 
int nla_put_flag (struct nl_msg *n, int attrtype)
 Add a flag netlink attribute to a netlink message. More...
 
int nla_put_msecs (struct nl_msg *n, int attrtype, unsigned long msecs)
 Add a msecs netlink attribute to a netlink message. More...
 
int nla_put_data (struct nl_msg *n, int attrtype, struct nl_data *data)
 Add an abstract data netlink attribute to a netlink message. More...
 
int nla_put_addr (struct nl_msg *n, int attrtype, struct nl_addr *addr)
 Add an abstract address netlink attribute to a netlink message. More...
 

Attribute Nesting

struct nlattr * nla_nest_start (struct nl_msg *n, int attrtype)
 Start a new level of nested attributes. More...
 
int nla_nest_end (struct nl_msg *n, struct nlattr *start)
 Finalize nesting of attributes. More...
 

Attribute Reading

uint32_t nla_get_u32 (struct nlattr *nla)
 Return payload of u32 attribute. More...
 
uint16_t nla_get_u16 (struct nlattr *nla)
 Return payload of u16 attribute. More...
 
uint8_t nla_get_u8 (struct nlattr *nla)
 Return payload of u8 attribute. More...
 
uint64_t nla_get_u64 (struct nlattr *nla)
 Return payload of u64 attribute. More...
 
char * nla_get_string (struct nlattr *nla)
 return payload of string attribute More...
 
int nla_get_flag (struct nlattr *nla)
 Return payload of flag attribute. More...
 
unsigned long nla_get_msecs (struct nlattr *nla)
 Return payload of msecs attribute. More...
 
struct nl_addr * nla_get_addr (struct nlattr *nla, int family)
 Return payload of address attribute. More...
 
struct nl_data * nla_get_data (struct nlattr *nla)
 Return payload of abstract data attribute. More...
 

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
}
 Standard attribute types to specify validation policy. More...
 
#define NLA_TYPE_MAX   (__NLA_TYPE_MAX - 1)
 Maximum netlink validation policy type.
 

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)
 Add a netlink attribute to a netlink message. More...
 
#define NLA_PUT_TYPE(n, type, attrtype, value)
 Add a basic netlink attribute to a netlink message. More...
 
#define NLA_PUT_U8(n, attrtype, value)   NLA_PUT_TYPE(n, uint8_t, attrtype, value)
 Add a u8 netlink attribute to a netlink message. More...
 
#define NLA_PUT_U16(n, attrtype, value)   NLA_PUT_TYPE(n, uint16_t, attrtype, value)
 Add a u16 netlink attribute to a netlink message. More...
 
#define NLA_PUT_U32(n, attrtype, value)   NLA_PUT_TYPE(n, uint32_t, attrtype, value)
 Add a u32 netlink attribute to a netlink message. More...
 
#define NLA_PUT_U64(n, attrtype, value)   NLA_PUT_TYPE(n, uint64_t, attrtype, value)
 Add a u64 netlink attribute to a netlink message. More...
 
#define NLA_PUT_STRING(n, attrtype, value)   NLA_PUT(n, attrtype, strlen(value) + 1, value)
 Add a character string netlink attribute to a netlink message. More...
 
#define NLA_PUT_FLAG(n, attrtype)   NLA_PUT(n, attrtype, 0, NULL)
 Add a flag netlink attribute to a netlink message. More...
 
#define NLA_PUT_MSECS(n, attrtype, msecs)   NLA_PUT_U64(n, attrtype, msecs)
 Add a msecs netlink attribute to a netlink message. More...
 
#define NLA_PUT_ADDR(n, attrtype, addr)
 Add a address attribute to a 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...
 

Detailed Description

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 nl_msg *msg = nlmsg_alloc();
nla_put_u32(msg, 1, param1);
nla_put_string(msg, 2, param2);
nl_send_auto_complete(handle, nl_msg_get(msg));
2) Constructing nested attributes
struct nl_msg * nested_config(void)
{
int a = 5, int b = 10;
struct nl_msg *n = nlmsg_alloc();
nla_put_u32(n, 10, a);
nla_put_u32(n, 20, b);
return n;
}
...
struct nl_msg *m = nlmsg_alloc();
struct nl_msg *nest = nested_config();
nla_put_nested(m, 1, nest);
nl_send_auto_complete(handle, nl_msg_get(m));
nlmsg_free(nest);

Macro Definition Documentation

◆ NLA_PUT

#define NLA_PUT (   n,
  attrtype,
  attrlen,
  data 
)
Value:
do { \
if (nla_put(n, attrtype, attrlen, data) < 0) \
goto nla_put_failure; \
} while(0)
int nla_put(struct nl_msg *, int, int, const void *)
Add a netlink attribute to a netlink message.
Definition: attr.c:511
Parameters
nnetlink message
attrtypeattribute type
attrlenlength of attribute payload
datahead of attribute payload

Definition at line 157 of file attr.h.

◆ NLA_PUT_TYPE

#define NLA_PUT_TYPE (   n,
  type,
  attrtype,
  value 
)
Value:
do { \
type __tmp = value; \
NLA_PUT(n, attrtype, sizeof(type), &__tmp); \
} while(0)
Parameters
nnetlink message
typeatomic type
attrtypeattribute type
valuehead of attribute payload

Definition at line 171 of file attr.h.

◆ nla_for_each_attr

#define nla_for_each_attr (   pos,
  head,
  len,
  rem 
)
Value:
for (pos = head, rem = len; \
nla_ok(pos, rem); \
pos = nla_next(pos, &(rem)))
struct nlattr * nla_next(const struct nlattr *, int *)
next netlink attribte in attribute stream
Definition: attr.c:192
Parameters
posloop counter, set to current attribute
headhead of attribute stream
lenlength of attribute stream
reminitialized to len, holds bytes currently remaining in stream

Definition at line 264 of file attr.h.

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

◆ nla_for_each_nested

#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)))
struct nlattr * nla_next(const struct nlattr *, int *)
next netlink attribte in attribute stream
Definition: attr.c:192
void * nla_data(const struct nlattr *)
head of payload
Definition: attr.c:151
int nla_len(const struct nlattr *)
length of payload
Definition: attr.c:160
Parameters
posloop counter, set to current attribute
nlaattribute containing the nested attributes
reminitialized to len, holds bytes currently remaining in stream

Definition at line 276 of file attr.h.

◆ NLA_PUT_U8

#define NLA_PUT_U8 (   n,
  attrtype,
  value 
)    NLA_PUT_TYPE(n, uint8_t, attrtype, value)
Parameters
nnetlink message
attrtypeattribute type
valuenumeric value

Definition at line 183 of file attr.h.

Referenced by rtnl_link_build_change_request().

◆ NLA_PUT_U16

#define NLA_PUT_U16 (   n,
  attrtype,
  value 
)    NLA_PUT_TYPE(n, uint16_t, attrtype, value)
Parameters
nnetlink message
attrtypeattribute type
valuenumeric value

Definition at line 192 of file attr.h.

◆ NLA_PUT_U32

#define NLA_PUT_U32 (   n,
  attrtype,
  value 
)    NLA_PUT_TYPE(n, uint32_t, attrtype, value)
Parameters
nnetlink message
attrtypeattribute type
valuenumeric value

Definition at line 201 of file attr.h.

Referenced by rtnl_link_build_change_request().

◆ NLA_PUT_U64

#define NLA_PUT_U64 (   n,
  attrtype,
  value 
)    NLA_PUT_TYPE(n, uint64_t, attrtype, value)
Parameters
nnetlink message
attrtypeattribute type
valuenumeric value

Definition at line 210 of file attr.h.

◆ NLA_PUT_STRING

#define NLA_PUT_STRING (   n,
  attrtype,
  value 
)    NLA_PUT(n, attrtype, strlen(value) + 1, value)
Parameters
nnetlink message
attrtypeattribute type
valuecharacter string

Definition at line 219 of file attr.h.

Referenced by rtnl_link_build_change_request().

◆ NLA_PUT_FLAG

#define NLA_PUT_FLAG (   n,
  attrtype 
)    NLA_PUT(n, attrtype, 0, NULL)
Parameters
nnetlink message
attrtypeattribute type

Definition at line 227 of file attr.h.

◆ NLA_PUT_MSECS

#define NLA_PUT_MSECS (   n,
  attrtype,
  msecs 
)    NLA_PUT_U64(n, attrtype, msecs)
Parameters
nnetlink message
attrtypeattribute type
msecsnumeric value in micro seconds

Definition at line 236 of file attr.h.

◆ NLA_PUT_ADDR

#define NLA_PUT_ADDR (   n,
  attrtype,
  addr 
)
Value:
NLA_PUT(n, attrtype, nl_addr_get_len(addr), \
#define NLA_PUT(n, attrtype, attrlen, data)
Add a netlink attribute to a netlink message.
Definition: attr.h:157
unsigned int nl_addr_get_len(struct nl_addr *addr)
Get length of binary address of abstract address object.
Definition: addr.c:765
void * nl_addr_get_binary_addr(struct nl_addr *addr)
Get binary address of abstract address object.
Definition: addr.c:756
Parameters
nnetlink message
attrtypeattribute type
addrabstract address object

Definition at line 245 of file attr.h.

Referenced by rtnl_link_build_change_request().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
NLA_UNSPEC 

Unspecified type.

NLA_U8 

8bit integer

NLA_U16 

16bit integer

NLA_U32 

32bit integer

NLA_U64 

64bit integer

NLA_STRING 

character string

NLA_FLAG 

flag

NLA_MSECS 

micro seconds (64bit)

NLA_NESTED 

nested attributes

Definition at line 35 of file attr.h.

35  {
36  NLA_UNSPEC, /**< Unspecified type */
37  NLA_U8, /**< 8bit integer */
38  NLA_U16, /**< 16bit integer */
39  NLA_U32, /**< 32bit integer */
40  NLA_U64, /**< 64bit integer */
41  NLA_STRING, /**< character string */
42  NLA_FLAG, /**< flag */
43  NLA_MSECS, /**< micro seconds (64bit) */
44  NLA_NESTED, /**< nested attributes */
45  __NLA_TYPE_MAX,
46 };
32bit integer
Definition: attr.h:39
Unspecified type.
Definition: attr.h:36
8bit integer
Definition: attr.h:37
nested attributes
Definition: attr.h:44
character string
Definition: attr.h:41
flag
Definition: attr.h:42
micro seconds (64bit)
Definition: attr.h:43
64bit integer
Definition: attr.h:40
16bit integer
Definition: attr.h:38

Function Documentation

◆ nla_attr_size()

int nla_attr_size ( int  payload)
Parameters
payloadlength of payload

Definition at line 108 of file attr.c.

Referenced by nla_padlen(), and nla_total_size().

109 {
110  return NLA_HDRLEN + payload;
111 }

◆ nla_total_size()

int nla_total_size ( int  payload)
Parameters
payloadlength of payload

Definition at line 117 of file attr.c.

References nla_attr_size().

Referenced by nla_padlen(), and nla_reserve().

118 {
119  return NLA_ALIGN(nla_attr_size(payload));
120 }
int nla_attr_size(int payload)
length of attribute not including padding
Definition: attr.c:108

◆ nla_padlen()

int nla_padlen ( int  payload)
Parameters
payloadlength of payload

Definition at line 126 of file attr.c.

References nla_attr_size(), and nla_total_size().

127 {
128  return nla_total_size(payload) - nla_attr_size(payload);
129 }
int nla_total_size(int payload)
total length of attribute including padding
Definition: attr.c:117
int nla_attr_size(int payload)
length of attribute not including padding
Definition: attr.c:108

◆ nla_type()

int nla_type ( const struct nlattr *  nla)
Parameters
nlanetlink attribute

Definition at line 142 of file attr.c.

Referenced by nla_find(), and nla_parse().

143 {
144  return nla->nla_type & NLA_TYPE_MASK;
145 }

◆ nla_data()

void* nla_data ( const struct nlattr *  nla)
Parameters
nlanetlink attribute

Definition at line 151 of file attr.c.

Referenced by nla_get_addr(), nla_get_data(), nla_get_string(), nla_get_u16(), nla_get_u32(), nla_get_u8(), nla_memcmp(), nla_memcpy(), nla_parse_nested(), nla_put(), nla_strcmp(), and nla_strlcpy().

152 {
153  return (char *) nla + NLA_HDRLEN;
154 }

◆ nla_len()

int nla_len ( const struct nlattr *  nla)
Parameters
nlanetlink attribute

Definition at line 160 of file attr.c.

Referenced by nla_get_addr(), nla_get_data(), nla_memcmp(), nla_memcpy(), nla_parse_nested(), nla_strcmp(), and nla_strlcpy().

161 {
162  return nla->nla_len - NLA_HDRLEN;
163 }

◆ nla_ok()

int nla_ok ( const struct nlattr *  nla,
int  remaining 
)
Parameters
nlanetlink attribute
remainingnumber of bytes remaining in attribute stream

Definition at line 177 of file attr.c.

178 {
179  return remaining >= sizeof(*nla) &&
180  nla->nla_len >= sizeof(*nla) &&
181  nla->nla_len <= remaining;
182 }

◆ nla_next()

struct nlattr* nla_next ( const struct nlattr *  nla,
int *  remaining 
)
Parameters
nlanetlink attribute
remainingnumber of bytes remaining in attribute stream
Returns
the next netlink attribute in the attribute stream and decrements remaining by the size of the current attribute.

Definition at line 192 of file attr.c.

193 {
194  int totlen = NLA_ALIGN(nla->nla_len);
195 
196  *remaining -= totlen;
197  return (struct nlattr *) ((char *) nla + totlen);
198 }

◆ nla_parse()

int nla_parse ( struct nlattr *  tb[],
int  maxtype,
struct nlattr *  head,
int  len,
struct nla_policy policy 
)
Parameters
tbdestination array with maxtype+1 elements
maxtypemaximum attribute type to be expected
headhead of attribute stream
lenlength of attribute stream
policyvalidation policy

Parses a stream of attributes and stores a pointer to each attribute in the tb array accessable via the attribute type. Attributes with a type exceeding maxtype will be silently ignored for backwards compatibility reasons. policy may be set to NULL if no validation is required.

Returns
0 on success or a negative error code.

Definition at line 262 of file attr.c.

References nla_for_each_attr, and nla_type().

Referenced by nla_parse_nested().

264 {
265  struct nlattr *nla;
266  int rem, err;
267 
268  memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
269 
270  nla_for_each_attr(nla, head, len, rem) {
271  int type = nla_type(nla);
272 
273  /* Padding attributes */
274  if (type == 0)
275  continue;
276 
277  if (type <= maxtype) {
278  if (policy) {
279  err = validate_nla(nla, maxtype, policy);
280  if (err < 0)
281  goto errout;
282  }
283 
284  tb[type] = nla;
285  }
286  }
287 
288  if (rem > 0)
289  fprintf(stderr, "netlink: %d bytes leftover after parsing "
290  "attributes.\n", rem);
291 
292  err = 0;
293 errout:
294  return err;
295 }
int nla_type(const struct nlattr *nla)
attribute type
Definition: attr.c:142
#define nla_for_each_attr(pos, head, len, rem)
iterate over a stream of attributes
Definition: attr.h:264

◆ nla_parse_nested()

int nla_parse_nested ( struct nlattr *  tb[],
int  maxtype,
struct nlattr *  nla,
struct nla_policy policy 
)
Parameters
tbdestination array with maxtype+1 elements
maxtypemaximum attribute type to be expected
nlaattribute containing the nested attributes
policyvalidation policy
See also
nla_parse()

Definition at line 307 of file attr.c.

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

309 {
310  return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
311 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151
int nla_len(const struct nlattr *nla)
length of payload
Definition: attr.c:160
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, struct nla_policy *policy)
Parse a stream of attributes into a tb buffer.
Definition: attr.c:262

◆ nla_validate()

int nla_validate ( struct nlattr *  head,
int  len,
int  maxtype,
struct nla_policy policy 
)
Parameters
headhead of attribute stream
lenlength of attribute stream
maxtypemaximum attribute type to be expected
policyvalidation policy

Validates all attributes in the specified attribute stream against the specified policy. Attributes with a type exceeding maxtype will be ignored. See documenation of struct nla_policy for more details.

Returns
0 on success or a negative error code.

Definition at line 327 of file attr.c.

References nla_for_each_attr.

329 {
330  struct nlattr *nla;
331  int rem, err;
332 
333  nla_for_each_attr(nla, head, len, rem) {
334  err = validate_nla(nla, maxtype, policy);
335  if (err < 0)
336  goto errout;
337  }
338 
339  err = 0;
340 errout:
341  return err;
342 }
#define nla_for_each_attr(pos, head, len, rem)
iterate over a stream of attributes
Definition: attr.h:264

◆ nla_find()

struct nlattr* nla_find ( struct nlattr *  head,
int  len,
int  attrtype 
)
Parameters
headhead of attribute stream
lenlength of attribute stream
attrtypetype of attribute to look for
Returns
the first attribute in the stream matching the specified type.

Definition at line 352 of file attr.c.

References nla_for_each_attr, and nla_type().

Referenced by nlmsg_find_attr().

353 {
354  struct nlattr *nla;
355  int rem;
356 
357  nla_for_each_attr(nla, head, len, rem)
358  if (nla_type(nla) == attrtype)
359  return nla;
360 
361  return NULL;
362 }
int nla_type(const struct nlattr *nla)
attribute type
Definition: attr.c:142
#define nla_for_each_attr(pos, head, len, rem)
iterate over a stream of attributes
Definition: attr.h:264

◆ nla_memcpy()

int nla_memcpy ( void *  dest,
struct nlattr *  src,
int  count 
)
Parameters
destwhere to copy to memcpy
srcnetlink attribute to copy from
countsize of the destination area

Note: The number of bytes copied is limited by the length of attribute's payload. memcpy

Returns
the number of bytes copied.

Definition at line 382 of file attr.c.

References nla_data(), and nla_len().

Referenced by nla_get_u64().

383 {
384  int minlen;
385 
386  if (!src)
387  return 0;
388 
389  minlen = min_t(int, count, nla_len(src));
390  memcpy(dest, nla_data(src), minlen);
391 
392  return minlen;
393 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151
int nla_len(const struct nlattr *nla)
length of payload
Definition: attr.c:160

◆ nla_strlcpy()

size_t nla_strlcpy ( char *  dst,
const struct nlattr *  nla,
size_t  dstsize 
)
Parameters
dstwhere to copy the string to
nlaattribute to copy the string from
dstsizesize of destination buffer

Copies at most dstsize - 1 bytes into 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 the source buffer.

Definition at line 407 of file attr.c.

References nla_data(), and nla_len().

408 {
409  size_t srclen = nla_len(nla);
410  char *src = nla_data(nla);
411 
412  if (srclen > 0 && src[srclen - 1] == '\0')
413  srclen--;
414 
415  if (dstsize > 0) {
416  size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
417 
418  memset(dst, 0, dstsize);
419  memcpy(dst, src, len);
420  }
421 
422  return srclen;
423 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151
int nla_len(const struct nlattr *nla)
length of payload
Definition: attr.c:160

◆ nla_memcmp()

int nla_memcmp ( const struct nlattr *  nla,
const void *  data,
size_t  size 
)
Parameters
nlanetlink attribute
datamemory area
sizesize of memory area

Definition at line 431 of file attr.c.

References nla_data(), and nla_len().

433 {
434  int d = nla_len(nla) - size;
435 
436  if (d == 0)
437  d = memcmp(nla_data(nla), data, size);
438 
439  return d;
440 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151
int nla_len(const struct nlattr *nla)
length of payload
Definition: attr.c:160

◆ nla_strcmp()

int nla_strcmp ( const struct nlattr *  nla,
const char *  str 
)
Parameters
nlanetlink string attribute
stranother string

Definition at line 447 of file attr.c.

References nla_data(), and nla_len().

448 {
449  int len = strlen(str) + 1;
450  int d = nla_len(nla) - len;
451 
452  if (d == 0)
453  d = memcmp(nla_data(nla), str, len);
454 
455  return d;
456 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151
int nla_len(const struct nlattr *nla)
length of payload
Definition: attr.c:160

◆ nla_reserve()

struct nlattr* nla_reserve ( struct nl_msg *  n,
int  attrtype,
int  attrlen 
)
Parameters
nnetlink message
attrtypeattribute type
attrlenlength of attribute payload

Adds a netlink attribute header to a netlink message and reserves room for the payload but does not copy it.

Definition at line 474 of file attr.c.

References nla_total_size().

Referenced by nla_put().

475 {
476  struct nlattr *nla;
477  int tlen;
478 
479  tlen = NLMSG_ALIGN(n->nm_nlh->nlmsg_len) + nla_total_size(attrlen);
480 
481  if (tlen > n->nm_size) {
482  nl_errno(ENOBUFS);
483  return NULL;
484  }
485 
486  nla = (struct nlattr *) nlmsg_tail(n->nm_nlh);
487  nla->nla_type = attrtype;
488  nla->nla_len = nla_attr_size(attrlen);
489 
490  memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
491  n->nm_nlh->nlmsg_len = tlen;
492 
493  NL_DBG(2, "msg %p: Reserved %d bytes at offset +%td for attr %d "
494  "nlmsg_len=%d\n", n, attrlen,
495  (void *) nla - nlmsg_data(n->nm_nlh),
496  attrtype, n->nm_nlh->nlmsg_len);
497 
498  return nla;
499 }
int nla_padlen(int payload)
length of padding at the tail of the attribute
Definition: attr.c:126
void * nlmsg_data(const struct nlmsghdr *nlh)
head of message payload
Definition: msg.c:218
int nla_total_size(int payload)
total length of attribute including padding
Definition: attr.c:117
int nla_attr_size(int payload)
length of attribute not including padding
Definition: attr.c:108

◆ nla_put()

int nla_put ( struct nl_msg *  n,
int  attrtype,
int  attrlen,
const void *  data 
)
Parameters
nnetlink message
attrtypeattribute type
attrlenlength of attribute payload
datahead of attribute payload
Returns
-1 if the tailroom of the skb is insufficient to store the attribute header and payload.

Definition at line 511 of file attr.c.

References nla_data(), nla_reserve(), and nlmsg_data().

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

512 {
513  struct nlattr *nla;
514 
515  nla = nla_reserve(n, attrtype, attrlen);
516  if (!nla)
517  return nl_errno(ENOMEM);
518 
519  memcpy(nla_data(nla), data, attrlen);
520  NL_DBG(2, "msg %p: Wrote %d bytes at offset +%td for attr %d\n",
521  n, attrlen, (void *) nla - nlmsg_data(n->nm_nlh), attrtype);
522 
523  return 0;
524 }
void * nlmsg_data(const struct nlmsghdr *nlh)
head of message payload
Definition: msg.c:218
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151
struct nlattr * nla_reserve(struct nl_msg *n, int attrtype, int attrlen)
reserve room for attribute on the skb
Definition: attr.c:474

◆ nla_put_nested()

int nla_put_nested ( struct nl_msg *  n,
int  attrtype,
struct nl_msg *  nested 
)
Parameters
nnetlink message
attrtypeattribute type
nestednetlink attribute to nest
Returns
-1 if the tailroom of the skb is insufficient to store the attribute header and payload.

Definition at line 535 of file attr.c.

References nla_put(), nlmsg_data(), and nlmsg_len().

Referenced by rtnl_neightbl_build_change_request().

536 {
537  return nla_put(n, attrtype, nlmsg_len(nested->nm_nlh),
538  nlmsg_data(nested->nm_nlh));
539 }
void * nlmsg_data(const struct nlmsghdr *nlh)
head of message payload
Definition: msg.c:218
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
Definition: attr.c:511
int nlmsg_len(const struct nlmsghdr *nlh)
length of message payload
Definition: msg.c:232

◆ nla_put_u8()

int nla_put_u8 ( struct nl_msg *  n,
int  attrtype,
uint8_t  value 
)
Parameters
nnetlink message
attrtypeattribute type
valuenumeric value

Definition at line 547 of file attr.c.

References nla_put().

548 {
549  return nla_put(n, attrtype, sizeof(uint8_t), &value);
550 }
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
Definition: attr.c:511

◆ nla_put_u16()

int nla_put_u16 ( struct nl_msg *  n,
int  attrtype,
uint16_t  value 
)
Parameters
nnetlink message
attrtypeattribute type
valuenumeric value

Definition at line 558 of file attr.c.

References nla_put().

559 {
560  return nla_put(n, attrtype, sizeof(uint16_t), &value);
561 }
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
Definition: attr.c:511

◆ nla_put_u32()

int nla_put_u32 ( struct nl_msg *  n,
int  attrtype,
uint32_t  value 
)
Parameters
nnetlink message
attrtypeattribute type
valuenumeric value

Definition at line 569 of file attr.c.

References nla_put().

Referenced by rtnl_neightbl_build_change_request().

570 {
571  return nla_put(n, attrtype, sizeof(uint32_t), &value);
572 }
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
Definition: attr.c:511

◆ nla_put_u64()

int nla_put_u64 ( struct nl_msg *  n,
int  attrtype,
uint64_t  value 
)
Parameters
nnetlink message
attrtypeattribute type
valuenumeric value

Definition at line 580 of file attr.c.

References nla_put().

Referenced by nla_put_msecs(), and rtnl_neightbl_build_change_request().

581 {
582  return nla_put(n, attrtype, sizeof(uint64_t), &value);
583 }
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
Definition: attr.c:511

◆ nla_put_string()

int nla_put_string ( struct nl_msg *  n,
int  attrtype,
const char *  str 
)
Parameters
nnetlink message
attrtypeattribute type
strNUL terminated string

Definition at line 591 of file attr.c.

References nla_put().

Referenced by rtnl_neightbl_build_change_request().

592 {
593  return nla_put(n, attrtype, strlen(str) + 1, str);
594 }
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
Definition: attr.c:511

◆ nla_put_flag()

int nla_put_flag ( struct nl_msg *  n,
int  attrtype 
)
Parameters
nnetlink message
attrtypeattribute type

Definition at line 601 of file attr.c.

References nla_put().

602 {
603  return nla_put(n, attrtype, 0, NULL);
604 }
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
Definition: attr.c:511

◆ nla_put_msecs()

int nla_put_msecs ( struct nl_msg *  n,
int  attrtype,
unsigned long  msecs 
)
Parameters
nnetlink message
attrtypeattribute type
msecsnumber of msecs

Definition at line 612 of file attr.c.

References nla_put_u64().

613 {
614  return nla_put_u64(n, attrtype, msecs);
615 }
int nla_put_u64(struct nl_msg *n, int attrtype, uint64_t value)
Add a u64 netlink attribute to a netlink message.
Definition: attr.c:580

◆ nla_put_data()

int nla_put_data ( struct nl_msg *  n,
int  attrtype,
struct nl_data *  data 
)
Parameters
nnetlink message
attrtypeattribute type
dataabstract data

Definition at line 623 of file attr.c.

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

624 {
625  return nla_put(n, attrtype, nl_data_get_size(data),
626  nl_data_get(data));
627 }
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
Definition: attr.c:511
size_t nl_data_get_size(struct nl_data *data)
Get size of data buffer of abstract data object.
Definition: data.c:140
void * nl_data_get(struct nl_data *data)
Get data buffer of abstract data object.
Definition: data.c:130

◆ nla_put_addr()

int nla_put_addr ( struct nl_msg *  n,
int  attrtype,
struct nl_addr *  addr 
)
Parameters
nnetlink message
attrtypeattribute type
addrabstract address

Definition at line 635 of file attr.c.

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

636 {
637  return nla_put(n, attrtype, nl_addr_get_len(addr),
639 }
unsigned int nl_addr_get_len(struct nl_addr *addr)
Get length of binary address of abstract address object.
Definition: addr.c:765
void * nl_addr_get_binary_addr(struct nl_addr *addr)
Get binary address of abstract address object.
Definition: addr.c:756
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
Definition: attr.c:511

◆ nla_nest_start()

struct nlattr* nla_nest_start ( struct nl_msg *  n,
int  attrtype 
)
Parameters
nnetlink message
attrtypeattribute type of container
Returns
the container attribute

Definition at line 655 of file attr.c.

Referenced by rtnl_link_build_change_request().

656 {
657  struct nlattr *start = (struct nlattr *) nlmsg_tail(n->nm_nlh);
658 
659  if (nla_put(n, attrtype, 0, NULL) < 0)
660  return NULL;
661 
662  return start;
663 }
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
Definition: attr.c:511

◆ nla_nest_end()

int nla_nest_end ( struct nl_msg *  n,
struct nlattr *  start 
)
Parameters
nnetlink message
startcontainer attribute

Corrects the container attribute header to include the all appeneded attributes.

Returns
the total data length of the skb.

Definition at line 675 of file attr.c.

Referenced by rtnl_link_build_change_request().

676 {
677  start->nla_len = (unsigned char *) nlmsg_tail(n->nm_nlh) -
678  (unsigned char *) start;
679  return 0;
680 }

◆ nla_get_u32()

uint32_t nla_get_u32 ( struct nlattr *  nla)
Parameters
nlau32 netlink attribute

Definition at line 693 of file attr.c.

References nla_data().

694 {
695  return *(uint32_t *) nla_data(nla);
696 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151

◆ nla_get_u16()

uint16_t nla_get_u16 ( struct nlattr *  nla)
Parameters
nlau16 netlink attribute

Definition at line 702 of file attr.c.

References nla_data().

703 {
704  return *(uint16_t *) nla_data(nla);
705 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151

◆ nla_get_u8()

uint8_t nla_get_u8 ( struct nlattr *  nla)
Parameters
nlau8 netlink attribute

Definition at line 711 of file attr.c.

References nla_data().

712 {
713  return *(uint8_t *) nla_data(nla);
714 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151

◆ nla_get_u64()

uint64_t nla_get_u64 ( struct nlattr *  nla)
Parameters
nlau64 netlink attribute

Definition at line 720 of file attr.c.

References nla_memcpy().

Referenced by nla_get_msecs().

721 {
722  uint64_t tmp;
723 
724  nla_memcpy(&tmp, nla, sizeof(tmp));
725 
726  return tmp;
727 }
int nla_memcpy(void *dest, struct nlattr *src, int count)
Copy a netlink attribute into another memory area.
Definition: attr.c:382

◆ nla_get_string()

char* nla_get_string ( struct nlattr *  nla)
Parameters
nlastring netlink attribute

Definition at line 733 of file attr.c.

References nla_data().

734 {
735  return (char *) nla_data(nla);
736 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151

◆ nla_get_flag()

int nla_get_flag ( struct nlattr *  nla)
Parameters
nlaflag netlink attribute

Definition at line 742 of file attr.c.

743 {
744  return !!nla;
745 }

◆ nla_get_msecs()

unsigned long nla_get_msecs ( struct nlattr *  nla)
Parameters
nlamsecs netlink attribute
Returns
the number of milliseconds.

Definition at line 753 of file attr.c.

References nla_get_u64().

754 {
755  return nla_get_u64(nla);
756 }
uint64_t nla_get_u64(struct nlattr *nla)
Return payload of u64 attribute.
Definition: attr.c:720

◆ nla_get_addr()

struct nl_addr* nla_get_addr ( struct nlattr *  nla,
int  family 
)
Parameters
nlaaddress netlink attribute
familyaddress family
Returns
Newly allocated address handle or NULL

Definition at line 765 of file attr.c.

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

766 {
767  return nl_addr_build(family, nla_data(nla), nla_len(nla));
768 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151
int nla_len(const struct nlattr *nla)
length of payload
Definition: attr.c:160
struct nl_addr * nl_addr_build(int family, void *buf, size_t size)
Allocate new abstract address object based on a binary address.
Definition: addr.c:187

◆ nla_get_data()

struct nl_data* nla_get_data ( struct nlattr *  nla)
Parameters
nlaabstract data netlink attribute
Returns
Newly allocated abstract data handle or NULL

Definition at line 776 of file attr.c.

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

777 {
778  return nl_data_alloc(nla_data(nla), nla_len(nla));
779 }
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151
int nla_len(const struct nlattr *nla)
length of payload
Definition: attr.c:160
struct nl_data * nl_data_alloc(void *buf, size_t size)
Allocate a new abstract data object.
Definition: data.c:38