xref: /openbmc/linux/include/net/genetlink.h (revision 3dabc7157859e706770c825aa229f8943db4e0e1)
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)
82482a8524SThomas Graf  * @type: netlink message type
83482a8524SThomas Graf  * @hdrlen: length of the user specific header
84482a8524SThomas Graf  * @flags netlink message flags
85482a8524SThomas Graf  * @cmd: generic netlink command
86482a8524SThomas Graf  * @version: version
87482a8524SThomas Graf  *
88482a8524SThomas Graf  * Returns pointer to user specific header
89482a8524SThomas Graf  */
90482a8524SThomas Graf static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
91482a8524SThomas Graf 				int type, int hdrlen, int flags,
92482a8524SThomas Graf 				u8 cmd, u8 version)
93482a8524SThomas Graf {
94482a8524SThomas Graf 	struct nlmsghdr *nlh;
95482a8524SThomas Graf 	struct genlmsghdr *hdr;
96482a8524SThomas Graf 
97482a8524SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, GENL_HDRLEN + hdrlen, flags);
98482a8524SThomas Graf 	if (nlh == NULL)
99482a8524SThomas Graf 		return NULL;
100482a8524SThomas Graf 
101482a8524SThomas Graf 	hdr = nlmsg_data(nlh);
102482a8524SThomas Graf 	hdr->cmd = cmd;
103482a8524SThomas Graf 	hdr->version = version;
104482a8524SThomas Graf 	hdr->reserved = 0;
105482a8524SThomas Graf 
106482a8524SThomas Graf 	return (char *) hdr + GENL_HDRLEN;
107482a8524SThomas Graf }
108482a8524SThomas Graf 
109482a8524SThomas Graf /**
110482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
111482a8524SThomas Graf  * @skb: socket buffer the message is stored in
112482a8524SThomas Graf  * @hdr: user specific header
113482a8524SThomas Graf  */
114482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
115482a8524SThomas Graf {
116482a8524SThomas Graf 	return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
117482a8524SThomas Graf }
118482a8524SThomas Graf 
119482a8524SThomas Graf /**
120482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
121482a8524SThomas Graf  * @skb: socket buffer the message is stored in
122482a8524SThomas Graf  * @hdr: generic netlink message header
123482a8524SThomas Graf  */
124482a8524SThomas Graf static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
125482a8524SThomas Graf {
126482a8524SThomas Graf 	return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
127482a8524SThomas Graf }
128482a8524SThomas Graf 
129482a8524SThomas Graf /**
130482a8524SThomas Graf  * genlmsg_multicast - multicast a netlink message
131482a8524SThomas Graf  * @skb: netlink message as socket buffer
132482a8524SThomas Graf  * @pid: own netlink pid to avoid sending to yourself
133482a8524SThomas Graf  * @group: multicast group id
134d387f6adSThomas Graf  * @flags: allocation flags
135482a8524SThomas Graf  */
136482a8524SThomas Graf static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
137d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
138482a8524SThomas Graf {
139d387f6adSThomas Graf 	return nlmsg_multicast(genl_sock, skb, pid, group, flags);
140482a8524SThomas Graf }
141482a8524SThomas Graf 
142482a8524SThomas Graf /**
143482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
144482a8524SThomas Graf  * @skb: netlink message as socket buffer
145482a8524SThomas Graf  * @pid: netlink pid of the destination socket
146482a8524SThomas Graf  */
147482a8524SThomas Graf static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
148482a8524SThomas Graf {
149482a8524SThomas Graf 	return nlmsg_unicast(genl_sock, skb, pid);
150482a8524SThomas Graf }
151482a8524SThomas Graf 
152fb0ba6bdSBalbir Singh /**
153fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
154fb0ba6bdSBalbir Singh  * @gnlh: genetlink messsage header
155fb0ba6bdSBalbir Singh  */
156fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
157fb0ba6bdSBalbir Singh {
158fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
159fb0ba6bdSBalbir Singh }
160fb0ba6bdSBalbir Singh 
161fb0ba6bdSBalbir Singh /**
162fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
163fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
164fb0ba6bdSBalbir Singh  */
165fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
166fb0ba6bdSBalbir Singh {
167fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
168fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
169fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
170fb0ba6bdSBalbir Singh }
171fb0ba6bdSBalbir Singh 
17217db952cSBalbir Singh /**
17317db952cSBalbir Singh  * genlmsg_msg_size - length of genetlink message not including padding
17417db952cSBalbir Singh  * @payload: length of message payload
17517db952cSBalbir Singh  */
17617db952cSBalbir Singh static inline int genlmsg_msg_size(int payload)
17717db952cSBalbir Singh {
17817db952cSBalbir Singh 	return GENL_HDRLEN + payload;
17917db952cSBalbir Singh }
18017db952cSBalbir Singh 
18117db952cSBalbir Singh /**
18217db952cSBalbir Singh  * genlmsg_total_size - length of genetlink message including padding
18317db952cSBalbir Singh  * @payload: length of message payload
18417db952cSBalbir Singh  */
18517db952cSBalbir Singh static inline int genlmsg_total_size(int payload)
18617db952cSBalbir Singh {
18717db952cSBalbir Singh 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
18817db952cSBalbir Singh }
18917db952cSBalbir Singh 
190*3dabc715SThomas Graf /**
191*3dabc715SThomas Graf  * genlmsg_new - Allocate a new generic netlink message
192*3dabc715SThomas Graf  * @payload: size of the message payload
193*3dabc715SThomas Graf  * @flags: the type of memory to allocate.
194*3dabc715SThomas Graf  */
195*3dabc715SThomas Graf static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
196*3dabc715SThomas Graf {
197*3dabc715SThomas Graf 	return nlmsg_new(genlmsg_total_size(payload), flags);
198*3dabc715SThomas Graf }
199*3dabc715SThomas Graf 
200*3dabc715SThomas Graf 
201482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
202