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));
129 range->max = U16_MAX;
133 range->max = U32_MAX;
137 range->max = U64_MAX;
144 switch (pt->validation_type) {
145 case NLA_VALIDATE_RANGE:
146 case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
147 range->min = pt->min;
148 range->max = pt->max;
150 case NLA_VALIDATE_RANGE_PTR:
153 case NLA_VALIDATE_MIN:
154 range->min = pt->min;
156 case NLA_VALIDATE_MAX:
157 range->max = pt->max;
164 static int nla_validate_range_unsigned(const struct nla_policy *pt,
165 const struct nlattr *nla,
166 struct netlink_ext_ack *extack,
167 unsigned int validate)
169 struct netlink_range_validation range;
174 value = nla_get_u8(nla);
177 value = nla_get_u16(nla);
180 value = nla_get_u32(nla);
183 value = nla_get_u64(nla);
186 value = nla_get_u64(nla);
189 value = nla_len(nla);
192 value = ntohs(nla_get_be16(nla));
195 value = ntohl(nla_get_be32(nla));
201 nla_get_range_unsigned(pt, &range);
203 if (pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG &&
204 pt->type == NLA_BINARY && value > range.max) {
205 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
206 current->comm, pt->type);
207 if (validate & NL_VALIDATE_STRICT_ATTRS) {
208 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
209 "invalid attribute length");
213 /* this assumes min <= max (don't validate against min) */
217 if (value < range.min || value > range.max) {
218 bool binary = pt->type == NLA_BINARY;
221 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
222 "binary attribute size out of range");
224 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
225 "integer out of range");
233 void nla_get_range_signed(const struct nla_policy *pt,
234 struct netlink_range_validation_signed *range)
242 range->min = S16_MIN;
243 range->max = S16_MAX;
246 range->min = S32_MIN;
247 range->max = S32_MAX;
250 range->min = S64_MIN;
251 range->max = S64_MAX;
258 switch (pt->validation_type) {
259 case NLA_VALIDATE_RANGE:
260 range->min = pt->min;
261 range->max = pt->max;
263 case NLA_VALIDATE_RANGE_PTR:
264 *range = *pt->range_signed;
266 case NLA_VALIDATE_MIN:
267 range->min = pt->min;
269 case NLA_VALIDATE_MAX:
270 range->max = pt->max;
277 static int nla_validate_int_range_signed(const struct nla_policy *pt,
278 const struct nlattr *nla,
279 struct netlink_ext_ack *extack)
281 struct netlink_range_validation_signed range;
286 value = nla_get_s8(nla);
289 value = nla_get_s16(nla);
292 value = nla_get_s32(nla);
295 value = nla_get_s64(nla);
301 nla_get_range_signed(pt, &range);
303 if (value < range.min || value > range.max) {
304 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
305 "integer out of range");
312 static int nla_validate_int_range(const struct nla_policy *pt,
313 const struct nlattr *nla,
314 struct netlink_ext_ack *extack,
315 unsigned int validate)
326 return nla_validate_range_unsigned(pt, nla, extack, validate);
331 return nla_validate_int_range_signed(pt, nla, extack);
338 static int nla_validate_mask(const struct nla_policy *pt,
339 const struct nlattr *nla,
340 struct netlink_ext_ack *extack)
346 value = nla_get_u8(nla);
349 value = nla_get_u16(nla);
352 value = nla_get_u32(nla);
355 value = nla_get_u64(nla);
361 if (value & ~(u64)pt->mask) {
362 NL_SET_ERR_MSG_ATTR(extack, nla, "reserved bit set");
369 static int validate_nla(const struct nlattr *nla, int maxtype,
370 const struct nla_policy *policy, unsigned int validate,
371 struct netlink_ext_ack *extack, unsigned int depth)
373 u16 strict_start_type = policy[0].strict_start_type;
374 const struct nla_policy *pt;
375 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
378 if (strict_start_type && type >= strict_start_type)
379 validate |= NL_VALIDATE_STRICT;
381 if (type <= 0 || type > maxtype)
386 BUG_ON(pt->type > NLA_TYPE_MAX);
388 if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) {
389 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
390 current->comm, type);
391 if (validate & NL_VALIDATE_STRICT_ATTRS) {
392 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
393 "invalid attribute length");
398 if (validate & NL_VALIDATE_NESTED) {
399 if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
400 !(nla->nla_type & NLA_F_NESTED)) {
401 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
402 "NLA_F_NESTED is missing");
405 if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
406 pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
407 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
408 "NLA_F_NESTED not expected");
415 if (extack && pt->reject_message) {
416 NL_SET_BAD_ATTR(extack, nla);
417 extack->_msg = pt->reject_message;
429 if (attrlen != sizeof(struct nla_bitfield32))
432 err = validate_nla_bitfield32(nla, pt->bitfield32_valid);
439 minlen = min_t(int, attrlen, pt->len + 1);
443 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
454 char *buf = nla_data(nla);
456 if (buf[attrlen - 1] == '\0')
459 if (attrlen > pt->len)
465 if (pt->len && attrlen > pt->len)
470 /* a nested attributes is allowed to be empty; if its not,
471 * it must have a size of at least NLA_HDRLEN.
475 if (attrlen < NLA_HDRLEN)
477 if (pt->nested_policy) {
478 err = __nla_validate_parse(nla_data(nla), nla_len(nla),
479 pt->len, pt->nested_policy,
480 validate, extack, NULL,
484 * return directly to preserve the inner
485 * error message/attribute pointer
491 case NLA_NESTED_ARRAY:
492 /* a nested array attribute is allowed to be empty; if its not,
493 * it must have a size of at least NLA_HDRLEN.
497 if (attrlen < NLA_HDRLEN)
499 if (pt->nested_policy) {
502 err = nla_validate_array(nla_data(nla), nla_len(nla),
503 pt->len, pt->nested_policy,
504 extack, validate, depth);
507 * return directly to preserve the inner
508 * error message/attribute pointer
516 if (validate & NL_VALIDATE_UNSPEC) {
517 NL_SET_ERR_MSG_ATTR(extack, nla,
518 "Unsupported attribute");
521 if (attrlen < pt->len)
529 minlen = nla_attr_minlen[pt->type];
531 if (attrlen < minlen)
535 /* further validation */
536 switch (pt->validation_type) {
537 case NLA_VALIDATE_NONE:
540 case NLA_VALIDATE_RANGE_PTR:
541 case NLA_VALIDATE_RANGE:
542 case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
543 case NLA_VALIDATE_MIN:
544 case NLA_VALIDATE_MAX:
545 err = nla_validate_int_range(pt, nla, extack, validate);
549 case NLA_VALIDATE_MASK:
550 err = nla_validate_mask(pt, nla, extack);
554 case NLA_VALIDATE_FUNCTION:
556 err = pt->validate(nla, extack);
565 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
566 "Attribute failed policy validation");
570 static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
571 const struct nla_policy *policy,
572 unsigned int validate,
573 struct netlink_ext_ack *extack,
574 struct nlattr **tb, unsigned int depth)
576 const struct nlattr *nla;
579 if (depth >= MAX_POLICY_RECURSION_DEPTH) {
580 NL_SET_ERR_MSG(extack,
581 "allowed policy recursion depth exceeded");
586 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
588 nla_for_each_attr(nla, head, len, rem) {
589 u16 type = nla_type(nla);
591 if (type == 0 || type > maxtype) {
592 if (validate & NL_VALIDATE_MAXTYPE) {
593 NL_SET_ERR_MSG_ATTR(extack, nla,
594 "Unknown attribute type");
600 int err = validate_nla(nla, maxtype, policy,
601 validate, extack, depth);
608 tb[type] = (struct nlattr *)nla;
611 if (unlikely(rem > 0)) {
612 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
614 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
615 if (validate & NL_VALIDATE_TRAILING)
623 * __nla_validate - Validate a stream of attributes
624 * @head: head of attribute stream
625 * @len: length of attribute stream
626 * @maxtype: maximum attribute type to be expected
627 * @policy: validation policy
628 * @validate: validation strictness
629 * @extack: extended ACK report struct
631 * Validates all attributes in the specified attribute stream against the
632 * specified policy. Validation depends on the validate flags passed, see
633 * &enum netlink_validation for more details on that.
634 * See documentation of struct nla_policy for more details.
636 * Returns 0 on success or a negative error code.
638 int __nla_validate(const struct nlattr *head, int len, int maxtype,
639 const struct nla_policy *policy, unsigned int validate,
640 struct netlink_ext_ack *extack)
642 return __nla_validate_parse(head, len, maxtype, policy, validate,
645 EXPORT_SYMBOL(__nla_validate);
648 * nla_policy_len - Determine the max. length of a policy
649 * @policy: policy to use
650 * @n: number of policies
652 * Determines the max. length of the policy. It is currently used
653 * to allocated Netlink buffers roughly the size of the actual
656 * Returns 0 on success or a negative error code.
659 nla_policy_len(const struct nla_policy *p, int n)
663 for (i = 0; i < n; i++, p++) {
665 len += nla_total_size(p->len);
666 else if (nla_attr_len[p->type])
667 len += nla_total_size(nla_attr_len[p->type]);
668 else if (nla_attr_minlen[p->type])
669 len += nla_total_size(nla_attr_minlen[p->type]);
674 EXPORT_SYMBOL(nla_policy_len);
677 * __nla_parse - Parse a stream of attributes into a tb buffer
678 * @tb: destination array with maxtype+1 elements
679 * @maxtype: maximum attribute type to be expected
680 * @head: head of attribute stream
681 * @len: length of attribute stream
682 * @policy: validation policy
683 * @validate: validation strictness
684 * @extack: extended ACK pointer
686 * Parses a stream of attributes and stores a pointer to each attribute in
687 * the tb array accessible via the attribute type.
688 * Validation is controlled by the @validate parameter.
690 * Returns 0 on success or a negative error code.
692 int __nla_parse(struct nlattr **tb, int maxtype,
693 const struct nlattr *head, int len,
694 const struct nla_policy *policy, unsigned int validate,
695 struct netlink_ext_ack *extack)
697 return __nla_validate_parse(head, len, maxtype, policy, validate,
700 EXPORT_SYMBOL(__nla_parse);
703 * nla_find - Find a specific attribute in a stream of attributes
704 * @head: head of attribute stream
705 * @len: length of attribute stream
706 * @attrtype: type of attribute to look for
708 * Returns the first attribute in the stream matching the specified type.
710 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
712 const struct nlattr *nla;
715 nla_for_each_attr(nla, head, len, rem)
716 if (nla_type(nla) == attrtype)
717 return (struct nlattr *)nla;
721 EXPORT_SYMBOL(nla_find);
724 * nla_strscpy - Copy string attribute payload into a sized buffer
725 * @dst: Where to copy the string to.
726 * @nla: Attribute to copy the string from.
727 * @dstsize: Size of destination buffer.
729 * Copies at most dstsize - 1 bytes into the destination buffer.
730 * Unlike strlcpy the destination buffer is always padded out.
733 * * srclen - Returns @nla length (not including the trailing %NUL).
734 * * -E2BIG - If @dstsize is 0 or greater than U16_MAX or @nla length greater
737 ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize)
739 size_t srclen = nla_len(nla);
740 char *src = nla_data(nla);
744 if (dstsize == 0 || WARN_ON_ONCE(dstsize > U16_MAX))
747 if (srclen > 0 && src[srclen - 1] == '\0')
750 if (srclen >= dstsize) {
758 memcpy(dst, src, len);
759 /* Zero pad end of dst. */
760 memset(dst + len, 0, dstsize - len);
764 EXPORT_SYMBOL(nla_strscpy);
767 * nla_strdup - Copy string attribute payload into a newly allocated buffer
768 * @nla: attribute to copy the string from
769 * @flags: the type of memory to allocate (see kmalloc).
771 * Returns a pointer to the allocated buffer or NULL on error.
773 char *nla_strdup(const struct nlattr *nla, gfp_t flags)
775 size_t srclen = nla_len(nla);
776 char *src = nla_data(nla), *dst;
778 if (srclen > 0 && src[srclen - 1] == '\0')
781 dst = kmalloc(srclen + 1, flags);
783 memcpy(dst, src, srclen);
788 EXPORT_SYMBOL(nla_strdup);
791 * nla_memcpy - Copy a netlink attribute into another memory area
792 * @dest: where to copy to memcpy
793 * @src: netlink attribute to copy from
794 * @count: size of the destination area
796 * Note: The number of bytes copied is limited by the length of
797 * attribute's payload. memcpy
799 * Returns the number of bytes copied.
801 int nla_memcpy(void *dest, const struct nlattr *src, int count)
803 int minlen = min_t(int, count, nla_len(src));
805 memcpy(dest, nla_data(src), minlen);
807 memset(dest + minlen, 0, count - minlen);
811 EXPORT_SYMBOL(nla_memcpy);
814 * nla_memcmp - Compare an attribute with sized memory area
815 * @nla: netlink attribute
817 * @size: size of memory area
819 int nla_memcmp(const struct nlattr *nla, const void *data,
822 int d = nla_len(nla) - size;
825 d = memcmp(nla_data(nla), data, size);
829 EXPORT_SYMBOL(nla_memcmp);
832 * nla_strcmp - Compare a string attribute against a string
833 * @nla: netlink string attribute
834 * @str: another string
836 int nla_strcmp(const struct nlattr *nla, const char *str)
838 int len = strlen(str);
839 char *buf = nla_data(nla);
840 int attrlen = nla_len(nla);
843 while (attrlen > 0 && buf[attrlen - 1] == '\0')
848 d = memcmp(nla_data(nla), str, len);
852 EXPORT_SYMBOL(nla_strcmp);
856 * __nla_reserve - reserve room for attribute on the skb
857 * @skb: socket buffer to reserve room on
858 * @attrtype: attribute type
859 * @attrlen: length of attribute payload
861 * Adds a netlink attribute header to a socket buffer and reserves
862 * room for the payload but does not copy it.
864 * The caller is responsible to ensure that the skb provides enough
865 * tailroom for the attribute header and payload.
867 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
871 nla = skb_put(skb, nla_total_size(attrlen));
872 nla->nla_type = attrtype;
873 nla->nla_len = nla_attr_size(attrlen);
875 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
879 EXPORT_SYMBOL(__nla_reserve);
882 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
883 * @skb: socket buffer to reserve room on
884 * @attrtype: attribute type
885 * @attrlen: length of attribute payload
886 * @padattr: attribute type for the padding
888 * Adds a netlink attribute header to a socket buffer and reserves
889 * room for the payload but does not copy it. It also ensure that this
890 * attribute will have a 64-bit aligned nla_data() area.
892 * The caller is responsible to ensure that the skb provides enough
893 * tailroom for the attribute header and payload.
895 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
896 int attrlen, int padattr)
898 nla_align_64bit(skb, padattr);
900 return __nla_reserve(skb, attrtype, attrlen);
902 EXPORT_SYMBOL(__nla_reserve_64bit);
905 * __nla_reserve_nohdr - reserve room for attribute without header
906 * @skb: socket buffer to reserve room on
907 * @attrlen: length of attribute payload
909 * Reserves room for attribute payload without a header.
911 * The caller is responsible to ensure that the skb provides enough
912 * tailroom for the payload.
914 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
916 return skb_put_zero(skb, NLA_ALIGN(attrlen));
918 EXPORT_SYMBOL(__nla_reserve_nohdr);
921 * nla_reserve - reserve room for attribute on the skb
922 * @skb: socket buffer to reserve room on
923 * @attrtype: attribute type
924 * @attrlen: length of attribute payload
926 * Adds a netlink attribute header to a socket buffer and reserves
927 * room for the payload but does not copy it.
929 * Returns NULL if the tailroom of the skb is insufficient to store
930 * the attribute header and payload.
932 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
934 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
937 return __nla_reserve(skb, attrtype, attrlen);
939 EXPORT_SYMBOL(nla_reserve);
942 * nla_reserve_64bit - reserve room for attribute on the skb and align it
943 * @skb: socket buffer to reserve room on
944 * @attrtype: attribute type
945 * @attrlen: length of attribute payload
946 * @padattr: attribute type for the padding
948 * Adds a netlink attribute header to a socket buffer and reserves
949 * room for the payload but does not copy it. It also ensure that this
950 * attribute will have a 64-bit aligned nla_data() area.
952 * Returns NULL if the tailroom of the skb is insufficient to store
953 * the attribute header and payload.
955 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
960 if (nla_need_padding_for_64bit(skb))
961 len = nla_total_size_64bit(attrlen);
963 len = nla_total_size(attrlen);
964 if (unlikely(skb_tailroom(skb) < len))
967 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
969 EXPORT_SYMBOL(nla_reserve_64bit);
972 * nla_reserve_nohdr - reserve room for attribute without header
973 * @skb: socket buffer to reserve room on
974 * @attrlen: length of attribute payload
976 * Reserves room for attribute payload without a header.
978 * Returns NULL if the tailroom of the skb is insufficient to store
979 * the attribute payload.
981 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
983 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
986 return __nla_reserve_nohdr(skb, attrlen);
988 EXPORT_SYMBOL(nla_reserve_nohdr);
991 * __nla_put - Add a netlink attribute to a socket buffer
992 * @skb: socket buffer to add attribute to
993 * @attrtype: attribute type
994 * @attrlen: length of attribute payload
995 * @data: head of attribute payload
997 * The caller is responsible to ensure that the skb provides enough
998 * tailroom for the attribute header and payload.
1000 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
1005 nla = __nla_reserve(skb, attrtype, attrlen);
1006 memcpy(nla_data(nla), data, attrlen);
1008 EXPORT_SYMBOL(__nla_put);
1011 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1012 * @skb: socket buffer to add attribute to
1013 * @attrtype: attribute type
1014 * @attrlen: length of attribute payload
1015 * @data: head of attribute payload
1016 * @padattr: attribute type for the padding
1018 * The caller is responsible to ensure that the skb provides enough
1019 * tailroom for the attribute header and payload.
1021 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
1022 const void *data, int padattr)
1026 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
1027 memcpy(nla_data(nla), data, attrlen);
1029 EXPORT_SYMBOL(__nla_put_64bit);
1032 * __nla_put_nohdr - Add a netlink attribute without header
1033 * @skb: socket buffer to add attribute to
1034 * @attrlen: length of attribute payload
1035 * @data: head of attribute payload
1037 * The caller is responsible to ensure that the skb provides enough
1038 * tailroom for the attribute payload.
1040 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
1044 start = __nla_reserve_nohdr(skb, attrlen);
1045 memcpy(start, data, attrlen);
1047 EXPORT_SYMBOL(__nla_put_nohdr);
1050 * nla_put - Add a netlink attribute to a socket buffer
1051 * @skb: socket buffer to add attribute to
1052 * @attrtype: attribute type
1053 * @attrlen: length of attribute payload
1054 * @data: head of attribute payload
1056 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1057 * the attribute header and payload.
1059 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
1061 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
1064 __nla_put(skb, attrtype, attrlen, data);
1067 EXPORT_SYMBOL(nla_put);
1070 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1071 * @skb: socket buffer to add attribute to
1072 * @attrtype: attribute type
1073 * @attrlen: length of attribute payload
1074 * @data: head of attribute payload
1075 * @padattr: attribute type for the padding
1077 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1078 * the attribute header and payload.
1080 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
1081 const void *data, int padattr)
1085 if (nla_need_padding_for_64bit(skb))
1086 len = nla_total_size_64bit(attrlen);
1088 len = nla_total_size(attrlen);
1089 if (unlikely(skb_tailroom(skb) < len))
1092 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
1095 EXPORT_SYMBOL(nla_put_64bit);
1098 * nla_put_nohdr - Add a netlink attribute without header
1099 * @skb: socket buffer to add attribute to
1100 * @attrlen: length of attribute payload
1101 * @data: head of attribute payload
1103 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1104 * the attribute payload.
1106 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
1108 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
1111 __nla_put_nohdr(skb, attrlen, data);
1114 EXPORT_SYMBOL(nla_put_nohdr);
1117 * nla_append - Add a netlink attribute without header or padding
1118 * @skb: socket buffer to add attribute to
1119 * @attrlen: length of attribute payload
1120 * @data: head of attribute payload
1122 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1123 * the attribute payload.
1125 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
1127 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
1130 skb_put_data(skb, data, attrlen);
1133 EXPORT_SYMBOL(nla_append);