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 u8 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_bitfield32(const struct nlattr
*nla
,
31 u32
*valid_flags_allowed
)
33 const struct nla_bitfield32
*bf
= nla_data(nla
);
34 u32
*valid_flags_mask
= valid_flags_allowed
;
36 if (!valid_flags_allowed
)
39 /*disallow invalid bit selector */
40 if (bf
->selector
& ~*valid_flags_mask
)
43 /*disallow invalid bit values */
44 if (bf
->value
& ~*valid_flags_mask
)
47 /*disallow valid bit values that are not selected*/
48 if (bf
->value
& ~bf
->selector
)
54 static int validate_nla(const struct nlattr
*nla
, int maxtype
,
55 const struct nla_policy
*policy
)
57 const struct nla_policy
*pt
;
58 int minlen
= 0, attrlen
= nla_len(nla
), type
= nla_type(nla
);
60 if (type
<= 0 || type
> maxtype
)
65 BUG_ON(pt
->type
> NLA_TYPE_MAX
);
74 if (attrlen
!= sizeof(struct nla_bitfield32
))
77 return validate_nla_bitfield32(nla
, pt
->validation_data
);
81 minlen
= min_t(int, attrlen
, pt
->len
+ 1);
85 if (!minlen
|| memchr(nla_data(nla
), '\0', minlen
) == NULL
)
94 char *buf
= nla_data(nla
);
96 if (buf
[attrlen
- 1] == '\0')
99 if (attrlen
> pt
->len
)
105 if (pt
->len
&& attrlen
> pt
->len
)
109 case NLA_NESTED_COMPAT
:
110 if (attrlen
< pt
->len
)
112 if (attrlen
< NLA_ALIGN(pt
->len
))
114 if (attrlen
< NLA_ALIGN(pt
->len
) + NLA_HDRLEN
)
116 nla
= nla_data(nla
) + NLA_ALIGN(pt
->len
);
117 if (attrlen
< NLA_ALIGN(pt
->len
) + NLA_HDRLEN
+ nla_len(nla
))
121 /* a nested attributes is allowed to be empty; if its not,
122 * it must have a size of at least NLA_HDRLEN.
129 else if (pt
->type
!= NLA_UNSPEC
)
130 minlen
= nla_attr_minlen
[pt
->type
];
132 if (attrlen
< minlen
)
140 * nla_validate - Validate a stream of attributes
141 * @head: head of attribute stream
142 * @len: length of attribute stream
143 * @maxtype: maximum attribute type to be expected
144 * @policy: validation policy
145 * @extack: extended ACK report struct
147 * Validates all attributes in the specified attribute stream against the
148 * specified policy. Attributes with a type exceeding maxtype will be
149 * ignored. See documenation of struct nla_policy for more details.
151 * Returns 0 on success or a negative error code.
153 int nla_validate(const struct nlattr
*head
, int len
, int maxtype
,
154 const struct nla_policy
*policy
,
155 struct netlink_ext_ack
*extack
)
157 const struct nlattr
*nla
;
160 nla_for_each_attr(nla
, head
, len
, rem
) {
161 int err
= validate_nla(nla
, maxtype
, policy
);
165 extack
->bad_attr
= nla
;
172 EXPORT_SYMBOL(nla_validate
);
175 * nla_policy_len - Determin the max. length of a policy
176 * @policy: policy to use
177 * @n: number of policies
179 * Determines the max. length of the policy. It is currently used
180 * to allocated Netlink buffers roughly the size of the actual
183 * Returns 0 on success or a negative error code.
186 nla_policy_len(const struct nla_policy
*p
, int n
)
190 for (i
= 0; i
< n
; i
++, p
++) {
192 len
+= nla_total_size(p
->len
);
193 else if (nla_attr_minlen
[p
->type
])
194 len
+= nla_total_size(nla_attr_minlen
[p
->type
]);
199 EXPORT_SYMBOL(nla_policy_len
);
202 * nla_parse - Parse a stream of attributes into a tb buffer
203 * @tb: destination array with maxtype+1 elements
204 * @maxtype: maximum attribute type to be expected
205 * @head: head of attribute stream
206 * @len: length of attribute stream
207 * @policy: validation policy
209 * Parses a stream of attributes and stores a pointer to each attribute in
210 * the tb array accessible via the attribute type. Attributes with a type
211 * exceeding maxtype will be silently ignored for backwards compatibility
212 * reasons. policy may be set to NULL if no validation is required.
214 * Returns 0 on success or a negative error code.
216 int nla_parse(struct nlattr
**tb
, int maxtype
, const struct nlattr
*head
,
217 int len
, const struct nla_policy
*policy
,
218 struct netlink_ext_ack
*extack
)
220 const struct nlattr
*nla
;
223 memset(tb
, 0, sizeof(struct nlattr
*) * (maxtype
+ 1));
225 nla_for_each_attr(nla
, head
, len
, rem
) {
226 u16 type
= nla_type(nla
);
228 if (type
> 0 && type
<= maxtype
) {
230 err
= validate_nla(nla
, maxtype
, policy
);
233 extack
->bad_attr
= nla
;
238 tb
[type
] = (struct nlattr
*)nla
;
242 if (unlikely(rem
> 0))
243 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
250 EXPORT_SYMBOL(nla_parse
);
253 * nla_find - Find a specific attribute in a stream of attributes
254 * @head: head of attribute stream
255 * @len: length of attribute stream
256 * @attrtype: type of attribute to look for
258 * Returns the first attribute in the stream matching the specified type.
260 struct nlattr
*nla_find(const struct nlattr
*head
, int len
, int attrtype
)
262 const struct nlattr
*nla
;
265 nla_for_each_attr(nla
, head
, len
, rem
)
266 if (nla_type(nla
) == attrtype
)
267 return (struct nlattr
*)nla
;
271 EXPORT_SYMBOL(nla_find
);
274 * nla_strlcpy - Copy string attribute payload into a sized buffer
275 * @dst: where to copy the string to
276 * @nla: attribute to copy the string from
277 * @dstsize: size of destination buffer
279 * Copies at most dstsize - 1 bytes into the destination buffer.
280 * The result is always a valid NUL-terminated string. Unlike
281 * strlcpy the destination buffer is always padded out.
283 * Returns the length of the source buffer.
285 size_t nla_strlcpy(char *dst
, const struct nlattr
*nla
, size_t dstsize
)
287 size_t srclen
= nla_len(nla
);
288 char *src
= nla_data(nla
);
290 if (srclen
> 0 && src
[srclen
- 1] == '\0')
294 size_t len
= (srclen
>= dstsize
) ? dstsize
- 1 : srclen
;
296 memset(dst
, 0, dstsize
);
297 memcpy(dst
, src
, len
);
302 EXPORT_SYMBOL(nla_strlcpy
);
305 * nla_strdup - Copy string attribute payload into a newly allocated buffer
306 * @nla: attribute to copy the string from
307 * @flags: the type of memory to allocate (see kmalloc).
309 * Returns a pointer to the allocated buffer or NULL on error.
311 char *nla_strdup(const struct nlattr
*nla
, gfp_t flags
)
313 size_t srclen
= nla_len(nla
);
314 char *src
= nla_data(nla
), *dst
;
316 if (srclen
> 0 && src
[srclen
- 1] == '\0')
319 dst
= kmalloc(srclen
+ 1, flags
);
321 memcpy(dst
, src
, srclen
);
326 EXPORT_SYMBOL(nla_strdup
);
329 * nla_memcpy - Copy a netlink attribute into another memory area
330 * @dest: where to copy to memcpy
331 * @src: netlink attribute to copy from
332 * @count: size of the destination area
334 * Note: The number of bytes copied is limited by the length of
335 * attribute's payload. memcpy
337 * Returns the number of bytes copied.
339 int nla_memcpy(void *dest
, const struct nlattr
*src
, int count
)
341 int minlen
= min_t(int, count
, nla_len(src
));
343 memcpy(dest
, nla_data(src
), minlen
);
345 memset(dest
+ minlen
, 0, count
- minlen
);
349 EXPORT_SYMBOL(nla_memcpy
);
352 * nla_memcmp - Compare an attribute with sized memory area
353 * @nla: netlink attribute
355 * @size: size of memory area
357 int nla_memcmp(const struct nlattr
*nla
, const void *data
,
360 int d
= nla_len(nla
) - size
;
363 d
= memcmp(nla_data(nla
), data
, size
);
367 EXPORT_SYMBOL(nla_memcmp
);
370 * nla_strcmp - Compare a string attribute against a string
371 * @nla: netlink string attribute
372 * @str: another string
374 int nla_strcmp(const struct nlattr
*nla
, const char *str
)
376 int len
= strlen(str
);
377 char *buf
= nla_data(nla
);
378 int attrlen
= nla_len(nla
);
381 if (attrlen
> 0 && buf
[attrlen
- 1] == '\0')
386 d
= memcmp(nla_data(nla
), str
, len
);
390 EXPORT_SYMBOL(nla_strcmp
);
394 * __nla_reserve - reserve room for attribute on the skb
395 * @skb: socket buffer to reserve room on
396 * @attrtype: attribute type
397 * @attrlen: length of attribute payload
399 * Adds a netlink attribute header to a socket buffer and reserves
400 * room for the payload but does not copy it.
402 * The caller is responsible to ensure that the skb provides enough
403 * tailroom for the attribute header and payload.
405 struct nlattr
*__nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
409 nla
= skb_put(skb
, nla_total_size(attrlen
));
410 nla
->nla_type
= attrtype
;
411 nla
->nla_len
= nla_attr_size(attrlen
);
413 memset((unsigned char *) nla
+ nla
->nla_len
, 0, nla_padlen(attrlen
));
417 EXPORT_SYMBOL(__nla_reserve
);
420 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
421 * @skb: socket buffer to reserve room on
422 * @attrtype: attribute type
423 * @attrlen: length of attribute payload
424 * @padattr: attribute type for the padding
426 * Adds a netlink attribute header to a socket buffer and reserves
427 * room for the payload but does not copy it. It also ensure that this
428 * attribute will have a 64-bit aligned nla_data() area.
430 * The caller is responsible to ensure that the skb provides enough
431 * tailroom for the attribute header and payload.
433 struct nlattr
*__nla_reserve_64bit(struct sk_buff
*skb
, int attrtype
,
434 int attrlen
, int padattr
)
436 if (nla_need_padding_for_64bit(skb
))
437 nla_align_64bit(skb
, padattr
);
439 return __nla_reserve(skb
, attrtype
, attrlen
);
441 EXPORT_SYMBOL(__nla_reserve_64bit
);
444 * __nla_reserve_nohdr - reserve room for attribute without header
445 * @skb: socket buffer to reserve room on
446 * @attrlen: length of attribute payload
448 * Reserves room for attribute payload without a header.
450 * The caller is responsible to ensure that the skb provides enough
451 * tailroom for the payload.
453 void *__nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
455 return skb_put_zero(skb
, NLA_ALIGN(attrlen
));
457 EXPORT_SYMBOL(__nla_reserve_nohdr
);
460 * nla_reserve - reserve room for attribute on the skb
461 * @skb: socket buffer to reserve room on
462 * @attrtype: attribute type
463 * @attrlen: length of attribute payload
465 * Adds a netlink attribute header to a socket buffer and reserves
466 * room for the payload but does not copy it.
468 * Returns NULL if the tailroom of the skb is insufficient to store
469 * the attribute header and payload.
471 struct nlattr
*nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
473 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
476 return __nla_reserve(skb
, attrtype
, attrlen
);
478 EXPORT_SYMBOL(nla_reserve
);
481 * nla_reserve_64bit - reserve room for attribute on the skb and align it
482 * @skb: socket buffer to reserve room on
483 * @attrtype: attribute type
484 * @attrlen: length of attribute payload
485 * @padattr: attribute type for the padding
487 * Adds a netlink attribute header to a socket buffer and reserves
488 * room for the payload but does not copy it. It also ensure that this
489 * attribute will have a 64-bit aligned nla_data() area.
491 * Returns NULL if the tailroom of the skb is insufficient to store
492 * the attribute header and payload.
494 struct nlattr
*nla_reserve_64bit(struct sk_buff
*skb
, int attrtype
, int attrlen
,
499 if (nla_need_padding_for_64bit(skb
))
500 len
= nla_total_size_64bit(attrlen
);
502 len
= nla_total_size(attrlen
);
503 if (unlikely(skb_tailroom(skb
) < len
))
506 return __nla_reserve_64bit(skb
, attrtype
, attrlen
, padattr
);
508 EXPORT_SYMBOL(nla_reserve_64bit
);
511 * nla_reserve_nohdr - reserve room for attribute without header
512 * @skb: socket buffer to reserve room on
513 * @attrlen: length of attribute payload
515 * Reserves room for attribute payload without a header.
517 * Returns NULL if the tailroom of the skb is insufficient to store
518 * the attribute payload.
520 void *nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
522 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
525 return __nla_reserve_nohdr(skb
, attrlen
);
527 EXPORT_SYMBOL(nla_reserve_nohdr
);
530 * __nla_put - Add a netlink attribute to a socket buffer
531 * @skb: socket buffer to add attribute to
532 * @attrtype: attribute type
533 * @attrlen: length of attribute payload
534 * @data: head of attribute payload
536 * The caller is responsible to ensure that the skb provides enough
537 * tailroom for the attribute header and payload.
539 void __nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
,
544 nla
= __nla_reserve(skb
, attrtype
, attrlen
);
545 memcpy(nla_data(nla
), data
, attrlen
);
547 EXPORT_SYMBOL(__nla_put
);
550 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
551 * @skb: socket buffer to add attribute to
552 * @attrtype: attribute type
553 * @attrlen: length of attribute payload
554 * @data: head of attribute payload
555 * @padattr: attribute type for the padding
557 * The caller is responsible to ensure that the skb provides enough
558 * tailroom for the attribute header and payload.
560 void __nla_put_64bit(struct sk_buff
*skb
, int attrtype
, int attrlen
,
561 const void *data
, int padattr
)
565 nla
= __nla_reserve_64bit(skb
, attrtype
, attrlen
, padattr
);
566 memcpy(nla_data(nla
), data
, attrlen
);
568 EXPORT_SYMBOL(__nla_put_64bit
);
571 * __nla_put_nohdr - Add a netlink attribute without header
572 * @skb: socket buffer to add attribute to
573 * @attrlen: length of attribute payload
574 * @data: head of attribute payload
576 * The caller is responsible to ensure that the skb provides enough
577 * tailroom for the attribute payload.
579 void __nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
583 start
= __nla_reserve_nohdr(skb
, attrlen
);
584 memcpy(start
, data
, attrlen
);
586 EXPORT_SYMBOL(__nla_put_nohdr
);
589 * nla_put - Add a netlink attribute to a socket buffer
590 * @skb: socket buffer to add attribute to
591 * @attrtype: attribute type
592 * @attrlen: length of attribute payload
593 * @data: head of attribute payload
595 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
596 * the attribute header and payload.
598 int nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
600 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
603 __nla_put(skb
, attrtype
, attrlen
, data
);
606 EXPORT_SYMBOL(nla_put
);
609 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
610 * @skb: socket buffer to add attribute to
611 * @attrtype: attribute type
612 * @attrlen: length of attribute payload
613 * @data: head of attribute payload
614 * @padattr: attribute type for the padding
616 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
617 * the attribute header and payload.
619 int nla_put_64bit(struct sk_buff
*skb
, int attrtype
, int attrlen
,
620 const void *data
, int padattr
)
624 if (nla_need_padding_for_64bit(skb
))
625 len
= nla_total_size_64bit(attrlen
);
627 len
= nla_total_size(attrlen
);
628 if (unlikely(skb_tailroom(skb
) < len
))
631 __nla_put_64bit(skb
, attrtype
, attrlen
, data
, padattr
);
634 EXPORT_SYMBOL(nla_put_64bit
);
637 * nla_put_nohdr - Add a netlink attribute without header
638 * @skb: socket buffer to add attribute to
639 * @attrlen: length of attribute payload
640 * @data: head of attribute payload
642 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
643 * the attribute payload.
645 int nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
647 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
650 __nla_put_nohdr(skb
, attrlen
, data
);
653 EXPORT_SYMBOL(nla_put_nohdr
);
656 * nla_append - Add a netlink attribute without header or padding
657 * @skb: socket buffer to add attribute to
658 * @attrlen: length of attribute payload
659 * @data: head of attribute payload
661 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
662 * the attribute payload.
664 int nla_append(struct sk_buff
*skb
, int attrlen
, const void *data
)
666 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
669 skb_put_data(skb
, data
, attrlen
);
672 EXPORT_SYMBOL(nla_append
);