]> Pileus Git - ~andy/linux/blobdiff - net/netfilter/nf_tables_api.c
netfilter: nf_tables: restore chain change atomicity
[~andy/linux] / net / netfilter / nf_tables_api.c
index f93b7d06f4be9525ad4ab4314a106b01e85749ba..d275d384bbc57863027a66eb6c23fd8517771054 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);
@@ -306,13 +307,17 @@ err:
        return err;
 }
 
-static int nf_tables_table_enable(struct nft_table *table)
+static int nf_tables_table_enable(const struct nft_af_info *afi,
+                                 struct nft_table *table)
 {
        struct nft_chain *chain;
        int err, i = 0;
 
        list_for_each_entry(chain, &table->chains, list) {
-               err = nf_register_hook(&nft_base_chain(chain)->ops);
+               if (!(chain->flags & NFT_BASE_CHAIN))
+                       continue;
+
+               err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
                if (err < 0)
                        goto err;
 
@@ -321,20 +326,27 @@ 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;
 
-               nf_unregister_hook(&nft_base_chain(chain)->ops);
+               nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
        }
        return err;
 }
 
-static int nf_tables_table_disable(struct nft_table *table)
+static int nf_tables_table_disable(const struct nft_af_info *afi,
+                                  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_hooks(nft_base_chain(chain)->ops,
+                                           afi->nops);
+       }
 
        return 0;
 }
@@ -356,12 +368,12 @@ static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
 
                if ((flags & NFT_TABLE_F_DORMANT) &&
                    !(table->flags & NFT_TABLE_F_DORMANT)) {
-                       ret = nf_tables_table_disable(table);
+                       ret = nf_tables_table_disable(afi, table);
                        if (ret >= 0)
                                table->flags |= NFT_TABLE_F_DORMANT;
                } else if (!(flags & NFT_TABLE_F_DORMANT) &&
                           table->flags & NFT_TABLE_F_DORMANT) {
-                       ret = nf_tables_table_enable(table);
+                       ret = nf_tables_table_enable(afi, table);
                        if (ret >= 0)
                                table->flags &= ~NFT_TABLE_F_DORMANT;
                }
@@ -589,7 +601,7 @@ static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
 
        if (chain->flags & NFT_BASE_CHAIN) {
                const struct nft_base_chain *basechain = nft_base_chain(chain);
-               const struct nf_hook_ops *ops = &basechain->ops;
+               const struct nf_hook_ops *ops = &basechain->ops[0];
                struct nlattr *nest;
 
                nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
@@ -748,22 +760,6 @@ err:
        return err;
 }
 
-static int
-nf_tables_chain_policy(struct nft_base_chain *chain, const struct nlattr *attr)
-{
-       switch (ntohl(nla_get_be32(attr))) {
-       case NF_DROP:
-               chain->policy = NF_DROP;
-               break;
-       case NF_ACCEPT:
-               chain->policy = NF_ACCEPT;
-               break;
-       default:
-               return -EINVAL;
-       }
-       return 0;
-}
-
 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
        [NFTA_COUNTER_PACKETS]  = { .type = NLA_U64 },
        [NFTA_COUNTER_BYTES]    = { .type = NLA_U64 },
@@ -822,7 +818,9 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
        struct nlattr *ha[NFTA_HOOK_MAX + 1];
        struct net *net = sock_net(skb->sk);
        int family = nfmsg->nfgen_family;
+       u8 policy = NF_ACCEPT;
        u64 handle = 0;
+       unsigned int i;
        int err;
        bool create;
 
@@ -856,6 +854,22 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
                }
        }
 
+       if (nla[NFTA_CHAIN_POLICY]) {
+               if ((chain != NULL &&
+                   !(chain->flags & NFT_BASE_CHAIN)) ||
+                   nla[NFTA_CHAIN_HOOK] == NULL)
+                       return -EOPNOTSUPP;
+
+               policy = nla_get_be32(nla[NFTA_CHAIN_POLICY]);
+               switch (policy) {
+               case NF_DROP:
+               case NF_ACCEPT:
+                       break;
+               default:
+                       return -EINVAL;
+               }
+       }
+
        if (chain != NULL) {
                if (nlh->nlmsg_flags & NLM_F_EXCL)
                        return -EEXIST;
@@ -866,16 +880,6 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
                    !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
                        return -EEXIST;
 
-               if (nla[NFTA_CHAIN_POLICY]) {
-                       if (!(chain->flags & NFT_BASE_CHAIN))
-                               return -EOPNOTSUPP;
-
-                       err = nf_tables_chain_policy(nft_base_chain(chain),
-                                                    nla[NFTA_CHAIN_POLICY]);
-                       if (err < 0)
-                               return err;
-               }
-
                if (nla[NFTA_CHAIN_COUNTERS]) {
                        if (!(chain->flags & NFT_BASE_CHAIN))
                                return -EOPNOTSUPP;
@@ -886,6 +890,9 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
                                return err;
                }
 
+               if (nla[NFTA_CHAIN_POLICY])
+                       nft_base_chain(chain)->policy = policy;
+
                if (nla[NFTA_CHAIN_HANDLE] && name)
                        nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
 
@@ -895,7 +902,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
        if (nla[NFTA_CHAIN_HOOK]) {
                struct nf_hook_ops *ops;
                nf_hookfn *hookfn;
-               u32 hooknum;
+               u32 hooknum, priority;
                int type = NFT_CHAIN_T_DEFAULT;
 
                if (nla[NFTA_CHAIN_TYPE]) {
@@ -917,46 +924,20 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
                hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
                if (hooknum >= afi->nhooks)
                        return -EINVAL;
+               priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
 
-               hookfn = chain_type[family][type]->fn[hooknum];
-               if (hookfn == NULL)
+               if (!(chain_type[family][type]->hook_mask & (1 << hooknum)))
                        return -EOPNOTSUPP;
+               hookfn = chain_type[family][type]->fn[hooknum];
 
                basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
                if (basechain == NULL)
                        return -ENOMEM;
 
-               basechain->type = type;
-               chain = &basechain->chain;
-
-               ops = &basechain->ops;
-               ops->pf         = family;
-               ops->owner      = afi->owner;
-               ops->hooknum    = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
-               ops->priority   = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
-               ops->priv       = chain;
-               ops->hook       = hookfn;
-               if (afi->hooks[ops->hooknum])
-                       ops->hook = afi->hooks[ops->hooknum];
-
-               chain->flags |= NFT_BASE_CHAIN;
-
-               if (nla[NFTA_CHAIN_POLICY]) {
-                       err = nf_tables_chain_policy(basechain,
-                                                    nla[NFTA_CHAIN_POLICY]);
-                       if (err < 0) {
-                               free_percpu(basechain->stats);
-                               kfree(basechain);
-                               return err;
-                       }
-               } else
-                       basechain->policy = NF_ACCEPT;
-
                if (nla[NFTA_CHAIN_COUNTERS]) {
                        err = nf_tables_counters(basechain,
                                                 nla[NFTA_CHAIN_COUNTERS]);
                        if (err < 0) {
-                               free_percpu(basechain->stats);
                                kfree(basechain);
                                return err;
                        }
@@ -964,12 +945,32 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
                        struct nft_stats __percpu *newstats;
 
                        newstats = alloc_percpu(struct nft_stats);
-                       if (newstats == NULL)
+                       if (newstats == NULL) {
+                               kfree(basechain);
                                return -ENOMEM;
+                       }
+                       rcu_assign_pointer(basechain->stats, newstats);
+               }
+
+               basechain->type = type;
+               chain = &basechain->chain;
 
-                       rcu_assign_pointer(nft_base_chain(chain)->stats,
-                                          newstats);
+               for (i = 0; i < afi->nops; i++) {
+                       ops = &basechain->ops[i];
+                       ops->pf         = family;
+                       ops->owner      = afi->owner;
+                       ops->hooknum    = hooknum;
+                       ops->priority   = priority;
+                       ops->priv       = chain;
+                       ops->hook       = afi->hooks[ops->hooknum];
+                       if (hookfn)
+                               ops->hook = hookfn;
+                       if (afi->hook_ops_init)
+                               afi->hook_ops_init(ops, i);
                }
+
+               chain->flags |= NFT_BASE_CHAIN;
+               basechain->policy = policy;
        } else {
                chain = kzalloc(sizeof(*chain), GFP_KERNEL);
                if (chain == NULL)
@@ -984,7 +985,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
 
        if (!(table->flags & NFT_TABLE_F_DORMANT) &&
            chain->flags & NFT_BASE_CHAIN) {
-               err = nf_register_hook(&nft_base_chain(chain)->ops);
+               err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
                if (err < 0) {
                        free_percpu(basechain->stats);
                        kfree(basechain);
@@ -1043,7 +1044,7 @@ static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
 
        if (!(table->flags & NFT_TABLE_F_DORMANT) &&
            chain->flags & NFT_BASE_CHAIN)
-               nf_unregister_hook(&nft_base_chain(chain)->ops);
+               nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
 
        nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
                               family);
@@ -1923,12 +1924,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 +1976,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 +2100,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 +2137,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 +2208,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 +2226,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 +2241,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 +2414,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 +2426,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 +2447,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 +2600,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;