xref: /openbmc/linux/include/net/genetlink.h (revision 2dbba6f773d1e1e4c78f03b0dbf19790d9017693)
1482a8524SThomas Graf #ifndef __NET_GENERIC_NETLINK_H
2482a8524SThomas Graf #define __NET_GENERIC_NETLINK_H
3482a8524SThomas Graf 
4482a8524SThomas Graf #include <linux/genetlink.h>
5482a8524SThomas Graf #include <net/netlink.h>
6482a8524SThomas Graf 
7482a8524SThomas Graf /**
8*2dbba6f7SJohannes Berg  * struct genl_multicast_group - generic netlink multicast group
9*2dbba6f7SJohannes Berg  * @name: name of the multicast group, names are per-family
10*2dbba6f7SJohannes Berg  * @id: multicast group ID, assigned by the core, to use with
11*2dbba6f7SJohannes Berg  *      genlmsg_multicast().
12*2dbba6f7SJohannes Berg  * @list: list entry for linking
13*2dbba6f7SJohannes Berg  * @family: pointer to family, need not be set before registering
14*2dbba6f7SJohannes Berg  */
15*2dbba6f7SJohannes Berg struct genl_multicast_group
16*2dbba6f7SJohannes Berg {
17*2dbba6f7SJohannes Berg 	struct genl_family	*family;	/* private */
18*2dbba6f7SJohannes Berg 	struct list_head	list;		/* private */
19*2dbba6f7SJohannes Berg 	char			name[GENL_NAMSIZ];
20*2dbba6f7SJohannes Berg 	u32			id;
21*2dbba6f7SJohannes Berg };
22*2dbba6f7SJohannes Berg 
23*2dbba6f7SJohannes Berg /**
24482a8524SThomas Graf  * struct genl_family - generic netlink family
25482a8524SThomas Graf  * @id: protocol family idenfitier
26482a8524SThomas Graf  * @hdrsize: length of user specific header in bytes
27482a8524SThomas Graf  * @name: name of family
28482a8524SThomas Graf  * @version: protocol version
29482a8524SThomas Graf  * @maxattr: maximum number of attributes supported
30482a8524SThomas Graf  * @attrbuf: buffer to store parsed attributes
31482a8524SThomas Graf  * @ops_list: list of all assigned operations
32482a8524SThomas Graf  * @family_list: family list
33*2dbba6f7SJohannes Berg  * @mcast_groups: multicast groups list
34482a8524SThomas Graf  */
35482a8524SThomas Graf struct genl_family
36482a8524SThomas Graf {
37482a8524SThomas Graf 	unsigned int		id;
38482a8524SThomas Graf 	unsigned int		hdrsize;
39482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
40482a8524SThomas Graf 	unsigned int		version;
41482a8524SThomas Graf 	unsigned int		maxattr;
42482a8524SThomas Graf 	struct nlattr **	attrbuf;	/* private */
43482a8524SThomas Graf 	struct list_head	ops_list;	/* private */
44482a8524SThomas Graf 	struct list_head	family_list;	/* private */
45*2dbba6f7SJohannes Berg 	struct list_head	mcast_groups;	/* private */
46482a8524SThomas Graf };
47482a8524SThomas Graf 
48482a8524SThomas Graf /**
49482a8524SThomas Graf  * struct genl_info - receiving information
50482a8524SThomas Graf  * @snd_seq: sending sequence number
51482a8524SThomas Graf  * @snd_pid: netlink pid of sender
52482a8524SThomas Graf  * @nlhdr: netlink message header
53482a8524SThomas Graf  * @genlhdr: generic netlink message header
54482a8524SThomas Graf  * @userhdr: user specific header
55482a8524SThomas Graf  * @attrs: netlink attributes
56482a8524SThomas Graf  */
57482a8524SThomas Graf struct genl_info
58482a8524SThomas Graf {
59482a8524SThomas Graf 	u32			snd_seq;
60482a8524SThomas Graf 	u32			snd_pid;
61482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
62482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
63482a8524SThomas Graf 	void *			userhdr;
64482a8524SThomas Graf 	struct nlattr **	attrs;
65482a8524SThomas Graf };
66482a8524SThomas Graf 
67482a8524SThomas Graf /**
68482a8524SThomas Graf  * struct genl_ops - generic netlink operations
69482a8524SThomas Graf  * @cmd: command identifier
70482a8524SThomas Graf  * @flags: flags
71482a8524SThomas Graf  * @policy: attribute validation policy
72482a8524SThomas Graf  * @doit: standard command callback
73482a8524SThomas Graf  * @dumpit: callback for dumpers
74a4d1366dSJamal Hadi Salim  * @done: completion callback for dumps
75482a8524SThomas Graf  * @ops_list: operations list
76482a8524SThomas Graf  */
77482a8524SThomas Graf struct genl_ops
78482a8524SThomas Graf {
79b461d2f2SPer Liden 	u8			cmd;
80482a8524SThomas Graf 	unsigned int		flags;
81ef7c79edSPatrick McHardy 	const struct nla_policy	*policy;
82482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
83482a8524SThomas Graf 				       struct genl_info *info);
84482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
85482a8524SThomas Graf 					 struct netlink_callback *cb);
86a4d1366dSJamal Hadi Salim 	int		       (*done)(struct netlink_callback *cb);
87482a8524SThomas Graf 	struct list_head	ops_list;
88482a8524SThomas Graf };
89482a8524SThomas Graf 
90482a8524SThomas Graf extern int genl_register_family(struct genl_family *family);
91482a8524SThomas Graf extern int genl_unregister_family(struct genl_family *family);
92482a8524SThomas Graf extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
93482a8524SThomas Graf extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
94*2dbba6f7SJohannes Berg extern int genl_register_mc_group(struct genl_family *family,
95*2dbba6f7SJohannes Berg 				  struct genl_multicast_group *grp);
96*2dbba6f7SJohannes Berg extern void genl_unregister_mc_group(struct genl_family *family,
97*2dbba6f7SJohannes Berg 				     struct genl_multicast_group *grp);
98482a8524SThomas Graf 
99482a8524SThomas Graf extern struct sock *genl_sock;
100482a8524SThomas Graf 
101482a8524SThomas Graf /**
102482a8524SThomas Graf  * genlmsg_put - Add generic netlink header to netlink message
103482a8524SThomas Graf  * @skb: socket buffer holding the message
104482a8524SThomas Graf  * @pid: netlink pid the message is addressed to
105482a8524SThomas Graf  * @seq: sequence number (usually the one of the sender)
10617c157c8SThomas Graf  * @family: generic netlink family
107482a8524SThomas Graf  * @flags netlink message flags
108482a8524SThomas Graf  * @cmd: generic netlink command
109482a8524SThomas Graf  *
110482a8524SThomas Graf  * Returns pointer to user specific header
111482a8524SThomas Graf  */
112482a8524SThomas Graf static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
11317c157c8SThomas Graf 				struct genl_family *family, int flags, u8 cmd)
114482a8524SThomas Graf {
115482a8524SThomas Graf 	struct nlmsghdr *nlh;
116482a8524SThomas Graf 	struct genlmsghdr *hdr;
117482a8524SThomas Graf 
11817c157c8SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
11917c157c8SThomas Graf 			family->hdrsize, flags);
120482a8524SThomas Graf 	if (nlh == NULL)
121482a8524SThomas Graf 		return NULL;
122482a8524SThomas Graf 
123482a8524SThomas Graf 	hdr = nlmsg_data(nlh);
124482a8524SThomas Graf 	hdr->cmd = cmd;
12517c157c8SThomas Graf 	hdr->version = family->version;
126482a8524SThomas Graf 	hdr->reserved = 0;
127482a8524SThomas Graf 
128482a8524SThomas Graf 	return (char *) hdr + GENL_HDRLEN;
129482a8524SThomas Graf }
130482a8524SThomas Graf 
131482a8524SThomas Graf /**
13217c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
13317c157c8SThomas Graf  * @skb: socket buffer holding the message
13417c157c8SThomas Graf  * @info: receiver info
13517c157c8SThomas Graf  * @family: generic netlink family
13617c157c8SThomas Graf  * @flags: netlink message flags
13717c157c8SThomas Graf  * @cmd: generic netlink command
13817c157c8SThomas Graf  *
13917c157c8SThomas Graf  * Returns pointer to user specific header
14017c157c8SThomas Graf  */
14117c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
14217c157c8SThomas Graf 				      struct genl_info *info,
14317c157c8SThomas Graf 				      struct genl_family *family,
14417c157c8SThomas Graf 				      int flags, u8 cmd)
14517c157c8SThomas Graf {
14617c157c8SThomas Graf 	return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
14717c157c8SThomas Graf 			   flags, cmd);
14817c157c8SThomas Graf }
14917c157c8SThomas Graf 
15017c157c8SThomas Graf /**
151482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
152482a8524SThomas Graf  * @skb: socket buffer the message is stored in
153482a8524SThomas Graf  * @hdr: user specific header
154482a8524SThomas Graf  */
155482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
156482a8524SThomas Graf {
157482a8524SThomas Graf 	return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
158482a8524SThomas Graf }
159482a8524SThomas Graf 
160482a8524SThomas Graf /**
161482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
162482a8524SThomas Graf  * @skb: socket buffer the message is stored in
163482a8524SThomas Graf  * @hdr: generic netlink message header
164482a8524SThomas Graf  */
165482a8524SThomas Graf static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
166482a8524SThomas Graf {
167482a8524SThomas Graf 	return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
168482a8524SThomas Graf }
169482a8524SThomas Graf 
170482a8524SThomas Graf /**
171482a8524SThomas Graf  * genlmsg_multicast - multicast a netlink message
172482a8524SThomas Graf  * @skb: netlink message as socket buffer
173482a8524SThomas Graf  * @pid: own netlink pid to avoid sending to yourself
174482a8524SThomas Graf  * @group: multicast group id
175d387f6adSThomas Graf  * @flags: allocation flags
176482a8524SThomas Graf  */
177482a8524SThomas Graf static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
178d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
179482a8524SThomas Graf {
180d387f6adSThomas Graf 	return nlmsg_multicast(genl_sock, skb, pid, group, flags);
181482a8524SThomas Graf }
182482a8524SThomas Graf 
183482a8524SThomas Graf /**
184482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
185482a8524SThomas Graf  * @skb: netlink message as socket buffer
186482a8524SThomas Graf  * @pid: netlink pid of the destination socket
187482a8524SThomas Graf  */
188482a8524SThomas Graf static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
189482a8524SThomas Graf {
190482a8524SThomas Graf 	return nlmsg_unicast(genl_sock, skb, pid);
191482a8524SThomas Graf }
192482a8524SThomas Graf 
193fb0ba6bdSBalbir Singh /**
19481878d27SThomas Graf  * genlmsg_reply - reply to a request
19581878d27SThomas Graf  * @skb: netlink message to be sent back
19681878d27SThomas Graf  * @info: receiver information
19781878d27SThomas Graf  */
19881878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
19981878d27SThomas Graf {
20081878d27SThomas Graf 	return genlmsg_unicast(skb, info->snd_pid);
20181878d27SThomas Graf }
20281878d27SThomas Graf 
20381878d27SThomas Graf /**
204fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
205fb0ba6bdSBalbir Singh  * @gnlh: genetlink messsage header
206fb0ba6bdSBalbir Singh  */
207fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
208fb0ba6bdSBalbir Singh {
209fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
210fb0ba6bdSBalbir Singh }
211fb0ba6bdSBalbir Singh 
212fb0ba6bdSBalbir Singh /**
213fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
214fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
215fb0ba6bdSBalbir Singh  */
216fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
217fb0ba6bdSBalbir Singh {
218fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
219fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
220fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
221fb0ba6bdSBalbir Singh }
222fb0ba6bdSBalbir Singh 
22317db952cSBalbir Singh /**
22417db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
22517db952cSBalbir Singh  * @payload: length of message payload
22617db952cSBalbir Singh  */
22717db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
22817db952cSBalbir Singh {
22917db952cSBalbir Singh 	return GENL_HDRLEN + payload;
23017db952cSBalbir Singh }
23117db952cSBalbir Singh 
23217db952cSBalbir Singh /**
23317db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
23417db952cSBalbir Singh  * @payload: length of message payload
23517db952cSBalbir Singh  */
23617db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
23717db952cSBalbir Singh {
23817db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
23917db952cSBalbir Singh }
24017db952cSBalbir Singh 
2413dabc715SThomas Graf /**
2423dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
2433dabc715SThomas Graf  * @payload: size of the message payload
2443dabc715SThomas Graf  * @flags: the type of memory to allocate.
2453dabc715SThomas Graf  */
2463dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
2473dabc715SThomas Graf {
2483dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
2493dabc715SThomas Graf }
2503dabc715SThomas Graf 
2513dabc715SThomas Graf 
252482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
253