Merge branch 'Support-of-Flow-Director-in-HNS3-Ethernet-Driver-for-HiP08-Rev2-SoC'
[linux-2.6-block.git] / lib / nlattr.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
bfa83a9e
TG
2/*
3 * NETLINK Netlink attributes
4 *
5 * Authors: Thomas Graf <tgraf@suug.ch>
6 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7 */
8
8bc3bcc9 9#include <linux/export.h>
bfa83a9e
TG
10#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/jiffies.h>
bfa83a9e
TG
13#include <linux/skbuff.h>
14#include <linux/string.h>
15#include <linux/types.h>
16#include <net/netlink.h>
17
6e237d09
DA
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 */
28033ae4 23static const u8 nla_attr_len[NLA_TYPE_MAX+1] = {
bfa83a9e
TG
24 [NLA_U8] = sizeof(u8),
25 [NLA_U16] = sizeof(u16),
26 [NLA_U32] = sizeof(u32),
27 [NLA_U64] = sizeof(u64),
9eca2eb9
JA
28 [NLA_S8] = sizeof(s8),
29 [NLA_S16] = sizeof(s16),
30 [NLA_S32] = sizeof(s32),
31 [NLA_S64] = sizeof(s64),
bfa83a9e
TG
32};
33
28033ae4 34static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
6e237d09
DA
35 [NLA_U8] = sizeof(u8),
36 [NLA_U16] = sizeof(u16),
37 [NLA_U32] = sizeof(u32),
38 [NLA_U64] = sizeof(u64),
28033ae4
DA
39 [NLA_MSECS] = sizeof(u64),
40 [NLA_NESTED] = NLA_HDRLEN,
6e237d09
DA
41 [NLA_S8] = sizeof(s8),
42 [NLA_S16] = sizeof(s16),
43 [NLA_S32] = sizeof(s32),
44 [NLA_S64] = sizeof(s64),
28033ae4
DA
45};
46
64c83d83 47static int validate_nla_bitfield32(const struct nlattr *nla,
48fde90a 48 const u32 *valid_flags_mask)
64c83d83
JHS
49{
50 const struct nla_bitfield32 *bf = nla_data(nla);
64c83d83 51
48fde90a 52 if (!valid_flags_mask)
64c83d83
JHS
53 return -EINVAL;
54
55 /*disallow invalid bit selector */
56 if (bf->selector & ~*valid_flags_mask)
57 return -EINVAL;
58
59 /*disallow invalid bit values */
60 if (bf->value & ~*valid_flags_mask)
61 return -EINVAL;
62
63 /*disallow valid bit values that are not selected*/
64 if (bf->value & ~bf->selector)
65 return -EINVAL;
66
67 return 0;
68}
69
1501d135
JB
70static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
71 const struct nla_policy *policy,
72 struct netlink_ext_ack *extack)
73{
74 const struct nlattr *entry;
75 int rem;
76
77 nla_for_each_attr(entry, head, len, rem) {
78 int ret;
79
80 if (nla_len(entry) == 0)
81 continue;
82
83 if (nla_len(entry) < NLA_HDRLEN) {
84 NL_SET_ERR_MSG_ATTR(extack, entry,
85 "Array element too short");
86 return -ERANGE;
87 }
88
89 ret = nla_validate(nla_data(entry), nla_len(entry),
90 maxtype, policy, extack);
91 if (ret < 0)
92 return ret;
93 }
94
95 return 0;
96}
97
3654654f 98static int validate_nla(const struct nlattr *nla, int maxtype,
568b742a 99 const struct nla_policy *policy,
c29f1845 100 struct netlink_ext_ack *extack)
bfa83a9e 101{
ef7c79ed 102 const struct nla_policy *pt;
8f4c1f9b 103 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
c29f1845 104 int err = -ERANGE;
bfa83a9e 105
8f4c1f9b 106 if (type <= 0 || type > maxtype)
bfa83a9e
TG
107 return 0;
108
8f4c1f9b 109 pt = &policy[type];
bfa83a9e
TG
110
111 BUG_ON(pt->type > NLA_TYPE_MAX);
112
b60b87fc
JB
113 if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) ||
114 (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) {
6e237d09
DA
115 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
116 current->comm, type);
28033ae4
DA
117 }
118
a5531a5d 119 switch (pt->type) {
b60b87fc
JB
120 case NLA_EXACT_LEN:
121 if (attrlen != pt->len)
c29f1845 122 goto out_err;
b60b87fc
JB
123 break;
124
568b742a 125 case NLA_REJECT:
c29f1845
JB
126 if (extack && pt->validation_data) {
127 NL_SET_BAD_ATTR(extack, nla);
128 extack->_msg = pt->validation_data;
129 return -EINVAL;
130 }
131 err = -EINVAL;
132 goto out_err;
568b742a 133
a5531a5d
TG
134 case NLA_FLAG:
135 if (attrlen > 0)
c29f1845 136 goto out_err;
a5531a5d 137 break;
bfa83a9e 138
64c83d83
JHS
139 case NLA_BITFIELD32:
140 if (attrlen != sizeof(struct nla_bitfield32))
c29f1845 141 goto out_err;
64c83d83 142
c29f1845
JB
143 err = validate_nla_bitfield32(nla, pt->validation_data);
144 if (err)
145 goto out_err;
146 break;
64c83d83 147
a5531a5d
TG
148 case NLA_NUL_STRING:
149 if (pt->len)
150 minlen = min_t(int, attrlen, pt->len + 1);
151 else
152 minlen = attrlen;
bfa83a9e 153
c29f1845
JB
154 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
155 err = -EINVAL;
156 goto out_err;
157 }
a5531a5d
TG
158 /* fall through */
159
160 case NLA_STRING:
161 if (attrlen < 1)
c29f1845 162 goto out_err;
a5531a5d
TG
163
164 if (pt->len) {
165 char *buf = nla_data(nla);
166
167 if (buf[attrlen - 1] == '\0')
168 attrlen--;
169
170 if (attrlen > pt->len)
c29f1845 171 goto out_err;
a5531a5d
TG
172 }
173 break;
174
d30045a0
JB
175 case NLA_BINARY:
176 if (pt->len && attrlen > pt->len)
c29f1845 177 goto out_err;
d30045a0
JB
178 break;
179
ea5693cc
PM
180 case NLA_NESTED:
181 /* a nested attributes is allowed to be empty; if its not,
182 * it must have a size of at least NLA_HDRLEN.
183 */
184 if (attrlen == 0)
185 break;
9a659a35
JB
186 if (attrlen < NLA_HDRLEN)
187 goto out_err;
188 if (pt->validation_data) {
189 err = nla_validate(nla_data(nla), nla_len(nla), pt->len,
190 pt->validation_data, extack);
191 if (err < 0) {
192 /*
193 * return directly to preserve the inner
194 * error message/attribute pointer
195 */
196 return err;
197 }
198 }
199 break;
1501d135
JB
200 case NLA_NESTED_ARRAY:
201 /* a nested array attribute is allowed to be empty; if its not,
202 * it must have a size of at least NLA_HDRLEN.
203 */
204 if (attrlen == 0)
205 break;
206 if (attrlen < NLA_HDRLEN)
207 goto out_err;
208 if (pt->validation_data) {
209 int err;
210
211 err = nla_validate_array(nla_data(nla), nla_len(nla),
212 pt->len, pt->validation_data,
213 extack);
214 if (err < 0) {
215 /*
216 * return directly to preserve the inner
217 * error message/attribute pointer
218 */
219 return err;
220 }
221 }
222 break;
a5531a5d
TG
223 default:
224 if (pt->len)
225 minlen = pt->len;
226 else if (pt->type != NLA_UNSPEC)
227 minlen = nla_attr_minlen[pt->type];
228
229 if (attrlen < minlen)
c29f1845 230 goto out_err;
a5531a5d 231 }
bfa83a9e
TG
232
233 return 0;
c29f1845
JB
234out_err:
235 NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation");
236 return err;
bfa83a9e
TG
237}
238
239/**
240 * nla_validate - Validate a stream of attributes
241 * @head: head of attribute stream
242 * @len: length of attribute stream
243 * @maxtype: maximum attribute type to be expected
244 * @policy: validation policy
fceb6435 245 * @extack: extended ACK report struct
bfa83a9e
TG
246 *
247 * Validates all attributes in the specified attribute stream against the
248 * specified policy. Attributes with a type exceeding maxtype will be
249 * ignored. See documenation of struct nla_policy for more details.
250 *
251 * Returns 0 on success or a negative error code.
252 */
3654654f 253int nla_validate(const struct nlattr *head, int len, int maxtype,
fceb6435
JB
254 const struct nla_policy *policy,
255 struct netlink_ext_ack *extack)
bfa83a9e 256{
3654654f 257 const struct nlattr *nla;
fceb6435 258 int rem;
bfa83a9e
TG
259
260 nla_for_each_attr(nla, head, len, rem) {
c29f1845 261 int err = validate_nla(nla, maxtype, policy, extack);
fceb6435 262
c29f1845 263 if (err < 0)
fceb6435 264 return err;
bfa83a9e
TG
265 }
266
fceb6435 267 return 0;
bfa83a9e 268}
6d6a138f 269EXPORT_SYMBOL(nla_validate);
bfa83a9e 270
e487eb99
HE
271/**
272 * nla_policy_len - Determin the max. length of a policy
273 * @policy: policy to use
274 * @n: number of policies
275 *
276 * Determines the max. length of the policy. It is currently used
277 * to allocated Netlink buffers roughly the size of the actual
278 * message.
279 *
280 * Returns 0 on success or a negative error code.
281 */
282int
283nla_policy_len(const struct nla_policy *p, int n)
284{
285 int i, len = 0;
286
e3fa3aff 287 for (i = 0; i < n; i++, p++) {
e487eb99
HE
288 if (p->len)
289 len += nla_total_size(p->len);
28033ae4
DA
290 else if (nla_attr_len[p->type])
291 len += nla_total_size(nla_attr_len[p->type]);
e487eb99
HE
292 else if (nla_attr_minlen[p->type])
293 len += nla_total_size(nla_attr_minlen[p->type]);
294 }
295
296 return len;
297}
6d6a138f 298EXPORT_SYMBOL(nla_policy_len);
e487eb99 299
bfa83a9e
TG
300/**
301 * nla_parse - Parse a stream of attributes into a tb buffer
302 * @tb: destination array with maxtype+1 elements
303 * @maxtype: maximum attribute type to be expected
304 * @head: head of attribute stream
305 * @len: length of attribute stream
10b595af 306 * @policy: validation policy
bfa83a9e
TG
307 *
308 * Parses a stream of attributes and stores a pointer to each attribute in
b595076a 309 * the tb array accessible via the attribute type. Attributes with a type
bfa83a9e
TG
310 * exceeding maxtype will be silently ignored for backwards compatibility
311 * reasons. policy may be set to NULL if no validation is required.
312 *
313 * Returns 0 on success or a negative error code.
314 */
3654654f 315int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
fceb6435
JB
316 int len, const struct nla_policy *policy,
317 struct netlink_ext_ack *extack)
bfa83a9e 318{
3654654f 319 const struct nlattr *nla;
c29f1845 320 int rem;
bfa83a9e
TG
321
322 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
323
324 nla_for_each_attr(nla, head, len, rem) {
8f4c1f9b 325 u16 type = nla_type(nla);
bfa83a9e
TG
326
327 if (type > 0 && type <= maxtype) {
328 if (policy) {
c29f1845
JB
329 int err = validate_nla(nla, maxtype, policy,
330 extack);
331
332 if (err < 0)
333 return err;
bfa83a9e
TG
334 }
335
3654654f 336 tb[type] = (struct nlattr *)nla;
bfa83a9e
TG
337 }
338 }
339
340 if (unlikely(rem > 0))
bfc5184b
MS
341 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
342 rem, current->comm);
bfa83a9e 343
c29f1845 344 return 0;
bfa83a9e 345}
6d6a138f 346EXPORT_SYMBOL(nla_parse);
bfa83a9e
TG
347
348/**
349 * nla_find - Find a specific attribute in a stream of attributes
350 * @head: head of attribute stream
351 * @len: length of attribute stream
352 * @attrtype: type of attribute to look for
353 *
354 * Returns the first attribute in the stream matching the specified type.
355 */
3654654f 356struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
bfa83a9e 357{
3654654f 358 const struct nlattr *nla;
bfa83a9e
TG
359 int rem;
360
361 nla_for_each_attr(nla, head, len, rem)
8f4c1f9b 362 if (nla_type(nla) == attrtype)
3654654f 363 return (struct nlattr *)nla;
bfa83a9e
TG
364
365 return NULL;
366}
6d6a138f 367EXPORT_SYMBOL(nla_find);
bfa83a9e
TG
368
369/**
370 * nla_strlcpy - Copy string attribute payload into a sized buffer
371 * @dst: where to copy the string to
10b595af 372 * @nla: attribute to copy the string from
bfa83a9e
TG
373 * @dstsize: size of destination buffer
374 *
375 * Copies at most dstsize - 1 bytes into the destination buffer.
376 * The result is always a valid NUL-terminated string. Unlike
377 * strlcpy the destination buffer is always padded out.
378 *
379 * Returns the length of the source buffer.
380 */
381size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
382{
383 size_t srclen = nla_len(nla);
384 char *src = nla_data(nla);
385
386 if (srclen > 0 && src[srclen - 1] == '\0')
387 srclen--;
388
389 if (dstsize > 0) {
390 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
391
392 memset(dst, 0, dstsize);
393 memcpy(dst, src, len);
394 }
395
396 return srclen;
397}
6d6a138f 398EXPORT_SYMBOL(nla_strlcpy);
bfa83a9e 399
2cf0c8b3
PS
400/**
401 * nla_strdup - Copy string attribute payload into a newly allocated buffer
402 * @nla: attribute to copy the string from
403 * @flags: the type of memory to allocate (see kmalloc).
404 *
405 * Returns a pointer to the allocated buffer or NULL on error.
406 */
407char *nla_strdup(const struct nlattr *nla, gfp_t flags)
408{
409 size_t srclen = nla_len(nla);
410 char *src = nla_data(nla), *dst;
411
412 if (srclen > 0 && src[srclen - 1] == '\0')
413 srclen--;
414
415 dst = kmalloc(srclen + 1, flags);
416 if (dst != NULL) {
417 memcpy(dst, src, srclen);
418 dst[srclen] = '\0';
419 }
420 return dst;
421}
422EXPORT_SYMBOL(nla_strdup);
423
bfa83a9e
TG
424/**
425 * nla_memcpy - Copy a netlink attribute into another memory area
426 * @dest: where to copy to memcpy
427 * @src: netlink attribute to copy from
428 * @count: size of the destination area
429 *
430 * Note: The number of bytes copied is limited by the length of
431 * attribute's payload. memcpy
432 *
433 * Returns the number of bytes copied.
434 */
b057efd4 435int nla_memcpy(void *dest, const struct nlattr *src, int count)
bfa83a9e
TG
436{
437 int minlen = min_t(int, count, nla_len(src));
438
439 memcpy(dest, nla_data(src), minlen);
5899f047
JB
440 if (count > minlen)
441 memset(dest + minlen, 0, count - minlen);
bfa83a9e
TG
442
443 return minlen;
444}
6d6a138f 445EXPORT_SYMBOL(nla_memcpy);
bfa83a9e
TG
446
447/**
448 * nla_memcmp - Compare an attribute with sized memory area
449 * @nla: netlink attribute
450 * @data: memory area
451 * @size: size of memory area
452 */
453int nla_memcmp(const struct nlattr *nla, const void *data,
454 size_t size)
455{
456 int d = nla_len(nla) - size;
457
458 if (d == 0)
459 d = memcmp(nla_data(nla), data, size);
460
461 return d;
462}
6d6a138f 463EXPORT_SYMBOL(nla_memcmp);
bfa83a9e
TG
464
465/**
466 * nla_strcmp - Compare a string attribute against a string
467 * @nla: netlink string attribute
468 * @str: another string
469 */
470int nla_strcmp(const struct nlattr *nla, const char *str)
471{
8b7b9324
PN
472 int len = strlen(str);
473 char *buf = nla_data(nla);
474 int attrlen = nla_len(nla);
475 int d;
bfa83a9e 476
8b7b9324
PN
477 if (attrlen > 0 && buf[attrlen - 1] == '\0')
478 attrlen--;
479
480 d = attrlen - len;
bfa83a9e
TG
481 if (d == 0)
482 d = memcmp(nla_data(nla), str, len);
483
484 return d;
485}
6d6a138f 486EXPORT_SYMBOL(nla_strcmp);
bfa83a9e 487
90800216 488#ifdef CONFIG_NET
bfa83a9e
TG
489/**
490 * __nla_reserve - reserve room for attribute on the skb
491 * @skb: socket buffer to reserve room on
492 * @attrtype: attribute type
493 * @attrlen: length of attribute payload
494 *
495 * Adds a netlink attribute header to a socket buffer and reserves
496 * room for the payload but does not copy it.
497 *
498 * The caller is responsible to ensure that the skb provides enough
499 * tailroom for the attribute header and payload.
500 */
501struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
502{
503 struct nlattr *nla;
504
4df864c1 505 nla = skb_put(skb, nla_total_size(attrlen));
bfa83a9e
TG
506 nla->nla_type = attrtype;
507 nla->nla_len = nla_attr_size(attrlen);
508
509 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
510
511 return nla;
512}
90800216 513EXPORT_SYMBOL(__nla_reserve);
bfa83a9e 514
089bf1a6
ND
515/**
516 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
517 * @skb: socket buffer to reserve room on
518 * @attrtype: attribute type
519 * @attrlen: length of attribute payload
11a99573 520 * @padattr: attribute type for the padding
089bf1a6
ND
521 *
522 * Adds a netlink attribute header to a socket buffer and reserves
523 * room for the payload but does not copy it. It also ensure that this
11a99573 524 * attribute will have a 64-bit aligned nla_data() area.
089bf1a6
ND
525 *
526 * The caller is responsible to ensure that the skb provides enough
527 * tailroom for the attribute header and payload.
528 */
529struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
530 int attrlen, int padattr)
531{
532 if (nla_need_padding_for_64bit(skb))
533 nla_align_64bit(skb, padattr);
534
535 return __nla_reserve(skb, attrtype, attrlen);
536}
537EXPORT_SYMBOL(__nla_reserve_64bit);
538
fe4944e5
TG
539/**
540 * __nla_reserve_nohdr - reserve room for attribute without header
541 * @skb: socket buffer to reserve room on
542 * @attrlen: length of attribute payload
543 *
544 * Reserves room for attribute payload without a header.
545 *
546 * The caller is responsible to ensure that the skb provides enough
547 * tailroom for the payload.
548 */
549void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
550{
b952f4df 551 return skb_put_zero(skb, NLA_ALIGN(attrlen));
fe4944e5 552}
90800216 553EXPORT_SYMBOL(__nla_reserve_nohdr);
fe4944e5 554
bfa83a9e
TG
555/**
556 * nla_reserve - reserve room for attribute on the skb
557 * @skb: socket buffer to reserve room on
558 * @attrtype: attribute type
559 * @attrlen: length of attribute payload
560 *
561 * Adds a netlink attribute header to a socket buffer and reserves
562 * room for the payload but does not copy it.
563 *
564 * Returns NULL if the tailroom of the skb is insufficient to store
565 * the attribute header and payload.
566 */
567struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
568{
569 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
570 return NULL;
571
572 return __nla_reserve(skb, attrtype, attrlen);
573}
90800216 574EXPORT_SYMBOL(nla_reserve);
bfa83a9e 575
089bf1a6
ND
576/**
577 * nla_reserve_64bit - reserve room for attribute on the skb and align it
578 * @skb: socket buffer to reserve room on
579 * @attrtype: attribute type
580 * @attrlen: length of attribute payload
11a99573 581 * @padattr: attribute type for the padding
089bf1a6
ND
582 *
583 * Adds a netlink attribute header to a socket buffer and reserves
584 * room for the payload but does not copy it. It also ensure that this
11a99573 585 * attribute will have a 64-bit aligned nla_data() area.
089bf1a6
ND
586 *
587 * Returns NULL if the tailroom of the skb is insufficient to store
588 * the attribute header and payload.
589 */
590struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
591 int padattr)
592{
593 size_t len;
594
595 if (nla_need_padding_for_64bit(skb))
596 len = nla_total_size_64bit(attrlen);
597 else
598 len = nla_total_size(attrlen);
599 if (unlikely(skb_tailroom(skb) < len))
600 return NULL;
601
602 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
603}
604EXPORT_SYMBOL(nla_reserve_64bit);
605
fe4944e5 606/**
10b595af 607 * nla_reserve_nohdr - reserve room for attribute without header
fe4944e5 608 * @skb: socket buffer to reserve room on
10b595af 609 * @attrlen: length of attribute payload
fe4944e5
TG
610 *
611 * Reserves room for attribute payload without a header.
612 *
613 * Returns NULL if the tailroom of the skb is insufficient to store
614 * the attribute payload.
615 */
616void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
617{
618 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
619 return NULL;
620
621 return __nla_reserve_nohdr(skb, attrlen);
622}
90800216 623EXPORT_SYMBOL(nla_reserve_nohdr);
fe4944e5 624
bfa83a9e
TG
625/**
626 * __nla_put - Add a netlink attribute to a socket buffer
627 * @skb: socket buffer to add attribute to
628 * @attrtype: attribute type
629 * @attrlen: length of attribute payload
630 * @data: head of attribute payload
631 *
632 * The caller is responsible to ensure that the skb provides enough
633 * tailroom for the attribute header and payload.
634 */
635void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
636 const void *data)
637{
638 struct nlattr *nla;
639
640 nla = __nla_reserve(skb, attrtype, attrlen);
641 memcpy(nla_data(nla), data, attrlen);
642}
90800216 643EXPORT_SYMBOL(__nla_put);
bfa83a9e 644
089bf1a6
ND
645/**
646 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
647 * @skb: socket buffer to add attribute to
648 * @attrtype: attribute type
649 * @attrlen: length of attribute payload
650 * @data: head of attribute payload
11a99573 651 * @padattr: attribute type for the padding
089bf1a6
ND
652 *
653 * The caller is responsible to ensure that the skb provides enough
654 * tailroom for the attribute header and payload.
655 */
656void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
657 const void *data, int padattr)
658{
659 struct nlattr *nla;
660
661 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
662 memcpy(nla_data(nla), data, attrlen);
663}
664EXPORT_SYMBOL(__nla_put_64bit);
665
fe4944e5
TG
666/**
667 * __nla_put_nohdr - Add a netlink attribute without header
668 * @skb: socket buffer to add attribute to
669 * @attrlen: length of attribute payload
670 * @data: head of attribute payload
671 *
672 * The caller is responsible to ensure that the skb provides enough
673 * tailroom for the attribute payload.
674 */
675void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
676{
677 void *start;
678
679 start = __nla_reserve_nohdr(skb, attrlen);
680 memcpy(start, data, attrlen);
681}
90800216 682EXPORT_SYMBOL(__nla_put_nohdr);
bfa83a9e
TG
683
684/**
685 * nla_put - Add a netlink attribute to a socket buffer
686 * @skb: socket buffer to add attribute to
687 * @attrtype: attribute type
688 * @attrlen: length of attribute payload
689 * @data: head of attribute payload
690 *
bc3ed28c 691 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
bfa83a9e
TG
692 * the attribute header and payload.
693 */
694int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
695{
696 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
bc3ed28c 697 return -EMSGSIZE;
bfa83a9e
TG
698
699 __nla_put(skb, attrtype, attrlen, data);
700 return 0;
701}
90800216 702EXPORT_SYMBOL(nla_put);
bfa83a9e 703
089bf1a6
ND
704/**
705 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
706 * @skb: socket buffer to add attribute to
707 * @attrtype: attribute type
708 * @attrlen: length of attribute payload
709 * @data: head of attribute payload
11a99573 710 * @padattr: attribute type for the padding
089bf1a6
ND
711 *
712 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
713 * the attribute header and payload.
714 */
715int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
716 const void *data, int padattr)
717{
718 size_t len;
719
720 if (nla_need_padding_for_64bit(skb))
721 len = nla_total_size_64bit(attrlen);
722 else
723 len = nla_total_size(attrlen);
724 if (unlikely(skb_tailroom(skb) < len))
725 return -EMSGSIZE;
726
727 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
728 return 0;
729}
730EXPORT_SYMBOL(nla_put_64bit);
731
fe4944e5
TG
732/**
733 * nla_put_nohdr - Add a netlink attribute without header
734 * @skb: socket buffer to add attribute to
735 * @attrlen: length of attribute payload
736 * @data: head of attribute payload
737 *
bc3ed28c 738 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
fe4944e5
TG
739 * the attribute payload.
740 */
741int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
742{
743 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
bc3ed28c 744 return -EMSGSIZE;
fe4944e5
TG
745
746 __nla_put_nohdr(skb, attrlen, data);
747 return 0;
748}
90800216 749EXPORT_SYMBOL(nla_put_nohdr);
bfa83a9e 750
01480e1c
PM
751/**
752 * nla_append - Add a netlink attribute without header or padding
753 * @skb: socket buffer to add attribute to
754 * @attrlen: length of attribute payload
755 * @data: head of attribute payload
756 *
bc3ed28c 757 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
01480e1c
PM
758 * the attribute payload.
759 */
760int nla_append(struct sk_buff *skb, int attrlen, const void *data)
761{
762 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
bc3ed28c 763 return -EMSGSIZE;
01480e1c 764
59ae1d12 765 skb_put_data(skb, data, attrlen);
01480e1c
PM
766 return 0;
767}
90800216
HX
768EXPORT_SYMBOL(nla_append);
769#endif