Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netlink / attr.c
blob2d106cfe1d276bf5e234195753eb091cba7b7f1e
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/types.h>
16 #include <net/netlink.h>
18 static u16 nla_attr_minlen[NLA_TYPE_MAX+1] __read_mostly = {
19 [NLA_U8] = sizeof(u8),
20 [NLA_U16] = sizeof(u16),
21 [NLA_U32] = sizeof(u32),
22 [NLA_U64] = sizeof(u64),
23 [NLA_NESTED] = NLA_HDRLEN,
26 static int validate_nla(struct nlattr *nla, int maxtype,
27 const struct nla_policy *policy)
29 const struct nla_policy *pt;
30 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
32 if (type <= 0 || type > maxtype)
33 return 0;
35 pt = &policy[type];
37 BUG_ON(pt->type > NLA_TYPE_MAX);
39 switch (pt->type) {
40 case NLA_FLAG:
41 if (attrlen > 0)
42 return -ERANGE;
43 break;
45 case NLA_NUL_STRING:
46 if (pt->len)
47 minlen = min_t(int, attrlen, pt->len + 1);
48 else
49 minlen = attrlen;
51 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
52 return -EINVAL;
53 /* fall through */
55 case NLA_STRING:
56 if (attrlen < 1)
57 return -ERANGE;
59 if (pt->len) {
60 char *buf = nla_data(nla);
62 if (buf[attrlen - 1] == '\0')
63 attrlen--;
65 if (attrlen > pt->len)
66 return -ERANGE;
68 break;
70 case NLA_BINARY:
71 if (pt->len && attrlen > pt->len)
72 return -ERANGE;
73 break;
75 case NLA_NESTED_COMPAT:
76 if (attrlen < pt->len)
77 return -ERANGE;
78 if (attrlen < NLA_ALIGN(pt->len))
79 break;
80 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
81 return -ERANGE;
82 nla = nla_data(nla) + NLA_ALIGN(pt->len);
83 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
84 return -ERANGE;
85 break;
86 default:
87 if (pt->len)
88 minlen = pt->len;
89 else if (pt->type != NLA_UNSPEC)
90 minlen = nla_attr_minlen[pt->type];
92 if (attrlen < minlen)
93 return -ERANGE;
96 return 0;
99 /**
100 * nla_validate - Validate a stream of attributes
101 * @head: head of attribute stream
102 * @len: length of attribute stream
103 * @maxtype: maximum attribute type to be expected
104 * @policy: validation policy
106 * Validates all attributes in the specified attribute stream against the
107 * specified policy. Attributes with a type exceeding maxtype will be
108 * ignored. See documenation of struct nla_policy for more details.
110 * Returns 0 on success or a negative error code.
112 int nla_validate(struct nlattr *head, int len, int maxtype,
113 const struct nla_policy *policy)
115 struct nlattr *nla;
116 int rem, err;
118 nla_for_each_attr(nla, head, len, rem) {
119 err = validate_nla(nla, maxtype, policy);
120 if (err < 0)
121 goto errout;
124 err = 0;
125 errout:
126 return err;
130 * nla_parse - Parse a stream of attributes into a tb buffer
131 * @tb: destination array with maxtype+1 elements
132 * @maxtype: maximum attribute type to be expected
133 * @head: head of attribute stream
134 * @len: length of attribute stream
135 * @policy: validation policy
137 * Parses a stream of attributes and stores a pointer to each attribute in
138 * the tb array accessable via the attribute type. Attributes with a type
139 * exceeding maxtype will be silently ignored for backwards compatibility
140 * reasons. policy may be set to NULL if no validation is required.
142 * Returns 0 on success or a negative error code.
144 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
145 const struct nla_policy *policy)
147 struct nlattr *nla;
148 int rem, err;
150 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
152 nla_for_each_attr(nla, head, len, rem) {
153 u16 type = nla_type(nla);
155 if (type > 0 && type <= maxtype) {
156 if (policy) {
157 err = validate_nla(nla, maxtype, policy);
158 if (err < 0)
159 goto errout;
162 tb[type] = nla;
166 if (unlikely(rem > 0))
167 printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
168 "attributes.\n", rem);
170 err = 0;
171 errout:
172 return err;
176 * nla_find - Find a specific attribute in a stream of attributes
177 * @head: head of attribute stream
178 * @len: length of attribute stream
179 * @attrtype: type of attribute to look for
181 * Returns the first attribute in the stream matching the specified type.
183 struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
185 struct nlattr *nla;
186 int rem;
188 nla_for_each_attr(nla, head, len, rem)
189 if (nla_type(nla) == attrtype)
190 return nla;
192 return NULL;
196 * nla_strlcpy - Copy string attribute payload into a sized buffer
197 * @dst: where to copy the string to
198 * @nla: attribute to copy the string from
199 * @dstsize: size of destination buffer
201 * Copies at most dstsize - 1 bytes into the destination buffer.
202 * The result is always a valid NUL-terminated string. Unlike
203 * strlcpy the destination buffer is always padded out.
205 * Returns the length of the source buffer.
207 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
209 size_t srclen = nla_len(nla);
210 char *src = nla_data(nla);
212 if (srclen > 0 && src[srclen - 1] == '\0')
213 srclen--;
215 if (dstsize > 0) {
216 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
218 memset(dst, 0, dstsize);
219 memcpy(dst, src, len);
222 return srclen;
226 * nla_memcpy - Copy a netlink attribute into another memory area
227 * @dest: where to copy to memcpy
228 * @src: netlink attribute to copy from
229 * @count: size of the destination area
231 * Note: The number of bytes copied is limited by the length of
232 * attribute's payload. memcpy
234 * Returns the number of bytes copied.
236 int nla_memcpy(void *dest, struct nlattr *src, int count)
238 int minlen = min_t(int, count, nla_len(src));
240 memcpy(dest, nla_data(src), minlen);
242 return minlen;
246 * nla_memcmp - Compare an attribute with sized memory area
247 * @nla: netlink attribute
248 * @data: memory area
249 * @size: size of memory area
251 int nla_memcmp(const struct nlattr *nla, const void *data,
252 size_t size)
254 int d = nla_len(nla) - size;
256 if (d == 0)
257 d = memcmp(nla_data(nla), data, size);
259 return d;
263 * nla_strcmp - Compare a string attribute against a string
264 * @nla: netlink string attribute
265 * @str: another string
267 int nla_strcmp(const struct nlattr *nla, const char *str)
269 int len = strlen(str) + 1;
270 int d = nla_len(nla) - len;
272 if (d == 0)
273 d = memcmp(nla_data(nla), str, len);
275 return d;
279 * __nla_reserve - reserve room for attribute on the skb
280 * @skb: socket buffer to reserve room on
281 * @attrtype: attribute type
282 * @attrlen: length of attribute payload
284 * Adds a netlink attribute header to a socket buffer and reserves
285 * room for the payload but does not copy it.
287 * The caller is responsible to ensure that the skb provides enough
288 * tailroom for the attribute header and payload.
290 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
292 struct nlattr *nla;
294 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
295 nla->nla_type = attrtype;
296 nla->nla_len = nla_attr_size(attrlen);
298 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
300 return nla;
304 * __nla_reserve_nohdr - reserve room for attribute without header
305 * @skb: socket buffer to reserve room on
306 * @attrlen: length of attribute payload
308 * Reserves room for attribute payload without a header.
310 * The caller is responsible to ensure that the skb provides enough
311 * tailroom for the payload.
313 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
315 void *start;
317 start = skb_put(skb, NLA_ALIGN(attrlen));
318 memset(start, 0, NLA_ALIGN(attrlen));
320 return start;
324 * nla_reserve - reserve room for attribute on the skb
325 * @skb: socket buffer to reserve room on
326 * @attrtype: attribute type
327 * @attrlen: length of attribute payload
329 * Adds a netlink attribute header to a socket buffer and reserves
330 * room for the payload but does not copy it.
332 * Returns NULL if the tailroom of the skb is insufficient to store
333 * the attribute header and payload.
335 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
337 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
338 return NULL;
340 return __nla_reserve(skb, attrtype, attrlen);
344 * nla_reserve_nohdr - reserve room for attribute without header
345 * @skb: socket buffer to reserve room on
346 * @attrlen: length of attribute payload
348 * Reserves room for attribute payload without a header.
350 * Returns NULL if the tailroom of the skb is insufficient to store
351 * the attribute payload.
353 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
355 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
356 return NULL;
358 return __nla_reserve_nohdr(skb, attrlen);
362 * __nla_put - Add a netlink attribute to a socket buffer
363 * @skb: socket buffer to add attribute to
364 * @attrtype: attribute type
365 * @attrlen: length of attribute payload
366 * @data: head of attribute payload
368 * The caller is responsible to ensure that the skb provides enough
369 * tailroom for the attribute header and payload.
371 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
372 const void *data)
374 struct nlattr *nla;
376 nla = __nla_reserve(skb, attrtype, attrlen);
377 memcpy(nla_data(nla), data, attrlen);
381 * __nla_put_nohdr - Add a netlink attribute without header
382 * @skb: socket buffer to add attribute to
383 * @attrlen: length of attribute payload
384 * @data: head of attribute payload
386 * The caller is responsible to ensure that the skb provides enough
387 * tailroom for the attribute payload.
389 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
391 void *start;
393 start = __nla_reserve_nohdr(skb, attrlen);
394 memcpy(start, data, attrlen);
398 * nla_put - Add a netlink attribute to a socket buffer
399 * @skb: socket buffer to add attribute to
400 * @attrtype: attribute type
401 * @attrlen: length of attribute payload
402 * @data: head of attribute payload
404 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
405 * the attribute header and payload.
407 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
409 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
410 return -EMSGSIZE;
412 __nla_put(skb, attrtype, attrlen, data);
413 return 0;
417 * nla_put_nohdr - Add a netlink attribute without header
418 * @skb: socket buffer to add attribute to
419 * @attrlen: length of attribute payload
420 * @data: head of attribute payload
422 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
423 * the attribute payload.
425 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
427 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
428 return -EMSGSIZE;
430 __nla_put_nohdr(skb, attrlen, data);
431 return 0;
435 * nla_append - Add a netlink attribute without header or padding
436 * @skb: socket buffer to add attribute to
437 * @attrlen: length of attribute payload
438 * @data: head of attribute payload
440 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
441 * the attribute payload.
443 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
445 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
446 return -EMSGSIZE;
448 memcpy(skb_put(skb, attrlen), data, attrlen);
449 return 0;
452 EXPORT_SYMBOL(nla_validate);
453 EXPORT_SYMBOL(nla_parse);
454 EXPORT_SYMBOL(nla_find);
455 EXPORT_SYMBOL(nla_strlcpy);
456 EXPORT_SYMBOL(__nla_reserve);
457 EXPORT_SYMBOL(__nla_reserve_nohdr);
458 EXPORT_SYMBOL(nla_reserve);
459 EXPORT_SYMBOL(nla_reserve_nohdr);
460 EXPORT_SYMBOL(__nla_put);
461 EXPORT_SYMBOL(__nla_put_nohdr);
462 EXPORT_SYMBOL(nla_put);
463 EXPORT_SYMBOL(nla_put_nohdr);
464 EXPORT_SYMBOL(nla_memcpy);
465 EXPORT_SYMBOL(nla_memcmp);
466 EXPORT_SYMBOL(nla_strcmp);
467 EXPORT_SYMBOL(nla_append);