allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / netlink / genetlink.c
blobfb9d5ae2dfdbd6a2c8a4a2312dd3376d0fcf436b
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 void genl_lock(void)
27 mutex_lock(&genl_mutex);
30 static void genl_unlock(void)
32 mutex_unlock(&genl_mutex);
34 if (genl_sock && genl_sock->sk_receive_queue.qlen)
35 genl_sock->sk_data_ready(genl_sock, 0);
38 #define GENL_FAM_TAB_SIZE 16
39 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
41 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
43 * Bitmap of multicast groups that are currently in use.
45 * To avoid an allocation at boot of just one unsigned long,
46 * declare it global instead.
47 * Bit 0 is marked as already used since group 0 is invalid.
49 static unsigned long mc_group_start = 0x1;
50 static unsigned long *mc_groups = &mc_group_start;
51 static unsigned long mc_groups_longs = 1;
53 static int genl_ctrl_event(int event, void *data);
55 static inline unsigned int genl_family_hash(unsigned int id)
57 return id & GENL_FAM_TAB_MASK;
60 static inline struct list_head *genl_family_chain(unsigned int id)
62 return &family_ht[genl_family_hash(id)];
65 static struct genl_family *genl_family_find_byid(unsigned int id)
67 struct genl_family *f;
69 list_for_each_entry(f, genl_family_chain(id), family_list)
70 if (f->id == id)
71 return f;
73 return NULL;
76 static struct genl_family *genl_family_find_byname(char *name)
78 struct genl_family *f;
79 int i;
81 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
82 list_for_each_entry(f, genl_family_chain(i), family_list)
83 if (strcmp(f->name, name) == 0)
84 return f;
86 return NULL;
89 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
91 struct genl_ops *ops;
93 list_for_each_entry(ops, &family->ops_list, ops_list)
94 if (ops->cmd == cmd)
95 return ops;
97 return NULL;
100 /* Of course we are going to have problems once we hit
101 * 2^16 alive types, but that can only happen by year 2K
103 static inline u16 genl_generate_id(void)
105 static u16 id_gen_idx;
106 int overflowed = 0;
108 do {
109 if (id_gen_idx == 0)
110 id_gen_idx = GENL_MIN_ID;
112 if (++id_gen_idx > GENL_MAX_ID) {
113 if (!overflowed) {
114 overflowed = 1;
115 id_gen_idx = 0;
116 continue;
117 } else
118 return 0;
121 } while (genl_family_find_byid(id_gen_idx));
123 return id_gen_idx;
126 static struct genl_multicast_group notify_grp;
129 * genl_register_mc_group - register a multicast group
131 * Registers the specified multicast group and notifies userspace
132 * about the new group.
134 * Returns 0 on success or a negative error code.
136 * @family: The generic netlink family the group shall be registered for.
137 * @grp: The group to register, must have a name.
139 int genl_register_mc_group(struct genl_family *family,
140 struct genl_multicast_group *grp)
142 int id;
143 unsigned long *new_groups;
144 int err;
146 BUG_ON(grp->name[0] == '\0');
148 genl_lock();
150 /* special-case our own group */
151 if (grp == &notify_grp)
152 id = GENL_ID_CTRL;
153 else
154 id = find_first_zero_bit(mc_groups,
155 mc_groups_longs * BITS_PER_LONG);
158 if (id >= mc_groups_longs * BITS_PER_LONG) {
159 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
161 if (mc_groups == &mc_group_start) {
162 new_groups = kzalloc(nlen, GFP_KERNEL);
163 if (!new_groups) {
164 err = -ENOMEM;
165 goto out;
167 mc_groups = new_groups;
168 *mc_groups = mc_group_start;
169 } else {
170 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
171 if (!new_groups) {
172 err = -ENOMEM;
173 goto out;
175 mc_groups = new_groups;
176 mc_groups[mc_groups_longs] = 0;
178 mc_groups_longs++;
181 err = netlink_change_ngroups(genl_sock,
182 mc_groups_longs * BITS_PER_LONG);
183 if (err)
184 goto out;
186 grp->id = id;
187 set_bit(id, mc_groups);
188 list_add_tail(&grp->list, &family->mcast_groups);
189 grp->family = family;
191 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
192 out:
193 genl_unlock();
194 return 0;
196 EXPORT_SYMBOL(genl_register_mc_group);
198 static void __genl_unregister_mc_group(struct genl_family *family,
199 struct genl_multicast_group *grp)
201 BUG_ON(grp->family != family);
202 netlink_clear_multicast_users(genl_sock, grp->id);
203 clear_bit(grp->id, mc_groups);
204 list_del(&grp->list);
205 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
206 grp->id = 0;
207 grp->family = NULL;
211 * genl_unregister_mc_group - unregister a multicast group
213 * Unregisters the specified multicast group and notifies userspace
214 * about it. All current listeners on the group are removed.
216 * Note: It is not necessary to unregister all multicast groups before
217 * unregistering the family, unregistering the family will cause
218 * all assigned multicast groups to be unregistered automatically.
220 * @family: Generic netlink family the group belongs to.
221 * @grp: The group to unregister, must have been registered successfully
222 * previously.
224 void genl_unregister_mc_group(struct genl_family *family,
225 struct genl_multicast_group *grp)
227 genl_lock();
228 __genl_unregister_mc_group(family, grp);
229 genl_unlock();
231 EXPORT_SYMBOL(genl_unregister_mc_group);
233 static void genl_unregister_mc_groups(struct genl_family *family)
235 struct genl_multicast_group *grp, *tmp;
237 genl_lock();
238 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
239 __genl_unregister_mc_group(family, grp);
240 genl_unlock();
244 * genl_register_ops - register generic netlink operations
245 * @family: generic netlink family
246 * @ops: operations to be registered
248 * Registers the specified operations and assigns them to the specified
249 * family. Either a doit or dumpit callback must be specified or the
250 * operation will fail. Only one operation structure per command
251 * identifier may be registered.
253 * See include/net/genetlink.h for more documenation on the operations
254 * structure.
256 * Returns 0 on success or a negative error code.
258 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
260 int err = -EINVAL;
262 if (ops->dumpit == NULL && ops->doit == NULL)
263 goto errout;
265 if (genl_get_cmd(ops->cmd, family)) {
266 err = -EEXIST;
267 goto errout;
270 if (ops->dumpit)
271 ops->flags |= GENL_CMD_CAP_DUMP;
272 if (ops->doit)
273 ops->flags |= GENL_CMD_CAP_DO;
274 if (ops->policy)
275 ops->flags |= GENL_CMD_CAP_HASPOL;
277 genl_lock();
278 list_add_tail(&ops->ops_list, &family->ops_list);
279 genl_unlock();
281 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
282 err = 0;
283 errout:
284 return err;
288 * genl_unregister_ops - unregister generic netlink operations
289 * @family: generic netlink family
290 * @ops: operations to be unregistered
292 * Unregisters the specified operations and unassigns them from the
293 * specified family. The operation blocks until the current message
294 * processing has finished and doesn't start again until the
295 * unregister process has finished.
297 * Note: It is not necessary to unregister all operations before
298 * unregistering the family, unregistering the family will cause
299 * all assigned operations to be unregistered automatically.
301 * Returns 0 on success or a negative error code.
303 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
305 struct genl_ops *rc;
307 genl_lock();
308 list_for_each_entry(rc, &family->ops_list, ops_list) {
309 if (rc == ops) {
310 list_del(&ops->ops_list);
311 genl_unlock();
312 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
313 return 0;
316 genl_unlock();
318 return -ENOENT;
322 * genl_register_family - register a generic netlink family
323 * @family: generic netlink family
325 * Registers the specified family after validating it first. Only one
326 * family may be registered with the same family name or identifier.
327 * The family id may equal GENL_ID_GENERATE causing an unique id to
328 * be automatically generated and assigned.
330 * Return 0 on success or a negative error code.
332 int genl_register_family(struct genl_family *family)
334 int err = -EINVAL;
336 if (family->id && family->id < GENL_MIN_ID)
337 goto errout;
339 if (family->id > GENL_MAX_ID)
340 goto errout;
342 INIT_LIST_HEAD(&family->ops_list);
343 INIT_LIST_HEAD(&family->mcast_groups);
345 genl_lock();
347 if (genl_family_find_byname(family->name)) {
348 err = -EEXIST;
349 goto errout_locked;
352 if (genl_family_find_byid(family->id)) {
353 err = -EEXIST;
354 goto errout_locked;
357 if (family->id == GENL_ID_GENERATE) {
358 u16 newid = genl_generate_id();
360 if (!newid) {
361 err = -ENOMEM;
362 goto errout_locked;
365 family->id = newid;
368 if (family->maxattr) {
369 family->attrbuf = kmalloc((family->maxattr+1) *
370 sizeof(struct nlattr *), GFP_KERNEL);
371 if (family->attrbuf == NULL) {
372 err = -ENOMEM;
373 goto errout_locked;
375 } else
376 family->attrbuf = NULL;
378 list_add_tail(&family->family_list, genl_family_chain(family->id));
379 genl_unlock();
381 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
383 return 0;
385 errout_locked:
386 genl_unlock();
387 errout:
388 return err;
392 * genl_unregister_family - unregister generic netlink family
393 * @family: generic netlink family
395 * Unregisters the specified family.
397 * Returns 0 on success or a negative error code.
399 int genl_unregister_family(struct genl_family *family)
401 struct genl_family *rc;
403 genl_unregister_mc_groups(family);
405 genl_lock();
407 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
408 if (family->id != rc->id || strcmp(rc->name, family->name))
409 continue;
411 list_del(&rc->family_list);
412 INIT_LIST_HEAD(&family->ops_list);
413 genl_unlock();
415 kfree(family->attrbuf);
416 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
417 return 0;
420 genl_unlock();
422 return -ENOENT;
425 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
427 struct genl_ops *ops;
428 struct genl_family *family;
429 struct genl_info info;
430 struct genlmsghdr *hdr = nlmsg_data(nlh);
431 int hdrlen, err;
433 family = genl_family_find_byid(nlh->nlmsg_type);
434 if (family == NULL)
435 return -ENOENT;
437 hdrlen = GENL_HDRLEN + family->hdrsize;
438 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
439 return -EINVAL;
441 ops = genl_get_cmd(hdr->cmd, family);
442 if (ops == NULL)
443 return -EOPNOTSUPP;
445 if ((ops->flags & GENL_ADMIN_PERM) &&
446 security_netlink_recv(skb, CAP_NET_ADMIN))
447 return -EPERM;
449 if (nlh->nlmsg_flags & NLM_F_DUMP) {
450 if (ops->dumpit == NULL)
451 return -EOPNOTSUPP;
453 return netlink_dump_start(genl_sock, skb, nlh,
454 ops->dumpit, ops->done);
457 if (ops->doit == NULL)
458 return -EOPNOTSUPP;
460 if (family->attrbuf) {
461 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
462 ops->policy);
463 if (err < 0)
464 return err;
467 info.snd_seq = nlh->nlmsg_seq;
468 info.snd_pid = NETLINK_CB(skb).pid;
469 info.nlhdr = nlh;
470 info.genlhdr = nlmsg_data(nlh);
471 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
472 info.attrs = family->attrbuf;
474 return ops->doit(skb, &info);
477 static void genl_rcv(struct sk_buff *skb)
479 genl_lock();
480 netlink_rcv_skb(skb, &genl_rcv_msg);
481 genl_unlock();
484 /**************************************************************************
485 * Controller
486 **************************************************************************/
488 static struct genl_family genl_ctrl = {
489 .id = GENL_ID_CTRL,
490 .name = "nlctrl",
491 .version = 0x2,
492 .maxattr = CTRL_ATTR_MAX,
495 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
496 u32 flags, struct sk_buff *skb, u8 cmd)
498 void *hdr;
500 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
501 if (hdr == NULL)
502 return -1;
504 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
505 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
506 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
507 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
508 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
510 if (!list_empty(&family->ops_list)) {
511 struct nlattr *nla_ops;
512 struct genl_ops *ops;
513 int idx = 1;
515 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
516 if (nla_ops == NULL)
517 goto nla_put_failure;
519 list_for_each_entry(ops, &family->ops_list, ops_list) {
520 struct nlattr *nest;
522 nest = nla_nest_start(skb, idx++);
523 if (nest == NULL)
524 goto nla_put_failure;
526 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
527 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
529 nla_nest_end(skb, nest);
532 nla_nest_end(skb, nla_ops);
535 if (!list_empty(&family->mcast_groups)) {
536 struct genl_multicast_group *grp;
537 struct nlattr *nla_grps;
538 int idx = 1;
540 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
541 if (nla_grps == NULL)
542 goto nla_put_failure;
544 list_for_each_entry(grp, &family->mcast_groups, list) {
545 struct nlattr *nest;
547 nest = nla_nest_start(skb, idx++);
548 if (nest == NULL)
549 goto nla_put_failure;
551 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
552 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
553 grp->name);
555 nla_nest_end(skb, nest);
557 nla_nest_end(skb, nla_grps);
560 return genlmsg_end(skb, hdr);
562 nla_put_failure:
563 return genlmsg_cancel(skb, hdr);
566 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
567 u32 seq, u32 flags, struct sk_buff *skb,
568 u8 cmd)
570 void *hdr;
571 struct nlattr *nla_grps;
572 struct nlattr *nest;
574 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
575 if (hdr == NULL)
576 return -1;
578 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
579 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
581 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
582 if (nla_grps == NULL)
583 goto nla_put_failure;
585 nest = nla_nest_start(skb, 1);
586 if (nest == NULL)
587 goto nla_put_failure;
589 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
590 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
591 grp->name);
593 nla_nest_end(skb, nest);
594 nla_nest_end(skb, nla_grps);
596 return genlmsg_end(skb, hdr);
598 nla_put_failure:
599 return genlmsg_cancel(skb, hdr);
602 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
605 int i, n = 0;
606 struct genl_family *rt;
607 int chains_to_skip = cb->args[0];
608 int fams_to_skip = cb->args[1];
610 if (chains_to_skip != 0)
611 genl_lock();
613 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
614 if (i < chains_to_skip)
615 continue;
616 n = 0;
617 list_for_each_entry(rt, genl_family_chain(i), family_list) {
618 if (++n < fams_to_skip)
619 continue;
620 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
621 cb->nlh->nlmsg_seq, NLM_F_MULTI,
622 skb, CTRL_CMD_NEWFAMILY) < 0)
623 goto errout;
626 fams_to_skip = 0;
629 errout:
630 if (chains_to_skip != 0)
631 genl_unlock();
633 cb->args[0] = i;
634 cb->args[1] = n;
636 return skb->len;
639 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
640 u32 pid, int seq, u8 cmd)
642 struct sk_buff *skb;
643 int err;
645 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
646 if (skb == NULL)
647 return ERR_PTR(-ENOBUFS);
649 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
650 if (err < 0) {
651 nlmsg_free(skb);
652 return ERR_PTR(err);
655 return skb;
658 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
659 u32 pid, int seq, u8 cmd)
661 struct sk_buff *skb;
662 int err;
664 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
665 if (skb == NULL)
666 return ERR_PTR(-ENOBUFS);
668 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
669 if (err < 0) {
670 nlmsg_free(skb);
671 return ERR_PTR(err);
674 return skb;
677 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
678 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
679 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
680 .len = GENL_NAMSIZ - 1 },
683 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
685 struct sk_buff *msg;
686 struct genl_family *res = NULL;
687 int err = -EINVAL;
689 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
690 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
691 res = genl_family_find_byid(id);
694 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
695 char *name;
697 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
698 res = genl_family_find_byname(name);
701 if (res == NULL) {
702 err = -ENOENT;
703 goto errout;
706 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
707 CTRL_CMD_NEWFAMILY);
708 if (IS_ERR(msg)) {
709 err = PTR_ERR(msg);
710 goto errout;
713 err = genlmsg_reply(msg, info);
714 errout:
715 return err;
718 static int genl_ctrl_event(int event, void *data)
720 struct sk_buff *msg;
722 if (genl_sock == NULL)
723 return 0;
725 switch (event) {
726 case CTRL_CMD_NEWFAMILY:
727 case CTRL_CMD_DELFAMILY:
728 msg = ctrl_build_family_msg(data, 0, 0, event);
729 if (IS_ERR(msg))
730 return PTR_ERR(msg);
732 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
733 break;
734 case CTRL_CMD_NEWMCAST_GRP:
735 case CTRL_CMD_DELMCAST_GRP:
736 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
737 if (IS_ERR(msg))
738 return PTR_ERR(msg);
740 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
741 break;
744 return 0;
747 static struct genl_ops genl_ctrl_ops = {
748 .cmd = CTRL_CMD_GETFAMILY,
749 .doit = ctrl_getfamily,
750 .dumpit = ctrl_dumpfamily,
751 .policy = ctrl_policy,
754 static struct genl_multicast_group notify_grp = {
755 .name = "notify",
758 static int __init genl_init(void)
760 int i, err;
762 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
763 INIT_LIST_HEAD(&family_ht[i]);
765 err = genl_register_family(&genl_ctrl);
766 if (err < 0)
767 goto errout;
769 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
770 if (err < 0)
771 goto errout_register;
773 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
775 /* we'll bump the group number right afterwards */
776 genl_sock = netlink_kernel_create(NETLINK_GENERIC, 0, genl_rcv,
777 NULL, THIS_MODULE);
778 if (genl_sock == NULL)
779 panic("GENL: Cannot initialize generic netlink\n");
781 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
782 if (err < 0)
783 goto errout_register;
785 return 0;
787 errout_register:
788 genl_unregister_family(&genl_ctrl);
789 errout:
790 panic("GENL: Cannot register controller: %d\n", err);
793 subsys_initcall(genl_init);
795 EXPORT_SYMBOL(genl_sock);
796 EXPORT_SYMBOL(genl_register_ops);
797 EXPORT_SYMBOL(genl_unregister_ops);
798 EXPORT_SYMBOL(genl_register_family);
799 EXPORT_SYMBOL(genl_unregister_family);