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