blob: eeae59d3ceb7401af5badf6297ee2fad3a388fd7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Lars Ellenbergec2c35a2011-03-07 10:20:08 +01002#ifndef GENL_MAGIC_STRUCT_H
3#define GENL_MAGIC_STRUCT_H
4
5#ifndef GENL_MAGIC_FAMILY
6# error "you need to define GENL_MAGIC_FAMILY before inclusion"
7#endif
8
9#ifndef GENL_MAGIC_VERSION
10# error "you need to define GENL_MAGIC_VERSION before inclusion"
11#endif
12
13#ifndef GENL_MAGIC_INCLUDE_FILE
14# error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion"
15#endif
16
17#include <linux/genetlink.h>
18#include <linux/types.h>
19
20#define CONCAT__(a,b) a ## b
21#define CONCAT_(a,b) CONCAT__(a,b)
22
23extern int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void);
24extern void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void);
25
26/*
27 * Extension of genl attribute validation policies {{{2
28 */
29
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020030/*
31 * @DRBD_GENLA_F_MANDATORY: By default, netlink ignores attributes it does not
32 * know about. This flag can be set in nlattr->nla_type to indicate that this
33 * attribute must not be ignored.
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010034 *
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020035 * We check and remove this flag in drbd_nla_check_mandatory() before
36 * validating the attribute types and lengths via nla_parse_nested().
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010037 */
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020038#define DRBD_GENLA_F_MANDATORY (1 << 14)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010039
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020040/*
41 * Flags specific to drbd and not visible at the netlink layer, used in
42 * <struct>_from_attrs and <struct>_to_skb:
43 *
44 * @DRBD_F_REQUIRED: Attribute is required; a request without this attribute is
45 * invalid.
46 *
47 * @DRBD_F_SENSITIVE: Attribute includes sensitive information and must not be
48 * included in unpriviledged get requests or broadcasts.
49 *
50 * @DRBD_F_INVARIANT: Attribute is set when an object is initially created, but
51 * cannot subsequently be changed.
52 */
53#define DRBD_F_REQUIRED (1 << 0)
54#define DRBD_F_SENSITIVE (1 << 1)
55#define DRBD_F_INVARIANT (1 << 2)
Lars Ellenbergf3990022011-03-23 14:31:09 +010056
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020057#define __nla_type(x) ((__u16)((x) & NLA_TYPE_MASK & ~DRBD_GENLA_F_MANDATORY))
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010058
59/* }}}1
60 * MAGIC
61 * multi-include macro expansion magic starts here
62 */
63
64/* MAGIC helpers {{{2 */
65
Nicolas Dichtel1dee3f52016-05-09 11:40:20 +020066static inline int nla_put_u64_0pad(struct sk_buff *skb, int attrtype, u64 value)
67{
68 return nla_put_64bit(skb, attrtype, sizeof(u64), &value, 0);
69}
70
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010071/* possible field types */
72#define __flg_field(attr_nr, attr_flag, name) \
Andreas Gruenbachera5d8e1f2011-05-04 16:06:51 +020073 __field(attr_nr, attr_flag, name, NLA_U8, char, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020074 nla_get_u8, nla_put_u8, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010075#define __u8_field(attr_nr, attr_flag, name) \
76 __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020077 nla_get_u8, nla_put_u8, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010078#define __u16_field(attr_nr, attr_flag, name) \
79 __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020080 nla_get_u16, nla_put_u16, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010081#define __u32_field(attr_nr, attr_flag, name) \
82 __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020083 nla_get_u32, nla_put_u32, false)
Lars Ellenberg563e4cf2011-05-04 10:33:52 +020084#define __s32_field(attr_nr, attr_flag, name) \
85 __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020086 nla_get_u32, nla_put_u32, true)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010087#define __u64_field(attr_nr, attr_flag, name) \
88 __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
Nicolas Dichtel1dee3f52016-05-09 11:40:20 +020089 nla_get_u64, nla_put_u64_0pad, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010090#define __str_field(attr_nr, attr_flag, name, maxlen) \
91 __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020092 nla_strlcpy, nla_put, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010093#define __bin_field(attr_nr, attr_flag, name, maxlen) \
94 __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020095 nla_memcpy, nla_put, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010096
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +020097/* fields with default values */
98#define __flg_field_def(attr_nr, attr_flag, name, default) \
99 __flg_field(attr_nr, attr_flag, name)
100#define __u32_field_def(attr_nr, attr_flag, name, default) \
101 __u32_field(attr_nr, attr_flag, name)
Andreas Gruenbacher3a45abd2011-05-12 12:02:54 +0200102#define __s32_field_def(attr_nr, attr_flag, name, default) \
103 __s32_field(attr_nr, attr_flag, name)
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +0200104#define __str_field_def(attr_nr, attr_flag, name, maxlen) \
105 __str_field(attr_nr, attr_flag, name, maxlen)
106
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100107#define GENL_op_init(args...) args
108#define GENL_doit(handler) \
109 .doit = handler, \
110 .flags = GENL_ADMIN_PERM,
111#define GENL_dumpit(handler) \
112 .dumpit = handler, \
113 .flags = GENL_ADMIN_PERM,
114
115/* }}}1
116 * Magic: define the enum symbols for genl_ops
117 * Magic: define the enum symbols for top level attributes
118 * Magic: define the enum symbols for nested attributes
119 * {{{2
120 */
121
122#undef GENL_struct
123#define GENL_struct(tag_name, tag_number, s_name, s_fields)
124
125#undef GENL_mc_group
126#define GENL_mc_group(group)
127
128#undef GENL_notification
129#define GENL_notification(op_name, op_num, mcast_group, tla_list) \
130 op_name = op_num,
131
132#undef GENL_op
133#define GENL_op(op_name, op_num, handler, tla_list) \
134 op_name = op_num,
135
136enum {
137#include GENL_MAGIC_INCLUDE_FILE
138};
139
140#undef GENL_notification
141#define GENL_notification(op_name, op_num, mcast_group, tla_list)
142
143#undef GENL_op
144#define GENL_op(op_name, op_num, handler, attr_list)
145
146#undef GENL_struct
147#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
148 tag_name = tag_number,
149
150enum {
151#include GENL_MAGIC_INCLUDE_FILE
152};
153
154#undef GENL_struct
155#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
156enum { \
157 s_fields \
158};
159
160#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200161#define __field(attr_nr, attr_flag, name, nla_type, type, \
162 __get, __put, __is_signed) \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +0200163 T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100164
165#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200166#define __array(attr_nr, attr_flag, name, nla_type, type, \
167 maxlen, __get, __put, __is_signed) \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +0200168 T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100169
170#include GENL_MAGIC_INCLUDE_FILE
171
172/* }}}1
173 * Magic: compile time assert unique numbers for operations
174 * Magic: -"- unique numbers for top level attributes
175 * Magic: -"- unique numbers for nested attributes
176 * {{{2
177 */
178
179#undef GENL_struct
180#define GENL_struct(tag_name, tag_number, s_name, s_fields)
181
182#undef GENL_op
183#define GENL_op(op_name, op_num, handler, attr_list) \
184 case op_name:
185
186#undef GENL_notification
187#define GENL_notification(op_name, op_num, mcast_group, tla_list) \
188 case op_name:
189
190static inline void ct_assert_unique_operations(void)
191{
192 switch (0) {
193#include GENL_MAGIC_INCLUDE_FILE
Nathan Chancellora52c5a12018-12-20 17:23:43 +0100194 case 0:
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100195 ;
196 }
197}
198
199#undef GENL_op
200#define GENL_op(op_name, op_num, handler, attr_list)
201
202#undef GENL_notification
203#define GENL_notification(op_name, op_num, mcast_group, tla_list)
204
205#undef GENL_struct
206#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
207 case tag_number:
208
209static inline void ct_assert_unique_top_level_attributes(void)
210{
211 switch (0) {
212#include GENL_MAGIC_INCLUDE_FILE
Nathan Chancellora52c5a12018-12-20 17:23:43 +0100213 case 0:
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100214 ;
215 }
216}
217
218#undef GENL_struct
219#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
220static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
221{ \
222 switch (0) { \
Nathan Chancellora52c5a12018-12-20 17:23:43 +0100223 s_fields \
224 case 0: \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100225 ; \
226 } \
227}
228
229#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200230#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
231 __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100232 case attr_nr:
233
234#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200235#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
236 __get, __put, __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100237 case attr_nr:
238
239#include GENL_MAGIC_INCLUDE_FILE
240
241/* }}}1
242 * Magic: declare structs
243 * struct <name> {
244 * fields
245 * };
246 * {{{2
247 */
248
249#undef GENL_struct
250#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
251struct s_name { s_fields };
252
253#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200254#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
255 __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100256 type name;
257
258#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200259#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
260 __get, __put, __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100261 type name[maxlen]; \
262 __u32 name ## _len;
263
264#include GENL_MAGIC_INCLUDE_FILE
265
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200266#undef GENL_struct
267#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
268enum { \
269 s_fields \
270};
271
272#undef __field
273#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
274 is_signed) \
275 F_ ## name ## _IS_SIGNED = is_signed,
276
277#undef __array
278#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
279 __get, __put, is_signed) \
280 F_ ## name ## _IS_SIGNED = is_signed,
281
282#include GENL_MAGIC_INCLUDE_FILE
283
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100284/* }}}1 */
285#endif /* GENL_MAGIC_STRUCT_H */
286/* vim: set foldmethod=marker nofoldenable : */