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),
48 * Nested policies might refer back to the original
49 * policy in some cases, and userspace could try to
50 * abuse that and recurse by nesting in the right
51 * ways. Limit recursion to avoid this problem.
53 #define MAX_POLICY_RECURSION_DEPTH 10
55 static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
56 const struct nla_policy *policy,
57 unsigned int validate,
58 struct netlink_ext_ack *extack,
59 struct nlattr **tb, unsigned int depth);
61 static int validate_nla_bitfield32(const struct nlattr *nla,
62 const u32 valid_flags_mask)
64 const struct nla_bitfield32 *bf = nla_data(nla);
66 if (!valid_flags_mask)
69 /*disallow invalid bit selector */
70 if (bf->selector & ~valid_flags_mask)
73 /*disallow invalid bit values */
74 if (bf->value & ~valid_flags_mask)
77 /*disallow valid bit values that are not selected*/
78 if (bf->value & ~bf->selector)
84 static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
85 const struct nla_policy *policy,
86 struct netlink_ext_ack *extack,
87 unsigned int validate, unsigned int depth)
89 const struct nlattr *entry;
92 nla_for_each_attr(entry, head, len, rem) {
95 if (nla_len(entry) == 0)
98 if (nla_len(entry) < NLA_HDRLEN) {
99 NL_SET_ERR_MSG_ATTR_POL(extack, entry, policy,
100 "Array element too short");
104 ret = __nla_validate_parse(nla_data(entry), nla_len(entry),
105 maxtype, policy, validate, extack,
114 void nla_get_range_unsigned(const struct nla_policy *pt,
115 struct netlink_range_validation *range)
117 WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR &&
118 (pt->min < 0 || pt->max < 0));
128 range->max = U16_MAX;
131 range->max = U32_MAX;
135 range->max = U64_MAX;
142 switch (pt->validation_type) {
143 case NLA_VALIDATE_RANGE:
144 case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
145 range->min = pt->min;
146 range->max = pt->max;
148 case NLA_VALIDATE_RANGE_PTR:
151 case NLA_VALIDATE_MIN:
152 range->min = pt->min;
154 case NLA_VALIDATE_MAX:
155 range->max = pt->max;
162 static u64 nla_get_attr_bo(const struct nla_policy *pt,
163 const struct nlattr *nla)
167 if (pt->network_byte_order)
168 return ntohs(nla_get_be16(nla));
170 return nla_get_u16(nla);
172 if (pt->network_byte_order)
173 return ntohl(nla_get_be32(nla));
175 return nla_get_u32(nla);
177 if (pt->network_byte_order)
178 return be64_to_cpu(nla_get_be64(nla));
180 return nla_get_u64(nla);
187 static int nla_validate_range_unsigned(const struct nla_policy *pt,
188 const struct nlattr *nla,
189 struct netlink_ext_ack *extack,
190 unsigned int validate)
192 struct netlink_range_validation range;
197 value = nla_get_u8(nla);
202 value = nla_get_attr_bo(pt, nla);
205 value = nla_get_u64(nla);
208 value = nla_len(nla);
214 nla_get_range_unsigned(pt, &range);
216 if (pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG &&
217 pt->type == NLA_BINARY && value > range.max) {
218 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
219 current->comm, pt->type);
220 if (validate & NL_VALIDATE_STRICT_ATTRS) {
221 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
222 "invalid attribute length");
226 /* this assumes min <= max (don't validate against min) */
230 if (value < range.min || value > range.max) {
231 bool binary = pt->type == NLA_BINARY;
234 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
235 "binary attribute size out of range");
237 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
238 "integer out of range");
246 void nla_get_range_signed(const struct nla_policy *pt,
247 struct netlink_range_validation_signed *range)
255 range->min = S16_MIN;
256 range->max = S16_MAX;
259 range->min = S32_MIN;
260 range->max = S32_MAX;
263 range->min = S64_MIN;
264 range->max = S64_MAX;
271 switch (pt->validation_type) {
272 case NLA_VALIDATE_RANGE:
273 range->min = pt->min;
274 range->max = pt->max;
276 case NLA_VALIDATE_RANGE_PTR:
277 *range = *pt->range_signed;
279 case NLA_VALIDATE_MIN:
280 range->min = pt->min;
282 case NLA_VALIDATE_MAX:
283 range->max = pt->max;
290 static int nla_validate_int_range_signed(const struct nla_policy *pt,
291 const struct nlattr *nla,
292 struct netlink_ext_ack *extack)
294 struct netlink_range_validation_signed range;
299 value = nla_get_s8(nla);
302 value = nla_get_s16(nla);
305 value = nla_get_s32(nla);
308 value = nla_get_s64(nla);
314 nla_get_range_signed(pt, &range);
316 if (value < range.min || value > range.max) {
317 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
318 "integer out of range");
325 static int nla_validate_int_range(const struct nla_policy *pt,
326 const struct nlattr *nla,
327 struct netlink_ext_ack *extack,
328 unsigned int validate)
337 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);
372 if (value & ~(u64)pt->mask) {
373 NL_SET_ERR_MSG_ATTR(extack, nla, "reserved bit set");
380 static int validate_nla(const struct nlattr *nla, int maxtype,
381 const struct nla_policy *policy, unsigned int validate,
382 struct netlink_ext_ack *extack, unsigned int depth)
384 u16 strict_start_type = policy[0].strict_start_type;
385 const struct nla_policy *pt;
386 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
389 if (strict_start_type && type >= strict_start_type)
390 validate |= NL_VALIDATE_STRICT;
392 if (type <= 0 || type > maxtype)
397 BUG_ON(pt->type > NLA_TYPE_MAX);
399 if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) {
400 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
401 current->comm, type);
402 if (validate & NL_VALIDATE_STRICT_ATTRS) {
403 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
404 "invalid attribute length");
409 if (validate & NL_VALIDATE_NESTED) {
410 if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
411 !(nla->nla_type & NLA_F_NESTED)) {
412 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
413 "NLA_F_NESTED is missing");
416 if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
417 pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
418 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
419 "NLA_F_NESTED not expected");
426 if (extack && pt->reject_message) {
427 NL_SET_BAD_ATTR(extack, nla);
428 extack->_msg = pt->reject_message;
440 if (attrlen != sizeof(struct nla_bitfield32))
443 err = validate_nla_bitfield32(nla, pt->bitfield32_valid);
450 minlen = min_t(int, attrlen, pt->len + 1);
454 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
465 char *buf = nla_data(nla);
467 if (buf[attrlen - 1] == '\0')
470 if (attrlen > pt->len)
476 if (pt->len && attrlen > pt->len)
481 /* a nested attributes is allowed to be empty; if its not,
482 * it must have a size of at least NLA_HDRLEN.
486 if (attrlen < NLA_HDRLEN)
488 if (pt->nested_policy) {
489 err = __nla_validate_parse(nla_data(nla), nla_len(nla),
490 pt->len, pt->nested_policy,
491 validate, extack, NULL,
495 * return directly to preserve the inner
496 * error message/attribute pointer
502 case NLA_NESTED_ARRAY:
503 /* a nested array attribute is allowed to be empty; if its not,
504 * it must have a size of at least NLA_HDRLEN.
508 if (attrlen < NLA_HDRLEN)
510 if (pt->nested_policy) {
513 err = nla_validate_array(nla_data(nla), nla_len(nla),
514 pt->len, pt->nested_policy,
515 extack, validate, depth);
518 * return directly to preserve the inner
519 * error message/attribute pointer
527 if (validate & NL_VALIDATE_UNSPEC) {
528 NL_SET_ERR_MSG_ATTR(extack, nla,
529 "Unsupported attribute");
532 if (attrlen < pt->len)
540 minlen = nla_attr_minlen[pt->type];
542 if (attrlen < minlen)
546 /* further validation */
547 switch (pt->validation_type) {
548 case NLA_VALIDATE_NONE:
551 case NLA_VALIDATE_RANGE_PTR:
552 case NLA_VALIDATE_RANGE:
553 case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
554 case NLA_VALIDATE_MIN:
555 case NLA_VALIDATE_MAX:
556 err = nla_validate_int_range(pt, nla, extack, validate);
560 case NLA_VALIDATE_MASK:
561 err = nla_validate_mask(pt, nla, extack);
565 case NLA_VALIDATE_FUNCTION:
567 err = pt->validate(nla, extack);
576 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
577 "Attribute failed policy validation");
581 static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
582 const struct nla_policy *policy,
583 unsigned int validate,
584 struct netlink_ext_ack *extack,
585 struct nlattr **tb, unsigned int depth)
587 const struct nlattr *nla;
590 if (depth >= MAX_POLICY_RECURSION_DEPTH) {
591 NL_SET_ERR_MSG(extack,
592 "allowed policy recursion depth exceeded");
597 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
599 nla_for_each_attr(nla, head, len, rem) {
600 u16 type = nla_type(nla);
602 if (type == 0 || type > maxtype) {
603 if (validate & NL_VALIDATE_MAXTYPE) {
604 NL_SET_ERR_MSG_ATTR(extack, nla,
605 "Unknown attribute type");
611 int err = validate_nla(nla, maxtype, policy,
612 validate, extack, depth);
619 tb[type] = (struct nlattr *)nla;
622 if (unlikely(rem > 0)) {
623 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
625 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
626 if (validate & NL_VALIDATE_TRAILING)
634 * __nla_validate - Validate a stream of attributes
635 * @head: head of attribute stream
636 * @len: length of attribute stream
637 * @maxtype: maximum attribute type to be expected
638 * @policy: validation policy
639 * @validate: validation strictness
640 * @extack: extended ACK report struct
642 * Validates all attributes in the specified attribute stream against the
643 * specified policy. Validation depends on the validate flags passed, see
644 * &enum netlink_validation for more details on that.
645 * See documentation of struct nla_policy for more details.
647 * Returns 0 on success or a negative error code.
649 int __nla_validate(const struct nlattr *head, int len, int maxtype,
650 const struct nla_policy *policy, unsigned int validate,
651 struct netlink_ext_ack *extack)
653 return __nla_validate_parse(head, len, maxtype, policy, validate,
656 EXPORT_SYMBOL(__nla_validate);
659 * nla_policy_len - Determine the max. length of a policy
660 * @policy: policy to use
661 * @n: number of policies
663 * Determines the max. length of the policy. It is currently used
664 * to allocated Netlink buffers roughly the size of the actual
667 * Returns 0 on success or a negative error code.
670 nla_policy_len(const struct nla_policy *p, int n)
674 for (i = 0; i < n; i++, p++) {
676 len += nla_total_size(p->len);
677 else if (nla_attr_len[p->type])
678 len += nla_total_size(nla_attr_len[p->type]);
679 else if (nla_attr_minlen[p->type])
680 len += nla_total_size(nla_attr_minlen[p->type]);
685 EXPORT_SYMBOL(nla_policy_len);
688 * __nla_parse - Parse a stream of attributes into a tb buffer
689 * @tb: destination array with maxtype+1 elements
690 * @maxtype: maximum attribute type to be expected
691 * @head: head of attribute stream
692 * @len: length of attribute stream
693 * @policy: validation policy
694 * @validate: validation strictness
695 * @extack: extended ACK pointer
697 * Parses a stream of attributes and stores a pointer to each attribute in
698 * the tb array accessible via the attribute type.
699 * Validation is controlled by the @validate parameter.
701 * Returns 0 on success or a negative error code.
703 int __nla_parse(struct nlattr **tb, int maxtype,
704 const struct nlattr *head, int len,
705 const struct nla_policy *policy, unsigned int validate,
706 struct netlink_ext_ack *extack)
708 return __nla_validate_parse(head, len, maxtype, policy, validate,
711 EXPORT_SYMBOL(__nla_parse);
714 * nla_find - Find a specific attribute in a stream of attributes
715 * @head: head of attribute stream
716 * @len: length of attribute stream
717 * @attrtype: type of attribute to look for
719 * Returns the first attribute in the stream matching the specified type.
721 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
723 const struct nlattr *nla;
726 nla_for_each_attr(nla, head, len, rem)
727 if (nla_type(nla) == attrtype)
728 return (struct nlattr *)nla;
732 EXPORT_SYMBOL(nla_find);
735 * nla_strscpy - Copy string attribute payload into a sized buffer
736 * @dst: Where to copy the string to.
737 * @nla: Attribute to copy the string from.
738 * @dstsize: Size of destination buffer.
740 * Copies at most dstsize - 1 bytes into the destination buffer.
741 * Unlike strlcpy the destination buffer is always padded out.
744 * * srclen - Returns @nla length (not including the trailing %NUL).
745 * * -E2BIG - If @dstsize is 0 or greater than U16_MAX or @nla length greater
748 ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize)
750 size_t srclen = nla_len(nla);
751 char *src = nla_data(nla);
755 if (dstsize == 0 || WARN_ON_ONCE(dstsize > U16_MAX))
758 if (srclen > 0 && src[srclen - 1] == '\0')
761 if (srclen >= dstsize) {
769 memcpy(dst, src, len);
770 /* Zero pad end of dst. */
771 memset(dst + len, 0, dstsize - len);
775 EXPORT_SYMBOL(nla_strscpy);
778 * nla_strdup - Copy string attribute payload into a newly allocated buffer
779 * @nla: attribute to copy the string from
780 * @flags: the type of memory to allocate (see kmalloc).
782 * Returns a pointer to the allocated buffer or NULL on error.
784 char *nla_strdup(const struct nlattr *nla, gfp_t flags)
786 size_t srclen = nla_len(nla);
787 char *src = nla_data(nla), *dst;
789 if (srclen > 0 && src[srclen - 1] == '\0')
792 dst = kmalloc(srclen + 1, flags);
794 memcpy(dst, src, srclen);
799 EXPORT_SYMBOL(nla_strdup);
802 * nla_memcpy - Copy a netlink attribute into another memory area
803 * @dest: where to copy to memcpy
804 * @src: netlink attribute to copy from
805 * @count: size of the destination area
807 * Note: The number of bytes copied is limited by the length of
808 * attribute's payload. memcpy
810 * Returns the number of bytes copied.
812 int nla_memcpy(void *dest, const struct nlattr *src, int count)
814 int minlen = min_t(int, count, nla_len(src));
816 memcpy(dest, nla_data(src), minlen);
818 memset(dest + minlen, 0, count - minlen);
822 EXPORT_SYMBOL(nla_memcpy);
825 * nla_memcmp - Compare an attribute with sized memory area
826 * @nla: netlink attribute
828 * @size: size of memory area
830 int nla_memcmp(const struct nlattr *nla, const void *data,
833 int d = nla_len(nla) - size;
836 d = memcmp(nla_data(nla), data, size);
840 EXPORT_SYMBOL(nla_memcmp);
843 * nla_strcmp - Compare a string attribute against a string
844 * @nla: netlink string attribute
845 * @str: another string
847 int nla_strcmp(const struct nlattr *nla, const char *str)
849 int len = strlen(str);
850 char *buf = nla_data(nla);
851 int attrlen = nla_len(nla);
854 while (attrlen > 0 && buf[attrlen - 1] == '\0')
859 d = memcmp(nla_data(nla), str, len);
863 EXPORT_SYMBOL(nla_strcmp);
867 * __nla_reserve - reserve room for attribute on the skb
868 * @skb: socket buffer to reserve room on
869 * @attrtype: attribute type
870 * @attrlen: length of attribute payload
872 * Adds a netlink attribute header to a socket buffer and reserves
873 * room for the payload but does not copy it.
875 * The caller is responsible to ensure that the skb provides enough
876 * tailroom for the attribute header and payload.
878 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
882 nla = skb_put(skb, nla_total_size(attrlen));
883 nla->nla_type = attrtype;
884 nla->nla_len = nla_attr_size(attrlen);
886 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
890 EXPORT_SYMBOL(__nla_reserve);
893 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
894 * @skb: socket buffer to reserve room on
895 * @attrtype: attribute type
896 * @attrlen: length of attribute payload
897 * @padattr: attribute type for the padding
899 * Adds a netlink attribute header to a socket buffer and reserves
900 * room for the payload but does not copy it. It also ensure that this
901 * attribute will have a 64-bit aligned nla_data() area.
903 * The caller is responsible to ensure that the skb provides enough
904 * tailroom for the attribute header and payload.
906 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
907 int attrlen, int padattr)
909 nla_align_64bit(skb, padattr);
911 return __nla_reserve(skb, attrtype, attrlen);
913 EXPORT_SYMBOL(__nla_reserve_64bit);
916 * __nla_reserve_nohdr - reserve room for attribute without header
917 * @skb: socket buffer to reserve room on
918 * @attrlen: length of attribute payload
920 * Reserves room for attribute payload without a header.
922 * The caller is responsible to ensure that the skb provides enough
923 * tailroom for the payload.
925 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
927 return skb_put_zero(skb, NLA_ALIGN(attrlen));
929 EXPORT_SYMBOL(__nla_reserve_nohdr);
932 * nla_reserve - reserve room for attribute on the skb
933 * @skb: socket buffer to reserve room on
934 * @attrtype: attribute type
935 * @attrlen: length of attribute payload
937 * Adds a netlink attribute header to a socket buffer and reserves
938 * room for the payload but does not copy it.
940 * Returns NULL if the tailroom of the skb is insufficient to store
941 * the attribute header and payload.
943 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
945 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
948 return __nla_reserve(skb, attrtype, attrlen);
950 EXPORT_SYMBOL(nla_reserve);
953 * nla_reserve_64bit - reserve room for attribute on the skb and align it
954 * @skb: socket buffer to reserve room on
955 * @attrtype: attribute type
956 * @attrlen: length of attribute payload
957 * @padattr: attribute type for the padding
959 * Adds a netlink attribute header to a socket buffer and reserves
960 * room for the payload but does not copy it. It also ensure that this
961 * attribute will have a 64-bit aligned nla_data() area.
963 * Returns NULL if the tailroom of the skb is insufficient to store
964 * the attribute header and payload.
966 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
971 if (nla_need_padding_for_64bit(skb))
972 len = nla_total_size_64bit(attrlen);
974 len = nla_total_size(attrlen);
975 if (unlikely(skb_tailroom(skb) < len))
978 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
980 EXPORT_SYMBOL(nla_reserve_64bit);
983 * nla_reserve_nohdr - reserve room for attribute without header
984 * @skb: socket buffer to reserve room on
985 * @attrlen: length of attribute payload
987 * Reserves room for attribute payload without a header.
989 * Returns NULL if the tailroom of the skb is insufficient to store
990 * the attribute payload.
992 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
994 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
997 return __nla_reserve_nohdr(skb, attrlen);
999 EXPORT_SYMBOL(nla_reserve_nohdr);
1002 * __nla_put - Add a netlink attribute to a socket buffer
1003 * @skb: socket buffer to add attribute to
1004 * @attrtype: attribute type
1005 * @attrlen: length of attribute payload
1006 * @data: head of attribute payload
1008 * The caller is responsible to ensure that the skb provides enough
1009 * tailroom for the attribute header and payload.
1011 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
1016 nla = __nla_reserve(skb, attrtype, attrlen);
1017 memcpy(nla_data(nla), data, attrlen);
1019 EXPORT_SYMBOL(__nla_put);
1022 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1023 * @skb: socket buffer to add attribute to
1024 * @attrtype: attribute type
1025 * @attrlen: length of attribute payload
1026 * @data: head of attribute payload
1027 * @padattr: attribute type for the padding
1029 * The caller is responsible to ensure that the skb provides enough
1030 * tailroom for the attribute header and payload.
1032 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
1033 const void *data, int padattr)
1037 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
1038 memcpy(nla_data(nla), data, attrlen);
1040 EXPORT_SYMBOL(__nla_put_64bit);
1043 * __nla_put_nohdr - Add a netlink attribute without header
1044 * @skb: socket buffer to add attribute to
1045 * @attrlen: length of attribute payload
1046 * @data: head of attribute payload
1048 * The caller is responsible to ensure that the skb provides enough
1049 * tailroom for the attribute payload.
1051 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
1055 start = __nla_reserve_nohdr(skb, attrlen);
1056 memcpy(start, data, attrlen);
1058 EXPORT_SYMBOL(__nla_put_nohdr);
1061 * nla_put - Add a netlink attribute to a socket buffer
1062 * @skb: socket buffer to add attribute to
1063 * @attrtype: attribute type
1064 * @attrlen: length of attribute payload
1065 * @data: head of attribute payload
1067 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1068 * the attribute header and payload.
1070 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
1072 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
1075 __nla_put(skb, attrtype, attrlen, data);
1078 EXPORT_SYMBOL(nla_put);
1081 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1082 * @skb: socket buffer to add attribute to
1083 * @attrtype: attribute type
1084 * @attrlen: length of attribute payload
1085 * @data: head of attribute payload
1086 * @padattr: attribute type for the padding
1088 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1089 * the attribute header and payload.
1091 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
1092 const void *data, int padattr)
1096 if (nla_need_padding_for_64bit(skb))
1097 len = nla_total_size_64bit(attrlen);
1099 len = nla_total_size(attrlen);
1100 if (unlikely(skb_tailroom(skb) < len))
1103 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
1106 EXPORT_SYMBOL(nla_put_64bit);
1109 * nla_put_nohdr - Add a netlink attribute without header
1110 * @skb: socket buffer to add attribute to
1111 * @attrlen: length of attribute payload
1112 * @data: head of attribute payload
1114 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1115 * the attribute payload.
1117 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
1119 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
1122 __nla_put_nohdr(skb, attrlen, data);
1125 EXPORT_SYMBOL(nla_put_nohdr);
1128 * nla_append - Add a netlink attribute without header or padding
1129 * @skb: socket buffer to add attribute to
1130 * @attrlen: length of attribute payload
1131 * @data: head of attribute payload
1133 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1134 * the attribute payload.
1136 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
1138 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
1141 skb_put_data(skb, data, attrlen);
1144 EXPORT_SYMBOL(nla_append);