From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: kaber@trash.net
Subject: [PATCH 2/2 nft] netlink_linearize: factor out prefix generation
Date: Mon, 2 Nov 2015 12:55:06 +0100 [thread overview]
Message-ID: <1446465306-3364-2-git-send-email-pablo@netfilter.org> (raw)
In-Reply-To: <1446465306-3364-1-git-send-email-pablo@netfilter.org>
Add a new netlink_gen_prefix() function that encapsulates the prefix
generation.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
This patch, I should have sent it before the one to add wildcard string.
src/netlink_linearize.c | 53 +++++++++++++++++++++++++++++--------------------
1 file changed, 31 insertions(+), 22 deletions(-)
diff --git a/src/netlink_linearize.c b/src/netlink_linearize.c
index aa44eea..a4cd370 100644
--- a/src/netlink_linearize.c
+++ b/src/netlink_linearize.c
@@ -290,13 +290,40 @@ static void payload_shift_value(const struct expr *left, struct expr *right)
mpz_lshift_ui(right->value, left->payload.offset % BITS_PER_BYTE);
}
+static struct expr *netlink_gen_prefix(struct netlink_linearize_ctx *ctx,
+ const struct expr *expr,
+ enum nft_registers sreg)
+{
+ struct nft_data_linearize nld, zero = {};
+ struct nftnl_expr *nle;
+ mpz_t mask;
+
+ mpz_init(mask);
+ mpz_prefixmask(mask, expr->right->len, expr->right->prefix_len);
+ netlink_gen_raw_data(mask, expr->right->byteorder,
+ expr->right->len / BITS_PER_BYTE, &nld);
+ mpz_clear(mask);
+
+ zero.len = nld.len;
+
+ nle = alloc_nft_expr("bitwise");
+ netlink_put_register(nle, NFTNL_EXPR_BITWISE_SREG, sreg);
+ netlink_put_register(nle, NFTNL_EXPR_BITWISE_DREG, sreg);
+ nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_LEN, nld.len);
+ nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_MASK, &nld.value, nld.len);
+ nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_XOR, &zero.value, zero.len);
+ nftnl_rule_add_expr(ctx->nlr, nle);
+
+ return expr->right->prefix;
+}
+
static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx,
const struct expr *expr,
enum nft_registers dreg)
{
+ struct nft_data_linearize nld;
struct nftnl_expr *nle;
enum nft_registers sreg;
- struct nft_data_linearize nld, zero = {};
struct expr *right;
assert(dreg == NFT_REG_VERDICT);
@@ -308,30 +335,12 @@ static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx,
netlink_gen_expr(ctx, expr->left, sreg);
switch (expr->right->ops->type) {
- case EXPR_PREFIX: {
- mpz_t mask;
-
- mpz_init(mask);
- mpz_prefixmask(mask, expr->right->len, expr->right->prefix_len);
- netlink_gen_raw_data(mask, expr->right->byteorder,
- expr->right->len / BITS_PER_BYTE, &nld);
- mpz_clear(mask);
-
- zero.len = nld.len;
-
- nle = alloc_nft_expr("bitwise");
- netlink_put_register(nle, NFTNL_EXPR_BITWISE_SREG, sreg);
- netlink_put_register(nle, NFTNL_EXPR_BITWISE_DREG, sreg);
- nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_LEN, nld.len);
- nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_MASK, &nld.value, nld.len);
- nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_XOR, &zero.value, zero.len);
- nftnl_rule_add_expr(ctx->nlr, nle);
-
- right = expr->right->prefix;
+ case EXPR_PREFIX:
+ right = netlink_gen_prefix(ctx, expr, sreg);
break;
- }
default:
right = expr->right;
+ break;
}
nle = alloc_nft_expr("cmp");
--
2.1.4
prev parent reply other threads:[~2015-11-02 11:48 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-02 11:55 [PATCH 1/2 nft] evaluate: check if table and chain exists when adding rules Pablo Neira Ayuso
2015-11-02 11:55 ` Pablo Neira Ayuso [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1446465306-3364-2-git-send-email-pablo@netfilter.org \
--to=pablo@netfilter.org \
--cc=kaber@trash.net \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).