GCC Code Coverage Report


Directory: ./
File: include/expression.h
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 8 8 100.0%
Functions: 3 3 100.0%
Branches: 2 2 100.0%

Line Branch Exec Source
1 #ifndef NFTABLES_EXPRESSION_H
2 #define NFTABLES_EXPRESSION_H
3
4 #include <gmputil.h>
5 #include <linux/netfilter/nf_tables.h>
6
7 #include <nftables.h>
8 #include <datatype.h>
9 #include <utils.h>
10 #include <list.h>
11 #include <json.h>
12 #include <libnftnl/udata.h>
13
14 #define NFT_MAX_EXPR_LEN_BYTES (NFT_REG32_COUNT * sizeof(uint32_t))
15 #define NFT_MAX_EXPR_LEN_BITS (NFT_MAX_EXPR_LEN_BYTES * BITS_PER_BYTE)
16 #define NFT_MAX_EXPR_RECURSION 16
17
18 /**
19 * enum expr_types
20 *
21 * @EXPR_INVALID: uninitialized type, should not happen
22 * @EXPR_VERDICT: nftables verdict expression
23 * @EXPR_SYMBOL: unparsed symbol
24 * @EXPR_VARIABLE: variable
25 * @EXPR_VALUE: literal numeric or string expression
26 * @EXPR_PREFIX: prefixed expression
27 * @EXPR_RANGE: literal range
28 * @EXPR_PAYLOAD: payload expression
29 * @EXPR_EXTHDR: exthdr expression
30 * @EXPR_META: meta expression
31 * @EXPR_SOCKET: socket expression
32 * @EXPR_OSF: osf expression
33 * @EXPR_CT: conntrack expression
34 * @EXPR_CONCAT: concatenation
35 * @EXPR_LIST: list of expressions
36 * @EXPR_SET: literal set
37 * @EXPR_SET_REF: set reference
38 * @EXPR_SET_ELEM: set element
39 * @EXPR_MAPPING: a single mapping (key : value)
40 * @EXPR_MAP: map operation (expr map { EXPR_MAPPING, ... })
41 * @EXPR_UNARY: byteorder conversion, generated during evaluation
42 * @EXPR_BINOP: binary operations (bitwise, shifts)
43 * @EXPR_RELATIONAL: equality and relational expressions
44 * @EXPR_NUMGEN: number generation expression
45 * @EXPR_HASH: hash expression
46 * @EXPR_RT: routing expression
47 * @EXPR_FIB forward information base expression
48 * @EXPR_XFRM XFRM (ipsec) expression
49 * @EXPR_SET_ELEM_CATCHALL catchall element expression
50 * @EXPR_RANGE_VALUE constant range expression
51 * @EXPR_RANGE_SYMBOL unparse symbol range expression
52 */
53 enum expr_types {
54 EXPR_INVALID,
55 EXPR_VERDICT,
56 EXPR_SYMBOL,
57 EXPR_VARIABLE,
58 EXPR_VALUE,
59 EXPR_PREFIX,
60 EXPR_RANGE,
61 EXPR_PAYLOAD,
62 EXPR_EXTHDR,
63 EXPR_META,
64 EXPR_SOCKET,
65 EXPR_OSF,
66 EXPR_CT,
67 EXPR_CONCAT,
68 EXPR_LIST,
69 EXPR_SET,
70 EXPR_SET_REF,
71 EXPR_SET_ELEM,
72 EXPR_MAPPING,
73 EXPR_MAP,
74 EXPR_UNARY,
75 EXPR_BINOP,
76 EXPR_RELATIONAL,
77 EXPR_NUMGEN,
78 EXPR_HASH,
79 EXPR_RT,
80 EXPR_TUNNEL,
81 EXPR_FIB,
82 EXPR_XFRM,
83 EXPR_SET_ELEM_CATCHALL,
84 EXPR_RANGE_VALUE,
85 EXPR_RANGE_SYMBOL,
86 __EXPR_MAX
87 };
88 #define EXPR_MAX (__EXPR_MAX - 1)
89
90 enum ops {
91 OP_INVALID,
92 OP_IMPLICIT,
93 /* Unary operations */
94 OP_HTON,
95 OP_NTOH,
96 /* Binary operations */
97 OP_LSHIFT,
98 OP_RSHIFT,
99 OP_AND,
100 OP_XOR,
101 OP_OR,
102 /* Relational operations */
103 OP_EQ,
104 OP_NEQ,
105 OP_LT,
106 OP_GT,
107 OP_LTE,
108 OP_GTE,
109 OP_NEG,
110 __OP_MAX
111 };
112 #define OP_MAX (__OP_MAX - 1)
113
114 extern const char *expr_op_symbols[];
115
116 enum symbol_types {
117 SYMBOL_VALUE,
118 SYMBOL_SET,
119 };
120
121 /**
122 * struct expr_ctx - type context for symbol parsing during evaluation
123 *
124 * @dtype: expected datatype
125 * @byteorder: expected byteorder
126 * @len: expected len
127 * @maxval: expected maximum value
128 */
129 struct expr_ctx {
130 /* expr_ctx does not own the reference to dtype. The caller must ensure
131 * the valid lifetime.
132 */
133 const struct datatype *dtype;
134
135 enum byteorder byteorder;
136 unsigned int len;
137 unsigned int maxval;
138 const struct expr *key;
139 };
140
141 − static inline void __expr_set_context(struct expr_ctx *ctx,
142 const struct datatype *dtype,
143 enum byteorder byteorder,
144 unsigned int len, unsigned int maxval)
145 {
146 − ctx->dtype = dtype;
147 − ctx->byteorder = byteorder;
148 − ctx->len = len;
149 − ctx->maxval = maxval;
150 − ctx->key = NULL;
151 − }
152
153 297337 static inline void expr_set_context(struct expr_ctx *ctx,
154 const struct datatype *dtype,
155 unsigned int len)
156 {
157
2/2
✓ Branch 0 (2→3) taken 294582 times.
✓ Branch 1 (2→4) taken 2755 times.
591919 __expr_set_context(ctx, dtype,
158 294582 dtype ? dtype->byteorder : BYTEORDER_INVALID,
159 len, 0);
160 297337 }
161
162 /**
163 * struct expr_ops
164 *
165 * @type: expression type
166 * @name: expression name for diagnostics
167 * @clone: function to clone type specific data
168 * @destroy: destructor, must release inner expressions
169 * @set_type: function to promote type and byteorder of inner types
170 * @print: function to print the expression
171 * @cmp: function to compare two expressions of the same types
172 * @pctx_update:update protocol context
173 */
174 struct proto_ctx;
175 struct expr_ops {
176 enum expr_types type;
177 const char *name;
178 void (*clone)(struct expr *new, const struct expr *expr);
179 void (*destroy)(struct expr *expr);
180 void (*set_type)(const struct expr *expr,
181 const struct datatype *dtype,
182 enum byteorder byteorder);
183 void (*print)(const struct expr *expr,
184 struct output_ctx *octx);
185 json_t *(*json)(const struct expr *expr,
186 struct output_ctx *octx);
187 bool (*cmp)(const struct expr *e1,
188 const struct expr *e2);
189 void (*pctx_update)(struct proto_ctx *ctx,
190 const struct location *loc,
191 const struct expr *left,
192 const struct expr *right);
193 int (*build_udata)(struct nftnl_udata_buf *udbuf,
194 const struct expr *expr);
195 struct expr * (*parse_udata)(const struct nftnl_udata *ud);
196 };
197
198 const struct expr_ops *expr_ops(const struct expr *e);
199 const struct expr_ops *expr_ops_by_type_u32(uint32_t value);
200
201 /**
202 * enum expr_flags
203 *
204 * @EXPR_F_CONSTANT: constant expression
205 * @EXPR_F_SINGLETON: singleton (implies primary and constant)
206 * @EXPR_F_PROTOCOL: expressions describes upper layer protocol
207 * @EXPR_F_INTERVAL_END: set member ends an open interval
208 * @EXPR_F_BOOLEAN: expression is boolean (set by relational expr on LHS)
209 * @EXPR_F_INTERVAL: expression describes a interval
210 * @EXPR_F_KERNEL: expression resides in the kernel
211 */
212 enum expr_flags {
213 EXPR_F_CONSTANT = 0x1,
214 EXPR_F_SINGLETON = 0x2,
215 EXPR_F_PROTOCOL = 0x4,
216 EXPR_F_INTERVAL_END = 0x8,
217 EXPR_F_BOOLEAN = 0x10,
218 EXPR_F_INTERVAL = 0x20,
219 EXPR_F_KERNEL = 0x40,
220 EXPR_F_REMOVE = 0x80,
221 EXPR_F_INTERVAL_OPEN = 0x100,
222 };
223
224 #include <payload.h>
225 #include <exthdr.h>
226 #include <fib.h>
227 #include <numgen.h>
228 #include <meta.h>
229 #include <rt.h>
230 #include <hash.h>
231 #include <ct.h>
232 #include <socket.h>
233 #include <tunnel.h>
234 #include <osf.h>
235 #include <xfrm.h>
236
237 /**
238 * struct expr
239 *
240 * @list: list node
241 * @location: location from parser
242 * @refcnt: reference count
243 * @flags: mask of enum expr_flags
244 * @dtype: data type of expression
245 * @byteorder: byteorder of expression
246 * @etype: expression type
247 * @op: operation for unary, binary and relational expressions
248 * @len: length of expression
249 * @union: type specific data
250 */
251 struct expr {
252 struct list_head list;
253 struct location location;
254
255 unsigned int refcnt;
256 unsigned int flags;
257
258 const struct datatype *dtype;
259 enum byteorder byteorder:8;
260 enum expr_types etype:8;
261 enum ops op:8;
262 unsigned int len;
263
264 union {
265 struct {
266 /* EXPR_SYMBOL, EXPR_RANGE_SYMBOL */
267 const struct scope *scope;
268 union {
269 const char *identifier;
270 const char *identifier_range[2];
271 };
272 enum symbol_types symtype;
273 };
274 struct {
275 /* EXPR_VARIABLE */
276 struct symbol *sym;
277 };
278 struct {
279 /* EXPR_VERDICT */
280 int verdict;
281 struct expr *chain;
282 uint32_t chain_id;
283 };
284 struct {
285 /* EXPR_VALUE */
286 mpz_t value;
287 };
288 struct {
289 /* EXPR_RANGE_VALUE */
290 mpz_t low;
291 mpz_t high;
292 } range;
293 struct {
294 /* EXPR_PREFIX */
295 struct expr *prefix;
296 unsigned int prefix_len;
297 };
298 struct expr_concat {
299 /* EXPR_CONCAT */
300 struct list_head expressions;
301 unsigned int size;
302 uint8_t field_len[NFT_REG32_COUNT];
303 uint8_t field_count;
304 } expr_concat;
305 struct expr_set {
306 /* EXPR_SET */
307 struct list_head expressions;
308 unsigned int size;
309 uint32_t set_flags;
310 } expr_set;
311 struct expr_list {
312 /* EXPR_LIST */
313 struct list_head expressions;
314 unsigned int size;
315 } expr_list;
316 struct {
317 /* EXPR_SET_REF */
318 struct set *set;
319 };
320 struct {
321 /* EXPR_SET_ELEM */
322 struct expr *key;
323 uint64_t timeout;
324 uint64_t expiration;
325 const char *comment;
326 struct list_head stmt_list;
327 };
328 struct {
329 /* EXPR_UNARY */
330 struct expr *arg;
331 };
332 struct {
333 /* EXPR_RANGE, EXPR_BINOP, EXPR_MAPPING, EXPR_RELATIONAL */
334 struct expr *left;
335 struct expr *right;
336 };
337 struct {
338 /* EXPR_MAP */
339 struct expr *map;
340 struct expr *mappings;
341 };
342
343 struct {
344 /* EXPR_PAYLOAD */
345 const struct proto_desc *desc;
346 const struct proto_hdr_template *tmpl;
347 const struct proto_desc *inner_desc;
348 enum proto_bases base;
349 unsigned int offset;
350 bool is_raw;
351 bool evaluated;
352 } payload;
353 struct {
354 /* EXPR_EXTHDR */
355 const struct exthdr_desc *desc;
356 const struct proto_hdr_template *tmpl;
357 uint16_t offset;
358 uint8_t raw_type;
359 enum nft_exthdr_op op;
360 unsigned int flags;
361 } exthdr;
362 struct {
363 /* EXPR_META */
364 enum nft_meta_keys key;
365 enum proto_bases base;
366 const struct proto_desc *inner_desc;
367 } meta;
368 struct {
369 /* SOCKET */
370 enum nft_socket_keys key;
371 uint32_t level;
372 } socket;
373 struct {
374 /* EXPR_TUNNEL */
375 enum nft_tunnel_keys key;
376 } tunnel;
377 struct {
378 /* EXPR_RT */
379 enum nft_rt_keys key;
380 } rt;
381 struct {
382 /* EXPR_CT */
383 enum nft_ct_keys key;
384 enum proto_bases base;
385 int8_t direction;
386 uint8_t nfproto;
387 } ct;
388 struct {
389 /* EXPR_NUMGEN */
390 enum nft_ng_types type;
391 uint32_t mod;
392 uint32_t offset;
393 } numgen;
394 struct {
395 /* EXPR_HASH */
396 struct expr *expr;
397 uint32_t mod;
398 bool seed_set;
399 uint32_t seed;
400 uint32_t offset;
401 enum nft_hash_types type;
402 } hash;
403 struct {
404 /* EXPR_FIB */
405 uint32_t flags;
406 uint32_t result;
407 } fib;
408 struct {
409 /* EXPR_XFRM */
410 enum nft_xfrm_keys key;
411 uint8_t direction;
412 uint8_t spnum;
413 } xfrm;
414 struct {
415 /* EXPR_OSF */
416 uint8_t ttl;
417 uint32_t flags;
418 } osf;
419 };
420 };
421
422 #define expr_set(__expr) (assert((__expr)->etype == EXPR_SET), &(__expr)->expr_set)
423 #define expr_concat(__expr) (assert((__expr)->etype == EXPR_CONCAT), &(__expr)->expr_concat)
424 #define expr_list(__expr) (assert((__expr)->etype == EXPR_LIST), &(__expr)->expr_list)
425
426 extern struct expr *expr_alloc(const struct location *loc,
427 enum expr_types etype,
428 const struct datatype *dtype,
429 enum byteorder byteorder, unsigned int len);
430 extern struct expr *expr_clone(const struct expr *expr);
431 extern struct expr *expr_get(struct expr *expr);
432 extern void expr_free(struct expr *expr);
433 extern void expr_print(const struct expr *expr, struct output_ctx *octx);
434 extern bool expr_cmp(const struct expr *e1, const struct expr *e2);
435 extern void expr_describe(const struct expr *expr, struct output_ctx *octx);
436
437 extern const struct datatype *expr_basetype(const struct expr *expr);
438 extern void expr_set_type(struct expr *expr, const struct datatype *dtype,
439 enum byteorder byteorder);
440
441 struct eval_ctx;
442 extern int expr_binary_error(struct list_head *msgs,
443 const struct expr *e1, const struct expr *e2,
444 const char *fmt, ...) __fmtstring(4, 5);
445
446 #define expr_error(msgs, expr, fmt, args...) \
447 expr_binary_error(msgs, expr, NULL, fmt, ## args)
448
449 5385040 static inline bool expr_is_constant(const struct expr *expr)
450 {
451 5385040 return expr->flags & EXPR_F_CONSTANT ? true : false;
452 }
453
454 2531671 static inline bool expr_is_singleton(const struct expr *expr)
455 {
456 2531671 return expr->flags & EXPR_F_SINGLETON ? true : false;
457 }
458
459 extern struct expr *unary_expr_alloc(const struct location *loc,
460 enum ops op, struct expr *arg);
461
462 extern struct expr *binop_expr_alloc(const struct location *loc, enum ops op,
463 struct expr *left, struct expr *right);
464
465 extern bool must_print_eq_op(const struct expr *expr);
466
467 extern struct expr *relational_expr_alloc(const struct location *loc, enum ops op,
468 struct expr *left, struct expr *right);
469
470 extern void relational_expr_pctx_update(struct proto_ctx *ctx,
471 const struct expr *expr);
472
473 extern struct expr *verdict_expr_alloc(const struct location *loc,
474 int verdict, struct expr *chain);
475
476 extern struct expr *symbol_expr_alloc(const struct location *loc,
477 enum symbol_types type, struct scope *scope,
478 const char *identifier);
479 #define is_symbol_value_expr(expr) \
480 ((expr)->etype == EXPR_SYMBOL && (expr)->symtype == SYMBOL_VALUE)
481
482 const char *expr_name(const struct expr *e);
483
484 static inline void symbol_expr_set_type(struct expr *expr,
485 const struct datatype *dtype)
486 {
487 if (expr->etype == EXPR_SYMBOL)
488 datatype_set(expr, dtype);
489 }
490
491 struct expr *variable_expr_alloc(const struct location *loc,
492 struct scope *scope, struct symbol *sym);
493
494 extern struct expr *constant_expr_alloc(const struct location *loc,
495 const struct datatype *dtype,
496 enum byteorder byteorder,
497 unsigned int len, const void *data);
498 extern struct expr *constant_expr_join(const struct expr *e1,
499 const struct expr *e2);
500 extern struct expr *constant_expr_splice(struct expr *expr, unsigned int len);
501
502 extern struct expr *constant_range_expr_alloc(const struct location *loc,
503 const struct datatype *dtype,
504 enum byteorder byteorder,
505 unsigned int len,
506 mpz_t low, mpz_t high);
507
508 struct expr *symbol_range_expr_alloc(const struct location *loc,
509 enum symbol_types type, const struct scope *scope,
510 const char *identifier_low, const char *identifier_high);
511
512 extern struct expr *flag_expr_alloc(const struct location *loc,
513 const struct datatype *dtype,
514 enum byteorder byteorder,
515 unsigned int len, unsigned long n);
516 extern struct expr *bitmask_expr_to_binops(struct expr *expr);
517
518 extern struct expr *prefix_expr_alloc(const struct location *loc,
519 struct expr *expr,
520 unsigned int prefix_len);
521
522 extern struct expr *range_expr_alloc(const struct location *loc,
523 struct expr *low, struct expr *high);
524 struct expr *range_expr_to_prefix(struct expr *range);
525
526 extern void list_expr_sort(struct list_head *head);
527 extern void list_splice_sorted(struct list_head *list, struct list_head *head);
528
529 extern struct expr *concat_expr_alloc(const struct location *loc);
530 void concat_expr_add(struct expr *concat, struct expr *item);
531 void concat_expr_remove(struct expr *concat, struct expr *expr);
532
533 extern struct expr *list_expr_alloc(const struct location *loc);
534 void list_expr_add(struct expr *expr, struct expr *item);
535 void list_expr_remove(struct expr *expr, struct expr *item);
536 struct expr *list_expr_to_binop(struct expr *expr);
537
538 extern struct expr *set_expr_alloc(const struct location *loc,
539 const struct set *set);
540 void __set_expr_add(struct expr *set, struct expr *elem);
541 void set_expr_add(struct expr *set, struct expr *elem);
542 void set_expr_remove(struct expr *expr, struct expr *item);
543
544 extern void concat_range_aggregate(struct expr *set);
545 extern void interval_map_decompose(struct expr *set);
546
547 extern struct expr *get_set_intervals(const struct set *set,
548 const struct expr *init);
549 struct table;
550 extern int get_set_decompose(struct set *cache_set, struct set *set);
551
552 extern struct expr *mapping_expr_alloc(const struct location *loc,
553 struct expr *from, struct expr *to);
554 extern struct expr *map_expr_alloc(const struct location *loc,
555 struct expr *arg, struct expr *list);
556
557 extern struct expr *set_ref_expr_alloc(const struct location *loc,
558 struct set *set);
559
560 extern struct expr *set_elem_expr_alloc(const struct location *loc,
561 struct expr *key);
562
563 struct expr *set_elem_catchall_expr_alloc(const struct location *loc);
564
565 #define expr_type_catchall(__expr) \
566 ((__expr)->etype == EXPR_SET_ELEM_CATCHALL)
567
568 extern void range_expr_value_low(mpz_t rop, const struct expr *expr);
569 extern void range_expr_value_high(mpz_t rop, const struct expr *expr);
570 void range_expr_swap_values(struct expr *range);
571
572 #endif /* NFTABLES_EXPRESSION_H */
573