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/init.h>
10 #include <linux/module.h>
11 #include <linux/kernel.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 <asm/semaphore.h>
19 #include <net/genetlink.h>
23 struct sock *genl_sock = NULL;
25 static DECLARE_MUTEX(genl_mutex); /* serialization of message processing */
27 static void genl_lock(void)
32 static int genl_trylock(void)
34 return down_trylock(&genl_mutex);
37 static void genl_unlock(void)
41 if (genl_sock && genl_sock->receive_queue.qlen)
42 genl_sock->data_ready(genl_sock, 0);
45 #define GENL_FAM_TAB_SIZE 16
46 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
48 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
50 * Bitmap of multicast groups that are currently in use.
52 * To avoid an allocation at boot of just one unsigned long,
53 * declare it global instead.
54 * Bit 0 is marked as already used since group 0 is invalid.
56 static unsigned long mc_group_start = 0x1;
57 static unsigned long *mc_groups = &mc_group_start;
58 static unsigned long mc_groups_longs = 1;
60 static int genl_ctrl_event(int event, void *data);
62 static inline unsigned int genl_family_hash(unsigned int id)
64 return id & GENL_FAM_TAB_MASK;
67 static inline struct list_head *genl_family_chain(unsigned int id)
69 return &family_ht[genl_family_hash(id)];
72 static struct genl_family *genl_family_find_byid(unsigned int id)
74 struct genl_family *f;
76 list_for_each_entry(f, genl_family_chain(id), family_list)
83 static struct genl_family *genl_family_find_byname(char *name)
85 struct genl_family *f;
88 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
89 list_for_each_entry(f, genl_family_chain(i), family_list)
90 if (strcmp(f->name, name) == 0)
96 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
100 list_for_each_entry(ops, &family->ops_list, ops_list)
107 /* Of course we are going to have problems once we hit
108 * 2^16 alive types, but that can only happen by year 2K
110 static inline u16 genl_generate_id(void)
112 static u16 id_gen_idx;
117 id_gen_idx = GENL_MIN_ID;
119 if (++id_gen_idx > GENL_MAX_ID) {
128 } while (genl_family_find_byid(id_gen_idx));
133 static struct genl_multicast_group notify_grp;
136 * genl_register_mc_group - register a multicast group
138 * Registers the specified multicast group and notifies userspace
139 * about the new group.
141 * Returns 0 on success or a negative error code.
143 * @family: The generic netlink family the group shall be registered for.
144 * @grp: The group to register, must have a name.
146 int genl_register_mc_group(struct genl_family *family,
147 struct genl_multicast_group *grp)
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) {
169 set_bit(id, mc_groups);
170 list_add_tail(&grp->list, &family->mcast_groups);
171 grp->family = family;
173 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
177 EXPORT_SYMBOL(genl_register_mc_group);
179 static void __genl_unregister_mc_group(struct genl_family *family,
180 struct genl_multicast_group *grp)
182 BUG_ON(grp->family != family);
184 /* We should clear this multicast group from any subscribers, but 2.4
185 * doesn't have the proper interface to do it, and we'd need a patch to
187 /*netlink_clear_multicast_users(genl_sock, grp->id);*/
188 clear_bit(grp->id, mc_groups);
189 list_del(&grp->list);
190 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
196 * genl_unregister_mc_group - unregister a multicast group
198 * Unregisters the specified multicast group and notifies userspace
199 * about it. All current listeners on the group are removed.
201 * Note: It is not necessary to unregister all multicast groups before
202 * unregistering the family, unregistering the family will cause
203 * all assigned multicast groups to be unregistered automatically.
205 * @family: Generic netlink family the group belongs to.
206 * @grp: The group to unregister, must have been registered successfully
209 void genl_unregister_mc_group(struct genl_family *family,
210 struct genl_multicast_group *grp)
213 __genl_unregister_mc_group(family, grp);
216 EXPORT_SYMBOL(genl_unregister_mc_group);
218 static void genl_unregister_mc_groups(struct genl_family *family)
220 struct genl_multicast_group *grp, *tmp;
223 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
224 __genl_unregister_mc_group(family, grp);
229 * genl_register_ops - register generic netlink operations
230 * @family: generic netlink family
231 * @ops: operations to be registered
233 * Registers the specified operations and assigns them to the specified
234 * family. Either a doit or dumpit callback must be specified or the
235 * operation will fail. Only one operation structure per command
236 * identifier may be registered.
238 * See include/net/genetlink.h for more documenation on the operations
241 * Returns 0 on success or a negative error code.
243 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
247 if (ops->dumpit == NULL && ops->doit == NULL)
250 if (genl_get_cmd(ops->cmd, family)) {
256 ops->flags |= GENL_CMD_CAP_DUMP;
258 ops->flags |= GENL_CMD_CAP_DO;
260 ops->flags |= GENL_CMD_CAP_HASPOL;
263 list_add_tail(&ops->ops_list, &family->ops_list);
266 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
273 * genl_unregister_ops - unregister generic netlink operations
274 * @family: generic netlink family
275 * @ops: operations to be unregistered
277 * Unregisters the specified operations and unassigns them from the
278 * specified family. The operation blocks until the current message
279 * processing has finished and doesn't start again until the
280 * unregister process has finished.
282 * Note: It is not necessary to unregister all operations before
283 * unregistering the family, unregistering the family will cause
284 * all assigned operations to be unregistered automatically.
286 * Returns 0 on success or a negative error code.
288 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
293 list_for_each_entry(rc, &family->ops_list, ops_list) {
295 list_del(&ops->ops_list);
297 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
307 * genl_register_family - register a generic netlink family
308 * @family: generic netlink family
310 * Registers the specified family after validating it first. Only one
311 * family may be registered with the same family name or identifier.
312 * The family id may equal GENL_ID_GENERATE causing an unique id to
313 * be automatically generated and assigned.
315 * Return 0 on success or a negative error code.
317 int genl_register_family(struct genl_family *family)
321 if (family->id && family->id < GENL_MIN_ID)
324 if (family->id > GENL_MAX_ID)
327 INIT_LIST_HEAD(&family->ops_list);
328 INIT_LIST_HEAD(&family->mcast_groups);
332 if (genl_family_find_byname(family->name)) {
337 if (genl_family_find_byid(family->id)) {
342 if (family->id == GENL_ID_GENERATE) {
343 u16 newid = genl_generate_id();
353 if (family->maxattr) {
354 family->attrbuf = kmalloc((family->maxattr+1) *
355 sizeof(struct nlattr *), GFP_KERNEL);
356 if (family->attrbuf == NULL) {
361 family->attrbuf = NULL;
363 list_add_tail(&family->family_list, genl_family_chain(family->id));
367 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
378 * genl_unregister_family - unregister generic netlink family
379 * @family: generic netlink family
381 * Unregisters the specified family.
383 * Returns 0 on success or a negative error code.
385 int genl_unregister_family(struct genl_family *family)
387 struct genl_family *rc;
389 genl_unregister_mc_groups(family);
393 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
394 if (family->id != rc->id || strcmp(rc->name, family->name))
397 list_del(&rc->family_list);
398 INIT_LIST_HEAD(&family->ops_list);
401 kfree(family->attrbuf);
402 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
412 static int null_done_func(struct netlink_callback *cb)
417 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
419 struct genl_ops *ops;
420 struct genl_family *family;
421 struct genl_info info;
422 struct genlmsghdr *hdr = nlmsg_data(nlh);
425 family = genl_family_find_byid(nlh->nlmsg_type);
429 hdrlen = GENL_HDRLEN + family->hdrsize;
430 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
433 ops = genl_get_cmd(hdr->cmd, family);
437 if ((ops->flags & GENL_ADMIN_PERM) && !capable(CAP_NET_ADMIN))
440 if (nlh->nlmsg_flags & NLM_F_DUMP) {
441 if (ops->dumpit == NULL)
444 return netlink_dump_start(genl_sock, skb, nlh,
446 ops->done ?: null_done_func);
449 if (ops->doit == NULL)
452 if (family->attrbuf) {
453 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
459 info.snd_seq = nlh->nlmsg_seq;
460 info.snd_pid = NETLINK_CB(skb).pid;
462 info.genlhdr = nlmsg_data(nlh);
463 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
464 info.attrs = family->attrbuf;
466 return ops->doit(skb, &info);
469 static void genl_rcv(struct sock *sk, int len)
471 unsigned int qlen = 0;
476 netlink_run_queue(sk, &qlen, genl_rcv_msg);
478 } while (qlen && genl_sock && genl_sock->receive_queue.qlen);
481 /**************************************************************************
483 **************************************************************************/
485 static struct genl_family genl_ctrl = {
489 .maxattr = CTRL_ATTR_MAX,
492 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
493 u32 flags, struct sk_buff *skb, u8 cmd)
497 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
501 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
502 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
503 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
504 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
505 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
507 if (!list_empty(&family->ops_list)) {
508 struct nlattr *nla_ops;
509 struct genl_ops *ops;
512 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
514 goto nla_put_failure;
516 list_for_each_entry(ops, &family->ops_list, ops_list) {
519 nest = nla_nest_start(skb, idx++);
521 goto nla_put_failure;
523 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
524 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
526 nla_nest_end(skb, nest);
529 nla_nest_end(skb, nla_ops);
532 if (!list_empty(&family->mcast_groups)) {
533 struct genl_multicast_group *grp;
534 struct nlattr *nla_grps;
537 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
538 if (nla_grps == NULL)
539 goto nla_put_failure;
541 list_for_each_entry(grp, &family->mcast_groups, list) {
544 nest = nla_nest_start(skb, idx++);
546 goto nla_put_failure;
548 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
549 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
552 nla_nest_end(skb, nest);
554 nla_nest_end(skb, nla_grps);
557 return genlmsg_end(skb, hdr);
560 return genlmsg_cancel(skb, hdr);
563 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
564 u32 seq, u32 flags, struct sk_buff *skb,
568 struct nlattr *nla_grps;
571 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
575 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
576 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
578 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
579 if (nla_grps == NULL)
580 goto nla_put_failure;
582 nest = nla_nest_start(skb, 1);
584 goto nla_put_failure;
586 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
587 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
590 nla_nest_end(skb, nest);
591 nla_nest_end(skb, nla_grps);
593 return genlmsg_end(skb, hdr);
596 return genlmsg_cancel(skb, hdr);
599 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
603 struct genl_family *rt;
604 int chains_to_skip = cb->args[0];
605 int fams_to_skip = cb->args[1];
607 if (chains_to_skip != 0)
610 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
611 if (i < chains_to_skip)
614 list_for_each_entry(rt, genl_family_chain(i), family_list) {
615 if (++n < fams_to_skip)
617 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
618 cb->nlh->nlmsg_seq, NLM_F_MULTI,
619 skb, CTRL_CMD_NEWFAMILY) < 0)
627 if (chains_to_skip != 0)
636 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
637 u32 pid, int seq, u8 cmd)
642 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
644 return ERR_PTR(-ENOBUFS);
646 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
655 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
656 u32 pid, int seq, u8 cmd)
661 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
663 return ERR_PTR(-ENOBUFS);
665 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
674 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
675 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
676 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
677 .len = GENL_NAMSIZ - 1 },
680 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
683 struct genl_family *res = NULL;
686 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
687 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
688 res = genl_family_find_byid(id);
691 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
694 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
695 res = genl_family_find_byname(name);
703 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
710 err = genlmsg_reply(msg, info);
715 static int genl_ctrl_event(int event, void *data)
719 if (genl_sock == NULL)
723 case CTRL_CMD_NEWFAMILY:
724 case CTRL_CMD_DELFAMILY:
725 msg = ctrl_build_family_msg(data, 0, 0, event);
729 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
731 case CTRL_CMD_NEWMCAST_GRP:
732 case CTRL_CMD_DELMCAST_GRP:
733 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
737 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
744 static struct genl_ops genl_ctrl_ops = {
745 .cmd = CTRL_CMD_GETFAMILY,
746 .doit = ctrl_getfamily,
747 .dumpit = ctrl_dumpfamily,
748 .policy = ctrl_policy,
751 static struct genl_multicast_group notify_grp = {
755 int __init genl_init(void)
759 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
760 INIT_LIST_HEAD(&family_ht[i]);
762 err = genl_register_family(&genl_ctrl);
766 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
768 goto errout_register;
770 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
771 genl_sock = netlink_kernel_create(NETLINK_GENERIC, genl_rcv);
772 if (genl_sock == NULL)
773 panic("GENL: Cannot initialize generic netlink\n");
775 err = genl_register_mc_group(&genl_ctrl, ¬ify_grp);
777 goto errout_register;
782 genl_unregister_family(&genl_ctrl);
784 panic("GENL: Cannot register controller: %d\n", err);
787 void __exit genl_exit(void)
791 err = genl_unregister_ops(&genl_ctrl, &genl_ctrl_ops);
793 printk("GENL: cannot unregister ops (%d)\n", err);
797 err = genl_unregister_family(&genl_ctrl);
799 printk("GENL: cannot unregister family (%d)\n", err);
805 EXPORT_SYMBOL(genl_sock);
806 EXPORT_SYMBOL(genl_register_ops);
807 EXPORT_SYMBOL(genl_unregister_ops);
808 EXPORT_SYMBOL(genl_register_family);
809 EXPORT_SYMBOL(genl_unregister_family);
811 MODULE_LICENSE("GPL");