2 * NETLINK Netlink attributes
4 * Authors: Thomas Graf <tgraf@suug.ch>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 #include <linux/export.h>
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/jiffies.h>
12 #include <linux/skbuff.h>
13 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <net/netlink.h>
17 static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = {
18 [NLA_U8] = sizeof(u8),
19 [NLA_U16] = sizeof(u16),
20 [NLA_U32] = sizeof(u32),
21 [NLA_U64] = sizeof(u64),
22 [NLA_MSECS] = sizeof(u64),
23 [NLA_NESTED] = NLA_HDRLEN,
24 [NLA_S8] = sizeof(s8),
25 [NLA_S16] = sizeof(s16),
26 [NLA_S32] = sizeof(s32),
27 [NLA_S64] = sizeof(s64),
30 static int validate_nla(const struct nlattr *nla, int maxtype,
31 const struct nla_policy *policy)
33 const struct nla_policy *pt;
34 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
36 if (type <= 0 || type > maxtype)
41 BUG_ON(pt->type > NLA_TYPE_MAX);
51 minlen = min_t(int, attrlen, pt->len + 1);
55 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
64 char *buf = nla_data(nla);
66 if (buf[attrlen - 1] == '\0')
69 if (attrlen > pt->len)
75 if (pt->len && attrlen > pt->len)
79 case NLA_NESTED_COMPAT:
80 if (attrlen < pt->len)
82 if (attrlen < NLA_ALIGN(pt->len))
84 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
86 nla = nla_data(nla) + NLA_ALIGN(pt->len);
87 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
91 /* a nested attributes is allowed to be empty; if its not,
92 * it must have a size of at least NLA_HDRLEN.
99 else if (pt->type != NLA_UNSPEC)
100 minlen = nla_attr_minlen[pt->type];
102 if (attrlen < minlen)
110 * nla_validate - Validate a stream of attributes
111 * @head: head of attribute stream
112 * @len: length of attribute stream
113 * @maxtype: maximum attribute type to be expected
114 * @policy: validation policy
116 * Validates all attributes in the specified attribute stream against the
117 * specified policy. Attributes with a type exceeding maxtype will be
118 * ignored. See documenation of struct nla_policy for more details.
120 * Returns 0 on success or a negative error code.
122 int nla_validate(const struct nlattr *head, int len, int maxtype,
123 const struct nla_policy *policy)
125 const struct nlattr *nla;
128 nla_for_each_attr(nla, head, len, rem) {
129 err = validate_nla(nla, maxtype, policy);
138 EXPORT_SYMBOL(nla_validate);
141 * nla_policy_len - Determin the max. length of a policy
142 * @policy: policy to use
143 * @n: number of policies
145 * Determines the max. length of the policy. It is currently used
146 * to allocated Netlink buffers roughly the size of the actual
149 * Returns 0 on success or a negative error code.
152 nla_policy_len(const struct nla_policy *p, int n)
156 for (i = 0; i < n; i++, p++) {
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]);
165 EXPORT_SYMBOL(nla_policy_len);
168 * nla_parse - Parse a stream of attributes into a tb buffer
169 * @tb: destination array with maxtype+1 elements
170 * @maxtype: maximum attribute type to be expected
171 * @head: head of attribute stream
172 * @len: length of attribute stream
173 * @policy: validation policy
175 * Parses a stream of attributes and stores a pointer to each attribute in
176 * the tb array accessible via the attribute type. Attributes with a type
177 * exceeding maxtype will be silently ignored for backwards compatibility
178 * reasons. policy may be set to NULL if no validation is required.
180 * Returns 0 on success or a negative error code.
182 int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
183 int len, const struct nla_policy *policy)
185 const struct nlattr *nla;
188 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
190 nla_for_each_attr(nla, head, len, rem) {
191 u16 type = nla_type(nla);
193 if (type > 0 && type <= maxtype) {
195 err = validate_nla(nla, maxtype, policy);
200 tb[type] = (struct nlattr *)nla;
204 if (unlikely(rem > 0))
205 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
212 EXPORT_SYMBOL(nla_parse);
215 * nla_find - Find a specific attribute in a stream of attributes
216 * @head: head of attribute stream
217 * @len: length of attribute stream
218 * @attrtype: type of attribute to look for
220 * Returns the first attribute in the stream matching the specified type.
222 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
224 const struct nlattr *nla;
227 nla_for_each_attr(nla, head, len, rem)
228 if (nla_type(nla) == attrtype)
229 return (struct nlattr *)nla;
233 EXPORT_SYMBOL(nla_find);
236 * nla_strlcpy - Copy string attribute payload into a sized buffer
237 * @dst: where to copy the string to
238 * @nla: attribute to copy the string from
239 * @dstsize: size of destination buffer
241 * Copies at most dstsize - 1 bytes into the destination buffer.
242 * The result is always a valid NUL-terminated string. Unlike
243 * strlcpy the destination buffer is always padded out.
245 * Returns the length of the source buffer.
247 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
249 size_t srclen = nla_len(nla);
250 char *src = nla_data(nla);
252 if (srclen > 0 && src[srclen - 1] == '\0')
256 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
258 memset(dst, 0, dstsize);
259 memcpy(dst, src, len);
264 EXPORT_SYMBOL(nla_strlcpy);
267 * nla_memcpy - Copy a netlink attribute into another memory area
268 * @dest: where to copy to memcpy
269 * @src: netlink attribute to copy from
270 * @count: size of the destination area
272 * Note: The number of bytes copied is limited by the length of
273 * attribute's payload. memcpy
275 * Returns the number of bytes copied.
277 int nla_memcpy(void *dest, const struct nlattr *src, int count)
279 int minlen = min_t(int, count, nla_len(src));
281 memcpy(dest, nla_data(src), minlen);
283 memset(dest + minlen, 0, count - minlen);
287 EXPORT_SYMBOL(nla_memcpy);
290 * nla_memcmp - Compare an attribute with sized memory area
291 * @nla: netlink attribute
293 * @size: size of memory area
295 int nla_memcmp(const struct nlattr *nla, const void *data,
298 int d = nla_len(nla) - size;
301 d = memcmp(nla_data(nla), data, size);
305 EXPORT_SYMBOL(nla_memcmp);
308 * nla_strcmp - Compare a string attribute against a string
309 * @nla: netlink string attribute
310 * @str: another string
312 int nla_strcmp(const struct nlattr *nla, const char *str)
314 int len = strlen(str);
315 char *buf = nla_data(nla);
316 int attrlen = nla_len(nla);
319 if (attrlen > 0 && buf[attrlen - 1] == '\0')
324 d = memcmp(nla_data(nla), str, len);
328 EXPORT_SYMBOL(nla_strcmp);
332 * __nla_reserve - reserve room for attribute on the skb
333 * @skb: socket buffer to reserve room on
334 * @attrtype: attribute type
335 * @attrlen: length of attribute payload
337 * Adds a netlink attribute header to a socket buffer and reserves
338 * room for the payload but does not copy it.
340 * The caller is responsible to ensure that the skb provides enough
341 * tailroom for the attribute header and payload.
343 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
347 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
348 nla->nla_type = attrtype;
349 nla->nla_len = nla_attr_size(attrlen);
351 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
355 EXPORT_SYMBOL(__nla_reserve);
358 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
359 * @skb: socket buffer to reserve room on
360 * @attrtype: attribute type
361 * @attrlen: length of attribute payload
363 * Adds a netlink attribute header to a socket buffer and reserves
364 * room for the payload but does not copy it. It also ensure that this
365 * attribute will be 64-bit aign.
367 * The caller is responsible to ensure that the skb provides enough
368 * tailroom for the attribute header and payload.
370 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
371 int attrlen, int padattr)
373 if (nla_need_padding_for_64bit(skb))
374 nla_align_64bit(skb, padattr);
376 return __nla_reserve(skb, attrtype, attrlen);
378 EXPORT_SYMBOL(__nla_reserve_64bit);
381 * __nla_reserve_nohdr - reserve room for attribute without header
382 * @skb: socket buffer to reserve room on
383 * @attrlen: length of attribute payload
385 * Reserves room for attribute payload without a header.
387 * The caller is responsible to ensure that the skb provides enough
388 * tailroom for the payload.
390 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
394 start = skb_put(skb, NLA_ALIGN(attrlen));
395 memset(start, 0, NLA_ALIGN(attrlen));
399 EXPORT_SYMBOL(__nla_reserve_nohdr);
402 * nla_reserve - reserve room for attribute on the skb
403 * @skb: socket buffer to reserve room on
404 * @attrtype: attribute type
405 * @attrlen: length of attribute payload
407 * Adds a netlink attribute header to a socket buffer and reserves
408 * room for the payload but does not copy it.
410 * Returns NULL if the tailroom of the skb is insufficient to store
411 * the attribute header and payload.
413 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
415 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
418 return __nla_reserve(skb, attrtype, attrlen);
420 EXPORT_SYMBOL(nla_reserve);
423 * nla_reserve_64bit - reserve room for attribute on the skb and align it
424 * @skb: socket buffer to reserve room on
425 * @attrtype: attribute type
426 * @attrlen: length of attribute payload
428 * Adds a netlink attribute header to a socket buffer and reserves
429 * room for the payload but does not copy it. It also ensure that this
430 * attribute will be 64-bit aign.
432 * Returns NULL if the tailroom of the skb is insufficient to store
433 * the attribute header and payload.
435 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
440 if (nla_need_padding_for_64bit(skb))
441 len = nla_total_size_64bit(attrlen);
443 len = nla_total_size(attrlen);
444 if (unlikely(skb_tailroom(skb) < len))
447 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
449 EXPORT_SYMBOL(nla_reserve_64bit);
452 * nla_reserve_nohdr - reserve room for attribute without header
453 * @skb: socket buffer to reserve room on
454 * @attrlen: length of attribute payload
456 * Reserves room for attribute payload without a header.
458 * Returns NULL if the tailroom of the skb is insufficient to store
459 * the attribute payload.
461 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
463 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
466 return __nla_reserve_nohdr(skb, attrlen);
468 EXPORT_SYMBOL(nla_reserve_nohdr);
471 * __nla_put - Add a netlink attribute to a socket buffer
472 * @skb: socket buffer to add attribute to
473 * @attrtype: attribute type
474 * @attrlen: length of attribute payload
475 * @data: head of attribute payload
477 * The caller is responsible to ensure that the skb provides enough
478 * tailroom for the attribute header and payload.
480 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
485 nla = __nla_reserve(skb, attrtype, attrlen);
486 memcpy(nla_data(nla), data, attrlen);
488 EXPORT_SYMBOL(__nla_put);
491 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
492 * @skb: socket buffer to add attribute to
493 * @attrtype: attribute type
494 * @attrlen: length of attribute payload
495 * @data: head of attribute payload
497 * The caller is responsible to ensure that the skb provides enough
498 * tailroom for the attribute header and payload.
500 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
501 const void *data, int padattr)
505 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
506 memcpy(nla_data(nla), data, attrlen);
508 EXPORT_SYMBOL(__nla_put_64bit);
511 * __nla_put_nohdr - Add a netlink attribute without header
512 * @skb: socket buffer to add attribute to
513 * @attrlen: length of attribute payload
514 * @data: head of attribute payload
516 * The caller is responsible to ensure that the skb provides enough
517 * tailroom for the attribute payload.
519 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
523 start = __nla_reserve_nohdr(skb, attrlen);
524 memcpy(start, data, attrlen);
526 EXPORT_SYMBOL(__nla_put_nohdr);
529 * nla_put - Add a netlink attribute to a socket buffer
530 * @skb: socket buffer to add attribute to
531 * @attrtype: attribute type
532 * @attrlen: length of attribute payload
533 * @data: head of attribute payload
535 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
536 * the attribute header and payload.
538 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
540 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
543 __nla_put(skb, attrtype, attrlen, data);
546 EXPORT_SYMBOL(nla_put);
549 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
550 * @skb: socket buffer to add attribute to
551 * @attrtype: attribute type
552 * @attrlen: length of attribute payload
553 * @data: head of attribute payload
555 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
556 * the attribute header and payload.
558 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
559 const void *data, int padattr)
563 if (nla_need_padding_for_64bit(skb))
564 len = nla_total_size_64bit(attrlen);
566 len = nla_total_size(attrlen);
567 if (unlikely(skb_tailroom(skb) < len))
570 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
573 EXPORT_SYMBOL(nla_put_64bit);
576 * nla_put_nohdr - Add a netlink attribute without header
577 * @skb: socket buffer to add attribute to
578 * @attrlen: length of attribute payload
579 * @data: head of attribute payload
581 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
582 * the attribute payload.
584 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
586 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
589 __nla_put_nohdr(skb, attrlen, data);
592 EXPORT_SYMBOL(nla_put_nohdr);
595 * nla_append - Add a netlink attribute without header or padding
596 * @skb: socket buffer to add attribute to
597 * @attrlen: length of attribute payload
598 * @data: head of attribute payload
600 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
601 * the attribute payload.
603 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
605 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
608 memcpy(skb_put(skb, attrlen), data, attrlen);
611 EXPORT_SYMBOL(nla_append);