From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Laura Garcia Liebana <nevola@gmail.com>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: [PATCH v3] netfilter: nf_tables: add hash expression
Date: Wed, 10 Aug 2016 20:52:44 +0200 [thread overview]
Message-ID: <20160810185244.GA6927@salvia> (raw)
In-Reply-To: <20160810094333.GA28920@sonyv>
On Wed, Aug 10, 2016 at 11:43:36AM +0200, Laura Garcia Liebana wrote:
> diff --git a/net/netfilter/nft_hash.c b/net/netfilter/nft_hash.c
> new file mode 100644
> index 0000000..d0069eb
> --- /dev/null
> +++ b/net/netfilter/nft_hash.c
> @@ -0,0 +1,141 @@
> +/*
> + * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/netlink.h>
> +#include <linux/netfilter.h>
> +#include <linux/netfilter/nf_tables.h>
> +#include <net/netfilter/nf_tables.h>
> +#include <net/netfilter/nf_tables_core.h>
> +#include <linux/jhash.h>
> +
> +struct nft_hash {
> + enum nft_registers sreg:8;
> + enum nft_registers dreg:8;
> + u8 len;
> + u32 modulus;
> + u32 seed;
> +};
> +
> +static void nft_hash_eval(const struct nft_expr *expr,
> + struct nft_regs *regs,
> + const struct nft_pktinfo *pkt)
> +{
> + struct nft_hash *priv = nft_expr_priv(expr);
> + const void *data = ®s->data[priv->sreg];
> + u32 h;
> +
> + h = reciprocal_scale(jhash(data, priv->len, priv->seed), priv->modulus);
> +
> + regs->data[priv->dreg] = h;
> +}
> +
> +const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
> + [NFTA_HASH_SREG] = { .type = NLA_U32 },
> + [NFTA_HASH_DREG] = { .type = NLA_U32 },
> + [NFTA_HASH_LEN] = { .type = NLA_U32 },
> + [NFTA_HASH_MODULUS] = { .type = NLA_U32 },
> + [NFTA_HASH_SEED] = { .type = NLA_U32 },
> +};
> +
> +static int nft_hash_init(const struct nft_ctx *ctx,
> + const struct nft_expr *expr,
> + const struct nlattr * const tb[])
> +{
> + struct nft_hash *priv = nft_expr_priv(expr);
> + u32 len;
> + u32 seed = 0;
> +
> + if (!tb[NFTA_HASH_SREG] ||
> + !tb[NFTA_HASH_DREG] ||
> + !tb[NFTA_HASH_LEN] ||
> + !tb[NFTA_HASH_MODULUS])
> + return -EINVAL;
> +
> + priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]);
> + priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
> +
> + len = ntohl(nla_get_be32(tb[NFTA_HASH_LEN]));
> + if (len == 0 || len > U8_MAX)
> + return -EINVAL;
Use return -ERANGE here so we get in sync with lib/nlattr.c policy
validation, that returns this error for values out of range.
> +
> + priv->len = len;
> +
> + priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
> + if (priv->modulus <= 1)
> + return -EINVAL;
Use return -ERANGE instead.
> +
> + if (tb[NFTA_HASH_LEN])
typo here, this should be NFTA_HASH_SEED.
> + seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
I don't see any benefit from making this optional, worst case
userspace can pass zero to us, right?
> +
> + priv->seed = seed;
> +
> + return nft_validate_register_store(ctx, priv->sreg, NULL,
This should be a validate_register_load instead.
> + NFT_DATA_VALUE, len) &&
> + nft_validate_register_store(ctx, priv->dreg, NULL,
^
Comestic: If you do this, you have to get this aligned with the line
above tabs and then spaces, ie.
return nft_validate_register_store(ctx, priv->sreg, NULL,
NFT_DATA_VALUE, len) &&
nft_validate_register_store(ctx, priv->dreg, NULL,
Anyway, the code above needs a respin.
> + NFT_DATA_VALUE, sizeof(u32));
> +}
Thanks.
prev parent reply other threads:[~2016-08-10 18:52 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-10 9:43 [PATCH v3] netfilter: nf_tables: add hash expression Laura Garcia Liebana
2016-08-10 18:52 ` 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=20160810185244.GA6927@salvia \
--to=pablo@netfilter.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=nevola@gmail.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.