]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/nlattr.c
Merge tag 'sound-4.12-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[mirror_ubuntu-artful-kernel.git] / lib / nlattr.c
CommitLineData
bfa83a9e
TG
1/*
2 * NETLINK Netlink attributes
3 *
4 * Authors: Thomas Graf <tgraf@suug.ch>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 */
7
8bc3bcc9 8#include <linux/export.h>
bfa83a9e
TG
9#include <linux/kernel.h>
10#include <linux/errno.h>
11#include <linux/jiffies.h>
bfa83a9e
TG
12#include <linux/skbuff.h>
13#include <linux/string.h>
14#include <linux/types.h>
15#include <net/netlink.h>
16
32d84cdc 17static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
bfa83a9e
TG
18 [NLA_U8] = sizeof(u8),
19 [NLA_U16] = sizeof(u16),
20 [NLA_U32] = sizeof(u32),
21 [NLA_U64] = sizeof(u64),
c30bc947 22 [NLA_MSECS] = sizeof(u64),
bfa83a9e 23 [NLA_NESTED] = NLA_HDRLEN,
9eca2eb9
JA
24 [NLA_S8] = sizeof(s8),
25 [NLA_S16] = sizeof(s16),
26 [NLA_S32] = sizeof(s32),
27 [NLA_S64] = sizeof(s64),
bfa83a9e
TG
28};
29
3654654f 30static int validate_nla(const struct nlattr *nla, int maxtype,
ef7c79ed 31 const struct nla_policy *policy)
bfa83a9e 32{
ef7c79ed 33 const struct nla_policy *pt;
8f4c1f9b 34 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
bfa83a9e 35
8f4c1f9b 36 if (type <= 0 || type > maxtype)
bfa83a9e
TG
37 return 0;
38
8f4c1f9b 39 pt = &policy[type];
bfa83a9e
TG
40
41 BUG_ON(pt->type > NLA_TYPE_MAX);
42
a5531a5d
TG
43 switch (pt->type) {
44 case NLA_FLAG:
45 if (attrlen > 0)
46 return -ERANGE;
47 break;
bfa83a9e 48
a5531a5d
TG
49 case NLA_NUL_STRING:
50 if (pt->len)
51 minlen = min_t(int, attrlen, pt->len + 1);
52 else
53 minlen = attrlen;
bfa83a9e 54
a5531a5d
TG
55 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
56 return -EINVAL;
57 /* fall through */
58
59 case NLA_STRING:
60 if (attrlen < 1)
61 return -ERANGE;
62
63 if (pt->len) {
64 char *buf = nla_data(nla);
65
66 if (buf[attrlen - 1] == '\0')
67 attrlen--;
68
69 if (attrlen > pt->len)
70 return -ERANGE;
71 }
72 break;
73
d30045a0
JB
74 case NLA_BINARY:
75 if (pt->len && attrlen > pt->len)
76 return -ERANGE;
77 break;
78
1092cb21
PM
79 case NLA_NESTED_COMPAT:
80 if (attrlen < pt->len)
81 return -ERANGE;
82 if (attrlen < NLA_ALIGN(pt->len))
83 break;
84 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
85 return -ERANGE;
86 nla = nla_data(nla) + NLA_ALIGN(pt->len);
87 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
88 return -ERANGE;
89 break;
ea5693cc
PM
90 case NLA_NESTED:
91 /* a nested attributes is allowed to be empty; if its not,
92 * it must have a size of at least NLA_HDRLEN.
93 */
94 if (attrlen == 0)
95 break;
a5531a5d
TG
96 default:
97 if (pt->len)
98 minlen = pt->len;
99 else if (pt->type != NLA_UNSPEC)
100 minlen = nla_attr_minlen[pt->type];
101
102 if (attrlen < minlen)
103 return -ERANGE;
104 }
bfa83a9e
TG
105
106 return 0;
107}
108
109/**
110 * nla_validate - Validate a stream of attributes
111 * @head: head of attribute stream
112 * @len: length of attribute stream
113 * @maxtype: maximum attribute type to be expected
114 * @policy: validation policy
fceb6435 115 * @extack: extended ACK report struct
bfa83a9e
TG
116 *
117 * Validates all attributes in the specified attribute stream against the
118 * specified policy. Attributes with a type exceeding maxtype will be
119 * ignored. See documenation of struct nla_policy for more details.
120 *
121 * Returns 0 on success or a negative error code.
122 */
3654654f 123int nla_validate(const struct nlattr *head, int len, int maxtype,
fceb6435
JB
124 const struct nla_policy *policy,
125 struct netlink_ext_ack *extack)
bfa83a9e 126{
3654654f 127 const struct nlattr *nla;
fceb6435 128 int rem;
bfa83a9e
TG
129
130 nla_for_each_attr(nla, head, len, rem) {
fceb6435
JB
131 int err = validate_nla(nla, maxtype, policy);
132
133 if (err < 0) {
134 if (extack)
135 extack->bad_attr = nla;
136 return err;
137 }
bfa83a9e
TG
138 }
139
fceb6435 140 return 0;
bfa83a9e 141}
6d6a138f 142EXPORT_SYMBOL(nla_validate);
bfa83a9e 143
e487eb99
HE
144/**
145 * nla_policy_len - Determin the max. length of a policy
146 * @policy: policy to use
147 * @n: number of policies
148 *
149 * Determines the max. length of the policy. It is currently used
150 * to allocated Netlink buffers roughly the size of the actual
151 * message.
152 *
153 * Returns 0 on success or a negative error code.
154 */
155int
156nla_policy_len(const struct nla_policy *p, int n)
157{
158 int i, len = 0;
159
e3fa3aff 160 for (i = 0; i < n; i++, p++) {
e487eb99
HE
161 if (p->len)
162 len += nla_total_size(p->len);
163 else if (nla_attr_minlen[p->type])
164 len += nla_total_size(nla_attr_minlen[p->type]);
165 }
166
167 return len;
168}
6d6a138f 169EXPORT_SYMBOL(nla_policy_len);
e487eb99 170
bfa83a9e
TG
171/**
172 * nla_parse - Parse a stream of attributes into a tb buffer
173 * @tb: destination array with maxtype+1 elements
174 * @maxtype: maximum attribute type to be expected
175 * @head: head of attribute stream
176 * @len: length of attribute stream
10b595af 177 * @policy: validation policy
bfa83a9e
TG
178 *
179 * Parses a stream of attributes and stores a pointer to each attribute in
b595076a 180 * the tb array accessible via the attribute type. Attributes with a type
bfa83a9e
TG
181 * exceeding maxtype will be silently ignored for backwards compatibility
182 * reasons. policy may be set to NULL if no validation is required.
183 *
184 * Returns 0 on success or a negative error code.
185 */
3654654f 186int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
fceb6435
JB
187 int len, const struct nla_policy *policy,
188 struct netlink_ext_ack *extack)
bfa83a9e 189{
3654654f 190 const struct nlattr *nla;
bfa83a9e
TG
191 int rem, err;
192
193 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
194
195 nla_for_each_attr(nla, head, len, rem) {
8f4c1f9b 196 u16 type = nla_type(nla);
bfa83a9e
TG
197
198 if (type > 0 && type <= maxtype) {
199 if (policy) {
200 err = validate_nla(nla, maxtype, policy);
fceb6435
JB
201 if (err < 0) {
202 if (extack)
203 extack->bad_attr = nla;
bfa83a9e 204 goto errout;
fceb6435 205 }
bfa83a9e
TG
206 }
207
3654654f 208 tb[type] = (struct nlattr *)nla;
bfa83a9e
TG
209 }
210 }
211
212 if (unlikely(rem > 0))
bfc5184b
MS
213 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
214 rem, current->comm);
bfa83a9e
TG
215
216 err = 0;
217errout:
218 return err;
219}
6d6a138f 220EXPORT_SYMBOL(nla_parse);
bfa83a9e
TG
221
222/**
223 * nla_find - Find a specific attribute in a stream of attributes
224 * @head: head of attribute stream
225 * @len: length of attribute stream
226 * @attrtype: type of attribute to look for
227 *
228 * Returns the first attribute in the stream matching the specified type.
229 */
3654654f 230struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
bfa83a9e 231{
3654654f 232 const struct nlattr *nla;
bfa83a9e
TG
233 int rem;
234
235 nla_for_each_attr(nla, head, len, rem)
8f4c1f9b 236 if (nla_type(nla) == attrtype)
3654654f 237 return (struct nlattr *)nla;
bfa83a9e
TG
238
239 return NULL;
240}
6d6a138f 241EXPORT_SYMBOL(nla_find);
bfa83a9e
TG
242
243/**
244 * nla_strlcpy - Copy string attribute payload into a sized buffer
245 * @dst: where to copy the string to
10b595af 246 * @nla: attribute to copy the string from
bfa83a9e
TG
247 * @dstsize: size of destination buffer
248 *
249 * Copies at most dstsize - 1 bytes into the destination buffer.
250 * The result is always a valid NUL-terminated string. Unlike
251 * strlcpy the destination buffer is always padded out.
252 *
253 * Returns the length of the source buffer.
254 */
255size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
256{
257 size_t srclen = nla_len(nla);
258 char *src = nla_data(nla);
259
260 if (srclen > 0 && src[srclen - 1] == '\0')
261 srclen--;
262
263 if (dstsize > 0) {
264 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
265
266 memset(dst, 0, dstsize);
267 memcpy(dst, src, len);
268 }
269
270 return srclen;
271}
6d6a138f 272EXPORT_SYMBOL(nla_strlcpy);
bfa83a9e
TG
273
274/**
275 * nla_memcpy - Copy a netlink attribute into another memory area
276 * @dest: where to copy to memcpy
277 * @src: netlink attribute to copy from
278 * @count: size of the destination area
279 *
280 * Note: The number of bytes copied is limited by the length of
281 * attribute's payload. memcpy
282 *
283 * Returns the number of bytes copied.
284 */
b057efd4 285int nla_memcpy(void *dest, const struct nlattr *src, int count)
bfa83a9e
TG
286{
287 int minlen = min_t(int, count, nla_len(src));
288
289 memcpy(dest, nla_data(src), minlen);
5899f047
JB
290 if (count > minlen)
291 memset(dest + minlen, 0, count - minlen);
bfa83a9e
TG
292
293 return minlen;
294}
6d6a138f 295EXPORT_SYMBOL(nla_memcpy);
bfa83a9e
TG
296
297/**
298 * nla_memcmp - Compare an attribute with sized memory area
299 * @nla: netlink attribute
300 * @data: memory area
301 * @size: size of memory area
302 */
303int nla_memcmp(const struct nlattr *nla, const void *data,
304 size_t size)
305{
306 int d = nla_len(nla) - size;
307
308 if (d == 0)
309 d = memcmp(nla_data(nla), data, size);
310
311 return d;
312}
6d6a138f 313EXPORT_SYMBOL(nla_memcmp);
bfa83a9e
TG
314
315/**
316 * nla_strcmp - Compare a string attribute against a string
317 * @nla: netlink string attribute
318 * @str: another string
319 */
320int nla_strcmp(const struct nlattr *nla, const char *str)
321{
8b7b9324
PN
322 int len = strlen(str);
323 char *buf = nla_data(nla);
324 int attrlen = nla_len(nla);
325 int d;
bfa83a9e 326
8b7b9324
PN
327 if (attrlen > 0 && buf[attrlen - 1] == '\0')
328 attrlen--;
329
330 d = attrlen - len;
bfa83a9e
TG
331 if (d == 0)
332 d = memcmp(nla_data(nla), str, len);
333
334 return d;
335}
6d6a138f 336EXPORT_SYMBOL(nla_strcmp);
bfa83a9e 337
90800216 338#ifdef CONFIG_NET
bfa83a9e
TG
339/**
340 * __nla_reserve - reserve room for attribute on the skb
341 * @skb: socket buffer to reserve room on
342 * @attrtype: attribute type
343 * @attrlen: length of attribute payload
344 *
345 * Adds a netlink attribute header to a socket buffer and reserves
346 * room for the payload but does not copy it.
347 *
348 * The caller is responsible to ensure that the skb provides enough
349 * tailroom for the attribute header and payload.
350 */
351struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
352{
353 struct nlattr *nla;
354
355 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
356 nla->nla_type = attrtype;
357 nla->nla_len = nla_attr_size(attrlen);
358
359 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
360
361 return nla;
362}
90800216 363EXPORT_SYMBOL(__nla_reserve);
bfa83a9e 364
089bf1a6
ND
365/**
366 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
367 * @skb: socket buffer to reserve room on
368 * @attrtype: attribute type
369 * @attrlen: length of attribute payload
11a99573 370 * @padattr: attribute type for the padding
089bf1a6
ND
371 *
372 * Adds a netlink attribute header to a socket buffer and reserves
373 * room for the payload but does not copy it. It also ensure that this
11a99573 374 * attribute will have a 64-bit aligned nla_data() area.
089bf1a6
ND
375 *
376 * The caller is responsible to ensure that the skb provides enough
377 * tailroom for the attribute header and payload.
378 */
379struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
380 int attrlen, int padattr)
381{
382 if (nla_need_padding_for_64bit(skb))
383 nla_align_64bit(skb, padattr);
384
385 return __nla_reserve(skb, attrtype, attrlen);
386}
387EXPORT_SYMBOL(__nla_reserve_64bit);
388
fe4944e5
TG
389/**
390 * __nla_reserve_nohdr - reserve room for attribute without header
391 * @skb: socket buffer to reserve room on
392 * @attrlen: length of attribute payload
393 *
394 * Reserves room for attribute payload without a header.
395 *
396 * The caller is responsible to ensure that the skb provides enough
397 * tailroom for the payload.
398 */
399void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
400{
401 void *start;
402
403 start = skb_put(skb, NLA_ALIGN(attrlen));
404 memset(start, 0, NLA_ALIGN(attrlen));
405
406 return start;
407}
90800216 408EXPORT_SYMBOL(__nla_reserve_nohdr);
fe4944e5 409
bfa83a9e
TG
410/**
411 * nla_reserve - reserve room for attribute on the skb
412 * @skb: socket buffer to reserve room on
413 * @attrtype: attribute type
414 * @attrlen: length of attribute payload
415 *
416 * Adds a netlink attribute header to a socket buffer and reserves
417 * room for the payload but does not copy it.
418 *
419 * Returns NULL if the tailroom of the skb is insufficient to store
420 * the attribute header and payload.
421 */
422struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
423{
424 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
425 return NULL;
426
427 return __nla_reserve(skb, attrtype, attrlen);
428}
90800216 429EXPORT_SYMBOL(nla_reserve);
bfa83a9e 430
089bf1a6
ND
431/**
432 * nla_reserve_64bit - reserve room for attribute on the skb and align it
433 * @skb: socket buffer to reserve room on
434 * @attrtype: attribute type
435 * @attrlen: length of attribute payload
11a99573 436 * @padattr: attribute type for the padding
089bf1a6
ND
437 *
438 * Adds a netlink attribute header to a socket buffer and reserves
439 * room for the payload but does not copy it. It also ensure that this
11a99573 440 * attribute will have a 64-bit aligned nla_data() area.
089bf1a6
ND
441 *
442 * Returns NULL if the tailroom of the skb is insufficient to store
443 * the attribute header and payload.
444 */
445struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
446 int padattr)
447{
448 size_t len;
449
450 if (nla_need_padding_for_64bit(skb))
451 len = nla_total_size_64bit(attrlen);
452 else
453 len = nla_total_size(attrlen);
454 if (unlikely(skb_tailroom(skb) < len))
455 return NULL;
456
457 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
458}
459EXPORT_SYMBOL(nla_reserve_64bit);
460
fe4944e5 461/**
10b595af 462 * nla_reserve_nohdr - reserve room for attribute without header
fe4944e5 463 * @skb: socket buffer to reserve room on
10b595af 464 * @attrlen: length of attribute payload
fe4944e5
TG
465 *
466 * Reserves room for attribute payload without a header.
467 *
468 * Returns NULL if the tailroom of the skb is insufficient to store
469 * the attribute payload.
470 */
471void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
472{
473 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
474 return NULL;
475
476 return __nla_reserve_nohdr(skb, attrlen);
477}
90800216 478EXPORT_SYMBOL(nla_reserve_nohdr);
fe4944e5 479
bfa83a9e
TG
480/**
481 * __nla_put - Add a netlink attribute to a socket buffer
482 * @skb: socket buffer to add attribute to
483 * @attrtype: attribute type
484 * @attrlen: length of attribute payload
485 * @data: head of attribute payload
486 *
487 * The caller is responsible to ensure that the skb provides enough
488 * tailroom for the attribute header and payload.
489 */
490void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
491 const void *data)
492{
493 struct nlattr *nla;
494
495 nla = __nla_reserve(skb, attrtype, attrlen);
496 memcpy(nla_data(nla), data, attrlen);
497}
90800216 498EXPORT_SYMBOL(__nla_put);
bfa83a9e 499
089bf1a6
ND
500/**
501 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
502 * @skb: socket buffer to add attribute to
503 * @attrtype: attribute type
504 * @attrlen: length of attribute payload
505 * @data: head of attribute payload
11a99573 506 * @padattr: attribute type for the padding
089bf1a6
ND
507 *
508 * The caller is responsible to ensure that the skb provides enough
509 * tailroom for the attribute header and payload.
510 */
511void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
512 const void *data, int padattr)
513{
514 struct nlattr *nla;
515
516 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
517 memcpy(nla_data(nla), data, attrlen);
518}
519EXPORT_SYMBOL(__nla_put_64bit);
520
fe4944e5
TG
521/**
522 * __nla_put_nohdr - Add a netlink attribute without header
523 * @skb: socket buffer to add attribute to
524 * @attrlen: length of attribute payload
525 * @data: head of attribute payload
526 *
527 * The caller is responsible to ensure that the skb provides enough
528 * tailroom for the attribute payload.
529 */
530void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
531{
532 void *start;
533
534 start = __nla_reserve_nohdr(skb, attrlen);
535 memcpy(start, data, attrlen);
536}
90800216 537EXPORT_SYMBOL(__nla_put_nohdr);
bfa83a9e
TG
538
539/**
540 * nla_put - Add a netlink attribute to a socket buffer
541 * @skb: socket buffer to add attribute to
542 * @attrtype: attribute type
543 * @attrlen: length of attribute payload
544 * @data: head of attribute payload
545 *
bc3ed28c 546 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
bfa83a9e
TG
547 * the attribute header and payload.
548 */
549int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
550{
551 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
bc3ed28c 552 return -EMSGSIZE;
bfa83a9e
TG
553
554 __nla_put(skb, attrtype, attrlen, data);
555 return 0;
556}
90800216 557EXPORT_SYMBOL(nla_put);
bfa83a9e 558
089bf1a6
ND
559/**
560 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
561 * @skb: socket buffer to add attribute to
562 * @attrtype: attribute type
563 * @attrlen: length of attribute payload
564 * @data: head of attribute payload
11a99573 565 * @padattr: attribute type for the padding
089bf1a6
ND
566 *
567 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
568 * the attribute header and payload.
569 */
570int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
571 const void *data, int padattr)
572{
573 size_t len;
574
575 if (nla_need_padding_for_64bit(skb))
576 len = nla_total_size_64bit(attrlen);
577 else
578 len = nla_total_size(attrlen);
579 if (unlikely(skb_tailroom(skb) < len))
580 return -EMSGSIZE;
581
582 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
583 return 0;
584}
585EXPORT_SYMBOL(nla_put_64bit);
586
fe4944e5
TG
587/**
588 * nla_put_nohdr - Add a netlink attribute without header
589 * @skb: socket buffer to add attribute to
590 * @attrlen: length of attribute payload
591 * @data: head of attribute payload
592 *
bc3ed28c 593 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
fe4944e5
TG
594 * the attribute payload.
595 */
596int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
597{
598 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
bc3ed28c 599 return -EMSGSIZE;
fe4944e5
TG
600
601 __nla_put_nohdr(skb, attrlen, data);
602 return 0;
603}
90800216 604EXPORT_SYMBOL(nla_put_nohdr);
bfa83a9e 605
01480e1c
PM
606/**
607 * nla_append - Add a netlink attribute without header or padding
608 * @skb: socket buffer to add attribute to
609 * @attrlen: length of attribute payload
610 * @data: head of attribute payload
611 *
bc3ed28c 612 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
01480e1c
PM
613 * the attribute payload.
614 */
615int nla_append(struct sk_buff *skb, int attrlen, const void *data)
616{
617 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
bc3ed28c 618 return -EMSGSIZE;
01480e1c
PM
619
620 memcpy(skb_put(skb, attrlen), data, attrlen);
621 return 0;
622}
90800216
HX
623EXPORT_SYMBOL(nla_append);
624#endif