10#include <libmnl/libmnl.h>
40 return attr->nla_type & NLA_TYPE_MASK;
63 return attr->nla_len - MNL_ATTR_HDRLEN;
74 return (
void *)attr + MNL_ATTR_HDRLEN;
93EXPORT_SYMBOL
bool mnl_attr_ok(
const struct nlattr *attr,
int len)
95 return len >= (int)
sizeof(
struct nlattr) &&
96 attr->nla_len >=
sizeof(
struct nlattr) &&
97 (int)attr->nla_len <= len;
110 return (
struct nlattr *)((
void *)attr + MNL_ALIGN(attr->nla_len));
136static int __mnl_attr_validate(
const struct nlattr *attr,
137 enum mnl_attr_data_type type,
size_t exp_len)
142 if (attr_len < exp_len) {
153 case MNL_TYPE_NUL_STRING:
158 if (attr_data[attr_len-1] !=
'\0') {
163 case MNL_TYPE_STRING:
169 case MNL_TYPE_NESTED:
174 if (attr_len < MNL_ATTR_HDRLEN) {
183 if (exp_len && attr_len > exp_len) {
190static const size_t mnl_attr_data_type_len[MNL_TYPE_MAX] = {
191 [MNL_TYPE_U8] =
sizeof(uint8_t),
192 [MNL_TYPE_U16] =
sizeof(uint16_t),
193 [MNL_TYPE_U32] =
sizeof(uint32_t),
194 [MNL_TYPE_U64] =
sizeof(uint64_t),
195 [MNL_TYPE_MSECS] =
sizeof(uint64_t),
211 if (type >= MNL_TYPE_MAX) {
215 exp_len = mnl_attr_data_type_len[type];
216 return __mnl_attr_validate(attr, type, exp_len);
230 enum mnl_attr_data_type type,
233 if (type >= MNL_TYPE_MAX) {
237 return __mnl_attr_validate(attr, type, exp_len);
256 unsigned int offset, mnl_attr_cb_t cb,
260 const struct nlattr *attr;
262 mnl_attr_for_each(attr, nlh, offset)
263 if ((ret = cb(attr, data)) <= MNL_CB_STOP)
283 mnl_attr_cb_t cb,
void *data)
286 const struct nlattr *attr;
288 mnl_attr_for_each_nested(attr, nested)
289 if ((ret = cb(attr, data)) <= MNL_CB_STOP)
315 mnl_attr_cb_t cb,
void *data)
318 const struct nlattr *attr;
320 mnl_attr_for_each_payload(payload, payload_len)
321 if ((ret = cb(attr, data)) <= MNL_CB_STOP)
396 size_t len,
const void *data)
399 uint16_t payload_len = MNL_ALIGN(
sizeof(
struct nlattr)) + len;
402 attr->nla_type = type;
403 attr->nla_len = payload_len;
405 pad = MNL_ALIGN(len) - len;
409 nlh->nlmsg_len += MNL_ALIGN(payload_len);
520 start->nla_type = NLA_F_NESTED | type;
521 nlh->nlmsg_len += MNL_ALIGN(
sizeof(
struct nlattr));
541 uint16_t type,
size_t len,
544 if (nlh->nlmsg_len + MNL_ATTR_HDRLEN + MNL_ALIGN(len) > buflen)
564 uint16_t type, uint8_t data)
585 uint16_t type, uint16_t data)
606 uint16_t type, uint32_t data)
627 uint16_t type, uint64_t data)
648 uint16_t type,
const char *data)
670 uint16_t type,
const char *data)
689 if (nlh->nlmsg_len + MNL_ATTR_HDRLEN > buflen)
702 struct nlattr *start)
715 struct nlattr *start)
void mnl_attr_put_u8(struct nlmsghdr *nlh, uint16_t type, uint8_t data)
bool mnl_attr_ok(const struct nlattr *attr, int len)
void mnl_attr_put_strz(struct nlmsghdr *nlh, uint16_t type, const char *data)
bool mnl_attr_put_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, size_t len, const void *data)
uint8_t mnl_attr_get_u8(const struct nlattr *attr)
void mnl_attr_put_u16(struct nlmsghdr *nlh, uint16_t type, uint16_t data)
int mnl_attr_parse_nested(const struct nlattr *nested, mnl_attr_cb_t cb, void *data)
bool mnl_attr_put_u8_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint8_t data)
uint64_t mnl_attr_get_u64(const struct nlattr *attr)
void * mnl_attr_get_payload(const struct nlattr *attr)
void mnl_attr_put_str(struct nlmsghdr *nlh, uint16_t type, const char *data)
bool mnl_attr_put_strz_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, const char *data)
bool mnl_attr_put_u16_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint16_t data)
uint16_t mnl_attr_get_payload_len(const struct nlattr *attr)
int mnl_attr_validate(const struct nlattr *attr, enum mnl_attr_data_type type)
void mnl_attr_put_u64(struct nlmsghdr *nlh, uint16_t type, uint64_t data)
int mnl_attr_parse(const struct nlmsghdr *nlh, unsigned int offset, mnl_attr_cb_t cb, void *data)
uint16_t mnl_attr_get_len(const struct nlattr *attr)
int mnl_attr_type_valid(const struct nlattr *attr, uint16_t max)
uint16_t mnl_attr_get_type(const struct nlattr *attr)
bool mnl_attr_put_u64_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint64_t data)
struct nlattr * mnl_attr_nest_start(struct nlmsghdr *nlh, uint16_t type)
bool mnl_attr_put_str_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, const char *data)
int mnl_attr_validate2(const struct nlattr *attr, enum mnl_attr_data_type type, size_t exp_len)
uint32_t mnl_attr_get_u32(const struct nlattr *attr)
const char * mnl_attr_get_str(const struct nlattr *attr)
void mnl_attr_put_u32(struct nlmsghdr *nlh, uint16_t type, uint32_t data)
struct nlattr * mnl_attr_next(const struct nlattr *attr)
void mnl_attr_put(struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data)
void mnl_attr_nest_end(struct nlmsghdr *nlh, struct nlattr *start)
uint16_t mnl_attr_get_u16(const struct nlattr *attr)
void mnl_attr_nest_cancel(struct nlmsghdr *nlh, struct nlattr *start)
int mnl_attr_parse_payload(const void *payload, size_t payload_len, mnl_attr_cb_t cb, void *data)
struct nlattr * mnl_attr_nest_start_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type)
bool mnl_attr_put_u32_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint32_t data)
void * mnl_nlmsg_get_payload_tail(const struct nlmsghdr *nlh)