Nftables  0.9
Nftables like the firewall for Linux but next generation
hash.c
1 /*
2  * Hash expression definitions.
3  *
4  * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <nftables.h>
12 #include <expression.h>
13 #include <datatype.h>
14 #include <gmputil.h>
15 #include <hash.h>
16 #include <utils.h>
17 
18 static void hash_expr_print(const struct expr *expr, struct output_ctx *octx)
19 {
20  switch (expr->hash.type) {
21  case NFT_HASH_SYM:
22  octx->print(octx->ctx, "symhash");
23  break;
24  case NFT_HASH_JENKINS:
25  default:
26  octx->print(octx->ctx, "jhash ");
27  expr_print(expr->hash.expr, octx);
28  }
29 
30  octx->print(octx->ctx, " mod %u", expr->hash.mod);
31  if (expr->hash.seed_set)
32  octx->print(octx->ctx, " seed 0x%x", expr->hash.seed);
33  if (expr->hash.offset)
34  octx->print(octx->ctx, " offset %u", expr->hash.offset);
35 }
36 
37 static bool hash_expr_cmp(const struct expr *e1, const struct expr *e2)
38 {
39  return (e1->hash.expr ||
40  expr_cmp(e1->hash.expr, e2->hash.expr)) &&
41  e1->hash.mod == e2->hash.mod &&
42  e1->hash.seed_set == e2->hash.seed_set &&
43  e1->hash.seed == e2->hash.seed &&
44  e1->hash.offset == e2->hash.offset &&
45  e1->hash.type == e2->hash.type;
46 }
47 
48 static void hash_expr_clone(struct expr *new, const struct expr *expr)
49 {
50  if (expr->hash.expr)
51  new->hash.expr = expr_clone(expr->hash.expr);
52  new->hash.mod = expr->hash.mod;
53  new->hash.seed_set = expr->hash.seed_set;
54  new->hash.seed = expr->hash.seed;
55  new->hash.offset = expr->hash.offset;
56  new->hash.type = expr->hash.type;
57 }
58 
59 static const struct expr_ops hash_expr_ops = {
60  .type = EXPR_HASH,
61  .name = "hash",
62  .print = hash_expr_print,
63  .cmp = hash_expr_cmp,
64  .clone = hash_expr_clone,
65 };
66 
67 struct expr *hash_expr_alloc(const struct location *loc,
68  uint32_t mod,
69  bool seed_set, uint32_t seed,
70  uint32_t offset,
71  enum nft_hash_types type)
72 {
73  struct expr *expr;
74 
75  expr = expr_alloc(loc, &hash_expr_ops, &integer_type,
76  BYTEORDER_HOST_ENDIAN, 4 * BITS_PER_BYTE);
77  expr->hash.mod = mod;
78  expr->hash.seed_set = seed_set;
79  expr->hash.seed = seed;
80  expr->hash.offset = offset;
81  expr->hash.type = type;
82 
83  return expr;
84 }