xref: /openbmc/linux/include/net/genetlink.h (revision 17c157c889f4b07258af6bfec9e4e9dcf3c00178)
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 /**
8482a8524SThomas Graf  * struct genl_family - generic netlink family
9482a8524SThomas Graf  * @id: protocol family idenfitier
10482a8524SThomas Graf  * @hdrsize: length of user specific header in bytes
11482a8524SThomas Graf  * @name: name of family
12482a8524SThomas Graf  * @version: protocol version
13482a8524SThomas Graf  * @maxattr: maximum number of attributes supported
14482a8524SThomas Graf  * @attrbuf: buffer to store parsed attributes
15482a8524SThomas Graf  * @ops_list: list of all assigned operations
16482a8524SThomas Graf  * @family_list: family list
17482a8524SThomas Graf  */
18482a8524SThomas Graf struct genl_family
19482a8524SThomas Graf {
20482a8524SThomas Graf 	unsigned int		id;
21482a8524SThomas Graf 	unsigned int		hdrsize;
22482a8524SThomas Graf 	char			name[GENL_NAMSIZ];
23482a8524SThomas Graf 	unsigned int		version;
24482a8524SThomas Graf 	unsigned int		maxattr;
25482a8524SThomas Graf 	struct nlattr **	attrbuf;	/* private */
26482a8524SThomas Graf 	struct list_head	ops_list;	/* private */
27482a8524SThomas Graf 	struct list_head	family_list;	/* private */
28482a8524SThomas Graf };
29482a8524SThomas Graf 
30482a8524SThomas Graf /**
31482a8524SThomas Graf  * struct genl_info - receiving information
32482a8524SThomas Graf  * @snd_seq: sending sequence number
33482a8524SThomas Graf  * @snd_pid: netlink pid of sender
34482a8524SThomas Graf  * @nlhdr: netlink message header
35482a8524SThomas Graf  * @genlhdr: generic netlink message header
36482a8524SThomas Graf  * @userhdr: user specific header
37482a8524SThomas Graf  * @attrs: netlink attributes
38482a8524SThomas Graf  */
39482a8524SThomas Graf struct genl_info
40482a8524SThomas Graf {
41482a8524SThomas Graf 	u32			snd_seq;
42482a8524SThomas Graf 	u32			snd_pid;
43482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
44482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
45482a8524SThomas Graf 	void *			userhdr;
46482a8524SThomas Graf 	struct nlattr **	attrs;
47482a8524SThomas Graf };
48482a8524SThomas Graf 
49482a8524SThomas Graf /**
50482a8524SThomas Graf  * struct genl_ops - generic netlink operations
51482a8524SThomas Graf  * @cmd: command identifier
52482a8524SThomas Graf  * @flags: flags
53482a8524SThomas Graf  * @policy: attribute validation policy
54482a8524SThomas Graf  * @doit: standard command callback
55482a8524SThomas Graf  * @dumpit: callback for dumpers
56482a8524SThomas Graf  * @ops_list: operations list
57482a8524SThomas Graf  */
58482a8524SThomas Graf struct genl_ops
59482a8524SThomas Graf {
60b461d2f2SPer Liden 	u8			cmd;
61482a8524SThomas Graf 	unsigned int		flags;
62482a8524SThomas Graf 	struct nla_policy	*policy;
63482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
64482a8524SThomas Graf 				       struct genl_info *info);
65482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
66482a8524SThomas Graf 					 struct netlink_callback *cb);
67482a8524SThomas Graf 	struct list_head	ops_list;
68482a8524SThomas Graf };
69482a8524SThomas Graf 
70482a8524SThomas Graf extern int genl_register_family(struct genl_family *family);
71482a8524SThomas Graf extern int genl_unregister_family(struct genl_family *family);
72482a8524SThomas Graf extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
73482a8524SThomas Graf extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
74482a8524SThomas Graf 
75482a8524SThomas Graf extern struct sock *genl_sock;
76482a8524SThomas Graf 
77482a8524SThomas Graf /**
78482a8524SThomas Graf  * genlmsg_put - Add generic netlink header to netlink message
79482a8524SThomas Graf  * @skb: socket buffer holding the message
80482a8524SThomas Graf  * @pid: netlink pid the message is addressed to
81482a8524SThomas Graf  * @seq: sequence number (usually the one of the sender)
82*17c157c8SThomas Graf  * @family: generic netlink family
83482a8524SThomas Graf  * @flags netlink message flags
84482a8524SThomas Graf  * @cmd: generic netlink command
85482a8524SThomas Graf  *
86482a8524SThomas Graf  * Returns pointer to user specific header
87482a8524SThomas Graf  */
88482a8524SThomas Graf static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
89*17c157c8SThomas Graf 				struct genl_family *family, int flags, u8 cmd)
90482a8524SThomas Graf {
91482a8524SThomas Graf 	struct nlmsghdr *nlh;
92482a8524SThomas Graf 	struct genlmsghdr *hdr;
93482a8524SThomas Graf 
94*17c157c8SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
95*17c157c8SThomas Graf 			family->hdrsize, flags);
96482a8524SThomas Graf 	if (nlh == NULL)
97482a8524SThomas Graf 		return NULL;
98482a8524SThomas Graf 
99482a8524SThomas Graf 	hdr = nlmsg_data(nlh);
100482a8524SThomas Graf 	hdr->cmd = cmd;
101*17c157c8SThomas Graf 	hdr->version = family->version;
102482a8524SThomas Graf 	hdr->reserved = 0;
103482a8524SThomas Graf 
104482a8524SThomas Graf 	return (char *) hdr + GENL_HDRLEN;
105482a8524SThomas Graf }
106482a8524SThomas Graf 
107482a8524SThomas Graf /**
108*17c157c8SThomas Graf  * genlmsg_put_reply - Add generic netlink header to a reply message
109*17c157c8SThomas Graf  * @skb: socket buffer holding the message
110*17c157c8SThomas Graf  * @info: receiver info
111*17c157c8SThomas Graf  * @family: generic netlink family
112*17c157c8SThomas Graf  * @flags: netlink message flags
113*17c157c8SThomas Graf  * @cmd: generic netlink command
114*17c157c8SThomas Graf  *
115*17c157c8SThomas Graf  * Returns pointer to user specific header
116*17c157c8SThomas Graf  */
117*17c157c8SThomas Graf static inline void *genlmsg_put_reply(struct sk_buff *skb,
118*17c157c8SThomas Graf 				      struct genl_info *info,
119*17c157c8SThomas Graf 				      struct genl_family *family,
120*17c157c8SThomas Graf 				      int flags, u8 cmd)
121*17c157c8SThomas Graf {
122*17c157c8SThomas Graf 	return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
123*17c157c8SThomas Graf 			   flags, cmd);
124*17c157c8SThomas Graf }
125*17c157c8SThomas Graf 
126*17c157c8SThomas Graf /**
127482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
128482a8524SThomas Graf  * @skb: socket buffer the message is stored in
129482a8524SThomas Graf  * @hdr: user specific header
130482a8524SThomas Graf  */
131482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
132482a8524SThomas Graf {
133482a8524SThomas Graf 	return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
134482a8524SThomas Graf }
135482a8524SThomas Graf 
136482a8524SThomas Graf /**
137482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
138482a8524SThomas Graf  * @skb: socket buffer the message is stored in
139482a8524SThomas Graf  * @hdr: generic netlink message header
140482a8524SThomas Graf  */
141482a8524SThomas Graf static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
142482a8524SThomas Graf {
143482a8524SThomas Graf 	return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
144482a8524SThomas Graf }
145482a8524SThomas Graf 
146482a8524SThomas Graf /**
147482a8524SThomas Graf  * genlmsg_multicast - multicast a netlink message
148482a8524SThomas Graf  * @skb: netlink message as socket buffer
149482a8524SThomas Graf  * @pid: own netlink pid to avoid sending to yourself
150482a8524SThomas Graf  * @group: multicast group id
151d387f6adSThomas Graf  * @flags: allocation flags
152482a8524SThomas Graf  */
153482a8524SThomas Graf static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
154d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
155482a8524SThomas Graf {
156d387f6adSThomas Graf 	return nlmsg_multicast(genl_sock, skb, pid, group, flags);
157482a8524SThomas Graf }
158482a8524SThomas Graf 
159482a8524SThomas Graf /**
160482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
161482a8524SThomas Graf  * @skb: netlink message as socket buffer
162482a8524SThomas Graf  * @pid: netlink pid of the destination socket
163482a8524SThomas Graf  */
164482a8524SThomas Graf static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
165482a8524SThomas Graf {
166482a8524SThomas Graf 	return nlmsg_unicast(genl_sock, skb, pid);
167482a8524SThomas Graf }
168482a8524SThomas Graf 
169fb0ba6bdSBalbir Singh /**
17081878d27SThomas Graf  * genlmsg_reply - reply to a request
17181878d27SThomas Graf  * @skb: netlink message to be sent back
17281878d27SThomas Graf  * @info: receiver information
17381878d27SThomas Graf  */
17481878d27SThomas Graf static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
17581878d27SThomas Graf {
17681878d27SThomas Graf 	return genlmsg_unicast(skb, info->snd_pid);
17781878d27SThomas Graf }
17881878d27SThomas Graf 
17981878d27SThomas Graf /**
180fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
181fb0ba6bdSBalbir Singh  * @gnlh: genetlink messsage header
182fb0ba6bdSBalbir Singh  */
183fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
184fb0ba6bdSBalbir Singh {
185fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
186fb0ba6bdSBalbir Singh }
187fb0ba6bdSBalbir Singh 
188fb0ba6bdSBalbir Singh /**
189fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
190fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
191fb0ba6bdSBalbir Singh  */
192fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
193fb0ba6bdSBalbir Singh {
194fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
195fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
196fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
197fb0ba6bdSBalbir Singh }
198fb0ba6bdSBalbir Singh 
19917db952cSBalbir Singh /**
20017db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
20117db952cSBalbir Singh  * @payload: length of message payload
20217db952cSBalbir Singh  */
20317db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
20417db952cSBalbir Singh {
20517db952cSBalbir Singh 	return GENL_HDRLEN + payload;
20617db952cSBalbir Singh }
20717db952cSBalbir Singh 
20817db952cSBalbir Singh /**
20917db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
21017db952cSBalbir Singh  * @payload: length of message payload
21117db952cSBalbir Singh  */
21217db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
21317db952cSBalbir Singh {
21417db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
21517db952cSBalbir Singh }
21617db952cSBalbir Singh 
2173dabc715SThomas Graf /**
2183dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
2193dabc715SThomas Graf  * @payload: size of the message payload
2203dabc715SThomas Graf  * @flags: the type of memory to allocate.
2213dabc715SThomas Graf  */
2223dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
2233dabc715SThomas Graf {
2243dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
2253dabc715SThomas Graf }
2263dabc715SThomas Graf 
2273dabc715SThomas Graf 
228482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
229