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/genetlink.h>
18 #include <linux/types.h>
20 #define CONCAT__(a,b) a ## b
21 #define CONCAT_(a,b) CONCAT__(a,b)
23 extern int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void);
24 extern void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void);
27 * Extension of genl attribute validation policies {{{2
31 * @DRBD_GENLA_F_MANDATORY: By default, netlink ignores attributes it does not
32 * know about. This flag can be set in nlattr->nla_type to indicate that this
33 * attribute must not be ignored.
35 * We check and remove this flag in drbd_nla_check_mandatory() before
36 * validating the attribute types and lengths via nla_parse_nested().
38 #define DRBD_GENLA_F_MANDATORY (1 << 14)
41 * Flags specific to drbd and not visible at the netlink layer, used in
42 * <struct>_from_attrs and <struct>_to_skb:
44 * @DRBD_F_REQUIRED: Attribute is required; a request without this attribute is
47 * @DRBD_F_SENSITIVE: Attribute includes sensitive information and must not be
48 * included in unpriviledged get requests or broadcasts.
50 * @DRBD_F_INVARIANT: Attribute is set when an object is initially created, but
51 * cannot subsequently be changed.
53 #define DRBD_F_REQUIRED (1 << 0)
54 #define DRBD_F_SENSITIVE (1 << 1)
55 #define DRBD_F_INVARIANT (1 << 2)
57 #define __nla_type(x) ((__u16)((x) & NLA_TYPE_MASK & ~DRBD_GENLA_F_MANDATORY))
61 * multi-include macro expansion magic starts here
64 /* MAGIC helpers {{{2 */
66 static inline int nla_put_u64_0pad(struct sk_buff *skb, int attrtype, u64 value)
68 return nla_put_64bit(skb, attrtype, sizeof(u64), &value, 0);
71 /* possible field types */
72 #define __flg_field(attr_nr, attr_flag, name) \
73 __field(attr_nr, attr_flag, name, NLA_U8, char, \
74 nla_get_u8, nla_put_u8, false)
75 #define __u8_field(attr_nr, attr_flag, name) \
76 __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
77 nla_get_u8, nla_put_u8, false)
78 #define __u16_field(attr_nr, attr_flag, name) \
79 __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
80 nla_get_u16, nla_put_u16, false)
81 #define __u32_field(attr_nr, attr_flag, name) \
82 __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
83 nla_get_u32, nla_put_u32, false)
84 #define __s32_field(attr_nr, attr_flag, name) \
85 __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
86 nla_get_u32, nla_put_u32, true)
87 #define __u64_field(attr_nr, attr_flag, name) \
88 __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
89 nla_get_u64, nla_put_u64_0pad, false)
90 #define __str_field(attr_nr, attr_flag, name, maxlen) \
91 __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
92 nla_strscpy, nla_put, false)
93 #define __bin_field(attr_nr, attr_flag, name, maxlen) \
94 __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
95 nla_memcpy, nla_put, false)
97 /* fields with default values */
98 #define __flg_field_def(attr_nr, attr_flag, name, default) \
99 __flg_field(attr_nr, attr_flag, name)
100 #define __u32_field_def(attr_nr, attr_flag, name, default) \
101 __u32_field(attr_nr, attr_flag, name)
102 #define __s32_field_def(attr_nr, attr_flag, name, default) \
103 __s32_field(attr_nr, attr_flag, name)
104 #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
105 __str_field(attr_nr, attr_flag, name, maxlen)
107 #define GENL_op_init(args...) args
108 #define GENL_doit(handler) \
110 .flags = GENL_ADMIN_PERM,
111 #define GENL_dumpit(handler) \
113 .flags = GENL_ADMIN_PERM,
116 * Magic: define the enum symbols for genl_ops
117 * Magic: define the enum symbols for top level attributes
118 * Magic: define the enum symbols for nested attributes
123 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
126 #define GENL_mc_group(group)
128 #undef GENL_notification
129 #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
133 #define GENL_op(op_name, op_num, handler, tla_list) \
137 #include GENL_MAGIC_INCLUDE_FILE
140 #undef GENL_notification
141 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
144 #define GENL_op(op_name, op_num, handler, attr_list)
147 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
148 tag_name = tag_number,
151 #include GENL_MAGIC_INCLUDE_FILE
155 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
161 #define __field(attr_nr, attr_flag, name, nla_type, type, \
162 __get, __put, __is_signed) \
163 T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
166 #define __array(attr_nr, attr_flag, name, nla_type, type, \
167 maxlen, __get, __put, __is_signed) \
168 T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
170 #include GENL_MAGIC_INCLUDE_FILE
173 * Magic: compile time assert unique numbers for operations
174 * Magic: -"- unique numbers for top level attributes
175 * Magic: -"- unique numbers for nested attributes
180 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
183 #define GENL_op(op_name, op_num, handler, attr_list) \
186 #undef GENL_notification
187 #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
190 static inline void ct_assert_unique_operations(void)
193 #include GENL_MAGIC_INCLUDE_FILE
200 #define GENL_op(op_name, op_num, handler, attr_list)
202 #undef GENL_notification
203 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
206 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
209 static inline void ct_assert_unique_top_level_attributes(void)
212 #include GENL_MAGIC_INCLUDE_FILE
219 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
220 static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
230 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
235 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
236 __get, __put, __is_signed) \
239 #include GENL_MAGIC_INCLUDE_FILE
242 * Magic: declare structs
250 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
251 struct s_name { s_fields };
254 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
259 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
260 __get, __put, __is_signed) \
264 #include GENL_MAGIC_INCLUDE_FILE
267 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
273 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
275 F_ ## name ## _IS_SIGNED = is_signed,
278 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
279 __get, __put, is_signed) \
280 F_ ## name ## _IS_SIGNED = is_signed,
282 #include GENL_MAGIC_INCLUDE_FILE
285 #endif /* GENL_MAGIC_STRUCT_H */
286 /* vim: set foldmethod=marker nofoldenable : */