GNU Linux-libre 5.4.274-gnu1
[releases.git] / lib / nlattr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NETLINK      Netlink attributes
4  *
5  *              Authors:        Thomas Graf <tgraf@suug.ch>
6  *                              Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7  */
8
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>
18
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.
23  */
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),
33 };
34
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),
46 };
47
48 static int validate_nla_bitfield32(const struct nlattr *nla,
49                                    const u32 *valid_flags_mask)
50 {
51         const struct nla_bitfield32 *bf = nla_data(nla);
52
53         if (!valid_flags_mask)
54                 return -EINVAL;
55
56         /*disallow invalid bit selector */
57         if (bf->selector & ~*valid_flags_mask)
58                 return -EINVAL;
59
60         /*disallow invalid bit values */
61         if (bf->value & ~*valid_flags_mask)
62                 return -EINVAL;
63
64         /*disallow valid bit values that are not selected*/
65         if (bf->value & ~bf->selector)
66                 return -EINVAL;
67
68         return 0;
69 }
70
71 static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
72                               const struct nla_policy *policy,
73                               struct netlink_ext_ack *extack,
74                               unsigned int validate)
75 {
76         const struct nlattr *entry;
77         int rem;
78
79         nla_for_each_attr(entry, head, len, rem) {
80                 int ret;
81
82                 if (nla_len(entry) == 0)
83                         continue;
84
85                 if (nla_len(entry) < NLA_HDRLEN) {
86                         NL_SET_ERR_MSG_ATTR(extack, entry,
87                                             "Array element too short");
88                         return -ERANGE;
89                 }
90
91                 ret = __nla_validate(nla_data(entry), nla_len(entry),
92                                      maxtype, policy, validate, extack);
93                 if (ret < 0)
94                         return ret;
95         }
96
97         return 0;
98 }
99
100 static int nla_validate_int_range(const struct nla_policy *pt,
101                                   const struct nlattr *nla,
102                                   struct netlink_ext_ack *extack)
103 {
104         bool validate_min, validate_max;
105         s64 value;
106
107         validate_min = pt->validation_type == NLA_VALIDATE_RANGE ||
108                        pt->validation_type == NLA_VALIDATE_MIN;
109         validate_max = pt->validation_type == NLA_VALIDATE_RANGE ||
110                        pt->validation_type == NLA_VALIDATE_MAX;
111
112         switch (pt->type) {
113         case NLA_U8:
114                 value = nla_get_u8(nla);
115                 break;
116         case NLA_U16:
117                 value = nla_get_u16(nla);
118                 break;
119         case NLA_U32:
120                 value = nla_get_u32(nla);
121                 break;
122         case NLA_S8:
123                 value = nla_get_s8(nla);
124                 break;
125         case NLA_S16:
126                 value = nla_get_s16(nla);
127                 break;
128         case NLA_S32:
129                 value = nla_get_s32(nla);
130                 break;
131         case NLA_S64:
132                 value = nla_get_s64(nla);
133                 break;
134         case NLA_U64:
135                 /* treat this one specially, since it may not fit into s64 */
136                 if ((validate_min && nla_get_u64(nla) < pt->min) ||
137                     (validate_max && nla_get_u64(nla) > pt->max)) {
138                         NL_SET_ERR_MSG_ATTR(extack, nla,
139                                             "integer out of range");
140                         return -ERANGE;
141                 }
142                 return 0;
143         default:
144                 WARN_ON(1);
145                 return -EINVAL;
146         }
147
148         if ((validate_min && value < pt->min) ||
149             (validate_max && value > pt->max)) {
150                 NL_SET_ERR_MSG_ATTR(extack, nla,
151                                     "integer out of range");
152                 return -ERANGE;
153         }
154
155         return 0;
156 }
157
158 static int validate_nla(const struct nlattr *nla, int maxtype,
159                         const struct nla_policy *policy, unsigned int validate,
160                         struct netlink_ext_ack *extack)
161 {
162         u16 strict_start_type = policy[0].strict_start_type;
163         const struct nla_policy *pt;
164         int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
165         int err = -ERANGE;
166
167         if (strict_start_type && type >= strict_start_type)
168                 validate |= NL_VALIDATE_STRICT;
169
170         if (type <= 0 || type > maxtype)
171                 return 0;
172
173         type = array_index_nospec(type, maxtype + 1);
174         pt = &policy[type];
175
176         BUG_ON(pt->type > NLA_TYPE_MAX);
177
178         if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) ||
179             (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) {
180                 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
181                                     current->comm, type);
182                 if (validate & NL_VALIDATE_STRICT_ATTRS) {
183                         NL_SET_ERR_MSG_ATTR(extack, nla,
184                                             "invalid attribute length");
185                         return -EINVAL;
186                 }
187         }
188
189         if (validate & NL_VALIDATE_NESTED) {
190                 if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
191                     !(nla->nla_type & NLA_F_NESTED)) {
192                         NL_SET_ERR_MSG_ATTR(extack, nla,
193                                             "NLA_F_NESTED is missing");
194                         return -EINVAL;
195                 }
196                 if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
197                     pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
198                         NL_SET_ERR_MSG_ATTR(extack, nla,
199                                             "NLA_F_NESTED not expected");
200                         return -EINVAL;
201                 }
202         }
203
204         switch (pt->type) {
205         case NLA_EXACT_LEN:
206                 if (attrlen != pt->len)
207                         goto out_err;
208                 break;
209
210         case NLA_REJECT:
211                 if (extack && pt->validation_data) {
212                         NL_SET_BAD_ATTR(extack, nla);
213                         extack->_msg = pt->validation_data;
214                         return -EINVAL;
215                 }
216                 err = -EINVAL;
217                 goto out_err;
218
219         case NLA_FLAG:
220                 if (attrlen > 0)
221                         goto out_err;
222                 break;
223
224         case NLA_BITFIELD32:
225                 if (attrlen != sizeof(struct nla_bitfield32))
226                         goto out_err;
227
228                 err = validate_nla_bitfield32(nla, pt->validation_data);
229                 if (err)
230                         goto out_err;
231                 break;
232
233         case NLA_NUL_STRING:
234                 if (pt->len)
235                         minlen = min_t(int, attrlen, pt->len + 1);
236                 else
237                         minlen = attrlen;
238
239                 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
240                         err = -EINVAL;
241                         goto out_err;
242                 }
243                 /* fall through */
244
245         case NLA_STRING:
246                 if (attrlen < 1)
247                         goto out_err;
248
249                 if (pt->len) {
250                         char *buf = nla_data(nla);
251
252                         if (buf[attrlen - 1] == '\0')
253                                 attrlen--;
254
255                         if (attrlen > pt->len)
256                                 goto out_err;
257                 }
258                 break;
259
260         case NLA_BINARY:
261                 if (pt->len && attrlen > pt->len)
262                         goto out_err;
263                 break;
264
265         case NLA_NESTED:
266                 /* a nested attributes is allowed to be empty; if its not,
267                  * it must have a size of at least NLA_HDRLEN.
268                  */
269                 if (attrlen == 0)
270                         break;
271                 if (attrlen < NLA_HDRLEN)
272                         goto out_err;
273                 if (pt->validation_data) {
274                         err = __nla_validate(nla_data(nla), nla_len(nla), pt->len,
275                                              pt->validation_data, validate,
276                                              extack);
277                         if (err < 0) {
278                                 /*
279                                  * return directly to preserve the inner
280                                  * error message/attribute pointer
281                                  */
282                                 return err;
283                         }
284                 }
285                 break;
286         case NLA_NESTED_ARRAY:
287                 /* a nested array attribute is allowed to be empty; if its not,
288                  * it must have a size of at least NLA_HDRLEN.
289                  */
290                 if (attrlen == 0)
291                         break;
292                 if (attrlen < NLA_HDRLEN)
293                         goto out_err;
294                 if (pt->validation_data) {
295                         int err;
296
297                         err = nla_validate_array(nla_data(nla), nla_len(nla),
298                                                  pt->len, pt->validation_data,
299                                                  extack, validate);
300                         if (err < 0) {
301                                 /*
302                                  * return directly to preserve the inner
303                                  * error message/attribute pointer
304                                  */
305                                 return err;
306                         }
307                 }
308                 break;
309
310         case NLA_UNSPEC:
311                 if (validate & NL_VALIDATE_UNSPEC) {
312                         NL_SET_ERR_MSG_ATTR(extack, nla,
313                                             "Unsupported attribute");
314                         return -EINVAL;
315                 }
316                 /* fall through */
317         case NLA_MIN_LEN:
318                 if (attrlen < pt->len)
319                         goto out_err;
320                 break;
321
322         default:
323                 if (pt->len)
324                         minlen = pt->len;
325                 else
326                         minlen = nla_attr_minlen[pt->type];
327
328                 if (attrlen < minlen)
329                         goto out_err;
330         }
331
332         /* further validation */
333         switch (pt->validation_type) {
334         case NLA_VALIDATE_NONE:
335                 /* nothing to do */
336                 break;
337         case NLA_VALIDATE_RANGE:
338         case NLA_VALIDATE_MIN:
339         case NLA_VALIDATE_MAX:
340                 err = nla_validate_int_range(pt, nla, extack);
341                 if (err)
342                         return err;
343                 break;
344         case NLA_VALIDATE_FUNCTION:
345                 if (pt->validate) {
346                         err = pt->validate(nla, extack);
347                         if (err)
348                                 return err;
349                 }
350                 break;
351         }
352
353         return 0;
354 out_err:
355         NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation");
356         return err;
357 }
358
359 static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
360                                 const struct nla_policy *policy,
361                                 unsigned int validate,
362                                 struct netlink_ext_ack *extack,
363                                 struct nlattr **tb)
364 {
365         const struct nlattr *nla;
366         int rem;
367
368         if (tb)
369                 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
370
371         nla_for_each_attr(nla, head, len, rem) {
372                 u16 type = nla_type(nla);
373
374                 if (type == 0 || type > maxtype) {
375                         if (validate & NL_VALIDATE_MAXTYPE) {
376                                 NL_SET_ERR_MSG_ATTR(extack, nla,
377                                                     "Unknown attribute type");
378                                 return -EINVAL;
379                         }
380                         continue;
381                 }
382                 type = array_index_nospec(type, maxtype + 1);
383                 if (policy) {
384                         int err = validate_nla(nla, maxtype, policy,
385                                                validate, extack);
386
387                         if (err < 0)
388                                 return err;
389                 }
390
391                 if (tb)
392                         tb[type] = (struct nlattr *)nla;
393         }
394
395         if (unlikely(rem > 0)) {
396                 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
397                                     rem, current->comm);
398                 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
399                 if (validate & NL_VALIDATE_TRAILING)
400                         return -EINVAL;
401         }
402
403         return 0;
404 }
405
406 /**
407  * __nla_validate - Validate a stream of attributes
408  * @head: head of attribute stream
409  * @len: length of attribute stream
410  * @maxtype: maximum attribute type to be expected
411  * @policy: validation policy
412  * @validate: validation strictness
413  * @extack: extended ACK report struct
414  *
415  * Validates all attributes in the specified attribute stream against the
416  * specified policy. Validation depends on the validate flags passed, see
417  * &enum netlink_validation for more details on that.
418  * See documenation of struct nla_policy for more details.
419  *
420  * Returns 0 on success or a negative error code.
421  */
422 int __nla_validate(const struct nlattr *head, int len, int maxtype,
423                    const struct nla_policy *policy, unsigned int validate,
424                    struct netlink_ext_ack *extack)
425 {
426         return __nla_validate_parse(head, len, maxtype, policy, validate,
427                                     extack, NULL);
428 }
429 EXPORT_SYMBOL(__nla_validate);
430
431 /**
432  * nla_policy_len - Determin the max. length of a policy
433  * @policy: policy to use
434  * @n: number of policies
435  *
436  * Determines the max. length of the policy.  It is currently used
437  * to allocated Netlink buffers roughly the size of the actual
438  * message.
439  *
440  * Returns 0 on success or a negative error code.
441  */
442 int
443 nla_policy_len(const struct nla_policy *p, int n)
444 {
445         int i, len = 0;
446
447         for (i = 0; i < n; i++, p++) {
448                 if (p->len)
449                         len += nla_total_size(p->len);
450                 else if (nla_attr_len[p->type])
451                         len += nla_total_size(nla_attr_len[p->type]);
452                 else if (nla_attr_minlen[p->type])
453                         len += nla_total_size(nla_attr_minlen[p->type]);
454         }
455
456         return len;
457 }
458 EXPORT_SYMBOL(nla_policy_len);
459
460 /**
461  * __nla_parse - Parse a stream of attributes into a tb buffer
462  * @tb: destination array with maxtype+1 elements
463  * @maxtype: maximum attribute type to be expected
464  * @head: head of attribute stream
465  * @len: length of attribute stream
466  * @policy: validation policy
467  * @validate: validation strictness
468  * @extack: extended ACK pointer
469  *
470  * Parses a stream of attributes and stores a pointer to each attribute in
471  * the tb array accessible via the attribute type.
472  * Validation is controlled by the @validate parameter.
473  *
474  * Returns 0 on success or a negative error code.
475  */
476 int __nla_parse(struct nlattr **tb, int maxtype,
477                 const struct nlattr *head, int len,
478                 const struct nla_policy *policy, unsigned int validate,
479                 struct netlink_ext_ack *extack)
480 {
481         return __nla_validate_parse(head, len, maxtype, policy, validate,
482                                     extack, tb);
483 }
484 EXPORT_SYMBOL(__nla_parse);
485
486 /**
487  * nla_find - Find a specific attribute in a stream of attributes
488  * @head: head of attribute stream
489  * @len: length of attribute stream
490  * @attrtype: type of attribute to look for
491  *
492  * Returns the first attribute in the stream matching the specified type.
493  */
494 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
495 {
496         const struct nlattr *nla;
497         int rem;
498
499         nla_for_each_attr(nla, head, len, rem)
500                 if (nla_type(nla) == attrtype)
501                         return (struct nlattr *)nla;
502
503         return NULL;
504 }
505 EXPORT_SYMBOL(nla_find);
506
507 /**
508  * nla_strlcpy - Copy string attribute payload into a sized buffer
509  * @dst: where to copy the string to
510  * @nla: attribute to copy the string from
511  * @dstsize: size of destination buffer
512  *
513  * Copies at most dstsize - 1 bytes into the destination buffer.
514  * The result is always a valid NUL-terminated string. Unlike
515  * strlcpy the destination buffer is always padded out.
516  *
517  * Returns the length of the source buffer.
518  */
519 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
520 {
521         size_t srclen = nla_len(nla);
522         char *src = nla_data(nla);
523
524         if (srclen > 0 && src[srclen - 1] == '\0')
525                 srclen--;
526
527         if (dstsize > 0) {
528                 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
529
530                 memset(dst, 0, dstsize);
531                 memcpy(dst, src, len);
532         }
533
534         return srclen;
535 }
536 EXPORT_SYMBOL(nla_strlcpy);
537
538 /**
539  * nla_strdup - Copy string attribute payload into a newly allocated buffer
540  * @nla: attribute to copy the string from
541  * @flags: the type of memory to allocate (see kmalloc).
542  *
543  * Returns a pointer to the allocated buffer or NULL on error.
544  */
545 char *nla_strdup(const struct nlattr *nla, gfp_t flags)
546 {
547         size_t srclen = nla_len(nla);
548         char *src = nla_data(nla), *dst;
549
550         if (srclen > 0 && src[srclen - 1] == '\0')
551                 srclen--;
552
553         dst = kmalloc(srclen + 1, flags);
554         if (dst != NULL) {
555                 memcpy(dst, src, srclen);
556                 dst[srclen] = '\0';
557         }
558         return dst;
559 }
560 EXPORT_SYMBOL(nla_strdup);
561
562 /**
563  * nla_memcpy - Copy a netlink attribute into another memory area
564  * @dest: where to copy to memcpy
565  * @src: netlink attribute to copy from
566  * @count: size of the destination area
567  *
568  * Note: The number of bytes copied is limited by the length of
569  *       attribute's payload. memcpy
570  *
571  * Returns the number of bytes copied.
572  */
573 int nla_memcpy(void *dest, const struct nlattr *src, int count)
574 {
575         int minlen = min_t(int, count, nla_len(src));
576
577         memcpy(dest, nla_data(src), minlen);
578         if (count > minlen)
579                 memset(dest + minlen, 0, count - minlen);
580
581         return minlen;
582 }
583 EXPORT_SYMBOL(nla_memcpy);
584
585 /**
586  * nla_memcmp - Compare an attribute with sized memory area
587  * @nla: netlink attribute
588  * @data: memory area
589  * @size: size of memory area
590  */
591 int nla_memcmp(const struct nlattr *nla, const void *data,
592                              size_t size)
593 {
594         int d = nla_len(nla) - size;
595
596         if (d == 0)
597                 d = memcmp(nla_data(nla), data, size);
598
599         return d;
600 }
601 EXPORT_SYMBOL(nla_memcmp);
602
603 /**
604  * nla_strcmp - Compare a string attribute against a string
605  * @nla: netlink string attribute
606  * @str: another string
607  */
608 int nla_strcmp(const struct nlattr *nla, const char *str)
609 {
610         int len = strlen(str);
611         char *buf = nla_data(nla);
612         int attrlen = nla_len(nla);
613         int d;
614
615         while (attrlen > 0 && buf[attrlen - 1] == '\0')
616                 attrlen--;
617
618         d = attrlen - len;
619         if (d == 0)
620                 d = memcmp(nla_data(nla), str, len);
621
622         return d;
623 }
624 EXPORT_SYMBOL(nla_strcmp);
625
626 #ifdef CONFIG_NET
627 /**
628  * __nla_reserve - reserve room for attribute on the skb
629  * @skb: socket buffer to reserve room on
630  * @attrtype: attribute type
631  * @attrlen: length of attribute payload
632  *
633  * Adds a netlink attribute header to a socket buffer and reserves
634  * room for the payload but does not copy it.
635  *
636  * The caller is responsible to ensure that the skb provides enough
637  * tailroom for the attribute header and payload.
638  */
639 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
640 {
641         struct nlattr *nla;
642
643         nla = skb_put(skb, nla_total_size(attrlen));
644         nla->nla_type = attrtype;
645         nla->nla_len = nla_attr_size(attrlen);
646
647         memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
648
649         return nla;
650 }
651 EXPORT_SYMBOL(__nla_reserve);
652
653 /**
654  * __nla_reserve_64bit - reserve room for attribute on the skb and align it
655  * @skb: socket buffer to reserve room on
656  * @attrtype: attribute type
657  * @attrlen: length of attribute payload
658  * @padattr: attribute type for the padding
659  *
660  * Adds a netlink attribute header to a socket buffer and reserves
661  * room for the payload but does not copy it. It also ensure that this
662  * attribute will have a 64-bit aligned nla_data() area.
663  *
664  * The caller is responsible to ensure that the skb provides enough
665  * tailroom for the attribute header and payload.
666  */
667 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
668                                    int attrlen, int padattr)
669 {
670         if (nla_need_padding_for_64bit(skb))
671                 nla_align_64bit(skb, padattr);
672
673         return __nla_reserve(skb, attrtype, attrlen);
674 }
675 EXPORT_SYMBOL(__nla_reserve_64bit);
676
677 /**
678  * __nla_reserve_nohdr - reserve room for attribute without header
679  * @skb: socket buffer to reserve room on
680  * @attrlen: length of attribute payload
681  *
682  * Reserves room for attribute payload without a header.
683  *
684  * The caller is responsible to ensure that the skb provides enough
685  * tailroom for the payload.
686  */
687 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
688 {
689         return skb_put_zero(skb, NLA_ALIGN(attrlen));
690 }
691 EXPORT_SYMBOL(__nla_reserve_nohdr);
692
693 /**
694  * nla_reserve - reserve room for attribute on the skb
695  * @skb: socket buffer to reserve room on
696  * @attrtype: attribute type
697  * @attrlen: length of attribute payload
698  *
699  * Adds a netlink attribute header to a socket buffer and reserves
700  * room for the payload but does not copy it.
701  *
702  * Returns NULL if the tailroom of the skb is insufficient to store
703  * the attribute header and payload.
704  */
705 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
706 {
707         if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
708                 return NULL;
709
710         return __nla_reserve(skb, attrtype, attrlen);
711 }
712 EXPORT_SYMBOL(nla_reserve);
713
714 /**
715  * nla_reserve_64bit - reserve room for attribute on the skb and align it
716  * @skb: socket buffer to reserve room on
717  * @attrtype: attribute type
718  * @attrlen: length of attribute payload
719  * @padattr: attribute type for the padding
720  *
721  * Adds a netlink attribute header to a socket buffer and reserves
722  * room for the payload but does not copy it. It also ensure that this
723  * attribute will have a 64-bit aligned nla_data() area.
724  *
725  * Returns NULL if the tailroom of the skb is insufficient to store
726  * the attribute header and payload.
727  */
728 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
729                                  int padattr)
730 {
731         size_t len;
732
733         if (nla_need_padding_for_64bit(skb))
734                 len = nla_total_size_64bit(attrlen);
735         else
736                 len = nla_total_size(attrlen);
737         if (unlikely(skb_tailroom(skb) < len))
738                 return NULL;
739
740         return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
741 }
742 EXPORT_SYMBOL(nla_reserve_64bit);
743
744 /**
745  * nla_reserve_nohdr - reserve room for attribute without header
746  * @skb: socket buffer to reserve room on
747  * @attrlen: length of attribute payload
748  *
749  * Reserves room for attribute payload without a header.
750  *
751  * Returns NULL if the tailroom of the skb is insufficient to store
752  * the attribute payload.
753  */
754 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
755 {
756         if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
757                 return NULL;
758
759         return __nla_reserve_nohdr(skb, attrlen);
760 }
761 EXPORT_SYMBOL(nla_reserve_nohdr);
762
763 /**
764  * __nla_put - Add a netlink attribute to a socket buffer
765  * @skb: socket buffer to add attribute to
766  * @attrtype: attribute type
767  * @attrlen: length of attribute payload
768  * @data: head of attribute payload
769  *
770  * The caller is responsible to ensure that the skb provides enough
771  * tailroom for the attribute header and payload.
772  */
773 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
774                              const void *data)
775 {
776         struct nlattr *nla;
777
778         nla = __nla_reserve(skb, attrtype, attrlen);
779         memcpy(nla_data(nla), data, attrlen);
780 }
781 EXPORT_SYMBOL(__nla_put);
782
783 /**
784  * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
785  * @skb: socket buffer to add attribute to
786  * @attrtype: attribute type
787  * @attrlen: length of attribute payload
788  * @data: head of attribute payload
789  * @padattr: attribute type for the padding
790  *
791  * The caller is responsible to ensure that the skb provides enough
792  * tailroom for the attribute header and payload.
793  */
794 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
795                      const void *data, int padattr)
796 {
797         struct nlattr *nla;
798
799         nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
800         memcpy(nla_data(nla), data, attrlen);
801 }
802 EXPORT_SYMBOL(__nla_put_64bit);
803
804 /**
805  * __nla_put_nohdr - Add a netlink attribute without header
806  * @skb: socket buffer to add attribute to
807  * @attrlen: length of attribute payload
808  * @data: head of attribute payload
809  *
810  * The caller is responsible to ensure that the skb provides enough
811  * tailroom for the attribute payload.
812  */
813 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
814 {
815         void *start;
816
817         start = __nla_reserve_nohdr(skb, attrlen);
818         memcpy(start, data, attrlen);
819 }
820 EXPORT_SYMBOL(__nla_put_nohdr);
821
822 /**
823  * nla_put - Add a netlink attribute to a socket buffer
824  * @skb: socket buffer to add attribute to
825  * @attrtype: attribute type
826  * @attrlen: length of attribute payload
827  * @data: head of attribute payload
828  *
829  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
830  * the attribute header and payload.
831  */
832 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
833 {
834         if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
835                 return -EMSGSIZE;
836
837         __nla_put(skb, attrtype, attrlen, data);
838         return 0;
839 }
840 EXPORT_SYMBOL(nla_put);
841
842 /**
843  * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
844  * @skb: socket buffer to add attribute to
845  * @attrtype: attribute type
846  * @attrlen: length of attribute payload
847  * @data: head of attribute payload
848  * @padattr: attribute type for the padding
849  *
850  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
851  * the attribute header and payload.
852  */
853 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
854                   const void *data, int padattr)
855 {
856         size_t len;
857
858         if (nla_need_padding_for_64bit(skb))
859                 len = nla_total_size_64bit(attrlen);
860         else
861                 len = nla_total_size(attrlen);
862         if (unlikely(skb_tailroom(skb) < len))
863                 return -EMSGSIZE;
864
865         __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
866         return 0;
867 }
868 EXPORT_SYMBOL(nla_put_64bit);
869
870 /**
871  * nla_put_nohdr - Add a netlink attribute without header
872  * @skb: socket buffer to add attribute to
873  * @attrlen: length of attribute payload
874  * @data: head of attribute payload
875  *
876  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
877  * the attribute payload.
878  */
879 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
880 {
881         if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
882                 return -EMSGSIZE;
883
884         __nla_put_nohdr(skb, attrlen, data);
885         return 0;
886 }
887 EXPORT_SYMBOL(nla_put_nohdr);
888
889 /**
890  * nla_append - Add a netlink attribute without header or padding
891  * @skb: socket buffer to add attribute to
892  * @attrlen: length of attribute payload
893  * @data: head of attribute payload
894  *
895  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
896  * the attribute payload.
897  */
898 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
899 {
900         if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
901                 return -EMSGSIZE;
902
903         skb_put_data(skb, data, attrlen);
904         return 0;
905 }
906 EXPORT_SYMBOL(nla_append);
907 #endif