summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaura Garcia Liebana <nevola@gmail.com>2017-02-23 12:11:08 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2017-03-06 17:59:14 +0100
commit1ebd1e974e95ea90389064265523840968f50859 (patch)
tree0e0874c800ff2c3961e709441e2c6632cfc6deb4 /src
parent059b9bf6fb31b971f79f83a01d9794288ab88a6e (diff)
expr: hash: support of symmetric hash
This patch provides symmetric hash support according to source ip address and port, and destination ip address and port. 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 Signed-off-by: Laura Garcia Liebana <laura.garcia@zevenet.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src')
-rw-r--r--src/expr/hash.c46
1 files changed, 40 insertions, 6 deletions
diff --git a/src/expr/hash.c b/src/expr/hash.c
index 83f9317..d870510 100644
--- a/src/expr/hash.c
+++ b/src/expr/hash.c
@@ -21,6 +21,7 @@
#include <libnftnl/rule.h>
struct nftnl_expr_hash {
+ enum nft_hash_types type;
enum nft_registers sreg;
enum nft_registers dreg;
unsigned int len;
@@ -34,7 +35,6 @@ nftnl_expr_hash_set(struct nftnl_expr *e, uint16_t type,
const void *data, uint32_t data_len)
{
struct nftnl_expr_hash *hash = nftnl_expr_data(e);
-
switch (type) {
case NFTNL_EXPR_HASH_SREG:
hash->sreg = *((uint32_t *)data);
@@ -54,6 +54,9 @@ nftnl_expr_hash_set(struct nftnl_expr *e, uint16_t type,
case NFTNL_EXPR_HASH_OFFSET:
hash->offset = *((uint32_t *)data);
break;
+ case NFTNL_EXPR_HASH_TYPE:
+ hash->type = *((uint32_t *)data);
+ break;
default:
return -1;
}
@@ -85,6 +88,9 @@ nftnl_expr_hash_get(const struct nftnl_expr *e, uint16_t type,
case NFTNL_EXPR_HASH_OFFSET:
*data_len = sizeof(hash->offset);
return &hash->offset;
+ case NFTNL_EXPR_HASH_TYPE:
+ *data_len = sizeof(hash->type);
+ return &hash->type;
}
return NULL;
}
@@ -104,6 +110,7 @@ static int nftnl_expr_hash_cb(const struct nlattr *attr, void *data)
case NFTA_HASH_MODULUS:
case NFTA_HASH_SEED:
case NFTA_HASH_OFFSET:
+ case NFTA_HASH_TYPE:
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
abi_breakage();
break;
@@ -130,6 +137,8 @@ nftnl_expr_hash_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
mnl_attr_put_u32(nlh, NFTA_HASH_SEED, htonl(hash->seed));
if (e->flags & (1 << NFTNL_EXPR_HASH_OFFSET))
mnl_attr_put_u32(nlh, NFTA_HASH_OFFSET, htonl(hash->offset));
+ if (e->flags & (1 << NFTNL_EXPR_HASH_TYPE))
+ mnl_attr_put_u32(nlh, NFTA_HASH_TYPE, htonl(hash->type));
}
static int
@@ -166,6 +175,10 @@ nftnl_expr_hash_parse(struct nftnl_expr *e, struct nlattr *attr)
hash->offset = ntohl(mnl_attr_get_u32(tb[NFTA_HASH_OFFSET]));
e->flags |= (1 << NFTNL_EXPR_HASH_OFFSET);
}
+ if (tb[NFTA_HASH_TYPE]) {
+ hash->type = ntohl(mnl_attr_get_u32(tb[NFTA_HASH_TYPE]));
+ e->flags |= (1 << NFTNL_EXPR_HASH_TYPE);
+ }
return ret;
}
@@ -174,7 +187,7 @@ static int nftnl_expr_hash_json_parse(struct nftnl_expr *e, json_t *root,
struct nftnl_parse_err *err)
{
#ifdef JSON_PARSING
- uint32_t sreg, dreg, len, modulus, seed, offset;
+ uint32_t sreg, dreg, len, modulus, seed, offset, type;
if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32,
&sreg, err) == 0)
@@ -200,6 +213,10 @@ static int nftnl_expr_hash_json_parse(struct nftnl_expr *e, json_t *root,
&offset, err) == 0)
nftnl_expr_set_u32(e, NFTNL_EXPR_HASH_OFFSET, offset);
+ if (nftnl_jansson_parse_val(root, "type", NFTNL_TYPE_U32,
+ &type, err) == 0)
+ nftnl_expr_set_u32(e, NFTNL_EXPR_HASH_TYPE, type);
+
return 0;
#else
errno = EOPNOTSUPP;
@@ -214,10 +231,23 @@ nftnl_expr_hash_snprintf_default(char *buf, size_t size,
struct nftnl_expr_hash *hash = nftnl_expr_data(e);
int len = size, offset = 0, ret;
- ret = snprintf(buf, len, "reg %u = jhash(reg %u, %u, 0x%x) %% mod %u ",
- hash->dreg, hash->sreg, hash->len, hash->seed,
- hash->modulus);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ switch (hash->type) {
+ case NFT_HASH_SYM:
+ ret =
+ snprintf(buf, len, "reg %u = symhash() %% mod %u ", hash->dreg,
+ hash->modulus);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ break;
+ case NFT_HASH_JENKINS:
+ default:
+ ret =
+ snprintf(buf, len,
+ "reg %u = jhash(reg %u, %u, 0x%x) %% mod %u ",
+ hash->dreg, hash->sreg, hash->len, hash->seed,
+ hash->modulus);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ break;
+ }
if (hash->offset) {
ret = snprintf(buf + offset, len, "offset %u ", hash->offset);
@@ -246,6 +276,8 @@ static int nftnl_expr_hash_export(char *buf, size_t size,
nftnl_buf_u32(&b, type, hash->seed, SEED);
if (e->flags & (1 << NFTNL_EXPR_HASH_OFFSET))
nftnl_buf_u32(&b, type, hash->offset, OFFSET);
+ if (e->flags & (1 << NFTNL_EXPR_HASH_TYPE))
+ nftnl_buf_u32(&b, type, hash->type, TYPE);
return nftnl_buf_done(&b);
}
@@ -285,6 +317,8 @@ static bool nftnl_expr_hash_cmp(const struct nftnl_expr *e1,
eq &= (h1->seed == h2->seed);
if (e1->flags & (1 << NFTNL_EXPR_HASH_OFFSET))
eq &= (h1->offset == h2->offset);
+ if (e1->flags & (1 << NFTNL_EXPR_HASH_TYPE))
+ eq &= (h1->type == h2->type);
return eq;
}