blob: 377257d8f7e3557fe1f98eae3a5f906eb68bac44 [file] [log] [blame]
Lars Ellenbergec2c35a2011-03-07 10:20:08 +01001#ifndef GENL_MAGIC_FUNC_H
2#define GENL_MAGIC_FUNC_H
3
4#include <linux/genl_magic_struct.h>
5
6/*
Lars Ellenbergec2c35a2011-03-07 10:20:08 +01007 * Magic: declare tla policy {{{1
8 * Magic: declare nested policies
9 * {{{2
10 */
11#undef GENL_mc_group
12#define GENL_mc_group(group)
13
14#undef GENL_notification
15#define GENL_notification(op_name, op_num, mcast_group, tla_list)
16
17#undef GENL_op
18#define GENL_op(op_name, op_num, handler, tla_list)
19
20#undef GENL_struct
21#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
22 [tag_name] = { .type = NLA_NESTED },
23
24static struct nla_policy CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy)[] = {
25#include GENL_MAGIC_INCLUDE_FILE
26};
27
28#undef GENL_struct
29#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
30static struct nla_policy s_name ## _nl_policy[] __read_mostly = \
31{ s_fields };
32
33#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +020034#define __field(attr_nr, attr_flag, name, nla_type, _type, __get, \
35 __put, __is_signed) \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020036 [attr_nr] = { .type = nla_type },
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010037
38#undef __array
39#define __array(attr_nr, attr_flag, name, nla_type, _type, maxlen, \
Andreas Gruenbacher509100e2011-05-17 13:29:46 +020040 __get, __put, __is_signed) \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020041 [attr_nr] = { .type = nla_type, \
42 .len = maxlen - (nla_type == NLA_NUL_STRING) },
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010043
44#include GENL_MAGIC_INCLUDE_FILE
45
46#ifndef __KERNEL__
47#ifndef pr_info
48#define pr_info(args...) fprintf(stderr, args);
49#endif
50#endif
51
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010052#ifdef GENL_MAGIC_DEBUG
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010053static void dprint_field(const char *dir, int nla_type,
54 const char *name, void *valp)
55{
56 __u64 val = valp ? *(__u32 *)valp : 1;
57 switch (nla_type) {
58 case NLA_U8: val = (__u8)val;
59 case NLA_U16: val = (__u16)val;
60 case NLA_U32: val = (__u32)val;
61 pr_info("%s attr %s: %d 0x%08x\n", dir,
62 name, (int)val, (unsigned)val);
63 break;
64 case NLA_U64:
65 val = *(__u64*)valp;
66 pr_info("%s attr %s: %lld 0x%08llx\n", dir,
67 name, (long long)val, (unsigned long long)val);
68 break;
69 case NLA_FLAG:
70 if (val)
71 pr_info("%s attr %s: set\n", dir, name);
72 break;
73 }
74}
75
76static void dprint_array(const char *dir, int nla_type,
77 const char *name, const char *val, unsigned len)
78{
79 switch (nla_type) {
80 case NLA_NUL_STRING:
81 if (len && val[len-1] == '\0')
82 len--;
83 pr_info("%s attr %s: [len:%u] '%s'\n", dir, name, len, val);
84 break;
85 default:
86 /* we can always show 4 byte,
87 * thats what nlattr are aligned to. */
88 pr_info("%s attr %s: [len:%u] %02x%02x%02x%02x ...\n",
89 dir, name, len, val[0], val[1], val[2], val[3]);
90 }
91}
92
93#define DPRINT_TLA(a, op, b) pr_info("%s %s %s\n", a, op, b);
94
95/* Name is a member field name of the struct s.
96 * If s is NULL (only parsing, no copy requested in *_from_attrs()),
97 * nla is supposed to point to the attribute containing the information
98 * corresponding to that struct member. */
99#define DPRINT_FIELD(dir, nla_type, name, s, nla) \
100 do { \
101 if (s) \
102 dprint_field(dir, nla_type, #name, &s->name); \
103 else if (nla) \
104 dprint_field(dir, nla_type, #name, \
105 (nla_type == NLA_FLAG) ? NULL \
106 : nla_data(nla)); \
107 } while (0)
108
109#define DPRINT_ARRAY(dir, nla_type, name, s, nla) \
110 do { \
111 if (s) \
112 dprint_array(dir, nla_type, #name, \
113 s->name, s->name ## _len); \
114 else if (nla) \
115 dprint_array(dir, nla_type, #name, \
116 nla_data(nla), nla_len(nla)); \
117 } while (0)
118#else
119#define DPRINT_TLA(a, op, b) do {} while (0)
120#define DPRINT_FIELD(dir, nla_type, name, s, nla) do {} while (0)
121#define DPRINT_ARRAY(dir, nla_type, name, s, nla) do {} while (0)
122#endif
123
124/*
125 * Magic: provide conversion functions {{{1
126 * populate struct from attribute table:
127 * {{{2
128 */
129
130/* processing of generic netlink messages is serialized.
131 * use one static buffer for parsing of nested attributes */
132static struct nlattr *nested_attr_tb[128];
133
134#ifndef BUILD_BUG_ON
135/* Force a compilation error if condition is true */
136#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
137/* Force a compilation error if condition is true, but also produce a
138 result (of value 0 and type size_t), so the expression can be used
139 e.g. in a structure initializer (or where-ever else comma expressions
140 aren't permitted). */
141#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
142#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
143#endif
144
145#undef GENL_struct
146#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
Lars Ellenbergf3990022011-03-23 14:31:09 +0100147/* *_from_attrs functions are static, but potentially unused */ \
148static int __ ## s_name ## _from_attrs(struct s_name *s, \
149 struct genl_info *info, bool exclude_invariants) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100150{ \
151 const int maxtype = ARRAY_SIZE(s_name ## _nl_policy)-1; \
Lars Ellenbergf3990022011-03-23 14:31:09 +0100152 struct nlattr *tla = info->attrs[tag_number]; \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100153 struct nlattr **ntb = nested_attr_tb; \
154 struct nlattr *nla; \
155 int err; \
156 BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb)); \
157 if (!tla) \
158 return -ENOMSG; \
159 DPRINT_TLA(#s_name, "<=-", #tag_name); \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +0200160 err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100161 if (err) \
162 return err; \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100163 \
164 s_fields \
165 return 0; \
Lars Ellenbergf3990022011-03-23 14:31:09 +0100166} __attribute__((unused)) \
167static int s_name ## _from_attrs(struct s_name *s, \
168 struct genl_info *info) \
169{ \
170 return __ ## s_name ## _from_attrs(s, info, false); \
171} __attribute__((unused)) \
172static int s_name ## _from_attrs_for_change(struct s_name *s, \
173 struct genl_info *info) \
174{ \
175 return __ ## s_name ## _from_attrs(s, info, true); \
176} __attribute__((unused)) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100177
Lars Ellenbergf3990022011-03-23 14:31:09 +0100178#define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +0200179 nla = ntb[attr_nr]; \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100180 if (nla) { \
Mark Rustaddb404b12014-10-14 06:28:38 -0700181 if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
Lars Ellenbergf3990022011-03-23 14:31:09 +0100182 pr_info("<< must not change invariant attr: %s\n", #name); \
183 return -EEXIST; \
184 } \
185 assignment; \
Mark Rustaddb404b12014-10-14 06:28:38 -0700186 } else if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
Lars Ellenbergf3990022011-03-23 14:31:09 +0100187 /* attribute missing from payload, */ \
188 /* which was expected */ \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +0200189 } else if ((attr_flag) & DRBD_F_REQUIRED) { \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100190 pr_info("<< missing attr: %s\n", #name); \
191 return -ENOMSG; \
192 }
193
Lars Ellenbergf3990022011-03-23 14:31:09 +0100194#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200195#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
196 __is_signed) \
Lars Ellenbergf3990022011-03-23 14:31:09 +0100197 __assign(attr_nr, attr_flag, name, nla_type, type, \
198 if (s) \
199 s->name = __get(nla); \
200 DPRINT_FIELD("<<", nla_type, name, s, nla))
201
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100202/* validate_nla() already checked nla_len <= maxlen appropriately. */
203#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200204#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
205 __get, __put, __is_signed) \
Lars Ellenbergf3990022011-03-23 14:31:09 +0100206 __assign(attr_nr, attr_flag, name, nla_type, type, \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100207 if (s) \
208 s->name ## _len = \
209 __get(s->name, nla, maxlen); \
Lars Ellenbergf3990022011-03-23 14:31:09 +0100210 DPRINT_ARRAY("<<", nla_type, name, s, nla))
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100211
212#include GENL_MAGIC_INCLUDE_FILE
213
214#undef GENL_struct
215#define GENL_struct(tag_name, tag_number, s_name, s_fields)
216
217/*
218 * Magic: define op number to op name mapping {{{1
219 * {{{2
220 */
221const char *CONCAT_(GENL_MAGIC_FAMILY, _genl_cmd_to_str)(__u8 cmd)
222{
223 switch (cmd) {
224#undef GENL_op
225#define GENL_op(op_name, op_num, handler, tla_list) \
226 case op_num: return #op_name;
227#include GENL_MAGIC_INCLUDE_FILE
228 default:
229 return "unknown";
230 }
231}
232
233#ifdef __KERNEL__
234#include <linux/stringify.h>
235/*
236 * Magic: define genl_ops {{{1
237 * {{{2
238 */
239
240#undef GENL_op
241#define GENL_op(op_name, op_num, handler, tla_list) \
242{ \
243 handler \
244 .cmd = op_name, \
245 .policy = CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy), \
246},
247
248#define ZZZ_genl_ops CONCAT_(GENL_MAGIC_FAMILY, _genl_ops)
249static struct genl_ops ZZZ_genl_ops[] __read_mostly = {
250#include GENL_MAGIC_INCLUDE_FILE
251};
252
253#undef GENL_op
254#define GENL_op(op_name, op_num, handler, tla_list)
255
256/*
257 * Define the genl_family, multicast groups, {{{1
258 * and provide register/unregister functions.
259 * {{{2
260 */
261#define ZZZ_genl_family CONCAT_(GENL_MAGIC_FAMILY, _genl_family)
Johannes Berg489111e2016-10-24 14:40:03 +0200262static struct genl_family ZZZ_genl_family;
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100263/*
264 * Magic: define multicast groups
265 * Magic: define multicast group registration helper
266 */
Johannes Berg2a94fe42013-11-19 15:19:39 +0100267#define ZZZ_genl_mcgrps CONCAT_(GENL_MAGIC_FAMILY, _genl_mcgrps)
268static const struct genl_multicast_group ZZZ_genl_mcgrps[] = {
269#undef GENL_mc_group
270#define GENL_mc_group(group) { .name = #group, },
271#include GENL_MAGIC_INCLUDE_FILE
272};
273
274enum CONCAT_(GENL_MAGIC_FAMILY, group_ids) {
275#undef GENL_mc_group
276#define GENL_mc_group(group) CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group),
277#include GENL_MAGIC_INCLUDE_FILE
278};
279
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100280#undef GENL_mc_group
281#define GENL_mc_group(group) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100282static int CONCAT_(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \
283 struct sk_buff *skb, gfp_t flags) \
284{ \
285 unsigned int group_id = \
Johannes Berg2a94fe42013-11-19 15:19:39 +0100286 CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group); \
Johannes Berg68eb5502013-11-19 15:19:38 +0100287 return genlmsg_multicast(&ZZZ_genl_family, skb, 0, \
288 group_id, flags); \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100289}
290
291#include GENL_MAGIC_INCLUDE_FILE
292
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100293#undef GENL_mc_group
294#define GENL_mc_group(group)
Johannes Berg2a94fe42013-11-19 15:19:39 +0100295
Johannes Berg56989f62016-10-24 14:40:05 +0200296static struct genl_family ZZZ_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200297 .name = __stringify(GENL_MAGIC_FAMILY),
298 .version = GENL_MAGIC_VERSION,
299#ifdef GENL_MAGIC_FAMILY_HDRSZ
300 .hdrsize = NLA_ALIGN(GENL_MAGIC_FAMILY_HDRSZ),
301#endif
302 .maxattr = ARRAY_SIZE(drbd_tla_nl_policy)-1,
303 .ops = ZZZ_genl_ops,
304 .n_ops = ARRAY_SIZE(ZZZ_genl_ops),
305 .mcgrps = ZZZ_genl_mcgrps,
306 .n_mcgrps = ARRAY_SIZE(ZZZ_genl_mcgrps),
307 .module = THIS_MODULE,
308};
309
Johannes Berg2a94fe42013-11-19 15:19:39 +0100310int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void)
311{
Johannes Berg489111e2016-10-24 14:40:03 +0200312 return genl_register_family(&ZZZ_genl_family);
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100313}
314
315void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void)
316{
317 genl_unregister_family(&ZZZ_genl_family);
318}
319
320/*
321 * Magic: provide conversion functions {{{1
322 * populate skb from struct.
323 * {{{2
324 */
325
326#undef GENL_op
327#define GENL_op(op_name, op_num, handler, tla_list)
328
329#undef GENL_struct
330#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
331static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s, \
332 const bool exclude_sensitive) \
333{ \
334 struct nlattr *tla = nla_nest_start(skb, tag_number); \
335 if (!tla) \
336 goto nla_put_failure; \
337 DPRINT_TLA(#s_name, "-=>", #tag_name); \
338 s_fields \
339 nla_nest_end(skb, tla); \
340 return 0; \
341 \
342nla_put_failure: \
343 if (tla) \
344 nla_nest_cancel(skb, tla); \
345 return -EMSGSIZE; \
346} \
347static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \
348 struct s_name *s) \
349{ \
350 return s_name ## _to_skb(skb, s, 0); \
351} \
352static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \
353 struct s_name *s) \
354{ \
355 return s_name ## _to_skb(skb, s, 1); \
356}
357
358
359#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200360#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
361 __is_signed) \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +0200362 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100363 DPRINT_FIELD(">>", nla_type, name, s, NULL); \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +0200364 if (__put(skb, attr_nr, s->name)) \
365 goto nla_put_failure; \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100366 }
367
368#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200369#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
370 __get, __put, __is_signed) \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +0200371 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100372 DPRINT_ARRAY(">>",nla_type, name, s, NULL); \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +0200373 if (__put(skb, attr_nr, min_t(int, maxlen, \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100374 s->name ## _len + (nla_type == NLA_NUL_STRING)),\
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +0200375 s->name)) \
376 goto nla_put_failure; \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100377 }
378
379#include GENL_MAGIC_INCLUDE_FILE
380
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +0200381
382/* Functions for initializing structs to default values. */
383
384#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200385#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
386 __is_signed)
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +0200387#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200388#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
389 __get, __put, __is_signed)
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +0200390#undef __u32_field_def
391#define __u32_field_def(attr_nr, attr_flag, name, default) \
392 x->name = default;
Andreas Gruenbacher3a45abd2011-05-12 12:02:54 +0200393#undef __s32_field_def
394#define __s32_field_def(attr_nr, attr_flag, name, default) \
395 x->name = default;
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +0200396#undef __flg_field_def
397#define __flg_field_def(attr_nr, attr_flag, name, default) \
398 x->name = default;
399#undef __str_field_def
400#define __str_field_def(attr_nr, attr_flag, name, maxlen) \
401 memset(x->name, 0, sizeof(x->name)); \
402 x->name ## _len = 0;
403#undef GENL_struct
404#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
405static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \
406static void set_ ## s_name ## _defaults(struct s_name *x) { \
407s_fields \
408}
409
410#include GENL_MAGIC_INCLUDE_FILE
411
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100412#endif /* __KERNEL__ */
413
414/* }}}1 */
415#endif /* GENL_MAGIC_FUNC_H */
416/* vim: set foldmethod=marker foldlevel=1 nofoldenable : */