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.
69 static unsigned long mc_group_start = 0x1;
70 static unsigned long *mc_groups = &mc_group_start;
71 static unsigned long mc_groups_longs = 1;
73 static int genl_ctrl_event(int event, void *data);
75 static inline unsigned int genl_family_hash(unsigned int id)
77 return id & GENL_FAM_TAB_MASK;
80 static inline struct list_head *genl_family_chain(unsigned int id)
82 return &family_ht[genl_family_hash(id)];
85 static struct genl_family *genl_family_find_byid(unsigned int id)
87 struct genl_family *f;
89 list_for_each_entry(f, genl_family_chain(id), family_list)
96 static struct genl_family *genl_family_find_byname(char *name)
98 struct genl_family *f;
101 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
102 list_for_each_entry(f, genl_family_chain(i), family_list)
103 if (strcmp(f->name, name) == 0)
109 static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
113 for (i = 0; i < family->n_ops; i++)
114 if (family->ops[i].cmd == cmd)
115 return &family->ops[i];
120 /* Of course we are going to have problems once we hit
121 * 2^16 alive types, but that can only happen by year 2K
123 static u16 genl_generate_id(void)
125 static u16 id_gen_idx = GENL_MIN_ID;
128 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
129 if (!genl_family_find_byid(id_gen_idx))
131 if (++id_gen_idx > GENL_MAX_ID)
132 id_gen_idx = GENL_MIN_ID;
138 static struct genl_multicast_group notify_grp;
141 * genl_register_mc_group - register a multicast group
143 * Registers the specified multicast group and notifies userspace
144 * about the new group.
146 * Returns 0 on success or a negative error code.
148 * @family: The generic netlink family the group shall be registered for.
149 * @grp: The group to register, must have a name.
151 int genl_register_mc_group(struct genl_family *family,
152 struct genl_multicast_group *grp)
155 unsigned long *new_groups;
158 BUG_ON(grp->name[0] == '\0');
159 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
163 /* special-case our own group */
164 if (grp == ¬ify_grp)
167 id = find_first_zero_bit(mc_groups,
168 mc_groups_longs * BITS_PER_LONG);
171 if (id >= mc_groups_longs * BITS_PER_LONG) {
172 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
174 if (mc_groups == &mc_group_start) {
175 new_groups = kzalloc(nlen, GFP_KERNEL);
180 mc_groups = new_groups;
181 *mc_groups = mc_group_start;
183 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
188 mc_groups = new_groups;
189 mc_groups[mc_groups_longs] = 0;
194 if (family->netnsok) {
197 netlink_table_grab();
199 for_each_net_rcu(net) {
200 err = __netlink_change_ngroups(net->genl_sock,
201 mc_groups_longs * BITS_PER_LONG);
204 * No need to roll back, can only fail if
205 * memory allocation fails and then the
206 * number of _possible_ groups has been
207 * increased on some sockets which is ok.
210 netlink_table_ungrab();
215 netlink_table_ungrab();
217 err = netlink_change_ngroups(init_net.genl_sock,
218 mc_groups_longs * BITS_PER_LONG);
224 set_bit(id, mc_groups);
225 list_add_tail(&grp->list, &family->mcast_groups);
226 grp->family = family;
228 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
233 EXPORT_SYMBOL(genl_register_mc_group);
235 static void __genl_unregister_mc_group(struct genl_family *family,
236 struct genl_multicast_group *grp)
239 BUG_ON(grp->family != family);
241 netlink_table_grab();
243 for_each_net_rcu(net)
244 __netlink_clear_multicast_users(net->genl_sock, grp->id);
246 netlink_table_ungrab();
248 clear_bit(grp->id, mc_groups);
249 list_del(&grp->list);
250 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
256 * genl_unregister_mc_group - unregister a multicast group
258 * Unregisters the specified multicast group and notifies userspace
259 * about it. All current listeners on the group are removed.
261 * Note: It is not necessary to unregister all multicast groups before
262 * unregistering the family, unregistering the family will cause
263 * all assigned multicast groups to be unregistered automatically.
265 * @family: Generic netlink family the group belongs to.
266 * @grp: The group to unregister, must have been registered successfully
269 void genl_unregister_mc_group(struct genl_family *family,
270 struct genl_multicast_group *grp)
273 __genl_unregister_mc_group(family, grp);
276 EXPORT_SYMBOL(genl_unregister_mc_group);
278 static void genl_unregister_mc_groups(struct genl_family *family)
280 struct genl_multicast_group *grp, *tmp;
282 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
283 __genl_unregister_mc_group(family, grp);
286 static int genl_validate_ops(struct genl_family *family)
288 const struct genl_ops *ops = family->ops;
289 unsigned int n_ops = family->n_ops;
292 if (WARN_ON(n_ops && !ops))
298 for (i = 0; i < n_ops; i++) {
299 if (ops[i].dumpit == NULL && ops[i].doit == NULL)
301 for (j = i + 1; j < n_ops; j++)
302 if (ops[i].cmd == ops[j].cmd)
306 /* family is not registered yet, so no locking needed */
308 family->n_ops = n_ops;
314 * __genl_register_family - register a generic netlink family
315 * @family: generic netlink family
317 * Registers the specified family after validating it first. Only one
318 * family may be registered with the same family name or identifier.
319 * The family id may equal GENL_ID_GENERATE causing an unique id to
320 * be automatically generated and assigned.
322 * The family's ops array must already be assigned, you can use the
323 * genl_register_family_with_ops() helper function.
325 * Return 0 on success or a negative error code.
327 int __genl_register_family(struct genl_family *family)
331 if (family->id && family->id < GENL_MIN_ID)
334 if (family->id > GENL_MAX_ID)
337 err = genl_validate_ops(family);
341 INIT_LIST_HEAD(&family->mcast_groups);
345 if (genl_family_find_byname(family->name)) {
350 if (family->id == GENL_ID_GENERATE) {
351 u16 newid = genl_generate_id();
359 } else if (genl_family_find_byid(family->id)) {
364 if (family->maxattr && !family->parallel_ops) {
365 family->attrbuf = kmalloc((family->maxattr+1) *
366 sizeof(struct nlattr *), GFP_KERNEL);
367 if (family->attrbuf == NULL) {
372 family->attrbuf = NULL;
374 list_add_tail(&family->family_list, genl_family_chain(family->id));
377 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
386 EXPORT_SYMBOL(__genl_register_family);
389 * genl_unregister_family - unregister generic netlink family
390 * @family: generic netlink family
392 * Unregisters the specified family.
394 * Returns 0 on success or a negative error code.
396 int genl_unregister_family(struct genl_family *family)
398 struct genl_family *rc;
402 genl_unregister_mc_groups(family);
404 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
405 if (family->id != rc->id || strcmp(rc->name, family->name))
408 list_del(&rc->family_list);
412 kfree(family->attrbuf);
413 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
421 EXPORT_SYMBOL(genl_unregister_family);
424 * genlmsg_put - Add generic netlink header to netlink message
425 * @skb: socket buffer holding the message
426 * @portid: netlink portid the message is addressed to
427 * @seq: sequence number (usually the one of the sender)
428 * @family: generic netlink family
429 * @flags: netlink message flags
430 * @cmd: generic netlink command
432 * Returns pointer to user specific header
434 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
435 struct genl_family *family, int flags, u8 cmd)
437 struct nlmsghdr *nlh;
438 struct genlmsghdr *hdr;
440 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
441 family->hdrsize, flags);
445 hdr = nlmsg_data(nlh);
447 hdr->version = family->version;
450 return (char *) hdr + GENL_HDRLEN;
452 EXPORT_SYMBOL(genlmsg_put);
454 static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
456 /* our ops are always const - netlink API doesn't propagate that */
457 const struct genl_ops *ops = cb->data;
461 rc = ops->dumpit(skb, cb);
466 static int genl_lock_done(struct netlink_callback *cb)
468 /* our ops are always const - netlink API doesn't propagate that */
469 const struct genl_ops *ops = cb->data;
480 static int genl_family_rcv_msg(struct genl_family *family,
482 struct nlmsghdr *nlh)
484 const struct genl_ops *ops;
485 struct net *net = sock_net(skb->sk);
486 struct genl_info info;
487 struct genlmsghdr *hdr = nlmsg_data(nlh);
488 struct nlattr **attrbuf;
491 /* this family doesn't exist in this netns */
492 if (!family->netnsok && !net_eq(net, &init_net))
495 hdrlen = GENL_HDRLEN + family->hdrsize;
496 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
499 ops = genl_get_cmd(hdr->cmd, family);
503 if ((ops->flags & GENL_ADMIN_PERM) &&
504 !capable(CAP_NET_ADMIN))
507 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
510 if (ops->dumpit == NULL)
513 if (!family->parallel_ops) {
514 struct netlink_dump_control c = {
515 .module = family->module,
516 /* we have const, but the netlink API doesn't */
518 .dump = genl_lock_dumpit,
519 .done = genl_lock_done,
523 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
527 struct netlink_dump_control c = {
528 .module = family->module,
533 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
539 if (ops->doit == NULL)
542 if (family->maxattr && family->parallel_ops) {
543 attrbuf = kmalloc((family->maxattr+1) *
544 sizeof(struct nlattr *), GFP_KERNEL);
548 attrbuf = family->attrbuf;
551 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
557 info.snd_seq = nlh->nlmsg_seq;
558 info.snd_portid = NETLINK_CB(skb).portid;
560 info.genlhdr = nlmsg_data(nlh);
561 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
562 info.attrs = attrbuf;
563 genl_info_net_set(&info, net);
564 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
566 if (family->pre_doit) {
567 err = family->pre_doit(ops, skb, &info);
572 err = ops->doit(skb, &info);
574 if (family->post_doit)
575 family->post_doit(ops, skb, &info);
578 if (family->parallel_ops)
584 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
586 struct genl_family *family;
589 family = genl_family_find_byid(nlh->nlmsg_type);
593 if (!family->parallel_ops)
596 err = genl_family_rcv_msg(family, skb, nlh);
598 if (!family->parallel_ops)
604 static void genl_rcv(struct sk_buff *skb)
607 netlink_rcv_skb(skb, &genl_rcv_msg);
611 /**************************************************************************
613 **************************************************************************/
615 static struct genl_family genl_ctrl = {
619 .maxattr = CTRL_ATTR_MAX,
623 static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
624 u32 flags, struct sk_buff *skb, u8 cmd)
628 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
632 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
633 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
634 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
635 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
636 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
637 goto nla_put_failure;
640 struct nlattr *nla_ops;
643 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
645 goto nla_put_failure;
647 for (i = 0; i < family->n_ops; i++) {
649 const struct genl_ops *ops = &family->ops[i];
650 u32 op_flags = ops->flags;
653 op_flags |= GENL_CMD_CAP_DUMP;
655 op_flags |= GENL_CMD_CAP_DO;
657 op_flags |= GENL_CMD_CAP_HASPOL;
659 nest = nla_nest_start(skb, i + 1);
661 goto nla_put_failure;
663 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
664 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
665 goto nla_put_failure;
667 nla_nest_end(skb, nest);
670 nla_nest_end(skb, nla_ops);
673 if (!list_empty(&family->mcast_groups)) {
674 struct genl_multicast_group *grp;
675 struct nlattr *nla_grps;
678 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
679 if (nla_grps == NULL)
680 goto nla_put_failure;
682 list_for_each_entry(grp, &family->mcast_groups, list) {
685 nest = nla_nest_start(skb, idx++);
687 goto nla_put_failure;
689 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
690 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
692 goto nla_put_failure;
694 nla_nest_end(skb, nest);
696 nla_nest_end(skb, nla_grps);
699 return genlmsg_end(skb, hdr);
702 genlmsg_cancel(skb, hdr);
706 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
707 u32 seq, u32 flags, struct sk_buff *skb,
711 struct nlattr *nla_grps;
714 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
718 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
719 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
720 goto nla_put_failure;
722 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
723 if (nla_grps == NULL)
724 goto nla_put_failure;
726 nest = nla_nest_start(skb, 1);
728 goto nla_put_failure;
730 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
731 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
733 goto nla_put_failure;
735 nla_nest_end(skb, nest);
736 nla_nest_end(skb, nla_grps);
738 return genlmsg_end(skb, hdr);
741 genlmsg_cancel(skb, hdr);
745 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
749 struct genl_family *rt;
750 struct net *net = sock_net(skb->sk);
751 int chains_to_skip = cb->args[0];
752 int fams_to_skip = cb->args[1];
754 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
756 list_for_each_entry(rt, genl_family_chain(i), family_list) {
757 if (!rt->netnsok && !net_eq(net, &init_net))
759 if (++n < fams_to_skip)
761 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
762 cb->nlh->nlmsg_seq, NLM_F_MULTI,
763 skb, CTRL_CMD_NEWFAMILY) < 0)
777 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
778 u32 portid, int seq, u8 cmd)
783 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
785 return ERR_PTR(-ENOBUFS);
787 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
796 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
797 u32 portid, int seq, u8 cmd)
802 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
804 return ERR_PTR(-ENOBUFS);
806 err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
815 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
816 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
817 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
818 .len = GENL_NAMSIZ - 1 },
821 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
824 struct genl_family *res = NULL;
827 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
828 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
829 res = genl_family_find_byid(id);
833 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
836 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
837 res = genl_family_find_byname(name);
838 #ifdef CONFIG_MODULES
842 request_module("net-pf-%d-proto-%d-family-%s",
843 PF_NETLINK, NETLINK_GENERIC, name);
846 res = genl_family_find_byname(name);
855 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
856 /* family doesn't exist here */
860 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
865 return genlmsg_reply(msg, info);
868 static int genl_ctrl_event(int event, void *data)
871 struct genl_family *family;
872 struct genl_multicast_group *grp;
874 /* genl is still initialising */
875 if (!init_net.genl_sock)
879 case CTRL_CMD_NEWFAMILY:
880 case CTRL_CMD_DELFAMILY:
882 msg = ctrl_build_family_msg(family, 0, 0, event);
884 case CTRL_CMD_NEWMCAST_GRP:
885 case CTRL_CMD_DELMCAST_GRP:
887 family = grp->family;
888 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
897 if (!family->netnsok) {
898 genlmsg_multicast_netns(&init_net, msg, 0,
899 GENL_ID_CTRL, GFP_KERNEL);
902 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
909 static struct genl_ops genl_ctrl_ops[] = {
911 .cmd = CTRL_CMD_GETFAMILY,
912 .doit = ctrl_getfamily,
913 .dumpit = ctrl_dumpfamily,
914 .policy = ctrl_policy,
918 static struct genl_multicast_group notify_grp = {
922 static int __net_init genl_pernet_init(struct net *net)
924 struct netlink_kernel_cfg cfg = {
926 .flags = NL_CFG_F_NONROOT_RECV,
929 /* we'll bump the group number right afterwards */
930 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
932 if (!net->genl_sock && net_eq(net, &init_net))
933 panic("GENL: Cannot initialize generic netlink\n");
941 static void __net_exit genl_pernet_exit(struct net *net)
943 netlink_kernel_release(net->genl_sock);
944 net->genl_sock = NULL;
947 static struct pernet_operations genl_pernet_ops = {
948 .init = genl_pernet_init,
949 .exit = genl_pernet_exit,
952 static int __init genl_init(void)
956 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
957 INIT_LIST_HEAD(&family_ht[i]);
959 err = genl_register_family_with_ops(&genl_ctrl, genl_ctrl_ops);
963 err = register_pernet_subsys(&genl_pernet_ops);
967 err = genl_register_mc_group(&genl_ctrl, ¬ify_grp);
974 panic("GENL: Cannot register controller: %d\n", err);
977 subsys_initcall(genl_init);
979 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
983 struct net *net, *prev = NULL;
986 for_each_net_rcu(net) {
988 tmp = skb_clone(skb, flags);
993 err = nlmsg_multicast(prev->genl_sock, tmp,
994 portid, group, flags);
1002 return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1008 int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
1011 return genlmsg_mcast(skb, portid, group, flags);
1013 EXPORT_SYMBOL(genlmsg_multicast_allns);
1015 void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
1016 struct nlmsghdr *nlh, gfp_t flags)
1018 struct sock *sk = net->genl_sock;
1022 report = nlmsg_report(nlh);
1024 nlmsg_notify(sk, skb, portid, group, report, flags);
1026 EXPORT_SYMBOL(genl_notify);