]> Pileus Git - ~andy/linux/blobdiff - net/netfilter/nf_tables_api.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[~andy/linux] / net / netfilter / nf_tables_api.c
index f93b7d06f4be9525ad4ab4314a106b01e85749ba..1fcef1ec1dc14a3cadf9b1effe2f4b95b320ad89 100644 (file)
@@ -180,7 +180,8 @@ static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
        nfmsg->res_id           = 0;
 
        if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
-           nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)))
+           nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
+           nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
                goto nla_put_failure;
 
        return nlmsg_end(skb, nlh);
@@ -312,6 +313,9 @@ static int nf_tables_table_enable(struct nft_table *table)
        int err, i = 0;
 
        list_for_each_entry(chain, &table->chains, list) {
+               if (!(chain->flags & NFT_BASE_CHAIN))
+                       continue;
+
                err = nf_register_hook(&nft_base_chain(chain)->ops);
                if (err < 0)
                        goto err;
@@ -321,6 +325,9 @@ static int nf_tables_table_enable(struct nft_table *table)
        return 0;
 err:
        list_for_each_entry(chain, &table->chains, list) {
+               if (!(chain->flags & NFT_BASE_CHAIN))
+                       continue;
+
                if (i-- <= 0)
                        break;
 
@@ -333,8 +340,10 @@ static int nf_tables_table_disable(struct nft_table *table)
 {
        struct nft_chain *chain;
 
-       list_for_each_entry(chain, &table->chains, list)
-               nf_unregister_hook(&nft_base_chain(chain)->ops);
+       list_for_each_entry(chain, &table->chains, list) {
+               if (chain->flags & NFT_BASE_CHAIN)
+                       nf_unregister_hook(&nft_base_chain(chain)->ops);
+       }
 
        return 0;
 }
@@ -1923,12 +1932,14 @@ static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
 {
        struct net *net = sock_net(skb->sk);
        const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
-       const struct nft_af_info *afi;
+       const struct nft_af_info *afi = NULL;
        const struct nft_table *table = NULL;
 
-       afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
-       if (IS_ERR(afi))
-               return PTR_ERR(afi);
+       if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
+               afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
+               if (IS_ERR(afi))
+                       return PTR_ERR(afi);
+       }
 
        if (nla[NFTA_SET_TABLE] != NULL) {
                table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
@@ -1973,11 +1984,14 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
                        return -ENOMEM;
 
                list_for_each_entry(i, &ctx->table->sets, list) {
-                       if (!sscanf(i->name, name, &n))
+                       int tmp;
+
+                       if (!sscanf(i->name, name, &tmp))
                                continue;
-                       if (n < 0 || n > BITS_PER_LONG * PAGE_SIZE)
+                       if (tmp < 0 || tmp > BITS_PER_LONG * PAGE_SIZE)
                                continue;
-                       set_bit(n, inuse);
+
+                       set_bit(tmp, inuse);
                }
 
                n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
@@ -2094,21 +2108,25 @@ done:
        return skb->len;
 }
 
-static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
-                                  struct netlink_callback *cb)
+static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
+                                     struct netlink_callback *cb)
 {
        const struct nft_set *set;
-       unsigned int idx = 0, s_idx = cb->args[0];
+       unsigned int idx, s_idx = cb->args[0];
        struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
 
        if (cb->args[1])
                return skb->len;
 
        list_for_each_entry(table, &ctx->afi->tables, list) {
-               if (cur_table && cur_table != table)
-                       continue;
+               if (cur_table) {
+                       if (cur_table != table)
+                               continue;
 
+                       cur_table = NULL;
+               }
                ctx->table = table;
+               idx = 0;
                list_for_each_entry(set, &ctx->table->sets, list) {
                        if (idx < s_idx)
                                goto cont;
@@ -2127,6 +2145,61 @@ done:
        return skb->len;
 }
 
+static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
+                                  struct netlink_callback *cb)
+{
+       const struct nft_set *set;
+       unsigned int idx, s_idx = cb->args[0];
+       const struct nft_af_info *afi;
+       struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
+       struct net *net = sock_net(skb->sk);
+       int cur_family = cb->args[3];
+
+       if (cb->args[1])
+               return skb->len;
+
+       list_for_each_entry(afi, &net->nft.af_info, list) {
+               if (cur_family) {
+                       if (afi->family != cur_family)
+                               continue;
+
+                       cur_family = 0;
+               }
+
+               list_for_each_entry(table, &afi->tables, list) {
+                       if (cur_table) {
+                               if (cur_table != table)
+                                       continue;
+
+                               cur_table = NULL;
+                       }
+
+                       ctx->table = table;
+                       ctx->afi = afi;
+                       idx = 0;
+                       list_for_each_entry(set, &ctx->table->sets, list) {
+                               if (idx < s_idx)
+                                       goto cont;
+                               if (nf_tables_fill_set(skb, ctx, set,
+                                                      NFT_MSG_NEWSET,
+                                                      NLM_F_MULTI) < 0) {
+                                       cb->args[0] = idx;
+                                       cb->args[2] = (unsigned long) table;
+                                       cb->args[3] = afi->family;
+                                       goto done;
+                               }
+cont:
+                               idx++;
+                       }
+                       if (s_idx)
+                               s_idx = 0;
+               }
+       }
+       cb->args[1] = 1;
+done:
+       return skb->len;
+}
+
 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
 {
        const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
@@ -2143,9 +2216,12 @@ static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
        if (err < 0)
                return err;
 
-       if (ctx.table == NULL)
-               ret = nf_tables_dump_sets_all(&ctx, skb, cb);
-       else
+       if (ctx.table == NULL) {
+               if (ctx.afi == NULL)
+                       ret = nf_tables_dump_sets_all(&ctx, skb, cb);
+               else
+                       ret = nf_tables_dump_sets_family(&ctx, skb, cb);
+       } else
                ret = nf_tables_dump_sets_table(&ctx, skb, cb);
 
        return ret;
@@ -2158,6 +2234,7 @@ static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
        const struct nft_set *set;
        struct nft_ctx ctx;
        struct sk_buff *skb2;
+       const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
        int err;
 
        /* Verify existance before starting dump */
@@ -2172,6 +2249,10 @@ static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
                return netlink_dump_start(nlsk, skb, nlh, &c);
        }
 
+       /* Only accept unspec with dump */
+       if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
+               return -EAFNOSUPPORT;
+
        set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
        if (IS_ERR(set))
                return PTR_ERR(set);
@@ -2341,6 +2422,7 @@ static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
                            const struct nlmsghdr *nlh,
                            const struct nlattr * const nla[])
 {
+       const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
        struct nft_set *set;
        struct nft_ctx ctx;
        int err;
@@ -2352,6 +2434,9 @@ static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
        if (err < 0)
                return err;
 
+       if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
+               return -EAFNOSUPPORT;
+
        set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
        if (IS_ERR(set))
                return PTR_ERR(set);
@@ -2370,7 +2455,9 @@ static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
        enum nft_registers dreg;
 
        dreg = nft_type_to_reg(set->dtype);
-       return nft_validate_data_load(ctx, dreg, &elem->data, set->dtype);
+       return nft_validate_data_load(ctx, dreg, &elem->data,
+                                     set->dtype == NFT_DATA_VERDICT ?
+                                     NFT_DATA_VERDICT : NFT_DATA_VALUE);
 }
 
 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
@@ -2521,9 +2608,8 @@ static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
        u32 portid, seq;
        int event, err;
 
-       nfmsg = nlmsg_data(cb->nlh);
-       err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_ELEM_LIST_MAX,
-                         nft_set_elem_list_policy);
+       err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
+                         NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
        if (err < 0)
                return err;