]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
payload: do not remove icmp echo dependency
authorFlorian Westphal <fw@strlen.de>
Tue, 15 Jun 2021 16:01:50 +0000 (18:01 +0200)
committerFlorian Westphal <fw@strlen.de>
Wed, 16 Jun 2021 23:12:46 +0000 (01:12 +0200)
"icmp type echo-request icmp id 2" and "icmp id 2" are not the same,
the latter gains an implicit dependency on both echo-request and
echo-reply.

Change payload dependency tracking to not store dependency in case
the value type is ICMP(6)_ECHO(REPLY).

Signed-off-by: Florian Westphal <fw@strlen.de>
src/payload.c

index cfa952248a156dd138ad10d53f33b2a46a708257..97b60713e8008249488121fdca66ea28f1f7d903 100644 (file)
@@ -98,12 +98,16 @@ static void payload_expr_pctx_update(struct proto_ctx *ctx,
        desc = proto_find_upper(base, proto);
 
        if (!desc) {
-               if (base == &proto_icmp || base == &proto_icmp6) {
+               if (base == &proto_icmp) {
                        /* proto 0 is ECHOREPLY, just pretend its ECHO.
                         * Not doing this would need an additional marker
                         * bit to tell when icmp.type was set.
                         */
                        ctx->th_dep.icmp.type = proto ? proto : ICMP_ECHO;
+               } else if (base == &proto_icmp6) {
+                       if (proto == ICMP6_ECHO_REPLY)
+                               proto = ICMP6_ECHO_REQUEST;
+                       ctx->th_dep.icmp.type = proto;
                }
                return;
        }
@@ -554,33 +558,39 @@ void payload_dependency_reset(struct payload_dep_ctx *ctx)
        memset(ctx, 0, sizeof(*ctx));
 }
 
-static uint8_t icmp_get_type(const struct proto_desc *desc, uint8_t value)
+static bool payload_dependency_store_icmp_type(struct payload_dep_ctx *ctx,
+                                              const struct stmt *stmt)
 {
-       if (desc == &proto_icmp && value == 0)
-               return ICMP_ECHO;
+       struct expr *dep = stmt->expr;
+       const struct proto_desc *desc;
+       const struct expr *right;
+       uint8_t type;
 
-       return value;
-}
+       if (dep->left->etype != EXPR_PAYLOAD)
+               return false;
 
-static uint8_t icmp_get_dep_type(const struct proto_desc *desc, struct expr *right)
-{
-       if (right->etype == EXPR_VALUE && right->len == BITS_PER_BYTE)
-               return icmp_get_type(desc, mpz_get_uint8(right->value));
+       right = dep->right;
+       if (right->etype != EXPR_VALUE || right->len != BITS_PER_BYTE)
+               return false;
 
-       return 0;
-}
+       desc = dep->left->payload.desc;
+       if (desc == &proto_icmp) {
+               type = mpz_get_uint8(right->value);
 
-static void payload_dependency_store_icmp_type(struct payload_dep_ctx *ctx)
-{
-       struct expr *dep = ctx->pdep->expr;
-       const struct proto_desc *desc;
+               if (type == ICMP_ECHOREPLY)
+                       type = ICMP_ECHO;
 
-       if (dep->left->etype != EXPR_PAYLOAD)
-               return;
+               ctx->icmp_type = type;
 
-       desc = dep->left->payload.desc;
-       if (desc == &proto_icmp || desc == &proto_icmp6)
-               ctx->icmp_type = icmp_get_dep_type(dep->left->payload.desc, dep->right);
+               return type == ICMP_ECHO;
+       } else if (desc == &proto_icmp6) {
+               type = mpz_get_uint8(right->value);
+
+               ctx->icmp_type = type;
+               return type == ICMP6_ECHO_REQUEST || type == ICMP6_ECHO_REPLY;
+       }
+
+       return false;
 }
 
 /**
@@ -593,10 +603,13 @@ static void payload_dependency_store_icmp_type(struct payload_dep_ctx *ctx)
 void payload_dependency_store(struct payload_dep_ctx *ctx,
                              struct stmt *stmt, enum proto_bases base)
 {
-       ctx->pbase = base + 1;
-       ctx->pdep  = stmt;
+       bool ignore_dep = payload_dependency_store_icmp_type(ctx, stmt);
+
+       if (ignore_dep)
+               return;
 
-       payload_dependency_store_icmp_type(ctx);
+       ctx->pdep  = stmt;
+       ctx->pbase = base + 1;
 }
 
 /**