GCC Code Coverage Report


Directory: ./
File: src/evaluate.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 3044 3518 86.5%
Functions: 173 177 97.7%
Branches: 1759 2302 76.4%

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 <arpa/inet.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_arp.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <linux/netfilter/nf_synproxy.h>
20 #include <linux/netfilter/nf_nat.h>
21 #include <linux/netfilter/nf_log.h>
22 #include <linux/netfilter_ipv4.h>
23 #include <netinet/ip_icmp.h>
24 #include <netinet/icmp6.h>
25 #include <net/ethernet.h>
26 #include <net/if.h>
27 #include <errno.h>
28
29 #include <expression.h>
30 #include <statement.h>
31 #include <intervals.h>
32 #include <netlink.h>
33 #include <time.h>
34 #include <rule.h>
35 #include <cache.h>
36 #include <erec.h>
37 #include <gmputil.h>
38 #include <utils.h>
39 #include <xt.h>
40
41 252102 struct proto_ctx *eval_proto_ctx(struct eval_ctx *ctx)
42 {
43 252102 uint8_t idx = ctx->inner_desc ? 1 : 0;
44
45 252102 return &ctx->_pctx[idx];
46 }
47
48 static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr);
49
50 static const char * const byteorder_names[] = {
51 [BYTEORDER_INVALID] = "invalid",
52 [BYTEORDER_HOST_ENDIAN] = "host endian",
53 [BYTEORDER_BIG_ENDIAN] = "big endian",
54 };
55
56 #define chain_error(ctx, s1, fmt, args...) \
57 __stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args)
58 #define monitor_error(ctx, s1, fmt, args...) \
59 __stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args)
60 #define cmd_error(ctx, loc, fmt, args...) \
61 __stmt_binary_error(ctx, loc, NULL, fmt, ## args)
62
63 25 static int __fmtstring(3, 4) set_error(struct eval_ctx *ctx,
64 const struct set *set,
65 const char *fmt, ...)
66 {
67 struct error_record *erec;
68 va_list ap;
69
70 25 va_start(ap, fmt);
71 25 erec = erec_vcreate(EREC_ERROR, &set->location, fmt, ap);
72 25 va_end(ap);
73 25 erec_queue(erec, ctx->msgs);
74 25 return -1;
75 }
76
77 36 const char *stmt_name(const struct stmt *stmt)
78 {
79
2/2
✓ Branch 0 (2→3) taken 14 times.
✓ Branch 1 (2→9) taken 22 times.
36 switch (stmt->type) {
80 14 case STMT_NAT:
81
2/5
✓ Branch 0 (3→4) taken 6 times.
✓ Branch 1 (3→5) taken 8 times.
✗ Branch 2 (3→6) not taken.
✗ Branch 3 (3→7) not taken.
✗ Branch 4 (3→8) not taken.
14 switch (stmt->nat.type) {
82 6 case NFT_NAT_SNAT:
83 6 return "snat";
84 8 case NFT_NAT_DNAT:
85 8 return "dnat";
86 ✗ case NFT_NAT_REDIR:
87 ✗ return "redirect";
88 ✗ case NFT_NAT_MASQ:
89 ✗ return "masquerade";
90 }
91 ✗ break;
92 22 default:
93 22 break;
94 }
95
96 22 return stmt_ops(stmt)->name;
97 }
98
99 7 static int stmt_error_range(struct eval_ctx *ctx, const struct stmt *stmt, const struct expr *e)
100 {
101 7 return expr_error(ctx->msgs, e, "%s: range argument not supported", stmt_name(stmt));
102 }
103
104 972 static void key_fix_dtype_byteorder(struct expr *key)
105 {
106 972 const struct datatype *dtype = key->dtype;
107
108
2/2
✓ Branch 0 (2→3) taken 900 times.
✓ Branch 1 (2→4) taken 72 times.
972 if (dtype->byteorder == key->byteorder)
109 900 return;
110
111 72 __datatype_set(key, set_datatype_alloc(dtype, key->byteorder));
112 }
113
114 static int set_evaluate(struct eval_ctx *ctx, struct set *set);
115 4164 static struct expr *implicit_set_declaration(struct eval_ctx *ctx,
116 const char *name,
117 struct expr *key,
118 struct expr *data,
119 struct expr *expr,
120 uint32_t flags)
121 {
122 struct cmd *cmd;
123 struct set *set;
124 struct handle h;
125
126
2/2
✓ Branch 0 (3→4) taken 972 times.
✓ Branch 1 (3→5) taken 3192 times.
4164 if (set_is_datamap(flags))
127 972 key_fix_dtype_byteorder(key);
128
129 4164 set = set_alloc(&expr->location);
130 4164 set->flags = flags;
131 4164 set->handle.set.name = xstrdup(name);
132 4164 set->key = key;
133 4164 set->data = data;
134 4164 set->init = expr;
135 4164 set->automerge = flags & NFT_SET_INTERVAL;
136
137 4164 handle_merge(&set->handle, &ctx->cmd->handle);
138
139
2/2
✓ Branch 0 (9→10) taken 4 times.
✓ Branch 1 (9→14) taken 4160 times.
4164 if (set_evaluate(ctx, set) < 0) {
140
2/2
✓ Branch 0 (10→11) taken 3 times.
✓ Branch 1 (10→12) taken 1 times.
4 if (set->flags & (NFT_SET_MAP|NFT_SET_OBJECT))
141 3 set->init = NULL;
142 4 set_free(set);
143 4 return NULL;
144 }
145
146
1/2
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 4160 times.
4160 if (ctx->table != NULL)
147 ✗ list_add_tail(&set->list, &ctx->table->sets);
148 else {
149 4160 memset(&h, 0, sizeof(h));
150 4160 handle_merge(&h, &set->handle);
151 4160 h.set.location = expr->location;
152 4160 cmd = cmd_alloc(CMD_ADD, CMD_OBJ_SET, &h, &expr->location, set);
153 4160 cmd->location = set->location;
154 4160 list_add_tail(&cmd->list, &ctx->cmd->list);
155 }
156
157 4160 return set_ref_expr_alloc(&expr->location, set);
158 }
159
160 948 static enum ops byteorder_conversion_op(struct expr *expr,
161 enum byteorder byteorder)
162 {
163
2/3
✓ Branch 0 (2→3) taken 888 times.
✓ Branch 1 (2→6) taken 60 times.
✗ Branch 2 (2→9) not taken.
948 switch (expr->byteorder) {
164 888 case BYTEORDER_HOST_ENDIAN:
165
1/2
✓ Branch 0 (3→4) taken 888 times.
✗ Branch 1 (3→5) not taken.
888 if (byteorder == BYTEORDER_BIG_ENDIAN)
166 888 return OP_HTON;
167 ✗ break;
168 60 case BYTEORDER_BIG_ENDIAN:
169
1/2
✓ Branch 0 (6→7) taken 60 times.
✗ Branch 1 (6→8) not taken.
60 if (byteorder == BYTEORDER_HOST_ENDIAN)
170 60 return OP_NTOH;
171 ✗ break;
172 ✗ default:
173 ✗ break;
174 }
175 ✗ BUG("invalid byte order conversion %u => %u",
176 expr->byteorder, byteorder);
177 }
178
179 395895 static int byteorder_conversion(struct eval_ctx *ctx, struct expr **expr,
180 enum byteorder byteorder)
181 {
182 enum datatypes basetype;
183 enum ops op;
184
185
3/4
✓ Branch 0 (3→4) taken 392714 times.
✓ Branch 1 (3→7) taken 3181 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→7) taken 392714 times.
395895 assert(!expr_is_constant(*expr) || expr_is_singleton(*expr));
186
187
2/2
✓ Branch 0 (7→8) taken 391661 times.
✓ Branch 1 (7→9) taken 4234 times.
395895 if ((*expr)->byteorder == byteorder)
188 391661 return 0;
189
190 /* Conversion for EXPR_CONCAT is handled for single composing ranges */
191
2/2
✓ Branch 0 (9→10) taken 368 times.
✓ Branch 1 (9→39) taken 3866 times.
4234 if ((*expr)->etype == EXPR_CONCAT) {
192 struct expr *i, *next, *unary;
193
194
4/6
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 368 times.
✗ Branch 2 (34→35) not taken.
✓ Branch 3 (34→36) taken 1319 times.
✓ Branch 4 (36→13) taken 951 times.
✓ Branch 5 (36→37) taken 368 times.
1319 list_for_each_entry_safe(i, next, &expr_concat(*expr)->expressions, list) {
195
2/2
✓ Branch 0 (13→14) taken 649 times.
✓ Branch 1 (13→15) taken 302 times.
951 if (i->byteorder == BYTEORDER_BIG_ENDIAN)
196 649 continue;
197
198 302 basetype = expr_basetype(i)->type;
199
2/2
✓ Branch 0 (16→17) taken 34 times.
✓ Branch 1 (16→18) taken 268 times.
302 if (basetype == TYPE_STRING)
200 34 continue;
201
202
1/2
✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 268 times.
268 assert(basetype == TYPE_INTEGER);
203
204
2/2
✓ Branch 0 (20→21) taken 16 times.
✓ Branch 1 (20→26) taken 252 times.
268 switch (i->etype) {
205 16 case EXPR_VALUE:
206
1/2
✓ Branch 0 (21→22) taken 16 times.
✗ Branch 1 (21→25) not taken.
16 if (i->byteorder == BYTEORDER_HOST_ENDIAN)
207
1/2
✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 16 times.
16 mpz_switch_byteorder(i->value, div_round_up(i->len, BITS_PER_BYTE));
208 16 break;
209 252 default:
210
2/2
✓ Branch 0 (26→27) taken 137 times.
✓ Branch 1 (26→33) taken 115 times.
252 if (div_round_up(i->len, BITS_PER_BYTE) >= 2) {
211 137 op = byteorder_conversion_op(i, byteorder);
212 137 unary = unary_expr_alloc(&i->location, op, i);
213
1/2
✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→32) taken 137 times.
137 if (expr_evaluate(ctx, &unary) < 0)
214 ✗ return -1;
215
216 137 list_replace(&i->list, &unary->list);
217 }
218 }
219 }
220
221 368 return 0;
222 }
223
224 3866 basetype = expr_basetype(*expr)->type;
225
2/3
✓ Branch 0 (40→41) taken 3851 times.
✓ Branch 1 (40→42) taken 15 times.
✗ Branch 2 (40→43) not taken.
3866 switch (basetype) {
226 3851 case TYPE_INTEGER:
227 3851 break;
228 15 case TYPE_STRING:
229 15 return 0;
230 ✗ default:
231 ✗ return expr_error(ctx->msgs, *expr,
232 "Byteorder mismatch: expected %s, %s got %s",
233 byteorder_names[byteorder],
234 expr_name(*expr),
235 byteorder_names[(*expr)->byteorder]);
236 }
237
238
4/4
✓ Branch 0 (46→47) taken 858 times.
✓ Branch 1 (46→48) taken 2993 times.
✓ Branch 2 (47→48) taken 107 times.
✓ Branch 3 (47→49) taken 751 times.
3851 if (expr_is_constant(*expr) || div_round_up((*expr)->len, BITS_PER_BYTE) < 2)
239 3100 (*expr)->byteorder = byteorder;
240 else {
241 751 op = byteorder_conversion_op(*expr, byteorder);
242 751 *expr = unary_expr_alloc(&(*expr)->location, op, *expr);
243
1/2
✗ Branch 0 (52→53) not taken.
✓ Branch 1 (52→54) taken 751 times.
751 if (expr_evaluate(ctx, expr) < 0)
244 ✗ return -1;
245 }
246 3851 return 0;
247 }
248
249 466 static int table_not_found(struct eval_ctx *ctx)
250 {
251 struct table *table;
252
253 466 table = table_lookup_fuzzy(&ctx->cmd->handle, &ctx->nft->cache);
254
1/2
✓ Branch 0 (3→4) taken 466 times.
✗ Branch 1 (3→7) not taken.
466 if (table == NULL)
255 466 return cmd_error(ctx, &ctx->cmd->handle.table.location,
256 "%s", strerror(ENOENT));
257
258 ✗ return cmd_error(ctx, &ctx->cmd->handle.table.location,
259 "%s; did you mean table '%s' in family %s?",
260 strerror(ENOENT), table->handle.table.name,
261 family2str(table->handle.family));
262 }
263
264 570 static int chain_not_found(struct eval_ctx *ctx)
265 {
266 const struct table *table;
267 struct chain *chain;
268
269 570 chain = chain_lookup_fuzzy(&ctx->cmd->handle, &ctx->nft->cache, &table);
270
1/2
✓ Branch 0 (3→4) taken 570 times.
✗ Branch 1 (3→7) not taken.
570 if (chain == NULL)
271 570 return cmd_error(ctx, &ctx->cmd->handle.chain.location,
272 "%s", strerror(ENOENT));
273
274 ✗ return cmd_error(ctx, &ctx->cmd->handle.chain.location,
275 "%s; did you mean chain '%s' in table %s '%s'?",
276 strerror(ENOENT), chain->handle.chain.name,
277 family2str(chain->handle.family),
278 table->handle.table.name);
279 }
280
281 192 static int set_not_found(struct eval_ctx *ctx, const struct location *loc,
282 const char *set_name)
283 {
284 const struct table *table;
285 struct set *set;
286
287 192 set = set_lookup_fuzzy(set_name, &ctx->nft->cache, &table);
288
2/2
✓ Branch 0 (3→4) taken 169 times.
✓ Branch 1 (3→7) taken 23 times.
192 if (set == NULL)
289 169 return cmd_error(ctx, loc, "%s", strerror(ENOENT));
290
291
2/2
✓ Branch 0 (9→10) taken 4 times.
✓ Branch 1 (9→11) taken 19 times.
23 return cmd_error(ctx, loc,
292 "%s; did you mean %s '%s' in table %s '%s'?",
293 strerror(ENOENT),
294 set_is_map(set->flags) ? "map" : "set",
295 set->handle.set.name,
296 family2str(set->handle.family),
297 table->handle.table.name);
298 }
299
300 ✗ static int flowtable_not_found(struct eval_ctx *ctx, const struct location *loc,
301 const char *ft_name)
302 {
303 const struct table *table;
304 struct flowtable *ft;
305
306 ✗ ft = flowtable_lookup_fuzzy(ft_name, &ctx->nft->cache, &table);
307 ✗ if (!ft)
308 ✗ return cmd_error(ctx, loc, "%s", strerror(ENOENT));
309
310 ✗ return cmd_error(ctx, loc,
311 "%s; did you mean flowtable '%s' in table %s '%s'?",
312 strerror(ENOENT), ft->handle.flowtable.name,
313 family2str(ft->handle.family),
314 table->handle.table.name);
315 }
316
317 /*
318 * Symbol expression: parse symbol and evaluate resulting expression.
319 */
320 1596854 static int expr_evaluate_symbol(struct eval_ctx *ctx, struct expr **expr)
321 {
322 1596854 struct parse_ctx parse_ctx = {
323 1596854 .tbl = &ctx->nft->output.tbl,
324 1596854 .input = &ctx->nft->input,
325 };
326 struct error_record *erec;
327 struct table *table;
328 struct set *set;
329 struct expr *new;
330
331
2/3
✓ Branch 0 (2→3) taken 1593438 times.
✓ Branch 1 (2→9) taken 3416 times.
✗ Branch 2 (2→20) not taken.
1596854 switch ((*expr)->symtype) {
332 1593438 case SYMBOL_VALUE:
333 1593438 datatype_set(*expr, ctx->ectx.dtype);
334 1593438 erec = symbol_parse(&parse_ctx, *expr, &new);
335
2/2
✓ Branch 0 (5→6) taken 89 times.
✓ Branch 1 (5→8) taken 1593349 times.
1593438 if (erec != NULL) {
336 89 erec_queue(erec, ctx->msgs);
337 89 return -1;
338 }
339 1593349 break;
340 3416 case SYMBOL_SET:
341 3416 table = table_cache_find(&ctx->nft->cache.table_cache,
342 3416 ctx->cmd->handle.table.name,
343 3416 ctx->cmd->handle.family);
344
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 3416 times.
3416 if (table == NULL)
345 ✗ return table_not_found(ctx);
346
347 3416 set = set_cache_find(table, (*expr)->identifier);
348
3/4
✓ Branch 0 (14→15) taken 3392 times.
✓ Branch 1 (14→16) taken 24 times.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→18) taken 3392 times.
3416 if (set == NULL || !set->key)
349 24 return set_not_found(ctx, &(*expr)->location,
350 24 (*expr)->identifier);
351
352 3392 new = set_ref_expr_alloc(&(*expr)->location, set);
353 3392 break;
354 }
355
356 1596741 expr_free(*expr);
357 1596741 *expr = new;
358
359 1596741 return expr_evaluate(ctx, expr);
360 }
361
362 88113 static int expr_evaluate_string(struct eval_ctx *ctx, struct expr **exprp)
363 88113 {
364 88113 struct expr *expr = *exprp;
365 88113 unsigned int len = div_round_up(expr->len, BITS_PER_BYTE), datalen;
366 struct expr *value, *prefix;
367
2/2
✓ Branch 0 (2→3) taken 85478 times.
✓ Branch 1 (2→4) taken 2635 times.
88113 int data_len = ctx->ectx.len > 0 ? ctx->ectx.len : len + 1;
368 88113 char data[data_len];
369
370
2/2
✓ Branch 0 (5→6) taken 85478 times.
✓ Branch 1 (5→10) taken 2635 times.
88113 if (ctx->ectx.len > 0) {
371
2/2
✓ Branch 0 (6→7) taken 9 times.
✓ Branch 1 (6→9) taken 85469 times.
85478 if (expr->len > ctx->ectx.len)
372 9 return expr_error(ctx->msgs, expr,
373 "String exceeds maximum length of %u",
374 ctx->ectx.len / BITS_PER_BYTE);
375 85469 expr->len = ctx->ectx.len;
376 }
377
378 88104 memset(data + len, 0, data_len - len);
379
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 88104 times.
88104 mpz_export_data(data, expr->value, BYTEORDER_HOST_ENDIAN, len);
380
381
2/2
✓ Branch 0 (13→14) taken 17 times.
✓ Branch 1 (13→16) taken 88087 times.
88104 if (strlen(data) == 0)
382 17 return expr_error(ctx->msgs, expr,
383 "Empty string is not allowed");
384
385 88087 datalen = strlen(data) - 1;
386
2/2
✓ Branch 0 (16→17) taken 87964 times.
✓ Branch 1 (16→20) taken 123 times.
88087 if (data[datalen] != '*') {
387 /* We need to reallocate the constant expression with the right
388 * expression length to avoid problems on big endian.
389 */
390 87964 value = constant_expr_alloc(&expr->location, ctx->ectx.dtype,
391 BYTEORDER_HOST_ENDIAN,
392 expr->len, data);
393 87964 expr_free(expr);
394 87964 *exprp = value;
395 87964 return 0;
396 }
397
398
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→23) taken 123 times.
123 if (datalen == 0)
399 ✗ return expr_error(ctx->msgs, expr,
400 "All-wildcard strings are not supported");
401
402
2/2
✓ Branch 0 (23→24) taken 42 times.
✓ Branch 1 (23→28) taken 81 times.
123 if (data[datalen - 1] == '\\') {
403 42 char unescaped_str[data_len];
404
405 42 memset(unescaped_str, 0, sizeof(unescaped_str));
406 42 xstrunescape(data, unescaped_str);
407
408 42 value = constant_expr_alloc(&expr->location, ctx->ectx.dtype,
409 BYTEORDER_HOST_ENDIAN,
410 expr->len, unescaped_str);
411 42 expr_free(expr);
412 42 *exprp = value;
413 42 return 0;
414 }
415
416 81 data[datalen] = 0;
417 81 value = constant_expr_alloc(&expr->location, ctx->ectx.dtype,
418 BYTEORDER_HOST_ENDIAN,
419 expr->len, data);
420
421 81 prefix = prefix_expr_alloc(&expr->location, value,
422 datalen * BITS_PER_BYTE);
423 81 datatype_set(prefix, ctx->ectx.dtype);
424 81 prefix->flags |= EXPR_F_CONSTANT;
425 81 prefix->byteorder = BYTEORDER_HOST_ENDIAN;
426 81 prefix->len = expr->len;
427
428 81 expr_free(expr);
429 81 *exprp = prefix;
430 81 return 0;
431 }
432
433 2382318 static int expr_evaluate_integer(struct eval_ctx *ctx, struct expr **exprp)
434 {
435 2382318 struct expr *expr = *exprp;
436 char *valstr, *rangestr;
437 uint32_t masklen;
438 mpz_t mask;
439
440
2/2
✓ Branch 0 (2→3) taken 80 times.
✓ Branch 1 (2→8) taken 2382238 times.
2382318 if (ctx->ectx.maxval > 0 &&
441
2/2
✓ Branch 0 (3→4) taken 13 times.
✓ Branch 1 (3→8) taken 67 times.
80 mpz_cmp_ui(expr->value, ctx->ectx.maxval) > 0) {
442 13 valstr = mpz_get_str(NULL, 10, expr->value);
443 13 expr_error(ctx->msgs, expr,
444 "Value %s exceeds valid range 0-%u",
445 valstr, ctx->ectx.maxval);
446 13 nft_gmp_free(valstr);
447 13 return -1;
448 }
449
450
2/2
✓ Branch 0 (8→9) taken 176 times.
✓ Branch 1 (8→10) taken 2382129 times.
2382305 if (ctx->stmt_len > ctx->ectx.len)
451 176 masklen = ctx->stmt_len;
452 else
453 2382129 masklen = ctx->ectx.len;
454
455 2382305 mpz_init_bitmask(mask, masklen);
456
2/2
✓ Branch 0 (12→13) taken 62 times.
✓ Branch 1 (12→20) taken 2382243 times.
2382305 if (mpz_cmp(expr->value, mask) > 0) {
457 62 valstr = mpz_get_str(NULL, 10, expr->value);
458 62 rangestr = mpz_get_str(NULL, 10, mask);
459 62 expr_error(ctx->msgs, expr,
460 "Value %s exceeds valid range 0-%s",
461 valstr, rangestr);
462 62 nft_gmp_free(valstr);
463 62 nft_gmp_free(rangestr);
464 62 mpz_clear(mask);
465 62 return -1;
466 }
467 2382243 expr->byteorder = ctx->ectx.byteorder;
468 2382243 expr->len = masklen;
469 2382243 mpz_clear(mask);
470 2382243 return 0;
471 }
472
473 2470432 static int expr_evaluate_value(struct eval_ctx *ctx, struct expr **expr)
474 {
475
3/3
✓ Branch 0 (3→4) taken 2382318 times.
✓ Branch 1 (3→8) taken 88113 times.
✓ Branch 2 (3→12) taken 1 times.
2470432 switch (expr_basetype(*expr)->type) {
476 2382318 case TYPE_INTEGER:
477
2/2
✓ Branch 0 (5→6) taken 75 times.
✓ Branch 1 (5→7) taken 2382243 times.
2382318 if (expr_evaluate_integer(ctx, expr) < 0)
478 75 return -1;
479 2382243 break;
480 88113 case TYPE_STRING:
481
2/2
✓ Branch 0 (9→10) taken 26 times.
✓ Branch 1 (9→11) taken 88087 times.
88113 if (expr_evaluate_string(ctx, expr) < 0)
482 26 return -1;
483 88087 break;
484 1 default:
485 1 return expr_error(ctx->msgs, *expr, "Unexpected datatype %s",
486 (*expr)->dtype->name);
487 }
488 2470330 return 0;
489 }
490
491 /*
492 * Primary expressions determine the datatype context.
493 */
494 266510 static int expr_evaluate_primary(struct eval_ctx *ctx, struct expr **expr)
495 {
496 266510 __expr_set_context(&ctx->ectx, (*expr)->dtype, (*expr)->byteorder,
497 266510 (*expr)->len, 0);
498 266510 return 0;
499 }
500
501 12529 int stmt_dependency_evaluate(struct eval_ctx *ctx, struct stmt *stmt)
502 {
503 12529 uint32_t stmt_len = ctx->stmt_len;
504
505
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 12529 times.
12529 if (stmt_evaluate(ctx, stmt) < 0)
506 ✗ return stmt_error(ctx, stmt, "dependency statement is invalid");
507
508 12529 ctx->stmt_len = stmt_len;
509
510 12529 return 0;
511 }
512
513 static int
514 1298 ll_conflict_resolution_gen_dependency(struct eval_ctx *ctx, int protocol,
515 const struct expr *expr,
516 struct stmt **res)
517 {
518 1298 enum proto_bases base = expr->payload.base;
519 const struct proto_hdr_template *tmpl;
520 1298 const struct proto_desc *desc = NULL;
521 struct expr *dep, *left, *right;
522 struct proto_ctx *pctx;
523 struct stmt *stmt;
524
525
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1298 times.
1298 assert(expr->payload.base == PROTO_BASE_LL_HDR);
526
527 1298 pctx = eval_proto_ctx(ctx);
528 1298 desc = pctx->protocol[base].desc;
529 1298 tmpl = &desc->templates[desc->protocol_key];
530 1298 left = payload_expr_alloc(&expr->location, desc, desc->protocol_key);
531
532 1298 right = constant_expr_alloc(&expr->location, tmpl->dtype,
533 1298 tmpl->dtype->byteorder, tmpl->len,
534 constant_data_ptr(protocol, tmpl->len));
535
536 1298 dep = relational_expr_alloc(&expr->location, OP_EQ, left, right);
537 1298 stmt = expr_stmt_alloc(&dep->location, dep);
538
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 1298 times.
1298 if (stmt_dependency_evaluate(ctx, stmt) < 0)
539 ✗ return expr_error(ctx->msgs, expr,
540 "dependency statement is invalid");
541
542
2/2
✓ Branch 0 (13→14) taken 30 times.
✓ Branch 1 (13→15) taken 1268 times.
1298 if (ctx->inner_desc)
543 30 left->payload.inner_desc = ctx->inner_desc;
544
545 1298 *res = stmt;
546 1298 return 0;
547 }
548
549 2311 static uint8_t expr_offset_shift(const struct expr *expr, unsigned int offset,
550 unsigned int *extra_len)
551 {
552 unsigned int new_offset, len;
553 int shift;
554
555 2311 new_offset = offset % BITS_PER_BYTE;
556 2311 len = round_up(expr->len, BITS_PER_BYTE);
557 2311 shift = len - (new_offset + expr->len);
558
2/2
✓ Branch 0 (4→3) taken 120 times.
✓ Branch 1 (4→5) taken 2311 times.
2431 while (shift < 0) {
559 120 shift += BITS_PER_BYTE;
560 120 *extra_len += BITS_PER_BYTE;
561 }
562 2311 return shift;
563 }
564
565 2150 static int expr_evaluate_bits(struct eval_ctx *ctx, struct expr **exprp)
566 {
567 2150 struct expr *expr = *exprp, *and, *mask, *rshift, *off;
568 2150 unsigned masklen, len = expr->len, extra_len = 0;
569 enum byteorder byteorder;
570 uint8_t shift;
571 mpz_t bitmask;
572
573 /* payload statement with relational expression as a value does not
574 * require the transformations that are needed for payload matching,
575 * skip this.
576 */
577
3/4
✓ Branch 0 (2→3) taken 2150 times.
✗ Branch 1 (2→5) not taken.
✓ Branch 2 (3→4) taken 125 times.
✓ Branch 3 (3→5) taken 2025 times.
2150 if (ctx->stmt && ctx->stmt->type == STMT_PAYLOAD)
578 125 return 0;
579
580
2/3
✓ Branch 0 (5→6) taken 1914 times.
✓ Branch 1 (5→8) taken 111 times.
✗ Branch 2 (5→10) not taken.
2025 switch (expr->etype) {
581 1914 case EXPR_PAYLOAD:
582 1914 shift = expr_offset_shift(expr, expr->payload.offset,
583 &extra_len);
584 1914 break;
585 111 case EXPR_EXTHDR:
586 111 shift = expr_offset_shift(expr, expr->exthdr.offset,
587 &extra_len);
588 111 break;
589 ✗ default:
590 ✗ BUG("Unknown expression %s", expr_name(expr));
591 }
592
593 2025 masklen = len + shift;
594
595
2/2
✓ Branch 0 (13→14) taken 1 times.
✓ Branch 1 (13→16) taken 2024 times.
2025 if (masklen > NFT_REG_SIZE * BITS_PER_BYTE)
596 1 return expr_error(ctx->msgs, expr, "mask length %u exceeds allowed maximum of %u\n",
597 masklen, NFT_REG_SIZE * BITS_PER_BYTE);
598
599 2024 mpz_init2(bitmask, masklen);
600 2024 mpz_bitmask(bitmask, len);
601 2024 mpz_lshift_ui(bitmask, shift);
602
603 2024 mask = constant_expr_alloc(&expr->location, expr_basetype(expr),
604 BYTEORDER_HOST_ENDIAN, masklen, NULL);
605 2024 mpz_set(mask->value, bitmask);
606 2024 mpz_clear(bitmask);
607
608 2024 and = binop_expr_alloc(&expr->location, OP_AND, expr, mask);
609 2024 and->dtype = expr->dtype;
610 2024 and->byteorder = expr->byteorder;
611 2024 and->len = masklen;
612
613
2/2
✓ Branch 0 (24→25) taken 563 times.
✓ Branch 1 (24→36) taken 1461 times.
2024 if (shift) {
614
4/4
✓ Branch 0 (25→26) taken 527 times.
✓ Branch 1 (25→27) taken 36 times.
✓ Branch 2 (26→27) taken 94 times.
✓ Branch 3 (26→31) taken 433 times.
563 if ((ctx->ectx.key || ctx->stmt_len > 0) &&
615
2/2
✓ Branch 0 (27→28) taken 60 times.
✓ Branch 1 (27→31) taken 70 times.
130 div_round_up(masklen, BITS_PER_BYTE) > 1) {
616 60 int op = byteorder_conversion_op(expr, BYTEORDER_HOST_ENDIAN);
617 60 and = unary_expr_alloc(&expr->location, op, and);
618 60 and->len = masklen;
619 60 byteorder = BYTEORDER_HOST_ENDIAN;
620 } else {
621 503 byteorder = expr->byteorder;
622 }
623
624 563 off = constant_expr_alloc(&expr->location,
625 expr_basetype(expr),
626 BYTEORDER_HOST_ENDIAN,
627 sizeof(shift), &shift);
628
629 563 rshift = binop_expr_alloc(&expr->location, OP_RSHIFT, and, off);
630 563 rshift->dtype = expr->dtype;
631 563 rshift->byteorder = byteorder;
632 563 rshift->len = masklen;
633
634 563 *exprp = rshift;
635 } else
636 1461 *exprp = and;
637
638
2/2
✓ Branch 0 (37→38) taken 93 times.
✓ Branch 1 (37→39) taken 1931 times.
2024 if (extra_len)
639 93 expr->len += extra_len;
640
641 2024 return 0;
642 }
643
644 − static int __expr_evaluate_exthdr(struct eval_ctx *ctx, struct expr **exprp)
645 {
646 − const struct expr *key = ctx->ectx.key;
647 − struct expr *expr = *exprp;
648
649 − if (expr->exthdr.flags & NFT_EXTHDR_F_PRESENT)
650 − datatype_set(expr, &boolean_type);
651
652 − if (expr_evaluate_primary(ctx, exprp) < 0)
653 − return -1;
654
655 − ctx->ectx.key = key;
656
657 − if (expr->exthdr.offset % BITS_PER_BYTE != 0 ||
658 − expr->len % BITS_PER_BYTE != 0) {
659 − int err = expr_evaluate_bits(ctx, exprp);
660
661 − if (err)
662 − return err;
663 }
664
665 − switch (expr->exthdr.op) {
666 − case NFT_EXTHDR_OP_TCPOPT: {
667 static const unsigned int max_tcpoptlen = (15 * 4 - 20) * BITS_PER_BYTE;
668 unsigned int totlen;
669
670 − totlen = expr->exthdr.tmpl->len + expr->exthdr.offset;
671
672 − if (totlen > max_tcpoptlen)
673 − return expr_error(ctx->msgs, expr,
674 "offset and size %u exceeds max tcp headerlen (%u)",
675 totlen, max_tcpoptlen);
676 − break;
677 }
678 − case NFT_EXTHDR_OP_IPV4: {
679 static const unsigned int max_ipoptlen = 40 * BITS_PER_BYTE;
680 unsigned int totlen;
681
682 − totlen = expr->exthdr.offset + expr->exthdr.tmpl->len;
683
684 − if (totlen > max_ipoptlen)
685 − return expr_error(ctx->msgs, expr,
686 "offset and size %u exceeds max ip option len (%u)",
687 totlen, max_ipoptlen);
688 − break;
689 }
690 − default:
691 − break;
692 }
693
694 − return 0;
695 }
696
697 /*
698 * Exthdr expression: check whether dependencies are fulfilled, otherwise
699 * generate the necessary relational expression and prepend it to the current
700 * statement.
701 */
702 1338 static int expr_evaluate_exthdr(struct eval_ctx *ctx, struct expr **exprp)
703 {
704 1338 const struct proto_desc *base, *dependency = NULL;
705 1338 enum proto_bases pb = PROTO_BASE_NETWORK_HDR;
706 1338 struct expr *expr = *exprp;
707 struct proto_ctx *pctx;
708 struct stmt *nstmt;
709
710
3/3
✓ Branch 0 (2→3) taken 617 times.
✓ Branch 1 (2→4) taken 5 times.
✓ Branch 2 (2→5) taken 716 times.
1338 switch (expr->exthdr.op) {
711 617 case NFT_EXTHDR_OP_TCPOPT:
712 case NFT_EXTHDR_OP_SCTP:
713 case NFT_EXTHDR_OP_DCCP:
714 617 return __expr_evaluate_exthdr(ctx, exprp);
715 5 case NFT_EXTHDR_OP_IPV4:
716 5 dependency = &proto_ip;
717 5 break;
718 716 case NFT_EXTHDR_OP_IPV6:
719 default:
720 716 dependency = &proto_ip6;
721 716 break;
722 }
723
724
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 721 times.
721 assert(dependency);
725
726 721 pctx = eval_proto_ctx(ctx);
727 721 base = pctx->protocol[pb].desc;
728
2/2
✓ Branch 0 (9→10) taken 321 times.
✓ Branch 1 (9→12) taken 400 times.
721 if (base == dependency)
729 321 return __expr_evaluate_exthdr(ctx, exprp);
730
731
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→15) taken 400 times.
400 if (base)
732 ✗ return expr_error(ctx->msgs, expr,
733 "cannot use exthdr with %s", base->name);
734
735
1/2
✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 400 times.
400 if (exthdr_gen_dependency(ctx, expr, dependency, pb - 1, &nstmt) < 0)
736 ✗ return -1;
737
738 400 list_add(&nstmt->list, &ctx->rule->stmts);
739
740 400 return __expr_evaluate_exthdr(ctx, exprp);
741 }
742
743 /* dependency supersede.
744 *
745 * 'inet' is a 'phony' l2 dependency used by NFPROTO_INET to fulfil network
746 * header dependency, i.e. ensure that 'ip saddr 1.2.3.4' only sees ip headers.
747 *
748 * If a match expression that depends on a particular L2 header, e.g. ethernet,
749 * is used, we thus get a conflict since we already have a l2 header dependency.
750 *
751 * But in the inet case we can just ignore the conflict since only another
752 * restriction is added, and these are not mutually exclusive.
753 *
754 * Example: inet filter in ip saddr 1.2.3.4 ether saddr a:b:c:d:e:f
755 *
756 * ip saddr adds meta dependency on ipv4 packets
757 * ether saddr adds another dependency on ethernet frames.
758 */
759 2894 static int meta_iiftype_gen_dependency(struct eval_ctx *ctx,
760 struct expr *payload, struct stmt **res)
761 {
762 struct stmt *nstmt;
763 uint16_t type;
764
765
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 2894 times.
2894 if (proto_dev_type(payload->payload.desc, &type) < 0)
766 ✗ return expr_error(ctx->msgs, payload,
767 "protocol specification is invalid "
768 "for this family");
769
770 2894 nstmt = meta_stmt_meta_iiftype(&payload->location, type);
771
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 2894 times.
2894 if (stmt_dependency_evaluate(ctx, nstmt) < 0)
772 ✗ return -1;
773
774
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 2894 times.
2894 if (ctx->inner_desc)
775 ✗ nstmt->expr->left->meta.inner_desc = ctx->inner_desc;
776
777 2894 *res = nstmt;
778 2894 return 0;
779 }
780
781 4035 static bool proto_is_dummy(const struct proto_desc *desc)
782 {
783
4/4
✓ Branch 0 (2→3) taken 3880 times.
✓ Branch 1 (2→4) taken 155 times.
✓ Branch 2 (3→4) taken 2739 times.
✓ Branch 3 (3→5) taken 1141 times.
4035 return desc == &proto_inet || desc == &proto_netdev;
784 }
785
786 12076 static int stmt_dep_conflict(struct eval_ctx *ctx, const struct stmt *nstmt)
787 {
788 struct stmt *stmt;
789
790
1/2
✓ Branch 0 (19→3) taken 15423 times.
✗ Branch 1 (19→20) not taken.
15423 list_for_each_entry(stmt, &ctx->rule->stmts, list) {
791
2/2
✓ Branch 0 (3→4) taken 12064 times.
✓ Branch 1 (3→5) taken 3359 times.
15423 if (stmt == nstmt)
792 12064 break;
793
794
2/2
✓ Branch 0 (5→6) taken 3347 times.
✓ Branch 1 (5→11) taken 12 times.
3359 if (stmt->type != STMT_EXPRESSION ||
795
1/2
✓ Branch 0 (6→7) taken 3347 times.
✗ Branch 1 (6→11) not taken.
3347 stmt->expr->etype != EXPR_RELATIONAL ||
796
2/2
✓ Branch 0 (7→8) taken 3285 times.
✓ Branch 1 (7→11) taken 62 times.
3347 stmt->expr->right->etype != EXPR_VALUE ||
797
2/2
✓ Branch 0 (8→9) taken 623 times.
✓ Branch 1 (8→11) taken 2662 times.
3285 stmt->expr->left->etype != EXPR_PAYLOAD ||
798
2/2
✓ Branch 0 (9→10) taken 134 times.
✓ Branch 1 (9→11) taken 489 times.
623 stmt->expr->left->etype != nstmt->expr->left->etype ||
799
2/2
✓ Branch 0 (10→11) taken 29 times.
✓ Branch 1 (10→12) taken 105 times.
134 stmt->expr->left->len != nstmt->expr->left->len)
800 3254 continue;
801
802
2/2
✓ Branch 0 (12→13) taken 18 times.
✓ Branch 1 (12→16) taken 87 times.
105 if (stmt->expr->left->payload.desc != nstmt->expr->left->payload.desc ||
803
1/2
✓ Branch 0 (13→14) taken 18 times.
✗ Branch 1 (13→16) not taken.
18 stmt->expr->left->payload.inner_desc != nstmt->expr->left->payload.inner_desc ||
804
1/2
✓ Branch 0 (14→15) taken 18 times.
✗ Branch 1 (14→16) not taken.
18 stmt->expr->left->payload.base != nstmt->expr->left->payload.base ||
805
2/2
✓ Branch 0 (15→16) taken 6 times.
✓ Branch 1 (15→17) taken 12 times.
18 stmt->expr->left->payload.offset != nstmt->expr->left->payload.offset)
806 93 continue;
807
808 12 return stmt_binary_error(ctx, stmt, nstmt,
809 "conflicting statements");
810 }
811
812 12064 return 0;
813 }
814
815 12076 static int rule_stmt_dep_add(struct eval_ctx *ctx,
816 struct stmt *nstmt, struct stmt *stmt)
817 {
818 12076 rule_stmt_insert_at(ctx->rule, nstmt, ctx->stmt);
819
820
2/2
✓ Branch 0 (4→5) taken 12 times.
✓ Branch 1 (4→6) taken 12064 times.
12076 if (stmt_dep_conflict(ctx, nstmt) < 0)
821 12 return -1;
822
823 12064 return 0;
824 }
825
826 4035 static int resolve_ll_protocol_conflict(struct eval_ctx *ctx,
827 const struct proto_desc *desc,
828 struct expr *payload)
829 {
830 4035 enum proto_bases base = payload->payload.base;
831 4035 struct stmt *nstmt = NULL;
832 struct proto_ctx *pctx;
833 unsigned int i;
834 int link, err;
835
836
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 4035 times.
4035 assert(base == PROTO_BASE_LL_HDR);
837
838 4035 pctx = eval_proto_ctx(ctx);
839
840
2/2
✓ Branch 0 (6→7) taken 2894 times.
✓ Branch 1 (6→15) taken 1141 times.
4035 if (proto_is_dummy(desc)) {
841
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 2894 times.
2894 if (ctx->inner_desc) {
842 ✗ proto_ctx_update(pctx, PROTO_BASE_LL_HDR, &payload->location, &proto_eth);
843 } else {
844 2894 err = meta_iiftype_gen_dependency(ctx, payload, &nstmt);
845
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 2894 times.
2894 if (err < 0)
846 ✗ return err;
847
848 2894 desc = payload->payload.desc;
849
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→20) taken 2894 times.
2894 if (rule_stmt_dep_add(ctx, nstmt, ctx->stmt) < 0)
850 ✗ return -1;
851 }
852 } else {
853 /* payload desc stored in the L2 header stack? No conflict. */
854
2/2
✓ Branch 0 (19→16) taken 3 times.
✓ Branch 1 (19→20) taken 1138 times.
1141 for (i = 0; i < pctx->stacked_ll_count; i++) {
855
1/2
✓ Branch 0 (16→17) taken 3 times.
✗ Branch 1 (16→18) not taken.
3 if (pctx->stacked_ll[i] == payload->payload.desc)
856 3 return 0;
857 }
858 }
859
860 4032 link = proto_find_num(desc, payload->payload.desc);
861
3/4
✓ Branch 0 (21→22) taken 1278 times.
✓ Branch 1 (21→24) taken 2754 times.
✗ Branch 2 (23→24) not taken.
✓ Branch 3 (23→25) taken 1278 times.
5310 if (link < 0 ||
862 1278 ll_conflict_resolution_gen_dependency(ctx, link, payload, &nstmt) < 0)
863 2754 return 1;
864
865
2/2
✓ Branch 0 (27→26) taken 1278 times.
✓ Branch 1 (27→28) taken 1278 times.
2556 for (i = 0; i < pctx->stacked_ll_count; i++)
866 1278 payload->payload.offset += pctx->stacked_ll[i]->length;
867
868
2/2
✓ Branch 0 (29→30) taken 12 times.
✓ Branch 1 (29→31) taken 1266 times.
1278 if (rule_stmt_dep_add(ctx, nstmt, ctx->stmt) < 0)
869 12 return -1;
870
871 1266 return 0;
872 }
873
874 /*
875 * Payload expression: check whether dependencies are fulfilled, otherwise
876 * generate the necessary relational expression and prepend it to the current
877 * statement.
878 */
879 − static int __expr_evaluate_payload(struct eval_ctx *ctx, struct expr *expr)
880 {
881 − struct expr *payload = expr;
882 − enum proto_bases base = payload->payload.base;
883 const struct proto_desc *desc;
884 struct proto_ctx *pctx;
885 struct stmt *nstmt;
886 int err;
887
888 − if (expr->etype == EXPR_PAYLOAD && expr->payload.is_raw)
889 − return 0;
890
891 − pctx = eval_proto_ctx(ctx);
892 − desc = pctx->protocol[base].desc;
893 − if (desc == NULL) {
894 − if (payload_gen_dependency(ctx, payload, &nstmt) < 0)
895 − return -1;
896
897 − if (rule_stmt_dep_add(ctx, nstmt, ctx->stmt) < 0)
898 − return -1;
899
900 − desc = pctx->protocol[base].desc;
901
902 − if (desc == expr->payload.desc)
903 − goto check_icmp;
904
905 − if (base == PROTO_BASE_LL_HDR) {
906 int link;
907
908 − link = proto_find_num(desc, payload->payload.desc);
909 − if (link < 0 ||
910 − ll_conflict_resolution_gen_dependency(ctx, link, payload, &nstmt) < 0)
911 − return expr_error(ctx->msgs, payload,
912 "conflicting protocols specified: %s vs. %s",
913 desc->name,
914 payload->payload.desc->name);
915
916 − assert(pctx->stacked_ll_count);
917 − payload->payload.offset += pctx->stacked_ll[0]->length;
918
919 − if (rule_stmt_dep_add(ctx, nstmt, ctx->stmt) < 0)
920 − return -1;
921
922 − return 1;
923 }
924 − goto check_icmp;
925 }
926
927 − if (payload->payload.base == desc->base &&
928 − proto_ctx_is_ambiguous(pctx, base)) {
929 − desc = proto_ctx_find_conflict(pctx, base, payload->payload.desc);
930 − assert(desc);
931
932 − return expr_error(ctx->msgs, payload,
933 "conflicting protocols specified: %s vs. %s",
934 desc->name,
935 payload->payload.desc->name);
936 }
937
938 /* No conflict: Same payload protocol as context, adjust offset
939 * if needed.
940 */
941 − if (desc == payload->payload.desc) {
942 const struct proto_hdr_template *tmpl;
943
944 − if (desc->base == PROTO_BASE_LL_HDR) {
945 unsigned int i;
946
947 − for (i = 0; i < pctx->stacked_ll_count; i++)
948 − payload->payload.offset += pctx->stacked_ll[i]->length;
949 }
950 − check_icmp:
951 − if (desc != &proto_icmp && desc != &proto_icmp6)
952 − return 0;
953
954 − tmpl = expr->payload.tmpl;
955
956 − if (!tmpl || !tmpl->icmp_dep)
957 − return 0;
958
959 − if (payload_gen_icmp_dependency(ctx, expr, &nstmt) < 0)
960 − return -1;
961
962 − if (nstmt && rule_stmt_dep_add(ctx, nstmt, ctx->stmt) < 0)
963 − return -1;
964
965 − return 0;
966 }
967 /* If we already have context and this payload is on the same
968 * base, try to resolve the protocol conflict.
969 */
970 − if (base == PROTO_BASE_LL_HDR) {
971 − err = resolve_ll_protocol_conflict(ctx, desc, payload);
972 − if (err <= 0)
973 − return err;
974
975 − desc = pctx->protocol[base].desc;
976 − if (desc == payload->payload.desc)
977 − return 0;
978 }
979 − return expr_error(ctx->msgs, payload,
980 "conflicting %s protocols specified: %s vs. %s",
981 proto_base_names[base],
982 pctx->protocol[base].desc->name,
983 payload->payload.desc->name);
984 }
985
986 16874 static bool payload_needs_adjustment(const struct expr *expr)
987 {
988
2/2
✓ Branch 0 (2→3) taken 14976 times.
✓ Branch 1 (2→4) taken 1898 times.
31850 return expr->payload.offset % BITS_PER_BYTE != 0 ||
989
2/2
✓ Branch 0 (3→4) taken 387 times.
✓ Branch 1 (3→5) taken 14589 times.
14976 expr->len % BITS_PER_BYTE != 0;
990 }
991
992 16603 static int expr_evaluate_payload(struct eval_ctx *ctx, struct expr **exprp)
993 {
994 16603 const struct expr *key = ctx->ectx.key;
995 16603 struct expr *expr = *exprp;
996
997
2/2
✓ Branch 0 (2→3) taken 130 times.
✓ Branch 1 (2→4) taken 16473 times.
16603 if (expr->payload.evaluated)
998 130 return 0;
999
1000
2/2
✓ Branch 0 (5→6) taken 19 times.
✓ Branch 1 (5→7) taken 16454 times.
16473 if (__expr_evaluate_payload(ctx, expr) < 0)
1001 19 return -1;
1002
1003
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 16454 times.
16454 if (expr_evaluate_primary(ctx, exprp) < 0)
1004 ✗ return -1;
1005
1006 16454 ctx->ectx.key = key;
1007
1008
2/2
✓ Branch 0 (11→12) taken 2039 times.
✓ Branch 1 (11→15) taken 14415 times.
16454 if (payload_needs_adjustment(expr)) {
1009 2039 int err = expr_evaluate_bits(ctx, exprp);
1010
1011
2/2
✓ Branch 0 (13→14) taken 1 times.
✓ Branch 1 (13→15) taken 2038 times.
2039 if (err)
1012 1 return err;
1013 }
1014
1015 16453 expr->payload.evaluated = true;
1016
1017 16453 return 0;
1018 }
1019
1020 431 static int expr_evaluate_inner(struct eval_ctx *ctx, struct expr **exprp)
1021 {
1022 431 struct proto_ctx *pctx = eval_proto_ctx(ctx);
1023 431 const struct proto_desc *desc = NULL;
1024 431 struct expr *expr = *exprp;
1025 int ret;
1026
1027
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 431 times.
431 assert(expr->etype == EXPR_PAYLOAD);
1028
1029 431 pctx = eval_proto_ctx(ctx);
1030 431 desc = pctx->protocol[PROTO_BASE_TRANSPORT_HDR].desc;
1031
1032
2/2
✓ Branch 0 (6→7) taken 170 times.
✓ Branch 1 (6→17) taken 261 times.
431 if (desc == NULL &&
1033
2/2
✓ Branch 0 (7→8) taken 160 times.
✓ Branch 1 (7→17) taken 10 times.
170 expr->payload.inner_desc->base < PROTO_BASE_INNER_HDR) {
1034 struct stmt *nstmt;
1035
1036
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 160 times.
160 if (payload_gen_inner_dependency(ctx, expr, &nstmt) < 0)
1037 ✗ return -1;
1038
1039
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 160 times.
160 if (rule_stmt_dep_add(ctx, nstmt, ctx->stmt) < 0)
1040 ✗ return -1;
1041
1042 160 proto_ctx_update(pctx, PROTO_BASE_TRANSPORT_HDR, &expr->location, expr->payload.inner_desc);
1043 }
1044
1045
2/2
✓ Branch 0 (17→18) taken 251 times.
✓ Branch 1 (17→26) taken 180 times.
431 if (expr->payload.inner_desc->base == PROTO_BASE_INNER_HDR) {
1046 251 desc = pctx->protocol[expr->payload.inner_desc->base - 1].desc;
1047
2/2
✓ Branch 0 (18→19) taken 10 times.
✓ Branch 1 (18→21) taken 241 times.
251 if (!desc) {
1048 10 return expr_error(ctx->msgs, expr,
1049 "no transport protocol specified");
1050 }
1051
1052
1/2
✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→25) taken 241 times.
241 if (proto_find_num(desc, expr->payload.inner_desc) < 0) {
1053 ✗ return expr_error(ctx->msgs, expr,
1054 "unexpected transport protocol %s",
1055 desc->name);
1056 }
1057
1058 241 proto_ctx_update(pctx, expr->payload.inner_desc->base, &expr->location,
1059 expr->payload.inner_desc);
1060 }
1061
1062
2/2
✓ Branch 0 (26→27) taken 401 times.
✓ Branch 1 (26→28) taken 20 times.
421 if (expr->payload.base != PROTO_BASE_INNER_HDR)
1063 401 ctx->inner_desc = expr->payload.inner_desc;
1064
1065 421 ret = expr_evaluate_payload(ctx, exprp);
1066
1067 421 return ret;
1068 }
1069
1070 16613 static int expr_evaluate_payload_inner(struct eval_ctx *ctx, struct expr **exprp)
1071 {
1072 int ret;
1073
1074
2/2
✓ Branch 0 (2→3) taken 431 times.
✓ Branch 1 (2→4) taken 16182 times.
16613 if ((*exprp)->payload.inner_desc)
1075 431 ret = expr_evaluate_inner(ctx, exprp);
1076 else
1077 16182 ret = expr_evaluate_payload(ctx, exprp);
1078
1079 16613 return ret;
1080 }
1081
1082 /*
1083 * RT expression: validate protocol dependencies.
1084 */
1085 41 static int expr_evaluate_rt(struct eval_ctx *ctx, struct expr **expr)
1086 {
1087 static const char emsg[] = "cannot determine ip protocol version, use \"ip nexthop\" or \"ip6 nexthop\" instead";
1088 41 struct expr *rt = *expr;
1089 struct proto_ctx *pctx;
1090
1091 41 pctx = eval_proto_ctx(ctx);
1092 41 rt_expr_update_type(pctx, rt);
1093
1094
3/3
✓ Branch 0 (4→5) taken 10 times.
✓ Branch 1 (4→12) taken 7 times.
✓ Branch 2 (4→19) taken 24 times.
41 switch (rt->rt.key) {
1095 10 case NFT_RT_NEXTHOP4:
1096
2/2
✓ Branch 0 (5→6) taken 2 times.
✓ Branch 1 (5→8) taken 8 times.
10 if (rt->dtype != &ipaddr_type)
1097 2 return expr_error(ctx->msgs, rt, "%s", emsg);
1098
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 8 times.
8 if (pctx->family == NFPROTO_IPV6)
1099 ✗ return expr_error(ctx->msgs, rt, "%s nexthop will not match", "ip");
1100 8 break;
1101 7 case NFT_RT_NEXTHOP6:
1102
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→15) taken 7 times.
7 if (rt->dtype != &ip6addr_type)
1103 ✗ return expr_error(ctx->msgs, rt, "%s", emsg);
1104
2/2
✓ Branch 0 (15→16) taken 1 times.
✓ Branch 1 (15→18) taken 6 times.
7 if (pctx->family == NFPROTO_IPV4)
1105 1 return expr_error(ctx->msgs, rt, "%s nexthop will not match", "ip6");
1106 6 break;
1107 24 default:
1108 24 break;
1109 }
1110
1111 38 return expr_evaluate_primary(ctx, expr);
1112 }
1113
1114 4 static int ct_gen_nh_dependency(struct eval_ctx *ctx, struct expr *ct)
1115 {
1116 const struct proto_desc *base, *base_now;
1117 struct expr *left, *right, *dep;
1118 4 struct stmt *nstmt = NULL;
1119 struct proto_ctx *pctx;
1120
1121 4 pctx = eval_proto_ctx(ctx);
1122 4 base_now = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
1123
1124
1/3
✗ Branch 0 (3→4) not taken.
✗ Branch 1 (3→5) not taken.
✓ Branch 2 (3→6) taken 4 times.
4 switch (ct->ct.nfproto) {
1125 ✗ case NFPROTO_IPV4:
1126 ✗ base = &proto_ip;
1127 ✗ break;
1128 ✗ case NFPROTO_IPV6:
1129 ✗ base = &proto_ip6;
1130 ✗ break;
1131 4 default:
1132 4 base = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
1133
2/2
✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→8) taken 2 times.
4 if (base == &proto_ip)
1134 2 ct->ct.nfproto = NFPROTO_IPV4;
1135
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 2 times.
2 else if (base == &proto_ip6)
1136 ✗ ct->ct.nfproto = NFPROTO_IPV6;
1137
1138
2/2
✓ Branch 0 (10→11) taken 3 times.
✓ Branch 1 (10→12) taken 1 times.
4 if (base)
1139 3 break;
1140
1141
1/2
✓ Branch 0 (12→13) taken 1 times.
✗ Branch 1 (12→14) not taken.
1 return expr_error(ctx->msgs, ct,
1142 "cannot determine ip protocol version, use \"ip %1$caddr\" or \"ip6 %1$caddr\" instead",
1143 ct->ct.key == NFT_CT_SRC ? 's' : 'd');
1144 }
1145
1146 /* no additional dependency needed? */
1147
1/2
✓ Branch 0 (16→17) taken 3 times.
✗ Branch 1 (16→18) not taken.
3 if (base == base_now)
1148 3 return 0;
1149
1150 ✗ if (base_now && base_now != base)
1151 ✗ return expr_error(ctx->msgs, ct,
1152 "conflicting dependencies: %s vs. %s\n",
1153 base->name,
1154 pctx->protocol[PROTO_BASE_NETWORK_HDR].desc->name);
1155 ✗ switch (pctx->family) {
1156 ✗ case NFPROTO_IPV4:
1157 case NFPROTO_IPV6:
1158 ✗ return 0;
1159 }
1160
1161 ✗ left = ct_expr_alloc(&ct->location, NFT_CT_L3PROTOCOL, ct->ct.direction);
1162
1163 ✗ right = constant_expr_alloc(&ct->location, left->dtype,
1164 ✗ left->dtype->byteorder, left->len,
1165 ✗ constant_data_ptr(ct->ct.nfproto, left->len));
1166 ✗ dep = relational_expr_alloc(&ct->location, OP_EQ, left, right);
1167
1168 ✗ relational_expr_pctx_update(pctx, dep);
1169
1170 ✗ nstmt = expr_stmt_alloc(&dep->location, dep);
1171
1172 ✗ if (rule_stmt_dep_add(ctx, nstmt, ctx->stmt) < 0)
1173 ✗ return -1;
1174
1175 ✗ return 0;
1176 }
1177
1178 /*
1179 * CT expression: update the protocol dependant types bases on the protocol
1180 * context.
1181 */
1182 997 static int expr_evaluate_ct(struct eval_ctx *ctx, struct expr **expr)
1183 {
1184 const struct proto_desc *base, *error;
1185 997 struct expr *ct = *expr;
1186 struct proto_ctx *pctx;
1187
1188 997 pctx = eval_proto_ctx(ctx);
1189 997 base = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
1190
1191
4/4
✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→8) taken 28 times.
✓ Branch 2 (3→11) taken 2 times.
✓ Branch 3 (3→14) taken 963 times.
997 switch (ct->ct.key) {
1192 4 case NFT_CT_SRC:
1193 case NFT_CT_DST:
1194
2/2
✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→7) taken 3 times.
4 if (ct_gen_nh_dependency(ctx, ct) < 0)
1195 1 return -1;
1196 3 break;
1197 28 case NFT_CT_SRC_IP:
1198 case NFT_CT_DST_IP:
1199
2/2
✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→10) taken 27 times.
28 if (base == &proto_ip6) {
1200 1 error = &proto_ip;
1201 1 goto err_conflict;
1202 }
1203 27 break;
1204 2 case NFT_CT_SRC_IP6:
1205 case NFT_CT_DST_IP6:
1206
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 2 times.
2 if (base == &proto_ip) {
1207 ✗ error = &proto_ip6;
1208 ✗ goto err_conflict;
1209 }
1210 2 break;
1211 963 default:
1212 963 break;
1213 }
1214
1215 995 ct_expr_update_type(pctx, ct);
1216
1217 995 return expr_evaluate_primary(ctx, expr);
1218
1219 1 err_conflict:
1220 1 return stmt_binary_error(ctx, ct,
1221 &pctx->protocol[PROTO_BASE_NETWORK_HDR],
1222 "conflicting protocols specified: %s vs. %s",
1223 base->name, error->name);
1224 }
1225
1226 /*
1227 * Prefix expression: the argument must be a constant value of integer or
1228 * string base type; the prefix length must be less than or equal to the type
1229 * width.
1230 */
1231 282146 static int expr_evaluate_prefix(struct eval_ctx *ctx, struct expr **expr)
1232 {
1233 282146 struct expr *prefix = *expr, *base, *and, *mask;
1234
1235
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 282146 times.
282146 if (expr_evaluate(ctx, &prefix->prefix) < 0)
1236 ✗ return -1;
1237 282146 base = prefix->prefix;
1238
1239
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 282146 times.
282146 if (!expr_is_constant(base))
1240 ✗ return expr_error(ctx->msgs, prefix,
1241 "Prefix expression is undefined for "
1242 "non-constant expressions");
1243
1244
1/2
✓ Branch 0 (10→11) taken 282146 times.
✗ Branch 1 (10→12) not taken.
282146 switch (expr_basetype(base)->type) {
1245 282146 case TYPE_INTEGER:
1246 case TYPE_STRING:
1247 282146 break;
1248 ✗ default:
1249 ✗ return expr_error(ctx->msgs, prefix,
1250 "Prefix expression is undefined for "
1251 "%s types", base->dtype->desc);
1252 }
1253
1254
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→16) taken 282146 times.
282146 if (prefix->prefix_len > base->len)
1255 ✗ return expr_error(ctx->msgs, prefix,
1256 "Prefix length %u is invalid for type "
1257 "of %u bits width",
1258 prefix->prefix_len, base->len);
1259
1260 /* Clear the uncovered bits of the base value */
1261 282146 mask = constant_expr_alloc(&prefix->location, expr_basetype(base),
1262 BYTEORDER_HOST_ENDIAN, base->len, NULL);
1263
2/3
✓ Branch 0 (19→20) taken 282139 times.
✓ Branch 1 (19→22) taken 7 times.
✗ Branch 2 (19→24) not taken.
282146 switch (expr_basetype(base)->type) {
1264 282139 case TYPE_INTEGER:
1265 282139 mpz_prefixmask(mask->value, base->len, prefix->prefix_len);
1266 282139 break;
1267 7 case TYPE_STRING:
1268 7 mpz_bitmask(mask->value, prefix->prefix_len);
1269 7 break;
1270 }
1271 282146 and = binop_expr_alloc(&prefix->location, OP_AND, base, mask);
1272 282146 prefix->prefix = and;
1273
2/2
✓ Branch 0 (26→27) taken 3 times.
✓ Branch 1 (26→28) taken 282143 times.
282146 if (expr_evaluate(ctx, &prefix->prefix) < 0)
1274 3 return -1;
1275 282143 base = prefix->prefix;
1276
1277 /* expr_evaluate may simplify EXPR_AND to another
1278 * prefix expression for inputs like "2.2.2.2.3*1"/80.
1279 *
1280 * Recurse until all the expressions have been simplified.
1281 * This also gets us the error checks for the expression
1282 * chain.
1283 */
1284
2/2
✓ Branch 0 (28→29) taken 2 times.
✓ Branch 1 (28→31) taken 282141 times.
282143 if (base->etype == EXPR_PREFIX)
1285 2 return expr_evaluate_prefix(ctx, &prefix->prefix);
1286
1/2
✗ Branch 0 (32→33) not taken.
✓ Branch 1 (32→34) taken 282141 times.
282141 assert(expr_is_constant(base));
1287
1288 282141 prefix->dtype = datatype_get(base->dtype);
1289 282141 prefix->byteorder = base->byteorder;
1290 282141 prefix->len = base->len;
1291 282141 prefix->flags |= EXPR_F_CONSTANT;
1292 282141 return 0;
1293 }
1294
1295 /*
1296 * Range expression: both sides must be constants of integer base type.
1297 */
1298 34407 static int expr_evaluate_range_expr(struct eval_ctx *ctx,
1299 const struct expr *range,
1300 struct expr **expr)
1301 {
1302
2/2
✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→5) taken 34406 times.
34407 if (expr_evaluate(ctx, expr) < 0)
1303 1 return -1;
1304
1305
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 34406 times.
34406 if (expr_basetype(*expr)->type != TYPE_INTEGER)
1306 ✗ return expr_binary_error(ctx->msgs, *expr, range,
1307 "Range expression is undefined for "
1308 ✗ "%s types", (*expr)->dtype->desc);
1309
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 34406 times.
34406 if (!expr_is_constant(*expr))
1310 ✗ return expr_binary_error(ctx->msgs, *expr, range,
1311 "Range is not constant");
1312 34406 return 0;
1313 }
1314
1315 − static int __expr_evaluate_range(struct eval_ctx *ctx, struct expr **expr)
1316 {
1317 − struct expr *range = *expr;
1318
1319 − if (expr_evaluate_range_expr(ctx, range, &range->left) < 0)
1320 − return -1;
1321 − if (expr_evaluate_range_expr(ctx, range, &range->right) < 0)
1322 − return -1;
1323
1324 − return 0;
1325 }
1326
1327 17099 static int expr_evaluate_range(struct eval_ctx *ctx, struct expr **exprp)
1328 {
1329 17099 struct expr *range = *exprp, *left, *right;
1330 int rc;
1331
1332 17099 rc = __expr_evaluate_range(ctx, exprp);
1333
2/2
✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→5) taken 17098 times.
17099 if (rc)
1334 1 return rc;
1335
1336 17098 left = range->left;
1337 17098 right = range->right;
1338
1339
2/2
✓ Branch 0 (5→6) taken 3 times.
✓ Branch 1 (5→8) taken 17095 times.
17098 if (mpz_cmp(left->value, right->value) > 0)
1340 3 return expr_error(ctx->msgs, range, "Range negative size");
1341
1342
2/2
✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→12) taken 17094 times.
17095 if (mpz_cmp(left->value, right->value) == 0) {
1343 1 *exprp = expr_get(left);
1344 1 expr_free(range);
1345 1 return 0;
1346 }
1347
1348 17094 datatype_set(range, left->dtype);
1349 17094 range->flags |= EXPR_F_CONSTANT;
1350 17094 return 0;
1351 }
1352
1353 /*
1354 * Unary expressions: unary expressions are only generated internally for
1355 * byteorder conversion of non-constant numerical expressions.
1356 */
1357 888 static int expr_evaluate_unary(struct eval_ctx *ctx, struct expr **expr)
1358 {
1359 888 struct expr *unary = *expr, *arg = unary->arg;
1360 enum byteorder byteorder;
1361
1362 /* unary expression arguments has already been evaluated. */
1363
1364
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 888 times.
888 assert(!expr_is_constant(arg));
1365
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 888 times.
888 assert(expr_basetype(arg)->type == TYPE_INTEGER);
1366
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 888 times.
888 assert(arg->etype != EXPR_UNARY);
1367
1368
1/3
✓ Branch 0 (10→11) taken 888 times.
✗ Branch 1 (10→14) not taken.
✗ Branch 2 (10→17) not taken.
888 switch (unary->op) {
1369 888 case OP_HTON:
1370
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 888 times.
888 assert(arg->byteorder == BYTEORDER_HOST_ENDIAN);
1371 888 byteorder = BYTEORDER_BIG_ENDIAN;
1372 888 break;
1373 ✗ case OP_NTOH:
1374 ✗ assert(arg->byteorder == BYTEORDER_BIG_ENDIAN);
1375 ✗ byteorder = BYTEORDER_HOST_ENDIAN;
1376 ✗ break;
1377 ✗ default:
1378 ✗ BUG("invalid unary operation %u", unary->op);
1379 }
1380
1381 888 __datatype_set(unary, datatype_clone(arg->dtype));
1382 888 unary->byteorder = byteorder;
1383 888 unary->len = arg->len;
1384 888 return 0;
1385 }
1386
1387 /*
1388 * Binops
1389 */
1390 284315 static int constant_binop_simplify(struct eval_ctx *ctx, struct expr **expr)
1391 {
1392 284315 struct expr *op = *expr, *left = (*expr)->left, *right = (*expr)->right;
1393 struct expr *new;
1394 mpz_t val, mask;
1395
1396
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 284315 times.
284315 assert(left->etype == EXPR_VALUE);
1397
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 284315 times.
284315 assert(right->etype == EXPR_VALUE);
1398
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 284315 times.
284315 assert(left->byteorder == right->byteorder);
1399
1400 284315 mpz_init2(val, op->len);
1401 284315 mpz_init_bitmask(mask, op->len);
1402
1403
4/6
✓ Branch 0 (10→11) taken 282190 times.
✓ Branch 1 (10→13) taken 74 times.
✓ Branch 2 (10→16) taken 1054 times.
✓ Branch 3 (10→19) taken 997 times.
✗ Branch 4 (10→26) not taken.
✗ Branch 5 (10→33) not taken.
284315 switch (op->op) {
1404 282190 case OP_AND:
1405 282190 mpz_and(val, left->value, right->value);
1406 282190 mpz_and(val, val, mask);
1407 282190 break;
1408 74 case OP_XOR:
1409 74 mpz_xor(val, left->value, right->value);
1410 74 mpz_and(val, val, mask);
1411 74 break;
1412 1054 case OP_OR:
1413 1054 mpz_ior(val, left->value, right->value);
1414 1054 mpz_and(val, val, mask);
1415 1054 break;
1416 997 case OP_LSHIFT:
1417
1/2
✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→21) taken 997 times.
997 assert(left->byteorder == BYTEORDER_HOST_ENDIAN);
1418 997 mpz_set(val, left->value);
1419 997 mpz_lshift_ui(val, mpz_get_uint32(right->value));
1420 997 mpz_and(val, val, mask);
1421 997 break;
1422 ✗ case OP_RSHIFT:
1423 ✗ assert(left->byteorder == BYTEORDER_HOST_ENDIAN);
1424 ✗ mpz_set(val, left->value);
1425 ✗ mpz_and(val, val, mask);
1426 ✗ mpz_rshift_ui(val, mpz_get_uint32(right->value));
1427 ✗ break;
1428 ✗ default:
1429 ✗ BUG("invalid binary operation %u", op->op);
1430 }
1431
1432 284315 new = constant_expr_alloc(&op->location, op->dtype, op->byteorder,
1433 op->len, NULL);
1434 284315 mpz_set(new->value, val);
1435
1436 284315 expr_free(*expr);
1437 284315 *expr = new;
1438
1439 284315 mpz_clear(mask);
1440 284315 mpz_clear(val);
1441
1442 284315 return expr_evaluate(ctx, expr);
1443 }
1444
1445 1075 static int expr_evaluate_shift(struct eval_ctx *ctx, struct expr **expr)
1446 {
1447 1075 struct expr *op = *expr, *left = op->left, *right = op->right;
1448 unsigned int shift, max_shift_len;
1449
1450 /* mpz_get_uint32 has assert() for huge values */
1451
2/2
✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→5) taken 1074 times.
1075 if (mpz_cmp_ui(right->value, UINT_MAX) > 0)
1452 1 return expr_binary_error(ctx->msgs, right, left,
1453 "shifts exceeding %u bits are not supported", UINT_MAX);
1454
1455 1074 shift = mpz_get_uint32(right->value);
1456
2/2
✓ Branch 0 (6→7) taken 48 times.
✓ Branch 1 (6→8) taken 1026 times.
1074 if (ctx->stmt_len > left->len)
1457 48 max_shift_len = ctx->stmt_len;
1458 else
1459 1026 max_shift_len = left->len;
1460
1461
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→15) taken 1074 times.
1074 if (shift >= max_shift_len)
1462 ✗ return expr_binary_error(ctx->msgs, right, left,
1463 "%s shift of %u bits is undefined for type of %u bits width",
1464 ✗ op->op == OP_LSHIFT ? "Left" : "Right",
1465 shift, max_shift_len);
1466
1467 /* Both sides need to be in host byte order */
1468
1/2
✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 1074 times.
1074 if (byteorder_conversion(ctx, &op->left, BYTEORDER_HOST_ENDIAN) < 0)
1469 ✗ return -1;
1470 1074 left = op->left;
1471
1/2
✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→21) taken 1074 times.
1074 if (byteorder_conversion(ctx, &op->right, BYTEORDER_HOST_ENDIAN) < 0)
1472 ✗ return -1;
1473
1474 1074 datatype_set(op, &integer_type);
1475 1074 op->byteorder = BYTEORDER_HOST_ENDIAN;
1476 1074 op->len = max_shift_len;
1477
1478
2/2
✓ Branch 0 (23→24) taken 997 times.
✓ Branch 1 (23→26) taken 77 times.
1074 if (expr_is_constant(left))
1479 997 return constant_binop_simplify(ctx, expr);
1480 77 return 0;
1481 }
1482
1483 284731 static int expr_evaluate_bitwise(struct eval_ctx *ctx, struct expr **expr)
1484 {
1485 284731 struct expr *op = *expr, *left = op->left;
1486 const struct datatype *dtype;
1487 enum byteorder byteorder;
1488 unsigned int max_len;
1489
1490
2/2
✓ Branch 0 (2→3) taken 22 times.
✓ Branch 1 (2→7) taken 284709 times.
284731 if (ctx->stmt_len > left->len) {
1491 22 max_len = ctx->stmt_len;
1492 22 byteorder = BYTEORDER_HOST_ENDIAN;
1493 22 dtype = &integer_type;
1494
1495 /* Both sides need to be in host byte order */
1496
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 22 times.
22 if (byteorder_conversion(ctx, &op->left, BYTEORDER_HOST_ENDIAN) < 0)
1497 ✗ return -1;
1498
1499 22 left = op->left;
1500 } else {
1501 284709 max_len = left->len;
1502 284709 byteorder = left->byteorder;
1503 284709 dtype = left->dtype;
1504 }
1505
1506
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 284731 times.
284731 if (byteorder_conversion(ctx, &op->right, byteorder) < 0)
1507 ✗ return -1;
1508
1509 284731 datatype_set(op, dtype);
1510 284731 op->byteorder = byteorder;
1511 284731 op->len = max_len;
1512
1513
3/4
✓ Branch 0 (13→14) taken 283318 times.
✓ Branch 1 (13→18) taken 1413 times.
✓ Branch 2 (15→16) taken 283318 times.
✗ Branch 3 (15→18) not taken.
284731 if (expr_is_constant(left) && expr_is_constant(op->right))
1514 283318 return constant_binop_simplify(ctx, expr);
1515 1413 return 0;
1516 }
1517
1518 /*
1519 * Binop expression: both sides must be of integer base type. The left-hand side
1520 * may be either constant or non-constant; if it is constant, it must be a
1521 * singleton. For bitwise operations, the right-hand side must be constant if
1522 * the left-hand side is constant; the right-hand side may be constant or
1523 * non-constant, if the left-hand side is non-constant; for shifts, the
1524 * right-hand side must be constant; if it is constant, it must be a singleton.
1525 */
1526 285831 static int expr_evaluate_binop(struct eval_ctx *ctx, struct expr **expr)
1527 {
1528 285831 struct expr *op = *expr, *left, *right;
1529 285831 const char *sym = expr_op_symbols[op->op];
1530 285831 unsigned int max_shift_len = ctx->ectx.len;
1531 285831 int ret = -1;
1532
1533
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 285831 times.
285831 if (ctx->recursion.binop >= USHRT_MAX)
1534 ✗ return expr_binary_error(ctx->msgs, op, NULL,
1535 "Binary operation limit %u reached ",
1536 ✗ ctx->recursion.binop);
1537 285831 ctx->recursion.binop++;
1538
1539
2/2
✓ Branch 0 (6→7) taken 4 times.
✓ Branch 1 (6→8) taken 285827 times.
285831 if (expr_evaluate(ctx, &op->left) < 0)
1540 4 return -1;
1541 285827 left = op->left;
1542
1543
4/4
✓ Branch 0 (8→9) taken 284766 times.
✓ Branch 1 (8→10) taken 1061 times.
✓ Branch 2 (9→10) taken 15 times.
✓ Branch 3 (9→13) taken 284751 times.
285827 if (op->op == OP_LSHIFT || op->op == OP_RSHIFT) {
1544
2/2
✓ Branch 0 (10→11) taken 13 times.
✓ Branch 1 (10→12) taken 1063 times.
1076 if (left->len > max_shift_len)
1545 13 max_shift_len = left->len;
1546
1547 1076 __expr_set_context(&ctx->ectx, &integer_type,
1548 1076 left->byteorder, max_shift_len, 0);
1549 }
1550
1551
2/2
✓ Branch 0 (14→15) taken 19 times.
✓ Branch 1 (14→16) taken 285808 times.
285827 if (expr_evaluate(ctx, &op->right) < 0)
1552 19 return -1;
1553 285808 right = op->right;
1554
1555 /* evaluation expects constant to the right hand side. */
1556
4/4
✓ Branch 0 (17→18) taken 284320 times.
✓ Branch 1 (17→22) taken 1488 times.
✓ Branch 2 (19→20) taken 3 times.
✓ Branch 3 (19→22) taken 284317 times.
285808 if (expr_is_constant(left) && !expr_is_constant(right)) {
1557 3 range_expr_swap_values(op);
1558 3 left = op->left;
1559 3 right = op->right;
1560 }
1561
1562
1/2
✓ Branch 0 (23→24) taken 285808 times.
✗ Branch 1 (23→25) not taken.
285808 switch (expr_basetype(left)->type) {
1563 285808 case TYPE_INTEGER:
1564 case TYPE_STRING:
1565 285808 break;
1566 ✗ default:
1567 ✗ return expr_binary_error(ctx->msgs, left, op,
1568 "Binary operation (%s) is undefined "
1569 "for %s types",
1570 ✗ sym, left->dtype->desc);
1571 }
1572
1573
4/4
✓ Branch 0 (27→28) taken 284317 times.
✓ Branch 1 (27→33) taken 1491 times.
✓ Branch 2 (29→30) taken 1 times.
✓ Branch 3 (29→33) taken 284316 times.
285808 if (expr_is_constant(left) && !expr_is_singleton(left))
1574 1 return expr_binary_error(ctx->msgs, left, op,
1575 "Binary operation (%s) is undefined "
1576 "for %s expressions",
1577 sym, expr_name(left));
1578
1579
2/2
✓ Branch 0 (36→37) taken 1 times.
✓ Branch 1 (36→41) taken 285806 times.
285807 if (!datatype_equal(expr_basetype(left), expr_basetype(right)))
1580 1 return expr_binary_error(ctx->msgs, left, op,
1581 "Binary operation (%s) with different base types "
1582 "(%s vs %s) is not supported",
1583 1 sym, expr_basetype(left)->name, expr_basetype(right)->name);
1584
1585
2/3
✓ Branch 0 (41→42) taken 1075 times.
✓ Branch 1 (41→52) taken 284731 times.
✗ Branch 2 (41→67) not taken.
285806 switch (op->op) {
1586 1075 case OP_LSHIFT:
1587 case OP_RSHIFT:
1588
1/2
✗ Branch 0 (43→44) not taken.
✓ Branch 1 (43→46) taken 1075 times.
1075 if (!expr_is_constant(right))
1589 ✗ return expr_binary_error(ctx->msgs, right, op,
1590 "Right hand side of binary operation "
1591 "(%s) must be constant", sym);
1592
1593
1/2
✗ Branch 0 (47→48) not taken.
✓ Branch 1 (47→51) taken 1075 times.
1075 if (!expr_is_singleton(right))
1594 ✗ return expr_binary_error(ctx->msgs, left, op,
1595 "Binary operation (%s) is undefined "
1596 "for %s expressions",
1597 sym, expr_name(right));
1598
1599 1075 ret = expr_evaluate_shift(ctx, expr);
1600 1075 break;
1601 284731 case OP_AND:
1602 case OP_XOR:
1603 case OP_OR:
1604
3/4
✓ Branch 0 (53→54) taken 283318 times.
✓ Branch 1 (53→58) taken 1413 times.
✗ Branch 2 (55→56) not taken.
✓ Branch 3 (55→58) taken 283318 times.
284731 if (expr_is_constant(left) && !expr_is_constant(right))
1605 ✗ return expr_binary_error(ctx->msgs, right, op,
1606 "Right hand side of binary operation "
1607 "(%s) must be constant", sym);
1608
1609
3/4
✓ Branch 0 (59→60) taken 284652 times.
✓ Branch 1 (59→65) taken 79 times.
✗ Branch 2 (61→62) not taken.
✓ Branch 3 (61→65) taken 284652 times.
284731 if (expr_is_constant(right) && !expr_is_singleton(right))
1610 ✗ return expr_binary_error(ctx->msgs, left, op,
1611 "Binary operation (%s) is undefined "
1612 "for %s expressions",
1613 sym, expr_name(right));
1614
1615 284731 ret = expr_evaluate_bitwise(ctx, expr);
1616 284731 break;
1617 ✗ default:
1618 ✗ BUG("invalid binary operation %u", op->op);
1619 }
1620
1621
1622
1/2
✗ Branch 0 (70→71) not taken.
✓ Branch 1 (70→73) taken 285806 times.
285806 if (ctx->recursion.binop == 0)
1623 ✗ BUG("recursion counter underflow");
1624
1625 /* can't check earlier: evaluate functions might do constant-merging + expr_free.
1626 *
1627 * So once we've evaluate everything check for remaining length of the
1628 * binop chain.
1629 */
1630
2/2
✓ Branch 0 (73→74) taken 284622 times.
✓ Branch 1 (73→82) taken 1184 times.
285806 if (--ctx->recursion.binop == 0) {
1631 284622 unsigned int to_linearize = 0;
1632
1633 284622 op = *expr;
1634
4/6
✓ Branch 0 (79→80) taken 286167 times.
✗ Branch 1 (79→82) not taken.
✓ Branch 2 (80→81) taken 1546 times.
✓ Branch 3 (80→82) taken 284621 times.
✓ Branch 4 (81→75) taken 1546 times.
✗ Branch 5 (81→82) not taken.
286167 while (op && op->etype == EXPR_BINOP && op->left != NULL) {
1635 1546 to_linearize++;
1636 1546 op = op->left;
1637
1638
2/2
✓ Branch 0 (75→76) taken 1 times.
✓ Branch 1 (75→78) taken 1545 times.
1546 if (to_linearize >= NFT_MAX_EXPR_RECURSION)
1639 1 return expr_binary_error(ctx->msgs, op, NULL,
1640 "Binary operation limit %u reached ",
1641 NFT_MAX_EXPR_RECURSION);
1642 }
1643 }
1644
1645 285805 return ret;
1646 }
1647
1648 1536862 static int list_member_evaluate(struct eval_ctx *ctx, struct expr **expr)
1649 {
1650 1536862 struct expr *next = list_entry((*expr)->list.next, struct expr, list);
1651 int err;
1652
1653 /* should never be hit in practice */
1654
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 1536862 times.
1536862 if (ctx->recursion.list >= USHRT_MAX)
1655 ✗ return expr_binary_error(ctx->msgs, next, NULL,
1656 "List limit %u reached ",
1657 ✗ ctx->recursion.list);
1658
1659 1536862 ctx->recursion.list++;
1660
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 1536862 times.
1536862 assert(*expr != next);
1661 1536862 list_del(&(*expr)->list);
1662 1536862 err = expr_evaluate(ctx, expr);
1663 1536862 list_add_tail(&(*expr)->list, &next->list);
1664 1536862 ctx->recursion.list--;
1665 1536862 return err;
1666 }
1667
1668 82116 static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
1669 {
1670 82116 const struct datatype *dtype = ctx->ectx.dtype, *tmp;
1671
2/2
✓ Branch 0 (2→3) taken 81515 times.
✓ Branch 1 (2→4) taken 601 times.
82116 uint32_t type = dtype ? dtype->type : 0, ntype = 0;
1672
2/2
✓ Branch 0 (5→6) taken 81515 times.
✓ Branch 1 (5→7) taken 601 times.
82116 int off = dtype ? dtype->subtypes : 0;
1673 82116 unsigned int flags = EXPR_F_CONSTANT | EXPR_F_SINGLETON;
1674 82116 const struct list_head *expressions = NULL;
1675 82116 struct expr *i, *next, *key = NULL;
1676 82116 const struct expr *key_ctx = NULL;
1677 82116 bool runaway = false;
1678 82116 uint32_t size = 0;
1679
1680
4/4
✓ Branch 0 (8→9) taken 81396 times.
✓ Branch 1 (8→20) taken 720 times.
✓ Branch 2 (9→10) taken 78503 times.
✓ Branch 3 (9→20) taken 2893 times.
82116 if (ctx->ectx.key && ctx->ectx.key->etype == EXPR_CONCAT) {
1681 78503 key_ctx = ctx->ectx.key;
1682
2/4
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 78503 times.
✗ Branch 2 (13→14) not taken.
✓ Branch 3 (13→15) taken 78503 times.
78503 assert(!list_empty(&expr_concat(ctx->ectx.key)->expressions));
1683
1/2
✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 78503 times.
78503 key = list_first_entry(&expr_concat(ctx->ectx.key)->expressions, struct expr, list);
1684
1/2
✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→19) taken 78503 times.
78503 expressions = &expr_concat(ctx->ectx.key)->expressions;
1685 }
1686
1687
4/6
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 82116 times.
✗ Branch 2 (88→89) not taken.
✓ Branch 3 (88→90) taken 250137 times.
✓ Branch 4 (90→23) taken 168026 times.
✓ Branch 5 (90→91) taken 82111 times.
250137 list_for_each_entry_safe(i, next, &expr_concat(*expr)->expressions, list) {
1688 168026 enum byteorder bo = BYTEORDER_INVALID;
1689 168026 unsigned dsize_bytes, dsize = 0;
1690
1691
1/2
✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→26) taken 168026 times.
168026 if (runaway) {
1692 ✗ return expr_binary_error(ctx->msgs, *expr, key_ctx,
1693 "too many concatenation components");
1694 }
1695
1696
2/2
✓ Branch 0 (26→27) taken 62 times.
✓ Branch 1 (26→31) taken 167964 times.
168026 if (i->etype == EXPR_CT &&
1697
2/2
✓ Branch 0 (27→28) taken 61 times.
✓ Branch 1 (27→29) taken 1 times.
62 (i->ct.key == NFT_CT_SRC ||
1698
1/2
✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→31) taken 61 times.
61 i->ct.key == NFT_CT_DST))
1699 1 return expr_error(ctx->msgs, i,
1700 "specify either ip or ip6 for address matching");
1701
1702
1/6
✗ Branch 0 (32→33) not taken.
✓ Branch 1 (32→37) taken 168025 times.
✗ Branch 2 (33→34) not taken.
✗ Branch 3 (33→37) not taken.
✗ Branch 4 (34→35) not taken.
✗ Branch 5 (34→37) not taken.
168025 if (expr_is_constant(*expr) && dtype && off == 0)
1703 ✗ return expr_binary_error(ctx->msgs, i, *expr,
1704 "unexpected concat component, "
1705 "expecting %s",
1706 ✗ dtype->desc);
1707
1708
2/2
✓ Branch 0 (37→38) taken 158667 times.
✓ Branch 1 (37→39) taken 9358 times.
168025 if (key) {
1709 158667 tmp = key->dtype;
1710 158667 dsize = key->len;
1711 158667 bo = key->byteorder;
1712 158667 off--;
1713
4/4
✓ Branch 0 (39→40) taken 7979 times.
✓ Branch 1 (39→41) taken 1379 times.
✓ Branch 2 (40→41) taken 1 times.
✓ Branch 3 (40→42) taken 7978 times.
9358 } else if (dtype == NULL || off == 0) {
1714 1380 tmp = datatype_lookup(TYPE_INVALID);
1715 } else {
1716 7978 tmp = concat_subtype_lookup(type, --off);
1717 7978 dsize = tmp->size;
1718 7978 bo = tmp->byteorder;
1719 }
1720
1721 168025 __expr_set_context(&ctx->ectx, tmp, bo, dsize, 0);
1722 168025 ctx->ectx.key = i;
1723
1724
2/2
✓ Branch 0 (46→47) taken 2 times.
✓ Branch 1 (46→48) taken 168023 times.
168025 if (list_member_evaluate(ctx, &i) < 0)
1725 2 return -1;
1726
1727
2/5
✗ Branch 0 (48→49) not taken.
✓ Branch 1 (48→51) taken 156819 times.
✓ Branch 2 (48→52) taken 11204 times.
✗ Branch 3 (48→56) not taken.
✗ Branch 4 (48→58) not taken.
168023 switch (i->etype) {
1728 ✗ case EXPR_INVALID:
1729 case __EXPR_MAX:
1730 ✗ BUG("Unexpected etype %d", i->etype);
1731 break;
1732 156819 case EXPR_VALUE:
1733 case EXPR_UNARY:
1734 case EXPR_BINOP:
1735 case EXPR_RELATIONAL:
1736 case EXPR_CONCAT:
1737 case EXPR_MAP:
1738 case EXPR_PAYLOAD:
1739 case EXPR_EXTHDR:
1740 case EXPR_META:
1741 case EXPR_RT:
1742 case EXPR_CT:
1743 case EXPR_SET_ELEM:
1744 case EXPR_NUMGEN:
1745 case EXPR_HASH:
1746 case EXPR_FIB:
1747 case EXPR_SOCKET:
1748 case EXPR_OSF:
1749 case EXPR_XFRM:
1750 case EXPR_TUNNEL:
1751 156819 break;
1752 11204 case EXPR_RANGE:
1753 case EXPR_PREFIX:
1754 case EXPR_RANGE_VALUE:
1755 /* allowed on RHS (e.g. th dport . mark { 1-65535 . 42 }
1756 * ~~~~~~~~ allowed
1757 * but not on LHS (e.g 1-4 . mark { ...}
1758 * ~~~ illegal
1759 *
1760 * recursion.list > 0 means that the concatenation is
1761 * part of another expression, such as EXPR_MAPPING or
1762 * EXPR_SET_ELEM (is used as RHS).
1763 */
1764
2/2
✓ Branch 0 (52→53) taken 11203 times.
✓ Branch 1 (52→54) taken 1 times.
11204 if (ctx->recursion.list > 0)
1765 11203 break;
1766
1767 1 return expr_error(ctx->msgs, i,
1768 "cannot use %s in concatenation",
1769 expr_name(i));
1770 ✗ case EXPR_VERDICT:
1771 case EXPR_SYMBOL:
1772 case EXPR_VARIABLE:
1773 case EXPR_LIST:
1774 case EXPR_SET:
1775 case EXPR_SET_REF:
1776 case EXPR_MAPPING:
1777 case EXPR_SET_ELEM_CATCHALL:
1778 case EXPR_RANGE_SYMBOL:
1779 ✗ return expr_error(ctx->msgs, i,
1780 "cannot use %s in concatenation",
1781 expr_name(i));
1782 }
1783
1784
1/2
✗ Branch 0 (58→59) not taken.
✓ Branch 1 (58→62) taken 168022 times.
168022 if (!i->dtype)
1785 ✗ return expr_error(ctx->msgs, i,
1786 "cannot use %s in concatenation, lacks datatype",
1787 expr_name(i));
1788
1789 168022 flags &= i->flags;
1790
1791
4/4
✓ Branch 0 (62→63) taken 9356 times.
✓ Branch 1 (62→66) taken 158666 times.
✓ Branch 2 (63→64) taken 91 times.
✓ Branch 3 (63→66) taken 9265 times.
168022 if (!key && i->dtype->type == TYPE_INTEGER) {
1792 struct datatype *clone;
1793
1794 91 clone = datatype_clone(i->dtype);
1795 91 clone->size = i->len;
1796 91 clone->byteorder = i->byteorder;
1797 91 __datatype_set(i, clone);
1798 }
1799
1800
3/4
✓ Branch 0 (66→67) taken 1378 times.
✓ Branch 1 (66→70) taken 166644 times.
✗ Branch 2 (67→68) not taken.
✓ Branch 3 (67→70) taken 1378 times.
168022 if (dtype == NULL && i->dtype->size == 0)
1801 ✗ return expr_binary_error(ctx->msgs, i, *expr,
1802 "can not use variable sized "
1803 "data types (%s) in concat "
1804 "expressions",
1805 ✗ i->dtype->name);
1806
2/2
✓ Branch 0 (70→71) taken 1378 times.
✓ Branch 1 (70→72) taken 166644 times.
168022 if (dsize == 0) /* reload after evaluation or clone above */
1807 1378 dsize = i->dtype->size;
1808
1809 168022 ntype = concat_subtype_add(ntype, i->dtype->type);
1810
1811 168022 dsize_bytes = div_round_up(dsize, BITS_PER_BYTE);
1812
2/4
✗ Branch 0 (73→74) not taken.
✓ Branch 1 (73→75) taken 168022 times.
✗ Branch 2 (75→76) not taken.
✓ Branch 3 (75→77) taken 168022 times.
168022 expr_concat(*expr)->field_len[expr_concat(*expr)->field_count++] = dsize_bytes;
1813 168022 size += netlink_padded_len(dsize);
1814
3/4
✓ Branch 0 (78→79) taken 158666 times.
✓ Branch 1 (78→84) taken 9356 times.
✓ Branch 2 (79→80) taken 158666 times.
✗ Branch 3 (79→84) not taken.
168022 if (key && expressions) {
1815
2/2
✓ Branch 0 (81→82) taken 78502 times.
✓ Branch 1 (81→83) taken 80164 times.
158666 if (list_is_last(&key->list, expressions))
1816 78502 runaway = true;
1817 else
1818 80164 key = list_next_entry(key, list);
1819 }
1820
1821 168022 ctx->inner_desc = NULL;
1822
1823
2/2
✓ Branch 0 (84→85) taken 1 times.
✓ Branch 1 (84→87) taken 168021 times.
168022 if (size > NFT_MAX_EXPR_LEN_BITS)
1824 1 return expr_error(ctx->msgs, i, "Concatenation of size %u exceeds maximum size of %lu",
1825 size, NFT_MAX_EXPR_LEN_BITS);
1826 }
1827
1828 82111 (*expr)->flags |= flags;
1829 82111 __datatype_set(*expr, concat_type_alloc(ntype));
1830 82111 (*expr)->len = size;
1831
1832
1/2
✗ Branch 0 (93→94) not taken.
✓ Branch 1 (93→96) taken 82111 times.
82111 if (off > 0)
1833 ✗ return expr_error(ctx->msgs, *expr,
1834 "datatype mismatch, expected %s, "
1835 "expression has type %s",
1836 dtype->desc, (*expr)->dtype->desc);
1837
1838 82111 expr_set_context(&ctx->ectx, (*expr)->dtype, (*expr)->len);
1839
2/2
✓ Branch 0 (97→98) taken 3609 times.
✓ Branch 1 (97→99) taken 78502 times.
82111 if (!key_ctx)
1840 3609 ctx->ectx.key = *expr;
1841 else
1842 78502 ctx->ectx.key = key_ctx;
1843
1844 82111 return 0;
1845 }
1846
1847 167 static int expr_evaluate_list(struct eval_ctx *ctx, struct expr **expr)
1848 {
1849 167 struct expr *list = *expr, *new, *i, *next;
1850 mpz_t val;
1851
1852 167 mpz_init_set_ui(val, 0);
1853
4/6
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 167 times.
✗ Branch 2 (21→22) not taken.
✓ Branch 3 (21→23) taken 576 times.
✓ Branch 4 (23→6) taken 410 times.
✓ Branch 5 (23→24) taken 166 times.
576 list_for_each_entry_safe(i, next, &expr_list(list)->expressions, list) {
1854
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 410 times.
410 if (list_member_evaluate(ctx, &i) < 0) {
1855 ✗ mpz_clear(val);
1856 ✗ return -1;
1857 }
1858
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→14) taken 410 times.
410 if (i->etype != EXPR_VALUE) {
1859 ✗ mpz_clear(val);
1860 ✗ return expr_error(ctx->msgs, i,
1861 "List member must be a constant "
1862 "value");
1863 }
1864
2/2
✓ Branch 0 (15→16) taken 1 times.
✓ Branch 1 (15→19) taken 409 times.
410 if (datatype_basetype(i->dtype)->type != TYPE_BITMASK) {
1865 1 mpz_clear(val);
1866 1 return expr_error(ctx->msgs, i,
1867 "Basetype of type %s is not bitmask",
1868 i->dtype->desc);
1869 }
1870 409 mpz_ior(val, val, i->value);
1871 }
1872
1873 166 new = constant_expr_alloc(&list->location, ctx->ectx.dtype,
1874 BYTEORDER_HOST_ENDIAN, ctx->ectx.len, NULL);
1875 166 mpz_set(new->value, val);
1876 166 mpz_clear(val);
1877
1878 166 expr_free(*expr);
1879 166 *expr = new;
1880 166 return 0;
1881 }
1882
1883 − static int __expr_evaluate_set_elem(struct eval_ctx *ctx, struct expr *elem)
1884 {
1885 − int num_elem_exprs = 0, num_set_exprs = 0;
1886 − struct set *set = ctx->set;
1887 struct stmt *stmt;
1888
1889 − list_for_each_entry(stmt, &elem->stmt_list, list)
1890 − num_elem_exprs++;
1891 − list_for_each_entry(stmt, &set->stmt_list, list)
1892 − num_set_exprs++;
1893
1894 − if (num_elem_exprs > 0) {
1895 struct stmt *set_stmt, *elem_stmt;
1896
1897 − if (num_set_exprs > 0 && num_elem_exprs != num_set_exprs) {
1898 − return expr_error(ctx->msgs, elem,
1899 "number of statements mismatch, set expects %d "
1900 "but element has %d", num_set_exprs,
1901 num_elem_exprs);
1902 − } else if (num_set_exprs == 0) {
1903 − if (!(set->flags & NFT_SET_ANONYMOUS) &&
1904 − !(set->flags & NFT_SET_EVAL)) {
1905 − elem_stmt = list_first_entry(&elem->stmt_list, struct stmt, list);
1906 − return stmt_error(ctx, elem_stmt,
1907 "missing statement in %s declaration",
1908 set_is_map(set->flags) ? "map" : "set");
1909 }
1910 − return 0;
1911 }
1912
1913 − set_stmt = list_first_entry(&set->stmt_list, struct stmt, list);
1914
1915 − list_for_each_entry(elem_stmt, &elem->stmt_list, list) {
1916 − if (set_stmt->type != elem_stmt->type) {
1917 − return stmt_error(ctx, elem_stmt,
1918 "statement mismatch, element expects %s, "
1919 "but %s has type %s",
1920 stmt_name(elem_stmt),
1921 set_is_map(set->flags) ? "map" : "set",
1922 stmt_name(set_stmt));
1923 }
1924 − set_stmt = list_next_entry(set_stmt, list);
1925 }
1926 }
1927
1928 − return 0;
1929 }
1930
1931 1549344 static bool datatype_compatible(const struct datatype *a, const struct datatype *b)
1932 {
1933
2/2
✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→9) taken 9349 times.
9350 return (a->type == TYPE_MARK &&
1934
4/4
✓ Branch 0 (2→3) taken 9350 times.
✓ Branch 1 (2→7) taken 1539994 times.
✓ Branch 2 (8→9) taken 1539988 times.
✓ Branch 3 (8→10) taken 7 times.
3098688 datatype_equal(datatype_basetype(a), datatype_basetype(b))) ||
1935 1539995 datatype_equal(a, b);
1936 }
1937
1938 1356743 static bool elem_key_compatible(const struct expr *set_key,
1939 const struct expr *elem_key)
1940 {
1941 /* Catchall element is always compatible with the set key declaration */
1942
2/2
✓ Branch 0 (2→3) taken 84 times.
✓ Branch 1 (2→4) taken 1356659 times.
1356743 if (expr_type_catchall(elem_key))
1943 84 return true;
1944
1945 1356659 return datatype_compatible(set_key->dtype, elem_key->dtype);
1946 }
1947
1948 1367172 static int expr_evaluate_set_elem(struct eval_ctx *ctx, struct expr **expr)
1949 {
1950 1367172 struct expr *elem = *expr;
1951 const struct expr *key;
1952
1953
2/2
✓ Branch 0 (2→3) taken 1356756 times.
✓ Branch 1 (2→8) taken 10416 times.
1367172 if (ctx->set) {
1954
2/2
✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 1356754 times.
1356756 if (__expr_evaluate_set_elem(ctx, elem) < 0)
1955 2 return -1;
1956
1957 1356754 key = ctx->set->key;
1958 1356754 __expr_set_context(&ctx->ectx, key->dtype, key->byteorder, key->len, 0);
1959 1356754 ctx->ectx.key = key;
1960 }
1961
1962
2/2
✓ Branch 0 (9→10) taken 15 times.
✓ Branch 1 (9→11) taken 1367155 times.
1367170 if (expr_evaluate(ctx, &elem->key) < 0)
1963 15 return -1;
1964
1965
2/2
✓ Branch 0 (11→12) taken 1356746 times.
✓ Branch 1 (11→27) taken 10409 times.
1367155 if (ctx->set &&
1966
2/2
✓ Branch 0 (12→13) taken 667427 times.
✓ Branch 1 (12→27) taken 689319 times.
1356746 !(ctx->set->flags & (NFT_SET_ANONYMOUS | NFT_SET_INTERVAL))) {
1967
3/3
✓ Branch 0 (13→14) taken 1 times.
✓ Branch 1 (13→15) taken 68849 times.
✓ Branch 2 (13→26) taken 598577 times.
667427 switch (elem->key->etype) {
1968 1 case EXPR_PREFIX:
1969 case EXPR_RANGE:
1970 case EXPR_RANGE_VALUE:
1971 1 key = elem->key;
1972 1 goto err_missing_flag;
1973 68849 case EXPR_CONCAT:
1974
4/6
✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 68849 times.
✗ Branch 2 (22→23) not taken.
✓ Branch 3 (22→24) taken 206608 times.
✓ Branch 4 (24→18) taken 137761 times.
✓ Branch 5 (24→25) taken 68847 times.
206608 list_for_each_entry(key, &expr_concat(elem->key)->expressions, list) {
1975
2/2
✓ Branch 0 (18→19) taken 2 times.
✓ Branch 1 (18→20) taken 137759 times.
137761 switch (key->etype) {
1976 2 case EXPR_PREFIX:
1977 case EXPR_RANGE:
1978 case EXPR_RANGE_VALUE:
1979 2 goto err_missing_flag;
1980 137759 default:
1981 137759 break;
1982 }
1983 }
1984 68847 break;
1985 598577 default:
1986 598577 break;
1987 }
1988 }
1989
1990
4/4
✓ Branch 0 (27→28) taken 1356743 times.
✓ Branch 1 (27→36) taken 10409 times.
✓ Branch 2 (29→30) taken 2 times.
✓ Branch 3 (29→36) taken 1356741 times.
1367152 if (ctx->set && !elem_key_compatible(ctx->set->key, elem->key))
1991
1/2
✗ Branch 0 (31→32) not taken.
✓ Branch 1 (31→33) taken 2 times.
2 return expr_error(ctx->msgs, elem,
1992 "Element mismatches %s definition, expected %s, not '%s'",
1993 set_is_map(ctx->set->flags) ? "map" : "set",
1994 ctx->set->key->dtype->desc, elem->key->dtype->desc);
1995
1996 1367150 datatype_set(elem, elem->key->dtype);
1997 1367150 elem->len = elem->key->len;
1998 1367150 elem->flags = elem->key->flags;
1999
2000 1367150 return 0;
2001
2002 3 err_missing_flag:
2003
2/2
✓ Branch 0 (40→41) taken 2 times.
✓ Branch 1 (40→42) taken 1 times.
3 return expr_error(ctx->msgs, key,
2004 "You must add 'flags interval' to your %s declaration if you want to add %s elements",
2005 set_is_map(ctx->set->flags) ? "map" : "set", expr_name(key));
2006 }
2007
2008 85 static int expr_evaluate_set_elem_catchall(struct eval_ctx *ctx, struct expr **expr)
2009 {
2010 85 struct expr *elem = *expr;
2011
2012
1/2
✓ Branch 0 (2→3) taken 85 times.
✗ Branch 1 (2→4) not taken.
85 if (ctx->set)
2013 85 elem->len = ctx->set->key->len;
2014
2015 85 return 0;
2016 }
2017
2018 1368395 static const struct expr *expr_set_elem(const struct expr *expr)
2019 {
2020
2/2
✓ Branch 0 (2→3) taken 170431 times.
✓ Branch 1 (2→4) taken 1197964 times.
1368395 if (expr->etype == EXPR_MAPPING)
2021 170431 return expr->left;
2022
2023 1197964 return expr;
2024 }
2025
2026 7154 static int interval_set_eval(struct eval_ctx *ctx, struct set *set,
2027 struct expr *init)
2028 {
2029 int ret;
2030
2031
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 7154 times.
7154 if (!init)
2032 ✗ return 0;
2033
2034 7154 ret = 0;
2035
3/4
✓ Branch 0 (4→5) taken 6052 times.
✓ Branch 1 (4→9) taken 380 times.
✓ Branch 2 (4→10) taken 722 times.
✗ Branch 3 (4→11) not taken.
7154 switch (ctx->cmd->op) {
2036 6052 case CMD_CREATE:
2037 case CMD_ADD:
2038 case CMD_REPLACE:
2039 case CMD_INSERT:
2040
2/2
✓ Branch 0 (5→6) taken 398 times.
✓ Branch 1 (5→7) taken 5654 times.
6052 if (set->automerge) {
2041 398 ret = set_automerge(ctx->msgs, ctx->cmd, set, init,
2042 398 ctx->nft->debug_mask);
2043 } else {
2044 5654 ret = set_overlap(ctx->msgs, set, init);
2045 }
2046 6052 break;
2047 380 case CMD_DELETE:
2048 case CMD_DESTROY:
2049 380 ret = set_delete(ctx->msgs, ctx->cmd, set, init,
2050 380 ctx->nft->debug_mask);
2051 380 break;
2052 722 case CMD_GET:
2053 case CMD_RESET:
2054 722 break;
2055 ✗ default:
2056 ✗ BUG("unhandled op %d", ctx->cmd->op);
2057 break;
2058 }
2059
2060 7154 return ret;
2061 }
2062
2063 5201 static void expr_evaluate_set_ref(struct eval_ctx *ctx, struct expr *expr)
2064 {
2065 5201 struct set *set = expr->set;
2066
2067 5201 expr_set_context(&ctx->ectx, set->key->dtype, set->key->len);
2068 5201 ctx->ectx.key = set->key;
2069 5201 }
2070
2071 33854 static int expr_evaluate_set(struct eval_ctx *ctx, struct expr **expr)
2072 {
2073 33854 struct expr *set = *expr, *i, *next;
2074 const struct expr *elem;
2075
2076
4/6
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 33854 times.
✗ Branch 2 (71→72) not taken.
✓ Branch 3 (71→73) taken 1402236 times.
✓ Branch 4 (73→5) taken 1368427 times.
✓ Branch 5 (73→74) taken 33809 times.
1402236 list_for_each_entry_safe(i, next, &expr_set(set)->expressions, list) {
2077
2/2
✓ Branch 0 (6→7) taken 45 times.
✓ Branch 1 (6→8) taken 1368382 times.
1368427 if (list_member_evaluate(ctx, &i) < 0)
2078 45 return -1;
2079
2080
2/2
✓ Branch 0 (8→9) taken 170433 times.
✓ Branch 1 (8→29) taken 1197949 times.
1368382 if (i->etype == EXPR_MAPPING &&
2081
1/2
✓ Branch 0 (9→10) taken 170433 times.
✗ Branch 1 (9→29) not taken.
170433 i->left->etype == EXPR_SET_ELEM &&
2082
2/2
✓ Branch 0 (10→11) taken 2 times.
✓ Branch 1 (10→29) taken 170431 times.
170433 i->left->key->etype == EXPR_SET) {
2083 struct expr *new, *j;
2084
2085
4/6
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 2 times.
✗ Branch 2 (23→24) not taken.
✓ Branch 3 (23→25) taken 6 times.
✓ Branch 4 (25→14) taken 4 times.
✓ Branch 5 (25→26) taken 2 times.
6 list_for_each_entry(j, &expr_set(i->left->key)->expressions, list) {
2086 4 new = mapping_expr_alloc(&i->location,
2087 expr_get(j),
2088 4 expr_get(i->right));
2089
1/2
✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→19) taken 4 times.
4 list_add_tail(&new->list, &expr_set(set)->expressions);
2090
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 4 times.
4 expr_set(set)->size++;
2091 }
2092 2 list_del(&i->list);
2093 2 expr_free(i);
2094 2 continue;
2095 }
2096
2097 1368380 elem = expr_set_elem(i);
2098
2099
2/2
✓ Branch 0 (30→31) taken 1367118 times.
✓ Branch 1 (30→34) taken 1262 times.
1368380 if (elem->etype == EXPR_SET_ELEM &&
2100
1/2
✗ Branch 0 (31→32) not taken.
✓ Branch 1 (31→34) taken 1367118 times.
1367118 elem->key->etype == EXPR_SET_REF)
2101 ✗ return expr_error(ctx->msgs, i,
2102 "Set reference cannot be part of another set");
2103
2104
2/2
✓ Branch 0 (34→35) taken 1367118 times.
✓ Branch 1 (34→44) taken 1262 times.
1368380 if (elem->etype == EXPR_SET_ELEM &&
2105
2/2
✓ Branch 0 (35→36) taken 15 times.
✓ Branch 1 (35→44) taken 1367103 times.
1367118 elem->key->etype == EXPR_SET) {
2106 15 struct expr *new = expr_get(elem->key);
2107
2108
2/4
✗ Branch 0 (37→38) not taken.
✓ Branch 1 (37→39) taken 15 times.
✗ Branch 2 (39→40) not taken.
✓ Branch 3 (39→41) taken 15 times.
15 expr_set(set)->set_flags |= expr_set(elem->key)->set_flags;
2109 15 list_replace(&i->list, &new->list);
2110 15 expr_free(i);
2111 15 i = new;
2112 15 elem = expr_set_elem(i);
2113 }
2114
2115
1/2
✗ Branch 0 (45→46) not taken.
✓ Branch 1 (45→48) taken 1368380 times.
1368380 if (!expr_is_constant(i))
2116 ✗ return expr_error(ctx->msgs, i,
2117 "Set member is not constant");
2118
2119
2/2
✓ Branch 0 (48→49) taken 1277 times.
✓ Branch 1 (48→62) taken 1367103 times.
1368380 if (i->etype == EXPR_SET) {
2120 /* Merge recursive set definitions */
2121
1/2
✗ Branch 0 (49→50) not taken.
✓ Branch 1 (49→51) taken 1277 times.
1277 list_splice_tail_init(&expr_set(i)->expressions, &i->list);
2122 1277 list_del(&i->list);
2123
2/4
✗ Branch 0 (53→54) not taken.
✓ Branch 1 (53→55) taken 1277 times.
✗ Branch 2 (55→56) not taken.
✓ Branch 3 (55→57) taken 1277 times.
1277 expr_set(set)->size += expr_set(i)->size - 1;
2124
2/4
✗ Branch 0 (57→58) not taken.
✓ Branch 1 (57→59) taken 1277 times.
✗ Branch 2 (59→60) not taken.
✓ Branch 3 (59→61) taken 1277 times.
1277 expr_set(set)->set_flags |= expr_set(i)->set_flags;
2125 1277 expr_free(i);
2126
2/2
✓ Branch 0 (63→64) taken 295494 times.
✓ Branch 1 (63→70) taken 1071609 times.
1367103 } else if (!expr_is_singleton(i)) {
2127
1/2
✗ Branch 0 (64→65) not taken.
✓ Branch 1 (64→66) taken 295494 times.
295494 expr_set(set)->set_flags |= NFT_SET_INTERVAL;
2128
2/2
✓ Branch 0 (66→67) taken 9327 times.
✓ Branch 1 (66→70) taken 286167 times.
295494 if (elem->key->etype == EXPR_CONCAT)
2129
1/2
✗ Branch 0 (67→68) not taken.
✓ Branch 1 (67→69) taken 9327 times.
9327 expr_set(set)->set_flags |= NFT_SET_CONCAT;
2130 }
2131 }
2132
2133
2/2
✓ Branch 0 (74→75) taken 30628 times.
✓ Branch 1 (74→79) taken 3181 times.
33809 if (ctx->set) {
2134
2/2
✓ Branch 0 (75→76) taken 8904 times.
✓ Branch 1 (75→79) taken 21724 times.
30628 if (ctx->set->flags & NFT_SET_CONCAT)
2135
1/2
✗ Branch 0 (76→77) not taken.
✓ Branch 1 (76→78) taken 8904 times.
8904 expr_set(set)->set_flags |= NFT_SET_CONCAT;
2136 }
2137
2138
1/2
✗ Branch 0 (79→80) not taken.
✓ Branch 1 (79→81) taken 33809 times.
33809 expr_set(set)->set_flags |= NFT_SET_CONSTANT;
2139
2140 33809 datatype_set(set, ctx->ectx.dtype);
2141 33809 set->len = ctx->ectx.len;
2142 33809 set->flags |= EXPR_F_CONSTANT;
2143
2144 33809 return 0;
2145 }
2146
2147 static int binop_transfer(struct eval_ctx *ctx, struct expr **expr);
2148
2149 1023 static void map_set_concat_info(struct expr *map)
2150 {
2151
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1023 times.
1023 map->mappings->set->flags |= expr_set(map->mappings->set->init)->set_flags;
2152
2153
2/2
✓ Branch 0 (4→5) taken 155 times.
✓ Branch 1 (4→11) taken 868 times.
1023 if (map->mappings->set->flags & NFT_SET_INTERVAL &&
2154
2/2
✓ Branch 0 (5→6) taken 52 times.
✓ Branch 1 (5→11) taken 103 times.
155 map->map->etype == EXPR_CONCAT) {
2155 52 memcpy(&map->mappings->set->desc.field_len,
2156
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 52 times.
52 &expr_concat(map->map)->field_len,
2157 sizeof(map->mappings->set->desc.field_len));
2158 52 map->mappings->set->desc.field_count =
2159
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 52 times.
52 expr_concat(map->map)->field_count;
2160 52 map->mappings->flags |= NFT_SET_CONCAT;
2161 }
2162 1023 }
2163
2164 − static void __mapping_expr_expand(struct expr *i)
2165 {
2166 struct expr *j, *range, *next;
2167
2168 − assert(i->etype == EXPR_MAPPING);
2169 − switch (i->right->etype) {
2170 − case EXPR_VALUE:
2171 − range = range_expr_alloc(&i->location, expr_get(i->right), expr_get(i->right));
2172 − expr_free(i->right);
2173 − i->right = range;
2174 − break;
2175 − case EXPR_CONCAT:
2176 − list_for_each_entry_safe(j, next, &expr_concat(i->right)->expressions, list) {
2177 − if (j->etype != EXPR_VALUE)
2178 − continue;
2179
2180 − range = range_expr_alloc(&j->location, expr_get(j), expr_get(j));
2181 − list_replace(&j->list, &range->list);
2182 − expr_free(j);
2183 }
2184 − i->right->flags &= ~EXPR_F_SINGLETON;
2185 − break;
2186 − default:
2187 − break;
2188 }
2189 − }
2190
2191 45 static int mapping_expr_expand(struct eval_ctx *ctx)
2192 {
2193 struct expr *i;
2194
2195
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 45 times.
45 if (!set_is_anonymous(ctx->set->flags))
2196 ✗ return 0;
2197
2198
4/6
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 45 times.
✗ Branch 2 (14→15) not taken.
✓ Branch 3 (14→16) taken 115 times.
✓ Branch 4 (16→8) taken 71 times.
✓ Branch 5 (16→17) taken 44 times.
115 list_for_each_entry(i, &expr_set(ctx->set->init)->expressions, list) {
2199
2/2
✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→12) taken 70 times.
71 if (i->etype != EXPR_MAPPING)
2200 1 return expr_error(ctx->msgs, i,
2201 "expected mapping, not %s", expr_name(i));
2202 70 __mapping_expr_expand(i);
2203 }
2204
2205 44 return 0;
2206 }
2207
2208 1357 static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)
2209 {
2210 1357 struct expr *map = *expr, *mappings;
2211 1357 struct expr_ctx ectx = ctx->ectx;
2212 1357 uint32_t set_flags = NFT_SET_MAP;
2213 struct expr *key, *data;
2214
2215
2/2
✓ Branch 0 (2→3) taken 36 times.
✓ Branch 1 (2→7) taken 1321 times.
1357 if (map->map->etype == EXPR_CT &&
2216
1/2
✓ Branch 0 (3→4) taken 36 times.
✗ Branch 1 (3→5) not taken.
36 (map->map->ct.key == NFT_CT_SRC ||
2217
2/2
✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→7) taken 35 times.
36 map->map->ct.key == NFT_CT_DST))
2218 1 return expr_error(ctx->msgs, map->map,
2219 "specify either ip or ip6 for address matching");
2220
2/2
✓ Branch 0 (7→8) taken 327 times.
✓ Branch 1 (7→20) taken 1029 times.
1356 else if (map->map->etype == EXPR_CONCAT) {
2221 struct expr *i;
2222
2223
4/6
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 327 times.
✗ Branch 2 (17→18) not taken.
✓ Branch 3 (17→19) taken 1109 times.
✓ Branch 4 (19→11) taken 783 times.
✓ Branch 5 (19→20) taken 326 times.
1109 list_for_each_entry(i, &expr_concat(map->map)->expressions, list) {
2224
2/2
✓ Branch 0 (11→12) taken 24 times.
✓ Branch 1 (11→16) taken 759 times.
783 if (i->etype == EXPR_CT &&
2225
2/2
✓ Branch 0 (12→13) taken 23 times.
✓ Branch 1 (12→14) taken 1 times.
24 (i->ct.key == NFT_CT_SRC ||
2226
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→16) taken 23 times.
23 i->ct.key == NFT_CT_DST))
2227 1 return expr_error(ctx->msgs, i,
2228 "specify either ip or ip6 for address matching");
2229 }
2230 }
2231
2232 1355 expr_set_context(&ctx->ectx, NULL, 0);
2233
2/2
✓ Branch 0 (22→23) taken 2 times.
✓ Branch 1 (22→24) taken 1353 times.
1355 if (expr_evaluate(ctx, &map->map) < 0)
2234 2 return -1;
2235
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→28) taken 1353 times.
1353 if (expr_is_constant(map->map))
2236 ✗ return expr_error(ctx->msgs, map->map,
2237 "Map expression can not be constant");
2238
2239 1353 ctx->stmt_len = 0;
2240 1353 mappings = map->mappings;
2241
2242
5/5
✓ Branch 0 (28→29) taken 970 times.
✓ Branch 1 (28→32) taken 3 times.
✓ Branch 2 (28→77) taken 256 times.
✓ Branch 3 (28→86) taken 122 times.
✓ Branch 4 (28→91) taken 2 times.
1353 switch (map->mappings->etype) {
2243 970 case EXPR_SET:
2244
1/2
✗ Branch 0 (29→30) not taken.
✓ Branch 1 (29→31) taken 970 times.
970 set_flags |= expr_set(mappings)->set_flags;
2245 /* fallthrough */
2246 973 case EXPR_VARIABLE:
2247
3/4
✓ Branch 0 (32→33) taken 143 times.
✓ Branch 1 (32→35) taken 830 times.
✓ Branch 2 (33→34) taken 143 times.
✗ Branch 3 (33→35) not taken.
973 if (ctx->ectx.key && ctx->ectx.key->etype == EXPR_CONCAT) {
2248 143 key = expr_clone(ctx->ectx.key);
2249 } else {
2250 830 key = constant_expr_alloc(&map->location,
2251 ctx->ectx.dtype,
2252 ctx->ectx.byteorder,
2253 ctx->ectx.len, NULL);
2254 }
2255
2256
2/2
✓ Branch 0 (36→37) taken 1 times.
✓ Branch 1 (36→40) taken 972 times.
973 if (!ectx.dtype) {
2257 1 expr_free(key);
2258 1 return expr_error(ctx->msgs, map,
2259 "Implicit map expression without known datatype");
2260 }
2261
2262
2/2
✓ Branch 0 (40→41) taken 721 times.
✓ Branch 1 (40→42) taken 251 times.
972 if (ectx.dtype->type == TYPE_VERDICT) {
2263 721 data = verdict_expr_alloc(&netlink_location, 0, NULL);
2264 } else {
2265 const struct datatype *dtype;
2266
2267 251 dtype = set_datatype_alloc(ectx.dtype, ectx.byteorder);
2268 251 data = constant_expr_alloc(&netlink_location, dtype,
2269 251 dtype->byteorder, ectx.len, NULL);
2270 251 datatype_free(dtype);
2271 }
2272
2273 972 mappings = implicit_set_declaration(ctx, "__map%d",
2274 key, data,
2275 mappings,
2276 NFT_SET_ANONYMOUS | set_flags);
2277
2/2
✓ Branch 0 (46→47) taken 2 times.
✓ Branch 1 (46→48) taken 970 times.
972 if (!mappings)
2278 2 return -1;
2279
2280
3/4
✓ Branch 0 (48→49) taken 250 times.
✓ Branch 1 (48→52) taken 720 times.
✗ Branch 2 (49→50) not taken.
✓ Branch 3 (49→52) taken 250 times.
970 if (ectx.len && mappings->set->data->len != ectx.len)
2281 ✗ BUG("%d vs %d", mappings->set->data->len, ectx.len);
2282
2283 970 map->mappings = mappings;
2284
2285 970 ctx->set = mappings->set;
2286
2/2
✓ Branch 0 (53→54) taken 3 times.
✓ Branch 1 (53→55) taken 967 times.
970 if (expr_evaluate(ctx, &map->mappings->set->init) < 0)
2287 3 return -1;
2288
2289
2/2
✓ Branch 0 (55→56) taken 1 times.
✓ Branch 1 (55→58) taken 966 times.
967 if (map->mappings->set->init->etype != EXPR_SET) {
2290 1 return expr_error(ctx->msgs, map->mappings->set->init,
2291 "Expression is not a map");
2292 }
2293
2294
3/4
✗ Branch 0 (58→59) not taken.
✓ Branch 1 (58→60) taken 966 times.
✓ Branch 2 (61→62) taken 145 times.
✓ Branch 3 (61→68) taken 821 times.
966 if (set_is_interval(expr_set(map->mappings->set->init)->set_flags) &&
2295
5/6
✗ Branch 0 (62→63) not taken.
✓ Branch 1 (62→64) taken 145 times.
✓ Branch 2 (64→65) taken 98 times.
✓ Branch 3 (64→68) taken 47 times.
✓ Branch 4 (66→67) taken 10 times.
✓ Branch 5 (66→68) taken 88 times.
243 !(expr_set(map->mappings->set->init)->set_flags & NFT_SET_CONCAT) &&
2296 98 interval_set_eval(ctx, ctx->set, map->mappings->set->init) < 0)
2297 10 return -1;
2298
2299 956 expr_set_context(&ctx->ectx, ctx->set->key->dtype, ctx->set->key->len);
2300
1/2
✗ Branch 0 (70→71) not taken.
✓ Branch 1 (70→72) taken 956 times.
956 if (binop_transfer(ctx, expr) < 0)
2301 ✗ return -1;
2302
2303
2/2
✓ Branch 0 (72→73) taken 45 times.
✓ Branch 1 (72→76) taken 911 times.
956 if (ctx->set->data->flags & EXPR_F_INTERVAL) {
2304 45 ctx->set->data->len *= 2;
2305
2306
2/2
✓ Branch 0 (74→75) taken 1 times.
✓ Branch 1 (74→76) taken 44 times.
45 if (mapping_expr_expand(ctx))
2307 1 return -1;
2308 }
2309
2310 955 ctx->set->key->len = ctx->ectx.len;
2311 955 ctx->set = NULL;
2312 955 map = *expr;
2313
2314 955 map_set_concat_info(map);
2315 955 break;
2316 256 case EXPR_SYMBOL:
2317
2/2
✓ Branch 0 (78→79) taken 3 times.
✓ Branch 1 (78→80) taken 253 times.
256 if (expr_evaluate(ctx, &map->mappings) < 0)
2318 3 return -1;
2319
1/2
✓ Branch 0 (80→81) taken 253 times.
✗ Branch 1 (80→83) not taken.
253 if (map->mappings->etype != EXPR_SET_REF ||
2320
1/2
✗ Branch 0 (82→83) not taken.
✓ Branch 1 (82→85) taken 253 times.
253 !set_is_datamap(map->mappings->set->flags))
2321 ✗ return expr_error(ctx->msgs, map->mappings,
2322 "Expression is not a map");
2323 253 break;
2324 122 case EXPR_SET_REF:
2325 /* symbol has been already evaluated to set reference */
2326
2/2
✓ Branch 0 (87→88) taken 1 times.
✓ Branch 1 (87→90) taken 121 times.
122 if (!set_is_map(mappings->set->flags))
2327 1 return expr_error(ctx->msgs, map->mappings,
2328 "Expression is not a map");
2329 121 break;
2330 2 default:
2331 2 return expr_binary_error(ctx->msgs, map->mappings, map->map,
2332 2 "invalid mapping expression %s", expr_name(map->mappings));
2333 }
2334
2335
2/2
✓ Branch 0 (95→96) taken 2 times.
✓ Branch 1 (95→98) taken 1327 times.
1329 if (!datatype_compatible(map->mappings->set->key->dtype, map->map->dtype))
2336 2 return expr_binary_error(ctx->msgs, map->mappings, map->map,
2337 "datatype mismatch, map expects %s, "
2338 "mapping expression has type %s",
2339 2 map->mappings->set->key->dtype->desc,
2340 2 map->map->dtype->desc);
2341
2342 1327 datatype_set(map, map->mappings->set->data->dtype);
2343 1327 map->flags |= EXPR_F_CONSTANT;
2344
2345 /* Data for range lookups needs to be in big endian order */
2346
3/4
✓ Branch 0 (99→100) taken 295 times.
✓ Branch 1 (99→103) taken 1032 times.
✗ Branch 2 (101→102) not taken.
✓ Branch 3 (101→103) taken 295 times.
1622 if (map->mappings->set->flags & NFT_SET_INTERVAL &&
2347 295 byteorder_conversion(ctx, &map->map, BYTEORDER_BIG_ENDIAN) < 0)
2348 ✗ return -1;
2349
2350 1327 return 0;
2351 }
2352
2353 1743 static bool data_mapping_has_interval(struct expr *data)
2354 {
2355 struct expr *i;
2356
2357
1/2
✓ Branch 0 (2→3) taken 1743 times.
✗ Branch 1 (2→5) not taken.
1743 if (data->etype == EXPR_RANGE ||
2358
2/2
✓ Branch 0 (3→4) taken 1725 times.
✓ Branch 1 (3→5) taken 18 times.
1743 data->etype == EXPR_RANGE_VALUE ||
2359
2/2
✓ Branch 0 (4→5) taken 33 times.
✓ Branch 1 (4→6) taken 1692 times.
1725 data->etype == EXPR_PREFIX)
2360 51 return true;
2361
2362
2/2
✓ Branch 0 (6→7) taken 1636 times.
✓ Branch 1 (6→8) taken 56 times.
1692 if (data->etype != EXPR_CONCAT)
2363 1636 return false;
2364
2365
4/6
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 56 times.
✗ Branch 2 (16→17) not taken.
✓ Branch 3 (16→18) taken 146 times.
✓ Branch 4 (18→11) taken 102 times.
✓ Branch 5 (18→19) taken 44 times.
146 list_for_each_entry(i, &expr_concat(data)->expressions, list) {
2366
1/2
✓ Branch 0 (11→12) taken 102 times.
✗ Branch 1 (11→14) not taken.
102 if (i->etype == EXPR_RANGE ||
2367
2/2
✓ Branch 0 (12→13) taken 94 times.
✓ Branch 1 (12→14) taken 8 times.
102 i->etype == EXPR_RANGE_VALUE ||
2368
2/2
✓ Branch 0 (13→14) taken 4 times.
✓ Branch 1 (13→15) taken 90 times.
94 i->etype == EXPR_PREFIX)
2369 12 return true;
2370 }
2371
2372 44 return false;
2373 }
2374
2375 170162 static bool elem_data_compatible(const struct expr *set_data,
2376 const struct expr *elem_data)
2377 {
2378
2/2
✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→5) taken 170161 times.
170162 if (elem_data->etype == EXPR_RANGE) {
2379 /* EXPR_RANGE has invalid_type, use the lhs type.
2380 * It should be impossible to have a EXPR_RANGE where
2381 * lhs and rhs don't have the same dtype.
2382 */
2383 1 return elem_data_compatible(set_data, elem_data->left);
2384 }
2385
2386 170161 return datatype_compatible(set_data->dtype, elem_data->dtype);
2387 }
2388
2389 170458 static int expr_evaluate_mapping(struct eval_ctx *ctx, struct expr **expr)
2390 {
2391 170458 struct expr *mapping = *expr;
2392 170458 struct set *set = ctx->set;
2393 uint32_t datalen;
2394
2395
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 170458 times.
170458 if (set == NULL)
2396 ✗ return expr_error(ctx->msgs, mapping,
2397 "mapping outside of map context");
2398
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 170458 times.
170458 if (!set_is_map(set->flags))
2399 ✗ return set_error(ctx, set, "set is not a map");
2400
2401 170458 expr_set_context(&ctx->ectx, set->key->dtype, set->key->len);
2402
2/2
✓ Branch 0 (11→12) taken 2 times.
✓ Branch 1 (11→13) taken 170456 times.
170458 if (expr_evaluate(ctx, &mapping->left) < 0)
2403 2 return -1;
2404
1/2
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→17) taken 170456 times.
170456 if (!expr_is_constant(mapping->left))
2405 ✗ return expr_error(ctx->msgs, mapping->left,
2406 "Key must be a constant");
2407 170456 mapping->flags |= mapping->left->flags & EXPR_F_SINGLETON;
2408
2409 /* This can happen for malformed map definitions */
2410
2/2
✓ Branch 0 (17→18) taken 1 times.
✓ Branch 1 (17→20) taken 170455 times.
170456 if (!set->data)
2411 1 return set_error(ctx, set, "map has no mapping data");
2412
2413
2/2
✓ Branch 0 (21→22) taken 168709 times.
✓ Branch 1 (21→24) taken 1746 times.
170455 if (!set_is_anonymous(set->flags) &&
2414
2/2
✓ Branch 0 (22→23) taken 22 times.
✓ Branch 1 (22→24) taken 168687 times.
168709 set->data->flags & EXPR_F_INTERVAL)
2415 22 datalen = set->data->len / 2;
2416 else
2417 170433 datalen = set->data->len;
2418 170455 __expr_set_context(&ctx->ectx, set->data->dtype,
2419 170455 set->data->byteorder, datalen, 0);
2420
2421
2/2
✓ Branch 0 (27→28) taken 8 times.
✓ Branch 1 (27→29) taken 170447 times.
170455 if (expr_evaluate(ctx, &mapping->right) < 0)
2422 8 return -1;
2423
1/2
✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→33) taken 170447 times.
170447 if (!expr_is_constant(mapping->right))
2424 ✗ return expr_error(ctx->msgs, mapping->right,
2425 "Value must be a constant");
2426
2427
4/4
✓ Branch 0 (34→35) taken 1743 times.
✓ Branch 1 (34→38) taken 168704 times.
✓ Branch 2 (36→37) taken 63 times.
✓ Branch 3 (36→38) taken 1680 times.
172190 if (set_is_anonymous(set->flags) &&
2428 1743 data_mapping_has_interval(mapping->right))
2429 63 set->data->flags |= EXPR_F_INTERVAL;
2430
2431
2/2
✓ Branch 0 (39→40) taken 168704 times.
✓ Branch 1 (39→42) taken 1743 times.
170447 if (!set_is_anonymous(set->flags) &&
2432
2/2
✓ Branch 0 (40→41) taken 22 times.
✓ Branch 1 (40→42) taken 168682 times.
168704 set->data->flags & EXPR_F_INTERVAL)
2433 22 __mapping_expr_expand(mapping);
2434
2435
2/2
✓ Branch 0 (42→43) taken 170361 times.
✓ Branch 1 (42→47) taken 86 times.
170447 if (!(set->data->flags & EXPR_F_INTERVAL) &&
2436
2/2
✓ Branch 0 (44→45) taken 12 times.
✓ Branch 1 (44→47) taken 170349 times.
170361 !expr_is_singleton(mapping->right))
2437 12 return expr_error(ctx->msgs, mapping->right,
2438 "Value must be a singleton");
2439
2440
4/4
✓ Branch 0 (48→49) taken 274 times.
✓ Branch 1 (48→53) taken 170161 times.
✓ Branch 2 (49→50) taken 1 times.
✓ Branch 3 (49→53) taken 273 times.
170435 if (set_is_objmap(set->flags) && mapping->right->etype != EXPR_VALUE)
2441 1 return expr_error(ctx->msgs, mapping->right,
2442 "Object mapping data should be a value, not %s",
2443 expr_name(mapping->right));
2444
2445
2/2
✓ Branch 0 (54→55) taken 170161 times.
✓ Branch 1 (54→59) taken 273 times.
170434 if (set_is_datamap(set->flags) &&
2446
2/2
✓ Branch 0 (56→57) taken 1 times.
✓ Branch 1 (56→59) taken 170160 times.
170161 !elem_data_compatible(set->data, mapping->right))
2447 1 return expr_error(ctx->msgs, mapping->right,
2448 "Element mapping mismatches map definition, expected %s, not '%s'",
2449 set->data->dtype->desc, mapping->right->dtype->desc);
2450
2451 170433 mapping->flags |= EXPR_F_CONSTANT;
2452 170433 return 0;
2453 }
2454
2455 16999 static struct expr *symbol_range_expand(struct expr *expr)
2456 {
2457 struct expr *left, *right;
2458
2459 /* expand to symbol and range expressions to consolidate evaluation. */
2460 16999 left = symbol_expr_alloc(&expr->location, expr->symtype,
2461 16999 (struct scope *)expr->scope,
2462 expr->identifier_range[0]);
2463 16999 right = symbol_expr_alloc(&expr->location, expr->symtype,
2464 16999 (struct scope *)expr->scope,
2465 expr->identifier_range[1]);
2466 16999 return range_expr_alloc(&expr->location, left, right);
2467 }
2468
2469 16897 static int expr_evaluate_symbol_range(struct eval_ctx *ctx, struct expr **exprp)
2470 {
2471 struct expr *left, *right, *range, *constant_range;
2472 16897 struct expr *expr = *exprp;
2473
2474 /* expand to symbol and range expressions to consolidate evaluation. */
2475 16897 range = symbol_range_expand(expr);
2476
2477
2/2
✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→7) taken 16894 times.
16897 if (expr_evaluate(ctx, &range) < 0) {
2478 3 expr_free(range);
2479 3 return -1;
2480 }
2481
2482
2/2
✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 16893 times.
16894 if (range->etype != EXPR_RANGE)
2483 1 goto out_done;
2484
2485 16893 left = range->left;
2486 16893 right = range->right;
2487
2488
2/2
✓ Branch 0 (9→10) taken 14791 times.
✓ Branch 1 (9→18) taken 2102 times.
16893 if (ctx->set &&
2489
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 14791 times.
14791 left->etype == EXPR_VALUE &&
2490
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 14791 times.
14791 right->etype == EXPR_VALUE) {
2491 14791 constant_range = constant_range_expr_alloc(&expr->location,
2492 left->dtype,
2493 14791 left->byteorder,
2494 left->len,
2495 14791 left->value,
2496 14791 right->value);
2497 14791 expr_free(range);
2498 14791 expr_free(expr);
2499 14791 *exprp = constant_range;
2500 14791 return 0;
2501 }
2502
2503 2102 out_done:
2504 2103 expr_free(expr);
2505 2103 *exprp = range;
2506
2507 2103 return 0;
2508 }
2509
2510 /* We got datatype context via statement. If the basetype is compatible, set
2511 * this expression datatype to the one of the statement to make it datatype
2512 * compatible. This is a more conservative approach than enabling datatype
2513 * compatibility between two different datatypes whose basetype is the same,
2514 * let's revisit this later once users come with valid usecases to generalize
2515 * this.
2516 */
2517 109 static void expr_dtype_integer_compatible(struct eval_ctx *ctx,
2518 struct expr *expr)
2519 {
2520
2/2
✓ Branch 0 (2→3) taken 64 times.
✓ Branch 1 (2→7) taken 45 times.
109 if (ctx->ectx.dtype &&
2521
2/2
✓ Branch 0 (3→4) taken 20 times.
✓ Branch 1 (3→7) taken 44 times.
64 ctx->ectx.dtype->basetype == &integer_type &&
2522
1/2
✓ Branch 0 (4→5) taken 20 times.
✗ Branch 1 (4→7) not taken.
20 ctx->ectx.len == 4 * BITS_PER_BYTE) {
2523 20 datatype_set(expr, ctx->ectx.dtype);
2524 20 expr->len = ctx->ectx.len;
2525 }
2526 109 }
2527
2528 57 static int expr_evaluate_numgen(struct eval_ctx *ctx, struct expr **exprp)
2529 {
2530 57 struct expr *expr = *exprp;
2531 unsigned int maxval;
2532
2533 57 expr_dtype_integer_compatible(ctx, expr);
2534
2535 57 maxval = expr->numgen.mod + expr->numgen.offset - 1;
2536 57 __expr_set_context(&ctx->ectx, expr->dtype, expr->byteorder, expr->len,
2537 maxval);
2538 57 return 0;
2539 }
2540
2541 52 static int expr_evaluate_hash(struct eval_ctx *ctx, struct expr **exprp)
2542 {
2543 52 struct expr *expr = *exprp;
2544 unsigned int maxval;
2545
2546 52 expr_dtype_integer_compatible(ctx, expr);
2547
2548 52 expr_set_context(&ctx->ectx, NULL, 0);
2549
3/4
✓ Branch 0 (4→5) taken 24 times.
✓ Branch 1 (4→8) taken 28 times.
✗ Branch 2 (6→7) not taken.
✓ Branch 3 (6→8) taken 24 times.
76 if (expr->hash.expr &&
2550 24 expr_evaluate(ctx, &expr->hash.expr) < 0)
2551 ✗ return -1;
2552
2553 /* expr_evaluate_primary() sets the context to what to the input
2554 * expression to be hashed. Since this input is transformed to a 4 bytes
2555 * integer, restore context to the datatype that results from hashing.
2556 */
2557 52 maxval = expr->hash.mod + expr->hash.offset - 1;
2558 52 __expr_set_context(&ctx->ectx, expr->dtype, expr->byteorder, expr->len,
2559 maxval);
2560
2561 52 return 0;
2562 }
2563
2564 /*
2565 * Transfer the invertible binops to the constant side of an equality
2566 * expression. A left shift is only invertible if the low n bits are
2567 * zero.
2568 */
2569 2728 static int binop_can_transfer(struct eval_ctx *ctx,
2570 struct expr *left, struct expr *right)
2571 {
2572 int err;
2573
2574
5/5
✓ Branch 0 (2→3) taken 1866 times.
✓ Branch 1 (2→4) taken 741 times.
✓ Branch 2 (2→5) taken 22 times.
✓ Branch 3 (2→9) taken 89 times.
✓ Branch 4 (2→10) taken 10 times.
2728 switch (right->etype) {
2575 1866 case EXPR_VALUE:
2576 1866 break;
2577 741 case EXPR_SET_ELEM:
2578 741 return binop_can_transfer(ctx, left, right->key);
2579 22 case EXPR_RANGE:
2580 22 err = binop_can_transfer(ctx, left, right->left);
2581
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 22 times.
22 if (err <= 0)
2582 ✗ return err;
2583 22 return binop_can_transfer(ctx, left, right->right);
2584 89 case EXPR_MAPPING:
2585 89 return binop_can_transfer(ctx, left, right->left);
2586 10 default:
2587 10 return 0;
2588 }
2589
2590
3/4
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→21) taken 992 times.
✓ Branch 2 (11→28) taken 44 times.
✓ Branch 3 (11→30) taken 830 times.
1866 switch (left->op) {
2591 ✗ case OP_LSHIFT:
2592 ✗ assert(left->right->etype == EXPR_VALUE);
2593 ✗ assert(right->etype == EXPR_VALUE);
2594
2595 ✗ if (mpz_scan1(right->value, 0) < mpz_get_uint32(left->right->value))
2596 ✗ return expr_binary_error(ctx->msgs, right, left,
2597 "Comparison is always false");
2598 ✗ return 1;
2599 992 case OP_RSHIFT:
2600
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 992 times.
992 assert(left->right->etype == EXPR_VALUE);
2601
2/2
✓ Branch 0 (24→25) taken 398 times.
✓ Branch 1 (24→27) taken 594 times.
992 if (ctx->ectx.len < right->len + mpz_get_uint32(left->right->value))
2602 398 ctx->ectx.len += mpz_get_uint32(left->right->value);
2603 992 return 1;
2604 44 case OP_XOR:
2605 44 return expr_is_constant(left->right);
2606 830 default:
2607 830 return 0;
2608 }
2609 }
2610
2611 1746 static int binop_transfer_one(struct eval_ctx *ctx,
2612 const struct expr *left, struct expr **right)
2613 {
2614 int err;
2615
2616
4/5
✓ Branch 0 (2→3) taken 36 times.
✓ Branch 1 (2→4) taken 1032 times.
✓ Branch 2 (2→5) taken 656 times.
✓ Branch 3 (2→6) taken 22 times.
✗ Branch 4 (2→10) not taken.
1746 switch ((*right)->etype) {
2617 36 case EXPR_MAPPING:
2618 36 return binop_transfer_one(ctx, left, &(*right)->left);
2619 1032 case EXPR_VALUE:
2620 1032 break;
2621 656 case EXPR_SET_ELEM:
2622 656 return binop_transfer_one(ctx, left, &(*right)->key);
2623 22 case EXPR_RANGE:
2624 22 err = binop_transfer_one(ctx, left, &(*right)->left);
2625
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 22 times.
22 if (err < 0)
2626 ✗ return err;
2627 22 return binop_transfer_one(ctx, left, &(*right)->right);
2628 ✗ default:
2629 ✗ return 0;
2630 }
2631
2632
2/4
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→15) taken 992 times.
✓ Branch 2 (11→18) taken 40 times.
✗ Branch 3 (11→21) not taken.
1032 switch (left->op) {
2633 ✗ case OP_LSHIFT:
2634 ✗ (*right) = binop_expr_alloc(&(*right)->location, OP_RSHIFT,
2635 ✗ *right, expr_get(left->right));
2636 ✗ break;
2637 992 case OP_RSHIFT:
2638 992 (*right) = binop_expr_alloc(&(*right)->location, OP_LSHIFT,
2639 992 *right, expr_get(left->right));
2640 992 break;
2641 40 case OP_XOR:
2642 40 (*right) = binop_expr_alloc(&(*right)->location, OP_XOR,
2643 40 *right, expr_get(left->right));
2644 40 break;
2645 ✗ default:
2646 ✗ BUG("invalid binary operation %u", left->op);
2647 }
2648
2649 1032 return expr_evaluate(ctx, right);
2650 }
2651
2652 438 static void binop_transfer_handle_lhs(struct expr **expr)
2653 {
2654 438 struct expr *tmp, *left = *expr;
2655 unsigned int shift;
2656
2657
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 438 times.
438 assert(left->etype == EXPR_BINOP);
2658
2659
2/3
✓ Branch 0 (4→5) taken 398 times.
✓ Branch 1 (4→9) taken 40 times.
✗ Branch 2 (4→13) not taken.
438 switch (left->op) {
2660 398 case OP_RSHIFT:
2661 /* Mask out the bits the shift would have masked out */
2662 398 shift = mpz_get_uint8(left->right->value);
2663 398 mpz_bitmask(left->right->value, left->left->len);
2664 398 mpz_lshift_ui(left->right->value, shift);
2665 398 left->op = OP_AND;
2666 398 break;
2667 40 case OP_LSHIFT:
2668 case OP_XOR:
2669 40 tmp = expr_get(left->left);
2670 40 datatype_set(tmp, left->dtype);
2671 40 expr_free(left);
2672 40 *expr = tmp;
2673 40 break;
2674 ✗ default:
2675 ✗ BUG("invalid binop operation %u", left->op);
2676 }
2677 438 }
2678
2679 − static int __binop_transfer(struct eval_ctx *ctx,
2680 struct expr *left, struct expr **right)
2681 {
2682 struct expr *i, *next;
2683 int err;
2684
2685 − assert(left->etype == EXPR_BINOP);
2686
2687 − switch ((*right)->etype) {
2688 − case EXPR_VALUE:
2689 − err = binop_can_transfer(ctx, left, *right);
2690 − if (err <= 0)
2691 − return err;
2692 − if (binop_transfer_one(ctx, left, right) < 0)
2693 − return -1;
2694 − break;
2695 − case EXPR_RANGE:
2696 − err = binop_can_transfer(ctx, left, *right);
2697 − if (err <= 0)
2698 − return err;
2699 − if (binop_transfer_one(ctx, left, right) < 0)
2700 − return -1;
2701 − break;
2702 − case EXPR_SET:
2703 − list_for_each_entry(i, &expr_set(*right)->expressions, list) {
2704 − err = binop_can_transfer(ctx, left, i);
2705 − if (err <= 0)
2706 − return err;
2707 }
2708 − list_for_each_entry_safe(i, next, &expr_set(*right)->expressions, list) {
2709 − list_del(&i->list);
2710 − err = binop_transfer_one(ctx, left, &i);
2711 − list_add_tail(&i->list, &next->list);
2712 − if (err < 0)
2713 − return err;
2714 }
2715 − break;
2716 − case EXPR_SET_REF:
2717 − if (!set_is_anonymous((*right)->set->flags))
2718 − return 0;
2719
2720 − return __binop_transfer(ctx, left, &(*right)->set->init);
2721 − default:
2722 − return 0;
2723 }
2724
2725 − return 1;
2726 }
2727
2728 110318 static int binop_transfer(struct eval_ctx *ctx, struct expr **expr)
2729 {
2730 110318 struct expr *left = (*expr)->left;
2731 int ret;
2732
2733
2/2
✓ Branch 0 (2→3) taken 108995 times.
✓ Branch 1 (2→4) taken 1323 times.
110318 if (left->etype != EXPR_BINOP)
2734 108995 return 0;
2735
2736 1323 ret = __binop_transfer(ctx, left, &(*expr)->right);
2737
2/2
✓ Branch 0 (5→6) taken 885 times.
✓ Branch 1 (5→7) taken 438 times.
1323 if (ret <= 0)
2738 885 return ret;
2739
2740 438 binop_transfer_handle_lhs(&(*expr)->left);
2741 438 return 0;
2742 }
2743
2744 110181 bool lhs_is_meta_hour(const struct expr *meta)
2745 {
2746
2/2
✓ Branch 0 (2→3) taken 14492 times.
✓ Branch 1 (2→4) taken 95689 times.
110181 if (meta->etype != EXPR_META)
2747 14492 return false;
2748
2749
2/2
✓ Branch 0 (4→5) taken 95295 times.
✓ Branch 1 (4→6) taken 394 times.
190984 return meta->meta.key == NFT_META_TIME_HOUR ||
2750
2/2
✓ Branch 0 (5→6) taken 51 times.
✓ Branch 1 (5→7) taken 95244 times.
95295 meta->meta.key == NFT_META_TIME_DAY;
2751 }
2752
2753 114 void range_expr_swap_values(struct expr *range)
2754 {
2755 struct expr *left_tmp;
2756
2757 114 left_tmp = range->left;
2758 114 range->left = range->right;
2759 114 range->right = left_tmp;
2760 114 }
2761
2762 105 static bool range_needs_swap(const struct expr *range)
2763 {
2764 105 const struct expr *right = range->right;
2765 105 const struct expr *left = range->left;
2766
2767 105 return mpz_cmp(left->value, right->value) > 0;
2768 }
2769
2770 38 static void optimize_singleton_set(struct expr *rel, struct expr **expr)
2771 {
2772 38 struct expr *set = rel->right, *i;
2773
2774
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 38 times.
38 i = list_first_entry(&expr_set(set)->expressions, struct expr, list);
2775
3/4
✓ Branch 0 (4→5) taken 38 times.
✗ Branch 1 (4→11) not taken.
✓ Branch 2 (6→7) taken 36 times.
✓ Branch 3 (6→11) taken 2 times.
76 if (i->etype == EXPR_SET_ELEM &&
2776 38 list_empty(&i->stmt_list)) {
2777
2778
1/2
✓ Branch 0 (7→8) taken 36 times.
✗ Branch 1 (7→10) not taken.
36 switch (i->key->etype) {
2779 36 case EXPR_PREFIX:
2780 case EXPR_RANGE:
2781 case EXPR_VALUE:
2782 36 rel->right = *expr = i->key;
2783 36 i->key = NULL;
2784 36 expr_free(set);
2785 36 break;
2786 ✗ default:
2787 ✗ break;
2788 }
2789 }
2790
2791
2/2
✓ Branch 0 (11→12) taken 29 times.
✓ Branch 1 (11→16) taken 9 times.
38 if (rel->op == OP_IMPLICIT &&
2792
1/2
✓ Branch 0 (12→13) taken 29 times.
✗ Branch 1 (12→16) not taken.
29 rel->right->dtype->basetype &&
2793
2/2
✓ Branch 0 (13→14) taken 2 times.
✓ Branch 1 (13→16) taken 27 times.
29 rel->right->dtype->basetype->type == TYPE_BITMASK &&
2794
1/2
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 2 times.
2 rel->right->dtype->type != TYPE_CT_STATE) {
2795 ✗ rel->op = OP_EQ;
2796 }
2797 38 }
2798
2799 109581 static int expr_evaluate_relational(struct eval_ctx *ctx, struct expr **expr)
2800 {
2801 109581 struct expr *rel = *expr, *left, *right;
2802 struct proto_ctx *pctx;
2803 struct expr *range;
2804 int ret;
2805
2806 109581 right = rel->right;
2807
2/2
✓ Branch 0 (2→3) taken 71111 times.
✓ Branch 1 (2→7) taken 38470 times.
109581 if (right->etype == EXPR_SYMBOL &&
2808
4/4
✓ Branch 0 (3→4) taken 1760 times.
✓ Branch 1 (3→7) taken 69351 times.
✓ Branch 2 (5→6) taken 18 times.
✓ Branch 3 (5→7) taken 1742 times.
72871 right->symtype == SYMBOL_SET &&
2809 1760 expr_evaluate(ctx, &rel->right) < 0)
2810 18 return -1;
2811
2812
2/2
✓ Branch 0 (8→9) taken 46 times.
✓ Branch 1 (8→10) taken 109517 times.
109563 if (expr_evaluate(ctx, &rel->left) < 0)
2813 46 return -1;
2814 109517 left = rel->left;
2815
2816 109517 pctx = eval_proto_ctx(ctx);
2817
2818
2/2
✓ Branch 0 (12→13) taken 301 times.
✓ Branch 1 (12→33) taken 109216 times.
109517 if (lhs_is_meta_hour(rel->left) &&
2819
2/2
✓ Branch 0 (13→14) taken 298 times.
✓ Branch 1 (13→15) taken 3 times.
301 (rel->right->etype == EXPR_RANGE ||
2820
2/2
✓ Branch 0 (14→15) taken 102 times.
✓ Branch 1 (14→33) taken 196 times.
298 rel->right->etype == EXPR_RANGE_SYMBOL)) {
2821
2/2
✓ Branch 0 (15→16) taken 102 times.
✓ Branch 1 (15→19) taken 3 times.
105 if (rel->right->etype == EXPR_RANGE_SYMBOL) {
2822 102 range = symbol_range_expand(rel->right);
2823 102 expr_free(rel->right);
2824 102 rel->right = range;
2825 }
2826 105 ret = __expr_evaluate_range(ctx, &rel->right);
2827
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 105 times.
105 if (ret)
2828 ✗ return ret;
2829
2830 105 range = rel->right;
2831
2832 /*
2833 * We may need to do this for proper cross-day ranges,
2834 * e.g. meta hour 23:15-03:22
2835 */
2836
2/2
✓ Branch 0 (23→24) taken 58 times.
✓ Branch 1 (23→33) taken 47 times.
105 if (range_needs_swap(range)) {
2837
2/2
✓ Branch 0 (24→25) taken 3 times.
✓ Branch 1 (24→26) taken 55 times.
58 if (ctx->nft->debug_mask & NFT_DEBUG_EVALUATION)
2838 3 nft_print(&ctx->nft->output,
2839 "Inverting range values for cross-day hour matching\n\n");
2840
2841
2/4
✓ Branch 0 (26→27) taken 58 times.
✗ Branch 1 (26→28) not taken.
✓ Branch 2 (27→28) taken 58 times.
✗ Branch 3 (27→30) not taken.
58 if (rel->op == OP_EQ || rel->op == OP_IMPLICIT) {
2842 58 range_expr_swap_values(range);
2843 58 rel->op = OP_NEQ;
2844 ✗ } else if (rel->op == OP_NEQ) {
2845 ✗ range_expr_swap_values(range);
2846 ✗ rel->op = OP_EQ;
2847 }
2848 }
2849 }
2850
2851
2/2
✓ Branch 0 (34→35) taken 139 times.
✓ Branch 1 (34→36) taken 109378 times.
109517 if (expr_evaluate(ctx, &rel->right) < 0)
2852 139 return -1;
2853 109378 right = rel->right;
2854
2855
2/2
✓ Branch 0 (37→38) taken 1 times.
✓ Branch 1 (37→40) taken 109377 times.
109378 if (!expr_is_constant(right))
2856 1 return expr_binary_error(ctx->msgs, right, rel,
2857 "Right hand side of relational "
2858 "expression (%s) must be constant",
2859 1 expr_op_symbols[rel->op]);
2860
1/2
✗ Branch 0 (41→42) not taken.
✓ Branch 1 (41→44) taken 109377 times.
109377 if (expr_is_constant(left))
2861 ✗ return expr_binary_error(ctx->msgs, left, right,
2862 "Relational expression (%s) has "
2863 "constant value",
2864 ✗ expr_op_symbols[rel->op]);
2865
2866
2/2
✓ Branch 0 (45→46) taken 8 times.
✓ Branch 1 (45→48) taken 109369 times.
109377 if (!datatype_equal(left->dtype, right->dtype))
2867 8 return expr_binary_error(ctx->msgs, right, left,
2868 "datatype mismatch, expected %s, "
2869 "expression has type %s",
2870 8 left->dtype->desc,
2871 8 right->dtype->desc);
2872
2873 /*
2874 * Statements like 'ct secmark 12' are parsed as relational,
2875 * disallow constant value on the right hand side.
2876 */
2877
2/2
✓ Branch 0 (48→49) taken 95057 times.
✓ Branch 1 (48→50) taken 14312 times.
109369 if (((left->etype == EXPR_META &&
2878
1/2
✓ Branch 0 (49→50) taken 95057 times.
✗ Branch 1 (49→52) not taken.
95057 left->meta.key == NFT_META_SECMARK) ||
2879
2/2
✓ Branch 0 (50→51) taken 738 times.
✓ Branch 1 (50→55) taken 108631 times.
109369 (left->etype == EXPR_CT &&
2880
1/2
✗ Branch 0 (51→52) not taken.
✓ Branch 1 (51→55) taken 738 times.
738 left->ct.key == NFT_CT_SECMARK)) &&
2881 ✗ right->flags & EXPR_F_CONSTANT)
2882 ✗ return expr_binary_error(ctx->msgs, right, left,
2883 "Cannot be used with right hand side constant value");
2884
2885
2/2
✓ Branch 0 (55→56) taken 108393 times.
✓ Branch 1 (55→64) taken 976 times.
109369 if (left->etype != EXPR_CONCAT) {
2886
2/2
✓ Branch 0 (56→57) taken 108100 times.
✓ Branch 1 (56→63) taken 293 times.
108393 switch (rel->op) {
2887 108100 case OP_EQ:
2888 case OP_IMPLICIT:
2889 case OP_NEQ:
2890
5/6
✓ Branch 0 (57→58) taken 2918 times.
✓ Branch 1 (57→62) taken 105182 times.
✗ Branch 2 (58→59) not taken.
✓ Branch 3 (58→60) taken 2918 times.
✓ Branch 4 (60→61) taken 38 times.
✓ Branch 5 (60→62) taken 2880 times.
108100 if (right->etype == EXPR_SET && expr_set(right)->size == 1)
2891 38 optimize_singleton_set(rel, &right);
2892 108100 break;
2893 293 default:
2894 293 break;
2895 }
2896 }
2897
2898
3/4
✓ Branch 0 (64→65) taken 105980 times.
✓ Branch 1 (64→66) taken 3137 times.
✓ Branch 2 (64→120) taken 252 times.
✗ Branch 3 (64→136) not taken.
109369 switch (rel->op) {
2899 105980 case OP_EQ:
2900 case OP_IMPLICIT:
2901 /*
2902 * Update protocol context for payload and meta iiftype
2903 * equality expressions.
2904 */
2905 105980 relational_expr_pctx_update(pctx, rel);
2906
2907 /* fall through */
2908 109117 case OP_NEQ:
2909 case OP_NEG:
2910
2/2
✓ Branch 0 (66→67) taken 41 times.
✓ Branch 1 (66→75) taken 109076 times.
109117 if (rel->op == OP_NEG) {
2911
2/2
✓ Branch 0 (67→68) taken 5 times.
✓ Branch 1 (67→70) taken 36 times.
41 if (left->etype == EXPR_BINOP)
2912 5 return expr_binary_error(ctx->msgs, left, right,
2913 "cannot combine negation with binary expression");
2914
1/2
✓ Branch 0 (70→71) taken 36 times.
✗ Branch 1 (70→73) not taken.
36 if (right->etype != EXPR_VALUE ||
2915
1/2
✓ Branch 0 (71→72) taken 36 times.
✗ Branch 1 (71→73) not taken.
36 right->dtype->basetype == NULL ||
2916
1/2
✗ Branch 0 (72→73) not taken.
✓ Branch 1 (72→75) taken 36 times.
36 right->dtype->basetype->type != TYPE_BITMASK)
2917 ✗ return expr_binary_error(ctx->msgs, left, right,
2918 "negation can only be used with singleton bitmask values. Did you mean \"!=\"?");
2919 }
2920
2921
5/7
✓ Branch 0 (75→76) taken 1477 times.
✓ Branch 1 (75→86) taken 239 times.
✓ Branch 2 (75→90) taken 102546 times.
✓ Branch 3 (75→94) taken 3116 times.
✓ Branch 4 (75→105) taken 1734 times.
✗ Branch 5 (75→115) not taken.
✗ Branch 6 (75→116) not taken.
109112 switch (right->etype) {
2922 1477 case EXPR_RANGE:
2923
1/2
✗ Branch 0 (77→78) not taken.
✓ Branch 1 (77→79) taken 1477 times.
1477 if (byteorder_conversion(ctx, &rel->left, BYTEORDER_BIG_ENDIAN) < 0)
2924 ✗ return -1;
2925
1/2
✗ Branch 0 (80→81) not taken.
✓ Branch 1 (80→82) taken 1477 times.
1477 if (byteorder_conversion(ctx, &right->left, BYTEORDER_BIG_ENDIAN) < 0)
2926 ✗ return -1;
2927
1/2
✗ Branch 0 (83→84) not taken.
✓ Branch 1 (83→85) taken 1477 times.
1477 if (byteorder_conversion(ctx, &right->right, BYTEORDER_BIG_ENDIAN) < 0)
2928 ✗ return -1;
2929 1477 break;
2930 239 case EXPR_PREFIX:
2931
1/2
✗ Branch 0 (87→88) not taken.
✓ Branch 1 (87→89) taken 239 times.
239 if (byteorder_conversion(ctx, &right->prefix, left->byteorder) < 0)
2932 ✗ return -1;
2933 239 break;
2934 102546 case EXPR_VALUE:
2935
1/2
✗ Branch 0 (91→92) not taken.
✓ Branch 1 (91→93) taken 102546 times.
102546 if (byteorder_conversion(ctx, &rel->right, left->byteorder) < 0)
2936 ✗ return -1;
2937 102546 break;
2938 3116 case EXPR_SET:
2939
3/4
✗ Branch 0 (94→95) not taken.
✓ Branch 1 (94→96) taken 3116 times.
✓ Branch 2 (96→97) taken 1 times.
✓ Branch 3 (96→99) taken 3115 times.
3116 if (expr_set(right)->size == 0)
2940 1 return expr_error(ctx->msgs, right, "Set is empty");
2941
2942 3115 right = rel->right =
2943 3115 implicit_set_declaration(ctx, "__set%d",
2944 expr_get(left), NULL,
2945 right,
2946
1/2
✗ Branch 0 (99→100) not taken.
✓ Branch 1 (99→101) taken 3115 times.
3115 expr_set(right)->set_flags | NFT_SET_ANONYMOUS);
2947
2/2
✓ Branch 0 (103→104) taken 1 times.
✓ Branch 1 (103→105) taken 3114 times.
3115 if (!right)
2948 1 return -1;
2949
2950 /* fall through */
2951 case EXPR_SET_REF:
2952
2/2
✓ Branch 0 (105→106) taken 135 times.
✓ Branch 1 (105→110) taken 4713 times.
4848 if (rel->left->etype == EXPR_CT &&
2953
1/2
✓ Branch 0 (106→107) taken 135 times.
✗ Branch 1 (106→108) not taken.
135 (rel->left->ct.key == NFT_CT_SRC ||
2954
1/2
✗ Branch 0 (107→108) not taken.
✓ Branch 1 (107→110) taken 135 times.
135 rel->left->ct.key == NFT_CT_DST))
2955 ✗ return expr_error(ctx->msgs, left,
2956 "specify either ip or ip6 for address matching");
2957
2958 /* Data for range lookups needs to be in big endian order */
2959
3/4
✓ Branch 0 (110→111) taken 590 times.
✓ Branch 1 (110→114) taken 4258 times.
✗ Branch 2 (112→113) not taken.
✓ Branch 3 (112→114) taken 590 times.
5438 if (right->set->flags & NFT_SET_INTERVAL &&
2960 590 byteorder_conversion(ctx, &rel->left, BYTEORDER_BIG_ENDIAN) < 0)
2961 ✗ return -1;
2962 4848 break;
2963 ✗ case EXPR_CONCAT:
2964 ✗ return expr_binary_error(ctx->msgs, left, right,
2965 "Use concatenations with sets and maps, not singleton values");
2966 break;
2967 ✗ default:
2968 ✗ BUG("invalid expression type %s", expr_name(right));
2969 }
2970 109110 break;
2971 252 case OP_LT:
2972 case OP_GT:
2973 case OP_LTE:
2974 case OP_GTE:
2975
1/2
✗ Branch 0 (120→121) not taken.
✓ Branch 1 (120→123) taken 252 times.
252 switch (left->etype) {
2976 ✗ case EXPR_CONCAT:
2977 ✗ return expr_binary_error(ctx->msgs, left, rel,
2978 "Relational expression (%s) is undefined "
2979 "for %s expressions",
2980 ✗ expr_op_symbols[rel->op],
2981 expr_name(left));
2982 252 default:
2983 252 break;
2984 }
2985
2986
1/2
✗ Branch 0 (125→126) not taken.
✓ Branch 1 (125→129) taken 252 times.
252 if (!expr_is_singleton(right))
2987 ✗ return expr_binary_error(ctx->msgs, right, rel,
2988 "Relational expression (%s) is undefined "
2989 "for %s expressions",
2990 ✗ expr_op_symbols[rel->op],
2991 expr_name(right));
2992
2993
1/2
✗ Branch 0 (130→131) not taken.
✓ Branch 1 (130→132) taken 252 times.
252 if (byteorder_conversion(ctx, &rel->left, BYTEORDER_BIG_ENDIAN) < 0)
2994 ✗ return -1;
2995
1/2
✗ Branch 0 (133→134) not taken.
✓ Branch 1 (133→135) taken 252 times.
252 if (byteorder_conversion(ctx, &rel->right, BYTEORDER_BIG_ENDIAN) < 0)
2996 ✗ return -1;
2997 252 break;
2998 ✗ default:
2999 ✗ BUG("invalid relational operation %u", rel->op);
3000 }
3001
3002
1/2
✗ Branch 0 (139→140) not taken.
✓ Branch 1 (139→141) taken 109362 times.
109362 if (binop_transfer(ctx, expr) < 0)
3003 ✗ return -1;
3004
3005 109362 return 0;
3006 }
3007
3008 66 static int expr_evaluate_fib(struct eval_ctx *ctx, struct expr **exprp)
3009 {
3010 66 struct expr *expr = *exprp;
3011
3012
2/2
✓ Branch 0 (2→3) taken 23 times.
✓ Branch 1 (2→5) taken 43 times.
66 if (expr->flags & EXPR_F_BOOLEAN) {
3013 23 expr->fib.flags |= NFTA_FIB_F_PRESENT;
3014 23 datatype_set(expr, &boolean_type);
3015 23 expr->len = BITS_PER_BYTE;
3016 }
3017 66 return expr_evaluate_primary(ctx, exprp);
3018 }
3019
3020 96004 static int expr_evaluate_meta(struct eval_ctx *ctx, struct expr **exprp)
3021 {
3022 96004 struct proto_ctx *pctx = eval_proto_ctx(ctx);
3023 96004 struct expr *meta = *exprp;
3024
3025
3/3
✓ Branch 0 (3→4) taken 1809 times.
✓ Branch 1 (3→9) taken 51 times.
✓ Branch 2 (3→11) taken 94144 times.
96004 switch (meta->meta.key) {
3026 1809 case NFT_META_NFPROTO:
3027
2/2
✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→8) taken 1807 times.
1809 if (pctx->family != NFPROTO_INET &&
3028
1/2
✓ Branch 0 (5→6) taken 2 times.
✗ Branch 1 (5→8) not taken.
2 meta->flags & EXPR_F_PROTOCOL)
3029 2 return expr_error(ctx->msgs, meta,
3030 "meta nfproto is only useful in the inet family");
3031 1807 break;
3032 51 case NFT_META_TIME_DAY:
3033 51 __expr_set_context(&ctx->ectx, meta->dtype, meta->byteorder,
3034 meta->len, 6);
3035 51 return 0;
3036 94144 default:
3037 94144 break;
3038 }
3039
3040 95951 return expr_evaluate_primary(ctx, exprp);
3041 }
3042
3043 37 static int expr_evaluate_socket(struct eval_ctx *ctx, struct expr **expr)
3044 {
3045 37 enum nft_socket_keys key = (*expr)->socket.key;
3046 37 int maxval = 0;
3047
3048
4/4
✓ Branch 0 (2→3) taken 22 times.
✓ Branch 1 (2→4) taken 15 times.
✓ Branch 2 (3→4) taken 15 times.
✓ Branch 3 (3→5) taken 7 times.
37 if (key == NFT_SOCKET_TRANSPARENT ||
3049 key == NFT_SOCKET_WILDCARD)
3050 30 maxval = 1;
3051 37 __expr_set_context(&ctx->ectx, (*expr)->dtype, (*expr)->byteorder,
3052 37 (*expr)->len, maxval);
3053 37 return 0;
3054 }
3055
3056 63 static int expr_evaluate_osf(struct eval_ctx *ctx, struct expr **expr)
3057 {
3058 126 struct netlink_ctx nl_ctx = {
3059 63 .nft = ctx->nft,
3060 63 .seqnum = time(NULL),
3061 };
3062
3063 63 nfnl_osf_load_fingerprints(&nl_ctx, 0);
3064
3065 63 return expr_evaluate_primary(ctx, expr);
3066 }
3067
3068 6 static int expr_evaluate_tunnel(struct eval_ctx *ctx, struct expr **exprp)
3069 {
3070 6 return expr_evaluate_primary(ctx, exprp);
3071 }
3072
3073 20211 static int expr_evaluate_variable(struct eval_ctx *ctx, struct expr **exprp)
3074 {
3075 20211 struct symbol *sym = (*exprp)->sym;
3076 struct expr *new;
3077
3078 /* If variable is reused from different locations in the ruleset, then
3079 * clone expression.
3080 */
3081
2/2
✓ Branch 0 (2→3) taken 108 times.
✓ Branch 1 (2→5) taken 20103 times.
20211 if (sym->refcnt > 2)
3082 108 new = expr_clone(sym->expr);
3083 else
3084 20103 new = expr_get(sym->expr);
3085
3086
2/2
✓ Branch 0 (8→9) taken 3 times.
✓ Branch 1 (8→11) taken 20208 times.
20211 if (expr_evaluate(ctx, &new) < 0) {
3087 3 expr_free(new);
3088 3 return -1;
3089 }
3090
3091 20208 expr_free(*exprp);
3092 20208 *exprp = new;
3093
3094 20208 return 0;
3095 }
3096
3097 71 static int expr_evaluate_xfrm(struct eval_ctx *ctx, struct expr **exprp)
3098 {
3099 71 struct proto_ctx *pctx = eval_proto_ctx(ctx);
3100 71 struct expr *expr = *exprp;
3101
3102
1/2
✓ Branch 0 (3→4) taken 71 times.
✗ Branch 1 (3→5) not taken.
71 switch (pctx->family) {
3103 71 case NFPROTO_IPV4:
3104 case NFPROTO_IPV6:
3105 case NFPROTO_INET:
3106 71 break;
3107 ✗ default:
3108 ✗ return expr_error(ctx->msgs, expr, "ipsec expression is only"
3109 " valid in ip/ip6/inet tables");
3110 }
3111
3112 71 return expr_evaluate_primary(ctx, exprp);
3113 }
3114
3115 141172 static int verdict_validate_chain(struct eval_ctx *ctx,
3116 struct expr *chain)
3117 {
3118 char buf[NFT_CHAIN_MAXNAMELEN];
3119 unsigned int len;
3120
3121 141172 len = chain->len / BITS_PER_BYTE;
3122
2/2
✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→5) taken 141170 times.
141172 if (len > NFT_CHAIN_MAXNAMELEN)
3123 2 return expr_error(ctx->msgs, chain,
3124 "chain name too long (%u, max %u)",
3125 chain->len / BITS_PER_BYTE,
3126 NFT_CHAIN_MAXNAMELEN);
3127
3128
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 141170 times.
141170 if (!len)
3129 ✗ return expr_error(ctx->msgs, chain,
3130 "chain name length 0 not allowed");
3131
3132 141170 memset(buf, 0, sizeof(buf));
3133
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 141170 times.
141170 mpz_export_data(buf, chain->value, BYTEORDER_HOST_ENDIAN, len);
3134
3135
2/2
✓ Branch 0 (11→12) taken 141169 times.
✓ Branch 1 (11→13) taken 1 times.
141170 if (strnlen(buf, sizeof(buf)) < sizeof(buf))
3136 141169 return 0;
3137
3138 1 return expr_error(ctx->msgs, chain,
3139 "chain name must be smaller than %u",
3140 NFT_CHAIN_MAXNAMELEN);
3141 }
3142
3143 151517 static int expr_evaluate_verdict(struct eval_ctx *ctx, struct expr **exprp)
3144 {
3145 151517 struct expr *expr = *exprp;
3146
3147
2/2
✓ Branch 0 (2→3) taken 138579 times.
✓ Branch 1 (2→8) taken 12938 times.
151517 switch (expr->verdict) {
3148 138579 case NFT_GOTO:
3149 case NFT_JUMP:
3150
4/4
✓ Branch 0 (3→4) taken 138576 times.
✓ Branch 1 (3→7) taken 3 times.
✓ Branch 2 (5→6) taken 2 times.
✓ Branch 3 (5→7) taken 138574 times.
277155 if (expr->chain->etype == EXPR_VALUE &&
3151 138576 verdict_validate_chain(ctx, expr->chain))
3152 2 return -1;
3153
3154 138577 break;
3155 }
3156
3157 151515 return expr_evaluate_primary(ctx, exprp);
3158 }
3159
3160 6727209 static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)
3161 {
3162
2/2
✓ Branch 0 (2→3) taken 34 times.
✓ Branch 1 (2→9) taken 6727175 times.
6727209 if (ctx->nft->debug_mask & NFT_DEBUG_EVALUATION) {
3163 struct error_record *erec;
3164 34 erec = erec_create(EREC_INFORMATIONAL, &(*expr)->location,
3165 "Evaluate %s", expr_name(*expr));
3166 34 erec_print(&ctx->nft->output, erec, ctx->nft->debug_mask);
3167 34 expr_print(*expr, &ctx->nft->output);
3168 34 nft_print(&ctx->nft->output, "\n\n");
3169 34 erec_destroy(erec);
3170 }
3171
3172
30/31
✓ Branch 0 (9→10) taken 1596854 times.
✓ Branch 1 (9→11) taken 20211 times.
✓ Branch 2 (9→12) taken 5201 times.
✓ Branch 3 (9→14) taken 2470432 times.
✓ Branch 4 (9→15) taken 1338 times.
✓ Branch 5 (9→16) taken 151517 times.
✓ Branch 6 (9→17) taken 96004 times.
✓ Branch 7 (9→18) taken 37 times.
✓ Branch 8 (9→19) taken 6 times.
✓ Branch 9 (9→20) taken 63 times.
✓ Branch 10 (9→21) taken 66 times.
✓ Branch 11 (9→22) taken 16613 times.
✓ Branch 12 (9→23) taken 41 times.
✓ Branch 13 (9→24) taken 997 times.
✓ Branch 14 (9→25) taken 282144 times.
✓ Branch 15 (9→26) taken 17099 times.
✓ Branch 16 (9→27) taken 888 times.
✓ Branch 17 (9→28) taken 285831 times.
✓ Branch 18 (9→29) taken 82116 times.
✓ Branch 19 (9→30) taken 167 times.
✓ Branch 20 (9→31) taken 33854 times.
✓ Branch 21 (9→32) taken 1367172 times.
✓ Branch 22 (9→33) taken 1357 times.
✓ Branch 23 (9→34) taken 170458 times.
✓ Branch 24 (9→35) taken 109581 times.
✓ Branch 25 (9→36) taken 57 times.
✓ Branch 26 (9→37) taken 52 times.
✓ Branch 27 (9→38) taken 71 times.
✓ Branch 28 (9→39) taken 85 times.
✓ Branch 29 (9→40) taken 16897 times.
✗ Branch 30 (9→41) not taken.
6727209 switch ((*expr)->etype) {
3173 1596854 case EXPR_SYMBOL:
3174 1596854 return expr_evaluate_symbol(ctx, expr);
3175 20211 case EXPR_VARIABLE:
3176 20211 return expr_evaluate_variable(ctx, expr);
3177 5201 case EXPR_SET_REF:
3178 5201 expr_evaluate_set_ref(ctx, *expr);
3179 5201 return 0;
3180 2470432 case EXPR_VALUE:
3181 2470432 return expr_evaluate_value(ctx, expr);
3182 1338 case EXPR_EXTHDR:
3183 1338 return expr_evaluate_exthdr(ctx, expr);
3184 151517 case EXPR_VERDICT:
3185 151517 return expr_evaluate_verdict(ctx, expr);
3186 96004 case EXPR_META:
3187 96004 return expr_evaluate_meta(ctx, expr);
3188 37 case EXPR_SOCKET:
3189 37 return expr_evaluate_socket(ctx, expr);
3190 6 case EXPR_TUNNEL:
3191 6 return expr_evaluate_tunnel(ctx, expr);
3192 63 case EXPR_OSF:
3193 63 return expr_evaluate_osf(ctx, expr);
3194 66 case EXPR_FIB:
3195 66 return expr_evaluate_fib(ctx, expr);
3196 16613 case EXPR_PAYLOAD:
3197 16613 return expr_evaluate_payload_inner(ctx, expr);
3198 41 case EXPR_RT:
3199 41 return expr_evaluate_rt(ctx, expr);
3200 997 case EXPR_CT:
3201 997 return expr_evaluate_ct(ctx, expr);
3202 282144 case EXPR_PREFIX:
3203 282144 return expr_evaluate_prefix(ctx, expr);
3204 17099 case EXPR_RANGE:
3205 17099 return expr_evaluate_range(ctx, expr);
3206 888 case EXPR_UNARY:
3207 888 return expr_evaluate_unary(ctx, expr);
3208 285831 case EXPR_BINOP:
3209 285831 return expr_evaluate_binop(ctx, expr);
3210 82116 case EXPR_CONCAT:
3211 82116 return expr_evaluate_concat(ctx, expr);
3212 167 case EXPR_LIST:
3213 167 return expr_evaluate_list(ctx, expr);
3214 33854 case EXPR_SET:
3215 33854 return expr_evaluate_set(ctx, expr);
3216 1367172 case EXPR_SET_ELEM:
3217 1367172 return expr_evaluate_set_elem(ctx, expr);
3218 1357 case EXPR_MAP:
3219 1357 return expr_evaluate_map(ctx, expr);
3220 170458 case EXPR_MAPPING:
3221 170458 return expr_evaluate_mapping(ctx, expr);
3222 109581 case EXPR_RELATIONAL:
3223 109581 return expr_evaluate_relational(ctx, expr);
3224 57 case EXPR_NUMGEN:
3225 57 return expr_evaluate_numgen(ctx, expr);
3226 52 case EXPR_HASH:
3227 52 return expr_evaluate_hash(ctx, expr);
3228 71 case EXPR_XFRM:
3229 71 return expr_evaluate_xfrm(ctx, expr);
3230 85 case EXPR_SET_ELEM_CATCHALL:
3231 85 return expr_evaluate_set_elem_catchall(ctx, expr);
3232 16897 case EXPR_RANGE_SYMBOL:
3233 16897 return expr_evaluate_symbol_range(ctx, expr);
3234 ✗ default:
3235 ✗ BUG("unknown expression type %s", expr_name(*expr));
3236 }
3237 }
3238
3239 109581 static int stmt_evaluate_expr(struct eval_ctx *ctx, struct stmt *stmt)
3240 {
3241 109581 memset(&ctx->ectx, 0, sizeof(ctx->ectx));
3242 109581 return expr_evaluate(ctx, &stmt->expr);
3243 }
3244
3245 13 static int stmt_prefix_conversion(struct eval_ctx *ctx, struct expr **expr,
3246 enum byteorder byteorder)
3247 {
3248 struct expr *mask, *and, *or, *prefix, *base, *range;
3249 int ret;
3250
3251 13 prefix = *expr;
3252 13 base = prefix->prefix;
3253
3254
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 13 times.
13 if (base->etype != EXPR_VALUE)
3255 ✗ return expr_error(ctx->msgs, prefix,
3256 "you cannot specify a prefix here, "
3257 "unknown type %s", base->dtype->name);
3258
3259
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 13 times.
13 if (!expr_is_constant(base))
3260 ✗ return expr_error(ctx->msgs, prefix,
3261 "Prefix expression is undefined for "
3262 "non-constant expressions");
3263
3264
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 13 times.
13 if (expr_basetype(base)->type != TYPE_INTEGER)
3265 ✗ return expr_error(ctx->msgs, prefix,
3266 "Prefix expression expected integer value");
3267
3268 13 mask = constant_expr_alloc(&prefix->location, expr_basetype(base),
3269 BYTEORDER_HOST_ENDIAN, base->len, NULL);
3270
3271 13 mpz_prefixmask(mask->value, base->len, prefix->prefix_len);
3272 13 and = binop_expr_alloc(&prefix->location, OP_AND, expr_get(base), mask);
3273
3274 13 mask = constant_expr_alloc(&prefix->location, expr_basetype(base),
3275 BYTEORDER_HOST_ENDIAN, base->len, NULL);
3276 13 mpz_bitmask(mask->value, prefix->len - prefix->prefix_len);
3277 13 or = binop_expr_alloc(&prefix->location, OP_OR, expr_get(base), mask);
3278
3279 13 range = range_expr_alloc(&prefix->location, and, or);
3280 13 ret = expr_evaluate(ctx, &range);
3281
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→28) taken 13 times.
13 if (ret < 0) {
3282 ✗ expr_free(range);
3283 ✗ return ret;
3284 }
3285
3286 13 expr_free(*expr);
3287 13 *expr = range;
3288 13 return 0;
3289 }
3290
3291 − static int __stmt_evaluate_arg(struct eval_ctx *ctx, struct stmt *stmt,
3292 const struct datatype *dtype, unsigned int len,
3293 enum byteorder byteorder, struct expr **expr)
3294 {
3295 − if ((*expr)->etype == EXPR_PAYLOAD &&
3296 − (*expr)->dtype->type == TYPE_INTEGER &&
3297 − ((*expr)->dtype->type != datatype_basetype(dtype)->type ||
3298 − (*expr)->len != len))
3299 − return stmt_binary_error(ctx, *expr, stmt,
3300 "datatype mismatch: expected %s, "
3301 "expression has type %s with length %d",
3302 dtype->desc, (*expr)->dtype->desc,
3303 (*expr)->len);
3304
3305 − if (!datatype_compatible(dtype, (*expr)->dtype))
3306 − return stmt_binary_error(ctx, *expr, stmt, /* verdict vs invalid? */
3307 "datatype mismatch: expected %s, "
3308 "expression has type %s",
3309 dtype->desc, (*expr)->dtype->desc);
3310
3311 − if (dtype->type == TYPE_MARK &&
3312 − datatype_equal(datatype_basetype(dtype), datatype_basetype((*expr)->dtype)) &&
3313 − !expr_is_constant(*expr))
3314 − return byteorder_conversion(ctx, expr, byteorder);
3315
3316 /* we are setting a value, we can't use a set */
3317 − switch ((*expr)->etype) {
3318 − case EXPR_SET:
3319 − return stmt_binary_error(ctx, *expr, stmt,
3320 "you cannot use a set here, unknown "
3321 "value to use");
3322 − case EXPR_SET_REF:
3323 − return stmt_binary_error(ctx, *expr, stmt,
3324 "you cannot reference a set here, "
3325 "unknown value to use");
3326 − case EXPR_RT:
3327 − return byteorder_conversion(ctx, expr, byteorder);
3328 − case EXPR_PREFIX:
3329 − return stmt_prefix_conversion(ctx, expr, byteorder);
3330 − case EXPR_NUMGEN:
3331 − if (dtype->type == TYPE_IPADDR)
3332 − return byteorder_conversion(ctx, expr, byteorder);
3333 − break;
3334 − default:
3335 − break;
3336 }
3337
3338 − return 0;
3339 }
3340
3341 19825 static int stmt_evaluate_arg(struct eval_ctx *ctx, struct stmt *stmt,
3342 const struct datatype *dtype, unsigned int len,
3343 enum byteorder byteorder, struct expr **expr)
3344 {
3345 19825 __expr_set_context(&ctx->ectx, dtype, byteorder, len, 0);
3346
2/2
✓ Branch 0 (4→5) taken 65 times.
✓ Branch 1 (4→6) taken 19760 times.
19825 if (expr_evaluate(ctx, expr) < 0)
3347 65 return -1;
3348
3349 19760 return __stmt_evaluate_arg(ctx, stmt, dtype, len, byteorder, expr);
3350 }
3351
3352 /* like stmt_evaluate_arg, but keep existing context created
3353 * by previous expr_evaluate().
3354 *
3355 * This is needed for add/update statements:
3356 * ctx->ectx.key has the set key, which may be needed for 'typeof'
3357 * sets: the 'add/update' expression might contain integer data types.
3358 *
3359 * Without the key we cannot derive the element size.
3360 */
3361 1239 static int stmt_evaluate_key(struct eval_ctx *ctx, struct stmt *stmt,
3362 const struct datatype *dtype, unsigned int len,
3363 enum byteorder byteorder, struct expr **expr)
3364 {
3365
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 1239 times.
1239 if (expr_evaluate(ctx, expr) < 0)
3366 ✗ return -1;
3367
3368 1239 return __stmt_evaluate_arg(ctx, stmt, dtype, len, byteorder, expr);
3369 }
3370
3371 14809 static int stmt_evaluate_verdict(struct eval_ctx *ctx, struct stmt *stmt)
3372 {
3373
2/2
✓ Branch 0 (3→4) taken 14 times.
✓ Branch 1 (3→5) taken 14795 times.
14809 if (stmt_evaluate_arg(ctx, stmt, &verdict_type, 0, 0, &stmt->expr) < 0)
3374 14 return -1;
3375
3376
2/3
✓ Branch 0 (5→6) taken 14005 times.
✓ Branch 1 (5→19) taken 790 times.
✗ Branch 2 (5→20) not taken.
14795 switch (stmt->expr->etype) {
3377 14005 case EXPR_VERDICT:
3378
2/2
✓ Branch 0 (6→7) taken 14004 times.
✓ Branch 1 (6→8) taken 1 times.
14005 if (stmt->expr->verdict != NFT_CONTINUE)
3379 14004 stmt->flags |= STMT_F_TERMINAL;
3380
2/2
✓ Branch 0 (8→9) taken 2597 times.
✓ Branch 1 (8→18) taken 11408 times.
14005 if (stmt->expr->chain != NULL) {
3381
2/2
✓ Branch 0 (10→11) taken 1 times.
✓ Branch 1 (10→12) taken 2596 times.
2597 if (stmt_evaluate_arg(ctx, stmt, &string_type, 0, 0,
3382 2597 &stmt->expr->chain) < 0)
3383 1 return -1;
3384
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→15) taken 2596 times.
2596 if (stmt->expr->chain->etype != EXPR_VALUE) {
3385 ✗ return expr_error(ctx->msgs, stmt->expr->chain,
3386 "not a value expression");
3387 }
3388
3389
2/2
✓ Branch 0 (16→17) taken 1 times.
✓ Branch 1 (16→18) taken 2595 times.
2596 if (verdict_validate_chain(ctx, stmt->expr->chain))
3390 1 return -1;
3391 }
3392 14003 break;
3393 790 case EXPR_MAP:
3394 790 break;
3395 ✗ default:
3396 ✗ BUG("invalid verdict expression %s", expr_name(stmt->expr));
3397 }
3398 14793 return 0;
3399 }
3400
3401 420 static bool stmt_evaluate_payload_need_csum(const struct expr *payload)
3402 {
3403 const struct proto_desc *desc;
3404
3405
2/2
✓ Branch 0 (2→3) taken 45 times.
✓ Branch 1 (2→4) taken 375 times.
420 if (payload->payload.base == PROTO_BASE_INNER_HDR)
3406 45 return true;
3407
3408 375 desc = payload->payload.desc;
3409
3410
3/4
✓ Branch 0 (4→5) taken 375 times.
✗ Branch 1 (4→7) not taken.
✓ Branch 2 (5→6) taken 251 times.
✓ Branch 3 (5→7) taken 124 times.
375 return desc && desc->checksum_key;
3411 }
3412
3413 124 static bool stmt_evaluate_is_vlan(const struct expr *payload)
3414 {
3415
2/2
✓ Branch 0 (2→3) taken 17 times.
✓ Branch 1 (2→5) taken 107 times.
141 return payload->payload.base == PROTO_BASE_LL_HDR &&
3416
2/2
✓ Branch 0 (3→4) taken 11 times.
✓ Branch 1 (3→5) taken 6 times.
17 payload->payload.desc == &proto_vlan;
3417 }
3418
3419 /** stmt_evaluate_payload_need_aligned_fetch
3420 *
3421 * @payload: payload expression to check
3422 *
3423 * Some types of stores need to round up to an even sized byte length,
3424 * typically 1 -> 2 or 3 -> 4 bytes.
3425 *
3426 * This includes anything that needs inet checksum fixups and also writes
3427 * to the vlan header. This is because of VLAN header removal in the
3428 * kernel: nftables kernel side provides illusion of a linear packet, i.e.
3429 * ethernet_header|vlan_header|network_header.
3430 *
3431 * When a write to the vlan header is performed, kernel side updates the
3432 * pseudoheader, but only accepts 2 or 4 byte writes to vlan proto/TCI.
3433 *
3434 * Return true if load needs to be expanded to cover even amount of bytes
3435 */
3436 420 static bool stmt_evaluate_payload_need_aligned_fetch(const struct expr *payload)
3437 {
3438
2/2
✓ Branch 0 (3→4) taken 296 times.
✓ Branch 1 (3→5) taken 124 times.
420 if (stmt_evaluate_payload_need_csum(payload))
3439 296 return true;
3440
3441
2/2
✓ Branch 0 (6→7) taken 11 times.
✓ Branch 1 (6→8) taken 113 times.
124 if (stmt_evaluate_is_vlan(payload))
3442 11 return true;
3443
3444 113 return false;
3445 }
3446
3447 13 static int stmt_evaluate_exthdr(struct eval_ctx *ctx, struct stmt *stmt)
3448 {
3449 struct expr *exthdr;
3450 int ret;
3451
3452
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 13 times.
13 if (__expr_evaluate_exthdr(ctx, &stmt->exthdr.expr) < 0)
3453 ✗ return -1;
3454
3455 13 exthdr = stmt->exthdr.expr;
3456 13 ret = stmt_evaluate_arg(ctx, stmt, exthdr->dtype, exthdr->len,
3457 BYTEORDER_BIG_ENDIAN,
3458 &stmt->exthdr.val);
3459
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 13 times.
13 if (ret < 0)
3460 ✗ return ret;
3461
3462
2/2
✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→11) taken 12 times.
13 if (stmt->exthdr.val->etype == EXPR_RANGE)
3463 1 return stmt_error_range(ctx, stmt, stmt->exthdr.val);
3464
3465 12 return 0;
3466 }
3467
3468 463 static int stmt_evaluate_payload(struct eval_ctx *ctx, struct stmt *stmt)
3469 {
3470 struct expr *mask, *and, *xor, *expr, *payload_bytes;
3471 unsigned int payload_byte_size, payload_byte_offset;
3472 uint8_t shift_imm, data[NFT_REG_SIZE];
3473 463 unsigned int masklen, extra_len = 0;
3474 struct expr *payload;
3475 mpz_t bitmask, ff;
3476 bool aligned_fetch;
3477
3478
2/2
✓ Branch 0 (2→3) taken 20 times.
✓ Branch 1 (2→5) taken 443 times.
463 if (stmt->payload.expr->payload.inner_desc) {
3479 20 return expr_error(ctx->msgs, stmt->payload.expr,
3480 "payload statement for this expression is not supported");
3481 }
3482
3483
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 443 times.
443 if (__expr_evaluate_payload(ctx, stmt->payload.expr) < 0)
3484 ✗ return -1;
3485
3486 443 payload = stmt->payload.expr;
3487
2/2
✓ Branch 0 (9→10) taken 22 times.
✓ Branch 1 (9→11) taken 421 times.
443 if (stmt_evaluate_arg(ctx, stmt, payload->dtype, payload->len,
3488 443 payload->byteorder, &stmt->payload.val) < 0)
3489 22 return -1;
3490
3491
3/4
✓ Branch 0 (12→13) taken 134 times.
✓ Branch 1 (12→16) taken 287 times.
✗ Branch 2 (14→15) not taken.
✓ Branch 3 (14→16) taken 134 times.
555 if (!expr_is_constant(stmt->payload.val) &&
3492 134 byteorder_conversion(ctx, &stmt->payload.val,
3493 134 payload->byteorder) < 0)
3494 ✗ return -1;
3495
3496
2/2
✓ Branch 0 (16→17) taken 1 times.
✓ Branch 1 (16→19) taken 420 times.
421 if (stmt->payload.val->etype == EXPR_RANGE)
3497 1 return stmt_error_range(ctx, stmt, stmt->payload.val);
3498
3499 420 aligned_fetch = stmt_evaluate_payload_need_aligned_fetch(payload);
3500
3501
2/2
✓ Branch 0 (21→22) taken 174 times.
✓ Branch 1 (21→25) taken 246 times.
420 if (!payload_needs_adjustment(payload)) {
3502
3503 /* We still need to munge the payload in case we have to
3504 * update checksum and the length is not even because
3505 * kernel checksum functions cannot deal with odd lengths.
3506 */
3507
4/4
✓ Branch 0 (22→23) taken 95 times.
✓ Branch 1 (22→24) taken 79 times.
✓ Branch 2 (23→24) taken 55 times.
✓ Branch 3 (23→25) taken 40 times.
174 if (!aligned_fetch || ((payload->len / BITS_PER_BYTE) & 1) == 0)
3508 134 return 0;
3509 }
3510
3511 286 payload_byte_offset = payload->payload.offset / BITS_PER_BYTE;
3512
3513 286 shift_imm = expr_offset_shift(payload, payload->payload.offset,
3514 &extra_len);
3515 286 payload_byte_size = div_round_up(payload->len + extra_len,
3516 BITS_PER_BYTE);
3517
3518
2/2
✓ Branch 0 (26→27) taken 1 times.
✓ Branch 1 (26→29) taken 285 times.
286 if (payload_byte_size > sizeof(data))
3519 1 return expr_error(ctx->msgs, stmt->payload.expr,
3520 "uneven load cannot span more than %zu bytes, got %u",
3521 sizeof(data), payload_byte_size);
3522
3523
4/4
✓ Branch 0 (29→30) taken 252 times.
✓ Branch 1 (29→35) taken 33 times.
✓ Branch 2 (30→31) taken 227 times.
✓ Branch 3 (30→35) taken 25 times.
285 if (aligned_fetch && payload_byte_size & 1) {
3524 227 payload_byte_size++;
3525
3526
2/2
✓ Branch 0 (31→32) taken 176 times.
✓ Branch 1 (31→34) taken 51 times.
227 if (payload_byte_offset & 1) { /* prefer 16bit aligned fetch */
3527 176 payload_byte_offset--;
3528
1/2
✗ Branch 0 (32→33) not taken.
✓ Branch 1 (32→35) taken 176 times.
176 assert(payload->payload.offset >= BITS_PER_BYTE);
3529 } else {
3530 51 shift_imm += BITS_PER_BYTE;
3531 }
3532 }
3533
3534
2/3
✓ Branch 0 (35→36) taken 155 times.
✓ Branch 1 (35→39) taken 130 times.
✗ Branch 2 (35→68) not taken.
285 switch (stmt->payload.val->etype) {
3535 155 case EXPR_VALUE:
3536
2/2
✓ Branch 0 (36→37) taken 66 times.
✓ Branch 1 (36→38) taken 89 times.
155 if (shift_imm)
3537 66 mpz_lshift_ui(stmt->payload.val->value, shift_imm);
3538 155 break;
3539 130 case EXPR_BINOP:
3540 130 expr = stmt->payload.val;
3541
2/2
✓ Branch 0 (41→40) taken 10 times.
✓ Branch 1 (41→42) taken 130 times.
140 while (expr->left->etype == EXPR_BINOP)
3542 10 expr = expr->left;
3543
3544
1/2
✗ Branch 0 (42→43) not taken.
✓ Branch 1 (42→44) taken 130 times.
130 if (expr->left->etype != EXPR_PAYLOAD)
3545 ✗ break;
3546
3547
1/2
✗ Branch 0 (45→46) not taken.
✓ Branch 1 (45→47) taken 130 times.
130 if (!payload_expr_cmp(payload, expr->left))
3548 ✗ break;
3549
3550 /* Adjust payload to fetch 16-bits. */
3551 130 expr->left->payload.offset = payload_byte_offset * BITS_PER_BYTE;
3552 130 expr->left->len = payload_byte_size * BITS_PER_BYTE;
3553 130 expr->left->payload.is_raw = 1;
3554
3555
1/2
✓ Branch 0 (47→48) taken 130 times.
✗ Branch 1 (47→61) not taken.
130 switch (expr->right->etype) {
3556 130 case EXPR_VALUE:
3557
2/2
✓ Branch 0 (48→49) taken 70 times.
✓ Branch 1 (48→50) taken 60 times.
130 if (shift_imm)
3558 70 mpz_lshift_ui(expr->right->value, shift_imm);
3559
3560 /* build bitmask to keep unmodified bits intact */
3561
2/2
✓ Branch 0 (50→51) taken 70 times.
✓ Branch 1 (50→60) taken 60 times.
130 if (expr->op == OP_AND) {
3562 70 masklen = payload_byte_size * BITS_PER_BYTE;
3563 70 mpz_init_bitmask(ff, masklen);
3564
3565 70 mpz_init2(bitmask, masklen);
3566 70 mpz_bitmask(bitmask, payload->len);
3567 70 mpz_lshift_ui(bitmask, shift_imm);
3568
3569 70 mpz_xor(bitmask, ff, bitmask);
3570 70 mpz_clear(ff);
3571
3572 70 mpz_ior(bitmask, expr->right->value, bitmask);
3573 70 mpz_set(expr->right->value, bitmask);
3574
3575 70 mpz_clear(bitmask);
3576 }
3577 130 break;
3578 ✗ default:
3579 ✗ return expr_error(ctx->msgs, expr->right,
3580 "payload statement for this expression is not supported");
3581 }
3582
3583 130 expr_free(stmt->payload.expr);
3584 /* statement payload is the same in expr and value, update it. */
3585 130 stmt->payload.expr = expr_clone(expr->left);
3586 130 payload = stmt->payload.expr;
3587 130 ctx->stmt_len = stmt->payload.expr->len;
3588
3589
1/2
✗ Branch 0 (65→66) not taken.
✓ Branch 1 (65→67) taken 130 times.
130 if (expr_evaluate(ctx, &stmt->payload.val) < 0)
3590 ✗ return -1;
3591
3592 130 return 0;
3593 ✗ default:
3594 ✗ return expr_error(ctx->msgs, stmt->payload.val,
3595 "payload statement for this expression is not supported");
3596 }
3597
3598 155 masklen = payload_byte_size * BITS_PER_BYTE;
3599 155 mpz_init_bitmask(ff, masklen);
3600
3601 155 mpz_init2(bitmask, masklen);
3602 155 mpz_bitmask(bitmask, payload->len);
3603 155 mpz_lshift_ui(bitmask, shift_imm);
3604
3605 155 mpz_xor(bitmask, ff, bitmask);
3606 155 mpz_clear(ff);
3607
3608
1/2
✗ Branch 0 (75→76) not taken.
✓ Branch 1 (75→77) taken 155 times.
155 assert(sizeof(data) * BITS_PER_BYTE >= masklen);
3609
1/2
✗ Branch 0 (77→78) not taken.
✓ Branch 1 (77→79) taken 155 times.
155 mpz_export_data(data, bitmask, payload->byteorder, payload_byte_size);
3610 155 mask = constant_expr_alloc(&payload->location, expr_basetype(payload),
3611 155 payload->byteorder, masklen, data);
3612 155 mpz_clear(bitmask);
3613
3614 155 payload_bytes = payload_expr_alloc(&payload->location, NULL, 0);
3615 155 payload_init_raw(payload_bytes, payload->payload.base,
3616 payload_byte_offset * BITS_PER_BYTE,
3617 payload_byte_size * BITS_PER_BYTE);
3618
3619 155 payload_bytes->payload.is_raw = 1;
3620 155 payload_bytes->payload.desc = payload->payload.desc;
3621 155 payload_bytes->byteorder = payload->byteorder;
3622
3623 155 payload->len = payload_bytes->len;
3624 155 payload->payload.offset = payload_bytes->payload.offset;
3625
3626 155 and = binop_expr_alloc(&payload->location, OP_AND, payload_bytes, mask);
3627
3628 155 and->dtype = payload_bytes->dtype;
3629 155 and->byteorder = payload_bytes->byteorder;
3630 155 and->len = payload_bytes->len;
3631
3632 155 xor = binop_expr_alloc(&payload->location, OP_XOR, and,
3633 stmt->payload.val);
3634 155 xor->dtype = payload->dtype;
3635 155 xor->byteorder = payload->byteorder;
3636 155 xor->len = mask->len;
3637
3638 155 stmt->payload.val = xor;
3639
3640 155 return expr_evaluate(ctx, &stmt->payload.val);
3641 }
3642
3643 176 static int stmt_evaluate_stateful(struct eval_ctx *ctx, struct stmt *stmt, const char *name)
3644 {
3645
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 176 times.
176 if (stmt_evaluate(ctx, stmt) < 0)
3646 ✗ return -1;
3647
3648
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 176 times.
176 if (!(stmt->flags & STMT_F_STATEFUL))
3649 ✗ return stmt_error(ctx, stmt, "%s statement must be stateful", name);
3650
3651 176 return 0;
3652 }
3653
3654 7 static int stmt_evaluate_meter(struct eval_ctx *ctx, struct stmt *stmt)
3655 {
3656 struct expr *key, *setref;
3657 struct set *existing_set;
3658 struct table *table;
3659
3660 7 table = table_cache_find(&ctx->nft->cache.table_cache,
3661 7 ctx->cmd->handle.table.name,
3662 7 ctx->cmd->handle.family);
3663
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 7 times.
7 if (table == NULL)
3664 ✗ return table_not_found(ctx);
3665
3666 7 existing_set = set_cache_find(table, stmt->meter.name);
3667
2/2
✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→20) taken 6 times.
7 if (existing_set &&
3668
2/4
✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (9→12) not taken.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→20) taken 1 times.
2 (!set_is_meter_compat(existing_set->flags) ||
3669 1 set_is_map(existing_set->flags)))
3670 ✗ return cmd_error(ctx, &stmt->location,
3671 "%s; meter '%s' overlaps an existing %s '%s' in family %s",
3672 strerror(EEXIST),
3673 stmt->meter.name,
3674 set_is_map(existing_set->flags) ? "map" : "set",
3675 existing_set->handle.set.name,
3676 family2str(existing_set->handle.family));
3677
3678 7 expr_set_context(&ctx->ectx, NULL, 0);
3679
1/2
✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 7 times.
7 if (expr_evaluate(ctx, &stmt->meter.key) < 0)
3680 ✗ return -1;
3681
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→28) taken 7 times.
7 if (expr_is_constant(stmt->meter.key))
3682 ✗ return expr_error(ctx->msgs, stmt->meter.key,
3683 "Meter key expression can not be constant");
3684
1/2
✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→31) taken 7 times.
7 if (stmt->meter.key->comment)
3685 ✗ return expr_error(ctx->msgs, stmt->meter.key,
3686 "Meter key expression can not contain comments");
3687
3688 /* Declare an empty set */
3689 7 key = stmt->meter.key;
3690
2/2
✓ Branch 0 (31→32) taken 1 times.
✓ Branch 1 (31→45) taken 6 times.
7 if (existing_set) {
3691
1/4
✗ Branch 0 (32→33) not taken.
✓ Branch 1 (32→36) taken 1 times.
✗ Branch 2 (33→34) not taken.
✗ Branch 3 (33→36) not taken.
1 if ((existing_set->flags & NFT_SET_TIMEOUT) && !key->timeout)
3692 ✗ return expr_error(ctx->msgs, stmt->meter.key,
3693 "existing set '%s' has timeout flag",
3694 stmt->meter.name);
3695
3696
2/4
✓ Branch 0 (36→37) taken 1 times.
✗ Branch 1 (36→40) not taken.
✗ Branch 2 (37→38) not taken.
✓ Branch 3 (37→40) taken 1 times.
1 if ((existing_set->flags & NFT_SET_TIMEOUT) == 0 && key->timeout)
3697 ✗ return expr_error(ctx->msgs, stmt->meter.key,
3698 "existing set '%s' lacks timeout flag",
3699 stmt->meter.name);
3700
3701
1/4
✗ Branch 0 (40→41) not taken.
✓ Branch 1 (40→44) taken 1 times.
✗ Branch 2 (41→42) not taken.
✗ Branch 3 (41→44) not taken.
1 if (stmt->meter.size > 0 && existing_set->desc.size != stmt->meter.size)
3702 ✗ return expr_error(ctx->msgs, stmt->meter.key,
3703 "existing set '%s' has size %u, meter has %u",
3704 stmt->meter.name, existing_set->desc.size,
3705 stmt->meter.size);
3706 1 setref = set_ref_expr_alloc(&key->location, existing_set);
3707 } else {
3708 struct expr *set;
3709
3710 6 set = set_expr_alloc(&key->location, existing_set);
3711
2/2
✓ Branch 0 (46→47) taken 3 times.
✓ Branch 1 (46→50) taken 3 times.
6 if (key->timeout)
3712
1/2
✗ Branch 0 (47→48) not taken.
✓ Branch 1 (47→49) taken 3 times.
3 expr_set(set)->set_flags |= NFT_SET_TIMEOUT;
3713
3714
1/2
✗ Branch 0 (50→51) not taken.
✓ Branch 1 (50→52) taken 6 times.
6 expr_set(set)->set_flags |= NFT_SET_EVAL;
3715 6 setref = implicit_set_declaration(ctx, stmt->meter.name,
3716 expr_get(key), NULL, set,
3717
1/2
✗ Branch 0 (52→53) not taken.
✓ Branch 1 (52→54) taken 6 times.
6 NFT_SET_EVAL | expr_set(set)->set_flags);
3718
1/2
✓ Branch 0 (56→57) taken 6 times.
✗ Branch 1 (56→58) not taken.
6 if (setref)
3719 6 setref->set->desc.size = stmt->meter.size;
3720 }
3721
3722
1/2
✗ Branch 0 (58→59) not taken.
✓ Branch 1 (58→60) taken 7 times.
7 if (!setref)
3723 ✗ return -1;
3724
3725 7 stmt->meter.set = setref;
3726
3727
1/2
✗ Branch 0 (61→62) not taken.
✓ Branch 1 (61→63) taken 7 times.
7 if (stmt_evaluate_stateful(ctx, stmt->meter.stmt, "meter") < 0)
3728 ✗ return -1;
3729
3730 7 return 0;
3731 }
3732
3733 524 static int stmt_evaluate_meta(struct eval_ctx *ctx, struct stmt *stmt)
3734 {
3735 int ret;
3736
3737 524 ctx->stmt_len = stmt->meta.tmpl->len;
3738
3739 524 ret = stmt_evaluate_arg(ctx, stmt,
3740 524 stmt->meta.tmpl->dtype,
3741 524 stmt->meta.tmpl->len,
3742 524 stmt->meta.tmpl->byteorder,
3743 &stmt->meta.expr);
3744
2/2
✓ Branch 0 (3→4) taken 5 times.
✓ Branch 1 (3→5) taken 519 times.
524 if (ret < 0)
3745 5 return ret;
3746
3747
2/2
✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→8) taken 518 times.
519 if (stmt->meta.expr->etype == EXPR_RANGE)
3748 1 return stmt_error_range(ctx, stmt, stmt->meta.expr);
3749
3750 518 return ret;
3751 }
3752
3753 278 static int stmt_evaluate_ct(struct eval_ctx *ctx, struct stmt *stmt)
3754 {
3755 int ret;
3756
3757 278 ctx->stmt_len = stmt->ct.tmpl->len;
3758
3759 278 ret = stmt_evaluate_arg(ctx, stmt,
3760 278 stmt->ct.tmpl->dtype,
3761 278 stmt->ct.tmpl->len,
3762 278 stmt->ct.tmpl->byteorder,
3763 &stmt->ct.expr);
3764
2/2
✓ Branch 0 (3→4) taken 3 times.
✓ Branch 1 (3→5) taken 275 times.
278 if (ret < 0)
3765 3 return -1;
3766
3767
3/4
✓ Branch 0 (5→6) taken 8 times.
✓ Branch 1 (5→10) taken 267 times.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→10) taken 8 times.
275 if (stmt->ct.key == NFT_CT_SECMARK && expr_is_constant(stmt->ct.expr))
3768 ✗ return stmt_error(ctx, stmt,
3769 "ct secmark must not be set to constant value");
3770
3771
2/2
✓ Branch 0 (10→11) taken 1 times.
✓ Branch 1 (10→13) taken 274 times.
275 if (stmt->ct.expr->etype == EXPR_RANGE)
3772 1 return stmt_error_range(ctx, stmt, stmt->ct.expr);
3773
3774 274 return 0;
3775 }
3776
3777 46 static int reject_payload_gen_dependency_tcp(struct eval_ctx *ctx,
3778 struct stmt *stmt,
3779 struct expr **payload)
3780 {
3781 46 struct proto_ctx *pctx = eval_proto_ctx(ctx);
3782 const struct proto_desc *desc;
3783
3784 46 desc = pctx->protocol[PROTO_BASE_TRANSPORT_HDR].desc;
3785
2/2
✓ Branch 0 (3→4) taken 35 times.
✓ Branch 1 (3→5) taken 11 times.
46 if (desc != NULL)
3786 35 return 0;
3787 11 *payload = payload_expr_alloc(&stmt->location, &proto_tcp,
3788 TCPHDR_UNSPEC);
3789 11 return 1;
3790 }
3791
3792 121 static int reject_payload_gen_dependency_family(struct eval_ctx *ctx,
3793 struct stmt *stmt,
3794 struct expr **payload)
3795 {
3796 121 struct proto_ctx *pctx = eval_proto_ctx(ctx);
3797 const struct proto_desc *base;
3798
3799 121 base = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
3800
2/2
✓ Branch 0 (3→4) taken 41 times.
✓ Branch 1 (3→5) taken 80 times.
121 if (base != NULL)
3801 41 return 0;
3802
3803
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 80 times.
80 if (stmt->reject.icmp_code < 0)
3804 ✗ return stmt_error(ctx, stmt, "missing icmp error type");
3805
3806 /* Generate a network dependency */
3807
2/3
✓ Branch 0 (8→9) taken 47 times.
✓ Branch 1 (8→11) taken 33 times.
✗ Branch 2 (8→13) not taken.
80 switch (stmt->reject.family) {
3808 47 case NFPROTO_IPV4:
3809 47 *payload = payload_expr_alloc(&stmt->location, &proto_ip,
3810 IPHDR_PROTOCOL);
3811 47 break;
3812 33 case NFPROTO_IPV6:
3813 33 *payload = payload_expr_alloc(&stmt->location, &proto_ip6,
3814 IP6HDR_NEXTHDR);
3815 33 break;
3816 ✗ default:
3817 ✗ BUG("unknown reject family");
3818 }
3819 80 return 1;
3820 }
3821
3822 167 static int stmt_reject_gen_dependency(struct eval_ctx *ctx, struct stmt *stmt)
3823 {
3824 167 struct expr *payload = NULL;
3825 struct stmt *nstmt;
3826 int ret;
3827
3828
2/3
✓ Branch 0 (2→3) taken 46 times.
✓ Branch 1 (2→4) taken 121 times.
✗ Branch 2 (2→6) not taken.
167 switch (stmt->reject.type) {
3829 46 case NFT_REJECT_TCP_RST:
3830 46 ret = reject_payload_gen_dependency_tcp(ctx, stmt, &payload);
3831 46 break;
3832 121 case NFT_REJECT_ICMP_UNREACH:
3833 121 ret = reject_payload_gen_dependency_family(ctx, stmt, &payload);
3834 121 break;
3835 ✗ default:
3836 ✗ BUG("cannot generate reject dependency for type %d",
3837 stmt->reject.type);
3838 }
3839
2/2
✓ Branch 0 (9→10) taken 76 times.
✓ Branch 1 (9→11) taken 91 times.
167 if (ret <= 0)
3840 76 return ret;
3841
3842
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 91 times.
91 if (payload_gen_dependency(ctx, payload, &nstmt) < 0) {
3843 ✗ ret = -1;
3844 ✗ goto out;
3845 }
3846
3847 /*
3848 * Unlike payload deps this adds the dependency at the beginning, i.e.
3849 * log ... reject with tcp-reset
3850 * turns into
3851 * meta l4proto tcp log ... reject with tcp-reset
3852 *
3853 * Otherwise we'd log things that won't be rejected.
3854 */
3855 91 list_add(&nstmt->list, &ctx->rule->stmts);
3856 91 out:
3857 91 free(payload);
3858 91 return ret;
3859 }
3860
3861 39 static int stmt_evaluate_reject_inet_family(struct eval_ctx *ctx,
3862 struct stmt *stmt,
3863 const struct proto_desc *desc)
3864 {
3865 39 struct proto_ctx *pctx = eval_proto_ctx(ctx);
3866 const struct proto_desc *base;
3867 int protocol;
3868
3869
3/4
✓ Branch 0 (3→4) taken 8 times.
✓ Branch 1 (3→5) taken 4 times.
✓ Branch 2 (3→6) taken 27 times.
✗ Branch 3 (3→16) not taken.
39 switch (stmt->reject.type) {
3870 8 case NFT_REJECT_TCP_RST:
3871 8 break;
3872 4 case NFT_REJECT_ICMPX_UNREACH:
3873 4 break;
3874 27 case NFT_REJECT_ICMP_UNREACH:
3875 27 base = pctx->protocol[PROTO_BASE_LL_HDR].desc;
3876 27 protocol = proto_find_num(base, desc);
3877
2/3
✓ Branch 0 (7→8) taken 10 times.
✓ Branch 1 (7→11) taken 17 times.
✗ Branch 2 (7→14) not taken.
27 switch (protocol) {
3878 10 case NFPROTO_IPV4:
3879 case __constant_htons(ETH_P_IP):
3880
2/2
✓ Branch 0 (8→9) taken 9 times.
✓ Branch 1 (8→10) taken 1 times.
10 if (stmt->reject.family == NFPROTO_IPV4)
3881 9 break;
3882 1 return stmt_binary_error(ctx, stmt->reject.expr,
3883 &pctx->protocol[PROTO_BASE_NETWORK_HDR],
3884 "conflicting protocols specified: ip vs ip6");
3885 17 case NFPROTO_IPV6:
3886 case __constant_htons(ETH_P_IPV6):
3887
2/2
✓ Branch 0 (11→12) taken 16 times.
✓ Branch 1 (11→13) taken 1 times.
17 if (stmt->reject.family == NFPROTO_IPV6)
3888 16 break;
3889 1 return stmt_binary_error(ctx, stmt->reject.expr,
3890 &pctx->protocol[PROTO_BASE_NETWORK_HDR],
3891 "conflicting protocols specified: ip vs ip6");
3892 ✗ default:
3893 ✗ return stmt_error(ctx, stmt,
3894 "cannot infer ICMP reject variant to use: explicit value required.\n");
3895 }
3896 25 break;
3897 }
3898
3899 37 return 0;
3900 }
3901
3902 115 static int stmt_evaluate_reject_inet(struct eval_ctx *ctx, struct stmt *stmt)
3903 {
3904 115 struct proto_ctx *pctx = eval_proto_ctx(ctx);
3905 const struct proto_desc *desc;
3906
3907 115 desc = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
3908
4/4
✓ Branch 0 (3→4) taken 39 times.
✓ Branch 1 (3→7) taken 76 times.
✓ Branch 2 (5→6) taken 2 times.
✓ Branch 3 (5→7) taken 37 times.
154 if (desc != NULL &&
3909 39 stmt_evaluate_reject_inet_family(ctx, stmt, desc) < 0)
3910 2 return -1;
3911
2/2
✓ Branch 0 (7→8) taken 45 times.
✓ Branch 1 (7→9) taken 68 times.
113 if (stmt->reject.type == NFT_REJECT_ICMPX_UNREACH)
3912 45 return 0;
3913
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 68 times.
68 if (stmt_reject_gen_dependency(ctx, stmt) < 0)
3914 ✗ return -1;
3915 68 return 0;
3916 }
3917
3918 34 static int stmt_evaluate_reject_bridge_family(struct eval_ctx *ctx,
3919 struct stmt *stmt,
3920 const struct proto_desc *desc)
3921 {
3922 34 struct proto_ctx *pctx = eval_proto_ctx(ctx);
3923 const struct proto_desc *base;
3924 int protocol;
3925
3926
2/3
✓ Branch 0 (3→4) taken 13 times.
✓ Branch 1 (3→9) taken 21 times.
✗ Branch 2 (3→19) not taken.
34 switch (stmt->reject.type) {
3927 13 case NFT_REJECT_ICMPX_UNREACH:
3928 case NFT_REJECT_TCP_RST:
3929 13 base = pctx->protocol[PROTO_BASE_LL_HDR].desc;
3930 13 protocol = proto_find_num(base, desc);
3931
2/2
✓ Branch 0 (5→6) taken 11 times.
✓ Branch 1 (5→7) taken 2 times.
13 switch (protocol) {
3932 11 case __constant_htons(ETH_P_IP):
3933 case __constant_htons(ETH_P_IPV6):
3934 11 break;
3935 2 default:
3936 2 return stmt_binary_error(ctx, stmt,
3937 &pctx->protocol[PROTO_BASE_NETWORK_HDR],
3938 "cannot reject this network family");
3939 }
3940 11 break;
3941 21 case NFT_REJECT_ICMP_UNREACH:
3942 21 base = pctx->protocol[PROTO_BASE_LL_HDR].desc;
3943 21 protocol = proto_find_num(base, desc);
3944
3/3
✓ Branch 0 (10→11) taken 10 times.
✓ Branch 1 (10→14) taken 10 times.
✓ Branch 2 (10→17) taken 1 times.
21 switch (protocol) {
3945 10 case __constant_htons(ETH_P_IP):
3946
2/2
✓ Branch 0 (11→12) taken 8 times.
✓ Branch 1 (11→13) taken 2 times.
10 if (NFPROTO_IPV4 == stmt->reject.family)
3947 8 break;
3948 2 return stmt_binary_error(ctx, stmt->reject.expr,
3949 &pctx->protocol[PROTO_BASE_NETWORK_HDR],
3950 "conflicting protocols specified: ip vs ip6");
3951 10 case __constant_htons(ETH_P_IPV6):
3952
2/2
✓ Branch 0 (14→15) taken 8 times.
✓ Branch 1 (14→16) taken 2 times.
10 if (NFPROTO_IPV6 == stmt->reject.family)
3953 8 break;
3954 2 return stmt_binary_error(ctx, stmt->reject.expr,
3955 &pctx->protocol[PROTO_BASE_NETWORK_HDR],
3956 "conflicting protocols specified: ip vs ip6");
3957 1 default:
3958 1 return stmt_binary_error(ctx, stmt,
3959 &pctx->protocol[PROTO_BASE_NETWORK_HDR],
3960 "cannot reject this network family");
3961 }
3962 16 break;
3963 }
3964
3965 27 return 0;
3966 }
3967
3968 123 static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt)
3969 {
3970 123 struct proto_ctx *pctx = eval_proto_ctx(ctx);
3971 const struct proto_desc *desc;
3972
3973 123 desc = pctx->protocol[PROTO_BASE_LL_HDR].desc;
3974
5/6
✓ Branch 0 (3→4) taken 72 times.
✓ Branch 1 (3→8) taken 51 times.
✓ Branch 2 (4→5) taken 64 times.
✓ Branch 3 (4→8) taken 8 times.
✗ Branch 4 (5→6) not taken.
✓ Branch 5 (5→8) taken 64 times.
123 if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
3975 ✗ return __stmt_binary_error(ctx, &stmt->location, NULL,
3976 "cannot reject from this link layer protocol");
3977
3978 123 desc = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
3979
4/4
✓ Branch 0 (8→9) taken 34 times.
✓ Branch 1 (8→12) taken 89 times.
✓ Branch 2 (10→11) taken 7 times.
✓ Branch 3 (10→12) taken 27 times.
157 if (desc != NULL &&
3980 34 stmt_evaluate_reject_bridge_family(ctx, stmt, desc) < 0)
3981 7 return -1;
3982
2/2
✓ Branch 0 (12→13) taken 35 times.
✓ Branch 1 (12→14) taken 81 times.
116 if (stmt->reject.type == NFT_REJECT_ICMPX_UNREACH)
3983 35 return 0;
3984
1/2
✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 81 times.
81 if (stmt_reject_gen_dependency(ctx, stmt) < 0)
3985 ✗ return -1;
3986 81 return 0;
3987 }
3988
3989 3 static int stmt_reject_error(struct eval_ctx *ctx,
3990 const struct stmt *stmt,
3991 const char *msg)
3992 {
3993 3 struct expr *e = stmt->reject.expr;
3994
3995
2/2
✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→5) taken 1 times.
3 if (e)
3996 2 return stmt_binary_error(ctx, e, stmt, "%s", msg);
3997
3998 1 return stmt_error(ctx, stmt, "%s", msg);
3999 }
4000
4001 312 static int stmt_evaluate_reject_family(struct eval_ctx *ctx, struct stmt *stmt)
4002 {
4003 312 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4004
4005
3/5
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 74 times.
✓ Branch 2 (3→15) taken 123 times.
✓ Branch 3 (3→19) taken 115 times.
✗ Branch 4 (3→23) not taken.
312 switch (pctx->family) {
4006 ✗ case NFPROTO_ARP:
4007 ✗ return stmt_error(ctx, stmt, "cannot use reject with arp");
4008 74 case NFPROTO_IPV4:
4009 case NFPROTO_IPV6:
4010
2/4
✓ Branch 0 (5→6) taken 18 times.
✗ Branch 1 (5→10) not taken.
✓ Branch 2 (5→11) taken 56 times.
✗ Branch 3 (5→14) not taken.
74 switch (stmt->reject.type) {
4011 18 case NFT_REJECT_TCP_RST:
4012
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 18 times.
18 if (stmt_reject_gen_dependency(ctx, stmt) < 0)
4013 ✗ return -1;
4014 18 break;
4015 ✗ case NFT_REJECT_ICMPX_UNREACH:
4016 ✗ return stmt_reject_error(ctx, stmt,
4017 "abstracted ICMP unreachable not supported");
4018 56 case NFT_REJECT_ICMP_UNREACH:
4019
2/2
✓ Branch 0 (11→12) taken 53 times.
✓ Branch 1 (11→13) taken 3 times.
56 if (stmt->reject.family == pctx->family)
4020 53 break;
4021 3 return stmt_reject_error(ctx, stmt,
4022 "conflicting protocols specified: ip vs ip6");
4023 }
4024 71 break;
4025 123 case NFPROTO_BRIDGE:
4026 case NFPROTO_NETDEV:
4027
2/2
✓ Branch 0 (16→17) taken 7 times.
✓ Branch 1 (16→18) taken 116 times.
123 if (stmt_evaluate_reject_bridge(ctx, stmt) < 0)
4028 7 return -1;
4029 116 break;
4030 115 case NFPROTO_INET:
4031
2/2
✓ Branch 0 (20→21) taken 2 times.
✓ Branch 1 (20→22) taken 113 times.
115 if (stmt_evaluate_reject_inet(ctx, stmt) < 0)
4032 2 return -1;
4033 113 break;
4034 }
4035
4036 300 stmt->flags |= STMT_F_TERMINAL;
4037 300 return 0;
4038 }
4039
4040 55 static int stmt_evaluate_reject_default(struct eval_ctx *ctx,
4041 struct stmt *stmt)
4042 {
4043 55 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4044 const struct proto_desc *desc, *base;
4045 int protocol;
4046
4047
3/4
✓ Branch 0 (3→4) taken 18 times.
✓ Branch 1 (3→8) taken 17 times.
✓ Branch 2 (3→15) taken 20 times.
✗ Branch 3 (3→22) not taken.
55 switch (pctx->family) {
4048 18 case NFPROTO_IPV4:
4049 case NFPROTO_IPV6:
4050 18 stmt->reject.type = NFT_REJECT_ICMP_UNREACH;
4051 18 stmt->reject.family = pctx->family;
4052
2/2
✓ Branch 0 (4→5) taken 10 times.
✓ Branch 1 (4→6) taken 8 times.
18 if (pctx->family == NFPROTO_IPV4)
4053 10 stmt->reject.icmp_code = ICMP_PORT_UNREACH;
4054 else
4055 8 stmt->reject.icmp_code = ICMP6_DST_UNREACH_NOPORT;
4056 18 break;
4057 17 case NFPROTO_INET:
4058 17 desc = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
4059
2/2
✓ Branch 0 (8→9) taken 11 times.
✓ Branch 1 (8→10) taken 6 times.
17 if (desc == NULL) {
4060 11 stmt->reject.type = NFT_REJECT_ICMPX_UNREACH;
4061 11 stmt->reject.icmp_code = NFT_REJECT_ICMPX_PORT_UNREACH;
4062 11 break;
4063 }
4064 6 stmt->reject.type = NFT_REJECT_ICMP_UNREACH;
4065 6 base = pctx->protocol[PROTO_BASE_LL_HDR].desc;
4066 6 protocol = proto_find_num(base, desc);
4067
2/3
✓ Branch 0 (11→12) taken 3 times.
✓ Branch 1 (11→13) taken 3 times.
✗ Branch 2 (11→14) not taken.
6 switch (protocol) {
4068 3 case NFPROTO_IPV4:
4069 case __constant_htons(ETH_P_IP):
4070 3 stmt->reject.family = NFPROTO_IPV4;
4071 3 stmt->reject.icmp_code = ICMP_PORT_UNREACH;
4072 3 break;
4073 3 case NFPROTO_IPV6:
4074 case __constant_htons(ETH_P_IPV6):
4075 3 stmt->reject.family = NFPROTO_IPV6;
4076 3 stmt->reject.icmp_code = ICMP6_DST_UNREACH_NOPORT;
4077 3 break;
4078 }
4079 6 break;
4080 20 case NFPROTO_BRIDGE:
4081 case NFPROTO_NETDEV:
4082 20 desc = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
4083
2/2
✓ Branch 0 (15→16) taken 9 times.
✓ Branch 1 (15→17) taken 11 times.
20 if (desc == NULL) {
4084 9 stmt->reject.type = NFT_REJECT_ICMPX_UNREACH;
4085 9 stmt->reject.icmp_code = NFT_REJECT_ICMPX_PORT_UNREACH;
4086 9 break;
4087 }
4088 11 stmt->reject.type = NFT_REJECT_ICMP_UNREACH;
4089 11 base = pctx->protocol[PROTO_BASE_LL_HDR].desc;
4090 11 protocol = proto_find_num(base, desc);
4091
3/3
✓ Branch 0 (18→19) taken 5 times.
✓ Branch 1 (18→20) taken 5 times.
✓ Branch 2 (18→21) taken 1 times.
11 switch (protocol) {
4092 5 case __constant_htons(ETH_P_IP):
4093 5 stmt->reject.family = NFPROTO_IPV4;
4094 5 stmt->reject.icmp_code = ICMP_PORT_UNREACH;
4095 5 break;
4096 5 case __constant_htons(ETH_P_IPV6):
4097 5 stmt->reject.family = NFPROTO_IPV6;
4098 5 stmt->reject.icmp_code = ICMP6_DST_UNREACH_NOPORT;
4099 5 break;
4100 }
4101 11 break;
4102 }
4103 55 return 0;
4104 }
4105
4106 212 static int stmt_evaluate_reject_icmp(struct eval_ctx *ctx, struct stmt *stmt)
4107 {
4108 212 struct parse_ctx parse_ctx = {
4109 212 .tbl = &ctx->nft->output.tbl,
4110 212 .input = &ctx->nft->input,
4111 };
4112 struct error_record *erec;
4113 struct expr *code;
4114
4115 212 erec = symbol_parse(&parse_ctx, stmt->reject.expr, &code);
4116
2/2
✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→6) taken 210 times.
212 if (erec != NULL) {
4117 2 erec_queue(erec, ctx->msgs);
4118 2 return -1;
4119 }
4120
4121
2/2
✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→10) taken 209 times.
210 if (mpz_cmp_ui(code->value, UINT8_MAX) > 0) {
4122 1 expr_free(code);
4123 1 return expr_error(ctx->msgs, stmt->reject.expr,
4124 "reject code must be integer in range 0-255");
4125 }
4126
4127 209 stmt->reject.icmp_code = mpz_get_uint8(code->value);
4128 209 expr_free(code);
4129
4130 209 return 0;
4131 }
4132
4133 51 static int stmt_evaluate_reset(struct eval_ctx *ctx, struct stmt *stmt)
4134 {
4135 51 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4136 const struct proto_desc *desc, *base;
4137 int protonum;
4138
4139 51 desc = pctx->protocol[PROTO_BASE_TRANSPORT_HDR].desc;
4140
2/2
✓ Branch 0 (3→4) taken 13 times.
✓ Branch 1 (3→5) taken 38 times.
51 if (desc == NULL)
4141 13 return 0;
4142
4143 38 base = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
4144
2/2
✓ Branch 0 (5→6) taken 14 times.
✓ Branch 1 (5→7) taken 24 times.
38 if (base == NULL)
4145 14 base = &proto_inet_service;
4146
4147 38 protonum = proto_find_num(base, desc);
4148
2/2
✓ Branch 0 (8→9) taken 35 times.
✓ Branch 1 (8→10) taken 3 times.
38 switch (protonum) {
4149 35 case IPPROTO_TCP:
4150 35 break;
4151 3 default:
4152
1/2
✓ Branch 0 (10→11) taken 3 times.
✗ Branch 1 (10→13) not taken.
3 if (stmt->reject.type == NFT_REJECT_TCP_RST) {
4153 3 return stmt_binary_error(ctx, stmt,
4154 &pctx->protocol[PROTO_BASE_TRANSPORT_HDR],
4155 "you cannot use tcp reset with this protocol");
4156 }
4157 ✗ break;
4158 }
4159 35 return 0;
4160 }
4161
4162 318 static int stmt_evaluate_reject(struct eval_ctx *ctx, struct stmt *stmt)
4163 {
4164
2/2
✓ Branch 0 (2→3) taken 55 times.
✓ Branch 1 (2→6) taken 263 times.
318 if (stmt->reject.icmp_code < 0) {
4165
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→13) taken 55 times.
55 if (stmt_evaluate_reject_default(ctx, stmt) < 0)
4166 ✗ return -1;
4167
2/2
✓ Branch 0 (6→7) taken 212 times.
✓ Branch 1 (6→10) taken 51 times.
263 } else if (stmt->reject.expr != NULL) {
4168
2/2
✓ Branch 0 (8→9) taken 3 times.
✓ Branch 1 (8→13) taken 209 times.
212 if (stmt_evaluate_reject_icmp(ctx, stmt) < 0)
4169 3 return -1;
4170 } else {
4171
2/2
✓ Branch 0 (11→12) taken 3 times.
✓ Branch 1 (11→13) taken 48 times.
51 if (stmt_evaluate_reset(ctx, stmt) < 0)
4172 3 return -1;
4173 }
4174
4175 312 return stmt_evaluate_reject_family(ctx, stmt);
4176 }
4177
4178 694 static int nat_evaluate_family(struct eval_ctx *ctx, struct stmt *stmt)
4179 {
4180 694 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4181 const struct proto_desc *nproto;
4182
4183
2/3
✓ Branch 0 (3→4) taken 519 times.
✓ Branch 1 (3→7) taken 175 times.
✗ Branch 2 (3→16) not taken.
694 switch (pctx->family) {
4184 519 case NFPROTO_IPV4:
4185 case NFPROTO_IPV6:
4186
2/2
✓ Branch 0 (4→5) taken 406 times.
✓ Branch 1 (4→6) taken 113 times.
519 if (stmt->nat.family == NFPROTO_UNSPEC)
4187 406 stmt->nat.family = pctx->family;
4188 519 return 0;
4189 175 case NFPROTO_INET:
4190
2/2
✓ Branch 0 (7→8) taken 33 times.
✓ Branch 1 (7→9) taken 142 times.
175 if (!stmt->nat.addr) {
4191 33 stmt->nat.family = NFPROTO_INET;
4192 33 return 0;
4193 }
4194
2/2
✓ Branch 0 (9→10) taken 107 times.
✓ Branch 1 (9→11) taken 35 times.
142 if (stmt->nat.family != NFPROTO_UNSPEC)
4195 107 return 0;
4196
4197 35 nproto = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
4198
4199
2/2
✓ Branch 0 (11→12) taken 20 times.
✓ Branch 1 (11→13) taken 15 times.
35 if (nproto == &proto_ip)
4200 20 stmt->nat.family = NFPROTO_IPV4;
4201
2/2
✓ Branch 0 (13→14) taken 4 times.
✓ Branch 1 (13→15) taken 11 times.
15 else if (nproto == &proto_ip6)
4202 4 stmt->nat.family = NFPROTO_IPV6;
4203
4204 35 return 0;
4205 ✗ default:
4206 ✗ return stmt_error(ctx, stmt,
4207 "NAT is only supported for IPv4/IPv6");
4208 }
4209 }
4210
4211 419 static const struct datatype *get_addr_dtype(uint8_t family)
4212 {
4213
3/3
✓ Branch 0 (2→3) taken 296 times.
✓ Branch 1 (2→4) taken 115 times.
✓ Branch 2 (2→5) taken 8 times.
419 switch (family) {
4214 296 case NFPROTO_IPV4:
4215 296 return &ipaddr_type;
4216 115 case NFPROTO_IPV6:
4217 115 return &ip6addr_type;
4218 }
4219
4220 8 return &invalid_type;
4221 }
4222
4223 230 static int evaluate_addr(struct eval_ctx *ctx, struct stmt *stmt,
4224 struct expr **expr)
4225 {
4226 230 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4227 const struct datatype *dtype;
4228
4229 230 dtype = get_addr_dtype(pctx->family);
4230
4231 230 return stmt_evaluate_arg(ctx, stmt, dtype, dtype->size,
4232 BYTEORDER_BIG_ENDIAN,
4233 expr);
4234 }
4235
4236 8 static bool nat_evaluate_addr_has_th_expr(const struct expr *map)
4237 {
4238 const struct expr *i, *concat;
4239
4240
4/4
✓ Branch 0 (2→3) taken 6 times.
✓ Branch 1 (2→4) taken 2 times.
✓ Branch 2 (3→4) taken 3 times.
✓ Branch 3 (3→5) taken 3 times.
8 if (!map || map->etype != EXPR_MAP)
4241 5 return false;
4242
4243 3 concat = map->map;
4244
2/2
✓ Branch 0 (5→6) taken 2 times.
✓ Branch 1 (5→7) taken 1 times.
3 if (concat ->etype != EXPR_CONCAT)
4245 2 return false;
4246
4247
4/6
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 1 times.
✗ Branch 2 (23→24) not taken.
✓ Branch 3 (23→25) taken 3 times.
✓ Branch 4 (25→10) taken 2 times.
✓ Branch 5 (25→26) taken 1 times.
3 list_for_each_entry(i, &expr_concat(concat)->expressions, list) {
4248 enum proto_bases base;
4249
4250
1/2
✓ Branch 0 (10→11) taken 2 times.
✗ Branch 1 (10→14) not taken.
2 if (i->etype == EXPR_PAYLOAD &&
4251
2/2
✓ Branch 0 (11→12) taken 1 times.
✓ Branch 1 (11→14) taken 1 times.
2 i->payload.base == PROTO_BASE_TRANSPORT_HDR &&
4252
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 1 times.
1 i->payload.desc != &proto_th)
4253 ✗ return true;
4254
4255
1/2
✓ Branch 0 (14→15) taken 2 times.
✗ Branch 1 (14→16) not taken.
2 if ((i->flags & EXPR_F_PROTOCOL) == 0)
4256 2 continue;
4257
4258 ✗ switch (i->etype) {
4259 ✗ case EXPR_META:
4260 ✗ base = i->meta.base;
4261 ✗ break;
4262 ✗ case EXPR_PAYLOAD:
4263 ✗ base = i->payload.base;
4264 ✗ break;
4265 ✗ default:
4266 ✗ return false;
4267 }
4268
4269 ✗ if (base == PROTO_BASE_NETWORK_HDR)
4270 ✗ return true;
4271 }
4272
4273 1 return false;
4274 }
4275
4276 276 static int nat_evaluate_transport(struct eval_ctx *ctx, struct stmt *stmt,
4277 struct expr **expr)
4278 {
4279 276 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4280 int err;
4281
4282 276 err = stmt_evaluate_arg(ctx, stmt,
4283 &inet_service_type, 2 * BITS_PER_BYTE,
4284 BYTEORDER_BIG_ENDIAN, expr);
4285
2/2
✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→6) taken 273 times.
276 if (err < 0)
4286 3 return err;
4287
4288
2/2
✓ Branch 0 (6→7) taken 5 times.
✓ Branch 1 (6→11) taken 268 times.
273 if (pctx->protocol[PROTO_BASE_TRANSPORT_HDR].desc == NULL &&
4289
1/2
✓ Branch 0 (8→9) taken 5 times.
✗ Branch 1 (8→11) not taken.
5 !nat_evaluate_addr_has_th_expr(stmt->nat.addr))
4290 5 return stmt_binary_error(ctx, *expr, stmt,
4291 "transport protocol mapping is only "
4292 "valid after transport protocol match");
4293
4294 268 return 0;
4295 }
4296
4297 473 static int stmt_evaluate_l3proto(struct eval_ctx *ctx,
4298 struct stmt *stmt, uint8_t family)
4299 {
4300 473 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4301 const struct proto_desc *nproto;
4302
4303 473 nproto = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc;
4304
4305
6/6
✓ Branch 0 (3→4) taken 273 times.
✓ Branch 1 (3→5) taken 200 times.
✓ Branch 2 (4→5) taken 272 times.
✓ Branch 3 (4→7) taken 1 times.
✓ Branch 4 (5→6) taken 84 times.
✓ Branch 5 (5→11) taken 388 times.
473 if ((nproto == &proto_ip && family != NFPROTO_IPV4) ||
4306
2/2
✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→11) taken 83 times.
84 (nproto == &proto_ip6 && family != NFPROTO_IPV6))
4307 2 return stmt_binary_error(ctx, stmt,
4308 &pctx->protocol[PROTO_BASE_NETWORK_HDR],
4309 "conflicting protocols specified: %s vs. %s. You must specify ip or ip6 family in %s statement",
4310 pctx->protocol[PROTO_BASE_NETWORK_HDR].desc->name,
4311 family2str(family),
4312 stmt_name(stmt));
4313 471 return 0;
4314 }
4315
4316 12 static void expr_family_infer(struct proto_ctx *pctx, const struct expr *expr,
4317 uint8_t *family)
4318 {
4319 struct expr *i;
4320
4321
2/2
✓ Branch 0 (2→3) taken 4 times.
✓ Branch 1 (2→23) taken 8 times.
12 if (expr->etype == EXPR_MAP) {
4322
2/3
✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→17) taken 3 times.
✗ Branch 2 (3→22) not taken.
4 switch (expr->map->etype) {
4323 1 case EXPR_CONCAT:
4324
4/6
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 1 times.
✗ Branch 2 (13→14) not taken.
✓ Branch 3 (13→15) taken 3 times.
✓ Branch 4 (15→7) taken 2 times.
✓ Branch 5 (15→16) taken 1 times.
3 list_for_each_entry(i, &expr_concat(expr->map)->expressions, list) {
4325
1/2
✓ Branch 0 (7→8) taken 2 times.
✗ Branch 1 (7→12) not taken.
2 if (i->etype == EXPR_PAYLOAD) {
4326
2/2
✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→10) taken 1 times.
2 if (i->payload.desc == &proto_ip)
4327 1 *family = NFPROTO_IPV4;
4328
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 1 times.
1 else if (i->payload.desc == &proto_ip6)
4329 ✗ *family = NFPROTO_IPV6;
4330 }
4331 }
4332 1 break;
4333 3 case EXPR_PAYLOAD:
4334
2/2
✓ Branch 0 (17→18) taken 2 times.
✓ Branch 1 (17→19) taken 1 times.
3 if (expr->map->payload.desc == &proto_ip)
4335 2 *family = NFPROTO_IPV4;
4336
1/2
✓ Branch 0 (19→20) taken 1 times.
✗ Branch 1 (19→21) not taken.
1 else if (expr->map->payload.desc == &proto_ip6)
4337 1 *family = NFPROTO_IPV6;
4338 3 break;
4339 ✗ default:
4340 ✗ break;
4341 }
4342 }
4343 12 }
4344
4345 338 static int stmt_evaluate_addr(struct eval_ctx *ctx, struct stmt *stmt,
4346 uint8_t *family, struct expr **addr)
4347 {
4348 338 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4349 const struct datatype *dtype;
4350 int err;
4351
4352
2/2
✓ Branch 0 (3→4) taken 121 times.
✓ Branch 1 (3→15) taken 217 times.
338 if (pctx->family == NFPROTO_INET) {
4353
1/2
✓ Branch 0 (4→5) taken 121 times.
✗ Branch 1 (4→6) not taken.
121 if (*family == NFPROTO_INET ||
4354
2/2
✓ Branch 0 (5→6) taken 12 times.
✓ Branch 1 (5→7) taken 109 times.
121 *family == NFPROTO_UNSPEC)
4355 12 expr_family_infer(pctx, *addr, family);
4356
4357 121 dtype = get_addr_dtype(*family);
4358
2/2
✓ Branch 0 (8→9) taken 8 times.
✓ Branch 1 (8→14) taken 113 times.
121 if (dtype->size == 0) {
4359 8 return stmt_error(ctx, stmt,
4360 "specify `%s ip' or '%s ip6' in %s table to disambiguate",
4361 stmt_name(stmt), stmt_name(stmt), family2str(pctx->family));
4362 }
4363
4364 113 err = stmt_evaluate_arg(ctx, stmt, dtype, dtype->size,
4365 BYTEORDER_BIG_ENDIAN, addr);
4366 } else {
4367 217 err = evaluate_addr(ctx, stmt, addr);
4368 }
4369
4370 330 return err;
4371 }
4372
4373 107 static int stmt_evaluate_nat_map(struct eval_ctx *ctx, struct stmt *stmt)
4374 {
4375 107 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4376 struct expr *one, *two, *data, *tmp;
4377 107 const struct datatype *dtype = NULL;
4378 const struct datatype *dtype2;
4379 int addr_type;
4380 int err;
4381
4382
2/2
✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→6) taken 106 times.
107 if (stmt->nat.proto)
4383 1 return stmt_binary_error(ctx, stmt, stmt->nat.proto,
4384 "nat map and protocol are mutually exclusive");
4385
4386
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 106 times.
106 if (stmt->nat.family == NFPROTO_INET)
4387 ✗ expr_family_infer(pctx, stmt->nat.addr, &stmt->nat.family);
4388
4389
3/3
✓ Branch 0 (8→9) taken 78 times.
✓ Branch 1 (8→10) taken 25 times.
✓ Branch 2 (8→11) taken 3 times.
106 switch (stmt->nat.family) {
4390 78 case NFPROTO_IPV4:
4391 78 addr_type = TYPE_IPADDR;
4392 78 break;
4393 25 case NFPROTO_IPV6:
4394 25 addr_type = TYPE_IP6ADDR;
4395 25 break;
4396 3 default:
4397 3 return stmt_error(ctx, stmt,
4398 "specify `%s ip' or '%s ip6' in %s table to disambiguate",
4399 stmt_name(stmt), stmt_name(stmt), family2str(pctx->family));
4400 }
4401 103 dtype = concat_type_alloc((addr_type << TYPE_BITS) | TYPE_INET_SERVICE);
4402
4403 103 expr_set_context(&ctx->ectx, dtype, dtype->size);
4404
2/2
✓ Branch 0 (18→19) taken 1 times.
✓ Branch 1 (18→20) taken 102 times.
103 if (expr_evaluate(ctx, &stmt->nat.addr)) {
4405 1 err = -1;
4406 1 goto out;
4407 }
4408
4409
2/2
✓ Branch 0 (20→21) taken 3 times.
✓ Branch 1 (20→25) taken 99 times.
102 if (pctx->protocol[PROTO_BASE_TRANSPORT_HDR].desc == NULL &&
4410
1/2
✓ Branch 0 (22→23) taken 3 times.
✗ Branch 1 (22→25) not taken.
3 !nat_evaluate_addr_has_th_expr(stmt->nat.addr)) {
4411 3 err = stmt_binary_error(ctx, stmt->nat.addr, stmt,
4412 "transport protocol mapping is only "
4413 "valid after transport protocol match");
4414 3 goto out;
4415 }
4416
4417
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 99 times.
99 if (stmt->nat.addr->etype != EXPR_MAP) {
4418 ✗ err = 0;
4419 ✗ goto out;
4420 }
4421
4422 99 data = stmt->nat.addr->mappings->set->data;
4423
2/2
✓ Branch 0 (27→28) taken 20 times.
✓ Branch 1 (27→29) taken 79 times.
99 if (data->flags & EXPR_F_INTERVAL)
4424 20 stmt->nat.type_flags |= STMT_NAT_F_INTERVAL;
4425
4426 99 datatype_set(data, dtype);
4427
4428
2/2
✓ Branch 0 (31→32) taken 31 times.
✓ Branch 1 (31→34) taken 68 times.
99 if (expr_ops(data)->type != EXPR_CONCAT) {
4429 31 err = __stmt_evaluate_arg(ctx, stmt, dtype, dtype->size,
4430 BYTEORDER_BIG_ENDIAN,
4431 &stmt->nat.addr);
4432 31 goto out;
4433 }
4434
4435
1/2
✗ Branch 0 (34→35) not taken.
✓ Branch 1 (34→36) taken 68 times.
68 one = list_first_entry(&expr_concat(data)->expressions, struct expr, list);
4436 68 two = list_entry(one->list.next, struct expr, list);
4437
4438
3/6
✓ Branch 0 (36→37) taken 68 times.
✗ Branch 1 (36→41) not taken.
✗ Branch 2 (37→38) not taken.
✓ Branch 3 (37→39) taken 68 times.
✗ Branch 4 (40→41) not taken.
✓ Branch 5 (40→43) taken 68 times.
68 if (one == two || !list_is_last(&two->list, &expr_concat(data)->expressions)) {
4439 ✗ err = __stmt_evaluate_arg(ctx, stmt, dtype, dtype->size,
4440 BYTEORDER_BIG_ENDIAN,
4441 &stmt->nat.addr);
4442 ✗ goto out;
4443 }
4444
4445 68 dtype2 = get_addr_dtype(stmt->nat.family);
4446 68 tmp = one;
4447 68 err = __stmt_evaluate_arg(ctx, stmt, dtype2, dtype2->size,
4448 BYTEORDER_BIG_ENDIAN,
4449 &tmp);
4450
1/2
✗ Branch 0 (45→46) not taken.
✓ Branch 1 (45→47) taken 68 times.
68 if (err < 0)
4451 ✗ goto out;
4452
1/2
✗ Branch 0 (47→48) not taken.
✓ Branch 1 (47→50) taken 68 times.
68 if (tmp != one)
4453 ✗ BUG("Internal error: Unexpected alteration of l3 expression");
4454
4455 68 tmp = two;
4456 68 err = nat_evaluate_transport(ctx, stmt, &tmp);
4457
2/2
✓ Branch 0 (51→52) taken 1 times.
✓ Branch 1 (51→53) taken 67 times.
68 if (err < 0)
4458 1 goto out;
4459
1/2
✗ Branch 0 (53→54) not taken.
✓ Branch 1 (53→56) taken 67 times.
67 if (tmp != two)
4460 ✗ BUG("Internal error: Unexpected alteration of l4 expression");
4461
4462 67 out:
4463 103 datatype_free(dtype);
4464 103 return err;
4465 }
4466
4467 409 static bool nat_concat_map(struct eval_ctx *ctx, struct stmt *stmt)
4468 {
4469 struct expr *i;
4470
4471
2/2
✓ Branch 0 (2→3) taken 167 times.
✓ Branch 1 (2→4) taken 242 times.
409 if (stmt->nat.addr->etype != EXPR_MAP)
4472 167 return false;
4473
4474
3/3
✓ Branch 0 (4→5) taken 110 times.
✓ Branch 1 (4→16) taken 129 times.
✓ Branch 2 (4→27) taken 3 times.
242 switch (stmt->nat.addr->mappings->etype) {
4475 110 case EXPR_SET:
4476
4/6
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 110 times.
✗ Branch 2 (12→13) not taken.
✓ Branch 3 (12→14) taken 240 times.
✓ Branch 4 (14→8) taken 163 times.
✓ Branch 5 (14→15) taken 77 times.
240 list_for_each_entry(i, &expr_set(stmt->nat.addr->mappings)->expressions, list) {
4477
2/2
✓ Branch 0 (8→9) taken 162 times.
✓ Branch 1 (8→11) taken 1 times.
163 if (i->etype == EXPR_MAPPING &&
4478
2/2
✓ Branch 0 (9→10) taken 33 times.
✓ Branch 1 (9→11) taken 129 times.
162 i->right->etype == EXPR_CONCAT) {
4479 33 stmt->nat.type_flags |= STMT_NAT_F_CONCAT;
4480 33 return true;
4481 }
4482 }
4483 77 break;
4484 129 case EXPR_SYMBOL:
4485 /* expr_evaluate_map() see EXPR_SET_REF after this evaluation. */
4486
2/2
✓ Branch 0 (17→18) taken 3 times.
✓ Branch 1 (17→19) taken 126 times.
129 if (expr_evaluate(ctx, &stmt->nat.addr->mappings))
4487 3 return false;
4488
4489
2/2
✓ Branch 0 (20→21) taken 1 times.
✓ Branch 1 (20→23) taken 125 times.
126 if (!set_is_datamap(stmt->nat.addr->mappings->set->flags)) {
4490 1 expr_error(ctx->msgs, stmt->nat.addr->mappings,
4491 "Expression is not a map");
4492 1 return false;
4493 }
4494
4495
2/2
✓ Branch 0 (23→24) taken 57 times.
✓ Branch 1 (23→25) taken 68 times.
125 if (stmt->nat.addr->mappings->set->data->etype == EXPR_CONCAT ||
4496
2/2
✓ Branch 0 (24→25) taken 6 times.
✓ Branch 1 (24→26) taken 51 times.
57 stmt->nat.addr->mappings->set->data->dtype->subtypes) {
4497 74 stmt->nat.type_flags |= STMT_NAT_F_CONCAT;
4498 74 return true;
4499 }
4500 51 break;
4501 3 default:
4502 3 break;
4503 }
4504
4505 131 return false;
4506 }
4507
4508 694 static int stmt_evaluate_nat(struct eval_ctx *ctx, struct stmt *stmt)
4509 {
4510 int err;
4511
4512 694 err = nat_evaluate_family(ctx, stmt);
4513
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 694 times.
694 if (err < 0)
4514 ✗ return err;
4515
4516
2/2
✓ Branch 0 (5→6) taken 409 times.
✓ Branch 1 (5→19) taken 285 times.
694 if (stmt->nat.addr != NULL) {
4517 409 err = stmt_evaluate_l3proto(ctx, stmt, stmt->nat.family);
4518
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 409 times.
409 if (err < 0)
4519 ✗ return err;
4520
4521
2/2
✓ Branch 0 (10→11) taken 302 times.
✓ Branch 1 (10→12) taken 107 times.
409 if (nat_concat_map(ctx, stmt) ||
4522
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→16) taken 302 times.
302 stmt->nat.type_flags & STMT_NAT_F_CONCAT) {
4523
4524 107 err = stmt_evaluate_nat_map(ctx, stmt);
4525
2/2
✓ Branch 0 (13→14) taken 9 times.
✓ Branch 1 (13→15) taken 98 times.
107 if (err < 0)
4526 9 return err;
4527
4528 98 stmt->flags |= STMT_F_TERMINAL;
4529 98 return 0;
4530 }
4531
4532 302 err = stmt_evaluate_addr(ctx, stmt, &stmt->nat.family,
4533 &stmt->nat.addr);
4534
2/2
✓ Branch 0 (17→18) taken 18 times.
✓ Branch 1 (17→19) taken 284 times.
302 if (err < 0)
4535 18 return err;
4536 }
4537
4538
2/2
✓ Branch 0 (19→20) taken 165 times.
✓ Branch 1 (19→24) taken 404 times.
569 if (stmt->nat.proto != NULL) {
4539 165 err = nat_evaluate_transport(ctx, stmt, &stmt->nat.proto);
4540
2/2
✓ Branch 0 (21→22) taken 7 times.
✓ Branch 1 (21→23) taken 158 times.
165 if (err < 0)
4541 7 return err;
4542
4543 158 stmt->nat.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
4544 }
4545
4546 562 stmt->flags |= STMT_F_TERMINAL;
4547 562 return 0;
4548 }
4549
4550 70 static int stmt_evaluate_tproxy(struct eval_ctx *ctx, struct stmt *stmt)
4551 {
4552 70 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4553 int err;
4554
4555
2/3
✓ Branch 0 (3→4) taken 42 times.
✓ Branch 1 (3→7) taken 28 times.
✗ Branch 2 (3→8) not taken.
70 switch (pctx->family) {
4556 42 case NFPROTO_IPV4:
4557 case NFPROTO_IPV6: /* fallthrough */
4558
2/2
✓ Branch 0 (4→5) taken 30 times.
✓ Branch 1 (4→6) taken 12 times.
42 if (stmt->tproxy.family == NFPROTO_UNSPEC)
4559 30 stmt->tproxy.family = pctx->family;
4560 42 break;
4561 28 case NFPROTO_INET:
4562 28 break;
4563 ✗ default:
4564 ✗ return stmt_error(ctx, stmt,
4565 "tproxy is only supported for IPv4/IPv6/INET");
4566 }
4567
4568
2/2
✓ Branch 0 (9→10) taken 6 times.
✓ Branch 1 (9→12) taken 64 times.
70 if (pctx->protocol[PROTO_BASE_TRANSPORT_HDR].desc == NULL)
4569 6 return stmt_error(ctx, stmt, "Transparent proxy support requires"
4570 " transport protocol match");
4571
4572
3/4
✓ Branch 0 (12→13) taken 26 times.
✓ Branch 1 (12→16) taken 38 times.
✗ Branch 2 (13→14) not taken.
✓ Branch 3 (13→16) taken 26 times.
64 if (!stmt->tproxy.addr && !stmt->tproxy.port)
4573 ✗ return stmt_error(ctx, stmt, "Either address or port must be specified!");
4574
4575 64 err = stmt_evaluate_l3proto(ctx, stmt, stmt->tproxy.family);
4576
2/2
✓ Branch 0 (17→18) taken 2 times.
✓ Branch 1 (17→19) taken 62 times.
64 if (err < 0)
4577 2 return err;
4578
4579
2/2
✓ Branch 0 (19→20) taken 36 times.
✓ Branch 1 (19→26) taken 26 times.
62 if (stmt->tproxy.addr != NULL) {
4580 36 err = stmt_evaluate_addr(ctx, stmt, &stmt->tproxy.family,
4581 &stmt->tproxy.addr);
4582
2/2
✓ Branch 0 (21→22) taken 4 times.
✓ Branch 1 (21→23) taken 32 times.
36 if (err < 0)
4583 4 return err;
4584
4585
2/2
✓ Branch 0 (23→24) taken 1 times.
✓ Branch 1 (23→26) taken 31 times.
32 if (stmt->tproxy.addr->etype == EXPR_RANGE)
4586 1 return stmt_error(ctx, stmt, "Address ranges are not supported for tproxy.");
4587 }
4588
4589
2/2
✓ Branch 0 (26→27) taken 43 times.
✓ Branch 1 (26→33) taken 14 times.
57 if (stmt->tproxy.port != NULL) {
4590 43 err = nat_evaluate_transport(ctx, stmt, &stmt->tproxy.port);
4591
1/2
✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→30) taken 43 times.
43 if (err < 0)
4592 ✗ return err;
4593
4594
2/2
✓ Branch 0 (30→31) taken 1 times.
✓ Branch 1 (30→33) taken 42 times.
43 if (stmt->tproxy.port->etype == EXPR_RANGE)
4595 1 return stmt_error(ctx, stmt, "Port ranges are not supported for tproxy.");
4596 }
4597
4598 56 return 0;
4599 }
4600
4601 41 static int stmt_evaluate_synproxy(struct eval_ctx *ctx, struct stmt *stmt)
4602 {
4603
2/2
✓ Branch 0 (2→3) taken 35 times.
✓ Branch 1 (2→6) taken 6 times.
41 if (stmt->synproxy.flags != 0 &&
4604
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 35 times.
35 !(stmt->synproxy.flags & (NF_SYNPROXY_OPT_MSS |
4605 NF_SYNPROXY_OPT_WSCALE |
4606 NF_SYNPROXY_OPT_TIMESTAMP |
4607 NF_SYNPROXY_OPT_SACK_PERM)))
4608 ✗ return stmt_error(ctx, stmt, "This flags are not supported for SYNPROXY");
4609
4610 41 return 0;
4611 }
4612
4613 static int rule_evaluate(struct eval_ctx *ctx, struct rule *rule,
4614 enum cmd_ops op);
4615
4616 63 static int stmt_evaluate_chain(struct eval_ctx *ctx, struct stmt *stmt)
4617 {
4618 63 struct chain *chain = chain_get(stmt->chain.chain);
4619 struct cmd *cmd;
4620
4621 63 chain->flags |= CHAIN_F_BINDING;
4622
4623
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 63 times.
63 if (ctx->table != NULL) {
4624 ✗ list_add_tail(&chain->list, &ctx->table->chains);
4625 } else {
4626 struct rule *rule, *next;
4627 struct handle h;
4628
4629 63 memset(&h, 0, sizeof(h));
4630 63 handle_merge(&h, &chain->handle);
4631 63 h.family = ctx->rule->handle.family;
4632 63 free_const(h.table.name);
4633 63 h.table.name = xstrdup(ctx->rule->handle.table.name);
4634 63 h.chain.location = stmt->location;
4635 63 h.chain_id = chain->handle.chain_id;
4636
4637 63 cmd = cmd_alloc(CMD_ADD, CMD_OBJ_CHAIN, &h, &stmt->location,
4638 chain);
4639 63 cmd->location = stmt->location;
4640 63 list_add_tail(&cmd->list, &ctx->cmd->list);
4641 63 h.chain_id = chain->handle.chain_id;
4642
4643
2/2
✓ Branch 0 (20→10) taken 113 times.
✓ Branch 1 (20→21) taken 63 times.
176 list_for_each_entry_safe(rule, next, &chain->rules, list) {
4644 113 struct eval_ctx rule_ctx = {
4645 113 .nft = ctx->nft,
4646 113 .msgs = ctx->msgs,
4647 113 .cmd = ctx->cmd,
4648 };
4649 113 struct handle h2 = {};
4650
4651 113 handle_merge(&rule->handle, &ctx->rule->handle);
4652 113 free_const(rule->handle.table.name);
4653 113 rule->handle.table.name = xstrdup(ctx->rule->handle.table.name);
4654 113 free_const(rule->handle.chain.name);
4655 113 rule->handle.chain.name = NULL;
4656 113 rule->handle.chain_id = chain->handle.chain_id;
4657
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 113 times.
113 if (rule_evaluate(&rule_ctx, rule, CMD_INVALID) < 0)
4658 ✗ return -1;
4659
4660 113 handle_merge(&h2, &rule->handle);
4661 113 cmd = cmd_alloc(CMD_ADD, CMD_OBJ_RULE, &h2,
4662 113 &rule->location, rule);
4663 113 list_add_tail(&cmd->list, &ctx->cmd->list);
4664 113 list_del(&rule->list);
4665 }
4666 }
4667
4668 63 return 0;
4669 }
4670
4671 26 static int stmt_evaluate_optstrip(struct eval_ctx *ctx, struct stmt *stmt)
4672 {
4673 26 return expr_evaluate(ctx, &stmt->optstrip.expr);
4674 }
4675
4676 22 static int stmt_evaluate_dup(struct eval_ctx *ctx, struct stmt *stmt)
4677 {
4678 22 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4679 int err;
4680
4681
2/3
✓ Branch 0 (3→4) taken 13 times.
✓ Branch 1 (3→18) taken 9 times.
✗ Branch 2 (3→28) not taken.
22 switch (pctx->family) {
4682 13 case NFPROTO_IPV4:
4683 case NFPROTO_IPV6:
4684
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 13 times.
13 if (stmt->dup.to == NULL)
4685 ✗ return stmt_error(ctx, stmt,
4686 "missing destination address");
4687 13 err = evaluate_addr(ctx, stmt, &stmt->dup.to);
4688
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 13 times.
13 if (err < 0)
4689 ✗ return err;
4690
4691
2/2
✓ Branch 0 (10→11) taken 9 times.
✓ Branch 1 (10→17) taken 4 times.
13 if (stmt->dup.dev != NULL) {
4692 9 err = stmt_evaluate_arg(ctx, stmt, &ifindex_type,
4693 sizeof(uint32_t) * BITS_PER_BYTE,
4694 BYTEORDER_HOST_ENDIAN,
4695 &stmt->dup.dev);
4696
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 9 times.
9 if (err < 0)
4697 ✗ return err;
4698
4699
2/2
✓ Branch 0 (14→15) taken 1 times.
✓ Branch 1 (14→17) taken 8 times.
9 if (stmt->dup.dev->etype == EXPR_RANGE)
4700 1 return stmt_error_range(ctx, stmt, stmt->dup.dev);
4701 }
4702 12 break;
4703 9 case NFPROTO_NETDEV:
4704
1/2
✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→21) taken 9 times.
9 if (stmt->dup.to == NULL)
4705 ✗ return stmt_error(ctx, stmt,
4706 "missing destination interface");
4707
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→24) taken 9 times.
9 if (stmt->dup.dev != NULL)
4708 ✗ return stmt_error(ctx, stmt, "cannot specify device");
4709
4710 9 err = stmt_evaluate_arg(ctx, stmt, &ifindex_type,
4711 sizeof(uint32_t) * BITS_PER_BYTE,
4712 BYTEORDER_HOST_ENDIAN, &stmt->dup.to);
4713
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 9 times.
9 if (err < 0)
4714 ✗ return err;
4715 9 break;
4716 ✗ default:
4717 ✗ return stmt_error(ctx, stmt, "unsupported family");
4718 }
4719
4720
2/2
✓ Branch 0 (29→30) taken 1 times.
✓ Branch 1 (29→32) taken 20 times.
21 if (stmt->dup.to->etype == EXPR_RANGE)
4721 1 return stmt_error_range(ctx, stmt, stmt->dup.to);
4722
4723 20 return 0;
4724 }
4725
4726 13 static int stmt_evaluate_fwd(struct eval_ctx *ctx, struct stmt *stmt)
4727 {
4728 13 struct proto_ctx *pctx = eval_proto_ctx(ctx);
4729 const struct datatype *dtype;
4730 int err, len;
4731
4732
1/2
✓ Branch 0 (3→4) taken 13 times.
✗ Branch 1 (3→26) not taken.
13 switch (pctx->family) {
4733 13 case NFPROTO_NETDEV:
4734
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 13 times.
13 if (stmt->fwd.dev == NULL)
4735 ✗ return stmt_error(ctx, stmt,
4736 "missing destination interface");
4737
4738 13 err = stmt_evaluate_arg(ctx, stmt, &ifindex_type,
4739 sizeof(uint32_t) * BITS_PER_BYTE,
4740 BYTEORDER_HOST_ENDIAN, &stmt->fwd.dev);
4741
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 13 times.
13 if (err < 0)
4742 ✗ return err;
4743
4744
2/2
✓ Branch 0 (10→11) taken 1 times.
✓ Branch 1 (10→13) taken 12 times.
13 if (stmt->fwd.dev->etype == EXPR_RANGE)
4745 1 return stmt_error_range(ctx, stmt, stmt->fwd.dev);
4746
4747
2/2
✓ Branch 0 (13→14) taken 4 times.
✓ Branch 1 (13→25) taken 8 times.
12 if (stmt->fwd.addr != NULL) {
4748
1/3
✓ Branch 0 (14→15) taken 4 times.
✗ Branch 1 (14→16) not taken.
✗ Branch 2 (14→17) not taken.
4 switch (stmt->fwd.family) {
4749 4 case NFPROTO_IPV4:
4750 4 dtype = &ipaddr_type;
4751 4 len = 4 * BITS_PER_BYTE;
4752 4 break;
4753 ✗ case NFPROTO_IPV6:
4754 ✗ dtype = &ip6addr_type;
4755 ✗ len = 16 * BITS_PER_BYTE;
4756 ✗ break;
4757 ✗ default:
4758 ✗ return stmt_error(ctx, stmt, "missing family");
4759 }
4760 4 err = stmt_evaluate_arg(ctx, stmt, dtype, len,
4761 BYTEORDER_BIG_ENDIAN,
4762 &stmt->fwd.addr);
4763
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 4 times.
4 if (err < 0)
4764 ✗ return err;
4765
4766
1/2
✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→25) taken 4 times.
4 if (stmt->fwd.addr->etype == EXPR_RANGE)
4767 ✗ return stmt_error_range(ctx, stmt, stmt->fwd.addr);
4768 }
4769 12 break;
4770 ✗ default:
4771 ✗ return stmt_error(ctx, stmt, "unsupported family");
4772 }
4773 12 stmt->flags |= STMT_F_TERMINAL;
4774 12 return 0;
4775 }
4776
4777 222 static int stmt_evaluate_queue(struct eval_ctx *ctx, struct stmt *stmt)
4778 {
4779
2/2
✓ Branch 0 (2→3) taken 214 times.
✓ Branch 1 (2→13) taken 8 times.
222 if (stmt->queue.queue != NULL) {
4780
2/2
✓ Branch 0 (4→5) taken 4 times.
✓ Branch 1 (4→6) taken 210 times.
214 if (stmt_evaluate_arg(ctx, stmt, &integer_type, 16,
4781 BYTEORDER_HOST_ENDIAN,
4782 &stmt->queue.queue) < 0)
4783 4 return -1;
4784
4785
2/2
✓ Branch 0 (6→7) taken 49 times.
✓ Branch 1 (6→10) taken 161 times.
210 if ((stmt->queue.flags & NFT_QUEUE_FLAG_CPU_FANOUT) &&
4786
2/2
✓ Branch 0 (7→8) taken 8 times.
✓ Branch 1 (7→10) taken 41 times.
49 stmt->queue.queue->etype != EXPR_RANGE)
4787 8 return expr_error(ctx->msgs, stmt->queue.queue,
4788 "fanout requires a range to be "
4789 "specified");
4790
4791
2/2
✓ Branch 0 (10→11) taken 4 times.
✓ Branch 1 (10→13) taken 198 times.
202 if (ctx->ectx.maxval > USHRT_MAX)
4792 4 return expr_error(ctx->msgs, stmt->queue.queue,
4793 "queue expression max value exceeds %u", USHRT_MAX);
4794 }
4795 206 stmt->flags |= STMT_F_TERMINAL;
4796 206 return 0;
4797 }
4798
4799 50 static int stmt_evaluate_log_prefix(struct eval_ctx *ctx, struct stmt *stmt)
4800 {
4801 50 unsigned int len = strlen(stmt->log.prefix);
4802
4803
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 50 times.
50 if (len >= NF_LOG_PREFIXLEN)
4804 ✗ return stmt_error(ctx, stmt, "log prefix is too long");
4805
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 50 times.
50 else if (len == 0)
4806 ✗ return stmt_error(ctx, stmt, "log prefix must have a minimum length of 1 character");
4807
4808 50 return 0;
4809 }
4810
4811 296 static int stmt_evaluate_log(struct eval_ctx *ctx, struct stmt *stmt)
4812 {
4813 296 int ret = 0;
4814
4815
2/2
✓ Branch 0 (2→3) taken 80 times.
✓ Branch 1 (2→9) taken 216 times.
296 if (stmt->log.flags & (STMT_LOG_GROUP | STMT_LOG_SNAPLEN |
4816 STMT_LOG_QTHRESHOLD)) {
4817
2/2
✓ Branch 0 (3→4) taken 25 times.
✓ Branch 1 (3→6) taken 55 times.
80 if (stmt->log.flags & STMT_LOG_LEVEL)
4818 25 return stmt_error(ctx, stmt,
4819 "level and group are mutually exclusive");
4820
2/2
✓ Branch 0 (6→7) taken 5 times.
✓ Branch 1 (6→9) taken 50 times.
55 if (stmt->log.logflags)
4821 5 return stmt_error(ctx, stmt,
4822 "flags and group are mutually exclusive");
4823 }
4824
2/2
✓ Branch 0 (9→10) taken 20 times.
✓ Branch 1 (9→14) taken 246 times.
266 if (stmt->log.level == NFT_LOGLEVEL_AUDIT &&
4825
4/4
✓ Branch 0 (10→11) taken 15 times.
✓ Branch 1 (10→12) taken 5 times.
✓ Branch 2 (11→12) taken 5 times.
✓ Branch 3 (11→14) taken 10 times.
20 (stmt->log.flags & ~STMT_LOG_LEVEL || stmt->log.logflags))
4826 10 return stmt_error(ctx, stmt,
4827 "log level audit doesn't support any further options");
4828
4829
2/2
✓ Branch 0 (14→15) taken 50 times.
✓ Branch 1 (14→16) taken 206 times.
256 if (stmt->log.prefix)
4830 50 ret = stmt_evaluate_log_prefix(ctx, stmt);
4831
4832 256 return ret;
4833 }
4834
4835 1167 static int stmt_evaluate_set(struct eval_ctx *ctx, struct stmt *stmt)
4836 {
4837 struct set *this_set;
4838 struct stmt *this;
4839
4840 1167 expr_set_context(&ctx->ectx, NULL, 0);
4841
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 1167 times.
1167 if (expr_evaluate(ctx, &stmt->set.set) < 0)
4842 ✗ return -1;
4843
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 1167 times.
1167 if (stmt->set.set->etype != EXPR_SET_REF)
4844 ✗ return expr_error(ctx->msgs, stmt->set.set,
4845 "Expression does not refer to a set");
4846
4847
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 1167 times.
1167 if (stmt_evaluate_key(ctx, stmt,
4848 1167 stmt->set.set->set->key->dtype,
4849 1167 stmt->set.set->set->key->len,
4850 1167 stmt->set.set->set->key->byteorder,
4851 1167 &stmt->set.key->key) < 0)
4852 ✗ return -1;
4853
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→16) taken 1167 times.
1167 if (expr_is_constant(stmt->set.key))
4854 ✗ return expr_error(ctx->msgs, stmt->set.key,
4855 "Key expression can not be constant");
4856
1/2
✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→19) taken 1167 times.
1167 if (stmt->set.key->comment != NULL)
4857 ✗ return expr_error(ctx->msgs, stmt->set.key,
4858 "Key expression comments are not supported");
4859
2/2
✓ Branch 0 (24→20) taken 42 times.
✓ Branch 1 (24→25) taken 1167 times.
1209 list_for_each_entry(this, &stmt->set.stmt_list, list) {
4860
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 42 times.
42 if (stmt_evaluate_stateful(ctx, this, "set") < 0)
4861 ✗ return -1;
4862 }
4863
4864 1167 this_set = stmt->set.set->set;
4865
4866 /* Make sure EVAL flag is set on set definition so that kernel
4867 * picks a set that allows updates from the packet path.
4868 *
4869 * Alternatively we could error out in case 'flags dynamic' was
4870 * not given, but we can repair this here.
4871 */
4872 1167 this_set->flags |= NFT_SET_EVAL;
4873 1167 return 0;
4874 }
4875
4876 73 static int stmt_evaluate_map(struct eval_ctx *ctx, struct stmt *stmt)
4877 {
4878 struct stmt *this;
4879
4880 73 expr_set_context(&ctx->ectx, NULL, 0);
4881
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 73 times.
73 if (expr_evaluate(ctx, &stmt->map.set) < 0)
4882 ✗ return -1;
4883
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 73 times.
73 if (stmt->map.set->etype != EXPR_SET_REF)
4884 ✗ return expr_error(ctx->msgs, stmt->map.set,
4885 "Expression does not refer to a set");
4886
4887
2/2
✓ Branch 0 (10→11) taken 1 times.
✓ Branch 1 (10→13) taken 72 times.
73 if (!set_is_map(stmt->map.set->set->flags))
4888 1 return expr_error(ctx->msgs, stmt->map.set,
4889 "%s is not a map", stmt->map.set->set->handle.set.name);
4890
4891
1/2
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 72 times.
72 if (stmt_evaluate_key(ctx, stmt,
4892 72 stmt->map.set->set->key->dtype,
4893 72 stmt->map.set->set->key->len,
4894 72 stmt->map.set->set->key->byteorder,
4895 72 &stmt->map.key->key) < 0)
4896 ✗ return -1;
4897
1/2
✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→20) taken 72 times.
72 if (expr_is_constant(stmt->map.key))
4898 ✗ return expr_error(ctx->msgs, stmt->map.key,
4899 "Key expression can not be constant");
4900
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→23) taken 72 times.
72 if (stmt->map.key->comment != NULL)
4901 ✗ return expr_error(ctx->msgs, stmt->map.key,
4902 "Key expression comments are not supported");
4903
4904
1/2
✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 72 times.
72 if (stmt_evaluate_arg(ctx, stmt,
4905 72 stmt->map.set->set->data->dtype,
4906 72 stmt->map.set->set->data->len,
4907 72 stmt->map.set->set->data->byteorder,
4908 72 &stmt->map.data->key) < 0)
4909 ✗ return -1;
4910
1/2
✗ Branch 0 (27→28) not taken.
✓ Branch 1 (27→30) taken 72 times.
72 if (expr_is_constant(stmt->map.data))
4911 ✗ return expr_error(ctx->msgs, stmt->map.data,
4912 "Data expression can not be constant");
4913
1/2
✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→33) taken 72 times.
72 if (stmt->map.data->comment != NULL)
4914 ✗ return expr_error(ctx->msgs, stmt->map.data,
4915 "Data expression comments are not supported");
4916
1/2
✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→36) taken 72 times.
72 if (stmt->map.data->timeout > 0)
4917 ✗ return expr_error(ctx->msgs, stmt->map.data,
4918 "Data expression timeouts are not supported");
4919
4920
1/2
✗ Branch 0 (41→37) not taken.
✓ Branch 1 (41→42) taken 72 times.
72 list_for_each_entry(this, &stmt->map.stmt_list, list) {
4921 ✗ if (stmt_evaluate_stateful(ctx, this, "map") < 0)
4922 ✗ return -1;
4923 }
4924
4925 72 return 0;
4926 }
4927
4928 101 static int stmt_evaluate_objref_map(struct eval_ctx *ctx, struct stmt *stmt)
4929 {
4930 101 struct expr *map = stmt->objref.expr;
4931 101 uint32_t set_flags = NFT_SET_OBJECT;
4932 struct expr *mappings;
4933 struct expr *key;
4934
4935 101 expr_set_context(&ctx->ectx, NULL, 0);
4936
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 101 times.
101 if (expr_evaluate(ctx, &map->map) < 0)
4937 ✗ return -1;
4938
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 101 times.
101 if (expr_is_constant(map->map))
4939 ✗ return expr_error(ctx->msgs, map->map,
4940 "Map expression can not be constant");
4941
4942 101 mappings = map->mappings;
4943
4944
3/4
✓ Branch 0 (10→11) taken 67 times.
✓ Branch 1 (10→14) taken 4 times.
✓ Branch 2 (10→35) taken 30 times.
✗ Branch 3 (10→46) not taken.
101 switch (map->mappings->etype) {
4945 67 case EXPR_SET:
4946
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 67 times.
67 set_flags |= expr_set(mappings)->set_flags;
4947 /* fallthrough */
4948 71 case EXPR_VARIABLE:
4949 71 key = constant_expr_alloc(&stmt->location,
4950 ctx->ectx.dtype,
4951 ctx->ectx.byteorder,
4952 ctx->ectx.len, NULL);
4953
4954 71 mappings = implicit_set_declaration(ctx, "__objmap%d",
4955 key, NULL, mappings,
4956 set_flags | NFT_SET_ANONYMOUS);
4957
2/2
✓ Branch 0 (16→17) taken 1 times.
✓ Branch 1 (16→18) taken 70 times.
71 if (!mappings)
4958 1 return -1;
4959 70 mappings->set->objtype = stmt->objref.type;
4960
4961 70 map->mappings = mappings;
4962
4963 70 ctx->set = mappings->set;
4964
2/2
✓ Branch 0 (19→20) taken 1 times.
✓ Branch 1 (19→21) taken 69 times.
70 if (expr_evaluate(ctx, &map->mappings->set->init) < 0)
4965 1 return -1;
4966
4967
2/2
✓ Branch 0 (21→22) taken 1 times.
✓ Branch 1 (21→24) taken 68 times.
69 if (map->mappings->set->init->etype != EXPR_SET) {
4968 1 return expr_error(ctx->msgs, map->mappings->set->init,
4969 "Expression is not a map");
4970 }
4971
4972
3/4
✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 68 times.
✓ Branch 2 (27→28) taken 21 times.
✓ Branch 3 (27→34) taken 47 times.
68 if (set_is_interval(expr_set(map->mappings->set->init)->set_flags) &&
4973
4/6
✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→30) taken 21 times.
✓ Branch 2 (30→31) taken 16 times.
✓ Branch 3 (30→34) taken 5 times.
✗ Branch 4 (32→33) not taken.
✓ Branch 5 (32→34) taken 16 times.
37 !(expr_set(map->mappings->set->init)->set_flags & NFT_SET_CONCAT) &&
4974 16 interval_set_eval(ctx, ctx->set, map->mappings->set->init) < 0)
4975 ✗ return -1;
4976
4977 68 ctx->set = NULL;
4978
4979 68 map_set_concat_info(map);
4980 /* fall through */
4981 98 case EXPR_SYMBOL:
4982
1/2
✗ Branch 0 (36→37) not taken.
✓ Branch 1 (36→38) taken 98 times.
98 if (expr_evaluate(ctx, &map->mappings) < 0)
4983 ✗ return -1;
4984
1/2
✗ Branch 0 (38→39) not taken.
✓ Branch 1 (38→41) taken 98 times.
98 if (map->mappings->etype != EXPR_SET_REF)
4985 ✗ return expr_error(ctx->msgs, map->mappings,
4986 "Expression is not a map");
4987
2/2
✓ Branch 0 (42→43) taken 1 times.
✓ Branch 1 (42→45) taken 97 times.
98 if (!set_is_objmap(map->mappings->set->flags))
4988 1 return expr_error(ctx->msgs, map->mappings,
4989 "Expression is not a map with objects");
4990 97 break;
4991 ✗ default:
4992 ✗ return expr_binary_error(ctx->msgs, map->mappings, map->map,
4993 "invalid mapping expression %s",
4994 ✗ expr_name(map->mappings));
4995 }
4996
4997
2/2
✓ Branch 0 (49→50) taken 1 times.
✓ Branch 1 (49→52) taken 96 times.
97 if (!datatype_compatible(map->mappings->set->key->dtype, map->map->dtype))
4998 1 return expr_binary_error(ctx->msgs, map->mappings, map->map,
4999 "datatype mismatch, map expects %s, "
5000 "mapping expression has type %s",
5001 1 map->mappings->set->key->dtype->desc,
5002 1 map->map->dtype->desc);
5003
5004 96 datatype_set(map, map->mappings->set->data->dtype);
5005 96 map->flags |= EXPR_F_CONSTANT;
5006
5007 /* Data for range lookups needs to be in big endian order */
5008
3/4
✓ Branch 0 (53→54) taken 33 times.
✓ Branch 1 (53→57) taken 63 times.
✗ Branch 2 (55→56) not taken.
✓ Branch 3 (55→57) taken 33 times.
129 if (map->mappings->set->flags & NFT_SET_INTERVAL &&
5009 33 byteorder_conversion(ctx, &map->map, BYTEORDER_BIG_ENDIAN) < 0)
5010 ✗ return -1;
5011
5012 96 return 0;
5013 }
5014
5015 322 static int stmt_evaluate_objref(struct eval_ctx *ctx, struct stmt *stmt)
5016 {
5017 /* We need specific map evaluation for stateful objects. */
5018
2/2
✓ Branch 0 (2→3) taken 101 times.
✓ Branch 1 (2→5) taken 221 times.
322 if (stmt->objref.expr->etype == EXPR_MAP)
5019 101 return stmt_evaluate_objref_map(ctx, stmt);
5020
5021
2/2
✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→8) taken 220 times.
221 if (stmt_evaluate_arg(ctx, stmt,
5022 &string_type, NFT_OBJ_MAXNAMELEN * BITS_PER_BYTE,
5023 BYTEORDER_HOST_ENDIAN, &stmt->objref.expr) < 0)
5024 1 return -1;
5025
5026
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 220 times.
220 if (!expr_is_constant(stmt->objref.expr))
5027 ✗ return expr_error(ctx->msgs, stmt->objref.expr,
5028 "Counter expression must be constant");
5029
5030 220 return 0;
5031 }
5032
5033 132060 int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)
5034 {
5035
2/2
✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→9) taken 132057 times.
132060 if (ctx->nft->debug_mask & NFT_DEBUG_EVALUATION) {
5036 struct error_record *erec;
5037 3 erec = erec_create(EREC_INFORMATIONAL, &stmt->location,
5038 "Evaluate %s", stmt_name(stmt));
5039 3 erec_print(&ctx->nft->output, erec, ctx->nft->debug_mask);
5040 3 stmt_print(stmt, &ctx->nft->output);
5041 3 nft_print(&ctx->nft->output, "\n\n");
5042 3 erec_destroy(erec);
5043 }
5044
5045 132060 ctx->stmt_len = 0;
5046
5047
21/22
✓ Branch 0 (9→10) taken 3058 times.
✓ Branch 1 (9→11) taken 109581 times.
✓ Branch 2 (9→12) taken 14809 times.
✓ Branch 3 (9→13) taken 463 times.
✓ Branch 4 (9→14) taken 13 times.
✓ Branch 5 (9→15) taken 7 times.
✓ Branch 6 (9→16) taken 524 times.
✓ Branch 7 (9→17) taken 278 times.
✓ Branch 8 (9→18) taken 296 times.
✓ Branch 9 (9→19) taken 318 times.
✓ Branch 10 (9→20) taken 694 times.
✓ Branch 11 (9→21) taken 70 times.
✓ Branch 12 (9→22) taken 222 times.
✓ Branch 13 (9→23) taken 22 times.
✓ Branch 14 (9→24) taken 13 times.
✓ Branch 15 (9→25) taken 1167 times.
✓ Branch 16 (9→26) taken 322 times.
✓ Branch 17 (9→27) taken 73 times.
✓ Branch 18 (9→28) taken 41 times.
✓ Branch 19 (9→29) taken 63 times.
✓ Branch 20 (9→30) taken 26 times.
✗ Branch 21 (9→31) not taken.
132060 switch (stmt->type) {
5048 3058 case STMT_CONNLIMIT:
5049 case STMT_COUNTER:
5050 case STMT_LAST:
5051 case STMT_LIMIT:
5052 case STMT_QUOTA:
5053 case STMT_NOTRACK:
5054 case STMT_FLOW_OFFLOAD:
5055 3058 return 0;
5056 109581 case STMT_EXPRESSION:
5057 109581 return stmt_evaluate_expr(ctx, stmt);
5058 14809 case STMT_VERDICT:
5059 14809 return stmt_evaluate_verdict(ctx, stmt);
5060 463 case STMT_PAYLOAD:
5061 463 return stmt_evaluate_payload(ctx, stmt);
5062 13 case STMT_EXTHDR:
5063 13 return stmt_evaluate_exthdr(ctx, stmt);
5064 7 case STMT_METER:
5065 7 return stmt_evaluate_meter(ctx, stmt);
5066 524 case STMT_META:
5067 524 return stmt_evaluate_meta(ctx, stmt);
5068 278 case STMT_CT:
5069 278 return stmt_evaluate_ct(ctx, stmt);
5070 296 case STMT_LOG:
5071 296 return stmt_evaluate_log(ctx, stmt);
5072 318 case STMT_REJECT:
5073 318 return stmt_evaluate_reject(ctx, stmt);
5074 694 case STMT_NAT:
5075 694 return stmt_evaluate_nat(ctx, stmt);
5076 70 case STMT_TPROXY:
5077 70 return stmt_evaluate_tproxy(ctx, stmt);
5078 222 case STMT_QUEUE:
5079 222 return stmt_evaluate_queue(ctx, stmt);
5080 22 case STMT_DUP:
5081 22 return stmt_evaluate_dup(ctx, stmt);
5082 13 case STMT_FWD:
5083 13 return stmt_evaluate_fwd(ctx, stmt);
5084 1167 case STMT_SET:
5085 1167 return stmt_evaluate_set(ctx, stmt);
5086 322 case STMT_OBJREF:
5087 322 return stmt_evaluate_objref(ctx, stmt);
5088 73 case STMT_MAP:
5089 73 return stmt_evaluate_map(ctx, stmt);
5090 41 case STMT_SYNPROXY:
5091 41 return stmt_evaluate_synproxy(ctx, stmt);
5092 63 case STMT_CHAIN:
5093 63 return stmt_evaluate_chain(ctx, stmt);
5094 26 case STMT_OPTSTRIP:
5095 26 return stmt_evaluate_optstrip(ctx, stmt);
5096 ✗ default:
5097 ✗ BUG("unknown statement type %d", stmt->type);
5098 }
5099 }
5100
5101 27305 static int setelem_evaluate(struct eval_ctx *ctx, struct cmd *cmd)
5102 {
5103 struct table *table;
5104 struct set *set;
5105
5106 27305 table = table_cache_find(&ctx->nft->cache.table_cache,
5107 27305 ctx->cmd->handle.table.name,
5108 27305 ctx->cmd->handle.family);
5109
2/2
✓ Branch 0 (3→4) taken 5 times.
✓ Branch 1 (3→6) taken 27300 times.
27305 if (table == NULL)
5110 5 return table_not_found(ctx);
5111
5112 27300 set = set_cache_find(table, ctx->cmd->handle.set.name);
5113
2/2
✓ Branch 0 (7→8) taken 8 times.
✓ Branch 1 (7→10) taken 27292 times.
27300 if (set == NULL)
5114 8 return set_not_found(ctx, &ctx->cmd->handle.set.location,
5115 8 ctx->cmd->handle.set.name);
5116
5117
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 27292 times.
27292 if (set->key == NULL)
5118 ✗ return -1;
5119
5120 27292 set->existing_set = set;
5121 27292 ctx->set = set;
5122 27292 expr_set_context(&ctx->ectx, set->key->dtype, set->key->len);
5123
2/2
✓ Branch 0 (14→15) taken 10 times.
✓ Branch 1 (14→16) taken 27282 times.
27292 if (expr_evaluate(ctx, &cmd->expr) < 0)
5124 10 return -1;
5125
5126 27282 cmd->elem.set = set_get(set);
5127
2/2
✓ Branch 0 (18→19) taken 15092 times.
✓ Branch 1 (18→28) taken 12190 times.
27282 if (set_is_interval(ctx->set->flags)) {
5128
4/4
✓ Branch 0 (19→20) taken 6454 times.
✓ Branch 1 (19→23) taken 8638 times.
✓ Branch 2 (21→22) taken 42 times.
✓ Branch 3 (21→23) taken 6412 times.
21546 if (!(set->flags & NFT_SET_CONCAT) &&
5129 6454 interval_set_eval(ctx, ctx->set, cmd->expr) < 0)
5130 42 return -1;
5131
5132
1/2
✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→25) taken 15050 times.
15050 assert(cmd->expr->etype == EXPR_SET);
5133
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 15050 times.
15050 expr_set(cmd->expr)->set_flags |= NFT_SET_INTERVAL;
5134 }
5135
5136 27240 ctx->set = NULL;
5137
5138 27240 return 0;
5139 }
5140
5141 5 static int set_key_data_error(struct eval_ctx *ctx, const struct set *set,
5142 const struct datatype *dtype,
5143 const char *name)
5144 {
5145 5 const char *hint = "";
5146
5147
1/2
✓ Branch 0 (2→3) taken 5 times.
✗ Branch 1 (2→4) not taken.
5 if (dtype->size == 0)
5148 5 hint = ". Try \"typeof expression\" instead of \"type datatype\".";
5149
5150 10 return set_error(ctx, set, "unqualified type %s "
5151 "specified in %s definition%s",
5152 5 dtype->name, name, hint);
5153 }
5154
5155 1171 static int set_expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
5156 {
5157 1171 unsigned int flags = EXPR_F_CONSTANT | EXPR_F_SINGLETON;
5158 1171 uint32_t ntype = 0, size = 0;
5159 struct expr *i, *next;
5160
5161
4/6
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1171 times.
✗ Branch 2 (31→32) not taken.
✓ Branch 3 (31→33) taken 3824 times.
✓ Branch 4 (33→5) taken 2654 times.
✓ Branch 5 (33→34) taken 1170 times.
3824 list_for_each_entry_safe(i, next, &expr_concat(*expr)->expressions, list) {
5162 unsigned dsize_bytes;
5163
5164
2/2
✓ Branch 0 (5→6) taken 4 times.
✓ Branch 1 (5→10) taken 2650 times.
2654 if (i->etype == EXPR_CT &&
5165
1/2
✓ Branch 0 (6→7) taken 4 times.
✗ Branch 1 (6→8) not taken.
4 (i->ct.key == NFT_CT_SRC ||
5166
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 4 times.
4 i->ct.key == NFT_CT_DST))
5167 ✗ return expr_error(ctx->msgs, i,
5168 "specify either ip or ip6 for address matching");
5169
5170
2/2
✓ Branch 0 (10→11) taken 1337 times.
✓ Branch 1 (10→14) taken 1317 times.
2654 if (i->etype == EXPR_PAYLOAD &&
5171
2/2
✓ Branch 0 (11→12) taken 557 times.
✓ Branch 1 (11→14) taken 780 times.
1337 i->dtype->type == TYPE_INTEGER) {
5172 struct datatype *dtype;
5173
5174 557 dtype = datatype_clone(i->dtype);
5175 557 dtype->size = i->len;
5176 557 dtype->byteorder = i->byteorder;
5177 557 __datatype_set(i, dtype);
5178 }
5179
5180
4/4
✓ Branch 0 (14→15) taken 17 times.
✓ Branch 1 (14→18) taken 2637 times.
✓ Branch 2 (15→16) taken 1 times.
✓ Branch 3 (15→18) taken 16 times.
2654 if (i->dtype->size == 0 && i->len == 0)
5181 1 return expr_binary_error(ctx->msgs, i, *expr,
5182 "can not use variable sized "
5183 "data types (%s) in concat "
5184 "expressions",
5185 1 i->dtype->name);
5186
5187 2653 flags &= i->flags;
5188
5189 2653 ntype = concat_subtype_add(ntype, i->dtype->type);
5190
5191 2653 dsize_bytes = div_round_up(i->len, BITS_PER_BYTE);
5192
5193
2/2
✓ Branch 0 (19→20) taken 2637 times.
✓ Branch 1 (19→22) taken 16 times.
2653 if (i->dtype->size)
5194
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 2637 times.
2637 assert(dsize_bytes == div_round_up(i->dtype->size, BITS_PER_BYTE));
5195
5196
2/4
✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 2653 times.
✗ Branch 2 (24→25) not taken.
✓ Branch 3 (24→26) taken 2653 times.
2653 expr_concat(*expr)->field_len[expr_concat(*expr)->field_count++] = dsize_bytes;
5197 2653 size += netlink_padded_len(i->len);
5198
5199
1/2
✗ Branch 0 (27→28) not taken.
✓ Branch 1 (27→30) taken 2653 times.
2653 if (size > NFT_MAX_EXPR_LEN_BITS)
5200 ✗ return expr_error(ctx->msgs, i, "Concatenation of size %u exceeds maximum size of %lu",
5201 size, NFT_MAX_EXPR_LEN_BITS);
5202 }
5203
5204 1170 (*expr)->flags |= flags;
5205 1170 __datatype_set(*expr, concat_type_alloc(ntype));
5206 1170 (*expr)->len = size;
5207
5208 1170 expr_set_context(&ctx->ectx, (*expr)->dtype, (*expr)->len);
5209 1170 ctx->ectx.key = *expr;
5210
5211 1170 return 0;
5212 }
5213
5214 1082 static int elems_evaluate(struct eval_ctx *ctx, struct set *set)
5215 {
5216 1082 ctx->set = set;
5217
1/2
✓ Branch 0 (2→3) taken 1082 times.
✗ Branch 1 (2→14) not taken.
1082 if (set->init != NULL) {
5218
2/2
✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→6) taken 1078 times.
1082 if (set->key == NULL)
5219 4 return set_error(ctx, set, "set definition does not specify key");
5220
5221 1078 __expr_set_context(&ctx->ectx, set->key->dtype,
5222 1078 set->key->byteorder, set->key->len, 0);
5223
2/2
✓ Branch 0 (8→9) taken 24 times.
✓ Branch 1 (8→10) taken 1054 times.
1078 if (expr_evaluate(ctx, &set->init) < 0) {
5224 24 set->errors = true;
5225 24 return -1;
5226 }
5227
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→14) taken 1054 times.
1054 if (set->init->etype != EXPR_SET)
5228 ✗ return expr_error(ctx->msgs, set->init, "Set %s: Unexpected initial type %s, missing { }?",
5229 set->handle.set.name, expr_name(set->init));
5230 }
5231
5232
2/2
✓ Branch 0 (15→16) taken 506 times.
✓ Branch 1 (15→20) taken 548 times.
1054 if (set_is_interval(ctx->set->flags) &&
5233
3/4
✓ Branch 0 (16→17) taken 240 times.
✓ Branch 1 (16→20) taken 266 times.
✗ Branch 2 (18→19) not taken.
✓ Branch 3 (18→20) taken 240 times.
746 !(ctx->set->flags & NFT_SET_CONCAT) &&
5234 240 interval_set_eval(ctx, ctx->set, set->init) < 0)
5235 ✗ return -1;
5236
5237 1054 ctx->set = NULL;
5238
5239 1054 return 0;
5240 }
5241
5242 12 static bool set_type_compatible(const struct set *set, const struct set *existing_set)
5243 {
5244
2/2
✓ Branch 0 (3→4) taken 5 times.
✓ Branch 1 (3→6) taken 7 times.
12 if (set_is_datamap(set->flags))
5245 5 return set_is_datamap(existing_set->flags);
5246
5247
2/2
✓ Branch 0 (7→8) taken 2 times.
✓ Branch 1 (7→10) taken 5 times.
7 if (set_is_objmap(set->flags))
5248 2 return set_is_objmap(existing_set->flags);
5249
5250
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 5 times.
5 assert(!set_is_map(set->flags));
5251 5 return !set_is_map(existing_set->flags);
5252 }
5253
5254 7093 static int set_evaluate(struct eval_ctx *ctx, struct set *set)
5255 {
5256 7093 struct set *existing_set = NULL;
5257 7093 unsigned int num_stmts = 0;
5258 struct table *table;
5259 struct stmt *stmt;
5260 const char *type;
5261
5262
2/2
✓ Branch 0 (3→4) taken 1725 times.
✓ Branch 1 (3→5) taken 5368 times.
7093 type = set_is_map(set->flags) ? "map" : "set";
5263
5264
2/2
✓ Branch 0 (6→7) taken 9 times.
✓ Branch 1 (6→9) taken 7084 times.
7093 if (set->key == NULL)
5265 9 return set_error(ctx, set, "%s definition does not specify key",
5266 type);
5267
5268
2/2
✓ Branch 0 (10→11) taken 2926 times.
✓ Branch 1 (10→22) taken 4158 times.
7084 if (!set_is_anonymous(set->flags)) {
5269 2926 table = table_cache_find(&ctx->nft->cache.table_cache,
5270 set->handle.table.name,
5271 set->handle.family);
5272
2/2
✓ Branch 0 (12→13) taken 1 times.
✓ Branch 1 (12→15) taken 2925 times.
2926 if (table == NULL)
5273 1 return table_not_found(ctx);
5274
5275 2925 existing_set = set_cache_find(table, set->handle.set.name);
5276
2/2
✓ Branch 0 (16→17) taken 17 times.
✓ Branch 1 (16→20) taken 2908 times.
2925 if (existing_set) {
5277
1/2
✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→22) taken 17 times.
17 if (existing_set->flags & NFT_SET_EVAL) {
5278 ✗ uint32_t existing_flags = existing_set->flags & ~NFT_SET_EVAL;
5279 ✗ uint32_t new_flags = set->flags & ~NFT_SET_EVAL;
5280
5281 ✗ if (existing_flags == new_flags)
5282 ✗ set->flags |= NFT_SET_EVAL;
5283 }
5284 } else {
5285 2908 set_cache_add(set_get(set), table);
5286 }
5287 }
5288
5289
3/4
✓ Branch 0 (22→23) taken 5958 times.
✓ Branch 1 (22→26) taken 1125 times.
✗ Branch 2 (23→24) not taken.
✓ Branch 3 (23→26) taken 5958 times.
7083 if (!(set->flags & NFT_SET_INTERVAL) && set->automerge)
5290 ✗ return set_error(ctx, set, "auto-merge only works with interval sets");
5291
5292
1/2
✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→29) taken 7083 times.
7083 if (set->key == NULL)
5293 ✗ return set_error(ctx, set, "%s definition does not specify key",
5294 type);
5295
5296
2/2
✓ Branch 0 (29→30) taken 1094 times.
✓ Branch 1 (29→37) taken 5989 times.
7083 if (set->key->len == 0) {
5297
3/4
✓ Branch 0 (30→31) taken 1091 times.
✓ Branch 1 (30→34) taken 3 times.
✗ Branch 2 (32→33) not taken.
✓ Branch 3 (32→34) taken 1091 times.
2185 if (set->key->etype == EXPR_CONCAT &&
5298 1091 set_expr_evaluate_concat(ctx, &set->key) < 0)
5299 ✗ return -1;
5300
5301
2/2
✓ Branch 0 (34→35) taken 3 times.
✓ Branch 1 (34→37) taken 1091 times.
1094 if (set->key->len == 0)
5302 3 return set_key_data_error(ctx, set,
5303 3 set->key->dtype, type);
5304 }
5305
5306
4/4
✓ Branch 0 (37→38) taken 1125 times.
✓ Branch 1 (37→45) taken 5955 times.
✓ Branch 2 (38→39) taken 394 times.
✓ Branch 3 (38→45) taken 731 times.
7080 if (set->flags & NFT_SET_INTERVAL && set->key->etype == EXPR_CONCAT) {
5307
1/2
✗ Branch 0 (39→40) not taken.
✓ Branch 1 (39→41) taken 394 times.
394 memcpy(&set->desc.field_len, &expr_concat(set->key)->field_len,
5308 sizeof(set->desc.field_len));
5309
1/2
✗ Branch 0 (41→42) not taken.
✓ Branch 1 (41→43) taken 394 times.
394 set->desc.field_count = expr_concat(set->key)->field_count;
5310 394 set->flags |= NFT_SET_CONCAT;
5311
5312
2/2
✓ Branch 0 (43→44) taken 52 times.
✓ Branch 1 (43→45) taken 342 times.
394 if (set->automerge)
5313 52 set->automerge = false;
5314 }
5315
5316
4/4
✓ Branch 0 (46→47) taken 4157 times.
✓ Branch 1 (46→64) taken 2923 times.
✓ Branch 2 (47→48) taken 377 times.
✓ Branch 3 (47→64) taken 3780 times.
7080 if (set_is_anonymous(set->flags) && set->key->etype == EXPR_CONCAT) {
5317 struct expr *i;
5318
5319
4/6
✗ Branch 0 (48→49) not taken.
✓ Branch 1 (48→50) taken 377 times.
✗ Branch 2 (61→62) not taken.
✓ Branch 3 (61→63) taken 1095 times.
✓ Branch 4 (63→51) taken 721 times.
✓ Branch 5 (63→64) taken 374 times.
1095 list_for_each_entry(i, &expr_set(set->init)->expressions, list) {
5320
2/2
✓ Branch 0 (51→52) taken 469 times.
✓ Branch 1 (51→54) taken 252 times.
721 if ((i->etype == EXPR_SET_ELEM &&
5321
2/2
✓ Branch 0 (52→53) taken 1 times.
✓ Branch 1 (52→54) taken 468 times.
469 i->key->etype != EXPR_CONCAT &&
5322
1/2
✗ Branch 0 (53→54) not taken.
✓ Branch 1 (53→58) taken 1 times.
1 i->key->etype != EXPR_SET_ELEM_CATCHALL) ||
5323
2/2
✓ Branch 0 (54→55) taken 252 times.
✓ Branch 1 (54→60) taken 468 times.
720 (i->etype == EXPR_MAPPING &&
5324
1/2
✓ Branch 0 (55→56) taken 252 times.
✗ Branch 1 (55→60) not taken.
252 i->left->etype == EXPR_SET_ELEM &&
5325
2/2
✓ Branch 0 (56→57) taken 6 times.
✓ Branch 1 (56→60) taken 246 times.
252 i->left->key->etype != EXPR_CONCAT &&
5326
2/2
✓ Branch 0 (57→58) taken 2 times.
✓ Branch 1 (57→60) taken 4 times.
6 i->left->key->etype != EXPR_SET_ELEM_CATCHALL))
5327 3 return expr_error(ctx->msgs, i, "expression is not a concatenation");
5328 }
5329 }
5330
5331
2/2
✓ Branch 0 (65→66) taken 1589 times.
✓ Branch 1 (65→79) taken 5488 times.
7077 if (set_is_datamap(set->flags)) {
5332
1/2
✗ Branch 0 (66→67) not taken.
✓ Branch 1 (66→69) taken 1589 times.
1589 if (set->data == NULL)
5333 ✗ return set_error(ctx, set, "map definition does not "
5334 "specify mapping data type");
5335
5336
4/4
✓ Branch 0 (69→70) taken 80 times.
✓ Branch 1 (69→73) taken 1509 times.
✓ Branch 2 (71→72) taken 1 times.
✓ Branch 3 (71→73) taken 79 times.
1669 if (set->data->etype == EXPR_CONCAT &&
5337 80 set_expr_evaluate_concat(ctx, &set->data) < 0)
5338 1 return -1;
5339
5340
2/2
✓ Branch 0 (73→74) taken 12 times.
✓ Branch 1 (73→75) taken 1576 times.
1588 if (set->data->flags & EXPR_F_INTERVAL)
5341 12 set->data->len *= 2;
5342
5343
4/4
✓ Branch 0 (75→76) taken 897 times.
✓ Branch 1 (75→85) taken 691 times.
✓ Branch 2 (76→77) taken 2 times.
✓ Branch 3 (76→85) taken 895 times.
1588 if (set->data->len == 0 && set->data->dtype->type != TYPE_VERDICT)
5344 2 return set_key_data_error(ctx, set,
5345 2 set->data->dtype, type);
5346
2/2
✓ Branch 0 (80→81) taken 132 times.
✓ Branch 1 (80→85) taken 5356 times.
5488 } else if (set_is_objmap(set->flags)) {
5347
1/2
✗ Branch 0 (81→82) not taken.
✓ Branch 1 (81→83) taken 132 times.
132 assert(set->data == NULL);
5348 132 set->data = constant_expr_alloc(&netlink_location, &string_type,
5349 BYTEORDER_HOST_ENDIAN,
5350 NFT_OBJ_MAXNAMELEN * BITS_PER_BYTE,
5351 NULL);
5352
5353 }
5354
5355 /* Default timeout value implies timeout support */
5356
2/2
✓ Branch 0 (85→86) taken 171 times.
✓ Branch 1 (85→87) taken 6903 times.
7074 if (set->timeout)
5357 171 set->flags |= NFT_SET_TIMEOUT;
5358
5359
2/2
✓ Branch 0 (92→88) taken 127 times.
✓ Branch 1 (92→93) taken 7074 times.
7201 list_for_each_entry(stmt, &set->stmt_list, list) {
5360
1/2
✗ Branch 0 (89→90) not taken.
✓ Branch 1 (89→91) taken 127 times.
127 if (stmt_evaluate_stateful(ctx, stmt,type) < 0)
5361 ✗ return -1;
5362 127 num_stmts++;
5363 }
5364
5365
2/2
✓ Branch 0 (93→94) taken 9 times.
✓ Branch 1 (93→95) taken 7065 times.
7074 if (num_stmts > 1)
5366 9 set->flags |= NFT_SET_EXPR;
5367
5368
2/2
✓ Branch 0 (96→97) taken 4154 times.
✓ Branch 1 (96→109) taken 2920 times.
7074 if (set_is_anonymous(set->flags)) {
5369
4/4
✓ Branch 0 (97→98) taken 4147 times.
✓ Branch 1 (97→108) taken 7 times.
✓ Branch 2 (101→102) taken 397 times.
✓ Branch 3 (101→108) taken 3750 times.
8301 if (set->init->etype == EXPR_SET &&
5370
1/2
✗ Branch 0 (98→99) not taken.
✓ Branch 1 (98→100) taken 4147 times.
4147 set_is_interval(expr_set(set->init)->set_flags) &&
5371
4/6
✗ Branch 0 (102→103) not taken.
✓ Branch 1 (102→104) taken 397 times.
✓ Branch 2 (104→105) taken 346 times.
✓ Branch 3 (104→108) taken 51 times.
✗ Branch 4 (106→107) not taken.
✓ Branch 5 (106→108) taken 346 times.
743 !(expr_set(set->init)->set_flags & NFT_SET_CONCAT) &&
5372 346 interval_set_eval(ctx, set, set->init) < 0)
5373 ✗ return -1;
5374
5375 4154 return 0;
5376 }
5377
5378
2/2
✓ Branch 0 (109→110) taken 17 times.
✓ Branch 1 (109→134) taken 2903 times.
2920 if (existing_set) {
5379
4/4
✓ Branch 0 (111→112) taken 7 times.
✓ Branch 1 (111→116) taken 10 times.
✓ Branch 2 (113→114) taken 3 times.
✓ Branch 3 (113→116) taken 4 times.
17 if (set_is_interval(set->flags) && !set_is_interval(existing_set->flags))
5380 3 return set_error(ctx, set,
5381 "existing %s lacks interval flag", type);
5382
3/4
✓ Branch 0 (116→117) taken 8 times.
✓ Branch 1 (116→122) taken 6 times.
✓ Branch 2 (117→118) taken 8 times.
✗ Branch 3 (117→122) not taken.
14 if (set->data && existing_set->data &&
5383
2/2
✓ Branch 0 (119→120) taken 1 times.
✓ Branch 1 (119→122) taken 7 times.
8 !datatype_equal(existing_set->data->dtype, set->data->dtype))
5384 1 return set_error(ctx, set,
5385 "%s already exists with different datatype (%s vs %s)",
5386 1 type, existing_set->data->dtype->desc,
5387 1 set->data->dtype->desc);
5388
2/2
✓ Branch 0 (123→124) taken 1 times.
✓ Branch 1 (123→126) taken 12 times.
13 if (!datatype_equal(existing_set->key->dtype, set->key->dtype))
5389 1 return set_error(ctx, set,
5390 "%s already exists with different datatype (%s vs %s)",
5391 1 type, existing_set->key->dtype->desc,
5392 1 set->key->dtype->desc);
5393 /* Catch attempt to merge set and map */
5394
2/2
✓ Branch 0 (127→128) taken 1 times.
✓ Branch 1 (127→134) taken 11 times.
12 if (!set_type_compatible(set, existing_set))
5395
1/2
✓ Branch 0 (129→130) taken 1 times.
✗ Branch 1 (129→131) not taken.
1 return set_error(ctx, set, "Cannot merge %s with incompatible existing %s of same name",
5396 type,
5397 1 set_is_map(existing_set->flags) ? "map" : "set");
5398 }
5399
5400 2914 set->existing_set = existing_set;
5401
5402 2914 return 0;
5403 }
5404
5405 4123 static bool evaluate_priority(struct eval_ctx *ctx, struct prio_spec *prio,
5406 int family, int hook)
5407 {
5408 char prio_str[NFT_NAME_MAXLEN];
5409 char prio_fst[NFT_NAME_MAXLEN];
5410 struct location loc;
5411 int priority;
5412 int prio_snd;
5413 char op;
5414
5415 4123 expr_set_context(&ctx->ectx, &priority_type, NFT_NAME_MAXLEN * BITS_PER_BYTE);
5416
5417
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 4123 times.
4123 if (expr_evaluate(ctx, &prio->expr) < 0)
5418 ✗ return false;
5419
2/2
✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→10) taken 4122 times.
4123 if (prio->expr->etype != EXPR_VALUE) {
5420 1 expr_error(ctx->msgs, prio->expr, "%s is not a valid "
5421 "priority expression", expr_name(prio->expr));
5422 1 return false;
5423 }
5424
2/2
✓ Branch 0 (10→11) taken 2110 times.
✓ Branch 1 (10→12) taken 2012 times.
4122 if (prio->expr->dtype->type == TYPE_INTEGER)
5425 2110 return true;
5426
5427 2012 mpz_export_data(prio_str, prio->expr->value, BYTEORDER_HOST_ENDIAN,
5428 NFT_NAME_MAXLEN);
5429 2012 loc = prio->expr->location;
5430
5431
2/2
✓ Branch 0 (13→14) taken 1505 times.
✓ Branch 1 (13→17) taken 507 times.
2012 if (sscanf(prio_str, "%255s %c %d", prio_fst, &op, &prio_snd) < 3) {
5432 1505 priority = std_prio_lookup(prio_str, family, hook);
5433
2/2
✓ Branch 0 (15→16) taken 58 times.
✓ Branch 1 (15→25) taken 1447 times.
1505 if (priority == NF_IP_PRI_LAST)
5434 58 return false;
5435 } else {
5436 507 priority = std_prio_lookup(prio_fst, family, hook);
5437
2/2
✓ Branch 0 (18→19) taken 1 times.
✓ Branch 1 (18→20) taken 506 times.
507 if (priority == NF_IP_PRI_LAST)
5438 1 return false;
5439
2/2
✓ Branch 0 (20→21) taken 261 times.
✓ Branch 1 (20→22) taken 245 times.
506 if (op == '+')
5440 261 priority += prio_snd;
5441
1/2
✓ Branch 0 (22→23) taken 245 times.
✗ Branch 1 (22→24) not taken.
245 else if (op == '-')
5442 245 priority -= prio_snd;
5443 else
5444 ✗ return false;
5445 }
5446 1953 expr_free(prio->expr);
5447 1953 prio->expr = constant_expr_alloc(&loc, &integer_type,
5448 BYTEORDER_HOST_ENDIAN,
5449 sizeof(int) * BITS_PER_BYTE,
5450 &priority);
5451 1953 return true;
5452 }
5453
5454 3133 static bool evaluate_expr_variable(struct eval_ctx *ctx, struct expr **exprp)
5455 {
5456 struct expr *expr;
5457
5458
2/2
✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 3131 times.
3133 if (expr_evaluate(ctx, exprp) < 0)
5459 2 return false;
5460
5461 3131 expr = *exprp;
5462
2/2
✓ Branch 0 (5→6) taken 5 times.
✓ Branch 1 (5→10) taken 3126 times.
3131 if (expr->etype != EXPR_VALUE &&
5463
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→10) taken 5 times.
5 expr->etype != EXPR_SET) {
5464 ✗ expr_error(ctx->msgs, expr, "%s is not a valid "
5465 "variable expression", expr_name(expr));
5466 ✗ return false;
5467 }
5468
5469 3131 return true;
5470 }
5471
5472 5 static struct expr *expr_set_to_list(struct eval_ctx *ctx, struct expr *dev_expr)
5473 {
5474 struct expr *expr, *next, *key;
5475 struct location loc;
5476 5 LIST_HEAD(tmp);
5477
5478
4/6
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 5 times.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→17) taken 16 times.
✓ Branch 4 (17→5) taken 11 times.
✓ Branch 5 (17→18) taken 5 times.
16 list_for_each_entry_safe(expr, next, &expr_set(dev_expr)->expressions, list) {
5479 11 list_del(&expr->list);
5480
5481
1/2
✓ Branch 0 (6→7) taken 11 times.
✗ Branch 1 (6→10) not taken.
11 switch (expr->etype) {
5482 11 case EXPR_SET_ELEM:
5483 11 key = expr_clone(expr->key);
5484 11 expr_free(expr);
5485 11 expr = key;
5486 11 break;
5487 ✗ default:
5488 ✗ BUG("invalid expression type %s", expr_name(expr));
5489 break;
5490 }
5491
5492 11 list_add(&expr->list, &tmp);
5493 }
5494
5495 5 loc = dev_expr->location;
5496 5 expr_free(dev_expr);
5497 5 dev_expr = list_expr_alloc(&loc);
5498
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 5 times.
5 list_splice_init(&tmp, &expr_list(dev_expr)->expressions);
5499
5500 5 return dev_expr;
5501 }
5502
5503 1296 static bool evaluate_device_expr(struct eval_ctx *ctx, struct expr **dev_expr)
5504 {
5505 struct expr *expr, *next;
5506 1296 LIST_HEAD(tmp);
5507
5508
2/2
✓ Branch 0 (2→3) taken 4 times.
✓ Branch 1 (2→7) taken 1292 times.
1296 if ((*dev_expr)->etype == EXPR_VARIABLE) {
5509 4 expr_set_context(&ctx->ectx, &ifname_type,
5510 IFNAMSIZ * BITS_PER_BYTE);
5511
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 4 times.
4 if (!evaluate_expr_variable(ctx, dev_expr))
5512 ✗ return false;
5513 }
5514
5515
2/2
✓ Branch 0 (7→8) taken 4 times.
✓ Branch 1 (7→10) taken 1292 times.
1296 if ((*dev_expr)->etype == EXPR_SET)
5516 4 *dev_expr = expr_set_to_list(ctx, *dev_expr);
5517
5518
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 1296 times.
1296 assert((*dev_expr)->etype == EXPR_LIST);
5519
5520
4/6
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 1296 times.
✗ Branch 2 (35→36) not taken.
✓ Branch 3 (35→37) taken 2645 times.
✓ Branch 4 (37→15) taken 1349 times.
✓ Branch 5 (37→38) taken 1296 times.
2645 list_for_each_entry_safe(expr, next, &expr_list(*dev_expr)->expressions, list) {
5521 1349 list_del(&expr->list);
5522
5523
2/3
✓ Branch 0 (16→17) taken 4 times.
✓ Branch 1 (16→29) taken 1345 times.
✗ Branch 2 (16→30) not taken.
1349 switch (expr->etype) {
5524 4 case EXPR_VARIABLE:
5525 4 expr_set_context(&ctx->ectx, &ifname_type,
5526 IFNAMSIZ * BITS_PER_BYTE);
5527
1/2
✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→21) taken 4 times.
4 if (!evaluate_expr_variable(ctx, &expr))
5528 ✗ return false;
5529
5530
2/2
✓ Branch 0 (21→22) taken 1 times.
✓ Branch 1 (21→28) taken 3 times.
4 if (expr->etype == EXPR_SET) {
5531 1 expr = expr_set_to_list(ctx, expr);
5532
1/2
✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→25) taken 1 times.
1 list_splice_init(&expr_list(expr)->expressions, &tmp);
5533 1 expr_free(expr);
5534 1 continue;
5535 }
5536 3 break;
5537 1345 case EXPR_VALUE:
5538 1345 break;
5539 ✗ default:
5540 ✗ BUG("invalid expression type %s", expr_name(expr));
5541 break;
5542 }
5543
5544 1348 list_add(&expr->list, &tmp);
5545 }
5546
1/2
✗ Branch 0 (38→39) not taken.
✓ Branch 1 (38→40) taken 1296 times.
1296 list_splice_init(&tmp, &expr_list(*dev_expr)->expressions);
5547
5548 1296 return true;
5549 }
5550
5551 static uint32_t str2hooknum(uint32_t family, const char *hook);
5552
5553 102 static int flowtable_evaluate(struct eval_ctx *ctx, struct flowtable *ft)
5554 {
5555 struct table *table;
5556
5557 102 table = table_cache_find(&ctx->nft->cache.table_cache,
5558 102 ctx->cmd->handle.table.name,
5559 102 ctx->cmd->handle.family);
5560
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 102 times.
102 if (table == NULL)
5561 ✗ return table_not_found(ctx);
5562
5563
1/2
✓ Branch 0 (7→8) taken 102 times.
✗ Branch 1 (7→14) not taken.
102 if (!ft_cache_find(table, ft->handle.flowtable.name)) {
5564
4/4
✓ Branch 0 (8→9) taken 5 times.
✓ Branch 1 (8→12) taken 97 times.
✓ Branch 2 (9→10) taken 2 times.
✓ Branch 3 (9→12) taken 3 times.
102 if (!ft->hook.name && !ft->dev_expr)
5565 2 return chain_error(ctx, ft, "missing hook and priority in flowtable declaration");
5566
5567 100 ft_cache_add(flowtable_get(ft), table);
5568 }
5569
5570
2/2
✓ Branch 0 (14→15) taken 97 times.
✓ Branch 1 (14→24) taken 3 times.
100 if (ft->hook.name) {
5571 97 ft->hook.num = str2hooknum(NFPROTO_NETDEV, ft->hook.name);
5572
1/2
✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→19) taken 97 times.
97 if (ft->hook.num == NF_INET_NUMHOOKS)
5573 ✗ return chain_error(ctx, ft, "invalid hook %s",
5574 ft->hook.name);
5575
2/2
✓ Branch 0 (20→21) taken 7 times.
✓ Branch 1 (20→24) taken 90 times.
97 if (!evaluate_priority(ctx, &ft->priority, NFPROTO_NETDEV, ft->hook.num))
5576 7 return __stmt_binary_error(ctx, &ft->priority.loc, NULL,
5577 "invalid priority expression %s.",
5578 7 expr_name(ft->priority.expr));
5579 }
5580
5581
3/4
✓ Branch 0 (24→25) taken 86 times.
✓ Branch 1 (24→28) taken 7 times.
✗ Branch 2 (26→27) not taken.
✓ Branch 3 (26→28) taken 86 times.
93 if (ft->dev_expr && !evaluate_device_expr(ctx, &ft->dev_expr))
5582 ✗ return -1;
5583
5584 93 return 0;
5585 }
5586
5587 /* make src point at dst, either via handle.position or handle.position_id */
5588 25 static void link_rules(struct rule *src, struct rule *dst)
5589 {
5590 static uint32_t ref_id = 0;
5591
5592
2/2
✓ Branch 0 (2→3) taken 10 times.
✓ Branch 1 (2→4) taken 15 times.
25 if (dst->handle.handle.id) {
5593 /* dst is in kernel, make src reference it by handle */
5594 10 src->handle.position.id = dst->handle.handle.id;
5595 10 src->handle.position.location = src->handle.index.location;
5596 10 return;
5597 }
5598
5599 /* dst is not in kernel, make src reference it by per-transaction ID */
5600
1/2
✓ Branch 0 (4→5) taken 15 times.
✗ Branch 1 (4→6) not taken.
15 if (!dst->handle.rule_id)
5601 15 dst->handle.rule_id = ++ref_id;
5602 15 src->handle.position_id = dst->handle.rule_id;
5603 }
5604
5605 232 static int rule_cache_update(struct eval_ctx *ctx, enum cmd_ops op)
5606 {
5607 232 struct rule *rule = ctx->rule, *ref = NULL;
5608 struct table *table;
5609 struct chain *chain;
5610
5611 232 table = table_cache_find(&ctx->nft->cache.table_cache,
5612 rule->handle.table.name,
5613 rule->handle.family);
5614
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 232 times.
232 if (!table)
5615 ✗ return table_not_found(ctx);
5616
5617 /* chain is anonymous, adding new rules via index is not supported. */
5618
2/2
✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→8) taken 231 times.
232 if (!rule->handle.chain.name)
5619 1 return 0;
5620
5621 231 chain = chain_cache_find(table, rule->handle.chain.name);
5622
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 231 times.
231 if (!chain)
5623 ✗ return chain_not_found(ctx);
5624
5625
2/2
✓ Branch 0 (12→13) taken 27 times.
✓ Branch 1 (12→19) taken 204 times.
231 if (rule->handle.index.id) {
5626 27 ref = rule_lookup_by_index(chain, rule->handle.index.id);
5627
2/2
✓ Branch 0 (14→15) taken 2 times.
✓ Branch 1 (14→18) taken 25 times.
27 if (!ref)
5628 2 return cmd_error(ctx, &rule->handle.index.location,
5629 "Could not process rule: %s",
5630 strerror(ENOENT));
5631
5632 25 link_rules(rule, ref);
5633
2/2
✓ Branch 0 (19→20) taken 1 times.
✓ Branch 1 (19→25) taken 203 times.
204 } else if (rule->handle.handle.id) {
5634 1 ref = rule_lookup(chain, rule->handle.handle.id);
5635
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→31) taken 1 times.
1 if (!ref)
5636 ✗ return cmd_error(ctx, &rule->handle.handle.location,
5637 "Could not process rule: %s",
5638 strerror(ENOENT));
5639
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→31) taken 203 times.
203 } else if (rule->handle.position.id) {
5640 ✗ ref = rule_lookup(chain, rule->handle.position.id);
5641 ✗ if (!ref)
5642 ✗ return cmd_error(ctx, &rule->handle.position.location,
5643 "Could not process rule: %s",
5644 strerror(ENOENT));
5645 }
5646
5647
3/5
✓ Branch 0 (31→32) taken 29 times.
✓ Branch 1 (31→37) taken 199 times.
✓ Branch 2 (31→42) taken 1 times.
✗ Branch 3 (31→44) not taken.
✗ Branch 4 (31→47) not taken.
229 switch (op) {
5648 29 case CMD_INSERT:
5649 29 rule_get(rule);
5650
2/2
✓ Branch 0 (33→34) taken 9 times.
✓ Branch 1 (33→35) taken 20 times.
29 if (ref)
5651 9 list_add_tail(&rule->list, &ref->list);
5652 else
5653 20 list_add(&rule->list, &chain->rules);
5654 29 break;
5655 199 case CMD_ADD:
5656 199 rule_get(rule);
5657
2/2
✓ Branch 0 (38→39) taken 16 times.
✓ Branch 1 (38→40) taken 183 times.
199 if (ref)
5658 16 list_add(&rule->list, &ref->list);
5659 else
5660 183 list_add_tail(&rule->list, &chain->rules);
5661 199 break;
5662 1 case CMD_REPLACE:
5663 1 rule_get(rule);
5664 1 list_add(&rule->list, &ref->list);
5665 /* fall through */
5666 1 case CMD_DELETE:
5667 1 list_del(&ref->list);
5668 1 rule_free(ref);
5669 1 break;
5670 ✗ default:
5671 ✗ break;
5672 }
5673 229 return 0;
5674 }
5675
5676 108851 static int rule_evaluate(struct eval_ctx *ctx, struct rule *rule,
5677 enum cmd_ops op)
5678 {
5679 108851 struct stmt *stmt, *tstmt = NULL;
5680 struct error_record *erec;
5681
5682 108851 proto_ctx_init(&ctx->_pctx[0], rule->handle.family, ctx->nft->debug_mask, false);
5683 /* use NFPROTO_BRIDGE to set up proto_eth as base protocol. */
5684 108851 proto_ctx_init(&ctx->_pctx[1], NFPROTO_BRIDGE, ctx->nft->debug_mask, true);
5685 108851 memset(&ctx->ectx, 0, sizeof(ctx->ectx));
5686
5687 108851 ctx->rule = rule;
5688
2/2
✓ Branch 0 (14→5) taken 119367 times.
✓ Branch 1 (14→15) taken 108417 times.
227784 list_for_each_entry(stmt, &rule->stmts, list) {
5689
2/2
✓ Branch 0 (5→6) taken 12 times.
✓ Branch 1 (5→8) taken 119355 times.
119367 if (tstmt != NULL)
5690 12 return stmt_binary_error(ctx, stmt, tstmt,
5691 "Statement after terminal "
5692 "statement has no effect");
5693
5694 119355 ctx->stmt = stmt;
5695
2/2
✓ Branch 0 (9→10) taken 422 times.
✓ Branch 1 (9→11) taken 118933 times.
119355 if (stmt_evaluate(ctx, stmt) < 0)
5696 422 return -1;
5697
2/2
✓ Branch 0 (11→12) taken 15180 times.
✓ Branch 1 (11→13) taken 103753 times.
118933 if (stmt->flags & STMT_F_TERMINAL)
5698 15180 tstmt = stmt;
5699
5700 118933 ctx->inner_desc = NULL;
5701 }
5702
5703 108417 erec = rule_postprocess(rule);
5704
1/2
✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→19) taken 108417 times.
108417 if (erec != NULL) {
5705 ✗ erec_queue(erec, ctx->msgs);
5706 ✗ return -1;
5707 }
5708
5709
2/2
✓ Branch 0 (20→21) taken 232 times.
✓ Branch 1 (20→23) taken 108185 times.
108417 if (nft_cache_needs_update(&ctx->nft->cache))
5710 232 return rule_cache_update(ctx, op);
5711
5712 108185 return 0;
5713 }
5714
5715 4127 static uint32_t str2hooknum(uint32_t family, const char *hook)
5716 {
5717
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 4127 times.
4127 if (!hook)
5718 ✗ return NF_INET_NUMHOOKS;
5719
5720
4/5
✓ Branch 0 (4→5) taken 1039 times.
✓ Branch 1 (4→7) taken 1738 times.
✓ Branch 2 (4→18) taken 60 times.
✓ Branch 3 (4→25) taken 1290 times.
✗ Branch 4 (4→30) not taken.
4127 switch (family) {
5721 1039 case NFPROTO_INET:
5722
2/2
✓ Branch 0 (5→6) taken 29 times.
✓ Branch 1 (5→7) taken 1010 times.
1039 if (!strcmp(hook, "ingress"))
5723 29 return NF_INET_INGRESS;
5724 /* fall through */
5725 case NFPROTO_IPV4:
5726 case NFPROTO_BRIDGE:
5727 case NFPROTO_IPV6:
5728 /* These families have overlapping values for each hook */
5729
2/2
✓ Branch 0 (7→8) taken 554 times.
✓ Branch 1 (7→9) taken 2194 times.
2748 if (!strcmp(hook, "prerouting"))
5730 554 return NF_INET_PRE_ROUTING;
5731
2/2
✓ Branch 0 (9→10) taken 658 times.
✓ Branch 1 (9→11) taken 1536 times.
2194 else if (!strcmp(hook, "input"))
5732 658 return NF_INET_LOCAL_IN;
5733
2/2
✓ Branch 0 (11→12) taken 331 times.
✓ Branch 1 (11→13) taken 1205 times.
1536 else if (!strcmp(hook, "forward"))
5734 331 return NF_INET_FORWARD;
5735
2/2
✓ Branch 0 (13→14) taken 423 times.
✓ Branch 1 (13→15) taken 782 times.
1205 else if (!strcmp(hook, "postrouting"))
5736 423 return NF_INET_POST_ROUTING;
5737
1/2
✓ Branch 0 (15→16) taken 782 times.
✗ Branch 1 (15→17) not taken.
782 else if (!strcmp(hook, "output"))
5738 782 return NF_INET_LOCAL_OUT;
5739 ✗ break;
5740 60 case NFPROTO_ARP:
5741
2/2
✓ Branch 0 (18→19) taken 30 times.
✓ Branch 1 (18→20) taken 30 times.
60 if (!strcmp(hook, "input"))
5742 30 return NF_ARP_IN;
5743
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 30 times.
30 else if (!strcmp(hook, "forward"))
5744 ✗ return NF_ARP_FORWARD;
5745
1/2
✓ Branch 0 (22→23) taken 30 times.
✗ Branch 1 (22→24) not taken.
30 else if (!strcmp(hook, "output"))
5746 30 return NF_ARP_OUT;
5747 ✗ break;
5748 1290 case NFPROTO_NETDEV:
5749
2/2
✓ Branch 0 (25→26) taken 718 times.
✓ Branch 1 (25→27) taken 572 times.
1290 if (!strcmp(hook, "ingress"))
5750 718 return NF_NETDEV_INGRESS;
5751
1/2
✓ Branch 0 (27→28) taken 572 times.
✗ Branch 1 (27→29) not taken.
572 else if (!strcmp(hook, "egress"))
5752 572 return NF_NETDEV_EGRESS;
5753 ✗ break;
5754 ✗ default:
5755 ✗ break;
5756 }
5757
5758 ✗ return NF_INET_NUMHOOKS;
5759 }
5760
5761 7503 static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain)
5762 {
5763 struct table *table;
5764
5765 7503 table = table_cache_find(&ctx->nft->cache.table_cache,
5766 7503 ctx->cmd->handle.table.name,
5767 7503 ctx->cmd->handle.family);
5768
2/2
✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→6) taken 7502 times.
7503 if (table == NULL)
5769 1 return table_not_found(ctx);
5770
5771
2/2
✓ Branch 0 (6→7) taken 2059 times.
✓ Branch 1 (6→13) taken 5443 times.
7502 if (chain == NULL) {
5772
2/2
✓ Branch 0 (8→9) taken 2058 times.
✓ Branch 1 (8→12) taken 1 times.
2059 if (!chain_cache_find(table, ctx->cmd->handle.chain.name)) {
5773 2058 chain = chain_alloc();
5774 2058 handle_merge(&chain->handle, &ctx->cmd->handle);
5775 2058 chain_cache_add(chain, table);
5776 }
5777 2059 return 0;
5778
1/2
✓ Branch 0 (13→14) taken 5443 times.
✗ Branch 1 (13→18) not taken.
5443 } else if (!(chain->flags & CHAIN_F_BINDING)) {
5779
2/2
✓ Branch 0 (15→16) taken 5438 times.
✓ Branch 1 (15→18) taken 5 times.
5443 if (!chain_cache_find(table, chain->handle.chain.name))
5780 5438 chain_cache_add(chain_get(chain), table);
5781 }
5782
5783
2/2
✓ Branch 0 (18→19) taken 4034 times.
✓ Branch 1 (18→38) taken 1409 times.
5443 if (chain->flags & CHAIN_F_BASECHAIN) {
5784
2/2
✓ Branch 0 (19→20) taken 4026 times.
✓ Branch 1 (19→25) taken 8 times.
4034 if (chain->hook.name) {
5785 4026 chain->hook.num = str2hooknum(chain->handle.family,
5786 chain->hook.name);
5787
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→25) taken 4026 times.
4026 if (chain->hook.num == NF_INET_NUMHOOKS)
5788 ✗ return __stmt_binary_error(ctx, &chain->hook.loc, NULL,
5789 "The %s family does not support this hook",
5790 ✗ family2str(chain->handle.family));
5791 }
5792
2/2
✓ Branch 0 (25→26) taken 4026 times.
✓ Branch 1 (25→31) taken 8 times.
4034 if (chain->priority.expr) {
5793
2/2
✓ Branch 0 (27→28) taken 53 times.
✓ Branch 1 (27→31) taken 3973 times.
4026 if (!evaluate_priority(ctx, &chain->priority,
5794 4026 chain->handle.family, chain->hook.num))
5795 53 return __stmt_binary_error(ctx, &chain->priority.loc, NULL,
5796 "invalid priority expression %s in this context.",
5797 53 expr_name(chain->priority.expr));
5798 }
5799
2/2
✓ Branch 0 (31→32) taken 3125 times.
✓ Branch 1 (31→38) taken 856 times.
3981 if (chain->policy) {
5800 3125 expr_set_context(&ctx->ectx, &policy_type,
5801 NFT_NAME_MAXLEN * BITS_PER_BYTE);
5802
2/2
✓ Branch 0 (34→35) taken 2 times.
✓ Branch 1 (34→38) taken 3123 times.
3125 if (!evaluate_expr_variable(ctx, &chain->policy))
5803 2 return chain_error(ctx, chain, "invalid policy expression %s",
5804 expr_name(chain->policy));
5805 }
5806 }
5807
5808
2/2
✓ Branch 0 (38→39) taken 1210 times.
✓ Branch 1 (38→52) taken 4178 times.
5388 if (chain->dev_expr) {
5809
2/2
✓ Branch 0 (39→40) taken 4 times.
✓ Branch 1 (39→41) taken 1206 times.
1210 if (!(chain->flags & CHAIN_F_BASECHAIN))
5810 4 chain->flags |= CHAIN_F_BASECHAIN;
5811
5812
2/2
✓ Branch 0 (41→42) taken 25 times.
✓ Branch 1 (41→44) taken 1185 times.
1210 if (chain->handle.family == NFPROTO_NETDEV ||
5813
1/2
✓ Branch 0 (42→43) taken 25 times.
✗ Branch 1 (42→49) not taken.
25 (chain->handle.family == NFPROTO_INET &&
5814
1/2
✓ Branch 0 (43→44) taken 25 times.
✗ Branch 1 (43→49) not taken.
25 chain->hook.num == NF_INET_INGRESS)) {
5815
1/2
✓ Branch 0 (44→45) taken 1210 times.
✗ Branch 1 (44→48) not taken.
1210 if (chain->dev_expr &&
5816
1/2
✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→48) taken 1210 times.
1210 !evaluate_device_expr(ctx, &chain->dev_expr))
5817 ✗ return -1;
5818 ✗ } else if (chain->dev_expr) {
5819 ✗ return __stmt_binary_error(ctx, &chain->dev_expr->location, NULL,
5820 "This chain type cannot be bound to device");
5821 }
5822 }
5823
5824 5388 return 0;
5825 }
5826
5827 26 static int ct_expect_evaluate(struct eval_ctx *ctx, struct obj *obj)
5828 {
5829 26 struct ct_expect *ct = &obj->ct_expect;
5830
5831
1/2
✓ Branch 0 (2→3) taken 26 times.
✗ Branch 1 (2→6) not taken.
26 if (!ct->l4proto ||
5832
2/2
✓ Branch 0 (3→4) taken 25 times.
✓ Branch 1 (3→6) taken 1 times.
26 !ct->dport ||
5833
2/2
✓ Branch 0 (4→5) taken 24 times.
✓ Branch 1 (4→6) taken 1 times.
25 !ct->timeout ||
5834
2/2
✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→8) taken 23 times.
24 !ct->size)
5835 3 return __stmt_binary_error(ctx, &obj->location, NULL,
5836 "missing options");
5837
5838 23 return 0;
5839 }
5840
5841 23 static int ct_timeout_evaluate(struct eval_ctx *ctx, struct obj *obj)
5842 {
5843 23 struct ct_timeout *ct = &obj->ct_timeout;
5844 struct timeout_state *ts, *next;
5845 unsigned int i;
5846
5847
2/2
✓ Branch 0 (4→3) taken 163 times.
✓ Branch 1 (4→5) taken 23 times.
186 for (i = 0; i < timeout_protocol[ct->l4proto].array_size; i++)
5848 163 ct->timeout[i] = timeout_protocol[ct->l4proto].dflt_timeout[i];
5849
5850
2/2
✓ Branch 0 (12→6) taken 41 times.
✓ Branch 1 (12→13) taken 21 times.
62 list_for_each_entry_safe(ts, next, &ct->timeout_list, head) {
5851
2/2
✓ Branch 0 (7→8) taken 2 times.
✓ Branch 1 (7→10) taken 39 times.
41 if (timeout_str2num(ct->l4proto, ts) < 0)
5852 2 return __stmt_binary_error(ctx, &ts->location, NULL,
5853 "invalid state for this protocol");
5854
5855 39 ct->timeout[ts->timeout_index] = ts->timeout_value;
5856 39 list_del(&ts->head);
5857 39 free_const(ts->timeout_str);
5858 39 free(ts);
5859 }
5860
5861 21 return 0;
5862 }
5863
5864 35 static int tunnel_evaluate_addr(struct eval_ctx *ctx, struct expr **exprp)
5865 {
5866 struct expr *e;
5867 int ret;
5868
5869 35 ret = expr_evaluate(ctx, exprp);
5870
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 35 times.
35 if (ret < 0)
5871 ✗ return ret;
5872
5873 35 e = *exprp;
5874
2/2
✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→9) taken 34 times.
35 if (e->etype != EXPR_VALUE)
5875 1 return expr_error(ctx->msgs, e, "must be a value, not %s", expr_name(e));
5876
5877 34 return 0;
5878 }
5879
5880 21 static int tunnel_evaluate(struct eval_ctx *ctx, struct obj *obj)
5881 {
5882
2/2
✓ Branch 0 (2→3) taken 18 times.
✓ Branch 1 (2→7) taken 3 times.
21 if (obj->tunnel.src) {
5883 18 expr_set_context(&ctx->ectx, obj->tunnel.src->dtype,
5884 18 obj->tunnel.src->dtype->size);
5885
2/2
✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→7) taken 17 times.
18 if (tunnel_evaluate_addr(ctx, &obj->tunnel.src) < 0)
5886 1 return -1;
5887 }
5888
5889
2/2
✓ Branch 0 (7→8) taken 17 times.
✓ Branch 1 (7→16) taken 3 times.
20 if (obj->tunnel.dst) {
5890 17 expr_set_context(&ctx->ectx, obj->tunnel.dst->dtype,
5891 17 obj->tunnel.dst->dtype->size);
5892
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 17 times.
17 if (tunnel_evaluate_addr(ctx, &obj->tunnel.dst) < 0)
5893 ✗ return -1;
5894
5895
1/2
✓ Branch 0 (12→13) taken 17 times.
✗ Branch 1 (12→16) not taken.
17 if (obj->tunnel.src &&
5896
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→16) taken 17 times.
17 obj->tunnel.src->dtype != obj->tunnel.dst->dtype)
5897 ✗ return __stmt_binary_error(ctx, &obj->location, NULL,
5898 "specify either ip or ip6 for address");
5899 }
5900
5901 20 return 0;
5902 }
5903
5904 469 static int obj_evaluate(struct eval_ctx *ctx, struct obj *obj)
5905 {
5906 struct table *table;
5907
5908 469 table = table_cache_find(&ctx->nft->cache.table_cache,
5909 469 ctx->cmd->handle.table.name,
5910 469 ctx->cmd->handle.family);
5911
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 469 times.
469 if (!table)
5912 ✗ return table_not_found(ctx);
5913
5914
1/2
✓ Branch 0 (7→8) taken 469 times.
✗ Branch 1 (7→10) not taken.
469 if (!obj_cache_find(table, obj->handle.obj.name, obj->type))
5915 469 obj_cache_add(obj_get(obj), table);
5916
5917
4/4
✓ Branch 0 (10→11) taken 23 times.
✓ Branch 1 (10→12) taken 26 times.
✓ Branch 2 (10→13) taken 21 times.
✓ Branch 3 (10→14) taken 399 times.
469 switch (obj->type) {
5918 23 case NFT_OBJECT_CT_TIMEOUT:
5919 23 return ct_timeout_evaluate(ctx, obj);
5920 26 case NFT_OBJECT_CT_EXPECT:
5921 26 return ct_expect_evaluate(ctx, obj);
5922 21 case NFT_OBJECT_TUNNEL:
5923 21 return tunnel_evaluate(ctx, obj);
5924 399 default:
5925 399 break;
5926 }
5927
5928 399 return 0;
5929 }
5930
5931 3524 static int table_evaluate(struct eval_ctx *ctx, struct table *table)
5932 {
5933
2/2
✓ Branch 0 (3→4) taken 3357 times.
✓ Branch 1 (3→10) taken 167 times.
3524 if (!table_cache_find(&ctx->nft->cache.table_cache,
5934 3524 ctx->cmd->handle.table.name,
5935 3524 ctx->cmd->handle.family)) {
5936
2/2
✓ Branch 0 (4→5) taken 2068 times.
✓ Branch 1 (4→8) taken 1289 times.
3357 if (!table) {
5937 2068 table = table_alloc();
5938 2068 handle_merge(&table->handle, &ctx->cmd->handle);
5939 2068 table_cache_add(table, &ctx->nft->cache);
5940 } else {
5941 1289 table_cache_add(table_get(table), &ctx->nft->cache);
5942 }
5943 }
5944
5945 3524 return 0;
5946 }
5947
5948 147704 static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)
5949 {
5950
8/9
✓ Branch 0 (2→3) taken 23357 times.
✓ Branch 1 (2→4) taken 2929 times.
✓ Branch 2 (2→6) taken 1082 times.
✓ Branch 3 (2→7) taken 108738 times.
✓ Branch 4 (2→9) taken 7503 times.
✓ Branch 5 (2→10) taken 3524 times.
✓ Branch 6 (2→11) taken 102 times.
✓ Branch 7 (2→13) taken 469 times.
✗ Branch 8 (2→15) not taken.
147704 switch (cmd->obj) {
5951 23357 case CMD_OBJ_ELEMENTS:
5952 23357 return setelem_evaluate(ctx, cmd);
5953 2929 case CMD_OBJ_SET:
5954 2929 handle_merge(&cmd->set->handle, &cmd->handle);
5955 2929 return set_evaluate(ctx, cmd->set);
5956 1082 case CMD_OBJ_SETELEMS:
5957 1082 return elems_evaluate(ctx, cmd->set);
5958 108738 case CMD_OBJ_RULE:
5959 108738 handle_merge(&cmd->rule->handle, &cmd->handle);
5960 108738 return rule_evaluate(ctx, cmd->rule, cmd->op);
5961 7503 case CMD_OBJ_CHAIN:
5962 7503 return chain_evaluate(ctx, cmd->chain);
5963 3524 case CMD_OBJ_TABLE:
5964 3524 return table_evaluate(ctx, cmd->table);
5965 102 case CMD_OBJ_FLOWTABLE:
5966 102 handle_merge(&cmd->flowtable->handle, &cmd->handle);
5967 102 return flowtable_evaluate(ctx, cmd->flowtable);
5968 469 case CMD_OBJ_COUNTER:
5969 case CMD_OBJ_QUOTA:
5970 case CMD_OBJ_CT_HELPER:
5971 case CMD_OBJ_LIMIT:
5972 case CMD_OBJ_CT_TIMEOUT:
5973 case CMD_OBJ_SECMARK:
5974 case CMD_OBJ_CT_EXPECT:
5975 case CMD_OBJ_SYNPROXY:
5976 case CMD_OBJ_TUNNEL:
5977 469 handle_merge(&cmd->object->handle, &cmd->handle);
5978 469 return obj_evaluate(ctx, cmd->object);
5979 ✗ default:
5980 ✗ BUG("invalid command object type %u", cmd->obj);
5981 }
5982 }
5983
5984 272 static void table_del_cache(struct eval_ctx *ctx, struct cmd *cmd)
5985 {
5986 struct table *table;
5987
5988
2/2
✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→4) taken 269 times.
272 if (!cmd->handle.table.name)
5989 3 return;
5990
5991 269 table = table_cache_find(&ctx->nft->cache.table_cache,
5992 cmd->handle.table.name,
5993 cmd->handle.family);
5994
2/2
✓ Branch 0 (5→6) taken 4 times.
✓ Branch 1 (5→7) taken 265 times.
269 if (!table)
5995 4 return;
5996
5997 265 table_cache_del(table);
5998 265 table_free(table);
5999 }
6000
6001 354 static void chain_del_cache(struct eval_ctx *ctx, struct cmd *cmd)
6002 {
6003 struct table *table;
6004 struct chain *chain;
6005
6006
2/2
✓ Branch 0 (2→3) taken 4 times.
✓ Branch 1 (2→4) taken 350 times.
354 if (!cmd->handle.chain.name)
6007 4 return;
6008
6009 350 table = table_cache_find(&ctx->nft->cache.table_cache,
6010 cmd->handle.table.name,
6011 cmd->handle.family);
6012
2/2
✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→7) taken 349 times.
350 if (!table)
6013 1 return;
6014
6015 349 chain = chain_cache_find(table, cmd->handle.chain.name);
6016
2/2
✓ Branch 0 (8→9) taken 345 times.
✓ Branch 1 (8→10) taken 4 times.
349 if (!chain)
6017 345 return;
6018
6019 4 chain_cache_del(chain);
6020 4 chain_free(chain);
6021 }
6022
6023 354 static int chain_del_evaluate(struct eval_ctx *ctx, struct cmd *cmd)
6024 {
6025 354 struct chain *chain = cmd->chain;
6026
6027
5/6
✓ Branch 0 (2→3) taken 6 times.
✓ Branch 1 (2→10) taken 348 times.
✓ Branch 2 (3→4) taken 4 times.
✓ Branch 3 (3→10) taken 2 times.
✓ Branch 4 (4→5) taken 4 times.
✗ Branch 5 (4→10) not taken.
354 if (chain && chain->flags & CHAIN_F_BASECHAIN && chain->hook.name) {
6028 4 chain->hook.num = str2hooknum(chain->handle.family,
6029 chain->hook.name);
6030
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→10) taken 4 times.
4 if (chain->hook.num == NF_INET_NUMHOOKS)
6031 ✗ return __stmt_binary_error(ctx, &chain->hook.loc, NULL,
6032 "The %s family does not support this hook",
6033 ✗ family2str(chain->handle.family));
6034 }
6035 354 chain_del_cache(ctx, cmd);
6036 354 return 0;
6037 }
6038
6039 89 static void set_del_cache(struct eval_ctx *ctx, struct cmd *cmd)
6040 {
6041 struct table *table;
6042 struct set *set;
6043
6044
2/2
✓ Branch 0 (2→3) taken 5 times.
✓ Branch 1 (2→4) taken 84 times.
89 if (!cmd->handle.set.name)
6045 5 return;
6046
6047 84 table = table_cache_find(&ctx->nft->cache.table_cache,
6048 cmd->handle.table.name,
6049 cmd->handle.family);
6050
2/2
✓ Branch 0 (5→6) taken 5 times.
✓ Branch 1 (5→7) taken 79 times.
84 if (!table)
6051 5 return;
6052
6053 79 set = set_cache_find(table, cmd->handle.set.name);
6054
2/2
✓ Branch 0 (8→9) taken 72 times.
✓ Branch 1 (8→10) taken 7 times.
79 if (!set)
6055 72 return;
6056
6057 7 set_cache_del(set);
6058 7 set_free(set);
6059 }
6060
6061 36 static void ft_del_cache(struct eval_ctx *ctx, struct cmd *cmd)
6062 {
6063 struct flowtable *ft;
6064 struct table *table;
6065
6066
2/2
✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→4) taken 33 times.
36 if (!cmd->handle.flowtable.name)
6067 3 return;
6068
6069 33 table = table_cache_find(&ctx->nft->cache.table_cache,
6070 cmd->handle.table.name,
6071 cmd->handle.family);
6072
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 33 times.
33 if (!table)
6073 ✗ return;
6074
6075 33 ft = ft_cache_find(table, cmd->handle.flowtable.name);
6076
1/2
✓ Branch 0 (8→9) taken 33 times.
✗ Branch 1 (8→10) not taken.
33 if (!ft)
6077 33 return;
6078
6079 ✗ ft_cache_del(ft);
6080 ✗ flowtable_free(ft);
6081 }
6082
6083 28 static void obj_del_cache(struct eval_ctx *ctx, struct cmd *cmd, int type)
6084 {
6085 struct table *table;
6086 struct obj *obj;
6087
6088
2/2
✓ Branch 0 (2→3) taken 4 times.
✓ Branch 1 (2→4) taken 24 times.
28 if (!cmd->handle.obj.name)
6089 4 return;
6090
6091 24 table = table_cache_find(&ctx->nft->cache.table_cache,
6092 cmd->handle.table.name,
6093 cmd->handle.family);
6094
2/2
✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→7) taken 23 times.
24 if (!table)
6095 1 return;
6096
6097 23 obj = obj_cache_find(table, cmd->handle.obj.name, type);
6098
2/2
✓ Branch 0 (8→9) taken 22 times.
✓ Branch 1 (8→10) taken 1 times.
23 if (!obj)
6099 22 return;
6100
6101 1 obj_cache_del(obj);
6102 1 obj_free(obj);
6103 }
6104
6105 2853 static int cmd_evaluate_delete(struct eval_ctx *ctx, struct cmd *cmd)
6106 {
6107
11/16
✓ Branch 0 (2→3) taken 2063 times.
✓ Branch 1 (2→4) taken 89 times.
✓ Branch 2 (2→6) taken 11 times.
✓ Branch 3 (2→7) taken 354 times.
✓ Branch 4 (2→8) taken 272 times.
✓ Branch 5 (2→10) taken 36 times.
✓ Branch 6 (2→12) taken 9 times.
✓ Branch 7 (2→14) taken 6 times.
✓ Branch 8 (2→16) taken 5 times.
✓ Branch 9 (2→18) taken 4 times.
✓ Branch 10 (2→20) taken 4 times.
✗ Branch 11 (2→22) not taken.
✗ Branch 12 (2→24) not taken.
✗ Branch 13 (2→26) not taken.
✗ Branch 14 (2→28) not taken.
✗ Branch 15 (2→30) not taken.
2853 switch (cmd->obj) {
6108 2063 case CMD_OBJ_ELEMENTS:
6109 2063 return setelem_evaluate(ctx, cmd);
6110 89 case CMD_OBJ_SET:
6111 89 set_del_cache(ctx, cmd);
6112 89 return 0;
6113 11 case CMD_OBJ_RULE:
6114 11 return 0;
6115 354 case CMD_OBJ_CHAIN:
6116 354 return chain_del_evaluate(ctx, cmd);
6117 272 case CMD_OBJ_TABLE:
6118 272 table_del_cache(ctx, cmd);
6119 272 return 0;
6120 36 case CMD_OBJ_FLOWTABLE:
6121 36 ft_del_cache(ctx, cmd);
6122 36 return 0;
6123 9 case CMD_OBJ_COUNTER:
6124 9 obj_del_cache(ctx, cmd, NFT_OBJECT_COUNTER);
6125 9 return 0;
6126 6 case CMD_OBJ_QUOTA:
6127 6 obj_del_cache(ctx, cmd, NFT_OBJECT_QUOTA);
6128 6 return 0;
6129 5 case CMD_OBJ_CT_HELPER:
6130 5 obj_del_cache(ctx, cmd, NFT_OBJECT_CT_HELPER);
6131 5 return 0;
6132 4 case CMD_OBJ_CT_TIMEOUT:
6133 4 obj_del_cache(ctx, cmd, NFT_OBJECT_CT_TIMEOUT);
6134 4 return 0;
6135 4 case CMD_OBJ_LIMIT:
6136 4 obj_del_cache(ctx, cmd, NFT_OBJECT_LIMIT);
6137 4 return 0;
6138 ✗ case CMD_OBJ_SECMARK:
6139 ✗ obj_del_cache(ctx, cmd, NFT_OBJECT_SECMARK);
6140 ✗ return 0;
6141 ✗ case CMD_OBJ_CT_EXPECT:
6142 ✗ obj_del_cache(ctx, cmd, NFT_OBJECT_CT_EXPECT);
6143 ✗ return 0;
6144 ✗ case CMD_OBJ_SYNPROXY:
6145 ✗ obj_del_cache(ctx, cmd, NFT_OBJECT_SYNPROXY);
6146 ✗ return 0;
6147 ✗ case CMD_OBJ_TUNNEL:
6148 ✗ obj_del_cache(ctx, cmd, NFT_OBJECT_TUNNEL);
6149 ✗ return 0;
6150 ✗ default:
6151 ✗ BUG("invalid command object type %u", cmd->obj);
6152 }
6153 }
6154
6155 1064 static int cmd_evaluate_get(struct eval_ctx *ctx, struct cmd *cmd)
6156 {
6157
1/2
✓ Branch 0 (2→3) taken 1064 times.
✗ Branch 1 (2→4) not taken.
1064 switch (cmd->obj) {
6158 1064 case CMD_OBJ_ELEMENTS:
6159 1064 return setelem_evaluate(ctx, cmd);
6160 ✗ default:
6161 ✗ BUG("invalid command object type %u", cmd->obj);
6162 }
6163 }
6164
6165 31 static int obj_not_found(struct eval_ctx *ctx, const struct location *loc,
6166 const char *obj_name)
6167 {
6168 const struct table *table;
6169 struct obj *obj;
6170
6171 31 obj = obj_lookup_fuzzy(obj_name, &ctx->nft->cache, &table);
6172
1/2
✓ Branch 0 (3→4) taken 31 times.
✗ Branch 1 (3→7) not taken.
31 if (obj == NULL)
6173 31 return cmd_error(ctx, loc, "%s", strerror(ENOENT));
6174
6175 ✗ return cmd_error(ctx, loc,
6176 "%s; did you mean obj '%s' in table %s '%s'?",
6177 strerror(ENOENT), obj->handle.obj.name,
6178 family2str(obj->handle.family),
6179 table->handle.table.name);
6180 }
6181
6182 325 static int cmd_evaluate_list_obj(struct eval_ctx *ctx, const struct cmd *cmd,
6183 uint32_t obj_type)
6184 {
6185 const struct table *table;
6186
6187
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 325 times.
325 if (obj_type == NFT_OBJECT_UNSPEC)
6188 ✗ obj_type = NFT_OBJECT_COUNTER;
6189
6190 325 table = table_cache_find(&ctx->nft->cache.table_cache,
6191 325 cmd->handle.table.name,
6192 325 cmd->handle.family);
6193
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 325 times.
325 if (table == NULL)
6194 ✗ return table_not_found(ctx);
6195
6196
2/2
✓ Branch 0 (9→10) taken 31 times.
✓ Branch 1 (9→12) taken 294 times.
325 if (!obj_cache_find(table, cmd->handle.obj.name, obj_type))
6197 31 return obj_not_found(ctx, &cmd->handle.obj.location,
6198 31 cmd->handle.obj.name);
6199
6200 294 return 0;
6201 }
6202
6203 16420 static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)
6204 {
6205 struct flowtable *ft;
6206 struct table *table;
6207 struct set *set;
6208
6209
14/17
✓ Branch 0 (2→3) taken 12609 times.
✓ Branch 1 (2→10) taken 1025 times.
✓ Branch 2 (2→32) taken 1278 times.
✓ Branch 3 (2→41) taken 2 times.
✓ Branch 4 (2→50) taken 9 times.
✓ Branch 5 (2→51) taken 280 times.
✓ Branch 6 (2→52) taken 4 times.
✓ Branch 7 (2→53) taken 10 times.
✓ Branch 8 (2→54) taken 8 times.
✗ Branch 9 (2→55) not taken.
✓ Branch 10 (2→56) taken 10 times.
✓ Branch 11 (2→57) taken 4 times.
✗ Branch 12 (2→58) not taken.
✓ Branch 13 (2→59) taken 13 times.
✓ Branch 14 (2→66) taken 1153 times.
✓ Branch 15 (2→67) taken 15 times.
✗ Branch 16 (2→74) not taken.
16420 switch (cmd->obj) {
6210 12609 case CMD_OBJ_TABLE:
6211
2/2
✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 12607 times.
12609 if (cmd->handle.table.name == NULL)
6212 2 return 0;
6213
6214 12607 table = table_cache_find(&ctx->nft->cache.table_cache,
6215 cmd->handle.table.name,
6216 cmd->handle.family);
6217
2/2
✓ Branch 0 (6→7) taken 458 times.
✓ Branch 1 (6→9) taken 12149 times.
12607 if (!table)
6218 458 return table_not_found(ctx);
6219
6220 12149 return 0;
6221 1025 case CMD_OBJ_SET:
6222 case CMD_OBJ_MAP:
6223 case CMD_OBJ_METER:
6224 1025 table = table_cache_find(&ctx->nft->cache.table_cache,
6225 cmd->handle.table.name,
6226 cmd->handle.family);
6227
2/2
✓ Branch 0 (11→12) taken 1 times.
✓ Branch 1 (11→14) taken 1024 times.
1025 if (!table)
6228 1 return table_not_found(ctx);
6229
6230 1024 set = set_cache_find(table, cmd->handle.set.name);
6231
2/2
✓ Branch 0 (15→16) taken 159 times.
✓ Branch 1 (15→18) taken 865 times.
1024 if (set == NULL)
6232 159 return set_not_found(ctx, &ctx->cmd->handle.set.location,
6233 159 ctx->cmd->handle.set.name);
6234
3/4
✓ Branch 0 (18→19) taken 642 times.
✓ Branch 1 (18→21) taken 223 times.
✓ Branch 2 (20→21) taken 642 times.
✗ Branch 3 (20→27) not taken.
865 if ((cmd->obj == CMD_OBJ_SET && !set_is_literal(set->flags)) ||
6235
4/4
✓ Branch 0 (21→22) taken 223 times.
✓ Branch 1 (21→24) taken 642 times.
✓ Branch 2 (23→24) taken 222 times.
✓ Branch 3 (23→27) taken 1 times.
865 (cmd->obj == CMD_OBJ_MAP && !map_is_literal(set->flags)) ||
6236
1/4
✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→30) taken 864 times.
✗ Branch 2 (26→27) not taken.
✗ Branch 3 (26→30) not taken.
864 (cmd->obj == CMD_OBJ_METER && !set_is_meter_compat(set->flags)))
6237 1 return cmd_error(ctx, &ctx->cmd->handle.set.location,
6238 "%s", strerror(ENOENT));
6239
6240 864 cmd->set = set_get(set);
6241 864 return 0;
6242 1278 case CMD_OBJ_CHAIN:
6243 1278 table = table_cache_find(&ctx->nft->cache.table_cache,
6244 cmd->handle.table.name,
6245 cmd->handle.family);
6246
1/2
✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→36) taken 1278 times.
1278 if (!table)
6247 ✗ return table_not_found(ctx);
6248
6249
2/2
✓ Branch 0 (37→38) taken 570 times.
✓ Branch 1 (37→40) taken 708 times.
1278 if (!chain_cache_find(table, cmd->handle.chain.name))
6250 570 return chain_not_found(ctx);
6251
6252 708 return 0;
6253 2 case CMD_OBJ_FLOWTABLE:
6254 2 table = table_cache_find(&ctx->nft->cache.table_cache,
6255 cmd->handle.table.name,
6256 cmd->handle.family);
6257
1/2
✗ Branch 0 (42→43) not taken.
✓ Branch 1 (42→45) taken 2 times.
2 if (!table)
6258 ✗ return table_not_found(ctx);
6259
6260 2 ft = ft_cache_find(table, cmd->handle.flowtable.name);
6261
1/2
✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→49) taken 2 times.
2 if (!ft)
6262 ✗ return flowtable_not_found(ctx, &ctx->cmd->handle.flowtable.location,
6263 ✗ ctx->cmd->handle.flowtable.name);
6264
6265 2 return 0;
6266 9 case CMD_OBJ_QUOTA:
6267 9 return cmd_evaluate_list_obj(ctx, cmd, NFT_OBJECT_QUOTA);
6268 280 case CMD_OBJ_COUNTER:
6269 280 return cmd_evaluate_list_obj(ctx, cmd, NFT_OBJECT_COUNTER);
6270 4 case CMD_OBJ_CT_HELPER:
6271 4 return cmd_evaluate_list_obj(ctx, cmd, NFT_OBJECT_CT_HELPER);
6272 10 case CMD_OBJ_CT_TIMEOUT:
6273 10 return cmd_evaluate_list_obj(ctx, cmd, NFT_OBJECT_CT_TIMEOUT);
6274 8 case CMD_OBJ_LIMIT:
6275 8 return cmd_evaluate_list_obj(ctx, cmd, NFT_OBJECT_LIMIT);
6276 ✗ case CMD_OBJ_SECMARK:
6277 ✗ return cmd_evaluate_list_obj(ctx, cmd, NFT_OBJECT_SECMARK);
6278 10 case CMD_OBJ_CT_EXPECT:
6279 10 return cmd_evaluate_list_obj(ctx, cmd, NFT_OBJECT_CT_EXPECT);
6280 4 case CMD_OBJ_SYNPROXY:
6281 4 return cmd_evaluate_list_obj(ctx, cmd, NFT_OBJECT_SYNPROXY);
6282 ✗ case CMD_OBJ_TUNNEL:
6283 ✗ return cmd_evaluate_list_obj(ctx, cmd, NFT_OBJECT_TUNNEL);
6284 13 case CMD_OBJ_COUNTERS:
6285 case CMD_OBJ_QUOTAS:
6286 case CMD_OBJ_CT_HELPERS:
6287 case CMD_OBJ_LIMITS:
6288 case CMD_OBJ_SETS:
6289 case CMD_OBJ_FLOWTABLES:
6290 case CMD_OBJ_SECMARKS:
6291 case CMD_OBJ_SYNPROXYS:
6292 case CMD_OBJ_CT_TIMEOUTS:
6293 case CMD_OBJ_CT_EXPECTATIONS:
6294 case CMD_OBJ_TUNNELS:
6295
1/2
✓ Branch 0 (59→60) taken 13 times.
✗ Branch 1 (59→61) not taken.
13 if (cmd->handle.table.name == NULL)
6296 13 return 0;
6297 ✗ if (!table_cache_find(&ctx->nft->cache.table_cache,
6298 cmd->handle.table.name,
6299 cmd->handle.family))
6300 ✗ return table_not_found(ctx);
6301
6302 ✗ return 0;
6303 1153 case CMD_OBJ_CHAINS:
6304 case CMD_OBJ_RULESET:
6305 case CMD_OBJ_METERS:
6306 case CMD_OBJ_MAPS:
6307 1153 return 0;
6308 15 case CMD_OBJ_HOOKS:
6309
1/2
✗ Branch 0 (67→68) not taken.
✓ Branch 1 (67→73) taken 15 times.
15 if (cmd->handle.chain.name) {
6310 ✗ int hooknum = str2hooknum(cmd->handle.family, cmd->handle.chain.name);
6311
6312 ✗ if (hooknum == NF_INET_NUMHOOKS)
6313 ✗ return chain_not_found(ctx);
6314
6315 ✗ cmd->handle.chain_id = hooknum;
6316 }
6317 15 return 0;
6318 ✗ default:
6319 ✗ BUG("invalid command object type %u", cmd->obj);
6320 }
6321 }
6322
6323 917 static int cmd_evaluate_reset(struct eval_ctx *ctx, struct cmd *cmd)
6324 {
6325
3/4
✓ Branch 0 (2→3) taken 88 times.
✓ Branch 1 (2→10) taken 821 times.
✓ Branch 2 (2→11) taken 8 times.
✗ Branch 3 (2→12) not taken.
917 switch (cmd->obj) {
6326 88 case CMD_OBJ_COUNTER:
6327 case CMD_OBJ_QUOTA:
6328 case CMD_OBJ_COUNTERS:
6329 case CMD_OBJ_QUOTAS:
6330 case CMD_OBJ_RULES:
6331 case CMD_OBJ_RULE:
6332
2/2
✓ Branch 0 (3→4) taken 8 times.
✓ Branch 1 (3→5) taken 80 times.
88 if (cmd->handle.table.name == NULL)
6333 8 return 0;
6334
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 80 times.
80 if (!table_cache_find(&ctx->nft->cache.table_cache,
6335 cmd->handle.table.name,
6336 cmd->handle.family))
6337 ✗ return table_not_found(ctx);
6338
6339 80 return 0;
6340 821 case CMD_OBJ_ELEMENTS:
6341 821 return setelem_evaluate(ctx, cmd);
6342 8 case CMD_OBJ_TABLE:
6343 case CMD_OBJ_CHAIN:
6344 case CMD_OBJ_SET:
6345 case CMD_OBJ_MAP:
6346 8 return cmd_evaluate_list(ctx, cmd);
6347 ✗ default:
6348 ✗ BUG("invalid command object type %u", cmd->obj);
6349 }
6350 }
6351
6352 − static void __flush_set_cache(struct set *set)
6353 {
6354 − if (set->init != NULL) {
6355 − expr_free(set->init);
6356 − set->init = NULL;
6357 }
6358 − }
6359
6360 15690 static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd)
6361 {
6362 15690 struct cache *table_cache = &ctx->nft->cache.table_cache;
6363 struct table *table;
6364 struct set *set;
6365
6366
5/6
✓ Branch 0 (2→3) taken 1378 times.
✓ Branch 1 (2→4) taken 14103 times.
✓ Branch 2 (2→5) taken 195 times.
✓ Branch 3 (2→20) taken 12 times.
✓ Branch 4 (2→35) taken 2 times.
✗ Branch 5 (2→50) not taken.
15690 switch (cmd->obj) {
6367 1378 case CMD_OBJ_RULESET:
6368 1378 break;
6369 14103 case CMD_OBJ_TABLE:
6370 /* Flushing a table does not empty the sets in the table nor remove
6371 * any chains.
6372 */
6373 case CMD_OBJ_CHAIN:
6374 /* Chains don't hold sets */
6375 14103 break;
6376 195 case CMD_OBJ_SET:
6377 195 table = table_cache_find(table_cache, cmd->handle.table.name,
6378 cmd->handle.family);
6379
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 195 times.
195 if (!table)
6380 ✗ return table_not_found(ctx);
6381
6382 195 set = set_cache_find(table, cmd->handle.set.name);
6383
2/2
✓ Branch 0 (10→11) taken 1 times.
✓ Branch 1 (10→13) taken 194 times.
195 if (set == NULL)
6384 1 return set_not_found(ctx, &ctx->cmd->handle.set.location,
6385 1 ctx->cmd->handle.set.name);
6386
2/2
✓ Branch 0 (14→15) taken 1 times.
✓ Branch 1 (14→18) taken 193 times.
194 else if (!set_is_literal(set->flags))
6387 1 return cmd_error(ctx, &ctx->cmd->handle.set.location,
6388 "%s", strerror(ENOENT));
6389
6390 193 __flush_set_cache(set);
6391
6392 193 return 0;
6393 12 case CMD_OBJ_MAP:
6394 12 table = table_cache_find(table_cache, cmd->handle.table.name,
6395 cmd->handle.family);
6396
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→24) taken 12 times.
12 if (!table)
6397 ✗ return table_not_found(ctx);
6398
6399 12 set = set_cache_find(table, cmd->handle.set.name);
6400
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→28) taken 12 times.
12 if (set == NULL)
6401 ✗ return set_not_found(ctx, &ctx->cmd->handle.set.location,
6402 ✗ ctx->cmd->handle.set.name);
6403
2/2
✓ Branch 0 (29→30) taken 3 times.
✓ Branch 1 (29→33) taken 9 times.
12 else if (!map_is_literal(set->flags))
6404 3 return cmd_error(ctx, &ctx->cmd->handle.set.location,
6405 "%s", strerror(ENOENT));
6406
6407 9 __flush_set_cache(set);
6408
6409 9 return 0;
6410 2 case CMD_OBJ_METER:
6411 2 table = table_cache_find(table_cache, cmd->handle.table.name,
6412 cmd->handle.family);
6413
1/2
✗ Branch 0 (36→37) not taken.
✓ Branch 1 (36→39) taken 2 times.
2 if (!table)
6414 ✗ return table_not_found(ctx);
6415
6416 2 set = set_cache_find(table, cmd->handle.set.name);
6417
1/2
✗ Branch 0 (40→41) not taken.
✓ Branch 1 (40→43) taken 2 times.
2 if (set == NULL)
6418 ✗ return set_not_found(ctx, &ctx->cmd->handle.set.location,
6419 ✗ ctx->cmd->handle.set.name);
6420
1/2
✓ Branch 0 (44→45) taken 2 times.
✗ Branch 1 (44→48) not taken.
2 else if (!set_is_meter_compat(set->flags))
6421 2 return cmd_error(ctx, &ctx->cmd->handle.set.location,
6422 "%s", strerror(ENOENT));
6423
6424 ✗ __flush_set_cache(set);
6425
6426 ✗ return 0;
6427 ✗ default:
6428 ✗ BUG("invalid command object type %u", cmd->obj);
6429 }
6430 15481 return 0;
6431 }
6432
6433 4 static int cmd_evaluate_rename(struct eval_ctx *ctx, struct cmd *cmd)
6434 {
6435 struct table *table;
6436
6437
1/2
✓ Branch 0 (2→3) taken 4 times.
✗ Branch 1 (2→12) not taken.
4 switch (cmd->obj) {
6438 4 case CMD_OBJ_CHAIN:
6439 4 table = table_cache_find(&ctx->nft->cache.table_cache,
6440 cmd->handle.table.name,
6441 cmd->handle.family);
6442
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 4 times.
4 if (!table)
6443 ✗ return table_not_found(ctx);
6444
6445
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 4 times.
4 if (!chain_cache_find(table, ctx->cmd->handle.chain.name))
6446 ✗ return chain_not_found(ctx);
6447
6448 4 break;
6449 ✗ default:
6450 ✗ BUG("invalid command object type %u", cmd->obj);
6451 }
6452 4 return 0;
6453 }
6454
6455 static uint32_t monitor_flags[CMD_MONITOR_EVENT_MAX][CMD_MONITOR_OBJ_MAX] = {
6456 [CMD_MONITOR_EVENT_ANY] = {
6457 [CMD_MONITOR_OBJ_ANY] = 0xffffffff,
6458 [CMD_MONITOR_OBJ_TABLES] = (1 << NFT_MSG_NEWTABLE) |
6459 (1 << NFT_MSG_DELTABLE),
6460 [CMD_MONITOR_OBJ_CHAINS] = (1 << NFT_MSG_NEWCHAIN) |
6461 (1 << NFT_MSG_DELCHAIN),
6462 [CMD_MONITOR_OBJ_RULES] = (1 << NFT_MSG_NEWRULE) |
6463 (1 << NFT_MSG_DELRULE),
6464 [CMD_MONITOR_OBJ_SETS] = (1 << NFT_MSG_NEWSET) |
6465 (1 << NFT_MSG_DELSET),
6466 [CMD_MONITOR_OBJ_ELEMS] = (1 << NFT_MSG_NEWSETELEM) |
6467 (1 << NFT_MSG_DELSETELEM),
6468 [CMD_MONITOR_OBJ_RULESET] = (1 << NFT_MSG_NEWTABLE) |
6469 (1 << NFT_MSG_DELTABLE) |
6470 (1 << NFT_MSG_NEWCHAIN) |
6471 (1 << NFT_MSG_DELCHAIN) |
6472 (1 << NFT_MSG_NEWRULE) |
6473 (1 << NFT_MSG_DELRULE) |
6474 (1 << NFT_MSG_NEWSET) |
6475 (1 << NFT_MSG_DELSET) |
6476 (1 << NFT_MSG_NEWSETELEM) |
6477 (1 << NFT_MSG_DELSETELEM) |
6478 (1 << NFT_MSG_NEWOBJ) |
6479 (1 << NFT_MSG_DELOBJ),
6480 [CMD_MONITOR_OBJ_TRACE] = (1 << NFT_MSG_TRACE),
6481 },
6482 [CMD_MONITOR_EVENT_NEW] = {
6483 [CMD_MONITOR_OBJ_ANY] = (1 << NFT_MSG_NEWTABLE) |
6484 (1 << NFT_MSG_NEWCHAIN) |
6485 (1 << NFT_MSG_NEWRULE) |
6486 (1 << NFT_MSG_NEWSET) |
6487 (1 << NFT_MSG_NEWSETELEM),
6488 [CMD_MONITOR_OBJ_TABLES] = (1 << NFT_MSG_NEWTABLE),
6489 [CMD_MONITOR_OBJ_CHAINS] = (1 << NFT_MSG_NEWCHAIN),
6490 [CMD_MONITOR_OBJ_RULES] = (1 << NFT_MSG_NEWRULE),
6491 [CMD_MONITOR_OBJ_SETS] = (1 << NFT_MSG_NEWSET),
6492 [CMD_MONITOR_OBJ_ELEMS] = (1 << NFT_MSG_NEWSETELEM),
6493 [CMD_MONITOR_OBJ_RULESET] = (1 << NFT_MSG_NEWTABLE) |
6494 (1 << NFT_MSG_NEWCHAIN) |
6495 (1 << NFT_MSG_NEWRULE) |
6496 (1 << NFT_MSG_NEWSET) |
6497 (1 << NFT_MSG_NEWSETELEM) |
6498 (1 << NFT_MSG_NEWOBJ),
6499 [CMD_MONITOR_OBJ_TRACE] = 0,
6500 },
6501 [CMD_MONITOR_EVENT_DEL] = {
6502 [CMD_MONITOR_OBJ_ANY] = (1 << NFT_MSG_DELTABLE) |
6503 (1 << NFT_MSG_DELCHAIN) |
6504 (1 << NFT_MSG_DELRULE) |
6505 (1 << NFT_MSG_DELSET) |
6506 (1 << NFT_MSG_DELSETELEM),
6507 [CMD_MONITOR_OBJ_TABLES] = (1 << NFT_MSG_DELTABLE),
6508 [CMD_MONITOR_OBJ_CHAINS] = (1 << NFT_MSG_DELCHAIN),
6509 [CMD_MONITOR_OBJ_RULES] = (1 << NFT_MSG_DELRULE),
6510 [CMD_MONITOR_OBJ_SETS] = (1 << NFT_MSG_DELSET),
6511 [CMD_MONITOR_OBJ_ELEMS] = (1 << NFT_MSG_DELSETELEM),
6512 [CMD_MONITOR_OBJ_RULESET] = (1 << NFT_MSG_DELTABLE) |
6513 (1 << NFT_MSG_DELCHAIN) |
6514 (1 << NFT_MSG_DELRULE) |
6515 (1 << NFT_MSG_DELSET) |
6516 (1 << NFT_MSG_DELSETELEM) |
6517 (1 << NFT_MSG_DELOBJ),
6518 [CMD_MONITOR_OBJ_TRACE] = 0,
6519 },
6520 };
6521
6522 ✗ static int cmd_evaluate_monitor(struct eval_ctx *ctx, struct cmd *cmd)
6523 {
6524 ✗ uint32_t *monitor_event_flags = monitor_flags[cmd->monitor->event];
6525
6526 ✗ cmd->monitor->flags = monitor_event_flags[cmd->monitor->type];
6527 ✗ return 0;
6528 }
6529
6530 ✗ static int cmd_evaluate_export(struct eval_ctx *ctx, struct cmd *cmd)
6531 {
6532 ✗ if (cmd->markup->format == __NFT_OUTPUT_NOTSUPP)
6533 ✗ return cmd_error(ctx, &cmd->location,
6534 "this output type is not supported, use nft -j list ruleset for JSON support instead");
6535 ✗ else if (cmd->markup->format == NFTNL_OUTPUT_JSON)
6536 ✗ return cmd_error(ctx, &cmd->location,
6537 "JSON export is no longer supported, use 'nft -j list ruleset' instead");
6538
6539 ✗ return 0;
6540 }
6541
6542 ✗ static int cmd_evaluate_import(struct eval_ctx *ctx, struct cmd *cmd)
6543 {
6544 ✗ if (cmd->markup->format == __NFT_OUTPUT_NOTSUPP)
6545 ✗ return cmd_error(ctx, &cmd->location,
6546 "this output type not supported");
6547
6548 ✗ return 0;
6549 }
6550
6551 static const char * const cmd_op_name[] = {
6552 [CMD_INVALID] = "invalid",
6553 [CMD_ADD] = "add",
6554 [CMD_REPLACE] = "replace",
6555 [CMD_CREATE] = "create",
6556 [CMD_INSERT] = "insert",
6557 [CMD_DELETE] = "delete",
6558 [CMD_GET] = "get",
6559 [CMD_LIST] = "list",
6560 [CMD_FLUSH] = "flush",
6561 [CMD_RENAME] = "rename",
6562 [CMD_EXPORT] = "export",
6563 [CMD_MONITOR] = "monitor",
6564 [CMD_DESCRIBE] = "describe",
6565 [CMD_DESTROY] = "destroy",
6566 };
6567
6568 5 static const char *cmd_op_to_name(enum cmd_ops op)
6569 {
6570
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 5 times.
5 if (op >= array_size(cmd_op_name))
6571 ✗ return "unknown";
6572
6573 5 return cmd_op_name[op];
6574 }
6575
6576 184644 int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)
6577 {
6578
2/2
✓ Branch 0 (2→3) taken 5 times.
✓ Branch 1 (2→8) taken 184639 times.
184644 if (ctx->nft->debug_mask & NFT_DEBUG_EVALUATION) {
6579 struct error_record *erec;
6580
6581 5 erec = erec_create(EREC_INFORMATIONAL, &cmd->location,
6582 "Evaluate %s", cmd_op_to_name(cmd->op));
6583 5 erec_print(&ctx->nft->output, erec, ctx->nft->debug_mask);
6584 5 nft_print(&ctx->nft->output, "\n\n");
6585 5 erec_destroy(erec);
6586 }
6587
6588 184644 memset(&ctx->ectx, 0, sizeof(ctx->ectx));
6589
6590 184644 ctx->cmd = cmd;
6591
7/13
✓ Branch 0 (8→9) taken 147704 times.
✓ Branch 1 (8→10) taken 2853 times.
✓ Branch 2 (8→11) taken 1064 times.
✓ Branch 3 (8→12) taken 16412 times.
✓ Branch 4 (8→13) taken 917 times.
✓ Branch 5 (8→14) taken 15690 times.
✓ Branch 6 (8→15) taken 4 times.
✗ Branch 7 (8→16) not taken.
✗ Branch 8 (8→17) not taken.
✗ Branch 9 (8→18) not taken.
✗ Branch 10 (8→19) not taken.
✗ Branch 11 (8→20) not taken.
✗ Branch 12 (8→21) not taken.
184644 switch (cmd->op) {
6592 147704 case CMD_ADD:
6593 case CMD_REPLACE:
6594 case CMD_CREATE:
6595 case CMD_INSERT:
6596 147704 return cmd_evaluate_add(ctx, cmd);
6597 2853 case CMD_DELETE:
6598 case CMD_DESTROY:
6599 2853 return cmd_evaluate_delete(ctx, cmd);
6600 1064 case CMD_GET:
6601 1064 return cmd_evaluate_get(ctx, cmd);
6602 16412 case CMD_LIST:
6603 16412 return cmd_evaluate_list(ctx, cmd);
6604 917 case CMD_RESET:
6605 917 return cmd_evaluate_reset(ctx, cmd);
6606 15690 case CMD_FLUSH:
6607 15690 return cmd_evaluate_flush(ctx, cmd);
6608 4 case CMD_RENAME:
6609 4 return cmd_evaluate_rename(ctx, cmd);
6610 ✗ case CMD_EXPORT:
6611 ✗ return cmd_evaluate_export(ctx, cmd);
6612 ✗ case CMD_DESCRIBE:
6613 ✗ return 0;
6614 ✗ case CMD_MONITOR:
6615 ✗ return cmd_evaluate_monitor(ctx, cmd);
6616 ✗ case CMD_IMPORT:
6617 ✗ return cmd_evaluate_import(ctx, cmd);
6618 ✗ case CMD_INVALID:
6619 ✗ break;
6620 };
6621
6622 ✗ BUG("invalid command operation %u", cmd->op);
6623 }
6624