]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - net/netlink/genetlink.c
net: genetlink: introduce dump info struct to be available during dumpit op
[linux.git] / net / netlink / genetlink.c
index efccd1ac9a666c6f344682081a720edd236c111a..c785080e94012e08d3740f6d5ada22684d27b7bc 100644 (file)
@@ -458,10 +458,19 @@ void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
 }
 EXPORT_SYMBOL(genlmsg_put);
 
+static struct genl_dumpit_info *genl_dumpit_info_alloc(void)
+{
+       return kmalloc(sizeof(struct genl_dumpit_info), GFP_KERNEL);
+}
+
+static void genl_dumpit_info_free(const struct genl_dumpit_info *info)
+{
+       kfree(info);
+}
+
 static int genl_lock_start(struct netlink_callback *cb)
 {
-       /* our ops are always const - netlink API doesn't propagate that */
-       const struct genl_ops *ops = cb->data;
+       const struct genl_ops *ops = genl_dumpit_info(cb)->ops;
        int rc = 0;
 
        if (ops->start) {
@@ -474,8 +483,7 @@ static int genl_lock_start(struct netlink_callback *cb)
 
 static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
 {
-       /* our ops are always const - netlink API doesn't propagate that */
-       const struct genl_ops *ops = cb->data;
+       const struct genl_ops *ops = genl_dumpit_info(cb)->ops;
        int rc;
 
        genl_lock();
@@ -486,8 +494,8 @@ static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
 
 static int genl_lock_done(struct netlink_callback *cb)
 {
-       /* our ops are always const - netlink API doesn't propagate that */
-       const struct genl_ops *ops = cb->data;
+       const struct genl_dumpit_info *info = genl_dumpit_info(cb);
+       const struct genl_ops *ops = info->ops;
        int rc = 0;
 
        if (ops->done) {
@@ -495,98 +503,100 @@ static int genl_lock_done(struct netlink_callback *cb)
                rc = ops->done(cb);
                genl_unlock();
        }
+       genl_dumpit_info_free(info);
        return rc;
 }
 
-static int genl_family_rcv_msg(const struct genl_family *family,
-                              struct sk_buff *skb,
-                              struct nlmsghdr *nlh,
-                              struct netlink_ext_ack *extack)
+static int genl_parallel_done(struct netlink_callback *cb)
 {
-       const struct genl_ops *ops;
-       struct net *net = sock_net(skb->sk);
-       struct genl_info info;
-       struct genlmsghdr *hdr = nlmsg_data(nlh);
-       struct nlattr **attrbuf;
-       int hdrlen, err;
+       const struct genl_dumpit_info *info = genl_dumpit_info(cb);
+       const struct genl_ops *ops = info->ops;
+       int rc = 0;
 
-       /* this family doesn't exist in this netns */
-       if (!family->netnsok && !net_eq(net, &init_net))
-               return -ENOENT;
+       if (ops->done)
+               rc = ops->done(cb);
+       genl_dumpit_info_free(info);
+       return rc;
+}
 
-       hdrlen = GENL_HDRLEN + family->hdrsize;
-       if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
-               return -EINVAL;
+static int genl_family_rcv_msg_dumpit(const struct genl_family *family,
+                                     struct sk_buff *skb,
+                                     struct nlmsghdr *nlh,
+                                     struct netlink_ext_ack *extack,
+                                     const struct genl_ops *ops,
+                                     int hdrlen, struct net *net)
+{
+       struct genl_dumpit_info *info;
+       int err;
 
-       ops = genl_get_cmd(hdr->cmd, family);
-       if (ops == NULL)
+       if (!ops->dumpit)
                return -EOPNOTSUPP;
 
-       if ((ops->flags & GENL_ADMIN_PERM) &&
-           !netlink_capable(skb, CAP_NET_ADMIN))
-               return -EPERM;
-
-       if ((ops->flags & GENL_UNS_ADMIN_PERM) &&
-           !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
-               return -EPERM;
-
-       if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
-               int rc;
-
-               if (ops->dumpit == NULL)
-                       return -EOPNOTSUPP;
-
-               if (!(ops->validate & GENL_DONT_VALIDATE_DUMP)) {
-                       int hdrlen = GENL_HDRLEN + family->hdrsize;
-
-                       if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
-                               return -EINVAL;
+       if (!(ops->validate & GENL_DONT_VALIDATE_DUMP)) {
+               if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
+                       return -EINVAL;
 
-                       if (family->maxattr) {
-                               unsigned int validate = NL_VALIDATE_STRICT;
-
-                               if (ops->validate &
-                                   GENL_DONT_VALIDATE_DUMP_STRICT)
-                                       validate = NL_VALIDATE_LIBERAL;
-                               rc = __nla_validate(nlmsg_attrdata(nlh, hdrlen),
-                                                   nlmsg_attrlen(nlh, hdrlen),
-                                                   family->maxattr,
-                                                   family->policy,
-                                                   validate, extack);
-                               if (rc)
-                                       return rc;
-                       }
+               if (family->maxattr) {
+                       unsigned int validate = NL_VALIDATE_STRICT;
+
+                       if (ops->validate & GENL_DONT_VALIDATE_DUMP_STRICT)
+                               validate = NL_VALIDATE_LIBERAL;
+                       err = __nla_validate(nlmsg_attrdata(nlh, hdrlen),
+                                            nlmsg_attrlen(nlh, hdrlen),
+                                            family->maxattr, family->policy,
+                                            validate, extack);
+                       if (err)
+                               return err;
                }
+       }
 
-               if (!family->parallel_ops) {
-                       struct netlink_dump_control c = {
-                               .module = family->module,
-                               /* we have const, but the netlink API doesn't */
-                               .data = (void *)ops,
-                               .start = genl_lock_start,
-                               .dump = genl_lock_dumpit,
-                               .done = genl_lock_done,
-                       };
+       /* Allocate dumpit info. It is going to be freed by done() callback. */
+       info = genl_dumpit_info_alloc();
+       if (!info)
+               return -ENOMEM;
 
-                       genl_unlock();
-                       rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
-                       genl_lock();
+       info->ops = ops;
 
-               } else {
-                       struct netlink_dump_control c = {
-                               .module = family->module,
-                               .start = ops->start,
-                               .dump = ops->dumpit,
-                               .done = ops->done,
-                       };
+       if (!family->parallel_ops) {
+               struct netlink_dump_control c = {
+                       .module = family->module,
+                       .data = info,
+                       .start = genl_lock_start,
+                       .dump = genl_lock_dumpit,
+                       .done = genl_lock_done,
+               };
 
-                       rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
-               }
+               genl_unlock();
+               err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
+               genl_lock();
 
-               return rc;
+       } else {
+               struct netlink_dump_control c = {
+                       .module = family->module,
+                       .data = info,
+                       .start = ops->start,
+                       .dump = ops->dumpit,
+                       .done = genl_parallel_done,
+               };
+
+               err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
        }
 
-       if (ops->doit == NULL)
+       return err;
+}
+
+static int genl_family_rcv_msg_doit(const struct genl_family *family,
+                                   struct sk_buff *skb,
+                                   struct nlmsghdr *nlh,
+                                   struct netlink_ext_ack *extack,
+                                   const struct genl_ops *ops,
+                                   int hdrlen, struct net *net)
+{
+       struct nlattr **attrbuf;
+       struct genl_info info;
+       int err;
+
+       if (!ops->doit)
                return -EOPNOTSUPP;
 
        if (family->maxattr && family->parallel_ops) {
@@ -638,6 +648,44 @@ static int genl_family_rcv_msg(const struct genl_family *family,
        return err;
 }
 
+static int genl_family_rcv_msg(const struct genl_family *family,
+                              struct sk_buff *skb,
+                              struct nlmsghdr *nlh,
+                              struct netlink_ext_ack *extack)
+{
+       const struct genl_ops *ops;
+       struct net *net = sock_net(skb->sk);
+       struct genlmsghdr *hdr = nlmsg_data(nlh);
+       int hdrlen;
+
+       /* this family doesn't exist in this netns */
+       if (!family->netnsok && !net_eq(net, &init_net))
+               return -ENOENT;
+
+       hdrlen = GENL_HDRLEN + family->hdrsize;
+       if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
+               return -EINVAL;
+
+       ops = genl_get_cmd(hdr->cmd, family);
+       if (ops == NULL)
+               return -EOPNOTSUPP;
+
+       if ((ops->flags & GENL_ADMIN_PERM) &&
+           !netlink_capable(skb, CAP_NET_ADMIN))
+               return -EPERM;
+
+       if ((ops->flags & GENL_UNS_ADMIN_PERM) &&
+           !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
+               return -EPERM;
+
+       if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP)
+               return genl_family_rcv_msg_dumpit(family, skb, nlh, extack,
+                                                 ops, hdrlen, net);
+       else
+               return genl_family_rcv_msg_doit(family, skb, nlh, extack,
+                                               ops, hdrlen, net);
+}
+
 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
                        struct netlink_ext_ack *extack)
 {