genetlink: fix netns vs. netlink table locking
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netlink / genetlink.c
blob566941e03363fbdd7591ded5ed16b41f0a903c7c
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 static inline void genl_lock(void)
25 mutex_lock(&genl_mutex);
28 static inline void genl_unlock(void)
30 mutex_unlock(&genl_mutex);
33 #define GENL_FAM_TAB_SIZE 16
34 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
36 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
38 * Bitmap of multicast groups that are currently in use.
40 * To avoid an allocation at boot of just one unsigned long,
41 * declare it global instead.
42 * Bit 0 is marked as already used since group 0 is invalid.
44 static unsigned long mc_group_start = 0x1;
45 static unsigned long *mc_groups = &mc_group_start;
46 static unsigned long mc_groups_longs = 1;
48 static int genl_ctrl_event(int event, void *data);
50 static inline unsigned int genl_family_hash(unsigned int id)
52 return id & GENL_FAM_TAB_MASK;
55 static inline struct list_head *genl_family_chain(unsigned int id)
57 return &family_ht[genl_family_hash(id)];
60 static struct genl_family *genl_family_find_byid(unsigned int id)
62 struct genl_family *f;
64 list_for_each_entry(f, genl_family_chain(id), family_list)
65 if (f->id == id)
66 return f;
68 return NULL;
71 static struct genl_family *genl_family_find_byname(char *name)
73 struct genl_family *f;
74 int i;
76 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
77 list_for_each_entry(f, genl_family_chain(i), family_list)
78 if (strcmp(f->name, name) == 0)
79 return f;
81 return NULL;
84 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
86 struct genl_ops *ops;
88 list_for_each_entry(ops, &family->ops_list, ops_list)
89 if (ops->cmd == cmd)
90 return ops;
92 return NULL;
95 /* Of course we are going to have problems once we hit
96 * 2^16 alive types, but that can only happen by year 2K
98 static inline u16 genl_generate_id(void)
100 static u16 id_gen_idx;
101 int overflowed = 0;
103 do {
104 if (id_gen_idx == 0)
105 id_gen_idx = GENL_MIN_ID;
107 if (++id_gen_idx > GENL_MAX_ID) {
108 if (!overflowed) {
109 overflowed = 1;
110 id_gen_idx = 0;
111 continue;
112 } else
113 return 0;
116 } while (genl_family_find_byid(id_gen_idx));
118 return id_gen_idx;
121 static struct genl_multicast_group notify_grp;
124 * genl_register_mc_group - register a multicast group
126 * Registers the specified multicast group and notifies userspace
127 * about the new group.
129 * Returns 0 on success or a negative error code.
131 * @family: The generic netlink family the group shall be registered for.
132 * @grp: The group to register, must have a name.
134 int genl_register_mc_group(struct genl_family *family,
135 struct genl_multicast_group *grp)
137 int id;
138 unsigned long *new_groups;
139 int err = 0;
141 BUG_ON(grp->name[0] == '\0');
143 genl_lock();
145 /* special-case our own group */
146 if (grp == &notify_grp)
147 id = GENL_ID_CTRL;
148 else
149 id = find_first_zero_bit(mc_groups,
150 mc_groups_longs * BITS_PER_LONG);
153 if (id >= mc_groups_longs * BITS_PER_LONG) {
154 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
156 if (mc_groups == &mc_group_start) {
157 new_groups = kzalloc(nlen, GFP_KERNEL);
158 if (!new_groups) {
159 err = -ENOMEM;
160 goto out;
162 mc_groups = new_groups;
163 *mc_groups = mc_group_start;
164 } else {
165 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
166 if (!new_groups) {
167 err = -ENOMEM;
168 goto out;
170 mc_groups = new_groups;
171 mc_groups[mc_groups_longs] = 0;
173 mc_groups_longs++;
176 if (family->netnsok) {
177 struct net *net;
179 netlink_table_grab();
180 rcu_read_lock();
181 for_each_net_rcu(net) {
182 err = __netlink_change_ngroups(net->genl_sock,
183 mc_groups_longs * BITS_PER_LONG);
184 if (err) {
186 * No need to roll back, can only fail if
187 * memory allocation fails and then the
188 * number of _possible_ groups has been
189 * increased on some sockets which is ok.
191 rcu_read_unlock();
192 netlink_table_ungrab();
193 goto out;
196 rcu_read_unlock();
197 netlink_table_ungrab();
198 } else {
199 err = netlink_change_ngroups(init_net.genl_sock,
200 mc_groups_longs * BITS_PER_LONG);
201 if (err)
202 goto out;
205 grp->id = id;
206 set_bit(id, mc_groups);
207 list_add_tail(&grp->list, &family->mcast_groups);
208 grp->family = family;
210 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
211 out:
212 genl_unlock();
213 return err;
215 EXPORT_SYMBOL(genl_register_mc_group);
217 static void __genl_unregister_mc_group(struct genl_family *family,
218 struct genl_multicast_group *grp)
220 struct net *net;
221 BUG_ON(grp->family != family);
223 rcu_read_lock();
224 for_each_net_rcu(net)
225 netlink_clear_multicast_users(net->genl_sock, grp->id);
226 rcu_read_unlock();
228 clear_bit(grp->id, mc_groups);
229 list_del(&grp->list);
230 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
231 grp->id = 0;
232 grp->family = NULL;
236 * genl_unregister_mc_group - unregister a multicast group
238 * Unregisters the specified multicast group and notifies userspace
239 * about it. All current listeners on the group are removed.
241 * Note: It is not necessary to unregister all multicast groups before
242 * unregistering the family, unregistering the family will cause
243 * all assigned multicast groups to be unregistered automatically.
245 * @family: Generic netlink family the group belongs to.
246 * @grp: The group to unregister, must have been registered successfully
247 * previously.
249 void genl_unregister_mc_group(struct genl_family *family,
250 struct genl_multicast_group *grp)
252 genl_lock();
253 __genl_unregister_mc_group(family, grp);
254 genl_unlock();
256 EXPORT_SYMBOL(genl_unregister_mc_group);
258 static void genl_unregister_mc_groups(struct genl_family *family)
260 struct genl_multicast_group *grp, *tmp;
262 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
263 __genl_unregister_mc_group(family, grp);
267 * genl_register_ops - register generic netlink operations
268 * @family: generic netlink family
269 * @ops: operations to be registered
271 * Registers the specified operations and assigns them to the specified
272 * family. Either a doit or dumpit callback must be specified or the
273 * operation will fail. Only one operation structure per command
274 * identifier may be registered.
276 * See include/net/genetlink.h for more documenation on the operations
277 * structure.
279 * Returns 0 on success or a negative error code.
281 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
283 int err = -EINVAL;
285 if (ops->dumpit == NULL && ops->doit == NULL)
286 goto errout;
288 if (genl_get_cmd(ops->cmd, family)) {
289 err = -EEXIST;
290 goto errout;
293 if (ops->dumpit)
294 ops->flags |= GENL_CMD_CAP_DUMP;
295 if (ops->doit)
296 ops->flags |= GENL_CMD_CAP_DO;
297 if (ops->policy)
298 ops->flags |= GENL_CMD_CAP_HASPOL;
300 genl_lock();
301 list_add_tail(&ops->ops_list, &family->ops_list);
302 genl_unlock();
304 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
305 err = 0;
306 errout:
307 return err;
311 * genl_unregister_ops - unregister generic netlink operations
312 * @family: generic netlink family
313 * @ops: operations to be unregistered
315 * Unregisters the specified operations and unassigns them from the
316 * specified family. The operation blocks until the current message
317 * processing has finished and doesn't start again until the
318 * unregister process has finished.
320 * Note: It is not necessary to unregister all operations before
321 * unregistering the family, unregistering the family will cause
322 * all assigned operations to be unregistered automatically.
324 * Returns 0 on success or a negative error code.
326 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
328 struct genl_ops *rc;
330 genl_lock();
331 list_for_each_entry(rc, &family->ops_list, ops_list) {
332 if (rc == ops) {
333 list_del(&ops->ops_list);
334 genl_unlock();
335 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
336 return 0;
339 genl_unlock();
341 return -ENOENT;
345 * genl_register_family - register a generic netlink family
346 * @family: generic netlink family
348 * Registers the specified family after validating it first. Only one
349 * family may be registered with the same family name or identifier.
350 * The family id may equal GENL_ID_GENERATE causing an unique id to
351 * be automatically generated and assigned.
353 * Return 0 on success or a negative error code.
355 int genl_register_family(struct genl_family *family)
357 int err = -EINVAL;
359 if (family->id && family->id < GENL_MIN_ID)
360 goto errout;
362 if (family->id > GENL_MAX_ID)
363 goto errout;
365 INIT_LIST_HEAD(&family->ops_list);
366 INIT_LIST_HEAD(&family->mcast_groups);
368 genl_lock();
370 if (genl_family_find_byname(family->name)) {
371 err = -EEXIST;
372 goto errout_locked;
375 if (genl_family_find_byid(family->id)) {
376 err = -EEXIST;
377 goto errout_locked;
380 if (family->id == GENL_ID_GENERATE) {
381 u16 newid = genl_generate_id();
383 if (!newid) {
384 err = -ENOMEM;
385 goto errout_locked;
388 family->id = newid;
391 if (family->maxattr) {
392 family->attrbuf = kmalloc((family->maxattr+1) *
393 sizeof(struct nlattr *), GFP_KERNEL);
394 if (family->attrbuf == NULL) {
395 err = -ENOMEM;
396 goto errout_locked;
398 } else
399 family->attrbuf = NULL;
401 list_add_tail(&family->family_list, genl_family_chain(family->id));
402 genl_unlock();
404 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
406 return 0;
408 errout_locked:
409 genl_unlock();
410 errout:
411 return err;
415 * genl_register_family_with_ops - register a generic netlink family
416 * @family: generic netlink family
417 * @ops: operations to be registered
418 * @n_ops: number of elements to register
420 * Registers the specified family and operations from the specified table.
421 * Only one family may be registered with the same family name or identifier.
423 * The family id may equal GENL_ID_GENERATE causing an unique id to
424 * be automatically generated and assigned.
426 * Either a doit or dumpit callback must be specified for every registered
427 * operation or the function will fail. Only one operation structure per
428 * command identifier may be registered.
430 * See include/net/genetlink.h for more documenation on the operations
431 * structure.
433 * This is equivalent to calling genl_register_family() followed by
434 * genl_register_ops() for every operation entry in the table taking
435 * care to unregister the family on error path.
437 * Return 0 on success or a negative error code.
439 int genl_register_family_with_ops(struct genl_family *family,
440 struct genl_ops *ops, size_t n_ops)
442 int err, i;
444 err = genl_register_family(family);
445 if (err)
446 return err;
448 for (i = 0; i < n_ops; ++i, ++ops) {
449 err = genl_register_ops(family, ops);
450 if (err)
451 goto err_out;
453 return 0;
454 err_out:
455 genl_unregister_family(family);
456 return err;
458 EXPORT_SYMBOL(genl_register_family_with_ops);
461 * genl_unregister_family - unregister generic netlink family
462 * @family: generic netlink family
464 * Unregisters the specified family.
466 * Returns 0 on success or a negative error code.
468 int genl_unregister_family(struct genl_family *family)
470 struct genl_family *rc;
472 genl_lock();
474 genl_unregister_mc_groups(family);
476 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
477 if (family->id != rc->id || strcmp(rc->name, family->name))
478 continue;
480 list_del(&rc->family_list);
481 INIT_LIST_HEAD(&family->ops_list);
482 genl_unlock();
484 kfree(family->attrbuf);
485 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
486 return 0;
489 genl_unlock();
491 return -ENOENT;
494 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
496 struct genl_ops *ops;
497 struct genl_family *family;
498 struct net *net = sock_net(skb->sk);
499 struct genl_info info;
500 struct genlmsghdr *hdr = nlmsg_data(nlh);
501 int hdrlen, err;
503 family = genl_family_find_byid(nlh->nlmsg_type);
504 if (family == NULL)
505 return -ENOENT;
507 /* this family doesn't exist in this netns */
508 if (!family->netnsok && !net_eq(net, &init_net))
509 return -ENOENT;
511 hdrlen = GENL_HDRLEN + family->hdrsize;
512 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
513 return -EINVAL;
515 ops = genl_get_cmd(hdr->cmd, family);
516 if (ops == NULL)
517 return -EOPNOTSUPP;
519 if ((ops->flags & GENL_ADMIN_PERM) &&
520 security_netlink_recv(skb, CAP_NET_ADMIN))
521 return -EPERM;
523 if (nlh->nlmsg_flags & NLM_F_DUMP) {
524 if (ops->dumpit == NULL)
525 return -EOPNOTSUPP;
527 genl_unlock();
528 err = netlink_dump_start(net->genl_sock, skb, nlh,
529 ops->dumpit, ops->done);
530 genl_lock();
531 return err;
534 if (ops->doit == NULL)
535 return -EOPNOTSUPP;
537 if (family->attrbuf) {
538 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
539 ops->policy);
540 if (err < 0)
541 return err;
544 info.snd_seq = nlh->nlmsg_seq;
545 info.snd_pid = NETLINK_CB(skb).pid;
546 info.nlhdr = nlh;
547 info.genlhdr = nlmsg_data(nlh);
548 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
549 info.attrs = family->attrbuf;
550 genl_info_net_set(&info, net);
552 return ops->doit(skb, &info);
555 static void genl_rcv(struct sk_buff *skb)
557 genl_lock();
558 netlink_rcv_skb(skb, &genl_rcv_msg);
559 genl_unlock();
562 /**************************************************************************
563 * Controller
564 **************************************************************************/
566 static struct genl_family genl_ctrl = {
567 .id = GENL_ID_CTRL,
568 .name = "nlctrl",
569 .version = 0x2,
570 .maxattr = CTRL_ATTR_MAX,
571 .netnsok = true,
574 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
575 u32 flags, struct sk_buff *skb, u8 cmd)
577 void *hdr;
579 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
580 if (hdr == NULL)
581 return -1;
583 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
584 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
585 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
586 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
587 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
589 if (!list_empty(&family->ops_list)) {
590 struct nlattr *nla_ops;
591 struct genl_ops *ops;
592 int idx = 1;
594 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
595 if (nla_ops == NULL)
596 goto nla_put_failure;
598 list_for_each_entry(ops, &family->ops_list, ops_list) {
599 struct nlattr *nest;
601 nest = nla_nest_start(skb, idx++);
602 if (nest == NULL)
603 goto nla_put_failure;
605 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
606 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
608 nla_nest_end(skb, nest);
611 nla_nest_end(skb, nla_ops);
614 if (!list_empty(&family->mcast_groups)) {
615 struct genl_multicast_group *grp;
616 struct nlattr *nla_grps;
617 int idx = 1;
619 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
620 if (nla_grps == NULL)
621 goto nla_put_failure;
623 list_for_each_entry(grp, &family->mcast_groups, list) {
624 struct nlattr *nest;
626 nest = nla_nest_start(skb, idx++);
627 if (nest == NULL)
628 goto nla_put_failure;
630 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
631 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
632 grp->name);
634 nla_nest_end(skb, nest);
636 nla_nest_end(skb, nla_grps);
639 return genlmsg_end(skb, hdr);
641 nla_put_failure:
642 genlmsg_cancel(skb, hdr);
643 return -EMSGSIZE;
646 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
647 u32 seq, u32 flags, struct sk_buff *skb,
648 u8 cmd)
650 void *hdr;
651 struct nlattr *nla_grps;
652 struct nlattr *nest;
654 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
655 if (hdr == NULL)
656 return -1;
658 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
659 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
661 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
662 if (nla_grps == NULL)
663 goto nla_put_failure;
665 nest = nla_nest_start(skb, 1);
666 if (nest == NULL)
667 goto nla_put_failure;
669 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
670 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
671 grp->name);
673 nla_nest_end(skb, nest);
674 nla_nest_end(skb, nla_grps);
676 return genlmsg_end(skb, hdr);
678 nla_put_failure:
679 genlmsg_cancel(skb, hdr);
680 return -EMSGSIZE;
683 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
686 int i, n = 0;
687 struct genl_family *rt;
688 struct net *net = sock_net(skb->sk);
689 int chains_to_skip = cb->args[0];
690 int fams_to_skip = cb->args[1];
692 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
693 if (i < chains_to_skip)
694 continue;
695 n = 0;
696 list_for_each_entry(rt, genl_family_chain(i), family_list) {
697 if (!rt->netnsok && !net_eq(net, &init_net))
698 continue;
699 if (++n < fams_to_skip)
700 continue;
701 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
702 cb->nlh->nlmsg_seq, NLM_F_MULTI,
703 skb, CTRL_CMD_NEWFAMILY) < 0)
704 goto errout;
707 fams_to_skip = 0;
710 errout:
711 cb->args[0] = i;
712 cb->args[1] = n;
714 return skb->len;
717 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
718 u32 pid, int seq, u8 cmd)
720 struct sk_buff *skb;
721 int err;
723 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
724 if (skb == NULL)
725 return ERR_PTR(-ENOBUFS);
727 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
728 if (err < 0) {
729 nlmsg_free(skb);
730 return ERR_PTR(err);
733 return skb;
736 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
737 u32 pid, int seq, u8 cmd)
739 struct sk_buff *skb;
740 int err;
742 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
743 if (skb == NULL)
744 return ERR_PTR(-ENOBUFS);
746 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
747 if (err < 0) {
748 nlmsg_free(skb);
749 return ERR_PTR(err);
752 return skb;
755 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
756 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
757 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
758 .len = GENL_NAMSIZ - 1 },
761 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
763 struct sk_buff *msg;
764 struct genl_family *res = NULL;
765 int err = -EINVAL;
767 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
768 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
769 res = genl_family_find_byid(id);
770 err = -ENOENT;
773 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
774 char *name;
776 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
777 res = genl_family_find_byname(name);
778 err = -ENOENT;
781 if (res == NULL)
782 return err;
784 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
785 /* family doesn't exist here */
786 return -ENOENT;
789 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
790 CTRL_CMD_NEWFAMILY);
791 if (IS_ERR(msg))
792 return PTR_ERR(msg);
794 return genlmsg_reply(msg, info);
797 static int genl_ctrl_event(int event, void *data)
799 struct sk_buff *msg;
800 struct genl_family *family;
801 struct genl_multicast_group *grp;
803 /* genl is still initialising */
804 if (!init_net.genl_sock)
805 return 0;
807 switch (event) {
808 case CTRL_CMD_NEWFAMILY:
809 case CTRL_CMD_DELFAMILY:
810 family = data;
811 msg = ctrl_build_family_msg(family, 0, 0, event);
812 break;
813 case CTRL_CMD_NEWMCAST_GRP:
814 case CTRL_CMD_DELMCAST_GRP:
815 grp = data;
816 family = grp->family;
817 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
818 break;
819 default:
820 return -EINVAL;
823 if (IS_ERR(msg))
824 return PTR_ERR(msg);
826 if (!family->netnsok) {
827 genlmsg_multicast_netns(&init_net, msg, 0,
828 GENL_ID_CTRL, GFP_KERNEL);
829 } else {
830 rcu_read_lock();
831 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
832 rcu_read_unlock();
835 return 0;
838 static struct genl_ops genl_ctrl_ops = {
839 .cmd = CTRL_CMD_GETFAMILY,
840 .doit = ctrl_getfamily,
841 .dumpit = ctrl_dumpfamily,
842 .policy = ctrl_policy,
845 static struct genl_multicast_group notify_grp = {
846 .name = "notify",
849 static int __net_init genl_pernet_init(struct net *net)
851 /* we'll bump the group number right afterwards */
852 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
853 genl_rcv, &genl_mutex,
854 THIS_MODULE);
856 if (!net->genl_sock && net_eq(net, &init_net))
857 panic("GENL: Cannot initialize generic netlink\n");
859 if (!net->genl_sock)
860 return -ENOMEM;
862 return 0;
865 static void __net_exit genl_pernet_exit(struct net *net)
867 netlink_kernel_release(net->genl_sock);
868 net->genl_sock = NULL;
871 static struct pernet_operations genl_pernet_ops = {
872 .init = genl_pernet_init,
873 .exit = genl_pernet_exit,
876 static int __init genl_init(void)
878 int i, err;
880 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
881 INIT_LIST_HEAD(&family_ht[i]);
883 err = genl_register_family(&genl_ctrl);
884 if (err < 0)
885 goto problem;
887 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
888 if (err < 0)
889 goto problem;
891 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
893 err = register_pernet_subsys(&genl_pernet_ops);
894 if (err)
895 goto problem;
897 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
898 if (err < 0)
899 goto problem;
901 return 0;
903 problem:
904 panic("GENL: Cannot register controller: %d\n", err);
907 subsys_initcall(genl_init);
909 EXPORT_SYMBOL(genl_register_ops);
910 EXPORT_SYMBOL(genl_unregister_ops);
911 EXPORT_SYMBOL(genl_register_family);
912 EXPORT_SYMBOL(genl_unregister_family);
914 static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
915 gfp_t flags)
917 struct sk_buff *tmp;
918 struct net *net, *prev = NULL;
919 int err;
921 for_each_net_rcu(net) {
922 if (prev) {
923 tmp = skb_clone(skb, flags);
924 if (!tmp) {
925 err = -ENOMEM;
926 goto error;
928 err = nlmsg_multicast(prev->genl_sock, tmp,
929 pid, group, flags);
930 if (err)
931 goto error;
934 prev = net;
937 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
938 error:
939 kfree_skb(skb);
940 return err;
943 int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
944 gfp_t flags)
946 return genlmsg_mcast(skb, pid, group, flags);
948 EXPORT_SYMBOL(genlmsg_multicast_allns);