| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 (or any | ||
| 8 | * later) as published by the Free Software Foundation. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <nft.h> | ||
| 12 | |||
| 13 | #include <nftables.h> | ||
| 14 | #include <expression.h> | ||
| 15 | #include <datatype.h> | ||
| 16 | #include <gmputil.h> | ||
| 17 | #include <hash.h> | ||
| 18 | #include <utils.h> | ||
| 19 | |||
| 20 | 20 | static void hash_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 21 | { | ||
| 22 |
2/2✓ Branch 0 (2→3) taken 9 times.
✓ Branch 1 (2→5) taken 11 times.
|
20 | switch (expr->hash.type) { |
| 23 | 9 | case NFT_HASH_SYM: | |
| 24 | 9 | nft_print(octx, "symhash"); | |
| 25 | 9 | break; | |
| 26 | 11 | case NFT_HASH_JENKINS: | |
| 27 | default: | ||
| 28 | 11 | nft_print(octx, "jhash "); | |
| 29 | 11 | expr_print(expr->hash.expr, octx); | |
| 30 | } | ||
| 31 | |||
| 32 | 20 | nft_print(octx, " mod %u", expr->hash.mod); | |
| 33 |
2/2✓ Branch 0 (8→9) taken 4 times.
✓ Branch 1 (8→10) taken 16 times.
|
20 | if (expr->hash.seed_set) |
| 34 | 4 | nft_print(octx, " seed 0x%x", expr->hash.seed); | |
| 35 |
2/2✓ Branch 0 (10→11) taken 4 times.
✓ Branch 1 (10→12) taken 16 times.
|
20 | if (expr->hash.offset) |
| 36 | 4 | nft_print(octx, " offset %u", expr->hash.offset); | |
| 37 | 20 | } | |
| 38 | |||
| 39 | ✗ | static bool hash_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 40 | { | ||
| 41 | ✗ | return (!e1->hash.expr || | |
| 42 | ✗ | expr_cmp(e1->hash.expr, e2->hash.expr)) && | |
| 43 | ✗ | e1->hash.mod == e2->hash.mod && | |
| 44 | ✗ | e1->hash.seed_set == e2->hash.seed_set && | |
| 45 | ✗ | e1->hash.seed == e2->hash.seed && | |
| 46 | ✗ | e1->hash.offset == e2->hash.offset && | |
| 47 | ✗ | e1->hash.type == e2->hash.type; | |
| 48 | } | ||
| 49 | |||
| 50 | 43 | static void hash_expr_clone(struct expr *new, const struct expr *expr) | |
| 51 | { | ||
| 52 |
2/2✓ Branch 0 (2→3) taken 24 times.
✓ Branch 1 (2→5) taken 19 times.
|
43 | if (expr->hash.expr) |
| 53 | 24 | new->hash.expr = expr_clone(expr->hash.expr); | |
| 54 | 43 | new->hash.mod = expr->hash.mod; | |
| 55 | 43 | new->hash.seed_set = expr->hash.seed_set; | |
| 56 | 43 | new->hash.seed = expr->hash.seed; | |
| 57 | 43 | new->hash.offset = expr->hash.offset; | |
| 58 | 43 | new->hash.type = expr->hash.type; | |
| 59 | 43 | } | |
| 60 | |||
| 61 | 133 | static void hash_expr_destroy(struct expr *expr) | |
| 62 | { | ||
| 63 | 133 | expr_free(expr->hash.expr); | |
| 64 | 133 | } | |
| 65 | |||
| 66 | #define NFTNL_UDATA_HASH_TYPE 0 | ||
| 67 | #define NFTNL_UDATA_HASH_OFFSET 1 | ||
| 68 | #define NFTNL_UDATA_HASH_MOD 2 | ||
| 69 | #define NFTNL_UDATA_HASH_SEED 3 | ||
| 70 | #define NFTNL_UDATA_HASH_SEED_SET 4 | ||
| 71 | #define NFTNL_UDATA_HASH_MAX 5 | ||
| 72 | |||
| 73 | ✗ | static int hash_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 74 | const struct expr *expr) | ||
| 75 | { | ||
| 76 | ✗ | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_HASH_TYPE, expr->hash.type); | |
| 77 | ✗ | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_HASH_OFFSET, expr->hash.offset); | |
| 78 | ✗ | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_HASH_MOD, expr->hash.mod); | |
| 79 | ✗ | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_HASH_SEED, expr->hash.seed); | |
| 80 | ✗ | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_HASH_SEED_SET, expr->hash.seed_set); | |
| 81 | |||
| 82 | ✗ | return 0; | |
| 83 | } | ||
| 84 | |||
| 85 | ✗ | static int hash_parse_udata(const struct nftnl_udata *attr, void *data) | |
| 86 | { | ||
| 87 | ✗ | const struct nftnl_udata **ud = data; | |
| 88 | ✗ | uint8_t type = nftnl_udata_type(attr); | |
| 89 | ✗ | uint8_t len = nftnl_udata_len(attr); | |
| 90 | |||
| 91 | ✗ | switch (type) { | |
| 92 | ✗ | case NFTNL_UDATA_HASH_TYPE: | |
| 93 | case NFTNL_UDATA_HASH_OFFSET: | ||
| 94 | case NFTNL_UDATA_HASH_SEED: | ||
| 95 | case NFTNL_UDATA_HASH_SEED_SET: | ||
| 96 | case NFTNL_UDATA_HASH_MOD: | ||
| 97 | ✗ | if (len != sizeof(uint32_t)) | |
| 98 | ✗ | return -1; | |
| 99 | ✗ | break; | |
| 100 | ✗ | default: | |
| 101 | ✗ | return 0; | |
| 102 | } | ||
| 103 | |||
| 104 | ✗ | ud[type] = attr; | |
| 105 | ✗ | return 0; | |
| 106 | } | ||
| 107 | |||
| 108 | ✗ | static struct expr *hash_expr_parse_udata(const struct nftnl_udata *attr) | |
| 109 | { | ||
| 110 | ✗ | const struct nftnl_udata *ud[NFTNL_UDATA_HASH_MAX + 1] = {}; | |
| 111 | uint32_t type, seed, seed_set, mod, offset; | ||
| 112 | int err; | ||
| 113 | |||
| 114 | ✗ | err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr), | |
| 115 | hash_parse_udata, ud); | ||
| 116 | ✗ | if (err < 0) | |
| 117 | ✗ | return NULL; | |
| 118 | |||
| 119 | ✗ | if (!ud[NFTNL_UDATA_HASH_TYPE] || | |
| 120 | ✗ | !ud[NFTNL_UDATA_HASH_OFFSET] || | |
| 121 | ✗ | !ud[NFTNL_UDATA_HASH_SEED] || | |
| 122 | ✗ | !ud[NFTNL_UDATA_HASH_MOD] || | |
| 123 | ✗ | !ud[NFTNL_UDATA_HASH_SEED_SET]) | |
| 124 | ✗ | return NULL; | |
| 125 | |||
| 126 | ✗ | type = nftnl_udata_get_u32(ud[NFTNL_UDATA_HASH_TYPE]); | |
| 127 | ✗ | offset = nftnl_udata_get_u32(ud[NFTNL_UDATA_HASH_OFFSET]); | |
| 128 | ✗ | seed = nftnl_udata_get_u32(ud[NFTNL_UDATA_HASH_SEED]); | |
| 129 | ✗ | seed_set = nftnl_udata_get_u32(ud[NFTNL_UDATA_HASH_SEED_SET]); | |
| 130 | ✗ | mod = nftnl_udata_get_u32(ud[NFTNL_UDATA_HASH_MOD]); | |
| 131 | |||
| 132 | ✗ | return hash_expr_alloc(&internal_location, mod, seed_set, seed, | |
| 133 | offset, type); | ||
| 134 | } | ||
| 135 | |||
| 136 | const struct expr_ops hash_expr_ops = { | ||
| 137 | .type = EXPR_HASH, | ||
| 138 | .name = "hash", | ||
| 139 | .print = hash_expr_print, | ||
| 140 | .json = hash_expr_json, | ||
| 141 | .cmp = hash_expr_cmp, | ||
| 142 | .clone = hash_expr_clone, | ||
| 143 | .destroy = hash_expr_destroy, | ||
| 144 | .parse_udata = hash_expr_parse_udata, | ||
| 145 | .build_udata = hash_expr_build_udata, | ||
| 146 | }; | ||
| 147 | |||
| 148 | 90 | struct expr *hash_expr_alloc(const struct location *loc, | |
| 149 | uint32_t mod, | ||
| 150 | bool seed_set, uint32_t seed, | ||
| 151 | uint32_t offset, | ||
| 152 | enum nft_hash_types type) | ||
| 153 | { | ||
| 154 | struct expr *expr; | ||
| 155 | |||
| 156 | 90 | expr = expr_alloc(loc, EXPR_HASH, &integer_type, | |
| 157 | BYTEORDER_HOST_ENDIAN, 4 * BITS_PER_BYTE); | ||
| 158 | 90 | expr->hash.mod = mod; | |
| 159 | 90 | expr->hash.seed_set = seed_set; | |
| 160 | 90 | expr->hash.seed = seed; | |
| 161 | 90 | expr->hash.offset = offset; | |
| 162 | 90 | expr->hash.type = type; | |
| 163 | |||
| 164 | 90 | return expr; | |
| 165 | } | ||
| 166 |