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