blob: ae32c573df00d1158ab019a3d91e3df92281a6ea [file] [log] [blame]
Thomas Grafbfa83a92005-11-10 02:25:51 +01001/*
2 * NETLINK Netlink attributes
3 *
4 * Authors: Thomas Graf <tgraf@suug.ch>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 */
7
Thomas Grafbfa83a92005-11-10 02:25:51 +01008#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>
17
18static 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),
Thomas Grafbfa83a92005-11-10 02:25:51 +010023 [NLA_NESTED] = NLA_HDRLEN,
24};
25
26static int validate_nla(struct nlattr *nla, int maxtype,
Patrick McHardyef7c79e2007-06-05 12:38:30 -070027 const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +010028{
Patrick McHardyef7c79e2007-06-05 12:38:30 -070029 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +020030 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +010031
Thomas Graf8f4c1f92007-09-12 14:44:36 +020032 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +010033 return 0;
34
Thomas Graf8f4c1f92007-09-12 14:44:36 +020035 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +010036
37 BUG_ON(pt->type > NLA_TYPE_MAX);
38
Thomas Grafa5531a52006-08-26 20:11:47 -070039 switch (pt->type) {
40 case NLA_FLAG:
41 if (attrlen > 0)
42 return -ERANGE;
43 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +010044
Thomas Grafa5531a52006-08-26 20:11:47 -070045 case NLA_NUL_STRING:
46 if (pt->len)
47 minlen = min_t(int, attrlen, pt->len + 1);
48 else
49 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +010050
Thomas Grafa5531a52006-08-26 20:11:47 -070051 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
52 return -EINVAL;
53 /* fall through */
54
55 case NLA_STRING:
56 if (attrlen < 1)
57 return -ERANGE;
58
59 if (pt->len) {
60 char *buf = nla_data(nla);
61
62 if (buf[attrlen - 1] == '\0')
63 attrlen--;
64
65 if (attrlen > pt->len)
66 return -ERANGE;
67 }
68 break;
69
Johannes Bergd30045a2007-03-23 11:37:48 -070070 case NLA_BINARY:
71 if (pt->len && attrlen > pt->len)
72 return -ERANGE;
73 break;
74
Patrick McHardy1092cb22007-06-25 13:49:35 -070075 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;
Patrick McHardyea5693c2008-11-28 03:05:19 -080086 case NLA_NESTED:
87 /* a nested attributes is allowed to be empty; if its not,
88 * it must have a size of at least NLA_HDRLEN.
89 */
90 if (attrlen == 0)
91 break;
Thomas Grafa5531a52006-08-26 20:11:47 -070092 default:
93 if (pt->len)
94 minlen = pt->len;
95 else if (pt->type != NLA_UNSPEC)
96 minlen = nla_attr_minlen[pt->type];
97
98 if (attrlen < minlen)
99 return -ERANGE;
100 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100101
102 return 0;
103}
104
105/**
106 * nla_validate - Validate a stream of attributes
107 * @head: head of attribute stream
108 * @len: length of attribute stream
109 * @maxtype: maximum attribute type to be expected
110 * @policy: validation policy
111 *
112 * Validates all attributes in the specified attribute stream against the
113 * specified policy. Attributes with a type exceeding maxtype will be
114 * ignored. See documenation of struct nla_policy for more details.
115 *
116 * Returns 0 on success or a negative error code.
117 */
118int nla_validate(struct nlattr *head, int len, int maxtype,
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700119 const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100120{
121 struct nlattr *nla;
122 int rem, err;
123
124 nla_for_each_attr(nla, head, len, rem) {
125 err = validate_nla(nla, maxtype, policy);
126 if (err < 0)
127 goto errout;
128 }
129
130 err = 0;
131errout:
132 return err;
133}
134
135/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100136 * nla_policy_len - Determin the max. length of a policy
137 * @policy: policy to use
138 * @n: number of policies
139 *
140 * Determines the max. length of the policy. It is currently used
141 * to allocated Netlink buffers roughly the size of the actual
142 * message.
143 *
144 * Returns 0 on success or a negative error code.
145 */
146int
147nla_policy_len(const struct nla_policy *p, int n)
148{
149 int i, len = 0;
150
151 for (i = 0; i < n; i++) {
152 if (p->len)
153 len += nla_total_size(p->len);
154 else if (nla_attr_minlen[p->type])
155 len += nla_total_size(nla_attr_minlen[p->type]);
156 }
157
158 return len;
159}
160
161/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100162 * nla_parse - Parse a stream of attributes into a tb buffer
163 * @tb: destination array with maxtype+1 elements
164 * @maxtype: maximum attribute type to be expected
165 * @head: head of attribute stream
166 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700167 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100168 *
169 * Parses a stream of attributes and stores a pointer to each attribute in
170 * the tb array accessable via the attribute type. Attributes with a type
171 * exceeding maxtype will be silently ignored for backwards compatibility
172 * reasons. policy may be set to NULL if no validation is required.
173 *
174 * Returns 0 on success or a negative error code.
175 */
176int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700177 const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100178{
179 struct nlattr *nla;
180 int rem, err;
181
182 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
183
184 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200185 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100186
187 if (type > 0 && type <= maxtype) {
188 if (policy) {
189 err = validate_nla(nla, maxtype, policy);
190 if (err < 0)
191 goto errout;
192 }
193
194 tb[type] = nla;
195 }
196 }
197
198 if (unlikely(rem > 0))
199 printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
200 "attributes.\n", rem);
201
202 err = 0;
203errout:
204 return err;
205}
206
207/**
208 * nla_find - Find a specific attribute in a stream of attributes
209 * @head: head of attribute stream
210 * @len: length of attribute stream
211 * @attrtype: type of attribute to look for
212 *
213 * Returns the first attribute in the stream matching the specified type.
214 */
215struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
216{
217 struct nlattr *nla;
218 int rem;
219
220 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200221 if (nla_type(nla) == attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100222 return nla;
223
224 return NULL;
225}
226
227/**
228 * nla_strlcpy - Copy string attribute payload into a sized buffer
229 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700230 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100231 * @dstsize: size of destination buffer
232 *
233 * Copies at most dstsize - 1 bytes into the destination buffer.
234 * The result is always a valid NUL-terminated string. Unlike
235 * strlcpy the destination buffer is always padded out.
236 *
237 * Returns the length of the source buffer.
238 */
239size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
240{
241 size_t srclen = nla_len(nla);
242 char *src = nla_data(nla);
243
244 if (srclen > 0 && src[srclen - 1] == '\0')
245 srclen--;
246
247 if (dstsize > 0) {
248 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
249
250 memset(dst, 0, dstsize);
251 memcpy(dst, src, len);
252 }
253
254 return srclen;
255}
256
257/**
258 * nla_memcpy - Copy a netlink attribute into another memory area
259 * @dest: where to copy to memcpy
260 * @src: netlink attribute to copy from
261 * @count: size of the destination area
262 *
263 * Note: The number of bytes copied is limited by the length of
264 * attribute's payload. memcpy
265 *
266 * Returns the number of bytes copied.
267 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700268int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100269{
270 int minlen = min_t(int, count, nla_len(src));
271
272 memcpy(dest, nla_data(src), minlen);
273
274 return minlen;
275}
276
277/**
278 * nla_memcmp - Compare an attribute with sized memory area
279 * @nla: netlink attribute
280 * @data: memory area
281 * @size: size of memory area
282 */
283int nla_memcmp(const struct nlattr *nla, const void *data,
284 size_t size)
285{
286 int d = nla_len(nla) - size;
287
288 if (d == 0)
289 d = memcmp(nla_data(nla), data, size);
290
291 return d;
292}
293
294/**
295 * nla_strcmp - Compare a string attribute against a string
296 * @nla: netlink string attribute
297 * @str: another string
298 */
299int nla_strcmp(const struct nlattr *nla, const char *str)
300{
301 int len = strlen(str) + 1;
302 int d = nla_len(nla) - len;
303
304 if (d == 0)
305 d = memcmp(nla_data(nla), str, len);
306
307 return d;
308}
309
310/**
311 * __nla_reserve - reserve room for attribute on the skb
312 * @skb: socket buffer to reserve room on
313 * @attrtype: attribute type
314 * @attrlen: length of attribute payload
315 *
316 * Adds a netlink attribute header to a socket buffer and reserves
317 * room for the payload but does not copy it.
318 *
319 * The caller is responsible to ensure that the skb provides enough
320 * tailroom for the attribute header and payload.
321 */
322struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
323{
324 struct nlattr *nla;
325
326 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
327 nla->nla_type = attrtype;
328 nla->nla_len = nla_attr_size(attrlen);
329
330 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
331
332 return nla;
333}
334
335/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700336 * __nla_reserve_nohdr - reserve room for attribute without header
337 * @skb: socket buffer to reserve room on
338 * @attrlen: length of attribute payload
339 *
340 * Reserves room for attribute payload without a header.
341 *
342 * The caller is responsible to ensure that the skb provides enough
343 * tailroom for the payload.
344 */
345void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
346{
347 void *start;
348
349 start = skb_put(skb, NLA_ALIGN(attrlen));
350 memset(start, 0, NLA_ALIGN(attrlen));
351
352 return start;
353}
354
355/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100356 * nla_reserve - reserve room for attribute on the skb
357 * @skb: socket buffer to reserve room on
358 * @attrtype: attribute type
359 * @attrlen: length of attribute payload
360 *
361 * Adds a netlink attribute header to a socket buffer and reserves
362 * room for the payload but does not copy it.
363 *
364 * Returns NULL if the tailroom of the skb is insufficient to store
365 * the attribute header and payload.
366 */
367struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
368{
369 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
370 return NULL;
371
372 return __nla_reserve(skb, attrtype, attrlen);
373}
374
375/**
Julius Volz10b595a2008-06-27 20:02:14 -0700376 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700377 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700378 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700379 *
380 * Reserves room for attribute payload without a header.
381 *
382 * Returns NULL if the tailroom of the skb is insufficient to store
383 * the attribute payload.
384 */
385void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
386{
387 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
388 return NULL;
389
390 return __nla_reserve_nohdr(skb, attrlen);
391}
392
393/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100394 * __nla_put - Add a netlink attribute to a socket buffer
395 * @skb: socket buffer to add attribute to
396 * @attrtype: attribute type
397 * @attrlen: length of attribute payload
398 * @data: head of attribute payload
399 *
400 * The caller is responsible to ensure that the skb provides enough
401 * tailroom for the attribute header and payload.
402 */
403void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
404 const void *data)
405{
406 struct nlattr *nla;
407
408 nla = __nla_reserve(skb, attrtype, attrlen);
409 memcpy(nla_data(nla), data, attrlen);
410}
411
Thomas Graffe4944e2006-08-04 23:03:05 -0700412/**
413 * __nla_put_nohdr - Add a netlink attribute without header
414 * @skb: socket buffer to add attribute to
415 * @attrlen: length of attribute payload
416 * @data: head of attribute payload
417 *
418 * The caller is responsible to ensure that the skb provides enough
419 * tailroom for the attribute payload.
420 */
421void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
422{
423 void *start;
424
425 start = __nla_reserve_nohdr(skb, attrlen);
426 memcpy(start, data, attrlen);
427}
Thomas Grafbfa83a92005-11-10 02:25:51 +0100428
429/**
430 * nla_put - Add a netlink attribute to a socket buffer
431 * @skb: socket buffer to add attribute to
432 * @attrtype: attribute type
433 * @attrlen: length of attribute payload
434 * @data: head of attribute payload
435 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700436 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100437 * the attribute header and payload.
438 */
439int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
440{
441 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700442 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100443
444 __nla_put(skb, attrtype, attrlen, data);
445 return 0;
446}
447
Thomas Graffe4944e2006-08-04 23:03:05 -0700448/**
449 * nla_put_nohdr - Add a netlink attribute without header
450 * @skb: socket buffer to add attribute to
451 * @attrlen: length of attribute payload
452 * @data: head of attribute payload
453 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700454 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700455 * the attribute payload.
456 */
457int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
458{
459 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700460 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700461
462 __nla_put_nohdr(skb, attrlen, data);
463 return 0;
464}
Thomas Grafbfa83a92005-11-10 02:25:51 +0100465
Patrick McHardy01480e12008-01-22 22:10:59 -0800466/**
467 * nla_append - Add a netlink attribute without header or padding
468 * @skb: socket buffer to add attribute to
469 * @attrlen: length of attribute payload
470 * @data: head of attribute payload
471 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700472 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800473 * the attribute payload.
474 */
475int nla_append(struct sk_buff *skb, int attrlen, const void *data)
476{
477 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700478 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800479
480 memcpy(skb_put(skb, attrlen), data, attrlen);
481 return 0;
482}
483
Thomas Grafbfa83a92005-11-10 02:25:51 +0100484EXPORT_SYMBOL(nla_validate);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100485EXPORT_SYMBOL(nla_policy_len);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100486EXPORT_SYMBOL(nla_parse);
487EXPORT_SYMBOL(nla_find);
488EXPORT_SYMBOL(nla_strlcpy);
489EXPORT_SYMBOL(__nla_reserve);
Thomas Graffe4944e2006-08-04 23:03:05 -0700490EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100491EXPORT_SYMBOL(nla_reserve);
Thomas Graffe4944e2006-08-04 23:03:05 -0700492EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100493EXPORT_SYMBOL(__nla_put);
Thomas Graffe4944e2006-08-04 23:03:05 -0700494EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100495EXPORT_SYMBOL(nla_put);
Thomas Graffe4944e2006-08-04 23:03:05 -0700496EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100497EXPORT_SYMBOL(nla_memcpy);
498EXPORT_SYMBOL(nla_memcmp);
499EXPORT_SYMBOL(nla_strcmp);
Patrick McHardy01480e12008-01-22 22:10:59 -0800500EXPORT_SYMBOL(nla_append);