blob: 18eca7809b08894cd135519f8991e138bbc0fa2a [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
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05008#include <linux/export.h>
Thomas Grafbfa83a92005-11-10 02:25:51 +01009#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
Jan Engelhardt36546542010-11-16 09:52:32 -080018static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = {
Thomas Grafbfa83a92005-11-10 02:25:51 +010019 [NLA_U8] = sizeof(u8),
20 [NLA_U16] = sizeof(u16),
21 [NLA_U32] = sizeof(u32),
22 [NLA_U64] = sizeof(u64),
Johannes Bergc30bc942011-11-03 00:07:32 +000023 [NLA_MSECS] = sizeof(u64),
Thomas Grafbfa83a92005-11-10 02:25:51 +010024 [NLA_NESTED] = NLA_HDRLEN,
Julian Anastasov9eca2eb2012-08-25 22:47:57 +000025 [NLA_S8] = sizeof(s8),
26 [NLA_S16] = sizeof(s16),
27 [NLA_S32] = sizeof(s32),
28 [NLA_S64] = sizeof(s64),
Thomas Grafbfa83a92005-11-10 02:25:51 +010029};
30
Jan Engelhardt36546542010-11-16 09:52:32 -080031static int validate_nla(const struct nlattr *nla, int maxtype,
Patrick McHardyef7c79e2007-06-05 12:38:30 -070032 const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +010033{
Patrick McHardyef7c79e2007-06-05 12:38:30 -070034 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +020035 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +010036
Thomas Graf8f4c1f92007-09-12 14:44:36 +020037 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +010038 return 0;
39
Thomas Graf8f4c1f92007-09-12 14:44:36 +020040 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +010041
42 BUG_ON(pt->type > NLA_TYPE_MAX);
43
Thomas Grafa5531a52006-08-26 20:11:47 -070044 switch (pt->type) {
45 case NLA_FLAG:
46 if (attrlen > 0)
47 return -ERANGE;
48 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +010049
Thomas Grafa5531a52006-08-26 20:11:47 -070050 case NLA_NUL_STRING:
51 if (pt->len)
52 minlen = min_t(int, attrlen, pt->len + 1);
53 else
54 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +010055
Thomas Grafa5531a52006-08-26 20:11:47 -070056 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
57 return -EINVAL;
58 /* fall through */
59
60 case NLA_STRING:
61 if (attrlen < 1)
62 return -ERANGE;
63
64 if (pt->len) {
65 char *buf = nla_data(nla);
66
67 if (buf[attrlen - 1] == '\0')
68 attrlen--;
69
70 if (attrlen > pt->len)
71 return -ERANGE;
72 }
73 break;
74
Johannes Bergd30045a2007-03-23 11:37:48 -070075 case NLA_BINARY:
76 if (pt->len && attrlen > pt->len)
77 return -ERANGE;
78 break;
79
Patrick McHardy1092cb22007-06-25 13:49:35 -070080 case NLA_NESTED_COMPAT:
81 if (attrlen < pt->len)
82 return -ERANGE;
83 if (attrlen < NLA_ALIGN(pt->len))
84 break;
85 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
86 return -ERANGE;
87 nla = nla_data(nla) + NLA_ALIGN(pt->len);
88 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
89 return -ERANGE;
90 break;
Patrick McHardyea5693c2008-11-28 03:05:19 -080091 case NLA_NESTED:
92 /* a nested attributes is allowed to be empty; if its not,
93 * it must have a size of at least NLA_HDRLEN.
94 */
95 if (attrlen == 0)
96 break;
Thomas Grafa5531a52006-08-26 20:11:47 -070097 default:
98 if (pt->len)
99 minlen = pt->len;
100 else if (pt->type != NLA_UNSPEC)
101 minlen = nla_attr_minlen[pt->type];
102
103 if (attrlen < minlen)
104 return -ERANGE;
105 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100106
107 return 0;
108}
109
110/**
111 * nla_validate - Validate a stream of attributes
112 * @head: head of attribute stream
113 * @len: length of attribute stream
114 * @maxtype: maximum attribute type to be expected
115 * @policy: validation policy
116 *
117 * Validates all attributes in the specified attribute stream against the
118 * specified policy. Attributes with a type exceeding maxtype will be
119 * ignored. See documenation of struct nla_policy for more details.
120 *
121 * Returns 0 on success or a negative error code.
122 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800123int nla_validate(const struct nlattr *head, int len, int maxtype,
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700124 const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100125{
Jan Engelhardt36546542010-11-16 09:52:32 -0800126 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100127 int rem, err;
128
129 nla_for_each_attr(nla, head, len, rem) {
130 err = validate_nla(nla, maxtype, policy);
131 if (err < 0)
132 goto errout;
133 }
134
135 err = 0;
136errout:
137 return err;
138}
139
140/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100141 * nla_policy_len - Determin the max. length of a policy
142 * @policy: policy to use
143 * @n: number of policies
144 *
145 * Determines the max. length of the policy. It is currently used
146 * to allocated Netlink buffers roughly the size of the actual
147 * message.
148 *
149 * Returns 0 on success or a negative error code.
150 */
151int
152nla_policy_len(const struct nla_policy *p, int n)
153{
154 int i, len = 0;
155
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800156 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100157 if (p->len)
158 len += nla_total_size(p->len);
159 else if (nla_attr_minlen[p->type])
160 len += nla_total_size(nla_attr_minlen[p->type]);
161 }
162
163 return len;
164}
165
166/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100167 * nla_parse - Parse a stream of attributes into a tb buffer
168 * @tb: destination array with maxtype+1 elements
169 * @maxtype: maximum attribute type to be expected
170 * @head: head of attribute stream
171 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700172 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100173 *
174 * Parses a stream of attributes and stores a pointer to each attribute in
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400175 * the tb array accessible via the attribute type. Attributes with a type
Thomas Grafbfa83a92005-11-10 02:25:51 +0100176 * exceeding maxtype will be silently ignored for backwards compatibility
177 * reasons. policy may be set to NULL if no validation is required.
178 *
179 * Returns 0 on success or a negative error code.
180 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800181int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
182 int len, const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100183{
Jan Engelhardt36546542010-11-16 09:52:32 -0800184 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100185 int rem, err;
186
187 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
188
189 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200190 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100191
192 if (type > 0 && type <= maxtype) {
193 if (policy) {
194 err = validate_nla(nla, maxtype, policy);
195 if (err < 0)
196 goto errout;
197 }
198
Jan Engelhardt36546542010-11-16 09:52:32 -0800199 tb[type] = (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100200 }
201 }
202
203 if (unlikely(rem > 0))
204 printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
205 "attributes.\n", rem);
206
207 err = 0;
208errout:
209 return err;
210}
211
212/**
213 * nla_find - Find a specific attribute in a stream of attributes
214 * @head: head of attribute stream
215 * @len: length of attribute stream
216 * @attrtype: type of attribute to look for
217 *
218 * Returns the first attribute in the stream matching the specified type.
219 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800220struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100221{
Jan Engelhardt36546542010-11-16 09:52:32 -0800222 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100223 int rem;
224
225 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200226 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800227 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100228
229 return NULL;
230}
231
232/**
233 * nla_strlcpy - Copy string attribute payload into a sized buffer
234 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700235 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100236 * @dstsize: size of destination buffer
237 *
238 * Copies at most dstsize - 1 bytes into the destination buffer.
239 * The result is always a valid NUL-terminated string. Unlike
240 * strlcpy the destination buffer is always padded out.
241 *
242 * Returns the length of the source buffer.
243 */
244size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
245{
246 size_t srclen = nla_len(nla);
247 char *src = nla_data(nla);
248
249 if (srclen > 0 && src[srclen - 1] == '\0')
250 srclen--;
251
252 if (dstsize > 0) {
253 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
254
255 memset(dst, 0, dstsize);
256 memcpy(dst, src, len);
257 }
258
259 return srclen;
260}
261
262/**
263 * nla_memcpy - Copy a netlink attribute into another memory area
264 * @dest: where to copy to memcpy
265 * @src: netlink attribute to copy from
266 * @count: size of the destination area
267 *
268 * Note: The number of bytes copied is limited by the length of
269 * attribute's payload. memcpy
270 *
271 * Returns the number of bytes copied.
272 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700273int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100274{
275 int minlen = min_t(int, count, nla_len(src));
276
277 memcpy(dest, nla_data(src), minlen);
278
279 return minlen;
280}
281
282/**
283 * nla_memcmp - Compare an attribute with sized memory area
284 * @nla: netlink attribute
285 * @data: memory area
286 * @size: size of memory area
287 */
288int nla_memcmp(const struct nlattr *nla, const void *data,
289 size_t size)
290{
291 int d = nla_len(nla) - size;
292
293 if (d == 0)
294 d = memcmp(nla_data(nla), data, size);
295
296 return d;
297}
298
299/**
300 * nla_strcmp - Compare a string attribute against a string
301 * @nla: netlink string attribute
302 * @str: another string
303 */
304int nla_strcmp(const struct nlattr *nla, const char *str)
305{
306 int len = strlen(str) + 1;
307 int d = nla_len(nla) - len;
308
309 if (d == 0)
310 d = memcmp(nla_data(nla), str, len);
311
312 return d;
313}
314
Herbert Xu90800212009-03-11 23:18:32 +0800315#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100316/**
317 * __nla_reserve - reserve room for attribute on the skb
318 * @skb: socket buffer to reserve room on
319 * @attrtype: attribute type
320 * @attrlen: length of attribute payload
321 *
322 * Adds a netlink attribute header to a socket buffer and reserves
323 * room for the payload but does not copy it.
324 *
325 * The caller is responsible to ensure that the skb provides enough
326 * tailroom for the attribute header and payload.
327 */
328struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
329{
330 struct nlattr *nla;
331
332 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
333 nla->nla_type = attrtype;
334 nla->nla_len = nla_attr_size(attrlen);
335
336 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
337
338 return nla;
339}
Herbert Xu90800212009-03-11 23:18:32 +0800340EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100341
342/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700343 * __nla_reserve_nohdr - reserve room for attribute without header
344 * @skb: socket buffer to reserve room on
345 * @attrlen: length of attribute payload
346 *
347 * Reserves room for attribute payload without a header.
348 *
349 * The caller is responsible to ensure that the skb provides enough
350 * tailroom for the payload.
351 */
352void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
353{
354 void *start;
355
356 start = skb_put(skb, NLA_ALIGN(attrlen));
357 memset(start, 0, NLA_ALIGN(attrlen));
358
359 return start;
360}
Herbert Xu90800212009-03-11 23:18:32 +0800361EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700362
363/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100364 * nla_reserve - reserve room for attribute on the skb
365 * @skb: socket buffer to reserve room on
366 * @attrtype: attribute type
367 * @attrlen: length of attribute payload
368 *
369 * Adds a netlink attribute header to a socket buffer and reserves
370 * room for the payload but does not copy it.
371 *
372 * Returns NULL if the tailroom of the skb is insufficient to store
373 * the attribute header and payload.
374 */
375struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
376{
377 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
378 return NULL;
379
380 return __nla_reserve(skb, attrtype, attrlen);
381}
Herbert Xu90800212009-03-11 23:18:32 +0800382EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100383
384/**
Julius Volz10b595a2008-06-27 20:02:14 -0700385 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700386 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700387 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700388 *
389 * Reserves room for attribute payload without a header.
390 *
391 * Returns NULL if the tailroom of the skb is insufficient to store
392 * the attribute payload.
393 */
394void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
395{
396 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
397 return NULL;
398
399 return __nla_reserve_nohdr(skb, attrlen);
400}
Herbert Xu90800212009-03-11 23:18:32 +0800401EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700402
403/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100404 * __nla_put - Add a netlink attribute to a socket buffer
405 * @skb: socket buffer to add attribute to
406 * @attrtype: attribute type
407 * @attrlen: length of attribute payload
408 * @data: head of attribute payload
409 *
410 * The caller is responsible to ensure that the skb provides enough
411 * tailroom for the attribute header and payload.
412 */
413void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
414 const void *data)
415{
416 struct nlattr *nla;
417
418 nla = __nla_reserve(skb, attrtype, attrlen);
419 memcpy(nla_data(nla), data, attrlen);
420}
Herbert Xu90800212009-03-11 23:18:32 +0800421EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100422
Thomas Graffe4944e2006-08-04 23:03:05 -0700423/**
424 * __nla_put_nohdr - Add a netlink attribute without header
425 * @skb: socket buffer to add attribute to
426 * @attrlen: length of attribute payload
427 * @data: head of attribute payload
428 *
429 * The caller is responsible to ensure that the skb provides enough
430 * tailroom for the attribute payload.
431 */
432void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
433{
434 void *start;
435
436 start = __nla_reserve_nohdr(skb, attrlen);
437 memcpy(start, data, attrlen);
438}
Herbert Xu90800212009-03-11 23:18:32 +0800439EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100440
441/**
442 * nla_put - Add a netlink attribute to a socket buffer
443 * @skb: socket buffer to add attribute to
444 * @attrtype: attribute type
445 * @attrlen: length of attribute payload
446 * @data: head of attribute payload
447 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700448 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100449 * the attribute header and payload.
450 */
451int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
452{
453 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700454 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100455
456 __nla_put(skb, attrtype, attrlen, data);
457 return 0;
458}
Herbert Xu90800212009-03-11 23:18:32 +0800459EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100460
Thomas Graffe4944e2006-08-04 23:03:05 -0700461/**
462 * nla_put_nohdr - Add a netlink attribute without header
463 * @skb: socket buffer to add attribute to
464 * @attrlen: length of attribute payload
465 * @data: head of attribute payload
466 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700467 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700468 * the attribute payload.
469 */
470int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
471{
472 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700473 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700474
475 __nla_put_nohdr(skb, attrlen, data);
476 return 0;
477}
Herbert Xu90800212009-03-11 23:18:32 +0800478EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100479
Patrick McHardy01480e12008-01-22 22:10:59 -0800480/**
481 * nla_append - Add a netlink attribute without header or padding
482 * @skb: socket buffer to add attribute to
483 * @attrlen: length of attribute payload
484 * @data: head of attribute payload
485 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700486 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800487 * the attribute payload.
488 */
489int nla_append(struct sk_buff *skb, int attrlen, const void *data)
490{
491 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700492 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800493
494 memcpy(skb_put(skb, attrlen), data, attrlen);
495 return 0;
496}
Herbert Xu90800212009-03-11 23:18:32 +0800497EXPORT_SYMBOL(nla_append);
498#endif
Patrick McHardy01480e12008-01-22 22:10:59 -0800499
Thomas Grafbfa83a92005-11-10 02:25:51 +0100500EXPORT_SYMBOL(nla_validate);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100501EXPORT_SYMBOL(nla_policy_len);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100502EXPORT_SYMBOL(nla_parse);
503EXPORT_SYMBOL(nla_find);
504EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100505EXPORT_SYMBOL(nla_memcpy);
506EXPORT_SYMBOL(nla_memcmp);
507EXPORT_SYMBOL(nla_strcmp);