netlink: add helpers for extack attr presence checking
authorJakub Kicinski <kuba@kernel.org>
Fri, 26 Aug 2022 03:09:32 +0000 (20:09 -0700)
committerPaolo Abeni <pabeni@redhat.com>
Tue, 30 Aug 2022 10:20:43 +0000 (12:20 +0200)
Being able to check attribute presence and set extack
if not on one line is handy, add helpers.

Reviewed-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
include/linux/netlink.h
include/net/genetlink.h

index 1619221..d51e041 100644 (file)
@@ -139,6 +139,17 @@ struct netlink_ext_ack {
        }                                               \
 } while (0)
 
+#define NL_REQ_ATTR_CHECK(extack, nest, tb, type) ({           \
+       struct nlattr **__tb = (tb);                            \
+       u32 __attr = (type);                                    \
+       int __retval;                                           \
+                                                               \
+       __retval = !__tb[__attr];                               \
+       if (__retval)                                           \
+               NL_SET_ERR_ATTR_MISS((extack), (nest), __attr); \
+       __retval;                                               \
+})
+
 static inline void nl_set_extack_cookie_u64(struct netlink_ext_ack *extack,
                                            u64 cookie)
 {
index a4827b5..8f78017 100644 (file)
@@ -110,6 +110,13 @@ static inline void genl_info_net_set(struct genl_info *info, struct net *net)
 
 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
 
+/* Report that a root attribute is missing */
+#define GENL_REQ_ATTR_CHECK(info, attr) ({                             \
+       struct genl_info *__info = (info);                              \
+                                                                       \
+       NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
+})
+
 enum genl_validate_flags {
        GENL_DONT_VALIDATE_STRICT               = BIT(0),
        GENL_DONT_VALIDATE_DUMP                 = BIT(1),