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 /* We need the last attribute with non-zero ID therefore a 2-entry array */
82 static struct nla_policy genl_policy_reject_all[] = {
83 { .type = NLA_REJECT },
84 { .type = NLA_REJECT },
87 static int genl_ctrl_event(int event, const struct genl_family *family,
88 const struct genl_multicast_group *grp,
92 genl_op_fill_in_reject_policy(const struct genl_family *family,
95 BUILD_BUG_ON(ARRAY_SIZE(genl_policy_reject_all) - 1 != 1);
97 if (op->policy || op->cmd < family->resv_start_op)
100 op->policy = genl_policy_reject_all;
104 static const struct genl_family *genl_family_find_byid(unsigned int id)
106 return idr_find(&genl_fam_idr, id);
109 static const struct genl_family *genl_family_find_byname(char *name)
111 const struct genl_family *family;
114 idr_for_each_entry(&genl_fam_idr, family, id)
115 if (strcmp(family->name, name) == 0)
121 static int genl_get_cmd_cnt(const struct genl_family *family)
123 return family->n_ops + family->n_small_ops;
126 static void genl_op_from_full(const struct genl_family *family,
127 unsigned int i, struct genl_ops *op)
129 *op = family->ops[i];
132 op->maxattr = family->maxattr;
134 op->policy = family->policy;
136 genl_op_fill_in_reject_policy(family, op);
139 static int genl_get_cmd_full(u32 cmd, const struct genl_family *family,
144 for (i = 0; i < family->n_ops; i++)
145 if (family->ops[i].cmd == cmd) {
146 genl_op_from_full(family, i, op);
153 static void genl_op_from_small(const struct genl_family *family,
154 unsigned int i, struct genl_ops *op)
156 memset(op, 0, sizeof(*op));
157 op->doit = family->small_ops[i].doit;
158 op->dumpit = family->small_ops[i].dumpit;
159 op->cmd = family->small_ops[i].cmd;
160 op->internal_flags = family->small_ops[i].internal_flags;
161 op->flags = family->small_ops[i].flags;
162 op->validate = family->small_ops[i].validate;
164 op->maxattr = family->maxattr;
165 op->policy = family->policy;
167 genl_op_fill_in_reject_policy(family, op);
170 static int genl_get_cmd_small(u32 cmd, const struct genl_family *family,
175 for (i = 0; i < family->n_small_ops; i++)
176 if (family->small_ops[i].cmd == cmd) {
177 genl_op_from_small(family, i, op);
184 static int genl_get_cmd(u32 cmd, const struct genl_family *family,
187 if (!genl_get_cmd_full(cmd, family, op))
189 return genl_get_cmd_small(cmd, family, op);
192 static void genl_get_cmd_by_index(unsigned int i,
193 const struct genl_family *family,
196 if (i < family->n_ops)
197 genl_op_from_full(family, i, op);
198 else if (i < family->n_ops + family->n_small_ops)
199 genl_op_from_small(family, i - family->n_ops, op);
204 static int genl_allocate_reserve_groups(int n_groups, int *first_id)
206 unsigned long *new_groups;
214 id = find_first_zero_bit(mc_groups,
218 id = find_next_zero_bit(mc_groups,
219 mc_groups_longs * BITS_PER_LONG,
224 i < min_t(int, id + n_groups,
225 mc_groups_longs * BITS_PER_LONG);
227 if (test_bit(i, mc_groups)) {
234 if (id + n_groups > mc_groups_longs * BITS_PER_LONG) {
235 unsigned long new_longs = mc_groups_longs +
236 BITS_TO_LONGS(n_groups);
237 size_t nlen = new_longs * sizeof(unsigned long);
239 if (mc_groups == &mc_group_start) {
240 new_groups = kzalloc(nlen, GFP_KERNEL);
243 mc_groups = new_groups;
244 *mc_groups = mc_group_start;
246 new_groups = krealloc(mc_groups, nlen,
250 mc_groups = new_groups;
251 for (i = 0; i < BITS_TO_LONGS(n_groups); i++)
252 mc_groups[mc_groups_longs + i] = 0;
254 mc_groups_longs = new_longs;
258 for (i = id; i < id + n_groups; i++)
259 set_bit(i, mc_groups);
264 static struct genl_family genl_ctrl;
266 static int genl_validate_assign_mc_groups(struct genl_family *family)
269 int n_groups = family->n_mcgrps;
271 bool groups_allocated = false;
276 for (i = 0; i < n_groups; i++) {
277 const struct genl_multicast_group *grp = &family->mcgrps[i];
279 if (WARN_ON(grp->name[0] == '\0'))
281 if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL))
285 /* special-case our own group and hacks */
286 if (family == &genl_ctrl) {
287 first_id = GENL_ID_CTRL;
288 BUG_ON(n_groups != 1);
289 } else if (strcmp(family->name, "NET_DM") == 0) {
291 BUG_ON(n_groups != 1);
292 } else if (family->id == GENL_ID_VFS_DQUOT) {
293 first_id = GENL_ID_VFS_DQUOT;
294 BUG_ON(n_groups != 1);
295 } else if (family->id == GENL_ID_PMCRAID) {
296 first_id = GENL_ID_PMCRAID;
297 BUG_ON(n_groups != 1);
299 groups_allocated = true;
300 err = genl_allocate_reserve_groups(n_groups, &first_id);
305 family->mcgrp_offset = first_id;
307 /* if still initializing, can't and don't need to realloc bitmaps */
308 if (!init_net.genl_sock)
311 if (family->netnsok) {
314 netlink_table_grab();
316 for_each_net_rcu(net) {
317 err = __netlink_change_ngroups(net->genl_sock,
318 mc_groups_longs * BITS_PER_LONG);
321 * No need to roll back, can only fail if
322 * memory allocation fails and then the
323 * number of _possible_ groups has been
324 * increased on some sockets which is ok.
330 netlink_table_ungrab();
332 err = netlink_change_ngroups(init_net.genl_sock,
333 mc_groups_longs * BITS_PER_LONG);
336 if (groups_allocated && err) {
337 for (i = 0; i < family->n_mcgrps; i++)
338 clear_bit(family->mcgrp_offset + i, mc_groups);
344 static void genl_unregister_mc_groups(const struct genl_family *family)
349 netlink_table_grab();
351 for_each_net_rcu(net) {
352 for (i = 0; i < family->n_mcgrps; i++)
353 __netlink_clear_multicast_users(
354 net->genl_sock, family->mcgrp_offset + i);
357 netlink_table_ungrab();
359 for (i = 0; i < family->n_mcgrps; i++) {
360 int grp_id = family->mcgrp_offset + i;
363 clear_bit(grp_id, mc_groups);
364 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family,
365 &family->mcgrps[i], grp_id);
369 static int genl_validate_ops(const struct genl_family *family)
373 if (WARN_ON(family->n_ops && !family->ops) ||
374 WARN_ON(family->n_small_ops && !family->small_ops))
377 for (i = 0; i < genl_get_cmd_cnt(family); i++) {
380 genl_get_cmd_by_index(i, family, &op);
381 if (op.dumpit == NULL && op.doit == NULL)
383 if (WARN_ON(op.cmd >= family->resv_start_op && op.validate))
385 for (j = i + 1; j < genl_get_cmd_cnt(family); j++) {
388 genl_get_cmd_by_index(j, family, &op2);
389 if (op.cmd == op2.cmd)
398 * genl_register_family - register a generic netlink family
399 * @family: generic netlink family
401 * Registers the specified family after validating it first. Only one
402 * family may be registered with the same family name or identifier.
404 * The family's ops, multicast groups and module pointer must already
407 * Return 0 on success or a negative error code.
409 int genl_register_family(struct genl_family *family)
412 int start = GENL_START_ALLOC, end = GENL_MAX_ID;
414 err = genl_validate_ops(family);
420 if (genl_family_find_byname(family->name)) {
426 * Sadly, a few cases need to be special-cased
427 * due to them having previously abused the API
428 * and having used their family ID also as their
429 * multicast group ID, so we use reserved IDs
430 * for both to be sure we can do that mapping.
432 if (family == &genl_ctrl) {
433 /* and this needs to be special for initial family lookups */
434 start = end = GENL_ID_CTRL;
435 } else if (strcmp(family->name, "pmcraid") == 0) {
436 start = end = GENL_ID_PMCRAID;
437 } else if (strcmp(family->name, "VFS_DQUOT") == 0) {
438 start = end = GENL_ID_VFS_DQUOT;
441 family->id = idr_alloc_cyclic(&genl_fam_idr, family,
442 start, end + 1, GFP_KERNEL);
443 if (family->id < 0) {
448 err = genl_validate_assign_mc_groups(family);
454 /* send all events */
455 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
456 for (i = 0; i < family->n_mcgrps; i++)
457 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
458 &family->mcgrps[i], family->mcgrp_offset + i);
463 idr_remove(&genl_fam_idr, family->id);
468 EXPORT_SYMBOL(genl_register_family);
471 * genl_unregister_family - unregister generic netlink family
472 * @family: generic netlink family
474 * Unregisters the specified family.
476 * Returns 0 on success or a negative error code.
478 int genl_unregister_family(const struct genl_family *family)
482 if (!genl_family_find_byid(family->id)) {
487 genl_unregister_mc_groups(family);
489 idr_remove(&genl_fam_idr, family->id);
492 wait_event(genl_sk_destructing_waitq,
493 atomic_read(&genl_sk_destructing_cnt) == 0);
496 genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
500 EXPORT_SYMBOL(genl_unregister_family);
503 * genlmsg_put - Add generic netlink header to netlink message
504 * @skb: socket buffer holding the message
505 * @portid: netlink portid the message is addressed to
506 * @seq: sequence number (usually the one of the sender)
507 * @family: generic netlink family
508 * @flags: netlink message flags
509 * @cmd: generic netlink command
511 * Returns pointer to user specific header
513 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
514 const struct genl_family *family, int flags, u8 cmd)
516 struct nlmsghdr *nlh;
517 struct genlmsghdr *hdr;
519 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
520 family->hdrsize, flags);
524 hdr = nlmsg_data(nlh);
526 hdr->version = family->version;
529 return (char *) hdr + GENL_HDRLEN;
531 EXPORT_SYMBOL(genlmsg_put);
533 static struct genl_dumpit_info *genl_dumpit_info_alloc(void)
535 return kmalloc(sizeof(struct genl_dumpit_info), GFP_KERNEL);
538 static void genl_dumpit_info_free(const struct genl_dumpit_info *info)
543 static struct nlattr **
544 genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
545 struct nlmsghdr *nlh,
546 struct netlink_ext_ack *extack,
547 const struct genl_ops *ops,
549 enum genl_validate_flags no_strict_flag)
551 enum netlink_validation validate = ops->validate & no_strict_flag ?
552 NL_VALIDATE_LIBERAL :
554 struct nlattr **attrbuf;
560 attrbuf = kmalloc_array(ops->maxattr + 1,
561 sizeof(struct nlattr *), GFP_KERNEL);
563 return ERR_PTR(-ENOMEM);
565 err = __nlmsg_parse(nlh, hdrlen, attrbuf, ops->maxattr, ops->policy,
574 static void genl_family_rcv_msg_attrs_free(struct nlattr **attrbuf)
579 struct genl_start_context {
580 const struct genl_family *family;
581 struct nlmsghdr *nlh;
582 struct netlink_ext_ack *extack;
583 const struct genl_ops *ops;
587 static int genl_start(struct netlink_callback *cb)
589 struct genl_start_context *ctx = cb->data;
590 const struct genl_ops *ops = ctx->ops;
591 struct genl_dumpit_info *info;
592 struct nlattr **attrs = NULL;
595 if (ops->validate & GENL_DONT_VALIDATE_DUMP)
598 if (ctx->nlh->nlmsg_len < nlmsg_msg_size(ctx->hdrlen))
601 attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
603 GENL_DONT_VALIDATE_DUMP_STRICT);
605 return PTR_ERR(attrs);
608 info = genl_dumpit_info_alloc();
610 genl_family_rcv_msg_attrs_free(attrs);
613 info->family = ctx->family;
619 if (!ctx->family->parallel_ops)
622 if (!ctx->family->parallel_ops)
627 genl_family_rcv_msg_attrs_free(info->attrs);
628 genl_dumpit_info_free(info);
634 static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
636 const struct genl_ops *ops = &genl_dumpit_info(cb)->op;
640 rc = ops->dumpit(skb, cb);
645 static int genl_lock_done(struct netlink_callback *cb)
647 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
648 const struct genl_ops *ops = &info->op;
656 genl_family_rcv_msg_attrs_free(info->attrs);
657 genl_dumpit_info_free(info);
661 static int genl_parallel_done(struct netlink_callback *cb)
663 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
664 const struct genl_ops *ops = &info->op;
669 genl_family_rcv_msg_attrs_free(info->attrs);
670 genl_dumpit_info_free(info);
674 static int genl_family_rcv_msg_dumpit(const struct genl_family *family,
676 struct nlmsghdr *nlh,
677 struct netlink_ext_ack *extack,
678 const struct genl_ops *ops,
679 int hdrlen, struct net *net)
681 struct genl_start_context ctx;
693 if (!family->parallel_ops) {
694 struct netlink_dump_control c = {
695 .module = family->module,
698 .dump = genl_lock_dumpit,
699 .done = genl_lock_done,
703 err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
706 struct netlink_dump_control c = {
707 .module = family->module,
711 .done = genl_parallel_done,
714 err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
720 static int genl_family_rcv_msg_doit(const struct genl_family *family,
722 struct nlmsghdr *nlh,
723 struct netlink_ext_ack *extack,
724 const struct genl_ops *ops,
725 int hdrlen, struct net *net)
727 struct nlattr **attrbuf;
728 struct genl_info info;
734 attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
736 GENL_DONT_VALIDATE_STRICT);
738 return PTR_ERR(attrbuf);
740 info.snd_seq = nlh->nlmsg_seq;
741 info.snd_portid = NETLINK_CB(skb).portid;
743 info.genlhdr = nlmsg_data(nlh);
744 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
745 info.attrs = attrbuf;
746 info.extack = extack;
747 genl_info_net_set(&info, net);
748 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
750 if (family->pre_doit) {
751 err = family->pre_doit(ops, skb, &info);
756 err = ops->doit(skb, &info);
758 if (family->post_doit)
759 family->post_doit(ops, skb, &info);
762 genl_family_rcv_msg_attrs_free(attrbuf);
767 static int genl_header_check(const struct genl_family *family,
768 struct nlmsghdr *nlh, struct genlmsghdr *hdr,
769 struct netlink_ext_ack *extack)
773 /* Only for commands added after we started validating */
774 if (hdr->cmd < family->resv_start_op)
778 NL_SET_ERR_MSG(extack, "genlmsghdr.reserved field is not 0");
782 /* Old netlink flags have pretty loose semantics, allow only the flags
783 * consumed by the core where we can enforce the meaning.
785 flags = nlh->nlmsg_flags;
786 if ((flags & NLM_F_DUMP) == NLM_F_DUMP) /* DUMP is 2 bits */
787 flags &= ~NLM_F_DUMP;
788 if (flags & ~(NLM_F_REQUEST | NLM_F_ACK | NLM_F_ECHO)) {
789 NL_SET_ERR_MSG(extack,
790 "ambiguous or reserved bits set in nlmsg_flags");
797 static int genl_family_rcv_msg(const struct genl_family *family,
799 struct nlmsghdr *nlh,
800 struct netlink_ext_ack *extack)
802 struct net *net = sock_net(skb->sk);
803 struct genlmsghdr *hdr = nlmsg_data(nlh);
807 /* this family doesn't exist in this netns */
808 if (!family->netnsok && !net_eq(net, &init_net))
811 hdrlen = GENL_HDRLEN + family->hdrsize;
812 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
815 if (genl_header_check(family, nlh, hdr, extack))
818 if (genl_get_cmd(hdr->cmd, family, &op))
821 if ((op.flags & GENL_ADMIN_PERM) &&
822 !netlink_capable(skb, CAP_NET_ADMIN))
825 if ((op.flags & GENL_UNS_ADMIN_PERM) &&
826 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
829 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP)
830 return genl_family_rcv_msg_dumpit(family, skb, nlh, extack,
833 return genl_family_rcv_msg_doit(family, skb, nlh, extack,
837 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
838 struct netlink_ext_ack *extack)
840 const struct genl_family *family;
843 family = genl_family_find_byid(nlh->nlmsg_type);
847 if (!family->parallel_ops)
850 err = genl_family_rcv_msg(family, skb, nlh, extack);
852 if (!family->parallel_ops)
858 static void genl_rcv(struct sk_buff *skb)
861 netlink_rcv_skb(skb, &genl_rcv_msg);
865 /**************************************************************************
867 **************************************************************************/
869 static struct genl_family genl_ctrl;
871 static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
872 u32 flags, struct sk_buff *skb, u8 cmd)
876 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
880 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
881 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
882 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
883 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
884 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
885 goto nla_put_failure;
887 if (genl_get_cmd_cnt(family)) {
888 struct nlattr *nla_ops;
891 nla_ops = nla_nest_start_noflag(skb, CTRL_ATTR_OPS);
893 goto nla_put_failure;
895 for (i = 0; i < genl_get_cmd_cnt(family); i++) {
900 genl_get_cmd_by_index(i, family, &op);
903 op_flags |= GENL_CMD_CAP_DUMP;
905 op_flags |= GENL_CMD_CAP_DO;
907 op_flags |= GENL_CMD_CAP_HASPOL;
909 nest = nla_nest_start_noflag(skb, i + 1);
911 goto nla_put_failure;
913 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, op.cmd) ||
914 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
915 goto nla_put_failure;
917 nla_nest_end(skb, nest);
920 nla_nest_end(skb, nla_ops);
923 if (family->n_mcgrps) {
924 struct nlattr *nla_grps;
927 nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
928 if (nla_grps == NULL)
929 goto nla_put_failure;
931 for (i = 0; i < family->n_mcgrps; i++) {
933 const struct genl_multicast_group *grp;
935 grp = &family->mcgrps[i];
937 nest = nla_nest_start_noflag(skb, i + 1);
939 goto nla_put_failure;
941 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
942 family->mcgrp_offset + i) ||
943 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
945 goto nla_put_failure;
947 nla_nest_end(skb, nest);
949 nla_nest_end(skb, nla_grps);
952 genlmsg_end(skb, hdr);
956 genlmsg_cancel(skb, hdr);
960 static int ctrl_fill_mcgrp_info(const struct genl_family *family,
961 const struct genl_multicast_group *grp,
962 int grp_id, u32 portid, u32 seq, u32 flags,
963 struct sk_buff *skb, u8 cmd)
966 struct nlattr *nla_grps;
969 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
973 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
974 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
975 goto nla_put_failure;
977 nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
978 if (nla_grps == NULL)
979 goto nla_put_failure;
981 nest = nla_nest_start_noflag(skb, 1);
983 goto nla_put_failure;
985 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
986 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
988 goto nla_put_failure;
990 nla_nest_end(skb, nest);
991 nla_nest_end(skb, nla_grps);
993 genlmsg_end(skb, hdr);
997 genlmsg_cancel(skb, hdr);
1001 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
1004 struct genl_family *rt;
1005 struct net *net = sock_net(skb->sk);
1006 int fams_to_skip = cb->args[0];
1009 idr_for_each_entry(&genl_fam_idr, rt, id) {
1010 if (!rt->netnsok && !net_eq(net, &init_net))
1013 if (n++ < fams_to_skip)
1016 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
1017 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1018 skb, CTRL_CMD_NEWFAMILY) < 0) {
1028 static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
1029 u32 portid, int seq, u8 cmd)
1031 struct sk_buff *skb;
1034 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1036 return ERR_PTR(-ENOBUFS);
1038 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
1041 return ERR_PTR(err);
1047 static struct sk_buff *
1048 ctrl_build_mcgrp_msg(const struct genl_family *family,
1049 const struct genl_multicast_group *grp,
1050 int grp_id, u32 portid, int seq, u8 cmd)
1052 struct sk_buff *skb;
1055 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1057 return ERR_PTR(-ENOBUFS);
1059 err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
1063 return ERR_PTR(err);
1069 static const struct nla_policy ctrl_policy_family[] = {
1070 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
1071 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
1072 .len = GENL_NAMSIZ - 1 },
1075 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
1077 struct sk_buff *msg;
1078 const struct genl_family *res = NULL;
1081 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
1082 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
1083 res = genl_family_find_byid(id);
1087 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
1090 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
1091 res = genl_family_find_byname(name);
1092 #ifdef CONFIG_MODULES
1096 request_module("net-pf-%d-proto-%d-family-%s",
1097 PF_NETLINK, NETLINK_GENERIC, name);
1098 down_read(&cb_lock);
1100 res = genl_family_find_byname(name);
1109 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
1110 /* family doesn't exist here */
1114 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
1115 CTRL_CMD_NEWFAMILY);
1117 return PTR_ERR(msg);
1119 return genlmsg_reply(msg, info);
1122 static int genl_ctrl_event(int event, const struct genl_family *family,
1123 const struct genl_multicast_group *grp,
1126 struct sk_buff *msg;
1128 /* genl is still initialising */
1129 if (!init_net.genl_sock)
1133 case CTRL_CMD_NEWFAMILY:
1134 case CTRL_CMD_DELFAMILY:
1136 msg = ctrl_build_family_msg(family, 0, 0, event);
1138 case CTRL_CMD_NEWMCAST_GRP:
1139 case CTRL_CMD_DELMCAST_GRP:
1141 msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
1148 return PTR_ERR(msg);
1150 if (!family->netnsok) {
1151 genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
1155 genlmsg_multicast_allns(&genl_ctrl, msg, 0,
1163 struct ctrl_dump_policy_ctx {
1164 struct netlink_policy_dump_state *state;
1165 const struct genl_family *rt;
1173 static const struct nla_policy ctrl_policy_policy[] = {
1174 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
1175 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
1176 .len = GENL_NAMSIZ - 1 },
1177 [CTRL_ATTR_OP] = { .type = NLA_U32 },
1180 static int ctrl_dumppolicy_start(struct netlink_callback *cb)
1182 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
1183 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1184 struct nlattr **tb = info->attrs;
1185 const struct genl_family *rt;
1189 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
1191 if (!tb[CTRL_ATTR_FAMILY_ID] && !tb[CTRL_ATTR_FAMILY_NAME])
1194 if (tb[CTRL_ATTR_FAMILY_ID]) {
1195 ctx->fam_id = nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
1197 rt = genl_family_find_byname(
1198 nla_data(tb[CTRL_ATTR_FAMILY_NAME]));
1201 ctx->fam_id = rt->id;
1204 rt = genl_family_find_byid(ctx->fam_id);
1210 if (tb[CTRL_ATTR_OP]) {
1211 ctx->single_op = true;
1212 ctx->op = nla_get_u32(tb[CTRL_ATTR_OP]);
1214 err = genl_get_cmd(ctx->op, rt, &op);
1216 NL_SET_BAD_ATTR(cb->extack, tb[CTRL_ATTR_OP]);
1223 return netlink_policy_dump_add_policy(&ctx->state, op.policy,
1227 for (i = 0; i < genl_get_cmd_cnt(rt); i++) {
1228 genl_get_cmd_by_index(i, rt, &op);
1231 err = netlink_policy_dump_add_policy(&ctx->state,
1235 goto err_free_state;
1244 netlink_policy_dump_free(ctx->state);
1248 static void *ctrl_dumppolicy_prep(struct sk_buff *skb,
1249 struct netlink_callback *cb)
1251 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1254 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1255 cb->nlh->nlmsg_seq, &genl_ctrl,
1256 NLM_F_MULTI, CTRL_CMD_GETPOLICY);
1260 if (nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, ctx->fam_id))
1266 static int ctrl_dumppolicy_put_op(struct sk_buff *skb,
1267 struct netlink_callback *cb,
1268 struct genl_ops *op)
1270 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1271 struct nlattr *nest_pol, *nest_op;
1275 /* skip if we have nothing to show */
1279 (!op->dumpit || op->validate & GENL_DONT_VALIDATE_DUMP))
1282 hdr = ctrl_dumppolicy_prep(skb, cb);
1286 nest_pol = nla_nest_start(skb, CTRL_ATTR_OP_POLICY);
1290 nest_op = nla_nest_start(skb, op->cmd);
1294 /* for now both do/dump are always the same */
1295 idx = netlink_policy_dump_get_policy_idx(ctx->state,
1299 if (op->doit && nla_put_u32(skb, CTRL_ATTR_POLICY_DO, idx))
1302 if (op->dumpit && !(op->validate & GENL_DONT_VALIDATE_DUMP) &&
1303 nla_put_u32(skb, CTRL_ATTR_POLICY_DUMP, idx))
1306 nla_nest_end(skb, nest_op);
1307 nla_nest_end(skb, nest_pol);
1308 genlmsg_end(skb, hdr);
1312 genlmsg_cancel(skb, hdr);
1316 static int ctrl_dumppolicy(struct sk_buff *skb, struct netlink_callback *cb)
1318 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1321 if (!ctx->policies) {
1322 while (ctx->opidx < genl_get_cmd_cnt(ctx->rt)) {
1325 if (ctx->single_op) {
1328 err = genl_get_cmd(ctx->op, ctx->rt, &op);
1332 /* break out of the loop after this one */
1333 ctx->opidx = genl_get_cmd_cnt(ctx->rt);
1335 genl_get_cmd_by_index(ctx->opidx, ctx->rt, &op);
1338 if (ctrl_dumppolicy_put_op(skb, cb, &op))
1344 /* completed with the per-op policy index list */
1345 ctx->policies = true;
1348 while (netlink_policy_dump_loop(ctx->state)) {
1349 struct nlattr *nest;
1351 hdr = ctrl_dumppolicy_prep(skb, cb);
1353 goto nla_put_failure;
1355 nest = nla_nest_start(skb, CTRL_ATTR_POLICY);
1357 goto nla_put_failure;
1359 if (netlink_policy_dump_write(skb, ctx->state))
1360 goto nla_put_failure;
1362 nla_nest_end(skb, nest);
1364 genlmsg_end(skb, hdr);
1370 genlmsg_cancel(skb, hdr);
1374 static int ctrl_dumppolicy_done(struct netlink_callback *cb)
1376 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1378 netlink_policy_dump_free(ctx->state);
1382 static const struct genl_ops genl_ctrl_ops[] = {
1384 .cmd = CTRL_CMD_GETFAMILY,
1385 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1386 .policy = ctrl_policy_family,
1387 .maxattr = ARRAY_SIZE(ctrl_policy_family) - 1,
1388 .doit = ctrl_getfamily,
1389 .dumpit = ctrl_dumpfamily,
1392 .cmd = CTRL_CMD_GETPOLICY,
1393 .policy = ctrl_policy_policy,
1394 .maxattr = ARRAY_SIZE(ctrl_policy_policy) - 1,
1395 .start = ctrl_dumppolicy_start,
1396 .dumpit = ctrl_dumppolicy,
1397 .done = ctrl_dumppolicy_done,
1401 static const struct genl_multicast_group genl_ctrl_groups[] = {
1402 { .name = "notify", },
1405 static struct genl_family genl_ctrl __ro_after_init = {
1406 .module = THIS_MODULE,
1407 .ops = genl_ctrl_ops,
1408 .n_ops = ARRAY_SIZE(genl_ctrl_ops),
1409 .resv_start_op = CTRL_CMD_GETPOLICY + 1,
1410 .mcgrps = genl_ctrl_groups,
1411 .n_mcgrps = ARRAY_SIZE(genl_ctrl_groups),
1418 static int genl_bind(struct net *net, int group)
1420 const struct genl_family *family;
1424 down_read(&cb_lock);
1426 idr_for_each_entry(&genl_fam_idr, family, id) {
1427 const struct genl_multicast_group *grp;
1430 if (family->n_mcgrps == 0)
1433 i = group - family->mcgrp_offset;
1434 if (i < 0 || i >= family->n_mcgrps)
1437 grp = &family->mcgrps[i];
1438 if ((grp->flags & GENL_UNS_ADMIN_PERM) &&
1439 !ns_capable(net->user_ns, CAP_NET_ADMIN))
1449 static int __net_init genl_pernet_init(struct net *net)
1451 struct netlink_kernel_cfg cfg = {
1453 .flags = NL_CFG_F_NONROOT_RECV,
1457 /* we'll bump the group number right afterwards */
1458 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
1460 if (!net->genl_sock && net_eq(net, &init_net))
1461 panic("GENL: Cannot initialize generic netlink\n");
1463 if (!net->genl_sock)
1469 static void __net_exit genl_pernet_exit(struct net *net)
1471 netlink_kernel_release(net->genl_sock);
1472 net->genl_sock = NULL;
1475 static struct pernet_operations genl_pernet_ops = {
1476 .init = genl_pernet_init,
1477 .exit = genl_pernet_exit,
1480 static int __init genl_init(void)
1484 err = genl_register_family(&genl_ctrl);
1488 err = register_pernet_subsys(&genl_pernet_ops);
1495 panic("GENL: Cannot register controller: %d\n", err);
1498 core_initcall(genl_init);
1500 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
1503 struct sk_buff *tmp;
1504 struct net *net, *prev = NULL;
1505 bool delivered = false;
1508 for_each_net_rcu(net) {
1510 tmp = skb_clone(skb, flags);
1515 err = nlmsg_multicast(prev->genl_sock, tmp,
1516 portid, group, flags);
1519 else if (err != -ESRCH)
1526 err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1529 else if (err != -ESRCH)
1531 return delivered ? 0 : -ESRCH;
1537 int genlmsg_multicast_allns(const struct genl_family *family,
1538 struct sk_buff *skb, u32 portid,
1539 unsigned int group, gfp_t flags)
1541 if (WARN_ON_ONCE(group >= family->n_mcgrps))
1544 group = family->mcgrp_offset + group;
1545 return genlmsg_mcast(skb, portid, group, flags);
1547 EXPORT_SYMBOL(genlmsg_multicast_allns);
1549 void genl_notify(const struct genl_family *family, struct sk_buff *skb,
1550 struct genl_info *info, u32 group, gfp_t flags)
1552 struct net *net = genl_info_net(info);
1553 struct sock *sk = net->genl_sock;
1555 if (WARN_ON_ONCE(group >= family->n_mcgrps))
1558 group = family->mcgrp_offset + group;
1559 nlmsg_notify(sk, skb, info->snd_portid, group,
1560 nlmsg_report(info->nlhdr), flags);
1562 EXPORT_SYMBOL(genl_notify);