]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
482a8524 TG |
2 | #ifndef __NET_GENERIC_NETLINK_H |
3 | #define __NET_GENERIC_NETLINK_H | |
4 | ||
5 | #include <linux/genetlink.h> | |
6 | #include <net/netlink.h> | |
134e6375 | 7 | #include <net/net_namespace.h> |
482a8524 | 8 | |
58050fce TG |
9 | #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN) |
10 | ||
2dbba6f7 JB |
11 | /** |
12 | * struct genl_multicast_group - generic netlink multicast group | |
13 | * @name: name of the multicast group, names are per-family | |
2dbba6f7 | 14 | */ |
fd2c3ef7 | 15 | struct genl_multicast_group { |
2dbba6f7 | 16 | char name[GENL_NAMSIZ]; |
2dbba6f7 JB |
17 | }; |
18 | ||
ff4c92d8 JB |
19 | struct genl_ops; |
20 | struct genl_info; | |
21 | ||
482a8524 TG |
22 | /** |
23 | * struct genl_family - generic netlink family | |
a07ea4d9 | 24 | * @id: protocol family identifier (private) |
482a8524 TG |
25 | * @hdrsize: length of user specific header in bytes |
26 | * @name: name of family | |
27 | * @version: protocol version | |
28 | * @maxattr: maximum number of attributes supported | |
3b0f31f2 | 29 | * @policy: netlink policy |
134e6375 JB |
30 | * @netnsok: set to true if the family can handle network |
31 | * namespaces and should be presented in all of them | |
f555f3d7 JB |
32 | * @parallel_ops: operations can be called in parallel and aren't |
33 | * synchronized by the core genetlink code | |
ff4c92d8 JB |
34 | * @pre_doit: called before an operation's doit callback, it may |
35 | * do additional, common, filtering and return an error | |
36 | * @post_doit: called after an operation's doit callback, it may | |
37 | * undo operations done by pre_doit, for example release locks | |
489111e5 JB |
38 | * @mcgrps: multicast groups used by this family |
39 | * @n_mcgrps: number of multicast groups | |
2a94fe48 | 40 | * @mcgrp_offset: starting number of multicast group IDs in this family |
489111e5 JB |
41 | * (private) |
42 | * @ops: the operations supported by this family | |
43 | * @n_ops: number of operations supported by this family | |
0b588afd JK |
44 | * @small_ops: the small-struct operations supported by this family |
45 | * @n_small_ops: number of small-struct operations supported by this family | |
482a8524 | 46 | */ |
fd2c3ef7 | 47 | struct genl_family { |
98e4321b | 48 | int id; /* private */ |
482a8524 TG |
49 | unsigned int hdrsize; |
50 | char name[GENL_NAMSIZ]; | |
51 | unsigned int version; | |
52 | unsigned int maxattr; | |
e5086736 JK |
53 | unsigned int mcgrp_offset; /* private */ |
54 | u8 netnsok:1; | |
55 | u8 parallel_ops:1; | |
56 | u8 n_ops; | |
0b588afd | 57 | u8 n_small_ops; |
e5086736 | 58 | u8 n_mcgrps; |
3b0f31f2 | 59 | const struct nla_policy *policy; |
f84f771d | 60 | int (*pre_doit)(const struct genl_ops *ops, |
ff4c92d8 JB |
61 | struct sk_buff *skb, |
62 | struct genl_info *info); | |
f84f771d | 63 | void (*post_doit)(const struct genl_ops *ops, |
ff4c92d8 JB |
64 | struct sk_buff *skb, |
65 | struct genl_info *info); | |
489111e5 | 66 | const struct genl_ops * ops; |
0b588afd | 67 | const struct genl_small_ops *small_ops; |
489111e5 | 68 | const struct genl_multicast_group *mcgrps; |
33c6b1f6 | 69 | struct module *module; |
482a8524 TG |
70 | }; |
71 | ||
482a8524 TG |
72 | /** |
73 | * struct genl_info - receiving information | |
74 | * @snd_seq: sending sequence number | |
15e47304 | 75 | * @snd_portid: netlink portid of sender |
482a8524 TG |
76 | * @nlhdr: netlink message header |
77 | * @genlhdr: generic netlink message header | |
78 | * @userhdr: user specific header | |
79 | * @attrs: netlink attributes | |
ff4c92d8 JB |
80 | * @_net: network namespace |
81 | * @user_ptr: user pointers | |
7ab606d1 | 82 | * @extack: extended ACK report struct |
482a8524 | 83 | */ |
fd2c3ef7 | 84 | struct genl_info { |
482a8524 | 85 | u32 snd_seq; |
15e47304 | 86 | u32 snd_portid; |
482a8524 TG |
87 | struct nlmsghdr * nlhdr; |
88 | struct genlmsghdr * genlhdr; | |
89 | void * userhdr; | |
90 | struct nlattr ** attrs; | |
0c5c9fb5 | 91 | possible_net_t _net; |
ff4c92d8 | 92 | void * user_ptr[2]; |
7ab606d1 | 93 | struct netlink_ext_ack *extack; |
482a8524 TG |
94 | }; |
95 | ||
134e6375 JB |
96 | static inline struct net *genl_info_net(struct genl_info *info) |
97 | { | |
c2d9ba9b | 98 | return read_pnet(&info->_net); |
134e6375 JB |
99 | } |
100 | ||
101 | static inline void genl_info_net_set(struct genl_info *info, struct net *net) | |
102 | { | |
c2d9ba9b | 103 | write_pnet(&info->_net, net); |
134e6375 | 104 | } |
134e6375 | 105 | |
7ab606d1 JB |
106 | #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg) |
107 | ||
ef6243ac JB |
108 | enum genl_validate_flags { |
109 | GENL_DONT_VALIDATE_STRICT = BIT(0), | |
110 | GENL_DONT_VALIDATE_DUMP = BIT(1), | |
111 | GENL_DONT_VALIDATE_DUMP_STRICT = BIT(2), | |
112 | }; | |
113 | ||
1927f41a | 114 | /** |
0b588afd JK |
115 | * struct genl_small_ops - generic netlink operations (small version) |
116 | * @cmd: command identifier | |
117 | * @internal_flags: flags used by the family | |
118 | * @flags: flags | |
119 | * @validate: validation flags from enum genl_validate_flags | |
120 | * @doit: standard command callback | |
121 | * @dumpit: callback for dumpers | |
122 | * | |
123 | * This is a cut-down version of struct genl_ops for users who don't need | |
124 | * most of the ancillary infra and want to save space. | |
1927f41a | 125 | */ |
0b588afd JK |
126 | struct genl_small_ops { |
127 | int (*doit)(struct sk_buff *skb, struct genl_info *info); | |
128 | int (*dumpit)(struct sk_buff *skb, struct netlink_callback *cb); | |
129 | u8 cmd; | |
130 | u8 internal_flags; | |
131 | u8 flags; | |
132 | u8 validate; | |
1927f41a JP |
133 | }; |
134 | ||
482a8524 TG |
135 | /** |
136 | * struct genl_ops - generic netlink operations | |
137 | * @cmd: command identifier | |
ff4c92d8 | 138 | * @internal_flags: flags used by the family |
482a8524 | 139 | * @flags: flags |
48526a0f JK |
140 | * @maxattr: maximum number of attributes supported |
141 | * @policy: netlink policy (takes precedence over family policy) | |
3ddf9b43 | 142 | * @validate: validation flags from enum genl_validate_flags |
482a8524 | 143 | * @doit: standard command callback |
fc9e50f5 | 144 | * @start: start callback for dumps |
482a8524 | 145 | * @dumpit: callback for dumpers |
a4d1366d | 146 | * @done: completion callback for dumps |
482a8524 | 147 | */ |
fd2c3ef7 | 148 | struct genl_ops { |
482a8524 TG |
149 | int (*doit)(struct sk_buff *skb, |
150 | struct genl_info *info); | |
fc9e50f5 | 151 | int (*start)(struct netlink_callback *cb); |
482a8524 TG |
152 | int (*dumpit)(struct sk_buff *skb, |
153 | struct netlink_callback *cb); | |
a4d1366d | 154 | int (*done)(struct netlink_callback *cb); |
48526a0f JK |
155 | const struct nla_policy *policy; |
156 | unsigned int maxattr; | |
3f5ccd06 JB |
157 | u8 cmd; |
158 | u8 internal_flags; | |
159 | u8 flags; | |
ef6243ac | 160 | u8 validate; |
482a8524 TG |
161 | }; |
162 | ||
0b588afd JK |
163 | /** |
164 | * struct genl_info - info that is available during dumpit op call | |
165 | * @family: generic netlink family - for internal genl code usage | |
166 | * @ops: generic netlink ops - for internal genl code usage | |
167 | * @attrs: netlink attributes | |
168 | */ | |
169 | struct genl_dumpit_info { | |
170 | const struct genl_family *family; | |
171 | struct genl_ops op; | |
172 | struct nlattr **attrs; | |
173 | }; | |
174 | ||
175 | static inline const struct genl_dumpit_info * | |
176 | genl_dumpit_info(struct netlink_callback *cb) | |
177 | { | |
178 | return cb->data; | |
179 | } | |
180 | ||
489111e5 | 181 | int genl_register_family(struct genl_family *family); |
2ae0f17d JB |
182 | int genl_unregister_family(const struct genl_family *family); |
183 | void genl_notify(const struct genl_family *family, struct sk_buff *skb, | |
92c14d9b | 184 | struct genl_info *info, u32 group, gfp_t flags); |
482a8524 | 185 | |
15e47304 | 186 | void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, |
2ae0f17d | 187 | const struct genl_family *family, int flags, u8 cmd); |
482a8524 | 188 | |
670dc283 JB |
189 | /** |
190 | * genlmsg_nlhdr - Obtain netlink header from user specified header | |
191 | * @user_hdr: user header as returned from genlmsg_put() | |
670dc283 JB |
192 | * |
193 | * Returns pointer to netlink header. | |
194 | */ | |
0a833c29 | 195 | static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr) |
670dc283 JB |
196 | { |
197 | return (struct nlmsghdr *)((char *)user_hdr - | |
670dc283 JB |
198 | GENL_HDRLEN - |
199 | NLMSG_HDRLEN); | |
200 | } | |
201 | ||
7b1883ce | 202 | /** |
8cb08174 | 203 | * genlmsg_parse_deprecated - parse attributes of a genetlink message |
7b1883ce JS |
204 | * @nlh: netlink message header |
205 | * @family: genetlink message family | |
206 | * @tb: destination array with maxtype+1 elements | |
207 | * @maxtype: maximum attribute type to be expected | |
208 | * @policy: validation policy | |
fceb6435 JB |
209 | * @extack: extended ACK report struct |
210 | */ | |
8cb08174 JB |
211 | static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh, |
212 | const struct genl_family *family, | |
213 | struct nlattr *tb[], int maxtype, | |
214 | const struct nla_policy *policy, | |
215 | struct netlink_ext_ack *extack) | |
7b1883ce | 216 | { |
8cb08174 JB |
217 | return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype, |
218 | policy, NL_VALIDATE_LIBERAL, extack); | |
7b1883ce JS |
219 | } |
220 | ||
3de64403 JB |
221 | /** |
222 | * genlmsg_parse - parse attributes of a genetlink message | |
223 | * @nlh: netlink message header | |
224 | * @family: genetlink message family | |
225 | * @tb: destination array with maxtype+1 elements | |
226 | * @maxtype: maximum attribute type to be expected | |
227 | * @policy: validation policy | |
228 | * @extack: extended ACK report struct | |
229 | */ | |
230 | static inline int genlmsg_parse(const struct nlmsghdr *nlh, | |
231 | const struct genl_family *family, | |
232 | struct nlattr *tb[], int maxtype, | |
233 | const struct nla_policy *policy, | |
234 | struct netlink_ext_ack *extack) | |
235 | { | |
236 | return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype, | |
237 | policy, NL_VALIDATE_STRICT, extack); | |
238 | } | |
239 | ||
670dc283 JB |
240 | /** |
241 | * genl_dump_check_consistent - check if sequence is consistent and advertise if not | |
242 | * @cb: netlink callback structure that stores the sequence number | |
243 | * @user_hdr: user header as returned from genlmsg_put() | |
670dc283 JB |
244 | * |
245 | * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it | |
246 | * simpler to use with generic netlink. | |
247 | */ | |
248 | static inline void genl_dump_check_consistent(struct netlink_callback *cb, | |
0a833c29 | 249 | void *user_hdr) |
670dc283 | 250 | { |
0a833c29 | 251 | nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr)); |
670dc283 JB |
252 | } |
253 | ||
17c157c8 TG |
254 | /** |
255 | * genlmsg_put_reply - Add generic netlink header to a reply message | |
256 | * @skb: socket buffer holding the message | |
257 | * @info: receiver info | |
258 | * @family: generic netlink family | |
259 | * @flags: netlink message flags | |
260 | * @cmd: generic netlink command | |
261 | * | |
262 | * Returns pointer to user specific header | |
263 | */ | |
264 | static inline void *genlmsg_put_reply(struct sk_buff *skb, | |
265 | struct genl_info *info, | |
2ae0f17d | 266 | const struct genl_family *family, |
17c157c8 TG |
267 | int flags, u8 cmd) |
268 | { | |
15e47304 | 269 | return genlmsg_put(skb, info->snd_portid, info->snd_seq, family, |
17c157c8 TG |
270 | flags, cmd); |
271 | } | |
272 | ||
482a8524 TG |
273 | /** |
274 | * genlmsg_end - Finalize a generic netlink message | |
275 | * @skb: socket buffer the message is stored in | |
276 | * @hdr: user specific header | |
277 | */ | |
053c095a | 278 | static inline void genlmsg_end(struct sk_buff *skb, void *hdr) |
482a8524 | 279 | { |
053c095a | 280 | nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); |
482a8524 TG |
281 | } |
282 | ||
283 | /** | |
284 | * genlmsg_cancel - Cancel construction of a generic netlink message | |
285 | * @skb: socket buffer the message is stored in | |
286 | * @hdr: generic netlink message header | |
287 | */ | |
bc3ed28c | 288 | static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) |
482a8524 | 289 | { |
38db9e1d JL |
290 | if (hdr) |
291 | nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); | |
482a8524 TG |
292 | } |
293 | ||
294 | /** | |
134e6375 | 295 | * genlmsg_multicast_netns - multicast a netlink message to a specific netns |
68eb5503 | 296 | * @family: the generic netlink family |
134e6375 JB |
297 | * @net: the net namespace |
298 | * @skb: netlink message as socket buffer | |
15e47304 | 299 | * @portid: own netlink portid to avoid sending to yourself |
2a94fe48 | 300 | * @group: offset of multicast group in groups array |
134e6375 JB |
301 | * @flags: allocation flags |
302 | */ | |
2ae0f17d | 303 | static inline int genlmsg_multicast_netns(const struct genl_family *family, |
68eb5503 | 304 | struct net *net, struct sk_buff *skb, |
15e47304 | 305 | u32 portid, unsigned int group, gfp_t flags) |
134e6375 | 306 | { |
220815a9 | 307 | if (WARN_ON_ONCE(group >= family->n_mcgrps)) |
2a94fe48 JB |
308 | return -EINVAL; |
309 | group = family->mcgrp_offset + group; | |
15e47304 | 310 | return nlmsg_multicast(net->genl_sock, skb, portid, group, flags); |
134e6375 JB |
311 | } |
312 | ||
313 | /** | |
314 | * genlmsg_multicast - multicast a netlink message to the default netns | |
68eb5503 | 315 | * @family: the generic netlink family |
482a8524 | 316 | * @skb: netlink message as socket buffer |
15e47304 | 317 | * @portid: own netlink portid to avoid sending to yourself |
2a94fe48 | 318 | * @group: offset of multicast group in groups array |
d387f6ad | 319 | * @flags: allocation flags |
482a8524 | 320 | */ |
2ae0f17d | 321 | static inline int genlmsg_multicast(const struct genl_family *family, |
68eb5503 | 322 | struct sk_buff *skb, u32 portid, |
d387f6ad | 323 | unsigned int group, gfp_t flags) |
482a8524 | 324 | { |
68eb5503 JB |
325 | return genlmsg_multicast_netns(family, &init_net, skb, |
326 | portid, group, flags); | |
482a8524 TG |
327 | } |
328 | ||
134e6375 JB |
329 | /** |
330 | * genlmsg_multicast_allns - multicast a netlink message to all net namespaces | |
68eb5503 | 331 | * @family: the generic netlink family |
134e6375 | 332 | * @skb: netlink message as socket buffer |
15e47304 | 333 | * @portid: own netlink portid to avoid sending to yourself |
2a94fe48 | 334 | * @group: offset of multicast group in groups array |
134e6375 JB |
335 | * @flags: allocation flags |
336 | * | |
337 | * This function must hold the RTNL or rcu_read_lock(). | |
338 | */ | |
2ae0f17d | 339 | int genlmsg_multicast_allns(const struct genl_family *family, |
68eb5503 | 340 | struct sk_buff *skb, u32 portid, |
134e6375 JB |
341 | unsigned int group, gfp_t flags); |
342 | ||
482a8524 TG |
343 | /** |
344 | * genlmsg_unicast - unicast a netlink message | |
345 | * @skb: netlink message as socket buffer | |
15e47304 | 346 | * @portid: netlink portid of the destination socket |
482a8524 | 347 | */ |
15e47304 | 348 | static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid) |
482a8524 | 349 | { |
15e47304 | 350 | return nlmsg_unicast(net->genl_sock, skb, portid); |
482a8524 TG |
351 | } |
352 | ||
81878d27 TG |
353 | /** |
354 | * genlmsg_reply - reply to a request | |
355 | * @skb: netlink message to be sent back | |
356 | * @info: receiver information | |
357 | */ | |
358 | static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info) | |
359 | { | |
15e47304 | 360 | return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid); |
81878d27 TG |
361 | } |
362 | ||
fb0ba6bd BS |
363 | /** |
364 | * gennlmsg_data - head of message payload | |
70f23fd6 | 365 | * @gnlh: genetlink message header |
fb0ba6bd BS |
366 | */ |
367 | static inline void *genlmsg_data(const struct genlmsghdr *gnlh) | |
368 | { | |
369 | return ((unsigned char *) gnlh + GENL_HDRLEN); | |
370 | } | |
371 | ||
372 | /** | |
373 | * genlmsg_len - length of message payload | |
374 | * @gnlh: genetlink message header | |
375 | */ | |
376 | static inline int genlmsg_len(const struct genlmsghdr *gnlh) | |
377 | { | |
378 | struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh - | |
379 | NLMSG_HDRLEN); | |
380 | return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN); | |
381 | } | |
382 | ||
17db952c BS |
383 | /** |
384 | * genlmsg_msg_size - length of genetlink message not including padding | |
385 | * @payload: length of message payload | |
386 | */ | |
387 | static inline int genlmsg_msg_size(int payload) | |
388 | { | |
389 | return GENL_HDRLEN + payload; | |
390 | } | |
391 | ||
392 | /** | |
393 | * genlmsg_total_size - length of genetlink message including padding | |
394 | * @payload: length of message payload | |
395 | */ | |
396 | static inline int genlmsg_total_size(int payload) | |
397 | { | |
398 | return NLMSG_ALIGN(genlmsg_msg_size(payload)); | |
399 | } | |
400 | ||
3dabc715 TG |
401 | /** |
402 | * genlmsg_new - Allocate a new generic netlink message | |
403 | * @payload: size of the message payload | |
404 | * @flags: the type of memory to allocate. | |
405 | */ | |
406 | static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags) | |
407 | { | |
408 | return nlmsg_new(genlmsg_total_size(payload), flags); | |
409 | } | |
410 | ||
62b68e99 JB |
411 | /** |
412 | * genl_set_err - report error to genetlink broadcast listeners | |
68eb5503 | 413 | * @family: the generic netlink family |
62b68e99 JB |
414 | * @net: the network namespace to report the error to |
415 | * @portid: the PORTID of a process that we want to skip (if any) | |
416 | * @group: the broadcast group that will notice the error | |
2a94fe48 | 417 | * (this is the offset of the multicast group in the groups array) |
62b68e99 JB |
418 | * @code: error code, must be negative (as usual in kernelspace) |
419 | * | |
420 | * This function returns the number of broadcast listeners that have set the | |
421 | * NETLINK_RECV_NO_ENOBUFS socket option. | |
422 | */ | |
2ae0f17d JB |
423 | static inline int genl_set_err(const struct genl_family *family, |
424 | struct net *net, u32 portid, | |
425 | u32 group, int code) | |
62b68e99 | 426 | { |
91398a09 JB |
427 | if (WARN_ON_ONCE(group >= family->n_mcgrps)) |
428 | return -EINVAL; | |
429 | group = family->mcgrp_offset + group; | |
62b68e99 JB |
430 | return netlink_set_err(net->genl_sock, portid, group, code); |
431 | } | |
3dabc715 | 432 | |
2ae0f17d | 433 | static inline int genl_has_listeners(const struct genl_family *family, |
f8403a2e | 434 | struct net *net, unsigned int group) |
0d566379 ND |
435 | { |
436 | if (WARN_ON_ONCE(group >= family->n_mcgrps)) | |
437 | return -EINVAL; | |
438 | group = family->mcgrp_offset + group; | |
f8403a2e | 439 | return netlink_has_listeners(net->genl_sock, group); |
0d566379 | 440 | } |
482a8524 | 441 | #endif /* __NET_GENERIC_NETLINK_H */ |