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