[IPV4] fib_trie: removes a memset() call in tnode_new()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netlink / attr.c
blobec39d12c2423347463e766fc83bda5ef9861901a
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
136 * Parses a stream of attributes and stores a pointer to each attribute in
137 * the tb array accessable via the attribute type. Attributes with a type
138 * exceeding maxtype will be silently ignored for backwards compatibility
139 * reasons. policy may be set to NULL if no validation is required.
141 * Returns 0 on success or a negative error code.
143 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
144 const struct nla_policy *policy)
146 struct nlattr *nla;
147 int rem, err;
149 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
151 nla_for_each_attr(nla, head, len, rem) {
152 u16 type = nla_type(nla);
154 if (type > 0 && type <= maxtype) {
155 if (policy) {
156 err = validate_nla(nla, maxtype, policy);
157 if (err < 0)
158 goto errout;
161 tb[type] = nla;
165 if (unlikely(rem > 0))
166 printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
167 "attributes.\n", rem);
169 err = 0;
170 errout:
171 return err;
175 * nla_find - Find a specific attribute in a stream of attributes
176 * @head: head of attribute stream
177 * @len: length of attribute stream
178 * @attrtype: type of attribute to look for
180 * Returns the first attribute in the stream matching the specified type.
182 struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
184 struct nlattr *nla;
185 int rem;
187 nla_for_each_attr(nla, head, len, rem)
188 if (nla_type(nla) == attrtype)
189 return nla;
191 return NULL;
195 * nla_strlcpy - Copy string attribute payload into a sized buffer
196 * @dst: where to copy the string to
197 * @src: attribute to copy the string from
198 * @dstsize: size of destination buffer
200 * Copies at most dstsize - 1 bytes into the destination buffer.
201 * The result is always a valid NUL-terminated string. Unlike
202 * strlcpy the destination buffer is always padded out.
204 * Returns the length of the source buffer.
206 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
208 size_t srclen = nla_len(nla);
209 char *src = nla_data(nla);
211 if (srclen > 0 && src[srclen - 1] == '\0')
212 srclen--;
214 if (dstsize > 0) {
215 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
217 memset(dst, 0, dstsize);
218 memcpy(dst, src, len);
221 return srclen;
225 * nla_memcpy - Copy a netlink attribute into another memory area
226 * @dest: where to copy to memcpy
227 * @src: netlink attribute to copy from
228 * @count: size of the destination area
230 * Note: The number of bytes copied is limited by the length of
231 * attribute's payload. memcpy
233 * Returns the number of bytes copied.
235 int nla_memcpy(void *dest, struct nlattr *src, int count)
237 int minlen = min_t(int, count, nla_len(src));
239 memcpy(dest, nla_data(src), minlen);
241 return minlen;
245 * nla_memcmp - Compare an attribute with sized memory area
246 * @nla: netlink attribute
247 * @data: memory area
248 * @size: size of memory area
250 int nla_memcmp(const struct nlattr *nla, const void *data,
251 size_t size)
253 int d = nla_len(nla) - size;
255 if (d == 0)
256 d = memcmp(nla_data(nla), data, size);
258 return d;
262 * nla_strcmp - Compare a string attribute against a string
263 * @nla: netlink string attribute
264 * @str: another string
266 int nla_strcmp(const struct nlattr *nla, const char *str)
268 int len = strlen(str) + 1;
269 int d = nla_len(nla) - len;
271 if (d == 0)
272 d = memcmp(nla_data(nla), str, len);
274 return d;
278 * __nla_reserve - reserve room for attribute on the skb
279 * @skb: socket buffer to reserve room on
280 * @attrtype: attribute type
281 * @attrlen: length of attribute payload
283 * Adds a netlink attribute header to a socket buffer and reserves
284 * room for the payload but does not copy it.
286 * The caller is responsible to ensure that the skb provides enough
287 * tailroom for the attribute header and payload.
289 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
291 struct nlattr *nla;
293 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
294 nla->nla_type = attrtype;
295 nla->nla_len = nla_attr_size(attrlen);
297 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
299 return nla;
303 * __nla_reserve_nohdr - reserve room for attribute without header
304 * @skb: socket buffer to reserve room on
305 * @attrlen: length of attribute payload
307 * Reserves room for attribute payload without a header.
309 * The caller is responsible to ensure that the skb provides enough
310 * tailroom for the payload.
312 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
314 void *start;
316 start = skb_put(skb, NLA_ALIGN(attrlen));
317 memset(start, 0, NLA_ALIGN(attrlen));
319 return start;
323 * nla_reserve - reserve room for attribute on the skb
324 * @skb: socket buffer to reserve room on
325 * @attrtype: attribute type
326 * @attrlen: length of attribute payload
328 * Adds a netlink attribute header to a socket buffer and reserves
329 * room for the payload but does not copy it.
331 * Returns NULL if the tailroom of the skb is insufficient to store
332 * the attribute header and payload.
334 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
336 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
337 return NULL;
339 return __nla_reserve(skb, attrtype, attrlen);
343 * nla_reserve - reserve room for attribute without header
344 * @skb: socket buffer to reserve room on
345 * @len: length of attribute payload
347 * Reserves room for attribute payload without a header.
349 * Returns NULL if the tailroom of the skb is insufficient to store
350 * the attribute payload.
352 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
354 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
355 return NULL;
357 return __nla_reserve_nohdr(skb, attrlen);
361 * __nla_put - Add a netlink attribute to a socket buffer
362 * @skb: socket buffer to add attribute to
363 * @attrtype: attribute type
364 * @attrlen: length of attribute payload
365 * @data: head of attribute payload
367 * The caller is responsible to ensure that the skb provides enough
368 * tailroom for the attribute header and payload.
370 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
371 const void *data)
373 struct nlattr *nla;
375 nla = __nla_reserve(skb, attrtype, attrlen);
376 memcpy(nla_data(nla), data, attrlen);
380 * __nla_put_nohdr - Add a netlink attribute without header
381 * @skb: socket buffer to add attribute to
382 * @attrlen: length of attribute payload
383 * @data: head of attribute payload
385 * The caller is responsible to ensure that the skb provides enough
386 * tailroom for the attribute payload.
388 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
390 void *start;
392 start = __nla_reserve_nohdr(skb, attrlen);
393 memcpy(start, data, attrlen);
397 * nla_put - Add a netlink attribute to a socket buffer
398 * @skb: socket buffer to add attribute to
399 * @attrtype: attribute type
400 * @attrlen: length of attribute payload
401 * @data: head of attribute payload
403 * Returns -1 if the tailroom of the skb is insufficient to store
404 * the attribute header and payload.
406 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
408 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
409 return -1;
411 __nla_put(skb, attrtype, attrlen, data);
412 return 0;
416 * nla_put_nohdr - Add a netlink attribute without header
417 * @skb: socket buffer to add attribute to
418 * @attrlen: length of attribute payload
419 * @data: head of attribute payload
421 * Returns -1 if the tailroom of the skb is insufficient to store
422 * the attribute payload.
424 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
426 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
427 return -1;
429 __nla_put_nohdr(skb, attrlen, data);
430 return 0;
433 EXPORT_SYMBOL(nla_validate);
434 EXPORT_SYMBOL(nla_parse);
435 EXPORT_SYMBOL(nla_find);
436 EXPORT_SYMBOL(nla_strlcpy);
437 EXPORT_SYMBOL(__nla_reserve);
438 EXPORT_SYMBOL(__nla_reserve_nohdr);
439 EXPORT_SYMBOL(nla_reserve);
440 EXPORT_SYMBOL(nla_reserve_nohdr);
441 EXPORT_SYMBOL(__nla_put);
442 EXPORT_SYMBOL(__nla_put_nohdr);
443 EXPORT_SYMBOL(nla_put);
444 EXPORT_SYMBOL(nla_put_nohdr);
445 EXPORT_SYMBOL(nla_memcpy);
446 EXPORT_SYMBOL(nla_memcmp);
447 EXPORT_SYMBOL(nla_strcmp);