1 #ifndef GENL_MAGIC_FUNC_H
2 #define GENL_MAGIC_FUNC_H
3 
4 #include <linux/genl_magic_struct.h>
5 
6 /*
7  * 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 
24 static 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)		\
30 static struct nla_policy s_name ## _nl_policy[] __read_mostly =		\
31 { s_fields };
32 
33 #undef __field
34 #define __field(attr_nr, attr_flag, name, nla_type, _type, __get,	\
35 		 __put, __is_signed)					\
36 	[attr_nr] = { .type = nla_type },
37 
38 #undef __array
39 #define __array(attr_nr, attr_flag, name, nla_type, _type, maxlen,	\
40 		__get, __put, __is_signed)				\
41 	[attr_nr] = { .type = nla_type,					\
42 		      .len = maxlen - (nla_type == NLA_NUL_STRING) },
43 
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 
52 #ifdef GENL_MAGIC_DEBUG
53 static 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 
76 static 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 */
132 static 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 static inline int drbd_nla_check_mandatory(int maxtype, struct nlattr *nla)
146 {
147 	struct nlattr *head = nla_data(nla);
148 	int len = nla_len(nla);
149 	int rem;
150 
151 	/*
152 	 * validate_nla (called from nla_parse_nested) ignores attributes
153 	 * beyond maxtype, and does not understand the DRBD_GENLA_F_MANDATORY flag.
154 	 * In order to have it validate attributes with the DRBD_GENLA_F_MANDATORY
155 	 * flag set also, check and remove that flag before calling
156 	 * nla_parse_nested.
157 	 */
158 
159 	nla_for_each_attr(nla, head, len, rem) {
160 		if (nla->nla_type & DRBD_GENLA_F_MANDATORY) {
161 			nla->nla_type &= ~DRBD_GENLA_F_MANDATORY;
162 			if (nla_type(nla) > maxtype)
163 				return -EOPNOTSUPP;
164 		}
165 	}
166 	return 0;
167 }
168 
169 static inline int drbd_nla_parse_nested(struct nlattr *tb[], int maxtype,
170 					struct nlattr *nla,
171 					const struct nla_policy *policy)
172 {
173 	int err;
174 
175 	err = drbd_nla_check_mandatory(maxtype, nla);
176 	if (!err)
177 		err = nla_parse_nested(tb, maxtype, nla, policy);
178 
179 	return err;
180 }
181 
182 #undef GENL_struct
183 #define GENL_struct(tag_name, tag_number, s_name, s_fields)		\
184 /* *_from_attrs functions are static, but potentially unused */		\
185 static int __ ## s_name ## _from_attrs(struct s_name *s,		\
186 		struct genl_info *info, bool exclude_invariants)	\
187 {									\
188 	const int maxtype = ARRAY_SIZE(s_name ## _nl_policy)-1;		\
189 	struct nlattr *tla = info->attrs[tag_number];			\
190 	struct nlattr **ntb = nested_attr_tb;				\
191 	struct nlattr *nla;						\
192 	int err;							\
193 	BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb));	\
194 	if (!tla)							\
195 		return -ENOMSG;						\
196 	DPRINT_TLA(#s_name, "<=-", #tag_name);				\
197 	err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy);	\
198 	if (err)							\
199 		return err;						\
200 									\
201 	s_fields							\
202 	return 0;							\
203 }					__attribute__((unused))		\
204 static int s_name ## _from_attrs(struct s_name *s,			\
205 						struct genl_info *info)	\
206 {									\
207 	return __ ## s_name ## _from_attrs(s, info, false);		\
208 }					__attribute__((unused))		\
209 static int s_name ## _from_attrs_for_change(struct s_name *s,		\
210 						struct genl_info *info)	\
211 {									\
212 	return __ ## s_name ## _from_attrs(s, info, true);		\
213 }					__attribute__((unused))		\
214 
215 #define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...)	\
216 		nla = ntb[attr_nr];						\
217 		if (nla) {						\
218 			if (exclude_invariants && ((attr_flag) & DRBD_F_INVARIANT)) {		\
219 				pr_info("<< must not change invariant attr: %s\n", #name);	\
220 				return -EEXIST;				\
221 			}						\
222 			assignment;					\
223 		} else if (exclude_invariants && ((attr_flag) & DRBD_F_INVARIANT)) {		\
224 			/* attribute missing from payload, */		\
225 			/* which was expected */			\
226 		} else if ((attr_flag) & DRBD_F_REQUIRED) {		\
227 			pr_info("<< missing attr: %s\n", #name);	\
228 			return -ENOMSG;					\
229 		}
230 
231 #undef __field
232 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put,	\
233 		__is_signed)						\
234 	__assign(attr_nr, attr_flag, name, nla_type, type,		\
235 			if (s)						\
236 				s->name = __get(nla);			\
237 			DPRINT_FIELD("<<", nla_type, name, s, nla))
238 
239 /* validate_nla() already checked nla_len <= maxlen appropriately. */
240 #undef __array
241 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen,	\
242 		__get, __put, __is_signed)				\
243 	__assign(attr_nr, attr_flag, name, nla_type, type,		\
244 			if (s)						\
245 				s->name ## _len =			\
246 					__get(s->name, nla, maxlen);	\
247 			DPRINT_ARRAY("<<", nla_type, name, s, nla))
248 
249 #include GENL_MAGIC_INCLUDE_FILE
250 
251 #undef GENL_struct
252 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
253 
254 /*
255  * Magic: define op number to op name mapping				{{{1
256  *									{{{2
257  */
258 const char *CONCAT_(GENL_MAGIC_FAMILY, _genl_cmd_to_str)(__u8 cmd)
259 {
260 	switch (cmd) {
261 #undef GENL_op
262 #define GENL_op(op_name, op_num, handler, tla_list)		\
263 	case op_num: return #op_name;
264 #include GENL_MAGIC_INCLUDE_FILE
265 	default:
266 		     return "unknown";
267 	}
268 }
269 
270 #ifdef __KERNEL__
271 #include <linux/stringify.h>
272 /*
273  * Magic: define genl_ops						{{{1
274  *									{{{2
275  */
276 
277 #undef GENL_op
278 #define GENL_op(op_name, op_num, handler, tla_list)		\
279 {								\
280 	handler							\
281 	.cmd = op_name,						\
282 	.policy	= CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy),	\
283 },
284 
285 #define ZZZ_genl_ops		CONCAT_(GENL_MAGIC_FAMILY, _genl_ops)
286 static struct genl_ops ZZZ_genl_ops[] __read_mostly = {
287 #include GENL_MAGIC_INCLUDE_FILE
288 };
289 
290 #undef GENL_op
291 #define GENL_op(op_name, op_num, handler, tla_list)
292 
293 /*
294  * Define the genl_family, multicast groups,				{{{1
295  * and provide register/unregister functions.
296  *									{{{2
297  */
298 #define ZZZ_genl_family		CONCAT_(GENL_MAGIC_FAMILY, _genl_family)
299 static struct genl_family ZZZ_genl_family __read_mostly = {
300 	.id = GENL_ID_GENERATE,
301 	.name = __stringify(GENL_MAGIC_FAMILY),
302 	.version = GENL_MAGIC_VERSION,
303 #ifdef GENL_MAGIC_FAMILY_HDRSZ
304 	.hdrsize = NLA_ALIGN(GENL_MAGIC_FAMILY_HDRSZ),
305 #endif
306 	.maxattr = ARRAY_SIZE(drbd_tla_nl_policy)-1,
307 };
308 
309 /*
310  * Magic: define multicast groups
311  * Magic: define multicast group registration helper
312  */
313 #undef GENL_mc_group
314 #define GENL_mc_group(group)						\
315 static struct genl_multicast_group					\
316 CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group) __read_mostly = {		\
317 	.name = #group,							\
318 };									\
319 static int CONCAT_(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)(	\
320 	struct sk_buff *skb, gfp_t flags)				\
321 {									\
322 	unsigned int group_id =						\
323 		CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group).id;	\
324 	if (!group_id)							\
325 		return -EINVAL;						\
326 	return genlmsg_multicast(skb, 0, group_id, flags);		\
327 }
328 
329 #include GENL_MAGIC_INCLUDE_FILE
330 
331 int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void)
332 {
333 	int err = genl_register_family_with_ops(&ZZZ_genl_family,
334 		ZZZ_genl_ops, ARRAY_SIZE(ZZZ_genl_ops));
335 	if (err)
336 		return err;
337 #undef GENL_mc_group
338 #define GENL_mc_group(group)						\
339 	err = genl_register_mc_group(&ZZZ_genl_family,			\
340 		&CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group));		\
341 	if (err)							\
342 		goto fail;						\
343 	else								\
344 		pr_info("%s: mcg %s: %u\n", #group,			\
345 			__stringify(GENL_MAGIC_FAMILY),			\
346 			CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group).id);
347 
348 #include GENL_MAGIC_INCLUDE_FILE
349 
350 #undef GENL_mc_group
351 #define GENL_mc_group(group)
352 	return 0;
353 fail:
354 	genl_unregister_family(&ZZZ_genl_family);
355 	return err;
356 }
357 
358 void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void)
359 {
360 	genl_unregister_family(&ZZZ_genl_family);
361 }
362 
363 /*
364  * Magic: provide conversion functions					{{{1
365  * populate skb from struct.
366  *									{{{2
367  */
368 
369 #undef GENL_op
370 #define GENL_op(op_name, op_num, handler, tla_list)
371 
372 #undef GENL_struct
373 #define GENL_struct(tag_name, tag_number, s_name, s_fields)		\
374 static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s,	\
375 		const bool exclude_sensitive)				\
376 {									\
377 	struct nlattr *tla = nla_nest_start(skb, tag_number);		\
378 	if (!tla)							\
379 		goto nla_put_failure;					\
380 	DPRINT_TLA(#s_name, "-=>", #tag_name);				\
381 	s_fields							\
382 	nla_nest_end(skb, tla);						\
383 	return 0;							\
384 									\
385 nla_put_failure:							\
386 	if (tla)							\
387 		nla_nest_cancel(skb, tla);				\
388         return -EMSGSIZE;						\
389 }									\
390 static inline int s_name ## _to_priv_skb(struct sk_buff *skb,		\
391 		struct s_name *s)					\
392 {									\
393 	return s_name ## _to_skb(skb, s, 0);				\
394 }									\
395 static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb,		\
396 		struct s_name *s)					\
397 {									\
398 	return s_name ## _to_skb(skb, s, 1);				\
399 }
400 
401 
402 #undef __field
403 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put,	\
404 		__is_signed)						\
405 	if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) {	\
406 		DPRINT_FIELD(">>", nla_type, name, s, NULL);		\
407 		__put(skb, attr_nr, s->name);				\
408 	}
409 
410 #undef __array
411 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen,	\
412 		__get, __put, __is_signed)				\
413 	if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) {	\
414 		DPRINT_ARRAY(">>",nla_type, name, s, NULL);		\
415 		__put(skb, attr_nr, min_t(int, maxlen,			\
416 			s->name ## _len + (nla_type == NLA_NUL_STRING)),\
417 						s->name);		\
418 	}
419 
420 #include GENL_MAGIC_INCLUDE_FILE
421 
422 
423 /* Functions for initializing structs to default values.  */
424 
425 #undef __field
426 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put,	\
427 		__is_signed)
428 #undef __array
429 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen,	\
430 		__get, __put, __is_signed)
431 #undef __u32_field_def
432 #define __u32_field_def(attr_nr, attr_flag, name, default)		\
433 	x->name = default;
434 #undef __s32_field_def
435 #define __s32_field_def(attr_nr, attr_flag, name, default)		\
436 	x->name = default;
437 #undef __flg_field_def
438 #define __flg_field_def(attr_nr, attr_flag, name, default)		\
439 	x->name = default;
440 #undef __str_field_def
441 #define __str_field_def(attr_nr, attr_flag, name, maxlen)		\
442 	memset(x->name, 0, sizeof(x->name));				\
443 	x->name ## _len = 0;
444 #undef GENL_struct
445 #define GENL_struct(tag_name, tag_number, s_name, s_fields)		\
446 static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \
447 static void set_ ## s_name ## _defaults(struct s_name *x) {	\
448 s_fields								\
449 }
450 
451 #include GENL_MAGIC_INCLUDE_FILE
452 
453 #endif /* __KERNEL__ */
454 
455 /* }}}1 */
456 #endif /* GENL_MAGIC_FUNC_H */
457 /* vim: set foldmethod=marker foldlevel=1 nofoldenable : */
458