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