ARM: 6135/1: mx21/devices: fix USBOTG resource
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netlink / genetlink.c
blob06438fa2b1e5d1bfcef02b274941e0553a533bd2
1 /*
2 * NETLINK Generic Netlink Family
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
6 * Johannes Berg <johannes@sipsolutions.net>
7 */
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/errno.h>
13 #include <linux/types.h>
14 #include <linux/socket.h>
15 #include <linux/string.h>
16 #include <linux/skbuff.h>
17 #include <linux/mutex.h>
18 #include <linux/bitmap.h>
19 #include <net/sock.h>
20 #include <net/genetlink.h>
22 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
24 static inline void genl_lock(void)
26 mutex_lock(&genl_mutex);
29 static inline void genl_unlock(void)
31 mutex_unlock(&genl_mutex);
34 #define GENL_FAM_TAB_SIZE 16
35 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
37 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
39 * Bitmap of multicast groups that are currently in use.
41 * To avoid an allocation at boot of just one unsigned long,
42 * declare it global instead.
43 * Bit 0 is marked as already used since group 0 is invalid.
45 static unsigned long mc_group_start = 0x1;
46 static unsigned long *mc_groups = &mc_group_start;
47 static unsigned long mc_groups_longs = 1;
49 static int genl_ctrl_event(int event, void *data);
51 static inline unsigned int genl_family_hash(unsigned int id)
53 return id & GENL_FAM_TAB_MASK;
56 static inline struct list_head *genl_family_chain(unsigned int id)
58 return &family_ht[genl_family_hash(id)];
61 static struct genl_family *genl_family_find_byid(unsigned int id)
63 struct genl_family *f;
65 list_for_each_entry(f, genl_family_chain(id), family_list)
66 if (f->id == id)
67 return f;
69 return NULL;
72 static struct genl_family *genl_family_find_byname(char *name)
74 struct genl_family *f;
75 int i;
77 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
78 list_for_each_entry(f, genl_family_chain(i), family_list)
79 if (strcmp(f->name, name) == 0)
80 return f;
82 return NULL;
85 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
87 struct genl_ops *ops;
89 list_for_each_entry(ops, &family->ops_list, ops_list)
90 if (ops->cmd == cmd)
91 return ops;
93 return NULL;
96 /* Of course we are going to have problems once we hit
97 * 2^16 alive types, but that can only happen by year 2K
99 static inline u16 genl_generate_id(void)
101 static u16 id_gen_idx = GENL_MIN_ID;
102 int i;
104 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
105 if (!genl_family_find_byid(id_gen_idx))
106 return id_gen_idx;
107 if (++id_gen_idx > GENL_MAX_ID)
108 id_gen_idx = GENL_MIN_ID;
111 return 0;
114 static struct genl_multicast_group notify_grp;
117 * genl_register_mc_group - register a multicast group
119 * Registers the specified multicast group and notifies userspace
120 * about the new group.
122 * Returns 0 on success or a negative error code.
124 * @family: The generic netlink family the group shall be registered for.
125 * @grp: The group to register, must have a name.
127 int genl_register_mc_group(struct genl_family *family,
128 struct genl_multicast_group *grp)
130 int id;
131 unsigned long *new_groups;
132 int err = 0;
134 BUG_ON(grp->name[0] == '\0');
136 genl_lock();
138 /* special-case our own group */
139 if (grp == &notify_grp)
140 id = GENL_ID_CTRL;
141 else
142 id = find_first_zero_bit(mc_groups,
143 mc_groups_longs * BITS_PER_LONG);
146 if (id >= mc_groups_longs * BITS_PER_LONG) {
147 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
149 if (mc_groups == &mc_group_start) {
150 new_groups = kzalloc(nlen, GFP_KERNEL);
151 if (!new_groups) {
152 err = -ENOMEM;
153 goto out;
155 mc_groups = new_groups;
156 *mc_groups = mc_group_start;
157 } else {
158 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
159 if (!new_groups) {
160 err = -ENOMEM;
161 goto out;
163 mc_groups = new_groups;
164 mc_groups[mc_groups_longs] = 0;
166 mc_groups_longs++;
169 if (family->netnsok) {
170 struct net *net;
172 netlink_table_grab();
173 rcu_read_lock();
174 for_each_net_rcu(net) {
175 err = __netlink_change_ngroups(net->genl_sock,
176 mc_groups_longs * BITS_PER_LONG);
177 if (err) {
179 * No need to roll back, can only fail if
180 * memory allocation fails and then the
181 * number of _possible_ groups has been
182 * increased on some sockets which is ok.
184 rcu_read_unlock();
185 netlink_table_ungrab();
186 goto out;
189 rcu_read_unlock();
190 netlink_table_ungrab();
191 } else {
192 err = netlink_change_ngroups(init_net.genl_sock,
193 mc_groups_longs * BITS_PER_LONG);
194 if (err)
195 goto out;
198 grp->id = id;
199 set_bit(id, mc_groups);
200 list_add_tail(&grp->list, &family->mcast_groups);
201 grp->family = family;
203 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
204 out:
205 genl_unlock();
206 return err;
208 EXPORT_SYMBOL(genl_register_mc_group);
210 static void __genl_unregister_mc_group(struct genl_family *family,
211 struct genl_multicast_group *grp)
213 struct net *net;
214 BUG_ON(grp->family != family);
216 netlink_table_grab();
217 rcu_read_lock();
218 for_each_net_rcu(net)
219 __netlink_clear_multicast_users(net->genl_sock, grp->id);
220 rcu_read_unlock();
221 netlink_table_ungrab();
223 clear_bit(grp->id, mc_groups);
224 list_del(&grp->list);
225 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
226 grp->id = 0;
227 grp->family = NULL;
231 * genl_unregister_mc_group - unregister a multicast group
233 * Unregisters the specified multicast group and notifies userspace
234 * about it. All current listeners on the group are removed.
236 * Note: It is not necessary to unregister all multicast groups before
237 * unregistering the family, unregistering the family will cause
238 * all assigned multicast groups to be unregistered automatically.
240 * @family: Generic netlink family the group belongs to.
241 * @grp: The group to unregister, must have been registered successfully
242 * previously.
244 void genl_unregister_mc_group(struct genl_family *family,
245 struct genl_multicast_group *grp)
247 genl_lock();
248 __genl_unregister_mc_group(family, grp);
249 genl_unlock();
251 EXPORT_SYMBOL(genl_unregister_mc_group);
253 static void genl_unregister_mc_groups(struct genl_family *family)
255 struct genl_multicast_group *grp, *tmp;
257 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
258 __genl_unregister_mc_group(family, grp);
262 * genl_register_ops - register generic netlink operations
263 * @family: generic netlink family
264 * @ops: operations to be registered
266 * Registers the specified operations and assigns them to the specified
267 * family. Either a doit or dumpit callback must be specified or the
268 * operation will fail. Only one operation structure per command
269 * identifier may be registered.
271 * See include/net/genetlink.h for more documenation on the operations
272 * structure.
274 * Returns 0 on success or a negative error code.
276 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
278 int err = -EINVAL;
280 if (ops->dumpit == NULL && ops->doit == NULL)
281 goto errout;
283 if (genl_get_cmd(ops->cmd, family)) {
284 err = -EEXIST;
285 goto errout;
288 if (ops->dumpit)
289 ops->flags |= GENL_CMD_CAP_DUMP;
290 if (ops->doit)
291 ops->flags |= GENL_CMD_CAP_DO;
292 if (ops->policy)
293 ops->flags |= GENL_CMD_CAP_HASPOL;
295 genl_lock();
296 list_add_tail(&ops->ops_list, &family->ops_list);
297 genl_unlock();
299 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
300 err = 0;
301 errout:
302 return err;
306 * genl_unregister_ops - unregister generic netlink operations
307 * @family: generic netlink family
308 * @ops: operations to be unregistered
310 * Unregisters the specified operations and unassigns them from the
311 * specified family. The operation blocks until the current message
312 * processing has finished and doesn't start again until the
313 * unregister process has finished.
315 * Note: It is not necessary to unregister all operations before
316 * unregistering the family, unregistering the family will cause
317 * all assigned operations to be unregistered automatically.
319 * Returns 0 on success or a negative error code.
321 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
323 struct genl_ops *rc;
325 genl_lock();
326 list_for_each_entry(rc, &family->ops_list, ops_list) {
327 if (rc == ops) {
328 list_del(&ops->ops_list);
329 genl_unlock();
330 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
331 return 0;
334 genl_unlock();
336 return -ENOENT;
340 * genl_register_family - register a generic netlink family
341 * @family: generic netlink family
343 * Registers the specified family after validating it first. Only one
344 * family may be registered with the same family name or identifier.
345 * The family id may equal GENL_ID_GENERATE causing an unique id to
346 * be automatically generated and assigned.
348 * Return 0 on success or a negative error code.
350 int genl_register_family(struct genl_family *family)
352 int err = -EINVAL;
354 if (family->id && family->id < GENL_MIN_ID)
355 goto errout;
357 if (family->id > GENL_MAX_ID)
358 goto errout;
360 INIT_LIST_HEAD(&family->ops_list);
361 INIT_LIST_HEAD(&family->mcast_groups);
363 genl_lock();
365 if (genl_family_find_byname(family->name)) {
366 err = -EEXIST;
367 goto errout_locked;
370 if (family->id == GENL_ID_GENERATE) {
371 u16 newid = genl_generate_id();
373 if (!newid) {
374 err = -ENOMEM;
375 goto errout_locked;
378 family->id = newid;
379 } else if (genl_family_find_byid(family->id)) {
380 err = -EEXIST;
381 goto errout_locked;
384 if (family->maxattr) {
385 family->attrbuf = kmalloc((family->maxattr+1) *
386 sizeof(struct nlattr *), GFP_KERNEL);
387 if (family->attrbuf == NULL) {
388 err = -ENOMEM;
389 goto errout_locked;
391 } else
392 family->attrbuf = NULL;
394 list_add_tail(&family->family_list, genl_family_chain(family->id));
395 genl_unlock();
397 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
399 return 0;
401 errout_locked:
402 genl_unlock();
403 errout:
404 return err;
408 * genl_register_family_with_ops - register a generic netlink family
409 * @family: generic netlink family
410 * @ops: operations to be registered
411 * @n_ops: number of elements to register
413 * Registers the specified family and operations from the specified table.
414 * Only one family may be registered with the same family name or identifier.
416 * The family id may equal GENL_ID_GENERATE causing an unique id to
417 * be automatically generated and assigned.
419 * Either a doit or dumpit callback must be specified for every registered
420 * operation or the function will fail. Only one operation structure per
421 * command identifier may be registered.
423 * See include/net/genetlink.h for more documenation on the operations
424 * structure.
426 * This is equivalent to calling genl_register_family() followed by
427 * genl_register_ops() for every operation entry in the table taking
428 * care to unregister the family on error path.
430 * Return 0 on success or a negative error code.
432 int genl_register_family_with_ops(struct genl_family *family,
433 struct genl_ops *ops, size_t n_ops)
435 int err, i;
437 err = genl_register_family(family);
438 if (err)
439 return err;
441 for (i = 0; i < n_ops; ++i, ++ops) {
442 err = genl_register_ops(family, ops);
443 if (err)
444 goto err_out;
446 return 0;
447 err_out:
448 genl_unregister_family(family);
449 return err;
451 EXPORT_SYMBOL(genl_register_family_with_ops);
454 * genl_unregister_family - unregister generic netlink family
455 * @family: generic netlink family
457 * Unregisters the specified family.
459 * Returns 0 on success or a negative error code.
461 int genl_unregister_family(struct genl_family *family)
463 struct genl_family *rc;
465 genl_lock();
467 genl_unregister_mc_groups(family);
469 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
470 if (family->id != rc->id || strcmp(rc->name, family->name))
471 continue;
473 list_del(&rc->family_list);
474 INIT_LIST_HEAD(&family->ops_list);
475 genl_unlock();
477 kfree(family->attrbuf);
478 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
479 return 0;
482 genl_unlock();
484 return -ENOENT;
487 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
489 struct genl_ops *ops;
490 struct genl_family *family;
491 struct net *net = sock_net(skb->sk);
492 struct genl_info info;
493 struct genlmsghdr *hdr = nlmsg_data(nlh);
494 int hdrlen, err;
496 family = genl_family_find_byid(nlh->nlmsg_type);
497 if (family == NULL)
498 return -ENOENT;
500 /* this family doesn't exist in this netns */
501 if (!family->netnsok && !net_eq(net, &init_net))
502 return -ENOENT;
504 hdrlen = GENL_HDRLEN + family->hdrsize;
505 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
506 return -EINVAL;
508 ops = genl_get_cmd(hdr->cmd, family);
509 if (ops == NULL)
510 return -EOPNOTSUPP;
512 if ((ops->flags & GENL_ADMIN_PERM) &&
513 security_netlink_recv(skb, CAP_NET_ADMIN))
514 return -EPERM;
516 if (nlh->nlmsg_flags & NLM_F_DUMP) {
517 if (ops->dumpit == NULL)
518 return -EOPNOTSUPP;
520 genl_unlock();
521 err = netlink_dump_start(net->genl_sock, skb, nlh,
522 ops->dumpit, ops->done);
523 genl_lock();
524 return err;
527 if (ops->doit == NULL)
528 return -EOPNOTSUPP;
530 if (family->attrbuf) {
531 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
532 ops->policy);
533 if (err < 0)
534 return err;
537 info.snd_seq = nlh->nlmsg_seq;
538 info.snd_pid = NETLINK_CB(skb).pid;
539 info.nlhdr = nlh;
540 info.genlhdr = nlmsg_data(nlh);
541 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
542 info.attrs = family->attrbuf;
543 genl_info_net_set(&info, net);
545 return ops->doit(skb, &info);
548 static void genl_rcv(struct sk_buff *skb)
550 genl_lock();
551 netlink_rcv_skb(skb, &genl_rcv_msg);
552 genl_unlock();
555 /**************************************************************************
556 * Controller
557 **************************************************************************/
559 static struct genl_family genl_ctrl = {
560 .id = GENL_ID_CTRL,
561 .name = "nlctrl",
562 .version = 0x2,
563 .maxattr = CTRL_ATTR_MAX,
564 .netnsok = true,
567 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
568 u32 flags, struct sk_buff *skb, u8 cmd)
570 void *hdr;
572 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
573 if (hdr == NULL)
574 return -1;
576 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
577 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
578 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
579 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
580 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
582 if (!list_empty(&family->ops_list)) {
583 struct nlattr *nla_ops;
584 struct genl_ops *ops;
585 int idx = 1;
587 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
588 if (nla_ops == NULL)
589 goto nla_put_failure;
591 list_for_each_entry(ops, &family->ops_list, ops_list) {
592 struct nlattr *nest;
594 nest = nla_nest_start(skb, idx++);
595 if (nest == NULL)
596 goto nla_put_failure;
598 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
599 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
601 nla_nest_end(skb, nest);
604 nla_nest_end(skb, nla_ops);
607 if (!list_empty(&family->mcast_groups)) {
608 struct genl_multicast_group *grp;
609 struct nlattr *nla_grps;
610 int idx = 1;
612 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
613 if (nla_grps == NULL)
614 goto nla_put_failure;
616 list_for_each_entry(grp, &family->mcast_groups, list) {
617 struct nlattr *nest;
619 nest = nla_nest_start(skb, idx++);
620 if (nest == NULL)
621 goto nla_put_failure;
623 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
624 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
625 grp->name);
627 nla_nest_end(skb, nest);
629 nla_nest_end(skb, nla_grps);
632 return genlmsg_end(skb, hdr);
634 nla_put_failure:
635 genlmsg_cancel(skb, hdr);
636 return -EMSGSIZE;
639 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
640 u32 seq, u32 flags, struct sk_buff *skb,
641 u8 cmd)
643 void *hdr;
644 struct nlattr *nla_grps;
645 struct nlattr *nest;
647 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
648 if (hdr == NULL)
649 return -1;
651 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
652 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
654 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
655 if (nla_grps == NULL)
656 goto nla_put_failure;
658 nest = nla_nest_start(skb, 1);
659 if (nest == NULL)
660 goto nla_put_failure;
662 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
663 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
664 grp->name);
666 nla_nest_end(skb, nest);
667 nla_nest_end(skb, nla_grps);
669 return genlmsg_end(skb, hdr);
671 nla_put_failure:
672 genlmsg_cancel(skb, hdr);
673 return -EMSGSIZE;
676 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
679 int i, n = 0;
680 struct genl_family *rt;
681 struct net *net = sock_net(skb->sk);
682 int chains_to_skip = cb->args[0];
683 int fams_to_skip = cb->args[1];
685 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
686 n = 0;
687 list_for_each_entry(rt, genl_family_chain(i), family_list) {
688 if (!rt->netnsok && !net_eq(net, &init_net))
689 continue;
690 if (++n < fams_to_skip)
691 continue;
692 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
693 cb->nlh->nlmsg_seq, NLM_F_MULTI,
694 skb, CTRL_CMD_NEWFAMILY) < 0)
695 goto errout;
698 fams_to_skip = 0;
701 errout:
702 cb->args[0] = i;
703 cb->args[1] = n;
705 return skb->len;
708 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
709 u32 pid, int seq, u8 cmd)
711 struct sk_buff *skb;
712 int err;
714 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
715 if (skb == NULL)
716 return ERR_PTR(-ENOBUFS);
718 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
719 if (err < 0) {
720 nlmsg_free(skb);
721 return ERR_PTR(err);
724 return skb;
727 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
728 u32 pid, int seq, u8 cmd)
730 struct sk_buff *skb;
731 int err;
733 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
734 if (skb == NULL)
735 return ERR_PTR(-ENOBUFS);
737 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
738 if (err < 0) {
739 nlmsg_free(skb);
740 return ERR_PTR(err);
743 return skb;
746 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
747 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
748 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
749 .len = GENL_NAMSIZ - 1 },
752 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
754 struct sk_buff *msg;
755 struct genl_family *res = NULL;
756 int err = -EINVAL;
758 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
759 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
760 res = genl_family_find_byid(id);
761 err = -ENOENT;
764 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
765 char *name;
767 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
768 res = genl_family_find_byname(name);
769 err = -ENOENT;
772 if (res == NULL)
773 return err;
775 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
776 /* family doesn't exist here */
777 return -ENOENT;
780 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
781 CTRL_CMD_NEWFAMILY);
782 if (IS_ERR(msg))
783 return PTR_ERR(msg);
785 return genlmsg_reply(msg, info);
788 static int genl_ctrl_event(int event, void *data)
790 struct sk_buff *msg;
791 struct genl_family *family;
792 struct genl_multicast_group *grp;
794 /* genl is still initialising */
795 if (!init_net.genl_sock)
796 return 0;
798 switch (event) {
799 case CTRL_CMD_NEWFAMILY:
800 case CTRL_CMD_DELFAMILY:
801 family = data;
802 msg = ctrl_build_family_msg(family, 0, 0, event);
803 break;
804 case CTRL_CMD_NEWMCAST_GRP:
805 case CTRL_CMD_DELMCAST_GRP:
806 grp = data;
807 family = grp->family;
808 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
809 break;
810 default:
811 return -EINVAL;
814 if (IS_ERR(msg))
815 return PTR_ERR(msg);
817 if (!family->netnsok) {
818 genlmsg_multicast_netns(&init_net, msg, 0,
819 GENL_ID_CTRL, GFP_KERNEL);
820 } else {
821 rcu_read_lock();
822 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
823 rcu_read_unlock();
826 return 0;
829 static struct genl_ops genl_ctrl_ops = {
830 .cmd = CTRL_CMD_GETFAMILY,
831 .doit = ctrl_getfamily,
832 .dumpit = ctrl_dumpfamily,
833 .policy = ctrl_policy,
836 static struct genl_multicast_group notify_grp = {
837 .name = "notify",
840 static int __net_init genl_pernet_init(struct net *net)
842 /* we'll bump the group number right afterwards */
843 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
844 genl_rcv, &genl_mutex,
845 THIS_MODULE);
847 if (!net->genl_sock && net_eq(net, &init_net))
848 panic("GENL: Cannot initialize generic netlink\n");
850 if (!net->genl_sock)
851 return -ENOMEM;
853 return 0;
856 static void __net_exit genl_pernet_exit(struct net *net)
858 netlink_kernel_release(net->genl_sock);
859 net->genl_sock = NULL;
862 static struct pernet_operations genl_pernet_ops = {
863 .init = genl_pernet_init,
864 .exit = genl_pernet_exit,
867 static int __init genl_init(void)
869 int i, err;
871 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
872 INIT_LIST_HEAD(&family_ht[i]);
874 err = genl_register_family(&genl_ctrl);
875 if (err < 0)
876 goto problem;
878 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
879 if (err < 0)
880 goto problem;
882 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
884 err = register_pernet_subsys(&genl_pernet_ops);
885 if (err)
886 goto problem;
888 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
889 if (err < 0)
890 goto problem;
892 return 0;
894 problem:
895 panic("GENL: Cannot register controller: %d\n", err);
898 subsys_initcall(genl_init);
900 EXPORT_SYMBOL(genl_register_ops);
901 EXPORT_SYMBOL(genl_unregister_ops);
902 EXPORT_SYMBOL(genl_register_family);
903 EXPORT_SYMBOL(genl_unregister_family);
905 static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
906 gfp_t flags)
908 struct sk_buff *tmp;
909 struct net *net, *prev = NULL;
910 int err;
912 for_each_net_rcu(net) {
913 if (prev) {
914 tmp = skb_clone(skb, flags);
915 if (!tmp) {
916 err = -ENOMEM;
917 goto error;
919 err = nlmsg_multicast(prev->genl_sock, tmp,
920 pid, group, flags);
921 if (err)
922 goto error;
925 prev = net;
928 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
929 error:
930 kfree_skb(skb);
931 return err;
934 int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
935 gfp_t flags)
937 return genlmsg_mcast(skb, pid, group, flags);
939 EXPORT_SYMBOL(genlmsg_multicast_allns);