xref: /openbmc/linux/lib/nlattr.c (revision 8b036556)
1 /*
2  * NETLINK      Netlink attributes
3  *
4  * 		Authors:	Thomas Graf <tgraf@suug.ch>
5  * 				Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6  */
7 
8 #include <linux/export.h>
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/jiffies.h>
12 #include <linux/skbuff.h>
13 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <net/netlink.h>
16 
17 static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = {
18 	[NLA_U8]	= sizeof(u8),
19 	[NLA_U16]	= sizeof(u16),
20 	[NLA_U32]	= sizeof(u32),
21 	[NLA_U64]	= sizeof(u64),
22 	[NLA_MSECS]	= sizeof(u64),
23 	[NLA_NESTED]	= NLA_HDRLEN,
24 	[NLA_S8]	= sizeof(s8),
25 	[NLA_S16]	= sizeof(s16),
26 	[NLA_S32]	= sizeof(s32),
27 	[NLA_S64]	= sizeof(s64),
28 };
29 
30 static int validate_nla(const struct nlattr *nla, int maxtype,
31 			const struct nla_policy *policy)
32 {
33 	const struct nla_policy *pt;
34 	int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
35 
36 	if (type <= 0 || type > maxtype)
37 		return 0;
38 
39 	pt = &policy[type];
40 
41 	BUG_ON(pt->type > NLA_TYPE_MAX);
42 
43 	switch (pt->type) {
44 	case NLA_FLAG:
45 		if (attrlen > 0)
46 			return -ERANGE;
47 		break;
48 
49 	case NLA_NUL_STRING:
50 		if (pt->len)
51 			minlen = min_t(int, attrlen, pt->len + 1);
52 		else
53 			minlen = attrlen;
54 
55 		if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
56 			return -EINVAL;
57 		/* fall through */
58 
59 	case NLA_STRING:
60 		if (attrlen < 1)
61 			return -ERANGE;
62 
63 		if (pt->len) {
64 			char *buf = nla_data(nla);
65 
66 			if (buf[attrlen - 1] == '\0')
67 				attrlen--;
68 
69 			if (attrlen > pt->len)
70 				return -ERANGE;
71 		}
72 		break;
73 
74 	case NLA_BINARY:
75 		if (pt->len && attrlen > pt->len)
76 			return -ERANGE;
77 		break;
78 
79 	case NLA_NESTED_COMPAT:
80 		if (attrlen < pt->len)
81 			return -ERANGE;
82 		if (attrlen < NLA_ALIGN(pt->len))
83 			break;
84 		if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
85 			return -ERANGE;
86 		nla = nla_data(nla) + NLA_ALIGN(pt->len);
87 		if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
88 			return -ERANGE;
89 		break;
90 	case NLA_NESTED:
91 		/* a nested attributes is allowed to be empty; if its not,
92 		 * it must have a size of at least NLA_HDRLEN.
93 		 */
94 		if (attrlen == 0)
95 			break;
96 	default:
97 		if (pt->len)
98 			minlen = pt->len;
99 		else if (pt->type != NLA_UNSPEC)
100 			minlen = nla_attr_minlen[pt->type];
101 
102 		if (attrlen < minlen)
103 			return -ERANGE;
104 	}
105 
106 	return 0;
107 }
108 
109 /**
110  * nla_validate - Validate a stream of attributes
111  * @head: head of attribute stream
112  * @len: length of attribute stream
113  * @maxtype: maximum attribute type to be expected
114  * @policy: validation policy
115  *
116  * Validates all attributes in the specified attribute stream against the
117  * specified policy. Attributes with a type exceeding maxtype will be
118  * ignored. See documenation of struct nla_policy for more details.
119  *
120  * Returns 0 on success or a negative error code.
121  */
122 int nla_validate(const struct nlattr *head, int len, int maxtype,
123 		 const struct nla_policy *policy)
124 {
125 	const struct nlattr *nla;
126 	int rem, err;
127 
128 	nla_for_each_attr(nla, head, len, rem) {
129 		err = validate_nla(nla, maxtype, policy);
130 		if (err < 0)
131 			goto errout;
132 	}
133 
134 	err = 0;
135 errout:
136 	return err;
137 }
138 EXPORT_SYMBOL(nla_validate);
139 
140 /**
141  * nla_policy_len - Determin the max. length of a policy
142  * @policy: policy to use
143  * @n: number of policies
144  *
145  * Determines the max. length of the policy.  It is currently used
146  * to allocated Netlink buffers roughly the size of the actual
147  * message.
148  *
149  * Returns 0 on success or a negative error code.
150  */
151 int
152 nla_policy_len(const struct nla_policy *p, int n)
153 {
154 	int i, len = 0;
155 
156 	for (i = 0; i < n; i++, p++) {
157 		if (p->len)
158 			len += nla_total_size(p->len);
159 		else if (nla_attr_minlen[p->type])
160 			len += nla_total_size(nla_attr_minlen[p->type]);
161 	}
162 
163 	return len;
164 }
165 EXPORT_SYMBOL(nla_policy_len);
166 
167 /**
168  * nla_parse - Parse a stream of attributes into a tb buffer
169  * @tb: destination array with maxtype+1 elements
170  * @maxtype: maximum attribute type to be expected
171  * @head: head of attribute stream
172  * @len: length of attribute stream
173  * @policy: validation policy
174  *
175  * Parses a stream of attributes and stores a pointer to each attribute in
176  * the tb array accessible via the attribute type. Attributes with a type
177  * exceeding maxtype will be silently ignored for backwards compatibility
178  * reasons. policy may be set to NULL if no validation is required.
179  *
180  * Returns 0 on success or a negative error code.
181  */
182 int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
183 	      int len, const struct nla_policy *policy)
184 {
185 	const struct nlattr *nla;
186 	int rem, err;
187 
188 	memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
189 
190 	nla_for_each_attr(nla, head, len, rem) {
191 		u16 type = nla_type(nla);
192 
193 		if (type > 0 && type <= maxtype) {
194 			if (policy) {
195 				err = validate_nla(nla, maxtype, policy);
196 				if (err < 0)
197 					goto errout;
198 			}
199 
200 			tb[type] = (struct nlattr *)nla;
201 		}
202 	}
203 
204 	if (unlikely(rem > 0))
205 		pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
206 				    rem, current->comm);
207 
208 	err = 0;
209 errout:
210 	return err;
211 }
212 EXPORT_SYMBOL(nla_parse);
213 
214 /**
215  * nla_find - Find a specific attribute in a stream of attributes
216  * @head: head of attribute stream
217  * @len: length of attribute stream
218  * @attrtype: type of attribute to look for
219  *
220  * Returns the first attribute in the stream matching the specified type.
221  */
222 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
223 {
224 	const struct nlattr *nla;
225 	int rem;
226 
227 	nla_for_each_attr(nla, head, len, rem)
228 		if (nla_type(nla) == attrtype)
229 			return (struct nlattr *)nla;
230 
231 	return NULL;
232 }
233 EXPORT_SYMBOL(nla_find);
234 
235 /**
236  * nla_strlcpy - Copy string attribute payload into a sized buffer
237  * @dst: where to copy the string to
238  * @nla: attribute to copy the string from
239  * @dstsize: size of destination buffer
240  *
241  * Copies at most dstsize - 1 bytes into the destination buffer.
242  * The result is always a valid NUL-terminated string. Unlike
243  * strlcpy the destination buffer is always padded out.
244  *
245  * Returns the length of the source buffer.
246  */
247 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
248 {
249 	size_t srclen = nla_len(nla);
250 	char *src = nla_data(nla);
251 
252 	if (srclen > 0 && src[srclen - 1] == '\0')
253 		srclen--;
254 
255 	if (dstsize > 0) {
256 		size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
257 
258 		memset(dst, 0, dstsize);
259 		memcpy(dst, src, len);
260 	}
261 
262 	return srclen;
263 }
264 EXPORT_SYMBOL(nla_strlcpy);
265 
266 /**
267  * nla_memcpy - Copy a netlink attribute into another memory area
268  * @dest: where to copy to memcpy
269  * @src: netlink attribute to copy from
270  * @count: size of the destination area
271  *
272  * Note: The number of bytes copied is limited by the length of
273  *       attribute's payload. memcpy
274  *
275  * Returns the number of bytes copied.
276  */
277 int nla_memcpy(void *dest, const struct nlattr *src, int count)
278 {
279 	int minlen = min_t(int, count, nla_len(src));
280 
281 	memcpy(dest, nla_data(src), minlen);
282 
283 	return minlen;
284 }
285 EXPORT_SYMBOL(nla_memcpy);
286 
287 /**
288  * nla_memcmp - Compare an attribute with sized memory area
289  * @nla: netlink attribute
290  * @data: memory area
291  * @size: size of memory area
292  */
293 int nla_memcmp(const struct nlattr *nla, const void *data,
294 			     size_t size)
295 {
296 	int d = nla_len(nla) - size;
297 
298 	if (d == 0)
299 		d = memcmp(nla_data(nla), data, size);
300 
301 	return d;
302 }
303 EXPORT_SYMBOL(nla_memcmp);
304 
305 /**
306  * nla_strcmp - Compare a string attribute against a string
307  * @nla: netlink string attribute
308  * @str: another string
309  */
310 int nla_strcmp(const struct nlattr *nla, const char *str)
311 {
312 	int len = strlen(str);
313 	char *buf = nla_data(nla);
314 	int attrlen = nla_len(nla);
315 	int d;
316 
317 	if (attrlen > 0 && buf[attrlen - 1] == '\0')
318 		attrlen--;
319 
320 	d = attrlen - len;
321 	if (d == 0)
322 		d = memcmp(nla_data(nla), str, len);
323 
324 	return d;
325 }
326 EXPORT_SYMBOL(nla_strcmp);
327 
328 #ifdef CONFIG_NET
329 /**
330  * __nla_reserve - reserve room for attribute on the skb
331  * @skb: socket buffer to reserve room on
332  * @attrtype: attribute type
333  * @attrlen: length of attribute payload
334  *
335  * Adds a netlink attribute header to a socket buffer and reserves
336  * room for the payload but does not copy it.
337  *
338  * The caller is responsible to ensure that the skb provides enough
339  * tailroom for the attribute header and payload.
340  */
341 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
342 {
343 	struct nlattr *nla;
344 
345 	nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
346 	nla->nla_type = attrtype;
347 	nla->nla_len = nla_attr_size(attrlen);
348 
349 	memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
350 
351 	return nla;
352 }
353 EXPORT_SYMBOL(__nla_reserve);
354 
355 /**
356  * __nla_reserve_nohdr - reserve room for attribute without header
357  * @skb: socket buffer to reserve room on
358  * @attrlen: length of attribute payload
359  *
360  * Reserves room for attribute payload without a header.
361  *
362  * The caller is responsible to ensure that the skb provides enough
363  * tailroom for the payload.
364  */
365 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
366 {
367 	void *start;
368 
369 	start = skb_put(skb, NLA_ALIGN(attrlen));
370 	memset(start, 0, NLA_ALIGN(attrlen));
371 
372 	return start;
373 }
374 EXPORT_SYMBOL(__nla_reserve_nohdr);
375 
376 /**
377  * nla_reserve - reserve room for attribute on the skb
378  * @skb: socket buffer to reserve room on
379  * @attrtype: attribute type
380  * @attrlen: length of attribute payload
381  *
382  * Adds a netlink attribute header to a socket buffer and reserves
383  * room for the payload but does not copy it.
384  *
385  * Returns NULL if the tailroom of the skb is insufficient to store
386  * the attribute header and payload.
387  */
388 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
389 {
390 	if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
391 		return NULL;
392 
393 	return __nla_reserve(skb, attrtype, attrlen);
394 }
395 EXPORT_SYMBOL(nla_reserve);
396 
397 /**
398  * nla_reserve_nohdr - reserve room for attribute without header
399  * @skb: socket buffer to reserve room on
400  * @attrlen: length of attribute payload
401  *
402  * Reserves room for attribute payload without a header.
403  *
404  * Returns NULL if the tailroom of the skb is insufficient to store
405  * the attribute payload.
406  */
407 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
408 {
409 	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
410 		return NULL;
411 
412 	return __nla_reserve_nohdr(skb, attrlen);
413 }
414 EXPORT_SYMBOL(nla_reserve_nohdr);
415 
416 /**
417  * __nla_put - Add a netlink attribute to a socket buffer
418  * @skb: socket buffer to add attribute to
419  * @attrtype: attribute type
420  * @attrlen: length of attribute payload
421  * @data: head of attribute payload
422  *
423  * The caller is responsible to ensure that the skb provides enough
424  * tailroom for the attribute header and payload.
425  */
426 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
427 			     const void *data)
428 {
429 	struct nlattr *nla;
430 
431 	nla = __nla_reserve(skb, attrtype, attrlen);
432 	memcpy(nla_data(nla), data, attrlen);
433 }
434 EXPORT_SYMBOL(__nla_put);
435 
436 /**
437  * __nla_put_nohdr - Add a netlink attribute without header
438  * @skb: socket buffer to add attribute to
439  * @attrlen: length of attribute payload
440  * @data: head of attribute payload
441  *
442  * The caller is responsible to ensure that the skb provides enough
443  * tailroom for the attribute payload.
444  */
445 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
446 {
447 	void *start;
448 
449 	start = __nla_reserve_nohdr(skb, attrlen);
450 	memcpy(start, data, attrlen);
451 }
452 EXPORT_SYMBOL(__nla_put_nohdr);
453 
454 /**
455  * nla_put - Add a netlink attribute to a socket buffer
456  * @skb: socket buffer to add attribute to
457  * @attrtype: attribute type
458  * @attrlen: length of attribute payload
459  * @data: head of attribute payload
460  *
461  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
462  * the attribute header and payload.
463  */
464 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
465 {
466 	if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
467 		return -EMSGSIZE;
468 
469 	__nla_put(skb, attrtype, attrlen, data);
470 	return 0;
471 }
472 EXPORT_SYMBOL(nla_put);
473 
474 /**
475  * nla_put_nohdr - Add a netlink attribute without header
476  * @skb: socket buffer to add attribute to
477  * @attrlen: length of attribute payload
478  * @data: head of attribute payload
479  *
480  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
481  * the attribute payload.
482  */
483 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
484 {
485 	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
486 		return -EMSGSIZE;
487 
488 	__nla_put_nohdr(skb, attrlen, data);
489 	return 0;
490 }
491 EXPORT_SYMBOL(nla_put_nohdr);
492 
493 /**
494  * nla_append - Add a netlink attribute without header or padding
495  * @skb: socket buffer to add attribute to
496  * @attrlen: length of attribute payload
497  * @data: head of attribute payload
498  *
499  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
500  * the attribute payload.
501  */
502 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
503 {
504 	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
505 		return -EMSGSIZE;
506 
507 	memcpy(skb_put(skb, attrlen), data, attrlen);
508 	return 0;
509 }
510 EXPORT_SYMBOL(nla_append);
511 #endif
512