Commit | Line | Data |
---|---|---|
482a8524 TG |
1 | /* |
2 | * NETLINK Generic Netlink Family | |
3 | * | |
4 | * Authors: Jamal Hadi Salim | |
5 | * Thomas Graf <tgraf@suug.ch> | |
2dbba6f7 | 6 | * Johannes Berg <johannes@sipsolutions.net> |
482a8524 TG |
7 | */ |
8 | ||
482a8524 TG |
9 | #include <linux/module.h> |
10 | #include <linux/kernel.h> | |
11 | #include <linux/errno.h> | |
12 | #include <linux/types.h> | |
13 | #include <linux/socket.h> | |
14 | #include <linux/string.h> | |
15 | #include <linux/skbuff.h> | |
14cc3e2b | 16 | #include <linux/mutex.h> |
2dbba6f7 | 17 | #include <linux/bitmap.h> |
482a8524 TG |
18 | #include <net/sock.h> |
19 | #include <net/genetlink.h> | |
20 | ||
21 | struct sock *genl_sock = NULL; | |
22 | ||
14cc3e2b | 23 | static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */ |
482a8524 | 24 | |
3b71535f | 25 | static inline void genl_lock(void) |
482a8524 | 26 | { |
14cc3e2b | 27 | mutex_lock(&genl_mutex); |
482a8524 TG |
28 | } |
29 | ||
3b71535f | 30 | static inline void genl_unlock(void) |
482a8524 | 31 | { |
14cc3e2b | 32 | mutex_unlock(&genl_mutex); |
482a8524 TG |
33 | } |
34 | ||
35 | #define GENL_FAM_TAB_SIZE 16 | |
36 | #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1) | |
37 | ||
38 | static struct list_head family_ht[GENL_FAM_TAB_SIZE]; | |
2dbba6f7 JB |
39 | /* |
40 | * Bitmap of multicast groups that are currently in use. | |
41 | * | |
42 | * To avoid an allocation at boot of just one unsigned long, | |
43 | * declare it global instead. | |
44 | * Bit 0 is marked as already used since group 0 is invalid. | |
45 | */ | |
46 | static unsigned long mc_group_start = 0x1; | |
47 | static unsigned long *mc_groups = &mc_group_start; | |
48 | static unsigned long mc_groups_longs = 1; | |
482a8524 TG |
49 | |
50 | static int genl_ctrl_event(int event, void *data); | |
51 | ||
52 | static inline unsigned int genl_family_hash(unsigned int id) | |
53 | { | |
54 | return id & GENL_FAM_TAB_MASK; | |
55 | } | |
56 | ||
57 | static inline struct list_head *genl_family_chain(unsigned int id) | |
58 | { | |
59 | return &family_ht[genl_family_hash(id)]; | |
60 | } | |
61 | ||
62 | static struct genl_family *genl_family_find_byid(unsigned int id) | |
63 | { | |
64 | struct genl_family *f; | |
65 | ||
66 | list_for_each_entry(f, genl_family_chain(id), family_list) | |
67 | if (f->id == id) | |
68 | return f; | |
69 | ||
70 | return NULL; | |
71 | } | |
72 | ||
73 | static struct genl_family *genl_family_find_byname(char *name) | |
74 | { | |
75 | struct genl_family *f; | |
76 | int i; | |
77 | ||
78 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) | |
79 | list_for_each_entry(f, genl_family_chain(i), family_list) | |
80 | if (strcmp(f->name, name) == 0) | |
81 | return f; | |
82 | ||
83 | return NULL; | |
84 | } | |
85 | ||
86 | static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family) | |
87 | { | |
88 | struct genl_ops *ops; | |
89 | ||
90 | list_for_each_entry(ops, &family->ops_list, ops_list) | |
91 | if (ops->cmd == cmd) | |
92 | return ops; | |
93 | ||
94 | return NULL; | |
95 | } | |
96 | ||
97 | /* Of course we are going to have problems once we hit | |
98 | * 2^16 alive types, but that can only happen by year 2K | |
99 | */ | |
100 | static inline u16 genl_generate_id(void) | |
101 | { | |
102 | static u16 id_gen_idx; | |
103 | int overflowed = 0; | |
104 | ||
105 | do { | |
106 | if (id_gen_idx == 0) | |
107 | id_gen_idx = GENL_MIN_ID; | |
108 | ||
109 | if (++id_gen_idx > GENL_MAX_ID) { | |
110 | if (!overflowed) { | |
111 | overflowed = 1; | |
112 | id_gen_idx = 0; | |
113 | continue; | |
114 | } else | |
115 | return 0; | |
116 | } | |
117 | ||
118 | } while (genl_family_find_byid(id_gen_idx)); | |
119 | ||
120 | return id_gen_idx; | |
121 | } | |
122 | ||
2dbba6f7 JB |
123 | static struct genl_multicast_group notify_grp; |
124 | ||
125 | /** | |
126 | * genl_register_mc_group - register a multicast group | |
127 | * | |
128 | * Registers the specified multicast group and notifies userspace | |
129 | * about the new group. | |
130 | * | |
131 | * Returns 0 on success or a negative error code. | |
132 | * | |
133 | * @family: The generic netlink family the group shall be registered for. | |
134 | * @grp: The group to register, must have a name. | |
135 | */ | |
136 | int genl_register_mc_group(struct genl_family *family, | |
137 | struct genl_multicast_group *grp) | |
138 | { | |
139 | int id; | |
140 | unsigned long *new_groups; | |
141 | int err; | |
142 | ||
143 | BUG_ON(grp->name[0] == '\0'); | |
144 | ||
145 | genl_lock(); | |
146 | ||
147 | /* special-case our own group */ | |
148 | if (grp == ¬ify_grp) | |
149 | id = GENL_ID_CTRL; | |
150 | else | |
151 | id = find_first_zero_bit(mc_groups, | |
152 | mc_groups_longs * BITS_PER_LONG); | |
153 | ||
154 | ||
155 | if (id >= mc_groups_longs * BITS_PER_LONG) { | |
156 | size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long); | |
157 | ||
158 | if (mc_groups == &mc_group_start) { | |
159 | new_groups = kzalloc(nlen, GFP_KERNEL); | |
160 | if (!new_groups) { | |
161 | err = -ENOMEM; | |
162 | goto out; | |
163 | } | |
164 | mc_groups = new_groups; | |
165 | *mc_groups = mc_group_start; | |
166 | } else { | |
167 | new_groups = krealloc(mc_groups, nlen, GFP_KERNEL); | |
168 | if (!new_groups) { | |
169 | err = -ENOMEM; | |
170 | goto out; | |
171 | } | |
172 | mc_groups = new_groups; | |
173 | mc_groups[mc_groups_longs] = 0; | |
174 | } | |
175 | mc_groups_longs++; | |
176 | } | |
177 | ||
178 | err = netlink_change_ngroups(genl_sock, | |
2c04ddb7 | 179 | mc_groups_longs * BITS_PER_LONG); |
2dbba6f7 JB |
180 | if (err) |
181 | goto out; | |
182 | ||
183 | grp->id = id; | |
184 | set_bit(id, mc_groups); | |
185 | list_add_tail(&grp->list, &family->mcast_groups); | |
186 | grp->family = family; | |
187 | ||
188 | genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp); | |
189 | out: | |
190 | genl_unlock(); | |
79d310d0 | 191 | return err; |
2dbba6f7 JB |
192 | } |
193 | EXPORT_SYMBOL(genl_register_mc_group); | |
194 | ||
79dc4386 TG |
195 | static void __genl_unregister_mc_group(struct genl_family *family, |
196 | struct genl_multicast_group *grp) | |
197 | { | |
198 | BUG_ON(grp->family != family); | |
199 | netlink_clear_multicast_users(genl_sock, grp->id); | |
200 | clear_bit(grp->id, mc_groups); | |
201 | list_del(&grp->list); | |
202 | genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp); | |
203 | grp->id = 0; | |
204 | grp->family = NULL; | |
205 | } | |
206 | ||
2dbba6f7 JB |
207 | /** |
208 | * genl_unregister_mc_group - unregister a multicast group | |
209 | * | |
210 | * Unregisters the specified multicast group and notifies userspace | |
211 | * about it. All current listeners on the group are removed. | |
212 | * | |
213 | * Note: It is not necessary to unregister all multicast groups before | |
214 | * unregistering the family, unregistering the family will cause | |
215 | * all assigned multicast groups to be unregistered automatically. | |
216 | * | |
217 | * @family: Generic netlink family the group belongs to. | |
218 | * @grp: The group to unregister, must have been registered successfully | |
219 | * previously. | |
220 | */ | |
221 | void genl_unregister_mc_group(struct genl_family *family, | |
222 | struct genl_multicast_group *grp) | |
223 | { | |
2dbba6f7 | 224 | genl_lock(); |
79dc4386 | 225 | __genl_unregister_mc_group(family, grp); |
2dbba6f7 JB |
226 | genl_unlock(); |
227 | } | |
228 | ||
229 | static void genl_unregister_mc_groups(struct genl_family *family) | |
230 | { | |
231 | struct genl_multicast_group *grp, *tmp; | |
232 | ||
233 | list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list) | |
79dc4386 | 234 | __genl_unregister_mc_group(family, grp); |
2dbba6f7 JB |
235 | } |
236 | ||
482a8524 TG |
237 | /** |
238 | * genl_register_ops - register generic netlink operations | |
239 | * @family: generic netlink family | |
240 | * @ops: operations to be registered | |
241 | * | |
242 | * Registers the specified operations and assigns them to the specified | |
243 | * family. Either a doit or dumpit callback must be specified or the | |
244 | * operation will fail. Only one operation structure per command | |
245 | * identifier may be registered. | |
246 | * | |
247 | * See include/net/genetlink.h for more documenation on the operations | |
248 | * structure. | |
249 | * | |
250 | * Returns 0 on success or a negative error code. | |
251 | */ | |
252 | int genl_register_ops(struct genl_family *family, struct genl_ops *ops) | |
253 | { | |
254 | int err = -EINVAL; | |
255 | ||
256 | if (ops->dumpit == NULL && ops->doit == NULL) | |
257 | goto errout; | |
258 | ||
259 | if (genl_get_cmd(ops->cmd, family)) { | |
260 | err = -EEXIST; | |
261 | goto errout; | |
262 | } | |
263 | ||
334c29a6 | 264 | if (ops->dumpit) |
334c29a6 | 265 | ops->flags |= GENL_CMD_CAP_DUMP; |
48d4ed7a JHS |
266 | if (ops->doit) |
267 | ops->flags |= GENL_CMD_CAP_DO; | |
334c29a6 JHS |
268 | if (ops->policy) |
269 | ops->flags |= GENL_CMD_CAP_HASPOL; | |
270 | ||
482a8524 TG |
271 | genl_lock(); |
272 | list_add_tail(&ops->ops_list, &family->ops_list); | |
273 | genl_unlock(); | |
274 | ||
275 | genl_ctrl_event(CTRL_CMD_NEWOPS, ops); | |
276 | err = 0; | |
277 | errout: | |
278 | return err; | |
279 | } | |
280 | ||
281 | /** | |
282 | * genl_unregister_ops - unregister generic netlink operations | |
283 | * @family: generic netlink family | |
284 | * @ops: operations to be unregistered | |
285 | * | |
286 | * Unregisters the specified operations and unassigns them from the | |
287 | * specified family. The operation blocks until the current message | |
288 | * processing has finished and doesn't start again until the | |
289 | * unregister process has finished. | |
290 | * | |
291 | * Note: It is not necessary to unregister all operations before | |
292 | * unregistering the family, unregistering the family will cause | |
293 | * all assigned operations to be unregistered automatically. | |
294 | * | |
295 | * Returns 0 on success or a negative error code. | |
296 | */ | |
297 | int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops) | |
298 | { | |
299 | struct genl_ops *rc; | |
300 | ||
301 | genl_lock(); | |
302 | list_for_each_entry(rc, &family->ops_list, ops_list) { | |
303 | if (rc == ops) { | |
304 | list_del(&ops->ops_list); | |
305 | genl_unlock(); | |
306 | genl_ctrl_event(CTRL_CMD_DELOPS, ops); | |
307 | return 0; | |
308 | } | |
309 | } | |
310 | genl_unlock(); | |
311 | ||
312 | return -ENOENT; | |
313 | } | |
314 | ||
315 | /** | |
316 | * genl_register_family - register a generic netlink family | |
317 | * @family: generic netlink family | |
318 | * | |
319 | * Registers the specified family after validating it first. Only one | |
320 | * family may be registered with the same family name or identifier. | |
321 | * The family id may equal GENL_ID_GENERATE causing an unique id to | |
322 | * be automatically generated and assigned. | |
323 | * | |
324 | * Return 0 on success or a negative error code. | |
325 | */ | |
326 | int genl_register_family(struct genl_family *family) | |
327 | { | |
328 | int err = -EINVAL; | |
329 | ||
330 | if (family->id && family->id < GENL_MIN_ID) | |
331 | goto errout; | |
332 | ||
333 | if (family->id > GENL_MAX_ID) | |
334 | goto errout; | |
335 | ||
336 | INIT_LIST_HEAD(&family->ops_list); | |
2dbba6f7 | 337 | INIT_LIST_HEAD(&family->mcast_groups); |
482a8524 TG |
338 | |
339 | genl_lock(); | |
340 | ||
341 | if (genl_family_find_byname(family->name)) { | |
342 | err = -EEXIST; | |
343 | goto errout_locked; | |
344 | } | |
345 | ||
346 | if (genl_family_find_byid(family->id)) { | |
347 | err = -EEXIST; | |
348 | goto errout_locked; | |
349 | } | |
350 | ||
482a8524 TG |
351 | if (family->id == GENL_ID_GENERATE) { |
352 | u16 newid = genl_generate_id(); | |
353 | ||
354 | if (!newid) { | |
355 | err = -ENOMEM; | |
356 | goto errout_locked; | |
357 | } | |
358 | ||
359 | family->id = newid; | |
360 | } | |
361 | ||
362 | if (family->maxattr) { | |
363 | family->attrbuf = kmalloc((family->maxattr+1) * | |
364 | sizeof(struct nlattr *), GFP_KERNEL); | |
365 | if (family->attrbuf == NULL) { | |
366 | err = -ENOMEM; | |
e200bd80 | 367 | goto errout_locked; |
482a8524 TG |
368 | } |
369 | } else | |
370 | family->attrbuf = NULL; | |
371 | ||
372 | list_add_tail(&family->family_list, genl_family_chain(family->id)); | |
373 | genl_unlock(); | |
374 | ||
375 | genl_ctrl_event(CTRL_CMD_NEWFAMILY, family); | |
376 | ||
377 | return 0; | |
378 | ||
379 | errout_locked: | |
380 | genl_unlock(); | |
381 | errout: | |
382 | return err; | |
383 | } | |
384 | ||
385 | /** | |
386 | * genl_unregister_family - unregister generic netlink family | |
387 | * @family: generic netlink family | |
388 | * | |
389 | * Unregisters the specified family. | |
390 | * | |
391 | * Returns 0 on success or a negative error code. | |
392 | */ | |
393 | int genl_unregister_family(struct genl_family *family) | |
394 | { | |
395 | struct genl_family *rc; | |
396 | ||
397 | genl_lock(); | |
398 | ||
910d6c32 PE |
399 | genl_unregister_mc_groups(family); |
400 | ||
482a8524 TG |
401 | list_for_each_entry(rc, genl_family_chain(family->id), family_list) { |
402 | if (family->id != rc->id || strcmp(rc->name, family->name)) | |
403 | continue; | |
404 | ||
405 | list_del(&rc->family_list); | |
406 | INIT_LIST_HEAD(&family->ops_list); | |
407 | genl_unlock(); | |
408 | ||
482a8524 TG |
409 | kfree(family->attrbuf); |
410 | genl_ctrl_event(CTRL_CMD_DELFAMILY, family); | |
411 | return 0; | |
412 | } | |
413 | ||
414 | genl_unlock(); | |
415 | ||
416 | return -ENOENT; | |
417 | } | |
418 | ||
1d00a4eb | 419 | static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
482a8524 TG |
420 | { |
421 | struct genl_ops *ops; | |
422 | struct genl_family *family; | |
423 | struct genl_info info; | |
424 | struct genlmsghdr *hdr = nlmsg_data(nlh); | |
1d00a4eb | 425 | int hdrlen, err; |
482a8524 | 426 | |
746fac4d | 427 | family = genl_family_find_byid(nlh->nlmsg_type); |
1d00a4eb TG |
428 | if (family == NULL) |
429 | return -ENOENT; | |
482a8524 TG |
430 | |
431 | hdrlen = GENL_HDRLEN + family->hdrsize; | |
432 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) | |
1d00a4eb | 433 | return -EINVAL; |
482a8524 TG |
434 | |
435 | ops = genl_get_cmd(hdr->cmd, family); | |
1d00a4eb TG |
436 | if (ops == NULL) |
437 | return -EOPNOTSUPP; | |
482a8524 | 438 | |
1d00a4eb TG |
439 | if ((ops->flags & GENL_ADMIN_PERM) && |
440 | security_netlink_recv(skb, CAP_NET_ADMIN)) | |
441 | return -EPERM; | |
482a8524 TG |
442 | |
443 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1d00a4eb TG |
444 | if (ops->dumpit == NULL) |
445 | return -EOPNOTSUPP; | |
482a8524 | 446 | |
6d1a3fb5 PM |
447 | genl_unlock(); |
448 | err = netlink_dump_start(genl_sock, skb, nlh, | |
449 | ops->dumpit, ops->done); | |
450 | genl_lock(); | |
451 | return err; | |
482a8524 TG |
452 | } |
453 | ||
1d00a4eb TG |
454 | if (ops->doit == NULL) |
455 | return -EOPNOTSUPP; | |
482a8524 TG |
456 | |
457 | if (family->attrbuf) { | |
458 | err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr, | |
459 | ops->policy); | |
460 | if (err < 0) | |
1d00a4eb | 461 | return err; |
482a8524 TG |
462 | } |
463 | ||
464 | info.snd_seq = nlh->nlmsg_seq; | |
465 | info.snd_pid = NETLINK_CB(skb).pid; | |
466 | info.nlhdr = nlh; | |
467 | info.genlhdr = nlmsg_data(nlh); | |
468 | info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN; | |
469 | info.attrs = family->attrbuf; | |
470 | ||
1d00a4eb | 471 | return ops->doit(skb, &info); |
482a8524 TG |
472 | } |
473 | ||
cd40b7d3 | 474 | static void genl_rcv(struct sk_buff *skb) |
482a8524 | 475 | { |
cd40b7d3 DL |
476 | genl_lock(); |
477 | netlink_rcv_skb(skb, &genl_rcv_msg); | |
478 | genl_unlock(); | |
482a8524 TG |
479 | } |
480 | ||
481 | /************************************************************************** | |
482 | * Controller | |
483 | **************************************************************************/ | |
484 | ||
17c157c8 TG |
485 | static struct genl_family genl_ctrl = { |
486 | .id = GENL_ID_CTRL, | |
487 | .name = "nlctrl", | |
334c29a6 | 488 | .version = 0x2, |
17c157c8 TG |
489 | .maxattr = CTRL_ATTR_MAX, |
490 | }; | |
491 | ||
482a8524 TG |
492 | static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq, |
493 | u32 flags, struct sk_buff *skb, u8 cmd) | |
494 | { | |
495 | void *hdr; | |
496 | ||
17c157c8 | 497 | hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd); |
482a8524 TG |
498 | if (hdr == NULL) |
499 | return -1; | |
500 | ||
501 | NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name); | |
502 | NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id); | |
eb328111 TG |
503 | NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version); |
504 | NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize); | |
505 | NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr); | |
506 | ||
e94ef682 TG |
507 | if (!list_empty(&family->ops_list)) { |
508 | struct nlattr *nla_ops; | |
509 | struct genl_ops *ops; | |
510 | int idx = 1; | |
eb328111 | 511 | |
e94ef682 TG |
512 | nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS); |
513 | if (nla_ops == NULL) | |
eb328111 TG |
514 | goto nla_put_failure; |
515 | ||
e94ef682 TG |
516 | list_for_each_entry(ops, &family->ops_list, ops_list) { |
517 | struct nlattr *nest; | |
eb328111 | 518 | |
e94ef682 TG |
519 | nest = nla_nest_start(skb, idx++); |
520 | if (nest == NULL) | |
521 | goto nla_put_failure; | |
eb328111 | 522 | |
e94ef682 TG |
523 | NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd); |
524 | NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags); | |
eb328111 | 525 | |
e94ef682 TG |
526 | nla_nest_end(skb, nest); |
527 | } | |
528 | ||
529 | nla_nest_end(skb, nla_ops); | |
530 | } | |
482a8524 | 531 | |
2dbba6f7 JB |
532 | if (!list_empty(&family->mcast_groups)) { |
533 | struct genl_multicast_group *grp; | |
534 | struct nlattr *nla_grps; | |
535 | int idx = 1; | |
536 | ||
537 | nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); | |
538 | if (nla_grps == NULL) | |
539 | goto nla_put_failure; | |
540 | ||
541 | list_for_each_entry(grp, &family->mcast_groups, list) { | |
542 | struct nlattr *nest; | |
543 | ||
544 | nest = nla_nest_start(skb, idx++); | |
545 | if (nest == NULL) | |
546 | goto nla_put_failure; | |
547 | ||
548 | NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id); | |
549 | NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME, | |
550 | grp->name); | |
551 | ||
552 | nla_nest_end(skb, nest); | |
553 | } | |
554 | nla_nest_end(skb, nla_grps); | |
555 | } | |
556 | ||
557 | return genlmsg_end(skb, hdr); | |
558 | ||
559 | nla_put_failure: | |
bc3ed28c TG |
560 | genlmsg_cancel(skb, hdr); |
561 | return -EMSGSIZE; | |
2dbba6f7 JB |
562 | } |
563 | ||
564 | static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid, | |
565 | u32 seq, u32 flags, struct sk_buff *skb, | |
566 | u8 cmd) | |
567 | { | |
568 | void *hdr; | |
569 | struct nlattr *nla_grps; | |
570 | struct nlattr *nest; | |
571 | ||
572 | hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd); | |
573 | if (hdr == NULL) | |
574 | return -1; | |
575 | ||
576 | NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name); | |
577 | NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id); | |
578 | ||
579 | nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); | |
580 | if (nla_grps == NULL) | |
581 | goto nla_put_failure; | |
582 | ||
583 | nest = nla_nest_start(skb, 1); | |
584 | if (nest == NULL) | |
585 | goto nla_put_failure; | |
586 | ||
587 | NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id); | |
588 | NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME, | |
589 | grp->name); | |
590 | ||
591 | nla_nest_end(skb, nest); | |
592 | nla_nest_end(skb, nla_grps); | |
593 | ||
482a8524 TG |
594 | return genlmsg_end(skb, hdr); |
595 | ||
596 | nla_put_failure: | |
bc3ed28c TG |
597 | genlmsg_cancel(skb, hdr); |
598 | return -EMSGSIZE; | |
482a8524 TG |
599 | } |
600 | ||
601 | static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb) | |
602 | { | |
603 | ||
604 | int i, n = 0; | |
605 | struct genl_family *rt; | |
606 | int chains_to_skip = cb->args[0]; | |
607 | int fams_to_skip = cb->args[1]; | |
608 | ||
609 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) { | |
610 | if (i < chains_to_skip) | |
611 | continue; | |
612 | n = 0; | |
613 | list_for_each_entry(rt, genl_family_chain(i), family_list) { | |
614 | if (++n < fams_to_skip) | |
615 | continue; | |
616 | if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid, | |
617 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
618 | skb, CTRL_CMD_NEWFAMILY) < 0) | |
619 | goto errout; | |
620 | } | |
621 | ||
622 | fams_to_skip = 0; | |
623 | } | |
624 | ||
625 | errout: | |
626 | cb->args[0] = i; | |
627 | cb->args[1] = n; | |
628 | ||
629 | return skb->len; | |
630 | } | |
631 | ||
2dbba6f7 JB |
632 | static struct sk_buff *ctrl_build_family_msg(struct genl_family *family, |
633 | u32 pid, int seq, u8 cmd) | |
482a8524 TG |
634 | { |
635 | struct sk_buff *skb; | |
636 | int err; | |
637 | ||
339bf98f | 638 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
482a8524 TG |
639 | if (skb == NULL) |
640 | return ERR_PTR(-ENOBUFS); | |
641 | ||
642 | err = ctrl_fill_info(family, pid, seq, 0, skb, cmd); | |
643 | if (err < 0) { | |
644 | nlmsg_free(skb); | |
645 | return ERR_PTR(err); | |
646 | } | |
647 | ||
648 | return skb; | |
649 | } | |
650 | ||
2dbba6f7 JB |
651 | static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp, |
652 | u32 pid, int seq, u8 cmd) | |
653 | { | |
654 | struct sk_buff *skb; | |
655 | int err; | |
656 | ||
657 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
658 | if (skb == NULL) | |
659 | return ERR_PTR(-ENOBUFS); | |
660 | ||
661 | err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd); | |
662 | if (err < 0) { | |
663 | nlmsg_free(skb); | |
664 | return ERR_PTR(err); | |
665 | } | |
666 | ||
667 | return skb; | |
668 | } | |
669 | ||
ef7c79ed | 670 | static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = { |
482a8524 | 671 | [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 }, |
5176f91e TG |
672 | [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING, |
673 | .len = GENL_NAMSIZ - 1 }, | |
482a8524 TG |
674 | }; |
675 | ||
676 | static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info) | |
677 | { | |
678 | struct sk_buff *msg; | |
679 | struct genl_family *res = NULL; | |
680 | int err = -EINVAL; | |
681 | ||
682 | if (info->attrs[CTRL_ATTR_FAMILY_ID]) { | |
683 | u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]); | |
684 | res = genl_family_find_byid(id); | |
685 | } | |
686 | ||
687 | if (info->attrs[CTRL_ATTR_FAMILY_NAME]) { | |
5176f91e | 688 | char *name; |
482a8524 | 689 | |
5176f91e | 690 | name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]); |
482a8524 TG |
691 | res = genl_family_find_byname(name); |
692 | } | |
693 | ||
694 | if (res == NULL) { | |
695 | err = -ENOENT; | |
696 | goto errout; | |
697 | } | |
698 | ||
2dbba6f7 JB |
699 | msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq, |
700 | CTRL_CMD_NEWFAMILY); | |
482a8524 TG |
701 | if (IS_ERR(msg)) { |
702 | err = PTR_ERR(msg); | |
703 | goto errout; | |
704 | } | |
705 | ||
81878d27 | 706 | err = genlmsg_reply(msg, info); |
482a8524 TG |
707 | errout: |
708 | return err; | |
709 | } | |
710 | ||
711 | static int genl_ctrl_event(int event, void *data) | |
712 | { | |
713 | struct sk_buff *msg; | |
714 | ||
715 | if (genl_sock == NULL) | |
716 | return 0; | |
717 | ||
718 | switch (event) { | |
719 | case CTRL_CMD_NEWFAMILY: | |
720 | case CTRL_CMD_DELFAMILY: | |
2dbba6f7 JB |
721 | msg = ctrl_build_family_msg(data, 0, 0, event); |
722 | if (IS_ERR(msg)) | |
723 | return PTR_ERR(msg); | |
724 | ||
725 | genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL); | |
726 | break; | |
727 | case CTRL_CMD_NEWMCAST_GRP: | |
728 | case CTRL_CMD_DELMCAST_GRP: | |
729 | msg = ctrl_build_mcgrp_msg(data, 0, 0, event); | |
482a8524 TG |
730 | if (IS_ERR(msg)) |
731 | return PTR_ERR(msg); | |
732 | ||
d387f6ad | 733 | genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL); |
482a8524 TG |
734 | break; |
735 | } | |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
740 | static struct genl_ops genl_ctrl_ops = { | |
741 | .cmd = CTRL_CMD_GETFAMILY, | |
742 | .doit = ctrl_getfamily, | |
743 | .dumpit = ctrl_dumpfamily, | |
744 | .policy = ctrl_policy, | |
745 | }; | |
746 | ||
2dbba6f7 JB |
747 | static struct genl_multicast_group notify_grp = { |
748 | .name = "notify", | |
749 | }; | |
750 | ||
482a8524 TG |
751 | static int __init genl_init(void) |
752 | { | |
753 | int i, err; | |
754 | ||
755 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) | |
756 | INIT_LIST_HEAD(&family_ht[i]); | |
757 | ||
758 | err = genl_register_family(&genl_ctrl); | |
759 | if (err < 0) | |
760 | goto errout; | |
761 | ||
762 | err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops); | |
763 | if (err < 0) | |
764 | goto errout_register; | |
765 | ||
766 | netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV); | |
2dbba6f7 JB |
767 | |
768 | /* we'll bump the group number right afterwards */ | |
b4b51029 | 769 | genl_sock = netlink_kernel_create(&init_net, NETLINK_GENERIC, 0, |
6d1a3fb5 | 770 | genl_rcv, &genl_mutex, THIS_MODULE); |
e200bd80 | 771 | if (genl_sock == NULL) |
482a8524 | 772 | panic("GENL: Cannot initialize generic netlink\n"); |
482a8524 | 773 | |
2dbba6f7 JB |
774 | err = genl_register_mc_group(&genl_ctrl, ¬ify_grp); |
775 | if (err < 0) | |
776 | goto errout_register; | |
777 | ||
482a8524 TG |
778 | return 0; |
779 | ||
780 | errout_register: | |
781 | genl_unregister_family(&genl_ctrl); | |
782 | errout: | |
783 | panic("GENL: Cannot register controller: %d\n", err); | |
482a8524 TG |
784 | } |
785 | ||
786 | subsys_initcall(genl_init); | |
787 | ||
788 | EXPORT_SYMBOL(genl_sock); | |
789 | EXPORT_SYMBOL(genl_register_ops); | |
790 | EXPORT_SYMBOL(genl_unregister_ops); | |
791 | EXPORT_SYMBOL(genl_register_family); | |
792 | EXPORT_SYMBOL(genl_unregister_family); |