]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
nft: Fix -Z for rules with NFTA_RULE_COMPAT
authorPhil Sutter <phil@nwl.cc>
Fri, 15 Nov 2019 09:47:25 +0000 (10:47 +0100)
committerPhil Sutter <phil@nwl.cc>
Fri, 15 Nov 2019 14:45:18 +0000 (15:45 +0100)
The special nested attribute NFTA_RULE_COMPAT holds information about
any present l4proto match (given via '-p' parameter) in input. The match
is contained as meta expression as well, but some xtables extensions
explicitly check it's value (see e.g. xt_TPROXY).

This nested attribute is input only, the information is lost after
parsing (and initialization of compat extensions). So in order to feed a
rule back to kernel with zeroed counters, the attribute has to be
reconstructed based on the rule's expressions.

Other code paths are not affected since rule_to_cs() callback will
populate respective fields in struct iptables_command_state and 'add'
callback (which is the inverse to rule_to_cs()) calls add_compat() in
any case.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
iptables/nft.c

index 83cf5fb703d3eddd744ce5d36d6e06585ce45b5d..599c2f7e4c08762fb0887edd42aa94c29bf6860c 100644 (file)
@@ -2897,6 +2897,44 @@ const char *nft_strerror(int err)
        return strerror(err);
 }
 
+static int recover_rule_compat(struct nftnl_rule *r)
+{
+       struct nftnl_expr_iter *iter;
+       struct nftnl_expr *e;
+       uint32_t reg;
+       int ret = -1;
+
+       iter = nftnl_expr_iter_create(r);
+       if (!iter)
+               return -1;
+
+next_expr:
+       e = nftnl_expr_iter_next(iter);
+       if (!e)
+               goto out;
+
+       if (strcmp("meta", nftnl_expr_get_str(e, NFTNL_EXPR_NAME)) ||
+           nftnl_expr_get_u32(e, NFTNL_EXPR_META_KEY) != NFT_META_L4PROTO)
+               goto next_expr;
+
+       reg = nftnl_expr_get_u32(e, NFTNL_EXPR_META_DREG);
+
+       e = nftnl_expr_iter_next(iter);
+       if (!e)
+               goto out;
+
+       if (strcmp("cmp", nftnl_expr_get_str(e, NFTNL_EXPR_NAME)) ||
+           reg != nftnl_expr_get_u32(e, NFTNL_EXPR_CMP_SREG))
+               goto next_expr;
+
+       add_compat(r, nftnl_expr_get_u8(e, NFTNL_EXPR_CMP_DATA),
+                  nftnl_expr_get_u32(e, NFTNL_EXPR_CMP_OP) == NFT_CMP_NEQ);
+       ret = 0;
+out:
+       nftnl_expr_iter_destroy(iter);
+       return ret;
+}
+
 struct chain_zero_data {
        struct nft_handle       *handle;
        bool                    verbose;
@@ -2961,6 +2999,7 @@ static int __nft_chain_zero_counters(struct nftnl_chain *c, void *data)
                         * Unset RULE_POSITION for older kernels, we want to replace
                         * rule based on its handle only.
                         */
+                       recover_rule_compat(r);
                        nftnl_rule_unset(r, NFTNL_RULE_POSITION);
                        if (!batch_rule_add(h, NFT_COMPAT_RULE_REPLACE, r)) {
                                nftnl_rule_iter_destroy(iter);