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>
18 #include <linux/bitmap.h>
37 int lockdep_genl_is_held(
void)
39 return lockdep_is_held(&genl_mutex);
44 #define GENL_FAM_TAB_SIZE 16
45 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
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)
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)
109 static
u16 genl_generate_id(
void)
115 if (!genl_family_find_byid(id_gen_idx))
141 unsigned long *new_groups;
149 if (grp == ¬ify_grp)
156 if (
id >= mc_groups_longs * BITS_PER_LONG) {
157 size_t nlen = (mc_groups_longs + 1) *
sizeof(
unsigned long);
159 if (mc_groups == &mc_group_start) {
165 mc_groups = new_groups;
166 *mc_groups = mc_group_start;
173 mc_groups = new_groups;
174 mc_groups[mc_groups_longs] = 0;
186 mc_groups_longs * BITS_PER_LONG);
203 mc_groups_longs * BITS_PER_LONG);
220 static void __genl_unregister_mc_group(
struct genl_family *family,
258 __genl_unregister_mc_group(family, grp);
263 static void genl_unregister_mc_groups(
struct genl_family *family)
268 __genl_unregister_mc_group(family, grp);
290 if (ops->dumpit == NULL && ops->doit == NULL)
293 if (genl_get_cmd(ops->cmd, family)) {
366 if (family->
id && family->
id < GENL_MIN_ID)
377 if (genl_family_find_byname(family->
name)) {
383 u16 newid = genl_generate_id();
391 }
else if (genl_family_find_byid(family->
id)) {
454 for (i = 0; i < n_ops; ++
i, ++ops) {
480 genl_unregister_mc_groups(family);
523 hdr = nlmsg_data(nlh);
536 struct net *net = sock_net(skb->
sk);
541 family = genl_family_find_byid(nlh->
nlmsg_type);
550 if (nlh->
nlmsg_len < nlmsg_msg_size(hdrlen))
553 ops = genl_get_cmd(hdr->
cmd, family);
571 err = netlink_dump_start(net->
genl_sock, skb, nlh, &c);
577 if (ops->
doit == NULL)
590 info.genlhdr = nlmsg_data(nlh);
593 genl_info_net_set(&
info, net);
610 static void genl_rcv(
struct sk_buff *skb)
634 hdr =
genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
643 goto nla_put_failure;
645 if (!list_empty(&family->
ops_list)) {
652 goto nla_put_failure;
657 nest = nla_nest_start(skb, idx++);
659 goto nla_put_failure;
663 goto nla_put_failure;
665 nla_nest_end(skb, nest);
668 nla_nest_end(skb, nla_ops);
677 if (nla_grps == NULL)
678 goto nla_put_failure;
683 nest = nla_nest_start(skb, idx++);
685 goto nla_put_failure;
690 goto nla_put_failure;
692 nla_nest_end(skb, nest);
694 nla_nest_end(skb, nla_grps);
697 return genlmsg_end(skb, hdr);
700 genlmsg_cancel(skb, hdr);
712 hdr =
genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
718 goto nla_put_failure;
721 if (nla_grps == NULL)
722 goto nla_put_failure;
724 nest = nla_nest_start(skb, 1);
726 goto nla_put_failure;
731 goto nla_put_failure;
733 nla_nest_end(skb, nest);
734 nla_nest_end(skb, nla_grps);
736 return genlmsg_end(skb, hdr);
739 genlmsg_cancel(skb, hdr);
748 struct net *net = sock_net(skb->
sk);
749 int chains_to_skip = cb->
args[0];
750 int fams_to_skip = cb->
args[1];
757 if (++n < fams_to_skip)
776 u32 portid,
int seq,
u8 cmd)
785 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
795 u32 portid,
int seq,
u8 cmd)
804 err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
827 res = genl_family_find_byid(
id);
835 res = genl_family_find_byname(name);
836 #ifdef CONFIG_MODULES
839 request_module(
"net-pf-%d-proto-%d-family-%s",
842 res = genl_family_find_byname(name);
861 return genlmsg_reply(msg, info);
864 static int genl_ctrl_event(
int event,
void *
data)
878 msg = ctrl_build_family_msg(family, 0, 0, event);
884 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
894 genlmsg_multicast_netns(&
init_net, msg, 0,
905 static struct genl_ops genl_ctrl_ops = {
907 .doit = ctrl_getfamily,
908 .dumpit = ctrl_dumpfamily,
909 .policy = ctrl_policy,
916 static int __net_init genl_pernet_init(
struct net *net)
920 .cb_mutex = &genl_mutex,
928 panic(
"GENL: Cannot initialize generic netlink\n");
936 static void __net_exit genl_pernet_exit(
struct net *net)
943 .init = genl_pernet_init,
944 .exit = genl_pernet_exit,
947 static int __init genl_init(
void)
952 INIT_LIST_HEAD(&family_ht[i]);
969 panic(
"GENL: Cannot register controller: %d\n", err);
974 static int genlmsg_mcast(
struct sk_buff *skb,
u32 portid,
unsigned long group,
988 err = nlmsg_multicast(prev->
genl_sock, tmp,
989 portid, group, flags);
997 return nlmsg_multicast(prev->
genl_sock, skb, portid, group, flags);
1006 return genlmsg_mcast(skb, portid, group, flags);
1017 report = nlmsg_report(nlh);