| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * (C) 2016 by Laura Garcia <[email protected]> |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <stdint.h> |
| 8 | #include <string.h> |
| 9 | #include <arpa/inet.h> |
| 10 | #include <errno.h> |
| 11 | #include <linux/netfilter/nf_tables.h> |
| 12 | |
| 13 | #include "internal.h" |
| 14 | #include <libmnl/libmnl.h> |
| 15 | #include <libnftnl/expr.h> |
| 16 | #include <libnftnl/rule.h> |
| 17 | |
| 18 | struct nftnl_expr_hash { |
| 19 | enum nft_hash_types type; |
| 20 | enum nft_registers sreg; |
| 21 | enum nft_registers dreg; |
| 22 | unsigned int len; |
| 23 | unsigned int modulus; |
| 24 | unsigned int seed; |
| 25 | unsigned int offset; |
| 26 | }; |
| 27 | |
| 28 | static int |
| 29 | nftnl_expr_hash_set(struct nftnl_expr *e, uint16_t type, |
| 30 | const void *data, uint32_t data_len, uint32_t byteorder) |
| 31 | { |
| 32 | struct nftnl_expr_hash *hash = nftnl_expr_data(e); |
| 33 | switch (type) { |
| 34 | case NFTNL_EXPR_HASH_SREG: |
| 35 | memcpy(&hash->sreg, data, data_len); |
| 36 | break; |
| 37 | case NFTNL_EXPR_HASH_DREG: |
| 38 | memcpy(&hash->dreg, data, data_len); |
| 39 | break; |
| 40 | case NFTNL_EXPR_HASH_LEN: |
| 41 | memcpy(&hash->len, data, data_len); |
| 42 | break; |
| 43 | case NFTNL_EXPR_HASH_MODULUS: |
| 44 | memcpy(&hash->modulus, data, data_len); |
| 45 | break; |
| 46 | case NFTNL_EXPR_HASH_SEED: |
| 47 | memcpy(&hash->seed, data, data_len); |
| 48 | break; |
| 49 | case NFTNL_EXPR_HASH_OFFSET: |
| 50 | memcpy(&hash->offset, data, data_len); |
| 51 | break; |
| 52 | case NFTNL_EXPR_HASH_TYPE: |
| 53 | memcpy(&hash->type, data, data_len); |
| 54 | break; |
| 55 | default: |
| 56 | return -1; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static const void * |
| 62 | nftnl_expr_hash_get(const struct nftnl_expr *e, uint16_t type, |
| 63 | uint32_t *data_len) |
| 64 | { |
| 65 | struct nftnl_expr_hash *hash = nftnl_expr_data(e); |
| 66 | |
| 67 | switch (type) { |
| 68 | case NFTNL_EXPR_HASH_SREG: |
| 69 | *data_len = sizeof(hash->sreg); |
| 70 | return &hash->sreg; |
| 71 | case NFTNL_EXPR_HASH_DREG: |
| 72 | *data_len = sizeof(hash->dreg); |
| 73 | return &hash->dreg; |
| 74 | case NFTNL_EXPR_HASH_LEN: |
| 75 | *data_len = sizeof(hash->len); |
| 76 | return &hash->len; |
| 77 | case NFTNL_EXPR_HASH_MODULUS: |
| 78 | *data_len = sizeof(hash->modulus); |
| 79 | return &hash->modulus; |
| 80 | case NFTNL_EXPR_HASH_SEED: |
| 81 | *data_len = sizeof(hash->seed); |
| 82 | return &hash->seed; |
| 83 | case NFTNL_EXPR_HASH_OFFSET: |
| 84 | *data_len = sizeof(hash->offset); |
| 85 | return &hash->offset; |
| 86 | case NFTNL_EXPR_HASH_TYPE: |
| 87 | *data_len = sizeof(hash->type); |
| 88 | return &hash->type; |
| 89 | } |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | static int nftnl_expr_hash_cb(const struct nlattr *attr, void *data) |
| 94 | { |
| 95 | const struct nlattr **tb = data; |
| 96 | int type = mnl_attr_get_type(attr); |
| 97 | |
| 98 | if (mnl_attr_type_valid(attr, NFTA_HASH_MAX) < 0) |
| 99 | return MNL_CB_OK; |
| 100 | |
| 101 | switch (type) { |
| 102 | case NFTA_HASH_SREG: |
| 103 | case NFTA_HASH_DREG: |
| 104 | case NFTA_HASH_LEN: |
| 105 | case NFTA_HASH_MODULUS: |
| 106 | case NFTA_HASH_SEED: |
| 107 | case NFTA_HASH_OFFSET: |
| 108 | case NFTA_HASH_TYPE: |
| 109 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 110 | abi_breakage(); |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | tb[type] = attr; |
| 115 | return MNL_CB_OK; |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | nftnl_expr_hash_build(struct nlmsghdr *nlh, const struct nftnl_expr *e) |
| 120 | { |
| 121 | struct nftnl_expr_hash *hash = nftnl_expr_data(e); |
| 122 | |
| 123 | if (e->flags & (1 << NFTNL_EXPR_HASH_SREG)) |
| 124 | mnl_attr_put_u32(nlh, NFTA_HASH_SREG, htonl(hash->sreg)); |
| 125 | if (e->flags & (1 << NFTNL_EXPR_HASH_DREG)) |
| 126 | mnl_attr_put_u32(nlh, NFTA_HASH_DREG, htonl(hash->dreg)); |
| 127 | if (e->flags & (1 << NFTNL_EXPR_HASH_LEN)) |
| 128 | mnl_attr_put_u32(nlh, NFTA_HASH_LEN, htonl(hash->len)); |
| 129 | if (e->flags & (1 << NFTNL_EXPR_HASH_MODULUS)) |
| 130 | mnl_attr_put_u32(nlh, NFTA_HASH_MODULUS, htonl(hash->modulus)); |
| 131 | if (e->flags & (1 << NFTNL_EXPR_HASH_SEED)) |
| 132 | mnl_attr_put_u32(nlh, NFTA_HASH_SEED, htonl(hash->seed)); |
| 133 | if (e->flags & (1 << NFTNL_EXPR_HASH_OFFSET)) |
| 134 | mnl_attr_put_u32(nlh, NFTA_HASH_OFFSET, htonl(hash->offset)); |
| 135 | if (e->flags & (1 << NFTNL_EXPR_HASH_TYPE)) |
| 136 | mnl_attr_put_u32(nlh, NFTA_HASH_TYPE, htonl(hash->type)); |
| 137 | } |
| 138 | |
| 139 | static int |
| 140 | nftnl_expr_hash_parse(struct nftnl_expr *e, struct nlattr *attr) |
| 141 | { |
| 142 | struct nftnl_expr_hash *hash = nftnl_expr_data(e); |
| 143 | struct nlattr *tb[NFTA_HASH_MAX+1] = {}; |
| 144 | int ret = 0; |
| 145 | |
| 146 | if (mnl_attr_parse_nested(attr, nftnl_expr_hash_cb, tb) < 0) |
| 147 | return -1; |
| 148 | |
| 149 | if (tb[NFTA_HASH_SREG]) { |
| 150 | hash->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_HASH_SREG])); |
| 151 | e->flags |= (1 << NFTNL_EXPR_HASH_SREG); |
| 152 | } |
| 153 | if (tb[NFTA_HASH_DREG]) { |
| 154 | hash->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_HASH_DREG])); |
| 155 | e->flags |= (1 << NFTNL_EXPR_HASH_DREG); |
| 156 | } |
| 157 | if (tb[NFTA_HASH_LEN]) { |
| 158 | hash->len = ntohl(mnl_attr_get_u32(tb[NFTA_HASH_LEN])); |
| 159 | e->flags |= (1 << NFTNL_EXPR_HASH_LEN); |
| 160 | } |
| 161 | if (tb[NFTA_HASH_MODULUS]) { |
| 162 | hash->modulus = ntohl(mnl_attr_get_u32(tb[NFTA_HASH_MODULUS])); |
| 163 | e->flags |= (1 << NFTNL_EXPR_HASH_MODULUS); |
| 164 | } |
| 165 | if (tb[NFTA_HASH_SEED]) { |
| 166 | hash->seed = ntohl(mnl_attr_get_u32(tb[NFTA_HASH_SEED])); |
| 167 | e->flags |= (1 << NFTNL_EXPR_HASH_SEED); |
| 168 | } |
| 169 | if (tb[NFTA_HASH_OFFSET]) { |
| 170 | hash->offset = ntohl(mnl_attr_get_u32(tb[NFTA_HASH_OFFSET])); |
| 171 | e->flags |= (1 << NFTNL_EXPR_HASH_OFFSET); |
| 172 | } |
| 173 | if (tb[NFTA_HASH_TYPE]) { |
| 174 | hash->type = ntohl(mnl_attr_get_u32(tb[NFTA_HASH_TYPE])); |
| 175 | e->flags |= (1 << NFTNL_EXPR_HASH_TYPE); |
| 176 | } |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static int |
| 182 | nftnl_expr_hash_snprintf(char *buf, size_t remain, |
| 183 | uint32_t flags, const struct nftnl_expr *e) |
| 184 | { |
| 185 | struct nftnl_expr_hash *hash = nftnl_expr_data(e); |
| 186 | int offset = 0, ret; |
| 187 | |
| 188 | switch (hash->type) { |
| 189 | case NFT_HASH_SYM: |
| 190 | ret = |
| 191 | snprintf(buf, remain, "reg %u = symhash() %% mod %u ", |
| 192 | hash->dreg, |
| 193 | hash->modulus); |
| 194 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 195 | break; |
| 196 | case NFT_HASH_JENKINS: |
| 197 | default: |
| 198 | ret = |
| 199 | snprintf(buf, remain, |
| 200 | "reg %u = jhash(reg %u, %u, 0x%x) %% mod %u ", |
| 201 | hash->dreg, hash->sreg, hash->len, hash->seed, |
| 202 | hash->modulus); |
| 203 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | if (hash->offset) { |
| 208 | ret = snprintf(buf + offset, remain, "offset %u ", |
| 209 | hash->offset); |
| 210 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 211 | } |
| 212 | |
| 213 | return offset; |
| 214 | } |
| 215 | |
| 216 | static struct attr_policy hash_attr_policy[__NFTNL_EXPR_HASH_MAX] = { |
| 217 | [NFTNL_EXPR_HASH_SREG] = { .maxlen = sizeof(uint32_t) }, |
| 218 | [NFTNL_EXPR_HASH_DREG] = { .maxlen = sizeof(uint32_t) }, |
| 219 | [NFTNL_EXPR_HASH_LEN] = { .maxlen = sizeof(uint32_t) }, |
| 220 | [NFTNL_EXPR_HASH_MODULUS] = { .maxlen = sizeof(uint32_t) }, |
| 221 | [NFTNL_EXPR_HASH_SEED] = { .maxlen = sizeof(uint32_t) }, |
| 222 | [NFTNL_EXPR_HASH_OFFSET] = { .maxlen = sizeof(uint32_t) }, |
| 223 | [NFTNL_EXPR_HASH_TYPE] = { .maxlen = sizeof(uint32_t) }, |
| 224 | }; |
| 225 | |
| 226 | struct expr_ops expr_ops_hash = { |
| 227 | .name = "hash", |
| 228 | .alloc_len = sizeof(struct nftnl_expr_hash), |
| 229 | .nftnl_max_attr = __NFTNL_EXPR_HASH_MAX - 1, |
| 230 | .attr_policy = hash_attr_policy, |
| 231 | .set = nftnl_expr_hash_set, |
| 232 | .get = nftnl_expr_hash_get, |
| 233 | .parse = nftnl_expr_hash_parse, |
| 234 | .build = nftnl_expr_hash_build, |
| 235 | .output = nftnl_expr_hash_snprintf, |
| 236 | }; |