1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef GENL_MAGIC_FUNC_H
3 #define GENL_MAGIC_FUNC_H
5 #include <linux/args.h>
6 #include <linux/build_bug.h>
7 #include <linux/genl_magic_struct.h>
10 * Magic: declare tla policy {{{1
11 * Magic: declare nested policies
15 #define GENL_mc_group(group)
17 #undef GENL_notification
18 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
21 #define GENL_op(op_name, op_num, handler, tla_list)
24 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
25 [tag_name] = { .type = NLA_NESTED },
27 static struct nla_policy
CONCATENATE(GENL_MAGIC_FAMILY
, _tla_nl_policy
)[] = {
28 #include GENL_MAGIC_INCLUDE_FILE
32 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
33 static struct nla_policy s_name ## _nl_policy[] __read_mostly = \
37 #define __field(attr_nr, attr_flag, name, nla_type, _type, __get, \
39 [attr_nr] = { .type = nla_type },
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) },
47 #include GENL_MAGIC_INCLUDE_FILE
51 #define pr_info(args...) fprintf(stderr, args);
55 #ifdef GENL_MAGIC_DEBUG
56 static void dprint_field(const char *dir
, int nla_type
,
57 const char *name
, void *valp
)
59 __u64 val
= valp
? *(__u32
*)valp
: 1;
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
);
69 pr_info("%s attr %s: %lld 0x%08llx\n", dir
,
70 name
, (long long)val
, (unsigned long long)val
);
74 pr_info("%s attr %s: set\n", dir
, name
);
79 static void dprint_array(const char *dir
, int nla_type
,
80 const char *name
, const char *val
, unsigned len
)
84 if (len
&& val
[len
-1] == '\0')
86 pr_info("%s attr %s: [len:%u] '%s'\n", dir
, name
, len
, val
);
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]);
96 #define DPRINT_TLA(a, op, b) pr_info("%s %s %s\n", a, op, b);
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) \
105 dprint_field(dir, nla_type, #name, &s->name); \
107 dprint_field(dir, nla_type, #name, \
108 (nla_type == NLA_FLAG) ? NULL \
112 #define DPRINT_ARRAY(dir, nla_type, name, s, nla) \
115 dprint_array(dir, nla_type, #name, \
116 s->name, s->name ## _len); \
118 dprint_array(dir, nla_type, #name, \
119 nla_data(nla), nla_len(nla)); \
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)
128 * Magic: provide conversion functions {{{1
129 * populate struct from attribute table:
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];
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) \
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; \
148 BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb)); \
151 DPRINT_TLA(#s_name, "<=-", #tag_name); \
152 err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \
158 } __attribute__((unused)) \
159 static int s_name ## _from_attrs(struct s_name *s, \
160 struct genl_info *info) \
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) \
167 return __ ## s_name ## _from_attrs(s, info, true); \
168 } __attribute__((unused)) \
170 #define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \
171 nla = ntb[attr_nr]; \
173 if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
174 pr_info("<< must not change invariant attr: %s\n", #name); \
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); \
187 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
189 __assign(attr_nr, attr_flag, name, nla_type, type, \
191 s->name = __get(nla); \
192 DPRINT_FIELD("<<", nla_type, name, s, nla))
194 /* validate_nla() already checked nla_len <= maxlen appropriately. */
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, \
201 __get(s->name, nla, maxlen); \
202 DPRINT_ARRAY("<<", nla_type, name, s, nla))
204 #include GENL_MAGIC_INCLUDE_FILE
207 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
210 * Magic: define op number to op name mapping {{{1
213 static const char *CONCATENATE(GENL_MAGIC_FAMILY
, _genl_cmd_to_str
)(__u8 cmd
)
217 #define GENL_op(op_name, op_num, handler, tla_list) \
218 case op_num: return #op_name;
219 #include GENL_MAGIC_INCLUDE_FILE
226 #include <linux/stringify.h>
228 * Magic: define genl_ops {{{1
233 #define GENL_op(op_name, op_num, handler, tla_list) \
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
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 CONCATENATE(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 CONCATENATE(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 CONCATENATE(GENL_MAGIC_FAMILY
, group_ids
) {
267 #define GENL_mc_group(group) CONCATENATE(GENL_MAGIC_FAMILY, _group_ ## group),
268 #include GENL_MAGIC_INCLUDE_FILE
272 #define GENL_mc_group(group) \
273 static int CONCATENATE(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \
274 struct sk_buff *skb, gfp_t flags) \
276 unsigned int group_id = \
277 CONCATENATE(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(CONCATENATE(GENL_MAGIC_FAMILY
, _tla_nl_policy
))-1,
294 .policy
= CONCATENATE(GENL_MAGIC_FAMILY
, _tla_nl_policy
),
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
,
303 int CONCATENATE(GENL_MAGIC_FAMILY
, _genl_register
)(void)
305 return genl_register_family(&ZZZ_genl_family
);
308 void CONCATENATE(GENL_MAGIC_FAMILY
, _genl_unregister
)(void)
310 genl_unregister_family(&ZZZ_genl_family
);
314 * Magic: provide conversion functions {{{1
315 * populate skb from struct.
320 #define GENL_op(op_name, op_num, handler, tla_list)
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) \
327 struct nlattr *tla = nla_nest_start(skb, tag_number); \
329 goto nla_put_failure; \
330 DPRINT_TLA(#s_name, "-=>", #tag_name); \
332 nla_nest_end(skb, tla); \
337 nla_nest_cancel(skb, tla); \
340 static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \
343 return s_name ## _to_skb(skb, s, 0); \
345 static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \
348 return s_name ## _to_skb(skb, s, 1); \
353 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
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; \
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)),\
369 goto nla_put_failure; \
372 #include GENL_MAGIC_INCLUDE_FILE
375 /* Functions for initializing structs to default values. */
378 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
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) \
386 #undef __s32_field_def
387 #define __s32_field_def(attr_nr, attr_flag, name, default) \
389 #undef __flg_field_def
390 #define __flg_field_def(attr_nr, attr_flag, 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)); \
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) { \
403 #include GENL_MAGIC_INCLUDE_FILE
405 #endif /* __KERNEL__ */
408 #endif /* GENL_MAGIC_FUNC_H */