xref: /openbmc/linux/include/net/genetlink.h (revision d387f6ad10764fc2174373b4a1cca443adee36e3)
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 #define GENL_ADMIN_PERM		0x01
31482a8524SThomas Graf 
32482a8524SThomas Graf /**
33482a8524SThomas Graf  * struct genl_info - receiving information
34482a8524SThomas Graf  * @snd_seq: sending sequence number
35482a8524SThomas Graf  * @snd_pid: netlink pid of sender
36482a8524SThomas Graf  * @nlhdr: netlink message header
37482a8524SThomas Graf  * @genlhdr: generic netlink message header
38482a8524SThomas Graf  * @userhdr: user specific header
39482a8524SThomas Graf  * @attrs: netlink attributes
40482a8524SThomas Graf  */
41482a8524SThomas Graf struct genl_info
42482a8524SThomas Graf {
43482a8524SThomas Graf 	u32			snd_seq;
44482a8524SThomas Graf 	u32			snd_pid;
45482a8524SThomas Graf 	struct nlmsghdr *	nlhdr;
46482a8524SThomas Graf 	struct genlmsghdr *	genlhdr;
47482a8524SThomas Graf 	void *			userhdr;
48482a8524SThomas Graf 	struct nlattr **	attrs;
49482a8524SThomas Graf };
50482a8524SThomas Graf 
51482a8524SThomas Graf /**
52482a8524SThomas Graf  * struct genl_ops - generic netlink operations
53482a8524SThomas Graf  * @cmd: command identifier
54482a8524SThomas Graf  * @flags: flags
55482a8524SThomas Graf  * @policy: attribute validation policy
56482a8524SThomas Graf  * @doit: standard command callback
57482a8524SThomas Graf  * @dumpit: callback for dumpers
58482a8524SThomas Graf  * @ops_list: operations list
59482a8524SThomas Graf  */
60482a8524SThomas Graf struct genl_ops
61482a8524SThomas Graf {
62b461d2f2SPer Liden 	u8			cmd;
63482a8524SThomas Graf 	unsigned int		flags;
64482a8524SThomas Graf 	struct nla_policy	*policy;
65482a8524SThomas Graf 	int		       (*doit)(struct sk_buff *skb,
66482a8524SThomas Graf 				       struct genl_info *info);
67482a8524SThomas Graf 	int		       (*dumpit)(struct sk_buff *skb,
68482a8524SThomas Graf 					 struct netlink_callback *cb);
69482a8524SThomas Graf 	struct list_head	ops_list;
70482a8524SThomas Graf };
71482a8524SThomas Graf 
72482a8524SThomas Graf extern int genl_register_family(struct genl_family *family);
73482a8524SThomas Graf extern int genl_unregister_family(struct genl_family *family);
74482a8524SThomas Graf extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
75482a8524SThomas Graf extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
76482a8524SThomas Graf 
77482a8524SThomas Graf extern struct sock *genl_sock;
78482a8524SThomas Graf 
79482a8524SThomas Graf /**
80482a8524SThomas Graf  * genlmsg_put - Add generic netlink header to netlink message
81482a8524SThomas Graf  * @skb: socket buffer holding the message
82482a8524SThomas Graf  * @pid: netlink pid the message is addressed to
83482a8524SThomas Graf  * @seq: sequence number (usually the one of the sender)
84482a8524SThomas Graf  * @type: netlink message type
85482a8524SThomas Graf  * @hdrlen: length of the user specific header
86482a8524SThomas Graf  * @flags netlink message flags
87482a8524SThomas Graf  * @cmd: generic netlink command
88482a8524SThomas Graf  * @version: version
89482a8524SThomas Graf  *
90482a8524SThomas Graf  * Returns pointer to user specific header
91482a8524SThomas Graf  */
92482a8524SThomas Graf static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
93482a8524SThomas Graf 				int type, int hdrlen, int flags,
94482a8524SThomas Graf 				u8 cmd, u8 version)
95482a8524SThomas Graf {
96482a8524SThomas Graf 	struct nlmsghdr *nlh;
97482a8524SThomas Graf 	struct genlmsghdr *hdr;
98482a8524SThomas Graf 
99482a8524SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, GENL_HDRLEN + hdrlen, flags);
100482a8524SThomas Graf 	if (nlh == NULL)
101482a8524SThomas Graf 		return NULL;
102482a8524SThomas Graf 
103482a8524SThomas Graf 	hdr = nlmsg_data(nlh);
104482a8524SThomas Graf 	hdr->cmd = cmd;
105482a8524SThomas Graf 	hdr->version = version;
106482a8524SThomas Graf 	hdr->reserved = 0;
107482a8524SThomas Graf 
108482a8524SThomas Graf 	return (char *) hdr + GENL_HDRLEN;
109482a8524SThomas Graf }
110482a8524SThomas Graf 
111482a8524SThomas Graf /**
112482a8524SThomas Graf  * genlmsg_end - Finalize a generic netlink message
113482a8524SThomas Graf  * @skb: socket buffer the message is stored in
114482a8524SThomas Graf  * @hdr: user specific header
115482a8524SThomas Graf  */
116482a8524SThomas Graf static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
117482a8524SThomas Graf {
118482a8524SThomas Graf 	return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
119482a8524SThomas Graf }
120482a8524SThomas Graf 
121482a8524SThomas Graf /**
122482a8524SThomas Graf  * genlmsg_cancel - Cancel construction of a generic netlink message
123482a8524SThomas Graf  * @skb: socket buffer the message is stored in
124482a8524SThomas Graf  * @hdr: generic netlink message header
125482a8524SThomas Graf  */
126482a8524SThomas Graf static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
127482a8524SThomas Graf {
128482a8524SThomas Graf 	return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
129482a8524SThomas Graf }
130482a8524SThomas Graf 
131482a8524SThomas Graf /**
132482a8524SThomas Graf  * genlmsg_multicast - multicast a netlink message
133482a8524SThomas Graf  * @skb: netlink message as socket buffer
134482a8524SThomas Graf  * @pid: own netlink pid to avoid sending to yourself
135482a8524SThomas Graf  * @group: multicast group id
136*d387f6adSThomas Graf  * @flags: allocation flags
137482a8524SThomas Graf  */
138482a8524SThomas Graf static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
139*d387f6adSThomas Graf 				    unsigned int group, gfp_t flags)
140482a8524SThomas Graf {
141*d387f6adSThomas Graf 	return nlmsg_multicast(genl_sock, skb, pid, group, flags);
142482a8524SThomas Graf }
143482a8524SThomas Graf 
144482a8524SThomas Graf /**
145482a8524SThomas Graf  * genlmsg_unicast - unicast a netlink message
146482a8524SThomas Graf  * @skb: netlink message as socket buffer
147482a8524SThomas Graf  * @pid: netlink pid of the destination socket
148482a8524SThomas Graf  */
149482a8524SThomas Graf static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
150482a8524SThomas Graf {
151482a8524SThomas Graf 	return nlmsg_unicast(genl_sock, skb, pid);
152482a8524SThomas Graf }
153482a8524SThomas Graf 
154fb0ba6bdSBalbir Singh /**
155fb0ba6bdSBalbir Singh  * gennlmsg_data - head of message payload
156fb0ba6bdSBalbir Singh  * @gnlh: genetlink messsage header
157fb0ba6bdSBalbir Singh  */
158fb0ba6bdSBalbir Singh static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
159fb0ba6bdSBalbir Singh {
160fb0ba6bdSBalbir Singh 	return ((unsigned char *) gnlh + GENL_HDRLEN);
161fb0ba6bdSBalbir Singh }
162fb0ba6bdSBalbir Singh 
163fb0ba6bdSBalbir Singh /**
164fb0ba6bdSBalbir Singh  * genlmsg_len - length of message payload
165fb0ba6bdSBalbir Singh  * @gnlh: genetlink message header
166fb0ba6bdSBalbir Singh  */
167fb0ba6bdSBalbir Singh static inline int genlmsg_len(const struct genlmsghdr *gnlh)
168fb0ba6bdSBalbir Singh {
169fb0ba6bdSBalbir Singh 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
170fb0ba6bdSBalbir Singh 							NLMSG_HDRLEN);
171fb0ba6bdSBalbir Singh 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
172fb0ba6bdSBalbir Singh }
173fb0ba6bdSBalbir Singh 
174482a8524SThomas Graf #endif	/* __NET_GENERIC_NETLINK_H */
175