| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 as | ||
| 6 | * published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <nft.h> | ||
| 12 | |||
| 13 | #include <stddef.h> | ||
| 14 | #include <stdio.h> | ||
| 15 | #include <limits.h> | ||
| 16 | |||
| 17 | #include <expression.h> | ||
| 18 | #include <statement.h> | ||
| 19 | #include <datatype.h> | ||
| 20 | #include <netlink.h> | ||
| 21 | #include <rule.h> | ||
| 22 | #include <gmputil.h> | ||
| 23 | #include <utils.h> | ||
| 24 | #include <list.h> | ||
| 25 | #include <erec.h> | ||
| 26 | #include <json.h> | ||
| 27 | |||
| 28 | extern const struct expr_ops ct_expr_ops; | ||
| 29 | extern const struct expr_ops fib_expr_ops; | ||
| 30 | extern const struct expr_ops hash_expr_ops; | ||
| 31 | extern const struct expr_ops inner_expr_ops; | ||
| 32 | extern const struct expr_ops meta_expr_ops; | ||
| 33 | extern const struct expr_ops numgen_expr_ops; | ||
| 34 | extern const struct expr_ops osf_expr_ops; | ||
| 35 | extern const struct expr_ops payload_expr_ops; | ||
| 36 | extern const struct expr_ops rt_expr_ops; | ||
| 37 | extern const struct expr_ops socket_expr_ops; | ||
| 38 | extern const struct expr_ops xfrm_expr_ops; | ||
| 39 | |||
| 40 | 27284803 | struct expr *expr_alloc(const struct location *loc, enum expr_types etype, | |
| 41 | const struct datatype *dtype, enum byteorder byteorder, | ||
| 42 | unsigned int len) | ||
| 43 | { | ||
| 44 | struct expr *expr; | ||
| 45 | |||
| 46 | 27284803 | expr = xzalloc(sizeof(*expr)); | |
| 47 | 27284803 | expr->location = *loc; | |
| 48 | 27284803 | expr->dtype = datatype_get(dtype); | |
| 49 | 27284803 | expr->etype = etype; | |
| 50 | 27284803 | expr->byteorder = byteorder; | |
| 51 | 27284803 | expr->len = len; | |
| 52 | 27284803 | expr->refcnt = 1; | |
| 53 | 27284803 | init_list_head(&expr->list); | |
| 54 | 27284803 | return expr; | |
| 55 | } | ||
| 56 | |||
| 57 | 539941 | struct expr *expr_clone(const struct expr *expr) | |
| 58 | { | ||
| 59 | struct expr *new; | ||
| 60 | |||
| 61 | 539941 | new = expr_alloc(&expr->location, expr->etype, | |
| 62 | 539941 | expr->dtype, expr->byteorder, expr->len); | |
| 63 | 539941 | new->flags = expr->flags; | |
| 64 | 539941 | new->op = expr->op; | |
| 65 | 539941 | expr_ops(expr)->clone(new, expr); | |
| 66 | 539941 | return new; | |
| 67 | } | ||
| 68 | |||
| 69 | 3268474 | struct expr *expr_get(struct expr *expr) | |
| 70 | { | ||
| 71 |
2/4✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 3268474 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 3268474 times.
|
3268474 | assert_refcount_safe(expr->refcnt); |
| 72 | 3268474 | expr->refcnt++; | |
| 73 | 3268474 | return expr; | |
| 74 | } | ||
| 75 | |||
| 76 | 27284712 | static void expr_destroy(struct expr *e) | |
| 77 | { | ||
| 78 | 27284712 | const struct expr_ops *ops = expr_ops(e); | |
| 79 | |||
| 80 |
2/2✓ Branch 0 (3→4) taken 27014980 times.
✓ Branch 1 (3→5) taken 269732 times.
|
27284712 | if (ops->destroy) |
| 81 | 27014980 | ops->destroy(e); | |
| 82 | 27284712 | } | |
| 83 | |||
| 84 | 31776678 | void expr_free(struct expr *expr) | |
| 85 | { | ||
| 86 |
2/2✓ Branch 0 (2→3) taken 1223492 times.
✓ Branch 1 (2→4) taken 30553186 times.
|
31776678 | if (expr == NULL) |
| 87 | 1223492 | return; | |
| 88 | |||
| 89 |
2/4✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 30553186 times.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→10) taken 30553186 times.
|
30553186 | assert_refcount_safe(expr->refcnt); |
| 90 |
2/2✓ Branch 0 (10→11) taken 3268474 times.
✓ Branch 1 (10→12) taken 27284712 times.
|
30553186 | if (--expr->refcnt > 0) |
| 91 | 3268474 | return; | |
| 92 | |||
| 93 | 27284712 | datatype_free(expr->dtype); | |
| 94 | |||
| 95 | /* EXPR_INVALID expressions lack ->ops structure. | ||
| 96 | * This happens for set, list and concat types. | ||
| 97 | */ | ||
| 98 |
1/2✓ Branch 0 (13→14) taken 27284712 times.
✗ Branch 1 (13→15) not taken.
|
27284712 | if (expr->etype != EXPR_INVALID) |
| 99 | 27284712 | expr_destroy(expr); | |
| 100 | 27284712 | free(expr); | |
| 101 | } | ||
| 102 | |||
| 103 | 763455 | void expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 104 | { | ||
| 105 | 763455 | const struct expr_ops *ops = expr_ops(expr); | |
| 106 | |||
| 107 |
1/2✓ Branch 0 (3→4) taken 763455 times.
✗ Branch 1 (3→5) not taken.
|
763455 | if (ops->print) |
| 108 | 763455 | ops->print(expr, octx); | |
| 109 | 763455 | } | |
| 110 | |||
| 111 | ✗ | bool expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 112 | { | ||
| 113 | ✗ | assert(e1->flags & EXPR_F_SINGLETON); | |
| 114 | ✗ | assert(e2->flags & EXPR_F_SINGLETON); | |
| 115 | |||
| 116 | ✗ | if (e1->etype != e2->etype) | |
| 117 | ✗ | return false; | |
| 118 | |||
| 119 | ✗ | return expr_ops(e1)->cmp(e1, e2); | |
| 120 | } | ||
| 121 | |||
| 122 | 108 | const char *expr_name(const struct expr *e) | |
| 123 | { | ||
| 124 | 108 | return expr_ops(e)->name; | |
| 125 | } | ||
| 126 | |||
| 127 | ✗ | void expr_describe(const struct expr *expr, struct output_ctx *octx) | |
| 128 | { | ||
| 129 | ✗ | const struct datatype *dtype = expr->dtype, *edtype = NULL; | |
| 130 | ✗ | unsigned int len = expr->len; | |
| 131 | ✗ | const char *delim = ""; | |
| 132 | |||
| 133 | ✗ | if (dtype == &invalid_type && | |
| 134 | ✗ | expr->etype == EXPR_SYMBOL) | |
| 135 | ✗ | edtype = datatype_lookup_byname(expr->identifier); | |
| 136 | |||
| 137 | ✗ | if (edtype) { | |
| 138 | ✗ | dtype = edtype; | |
| 139 | ✗ | nft_print(octx, "datatype %s (%s)", | |
| 140 | ✗ | dtype->name, dtype->desc); | |
| 141 | ✗ | len = dtype->size; | |
| 142 | } else { | ||
| 143 | ✗ | nft_print(octx, "%s expression, datatype %s (%s)", | |
| 144 | ✗ | expr_name(expr), dtype->name, dtype->desc); | |
| 145 | |||
| 146 | ✗ | if (dtype == &invalid_type) { | |
| 147 | ✗ | nft_print(octx, "\n"); | |
| 148 | ✗ | return; | |
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | ✗ | if (dtype->basetype != NULL) { | |
| 153 | ✗ | nft_print(octx, " (basetype "); | |
| 154 | ✗ | for (dtype = dtype->basetype; dtype != NULL; | |
| 155 | ✗ | dtype = dtype->basetype) { | |
| 156 | ✗ | nft_print(octx, "%s%s", delim, dtype->desc); | |
| 157 | ✗ | delim = ", "; | |
| 158 | } | ||
| 159 | ✗ | nft_print(octx, ")"); | |
| 160 | } | ||
| 161 | |||
| 162 | ✗ | if (expr_basetype(expr)->type == TYPE_STRING) { | |
| 163 | ✗ | if (len) | |
| 164 | ✗ | nft_print(octx, ", %u characters", | |
| 165 | len / BITS_PER_BYTE); | ||
| 166 | else | ||
| 167 | ✗ | nft_print(octx, ", dynamic length"); | |
| 168 | } else | ||
| 169 | ✗ | nft_print(octx, ", %u bits", len); | |
| 170 | |||
| 171 | ✗ | if (!edtype) | |
| 172 | ✗ | edtype = expr->dtype; | |
| 173 | |||
| 174 | ✗ | nft_print(octx, "\n"); | |
| 175 | |||
| 176 | ✗ | if (edtype->sym_tbl != NULL) { | |
| 177 | ✗ | nft_print(octx, "\npre-defined symbolic constants "); | |
| 178 | ✗ | if (edtype->sym_tbl->base == BASE_DECIMAL) | |
| 179 | ✗ | nft_print(octx, "(in decimal):\n"); | |
| 180 | else | ||
| 181 | ✗ | nft_print(octx, "(in hexadecimal):\n"); | |
| 182 | ✗ | symbol_table_print(edtype->sym_tbl, edtype, | |
| 183 | ✗ | expr->byteorder, octx); | |
| 184 | ✗ | } else if (edtype->describe) { | |
| 185 | ✗ | edtype->describe(octx); | |
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | 104236 | void expr_set_type(struct expr *expr, const struct datatype *dtype, | |
| 190 | enum byteorder byteorder) | ||
| 191 | { | ||
| 192 | 104236 | const struct expr_ops *ops = expr_ops(expr); | |
| 193 | |||
| 194 |
2/2✓ Branch 0 (3→4) taken 21728 times.
✓ Branch 1 (3→5) taken 82508 times.
|
104236 | if (ops->set_type) |
| 195 | 21728 | ops->set_type(expr, dtype, byteorder); | |
| 196 | else { | ||
| 197 | 82508 | datatype_set(expr, dtype); | |
| 198 | 82508 | expr->byteorder = byteorder; | |
| 199 | } | ||
| 200 | 104236 | } | |
| 201 | |||
| 202 | 10037326 | const struct datatype *expr_basetype(const struct expr *expr) | |
| 203 | { | ||
| 204 | 10037326 | const struct datatype *type = expr->dtype; | |
| 205 | |||
| 206 |
2/2✓ Branch 0 (4→3) taken 9375899 times.
✓ Branch 1 (4→5) taken 10037326 times.
|
19413225 | while (type->basetype != NULL) |
| 207 | 9375899 | type = type->basetype; | |
| 208 | 10037326 | return type; | |
| 209 | } | ||
| 210 | |||
| 211 | 289 | int __fmtstring(4, 5) expr_binary_error(struct list_head *msgs, | |
| 212 | const struct expr *e1, const struct expr *e2, | ||
| 213 | const char *fmt, ...) | ||
| 214 | { | ||
| 215 | struct error_record *erec; | ||
| 216 | va_list ap; | ||
| 217 | |||
| 218 | 289 | va_start(ap, fmt); | |
| 219 | 289 | erec = erec_vcreate(EREC_ERROR, &e1->location, fmt, ap); | |
| 220 |
2/2✓ Branch 0 (3→4) taken 33 times.
✓ Branch 1 (3→5) taken 256 times.
|
289 | if (e2 != NULL) |
| 221 | 33 | erec_add_location(erec, &e2->location); | |
| 222 | 289 | va_end(ap); | |
| 223 | 289 | erec_queue(erec, msgs); | |
| 224 | 289 | return -1; | |
| 225 | } | ||
| 226 | |||
| 227 | 5373 | static void verdict_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 228 | { | ||
| 229 | 5373 | datatype_print(expr, octx); | |
| 230 | 5373 | } | |
| 231 | |||
| 232 | ✗ | static bool verdict_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 233 | { | ||
| 234 | ✗ | if (e1->verdict != e2->verdict) | |
| 235 | ✗ | return false; | |
| 236 | |||
| 237 | ✗ | if ((e1->verdict == NFT_JUMP || | |
| 238 | ✗ | e1->verdict == NFT_GOTO) && | |
| 239 | ✗ | expr_cmp(e1->chain, e2->chain)) | |
| 240 | ✗ | return true; | |
| 241 | |||
| 242 | ✗ | return false; | |
| 243 | } | ||
| 244 | |||
| 245 | 1709 | static void verdict_expr_clone(struct expr *new, const struct expr *expr) | |
| 246 | { | ||
| 247 | 1709 | new->verdict = expr->verdict; | |
| 248 |
2/2✓ Branch 0 (2→3) taken 1453 times.
✓ Branch 1 (2→5) taken 256 times.
|
1709 | if (expr->chain != NULL) |
| 249 | 1453 | new->chain = expr_clone(expr->chain); | |
| 250 | 1709 | } | |
| 251 | |||
| 252 | 168690 | static void verdict_expr_destroy(struct expr *expr) | |
| 253 | { | ||
| 254 | 168690 | expr_free(expr->chain); | |
| 255 | 168690 | } | |
| 256 | |||
| 257 | 782 | static int verdict_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 258 | const struct expr *expr) | ||
| 259 | { | ||
| 260 | 782 | return 0; | |
| 261 | } | ||
| 262 | |||
| 263 | 2333 | static struct expr *verdict_expr_parse_udata(const struct nftnl_udata *attr) | |
| 264 | { | ||
| 265 | struct expr *e; | ||
| 266 | |||
| 267 | 2333 | e = symbol_expr_alloc(&internal_location, SYMBOL_VALUE, NULL, "verdict"); | |
| 268 | 2333 | e->dtype = &verdict_type; | |
| 269 | 2333 | e->len = NFT_REG_SIZE * BITS_PER_BYTE; | |
| 270 | 2333 | return e; | |
| 271 | } | ||
| 272 | |||
| 273 | static const struct expr_ops verdict_expr_ops = { | ||
| 274 | .type = EXPR_VERDICT, | ||
| 275 | .name = "verdict", | ||
| 276 | .print = verdict_expr_print, | ||
| 277 | .json = verdict_expr_json, | ||
| 278 | .cmp = verdict_expr_cmp, | ||
| 279 | .clone = verdict_expr_clone, | ||
| 280 | .destroy = verdict_expr_destroy, | ||
| 281 | .build_udata = verdict_expr_build_udata, | ||
| 282 | .parse_udata = verdict_expr_parse_udata, | ||
| 283 | }; | ||
| 284 | |||
| 285 | 166981 | struct expr *verdict_expr_alloc(const struct location *loc, | |
| 286 | int verdict, struct expr *chain) | ||
| 287 | { | ||
| 288 | struct expr *expr; | ||
| 289 | |||
| 290 | 166981 | expr = expr_alloc(loc, EXPR_VERDICT, &verdict_type, | |
| 291 | BYTEORDER_INVALID, 0); | ||
| 292 | 166981 | expr->verdict = verdict; | |
| 293 |
2/2✓ Branch 0 (3→4) taken 139876 times.
✓ Branch 1 (3→5) taken 27105 times.
|
166981 | if (chain != NULL) |
| 294 | 139876 | expr->chain = chain; | |
| 295 | 166981 | expr->flags = EXPR_F_CONSTANT | EXPR_F_SINGLETON; | |
| 296 | 166981 | return expr; | |
| 297 | } | ||
| 298 | |||
| 299 | 217 | static void symbol_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 300 | { | ||
| 301 | 217 | nft_print(octx, "%s", expr->identifier); | |
| 302 | 217 | } | |
| 303 | |||
| 304 | 254 | static void symbol_expr_clone(struct expr *new, const struct expr *expr) | |
| 305 | { | ||
| 306 | 254 | new->symtype = expr->symtype; | |
| 307 | 254 | new->scope = expr->scope; | |
| 308 | 254 | new->identifier = xstrdup(expr->identifier); | |
| 309 | 254 | } | |
| 310 | |||
| 311 | 1633862 | static void symbol_expr_destroy(struct expr *expr) | |
| 312 | { | ||
| 313 | 1633862 | free_const(expr->identifier); | |
| 314 | 1633862 | } | |
| 315 | |||
| 316 | static const struct expr_ops symbol_expr_ops = { | ||
| 317 | .type = EXPR_SYMBOL, | ||
| 318 | .name = "symbol", | ||
| 319 | .print = symbol_expr_print, | ||
| 320 | .clone = symbol_expr_clone, | ||
| 321 | .destroy = symbol_expr_destroy, | ||
| 322 | }; | ||
| 323 | |||
| 324 | 1633608 | struct expr *symbol_expr_alloc(const struct location *loc, | |
| 325 | enum symbol_types type, struct scope *scope, | ||
| 326 | const char *identifier) | ||
| 327 | { | ||
| 328 | struct expr *expr; | ||
| 329 | |||
| 330 | 1633608 | expr = expr_alloc(loc, EXPR_SYMBOL, &invalid_type, | |
| 331 | BYTEORDER_INVALID, 0); | ||
| 332 | 1633608 | expr->symtype = type; | |
| 333 | 1633608 | expr->scope = scope; | |
| 334 | 1633608 | expr->identifier = xstrdup(identifier); | |
| 335 | 1633608 | return expr; | |
| 336 | } | ||
| 337 | |||
| 338 | 12 | static void variable_expr_print(const struct expr *expr, | |
| 339 | struct output_ctx *octx) | ||
| 340 | { | ||
| 341 | 12 | nft_print(octx, "$%s", expr->sym->identifier); | |
| 342 | 12 | } | |
| 343 | |||
| 344 | 5 | static void variable_expr_clone(struct expr *new, const struct expr *expr) | |
| 345 | { | ||
| 346 | 5 | new->scope = expr->scope; | |
| 347 | 5 | new->sym = expr->sym; | |
| 348 | |||
| 349 |
2/4✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 5 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 5 times.
|
5 | assert_refcount_safe(expr->sym->refcnt); |
| 350 | 5 | expr->sym->refcnt++; | |
| 351 | 5 | } | |
| 352 | |||
| 353 | 20217 | static void variable_expr_destroy(struct expr *expr) | |
| 354 | { | ||
| 355 |
2/4✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 20217 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 20217 times.
|
20217 | assert_refcount_safe(expr->sym->refcnt); |
| 356 | 20217 | expr->sym->refcnt--; | |
| 357 | 20217 | } | |
| 358 | |||
| 359 | static const struct expr_ops variable_expr_ops = { | ||
| 360 | .type = EXPR_VARIABLE, | ||
| 361 | .name = "variable", | ||
| 362 | .print = variable_expr_print, | ||
| 363 | .clone = variable_expr_clone, | ||
| 364 | .destroy = variable_expr_destroy, | ||
| 365 | }; | ||
| 366 | |||
| 367 | 20212 | struct expr *variable_expr_alloc(const struct location *loc, | |
| 368 | struct scope *scope, struct symbol *sym) | ||
| 369 | { | ||
| 370 | struct expr *expr; | ||
| 371 | |||
| 372 | 20212 | expr = expr_alloc(loc, EXPR_VARIABLE, &invalid_type, | |
| 373 | BYTEORDER_INVALID, 0); | ||
| 374 | 20212 | expr->scope = scope; | |
| 375 | 20212 | expr->sym = sym; | |
| 376 | 20212 | return expr; | |
| 377 | } | ||
| 378 | |||
| 379 | #define NFTNL_UDATA_CONSTANT_TYPE 0 | ||
| 380 | #define NFTNL_UDATA_CONSTANT_MAX NFTNL_UDATA_CONSTANT_TYPE | ||
| 381 | |||
| 382 | #define CONSTANT_EXPR_NFQUEUE_ID 0 | ||
| 383 | |||
| 384 | 3000 | static int constant_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 385 | const struct expr *expr) | ||
| 386 | { | ||
| 387 | uint32_t type; | ||
| 388 | |||
| 389 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 2998 times.
|
3000 | if (expr->dtype == &queue_type) |
| 390 | 2 | type = CONSTANT_EXPR_NFQUEUE_ID; | |
| 391 | else | ||
| 392 | 2998 | return -1; | |
| 393 | |||
| 394 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 2 times.
|
2 | if (!nftnl_udata_put_u32(udbuf, NFTNL_UDATA_CONSTANT_TYPE, type)) |
| 395 | ✗ | return -1; | |
| 396 | |||
| 397 | 2 | return 0; | |
| 398 | } | ||
| 399 | |||
| 400 | 2 | static int constant_parse_udata(const struct nftnl_udata *attr, void *data) | |
| 401 | { | ||
| 402 | 2 | const struct nftnl_udata **ud = data; | |
| 403 | 2 | uint8_t type = nftnl_udata_type(attr); | |
| 404 | 2 | uint8_t len = nftnl_udata_len(attr); | |
| 405 | uint32_t value; | ||
| 406 | |||
| 407 |
1/2✓ Branch 0 (4→5) taken 2 times.
✗ Branch 1 (4→12) not taken.
|
2 | switch (type) { |
| 408 | 2 | case NFTNL_UDATA_CONSTANT_TYPE: | |
| 409 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 2 times.
|
2 | if (len != sizeof(uint32_t)) |
| 410 | ✗ | return -1; | |
| 411 | |||
| 412 | 2 | value = nftnl_udata_get_u32(attr); | |
| 413 |
1/2✓ Branch 0 (8→9) taken 2 times.
✗ Branch 1 (8→10) not taken.
|
2 | switch (value) { |
| 414 | 2 | case CONSTANT_EXPR_NFQUEUE_ID: | |
| 415 | 2 | break; | |
| 416 | ✗ | default: | |
| 417 | ✗ | return -1; | |
| 418 | } | ||
| 419 | 2 | break; | |
| 420 | ✗ | default: | |
| 421 | ✗ | return 0; | |
| 422 | } | ||
| 423 | |||
| 424 | 2 | ud[type] = attr; | |
| 425 | |||
| 426 | 2 | return 0; | |
| 427 | } | ||
| 428 | |||
| 429 | 25149 | static struct expr *constant_expr_parse_udata(const struct nftnl_udata *attr) | |
| 430 | { | ||
| 431 | 25149 | const struct nftnl_udata *ud[NFTNL_UDATA_CONSTANT_MAX + 1] = {}; | |
| 432 | 25149 | const struct datatype *dtype = NULL; | |
| 433 | uint32_t type; | ||
| 434 | int err; | ||
| 435 | |||
| 436 | 25149 | err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr), | |
| 437 | constant_parse_udata, ud); | ||
| 438 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 25149 times.
|
25149 | if (err < 0) |
| 439 | ✗ | return NULL; | |
| 440 | |||
| 441 |
2/2✓ Branch 0 (7→8) taken 25147 times.
✓ Branch 1 (7→9) taken 2 times.
|
25149 | if (!ud[NFTNL_UDATA_CONSTANT_TYPE]) |
| 442 | 25147 | return NULL; | |
| 443 | |||
| 444 | 2 | type = nftnl_udata_get_u32(ud[NFTNL_UDATA_CONSTANT_TYPE]); | |
| 445 |
1/2✓ Branch 0 (10→11) taken 2 times.
✗ Branch 1 (10→12) not taken.
|
2 | switch (type) { |
| 446 | 2 | case CONSTANT_EXPR_NFQUEUE_ID: | |
| 447 | 2 | dtype = &queue_type; | |
| 448 | 2 | break; | |
| 449 | ✗ | default: | |
| 450 | ✗ | break; | |
| 451 | } | ||
| 452 | |||
| 453 | 2 | return constant_expr_alloc(&internal_location, dtype, BYTEORDER_HOST_ENDIAN, | |
| 454 | 16, NULL); | ||
| 455 | } | ||
| 456 | |||
| 457 | 318516 | static void constant_expr_print(const struct expr *expr, | |
| 458 | struct output_ctx *octx) | ||
| 459 | { | ||
| 460 | 318516 | datatype_print(expr, octx); | |
| 461 | 318516 | } | |
| 462 | |||
| 463 | ✗ | static bool constant_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 464 | { | ||
| 465 | ✗ | return expr_basetype(e1) == expr_basetype(e2) && | |
| 466 | ✗ | !mpz_cmp(e1->value, e2->value); | |
| 467 | } | ||
| 468 | |||
| 469 | 228829 | static void constant_expr_clone(struct expr *new, const struct expr *expr) | |
| 470 | { | ||
| 471 | 228829 | mpz_init_set(new->value, expr->value); | |
| 472 | 228829 | } | |
| 473 | |||
| 474 | 11401593 | static void constant_expr_destroy(struct expr *expr) | |
| 475 | { | ||
| 476 | 11401593 | mpz_clear(expr->value); | |
| 477 | 11401593 | } | |
| 478 | |||
| 479 | static const struct expr_ops constant_expr_ops = { | ||
| 480 | .type = EXPR_VALUE, | ||
| 481 | .name = "value", | ||
| 482 | .print = constant_expr_print, | ||
| 483 | .json = constant_expr_json, | ||
| 484 | .cmp = constant_expr_cmp, | ||
| 485 | .clone = constant_expr_clone, | ||
| 486 | .destroy = constant_expr_destroy, | ||
| 487 | .build_udata = constant_expr_build_udata, | ||
| 488 | .parse_udata = constant_expr_parse_udata, | ||
| 489 | }; | ||
| 490 | |||
| 491 | 11172764 | struct expr *constant_expr_alloc(const struct location *loc, | |
| 492 | const struct datatype *dtype, | ||
| 493 | enum byteorder byteorder, | ||
| 494 | unsigned int len, const void *data) | ||
| 495 | { | ||
| 496 | struct expr *expr; | ||
| 497 | |||
| 498 | 11172764 | expr = expr_alloc(loc, EXPR_VALUE, dtype, byteorder, len); | |
| 499 | 11172764 | expr->flags = EXPR_F_CONSTANT | EXPR_F_SINGLETON; | |
| 500 | |||
| 501 | 11172764 | mpz_init2(expr->value, len); | |
| 502 |
3/4✓ Branch 0 (4→5) taken 9073571 times.
✓ Branch 1 (4→9) taken 2099193 times.
✓ Branch 2 (5→6) taken 9073571 times.
✗ Branch 3 (5→9) not taken.
|
11172764 | if (data != NULL && len) |
| 503 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 9073571 times.
|
9073571 | mpz_import_data(expr->value, data, byteorder, |
| 504 | div_round_up(len, BITS_PER_BYTE)); | ||
| 505 | |||
| 506 | 11172764 | return expr; | |
| 507 | } | ||
| 508 | |||
| 509 | 144 | struct expr *constant_expr_join(const struct expr *e1, const struct expr *e2) | |
| 510 | 144 | { | |
| 511 | 144 | unsigned int len = (e1->len + e2->len) / BITS_PER_BYTE, tmp; | |
| 512 | 144 | unsigned char data[len]; | |
| 513 | |||
| 514 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 144 times.
|
144 | assert(e1->etype == EXPR_VALUE); |
| 515 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 144 times.
|
144 | assert(e2->etype == EXPR_VALUE); |
| 516 | |||
| 517 | 144 | tmp = e1->len / BITS_PER_BYTE; | |
| 518 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 144 times.
|
144 | mpz_export_data(data, e1->value, e1->byteorder, tmp); |
| 519 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 144 times.
|
144 | mpz_export_data(data + tmp, e2->value, e2->byteorder, |
| 520 | e2->len / BITS_PER_BYTE); | ||
| 521 | |||
| 522 | 144 | return constant_expr_alloc(&e1->location, &invalid_type, | |
| 523 | BYTEORDER_INVALID, len * BITS_PER_BYTE, | ||
| 524 | data); | ||
| 525 | } | ||
| 526 | |||
| 527 | 64712 | struct expr *constant_expr_splice(struct expr *expr, unsigned int len) | |
| 528 | { | ||
| 529 | struct expr *slice; | ||
| 530 | mpz_t mask; | ||
| 531 | |||
| 532 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 64712 times.
|
64712 | assert(expr->etype == EXPR_VALUE); |
| 533 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 64712 times.
|
64712 | assert(len <= expr->len); |
| 534 | |||
| 535 | 64712 | slice = constant_expr_alloc(&expr->location, &invalid_type, | |
| 536 | BYTEORDER_INVALID, len, NULL); | ||
| 537 | 64712 | mpz_init2(mask, len); | |
| 538 | 64712 | mpz_bitmask(mask, len); | |
| 539 | 64712 | mpz_lshift_ui(mask, expr->len - len); | |
| 540 | |||
| 541 | 64712 | mpz_set(slice->value, expr->value); | |
| 542 | 64712 | mpz_and(slice->value, slice->value, mask); | |
| 543 | 64712 | mpz_rshift_ui(slice->value, expr->len - len); | |
| 544 | 64712 | mpz_clear(mask); | |
| 545 | |||
| 546 | 64712 | expr->len -= len; | |
| 547 | 64712 | return slice; | |
| 548 | } | ||
| 549 | |||
| 550 | ✗ | static void constant_range_expr_print_one(const struct expr *expr, | |
| 551 | const mpz_t value, | ||
| 552 | struct output_ctx *octx) | ||
| 553 | ✗ | { | |
| 554 | ✗ | unsigned int len = div_round_up(expr->len, BITS_PER_BYTE); | |
| 555 | ✗ | unsigned char data[len]; | |
| 556 | struct expr *dummy; | ||
| 557 | |||
| 558 | /* create dummy temporary constant expression to print range. */ | ||
| 559 | ✗ | mpz_export_data(data, value, expr->byteorder, len); | |
| 560 | ✗ | dummy = constant_expr_alloc(&expr->location, expr->dtype, | |
| 561 | ✗ | expr->byteorder, expr->len, data); | |
| 562 | ✗ | expr_print(dummy, octx); | |
| 563 | ✗ | expr_free(dummy); | |
| 564 | ✗ | } | |
| 565 | |||
| 566 | ✗ | static void constant_range_expr_print(const struct expr *expr, | |
| 567 | struct output_ctx *octx) | ||
| 568 | { | ||
| 569 | ✗ | unsigned int flags = octx->flags; | |
| 570 | |||
| 571 | /* similar to range_expr_print(). */ | ||
| 572 | ✗ | octx->flags &= ~(NFT_CTX_OUTPUT_SERVICE | | |
| 573 | NFT_CTX_OUTPUT_REVERSEDNS | | ||
| 574 | NFT_CTX_OUTPUT_GUID); | ||
| 575 | ✗ | octx->flags |= NFT_CTX_OUTPUT_NUMERIC_ALL; | |
| 576 | |||
| 577 | ✗ | constant_range_expr_print_one(expr, expr->range.low, octx); | |
| 578 | ✗ | nft_print(octx, "-"); | |
| 579 | ✗ | constant_range_expr_print_one(expr, expr->range.high, octx); | |
| 580 | |||
| 581 | ✗ | octx->flags = flags; | |
| 582 | ✗ | } | |
| 583 | |||
| 584 | ✗ | static bool constant_range_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 585 | { | ||
| 586 | ✗ | return expr_basetype(e1) == expr_basetype(e2) && | |
| 587 | ✗ | !mpz_cmp(e1->range.low, e2->range.low) && | |
| 588 | ✗ | !mpz_cmp(e1->range.high, e2->range.high); | |
| 589 | } | ||
| 590 | |||
| 591 | 111627 | static void constant_range_expr_clone(struct expr *new, const struct expr *expr) | |
| 592 | { | ||
| 593 | 111627 | mpz_init_set(new->range.low, expr->range.low); | |
| 594 | 111627 | mpz_init_set(new->range.high, expr->range.high); | |
| 595 | 111627 | } | |
| 596 | |||
| 597 | 2188394 | static void constant_range_expr_destroy(struct expr *expr) | |
| 598 | { | ||
| 599 | 2188394 | mpz_clear(expr->range.low); | |
| 600 | 2188394 | mpz_clear(expr->range.high); | |
| 601 | 2188394 | } | |
| 602 | |||
| 603 | static const struct expr_ops constant_range_expr_ops = { | ||
| 604 | .type = EXPR_RANGE_VALUE, | ||
| 605 | .name = "range_value", | ||
| 606 | .print = constant_range_expr_print, | ||
| 607 | .cmp = constant_range_expr_cmp, | ||
| 608 | .clone = constant_range_expr_clone, | ||
| 609 | .destroy = constant_range_expr_destroy, | ||
| 610 | }; | ||
| 611 | |||
| 612 | 2076767 | struct expr *constant_range_expr_alloc(const struct location *loc, | |
| 613 | const struct datatype *dtype, | ||
| 614 | enum byteorder byteorder, | ||
| 615 | unsigned int len, mpz_t low, mpz_t high) | ||
| 616 | { | ||
| 617 | struct expr *expr; | ||
| 618 | |||
| 619 | 2076767 | expr = expr_alloc(loc, EXPR_RANGE_VALUE, dtype, byteorder, len); | |
| 620 | 2076767 | expr->flags = EXPR_F_CONSTANT; | |
| 621 | |||
| 622 | 2076767 | mpz_init_set(expr->range.low, low); | |
| 623 | 2076767 | mpz_init_set(expr->range.high, high); | |
| 624 | |||
| 625 | 2076767 | return expr; | |
| 626 | } | ||
| 627 | |||
| 628 | 23 | static void symbol_range_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 629 | { | ||
| 630 | 23 | nft_print(octx, "%s", expr->identifier_range[0]); | |
| 631 | 23 | nft_print(octx, "-"); | |
| 632 | 23 | nft_print(octx, "%s", expr->identifier_range[1]); | |
| 633 | 23 | } | |
| 634 | |||
| 635 | 20 | static void symbol_range_expr_clone(struct expr *new, const struct expr *expr) | |
| 636 | { | ||
| 637 | 20 | new->symtype = expr->symtype; | |
| 638 | 20 | new->scope = expr->scope; | |
| 639 | 20 | new->identifier_range[0] = xstrdup(expr->identifier_range[0]); | |
| 640 | 20 | new->identifier_range[1] = xstrdup(expr->identifier_range[1]); | |
| 641 | 20 | } | |
| 642 | |||
| 643 | 17034 | static void symbol_range_expr_destroy(struct expr *expr) | |
| 644 | { | ||
| 645 | 17034 | free_const(expr->identifier_range[0]); | |
| 646 | 17034 | free_const(expr->identifier_range[1]); | |
| 647 | 17034 | } | |
| 648 | |||
| 649 | static const struct expr_ops symbol_range_expr_ops = { | ||
| 650 | .type = EXPR_RANGE_SYMBOL, | ||
| 651 | .name = "range_symbol", | ||
| 652 | .print = symbol_range_expr_print, | ||
| 653 | .clone = symbol_range_expr_clone, | ||
| 654 | .destroy = symbol_range_expr_destroy, | ||
| 655 | }; | ||
| 656 | |||
| 657 | 17014 | struct expr *symbol_range_expr_alloc(const struct location *loc, | |
| 658 | enum symbol_types type, const struct scope *scope, | ||
| 659 | const char *identifier_low, const char *identifier_high) | ||
| 660 | { | ||
| 661 | struct expr *expr; | ||
| 662 | |||
| 663 | 17014 | expr = expr_alloc(loc, EXPR_RANGE_SYMBOL, &invalid_type, | |
| 664 | BYTEORDER_INVALID, 0); | ||
| 665 | 17014 | expr->symtype = type; | |
| 666 | 17014 | expr->scope = scope; | |
| 667 | 17014 | expr->identifier_range[0] = xstrdup(identifier_low); | |
| 668 | 17014 | expr->identifier_range[1] = xstrdup(identifier_high); | |
| 669 | 17014 | return expr; | |
| 670 | } | ||
| 671 | |||
| 672 | /* | ||
| 673 | * Allocate a constant expression with a single bit set at position n. | ||
| 674 | */ | ||
| 675 | 1076 | struct expr *flag_expr_alloc(const struct location *loc, | |
| 676 | const struct datatype *dtype, | ||
| 677 | enum byteorder byteorder, | ||
| 678 | unsigned int len, unsigned long n) | ||
| 679 | { | ||
| 680 | struct expr *expr; | ||
| 681 | |||
| 682 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1076 times.
|
1076 | assert(n < len); |
| 683 | |||
| 684 | 1076 | expr = constant_expr_alloc(loc, dtype, byteorder, len, NULL); | |
| 685 | 1076 | mpz_set_ui(expr->value, 1); | |
| 686 | 1076 | mpz_lshift_ui(expr->value, n); | |
| 687 | |||
| 688 | 1076 | return expr; | |
| 689 | } | ||
| 690 | |||
| 691 | /* | ||
| 692 | * Convert an expression of basetype TYPE_BITMASK into a series of inclusive | ||
| 693 | * OR binop expressions of the individual flag values. | ||
| 694 | */ | ||
| 695 | 1356 | struct expr *bitmask_expr_to_binops(struct expr *expr) | |
| 696 | { | ||
| 697 | struct expr *binop, *flag; | ||
| 698 | unsigned long n; | ||
| 699 | |||
| 700 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1356 times.
|
1356 | assert(expr->etype == EXPR_VALUE); |
| 701 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 1356 times.
|
1356 | assert(expr->dtype->basetype->type == TYPE_BITMASK); |
| 702 | |||
| 703 | 1356 | n = mpz_popcount(expr->value); | |
| 704 |
4/4✓ Branch 0 (6→7) taken 1064 times.
✓ Branch 1 (6→8) taken 292 times.
✓ Branch 2 (7→8) taken 694 times.
✓ Branch 3 (7→9) taken 370 times.
|
1356 | if (n == 0 || n == 1) |
| 705 | 986 | return expr; | |
| 706 | |||
| 707 | 370 | binop = NULL; | |
| 708 | 370 | n = 0; | |
| 709 |
2/2✓ Branch 0 (15→10) taken 1076 times.
✓ Branch 1 (15→16) taken 370 times.
|
1446 | while ((n = mpz_scan1(expr->value, n)) != ULONG_MAX) { |
| 710 | 1076 | flag = flag_expr_alloc(&expr->location, expr->dtype, | |
| 711 | 1076 | expr->byteorder, expr->len, n); | |
| 712 |
2/2✓ Branch 0 (11→12) taken 706 times.
✓ Branch 1 (11→13) taken 370 times.
|
1076 | if (binop != NULL) |
| 713 | 706 | binop = binop_expr_alloc(&expr->location, | |
| 714 | OP_OR, binop, flag); | ||
| 715 | else | ||
| 716 | 370 | binop = flag; | |
| 717 | |||
| 718 | 1076 | n++; | |
| 719 | } | ||
| 720 | |||
| 721 | 370 | expr_free(expr); | |
| 722 | 370 | return binop; | |
| 723 | } | ||
| 724 | |||
| 725 | 82992 | static void prefix_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 726 | { | ||
| 727 | 82992 | expr_print(expr->prefix, octx); | |
| 728 | 82992 | nft_print(octx, "/%u", expr->prefix_len); | |
| 729 | 82992 | } | |
| 730 | |||
| 731 | 279 | static void prefix_expr_set_type(const struct expr *expr, | |
| 732 | const struct datatype *type, | ||
| 733 | enum byteorder byteorder) | ||
| 734 | { | ||
| 735 | 279 | expr_set_type(expr->prefix, type, byteorder); | |
| 736 | 279 | } | |
| 737 | |||
| 738 | 71 | static void prefix_expr_clone(struct expr *new, const struct expr *expr) | |
| 739 | { | ||
| 740 | 71 | new->prefix = expr_clone(expr->prefix); | |
| 741 | 71 | new->prefix_len = expr->prefix_len; | |
| 742 | 71 | } | |
| 743 | |||
| 744 | 438258 | static void prefix_expr_destroy(struct expr *expr) | |
| 745 | { | ||
| 746 | 438258 | expr_free(expr->prefix); | |
| 747 | 438258 | } | |
| 748 | |||
| 749 | static const struct expr_ops prefix_expr_ops = { | ||
| 750 | .type = EXPR_PREFIX, | ||
| 751 | .name = "prefix", | ||
| 752 | .print = prefix_expr_print, | ||
| 753 | .json = prefix_expr_json, | ||
| 754 | .set_type = prefix_expr_set_type, | ||
| 755 | .clone = prefix_expr_clone, | ||
| 756 | .destroy = prefix_expr_destroy, | ||
| 757 | }; | ||
| 758 | |||
| 759 | 438187 | struct expr *prefix_expr_alloc(const struct location *loc, | |
| 760 | struct expr *expr, unsigned int prefix_len) | ||
| 761 | { | ||
| 762 | struct expr *prefix; | ||
| 763 | |||
| 764 | 438187 | prefix = expr_alloc(loc, EXPR_PREFIX, &invalid_type, | |
| 765 | BYTEORDER_INVALID, 0); | ||
| 766 | 438187 | prefix->prefix = expr; | |
| 767 | 438187 | prefix->prefix_len = prefix_len; | |
| 768 | 438187 | return prefix; | |
| 769 | } | ||
| 770 | |||
| 771 | const char *expr_op_symbols[] = { | ||
| 772 | [OP_INVALID] = "invalid", | ||
| 773 | [OP_HTON] = "hton", | ||
| 774 | [OP_NTOH] = "ntoh", | ||
| 775 | [OP_AND] = "&", | ||
| 776 | [OP_OR] = "|", | ||
| 777 | [OP_XOR] = "^", | ||
| 778 | [OP_LSHIFT] = "<<", | ||
| 779 | [OP_RSHIFT] = ">>", | ||
| 780 | [OP_EQ] = "==", | ||
| 781 | [OP_NEQ] = "!=", | ||
| 782 | [OP_LT] = "<", | ||
| 783 | [OP_GT] = ">", | ||
| 784 | [OP_LTE] = "<=", | ||
| 785 | [OP_GTE] = ">=", | ||
| 786 | [OP_NEG] = "!", | ||
| 787 | }; | ||
| 788 | |||
| 789 | 395 | static void unary_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 790 | { | ||
| 791 | 395 | expr_print(expr->arg, octx); | |
| 792 | 395 | } | |
| 793 | |||
| 794 | 882 | static void unary_expr_clone(struct expr *new, const struct expr *expr) | |
| 795 | { | ||
| 796 | 882 | new->arg = expr_clone(expr->arg); | |
| 797 | 882 | } | |
| 798 | |||
| 799 | 2576 | static void unary_expr_destroy(struct expr *expr) | |
| 800 | { | ||
| 801 | 2576 | expr_free(expr->arg); | |
| 802 | 2576 | } | |
| 803 | |||
| 804 | static const struct expr_ops unary_expr_ops = { | ||
| 805 | .type = EXPR_UNARY, | ||
| 806 | .name = "unary", | ||
| 807 | .print = unary_expr_print, | ||
| 808 | .json = unary_expr_json, | ||
| 809 | .clone = unary_expr_clone, | ||
| 810 | .destroy = unary_expr_destroy, | ||
| 811 | }; | ||
| 812 | |||
| 813 | 1694 | struct expr *unary_expr_alloc(const struct location *loc, | |
| 814 | enum ops op, struct expr *arg) | ||
| 815 | { | ||
| 816 | struct expr *expr; | ||
| 817 | |||
| 818 | 1694 | expr = expr_alloc(loc, EXPR_UNARY, &invalid_type, | |
| 819 | BYTEORDER_INVALID, 0); | ||
| 820 | 1694 | expr->op = op; | |
| 821 | 1694 | expr->arg = arg; | |
| 822 | 1694 | return expr; | |
| 823 | } | ||
| 824 | |||
| 825 | static uint8_t expr_binop_precedence[OP_MAX + 1] = { | ||
| 826 | [OP_LSHIFT] = 1, | ||
| 827 | [OP_RSHIFT] = 1, | ||
| 828 | [OP_AND] = 2, | ||
| 829 | [OP_XOR] = 3, | ||
| 830 | [OP_OR] = 4, | ||
| 831 | }; | ||
| 832 | |||
| 833 | 54424 | static void binop_arg_print(const struct expr *op, const struct expr *arg, | |
| 834 | struct output_ctx *octx) | ||
| 835 | { | ||
| 836 | 54424 | bool prec = false; | |
| 837 | |||
| 838 |
2/2✓ Branch 0 (2→3) taken 448 times.
✓ Branch 1 (2→6) taken 53976 times.
|
54424 | if (arg->etype == EXPR_BINOP && |
| 839 |
2/2✓ Branch 0 (3→4) taken 271 times.
✓ Branch 1 (3→6) taken 177 times.
|
448 | expr_binop_precedence[op->op] != 0 && |
| 840 |
2/2✓ Branch 0 (4→5) taken 71 times.
✓ Branch 1 (4→6) taken 200 times.
|
271 | expr_binop_precedence[op->op] < expr_binop_precedence[arg->op]) |
| 841 | 71 | prec = 1; | |
| 842 | |||
| 843 |
2/2✓ Branch 0 (6→7) taken 71 times.
✓ Branch 1 (6→8) taken 54353 times.
|
54424 | if (prec) |
| 844 | 71 | nft_print(octx, "("); | |
| 845 | 54424 | expr_print(arg, octx); | |
| 846 |
2/2✓ Branch 0 (9→10) taken 71 times.
✓ Branch 1 (9→11) taken 54353 times.
|
54424 | if (prec) |
| 847 | 71 | nft_print(octx, ")"); | |
| 848 | 54424 | } | |
| 849 | |||
| 850 | 24851 | bool must_print_eq_op(const struct expr *expr) | |
| 851 | { | ||
| 852 |
2/2✓ Branch 0 (2→3) taken 23248 times.
✓ Branch 1 (2→6) taken 1603 times.
|
24851 | if (expr->right->dtype->basetype != NULL && |
| 853 |
2/2✓ Branch 0 (3→4) taken 81 times.
✓ Branch 1 (3→6) taken 23167 times.
|
23248 | expr->right->dtype->basetype->type == TYPE_BITMASK && |
| 854 |
2/2✓ Branch 0 (4→5) taken 35 times.
✓ Branch 1 (4→6) taken 46 times.
|
81 | expr->right->etype == EXPR_VALUE) |
| 855 | 35 | return true; | |
| 856 | |||
| 857 | 24816 | return expr->left->etype == EXPR_BINOP; | |
| 858 | } | ||
| 859 | |||
| 860 | 27212 | static void binop_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 861 | { | ||
| 862 | 27212 | binop_arg_print(expr, expr->left, octx); | |
| 863 | |||
| 864 |
2/2✓ Branch 0 (3→4) taken 27005 times.
✓ Branch 1 (3→8) taken 207 times.
|
27212 | if (expr_op_symbols[expr->op] && |
| 865 |
4/4✓ Branch 0 (4→5) taken 24851 times.
✓ Branch 1 (4→7) taken 2154 times.
✓ Branch 2 (6→7) taken 103 times.
✓ Branch 3 (6→8) taken 24748 times.
|
27005 | (expr->op != OP_EQ || must_print_eq_op(expr))) |
| 866 | 2257 | nft_print(octx, " %s ", expr_op_symbols[expr->op]); | |
| 867 | else | ||
| 868 | 24955 | nft_print(octx, " "); | |
| 869 | |||
| 870 | 27212 | binop_arg_print(expr, expr->right, octx); | |
| 871 | 27212 | } | |
| 872 | |||
| 873 | 2843 | static void binop_expr_clone(struct expr *new, const struct expr *expr) | |
| 874 | { | ||
| 875 | 2843 | new->left = expr_clone(expr->left); | |
| 876 | 2843 | new->right = expr_clone(expr->right); | |
| 877 | 2843 | } | |
| 878 | |||
| 879 | 469006 | static void binop_expr_destroy(struct expr *expr) | |
| 880 | { | ||
| 881 | 469006 | expr_free(expr->left); | |
| 882 | 469006 | expr_free(expr->right); | |
| 883 | 469006 | } | |
| 884 | |||
| 885 | static const struct expr_ops binop_expr_ops = { | ||
| 886 | .type = EXPR_BINOP, | ||
| 887 | .name = "binop", | ||
| 888 | .print = binop_expr_print, | ||
| 889 | .json = binop_expr_json, | ||
| 890 | .clone = binop_expr_clone, | ||
| 891 | .destroy = binop_expr_destroy, | ||
| 892 | }; | ||
| 893 | |||
| 894 | 291251 | struct expr *binop_expr_alloc(const struct location *loc, enum ops op, | |
| 895 | struct expr *left, struct expr *right) | ||
| 896 | { | ||
| 897 | struct expr *expr; | ||
| 898 | |||
| 899 | 291251 | expr = expr_alloc(loc, EXPR_BINOP, left->dtype, | |
| 900 | 291251 | left->byteorder, 0); | |
| 901 | 291251 | expr->left = left; | |
| 902 | 291251 | expr->op = op; | |
| 903 | 291251 | expr->right = right; | |
| 904 | 291251 | return expr; | |
| 905 | } | ||
| 906 | |||
| 907 | static const struct expr_ops relational_expr_ops = { | ||
| 908 | .type = EXPR_RELATIONAL, | ||
| 909 | .name = "relational", | ||
| 910 | .print = binop_expr_print, | ||
| 911 | .json = relational_expr_json, | ||
| 912 | .destroy = binop_expr_destroy, | ||
| 913 | }; | ||
| 914 | |||
| 915 | 174912 | struct expr *relational_expr_alloc(const struct location *loc, enum ops op, | |
| 916 | struct expr *left, struct expr *right) | ||
| 917 | { | ||
| 918 | struct expr *expr; | ||
| 919 | |||
| 920 | 174912 | expr = expr_alloc(loc, EXPR_RELATIONAL, &verdict_type, | |
| 921 | BYTEORDER_INVALID, 0); | ||
| 922 | 174912 | expr->left = left; | |
| 923 | 174912 | expr->op = op; | |
| 924 | 174912 | expr->right = right; | |
| 925 | |||
| 926 |
2/2✓ Branch 0 (3→4) taken 342 times.
✓ Branch 1 (3→5) taken 174570 times.
|
174912 | if (right->dtype == &boolean_type) |
| 927 | 342 | left->flags |= EXPR_F_BOOLEAN; | |
| 928 | |||
| 929 | 174912 | return expr; | |
| 930 | } | ||
| 931 | |||
| 932 | 166810 | void relational_expr_pctx_update(struct proto_ctx *ctx, | |
| 933 | const struct expr *expr) | ||
| 934 | { | ||
| 935 | 166810 | const struct expr *left = expr->left, *right = expr->right; | |
| 936 | const struct expr_ops *ops; | ||
| 937 | const struct expr *i; | ||
| 938 | |||
| 939 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 166810 times.
|
166810 | assert(expr->etype == EXPR_RELATIONAL); |
| 940 |
3/4✓ Branch 0 (4→5) taken 48746 times.
✓ Branch 1 (4→7) taken 118064 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→7) taken 48746 times.
|
166810 | assert(expr->op == OP_EQ || expr->op == OP_IMPLICIT); |
| 941 | |||
| 942 | 166810 | ops = expr_ops(left); | |
| 943 |
2/2✓ Branch 0 (8→9) taken 163758 times.
✓ Branch 1 (8→29) taken 3052 times.
|
166810 | if (ops->pctx_update && |
| 944 |
2/2✓ Branch 0 (9→10) taken 31197 times.
✓ Branch 1 (9→29) taken 132561 times.
|
163758 | (left->flags & EXPR_F_PROTOCOL)) { |
| 945 |
2/2✓ Branch 0 (11→12) taken 30846 times.
✓ Branch 1 (11→13) taken 351 times.
|
31197 | if (expr_is_singleton(right)) |
| 946 | 30846 | ops->pctx_update(ctx, &expr->location, left, right); | |
| 947 |
2/2✓ Branch 0 (13→14) taken 320 times.
✓ Branch 1 (13→24) taken 31 times.
|
351 | else if (right->etype == EXPR_SET) { |
| 948 |
4/6✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 320 times.
✗ Branch 2 (21→22) not taken.
✓ Branch 3 (21→23) taken 1402 times.
✓ Branch 4 (23→17) taken 1082 times.
✓ Branch 5 (23→29) taken 320 times.
|
1402 | list_for_each_entry(i, &expr_set(right)->expressions, list) { |
| 949 |
1/2✓ Branch 0 (17→18) taken 1082 times.
✗ Branch 1 (17→20) not taken.
|
1082 | if (i->etype == EXPR_SET_ELEM && |
| 950 |
2/2✓ Branch 0 (18→19) taken 1054 times.
✓ Branch 1 (18→20) taken 28 times.
|
1082 | i->key->etype == EXPR_VALUE) |
| 951 | 1054 | ops->pctx_update(ctx, &expr->location, left, i->key); | |
| 952 | } | ||
| 953 |
2/2✓ Branch 0 (24→25) taken 18 times.
✓ Branch 1 (24→29) taken 13 times.
|
31 | } else if (ops == &meta_expr_ops && |
| 954 |
2/2✓ Branch 0 (25→26) taken 4 times.
✓ Branch 1 (25→29) taken 14 times.
|
18 | right->etype == EXPR_SET_REF) { |
| 955 | 4 | const struct expr *key = right->set->key; | |
| 956 | struct expr *tmp; | ||
| 957 | |||
| 958 | 4 | tmp = constant_expr_alloc(&expr->location, key->dtype, | |
| 959 | 4 | key->byteorder, key->len, | |
| 960 | NULL); | ||
| 961 | |||
| 962 | 4 | ops->pctx_update(ctx, &expr->location, left, tmp); | |
| 963 | 4 | expr_free(tmp); | |
| 964 | } | ||
| 965 | } | ||
| 966 | 166810 | } | |
| 967 | |||
| 968 | 9472 | static void range_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 969 | { | ||
| 970 | 9472 | unsigned int flags = octx->flags; | |
| 971 | |||
| 972 | 9472 | octx->flags &= ~(NFT_CTX_OUTPUT_SERVICE | | |
| 973 | NFT_CTX_OUTPUT_REVERSEDNS | | ||
| 974 | NFT_CTX_OUTPUT_GUID); | ||
| 975 | 9472 | octx->flags |= NFT_CTX_OUTPUT_NUMERIC_ALL; | |
| 976 | 9472 | expr_print(expr->left, octx); | |
| 977 | 9472 | nft_print(octx, "-"); | |
| 978 | 9472 | expr_print(expr->right, octx); | |
| 979 | 9472 | octx->flags = flags; | |
| 980 | 9472 | } | |
| 981 | |||
| 982 | 683 | static void range_expr_clone(struct expr *new, const struct expr *expr) | |
| 983 | { | ||
| 984 | 683 | new->left = expr_clone(expr->left); | |
| 985 | 683 | new->right = expr_clone(expr->right); | |
| 986 | 683 | } | |
| 987 | |||
| 988 | 71716 | static void range_expr_destroy(struct expr *expr) | |
| 989 | { | ||
| 990 | 71716 | expr_free(expr->left); | |
| 991 | 71716 | expr_free(expr->right); | |
| 992 | 71716 | } | |
| 993 | |||
| 994 | 1965 | static void range_expr_set_type(const struct expr *expr, | |
| 995 | const struct datatype *type, | ||
| 996 | enum byteorder byteorder) | ||
| 997 | { | ||
| 998 | 1965 | expr_set_type(expr->left, type, byteorder); | |
| 999 | 1965 | expr_set_type(expr->right, type, byteorder); | |
| 1000 | 1965 | } | |
| 1001 | |||
| 1002 | static const struct expr_ops range_expr_ops = { | ||
| 1003 | .type = EXPR_RANGE, | ||
| 1004 | .name = "range", | ||
| 1005 | .print = range_expr_print, | ||
| 1006 | .json = range_expr_json, | ||
| 1007 | .clone = range_expr_clone, | ||
| 1008 | .destroy = range_expr_destroy, | ||
| 1009 | .set_type = range_expr_set_type, | ||
| 1010 | }; | ||
| 1011 | |||
| 1012 | 71033 | struct expr *range_expr_alloc(const struct location *loc, | |
| 1013 | struct expr *left, struct expr *right) | ||
| 1014 | { | ||
| 1015 | struct expr *expr; | ||
| 1016 | |||
| 1017 | 71033 | expr = expr_alloc(loc, EXPR_RANGE, &invalid_type, | |
| 1018 | BYTEORDER_INVALID, 0); | ||
| 1019 | 71033 | expr->left = left; | |
| 1020 | 71033 | expr->right = right; | |
| 1021 | 71033 | return expr; | |
| 1022 | } | ||
| 1023 | |||
| 1024 | 114003 | static void concat_expr_destroy(struct expr *expr) | |
| 1025 | { | ||
| 1026 | struct expr *i, *next; | ||
| 1027 | |||
| 1028 |
4/6✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 114003 times.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→9) taken 333895 times.
✓ Branch 4 (9→5) taken 219892 times.
✓ Branch 5 (9→10) taken 114003 times.
|
333895 | list_for_each_entry_safe(i, next, &expr_concat(expr)->expressions, list) |
| 1029 | 219892 | expr_free(i); | |
| 1030 | 114003 | } | |
| 1031 | |||
| 1032 | 7636 | static void concat_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 1033 | { | ||
| 1034 | const struct expr *i; | ||
| 1035 | 7636 | const char *d = ""; | |
| 1036 | |||
| 1037 |
4/6✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 7636 times.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→10) taken 24529 times.
✓ Branch 4 (10→5) taken 16893 times.
✓ Branch 5 (10→11) taken 7636 times.
|
24529 | list_for_each_entry(i, &expr_concat(expr)->expressions, list) { |
| 1038 | 16893 | nft_print(octx, "%s", d); | |
| 1039 | 16893 | expr_print(i, octx); | |
| 1040 | 16893 | d = " . "; | |
| 1041 | } | ||
| 1042 | 7636 | } | |
| 1043 | |||
| 1044 | 259 | static void concat_expr_clone(struct expr *new, const struct expr *expr) | |
| 1045 | { | ||
| 1046 | struct expr *i; | ||
| 1047 | |||
| 1048 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 259 times.
|
259 | init_list_head(&expr_concat(new)->expressions); |
| 1049 |
4/6✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 259 times.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→13) taken 772 times.
✓ Branch 4 (13→8) taken 513 times.
✓ Branch 5 (13→14) taken 259 times.
|
772 | list_for_each_entry(i, &expr_concat(expr)->expressions, list) |
| 1050 | 513 | concat_expr_add(new, expr_clone(i)); | |
| 1051 | 259 | } | |
| 1052 | |||
| 1053 | #define NFTNL_UDATA_SET_KEY_CONCAT_NEST 0 | ||
| 1054 | #define NFTNL_UDATA_SET_KEY_CONCAT_NEST_MAX NFT_REG32_SIZE | ||
| 1055 | |||
| 1056 | #define NFTNL_UDATA_SET_KEY_CONCAT_SUB_TYPE 0 | ||
| 1057 | #define NFTNL_UDATA_SET_KEY_CONCAT_SUB_DATA 1 | ||
| 1058 | #define NFTNL_UDATA_SET_KEY_CONCAT_SUB_MAX 2 | ||
| 1059 | |||
| 1060 | 2666 | static struct expr *expr_build_udata_recurse(struct expr *e) | |
| 1061 | { | ||
| 1062 |
2/2✓ Branch 0 (2→3) taken 20 times.
✓ Branch 1 (2→4) taken 2646 times.
|
2666 | switch (e->etype) { |
| 1063 | 20 | case EXPR_BINOP: | |
| 1064 | 20 | return expr_build_udata_recurse(e->left); | |
| 1065 | 2646 | default: | |
| 1066 | 2646 | break; | |
| 1067 | } | ||
| 1068 | |||
| 1069 | 2646 | return e; | |
| 1070 | } | ||
| 1071 | |||
| 1072 | 1507 | static int concat_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 1073 | const struct expr *concat_expr) | ||
| 1074 | { | ||
| 1075 | struct nftnl_udata *nest; | ||
| 1076 | struct expr *expr, *tmp; | ||
| 1077 | 1507 | unsigned int i = 0; | |
| 1078 | |||
| 1079 |
4/6✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1507 times.
✗ Branch 2 (20→21) not taken.
✓ Branch 3 (20→22) taken 3701 times.
✓ Branch 4 (22→5) taken 2646 times.
✓ Branch 5 (22→23) taken 1055 times.
|
3701 | list_for_each_entry_safe(expr, tmp, &expr_concat(concat_expr)->expressions, list) { |
| 1080 | struct nftnl_udata *nest_expr; | ||
| 1081 | int err; | ||
| 1082 | |||
| 1083 | 2646 | expr = expr_build_udata_recurse(expr); | |
| 1084 |
3/4✓ Branch 0 (7→8) taken 2640 times.
✓ Branch 1 (7→9) taken 6 times.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→10) taken 2640 times.
|
2646 | if (!expr_ops(expr)->build_udata || i >= NFT_REG32_SIZE) |
| 1085 | 6 | return -1; | |
| 1086 | |||
| 1087 | 2640 | nest = nftnl_udata_nest_start(udbuf, NFTNL_UDATA_SET_KEY_CONCAT_NEST + i); | |
| 1088 | 2640 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_KEY_CONCAT_SUB_TYPE, expr->etype); | |
| 1089 | 2640 | nest_expr = nftnl_udata_nest_start(udbuf, NFTNL_UDATA_SET_KEY_CONCAT_SUB_DATA); | |
| 1090 | 2640 | err = expr_ops(expr)->build_udata(udbuf, expr); | |
| 1091 |
2/2✓ Branch 0 (15→16) taken 446 times.
✓ Branch 1 (15→17) taken 2194 times.
|
2640 | if (err < 0) |
| 1092 | 446 | return err; | |
| 1093 | 2194 | nftnl_udata_nest_end(udbuf, nest_expr); | |
| 1094 | 2194 | nftnl_udata_nest_end(udbuf, nest); | |
| 1095 | 2194 | i++; | |
| 1096 | } | ||
| 1097 | |||
| 1098 | 1055 | return 0; | |
| 1099 | } | ||
| 1100 | |||
| 1101 | 10914 | static int concat_parse_udata_nest(const struct nftnl_udata *attr, void *data) | |
| 1102 | { | ||
| 1103 | 10914 | const struct nftnl_udata **ud = data; | |
| 1104 | 10914 | uint8_t type = nftnl_udata_type(attr); | |
| 1105 | 10914 | uint8_t len = nftnl_udata_len(attr); | |
| 1106 | |||
| 1107 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 10914 times.
|
10914 | if (type >= NFTNL_UDATA_SET_KEY_CONCAT_NEST_MAX) |
| 1108 | ✗ | return -1; | |
| 1109 | |||
| 1110 |
2/2✓ Branch 0 (6→7) taken 7484 times.
✓ Branch 1 (6→8) taken 3430 times.
|
10914 | if (len <= sizeof(uint32_t)) |
| 1111 | 7484 | return -1; | |
| 1112 | |||
| 1113 | 3430 | ud[type] = attr; | |
| 1114 | 3430 | return 0; | |
| 1115 | } | ||
| 1116 | |||
| 1117 | 6860 | static int concat_parse_udata_nested(const struct nftnl_udata *attr, void *data) | |
| 1118 | { | ||
| 1119 | 6860 | const struct nftnl_udata **ud = data; | |
| 1120 | 6860 | uint8_t type = nftnl_udata_type(attr); | |
| 1121 | 6860 | uint8_t len = nftnl_udata_len(attr); | |
| 1122 | |||
| 1123 |
2/3✓ Branch 0 (4→5) taken 3430 times.
✓ Branch 1 (4→8) taken 3430 times.
✗ Branch 2 (4→11) not taken.
|
6860 | switch (type) { |
| 1124 | 3430 | case NFTNL_UDATA_SET_KEY_CONCAT_SUB_TYPE: | |
| 1125 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 3430 times.
|
3430 | if (len != sizeof(uint32_t)) |
| 1126 | ✗ | return -1; | |
| 1127 | 3430 | break; | |
| 1128 | 3430 | case NFTNL_UDATA_SET_KEY_CONCAT_SUB_DATA: | |
| 1129 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 3430 times.
|
3430 | if (len <= sizeof(uint32_t)) |
| 1130 | ✗ | return -1; | |
| 1131 | 3430 | break; | |
| 1132 | ✗ | default: | |
| 1133 | ✗ | return 0; | |
| 1134 | } | ||
| 1135 | |||
| 1136 | 6860 | ud[type] = attr; | |
| 1137 | 6860 | return 0; | |
| 1138 | } | ||
| 1139 | |||
| 1140 | 9091 | static struct expr *concat_expr_parse_udata(const struct nftnl_udata *attr) | |
| 1141 | { | ||
| 1142 | 9091 | const struct nftnl_udata *ud[NFTNL_UDATA_SET_KEY_CONCAT_NEST_MAX] = {}; | |
| 1143 | const struct datatype *dtype; | ||
| 1144 | struct expr *concat_expr; | ||
| 1145 | 9091 | uint32_t dt = 0, len = 0; | |
| 1146 | unsigned int i; | ||
| 1147 | int err; | ||
| 1148 | |||
| 1149 | 9091 | err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr), | |
| 1150 | concat_parse_udata_nest, ud); | ||
| 1151 |
2/2✓ Branch 0 (5→6) taken 7484 times.
✓ Branch 1 (5→7) taken 1607 times.
|
9091 | if (err < 0) |
| 1152 | 7484 | return NULL; | |
| 1153 | |||
| 1154 | 1607 | concat_expr = concat_expr_alloc(&internal_location); | |
| 1155 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 1607 times.
|
1607 | if (!concat_expr) |
| 1156 | ✗ | return NULL; | |
| 1157 | |||
| 1158 |
1/2✓ Branch 0 (31→11) taken 5037 times.
✗ Branch 1 (31→32) not taken.
|
5037 | for (i = 0; i < array_size(ud); i++) { |
| 1159 | const struct nftnl_udata *nest_ud[NFTNL_UDATA_SET_KEY_CONCAT_SUB_MAX]; | ||
| 1160 | const struct nftnl_udata *nested, *subdata; | ||
| 1161 | const struct expr_ops *ops; | ||
| 1162 | struct expr *expr; | ||
| 1163 | uint32_t etype; | ||
| 1164 | |||
| 1165 |
2/2✓ Branch 0 (11→12) taken 1607 times.
✓ Branch 1 (11→13) taken 3430 times.
|
5037 | if (ud[NFTNL_UDATA_SET_KEY_CONCAT_NEST + i] == NULL) |
| 1166 | 1607 | break; | |
| 1167 | |||
| 1168 | 3430 | nested = ud[NFTNL_UDATA_SET_KEY_CONCAT_NEST + i]; | |
| 1169 | 3430 | err = nftnl_udata_parse(nftnl_udata_get(nested), nftnl_udata_len(nested), | |
| 1170 | concat_parse_udata_nested, nest_ud); | ||
| 1171 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 3430 times.
|
3430 | if (err < 0) |
| 1172 | ✗ | goto err_free; | |
| 1173 | |||
| 1174 | 3430 | etype = nftnl_udata_get_u32(nest_ud[NFTNL_UDATA_SET_KEY_CONCAT_SUB_TYPE]); | |
| 1175 | 3430 | ops = expr_ops_by_type_u32(etype); | |
| 1176 |
2/4✓ Branch 0 (20→21) taken 3430 times.
✗ Branch 1 (20→22) not taken.
✗ Branch 2 (21→22) not taken.
✓ Branch 3 (21→23) taken 3430 times.
|
3430 | if (!ops || !ops->parse_udata) |
| 1177 | ✗ | goto err_free; | |
| 1178 | |||
| 1179 | 3430 | subdata = nest_ud[NFTNL_UDATA_SET_KEY_CONCAT_SUB_DATA]; | |
| 1180 | 3430 | expr = ops->parse_udata(subdata); | |
| 1181 |
1/2✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 3430 times.
|
3430 | if (!expr) |
| 1182 | ✗ | goto err_free; | |
| 1183 | |||
| 1184 | 3430 | dt = concat_subtype_add(dt, expr->dtype->type); | |
| 1185 | 3430 | concat_expr_add(concat_expr, expr); | |
| 1186 | 3430 | len += netlink_padded_len(expr->len); | |
| 1187 | } | ||
| 1188 | |||
| 1189 | 1607 | dtype = concat_type_alloc(dt); | |
| 1190 |
1/2✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 1607 times.
|
1607 | if (!dtype) |
| 1191 | ✗ | goto err_free; | |
| 1192 | |||
| 1193 | 1607 | __datatype_set(concat_expr, dtype); | |
| 1194 | 1607 | concat_expr->len = len; | |
| 1195 | |||
| 1196 | 1607 | return concat_expr; | |
| 1197 | |||
| 1198 | ✗ | err_free: | |
| 1199 | ✗ | expr_free(concat_expr); | |
| 1200 | ✗ | return NULL; | |
| 1201 | } | ||
| 1202 | |||
| 1203 | static const struct expr_ops concat_expr_ops = { | ||
| 1204 | .type = EXPR_CONCAT, | ||
| 1205 | .name = "concat", | ||
| 1206 | .print = concat_expr_print, | ||
| 1207 | .json = concat_expr_json, | ||
| 1208 | .clone = concat_expr_clone, | ||
| 1209 | .destroy = concat_expr_destroy, | ||
| 1210 | .build_udata = concat_expr_build_udata, | ||
| 1211 | .parse_udata = concat_expr_parse_udata, | ||
| 1212 | }; | ||
| 1213 | |||
| 1214 | 113744 | struct expr *concat_expr_alloc(const struct location *loc) | |
| 1215 | { | ||
| 1216 | struct expr *expr; | ||
| 1217 | |||
| 1218 | 113744 | expr = expr_alloc(loc, EXPR_CONCAT, &invalid_type, BYTEORDER_INVALID, 0); | |
| 1219 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 113744 times.
|
113744 | init_list_head(&expr_concat(expr)->expressions); |
| 1220 | |||
| 1221 | 113744 | return expr; | |
| 1222 | } | ||
| 1223 | |||
| 1224 | 236626 | void concat_expr_add(struct expr *concat, struct expr *item) | |
| 1225 | { | ||
| 1226 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 236626 times.
|
236626 | struct expr_concat *expr_concat = expr_concat(concat); |
| 1227 | |||
| 1228 | 236626 | list_add_tail(&item->list, &expr_concat->expressions); | |
| 1229 | 236626 | expr_concat->size++; | |
| 1230 | 236626 | } | |
| 1231 | |||
| 1232 | 16794 | void concat_expr_remove(struct expr *concat, struct expr *expr) | |
| 1233 | { | ||
| 1234 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 16794 times.
|
16794 | expr_concat(concat)->size--; |
| 1235 | 16794 | list_del(&expr->list); | |
| 1236 | 16794 | } | |
| 1237 | |||
| 1238 | 59 | static void list_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 1239 | { | ||
| 1240 | const struct expr *i; | ||
| 1241 | 59 | const char *d = ""; | |
| 1242 | |||
| 1243 |
4/6✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 59 times.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→10) taken 211 times.
✓ Branch 4 (10→5) taken 152 times.
✓ Branch 5 (10→11) taken 59 times.
|
211 | list_for_each_entry(i, &expr_list(expr)->expressions, list) { |
| 1244 | 152 | nft_print(octx, "%s", d); | |
| 1245 | 152 | expr_print(i, octx); | |
| 1246 | 152 | d = ","; | |
| 1247 | } | ||
| 1248 | 59 | } | |
| 1249 | |||
| 1250 | 1 | static void list_expr_clone(struct expr *new, const struct expr *expr) | |
| 1251 | { | ||
| 1252 | struct expr *i; | ||
| 1253 | |||
| 1254 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1 times.
|
1 | init_list_head(&expr_list(new)->expressions); |
| 1255 |
4/6✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 1 times.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→13) taken 4 times.
✓ Branch 4 (13→8) taken 3 times.
✓ Branch 5 (13→14) taken 1 times.
|
4 | list_for_each_entry(i, &expr_list(expr)->expressions, list) |
| 1256 | 3 | list_expr_add(new, expr_clone(i)); | |
| 1257 | 1 | } | |
| 1258 | |||
| 1259 | 1639 | static void list_expr_destroy(struct expr *expr) | |
| 1260 | { | ||
| 1261 | struct expr *i, *next; | ||
| 1262 | |||
| 1263 |
4/6✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1639 times.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→9) taken 3746 times.
✓ Branch 4 (9→5) taken 2107 times.
✓ Branch 5 (9→10) taken 1639 times.
|
3746 | list_for_each_entry_safe(i, next, &expr_list(expr)->expressions, list) |
| 1264 | 2107 | expr_free(i); | |
| 1265 | 1639 | } | |
| 1266 | |||
| 1267 | static const struct expr_ops list_expr_ops = { | ||
| 1268 | .type = EXPR_LIST, | ||
| 1269 | .name = "list", | ||
| 1270 | .print = list_expr_print, | ||
| 1271 | .json = list_expr_json, | ||
| 1272 | .clone = list_expr_clone, | ||
| 1273 | .destroy = list_expr_destroy, | ||
| 1274 | }; | ||
| 1275 | |||
| 1276 | 1638 | struct expr *list_expr_alloc(const struct location *loc) | |
| 1277 | { | ||
| 1278 | struct expr *expr; | ||
| 1279 | |||
| 1280 | 1638 | expr = expr_alloc(loc, EXPR_LIST, &invalid_type, BYTEORDER_INVALID, 0); | |
| 1281 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 1638 times.
|
1638 | init_list_head(&expr_list(expr)->expressions); |
| 1282 | |||
| 1283 | 1638 | return expr; | |
| 1284 | } | ||
| 1285 | |||
| 1286 | 2127 | void list_expr_add(struct expr *expr, struct expr *item) | |
| 1287 | { | ||
| 1288 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2127 times.
|
2127 | struct expr_list *expr_list = expr_list(expr); |
| 1289 | |||
| 1290 | 2127 | list_add_tail(&item->list, &expr_list->expressions); | |
| 1291 | 2127 | expr_list->size++; | |
| 1292 | 2127 | } | |
| 1293 | |||
| 1294 | ✗ | void list_expr_remove(struct expr *list, struct expr *expr) | |
| 1295 | { | ||
| 1296 | ✗ | expr_list(list)->size--; | |
| 1297 | ✗ | list_del(&expr->list); | |
| 1298 | ✗ | } | |
| 1299 | |||
| 1300 | /* list is assumed to have two items at least, otherwise extend this! */ | ||
| 1301 | 15 | struct expr *list_expr_to_binop(struct expr *expr) | |
| 1302 | { | ||
| 1303 | 15 | struct expr *first, *last = NULL, *i; | |
| 1304 | |||
| 1305 |
2/4✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 15 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→7) taken 15 times.
|
15 | assert(!list_empty(&expr_list(expr)->expressions)); |
| 1306 | |||
| 1307 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 15 times.
|
15 | first = list_first_entry(&expr_list(expr)->expressions, struct expr, list); |
| 1308 | 15 | i = first; | |
| 1309 | |||
| 1310 |
3/4✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 30 times.
✓ Branch 2 (17→10) taken 15 times.
✓ Branch 3 (17→18) taken 15 times.
|
30 | list_for_each_entry_continue(i, &expr_list(expr)->expressions, list) { |
| 1311 |
1/2✓ Branch 0 (10→11) taken 15 times.
✗ Branch 1 (10→13) not taken.
|
15 | if (first) { |
| 1312 | 15 | last = binop_expr_alloc(&expr->location, OP_OR, first, i); | |
| 1313 | 15 | first = NULL; | |
| 1314 | } else { | ||
| 1315 | ✗ | last = binop_expr_alloc(&expr->location, OP_OR, i, last); | |
| 1316 | } | ||
| 1317 | } | ||
| 1318 | /* list with one single item only, this should not happen. */ | ||
| 1319 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 15 times.
|
15 | assert(!first); |
| 1320 | |||
| 1321 | /* zap list expressions, they have been moved to binop expression. */ | ||
| 1322 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 15 times.
|
15 | init_list_head(&expr_list(expr)->expressions); |
| 1323 | 15 | expr_free(expr); | |
| 1324 | |||
| 1325 | 15 | return last; | |
| 1326 | } | ||
| 1327 | |||
| 1328 | 259455 | static const char *calculate_delim(const struct expr *expr, int *count, | |
| 1329 | struct output_ctx *octx) | ||
| 1330 | { | ||
| 1331 | 259455 | const char *newline = ",\n\t\t\t "; | |
| 1332 | 259455 | const char *singleline = ", "; | |
| 1333 | |||
| 1334 |
2/2✓ Branch 0 (2→3) taken 23172 times.
✓ Branch 1 (2→4) taken 236283 times.
|
259455 | if (octx->force_newline) |
| 1335 | 23172 | return newline; | |
| 1336 | |||
| 1337 |
3/4✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 236283 times.
✓ Branch 2 (7→8) taken 5247 times.
✓ Branch 3 (7→9) taken 231036 times.
|
236283 | if (set_is_anonymous(expr_set(expr)->set_flags)) |
| 1338 | 5247 | return singleline; | |
| 1339 | |||
| 1340 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 231036 times.
|
231036 | if (!expr->dtype) |
| 1341 | ✗ | return newline; | |
| 1342 | |||
| 1343 |
3/3✓ Branch 0 (11→12) taken 33007 times.
✓ Branch 1 (11→15) taken 197325 times.
✓ Branch 2 (11→18) taken 704 times.
|
231036 | switch (expr->dtype->type) { |
| 1344 | 33007 | case TYPE_NFPROTO: | |
| 1345 | case TYPE_INTEGER: | ||
| 1346 | case TYPE_ARPOP: | ||
| 1347 | case TYPE_INET_PROTOCOL: | ||
| 1348 | case TYPE_INET_SERVICE: | ||
| 1349 | case TYPE_TCP_FLAG: | ||
| 1350 | case TYPE_DCCP_PKTTYPE: | ||
| 1351 | case TYPE_MARK: | ||
| 1352 | case TYPE_IFINDEX: | ||
| 1353 | case TYPE_CLASSID: | ||
| 1354 | case TYPE_UID: | ||
| 1355 | case TYPE_GID: | ||
| 1356 | case TYPE_CT_DIR: | ||
| 1357 |
2/2✓ Branch 0 (12→13) taken 26441 times.
✓ Branch 1 (12→14) taken 6566 times.
|
33007 | if (*count < 5) |
| 1358 | 26441 | return singleline; | |
| 1359 | 6566 | *count = 0; | |
| 1360 | 6566 | break; | |
| 1361 | 197325 | case TYPE_IPADDR: | |
| 1362 | case TYPE_CT_STATE: | ||
| 1363 | case TYPE_CT_STATUS: | ||
| 1364 | case TYPE_PKTTYPE: | ||
| 1365 |
2/2✓ Branch 0 (15→16) taken 99491 times.
✓ Branch 1 (15→17) taken 97834 times.
|
197325 | if (*count < 2) |
| 1366 | 99491 | return singleline; | |
| 1367 | 97834 | *count = 0; | |
| 1368 | 97834 | break; | |
| 1369 | |||
| 1370 | 704 | default: | |
| 1371 | 704 | break; | |
| 1372 | } | ||
| 1373 | |||
| 1374 | 105104 | return newline; | |
| 1375 | } | ||
| 1376 | |||
| 1377 | 4975 | static void set_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 1378 | { | ||
| 1379 | const struct expr *i; | ||
| 1380 | 4975 | const char *d = ""; | |
| 1381 | 4975 | int count = 0; | |
| 1382 | |||
| 1383 | 4975 | nft_print(octx, "{ "); | |
| 1384 | |||
| 1385 |
4/6✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 4975 times.
✗ Branch 2 (10→11) not taken.
✓ Branch 3 (10→12) taken 264430 times.
✓ Branch 4 (12→6) taken 259455 times.
✓ Branch 5 (12→13) taken 4975 times.
|
264430 | list_for_each_entry(i, &expr_set(expr)->expressions, list) { |
| 1386 | 259455 | nft_print(octx, "%s", d); | |
| 1387 | 259455 | expr_print(i, octx); | |
| 1388 | 259455 | count++; | |
| 1389 | 259455 | d = calculate_delim(expr, &count, octx); | |
| 1390 | } | ||
| 1391 | |||
| 1392 | 4975 | nft_print(octx, " }"); | |
| 1393 | 4975 | } | |
| 1394 | |||
| 1395 | 9 | static void set_expr_clone(struct expr *new, const struct expr *expr) | |
| 1396 | { | ||
| 1397 | struct expr *i; | ||
| 1398 | |||
| 1399 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 9 times.
|
9 | init_list_head(&expr_set(new)->expressions); |
| 1400 |
4/6✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 9 times.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→13) taken 45 times.
✓ Branch 4 (13→8) taken 36 times.
✓ Branch 5 (13→14) taken 9 times.
|
45 | list_for_each_entry(i, &expr_set(expr)->expressions, list) |
| 1401 | 36 | set_expr_add(new, expr_clone(i)); | |
| 1402 | 9 | } | |
| 1403 | |||
| 1404 | 50617 | static void set_expr_destroy(struct expr *expr) | |
| 1405 | { | ||
| 1406 | struct expr *i, *next; | ||
| 1407 | |||
| 1408 |
4/6✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 50617 times.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→9) taken 5239089 times.
✓ Branch 4 (9→5) taken 5188472 times.
✓ Branch 5 (9→10) taken 50617 times.
|
5239089 | list_for_each_entry_safe(i, next, &expr_set(expr)->expressions, list) |
| 1409 | 5188472 | expr_free(i); | |
| 1410 | 50617 | } | |
| 1411 | |||
| 1412 | 3309 | static void set_expr_set_type(const struct expr *expr, | |
| 1413 | const struct datatype *dtype, | ||
| 1414 | enum byteorder byteorder) | ||
| 1415 | { | ||
| 1416 | struct expr *i; | ||
| 1417 | |||
| 1418 |
4/6✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 3309 times.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→9) taken 14717 times.
✓ Branch 4 (9→5) taken 11408 times.
✓ Branch 5 (9→10) taken 3309 times.
|
14717 | list_for_each_entry(i, &expr_set(expr)->expressions, list) |
| 1419 | 11408 | expr_set_type(i, dtype, byteorder); | |
| 1420 | 3309 | } | |
| 1421 | |||
| 1422 | static const struct expr_ops set_expr_ops = { | ||
| 1423 | .type = EXPR_SET, | ||
| 1424 | .name = "set", | ||
| 1425 | .print = set_expr_print, | ||
| 1426 | .json = set_expr_json, | ||
| 1427 | .set_type = set_expr_set_type, | ||
| 1428 | .clone = set_expr_clone, | ||
| 1429 | .destroy = set_expr_destroy, | ||
| 1430 | }; | ||
| 1431 | |||
| 1432 | 50608 | struct expr *set_expr_alloc(const struct location *loc, const struct set *set) | |
| 1433 | { | ||
| 1434 | struct expr *set_expr; | ||
| 1435 | |||
| 1436 | 50608 | set_expr = expr_alloc(loc, EXPR_SET, &invalid_type, BYTEORDER_INVALID, 0); | |
| 1437 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 50608 times.
|
50608 | init_list_head(&expr_set(set_expr)->expressions); |
| 1438 | |||
| 1439 |
2/2✓ Branch 0 (6→7) taken 34676 times.
✓ Branch 1 (6→8) taken 15932 times.
|
50608 | if (!set) |
| 1440 | 34676 | return set_expr; | |
| 1441 | |||
| 1442 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 15932 times.
|
15932 | expr_set(set_expr)->set_flags = set->flags; |
| 1443 | 15932 | datatype_set(set_expr, set->key->dtype); | |
| 1444 | |||
| 1445 | 15932 | return set_expr; | |
| 1446 | } | ||
| 1447 | |||
| 1448 | − | void __set_expr_add(struct expr *set, struct expr *elem) | |
| 1449 | { | ||
| 1450 | − | list_add_tail(&elem->list, &expr_set(set)->expressions); | |
| 1451 | − | } | |
| 1452 | |||
| 1453 | 11900239 | void set_expr_add(struct expr *set, struct expr *elem) | |
| 1454 | { | ||
| 1455 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 11900239 times.
|
11900239 | struct expr_set *expr_set = expr_set(set); |
| 1456 | |||
| 1457 | 11900239 | list_add_tail(&elem->list, &expr_set->expressions); | |
| 1458 | 11900239 | expr_set->size++; | |
| 1459 | 11900239 | } | |
| 1460 | |||
| 1461 | 6826085 | void set_expr_remove(struct expr *set, struct expr *expr) | |
| 1462 | { | ||
| 1463 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 6826085 times.
|
6826085 | expr_set(set)->size--; |
| 1464 | 6826085 | list_del(&expr->list); | |
| 1465 | 6826085 | } | |
| 1466 | |||
| 1467 | 15388 | static void mapping_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 1468 | { | ||
| 1469 | 15388 | expr_print(expr->left, octx); | |
| 1470 | 15388 | nft_print(octx, " : "); | |
| 1471 | 15388 | expr_print(expr->right, octx); | |
| 1472 | 15388 | } | |
| 1473 | |||
| 1474 | 120 | static void mapping_expr_set_type(const struct expr *expr, | |
| 1475 | const struct datatype *dtype, | ||
| 1476 | enum byteorder byteorder) | ||
| 1477 | { | ||
| 1478 | 120 | expr_set_type(expr->left, dtype, byteorder); | |
| 1479 | 120 | } | |
| 1480 | |||
| 1481 | 10896 | static void mapping_expr_clone(struct expr *new, const struct expr *expr) | |
| 1482 | { | ||
| 1483 | 10896 | new->left = expr_clone(expr->left); | |
| 1484 | 10896 | new->right = expr_clone(expr->right); | |
| 1485 | 10896 | } | |
| 1486 | |||
| 1487 | 255641 | static void mapping_expr_destroy(struct expr *expr) | |
| 1488 | { | ||
| 1489 | 255641 | expr_free(expr->left); | |
| 1490 | 255641 | expr_free(expr->right); | |
| 1491 | 255641 | } | |
| 1492 | |||
| 1493 | static const struct expr_ops mapping_expr_ops = { | ||
| 1494 | .type = EXPR_MAPPING, | ||
| 1495 | .name = "mapping", | ||
| 1496 | .print = mapping_expr_print, | ||
| 1497 | .json = mapping_expr_json, | ||
| 1498 | .set_type = mapping_expr_set_type, | ||
| 1499 | .clone = mapping_expr_clone, | ||
| 1500 | .destroy = mapping_expr_destroy, | ||
| 1501 | }; | ||
| 1502 | |||
| 1503 | 244745 | struct expr *mapping_expr_alloc(const struct location *loc, | |
| 1504 | struct expr *from, struct expr *to) | ||
| 1505 | { | ||
| 1506 | struct expr *expr; | ||
| 1507 | |||
| 1508 | 244745 | expr = expr_alloc(loc, EXPR_MAPPING, from->dtype, | |
| 1509 | 244745 | from->byteorder, 0); | |
| 1510 | 244745 | expr->left = from; | |
| 1511 | 244745 | expr->right = to; | |
| 1512 | 244745 | return expr; | |
| 1513 | } | ||
| 1514 | |||
| 1515 | − | static bool __set_expr_is_vmap(const struct expr *mappings) | |
| 1516 | { | ||
| 1517 | const struct expr *mapping; | ||
| 1518 | |||
| 1519 | − | if (list_empty(&expr_set(mappings)->expressions)) | |
| 1520 | − | return false; | |
| 1521 | |||
| 1522 | − | mapping = list_first_entry(&expr_set(mappings)->expressions, struct expr, list); | |
| 1523 | − | if (mapping->etype == EXPR_MAPPING && | |
| 1524 | − | mapping->right->etype == EXPR_VERDICT) | |
| 1525 | − | return true; | |
| 1526 | |||
| 1527 | − | return false; | |
| 1528 | } | ||
| 1529 | |||
| 1530 | 220 | static bool set_expr_is_vmap(const struct expr *expr) | |
| 1531 | { | ||
| 1532 | |||
| 1533 |
2/2✓ Branch 0 (2→3) taken 23 times.
✓ Branch 1 (2→5) taken 197 times.
|
220 | if (expr->mappings->etype == EXPR_SET) |
| 1534 | 23 | return __set_expr_is_vmap(expr->mappings); | |
| 1535 | |||
| 1536 | 197 | return false; | |
| 1537 | } | ||
| 1538 | |||
| 1539 | 519 | static void map_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 1540 | { | ||
| 1541 | 519 | expr_print(expr->map, octx); | |
| 1542 |
2/2✓ Branch 0 (3→4) taken 496 times.
✓ Branch 1 (3→5) taken 23 times.
|
519 | if ((expr->mappings->etype == EXPR_SET_REF && |
| 1543 |
4/4✓ Branch 0 (4→5) taken 197 times.
✓ Branch 1 (4→7) taken 299 times.
✓ Branch 2 (6→7) taken 16 times.
✓ Branch 3 (6→8) taken 204 times.
|
716 | expr->mappings->set->data->dtype->type == TYPE_VERDICT) || |
| 1544 | 220 | set_expr_is_vmap(expr)) | |
| 1545 | 315 | nft_print(octx, " vmap "); | |
| 1546 | else | ||
| 1547 | 204 | nft_print(octx, " map "); | |
| 1548 | |||
| 1549 | 519 | expr_print(expr->mappings, octx); | |
| 1550 | 519 | } | |
| 1551 | |||
| 1552 | 316 | static void map_expr_clone(struct expr *new, const struct expr *expr) | |
| 1553 | { | ||
| 1554 | 316 | new->map = expr_clone(expr->map); | |
| 1555 | 316 | new->mappings = expr_clone(expr->mappings); | |
| 1556 | 316 | } | |
| 1557 | |||
| 1558 | 2734 | static void map_expr_destroy(struct expr *expr) | |
| 1559 | { | ||
| 1560 | 2734 | expr_free(expr->map); | |
| 1561 | 2734 | expr_free(expr->mappings); | |
| 1562 | 2734 | } | |
| 1563 | |||
| 1564 | static const struct expr_ops map_expr_ops = { | ||
| 1565 | .type = EXPR_MAP, | ||
| 1566 | .name = "map", | ||
| 1567 | .print = map_expr_print, | ||
| 1568 | .json = map_expr_json, | ||
| 1569 | .clone = map_expr_clone, | ||
| 1570 | .destroy = map_expr_destroy, | ||
| 1571 | }; | ||
| 1572 | |||
| 1573 | 2418 | struct expr *map_expr_alloc(const struct location *loc, struct expr *arg, | |
| 1574 | struct expr *mappings) | ||
| 1575 | { | ||
| 1576 | struct expr *expr; | ||
| 1577 | |||
| 1578 | 2418 | expr = expr_alloc(loc, EXPR_MAP, &invalid_type, BYTEORDER_INVALID, 0); | |
| 1579 | 2418 | expr->map = arg; | |
| 1580 | 2418 | expr->mappings = mappings; | |
| 1581 | 2418 | return expr; | |
| 1582 | } | ||
| 1583 | |||
| 1584 | 2368 | static void set_ref_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 1585 | { | ||
| 1586 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2368 times.
|
2368 | if (set_is_meter(expr->set->flags)) |
| 1587 | ✗ | nft_print(octx, "%s", expr->set->handle.set.name); | |
| 1588 |
2/2✓ Branch 0 (6→7) taken 1740 times.
✓ Branch 1 (6→8) taken 628 times.
|
2368 | else if (set_is_anonymous(expr->set->flags)) |
| 1589 | 1740 | expr_print(expr->set->init, octx); | |
| 1590 | else | ||
| 1591 | 628 | nft_print(octx, "@%s", expr->set->handle.set.name); | |
| 1592 | 2368 | } | |
| 1593 | |||
| 1594 | 316 | static void set_ref_expr_clone(struct expr *new, const struct expr *expr) | |
| 1595 | { | ||
| 1596 | 316 | new->set = set_get(expr->set); | |
| 1597 | 316 | } | |
| 1598 | |||
| 1599 | 12947 | static void set_ref_expr_destroy(struct expr *expr) | |
| 1600 | { | ||
| 1601 | 12947 | set_free(expr->set); | |
| 1602 | 12947 | } | |
| 1603 | |||
| 1604 | 4647 | static void set_ref_expr_set_type(const struct expr *expr, | |
| 1605 | const struct datatype *dtype, | ||
| 1606 | enum byteorder byteorder) | ||
| 1607 | { | ||
| 1608 | 4647 | const struct set *s = expr->set; | |
| 1609 | |||
| 1610 | /* normal sets already have a precise datatype that is given in | ||
| 1611 | * the set definition via type foo. | ||
| 1612 | * | ||
| 1613 | * Anon sets do not have this, and need to rely on type info | ||
| 1614 | * generated at rule creation time. | ||
| 1615 | * | ||
| 1616 | * For most cases, the type info is correct. | ||
| 1617 | * In some cases however, the kernel only stores TYPE_INTEGER. | ||
| 1618 | * | ||
| 1619 | * This happens with expressions that only use an integer alias | ||
| 1620 | * type, e.g. the mptcpopt_subtype datatype. | ||
| 1621 | * | ||
| 1622 | * In this case nft will print the elements as numerical values | ||
| 1623 | * because the base type lacks the ->sym_tbl information of the | ||
| 1624 | * subtypes. | ||
| 1625 | */ | ||
| 1626 |
4/4✓ Branch 0 (2→3) taken 4537 times.
✓ Branch 1 (2→6) taken 110 times.
✓ Branch 2 (4→5) taken 3309 times.
✓ Branch 3 (4→6) taken 1228 times.
|
4647 | if (s->init && set_is_anonymous(s->flags)) |
| 1627 | 3309 | expr_set_type(s->init, dtype, byteorder); | |
| 1628 | 4647 | } | |
| 1629 | |||
| 1630 | static const struct expr_ops set_ref_expr_ops = { | ||
| 1631 | .type = EXPR_SET_REF, | ||
| 1632 | .name = "set reference", | ||
| 1633 | .print = set_ref_expr_print, | ||
| 1634 | .json = set_ref_expr_json, | ||
| 1635 | .clone = set_ref_expr_clone, | ||
| 1636 | .destroy = set_ref_expr_destroy, | ||
| 1637 | .set_type = set_ref_expr_set_type, | ||
| 1638 | }; | ||
| 1639 | |||
| 1640 | 12631 | struct expr *set_ref_expr_alloc(const struct location *loc, struct set *set) | |
| 1641 | { | ||
| 1642 | struct expr *expr; | ||
| 1643 | |||
| 1644 | 12631 | expr = expr_alloc(loc, EXPR_SET_REF, set->key->dtype, 0, 0); | |
| 1645 | 12631 | expr->set = set_get(set); | |
| 1646 | 12631 | expr->flags |= EXPR_F_CONSTANT; | |
| 1647 | 12631 | return expr; | |
| 1648 | } | ||
| 1649 | |||
| 1650 | 259654 | static void set_elem_expr_print(const struct expr *expr, | |
| 1651 | struct output_ctx *octx) | ||
| 1652 | { | ||
| 1653 | struct stmt *stmt; | ||
| 1654 | |||
| 1655 | 259654 | expr_print(expr->key, octx); | |
| 1656 |
2/2✓ Branch 0 (7→4) taken 4347 times.
✓ Branch 1 (7→8) taken 259654 times.
|
264001 | list_for_each_entry(stmt, &expr->stmt_list, list) { |
| 1657 | 4347 | nft_print(octx, " "); | |
| 1658 | 4347 | stmt_print(stmt, octx); | |
| 1659 | } | ||
| 1660 |
2/2✓ Branch 0 (8→9) taken 277 times.
✓ Branch 1 (8→13) taken 259377 times.
|
259654 | if (expr->timeout) { |
| 1661 | 277 | nft_print(octx, " timeout "); | |
| 1662 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 277 times.
|
277 | if (expr->timeout == NFT_NEVER_TIMEOUT) |
| 1663 | ✗ | nft_print(octx, "never"); | |
| 1664 | else | ||
| 1665 | 277 | time_print(expr->timeout, octx); | |
| 1666 | } | ||
| 1667 |
2/2✓ Branch 0 (14→15) taken 253840 times.
✓ Branch 1 (14→19) taken 5814 times.
|
259654 | if (!nft_output_stateless(octx) && |
| 1668 |
1/2✓ Branch 0 (15→16) taken 253840 times.
✗ Branch 1 (15→19) not taken.
|
253840 | expr->timeout != NFT_NEVER_TIMEOUT && |
| 1669 |
2/2✓ Branch 0 (16→17) taken 426 times.
✓ Branch 1 (16→19) taken 253414 times.
|
253840 | expr->expiration) { |
| 1670 | 426 | nft_print(octx, " expires "); | |
| 1671 | 426 | time_print(expr->expiration, octx); | |
| 1672 | } | ||
| 1673 |
2/2✓ Branch 0 (19→20) taken 9 times.
✓ Branch 1 (19→21) taken 259645 times.
|
259654 | if (expr->comment) |
| 1674 | 9 | nft_print(octx, " comment \"%s\"", expr->comment); | |
| 1675 | 259654 | } | |
| 1676 | |||
| 1677 | 10165920 | static void set_elem_expr_destroy(struct expr *expr) | |
| 1678 | { | ||
| 1679 | struct stmt *stmt, *next; | ||
| 1680 | |||
| 1681 | 10165920 | free_const(expr->comment); | |
| 1682 | 10165920 | expr_free(expr->key); | |
| 1683 |
2/2✓ Branch 0 (6→4) taken 183796 times.
✓ Branch 1 (6→7) taken 10165920 times.
|
10349716 | list_for_each_entry_safe(stmt, next, &expr->stmt_list, list) |
| 1684 | 183796 | stmt_free(stmt); | |
| 1685 | 10165920 | } | |
| 1686 | |||
| 1687 | − | static void __set_elem_expr_clone(struct expr *new, const struct expr *expr) | |
| 1688 | { | ||
| 1689 | − | new->expiration = expr->expiration; | |
| 1690 | − | new->timeout = expr->timeout; | |
| 1691 | − | if (expr->comment) | |
| 1692 | − | new->comment = xstrdup(expr->comment); | |
| 1693 | − | init_list_head(&new->stmt_list); | |
| 1694 | − | } | |
| 1695 | |||
| 1696 | 112441 | static void set_elem_expr_clone(struct expr *new, const struct expr *expr) | |
| 1697 | { | ||
| 1698 | 112441 | new->key = expr_clone(expr->key); | |
| 1699 | 112441 | __set_elem_expr_clone(new, expr); | |
| 1700 | 112441 | } | |
| 1701 | |||
| 1702 | 11408 | static void set_elem_expr_set_type(const struct expr *expr, | |
| 1703 | const struct datatype *dtype, | ||
| 1704 | enum byteorder byteorder) | ||
| 1705 | { | ||
| 1706 | 11408 | expr_set_type(expr->key, dtype, byteorder); | |
| 1707 | 11408 | } | |
| 1708 | |||
| 1709 | static const struct expr_ops set_elem_expr_ops = { | ||
| 1710 | .type = EXPR_SET_ELEM, | ||
| 1711 | .name = "set element", | ||
| 1712 | .clone = set_elem_expr_clone, | ||
| 1713 | .print = set_elem_expr_print, | ||
| 1714 | .json = set_elem_expr_json, | ||
| 1715 | .destroy = set_elem_expr_destroy, | ||
| 1716 | .set_type = set_elem_expr_set_type, | ||
| 1717 | }; | ||
| 1718 | |||
| 1719 | 10053479 | struct expr *set_elem_expr_alloc(const struct location *loc, struct expr *key) | |
| 1720 | { | ||
| 1721 | struct expr *expr; | ||
| 1722 | |||
| 1723 | 10053479 | expr = expr_alloc(loc, EXPR_SET_ELEM, key->dtype, | |
| 1724 | 10053479 | key->byteorder, key->len); | |
| 1725 | 10053479 | expr->key = key; | |
| 1726 | 10053479 | init_list_head(&expr->stmt_list); | |
| 1727 | |||
| 1728 | 10053479 | return expr; | |
| 1729 | } | ||
| 1730 | |||
| 1731 | 38 | static void set_elem_catchall_expr_print(const struct expr *expr, | |
| 1732 | struct output_ctx *octx) | ||
| 1733 | { | ||
| 1734 | 38 | nft_print(octx, "*"); | |
| 1735 | 38 | } | |
| 1736 | |||
| 1737 | 3 | static void set_elem_catchall_expr_clone(struct expr *new, const struct expr *expr) | |
| 1738 | { | ||
| 1739 | 3 | __set_elem_expr_clone(new, expr); | |
| 1740 | 3 | } | |
| 1741 | |||
| 1742 | static const struct expr_ops set_elem_catchall_expr_ops = { | ||
| 1743 | .type = EXPR_SET_ELEM_CATCHALL, | ||
| 1744 | .name = "catch-all set element", | ||
| 1745 | .print = set_elem_catchall_expr_print, | ||
| 1746 | .json = set_elem_catchall_expr_json, | ||
| 1747 | .clone = set_elem_catchall_expr_clone, | ||
| 1748 | }; | ||
| 1749 | |||
| 1750 | 171 | struct expr *set_elem_catchall_expr_alloc(const struct location *loc) | |
| 1751 | { | ||
| 1752 | struct expr *expr; | ||
| 1753 | |||
| 1754 | 171 | expr = expr_alloc(loc, EXPR_SET_ELEM_CATCHALL, &invalid_type, | |
| 1755 | BYTEORDER_INVALID, 0); | ||
| 1756 | 171 | expr->flags = EXPR_F_CONSTANT | EXPR_F_SINGLETON; | |
| 1757 | |||
| 1758 | 171 | return expr; | |
| 1759 | } | ||
| 1760 | |||
| 1761 | 6168514 | void range_expr_value_low(mpz_t rop, const struct expr *expr) | |
| 1762 | { | ||
| 1763 |
6/7✓ Branch 0 (2→3) taken 21119 times.
✓ Branch 1 (2→4) taken 3052998 times.
✓ Branch 2 (2→5) taken 522 times.
✓ Branch 3 (2→6) taken 19935 times.
✓ Branch 4 (2→7) taken 4 times.
✓ Branch 5 (2→8) taken 3073936 times.
✗ Branch 6 (2→9) not taken.
|
6168514 | switch (expr->etype) { |
| 1764 | 21119 | case EXPR_VALUE: | |
| 1765 | 21119 | return mpz_set(rop, expr->value); | |
| 1766 | 3052998 | case EXPR_RANGE_VALUE: | |
| 1767 | 3052998 | return mpz_set(rop, expr->range.low); | |
| 1768 | 522 | case EXPR_PREFIX: | |
| 1769 | 522 | return range_expr_value_low(rop, expr->prefix); | |
| 1770 | 19935 | case EXPR_RANGE: | |
| 1771 | 19935 | return range_expr_value_low(rop, expr->left); | |
| 1772 | 4 | case EXPR_MAPPING: | |
| 1773 | 4 | return range_expr_value_low(rop, expr->left); | |
| 1774 | 3073936 | case EXPR_SET_ELEM: | |
| 1775 | 3073936 | return range_expr_value_low(rop, expr->key); | |
| 1776 | ✗ | default: | |
| 1777 | ✗ | BUG("invalid range expression type %s", expr_name(expr)); | |
| 1778 | } | ||
| 1779 | } | ||
| 1780 | |||
| 1781 | 6108319 | void range_expr_value_high(mpz_t rop, const struct expr *expr) | |
| 1782 | { | ||
| 1783 | mpz_t tmp; | ||
| 1784 | |||
| 1785 |
6/7✓ Branch 0 (2→3) taken 827 times.
✓ Branch 1 (2→4) taken 3052998 times.
✓ Branch 2 (2→5) taken 25 times.
✓ Branch 3 (2→11) taken 771 times.
✓ Branch 4 (2→12) taken 4 times.
✓ Branch 5 (2→13) taken 3053694 times.
✗ Branch 6 (2→14) not taken.
|
6108319 | switch (expr->etype) { |
| 1786 | 827 | case EXPR_VALUE: | |
| 1787 | 827 | return mpz_set(rop, expr->value); | |
| 1788 | 3052998 | case EXPR_RANGE_VALUE: | |
| 1789 | 3052998 | return mpz_set(rop, expr->range.high); | |
| 1790 | 25 | case EXPR_PREFIX: | |
| 1791 | 25 | range_expr_value_low(rop, expr->prefix); | |
| 1792 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 25 times.
|
25 | assert(expr->len >= expr->prefix_len); |
| 1793 | 25 | mpz_init_bitmask(tmp, expr->len - expr->prefix_len); | |
| 1794 | 25 | mpz_add(rop, rop, tmp); | |
| 1795 | 25 | mpz_clear(tmp); | |
| 1796 | 25 | return; | |
| 1797 | 771 | case EXPR_RANGE: | |
| 1798 | 771 | return range_expr_value_high(rop, expr->right); | |
| 1799 | 4 | case EXPR_MAPPING: | |
| 1800 | 4 | return range_expr_value_high(rop, expr->left); | |
| 1801 | 3053694 | case EXPR_SET_ELEM: | |
| 1802 | 3053694 | return range_expr_value_high(rop, expr->key); | |
| 1803 | ✗ | default: | |
| 1804 | ✗ | BUG("invalid range expression type %s", expr_name(expr)); | |
| 1805 | } | ||
| 1806 | } | ||
| 1807 | |||
| 1808 | − | static const struct expr_ops *__expr_ops_by_type(enum expr_types etype) | |
| 1809 | { | ||
| 1810 | − | switch (etype) { | |
| 1811 | − | case EXPR_INVALID: break; | |
| 1812 | − | case EXPR_VERDICT: return &verdict_expr_ops; | |
| 1813 | − | case EXPR_SYMBOL: return &symbol_expr_ops; | |
| 1814 | − | case EXPR_VARIABLE: return &variable_expr_ops; | |
| 1815 | − | case EXPR_VALUE: return &constant_expr_ops; | |
| 1816 | − | case EXPR_PREFIX: return &prefix_expr_ops; | |
| 1817 | − | case EXPR_RANGE: return &range_expr_ops; | |
| 1818 | − | case EXPR_PAYLOAD: return &payload_expr_ops; | |
| 1819 | − | case EXPR_EXTHDR: return &exthdr_expr_ops; | |
| 1820 | − | case EXPR_META: return &meta_expr_ops; | |
| 1821 | − | case EXPR_SOCKET: return &socket_expr_ops; | |
| 1822 | − | case EXPR_OSF: return &osf_expr_ops; | |
| 1823 | − | case EXPR_CT: return &ct_expr_ops; | |
| 1824 | − | case EXPR_CONCAT: return &concat_expr_ops; | |
| 1825 | − | case EXPR_LIST: return &list_expr_ops; | |
| 1826 | − | case EXPR_SET: return &set_expr_ops; | |
| 1827 | − | case EXPR_SET_REF: return &set_ref_expr_ops; | |
| 1828 | − | case EXPR_SET_ELEM: return &set_elem_expr_ops; | |
| 1829 | − | case EXPR_MAPPING: return &mapping_expr_ops; | |
| 1830 | − | case EXPR_MAP: return &map_expr_ops; | |
| 1831 | − | case EXPR_UNARY: return &unary_expr_ops; | |
| 1832 | − | case EXPR_BINOP: return &binop_expr_ops; | |
| 1833 | − | case EXPR_RELATIONAL: return &relational_expr_ops; | |
| 1834 | − | case EXPR_NUMGEN: return &numgen_expr_ops; | |
| 1835 | − | case EXPR_HASH: return &hash_expr_ops; | |
| 1836 | − | case EXPR_RT: return &rt_expr_ops; | |
| 1837 | − | case EXPR_TUNNEL: return &tunnel_expr_ops; | |
| 1838 | − | case EXPR_FIB: return &fib_expr_ops; | |
| 1839 | − | case EXPR_XFRM: return &xfrm_expr_ops; | |
| 1840 | − | case EXPR_SET_ELEM_CATCHALL: return &set_elem_catchall_expr_ops; | |
| 1841 | − | case EXPR_RANGE_VALUE: return &constant_range_expr_ops; | |
| 1842 | − | case EXPR_RANGE_SYMBOL: return &symbol_range_expr_ops; | |
| 1843 | − | case __EXPR_MAX: break; | |
| 1844 | } | ||
| 1845 | |||
| 1846 | − | return NULL; | |
| 1847 | } | ||
| 1848 | |||
| 1849 | 29532304 | const struct expr_ops *expr_ops(const struct expr *e) | |
| 1850 | { | ||
| 1851 | const struct expr_ops *ops; | ||
| 1852 | |||
| 1853 | 29532304 | ops = __expr_ops_by_type(e->etype); | |
| 1854 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 29532304 times.
|
29532304 | if (!ops) |
| 1855 | ✗ | BUG("Unknown expression type %d", e->etype); | |
| 1856 | |||
| 1857 | 29532304 | return ops; | |
| 1858 | } | ||
| 1859 | |||
| 1860 | 50515 | const struct expr_ops *expr_ops_by_type_u32(uint32_t value) | |
| 1861 | { | ||
| 1862 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 50515 times.
|
50515 | if (value > EXPR_MAX) |
| 1863 | ✗ | return NULL; | |
| 1864 | |||
| 1865 | 50515 | return __expr_ops_by_type(value); | |
| 1866 | } | ||
| 1867 |