pciehp: minor cleanups for pciehp_hpc.c
[linux-2.6/zen-sources.git] / net / netlink / genetlink.c
blob150579a21469d664563dfcee8bdef5d3f0e355e1
1 /*
2 * NETLINK Generic Netlink Family
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
6 * Johannes Berg <johannes@sipsolutions.net>
7 */
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>
18 #include <net/sock.h>
19 #include <net/genetlink.h>
21 struct sock *genl_sock = NULL;
23 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
25 static inline void genl_lock(void)
27 mutex_lock(&genl_mutex);
30 static inline void genl_unlock(void)
32 mutex_unlock(&genl_mutex);
35 #define GENL_FAM_TAB_SIZE 16
36 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
38 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
40 * Bitmap of multicast groups that are currently in use.
42 * To avoid an allocation at boot of just one unsigned long,
43 * declare it global instead.
44 * Bit 0 is marked as already used since group 0 is invalid.
46 static unsigned long mc_group_start = 0x1;
47 static unsigned long *mc_groups = &mc_group_start;
48 static unsigned long mc_groups_longs = 1;
50 static int genl_ctrl_event(int event, void *data);
52 static inline unsigned int genl_family_hash(unsigned int id)
54 return id & GENL_FAM_TAB_MASK;
57 static inline struct list_head *genl_family_chain(unsigned int id)
59 return &family_ht[genl_family_hash(id)];
62 static struct genl_family *genl_family_find_byid(unsigned int id)
64 struct genl_family *f;
66 list_for_each_entry(f, genl_family_chain(id), family_list)
67 if (f->id == id)
68 return f;
70 return NULL;
73 static struct genl_family *genl_family_find_byname(char *name)
75 struct genl_family *f;
76 int i;
78 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
79 list_for_each_entry(f, genl_family_chain(i), family_list)
80 if (strcmp(f->name, name) == 0)
81 return f;
83 return NULL;
86 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
88 struct genl_ops *ops;
90 list_for_each_entry(ops, &family->ops_list, ops_list)
91 if (ops->cmd == cmd)
92 return ops;
94 return NULL;
97 /* Of course we are going to have problems once we hit
98 * 2^16 alive types, but that can only happen by year 2K
100 static inline u16 genl_generate_id(void)
102 static u16 id_gen_idx;
103 int overflowed = 0;
105 do {
106 if (id_gen_idx == 0)
107 id_gen_idx = GENL_MIN_ID;
109 if (++id_gen_idx > GENL_MAX_ID) {
110 if (!overflowed) {
111 overflowed = 1;
112 id_gen_idx = 0;
113 continue;
114 } else
115 return 0;
118 } while (genl_family_find_byid(id_gen_idx));
120 return id_gen_idx;
123 static struct genl_multicast_group notify_grp;
126 * genl_register_mc_group - register a multicast group
128 * Registers the specified multicast group and notifies userspace
129 * about the new group.
131 * Returns 0 on success or a negative error code.
133 * @family: The generic netlink family the group shall be registered for.
134 * @grp: The group to register, must have a name.
136 int genl_register_mc_group(struct genl_family *family,
137 struct genl_multicast_group *grp)
139 int id;
140 unsigned long *new_groups;
141 int err;
143 BUG_ON(grp->name[0] == '\0');
145 genl_lock();
147 /* special-case our own group */
148 if (grp == &notify_grp)
149 id = GENL_ID_CTRL;
150 else
151 id = find_first_zero_bit(mc_groups,
152 mc_groups_longs * BITS_PER_LONG);
155 if (id >= mc_groups_longs * BITS_PER_LONG) {
156 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
158 if (mc_groups == &mc_group_start) {
159 new_groups = kzalloc(nlen, GFP_KERNEL);
160 if (!new_groups) {
161 err = -ENOMEM;
162 goto out;
164 mc_groups = new_groups;
165 *mc_groups = mc_group_start;
166 } else {
167 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
168 if (!new_groups) {
169 err = -ENOMEM;
170 goto out;
172 mc_groups = new_groups;
173 mc_groups[mc_groups_longs] = 0;
175 mc_groups_longs++;
178 err = netlink_change_ngroups(genl_sock,
179 mc_groups_longs * BITS_PER_LONG);
180 if (err)
181 goto out;
183 grp->id = id;
184 set_bit(id, mc_groups);
185 list_add_tail(&grp->list, &family->mcast_groups);
186 grp->family = family;
188 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
189 out:
190 genl_unlock();
191 return err;
193 EXPORT_SYMBOL(genl_register_mc_group);
195 static void __genl_unregister_mc_group(struct genl_family *family,
196 struct genl_multicast_group *grp)
198 BUG_ON(grp->family != family);
199 netlink_clear_multicast_users(genl_sock, grp->id);
200 clear_bit(grp->id, mc_groups);
201 list_del(&grp->list);
202 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
203 grp->id = 0;
204 grp->family = NULL;
208 * genl_unregister_mc_group - unregister a multicast group
210 * Unregisters the specified multicast group and notifies userspace
211 * about it. All current listeners on the group are removed.
213 * Note: It is not necessary to unregister all multicast groups before
214 * unregistering the family, unregistering the family will cause
215 * all assigned multicast groups to be unregistered automatically.
217 * @family: Generic netlink family the group belongs to.
218 * @grp: The group to unregister, must have been registered successfully
219 * previously.
221 void genl_unregister_mc_group(struct genl_family *family,
222 struct genl_multicast_group *grp)
224 genl_lock();
225 __genl_unregister_mc_group(family, grp);
226 genl_unlock();
229 static void genl_unregister_mc_groups(struct genl_family *family)
231 struct genl_multicast_group *grp, *tmp;
233 genl_lock();
234 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
235 __genl_unregister_mc_group(family, grp);
236 genl_unlock();
240 * genl_register_ops - register generic netlink operations
241 * @family: generic netlink family
242 * @ops: operations to be registered
244 * Registers the specified operations and assigns them to the specified
245 * family. Either a doit or dumpit callback must be specified or the
246 * operation will fail. Only one operation structure per command
247 * identifier may be registered.
249 * See include/net/genetlink.h for more documenation on the operations
250 * structure.
252 * Returns 0 on success or a negative error code.
254 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
256 int err = -EINVAL;
258 if (ops->dumpit == NULL && ops->doit == NULL)
259 goto errout;
261 if (genl_get_cmd(ops->cmd, family)) {
262 err = -EEXIST;
263 goto errout;
266 if (ops->dumpit)
267 ops->flags |= GENL_CMD_CAP_DUMP;
268 if (ops->doit)
269 ops->flags |= GENL_CMD_CAP_DO;
270 if (ops->policy)
271 ops->flags |= GENL_CMD_CAP_HASPOL;
273 genl_lock();
274 list_add_tail(&ops->ops_list, &family->ops_list);
275 genl_unlock();
277 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
278 err = 0;
279 errout:
280 return err;
284 * genl_unregister_ops - unregister generic netlink operations
285 * @family: generic netlink family
286 * @ops: operations to be unregistered
288 * Unregisters the specified operations and unassigns them from the
289 * specified family. The operation blocks until the current message
290 * processing has finished and doesn't start again until the
291 * unregister process has finished.
293 * Note: It is not necessary to unregister all operations before
294 * unregistering the family, unregistering the family will cause
295 * all assigned operations to be unregistered automatically.
297 * Returns 0 on success or a negative error code.
299 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
301 struct genl_ops *rc;
303 genl_lock();
304 list_for_each_entry(rc, &family->ops_list, ops_list) {
305 if (rc == ops) {
306 list_del(&ops->ops_list);
307 genl_unlock();
308 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
309 return 0;
312 genl_unlock();
314 return -ENOENT;
318 * genl_register_family - register a generic netlink family
319 * @family: generic netlink family
321 * Registers the specified family after validating it first. Only one
322 * family may be registered with the same family name or identifier.
323 * The family id may equal GENL_ID_GENERATE causing an unique id to
324 * be automatically generated and assigned.
326 * Return 0 on success or a negative error code.
328 int genl_register_family(struct genl_family *family)
330 int err = -EINVAL;
332 if (family->id && family->id < GENL_MIN_ID)
333 goto errout;
335 if (family->id > GENL_MAX_ID)
336 goto errout;
338 INIT_LIST_HEAD(&family->ops_list);
339 INIT_LIST_HEAD(&family->mcast_groups);
341 genl_lock();
343 if (genl_family_find_byname(family->name)) {
344 err = -EEXIST;
345 goto errout_locked;
348 if (genl_family_find_byid(family->id)) {
349 err = -EEXIST;
350 goto errout_locked;
353 if (family->id == GENL_ID_GENERATE) {
354 u16 newid = genl_generate_id();
356 if (!newid) {
357 err = -ENOMEM;
358 goto errout_locked;
361 family->id = newid;
364 if (family->maxattr) {
365 family->attrbuf = kmalloc((family->maxattr+1) *
366 sizeof(struct nlattr *), GFP_KERNEL);
367 if (family->attrbuf == NULL) {
368 err = -ENOMEM;
369 goto errout_locked;
371 } else
372 family->attrbuf = NULL;
374 list_add_tail(&family->family_list, genl_family_chain(family->id));
375 genl_unlock();
377 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
379 return 0;
381 errout_locked:
382 genl_unlock();
383 errout:
384 return err;
388 * genl_unregister_family - unregister generic netlink family
389 * @family: generic netlink family
391 * Unregisters the specified family.
393 * Returns 0 on success or a negative error code.
395 int genl_unregister_family(struct genl_family *family)
397 struct genl_family *rc;
399 genl_unregister_mc_groups(family);
401 genl_lock();
403 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
404 if (family->id != rc->id || strcmp(rc->name, family->name))
405 continue;
407 list_del(&rc->family_list);
408 INIT_LIST_HEAD(&family->ops_list);
409 genl_unlock();
411 kfree(family->attrbuf);
412 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
413 return 0;
416 genl_unlock();
418 return -ENOENT;
421 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
423 struct genl_ops *ops;
424 struct genl_family *family;
425 struct genl_info info;
426 struct genlmsghdr *hdr = nlmsg_data(nlh);
427 int hdrlen, err;
429 family = genl_family_find_byid(nlh->nlmsg_type);
430 if (family == NULL)
431 return -ENOENT;
433 hdrlen = GENL_HDRLEN + family->hdrsize;
434 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
435 return -EINVAL;
437 ops = genl_get_cmd(hdr->cmd, family);
438 if (ops == NULL)
439 return -EOPNOTSUPP;
441 if ((ops->flags & GENL_ADMIN_PERM) &&
442 security_netlink_recv(skb, CAP_NET_ADMIN))
443 return -EPERM;
445 if (nlh->nlmsg_flags & NLM_F_DUMP) {
446 if (ops->dumpit == NULL)
447 return -EOPNOTSUPP;
449 return netlink_dump_start(genl_sock, skb, nlh,
450 ops->dumpit, ops->done);
453 if (ops->doit == NULL)
454 return -EOPNOTSUPP;
456 if (family->attrbuf) {
457 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
458 ops->policy);
459 if (err < 0)
460 return err;
463 info.snd_seq = nlh->nlmsg_seq;
464 info.snd_pid = NETLINK_CB(skb).pid;
465 info.nlhdr = nlh;
466 info.genlhdr = nlmsg_data(nlh);
467 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
468 info.attrs = family->attrbuf;
470 return ops->doit(skb, &info);
473 static void genl_rcv(struct sk_buff *skb)
475 genl_lock();
476 netlink_rcv_skb(skb, &genl_rcv_msg);
477 genl_unlock();
480 /**************************************************************************
481 * Controller
482 **************************************************************************/
484 static struct genl_family genl_ctrl = {
485 .id = GENL_ID_CTRL,
486 .name = "nlctrl",
487 .version = 0x2,
488 .maxattr = CTRL_ATTR_MAX,
491 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
492 u32 flags, struct sk_buff *skb, u8 cmd)
494 void *hdr;
496 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
497 if (hdr == NULL)
498 return -1;
500 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
501 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
502 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
503 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
504 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
506 if (!list_empty(&family->ops_list)) {
507 struct nlattr *nla_ops;
508 struct genl_ops *ops;
509 int idx = 1;
511 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
512 if (nla_ops == NULL)
513 goto nla_put_failure;
515 list_for_each_entry(ops, &family->ops_list, ops_list) {
516 struct nlattr *nest;
518 nest = nla_nest_start(skb, idx++);
519 if (nest == NULL)
520 goto nla_put_failure;
522 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
523 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
525 nla_nest_end(skb, nest);
528 nla_nest_end(skb, nla_ops);
531 if (!list_empty(&family->mcast_groups)) {
532 struct genl_multicast_group *grp;
533 struct nlattr *nla_grps;
534 int idx = 1;
536 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
537 if (nla_grps == NULL)
538 goto nla_put_failure;
540 list_for_each_entry(grp, &family->mcast_groups, list) {
541 struct nlattr *nest;
543 nest = nla_nest_start(skb, idx++);
544 if (nest == NULL)
545 goto nla_put_failure;
547 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
548 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
549 grp->name);
551 nla_nest_end(skb, nest);
553 nla_nest_end(skb, nla_grps);
556 return genlmsg_end(skb, hdr);
558 nla_put_failure:
559 return genlmsg_cancel(skb, hdr);
562 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
563 u32 seq, u32 flags, struct sk_buff *skb,
564 u8 cmd)
566 void *hdr;
567 struct nlattr *nla_grps;
568 struct nlattr *nest;
570 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
571 if (hdr == NULL)
572 return -1;
574 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
575 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
577 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
578 if (nla_grps == NULL)
579 goto nla_put_failure;
581 nest = nla_nest_start(skb, 1);
582 if (nest == NULL)
583 goto nla_put_failure;
585 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
586 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
587 grp->name);
589 nla_nest_end(skb, nest);
590 nla_nest_end(skb, nla_grps);
592 return genlmsg_end(skb, hdr);
594 nla_put_failure:
595 return genlmsg_cancel(skb, hdr);
598 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
601 int i, n = 0;
602 struct genl_family *rt;
603 int chains_to_skip = cb->args[0];
604 int fams_to_skip = cb->args[1];
606 if (chains_to_skip != 0)
607 genl_lock();
609 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
610 if (i < chains_to_skip)
611 continue;
612 n = 0;
613 list_for_each_entry(rt, genl_family_chain(i), family_list) {
614 if (++n < fams_to_skip)
615 continue;
616 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
617 cb->nlh->nlmsg_seq, NLM_F_MULTI,
618 skb, CTRL_CMD_NEWFAMILY) < 0)
619 goto errout;
622 fams_to_skip = 0;
625 errout:
626 if (chains_to_skip != 0)
627 genl_unlock();
629 cb->args[0] = i;
630 cb->args[1] = n;
632 return skb->len;
635 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
636 u32 pid, int seq, u8 cmd)
638 struct sk_buff *skb;
639 int err;
641 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
642 if (skb == NULL)
643 return ERR_PTR(-ENOBUFS);
645 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
646 if (err < 0) {
647 nlmsg_free(skb);
648 return ERR_PTR(err);
651 return skb;
654 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
655 u32 pid, int seq, u8 cmd)
657 struct sk_buff *skb;
658 int err;
660 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
661 if (skb == NULL)
662 return ERR_PTR(-ENOBUFS);
664 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
665 if (err < 0) {
666 nlmsg_free(skb);
667 return ERR_PTR(err);
670 return skb;
673 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
674 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
675 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
676 .len = GENL_NAMSIZ - 1 },
679 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
681 struct sk_buff *msg;
682 struct genl_family *res = NULL;
683 int err = -EINVAL;
685 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
686 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
687 res = genl_family_find_byid(id);
690 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
691 char *name;
693 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
694 res = genl_family_find_byname(name);
697 if (res == NULL) {
698 err = -ENOENT;
699 goto errout;
702 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
703 CTRL_CMD_NEWFAMILY);
704 if (IS_ERR(msg)) {
705 err = PTR_ERR(msg);
706 goto errout;
709 err = genlmsg_reply(msg, info);
710 errout:
711 return err;
714 static int genl_ctrl_event(int event, void *data)
716 struct sk_buff *msg;
718 if (genl_sock == NULL)
719 return 0;
721 switch (event) {
722 case CTRL_CMD_NEWFAMILY:
723 case CTRL_CMD_DELFAMILY:
724 msg = ctrl_build_family_msg(data, 0, 0, event);
725 if (IS_ERR(msg))
726 return PTR_ERR(msg);
728 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
729 break;
730 case CTRL_CMD_NEWMCAST_GRP:
731 case CTRL_CMD_DELMCAST_GRP:
732 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
733 if (IS_ERR(msg))
734 return PTR_ERR(msg);
736 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
737 break;
740 return 0;
743 static struct genl_ops genl_ctrl_ops = {
744 .cmd = CTRL_CMD_GETFAMILY,
745 .doit = ctrl_getfamily,
746 .dumpit = ctrl_dumpfamily,
747 .policy = ctrl_policy,
750 static struct genl_multicast_group notify_grp = {
751 .name = "notify",
754 static int __init genl_init(void)
756 int i, err;
758 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
759 INIT_LIST_HEAD(&family_ht[i]);
761 err = genl_register_family(&genl_ctrl);
762 if (err < 0)
763 goto errout;
765 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
766 if (err < 0)
767 goto errout_register;
769 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
771 /* we'll bump the group number right afterwards */
772 genl_sock = netlink_kernel_create(&init_net, NETLINK_GENERIC, 0,
773 genl_rcv, NULL, THIS_MODULE);
774 if (genl_sock == NULL)
775 panic("GENL: Cannot initialize generic netlink\n");
777 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
778 if (err < 0)
779 goto errout_register;
781 return 0;
783 errout_register:
784 genl_unregister_family(&genl_ctrl);
785 errout:
786 panic("GENL: Cannot register controller: %d\n", err);
789 subsys_initcall(genl_init);
791 EXPORT_SYMBOL(genl_sock);
792 EXPORT_SYMBOL(genl_register_ops);
793 EXPORT_SYMBOL(genl_unregister_ops);
794 EXPORT_SYMBOL(genl_register_family);
795 EXPORT_SYMBOL(genl_unregister_family);