1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef GENL_MAGIC_FUNC_H
3 #define GENL_MAGIC_FUNC_H
5 #include <linux/build_bug.h>
6 #include <linux/genl_magic_struct.h>
9 * Magic: declare tla policy {{{1
10 * Magic: declare nested policies
14 #define GENL_mc_group(group)
16 #undef GENL_notification
17 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
20 #define GENL_op(op_name, op_num, handler, tla_list)
23 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
24 [tag_name] = { .type = NLA_NESTED },
26 static struct nla_policy
CONCAT_(GENL_MAGIC_FAMILY
, _tla_nl_policy
)[] = {
27 #include GENL_MAGIC_INCLUDE_FILE
31 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
32 static struct nla_policy s_name ## _nl_policy[] __read_mostly = \
36 #define __field(attr_nr, attr_flag, name, nla_type, _type, __get, \
38 [attr_nr] = { .type = nla_type },
41 #define __array(attr_nr, attr_flag, name, nla_type, _type, maxlen, \
42 __get, __put, __is_signed) \
43 [attr_nr] = { .type = nla_type, \
44 .len = maxlen - (nla_type == NLA_NUL_STRING) },
46 #include GENL_MAGIC_INCLUDE_FILE
50 #define pr_info(args...) fprintf(stderr, args);
54 #ifdef GENL_MAGIC_DEBUG
55 static void dprint_field(const char *dir
, int nla_type
,
56 const char *name
, void *valp
)
58 __u64 val
= valp
? *(__u32
*)valp
: 1;
60 case NLA_U8
: val
= (__u8
)val
;
61 case NLA_U16
: val
= (__u16
)val
;
62 case NLA_U32
: val
= (__u32
)val
;
63 pr_info("%s attr %s: %d 0x%08x\n", dir
,
64 name
, (int)val
, (unsigned)val
);
68 pr_info("%s attr %s: %lld 0x%08llx\n", dir
,
69 name
, (long long)val
, (unsigned long long)val
);
73 pr_info("%s attr %s: set\n", dir
, name
);
78 static void dprint_array(const char *dir
, int nla_type
,
79 const char *name
, const char *val
, unsigned len
)
83 if (len
&& val
[len
-1] == '\0')
85 pr_info("%s attr %s: [len:%u] '%s'\n", dir
, name
, len
, val
);
88 /* we can always show 4 byte,
89 * thats what nlattr are aligned to. */
90 pr_info("%s attr %s: [len:%u] %02x%02x%02x%02x ...\n",
91 dir
, name
, len
, val
[0], val
[1], val
[2], val
[3]);
95 #define DPRINT_TLA(a, op, b) pr_info("%s %s %s\n", a, op, b);
97 /* Name is a member field name of the struct s.
98 * If s is NULL (only parsing, no copy requested in *_from_attrs()),
99 * nla is supposed to point to the attribute containing the information
100 * corresponding to that struct member. */
101 #define DPRINT_FIELD(dir, nla_type, name, s, nla) \
104 dprint_field(dir, nla_type, #name, &s->name); \
106 dprint_field(dir, nla_type, #name, \
107 (nla_type == NLA_FLAG) ? NULL \
111 #define DPRINT_ARRAY(dir, nla_type, name, s, nla) \
114 dprint_array(dir, nla_type, #name, \
115 s->name, s->name ## _len); \
117 dprint_array(dir, nla_type, #name, \
118 nla_data(nla), nla_len(nla)); \
121 #define DPRINT_TLA(a, op, b) do {} while (0)
122 #define DPRINT_FIELD(dir, nla_type, name, s, nla) do {} while (0)
123 #define DPRINT_ARRAY(dir, nla_type, name, s, nla) do {} while (0)
127 * Magic: provide conversion functions {{{1
128 * populate struct from attribute table:
132 /* processing of generic netlink messages is serialized.
133 * use one static buffer for parsing of nested attributes */
134 static struct nlattr
*nested_attr_tb
[128];
137 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
138 /* *_from_attrs functions are static, but potentially unused */ \
139 static int __ ## s_name ## _from_attrs(struct s_name *s, \
140 struct genl_info *info, bool exclude_invariants) \
142 const int maxtype = ARRAY_SIZE(s_name ## _nl_policy)-1; \
143 struct nlattr *tla = info->attrs[tag_number]; \
144 struct nlattr **ntb = nested_attr_tb; \
145 struct nlattr *nla; \
147 BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb)); \
150 DPRINT_TLA(#s_name, "<=-", #tag_name); \
151 err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \
157 } __attribute__((unused)) \
158 static int s_name ## _from_attrs(struct s_name *s, \
159 struct genl_info *info) \
161 return __ ## s_name ## _from_attrs(s, info, false); \
162 } __attribute__((unused)) \
163 static int s_name ## _from_attrs_for_change(struct s_name *s, \
164 struct genl_info *info) \
166 return __ ## s_name ## _from_attrs(s, info, true); \
167 } __attribute__((unused)) \
169 #define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \
170 nla = ntb[attr_nr]; \
172 if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
173 pr_info("<< must not change invariant attr: %s\n", #name); \
177 } else if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
178 /* attribute missing from payload, */ \
179 /* which was expected */ \
180 } else if ((attr_flag) & DRBD_F_REQUIRED) { \
181 pr_info("<< missing attr: %s\n", #name); \
186 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
188 __assign(attr_nr, attr_flag, name, nla_type, type, \
190 s->name = __get(nla); \
191 DPRINT_FIELD("<<", nla_type, name, s, nla))
193 /* validate_nla() already checked nla_len <= maxlen appropriately. */
195 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
196 __get, __put, __is_signed) \
197 __assign(attr_nr, attr_flag, name, nla_type, type, \
200 __get(s->name, nla, maxlen); \
201 DPRINT_ARRAY("<<", nla_type, name, s, nla))
203 #include GENL_MAGIC_INCLUDE_FILE
206 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
209 * Magic: define op number to op name mapping {{{1
212 const char *CONCAT_(GENL_MAGIC_FAMILY
, _genl_cmd_to_str
)(__u8 cmd
)
216 #define GENL_op(op_name, op_num, handler, tla_list) \
217 case op_num: return #op_name;
218 #include GENL_MAGIC_INCLUDE_FILE
225 #include <linux/stringify.h>
227 * Magic: define genl_ops {{{1
232 #define GENL_op(op_name, op_num, handler, tla_list) \
236 .policy = CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy), \
239 #define ZZZ_genl_ops CONCAT_(GENL_MAGIC_FAMILY, _genl_ops)
240 static struct genl_ops ZZZ_genl_ops
[] __read_mostly
= {
241 #include GENL_MAGIC_INCLUDE_FILE
245 #define GENL_op(op_name, op_num, handler, tla_list)
248 * Define the genl_family, multicast groups, {{{1
249 * and provide register/unregister functions.
252 #define ZZZ_genl_family CONCAT_(GENL_MAGIC_FAMILY, _genl_family)
253 static struct genl_family ZZZ_genl_family
;
255 * Magic: define multicast groups
256 * Magic: define multicast group registration helper
258 #define ZZZ_genl_mcgrps CONCAT_(GENL_MAGIC_FAMILY, _genl_mcgrps)
259 static const struct genl_multicast_group ZZZ_genl_mcgrps
[] = {
261 #define GENL_mc_group(group) { .name = #group, },
262 #include GENL_MAGIC_INCLUDE_FILE
265 enum CONCAT_(GENL_MAGIC_FAMILY
, group_ids
) {
267 #define GENL_mc_group(group) CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group),
268 #include GENL_MAGIC_INCLUDE_FILE
272 #define GENL_mc_group(group) \
273 static int CONCAT_(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \
274 struct sk_buff *skb, gfp_t flags) \
276 unsigned int group_id = \
277 CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group); \
278 return genlmsg_multicast(&ZZZ_genl_family, skb, 0, \
282 #include GENL_MAGIC_INCLUDE_FILE
285 #define GENL_mc_group(group)
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
),
293 .maxattr
= ARRAY_SIZE(drbd_tla_nl_policy
)-1,
295 .n_ops
= ARRAY_SIZE(ZZZ_genl_ops
),
296 .mcgrps
= ZZZ_genl_mcgrps
,
297 .n_mcgrps
= ARRAY_SIZE(ZZZ_genl_mcgrps
),
298 .module
= THIS_MODULE
,
301 int CONCAT_(GENL_MAGIC_FAMILY
, _genl_register
)(void)
303 return genl_register_family(&ZZZ_genl_family
);
306 void CONCAT_(GENL_MAGIC_FAMILY
, _genl_unregister
)(void)
308 genl_unregister_family(&ZZZ_genl_family
);
312 * Magic: provide conversion functions {{{1
313 * populate skb from struct.
318 #define GENL_op(op_name, op_num, handler, tla_list)
321 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
322 static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s, \
323 const bool exclude_sensitive) \
325 struct nlattr *tla = nla_nest_start(skb, tag_number); \
327 goto nla_put_failure; \
328 DPRINT_TLA(#s_name, "-=>", #tag_name); \
330 nla_nest_end(skb, tla); \
335 nla_nest_cancel(skb, tla); \
338 static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \
341 return s_name ## _to_skb(skb, s, 0); \
343 static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \
346 return s_name ## _to_skb(skb, s, 1); \
351 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
353 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
354 DPRINT_FIELD(">>", nla_type, name, s, NULL); \
355 if (__put(skb, attr_nr, s->name)) \
356 goto nla_put_failure; \
360 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
361 __get, __put, __is_signed) \
362 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
363 DPRINT_ARRAY(">>",nla_type, name, s, NULL); \
364 if (__put(skb, attr_nr, min_t(int, maxlen, \
365 s->name ## _len + (nla_type == NLA_NUL_STRING)),\
367 goto nla_put_failure; \
370 #include GENL_MAGIC_INCLUDE_FILE
373 /* Functions for initializing structs to default values. */
376 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
379 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
380 __get, __put, __is_signed)
381 #undef __u32_field_def
382 #define __u32_field_def(attr_nr, attr_flag, name, default) \
384 #undef __s32_field_def
385 #define __s32_field_def(attr_nr, attr_flag, name, default) \
387 #undef __flg_field_def
388 #define __flg_field_def(attr_nr, attr_flag, name, default) \
390 #undef __str_field_def
391 #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
392 memset(x->name, 0, sizeof(x->name)); \
395 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
396 static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \
397 static void set_ ## s_name ## _defaults(struct s_name *x) { \
401 #include GENL_MAGIC_INCLUDE_FILE
403 #endif /* __KERNEL__ */
406 #endif /* GENL_MAGIC_FUNC_H */
407 /* vim: set foldmethod=marker foldlevel=1 nofoldenable : */