From 345236211715ffb7cc28f6ff0b26acb90181e738 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Fri, 26 Aug 2016 18:00:00 +0200 Subject: src: add hash expression This is special expression that transforms an input expression into a 32-bit unsigned integer. This expression takes a modulus parameter to scale the result and the random seed so the hash result becomes harder to predict. You can use it to set the packet mark, eg. # nft add rule x y meta mark set jhash ip saddr . ip daddr mod 2 seed 0xdeadbeef You can combine this with maps too, eg. # nft add rule x y dnat to jhash ip saddr mod 2 seed 0xdeadbeef map { \ 0 : 192.168.20.100, \ 1 : 192.168.30.100 \ } Currently, this expression implements the jenkins hash implementation available in the Linux kernel: http://lxr.free-electrons.com/source/include/linux/jhash.h But it should be possible to extend it to support any other hash function type. Signed-off-by: Pablo Neira Ayuso --- src/parser_bison.y | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/parser_bison.y') diff --git a/src/parser_bison.y b/src/parser_bison.y index 23e8b275..dc794656 100644 --- a/src/parser_bison.y +++ b/src/parser_bison.y @@ -411,6 +411,9 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token INC "inc" %token MOD "mod" +%token JHASH "jhash" +%token SEED "seed" + %token POSITION "position" %token COMMENT "comment" @@ -556,8 +559,8 @@ static void location_update(struct location *loc, struct location *rhs, int n) %type arp_hdr_expr %destructor { expr_free($$); } arp_hdr_expr %type arp_hdr_field -%type ip_hdr_expr icmp_hdr_expr numgen_expr -%destructor { expr_free($$); } ip_hdr_expr icmp_hdr_expr numgen_expr +%type ip_hdr_expr icmp_hdr_expr numgen_expr hash_expr +%destructor { expr_free($$); } ip_hdr_expr icmp_hdr_expr numgen_expr hash_expr %type ip_hdr_field icmp_hdr_field %type ip6_hdr_expr icmp6_hdr_expr %destructor { expr_free($$); } ip6_hdr_expr icmp6_hdr_expr @@ -1972,6 +1975,7 @@ primary_expr : symbol_expr { $$ = $1; } | meta_expr { $$ = $1; } | ct_expr { $$ = $1; } | numgen_expr { $$ = $1; } + | hash_expr { $$ = $1; } | '(' basic_expr ')' { $$ = $2; } ; @@ -2469,6 +2473,13 @@ numgen_expr : NUMGEN numgen_type MOD NUM } ; +hash_expr : JHASH expr MOD NUM SEED NUM + { + $$ = hash_expr_alloc(&@$, $4, $6); + $$->hash.expr = $2; + } + ; + ct_expr : CT ct_key { $$ = ct_expr_alloc(&@$, $2, -1); -- cgit v1.2.3