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
87
88struct genl_info {
89 u32 snd_seq;
90 u32 snd_portid;
91 struct nlmsghdr * nlhdr;
92 struct genlmsghdr * genlhdr;
93 void * userhdr;
94 struct nlattr ** attrs;
95 possible_net_t _net;
96 void * user_ptr[2];
97 struct sock * dst_sk;
98};
99
100static inline struct net *genl_info_net(struct genl_info *info)
101{
102 return read_pnet(&info->_net);
103}
104
105static inline void genl_info_net_set(struct genl_info *info, struct net *net)
106{
107 write_pnet(&info->_net, net);
108}
109
110
111
112
113
114
115
116
117
118
119
120
121struct genl_ops {
122 const struct nla_policy *policy;
123 int (*doit)(struct sk_buff *skb,
124 struct genl_info *info);
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,
187 struct sk_buff *skb, struct net *net, u32 portid,
188 u32 group, struct nlmsghdr *nlh, gfp_t flags);
189
190struct sk_buff *genlmsg_new_unicast(size_t payload, struct genl_info *info,
191 gfp_t flags);
192void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
193 struct genl_family *family, int flags, u8 cmd);
194
195
196
197
198
199
200
201
202static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr,
203 struct genl_family *family)
204{
205 return (struct nlmsghdr *)((char *)user_hdr -
206 family->hdrsize -
207 GENL_HDRLEN -
208 NLMSG_HDRLEN);
209}
210
211
212
213
214
215
216
217
218
219static inline int genlmsg_parse(const struct nlmsghdr *nlh,
220 const struct genl_family *family,
221 struct nlattr *tb[], int maxtype,
222 const struct nla_policy *policy)
223{
224 return nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
225 policy);
226}
227
228
229
230
231
232
233
234
235
236
237static inline void genl_dump_check_consistent(struct netlink_callback *cb,
238 void *user_hdr,
239 struct genl_family *family)
240{
241 nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));
242}
243
244
245
246
247
248
249
250
251
252
253
254static inline void *genlmsg_put_reply(struct sk_buff *skb,
255 struct genl_info *info,
256 struct genl_family *family,
257 int flags, u8 cmd)
258{
259 return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
260 flags, cmd);
261}
262
263
264
265
266
267
268static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
269{
270 nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
271}
272
273
274
275
276
277
278static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
279{
280 if (hdr)
281 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
282}
283
284
285
286
287
288
289
290
291
292
293static inline int genlmsg_multicast_netns(struct genl_family *family,
294 struct net *net, struct sk_buff *skb,
295 u32 portid, unsigned int group, gfp_t flags)
296{
297 if (WARN_ON_ONCE(group >= family->n_mcgrps))
298 return -EINVAL;
299 group = family->mcgrp_offset + group;
300 return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
301}
302
303
304
305
306
307
308
309
310
311static inline int genlmsg_multicast(struct genl_family *family,
312 struct sk_buff *skb, u32 portid,
313 unsigned int group, gfp_t flags)
314{
315 return genlmsg_multicast_netns(family, &init_net, skb,
316 portid, group, flags);
317}
318
319
320
321
322
323
324
325
326
327
328
329int genlmsg_multicast_allns(struct genl_family *family,
330 struct sk_buff *skb, u32 portid,
331 unsigned int group, gfp_t flags);
332
333
334
335
336
337
338static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
339{
340 return nlmsg_unicast(net->genl_sock, skb, portid);
341}
342
343
344
345
346
347
348static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
349{
350 return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
351}
352
353
354
355
356
357static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
358{
359 return ((unsigned char *) gnlh + GENL_HDRLEN);
360}
361
362
363
364
365
366static inline int genlmsg_len(const struct genlmsghdr *gnlh)
367{
368 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
369 NLMSG_HDRLEN);
370 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
371}
372
373
374
375
376
377static inline int genlmsg_msg_size(int payload)
378{
379 return GENL_HDRLEN + payload;
380}
381
382
383
384
385
386static inline int genlmsg_total_size(int payload)
387{
388 return NLMSG_ALIGN(genlmsg_msg_size(payload));
389}
390
391
392
393
394
395
396static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
397{
398 return nlmsg_new(genlmsg_total_size(payload), flags);
399}
400
401
402
403
404
405
406
407
408
409
410
411
412
413static inline int genl_set_err(struct genl_family *family, struct net *net,
414 u32 portid, u32 group, int code)
415{
416 if (WARN_ON_ONCE(group >= family->n_mcgrps))
417 return -EINVAL;
418 group = family->mcgrp_offset + group;
419 return netlink_set_err(net->genl_sock, portid, group, code);
420}
421
422static inline int genl_has_listeners(struct genl_family *family,
423 struct net *net, unsigned int group)
424{
425 if (WARN_ON_ONCE(group >= family->n_mcgrps))
426 return -EINVAL;
427 group = family->mcgrp_offset + group;
428 return netlink_has_listeners(net->genl_sock, group);
429}
430#endif
431