netfilter: nft_hash: support of symmetric hash
Subsystems (2)
PATCH v2 2/2 netfilter: nft_hash: support of symmetric hash
This patch provides symmetric hash support according to source ip address and port, and destination ip address and port.
For this purpose, the __skb_get_hash_symmetric() is used to identify the flow as it uses FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL flag by default.
The new attribute NFTA_HASH_TYPE has been included to support different types of hashing functions. Currently supported NFT_HASH_JENKINS through jhash and NFT_HASH_SYM through symhash.
The main difference between both types are:
- jhash requires an expression with sreg, symhash doesn't.
- symhash supports modulus and offset, but not seed.
Examples:
nft add rule ip nat prerouting ct mark set jhash ip saddr mod 2 nft add rule ip nat prerouting ct mark set symhash mod 2
---
v2:
- Avoid warning due to 'const' from symhash eval skb
include/uapi/linux/netfilter/nf_tables.h | 13 +++++ net/netfilter/nft_hash.c | 98 +++++++++++++++++++++++++++++++- 2 files changed, 110 insertions(+), 1 deletion(-)
include/uapi/linux/netfilter/nf_tables.h +13 -0
@@ -793,6 +793,17 @@ enum nft_rt_keys { };/** +* enum nft_hash_types - nf_tables hash expression types +* +* @NFT_HASH_JENKINS: Jenkins Hash +* @NFT_HASH_SYM: Symmetric Hash +*/ +enum nft_hash_types { +NFT_HASH_JENKINS , +NFT_HASH_SYM , +}; + +/** * enum nft_hash_attributes - nf_tables hash expression netlink attributes * * @NFTA_HASH_SREG: source register (NLA_U32)
@@ -801,6 +812,7 @@ enum nft_rt_keys {* @NFTA_HASH_MODULUS: modulus value (NLA_U32) * @NFTA_HASH_SEED: seed value (NLA_U32) * @NFTA_HASH_OFFSET: add this offset value to hash result (NLA_U32) +* @NFTA_HASH_TYPE: hash operation (NLA_U32: nft_hash_types) */ enum nft_hash_attributes {NFTA_HASH_UNSPEC ,
@@ -810,6 +822,7 @@ enum nft_hash_attributes {NFTA_HASH_MODULUS ,NFTA_HASH_SEED ,NFTA_HASH_OFFSET , +NFTA_HASH_TYPE , __NFTA_HASH_MAX, };#define NFTA_HASH_MAX (__NFTA_HASH_MAX - 1)
net/netfilter/nft_hash.c +97 -1
130 lines not on this page · raw message
Signature · 1 line
2.11.0
Hi,
2017-03-01 1:38 GMT+08:00 Laura Garcia Liebana <[email protected]>:
[...]
net/netfilter/nft_hash.c static const struct nft_expr_ops nft_jhash_ops = { · line 211
Quoted 8 lines
net/netfilter/nft_hash.c +8
-0+static const struct nft_expr_ops * +nft_hash_select_ops (const struct nft_ctx *ctx, +const struct nlattr *const tb[]) +{ +u32 type; + +if (!tb[NFTA_HASH_TYPE ]) +return ERR_PTR (-EINVAL );
For compatibility reason, if tb[NFTA_HASH_TYPE] is NULL, maybe it's better to return &nft_jhash_ops instead of reporting error.
Pablo Neira Ayuso
maintainerQuoted 4 lines · Liping Zhang
Hi,
2017-03-01 1:38 GMT+08:00 Laura Garcia Liebana <[email protected]>:
[...]
Quoted 11 lines
net/netfilter/nft_hash.c static const struct nft_expr_ops nft_jhash_ops = { · line 211
Quoted 8 lines
net/netfilter/nft_hash.c +8
-0+static const struct nft_expr_ops * +nft_hash_select_ops (const struct nft_ctx *ctx, +const struct nlattr *const tb[]) +{ +u32 type; + +if (!tb[NFTA_HASH_TYPE ]) +return ERR_PTR (-EINVAL );For compatibility reason, if tb[NFTA_HASH_TYPE] is NULL, maybe it's better to return
&nft_jhash_opsinstead of reporting error.
Right.
I think this function should end up looking like this:
static const struct nft_expr_ops *
nft_hash_select_ops(const struct nft_ctx *ctx,
const struct nlattr * const tb[])
{
u32 type;
if (!tb[NFTA_HASH_TYPE])
return &nft_jhash_ops;
type = ntohl(nla_get_be32(tb[NFTA_HASH_TYPE]));
switch (type) {
case NFT_HASH_SYM:
return &nft_symhash_ops;
case NFT_HASH_JENKINS:
return &nft_jhash_ops;
default:
break;
}
return ERR_PTR(-EOPNOTSUPP);
}So, this returns nft_jhash_ops if not NFTA_HASH_TYPE is set.
Then, if NFTA_HASH_TYPE is set, select the type according to what userspace requests.
If userspace request a hash type we don't support, then bail out with "operation not supported".
Thanks.