]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
datatype: add define for maximum number of bits and mask of datatype id
authorPatrick McHardy <kaber@trash.net>
Sat, 13 Dec 2014 07:50:35 +0000 (07:50 +0000)
committerPatrick McHardy <kaber@trash.net>
Tue, 16 Dec 2014 17:20:54 +0000 (18:20 +0100)
The id of concat datatypes is composed of the ids of the individual
datatypes. Add a define for the number of bits for each datatype id
and a mask.

The number of bits is chosen as 6, allowing for 63 datatypes, or twice
as much as we currently have. This allows for concatenations of 5
types using 32 bits.

The value is statically chosen instead of basing it on the current
numbers of datatypes since we don't want the maximum concatenation
size to vary between versions, also new versions are supposed to be
able to propery parse a ruleset generated by an older version.

Signed-off-by: Patrick McHardy <kaber@trash.net>
include/datatype.h
src/datatype.c
src/evaluate.c

index 50b85c31d8b0da017b1609ac0d51885f8669dd67..ca6ba9fe50ee633d30a03df50fcfce710138f5bb 100644 (file)
@@ -82,6 +82,9 @@ enum datatypes {
 };
 #define TYPE_MAX               (__TYPE_MAX - 1)
 
+#define TYPE_BITS              6
+#define TYPE_MASK              ((1 << TYPE_BITS) - 1)
+
 /**
  * enum byteorder
  *
index 8583531fcb728ab7860171e8e317bc92da548e2d..91e5ed6d4bd3577f9ecca6bde8db02954e41e847 100644 (file)
@@ -51,6 +51,7 @@ static const struct datatype *datatypes[TYPE_MAX + 1] = {
 
 void datatype_register(const struct datatype *dtype)
 {
+       BUILD_BUG_ON(TYPE_MAX & ~TYPE_MASK);
        datatypes[dtype->type] = dtype;
 }
 
@@ -939,7 +940,7 @@ const struct datatype *concat_type_alloc(const struct expr *expr)
                strncat(desc, i->dtype->desc, sizeof(desc) - strlen(desc) - 1);
                strncat(name, i->dtype->name, sizeof(name) - strlen(name) - 1);
 
-               type <<= 8;
+               type <<= TYPE_BITS;
                type  |= i->dtype->type;
                size  += i->dtype->size;
                subtypes++;
index 79edf02166203b5fb5cd536b137ee9ca15a65409..9cb2376aaed73fd0f63038042cebbcf1c5041166 100644 (file)
@@ -617,7 +617,7 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
                                                 "unexpected concat component, "
                                                 "expecting %s",
                                                 dtype->desc);
-               tmp = datatype_lookup((type >> 8 * --off) & 0xff);
+               tmp = datatype_lookup((type >> TYPE_BITS * --off) & TYPE_MASK);
                expr_set_context(&ctx->ectx, tmp, tmp->size);
 
                if (list_member_evaluate(ctx, &i) < 0)