2 * NETLINK Generic Netlink Family
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
6 * Johannes Berg <johannes@sipsolutions.net>
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>
20 #include <net/genetlink.h>
22 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
26 mutex_lock(&genl_mutex);
28 EXPORT_SYMBOL(genl_lock);
30 void genl_unlock(void)
32 mutex_unlock(&genl_mutex);
34 EXPORT_SYMBOL(genl_unlock);
36 #define GENL_FAM_TAB_SIZE 16
37 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
39 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
41 * Bitmap of multicast groups that are currently in use.
43 * To avoid an allocation at boot of just one unsigned long,
44 * declare it global instead.
45 * Bit 0 is marked as already used since group 0 is invalid.
47 static unsigned long mc_group_start = 0x1;
48 static unsigned long *mc_groups = &mc_group_start;
49 static unsigned long mc_groups_longs = 1;
51 static int genl_ctrl_event(int event, void *data);
53 static inline unsigned int genl_family_hash(unsigned int id)
55 return id & GENL_FAM_TAB_MASK;
58 static inline struct list_head *genl_family_chain(unsigned int id)
60 return &family_ht[genl_family_hash(id)];
63 static struct genl_family *genl_family_find_byid(unsigned int id)
65 struct genl_family *f;
67 list_for_each_entry(f, genl_family_chain(id), family_list)
74 static struct genl_family *genl_family_find_byname(char *name)
76 struct genl_family *f;
79 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
80 list_for_each_entry(f, genl_family_chain(i), family_list)
81 if (strcmp(f->name, name) == 0)
87 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
91 list_for_each_entry(ops, &family->ops_list, ops_list)
98 /* Of course we are going to have problems once we hit
99 * 2^16 alive types, but that can only happen by year 2K
101 static inline u16 genl_generate_id(void)
103 static u16 id_gen_idx = GENL_MIN_ID;
106 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
107 if (!genl_family_find_byid(id_gen_idx))
109 if (++id_gen_idx > GENL_MAX_ID)
110 id_gen_idx = GENL_MIN_ID;
116 static struct genl_multicast_group notify_grp;
119 * genl_register_mc_group - register a multicast group
121 * Registers the specified multicast group and notifies userspace
122 * about the new group.
124 * Returns 0 on success or a negative error code.
126 * @family: The generic netlink family the group shall be registered for.
127 * @grp: The group to register, must have a name.
129 int genl_register_mc_group(struct genl_family *family,
130 struct genl_multicast_group *grp)
133 unsigned long *new_groups;
136 BUG_ON(grp->name[0] == '\0');
137 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
141 /* special-case our own group */
142 if (grp == ¬ify_grp)
145 id = find_first_zero_bit(mc_groups,
146 mc_groups_longs * BITS_PER_LONG);
149 if (id >= mc_groups_longs * BITS_PER_LONG) {
150 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
152 if (mc_groups == &mc_group_start) {
153 new_groups = kzalloc(nlen, GFP_KERNEL);
158 mc_groups = new_groups;
159 *mc_groups = mc_group_start;
161 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
166 mc_groups = new_groups;
167 mc_groups[mc_groups_longs] = 0;
172 if (family->netnsok) {
175 netlink_table_grab();
177 for_each_net_rcu(net) {
178 err = __netlink_change_ngroups(net->genl_sock,
179 mc_groups_longs * BITS_PER_LONG);
182 * No need to roll back, can only fail if
183 * memory allocation fails and then the
184 * number of _possible_ groups has been
185 * increased on some sockets which is ok.
188 netlink_table_ungrab();
193 netlink_table_ungrab();
195 err = netlink_change_ngroups(init_net.genl_sock,
196 mc_groups_longs * BITS_PER_LONG);
202 set_bit(id, mc_groups);
203 list_add_tail(&grp->list, &family->mcast_groups);
204 grp->family = family;
206 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
211 EXPORT_SYMBOL(genl_register_mc_group);
213 static void __genl_unregister_mc_group(struct genl_family *family,
214 struct genl_multicast_group *grp)
217 BUG_ON(grp->family != family);
219 netlink_table_grab();
221 for_each_net_rcu(net)
222 __netlink_clear_multicast_users(net->genl_sock, grp->id);
224 netlink_table_ungrab();
226 clear_bit(grp->id, mc_groups);
227 list_del(&grp->list);
228 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
234 * genl_unregister_mc_group - unregister a multicast group
236 * Unregisters the specified multicast group and notifies userspace
237 * about it. All current listeners on the group are removed.
239 * Note: It is not necessary to unregister all multicast groups before
240 * unregistering the family, unregistering the family will cause
241 * all assigned multicast groups to be unregistered automatically.
243 * @family: Generic netlink family the group belongs to.
244 * @grp: The group to unregister, must have been registered successfully
247 void genl_unregister_mc_group(struct genl_family *family,
248 struct genl_multicast_group *grp)
251 __genl_unregister_mc_group(family, grp);
254 EXPORT_SYMBOL(genl_unregister_mc_group);
256 static void genl_unregister_mc_groups(struct genl_family *family)
258 struct genl_multicast_group *grp, *tmp;
260 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
261 __genl_unregister_mc_group(family, grp);
265 * genl_register_ops - register generic netlink operations
266 * @family: generic netlink family
267 * @ops: operations to be registered
269 * Registers the specified operations and assigns them to the specified
270 * family. Either a doit or dumpit callback must be specified or the
271 * operation will fail. Only one operation structure per command
272 * identifier may be registered.
274 * See include/net/genetlink.h for more documenation on the operations
277 * Returns 0 on success or a negative error code.
279 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
283 if (ops->dumpit == NULL && ops->doit == NULL)
286 if (genl_get_cmd(ops->cmd, family)) {
292 ops->flags |= GENL_CMD_CAP_DUMP;
294 ops->flags |= GENL_CMD_CAP_DO;
296 ops->flags |= GENL_CMD_CAP_HASPOL;
299 list_add_tail(&ops->ops_list, &family->ops_list);
302 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
307 EXPORT_SYMBOL(genl_register_ops);
310 * genl_unregister_ops - unregister generic netlink operations
311 * @family: generic netlink family
312 * @ops: operations to be unregistered
314 * Unregisters the specified operations and unassigns them from the
315 * specified family. The operation blocks until the current message
316 * processing has finished and doesn't start again until the
317 * unregister process has finished.
319 * Note: It is not necessary to unregister all operations before
320 * unregistering the family, unregistering the family will cause
321 * all assigned operations to be unregistered automatically.
323 * Returns 0 on success or a negative error code.
325 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
330 list_for_each_entry(rc, &family->ops_list, ops_list) {
332 list_del(&ops->ops_list);
334 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
342 EXPORT_SYMBOL(genl_unregister_ops);
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)
359 if (family->id && family->id < GENL_MIN_ID)
362 if (family->id > GENL_MAX_ID)
365 INIT_LIST_HEAD(&family->ops_list);
366 INIT_LIST_HEAD(&family->mcast_groups);
370 if (genl_family_find_byname(family->name)) {
375 if (family->id == GENL_ID_GENERATE) {
376 u16 newid = genl_generate_id();
384 } else if (genl_family_find_byid(family->id)) {
389 if (family->maxattr) {
390 family->attrbuf = kmalloc((family->maxattr+1) *
391 sizeof(struct nlattr *), GFP_KERNEL);
392 if (family->attrbuf == NULL) {
397 family->attrbuf = NULL;
399 list_add_tail(&family->family_list, genl_family_chain(family->id));
402 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
411 EXPORT_SYMBOL(genl_register_family);
414 * genl_register_family_with_ops - register a generic netlink family
415 * @family: generic netlink family
416 * @ops: operations to be registered
417 * @n_ops: number of elements to register
419 * Registers the specified family and operations from the specified table.
420 * Only one family may be registered with the same family name or identifier.
422 * The family id may equal GENL_ID_GENERATE causing an unique id to
423 * be automatically generated and assigned.
425 * Either a doit or dumpit callback must be specified for every registered
426 * operation or the function will fail. Only one operation structure per
427 * command identifier may be registered.
429 * See include/net/genetlink.h for more documenation on the operations
432 * This is equivalent to calling genl_register_family() followed by
433 * genl_register_ops() for every operation entry in the table taking
434 * care to unregister the family on error path.
436 * Return 0 on success or a negative error code.
438 int genl_register_family_with_ops(struct genl_family *family,
439 struct genl_ops *ops, size_t n_ops)
443 err = genl_register_family(family);
447 for (i = 0; i < n_ops; ++i, ++ops) {
448 err = genl_register_ops(family, ops);
454 genl_unregister_family(family);
457 EXPORT_SYMBOL(genl_register_family_with_ops);
460 * genl_unregister_family - unregister generic netlink family
461 * @family: generic netlink family
463 * Unregisters the specified family.
465 * Returns 0 on success or a negative error code.
467 int genl_unregister_family(struct genl_family *family)
469 struct genl_family *rc;
473 genl_unregister_mc_groups(family);
475 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
476 if (family->id != rc->id || strcmp(rc->name, family->name))
479 list_del(&rc->family_list);
480 INIT_LIST_HEAD(&family->ops_list);
483 kfree(family->attrbuf);
484 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
492 EXPORT_SYMBOL(genl_unregister_family);
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);
503 family = genl_family_find_byid(nlh->nlmsg_type);
507 /* this family doesn't exist in this netns */
508 if (!family->netnsok && !net_eq(net, &init_net))
511 hdrlen = GENL_HDRLEN + family->hdrsize;
512 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
515 ops = genl_get_cmd(hdr->cmd, family);
519 if ((ops->flags & GENL_ADMIN_PERM) &&
520 security_netlink_recv(skb, CAP_NET_ADMIN))
523 if (nlh->nlmsg_flags & NLM_F_DUMP) {
524 if (ops->dumpit == NULL)
528 err = netlink_dump_start(net->genl_sock, skb, nlh,
529 ops->dumpit, ops->done, 0);
534 if (ops->doit == NULL)
537 if (family->attrbuf) {
538 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
544 info.snd_seq = nlh->nlmsg_seq;
545 info.snd_pid = NETLINK_CB(skb).pid;
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);
551 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
553 if (family->pre_doit) {
554 err = family->pre_doit(ops, skb, &info);
559 err = ops->doit(skb, &info);
561 if (family->post_doit)
562 family->post_doit(ops, skb, &info);
567 static void genl_rcv(struct sk_buff *skb)
570 netlink_rcv_skb(skb, &genl_rcv_msg);
574 /**************************************************************************
576 **************************************************************************/
578 static struct genl_family genl_ctrl = {
582 .maxattr = CTRL_ATTR_MAX,
586 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
587 u32 flags, struct sk_buff *skb, u8 cmd)
591 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
595 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
596 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
597 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
598 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
599 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
601 if (!list_empty(&family->ops_list)) {
602 struct nlattr *nla_ops;
603 struct genl_ops *ops;
606 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
608 goto nla_put_failure;
610 list_for_each_entry(ops, &family->ops_list, ops_list) {
613 nest = nla_nest_start(skb, idx++);
615 goto nla_put_failure;
617 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
618 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
620 nla_nest_end(skb, nest);
623 nla_nest_end(skb, nla_ops);
626 if (!list_empty(&family->mcast_groups)) {
627 struct genl_multicast_group *grp;
628 struct nlattr *nla_grps;
631 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
632 if (nla_grps == NULL)
633 goto nla_put_failure;
635 list_for_each_entry(grp, &family->mcast_groups, list) {
638 nest = nla_nest_start(skb, idx++);
640 goto nla_put_failure;
642 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
643 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
646 nla_nest_end(skb, nest);
648 nla_nest_end(skb, nla_grps);
651 return genlmsg_end(skb, hdr);
654 genlmsg_cancel(skb, hdr);
658 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
659 u32 seq, u32 flags, struct sk_buff *skb,
663 struct nlattr *nla_grps;
666 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
670 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
671 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
673 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
674 if (nla_grps == NULL)
675 goto nla_put_failure;
677 nest = nla_nest_start(skb, 1);
679 goto nla_put_failure;
681 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
682 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
685 nla_nest_end(skb, nest);
686 nla_nest_end(skb, nla_grps);
688 return genlmsg_end(skb, hdr);
691 genlmsg_cancel(skb, hdr);
695 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
699 struct genl_family *rt;
700 struct net *net = sock_net(skb->sk);
701 int chains_to_skip = cb->args[0];
702 int fams_to_skip = cb->args[1];
704 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
706 list_for_each_entry(rt, genl_family_chain(i), family_list) {
707 if (!rt->netnsok && !net_eq(net, &init_net))
709 if (++n < fams_to_skip)
711 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
712 cb->nlh->nlmsg_seq, NLM_F_MULTI,
713 skb, CTRL_CMD_NEWFAMILY) < 0)
727 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
728 u32 pid, int seq, u8 cmd)
733 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
735 return ERR_PTR(-ENOBUFS);
737 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
746 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
747 u32 pid, int seq, u8 cmd)
752 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
754 return ERR_PTR(-ENOBUFS);
756 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
765 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
766 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
767 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
768 .len = GENL_NAMSIZ - 1 },
771 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
774 struct genl_family *res = NULL;
777 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
778 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
779 res = genl_family_find_byid(id);
783 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
786 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
787 res = genl_family_find_byname(name);
794 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
795 /* family doesn't exist here */
799 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
804 return genlmsg_reply(msg, info);
807 static int genl_ctrl_event(int event, void *data)
810 struct genl_family *family;
811 struct genl_multicast_group *grp;
813 /* genl is still initialising */
814 if (!init_net.genl_sock)
818 case CTRL_CMD_NEWFAMILY:
819 case CTRL_CMD_DELFAMILY:
821 msg = ctrl_build_family_msg(family, 0, 0, event);
823 case CTRL_CMD_NEWMCAST_GRP:
824 case CTRL_CMD_DELMCAST_GRP:
826 family = grp->family;
827 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
836 if (!family->netnsok) {
837 genlmsg_multicast_netns(&init_net, msg, 0,
838 GENL_ID_CTRL, GFP_KERNEL);
841 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
848 static struct genl_ops genl_ctrl_ops = {
849 .cmd = CTRL_CMD_GETFAMILY,
850 .doit = ctrl_getfamily,
851 .dumpit = ctrl_dumpfamily,
852 .policy = ctrl_policy,
855 static struct genl_multicast_group notify_grp = {
859 static int __net_init genl_pernet_init(struct net *net)
861 /* we'll bump the group number right afterwards */
862 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
863 genl_rcv, &genl_mutex,
866 if (!net->genl_sock && net_eq(net, &init_net))
867 panic("GENL: Cannot initialize generic netlink\n");
875 static void __net_exit genl_pernet_exit(struct net *net)
877 netlink_kernel_release(net->genl_sock);
878 net->genl_sock = NULL;
881 static struct pernet_operations genl_pernet_ops = {
882 .init = genl_pernet_init,
883 .exit = genl_pernet_exit,
886 static int __init genl_init(void)
890 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
891 INIT_LIST_HEAD(&family_ht[i]);
893 err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
897 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
899 err = register_pernet_subsys(&genl_pernet_ops);
903 err = genl_register_mc_group(&genl_ctrl, ¬ify_grp);
910 panic("GENL: Cannot register controller: %d\n", err);
913 subsys_initcall(genl_init);
915 static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
919 struct net *net, *prev = NULL;
922 for_each_net_rcu(net) {
924 tmp = skb_clone(skb, flags);
929 err = nlmsg_multicast(prev->genl_sock, tmp,
938 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
944 int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
947 return genlmsg_mcast(skb, pid, group, flags);
949 EXPORT_SYMBOL(genlmsg_multicast_allns);