1 // SPDX-License-Identifier: GPL-2.0
3 * NETLINK Generic Netlink Family
5 * Authors: Jamal Hadi Salim
6 * Thomas Graf <tgraf@suug.ch>
7 * Johannes Berg <johannes@sipsolutions.net>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/socket.h>
16 #include <linux/string.h>
17 #include <linux/skbuff.h>
18 #include <linux/mutex.h>
19 #include <linux/bitmap.h>
20 #include <linux/rwsem.h>
21 #include <linux/idr.h>
23 #include <net/genetlink.h>
25 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
26 static DECLARE_RWSEM(cb_lock);
28 atomic_t genl_sk_destructing_cnt = ATOMIC_INIT(0);
29 DECLARE_WAIT_QUEUE_HEAD(genl_sk_destructing_waitq);
33 mutex_lock(&genl_mutex);
35 EXPORT_SYMBOL(genl_lock);
37 void genl_unlock(void)
39 mutex_unlock(&genl_mutex);
41 EXPORT_SYMBOL(genl_unlock);
43 static void genl_lock_all(void)
49 static void genl_unlock_all(void)
55 static DEFINE_IDR(genl_fam_idr);
58 * Bitmap of multicast groups that are currently in use.
60 * To avoid an allocation at boot of just one unsigned long,
61 * declare it global instead.
62 * Bit 0 is marked as already used since group 0 is invalid.
63 * Bit 1 is marked as already used since the drop-monitor code
64 * abuses the API and thinks it can statically use group 1.
65 * That group will typically conflict with other groups that
66 * any proper users use.
67 * Bit 16 is marked as used since it's used for generic netlink
68 * and the code no longer marks pre-reserved IDs as used.
69 * Bit 17 is marked as already used since the VFS quota code
70 * also abused this API and relied on family == group ID, we
71 * cater to that by giving it a static family and group ID.
72 * Bit 18 is marked as already used since the PMCRAID driver
73 * did the same thing as the VFS quota code (maybe copied?)
75 static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) |
76 BIT(GENL_ID_VFS_DQUOT) |
78 static unsigned long *mc_groups = &mc_group_start;
79 static unsigned long mc_groups_longs = 1;
81 static int genl_ctrl_event(int event, const struct genl_family *family,
82 const struct genl_multicast_group *grp,
85 static const struct genl_family *genl_family_find_byid(unsigned int id)
87 return idr_find(&genl_fam_idr, id);
90 static const struct genl_family *genl_family_find_byname(char *name)
92 const struct genl_family *family;
95 idr_for_each_entry(&genl_fam_idr, family, id)
96 if (strcmp(family->name, name) == 0)
102 static int genl_get_cmd_cnt(const struct genl_family *family)
104 return family->n_ops + family->n_small_ops;
107 static void genl_op_from_full(const struct genl_family *family,
108 unsigned int i, struct genl_ops *op)
110 *op = family->ops[i];
113 op->maxattr = family->maxattr;
115 op->policy = family->policy;
118 static int genl_get_cmd_full(u32 cmd, const struct genl_family *family,
123 for (i = 0; i < family->n_ops; i++)
124 if (family->ops[i].cmd == cmd) {
125 genl_op_from_full(family, i, op);
132 static void genl_op_from_small(const struct genl_family *family,
133 unsigned int i, struct genl_ops *op)
135 memset(op, 0, sizeof(*op));
136 op->doit = family->small_ops[i].doit;
137 op->dumpit = family->small_ops[i].dumpit;
138 op->cmd = family->small_ops[i].cmd;
139 op->internal_flags = family->small_ops[i].internal_flags;
140 op->flags = family->small_ops[i].flags;
141 op->validate = family->small_ops[i].validate;
143 op->maxattr = family->maxattr;
144 op->policy = family->policy;
147 static int genl_get_cmd_small(u32 cmd, const struct genl_family *family,
152 for (i = 0; i < family->n_small_ops; i++)
153 if (family->small_ops[i].cmd == cmd) {
154 genl_op_from_small(family, i, op);
161 static int genl_get_cmd(u32 cmd, const struct genl_family *family,
164 if (!genl_get_cmd_full(cmd, family, op))
166 return genl_get_cmd_small(cmd, family, op);
169 static void genl_get_cmd_by_index(unsigned int i,
170 const struct genl_family *family,
173 if (i < family->n_ops)
174 genl_op_from_full(family, i, op);
175 else if (i < family->n_ops + family->n_small_ops)
176 genl_op_from_small(family, i - family->n_ops, op);
181 static int genl_allocate_reserve_groups(int n_groups, int *first_id)
183 unsigned long *new_groups;
191 id = find_first_zero_bit(mc_groups,
195 id = find_next_zero_bit(mc_groups,
196 mc_groups_longs * BITS_PER_LONG,
201 i < min_t(int, id + n_groups,
202 mc_groups_longs * BITS_PER_LONG);
204 if (test_bit(i, mc_groups)) {
211 if (id + n_groups > mc_groups_longs * BITS_PER_LONG) {
212 unsigned long new_longs = mc_groups_longs +
213 BITS_TO_LONGS(n_groups);
214 size_t nlen = new_longs * sizeof(unsigned long);
216 if (mc_groups == &mc_group_start) {
217 new_groups = kzalloc(nlen, GFP_KERNEL);
220 mc_groups = new_groups;
221 *mc_groups = mc_group_start;
223 new_groups = krealloc(mc_groups, nlen,
227 mc_groups = new_groups;
228 for (i = 0; i < BITS_TO_LONGS(n_groups); i++)
229 mc_groups[mc_groups_longs + i] = 0;
231 mc_groups_longs = new_longs;
235 for (i = id; i < id + n_groups; i++)
236 set_bit(i, mc_groups);
241 static struct genl_family genl_ctrl;
243 static int genl_validate_assign_mc_groups(struct genl_family *family)
246 int n_groups = family->n_mcgrps;
248 bool groups_allocated = false;
253 for (i = 0; i < n_groups; i++) {
254 const struct genl_multicast_group *grp = &family->mcgrps[i];
256 if (WARN_ON(grp->name[0] == '\0'))
258 if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL))
262 /* special-case our own group and hacks */
263 if (family == &genl_ctrl) {
264 first_id = GENL_ID_CTRL;
265 BUG_ON(n_groups != 1);
266 } else if (strcmp(family->name, "NET_DM") == 0) {
268 BUG_ON(n_groups != 1);
269 } else if (family->id == GENL_ID_VFS_DQUOT) {
270 first_id = GENL_ID_VFS_DQUOT;
271 BUG_ON(n_groups != 1);
272 } else if (family->id == GENL_ID_PMCRAID) {
273 first_id = GENL_ID_PMCRAID;
274 BUG_ON(n_groups != 1);
276 groups_allocated = true;
277 err = genl_allocate_reserve_groups(n_groups, &first_id);
282 family->mcgrp_offset = first_id;
284 /* if still initializing, can't and don't need to realloc bitmaps */
285 if (!init_net.genl_sock)
288 if (family->netnsok) {
291 netlink_table_grab();
293 for_each_net_rcu(net) {
294 err = __netlink_change_ngroups(net->genl_sock,
295 mc_groups_longs * BITS_PER_LONG);
298 * No need to roll back, can only fail if
299 * memory allocation fails and then the
300 * number of _possible_ groups has been
301 * increased on some sockets which is ok.
307 netlink_table_ungrab();
309 err = netlink_change_ngroups(init_net.genl_sock,
310 mc_groups_longs * BITS_PER_LONG);
313 if (groups_allocated && err) {
314 for (i = 0; i < family->n_mcgrps; i++)
315 clear_bit(family->mcgrp_offset + i, mc_groups);
321 static void genl_unregister_mc_groups(const struct genl_family *family)
326 netlink_table_grab();
328 for_each_net_rcu(net) {
329 for (i = 0; i < family->n_mcgrps; i++)
330 __netlink_clear_multicast_users(
331 net->genl_sock, family->mcgrp_offset + i);
334 netlink_table_ungrab();
336 for (i = 0; i < family->n_mcgrps; i++) {
337 int grp_id = family->mcgrp_offset + i;
340 clear_bit(grp_id, mc_groups);
341 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family,
342 &family->mcgrps[i], grp_id);
346 static int genl_validate_ops(const struct genl_family *family)
350 if (WARN_ON(family->n_ops && !family->ops) ||
351 WARN_ON(family->n_small_ops && !family->small_ops))
354 for (i = 0; i < genl_get_cmd_cnt(family); i++) {
357 genl_get_cmd_by_index(i, family, &op);
358 if (op.dumpit == NULL && op.doit == NULL)
360 for (j = i + 1; j < genl_get_cmd_cnt(family); j++) {
363 genl_get_cmd_by_index(j, family, &op2);
364 if (op.cmd == op2.cmd)
373 * genl_register_family - register a generic netlink family
374 * @family: generic netlink family
376 * Registers the specified family after validating it first. Only one
377 * family may be registered with the same family name or identifier.
379 * The family's ops, multicast groups and module pointer must already
382 * Return 0 on success or a negative error code.
384 int genl_register_family(struct genl_family *family)
387 int start = GENL_START_ALLOC, end = GENL_MAX_ID;
389 err = genl_validate_ops(family);
395 if (genl_family_find_byname(family->name)) {
401 * Sadly, a few cases need to be special-cased
402 * due to them having previously abused the API
403 * and having used their family ID also as their
404 * multicast group ID, so we use reserved IDs
405 * for both to be sure we can do that mapping.
407 if (family == &genl_ctrl) {
408 /* and this needs to be special for initial family lookups */
409 start = end = GENL_ID_CTRL;
410 } else if (strcmp(family->name, "pmcraid") == 0) {
411 start = end = GENL_ID_PMCRAID;
412 } else if (strcmp(family->name, "VFS_DQUOT") == 0) {
413 start = end = GENL_ID_VFS_DQUOT;
416 family->id = idr_alloc_cyclic(&genl_fam_idr, family,
417 start, end + 1, GFP_KERNEL);
418 if (family->id < 0) {
423 err = genl_validate_assign_mc_groups(family);
429 /* send all events */
430 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
431 for (i = 0; i < family->n_mcgrps; i++)
432 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
433 &family->mcgrps[i], family->mcgrp_offset + i);
438 idr_remove(&genl_fam_idr, family->id);
443 EXPORT_SYMBOL(genl_register_family);
446 * genl_unregister_family - unregister generic netlink family
447 * @family: generic netlink family
449 * Unregisters the specified family.
451 * Returns 0 on success or a negative error code.
453 int genl_unregister_family(const struct genl_family *family)
457 if (!genl_family_find_byid(family->id)) {
462 genl_unregister_mc_groups(family);
464 idr_remove(&genl_fam_idr, family->id);
467 wait_event(genl_sk_destructing_waitq,
468 atomic_read(&genl_sk_destructing_cnt) == 0);
471 genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
475 EXPORT_SYMBOL(genl_unregister_family);
478 * genlmsg_put - Add generic netlink header to netlink message
479 * @skb: socket buffer holding the message
480 * @portid: netlink portid the message is addressed to
481 * @seq: sequence number (usually the one of the sender)
482 * @family: generic netlink family
483 * @flags: netlink message flags
484 * @cmd: generic netlink command
486 * Returns pointer to user specific header
488 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
489 const struct genl_family *family, int flags, u8 cmd)
491 struct nlmsghdr *nlh;
492 struct genlmsghdr *hdr;
494 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
495 family->hdrsize, flags);
499 hdr = nlmsg_data(nlh);
501 hdr->version = family->version;
504 return (char *) hdr + GENL_HDRLEN;
506 EXPORT_SYMBOL(genlmsg_put);
508 static struct genl_dumpit_info *genl_dumpit_info_alloc(void)
510 return kmalloc(sizeof(struct genl_dumpit_info), GFP_KERNEL);
513 static void genl_dumpit_info_free(const struct genl_dumpit_info *info)
518 static struct nlattr **
519 genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
520 struct nlmsghdr *nlh,
521 struct netlink_ext_ack *extack,
522 const struct genl_ops *ops,
524 enum genl_validate_flags no_strict_flag)
526 enum netlink_validation validate = ops->validate & no_strict_flag ?
527 NL_VALIDATE_LIBERAL :
529 struct nlattr **attrbuf;
535 attrbuf = kmalloc_array(ops->maxattr + 1,
536 sizeof(struct nlattr *), GFP_KERNEL);
538 return ERR_PTR(-ENOMEM);
540 err = __nlmsg_parse(nlh, hdrlen, attrbuf, ops->maxattr, ops->policy,
549 static void genl_family_rcv_msg_attrs_free(struct nlattr **attrbuf)
554 struct genl_start_context {
555 const struct genl_family *family;
556 struct nlmsghdr *nlh;
557 struct netlink_ext_ack *extack;
558 const struct genl_ops *ops;
562 static int genl_start(struct netlink_callback *cb)
564 struct genl_start_context *ctx = cb->data;
565 const struct genl_ops *ops = ctx->ops;
566 struct genl_dumpit_info *info;
567 struct nlattr **attrs = NULL;
570 if (ops->validate & GENL_DONT_VALIDATE_DUMP)
573 if (ctx->nlh->nlmsg_len < nlmsg_msg_size(ctx->hdrlen))
576 attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
578 GENL_DONT_VALIDATE_DUMP_STRICT);
580 return PTR_ERR(attrs);
583 info = genl_dumpit_info_alloc();
585 genl_family_rcv_msg_attrs_free(attrs);
588 info->family = ctx->family;
594 if (!ctx->family->parallel_ops)
597 if (!ctx->family->parallel_ops)
602 genl_family_rcv_msg_attrs_free(info->attrs);
603 genl_dumpit_info_free(info);
609 static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
611 const struct genl_ops *ops = &genl_dumpit_info(cb)->op;
615 rc = ops->dumpit(skb, cb);
620 static int genl_lock_done(struct netlink_callback *cb)
622 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
623 const struct genl_ops *ops = &info->op;
631 genl_family_rcv_msg_attrs_free(info->attrs);
632 genl_dumpit_info_free(info);
636 static int genl_parallel_done(struct netlink_callback *cb)
638 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
639 const struct genl_ops *ops = &info->op;
644 genl_family_rcv_msg_attrs_free(info->attrs);
645 genl_dumpit_info_free(info);
649 static int genl_family_rcv_msg_dumpit(const struct genl_family *family,
651 struct nlmsghdr *nlh,
652 struct netlink_ext_ack *extack,
653 const struct genl_ops *ops,
654 int hdrlen, struct net *net)
656 struct genl_start_context ctx;
668 if (!family->parallel_ops) {
669 struct netlink_dump_control c = {
670 .module = family->module,
673 .dump = genl_lock_dumpit,
674 .done = genl_lock_done,
678 err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
681 struct netlink_dump_control c = {
682 .module = family->module,
686 .done = genl_parallel_done,
689 err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
695 static int genl_family_rcv_msg_doit(const struct genl_family *family,
697 struct nlmsghdr *nlh,
698 struct netlink_ext_ack *extack,
699 const struct genl_ops *ops,
700 int hdrlen, struct net *net)
702 struct nlattr **attrbuf;
703 struct genl_info info;
709 attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
711 GENL_DONT_VALIDATE_STRICT);
713 return PTR_ERR(attrbuf);
715 info.snd_seq = nlh->nlmsg_seq;
716 info.snd_portid = NETLINK_CB(skb).portid;
718 info.genlhdr = nlmsg_data(nlh);
719 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
720 info.attrs = attrbuf;
721 info.extack = extack;
722 genl_info_net_set(&info, net);
723 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
725 if (family->pre_doit) {
726 err = family->pre_doit(ops, skb, &info);
731 err = ops->doit(skb, &info);
733 if (family->post_doit)
734 family->post_doit(ops, skb, &info);
737 genl_family_rcv_msg_attrs_free(attrbuf);
742 static int genl_family_rcv_msg(const struct genl_family *family,
744 struct nlmsghdr *nlh,
745 struct netlink_ext_ack *extack)
747 struct net *net = sock_net(skb->sk);
748 struct genlmsghdr *hdr = nlmsg_data(nlh);
752 /* this family doesn't exist in this netns */
753 if (!family->netnsok && !net_eq(net, &init_net))
756 hdrlen = GENL_HDRLEN + family->hdrsize;
757 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
760 if (genl_get_cmd(hdr->cmd, family, &op))
763 if ((op.flags & GENL_ADMIN_PERM) &&
764 !netlink_capable(skb, CAP_NET_ADMIN))
767 if ((op.flags & GENL_UNS_ADMIN_PERM) &&
768 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
771 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP)
772 return genl_family_rcv_msg_dumpit(family, skb, nlh, extack,
775 return genl_family_rcv_msg_doit(family, skb, nlh, extack,
779 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
780 struct netlink_ext_ack *extack)
782 const struct genl_family *family;
785 family = genl_family_find_byid(nlh->nlmsg_type);
789 if (!family->parallel_ops)
792 err = genl_family_rcv_msg(family, skb, nlh, extack);
794 if (!family->parallel_ops)
800 static void genl_rcv(struct sk_buff *skb)
803 netlink_rcv_skb(skb, &genl_rcv_msg);
807 /**************************************************************************
809 **************************************************************************/
811 static struct genl_family genl_ctrl;
813 static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
814 u32 flags, struct sk_buff *skb, u8 cmd)
818 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
822 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
823 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
824 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
825 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
826 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
827 goto nla_put_failure;
829 if (genl_get_cmd_cnt(family)) {
830 struct nlattr *nla_ops;
833 nla_ops = nla_nest_start_noflag(skb, CTRL_ATTR_OPS);
835 goto nla_put_failure;
837 for (i = 0; i < genl_get_cmd_cnt(family); i++) {
842 genl_get_cmd_by_index(i, family, &op);
845 op_flags |= GENL_CMD_CAP_DUMP;
847 op_flags |= GENL_CMD_CAP_DO;
849 op_flags |= GENL_CMD_CAP_HASPOL;
851 nest = nla_nest_start_noflag(skb, i + 1);
853 goto nla_put_failure;
855 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, op.cmd) ||
856 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
857 goto nla_put_failure;
859 nla_nest_end(skb, nest);
862 nla_nest_end(skb, nla_ops);
865 if (family->n_mcgrps) {
866 struct nlattr *nla_grps;
869 nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
870 if (nla_grps == NULL)
871 goto nla_put_failure;
873 for (i = 0; i < family->n_mcgrps; i++) {
875 const struct genl_multicast_group *grp;
877 grp = &family->mcgrps[i];
879 nest = nla_nest_start_noflag(skb, i + 1);
881 goto nla_put_failure;
883 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
884 family->mcgrp_offset + i) ||
885 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
887 goto nla_put_failure;
889 nla_nest_end(skb, nest);
891 nla_nest_end(skb, nla_grps);
894 genlmsg_end(skb, hdr);
898 genlmsg_cancel(skb, hdr);
902 static int ctrl_fill_mcgrp_info(const struct genl_family *family,
903 const struct genl_multicast_group *grp,
904 int grp_id, u32 portid, u32 seq, u32 flags,
905 struct sk_buff *skb, u8 cmd)
908 struct nlattr *nla_grps;
911 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
915 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
916 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
917 goto nla_put_failure;
919 nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
920 if (nla_grps == NULL)
921 goto nla_put_failure;
923 nest = nla_nest_start_noflag(skb, 1);
925 goto nla_put_failure;
927 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
928 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
930 goto nla_put_failure;
932 nla_nest_end(skb, nest);
933 nla_nest_end(skb, nla_grps);
935 genlmsg_end(skb, hdr);
939 genlmsg_cancel(skb, hdr);
943 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
946 struct genl_family *rt;
947 struct net *net = sock_net(skb->sk);
948 int fams_to_skip = cb->args[0];
951 idr_for_each_entry(&genl_fam_idr, rt, id) {
952 if (!rt->netnsok && !net_eq(net, &init_net))
955 if (n++ < fams_to_skip)
958 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
959 cb->nlh->nlmsg_seq, NLM_F_MULTI,
960 skb, CTRL_CMD_NEWFAMILY) < 0) {
970 static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
971 u32 portid, int seq, u8 cmd)
976 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
978 return ERR_PTR(-ENOBUFS);
980 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
989 static struct sk_buff *
990 ctrl_build_mcgrp_msg(const struct genl_family *family,
991 const struct genl_multicast_group *grp,
992 int grp_id, u32 portid, int seq, u8 cmd)
997 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
999 return ERR_PTR(-ENOBUFS);
1001 err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
1005 return ERR_PTR(err);
1011 static const struct nla_policy ctrl_policy_family[] = {
1012 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
1013 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
1014 .len = GENL_NAMSIZ - 1 },
1017 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
1019 struct sk_buff *msg;
1020 const struct genl_family *res = NULL;
1023 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
1024 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
1025 res = genl_family_find_byid(id);
1029 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
1032 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
1033 res = genl_family_find_byname(name);
1034 #ifdef CONFIG_MODULES
1038 request_module("net-pf-%d-proto-%d-family-%s",
1039 PF_NETLINK, NETLINK_GENERIC, name);
1040 down_read(&cb_lock);
1042 res = genl_family_find_byname(name);
1051 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
1052 /* family doesn't exist here */
1056 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
1057 CTRL_CMD_NEWFAMILY);
1059 return PTR_ERR(msg);
1061 return genlmsg_reply(msg, info);
1064 static int genl_ctrl_event(int event, const struct genl_family *family,
1065 const struct genl_multicast_group *grp,
1068 struct sk_buff *msg;
1070 /* genl is still initialising */
1071 if (!init_net.genl_sock)
1075 case CTRL_CMD_NEWFAMILY:
1076 case CTRL_CMD_DELFAMILY:
1078 msg = ctrl_build_family_msg(family, 0, 0, event);
1080 case CTRL_CMD_NEWMCAST_GRP:
1081 case CTRL_CMD_DELMCAST_GRP:
1083 msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
1090 return PTR_ERR(msg);
1092 if (!family->netnsok) {
1093 genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
1097 genlmsg_multicast_allns(&genl_ctrl, msg, 0,
1105 struct ctrl_dump_policy_ctx {
1106 struct netlink_policy_dump_state *state;
1107 const struct genl_family *rt;
1115 static const struct nla_policy ctrl_policy_policy[] = {
1116 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
1117 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
1118 .len = GENL_NAMSIZ - 1 },
1119 [CTRL_ATTR_OP] = { .type = NLA_U32 },
1122 static int ctrl_dumppolicy_start(struct netlink_callback *cb)
1124 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
1125 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1126 struct nlattr **tb = info->attrs;
1127 const struct genl_family *rt;
1131 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
1133 if (!tb[CTRL_ATTR_FAMILY_ID] && !tb[CTRL_ATTR_FAMILY_NAME])
1136 if (tb[CTRL_ATTR_FAMILY_ID]) {
1137 ctx->fam_id = nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
1139 rt = genl_family_find_byname(
1140 nla_data(tb[CTRL_ATTR_FAMILY_NAME]));
1143 ctx->fam_id = rt->id;
1146 rt = genl_family_find_byid(ctx->fam_id);
1152 if (tb[CTRL_ATTR_OP]) {
1153 ctx->single_op = true;
1154 ctx->op = nla_get_u32(tb[CTRL_ATTR_OP]);
1156 err = genl_get_cmd(ctx->op, rt, &op);
1158 NL_SET_BAD_ATTR(cb->extack, tb[CTRL_ATTR_OP]);
1165 return netlink_policy_dump_add_policy(&ctx->state, op.policy,
1169 for (i = 0; i < genl_get_cmd_cnt(rt); i++) {
1170 genl_get_cmd_by_index(i, rt, &op);
1173 err = netlink_policy_dump_add_policy(&ctx->state,
1186 static void *ctrl_dumppolicy_prep(struct sk_buff *skb,
1187 struct netlink_callback *cb)
1189 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1192 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1193 cb->nlh->nlmsg_seq, &genl_ctrl,
1194 NLM_F_MULTI, CTRL_CMD_GETPOLICY);
1198 if (nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, ctx->fam_id))
1204 static int ctrl_dumppolicy_put_op(struct sk_buff *skb,
1205 struct netlink_callback *cb,
1206 struct genl_ops *op)
1208 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1209 struct nlattr *nest_pol, *nest_op;
1213 /* skip if we have nothing to show */
1217 (!op->dumpit || op->validate & GENL_DONT_VALIDATE_DUMP))
1220 hdr = ctrl_dumppolicy_prep(skb, cb);
1224 nest_pol = nla_nest_start(skb, CTRL_ATTR_OP_POLICY);
1228 nest_op = nla_nest_start(skb, op->cmd);
1232 /* for now both do/dump are always the same */
1233 idx = netlink_policy_dump_get_policy_idx(ctx->state,
1237 if (op->doit && nla_put_u32(skb, CTRL_ATTR_POLICY_DO, idx))
1240 if (op->dumpit && !(op->validate & GENL_DONT_VALIDATE_DUMP) &&
1241 nla_put_u32(skb, CTRL_ATTR_POLICY_DUMP, idx))
1244 nla_nest_end(skb, nest_op);
1245 nla_nest_end(skb, nest_pol);
1246 genlmsg_end(skb, hdr);
1250 genlmsg_cancel(skb, hdr);
1254 static int ctrl_dumppolicy(struct sk_buff *skb, struct netlink_callback *cb)
1256 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1259 if (!ctx->policies) {
1260 while (ctx->opidx < genl_get_cmd_cnt(ctx->rt)) {
1263 if (ctx->single_op) {
1266 err = genl_get_cmd(ctx->op, ctx->rt, &op);
1270 /* break out of the loop after this one */
1271 ctx->opidx = genl_get_cmd_cnt(ctx->rt);
1273 genl_get_cmd_by_index(ctx->opidx, ctx->rt, &op);
1276 if (ctrl_dumppolicy_put_op(skb, cb, &op))
1282 /* completed with the per-op policy index list */
1283 ctx->policies = true;
1286 while (netlink_policy_dump_loop(ctx->state)) {
1287 struct nlattr *nest;
1289 hdr = ctrl_dumppolicy_prep(skb, cb);
1291 goto nla_put_failure;
1293 nest = nla_nest_start(skb, CTRL_ATTR_POLICY);
1295 goto nla_put_failure;
1297 if (netlink_policy_dump_write(skb, ctx->state))
1298 goto nla_put_failure;
1300 nla_nest_end(skb, nest);
1302 genlmsg_end(skb, hdr);
1308 genlmsg_cancel(skb, hdr);
1312 static int ctrl_dumppolicy_done(struct netlink_callback *cb)
1314 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1316 netlink_policy_dump_free(ctx->state);
1320 static const struct genl_ops genl_ctrl_ops[] = {
1322 .cmd = CTRL_CMD_GETFAMILY,
1323 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1324 .policy = ctrl_policy_family,
1325 .maxattr = ARRAY_SIZE(ctrl_policy_family) - 1,
1326 .doit = ctrl_getfamily,
1327 .dumpit = ctrl_dumpfamily,
1330 .cmd = CTRL_CMD_GETPOLICY,
1331 .policy = ctrl_policy_policy,
1332 .maxattr = ARRAY_SIZE(ctrl_policy_policy) - 1,
1333 .start = ctrl_dumppolicy_start,
1334 .dumpit = ctrl_dumppolicy,
1335 .done = ctrl_dumppolicy_done,
1339 static const struct genl_multicast_group genl_ctrl_groups[] = {
1340 { .name = "notify", },
1343 static struct genl_family genl_ctrl __ro_after_init = {
1344 .module = THIS_MODULE,
1345 .ops = genl_ctrl_ops,
1346 .n_ops = ARRAY_SIZE(genl_ctrl_ops),
1347 .mcgrps = genl_ctrl_groups,
1348 .n_mcgrps = ARRAY_SIZE(genl_ctrl_groups),
1355 static int genl_bind(struct net *net, int group)
1357 const struct genl_family *family;
1363 idr_for_each_entry(&genl_fam_idr, family, id) {
1364 const struct genl_multicast_group *grp;
1367 if (family->n_mcgrps == 0)
1370 i = group - family->mcgrp_offset;
1371 if (i < 0 || i >= family->n_mcgrps)
1374 grp = &family->mcgrps[i];
1375 if ((grp->flags & GENL_UNS_ADMIN_PERM) &&
1376 !ns_capable(net->user_ns, CAP_NET_ADMIN))
1386 static int __net_init genl_pernet_init(struct net *net)
1388 struct netlink_kernel_cfg cfg = {
1390 .flags = NL_CFG_F_NONROOT_RECV,
1394 /* we'll bump the group number right afterwards */
1395 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
1397 if (!net->genl_sock && net_eq(net, &init_net))
1398 panic("GENL: Cannot initialize generic netlink\n");
1400 if (!net->genl_sock)
1406 static void __net_exit genl_pernet_exit(struct net *net)
1408 netlink_kernel_release(net->genl_sock);
1409 net->genl_sock = NULL;
1412 static struct pernet_operations genl_pernet_ops = {
1413 .init = genl_pernet_init,
1414 .exit = genl_pernet_exit,
1417 static int __init genl_init(void)
1421 err = genl_register_family(&genl_ctrl);
1425 err = register_pernet_subsys(&genl_pernet_ops);
1432 panic("GENL: Cannot register controller: %d\n", err);
1435 core_initcall(genl_init);
1437 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
1440 struct sk_buff *tmp;
1441 struct net *net, *prev = NULL;
1442 bool delivered = false;
1445 for_each_net_rcu(net) {
1447 tmp = skb_clone(skb, flags);
1452 err = nlmsg_multicast(prev->genl_sock, tmp,
1453 portid, group, flags);
1456 else if (err != -ESRCH)
1463 err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1466 else if (err != -ESRCH)
1468 return delivered ? 0 : -ESRCH;
1474 int genlmsg_multicast_allns(const struct genl_family *family,
1475 struct sk_buff *skb, u32 portid,
1476 unsigned int group, gfp_t flags)
1478 if (WARN_ON_ONCE(group >= family->n_mcgrps))
1481 group = family->mcgrp_offset + group;
1482 return genlmsg_mcast(skb, portid, group, flags);
1484 EXPORT_SYMBOL(genlmsg_multicast_allns);
1486 void genl_notify(const struct genl_family *family, struct sk_buff *skb,
1487 struct genl_info *info, u32 group, gfp_t flags)
1489 struct net *net = genl_info_net(info);
1490 struct sock *sk = net->genl_sock;
1492 if (WARN_ON_ONCE(group >= family->n_mcgrps))
1495 group = family->mcgrp_offset + group;
1496 nlmsg_notify(sk, skb, info->snd_portid, group,
1497 nlmsg_report(info->nlhdr), flags);
1499 EXPORT_SYMBOL(genl_notify);