xref: /linux/include/net/genetlink.h (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
1 #ifndef __NET_GENERIC_NETLINK_H
2 #define __NET_GENERIC_NETLINK_H
3 
4 #include <linux/genetlink.h>
5 #include <net/netlink.h>
6 
7 /**
8  * struct genl_family - generic netlink family
9  * @id: protocol family idenfitier
10  * @hdrsize: length of user specific header in bytes
11  * @name: name of family
12  * @version: protocol version
13  * @maxattr: maximum number of attributes supported
14  * @attrbuf: buffer to store parsed attributes
15  * @ops_list: list of all assigned operations
16  * @family_list: family list
17  */
18 struct genl_family
19 {
20 	unsigned int		id;
21 	unsigned int		hdrsize;
22 	char			name[GENL_NAMSIZ];
23 	unsigned int		version;
24 	unsigned int		maxattr;
25 	struct nlattr **	attrbuf;	/* private */
26 	struct list_head	ops_list;	/* private */
27 	struct list_head	family_list;	/* private */
28 };
29 
30 /**
31  * struct genl_info - receiving information
32  * @snd_seq: sending sequence number
33  * @snd_pid: netlink pid of sender
34  * @nlhdr: netlink message header
35  * @genlhdr: generic netlink message header
36  * @userhdr: user specific header
37  * @attrs: netlink attributes
38  */
39 struct genl_info
40 {
41 	u32			snd_seq;
42 	u32			snd_pid;
43 	struct nlmsghdr *	nlhdr;
44 	struct genlmsghdr *	genlhdr;
45 	void *			userhdr;
46 	struct nlattr **	attrs;
47 };
48 
49 /**
50  * struct genl_ops - generic netlink operations
51  * @cmd: command identifier
52  * @flags: flags
53  * @policy: attribute validation policy
54  * @doit: standard command callback
55  * @dumpit: callback for dumpers
56  * @done: completion callback for dumps
57  * @ops_list: operations list
58  */
59 struct genl_ops
60 {
61 	u8			cmd;
62 	unsigned int		flags;
63 	struct nla_policy	*policy;
64 	int		       (*doit)(struct sk_buff *skb,
65 				       struct genl_info *info);
66 	int		       (*dumpit)(struct sk_buff *skb,
67 					 struct netlink_callback *cb);
68 	int		       (*done)(struct netlink_callback *cb);
69 	struct list_head	ops_list;
70 };
71 
72 extern int genl_register_family(struct genl_family *family);
73 extern int genl_unregister_family(struct genl_family *family);
74 extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
75 extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
76 
77 extern struct sock *genl_sock;
78 
79 /**
80  * genlmsg_put - Add generic netlink header to netlink message
81  * @skb: socket buffer holding the message
82  * @pid: netlink pid the message is addressed to
83  * @seq: sequence number (usually the one of the sender)
84  * @family: generic netlink family
85  * @flags netlink message flags
86  * @cmd: generic netlink command
87  *
88  * Returns pointer to user specific header
89  */
90 static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
91 				struct genl_family *family, int flags, u8 cmd)
92 {
93 	struct nlmsghdr *nlh;
94 	struct genlmsghdr *hdr;
95 
96 	nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
97 			family->hdrsize, flags);
98 	if (nlh == NULL)
99 		return NULL;
100 
101 	hdr = nlmsg_data(nlh);
102 	hdr->cmd = cmd;
103 	hdr->version = family->version;
104 	hdr->reserved = 0;
105 
106 	return (char *) hdr + GENL_HDRLEN;
107 }
108 
109 /**
110  * genlmsg_put_reply - Add generic netlink header to a reply message
111  * @skb: socket buffer holding the message
112  * @info: receiver info
113  * @family: generic netlink family
114  * @flags: netlink message flags
115  * @cmd: generic netlink command
116  *
117  * Returns pointer to user specific header
118  */
119 static inline void *genlmsg_put_reply(struct sk_buff *skb,
120 				      struct genl_info *info,
121 				      struct genl_family *family,
122 				      int flags, u8 cmd)
123 {
124 	return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
125 			   flags, cmd);
126 }
127 
128 /**
129  * genlmsg_end - Finalize a generic netlink message
130  * @skb: socket buffer the message is stored in
131  * @hdr: user specific header
132  */
133 static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
134 {
135 	return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
136 }
137 
138 /**
139  * genlmsg_cancel - Cancel construction of a generic netlink message
140  * @skb: socket buffer the message is stored in
141  * @hdr: generic netlink message header
142  */
143 static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
144 {
145 	return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
146 }
147 
148 /**
149  * genlmsg_multicast - multicast a netlink message
150  * @skb: netlink message as socket buffer
151  * @pid: own netlink pid to avoid sending to yourself
152  * @group: multicast group id
153  * @flags: allocation flags
154  */
155 static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
156 				    unsigned int group, gfp_t flags)
157 {
158 	return nlmsg_multicast(genl_sock, skb, pid, group, flags);
159 }
160 
161 /**
162  * genlmsg_unicast - unicast a netlink message
163  * @skb: netlink message as socket buffer
164  * @pid: netlink pid of the destination socket
165  */
166 static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
167 {
168 	return nlmsg_unicast(genl_sock, skb, pid);
169 }
170 
171 /**
172  * genlmsg_reply - reply to a request
173  * @skb: netlink message to be sent back
174  * @info: receiver information
175  */
176 static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
177 {
178 	return genlmsg_unicast(skb, info->snd_pid);
179 }
180 
181 /**
182  * gennlmsg_data - head of message payload
183  * @gnlh: genetlink messsage header
184  */
185 static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
186 {
187 	return ((unsigned char *) gnlh + GENL_HDRLEN);
188 }
189 
190 /**
191  * genlmsg_len - length of message payload
192  * @gnlh: genetlink message header
193  */
194 static inline int genlmsg_len(const struct genlmsghdr *gnlh)
195 {
196 	struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
197 							NLMSG_HDRLEN);
198 	return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
199 }
200 
201 /**
202  * genlmsg_msg_size - length of genetlink message not including padding
203  * @payload: length of message payload
204  */
205 static inline int genlmsg_msg_size(int payload)
206 {
207 	return GENL_HDRLEN + payload;
208 }
209 
210 /**
211  * genlmsg_total_size - length of genetlink message including padding
212  * @payload: length of message payload
213  */
214 static inline int genlmsg_total_size(int payload)
215 {
216 	return NLMSG_ALIGN(genlmsg_msg_size(payload));
217 }
218 
219 /**
220  * genlmsg_new - Allocate a new generic netlink message
221  * @payload: size of the message payload
222  * @flags: the type of memory to allocate.
223  */
224 static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
225 {
226 	return nlmsg_new(genlmsg_total_size(payload), flags);
227 }
228 
229 
230 #endif	/* __NET_GENERIC_NETLINK_H */
231