1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4 * NETLINK Netlink attributes
11 #include <linux/rtnetlink.h>
15 static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = {
16 [LIBBPF_NLA_U8] = sizeof(uint8_t),
17 [LIBBPF_NLA_U16] = sizeof(uint16_t),
18 [LIBBPF_NLA_U32] = sizeof(uint32_t),
19 [LIBBPF_NLA_U64] = sizeof(uint64_t),
20 [LIBBPF_NLA_STRING] = 1,
21 [LIBBPF_NLA_FLAG] = 0,
24 static struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
26 int totlen = NLA_ALIGN(nla->nla_len);
29 return (struct nlattr *) ((char *) nla + totlen);
32 static int nla_ok(const struct nlattr *nla, int remaining)
34 return remaining >= sizeof(*nla) &&
35 nla->nla_len >= sizeof(*nla) &&
36 nla->nla_len <= remaining;
39 static int nla_type(const struct nlattr *nla)
41 return nla->nla_type & NLA_TYPE_MASK;
44 static int validate_nla(struct nlattr *nla, int maxtype,
45 struct libbpf_nla_policy *policy)
47 struct libbpf_nla_policy *pt;
48 unsigned int minlen = 0;
49 int type = nla_type(nla);
51 if (type < 0 || type > maxtype)
56 if (pt->type > LIBBPF_NLA_TYPE_MAX)
61 else if (pt->type != LIBBPF_NLA_UNSPEC)
62 minlen = nla_attr_minlen[pt->type];
64 if (libbpf_nla_len(nla) < minlen)
67 if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
70 if (pt->type == LIBBPF_NLA_STRING) {
71 char *data = libbpf_nla_data(nla);
73 if (data[libbpf_nla_len(nla) - 1] != '\0')
80 static inline int nlmsg_len(const struct nlmsghdr *nlh)
82 return nlh->nlmsg_len - NLMSG_HDRLEN;
86 * Create attribute index based on a stream of attributes.
87 * @arg tb Index array to be filled (maxtype+1 elements).
88 * @arg maxtype Maximum attribute type expected and accepted.
89 * @arg head Head of attribute stream.
90 * @arg len Length of attribute stream.
91 * @arg policy Attribute validation policy.
93 * Iterates over the stream of attributes and stores a pointer to each
94 * attribute in the index array using the attribute type as index to
95 * the array. Attribute with a type greater than the maximum type
96 * specified will be silently ignored in order to maintain backwards
97 * compatibility. If \a policy is not NULL, the attribute will be
98 * validated using the specified policy.
101 * @return 0 on success or a negative error code.
103 int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
104 int len, struct libbpf_nla_policy *policy)
109 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
111 libbpf_nla_for_each_attr(nla, head, len, rem) {
112 int type = nla_type(nla);
118 err = validate_nla(nla, maxtype, policy);
124 fprintf(stderr, "Attribute of type %#x found multiple times in message, "
125 "previous attribute is being ignored.\n", type);
136 * Create attribute index based on nested attribute
137 * @arg tb Index array to be filled (maxtype+1 elements).
138 * @arg maxtype Maximum attribute type expected and accepted.
139 * @arg nla Nested Attribute.
140 * @arg policy Attribute validation policy.
142 * Feeds the stream of attributes nested into the specified attribute
143 * to libbpf_nla_parse().
145 * @see libbpf_nla_parse
146 * @return 0 on success or a negative error code.
148 int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
150 struct libbpf_nla_policy *policy)
152 return libbpf_nla_parse(tb, maxtype, libbpf_nla_data(nla),
153 libbpf_nla_len(nla), policy);
156 /* dump netlink extended ack error message */
157 int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh)
159 struct libbpf_nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = {
160 [NLMSGERR_ATTR_MSG] = { .type = LIBBPF_NLA_STRING },
161 [NLMSGERR_ATTR_OFFS] = { .type = LIBBPF_NLA_U32 },
163 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr;
164 struct nlmsgerr *err;
168 /* no TLVs, nothing to do here */
169 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
172 err = (struct nlmsgerr *)NLMSG_DATA(nlh);
175 /* if NLM_F_CAPPED is set then the inner err msg was capped */
176 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
177 hlen += nlmsg_len(&err->msg);
179 attr = (struct nlattr *) ((void *) err + hlen);
180 alen = nlh->nlmsg_len - hlen;
182 if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen,
183 extack_policy) != 0) {
185 "Failed to parse extended error attributes\n");
189 if (tb[NLMSGERR_ATTR_MSG])
190 errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]);
192 fprintf(stderr, "Kernel error message: %s\n", errmsg);