netlink: Add nla_put_empty_nest helper
authorJoe Damato <jdamato@fastly.com>
Fri, 14 Feb 2025 21:12:29 +0000 (21:12 +0000)
committerJakub Kicinski <kuba@kernel.org>
Tue, 18 Feb 2025 00:46:03 +0000 (16:46 -0800)
Creating empty nests is helpful when the exact attributes to be exposed
in the future are not known. Encapsulate the logic in a helper.

Signed-off-by: Joe Damato <jdamato@fastly.com>
Suggested-by: Jakub Kicinski <kuba@kernel.org>
Link: https://patch.msgid.link/20250214211255.14194-2-jdamato@fastly.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/netlink.h

index e015ffb..29e0db9 100644 (file)
  *   nla_nest_start(skb, type)         start a nested attribute
  *   nla_nest_end(skb, nla)            finalize a nested attribute
  *   nla_nest_cancel(skb, nla)         cancel nested attribute construction
+ *   nla_put_empty_nest(skb, type)     create an empty nest
  *
  * Attribute Length Calculations:
  *   nla_attr_size(payload)            length of attribute w/o padding
@@ -2240,6 +2241,20 @@ static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
        nlmsg_trim(skb, start);
 }
 
+/**
+ * nla_put_empty_nest - Create an empty nest
+ * @skb: socket buffer the message is stored in
+ * @attrtype: attribute type of the container
+ *
+ * This function is a helper for creating empty nests.
+ *
+ * Returns: 0 when successful or -EMSGSIZE on failure.
+ */
+static inline int nla_put_empty_nest(struct sk_buff *skb, int attrtype)
+{
+       return nla_nest_start(skb, attrtype) ? 0 : -EMSGSIZE;
+}
+
 /**
  * __nla_validate_nested - Validate a stream of nested attributes
  * @start: container attribute