blob: 2a1c06874c42f212970513f585d248c148b58281 [file] [log] [blame]
Thomas Graf482a8522005-11-10 02:25:56 +01001#ifndef __NET_GENERIC_NETLINK_H
2#define __NET_GENERIC_NETLINK_H
3
4#include <linux/genetlink.h>
5#include <net/netlink.h>
Johannes Berg134e6372009-07-10 09:51:34 +00006#include <net/net_namespace.h>
Thomas Graf482a8522005-11-10 02:25:56 +01007
8/**
Johannes Berg2dbba6f2007-07-18 15:47:52 -07009 * struct genl_multicast_group - generic netlink multicast group
10 * @name: name of the multicast group, names are per-family
11 * @id: multicast group ID, assigned by the core, to use with
12 * genlmsg_multicast().
13 * @list: list entry for linking
14 * @family: pointer to family, need not be set before registering
15 */
16struct genl_multicast_group
17{
18 struct genl_family *family; /* private */
19 struct list_head list; /* private */
20 char name[GENL_NAMSIZ];
21 u32 id;
22};
23
24/**
Thomas Graf482a8522005-11-10 02:25:56 +010025 * struct genl_family - generic netlink family
26 * @id: protocol family idenfitier
27 * @hdrsize: length of user specific header in bytes
28 * @name: name of family
29 * @version: protocol version
30 * @maxattr: maximum number of attributes supported
Johannes Berg134e6372009-07-10 09:51:34 +000031 * @netnsok: set to true if the family can handle network
32 * namespaces and should be presented in all of them
Thomas Graf482a8522005-11-10 02:25:56 +010033 * @attrbuf: buffer to store parsed attributes
34 * @ops_list: list of all assigned operations
35 * @family_list: family list
Johannes Berg2dbba6f2007-07-18 15:47:52 -070036 * @mcast_groups: multicast groups list
Thomas Graf482a8522005-11-10 02:25:56 +010037 */
38struct genl_family
39{
40 unsigned int id;
41 unsigned int hdrsize;
42 char name[GENL_NAMSIZ];
43 unsigned int version;
44 unsigned int maxattr;
Johannes Berg134e6372009-07-10 09:51:34 +000045 bool netnsok;
Thomas Graf482a8522005-11-10 02:25:56 +010046 struct nlattr ** attrbuf; /* private */
47 struct list_head ops_list; /* private */
48 struct list_head family_list; /* private */
Johannes Berg2dbba6f2007-07-18 15:47:52 -070049 struct list_head mcast_groups; /* private */
Thomas Graf482a8522005-11-10 02:25:56 +010050};
51
Thomas Graf482a8522005-11-10 02:25:56 +010052/**
53 * struct genl_info - receiving information
54 * @snd_seq: sending sequence number
55 * @snd_pid: netlink pid of sender
56 * @nlhdr: netlink message header
57 * @genlhdr: generic netlink message header
58 * @userhdr: user specific header
59 * @attrs: netlink attributes
60 */
61struct genl_info
62{
63 u32 snd_seq;
64 u32 snd_pid;
65 struct nlmsghdr * nlhdr;
66 struct genlmsghdr * genlhdr;
67 void * userhdr;
68 struct nlattr ** attrs;
Johannes Berg134e6372009-07-10 09:51:34 +000069#ifdef CONFIG_NET_NS
70 struct net * _net;
71#endif
Thomas Graf482a8522005-11-10 02:25:56 +010072};
73
Johannes Berg134e6372009-07-10 09:51:34 +000074#ifdef CONFIG_NET_NS
75static inline struct net *genl_info_net(struct genl_info *info)
76{
77 return info->_net;
78}
79
80static inline void genl_info_net_set(struct genl_info *info, struct net *net)
81{
82 info->_net = net;
83}
84#else
85static inline struct net *genl_info_net(struct genl_info *info)
86{
87 return &init_net;
88}
89
90static inline void genl_info_net_set(struct genl_info *info, struct net *net)
91{
92}
93#endif
94
Thomas Graf482a8522005-11-10 02:25:56 +010095/**
96 * struct genl_ops - generic netlink operations
97 * @cmd: command identifier
98 * @flags: flags
99 * @policy: attribute validation policy
100 * @doit: standard command callback
101 * @dumpit: callback for dumpers
Jamal Hadi Salima4d13662006-12-01 20:07:42 -0800102 * @done: completion callback for dumps
Thomas Graf482a8522005-11-10 02:25:56 +0100103 * @ops_list: operations list
104 */
105struct genl_ops
106{
Per Lidenb461d2f22006-01-03 14:13:29 -0800107 u8 cmd;
Thomas Graf482a8522005-11-10 02:25:56 +0100108 unsigned int flags;
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700109 const struct nla_policy *policy;
Thomas Graf482a8522005-11-10 02:25:56 +0100110 int (*doit)(struct sk_buff *skb,
111 struct genl_info *info);
112 int (*dumpit)(struct sk_buff *skb,
113 struct netlink_callback *cb);
Jamal Hadi Salima4d13662006-12-01 20:07:42 -0800114 int (*done)(struct netlink_callback *cb);
Thomas Graf482a8522005-11-10 02:25:56 +0100115 struct list_head ops_list;
116};
117
118extern int genl_register_family(struct genl_family *family);
Michał Mirosława7b11d72009-05-21 10:34:04 +0000119extern int genl_register_family_with_ops(struct genl_family *family,
120 struct genl_ops *ops, size_t n_ops);
Thomas Graf482a8522005-11-10 02:25:56 +0100121extern int genl_unregister_family(struct genl_family *family);
122extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
123extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700124extern int genl_register_mc_group(struct genl_family *family,
125 struct genl_multicast_group *grp);
126extern void genl_unregister_mc_group(struct genl_family *family,
127 struct genl_multicast_group *grp);
Thomas Graf482a8522005-11-10 02:25:56 +0100128
Thomas Graf482a8522005-11-10 02:25:56 +0100129/**
130 * genlmsg_put - Add generic netlink header to netlink message
131 * @skb: socket buffer holding the message
132 * @pid: netlink pid the message is addressed to
133 * @seq: sequence number (usually the one of the sender)
Thomas Graf17c157c2006-11-14 19:46:02 -0800134 * @family: generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +0100135 * @flags netlink message flags
136 * @cmd: generic netlink command
Thomas Graf482a8522005-11-10 02:25:56 +0100137 *
138 * Returns pointer to user specific header
139 */
140static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
Thomas Graf17c157c2006-11-14 19:46:02 -0800141 struct genl_family *family, int flags, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100142{
143 struct nlmsghdr *nlh;
144 struct genlmsghdr *hdr;
145
Thomas Graf17c157c2006-11-14 19:46:02 -0800146 nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
147 family->hdrsize, flags);
Thomas Graf482a8522005-11-10 02:25:56 +0100148 if (nlh == NULL)
149 return NULL;
150
151 hdr = nlmsg_data(nlh);
152 hdr->cmd = cmd;
Thomas Graf17c157c2006-11-14 19:46:02 -0800153 hdr->version = family->version;
Thomas Graf482a8522005-11-10 02:25:56 +0100154 hdr->reserved = 0;
155
156 return (char *) hdr + GENL_HDRLEN;
157}
158
159/**
Thomas Graf17c157c2006-11-14 19:46:02 -0800160 * genlmsg_put_reply - Add generic netlink header to a reply message
161 * @skb: socket buffer holding the message
162 * @info: receiver info
163 * @family: generic netlink family
164 * @flags: netlink message flags
165 * @cmd: generic netlink command
166 *
167 * Returns pointer to user specific header
168 */
169static inline void *genlmsg_put_reply(struct sk_buff *skb,
170 struct genl_info *info,
171 struct genl_family *family,
172 int flags, u8 cmd)
173{
174 return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
175 flags, cmd);
176}
177
178/**
Thomas Graf482a8522005-11-10 02:25:56 +0100179 * genlmsg_end - Finalize a generic netlink message
180 * @skb: socket buffer the message is stored in
181 * @hdr: user specific header
182 */
183static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
184{
185 return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
186}
187
188/**
189 * genlmsg_cancel - Cancel construction of a generic netlink message
190 * @skb: socket buffer the message is stored in
191 * @hdr: generic netlink message header
192 */
Thomas Grafbc3ed282008-06-03 16:36:54 -0700193static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
Thomas Graf482a8522005-11-10 02:25:56 +0100194{
Thomas Grafbc3ed282008-06-03 16:36:54 -0700195 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
Thomas Graf482a8522005-11-10 02:25:56 +0100196}
197
198/**
Johannes Berg134e6372009-07-10 09:51:34 +0000199 * genlmsg_multicast_netns - multicast a netlink message to a specific netns
200 * @net: the net namespace
201 * @skb: netlink message as socket buffer
202 * @pid: own netlink pid to avoid sending to yourself
203 * @group: multicast group id
204 * @flags: allocation flags
205 */
206static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb,
207 u32 pid, unsigned int group, gfp_t flags)
208{
209 return nlmsg_multicast(net->genl_sock, skb, pid, group, flags);
210}
211
212/**
213 * genlmsg_multicast - multicast a netlink message to the default netns
Thomas Graf482a8522005-11-10 02:25:56 +0100214 * @skb: netlink message as socket buffer
215 * @pid: own netlink pid to avoid sending to yourself
216 * @group: multicast group id
Thomas Grafd387f6a2006-08-15 00:31:06 -0700217 * @flags: allocation flags
Thomas Graf482a8522005-11-10 02:25:56 +0100218 */
219static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
Thomas Grafd387f6a2006-08-15 00:31:06 -0700220 unsigned int group, gfp_t flags)
Thomas Graf482a8522005-11-10 02:25:56 +0100221{
Johannes Berg134e6372009-07-10 09:51:34 +0000222 return genlmsg_multicast_netns(&init_net, skb, pid, group, flags);
Thomas Graf482a8522005-11-10 02:25:56 +0100223}
224
225/**
Johannes Berg134e6372009-07-10 09:51:34 +0000226 * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
227 * @skb: netlink message as socket buffer
228 * @pid: own netlink pid to avoid sending to yourself
229 * @group: multicast group id
230 * @flags: allocation flags
231 *
232 * This function must hold the RTNL or rcu_read_lock().
233 */
234int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid,
235 unsigned int group, gfp_t flags);
236
237/**
Thomas Graf482a8522005-11-10 02:25:56 +0100238 * genlmsg_unicast - unicast a netlink message
239 * @skb: netlink message as socket buffer
240 * @pid: netlink pid of the destination socket
241 */
Johannes Berg134e6372009-07-10 09:51:34 +0000242static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid)
Thomas Graf482a8522005-11-10 02:25:56 +0100243{
Johannes Berg134e6372009-07-10 09:51:34 +0000244 return nlmsg_unicast(net->genl_sock, skb, pid);
Thomas Graf482a8522005-11-10 02:25:56 +0100245}
246
Balbir Singhfb0ba6bd2006-07-14 00:24:39 -0700247/**
Thomas Graf81878d22006-11-14 19:45:27 -0800248 * genlmsg_reply - reply to a request
249 * @skb: netlink message to be sent back
250 * @info: receiver information
251 */
252static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
253{
Johannes Berg134e6372009-07-10 09:51:34 +0000254 return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
Thomas Graf81878d22006-11-14 19:45:27 -0800255}
256
257/**
Balbir Singhfb0ba6bd2006-07-14 00:24:39 -0700258 * gennlmsg_data - head of message payload
259 * @gnlh: genetlink messsage header
260 */
261static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
262{
263 return ((unsigned char *) gnlh + GENL_HDRLEN);
264}
265
266/**
267 * genlmsg_len - length of message payload
268 * @gnlh: genetlink message header
269 */
270static inline int genlmsg_len(const struct genlmsghdr *gnlh)
271{
272 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
273 NLMSG_HDRLEN);
274 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
275}
276
Balbir Singh17db9522006-09-30 23:28:51 -0700277/**
278 * genlmsg_msg_size - length of genetlink message not including padding
279 * @payload: length of message payload
280 */
281static inline int genlmsg_msg_size(int payload)
282{
283 return GENL_HDRLEN + payload;
284}
285
286/**
287 * genlmsg_total_size - length of genetlink message including padding
288 * @payload: length of message payload
289 */
290static inline int genlmsg_total_size(int payload)
291{
292 return NLMSG_ALIGN(genlmsg_msg_size(payload));
293}
294
Thomas Graf3dabc712006-11-14 19:44:52 -0800295/**
296 * genlmsg_new - Allocate a new generic netlink message
297 * @payload: size of the message payload
298 * @flags: the type of memory to allocate.
299 */
300static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
301{
302 return nlmsg_new(genlmsg_total_size(payload), flags);
303}
304
305
Thomas Graf482a8522005-11-10 02:25:56 +0100306#endif /* __NET_GENERIC_NETLINK_H */