xref: /OK3568_Linux_fs/kernel/tools/lib/bpf/nlattr.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /*
4*4882a593Smuzhiyun  * NETLINK      Netlink attributes
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <errno.h>
10*4882a593Smuzhiyun #include <string.h>
11*4882a593Smuzhiyun #include <stdio.h>
12*4882a593Smuzhiyun #include <linux/rtnetlink.h>
13*4882a593Smuzhiyun #include "nlattr.h"
14*4882a593Smuzhiyun #include "libbpf_internal.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = {
17*4882a593Smuzhiyun 	[LIBBPF_NLA_U8]		= sizeof(uint8_t),
18*4882a593Smuzhiyun 	[LIBBPF_NLA_U16]	= sizeof(uint16_t),
19*4882a593Smuzhiyun 	[LIBBPF_NLA_U32]	= sizeof(uint32_t),
20*4882a593Smuzhiyun 	[LIBBPF_NLA_U64]	= sizeof(uint64_t),
21*4882a593Smuzhiyun 	[LIBBPF_NLA_STRING]	= 1,
22*4882a593Smuzhiyun 	[LIBBPF_NLA_FLAG]	= 0,
23*4882a593Smuzhiyun };
24*4882a593Smuzhiyun 
nla_next(const struct nlattr * nla,int * remaining)25*4882a593Smuzhiyun static struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	int totlen = NLA_ALIGN(nla->nla_len);
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	*remaining -= totlen;
30*4882a593Smuzhiyun 	return (struct nlattr *) ((char *) nla + totlen);
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun 
nla_ok(const struct nlattr * nla,int remaining)33*4882a593Smuzhiyun static int nla_ok(const struct nlattr *nla, int remaining)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	return remaining >= sizeof(*nla) &&
36*4882a593Smuzhiyun 	       nla->nla_len >= sizeof(*nla) &&
37*4882a593Smuzhiyun 	       nla->nla_len <= remaining;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
nla_type(const struct nlattr * nla)40*4882a593Smuzhiyun static int nla_type(const struct nlattr *nla)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	return nla->nla_type & NLA_TYPE_MASK;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
validate_nla(struct nlattr * nla,int maxtype,struct libbpf_nla_policy * policy)45*4882a593Smuzhiyun static int validate_nla(struct nlattr *nla, int maxtype,
46*4882a593Smuzhiyun 			struct libbpf_nla_policy *policy)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	struct libbpf_nla_policy *pt;
49*4882a593Smuzhiyun 	unsigned int minlen = 0;
50*4882a593Smuzhiyun 	int type = nla_type(nla);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	if (type < 0 || type > maxtype)
53*4882a593Smuzhiyun 		return 0;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	pt = &policy[type];
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (pt->type > LIBBPF_NLA_TYPE_MAX)
58*4882a593Smuzhiyun 		return 0;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	if (pt->minlen)
61*4882a593Smuzhiyun 		minlen = pt->minlen;
62*4882a593Smuzhiyun 	else if (pt->type != LIBBPF_NLA_UNSPEC)
63*4882a593Smuzhiyun 		minlen = nla_attr_minlen[pt->type];
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	if (libbpf_nla_len(nla) < minlen)
66*4882a593Smuzhiyun 		return -1;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
69*4882a593Smuzhiyun 		return -1;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	if (pt->type == LIBBPF_NLA_STRING) {
72*4882a593Smuzhiyun 		char *data = libbpf_nla_data(nla);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 		if (data[libbpf_nla_len(nla) - 1] != '\0')
75*4882a593Smuzhiyun 			return -1;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	return 0;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
nlmsg_len(const struct nlmsghdr * nlh)81*4882a593Smuzhiyun static inline int nlmsg_len(const struct nlmsghdr *nlh)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	return nlh->nlmsg_len - NLMSG_HDRLEN;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /**
87*4882a593Smuzhiyun  * Create attribute index based on a stream of attributes.
88*4882a593Smuzhiyun  * @arg tb		Index array to be filled (maxtype+1 elements).
89*4882a593Smuzhiyun  * @arg maxtype		Maximum attribute type expected and accepted.
90*4882a593Smuzhiyun  * @arg head		Head of attribute stream.
91*4882a593Smuzhiyun  * @arg len		Length of attribute stream.
92*4882a593Smuzhiyun  * @arg policy		Attribute validation policy.
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * Iterates over the stream of attributes and stores a pointer to each
95*4882a593Smuzhiyun  * attribute in the index array using the attribute type as index to
96*4882a593Smuzhiyun  * the array. Attribute with a type greater than the maximum type
97*4882a593Smuzhiyun  * specified will be silently ignored in order to maintain backwards
98*4882a593Smuzhiyun  * compatibility. If \a policy is not NULL, the attribute will be
99*4882a593Smuzhiyun  * validated using the specified policy.
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * @see nla_validate
102*4882a593Smuzhiyun  * @return 0 on success or a negative error code.
103*4882a593Smuzhiyun  */
libbpf_nla_parse(struct nlattr * tb[],int maxtype,struct nlattr * head,int len,struct libbpf_nla_policy * policy)104*4882a593Smuzhiyun int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
105*4882a593Smuzhiyun 		     int len, struct libbpf_nla_policy *policy)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	struct nlattr *nla;
108*4882a593Smuzhiyun 	int rem, err;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	libbpf_nla_for_each_attr(nla, head, len, rem) {
113*4882a593Smuzhiyun 		int type = nla_type(nla);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 		if (type > maxtype)
116*4882a593Smuzhiyun 			continue;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 		if (policy) {
119*4882a593Smuzhiyun 			err = validate_nla(nla, maxtype, policy);
120*4882a593Smuzhiyun 			if (err < 0)
121*4882a593Smuzhiyun 				goto errout;
122*4882a593Smuzhiyun 		}
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 		if (tb[type])
125*4882a593Smuzhiyun 			pr_warn("Attribute of type %#x found multiple times in message, "
126*4882a593Smuzhiyun 				"previous attribute is being ignored.\n", type);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 		tb[type] = nla;
129*4882a593Smuzhiyun 	}
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	err = 0;
132*4882a593Smuzhiyun errout:
133*4882a593Smuzhiyun 	return err;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun  * Create attribute index based on nested attribute
138*4882a593Smuzhiyun  * @arg tb              Index array to be filled (maxtype+1 elements).
139*4882a593Smuzhiyun  * @arg maxtype         Maximum attribute type expected and accepted.
140*4882a593Smuzhiyun  * @arg nla             Nested Attribute.
141*4882a593Smuzhiyun  * @arg policy          Attribute validation policy.
142*4882a593Smuzhiyun  *
143*4882a593Smuzhiyun  * Feeds the stream of attributes nested into the specified attribute
144*4882a593Smuzhiyun  * to libbpf_nla_parse().
145*4882a593Smuzhiyun  *
146*4882a593Smuzhiyun  * @see libbpf_nla_parse
147*4882a593Smuzhiyun  * @return 0 on success or a negative error code.
148*4882a593Smuzhiyun  */
libbpf_nla_parse_nested(struct nlattr * tb[],int maxtype,struct nlattr * nla,struct libbpf_nla_policy * policy)149*4882a593Smuzhiyun int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
150*4882a593Smuzhiyun 			    struct nlattr *nla,
151*4882a593Smuzhiyun 			    struct libbpf_nla_policy *policy)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	return libbpf_nla_parse(tb, maxtype, libbpf_nla_data(nla),
154*4882a593Smuzhiyun 				libbpf_nla_len(nla), policy);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /* dump netlink extended ack error message */
libbpf_nla_dump_errormsg(struct nlmsghdr * nlh)158*4882a593Smuzhiyun int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	struct libbpf_nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = {
161*4882a593Smuzhiyun 		[NLMSGERR_ATTR_MSG]	= { .type = LIBBPF_NLA_STRING },
162*4882a593Smuzhiyun 		[NLMSGERR_ATTR_OFFS]	= { .type = LIBBPF_NLA_U32 },
163*4882a593Smuzhiyun 	};
164*4882a593Smuzhiyun 	struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr;
165*4882a593Smuzhiyun 	struct nlmsgerr *err;
166*4882a593Smuzhiyun 	char *errmsg = NULL;
167*4882a593Smuzhiyun 	int hlen, alen;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/* no TLVs, nothing to do here */
170*4882a593Smuzhiyun 	if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
171*4882a593Smuzhiyun 		return 0;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	err = (struct nlmsgerr *)NLMSG_DATA(nlh);
174*4882a593Smuzhiyun 	hlen = sizeof(*err);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/* if NLM_F_CAPPED is set then the inner err msg was capped */
177*4882a593Smuzhiyun 	if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
178*4882a593Smuzhiyun 		hlen += nlmsg_len(&err->msg);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	attr = (struct nlattr *) ((void *) err + hlen);
181*4882a593Smuzhiyun 	alen = nlh->nlmsg_len - hlen;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen,
184*4882a593Smuzhiyun 			     extack_policy) != 0) {
185*4882a593Smuzhiyun 		pr_warn("Failed to parse extended error attributes\n");
186*4882a593Smuzhiyun 		return 0;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	if (tb[NLMSGERR_ATTR_MSG])
190*4882a593Smuzhiyun 		errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	pr_warn("Kernel error message: %s\n", errmsg);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	return 0;
195*4882a593Smuzhiyun }
196