1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef GENL_MAGIC_FUNC_H
3 #define GENL_MAGIC_FUNC_H
5 #include <linux/genl_magic_struct.h>
8 * Magic: declare tla policy {{{1
9 * Magic: declare nested policies
13 #define GENL_mc_group(group)
15 #undef GENL_notification
16 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
19 #define GENL_op(op_name, op_num, handler, tla_list)
22 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
23 [tag_name] = { .type = NLA_NESTED },
25 static struct nla_policy CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy)[] = {
26 #include GENL_MAGIC_INCLUDE_FILE
30 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
31 static struct nla_policy s_name ## _nl_policy[] __read_mostly = \
35 #define __field(attr_nr, attr_flag, name, nla_type, _type, __get, \
37 [attr_nr] = { .type = nla_type },
40 #define __array(attr_nr, attr_flag, name, nla_type, _type, maxlen, \
41 __get, __put, __is_signed) \
42 [attr_nr] = { .type = nla_type, \
43 .len = maxlen - (nla_type == NLA_NUL_STRING) },
45 #include GENL_MAGIC_INCLUDE_FILE
49 #define pr_info(args...) fprintf(stderr, args);
53 #ifdef GENL_MAGIC_DEBUG
54 static void dprint_field(const char *dir, int nla_type,
55 const char *name, void *valp)
57 __u64 val = valp ? *(__u32 *)valp : 1;
59 case NLA_U8: val = (__u8)val;
60 case NLA_U16: val = (__u16)val;
61 case NLA_U32: val = (__u32)val;
62 pr_info("%s attr %s: %d 0x%08x\n", dir,
63 name, (int)val, (unsigned)val);
67 pr_info("%s attr %s: %lld 0x%08llx\n", dir,
68 name, (long long)val, (unsigned long long)val);
72 pr_info("%s attr %s: set\n", dir, name);
77 static void dprint_array(const char *dir, int nla_type,
78 const char *name, const char *val, unsigned len)
82 if (len && val[len-1] == '\0')
84 pr_info("%s attr %s: [len:%u] '%s'\n", dir, name, len, val);
87 /* we can always show 4 byte,
88 * thats what nlattr are aligned to. */
89 pr_info("%s attr %s: [len:%u] %02x%02x%02x%02x ...\n",
90 dir, name, len, val[0], val[1], val[2], val[3]);
94 #define DPRINT_TLA(a, op, b) pr_info("%s %s %s\n", a, op, b);
96 /* Name is a member field name of the struct s.
97 * If s is NULL (only parsing, no copy requested in *_from_attrs()),
98 * nla is supposed to point to the attribute containing the information
99 * corresponding to that struct member. */
100 #define DPRINT_FIELD(dir, nla_type, name, s, nla) \
103 dprint_field(dir, nla_type, #name, &s->name); \
105 dprint_field(dir, nla_type, #name, \
106 (nla_type == NLA_FLAG) ? NULL \
110 #define DPRINT_ARRAY(dir, nla_type, name, s, nla) \
113 dprint_array(dir, nla_type, #name, \
114 s->name, s->name ## _len); \
116 dprint_array(dir, nla_type, #name, \
117 nla_data(nla), nla_len(nla)); \
120 #define DPRINT_TLA(a, op, b) do {} while (0)
121 #define DPRINT_FIELD(dir, nla_type, name, s, nla) do {} while (0)
122 #define DPRINT_ARRAY(dir, nla_type, name, s, nla) do {} while (0)
126 * Magic: provide conversion functions {{{1
127 * populate struct from attribute table:
131 /* processing of generic netlink messages is serialized.
132 * use one static buffer for parsing of nested attributes */
133 static struct nlattr *nested_attr_tb[128];
136 /* Force a compilation error if condition is true */
137 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
138 /* Force a compilation error if condition is true, but also produce a
139 result (of value 0 and type size_t), so the expression can be used
140 e.g. in a structure initializer (or where-ever else comma expressions
141 aren't permitted). */
142 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
143 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
147 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
148 /* *_from_attrs functions are static, but potentially unused */ \
149 static int __ ## s_name ## _from_attrs(struct s_name *s, \
150 struct genl_info *info, bool exclude_invariants) \
152 const int maxtype = ARRAY_SIZE(s_name ## _nl_policy)-1; \
153 struct nlattr *tla = info->attrs[tag_number]; \
154 struct nlattr **ntb = nested_attr_tb; \
155 struct nlattr *nla; \
157 BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb)); \
160 DPRINT_TLA(#s_name, "<=-", #tag_name); \
161 err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \
167 } __attribute__((unused)) \
168 static int s_name ## _from_attrs(struct s_name *s, \
169 struct genl_info *info) \
171 return __ ## s_name ## _from_attrs(s, info, false); \
172 } __attribute__((unused)) \
173 static int s_name ## _from_attrs_for_change(struct s_name *s, \
174 struct genl_info *info) \
176 return __ ## s_name ## _from_attrs(s, info, true); \
177 } __attribute__((unused)) \
179 #define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \
180 nla = ntb[attr_nr]; \
182 if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
183 pr_info("<< must not change invariant attr: %s\n", #name); \
187 } else if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
188 /* attribute missing from payload, */ \
189 /* which was expected */ \
190 } else if ((attr_flag) & DRBD_F_REQUIRED) { \
191 pr_info("<< missing attr: %s\n", #name); \
196 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
198 __assign(attr_nr, attr_flag, name, nla_type, type, \
200 s->name = __get(nla); \
201 DPRINT_FIELD("<<", nla_type, name, s, nla))
203 /* validate_nla() already checked nla_len <= maxlen appropriately. */
205 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
206 __get, __put, __is_signed) \
207 __assign(attr_nr, attr_flag, name, nla_type, type, \
210 __get(s->name, nla, maxlen); \
211 DPRINT_ARRAY("<<", nla_type, name, s, nla))
213 #include GENL_MAGIC_INCLUDE_FILE
216 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
219 * Magic: define op number to op name mapping {{{1
222 const char *CONCAT_(GENL_MAGIC_FAMILY, _genl_cmd_to_str)(__u8 cmd)
226 #define GENL_op(op_name, op_num, handler, tla_list) \
227 case op_num: return #op_name;
228 #include GENL_MAGIC_INCLUDE_FILE
235 #include <linux/stringify.h>
237 * Magic: define genl_ops {{{1
242 #define GENL_op(op_name, op_num, handler, tla_list) \
246 .policy = CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy), \
249 #define ZZZ_genl_ops CONCAT_(GENL_MAGIC_FAMILY, _genl_ops)
250 static struct genl_ops ZZZ_genl_ops[] __read_mostly = {
251 #include GENL_MAGIC_INCLUDE_FILE
255 #define GENL_op(op_name, op_num, handler, tla_list)
258 * Define the genl_family, multicast groups, {{{1
259 * and provide register/unregister functions.
262 #define ZZZ_genl_family CONCAT_(GENL_MAGIC_FAMILY, _genl_family)
263 static struct genl_family ZZZ_genl_family;
265 * Magic: define multicast groups
266 * Magic: define multicast group registration helper
268 #define ZZZ_genl_mcgrps CONCAT_(GENL_MAGIC_FAMILY, _genl_mcgrps)
269 static const struct genl_multicast_group ZZZ_genl_mcgrps[] = {
271 #define GENL_mc_group(group) { .name = #group, },
272 #include GENL_MAGIC_INCLUDE_FILE
275 enum CONCAT_(GENL_MAGIC_FAMILY, group_ids) {
277 #define GENL_mc_group(group) CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group),
278 #include GENL_MAGIC_INCLUDE_FILE
282 #define GENL_mc_group(group) \
283 static int CONCAT_(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \
284 struct sk_buff *skb, gfp_t flags) \
286 unsigned int group_id = \
287 CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group); \
288 return genlmsg_multicast(&ZZZ_genl_family, skb, 0, \
292 #include GENL_MAGIC_INCLUDE_FILE
295 #define GENL_mc_group(group)
297 static struct genl_family ZZZ_genl_family __ro_after_init = {
298 .name = __stringify(GENL_MAGIC_FAMILY),
299 .version = GENL_MAGIC_VERSION,
300 #ifdef GENL_MAGIC_FAMILY_HDRSZ
301 .hdrsize = NLA_ALIGN(GENL_MAGIC_FAMILY_HDRSZ),
303 .maxattr = ARRAY_SIZE(drbd_tla_nl_policy)-1,
305 .n_ops = ARRAY_SIZE(ZZZ_genl_ops),
306 .mcgrps = ZZZ_genl_mcgrps,
307 .n_mcgrps = ARRAY_SIZE(ZZZ_genl_mcgrps),
308 .module = THIS_MODULE,
311 int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void)
313 return genl_register_family(&ZZZ_genl_family);
316 void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void)
318 genl_unregister_family(&ZZZ_genl_family);
322 * Magic: provide conversion functions {{{1
323 * populate skb from struct.
328 #define GENL_op(op_name, op_num, handler, tla_list)
331 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
332 static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s, \
333 const bool exclude_sensitive) \
335 struct nlattr *tla = nla_nest_start(skb, tag_number); \
337 goto nla_put_failure; \
338 DPRINT_TLA(#s_name, "-=>", #tag_name); \
340 nla_nest_end(skb, tla); \
345 nla_nest_cancel(skb, tla); \
348 static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \
351 return s_name ## _to_skb(skb, s, 0); \
353 static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \
356 return s_name ## _to_skb(skb, s, 1); \
361 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
363 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
364 DPRINT_FIELD(">>", nla_type, name, s, NULL); \
365 if (__put(skb, attr_nr, s->name)) \
366 goto nla_put_failure; \
370 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
371 __get, __put, __is_signed) \
372 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
373 DPRINT_ARRAY(">>",nla_type, name, s, NULL); \
374 if (__put(skb, attr_nr, min_t(int, maxlen, \
375 s->name ## _len + (nla_type == NLA_NUL_STRING)),\
377 goto nla_put_failure; \
380 #include GENL_MAGIC_INCLUDE_FILE
383 /* Functions for initializing structs to default values. */
386 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
389 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
390 __get, __put, __is_signed)
391 #undef __u32_field_def
392 #define __u32_field_def(attr_nr, attr_flag, name, default) \
394 #undef __s32_field_def
395 #define __s32_field_def(attr_nr, attr_flag, name, default) \
397 #undef __flg_field_def
398 #define __flg_field_def(attr_nr, attr_flag, name, default) \
400 #undef __str_field_def
401 #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
402 memset(x->name, 0, sizeof(x->name)); \
405 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
406 static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \
407 static void set_ ## s_name ## _defaults(struct s_name *x) { \
411 #include GENL_MAGIC_INCLUDE_FILE
413 #endif /* __KERNEL__ */
416 #endif /* GENL_MAGIC_FUNC_H */
417 /* vim: set foldmethod=marker foldlevel=1 nofoldenable : */