1
2#ifndef __NET_GENERIC_NETLINK_H
3#define __NET_GENERIC_NETLINK_H
4
5#include <linux/genetlink.h>
6#include <net/netlink.h>
7#include <net/net_namespace.h>
8
9#include <linux/rh_kabi.h>
10
11#define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
12
13
14
15
16
17struct genl_multicast_group {
18 char name[GENL_NAMSIZ];
19};
20
21struct genl_ops;
22struct genl_info;
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
50
51
52
53
54
55struct genl_family {
56 int id;
57 unsigned int hdrsize;
58 char name[GENL_NAMSIZ];
59 unsigned int version;
60 unsigned int maxattr;
61 bool netnsok;
62 bool parallel_ops;
63 RH_KABI_FILL_HOLE(u8 n_small_ops)
64
65 const struct nla_policy *policy;
66 int (*pre_doit)(const struct genl_ops *ops,
67 struct sk_buff *skb,
68 struct genl_info *info);
69 void (*post_doit)(const struct genl_ops *ops,
70 struct sk_buff *skb,
71 struct genl_info *info);
72 int (*mcast_bind)(struct net *net, int group);
73 void (*mcast_unbind)(struct net *net, int group);
74 RH_KABI_DEPRECATE(struct nlattr **, attrbuf)
75 const struct genl_ops * ops;
76 const struct genl_multicast_group *mcgrps;
77 unsigned int n_ops;
78 unsigned int n_mcgrps;
79 unsigned int mcgrp_offset;
80 struct module *module;
81
82 RH_KABI_USE(1, const struct genl_small_ops *small_ops)
83 RH_KABI_RESERVE(2)
84 RH_KABI_RESERVE(3)
85 RH_KABI_RESERVE(4)
86 RH_KABI_RESERVE(5)
87 RH_KABI_RESERVE(6)
88 RH_KABI_RESERVE(7)
89 RH_KABI_RESERVE(8)
90};
91
92
93
94
95
96
97
98
99
100
101
102
103
104struct genl_info {
105 u32 snd_seq;
106 u32 snd_portid;
107 struct nlmsghdr * nlhdr;
108 struct genlmsghdr * genlhdr;
109 void * userhdr;
110 struct nlattr ** attrs;
111 possible_net_t _net;
112 void * user_ptr[2];
113 struct netlink_ext_ack *extack;
114};
115
116static inline struct net *genl_info_net(struct genl_info *info)
117{
118 return read_pnet(&info->_net);
119}
120
121static inline void genl_info_net_set(struct genl_info *info, struct net *net)
122{
123 write_pnet(&info->_net, net);
124}
125
126#define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
127
128static inline int genl_err_attr(struct genl_info *info, int err,
129 const struct nlattr *attr)
130{
131 info->extack->bad_attr = attr;
132
133 return err;
134}
135
136enum genl_validate_flags {
137 GENL_DONT_VALIDATE_STRICT = BIT(0),
138 GENL_DONT_VALIDATE_DUMP = BIT(1),
139 GENL_DONT_VALIDATE_DUMP_STRICT = BIT(2),
140};
141
142
143
144
145
146
147
148
149
150
151
152
153
154struct genl_small_ops {
155 int (*doit)(struct sk_buff *skb, struct genl_info *info);
156 int (*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
157 u8 cmd;
158 u8 internal_flags;
159 u8 flags;
160 u8 validate;
161};
162
163struct genl_ops_extended_rh {
164};
165
166
167
168
169
170
171
172
173
174
175
176
177
178struct genl_ops {
179 int (*doit)(struct sk_buff *skb,
180 struct genl_info *info);
181 int (*start)(struct netlink_callback *cb);
182 int (*dumpit)(struct sk_buff *skb,
183 struct netlink_callback *cb);
184 int (*done)(struct netlink_callback *cb);
185 u8 cmd;
186 u8 internal_flags;
187 u8 flags;
188 u8 validate;
189
190 RH_KABI_USE(1, const struct nla_policy *policy)
191 RH_KABI_USE_SPLIT(2, unsigned int maxattr)
192 RH_KABI_RESERVE(3)
193 RH_KABI_RESERVE(4)
194 RH_KABI_RESERVE(5)
195 RH_KABI_RESERVE(6)
196 RH_KABI_RESERVE(7)
197 RH_KABI_AUX_EMBED(genl_ops_extended)
198};
199
200
201
202
203
204
205
206struct genl_dumpit_info {
207 const struct genl_family *family;
208 struct genl_ops op;
209 struct nlattr **attrs;
210};
211
212static inline const struct genl_dumpit_info *
213genl_dumpit_info(struct netlink_callback *cb)
214{
215 return cb->data;
216}
217
218int genl_register_family(struct genl_family *family);
219int genl_unregister_family(const struct genl_family *family);
220void genl_notify(const struct genl_family *family, struct sk_buff *skb,
221 struct genl_info *info, u32 group, gfp_t flags);
222
223void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
224 const struct genl_family *family, int flags, u8 cmd);
225
226
227
228
229
230
231
232static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
233{
234 return (struct nlmsghdr *)((char *)user_hdr -
235 GENL_HDRLEN -
236 NLMSG_HDRLEN);
237}
238
239
240
241
242
243
244
245
246
247
248static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
249 const struct genl_family *family,
250 struct nlattr *tb[], int maxtype,
251 const struct nla_policy *policy,
252 struct netlink_ext_ack *extack)
253{
254 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
255 policy, NL_VALIDATE_LIBERAL, extack);
256}
257
258
259
260
261
262
263
264
265
266
267static inline int genlmsg_parse(const struct nlmsghdr *nlh,
268 const struct genl_family *family,
269 struct nlattr *tb[], int maxtype,
270 const struct nla_policy *policy,
271 struct netlink_ext_ack *extack)
272{
273 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
274 policy, NL_VALIDATE_STRICT, extack);
275}
276
277
278
279
280
281
282
283
284
285static inline void genl_dump_check_consistent(struct netlink_callback *cb,
286 void *user_hdr)
287{
288 nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
289}
290
291
292
293
294
295
296
297
298
299
300
301static inline void *genlmsg_put_reply(struct sk_buff *skb,
302 struct genl_info *info,
303 const struct genl_family *family,
304 int flags, u8 cmd)
305{
306 return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
307 flags, cmd);
308}
309
310
311
312
313
314
315static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
316{
317 nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
318}
319
320
321
322
323
324
325static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
326{
327 if (hdr)
328 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
329}
330
331
332
333
334
335
336
337
338
339
340static inline int genlmsg_multicast_netns(const struct genl_family *family,
341 struct net *net, struct sk_buff *skb,
342 u32 portid, unsigned int group, gfp_t flags)
343{
344 if (WARN_ON_ONCE(group >= family->n_mcgrps))
345 return -EINVAL;
346 group = family->mcgrp_offset + group;
347 return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
348}
349
350
351
352
353
354
355
356
357
358static inline int genlmsg_multicast(const struct genl_family *family,
359 struct sk_buff *skb, u32 portid,
360 unsigned int group, gfp_t flags)
361{
362 return genlmsg_multicast_netns(family, &init_net, skb,
363 portid, group, flags);
364}
365
366
367
368
369
370
371
372
373
374
375
376int genlmsg_multicast_allns(const struct genl_family *family,
377 struct sk_buff *skb, u32 portid,
378 unsigned int group, gfp_t flags);
379
380
381
382
383
384
385static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
386{
387 return nlmsg_unicast(net->genl_sock, skb, portid);
388}
389
390
391
392
393
394
395static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
396{
397 return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
398}
399
400
401
402
403
404static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
405{
406 return ((unsigned char *) gnlh + GENL_HDRLEN);
407}
408
409
410
411
412
413static inline int genlmsg_len(const struct genlmsghdr *gnlh)
414{
415 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
416 NLMSG_HDRLEN);
417 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
418}
419
420
421
422
423
424static inline int genlmsg_msg_size(int payload)
425{
426 return GENL_HDRLEN + payload;
427}
428
429
430
431
432
433static inline int genlmsg_total_size(int payload)
434{
435 return NLMSG_ALIGN(genlmsg_msg_size(payload));
436}
437
438
439
440
441
442
443static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
444{
445 return nlmsg_new(genlmsg_total_size(payload), flags);
446}
447
448
449
450
451
452
453
454
455
456
457
458
459
460static inline int genl_set_err(const struct genl_family *family,
461 struct net *net, u32 portid,
462 u32 group, int code)
463{
464 if (WARN_ON_ONCE(group >= family->n_mcgrps))
465 return -EINVAL;
466 group = family->mcgrp_offset + group;
467 return netlink_set_err(net->genl_sock, portid, group, code);
468}
469
470static inline int genl_has_listeners(const struct genl_family *family,
471 struct net *net, unsigned int group)
472{
473 if (WARN_ON_ONCE(group >= family->n_mcgrps))
474 return -EINVAL;
475 group = family->mcgrp_offset + group;
476 return netlink_has_listeners(net->genl_sock, group);
477}
478#endif
479