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/errno.h>
12 #include <linux/types.h>
13 #include <linux/socket.h>
14 #include <linux/string.h>
15 #include <linux/skbuff.h>
16 #include <linux/mutex.h>
17 #include <linux/bitmap.h>
19 #include <net/genetlink.h>
21 struct sock *genl_sock = NULL;
23 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
25 static void genl_lock(void)
27 mutex_lock(&genl_mutex);
30 static int genl_trylock(void)
32 return !mutex_trylock(&genl_mutex);
35 static void genl_unlock(void)
37 mutex_unlock(&genl_mutex);
39 if (genl_sock && genl_sock->sk_receive_queue.qlen)
40 genl_sock->sk_data_ready(genl_sock, 0);
43 #define GENL_FAM_TAB_SIZE 16
44 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
46 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
48 * Bitmap of multicast groups that are currently in use.
50 * To avoid an allocation at boot of just one unsigned long,
51 * declare it global instead.
52 * Bit 0 is marked as already used since group 0 is invalid.
54 static unsigned long mc_group_start = 0x1;
55 static unsigned long *mc_groups = &mc_group_start;
56 static unsigned long mc_groups_longs = 1;
58 static int genl_ctrl_event(int event, void *data);
60 static inline unsigned int genl_family_hash(unsigned int id)
62 return id & GENL_FAM_TAB_MASK;
65 static inline struct list_head *genl_family_chain(unsigned int id)
67 return &family_ht[genl_family_hash(id)];
70 static struct genl_family *genl_family_find_byid(unsigned int id)
72 struct genl_family *f;
74 list_for_each_entry(f, genl_family_chain(id), family_list)
81 static struct genl_family *genl_family_find_byname(char *name)
83 struct genl_family *f;
86 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
87 list_for_each_entry(f, genl_family_chain(i), family_list)
88 if (strcmp(f->name, name) == 0)
94 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
98 list_for_each_entry(ops, &family->ops_list, ops_list)
105 /* Of course we are going to have problems once we hit
106 * 2^16 alive types, but that can only happen by year 2K
108 static inline u16 genl_generate_id(void)
110 static u16 id_gen_idx;
115 id_gen_idx = GENL_MIN_ID;
117 if (++id_gen_idx > GENL_MAX_ID) {
126 } while (genl_family_find_byid(id_gen_idx));
131 static struct genl_multicast_group notify_grp;
134 * genl_register_mc_group - register a multicast group
136 * Registers the specified multicast group and notifies userspace
137 * about the new group.
139 * Returns 0 on success or a negative error code.
141 * @family: The generic netlink family the group shall be registered for.
142 * @grp: The group to register, must have a name.
144 int genl_register_mc_group(struct genl_family *family,
145 struct genl_multicast_group *grp)
148 unsigned long *new_groups;
151 BUG_ON(grp->name[0] == '\0');
155 /* special-case our own group */
156 if (grp == ¬ify_grp)
159 id = find_first_zero_bit(mc_groups,
160 mc_groups_longs * BITS_PER_LONG);
163 if (id >= mc_groups_longs * BITS_PER_LONG) {
164 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
166 if (mc_groups == &mc_group_start) {
167 new_groups = kzalloc(nlen, GFP_KERNEL);
172 mc_groups = new_groups;
173 *mc_groups = mc_group_start;
175 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
180 mc_groups = new_groups;
181 mc_groups[mc_groups_longs] = 0;
186 err = netlink_change_ngroups(genl_sock,
187 mc_groups_longs * BITS_PER_LONG);
192 set_bit(id, mc_groups);
193 list_add_tail(&grp->list, &family->mcast_groups);
194 grp->family = family;
196 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
201 EXPORT_SYMBOL(genl_register_mc_group);
203 static void __genl_unregister_mc_group(struct genl_family *family,
204 struct genl_multicast_group *grp)
206 BUG_ON(grp->family != family);
207 netlink_clear_multicast_users(genl_sock, grp->id);
208 clear_bit(grp->id, mc_groups);
209 list_del(&grp->list);
210 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
216 * genl_unregister_mc_group - unregister a multicast group
218 * Unregisters the specified multicast group and notifies userspace
219 * about it. All current listeners on the group are removed.
221 * Note: It is not necessary to unregister all multicast groups before
222 * unregistering the family, unregistering the family will cause
223 * all assigned multicast groups to be unregistered automatically.
225 * @family: Generic netlink family the group belongs to.
226 * @grp: The group to unregister, must have been registered successfully
229 void genl_unregister_mc_group(struct genl_family *family,
230 struct genl_multicast_group *grp)
233 __genl_unregister_mc_group(family, grp);
237 static void genl_unregister_mc_groups(struct genl_family *family)
239 struct genl_multicast_group *grp, *tmp;
242 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
243 __genl_unregister_mc_group(family, grp);
248 * genl_register_ops - register generic netlink operations
249 * @family: generic netlink family
250 * @ops: operations to be registered
252 * Registers the specified operations and assigns them to the specified
253 * family. Either a doit or dumpit callback must be specified or the
254 * operation will fail. Only one operation structure per command
255 * identifier may be registered.
257 * See include/net/genetlink.h for more documenation on the operations
260 * Returns 0 on success or a negative error code.
262 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
266 if (ops->dumpit == NULL && ops->doit == NULL)
269 if (genl_get_cmd(ops->cmd, family)) {
275 ops->flags |= GENL_CMD_CAP_DUMP;
277 ops->flags |= GENL_CMD_CAP_DO;
279 ops->flags |= GENL_CMD_CAP_HASPOL;
282 list_add_tail(&ops->ops_list, &family->ops_list);
285 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
292 * genl_unregister_ops - unregister generic netlink operations
293 * @family: generic netlink family
294 * @ops: operations to be unregistered
296 * Unregisters the specified operations and unassigns them from the
297 * specified family. The operation blocks until the current message
298 * processing has finished and doesn't start again until the
299 * unregister process has finished.
301 * Note: It is not necessary to unregister all operations before
302 * unregistering the family, unregistering the family will cause
303 * all assigned operations to be unregistered automatically.
305 * Returns 0 on success or a negative error code.
307 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
312 list_for_each_entry(rc, &family->ops_list, ops_list) {
314 list_del(&ops->ops_list);
316 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
326 * genl_register_family - register a generic netlink family
327 * @family: generic netlink family
329 * Registers the specified family after validating it first. Only one
330 * family may be registered with the same family name or identifier.
331 * The family id may equal GENL_ID_GENERATE causing an unique id to
332 * be automatically generated and assigned.
334 * Return 0 on success or a negative error code.
336 int genl_register_family(struct genl_family *family)
340 if (family->id && family->id < GENL_MIN_ID)
343 if (family->id > GENL_MAX_ID)
346 INIT_LIST_HEAD(&family->ops_list);
347 INIT_LIST_HEAD(&family->mcast_groups);
351 if (genl_family_find_byname(family->name)) {
356 if (genl_family_find_byid(family->id)) {
361 if (family->id == GENL_ID_GENERATE) {
362 u16 newid = genl_generate_id();
372 if (family->maxattr) {
373 family->attrbuf = kmalloc((family->maxattr+1) *
374 sizeof(struct nlattr *), GFP_KERNEL);
375 if (family->attrbuf == NULL) {
380 family->attrbuf = NULL;
382 list_add_tail(&family->family_list, genl_family_chain(family->id));
385 genl_ctrl_event(CTRL_CMD_NEWFAMILY, 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;
407 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);
416 INIT_LIST_HEAD(&family->ops_list);
419 kfree(family->attrbuf);
420 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
429 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
431 struct genl_ops *ops;
432 struct genl_family *family;
433 struct genl_info info;
434 struct genlmsghdr *hdr = nlmsg_data(nlh);
437 family = genl_family_find_byid(nlh->nlmsg_type);
441 hdrlen = GENL_HDRLEN + family->hdrsize;
442 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
445 ops = genl_get_cmd(hdr->cmd, family);
449 if ((ops->flags & GENL_ADMIN_PERM) &&
450 security_netlink_recv(skb, CAP_NET_ADMIN))
453 if (nlh->nlmsg_flags & NLM_F_DUMP) {
454 if (ops->dumpit == NULL)
457 return netlink_dump_start(genl_sock, skb, nlh,
458 ops->dumpit, ops->done);
461 if (ops->doit == NULL)
464 if (family->attrbuf) {
465 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
471 info.snd_seq = nlh->nlmsg_seq;
472 info.snd_pid = NETLINK_CB(skb).pid;
474 info.genlhdr = nlmsg_data(nlh);
475 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
476 info.attrs = family->attrbuf;
478 return ops->doit(skb, &info);
481 static void genl_rcv(struct sock *sk, int len)
483 unsigned int qlen = 0;
488 netlink_run_queue(sk, &qlen, genl_rcv_msg);
490 } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen);
493 /**************************************************************************
495 **************************************************************************/
497 static struct genl_family genl_ctrl = {
501 .maxattr = CTRL_ATTR_MAX,
504 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
505 u32 flags, struct sk_buff *skb, u8 cmd)
509 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
513 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
514 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
515 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
516 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
517 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
519 if (!list_empty(&family->ops_list)) {
520 struct nlattr *nla_ops;
521 struct genl_ops *ops;
524 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
526 goto nla_put_failure;
528 list_for_each_entry(ops, &family->ops_list, ops_list) {
531 nest = nla_nest_start(skb, idx++);
533 goto nla_put_failure;
535 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
536 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
538 nla_nest_end(skb, nest);
541 nla_nest_end(skb, nla_ops);
544 if (!list_empty(&family->mcast_groups)) {
545 struct genl_multicast_group *grp;
546 struct nlattr *nla_grps;
549 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
550 if (nla_grps == NULL)
551 goto nla_put_failure;
553 list_for_each_entry(grp, &family->mcast_groups, list) {
556 nest = nla_nest_start(skb, idx++);
558 goto nla_put_failure;
560 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
561 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
564 nla_nest_end(skb, nest);
566 nla_nest_end(skb, nla_grps);
569 return genlmsg_end(skb, hdr);
572 return genlmsg_cancel(skb, hdr);
575 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
576 u32 seq, u32 flags, struct sk_buff *skb,
580 struct nlattr *nla_grps;
583 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
587 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
588 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
590 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
591 if (nla_grps == NULL)
592 goto nla_put_failure;
594 nest = nla_nest_start(skb, 1);
596 goto nla_put_failure;
598 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
599 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
602 nla_nest_end(skb, nest);
603 nla_nest_end(skb, nla_grps);
605 return genlmsg_end(skb, hdr);
608 return genlmsg_cancel(skb, hdr);
611 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
615 struct genl_family *rt;
616 int chains_to_skip = cb->args[0];
617 int fams_to_skip = cb->args[1];
619 if (chains_to_skip != 0)
622 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
623 if (i < chains_to_skip)
626 list_for_each_entry(rt, genl_family_chain(i), family_list) {
627 if (++n < fams_to_skip)
629 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
630 cb->nlh->nlmsg_seq, NLM_F_MULTI,
631 skb, CTRL_CMD_NEWFAMILY) < 0)
639 if (chains_to_skip != 0)
648 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
649 u32 pid, int seq, u8 cmd)
654 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
656 return ERR_PTR(-ENOBUFS);
658 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
667 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
668 u32 pid, int seq, u8 cmd)
673 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
675 return ERR_PTR(-ENOBUFS);
677 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
686 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
687 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
688 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
689 .len = GENL_NAMSIZ - 1 },
692 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
695 struct genl_family *res = NULL;
698 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
699 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
700 res = genl_family_find_byid(id);
703 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
706 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
707 res = genl_family_find_byname(name);
715 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
722 err = genlmsg_reply(msg, info);
727 static int genl_ctrl_event(int event, void *data)
731 if (genl_sock == NULL)
735 case CTRL_CMD_NEWFAMILY:
736 case CTRL_CMD_DELFAMILY:
737 msg = ctrl_build_family_msg(data, 0, 0, event);
741 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
743 case CTRL_CMD_NEWMCAST_GRP:
744 case CTRL_CMD_DELMCAST_GRP:
745 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
749 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
756 static struct genl_ops genl_ctrl_ops = {
757 .cmd = CTRL_CMD_GETFAMILY,
758 .doit = ctrl_getfamily,
759 .dumpit = ctrl_dumpfamily,
760 .policy = ctrl_policy,
763 static struct genl_multicast_group notify_grp = {
767 static int __init genl_init(void)
771 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
772 INIT_LIST_HEAD(&family_ht[i]);
774 err = genl_register_family(&genl_ctrl);
778 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
780 goto errout_register;
782 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
784 /* we'll bump the group number right afterwards */
785 genl_sock = netlink_kernel_create(NETLINK_GENERIC, 0, genl_rcv,
787 if (genl_sock == NULL)
788 panic("GENL: Cannot initialize generic netlink\n");
790 err = genl_register_mc_group(&genl_ctrl, ¬ify_grp);
792 goto errout_register;
797 genl_unregister_family(&genl_ctrl);
799 panic("GENL: Cannot register controller: %d\n", err);
802 subsys_initcall(genl_init);
804 EXPORT_SYMBOL(genl_sock);
805 EXPORT_SYMBOL(genl_register_ops);
806 EXPORT_SYMBOL(genl_unregister_ops);
807 EXPORT_SYMBOL(genl_register_family);
808 EXPORT_SYMBOL(genl_unregister_family);