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);
37 int lockdep_genl_is_held(void)
39 return lockdep_is_held(&genl_mutex);
41 EXPORT_SYMBOL(lockdep_genl_is_held);
44 #define GENL_FAM_TAB_SIZE 16
45 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
47 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
49 * Bitmap of multicast groups that are currently in use.
51 * To avoid an allocation at boot of just one unsigned long,
52 * declare it global instead.
53 * Bit 0 is marked as already used since group 0 is invalid.
55 static unsigned long mc_group_start = 0x1;
56 static unsigned long *mc_groups = &mc_group_start;
57 static unsigned long mc_groups_longs = 1;
59 static int genl_ctrl_event(int event, void *data);
61 static inline unsigned int genl_family_hash(unsigned int id)
63 return id & GENL_FAM_TAB_MASK;
66 static inline struct list_head *genl_family_chain(unsigned int id)
68 return &family_ht[genl_family_hash(id)];
71 static struct genl_family *genl_family_find_byid(unsigned int id)
73 struct genl_family *f;
75 list_for_each_entry(f, genl_family_chain(id), family_list)
82 static struct genl_family *genl_family_find_byname(char *name)
84 struct genl_family *f;
87 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
88 list_for_each_entry(f, genl_family_chain(i), family_list)
89 if (strcmp(f->name, name) == 0)
95 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
99 list_for_each_entry(ops, &family->ops_list, ops_list)
106 /* Of course we are going to have problems once we hit
107 * 2^16 alive types, but that can only happen by year 2K
109 static u16 genl_generate_id(void)
111 static u16 id_gen_idx = GENL_MIN_ID;
114 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
115 if (!genl_family_find_byid(id_gen_idx))
117 if (++id_gen_idx > GENL_MAX_ID)
118 id_gen_idx = GENL_MIN_ID;
124 static struct genl_multicast_group notify_grp;
127 * genl_register_mc_group - register a multicast group
129 * Registers the specified multicast group and notifies userspace
130 * about the new group.
132 * Returns 0 on success or a negative error code.
134 * @family: The generic netlink family the group shall be registered for.
135 * @grp: The group to register, must have a name.
137 int genl_register_mc_group(struct genl_family *family,
138 struct genl_multicast_group *grp)
141 unsigned long *new_groups;
144 BUG_ON(grp->name[0] == '\0');
145 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
149 /* special-case our own group */
150 if (grp == ¬ify_grp)
153 id = find_first_zero_bit(mc_groups,
154 mc_groups_longs * BITS_PER_LONG);
157 if (id >= mc_groups_longs * BITS_PER_LONG) {
158 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
160 if (mc_groups == &mc_group_start) {
161 new_groups = kzalloc(nlen, GFP_KERNEL);
166 mc_groups = new_groups;
167 *mc_groups = mc_group_start;
169 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
174 mc_groups = new_groups;
175 mc_groups[mc_groups_longs] = 0;
180 if (family->netnsok) {
183 netlink_table_grab();
185 for_each_net_rcu(net) {
186 err = __netlink_change_ngroups(net->genl_sock,
187 mc_groups_longs * BITS_PER_LONG);
190 * No need to roll back, can only fail if
191 * memory allocation fails and then the
192 * number of _possible_ groups has been
193 * increased on some sockets which is ok.
196 netlink_table_ungrab();
201 netlink_table_ungrab();
203 err = netlink_change_ngroups(init_net.genl_sock,
204 mc_groups_longs * BITS_PER_LONG);
210 set_bit(id, mc_groups);
211 list_add_tail(&grp->list, &family->mcast_groups);
212 grp->family = family;
214 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
219 EXPORT_SYMBOL(genl_register_mc_group);
221 static void __genl_unregister_mc_group(struct genl_family *family,
222 struct genl_multicast_group *grp)
225 BUG_ON(grp->family != family);
227 netlink_table_grab();
229 for_each_net_rcu(net)
230 __netlink_clear_multicast_users(net->genl_sock, grp->id);
232 netlink_table_ungrab();
234 clear_bit(grp->id, mc_groups);
235 list_del(&grp->list);
236 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
242 * genl_unregister_mc_group - unregister a multicast group
244 * Unregisters the specified multicast group and notifies userspace
245 * about it. All current listeners on the group are removed.
247 * Note: It is not necessary to unregister all multicast groups before
248 * unregistering the family, unregistering the family will cause
249 * all assigned multicast groups to be unregistered automatically.
251 * @family: Generic netlink family the group belongs to.
252 * @grp: The group to unregister, must have been registered successfully
255 void genl_unregister_mc_group(struct genl_family *family,
256 struct genl_multicast_group *grp)
259 __genl_unregister_mc_group(family, grp);
262 EXPORT_SYMBOL(genl_unregister_mc_group);
264 static void genl_unregister_mc_groups(struct genl_family *family)
266 struct genl_multicast_group *grp, *tmp;
268 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
269 __genl_unregister_mc_group(family, grp);
273 * genl_register_ops - register generic netlink operations
274 * @family: generic netlink family
275 * @ops: operations to be registered
277 * Registers the specified operations and assigns them to the specified
278 * family. Either a doit or dumpit callback must be specified or the
279 * operation will fail. Only one operation structure per command
280 * identifier may be registered.
282 * See include/net/genetlink.h for more documenation on the operations
285 * Returns 0 on success or a negative error code.
287 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
291 if (ops->dumpit == NULL && ops->doit == NULL)
294 if (genl_get_cmd(ops->cmd, family)) {
300 ops->flags |= GENL_CMD_CAP_DUMP;
302 ops->flags |= GENL_CMD_CAP_DO;
304 ops->flags |= GENL_CMD_CAP_HASPOL;
307 list_add_tail(&ops->ops_list, &family->ops_list);
310 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
315 EXPORT_SYMBOL(genl_register_ops);
318 * genl_unregister_ops - unregister generic netlink operations
319 * @family: generic netlink family
320 * @ops: operations to be unregistered
322 * Unregisters the specified operations and unassigns them from the
323 * specified family. The operation blocks until the current message
324 * processing has finished and doesn't start again until the
325 * unregister process has finished.
327 * Note: It is not necessary to unregister all operations before
328 * unregistering the family, unregistering the family will cause
329 * all assigned operations to be unregistered automatically.
331 * Returns 0 on success or a negative error code.
333 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
338 list_for_each_entry(rc, &family->ops_list, ops_list) {
340 list_del(&ops->ops_list);
342 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
350 EXPORT_SYMBOL(genl_unregister_ops);
353 * genl_register_family - register a generic netlink family
354 * @family: generic netlink family
356 * Registers the specified family after validating it first. Only one
357 * family may be registered with the same family name or identifier.
358 * The family id may equal GENL_ID_GENERATE causing an unique id to
359 * be automatically generated and assigned.
361 * Return 0 on success or a negative error code.
363 int genl_register_family(struct genl_family *family)
367 if (family->id && family->id < GENL_MIN_ID)
370 if (family->id > GENL_MAX_ID)
373 INIT_LIST_HEAD(&family->ops_list);
374 INIT_LIST_HEAD(&family->mcast_groups);
378 if (genl_family_find_byname(family->name)) {
383 if (family->id == GENL_ID_GENERATE) {
384 u16 newid = genl_generate_id();
392 } else if (genl_family_find_byid(family->id)) {
397 if (family->maxattr) {
398 family->attrbuf = kmalloc((family->maxattr+1) *
399 sizeof(struct nlattr *), GFP_KERNEL);
400 if (family->attrbuf == NULL) {
405 family->attrbuf = NULL;
407 list_add_tail(&family->family_list, genl_family_chain(family->id));
410 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
419 EXPORT_SYMBOL(genl_register_family);
422 * genl_register_family_with_ops - register a generic netlink family
423 * @family: generic netlink family
424 * @ops: operations to be registered
425 * @n_ops: number of elements to register
427 * Registers the specified family and operations from the specified table.
428 * Only one family may be registered with the same family name or identifier.
430 * The family id may equal GENL_ID_GENERATE causing an unique id to
431 * be automatically generated and assigned.
433 * Either a doit or dumpit callback must be specified for every registered
434 * operation or the function will fail. Only one operation structure per
435 * command identifier may be registered.
437 * See include/net/genetlink.h for more documenation on the operations
440 * This is equivalent to calling genl_register_family() followed by
441 * genl_register_ops() for every operation entry in the table taking
442 * care to unregister the family on error path.
444 * Return 0 on success or a negative error code.
446 int genl_register_family_with_ops(struct genl_family *family,
447 struct genl_ops *ops, size_t n_ops)
451 err = genl_register_family(family);
455 for (i = 0; i < n_ops; ++i, ++ops) {
456 err = genl_register_ops(family, ops);
462 genl_unregister_family(family);
465 EXPORT_SYMBOL(genl_register_family_with_ops);
468 * genl_unregister_family - unregister generic netlink family
469 * @family: generic netlink family
471 * Unregisters the specified family.
473 * Returns 0 on success or a negative error code.
475 int genl_unregister_family(struct genl_family *family)
477 struct genl_family *rc;
481 genl_unregister_mc_groups(family);
483 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
484 if (family->id != rc->id || strcmp(rc->name, family->name))
487 list_del(&rc->family_list);
488 INIT_LIST_HEAD(&family->ops_list);
491 kfree(family->attrbuf);
492 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
500 EXPORT_SYMBOL(genl_unregister_family);
503 * genlmsg_put - Add generic netlink header to netlink message
504 * @skb: socket buffer holding the message
505 * @portid: netlink portid the message is addressed to
506 * @seq: sequence number (usually the one of the sender)
507 * @family: generic netlink family
508 * @flags: netlink message flags
509 * @cmd: generic netlink command
511 * Returns pointer to user specific header
513 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
514 struct genl_family *family, int flags, u8 cmd)
516 struct nlmsghdr *nlh;
517 struct genlmsghdr *hdr;
519 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
520 family->hdrsize, flags);
524 hdr = nlmsg_data(nlh);
526 hdr->version = family->version;
529 return (char *) hdr + GENL_HDRLEN;
531 EXPORT_SYMBOL(genlmsg_put);
533 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
535 struct genl_ops *ops;
536 struct genl_family *family;
537 struct net *net = sock_net(skb->sk);
538 struct genl_info info;
539 struct genlmsghdr *hdr = nlmsg_data(nlh);
542 family = genl_family_find_byid(nlh->nlmsg_type);
546 /* this family doesn't exist in this netns */
547 if (!family->netnsok && !net_eq(net, &init_net))
550 hdrlen = GENL_HDRLEN + family->hdrsize;
551 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
554 ops = genl_get_cmd(hdr->cmd, family);
558 if ((ops->flags & GENL_ADMIN_PERM) &&
559 !capable(CAP_NET_ADMIN))
562 if (nlh->nlmsg_flags & NLM_F_DUMP) {
563 if (ops->dumpit == NULL)
568 struct netlink_dump_control c = {
572 err = netlink_dump_start(net->genl_sock, skb, nlh, &c);
578 if (ops->doit == NULL)
581 if (family->attrbuf) {
582 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
588 info.snd_seq = nlh->nlmsg_seq;
589 info.snd_portid = NETLINK_CB(skb).portid;
591 info.genlhdr = nlmsg_data(nlh);
592 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
593 info.attrs = family->attrbuf;
594 genl_info_net_set(&info, net);
595 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
597 if (family->pre_doit) {
598 err = family->pre_doit(ops, skb, &info);
603 err = ops->doit(skb, &info);
605 if (family->post_doit)
606 family->post_doit(ops, skb, &info);
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;
646 if (!list_empty(&family->ops_list)) {
647 struct nlattr *nla_ops;
648 struct genl_ops *ops;
651 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
653 goto nla_put_failure;
655 list_for_each_entry(ops, &family->ops_list, ops_list) {
658 nest = nla_nest_start(skb, idx++);
660 goto nla_put_failure;
662 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
663 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, ops->flags))
664 goto nla_put_failure;
666 nla_nest_end(skb, nest);
669 nla_nest_end(skb, nla_ops);
672 if (!list_empty(&family->mcast_groups)) {
673 struct genl_multicast_group *grp;
674 struct nlattr *nla_grps;
677 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
678 if (nla_grps == NULL)
679 goto nla_put_failure;
681 list_for_each_entry(grp, &family->mcast_groups, list) {
684 nest = nla_nest_start(skb, idx++);
686 goto nla_put_failure;
688 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
689 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
691 goto nla_put_failure;
693 nla_nest_end(skb, nest);
695 nla_nest_end(skb, nla_grps);
698 return genlmsg_end(skb, hdr);
701 genlmsg_cancel(skb, hdr);
705 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
706 u32 seq, u32 flags, struct sk_buff *skb,
710 struct nlattr *nla_grps;
713 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
717 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
718 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
719 goto nla_put_failure;
721 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
722 if (nla_grps == NULL)
723 goto nla_put_failure;
725 nest = nla_nest_start(skb, 1);
727 goto nla_put_failure;
729 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
730 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
732 goto nla_put_failure;
734 nla_nest_end(skb, nest);
735 nla_nest_end(skb, nla_grps);
737 return genlmsg_end(skb, hdr);
740 genlmsg_cancel(skb, hdr);
744 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
748 struct genl_family *rt;
749 struct net *net = sock_net(skb->sk);
750 int chains_to_skip = cb->args[0];
751 int fams_to_skip = cb->args[1];
753 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
755 list_for_each_entry(rt, genl_family_chain(i), family_list) {
756 if (!rt->netnsok && !net_eq(net, &init_net))
758 if (++n < fams_to_skip)
760 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
761 cb->nlh->nlmsg_seq, NLM_F_MULTI,
762 skb, CTRL_CMD_NEWFAMILY) < 0)
776 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
777 u32 portid, int seq, u8 cmd)
782 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
784 return ERR_PTR(-ENOBUFS);
786 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
795 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
796 u32 portid, int seq, u8 cmd)
801 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
803 return ERR_PTR(-ENOBUFS);
805 err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
814 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
815 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
816 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
817 .len = GENL_NAMSIZ - 1 },
820 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
823 struct genl_family *res = NULL;
826 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
827 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
828 res = genl_family_find_byid(id);
832 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
835 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
836 res = genl_family_find_byname(name);
837 #ifdef CONFIG_MODULES
840 request_module("net-pf-%d-proto-%d-family-%s",
841 PF_NETLINK, NETLINK_GENERIC, name);
843 res = genl_family_find_byname(name);
852 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
853 /* family doesn't exist here */
857 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
862 return genlmsg_reply(msg, info);
865 static int genl_ctrl_event(int event, void *data)
868 struct genl_family *family;
869 struct genl_multicast_group *grp;
871 /* genl is still initialising */
872 if (!init_net.genl_sock)
876 case CTRL_CMD_NEWFAMILY:
877 case CTRL_CMD_DELFAMILY:
879 msg = ctrl_build_family_msg(family, 0, 0, event);
881 case CTRL_CMD_NEWMCAST_GRP:
882 case CTRL_CMD_DELMCAST_GRP:
884 family = grp->family;
885 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
894 if (!family->netnsok) {
895 genlmsg_multicast_netns(&init_net, msg, 0,
896 GENL_ID_CTRL, GFP_KERNEL);
899 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
906 static struct genl_ops genl_ctrl_ops = {
907 .cmd = CTRL_CMD_GETFAMILY,
908 .doit = ctrl_getfamily,
909 .dumpit = ctrl_dumpfamily,
910 .policy = ctrl_policy,
913 static struct genl_multicast_group notify_grp = {
917 static int __net_init genl_pernet_init(struct net *net)
919 struct netlink_kernel_cfg cfg = {
921 .cb_mutex = &genl_mutex,
922 .flags = NL_CFG_F_NONROOT_RECV,
925 /* we'll bump the group number right afterwards */
926 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
928 if (!net->genl_sock && net_eq(net, &init_net))
929 panic("GENL: Cannot initialize generic netlink\n");
937 static void __net_exit genl_pernet_exit(struct net *net)
939 netlink_kernel_release(net->genl_sock);
940 net->genl_sock = NULL;
943 static struct pernet_operations genl_pernet_ops = {
944 .init = genl_pernet_init,
945 .exit = genl_pernet_exit,
948 static int __init genl_init(void)
952 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
953 INIT_LIST_HEAD(&family_ht[i]);
955 err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
959 err = register_pernet_subsys(&genl_pernet_ops);
963 err = genl_register_mc_group(&genl_ctrl, ¬ify_grp);
970 panic("GENL: Cannot register controller: %d\n", err);
973 subsys_initcall(genl_init);
975 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
979 struct net *net, *prev = NULL;
982 for_each_net_rcu(net) {
984 tmp = skb_clone(skb, flags);
989 err = nlmsg_multicast(prev->genl_sock, tmp,
990 portid, group, flags);
998 return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1004 int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
1007 return genlmsg_mcast(skb, portid, group, flags);
1009 EXPORT_SYMBOL(genlmsg_multicast_allns);
1011 void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
1012 struct nlmsghdr *nlh, gfp_t flags)
1014 struct sock *sk = net->genl_sock;
1018 report = nlmsg_report(nlh);
1020 nlmsg_notify(sk, skb, portid, group, report, flags);
1022 EXPORT_SYMBOL(genl_notify);