1 // SPDX-License-Identifier: GPL-2.0
3 * NETLINK Netlink attributes
5 * Authors: Thomas Graf <tgraf@suug.ch>
6 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
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 /* For these data types, attribute length should be exactly the given
19 * size. However, to maintain compatibility with broken commands, if the
20 * attribute length does not match the expected size a warning is emitted
21 * to the user that the command is sending invalid data and needs to be fixed.
23 static const u8 nla_attr_len
[NLA_TYPE_MAX
+1] = {
24 [NLA_U8
] = sizeof(u8
),
25 [NLA_U16
] = sizeof(u16
),
26 [NLA_U32
] = sizeof(u32
),
27 [NLA_U64
] = sizeof(u64
),
28 [NLA_S8
] = sizeof(s8
),
29 [NLA_S16
] = sizeof(s16
),
30 [NLA_S32
] = sizeof(s32
),
31 [NLA_S64
] = sizeof(s64
),
34 static const u8 nla_attr_minlen
[NLA_TYPE_MAX
+1] = {
35 [NLA_U8
] = sizeof(u8
),
36 [NLA_U16
] = sizeof(u16
),
37 [NLA_U32
] = sizeof(u32
),
38 [NLA_U64
] = sizeof(u64
),
39 [NLA_MSECS
] = sizeof(u64
),
40 [NLA_NESTED
] = NLA_HDRLEN
,
41 [NLA_S8
] = sizeof(s8
),
42 [NLA_S16
] = sizeof(s16
),
43 [NLA_S32
] = sizeof(s32
),
44 [NLA_S64
] = sizeof(s64
),
47 static int validate_nla_bitfield32(const struct nlattr
*nla
,
48 const u32
*valid_flags_mask
)
50 const struct nla_bitfield32
*bf
= nla_data(nla
);
52 if (!valid_flags_mask
)
55 /*disallow invalid bit selector */
56 if (bf
->selector
& ~*valid_flags_mask
)
59 /*disallow invalid bit values */
60 if (bf
->value
& ~*valid_flags_mask
)
63 /*disallow valid bit values that are not selected*/
64 if (bf
->value
& ~bf
->selector
)
70 static int nla_validate_array(const struct nlattr
*head
, int len
, int maxtype
,
71 const struct nla_policy
*policy
,
72 struct netlink_ext_ack
*extack
)
74 const struct nlattr
*entry
;
77 nla_for_each_attr(entry
, head
, len
, rem
) {
80 if (nla_len(entry
) == 0)
83 if (nla_len(entry
) < NLA_HDRLEN
) {
84 NL_SET_ERR_MSG_ATTR(extack
, entry
,
85 "Array element too short");
89 ret
= nla_validate(nla_data(entry
), nla_len(entry
),
90 maxtype
, policy
, extack
);
98 static int nla_validate_int_range(const struct nla_policy
*pt
,
99 const struct nlattr
*nla
,
100 struct netlink_ext_ack
*extack
)
102 bool validate_min
, validate_max
;
105 validate_min
= pt
->validation_type
== NLA_VALIDATE_RANGE
||
106 pt
->validation_type
== NLA_VALIDATE_MIN
;
107 validate_max
= pt
->validation_type
== NLA_VALIDATE_RANGE
||
108 pt
->validation_type
== NLA_VALIDATE_MAX
;
112 value
= nla_get_u8(nla
);
115 value
= nla_get_u16(nla
);
118 value
= nla_get_u32(nla
);
121 value
= nla_get_s8(nla
);
124 value
= nla_get_s16(nla
);
127 value
= nla_get_s32(nla
);
130 value
= nla_get_s64(nla
);
133 /* treat this one specially, since it may not fit into s64 */
134 if ((validate_min
&& nla_get_u64(nla
) < pt
->min
) ||
135 (validate_max
&& nla_get_u64(nla
) > pt
->max
)) {
136 NL_SET_ERR_MSG_ATTR(extack
, nla
,
137 "integer out of range");
146 if ((validate_min
&& value
< pt
->min
) ||
147 (validate_max
&& value
> pt
->max
)) {
148 NL_SET_ERR_MSG_ATTR(extack
, nla
,
149 "integer out of range");
156 static int validate_nla(const struct nlattr
*nla
, int maxtype
,
157 const struct nla_policy
*policy
,
158 struct netlink_ext_ack
*extack
)
160 const struct nla_policy
*pt
;
161 int minlen
= 0, attrlen
= nla_len(nla
), type
= nla_type(nla
);
164 if (type
<= 0 || type
> maxtype
)
169 BUG_ON(pt
->type
> NLA_TYPE_MAX
);
171 if ((nla_attr_len
[pt
->type
] && attrlen
!= nla_attr_len
[pt
->type
]) ||
172 (pt
->type
== NLA_EXACT_LEN_WARN
&& attrlen
!= pt
->len
)) {
173 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
174 current
->comm
, type
);
179 if (attrlen
!= pt
->len
)
184 if (extack
&& pt
->validation_data
) {
185 NL_SET_BAD_ATTR(extack
, nla
);
186 extack
->_msg
= pt
->validation_data
;
198 if (attrlen
!= sizeof(struct nla_bitfield32
))
201 err
= validate_nla_bitfield32(nla
, pt
->validation_data
);
208 minlen
= min_t(int, attrlen
, pt
->len
+ 1);
212 if (!minlen
|| memchr(nla_data(nla
), '\0', minlen
) == NULL
) {
223 char *buf
= nla_data(nla
);
225 if (buf
[attrlen
- 1] == '\0')
228 if (attrlen
> pt
->len
)
234 if (pt
->len
&& attrlen
> pt
->len
)
239 /* a nested attributes is allowed to be empty; if its not,
240 * it must have a size of at least NLA_HDRLEN.
244 if (attrlen
< NLA_HDRLEN
)
246 if (pt
->validation_data
) {
247 err
= nla_validate(nla_data(nla
), nla_len(nla
), pt
->len
,
248 pt
->validation_data
, extack
);
251 * return directly to preserve the inner
252 * error message/attribute pointer
258 case NLA_NESTED_ARRAY
:
259 /* a nested array attribute is allowed to be empty; if its not,
260 * it must have a size of at least NLA_HDRLEN.
264 if (attrlen
< NLA_HDRLEN
)
266 if (pt
->validation_data
) {
269 err
= nla_validate_array(nla_data(nla
), nla_len(nla
),
270 pt
->len
, pt
->validation_data
,
274 * return directly to preserve the inner
275 * error message/attribute pointer
284 else if (pt
->type
!= NLA_UNSPEC
)
285 minlen
= nla_attr_minlen
[pt
->type
];
287 if (attrlen
< minlen
)
291 /* further validation */
292 switch (pt
->validation_type
) {
293 case NLA_VALIDATE_NONE
:
296 case NLA_VALIDATE_RANGE
:
297 case NLA_VALIDATE_MIN
:
298 case NLA_VALIDATE_MAX
:
299 err
= nla_validate_int_range(pt
, nla
, extack
);
303 case NLA_VALIDATE_FUNCTION
:
305 err
= pt
->validate(nla
, extack
);
314 NL_SET_ERR_MSG_ATTR(extack
, nla
, "Attribute failed policy validation");
319 * nla_validate - Validate a stream of attributes
320 * @head: head of attribute stream
321 * @len: length of attribute stream
322 * @maxtype: maximum attribute type to be expected
323 * @policy: validation policy
324 * @extack: extended ACK report struct
326 * Validates all attributes in the specified attribute stream against the
327 * specified policy. Attributes with a type exceeding maxtype will be
328 * ignored. See documenation of struct nla_policy for more details.
330 * Returns 0 on success or a negative error code.
332 int nla_validate(const struct nlattr
*head
, int len
, int maxtype
,
333 const struct nla_policy
*policy
,
334 struct netlink_ext_ack
*extack
)
336 const struct nlattr
*nla
;
339 nla_for_each_attr(nla
, head
, len
, rem
) {
340 int err
= validate_nla(nla
, maxtype
, policy
, extack
);
348 EXPORT_SYMBOL(nla_validate
);
351 * nla_policy_len - Determin the max. length of a policy
352 * @policy: policy to use
353 * @n: number of policies
355 * Determines the max. length of the policy. It is currently used
356 * to allocated Netlink buffers roughly the size of the actual
359 * Returns 0 on success or a negative error code.
362 nla_policy_len(const struct nla_policy
*p
, int n
)
366 for (i
= 0; i
< n
; i
++, p
++) {
368 len
+= nla_total_size(p
->len
);
369 else if (nla_attr_len
[p
->type
])
370 len
+= nla_total_size(nla_attr_len
[p
->type
]);
371 else if (nla_attr_minlen
[p
->type
])
372 len
+= nla_total_size(nla_attr_minlen
[p
->type
]);
377 EXPORT_SYMBOL(nla_policy_len
);
380 * nla_parse - Parse a stream of attributes into a tb buffer
381 * @tb: destination array with maxtype+1 elements
382 * @maxtype: maximum attribute type to be expected
383 * @head: head of attribute stream
384 * @len: length of attribute stream
385 * @policy: validation policy
387 * Parses a stream of attributes and stores a pointer to each attribute in
388 * the tb array accessible via the attribute type. Attributes with a type
389 * exceeding maxtype will be silently ignored for backwards compatibility
390 * reasons. policy may be set to NULL if no validation is required.
392 * Returns 0 on success or a negative error code.
394 static int __nla_parse(struct nlattr
**tb
, int maxtype
,
395 const struct nlattr
*head
, int len
,
396 bool strict
, const struct nla_policy
*policy
,
397 struct netlink_ext_ack
*extack
)
399 const struct nlattr
*nla
;
402 memset(tb
, 0, sizeof(struct nlattr
*) * (maxtype
+ 1));
404 nla_for_each_attr(nla
, head
, len
, rem
) {
405 u16 type
= nla_type(nla
);
407 if (type
== 0 || type
> maxtype
) {
409 NL_SET_ERR_MSG(extack
, "Unknown attribute type");
415 int err
= validate_nla(nla
, maxtype
, policy
, extack
);
421 tb
[type
] = (struct nlattr
*)nla
;
424 if (unlikely(rem
> 0)) {
425 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
427 NL_SET_ERR_MSG(extack
, "bytes leftover after parsing attributes");
435 int nla_parse(struct nlattr
**tb
, int maxtype
, const struct nlattr
*head
,
436 int len
, const struct nla_policy
*policy
,
437 struct netlink_ext_ack
*extack
)
439 return __nla_parse(tb
, maxtype
, head
, len
, false, policy
, extack
);
441 EXPORT_SYMBOL(nla_parse
);
443 int nla_parse_strict(struct nlattr
**tb
, int maxtype
, const struct nlattr
*head
,
444 int len
, const struct nla_policy
*policy
,
445 struct netlink_ext_ack
*extack
)
447 return __nla_parse(tb
, maxtype
, head
, len
, true, policy
, extack
);
449 EXPORT_SYMBOL(nla_parse_strict
);
452 * nla_find - Find a specific attribute in a stream of attributes
453 * @head: head of attribute stream
454 * @len: length of attribute stream
455 * @attrtype: type of attribute to look for
457 * Returns the first attribute in the stream matching the specified type.
459 struct nlattr
*nla_find(const struct nlattr
*head
, int len
, int attrtype
)
461 const struct nlattr
*nla
;
464 nla_for_each_attr(nla
, head
, len
, rem
)
465 if (nla_type(nla
) == attrtype
)
466 return (struct nlattr
*)nla
;
470 EXPORT_SYMBOL(nla_find
);
473 * nla_strlcpy - Copy string attribute payload into a sized buffer
474 * @dst: where to copy the string to
475 * @nla: attribute to copy the string from
476 * @dstsize: size of destination buffer
478 * Copies at most dstsize - 1 bytes into the destination buffer.
479 * The result is always a valid NUL-terminated string. Unlike
480 * strlcpy the destination buffer is always padded out.
482 * Returns the length of the source buffer.
484 size_t nla_strlcpy(char *dst
, const struct nlattr
*nla
, size_t dstsize
)
486 size_t srclen
= nla_len(nla
);
487 char *src
= nla_data(nla
);
489 if (srclen
> 0 && src
[srclen
- 1] == '\0')
493 size_t len
= (srclen
>= dstsize
) ? dstsize
- 1 : srclen
;
495 memset(dst
, 0, dstsize
);
496 memcpy(dst
, src
, len
);
501 EXPORT_SYMBOL(nla_strlcpy
);
504 * nla_strdup - Copy string attribute payload into a newly allocated buffer
505 * @nla: attribute to copy the string from
506 * @flags: the type of memory to allocate (see kmalloc).
508 * Returns a pointer to the allocated buffer or NULL on error.
510 char *nla_strdup(const struct nlattr
*nla
, gfp_t flags
)
512 size_t srclen
= nla_len(nla
);
513 char *src
= nla_data(nla
), *dst
;
515 if (srclen
> 0 && src
[srclen
- 1] == '\0')
518 dst
= kmalloc(srclen
+ 1, flags
);
520 memcpy(dst
, src
, srclen
);
525 EXPORT_SYMBOL(nla_strdup
);
528 * nla_memcpy - Copy a netlink attribute into another memory area
529 * @dest: where to copy to memcpy
530 * @src: netlink attribute to copy from
531 * @count: size of the destination area
533 * Note: The number of bytes copied is limited by the length of
534 * attribute's payload. memcpy
536 * Returns the number of bytes copied.
538 int nla_memcpy(void *dest
, const struct nlattr
*src
, int count
)
540 int minlen
= min_t(int, count
, nla_len(src
));
542 memcpy(dest
, nla_data(src
), minlen
);
544 memset(dest
+ minlen
, 0, count
- minlen
);
548 EXPORT_SYMBOL(nla_memcpy
);
551 * nla_memcmp - Compare an attribute with sized memory area
552 * @nla: netlink attribute
554 * @size: size of memory area
556 int nla_memcmp(const struct nlattr
*nla
, const void *data
,
559 int d
= nla_len(nla
) - size
;
562 d
= memcmp(nla_data(nla
), data
, size
);
566 EXPORT_SYMBOL(nla_memcmp
);
569 * nla_strcmp - Compare a string attribute against a string
570 * @nla: netlink string attribute
571 * @str: another string
573 int nla_strcmp(const struct nlattr
*nla
, const char *str
)
575 int len
= strlen(str
);
576 char *buf
= nla_data(nla
);
577 int attrlen
= nla_len(nla
);
580 if (attrlen
> 0 && buf
[attrlen
- 1] == '\0')
585 d
= memcmp(nla_data(nla
), str
, len
);
589 EXPORT_SYMBOL(nla_strcmp
);
593 * __nla_reserve - reserve room for attribute on the skb
594 * @skb: socket buffer to reserve room on
595 * @attrtype: attribute type
596 * @attrlen: length of attribute payload
598 * Adds a netlink attribute header to a socket buffer and reserves
599 * room for the payload but does not copy it.
601 * The caller is responsible to ensure that the skb provides enough
602 * tailroom for the attribute header and payload.
604 struct nlattr
*__nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
608 nla
= skb_put(skb
, nla_total_size(attrlen
));
609 nla
->nla_type
= attrtype
;
610 nla
->nla_len
= nla_attr_size(attrlen
);
612 memset((unsigned char *) nla
+ nla
->nla_len
, 0, nla_padlen(attrlen
));
616 EXPORT_SYMBOL(__nla_reserve
);
619 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
620 * @skb: socket buffer to reserve room on
621 * @attrtype: attribute type
622 * @attrlen: length of attribute payload
623 * @padattr: attribute type for the padding
625 * Adds a netlink attribute header to a socket buffer and reserves
626 * room for the payload but does not copy it. It also ensure that this
627 * attribute will have a 64-bit aligned nla_data() area.
629 * The caller is responsible to ensure that the skb provides enough
630 * tailroom for the attribute header and payload.
632 struct nlattr
*__nla_reserve_64bit(struct sk_buff
*skb
, int attrtype
,
633 int attrlen
, int padattr
)
635 if (nla_need_padding_for_64bit(skb
))
636 nla_align_64bit(skb
, padattr
);
638 return __nla_reserve(skb
, attrtype
, attrlen
);
640 EXPORT_SYMBOL(__nla_reserve_64bit
);
643 * __nla_reserve_nohdr - reserve room for attribute without header
644 * @skb: socket buffer to reserve room on
645 * @attrlen: length of attribute payload
647 * Reserves room for attribute payload without a header.
649 * The caller is responsible to ensure that the skb provides enough
650 * tailroom for the payload.
652 void *__nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
654 return skb_put_zero(skb
, NLA_ALIGN(attrlen
));
656 EXPORT_SYMBOL(__nla_reserve_nohdr
);
659 * nla_reserve - reserve room for attribute on the skb
660 * @skb: socket buffer to reserve room on
661 * @attrtype: attribute type
662 * @attrlen: length of attribute payload
664 * Adds a netlink attribute header to a socket buffer and reserves
665 * room for the payload but does not copy it.
667 * Returns NULL if the tailroom of the skb is insufficient to store
668 * the attribute header and payload.
670 struct nlattr
*nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
672 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
675 return __nla_reserve(skb
, attrtype
, attrlen
);
677 EXPORT_SYMBOL(nla_reserve
);
680 * nla_reserve_64bit - reserve room for attribute on the skb and align it
681 * @skb: socket buffer to reserve room on
682 * @attrtype: attribute type
683 * @attrlen: length of attribute payload
684 * @padattr: attribute type for the padding
686 * Adds a netlink attribute header to a socket buffer and reserves
687 * room for the payload but does not copy it. It also ensure that this
688 * attribute will have a 64-bit aligned nla_data() area.
690 * Returns NULL if the tailroom of the skb is insufficient to store
691 * the attribute header and payload.
693 struct nlattr
*nla_reserve_64bit(struct sk_buff
*skb
, int attrtype
, int attrlen
,
698 if (nla_need_padding_for_64bit(skb
))
699 len
= nla_total_size_64bit(attrlen
);
701 len
= nla_total_size(attrlen
);
702 if (unlikely(skb_tailroom(skb
) < len
))
705 return __nla_reserve_64bit(skb
, attrtype
, attrlen
, padattr
);
707 EXPORT_SYMBOL(nla_reserve_64bit
);
710 * nla_reserve_nohdr - reserve room for attribute without header
711 * @skb: socket buffer to reserve room on
712 * @attrlen: length of attribute payload
714 * Reserves room for attribute payload without a header.
716 * Returns NULL if the tailroom of the skb is insufficient to store
717 * the attribute payload.
719 void *nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
721 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
724 return __nla_reserve_nohdr(skb
, attrlen
);
726 EXPORT_SYMBOL(nla_reserve_nohdr
);
729 * __nla_put - Add a netlink attribute to a socket buffer
730 * @skb: socket buffer to add attribute to
731 * @attrtype: attribute type
732 * @attrlen: length of attribute payload
733 * @data: head of attribute payload
735 * The caller is responsible to ensure that the skb provides enough
736 * tailroom for the attribute header and payload.
738 void __nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
,
743 nla
= __nla_reserve(skb
, attrtype
, attrlen
);
744 memcpy(nla_data(nla
), data
, attrlen
);
746 EXPORT_SYMBOL(__nla_put
);
749 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
750 * @skb: socket buffer to add attribute to
751 * @attrtype: attribute type
752 * @attrlen: length of attribute payload
753 * @data: head of attribute payload
754 * @padattr: attribute type for the padding
756 * The caller is responsible to ensure that the skb provides enough
757 * tailroom for the attribute header and payload.
759 void __nla_put_64bit(struct sk_buff
*skb
, int attrtype
, int attrlen
,
760 const void *data
, int padattr
)
764 nla
= __nla_reserve_64bit(skb
, attrtype
, attrlen
, padattr
);
765 memcpy(nla_data(nla
), data
, attrlen
);
767 EXPORT_SYMBOL(__nla_put_64bit
);
770 * __nla_put_nohdr - Add a netlink attribute without header
771 * @skb: socket buffer to add attribute to
772 * @attrlen: length of attribute payload
773 * @data: head of attribute payload
775 * The caller is responsible to ensure that the skb provides enough
776 * tailroom for the attribute payload.
778 void __nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
782 start
= __nla_reserve_nohdr(skb
, attrlen
);
783 memcpy(start
, data
, attrlen
);
785 EXPORT_SYMBOL(__nla_put_nohdr
);
788 * nla_put - Add a netlink attribute to a socket buffer
789 * @skb: socket buffer to add attribute to
790 * @attrtype: attribute type
791 * @attrlen: length of attribute payload
792 * @data: head of attribute payload
794 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
795 * the attribute header and payload.
797 int nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
799 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
802 __nla_put(skb
, attrtype
, attrlen
, data
);
805 EXPORT_SYMBOL(nla_put
);
808 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
809 * @skb: socket buffer to add attribute to
810 * @attrtype: attribute type
811 * @attrlen: length of attribute payload
812 * @data: head of attribute payload
813 * @padattr: attribute type for the padding
815 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
816 * the attribute header and payload.
818 int nla_put_64bit(struct sk_buff
*skb
, int attrtype
, int attrlen
,
819 const void *data
, int padattr
)
823 if (nla_need_padding_for_64bit(skb
))
824 len
= nla_total_size_64bit(attrlen
);
826 len
= nla_total_size(attrlen
);
827 if (unlikely(skb_tailroom(skb
) < len
))
830 __nla_put_64bit(skb
, attrtype
, attrlen
, data
, padattr
);
833 EXPORT_SYMBOL(nla_put_64bit
);
836 * nla_put_nohdr - Add a netlink attribute without header
837 * @skb: socket buffer to add attribute to
838 * @attrlen: length of attribute payload
839 * @data: head of attribute payload
841 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
842 * the attribute payload.
844 int nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
846 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
849 __nla_put_nohdr(skb
, attrlen
, data
);
852 EXPORT_SYMBOL(nla_put_nohdr
);
855 * nla_append - Add a netlink attribute without header or padding
856 * @skb: socket buffer to add attribute to
857 * @attrlen: length of attribute payload
858 * @data: head of attribute payload
860 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
861 * the attribute payload.
863 int nla_append(struct sk_buff
*skb
, int attrlen
, const void *data
)
865 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
868 skb_put_data(skb
, data
, attrlen
);
871 EXPORT_SYMBOL(nla_append
);