1#ifndef __NET_GENERIC_NETLINK_H
2#define __NET_GENERIC_NETLINK_H
3
4#include <linux/genetlink.h>
5#include <net/netlink.h>
6#include <net/net_namespace.h>
7
8
9
10
11
12
13
14
15
16struct genl_multicast_group {
17 struct genl_family *family;
18 struct list_head list;
19 char name[GENL_NAMSIZ];
20 u32 id;
21};
22
23struct genl_ops;
24struct genl_info;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44struct genl_family {
45 unsigned int id;
46 unsigned int hdrsize;
47 char name[GENL_NAMSIZ];
48 unsigned int version;
49 unsigned int maxattr;
50 bool netnsok;
51 int (*pre_doit)(struct genl_ops *ops,
52 struct sk_buff *skb,
53 struct genl_info *info);
54 void (*post_doit)(struct genl_ops *ops,
55 struct sk_buff *skb,
56 struct genl_info *info);
57 struct nlattr ** attrbuf;
58 struct list_head ops_list;
59 struct list_head family_list;
60 struct list_head mcast_groups;
61};
62
63
64
65
66
67
68
69
70
71
72
73
74struct genl_info {
75 u32 snd_seq;
76 u32 snd_pid;
77 struct nlmsghdr * nlhdr;
78 struct genlmsghdr * genlhdr;
79 void * userhdr;
80 struct nlattr ** attrs;
81#ifdef CONFIG_NET_NS
82 struct net * _net;
83#endif
84 void * user_ptr[2];
85};
86
87static inline struct net *genl_info_net(struct genl_info *info)
88{
89 return read_pnet(&info->_net);
90}
91
92static inline void genl_info_net_set(struct genl_info *info, struct net *net)
93{
94 write_pnet(&info->_net, net);
95}
96
97
98
99
100
101
102
103
104
105
106
107
108struct genl_ops {
109 u8 cmd;
110 u8 internal_flags;
111 unsigned int flags;
112 const struct nla_policy *policy;
113 int (*doit)(struct sk_buff *skb,
114 struct genl_info *info);
115 int (*dumpit)(struct sk_buff *skb,
116 struct netlink_callback *cb);
117 int (*done)(struct netlink_callback *cb);
118 struct list_head ops_list;
119};
120
121extern int genl_register_family(struct genl_family *family);
122extern int genl_register_family_with_ops(struct genl_family *family,
123 struct genl_ops *ops, size_t n_ops);
124extern int genl_unregister_family(struct genl_family *family);
125extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
126extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
127extern int genl_register_mc_group(struct genl_family *family,
128 struct genl_multicast_group *grp);
129extern void genl_unregister_mc_group(struct genl_family *family,
130 struct genl_multicast_group *grp);
131
132
133
134
135
136
137
138
139
140
141
142
143static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
144 struct genl_family *family, int flags, u8 cmd)
145{
146 struct nlmsghdr *nlh;
147 struct genlmsghdr *hdr;
148
149 nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
150 family->hdrsize, flags);
151 if (nlh == NULL)
152 return NULL;
153
154 hdr = nlmsg_data(nlh);
155 hdr->cmd = cmd;
156 hdr->version = family->version;
157 hdr->reserved = 0;
158
159 return (char *) hdr + GENL_HDRLEN;
160}
161
162
163
164
165
166
167
168
169
170
171
172static inline void *genlmsg_put_reply(struct sk_buff *skb,
173 struct genl_info *info,
174 struct genl_family *family,
175 int flags, u8 cmd)
176{
177 return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
178 flags, cmd);
179}
180
181
182
183
184
185
186static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
187{
188 return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
189}
190
191
192
193
194
195
196static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
197{
198 if (hdr)
199 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
200}
201
202
203
204
205
206
207
208
209
210static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb,
211 u32 pid, unsigned int group, gfp_t flags)
212{
213 return nlmsg_multicast(net->genl_sock, skb, pid, group, flags);
214}
215
216
217
218
219
220
221
222
223static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
224 unsigned int group, gfp_t flags)
225{
226 return genlmsg_multicast_netns(&init_net, skb, pid, group, flags);
227}
228
229
230
231
232
233
234
235
236
237
238int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid,
239 unsigned int group, gfp_t flags);
240
241
242
243
244
245
246static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid)
247{
248 return nlmsg_unicast(net->genl_sock, skb, pid);
249}
250
251
252
253
254
255
256static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
257{
258 return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
259}
260
261
262
263
264
265static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
266{
267 return ((unsigned char *) gnlh + GENL_HDRLEN);
268}
269
270
271
272
273
274static inline int genlmsg_len(const struct genlmsghdr *gnlh)
275{
276 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
277 NLMSG_HDRLEN);
278 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
279}
280
281
282
283
284
285static inline int genlmsg_msg_size(int payload)
286{
287 return GENL_HDRLEN + payload;
288}
289
290
291
292
293
294static inline int genlmsg_total_size(int payload)
295{
296 return NLMSG_ALIGN(genlmsg_msg_size(payload));
297}
298
299
300
301
302
303
304static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
305{
306 return nlmsg_new(genlmsg_total_size(payload), flags);
307}
308
309
310#endif
311