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