netlink: Export genl_lock() API for use by modules
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netlink / genetlink.c
bloba28fda7420d9d2699cb0c0f7a6707ecd9bd0503c
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/errno.h>
12 #include <linux/types.h>
13 #include <linux/socket.h>
14 #include <linux/string.h>
15 #include <linux/skbuff.h>
16 #include <linux/mutex.h>
17 #include <linux/bitmap.h>
18 #include <net/sock.h>
19 #include <net/genetlink.h>
21 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
23 void genl_lock(void)
25 mutex_lock(&genl_mutex);
27 EXPORT_SYMBOL(genl_lock);
29 void genl_unlock(void)
31 mutex_unlock(&genl_mutex);
33 EXPORT_SYMBOL(genl_unlock);
35 #define GENL_FAM_TAB_SIZE 16
36 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
38 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
40 * Bitmap of multicast groups that are currently in use.
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.
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;
50 static int genl_ctrl_event(int event, void *data);
52 static inline unsigned int genl_family_hash(unsigned int id)
54 return id & GENL_FAM_TAB_MASK;
57 static inline struct list_head *genl_family_chain(unsigned int id)
59 return &family_ht[genl_family_hash(id)];
62 static struct genl_family *genl_family_find_byid(unsigned int id)
64 struct genl_family *f;
66 list_for_each_entry(f, genl_family_chain(id), family_list)
67 if (f->id == id)
68 return f;
70 return NULL;
73 static struct genl_family *genl_family_find_byname(char *name)
75 struct genl_family *f;
76 int i;
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;
83 return NULL;
86 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
88 struct genl_ops *ops;
90 list_for_each_entry(ops, &family->ops_list, ops_list)
91 if (ops->cmd == cmd)
92 return ops;
94 return NULL;
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
100 static inline u16 genl_generate_id(void)
102 static u16 id_gen_idx = GENL_MIN_ID;
103 int i;
105 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
106 if (!genl_family_find_byid(id_gen_idx))
107 return id_gen_idx;
108 if (++id_gen_idx > GENL_MAX_ID)
109 id_gen_idx = GENL_MIN_ID;
112 return 0;
115 static struct genl_multicast_group notify_grp;
118 * genl_register_mc_group - register a multicast group
120 * Registers the specified multicast group and notifies userspace
121 * about the new group.
123 * Returns 0 on success or a negative error code.
125 * @family: The generic netlink family the group shall be registered for.
126 * @grp: The group to register, must have a name.
128 int genl_register_mc_group(struct genl_family *family,
129 struct genl_multicast_group *grp)
131 int id;
132 unsigned long *new_groups;
133 int err = 0;
135 BUG_ON(grp->name[0] == '\0');
137 genl_lock();
139 /* special-case our own group */
140 if (grp == &notify_grp)
141 id = GENL_ID_CTRL;
142 else
143 id = find_first_zero_bit(mc_groups,
144 mc_groups_longs * BITS_PER_LONG);
147 if (id >= mc_groups_longs * BITS_PER_LONG) {
148 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
150 if (mc_groups == &mc_group_start) {
151 new_groups = kzalloc(nlen, GFP_KERNEL);
152 if (!new_groups) {
153 err = -ENOMEM;
154 goto out;
156 mc_groups = new_groups;
157 *mc_groups = mc_group_start;
158 } else {
159 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
160 if (!new_groups) {
161 err = -ENOMEM;
162 goto out;
164 mc_groups = new_groups;
165 mc_groups[mc_groups_longs] = 0;
167 mc_groups_longs++;
170 if (family->netnsok) {
171 struct net *net;
173 netlink_table_grab();
174 rcu_read_lock();
175 for_each_net_rcu(net) {
176 err = __netlink_change_ngroups(net->genl_sock,
177 mc_groups_longs * BITS_PER_LONG);
178 if (err) {
180 * No need to roll back, can only fail if
181 * memory allocation fails and then the
182 * number of _possible_ groups has been
183 * increased on some sockets which is ok.
185 rcu_read_unlock();
186 netlink_table_ungrab();
187 goto out;
190 rcu_read_unlock();
191 netlink_table_ungrab();
192 } else {
193 err = netlink_change_ngroups(init_net.genl_sock,
194 mc_groups_longs * BITS_PER_LONG);
195 if (err)
196 goto out;
199 grp->id = id;
200 set_bit(id, mc_groups);
201 list_add_tail(&grp->list, &family->mcast_groups);
202 grp->family = family;
204 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
205 out:
206 genl_unlock();
207 return err;
209 EXPORT_SYMBOL(genl_register_mc_group);
211 static void __genl_unregister_mc_group(struct genl_family *family,
212 struct genl_multicast_group *grp)
214 struct net *net;
215 BUG_ON(grp->family != family);
217 netlink_table_grab();
218 rcu_read_lock();
219 for_each_net_rcu(net)
220 __netlink_clear_multicast_users(net->genl_sock, grp->id);
221 rcu_read_unlock();
222 netlink_table_ungrab();
224 clear_bit(grp->id, mc_groups);
225 list_del(&grp->list);
226 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
227 grp->id = 0;
228 grp->family = NULL;
232 * genl_unregister_mc_group - unregister a multicast group
234 * Unregisters the specified multicast group and notifies userspace
235 * about it. All current listeners on the group are removed.
237 * Note: It is not necessary to unregister all multicast groups before
238 * unregistering the family, unregistering the family will cause
239 * all assigned multicast groups to be unregistered automatically.
241 * @family: Generic netlink family the group belongs to.
242 * @grp: The group to unregister, must have been registered successfully
243 * previously.
245 void genl_unregister_mc_group(struct genl_family *family,
246 struct genl_multicast_group *grp)
248 genl_lock();
249 __genl_unregister_mc_group(family, grp);
250 genl_unlock();
252 EXPORT_SYMBOL(genl_unregister_mc_group);
254 static void genl_unregister_mc_groups(struct genl_family *family)
256 struct genl_multicast_group *grp, *tmp;
258 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
259 __genl_unregister_mc_group(family, grp);
263 * genl_register_ops - register generic netlink operations
264 * @family: generic netlink family
265 * @ops: operations to be registered
267 * Registers the specified operations and assigns them to the specified
268 * family. Either a doit or dumpit callback must be specified or the
269 * operation will fail. Only one operation structure per command
270 * identifier may be registered.
272 * See include/net/genetlink.h for more documenation on the operations
273 * structure.
275 * Returns 0 on success or a negative error code.
277 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
279 int err = -EINVAL;
281 if (ops->dumpit == NULL && ops->doit == NULL)
282 goto errout;
284 if (genl_get_cmd(ops->cmd, family)) {
285 err = -EEXIST;
286 goto errout;
289 if (ops->dumpit)
290 ops->flags |= GENL_CMD_CAP_DUMP;
291 if (ops->doit)
292 ops->flags |= GENL_CMD_CAP_DO;
293 if (ops->policy)
294 ops->flags |= GENL_CMD_CAP_HASPOL;
296 genl_lock();
297 list_add_tail(&ops->ops_list, &family->ops_list);
298 genl_unlock();
300 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
301 err = 0;
302 errout:
303 return err;
307 * genl_unregister_ops - unregister generic netlink operations
308 * @family: generic netlink family
309 * @ops: operations to be unregistered
311 * Unregisters the specified operations and unassigns them from the
312 * specified family. The operation blocks until the current message
313 * processing has finished and doesn't start again until the
314 * unregister process has finished.
316 * Note: It is not necessary to unregister all operations before
317 * unregistering the family, unregistering the family will cause
318 * all assigned operations to be unregistered automatically.
320 * Returns 0 on success or a negative error code.
322 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
324 struct genl_ops *rc;
326 genl_lock();
327 list_for_each_entry(rc, &family->ops_list, ops_list) {
328 if (rc == ops) {
329 list_del(&ops->ops_list);
330 genl_unlock();
331 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
332 return 0;
335 genl_unlock();
337 return -ENOENT;
341 * genl_register_family - register a generic netlink family
342 * @family: generic netlink family
344 * Registers the specified family after validating it first. Only one
345 * family may be registered with the same family name or identifier.
346 * The family id may equal GENL_ID_GENERATE causing an unique id to
347 * be automatically generated and assigned.
349 * Return 0 on success or a negative error code.
351 int genl_register_family(struct genl_family *family)
353 int err = -EINVAL;
355 if (family->id && family->id < GENL_MIN_ID)
356 goto errout;
358 if (family->id > GENL_MAX_ID)
359 goto errout;
361 INIT_LIST_HEAD(&family->ops_list);
362 INIT_LIST_HEAD(&family->mcast_groups);
364 genl_lock();
366 if (genl_family_find_byname(family->name)) {
367 err = -EEXIST;
368 goto errout_locked;
371 if (family->id == GENL_ID_GENERATE) {
372 u16 newid = genl_generate_id();
374 if (!newid) {
375 err = -ENOMEM;
376 goto errout_locked;
379 family->id = newid;
380 } else if (genl_family_find_byid(family->id)) {
381 err = -EEXIST;
382 goto errout_locked;
385 if (family->maxattr) {
386 family->attrbuf = kmalloc((family->maxattr+1) *
387 sizeof(struct nlattr *), GFP_KERNEL);
388 if (family->attrbuf == NULL) {
389 err = -ENOMEM;
390 goto errout_locked;
392 } else
393 family->attrbuf = NULL;
395 list_add_tail(&family->family_list, genl_family_chain(family->id));
396 genl_unlock();
398 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
400 return 0;
402 errout_locked:
403 genl_unlock();
404 errout:
405 return err;
409 * genl_register_family_with_ops - register a generic netlink family
410 * @family: generic netlink family
411 * @ops: operations to be registered
412 * @n_ops: number of elements to register
414 * Registers the specified family and operations from the specified table.
415 * Only one family may be registered with the same family name or identifier.
417 * The family id may equal GENL_ID_GENERATE causing an unique id to
418 * be automatically generated and assigned.
420 * Either a doit or dumpit callback must be specified for every registered
421 * operation or the function will fail. Only one operation structure per
422 * command identifier may be registered.
424 * See include/net/genetlink.h for more documenation on the operations
425 * structure.
427 * This is equivalent to calling genl_register_family() followed by
428 * genl_register_ops() for every operation entry in the table taking
429 * care to unregister the family on error path.
431 * Return 0 on success or a negative error code.
433 int genl_register_family_with_ops(struct genl_family *family,
434 struct genl_ops *ops, size_t n_ops)
436 int err, i;
438 err = genl_register_family(family);
439 if (err)
440 return err;
442 for (i = 0; i < n_ops; ++i, ++ops) {
443 err = genl_register_ops(family, ops);
444 if (err)
445 goto err_out;
447 return 0;
448 err_out:
449 genl_unregister_family(family);
450 return err;
452 EXPORT_SYMBOL(genl_register_family_with_ops);
455 * genl_unregister_family - unregister generic netlink family
456 * @family: generic netlink family
458 * Unregisters the specified family.
460 * Returns 0 on success or a negative error code.
462 int genl_unregister_family(struct genl_family *family)
464 struct genl_family *rc;
466 genl_lock();
468 genl_unregister_mc_groups(family);
470 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
471 if (family->id != rc->id || strcmp(rc->name, family->name))
472 continue;
474 list_del(&rc->family_list);
475 INIT_LIST_HEAD(&family->ops_list);
476 genl_unlock();
478 kfree(family->attrbuf);
479 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
480 return 0;
483 genl_unlock();
485 return -ENOENT;
488 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
490 struct genl_ops *ops;
491 struct genl_family *family;
492 struct net *net = sock_net(skb->sk);
493 struct genl_info info;
494 struct genlmsghdr *hdr = nlmsg_data(nlh);
495 int hdrlen, err;
497 family = genl_family_find_byid(nlh->nlmsg_type);
498 if (family == NULL)
499 return -ENOENT;
501 /* this family doesn't exist in this netns */
502 if (!family->netnsok && !net_eq(net, &init_net))
503 return -ENOENT;
505 hdrlen = GENL_HDRLEN + family->hdrsize;
506 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
507 return -EINVAL;
509 ops = genl_get_cmd(hdr->cmd, family);
510 if (ops == NULL)
511 return -EOPNOTSUPP;
513 if ((ops->flags & GENL_ADMIN_PERM) &&
514 security_netlink_recv(skb, CAP_NET_ADMIN))
515 return -EPERM;
517 if (nlh->nlmsg_flags & NLM_F_DUMP) {
518 if (ops->dumpit == NULL)
519 return -EOPNOTSUPP;
521 genl_unlock();
522 err = netlink_dump_start(net->genl_sock, skb, nlh,
523 ops->dumpit, ops->done);
524 genl_lock();
525 return err;
528 if (ops->doit == NULL)
529 return -EOPNOTSUPP;
531 if (family->attrbuf) {
532 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
533 ops->policy);
534 if (err < 0)
535 return err;
538 info.snd_seq = nlh->nlmsg_seq;
539 info.snd_pid = NETLINK_CB(skb).pid;
540 info.nlhdr = nlh;
541 info.genlhdr = nlmsg_data(nlh);
542 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
543 info.attrs = family->attrbuf;
544 genl_info_net_set(&info, net);
546 return ops->doit(skb, &info);
549 static void genl_rcv(struct sk_buff *skb)
551 genl_lock();
552 netlink_rcv_skb(skb, &genl_rcv_msg);
553 genl_unlock();
556 /**************************************************************************
557 * Controller
558 **************************************************************************/
560 static struct genl_family genl_ctrl = {
561 .id = GENL_ID_CTRL,
562 .name = "nlctrl",
563 .version = 0x2,
564 .maxattr = CTRL_ATTR_MAX,
565 .netnsok = true,
568 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
569 u32 flags, struct sk_buff *skb, u8 cmd)
571 void *hdr;
573 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
574 if (hdr == NULL)
575 return -1;
577 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
578 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
579 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
580 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
581 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
583 if (!list_empty(&family->ops_list)) {
584 struct nlattr *nla_ops;
585 struct genl_ops *ops;
586 int idx = 1;
588 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
589 if (nla_ops == NULL)
590 goto nla_put_failure;
592 list_for_each_entry(ops, &family->ops_list, ops_list) {
593 struct nlattr *nest;
595 nest = nla_nest_start(skb, idx++);
596 if (nest == NULL)
597 goto nla_put_failure;
599 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
600 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
602 nla_nest_end(skb, nest);
605 nla_nest_end(skb, nla_ops);
608 if (!list_empty(&family->mcast_groups)) {
609 struct genl_multicast_group *grp;
610 struct nlattr *nla_grps;
611 int idx = 1;
613 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
614 if (nla_grps == NULL)
615 goto nla_put_failure;
617 list_for_each_entry(grp, &family->mcast_groups, list) {
618 struct nlattr *nest;
620 nest = nla_nest_start(skb, idx++);
621 if (nest == NULL)
622 goto nla_put_failure;
624 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
625 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
626 grp->name);
628 nla_nest_end(skb, nest);
630 nla_nest_end(skb, nla_grps);
633 return genlmsg_end(skb, hdr);
635 nla_put_failure:
636 genlmsg_cancel(skb, hdr);
637 return -EMSGSIZE;
640 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
641 u32 seq, u32 flags, struct sk_buff *skb,
642 u8 cmd)
644 void *hdr;
645 struct nlattr *nla_grps;
646 struct nlattr *nest;
648 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
649 if (hdr == NULL)
650 return -1;
652 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
653 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
655 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
656 if (nla_grps == NULL)
657 goto nla_put_failure;
659 nest = nla_nest_start(skb, 1);
660 if (nest == NULL)
661 goto nla_put_failure;
663 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
664 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
665 grp->name);
667 nla_nest_end(skb, nest);
668 nla_nest_end(skb, nla_grps);
670 return genlmsg_end(skb, hdr);
672 nla_put_failure:
673 genlmsg_cancel(skb, hdr);
674 return -EMSGSIZE;
677 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
680 int i, n = 0;
681 struct genl_family *rt;
682 struct net *net = sock_net(skb->sk);
683 int chains_to_skip = cb->args[0];
684 int fams_to_skip = cb->args[1];
686 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
687 n = 0;
688 list_for_each_entry(rt, genl_family_chain(i), family_list) {
689 if (!rt->netnsok && !net_eq(net, &init_net))
690 continue;
691 if (++n < fams_to_skip)
692 continue;
693 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
694 cb->nlh->nlmsg_seq, NLM_F_MULTI,
695 skb, CTRL_CMD_NEWFAMILY) < 0)
696 goto errout;
699 fams_to_skip = 0;
702 errout:
703 cb->args[0] = i;
704 cb->args[1] = n;
706 return skb->len;
709 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
710 u32 pid, int seq, u8 cmd)
712 struct sk_buff *skb;
713 int err;
715 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
716 if (skb == NULL)
717 return ERR_PTR(-ENOBUFS);
719 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
720 if (err < 0) {
721 nlmsg_free(skb);
722 return ERR_PTR(err);
725 return skb;
728 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
729 u32 pid, int seq, u8 cmd)
731 struct sk_buff *skb;
732 int err;
734 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
735 if (skb == NULL)
736 return ERR_PTR(-ENOBUFS);
738 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
739 if (err < 0) {
740 nlmsg_free(skb);
741 return ERR_PTR(err);
744 return skb;
747 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
748 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
749 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
750 .len = GENL_NAMSIZ - 1 },
753 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
755 struct sk_buff *msg;
756 struct genl_family *res = NULL;
757 int err = -EINVAL;
759 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
760 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
761 res = genl_family_find_byid(id);
762 err = -ENOENT;
765 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
766 char *name;
768 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
769 res = genl_family_find_byname(name);
770 err = -ENOENT;
773 if (res == NULL)
774 return err;
776 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
777 /* family doesn't exist here */
778 return -ENOENT;
781 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
782 CTRL_CMD_NEWFAMILY);
783 if (IS_ERR(msg))
784 return PTR_ERR(msg);
786 return genlmsg_reply(msg, info);
789 static int genl_ctrl_event(int event, void *data)
791 struct sk_buff *msg;
792 struct genl_family *family;
793 struct genl_multicast_group *grp;
795 /* genl is still initialising */
796 if (!init_net.genl_sock)
797 return 0;
799 switch (event) {
800 case CTRL_CMD_NEWFAMILY:
801 case CTRL_CMD_DELFAMILY:
802 family = data;
803 msg = ctrl_build_family_msg(family, 0, 0, event);
804 break;
805 case CTRL_CMD_NEWMCAST_GRP:
806 case CTRL_CMD_DELMCAST_GRP:
807 grp = data;
808 family = grp->family;
809 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
810 break;
811 default:
812 return -EINVAL;
815 if (IS_ERR(msg))
816 return PTR_ERR(msg);
818 if (!family->netnsok) {
819 genlmsg_multicast_netns(&init_net, msg, 0,
820 GENL_ID_CTRL, GFP_KERNEL);
821 } else {
822 rcu_read_lock();
823 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
824 rcu_read_unlock();
827 return 0;
830 static struct genl_ops genl_ctrl_ops = {
831 .cmd = CTRL_CMD_GETFAMILY,
832 .doit = ctrl_getfamily,
833 .dumpit = ctrl_dumpfamily,
834 .policy = ctrl_policy,
837 static struct genl_multicast_group notify_grp = {
838 .name = "notify",
841 static int __net_init genl_pernet_init(struct net *net)
843 /* we'll bump the group number right afterwards */
844 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
845 genl_rcv, &genl_mutex,
846 THIS_MODULE);
848 if (!net->genl_sock && net_eq(net, &init_net))
849 panic("GENL: Cannot initialize generic netlink\n");
851 if (!net->genl_sock)
852 return -ENOMEM;
854 return 0;
857 static void __net_exit genl_pernet_exit(struct net *net)
859 netlink_kernel_release(net->genl_sock);
860 net->genl_sock = NULL;
863 static struct pernet_operations genl_pernet_ops = {
864 .init = genl_pernet_init,
865 .exit = genl_pernet_exit,
868 static int __init genl_init(void)
870 int i, err;
872 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
873 INIT_LIST_HEAD(&family_ht[i]);
875 err = genl_register_family(&genl_ctrl);
876 if (err < 0)
877 goto problem;
879 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
880 if (err < 0)
881 goto problem;
883 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
885 err = register_pernet_subsys(&genl_pernet_ops);
886 if (err)
887 goto problem;
889 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
890 if (err < 0)
891 goto problem;
893 return 0;
895 problem:
896 panic("GENL: Cannot register controller: %d\n", err);
899 subsys_initcall(genl_init);
901 EXPORT_SYMBOL(genl_register_ops);
902 EXPORT_SYMBOL(genl_unregister_ops);
903 EXPORT_SYMBOL(genl_register_family);
904 EXPORT_SYMBOL(genl_unregister_family);
906 static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
907 gfp_t flags)
909 struct sk_buff *tmp;
910 struct net *net, *prev = NULL;
911 int err;
913 for_each_net_rcu(net) {
914 if (prev) {
915 tmp = skb_clone(skb, flags);
916 if (!tmp) {
917 err = -ENOMEM;
918 goto error;
920 err = nlmsg_multicast(prev->genl_sock, tmp,
921 pid, group, flags);
922 if (err)
923 goto error;
926 prev = net;
929 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
930 error:
931 kfree_skb(skb);
932 return err;
935 int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
936 gfp_t flags)
938 return genlmsg_mcast(skb, pid, group, flags);
940 EXPORT_SYMBOL(genlmsg_multicast_allns);