Linux 3.2.84
[linux/fpc-iii.git] / lib / nlattr.c
blobbe25e355a5add38adacae6939f6352a8692c91fa
1 /*
2 * NETLINK Netlink attributes
4 * Authors: Thomas Graf <tgraf@suug.ch>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 */
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/jiffies.h>
12 #include <linux/netdevice.h>
13 #include <linux/skbuff.h>
14 #include <linux/string.h>
15 #include <linux/ratelimit.h>
16 #include <linux/sched.h>
17 #include <linux/types.h>
18 #include <net/netlink.h>
20 static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = {
21 [NLA_U8] = sizeof(u8),
22 [NLA_U16] = sizeof(u16),
23 [NLA_U32] = sizeof(u32),
24 [NLA_U64] = sizeof(u64),
25 [NLA_MSECS] = sizeof(u64),
26 [NLA_NESTED] = NLA_HDRLEN,
29 static int validate_nla(const struct nlattr *nla, int maxtype,
30 const struct nla_policy *policy)
32 const struct nla_policy *pt;
33 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
35 if (type <= 0 || type > maxtype)
36 return 0;
38 pt = &policy[type];
40 BUG_ON(pt->type > NLA_TYPE_MAX);
42 switch (pt->type) {
43 case NLA_FLAG:
44 if (attrlen > 0)
45 return -ERANGE;
46 break;
48 case NLA_NUL_STRING:
49 if (pt->len)
50 minlen = min_t(int, attrlen, pt->len + 1);
51 else
52 minlen = attrlen;
54 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
55 return -EINVAL;
56 /* fall through */
58 case NLA_STRING:
59 if (attrlen < 1)
60 return -ERANGE;
62 if (pt->len) {
63 char *buf = nla_data(nla);
65 if (buf[attrlen - 1] == '\0')
66 attrlen--;
68 if (attrlen > pt->len)
69 return -ERANGE;
71 break;
73 case NLA_BINARY:
74 if (pt->len && attrlen > pt->len)
75 return -ERANGE;
76 break;
78 case NLA_NESTED_COMPAT:
79 if (attrlen < pt->len)
80 return -ERANGE;
81 if (attrlen < NLA_ALIGN(pt->len))
82 break;
83 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
84 return -ERANGE;
85 nla = nla_data(nla) + NLA_ALIGN(pt->len);
86 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
87 return -ERANGE;
88 break;
89 case NLA_NESTED:
90 /* a nested attributes is allowed to be empty; if its not,
91 * it must have a size of at least NLA_HDRLEN.
93 if (attrlen == 0)
94 break;
95 default:
96 if (pt->len)
97 minlen = pt->len;
98 else if (pt->type != NLA_UNSPEC)
99 minlen = nla_attr_minlen[pt->type];
101 if (attrlen < minlen)
102 return -ERANGE;
105 return 0;
109 * nla_validate - Validate a stream of attributes
110 * @head: head of attribute stream
111 * @len: length of attribute stream
112 * @maxtype: maximum attribute type to be expected
113 * @policy: validation policy
115 * Validates all attributes in the specified attribute stream against the
116 * specified policy. Attributes with a type exceeding maxtype will be
117 * ignored. See documenation of struct nla_policy for more details.
119 * Returns 0 on success or a negative error code.
121 int nla_validate(const struct nlattr *head, int len, int maxtype,
122 const struct nla_policy *policy)
124 const struct nlattr *nla;
125 int rem, err;
127 nla_for_each_attr(nla, head, len, rem) {
128 err = validate_nla(nla, maxtype, policy);
129 if (err < 0)
130 goto errout;
133 err = 0;
134 errout:
135 return err;
139 * nla_policy_len - Determin the max. length of a policy
140 * @policy: policy to use
141 * @n: number of policies
143 * Determines the max. length of the policy. It is currently used
144 * to allocated Netlink buffers roughly the size of the actual
145 * message.
147 * Returns 0 on success or a negative error code.
150 nla_policy_len(const struct nla_policy *p, int n)
152 int i, len = 0;
154 for (i = 0; i < n; i++, p++) {
155 if (p->len)
156 len += nla_total_size(p->len);
157 else if (nla_attr_minlen[p->type])
158 len += nla_total_size(nla_attr_minlen[p->type]);
161 return len;
165 * nla_parse - Parse a stream of attributes into a tb buffer
166 * @tb: destination array with maxtype+1 elements
167 * @maxtype: maximum attribute type to be expected
168 * @head: head of attribute stream
169 * @len: length of attribute stream
170 * @policy: validation policy
172 * Parses a stream of attributes and stores a pointer to each attribute in
173 * the tb array accessible via the attribute type. Attributes with a type
174 * exceeding maxtype will be silently ignored for backwards compatibility
175 * reasons. policy may be set to NULL if no validation is required.
177 * Returns 0 on success or a negative error code.
179 int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
180 int len, const struct nla_policy *policy)
182 const struct nlattr *nla;
183 int rem, err;
185 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
187 nla_for_each_attr(nla, head, len, rem) {
188 u16 type = nla_type(nla);
190 if (type > 0 && type <= maxtype) {
191 if (policy) {
192 err = validate_nla(nla, maxtype, policy);
193 if (err < 0)
194 goto errout;
197 tb[type] = (struct nlattr *)nla;
201 if (unlikely(rem > 0))
202 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
203 rem, current->comm);
205 err = 0;
206 errout:
207 return err;
211 * nla_find - Find a specific attribute in a stream of attributes
212 * @head: head of attribute stream
213 * @len: length of attribute stream
214 * @attrtype: type of attribute to look for
216 * Returns the first attribute in the stream matching the specified type.
218 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
220 const struct nlattr *nla;
221 int rem;
223 nla_for_each_attr(nla, head, len, rem)
224 if (nla_type(nla) == attrtype)
225 return (struct nlattr *)nla;
227 return NULL;
231 * nla_strlcpy - Copy string attribute payload into a sized buffer
232 * @dst: where to copy the string to
233 * @nla: attribute to copy the string from
234 * @dstsize: size of destination buffer
236 * Copies at most dstsize - 1 bytes into the destination buffer.
237 * The result is always a valid NUL-terminated string. Unlike
238 * strlcpy the destination buffer is always padded out.
240 * Returns the length of the source buffer.
242 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
244 size_t srclen = nla_len(nla);
245 char *src = nla_data(nla);
247 if (srclen > 0 && src[srclen - 1] == '\0')
248 srclen--;
250 if (dstsize > 0) {
251 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
253 memset(dst, 0, dstsize);
254 memcpy(dst, src, len);
257 return srclen;
261 * nla_memcpy - Copy a netlink attribute into another memory area
262 * @dest: where to copy to memcpy
263 * @src: netlink attribute to copy from
264 * @count: size of the destination area
266 * Note: The number of bytes copied is limited by the length of
267 * attribute's payload. memcpy
269 * Returns the number of bytes copied.
271 int nla_memcpy(void *dest, const struct nlattr *src, int count)
273 int minlen = min_t(int, count, nla_len(src));
275 memcpy(dest, nla_data(src), minlen);
277 return minlen;
281 * nla_memcmp - Compare an attribute with sized memory area
282 * @nla: netlink attribute
283 * @data: memory area
284 * @size: size of memory area
286 int nla_memcmp(const struct nlattr *nla, const void *data,
287 size_t size)
289 int d = nla_len(nla) - size;
291 if (d == 0)
292 d = memcmp(nla_data(nla), data, size);
294 return d;
298 * nla_strcmp - Compare a string attribute against a string
299 * @nla: netlink string attribute
300 * @str: another string
302 int nla_strcmp(const struct nlattr *nla, const char *str)
304 int len = strlen(str);
305 char *buf = nla_data(nla);
306 int attrlen = nla_len(nla);
307 int d;
309 if (attrlen > 0 && buf[attrlen - 1] == '\0')
310 attrlen--;
312 d = attrlen - len;
313 if (d == 0)
314 d = memcmp(nla_data(nla), str, len);
316 return d;
319 #ifdef CONFIG_NET
321 * __nla_reserve - reserve room for attribute on the skb
322 * @skb: socket buffer to reserve room on
323 * @attrtype: attribute type
324 * @attrlen: length of attribute payload
326 * Adds a netlink attribute header to a socket buffer and reserves
327 * room for the payload but does not copy it.
329 * The caller is responsible to ensure that the skb provides enough
330 * tailroom for the attribute header and payload.
332 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
334 struct nlattr *nla;
336 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
337 nla->nla_type = attrtype;
338 nla->nla_len = nla_attr_size(attrlen);
340 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
342 return nla;
344 EXPORT_SYMBOL(__nla_reserve);
347 * __nla_reserve_nohdr - reserve room for attribute without header
348 * @skb: socket buffer to reserve room on
349 * @attrlen: length of attribute payload
351 * Reserves room for attribute payload without a header.
353 * The caller is responsible to ensure that the skb provides enough
354 * tailroom for the payload.
356 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
358 void *start;
360 start = skb_put(skb, NLA_ALIGN(attrlen));
361 memset(start, 0, NLA_ALIGN(attrlen));
363 return start;
365 EXPORT_SYMBOL(__nla_reserve_nohdr);
368 * nla_reserve - reserve room for attribute on the skb
369 * @skb: socket buffer to reserve room on
370 * @attrtype: attribute type
371 * @attrlen: length of attribute payload
373 * Adds a netlink attribute header to a socket buffer and reserves
374 * room for the payload but does not copy it.
376 * Returns NULL if the tailroom of the skb is insufficient to store
377 * the attribute header and payload.
379 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
381 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
382 return NULL;
384 return __nla_reserve(skb, attrtype, attrlen);
386 EXPORT_SYMBOL(nla_reserve);
389 * nla_reserve_nohdr - reserve room for attribute without header
390 * @skb: socket buffer to reserve room on
391 * @attrlen: length of attribute payload
393 * Reserves room for attribute payload without a header.
395 * Returns NULL if the tailroom of the skb is insufficient to store
396 * the attribute payload.
398 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
400 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
401 return NULL;
403 return __nla_reserve_nohdr(skb, attrlen);
405 EXPORT_SYMBOL(nla_reserve_nohdr);
408 * __nla_put - Add a netlink attribute to a socket buffer
409 * @skb: socket buffer to add attribute to
410 * @attrtype: attribute type
411 * @attrlen: length of attribute payload
412 * @data: head of attribute payload
414 * The caller is responsible to ensure that the skb provides enough
415 * tailroom for the attribute header and payload.
417 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
418 const void *data)
420 struct nlattr *nla;
422 nla = __nla_reserve(skb, attrtype, attrlen);
423 memcpy(nla_data(nla), data, attrlen);
425 EXPORT_SYMBOL(__nla_put);
428 * __nla_put_nohdr - Add a netlink attribute without header
429 * @skb: socket buffer to add attribute to
430 * @attrlen: length of attribute payload
431 * @data: head of attribute payload
433 * The caller is responsible to ensure that the skb provides enough
434 * tailroom for the attribute payload.
436 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
438 void *start;
440 start = __nla_reserve_nohdr(skb, attrlen);
441 memcpy(start, data, attrlen);
443 EXPORT_SYMBOL(__nla_put_nohdr);
446 * nla_put - Add a netlink attribute to a socket buffer
447 * @skb: socket buffer to add attribute to
448 * @attrtype: attribute type
449 * @attrlen: length of attribute payload
450 * @data: head of attribute payload
452 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
453 * the attribute header and payload.
455 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
457 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
458 return -EMSGSIZE;
460 __nla_put(skb, attrtype, attrlen, data);
461 return 0;
463 EXPORT_SYMBOL(nla_put);
466 * nla_put_nohdr - Add a netlink attribute without header
467 * @skb: socket buffer to add attribute to
468 * @attrlen: length of attribute payload
469 * @data: head of attribute payload
471 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
472 * the attribute payload.
474 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
476 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
477 return -EMSGSIZE;
479 __nla_put_nohdr(skb, attrlen, data);
480 return 0;
482 EXPORT_SYMBOL(nla_put_nohdr);
485 * nla_append - Add a netlink attribute without header or padding
486 * @skb: socket buffer to add attribute to
487 * @attrlen: length of attribute payload
488 * @data: head of attribute payload
490 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
491 * the attribute payload.
493 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
495 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
496 return -EMSGSIZE;
498 memcpy(skb_put(skb, attrlen), data, attrlen);
499 return 0;
501 EXPORT_SYMBOL(nla_append);
502 #endif
504 EXPORT_SYMBOL(nla_validate);
505 EXPORT_SYMBOL(nla_policy_len);
506 EXPORT_SYMBOL(nla_parse);
507 EXPORT_SYMBOL(nla_find);
508 EXPORT_SYMBOL(nla_strlcpy);
509 EXPORT_SYMBOL(nla_memcpy);
510 EXPORT_SYMBOL(nla_memcmp);
511 EXPORT_SYMBOL(nla_strcmp);