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  * @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  */
57 enum {
58 	GENLA_F_MAY_IGNORE	= 0,
59 	GENLA_F_MANDATORY	= 1 << 14,
60 	GENLA_F_REQUIRED	= 1 << 15,
61 
62 	/* 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.  */
68 	GENLA_F_SENSITIVE	= 1 << 16,
69 
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,
76 };
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) \
89 	__field(attr_nr, attr_flag, name, NLA_FLAG, char, \
90 			nla_get_flag, __nla_put_flag)
91 #define __u8_field(attr_nr, attr_flag, name)	\
92 	__field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
93 			nla_get_u8, NLA_PUT_U8)
94 #define __u16_field(attr_nr, attr_flag, name)	\
95 	__field(attr_nr, attr_flag, name, NLA_U16, __u16, \
96 			nla_get_u16, NLA_PUT_U16)
97 #define __u32_field(attr_nr, attr_flag, name)	\
98 	__field(attr_nr, attr_flag, name, NLA_U32, __u32, \
99 			nla_get_u32, NLA_PUT_U32)
100 #define __s32_field(attr_nr, attr_flag, name)	\
101 	__field(attr_nr, attr_flag, name, NLA_U32, __s32, \
102 			nla_get_u32, NLA_PUT_U32)
103 #define __u64_field(attr_nr, attr_flag, name)	\
104 	__field(attr_nr, attr_flag, name, NLA_U64, __u64, \
105 			nla_get_u64, NLA_PUT_U64)
106 #define __str_field(attr_nr, attr_flag, name, maxlen) \
107 	__array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
108 			nla_strlcpy, NLA_PUT)
109 #define __bin_field(attr_nr, attr_flag, name, maxlen) \
110 	__array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
111 			nla_memcpy, NLA_PUT)
112 
113 /* 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)
118 #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
119 	__str_field(attr_nr, attr_flag, name, maxlen)
120 
121 #define __nla_put_flag(skb, attrtype, value)		\
122 	do {						\
123 		if (value)				\
124 			NLA_PUT_FLAG(skb, attrtype);	\
125 	} while (0)
126 
127 #define GENL_op_init(args...)	args
128 #define GENL_doit(handler)		\
129 	.doit = handler,		\
130 	.flags = GENL_ADMIN_PERM,
131 #define GENL_dumpit(handler)		\
132 	.dumpit = handler,		\
133 	.flags = GENL_ADMIN_PERM,
134 
135 /*									}}}1
136  * Magic: define the enum symbols for genl_ops
137  * Magic: define the enum symbols for top level attributes
138  * Magic: define the enum symbols for nested attributes
139  *									{{{2
140  */
141 
142 #undef GENL_struct
143 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
144 
145 #undef GENL_mc_group
146 #define GENL_mc_group(group)
147 
148 #undef GENL_notification
149 #define GENL_notification(op_name, op_num, mcast_group, tla_list)	\
150 	op_name = op_num,
151 
152 #undef GENL_op
153 #define GENL_op(op_name, op_num, handler, tla_list)			\
154 	op_name = op_num,
155 
156 enum {
157 #include GENL_MAGIC_INCLUDE_FILE
158 };
159 
160 #undef GENL_notification
161 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
162 
163 #undef GENL_op
164 #define GENL_op(op_name, op_num, handler, attr_list)
165 
166 #undef GENL_struct
167 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
168 		tag_name = tag_number,
169 
170 enum {
171 #include GENL_MAGIC_INCLUDE_FILE
172 };
173 
174 #undef GENL_struct
175 #define GENL_struct(tag_name, tag_number, s_name, s_fields)	\
176 enum {								\
177 	s_fields						\
178 };
179 
180 #undef __field
181 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put)	\
182 	T_ ## name = (__u16)(attr_nr | attr_flag),
183 
184 #undef __array
185 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
186 	T_ ## name = (__u16)(attr_nr | attr_flag),
187 
188 #include GENL_MAGIC_INCLUDE_FILE
189 
190 /*									}}}1
191  * Magic: compile time assert unique numbers for operations
192  * Magic: -"- unique numbers for top level attributes
193  * Magic: -"- unique numbers for nested attributes
194  *									{{{2
195  */
196 
197 #undef GENL_struct
198 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
199 
200 #undef GENL_op
201 #define GENL_op(op_name, op_num, handler, attr_list)	\
202 	case op_name:
203 
204 #undef GENL_notification
205 #define GENL_notification(op_name, op_num, mcast_group, tla_list)	\
206 	case op_name:
207 
208 static inline void ct_assert_unique_operations(void)
209 {
210 	switch (0) {
211 #include GENL_MAGIC_INCLUDE_FILE
212 		;
213 	}
214 }
215 
216 #undef GENL_op
217 #define GENL_op(op_name, op_num, handler, attr_list)
218 
219 #undef GENL_notification
220 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
221 
222 #undef GENL_struct
223 #define GENL_struct(tag_name, tag_number, s_name, s_fields)		\
224 		case tag_number:
225 
226 static inline void ct_assert_unique_top_level_attributes(void)
227 {
228 	switch (0) {
229 #include GENL_MAGIC_INCLUDE_FILE
230 		;
231 	}
232 }
233 
234 #undef GENL_struct
235 #define GENL_struct(tag_name, tag_number, s_name, s_fields)		\
236 static inline void ct_assert_unique_ ## s_name ## _attributes(void)	\
237 {									\
238 	switch (0) {							\
239 		s_fields						\
240 			;						\
241 	}								\
242 }
243 
244 #undef __field
245 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put)	\
246 	case attr_nr:
247 
248 #undef __array
249 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
250 	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)		\
264 struct s_name { s_fields };
265 
266 #undef __field
267 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put) \
268 	type name;
269 
270 #undef __array
271 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
272 	type name[maxlen];	\
273 	__u32 name ## _len;
274 
275 #include GENL_MAGIC_INCLUDE_FILE
276 
277 /* }}}1 */
278 #endif /* GENL_MAGIC_STRUCT_H */
279 /* vim: set foldmethod=marker nofoldenable : */
280