xref: /openbmc/linux/include/net/genetlink.h (revision 3de644035446567017e952f16da2594d6bd195fc)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2482a8524SThomas Graf #ifndef __NET_GENERIC_NETLINK_H
3482a8524SThomas Graf #define __NET_GENERIC_NETLINK_H
4482a8524SThomas Graf 
5482a8524SThomas Graf #include <linux/genetlink.h>
6482a8524SThomas Graf #include <net/netlink.h>
7134e6375SJohannes Berg #include <net/net_namespace.h>
8482a8524SThomas Graf 
958050fceSThomas Graf #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
1058050fceSThomas Graf 
11482a8524SThomas Graf /**
122dbba6f7SJohannes Berg  * struct genl_multicast_group - generic netlink multicast group
132dbba6f7SJohannes Berg  * @name: name of the multicast group, names are per-family
142dbba6f7SJohannes Berg  */
15fd2c3ef7SEric Dumazet struct genl_multicast_group {
162dbba6f7SJohannes Berg 	char			name[GENL_NAMSIZ];
172dbba6f7SJohannes Berg };
182dbba6f7SJohannes Berg 
19ff4c92d8SJohannes Berg struct genl_ops;
20ff4c92d8SJohannes Berg struct genl_info;
21ff4c92d8SJohannes Berg 
222dbba6f7SJohannes Berg /**
23482a8524SThomas Graf  * struct genl_family - generic netlink family
24a07ea4d9SJohannes Berg  * @id: protocol family identifier (private)
25482a8524SThomas Graf  * @hdrsize: length of user specific header in bytes
26482a8524SThomas Graf  * @name: name of family
27482a8524SThomas Graf  * @version: protocol version
28482a8524SThomas Graf  * @maxattr: maximum number of attributes supported
293b0f31f2SJohannes Berg  * @policy: netlink policy
30134e6375SJohannes Berg  * @netnsok: set to true if the family can handle network
31134e6375SJohannes Berg  *	namespaces and should be presented in all of them
32f555f3d7SJohannes Berg  * @parallel_ops: operations can be called in parallel and aren't
33f555f3d7SJohannes Berg  *	synchronized by the core genetlink code
34ff4c92d8SJohannes Berg  * @pre_doit: called before an operation's doit callback, it may
35ff4c92d8SJohannes Berg  *	do additional, common, filtering and return an error
36ff4c92d8SJohannes Berg  * @post_doit: called after an operation's doit callback, it may
37ff4c92d8SJohannes Berg  *	undo operations done by pre_doit, for example release locks
38c380d9a7SJohannes Berg  * @mcast_bind: a socket bound to the given multicast group (which
39c380d9a7SJohannes Berg  *	is given as the offset into the groups array)
40ee1c2442SJohannes Berg  * @mcast_unbind: a socket was unbound from the given multicast group.
41ee1c2442SJohannes Berg  *	Note that unbind() will not be called symmetrically if the
42ee1c2442SJohannes Berg  *	generic netlink family is removed while there are still open
43ee1c2442SJohannes Berg  *	sockets.
44489111e5SJohannes Berg  * @attrbuf: buffer to store parsed attributes (private)
45489111e5SJohannes Berg  * @mcgrps: multicast groups used by this family
46489111e5SJohannes Berg  * @n_mcgrps: number of multicast groups
472a94fe48SJohannes Berg  * @mcgrp_offset: starting number of multicast group IDs in this family
48489111e5SJohannes Berg  *	(private)
49489111e5SJohannes Berg  * @ops: the operations supported by this family
50489111e5SJohannes Berg  * @n_ops: number of operations supported by this family
51482a8524SThomas Graf  */
52fd2c3ef7SEric Dumazet struct genl_family {
5398e4321bSDavid S. Miller 	int			id;		/* private */
54482a8524SThomas Graf 	unsigned int		hdrsize;
55482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
56482a8524SThomas Graf 	unsigned int		version;
57482a8524SThomas Graf 	unsigned int		maxattr;
58134e6375SJohannes Berg 	bool			netnsok;
59def31174SPravin B Shelar 	bool			parallel_ops;
603b0f31f2SJohannes Berg 	const struct nla_policy *policy;
61f84f771dSJohannes Berg 	int			(*pre_doit)(const struct genl_ops *ops,
62ff4c92d8SJohannes Berg 					    struct sk_buff *skb,
63ff4c92d8SJohannes Berg 					    struct genl_info *info);
64f84f771dSJohannes Berg 	void			(*post_doit)(const struct genl_ops *ops,
65ff4c92d8SJohannes Berg 					     struct sk_buff *skb,
66ff4c92d8SJohannes Berg 					     struct genl_info *info);
67023e2cfaSJohannes Berg 	int			(*mcast_bind)(struct net *net, int group);
68023e2cfaSJohannes Berg 	void			(*mcast_unbind)(struct net *net, int group);
69482a8524SThomas Graf 	struct nlattr **	attrbuf;	/* private */
70489111e5SJohannes Berg 	const struct genl_ops *	ops;
71489111e5SJohannes Berg 	const struct genl_multicast_group *mcgrps;
72489111e5SJohannes Berg 	unsigned int		n_ops;
73489111e5SJohannes Berg 	unsigned int		n_mcgrps;
742a94fe48SJohannes Berg 	unsigned int		mcgrp_offset;	/* private */
7533c6b1f6SPravin B Shelar 	struct module		*module;
76482a8524SThomas Graf };
77482a8524SThomas Graf 
782ae0f17dSJohannes Berg struct nlattr **genl_family_attrbuf(const struct genl_family *family);
79c90c39daSJohannes Berg 
80482a8524SThomas Graf /**
81482a8524SThomas Graf  * struct genl_info - receiving information
82482a8524SThomas Graf  * @snd_seq: sending sequence number
8315e47304SEric W. Biederman  * @snd_portid: netlink portid of sender
84482a8524SThomas Graf  * @nlhdr: netlink message header
85482a8524SThomas Graf  * @genlhdr: generic netlink message header
86482a8524SThomas Graf  * @userhdr: user specific header
87482a8524SThomas Graf  * @attrs: netlink attributes
88ff4c92d8SJohannes Berg  * @_net: network namespace
89ff4c92d8SJohannes Berg  * @user_ptr: user pointers
907ab606d1SJohannes Berg  * @extack: extended ACK report struct
91482a8524SThomas Graf  */
92fd2c3ef7SEric Dumazet struct genl_info {
93482a8524SThomas Graf 	u32			snd_seq;
9415e47304SEric W. Biederman 	u32			snd_portid;
95482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
96482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
97482a8524SThomas Graf 	void *			userhdr;
98482a8524SThomas Graf 	struct nlattr **	attrs;
990c5c9fb5SEric W. Biederman 	possible_net_t		_net;
100ff4c92d8SJohannes Berg 	void *			user_ptr[2];
1017ab606d1SJohannes Berg 	struct netlink_ext_ack *extack;
102482a8524SThomas Graf };
103482a8524SThomas Graf 
104134e6375SJohannes Berg static inline struct net *genl_info_net(struct genl_info *info)
105134e6375SJohannes Berg {
106c2d9ba9bSEric Dumazet 	return read_pnet(&info->_net);
107134e6375SJohannes Berg }
108134e6375SJohannes Berg 
109134e6375SJohannes Berg static inline void genl_info_net_set(struct genl_info *info, struct net *net)
110134e6375SJohannes Berg {
111c2d9ba9bSEric Dumazet 	write_pnet(&info->_net, net);
112134e6375SJohannes Berg }
113134e6375SJohannes Berg 
1147ab606d1SJohannes Berg #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
1157ab606d1SJohannes Berg 
1167ab606d1SJohannes Berg static inline int genl_err_attr(struct genl_info *info, int err,
1176fce10f7SMichal Kubecek 				const struct nlattr *attr)
1187ab606d1SJohannes Berg {
1197ab606d1SJohannes Berg 	info->extack->bad_attr = attr;
1207ab606d1SJohannes Berg 
1217ab606d1SJohannes Berg 	return err;
1227ab606d1SJohannes Berg }
1237ab606d1SJohannes Berg 
124482a8524SThomas Graf /**
125482a8524SThomas Graf  * struct genl_ops - generic netlink operations
126482a8524SThomas Graf  * @cmd: command identifier
127ff4c92d8SJohannes Berg  * @internal_flags: flags used by the family
128482a8524SThomas Graf  * @flags: flags
129482a8524SThomas Graf  * @doit: standard command callback
130fc9e50f5STom Herbert  * @start: start callback for dumps
131482a8524SThomas Graf  * @dumpit: callback for dumpers
132a4d1366dSJamal Hadi Salim  * @done: completion callback for dumps
133482a8524SThomas Graf  */
134fd2c3ef7SEric Dumazet struct genl_ops {
135482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
136482a8524SThomas Graf 				       struct genl_info *info);
137fc9e50f5STom Herbert 	int		       (*start)(struct netlink_callback *cb);
138482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
139482a8524SThomas Graf 					 struct netlink_callback *cb);
140a4d1366dSJamal Hadi Salim 	int		       (*done)(struct netlink_callback *cb);
1413f5ccd06SJohannes Berg 	u8			cmd;
1423f5ccd06SJohannes Berg 	u8			internal_flags;
1433f5ccd06SJohannes Berg 	u8			flags;
144482a8524SThomas Graf };
145482a8524SThomas Graf 
146489111e5SJohannes Berg int genl_register_family(struct genl_family *family);
1472ae0f17dSJohannes Berg int genl_unregister_family(const struct genl_family *family);
1482ae0f17dSJohannes Berg void genl_notify(const struct genl_family *family, struct sk_buff *skb,
14992c14d9bSJiri Benc 		 struct genl_info *info, u32 group, gfp_t flags);
150482a8524SThomas Graf 
15115e47304SEric W. Biederman void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
1522ae0f17dSJohannes Berg 		  const struct genl_family *family, int flags, u8 cmd);
153482a8524SThomas Graf 
154482a8524SThomas Graf /**
155670dc283SJohannes Berg  * genlmsg_nlhdr - Obtain netlink header from user specified header
156670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
157670dc283SJohannes Berg  *
158670dc283SJohannes Berg  * Returns pointer to netlink header.
159670dc283SJohannes Berg  */
1600a833c29SMichal Kubecek static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
161670dc283SJohannes Berg {
162670dc283SJohannes Berg 	return (struct nlmsghdr *)((char *)user_hdr -
163670dc283SJohannes Berg 				   GENL_HDRLEN -
164670dc283SJohannes Berg 				   NLMSG_HDRLEN);
165670dc283SJohannes Berg }
166670dc283SJohannes Berg 
167670dc283SJohannes Berg /**
1688cb08174SJohannes Berg  * genlmsg_parse_deprecated - parse attributes of a genetlink message
1697b1883ceSJoe Stringer  * @nlh: netlink message header
1707b1883ceSJoe Stringer  * @family: genetlink message family
1717b1883ceSJoe Stringer  * @tb: destination array with maxtype+1 elements
1727b1883ceSJoe Stringer  * @maxtype: maximum attribute type to be expected
1737b1883ceSJoe Stringer  * @policy: validation policy
174fceb6435SJohannes Berg  * @extack: extended ACK report struct
175fceb6435SJohannes Berg  */
1768cb08174SJohannes Berg static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
1777b1883ceSJoe Stringer 					   const struct genl_family *family,
1787b1883ceSJoe Stringer 					   struct nlattr *tb[], int maxtype,
179fceb6435SJohannes Berg 					   const struct nla_policy *policy,
180fceb6435SJohannes Berg 					   struct netlink_ext_ack *extack)
1817b1883ceSJoe Stringer {
1828cb08174SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
1838cb08174SJohannes Berg 			     policy, NL_VALIDATE_LIBERAL, extack);
1847b1883ceSJoe Stringer }
1857b1883ceSJoe Stringer 
1867b1883ceSJoe Stringer /**
187*3de64403SJohannes Berg  * genlmsg_parse - parse attributes of a genetlink message
188*3de64403SJohannes Berg  * @nlh: netlink message header
189*3de64403SJohannes Berg  * @family: genetlink message family
190*3de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
191*3de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
192*3de64403SJohannes Berg  * @policy: validation policy
193*3de64403SJohannes Berg  * @extack: extended ACK report struct
194*3de64403SJohannes Berg  */
195*3de64403SJohannes Berg static inline int genlmsg_parse(const struct nlmsghdr *nlh,
196*3de64403SJohannes Berg 				const struct genl_family *family,
197*3de64403SJohannes Berg 				struct nlattr *tb[], int maxtype,
198*3de64403SJohannes Berg 				const struct nla_policy *policy,
199*3de64403SJohannes Berg 				struct netlink_ext_ack *extack)
200*3de64403SJohannes Berg {
201*3de64403SJohannes Berg 	return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
202*3de64403SJohannes Berg 			     policy, NL_VALIDATE_STRICT, extack);
203*3de64403SJohannes Berg }
204*3de64403SJohannes Berg 
205*3de64403SJohannes Berg /**
206670dc283SJohannes Berg  * genl_dump_check_consistent - check if sequence is consistent and advertise if not
207670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
208670dc283SJohannes Berg  * @user_hdr: user header as returned from genlmsg_put()
209670dc283SJohannes Berg  *
210670dc283SJohannes Berg  * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
211670dc283SJohannes Berg  * simpler to use with generic netlink.
212670dc283SJohannes Berg  */
213670dc283SJohannes Berg static inline void genl_dump_check_consistent(struct netlink_callback *cb,
2140a833c29SMichal Kubecek 					      void *user_hdr)
215670dc283SJohannes Berg {
2160a833c29SMichal Kubecek 	nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
217670dc283SJohannes Berg }
218670dc283SJohannes Berg 
219670dc283SJohannes Berg /**
22017c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
22117c157c8SThomas Graf  * @skb: socket buffer holding the message
22217c157c8SThomas Graf  * @info: receiver info
22317c157c8SThomas Graf  * @family: generic netlink family
22417c157c8SThomas Graf  * @flags: netlink message flags
22517c157c8SThomas Graf  * @cmd: generic netlink command
22617c157c8SThomas Graf  *
22717c157c8SThomas Graf  * Returns pointer to user specific header
22817c157c8SThomas Graf  */
22917c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
23017c157c8SThomas Graf 				      struct genl_info *info,
2312ae0f17dSJohannes Berg 				      const struct genl_family *family,
23217c157c8SThomas Graf 				      int flags, u8 cmd)
23317c157c8SThomas Graf {
23415e47304SEric W. Biederman 	return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
23517c157c8SThomas Graf 			   flags, cmd);
23617c157c8SThomas Graf }
23717c157c8SThomas Graf 
23817c157c8SThomas Graf /**
239482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
240482a8524SThomas Graf  * @skb: socket buffer the message is stored in
241482a8524SThomas Graf  * @hdr: user specific header
242482a8524SThomas Graf  */
243053c095aSJohannes Berg static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
244482a8524SThomas Graf {
245053c095aSJohannes Berg 	nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
246482a8524SThomas Graf }
247482a8524SThomas Graf 
248482a8524SThomas Graf /**
249482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
250482a8524SThomas Graf  * @skb: socket buffer the message is stored in
251482a8524SThomas Graf  * @hdr: generic netlink message header
252482a8524SThomas Graf  */
253bc3ed28cSThomas Graf static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
254482a8524SThomas Graf {
25538db9e1dSJulia Lawall 	if (hdr)
256bc3ed28cSThomas Graf 		nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
257482a8524SThomas Graf }
258482a8524SThomas Graf 
259482a8524SThomas Graf /**
260134e6375SJohannes Berg  * genlmsg_multicast_netns - multicast a netlink message to a specific netns
26168eb5503SJohannes Berg  * @family: the generic netlink family
262134e6375SJohannes Berg  * @net: the net namespace
263134e6375SJohannes Berg  * @skb: netlink message as socket buffer
26415e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
2652a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
266134e6375SJohannes Berg  * @flags: allocation flags
267134e6375SJohannes Berg  */
2682ae0f17dSJohannes Berg static inline int genlmsg_multicast_netns(const struct genl_family *family,
26968eb5503SJohannes Berg 					  struct net *net, struct sk_buff *skb,
27015e47304SEric W. Biederman 					  u32 portid, unsigned int group, gfp_t flags)
271134e6375SJohannes Berg {
272220815a9SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
2732a94fe48SJohannes Berg 		return -EINVAL;
2742a94fe48SJohannes Berg 	group = family->mcgrp_offset + group;
27515e47304SEric W. Biederman 	return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
276134e6375SJohannes Berg }
277134e6375SJohannes Berg 
278134e6375SJohannes Berg /**
279134e6375SJohannes Berg  * genlmsg_multicast - multicast a netlink message to the default netns
28068eb5503SJohannes Berg  * @family: the generic netlink family
281482a8524SThomas Graf  * @skb: netlink message as socket buffer
28215e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
2832a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
284d387f6adSThomas Graf  * @flags: allocation flags
285482a8524SThomas Graf  */
2862ae0f17dSJohannes Berg static inline int genlmsg_multicast(const struct genl_family *family,
28768eb5503SJohannes Berg 				    struct sk_buff *skb, u32 portid,
288d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
289482a8524SThomas Graf {
29068eb5503SJohannes Berg 	return genlmsg_multicast_netns(family, &init_net, skb,
29168eb5503SJohannes Berg 				       portid, group, flags);
292482a8524SThomas Graf }
293482a8524SThomas Graf 
294482a8524SThomas Graf /**
295134e6375SJohannes Berg  * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
29668eb5503SJohannes Berg  * @family: the generic netlink family
297134e6375SJohannes Berg  * @skb: netlink message as socket buffer
29815e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
2992a94fe48SJohannes Berg  * @group: offset of multicast group in groups array
300134e6375SJohannes Berg  * @flags: allocation flags
301134e6375SJohannes Berg  *
302134e6375SJohannes Berg  * This function must hold the RTNL or rcu_read_lock().
303134e6375SJohannes Berg  */
3042ae0f17dSJohannes Berg int genlmsg_multicast_allns(const struct genl_family *family,
30568eb5503SJohannes Berg 			    struct sk_buff *skb, u32 portid,
306134e6375SJohannes Berg 			    unsigned int group, gfp_t flags);
307134e6375SJohannes Berg 
308134e6375SJohannes Berg /**
309482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
310482a8524SThomas Graf  * @skb: netlink message as socket buffer
31115e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
312482a8524SThomas Graf  */
31315e47304SEric W. Biederman static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
314482a8524SThomas Graf {
31515e47304SEric W. Biederman 	return nlmsg_unicast(net->genl_sock, skb, portid);
316482a8524SThomas Graf }
317482a8524SThomas Graf 
318fb0ba6bdSBalbir Singh /**
31981878d27SThomas Graf  * genlmsg_reply - reply to a request
32081878d27SThomas Graf  * @skb: netlink message to be sent back
32181878d27SThomas Graf  * @info: receiver information
32281878d27SThomas Graf  */
32381878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
32481878d27SThomas Graf {
32515e47304SEric W. Biederman 	return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
32681878d27SThomas Graf }
32781878d27SThomas Graf 
32881878d27SThomas Graf /**
329fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
33070f23fd6SJustin P. Mattock  * @gnlh: genetlink message header
331fb0ba6bdSBalbir Singh  */
332fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
333fb0ba6bdSBalbir Singh {
334fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
335fb0ba6bdSBalbir Singh }
336fb0ba6bdSBalbir Singh 
337fb0ba6bdSBalbir Singh /**
338fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
339fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
340fb0ba6bdSBalbir Singh  */
341fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
342fb0ba6bdSBalbir Singh {
343fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
344fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
345fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
346fb0ba6bdSBalbir Singh }
347fb0ba6bdSBalbir Singh 
34817db952cSBalbir Singh /**
34917db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
35017db952cSBalbir Singh  * @payload: length of message payload
35117db952cSBalbir Singh  */
35217db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
35317db952cSBalbir Singh {
35417db952cSBalbir Singh 	return GENL_HDRLEN + payload;
35517db952cSBalbir Singh }
35617db952cSBalbir Singh 
35717db952cSBalbir Singh /**
35817db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
35917db952cSBalbir Singh  * @payload: length of message payload
36017db952cSBalbir Singh  */
36117db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
36217db952cSBalbir Singh {
36317db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
36417db952cSBalbir Singh }
36517db952cSBalbir Singh 
3663dabc715SThomas Graf /**
3673dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
3683dabc715SThomas Graf  * @payload: size of the message payload
3693dabc715SThomas Graf  * @flags: the type of memory to allocate.
3703dabc715SThomas Graf  */
3713dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
3723dabc715SThomas Graf {
3733dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
3743dabc715SThomas Graf }
3753dabc715SThomas Graf 
37662b68e99SJohannes Berg /**
37762b68e99SJohannes Berg  * genl_set_err - report error to genetlink broadcast listeners
37868eb5503SJohannes Berg  * @family: the generic netlink family
37962b68e99SJohannes Berg  * @net: the network namespace to report the error to
38062b68e99SJohannes Berg  * @portid: the PORTID of a process that we want to skip (if any)
38162b68e99SJohannes Berg  * @group: the broadcast group that will notice the error
3822a94fe48SJohannes Berg  * 	(this is the offset of the multicast group in the groups array)
38362b68e99SJohannes Berg  * @code: error code, must be negative (as usual in kernelspace)
38462b68e99SJohannes Berg  *
38562b68e99SJohannes Berg  * This function returns the number of broadcast listeners that have set the
38662b68e99SJohannes Berg  * NETLINK_RECV_NO_ENOBUFS socket option.
38762b68e99SJohannes Berg  */
3882ae0f17dSJohannes Berg static inline int genl_set_err(const struct genl_family *family,
3892ae0f17dSJohannes Berg 			       struct net *net, u32 portid,
3902ae0f17dSJohannes Berg 			       u32 group, int code)
39162b68e99SJohannes Berg {
39291398a09SJohannes Berg 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
39391398a09SJohannes Berg 		return -EINVAL;
39491398a09SJohannes Berg 	group = family->mcgrp_offset + group;
39562b68e99SJohannes Berg 	return netlink_set_err(net->genl_sock, portid, group, code);
39662b68e99SJohannes Berg }
3973dabc715SThomas Graf 
3982ae0f17dSJohannes Berg static inline int genl_has_listeners(const struct genl_family *family,
399f8403a2eSJohannes Berg 				     struct net *net, unsigned int group)
4000d566379SNicolas Dichtel {
4010d566379SNicolas Dichtel 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
4020d566379SNicolas Dichtel 		return -EINVAL;
4030d566379SNicolas Dichtel 	group = family->mcgrp_offset + group;
404f8403a2eSJohannes Berg 	return netlink_has_listeners(net->genl_sock, group);
4050d566379SNicolas Dichtel }
406482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
407