1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_GENERIC_NETLINK_H
3 #define __NET_GENERIC_NETLINK_H
5 #include <linux/genetlink.h>
6 #include <net/netlink.h>
7 #include <net/net_namespace.h>
9 #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
12 * struct genl_multicast_group - generic netlink multicast group
13 * @name: name of the multicast group, names are per-family
15 struct genl_multicast_group
{
16 char name
[GENL_NAMSIZ
];
23 * struct genl_family - generic netlink family
24 * @id: protocol family identifier (private)
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
29 * @policy: netlink policy
30 * @netnsok: set to true if the family can handle network
31 * namespaces and should be presented in all of them
32 * @parallel_ops: operations can be called in parallel and aren't
33 * synchronized by the core genetlink code
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
38 * @mcgrps: multicast groups used by this family
39 * @n_mcgrps: number of multicast groups
40 * @mcgrp_offset: starting number of multicast group IDs in this family
42 * @ops: the operations supported by this family
43 * @n_ops: number of operations supported by this family
44 * @small_ops: the small-struct operations supported by this family
45 * @n_small_ops: number of small-struct operations supported by this family
50 char name
[GENL_NAMSIZ
];
53 unsigned int mcgrp_offset
; /* private */
59 const struct nla_policy
*policy
;
60 int (*pre_doit
)(const struct genl_ops
*ops
,
62 struct genl_info
*info
);
63 void (*post_doit
)(const struct genl_ops
*ops
,
65 struct genl_info
*info
);
66 const struct genl_ops
* ops
;
67 const struct genl_small_ops
*small_ops
;
68 const struct genl_multicast_group
*mcgrps
;
69 struct module
*module
;
73 * struct genl_info - receiving information
74 * @snd_seq: sending sequence number
75 * @snd_portid: netlink portid of sender
76 * @nlhdr: netlink message header
77 * @genlhdr: generic netlink message header
78 * @userhdr: user specific header
79 * @attrs: netlink attributes
80 * @_net: network namespace
81 * @user_ptr: user pointers
82 * @extack: extended ACK report struct
87 struct nlmsghdr
* nlhdr
;
88 struct genlmsghdr
* genlhdr
;
90 struct nlattr
** attrs
;
93 struct netlink_ext_ack
*extack
;
96 static inline struct net
*genl_info_net(struct genl_info
*info
)
98 return read_pnet(&info
->_net
);
101 static inline void genl_info_net_set(struct genl_info
*info
, struct net
*net
)
103 write_pnet(&info
->_net
, net
);
106 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
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),
115 * struct genl_small_ops - generic netlink operations (small version)
116 * @cmd: command identifier
117 * @internal_flags: flags used by the family
119 * @validate: validation flags from enum genl_validate_flags
120 * @doit: standard command callback
121 * @dumpit: callback for dumpers
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.
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
);
136 * struct genl_ops - generic netlink operations
137 * @cmd: command identifier
138 * @internal_flags: flags used by the family
140 * @maxattr: maximum number of attributes supported
141 * @policy: netlink policy (takes precedence over family policy)
142 * @validate: validation flags from enum genl_validate_flags
143 * @doit: standard command callback
144 * @start: start callback for dumps
145 * @dumpit: callback for dumpers
146 * @done: completion callback for dumps
149 int (*doit
)(struct sk_buff
*skb
,
150 struct genl_info
*info
);
151 int (*start
)(struct netlink_callback
*cb
);
152 int (*dumpit
)(struct sk_buff
*skb
,
153 struct netlink_callback
*cb
);
154 int (*done
)(struct netlink_callback
*cb
);
155 const struct nla_policy
*policy
;
156 unsigned int maxattr
;
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
169 struct genl_dumpit_info
{
170 const struct genl_family
*family
;
172 struct nlattr
**attrs
;
175 static inline const struct genl_dumpit_info
*
176 genl_dumpit_info(struct netlink_callback
*cb
)
181 int genl_register_family(struct genl_family
*family
);
182 int genl_unregister_family(const struct genl_family
*family
);
183 void genl_notify(const struct genl_family
*family
, struct sk_buff
*skb
,
184 struct genl_info
*info
, u32 group
, gfp_t flags
);
186 void *genlmsg_put(struct sk_buff
*skb
, u32 portid
, u32 seq
,
187 const struct genl_family
*family
, int flags
, u8 cmd
);
190 * genlmsg_nlhdr - Obtain netlink header from user specified header
191 * @user_hdr: user header as returned from genlmsg_put()
193 * Returns pointer to netlink header.
195 static inline struct nlmsghdr
*genlmsg_nlhdr(void *user_hdr
)
197 return (struct nlmsghdr
*)((char *)user_hdr
-
203 * genlmsg_parse_deprecated - parse attributes of a genetlink message
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
209 * @extack: extended ACK report struct
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
)
217 return __nlmsg_parse(nlh
, family
->hdrsize
+ GENL_HDRLEN
, tb
, maxtype
,
218 policy
, NL_VALIDATE_LIBERAL
, extack
);
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
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
)
236 return __nlmsg_parse(nlh
, family
->hdrsize
+ GENL_HDRLEN
, tb
, maxtype
,
237 policy
, NL_VALIDATE_STRICT
, extack
);
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()
245 * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
246 * simpler to use with generic netlink.
248 static inline void genl_dump_check_consistent(struct netlink_callback
*cb
,
251 nl_dump_check_consistent(cb
, genlmsg_nlhdr(user_hdr
));
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
262 * Returns pointer to user specific header
264 static inline void *genlmsg_put_reply(struct sk_buff
*skb
,
265 struct genl_info
*info
,
266 const struct genl_family
*family
,
269 return genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
, family
,
274 * genlmsg_end - Finalize a generic netlink message
275 * @skb: socket buffer the message is stored in
276 * @hdr: user specific header
278 static inline void genlmsg_end(struct sk_buff
*skb
, void *hdr
)
280 nlmsg_end(skb
, hdr
- GENL_HDRLEN
- NLMSG_HDRLEN
);
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
288 static inline void genlmsg_cancel(struct sk_buff
*skb
, void *hdr
)
291 nlmsg_cancel(skb
, hdr
- GENL_HDRLEN
- NLMSG_HDRLEN
);
295 * genlmsg_multicast_netns - multicast a netlink message to a specific netns
296 * @family: the generic netlink family
297 * @net: the net namespace
298 * @skb: netlink message as socket buffer
299 * @portid: own netlink portid to avoid sending to yourself
300 * @group: offset of multicast group in groups array
301 * @flags: allocation flags
303 static inline int genlmsg_multicast_netns(const struct genl_family
*family
,
304 struct net
*net
, struct sk_buff
*skb
,
305 u32 portid
, unsigned int group
, gfp_t flags
)
307 if (WARN_ON_ONCE(group
>= family
->n_mcgrps
))
309 group
= family
->mcgrp_offset
+ group
;
310 return nlmsg_multicast(net
->genl_sock
, skb
, portid
, group
, flags
);
314 * genlmsg_multicast - multicast a netlink message to the default netns
315 * @family: the generic netlink family
316 * @skb: netlink message as socket buffer
317 * @portid: own netlink portid to avoid sending to yourself
318 * @group: offset of multicast group in groups array
319 * @flags: allocation flags
321 static inline int genlmsg_multicast(const struct genl_family
*family
,
322 struct sk_buff
*skb
, u32 portid
,
323 unsigned int group
, gfp_t flags
)
325 return genlmsg_multicast_netns(family
, &init_net
, skb
,
326 portid
, group
, flags
);
330 * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
331 * @family: the generic netlink family
332 * @skb: netlink message as socket buffer
333 * @portid: own netlink portid to avoid sending to yourself
334 * @group: offset of multicast group in groups array
335 * @flags: allocation flags
337 * This function must hold the RTNL or rcu_read_lock().
339 int genlmsg_multicast_allns(const struct genl_family
*family
,
340 struct sk_buff
*skb
, u32 portid
,
341 unsigned int group
, gfp_t flags
);
344 * genlmsg_unicast - unicast a netlink message
345 * @skb: netlink message as socket buffer
346 * @portid: netlink portid of the destination socket
348 static inline int genlmsg_unicast(struct net
*net
, struct sk_buff
*skb
, u32 portid
)
350 return nlmsg_unicast(net
->genl_sock
, skb
, portid
);
354 * genlmsg_reply - reply to a request
355 * @skb: netlink message to be sent back
356 * @info: receiver information
358 static inline int genlmsg_reply(struct sk_buff
*skb
, struct genl_info
*info
)
360 return genlmsg_unicast(genl_info_net(info
), skb
, info
->snd_portid
);
364 * gennlmsg_data - head of message payload
365 * @gnlh: genetlink message header
367 static inline void *genlmsg_data(const struct genlmsghdr
*gnlh
)
369 return ((unsigned char *) gnlh
+ GENL_HDRLEN
);
373 * genlmsg_len - length of message payload
374 * @gnlh: genetlink message header
376 static inline int genlmsg_len(const struct genlmsghdr
*gnlh
)
378 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)((unsigned char *)gnlh
-
380 return (nlh
->nlmsg_len
- GENL_HDRLEN
- NLMSG_HDRLEN
);
384 * genlmsg_msg_size - length of genetlink message not including padding
385 * @payload: length of message payload
387 static inline int genlmsg_msg_size(int payload
)
389 return GENL_HDRLEN
+ payload
;
393 * genlmsg_total_size - length of genetlink message including padding
394 * @payload: length of message payload
396 static inline int genlmsg_total_size(int payload
)
398 return NLMSG_ALIGN(genlmsg_msg_size(payload
));
402 * genlmsg_new - Allocate a new generic netlink message
403 * @payload: size of the message payload
404 * @flags: the type of memory to allocate.
406 static inline struct sk_buff
*genlmsg_new(size_t payload
, gfp_t flags
)
408 return nlmsg_new(genlmsg_total_size(payload
), flags
);
412 * genl_set_err - report error to genetlink broadcast listeners
413 * @family: the generic netlink family
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
417 * (this is the offset of the multicast group in the groups array)
418 * @code: error code, must be negative (as usual in kernelspace)
420 * This function returns the number of broadcast listeners that have set the
421 * NETLINK_RECV_NO_ENOBUFS socket option.
423 static inline int genl_set_err(const struct genl_family
*family
,
424 struct net
*net
, u32 portid
,
427 if (WARN_ON_ONCE(group
>= family
->n_mcgrps
))
429 group
= family
->mcgrp_offset
+ group
;
430 return netlink_set_err(net
->genl_sock
, portid
, group
, code
);
433 static inline int genl_has_listeners(const struct genl_family
*family
,
434 struct net
*net
, unsigned int group
)
436 if (WARN_ON_ONCE(group
>= family
->n_mcgrps
))
438 group
= family
->mcgrp_offset
+ group
;
439 return netlink_has_listeners(net
->genl_sock
, group
);
441 #endif /* __NET_GENERIC_NETLINK_H */