net: make namespace iteration possible under RCU
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netlink / genetlink.c
CommitLineData
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
21struct sock *genl_sock = NULL;
22
14cc3e2b 23static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
482a8524 24
3b71535f 25static inline void genl_lock(void)
482a8524 26{
14cc3e2b 27 mutex_lock(&genl_mutex);
482a8524
TG
28}
29
3b71535f 30static 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
38static 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 */
46static unsigned long mc_group_start = 0x1;
47static unsigned long *mc_groups = &mc_group_start;
48static unsigned long mc_groups_longs = 1;
482a8524
TG
49
50static int genl_ctrl_event(int event, void *data);
51
52static inline unsigned int genl_family_hash(unsigned int id)
53{
54 return id & GENL_FAM_TAB_MASK;
55}
56
57static inline struct list_head *genl_family_chain(unsigned int id)
58{
59 return &family_ht[genl_family_hash(id)];
60}
61
62static 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
73static 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
86static 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*/
100static 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
123static 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 */
136int 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 == &notify_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}
193EXPORT_SYMBOL(genl_register_mc_group);
194
79dc4386
TG
195static 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 */
221void 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}
3efb40c2 228EXPORT_SYMBOL(genl_unregister_mc_group);
2dbba6f7
JB
229
230static void genl_unregister_mc_groups(struct genl_family *family)
231{
232 struct genl_multicast_group *grp, *tmp;
233
234 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
79dc4386 235 __genl_unregister_mc_group(family, grp);
2dbba6f7
JB
236}
237
482a8524
TG
238/**
239 * genl_register_ops - register generic netlink operations
240 * @family: generic netlink family
241 * @ops: operations to be registered
242 *
243 * Registers the specified operations and assigns them to the specified
244 * family. Either a doit or dumpit callback must be specified or the
245 * operation will fail. Only one operation structure per command
246 * identifier may be registered.
247 *
248 * See include/net/genetlink.h for more documenation on the operations
249 * structure.
250 *
251 * Returns 0 on success or a negative error code.
252 */
253int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
254{
255 int err = -EINVAL;
256
257 if (ops->dumpit == NULL && ops->doit == NULL)
258 goto errout;
259
260 if (genl_get_cmd(ops->cmd, family)) {
261 err = -EEXIST;
262 goto errout;
263 }
264
334c29a6 265 if (ops->dumpit)
334c29a6 266 ops->flags |= GENL_CMD_CAP_DUMP;
48d4ed7a
JHS
267 if (ops->doit)
268 ops->flags |= GENL_CMD_CAP_DO;
334c29a6
JHS
269 if (ops->policy)
270 ops->flags |= GENL_CMD_CAP_HASPOL;
271
482a8524
TG
272 genl_lock();
273 list_add_tail(&ops->ops_list, &family->ops_list);
274 genl_unlock();
275
276 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
277 err = 0;
278errout:
279 return err;
280}
281
282/**
283 * genl_unregister_ops - unregister generic netlink operations
284 * @family: generic netlink family
285 * @ops: operations to be unregistered
286 *
287 * Unregisters the specified operations and unassigns them from the
288 * specified family. The operation blocks until the current message
289 * processing has finished and doesn't start again until the
290 * unregister process has finished.
291 *
292 * Note: It is not necessary to unregister all operations before
293 * unregistering the family, unregistering the family will cause
294 * all assigned operations to be unregistered automatically.
295 *
296 * Returns 0 on success or a negative error code.
297 */
298int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
299{
300 struct genl_ops *rc;
301
302 genl_lock();
303 list_for_each_entry(rc, &family->ops_list, ops_list) {
304 if (rc == ops) {
305 list_del(&ops->ops_list);
306 genl_unlock();
307 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
308 return 0;
309 }
310 }
311 genl_unlock();
312
313 return -ENOENT;
314}
315
316/**
317 * genl_register_family - register a generic netlink family
318 * @family: generic netlink family
319 *
320 * Registers the specified family after validating it first. Only one
321 * family may be registered with the same family name or identifier.
322 * The family id may equal GENL_ID_GENERATE causing an unique id to
323 * be automatically generated and assigned.
324 *
325 * Return 0 on success or a negative error code.
326 */
327int genl_register_family(struct genl_family *family)
328{
329 int err = -EINVAL;
330
331 if (family->id && family->id < GENL_MIN_ID)
332 goto errout;
333
334 if (family->id > GENL_MAX_ID)
335 goto errout;
336
337 INIT_LIST_HEAD(&family->ops_list);
2dbba6f7 338 INIT_LIST_HEAD(&family->mcast_groups);
482a8524
TG
339
340 genl_lock();
341
342 if (genl_family_find_byname(family->name)) {
343 err = -EEXIST;
344 goto errout_locked;
345 }
346
347 if (genl_family_find_byid(family->id)) {
348 err = -EEXIST;
349 goto errout_locked;
350 }
351
482a8524
TG
352 if (family->id == GENL_ID_GENERATE) {
353 u16 newid = genl_generate_id();
354
355 if (!newid) {
356 err = -ENOMEM;
357 goto errout_locked;
358 }
359
360 family->id = newid;
361 }
362
363 if (family->maxattr) {
364 family->attrbuf = kmalloc((family->maxattr+1) *
365 sizeof(struct nlattr *), GFP_KERNEL);
366 if (family->attrbuf == NULL) {
367 err = -ENOMEM;
e200bd80 368 goto errout_locked;
482a8524
TG
369 }
370 } else
371 family->attrbuf = NULL;
372
373 list_add_tail(&family->family_list, genl_family_chain(family->id));
374 genl_unlock();
375
376 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
377
378 return 0;
379
380errout_locked:
381 genl_unlock();
382errout:
383 return err;
384}
385
a7b11d73
MM
386/**
387 * genl_register_family_with_ops - register a generic netlink family
388 * @family: generic netlink family
389 * @ops: operations to be registered
390 * @n_ops: number of elements to register
391 *
392 * Registers the specified family and operations from the specified table.
393 * Only one family may be registered with the same family name or identifier.
394 *
395 * The family id may equal GENL_ID_GENERATE causing an unique id to
396 * be automatically generated and assigned.
397 *
398 * Either a doit or dumpit callback must be specified for every registered
399 * operation or the function will fail. Only one operation structure per
400 * command identifier may be registered.
401 *
402 * See include/net/genetlink.h for more documenation on the operations
403 * structure.
404 *
405 * This is equivalent to calling genl_register_family() followed by
406 * genl_register_ops() for every operation entry in the table taking
407 * care to unregister the family on error path.
408 *
409 * Return 0 on success or a negative error code.
410 */
411int genl_register_family_with_ops(struct genl_family *family,
412 struct genl_ops *ops, size_t n_ops)
413{
414 int err, i;
415
416 err = genl_register_family(family);
417 if (err)
418 return err;
419
420 for (i = 0; i < n_ops; ++i, ++ops) {
421 err = genl_register_ops(family, ops);
422 if (err)
423 goto err_out;
424 }
425 return 0;
426err_out:
427 genl_unregister_family(family);
428 return err;
429}
430EXPORT_SYMBOL(genl_register_family_with_ops);
431
482a8524
TG
432/**
433 * genl_unregister_family - unregister generic netlink family
434 * @family: generic netlink family
435 *
436 * Unregisters the specified family.
437 *
438 * Returns 0 on success or a negative error code.
439 */
440int genl_unregister_family(struct genl_family *family)
441{
442 struct genl_family *rc;
443
444 genl_lock();
445
910d6c32
PE
446 genl_unregister_mc_groups(family);
447
482a8524
TG
448 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
449 if (family->id != rc->id || strcmp(rc->name, family->name))
450 continue;
451
452 list_del(&rc->family_list);
453 INIT_LIST_HEAD(&family->ops_list);
454 genl_unlock();
455
482a8524
TG
456 kfree(family->attrbuf);
457 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
458 return 0;
459 }
460
461 genl_unlock();
462
463 return -ENOENT;
464}
465
1d00a4eb 466static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
482a8524
TG
467{
468 struct genl_ops *ops;
469 struct genl_family *family;
470 struct genl_info info;
471 struct genlmsghdr *hdr = nlmsg_data(nlh);
1d00a4eb 472 int hdrlen, err;
482a8524 473
746fac4d 474 family = genl_family_find_byid(nlh->nlmsg_type);
1d00a4eb
TG
475 if (family == NULL)
476 return -ENOENT;
482a8524
TG
477
478 hdrlen = GENL_HDRLEN + family->hdrsize;
479 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
1d00a4eb 480 return -EINVAL;
482a8524
TG
481
482 ops = genl_get_cmd(hdr->cmd, family);
1d00a4eb
TG
483 if (ops == NULL)
484 return -EOPNOTSUPP;
482a8524 485
1d00a4eb
TG
486 if ((ops->flags & GENL_ADMIN_PERM) &&
487 security_netlink_recv(skb, CAP_NET_ADMIN))
488 return -EPERM;
482a8524
TG
489
490 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1d00a4eb
TG
491 if (ops->dumpit == NULL)
492 return -EOPNOTSUPP;
482a8524 493
6d1a3fb5
PM
494 genl_unlock();
495 err = netlink_dump_start(genl_sock, skb, nlh,
496 ops->dumpit, ops->done);
497 genl_lock();
498 return err;
482a8524
TG
499 }
500
1d00a4eb
TG
501 if (ops->doit == NULL)
502 return -EOPNOTSUPP;
482a8524
TG
503
504 if (family->attrbuf) {
505 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
506 ops->policy);
507 if (err < 0)
1d00a4eb 508 return err;
482a8524
TG
509 }
510
511 info.snd_seq = nlh->nlmsg_seq;
512 info.snd_pid = NETLINK_CB(skb).pid;
513 info.nlhdr = nlh;
514 info.genlhdr = nlmsg_data(nlh);
515 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
516 info.attrs = family->attrbuf;
517
1d00a4eb 518 return ops->doit(skb, &info);
482a8524
TG
519}
520
cd40b7d3 521static void genl_rcv(struct sk_buff *skb)
482a8524 522{
cd40b7d3
DL
523 genl_lock();
524 netlink_rcv_skb(skb, &genl_rcv_msg);
525 genl_unlock();
482a8524
TG
526}
527
528/**************************************************************************
529 * Controller
530 **************************************************************************/
531
17c157c8
TG
532static struct genl_family genl_ctrl = {
533 .id = GENL_ID_CTRL,
534 .name = "nlctrl",
334c29a6 535 .version = 0x2,
17c157c8
TG
536 .maxattr = CTRL_ATTR_MAX,
537};
538
482a8524
TG
539static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
540 u32 flags, struct sk_buff *skb, u8 cmd)
541{
542 void *hdr;
543
17c157c8 544 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
482a8524
TG
545 if (hdr == NULL)
546 return -1;
547
548 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
549 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
eb328111
TG
550 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
551 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
552 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
553
e94ef682
TG
554 if (!list_empty(&family->ops_list)) {
555 struct nlattr *nla_ops;
556 struct genl_ops *ops;
557 int idx = 1;
eb328111 558
e94ef682
TG
559 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
560 if (nla_ops == NULL)
eb328111
TG
561 goto nla_put_failure;
562
e94ef682
TG
563 list_for_each_entry(ops, &family->ops_list, ops_list) {
564 struct nlattr *nest;
eb328111 565
e94ef682
TG
566 nest = nla_nest_start(skb, idx++);
567 if (nest == NULL)
568 goto nla_put_failure;
eb328111 569
e94ef682
TG
570 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
571 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
eb328111 572
e94ef682
TG
573 nla_nest_end(skb, nest);
574 }
575
576 nla_nest_end(skb, nla_ops);
577 }
482a8524 578
2dbba6f7
JB
579 if (!list_empty(&family->mcast_groups)) {
580 struct genl_multicast_group *grp;
581 struct nlattr *nla_grps;
582 int idx = 1;
583
584 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
585 if (nla_grps == NULL)
586 goto nla_put_failure;
587
588 list_for_each_entry(grp, &family->mcast_groups, list) {
589 struct nlattr *nest;
590
591 nest = nla_nest_start(skb, idx++);
592 if (nest == NULL)
593 goto nla_put_failure;
594
595 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
596 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
597 grp->name);
598
599 nla_nest_end(skb, nest);
600 }
601 nla_nest_end(skb, nla_grps);
602 }
603
604 return genlmsg_end(skb, hdr);
605
606nla_put_failure:
bc3ed28c
TG
607 genlmsg_cancel(skb, hdr);
608 return -EMSGSIZE;
2dbba6f7
JB
609}
610
611static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
612 u32 seq, u32 flags, struct sk_buff *skb,
613 u8 cmd)
614{
615 void *hdr;
616 struct nlattr *nla_grps;
617 struct nlattr *nest;
618
619 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
620 if (hdr == NULL)
621 return -1;
622
623 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
624 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
625
626 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
627 if (nla_grps == NULL)
628 goto nla_put_failure;
629
630 nest = nla_nest_start(skb, 1);
631 if (nest == NULL)
632 goto nla_put_failure;
633
634 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
635 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
636 grp->name);
637
638 nla_nest_end(skb, nest);
639 nla_nest_end(skb, nla_grps);
640
482a8524
TG
641 return genlmsg_end(skb, hdr);
642
643nla_put_failure:
bc3ed28c
TG
644 genlmsg_cancel(skb, hdr);
645 return -EMSGSIZE;
482a8524
TG
646}
647
648static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
649{
650
651 int i, n = 0;
652 struct genl_family *rt;
653 int chains_to_skip = cb->args[0];
654 int fams_to_skip = cb->args[1];
655
656 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
657 if (i < chains_to_skip)
658 continue;
659 n = 0;
660 list_for_each_entry(rt, genl_family_chain(i), family_list) {
661 if (++n < fams_to_skip)
662 continue;
663 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
664 cb->nlh->nlmsg_seq, NLM_F_MULTI,
665 skb, CTRL_CMD_NEWFAMILY) < 0)
666 goto errout;
667 }
668
669 fams_to_skip = 0;
670 }
671
672errout:
673 cb->args[0] = i;
674 cb->args[1] = n;
675
676 return skb->len;
677}
678
2dbba6f7
JB
679static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
680 u32 pid, int seq, u8 cmd)
482a8524
TG
681{
682 struct sk_buff *skb;
683 int err;
684
339bf98f 685 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
482a8524
TG
686 if (skb == NULL)
687 return ERR_PTR(-ENOBUFS);
688
689 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
690 if (err < 0) {
691 nlmsg_free(skb);
692 return ERR_PTR(err);
693 }
694
695 return skb;
696}
697
2dbba6f7
JB
698static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
699 u32 pid, int seq, u8 cmd)
700{
701 struct sk_buff *skb;
702 int err;
703
704 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
705 if (skb == NULL)
706 return ERR_PTR(-ENOBUFS);
707
708 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
709 if (err < 0) {
710 nlmsg_free(skb);
711 return ERR_PTR(err);
712 }
713
714 return skb;
715}
716
ef7c79ed 717static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
482a8524 718 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
5176f91e
TG
719 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
720 .len = GENL_NAMSIZ - 1 },
482a8524
TG
721};
722
723static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
724{
725 struct sk_buff *msg;
726 struct genl_family *res = NULL;
727 int err = -EINVAL;
728
729 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
730 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
731 res = genl_family_find_byid(id);
732 }
733
734 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
5176f91e 735 char *name;
482a8524 736
5176f91e 737 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
482a8524
TG
738 res = genl_family_find_byname(name);
739 }
740
741 if (res == NULL) {
742 err = -ENOENT;
743 goto errout;
744 }
745
2dbba6f7
JB
746 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
747 CTRL_CMD_NEWFAMILY);
482a8524
TG
748 if (IS_ERR(msg)) {
749 err = PTR_ERR(msg);
750 goto errout;
751 }
752
81878d27 753 err = genlmsg_reply(msg, info);
482a8524
TG
754errout:
755 return err;
756}
757
758static int genl_ctrl_event(int event, void *data)
759{
760 struct sk_buff *msg;
761
762 if (genl_sock == NULL)
763 return 0;
764
765 switch (event) {
766 case CTRL_CMD_NEWFAMILY:
767 case CTRL_CMD_DELFAMILY:
2dbba6f7
JB
768 msg = ctrl_build_family_msg(data, 0, 0, event);
769 if (IS_ERR(msg))
770 return PTR_ERR(msg);
771
772 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
773 break;
774 case CTRL_CMD_NEWMCAST_GRP:
775 case CTRL_CMD_DELMCAST_GRP:
776 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
482a8524
TG
777 if (IS_ERR(msg))
778 return PTR_ERR(msg);
779
d387f6ad 780 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
482a8524
TG
781 break;
782 }
783
784 return 0;
785}
786
787static struct genl_ops genl_ctrl_ops = {
788 .cmd = CTRL_CMD_GETFAMILY,
789 .doit = ctrl_getfamily,
790 .dumpit = ctrl_dumpfamily,
791 .policy = ctrl_policy,
792};
793
2dbba6f7
JB
794static struct genl_multicast_group notify_grp = {
795 .name = "notify",
796};
797
482a8524
TG
798static int __init genl_init(void)
799{
800 int i, err;
801
802 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
803 INIT_LIST_HEAD(&family_ht[i]);
804
805 err = genl_register_family(&genl_ctrl);
806 if (err < 0)
807 goto errout;
808
809 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
810 if (err < 0)
811 goto errout_register;
812
813 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
2dbba6f7
JB
814
815 /* we'll bump the group number right afterwards */
b4b51029 816 genl_sock = netlink_kernel_create(&init_net, NETLINK_GENERIC, 0,
6d1a3fb5 817 genl_rcv, &genl_mutex, THIS_MODULE);
e200bd80 818 if (genl_sock == NULL)
482a8524 819 panic("GENL: Cannot initialize generic netlink\n");
482a8524 820
2dbba6f7
JB
821 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
822 if (err < 0)
823 goto errout_register;
824
482a8524
TG
825 return 0;
826
827errout_register:
828 genl_unregister_family(&genl_ctrl);
829errout:
830 panic("GENL: Cannot register controller: %d\n", err);
482a8524
TG
831}
832
833subsys_initcall(genl_init);
834
835EXPORT_SYMBOL(genl_sock);
836EXPORT_SYMBOL(genl_register_ops);
837EXPORT_SYMBOL(genl_unregister_ops);
838EXPORT_SYMBOL(genl_register_family);
839EXPORT_SYMBOL(genl_unregister_family);