Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6-block.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
3654654f 17static const u16 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
115 *
116 * Validates all attributes in the specified attribute stream against the
117 * specified policy. Attributes with a type exceeding maxtype will be
118 * ignored. See documenation of struct nla_policy for more details.
119 *
120 * Returns 0 on success or a negative error code.
121 */
3654654f 122int nla_validate(const struct nlattr *head, int len, int maxtype,
ef7c79ed 123 const struct nla_policy *policy)
bfa83a9e 124{
3654654f 125 const struct nlattr *nla;
bfa83a9e
TG
126 int rem, err;
127
128 nla_for_each_attr(nla, head, len, rem) {
129 err = validate_nla(nla, maxtype, policy);
130 if (err < 0)
131 goto errout;
132 }
133
134 err = 0;
135errout:
136 return err;
137}
6d6a138f 138EXPORT_SYMBOL(nla_validate);
bfa83a9e 139
e487eb99
HE
140/**
141 * nla_policy_len - Determin the max. length of a policy
142 * @policy: policy to use
143 * @n: number of policies
144 *
145 * Determines the max. length of the policy. It is currently used
146 * to allocated Netlink buffers roughly the size of the actual
147 * message.
148 *
149 * Returns 0 on success or a negative error code.
150 */
151int
152nla_policy_len(const struct nla_policy *p, int n)
153{
154 int i, len = 0;
155
e3fa3aff 156 for (i = 0; i < n; i++, p++) {
e487eb99
HE
157 if (p->len)
158 len += nla_total_size(p->len);
159 else if (nla_attr_minlen[p->type])
160 len += nla_total_size(nla_attr_minlen[p->type]);
161 }
162
163 return len;
164}
6d6a138f 165EXPORT_SYMBOL(nla_policy_len);
e487eb99 166
bfa83a9e
TG
167/**
168 * nla_parse - Parse a stream of attributes into a tb buffer
169 * @tb: destination array with maxtype+1 elements
170 * @maxtype: maximum attribute type to be expected
171 * @head: head of attribute stream
172 * @len: length of attribute stream
10b595af 173 * @policy: validation policy
bfa83a9e
TG
174 *
175 * Parses a stream of attributes and stores a pointer to each attribute in
b595076a 176 * the tb array accessible via the attribute type. Attributes with a type
bfa83a9e
TG
177 * exceeding maxtype will be silently ignored for backwards compatibility
178 * reasons. policy may be set to NULL if no validation is required.
179 *
180 * Returns 0 on success or a negative error code.
181 */
3654654f
JE
182int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
183 int len, const struct nla_policy *policy)
bfa83a9e 184{
3654654f 185 const struct nlattr *nla;
bfa83a9e
TG
186 int rem, err;
187
188 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
189
190 nla_for_each_attr(nla, head, len, rem) {
8f4c1f9b 191 u16 type = nla_type(nla);
bfa83a9e
TG
192
193 if (type > 0 && type <= maxtype) {
194 if (policy) {
195 err = validate_nla(nla, maxtype, policy);
196 if (err < 0)
197 goto errout;
198 }
199
3654654f 200 tb[type] = (struct nlattr *)nla;
bfa83a9e
TG
201 }
202 }
203
204 if (unlikely(rem > 0))
bfc5184b
MS
205 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
206 rem, current->comm);
bfa83a9e
TG
207
208 err = 0;
209errout:
210 return err;
211}
6d6a138f 212EXPORT_SYMBOL(nla_parse);
bfa83a9e
TG
213
214/**
215 * nla_find - Find a specific attribute in a stream of attributes
216 * @head: head of attribute stream
217 * @len: length of attribute stream
218 * @attrtype: type of attribute to look for
219 *
220 * Returns the first attribute in the stream matching the specified type.
221 */
3654654f 222struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
bfa83a9e 223{
3654654f 224 const struct nlattr *nla;
bfa83a9e
TG
225 int rem;
226
227 nla_for_each_attr(nla, head, len, rem)
8f4c1f9b 228 if (nla_type(nla) == attrtype)
3654654f 229 return (struct nlattr *)nla;
bfa83a9e
TG
230
231 return NULL;
232}
6d6a138f 233EXPORT_SYMBOL(nla_find);
bfa83a9e
TG
234
235/**
236 * nla_strlcpy - Copy string attribute payload into a sized buffer
237 * @dst: where to copy the string to
10b595af 238 * @nla: attribute to copy the string from
bfa83a9e
TG
239 * @dstsize: size of destination buffer
240 *
241 * Copies at most dstsize - 1 bytes into the destination buffer.
242 * The result is always a valid NUL-terminated string. Unlike
243 * strlcpy the destination buffer is always padded out.
244 *
245 * Returns the length of the source buffer.
246 */
247size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
248{
249 size_t srclen = nla_len(nla);
250 char *src = nla_data(nla);
251
252 if (srclen > 0 && src[srclen - 1] == '\0')
253 srclen--;
254
255 if (dstsize > 0) {
256 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
257
258 memset(dst, 0, dstsize);
259 memcpy(dst, src, len);
260 }
261
262 return srclen;
263}
6d6a138f 264EXPORT_SYMBOL(nla_strlcpy);
bfa83a9e
TG
265
266/**
267 * nla_memcpy - Copy a netlink attribute into another memory area
268 * @dest: where to copy to memcpy
269 * @src: netlink attribute to copy from
270 * @count: size of the destination area
271 *
272 * Note: The number of bytes copied is limited by the length of
273 * attribute's payload. memcpy
274 *
275 * Returns the number of bytes copied.
276 */
b057efd4 277int nla_memcpy(void *dest, const struct nlattr *src, int count)
bfa83a9e
TG
278{
279 int minlen = min_t(int, count, nla_len(src));
280
281 memcpy(dest, nla_data(src), minlen);
5899f047
JB
282 if (count > minlen)
283 memset(dest + minlen, 0, count - minlen);
bfa83a9e
TG
284
285 return minlen;
286}
6d6a138f 287EXPORT_SYMBOL(nla_memcpy);
bfa83a9e
TG
288
289/**
290 * nla_memcmp - Compare an attribute with sized memory area
291 * @nla: netlink attribute
292 * @data: memory area
293 * @size: size of memory area
294 */
295int nla_memcmp(const struct nlattr *nla, const void *data,
296 size_t size)
297{
298 int d = nla_len(nla) - size;
299
300 if (d == 0)
301 d = memcmp(nla_data(nla), data, size);
302
303 return d;
304}
6d6a138f 305EXPORT_SYMBOL(nla_memcmp);
bfa83a9e
TG
306
307/**
308 * nla_strcmp - Compare a string attribute against a string
309 * @nla: netlink string attribute
310 * @str: another string
311 */
312int nla_strcmp(const struct nlattr *nla, const char *str)
313{
8b7b9324
PN
314 int len = strlen(str);
315 char *buf = nla_data(nla);
316 int attrlen = nla_len(nla);
317 int d;
bfa83a9e 318
8b7b9324
PN
319 if (attrlen > 0 && buf[attrlen - 1] == '\0')
320 attrlen--;
321
322 d = attrlen - len;
bfa83a9e
TG
323 if (d == 0)
324 d = memcmp(nla_data(nla), str, len);
325
326 return d;
327}
6d6a138f 328EXPORT_SYMBOL(nla_strcmp);
bfa83a9e 329
90800216 330#ifdef CONFIG_NET
bfa83a9e
TG
331/**
332 * __nla_reserve - reserve room for attribute on the skb
333 * @skb: socket buffer to reserve room on
334 * @attrtype: attribute type
335 * @attrlen: length of attribute payload
336 *
337 * Adds a netlink attribute header to a socket buffer and reserves
338 * room for the payload but does not copy it.
339 *
340 * The caller is responsible to ensure that the skb provides enough
341 * tailroom for the attribute header and payload.
342 */
343struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
344{
345 struct nlattr *nla;
346
347 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
348 nla->nla_type = attrtype;
349 nla->nla_len = nla_attr_size(attrlen);
350
351 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
352
353 return nla;
354}
90800216 355EXPORT_SYMBOL(__nla_reserve);
bfa83a9e 356
089bf1a6
ND
357/**
358 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
359 * @skb: socket buffer to reserve room on
360 * @attrtype: attribute type
361 * @attrlen: length of attribute payload
362 *
363 * Adds a netlink attribute header to a socket buffer and reserves
364 * room for the payload but does not copy it. It also ensure that this
365 * attribute will be 64-bit aign.
366 *
367 * The caller is responsible to ensure that the skb provides enough
368 * tailroom for the attribute header and payload.
369 */
370struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
371 int attrlen, int padattr)
372{
373 if (nla_need_padding_for_64bit(skb))
374 nla_align_64bit(skb, padattr);
375
376 return __nla_reserve(skb, attrtype, attrlen);
377}
378EXPORT_SYMBOL(__nla_reserve_64bit);
379
fe4944e5
TG
380/**
381 * __nla_reserve_nohdr - reserve room for attribute without header
382 * @skb: socket buffer to reserve room on
383 * @attrlen: length of attribute payload
384 *
385 * Reserves room for attribute payload without a header.
386 *
387 * The caller is responsible to ensure that the skb provides enough
388 * tailroom for the payload.
389 */
390void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
391{
392 void *start;
393
394 start = skb_put(skb, NLA_ALIGN(attrlen));
395 memset(start, 0, NLA_ALIGN(attrlen));
396
397 return start;
398}
90800216 399EXPORT_SYMBOL(__nla_reserve_nohdr);
fe4944e5 400
bfa83a9e
TG
401/**
402 * nla_reserve - reserve room for attribute on the skb
403 * @skb: socket buffer to reserve room on
404 * @attrtype: attribute type
405 * @attrlen: length of attribute payload
406 *
407 * Adds a netlink attribute header to a socket buffer and reserves
408 * room for the payload but does not copy it.
409 *
410 * Returns NULL if the tailroom of the skb is insufficient to store
411 * the attribute header and payload.
412 */
413struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
414{
415 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
416 return NULL;
417
418 return __nla_reserve(skb, attrtype, attrlen);
419}
90800216 420EXPORT_SYMBOL(nla_reserve);
bfa83a9e 421
089bf1a6
ND
422/**
423 * nla_reserve_64bit - reserve room for attribute on the skb and align it
424 * @skb: socket buffer to reserve room on
425 * @attrtype: attribute type
426 * @attrlen: length of attribute payload
427 *
428 * Adds a netlink attribute header to a socket buffer and reserves
429 * room for the payload but does not copy it. It also ensure that this
430 * attribute will be 64-bit aign.
431 *
432 * Returns NULL if the tailroom of the skb is insufficient to store
433 * the attribute header and payload.
434 */
435struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
436 int padattr)
437{
438 size_t len;
439
440 if (nla_need_padding_for_64bit(skb))
441 len = nla_total_size_64bit(attrlen);
442 else
443 len = nla_total_size(attrlen);
444 if (unlikely(skb_tailroom(skb) < len))
445 return NULL;
446
447 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
448}
449EXPORT_SYMBOL(nla_reserve_64bit);
450
fe4944e5 451/**
10b595af 452 * nla_reserve_nohdr - reserve room for attribute without header
fe4944e5 453 * @skb: socket buffer to reserve room on
10b595af 454 * @attrlen: length of attribute payload
fe4944e5
TG
455 *
456 * Reserves room for attribute payload without a header.
457 *
458 * Returns NULL if the tailroom of the skb is insufficient to store
459 * the attribute payload.
460 */
461void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
462{
463 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
464 return NULL;
465
466 return __nla_reserve_nohdr(skb, attrlen);
467}
90800216 468EXPORT_SYMBOL(nla_reserve_nohdr);
fe4944e5 469
bfa83a9e
TG
470/**
471 * __nla_put - Add a netlink attribute to a socket buffer
472 * @skb: socket buffer to add attribute to
473 * @attrtype: attribute type
474 * @attrlen: length of attribute payload
475 * @data: head of attribute payload
476 *
477 * The caller is responsible to ensure that the skb provides enough
478 * tailroom for the attribute header and payload.
479 */
480void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
481 const void *data)
482{
483 struct nlattr *nla;
484
485 nla = __nla_reserve(skb, attrtype, attrlen);
486 memcpy(nla_data(nla), data, attrlen);
487}
90800216 488EXPORT_SYMBOL(__nla_put);
bfa83a9e 489
089bf1a6
ND
490/**
491 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
492 * @skb: socket buffer to add attribute to
493 * @attrtype: attribute type
494 * @attrlen: length of attribute payload
495 * @data: head of attribute payload
496 *
497 * The caller is responsible to ensure that the skb provides enough
498 * tailroom for the attribute header and payload.
499 */
500void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
501 const void *data, int padattr)
502{
503 struct nlattr *nla;
504
505 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
506 memcpy(nla_data(nla), data, attrlen);
507}
508EXPORT_SYMBOL(__nla_put_64bit);
509
fe4944e5
TG
510/**
511 * __nla_put_nohdr - Add a netlink attribute without header
512 * @skb: socket buffer to add attribute to
513 * @attrlen: length of attribute payload
514 * @data: head of attribute payload
515 *
516 * The caller is responsible to ensure that the skb provides enough
517 * tailroom for the attribute payload.
518 */
519void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
520{
521 void *start;
522
523 start = __nla_reserve_nohdr(skb, attrlen);
524 memcpy(start, data, attrlen);
525}
90800216 526EXPORT_SYMBOL(__nla_put_nohdr);
bfa83a9e
TG
527
528/**
529 * nla_put - Add a netlink attribute to a socket buffer
530 * @skb: socket buffer to add attribute to
531 * @attrtype: attribute type
532 * @attrlen: length of attribute payload
533 * @data: head of attribute payload
534 *
bc3ed28c 535 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
bfa83a9e
TG
536 * the attribute header and payload.
537 */
538int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
539{
540 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
bc3ed28c 541 return -EMSGSIZE;
bfa83a9e
TG
542
543 __nla_put(skb, attrtype, attrlen, data);
544 return 0;
545}
90800216 546EXPORT_SYMBOL(nla_put);
bfa83a9e 547
089bf1a6
ND
548/**
549 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
550 * @skb: socket buffer to add attribute to
551 * @attrtype: attribute type
552 * @attrlen: length of attribute payload
553 * @data: head of attribute payload
554 *
555 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
556 * the attribute header and payload.
557 */
558int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
559 const void *data, int padattr)
560{
561 size_t len;
562
563 if (nla_need_padding_for_64bit(skb))
564 len = nla_total_size_64bit(attrlen);
565 else
566 len = nla_total_size(attrlen);
567 if (unlikely(skb_tailroom(skb) < len))
568 return -EMSGSIZE;
569
570 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
571 return 0;
572}
573EXPORT_SYMBOL(nla_put_64bit);
574
fe4944e5
TG
575/**
576 * nla_put_nohdr - Add a netlink attribute without header
577 * @skb: socket buffer to add attribute to
578 * @attrlen: length of attribute payload
579 * @data: head of attribute payload
580 *
bc3ed28c 581 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
fe4944e5
TG
582 * the attribute payload.
583 */
584int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
585{
586 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
bc3ed28c 587 return -EMSGSIZE;
fe4944e5
TG
588
589 __nla_put_nohdr(skb, attrlen, data);
590 return 0;
591}
90800216 592EXPORT_SYMBOL(nla_put_nohdr);
bfa83a9e 593
01480e1c
PM
594/**
595 * nla_append - Add a netlink attribute without header or padding
596 * @skb: socket buffer to add attribute to
597 * @attrlen: length of attribute payload
598 * @data: head of attribute payload
599 *
bc3ed28c 600 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
01480e1c
PM
601 * the attribute payload.
602 */
603int nla_append(struct sk_buff *skb, int attrlen, const void *data)
604{
605 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
bc3ed28c 606 return -EMSGSIZE;
01480e1c
PM
607
608 memcpy(skb_put(skb, attrlen), data, attrlen);
609 return 0;
610}
90800216
HX
611EXPORT_SYMBOL(nla_append);
612#endif