GCC Code Coverage Report


Directory: ./
File: src/trace.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 0 241 0.0%
Functions: 0 12 0.0%
Branches: 0 112 0.0%

Line Branch Exec Source
1 #include <nft.h>
2 #include <trace.h>
3
4 #include <libnftnl/trace.h>
5
6 #include <errno.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <inttypes.h>
10
11 #include <linux/netfilter/nfnetlink.h>
12 #include <linux/netfilter/nf_tables.h>
13 #include <linux/netfilter.h>
14
15 #include <nftables.h>
16 #include <mnl.h>
17 #include <parser.h>
18 #include <netlink.h>
19 #include <expression.h>
20 #include <statement.h>
21 #include <utils.h>
22
23 #define nft_mon_print(monh, ...) nft_print(&monh->ctx->nft->output, __VA_ARGS__)
24
25 ✗ static void trace_print_hdr(const struct nftnl_trace *nlt,
26 struct output_ctx *octx)
27 {
28 ✗ nft_print(octx, "trace id %08x %s ",
29 nftnl_trace_get_u32(nlt, NFTNL_TRACE_ID),
30 ✗ family2str(nftnl_trace_get_u32(nlt, NFTNL_TRACE_FAMILY)));
31 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_TABLE))
32 ✗ nft_print(octx, "%s ",
33 nftnl_trace_get_str(nlt, NFTNL_TRACE_TABLE));
34 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_CHAIN))
35 ✗ nft_print(octx, "%s ",
36 nftnl_trace_get_str(nlt, NFTNL_TRACE_CHAIN));
37 ✗ }
38
39 ✗ static void trace_print_expr(const struct nftnl_trace *nlt, unsigned int attr,
40 struct expr *lhs, struct output_ctx *octx)
41 {
42 struct expr *rhs, *rel;
43 const void *data;
44 uint32_t len;
45
46 ✗ data = nftnl_trace_get_data(nlt, attr, &len);
47 ✗ rhs = constant_expr_alloc(&netlink_location,
48 ✗ lhs->dtype, lhs->byteorder,
49 ✗ len * BITS_PER_BYTE, data);
50 ✗ rel = relational_expr_alloc(&netlink_location, OP_EQ, lhs, rhs);
51
52 ✗ expr_print(rel, octx);
53 ✗ nft_print(octx, " ");
54 ✗ expr_free(rel);
55 ✗ }
56
57 ✗ static void trace_print_verdict(const struct nftnl_trace *nlt,
58 struct output_ctx *octx)
59 {
60 ✗ struct expr *chain_expr = NULL;
61 ✗ const char *chain = NULL;
62 unsigned int verdict;
63 struct expr *expr;
64
65 ✗ verdict = nftnl_trace_get_u32(nlt, NFTNL_TRACE_VERDICT);
66 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_JUMP_TARGET)) {
67 ✗ chain = xstrdup(nftnl_trace_get_str(nlt, NFTNL_TRACE_JUMP_TARGET));
68 ✗ chain_expr = constant_expr_alloc(&netlink_location,
69 &string_type,
70 BYTEORDER_HOST_ENDIAN,
71 ✗ strlen(chain) * BITS_PER_BYTE,
72 chain);
73 }
74 ✗ expr = verdict_expr_alloc(&netlink_location, verdict, chain_expr);
75
76 ✗ nft_print(octx, "verdict ");
77 ✗ expr_print(expr, octx);
78 ✗ expr_free(expr);
79 ✗ }
80
81 ✗ static void trace_print_policy(const struct nftnl_trace *nlt,
82 struct output_ctx *octx)
83 {
84 unsigned int policy;
85 struct expr *expr;
86
87 ✗ policy = nftnl_trace_get_u32(nlt, NFTNL_TRACE_POLICY);
88
89 ✗ expr = verdict_expr_alloc(&netlink_location, policy, NULL);
90
91 ✗ nft_print(octx, "policy ");
92 ✗ expr_print(expr, octx);
93 ✗ expr_free(expr);
94 ✗ }
95
96 ✗ static struct rule *trace_lookup_rule(const struct nftnl_trace *nlt,
97 uint64_t rule_handle,
98 struct nft_cache *cache)
99 {
100 struct chain *chain;
101 struct table *table;
102 struct handle h;
103
104 ✗ h.family = nftnl_trace_get_u32(nlt, NFTNL_TRACE_FAMILY);
105 ✗ h.table.name = nftnl_trace_get_str(nlt, NFTNL_TRACE_TABLE);
106 ✗ h.chain.name = nftnl_trace_get_str(nlt, NFTNL_TRACE_CHAIN);
107
108 ✗ if (!h.table.name)
109 ✗ return NULL;
110
111 ✗ table = table_cache_find(&cache->table_cache, h.table.name, h.family);
112 ✗ if (!table)
113 ✗ return NULL;
114
115 ✗ chain = chain_cache_find(table, h.chain.name);
116 ✗ if (!chain)
117 ✗ return NULL;
118
119 ✗ return rule_lookup(chain, rule_handle);
120 }
121
122 ✗ static void trace_print_rule(const struct nftnl_trace *nlt,
123 struct output_ctx *octx, struct nft_cache *cache)
124 {
125 uint64_t rule_handle;
126 struct rule *rule;
127
128 ✗ rule_handle = nftnl_trace_get_u64(nlt, NFTNL_TRACE_RULE_HANDLE);
129 ✗ rule = trace_lookup_rule(nlt, rule_handle, cache);
130
131 ✗ trace_print_hdr(nlt, octx);
132
133 ✗ if (rule) {
134 ✗ nft_print(octx, "rule ");
135 ✗ rule_print(rule, octx);
136 } else {
137 ✗ nft_print(octx, "unknown rule handle %" PRIu64, rule_handle);
138 }
139
140 ✗ nft_print(octx, " (");
141 ✗ trace_print_verdict(nlt, octx);
142 ✗ nft_print(octx, ")\n");
143 ✗ }
144
145 ✗ static void trace_gen_stmts(struct list_head *stmts,
146 struct proto_ctx *ctx, struct payload_dep_ctx *pctx,
147 const struct nftnl_trace *nlt, unsigned int attr,
148 enum proto_bases base)
149 {
150 ✗ struct list_head unordered = LIST_HEAD_INIT(unordered);
151 struct list_head list;
152 struct expr *rel, *lhs, *rhs, *tmp, *nexpr;
153 struct stmt *stmt;
154 const struct proto_desc *desc;
155 const void *hdr;
156 uint32_t hlen;
157 unsigned int n;
158
159 ✗ if (!nftnl_trace_is_set(nlt, attr))
160 ✗ return;
161 ✗ hdr = nftnl_trace_get_data(nlt, attr, &hlen);
162
163 ✗ lhs = payload_expr_alloc(&netlink_location, NULL, 0);
164 ✗ payload_init_raw(lhs, base, 0, hlen * BITS_PER_BYTE);
165 ✗ rhs = constant_expr_alloc(&netlink_location,
166 &invalid_type, BYTEORDER_INVALID,
167 ✗ hlen * BITS_PER_BYTE, hdr);
168
169 ✗ restart:
170 ✗ init_list_head(&list);
171 ✗ payload_expr_expand(&list, lhs, ctx);
172 ✗ expr_free(lhs);
173
174 ✗ desc = NULL;
175 ✗ list_for_each_entry_safe(lhs, nexpr, &list, list) {
176 ✗ if (desc && desc != ctx->protocol[base].desc) {
177 /* Chained protocols */
178 ✗ lhs->payload.offset = 0;
179 ✗ if (ctx->protocol[base].desc == NULL)
180 ✗ break;
181 ✗ goto restart;
182 }
183
184 ✗ tmp = constant_expr_splice(rhs, lhs->len);
185 ✗ expr_set_type(tmp, lhs->dtype, lhs->byteorder);
186 ✗ if (tmp->byteorder == BYTEORDER_HOST_ENDIAN)
187 ✗ mpz_switch_byteorder(tmp->value, tmp->len / BITS_PER_BYTE);
188
189 /* Skip unknown and filtered expressions */
190 ✗ desc = lhs->payload.desc;
191 ✗ if (lhs->dtype == &invalid_type ||
192 ✗ lhs->payload.tmpl == &proto_unknown_template ||
193 ✗ desc->checksum_key == payload_hdr_field(lhs) ||
194 ✗ desc->format.filter & (1 << payload_hdr_field(lhs))) {
195 ✗ expr_free(lhs);
196 ✗ expr_free(tmp);
197 ✗ continue;
198 }
199
200 ✗ rel = relational_expr_alloc(&lhs->location, OP_EQ, lhs, tmp);
201 ✗ stmt = expr_stmt_alloc(&rel->location, rel);
202 ✗ list_add_tail(&stmt->list, &unordered);
203
204 ✗ desc = ctx->protocol[base].desc;
205 ✗ relational_expr_pctx_update(ctx, rel);
206 }
207
208 ✗ expr_free(rhs);
209
210 ✗ n = 0;
211 ✗ next:
212 ✗ list_for_each_entry(stmt, &unordered, list) {
213 ✗ enum proto_bases b = base;
214
215 ✗ rel = stmt->expr;
216 ✗ lhs = rel->left;
217
218 /* Move statements to result list in defined order */
219 ✗ desc = lhs->payload.desc;
220 ✗ if (desc->format.order[n] &&
221 ✗ desc->format.order[n] != payload_hdr_field(lhs))
222 ✗ continue;
223
224 ✗ list_move_tail(&stmt->list, stmts);
225 ✗ n++;
226
227 ✗ if (payload_is_stacked(desc, rel))
228 ✗ b--;
229
230 /* Don't strip 'icmp type' from payload dump. */
231 ✗ if (pctx->icmp_type == 0)
232 ✗ payload_dependency_kill(pctx, lhs, ctx->family);
233 ✗ if (lhs->flags & EXPR_F_PROTOCOL)
234 ✗ payload_dependency_store(pctx, stmt, b);
235
236 ✗ goto next;
237 }
238 }
239
240 ✗ static struct expr *trace_alloc_list(const struct datatype *dtype,
241 enum byteorder byteorder,
242 unsigned int len, const void *data)
243 {
244 struct expr *list_expr;
245 unsigned int i;
246 mpz_t value;
247 uint32_t v;
248
249 ✗ if (len != sizeof(v))
250 ✗ return constant_expr_alloc(&netlink_location,
251 dtype, byteorder,
252 len * BITS_PER_BYTE, data);
253
254 ✗ list_expr = list_expr_alloc(&netlink_location);
255
256 ✗ mpz_init2(value, 32);
257 ✗ mpz_import_data(value, data, byteorder, len);
258 ✗ v = mpz_get_uint32(value);
259 ✗ if (v == 0) {
260 ✗ mpz_clear(value);
261 ✗ expr_free(list_expr);
262 ✗ return NULL;
263 }
264
265 ✗ for (i = 0; i < 32; i++) {
266 ✗ uint32_t bitv = v & (1 << i);
267
268 ✗ if (bitv == 0)
269 ✗ continue;
270
271 ✗ list_expr_add(list_expr,
272 constant_expr_alloc(&netlink_location,
273 dtype, byteorder,
274 len * BITS_PER_BYTE,
275 &bitv));
276 }
277
278 ✗ mpz_clear(value);
279 ✗ return list_expr;
280 }
281
282 ✗ static void trace_print_ct_expr(const struct nftnl_trace *nlt, unsigned int attr,
283 enum nft_ct_keys key, struct output_ctx *octx)
284 {
285 struct expr *lhs, *rhs, *rel;
286 const void *data;
287 uint32_t len;
288
289 ✗ data = nftnl_trace_get_data(nlt, attr, &len);
290 ✗ lhs = ct_expr_alloc(&netlink_location, key, -1);
291
292 ✗ switch (key) {
293 ✗ case NFT_CT_STATUS:
294 ✗ rhs = trace_alloc_list(lhs->dtype, lhs->byteorder, len, data);
295 ✗ if (!rhs) {
296 ✗ expr_free(lhs);
297 ✗ return;
298 }
299 ✗ rel = binop_expr_alloc(&netlink_location, OP_IMPLICIT, lhs, rhs);
300 ✗ break;
301 ✗ case NFT_CT_DIRECTION:
302 case NFT_CT_STATE:
303 case NFT_CT_ID:
304 /* fallthrough */
305 default:
306 ✗ rhs = constant_expr_alloc(&netlink_location,
307 ✗ lhs->dtype, lhs->byteorder,
308 ✗ len * BITS_PER_BYTE, data);
309 ✗ rel = relational_expr_alloc(&netlink_location, OP_IMPLICIT, lhs, rhs);
310 ✗ break;
311 }
312
313 ✗ expr_print(rel, octx);
314 ✗ nft_print(octx, " ");
315 ✗ expr_free(rel);
316 }
317
318 ✗ static void trace_print_ct(const struct nftnl_trace *nlt,
319 struct output_ctx *octx)
320 {
321 ✗ bool ct = nftnl_trace_is_set(nlt, NFTNL_TRACE_CT_STATE);
322
323 ✗ if (!ct)
324 ✗ return;
325
326 ✗ trace_print_hdr(nlt, octx);
327
328 ✗ nft_print(octx, "conntrack: ");
329
330 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_CT_DIRECTION))
331 ✗ trace_print_ct_expr(nlt, NFTNL_TRACE_CT_DIRECTION,
332 NFT_CT_DIRECTION, octx);
333
334 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_CT_STATE))
335 ✗ trace_print_ct_expr(nlt, NFTNL_TRACE_CT_STATE,
336 NFT_CT_STATE, octx);
337
338 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_CT_STATUS))
339 ✗ trace_print_ct_expr(nlt, NFTNL_TRACE_CT_STATUS,
340 NFT_CT_STATUS, octx);
341
342 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_CT_ID))
343 ✗ trace_print_ct_expr(nlt, NFTNL_TRACE_CT_ID,
344 NFT_CT_ID, octx);
345
346 ✗ nft_print(octx, "\n");
347 }
348
349 ✗ static void trace_print_packet(const struct nftnl_trace *nlt,
350 struct output_ctx *octx)
351 {
352 ✗ struct list_head stmts = LIST_HEAD_INIT(stmts);
353 const struct proto_desc *ll_desc;
354 ✗ struct payload_dep_ctx pctx = {};
355 struct proto_ctx ctx;
356 uint16_t dev_type;
357 uint32_t nfproto;
358 struct stmt *stmt, *next;
359
360 ✗ trace_print_ct(nlt, octx);
361 ✗ trace_print_hdr(nlt, octx);
362
363 ✗ nft_print(octx, "packet: ");
364 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_IIF))
365 ✗ trace_print_expr(nlt, NFTNL_TRACE_IIF,
366 meta_expr_alloc(&netlink_location,
367 NFT_META_IIF), octx);
368 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_OIF))
369 ✗ trace_print_expr(nlt, NFTNL_TRACE_OIF,
370 meta_expr_alloc(&netlink_location,
371 NFT_META_OIF), octx);
372
373 ✗ proto_ctx_init(&ctx, nftnl_trace_get_u32(nlt, NFTNL_TRACE_FAMILY), 0, false);
374 ✗ ll_desc = ctx.protocol[PROTO_BASE_LL_HDR].desc;
375 ✗ if ((ll_desc == &proto_inet || ll_desc == &proto_netdev) &&
376 ✗ nftnl_trace_is_set(nlt, NFTNL_TRACE_NFPROTO)) {
377 ✗ nfproto = nftnl_trace_get_u32(nlt, NFTNL_TRACE_NFPROTO);
378
379 ✗ proto_ctx_update(&ctx, PROTO_BASE_LL_HDR, &netlink_location, NULL);
380 ✗ proto_ctx_update(&ctx, PROTO_BASE_NETWORK_HDR, &netlink_location,
381 proto_find_upper(ll_desc, nfproto));
382 }
383 ✗ if (ctx.protocol[PROTO_BASE_LL_HDR].desc == NULL &&
384 ✗ nftnl_trace_is_set(nlt, NFTNL_TRACE_IIFTYPE)) {
385 ✗ dev_type = nftnl_trace_get_u16(nlt, NFTNL_TRACE_IIFTYPE);
386 ✗ proto_ctx_update(&ctx, PROTO_BASE_LL_HDR, &netlink_location,
387 proto_dev_desc(dev_type));
388 }
389
390 ✗ trace_gen_stmts(&stmts, &ctx, &pctx, nlt, NFTNL_TRACE_LL_HEADER,
391 PROTO_BASE_LL_HDR);
392 ✗ trace_gen_stmts(&stmts, &ctx, &pctx, nlt, NFTNL_TRACE_NETWORK_HEADER,
393 PROTO_BASE_NETWORK_HDR);
394 ✗ trace_gen_stmts(&stmts, &ctx, &pctx, nlt, NFTNL_TRACE_TRANSPORT_HEADER,
395 PROTO_BASE_TRANSPORT_HDR);
396
397 ✗ list_for_each_entry_safe(stmt, next, &stmts, list) {
398 ✗ stmt_print(stmt, octx);
399 ✗ nft_print(octx, " ");
400 ✗ stmt_free(stmt);
401 }
402 ✗ nft_print(octx, "\n");
403 ✗ }
404
405 ✗ int netlink_events_trace_cb(const struct nlmsghdr *nlh, int type,
406 struct netlink_mon_handler *monh)
407 {
408 struct nftnl_trace *nlt;
409
410 ✗ assert(type == NFT_MSG_TRACE);
411
412 ✗ nlt = nftnl_trace_alloc();
413 ✗ if (!nlt)
414 ✗ memory_allocation_error();
415
416 ✗ if (nftnl_trace_nlmsg_parse(nlh, nlt) < 0)
417 ✗ netlink_abi_error();
418
419 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_LL_HEADER) ||
420 ✗ nftnl_trace_is_set(nlt, NFTNL_TRACE_NETWORK_HEADER))
421 ✗ trace_print_packet(nlt, &monh->ctx->nft->output);
422
423 ✗ switch (nftnl_trace_get_u32(nlt, NFTNL_TRACE_TYPE)) {
424 ✗ case NFT_TRACETYPE_RULE:
425 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_RULE_HANDLE))
426 ✗ trace_print_rule(nlt, &monh->ctx->nft->output,
427 ✗ &monh->ctx->nft->cache);
428 ✗ break;
429 ✗ case NFT_TRACETYPE_POLICY:
430 ✗ trace_print_hdr(nlt, &monh->ctx->nft->output);
431
432 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_POLICY)) {
433 ✗ trace_print_policy(nlt, &monh->ctx->nft->output);
434 ✗ nft_mon_print(monh, " ");
435 }
436
437 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_MARK))
438 ✗ trace_print_expr(nlt, NFTNL_TRACE_MARK,
439 meta_expr_alloc(&netlink_location,
440 NFT_META_MARK),
441 ✗ &monh->ctx->nft->output);
442 ✗ nft_mon_print(monh, "\n");
443 ✗ break;
444 ✗ case NFT_TRACETYPE_RETURN:
445 ✗ trace_print_hdr(nlt, &monh->ctx->nft->output);
446
447 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_VERDICT)) {
448 ✗ trace_print_verdict(nlt, &monh->ctx->nft->output);
449 ✗ nft_mon_print(monh, " ");
450 }
451
452 ✗ if (nftnl_trace_is_set(nlt, NFTNL_TRACE_MARK))
453 ✗ trace_print_expr(nlt, NFTNL_TRACE_MARK,
454 meta_expr_alloc(&netlink_location,
455 NFT_META_MARK),
456 ✗ &monh->ctx->nft->output);
457 ✗ nft_mon_print(monh, "\n");
458 ✗ break;
459 }
460
461 ✗ nftnl_trace_free(nlt);
462 ✗ return MNL_CB_OK;
463 }
464