blob: ba911da84d9ff4c29bba473c2bbc8a44989754c3 [file] [log] [blame]
Lars Ellenbergec2c35a2011-03-07 10:20:08 +01001#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
22extern int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void);
23extern 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 * @GENLA_F_MAY_IGNORE: To clearly document the fact, for good measure.
44 * To be used for API extensions for things that have sane defaults,
45 * so newer userland can still talk to older kernel, knowing it will
46 * silently ignore these attributes if not yet known.
47 *
48 * NOTE: These flags overload
49 * NLA_F_NESTED (1 << 15)
50 * NLA_F_NET_BYTEORDER (1 << 14)
51 * from linux/netlink.h, which are not useful for validate_nla():
52 * NET_BYTEORDER is not used anywhere, and NESTED would be specified by setting
53 * .type = NLA_NESTED in the appropriate policy.
54 *
55 * See also: nla_type()
56 */
57enum {
58 GENLA_F_MAY_IGNORE = 0,
59 GENLA_F_MANDATORY = 1 << 14,
60 GENLA_F_REQUIRED = 1 << 15,
61
Lars Ellenbergf3990022011-03-23 14:31:09 +010062 /* Below will not be present in the __u16 .nla_type, but can be
63 * triggered on in <struct>_to_skb resp. <struct>_from_attrs */
64
65 /* To exclude "sensitive" information from broadcasts, or on
66 * unpriviledged get requests. This is useful because genetlink
67 * multicast groups can be listened in on by anyone. */
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010068 GENLA_F_SENSITIVE = 1 << 16,
Lars Ellenbergf3990022011-03-23 14:31:09 +010069
70 /* INVARIAN options cannot be changed at runtime.
71 * Useful to share an attribute policy and struct definition,
72 * between some "create" and "change" commands,
73 * but disallow certain fields to be changed online.
74 */
75 GENLA_F_INVARIANT = 1 << 17,
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010076};
77
78#define __nla_type(x) ((__u16)((__u16)(x) & (__u16)NLA_TYPE_MASK))
79
80/* }}}1
81 * MAGIC
82 * multi-include macro expansion magic starts here
83 */
84
85/* MAGIC helpers {{{2 */
86
87/* possible field types */
88#define __flg_field(attr_nr, attr_flag, name) \
Andreas Gruenbachera5d8e1f2011-05-04 16:06:51 +020089 __field(attr_nr, attr_flag, name, NLA_U8, char, \
Andreas Gruenbacher509100e2011-05-17 13:29:46 +020090 nla_get_u8, NLA_PUT_U8, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010091#define __u8_field(attr_nr, attr_flag, name) \
92 __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
Andreas Gruenbacher509100e2011-05-17 13:29:46 +020093 nla_get_u8, NLA_PUT_U8, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010094#define __u16_field(attr_nr, attr_flag, name) \
95 __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
Andreas Gruenbacher509100e2011-05-17 13:29:46 +020096 nla_get_u16, NLA_PUT_U16, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010097#define __u32_field(attr_nr, attr_flag, name) \
98 __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
Andreas Gruenbacher509100e2011-05-17 13:29:46 +020099 nla_get_u32, NLA_PUT_U32, false)
Lars Ellenberg563e4cf2011-05-04 10:33:52 +0200100#define __s32_field(attr_nr, attr_flag, name) \
101 __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200102 nla_get_u32, NLA_PUT_U32, true)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100103#define __u64_field(attr_nr, attr_flag, name) \
104 __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200105 nla_get_u64, NLA_PUT_U64, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100106#define __str_field(attr_nr, attr_flag, name, maxlen) \
107 __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200108 nla_strlcpy, NLA_PUT, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100109#define __bin_field(attr_nr, attr_flag, name, maxlen) \
110 __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200111 nla_memcpy, NLA_PUT, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100112
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +0200113/* fields with default values */
114#define __flg_field_def(attr_nr, attr_flag, name, default) \
115 __flg_field(attr_nr, attr_flag, name)
116#define __u32_field_def(attr_nr, attr_flag, name, default) \
117 __u32_field(attr_nr, attr_flag, name)
Andreas Gruenbacher3a45abd2011-05-12 12:02:54 +0200118#define __s32_field_def(attr_nr, attr_flag, name, default) \
119 __s32_field(attr_nr, attr_flag, name)
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +0200120#define __str_field_def(attr_nr, attr_flag, name, maxlen) \
121 __str_field(attr_nr, attr_flag, name, maxlen)
122
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100123#define GENL_op_init(args...) args
124#define GENL_doit(handler) \
125 .doit = handler, \
126 .flags = GENL_ADMIN_PERM,
127#define GENL_dumpit(handler) \
128 .dumpit = handler, \
129 .flags = GENL_ADMIN_PERM,
130
131/* }}}1
132 * Magic: define the enum symbols for genl_ops
133 * Magic: define the enum symbols for top level attributes
134 * Magic: define the enum symbols for nested attributes
135 * {{{2
136 */
137
138#undef GENL_struct
139#define GENL_struct(tag_name, tag_number, s_name, s_fields)
140
141#undef GENL_mc_group
142#define GENL_mc_group(group)
143
144#undef GENL_notification
145#define GENL_notification(op_name, op_num, mcast_group, tla_list) \
146 op_name = op_num,
147
148#undef GENL_op
149#define GENL_op(op_name, op_num, handler, tla_list) \
150 op_name = op_num,
151
152enum {
153#include GENL_MAGIC_INCLUDE_FILE
154};
155
156#undef GENL_notification
157#define GENL_notification(op_name, op_num, mcast_group, tla_list)
158
159#undef GENL_op
160#define GENL_op(op_name, op_num, handler, attr_list)
161
162#undef GENL_struct
163#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
164 tag_name = tag_number,
165
166enum {
167#include GENL_MAGIC_INCLUDE_FILE
168};
169
170#undef GENL_struct
171#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
172enum { \
173 s_fields \
174};
175
176#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200177#define __field(attr_nr, attr_flag, name, nla_type, type, \
178 __get, __put, __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100179 T_ ## name = (__u16)(attr_nr | attr_flag),
180
181#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200182#define __array(attr_nr, attr_flag, name, nla_type, type, \
183 maxlen, __get, __put, __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100184 T_ ## name = (__u16)(attr_nr | attr_flag),
185
186#include GENL_MAGIC_INCLUDE_FILE
187
188/* }}}1
189 * Magic: compile time assert unique numbers for operations
190 * Magic: -"- unique numbers for top level attributes
191 * Magic: -"- unique numbers for nested attributes
192 * {{{2
193 */
194
195#undef GENL_struct
196#define GENL_struct(tag_name, tag_number, s_name, s_fields)
197
198#undef GENL_op
199#define GENL_op(op_name, op_num, handler, attr_list) \
200 case op_name:
201
202#undef GENL_notification
203#define GENL_notification(op_name, op_num, mcast_group, tla_list) \
204 case op_name:
205
206static inline void ct_assert_unique_operations(void)
207{
208 switch (0) {
209#include GENL_MAGIC_INCLUDE_FILE
210 ;
211 }
212}
213
214#undef GENL_op
215#define GENL_op(op_name, op_num, handler, attr_list)
216
217#undef GENL_notification
218#define GENL_notification(op_name, op_num, mcast_group, tla_list)
219
220#undef GENL_struct
221#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
222 case tag_number:
223
224static inline void ct_assert_unique_top_level_attributes(void)
225{
226 switch (0) {
227#include GENL_MAGIC_INCLUDE_FILE
228 ;
229 }
230}
231
232#undef GENL_struct
233#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
234static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
235{ \
236 switch (0) { \
237 s_fields \
238 ; \
239 } \
240}
241
242#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200243#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
244 __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100245 case attr_nr:
246
247#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200248#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
249 __get, __put, __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100250 case attr_nr:
251
252#include GENL_MAGIC_INCLUDE_FILE
253
254/* }}}1
255 * Magic: declare structs
256 * struct <name> {
257 * fields
258 * };
259 * {{{2
260 */
261
262#undef GENL_struct
263#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
264struct s_name { s_fields };
265
266#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200267#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
268 __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100269 type name;
270
271#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200272#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
273 __get, __put, __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100274 type name[maxlen]; \
275 __u32 name ## _len;
276
277#include GENL_MAGIC_INCLUDE_FILE
278
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200279#undef GENL_struct
280#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
281enum { \
282 s_fields \
283};
284
285#undef __field
286#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
287 is_signed) \
288 F_ ## name ## _IS_SIGNED = is_signed,
289
290#undef __array
291#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
292 __get, __put, is_signed) \
293 F_ ## name ## _IS_SIGNED = is_signed,
294
295#include GENL_MAGIC_INCLUDE_FILE
296
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100297/* }}}1 */
298#endif /* GENL_MAGIC_STRUCT_H */
299/* vim: set foldmethod=marker nofoldenable : */