12 #include <netlink-local.h>
13 #include <netlink/netlink.h>
14 #include <netlink/utils.h>
15 #include <netlink/addr.h>
16 #include <netlink/attr.h>
17 #include <netlink/msg.h>
18 #include <linux/socket.h>
110 return NLA_HDRLEN + payload;
144 return nla->nla_type & NLA_TYPE_MASK;
153 return (
char *) nla + NLA_HDRLEN;
162 return nla->nla_len - NLA_HDRLEN;
177 int nla_ok(
const struct nlattr *nla,
int remaining)
179 return remaining >=
sizeof(*nla) &&
180 nla->nla_len >=
sizeof(*nla) &&
181 nla->nla_len <= remaining;
192 struct nlattr *
nla_next(
const struct nlattr *nla,
int *remaining)
194 int totlen = NLA_ALIGN(nla->nla_len);
196 *remaining -= totlen;
197 return (
struct nlattr *) ((
char *) nla + totlen);
201 [
NLA_U8] =
sizeof(uint8_t),
209 static int validate_nla(
struct nlattr *nla,
int maxtype,
215 if (type <= 0 || type > maxtype)
226 minlen = nla_attr_minlen[pt->
type];
229 return nl_errno(ERANGE);
232 return nl_errno(ERANGE);
235 return nl_errno(ERANGE);
239 if (data[
nla_len(nla) - 1] !=
'\0')
240 return nl_errno(EINVAL);
262 int nla_parse(
struct nlattr *tb[],
int maxtype,
struct nlattr *head,
int len,
268 memset(tb, 0,
sizeof(
struct nlattr *) * (maxtype + 1));
277 if (type <= maxtype) {
279 err = validate_nla(nla, maxtype, policy);
289 fprintf(stderr,
"netlink: %d bytes leftover after parsing "
290 "attributes.\n", rem);
334 err = validate_nla(nla, maxtype, policy);
352 struct nlattr *
nla_find(
struct nlattr *head,
int len,
int attrtype)
389 minlen = min_t(
int, count,
nla_len(src));
390 memcpy(dest,
nla_data(src), minlen);
407 size_t nla_strlcpy(
char *dst,
const struct nlattr *nla,
size_t dstsize)
412 if (srclen > 0 && src[srclen - 1] ==
'\0')
416 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
418 memset(dst, 0, dstsize);
419 memcpy(dst, src, len);
437 d = memcmp(
nla_data(nla), data, size);
449 int len = strlen(str) + 1;
453 d = memcmp(
nla_data(nla), str, len);
474 struct nlattr *
nla_reserve(
struct nl_msg *n,
int attrtype,
int attrlen)
479 tlen = NLMSG_ALIGN(n->nm_nlh->nlmsg_len) +
nla_total_size(attrlen);
481 if (tlen > n->nm_size) {
486 nla = (
struct nlattr *) nlmsg_tail(n->nm_nlh);
487 nla->nla_type = attrtype;
490 memset((
unsigned char *) nla + nla->nla_len, 0,
nla_padlen(attrlen));
491 n->nm_nlh->nlmsg_len = tlen;
493 NL_DBG(2,
"msg %p: Reserved %d bytes at offset +%td for attr %d "
494 "nlmsg_len=%d\n", n, attrlen,
496 attrtype, n->nm_nlh->nlmsg_len);
511 int nla_put(
struct nl_msg *n,
int attrtype,
int attrlen,
const void *data)
517 return nl_errno(ENOMEM);
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);
547 int nla_put_u8(
struct nl_msg *n,
int attrtype, uint8_t value)
549 return nla_put(n, attrtype,
sizeof(uint8_t), &value);
560 return nla_put(n, attrtype,
sizeof(uint16_t), &value);
571 return nla_put(n, attrtype,
sizeof(uint32_t), &value);
582 return nla_put(n, attrtype,
sizeof(uint64_t), &value);
593 return nla_put(n, attrtype, strlen(str) + 1, str);
603 return nla_put(n, attrtype, 0, NULL);
657 struct nlattr *start = (
struct nlattr *) nlmsg_tail(n->nm_nlh);
659 if (
nla_put(n, attrtype, 0, NULL) < 0)
677 start->nla_len = (
unsigned char *) nlmsg_tail(n->nm_nlh) -
678 (
unsigned char *) start;