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>
19 #include <linux/rwsem.h>
21 #include <net/genetlink.h>
23 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
24 static DECLARE_RWSEM(cb_lock);
28 mutex_lock(&genl_mutex);
30 EXPORT_SYMBOL(genl_lock);
32 void genl_unlock(void)
34 mutex_unlock(&genl_mutex);
36 EXPORT_SYMBOL(genl_unlock);
39 int lockdep_genl_is_held(void)
41 return lockdep_is_held(&genl_mutex);
43 EXPORT_SYMBOL(lockdep_genl_is_held);
46 static void genl_lock_all(void)
52 static void genl_unlock_all(void)
58 #define GENL_FAM_TAB_SIZE 16
59 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
61 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
63 * Bitmap of multicast groups that are currently in use.
65 * To avoid an allocation at boot of just one unsigned long,
66 * declare it global instead.
67 * Bit 0 is marked as already used since group 0 is invalid.
68 * Bit 1 is marked as already used since the drop-monitor code
69 * abuses the API and thinks it can statically use group 1.
70 * That group will typically conflict with other groups that
71 * any proper users use.
72 * Bit 17 is marked as already used since the VFS quota code
73 * also abused this API and relied on family == group ID, we
74 * cater to that by giving it a static family and group ID.
76 static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_VFS_DQUOT);
77 static unsigned long *mc_groups = &mc_group_start;
78 static unsigned long mc_groups_longs = 1;
80 static int genl_ctrl_event(int event, struct genl_family *family,
81 struct genl_multicast_group *grp);
83 static inline unsigned int genl_family_hash(unsigned int id)
85 return id & GENL_FAM_TAB_MASK;
88 static inline struct list_head *genl_family_chain(unsigned int id)
90 return &family_ht[genl_family_hash(id)];
93 static struct genl_family *genl_family_find_byid(unsigned int id)
95 struct genl_family *f;
97 list_for_each_entry(f, genl_family_chain(id), family_list)
104 static struct genl_family *genl_family_find_byname(char *name)
106 struct genl_family *f;
109 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
110 list_for_each_entry(f, genl_family_chain(i), family_list)
111 if (strcmp(f->name, name) == 0)
117 static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
121 for (i = 0; i < family->n_ops; i++)
122 if (family->ops[i].cmd == cmd)
123 return &family->ops[i];
128 /* Of course we are going to have problems once we hit
129 * 2^16 alive types, but that can only happen by year 2K
131 static u16 genl_generate_id(void)
133 static u16 id_gen_idx = GENL_MIN_ID;
136 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
137 if (id_gen_idx != GENL_ID_VFS_DQUOT &&
138 !genl_family_find_byid(id_gen_idx))
140 if (++id_gen_idx > GENL_MAX_ID)
141 id_gen_idx = GENL_MIN_ID;
147 static struct genl_multicast_group notify_grp;
150 * genl_register_mc_group - register a multicast group
152 * Registers the specified multicast group and notifies userspace
153 * about the new group.
155 * Returns 0 on success or a negative error code.
157 * @family: The generic netlink family the group shall be registered for.
158 * @grp: The group to register, must have a name.
160 int genl_register_mc_group(struct genl_family *family,
161 struct genl_multicast_group *grp)
164 unsigned long *new_groups;
167 BUG_ON(grp->name[0] == '\0');
168 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
172 /* special-case our own group and hacks */
173 if (grp == ¬ify_grp)
175 else if (strcmp(family->name, "NET_DM") == 0)
177 else if (strcmp(family->name, "VFS_DQUOT") == 0)
178 id = GENL_ID_VFS_DQUOT;
180 id = find_first_zero_bit(mc_groups,
181 mc_groups_longs * BITS_PER_LONG);
184 if (id >= mc_groups_longs * BITS_PER_LONG) {
185 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
187 if (mc_groups == &mc_group_start) {
188 new_groups = kzalloc(nlen, GFP_KERNEL);
193 mc_groups = new_groups;
194 *mc_groups = mc_group_start;
196 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
201 mc_groups = new_groups;
202 mc_groups[mc_groups_longs] = 0;
207 if (family->netnsok) {
210 netlink_table_grab();
212 for_each_net_rcu(net) {
213 err = __netlink_change_ngroups(net->genl_sock,
214 mc_groups_longs * BITS_PER_LONG);
217 * No need to roll back, can only fail if
218 * memory allocation fails and then the
219 * number of _possible_ groups has been
220 * increased on some sockets which is ok.
223 netlink_table_ungrab();
228 netlink_table_ungrab();
230 err = netlink_change_ngroups(init_net.genl_sock,
231 mc_groups_longs * BITS_PER_LONG);
237 set_bit(id, mc_groups);
238 list_add_tail(&grp->list, &family->mcast_groups);
240 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family, grp);
245 EXPORT_SYMBOL(genl_register_mc_group);
247 static void __genl_unregister_mc_group(struct genl_family *family,
248 struct genl_multicast_group *grp)
252 netlink_table_grab();
254 for_each_net_rcu(net)
255 __netlink_clear_multicast_users(net->genl_sock, grp->id);
257 netlink_table_ungrab();
260 clear_bit(grp->id, mc_groups);
261 list_del(&grp->list);
262 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family, grp);
266 static void genl_unregister_mc_groups(struct genl_family *family)
268 struct genl_multicast_group *grp, *tmp;
270 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
271 __genl_unregister_mc_group(family, grp);
274 static int genl_validate_ops(struct genl_family *family)
276 const struct genl_ops *ops = family->ops;
277 unsigned int n_ops = family->n_ops;
280 if (WARN_ON(n_ops && !ops))
286 for (i = 0; i < n_ops; i++) {
287 if (ops[i].dumpit == NULL && ops[i].doit == NULL)
289 for (j = i + 1; j < n_ops; j++)
290 if (ops[i].cmd == ops[j].cmd)
294 /* family is not registered yet, so no locking needed */
296 family->n_ops = n_ops;
302 * __genl_register_family - register a generic netlink family
303 * @family: generic netlink family
305 * Registers the specified family after validating it first. Only one
306 * family may be registered with the same family name or identifier.
307 * The family id may equal GENL_ID_GENERATE causing an unique id to
308 * be automatically generated and assigned.
310 * The family's ops array must already be assigned, you can use the
311 * genl_register_family_with_ops() helper function.
313 * Return 0 on success or a negative error code.
315 int __genl_register_family(struct genl_family *family)
319 if (family->id && family->id < GENL_MIN_ID)
322 if (family->id > GENL_MAX_ID)
325 err = genl_validate_ops(family);
329 INIT_LIST_HEAD(&family->mcast_groups);
333 if (genl_family_find_byname(family->name)) {
338 if (family->id == GENL_ID_GENERATE) {
339 u16 newid = genl_generate_id();
347 } else if (genl_family_find_byid(family->id)) {
352 if (family->maxattr && !family->parallel_ops) {
353 family->attrbuf = kmalloc((family->maxattr+1) *
354 sizeof(struct nlattr *), GFP_KERNEL);
355 if (family->attrbuf == NULL) {
360 family->attrbuf = NULL;
362 list_add_tail(&family->family_list, genl_family_chain(family->id));
365 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL);
374 EXPORT_SYMBOL(__genl_register_family);
377 * genl_unregister_family - unregister generic netlink family
378 * @family: generic netlink family
380 * Unregisters the specified family.
382 * Returns 0 on success or a negative error code.
384 int genl_unregister_family(struct genl_family *family)
386 struct genl_family *rc;
390 genl_unregister_mc_groups(family);
392 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
393 if (family->id != rc->id || strcmp(rc->name, family->name))
396 list_del(&rc->family_list);
400 kfree(family->attrbuf);
401 genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL);
409 EXPORT_SYMBOL(genl_unregister_family);
412 * genlmsg_put - Add generic netlink header to netlink message
413 * @skb: socket buffer holding the message
414 * @portid: netlink portid the message is addressed to
415 * @seq: sequence number (usually the one of the sender)
416 * @family: generic netlink family
417 * @flags: netlink message flags
418 * @cmd: generic netlink command
420 * Returns pointer to user specific header
422 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
423 struct genl_family *family, int flags, u8 cmd)
425 struct nlmsghdr *nlh;
426 struct genlmsghdr *hdr;
428 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
429 family->hdrsize, flags);
433 hdr = nlmsg_data(nlh);
435 hdr->version = family->version;
438 return (char *) hdr + GENL_HDRLEN;
440 EXPORT_SYMBOL(genlmsg_put);
442 static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
444 /* our ops are always const - netlink API doesn't propagate that */
445 const struct genl_ops *ops = cb->data;
449 rc = ops->dumpit(skb, cb);
454 static int genl_lock_done(struct netlink_callback *cb)
456 /* our ops are always const - netlink API doesn't propagate that */
457 const struct genl_ops *ops = cb->data;
468 static int genl_family_rcv_msg(struct genl_family *family,
470 struct nlmsghdr *nlh)
472 const struct genl_ops *ops;
473 struct net *net = sock_net(skb->sk);
474 struct genl_info info;
475 struct genlmsghdr *hdr = nlmsg_data(nlh);
476 struct nlattr **attrbuf;
479 /* this family doesn't exist in this netns */
480 if (!family->netnsok && !net_eq(net, &init_net))
483 hdrlen = GENL_HDRLEN + family->hdrsize;
484 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
487 ops = genl_get_cmd(hdr->cmd, family);
491 if ((ops->flags & GENL_ADMIN_PERM) &&
492 !capable(CAP_NET_ADMIN))
495 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
498 if (ops->dumpit == NULL)
501 if (!family->parallel_ops) {
502 struct netlink_dump_control c = {
503 .module = family->module,
504 /* we have const, but the netlink API doesn't */
506 .dump = genl_lock_dumpit,
507 .done = genl_lock_done,
511 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
515 struct netlink_dump_control c = {
516 .module = family->module,
521 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
527 if (ops->doit == NULL)
530 if (family->maxattr && family->parallel_ops) {
531 attrbuf = kmalloc((family->maxattr+1) *
532 sizeof(struct nlattr *), GFP_KERNEL);
536 attrbuf = family->attrbuf;
539 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
545 info.snd_seq = nlh->nlmsg_seq;
546 info.snd_portid = NETLINK_CB(skb).portid;
548 info.genlhdr = nlmsg_data(nlh);
549 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
550 info.attrs = attrbuf;
551 genl_info_net_set(&info, net);
552 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
554 if (family->pre_doit) {
555 err = family->pre_doit(ops, skb, &info);
560 err = ops->doit(skb, &info);
562 if (family->post_doit)
563 family->post_doit(ops, skb, &info);
566 if (family->parallel_ops)
572 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
574 struct genl_family *family;
577 family = genl_family_find_byid(nlh->nlmsg_type);
581 if (!family->parallel_ops)
584 err = genl_family_rcv_msg(family, skb, nlh);
586 if (!family->parallel_ops)
592 static void genl_rcv(struct sk_buff *skb)
595 netlink_rcv_skb(skb, &genl_rcv_msg);
599 /**************************************************************************
601 **************************************************************************/
603 static struct genl_family genl_ctrl = {
607 .maxattr = CTRL_ATTR_MAX,
611 static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
612 u32 flags, struct sk_buff *skb, u8 cmd)
616 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
620 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
621 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
622 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
623 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
624 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
625 goto nla_put_failure;
628 struct nlattr *nla_ops;
631 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
633 goto nla_put_failure;
635 for (i = 0; i < family->n_ops; i++) {
637 const struct genl_ops *ops = &family->ops[i];
638 u32 op_flags = ops->flags;
641 op_flags |= GENL_CMD_CAP_DUMP;
643 op_flags |= GENL_CMD_CAP_DO;
645 op_flags |= GENL_CMD_CAP_HASPOL;
647 nest = nla_nest_start(skb, i + 1);
649 goto nla_put_failure;
651 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
652 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
653 goto nla_put_failure;
655 nla_nest_end(skb, nest);
658 nla_nest_end(skb, nla_ops);
661 if (!list_empty(&family->mcast_groups)) {
662 struct genl_multicast_group *grp;
663 struct nlattr *nla_grps;
666 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
667 if (nla_grps == NULL)
668 goto nla_put_failure;
670 list_for_each_entry(grp, &family->mcast_groups, list) {
673 nest = nla_nest_start(skb, idx++);
675 goto nla_put_failure;
677 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
678 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
680 goto nla_put_failure;
682 nla_nest_end(skb, nest);
684 nla_nest_end(skb, nla_grps);
687 return genlmsg_end(skb, hdr);
690 genlmsg_cancel(skb, hdr);
694 static int ctrl_fill_mcgrp_info(struct genl_family *family,
695 struct genl_multicast_group *grp, u32 portid,
696 u32 seq, u32 flags, struct sk_buff *skb,
700 struct nlattr *nla_grps;
703 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
707 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
708 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
709 goto nla_put_failure;
711 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
712 if (nla_grps == NULL)
713 goto nla_put_failure;
715 nest = nla_nest_start(skb, 1);
717 goto nla_put_failure;
719 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
720 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
722 goto nla_put_failure;
724 nla_nest_end(skb, nest);
725 nla_nest_end(skb, nla_grps);
727 return genlmsg_end(skb, hdr);
730 genlmsg_cancel(skb, hdr);
734 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
738 struct genl_family *rt;
739 struct net *net = sock_net(skb->sk);
740 int chains_to_skip = cb->args[0];
741 int fams_to_skip = cb->args[1];
743 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
745 list_for_each_entry(rt, genl_family_chain(i), family_list) {
746 if (!rt->netnsok && !net_eq(net, &init_net))
748 if (++n < fams_to_skip)
750 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
751 cb->nlh->nlmsg_seq, NLM_F_MULTI,
752 skb, CTRL_CMD_NEWFAMILY) < 0)
766 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
767 u32 portid, int seq, u8 cmd)
772 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
774 return ERR_PTR(-ENOBUFS);
776 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
785 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_family *family,
786 struct genl_multicast_group *grp,
787 u32 portid, int seq, u8 cmd)
792 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
794 return ERR_PTR(-ENOBUFS);
796 err = ctrl_fill_mcgrp_info(family, grp, portid, seq, 0, skb, cmd);
805 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
806 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
807 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
808 .len = GENL_NAMSIZ - 1 },
811 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
814 struct genl_family *res = NULL;
817 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
818 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
819 res = genl_family_find_byid(id);
823 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
826 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
827 res = genl_family_find_byname(name);
828 #ifdef CONFIG_MODULES
832 request_module("net-pf-%d-proto-%d-family-%s",
833 PF_NETLINK, NETLINK_GENERIC, name);
836 res = genl_family_find_byname(name);
845 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
846 /* family doesn't exist here */
850 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
855 return genlmsg_reply(msg, info);
858 static int genl_ctrl_event(int event, struct genl_family *family,
859 struct genl_multicast_group *grp)
863 /* genl is still initialising */
864 if (!init_net.genl_sock)
868 case CTRL_CMD_NEWFAMILY:
869 case CTRL_CMD_DELFAMILY:
871 msg = ctrl_build_family_msg(family, 0, 0, event);
873 case CTRL_CMD_NEWMCAST_GRP:
874 case CTRL_CMD_DELMCAST_GRP:
876 msg = ctrl_build_mcgrp_msg(family, grp, 0, 0, event);
885 if (!family->netnsok) {
886 genlmsg_multicast_netns(&init_net, msg, 0,
887 GENL_ID_CTRL, GFP_KERNEL);
890 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
897 static struct genl_ops genl_ctrl_ops[] = {
899 .cmd = CTRL_CMD_GETFAMILY,
900 .doit = ctrl_getfamily,
901 .dumpit = ctrl_dumpfamily,
902 .policy = ctrl_policy,
906 static struct genl_multicast_group notify_grp = {
910 static int __net_init genl_pernet_init(struct net *net)
912 struct netlink_kernel_cfg cfg = {
914 .flags = NL_CFG_F_NONROOT_RECV,
917 /* we'll bump the group number right afterwards */
918 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
920 if (!net->genl_sock && net_eq(net, &init_net))
921 panic("GENL: Cannot initialize generic netlink\n");
929 static void __net_exit genl_pernet_exit(struct net *net)
931 netlink_kernel_release(net->genl_sock);
932 net->genl_sock = NULL;
935 static struct pernet_operations genl_pernet_ops = {
936 .init = genl_pernet_init,
937 .exit = genl_pernet_exit,
940 static int __init genl_init(void)
944 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
945 INIT_LIST_HEAD(&family_ht[i]);
947 err = genl_register_family_with_ops(&genl_ctrl, genl_ctrl_ops);
951 err = register_pernet_subsys(&genl_pernet_ops);
955 err = genl_register_mc_group(&genl_ctrl, ¬ify_grp);
962 panic("GENL: Cannot register controller: %d\n", err);
965 subsys_initcall(genl_init);
967 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
971 struct net *net, *prev = NULL;
974 for_each_net_rcu(net) {
976 tmp = skb_clone(skb, flags);
981 err = nlmsg_multicast(prev->genl_sock, tmp,
982 portid, group, flags);
990 return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
996 int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
999 return genlmsg_mcast(skb, portid, group, flags);
1001 EXPORT_SYMBOL(genlmsg_multicast_allns);
1003 void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
1004 struct nlmsghdr *nlh, gfp_t flags)
1006 struct sock *sk = net->genl_sock;
1010 report = nlmsg_report(nlh);
1012 nlmsg_notify(sk, skb, portid, group, report, flags);
1014 EXPORT_SYMBOL(genl_notify);