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_helpers.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 void genl_op_lock(const struct genl_family *family)
57 if (!family->parallel_ops)
61 static void genl_op_unlock(const struct genl_family *family)
63 if (!family->parallel_ops)
67 static DEFINE_IDR(genl_fam_idr);
70 * Bitmap of multicast groups that are currently in use.
72 * To avoid an allocation at boot of just one unsigned long,
73 * declare it global instead.
74 * Bit 0 is marked as already used since group 0 is invalid.
75 * Bit 1 is marked as already used since the drop-monitor code
76 * abuses the API and thinks it can statically use group 1.
77 * That group will typically conflict with other groups that
78 * any proper users use.
79 * Bit 16 is marked as used since it's used for generic netlink
80 * and the code no longer marks pre-reserved IDs as used.
81 * Bit 17 is marked as already used since the VFS quota code
82 * also abused this API and relied on family == group ID, we
83 * cater to that by giving it a static family and group ID.
84 * Bit 18 is marked as already used since the PMCRAID driver
85 * did the same thing as the VFS quota code (maybe copied?)
87 static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) |
88 BIT(GENL_ID_VFS_DQUOT) |
90 static unsigned long *mc_groups = &mc_group_start;
91 static unsigned long mc_groups_longs = 1;
93 /* We need the last attribute with non-zero ID therefore a 2-entry array */
94 static struct nla_policy genl_policy_reject_all[] = {
95 { .type = NLA_REJECT },
96 { .type = NLA_REJECT },
99 static int genl_ctrl_event(int event, const struct genl_family *family,
100 const struct genl_multicast_group *grp,
104 genl_op_fill_in_reject_policy(const struct genl_family *family,
107 BUILD_BUG_ON(ARRAY_SIZE(genl_policy_reject_all) - 1 != 1);
109 if (op->policy || op->cmd < family->resv_start_op)
112 op->policy = genl_policy_reject_all;
117 genl_op_fill_in_reject_policy_split(const struct genl_family *family,
118 struct genl_split_ops *op)
123 op->policy = genl_policy_reject_all;
127 static const struct genl_family *genl_family_find_byid(unsigned int id)
129 return idr_find(&genl_fam_idr, id);
132 static const struct genl_family *genl_family_find_byname(char *name)
134 const struct genl_family *family;
137 idr_for_each_entry(&genl_fam_idr, family, id)
138 if (strcmp(family->name, name) == 0)
144 struct genl_op_iter {
145 const struct genl_family *family;
146 struct genl_split_ops doit;
147 struct genl_split_ops dumpit;
154 static void genl_op_from_full(const struct genl_family *family,
155 unsigned int i, struct genl_ops *op)
157 *op = family->ops[i];
160 op->maxattr = family->maxattr;
162 op->policy = family->policy;
164 genl_op_fill_in_reject_policy(family, op);
167 static int genl_get_cmd_full(u32 cmd, const struct genl_family *family,
172 for (i = 0; i < family->n_ops; i++)
173 if (family->ops[i].cmd == cmd) {
174 genl_op_from_full(family, i, op);
181 static void genl_op_from_small(const struct genl_family *family,
182 unsigned int i, struct genl_ops *op)
184 memset(op, 0, sizeof(*op));
185 op->doit = family->small_ops[i].doit;
186 op->dumpit = family->small_ops[i].dumpit;
187 op->cmd = family->small_ops[i].cmd;
188 op->internal_flags = family->small_ops[i].internal_flags;
189 op->flags = family->small_ops[i].flags;
190 op->validate = family->small_ops[i].validate;
192 op->maxattr = family->maxattr;
193 op->policy = family->policy;
195 genl_op_fill_in_reject_policy(family, op);
198 static int genl_get_cmd_small(u32 cmd, const struct genl_family *family,
203 for (i = 0; i < family->n_small_ops; i++)
204 if (family->small_ops[i].cmd == cmd) {
205 genl_op_from_small(family, i, op);
212 static void genl_op_from_split(struct genl_op_iter *iter)
214 const struct genl_family *family = iter->family;
217 i = iter->entry_idx - family->n_ops - family->n_small_ops;
219 if (family->split_ops[i + cnt].flags & GENL_CMD_CAP_DO) {
220 iter->doit = family->split_ops[i + cnt];
221 genl_op_fill_in_reject_policy_split(family, &iter->doit);
224 memset(&iter->doit, 0, sizeof(iter->doit));
227 if (i + cnt < family->n_split_ops &&
228 family->split_ops[i + cnt].flags & GENL_CMD_CAP_DUMP) {
229 iter->dumpit = family->split_ops[i + cnt];
230 genl_op_fill_in_reject_policy_split(family, &iter->dumpit);
233 memset(&iter->dumpit, 0, sizeof(iter->dumpit));
237 iter->entry_idx += cnt;
241 genl_get_cmd_split(u32 cmd, u8 flag, const struct genl_family *family,
242 struct genl_split_ops *op)
246 for (i = 0; i < family->n_split_ops; i++)
247 if (family->split_ops[i].cmd == cmd &&
248 family->split_ops[i].flags & flag) {
249 *op = family->split_ops[i];
257 genl_cmd_full_to_split(struct genl_split_ops *op,
258 const struct genl_family *family,
259 const struct genl_ops *full, u8 flags)
261 if ((flags & GENL_CMD_CAP_DO && !full->doit) ||
262 (flags & GENL_CMD_CAP_DUMP && !full->dumpit)) {
263 memset(op, 0, sizeof(*op));
267 if (flags & GENL_CMD_CAP_DUMP) {
268 op->start = full->start;
269 op->dumpit = full->dumpit;
270 op->done = full->done;
272 op->pre_doit = family->pre_doit;
273 op->doit = full->doit;
274 op->post_doit = family->post_doit;
277 if (flags & GENL_CMD_CAP_DUMP &&
278 full->validate & GENL_DONT_VALIDATE_DUMP) {
282 op->policy = full->policy;
283 op->maxattr = full->maxattr;
287 op->internal_flags = full->internal_flags;
288 op->flags = full->flags;
289 op->validate = full->validate;
291 /* Make sure flags include the GENL_CMD_CAP_DO / GENL_CMD_CAP_DUMP */
297 /* Must make sure that op is initialized to 0 on failure */
299 genl_get_cmd(u32 cmd, u8 flags, const struct genl_family *family,
300 struct genl_split_ops *op)
302 struct genl_ops full;
305 err = genl_get_cmd_full(cmd, family, &full);
307 err = genl_get_cmd_small(cmd, family, &full);
308 /* Found one of legacy forms */
310 return genl_cmd_full_to_split(op, family, &full, flags);
312 err = genl_get_cmd_split(cmd, flags, family, op);
314 memset(op, 0, sizeof(*op));
318 /* For policy dumping only, get ops of both do and dump.
319 * Fail if both are missing, genl_get_cmd() will zero-init in case of failure.
322 genl_get_cmd_both(u32 cmd, const struct genl_family *family,
323 struct genl_split_ops *doit, struct genl_split_ops *dumpit)
327 err1 = genl_get_cmd(cmd, GENL_CMD_CAP_DO, family, doit);
328 err2 = genl_get_cmd(cmd, GENL_CMD_CAP_DUMP, family, dumpit);
330 return err1 && err2 ? -ENOENT : 0;
334 genl_op_iter_init(const struct genl_family *family, struct genl_op_iter *iter)
336 iter->family = family;
342 return iter->family->n_ops +
343 iter->family->n_small_ops +
344 iter->family->n_split_ops;
347 static bool genl_op_iter_next(struct genl_op_iter *iter)
349 const struct genl_family *family = iter->family;
350 bool legacy_op = true;
353 if (iter->entry_idx < family->n_ops) {
354 genl_op_from_full(family, iter->entry_idx, &op);
355 } else if (iter->entry_idx < family->n_ops + family->n_small_ops) {
356 genl_op_from_small(family, iter->entry_idx - family->n_ops,
358 } else if (iter->entry_idx <
359 family->n_ops + family->n_small_ops + family->n_split_ops) {
361 /* updates entry_idx */
362 genl_op_from_split(iter);
372 genl_cmd_full_to_split(&iter->doit, family,
373 &op, GENL_CMD_CAP_DO);
374 genl_cmd_full_to_split(&iter->dumpit, family,
375 &op, GENL_CMD_CAP_DUMP);
378 iter->cmd = iter->doit.cmd | iter->dumpit.cmd;
379 iter->flags = iter->doit.flags | iter->dumpit.flags;
385 genl_op_iter_copy(struct genl_op_iter *dst, struct genl_op_iter *src)
390 static unsigned int genl_op_iter_idx(struct genl_op_iter *iter)
392 return iter->cmd_idx;
395 static int genl_allocate_reserve_groups(int n_groups, int *first_id)
397 unsigned long *new_groups;
405 id = find_first_zero_bit(mc_groups,
409 id = find_next_zero_bit(mc_groups,
410 mc_groups_longs * BITS_PER_LONG,
415 i < min_t(int, id + n_groups,
416 mc_groups_longs * BITS_PER_LONG);
418 if (test_bit(i, mc_groups)) {
425 if (id + n_groups > mc_groups_longs * BITS_PER_LONG) {
426 unsigned long new_longs = mc_groups_longs +
427 BITS_TO_LONGS(n_groups);
428 size_t nlen = new_longs * sizeof(unsigned long);
430 if (mc_groups == &mc_group_start) {
431 new_groups = kzalloc(nlen, GFP_KERNEL);
434 mc_groups = new_groups;
435 *mc_groups = mc_group_start;
437 new_groups = krealloc(mc_groups, nlen,
441 mc_groups = new_groups;
442 for (i = 0; i < BITS_TO_LONGS(n_groups); i++)
443 mc_groups[mc_groups_longs + i] = 0;
445 mc_groups_longs = new_longs;
449 for (i = id; i < id + n_groups; i++)
450 set_bit(i, mc_groups);
455 static struct genl_family genl_ctrl;
457 static int genl_validate_assign_mc_groups(struct genl_family *family)
460 int n_groups = family->n_mcgrps;
462 bool groups_allocated = false;
467 for (i = 0; i < n_groups; i++) {
468 const struct genl_multicast_group *grp = &family->mcgrps[i];
470 if (WARN_ON(grp->name[0] == '\0'))
472 if (WARN_ON(!string_is_terminated(grp->name, GENL_NAMSIZ)))
476 /* special-case our own group and hacks */
477 if (family == &genl_ctrl) {
478 first_id = GENL_ID_CTRL;
479 BUG_ON(n_groups != 1);
480 } else if (strcmp(family->name, "NET_DM") == 0) {
482 BUG_ON(n_groups != 1);
483 } else if (family->id == GENL_ID_VFS_DQUOT) {
484 first_id = GENL_ID_VFS_DQUOT;
485 BUG_ON(n_groups != 1);
486 } else if (family->id == GENL_ID_PMCRAID) {
487 first_id = GENL_ID_PMCRAID;
488 BUG_ON(n_groups != 1);
490 groups_allocated = true;
491 err = genl_allocate_reserve_groups(n_groups, &first_id);
496 family->mcgrp_offset = first_id;
498 /* if still initializing, can't and don't need to realloc bitmaps */
499 if (!init_net.genl_sock)
502 if (family->netnsok) {
505 netlink_table_grab();
507 for_each_net_rcu(net) {
508 err = __netlink_change_ngroups(net->genl_sock,
509 mc_groups_longs * BITS_PER_LONG);
512 * No need to roll back, can only fail if
513 * memory allocation fails and then the
514 * number of _possible_ groups has been
515 * increased on some sockets which is ok.
521 netlink_table_ungrab();
523 err = netlink_change_ngroups(init_net.genl_sock,
524 mc_groups_longs * BITS_PER_LONG);
527 if (groups_allocated && err) {
528 for (i = 0; i < family->n_mcgrps; i++)
529 clear_bit(family->mcgrp_offset + i, mc_groups);
535 static void genl_unregister_mc_groups(const struct genl_family *family)
540 netlink_table_grab();
542 for_each_net_rcu(net) {
543 for (i = 0; i < family->n_mcgrps; i++)
544 __netlink_clear_multicast_users(
545 net->genl_sock, family->mcgrp_offset + i);
548 netlink_table_ungrab();
550 for (i = 0; i < family->n_mcgrps; i++) {
551 int grp_id = family->mcgrp_offset + i;
554 clear_bit(grp_id, mc_groups);
555 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family,
556 &family->mcgrps[i], grp_id);
560 static bool genl_split_op_check(const struct genl_split_ops *op)
562 if (WARN_ON(hweight8(op->flags & (GENL_CMD_CAP_DO |
563 GENL_CMD_CAP_DUMP)) != 1))
568 static int genl_validate_ops(const struct genl_family *family)
570 struct genl_op_iter i, j;
573 if (WARN_ON(family->n_ops && !family->ops) ||
574 WARN_ON(family->n_small_ops && !family->small_ops) ||
575 WARN_ON(family->n_split_ops && !family->split_ops))
578 for (genl_op_iter_init(family, &i); genl_op_iter_next(&i); ) {
579 if (!(i.flags & (GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP)))
582 if (WARN_ON(i.cmd >= family->resv_start_op &&
583 (i.doit.validate || i.dumpit.validate)))
586 genl_op_iter_copy(&j, &i);
587 while (genl_op_iter_next(&j)) {
593 if (family->n_split_ops) {
594 if (genl_split_op_check(&family->split_ops[0]))
598 for (s = 1; s < family->n_split_ops; s++) {
599 const struct genl_split_ops *a, *b;
601 a = &family->split_ops[s - 1];
602 b = &family->split_ops[s];
604 if (genl_split_op_check(b))
607 /* Check sort order */
608 if (a->cmd < b->cmd) {
610 } else if (a->cmd > b->cmd) {
615 if (a->internal_flags != b->internal_flags ||
616 ((a->flags ^ b->flags) & ~(GENL_CMD_CAP_DO |
617 GENL_CMD_CAP_DUMP))) {
622 if ((a->flags & GENL_CMD_CAP_DO) &&
623 (b->flags & GENL_CMD_CAP_DUMP))
634 * genl_register_family - register a generic netlink family
635 * @family: generic netlink family
637 * Registers the specified family after validating it first. Only one
638 * family may be registered with the same family name or identifier.
640 * The family's ops, multicast groups and module pointer must already
643 * Return 0 on success or a negative error code.
645 int genl_register_family(struct genl_family *family)
648 int start = GENL_START_ALLOC, end = GENL_MAX_ID;
650 err = genl_validate_ops(family);
656 if (genl_family_find_byname(family->name)) {
662 * Sadly, a few cases need to be special-cased
663 * due to them having previously abused the API
664 * and having used their family ID also as their
665 * multicast group ID, so we use reserved IDs
666 * for both to be sure we can do that mapping.
668 if (family == &genl_ctrl) {
669 /* and this needs to be special for initial family lookups */
670 start = end = GENL_ID_CTRL;
671 } else if (strcmp(family->name, "pmcraid") == 0) {
672 start = end = GENL_ID_PMCRAID;
673 } else if (strcmp(family->name, "VFS_DQUOT") == 0) {
674 start = end = GENL_ID_VFS_DQUOT;
677 family->id = idr_alloc_cyclic(&genl_fam_idr, family,
678 start, end + 1, GFP_KERNEL);
679 if (family->id < 0) {
684 err = genl_validate_assign_mc_groups(family);
690 /* send all events */
691 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
692 for (i = 0; i < family->n_mcgrps; i++)
693 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
694 &family->mcgrps[i], family->mcgrp_offset + i);
699 idr_remove(&genl_fam_idr, family->id);
704 EXPORT_SYMBOL(genl_register_family);
707 * genl_unregister_family - unregister generic netlink family
708 * @family: generic netlink family
710 * Unregisters the specified family.
712 * Returns 0 on success or a negative error code.
714 int genl_unregister_family(const struct genl_family *family)
718 if (!genl_family_find_byid(family->id)) {
723 genl_unregister_mc_groups(family);
725 idr_remove(&genl_fam_idr, family->id);
728 wait_event(genl_sk_destructing_waitq,
729 atomic_read(&genl_sk_destructing_cnt) == 0);
732 genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
736 EXPORT_SYMBOL(genl_unregister_family);
739 * genlmsg_put - Add generic netlink header to netlink message
740 * @skb: socket buffer holding the message
741 * @portid: netlink portid the message is addressed to
742 * @seq: sequence number (usually the one of the sender)
743 * @family: generic netlink family
744 * @flags: netlink message flags
745 * @cmd: generic netlink command
747 * Returns pointer to user specific header
749 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
750 const struct genl_family *family, int flags, u8 cmd)
752 struct nlmsghdr *nlh;
753 struct genlmsghdr *hdr;
755 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
756 family->hdrsize, flags);
760 hdr = nlmsg_data(nlh);
762 hdr->version = family->version;
765 return (char *) hdr + GENL_HDRLEN;
767 EXPORT_SYMBOL(genlmsg_put);
769 static struct genl_dumpit_info *genl_dumpit_info_alloc(void)
771 return kmalloc(sizeof(struct genl_dumpit_info), GFP_KERNEL);
774 static void genl_dumpit_info_free(const struct genl_dumpit_info *info)
779 static struct nlattr **
780 genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
781 struct nlmsghdr *nlh,
782 struct netlink_ext_ack *extack,
783 const struct genl_split_ops *ops,
785 enum genl_validate_flags no_strict_flag)
787 enum netlink_validation validate = ops->validate & no_strict_flag ?
788 NL_VALIDATE_LIBERAL :
790 struct nlattr **attrbuf;
796 attrbuf = kmalloc_array(ops->maxattr + 1,
797 sizeof(struct nlattr *), GFP_KERNEL);
799 return ERR_PTR(-ENOMEM);
801 err = __nlmsg_parse(nlh, hdrlen, attrbuf, ops->maxattr, ops->policy,
810 static void genl_family_rcv_msg_attrs_free(struct nlattr **attrbuf)
815 struct genl_start_context {
816 const struct genl_family *family;
817 struct nlmsghdr *nlh;
818 struct netlink_ext_ack *extack;
819 const struct genl_split_ops *ops;
823 static int genl_start(struct netlink_callback *cb)
825 struct genl_start_context *ctx = cb->data;
826 const struct genl_split_ops *ops;
827 struct genl_dumpit_info *info;
828 struct nlattr **attrs = NULL;
832 if (!(ops->validate & GENL_DONT_VALIDATE_DUMP) &&
833 ctx->nlh->nlmsg_len < nlmsg_msg_size(ctx->hdrlen))
836 attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
838 GENL_DONT_VALIDATE_DUMP_STRICT);
840 return PTR_ERR(attrs);
842 info = genl_dumpit_info_alloc();
844 genl_family_rcv_msg_attrs_free(attrs);
848 info->info.family = ctx->family;
849 info->info.snd_seq = cb->nlh->nlmsg_seq;
850 info->info.snd_portid = NETLINK_CB(cb->skb).portid;
851 info->info.nlhdr = cb->nlh;
852 info->info.genlhdr = nlmsg_data(cb->nlh);
853 info->info.attrs = attrs;
854 genl_info_net_set(&info->info, sock_net(cb->skb->sk));
855 info->info.extack = cb->extack;
856 memset(&info->info.user_ptr, 0, sizeof(info->info.user_ptr));
860 genl_op_lock(ctx->family);
862 genl_op_unlock(ctx->family);
866 genl_family_rcv_msg_attrs_free(info->info.attrs);
867 genl_dumpit_info_free(info);
873 static int genl_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
875 struct genl_dumpit_info *dump_info = cb->data;
876 const struct genl_split_ops *ops = &dump_info->op;
877 struct genl_info *info = &dump_info->info;
880 info->extack = cb->extack;
882 genl_op_lock(info->family);
883 rc = ops->dumpit(skb, cb);
884 genl_op_unlock(info->family);
888 static int genl_done(struct netlink_callback *cb)
890 struct genl_dumpit_info *dump_info = cb->data;
891 const struct genl_split_ops *ops = &dump_info->op;
892 struct genl_info *info = &dump_info->info;
895 info->extack = cb->extack;
898 genl_op_lock(info->family);
900 genl_op_unlock(info->family);
902 genl_family_rcv_msg_attrs_free(info->attrs);
903 genl_dumpit_info_free(dump_info);
907 static int genl_family_rcv_msg_dumpit(const struct genl_family *family,
909 struct nlmsghdr *nlh,
910 struct netlink_ext_ack *extack,
911 const struct genl_split_ops *ops,
912 int hdrlen, struct net *net)
914 struct genl_start_context ctx;
915 struct netlink_dump_control c = {
916 .module = family->module,
931 genl_op_unlock(family);
932 err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
933 genl_op_lock(family);
938 static int genl_family_rcv_msg_doit(const struct genl_family *family,
940 struct nlmsghdr *nlh,
941 struct netlink_ext_ack *extack,
942 const struct genl_split_ops *ops,
943 int hdrlen, struct net *net)
945 struct nlattr **attrbuf;
946 struct genl_info info;
949 attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
951 GENL_DONT_VALIDATE_STRICT);
953 return PTR_ERR(attrbuf);
955 info.snd_seq = nlh->nlmsg_seq;
956 info.snd_portid = NETLINK_CB(skb).portid;
957 info.family = family;
959 info.genlhdr = nlmsg_data(nlh);
960 info.attrs = attrbuf;
961 info.extack = extack;
962 genl_info_net_set(&info, net);
963 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
966 err = ops->pre_doit(ops, skb, &info);
971 err = ops->doit(skb, &info);
974 ops->post_doit(ops, skb, &info);
977 genl_family_rcv_msg_attrs_free(attrbuf);
982 static int genl_header_check(const struct genl_family *family,
983 struct nlmsghdr *nlh, struct genlmsghdr *hdr,
984 struct netlink_ext_ack *extack)
988 /* Only for commands added after we started validating */
989 if (hdr->cmd < family->resv_start_op)
993 NL_SET_ERR_MSG(extack, "genlmsghdr.reserved field is not 0");
997 /* Old netlink flags have pretty loose semantics, allow only the flags
998 * consumed by the core where we can enforce the meaning.
1000 flags = nlh->nlmsg_flags;
1001 if ((flags & NLM_F_DUMP) == NLM_F_DUMP) /* DUMP is 2 bits */
1002 flags &= ~NLM_F_DUMP;
1003 if (flags & ~(NLM_F_REQUEST | NLM_F_ACK | NLM_F_ECHO)) {
1004 NL_SET_ERR_MSG(extack,
1005 "ambiguous or reserved bits set in nlmsg_flags");
1012 static int genl_family_rcv_msg(const struct genl_family *family,
1013 struct sk_buff *skb,
1014 struct nlmsghdr *nlh,
1015 struct netlink_ext_ack *extack)
1017 struct net *net = sock_net(skb->sk);
1018 struct genlmsghdr *hdr = nlmsg_data(nlh);
1019 struct genl_split_ops op;
1023 /* this family doesn't exist in this netns */
1024 if (!family->netnsok && !net_eq(net, &init_net))
1027 hdrlen = GENL_HDRLEN + family->hdrsize;
1028 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
1031 if (genl_header_check(family, nlh, hdr, extack))
1034 flags = (nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP ?
1035 GENL_CMD_CAP_DUMP : GENL_CMD_CAP_DO;
1036 if (genl_get_cmd(hdr->cmd, flags, family, &op))
1039 if ((op.flags & GENL_ADMIN_PERM) &&
1040 !netlink_capable(skb, CAP_NET_ADMIN))
1043 if ((op.flags & GENL_UNS_ADMIN_PERM) &&
1044 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1047 if (flags & GENL_CMD_CAP_DUMP)
1048 return genl_family_rcv_msg_dumpit(family, skb, nlh, extack,
1051 return genl_family_rcv_msg_doit(family, skb, nlh, extack,
1055 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
1056 struct netlink_ext_ack *extack)
1058 const struct genl_family *family;
1061 family = genl_family_find_byid(nlh->nlmsg_type);
1065 genl_op_lock(family);
1066 err = genl_family_rcv_msg(family, skb, nlh, extack);
1067 genl_op_unlock(family);
1072 static void genl_rcv(struct sk_buff *skb)
1074 down_read(&cb_lock);
1075 netlink_rcv_skb(skb, &genl_rcv_msg);
1079 /**************************************************************************
1081 **************************************************************************/
1083 static struct genl_family genl_ctrl;
1085 static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
1086 u32 flags, struct sk_buff *skb, u8 cmd)
1088 struct genl_op_iter i;
1091 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
1095 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
1096 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
1097 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
1098 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
1099 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
1100 goto nla_put_failure;
1102 if (genl_op_iter_init(family, &i)) {
1103 struct nlattr *nla_ops;
1105 nla_ops = nla_nest_start_noflag(skb, CTRL_ATTR_OPS);
1106 if (nla_ops == NULL)
1107 goto nla_put_failure;
1109 while (genl_op_iter_next(&i)) {
1110 struct nlattr *nest;
1114 if (i.doit.policy || i.dumpit.policy)
1115 op_flags |= GENL_CMD_CAP_HASPOL;
1117 nest = nla_nest_start_noflag(skb, genl_op_iter_idx(&i));
1119 goto nla_put_failure;
1121 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, i.cmd) ||
1122 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
1123 goto nla_put_failure;
1125 nla_nest_end(skb, nest);
1128 nla_nest_end(skb, nla_ops);
1131 if (family->n_mcgrps) {
1132 struct nlattr *nla_grps;
1135 nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
1136 if (nla_grps == NULL)
1137 goto nla_put_failure;
1139 for (i = 0; i < family->n_mcgrps; i++) {
1140 struct nlattr *nest;
1141 const struct genl_multicast_group *grp;
1143 grp = &family->mcgrps[i];
1145 nest = nla_nest_start_noflag(skb, i + 1);
1147 goto nla_put_failure;
1149 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
1150 family->mcgrp_offset + i) ||
1151 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
1153 goto nla_put_failure;
1155 nla_nest_end(skb, nest);
1157 nla_nest_end(skb, nla_grps);
1160 genlmsg_end(skb, hdr);
1164 genlmsg_cancel(skb, hdr);
1168 static int ctrl_fill_mcgrp_info(const struct genl_family *family,
1169 const struct genl_multicast_group *grp,
1170 int grp_id, u32 portid, u32 seq, u32 flags,
1171 struct sk_buff *skb, u8 cmd)
1174 struct nlattr *nla_grps;
1175 struct nlattr *nest;
1177 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
1181 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
1182 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
1183 goto nla_put_failure;
1185 nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
1186 if (nla_grps == NULL)
1187 goto nla_put_failure;
1189 nest = nla_nest_start_noflag(skb, 1);
1191 goto nla_put_failure;
1193 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
1194 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
1196 goto nla_put_failure;
1198 nla_nest_end(skb, nest);
1199 nla_nest_end(skb, nla_grps);
1201 genlmsg_end(skb, hdr);
1205 genlmsg_cancel(skb, hdr);
1209 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
1212 struct genl_family *rt;
1213 struct net *net = sock_net(skb->sk);
1214 int fams_to_skip = cb->args[0];
1217 idr_for_each_entry(&genl_fam_idr, rt, id) {
1218 if (!rt->netnsok && !net_eq(net, &init_net))
1221 if (n++ < fams_to_skip)
1224 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
1225 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1226 skb, CTRL_CMD_NEWFAMILY) < 0) {
1236 static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
1237 u32 portid, int seq, u8 cmd)
1239 struct sk_buff *skb;
1242 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1244 return ERR_PTR(-ENOBUFS);
1246 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
1249 return ERR_PTR(err);
1255 static struct sk_buff *
1256 ctrl_build_mcgrp_msg(const struct genl_family *family,
1257 const struct genl_multicast_group *grp,
1258 int grp_id, u32 portid, int seq, u8 cmd)
1260 struct sk_buff *skb;
1263 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1265 return ERR_PTR(-ENOBUFS);
1267 err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
1271 return ERR_PTR(err);
1277 static const struct nla_policy ctrl_policy_family[] = {
1278 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
1279 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
1280 .len = GENL_NAMSIZ - 1 },
1283 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
1285 struct sk_buff *msg;
1286 const struct genl_family *res = NULL;
1289 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
1290 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
1291 res = genl_family_find_byid(id);
1295 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
1298 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
1299 res = genl_family_find_byname(name);
1300 #ifdef CONFIG_MODULES
1304 request_module("net-pf-%d-proto-%d-family-%s",
1305 PF_NETLINK, NETLINK_GENERIC, name);
1306 down_read(&cb_lock);
1308 res = genl_family_find_byname(name);
1317 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
1318 /* family doesn't exist here */
1322 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
1323 CTRL_CMD_NEWFAMILY);
1325 return PTR_ERR(msg);
1327 return genlmsg_reply(msg, info);
1330 static int genl_ctrl_event(int event, const struct genl_family *family,
1331 const struct genl_multicast_group *grp,
1334 struct sk_buff *msg;
1336 /* genl is still initialising */
1337 if (!init_net.genl_sock)
1341 case CTRL_CMD_NEWFAMILY:
1342 case CTRL_CMD_DELFAMILY:
1344 msg = ctrl_build_family_msg(family, 0, 0, event);
1346 case CTRL_CMD_NEWMCAST_GRP:
1347 case CTRL_CMD_DELMCAST_GRP:
1349 msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
1356 return PTR_ERR(msg);
1358 if (!family->netnsok) {
1359 genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
1363 genlmsg_multicast_allns(&genl_ctrl, msg, 0,
1371 struct ctrl_dump_policy_ctx {
1372 struct netlink_policy_dump_state *state;
1373 const struct genl_family *rt;
1374 struct genl_op_iter *op_iter;
1381 static const struct nla_policy ctrl_policy_policy[] = {
1382 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
1383 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
1384 .len = GENL_NAMSIZ - 1 },
1385 [CTRL_ATTR_OP] = { .type = NLA_U32 },
1388 static int ctrl_dumppolicy_start(struct netlink_callback *cb)
1390 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
1391 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1392 struct nlattr **tb = info->info.attrs;
1393 const struct genl_family *rt;
1394 struct genl_op_iter i;
1397 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
1399 if (!tb[CTRL_ATTR_FAMILY_ID] && !tb[CTRL_ATTR_FAMILY_NAME])
1402 if (tb[CTRL_ATTR_FAMILY_ID]) {
1403 ctx->fam_id = nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
1405 rt = genl_family_find_byname(
1406 nla_data(tb[CTRL_ATTR_FAMILY_NAME]));
1409 ctx->fam_id = rt->id;
1412 rt = genl_family_find_byid(ctx->fam_id);
1418 if (tb[CTRL_ATTR_OP]) {
1419 struct genl_split_ops doit, dump;
1421 ctx->single_op = true;
1422 ctx->op = nla_get_u32(tb[CTRL_ATTR_OP]);
1424 err = genl_get_cmd_both(ctx->op, rt, &doit, &dump);
1426 NL_SET_BAD_ATTR(cb->extack, tb[CTRL_ATTR_OP]);
1431 err = netlink_policy_dump_add_policy(&ctx->state,
1435 goto err_free_state;
1438 err = netlink_policy_dump_add_policy(&ctx->state,
1442 goto err_free_state;
1452 ctx->op_iter = kmalloc(sizeof(*ctx->op_iter), GFP_KERNEL);
1456 genl_op_iter_init(rt, ctx->op_iter);
1457 ctx->dump_map = genl_op_iter_next(ctx->op_iter);
1459 for (genl_op_iter_init(rt, &i); genl_op_iter_next(&i); ) {
1460 if (i.doit.policy) {
1461 err = netlink_policy_dump_add_policy(&ctx->state,
1465 goto err_free_state;
1467 if (i.dumpit.policy) {
1468 err = netlink_policy_dump_add_policy(&ctx->state,
1472 goto err_free_state;
1478 goto err_free_op_iter;
1483 netlink_policy_dump_free(ctx->state);
1485 kfree(ctx->op_iter);
1489 static void *ctrl_dumppolicy_prep(struct sk_buff *skb,
1490 struct netlink_callback *cb)
1492 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1495 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1496 cb->nlh->nlmsg_seq, &genl_ctrl,
1497 NLM_F_MULTI, CTRL_CMD_GETPOLICY);
1501 if (nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, ctx->fam_id))
1507 static int ctrl_dumppolicy_put_op(struct sk_buff *skb,
1508 struct netlink_callback *cb,
1509 struct genl_split_ops *doit,
1510 struct genl_split_ops *dumpit)
1512 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1513 struct nlattr *nest_pol, *nest_op;
1517 /* skip if we have nothing to show */
1518 if (!doit->policy && !dumpit->policy)
1521 hdr = ctrl_dumppolicy_prep(skb, cb);
1525 nest_pol = nla_nest_start(skb, CTRL_ATTR_OP_POLICY);
1529 nest_op = nla_nest_start(skb, doit->cmd);
1534 idx = netlink_policy_dump_get_policy_idx(ctx->state,
1538 if (nla_put_u32(skb, CTRL_ATTR_POLICY_DO, idx))
1541 if (dumpit->policy) {
1542 idx = netlink_policy_dump_get_policy_idx(ctx->state,
1546 if (nla_put_u32(skb, CTRL_ATTR_POLICY_DUMP, idx))
1550 nla_nest_end(skb, nest_op);
1551 nla_nest_end(skb, nest_pol);
1552 genlmsg_end(skb, hdr);
1556 genlmsg_cancel(skb, hdr);
1560 static int ctrl_dumppolicy(struct sk_buff *skb, struct netlink_callback *cb)
1562 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1565 if (ctx->dump_map) {
1566 if (ctx->single_op) {
1567 struct genl_split_ops doit, dumpit;
1569 if (WARN_ON(genl_get_cmd_both(ctx->op, ctx->rt,
1573 if (ctrl_dumppolicy_put_op(skb, cb, &doit, &dumpit))
1576 /* done with the per-op policy index list */
1580 while (ctx->dump_map) {
1581 if (ctrl_dumppolicy_put_op(skb, cb,
1582 &ctx->op_iter->doit,
1583 &ctx->op_iter->dumpit))
1586 ctx->dump_map = genl_op_iter_next(ctx->op_iter);
1590 while (netlink_policy_dump_loop(ctx->state)) {
1591 struct nlattr *nest;
1593 hdr = ctrl_dumppolicy_prep(skb, cb);
1595 goto nla_put_failure;
1597 nest = nla_nest_start(skb, CTRL_ATTR_POLICY);
1599 goto nla_put_failure;
1601 if (netlink_policy_dump_write(skb, ctx->state))
1602 goto nla_put_failure;
1604 nla_nest_end(skb, nest);
1606 genlmsg_end(skb, hdr);
1612 genlmsg_cancel(skb, hdr);
1616 static int ctrl_dumppolicy_done(struct netlink_callback *cb)
1618 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1620 kfree(ctx->op_iter);
1621 netlink_policy_dump_free(ctx->state);
1625 static const struct genl_split_ops genl_ctrl_ops[] = {
1627 .cmd = CTRL_CMD_GETFAMILY,
1628 .validate = GENL_DONT_VALIDATE_STRICT,
1629 .policy = ctrl_policy_family,
1630 .maxattr = ARRAY_SIZE(ctrl_policy_family) - 1,
1631 .doit = ctrl_getfamily,
1632 .flags = GENL_CMD_CAP_DO,
1635 .cmd = CTRL_CMD_GETFAMILY,
1636 .validate = GENL_DONT_VALIDATE_DUMP,
1637 .policy = ctrl_policy_family,
1638 .maxattr = ARRAY_SIZE(ctrl_policy_family) - 1,
1639 .dumpit = ctrl_dumpfamily,
1640 .flags = GENL_CMD_CAP_DUMP,
1643 .cmd = CTRL_CMD_GETPOLICY,
1644 .policy = ctrl_policy_policy,
1645 .maxattr = ARRAY_SIZE(ctrl_policy_policy) - 1,
1646 .start = ctrl_dumppolicy_start,
1647 .dumpit = ctrl_dumppolicy,
1648 .done = ctrl_dumppolicy_done,
1649 .flags = GENL_CMD_CAP_DUMP,
1653 static const struct genl_multicast_group genl_ctrl_groups[] = {
1654 { .name = "notify", },
1657 static struct genl_family genl_ctrl __ro_after_init = {
1658 .module = THIS_MODULE,
1659 .split_ops = genl_ctrl_ops,
1660 .n_split_ops = ARRAY_SIZE(genl_ctrl_ops),
1661 .resv_start_op = CTRL_CMD_GETPOLICY + 1,
1662 .mcgrps = genl_ctrl_groups,
1663 .n_mcgrps = ARRAY_SIZE(genl_ctrl_groups),
1670 static int genl_bind(struct net *net, int group)
1672 const struct genl_family *family;
1676 down_read(&cb_lock);
1678 idr_for_each_entry(&genl_fam_idr, family, id) {
1679 const struct genl_multicast_group *grp;
1682 if (family->n_mcgrps == 0)
1685 i = group - family->mcgrp_offset;
1686 if (i < 0 || i >= family->n_mcgrps)
1689 grp = &family->mcgrps[i];
1690 if ((grp->flags & GENL_UNS_ADMIN_PERM) &&
1691 !ns_capable(net->user_ns, CAP_NET_ADMIN))
1701 static int __net_init genl_pernet_init(struct net *net)
1703 struct netlink_kernel_cfg cfg = {
1705 .flags = NL_CFG_F_NONROOT_RECV,
1709 /* we'll bump the group number right afterwards */
1710 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
1712 if (!net->genl_sock && net_eq(net, &init_net))
1713 panic("GENL: Cannot initialize generic netlink\n");
1715 if (!net->genl_sock)
1721 static void __net_exit genl_pernet_exit(struct net *net)
1723 netlink_kernel_release(net->genl_sock);
1724 net->genl_sock = NULL;
1727 static struct pernet_operations genl_pernet_ops = {
1728 .init = genl_pernet_init,
1729 .exit = genl_pernet_exit,
1732 static int __init genl_init(void)
1736 err = genl_register_family(&genl_ctrl);
1740 err = register_pernet_subsys(&genl_pernet_ops);
1747 panic("GENL: Cannot register controller: %d\n", err);
1750 core_initcall(genl_init);
1752 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
1755 struct sk_buff *tmp;
1756 struct net *net, *prev = NULL;
1757 bool delivered = false;
1760 for_each_net_rcu(net) {
1762 tmp = skb_clone(skb, flags);
1767 err = nlmsg_multicast(prev->genl_sock, tmp,
1768 portid, group, flags);
1771 else if (err != -ESRCH)
1778 err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1781 else if (err != -ESRCH)
1783 return delivered ? 0 : -ESRCH;
1789 int genlmsg_multicast_allns(const struct genl_family *family,
1790 struct sk_buff *skb, u32 portid,
1791 unsigned int group, gfp_t flags)
1793 if (WARN_ON_ONCE(group >= family->n_mcgrps))
1796 group = family->mcgrp_offset + group;
1797 return genlmsg_mcast(skb, portid, group, flags);
1799 EXPORT_SYMBOL(genlmsg_multicast_allns);
1801 void genl_notify(const struct genl_family *family, struct sk_buff *skb,
1802 struct genl_info *info, u32 group, gfp_t flags)
1804 struct net *net = genl_info_net(info);
1805 struct sock *sk = net->genl_sock;
1807 if (WARN_ON_ONCE(group >= family->n_mcgrps))
1810 group = family->mcgrp_offset + group;
1811 nlmsg_notify(sk, skb, info->snd_portid, group,
1812 nlmsg_report(info->nlhdr), flags);
1814 EXPORT_SYMBOL(genl_notify);