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#define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
9
10
11
12
13
14struct genl_multicast_group {
15 char name[GENL_NAMSIZ];
16};
17
18struct genl_ops;
19struct genl_info;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50struct genl_family {
51 unsigned int id;
52 unsigned int hdrsize;
53 char name[GENL_NAMSIZ];
54 unsigned int version;
55 unsigned int maxattr;
56 bool netnsok;
57 bool parallel_ops;
58 int (*pre_doit)(const struct genl_ops *ops,
59 struct sk_buff *skb,
60 struct genl_info *info);
61 void (*post_doit)(const struct genl_ops *ops,
62 struct sk_buff *skb,
63 struct genl_info *info);
64 int (*mcast_bind)(struct net *net, int group);
65 void (*mcast_unbind)(struct net *net, int group);
66 struct nlattr ** attrbuf;
67 const struct genl_ops * ops;
68 const struct genl_multicast_group *mcgrps;
69 unsigned int n_ops;
70 unsigned int n_mcgrps;
71 unsigned int mcgrp_offset;
72 struct list_head family_list;
73 struct module *module;
74};
75
76
77
78
79
80
81
82
83
84
85
86
87struct genl_info {
88 u32 snd_seq;
89 u32 snd_portid;
90 struct nlmsghdr * nlhdr;
91 struct genlmsghdr * genlhdr;
92 void * userhdr;
93 struct nlattr ** attrs;
94 possible_net_t _net;
95 void * user_ptr[2];
96};
97
98static inline struct net *genl_info_net(struct genl_info *info)
99{
100 return read_pnet(&info->_net);
101}
102
103static inline void genl_info_net_set(struct genl_info *info, struct net *net)
104{
105 write_pnet(&info->_net, net);
106}
107
108
109
110
111
112
113
114
115
116
117
118
119
120struct genl_ops {
121 const struct nla_policy *policy;
122 int (*doit)(struct sk_buff *skb,
123 struct genl_info *info);
124 int (*start)(struct netlink_callback *cb);
125 int (*dumpit)(struct sk_buff *skb,
126 struct netlink_callback *cb);
127 int (*done)(struct netlink_callback *cb);
128 u8 cmd;
129 u8 internal_flags;
130 u8 flags;
131};
132
133int __genl_register_family(struct genl_family *family);
134
135static inline int genl_register_family(struct genl_family *family)
136{
137 family->module = THIS_MODULE;
138 return __genl_register_family(family);
139}
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162static inline int
163_genl_register_family_with_ops_grps(struct genl_family *family,
164 const struct genl_ops *ops, size_t n_ops,
165 const struct genl_multicast_group *mcgrps,
166 size_t n_mcgrps)
167{
168 family->module = THIS_MODULE;
169 family->ops = ops;
170 family->n_ops = n_ops;
171 family->mcgrps = mcgrps;
172 family->n_mcgrps = n_mcgrps;
173 return __genl_register_family(family);
174}
175
176#define genl_register_family_with_ops(family, ops) \
177 _genl_register_family_with_ops_grps((family), \
178 (ops), ARRAY_SIZE(ops), \
179 NULL, 0)
180#define genl_register_family_with_ops_groups(family, ops, grps) \
181 _genl_register_family_with_ops_grps((family), \
182 (ops), ARRAY_SIZE(ops), \
183 (grps), ARRAY_SIZE(grps))
184
185int genl_unregister_family(struct genl_family *family);
186void genl_notify(struct genl_family *family, struct sk_buff *skb,
187 struct genl_info *info, u32 group, gfp_t flags);
188
189void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
190 struct genl_family *family, int flags, u8 cmd);
191
192
193
194
195
196
197
198
199static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr,
200 struct genl_family *family)
201{
202 return (struct nlmsghdr *)((char *)user_hdr -
203 family->hdrsize -
204 GENL_HDRLEN -
205 NLMSG_HDRLEN);
206}
207
208
209
210
211
212
213
214
215
216static inline int genlmsg_parse(const struct nlmsghdr *nlh,
217 const struct genl_family *family,
218 struct nlattr *tb[], int maxtype,
219 const struct nla_policy *policy)
220{
221 return nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
222 policy);
223}
224
225
226
227
228
229
230
231
232
233
234static inline void genl_dump_check_consistent(struct netlink_callback *cb,
235 void *user_hdr,
236 struct genl_family *family)
237{
238 nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));
239}
240
241
242
243
244
245
246
247
248
249
250
251static inline void *genlmsg_put_reply(struct sk_buff *skb,
252 struct genl_info *info,
253 struct genl_family *family,
254 int flags, u8 cmd)
255{
256 return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
257 flags, cmd);
258}
259
260
261
262
263
264
265static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
266{
267 nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
268}
269
270
271
272
273
274
275static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
276{
277 if (hdr)
278 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
279}
280
281
282
283
284
285
286
287
288
289
290static inline int genlmsg_multicast_netns(struct genl_family *family,
291 struct net *net, struct sk_buff *skb,
292 u32 portid, unsigned int group, gfp_t flags)
293{
294 if (WARN_ON_ONCE(group >= family->n_mcgrps))
295 return -EINVAL;
296 group = family->mcgrp_offset + group;
297 return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
298}
299
300
301
302
303
304
305
306
307
308static inline int genlmsg_multicast(struct genl_family *family,
309 struct sk_buff *skb, u32 portid,
310 unsigned int group, gfp_t flags)
311{
312 return genlmsg_multicast_netns(family, &init_net, skb,
313 portid, group, flags);
314}
315
316
317
318
319
320
321
322
323
324
325
326int genlmsg_multicast_allns(struct genl_family *family,
327 struct sk_buff *skb, u32 portid,
328 unsigned int group, gfp_t flags);
329
330
331
332
333
334
335static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
336{
337 return nlmsg_unicast(net->genl_sock, skb, portid);
338}
339
340
341
342
343
344
345static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
346{
347 return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
348}
349
350
351
352
353
354static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
355{
356 return ((unsigned char *) gnlh + GENL_HDRLEN);
357}
358
359
360
361
362
363static inline int genlmsg_len(const struct genlmsghdr *gnlh)
364{
365 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
366 NLMSG_HDRLEN);
367 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
368}
369
370
371
372
373
374static inline int genlmsg_msg_size(int payload)
375{
376 return GENL_HDRLEN + payload;
377}
378
379
380
381
382
383static inline int genlmsg_total_size(int payload)
384{
385 return NLMSG_ALIGN(genlmsg_msg_size(payload));
386}
387
388
389
390
391
392
393static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
394{
395 return nlmsg_new(genlmsg_total_size(payload), flags);
396}
397
398
399
400
401
402
403
404
405
406
407
408
409
410static inline int genl_set_err(struct genl_family *family, struct net *net,
411 u32 portid, u32 group, int code)
412{
413 if (WARN_ON_ONCE(group >= family->n_mcgrps))
414 return -EINVAL;
415 group = family->mcgrp_offset + group;
416 return netlink_set_err(net->genl_sock, portid, group, code);
417}
418
419static inline int genl_has_listeners(struct genl_family *family,
420 struct net *net, unsigned int group)
421{
422 if (WARN_ON_ONCE(group >= family->n_mcgrps))
423 return -EINVAL;
424 group = family->mcgrp_offset + group;
425 return netlink_has_listeners(net->genl_sock, group);
426}
427#endif
428