blob: 60abf1b5545ab71823467f5b52ea10860947f0b0 [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>
Michal Schmidt11d6e452014-06-02 18:25:02 +020016#include <linux/ratelimit.h>
Thomas Grafbfa83a92005-11-10 02:25:51 +010017#include <net/netlink.h>
18
Jan Engelhardt36546542010-11-16 09:52:32 -080019static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = {
Thomas Grafbfa83a92005-11-10 02:25:51 +010020 [NLA_U8] = sizeof(u8),
21 [NLA_U16] = sizeof(u16),
22 [NLA_U32] = sizeof(u32),
23 [NLA_U64] = sizeof(u64),
Johannes Bergc30bc942011-11-03 00:07:32 +000024 [NLA_MSECS] = sizeof(u64),
Thomas Grafbfa83a92005-11-10 02:25:51 +010025 [NLA_NESTED] = NLA_HDRLEN,
26};
27
Jan Engelhardt36546542010-11-16 09:52:32 -080028static int validate_nla(const struct nlattr *nla, int maxtype,
Patrick McHardyef7c79e2007-06-05 12:38:30 -070029 const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +010030{
Patrick McHardyef7c79e2007-06-05 12:38:30 -070031 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +020032 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +010033
Thomas Graf8f4c1f92007-09-12 14:44:36 +020034 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +010035 return 0;
36
Thomas Graf8f4c1f92007-09-12 14:44:36 +020037 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +010038
39 BUG_ON(pt->type > NLA_TYPE_MAX);
40
Thomas Grafa5531a52006-08-26 20:11:47 -070041 switch (pt->type) {
42 case NLA_FLAG:
43 if (attrlen > 0)
44 return -ERANGE;
45 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +010046
Thomas Grafa5531a52006-08-26 20:11:47 -070047 case NLA_NUL_STRING:
48 if (pt->len)
49 minlen = min_t(int, attrlen, pt->len + 1);
50 else
51 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +010052
Thomas Grafa5531a52006-08-26 20:11:47 -070053 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
54 return -EINVAL;
55 /* fall through */
56
57 case NLA_STRING:
58 if (attrlen < 1)
59 return -ERANGE;
60
61 if (pt->len) {
62 char *buf = nla_data(nla);
63
64 if (buf[attrlen - 1] == '\0')
65 attrlen--;
66
67 if (attrlen > pt->len)
68 return -ERANGE;
69 }
70 break;
71
Johannes Bergd30045a2007-03-23 11:37:48 -070072 case NLA_BINARY:
73 if (pt->len && attrlen > pt->len)
74 return -ERANGE;
75 break;
76
Patrick McHardy1092cb22007-06-25 13:49:35 -070077 case NLA_NESTED_COMPAT:
78 if (attrlen < pt->len)
79 return -ERANGE;
80 if (attrlen < NLA_ALIGN(pt->len))
81 break;
82 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
83 return -ERANGE;
84 nla = nla_data(nla) + NLA_ALIGN(pt->len);
85 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
86 return -ERANGE;
87 break;
Patrick McHardyea5693c2008-11-28 03:05:19 -080088 case NLA_NESTED:
89 /* a nested attributes is allowed to be empty; if its not,
90 * it must have a size of at least NLA_HDRLEN.
91 */
92 if (attrlen == 0)
93 break;
Thomas Grafa5531a52006-08-26 20:11:47 -070094 default:
95 if (pt->len)
96 minlen = pt->len;
97 else if (pt->type != NLA_UNSPEC)
98 minlen = nla_attr_minlen[pt->type];
99
100 if (attrlen < minlen)
101 return -ERANGE;
102 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100103
104 return 0;
105}
106
107/**
108 * nla_validate - Validate a stream of attributes
109 * @head: head of attribute stream
110 * @len: length of attribute stream
111 * @maxtype: maximum attribute type to be expected
112 * @policy: validation policy
113 *
114 * Validates all attributes in the specified attribute stream against the
115 * specified policy. Attributes with a type exceeding maxtype will be
116 * ignored. See documenation of struct nla_policy for more details.
117 *
118 * Returns 0 on success or a negative error code.
119 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800120int nla_validate(const struct nlattr *head, int len, int maxtype,
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700121 const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100122{
Jan Engelhardt36546542010-11-16 09:52:32 -0800123 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100124 int rem, err;
125
126 nla_for_each_attr(nla, head, len, rem) {
127 err = validate_nla(nla, maxtype, policy);
128 if (err < 0)
129 goto errout;
130 }
131
132 err = 0;
133errout:
134 return err;
135}
136
137/**
Holger Eitzenbergere487eb92009-03-25 18:26:30 +0100138 * nla_policy_len - Determin the max. length of a policy
139 * @policy: policy to use
140 * @n: number of policies
141 *
142 * Determines the max. length of the policy. It is currently used
143 * to allocated Netlink buffers roughly the size of the actual
144 * message.
145 *
146 * Returns 0 on success or a negative error code.
147 */
148int
149nla_policy_len(const struct nla_policy *p, int n)
150{
151 int i, len = 0;
152
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800153 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb92009-03-25 18:26:30 +0100154 if (p->len)
155 len += nla_total_size(p->len);
156 else if (nla_attr_minlen[p->type])
157 len += nla_total_size(nla_attr_minlen[p->type]);
158 }
159
160 return len;
161}
162
163/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100164 * nla_parse - Parse a stream of attributes into a tb buffer
165 * @tb: destination array with maxtype+1 elements
166 * @maxtype: maximum attribute type to be expected
167 * @head: head of attribute stream
168 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700169 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100170 *
171 * Parses a stream of attributes and stores a pointer to each attribute in
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400172 * the tb array accessible via the attribute type. Attributes with a type
Thomas Grafbfa83a92005-11-10 02:25:51 +0100173 * exceeding maxtype will be silently ignored for backwards compatibility
174 * reasons. policy may be set to NULL if no validation is required.
175 *
176 * Returns 0 on success or a negative error code.
177 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800178int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
179 int len, const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100180{
Jan Engelhardt36546542010-11-16 09:52:32 -0800181 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100182 int rem, err;
183
184 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
185
186 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200187 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100188
189 if (type > 0 && type <= maxtype) {
190 if (policy) {
191 err = validate_nla(nla, maxtype, policy);
192 if (err < 0)
193 goto errout;
194 }
195
Jan Engelhardt36546542010-11-16 09:52:32 -0800196 tb[type] = (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100197 }
198 }
199
200 if (unlikely(rem > 0))
Michal Schmidt11d6e452014-06-02 18:25:02 +0200201 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
202 rem, current->comm);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100203
204 err = 0;
205errout:
206 return err;
207}
208
209/**
210 * nla_find - Find a specific attribute in a stream of attributes
211 * @head: head of attribute stream
212 * @len: length of attribute stream
213 * @attrtype: type of attribute to look for
214 *
215 * Returns the first attribute in the stream matching the specified type.
216 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800217struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100218{
Jan Engelhardt36546542010-11-16 09:52:32 -0800219 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100220 int rem;
221
222 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200223 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800224 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100225
226 return NULL;
227}
228
229/**
230 * nla_strlcpy - Copy string attribute payload into a sized buffer
231 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700232 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100233 * @dstsize: size of destination buffer
234 *
235 * Copies at most dstsize - 1 bytes into the destination buffer.
236 * The result is always a valid NUL-terminated string. Unlike
237 * strlcpy the destination buffer is always padded out.
238 *
239 * Returns the length of the source buffer.
240 */
241size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
242{
243 size_t srclen = nla_len(nla);
244 char *src = nla_data(nla);
245
246 if (srclen > 0 && src[srclen - 1] == '\0')
247 srclen--;
248
249 if (dstsize > 0) {
250 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
251
252 memset(dst, 0, dstsize);
253 memcpy(dst, src, len);
254 }
255
256 return srclen;
257}
258
259/**
260 * nla_memcpy - Copy a netlink attribute into another memory area
261 * @dest: where to copy to memcpy
262 * @src: netlink attribute to copy from
263 * @count: size of the destination area
264 *
265 * Note: The number of bytes copied is limited by the length of
266 * attribute's payload. memcpy
267 *
268 * Returns the number of bytes copied.
269 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700270int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100271{
272 int minlen = min_t(int, count, nla_len(src));
273
274 memcpy(dest, nla_data(src), minlen);
275
276 return minlen;
277}
278
279/**
280 * nla_memcmp - Compare an attribute with sized memory area
281 * @nla: netlink attribute
282 * @data: memory area
283 * @size: size of memory area
284 */
285int nla_memcmp(const struct nlattr *nla, const void *data,
286 size_t size)
287{
288 int d = nla_len(nla) - size;
289
290 if (d == 0)
291 d = memcmp(nla_data(nla), data, size);
292
293 return d;
294}
295
296/**
297 * nla_strcmp - Compare a string attribute against a string
298 * @nla: netlink string attribute
299 * @str: another string
300 */
301int nla_strcmp(const struct nlattr *nla, const char *str)
302{
Pablo Neira9e51fbd2014-04-01 19:38:44 +0200303 int len = strlen(str);
304 char *buf = nla_data(nla);
305 int attrlen = nla_len(nla);
306 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100307
Pablo Neira9e51fbd2014-04-01 19:38:44 +0200308 if (attrlen > 0 && buf[attrlen - 1] == '\0')
309 attrlen--;
310
311 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100312 if (d == 0)
313 d = memcmp(nla_data(nla), str, len);
314
315 return d;
316}
317
Herbert Xu90800212009-03-11 23:18:32 +0800318#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100319/**
320 * __nla_reserve - reserve room for attribute on the skb
321 * @skb: socket buffer to reserve room on
322 * @attrtype: attribute type
323 * @attrlen: length of attribute payload
324 *
325 * Adds a netlink attribute header to a socket buffer and reserves
326 * room for the payload but does not copy it.
327 *
328 * The caller is responsible to ensure that the skb provides enough
329 * tailroom for the attribute header and payload.
330 */
331struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
332{
333 struct nlattr *nla;
334
335 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
336 nla->nla_type = attrtype;
337 nla->nla_len = nla_attr_size(attrlen);
338
339 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
340
341 return nla;
342}
Herbert Xu90800212009-03-11 23:18:32 +0800343EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100344
345/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700346 * __nla_reserve_nohdr - reserve room for attribute without header
347 * @skb: socket buffer to reserve room on
348 * @attrlen: length of attribute payload
349 *
350 * Reserves room for attribute payload without a header.
351 *
352 * The caller is responsible to ensure that the skb provides enough
353 * tailroom for the payload.
354 */
355void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
356{
357 void *start;
358
359 start = skb_put(skb, NLA_ALIGN(attrlen));
360 memset(start, 0, NLA_ALIGN(attrlen));
361
362 return start;
363}
Herbert Xu90800212009-03-11 23:18:32 +0800364EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700365
366/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100367 * nla_reserve - reserve room for attribute on the skb
368 * @skb: socket buffer to reserve room on
369 * @attrtype: attribute type
370 * @attrlen: length of attribute payload
371 *
372 * Adds a netlink attribute header to a socket buffer and reserves
373 * room for the payload but does not copy it.
374 *
375 * Returns NULL if the tailroom of the skb is insufficient to store
376 * the attribute header and payload.
377 */
378struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
379{
380 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
381 return NULL;
382
383 return __nla_reserve(skb, attrtype, attrlen);
384}
Herbert Xu90800212009-03-11 23:18:32 +0800385EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100386
387/**
Julius Volz10b595a2008-06-27 20:02:14 -0700388 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700389 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700390 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700391 *
392 * Reserves room for attribute payload without a header.
393 *
394 * Returns NULL if the tailroom of the skb is insufficient to store
395 * the attribute payload.
396 */
397void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
398{
399 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
400 return NULL;
401
402 return __nla_reserve_nohdr(skb, attrlen);
403}
Herbert Xu90800212009-03-11 23:18:32 +0800404EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700405
406/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100407 * __nla_put - Add a netlink attribute to a socket buffer
408 * @skb: socket buffer to add attribute to
409 * @attrtype: attribute type
410 * @attrlen: length of attribute payload
411 * @data: head of attribute payload
412 *
413 * The caller is responsible to ensure that the skb provides enough
414 * tailroom for the attribute header and payload.
415 */
416void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
417 const void *data)
418{
419 struct nlattr *nla;
420
421 nla = __nla_reserve(skb, attrtype, attrlen);
422 memcpy(nla_data(nla), data, attrlen);
423}
Herbert Xu90800212009-03-11 23:18:32 +0800424EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100425
Thomas Graffe4944e2006-08-04 23:03:05 -0700426/**
427 * __nla_put_nohdr - Add a netlink attribute without header
428 * @skb: socket buffer to add attribute to
429 * @attrlen: length of attribute payload
430 * @data: head of attribute payload
431 *
432 * The caller is responsible to ensure that the skb provides enough
433 * tailroom for the attribute payload.
434 */
435void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
436{
437 void *start;
438
439 start = __nla_reserve_nohdr(skb, attrlen);
440 memcpy(start, data, attrlen);
441}
Herbert Xu90800212009-03-11 23:18:32 +0800442EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100443
444/**
445 * nla_put - Add a netlink attribute to a socket buffer
446 * @skb: socket buffer to add attribute to
447 * @attrtype: attribute type
448 * @attrlen: length of attribute payload
449 * @data: head of attribute payload
450 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700451 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100452 * the attribute header and payload.
453 */
454int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
455{
456 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700457 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100458
459 __nla_put(skb, attrtype, attrlen, data);
460 return 0;
461}
Herbert Xu90800212009-03-11 23:18:32 +0800462EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100463
Thomas Graffe4944e2006-08-04 23:03:05 -0700464/**
465 * nla_put_nohdr - Add a netlink attribute without header
466 * @skb: socket buffer to add attribute to
467 * @attrlen: length of attribute payload
468 * @data: head of attribute payload
469 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700470 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700471 * the attribute payload.
472 */
473int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
474{
475 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700476 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700477
478 __nla_put_nohdr(skb, attrlen, data);
479 return 0;
480}
Herbert Xu90800212009-03-11 23:18:32 +0800481EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100482
Patrick McHardy01480e12008-01-22 22:10:59 -0800483/**
484 * nla_append - Add a netlink attribute without header or padding
485 * @skb: socket buffer to add attribute to
486 * @attrlen: length of attribute payload
487 * @data: head of attribute payload
488 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700489 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800490 * the attribute payload.
491 */
492int nla_append(struct sk_buff *skb, int attrlen, const void *data)
493{
494 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700495 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800496
497 memcpy(skb_put(skb, attrlen), data, attrlen);
498 return 0;
499}
Herbert Xu90800212009-03-11 23:18:32 +0800500EXPORT_SYMBOL(nla_append);
501#endif
Patrick McHardy01480e12008-01-22 22:10:59 -0800502
Thomas Grafbfa83a92005-11-10 02:25:51 +0100503EXPORT_SYMBOL(nla_validate);
Holger Eitzenbergere487eb92009-03-25 18:26:30 +0100504EXPORT_SYMBOL(nla_policy_len);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100505EXPORT_SYMBOL(nla_parse);
506EXPORT_SYMBOL(nla_find);
507EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100508EXPORT_SYMBOL(nla_memcpy);
509EXPORT_SYMBOL(nla_memcmp);
510EXPORT_SYMBOL(nla_strcmp);