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