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/nospec.h>
14 #include <linux/skbuff.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <net/netlink.h>
19 /* For these data types, attribute length should be exactly the given
20 * size. However, to maintain compatibility with broken commands, if the
21 * attribute length does not match the expected size a warning is emitted
22 * to the user that the command is sending invalid data and needs to be fixed.
24 static const u8 nla_attr_len
[NLA_TYPE_MAX
+1] = {
25 [NLA_U8
] = sizeof(u8
),
26 [NLA_U16
] = sizeof(u16
),
27 [NLA_U32
] = sizeof(u32
),
28 [NLA_U64
] = sizeof(u64
),
29 [NLA_S8
] = sizeof(s8
),
30 [NLA_S16
] = sizeof(s16
),
31 [NLA_S32
] = sizeof(s32
),
32 [NLA_S64
] = sizeof(s64
),
35 static const u8 nla_attr_minlen
[NLA_TYPE_MAX
+1] = {
36 [NLA_U8
] = sizeof(u8
),
37 [NLA_U16
] = sizeof(u16
),
38 [NLA_U32
] = sizeof(u32
),
39 [NLA_U64
] = sizeof(u64
),
40 [NLA_MSECS
] = sizeof(u64
),
41 [NLA_NESTED
] = NLA_HDRLEN
,
42 [NLA_S8
] = sizeof(s8
),
43 [NLA_S16
] = sizeof(s16
),
44 [NLA_S32
] = sizeof(s32
),
45 [NLA_S64
] = sizeof(s64
),
49 * Nested policies might refer back to the original
50 * policy in some cases, and userspace could try to
51 * abuse that and recurse by nesting in the right
52 * ways. Limit recursion to avoid this problem.
54 #define MAX_POLICY_RECURSION_DEPTH 10
56 static int __nla_validate_parse(const struct nlattr
*head
, int len
, int maxtype
,
57 const struct nla_policy
*policy
,
58 unsigned int validate
,
59 struct netlink_ext_ack
*extack
,
60 struct nlattr
**tb
, unsigned int depth
);
62 static int validate_nla_bitfield32(const struct nlattr
*nla
,
63 const u32 valid_flags_mask
)
65 const struct nla_bitfield32
*bf
= nla_data(nla
);
67 if (!valid_flags_mask
)
70 /*disallow invalid bit selector */
71 if (bf
->selector
& ~valid_flags_mask
)
74 /*disallow invalid bit values */
75 if (bf
->value
& ~valid_flags_mask
)
78 /*disallow valid bit values that are not selected*/
79 if (bf
->value
& ~bf
->selector
)
85 static int nla_validate_array(const struct nlattr
*head
, int len
, int maxtype
,
86 const struct nla_policy
*policy
,
87 struct netlink_ext_ack
*extack
,
88 unsigned int validate
, unsigned int depth
)
90 const struct nlattr
*entry
;
93 nla_for_each_attr(entry
, head
, len
, rem
) {
96 if (nla_len(entry
) == 0)
99 if (nla_len(entry
) < NLA_HDRLEN
) {
100 NL_SET_ERR_MSG_ATTR_POL(extack
, entry
, policy
,
101 "Array element too short");
105 ret
= __nla_validate_parse(nla_data(entry
), nla_len(entry
),
106 maxtype
, policy
, validate
, extack
,
115 void nla_get_range_unsigned(const struct nla_policy
*pt
,
116 struct netlink_range_validation
*range
)
118 WARN_ON_ONCE(pt
->validation_type
!= NLA_VALIDATE_RANGE_PTR
&&
119 (pt
->min
< 0 || pt
->max
< 0));
130 range
->max
= U16_MAX
;
134 range
->max
= U32_MAX
;
139 range
->max
= U64_MAX
;
146 switch (pt
->validation_type
) {
147 case NLA_VALIDATE_RANGE
:
148 case NLA_VALIDATE_RANGE_WARN_TOO_LONG
:
149 range
->min
= pt
->min
;
150 range
->max
= pt
->max
;
152 case NLA_VALIDATE_RANGE_PTR
:
155 case NLA_VALIDATE_MIN
:
156 range
->min
= pt
->min
;
158 case NLA_VALIDATE_MAX
:
159 range
->max
= pt
->max
;
166 static int nla_validate_range_unsigned(const struct nla_policy
*pt
,
167 const struct nlattr
*nla
,
168 struct netlink_ext_ack
*extack
,
169 unsigned int validate
)
171 struct netlink_range_validation range
;
176 value
= nla_get_u8(nla
);
179 value
= nla_get_u16(nla
);
182 value
= nla_get_u32(nla
);
185 value
= nla_get_u64(nla
);
188 value
= nla_get_uint(nla
);
191 value
= nla_get_u64(nla
);
194 value
= nla_len(nla
);
197 value
= ntohs(nla_get_be16(nla
));
200 value
= ntohl(nla_get_be32(nla
));
206 nla_get_range_unsigned(pt
, &range
);
208 if (pt
->validation_type
== NLA_VALIDATE_RANGE_WARN_TOO_LONG
&&
209 pt
->type
== NLA_BINARY
&& value
> range
.max
) {
210 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
211 current
->comm
, pt
->type
);
212 if (validate
& NL_VALIDATE_STRICT_ATTRS
) {
213 NL_SET_ERR_MSG_ATTR_POL(extack
, nla
, pt
,
214 "invalid attribute length");
218 /* this assumes min <= max (don't validate against min) */
222 if (value
< range
.min
|| value
> range
.max
) {
223 bool binary
= pt
->type
== NLA_BINARY
;
226 NL_SET_ERR_MSG_ATTR_POL(extack
, nla
, pt
,
227 "binary attribute size out of range");
229 NL_SET_ERR_MSG_ATTR_POL(extack
, nla
, pt
,
230 "integer out of range");
238 void nla_get_range_signed(const struct nla_policy
*pt
,
239 struct netlink_range_validation_signed
*range
)
247 range
->min
= S16_MIN
;
248 range
->max
= S16_MAX
;
251 range
->min
= S32_MIN
;
252 range
->max
= S32_MAX
;
256 range
->min
= S64_MIN
;
257 range
->max
= S64_MAX
;
264 switch (pt
->validation_type
) {
265 case NLA_VALIDATE_RANGE
:
266 range
->min
= pt
->min
;
267 range
->max
= pt
->max
;
269 case NLA_VALIDATE_RANGE_PTR
:
270 *range
= *pt
->range_signed
;
272 case NLA_VALIDATE_MIN
:
273 range
->min
= pt
->min
;
275 case NLA_VALIDATE_MAX
:
276 range
->max
= pt
->max
;
283 static int nla_validate_int_range_signed(const struct nla_policy
*pt
,
284 const struct nlattr
*nla
,
285 struct netlink_ext_ack
*extack
)
287 struct netlink_range_validation_signed range
;
292 value
= nla_get_s8(nla
);
295 value
= nla_get_s16(nla
);
298 value
= nla_get_s32(nla
);
301 value
= nla_get_s64(nla
);
304 value
= nla_get_sint(nla
);
310 nla_get_range_signed(pt
, &range
);
312 if (value
< range
.min
|| value
> range
.max
) {
313 NL_SET_ERR_MSG_ATTR_POL(extack
, nla
, pt
,
314 "integer out of range");
321 static int nla_validate_int_range(const struct nla_policy
*pt
,
322 const struct nlattr
*nla
,
323 struct netlink_ext_ack
*extack
,
324 unsigned int validate
)
336 return nla_validate_range_unsigned(pt
, nla
, extack
, validate
);
342 return nla_validate_int_range_signed(pt
, nla
, extack
);
349 static int nla_validate_mask(const struct nla_policy
*pt
,
350 const struct nlattr
*nla
,
351 struct netlink_ext_ack
*extack
)
357 value
= nla_get_u8(nla
);
360 value
= nla_get_u16(nla
);
363 value
= nla_get_u32(nla
);
366 value
= nla_get_u64(nla
);
369 value
= nla_get_uint(nla
);
372 value
= ntohs(nla_get_be16(nla
));
375 value
= ntohl(nla_get_be32(nla
));
381 if (value
& ~(u64
)pt
->mask
) {
382 NL_SET_ERR_MSG_ATTR(extack
, nla
, "reserved bit set");
389 static int validate_nla(const struct nlattr
*nla
, int maxtype
,
390 const struct nla_policy
*policy
, unsigned int validate
,
391 struct netlink_ext_ack
*extack
, unsigned int depth
)
393 u16 strict_start_type
= policy
[0].strict_start_type
;
394 const struct nla_policy
*pt
;
395 int minlen
= 0, attrlen
= nla_len(nla
), type
= nla_type(nla
);
398 if (strict_start_type
&& type
>= strict_start_type
)
399 validate
|= NL_VALIDATE_STRICT
;
401 if (type
<= 0 || type
> maxtype
)
404 type
= array_index_nospec(type
, maxtype
+ 1);
407 BUG_ON(pt
->type
> NLA_TYPE_MAX
);
409 if (nla_attr_len
[pt
->type
] && attrlen
!= nla_attr_len
[pt
->type
]) {
410 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
411 current
->comm
, type
);
412 if (validate
& NL_VALIDATE_STRICT_ATTRS
) {
413 NL_SET_ERR_MSG_ATTR_POL(extack
, nla
, pt
,
414 "invalid attribute length");
419 if (validate
& NL_VALIDATE_NESTED
) {
420 if ((pt
->type
== NLA_NESTED
|| pt
->type
== NLA_NESTED_ARRAY
) &&
421 !(nla
->nla_type
& NLA_F_NESTED
)) {
422 NL_SET_ERR_MSG_ATTR_POL(extack
, nla
, pt
,
423 "NLA_F_NESTED is missing");
426 if (pt
->type
!= NLA_NESTED
&& pt
->type
!= NLA_NESTED_ARRAY
&&
427 pt
->type
!= NLA_UNSPEC
&& (nla
->nla_type
& NLA_F_NESTED
)) {
428 NL_SET_ERR_MSG_ATTR_POL(extack
, nla
, pt
,
429 "NLA_F_NESTED not expected");
436 if (extack
&& pt
->reject_message
) {
437 NL_SET_BAD_ATTR(extack
, nla
);
438 extack
->_msg
= pt
->reject_message
;
451 if (attrlen
!= sizeof(u32
) && attrlen
!= sizeof(u64
)) {
452 NL_SET_ERR_MSG_ATTR_POL(extack
, nla
, pt
,
453 "invalid attribute length");
459 if (attrlen
!= sizeof(struct nla_bitfield32
))
462 err
= validate_nla_bitfield32(nla
, pt
->bitfield32_valid
);
469 minlen
= min_t(int, attrlen
, pt
->len
+ 1);
473 if (!minlen
|| memchr(nla_data(nla
), '\0', minlen
) == NULL
) {
484 char *buf
= nla_data(nla
);
486 if (buf
[attrlen
- 1] == '\0')
489 if (attrlen
> pt
->len
)
495 if (pt
->len
&& attrlen
> pt
->len
)
500 /* a nested attributes is allowed to be empty; if its not,
501 * it must have a size of at least NLA_HDRLEN.
505 if (attrlen
< NLA_HDRLEN
)
507 if (pt
->nested_policy
) {
508 err
= __nla_validate_parse(nla_data(nla
), nla_len(nla
),
509 pt
->len
, pt
->nested_policy
,
510 validate
, extack
, NULL
,
514 * return directly to preserve the inner
515 * error message/attribute pointer
521 case NLA_NESTED_ARRAY
:
522 /* a nested array attribute is allowed to be empty; if its not,
523 * it must have a size of at least NLA_HDRLEN.
527 if (attrlen
< NLA_HDRLEN
)
529 if (pt
->nested_policy
) {
532 err
= nla_validate_array(nla_data(nla
), nla_len(nla
),
533 pt
->len
, pt
->nested_policy
,
534 extack
, validate
, depth
);
537 * return directly to preserve the inner
538 * error message/attribute pointer
546 if (validate
& NL_VALIDATE_UNSPEC
) {
547 NL_SET_ERR_MSG_ATTR(extack
, nla
,
548 "Unsupported attribute");
551 if (attrlen
< pt
->len
)
559 minlen
= nla_attr_minlen
[pt
->type
];
561 if (attrlen
< minlen
)
565 /* further validation */
566 switch (pt
->validation_type
) {
567 case NLA_VALIDATE_NONE
:
570 case NLA_VALIDATE_RANGE_PTR
:
571 case NLA_VALIDATE_RANGE
:
572 case NLA_VALIDATE_RANGE_WARN_TOO_LONG
:
573 case NLA_VALIDATE_MIN
:
574 case NLA_VALIDATE_MAX
:
575 err
= nla_validate_int_range(pt
, nla
, extack
, validate
);
579 case NLA_VALIDATE_MASK
:
580 err
= nla_validate_mask(pt
, nla
, extack
);
584 case NLA_VALIDATE_FUNCTION
:
586 err
= pt
->validate(nla
, extack
);
595 NL_SET_ERR_MSG_ATTR_POL(extack
, nla
, pt
,
596 "Attribute failed policy validation");
600 static int __nla_validate_parse(const struct nlattr
*head
, int len
, int maxtype
,
601 const struct nla_policy
*policy
,
602 unsigned int validate
,
603 struct netlink_ext_ack
*extack
,
604 struct nlattr
**tb
, unsigned int depth
)
606 const struct nlattr
*nla
;
609 if (depth
>= MAX_POLICY_RECURSION_DEPTH
) {
610 NL_SET_ERR_MSG(extack
,
611 "allowed policy recursion depth exceeded");
616 memset(tb
, 0, sizeof(struct nlattr
*) * (maxtype
+ 1));
618 nla_for_each_attr(nla
, head
, len
, rem
) {
619 u16 type
= nla_type(nla
);
621 if (type
== 0 || type
> maxtype
) {
622 if (validate
& NL_VALIDATE_MAXTYPE
) {
623 NL_SET_ERR_MSG_ATTR(extack
, nla
,
624 "Unknown attribute type");
629 type
= array_index_nospec(type
, maxtype
+ 1);
631 int err
= validate_nla(nla
, maxtype
, policy
,
632 validate
, extack
, depth
);
639 tb
[type
] = (struct nlattr
*)nla
;
642 if (unlikely(rem
> 0)) {
643 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
645 NL_SET_ERR_MSG(extack
, "bytes leftover after parsing attributes");
646 if (validate
& NL_VALIDATE_TRAILING
)
654 * __nla_validate - Validate a stream of attributes
655 * @head: head of attribute stream
656 * @len: length of attribute stream
657 * @maxtype: maximum attribute type to be expected
658 * @policy: validation policy
659 * @validate: validation strictness
660 * @extack: extended ACK report struct
662 * Validates all attributes in the specified attribute stream against the
663 * specified policy. Validation depends on the validate flags passed, see
664 * &enum netlink_validation for more details on that.
665 * See documentation of struct nla_policy for more details.
667 * Returns 0 on success or a negative error code.
669 int __nla_validate(const struct nlattr
*head
, int len
, int maxtype
,
670 const struct nla_policy
*policy
, unsigned int validate
,
671 struct netlink_ext_ack
*extack
)
673 return __nla_validate_parse(head
, len
, maxtype
, policy
, validate
,
676 EXPORT_SYMBOL(__nla_validate
);
679 * nla_policy_len - Determine the max. length of a policy
681 * @n: number of policies
683 * Determines the max. length of the policy. It is currently used
684 * to allocated Netlink buffers roughly the size of the actual
687 * Returns 0 on success or a negative error code.
690 nla_policy_len(const struct nla_policy
*p
, int n
)
694 for (i
= 0; i
< n
; i
++, p
++) {
696 len
+= nla_total_size(p
->len
);
697 else if (nla_attr_len
[p
->type
])
698 len
+= nla_total_size(nla_attr_len
[p
->type
]);
699 else if (nla_attr_minlen
[p
->type
])
700 len
+= nla_total_size(nla_attr_minlen
[p
->type
]);
705 EXPORT_SYMBOL(nla_policy_len
);
708 * __nla_parse - Parse a stream of attributes into a tb buffer
709 * @tb: destination array with maxtype+1 elements
710 * @maxtype: maximum attribute type to be expected
711 * @head: head of attribute stream
712 * @len: length of attribute stream
713 * @policy: validation policy
714 * @validate: validation strictness
715 * @extack: extended ACK pointer
717 * Parses a stream of attributes and stores a pointer to each attribute in
718 * the tb array accessible via the attribute type.
719 * Validation is controlled by the @validate parameter.
721 * Returns 0 on success or a negative error code.
723 int __nla_parse(struct nlattr
**tb
, int maxtype
,
724 const struct nlattr
*head
, int len
,
725 const struct nla_policy
*policy
, unsigned int validate
,
726 struct netlink_ext_ack
*extack
)
728 return __nla_validate_parse(head
, len
, maxtype
, policy
, validate
,
731 EXPORT_SYMBOL(__nla_parse
);
734 * nla_find - Find a specific attribute in a stream of attributes
735 * @head: head of attribute stream
736 * @len: length of attribute stream
737 * @attrtype: type of attribute to look for
739 * Returns the first attribute in the stream matching the specified type.
741 struct nlattr
*nla_find(const struct nlattr
*head
, int len
, int attrtype
)
743 const struct nlattr
*nla
;
746 nla_for_each_attr(nla
, head
, len
, rem
)
747 if (nla_type(nla
) == attrtype
)
748 return (struct nlattr
*)nla
;
752 EXPORT_SYMBOL(nla_find
);
755 * nla_strscpy - Copy string attribute payload into a sized buffer
756 * @dst: Where to copy the string to.
757 * @nla: Attribute to copy the string from.
758 * @dstsize: Size of destination buffer.
760 * Copies at most dstsize - 1 bytes into the destination buffer.
761 * Unlike strlcpy the destination buffer is always padded out.
764 * * srclen - Returns @nla length (not including the trailing %NUL).
765 * * -E2BIG - If @dstsize is 0 or greater than U16_MAX or @nla length greater
768 ssize_t
nla_strscpy(char *dst
, const struct nlattr
*nla
, size_t dstsize
)
770 size_t srclen
= nla_len(nla
);
771 char *src
= nla_data(nla
);
775 if (dstsize
== 0 || WARN_ON_ONCE(dstsize
> U16_MAX
))
778 if (srclen
> 0 && src
[srclen
- 1] == '\0')
781 if (srclen
>= dstsize
) {
789 memcpy(dst
, src
, len
);
790 /* Zero pad end of dst. */
791 memset(dst
+ len
, 0, dstsize
- len
);
795 EXPORT_SYMBOL(nla_strscpy
);
798 * nla_strdup - Copy string attribute payload into a newly allocated buffer
799 * @nla: attribute to copy the string from
800 * @flags: the type of memory to allocate (see kmalloc).
802 * Returns a pointer to the allocated buffer or NULL on error.
804 char *nla_strdup(const struct nlattr
*nla
, gfp_t flags
)
806 size_t srclen
= nla_len(nla
);
807 char *src
= nla_data(nla
), *dst
;
809 if (srclen
> 0 && src
[srclen
- 1] == '\0')
812 dst
= kmalloc(srclen
+ 1, flags
);
814 memcpy(dst
, src
, srclen
);
819 EXPORT_SYMBOL(nla_strdup
);
822 * nla_memcpy - Copy a netlink attribute into another memory area
823 * @dest: where to copy to memcpy
824 * @src: netlink attribute to copy from
825 * @count: size of the destination area
827 * Note: The number of bytes copied is limited by the length of
828 * attribute's payload. memcpy
830 * Returns the number of bytes copied.
832 int nla_memcpy(void *dest
, const struct nlattr
*src
, int count
)
834 int minlen
= min_t(int, count
, nla_len(src
));
836 memcpy(dest
, nla_data(src
), minlen
);
838 memset(dest
+ minlen
, 0, count
- minlen
);
842 EXPORT_SYMBOL(nla_memcpy
);
845 * nla_memcmp - Compare an attribute with sized memory area
846 * @nla: netlink attribute
848 * @size: size of memory area
850 int nla_memcmp(const struct nlattr
*nla
, const void *data
,
853 int d
= nla_len(nla
) - size
;
856 d
= memcmp(nla_data(nla
), data
, size
);
860 EXPORT_SYMBOL(nla_memcmp
);
863 * nla_strcmp - Compare a string attribute against a string
864 * @nla: netlink string attribute
865 * @str: another string
867 int nla_strcmp(const struct nlattr
*nla
, const char *str
)
869 int len
= strlen(str
);
870 char *buf
= nla_data(nla
);
871 int attrlen
= nla_len(nla
);
874 while (attrlen
> 0 && buf
[attrlen
- 1] == '\0')
879 d
= memcmp(nla_data(nla
), str
, len
);
883 EXPORT_SYMBOL(nla_strcmp
);
887 * __nla_reserve - reserve room for attribute on the skb
888 * @skb: socket buffer to reserve room on
889 * @attrtype: attribute type
890 * @attrlen: length of attribute payload
892 * Adds a netlink attribute header to a socket buffer and reserves
893 * room for the payload but does not copy it.
895 * The caller is responsible to ensure that the skb provides enough
896 * tailroom for the attribute header and payload.
898 struct nlattr
*__nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
902 nla
= skb_put(skb
, nla_total_size(attrlen
));
903 nla
->nla_type
= attrtype
;
904 nla
->nla_len
= nla_attr_size(attrlen
);
906 memset((unsigned char *) nla
+ nla
->nla_len
, 0, nla_padlen(attrlen
));
910 EXPORT_SYMBOL(__nla_reserve
);
913 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
914 * @skb: socket buffer to reserve room on
915 * @attrtype: attribute type
916 * @attrlen: length of attribute payload
917 * @padattr: attribute type for the padding
919 * Adds a netlink attribute header to a socket buffer and reserves
920 * room for the payload but does not copy it. It also ensure that this
921 * attribute will have a 64-bit aligned nla_data() area.
923 * The caller is responsible to ensure that the skb provides enough
924 * tailroom for the attribute header and payload.
926 struct nlattr
*__nla_reserve_64bit(struct sk_buff
*skb
, int attrtype
,
927 int attrlen
, int padattr
)
929 nla_align_64bit(skb
, padattr
);
931 return __nla_reserve(skb
, attrtype
, attrlen
);
933 EXPORT_SYMBOL(__nla_reserve_64bit
);
936 * __nla_reserve_nohdr - reserve room for attribute without header
937 * @skb: socket buffer to reserve room on
938 * @attrlen: length of attribute payload
940 * Reserves room for attribute payload without a header.
942 * The caller is responsible to ensure that the skb provides enough
943 * tailroom for the payload.
945 void *__nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
947 return skb_put_zero(skb
, NLA_ALIGN(attrlen
));
949 EXPORT_SYMBOL(__nla_reserve_nohdr
);
952 * nla_reserve - reserve room for attribute on the skb
953 * @skb: socket buffer to reserve room on
954 * @attrtype: attribute type
955 * @attrlen: length of attribute payload
957 * Adds a netlink attribute header to a socket buffer and reserves
958 * room for the payload but does not copy it.
960 * Returns NULL if the tailroom of the skb is insufficient to store
961 * the attribute header and payload.
963 struct nlattr
*nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
965 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
968 return __nla_reserve(skb
, attrtype
, attrlen
);
970 EXPORT_SYMBOL(nla_reserve
);
973 * nla_reserve_64bit - reserve room for attribute on the skb and align it
974 * @skb: socket buffer to reserve room on
975 * @attrtype: attribute type
976 * @attrlen: length of attribute payload
977 * @padattr: attribute type for the padding
979 * Adds a netlink attribute header to a socket buffer and reserves
980 * room for the payload but does not copy it. It also ensure that this
981 * attribute will have a 64-bit aligned nla_data() area.
983 * Returns NULL if the tailroom of the skb is insufficient to store
984 * the attribute header and payload.
986 struct nlattr
*nla_reserve_64bit(struct sk_buff
*skb
, int attrtype
, int attrlen
,
991 if (nla_need_padding_for_64bit(skb
))
992 len
= nla_total_size_64bit(attrlen
);
994 len
= nla_total_size(attrlen
);
995 if (unlikely(skb_tailroom(skb
) < len
))
998 return __nla_reserve_64bit(skb
, attrtype
, attrlen
, padattr
);
1000 EXPORT_SYMBOL(nla_reserve_64bit
);
1003 * nla_reserve_nohdr - reserve room for attribute without header
1004 * @skb: socket buffer to reserve room on
1005 * @attrlen: length of attribute payload
1007 * Reserves room for attribute payload without a header.
1009 * Returns NULL if the tailroom of the skb is insufficient to store
1010 * the attribute payload.
1012 void *nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
1014 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
1017 return __nla_reserve_nohdr(skb
, attrlen
);
1019 EXPORT_SYMBOL(nla_reserve_nohdr
);
1022 * __nla_put - Add a netlink attribute to a socket buffer
1023 * @skb: socket buffer to add attribute to
1024 * @attrtype: attribute type
1025 * @attrlen: length of attribute payload
1026 * @data: head of attribute payload
1028 * The caller is responsible to ensure that the skb provides enough
1029 * tailroom for the attribute header and payload.
1031 void __nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
,
1036 nla
= __nla_reserve(skb
, attrtype
, attrlen
);
1037 memcpy(nla_data(nla
), data
, attrlen
);
1039 EXPORT_SYMBOL(__nla_put
);
1042 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1043 * @skb: socket buffer to add attribute to
1044 * @attrtype: attribute type
1045 * @attrlen: length of attribute payload
1046 * @data: head of attribute payload
1047 * @padattr: attribute type for the padding
1049 * The caller is responsible to ensure that the skb provides enough
1050 * tailroom for the attribute header and payload.
1052 void __nla_put_64bit(struct sk_buff
*skb
, int attrtype
, int attrlen
,
1053 const void *data
, int padattr
)
1057 nla
= __nla_reserve_64bit(skb
, attrtype
, attrlen
, padattr
);
1058 memcpy(nla_data(nla
), data
, attrlen
);
1060 EXPORT_SYMBOL(__nla_put_64bit
);
1063 * __nla_put_nohdr - Add a netlink attribute without header
1064 * @skb: socket buffer to add attribute to
1065 * @attrlen: length of attribute payload
1066 * @data: head of attribute payload
1068 * The caller is responsible to ensure that the skb provides enough
1069 * tailroom for the attribute payload.
1071 void __nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
1075 start
= __nla_reserve_nohdr(skb
, attrlen
);
1076 memcpy(start
, data
, attrlen
);
1078 EXPORT_SYMBOL(__nla_put_nohdr
);
1081 * nla_put - Add a netlink attribute to a socket buffer
1082 * @skb: socket buffer to add attribute to
1083 * @attrtype: attribute type
1084 * @attrlen: length of attribute payload
1085 * @data: head of attribute payload
1087 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1088 * the attribute header and payload.
1090 int nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
1092 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
1095 __nla_put(skb
, attrtype
, attrlen
, data
);
1098 EXPORT_SYMBOL(nla_put
);
1101 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1102 * @skb: socket buffer to add attribute to
1103 * @attrtype: attribute type
1104 * @attrlen: length of attribute payload
1105 * @data: head of attribute payload
1106 * @padattr: attribute type for the padding
1108 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1109 * the attribute header and payload.
1111 int nla_put_64bit(struct sk_buff
*skb
, int attrtype
, int attrlen
,
1112 const void *data
, int padattr
)
1116 if (nla_need_padding_for_64bit(skb
))
1117 len
= nla_total_size_64bit(attrlen
);
1119 len
= nla_total_size(attrlen
);
1120 if (unlikely(skb_tailroom(skb
) < len
))
1123 __nla_put_64bit(skb
, attrtype
, attrlen
, data
, padattr
);
1126 EXPORT_SYMBOL(nla_put_64bit
);
1129 * nla_put_nohdr - Add a netlink attribute without header
1130 * @skb: socket buffer to add attribute to
1131 * @attrlen: length of attribute payload
1132 * @data: head of attribute payload
1134 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1135 * the attribute payload.
1137 int nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
1139 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
1142 __nla_put_nohdr(skb
, attrlen
, data
);
1145 EXPORT_SYMBOL(nla_put_nohdr
);
1148 * nla_append - Add a netlink attribute without header or padding
1149 * @skb: socket buffer to add attribute to
1150 * @attrlen: length of attribute payload
1151 * @data: head of attribute payload
1153 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1154 * the attribute payload.
1156 int nla_append(struct sk_buff
*skb
, int attrlen
, const void *data
)
1158 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
1161 skb_put_data(skb
, data
, attrlen
);
1164 EXPORT_SYMBOL(nla_append
);