1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef GENL_MAGIC_STRUCT_H
3 #define GENL_MAGIC_STRUCT_H
4
5 #ifndef GENL_MAGIC_FAMILY
6 # error "you need to define GENL_MAGIC_FAMILY before inclusion"
7 #endif
8
9 #ifndef GENL_MAGIC_VERSION
10 # error "you need to define GENL_MAGIC_VERSION before inclusion"
11 #endif
12
13 #ifndef GENL_MAGIC_INCLUDE_FILE
14 # error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion"
15 #endif
16
17 #include <linux/args.h>
18 #include <linux/types.h>
19 #include <net/genetlink.h>
20
21 extern int CONCATENATE(GENL_MAGIC_FAMILY, _genl_register)(void);
22 extern void CONCATENATE(GENL_MAGIC_FAMILY, _genl_unregister)(void);
23
24 /*
25 * Extension of genl attribute validation policies {{{2
26 */
27
28 /*
29 * Flags specific to drbd and not visible at the netlink layer, used in
30 * <struct>_from_attrs and <struct>_to_skb:
31 *
32 * @DRBD_F_REQUIRED: Attribute is required; a request without this attribute is
33 * invalid.
34 *
35 * @DRBD_F_SENSITIVE: Attribute includes sensitive information and must not be
36 * included in unpriviledged get requests or broadcasts.
37 *
38 * @DRBD_F_INVARIANT: Attribute is set when an object is initially created, but
39 * cannot subsequently be changed.
40 */
41 #define DRBD_F_REQUIRED (1 << 0)
42 #define DRBD_F_SENSITIVE (1 << 1)
43 #define DRBD_F_INVARIANT (1 << 2)
44
45
46 /* }}}1
47 * MAGIC
48 * multi-include macro expansion magic starts here
49 */
50
51 /* MAGIC helpers {{{2 */
52
nla_put_u64_0pad(struct sk_buff * skb,int attrtype,u64 value)53 static inline int nla_put_u64_0pad(struct sk_buff *skb, int attrtype, u64 value)
54 {
55 return nla_put_64bit(skb, attrtype, sizeof(u64), &value, 0);
56 }
57
58 /* possible field types */
59 #define __flg_field(attr_nr, attr_flag, name) \
60 __field(attr_nr, attr_flag, name, NLA_U8, char, \
61 nla_get_u8, nla_put_u8, false)
62 #define __u8_field(attr_nr, attr_flag, name) \
63 __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
64 nla_get_u8, nla_put_u8, false)
65 #define __u16_field(attr_nr, attr_flag, name) \
66 __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
67 nla_get_u16, nla_put_u16, false)
68 #define __u32_field(attr_nr, attr_flag, name) \
69 __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
70 nla_get_u32, nla_put_u32, false)
71 #define __s32_field(attr_nr, attr_flag, name) \
72 __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
73 nla_get_u32, nla_put_u32, true)
74 #define __u64_field(attr_nr, attr_flag, name) \
75 __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
76 nla_get_u64, nla_put_u64_0pad, false)
77 #define __str_field(attr_nr, attr_flag, name, maxlen) \
78 __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
79 nla_strscpy, nla_put, false)
80 #define __bin_field(attr_nr, attr_flag, name, maxlen) \
81 __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
82 nla_memcpy, nla_put, false)
83
84 /* fields with default values */
85 #define __flg_field_def(attr_nr, attr_flag, name, default) \
86 __flg_field(attr_nr, attr_flag, name)
87 #define __u32_field_def(attr_nr, attr_flag, name, default) \
88 __u32_field(attr_nr, attr_flag, name)
89 #define __s32_field_def(attr_nr, attr_flag, name, default) \
90 __s32_field(attr_nr, attr_flag, name)
91 #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
92 __str_field(attr_nr, attr_flag, name, maxlen)
93
94 #define GENL_op_init(args...) args
95 #define GENL_doit(handler) \
96 .doit = handler, \
97 .flags = GENL_ADMIN_PERM,
98 #define GENL_dumpit(handler) \
99 .dumpit = handler, \
100 .flags = GENL_ADMIN_PERM,
101
102 /* }}}1
103 * Magic: define the enum symbols for genl_ops
104 * Magic: define the enum symbols for top level attributes
105 * Magic: define the enum symbols for nested attributes
106 * {{{2
107 */
108
109 #undef GENL_struct
110 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
111
112 #undef GENL_mc_group
113 #define GENL_mc_group(group)
114
115 #undef GENL_notification
116 #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
117 op_name = op_num,
118
119 #undef GENL_op
120 #define GENL_op(op_name, op_num, handler, tla_list) \
121 op_name = op_num,
122
123 enum {
124 #include GENL_MAGIC_INCLUDE_FILE
125 };
126
127 #undef GENL_notification
128 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
129
130 #undef GENL_op
131 #define GENL_op(op_name, op_num, handler, attr_list)
132
133 #undef GENL_struct
134 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
135 tag_name = tag_number,
136
137 enum {
138 #include GENL_MAGIC_INCLUDE_FILE
139 };
140
141 #undef GENL_struct
142 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
143 enum { \
144 s_fields \
145 };
146
147 #undef __field
148 #define __field(attr_nr, attr_flag, name, nla_type, type, \
149 __get, __put, __is_signed) \
150 T_ ## name = (__u16)(attr_nr),
151
152 #undef __array
153 #define __array(attr_nr, attr_flag, name, nla_type, type, \
154 maxlen, __get, __put, __is_signed) \
155 T_ ## name = (__u16)(attr_nr),
156
157 #include GENL_MAGIC_INCLUDE_FILE
158
159 /* }}}1
160 * Magic: compile time assert unique numbers for operations
161 * Magic: -"- unique numbers for top level attributes
162 * Magic: -"- unique numbers for nested attributes
163 * {{{2
164 */
165
166 #undef GENL_struct
167 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
168
169 #undef GENL_op
170 #define GENL_op(op_name, op_num, handler, attr_list) \
171 case op_name:
172
173 #undef GENL_notification
174 #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
175 case op_name:
176
ct_assert_unique_operations(void)177 static inline void ct_assert_unique_operations(void)
178 {
179 switch (0) {
180 #include GENL_MAGIC_INCLUDE_FILE
181 case 0:
182 ;
183 }
184 }
185
186 #undef GENL_op
187 #define GENL_op(op_name, op_num, handler, attr_list)
188
189 #undef GENL_notification
190 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
191
192 #undef GENL_struct
193 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
194 case tag_number:
195
ct_assert_unique_top_level_attributes(void)196 static inline void ct_assert_unique_top_level_attributes(void)
197 {
198 switch (0) {
199 #include GENL_MAGIC_INCLUDE_FILE
200 case 0:
201 ;
202 }
203 }
204
205 #undef GENL_struct
206 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
207 static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
208 { \
209 switch (0) { \
210 s_fields \
211 case 0: \
212 ; \
213 } \
214 }
215
216 #undef __field
217 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
218 __is_signed) \
219 case attr_nr:
220
221 #undef __array
222 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
223 __get, __put, __is_signed) \
224 case attr_nr:
225
226 #include GENL_MAGIC_INCLUDE_FILE
227
228 /* }}}1
229 * Magic: declare structs
230 * struct <name> {
231 * fields
232 * };
233 * {{{2
234 */
235
236 #undef GENL_struct
237 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
238 struct s_name { s_fields };
239
240 #undef __field
241 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
242 __is_signed) \
243 type name;
244
245 #undef __array
246 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
247 __get, __put, __is_signed) \
248 type name[maxlen]; \
249 __u32 name ## _len;
250
251 #include GENL_MAGIC_INCLUDE_FILE
252
253 #undef GENL_struct
254 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
255 enum { \
256 s_fields \
257 };
258
259 #undef __field
260 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
261 is_signed) \
262 F_ ## name ## _IS_SIGNED = is_signed,
263
264 #undef __array
265 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
266 __get, __put, is_signed) \
267 F_ ## name ## _IS_SIGNED = is_signed,
268
269 #include GENL_MAGIC_INCLUDE_FILE
270
271 /* }}}1 */
272 #endif /* GENL_MAGIC_STRUCT_H */
273