1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef GENL_MAGIC_STRUCT_H
3 #define GENL_MAGIC_STRUCT_H
5 #ifndef GENL_MAGIC_FAMILY
6 # error "you need to define GENL_MAGIC_FAMILY before inclusion"
9 #ifndef GENL_MAGIC_VERSION
10 # error "you need to define GENL_MAGIC_VERSION before inclusion"
13 #ifndef GENL_MAGIC_INCLUDE_FILE
14 # error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion"
17 #include <linux/args.h>
18 #include <linux/genetlink.h>
19 #include <linux/types.h>
21 extern int CONCATENATE(GENL_MAGIC_FAMILY, _genl_register)(void);
22 extern void CONCATENATE(GENL_MAGIC_FAMILY, _genl_unregister)(void);
25 * Extension of genl attribute validation policies {{{2
29 * @DRBD_GENLA_F_MANDATORY: By default, netlink ignores attributes it does not
30 * know about. This flag can be set in nlattr->nla_type to indicate that this
31 * attribute must not be ignored.
33 * We check and remove this flag in drbd_nla_check_mandatory() before
34 * validating the attribute types and lengths via nla_parse_nested().
36 #define DRBD_GENLA_F_MANDATORY (1 << 14)
39 * Flags specific to drbd and not visible at the netlink layer, used in
40 * <struct>_from_attrs and <struct>_to_skb:
42 * @DRBD_F_REQUIRED: Attribute is required; a request without this attribute is
45 * @DRBD_F_SENSITIVE: Attribute includes sensitive information and must not be
46 * included in unpriviledged get requests or broadcasts.
48 * @DRBD_F_INVARIANT: Attribute is set when an object is initially created, but
49 * cannot subsequently be changed.
51 #define DRBD_F_REQUIRED (1 << 0)
52 #define DRBD_F_SENSITIVE (1 << 1)
53 #define DRBD_F_INVARIANT (1 << 2)
55 #define __nla_type(x) ((__u16)((x) & NLA_TYPE_MASK & ~DRBD_GENLA_F_MANDATORY))
59 * multi-include macro expansion magic starts here
62 /* MAGIC helpers {{{2 */
64 static inline int nla_put_u64_0pad(struct sk_buff *skb, int attrtype, u64 value)
66 return nla_put_64bit(skb, attrtype, sizeof(u64), &value, 0);
69 /* possible field types */
70 #define __flg_field(attr_nr, attr_flag, name) \
71 __field(attr_nr, attr_flag, name, NLA_U8, char, \
72 nla_get_u8, nla_put_u8, false)
73 #define __u8_field(attr_nr, attr_flag, name) \
74 __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
75 nla_get_u8, nla_put_u8, false)
76 #define __u16_field(attr_nr, attr_flag, name) \
77 __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
78 nla_get_u16, nla_put_u16, false)
79 #define __u32_field(attr_nr, attr_flag, name) \
80 __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
81 nla_get_u32, nla_put_u32, false)
82 #define __s32_field(attr_nr, attr_flag, name) \
83 __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
84 nla_get_u32, nla_put_u32, true)
85 #define __u64_field(attr_nr, attr_flag, name) \
86 __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
87 nla_get_u64, nla_put_u64_0pad, false)
88 #define __str_field(attr_nr, attr_flag, name, maxlen) \
89 __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
90 nla_strscpy, nla_put, false)
91 #define __bin_field(attr_nr, attr_flag, name, maxlen) \
92 __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
93 nla_memcpy, nla_put, false)
95 /* fields with default values */
96 #define __flg_field_def(attr_nr, attr_flag, name, default) \
97 __flg_field(attr_nr, attr_flag, name)
98 #define __u32_field_def(attr_nr, attr_flag, name, default) \
99 __u32_field(attr_nr, attr_flag, name)
100 #define __s32_field_def(attr_nr, attr_flag, name, default) \
101 __s32_field(attr_nr, attr_flag, name)
102 #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
103 __str_field(attr_nr, attr_flag, name, maxlen)
105 #define GENL_op_init(args...) args
106 #define GENL_doit(handler) \
108 .flags = GENL_ADMIN_PERM,
109 #define GENL_dumpit(handler) \
111 .flags = GENL_ADMIN_PERM,
114 * Magic: define the enum symbols for genl_ops
115 * Magic: define the enum symbols for top level attributes
116 * Magic: define the enum symbols for nested attributes
121 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
124 #define GENL_mc_group(group)
126 #undef GENL_notification
127 #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
131 #define GENL_op(op_name, op_num, handler, tla_list) \
135 #include GENL_MAGIC_INCLUDE_FILE
138 #undef GENL_notification
139 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
142 #define GENL_op(op_name, op_num, handler, attr_list)
145 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
146 tag_name = tag_number,
149 #include GENL_MAGIC_INCLUDE_FILE
153 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
159 #define __field(attr_nr, attr_flag, name, nla_type, type, \
160 __get, __put, __is_signed) \
161 T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
164 #define __array(attr_nr, attr_flag, name, nla_type, type, \
165 maxlen, __get, __put, __is_signed) \
166 T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
168 #include GENL_MAGIC_INCLUDE_FILE
171 * Magic: compile time assert unique numbers for operations
172 * Magic: -"- unique numbers for top level attributes
173 * Magic: -"- unique numbers for nested attributes
178 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
181 #define GENL_op(op_name, op_num, handler, attr_list) \
184 #undef GENL_notification
185 #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
188 static inline void ct_assert_unique_operations(void)
191 #include GENL_MAGIC_INCLUDE_FILE
198 #define GENL_op(op_name, op_num, handler, attr_list)
200 #undef GENL_notification
201 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
204 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
207 static inline void ct_assert_unique_top_level_attributes(void)
210 #include GENL_MAGIC_INCLUDE_FILE
217 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
218 static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
228 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
233 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
234 __get, __put, __is_signed) \
237 #include GENL_MAGIC_INCLUDE_FILE
240 * Magic: declare structs
248 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
249 struct s_name { s_fields };
252 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
257 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
258 __get, __put, __is_signed) \
262 #include GENL_MAGIC_INCLUDE_FILE
265 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
271 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
273 F_ ## name ## _IS_SIGNED = is_signed,
276 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
277 __get, __put, is_signed) \
278 F_ ## name ## _IS_SIGNED = is_signed,
280 #include GENL_MAGIC_INCLUDE_FILE
283 #endif /* GENL_MAGIC_STRUCT_H */