x86/PCI: Mark Broadwell-EP Home Agent 1 as having non-compliant BARs
[pandora-kernel.git] / lib / nlattr.c
1 /*
2  * NETLINK      Netlink attributes
3  *
4  *              Authors:        Thomas Graf <tgraf@suug.ch>
5  *                              Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6  */
7
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>
19
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,
27 };
28
29 static int validate_nla(const struct nlattr *nla, int maxtype,
30                         const struct nla_policy *policy)
31 {
32         const struct nla_policy *pt;
33         int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
34
35         if (type <= 0 || type > maxtype)
36                 return 0;
37
38         pt = &policy[type];
39
40         BUG_ON(pt->type > NLA_TYPE_MAX);
41
42         switch (pt->type) {
43         case NLA_FLAG:
44                 if (attrlen > 0)
45                         return -ERANGE;
46                 break;
47
48         case NLA_NUL_STRING:
49                 if (pt->len)
50                         minlen = min_t(int, attrlen, pt->len + 1);
51                 else
52                         minlen = attrlen;
53
54                 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
55                         return -EINVAL;
56                 /* fall through */
57
58         case NLA_STRING:
59                 if (attrlen < 1)
60                         return -ERANGE;
61
62                 if (pt->len) {
63                         char *buf = nla_data(nla);
64
65                         if (buf[attrlen - 1] == '\0')
66                                 attrlen--;
67
68                         if (attrlen > pt->len)
69                                 return -ERANGE;
70                 }
71                 break;
72
73         case NLA_BINARY:
74                 if (pt->len && attrlen > pt->len)
75                         return -ERANGE;
76                 break;
77
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.
92                  */
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];
100
101                 if (attrlen < minlen)
102                         return -ERANGE;
103         }
104
105         return 0;
106 }
107
108 /**
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
114  *
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.
118  *
119  * Returns 0 on success or a negative error code.
120  */
121 int nla_validate(const struct nlattr *head, int len, int maxtype,
122                  const struct nla_policy *policy)
123 {
124         const struct nlattr *nla;
125         int rem, err;
126
127         nla_for_each_attr(nla, head, len, rem) {
128                 err = validate_nla(nla, maxtype, policy);
129                 if (err < 0)
130                         goto errout;
131         }
132
133         err = 0;
134 errout:
135         return err;
136 }
137
138 /**
139  * nla_policy_len - Determin the max. length of a policy
140  * @policy: policy to use
141  * @n: number of policies
142  *
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.
146  *
147  * Returns 0 on success or a negative error code.
148  */
149 int
150 nla_policy_len(const struct nla_policy *p, int n)
151 {
152         int i, len = 0;
153
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]);
159         }
160
161         return len;
162 }
163
164 /**
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
171  *
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.
176  *
177  * Returns 0 on success or a negative error code.
178  */
179 int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
180               int len, const struct nla_policy *policy)
181 {
182         const struct nlattr *nla;
183         int rem, err;
184
185         memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
186
187         nla_for_each_attr(nla, head, len, rem) {
188                 u16 type = nla_type(nla);
189
190                 if (type > 0 && type <= maxtype) {
191                         if (policy) {
192                                 err = validate_nla(nla, maxtype, policy);
193                                 if (err < 0)
194                                         goto errout;
195                         }
196
197                         tb[type] = (struct nlattr *)nla;
198                 }
199         }
200
201         if (unlikely(rem > 0))
202                 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
203                                     rem, current->comm);
204
205         err = 0;
206 errout:
207         return err;
208 }
209
210 /**
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
215  *
216  * Returns the first attribute in the stream matching the specified type.
217  */
218 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
219 {
220         const struct nlattr *nla;
221         int rem;
222
223         nla_for_each_attr(nla, head, len, rem)
224                 if (nla_type(nla) == attrtype)
225                         return (struct nlattr *)nla;
226
227         return NULL;
228 }
229
230 /**
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
235  *
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.
239  *
240  * Returns the length of the source buffer.
241  */
242 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
243 {
244         size_t srclen = nla_len(nla);
245         char *src = nla_data(nla);
246
247         if (srclen > 0 && src[srclen - 1] == '\0')
248                 srclen--;
249
250         if (dstsize > 0) {
251                 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
252
253                 memset(dst, 0, dstsize);
254                 memcpy(dst, src, len);
255         }
256
257         return srclen;
258 }
259
260 /**
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
265  *
266  * Note: The number of bytes copied is limited by the length of
267  *       attribute's payload. memcpy
268  *
269  * Returns the number of bytes copied.
270  */
271 int nla_memcpy(void *dest, const struct nlattr *src, int count)
272 {
273         int minlen = min_t(int, count, nla_len(src));
274
275         memcpy(dest, nla_data(src), minlen);
276
277         return minlen;
278 }
279
280 /**
281  * nla_memcmp - Compare an attribute with sized memory area
282  * @nla: netlink attribute
283  * @data: memory area
284  * @size: size of memory area
285  */
286 int nla_memcmp(const struct nlattr *nla, const void *data,
287                              size_t size)
288 {
289         int d = nla_len(nla) - size;
290
291         if (d == 0)
292                 d = memcmp(nla_data(nla), data, size);
293
294         return d;
295 }
296
297 /**
298  * nla_strcmp - Compare a string attribute against a string
299  * @nla: netlink string attribute
300  * @str: another string
301  */
302 int nla_strcmp(const struct nlattr *nla, const char *str)
303 {
304         int len = strlen(str);
305         char *buf = nla_data(nla);
306         int attrlen = nla_len(nla);
307         int d;
308
309         if (attrlen > 0 && buf[attrlen - 1] == '\0')
310                 attrlen--;
311
312         d = attrlen - len;
313         if (d == 0)
314                 d = memcmp(nla_data(nla), str, len);
315
316         return d;
317 }
318
319 #ifdef CONFIG_NET
320 /**
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
325  *
326  * Adds a netlink attribute header to a socket buffer and reserves
327  * room for the payload but does not copy it.
328  *
329  * The caller is responsible to ensure that the skb provides enough
330  * tailroom for the attribute header and payload.
331  */
332 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
333 {
334         struct nlattr *nla;
335
336         nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
337         nla->nla_type = attrtype;
338         nla->nla_len = nla_attr_size(attrlen);
339
340         memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
341
342         return nla;
343 }
344 EXPORT_SYMBOL(__nla_reserve);
345
346 /**
347  * __nla_reserve_nohdr - reserve room for attribute without header
348  * @skb: socket buffer to reserve room on
349  * @attrlen: length of attribute payload
350  *
351  * Reserves room for attribute payload without a header.
352  *
353  * The caller is responsible to ensure that the skb provides enough
354  * tailroom for the payload.
355  */
356 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
357 {
358         void *start;
359
360         start = skb_put(skb, NLA_ALIGN(attrlen));
361         memset(start, 0, NLA_ALIGN(attrlen));
362
363         return start;
364 }
365 EXPORT_SYMBOL(__nla_reserve_nohdr);
366
367 /**
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
372  *
373  * Adds a netlink attribute header to a socket buffer and reserves
374  * room for the payload but does not copy it.
375  *
376  * Returns NULL if the tailroom of the skb is insufficient to store
377  * the attribute header and payload.
378  */
379 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
380 {
381         if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
382                 return NULL;
383
384         return __nla_reserve(skb, attrtype, attrlen);
385 }
386 EXPORT_SYMBOL(nla_reserve);
387
388 /**
389  * nla_reserve_nohdr - reserve room for attribute without header
390  * @skb: socket buffer to reserve room on
391  * @attrlen: length of attribute payload
392  *
393  * Reserves room for attribute payload without a header.
394  *
395  * Returns NULL if the tailroom of the skb is insufficient to store
396  * the attribute payload.
397  */
398 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
399 {
400         if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
401                 return NULL;
402
403         return __nla_reserve_nohdr(skb, attrlen);
404 }
405 EXPORT_SYMBOL(nla_reserve_nohdr);
406
407 /**
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
413  *
414  * The caller is responsible to ensure that the skb provides enough
415  * tailroom for the attribute header and payload.
416  */
417 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
418                              const void *data)
419 {
420         struct nlattr *nla;
421
422         nla = __nla_reserve(skb, attrtype, attrlen);
423         memcpy(nla_data(nla), data, attrlen);
424 }
425 EXPORT_SYMBOL(__nla_put);
426
427 /**
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
432  *
433  * The caller is responsible to ensure that the skb provides enough
434  * tailroom for the attribute payload.
435  */
436 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
437 {
438         void *start;
439
440         start = __nla_reserve_nohdr(skb, attrlen);
441         memcpy(start, data, attrlen);
442 }
443 EXPORT_SYMBOL(__nla_put_nohdr);
444
445 /**
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
451  *
452  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
453  * the attribute header and payload.
454  */
455 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
456 {
457         if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
458                 return -EMSGSIZE;
459
460         __nla_put(skb, attrtype, attrlen, data);
461         return 0;
462 }
463 EXPORT_SYMBOL(nla_put);
464
465 /**
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
470  *
471  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
472  * the attribute payload.
473  */
474 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
475 {
476         if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
477                 return -EMSGSIZE;
478
479         __nla_put_nohdr(skb, attrlen, data);
480         return 0;
481 }
482 EXPORT_SYMBOL(nla_put_nohdr);
483
484 /**
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
489  *
490  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
491  * the attribute payload.
492  */
493 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
494 {
495         if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
496                 return -EMSGSIZE;
497
498         memcpy(skb_put(skb, attrlen), data, attrlen);
499         return 0;
500 }
501 EXPORT_SYMBOL(nla_append);
502 #endif
503
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);