Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * NETLINK      Generic Netlink Family
0004  *
0005  *      Authors:    Jamal Hadi Salim
0006  *              Thomas Graf <tgraf@suug.ch>
0007  *              Johannes Berg <johannes@sipsolutions.net>
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/slab.h>
0013 #include <linux/errno.h>
0014 #include <linux/types.h>
0015 #include <linux/socket.h>
0016 #include <linux/string.h>
0017 #include <linux/skbuff.h>
0018 #include <linux/mutex.h>
0019 #include <linux/bitmap.h>
0020 #include <linux/rwsem.h>
0021 #include <linux/idr.h>
0022 #include <net/sock.h>
0023 #include <net/genetlink.h>
0024 
0025 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
0026 static DECLARE_RWSEM(cb_lock);
0027 
0028 atomic_t genl_sk_destructing_cnt = ATOMIC_INIT(0);
0029 DECLARE_WAIT_QUEUE_HEAD(genl_sk_destructing_waitq);
0030 
0031 void genl_lock(void)
0032 {
0033     mutex_lock(&genl_mutex);
0034 }
0035 EXPORT_SYMBOL(genl_lock);
0036 
0037 void genl_unlock(void)
0038 {
0039     mutex_unlock(&genl_mutex);
0040 }
0041 EXPORT_SYMBOL(genl_unlock);
0042 
0043 static void genl_lock_all(void)
0044 {
0045     down_write(&cb_lock);
0046     genl_lock();
0047 }
0048 
0049 static void genl_unlock_all(void)
0050 {
0051     genl_unlock();
0052     up_write(&cb_lock);
0053 }
0054 
0055 static DEFINE_IDR(genl_fam_idr);
0056 
0057 /*
0058  * Bitmap of multicast groups that are currently in use.
0059  *
0060  * To avoid an allocation at boot of just one unsigned long,
0061  * declare it global instead.
0062  * Bit 0 is marked as already used since group 0 is invalid.
0063  * Bit 1 is marked as already used since the drop-monitor code
0064  * abuses the API and thinks it can statically use group 1.
0065  * That group will typically conflict with other groups that
0066  * any proper users use.
0067  * Bit 16 is marked as used since it's used for generic netlink
0068  * and the code no longer marks pre-reserved IDs as used.
0069  * Bit 17 is marked as already used since the VFS quota code
0070  * also abused this API and relied on family == group ID, we
0071  * cater to that by giving it a static family and group ID.
0072  * Bit 18 is marked as already used since the PMCRAID driver
0073  * did the same thing as the VFS quota code (maybe copied?)
0074  */
0075 static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) |
0076                       BIT(GENL_ID_VFS_DQUOT) |
0077                       BIT(GENL_ID_PMCRAID);
0078 static unsigned long *mc_groups = &mc_group_start;
0079 static unsigned long mc_groups_longs = 1;
0080 
0081 static int genl_ctrl_event(int event, const struct genl_family *family,
0082                const struct genl_multicast_group *grp,
0083                int grp_id);
0084 
0085 static const struct genl_family *genl_family_find_byid(unsigned int id)
0086 {
0087     return idr_find(&genl_fam_idr, id);
0088 }
0089 
0090 static const struct genl_family *genl_family_find_byname(char *name)
0091 {
0092     const struct genl_family *family;
0093     unsigned int id;
0094 
0095     idr_for_each_entry(&genl_fam_idr, family, id)
0096         if (strcmp(family->name, name) == 0)
0097             return family;
0098 
0099     return NULL;
0100 }
0101 
0102 static int genl_get_cmd_cnt(const struct genl_family *family)
0103 {
0104     return family->n_ops + family->n_small_ops;
0105 }
0106 
0107 static void genl_op_from_full(const struct genl_family *family,
0108                   unsigned int i, struct genl_ops *op)
0109 {
0110     *op = family->ops[i];
0111 
0112     if (!op->maxattr)
0113         op->maxattr = family->maxattr;
0114     if (!op->policy)
0115         op->policy = family->policy;
0116 }
0117 
0118 static int genl_get_cmd_full(u32 cmd, const struct genl_family *family,
0119                  struct genl_ops *op)
0120 {
0121     int i;
0122 
0123     for (i = 0; i < family->n_ops; i++)
0124         if (family->ops[i].cmd == cmd) {
0125             genl_op_from_full(family, i, op);
0126             return 0;
0127         }
0128 
0129     return -ENOENT;
0130 }
0131 
0132 static void genl_op_from_small(const struct genl_family *family,
0133                    unsigned int i, struct genl_ops *op)
0134 {
0135     memset(op, 0, sizeof(*op));
0136     op->doit    = family->small_ops[i].doit;
0137     op->dumpit  = family->small_ops[i].dumpit;
0138     op->cmd     = family->small_ops[i].cmd;
0139     op->internal_flags = family->small_ops[i].internal_flags;
0140     op->flags   = family->small_ops[i].flags;
0141     op->validate    = family->small_ops[i].validate;
0142 
0143     op->maxattr = family->maxattr;
0144     op->policy = family->policy;
0145 }
0146 
0147 static int genl_get_cmd_small(u32 cmd, const struct genl_family *family,
0148                   struct genl_ops *op)
0149 {
0150     int i;
0151 
0152     for (i = 0; i < family->n_small_ops; i++)
0153         if (family->small_ops[i].cmd == cmd) {
0154             genl_op_from_small(family, i, op);
0155             return 0;
0156         }
0157 
0158     return -ENOENT;
0159 }
0160 
0161 static int genl_get_cmd(u32 cmd, const struct genl_family *family,
0162             struct genl_ops *op)
0163 {
0164     if (!genl_get_cmd_full(cmd, family, op))
0165         return 0;
0166     return genl_get_cmd_small(cmd, family, op);
0167 }
0168 
0169 static void genl_get_cmd_by_index(unsigned int i,
0170                   const struct genl_family *family,
0171                   struct genl_ops *op)
0172 {
0173     if (i < family->n_ops)
0174         genl_op_from_full(family, i, op);
0175     else if (i < family->n_ops + family->n_small_ops)
0176         genl_op_from_small(family, i - family->n_ops, op);
0177     else
0178         WARN_ON_ONCE(1);
0179 }
0180 
0181 static int genl_allocate_reserve_groups(int n_groups, int *first_id)
0182 {
0183     unsigned long *new_groups;
0184     int start = 0;
0185     int i;
0186     int id;
0187     bool fits;
0188 
0189     do {
0190         if (start == 0)
0191             id = find_first_zero_bit(mc_groups,
0192                          mc_groups_longs *
0193                          BITS_PER_LONG);
0194         else
0195             id = find_next_zero_bit(mc_groups,
0196                         mc_groups_longs * BITS_PER_LONG,
0197                         start);
0198 
0199         fits = true;
0200         for (i = id;
0201              i < min_t(int, id + n_groups,
0202                    mc_groups_longs * BITS_PER_LONG);
0203              i++) {
0204             if (test_bit(i, mc_groups)) {
0205                 start = i;
0206                 fits = false;
0207                 break;
0208             }
0209         }
0210 
0211         if (id + n_groups > mc_groups_longs * BITS_PER_LONG) {
0212             unsigned long new_longs = mc_groups_longs +
0213                           BITS_TO_LONGS(n_groups);
0214             size_t nlen = new_longs * sizeof(unsigned long);
0215 
0216             if (mc_groups == &mc_group_start) {
0217                 new_groups = kzalloc(nlen, GFP_KERNEL);
0218                 if (!new_groups)
0219                     return -ENOMEM;
0220                 mc_groups = new_groups;
0221                 *mc_groups = mc_group_start;
0222             } else {
0223                 new_groups = krealloc(mc_groups, nlen,
0224                               GFP_KERNEL);
0225                 if (!new_groups)
0226                     return -ENOMEM;
0227                 mc_groups = new_groups;
0228                 for (i = 0; i < BITS_TO_LONGS(n_groups); i++)
0229                     mc_groups[mc_groups_longs + i] = 0;
0230             }
0231             mc_groups_longs = new_longs;
0232         }
0233     } while (!fits);
0234 
0235     for (i = id; i < id + n_groups; i++)
0236         set_bit(i, mc_groups);
0237     *first_id = id;
0238     return 0;
0239 }
0240 
0241 static struct genl_family genl_ctrl;
0242 
0243 static int genl_validate_assign_mc_groups(struct genl_family *family)
0244 {
0245     int first_id;
0246     int n_groups = family->n_mcgrps;
0247     int err = 0, i;
0248     bool groups_allocated = false;
0249 
0250     if (!n_groups)
0251         return 0;
0252 
0253     for (i = 0; i < n_groups; i++) {
0254         const struct genl_multicast_group *grp = &family->mcgrps[i];
0255 
0256         if (WARN_ON(grp->name[0] == '\0'))
0257             return -EINVAL;
0258         if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL))
0259             return -EINVAL;
0260     }
0261 
0262     /* special-case our own group and hacks */
0263     if (family == &genl_ctrl) {
0264         first_id = GENL_ID_CTRL;
0265         BUG_ON(n_groups != 1);
0266     } else if (strcmp(family->name, "NET_DM") == 0) {
0267         first_id = 1;
0268         BUG_ON(n_groups != 1);
0269     } else if (family->id == GENL_ID_VFS_DQUOT) {
0270         first_id = GENL_ID_VFS_DQUOT;
0271         BUG_ON(n_groups != 1);
0272     } else if (family->id == GENL_ID_PMCRAID) {
0273         first_id = GENL_ID_PMCRAID;
0274         BUG_ON(n_groups != 1);
0275     } else {
0276         groups_allocated = true;
0277         err = genl_allocate_reserve_groups(n_groups, &first_id);
0278         if (err)
0279             return err;
0280     }
0281 
0282     family->mcgrp_offset = first_id;
0283 
0284     /* if still initializing, can't and don't need to realloc bitmaps */
0285     if (!init_net.genl_sock)
0286         return 0;
0287 
0288     if (family->netnsok) {
0289         struct net *net;
0290 
0291         netlink_table_grab();
0292         rcu_read_lock();
0293         for_each_net_rcu(net) {
0294             err = __netlink_change_ngroups(net->genl_sock,
0295                     mc_groups_longs * BITS_PER_LONG);
0296             if (err) {
0297                 /*
0298                  * No need to roll back, can only fail if
0299                  * memory allocation fails and then the
0300                  * number of _possible_ groups has been
0301                  * increased on some sockets which is ok.
0302                  */
0303                 break;
0304             }
0305         }
0306         rcu_read_unlock();
0307         netlink_table_ungrab();
0308     } else {
0309         err = netlink_change_ngroups(init_net.genl_sock,
0310                          mc_groups_longs * BITS_PER_LONG);
0311     }
0312 
0313     if (groups_allocated && err) {
0314         for (i = 0; i < family->n_mcgrps; i++)
0315             clear_bit(family->mcgrp_offset + i, mc_groups);
0316     }
0317 
0318     return err;
0319 }
0320 
0321 static void genl_unregister_mc_groups(const struct genl_family *family)
0322 {
0323     struct net *net;
0324     int i;
0325 
0326     netlink_table_grab();
0327     rcu_read_lock();
0328     for_each_net_rcu(net) {
0329         for (i = 0; i < family->n_mcgrps; i++)
0330             __netlink_clear_multicast_users(
0331                 net->genl_sock, family->mcgrp_offset + i);
0332     }
0333     rcu_read_unlock();
0334     netlink_table_ungrab();
0335 
0336     for (i = 0; i < family->n_mcgrps; i++) {
0337         int grp_id = family->mcgrp_offset + i;
0338 
0339         if (grp_id != 1)
0340             clear_bit(grp_id, mc_groups);
0341         genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family,
0342                 &family->mcgrps[i], grp_id);
0343     }
0344 }
0345 
0346 static int genl_validate_ops(const struct genl_family *family)
0347 {
0348     int i, j;
0349 
0350     if (WARN_ON(family->n_ops && !family->ops) ||
0351         WARN_ON(family->n_small_ops && !family->small_ops))
0352         return -EINVAL;
0353 
0354     for (i = 0; i < genl_get_cmd_cnt(family); i++) {
0355         struct genl_ops op;
0356 
0357         genl_get_cmd_by_index(i, family, &op);
0358         if (op.dumpit == NULL && op.doit == NULL)
0359             return -EINVAL;
0360         for (j = i + 1; j < genl_get_cmd_cnt(family); j++) {
0361             struct genl_ops op2;
0362 
0363             genl_get_cmd_by_index(j, family, &op2);
0364             if (op.cmd == op2.cmd)
0365                 return -EINVAL;
0366         }
0367     }
0368 
0369     return 0;
0370 }
0371 
0372 /**
0373  * genl_register_family - register a generic netlink family
0374  * @family: generic netlink family
0375  *
0376  * Registers the specified family after validating it first. Only one
0377  * family may be registered with the same family name or identifier.
0378  *
0379  * The family's ops, multicast groups and module pointer must already
0380  * be assigned.
0381  *
0382  * Return 0 on success or a negative error code.
0383  */
0384 int genl_register_family(struct genl_family *family)
0385 {
0386     int err, i;
0387     int start = GENL_START_ALLOC, end = GENL_MAX_ID;
0388 
0389     err = genl_validate_ops(family);
0390     if (err)
0391         return err;
0392 
0393     genl_lock_all();
0394 
0395     if (genl_family_find_byname(family->name)) {
0396         err = -EEXIST;
0397         goto errout_locked;
0398     }
0399 
0400     /*
0401      * Sadly, a few cases need to be special-cased
0402      * due to them having previously abused the API
0403      * and having used their family ID also as their
0404      * multicast group ID, so we use reserved IDs
0405      * for both to be sure we can do that mapping.
0406      */
0407     if (family == &genl_ctrl) {
0408         /* and this needs to be special for initial family lookups */
0409         start = end = GENL_ID_CTRL;
0410     } else if (strcmp(family->name, "pmcraid") == 0) {
0411         start = end = GENL_ID_PMCRAID;
0412     } else if (strcmp(family->name, "VFS_DQUOT") == 0) {
0413         start = end = GENL_ID_VFS_DQUOT;
0414     }
0415 
0416     family->id = idr_alloc_cyclic(&genl_fam_idr, family,
0417                       start, end + 1, GFP_KERNEL);
0418     if (family->id < 0) {
0419         err = family->id;
0420         goto errout_locked;
0421     }
0422 
0423     err = genl_validate_assign_mc_groups(family);
0424     if (err)
0425         goto errout_remove;
0426 
0427     genl_unlock_all();
0428 
0429     /* send all events */
0430     genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
0431     for (i = 0; i < family->n_mcgrps; i++)
0432         genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
0433                 &family->mcgrps[i], family->mcgrp_offset + i);
0434 
0435     return 0;
0436 
0437 errout_remove:
0438     idr_remove(&genl_fam_idr, family->id);
0439 errout_locked:
0440     genl_unlock_all();
0441     return err;
0442 }
0443 EXPORT_SYMBOL(genl_register_family);
0444 
0445 /**
0446  * genl_unregister_family - unregister generic netlink family
0447  * @family: generic netlink family
0448  *
0449  * Unregisters the specified family.
0450  *
0451  * Returns 0 on success or a negative error code.
0452  */
0453 int genl_unregister_family(const struct genl_family *family)
0454 {
0455     genl_lock_all();
0456 
0457     if (!genl_family_find_byid(family->id)) {
0458         genl_unlock_all();
0459         return -ENOENT;
0460     }
0461 
0462     genl_unregister_mc_groups(family);
0463 
0464     idr_remove(&genl_fam_idr, family->id);
0465 
0466     up_write(&cb_lock);
0467     wait_event(genl_sk_destructing_waitq,
0468            atomic_read(&genl_sk_destructing_cnt) == 0);
0469     genl_unlock();
0470 
0471     genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
0472 
0473     return 0;
0474 }
0475 EXPORT_SYMBOL(genl_unregister_family);
0476 
0477 /**
0478  * genlmsg_put - Add generic netlink header to netlink message
0479  * @skb: socket buffer holding the message
0480  * @portid: netlink portid the message is addressed to
0481  * @seq: sequence number (usually the one of the sender)
0482  * @family: generic netlink family
0483  * @flags: netlink message flags
0484  * @cmd: generic netlink command
0485  *
0486  * Returns pointer to user specific header
0487  */
0488 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
0489           const struct genl_family *family, int flags, u8 cmd)
0490 {
0491     struct nlmsghdr *nlh;
0492     struct genlmsghdr *hdr;
0493 
0494     nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
0495             family->hdrsize, flags);
0496     if (nlh == NULL)
0497         return NULL;
0498 
0499     hdr = nlmsg_data(nlh);
0500     hdr->cmd = cmd;
0501     hdr->version = family->version;
0502     hdr->reserved = 0;
0503 
0504     return (char *) hdr + GENL_HDRLEN;
0505 }
0506 EXPORT_SYMBOL(genlmsg_put);
0507 
0508 static struct genl_dumpit_info *genl_dumpit_info_alloc(void)
0509 {
0510     return kmalloc(sizeof(struct genl_dumpit_info), GFP_KERNEL);
0511 }
0512 
0513 static void genl_dumpit_info_free(const struct genl_dumpit_info *info)
0514 {
0515     kfree(info);
0516 }
0517 
0518 static struct nlattr **
0519 genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
0520                 struct nlmsghdr *nlh,
0521                 struct netlink_ext_ack *extack,
0522                 const struct genl_ops *ops,
0523                 int hdrlen,
0524                 enum genl_validate_flags no_strict_flag)
0525 {
0526     enum netlink_validation validate = ops->validate & no_strict_flag ?
0527                        NL_VALIDATE_LIBERAL :
0528                        NL_VALIDATE_STRICT;
0529     struct nlattr **attrbuf;
0530     int err;
0531 
0532     if (!ops->maxattr)
0533         return NULL;
0534 
0535     attrbuf = kmalloc_array(ops->maxattr + 1,
0536                 sizeof(struct nlattr *), GFP_KERNEL);
0537     if (!attrbuf)
0538         return ERR_PTR(-ENOMEM);
0539 
0540     err = __nlmsg_parse(nlh, hdrlen, attrbuf, ops->maxattr, ops->policy,
0541                 validate, extack);
0542     if (err) {
0543         kfree(attrbuf);
0544         return ERR_PTR(err);
0545     }
0546     return attrbuf;
0547 }
0548 
0549 static void genl_family_rcv_msg_attrs_free(struct nlattr **attrbuf)
0550 {
0551     kfree(attrbuf);
0552 }
0553 
0554 struct genl_start_context {
0555     const struct genl_family *family;
0556     struct nlmsghdr *nlh;
0557     struct netlink_ext_ack *extack;
0558     const struct genl_ops *ops;
0559     int hdrlen;
0560 };
0561 
0562 static int genl_start(struct netlink_callback *cb)
0563 {
0564     struct genl_start_context *ctx = cb->data;
0565     const struct genl_ops *ops = ctx->ops;
0566     struct genl_dumpit_info *info;
0567     struct nlattr **attrs = NULL;
0568     int rc = 0;
0569 
0570     if (ops->validate & GENL_DONT_VALIDATE_DUMP)
0571         goto no_attrs;
0572 
0573     if (ctx->nlh->nlmsg_len < nlmsg_msg_size(ctx->hdrlen))
0574         return -EINVAL;
0575 
0576     attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
0577                         ops, ctx->hdrlen,
0578                         GENL_DONT_VALIDATE_DUMP_STRICT);
0579     if (IS_ERR(attrs))
0580         return PTR_ERR(attrs);
0581 
0582 no_attrs:
0583     info = genl_dumpit_info_alloc();
0584     if (!info) {
0585         genl_family_rcv_msg_attrs_free(attrs);
0586         return -ENOMEM;
0587     }
0588     info->family = ctx->family;
0589     info->op = *ops;
0590     info->attrs = attrs;
0591 
0592     cb->data = info;
0593     if (ops->start) {
0594         if (!ctx->family->parallel_ops)
0595             genl_lock();
0596         rc = ops->start(cb);
0597         if (!ctx->family->parallel_ops)
0598             genl_unlock();
0599     }
0600 
0601     if (rc) {
0602         genl_family_rcv_msg_attrs_free(info->attrs);
0603         genl_dumpit_info_free(info);
0604         cb->data = NULL;
0605     }
0606     return rc;
0607 }
0608 
0609 static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
0610 {
0611     const struct genl_ops *ops = &genl_dumpit_info(cb)->op;
0612     int rc;
0613 
0614     genl_lock();
0615     rc = ops->dumpit(skb, cb);
0616     genl_unlock();
0617     return rc;
0618 }
0619 
0620 static int genl_lock_done(struct netlink_callback *cb)
0621 {
0622     const struct genl_dumpit_info *info = genl_dumpit_info(cb);
0623     const struct genl_ops *ops = &info->op;
0624     int rc = 0;
0625 
0626     if (ops->done) {
0627         genl_lock();
0628         rc = ops->done(cb);
0629         genl_unlock();
0630     }
0631     genl_family_rcv_msg_attrs_free(info->attrs);
0632     genl_dumpit_info_free(info);
0633     return rc;
0634 }
0635 
0636 static int genl_parallel_done(struct netlink_callback *cb)
0637 {
0638     const struct genl_dumpit_info *info = genl_dumpit_info(cb);
0639     const struct genl_ops *ops = &info->op;
0640     int rc = 0;
0641 
0642     if (ops->done)
0643         rc = ops->done(cb);
0644     genl_family_rcv_msg_attrs_free(info->attrs);
0645     genl_dumpit_info_free(info);
0646     return rc;
0647 }
0648 
0649 static int genl_family_rcv_msg_dumpit(const struct genl_family *family,
0650                       struct sk_buff *skb,
0651                       struct nlmsghdr *nlh,
0652                       struct netlink_ext_ack *extack,
0653                       const struct genl_ops *ops,
0654                       int hdrlen, struct net *net)
0655 {
0656     struct genl_start_context ctx;
0657     int err;
0658 
0659     if (!ops->dumpit)
0660         return -EOPNOTSUPP;
0661 
0662     ctx.family = family;
0663     ctx.nlh = nlh;
0664     ctx.extack = extack;
0665     ctx.ops = ops;
0666     ctx.hdrlen = hdrlen;
0667 
0668     if (!family->parallel_ops) {
0669         struct netlink_dump_control c = {
0670             .module = family->module,
0671             .data = &ctx,
0672             .start = genl_start,
0673             .dump = genl_lock_dumpit,
0674             .done = genl_lock_done,
0675         };
0676 
0677         genl_unlock();
0678         err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
0679         genl_lock();
0680     } else {
0681         struct netlink_dump_control c = {
0682             .module = family->module,
0683             .data = &ctx,
0684             .start = genl_start,
0685             .dump = ops->dumpit,
0686             .done = genl_parallel_done,
0687         };
0688 
0689         err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
0690     }
0691 
0692     return err;
0693 }
0694 
0695 static int genl_family_rcv_msg_doit(const struct genl_family *family,
0696                     struct sk_buff *skb,
0697                     struct nlmsghdr *nlh,
0698                     struct netlink_ext_ack *extack,
0699                     const struct genl_ops *ops,
0700                     int hdrlen, struct net *net)
0701 {
0702     struct nlattr **attrbuf;
0703     struct genl_info info;
0704     int err;
0705 
0706     if (!ops->doit)
0707         return -EOPNOTSUPP;
0708 
0709     attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
0710                           ops, hdrlen,
0711                           GENL_DONT_VALIDATE_STRICT);
0712     if (IS_ERR(attrbuf))
0713         return PTR_ERR(attrbuf);
0714 
0715     info.snd_seq = nlh->nlmsg_seq;
0716     info.snd_portid = NETLINK_CB(skb).portid;
0717     info.nlhdr = nlh;
0718     info.genlhdr = nlmsg_data(nlh);
0719     info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
0720     info.attrs = attrbuf;
0721     info.extack = extack;
0722     genl_info_net_set(&info, net);
0723     memset(&info.user_ptr, 0, sizeof(info.user_ptr));
0724 
0725     if (family->pre_doit) {
0726         err = family->pre_doit(ops, skb, &info);
0727         if (err)
0728             goto out;
0729     }
0730 
0731     err = ops->doit(skb, &info);
0732 
0733     if (family->post_doit)
0734         family->post_doit(ops, skb, &info);
0735 
0736 out:
0737     genl_family_rcv_msg_attrs_free(attrbuf);
0738 
0739     return err;
0740 }
0741 
0742 static int genl_family_rcv_msg(const struct genl_family *family,
0743                    struct sk_buff *skb,
0744                    struct nlmsghdr *nlh,
0745                    struct netlink_ext_ack *extack)
0746 {
0747     struct net *net = sock_net(skb->sk);
0748     struct genlmsghdr *hdr = nlmsg_data(nlh);
0749     struct genl_ops op;
0750     int hdrlen;
0751 
0752     /* this family doesn't exist in this netns */
0753     if (!family->netnsok && !net_eq(net, &init_net))
0754         return -ENOENT;
0755 
0756     hdrlen = GENL_HDRLEN + family->hdrsize;
0757     if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
0758         return -EINVAL;
0759 
0760     if (genl_get_cmd(hdr->cmd, family, &op))
0761         return -EOPNOTSUPP;
0762 
0763     if ((op.flags & GENL_ADMIN_PERM) &&
0764         !netlink_capable(skb, CAP_NET_ADMIN))
0765         return -EPERM;
0766 
0767     if ((op.flags & GENL_UNS_ADMIN_PERM) &&
0768         !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
0769         return -EPERM;
0770 
0771     if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP)
0772         return genl_family_rcv_msg_dumpit(family, skb, nlh, extack,
0773                           &op, hdrlen, net);
0774     else
0775         return genl_family_rcv_msg_doit(family, skb, nlh, extack,
0776                         &op, hdrlen, net);
0777 }
0778 
0779 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
0780             struct netlink_ext_ack *extack)
0781 {
0782     const struct genl_family *family;
0783     int err;
0784 
0785     family = genl_family_find_byid(nlh->nlmsg_type);
0786     if (family == NULL)
0787         return -ENOENT;
0788 
0789     if (!family->parallel_ops)
0790         genl_lock();
0791 
0792     err = genl_family_rcv_msg(family, skb, nlh, extack);
0793 
0794     if (!family->parallel_ops)
0795         genl_unlock();
0796 
0797     return err;
0798 }
0799 
0800 static void genl_rcv(struct sk_buff *skb)
0801 {
0802     down_read(&cb_lock);
0803     netlink_rcv_skb(skb, &genl_rcv_msg);
0804     up_read(&cb_lock);
0805 }
0806 
0807 /**************************************************************************
0808  * Controller
0809  **************************************************************************/
0810 
0811 static struct genl_family genl_ctrl;
0812 
0813 static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
0814               u32 flags, struct sk_buff *skb, u8 cmd)
0815 {
0816     void *hdr;
0817 
0818     hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
0819     if (hdr == NULL)
0820         return -1;
0821 
0822     if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
0823         nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
0824         nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
0825         nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
0826         nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
0827         goto nla_put_failure;
0828 
0829     if (genl_get_cmd_cnt(family)) {
0830         struct nlattr *nla_ops;
0831         int i;
0832 
0833         nla_ops = nla_nest_start_noflag(skb, CTRL_ATTR_OPS);
0834         if (nla_ops == NULL)
0835             goto nla_put_failure;
0836 
0837         for (i = 0; i < genl_get_cmd_cnt(family); i++) {
0838             struct nlattr *nest;
0839             struct genl_ops op;
0840             u32 op_flags;
0841 
0842             genl_get_cmd_by_index(i, family, &op);
0843             op_flags = op.flags;
0844             if (op.dumpit)
0845                 op_flags |= GENL_CMD_CAP_DUMP;
0846             if (op.doit)
0847                 op_flags |= GENL_CMD_CAP_DO;
0848             if (op.policy)
0849                 op_flags |= GENL_CMD_CAP_HASPOL;
0850 
0851             nest = nla_nest_start_noflag(skb, i + 1);
0852             if (nest == NULL)
0853                 goto nla_put_failure;
0854 
0855             if (nla_put_u32(skb, CTRL_ATTR_OP_ID, op.cmd) ||
0856                 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
0857                 goto nla_put_failure;
0858 
0859             nla_nest_end(skb, nest);
0860         }
0861 
0862         nla_nest_end(skb, nla_ops);
0863     }
0864 
0865     if (family->n_mcgrps) {
0866         struct nlattr *nla_grps;
0867         int i;
0868 
0869         nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
0870         if (nla_grps == NULL)
0871             goto nla_put_failure;
0872 
0873         for (i = 0; i < family->n_mcgrps; i++) {
0874             struct nlattr *nest;
0875             const struct genl_multicast_group *grp;
0876 
0877             grp = &family->mcgrps[i];
0878 
0879             nest = nla_nest_start_noflag(skb, i + 1);
0880             if (nest == NULL)
0881                 goto nla_put_failure;
0882 
0883             if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
0884                     family->mcgrp_offset + i) ||
0885                 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
0886                        grp->name))
0887                 goto nla_put_failure;
0888 
0889             nla_nest_end(skb, nest);
0890         }
0891         nla_nest_end(skb, nla_grps);
0892     }
0893 
0894     genlmsg_end(skb, hdr);
0895     return 0;
0896 
0897 nla_put_failure:
0898     genlmsg_cancel(skb, hdr);
0899     return -EMSGSIZE;
0900 }
0901 
0902 static int ctrl_fill_mcgrp_info(const struct genl_family *family,
0903                 const struct genl_multicast_group *grp,
0904                 int grp_id, u32 portid, u32 seq, u32 flags,
0905                 struct sk_buff *skb, u8 cmd)
0906 {
0907     void *hdr;
0908     struct nlattr *nla_grps;
0909     struct nlattr *nest;
0910 
0911     hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
0912     if (hdr == NULL)
0913         return -1;
0914 
0915     if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
0916         nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
0917         goto nla_put_failure;
0918 
0919     nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
0920     if (nla_grps == NULL)
0921         goto nla_put_failure;
0922 
0923     nest = nla_nest_start_noflag(skb, 1);
0924     if (nest == NULL)
0925         goto nla_put_failure;
0926 
0927     if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
0928         nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
0929                grp->name))
0930         goto nla_put_failure;
0931 
0932     nla_nest_end(skb, nest);
0933     nla_nest_end(skb, nla_grps);
0934 
0935     genlmsg_end(skb, hdr);
0936     return 0;
0937 
0938 nla_put_failure:
0939     genlmsg_cancel(skb, hdr);
0940     return -EMSGSIZE;
0941 }
0942 
0943 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
0944 {
0945     int n = 0;
0946     struct genl_family *rt;
0947     struct net *net = sock_net(skb->sk);
0948     int fams_to_skip = cb->args[0];
0949     unsigned int id;
0950 
0951     idr_for_each_entry(&genl_fam_idr, rt, id) {
0952         if (!rt->netnsok && !net_eq(net, &init_net))
0953             continue;
0954 
0955         if (n++ < fams_to_skip)
0956             continue;
0957 
0958         if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
0959                    cb->nlh->nlmsg_seq, NLM_F_MULTI,
0960                    skb, CTRL_CMD_NEWFAMILY) < 0) {
0961             n--;
0962             break;
0963         }
0964     }
0965 
0966     cb->args[0] = n;
0967     return skb->len;
0968 }
0969 
0970 static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
0971                          u32 portid, int seq, u8 cmd)
0972 {
0973     struct sk_buff *skb;
0974     int err;
0975 
0976     skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0977     if (skb == NULL)
0978         return ERR_PTR(-ENOBUFS);
0979 
0980     err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
0981     if (err < 0) {
0982         nlmsg_free(skb);
0983         return ERR_PTR(err);
0984     }
0985 
0986     return skb;
0987 }
0988 
0989 static struct sk_buff *
0990 ctrl_build_mcgrp_msg(const struct genl_family *family,
0991              const struct genl_multicast_group *grp,
0992              int grp_id, u32 portid, int seq, u8 cmd)
0993 {
0994     struct sk_buff *skb;
0995     int err;
0996 
0997     skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0998     if (skb == NULL)
0999         return ERR_PTR(-ENOBUFS);
1000 
1001     err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
1002                    seq, 0, skb, cmd);
1003     if (err < 0) {
1004         nlmsg_free(skb);
1005         return ERR_PTR(err);
1006     }
1007 
1008     return skb;
1009 }
1010 
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 },
1015 };
1016 
1017 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
1018 {
1019     struct sk_buff *msg;
1020     const struct genl_family *res = NULL;
1021     int err = -EINVAL;
1022 
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);
1026         err = -ENOENT;
1027     }
1028 
1029     if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
1030         char *name;
1031 
1032         name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
1033         res = genl_family_find_byname(name);
1034 #ifdef CONFIG_MODULES
1035         if (res == NULL) {
1036             genl_unlock();
1037             up_read(&cb_lock);
1038             request_module("net-pf-%d-proto-%d-family-%s",
1039                        PF_NETLINK, NETLINK_GENERIC, name);
1040             down_read(&cb_lock);
1041             genl_lock();
1042             res = genl_family_find_byname(name);
1043         }
1044 #endif
1045         err = -ENOENT;
1046     }
1047 
1048     if (res == NULL)
1049         return err;
1050 
1051     if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
1052         /* family doesn't exist here */
1053         return -ENOENT;
1054     }
1055 
1056     msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
1057                     CTRL_CMD_NEWFAMILY);
1058     if (IS_ERR(msg))
1059         return PTR_ERR(msg);
1060 
1061     return genlmsg_reply(msg, info);
1062 }
1063 
1064 static int genl_ctrl_event(int event, const struct genl_family *family,
1065                const struct genl_multicast_group *grp,
1066                int grp_id)
1067 {
1068     struct sk_buff *msg;
1069 
1070     /* genl is still initialising */
1071     if (!init_net.genl_sock)
1072         return 0;
1073 
1074     switch (event) {
1075     case CTRL_CMD_NEWFAMILY:
1076     case CTRL_CMD_DELFAMILY:
1077         WARN_ON(grp);
1078         msg = ctrl_build_family_msg(family, 0, 0, event);
1079         break;
1080     case CTRL_CMD_NEWMCAST_GRP:
1081     case CTRL_CMD_DELMCAST_GRP:
1082         BUG_ON(!grp);
1083         msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
1084         break;
1085     default:
1086         return -EINVAL;
1087     }
1088 
1089     if (IS_ERR(msg))
1090         return PTR_ERR(msg);
1091 
1092     if (!family->netnsok) {
1093         genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
1094                     0, GFP_KERNEL);
1095     } else {
1096         rcu_read_lock();
1097         genlmsg_multicast_allns(&genl_ctrl, msg, 0,
1098                     0, GFP_ATOMIC);
1099         rcu_read_unlock();
1100     }
1101 
1102     return 0;
1103 }
1104 
1105 struct ctrl_dump_policy_ctx {
1106     struct netlink_policy_dump_state *state;
1107     const struct genl_family *rt;
1108     unsigned int opidx;
1109     u32 op;
1110     u16 fam_id;
1111     u8 policies:1,
1112        single_op:1;
1113 };
1114 
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 },
1120 };
1121 
1122 static int ctrl_dumppolicy_start(struct netlink_callback *cb)
1123 {
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;
1128     struct genl_ops op;
1129     int err, i;
1130 
1131     BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
1132 
1133     if (!tb[CTRL_ATTR_FAMILY_ID] && !tb[CTRL_ATTR_FAMILY_NAME])
1134         return -EINVAL;
1135 
1136     if (tb[CTRL_ATTR_FAMILY_ID]) {
1137         ctx->fam_id = nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
1138     } else {
1139         rt = genl_family_find_byname(
1140             nla_data(tb[CTRL_ATTR_FAMILY_NAME]));
1141         if (!rt)
1142             return -ENOENT;
1143         ctx->fam_id = rt->id;
1144     }
1145 
1146     rt = genl_family_find_byid(ctx->fam_id);
1147     if (!rt)
1148         return -ENOENT;
1149 
1150     ctx->rt = rt;
1151 
1152     if (tb[CTRL_ATTR_OP]) {
1153         ctx->single_op = true;
1154         ctx->op = nla_get_u32(tb[CTRL_ATTR_OP]);
1155 
1156         err = genl_get_cmd(ctx->op, rt, &op);
1157         if (err) {
1158             NL_SET_BAD_ATTR(cb->extack, tb[CTRL_ATTR_OP]);
1159             return err;
1160         }
1161 
1162         if (!op.policy)
1163             return -ENODATA;
1164 
1165         return netlink_policy_dump_add_policy(&ctx->state, op.policy,
1166                               op.maxattr);
1167     }
1168 
1169     for (i = 0; i < genl_get_cmd_cnt(rt); i++) {
1170         genl_get_cmd_by_index(i, rt, &op);
1171 
1172         if (op.policy) {
1173             err = netlink_policy_dump_add_policy(&ctx->state,
1174                                  op.policy,
1175                                  op.maxattr);
1176             if (err)
1177                 goto err_free_state;
1178         }
1179     }
1180 
1181     if (!ctx->state)
1182         return -ENODATA;
1183     return 0;
1184 
1185 err_free_state:
1186     netlink_policy_dump_free(ctx->state);
1187     return err;
1188 }
1189 
1190 static void *ctrl_dumppolicy_prep(struct sk_buff *skb,
1191                   struct netlink_callback *cb)
1192 {
1193     struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1194     void *hdr;
1195 
1196     hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1197               cb->nlh->nlmsg_seq, &genl_ctrl,
1198               NLM_F_MULTI, CTRL_CMD_GETPOLICY);
1199     if (!hdr)
1200         return NULL;
1201 
1202     if (nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, ctx->fam_id))
1203         return NULL;
1204 
1205     return hdr;
1206 }
1207 
1208 static int ctrl_dumppolicy_put_op(struct sk_buff *skb,
1209                   struct netlink_callback *cb,
1210                       struct genl_ops *op)
1211 {
1212     struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1213     struct nlattr *nest_pol, *nest_op;
1214     void *hdr;
1215     int idx;
1216 
1217     /* skip if we have nothing to show */
1218     if (!op->policy)
1219         return 0;
1220     if (!op->doit &&
1221         (!op->dumpit || op->validate & GENL_DONT_VALIDATE_DUMP))
1222         return 0;
1223 
1224     hdr = ctrl_dumppolicy_prep(skb, cb);
1225     if (!hdr)
1226         return -ENOBUFS;
1227 
1228     nest_pol = nla_nest_start(skb, CTRL_ATTR_OP_POLICY);
1229     if (!nest_pol)
1230         goto err;
1231 
1232     nest_op = nla_nest_start(skb, op->cmd);
1233     if (!nest_op)
1234         goto err;
1235 
1236     /* for now both do/dump are always the same */
1237     idx = netlink_policy_dump_get_policy_idx(ctx->state,
1238                          op->policy,
1239                          op->maxattr);
1240 
1241     if (op->doit && nla_put_u32(skb, CTRL_ATTR_POLICY_DO, idx))
1242         goto err;
1243 
1244     if (op->dumpit && !(op->validate & GENL_DONT_VALIDATE_DUMP) &&
1245         nla_put_u32(skb, CTRL_ATTR_POLICY_DUMP, idx))
1246         goto err;
1247 
1248     nla_nest_end(skb, nest_op);
1249     nla_nest_end(skb, nest_pol);
1250     genlmsg_end(skb, hdr);
1251 
1252     return 0;
1253 err:
1254     genlmsg_cancel(skb, hdr);
1255     return -ENOBUFS;
1256 }
1257 
1258 static int ctrl_dumppolicy(struct sk_buff *skb, struct netlink_callback *cb)
1259 {
1260     struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1261     void *hdr;
1262 
1263     if (!ctx->policies) {
1264         while (ctx->opidx < genl_get_cmd_cnt(ctx->rt)) {
1265             struct genl_ops op;
1266 
1267             if (ctx->single_op) {
1268                 int err;
1269 
1270                 err = genl_get_cmd(ctx->op, ctx->rt, &op);
1271                 if (WARN_ON(err))
1272                     return skb->len;
1273 
1274                 /* break out of the loop after this one */
1275                 ctx->opidx = genl_get_cmd_cnt(ctx->rt);
1276             } else {
1277                 genl_get_cmd_by_index(ctx->opidx, ctx->rt, &op);
1278             }
1279 
1280             if (ctrl_dumppolicy_put_op(skb, cb, &op))
1281                 return skb->len;
1282 
1283             ctx->opidx++;
1284         }
1285 
1286         /* completed with the per-op policy index list */
1287         ctx->policies = true;
1288     }
1289 
1290     while (netlink_policy_dump_loop(ctx->state)) {
1291         struct nlattr *nest;
1292 
1293         hdr = ctrl_dumppolicy_prep(skb, cb);
1294         if (!hdr)
1295             goto nla_put_failure;
1296 
1297         nest = nla_nest_start(skb, CTRL_ATTR_POLICY);
1298         if (!nest)
1299             goto nla_put_failure;
1300 
1301         if (netlink_policy_dump_write(skb, ctx->state))
1302             goto nla_put_failure;
1303 
1304         nla_nest_end(skb, nest);
1305 
1306         genlmsg_end(skb, hdr);
1307     }
1308 
1309     return skb->len;
1310 
1311 nla_put_failure:
1312     genlmsg_cancel(skb, hdr);
1313     return skb->len;
1314 }
1315 
1316 static int ctrl_dumppolicy_done(struct netlink_callback *cb)
1317 {
1318     struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1319 
1320     netlink_policy_dump_free(ctx->state);
1321     return 0;
1322 }
1323 
1324 static const struct genl_ops genl_ctrl_ops[] = {
1325     {
1326         .cmd        = CTRL_CMD_GETFAMILY,
1327         .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1328         .policy     = ctrl_policy_family,
1329         .maxattr    = ARRAY_SIZE(ctrl_policy_family) - 1,
1330         .doit       = ctrl_getfamily,
1331         .dumpit     = ctrl_dumpfamily,
1332     },
1333     {
1334         .cmd        = CTRL_CMD_GETPOLICY,
1335         .policy     = ctrl_policy_policy,
1336         .maxattr    = ARRAY_SIZE(ctrl_policy_policy) - 1,
1337         .start      = ctrl_dumppolicy_start,
1338         .dumpit     = ctrl_dumppolicy,
1339         .done       = ctrl_dumppolicy_done,
1340     },
1341 };
1342 
1343 static const struct genl_multicast_group genl_ctrl_groups[] = {
1344     { .name = "notify", },
1345 };
1346 
1347 static struct genl_family genl_ctrl __ro_after_init = {
1348     .module = THIS_MODULE,
1349     .ops = genl_ctrl_ops,
1350     .n_ops = ARRAY_SIZE(genl_ctrl_ops),
1351     .mcgrps = genl_ctrl_groups,
1352     .n_mcgrps = ARRAY_SIZE(genl_ctrl_groups),
1353     .id = GENL_ID_CTRL,
1354     .name = "nlctrl",
1355     .version = 0x2,
1356     .netnsok = true,
1357 };
1358 
1359 static int genl_bind(struct net *net, int group)
1360 {
1361     const struct genl_family *family;
1362     unsigned int id;
1363     int ret = 0;
1364 
1365     genl_lock_all();
1366 
1367     idr_for_each_entry(&genl_fam_idr, family, id) {
1368         const struct genl_multicast_group *grp;
1369         int i;
1370 
1371         if (family->n_mcgrps == 0)
1372             continue;
1373 
1374         i = group - family->mcgrp_offset;
1375         if (i < 0 || i >= family->n_mcgrps)
1376             continue;
1377 
1378         grp = &family->mcgrps[i];
1379         if ((grp->flags & GENL_UNS_ADMIN_PERM) &&
1380             !ns_capable(net->user_ns, CAP_NET_ADMIN))
1381             ret = -EPERM;
1382 
1383         break;
1384     }
1385 
1386     genl_unlock_all();
1387     return ret;
1388 }
1389 
1390 static int __net_init genl_pernet_init(struct net *net)
1391 {
1392     struct netlink_kernel_cfg cfg = {
1393         .input      = genl_rcv,
1394         .flags      = NL_CFG_F_NONROOT_RECV,
1395         .bind       = genl_bind,
1396     };
1397 
1398     /* we'll bump the group number right afterwards */
1399     net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
1400 
1401     if (!net->genl_sock && net_eq(net, &init_net))
1402         panic("GENL: Cannot initialize generic netlink\n");
1403 
1404     if (!net->genl_sock)
1405         return -ENOMEM;
1406 
1407     return 0;
1408 }
1409 
1410 static void __net_exit genl_pernet_exit(struct net *net)
1411 {
1412     netlink_kernel_release(net->genl_sock);
1413     net->genl_sock = NULL;
1414 }
1415 
1416 static struct pernet_operations genl_pernet_ops = {
1417     .init = genl_pernet_init,
1418     .exit = genl_pernet_exit,
1419 };
1420 
1421 static int __init genl_init(void)
1422 {
1423     int err;
1424 
1425     err = genl_register_family(&genl_ctrl);
1426     if (err < 0)
1427         goto problem;
1428 
1429     err = register_pernet_subsys(&genl_pernet_ops);
1430     if (err)
1431         goto problem;
1432 
1433     return 0;
1434 
1435 problem:
1436     panic("GENL: Cannot register controller: %d\n", err);
1437 }
1438 
1439 core_initcall(genl_init);
1440 
1441 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
1442              gfp_t flags)
1443 {
1444     struct sk_buff *tmp;
1445     struct net *net, *prev = NULL;
1446     bool delivered = false;
1447     int err;
1448 
1449     for_each_net_rcu(net) {
1450         if (prev) {
1451             tmp = skb_clone(skb, flags);
1452             if (!tmp) {
1453                 err = -ENOMEM;
1454                 goto error;
1455             }
1456             err = nlmsg_multicast(prev->genl_sock, tmp,
1457                           portid, group, flags);
1458             if (!err)
1459                 delivered = true;
1460             else if (err != -ESRCH)
1461                 goto error;
1462         }
1463 
1464         prev = net;
1465     }
1466 
1467     err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1468     if (!err)
1469         delivered = true;
1470     else if (err != -ESRCH)
1471         return err;
1472     return delivered ? 0 : -ESRCH;
1473  error:
1474     kfree_skb(skb);
1475     return err;
1476 }
1477 
1478 int genlmsg_multicast_allns(const struct genl_family *family,
1479                 struct sk_buff *skb, u32 portid,
1480                 unsigned int group, gfp_t flags)
1481 {
1482     if (WARN_ON_ONCE(group >= family->n_mcgrps))
1483         return -EINVAL;
1484 
1485     group = family->mcgrp_offset + group;
1486     return genlmsg_mcast(skb, portid, group, flags);
1487 }
1488 EXPORT_SYMBOL(genlmsg_multicast_allns);
1489 
1490 void genl_notify(const struct genl_family *family, struct sk_buff *skb,
1491          struct genl_info *info, u32 group, gfp_t flags)
1492 {
1493     struct net *net = genl_info_net(info);
1494     struct sock *sk = net->genl_sock;
1495 
1496     if (WARN_ON_ONCE(group >= family->n_mcgrps))
1497         return;
1498 
1499     group = family->mcgrp_offset + group;
1500     nlmsg_notify(sk, skb, info->snd_portid, group,
1501              nlmsg_report(info->nlhdr), flags);
1502 }
1503 EXPORT_SYMBOL(genl_notify);