1 #ifndef GENL_MAGIC_STRUCT_H
2 #define GENL_MAGIC_STRUCT_H
3 
4 #ifndef GENL_MAGIC_FAMILY
5 # error "you need to define GENL_MAGIC_FAMILY before inclusion"
6 #endif
7 
8 #ifndef GENL_MAGIC_VERSION
9 # error "you need to define GENL_MAGIC_VERSION before inclusion"
10 #endif
11 
12 #ifndef GENL_MAGIC_INCLUDE_FILE
13 # error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion"
14 #endif
15 
16 #include <linux/genetlink.h>
17 #include <linux/types.h>
18 
19 #define CONCAT__(a,b)	a ## b
20 #define CONCAT_(a,b)	CONCAT__(a,b)
21 
22 extern int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void);
23 extern void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void);
24 
25 /*
26  * Extension of genl attribute validation policies			{{{2
27  */
28 
29 /**
30  * GENLA_F_FLAGS - policy type flags to ease compatible ABI evolvement
31  *
32  * @GENLA_F_REQUIRED: attribute has to be present, or message is considered invalid.
33  * Adding new REQUIRED attributes breaks ABI compatibility, so don't do that.
34  *
35  * @GENLA_F_MANDATORY: if present, receiver _must_ understand it.
36  * Without this, unknown attributes (> maxtype) are _silently_ ignored
37  * by validate_nla().
38  *
39  * To be used for API extensions, so older kernel can reject requests for not
40  * yet implemented features, if newer userland tries to use them even though
41  * the genl_family version clearly indicates they are not available.
42  *
43  * NOTE: These flags overload
44  *   NLA_F_NESTED		(1 << 15)
45  *   NLA_F_NET_BYTEORDER	(1 << 14)
46  * from linux/netlink.h, which are not useful for validate_nla():
47  * NET_BYTEORDER is not used anywhere, and NESTED would be specified by setting
48  * .type = NLA_NESTED in the appropriate policy.
49  *
50  * See also: nla_type()
51  */
52 enum {
53 	GENLA_F_MANDATORY	= 1 << 14,
54 	GENLA_F_REQUIRED	= 1 << 15,
55 
56 	/* Below will not be present in the __u16 .nla_type, but can be
57 	 * triggered on in <struct>_to_skb resp. <struct>_from_attrs */
58 
59 	/* To exclude "sensitive" information from broadcasts, or on
60 	 * unpriviledged get requests.  This is useful because genetlink
61 	 * multicast groups can be listened in on by anyone.  */
62 	GENLA_F_SENSITIVE	= 1 << 16,
63 
64 	/* INVARIAN options cannot be changed at runtime.
65 	 * Useful to share an attribute policy and struct definition,
66 	 * between some "create" and "change" commands,
67 	 * but disallow certain fields to be changed online.
68 	 */
69 	GENLA_F_INVARIANT	= 1 << 17,
70 };
71 
72 #define __nla_type(x)	((__u16)((__u16)(x) & (__u16)NLA_TYPE_MASK))
73 
74 /*									}}}1
75  * MAGIC
76  * multi-include macro expansion magic starts here
77  */
78 
79 /* MAGIC helpers							{{{2 */
80 
81 /* possible field types */
82 #define __flg_field(attr_nr, attr_flag, name) \
83 	__field(attr_nr, attr_flag, name, NLA_U8, char, \
84 			nla_get_u8, NLA_PUT_U8, false)
85 #define __u8_field(attr_nr, attr_flag, name)	\
86 	__field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
87 			nla_get_u8, NLA_PUT_U8, false)
88 #define __u16_field(attr_nr, attr_flag, name)	\
89 	__field(attr_nr, attr_flag, name, NLA_U16, __u16, \
90 			nla_get_u16, NLA_PUT_U16, false)
91 #define __u32_field(attr_nr, attr_flag, name)	\
92 	__field(attr_nr, attr_flag, name, NLA_U32, __u32, \
93 			nla_get_u32, NLA_PUT_U32, false)
94 #define __s32_field(attr_nr, attr_flag, name)	\
95 	__field(attr_nr, attr_flag, name, NLA_U32, __s32, \
96 			nla_get_u32, NLA_PUT_U32, true)
97 #define __u64_field(attr_nr, attr_flag, name)	\
98 	__field(attr_nr, attr_flag, name, NLA_U64, __u64, \
99 			nla_get_u64, NLA_PUT_U64, false)
100 #define __str_field(attr_nr, attr_flag, name, maxlen) \
101 	__array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
102 			nla_strlcpy, NLA_PUT, false)
103 #define __bin_field(attr_nr, attr_flag, name, maxlen) \
104 	__array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
105 			nla_memcpy, NLA_PUT, false)
106 
107 /* fields with default values */
108 #define __flg_field_def(attr_nr, attr_flag, name, default) \
109 	__flg_field(attr_nr, attr_flag, name)
110 #define __u32_field_def(attr_nr, attr_flag, name, default) \
111 	__u32_field(attr_nr, attr_flag, name)
112 #define __s32_field_def(attr_nr, attr_flag, name, default) \
113 	__s32_field(attr_nr, attr_flag, name)
114 #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
115 	__str_field(attr_nr, attr_flag, name, maxlen)
116 
117 #define GENL_op_init(args...)	args
118 #define GENL_doit(handler)		\
119 	.doit = handler,		\
120 	.flags = GENL_ADMIN_PERM,
121 #define GENL_dumpit(handler)		\
122 	.dumpit = handler,		\
123 	.flags = GENL_ADMIN_PERM,
124 
125 /*									}}}1
126  * Magic: define the enum symbols for genl_ops
127  * Magic: define the enum symbols for top level attributes
128  * Magic: define the enum symbols for nested attributes
129  *									{{{2
130  */
131 
132 #undef GENL_struct
133 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
134 
135 #undef GENL_mc_group
136 #define GENL_mc_group(group)
137 
138 #undef GENL_notification
139 #define GENL_notification(op_name, op_num, mcast_group, tla_list)	\
140 	op_name = op_num,
141 
142 #undef GENL_op
143 #define GENL_op(op_name, op_num, handler, tla_list)			\
144 	op_name = op_num,
145 
146 enum {
147 #include GENL_MAGIC_INCLUDE_FILE
148 };
149 
150 #undef GENL_notification
151 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
152 
153 #undef GENL_op
154 #define GENL_op(op_name, op_num, handler, attr_list)
155 
156 #undef GENL_struct
157 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
158 		tag_name = tag_number,
159 
160 enum {
161 #include GENL_MAGIC_INCLUDE_FILE
162 };
163 
164 #undef GENL_struct
165 #define GENL_struct(tag_name, tag_number, s_name, s_fields)	\
166 enum {								\
167 	s_fields						\
168 };
169 
170 #undef __field
171 #define __field(attr_nr, attr_flag, name, nla_type, type,	\
172 		__get, __put, __is_signed)			\
173 	T_ ## name = (__u16)(attr_nr | attr_flag),
174 
175 #undef __array
176 #define __array(attr_nr, attr_flag, name, nla_type, type,	\
177 		maxlen, __get, __put, __is_signed)		\
178 	T_ ## name = (__u16)(attr_nr | attr_flag),
179 
180 #include GENL_MAGIC_INCLUDE_FILE
181 
182 /*									}}}1
183  * Magic: compile time assert unique numbers for operations
184  * Magic: -"- unique numbers for top level attributes
185  * Magic: -"- unique numbers for nested attributes
186  *									{{{2
187  */
188 
189 #undef GENL_struct
190 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
191 
192 #undef GENL_op
193 #define GENL_op(op_name, op_num, handler, attr_list)	\
194 	case op_name:
195 
196 #undef GENL_notification
197 #define GENL_notification(op_name, op_num, mcast_group, tla_list)	\
198 	case op_name:
199 
200 static inline void ct_assert_unique_operations(void)
201 {
202 	switch (0) {
203 #include GENL_MAGIC_INCLUDE_FILE
204 		;
205 	}
206 }
207 
208 #undef GENL_op
209 #define GENL_op(op_name, op_num, handler, attr_list)
210 
211 #undef GENL_notification
212 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
213 
214 #undef GENL_struct
215 #define GENL_struct(tag_name, tag_number, s_name, s_fields)		\
216 		case tag_number:
217 
218 static inline void ct_assert_unique_top_level_attributes(void)
219 {
220 	switch (0) {
221 #include GENL_MAGIC_INCLUDE_FILE
222 		;
223 	}
224 }
225 
226 #undef GENL_struct
227 #define GENL_struct(tag_name, tag_number, s_name, s_fields)		\
228 static inline void ct_assert_unique_ ## s_name ## _attributes(void)	\
229 {									\
230 	switch (0) {							\
231 		s_fields						\
232 			;						\
233 	}								\
234 }
235 
236 #undef __field
237 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put,	\
238 		__is_signed)						\
239 	case attr_nr:
240 
241 #undef __array
242 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen,	\
243 		__get, __put, __is_signed)				\
244 	case attr_nr:
245 
246 #include GENL_MAGIC_INCLUDE_FILE
247 
248 /*									}}}1
249  * Magic: declare structs
250  * struct <name> {
251  *	fields
252  * };
253  *									{{{2
254  */
255 
256 #undef GENL_struct
257 #define GENL_struct(tag_name, tag_number, s_name, s_fields)		\
258 struct s_name { s_fields };
259 
260 #undef __field
261 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put,	\
262 		__is_signed)						\
263 	type name;
264 
265 #undef __array
266 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen,	\
267 		__get, __put, __is_signed)				\
268 	type name[maxlen];	\
269 	__u32 name ## _len;
270 
271 #include GENL_MAGIC_INCLUDE_FILE
272 
273 #undef GENL_struct
274 #define GENL_struct(tag_name, tag_number, s_name, s_fields)		\
275 enum {									\
276 	s_fields							\
277 };
278 
279 #undef __field
280 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put,	\
281 		is_signed)						\
282 	F_ ## name ## _IS_SIGNED = is_signed,
283 
284 #undef __array
285 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen,	\
286 		__get, __put, is_signed)				\
287 	F_ ## name ## _IS_SIGNED = is_signed,
288 
289 #include GENL_MAGIC_INCLUDE_FILE
290 
291 /* }}}1 */
292 #endif /* GENL_MAGIC_STRUCT_H */
293 /* vim: set foldmethod=marker nofoldenable : */
294