summaryrefslogtreecommitdiffstats
path: root/src/parser_bison.y
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@netfilter.org>2016-08-26 18:00:00 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2016-08-29 20:30:29 +0200
commit345236211715ffb7cc28f6ff0b26acb90181e738 (patch)
treeaebfc9aeece1dda1bb1ffab52aa93a9b1114f928 /src/parser_bison.y
parent13eeed6ea6f0a5d1353ee5ad14c4322695b4f59b (diff)
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 <pablo@netfilter.org>
Diffstat (limited to 'src/parser_bison.y')
-rw-r--r--src/parser_bison.y15
1 files changed, 13 insertions, 2 deletions
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 <expr> arp_hdr_expr
%destructor { expr_free($$); } arp_hdr_expr
%type <val> arp_hdr_field
-%type <expr> ip_hdr_expr icmp_hdr_expr numgen_expr
-%destructor { expr_free($$); } ip_hdr_expr icmp_hdr_expr numgen_expr
+%type <expr> ip_hdr_expr icmp_hdr_expr numgen_expr hash_expr
+%destructor { expr_free($$); } ip_hdr_expr icmp_hdr_expr numgen_expr hash_expr
%type <val> ip_hdr_field icmp_hdr_field
%type <expr> 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);