| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | ||
| 3 | * Copyright (c) 2013 Pablo Neira Ayuso <pablo@netfilter.org> | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License version 2 as | ||
| 7 | * published by the Free Software Foundation. | ||
| 8 | * | ||
| 9 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <nft.h> | ||
| 13 | |||
| 14 | #include <limits.h> | ||
| 15 | #include <linux/netfilter/nf_tables.h> | ||
| 16 | #include <arpa/inet.h> | ||
| 17 | #include <linux/netfilter/nf_nat.h> | ||
| 18 | #include <linux/netfilter.h> | ||
| 19 | #include <net/ethernet.h> | ||
| 20 | #include <netlink.h> | ||
| 21 | #include <rule.h> | ||
| 22 | #include <statement.h> | ||
| 23 | #include <expression.h> | ||
| 24 | #include <gmputil.h> | ||
| 25 | #include <utils.h> | ||
| 26 | #include <erec.h> | ||
| 27 | #include <sys/socket.h> | ||
| 28 | #include <libnftnl/udata.h> | ||
| 29 | #include <cache.h> | ||
| 30 | #include <xt.h> | ||
| 31 | |||
| 32 | 308099 | struct dl_proto_ctx *dl_proto_ctx(struct rule_pp_ctx *ctx) | |
| 33 | { | ||
| 34 | 308099 | return ctx->dl; | |
| 35 | } | ||
| 36 | |||
| 37 | 700 | static struct dl_proto_ctx *dl_proto_ctx_outer(struct rule_pp_ctx *ctx) | |
| 38 | { | ||
| 39 | 700 | return &ctx->_dl[0]; | |
| 40 | } | ||
| 41 | |||
| 42 | static int netlink_parse_expr(const struct nftnl_expr *nle, | ||
| 43 | struct netlink_parse_ctx *ctx); | ||
| 44 | |||
| 45 | ✗ | static void __fmtstring(3, 4) netlink_error(struct netlink_parse_ctx *ctx, | |
| 46 | const struct location *loc, | ||
| 47 | const char *fmt, ...) | ||
| 48 | { | ||
| 49 | struct error_record *erec; | ||
| 50 | va_list ap; | ||
| 51 | |||
| 52 | ✗ | va_start(ap, fmt); | |
| 53 | ✗ | erec = erec_vcreate(EREC_ERROR, loc, fmt, ap); | |
| 54 | ✗ | va_end(ap); | |
| 55 | ✗ | erec_queue(erec, ctx->msgs); | |
| 56 | ✗ | } | |
| 57 | |||
| 58 | 142159 | static unsigned int netlink_parse_register(const struct nftnl_expr *nle, | |
| 59 | unsigned int attr) | ||
| 60 | { | ||
| 61 | unsigned int reg; | ||
| 62 | |||
| 63 | 142159 | reg = nftnl_expr_get_u32(nle, attr); | |
| 64 | /* Translate 128bit registers to corresponding 32bit registers */ | ||
| 65 |
4/4✓ Branch 0 (3→4) taken 133921 times.
✓ Branch 1 (3→6) taken 8238 times.
✓ Branch 2 (4→5) taken 133018 times.
✓ Branch 3 (4→6) taken 903 times.
|
142159 | if (reg >= NFT_REG_1 && reg <= NFT_REG_4) |
| 66 | 133018 | reg = 1 + (reg - NFT_REG_1) * (NFT_REG_SIZE / NFT_REG32_SIZE); | |
| 67 |
2/2✓ Branch 0 (6→7) taken 903 times.
✓ Branch 1 (6→8) taken 8238 times.
|
9141 | else if (reg >= NFT_REG32_00) |
| 68 | 903 | reg = 1 + reg - NFT_REG32_00; | |
| 69 | |||
| 70 | 142159 | return reg; | |
| 71 | } | ||
| 72 | |||
| 73 | 68100 | static void netlink_set_register(struct netlink_parse_ctx *ctx, | |
| 74 | enum nft_registers reg, | ||
| 75 | struct expr *expr) | ||
| 76 | { | ||
| 77 |
2/4✓ Branch 0 (2→3) taken 68100 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→7) taken 68100 times.
|
68100 | if (reg == NFT_REG_VERDICT || reg > MAX_REGS) { |
| 78 | ✗ | netlink_error(ctx, &expr->location, | |
| 79 | "Invalid destination register %u", reg); | ||
| 80 | ✗ | expr_free(expr); | |
| 81 | ✗ | return; | |
| 82 | } | ||
| 83 | |||
| 84 | 68100 | expr_free(ctx->registers[reg]); | |
| 85 | |||
| 86 | 68100 | ctx->registers[reg] = expr; | |
| 87 | } | ||
| 88 | |||
| 89 | 68878 | static struct expr *netlink_get_register(struct netlink_parse_ctx *ctx, | |
| 90 | const struct location *loc, | ||
| 91 | enum nft_registers reg) | ||
| 92 | { | ||
| 93 | struct expr *expr; | ||
| 94 | |||
| 95 |
2/4✓ Branch 0 (2→3) taken 68878 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→6) taken 68878 times.
|
68878 | if (reg == NFT_REG_VERDICT || reg > MAX_REGS) { |
| 96 | ✗ | netlink_error(ctx, loc, "Invalid source register %u", reg); | |
| 97 | ✗ | return NULL; | |
| 98 | } | ||
| 99 | |||
| 100 | 68878 | expr = ctx->registers[reg]; | |
| 101 |
1/2✓ Branch 0 (6→7) taken 68878 times.
✗ Branch 1 (6→8) not taken.
|
68878 | if (expr != NULL) |
| 102 | 68878 | expr = expr_clone(expr); | |
| 103 | |||
| 104 | 68878 | return expr; | |
| 105 | } | ||
| 106 | |||
| 107 | 59661 | static void netlink_release_registers(struct netlink_parse_ctx *ctx) | |
| 108 | { | ||
| 109 | int i; | ||
| 110 | |||
| 111 |
2/2✓ Branch 0 (5→3) taken 1014237 times.
✓ Branch 1 (5→6) taken 59661 times.
|
1073898 | for (i = 0; i <= MAX_REGS; i++) |
| 112 | 1014237 | expr_free(ctx->registers[i]); | |
| 113 | 59661 | } | |
| 114 | |||
| 115 | 576 | static struct expr *netlink_parse_concat_expr(struct netlink_parse_ctx *ctx, | |
| 116 | const struct location *loc, | ||
| 117 | unsigned int reg, | ||
| 118 | unsigned int len) | ||
| 119 | { | ||
| 120 | struct expr *concat, *expr; | ||
| 121 | unsigned int consumed; | ||
| 122 | |||
| 123 | 576 | concat = concat_expr_alloc(loc); | |
| 124 |
2/2✓ Branch 0 (14→4) taken 1226 times.
✓ Branch 1 (14→15) taken 576 times.
|
1802 | while (len > 0) { |
| 125 | 1226 | expr = netlink_get_register(ctx, loc, reg); | |
| 126 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 1226 times.
|
1226 | if (expr == NULL) { |
| 127 | ✗ | netlink_error(ctx, loc, | |
| 128 | "Relational expression size mismatch"); | ||
| 129 | ✗ | goto err; | |
| 130 | } | ||
| 131 | 1226 | concat_expr_add(concat, expr); | |
| 132 | |||
| 133 | 1226 | consumed = netlink_padded_len(expr->len); | |
| 134 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 1226 times.
|
1226 | assert(consumed > 0); |
| 135 | 1226 | len -= consumed; | |
| 136 | 1226 | reg += netlink_register_space(expr->len); | |
| 137 | } | ||
| 138 | 576 | return concat; | |
| 139 | |||
| 140 | ✗ | err: | |
| 141 | ✗ | expr_free(concat); | |
| 142 | ✗ | return NULL; | |
| 143 | } | ||
| 144 | |||
| 145 | 202 | static struct expr *netlink_parse_concat_key(struct netlink_parse_ctx *ctx, | |
| 146 | const struct location *loc, | ||
| 147 | unsigned int reg, | ||
| 148 | const struct expr *key) | ||
| 149 | { | ||
| 150 | 202 | uint32_t type = key->dtype->type; | |
| 151 | 202 | unsigned int n, len = key->len; | |
| 152 | struct expr *concat, *expr; | ||
| 153 | unsigned int consumed; | ||
| 154 | |||
| 155 | 202 | concat = concat_expr_alloc(loc); | |
| 156 | 202 | n = div_round_up(fls(type), TYPE_BITS); | |
| 157 | |||
| 158 |
2/2✓ Branch 0 (20→5) taken 535 times.
✓ Branch 1 (20→21) taken 202 times.
|
737 | while (len > 0) { |
| 159 | const struct datatype *i; | ||
| 160 | |||
| 161 | 535 | expr = netlink_get_register(ctx, loc, reg); | |
| 162 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 535 times.
|
535 | if (expr == NULL) { |
| 163 | ✗ | netlink_error(ctx, loc, | |
| 164 | "Concat expression size mismatch"); | ||
| 165 | ✗ | goto err; | |
| 166 | } | ||
| 167 | |||
| 168 |
2/4✓ Branch 0 (9→10) taken 535 times.
✗ Branch 1 (9→14) not taken.
✓ Branch 2 (11→12) taken 535 times.
✗ Branch 3 (11→14) not taken.
|
535 | if (n > 0 && concat_subtype_id(type, --n)) { |
| 169 | 535 | i = concat_subtype_lookup(type, n); | |
| 170 | |||
| 171 | 535 | expr_set_type(expr, i, i->byteorder); | |
| 172 | } | ||
| 173 | |||
| 174 | 535 | concat_expr_add(concat, expr); | |
| 175 | |||
| 176 | 535 | consumed = netlink_padded_len(expr->len); | |
| 177 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 535 times.
|
535 | assert(consumed > 0); |
| 178 | 535 | len -= consumed; | |
| 179 | 535 | reg += netlink_register_space(expr->len); | |
| 180 | } | ||
| 181 | |||
| 182 | 202 | return concat; | |
| 183 | |||
| 184 | ✗ | err: | |
| 185 | ✗ | expr_free(concat); | |
| 186 | ✗ | return NULL; | |
| 187 | } | ||
| 188 | |||
| 189 | ✗ | static struct expr *netlink_parse_concat_data(struct netlink_parse_ctx *ctx, | |
| 190 | const struct location *loc, | ||
| 191 | unsigned int reg, | ||
| 192 | unsigned int len, | ||
| 193 | struct expr *data) | ||
| 194 | { | ||
| 195 | struct expr *concat, *expr, *i; | ||
| 196 | |||
| 197 | ✗ | concat = concat_expr_alloc(loc); | |
| 198 | ✗ | while (len > 0) { | |
| 199 | ✗ | expr = netlink_get_register(ctx, loc, reg); | |
| 200 | ✗ | if (expr == NULL) { | |
| 201 | ✗ | netlink_error(ctx, loc, | |
| 202 | "Relational expression size mismatch"); | ||
| 203 | ✗ | goto err; | |
| 204 | } | ||
| 205 | ✗ | i = constant_expr_splice(data, expr->len); | |
| 206 | ✗ | data->len -= netlink_padding_len(expr->len); | |
| 207 | ✗ | concat_expr_add(concat, i); | |
| 208 | |||
| 209 | ✗ | len -= netlink_padded_len(expr->len); | |
| 210 | ✗ | reg += netlink_register_space(expr->len); | |
| 211 | ✗ | expr_free(expr); | |
| 212 | } | ||
| 213 | ✗ | return concat; | |
| 214 | |||
| 215 | ✗ | err: | |
| 216 | ✗ | expr_free(concat); | |
| 217 | ✗ | return NULL; | |
| 218 | } | ||
| 219 | |||
| 220 | 916 | static void netlink_parse_chain_verdict(struct netlink_parse_ctx *ctx, | |
| 221 | const struct location *loc, | ||
| 222 | struct expr *expr, | ||
| 223 | enum nft_verdicts verdict) | ||
| 224 | { | ||
| 225 | 916 | char chain_name[NFT_CHAIN_MAXNAMELEN] = {}; | |
| 226 | struct chain *chain; | ||
| 227 | |||
| 228 | 916 | expr_chain_export(expr->chain, chain_name); | |
| 229 | 916 | chain = chain_binding_lookup(ctx->table, chain_name); | |
| 230 | |||
| 231 | /* Special case: 'nft list chain x y' needs to pull in implicit chains */ | ||
| 232 |
4/4✓ Branch 0 (4→5) taken 892 times.
✓ Branch 1 (4→8) taken 24 times.
✓ Branch 2 (5→6) taken 3 times.
✓ Branch 3 (5→8) taken 889 times.
|
916 | if (!chain && !strncmp(chain_name, "__chain", strlen("__chain"))) { |
| 233 | 3 | nft_chain_cache_update(ctx->nlctx, ctx->table, chain_name); | |
| 234 | 3 | chain = chain_binding_lookup(ctx->table, chain_name); | |
| 235 | } | ||
| 236 | |||
| 237 |
2/2✓ Branch 0 (8→9) taken 27 times.
✓ Branch 1 (8→12) taken 889 times.
|
916 | if (chain) { |
| 238 | 27 | ctx->stmt = chain_stmt_alloc(loc, chain_get(chain), verdict); | |
| 239 | 27 | expr_free(expr); | |
| 240 | } else { | ||
| 241 | 889 | ctx->stmt = verdict_stmt_alloc(loc, expr); | |
| 242 | } | ||
| 243 | 916 | } | |
| 244 | |||
| 245 | 7883 | static void netlink_parse_immediate(struct netlink_parse_ctx *ctx, | |
| 246 | const struct location *loc, | ||
| 247 | const struct nftnl_expr *nle) | ||
| 248 | { | ||
| 249 | struct nft_data_delinearize nld; | ||
| 250 | enum nft_registers dreg; | ||
| 251 | struct expr *expr; | ||
| 252 | |||
| 253 |
2/2✓ Branch 0 (3→4) taken 7110 times.
✓ Branch 1 (3→9) taken 773 times.
|
7883 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_IMM_VERDICT)) { |
| 254 | 7110 | nld.verdict = nftnl_expr_get_u32(nle, NFTNL_EXPR_IMM_VERDICT); | |
| 255 |
2/2✓ Branch 0 (6→7) taken 916 times.
✓ Branch 1 (6→13) taken 6194 times.
|
7110 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_IMM_CHAIN)) { |
| 256 | 916 | nld.chain = nftnl_expr_get(nle, NFTNL_EXPR_IMM_CHAIN, | |
| 257 | &nld.len); | ||
| 258 | } | ||
| 259 |
1/2✓ Branch 0 (10→11) taken 773 times.
✗ Branch 1 (10→13) not taken.
|
773 | } else if (nftnl_expr_is_set(nle, NFTNL_EXPR_IMM_DATA)) { |
| 260 | 773 | nld.value = nftnl_expr_get(nle, NFTNL_EXPR_IMM_DATA, &nld.len); | |
| 261 | } | ||
| 262 | |||
| 263 | 7883 | dreg = netlink_parse_register(nle, NFTNL_EXPR_IMM_DREG); | |
| 264 | 7883 | expr = netlink_alloc_data(loc, &nld, dreg); | |
| 265 | |||
| 266 |
2/2✓ Branch 0 (15→16) taken 7110 times.
✓ Branch 1 (15→24) taken 773 times.
|
7883 | if (dreg == NFT_REG_VERDICT) { |
| 267 |
3/3✓ Branch 0 (16→17) taken 802 times.
✓ Branch 1 (16→19) taken 114 times.
✓ Branch 2 (16→21) taken 6194 times.
|
7110 | switch (expr->verdict) { |
| 268 | 802 | case NFT_JUMP: | |
| 269 | 802 | netlink_parse_chain_verdict(ctx, loc, expr, NFT_JUMP); | |
| 270 | 802 | break; | |
| 271 | 114 | case NFT_GOTO: | |
| 272 | 114 | netlink_parse_chain_verdict(ctx, loc, expr, NFT_GOTO); | |
| 273 | 114 | break; | |
| 274 | 6194 | default: | |
| 275 | 6194 | ctx->stmt = verdict_stmt_alloc(loc, expr); | |
| 276 | 6194 | break; | |
| 277 | } | ||
| 278 | } else { | ||
| 279 | 773 | netlink_set_register(ctx, dreg, expr); | |
| 280 | } | ||
| 281 | 7883 | } | |
| 282 | |||
| 283 | 61 | static void netlink_parse_xfrm(struct netlink_parse_ctx *ctx, | |
| 284 | const struct location *loc, | ||
| 285 | const struct nftnl_expr *nle) | ||
| 286 | { | ||
| 287 | enum nft_registers dreg; | ||
| 288 | enum nft_xfrm_keys key; | ||
| 289 | struct expr *expr; | ||
| 290 | uint32_t spnum; | ||
| 291 | uint8_t dir; | ||
| 292 | |||
| 293 | 61 | key = nftnl_expr_get_u32(nle, NFTNL_EXPR_XFRM_KEY); | |
| 294 | 61 | dir = nftnl_expr_get_u8(nle, NFTNL_EXPR_XFRM_DIR); | |
| 295 | 61 | spnum = nftnl_expr_get_u32(nle, NFTNL_EXPR_XFRM_SPNUM); | |
| 296 | 61 | expr = xfrm_expr_alloc(loc, dir, spnum, key); | |
| 297 | |||
| 298 | 61 | dreg = netlink_parse_register(nle, NFTNL_EXPR_XFRM_DREG); | |
| 299 | 61 | netlink_set_register(ctx, dreg, expr); | |
| 300 | 61 | } | |
| 301 | |||
| 302 | 1445 | static enum ops netlink_parse_range_op(const struct nftnl_expr *nle) | |
| 303 | { | ||
| 304 |
2/3✓ Branch 0 (3→4) taken 790 times.
✓ Branch 1 (3→5) taken 655 times.
✗ Branch 2 (3→6) not taken.
|
1445 | switch (nftnl_expr_get_u32(nle, NFTNL_EXPR_RANGE_OP)) { |
| 305 | 790 | case NFT_RANGE_EQ: | |
| 306 | 790 | return OP_EQ; | |
| 307 | 655 | case NFT_RANGE_NEQ: | |
| 308 | 655 | return OP_NEQ; | |
| 309 | ✗ | default: | |
| 310 | ✗ | return OP_INVALID; | |
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | 1445 | static void netlink_parse_range(struct netlink_parse_ctx *ctx, | |
| 315 | const struct location *loc, | ||
| 316 | const struct nftnl_expr *nle) | ||
| 317 | { | ||
| 318 | struct expr *expr, *left, *right, *from, *to; | ||
| 319 | struct nft_data_delinearize nld; | ||
| 320 | enum nft_registers sreg; | ||
| 321 | enum ops op; | ||
| 322 | |||
| 323 | 1445 | sreg = netlink_parse_register(nle, NFTNL_EXPR_RANGE_SREG); | |
| 324 | 1445 | left = netlink_get_register(ctx, loc, sreg); | |
| 325 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 1445 times.
|
1445 | if (left == NULL) |
| 326 | ✗ | return netlink_error(ctx, loc, | |
| 327 | "Relational expression has no left hand side"); | ||
| 328 | |||
| 329 | 1445 | op = netlink_parse_range_op(nle); | |
| 330 | |||
| 331 | 1445 | nld.value = nftnl_expr_get(nle, NFTNL_EXPR_RANGE_FROM_DATA, &nld.len); | |
| 332 | 1445 | from = netlink_alloc_value(loc, &nld); | |
| 333 | |||
| 334 | 1445 | nld.value = nftnl_expr_get(nle, NFTNL_EXPR_RANGE_TO_DATA, &nld.len); | |
| 335 | 1445 | to = netlink_alloc_value(loc, &nld); | |
| 336 | |||
| 337 | 1445 | right = range_expr_alloc(loc, from, to); | |
| 338 | 1445 | expr = relational_expr_alloc(loc, op, left, right); | |
| 339 | 1445 | ctx->stmt = expr_stmt_alloc(loc, expr); | |
| 340 | } | ||
| 341 | |||
| 342 | 55322 | static enum ops netlink_parse_cmp_op(const struct nftnl_expr *nle) | |
| 343 | { | ||
| 344 |
5/7✓ Branch 0 (3→4) taken 53691 times.
✓ Branch 1 (3→5) taken 1432 times.
✓ Branch 2 (3→6) taken 78 times.
✗ Branch 3 (3→7) not taken.
✓ Branch 4 (3→8) taken 117 times.
✓ Branch 5 (3→9) taken 4 times.
✗ Branch 6 (3→10) not taken.
|
55322 | switch (nftnl_expr_get_u32(nle, NFTNL_EXPR_CMP_OP)) { |
| 345 | 53691 | case NFT_CMP_EQ: | |
| 346 | 53691 | return OP_EQ; | |
| 347 | 1432 | case NFT_CMP_NEQ: | |
| 348 | 1432 | return OP_NEQ; | |
| 349 | 78 | case NFT_CMP_LT: | |
| 350 | 78 | return OP_LT; | |
| 351 | ✗ | case NFT_CMP_LTE: | |
| 352 | ✗ | return OP_LTE; | |
| 353 | 117 | case NFT_CMP_GT: | |
| 354 | 117 | return OP_GT; | |
| 355 | 4 | case NFT_CMP_GTE: | |
| 356 | 4 | return OP_GTE; | |
| 357 | ✗ | default: | |
| 358 | ✗ | return OP_INVALID; | |
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | 55322 | static void netlink_parse_cmp(struct netlink_parse_ctx *ctx, | |
| 363 | const struct location *loc, | ||
| 364 | const struct nftnl_expr *nle) | ||
| 365 | { | ||
| 366 | struct nft_data_delinearize nld; | ||
| 367 | enum nft_registers sreg; | ||
| 368 | struct expr *expr, *left, *right, *tmp; | ||
| 369 | enum ops op; | ||
| 370 | |||
| 371 | 55322 | sreg = netlink_parse_register(nle, NFTNL_EXPR_CMP_SREG); | |
| 372 | 55322 | left = netlink_get_register(ctx, loc, sreg); | |
| 373 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 55322 times.
|
55322 | if (left == NULL) |
| 374 | 55322 | return netlink_error(ctx, loc, | |
| 375 | "Relational expression has no left " | ||
| 376 | "hand side"); | ||
| 377 | |||
| 378 | 55322 | op = netlink_parse_cmp_op(nle); | |
| 379 | |||
| 380 | 55322 | nld.value = nftnl_expr_get(nle, NFTNL_EXPR_CMP_DATA, &nld.len); | |
| 381 | 55322 | right = netlink_alloc_value(loc, &nld); | |
| 382 | |||
| 383 |
4/4✓ Branch 0 (10→11) taken 36 times.
✓ Branch 1 (10→16) taken 55286 times.
✓ Branch 2 (12→13) taken 8 times.
✓ Branch 3 (12→16) taken 28 times.
|
55358 | if (left->len > right->len && |
| 384 | 36 | expr_basetype(left) != &string_type) { | |
| 385 | 8 | mpz_lshift_ui(right->value, left->len - right->len); | |
| 386 | 8 | right = prefix_expr_alloc(loc, right, right->len); | |
| 387 | 8 | right->prefix->len = left->len; | |
| 388 |
3/4✓ Branch 0 (16→17) taken 55312 times.
✓ Branch 1 (16→27) taken 2 times.
✗ Branch 2 (17→18) not taken.
✓ Branch 3 (17→27) taken 55312 times.
|
55314 | } else if (left->len > 0 && left->len < right->len) { |
| 389 | ✗ | expr_free(left); | |
| 390 | ✗ | left = netlink_parse_concat_expr(ctx, loc, sreg, right->len); | |
| 391 | ✗ | if (left == NULL) | |
| 392 | ✗ | goto err_free; | |
| 393 | ✗ | tmp = netlink_parse_concat_data(ctx, loc, sreg, right->len, right); | |
| 394 | ✗ | if (tmp == NULL) | |
| 395 | ✗ | goto err_free; | |
| 396 | ✗ | expr_free(right); | |
| 397 | ✗ | right = tmp; | |
| 398 | } | ||
| 399 | |||
| 400 | 55322 | expr = relational_expr_alloc(loc, op, left, right); | |
| 401 | 55322 | ctx->stmt = expr_stmt_alloc(loc, expr); | |
| 402 | 55322 | return; | |
| 403 | ✗ | err_free: | |
| 404 | ✗ | expr_free(left); | |
| 405 | ✗ | expr_free(right); | |
| 406 | } | ||
| 407 | |||
| 408 | 4752 | static void netlink_parse_lookup(struct netlink_parse_ctx *ctx, | |
| 409 | const struct location *loc, | ||
| 410 | const struct nftnl_expr *nle) | ||
| 411 | { | ||
| 412 | enum nft_registers sreg, dreg; | ||
| 413 | const char *name; | ||
| 414 | struct expr *expr, *left, *right; | ||
| 415 | struct set *set; | ||
| 416 | uint32_t flag; | ||
| 417 | |||
| 418 | 4752 | name = nftnl_expr_get_str(nle, NFTNL_EXPR_LOOKUP_SET); | |
| 419 | 4752 | set = set_cache_find(ctx->table, name); | |
| 420 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 4752 times.
|
4752 | if (set == NULL) |
| 421 | ✗ | return netlink_error(ctx, loc, | |
| 422 | "Unknown set '%s' in lookup expression", | ||
| 423 | name); | ||
| 424 | |||
| 425 | 4752 | sreg = netlink_parse_register(nle, NFTNL_EXPR_LOOKUP_SREG); | |
| 426 | 4752 | left = netlink_get_register(ctx, loc, sreg); | |
| 427 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 4752 times.
|
4752 | if (left == NULL) |
| 428 | ✗ | return netlink_error(ctx, loc, | |
| 429 | "Lookup expression has no left hand side"); | ||
| 430 | |||
| 431 |
2/2✓ Branch 0 (12→13) taken 524 times.
✓ Branch 1 (12→17) taken 4228 times.
|
4752 | if (left->len < set->key->len) { |
| 432 | 524 | expr_free(left); | |
| 433 | 524 | left = netlink_parse_concat_expr(ctx, loc, sreg, set->key->len); | |
| 434 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 524 times.
|
524 | if (left == NULL) |
| 435 | ✗ | return; | |
| 436 | } | ||
| 437 | |||
| 438 | 4752 | right = set_ref_expr_alloc(loc, set); | |
| 439 | |||
| 440 |
2/2✓ Branch 0 (19→20) taken 899 times.
✓ Branch 1 (19→25) taken 3853 times.
|
4752 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_LOOKUP_DREG)) { |
| 441 | 899 | dreg = netlink_parse_register(nle, NFTNL_EXPR_LOOKUP_DREG); | |
| 442 | 899 | expr = map_expr_alloc(loc, left, right); | |
| 443 |
2/2✓ Branch 0 (22→23) taken 316 times.
✓ Branch 1 (22→26) taken 583 times.
|
899 | if (dreg != NFT_REG_VERDICT) |
| 444 | 316 | return netlink_set_register(ctx, dreg, expr); | |
| 445 | } else { | ||
| 446 | 3853 | expr = relational_expr_alloc(loc, OP_EQ, left, right); | |
| 447 | } | ||
| 448 | |||
| 449 |
1/2✓ Branch 0 (27→28) taken 4436 times.
✗ Branch 1 (27→31) not taken.
|
4436 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_LOOKUP_FLAGS)) { |
| 450 | 4436 | flag = nftnl_expr_get_u32(nle, NFTNL_EXPR_LOOKUP_FLAGS); | |
| 451 |
2/2✓ Branch 0 (29→30) taken 970 times.
✓ Branch 1 (29→31) taken 3466 times.
|
4436 | if (flag & NFT_LOOKUP_F_INV) |
| 452 | 970 | expr->op = OP_NEQ; | |
| 453 | } | ||
| 454 | |||
| 455 | 4436 | ctx->stmt = expr_stmt_alloc(loc, expr); | |
| 456 | } | ||
| 457 | |||
| 458 | static struct expr * | ||
| 459 | 1906 | netlink_parse_bitwise_mask_xor(struct netlink_parse_ctx *ctx, | |
| 460 | const struct location *loc, | ||
| 461 | const struct nftnl_expr *nle, | ||
| 462 | enum nft_registers sreg, | ||
| 463 | struct expr *left) | ||
| 464 | { | ||
| 465 | struct nft_data_delinearize nld; | ||
| 466 | struct expr *expr, *mask, *xor, *or; | ||
| 467 | mpz_t m, x, o; | ||
| 468 | |||
| 469 | 1906 | expr = left; | |
| 470 | |||
| 471 | 1906 | nld.value = nftnl_expr_get(nle, NFTNL_EXPR_BITWISE_MASK, &nld.len); | |
| 472 | 1906 | mask = netlink_alloc_value(loc, &nld); | |
| 473 | 1906 | mpz_init_set(m, mask->value); | |
| 474 | |||
| 475 | 1906 | nld.value = nftnl_expr_get(nle, NFTNL_EXPR_BITWISE_XOR, &nld.len); | |
| 476 | 1906 | xor = netlink_alloc_value(loc, &nld); | |
| 477 | 1906 | mpz_init_set(x, xor->value); | |
| 478 | |||
| 479 | 1906 | mpz_init_set_ui(o, 0); | |
| 480 |
5/6✓ Branch 0 (9→10) taken 14 times.
✓ Branch 1 (9→12) taken 1892 times.
✓ Branch 2 (10→11) taken 14 times.
✗ Branch 3 (10→12) not taken.
✓ Branch 4 (11→12) taken 4 times.
✓ Branch 5 (11→16) taken 10 times.
|
1906 | if (mpz_scan0(m, 0) != mask->len || mpz_cmp_ui(x, 0)) { |
| 481 | /* o = (m & x) ^ x */ | ||
| 482 | 1896 | mpz_and(o, m, x); | |
| 483 | 1896 | mpz_xor(o, o, x); | |
| 484 | /* x &= m */ | ||
| 485 | 1896 | mpz_and(x, x, m); | |
| 486 | /* m |= o */ | ||
| 487 | 1896 | mpz_ior(m, m, o); | |
| 488 | } | ||
| 489 | |||
| 490 |
3/4✓ Branch 0 (16→17) taken 1906 times.
✗ Branch 1 (16→19) not taken.
✓ Branch 2 (17→18) taken 212 times.
✓ Branch 3 (17→19) taken 1694 times.
|
1906 | if (left->len > 0 && mpz_scan0(m, 0) >= left->len) { |
| 491 | /* mask encompasses the entire value */ | ||
| 492 | 212 | expr_free(mask); | |
| 493 | } else { | ||
| 494 | 1694 | mpz_set(mask->value, m); | |
| 495 | 1694 | expr = binop_expr_alloc(loc, OP_AND, expr, mask); | |
| 496 | 1694 | expr->len = left->len; | |
| 497 | } | ||
| 498 | |||
| 499 |
3/4✓ Branch 0 (22→23) taken 1906 times.
✗ Branch 1 (22→24) not taken.
✓ Branch 2 (23→24) taken 4 times.
✓ Branch 3 (23→27) taken 1902 times.
|
1906 | if (mpz_cmp_ui(x, 0)) { |
| 500 | 4 | mpz_set(xor->value, x); | |
| 501 | 4 | expr = binop_expr_alloc(loc, OP_XOR, expr, xor); | |
| 502 | 4 | expr->len = left->len; | |
| 503 | } else | ||
| 504 | 1902 | expr_free(xor); | |
| 505 | |||
| 506 |
3/4✓ Branch 0 (28→29) taken 1906 times.
✗ Branch 1 (28→30) not taken.
✓ Branch 2 (29→30) taken 284 times.
✓ Branch 3 (29→35) taken 1622 times.
|
1906 | if (mpz_cmp_ui(o, 0)) { |
| 507 | 284 | nld.value = nftnl_expr_get(nle, NFTNL_EXPR_BITWISE_XOR, | |
| 508 | &nld.len); | ||
| 509 | |||
| 510 | 284 | or = netlink_alloc_value(loc, &nld); | |
| 511 | 284 | mpz_set(or->value, o); | |
| 512 | 284 | expr = binop_expr_alloc(loc, OP_OR, expr, or); | |
| 513 | 284 | expr->len = left->len; | |
| 514 | } | ||
| 515 | |||
| 516 | 1906 | mpz_clear(m); | |
| 517 | 1906 | mpz_clear(x); | |
| 518 | 1906 | mpz_clear(o); | |
| 519 | |||
| 520 | 1906 | return expr; | |
| 521 | } | ||
| 522 | |||
| 523 | 75 | static struct expr *netlink_parse_bitwise_bool(struct netlink_parse_ctx *ctx, | |
| 524 | const struct location *loc, | ||
| 525 | const struct nftnl_expr *nle, | ||
| 526 | enum nft_bitwise_ops op, | ||
| 527 | enum nft_registers sreg, | ||
| 528 | struct expr *left) | ||
| 529 | { | ||
| 530 | enum nft_registers sreg2; | ||
| 531 | struct expr *right, *expr; | ||
| 532 | |||
| 533 | 75 | sreg2 = netlink_parse_register(nle, NFTNL_EXPR_BITWISE_SREG2); | |
| 534 | 75 | right = netlink_get_register(ctx, loc, sreg2); | |
| 535 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 75 times.
|
75 | if (right == NULL) { |
| 536 | ✗ | netlink_error(ctx, loc, | |
| 537 | "Bitwise expression has no right-hand expression"); | ||
| 538 | ✗ | return NULL; | |
| 539 | } | ||
| 540 | |||
| 541 |
2/2✓ Branch 0 (7→8) taken 69 times.
✓ Branch 1 (7→11) taken 6 times.
|
144 | expr = binop_expr_alloc(loc, |
| 542 |
2/2✓ Branch 0 (8→9) taken 2 times.
✓ Branch 1 (8→10) taken 67 times.
|
69 | op == NFT_BITWISE_XOR ? OP_XOR : |
| 543 | op == NFT_BITWISE_AND ? OP_AND : OP_OR, | ||
| 544 | left, right); | ||
| 545 | |||
| 546 |
1/2✓ Branch 0 (13→14) taken 75 times.
✗ Branch 1 (13→15) not taken.
|
75 | if (left->len > 0) |
| 547 | 75 | expr->len = left->len; | |
| 548 | |||
| 549 | 75 | return expr; | |
| 550 | } | ||
| 551 | |||
| 552 | 164 | static struct expr *netlink_parse_bitwise_shift(struct netlink_parse_ctx *ctx, | |
| 553 | const struct location *loc, | ||
| 554 | const struct nftnl_expr *nle, | ||
| 555 | enum nft_bitwise_ops op, | ||
| 556 | enum nft_registers sreg, | ||
| 557 | struct expr *left) | ||
| 558 | { | ||
| 559 | struct nft_data_delinearize nld; | ||
| 560 | struct expr *expr, *right; | ||
| 561 | |||
| 562 | 164 | nld.value = nftnl_expr_get(nle, NFTNL_EXPR_BITWISE_DATA, &nld.len); | |
| 563 | 164 | right = netlink_alloc_value(loc, &nld); | |
| 564 | 164 | right->byteorder = BYTEORDER_HOST_ENDIAN; | |
| 565 | |||
| 566 |
2/2✓ Branch 0 (4→5) taken 42 times.
✓ Branch 1 (4→6) taken 122 times.
|
164 | expr = binop_expr_alloc(loc, |
| 567 | op == NFT_BITWISE_LSHIFT ? OP_LSHIFT : OP_RSHIFT, | ||
| 568 | left, right); | ||
| 569 | 164 | expr->len = nftnl_expr_get_u32(nle, NFTNL_EXPR_BITWISE_LEN) * BITS_PER_BYTE; | |
| 570 | |||
| 571 | 164 | return expr; | |
| 572 | } | ||
| 573 | |||
| 574 | 2145 | static void netlink_parse_bitwise(struct netlink_parse_ctx *ctx, | |
| 575 | const struct location *loc, | ||
| 576 | const struct nftnl_expr *nle) | ||
| 577 | { | ||
| 578 | enum nft_registers sreg, dreg; | ||
| 579 | struct expr *expr, *left; | ||
| 580 | enum nft_bitwise_ops op; | ||
| 581 | |||
| 582 | 2145 | sreg = netlink_parse_register(nle, NFTNL_EXPR_BITWISE_SREG); | |
| 583 | 2145 | left = netlink_get_register(ctx, loc, sreg); | |
| 584 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 2145 times.
|
2145 | if (left == NULL) |
| 585 | ✗ | return netlink_error(ctx, loc, | |
| 586 | "Bitwise expression has no left " | ||
| 587 | "hand side"); | ||
| 588 | |||
| 589 | 2145 | op = nftnl_expr_get_u32(nle, NFTNL_EXPR_BITWISE_OP); | |
| 590 | |||
| 591 |
3/4✓ Branch 0 (8→9) taken 1906 times.
✓ Branch 1 (8→10) taken 75 times.
✓ Branch 2 (8→12) taken 164 times.
✗ Branch 3 (8→14) not taken.
|
2145 | switch (op) { |
| 592 | 1906 | case NFT_BITWISE_MASK_XOR: | |
| 593 | 1906 | expr = netlink_parse_bitwise_mask_xor(ctx, loc, nle, sreg, | |
| 594 | left); | ||
| 595 | 1906 | break; | |
| 596 | 75 | case NFT_BITWISE_XOR: | |
| 597 | case NFT_BITWISE_AND: | ||
| 598 | case NFT_BITWISE_OR: | ||
| 599 | 75 | expr = netlink_parse_bitwise_bool(ctx, loc, nle, op, | |
| 600 | sreg, left); | ||
| 601 | 75 | break; | |
| 602 | 164 | case NFT_BITWISE_LSHIFT: | |
| 603 | case NFT_BITWISE_RSHIFT: | ||
| 604 | 164 | expr = netlink_parse_bitwise_shift(ctx, loc, nle, op, | |
| 605 | sreg, left); | ||
| 606 | 164 | break; | |
| 607 | ✗ | default: | |
| 608 | ✗ | return netlink_error(ctx, loc, | |
| 609 | "Invalid bitwise operation %u", op); | ||
| 610 | } | ||
| 611 | |||
| 612 | 2145 | dreg = netlink_parse_register(nle, NFTNL_EXPR_BITWISE_DREG); | |
| 613 | 2145 | netlink_set_register(ctx, dreg, expr); | |
| 614 | } | ||
| 615 | |||
| 616 | 746 | static void netlink_parse_byteorder(struct netlink_parse_ctx *ctx, | |
| 617 | const struct location *loc, | ||
| 618 | const struct nftnl_expr *nle) | ||
| 619 | { | ||
| 620 | 746 | uint32_t opval = nftnl_expr_get_u32(nle, NFTNL_EXPR_BYTEORDER_OP); | |
| 621 | enum nft_registers sreg, dreg; | ||
| 622 | struct expr *expr, *arg; | ||
| 623 | enum ops op; | ||
| 624 | |||
| 625 | 746 | sreg = netlink_parse_register(nle, NFTNL_EXPR_BYTEORDER_SREG); | |
| 626 | 746 | arg = netlink_get_register(ctx, loc, sreg); | |
| 627 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 746 times.
|
746 | if (arg == NULL) |
| 628 | ✗ | return netlink_error(ctx, loc, | |
| 629 | "Byteorder expression has no left " | ||
| 630 | "hand side"); | ||
| 631 | |||
| 632 |
2/3✓ Branch 0 (8→9) taken 48 times.
✓ Branch 1 (8→10) taken 698 times.
✗ Branch 2 (8→11) not taken.
|
746 | switch (opval) { |
| 633 | 48 | case NFT_BYTEORDER_NTOH: | |
| 634 | 48 | op = OP_NTOH; | |
| 635 | 48 | break; | |
| 636 | 698 | case NFT_BYTEORDER_HTON: | |
| 637 | 698 | op = OP_HTON; | |
| 638 | 698 | break; | |
| 639 | ✗ | default: | |
| 640 | ✗ | expr_free(arg); | |
| 641 | ✗ | return netlink_error(ctx, loc, | |
| 642 | "Invalid byteorder operation %u", opval); | ||
| 643 | } | ||
| 644 | |||
| 645 | 746 | expr = unary_expr_alloc(loc, op, arg); | |
| 646 | 746 | expr->len = arg->len; | |
| 647 | |||
| 648 | 746 | dreg = netlink_parse_register(nle, NFTNL_EXPR_BYTEORDER_DREG); | |
| 649 | 746 | netlink_set_register(ctx, dreg, expr); | |
| 650 | } | ||
| 651 | |||
| 652 | 10318 | static void netlink_parse_payload_expr(struct netlink_parse_ctx *ctx, | |
| 653 | const struct location *loc, | ||
| 654 | const struct nftnl_expr *nle) | ||
| 655 | { | ||
| 656 | enum nft_registers dreg; | ||
| 657 | uint32_t base, offset, len; | ||
| 658 | struct expr *expr; | ||
| 659 | |||
| 660 | 10318 | base = nftnl_expr_get_u32(nle, NFTNL_EXPR_PAYLOAD_BASE) + 1; | |
| 661 | |||
| 662 |
2/2✓ Branch 0 (3→4) taken 20 times.
✓ Branch 1 (3→5) taken 10298 times.
|
10318 | if (base == NFT_PAYLOAD_TUN_HEADER + 1) |
| 663 | 20 | base = NFT_PAYLOAD_INNER_HEADER + 1; | |
| 664 | |||
| 665 | 10318 | offset = nftnl_expr_get_u32(nle, NFTNL_EXPR_PAYLOAD_OFFSET) * BITS_PER_BYTE; | |
| 666 | 10318 | len = nftnl_expr_get_u32(nle, NFTNL_EXPR_PAYLOAD_LEN) * BITS_PER_BYTE; | |
| 667 | |||
| 668 | 10318 | expr = payload_expr_alloc(loc, NULL, 0); | |
| 669 | 10318 | payload_init_raw(expr, base, offset, len); | |
| 670 | |||
| 671 | 10318 | dreg = netlink_parse_register(nle, NFTNL_EXPR_PAYLOAD_DREG); | |
| 672 | |||
| 673 |
2/2✓ Branch 0 (10→11) taken 470 times.
✓ Branch 1 (10→12) taken 9848 times.
|
10318 | if (ctx->inner) |
| 674 | 470 | ctx->inner_reg = dreg; | |
| 675 | |||
| 676 | 10318 | netlink_set_register(ctx, dreg, expr); | |
| 677 | 10318 | } | |
| 678 | |||
| 679 | 766 | static void netlink_parse_inner(struct netlink_parse_ctx *ctx, | |
| 680 | const struct location *loc, | ||
| 681 | const struct nftnl_expr *nle) | ||
| 682 | { | ||
| 683 | const struct proto_desc *inner_desc; | ||
| 684 | const struct nftnl_expr *inner_nle; | ||
| 685 | uint32_t hdrsize, flags, type; | ||
| 686 | struct expr *expr; | ||
| 687 | |||
| 688 | 766 | hdrsize = nftnl_expr_get_u32(nle, NFTNL_EXPR_INNER_HDRSIZE); | |
| 689 | 766 | type = nftnl_expr_get_u32(nle, NFTNL_EXPR_INNER_TYPE); | |
| 690 | 766 | flags = nftnl_expr_get_u32(nle, NFTNL_EXPR_INNER_FLAGS); | |
| 691 | |||
| 692 | 766 | inner_nle = nftnl_expr_get(nle, NFTNL_EXPR_INNER_EXPR, NULL); | |
| 693 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 766 times.
|
766 | if (!inner_nle) { |
| 694 | ✗ | netlink_error(ctx, loc, "Could not parse inner expression"); | |
| 695 | ✗ | return; | |
| 696 | } | ||
| 697 | |||
| 698 | 766 | inner_desc = proto_find_inner(type, hdrsize, flags); | |
| 699 | |||
| 700 | 766 | ctx->inner = true; | |
| 701 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 766 times.
|
766 | if (netlink_parse_expr(inner_nle, ctx) < 0) { |
| 702 | ✗ | ctx->inner = false; | |
| 703 | ✗ | return; | |
| 704 | } | ||
| 705 | 766 | ctx->inner = false; | |
| 706 | |||
| 707 | 766 | expr = netlink_get_register(ctx, loc, ctx->inner_reg); | |
| 708 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 766 times.
|
766 | assert(expr); |
| 709 | |||
| 710 |
2/2✓ Branch 0 (16→17) taken 470 times.
✓ Branch 1 (16→27) taken 296 times.
|
766 | if (expr->etype == EXPR_PAYLOAD && |
| 711 |
2/2✓ Branch 0 (17→18) taken 20 times.
✓ Branch 1 (17→27) taken 450 times.
|
470 | expr->payload.base == PROTO_BASE_INNER_HDR) { |
| 712 | const struct proto_hdr_template *tmpl; | ||
| 713 | unsigned int i; | ||
| 714 | |||
| 715 |
1/2✓ Branch 0 (26→19) taken 20 times.
✗ Branch 1 (26→27) not taken.
|
20 | for (i = 1; i < array_size(inner_desc->templates); i++) { |
| 716 | 20 | tmpl = &inner_desc->templates[i]; | |
| 717 | |||
| 718 |
1/2✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→21) taken 20 times.
|
20 | if (tmpl->len == 0) |
| 719 | ✗ | return; | |
| 720 | |||
| 721 |
1/2✓ Branch 0 (21→22) taken 20 times.
✗ Branch 1 (21→23) not taken.
|
20 | if (tmpl->offset != expr->payload.offset || |
| 722 |
1/2✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 20 times.
|
20 | tmpl->len != expr->len) |
| 723 | ✗ | continue; | |
| 724 | |||
| 725 | 20 | expr->payload.desc = inner_desc; | |
| 726 | 20 | expr->payload.tmpl = tmpl; | |
| 727 | 20 | break; | |
| 728 | } | ||
| 729 | } | ||
| 730 | |||
| 731 |
2/3✓ Branch 0 (27→28) taken 470 times.
✓ Branch 1 (27→29) taken 296 times.
✗ Branch 2 (27→30) not taken.
|
766 | switch (expr->etype) { |
| 732 | 470 | case EXPR_PAYLOAD: | |
| 733 | 470 | expr->payload.inner_desc = inner_desc; | |
| 734 | 470 | break; | |
| 735 | 296 | case EXPR_META: | |
| 736 | 296 | expr->meta.inner_desc = inner_desc; | |
| 737 | 296 | break; | |
| 738 | ✗ | default: | |
| 739 | ✗ | netlink_error(ctx, loc, "Unsupported inner expression type %s", | |
| 740 | ✗ | expr_ops(expr)->name); | |
| 741 | ✗ | expr_free(expr); | |
| 742 | ✗ | return; | |
| 743 | } | ||
| 744 | |||
| 745 | 766 | netlink_set_register(ctx, ctx->inner_reg, expr); | |
| 746 | } | ||
| 747 | |||
| 748 | 398 | static void netlink_parse_payload_stmt(struct netlink_parse_ctx *ctx, | |
| 749 | const struct location *loc, | ||
| 750 | const struct nftnl_expr *nle) | ||
| 751 | { | ||
| 752 | enum nft_registers sreg; | ||
| 753 | uint32_t base, offset, len; | ||
| 754 | struct expr *expr, *val; | ||
| 755 | struct stmt *stmt; | ||
| 756 | |||
| 757 | 398 | base = nftnl_expr_get_u32(nle, NFTNL_EXPR_PAYLOAD_BASE) + 1; | |
| 758 | 398 | offset = nftnl_expr_get_u32(nle, NFTNL_EXPR_PAYLOAD_OFFSET) * BITS_PER_BYTE; | |
| 759 | 398 | len = nftnl_expr_get_u32(nle, NFTNL_EXPR_PAYLOAD_LEN) * BITS_PER_BYTE; | |
| 760 | |||
| 761 | 398 | sreg = netlink_parse_register(nle, NFTNL_EXPR_PAYLOAD_SREG); | |
| 762 | 398 | val = netlink_get_register(ctx, loc, sreg); | |
| 763 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 398 times.
|
398 | if (val == NULL) |
| 764 | ✗ | return netlink_error(ctx, loc, | |
| 765 | "payload statement has no expression"); | ||
| 766 | |||
| 767 | 398 | expr = payload_expr_alloc(loc, NULL, 0); | |
| 768 | 398 | payload_init_raw(expr, base, offset, len); | |
| 769 | |||
| 770 | 398 | stmt = payload_stmt_alloc(loc, expr, val); | |
| 771 | 398 | rule_stmt_append(ctx->rule, stmt); | |
| 772 | } | ||
| 773 | |||
| 774 | 10716 | static void netlink_parse_payload(struct netlink_parse_ctx *ctx, | |
| 775 | const struct location *loc, | ||
| 776 | const struct nftnl_expr *nle) | ||
| 777 | { | ||
| 778 |
2/2✓ Branch 0 (3→4) taken 10318 times.
✓ Branch 1 (3→5) taken 398 times.
|
10716 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_PAYLOAD_DREG)) |
| 779 | 10318 | netlink_parse_payload_expr(ctx, loc, nle); | |
| 780 | else | ||
| 781 | 398 | netlink_parse_payload_stmt(ctx, loc, nle); | |
| 782 | 10716 | } | |
| 783 | |||
| 784 | 1261 | static void netlink_parse_exthdr(struct netlink_parse_ctx *ctx, | |
| 785 | const struct location *loc, | ||
| 786 | const struct nftnl_expr *nle) | ||
| 787 | { | ||
| 788 | uint32_t offset, len, flags; | ||
| 789 | enum nft_registers dreg; | ||
| 790 | enum nft_exthdr_op op; | ||
| 791 | uint8_t type; | ||
| 792 | struct expr *expr; | ||
| 793 | |||
| 794 | 1261 | type = nftnl_expr_get_u8(nle, NFTNL_EXPR_EXTHDR_TYPE); | |
| 795 | 1261 | offset = nftnl_expr_get_u32(nle, NFTNL_EXPR_EXTHDR_OFFSET) * BITS_PER_BYTE; | |
| 796 | 1261 | len = nftnl_expr_get_u32(nle, NFTNL_EXPR_EXTHDR_LEN) * BITS_PER_BYTE; | |
| 797 | 1261 | op = nftnl_expr_get_u32(nle, NFTNL_EXPR_EXTHDR_OP); | |
| 798 | 1261 | flags = nftnl_expr_get_u32(nle, NFTNL_EXPR_EXTHDR_FLAGS); | |
| 799 | |||
| 800 | 1261 | expr = exthdr_expr_alloc(loc, NULL, 0); | |
| 801 | 1261 | exthdr_init_raw(expr, type, offset, len, op, flags); | |
| 802 | |||
| 803 |
2/2✓ Branch 0 (10→11) taken 1230 times.
✓ Branch 1 (10→13) taken 31 times.
|
1261 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_EXTHDR_DREG)) { |
| 804 | 1230 | dreg = netlink_parse_register(nle, NFTNL_EXPR_EXTHDR_DREG); | |
| 805 | 1230 | netlink_set_register(ctx, dreg, expr); | |
| 806 |
2/2✓ Branch 0 (14→15) taken 11 times.
✓ Branch 1 (14→24) taken 20 times.
|
31 | } else if (nftnl_expr_is_set(nle, NFTNL_EXPR_EXTHDR_SREG)) { |
| 807 | enum nft_registers sreg; | ||
| 808 | struct stmt *stmt; | ||
| 809 | struct expr *val; | ||
| 810 | |||
| 811 | 11 | sreg = netlink_parse_register(nle, NFTNL_EXPR_EXTHDR_SREG); | |
| 812 | 11 | val = netlink_get_register(ctx, loc, sreg); | |
| 813 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→21) taken 11 times.
|
11 | if (val == NULL) { |
| 814 | ✗ | expr_free(expr); | |
| 815 | ✗ | return netlink_error(ctx, loc, | |
| 816 | "exthdr statement has no expression"); | ||
| 817 | } | ||
| 818 | |||
| 819 | 11 | expr_set_type(val, expr->dtype, expr->byteorder); | |
| 820 | |||
| 821 | 11 | stmt = exthdr_stmt_alloc(loc, expr, val); | |
| 822 | 11 | rule_stmt_append(ctx->rule, stmt); | |
| 823 | } else { | ||
| 824 | 20 | struct stmt *stmt = optstrip_stmt_alloc(loc, expr); | |
| 825 | |||
| 826 | 20 | rule_stmt_append(ctx->rule, stmt); | |
| 827 | } | ||
| 828 | } | ||
| 829 | |||
| 830 | 38 | static void netlink_parse_hash(struct netlink_parse_ctx *ctx, | |
| 831 | const struct location *loc, | ||
| 832 | const struct nftnl_expr *nle) | ||
| 833 | { | ||
| 834 | enum nft_registers sreg, dreg; | ||
| 835 | struct expr *expr, *hexpr; | ||
| 836 | uint32_t mod, seed, len, offset; | ||
| 837 | enum nft_hash_types type; | ||
| 838 | bool seed_set; | ||
| 839 | |||
| 840 | 38 | type = nftnl_expr_get_u32(nle, NFTNL_EXPR_HASH_TYPE); | |
| 841 | 38 | offset = nftnl_expr_get_u32(nle, NFTNL_EXPR_HASH_OFFSET); | |
| 842 | 38 | seed_set = nftnl_expr_is_set(nle, NFTNL_EXPR_HASH_SEED); | |
| 843 | 38 | seed = nftnl_expr_get_u32(nle, NFTNL_EXPR_HASH_SEED); | |
| 844 | 38 | mod = nftnl_expr_get_u32(nle, NFTNL_EXPR_HASH_MODULUS); | |
| 845 | |||
| 846 | 38 | expr = hash_expr_alloc(loc, mod, seed_set, seed, offset, type); | |
| 847 | |||
| 848 |
2/2✓ Branch 0 (8→9) taken 22 times.
✓ Branch 1 (8→21) taken 16 times.
|
38 | if (type != NFT_HASH_SYM) { |
| 849 | 22 | sreg = netlink_parse_register(nle, NFTNL_EXPR_HASH_SREG); | |
| 850 | 22 | hexpr = netlink_get_register(ctx, loc, sreg); | |
| 851 | |||
| 852 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→14) taken 22 times.
|
22 | if (hexpr == NULL) { |
| 853 | ✗ | netlink_error(ctx, loc, | |
| 854 | "hash statement has no expression"); | ||
| 855 | ✗ | goto out_err; | |
| 856 | } | ||
| 857 | 22 | len = nftnl_expr_get_u32(nle, | |
| 858 | NFTNL_EXPR_HASH_LEN) * BITS_PER_BYTE; | ||
| 859 |
2/2✓ Branch 0 (15→16) taken 20 times.
✓ Branch 1 (15→20) taken 2 times.
|
22 | if (hexpr->len < len) { |
| 860 | 20 | expr_free(hexpr); | |
| 861 | 20 | hexpr = netlink_parse_concat_expr(ctx, loc, sreg, len); | |
| 862 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 20 times.
|
20 | if (hexpr == NULL) |
| 863 | ✗ | goto out_err; | |
| 864 | } | ||
| 865 | 22 | expr->hash.expr = hexpr; | |
| 866 | } | ||
| 867 | |||
| 868 | 38 | dreg = netlink_parse_register(nle, NFTNL_EXPR_HASH_DREG); | |
| 869 | 38 | netlink_set_register(ctx, dreg, expr); | |
| 870 | 38 | return; | |
| 871 | ✗ | out_err: | |
| 872 | ✗ | expr_free(expr); | |
| 873 | } | ||
| 874 | |||
| 875 | 53 | static void netlink_parse_fib(struct netlink_parse_ctx *ctx, | |
| 876 | const struct location *loc, | ||
| 877 | const struct nftnl_expr *nle) | ||
| 878 | { | ||
| 879 | enum nft_registers dreg; | ||
| 880 | struct expr *expr; | ||
| 881 | uint32_t flags, result; | ||
| 882 | |||
| 883 | 53 | flags = nftnl_expr_get_u32(nle, NFTNL_EXPR_FIB_FLAGS); | |
| 884 | 53 | result = nftnl_expr_get_u32(nle, NFTNL_EXPR_FIB_RESULT); | |
| 885 | |||
| 886 | 53 | expr = fib_expr_alloc(loc, flags, result); | |
| 887 | |||
| 888 | 53 | dreg = netlink_parse_register(nle, NFTNL_EXPR_FIB_DREG); | |
| 889 | 53 | netlink_set_register(ctx, dreg, expr); | |
| 890 | 53 | } | |
| 891 | |||
| 892 | 50789 | static void netlink_parse_meta_expr(struct netlink_parse_ctx *ctx, | |
| 893 | const struct location *loc, | ||
| 894 | const struct nftnl_expr *nle) | ||
| 895 | { | ||
| 896 | enum nft_registers dreg; | ||
| 897 | uint32_t key; | ||
| 898 | struct expr *expr; | ||
| 899 | |||
| 900 | 50789 | key = nftnl_expr_get_u32(nle, NFTNL_EXPR_META_KEY); | |
| 901 | 50789 | expr = meta_expr_alloc(loc, key); | |
| 902 | |||
| 903 | 50789 | dreg = netlink_parse_register(nle, NFTNL_EXPR_META_DREG); | |
| 904 |
2/2✓ Branch 0 (5→6) taken 296 times.
✓ Branch 1 (5→7) taken 50493 times.
|
50789 | if (ctx->inner) |
| 905 | 296 | ctx->inner_reg = dreg; | |
| 906 | |||
| 907 | 50789 | netlink_set_register(ctx, dreg, expr); | |
| 908 | 50789 | } | |
| 909 | |||
| 910 | 30 | static void netlink_parse_socket(struct netlink_parse_ctx *ctx, | |
| 911 | const struct location *loc, | ||
| 912 | const struct nftnl_expr *nle) | ||
| 913 | { | ||
| 914 | enum nft_registers dreg; | ||
| 915 | uint32_t key, level; | ||
| 916 | struct expr * expr; | ||
| 917 | |||
| 918 | 30 | key = nftnl_expr_get_u32(nle, NFTNL_EXPR_SOCKET_KEY); | |
| 919 | 30 | level = nftnl_expr_get_u32(nle, NFTNL_EXPR_SOCKET_LEVEL); | |
| 920 | 30 | expr = socket_expr_alloc(loc, key, level); | |
| 921 | |||
| 922 | 30 | dreg = netlink_parse_register(nle, NFTNL_EXPR_SOCKET_DREG); | |
| 923 | 30 | netlink_set_register(ctx, dreg, expr); | |
| 924 | 30 | } | |
| 925 | |||
| 926 | 57 | static void netlink_parse_osf(struct netlink_parse_ctx *ctx, | |
| 927 | const struct location *loc, | ||
| 928 | const struct nftnl_expr *nle) | ||
| 929 | { | ||
| 930 | enum nft_registers dreg; | ||
| 931 | struct expr *expr; | ||
| 932 | uint32_t flags; | ||
| 933 | uint8_t ttl; | ||
| 934 | |||
| 935 | 57 | ttl = nftnl_expr_get_u8(nle, NFTNL_EXPR_OSF_TTL); | |
| 936 | 57 | flags = nftnl_expr_get_u32(nle, NFTNL_EXPR_OSF_FLAGS); | |
| 937 | 57 | expr = osf_expr_alloc(loc, ttl, flags); | |
| 938 | |||
| 939 | 57 | dreg = netlink_parse_register(nle, NFTNL_EXPR_OSF_DREG); | |
| 940 | 57 | netlink_set_register(ctx, dreg, expr); | |
| 941 | 57 | } | |
| 942 | |||
| 943 | 7 | static void netlink_parse_tunnel(struct netlink_parse_ctx *ctx, | |
| 944 | const struct location *loc, | ||
| 945 | const struct nftnl_expr *nle) | ||
| 946 | { | ||
| 947 | enum nft_registers dreg; | ||
| 948 | struct expr * expr; | ||
| 949 | uint32_t key; | ||
| 950 | |||
| 951 | 7 | key = nftnl_expr_get_u32(nle, NFTNL_EXPR_TUNNEL_KEY); | |
| 952 | 7 | expr = tunnel_expr_alloc(loc, key); | |
| 953 | |||
| 954 | 7 | dreg = netlink_parse_register(nle, NFTNL_EXPR_TUNNEL_DREG); | |
| 955 | 7 | netlink_set_register(ctx, dreg, expr); | |
| 956 | 7 | } | |
| 957 | |||
| 958 | 317 | static void netlink_parse_meta_stmt(struct netlink_parse_ctx *ctx, | |
| 959 | const struct location *loc, | ||
| 960 | const struct nftnl_expr *nle) | ||
| 961 | { | ||
| 962 | enum nft_registers sreg; | ||
| 963 | uint32_t key; | ||
| 964 | struct stmt *stmt; | ||
| 965 | struct expr *expr; | ||
| 966 | |||
| 967 | 317 | sreg = netlink_parse_register(nle, NFTNL_EXPR_META_SREG); | |
| 968 | 317 | expr = netlink_get_register(ctx, loc, sreg); | |
| 969 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 317 times.
|
317 | if (expr == NULL) |
| 970 | ✗ | return netlink_error(ctx, loc, | |
| 971 | "meta statement has no expression"); | ||
| 972 | |||
| 973 | 317 | key = nftnl_expr_get_u32(nle, NFTNL_EXPR_META_KEY); | |
| 974 | 317 | stmt = meta_stmt_alloc(loc, key, expr); | |
| 975 | |||
| 976 |
1/2✓ Branch 0 (9→10) taken 317 times.
✗ Branch 1 (9→11) not taken.
|
317 | if (stmt->meta.tmpl) |
| 977 | 317 | expr_set_type(expr, stmt->meta.tmpl->dtype, stmt->meta.tmpl->byteorder); | |
| 978 | |||
| 979 | 317 | ctx->stmt = stmt; | |
| 980 | } | ||
| 981 | |||
| 982 | 51106 | static void netlink_parse_meta(struct netlink_parse_ctx *ctx, | |
| 983 | const struct location *loc, | ||
| 984 | const struct nftnl_expr *nle) | ||
| 985 | { | ||
| 986 |
2/2✓ Branch 0 (3→4) taken 50789 times.
✓ Branch 1 (3→5) taken 317 times.
|
51106 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_META_DREG)) |
| 987 | 50789 | netlink_parse_meta_expr(ctx, loc, nle); | |
| 988 | else | ||
| 989 | 317 | netlink_parse_meta_stmt(ctx, loc, nle); | |
| 990 | 51106 | } | |
| 991 | |||
| 992 | 35 | static void netlink_parse_rt(struct netlink_parse_ctx *ctx, | |
| 993 | const struct location *loc, | ||
| 994 | const struct nftnl_expr *nle) | ||
| 995 | { | ||
| 996 | enum nft_registers dreg; | ||
| 997 | uint32_t key; | ||
| 998 | struct expr *expr; | ||
| 999 | |||
| 1000 | 35 | key = nftnl_expr_get_u32(nle, NFTNL_EXPR_RT_KEY); | |
| 1001 | 35 | expr = rt_expr_alloc(loc, key, false); | |
| 1002 | |||
| 1003 | 35 | dreg = netlink_parse_register(nle, NFTNL_EXPR_RT_DREG); | |
| 1004 | 35 | netlink_set_register(ctx, dreg, expr); | |
| 1005 | 35 | } | |
| 1006 | |||
| 1007 | 39 | static void netlink_parse_numgen(struct netlink_parse_ctx *ctx, | |
| 1008 | const struct location *loc, | ||
| 1009 | const struct nftnl_expr *nle) | ||
| 1010 | { | ||
| 1011 | uint32_t type, until, offset; | ||
| 1012 | enum nft_registers dreg; | ||
| 1013 | struct expr *expr; | ||
| 1014 | |||
| 1015 | 39 | type = nftnl_expr_get_u32(nle, NFTNL_EXPR_NG_TYPE); | |
| 1016 | 39 | until = nftnl_expr_get_u32(nle, NFTNL_EXPR_NG_MODULUS); | |
| 1017 | 39 | offset = nftnl_expr_get_u32(nle, NFTNL_EXPR_NG_OFFSET); | |
| 1018 | |||
| 1019 | 39 | expr = numgen_expr_alloc(loc, type, until, offset); | |
| 1020 | 39 | dreg = netlink_parse_register(nle, NFTNL_EXPR_NG_DREG); | |
| 1021 | 39 | netlink_set_register(ctx, dreg, expr); | |
| 1022 | 39 | } | |
| 1023 | |||
| 1024 | 6 | static void netlink_parse_notrack(struct netlink_parse_ctx *ctx, | |
| 1025 | const struct location *loc, | ||
| 1026 | const struct nftnl_expr *nle) | ||
| 1027 | { | ||
| 1028 | 6 | ctx->stmt = notrack_stmt_alloc(loc); | |
| 1029 | 6 | } | |
| 1030 | |||
| 1031 | 6 | static void netlink_parse_flow_offload(struct netlink_parse_ctx *ctx, | |
| 1032 | const struct location *loc, | ||
| 1033 | const struct nftnl_expr *nle) | ||
| 1034 | { | ||
| 1035 | const char *table_name; | ||
| 1036 | |||
| 1037 | 6 | table_name = xstrdup(nftnl_expr_get_str(nle, NFTNL_EXPR_FLOW_TABLE_NAME)); | |
| 1038 | 6 | ctx->stmt = flow_offload_stmt_alloc(loc, table_name); | |
| 1039 | 6 | } | |
| 1040 | |||
| 1041 | 211 | static void netlink_parse_ct_stmt(struct netlink_parse_ctx *ctx, | |
| 1042 | const struct location *loc, | ||
| 1043 | const struct nftnl_expr *nle) | ||
| 1044 | { | ||
| 1045 | enum nft_registers sreg; | ||
| 1046 | uint32_t key; | ||
| 1047 | struct stmt *stmt; | ||
| 1048 | struct expr *expr; | ||
| 1049 | 211 | int8_t dir = -1; | |
| 1050 | |||
| 1051 | 211 | sreg = netlink_parse_register(nle, NFTNL_EXPR_CT_SREG); | |
| 1052 | 211 | expr = netlink_get_register(ctx, loc, sreg); | |
| 1053 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 211 times.
|
211 | if (expr == NULL) |
| 1054 | ✗ | return netlink_error(ctx, loc, | |
| 1055 | "ct statement has no expression"); | ||
| 1056 | |||
| 1057 |
2/2✓ Branch 0 (8→9) taken 12 times.
✓ Branch 1 (8→11) taken 199 times.
|
211 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_CT_DIR)) |
| 1058 | 12 | dir = nftnl_expr_get_u8(nle, NFTNL_EXPR_CT_DIR); | |
| 1059 | |||
| 1060 | 211 | key = nftnl_expr_get_u32(nle, NFTNL_EXPR_CT_KEY); | |
| 1061 | 211 | stmt = ct_stmt_alloc(loc, key, dir, expr); | |
| 1062 | 211 | expr_set_type(expr, stmt->ct.tmpl->dtype, stmt->ct.tmpl->byteorder); | |
| 1063 | |||
| 1064 | 211 | ctx->stmt = stmt; | |
| 1065 | } | ||
| 1066 | |||
| 1067 | 697 | static void netlink_parse_ct_expr(struct netlink_parse_ctx *ctx, | |
| 1068 | const struct location *loc, | ||
| 1069 | const struct nftnl_expr *nle) | ||
| 1070 | { | ||
| 1071 | 697 | struct expr *expr = NULL; | |
| 1072 | enum nft_registers dreg; | ||
| 1073 | 697 | int8_t dir = -1; | |
| 1074 | uint32_t key; | ||
| 1075 | |||
| 1076 |
2/2✓ Branch 0 (3→4) taken 66 times.
✓ Branch 1 (3→6) taken 631 times.
|
697 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_CT_DIR)) |
| 1077 | 66 | dir = nftnl_expr_get_u8(nle, NFTNL_EXPR_CT_DIR); | |
| 1078 | |||
| 1079 | 697 | key = nftnl_expr_get_u32(nle, NFTNL_EXPR_CT_KEY); | |
| 1080 | 697 | expr = ct_expr_alloc(loc, key, dir); | |
| 1081 | |||
| 1082 | 697 | dreg = netlink_parse_register(nle, NFTNL_EXPR_CT_DREG); | |
| 1083 | 697 | netlink_set_register(ctx, dreg, expr); | |
| 1084 | 697 | } | |
| 1085 | |||
| 1086 | 908 | static void netlink_parse_ct(struct netlink_parse_ctx *ctx, | |
| 1087 | const struct location *loc, | ||
| 1088 | const struct nftnl_expr *nle) | ||
| 1089 | { | ||
| 1090 |
2/2✓ Branch 0 (3→4) taken 697 times.
✓ Branch 1 (3→5) taken 211 times.
|
908 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_CT_DREG)) |
| 1091 | 697 | netlink_parse_ct_expr(ctx, loc, nle); | |
| 1092 | else | ||
| 1093 | 211 | netlink_parse_ct_stmt(ctx, loc, nle); | |
| 1094 | 908 | } | |
| 1095 | |||
| 1096 | 37 | static void netlink_parse_connlimit(struct netlink_parse_ctx *ctx, | |
| 1097 | const struct location *loc, | ||
| 1098 | const struct nftnl_expr *nle) | ||
| 1099 | { | ||
| 1100 | struct stmt *stmt; | ||
| 1101 | |||
| 1102 | 37 | stmt = connlimit_stmt_alloc(loc); | |
| 1103 | 37 | stmt->connlimit.count = | |
| 1104 | 37 | nftnl_expr_get_u32(nle, NFTNL_EXPR_CONNLIMIT_COUNT); | |
| 1105 | 37 | stmt->connlimit.flags = | |
| 1106 | 37 | nftnl_expr_get_u32(nle, NFTNL_EXPR_CONNLIMIT_FLAGS); | |
| 1107 | |||
| 1108 | 37 | ctx->stmt = stmt; | |
| 1109 | 37 | } | |
| 1110 | |||
| 1111 | 185909 | static void netlink_parse_counter(struct netlink_parse_ctx *ctx, | |
| 1112 | const struct location *loc, | ||
| 1113 | const struct nftnl_expr *nle) | ||
| 1114 | { | ||
| 1115 | struct stmt *stmt; | ||
| 1116 | |||
| 1117 | 185909 | stmt = counter_stmt_alloc(loc); | |
| 1118 | 185909 | stmt->counter.packets = nftnl_expr_get_u64(nle, NFTNL_EXPR_CTR_PACKETS); | |
| 1119 | 185909 | stmt->counter.bytes = nftnl_expr_get_u64(nle, NFTNL_EXPR_CTR_BYTES); | |
| 1120 | |||
| 1121 | 185909 | ctx->stmt = stmt; | |
| 1122 | 185909 | } | |
| 1123 | |||
| 1124 | 39 | static void netlink_parse_last(struct netlink_parse_ctx *ctx, | |
| 1125 | const struct location *loc, | ||
| 1126 | const struct nftnl_expr *nle) | ||
| 1127 | { | ||
| 1128 | struct stmt *stmt; | ||
| 1129 | |||
| 1130 | 39 | stmt = last_stmt_alloc(loc); | |
| 1131 | 39 | stmt->last.used = nftnl_expr_get_u64(nle, NFTNL_EXPR_LAST_MSECS); | |
| 1132 | 39 | stmt->last.set = nftnl_expr_get_u32(nle, NFTNL_EXPR_LAST_SET); | |
| 1133 | |||
| 1134 | 39 | ctx->stmt = stmt; | |
| 1135 | 39 | } | |
| 1136 | |||
| 1137 | 210 | static void netlink_parse_log(struct netlink_parse_ctx *ctx, | |
| 1138 | const struct location *loc, | ||
| 1139 | const struct nftnl_expr *nle) | ||
| 1140 | { | ||
| 1141 | struct stmt *stmt; | ||
| 1142 | const char *prefix; | ||
| 1143 | |||
| 1144 | 210 | stmt = log_stmt_alloc(loc); | |
| 1145 | 210 | prefix = nftnl_expr_get_str(nle, NFTNL_EXPR_LOG_PREFIX); | |
| 1146 |
2/2✓ Branch 0 (5→6) taken 34 times.
✓ Branch 1 (5→8) taken 176 times.
|
210 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_LOG_PREFIX)) { |
| 1147 | 34 | stmt->log.prefix = xstrdup(prefix); | |
| 1148 | 34 | stmt->log.flags |= STMT_LOG_PREFIX; | |
| 1149 | } | ||
| 1150 |
2/2✓ Branch 0 (9→10) taken 40 times.
✓ Branch 1 (9→12) taken 170 times.
|
210 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_LOG_GROUP)) { |
| 1151 | 40 | stmt->log.group = nftnl_expr_get_u16(nle, NFTNL_EXPR_LOG_GROUP); | |
| 1152 | 40 | stmt->log.flags |= STMT_LOG_GROUP; | |
| 1153 | } | ||
| 1154 |
2/2✓ Branch 0 (13→14) taken 20 times.
✓ Branch 1 (13→16) taken 190 times.
|
210 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_LOG_SNAPLEN)) { |
| 1155 | 20 | stmt->log.snaplen = | |
| 1156 | 20 | nftnl_expr_get_u32(nle, NFTNL_EXPR_LOG_SNAPLEN); | |
| 1157 | 20 | stmt->log.flags |= STMT_LOG_SNAPLEN; | |
| 1158 | } | ||
| 1159 |
2/2✓ Branch 0 (17→18) taken 10 times.
✓ Branch 1 (17→20) taken 200 times.
|
210 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_LOG_QTHRESHOLD)) { |
| 1160 | 10 | stmt->log.qthreshold = | |
| 1161 | 10 | nftnl_expr_get_u16(nle, NFTNL_EXPR_LOG_QTHRESHOLD); | |
| 1162 | 10 | stmt->log.flags |= STMT_LOG_QTHRESHOLD; | |
| 1163 | } | ||
| 1164 |
2/2✓ Branch 0 (21→22) taken 170 times.
✓ Branch 1 (21→24) taken 40 times.
|
210 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_LOG_LEVEL)) { |
| 1165 | 170 | stmt->log.level = | |
| 1166 | 170 | nftnl_expr_get_u32(nle, NFTNL_EXPR_LOG_LEVEL); | |
| 1167 | 170 | stmt->log.flags |= STMT_LOG_LEVEL; | |
| 1168 | } | ||
| 1169 |
2/2✓ Branch 0 (25→26) taken 55 times.
✓ Branch 1 (25→28) taken 155 times.
|
210 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_LOG_FLAGS)) { |
| 1170 | 55 | stmt->log.logflags = | |
| 1171 | 55 | nftnl_expr_get_u32(nle, NFTNL_EXPR_LOG_FLAGS); | |
| 1172 | } | ||
| 1173 | |||
| 1174 | 210 | ctx->stmt = stmt; | |
| 1175 | 210 | } | |
| 1176 | |||
| 1177 | 575 | static void netlink_parse_limit(struct netlink_parse_ctx *ctx, | |
| 1178 | const struct location *loc, | ||
| 1179 | const struct nftnl_expr *nle) | ||
| 1180 | { | ||
| 1181 | struct stmt *stmt; | ||
| 1182 | |||
| 1183 | 575 | stmt = limit_stmt_alloc(loc); | |
| 1184 | 575 | stmt->limit.rate = nftnl_expr_get_u64(nle, NFTNL_EXPR_LIMIT_RATE); | |
| 1185 | 575 | stmt->limit.unit = nftnl_expr_get_u64(nle, NFTNL_EXPR_LIMIT_UNIT); | |
| 1186 | 575 | stmt->limit.type = nftnl_expr_get_u32(nle, NFTNL_EXPR_LIMIT_TYPE); | |
| 1187 | 575 | stmt->limit.burst = nftnl_expr_get_u32(nle, NFTNL_EXPR_LIMIT_BURST); | |
| 1188 | 575 | stmt->limit.flags = nftnl_expr_get_u32(nle, NFTNL_EXPR_LIMIT_FLAGS); | |
| 1189 | |||
| 1190 | 575 | ctx->stmt = stmt; | |
| 1191 | 575 | } | |
| 1192 | |||
| 1193 | 286 | static void netlink_parse_quota(struct netlink_parse_ctx *ctx, | |
| 1194 | const struct location *loc, | ||
| 1195 | const struct nftnl_expr *nle) | ||
| 1196 | { | ||
| 1197 | struct stmt *stmt; | ||
| 1198 | |||
| 1199 | 286 | stmt = quota_stmt_alloc(loc); | |
| 1200 | 286 | stmt->quota.bytes = nftnl_expr_get_u64(nle, NFTNL_EXPR_QUOTA_BYTES); | |
| 1201 | 286 | stmt->quota.used = | |
| 1202 | 286 | nftnl_expr_get_u64(nle, NFTNL_EXPR_QUOTA_CONSUMED); | |
| 1203 | 286 | stmt->quota.flags = nftnl_expr_get_u32(nle, NFTNL_EXPR_QUOTA_FLAGS); | |
| 1204 | |||
| 1205 | 286 | ctx->stmt = stmt; | |
| 1206 | 286 | } | |
| 1207 | |||
| 1208 | 226 | static void netlink_parse_reject(struct netlink_parse_ctx *ctx, | |
| 1209 | const struct location *loc, | ||
| 1210 | const struct nftnl_expr *expr) | ||
| 1211 | { | ||
| 1212 | struct stmt *stmt; | ||
| 1213 | uint8_t icmp_code; | ||
| 1214 | |||
| 1215 | 226 | stmt = reject_stmt_alloc(loc); | |
| 1216 | 226 | stmt->reject.type = nftnl_expr_get_u32(expr, NFTNL_EXPR_REJECT_TYPE); | |
| 1217 | 226 | icmp_code = nftnl_expr_get_u8(expr, NFTNL_EXPR_REJECT_CODE); | |
| 1218 | 226 | stmt->reject.icmp_code = icmp_code; | |
| 1219 | 226 | stmt->reject.expr = constant_expr_alloc(loc, &integer_type, | |
| 1220 | BYTEORDER_HOST_ENDIAN, 8, | ||
| 1221 | &icmp_code); | ||
| 1222 | 226 | ctx->stmt = stmt; | |
| 1223 | 226 | } | |
| 1224 | |||
| 1225 | 229 | static bool is_nat_addr_map(const struct expr *addr, uint8_t family, | |
| 1226 | struct stmt *stmt) | ||
| 1227 | { | ||
| 1228 | const struct expr *mappings, *data; | ||
| 1229 | const struct set *set; | ||
| 1230 | |||
| 1231 |
2/2✓ Branch 0 (2→3) taken 227 times.
✓ Branch 1 (2→5) taken 2 times.
|
229 | if (!addr || |
| 1232 |
2/2✓ Branch 0 (4→5) taken 98 times.
✓ Branch 1 (4→6) taken 129 times.
|
227 | expr_ops(addr)->type != EXPR_MAP) |
| 1233 | 100 | return false; | |
| 1234 | |||
| 1235 | 129 | mappings = addr->right; | |
| 1236 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 129 times.
|
129 | if (expr_ops(mappings)->type != EXPR_SET_REF) |
| 1237 | ✗ | return false; | |
| 1238 | |||
| 1239 | 129 | set = mappings->set; | |
| 1240 | 129 | data = set->data; | |
| 1241 | |||
| 1242 |
2/2✓ Branch 0 (9→10) taken 88 times.
✓ Branch 1 (9→11) taken 41 times.
|
129 | if (!(data->flags & EXPR_F_INTERVAL)) |
| 1243 | 88 | return false; | |
| 1244 | |||
| 1245 | 41 | stmt->nat.family = family; | |
| 1246 | |||
| 1247 | /* if we're dealing with an address:address map, | ||
| 1248 | * the length will be bit_sizeof(addr) + 32 (one register). | ||
| 1249 | */ | ||
| 1250 |
2/3✓ Branch 0 (11→12) taken 39 times.
✓ Branch 1 (11→17) taken 2 times.
✗ Branch 2 (11→21) not taken.
|
41 | switch (family) { |
| 1251 | 39 | case NFPROTO_IPV4: | |
| 1252 |
2/2✓ Branch 0 (12→13) taken 21 times.
✓ Branch 1 (12→14) taken 18 times.
|
39 | if (data->len == 32 + 32) { |
| 1253 | 21 | stmt->nat.type_flags |= STMT_NAT_F_INTERVAL; | |
| 1254 | 21 | return true; | |
| 1255 |
1/2✓ Branch 0 (14→15) taken 18 times.
✗ Branch 1 (14→16) not taken.
|
18 | } else if (data->len == 32 + 32 + 32 + 32) { |
| 1256 | 18 | stmt->nat.type_flags |= STMT_NAT_F_INTERVAL | | |
| 1257 | STMT_NAT_F_CONCAT; | ||
| 1258 | 18 | return true; | |
| 1259 | } | ||
| 1260 | ✗ | break; | |
| 1261 | 2 | case NFPROTO_IPV6: | |
| 1262 |
1/2✓ Branch 0 (17→18) taken 2 times.
✗ Branch 1 (17→19) not taken.
|
2 | if (data->len == 128 + 128) { |
| 1263 | 2 | stmt->nat.type_flags |= STMT_NAT_F_INTERVAL; | |
| 1264 | 2 | return true; | |
| 1265 | ✗ | } else if (data->len == 128 + 32 + 128 + 32) { | |
| 1266 | ✗ | stmt->nat.type_flags |= STMT_NAT_F_INTERVAL | | |
| 1267 | STMT_NAT_F_CONCAT; | ||
| 1268 | ✗ | return true; | |
| 1269 | } | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | ✗ | return false; | |
| 1273 | } | ||
| 1274 | |||
| 1275 | 188 | static bool is_nat_proto_map(const struct expr *addr, uint8_t family) | |
| 1276 | { | ||
| 1277 | const struct expr *mappings, *data; | ||
| 1278 | const struct set *set; | ||
| 1279 | |||
| 1280 |
2/2✓ Branch 0 (2→3) taken 186 times.
✓ Branch 1 (2→5) taken 2 times.
|
188 | if (!addr || |
| 1281 |
2/2✓ Branch 0 (4→5) taken 98 times.
✓ Branch 1 (4→6) taken 88 times.
|
186 | expr_ops(addr)->type != EXPR_MAP) |
| 1282 | 100 | return false; | |
| 1283 | |||
| 1284 | 88 | mappings = addr->right; | |
| 1285 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 88 times.
|
88 | if (expr_ops(mappings)->type != EXPR_SET_REF) |
| 1286 | ✗ | return false; | |
| 1287 | |||
| 1288 | 88 | set = mappings->set; | |
| 1289 | 88 | data = set->data; | |
| 1290 | |||
| 1291 | /* if we're dealing with an address:inet_service map, | ||
| 1292 | * the length will be bit_sizeof(addr) + 32 (one register). | ||
| 1293 | */ | ||
| 1294 |
2/3✓ Branch 0 (9→10) taken 68 times.
✓ Branch 1 (9→11) taken 20 times.
✗ Branch 2 (9→12) not taken.
|
88 | switch (family) { |
| 1295 | 68 | case NFPROTO_IPV4: | |
| 1296 | 68 | return data->len == 32 + 32; | |
| 1297 | 20 | case NFPROTO_IPV6: | |
| 1298 | 20 | return data->len == 128 + 32; | |
| 1299 | } | ||
| 1300 | |||
| 1301 | ✗ | return false; | |
| 1302 | } | ||
| 1303 | |||
| 1304 | 229 | static void netlink_parse_nat(struct netlink_parse_ctx *ctx, | |
| 1305 | const struct location *loc, | ||
| 1306 | const struct nftnl_expr *nle) | ||
| 1307 | { | ||
| 1308 | struct stmt *stmt; | ||
| 1309 | struct expr *addr, *proto; | ||
| 1310 | enum nft_registers reg1, reg2; | ||
| 1311 | int family; | ||
| 1312 | |||
| 1313 | 229 | stmt = nat_stmt_alloc(loc, | |
| 1314 | 229 | nftnl_expr_get_u32(nle, NFTNL_EXPR_NAT_TYPE)); | |
| 1315 | |||
| 1316 | 229 | family = nftnl_expr_get_u32(nle, NFTNL_EXPR_NAT_FAMILY); | |
| 1317 | |||
| 1318 |
2/2✓ Branch 0 (5→6) taken 64 times.
✓ Branch 1 (5→7) taken 165 times.
|
229 | if (ctx->table->handle.family == NFPROTO_INET) |
| 1319 | 64 | stmt->nat.family = family; | |
| 1320 | |||
| 1321 |
1/2✓ Branch 0 (8→9) taken 229 times.
✗ Branch 1 (8→11) not taken.
|
229 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_NAT_FLAGS)) |
| 1322 | 229 | stmt->nat.flags = nftnl_expr_get_u32(nle, NFTNL_EXPR_NAT_FLAGS); | |
| 1323 | |||
| 1324 |
2/2✓ Branch 0 (11→12) taken 6 times.
✓ Branch 1 (11→13) taken 223 times.
|
229 | if (stmt->nat.flags & NF_NAT_RANGE_NETMAP) |
| 1325 | 6 | stmt->nat.type_flags |= STMT_NAT_F_PREFIX; | |
| 1326 | |||
| 1327 | 229 | addr = NULL; | |
| 1328 | 229 | reg1 = netlink_parse_register(nle, NFTNL_EXPR_NAT_REG_ADDR_MIN); | |
| 1329 |
2/2✓ Branch 0 (14→15) taken 227 times.
✓ Branch 1 (14→23) taken 2 times.
|
229 | if (reg1) { |
| 1330 | 227 | addr = netlink_get_register(ctx, loc, reg1); | |
| 1331 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→19) taken 227 times.
|
227 | if (addr == NULL) { |
| 1332 | ✗ | netlink_error(ctx, loc, | |
| 1333 | "NAT statement has no address expression"); | ||
| 1334 | ✗ | goto out_err; | |
| 1335 | } | ||
| 1336 | |||
| 1337 |
2/2✓ Branch 0 (19→20) taken 177 times.
✓ Branch 1 (19→21) taken 50 times.
|
227 | if (family == NFPROTO_IPV4) |
| 1338 | 177 | expr_set_type(addr, &ipaddr_type, BYTEORDER_BIG_ENDIAN); | |
| 1339 | else | ||
| 1340 | 50 | expr_set_type(addr, &ip6addr_type, | |
| 1341 | BYTEORDER_BIG_ENDIAN); | ||
| 1342 | 227 | stmt->nat.addr = addr; | |
| 1343 | } | ||
| 1344 | |||
| 1345 |
2/2✓ Branch 0 (24→25) taken 41 times.
✓ Branch 1 (24→26) taken 188 times.
|
229 | if (is_nat_addr_map(addr, family, stmt)) { |
| 1346 | 41 | stmt->nat.family = family; | |
| 1347 | 41 | ctx->stmt = stmt; | |
| 1348 | 41 | return; | |
| 1349 | } | ||
| 1350 | |||
| 1351 | 188 | reg2 = netlink_parse_register(nle, NFTNL_EXPR_NAT_REG_ADDR_MAX); | |
| 1352 |
4/4✓ Branch 0 (27→28) taken 186 times.
✓ Branch 1 (27→40) taken 2 times.
✓ Branch 2 (28→29) taken 27 times.
✓ Branch 3 (28→40) taken 159 times.
|
188 | if (reg2 && reg2 != reg1) { |
| 1353 | 27 | addr = netlink_get_register(ctx, loc, reg2); | |
| 1354 |
1/2✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→33) taken 27 times.
|
27 | if (addr == NULL) { |
| 1355 | ✗ | netlink_error(ctx, loc, | |
| 1356 | "NAT statement has no address expression"); | ||
| 1357 | ✗ | goto out_err; | |
| 1358 | } | ||
| 1359 | |||
| 1360 |
2/2✓ Branch 0 (33→34) taken 13 times.
✓ Branch 1 (33→35) taken 14 times.
|
27 | if (family == NFPROTO_IPV4) |
| 1361 | 13 | expr_set_type(addr, &ipaddr_type, BYTEORDER_BIG_ENDIAN); | |
| 1362 | else | ||
| 1363 | 14 | expr_set_type(addr, &ip6addr_type, | |
| 1364 | BYTEORDER_BIG_ENDIAN); | ||
| 1365 |
1/2✓ Branch 0 (36→37) taken 27 times.
✗ Branch 1 (36→39) not taken.
|
27 | if (stmt->nat.addr != NULL) { |
| 1366 | 27 | addr = range_expr_alloc(loc, stmt->nat.addr, addr); | |
| 1367 | 27 | addr = range_expr_to_prefix(addr); | |
| 1368 | } | ||
| 1369 | 27 | stmt->nat.addr = addr; | |
| 1370 | } | ||
| 1371 | |||
| 1372 |
2/2✓ Branch 0 (41→42) taken 38 times.
✓ Branch 1 (41→43) taken 150 times.
|
188 | if (is_nat_proto_map(addr, family)) { |
| 1373 | 38 | stmt->nat.family = family; | |
| 1374 | 38 | stmt->nat.type_flags |= STMT_NAT_F_CONCAT; | |
| 1375 | 38 | ctx->stmt = stmt; | |
| 1376 | 38 | return; | |
| 1377 | } | ||
| 1378 | |||
| 1379 | 150 | reg1 = netlink_parse_register(nle, NFTNL_EXPR_NAT_REG_PROTO_MIN); | |
| 1380 |
2/2✓ Branch 0 (44→45) taken 41 times.
✓ Branch 1 (44→51) taken 109 times.
|
150 | if (reg1) { |
| 1381 | 41 | proto = netlink_get_register(ctx, loc, reg1); | |
| 1382 |
1/2✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→49) taken 41 times.
|
41 | if (proto == NULL) { |
| 1383 | ✗ | netlink_error(ctx, loc, | |
| 1384 | "NAT statement has no proto expression"); | ||
| 1385 | ✗ | goto out_err; | |
| 1386 | } | ||
| 1387 | |||
| 1388 | 41 | expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN); | |
| 1389 | 41 | stmt->nat.proto = proto; | |
| 1390 | } | ||
| 1391 | |||
| 1392 | 150 | reg2 = netlink_parse_register(nle, NFTNL_EXPR_NAT_REG_PROTO_MAX); | |
| 1393 |
4/4✓ Branch 0 (52→53) taken 41 times.
✓ Branch 1 (52→62) taken 109 times.
✓ Branch 2 (53→54) taken 8 times.
✓ Branch 3 (53→62) taken 33 times.
|
150 | if (reg2 && reg2 != reg1) { |
| 1394 | 8 | proto = netlink_get_register(ctx, loc, reg2); | |
| 1395 |
1/2✗ Branch 0 (55→56) not taken.
✓ Branch 1 (55→58) taken 8 times.
|
8 | if (proto == NULL) { |
| 1396 | ✗ | netlink_error(ctx, loc, | |
| 1397 | "NAT statement has no proto expression"); | ||
| 1398 | ✗ | goto out_err; | |
| 1399 | } | ||
| 1400 | |||
| 1401 | 8 | expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN); | |
| 1402 |
1/2✓ Branch 0 (59→60) taken 8 times.
✗ Branch 1 (59→61) not taken.
|
8 | if (stmt->nat.proto != NULL) |
| 1403 | 8 | proto = range_expr_alloc(loc, stmt->nat.proto, proto); | |
| 1404 | 8 | stmt->nat.proto = proto; | |
| 1405 | } | ||
| 1406 | |||
| 1407 | 150 | ctx->stmt = stmt; | |
| 1408 | 150 | return; | |
| 1409 | ✗ | out_err: | |
| 1410 | ✗ | stmt_free(stmt); | |
| 1411 | } | ||
| 1412 | |||
| 1413 | 44 | static void netlink_parse_synproxy(struct netlink_parse_ctx *ctx, | |
| 1414 | const struct location *loc, | ||
| 1415 | const struct nftnl_expr *nle) | ||
| 1416 | { | ||
| 1417 | struct stmt *stmt; | ||
| 1418 | |||
| 1419 | 44 | stmt = synproxy_stmt_alloc(loc); | |
| 1420 | 44 | stmt->synproxy.mss = nftnl_expr_get_u16(nle, NFTNL_EXPR_SYNPROXY_MSS); | |
| 1421 | 44 | stmt->synproxy.wscale = nftnl_expr_get_u8(nle, | |
| 1422 | NFTNL_EXPR_SYNPROXY_WSCALE); | ||
| 1423 | 44 | stmt->synproxy.flags = nftnl_expr_get_u32(nle, | |
| 1424 | NFTNL_EXPR_SYNPROXY_FLAGS); | ||
| 1425 | |||
| 1426 | 44 | ctx->stmt = stmt; | |
| 1427 | 44 | } | |
| 1428 | |||
| 1429 | 49 | static void netlink_parse_tproxy(struct netlink_parse_ctx *ctx, | |
| 1430 | const struct location *loc, | ||
| 1431 | const struct nftnl_expr *nle) | ||
| 1432 | { | ||
| 1433 | struct stmt *stmt; | ||
| 1434 | struct expr *addr, *port; | ||
| 1435 | enum nft_registers reg; | ||
| 1436 | |||
| 1437 | 49 | stmt = tproxy_stmt_alloc(loc); | |
| 1438 | 49 | stmt->tproxy.family = nftnl_expr_get_u32(nle, NFTNL_EXPR_TPROXY_FAMILY); | |
| 1439 | 49 | stmt->tproxy.table_family = ctx->table->handle.family; | |
| 1440 | |||
| 1441 | 49 | reg = netlink_parse_register(nle, NFTNL_EXPR_TPROXY_REG_ADDR); | |
| 1442 |
2/2✓ Branch 0 (5→6) taken 27 times.
✓ Branch 1 (5→17) taken 22 times.
|
49 | if (reg) { |
| 1443 | 27 | addr = netlink_get_register(ctx, loc, reg); | |
| 1444 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 27 times.
|
27 | if (addr == NULL) |
| 1445 | ✗ | goto err; | |
| 1446 | |||
| 1447 |
2/3✓ Branch 0 (9→10) taken 15 times.
✓ Branch 1 (9→11) taken 12 times.
✗ Branch 2 (9→13) not taken.
|
27 | switch (stmt->tproxy.family) { |
| 1448 | 15 | case NFPROTO_IPV4: | |
| 1449 | 15 | expr_set_type(addr, &ipaddr_type, BYTEORDER_BIG_ENDIAN); | |
| 1450 | 15 | break; | |
| 1451 | 12 | case NFPROTO_IPV6: | |
| 1452 | 12 | expr_set_type(addr, &ip6addr_type, BYTEORDER_BIG_ENDIAN); | |
| 1453 | 12 | break; | |
| 1454 | ✗ | default: | |
| 1455 | ✗ | netlink_error(ctx, loc, | |
| 1456 | "tproxy address must be IPv4 or IPv6"); | ||
| 1457 | ✗ | goto err; | |
| 1458 | } | ||
| 1459 | 27 | stmt->tproxy.addr = addr; | |
| 1460 | } | ||
| 1461 | |||
| 1462 | 49 | reg = netlink_parse_register(nle, NFTNL_EXPR_TPROXY_REG_PORT); | |
| 1463 |
2/2✓ Branch 0 (18→19) taken 37 times.
✓ Branch 1 (18→24) taken 12 times.
|
49 | if (reg) { |
| 1464 | 37 | port = netlink_get_register(ctx, loc, reg); | |
| 1465 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 37 times.
|
37 | if (port == NULL) |
| 1466 | ✗ | goto err; | |
| 1467 | 37 | expr_set_type(port, &inet_service_type, BYTEORDER_BIG_ENDIAN); | |
| 1468 | 37 | stmt->tproxy.port = port; | |
| 1469 | } | ||
| 1470 | |||
| 1471 | 49 | ctx->stmt = stmt; | |
| 1472 | 49 | return; | |
| 1473 | ✗ | err: | |
| 1474 | ✗ | stmt_free(stmt); | |
| 1475 | } | ||
| 1476 | |||
| 1477 | 82 | static void netlink_parse_masq(struct netlink_parse_ctx *ctx, | |
| 1478 | const struct location *loc, | ||
| 1479 | const struct nftnl_expr *nle) | ||
| 1480 | { | ||
| 1481 | enum nft_registers reg1, reg2; | ||
| 1482 | struct expr *proto; | ||
| 1483 | struct stmt *stmt; | ||
| 1484 | 82 | uint32_t flags = 0; | |
| 1485 | |||
| 1486 |
2/2✓ Branch 0 (3→4) taken 50 times.
✓ Branch 1 (3→5) taken 32 times.
|
82 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_MASQ_FLAGS)) |
| 1487 | 50 | flags = nftnl_expr_get_u32(nle, NFTNL_EXPR_MASQ_FLAGS); | |
| 1488 | |||
| 1489 | 82 | stmt = nat_stmt_alloc(loc, NFT_NAT_MASQ); | |
| 1490 | 82 | stmt->nat.flags = flags; | |
| 1491 | |||
| 1492 | 82 | reg1 = netlink_parse_register(nle, NFTNL_EXPR_MASQ_REG_PROTO_MIN); | |
| 1493 |
2/2✓ Branch 0 (7→8) taken 8 times.
✓ Branch 1 (7→14) taken 74 times.
|
82 | if (reg1) { |
| 1494 | 8 | proto = netlink_get_register(ctx, loc, reg1); | |
| 1495 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 8 times.
|
8 | if (proto == NULL) { |
| 1496 | ✗ | netlink_error(ctx, loc, | |
| 1497 | "MASQUERADE statement has no proto expression"); | ||
| 1498 | ✗ | goto out_err; | |
| 1499 | } | ||
| 1500 | 8 | expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN); | |
| 1501 | 8 | stmt->nat.proto = proto; | |
| 1502 | } | ||
| 1503 | |||
| 1504 | 82 | reg2 = netlink_parse_register(nle, NFTNL_EXPR_MASQ_REG_PROTO_MAX); | |
| 1505 |
4/4✓ Branch 0 (15→16) taken 8 times.
✓ Branch 1 (15→25) taken 74 times.
✓ Branch 2 (16→17) taken 4 times.
✓ Branch 3 (16→25) taken 4 times.
|
82 | if (reg2 && reg2 != reg1) { |
| 1506 | 4 | proto = netlink_get_register(ctx, loc, reg2); | |
| 1507 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→21) taken 4 times.
|
4 | if (proto == NULL) { |
| 1508 | ✗ | netlink_error(ctx, loc, | |
| 1509 | "MASQUERADE statement has no proto expression"); | ||
| 1510 | ✗ | goto out_err; | |
| 1511 | } | ||
| 1512 | 4 | expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN); | |
| 1513 |
1/2✓ Branch 0 (22→23) taken 4 times.
✗ Branch 1 (22→24) not taken.
|
4 | if (stmt->nat.proto != NULL) |
| 1514 | 4 | proto = range_expr_alloc(loc, stmt->nat.proto, proto); | |
| 1515 | 4 | stmt->nat.proto = proto; | |
| 1516 | } | ||
| 1517 | |||
| 1518 | 82 | ctx->stmt = stmt; | |
| 1519 | 82 | return; | |
| 1520 | ✗ | out_err: | |
| 1521 | ✗ | stmt_free(stmt); | |
| 1522 | } | ||
| 1523 | |||
| 1524 | 108 | static void netlink_parse_redir(struct netlink_parse_ctx *ctx, | |
| 1525 | const struct location *loc, | ||
| 1526 | const struct nftnl_expr *nle) | ||
| 1527 | { | ||
| 1528 | struct stmt *stmt; | ||
| 1529 | struct expr *proto; | ||
| 1530 | enum nft_registers reg1, reg2; | ||
| 1531 | uint32_t flags; | ||
| 1532 | |||
| 1533 | 108 | stmt = nat_stmt_alloc(loc, NFT_NAT_REDIR); | |
| 1534 | |||
| 1535 |
2/2✓ Branch 0 (4→5) taken 84 times.
✓ Branch 1 (4→7) taken 24 times.
|
108 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_REDIR_FLAGS)) { |
| 1536 | 84 | flags = nftnl_expr_get_u32(nle, NFTNL_EXPR_REDIR_FLAGS); | |
| 1537 | 84 | stmt->nat.flags = flags; | |
| 1538 | } | ||
| 1539 | |||
| 1540 | 108 | reg1 = netlink_parse_register(nle, NFTNL_EXPR_REDIR_REG_PROTO_MIN); | |
| 1541 |
2/2✓ Branch 0 (8→9) taken 44 times.
✓ Branch 1 (8→15) taken 64 times.
|
108 | if (reg1) { |
| 1542 | 44 | proto = netlink_get_register(ctx, loc, reg1); | |
| 1543 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 44 times.
|
44 | if (proto == NULL) { |
| 1544 | ✗ | netlink_error(ctx, loc, | |
| 1545 | "redirect statement has no proto expression"); | ||
| 1546 | ✗ | goto out_err; | |
| 1547 | } | ||
| 1548 | |||
| 1549 | 44 | expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN); | |
| 1550 | 44 | stmt->nat.proto = proto; | |
| 1551 | } | ||
| 1552 | |||
| 1553 | 108 | reg2 = netlink_parse_register(nle, NFTNL_EXPR_REDIR_REG_PROTO_MAX); | |
| 1554 |
4/4✓ Branch 0 (16→17) taken 44 times.
✓ Branch 1 (16→26) taken 64 times.
✓ Branch 2 (17→18) taken 4 times.
✓ Branch 3 (17→26) taken 40 times.
|
108 | if (reg2 && reg2 != reg1) { |
| 1555 | 4 | proto = netlink_get_register(ctx, loc, reg2); | |
| 1556 |
1/2✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→22) taken 4 times.
|
4 | if (proto == NULL) { |
| 1557 | ✗ | netlink_error(ctx, loc, | |
| 1558 | "redirect statement has no proto expression"); | ||
| 1559 | ✗ | goto out_err; | |
| 1560 | } | ||
| 1561 | |||
| 1562 | 4 | expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN); | |
| 1563 |
1/2✓ Branch 0 (23→24) taken 4 times.
✗ Branch 1 (23→25) not taken.
|
4 | if (stmt->nat.proto != NULL) |
| 1564 | 4 | proto = range_expr_alloc(loc, stmt->nat.proto, | |
| 1565 | proto); | ||
| 1566 | 4 | stmt->nat.proto = proto; | |
| 1567 | } | ||
| 1568 | |||
| 1569 | 108 | ctx->stmt = stmt; | |
| 1570 | 108 | return; | |
| 1571 | ✗ | out_err: | |
| 1572 | ✗ | stmt_free(stmt); | |
| 1573 | } | ||
| 1574 | |||
| 1575 | 23 | static void netlink_parse_dup(struct netlink_parse_ctx *ctx, | |
| 1576 | const struct location *loc, | ||
| 1577 | const struct nftnl_expr *nle) | ||
| 1578 | { | ||
| 1579 | enum nft_registers reg1, reg2; | ||
| 1580 | struct expr *addr, *dev; | ||
| 1581 | struct stmt *stmt; | ||
| 1582 | |||
| 1583 | 23 | stmt = dup_stmt_alloc(loc); | |
| 1584 | |||
| 1585 | 23 | reg1 = netlink_parse_register(nle, NFTNL_EXPR_DUP_SREG_ADDR); | |
| 1586 |
2/2✓ Branch 0 (4→5) taken 14 times.
✓ Branch 1 (4→15) taken 9 times.
|
23 | if (reg1) { |
| 1587 | 14 | addr = netlink_get_register(ctx, loc, reg1); | |
| 1588 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 14 times.
|
14 | if (addr == NULL) { |
| 1589 | ✗ | netlink_error(ctx, loc, | |
| 1590 | "DUP statement has no destination expression"); | ||
| 1591 | ✗ | goto out_err; | |
| 1592 | } | ||
| 1593 | |||
| 1594 |
2/3✓ Branch 0 (9→10) taken 7 times.
✓ Branch 1 (9→12) taken 7 times.
✗ Branch 2 (9→14) not taken.
|
14 | switch (ctx->table->handle.family) { |
| 1595 | 7 | case NFPROTO_IPV4: | |
| 1596 | 7 | expr_set_type(addr, &ipaddr_type, BYTEORDER_BIG_ENDIAN); | |
| 1597 | 7 | break; | |
| 1598 | 7 | case NFPROTO_IPV6: | |
| 1599 | 7 | expr_set_type(addr, &ip6addr_type, | |
| 1600 | BYTEORDER_BIG_ENDIAN); | ||
| 1601 | 7 | break; | |
| 1602 | } | ||
| 1603 | 14 | stmt->dup.to = addr; | |
| 1604 | } | ||
| 1605 | |||
| 1606 | 23 | reg2 = netlink_parse_register(nle, NFTNL_EXPR_DUP_SREG_DEV); | |
| 1607 |
2/2✓ Branch 0 (16→17) taken 19 times.
✓ Branch 1 (16→25) taken 4 times.
|
23 | if (reg2) { |
| 1608 | 19 | dev = netlink_get_register(ctx, loc, reg2); | |
| 1609 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→21) taken 19 times.
|
19 | if (dev == NULL) { |
| 1610 | ✗ | netlink_error(ctx, loc, | |
| 1611 | "DUP statement has no output expression"); | ||
| 1612 | ✗ | goto out_err; | |
| 1613 | } | ||
| 1614 | |||
| 1615 | 19 | expr_set_type(dev, &ifindex_type, BYTEORDER_HOST_ENDIAN); | |
| 1616 |
2/2✓ Branch 0 (22→23) taken 9 times.
✓ Branch 1 (22→24) taken 10 times.
|
19 | if (stmt->dup.to == NULL) |
| 1617 | 9 | stmt->dup.to = dev; | |
| 1618 | else | ||
| 1619 | 10 | stmt->dup.dev = dev; | |
| 1620 | } | ||
| 1621 | |||
| 1622 | 23 | ctx->stmt = stmt; | |
| 1623 | 23 | return; | |
| 1624 | ✗ | out_err: | |
| 1625 | ✗ | stmt_free(stmt); | |
| 1626 | } | ||
| 1627 | |||
| 1628 | 13 | static void netlink_parse_fwd(struct netlink_parse_ctx *ctx, | |
| 1629 | const struct location *loc, | ||
| 1630 | const struct nftnl_expr *nle) | ||
| 1631 | { | ||
| 1632 | enum nft_registers reg1, reg2; | ||
| 1633 | struct expr *dev, *addr; | ||
| 1634 | struct stmt *stmt; | ||
| 1635 | |||
| 1636 | 13 | stmt = fwd_stmt_alloc(loc); | |
| 1637 | |||
| 1638 | 13 | reg1 = netlink_parse_register(nle, NFTNL_EXPR_FWD_SREG_DEV); | |
| 1639 |
1/2✓ Branch 0 (4→5) taken 13 times.
✗ Branch 1 (4→11) not taken.
|
13 | if (reg1) { |
| 1640 | 13 | dev = netlink_get_register(ctx, loc, reg1); | |
| 1641 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 13 times.
|
13 | if (dev == NULL) { |
| 1642 | ✗ | netlink_error(ctx, loc, | |
| 1643 | "fwd statement has no output expression"); | ||
| 1644 | ✗ | goto out_err; | |
| 1645 | } | ||
| 1646 | |||
| 1647 | 13 | expr_set_type(dev, &ifindex_type, BYTEORDER_HOST_ENDIAN); | |
| 1648 | 13 | stmt->fwd.dev = dev; | |
| 1649 | } | ||
| 1650 | |||
| 1651 |
2/2✓ Branch 0 (12→13) taken 5 times.
✓ Branch 1 (12→15) taken 8 times.
|
13 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_FWD_NFPROTO)) { |
| 1652 | 5 | stmt->fwd.family = | |
| 1653 | 5 | nftnl_expr_get_u32(nle, NFTNL_EXPR_FWD_NFPROTO); | |
| 1654 | } | ||
| 1655 | |||
| 1656 |
2/2✓ Branch 0 (16→17) taken 5 times.
✓ Branch 1 (16→31) taken 8 times.
|
13 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_FWD_SREG_ADDR)) { |
| 1657 | 5 | reg2 = netlink_parse_register(nle, NFTNL_EXPR_FWD_SREG_ADDR); | |
| 1658 |
1/2✓ Branch 0 (18→19) taken 5 times.
✗ Branch 1 (18→31) not taken.
|
5 | if (reg2) { |
| 1659 | 5 | addr = netlink_get_register(ctx, loc, reg2); | |
| 1660 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→23) taken 5 times.
|
5 | if (addr == NULL) { |
| 1661 | ✗ | netlink_error(ctx, loc, | |
| 1662 | "fwd statement has no output expression"); | ||
| 1663 | ✗ | goto out_err; | |
| 1664 | } | ||
| 1665 | |||
| 1666 |
1/3✓ Branch 0 (23→24) taken 5 times.
✗ Branch 1 (23→25) not taken.
✗ Branch 2 (23→27) not taken.
|
5 | switch (stmt->fwd.family) { |
| 1667 | 5 | case AF_INET: | |
| 1668 | 5 | expr_set_type(addr, &ipaddr_type, | |
| 1669 | BYTEORDER_BIG_ENDIAN); | ||
| 1670 | 5 | break; | |
| 1671 | ✗ | case AF_INET6: | |
| 1672 | ✗ | expr_set_type(addr, &ip6addr_type, | |
| 1673 | BYTEORDER_BIG_ENDIAN); | ||
| 1674 | ✗ | break; | |
| 1675 | ✗ | default: | |
| 1676 | ✗ | netlink_error(ctx, loc, | |
| 1677 | "fwd statement has no family"); | ||
| 1678 | ✗ | goto out_err; | |
| 1679 | } | ||
| 1680 | 5 | stmt->fwd.addr = addr; | |
| 1681 | } | ||
| 1682 | } | ||
| 1683 | |||
| 1684 | 13 | ctx->stmt = stmt; | |
| 1685 | 13 | return; | |
| 1686 | ✗ | out_err: | |
| 1687 | ✗ | stmt_free(stmt); | |
| 1688 | } | ||
| 1689 | |||
| 1690 | 167 | static void netlink_parse_queue(struct netlink_parse_ctx *ctx, | |
| 1691 | const struct location *loc, | ||
| 1692 | const struct nftnl_expr *nle) | ||
| 1693 | { | ||
| 1694 | struct expr *expr; | ||
| 1695 | uint16_t flags; | ||
| 1696 | |||
| 1697 |
2/2✓ Branch 0 (3→4) taken 42 times.
✓ Branch 1 (3→9) taken 125 times.
|
167 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_QUEUE_SREG_QNUM)) { |
| 1698 | 42 | enum nft_registers reg = netlink_parse_register(nle, NFTNL_EXPR_QUEUE_SREG_QNUM); | |
| 1699 | |||
| 1700 | 42 | expr = netlink_get_register(ctx, loc, reg); | |
| 1701 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→16) taken 42 times.
|
42 | if (!expr) { |
| 1702 | ✗ | netlink_error(ctx, loc, "queue statement has no sreg expression"); | |
| 1703 | ✗ | return; | |
| 1704 | } | ||
| 1705 | } else { | ||
| 1706 | 125 | uint16_t total = nftnl_expr_get_u16(nle, NFTNL_EXPR_QUEUE_TOTAL); | |
| 1707 | 125 | uint16_t num = nftnl_expr_get_u16(nle, NFTNL_EXPR_QUEUE_NUM); | |
| 1708 | |||
| 1709 | 125 | expr = constant_expr_alloc(loc, &integer_type, | |
| 1710 | BYTEORDER_HOST_ENDIAN, 16, &num); | ||
| 1711 | |||
| 1712 |
2/2✓ Branch 0 (12→13) taken 75 times.
✓ Branch 1 (12→15) taken 50 times.
|
125 | if (total > 1) { |
| 1713 | struct expr *high; | ||
| 1714 | |||
| 1715 | 75 | total += num - 1; | |
| 1716 | 75 | high = constant_expr_alloc(loc, &integer_type, | |
| 1717 | BYTEORDER_HOST_ENDIAN, 16, &total); | ||
| 1718 | 75 | expr = range_expr_alloc(loc, expr, high); | |
| 1719 | } | ||
| 1720 | } | ||
| 1721 | |||
| 1722 | 167 | flags = nftnl_expr_get_u16(nle, NFTNL_EXPR_QUEUE_FLAGS); | |
| 1723 | 167 | ctx->stmt = queue_stmt_alloc(loc, expr, flags); | |
| 1724 | } | ||
| 1725 | |||
| 1726 | struct dynset_parse_ctx { | ||
| 1727 | struct netlink_parse_ctx *nlctx; | ||
| 1728 | const struct location *loc; | ||
| 1729 | struct list_head stmt_list; | ||
| 1730 | }; | ||
| 1731 | |||
| 1732 | 4 | static int dynset_parse_expressions(struct nftnl_expr *e, void *data) | |
| 1733 | { | ||
| 1734 | 4 | struct dynset_parse_ctx *dynset_parse_ctx = data; | |
| 1735 | 4 | struct netlink_parse_ctx *ctx = dynset_parse_ctx->nlctx; | |
| 1736 | 4 | const struct location *loc = dynset_parse_ctx->loc; | |
| 1737 | struct stmt *stmt; | ||
| 1738 | |||
| 1739 |
2/4✓ Branch 0 (3→4) taken 4 times.
✗ Branch 1 (3→5) not taken.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→7) taken 4 times.
|
4 | if (netlink_parse_expr(e, ctx) < 0 || !ctx->stmt) { |
| 1740 | ✗ | netlink_error(ctx, loc, "Could not parse dynset stmt"); | |
| 1741 | ✗ | return -1; | |
| 1742 | } | ||
| 1743 | 4 | stmt = ctx->stmt; | |
| 1744 | |||
| 1745 | 4 | list_add_tail(&stmt->list, &dynset_parse_ctx->stmt_list); | |
| 1746 | |||
| 1747 | 4 | return 0; | |
| 1748 | } | ||
| 1749 | |||
| 1750 | 273 | static void netlink_parse_dynset(struct netlink_parse_ctx *ctx, | |
| 1751 | const struct location *loc, | ||
| 1752 | const struct nftnl_expr *nle) | ||
| 1753 | { | ||
| 1754 | 273 | struct dynset_parse_ctx dynset_parse_ctx = { | |
| 1755 | .nlctx = ctx, | ||
| 1756 | .loc = loc, | ||
| 1757 | }; | ||
| 1758 | 273 | struct expr *expr, *expr_data = NULL; | |
| 1759 | enum nft_registers sreg, sreg_data; | ||
| 1760 | struct stmt *stmt, *dstmt, *next; | ||
| 1761 | const struct nftnl_expr *dnle; | ||
| 1762 | struct set *set; | ||
| 1763 | const char *name; | ||
| 1764 | |||
| 1765 | 273 | init_list_head(&dynset_parse_ctx.stmt_list); | |
| 1766 | |||
| 1767 | 273 | name = nftnl_expr_get_str(nle, NFTNL_EXPR_DYNSET_SET_NAME); | |
| 1768 | 273 | set = set_cache_find(ctx->table, name); | |
| 1769 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 273 times.
|
273 | if (set == NULL) |
| 1770 | 273 | return netlink_error(ctx, loc, | |
| 1771 | "Unknown set '%s' in dynset statement", | ||
| 1772 | name); | ||
| 1773 | |||
| 1774 | 273 | sreg = netlink_parse_register(nle, NFTNL_EXPR_DYNSET_SREG_KEY); | |
| 1775 | 273 | expr = netlink_get_register(ctx, loc, sreg); | |
| 1776 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 273 times.
|
273 | if (expr == NULL) |
| 1777 | ✗ | return netlink_error(ctx, loc, | |
| 1778 | "Dynset statement has no key expression"); | ||
| 1779 | |||
| 1780 |
2/2✓ Branch 0 (13→14) taken 202 times.
✓ Branch 1 (13→18) taken 71 times.
|
273 | if (expr->len < set->key->len) { |
| 1781 | 202 | expr_free(expr); | |
| 1782 | 202 | expr = netlink_parse_concat_key(ctx, loc, sreg, set->key); | |
| 1783 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→21) taken 202 times.
|
202 | if (expr == NULL) |
| 1784 | ✗ | return; | |
| 1785 |
2/2✓ Branch 0 (18→19) taken 3 times.
✓ Branch 1 (18→21) taken 68 times.
|
71 | } else if (expr->dtype == &invalid_type) { |
| 1786 | 3 | expr_set_type(expr, datatype_get(set->key->dtype), set->key->byteorder); | |
| 1787 | } | ||
| 1788 | |||
| 1789 | 273 | expr = set_elem_expr_alloc(&expr->location, expr); | |
| 1790 | 273 | expr->timeout = nftnl_expr_get_u64(nle, NFTNL_EXPR_DYNSET_TIMEOUT); | |
| 1791 | |||
| 1792 |
2/2✓ Branch 0 (24→25) taken 20 times.
✓ Branch 1 (24→34) taken 253 times.
|
273 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_DYNSET_EXPR)) { |
| 1793 | 20 | dstmt = NULL; | |
| 1794 | 20 | dnle = nftnl_expr_get(nle, NFTNL_EXPR_DYNSET_EXPR, NULL); | |
| 1795 |
1/2✓ Branch 0 (26→27) taken 20 times.
✗ Branch 1 (26→39) not taken.
|
20 | if (dnle != NULL) { |
| 1796 |
1/2✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→30) taken 20 times.
|
20 | if (netlink_parse_expr(dnle, ctx) < 0) |
| 1797 | ✗ | goto out_err; | |
| 1798 |
1/2✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→33) taken 20 times.
|
20 | if (ctx->stmt == NULL) { |
| 1799 | ✗ | netlink_error(ctx, loc, | |
| 1800 | "Could not parse dynset stmt"); | ||
| 1801 | ✗ | goto out_err; | |
| 1802 | } | ||
| 1803 | 20 | dstmt = ctx->stmt; | |
| 1804 | 20 | list_add_tail(&dstmt->list, | |
| 1805 | &dynset_parse_ctx.stmt_list); | ||
| 1806 | } | ||
| 1807 |
2/2✓ Branch 0 (35→36) taken 2 times.
✓ Branch 1 (35→39) taken 251 times.
|
253 | } else if (nftnl_expr_is_set(nle, NFTNL_EXPR_DYNSET_EXPRESSIONS)) { |
| 1808 |
1/2✗ Branch 0 (37→38) not taken.
✓ Branch 1 (37→39) taken 2 times.
|
2 | if (nftnl_expr_expr_foreach(nle, dynset_parse_expressions, |
| 1809 | &dynset_parse_ctx) < 0) | ||
| 1810 | ✗ | goto out_err; | |
| 1811 | } | ||
| 1812 | |||
| 1813 |
2/2✓ Branch 0 (40→41) taken 61 times.
✓ Branch 1 (40→49) taken 212 times.
|
273 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_DYNSET_SREG_DATA)) { |
| 1814 | 61 | sreg_data = netlink_parse_register(nle, NFTNL_EXPR_DYNSET_SREG_DATA); | |
| 1815 | 61 | expr_data = netlink_get_register(ctx, loc, sreg_data); | |
| 1816 | |||
| 1817 |
3/4✓ Branch 0 (43→44) taken 61 times.
✗ Branch 1 (43→49) not taken.
✓ Branch 2 (44→45) taken 26 times.
✓ Branch 3 (44→49) taken 35 times.
|
61 | if (expr_data && expr_data->len < set->data->len) { |
| 1818 | 26 | expr_free(expr_data); | |
| 1819 | 26 | expr_data = netlink_parse_concat_expr(ctx, loc, sreg_data, set->data->len); | |
| 1820 |
1/2✗ Branch 0 (47→48) not taken.
✓ Branch 1 (47→49) taken 26 times.
|
26 | if (expr_data == NULL) |
| 1821 | ✗ | netlink_error(ctx, loc, | |
| 1822 | "Could not parse dynset map data expressions"); | ||
| 1823 | } | ||
| 1824 | } | ||
| 1825 | |||
| 1826 |
2/2✓ Branch 0 (49→50) taken 61 times.
✓ Branch 1 (49→55) taken 212 times.
|
273 | if (expr_data != NULL) { |
| 1827 | 61 | expr_set_type(expr_data, set->data->dtype, set->data->byteorder); | |
| 1828 | 61 | stmt = map_stmt_alloc(loc); | |
| 1829 | 61 | stmt->map.set = set_ref_expr_alloc(loc, set); | |
| 1830 | 61 | stmt->map.key = expr; | |
| 1831 | 61 | stmt->map.data = expr_data; | |
| 1832 | 61 | stmt->map.op = nftnl_expr_get_u32(nle, NFTNL_EXPR_DYNSET_OP); | |
| 1833 | 61 | list_splice_tail(&dynset_parse_ctx.stmt_list, | |
| 1834 | &stmt->map.stmt_list); | ||
| 1835 | } else { | ||
| 1836 |
3/4✓ Branch 0 (56→57) taken 22 times.
✓ Branch 1 (56→62) taken 190 times.
✗ Branch 2 (58→59) not taken.
✓ Branch 3 (58→62) taken 22 times.
|
234 | if (!list_empty(&dynset_parse_ctx.stmt_list) && |
| 1837 | 22 | set_is_anonymous(set->flags)) { | |
| 1838 | ✗ | stmt = meter_stmt_alloc(loc); | |
| 1839 | ✗ | stmt->meter.set = set_ref_expr_alloc(loc, set); | |
| 1840 | ✗ | stmt->meter.key = expr; | |
| 1841 | ✗ | stmt->meter.stmt = list_first_entry(&dynset_parse_ctx.stmt_list, | |
| 1842 | struct stmt, list); | ||
| 1843 | ✗ | stmt->meter.size = set->desc.size; | |
| 1844 | } else { | ||
| 1845 | 212 | stmt = set_stmt_alloc(loc); | |
| 1846 | 212 | stmt->set.set = set_ref_expr_alloc(loc, set); | |
| 1847 | 212 | stmt->set.op = nftnl_expr_get_u32(nle, NFTNL_EXPR_DYNSET_OP); | |
| 1848 | 212 | stmt->set.key = expr; | |
| 1849 | 212 | list_splice_tail(&dynset_parse_ctx.stmt_list, | |
| 1850 | &stmt->set.stmt_list); | ||
| 1851 | } | ||
| 1852 | } | ||
| 1853 | |||
| 1854 | 273 | ctx->stmt = stmt; | |
| 1855 | 273 | return; | |
| 1856 | ✗ | out_err: | |
| 1857 | ✗ | list_for_each_entry_safe(dstmt, next, &dynset_parse_ctx.stmt_list, list) | |
| 1858 | ✗ | stmt_free(dstmt); | |
| 1859 | |||
| 1860 | ✗ | expr_free(expr); | |
| 1861 | } | ||
| 1862 | |||
| 1863 | 1853 | static void netlink_parse_objref(struct netlink_parse_ctx *ctx, | |
| 1864 | const struct location *loc, | ||
| 1865 | const struct nftnl_expr *nle) | ||
| 1866 | { | ||
| 1867 | 1853 | uint32_t type = nftnl_expr_get_u32(nle, NFTNL_EXPR_OBJREF_IMM_TYPE); | |
| 1868 | struct expr *expr; | ||
| 1869 | struct stmt *stmt; | ||
| 1870 | |||
| 1871 |
2/2✓ Branch 0 (4→5) taken 1800 times.
✓ Branch 1 (4→10) taken 53 times.
|
1853 | if (nftnl_expr_is_set(nle, NFTNL_EXPR_OBJREF_IMM_NAME)) { |
| 1872 | struct nft_data_delinearize nld; | ||
| 1873 | |||
| 1874 | 1800 | type = nftnl_expr_get_u32(nle, NFTNL_EXPR_OBJREF_IMM_TYPE); | |
| 1875 | 1800 | nld.value = nftnl_expr_get(nle, NFTNL_EXPR_OBJREF_IMM_NAME, | |
| 1876 | &nld.len); | ||
| 1877 | 1800 | expr = netlink_alloc_value(&netlink_location, &nld); | |
| 1878 | 1800 | datatype_set(expr, &string_type); | |
| 1879 | 1800 | expr->byteorder = BYTEORDER_HOST_ENDIAN; | |
| 1880 |
1/2✓ Branch 0 (11→12) taken 53 times.
✗ Branch 1 (11→31) not taken.
|
53 | } else if (nftnl_expr_is_set(nle, NFTNL_EXPR_OBJREF_SET_SREG)) { |
| 1881 | struct expr *left, *right; | ||
| 1882 | enum nft_registers sreg; | ||
| 1883 | const char *name; | ||
| 1884 | struct set *set; | ||
| 1885 | |||
| 1886 | 53 | name = nftnl_expr_get_str(nle, NFTNL_EXPR_OBJREF_SET_NAME); | |
| 1887 | 53 | set = set_cache_find(ctx->table, name); | |
| 1888 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→17) taken 53 times.
|
53 | if (set == NULL) |
| 1889 | ✗ | return netlink_error(ctx, loc, | |
| 1890 | "Unknown set '%s' in objref expression", | ||
| 1891 | name); | ||
| 1892 | |||
| 1893 | 53 | sreg = netlink_parse_register(nle, NFTNL_EXPR_OBJREF_SET_SREG); | |
| 1894 | 53 | left = netlink_get_register(ctx, loc, sreg); | |
| 1895 |
1/2✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→22) taken 53 times.
|
53 | if (left == NULL) |
| 1896 | ✗ | return netlink_error(ctx, loc, | |
| 1897 | "objref expression has no left hand side"); | ||
| 1898 | |||
| 1899 |
2/2✓ Branch 0 (22→23) taken 6 times.
✓ Branch 1 (22→27) taken 47 times.
|
53 | if (left->len < set->key->len) { |
| 1900 | 6 | expr_free(left); | |
| 1901 | 6 | left = netlink_parse_concat_expr(ctx, loc, sreg, set->key->len); | |
| 1902 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 6 times.
|
6 | if (left == NULL) |
| 1903 | ✗ | return; | |
| 1904 | } | ||
| 1905 | |||
| 1906 | 53 | right = set_ref_expr_alloc(loc, set); | |
| 1907 | 53 | expr = map_expr_alloc(loc, left, right); | |
| 1908 | 53 | expr_set_type(expr, &string_type, BYTEORDER_HOST_ENDIAN); | |
| 1909 | 53 | type = set->objtype; | |
| 1910 | } else { | ||
| 1911 | ✗ | netlink_error(ctx, loc, "unknown objref expression type %u", | |
| 1912 | type); | ||
| 1913 | ✗ | return; | |
| 1914 | } | ||
| 1915 | |||
| 1916 | 1853 | stmt = objref_stmt_alloc(loc); | |
| 1917 | 1853 | stmt->objref.type = type; | |
| 1918 | 1853 | stmt->objref.expr = expr; | |
| 1919 | 1853 | ctx->stmt = stmt; | |
| 1920 | } | ||
| 1921 | |||
| 1922 | struct expr_handler { | ||
| 1923 | const char *name; | ||
| 1924 | void (*parse)(struct netlink_parse_ctx *ctx, | ||
| 1925 | const struct location *loc, | ||
| 1926 | const struct nftnl_expr *nle); | ||
| 1927 | }; | ||
| 1928 | |||
| 1929 | static const struct expr_handler netlink_parsers[] = { | ||
| 1930 | { .name = "immediate", .parse = netlink_parse_immediate }, | ||
| 1931 | { .name = "cmp", .parse = netlink_parse_cmp }, | ||
| 1932 | { .name = "lookup", .parse = netlink_parse_lookup }, | ||
| 1933 | { .name = "bitwise", .parse = netlink_parse_bitwise }, | ||
| 1934 | { .name = "byteorder", .parse = netlink_parse_byteorder }, | ||
| 1935 | { .name = "payload", .parse = netlink_parse_payload }, | ||
| 1936 | { .name = "inner", .parse = netlink_parse_inner }, | ||
| 1937 | { .name = "exthdr", .parse = netlink_parse_exthdr }, | ||
| 1938 | { .name = "meta", .parse = netlink_parse_meta }, | ||
| 1939 | { .name = "socket", .parse = netlink_parse_socket }, | ||
| 1940 | { .name = "tunnel", .parse = netlink_parse_tunnel }, | ||
| 1941 | { .name = "osf", .parse = netlink_parse_osf }, | ||
| 1942 | { .name = "rt", .parse = netlink_parse_rt }, | ||
| 1943 | { .name = "ct", .parse = netlink_parse_ct }, | ||
| 1944 | { .name = "connlimit", .parse = netlink_parse_connlimit }, | ||
| 1945 | { .name = "counter", .parse = netlink_parse_counter }, | ||
| 1946 | { .name = "last", .parse = netlink_parse_last }, | ||
| 1947 | { .name = "log", .parse = netlink_parse_log }, | ||
| 1948 | { .name = "limit", .parse = netlink_parse_limit }, | ||
| 1949 | { .name = "range", .parse = netlink_parse_range }, | ||
| 1950 | { .name = "reject", .parse = netlink_parse_reject }, | ||
| 1951 | { .name = "nat", .parse = netlink_parse_nat }, | ||
| 1952 | { .name = "tproxy", .parse = netlink_parse_tproxy }, | ||
| 1953 | { .name = "notrack", .parse = netlink_parse_notrack }, | ||
| 1954 | { .name = "masq", .parse = netlink_parse_masq }, | ||
| 1955 | { .name = "redir", .parse = netlink_parse_redir }, | ||
| 1956 | { .name = "dup", .parse = netlink_parse_dup }, | ||
| 1957 | { .name = "queue", .parse = netlink_parse_queue }, | ||
| 1958 | { .name = "dynset", .parse = netlink_parse_dynset }, | ||
| 1959 | { .name = "fwd", .parse = netlink_parse_fwd }, | ||
| 1960 | { .name = "target", .parse = netlink_parse_target }, | ||
| 1961 | { .name = "match", .parse = netlink_parse_match }, | ||
| 1962 | { .name = "objref", .parse = netlink_parse_objref }, | ||
| 1963 | { .name = "quota", .parse = netlink_parse_quota }, | ||
| 1964 | { .name = "numgen", .parse = netlink_parse_numgen }, | ||
| 1965 | { .name = "hash", .parse = netlink_parse_hash }, | ||
| 1966 | { .name = "fib", .parse = netlink_parse_fib }, | ||
| 1967 | { .name = "tcpopt", .parse = netlink_parse_exthdr }, | ||
| 1968 | { .name = "flow_offload", .parse = netlink_parse_flow_offload }, | ||
| 1969 | { .name = "xfrm", .parse = netlink_parse_xfrm }, | ||
| 1970 | { .name = "synproxy", .parse = netlink_parse_synproxy }, | ||
| 1971 | }; | ||
| 1972 | |||
| 1973 | 327505 | static int netlink_parse_expr(const struct nftnl_expr *nle, | |
| 1974 | struct netlink_parse_ctx *ctx) | ||
| 1975 | { | ||
| 1976 | 327505 | const char *type = nftnl_expr_get_str(nle, NFTNL_EXPR_NAME); | |
| 1977 | struct location loc; | ||
| 1978 | unsigned int i; | ||
| 1979 | |||
| 1980 | 327505 | memset(&loc, 0, sizeof(loc)); | |
| 1981 | 327505 | loc.indesc = &indesc_netlink; | |
| 1982 | 327505 | loc.nle = nle; | |
| 1983 | |||
| 1984 |
1/2✓ Branch 0 (9→4) taken 3827950 times.
✗ Branch 1 (9→10) not taken.
|
3827950 | for (i = 0; i < array_size(netlink_parsers); i++) { |
| 1985 |
2/2✓ Branch 0 (4→5) taken 3500445 times.
✓ Branch 1 (4→6) taken 327505 times.
|
3827950 | if (strcmp(type, netlink_parsers[i].name)) |
| 1986 | 3500445 | continue; | |
| 1987 | |||
| 1988 | 327505 | netlink_parsers[i].parse(ctx, &loc, nle); | |
| 1989 | |||
| 1990 | 327505 | return 0; | |
| 1991 | } | ||
| 1992 | ✗ | netlink_error(ctx, &loc, "unknown expression type '%s'", type); | |
| 1993 | |||
| 1994 | ✗ | return 0; | |
| 1995 | } | ||
| 1996 | |||
| 1997 | 141221 | static int netlink_parse_rule_expr(struct nftnl_expr *nle, void *arg) | |
| 1998 | { | ||
| 1999 | 141221 | struct netlink_parse_ctx *ctx = arg; | |
| 2000 | int err; | ||
| 2001 | |||
| 2002 | 141221 | err = netlink_parse_expr(nle, ctx); | |
| 2003 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 141221 times.
|
141221 | if (err < 0) |
| 2004 | ✗ | return err; | |
| 2005 |
2/2✓ Branch 0 (5→6) taken 73458 times.
✓ Branch 1 (5→8) taken 67763 times.
|
141221 | if (ctx->stmt != NULL) { |
| 2006 | 73458 | rule_stmt_append(ctx->rule, ctx->stmt); | |
| 2007 | 73458 | ctx->stmt = NULL; | |
| 2008 | } | ||
| 2009 | 141221 | return 0; | |
| 2010 | } | ||
| 2011 | |||
| 2012 | 185494 | struct stmt *netlink_parse_set_expr(const struct set *set, | |
| 2013 | const struct nft_cache *cache, | ||
| 2014 | const struct nftnl_expr *nle) | ||
| 2015 | { | ||
| 2016 | 185494 | struct netlink_parse_ctx ctx, *pctx = &ctx; | |
| 2017 | 185494 | struct handle h = {}; | |
| 2018 | |||
| 2019 | 185494 | handle_merge(&h, &set->handle); | |
| 2020 | 185494 | pctx->rule = rule_alloc(&netlink_location, &h); | |
| 2021 | 370988 | pctx->table = table_cache_find(&cache->table_cache, | |
| 2022 | 185494 | set->handle.table.name, | |
| 2023 | 185494 | set->handle.family); | |
| 2024 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 185494 times.
|
185494 | assert(pctx->table != NULL); |
| 2025 | |||
| 2026 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 185494 times.
|
185494 | if (netlink_parse_expr(nle, pctx) < 0) |
| 2027 | ✗ | return NULL; | |
| 2028 | |||
| 2029 | 185494 | init_list_head(&pctx->rule->stmts); | |
| 2030 | 185494 | rule_free(pctx->rule); | |
| 2031 | |||
| 2032 | 185494 | return pctx->stmt; | |
| 2033 | } | ||
| 2034 | |||
| 2035 | 540 | static bool meta_outer_may_dependency_kill(struct rule_pp_ctx *ctx, | |
| 2036 | const struct expr *expr) | ||
| 2037 | { | ||
| 2038 | 540 | struct dl_proto_ctx *dl_outer = dl_proto_ctx_outer(ctx); | |
| 2039 | 540 | struct stmt *stmt = dl_outer->pdctx.pdeps[expr->payload.inner_desc->base]; | |
| 2040 | struct expr *dep; | ||
| 2041 | uint8_t l4proto; | ||
| 2042 | |||
| 2043 |
2/2✓ Branch 0 (3→4) taken 380 times.
✓ Branch 1 (3→5) taken 160 times.
|
540 | if (!stmt) |
| 2044 | 380 | return false; | |
| 2045 | |||
| 2046 | 160 | dep = stmt->expr; | |
| 2047 | |||
| 2048 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 160 times.
|
160 | if (dep->left->meta.key != NFT_META_L4PROTO) |
| 2049 | ✗ | return false; | |
| 2050 | |||
| 2051 | 160 | l4proto = mpz_get_uint8(dep->right->value); | |
| 2052 | |||
| 2053 |
1/2✓ Branch 0 (8→9) taken 160 times.
✗ Branch 1 (8→13) not taken.
|
160 | switch (l4proto) { |
| 2054 | 160 | case IPPROTO_GRE: | |
| 2055 |
2/2✓ Branch 0 (9→10) taken 90 times.
✓ Branch 1 (9→11) taken 70 times.
|
160 | if (expr->payload.inner_desc == &proto_gre || |
| 2056 |
1/2✓ Branch 0 (10→11) taken 90 times.
✗ Branch 1 (10→12) not taken.
|
90 | expr->payload.inner_desc == &proto_gretap) |
| 2057 | 160 | return true; | |
| 2058 | ✗ | break; | |
| 2059 | ✗ | default: | |
| 2060 | ✗ | break; | |
| 2061 | } | ||
| 2062 | |||
| 2063 | ✗ | return false; | |
| 2064 | } | ||
| 2065 | |||
| 2066 | static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp); | ||
| 2067 | |||
| 2068 | 4254 | static void payload_match_expand(struct rule_pp_ctx *ctx, | |
| 2069 | struct expr *expr, | ||
| 2070 | struct expr *payload) | ||
| 2071 | { | ||
| 2072 | 4254 | struct expr *left = payload, *right = expr->right, *tmp; | |
| 2073 | 4254 | struct list_head list = LIST_HEAD_INIT(list); | |
| 2074 | 4254 | struct dl_proto_ctx *dl = dl_proto_ctx(ctx); | |
| 2075 | 4254 | enum proto_bases base = left->payload.base; | |
| 2076 | 4254 | struct expr *nexpr = NULL; | |
| 2077 | struct stmt *nstmt; | ||
| 2078 | |||
| 2079 | 4254 | payload_expr_expand(&list, left, &dl->pctx); | |
| 2080 | |||
| 2081 |
2/2✓ Branch 0 (38→5) taken 4375 times.
✓ Branch 1 (38→39) taken 4254 times.
|
8629 | list_for_each_entry(left, &list, list) { |
| 2082 | 4375 | tmp = constant_expr_splice(right, left->len); | |
| 2083 | 4375 | expr_set_type(tmp, left->dtype, left->byteorder); | |
| 2084 | |||
| 2085 |
3/4✓ Branch 0 (7→8) taken 4375 times.
✗ Branch 1 (7→11) not taken.
✓ Branch 2 (8→9) taken 103 times.
✓ Branch 3 (8→11) taken 4272 times.
|
4375 | if (left->payload.tmpl && (left->len < left->payload.tmpl->len)) { |
| 2086 | 103 | mpz_lshift_ui(tmp->value, left->payload.tmpl->len - left->len); | |
| 2087 | 103 | tmp->len = left->payload.tmpl->len; | |
| 2088 | 103 | tmp = prefix_expr_alloc(&tmp->location, tmp, left->len); | |
| 2089 | } | ||
| 2090 | |||
| 2091 | 4375 | nexpr = relational_expr_alloc(&expr->location, expr->op, | |
| 2092 | left, tmp); | ||
| 2093 |
2/2✓ Branch 0 (12→13) taken 3841 times.
✓ Branch 1 (12→14) taken 534 times.
|
4375 | if (expr->op == OP_EQ) |
| 2094 | 3841 | relational_expr_pctx_update(&dl->pctx, nexpr); | |
| 2095 | |||
| 2096 | 4375 | nstmt = expr_stmt_alloc(&ctx->stmt->location, nexpr); | |
| 2097 | 4375 | list_add_tail(&nstmt->list, &ctx->stmt->list); | |
| 2098 | |||
| 2099 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 4375 times.
|
4375 | assert(left->etype == EXPR_PAYLOAD); |
| 2100 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 4375 times.
|
4375 | assert(left->payload.base); |
| 2101 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 4375 times.
|
4375 | assert(base == left->payload.base); |
| 2102 | |||
| 2103 |
2/2✓ Branch 0 (22→23) taken 380 times.
✓ Branch 1 (22→29) taken 3995 times.
|
4375 | if (expr->left->payload.inner_desc) { |
| 2104 |
2/2✓ Branch 0 (23→24) taken 20 times.
✓ Branch 1 (23→25) taken 360 times.
|
380 | if (expr->left->payload.inner_desc == expr->left->payload.desc) { |
| 2105 | 20 | nexpr->left->payload.desc = expr->left->payload.desc; | |
| 2106 | 20 | nexpr->left->payload.tmpl = expr->left->payload.tmpl; | |
| 2107 | } | ||
| 2108 | 380 | nexpr->left->payload.inner_desc = expr->left->payload.inner_desc; | |
| 2109 | |||
| 2110 |
2/2✓ Branch 0 (26→27) taken 120 times.
✓ Branch 1 (26→29) taken 260 times.
|
380 | if (meta_outer_may_dependency_kill(ctx, expr->left)) { |
| 2111 | 120 | struct dl_proto_ctx *dl_outer = dl_proto_ctx_outer(ctx); | |
| 2112 | |||
| 2113 | 120 | payload_dependency_release(&dl_outer->pdctx, expr->left->payload.inner_desc->base); | |
| 2114 | } | ||
| 2115 | } | ||
| 2116 | |||
| 2117 |
2/2✓ Branch 0 (30→31) taken 315 times.
✓ Branch 1 (30→32) taken 4060 times.
|
4375 | if (payload_is_stacked(dl->pctx.protocol[base].desc, nexpr)) |
| 2118 | 315 | base--; | |
| 2119 | |||
| 2120 | /* Remember the first payload protocol expression to | ||
| 2121 | * kill it later on if made redundant by a higher layer | ||
| 2122 | * payload expression. | ||
| 2123 | */ | ||
| 2124 | 4375 | payload_dependency_kill(&dl->pdctx, nexpr->left, | |
| 2125 | 4375 | dl->pctx.family); | |
| 2126 | 4375 | expr_set_type(tmp, nexpr->left->dtype, nexpr->byteorder); | |
| 2127 |
4/4✓ Branch 0 (34→35) taken 3841 times.
✓ Branch 1 (34→37) taken 534 times.
✓ Branch 2 (35→36) taken 903 times.
✓ Branch 3 (35→37) taken 2938 times.
|
4375 | if (expr->op == OP_EQ && left->flags & EXPR_F_PROTOCOL) |
| 2128 | 903 | payload_dependency_store(&dl->pdctx, nstmt, base); | |
| 2129 | } | ||
| 2130 | 4254 | list_del(&ctx->stmt->list); | |
| 2131 | 4254 | stmt_free(ctx->stmt); | |
| 2132 | 4254 | ctx->stmt = NULL; | |
| 2133 | 4254 | } | |
| 2134 | |||
| 2135 | 1304 | static void payload_icmp_check(struct rule_pp_ctx *rctx, struct expr *expr, const struct expr *value) | |
| 2136 | { | ||
| 2137 | 1304 | struct dl_proto_ctx *dl = dl_proto_ctx(rctx); | |
| 2138 | const struct proto_hdr_template *tmpl; | ||
| 2139 | const struct proto_desc *desc; | ||
| 2140 | uint8_t icmp_type; | ||
| 2141 | unsigned int i; | ||
| 2142 | |||
| 2143 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 1304 times.
|
1304 | assert(expr->etype == EXPR_PAYLOAD); |
| 2144 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 1304 times.
|
1304 | assert(value->etype == EXPR_VALUE); |
| 2145 | |||
| 2146 |
2/2✓ Branch 0 (7→8) taken 313 times.
✓ Branch 1 (7→9) taken 991 times.
|
1304 | if (expr->payload.base != PROTO_BASE_TRANSPORT_HDR) |
| 2147 | 313 | return; | |
| 2148 | |||
| 2149 | /* icmp(v6) type is 8 bit, if value is smaller or larger, this is not | ||
| 2150 | * a protocol dependency. | ||
| 2151 | */ | ||
| 2152 |
4/6✓ Branch 0 (9→10) taken 193 times.
✓ Branch 1 (9→12) taken 798 times.
✓ Branch 2 (10→11) taken 193 times.
✗ Branch 3 (10→12) not taken.
✗ Branch 4 (11→12) not taken.
✓ Branch 5 (11→13) taken 193 times.
|
991 | if (expr->len != 8 || value->len != 8 || dl->pctx.th_dep.icmp.type) |
| 2153 | 798 | return; | |
| 2154 | |||
| 2155 | 193 | desc = dl->pctx.protocol[expr->payload.base].desc; | |
| 2156 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 193 times.
|
193 | if (desc == NULL) |
| 2157 | ✗ | return; | |
| 2158 | |||
| 2159 | /* not icmp? ignore. */ | ||
| 2160 |
4/4✓ Branch 0 (15→16) taken 146 times.
✓ Branch 1 (15→18) taken 47 times.
✓ Branch 2 (16→17) taken 74 times.
✓ Branch 3 (16→18) taken 72 times.
|
193 | if (desc != &proto_icmp && desc != &proto_icmp6) |
| 2161 | 74 | return; | |
| 2162 | |||
| 2163 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 119 times.
|
119 | assert(desc->base == expr->payload.base); |
| 2164 | |||
| 2165 | 119 | icmp_type = mpz_get_uint8(value->value); | |
| 2166 | |||
| 2167 |
1/2✓ Branch 0 (31→22) taken 127 times.
✗ Branch 1 (31→32) not taken.
|
127 | for (i = 1; i < array_size(desc->templates); i++) { |
| 2168 | 127 | tmpl = &desc->templates[i]; | |
| 2169 | |||
| 2170 |
1/2✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 127 times.
|
127 | if (tmpl->len == 0) |
| 2171 | ✗ | return; | |
| 2172 | |||
| 2173 |
2/2✓ Branch 0 (24→25) taken 119 times.
✓ Branch 1 (24→26) taken 8 times.
|
127 | if (tmpl->offset != expr->payload.offset || |
| 2174 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 119 times.
|
119 | tmpl->len != expr->len) |
| 2175 | 8 | continue; | |
| 2176 | |||
| 2177 | /* Matches but doesn't load a protocol key -> ignore. */ | ||
| 2178 |
2/2✓ Branch 0 (27→28) taken 8 times.
✓ Branch 1 (27→29) taken 111 times.
|
119 | if (desc->protocol_key != i) |
| 2179 | 8 | return; | |
| 2180 | |||
| 2181 | 111 | expr->payload.desc = desc; | |
| 2182 | 111 | expr->payload.tmpl = tmpl; | |
| 2183 | 111 | dl->pctx.th_dep.icmp.type = icmp_type; | |
| 2184 | 111 | return; | |
| 2185 | } | ||
| 2186 | } | ||
| 2187 | |||
| 2188 | 7850 | static void payload_match_postprocess(struct rule_pp_ctx *ctx, | |
| 2189 | struct expr *expr, | ||
| 2190 | struct expr *payload) | ||
| 2191 | { | ||
| 2192 | 7850 | struct dl_proto_ctx *dl = dl_proto_ctx(ctx); | |
| 2193 | |||
| 2194 |
2/2✓ Branch 0 (3→4) taken 7573 times.
✓ Branch 1 (3→20) taken 277 times.
|
7850 | switch (expr->op) { |
| 2195 | 7573 | case OP_EQ: | |
| 2196 | case OP_NEQ: | ||
| 2197 |
2/2✓ Branch 0 (4→5) taken 4254 times.
✓ Branch 1 (4→7) taken 3319 times.
|
7573 | if (expr->right->etype == EXPR_VALUE) { |
| 2198 | 4254 | payload_match_expand(ctx, expr, payload); | |
| 2199 | 4254 | break; | |
| 2200 |
2/2✓ Branch 0 (7→8) taken 2396 times.
✓ Branch 1 (7→20) taken 923 times.
|
3319 | } else if (expr->right->etype == EXPR_SET_REF) { |
| 2201 | 2396 | struct set *set = expr->right->set; | |
| 2202 | |||
| 2203 |
2/2✓ Branch 0 (9→10) taken 1346 times.
✓ Branch 1 (9→20) taken 1050 times.
|
2396 | if (set_is_anonymous(set->flags) && |
| 2204 |
2/4✓ Branch 0 (10→11) taken 1346 times.
✗ Branch 1 (10→20) not taken.
✓ Branch 2 (14→15) taken 1346 times.
✗ Branch 3 (14→20) not taken.
|
2692 | set->init && |
| 2205 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 1346 times.
|
1346 | !list_empty(&expr_set(set->init)->expressions)) { |
| 2206 | struct expr *elem; | ||
| 2207 | |||
| 2208 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 1346 times.
|
1346 | elem = list_first_entry(&expr_set(set->init)->expressions, struct expr, list); |
| 2209 | |||
| 2210 |
1/2✓ Branch 0 (17→18) taken 1346 times.
✗ Branch 1 (17→20) not taken.
|
1346 | if (elem->etype == EXPR_SET_ELEM && |
| 2211 |
2/2✓ Branch 0 (18→19) taken 1304 times.
✓ Branch 1 (18→20) taken 42 times.
|
1346 | elem->key->etype == EXPR_VALUE) |
| 2212 | 1304 | payload_icmp_check(ctx, payload, elem->key); | |
| 2213 | } | ||
| 2214 | } | ||
| 2215 | /* Fall through */ | ||
| 2216 | default: | ||
| 2217 | 3596 | payload_expr_complete(payload, &dl->pctx); | |
| 2218 | 3596 | expr_set_type(expr->right, payload->dtype, | |
| 2219 | 3596 | payload->byteorder); | |
| 2220 | 3596 | payload_dependency_kill(&dl->pdctx, payload, dl->pctx.family); | |
| 2221 | 3596 | break; | |
| 2222 | } | ||
| 2223 | 7850 | } | |
| 2224 | |||
| 2225 | 116 | static uint8_t ether_type_to_nfproto(uint16_t l3proto) | |
| 2226 | { | ||
| 2227 |
2/3✓ Branch 0 (2→3) taken 91 times.
✓ Branch 1 (2→4) taken 25 times.
✗ Branch 2 (2→5) not taken.
|
116 | switch(l3proto) { |
| 2228 | 91 | case ETH_P_IP: | |
| 2229 | 91 | return NFPROTO_IPV4; | |
| 2230 | 25 | case ETH_P_IPV6: | |
| 2231 | 25 | return NFPROTO_IPV6; | |
| 2232 | ✗ | default: | |
| 2233 | ✗ | break; | |
| 2234 | } | ||
| 2235 | |||
| 2236 | ✗ | return NFPROTO_UNSPEC; | |
| 2237 | } | ||
| 2238 | |||
| 2239 | − | static bool __meta_dependency_may_kill(const struct expr *dep, uint8_t *nfproto) | |
| 2240 | { | ||
| 2241 | uint16_t l3proto; | ||
| 2242 | |||
| 2243 | − | switch (dep->left->etype) { | |
| 2244 | − | case EXPR_META: | |
| 2245 | − | switch (dep->left->meta.key) { | |
| 2246 | − | case NFT_META_NFPROTO: | |
| 2247 | − | *nfproto = mpz_get_uint8(dep->right->value); | |
| 2248 | − | break; | |
| 2249 | − | case NFT_META_PROTOCOL: | |
| 2250 | − | l3proto = mpz_get_uint16(dep->right->value); | |
| 2251 | − | *nfproto = ether_type_to_nfproto(l3proto); | |
| 2252 | − | break; | |
| 2253 | − | default: | |
| 2254 | − | return true; | |
| 2255 | } | ||
| 2256 | − | break; | |
| 2257 | − | case EXPR_PAYLOAD: | |
| 2258 | − | if (dep->left->payload.base != PROTO_BASE_LL_HDR) | |
| 2259 | − | return true; | |
| 2260 | |||
| 2261 | − | if (dep->left->dtype != ðertype_type) | |
| 2262 | − | return true; | |
| 2263 | |||
| 2264 | − | l3proto = mpz_get_uint16(dep->right->value); | |
| 2265 | − | *nfproto = ether_type_to_nfproto(l3proto); | |
| 2266 | − | break; | |
| 2267 | − | default: | |
| 2268 | − | return true; | |
| 2269 | } | ||
| 2270 | |||
| 2271 | − | return false; | |
| 2272 | } | ||
| 2273 | |||
| 2274 | 4 | static bool ct_may_dependency_kill(unsigned int meta_nfproto, | |
| 2275 | const struct expr *ct) | ||
| 2276 | { | ||
| 2277 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 4 times.
|
4 | assert(ct->etype == EXPR_CT); |
| 2278 | |||
| 2279 |
2/4✓ Branch 0 (4→5) taken 2 times.
✗ Branch 1 (4→10) not taken.
✗ Branch 2 (4→11) not taken.
✓ Branch 3 (4→12) taken 2 times.
|
4 | switch (ct->ct.key) { |
| 2280 | 2 | case NFT_CT_DST: | |
| 2281 | case NFT_CT_SRC: | ||
| 2282 |
1/3✓ Branch 0 (5→6) taken 2 times.
✗ Branch 1 (5→7) not taken.
✗ Branch 2 (5→8) not taken.
|
2 | switch (ct->len) { |
| 2283 | 2 | case 32: | |
| 2284 | 2 | return meta_nfproto == NFPROTO_IPV4; | |
| 2285 | ✗ | case 128: | |
| 2286 | ✗ | return meta_nfproto == NFPROTO_IPV6; | |
| 2287 | ✗ | default: | |
| 2288 | ✗ | break; | |
| 2289 | } | ||
| 2290 | ✗ | return false; | |
| 2291 | ✗ | case NFT_CT_DST_IP: | |
| 2292 | case NFT_CT_SRC_IP: | ||
| 2293 | ✗ | return meta_nfproto == NFPROTO_IPV4; | |
| 2294 | ✗ | case NFT_CT_DST_IP6: | |
| 2295 | case NFT_CT_SRC_IP6: | ||
| 2296 | ✗ | return meta_nfproto == NFPROTO_IPV6; | |
| 2297 | 2 | default: | |
| 2298 | 2 | break; | |
| 2299 | } | ||
| 2300 | |||
| 2301 | 2 | return false; | |
| 2302 | } | ||
| 2303 | |||
| 2304 | 205 | static bool meta_may_dependency_kill(uint8_t nfproto, const struct expr *meta, const struct expr *v) | |
| 2305 | { | ||
| 2306 | uint8_t l4proto; | ||
| 2307 | |||
| 2308 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 205 times.
|
205 | if (meta->meta.key != NFT_META_L4PROTO) |
| 2309 | ✗ | return true; | |
| 2310 | |||
| 2311 |
2/4✓ Branch 0 (4→5) taken 205 times.
✗ Branch 1 (4→6) not taken.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→7) taken 205 times.
|
205 | if (v->etype != EXPR_VALUE || v->len != 8) |
| 2312 | ✗ | return false; | |
| 2313 | |||
| 2314 | 205 | l4proto = mpz_get_uint8(v->value); | |
| 2315 | |||
| 2316 |
3/3✓ Branch 0 (8→9) taken 63 times.
✓ Branch 1 (8→10) taken 50 times.
✓ Branch 2 (8→11) taken 92 times.
|
205 | switch (l4proto) { |
| 2317 | 63 | case IPPROTO_ICMP: | |
| 2318 | 63 | return nfproto == NFPROTO_IPV4; | |
| 2319 | 50 | case IPPROTO_ICMPV6: | |
| 2320 | 50 | return nfproto == NFPROTO_IPV6; | |
| 2321 | 92 | default: | |
| 2322 | 92 | break; | |
| 2323 | } | ||
| 2324 | |||
| 2325 | 92 | return false; | |
| 2326 | } | ||
| 2327 | |||
| 2328 | /* We have seen a protocol key expression that restricts matching at the network | ||
| 2329 | * base, leave it in place since this is meaningful in bridge, inet and netdev | ||
| 2330 | * families. Exceptions are ICMP and ICMPv6 where this code assumes that can | ||
| 2331 | * only happen with IPv4 and IPv6. | ||
| 2332 | */ | ||
| 2333 | 225 | static bool ct_meta_may_dependency_kill(struct payload_dep_ctx *ctx, | |
| 2334 | unsigned int family, | ||
| 2335 | const struct expr *expr) | ||
| 2336 | { | ||
| 2337 | 225 | struct expr *dep = payload_dependency_get(ctx, PROTO_BASE_NETWORK_HDR); | |
| 2338 | 225 | uint8_t nfproto = NFPROTO_UNSPEC; | |
| 2339 | |||
| 2340 |
2/2✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 223 times.
|
225 | if (!dep) |
| 2341 | 2 | return true; | |
| 2342 | |||
| 2343 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 223 times.
|
223 | if (__meta_dependency_may_kill(dep, &nfproto)) |
| 2344 | ✗ | return true; | |
| 2345 | |||
| 2346 |
2/3✓ Branch 0 (8→9) taken 209 times.
✓ Branch 1 (8→10) taken 14 times.
✗ Branch 2 (8→11) not taken.
|
223 | switch (family) { |
| 2347 | 209 | case NFPROTO_INET: | |
| 2348 | case NFPROTO_NETDEV: | ||
| 2349 | case NFPROTO_BRIDGE: | ||
| 2350 | 209 | break; | |
| 2351 | 14 | case NFPROTO_IPV4: | |
| 2352 | case NFPROTO_IPV6: | ||
| 2353 | 14 | return family == nfproto; | |
| 2354 | ✗ | default: | |
| 2355 | ✗ | return true; | |
| 2356 | } | ||
| 2357 | |||
| 2358 |
2/3✓ Branch 0 (12→13) taken 205 times.
✓ Branch 1 (12→14) taken 4 times.
✗ Branch 2 (12→15) not taken.
|
209 | switch (expr->left->etype) { |
| 2359 | 205 | case EXPR_META: | |
| 2360 | 205 | return meta_may_dependency_kill(nfproto, expr->left, expr->right); | |
| 2361 | 4 | case EXPR_CT: | |
| 2362 | 4 | return ct_may_dependency_kill(nfproto, expr->left); | |
| 2363 | ✗ | default: | |
| 2364 | ✗ | break; | |
| 2365 | } | ||
| 2366 | |||
| 2367 | ✗ | return true; | |
| 2368 | } | ||
| 2369 | |||
| 2370 | 49909 | static void ct_meta_common_postprocess(struct rule_pp_ctx *ctx, | |
| 2371 | const struct expr *expr, | ||
| 2372 | enum proto_bases base) | ||
| 2373 | { | ||
| 2374 | 49909 | struct dl_proto_ctx *dl = dl_proto_ctx(ctx); | |
| 2375 | 49909 | const struct expr *left = expr->left; | |
| 2376 | 49909 | struct expr *right = expr->right; | |
| 2377 | |||
| 2378 |
3/4✓ Branch 0 (3→4) taken 49909 times.
✗ Branch 1 (3→5) not taken.
✓ Branch 2 (4→5) taken 630 times.
✓ Branch 3 (4→6) taken 49279 times.
|
49909 | if (right->etype == EXPR_SET || right->etype == EXPR_SET_REF) |
| 2379 | 630 | expr_set_type(right, left->dtype, left->byteorder); | |
| 2380 | |||
| 2381 |
2/2✓ Branch 0 (6→7) taken 49205 times.
✓ Branch 1 (6→20) taken 704 times.
|
49909 | switch (expr->op) { |
| 2382 | 49205 | case OP_EQ: | |
| 2383 |
2/2✓ Branch 0 (7→8) taken 49191 times.
✓ Branch 1 (7→21) taken 14 times.
|
49205 | if (expr->right->etype == EXPR_RANGE || |
| 2384 |
1/2✓ Branch 0 (8→9) taken 49191 times.
✗ Branch 1 (8→21) not taken.
|
49191 | expr->right->etype == EXPR_SET || |
| 2385 |
2/2✓ Branch 0 (9→10) taken 48819 times.
✓ Branch 1 (9→21) taken 372 times.
|
49191 | expr->right->etype == EXPR_SET_REF) |
| 2386 | break; | ||
| 2387 | |||
| 2388 | 48819 | relational_expr_pctx_update(&dl->pctx, expr); | |
| 2389 | |||
| 2390 |
2/2✓ Branch 0 (11→12) taken 48815 times.
✓ Branch 1 (11→19) taken 4 times.
|
48819 | if (base < PROTO_BASE_TRANSPORT_HDR) { |
| 2391 |
4/4✓ Branch 0 (13→14) taken 225 times.
✓ Branch 1 (13→17) taken 48590 times.
✓ Branch 2 (15→16) taken 117 times.
✓ Branch 3 (15→17) taken 108 times.
|
49040 | if (payload_dependency_exists(&dl->pdctx, base) && |
| 2392 | 225 | ct_meta_may_dependency_kill(&dl->pdctx, | |
| 2393 | 225 | dl->pctx.family, expr)) | |
| 2394 | 117 | payload_dependency_release(&dl->pdctx, base); | |
| 2395 | |||
| 2396 |
2/2✓ Branch 0 (17→18) taken 7473 times.
✓ Branch 1 (17→19) taken 41342 times.
|
48815 | if (left->flags & EXPR_F_PROTOCOL) |
| 2397 | 7473 | payload_dependency_store(&dl->pdctx, ctx->stmt, base); | |
| 2398 | } | ||
| 2399 | 48819 | break; | |
| 2400 | 704 | default: | |
| 2401 | 704 | break; | |
| 2402 | } | ||
| 2403 | 49909 | } | |
| 2404 | |||
| 2405 | 49667 | static void meta_match_postprocess(struct rule_pp_ctx *ctx, | |
| 2406 | const struct expr *expr) | ||
| 2407 | { | ||
| 2408 | 49667 | const struct expr *left = expr->left; | |
| 2409 | |||
| 2410 | 49667 | ct_meta_common_postprocess(ctx, expr, left->meta.base); | |
| 2411 | 49667 | } | |
| 2412 | |||
| 2413 | 242 | static void ct_match_postprocess(struct rule_pp_ctx *ctx, | |
| 2414 | const struct expr *expr) | ||
| 2415 | { | ||
| 2416 | 242 | const struct expr *left = expr->left; | |
| 2417 | |||
| 2418 | 242 | ct_meta_common_postprocess(ctx, expr, left->ct.base); | |
| 2419 | 242 | } | |
| 2420 | |||
| 2421 | /* Convert a bitmask to a prefix length */ | ||
| 2422 | 58 | static unsigned int expr_mask_to_prefix(const struct expr *expr) | |
| 2423 | { | ||
| 2424 | unsigned long n; | ||
| 2425 | |||
| 2426 | 58 | n = mpz_scan1(expr->value, 0); | |
| 2427 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 56 times.
|
58 | if (n == ULONG_MAX) |
| 2428 | 2 | return 0; | |
| 2429 | 56 | return mpz_scan0(expr->value, n + 1) - n; | |
| 2430 | } | ||
| 2431 | |||
| 2432 | /* Return true if a bitmask can be expressed as a prefix length */ | ||
| 2433 | 50 | static bool expr_mask_is_prefix(const struct expr *expr) | |
| 2434 | { | ||
| 2435 | unsigned long n1, n2; | ||
| 2436 | |||
| 2437 | 50 | n1 = mpz_scan1(expr->value, 0); | |
| 2438 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 48 times.
|
50 | if (n1 == ULONG_MAX) |
| 2439 | 2 | return true; | |
| 2440 | 48 | n2 = mpz_scan0(expr->value, n1 + 1); | |
| 2441 |
3/4✓ Branch 0 (4→5) taken 28 times.
✓ Branch 1 (4→6) taken 20 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→7) taken 28 times.
|
48 | if (n2 < expr->len || n2 == ULONG_MAX) |
| 2442 | 20 | return false; | |
| 2443 | 28 | return true; | |
| 2444 | } | ||
| 2445 | |||
| 2446 | /* Convert a series of inclusive OR expressions into a list */ | ||
| 2447 | 672 | static struct expr *binop_tree_to_list(struct expr *list, struct expr *expr) | |
| 2448 | { | ||
| 2449 |
3/4✓ Branch 0 (2→3) taken 184 times.
✓ Branch 1 (2→8) taken 488 times.
✓ Branch 2 (3→4) taken 184 times.
✗ Branch 3 (3→8) not taken.
|
672 | if (expr->etype == EXPR_BINOP && expr->op == OP_OR) { |
| 2450 |
2/2✓ Branch 0 (4→5) taken 116 times.
✓ Branch 1 (4→6) taken 68 times.
|
184 | if (list == NULL) |
| 2451 | 116 | list = list_expr_alloc(&expr->location); | |
| 2452 | 184 | list = binop_tree_to_list(list, expr->left); | |
| 2453 | 184 | list = binop_tree_to_list(list, expr->right); | |
| 2454 | } else { | ||
| 2455 |
2/2✓ Branch 0 (8→9) taken 188 times.
✓ Branch 1 (8→11) taken 300 times.
|
488 | if (list == NULL) |
| 2456 | 188 | return expr_get(expr); | |
| 2457 | 300 | list_expr_add(list, expr_get(expr)); | |
| 2458 | } | ||
| 2459 | |||
| 2460 | 484 | return list; | |
| 2461 | } | ||
| 2462 | |||
| 2463 | 1611 | static void binop_adjust_one(const struct expr *binop, struct expr *value, | |
| 2464 | unsigned int shift) | ||
| 2465 | { | ||
| 2466 | 1611 | struct expr *left = binop->left; | |
| 2467 | |||
| 2468 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1611 times.
|
1611 | assert(value->len >= binop->right->len); |
| 2469 | |||
| 2470 | 1611 | mpz_rshift_ui(value->value, shift); | |
| 2471 |
1/2✓ Branch 0 (5→6) taken 1611 times.
✗ Branch 1 (5→7) not taken.
|
1611 | switch (left->etype) { |
| 2472 | 1611 | case EXPR_PAYLOAD: | |
| 2473 | case EXPR_EXTHDR: | ||
| 2474 | 1611 | value->len = left->len; | |
| 2475 | 1611 | break; | |
| 2476 | ✗ | default: | |
| 2477 | ✗ | BUG("unknown expression type %s", expr_name(left)); | |
| 2478 | break; | ||
| 2479 | } | ||
| 2480 | 1611 | } | |
| 2481 | |||
| 2482 | 1057 | static void binop_adjust(const struct expr *binop, struct expr *right, | |
| 2483 | unsigned int shift) | ||
| 2484 | { | ||
| 2485 | struct expr *i; | ||
| 2486 | |||
| 2487 |
3/4✓ Branch 0 (2→3) taken 861 times.
✓ Branch 1 (2→4) taken 164 times.
✓ Branch 2 (2→26) taken 32 times.
✗ Branch 3 (2→29) not taken.
|
1057 | switch (right->etype) { |
| 2488 | 861 | case EXPR_VALUE: | |
| 2489 | 861 | binop_adjust_one(binop, right, shift); | |
| 2490 | 861 | break; | |
| 2491 | 164 | case EXPR_SET_REF: | |
| 2492 |
2/2✓ Branch 0 (5→6) taken 27 times.
✓ Branch 1 (5→7) taken 137 times.
|
164 | if (!set_is_anonymous(right->set->flags)) |
| 2493 | 27 | break; | |
| 2494 | |||
| 2495 |
4/6✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 137 times.
✗ Branch 2 (22→23) not taken.
✓ Branch 3 (22→24) taken 943 times.
✓ Branch 4 (24→10) taken 806 times.
✓ Branch 5 (24→25) taken 137 times.
|
943 | list_for_each_entry(i, &expr_set(right->set->init)->expressions, list) { |
| 2496 |
2/4✓ Branch 0 (10→11) taken 686 times.
✗ Branch 1 (10→12) not taken.
✓ Branch 2 (10→15) taken 120 times.
✗ Branch 3 (10→17) not taken.
|
806 | switch (i->key->etype) { |
| 2497 | 686 | case EXPR_VALUE: | |
| 2498 | 686 | binop_adjust_one(binop, i->key, shift); | |
| 2499 | 686 | break; | |
| 2500 | ✗ | case EXPR_RANGE: | |
| 2501 | ✗ | binop_adjust_one(binop, i->key->left, shift); | |
| 2502 | ✗ | binop_adjust_one(binop, i->key->right, shift); | |
| 2503 | ✗ | break; | |
| 2504 | 120 | case EXPR_SET_ELEM: | |
| 2505 | 120 | binop_adjust(binop, i->key->key, shift); | |
| 2506 | 120 | break; | |
| 2507 | ✗ | default: | |
| 2508 | ✗ | BUG("unknown expression type %s", | |
| 2509 | expr_name(i->key)); | ||
| 2510 | } | ||
| 2511 | } | ||
| 2512 | 137 | break; | |
| 2513 | 32 | case EXPR_RANGE: | |
| 2514 | 32 | binop_adjust_one(binop, right->left, shift); | |
| 2515 | 32 | binop_adjust_one(binop, right->right, shift); | |
| 2516 | 32 | break; | |
| 2517 | ✗ | default: | |
| 2518 | ✗ | BUG("unknown expression type %s", expr_name(right)); | |
| 2519 | break; | ||
| 2520 | } | ||
| 2521 | 1057 | } | |
| 2522 | |||
| 2523 | − | static bool __binop_postprocess(struct rule_pp_ctx *ctx, | |
| 2524 | struct expr *expr, | ||
| 2525 | struct expr *left, | ||
| 2526 | struct expr *mask, | ||
| 2527 | struct expr **expr_binop) | ||
| 2528 | { | ||
| 2529 | − | struct dl_proto_ctx *dl = dl_proto_ctx(ctx); | |
| 2530 | − | struct expr *binop = *expr_binop; | |
| 2531 | unsigned int shift; | ||
| 2532 | |||
| 2533 | − | assert(binop->etype == EXPR_BINOP); | |
| 2534 | |||
| 2535 | − | if ((left->etype == EXPR_PAYLOAD && | |
| 2536 | − | payload_expr_trim(left, mask, &dl->pctx, &shift)) || | |
| 2537 | − | (left->etype == EXPR_EXTHDR && | |
| 2538 | − | exthdr_find_template(left, mask, &shift))) { | |
| 2539 | − | struct expr *right = NULL; | |
| 2540 | |||
| 2541 | /* mask is implicit, binop needs to be removed. | ||
| 2542 | * | ||
| 2543 | * Fix all values of the expression according to the mask | ||
| 2544 | * and then process the payload instruction using the real | ||
| 2545 | * sizes and offsets we're interested in. | ||
| 2546 | * | ||
| 2547 | * Finally, convert the expression to 1) by replacing | ||
| 2548 | * the binop with the binop payload/exthdr expression. | ||
| 2549 | */ | ||
| 2550 | − | switch (expr->etype) { | |
| 2551 | − | case EXPR_BINOP: | |
| 2552 | case EXPR_RELATIONAL: | ||
| 2553 | − | right = expr->right; | |
| 2554 | − | binop_adjust(binop, right, shift); | |
| 2555 | − | break; | |
| 2556 | − | case EXPR_MAP: | |
| 2557 | − | right = expr->mappings; | |
| 2558 | − | binop_adjust(binop, right, shift); | |
| 2559 | − | break; | |
| 2560 | − | default: | |
| 2561 | − | break; | |
| 2562 | } | ||
| 2563 | |||
| 2564 | − | assert(binop->left == left); | |
| 2565 | − | *expr_binop = expr_get(left); | |
| 2566 | |||
| 2567 | − | if (left->etype == EXPR_PAYLOAD) | |
| 2568 | − | payload_match_postprocess(ctx, expr, left); | |
| 2569 | − | else if (left->etype == EXPR_EXTHDR && right) | |
| 2570 | − | expr_set_type(right, left->dtype, left->byteorder); | |
| 2571 | |||
| 2572 | − | expr_free(binop); | |
| 2573 | − | return true; | |
| 2574 | − | } else if (left->etype == EXPR_PAYLOAD && | |
| 2575 | − | expr->right->etype == EXPR_VALUE && | |
| 2576 | − | payload_expr_trim_force(left, mask, &shift)) { | |
| 2577 | − | mpz_rshift_ui(expr->right->value, shift); | |
| 2578 | − | *expr_binop = expr_get(left); | |
| 2579 | − | expr_free(binop); | |
| 2580 | − | return true; | |
| 2581 | } | ||
| 2582 | |||
| 2583 | − | return false; | |
| 2584 | } | ||
| 2585 | |||
| 2586 | 1099 | static bool binop_postprocess(struct rule_pp_ctx *ctx, struct expr *expr, | |
| 2587 | struct expr **expr_binop) | ||
| 2588 | { | ||
| 2589 | 1099 | struct expr *binop = *expr_binop; | |
| 2590 | 1099 | struct expr *left = binop->left; | |
| 2591 | 1099 | struct expr *mask = binop->right; | |
| 2592 | |||
| 2593 | 1099 | return __binop_postprocess(ctx, expr, left, mask, expr_binop); | |
| 2594 | } | ||
| 2595 | |||
| 2596 | 99 | static void map_binop_postprocess(struct rule_pp_ctx *ctx, struct expr *expr) | |
| 2597 | { | ||
| 2598 | 99 | struct expr *binop = expr->map; | |
| 2599 | |||
| 2600 |
2/2✓ Branch 0 (2→3) taken 14 times.
✓ Branch 1 (2→4) taken 85 times.
|
99 | if (binop->op != OP_AND) |
| 2601 | 14 | return; | |
| 2602 | |||
| 2603 |
2/2✓ Branch 0 (4→5) taken 81 times.
✓ Branch 1 (4→7) taken 4 times.
|
85 | if (binop->left->etype == EXPR_PAYLOAD && |
| 2604 |
2/2✓ Branch 0 (5→6) taken 79 times.
✓ Branch 1 (5→7) taken 2 times.
|
81 | binop->right->etype == EXPR_VALUE) |
| 2605 | 79 | binop_postprocess(ctx, expr, &expr->map); | |
| 2606 | } | ||
| 2607 | |||
| 2608 | ✗ | static bool is_shift_by_zero(const struct expr *binop) | |
| 2609 | { | ||
| 2610 | struct expr *rhs; | ||
| 2611 | |||
| 2612 | ✗ | if (binop->op != OP_RSHIFT && binop->op != OP_LSHIFT) | |
| 2613 | ✗ | return false; | |
| 2614 | |||
| 2615 | ✗ | rhs = binop->right; | |
| 2616 | ✗ | if (rhs->etype != EXPR_VALUE || rhs->len > 64) | |
| 2617 | ✗ | return false; | |
| 2618 | |||
| 2619 | ✗ | return mpz_get_uint64(rhs->value) == 0; | |
| 2620 | } | ||
| 2621 | |||
| 2622 | 1292 | static void relational_binop_postprocess(struct rule_pp_ctx *ctx, | |
| 2623 | struct expr **exprp) | ||
| 2624 | { | ||
| 2625 | 1292 | struct expr *expr = *exprp, *binop = expr->left, *right = expr->right; | |
| 2626 | |||
| 2627 |
6/6✓ Branch 0 (2→3) taken 1252 times.
✓ Branch 1 (2→19) taken 40 times.
✓ Branch 2 (3→4) taken 804 times.
✓ Branch 3 (3→5) taken 448 times.
✓ Branch 4 (4→5) taken 794 times.
✓ Branch 5 (4→19) taken 10 times.
|
1292 | if (binop->op == OP_AND && (expr->op == OP_NEQ || expr->op == OP_EQ) && |
| 2628 |
2/2✓ Branch 0 (5→6) taken 1120 times.
✓ Branch 1 (5→19) taken 122 times.
|
1242 | right->dtype->basetype && |
| 2629 |
2/2✓ Branch 0 (6→7) taken 422 times.
✓ Branch 1 (6→19) taken 698 times.
|
1120 | right->dtype->basetype->type == TYPE_BITMASK && |
| 2630 |
2/2✓ Branch 0 (7→8) taken 366 times.
✓ Branch 1 (7→19) taken 56 times.
|
422 | right->etype == EXPR_VALUE && |
| 2631 |
3/4✓ Branch 0 (8→9) taken 366 times.
✗ Branch 1 (8→19) not taken.
✓ Branch 2 (9→10) taken 286 times.
✓ Branch 3 (9→19) taken 80 times.
|
366 | !mpz_cmp_ui(right->value, 0)) { |
| 2632 | /* Flag comparison: data & flags != 0 | ||
| 2633 | * | ||
| 2634 | * Split the flags into a list of flag values and convert the | ||
| 2635 | * op to OP_EQ. | ||
| 2636 | */ | ||
| 2637 | 286 | expr_free(right); | |
| 2638 | |||
| 2639 | 286 | expr->left = expr_get(binop->left); | |
| 2640 | 286 | expr->right = binop_tree_to_list(NULL, binop->right); | |
| 2641 |
2/3✓ Branch 0 (13→14) taken 250 times.
✓ Branch 1 (13→15) taken 36 times.
✗ Branch 2 (13→16) not taken.
|
286 | switch (expr->op) { |
| 2642 | 250 | case OP_NEQ: | |
| 2643 | 250 | expr->op = OP_IMPLICIT; | |
| 2644 | 250 | break; | |
| 2645 | 36 | case OP_EQ: | |
| 2646 | 36 | expr->op = OP_NEG; | |
| 2647 | 36 | break; | |
| 2648 | ✗ | default: | |
| 2649 | ✗ | BUG("unknown operation type %d", expr->op); | |
| 2650 | } | ||
| 2651 | 286 | expr_free(binop); | |
| 2652 |
2/2✓ Branch 0 (20→21) taken 50 times.
✓ Branch 1 (20→31) taken 956 times.
|
1006 | } else if (datatype_prefix_notation(binop->left->dtype) && |
| 2653 |
4/6✓ Branch 0 (21→22) taken 50 times.
✗ Branch 1 (21→31) not taken.
✓ Branch 2 (22→23) taken 50 times.
✗ Branch 3 (22→31) not taken.
✓ Branch 4 (24→25) taken 30 times.
✓ Branch 5 (24→31) taken 20 times.
|
100 | binop->op == OP_AND && expr->right->etype == EXPR_VALUE && |
| 2654 | 50 | expr_mask_is_prefix(binop->right)) { | |
| 2655 | 30 | expr->left = expr_get(binop->left); | |
| 2656 | 30 | expr->right = prefix_expr_alloc(&expr->location, | |
| 2657 | expr_get(right), | ||
| 2658 | 30 | expr_mask_to_prefix(binop->right)); | |
| 2659 | 30 | expr_free(right); | |
| 2660 | 30 | expr_free(binop); | |
| 2661 |
2/2✓ Branch 0 (31→32) taken 936 times.
✓ Branch 1 (31→34) taken 40 times.
|
976 | } else if (binop->op == OP_AND && |
| 2662 |
2/2✓ Branch 0 (32→33) taken 820 times.
✓ Branch 1 (32→34) taken 116 times.
|
936 | binop->right->etype == EXPR_VALUE) { |
| 2663 | /* | ||
| 2664 | * This *might* be a payload match testing header fields that | ||
| 2665 | * have non byte divisible offsets and/or bit lengths. | ||
| 2666 | * | ||
| 2667 | * Thus we need to deal with two different cases. | ||
| 2668 | * | ||
| 2669 | * 1 the simple version: | ||
| 2670 | * relation | ||
| 2671 | * payload value|setlookup | ||
| 2672 | * | ||
| 2673 | * expr: relation, left: payload, right: value, e.g. tcp dport == 22. | ||
| 2674 | * | ||
| 2675 | * 2. The '&' version (this is what we're looking at now). | ||
| 2676 | * relation | ||
| 2677 | * binop value1|setlookup | ||
| 2678 | * payload value2 | ||
| 2679 | * | ||
| 2680 | * expr: relation, left: binop, right: value, e.g. | ||
| 2681 | * ip saddr 10.0.0.0/8 | ||
| 2682 | * | ||
| 2683 | * payload_expr_trim will figure out if the mask is needed to match | ||
| 2684 | * templates. | ||
| 2685 | */ | ||
| 2686 | 820 | binop_postprocess(ctx, expr, &expr->left); | |
| 2687 |
1/4✗ Branch 0 (34→35) not taken.
✓ Branch 1 (34→44) taken 156 times.
✗ Branch 2 (35→36) not taken.
✗ Branch 3 (35→44) not taken.
|
156 | } else if (binop->op == OP_RSHIFT && binop->left->op == OP_AND && |
| 2688 | ✗ | binop->right->etype == EXPR_VALUE && binop->left->right->etype == EXPR_VALUE) { | |
| 2689 | /* Handle 'ip version @s4' and similar, i.e. set lookups where the lhs needs | ||
| 2690 | * fixups to mask out unwanted bits AND a shift. | ||
| 2691 | */ | ||
| 2692 | |||
| 2693 | ✗ | binop_postprocess(ctx, binop, &binop->left); | |
| 2694 | ✗ | if (is_shift_by_zero(binop)) { | |
| 2695 | ✗ | struct expr *lhs = binop->left; | |
| 2696 | |||
| 2697 | ✗ | expr_get(lhs); | |
| 2698 | ✗ | expr_free(binop); | |
| 2699 | ✗ | expr->left = lhs; | |
| 2700 | } | ||
| 2701 | } | ||
| 2702 | 1292 | } | |
| 2703 | |||
| 2704 | 1809 | static bool payload_binop_postprocess(struct rule_pp_ctx *ctx, | |
| 2705 | struct expr **exprp) | ||
| 2706 | { | ||
| 2707 | 1809 | struct expr *expr = *exprp; | |
| 2708 | |||
| 2709 |
2/2✓ Branch 0 (2→3) taken 1687 times.
✓ Branch 1 (2→4) taken 122 times.
|
1809 | if (expr->op != OP_RSHIFT) |
| 2710 | 1687 | return false; | |
| 2711 | |||
| 2712 |
2/2✓ Branch 0 (4→5) taken 48 times.
✓ Branch 1 (4→8) taken 74 times.
|
122 | if (expr->left->etype == EXPR_UNARY) { |
| 2713 | /* | ||
| 2714 | * If the payload value was originally in a different byte-order | ||
| 2715 | * from the payload expression, there will be a byte-order | ||
| 2716 | * conversion to remove. | ||
| 2717 | */ | ||
| 2718 | 48 | struct expr *left = expr_get(expr->left->arg); | |
| 2719 | 48 | expr_free(expr->left); | |
| 2720 | 48 | expr->left = left; | |
| 2721 | } | ||
| 2722 | |||
| 2723 |
3/4✓ Branch 0 (8→9) taken 114 times.
✓ Branch 1 (8→10) taken 8 times.
✗ Branch 2 (9→10) not taken.
✓ Branch 3 (9→11) taken 114 times.
|
122 | if (expr->left->etype != EXPR_BINOP || expr->left->op != OP_AND) |
| 2724 | 8 | return false; | |
| 2725 | |||
| 2726 |
2/3✓ Branch 0 (11→12) taken 15 times.
✓ Branch 1 (11→13) taken 99 times.
✗ Branch 2 (11→14) not taken.
|
114 | switch (expr->left->left->etype) { |
| 2727 | 15 | case EXPR_EXTHDR: | |
| 2728 | 15 | break; | |
| 2729 | 99 | case EXPR_PAYLOAD: | |
| 2730 | 99 | break; | |
| 2731 | ✗ | default: | |
| 2732 | ✗ | return false; | |
| 2733 | } | ||
| 2734 | |||
| 2735 | 114 | expr_postprocess(ctx, &expr->left->left); | |
| 2736 | |||
| 2737 | 114 | expr_set_type(expr->right, &integer_type, | |
| 2738 | BYTEORDER_HOST_ENDIAN); | ||
| 2739 | 114 | expr_postprocess(ctx, &expr->right); | |
| 2740 | |||
| 2741 | 114 | binop_postprocess(ctx, expr, &expr->left); | |
| 2742 | 114 | *exprp = expr_get(expr->left); | |
| 2743 | 114 | expr_free(expr); | |
| 2744 | |||
| 2745 | 114 | return true; | |
| 2746 | } | ||
| 2747 | |||
| 2748 | 28 | static struct expr *string_wildcard_expr_alloc(struct location *loc, | |
| 2749 | const struct expr *mask, | ||
| 2750 | const struct expr *expr) | ||
| 2751 | 28 | { | |
| 2752 | 28 | unsigned int len = div_round_up(expr->len, BITS_PER_BYTE); | |
| 2753 | 28 | char data[len + 2]; | |
| 2754 | int pos; | ||
| 2755 | |||
| 2756 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 28 times.
|
28 | mpz_export_data(data, expr->value, BYTEORDER_HOST_ENDIAN, len); |
| 2757 | 28 | pos = div_round_up(expr_mask_to_prefix(mask), BITS_PER_BYTE); | |
| 2758 | 28 | data[pos] = '*'; | |
| 2759 | 28 | data[pos + 1] = '\0'; | |
| 2760 | |||
| 2761 | 56 | return constant_expr_alloc(loc, expr->dtype, BYTEORDER_HOST_ENDIAN, | |
| 2762 | 28 | expr->len + BITS_PER_BYTE, data); | |
| 2763 | } | ||
| 2764 | |||
| 2765 | /* This calculates the string length and checks if it is nul-terminated, this | ||
| 2766 | * function is quite a hack :) | ||
| 2767 | */ | ||
| 2768 | − | static bool __expr_postprocess_string(struct expr **exprp) | |
| 2769 | − | { | |
| 2770 | − | struct expr *expr = *exprp; | |
| 2771 | − | unsigned int len = div_round_up(expr->len, BITS_PER_BYTE); | |
| 2772 | − | char data[len + 1]; | |
| 2773 | |||
| 2774 | − | mpz_export_data(data, expr->value, BYTEORDER_HOST_ENDIAN, len); | |
| 2775 | |||
| 2776 | − | if (data[len - 1] != '\0') | |
| 2777 | − | return false; | |
| 2778 | |||
| 2779 | − | len = strlen(data); | |
| 2780 | − | if (len && data[len - 1] == '*') { | |
| 2781 | − | data[len - 1] = '\\'; | |
| 2782 | − | data[len] = '*'; | |
| 2783 | − | data[len + 1] = '\0'; | |
| 2784 | − | expr = constant_expr_alloc(&expr->location, expr->dtype, | |
| 2785 | BYTEORDER_HOST_ENDIAN, | ||
| 2786 | − | (len + 2) * BITS_PER_BYTE, data); | |
| 2787 | − | expr_free(*exprp); | |
| 2788 | − | *exprp = expr; | |
| 2789 | } | ||
| 2790 | |||
| 2791 | − | return true; | |
| 2792 | } | ||
| 2793 | |||
| 2794 | 42161 | static struct expr *expr_postprocess_string(struct expr *expr) | |
| 2795 | { | ||
| 2796 | struct expr *mask, *out; | ||
| 2797 | |||
| 2798 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 42161 times.
|
42161 | assert(expr_basetype(expr)->type == TYPE_STRING); |
| 2799 |
2/2✓ Branch 0 (6→7) taken 42133 times.
✓ Branch 1 (6→8) taken 28 times.
|
42161 | if (__expr_postprocess_string(&expr)) |
| 2800 | 42133 | return expr; | |
| 2801 | |||
| 2802 | 28 | mask = constant_expr_alloc(&expr->location, &integer_type, | |
| 2803 | BYTEORDER_HOST_ENDIAN, | ||
| 2804 | 28 | expr->len + BITS_PER_BYTE, NULL); | |
| 2805 | 28 | mpz_clear(mask->value); | |
| 2806 | 28 | mpz_init_bitmask(mask->value, expr->len); | |
| 2807 | 28 | out = string_wildcard_expr_alloc(&expr->location, mask, expr); | |
| 2808 | 28 | expr_free(expr); | |
| 2809 | 28 | expr_free(mask); | |
| 2810 | 28 | return out; | |
| 2811 | } | ||
| 2812 | |||
| 2813 | 56989 | static void expr_postprocess_value(struct rule_pp_ctx *ctx, struct expr **exprp) | |
| 2814 | { | ||
| 2815 |
4/4✓ Branch 0 (2→3) taken 33 times.
✓ Branch 1 (2→5) taken 56956 times.
✓ Branch 2 (3→4) taken 8 times.
✓ Branch 3 (3→5) taken 25 times.
|
56989 | bool interval = (ctx->set && ctx->set->flags & NFT_SET_INTERVAL); |
| 2816 | 56989 | struct expr *expr = *exprp; | |
| 2817 | |||
| 2818 | // FIXME | ||
| 2819 |
4/4✓ Branch 0 (6→7) taken 50810 times.
✓ Branch 1 (6→11) taken 6179 times.
✓ Branch 2 (7→8) taken 50802 times.
✓ Branch 3 (7→11) taken 8 times.
|
56989 | if (expr->byteorder == BYTEORDER_HOST_ENDIAN && !interval) |
| 2820 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 50802 times.
|
50802 | mpz_switch_byteorder(expr->value, expr->len / BITS_PER_BYTE); |
| 2821 | |||
| 2822 |
2/2✓ Branch 0 (12→13) taken 42161 times.
✓ Branch 1 (12→15) taken 14828 times.
|
56989 | if (expr_basetype(expr)->type == TYPE_STRING) |
| 2823 | 42161 | *exprp = expr_postprocess_string(expr); | |
| 2824 | |||
| 2825 | 56989 | expr = *exprp; | |
| 2826 |
2/2✓ Branch 0 (15→16) taken 53678 times.
✓ Branch 1 (15→19) taken 3311 times.
|
56989 | if (expr->dtype->basetype != NULL && |
| 2827 |
2/2✓ Branch 0 (16→17) taken 916 times.
✓ Branch 1 (16→19) taken 52762 times.
|
53678 | expr->dtype->basetype->type == TYPE_BITMASK) |
| 2828 | 916 | *exprp = bitmask_expr_to_binops(expr); | |
| 2829 | 56989 | } | |
| 2830 | |||
| 2831 | 778 | static void expr_postprocess_concat(struct rule_pp_ctx *ctx, struct expr **exprp) | |
| 2832 | { | ||
| 2833 | 778 | struct expr *i, *n, *expr = *exprp; | |
| 2834 | 778 | unsigned int type = expr->dtype->type, ntype = 0; | |
| 2835 | 778 | int off = expr->dtype->subtypes; | |
| 2836 | const struct datatype *dtype; | ||
| 2837 | 778 | LIST_HEAD(tmp); | |
| 2838 | |||
| 2839 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 778 times.
|
778 | assert(expr->etype == EXPR_CONCAT); |
| 2840 | |||
| 2841 | 778 | ctx->flags |= RULE_PP_IN_CONCATENATION; | |
| 2842 |
4/6✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 778 times.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→17) taken 2539 times.
✓ Branch 4 (17→7) taken 1761 times.
✓ Branch 5 (17→18) taken 778 times.
|
2539 | list_for_each_entry_safe(i, n, &expr_concat(expr)->expressions, list) { |
| 2843 |
2/2✓ Branch 0 (7→8) taken 852 times.
✓ Branch 1 (7→10) taken 909 times.
|
1761 | if (type) { |
| 2844 | 852 | dtype = concat_subtype_lookup(type, --off); | |
| 2845 | 852 | expr_set_type(i, dtype, dtype->byteorder); | |
| 2846 | } | ||
| 2847 | 1761 | list_del(&i->list); | |
| 2848 | 1761 | expr_postprocess(ctx, &i); | |
| 2849 | 1761 | list_add_tail(&i->list, &tmp); | |
| 2850 | |||
| 2851 | 1761 | ntype = concat_subtype_add(ntype, i->dtype->type); | |
| 2852 | } | ||
| 2853 | 778 | ctx->flags &= ~RULE_PP_IN_CONCATENATION; | |
| 2854 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 778 times.
|
778 | list_splice(&tmp, &expr_concat(expr)->expressions); |
| 2855 | 778 | __datatype_set(expr, concat_type_alloc(ntype)); | |
| 2856 | 778 | } | |
| 2857 | |||
| 2858 | 181735 | static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp) | |
| 2859 | { | ||
| 2860 | 181735 | struct dl_proto_ctx *dl = dl_proto_ctx(ctx); | |
| 2861 | 181735 | struct expr *expr = *exprp, *i; | |
| 2862 | |||
| 2863 |
14/17✓ Branch 0 (3→4) taken 952 times.
✗ Branch 1 (3→10) not taken.
✗ Branch 2 (3→13) not taken.
✓ Branch 3 (3→22) taken 778 times.
✓ Branch 4 (3→24) taken 693 times.
✓ Branch 5 (3→27) taken 1809 times.
✓ Branch 6 (3→52) taken 60620 times.
✓ Branch 7 (3→82) taken 3532 times.
✓ Branch 8 (3→90) taken 56989 times.
✓ Branch 9 (3→92) taken 559 times.
✓ Branch 10 (3→95) taken 20 times.
✓ Branch 11 (3→97) taken 273 times.
✓ Branch 12 (3→99) taken 1230 times.
✓ Branch 13 (3→101) taken 53545 times.
✓ Branch 14 (3→102) taken 38 times.
✓ Branch 15 (3→105) taken 697 times.
✗ Branch 16 (3→107) not taken.
|
181735 | switch (expr->etype) { |
| 2864 | 952 | case EXPR_MAP: | |
| 2865 |
2/2✓ Branch 0 (4→5) taken 99 times.
✓ Branch 1 (4→7) taken 853 times.
|
952 | switch (expr->map->etype) { |
| 2866 | 99 | case EXPR_BINOP: | |
| 2867 | 99 | map_binop_postprocess(ctx, expr); | |
| 2868 | 99 | break; | |
| 2869 | 853 | default: | |
| 2870 | 853 | break; | |
| 2871 | } | ||
| 2872 | |||
| 2873 | 952 | expr_postprocess(ctx, &expr->map); | |
| 2874 | 952 | expr_postprocess(ctx, &expr->mappings); | |
| 2875 | 952 | break; | |
| 2876 | ✗ | case EXPR_MAPPING: | |
| 2877 | ✗ | expr_postprocess(ctx, &expr->left); | |
| 2878 | ✗ | expr_postprocess(ctx, &expr->right); | |
| 2879 | ✗ | break; | |
| 2880 | ✗ | case EXPR_SET: | |
| 2881 | ✗ | list_for_each_entry(i, &expr_set(expr)->expressions, list) | |
| 2882 | ✗ | expr_postprocess(ctx, &i); | |
| 2883 | ✗ | break; | |
| 2884 | 778 | case EXPR_CONCAT: | |
| 2885 | 778 | expr_postprocess_concat(ctx, exprp); | |
| 2886 | 778 | break; | |
| 2887 | 693 | case EXPR_UNARY: | |
| 2888 | 693 | expr_postprocess(ctx, &expr->arg); | |
| 2889 | 693 | expr_set_type(expr, expr->arg->dtype, !expr->arg->byteorder); | |
| 2890 | 693 | break; | |
| 2891 | 1809 | case EXPR_BINOP: | |
| 2892 |
2/2✓ Branch 0 (28→29) taken 114 times.
✓ Branch 1 (28→30) taken 1695 times.
|
1809 | if (payload_binop_postprocess(ctx, exprp)) |
| 2893 | 114 | break; | |
| 2894 | |||
| 2895 | 1695 | expr_postprocess(ctx, &expr->left); | |
| 2896 |
3/3✓ Branch 0 (31→32) taken 50 times.
✓ Branch 1 (31→34) taken 1392 times.
✓ Branch 2 (31→43) taken 253 times.
|
1695 | switch (expr->op) { |
| 2897 | 50 | case OP_LSHIFT: | |
| 2898 | case OP_RSHIFT: | ||
| 2899 | 50 | expr_set_type(expr->right, &integer_type, | |
| 2900 | BYTEORDER_HOST_ENDIAN); | ||
| 2901 | 50 | break; | |
| 2902 | 1392 | case OP_AND: | |
| 2903 |
2/2✓ Branch 0 (34→35) taken 16 times.
✓ Branch 1 (34→36) taken 1376 times.
|
1392 | if (expr->right->len > expr->left->len) { |
| 2904 | 16 | expr_set_type(expr->right, expr->left->dtype, | |
| 2905 | BYTEORDER_HOST_ENDIAN); | ||
| 2906 | } else { | ||
| 2907 | 1376 | expr_set_type(expr->right, expr->left->dtype, | |
| 2908 | 1376 | expr->left->byteorder); | |
| 2909 | } | ||
| 2910 | |||
| 2911 | /* Do not process OP_AND in ordinary rule context. | ||
| 2912 | * | ||
| 2913 | * Removal needs to be performed as part of the relational | ||
| 2914 | * operation because the RHS constant might need to be adjusted | ||
| 2915 | * (shifted). | ||
| 2916 | * | ||
| 2917 | * This is different in set element context or concatenations: | ||
| 2918 | * There is no relational operation (eq, neq and so on), thus | ||
| 2919 | * it needs to be processed right away. | ||
| 2920 | */ | ||
| 2921 |
2/2✓ Branch 0 (37→38) taken 17 times.
✓ Branch 1 (37→42) taken 1375 times.
|
1392 | if ((ctx->flags & RULE_PP_REMOVE_OP_AND) && |
| 2922 |
1/2✓ Branch 0 (38→39) taken 17 times.
✗ Branch 1 (38→42) not taken.
|
17 | expr->left->etype == EXPR_PAYLOAD && |
| 2923 |
1/2✓ Branch 0 (39→40) taken 17 times.
✗ Branch 1 (39→42) not taken.
|
17 | expr->right->etype == EXPR_VALUE) { |
| 2924 | 17 | __binop_postprocess(ctx, expr, expr->left, expr->right, exprp); | |
| 2925 | 7061 | return; | |
| 2926 | } | ||
| 2927 | 1375 | break; | |
| 2928 | 253 | default: | |
| 2929 |
2/2✓ Branch 0 (43→44) taken 12 times.
✓ Branch 1 (43→45) taken 241 times.
|
253 | if (expr->right->len > expr->left->len) { |
| 2930 | 12 | expr_set_type(expr->right, expr->left->dtype, | |
| 2931 | BYTEORDER_HOST_ENDIAN); | ||
| 2932 | } else { | ||
| 2933 | 241 | expr_set_type(expr->right, expr->left->dtype, | |
| 2934 | 241 | expr->left->byteorder); | |
| 2935 | } | ||
| 2936 | } | ||
| 2937 | 1678 | expr_postprocess(ctx, &expr->right); | |
| 2938 | |||
| 2939 |
2/2✓ Branch 0 (47→48) taken 50 times.
✓ Branch 1 (47→50) taken 1628 times.
|
1678 | switch (expr->op) { |
| 2940 | 50 | case OP_LSHIFT: | |
| 2941 | case OP_RSHIFT: | ||
| 2942 | 50 | expr_set_type(expr, &xinteger_type, | |
| 2943 | BYTEORDER_HOST_ENDIAN); | ||
| 2944 | 50 | break; | |
| 2945 | 1628 | default: | |
| 2946 | 1628 | expr_set_type(expr, expr->left->dtype, | |
| 2947 | 1628 | expr->left->byteorder); | |
| 2948 | } | ||
| 2949 | |||
| 2950 | 1678 | break; | |
| 2951 | 60620 | case EXPR_RELATIONAL: | |
| 2952 |
4/4✓ Branch 0 (52→53) taken 7044 times.
✓ Branch 1 (52→54) taken 375 times.
✓ Branch 2 (52→62) taken 664 times.
✓ Branch 3 (52→69) taken 52537 times.
|
60620 | switch (expr->left->etype) { |
| 2953 | 7044 | case EXPR_PAYLOAD: | |
| 2954 | 7044 | payload_match_postprocess(ctx, expr, expr->left); | |
| 2955 | 7044 | return; | |
| 2956 | 375 | case EXPR_CONCAT: | |
| 2957 |
1/2✓ Branch 0 (54→55) taken 375 times.
✗ Branch 1 (54→60) not taken.
|
375 | if (expr->right->etype == EXPR_SET_REF) { |
| 2958 |
1/2✗ Branch 0 (55→56) not taken.
✓ Branch 1 (55→57) taken 375 times.
|
375 | assert(expr->left->dtype == &invalid_type); |
| 2959 |
1/2✗ Branch 0 (57→58) not taken.
✓ Branch 1 (57→59) taken 375 times.
|
375 | assert(expr->right->dtype != &invalid_type); |
| 2960 | |||
| 2961 | 375 | datatype_set(expr->left, expr->right->dtype); | |
| 2962 | } | ||
| 2963 | 375 | ctx->set = expr->right->set; | |
| 2964 | 375 | expr_postprocess(ctx, &expr->left); | |
| 2965 | 375 | ctx->set = NULL; | |
| 2966 | 375 | break; | |
| 2967 | 664 | case EXPR_UNARY: | |
| 2968 |
2/2✓ Branch 0 (63→64) taken 144 times.
✓ Branch 1 (63→69) taken 520 times.
|
664 | if (lhs_is_meta_hour(expr->left->arg) && |
| 2969 |
1/2✓ Branch 0 (64→65) taken 144 times.
✗ Branch 1 (64→69) not taken.
|
144 | expr->right->etype == EXPR_RANGE) { |
| 2970 | 144 | struct expr *range = expr->right; | |
| 2971 | |||
| 2972 | /* Cross-day range needs to be reversed. | ||
| 2973 | * Kernel handles time in UTC. Therefore, | ||
| 2974 | * 03:00-14:00 AEDT (Sidney, Australia) time | ||
| 2975 | * is a cross-day range. | ||
| 2976 | */ | ||
| 2977 | 144 | if (mpz_cmp(range->left->value, | |
| 2978 |
1/2✓ Branch 0 (65→66) taken 144 times.
✗ Branch 1 (65→69) not taken.
|
144 | range->right->value) <= 0 && |
| 2979 |
2/2✓ Branch 0 (66→67) taken 53 times.
✓ Branch 1 (66→69) taken 91 times.
|
144 | expr->op == OP_NEQ) { |
| 2980 | 53 | range_expr_swap_values(range); | |
| 2981 | 53 | expr->op = OP_IMPLICIT; | |
| 2982 | } | ||
| 2983 | } | ||
| 2984 | /* fallthrough */ | ||
| 2985 | default: | ||
| 2986 | 53201 | expr_postprocess(ctx, &expr->left); | |
| 2987 | 53201 | break; | |
| 2988 | } | ||
| 2989 | |||
| 2990 | 53576 | expr_set_type(expr->right, expr->left->dtype, expr->left->byteorder); | |
| 2991 | 53576 | expr_postprocess(ctx, &expr->right); | |
| 2992 | |||
| 2993 |
4/4✓ Branch 0 (73→74) taken 242 times.
✓ Branch 1 (73→76) taken 49667 times.
✓ Branch 2 (73→78) taken 1292 times.
✓ Branch 3 (73→80) taken 2375 times.
|
53576 | switch (expr->left->etype) { |
| 2994 | 242 | case EXPR_CT: | |
| 2995 | 242 | ct_match_postprocess(ctx, expr); | |
| 2996 | 242 | break; | |
| 2997 | 49667 | case EXPR_META: | |
| 2998 | 49667 | meta_match_postprocess(ctx, expr); | |
| 2999 | 49667 | break; | |
| 3000 | 1292 | case EXPR_BINOP: | |
| 3001 | 1292 | relational_binop_postprocess(ctx, exprp); | |
| 3002 | 1292 | break; | |
| 3003 | 2375 | default: | |
| 3004 | 2375 | break; | |
| 3005 | } | ||
| 3006 | 53576 | break; | |
| 3007 | 3532 | case EXPR_PAYLOAD: | |
| 3008 | 3532 | payload_expr_complete(expr, &dl->pctx); | |
| 3009 |
2/2✓ Branch 0 (83→84) taken 160 times.
✓ Branch 1 (83→88) taken 3372 times.
|
3532 | if (expr->payload.inner_desc) { |
| 3010 |
2/2✓ Branch 0 (85→86) taken 40 times.
✓ Branch 1 (85→88) taken 120 times.
|
160 | if (meta_outer_may_dependency_kill(ctx, expr)) { |
| 3011 | 40 | struct dl_proto_ctx *dl_outer = dl_proto_ctx_outer(ctx); | |
| 3012 | |||
| 3013 | 40 | payload_dependency_release(&dl_outer->pdctx, expr->payload.inner_desc->base); | |
| 3014 | } | ||
| 3015 | } | ||
| 3016 | 3532 | payload_dependency_kill(&dl->pdctx, expr, dl->pctx.family); | |
| 3017 | 3532 | break; | |
| 3018 | 56989 | case EXPR_VALUE: | |
| 3019 | 56989 | expr_postprocess_value(ctx, exprp); | |
| 3020 | 56989 | break; | |
| 3021 | 559 | case EXPR_RANGE: | |
| 3022 | 559 | expr_postprocess(ctx, &expr->left); | |
| 3023 | 559 | expr_postprocess(ctx, &expr->right); | |
| 3024 | 559 | break; | |
| 3025 | 20 | case EXPR_PREFIX: | |
| 3026 | 20 | expr_postprocess(ctx, &expr->prefix); | |
| 3027 | 20 | break; | |
| 3028 | 273 | case EXPR_SET_ELEM: | |
| 3029 | 273 | ctx->flags |= RULE_PP_IN_SET_ELEM; | |
| 3030 | 273 | expr_postprocess(ctx, &expr->key); | |
| 3031 | 273 | ctx->flags &= ~RULE_PP_IN_SET_ELEM; | |
| 3032 | 273 | break; | |
| 3033 | 1230 | case EXPR_EXTHDR: | |
| 3034 | 1230 | exthdr_dependency_kill(&dl->pdctx, expr, dl->pctx.family); | |
| 3035 | 1230 | break; | |
| 3036 | 53545 | case EXPR_SET_REF: | |
| 3037 | case EXPR_META: | ||
| 3038 | case EXPR_RT: | ||
| 3039 | case EXPR_VERDICT: | ||
| 3040 | case EXPR_NUMGEN: | ||
| 3041 | case EXPR_FIB: | ||
| 3042 | case EXPR_SOCKET: | ||
| 3043 | case EXPR_TUNNEL: | ||
| 3044 | case EXPR_OSF: | ||
| 3045 | case EXPR_XFRM: | ||
| 3046 | 53545 | break; | |
| 3047 | 38 | case EXPR_HASH: | |
| 3048 |
2/2✓ Branch 0 (102→103) taken 22 times.
✓ Branch 1 (102→104) taken 16 times.
|
38 | if (expr->hash.expr) |
| 3049 | 22 | expr_postprocess(ctx, &expr->hash.expr); | |
| 3050 | 38 | break; | |
| 3051 | 697 | case EXPR_CT: | |
| 3052 | 697 | ct_expr_update_type(&dl->pctx, expr); | |
| 3053 | 697 | break; | |
| 3054 | ✗ | default: | |
| 3055 | ✗ | BUG("unknown expression type %s", expr_name(expr)); | |
| 3056 | } | ||
| 3057 | } | ||
| 3058 | |||
| 3059 | 226 | static void stmt_reject_postprocess(struct rule_pp_ctx *rctx) | |
| 3060 | { | ||
| 3061 | 226 | struct dl_proto_ctx *dl = dl_proto_ctx(rctx); | |
| 3062 | const struct proto_desc *desc, *base; | ||
| 3063 | 226 | struct stmt *stmt = rctx->stmt; | |
| 3064 | int protocol; | ||
| 3065 | |||
| 3066 |
3/4✓ Branch 0 (3→4) taken 24 times.
✓ Branch 1 (3→10) taken 22 times.
✓ Branch 2 (3→16) taken 180 times.
✗ Branch 3 (3→30) not taken.
|
226 | switch (dl->pctx.family) { |
| 3067 | 24 | case NFPROTO_IPV4: | |
| 3068 | 24 | stmt->reject.family = dl->pctx.family; | |
| 3069 | 24 | datatype_set(stmt->reject.expr, &reject_icmp_code_type); | |
| 3070 |
4/4✓ Branch 0 (5→6) taken 4 times.
✓ Branch 1 (5→9) taken 20 times.
✓ Branch 2 (7→8) taken 2 times.
✓ Branch 3 (7→9) taken 2 times.
|
28 | if (stmt->reject.type == NFT_REJECT_TCP_RST && |
| 3071 | 4 | payload_dependency_exists(&dl->pdctx, | |
| 3072 | PROTO_BASE_TRANSPORT_HDR)) | ||
| 3073 | 2 | payload_dependency_release(&dl->pdctx, | |
| 3074 | PROTO_BASE_TRANSPORT_HDR); | ||
| 3075 | 24 | break; | |
| 3076 | 22 | case NFPROTO_IPV6: | |
| 3077 | 22 | stmt->reject.family = dl->pctx.family; | |
| 3078 | 22 | datatype_set(stmt->reject.expr, &reject_icmpv6_code_type); | |
| 3079 |
4/4✓ Branch 0 (11→12) taken 4 times.
✓ Branch 1 (11→15) taken 18 times.
✓ Branch 2 (13→14) taken 2 times.
✓ Branch 3 (13→15) taken 2 times.
|
26 | if (stmt->reject.type == NFT_REJECT_TCP_RST && |
| 3080 | 4 | payload_dependency_exists(&dl->pdctx, | |
| 3081 | PROTO_BASE_TRANSPORT_HDR)) | ||
| 3082 | 2 | payload_dependency_release(&dl->pdctx, | |
| 3083 | PROTO_BASE_TRANSPORT_HDR); | ||
| 3084 | 22 | break; | |
| 3085 | 180 | case NFPROTO_INET: | |
| 3086 | case NFPROTO_BRIDGE: | ||
| 3087 | case NFPROTO_NETDEV: | ||
| 3088 |
2/2✓ Branch 0 (16→17) taken 65 times.
✓ Branch 1 (16→19) taken 115 times.
|
180 | if (stmt->reject.type == NFT_REJECT_ICMPX_UNREACH) { |
| 3089 | 65 | datatype_set(stmt->reject.expr, &reject_icmpx_code_type); | |
| 3090 | 65 | break; | |
| 3091 | } | ||
| 3092 | |||
| 3093 | /* always print full icmp(6) name, simple 'reject' might be ambiguious | ||
| 3094 | * because ipv4 vs. ipv6 info might be lost | ||
| 3095 | */ | ||
| 3096 | 115 | stmt->reject.verbose_print = 1; | |
| 3097 | |||
| 3098 | 115 | base = dl->pctx.protocol[PROTO_BASE_LL_HDR].desc; | |
| 3099 | 115 | desc = dl->pctx.protocol[PROTO_BASE_NETWORK_HDR].desc; | |
| 3100 | 115 | protocol = proto_find_num(base, desc); | |
| 3101 |
3/3✓ Branch 0 (20→21) taken 60 times.
✓ Branch 1 (20→23) taken 47 times.
✓ Branch 2 (20→25) taken 8 times.
|
115 | switch (protocol) { |
| 3102 | 60 | case NFPROTO_IPV4: /* INET */ | |
| 3103 | case __constant_htons(ETH_P_IP): /* BRIDGE, NETDEV */ | ||
| 3104 | 60 | stmt->reject.family = NFPROTO_IPV4; | |
| 3105 | 60 | datatype_set(stmt->reject.expr, &reject_icmp_code_type); | |
| 3106 | 60 | break; | |
| 3107 | 47 | case NFPROTO_IPV6: /* INET */ | |
| 3108 | case __constant_htons(ETH_P_IPV6): /* BRIDGE, NETDEV */ | ||
| 3109 | 47 | stmt->reject.family = NFPROTO_IPV6; | |
| 3110 | 47 | datatype_set(stmt->reject.expr, &reject_icmpv6_code_type); | |
| 3111 | 47 | break; | |
| 3112 | 8 | default: | |
| 3113 | 8 | break; | |
| 3114 | } | ||
| 3115 | |||
| 3116 |
2/2✓ Branch 0 (27→28) taken 90 times.
✓ Branch 1 (27→29) taken 25 times.
|
115 | if (payload_dependency_exists(&dl->pdctx, PROTO_BASE_NETWORK_HDR)) |
| 3117 | 90 | payload_dependency_release(&dl->pdctx, | |
| 3118 | PROTO_BASE_NETWORK_HDR); | ||
| 3119 | 115 | break; | |
| 3120 | ✗ | default: | |
| 3121 | ✗ | break; | |
| 3122 | } | ||
| 3123 | 226 | } | |
| 3124 | |||
| 3125 | 453 | static bool expr_may_merge_range(struct expr *expr, struct expr *prev, | |
| 3126 | enum ops *op) | ||
| 3127 | { | ||
| 3128 | struct expr *left, *prev_left; | ||
| 3129 | |||
| 3130 |
1/2✓ Branch 0 (2→3) taken 453 times.
✗ Branch 1 (2→17) not taken.
|
453 | if (prev->etype == EXPR_RELATIONAL && |
| 3131 |
2/2✓ Branch 0 (3→4) taken 445 times.
✓ Branch 1 (3→17) taken 8 times.
|
453 | expr->etype == EXPR_RELATIONAL) { |
| 3132 | /* ct and meta needs an unary to swap byteorder, in this case | ||
| 3133 | * we have to explore the inner branch in this tree. | ||
| 3134 | */ | ||
| 3135 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 445 times.
|
445 | if (expr->left->etype == EXPR_UNARY) |
| 3136 | ✗ | left = expr->left->arg; | |
| 3137 | else | ||
| 3138 | 445 | left = expr->left; | |
| 3139 | |||
| 3140 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 445 times.
|
445 | if (prev->left->etype == EXPR_UNARY) |
| 3141 | ✗ | prev_left = prev->left->arg; | |
| 3142 | else | ||
| 3143 | 445 | prev_left = prev->left; | |
| 3144 | |||
| 3145 |
2/2✓ Branch 0 (10→11) taken 355 times.
✓ Branch 1 (10→17) taken 90 times.
|
445 | if (left->etype == prev_left->etype) { |
| 3146 |
1/4✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→14) taken 355 times.
✗ Branch 2 (12→13) not taken.
✗ Branch 3 (12→14) not taken.
|
355 | if (expr->op == OP_LTE && prev->op == OP_GTE) { |
| 3147 | ✗ | *op = OP_EQ; | |
| 3148 | ✗ | return true; | |
| 3149 |
3/4✓ Branch 0 (14→15) taken 6 times.
✓ Branch 1 (14→17) taken 349 times.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→17) taken 6 times.
|
355 | } else if (expr->op == OP_GT && prev->op == OP_LT) { |
| 3150 | ✗ | *op = OP_NEQ; | |
| 3151 | ✗ | return true; | |
| 3152 | } | ||
| 3153 | } | ||
| 3154 | } | ||
| 3155 | |||
| 3156 | 453 | return false; | |
| 3157 | } | ||
| 3158 | |||
| 3159 | ✗ | static void expr_postprocess_range(struct rule_pp_ctx *ctx, enum ops op) | |
| 3160 | { | ||
| 3161 | ✗ | struct dl_proto_ctx *dl = dl_proto_ctx(ctx); | |
| 3162 | ✗ | struct stmt *nstmt, *stmt = ctx->stmt; | |
| 3163 | struct expr *nexpr, *rel; | ||
| 3164 | |||
| 3165 | ✗ | nexpr = range_expr_alloc(&dl->pdctx.prev->location, | |
| 3166 | ✗ | expr_clone(dl->pdctx.prev->expr->right), | |
| 3167 | ✗ | expr_clone(stmt->expr->right)); | |
| 3168 | ✗ | expr_set_type(nexpr, stmt->expr->right->dtype, | |
| 3169 | ✗ | stmt->expr->right->byteorder); | |
| 3170 | |||
| 3171 | ✗ | rel = relational_expr_alloc(&dl->pdctx.prev->location, op, | |
| 3172 | ✗ | expr_clone(stmt->expr->left), nexpr); | |
| 3173 | |||
| 3174 | ✗ | nstmt = expr_stmt_alloc(&stmt->location, rel); | |
| 3175 | ✗ | list_add_tail(&nstmt->list, &stmt->list); | |
| 3176 | |||
| 3177 | ✗ | list_del(&dl->pdctx.prev->list); | |
| 3178 | ✗ | stmt_free(dl->pdctx.prev); | |
| 3179 | |||
| 3180 | ✗ | list_del(&stmt->list); | |
| 3181 | ✗ | stmt_free(stmt); | |
| 3182 | ✗ | ctx->stmt = nstmt; | |
| 3183 | ✗ | } | |
| 3184 | |||
| 3185 | 61203 | static void stmt_expr_postprocess(struct rule_pp_ctx *ctx) | |
| 3186 | { | ||
| 3187 | 61203 | struct dl_proto_ctx *dl = dl_proto_ctx(ctx); | |
| 3188 | enum ops op; | ||
| 3189 | |||
| 3190 | 61203 | expr_postprocess(ctx, &ctx->stmt->expr); | |
| 3191 | |||
| 3192 |
4/4✓ Branch 0 (4→5) taken 556 times.
✓ Branch 1 (4→10) taken 60647 times.
✓ Branch 2 (5→6) taken 453 times.
✓ Branch 3 (5→10) taken 103 times.
|
61203 | if (dl->pdctx.prev && ctx->stmt && |
| 3193 |
2/4✓ Branch 0 (6→7) taken 453 times.
✗ Branch 1 (6→10) not taken.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→10) taken 453 times.
|
906 | ctx->stmt->type == dl->pdctx.prev->type && |
| 3194 | 453 | expr_may_merge_range(ctx->stmt->expr, dl->pdctx.prev->expr, &op)) | |
| 3195 | ✗ | expr_postprocess_range(ctx, op); | |
| 3196 | 61203 | } | |
| 3197 | |||
| 3198 | 104 | static void stmt_payload_binop_pp(struct rule_pp_ctx *ctx, struct expr *binop) | |
| 3199 | { | ||
| 3200 | 104 | struct dl_proto_ctx *dl = dl_proto_ctx(ctx); | |
| 3201 | 104 | struct expr *payload = binop->left; | |
| 3202 | 104 | struct expr *mask = binop->right; | |
| 3203 | unsigned int shift; | ||
| 3204 | |||
| 3205 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 104 times.
|
104 | assert(payload->etype == EXPR_PAYLOAD); |
| 3206 |
2/2✓ Branch 0 (6→7) taken 26 times.
✓ Branch 1 (6→10) taken 78 times.
|
104 | if (payload_expr_trim(payload, mask, &dl->pctx, &shift)) { |
| 3207 | 26 | binop_adjust(binop, mask, shift); | |
| 3208 | 26 | payload_expr_complete(payload, &dl->pctx); | |
| 3209 | 26 | expr_set_type(mask, payload->dtype, | |
| 3210 | 26 | payload->byteorder); | |
| 3211 | } | ||
| 3212 | 104 | } | |
| 3213 | |||
| 3214 | /** | ||
| 3215 | * stmt_payload_binop_postprocess - decode payload set binop | ||
| 3216 | * | ||
| 3217 | * @ctx: rule postprocessing context | ||
| 3218 | * | ||
| 3219 | * This helper has to be called if expr_postprocess() failed to | ||
| 3220 | * decode the payload operation. | ||
| 3221 | * | ||
| 3222 | * Usually a failure to decode means that userspace had to munge | ||
| 3223 | * the original payload expression because it has an odd size or | ||
| 3224 | * a non-byte divisible offset/length. | ||
| 3225 | * | ||
| 3226 | * If that was the case, the 'value' expression is not a value but | ||
| 3227 | * a binop expression with a munged payload expression on the left | ||
| 3228 | * and a mask to clear the real payload offset/length. | ||
| 3229 | * | ||
| 3230 | * So chech if we have one of the following binops: | ||
| 3231 | * I) | ||
| 3232 | * binop (|) | ||
| 3233 | * binop(&) value/set | ||
| 3234 | * payload value(mask) | ||
| 3235 | * | ||
| 3236 | * This is the normal case, the | RHS is the value the user wants | ||
| 3237 | * to set, the & RHS is the mask value that discards bits we need | ||
| 3238 | * to clear but retains everything unrelated to the set operation. | ||
| 3239 | * | ||
| 3240 | * IIa) | ||
| 3241 | * binop (&) | ||
| 3242 | * payload mask | ||
| 3243 | * | ||
| 3244 | * User specified a zero set value -- netlink bitwise decoding | ||
| 3245 | * discarded the redundant "| 0" part. This is identical to I), | ||
| 3246 | * we can just set value to 0 after we inferred the real payload size. | ||
| 3247 | * | ||
| 3248 | * IIb) | ||
| 3249 | * binop (|) | ||
| 3250 | * payload value/set | ||
| 3251 | * | ||
| 3252 | * This happens when user wants to set all bits, netlink bitwise | ||
| 3253 | * decoding changed '(payload & mask) ^ bits_to_set' into | ||
| 3254 | * 'payload | bits_to_set', discarding the redundant "& 0xfff...". | ||
| 3255 | */ | ||
| 3256 | 270 | static void stmt_payload_binop_postprocess(struct rule_pp_ctx *ctx, | |
| 3257 | const struct proto_ctx *pctx) | ||
| 3258 | { | ||
| 3259 | struct expr *expr, *binop, *payload, *value, *mask; | ||
| 3260 | 270 | struct stmt *stmt = ctx->stmt; | |
| 3261 | mpz_t bitmask; | ||
| 3262 | |||
| 3263 | 270 | expr = stmt->payload.val; | |
| 3264 | |||
| 3265 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 270 times.
|
270 | if (expr->etype != EXPR_BINOP) |
| 3266 | 140 | return; | |
| 3267 | |||
| 3268 |
2/3✓ Branch 0 (4→5) taken 86 times.
✓ Branch 1 (4→43) taken 184 times.
✗ Branch 2 (4→88) not taken.
|
270 | switch (expr->left->etype) { |
| 3269 | 86 | case EXPR_BINOP: {/* I? */ | |
| 3270 | 86 | unsigned int shift = 0; | |
| 3271 | mpz_t tmp; | ||
| 3272 | |||
| 3273 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 86 times.
|
86 | if (expr->op != OP_OR) |
| 3274 | ✗ | return; | |
| 3275 | |||
| 3276 | 86 | value = expr->right; | |
| 3277 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 86 times.
|
86 | if (value->etype != EXPR_VALUE) |
| 3278 | ✗ | return; | |
| 3279 | |||
| 3280 | 86 | binop = expr->left; | |
| 3281 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 86 times.
|
86 | if (binop->op != OP_AND) |
| 3282 | ✗ | return; | |
| 3283 | |||
| 3284 | 86 | payload = binop->left; | |
| 3285 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 86 times.
|
86 | if (payload->etype != EXPR_PAYLOAD) |
| 3286 | ✗ | return; | |
| 3287 | |||
| 3288 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 86 times.
|
86 | if (!payload_expr_cmp(stmt->payload.expr, payload)) |
| 3289 | ✗ | return; | |
| 3290 | |||
| 3291 | 86 | mask = binop->right; | |
| 3292 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 86 times.
|
86 | if (mask->etype != EXPR_VALUE) |
| 3293 | ✗ | return; | |
| 3294 | |||
| 3295 | 86 | mpz_init(tmp); | |
| 3296 | 86 | mpz_set(tmp, mask->value); | |
| 3297 | |||
| 3298 | 86 | mpz_init_bitmask(bitmask, payload->len); | |
| 3299 | 86 | mpz_xor(bitmask, bitmask, mask->value); | |
| 3300 | 86 | mpz_xor(bitmask, bitmask, value->value); | |
| 3301 | 86 | mpz_set(mask->value, bitmask); | |
| 3302 | 86 | mpz_clear(bitmask); | |
| 3303 | |||
| 3304 |
1/2✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→34) taken 86 times.
|
86 | if (!binop_postprocess(ctx, expr, &expr->left) && |
| 3305 | ✗ | !payload_is_known(payload) && | |
| 3306 | ✗ | !payload_expr_trim_force(payload, | |
| 3307 | mask, &shift)) { | ||
| 3308 | ✗ | mpz_set(mask->value, tmp); | |
| 3309 | ✗ | mpz_clear(tmp); | |
| 3310 | ✗ | return; | |
| 3311 | } | ||
| 3312 | |||
| 3313 |
1/2✗ Branch 0 (34→35) not taken.
✓ Branch 1 (34→36) taken 86 times.
|
86 | if (shift) |
| 3314 | ✗ | mpz_rshift_ui(value->value, shift); | |
| 3315 | |||
| 3316 | 86 | mpz_clear(tmp); | |
| 3317 | 86 | expr_free(stmt->payload.expr); | |
| 3318 | 86 | stmt->payload.expr = expr_get(payload); | |
| 3319 | 86 | stmt->payload.val = expr_get(expr->right); | |
| 3320 | 86 | expr_free(expr); | |
| 3321 | 86 | break; | |
| 3322 | } | ||
| 3323 | 184 | case EXPR_PAYLOAD: /* II? */ | |
| 3324 | 184 | payload = expr->left; | |
| 3325 | 184 | mask = expr->right; | |
| 3326 | |||
| 3327 |
1/2✗ Branch 0 (43→44) not taken.
✓ Branch 1 (43→45) taken 184 times.
|
184 | if (mask->etype != EXPR_VALUE) |
| 3328 | ✗ | return; | |
| 3329 | |||
| 3330 |
1/2✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→48) taken 184 times.
|
184 | if (!payload_expr_cmp(stmt->payload.expr, payload)) |
| 3331 | ✗ | return; | |
| 3332 | |||
| 3333 |
2/4✓ Branch 0 (48→49) taken 102 times.
✓ Branch 1 (48→70) taken 82 times.
✗ Branch 2 (48→78) not taken.
✗ Branch 3 (48→82) not taken.
|
184 | switch (expr->op) { |
| 3334 | 102 | case OP_AND: { /* IIa */ | |
| 3335 | unsigned int shift_unused; | ||
| 3336 | mpz_t tmp; | ||
| 3337 | |||
| 3338 |
2/2✓ Branch 0 (50→51) taken 80 times.
✓ Branch 1 (50→52) taken 22 times.
|
102 | if (stmt_payload_expr_trim(stmt, pctx)) |
| 3339 | 80 | return; | |
| 3340 | |||
| 3341 | 22 | mpz_init(tmp); | |
| 3342 | 22 | mpz_set(tmp, mask->value); | |
| 3343 | |||
| 3344 | 22 | mpz_init_bitmask(bitmask, payload->len); | |
| 3345 | 22 | mpz_xor(bitmask, bitmask, mask->value); | |
| 3346 | 22 | mpz_set(mask->value, bitmask); | |
| 3347 | 22 | mpz_clear(bitmask); | |
| 3348 | |||
| 3349 | 22 | stmt_payload_binop_pp(ctx, expr); | |
| 3350 |
2/2✓ Branch 0 (60→61) taken 18 times.
✓ Branch 1 (60→66) taken 4 times.
|
22 | if (!payload_is_known(expr->left) && |
| 3351 |
1/2✗ Branch 0 (62→63) not taken.
✓ Branch 1 (62→66) taken 18 times.
|
18 | !payload_expr_trim_force(expr->left, mask, &shift_unused)) { |
| 3352 | ✗ | mpz_set(mask->value, tmp); | |
| 3353 | ✗ | mpz_clear(tmp); | |
| 3354 | ✗ | return; | |
| 3355 | } | ||
| 3356 | |||
| 3357 | 22 | mpz_clear(tmp); | |
| 3358 | |||
| 3359 | /* Mask was used to match payload, i.e. user asked to | ||
| 3360 | * clear the payload expression. | ||
| 3361 | * The "mask" value becomes new stmt->payload.value | ||
| 3362 | * so set this to 0. | ||
| 3363 | * Also the reason why &shift_unused is ignored. | ||
| 3364 | */ | ||
| 3365 | 22 | mpz_set_ui(mask->value, 0); | |
| 3366 | 22 | break; | |
| 3367 | } | ||
| 3368 | 82 | case OP_OR: /* IIb */ | |
| 3369 | 82 | stmt_payload_binop_pp(ctx, expr); | |
| 3370 |
2/2✓ Branch 0 (72→73) taken 60 times.
✓ Branch 1 (72→74) taken 22 times.
|
82 | if (stmt_payload_expr_trim(stmt, pctx)) |
| 3371 | 60 | return; | |
| 3372 |
1/2✗ Branch 0 (75→76) not taken.
✓ Branch 1 (75→77) taken 22 times.
|
22 | if (!payload_is_known(expr->left)) |
| 3373 | ✗ | return; | |
| 3374 | 22 | break; | |
| 3375 | ✗ | case OP_XOR: | |
| 3376 | ✗ | if (stmt_payload_expr_trim(stmt, pctx)) | |
| 3377 | ✗ | return; | |
| 3378 | |||
| 3379 | ✗ | return; | |
| 3380 | ✗ | default: /* No idea what to do */ | |
| 3381 | ✗ | return; | |
| 3382 | } | ||
| 3383 | |||
| 3384 | 44 | expr_free(stmt->payload.expr); | |
| 3385 | 44 | stmt->payload.expr = expr_get(expr->left); | |
| 3386 | 44 | stmt->payload.val = expr_get(expr->right); | |
| 3387 | 44 | expr_free(expr); | |
| 3388 | 44 | break; | |
| 3389 | ✗ | default: /* No idea */ | |
| 3390 | ✗ | break; | |
| 3391 | } | ||
| 3392 | } | ||
| 3393 | |||
| 3394 | 398 | static void stmt_payload_postprocess(struct rule_pp_ctx *ctx) | |
| 3395 | { | ||
| 3396 | 398 | struct dl_proto_ctx *dl = dl_proto_ctx(ctx); | |
| 3397 | 398 | struct stmt *stmt = ctx->stmt; | |
| 3398 | |||
| 3399 | 398 | payload_expr_complete(stmt->payload.expr, &dl->pctx); | |
| 3400 |
2/2✓ Branch 0 (5→6) taken 270 times.
✓ Branch 1 (5→7) taken 128 times.
|
398 | if (!payload_is_known(stmt->payload.expr)) |
| 3401 | 270 | stmt_payload_binop_postprocess(ctx, &dl->pctx); | |
| 3402 | |||
| 3403 | 398 | expr_postprocess(ctx, &stmt->payload.expr); | |
| 3404 | |||
| 3405 | 398 | expr_set_type(stmt->payload.val, | |
| 3406 | 398 | stmt->payload.expr->dtype, | |
| 3407 | 398 | stmt->payload.expr->byteorder); | |
| 3408 | |||
| 3409 | 398 | expr_postprocess(ctx, &stmt->payload.val); | |
| 3410 | 398 | } | |
| 3411 | |||
| 3412 | 167 | static void stmt_queue_postprocess(struct rule_pp_ctx *ctx) | |
| 3413 | { | ||
| 3414 | 167 | struct stmt *stmt = ctx->stmt; | |
| 3415 | 167 | struct expr *e = stmt->queue.queue; | |
| 3416 | |||
| 3417 |
3/4✓ Branch 0 (2→3) taken 167 times.
✗ Branch 1 (2→5) not taken.
✓ Branch 2 (3→4) taken 117 times.
✓ Branch 3 (3→5) taken 50 times.
|
167 | if (e == NULL || e->etype == EXPR_VALUE || |
| 3418 |
2/2✓ Branch 0 (4→5) taken 75 times.
✓ Branch 1 (4→6) taken 42 times.
|
117 | e->etype == EXPR_RANGE) |
| 3419 | 125 | return; | |
| 3420 | |||
| 3421 | 42 | expr_postprocess(ctx, &stmt->queue.queue); | |
| 3422 | } | ||
| 3423 | |||
| 3424 | /* | ||
| 3425 | * We can only remove payload dependencies if they occur without | ||
| 3426 | * a statement with side effects in between. | ||
| 3427 | * | ||
| 3428 | * For instance: | ||
| 3429 | * 'ip protocol tcp tcp dport 22 counter' is same as | ||
| 3430 | * 'tcp dport 22 counter'. | ||
| 3431 | * | ||
| 3432 | * 'ip protocol tcp counter tcp dport 22' cannot be written as | ||
| 3433 | * 'counter tcp dport 22' (that would be counter ip protocol tcp, but | ||
| 3434 | * that counts every packet, not just ip/tcp). | ||
| 3435 | */ | ||
| 3436 | static void | ||
| 3437 | 73887 | rule_maybe_reset_payload_deps(struct payload_dep_ctx *pdctx, enum stmt_types t) | |
| 3438 | { | ||
| 3439 |
2/2✓ Branch 0 (2→3) taken 61203 times.
✓ Branch 1 (2→4) taken 12684 times.
|
73887 | if (t == STMT_EXPRESSION) |
| 3440 | 61203 | return; | |
| 3441 | |||
| 3442 | 12684 | payload_dependency_reset(pdctx); | |
| 3443 | } | ||
| 3444 | |||
| 3445 | 64397 | static bool has_inner_desc(const struct expr *expr) | |
| 3446 | { | ||
| 3447 | struct expr *i; | ||
| 3448 | |||
| 3449 |
6/6✓ Branch 0 (2→3) taken 1413 times.
✓ Branch 1 (2→4) taken 614 times.
✓ Branch 2 (2→15) taken 50008 times.
✓ Branch 3 (2→16) taken 9446 times.
✓ Branch 4 (2→17) taken 212 times.
✓ Branch 5 (2→18) taken 2704 times.
|
64397 | switch (expr->etype) { |
| 3450 | 1413 | case EXPR_BINOP: | |
| 3451 | 1413 | return has_inner_desc(expr->left); | |
| 3452 | 614 | case EXPR_CONCAT: | |
| 3453 |
4/6✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 614 times.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→13) taken 1927 times.
✓ Branch 4 (13→7) taken 1357 times.
✓ Branch 5 (13→14) taken 570 times.
|
1927 | list_for_each_entry(i, &expr_concat(expr)->expressions, list) { |
| 3454 |
2/2✓ Branch 0 (8→9) taken 44 times.
✓ Branch 1 (8→10) taken 1313 times.
|
1357 | if (has_inner_desc(i)) |
| 3455 | 44 | return true; | |
| 3456 | } | ||
| 3457 | 570 | break; | |
| 3458 | 50008 | case EXPR_META: | |
| 3459 | 50008 | return expr->meta.inner_desc; | |
| 3460 | 9446 | case EXPR_PAYLOAD: | |
| 3461 | 9446 | return expr->payload.inner_desc; | |
| 3462 | 212 | case EXPR_SET_ELEM: | |
| 3463 | 212 | return has_inner_desc(expr->key); | |
| 3464 | 2704 | default: | |
| 3465 | 2704 | break; | |
| 3466 | } | ||
| 3467 | |||
| 3468 | 3274 | return false; | |
| 3469 | } | ||
| 3470 | |||
| 3471 | 73887 | static struct dl_proto_ctx *rule_update_dl_proto_ctx(struct rule_pp_ctx *rctx) | |
| 3472 | { | ||
| 3473 | 73887 | const struct stmt *stmt = rctx->stmt; | |
| 3474 | 73887 | bool inner = false; | |
| 3475 | |||
| 3476 |
3/3✓ Branch 0 (2→3) taken 61203 times.
✓ Branch 1 (2→7) taken 212 times.
✓ Branch 2 (2→11) taken 12472 times.
|
73887 | switch (stmt->type) { |
| 3477 | 61203 | case STMT_EXPRESSION: | |
| 3478 |
2/2✓ Branch 0 (4→5) taken 720 times.
✓ Branch 1 (4→6) taken 60483 times.
|
61203 | if (has_inner_desc(stmt->expr->left)) |
| 3479 | 720 | inner = true; | |
| 3480 | 61203 | break; | |
| 3481 | 212 | case STMT_SET: | |
| 3482 |
2/2✓ Branch 0 (8→9) taken 2 times.
✓ Branch 1 (8→10) taken 210 times.
|
212 | if (has_inner_desc(stmt->set.key)) |
| 3483 | 2 | inner = true; | |
| 3484 | 212 | break; | |
| 3485 | 12472 | default: | |
| 3486 | 12472 | break; | |
| 3487 | } | ||
| 3488 | |||
| 3489 |
2/2✓ Branch 0 (12→13) taken 722 times.
✓ Branch 1 (12→14) taken 73165 times.
|
73887 | if (inner) |
| 3490 | 722 | rctx->dl = &rctx->_dl[1]; | |
| 3491 | else | ||
| 3492 | 73165 | rctx->dl = &rctx->_dl[0]; | |
| 3493 | |||
| 3494 | 73887 | return rctx->dl; | |
| 3495 | } | ||
| 3496 | |||
| 3497 | 59661 | static void rule_parse_postprocess(struct netlink_parse_ctx *ctx, struct rule *rule) | |
| 3498 | { | ||
| 3499 | struct stmt *stmt, *next; | ||
| 3500 | struct dl_proto_ctx *dl; | ||
| 3501 | struct rule_pp_ctx rctx; | ||
| 3502 | struct expr *expr; | ||
| 3503 | |||
| 3504 | 59661 | memset(&rctx, 0, sizeof(rctx)); | |
| 3505 | 59661 | proto_ctx_init(&rctx._dl[0].pctx, rule->handle.family, ctx->debug_mask, false); | |
| 3506 | /* use NFPROTO_BRIDGE to set up proto_eth as base protocol. */ | ||
| 3507 | 59661 | proto_ctx_init(&rctx._dl[1].pctx, NFPROTO_BRIDGE, ctx->debug_mask, true); | |
| 3508 | |||
| 3509 |
2/2✓ Branch 0 (60→5) taken 73887 times.
✓ Branch 1 (60→61) taken 59661 times.
|
133548 | list_for_each_entry_safe(stmt, next, &rule->stmts, list) { |
| 3510 | 73887 | enum stmt_types type = stmt->type; | |
| 3511 | |||
| 3512 | 73887 | rctx.stmt = stmt; | |
| 3513 | 73887 | dl = rule_update_dl_proto_ctx(&rctx); | |
| 3514 | |||
| 3515 |
14/16✓ Branch 0 (6→7) taken 61203 times.
✓ Branch 1 (6→9) taken 398 times.
✗ Branch 2 (6→11) not taken.
✓ Branch 3 (6→13) taken 317 times.
✓ Branch 4 (6→16) taken 211 times.
✓ Branch 5 (6→24) taken 419 times.
✓ Branch 6 (6→29) taken 49 times.
✓ Branch 7 (6→35) taken 226 times.
✓ Branch 8 (6→37) taken 212 times.
✓ Branch 9 (6→39) taken 61 times.
✓ Branch 10 (6→42) taken 23 times.
✓ Branch 11 (6→47) taken 13 times.
✗ Branch 12 (6→51) not taken.
✓ Branch 13 (6→53) taken 1853 times.
✓ Branch 14 (6→55) taken 167 times.
✓ Branch 15 (6→57) taken 8735 times.
|
73887 | switch (type) { |
| 3516 | 61203 | case STMT_EXPRESSION: | |
| 3517 | 61203 | stmt_expr_postprocess(&rctx); | |
| 3518 | 61203 | break; | |
| 3519 | 398 | case STMT_PAYLOAD: | |
| 3520 | 398 | stmt_payload_postprocess(&rctx); | |
| 3521 | 398 | break; | |
| 3522 | ✗ | case STMT_METER: | |
| 3523 | ✗ | expr_postprocess(&rctx, &stmt->meter.key); | |
| 3524 | ✗ | break; | |
| 3525 | 317 | case STMT_META: | |
| 3526 |
1/2✓ Branch 0 (13→14) taken 317 times.
✗ Branch 1 (13→15) not taken.
|
317 | if (stmt->meta.expr != NULL) |
| 3527 | 317 | expr_postprocess(&rctx, &stmt->meta.expr); | |
| 3528 | 317 | break; | |
| 3529 | 211 | case STMT_CT: | |
| 3530 |
1/2✓ Branch 0 (16→17) taken 211 times.
✗ Branch 1 (16→23) not taken.
|
211 | if (stmt->ct.expr != NULL) { |
| 3531 | 211 | expr_postprocess(&rctx, &stmt->ct.expr); | |
| 3532 | |||
| 3533 |
2/2✓ Branch 0 (18→19) taken 88 times.
✓ Branch 1 (18→23) taken 123 times.
|
211 | if (stmt->ct.expr->etype == EXPR_BINOP && |
| 3534 |
2/2✓ Branch 0 (19→20) taken 18 times.
✓ Branch 1 (19→23) taken 70 times.
|
88 | stmt->ct.key == NFT_CT_EVENTMASK) { |
| 3535 | 18 | expr = binop_tree_to_list(NULL, stmt->ct.expr); | |
| 3536 | 18 | expr_free(stmt->ct.expr); | |
| 3537 | 18 | stmt->ct.expr = expr; | |
| 3538 | } | ||
| 3539 | } | ||
| 3540 | 211 | break; | |
| 3541 | 419 | case STMT_NAT: | |
| 3542 |
2/2✓ Branch 0 (24→25) taken 227 times.
✓ Branch 1 (24→26) taken 192 times.
|
419 | if (stmt->nat.addr != NULL) |
| 3543 | 227 | expr_postprocess(&rctx, &stmt->nat.addr); | |
| 3544 |
2/2✓ Branch 0 (26→27) taken 93 times.
✓ Branch 1 (26→28) taken 326 times.
|
419 | if (stmt->nat.proto != NULL) |
| 3545 | 93 | expr_postprocess(&rctx, &stmt->nat.proto); | |
| 3546 | 419 | break; | |
| 3547 | 49 | case STMT_TPROXY: | |
| 3548 |
2/2✓ Branch 0 (29→30) taken 27 times.
✓ Branch 1 (29→31) taken 22 times.
|
49 | if (stmt->tproxy.addr) |
| 3549 | 27 | expr_postprocess(&rctx, &stmt->tproxy.addr); | |
| 3550 |
2/2✓ Branch 0 (31→32) taken 37 times.
✓ Branch 1 (31→34) taken 12 times.
|
49 | if (stmt->tproxy.port) { |
| 3551 | 37 | payload_dependency_reset(&dl->pdctx); | |
| 3552 | 37 | expr_postprocess(&rctx, &stmt->tproxy.port); | |
| 3553 | } | ||
| 3554 | 49 | break; | |
| 3555 | 226 | case STMT_REJECT: | |
| 3556 | 226 | stmt_reject_postprocess(&rctx); | |
| 3557 | 226 | break; | |
| 3558 | 212 | case STMT_SET: | |
| 3559 | 212 | expr_postprocess(&rctx, &stmt->set.key); | |
| 3560 | 212 | break; | |
| 3561 | 61 | case STMT_MAP: | |
| 3562 | 61 | expr_postprocess(&rctx, &stmt->map.key); | |
| 3563 | 61 | expr_postprocess(&rctx, &stmt->map.data); | |
| 3564 | 61 | break; | |
| 3565 | 23 | case STMT_DUP: | |
| 3566 |
1/2✓ Branch 0 (42→43) taken 23 times.
✗ Branch 1 (42→44) not taken.
|
23 | if (stmt->dup.to != NULL) |
| 3567 | 23 | expr_postprocess(&rctx, &stmt->dup.to); | |
| 3568 |
2/2✓ Branch 0 (44→45) taken 10 times.
✓ Branch 1 (44→46) taken 13 times.
|
23 | if (stmt->dup.dev != NULL) |
| 3569 | 10 | expr_postprocess(&rctx, &stmt->dup.dev); | |
| 3570 | 23 | break; | |
| 3571 | 13 | case STMT_FWD: | |
| 3572 | 13 | expr_postprocess(&rctx, &stmt->fwd.dev); | |
| 3573 |
2/2✓ Branch 0 (48→49) taken 5 times.
✓ Branch 1 (48→50) taken 8 times.
|
13 | if (stmt->fwd.addr != NULL) |
| 3574 | 5 | expr_postprocess(&rctx, &stmt->fwd.addr); | |
| 3575 | 13 | break; | |
| 3576 | ✗ | case STMT_XT: | |
| 3577 | ✗ | stmt_xt_postprocess(&rctx, stmt, rule); | |
| 3578 | ✗ | break; | |
| 3579 | 1853 | case STMT_OBJREF: | |
| 3580 | 1853 | expr_postprocess(&rctx, &stmt->objref.expr); | |
| 3581 | 1853 | break; | |
| 3582 | 167 | case STMT_QUEUE: | |
| 3583 | 167 | stmt_queue_postprocess(&rctx); | |
| 3584 | 167 | break; | |
| 3585 | 8735 | default: | |
| 3586 | 8735 | break; | |
| 3587 | } | ||
| 3588 | |||
| 3589 | 73887 | dl->pdctx.prev = rctx.stmt; | |
| 3590 | |||
| 3591 | 73887 | rule_maybe_reset_payload_deps(&dl->pdctx, type); | |
| 3592 | } | ||
| 3593 | 59661 | } | |
| 3594 | |||
| 3595 | 3149 | static int parse_rule_udata_cb(const struct nftnl_udata *attr, void *data) | |
| 3596 | { | ||
| 3597 | 3149 | unsigned char *value = nftnl_udata_get(attr); | |
| 3598 | 3149 | uint8_t type = nftnl_udata_type(attr); | |
| 3599 | 3149 | uint8_t len = nftnl_udata_len(attr); | |
| 3600 | 3149 | const struct nftnl_udata **tb = data; | |
| 3601 | |||
| 3602 |
1/2✓ Branch 0 (5→6) taken 3149 times.
✗ Branch 1 (5→9) not taken.
|
3149 | switch (type) { |
| 3603 | 3149 | case NFTNL_UDATA_RULE_COMMENT: | |
| 3604 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 3149 times.
|
3149 | if (value[len - 1] != '\0') |
| 3605 | ✗ | return -1; | |
| 3606 | 3149 | break; | |
| 3607 | ✗ | default: | |
| 3608 | ✗ | return 0; | |
| 3609 | } | ||
| 3610 | 3149 | tb[type] = attr; | |
| 3611 | 3149 | return 0; | |
| 3612 | } | ||
| 3613 | |||
| 3614 | 59661 | static char *nftnl_rule_get_comment(const struct nftnl_rule *nlr) | |
| 3615 | { | ||
| 3616 | 59661 | const struct nftnl_udata *tb[NFTNL_UDATA_RULE_MAX + 1] = {}; | |
| 3617 | const void *data; | ||
| 3618 | uint32_t len; | ||
| 3619 | |||
| 3620 |
2/2✓ Branch 0 (3→4) taken 56512 times.
✓ Branch 1 (3→5) taken 3149 times.
|
59661 | if (!nftnl_rule_is_set(nlr, NFTNL_RULE_USERDATA)) |
| 3621 | 56512 | return NULL; | |
| 3622 | |||
| 3623 | 3149 | data = nftnl_rule_get_data(nlr, NFTNL_RULE_USERDATA, &len); | |
| 3624 | |||
| 3625 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 3149 times.
|
3149 | if (nftnl_udata_parse(data, len, parse_rule_udata_cb, tb) < 0) |
| 3626 | ✗ | return NULL; | |
| 3627 | |||
| 3628 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 3149 times.
|
3149 | if (!tb[NFTNL_UDATA_RULE_COMMENT]) |
| 3629 | ✗ | return NULL; | |
| 3630 | |||
| 3631 | 3149 | return xstrdup(nftnl_udata_get(tb[NFTNL_UDATA_RULE_COMMENT])); | |
| 3632 | } | ||
| 3633 | |||
| 3634 | 59661 | struct rule *netlink_delinearize_rule(struct netlink_ctx *ctx, | |
| 3635 | struct nftnl_rule *nlr) | ||
| 3636 | { | ||
| 3637 | 59661 | struct netlink_parse_ctx _ctx, *pctx = &_ctx; | |
| 3638 | struct handle h; | ||
| 3639 | |||
| 3640 | 59661 | memset(&_ctx, 0, sizeof(_ctx)); | |
| 3641 | 59661 | _ctx.msgs = ctx->msgs; | |
| 3642 | 59661 | _ctx.debug_mask = ctx->nft->debug_mask; | |
| 3643 | 59661 | _ctx.nlctx = ctx; | |
| 3644 | |||
| 3645 | 59661 | memset(&h, 0, sizeof(h)); | |
| 3646 | 59661 | h.family = nftnl_rule_get_u32(nlr, NFTNL_RULE_FAMILY); | |
| 3647 | 59661 | h.table.name = xstrdup(nftnl_rule_get_str(nlr, NFTNL_RULE_TABLE)); | |
| 3648 | 59661 | h.chain.name = xstrdup(nftnl_rule_get_str(nlr, NFTNL_RULE_CHAIN)); | |
| 3649 | 59661 | h.handle.id = nftnl_rule_get_u64(nlr, NFTNL_RULE_HANDLE); | |
| 3650 | |||
| 3651 |
2/2✓ Branch 0 (9→10) taken 45606 times.
✓ Branch 1 (9→12) taken 14055 times.
|
59661 | if (nftnl_rule_is_set(nlr, NFTNL_RULE_POSITION)) |
| 3652 | 45606 | h.position.id = nftnl_rule_get_u64(nlr, NFTNL_RULE_POSITION); | |
| 3653 | |||
| 3654 | 59661 | pctx->rule = rule_alloc(&netlink_location, &h); | |
| 3655 | 59661 | pctx->table = table_cache_find(&ctx->nft->cache.table_cache, | |
| 3656 | h.table.name, h.family); | ||
| 3657 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 59661 times.
|
59661 | if (!pctx->table) { |
| 3658 | ✗ | errno = ENOENT; | |
| 3659 | ✗ | return NULL; | |
| 3660 | } | ||
| 3661 | |||
| 3662 | 59661 | pctx->rule->comment = nftnl_rule_get_comment(nlr); | |
| 3663 | |||
| 3664 | 59661 | nftnl_expr_foreach(nlr, netlink_parse_rule_expr, pctx); | |
| 3665 | |||
| 3666 | 59661 | rule_parse_postprocess(pctx, pctx->rule); | |
| 3667 | 59661 | netlink_release_registers(pctx); | |
| 3668 | 59661 | return pctx->rule; | |
| 3669 | } | ||
| 3670 |