From: Patrick McHardy Subject: [PATCH 3/6] datatype: add define for maximum number of bits and mask of datatype id Date: Fri, 12 Dec 2014 00:22:33 +0000 Message-ID: <1418343756-2886-4-git-send-email-kaber@trash.net> References: <1418343756-2886-1-git-send-email-kaber@trash.net> Cc: netfilter-devel@vger.kernel.org To: pablo@netfilter.org Return-path: Received: from stinky.trash.net ([213.144.137.162]:57281 "EHLO stinky.trash.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933361AbaLLAWo (ORCPT ); Thu, 11 Dec 2014 19:22:44 -0500 In-Reply-To: <1418343756-2886-1-git-send-email-kaber@trash.net> Sender: netfilter-devel-owner@vger.kernel.org List-ID: 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 --- include/datatype.h | 3 +++ src/datatype.c | 3 ++- src/evaluate.c | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/datatype.h b/include/datatype.h index 50b85c3..ca6ba9f 100644 --- a/include/datatype.h +++ b/include/datatype.h @@ -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 * diff --git a/src/datatype.c b/src/datatype.c index 7fc3287..b63d96d 100644 --- a/src/datatype.c +++ b/src/datatype.c @@ -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; } @@ -938,7 +939,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++; diff --git a/src/evaluate.c b/src/evaluate.c index 79edf02..9cb2376 100644 --- a/src/evaluate.c +++ b/src/evaluate.c @@ -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) -- 2.1.0