1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | #ifndef GENL_MAGIC_FUNC_H |
3 | #define GENL_MAGIC_FUNC_H |
4 | |
5 | #include <linux/args.h> |
6 | #include <linux/build_bug.h> |
7 | #include <linux/genl_magic_struct.h> |
8 | |
9 | /* |
10 | * Magic: declare tla policy {{{1 |
11 | * Magic: declare nested policies |
12 | * {{{2 |
13 | */ |
14 | #undef GENL_mc_group |
15 | #define GENL_mc_group(group) |
16 | |
17 | #undef GENL_notification |
18 | #define GENL_notification(op_name, op_num, mcast_group, tla_list) |
19 | |
20 | #undef GENL_op |
21 | #define GENL_op(op_name, op_num, handler, tla_list) |
22 | |
23 | #undef GENL_struct |
24 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
25 | [tag_name] = { .type = NLA_NESTED }, |
26 | |
27 | static struct nla_policy CONCATENATE(GENL_MAGIC_FAMILY, _tla_nl_policy)[] = { |
28 | #include GENL_MAGIC_INCLUDE_FILE |
29 | }; |
30 | |
31 | #undef GENL_struct |
32 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
33 | static struct nla_policy s_name ## _nl_policy[] __read_mostly = \ |
34 | { s_fields }; |
35 | |
36 | #undef __field |
37 | #define __field(attr_nr, attr_flag, name, nla_type, _type, __get, \ |
38 | __put, __is_signed) \ |
39 | [attr_nr] = { .type = nla_type }, |
40 | |
41 | #undef __array |
42 | #define __array(attr_nr, attr_flag, name, nla_type, _type, maxlen, \ |
43 | __get, __put, __is_signed) \ |
44 | [attr_nr] = { .type = nla_type, \ |
45 | .len = maxlen - (nla_type == NLA_NUL_STRING) }, |
46 | |
47 | #include GENL_MAGIC_INCLUDE_FILE |
48 | |
49 | #ifndef __KERNEL__ |
50 | #ifndef pr_info |
51 | #define pr_info(args...) fprintf(stderr, args); |
52 | #endif |
53 | #endif |
54 | |
55 | #ifdef GENL_MAGIC_DEBUG |
56 | static void dprint_field(const char *dir, int nla_type, |
57 | const char *name, void *valp) |
58 | { |
59 | __u64 val = valp ? *(__u32 *)valp : 1; |
60 | switch (nla_type) { |
61 | case NLA_U8: val = (__u8)val; |
62 | case NLA_U16: val = (__u16)val; |
63 | case NLA_U32: val = (__u32)val; |
64 | pr_info("%s attr %s: %d 0x%08x\n" , dir, |
65 | name, (int)val, (unsigned)val); |
66 | break; |
67 | case NLA_U64: |
68 | val = *(__u64*)valp; |
69 | pr_info("%s attr %s: %lld 0x%08llx\n" , dir, |
70 | name, (long long)val, (unsigned long long)val); |
71 | break; |
72 | case NLA_FLAG: |
73 | if (val) |
74 | pr_info("%s attr %s: set\n" , dir, name); |
75 | break; |
76 | } |
77 | } |
78 | |
79 | static void dprint_array(const char *dir, int nla_type, |
80 | const char *name, const char *val, unsigned len) |
81 | { |
82 | switch (nla_type) { |
83 | case NLA_NUL_STRING: |
84 | if (len && val[len-1] == '\0') |
85 | len--; |
86 | pr_info("%s attr %s: [len:%u] '%s'\n" , dir, name, len, val); |
87 | break; |
88 | default: |
89 | /* we can always show 4 byte, |
90 | * thats what nlattr are aligned to. */ |
91 | pr_info("%s attr %s: [len:%u] %02x%02x%02x%02x ...\n" , |
92 | dir, name, len, val[0], val[1], val[2], val[3]); |
93 | } |
94 | } |
95 | |
96 | #define DPRINT_TLA(a, op, b) pr_info("%s %s %s\n", a, op, b); |
97 | |
98 | /* Name is a member field name of the struct s. |
99 | * If s is NULL (only parsing, no copy requested in *_from_attrs()), |
100 | * nla is supposed to point to the attribute containing the information |
101 | * corresponding to that struct member. */ |
102 | #define DPRINT_FIELD(dir, nla_type, name, s, nla) \ |
103 | do { \ |
104 | if (s) \ |
105 | dprint_field(dir, nla_type, #name, &s->name); \ |
106 | else if (nla) \ |
107 | dprint_field(dir, nla_type, #name, \ |
108 | (nla_type == NLA_FLAG) ? NULL \ |
109 | : nla_data(nla)); \ |
110 | } while (0) |
111 | |
112 | #define DPRINT_ARRAY(dir, nla_type, name, s, nla) \ |
113 | do { \ |
114 | if (s) \ |
115 | dprint_array(dir, nla_type, #name, \ |
116 | s->name, s->name ## _len); \ |
117 | else if (nla) \ |
118 | dprint_array(dir, nla_type, #name, \ |
119 | nla_data(nla), nla_len(nla)); \ |
120 | } while (0) |
121 | #else |
122 | #define DPRINT_TLA(a, op, b) do {} while (0) |
123 | #define DPRINT_FIELD(dir, nla_type, name, s, nla) do {} while (0) |
124 | #define DPRINT_ARRAY(dir, nla_type, name, s, nla) do {} while (0) |
125 | #endif |
126 | |
127 | /* |
128 | * Magic: provide conversion functions {{{1 |
129 | * populate struct from attribute table: |
130 | * {{{2 |
131 | */ |
132 | |
133 | /* processing of generic netlink messages is serialized. |
134 | * use one static buffer for parsing of nested attributes */ |
135 | static struct nlattr *nested_attr_tb[128]; |
136 | |
137 | #undef GENL_struct |
138 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
139 | /* *_from_attrs functions are static, but potentially unused */ \ |
140 | static int __ ## s_name ## _from_attrs(struct s_name *s, \ |
141 | struct genl_info *info, bool exclude_invariants) \ |
142 | { \ |
143 | const int maxtype = ARRAY_SIZE(s_name ## _nl_policy)-1; \ |
144 | struct nlattr *tla = info->attrs[tag_number]; \ |
145 | struct nlattr **ntb = nested_attr_tb; \ |
146 | struct nlattr *nla; \ |
147 | int err; \ |
148 | BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb)); \ |
149 | if (!tla) \ |
150 | return -ENOMSG; \ |
151 | DPRINT_TLA(#s_name, "<=-", #tag_name); \ |
152 | err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \ |
153 | if (err) \ |
154 | return err; \ |
155 | \ |
156 | s_fields \ |
157 | return 0; \ |
158 | } __attribute__((unused)) \ |
159 | static int s_name ## _from_attrs(struct s_name *s, \ |
160 | struct genl_info *info) \ |
161 | { \ |
162 | return __ ## s_name ## _from_attrs(s, info, false); \ |
163 | } __attribute__((unused)) \ |
164 | static int s_name ## _from_attrs_for_change(struct s_name *s, \ |
165 | struct genl_info *info) \ |
166 | { \ |
167 | return __ ## s_name ## _from_attrs(s, info, true); \ |
168 | } __attribute__((unused)) \ |
169 | |
170 | #define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \ |
171 | nla = ntb[attr_nr]; \ |
172 | if (nla) { \ |
173 | if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \ |
174 | pr_info("<< must not change invariant attr: %s\n", #name); \ |
175 | return -EEXIST; \ |
176 | } \ |
177 | assignment; \ |
178 | } else if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \ |
179 | /* attribute missing from payload, */ \ |
180 | /* which was expected */ \ |
181 | } else if ((attr_flag) & DRBD_F_REQUIRED) { \ |
182 | pr_info("<< missing attr: %s\n", #name); \ |
183 | return -ENOMSG; \ |
184 | } |
185 | |
186 | #undef __field |
187 | #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ |
188 | __is_signed) \ |
189 | __assign(attr_nr, attr_flag, name, nla_type, type, \ |
190 | if (s) \ |
191 | s->name = __get(nla); \ |
192 | DPRINT_FIELD("<<", nla_type, name, s, nla)) |
193 | |
194 | /* validate_nla() already checked nla_len <= maxlen appropriately. */ |
195 | #undef __array |
196 | #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ |
197 | __get, __put, __is_signed) \ |
198 | __assign(attr_nr, attr_flag, name, nla_type, type, \ |
199 | if (s) \ |
200 | s->name ## _len = \ |
201 | __get(s->name, nla, maxlen); \ |
202 | DPRINT_ARRAY("<<", nla_type, name, s, nla)) |
203 | |
204 | #include GENL_MAGIC_INCLUDE_FILE |
205 | |
206 | #undef GENL_struct |
207 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) |
208 | |
209 | /* |
210 | * Magic: define op number to op name mapping {{{1 |
211 | * {{{2 |
212 | */ |
213 | static const char *CONCATENATE(GENL_MAGIC_FAMILY, _genl_cmd_to_str)(__u8 cmd) |
214 | { |
215 | switch (cmd) { |
216 | #undef GENL_op |
217 | #define GENL_op(op_name, op_num, handler, tla_list) \ |
218 | case op_num: return #op_name; |
219 | #include GENL_MAGIC_INCLUDE_FILE |
220 | default: |
221 | return "unknown" ; |
222 | } |
223 | } |
224 | |
225 | #ifdef __KERNEL__ |
226 | #include <linux/stringify.h> |
227 | /* |
228 | * Magic: define genl_ops {{{1 |
229 | * {{{2 |
230 | */ |
231 | |
232 | #undef GENL_op |
233 | #define GENL_op(op_name, op_num, handler, tla_list) \ |
234 | { \ |
235 | handler \ |
236 | .cmd = op_name, \ |
237 | }, |
238 | |
239 | #define ZZZ_genl_ops CONCATENATE(GENL_MAGIC_FAMILY, _genl_ops) |
240 | static struct genl_ops ZZZ_genl_ops[] __read_mostly = { |
241 | #include GENL_MAGIC_INCLUDE_FILE |
242 | }; |
243 | |
244 | #undef GENL_op |
245 | #define GENL_op(op_name, op_num, handler, tla_list) |
246 | |
247 | /* |
248 | * Define the genl_family, multicast groups, {{{1 |
249 | * and provide register/unregister functions. |
250 | * {{{2 |
251 | */ |
252 | #define ZZZ_genl_family CONCATENATE(GENL_MAGIC_FAMILY, _genl_family) |
253 | static struct genl_family ZZZ_genl_family; |
254 | /* |
255 | * Magic: define multicast groups |
256 | * Magic: define multicast group registration helper |
257 | */ |
258 | #define ZZZ_genl_mcgrps CONCATENATE(GENL_MAGIC_FAMILY, _genl_mcgrps) |
259 | static const struct genl_multicast_group ZZZ_genl_mcgrps[] = { |
260 | #undef GENL_mc_group |
261 | #define GENL_mc_group(group) { .name = #group, }, |
262 | #include GENL_MAGIC_INCLUDE_FILE |
263 | }; |
264 | |
265 | enum CONCATENATE(GENL_MAGIC_FAMILY, group_ids) { |
266 | #undef GENL_mc_group |
267 | #define GENL_mc_group(group) CONCATENATE(GENL_MAGIC_FAMILY, _group_ ## group), |
268 | #include GENL_MAGIC_INCLUDE_FILE |
269 | }; |
270 | |
271 | #undef GENL_mc_group |
272 | #define GENL_mc_group(group) \ |
273 | static int CONCATENATE(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \ |
274 | struct sk_buff *skb, gfp_t flags) \ |
275 | { \ |
276 | unsigned int group_id = \ |
277 | CONCATENATE(GENL_MAGIC_FAMILY, _group_ ## group); \ |
278 | return genlmsg_multicast(&ZZZ_genl_family, skb, 0, \ |
279 | group_id, flags); \ |
280 | } |
281 | |
282 | #include GENL_MAGIC_INCLUDE_FILE |
283 | |
284 | #undef GENL_mc_group |
285 | #define GENL_mc_group(group) |
286 | |
287 | static struct genl_family ZZZ_genl_family __ro_after_init = { |
288 | .name = __stringify(GENL_MAGIC_FAMILY), |
289 | .version = GENL_MAGIC_VERSION, |
290 | #ifdef GENL_MAGIC_FAMILY_HDRSZ |
291 | .hdrsize = NLA_ALIGN(GENL_MAGIC_FAMILY_HDRSZ), |
292 | #endif |
293 | .maxattr = ARRAY_SIZE(CONCATENATE(GENL_MAGIC_FAMILY, _tla_nl_policy))-1, |
294 | .policy = CONCATENATE(GENL_MAGIC_FAMILY, _tla_nl_policy), |
295 | .ops = ZZZ_genl_ops, |
296 | .n_ops = ARRAY_SIZE(ZZZ_genl_ops), |
297 | .mcgrps = ZZZ_genl_mcgrps, |
298 | .resv_start_op = 42, /* drbd is currently the only user */ |
299 | .n_mcgrps = ARRAY_SIZE(ZZZ_genl_mcgrps), |
300 | .module = THIS_MODULE, |
301 | }; |
302 | |
303 | int CONCATENATE(GENL_MAGIC_FAMILY, _genl_register)(void) |
304 | { |
305 | return genl_register_family(family: &ZZZ_genl_family); |
306 | } |
307 | |
308 | void CONCATENATE(GENL_MAGIC_FAMILY, _genl_unregister)(void) |
309 | { |
310 | genl_unregister_family(family: &ZZZ_genl_family); |
311 | } |
312 | |
313 | /* |
314 | * Magic: provide conversion functions {{{1 |
315 | * populate skb from struct. |
316 | * {{{2 |
317 | */ |
318 | |
319 | #undef GENL_op |
320 | #define GENL_op(op_name, op_num, handler, tla_list) |
321 | |
322 | #undef GENL_struct |
323 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
324 | static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s, \ |
325 | const bool exclude_sensitive) \ |
326 | { \ |
327 | struct nlattr *tla = nla_nest_start(skb, tag_number); \ |
328 | if (!tla) \ |
329 | goto nla_put_failure; \ |
330 | DPRINT_TLA(#s_name, "-=>", #tag_name); \ |
331 | s_fields \ |
332 | nla_nest_end(skb, tla); \ |
333 | return 0; \ |
334 | \ |
335 | nla_put_failure: \ |
336 | if (tla) \ |
337 | nla_nest_cancel(skb, tla); \ |
338 | return -EMSGSIZE; \ |
339 | } \ |
340 | static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \ |
341 | struct s_name *s) \ |
342 | { \ |
343 | return s_name ## _to_skb(skb, s, 0); \ |
344 | } \ |
345 | static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \ |
346 | struct s_name *s) \ |
347 | { \ |
348 | return s_name ## _to_skb(skb, s, 1); \ |
349 | } |
350 | |
351 | |
352 | #undef __field |
353 | #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ |
354 | __is_signed) \ |
355 | if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \ |
356 | DPRINT_FIELD(">>", nla_type, name, s, NULL); \ |
357 | if (__put(skb, attr_nr, s->name)) \ |
358 | goto nla_put_failure; \ |
359 | } |
360 | |
361 | #undef __array |
362 | #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ |
363 | __get, __put, __is_signed) \ |
364 | if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \ |
365 | DPRINT_ARRAY(">>",nla_type, name, s, NULL); \ |
366 | if (__put(skb, attr_nr, min_t(int, maxlen, \ |
367 | s->name ## _len + (nla_type == NLA_NUL_STRING)),\ |
368 | s->name)) \ |
369 | goto nla_put_failure; \ |
370 | } |
371 | |
372 | #include GENL_MAGIC_INCLUDE_FILE |
373 | |
374 | |
375 | /* Functions for initializing structs to default values. */ |
376 | |
377 | #undef __field |
378 | #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ |
379 | __is_signed) |
380 | #undef __array |
381 | #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ |
382 | __get, __put, __is_signed) |
383 | #undef __u32_field_def |
384 | #define __u32_field_def(attr_nr, attr_flag, name, default) \ |
385 | x->name = default; |
386 | #undef __s32_field_def |
387 | #define __s32_field_def(attr_nr, attr_flag, name, default) \ |
388 | x->name = default; |
389 | #undef __flg_field_def |
390 | #define __flg_field_def(attr_nr, attr_flag, name, default) \ |
391 | x->name = default; |
392 | #undef __str_field_def |
393 | #define __str_field_def(attr_nr, attr_flag, name, maxlen) \ |
394 | memset(x->name, 0, sizeof(x->name)); \ |
395 | x->name ## _len = 0; |
396 | #undef GENL_struct |
397 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
398 | static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \ |
399 | static void set_ ## s_name ## _defaults(struct s_name *x) { \ |
400 | s_fields \ |
401 | } |
402 | |
403 | #include GENL_MAGIC_INCLUDE_FILE |
404 | |
405 | #endif /* __KERNEL__ */ |
406 | |
407 | /* }}}1 */ |
408 | #endif /* GENL_MAGIC_FUNC_H */ |
409 | |