netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH libmnl,v2] attr: expand mnl_attr_get_uint() documentation
@ 2024-10-13 19:43 Pablo Neira Ayuso
  2024-10-14  9:07 ` Phil Sutter
  0 siblings, 1 reply; 2+ messages in thread
From: Pablo Neira Ayuso @ 2024-10-13 19:43 UTC (permalink / raw)
  To: netfilter-devel; +Cc: petrm, danieller, phil, mlxsw, kuba

This function is modelled after rta_getattr_uint() in libnetlink to fetch the
netlink attribute payload of NLA_UINT, although it was extended to make it
universal for 8-bit, 16-bit, 32-bit and 64-bit integers.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
v2: fix reference to bytes instead of bits per Phil Sutter.

 src/attr.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/attr.c b/src/attr.c
index 399318eebaa8..20e99b195ab7 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -393,7 +393,24 @@ EXPORT_SYMBOL uint64_t mnl_attr_get_u64(const struct nlattr *attr)
  * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
  * \param attr pointer to netlink attribute
  *
- * This function returns the 64-bit value of the attribute payload.
+ * This helper function reads the variable-length netlink attribute NLA_UINT
+ * that provides a 32-bit or 64-bit integer payload. Its use is recommended only
+ * in these cases.
+ *
+ * Recommended validation for NLA_UINT is:
+ *
+ * \verbatim
+	if (!mnl_attr_validate(attr, NLA_U32) &&
+	    !mnl_attr_validate(attr, NLA_U64)) {
+		perror("mnl_attr_validate");
+		return MNL_CB_ERROR;
+	}
+\endverbatim
+ *
+ * \returns the 64-bit value of the attribute payload. On error, it returns
+ * UINT64_MAX if the length of the netlink attribute is not an 8-bit, 16-bit,
+ * 32-bit and 64-bit integer. Therefore, there is no way to distinguish between
+ * UINT64_MAX and an error. Also, errno is never set.
  */
 EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
 {
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH libmnl,v2] attr: expand mnl_attr_get_uint() documentation
  2024-10-13 19:43 [PATCH libmnl,v2] attr: expand mnl_attr_get_uint() documentation Pablo Neira Ayuso
@ 2024-10-14  9:07 ` Phil Sutter
  0 siblings, 0 replies; 2+ messages in thread
From: Phil Sutter @ 2024-10-14  9:07 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel, petrm, danieller, mlxsw, kuba

On Sun, Oct 13, 2024 at 09:43:19PM +0200, Pablo Neira Ayuso wrote:
> This function is modelled after rta_getattr_uint() in libnetlink to fetch the
> netlink attribute payload of NLA_UINT, although it was extended to make it
> universal for 8-bit, 16-bit, 32-bit and 64-bit integers.
> 
> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
> ---
> v2: fix reference to bytes instead of bits per Phil Sutter.
> 
>  src/attr.c | 19 ++++++++++++++++++-
>  1 file changed, 18 insertions(+), 1 deletion(-)
> 
> diff --git a/src/attr.c b/src/attr.c
> index 399318eebaa8..20e99b195ab7 100644
> --- a/src/attr.c
> +++ b/src/attr.c
> @@ -393,7 +393,24 @@ EXPORT_SYMBOL uint64_t mnl_attr_get_u64(const struct nlattr *attr)
>   * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
>   * \param attr pointer to netlink attribute
>   *
> - * This function returns the 64-bit value of the attribute payload.
> + * This helper function reads the variable-length netlink attribute NLA_UINT
> + * that provides a 32-bit or 64-bit integer payload. Its use is recommended only
> + * in these cases.
> + *
> + * Recommended validation for NLA_UINT is:
> + *
> + * \verbatim
> +	if (!mnl_attr_validate(attr, NLA_U32) &&
> +	    !mnl_attr_validate(attr, NLA_U64)) {
> +		perror("mnl_attr_validate");
> +		return MNL_CB_ERROR;
> +	}
> +\endverbatim
> + *
> + * \returns the 64-bit value of the attribute payload. On error, it returns
> + * UINT64_MAX if the length of the netlink attribute is not an 8-bit, 16-bit,
> + * 32-bit and 64-bit integer. Therefore, there is no way to distinguish between
             ~~~

Nit: This should be "or", since the length is expected to be either one
of the listed values.

That aside:

Acked-by: Phil Sutter <phil@nwl.cc>

Thanks!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-10-14  9:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-13 19:43 [PATCH libmnl,v2] attr: expand mnl_attr_get_uint() documentation Pablo Neira Ayuso
2024-10-14  9:07 ` Phil Sutter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).