x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / lib / nlattr.c
blob3d8295c8550532cea0a6a50409deed23c8a2026b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * NETLINK Netlink attributes
5 * Authors: Thomas Graf <tgraf@suug.ch>
6 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7 */
9 #include <linux/export.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/jiffies.h>
13 #include <linux/skbuff.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
16 #include <net/netlink.h>
18 static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
19 [NLA_U8] = sizeof(u8),
20 [NLA_U16] = sizeof(u16),
21 [NLA_U32] = sizeof(u32),
22 [NLA_U64] = sizeof(u64),
23 [NLA_MSECS] = sizeof(u64),
24 [NLA_NESTED] = NLA_HDRLEN,
25 [NLA_S8] = sizeof(s8),
26 [NLA_S16] = sizeof(s16),
27 [NLA_S32] = sizeof(s32),
28 [NLA_S64] = sizeof(s64),
31 static int validate_nla_bitfield32(const struct nlattr *nla,
32 u32 *valid_flags_allowed)
34 const struct nla_bitfield32 *bf = nla_data(nla);
35 u32 *valid_flags_mask = valid_flags_allowed;
37 if (!valid_flags_allowed)
38 return -EINVAL;
40 /*disallow invalid bit selector */
41 if (bf->selector & ~*valid_flags_mask)
42 return -EINVAL;
44 /*disallow invalid bit values */
45 if (bf->value & ~*valid_flags_mask)
46 return -EINVAL;
48 /*disallow valid bit values that are not selected*/
49 if (bf->value & ~bf->selector)
50 return -EINVAL;
52 return 0;
55 static int validate_nla(const struct nlattr *nla, int maxtype,
56 const struct nla_policy *policy)
58 const struct nla_policy *pt;
59 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
61 if (type <= 0 || type > maxtype)
62 return 0;
64 pt = &policy[type];
66 BUG_ON(pt->type > NLA_TYPE_MAX);
68 switch (pt->type) {
69 case NLA_FLAG:
70 if (attrlen > 0)
71 return -ERANGE;
72 break;
74 case NLA_BITFIELD32:
75 if (attrlen != sizeof(struct nla_bitfield32))
76 return -ERANGE;
78 return validate_nla_bitfield32(nla, pt->validation_data);
80 case NLA_NUL_STRING:
81 if (pt->len)
82 minlen = min_t(int, attrlen, pt->len + 1);
83 else
84 minlen = attrlen;
86 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
87 return -EINVAL;
88 /* fall through */
90 case NLA_STRING:
91 if (attrlen < 1)
92 return -ERANGE;
94 if (pt->len) {
95 char *buf = nla_data(nla);
97 if (buf[attrlen - 1] == '\0')
98 attrlen--;
100 if (attrlen > pt->len)
101 return -ERANGE;
103 break;
105 case NLA_BINARY:
106 if (pt->len && attrlen > pt->len)
107 return -ERANGE;
108 break;
110 case NLA_NESTED_COMPAT:
111 if (attrlen < pt->len)
112 return -ERANGE;
113 if (attrlen < NLA_ALIGN(pt->len))
114 break;
115 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
116 return -ERANGE;
117 nla = nla_data(nla) + NLA_ALIGN(pt->len);
118 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
119 return -ERANGE;
120 break;
121 case NLA_NESTED:
122 /* a nested attributes is allowed to be empty; if its not,
123 * it must have a size of at least NLA_HDRLEN.
125 if (attrlen == 0)
126 break;
127 default:
128 if (pt->len)
129 minlen = pt->len;
130 else if (pt->type != NLA_UNSPEC)
131 minlen = nla_attr_minlen[pt->type];
133 if (attrlen < minlen)
134 return -ERANGE;
137 return 0;
141 * nla_validate - Validate a stream of attributes
142 * @head: head of attribute stream
143 * @len: length of attribute stream
144 * @maxtype: maximum attribute type to be expected
145 * @policy: validation policy
146 * @extack: extended ACK report struct
148 * Validates all attributes in the specified attribute stream against the
149 * specified policy. Attributes with a type exceeding maxtype will be
150 * ignored. See documenation of struct nla_policy for more details.
152 * Returns 0 on success or a negative error code.
154 int nla_validate(const struct nlattr *head, int len, int maxtype,
155 const struct nla_policy *policy,
156 struct netlink_ext_ack *extack)
158 const struct nlattr *nla;
159 int rem;
161 nla_for_each_attr(nla, head, len, rem) {
162 int err = validate_nla(nla, maxtype, policy);
164 if (err < 0) {
165 if (extack)
166 extack->bad_attr = nla;
167 return err;
171 return 0;
173 EXPORT_SYMBOL(nla_validate);
176 * nla_policy_len - Determin the max. length of a policy
177 * @policy: policy to use
178 * @n: number of policies
180 * Determines the max. length of the policy. It is currently used
181 * to allocated Netlink buffers roughly the size of the actual
182 * message.
184 * Returns 0 on success or a negative error code.
187 nla_policy_len(const struct nla_policy *p, int n)
189 int i, len = 0;
191 for (i = 0; i < n; i++, p++) {
192 if (p->len)
193 len += nla_total_size(p->len);
194 else if (nla_attr_minlen[p->type])
195 len += nla_total_size(nla_attr_minlen[p->type]);
198 return len;
200 EXPORT_SYMBOL(nla_policy_len);
203 * nla_parse - Parse a stream of attributes into a tb buffer
204 * @tb: destination array with maxtype+1 elements
205 * @maxtype: maximum attribute type to be expected
206 * @head: head of attribute stream
207 * @len: length of attribute stream
208 * @policy: validation policy
210 * Parses a stream of attributes and stores a pointer to each attribute in
211 * the tb array accessible via the attribute type. Attributes with a type
212 * exceeding maxtype will be silently ignored for backwards compatibility
213 * reasons. policy may be set to NULL if no validation is required.
215 * Returns 0 on success or a negative error code.
217 int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
218 int len, const struct nla_policy *policy,
219 struct netlink_ext_ack *extack)
221 const struct nlattr *nla;
222 int rem, err;
224 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
226 nla_for_each_attr(nla, head, len, rem) {
227 u16 type = nla_type(nla);
229 if (type > 0 && type <= maxtype) {
230 if (policy) {
231 err = validate_nla(nla, maxtype, policy);
232 if (err < 0) {
233 if (extack)
234 extack->bad_attr = nla;
235 goto errout;
239 tb[type] = (struct nlattr *)nla;
243 if (unlikely(rem > 0))
244 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
245 rem, current->comm);
247 err = 0;
248 errout:
249 return err;
251 EXPORT_SYMBOL(nla_parse);
254 * nla_find - Find a specific attribute in a stream of attributes
255 * @head: head of attribute stream
256 * @len: length of attribute stream
257 * @attrtype: type of attribute to look for
259 * Returns the first attribute in the stream matching the specified type.
261 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
263 const struct nlattr *nla;
264 int rem;
266 nla_for_each_attr(nla, head, len, rem)
267 if (nla_type(nla) == attrtype)
268 return (struct nlattr *)nla;
270 return NULL;
272 EXPORT_SYMBOL(nla_find);
275 * nla_strlcpy - Copy string attribute payload into a sized buffer
276 * @dst: where to copy the string to
277 * @nla: attribute to copy the string from
278 * @dstsize: size of destination buffer
280 * Copies at most dstsize - 1 bytes into the destination buffer.
281 * The result is always a valid NUL-terminated string. Unlike
282 * strlcpy the destination buffer is always padded out.
284 * Returns the length of the source buffer.
286 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
288 size_t srclen = nla_len(nla);
289 char *src = nla_data(nla);
291 if (srclen > 0 && src[srclen - 1] == '\0')
292 srclen--;
294 if (dstsize > 0) {
295 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
297 memset(dst, 0, dstsize);
298 memcpy(dst, src, len);
301 return srclen;
303 EXPORT_SYMBOL(nla_strlcpy);
306 * nla_strdup - Copy string attribute payload into a newly allocated buffer
307 * @nla: attribute to copy the string from
308 * @flags: the type of memory to allocate (see kmalloc).
310 * Returns a pointer to the allocated buffer or NULL on error.
312 char *nla_strdup(const struct nlattr *nla, gfp_t flags)
314 size_t srclen = nla_len(nla);
315 char *src = nla_data(nla), *dst;
317 if (srclen > 0 && src[srclen - 1] == '\0')
318 srclen--;
320 dst = kmalloc(srclen + 1, flags);
321 if (dst != NULL) {
322 memcpy(dst, src, srclen);
323 dst[srclen] = '\0';
325 return dst;
327 EXPORT_SYMBOL(nla_strdup);
330 * nla_memcpy - Copy a netlink attribute into another memory area
331 * @dest: where to copy to memcpy
332 * @src: netlink attribute to copy from
333 * @count: size of the destination area
335 * Note: The number of bytes copied is limited by the length of
336 * attribute's payload. memcpy
338 * Returns the number of bytes copied.
340 int nla_memcpy(void *dest, const struct nlattr *src, int count)
342 int minlen = min_t(int, count, nla_len(src));
344 memcpy(dest, nla_data(src), minlen);
345 if (count > minlen)
346 memset(dest + minlen, 0, count - minlen);
348 return minlen;
350 EXPORT_SYMBOL(nla_memcpy);
353 * nla_memcmp - Compare an attribute with sized memory area
354 * @nla: netlink attribute
355 * @data: memory area
356 * @size: size of memory area
358 int nla_memcmp(const struct nlattr *nla, const void *data,
359 size_t size)
361 int d = nla_len(nla) - size;
363 if (d == 0)
364 d = memcmp(nla_data(nla), data, size);
366 return d;
368 EXPORT_SYMBOL(nla_memcmp);
371 * nla_strcmp - Compare a string attribute against a string
372 * @nla: netlink string attribute
373 * @str: another string
375 int nla_strcmp(const struct nlattr *nla, const char *str)
377 int len = strlen(str);
378 char *buf = nla_data(nla);
379 int attrlen = nla_len(nla);
380 int d;
382 if (attrlen > 0 && buf[attrlen - 1] == '\0')
383 attrlen--;
385 d = attrlen - len;
386 if (d == 0)
387 d = memcmp(nla_data(nla), str, len);
389 return d;
391 EXPORT_SYMBOL(nla_strcmp);
393 #ifdef CONFIG_NET
395 * __nla_reserve - reserve room for attribute on the skb
396 * @skb: socket buffer to reserve room on
397 * @attrtype: attribute type
398 * @attrlen: length of attribute payload
400 * Adds a netlink attribute header to a socket buffer and reserves
401 * room for the payload but does not copy it.
403 * The caller is responsible to ensure that the skb provides enough
404 * tailroom for the attribute header and payload.
406 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
408 struct nlattr *nla;
410 nla = skb_put(skb, nla_total_size(attrlen));
411 nla->nla_type = attrtype;
412 nla->nla_len = nla_attr_size(attrlen);
414 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
416 return nla;
418 EXPORT_SYMBOL(__nla_reserve);
421 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
422 * @skb: socket buffer to reserve room on
423 * @attrtype: attribute type
424 * @attrlen: length of attribute payload
425 * @padattr: attribute type for the padding
427 * Adds a netlink attribute header to a socket buffer and reserves
428 * room for the payload but does not copy it. It also ensure that this
429 * attribute will have a 64-bit aligned nla_data() area.
431 * The caller is responsible to ensure that the skb provides enough
432 * tailroom for the attribute header and payload.
434 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
435 int attrlen, int padattr)
437 if (nla_need_padding_for_64bit(skb))
438 nla_align_64bit(skb, padattr);
440 return __nla_reserve(skb, attrtype, attrlen);
442 EXPORT_SYMBOL(__nla_reserve_64bit);
445 * __nla_reserve_nohdr - reserve room for attribute without header
446 * @skb: socket buffer to reserve room on
447 * @attrlen: length of attribute payload
449 * Reserves room for attribute payload without a header.
451 * The caller is responsible to ensure that the skb provides enough
452 * tailroom for the payload.
454 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
456 return skb_put_zero(skb, NLA_ALIGN(attrlen));
458 EXPORT_SYMBOL(__nla_reserve_nohdr);
461 * nla_reserve - reserve room for attribute on the skb
462 * @skb: socket buffer to reserve room on
463 * @attrtype: attribute type
464 * @attrlen: length of attribute payload
466 * Adds a netlink attribute header to a socket buffer and reserves
467 * room for the payload but does not copy it.
469 * Returns NULL if the tailroom of the skb is insufficient to store
470 * the attribute header and payload.
472 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
474 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
475 return NULL;
477 return __nla_reserve(skb, attrtype, attrlen);
479 EXPORT_SYMBOL(nla_reserve);
482 * nla_reserve_64bit - reserve room for attribute on the skb and align it
483 * @skb: socket buffer to reserve room on
484 * @attrtype: attribute type
485 * @attrlen: length of attribute payload
486 * @padattr: attribute type for the padding
488 * Adds a netlink attribute header to a socket buffer and reserves
489 * room for the payload but does not copy it. It also ensure that this
490 * attribute will have a 64-bit aligned nla_data() area.
492 * Returns NULL if the tailroom of the skb is insufficient to store
493 * the attribute header and payload.
495 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
496 int padattr)
498 size_t len;
500 if (nla_need_padding_for_64bit(skb))
501 len = nla_total_size_64bit(attrlen);
502 else
503 len = nla_total_size(attrlen);
504 if (unlikely(skb_tailroom(skb) < len))
505 return NULL;
507 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
509 EXPORT_SYMBOL(nla_reserve_64bit);
512 * nla_reserve_nohdr - reserve room for attribute without header
513 * @skb: socket buffer to reserve room on
514 * @attrlen: length of attribute payload
516 * Reserves room for attribute payload without a header.
518 * Returns NULL if the tailroom of the skb is insufficient to store
519 * the attribute payload.
521 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
523 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
524 return NULL;
526 return __nla_reserve_nohdr(skb, attrlen);
528 EXPORT_SYMBOL(nla_reserve_nohdr);
531 * __nla_put - Add a netlink attribute to a socket buffer
532 * @skb: socket buffer to add attribute to
533 * @attrtype: attribute type
534 * @attrlen: length of attribute payload
535 * @data: head of attribute payload
537 * The caller is responsible to ensure that the skb provides enough
538 * tailroom for the attribute header and payload.
540 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
541 const void *data)
543 struct nlattr *nla;
545 nla = __nla_reserve(skb, attrtype, attrlen);
546 memcpy(nla_data(nla), data, attrlen);
548 EXPORT_SYMBOL(__nla_put);
551 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
552 * @skb: socket buffer to add attribute to
553 * @attrtype: attribute type
554 * @attrlen: length of attribute payload
555 * @data: head of attribute payload
556 * @padattr: attribute type for the padding
558 * The caller is responsible to ensure that the skb provides enough
559 * tailroom for the attribute header and payload.
561 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
562 const void *data, int padattr)
564 struct nlattr *nla;
566 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
567 memcpy(nla_data(nla), data, attrlen);
569 EXPORT_SYMBOL(__nla_put_64bit);
572 * __nla_put_nohdr - Add a netlink attribute without header
573 * @skb: socket buffer to add attribute to
574 * @attrlen: length of attribute payload
575 * @data: head of attribute payload
577 * The caller is responsible to ensure that the skb provides enough
578 * tailroom for the attribute payload.
580 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
582 void *start;
584 start = __nla_reserve_nohdr(skb, attrlen);
585 memcpy(start, data, attrlen);
587 EXPORT_SYMBOL(__nla_put_nohdr);
590 * nla_put - Add a netlink attribute to a socket buffer
591 * @skb: socket buffer to add attribute to
592 * @attrtype: attribute type
593 * @attrlen: length of attribute payload
594 * @data: head of attribute payload
596 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
597 * the attribute header and payload.
599 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
601 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
602 return -EMSGSIZE;
604 __nla_put(skb, attrtype, attrlen, data);
605 return 0;
607 EXPORT_SYMBOL(nla_put);
610 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
611 * @skb: socket buffer to add attribute to
612 * @attrtype: attribute type
613 * @attrlen: length of attribute payload
614 * @data: head of attribute payload
615 * @padattr: attribute type for the padding
617 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
618 * the attribute header and payload.
620 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
621 const void *data, int padattr)
623 size_t len;
625 if (nla_need_padding_for_64bit(skb))
626 len = nla_total_size_64bit(attrlen);
627 else
628 len = nla_total_size(attrlen);
629 if (unlikely(skb_tailroom(skb) < len))
630 return -EMSGSIZE;
632 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
633 return 0;
635 EXPORT_SYMBOL(nla_put_64bit);
638 * nla_put_nohdr - Add a netlink attribute without header
639 * @skb: socket buffer to add attribute to
640 * @attrlen: length of attribute payload
641 * @data: head of attribute payload
643 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
644 * the attribute payload.
646 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
648 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
649 return -EMSGSIZE;
651 __nla_put_nohdr(skb, attrlen, data);
652 return 0;
654 EXPORT_SYMBOL(nla_put_nohdr);
657 * nla_append - Add a netlink attribute without header or padding
658 * @skb: socket buffer to add attribute to
659 * @attrlen: length of attribute payload
660 * @data: head of attribute payload
662 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
663 * the attribute payload.
665 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
667 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
668 return -EMSGSIZE;
670 skb_put_data(skb, data, attrlen);
671 return 0;
673 EXPORT_SYMBOL(nla_append);
674 #endif