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.
73 static unsigned long mc_group_start = 0x3;
74 static unsigned long *mc_groups = &mc_group_start;
75 static unsigned long mc_groups_longs = 1;
77 static int genl_ctrl_event(int event, void *data);
79 static inline unsigned int genl_family_hash(unsigned int id)
81 return id & GENL_FAM_TAB_MASK;
84 static inline struct list_head *genl_family_chain(unsigned int id)
86 return &family_ht[genl_family_hash(id)];
89 static struct genl_family *genl_family_find_byid(unsigned int id)
91 struct genl_family *f;
93 list_for_each_entry(f, genl_family_chain(id), family_list)
100 static struct genl_family *genl_family_find_byname(char *name)
102 struct genl_family *f;
105 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
106 list_for_each_entry(f, genl_family_chain(i), family_list)
107 if (strcmp(f->name, name) == 0)
113 static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
117 for (i = 0; i < family->n_ops; i++)
118 if (family->ops[i].cmd == cmd)
119 return &family->ops[i];
124 /* Of course we are going to have problems once we hit
125 * 2^16 alive types, but that can only happen by year 2K
127 static u16 genl_generate_id(void)
129 static u16 id_gen_idx = GENL_MIN_ID;
132 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
133 if (!genl_family_find_byid(id_gen_idx))
135 if (++id_gen_idx > GENL_MAX_ID)
136 id_gen_idx = GENL_MIN_ID;
142 static struct genl_multicast_group notify_grp;
145 * genl_register_mc_group - register a multicast group
147 * Registers the specified multicast group and notifies userspace
148 * about the new group.
150 * Returns 0 on success or a negative error code.
152 * @family: The generic netlink family the group shall be registered for.
153 * @grp: The group to register, must have a name.
155 int genl_register_mc_group(struct genl_family *family,
156 struct genl_multicast_group *grp)
159 unsigned long *new_groups;
162 BUG_ON(grp->name[0] == '\0');
163 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
167 /* special-case our own group and hacks */
168 if (grp == ¬ify_grp)
170 else if (strcmp(family->name, "NET_DM") == 0)
173 id = find_first_zero_bit(mc_groups,
174 mc_groups_longs * BITS_PER_LONG);
177 if (id >= mc_groups_longs * BITS_PER_LONG) {
178 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
180 if (mc_groups == &mc_group_start) {
181 new_groups = kzalloc(nlen, GFP_KERNEL);
186 mc_groups = new_groups;
187 *mc_groups = mc_group_start;
189 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
194 mc_groups = new_groups;
195 mc_groups[mc_groups_longs] = 0;
200 if (family->netnsok) {
203 netlink_table_grab();
205 for_each_net_rcu(net) {
206 err = __netlink_change_ngroups(net->genl_sock,
207 mc_groups_longs * BITS_PER_LONG);
210 * No need to roll back, can only fail if
211 * memory allocation fails and then the
212 * number of _possible_ groups has been
213 * increased on some sockets which is ok.
216 netlink_table_ungrab();
221 netlink_table_ungrab();
223 err = netlink_change_ngroups(init_net.genl_sock,
224 mc_groups_longs * BITS_PER_LONG);
230 set_bit(id, mc_groups);
231 list_add_tail(&grp->list, &family->mcast_groups);
232 grp->family = family;
234 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
239 EXPORT_SYMBOL(genl_register_mc_group);
241 static void __genl_unregister_mc_group(struct genl_family *family,
242 struct genl_multicast_group *grp)
245 BUG_ON(grp->family != family);
247 netlink_table_grab();
249 for_each_net_rcu(net)
250 __netlink_clear_multicast_users(net->genl_sock, grp->id);
252 netlink_table_ungrab();
255 clear_bit(grp->id, mc_groups);
256 list_del(&grp->list);
257 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
263 * genl_unregister_mc_group - unregister a multicast group
265 * Unregisters the specified multicast group and notifies userspace
266 * about it. All current listeners on the group are removed.
268 * Note: It is not necessary to unregister all multicast groups before
269 * unregistering the family, unregistering the family will cause
270 * all assigned multicast groups to be unregistered automatically.
272 * @family: Generic netlink family the group belongs to.
273 * @grp: The group to unregister, must have been registered successfully
276 void genl_unregister_mc_group(struct genl_family *family,
277 struct genl_multicast_group *grp)
280 __genl_unregister_mc_group(family, grp);
283 EXPORT_SYMBOL(genl_unregister_mc_group);
285 static void genl_unregister_mc_groups(struct genl_family *family)
287 struct genl_multicast_group *grp, *tmp;
289 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
290 __genl_unregister_mc_group(family, grp);
293 static int genl_validate_ops(struct genl_family *family)
295 const struct genl_ops *ops = family->ops;
296 unsigned int n_ops = family->n_ops;
299 if (WARN_ON(n_ops && !ops))
305 for (i = 0; i < n_ops; i++) {
306 if (ops[i].dumpit == NULL && ops[i].doit == NULL)
308 for (j = i + 1; j < n_ops; j++)
309 if (ops[i].cmd == ops[j].cmd)
313 /* family is not registered yet, so no locking needed */
315 family->n_ops = n_ops;
321 * __genl_register_family - register a generic netlink family
322 * @family: generic netlink family
324 * Registers the specified family after validating it first. Only one
325 * family may be registered with the same family name or identifier.
326 * The family id may equal GENL_ID_GENERATE causing an unique id to
327 * be automatically generated and assigned.
329 * The family's ops array must already be assigned, you can use the
330 * genl_register_family_with_ops() helper function.
332 * Return 0 on success or a negative error code.
334 int __genl_register_family(struct genl_family *family)
338 if (family->id && family->id < GENL_MIN_ID)
341 if (family->id > GENL_MAX_ID)
344 err = genl_validate_ops(family);
348 INIT_LIST_HEAD(&family->mcast_groups);
352 if (genl_family_find_byname(family->name)) {
357 if (family->id == GENL_ID_GENERATE) {
358 u16 newid = genl_generate_id();
366 } else if (genl_family_find_byid(family->id)) {
371 if (family->maxattr && !family->parallel_ops) {
372 family->attrbuf = kmalloc((family->maxattr+1) *
373 sizeof(struct nlattr *), GFP_KERNEL);
374 if (family->attrbuf == NULL) {
379 family->attrbuf = NULL;
381 list_add_tail(&family->family_list, genl_family_chain(family->id));
384 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
393 EXPORT_SYMBOL(__genl_register_family);
396 * genl_unregister_family - unregister generic netlink family
397 * @family: generic netlink family
399 * Unregisters the specified family.
401 * Returns 0 on success or a negative error code.
403 int genl_unregister_family(struct genl_family *family)
405 struct genl_family *rc;
409 genl_unregister_mc_groups(family);
411 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
412 if (family->id != rc->id || strcmp(rc->name, family->name))
415 list_del(&rc->family_list);
419 kfree(family->attrbuf);
420 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
428 EXPORT_SYMBOL(genl_unregister_family);
431 * genlmsg_put - Add generic netlink header to netlink message
432 * @skb: socket buffer holding the message
433 * @portid: netlink portid the message is addressed to
434 * @seq: sequence number (usually the one of the sender)
435 * @family: generic netlink family
436 * @flags: netlink message flags
437 * @cmd: generic netlink command
439 * Returns pointer to user specific header
441 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
442 struct genl_family *family, int flags, u8 cmd)
444 struct nlmsghdr *nlh;
445 struct genlmsghdr *hdr;
447 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
448 family->hdrsize, flags);
452 hdr = nlmsg_data(nlh);
454 hdr->version = family->version;
457 return (char *) hdr + GENL_HDRLEN;
459 EXPORT_SYMBOL(genlmsg_put);
461 static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
463 /* our ops are always const - netlink API doesn't propagate that */
464 const struct genl_ops *ops = cb->data;
468 rc = ops->dumpit(skb, cb);
473 static int genl_lock_done(struct netlink_callback *cb)
475 /* our ops are always const - netlink API doesn't propagate that */
476 const struct genl_ops *ops = cb->data;
487 static int genl_family_rcv_msg(struct genl_family *family,
489 struct nlmsghdr *nlh)
491 const struct genl_ops *ops;
492 struct net *net = sock_net(skb->sk);
493 struct genl_info info;
494 struct genlmsghdr *hdr = nlmsg_data(nlh);
495 struct nlattr **attrbuf;
498 /* this family doesn't exist in this netns */
499 if (!family->netnsok && !net_eq(net, &init_net))
502 hdrlen = GENL_HDRLEN + family->hdrsize;
503 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
506 ops = genl_get_cmd(hdr->cmd, family);
510 if ((ops->flags & GENL_ADMIN_PERM) &&
511 !capable(CAP_NET_ADMIN))
514 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
517 if (ops->dumpit == NULL)
520 if (!family->parallel_ops) {
521 struct netlink_dump_control c = {
522 .module = family->module,
523 /* we have const, but the netlink API doesn't */
525 .dump = genl_lock_dumpit,
526 .done = genl_lock_done,
530 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
534 struct netlink_dump_control c = {
535 .module = family->module,
540 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
546 if (ops->doit == NULL)
549 if (family->maxattr && family->parallel_ops) {
550 attrbuf = kmalloc((family->maxattr+1) *
551 sizeof(struct nlattr *), GFP_KERNEL);
555 attrbuf = family->attrbuf;
558 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
564 info.snd_seq = nlh->nlmsg_seq;
565 info.snd_portid = NETLINK_CB(skb).portid;
567 info.genlhdr = nlmsg_data(nlh);
568 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
569 info.attrs = attrbuf;
570 genl_info_net_set(&info, net);
571 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
573 if (family->pre_doit) {
574 err = family->pre_doit(ops, skb, &info);
579 err = ops->doit(skb, &info);
581 if (family->post_doit)
582 family->post_doit(ops, skb, &info);
585 if (family->parallel_ops)
591 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
593 struct genl_family *family;
596 family = genl_family_find_byid(nlh->nlmsg_type);
600 if (!family->parallel_ops)
603 err = genl_family_rcv_msg(family, skb, nlh);
605 if (!family->parallel_ops)
611 static void genl_rcv(struct sk_buff *skb)
614 netlink_rcv_skb(skb, &genl_rcv_msg);
618 /**************************************************************************
620 **************************************************************************/
622 static struct genl_family genl_ctrl = {
626 .maxattr = CTRL_ATTR_MAX,
630 static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
631 u32 flags, struct sk_buff *skb, u8 cmd)
635 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
639 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
640 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
641 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
642 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
643 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
644 goto nla_put_failure;
647 struct nlattr *nla_ops;
650 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
652 goto nla_put_failure;
654 for (i = 0; i < family->n_ops; i++) {
656 const struct genl_ops *ops = &family->ops[i];
657 u32 op_flags = ops->flags;
660 op_flags |= GENL_CMD_CAP_DUMP;
662 op_flags |= GENL_CMD_CAP_DO;
664 op_flags |= GENL_CMD_CAP_HASPOL;
666 nest = nla_nest_start(skb, i + 1);
668 goto nla_put_failure;
670 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
671 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
672 goto nla_put_failure;
674 nla_nest_end(skb, nest);
677 nla_nest_end(skb, nla_ops);
680 if (!list_empty(&family->mcast_groups)) {
681 struct genl_multicast_group *grp;
682 struct nlattr *nla_grps;
685 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
686 if (nla_grps == NULL)
687 goto nla_put_failure;
689 list_for_each_entry(grp, &family->mcast_groups, list) {
692 nest = nla_nest_start(skb, idx++);
694 goto nla_put_failure;
696 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
697 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
699 goto nla_put_failure;
701 nla_nest_end(skb, nest);
703 nla_nest_end(skb, nla_grps);
706 return genlmsg_end(skb, hdr);
709 genlmsg_cancel(skb, hdr);
713 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
714 u32 seq, u32 flags, struct sk_buff *skb,
718 struct nlattr *nla_grps;
721 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
725 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
726 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
727 goto nla_put_failure;
729 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
730 if (nla_grps == NULL)
731 goto nla_put_failure;
733 nest = nla_nest_start(skb, 1);
735 goto nla_put_failure;
737 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
738 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
740 goto nla_put_failure;
742 nla_nest_end(skb, nest);
743 nla_nest_end(skb, nla_grps);
745 return genlmsg_end(skb, hdr);
748 genlmsg_cancel(skb, hdr);
752 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
756 struct genl_family *rt;
757 struct net *net = sock_net(skb->sk);
758 int chains_to_skip = cb->args[0];
759 int fams_to_skip = cb->args[1];
761 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
763 list_for_each_entry(rt, genl_family_chain(i), family_list) {
764 if (!rt->netnsok && !net_eq(net, &init_net))
766 if (++n < fams_to_skip)
768 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
769 cb->nlh->nlmsg_seq, NLM_F_MULTI,
770 skb, CTRL_CMD_NEWFAMILY) < 0)
784 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
785 u32 portid, int seq, u8 cmd)
790 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
792 return ERR_PTR(-ENOBUFS);
794 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
803 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
804 u32 portid, int seq, u8 cmd)
809 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
811 return ERR_PTR(-ENOBUFS);
813 err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
822 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
823 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
824 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
825 .len = GENL_NAMSIZ - 1 },
828 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
831 struct genl_family *res = NULL;
834 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
835 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
836 res = genl_family_find_byid(id);
840 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
843 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
844 res = genl_family_find_byname(name);
845 #ifdef CONFIG_MODULES
849 request_module("net-pf-%d-proto-%d-family-%s",
850 PF_NETLINK, NETLINK_GENERIC, name);
853 res = genl_family_find_byname(name);
862 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
863 /* family doesn't exist here */
867 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
872 return genlmsg_reply(msg, info);
875 static int genl_ctrl_event(int event, void *data)
878 struct genl_family *family;
879 struct genl_multicast_group *grp;
881 /* genl is still initialising */
882 if (!init_net.genl_sock)
886 case CTRL_CMD_NEWFAMILY:
887 case CTRL_CMD_DELFAMILY:
889 msg = ctrl_build_family_msg(family, 0, 0, event);
891 case CTRL_CMD_NEWMCAST_GRP:
892 case CTRL_CMD_DELMCAST_GRP:
894 family = grp->family;
895 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
904 if (!family->netnsok) {
905 genlmsg_multicast_netns(&init_net, msg, 0,
906 GENL_ID_CTRL, GFP_KERNEL);
909 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
916 static struct genl_ops genl_ctrl_ops[] = {
918 .cmd = CTRL_CMD_GETFAMILY,
919 .doit = ctrl_getfamily,
920 .dumpit = ctrl_dumpfamily,
921 .policy = ctrl_policy,
925 static struct genl_multicast_group notify_grp = {
929 static int __net_init genl_pernet_init(struct net *net)
931 struct netlink_kernel_cfg cfg = {
933 .flags = NL_CFG_F_NONROOT_RECV,
936 /* we'll bump the group number right afterwards */
937 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
939 if (!net->genl_sock && net_eq(net, &init_net))
940 panic("GENL: Cannot initialize generic netlink\n");
948 static void __net_exit genl_pernet_exit(struct net *net)
950 netlink_kernel_release(net->genl_sock);
951 net->genl_sock = NULL;
954 static struct pernet_operations genl_pernet_ops = {
955 .init = genl_pernet_init,
956 .exit = genl_pernet_exit,
959 static int __init genl_init(void)
963 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
964 INIT_LIST_HEAD(&family_ht[i]);
966 err = genl_register_family_with_ops(&genl_ctrl, genl_ctrl_ops);
970 err = register_pernet_subsys(&genl_pernet_ops);
974 err = genl_register_mc_group(&genl_ctrl, ¬ify_grp);
981 panic("GENL: Cannot register controller: %d\n", err);
984 subsys_initcall(genl_init);
986 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
990 struct net *net, *prev = NULL;
993 for_each_net_rcu(net) {
995 tmp = skb_clone(skb, flags);
1000 err = nlmsg_multicast(prev->genl_sock, tmp,
1001 portid, group, flags);
1009 return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1015 int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
1018 return genlmsg_mcast(skb, portid, group, flags);
1020 EXPORT_SYMBOL(genlmsg_multicast_allns);
1022 void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
1023 struct nlmsghdr *nlh, gfp_t flags)
1025 struct sock *sk = net->genl_sock;
1029 report = nlmsg_report(nlh);
1031 nlmsg_notify(sk, skb, portid, group, report, flags);
1033 EXPORT_SYMBOL(genl_notify);