GCC Code Coverage Report


Directory: ./
File: src/json.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 947 1173 80.7%
Functions: 99 109 90.8%
Branches: 415 595 69.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) Red Hat GmbH. Author: Phil Sutter <phil@nwl.cc>
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 (or any
6 * later) as published by the Free Software Foundation.
7 */
8
9 #include <nft.h>
10
11 #include <stdio.h>
12
13 #include <expression.h>
14 #include <list.h>
15 #include <netlink.h>
16 #include <rule.h>
17 #include <rt.h>
18 #include "nftutils.h"
19
20 #include <netdb.h>
21 #include <netinet/icmp6.h>
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
24 #include <netinet/ip_icmp.h>
25 #include <linux/netfilter.h>
26 #include <linux/netfilter/nf_log.h>
27 #include <linux/netfilter/nf_nat.h>
28 #include <linux/netfilter/nf_tables.h>
29 #include <linux/netfilter/nf_synproxy.h>
30 #include <linux/xfrm.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <jansson.h>
34 #include <syslog.h>
35
36 − static json_t *__nft_json_pack(unsigned int line, const char *fmt, ...)
37 {
38 json_error_t error;
39 json_t *value;
40 va_list ap;
41
42 − va_start(ap, fmt);
43 − value = json_vpack_ex(&error, 0, fmt, ap);
44 − va_end(ap);
45
46 − if (value)
47 − return value;
48
49 − fprintf(stderr, "%s:%d: json_pack failure (%s)\n", __FILE__, line, error.text);
50 − return NULL;
51 }
52 #define nft_json_pack(...) __nft_json_pack(__LINE__, __VA_ARGS__)
53
54 8012 static int json_array_extend_new(json_t *array, json_t *other_array)
55 {
56 int ret;
57
58 8012 ret = json_array_extend(array, other_array);
59 8012 json_decref(other_array);
60 8012 return ret;
61 }
62
63 8507 static void json_add_array_new(json_t *obj, const char *name, json_t *array)
64 {
65
2/2
✓ Branch 0 (3→4) taken 494 times.
✓ Branch 1 (3→5) taken 8013 times.
8507 if (json_array_size(array))
66 494 json_object_set_new(obj, name, array);
67 else
68 8013 json_decref(array);
69 8507 }
70
71 647771 static json_t *expr_print_json(const struct expr *expr, struct output_ctx *octx)
72 {
73 const struct expr_ops *ops;
74 char buf[1024];
75 FILE *fp;
76
77 647771 ops = expr_ops(expr);
78
1/2
✓ Branch 0 (3→4) taken 647771 times.
✗ Branch 1 (3→6) not taken.
647771 if (ops->json)
79 647771 return ops->json(expr, octx);
80
81 ✗ fprintf(stderr, "warning: expr ops %s have no json callback\n",
82 expr_name(expr));
83
84 ✗ fp = octx->output_fp;
85 ✗ octx->output_fp = fmemopen(buf, 1024, "w");
86
87 ✗ ops->print(expr, octx);
88
89 ✗ fclose(octx->output_fp);
90 ✗ octx->output_fp = fp;
91
92 ✗ return nft_json_pack("s", buf);
93 }
94
95 2349 static json_t *set_dtype_json(const struct expr *key)
96 {
97 2349 char *namedup = xstrdup(key->dtype->name), *tok;
98 2349 json_t *root = NULL;
99 char *tok_safe;
100
101 2349 tok = strtok_r(namedup, " .", &tok_safe);
102
2/2
✓ Branch 0 (13→5) taken 2871 times.
✓ Branch 1 (13→14) taken 2349 times.
5220 while (tok) {
103 2871 json_t *jtok = json_string(tok);
104
2/2
✓ Branch 0 (6→7) taken 2349 times.
✓ Branch 1 (6→8) taken 522 times.
2871 if (!root)
105 2349 root = jtok;
106
3/4
✓ Branch 0 (8→9) taken 522 times.
✗ Branch 1 (8→11) not taken.
✓ Branch 2 (9→10) taken 484 times.
✓ Branch 3 (9→11) taken 38 times.
522 else if (json_is_string(root))
107 484 root = nft_json_pack("[o, o]", root, jtok);
108 else
109 38 json_array_append_new(root, jtok);
110 2871 tok = strtok_r(NULL, " .", &tok_safe);
111 }
112 2349 free(namedup);
113 2349 return root;
114 }
115
116 1676 static json_t *set_key_dtype_json(const struct set *set,
117 struct output_ctx *octx)
118 {
119 1676 bool use_typeof = set->key_typeof_valid;
120
121
2/2
✓ Branch 0 (2→3) taken 1584 times.
✓ Branch 1 (2→5) taken 92 times.
1676 if (!use_typeof)
122 1584 return set_dtype_json(set->key);
123
124 92 return nft_json_pack("{s:o}", "typeof", expr_print_json(set->key, octx));
125 }
126
127 29152 static json_t *stmt_print_json(const struct stmt *stmt, struct output_ctx *octx)
128 {
129 29152 const struct stmt_ops *ops = stmt_ops(stmt);
130 char buf[1024];
131 FILE *fp;
132
133
2/2
✓ Branch 0 (3→4) taken 29140 times.
✓ Branch 1 (3→6) taken 12 times.
29152 if (ops->json)
134 29140 return ops->json(stmt, octx);
135
136 12 fprintf(stderr, "warning: stmt ops %s have no json callback\n",
137 12 ops->name);
138
139 12 fp = octx->output_fp;
140 12 octx->output_fp = fmemopen(buf, 1024, "w");
141
142 12 ops->print(stmt, octx);
143
144 12 fclose(octx->output_fp);
145 12 octx->output_fp = fp;
146
147 12 return nft_json_pack("s", buf);
148 }
149
150 37 static json_t *set_stmt_list_json(const struct list_head *stmt_list,
151 struct output_ctx *octx)
152 {
153 37 unsigned int flags = octx->flags;
154 json_t *root, *tmp;
155 struct stmt *i;
156
157 37 root = json_array();
158 37 octx->flags |= NFT_CTX_OUTPUT_STATELESS;
159
160
2/2
✓ Branch 0 (7→4) taken 40 times.
✓ Branch 1 (7→8) taken 37 times.
77 list_for_each_entry(i, stmt_list, list) {
161 40 tmp = stmt_print_json(i, octx);
162 40 json_array_append_new(root, tmp);
163 }
164 37 octx->flags = flags;
165
166 37 return root;
167 }
168
169 1676 static json_t *set_print_json(struct output_ctx *octx, const struct set *set)
170 {
171 1676 json_t *root, *tmp, *datatype_ext = NULL;
172 const char *type;
173
174
2/2
✓ Branch 0 (3→4) taken 765 times.
✓ Branch 1 (3→5) taken 911 times.
1676 if (set_is_datamap(set->flags)) {
175 765 type = "map";
176 765 datatype_ext = set_dtype_json(set->data);
177
2/2
✓ Branch 0 (6→7) taken 14 times.
✓ Branch 1 (6→9) taken 897 times.
911 } else if (set_is_objmap(set->flags)) {
178 14 type = "map";
179 14 datatype_ext = json_string(obj_type_name(set->objtype));
180
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 897 times.
897 } else if (set_is_meter(set->flags)) {
181 ✗ type = "meter";
182 } else {
183 897 type = "set";
184 }
185
186 1676 root = nft_json_pack("{s:s, s:s, s:s, s:o, s:I}",
187 "family", family2str(set->handle.family),
188 "name", set->handle.set.name,
189 "table", set->handle.table.name,
190 "type", set_key_dtype_json(set, octx),
191 "handle", set->handle.handle.id);
192
193
2/2
✓ Branch 0 (16→17) taken 2 times.
✓ Branch 1 (16→19) taken 1674 times.
1676 if (set->comment)
194 2 json_object_set_new(root, "comment", json_string(set->comment));
195
2/2
✓ Branch 0 (19→20) taken 779 times.
✓ Branch 1 (19→21) taken 897 times.
1676 if (datatype_ext)
196 779 json_object_set_new(root, "map", datatype_ext);
197
198
2/2
✓ Branch 0 (21→22) taken 1670 times.
✓ Branch 1 (21→29) taken 6 times.
1676 if (!(set->flags & (NFT_SET_CONSTANT))) {
199
1/2
✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→26) taken 1670 times.
1670 if (set->policy != NFT_SET_POL_PERFORMANCE) {
200 ✗ tmp = nft_json_pack("s", set_policy2str(set->policy));
201 ✗ json_object_set_new(root, "policy", tmp);
202 }
203
2/2
✓ Branch 0 (26→27) taken 147 times.
✓ Branch 1 (26→29) taken 1523 times.
1670 if (set->desc.size) {
204 147 tmp = nft_json_pack("i", set->desc.size);
205 147 json_object_set_new(root, "size", tmp);
206 }
207 }
208
209 1676 tmp = json_array();
210
2/2
✓ Branch 0 (30→31) taken 6 times.
✓ Branch 1 (30→33) taken 1670 times.
1676 if (set->flags & NFT_SET_CONSTANT)
211 6 json_array_append_new(tmp, nft_json_pack("s", "constant"));
212
2/2
✓ Branch 0 (33→34) taken 222 times.
✓ Branch 1 (33→36) taken 1454 times.
1676 if (set->flags & NFT_SET_INTERVAL)
213 222 json_array_append_new(tmp, nft_json_pack("s", "interval"));
214
2/2
✓ Branch 0 (36→37) taken 81 times.
✓ Branch 1 (36→39) taken 1595 times.
1676 if (set->flags & NFT_SET_TIMEOUT)
215 81 json_array_append_new(tmp, nft_json_pack("s", "timeout"));
216
2/2
✓ Branch 0 (39→40) taken 44 times.
✓ Branch 1 (39→42) taken 1632 times.
1676 if (set->flags & NFT_SET_EVAL)
217 44 json_array_append_new(tmp, nft_json_pack("s", "dynamic"));
218 1676 json_add_array_new(root, "flags", tmp);
219
220
2/2
✓ Branch 0 (43→44) taken 52 times.
✓ Branch 1 (43→46) taken 1624 times.
1676 if (set->timeout) {
221 52 tmp = json_integer(set->timeout / 1000);
222 52 json_object_set_new(root, "timeout", tmp);
223 }
224
2/2
✓ Branch 0 (46→47) taken 4 times.
✓ Branch 1 (46→49) taken 1672 times.
1676 if (set->gc_int) {
225 4 tmp = nft_json_pack("i", set->gc_int / 1000);
226 4 json_object_set_new(root, "gc-interval", tmp);
227 }
228
2/2
✓ Branch 0 (49→50) taken 7 times.
✓ Branch 1 (49→52) taken 1669 times.
1676 if (set->automerge)
229 7 json_object_set_new(root, "auto-merge", json_true());
230
231
7/8
✓ Branch 0 (53→54) taken 1675 times.
✓ Branch 1 (53→69) taken 1 times.
✓ Branch 2 (54→55) taken 1666 times.
✓ Branch 3 (54→69) taken 9 times.
✗ Branch 4 (55→56) not taken.
✓ Branch 5 (55→57) taken 1666 times.
✓ Branch 6 (57→58) taken 491 times.
✓ Branch 7 (57→69) taken 1175 times.
1676 if (!nft_output_terse(octx) && set->init && expr_set(set->init)->size > 0) {
232 491 json_t *array = json_array();
233 const struct expr *i;
234
235
4/6
✗ Branch 0 (59→60) not taken.
✓ Branch 1 (59→61) taken 491 times.
✗ Branch 2 (65→66) not taken.
✓ Branch 3 (65→67) taken 234633 times.
✓ Branch 4 (67→62) taken 234142 times.
✓ Branch 5 (67→68) taken 491 times.
234633 list_for_each_entry(i, &expr_set(set->init)->expressions, list)
236 234142 json_array_append_new(array, expr_print_json(i, octx));
237
238 491 json_object_set_new(root, "elem", array);
239 }
240
241
2/2
✓ Branch 0 (70→71) taken 27 times.
✓ Branch 1 (70→73) taken 1649 times.
1676 if (!list_empty(&set->stmt_list)) {
242 27 json_object_set_new(root, "stmt",
243 set_stmt_list_json(&set->stmt_list, octx));
244 }
245
246 1676 return nft_json_pack("{s:o}", type, root);
247 }
248
249 /* XXX: Merge with set_print_json()? */
250 19 static json_t *element_print_json(struct output_ctx *octx,
251 const struct set *set)
252 {
253 19 json_t *root = expr_print_json(set->init, octx);
254
255 19 return nft_json_pack("{s: {s:s, s:s, s:s, s:o}}", "element",
256 "family", family2str(set->handle.family),
257 "table", set->handle.table.name,
258 "name", set->handle.set.name,
259 "elem", root);
260 }
261
262 26914 static json_t *rule_print_json(struct output_ctx *octx,
263 const struct rule *rule)
264 {
265 const struct stmt *stmt;
266 json_t *root, *tmp;
267
268 26914 root = nft_json_pack("{s:s, s:s, s:s, s:I}",
269 "family", family2str(rule->handle.family),
270 "table", rule->handle.table.name,
271 "chain", rule->handle.chain.name,
272 "handle", rule->handle.handle.id);
273
2/2
✓ Branch 0 (4→5) taken 16 times.
✓ Branch 1 (4→7) taken 26898 times.
26914 if (rule->comment)
274 16 json_object_set_new(root, "comment",
275 16 json_string(rule->comment));
276
277 26914 tmp = json_array();
278
2/2
✓ Branch 0 (12→9) taken 29049 times.
✓ Branch 1 (12→13) taken 26914 times.
55963 list_for_each_entry(stmt, &rule->stmts, list)
279 29049 json_array_append_new(tmp, stmt_print_json(stmt, octx));
280
281
1/2
✓ Branch 0 (14→15) taken 26914 times.
✗ Branch 1 (14→16) not taken.
26914 if (json_array_size(tmp))
282 26914 json_object_set_new(root, "expr", tmp);
283 else {
284 ✗ fprintf(stderr, "rule without statements?!\n");
285 ✗ json_decref(tmp);
286 }
287
288 26914 return nft_json_pack("{s:o}", "rule", root);
289 }
290
291 8657 static json_t *chain_print_json(const struct chain *chain)
292 {
293 json_t *root;
294
295 8657 root = nft_json_pack("{s:s, s:s, s:s, s:I}",
296 "family", family2str(chain->handle.family),
297 "table", chain->handle.table.name,
298 "name", chain->handle.chain.name,
299 "handle", chain->handle.handle.id);
300
301
2/2
✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→7) taken 8656 times.
8657 if (chain->comment)
302 1 json_object_set_new(root, "comment", json_string(chain->comment));
303
304
2/2
✓ Branch 0 (7→8) taken 8008 times.
✓ Branch 1 (7→34) taken 649 times.
8657 if (chain->flags & CHAIN_F_BASECHAIN) {
305 8008 json_t *tmp = NULL, *devs = NULL;
306 8008 int priority = 0, policy, i;
307
308
2/2
✓ Branch 0 (8→9) taken 8007 times.
✓ Branch 1 (8→10) taken 1 times.
8008 if (chain->priority.expr)
309 8007 mpz_export_data(&priority, chain->priority.expr->value,
310 BYTEORDER_HOST_ENDIAN, sizeof(int));
311
312
2/2
✓ Branch 0 (10→11) taken 8006 times.
✓ Branch 1 (10→12) taken 2 times.
8008 if (chain->policy) {
313 8006 mpz_export_data(&policy, chain->policy->value,
314 BYTEORDER_HOST_ENDIAN, sizeof(int));
315 } else {
316 2 policy = NF_ACCEPT;
317 }
318
319
2/2
✓ Branch 0 (13→14) taken 8007 times.
✓ Branch 1 (13→17) taken 1 times.
8008 if (chain->type.str)
320 8007 tmp = nft_json_pack("{s:s, s:s, s:i, s:s}",
321 "type", chain->type.str,
322 "hook", hooknum2str(chain->handle.family,
323 chain->hook.num),
324 "prio", priority,
325 "policy", chain_policy2str(policy));
326 else
327 1 tmp = NULL;
328
329
2/2
✓ Branch 0 (27→19) taken 3302 times.
✓ Branch 1 (27→28) taken 8008 times.
11310 for (i = 0; i < chain->dev_array_len; i++) {
330 3302 const char *dev = chain->dev_array[i];
331
2/2
✓ Branch 0 (19→20) taken 3294 times.
✓ Branch 1 (19→21) taken 8 times.
3302 if (!devs)
332 3294 devs = json_string(dev);
333
3/4
✓ Branch 0 (21→22) taken 8 times.
✗ Branch 1 (21→24) not taken.
✓ Branch 2 (22→23) taken 6 times.
✓ Branch 3 (22→24) taken 2 times.
8 else if (json_is_string(devs))
334 6 devs = nft_json_pack("[o, s]", devs, dev);
335 else
336 2 json_array_append_new(devs, json_string(dev));
337 }
338
2/2
✓ Branch 0 (28→29) taken 3294 times.
✓ Branch 1 (28→30) taken 4714 times.
8008 if (devs)
339 3294 json_object_set_new(root, "dev", devs);
340
341
2/2
✓ Branch 0 (30→31) taken 8007 times.
✓ Branch 1 (30→33) taken 1 times.
8008 if (tmp) {
342 8007 json_object_update(root, tmp);
343 8007 json_decref(tmp);
344 }
345 }
346
347 8657 return nft_json_pack("{s:o}", "chain", root);
348 }
349
350 36 static json_t *proto_name_json(uint8_t proto)
351 {
352 char name[NFT_PROTONAME_MAXSIZE];
353
354
1/2
✓ Branch 0 (3→4) taken 36 times.
✗ Branch 1 (3→6) not taken.
36 if (nft_getprotobynumber(proto, name, sizeof(name)))
355 36 return json_string(name);
356 ✗ return json_integer(proto);
357 }
358
359 13 static json_t *timeout_policy_json(uint8_t l4, const uint32_t *timeout)
360 {
361 13 json_t *root = NULL;
362 unsigned int i;
363
364
2/2
✓ Branch 0 (10→3) taken 98 times.
✓ Branch 1 (10→11) taken 13 times.
111 for (i = 0; i < timeout_protocol[l4].array_size; i++) {
365
2/2
✓ Branch 0 (3→4) taken 72 times.
✓ Branch 1 (3→5) taken 26 times.
98 if (timeout[i] == timeout_protocol[l4].dflt_timeout[i])
366 72 continue;
367
368
2/2
✓ Branch 0 (5→6) taken 13 times.
✓ Branch 1 (5→7) taken 13 times.
26 if (!root)
369 13 root = json_object();
370 26 json_object_set_new(root, timeout_protocol[l4].state_to_name[i],
371 26 json_integer(timeout[i]));
372 }
373
1/2
✓ Branch 0 (11→12) taken 13 times.
✗ Branch 1 (11→13) not taken.
13 return root ? : json_null();
374 }
375
376 2 static json_t *tunnel_erspan_print_json(const struct obj *obj)
377 {
378 json_t *tunnel;
379
380
2/3
✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 1 times.
✗ Branch 2 (2→9) not taken.
2 switch (obj->tunnel.erspan.version) {
381 1 case 1:
382 1 tunnel = json_pack("{s:i, s:i}",
383 1 "version", obj->tunnel.erspan.version,
384 1 "index", obj->tunnel.erspan.v1.index);
385 1 break;
386 1 case 2:
387 1 tunnel = json_pack("{s:i, s:s, s:i}",
388 1 "version", obj->tunnel.erspan.version,
389 1 "dir", obj->tunnel.erspan.v2.direction ?
390 "egress" : "ingress",
391
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 1 times.
1 "hwid", obj->tunnel.erspan.v2.hwid);
392 1 break;
393 ✗ default:
394 ✗ BUG("Unknown tunnel erspan version %d", obj->tunnel.erspan.version);
395 }
396
397 2 return tunnel;
398 }
399
400 141 static json_t *obj_print_json(struct output_ctx *octx, const struct obj *obj,
401 bool delete)
402 {
403 141 const char *rate_unit = NULL, *burst_unit = NULL;
404 141 const char *type = obj_type_name(obj->type);
405 json_t *root, *tmp, *flags;
406 uint64_t rate, burst;
407
408 141 root = nft_json_pack("{s:s, s:s, s:s, s:I}",
409 "family", family2str(obj->handle.family),
410 "name", obj->handle.obj.name,
411 "table", obj->handle.table.name,
412 "handle", obj->handle.handle.id);
413
414
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 141 times.
141 if (delete)
415 ✗ goto out;
416
417
2/2
✓ Branch 0 (7→8) taken 9 times.
✓ Branch 1 (7→11) taken 132 times.
141 if (obj->comment) {
418 9 tmp = nft_json_pack("{s:s}", "comment", obj->comment);
419 9 json_object_update(root, tmp);
420 9 json_decref(tmp);
421 }
422
423
9/10
✓ Branch 0 (11→12) taken 43 times.
✓ Branch 1 (11→16) taken 32 times.
✓ Branch 2 (11→20) taken 1 times.
✓ Branch 3 (11→24) taken 14 times.
✓ Branch 4 (11→30) taken 13 times.
✓ Branch 5 (11→37) taken 9 times.
✓ Branch 6 (11→43) taken 17 times.
✓ Branch 7 (11→63) taken 8 times.
✓ Branch 8 (11→75) taken 4 times.
✗ Branch 9 (11→112) not taken.
141 switch (obj->type) {
424 43 case NFT_OBJECT_COUNTER:
425 43 tmp = nft_json_pack("{s:I, s:I}",
426 "packets", obj->counter.packets,
427 "bytes", obj->counter.bytes);
428 43 json_object_update(root, tmp);
429 43 json_decref(tmp);
430 43 break;
431 32 case NFT_OBJECT_QUOTA:
432 32 tmp = nft_json_pack("{s:I, s:I, s:b}",
433 "bytes", obj->quota.bytes,
434 "used", obj->quota.used,
435 "inv", obj->quota.flags & NFT_QUOTA_F_INV);
436 32 json_object_update(root, tmp);
437 32 json_decref(tmp);
438 32 break;
439 1 case NFT_OBJECT_SECMARK:
440 1 tmp = nft_json_pack("{s:s}",
441 "context", obj->secmark.ctx);
442 1 json_object_update(root, tmp);
443 1 json_decref(tmp);
444 1 break;
445 14 case NFT_OBJECT_CT_HELPER:
446 14 tmp = nft_json_pack("{s:s, s:o, s:s}",
447 "type", obj->ct_helper.name, "protocol",
448 proto_name_json(obj->ct_helper.l4proto),
449 "l3proto", family2str(obj->ct_helper.l3proto));
450 14 json_object_update(root, tmp);
451 14 json_decref(tmp);
452 14 break;
453 13 case NFT_OBJECT_CT_TIMEOUT:
454 13 tmp = timeout_policy_json(obj->ct_timeout.l4proto,
455 13 obj->ct_timeout.timeout);
456 13 tmp = nft_json_pack("{s:o, s:s, s:o}",
457 "protocol",
458 proto_name_json(obj->ct_timeout.l4proto),
459 "l3proto", family2str(obj->ct_timeout.l3proto),
460 "policy", tmp);
461 13 json_object_update(root, tmp);
462 13 json_decref(tmp);
463 13 break;
464 9 case NFT_OBJECT_CT_EXPECT:
465 9 tmp = nft_json_pack("{s:o, s:I, s:I, s:I, s:s}",
466 "protocol",
467 proto_name_json(obj->ct_expect.l4proto),
468 "dport", obj->ct_expect.dport,
469 "timeout", obj->ct_expect.timeout,
470 "size", obj->ct_expect.size,
471 "l3proto", family2str(obj->ct_expect.l3proto));
472 9 json_object_update(root, tmp);
473 9 json_decref(tmp);
474 9 break;
475 17 case NFT_OBJECT_LIMIT:
476 17 rate = obj->limit.rate;
477 17 burst = obj->limit.burst;
478
479
2/2
✓ Branch 0 (43→44) taken 8 times.
✓ Branch 1 (43→46) taken 9 times.
17 if (obj->limit.type == NFT_LIMIT_PKT_BYTES) {
480 8 rate_unit = get_rate(obj->limit.rate, &rate);
481 8 burst_unit = get_rate(obj->limit.burst, &burst);
482 }
483
484 17 tmp = nft_json_pack("{s:I, s:s}",
485 "rate", rate,
486 "per", get_unit(obj->limit.unit));
487
488
2/2
✓ Branch 0 (48→49) taken 5 times.
✓ Branch 1 (48→51) taken 12 times.
17 if (obj->limit.flags & NFT_LIMIT_F_INV)
489 5 json_object_set_new(tmp, "inv", json_true());
490
2/2
✓ Branch 0 (51→52) taken 8 times.
✓ Branch 1 (51→54) taken 9 times.
17 if (rate_unit)
491 8 json_object_set_new(tmp, "rate_unit",
492 json_string(rate_unit));
493
2/2
✓ Branch 0 (54→55) taken 14 times.
✓ Branch 1 (54→60) taken 3 times.
17 if (burst) {
494 14 json_object_set_new(tmp, "burst", json_integer(burst));
495
2/2
✓ Branch 0 (57→58) taken 5 times.
✓ Branch 1 (57→60) taken 9 times.
14 if (burst_unit)
496 5 json_object_set_new(tmp, "burst_unit",
497 json_string(burst_unit));
498 }
499
500 17 json_object_update(root, tmp);
501 17 json_decref(tmp);
502 17 break;
503 8 case NFT_OBJECT_SYNPROXY:
504 8 tmp = nft_json_pack("{s:i, s:i}",
505 "mss", obj->synproxy.mss,
506 "wscale", obj->synproxy.wscale);
507
508 8 flags = json_array();
509
2/2
✓ Branch 0 (65→66) taken 3 times.
✓ Branch 1 (65→68) taken 5 times.
8 if (obj->synproxy.flags & NF_SYNPROXY_OPT_TIMESTAMP)
510 3 json_array_append_new(flags, json_string("timestamp"));
511
2/2
✓ Branch 0 (68→69) taken 5 times.
✓ Branch 1 (68→71) taken 3 times.
8 if (obj->synproxy.flags & NF_SYNPROXY_OPT_SACK_PERM)
512 5 json_array_append_new(flags, json_string("sack-perm"));
513 8 json_add_array_new(tmp, "flags", flags);
514
515 8 json_object_update(root, tmp);
516 8 json_decref(tmp);
517 8 break;
518 4 case NFT_OBJECT_TUNNEL:
519 8 tmp = json_pack("{s:i, s:o, s:o, s:i, s:i, s:i, s:i}",
520 4 "id", obj->tunnel.id,
521
1/2
✓ Branch 0 (80→81) taken 4 times.
✗ Branch 1 (80→82) not taken.
4 obj->tunnel.src->dtype->type == TYPE_IPADDR ? "src-ipv4" : "src-ipv6",
522 4 expr_print_json(obj->tunnel.src, octx),
523
1/2
✓ Branch 0 (76→77) taken 4 times.
✗ Branch 1 (76→78) not taken.
4 obj->tunnel.dst->dtype->type == TYPE_IPADDR ? "dst-ipv4" : "dst-ipv6",
524 4 expr_print_json(obj->tunnel.dst, octx),
525 4 "sport", obj->tunnel.sport,
526 4 "dport", obj->tunnel.dport,
527 4 "tos", obj->tunnel.tos,
528 4 "ttl", obj->tunnel.ttl);
529
530
3/5
✗ Branch 0 (84→85) not taken.
✓ Branch 1 (84→86) taken 2 times.
✓ Branch 2 (84→91) taken 1 times.
✓ Branch 3 (84→96) taken 1 times.
✗ Branch 4 (84→109) not taken.
4 switch (obj->tunnel.type) {
531 ✗ case TUNNEL_UNSPEC:
532 ✗ break;
533 2 case TUNNEL_ERSPAN:
534 2 json_object_set_new(tmp, "type", json_string("erspan"));
535 2 json_object_set_new(tmp, "tunnel",
536 tunnel_erspan_print_json(obj));
537 2 break;
538 1 case TUNNEL_VXLAN:
539 1 json_object_set_new(tmp, "type", json_string("vxlan"));
540 1 json_object_set_new(tmp, "tunnel",
541 json_pack("{s:i}",
542 "gbp",
543 1 obj->tunnel.vxlan.gbp));
544 1 break;
545 1 case TUNNEL_GENEVE:
546 struct tunnel_geneve *geneve;
547 1 json_t *opts = json_array();
548
549
2/2
✓ Branch 0 (104→98) taken 3 times.
✓ Branch 1 (104→105) taken 1 times.
4 list_for_each_entry(geneve, &obj->tunnel.geneve_opts, list) {
550 char data_str[256];
551 json_t *opt;
552 int offset;
553
554 3 data_str[0] = '0';
555 3 data_str[1] = 'x';
556 3 offset = 2;
557
2/2
✓ Branch 0 (100→99) taken 16 times.
✓ Branch 1 (100→101) taken 3 times.
19 for (uint32_t i = 0; i < geneve->data_len; i++)
558 16 offset += snprintf(data_str + offset,
559 16 3, "%x", geneve->data[i]);
560
561 3 opt = json_pack("{s:i, s:i, s:s}",
562 3 "class", geneve->geneve_class,
563 3 "opt-type", geneve->type,
564 "data", data_str);
565 3 json_array_append_new(opts, opt);
566 }
567
568 1 json_object_set_new(tmp, "type", json_string("geneve"));
569 1 json_object_set_new(tmp, "tunnel", opts);
570 1 break;
571 }
572 4 json_object_update(root, tmp);
573 4 json_decref(tmp);
574 4 break;
575 }
576
577 141 out:
578 141 return nft_json_pack("{s:o}", type, root);
579 }
580
581 18 static json_t *flowtable_print_json(const struct flowtable *ftable)
582 {
583 18 json_t *root, *devs = NULL;
584 18 int i, priority = 0;
585
586 18 root = nft_json_pack("{s:s, s:s, s:s, s:I}",
587 "family", family2str(ftable->handle.family),
588 "name", ftable->handle.flowtable.name,
589 "table", ftable->handle.table.name,
590 "handle", ftable->handle.handle.id);
591
592
2/2
✓ Branch 0 (4→5) taken 17 times.
✓ Branch 1 (4→9) taken 1 times.
18 if (ftable->priority.expr) {
593 json_t *tmp;
594
595 17 mpz_export_data(&priority, ftable->priority.expr->value,
596 BYTEORDER_HOST_ENDIAN, sizeof(int));
597
598 17 tmp = nft_json_pack("{s:s, s:i}",
599 "hook", hooknum2str(NFPROTO_NETDEV,
600 ftable->hook.num),
601 "prio", priority);
602 17 json_object_update_new(root, tmp);
603 }
604
605
2/2
✓ Branch 0 (18→10) taken 17 times.
✓ Branch 1 (18→19) taken 18 times.
35 for (i = 0; i < ftable->dev_array_len; i++) {
606 17 const char *dev = ftable->dev_array[i];
607
2/2
✓ Branch 0 (10→11) taken 15 times.
✓ Branch 1 (10→12) taken 2 times.
17 if (!devs)
608 15 devs = json_string(dev);
609
2/4
✓ Branch 0 (12→13) taken 2 times.
✗ Branch 1 (12→15) not taken.
✓ Branch 2 (13→14) taken 2 times.
✗ Branch 3 (13→15) not taken.
2 else if (json_is_string(devs))
610 2 devs = nft_json_pack("[o, s]", devs, dev);
611 else
612 ✗ json_array_append_new(devs, json_string(dev));
613 }
614
2/2
✓ Branch 0 (19→20) taken 15 times.
✓ Branch 1 (19→21) taken 3 times.
18 if (devs)
615 15 json_object_set_new(root, "dev", devs);
616
617 18 return nft_json_pack("{s:o}", "flowtable", root);
618 }
619
620 6346 static json_t *table_flags_json(const struct table *table)
621 {
622 6346 uint32_t flags = table->flags;
623 6346 json_t *root = json_array(), *tmp;
624 6346 int i = 0;
625
626
2/2
✓ Branch 0 (9→4) taken 9 times.
✓ Branch 1 (9→10) taken 6346 times.
6355 while (flags) {
627
2/2
✓ Branch 0 (4→5) taken 8 times.
✓ Branch 1 (4→8) taken 1 times.
9 if (flags & 0x1) {
628 8 tmp = json_string(table_flag_name(i));
629 8 json_array_append_new(root, tmp);
630 }
631 9 flags >>= 1;
632 9 i++;
633 }
634 6346 return root;
635 }
636
637 6346 static json_t *table_print_json(const struct table *table)
638 {
639 json_t *root;
640
641 6346 root = nft_json_pack("{s:s, s:s, s:I}",
642 "family", family2str(table->handle.family),
643 "name", table->handle.table.name,
644 "handle", table->handle.handle.id);
645 6346 json_add_array_new(root, "flags", table_flags_json(table));
646
647
2/2
✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→9) taken 6344 times.
6346 if (table->comment)
648 2 json_object_set_new(root, "comment", json_string(table->comment));
649
650 6346 return nft_json_pack("{s:o}", "table", root);
651 }
652
653 static json_t *
654 − __binop_expr_json(int op, const struct expr *expr, struct output_ctx *octx)
655 {
656 − json_t *a = json_array();
657
658 − if (expr->etype == EXPR_BINOP && expr->op == op) {
659 − json_array_extend_new(a,
660 − __binop_expr_json(op, expr->left, octx));
661 − json_array_extend_new(a,
662 − __binop_expr_json(op, expr->right, octx));
663 } else {
664 − json_array_append_new(a, expr_print_json(expr, octx));
665 }
666 − return a;
667 }
668
669 442 json_t *binop_expr_json(const struct expr *expr, struct output_ctx *octx)
670 {
671 442 return nft_json_pack("{s:o}", expr_op_symbols[expr->op],
672 __binop_expr_json(expr->op, expr, octx));
673 }
674
675 /* Workaround to retain backwards compatibility in fib output. */
676 #define __NFT_CTX_OUTPUT_RELATIONAL (1 << 30)
677
678 25719 json_t *relational_expr_json(const struct expr *expr, struct output_ctx *octx)
679 {
680 json_t *ret;
681
682 25719 octx->flags |= __NFT_CTX_OUTPUT_RELATIONAL;
683
2/2
✓ Branch 0 (4→5) taken 25604 times.
✓ Branch 1 (4→6) taken 115 times.
25719 ret = nft_json_pack("{s:{s:s, s:o, s:o}}", "match",
684 "op", expr_op_symbols[expr->op] ? : "in",
685 "left", expr_print_json(expr->left, octx),
686 "right", expr_print_json(expr->right, octx));
687 25719 octx->flags &= ~__NFT_CTX_OUTPUT_RELATIONAL;
688
689 25719 return ret;
690 }
691
692 1256 json_t *range_expr_json(const struct expr *expr, struct output_ctx *octx)
693 {
694 1256 unsigned int flags = octx->flags;
695 json_t *root;
696
697 1256 octx->flags &= ~NFT_CTX_OUTPUT_SERVICE;
698 1256 octx->flags |= NFT_CTX_OUTPUT_NUMERIC_PROTO;
699 1256 root = nft_json_pack("{s:[o, o]}", "range",
700 expr_print_json(expr->left, octx),
701 expr_print_json(expr->right, octx));
702 1256 octx->flags = flags;
703
704 1256 return root;
705 }
706
707 21683 json_t *meta_expr_json(const struct expr *expr, struct output_ctx *octx)
708 {
709 21683 return nft_json_pack("{s:{s:s}}", "meta",
710 "key", meta_templates[expr->meta.key].token);
711 }
712
713 4258 json_t *payload_expr_json(const struct expr *expr, struct output_ctx *octx)
714 {
715 json_t *root;
716
717
2/2
✓ Branch 0 (3→4) taken 4180 times.
✓ Branch 1 (3→7) taken 78 times.
4258 if (payload_is_known(expr)) {
718
2/2
✓ Branch 0 (4→5) taken 208 times.
✓ Branch 1 (4→6) taken 3972 times.
4180 if (expr->payload.inner_desc) {
719 208 root = nft_json_pack("{s:s, s:s, s:s}",
720 "tunnel", expr->payload.inner_desc->name,
721 "protocol", expr->payload.desc->name,
722 "field", expr->payload.tmpl->token);
723 } else {
724 3972 root = nft_json_pack("{s:s, s:s}",
725 "protocol", expr->payload.desc->name,
726 "field", expr->payload.tmpl->token);
727 }
728 } else {
729 78 root = nft_json_pack("{s:s, s:i, s:i}",
730 "base", proto_base_tokens[expr->payload.base],
731 "offset", expr->payload.offset,
732 "len", expr->len);
733 }
734
735 4258 return nft_json_pack("{s:o}", "payload", root);
736 }
737
738 419 json_t *ct_expr_json(const struct expr *expr, struct output_ctx *octx)
739 {
740 419 const char *dirstr = ct_dir2str(expr->ct.direction);
741 419 enum nft_ct_keys key = expr->ct.key;
742 json_t *root;
743
744 419 root = nft_json_pack("{s:s}", "key", ct_templates[key].token);
745
746
2/2
✓ Branch 0 (4→5) taken 379 times.
✓ Branch 1 (4→6) taken 40 times.
419 if (expr->ct.direction < 0)
747 379 goto out;
748
749
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 40 times.
40 if (dirstr)
750 40 json_object_set_new(root, "dir", json_string(dirstr));
751 ✗ out:
752 419 return nft_json_pack("{s:o}", "ct", root);
753 }
754
755 757 json_t *concat_expr_json(const struct expr *expr, struct output_ctx *octx)
756 {
757 757 json_t *array = json_array();
758 const struct expr *i;
759
760
4/6
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 757 times.
✗ Branch 2 (9→10) not taken.
✓ Branch 3 (9→11) taken 2391 times.
✓ Branch 4 (11→6) taken 1634 times.
✓ Branch 5 (11→12) taken 757 times.
2391 list_for_each_entry(i, &expr_concat(expr)->expressions, list)
761 1634 json_array_append_new(array, expr_print_json(i, octx));
762
763 757 return nft_json_pack("{s:o}", "concat", array);
764 }
765
766 1618 json_t *set_expr_json(const struct expr *expr, struct output_ctx *octx)
767 {
768 1618 json_t *array = json_array();
769 const struct expr *i;
770
771
4/6
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 1618 times.
✗ Branch 2 (9→10) not taken.
✓ Branch 3 (9→11) taken 6464 times.
✓ Branch 4 (11→6) taken 4846 times.
✓ Branch 5 (11→12) taken 1618 times.
6464 list_for_each_entry(i, &expr_set(expr)->expressions, list)
772 4846 json_array_append_new(array, expr_print_json(i, octx));
773
774 1618 return nft_json_pack("{s:o}", "set", array);
775 }
776
777 1820 json_t *set_ref_expr_json(const struct expr *expr, struct output_ctx *octx)
778 {
779
2/2
✓ Branch 0 (3→4) taken 1599 times.
✓ Branch 1 (3→6) taken 221 times.
1820 if (set_is_anonymous(expr->set->flags)) {
780 1599 return expr_print_json(expr->set->init, octx);
781 } else {
782 221 return nft_json_pack("s+", "@", expr->set->handle.set.name);
783 }
784 }
785
786 239060 json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx)
787 {
788 239060 json_t *root = expr_print_json(expr->key, octx);
789 struct stmt *stmt;
790 json_t *tmp;
791
792
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 239060 times.
239060 if (!root)
793 ✗ return NULL;
794
795 /* these element attributes require formal set elem syntax */
796
8/8
✓ Branch 0 (5→6) taken 239000 times.
✓ Branch 1 (5→10) taken 60 times.
✓ Branch 2 (6→7) taken 238966 times.
✓ Branch 3 (6→10) taken 34 times.
✓ Branch 4 (7→8) taken 238961 times.
✓ Branch 5 (7→10) taken 5 times.
✓ Branch 6 (9→10) taken 53 times.
✓ Branch 7 (9→28) taken 238908 times.
478021 if (expr->timeout || expr->expiration || expr->comment ||
797 238961 !list_empty(&expr->stmt_list)) {
798 152 root = nft_json_pack("{s:o}", "val", root);
799
800
2/2
✓ Branch 0 (11→12) taken 60 times.
✓ Branch 1 (11→14) taken 92 times.
152 if (expr->timeout) {
801 60 tmp = json_integer(expr->timeout / 1000);
802 60 json_object_set_new(root, "timeout", tmp);
803 }
804
2/2
✓ Branch 0 (14→15) taken 72 times.
✓ Branch 1 (14→17) taken 80 times.
152 if (expr->expiration) {
805 72 tmp = json_integer(expr->expiration / 1000);
806 72 json_object_set_new(root, "expires", tmp);
807 }
808
2/2
✓ Branch 0 (17→18) taken 5 times.
✓ Branch 1 (17→20) taken 147 times.
152 if (expr->comment) {
809 5 tmp = json_string(expr->comment);
810 5 json_object_set_new(root, "comment", tmp);
811 }
812
2/2
✓ Branch 0 (25→21) taken 63 times.
✓ Branch 1 (25→26) taken 89 times.
152 list_for_each_entry(stmt, &expr->stmt_list, list) {
813 63 tmp = stmt_print_json(stmt, octx);
814 /* XXX: detect and complain about clashes? */
815 63 json_object_update_missing(root, tmp);
816 63 json_decref(tmp);
817 /* TODO: only one statement per element. */
818 63 break;
819 }
820 152 return nft_json_pack("{s:o}", "elem", root);
821 }
822
823 238908 return root;
824 }
825
826 69750 json_t *prefix_expr_json(const struct expr *expr, struct output_ctx *octx)
827 {
828 69750 json_t *root = expr_print_json(expr->prefix, octx);
829
830 69750 return nft_json_pack("{s:{s:o, s:i}}", "prefix",
831 "addr", root,
832 "len", expr->prefix_len);
833 }
834
835 57 json_t *list_expr_json(const struct expr *expr, struct output_ctx *octx)
836 {
837 57 json_t *array = json_array();
838 const struct expr *i;
839
840
4/6
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 57 times.
✗ Branch 2 (9→10) not taken.
✓ Branch 3 (9→11) taken 205 times.
✓ Branch 4 (11→6) taken 148 times.
✓ Branch 5 (11→12) taken 57 times.
205 list_for_each_entry(i, &expr_list(expr)->expressions, list)
841 148 json_array_append_new(array, expr_print_json(i, octx));
842
843 //return nft_json_pack("{s:s, s:o}", "type", "list", "val", array);
844 57 return array;
845 }
846
847 306 json_t *unary_expr_json(const struct expr *expr, struct output_ctx *octx)
848 {
849 306 return expr_print_json(expr->arg, octx);
850 }
851
852 5648 json_t *mapping_expr_json(const struct expr *expr, struct output_ctx *octx)
853 {
854 5648 return nft_json_pack("[o, o]",
855 expr_print_json(expr->left, octx),
856 expr_print_json(expr->right, octx));
857 }
858
859 456 json_t *map_expr_json(const struct expr *expr, struct output_ctx *octx)
860 {
861 456 const char *type = "map";
862
863
1/2
✓ Branch 0 (2→3) taken 456 times.
✗ Branch 1 (2→5) not taken.
456 if (expr->mappings->etype == EXPR_SET_REF &&
864
2/2
✓ Branch 0 (3→4) taken 284 times.
✓ Branch 1 (3→5) taken 172 times.
456 expr->mappings->set->data->dtype->type == TYPE_VERDICT)
865 284 type = "vmap";
866
867 456 return nft_json_pack("{s:{s:o, s:o}}", type,
868 "key", expr_print_json(expr->map, octx),
869 "data", expr_print_json(expr->mappings, octx));
870 }
871
872 623 json_t *exthdr_expr_json(const struct expr *expr, struct output_ctx *octx)
873 {
874 13 const char *desc = expr->exthdr.desc ?
875
2/2
✓ Branch 0 (2→3) taken 610 times.
✓ Branch 1 (2→4) taken 13 times.
623 expr->exthdr.desc->name : NULL;
876 623 const char *field = expr->exthdr.tmpl->token;
877 json_t *root;
878 623 bool is_exists = expr->exthdr.flags & NFT_EXTHDR_F_PRESENT;
879
880
2/2
✓ Branch 0 (5→6) taken 138 times.
✓ Branch 1 (5→16) taken 485 times.
623 if (expr->exthdr.op == NFT_EXTHDR_OP_TCPOPT) {
881 static const char *offstrs[] = { "", "1", "2", "3" };
882 138 unsigned int offset = expr->exthdr.offset / 64;
883 138 const char *offstr = "";
884
885
2/2
✓ Branch 0 (6→7) taken 125 times.
✓ Branch 1 (6→13) taken 13 times.
138 if (desc) {
886
1/2
✓ Branch 0 (7→8) taken 125 times.
✗ Branch 1 (7→9) not taken.
125 if (offset < 4)
887 125 offstr = offstrs[offset];
888
889 125 root = nft_json_pack("{s:s+}", "name", desc, offstr);
890
891
2/2
✓ Branch 0 (10→11) taken 81 times.
✓ Branch 1 (10→14) taken 44 times.
125 if (!is_exists)
892 81 json_object_set_new(root, "field", json_string(field));
893 } else {
894 13 root = nft_json_pack("{s:i, s:i, s:i}",
895 "base", expr->exthdr.raw_type,
896 "offset", expr->exthdr.offset,
897 "len", expr->len);
898 }
899
900 138 return nft_json_pack("{s:o}", "tcp option", root);
901 }
902
903
1/2
✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→20) taken 485 times.
485 if (expr->exthdr.op == NFT_EXTHDR_OP_DCCP) {
904 ✗ root = nft_json_pack("{s:i}", "type", expr->exthdr.raw_type);
905 ✗ return nft_json_pack("{s:o}", "dccp option", root);
906 }
907
908 485 root = nft_json_pack("{s:s}", "name", desc);
909
2/2
✓ Branch 0 (21→22) taken 383 times.
✓ Branch 1 (21→24) taken 102 times.
485 if (!is_exists)
910 383 json_object_set_new(root, "field", json_string(field));
911
912
3/3
✓ Branch 0 (24→25) taken 2 times.
✓ Branch 1 (24→26) taken 157 times.
✓ Branch 2 (24→27) taken 326 times.
485 switch (expr->exthdr.op) {
913 2 case NFT_EXTHDR_OP_IPV4:
914 2 return nft_json_pack("{s:o}", "ip option", root);
915 157 case NFT_EXTHDR_OP_SCTP:
916 157 return nft_json_pack("{s:o}", "sctp chunk", root);
917 326 default:
918 326 return nft_json_pack("{s:o}", "exthdr", root);
919 }
920 }
921
922 1799 json_t *verdict_expr_json(const struct expr *expr, struct output_ctx *octx)
923 {
924 const struct {
925 int verdict;
926 const char *name;
927 bool chain;
928 1799 } verdict_tbl[] = {
929 { NFT_CONTINUE, "continue", false },
930 { NFT_BREAK, "break", false },
931 { NFT_JUMP, "jump", true },
932 { NFT_GOTO, "goto", true },
933 { NFT_RETURN, "return", false },
934 { NF_ACCEPT, "accept", false },
935 { NF_DROP, "drop", false },
936 { NF_QUEUE, "queue", false },
937 };
938 1799 const char *name = NULL;
939 1799 json_t *chain = NULL;
940 unsigned int i;
941
942
1/2
✓ Branch 0 (9→3) taken 9843 times.
✗ Branch 1 (9→10) not taken.
9843 for (i = 0; i < array_size(verdict_tbl); i++) {
943
2/2
✓ Branch 0 (3→4) taken 1799 times.
✓ Branch 1 (3→8) taken 8044 times.
9843 if (expr->verdict == verdict_tbl[i].verdict) {
944 1799 name = verdict_tbl[i].name;
945
3/4
✓ Branch 0 (4→5) taken 446 times.
✓ Branch 1 (4→7) taken 1353 times.
✓ Branch 2 (5→6) taken 446 times.
✗ Branch 3 (5→7) not taken.
1799 if (verdict_tbl[i].chain && expr->chain)
946 446 chain = expr_print_json(expr->chain, octx);
947 1799 break;
948 }
949 }
950
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 1799 times.
1799 if (!name) {
951 ✗ BUG("Unknown verdict %d.", expr->verdict);
952 return NULL;
953 }
954
2/2
✓ Branch 0 (13→14) taken 446 times.
✓ Branch 1 (13→16) taken 1353 times.
1799 if (chain)
955 446 return nft_json_pack("{s:{s:o}}", name, "target", chain);
956 else
957 1353 return nft_json_pack("{s:n}", name);
958 }
959
960 14 json_t *rt_expr_json(const struct expr *expr, struct output_ctx *octx)
961 {
962 14 const char *key = rt_templates[expr->rt.key].token;
963 14 json_t *root = nft_json_pack("{s:s}", "key", key);
964 14 const char *family = NULL;
965
966
3/3
✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 2 times.
✓ Branch 2 (3→6) taken 10 times.
14 switch (expr->rt.key) {
967 2 case NFT_RT_NEXTHOP4:
968 2 family = "ip";
969 2 break;
970 2 case NFT_RT_NEXTHOP6:
971 2 family = "ip6";
972 2 break;
973 10 default:
974 10 break;
975 }
976
977
2/2
✓ Branch 0 (7→8) taken 4 times.
✓ Branch 1 (7→10) taken 10 times.
14 if (family)
978 4 json_object_set_new(root, "family", json_string(family));
979
980 14 return nft_json_pack("{s:o}", "rt", root);
981 }
982
983 27 json_t *numgen_expr_json(const struct expr *expr, struct output_ctx *octx)
984 {
985 const char *mode;
986
987
1/3
✓ Branch 0 (2→3) taken 27 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (2→5) not taken.
27 switch (expr->numgen.type) {
988 27 case NFT_NG_INCREMENTAL:
989 27 mode = "inc";
990 27 break;
991 ✗ case NFT_NG_RANDOM:
992 ✗ mode = "random";
993 ✗ break;
994 ✗ default:
995 ✗ mode = "unknown";
996 ✗ break;
997 }
998
999 27 return nft_json_pack("{s:{s:s, s:i, s:i}}", "numgen",
1000 "mode", mode,
1001 "mod", expr->numgen.mod,
1002 "offset", expr->numgen.offset);
1003 }
1004
1005 18 json_t *hash_expr_json(const struct expr *expr, struct output_ctx *octx)
1006 {
1007 const char *type;
1008 18 json_t *root, *jexpr = NULL;
1009
1010
2/2
✓ Branch 0 (2→3) taken 7 times.
✓ Branch 1 (2→4) taken 11 times.
18 switch (expr->hash.type) {
1011 7 case NFT_HASH_SYM:
1012 7 type = "symhash";
1013 7 break;
1014 11 case NFT_HASH_JENKINS:
1015 default:
1016 11 type = "jhash";
1017 11 jexpr = expr_print_json(expr->hash.expr, octx);
1018 11 break;
1019 }
1020
1021 18 root = nft_json_pack("{s:i}", "mod", expr->hash.mod);
1022
2/2
✓ Branch 0 (7→8) taken 4 times.
✓ Branch 1 (7→10) taken 14 times.
18 if (expr->hash.seed_set)
1023 4 json_object_set_new(root, "seed",
1024 4 json_integer(expr->hash.seed));
1025
2/2
✓ Branch 0 (10→11) taken 3 times.
✓ Branch 1 (10→13) taken 15 times.
18 if (expr->hash.offset)
1026 3 json_object_set_new(root, "offset",
1027 3 json_integer(expr->hash.offset));
1028
2/2
✓ Branch 0 (13→14) taken 11 times.
✓ Branch 1 (13→15) taken 7 times.
18 if (jexpr)
1029 11 json_object_set_new(root, "expr", jexpr);
1030
1031 18 return nft_json_pack("{s:o}", type, root);
1032 }
1033
1034 26 json_t *fib_expr_json(const struct expr *expr, struct output_ctx *octx)
1035 {
1036 26 const char *fib_flags[] = { "saddr", "daddr", "mark", "iif", "oif" };
1037 26 unsigned int flags = expr->fib.flags & ~NFTA_FIB_F_PRESENT;
1038 26 bool check = !(octx->flags & __NFT_CTX_OUTPUT_RELATIONAL);
1039 json_t *root;
1040
1041 26 root = nft_json_pack("{s:s}", "result", fib_result_str(expr, check));
1042
1043
1/2
✓ Branch 0 (4→5) taken 26 times.
✗ Branch 1 (4→17) not taken.
26 if (flags) {
1044 26 json_t *tmp = json_array();
1045 unsigned int i;
1046
1047
2/2
✓ Branch 0 (12→7) taken 130 times.
✓ Branch 1 (12→13) taken 26 times.
156 for (i = 0; i < array_size(fib_flags); i++) {
1048
2/2
✓ Branch 0 (7→8) taken 38 times.
✓ Branch 1 (7→11) taken 92 times.
130 if (flags & (1 << i)) {
1049 38 json_array_append_new(tmp, json_string(fib_flags[i]));
1050 38 flags &= ~(1 << i);
1051 }
1052 }
1053
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→16) taken 26 times.
26 if (flags)
1054 ✗ json_array_append_new(tmp, json_integer(flags));
1055
1056 26 json_add_array_new(root, "flags", tmp);
1057 }
1058 26 return nft_json_pack("{s:o}", "fib", root);
1059 }
1060
1061 3139 static json_t *symbolic_constant_json(const struct symbol_table *tbl,
1062 const struct expr *expr,
1063 struct output_ctx *octx)
1064 {
1065 3139 unsigned int len = div_round_up(expr->len, BITS_PER_BYTE);
1066 const struct symbolic_constant *s;
1067 3139 uint64_t val = 0;
1068
1069 /* Export the data in the correct byteorder for comparison */
1070
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 3139 times.
3139 assert(expr->len / BITS_PER_BYTE <= sizeof(val));
1071
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 3139 times.
3139 mpz_export_data(constant_data_ptr(val, expr->len), expr->value,
1072 expr->byteorder, len);
1073
1074
2/2
✓ Branch 0 (11→8) taken 12250 times.
✓ Branch 1 (11→12) taken 749 times.
12999 for (s = tbl->symbols; s->identifier != NULL; s++) {
1075
2/2
✓ Branch 0 (8→9) taken 2390 times.
✓ Branch 1 (8→10) taken 9860 times.
12250 if (val == s->value)
1076 2390 break;
1077 }
1078
2/2
✓ Branch 0 (12→13) taken 749 times.
✓ Branch 1 (12→16) taken 2390 times.
3139 if (!s->identifier)
1079 749 return expr_basetype(expr)->json(expr, octx);
1080
1081
1/2
✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→20) taken 2390 times.
2390 if (nft_output_numeric_symbol(octx))
1082 ✗ return json_integer(val);
1083 else
1084 2390 return json_string(s->identifier);
1085 }
1086
1087 35 json_t *set_elem_catchall_expr_json(const struct expr *expr, struct output_ctx *octx)
1088 {
1089 35 return json_string("*");
1090 }
1091
1092 272004 static json_t *datatype_json(const struct expr *expr, struct output_ctx *octx)
1093 {
1094 272004 const struct datatype *dtype = expr->dtype;
1095
1096 do {
1097
2/2
✓ Branch 0 (3→4) taken 61264 times.
✓ Branch 1 (3→6) taken 231257 times.
292521 if (dtype->json)
1098 61264 return dtype->json(expr, octx);
1099
2/2
✓ Branch 0 (6→7) taken 2364 times.
✓ Branch 1 (6→9) taken 228893 times.
231257 if (dtype->sym_tbl)
1100 2364 return symbolic_constant_json(dtype->sym_tbl,
1101 expr, octx);
1102
2/2
✓ Branch 0 (9→10) taken 208376 times.
✓ Branch 1 (9→18) taken 20517 times.
228893 if (dtype->print) {
1103 char buf[1024];
1104 208376 FILE *ofp = octx->output_fp;
1105
1106 208376 octx->output_fp = fmemopen(buf, 1024, "w");
1107 208376 dtype->print(expr, octx);
1108 208376 fclose(octx->output_fp);
1109 208376 octx->output_fp = ofp;
1110
1111
2/2
✓ Branch 0 (13→14) taken 416 times.
✓ Branch 1 (13→15) taken 207960 times.
208376 if (buf[0] == '"') {
1112 416 memmove(buf, buf + 1, strlen(buf));
1113 416 *strchrnul(buf, '"') = '\0';
1114 }
1115
1116 208376 return json_string(buf);
1117 }
1118
1/2
✓ Branch 0 (18→3) taken 20517 times.
✗ Branch 1 (18→19) not taken.
20517 } while ((dtype = dtype->basetype));
1119
1120 ✗ BUG("datatype %s has no print method or symbol table",
1121 expr->dtype->name);
1122 }
1123
1124 272004 json_t *constant_expr_json(const struct expr *expr, struct output_ctx *octx)
1125 {
1126 272004 return datatype_json(expr, octx);
1127 }
1128
1129 15 json_t *socket_expr_json(const struct expr *expr, struct output_ctx *octx)
1130 {
1131 15 return nft_json_pack("{s:{s:s}}", "socket", "key",
1132 socket_templates[expr->socket.key].token);
1133 }
1134
1135 28 json_t *osf_expr_json(const struct expr *expr, struct output_ctx *octx)
1136 {
1137 json_t *root;
1138
1139
2/2
✓ Branch 0 (2→3) taken 9 times.
✓ Branch 1 (2→4) taken 19 times.
28 if (expr->osf.flags & NFT_OSF_F_VERSION)
1140 9 root = nft_json_pack("{s:s}", "key", "version");
1141 else
1142 19 root = nft_json_pack("{s:s}", "key", "name");
1143
1144
3/3
✓ Branch 0 (5→6) taken 3 times.
✓ Branch 1 (5→9) taken 6 times.
✓ Branch 2 (5→12) taken 19 times.
28 switch (expr->osf.ttl) {
1145 3 case 1:
1146 3 json_object_set_new(root, "ttl", json_string("loose"));
1147 3 break;
1148 6 case 2:
1149 6 json_object_set_new(root, "ttl", json_string("skip"));
1150 6 break;
1151 }
1152
1153 28 return nft_json_pack("{s:o}", "osf", root);
1154 }
1155
1156 32 json_t *xfrm_expr_json(const struct expr *expr, struct output_ctx *octx)
1157 {
1158 32 const char *name = xfrm_templates[expr->xfrm.key].token;
1159 32 const char *family = NULL;
1160 const char *dirstr;
1161 json_t *root;
1162
1163
2/3
✓ Branch 0 (2→3) taken 20 times.
✓ Branch 1 (2→4) taken 12 times.
✗ Branch 2 (2→5) not taken.
32 switch (expr->xfrm.direction) {
1164 20 case XFRM_POLICY_IN:
1165 20 dirstr = "in";
1166 20 break;
1167 12 case XFRM_POLICY_OUT:
1168 12 dirstr = "out";
1169 12 break;
1170 ✗ default:
1171 ✗ return NULL;
1172 }
1173
1174
3/4
✓ Branch 0 (6→7) taken 20 times.
✓ Branch 1 (6→8) taken 6 times.
✓ Branch 2 (6→9) taken 6 times.
✗ Branch 3 (6→10) not taken.
32 switch (expr->xfrm.key) {
1175 20 case NFT_XFRM_KEY_UNSPEC:
1176 case NFT_XFRM_KEY_SPI:
1177 case NFT_XFRM_KEY_REQID:
1178 case __NFT_XFRM_KEY_MAX:
1179 20 break;
1180 6 case NFT_XFRM_KEY_DADDR_IP4:
1181 case NFT_XFRM_KEY_SADDR_IP4:
1182 6 family = "ip";
1183 6 break;
1184 6 case NFT_XFRM_KEY_DADDR_IP6:
1185 case NFT_XFRM_KEY_SADDR_IP6:
1186 6 family = "ip6";
1187 6 break;
1188 }
1189
1190 32 root = nft_json_pack("{s:s}", "key", name);
1191
1192
2/2
✓ Branch 0 (11→12) taken 12 times.
✓ Branch 1 (11→14) taken 20 times.
32 if (family)
1193 12 json_object_set_new(root, "family", json_string(family));
1194
1195 32 json_object_set_new(root, "dir", json_string(dirstr));
1196 32 json_object_set_new(root, "spnum", json_integer(expr->xfrm.spnum));
1197
1198 32 return nft_json_pack("{s:o}", "ipsec", root);
1199 }
1200
1201 3 json_t *tunnel_expr_json(const struct expr *expr, struct output_ctx *octx)
1202 {
1203 6 return json_pack("{s:{s:s}}", "tunnel",
1204 3 "key", tunnel_templates[expr->tunnel.key].token);
1205 }
1206
1207 5187 json_t *integer_type_json(const struct expr *expr, struct output_ctx *octx)
1208 {
1209 5187 char buf[1024] = "0x";
1210
1211
2/2
✓ Branch 0 (2→3) taken 5174 times.
✓ Branch 1 (2→5) taken 13 times.
5187 if (mpz_fits_ulong_p(expr->value))
1212 5174 return json_integer(mpz_get_ui(expr->value));
1213
1214 13 mpz_get_str(buf + 2, 16, expr->value);
1215 13 return json_string(buf);
1216 }
1217
1218 20899 json_t *string_type_json(const struct expr *expr, struct output_ctx *octx)
1219 20899 {
1220 20899 unsigned int len = div_round_up(expr->len, BITS_PER_BYTE);
1221 20899 char data[len+1];
1222
1223
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 20899 times.
20899 mpz_export_data(data, expr->value, BYTEORDER_HOST_ENDIAN, len);
1224 20899 data[len] = '\0';
1225
1226 20899 return json_string(data);
1227 }
1228
1229 171 json_t *boolean_type_json(const struct expr *expr, struct output_ctx *octx)
1230 {
1231 171 unsigned int len = div_round_up(expr->len, BITS_PER_BYTE);
1232 171 uint64_t val = 0;
1233
1234 /* Export the data in the correct byteorder for comparison */
1235
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 171 times.
171 assert(expr->len / BITS_PER_BYTE <= sizeof(val));
1236
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 171 times.
171 mpz_export_data(constant_data_ptr(val, expr->len), expr->value,
1237 expr->byteorder, len);
1238
1239
2/2
✓ Branch 0 (7→8) taken 148 times.
✓ Branch 1 (7→9) taken 23 times.
171 return json_boolean((int)val);
1240 }
1241
1242 906 json_t *inet_protocol_type_json(const struct expr *expr,
1243 struct output_ctx *octx)
1244 {
1245
2/2
✓ Branch 0 (3→4) taken 90 times.
✓ Branch 1 (3→11) taken 816 times.
906 if (!nft_output_numeric_proto(octx)) {
1246 char name[NFT_PROTONAME_MAXSIZE];
1247
1248
1/2
✓ Branch 0 (6→7) taken 90 times.
✗ Branch 1 (6→9) not taken.
90 if (nft_getprotobynumber(mpz_get_uint8(expr->value), name, sizeof(name)))
1249 90 return json_string(name);
1250 }
1251 816 return integer_type_json(expr, octx);
1252 }
1253
1254 34527 json_t *inet_service_type_json(const struct expr *expr, struct output_ctx *octx)
1255 {
1256 34527 uint16_t port = mpz_get_be16(expr->value);
1257 char name[NFT_SERVNAME_MAXSIZE];
1258
1259
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 34527 times.
34527 if (!nft_output_service(octx) ||
1260 ✗ !nft_getservbyport(port, NULL, name, sizeof(name)))
1261 34527 return json_integer(ntohs(port));
1262
1263 ✗ return json_string(name);
1264 }
1265
1266 495 json_t *mark_type_json(const struct expr *expr, struct output_ctx *octx)
1267 {
1268 495 return symbolic_constant_json(octx->tbl.mark, expr, octx);
1269 }
1270
1271 280 json_t *devgroup_type_json(const struct expr *expr, struct output_ctx *octx)
1272 {
1273 280 return symbolic_constant_json(octx->tbl.devgroup, expr, octx);
1274 }
1275
1276 6 json_t *ct_label_type_json(const struct expr *expr, struct output_ctx *octx)
1277 {
1278 6 unsigned long bit = mpz_scan1(expr->value, 0);
1279 6 const char *labelstr = ct_label2str(octx->tbl.ct_label, bit);
1280
1281
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 6 times.
6 if (labelstr)
1282 ✗ return json_string(labelstr);
1283
1284 /* can happen when connlabel.conf is altered after rules were added */
1285 6 return json_integer(bit);
1286 }
1287
1288 78 json_t *time_type_json(const struct expr *expr, struct output_ctx *octx)
1289 {
1290 78 return json_integer(mpz_get_uint64(expr->value) / MSEC_PER_SEC);
1291 }
1292
1293 168 json_t *uid_type_json(const struct expr *expr, struct output_ctx *octx)
1294 {
1295 168 uint32_t uid = mpz_get_uint32(expr->value);
1296
1297
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→9) taken 168 times.
168 if (nft_output_guid(octx)) {
1298 ✗ struct passwd *pw = getpwuid(uid);
1299
1300 ✗ if (pw)
1301 ✗ return json_string(pw->pw_name);
1302 }
1303 168 return json_integer(uid);
1304 }
1305
1306 112 json_t *gid_type_json(const struct expr *expr, struct output_ctx *octx)
1307 {
1308 112 uint32_t gid = mpz_get_uint32(expr->value);
1309
1310
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→9) taken 112 times.
112 if (nft_output_guid(octx)) {
1311 ✗ struct group *gr = getgrgid(gid);
1312
1313 ✗ if (gr)
1314 ✗ return json_string(gr->gr_name);
1315 }
1316 112 return json_integer(gid);
1317 }
1318
1319 26003 json_t *expr_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1320 {
1321 26003 return expr_print_json(stmt->expr, octx);
1322 }
1323
1324 3 json_t *flow_offload_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1325 {
1326 3 return nft_json_pack("{s:{s:s, s:s+}}", "flow",
1327 "op", "add", "flowtable",
1328 "@", stmt->flow.table_name);
1329 }
1330
1331 167 json_t *payload_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1332 {
1333 167 return nft_json_pack("{s: {s:o, s:o}}", "mangle",
1334 "key", expr_print_json(stmt->payload.expr, octx),
1335 "value", expr_print_json(stmt->payload.val, octx));
1336 }
1337
1338 4 json_t *exthdr_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1339 {
1340 4 return nft_json_pack("{s: {s:o, s:o}}", "mangle",
1341 "key", expr_print_json(stmt->exthdr.expr, octx),
1342 "value", expr_print_json(stmt->exthdr.val, octx));
1343 }
1344
1345 100 json_t *quota_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1346 {
1347 const char *data_unit;
1348 uint64_t bytes;
1349 json_t *root;
1350
1351 100 data_unit = get_rate(stmt->quota.bytes, &bytes);
1352 100 root = nft_json_pack("{s:I, s:s}",
1353 "val", bytes,
1354 "val_unit", data_unit);
1355
1356
2/2
✓ Branch 0 (4→5) taken 47 times.
✓ Branch 1 (4→7) taken 53 times.
100 if (stmt->quota.flags & NFT_QUOTA_F_INV)
1357 47 json_object_set_new(root, "inv", json_true());
1358
4/4
✓ Branch 0 (8→9) taken 6 times.
✓ Branch 1 (8→15) taken 94 times.
✓ Branch 2 (9→10) taken 2 times.
✓ Branch 3 (9→15) taken 4 times.
100 if (!nft_output_stateless(octx) && stmt->quota.used) {
1359 2 data_unit = get_rate(stmt->quota.used, &bytes);
1360 2 json_object_set_new(root, "used", json_integer(bytes));
1361 2 json_object_set_new(root, "used_unit", json_string(data_unit));
1362 }
1363
1364 100 return nft_json_pack("{s:o}", "quota", root);
1365 }
1366
1367 102 json_t *ct_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1368 {
1369 102 struct expr expr = {
1370 .ct = {
1371 102 .key = stmt->ct.key,
1372 102 .direction = stmt->ct.direction,
1373 .nfproto = 0,
1374 },
1375 };
1376
1377 102 return nft_json_pack("{s:{s:o, s:o}}", "mangle",
1378 "key", ct_expr_json(&expr, octx),
1379 "value", expr_print_json(stmt->ct.expr, octx));
1380 }
1381
1382 274 json_t *limit_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1383 {
1384 274 const char *rate_unit = NULL, *burst_unit = NULL;
1385 274 bool inv = stmt->limit.flags & NFT_LIMIT_F_INV;
1386 274 uint64_t burst = stmt->limit.burst;
1387 274 uint64_t rate = stmt->limit.rate;
1388 json_t *root;
1389
1390
2/2
✓ Branch 0 (2→3) taken 170 times.
✓ Branch 1 (2→5) taken 104 times.
274 if (stmt->limit.type == NFT_LIMIT_PKT_BYTES) {
1391 170 rate_unit = get_rate(stmt->limit.rate, &rate);
1392 170 burst_unit = get_rate(stmt->limit.burst, &burst);
1393 }
1394
1395 274 root = nft_json_pack("{s:I, s:I, s:s}",
1396 "rate", rate,
1397 "burst", burst,
1398 "per", get_unit(stmt->limit.unit));
1399
2/2
✓ Branch 0 (7→8) taken 136 times.
✓ Branch 1 (7→12) taken 138 times.
274 if (inv)
1400
1/2
✓ Branch 0 (8→9) taken 136 times.
✗ Branch 1 (8→10) not taken.
136 json_object_set_new(root, "inv", json_boolean(inv));
1401
2/2
✓ Branch 0 (12→13) taken 170 times.
✓ Branch 1 (12→15) taken 104 times.
274 if (rate_unit)
1402 170 json_object_set_new(root, "rate_unit", json_string(rate_unit));
1403
2/2
✓ Branch 0 (15→16) taken 170 times.
✓ Branch 1 (15→18) taken 104 times.
274 if (burst_unit)
1404 170 json_object_set_new(root, "burst_unit",
1405 json_string(burst_unit));
1406
1407 274 return nft_json_pack("{s:o}", "limit", root);
1408 }
1409
1410 6 json_t *fwd_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1411 {
1412 json_t *root, *tmp;
1413
1414 6 root = nft_json_pack("{s:o}", "dev", expr_print_json(stmt->fwd.dev, octx));
1415
1416
2/2
✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→10) taken 4 times.
6 if (stmt->fwd.addr) {
1417 2 tmp = json_string(family2str(stmt->fwd.family));
1418 2 json_object_set_new(root, "family", tmp);
1419
1420 2 tmp = expr_print_json(stmt->fwd.addr, octx);
1421 2 json_object_set_new(root, "addr", tmp);
1422 }
1423
1424 6 return nft_json_pack("{s:o}", "fwd", root);
1425 }
1426
1427 3 json_t *notrack_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1428 {
1429 3 return nft_json_pack("{s:n}", "notrack");
1430 }
1431
1432 10 json_t *dup_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1433 {
1434 json_t *root;
1435
1436
1/2
✓ Branch 0 (2→3) taken 10 times.
✗ Branch 1 (2→8) not taken.
10 if (stmt->dup.to) {
1437 10 root = nft_json_pack("{s:o}", "addr", expr_print_json(stmt->dup.to, octx));
1438
2/2
✓ Branch 0 (5→6) taken 4 times.
✓ Branch 1 (5→9) taken 6 times.
10 if (stmt->dup.dev)
1439 4 json_object_set_new(root, "dev",
1440 4 expr_print_json(stmt->dup.dev, octx));
1441 } else {
1442 ✗ root = json_null();
1443 }
1444 10 return nft_json_pack("{s:o}", "dup", root);
1445 }
1446
1447 148 json_t *meta_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1448 {
1449 json_t *root;
1450
1451 148 root = nft_json_pack("{s:{s:s}}", "meta",
1452 "key", meta_templates[stmt->meta.key].token);
1453 148 root = nft_json_pack("{s:o, s:o}",
1454 "key", root,
1455 "value", expr_print_json(stmt->meta.expr, octx));
1456
1457 148 return nft_json_pack("{s:o}", "mangle", root);
1458 }
1459
1460 104 json_t *log_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1461 {
1462 104 json_t *root = json_object(), *flags;
1463
1464
2/2
✓ Branch 0 (3→4) taken 16 times.
✓ Branch 1 (3→6) taken 88 times.
104 if (stmt->log.flags & STMT_LOG_PREFIX)
1465 16 json_object_set_new(root, "prefix", json_string(stmt->log.prefix));
1466
1467
2/2
✓ Branch 0 (6→7) taken 20 times.
✓ Branch 1 (6→9) taken 84 times.
104 if (stmt->log.flags & STMT_LOG_GROUP)
1468 20 json_object_set_new(root, "group",
1469 20 json_integer(stmt->log.group));
1470
2/2
✓ Branch 0 (9→10) taken 10 times.
✓ Branch 1 (9→12) taken 94 times.
104 if (stmt->log.flags & STMT_LOG_SNAPLEN)
1471 10 json_object_set_new(root, "snaplen",
1472 10 json_integer(stmt->log.snaplen));
1473
2/2
✓ Branch 0 (12→13) taken 5 times.
✓ Branch 1 (12→15) taken 99 times.
104 if (stmt->log.flags & STMT_LOG_QTHRESHOLD)
1474 5 json_object_set_new(root, "queue-threshold",
1475 5 json_integer(stmt->log.qthreshold));
1476
2/2
✓ Branch 0 (15→16) taken 84 times.
✓ Branch 1 (15→20) taken 20 times.
104 if ((stmt->log.flags & STMT_LOG_LEVEL) &&
1477
2/2
✓ Branch 0 (16→17) taken 45 times.
✓ Branch 1 (16→20) taken 39 times.
84 stmt->log.level != LOG_WARNING)
1478 45 json_object_set_new(root, "level",
1479 45 json_string(log_level(stmt->log.level)));
1480
1481 104 flags = json_array();
1482
1483
2/2
✓ Branch 0 (21→22) taken 10 times.
✓ Branch 1 (21→24) taken 94 times.
104 if ((stmt->log.logflags & NF_LOG_MASK) == NF_LOG_MASK) {
1484 10 json_array_append_new(flags, json_string("all"));
1485 } else {
1486
2/2
✓ Branch 0 (24→25) taken 8 times.
✓ Branch 1 (24→27) taken 86 times.
94 if (stmt->log.logflags & NF_LOG_TCPSEQ)
1487 8 json_array_append_new(flags,
1488 json_string("tcp sequence"));
1489
2/2
✓ Branch 0 (27→28) taken 6 times.
✓ Branch 1 (27→30) taken 88 times.
94 if (stmt->log.logflags & NF_LOG_TCPOPT)
1490 6 json_array_append_new(flags,
1491 json_string("tcp options"));
1492
2/2
✓ Branch 0 (30→31) taken 9 times.
✓ Branch 1 (30→33) taken 85 times.
94 if (stmt->log.logflags & NF_LOG_IPOPT)
1493 9 json_array_append_new(flags, json_string("ip options"));
1494
2/2
✓ Branch 0 (33→34) taken 5 times.
✓ Branch 1 (33→36) taken 89 times.
94 if (stmt->log.logflags & NF_LOG_UID)
1495 5 json_array_append_new(flags, json_string("skuid"));
1496
2/2
✓ Branch 0 (36→37) taken 4 times.
✓ Branch 1 (36→39) taken 90 times.
94 if (stmt->log.logflags & NF_LOG_MACDECODE)
1497 4 json_array_append_new(flags, json_string("ether"));
1498 }
1499 104 json_add_array_new(root, "flags", flags);
1500
1501
2/2
✓ Branch 0 (41→42) taken 15 times.
✓ Branch 1 (41→44) taken 89 times.
104 if (!json_object_size(root)) {
1502 15 json_decref(root);
1503 15 root = json_null();
1504 }
1505
1506 104 return nft_json_pack("{s:o}", "log", root);
1507 }
1508
1509 205 static json_t *nat_flags_json(uint32_t flags)
1510 {
1511 205 json_t *array = json_array();
1512
1513
2/2
✓ Branch 0 (3→4) taken 37 times.
✓ Branch 1 (3→6) taken 168 times.
205 if (flags & NF_NAT_RANGE_PROTO_RANDOM)
1514 37 json_array_append_new(array, json_string("random"));
1515
2/2
✓ Branch 0 (6→7) taken 26 times.
✓ Branch 1 (6→9) taken 179 times.
205 if (flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY)
1516 26 json_array_append_new(array, json_string("fully-random"));
1517
2/2
✓ Branch 0 (9→10) taken 38 times.
✓ Branch 1 (9→12) taken 167 times.
205 if (flags & NF_NAT_RANGE_PERSISTENT)
1518 38 json_array_append_new(array, json_string("persistent"));
1519
2/2
✓ Branch 0 (12→13) taken 3 times.
✓ Branch 1 (12→15) taken 202 times.
205 if (flags & NF_NAT_RANGE_NETMAP)
1520 3 json_array_append_new(array, json_string("netmap"));
1521 205 return array;
1522 }
1523
1524 39 static json_t *nat_type_flags_json(uint32_t type_flags)
1525 {
1526 39 json_t *array = json_array();
1527
1528
2/2
✓ Branch 0 (3→4) taken 3 times.
✓ Branch 1 (3→6) taken 36 times.
39 if (type_flags & STMT_NAT_F_PREFIX)
1529 3 json_array_append_new(array, json_string("prefix"));
1530
1531 39 return array;
1532 }
1533
1534 205 json_t *nat_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1535 {
1536 205 json_t *root = json_object();
1537 205 json_t *array = nat_flags_json(stmt->nat.flags);
1538
1539
2/2
✓ Branch 0 (4→5) taken 61 times.
✓ Branch 1 (4→9) taken 144 times.
205 switch (stmt->nat.family) {
1540 61 case NFPROTO_IPV4:
1541 case NFPROTO_IPV6:
1542 61 json_object_set_new(root, "family",
1543 61 json_string(family2str(stmt->nat.family)));
1544 61 break;
1545 }
1546
1547
2/2
✓ Branch 0 (9→10) taken 111 times.
✓ Branch 1 (9→12) taken 94 times.
205 if (stmt->nat.addr)
1548 111 json_object_set_new(root, "addr",
1549 111 expr_print_json(stmt->nat.addr, octx));
1550
1551
2/2
✓ Branch 0 (12→13) taken 45 times.
✓ Branch 1 (12→15) taken 160 times.
205 if (stmt->nat.proto)
1552 45 json_object_set_new(root, "port",
1553 45 expr_print_json(stmt->nat.proto, octx));
1554
1555 205 json_add_array_new(root, "flags", array);
1556
1557
2/2
✓ Branch 0 (16→17) taken 39 times.
✓ Branch 1 (16→19) taken 166 times.
205 if (stmt->nat.type_flags) {
1558 39 array = nat_type_flags_json(stmt->nat.type_flags);
1559
1560 39 json_add_array_new(root, "type_flags", array);
1561 }
1562
1563
2/2
✓ Branch 0 (20→21) taken 27 times.
✓ Branch 1 (20→23) taken 178 times.
205 if (!json_object_size(root)) {
1564 27 json_decref(root);
1565 27 root = json_null();
1566 }
1567
1568 205 return nft_json_pack("{s:o}", nat_etype2str(stmt->nat.type), root);
1569 }
1570
1571 110 json_t *reject_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1572 {
1573 110 json_t *root, *jexpr = NULL;
1574 110 const char *type = NULL;
1575
1576
3/4
✓ Branch 0 (2→3) taken 11 times.
✓ Branch 1 (2→4) taken 32 times.
✓ Branch 2 (2→6) taken 67 times.
✗ Branch 3 (2→11) not taken.
110 switch (stmt->reject.type) {
1577 11 case NFT_REJECT_TCP_RST:
1578 11 type = "tcp reset";
1579 11 break;
1580 32 case NFT_REJECT_ICMPX_UNREACH:
1581 32 type = "icmpx";
1582 32 jexpr = expr_print_json(stmt->reject.expr, octx);
1583 32 break;
1584 67 case NFT_REJECT_ICMP_UNREACH:
1585
2/3
✓ Branch 0 (6→7) taken 37 times.
✓ Branch 1 (6→9) taken 30 times.
✗ Branch 2 (6→11) not taken.
67 switch (stmt->reject.family) {
1586 37 case NFPROTO_IPV4:
1587 37 type = "icmp";
1588 37 jexpr = expr_print_json(stmt->reject.expr, octx);
1589 37 break;
1590 30 case NFPROTO_IPV6:
1591 30 type = "icmpv6";
1592 30 jexpr = expr_print_json(stmt->reject.expr, octx);
1593 30 break;
1594 }
1595 }
1596
1597
1/4
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→15) taken 110 times.
✗ Branch 2 (12→13) not taken.
✗ Branch 3 (12→15) not taken.
110 if (!type && !jexpr)
1598 ✗ return nft_json_pack("{s:n}", "reject");
1599
1600 110 root = json_object();
1601
1/2
✓ Branch 0 (16→17) taken 110 times.
✗ Branch 1 (16→19) not taken.
110 if (type)
1602 110 json_object_set_new(root, "type", json_string(type));
1603
2/2
✓ Branch 0 (19→20) taken 99 times.
✓ Branch 1 (19→21) taken 11 times.
110 if (jexpr)
1604 99 json_object_set_new(root, "expr", jexpr);
1605
1606 110 return nft_json_pack("{s:o}", "reject", root);
1607 }
1608
1609 238 json_t *counter_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1610 {
1611
2/2
✓ Branch 0 (3→4) taken 91 times.
✓ Branch 1 (3→6) taken 147 times.
238 if (nft_output_stateless(octx))
1612 91 return nft_json_pack("{s:n}", "counter");
1613
1614 147 return nft_json_pack("{s:{s:I, s:I}}", "counter",
1615 "packets", stmt->counter.packets,
1616 "bytes", stmt->counter.bytes);
1617 }
1618
1619 17 json_t *last_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1620 {
1621
3/4
✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→5) taken 13 times.
✓ Branch 2 (4→5) taken 4 times.
✗ Branch 3 (4→7) not taken.
17 if (nft_output_stateless(octx) || stmt->last.set == 0)
1622 17 return nft_json_pack("{s:n}", "last");
1623
1624 ✗ return nft_json_pack("{s:{s:I}}", "last", "used", stmt->last.used);
1625 }
1626
1627 46 json_t *set_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1628 {
1629 json_t *root;
1630
1631 46 root = nft_json_pack("{s:s, s:o, s:s+}",
1632 "op", set_stmt_op_names[stmt->set.op],
1633 "elem", expr_print_json(stmt->set.key, octx),
1634 "set", "@", stmt->set.set->set->handle.set.name);
1635
1636
2/2
✓ Branch 0 (5→6) taken 10 times.
✓ Branch 1 (5→8) taken 36 times.
46 if (!list_empty(&stmt->set.stmt_list)) {
1637 10 json_object_set_new(root, "stmt",
1638 set_stmt_list_json(&stmt->set.stmt_list,
1639 octx));
1640 }
1641
1642 46 return nft_json_pack("{s:o}", "set", root);
1643 }
1644
1645 26 json_t *map_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1646 {
1647 json_t *root;
1648
1649 26 root = nft_json_pack("{s:s, s:o, s:o, s:s+}",
1650 "op", set_stmt_op_names[stmt->map.op],
1651 "elem", expr_print_json(stmt->map.key, octx),
1652 "data", expr_print_json(stmt->map.data, octx),
1653 "map", "@", stmt->map.set->set->handle.set.name);
1654
1655
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 26 times.
26 if (!list_empty(&stmt->map.stmt_list)) {
1656 ✗ json_object_set_new(root, "stmt",
1657 set_stmt_list_json(&stmt->map.stmt_list,
1658 octx));
1659 }
1660
1661 26 return nft_json_pack("{s:o}", "map", root);
1662 }
1663
1664 45 json_t *objref_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1665 {
1666 const char *name;
1667
1668
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 45 times.
45 if (stmt->objref.type > NFT_OBJECT_MAX)
1669 ✗ name = "unknown";
1670 else
1671 45 name = objref_type_name(stmt->objref.type);
1672
1673 45 return nft_json_pack("{s:o}", name, expr_print_json(stmt->objref.expr, octx));
1674 }
1675
1676 ✗ json_t *meter_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1677 {
1678 ✗ unsigned int flags = octx->flags;
1679 json_t *root, *tmp;
1680
1681 ✗ octx->flags |= NFT_CTX_OUTPUT_STATELESS;
1682 ✗ tmp = stmt_print_json(stmt->meter.stmt, octx);
1683 ✗ octx->flags = flags;
1684
1685 ✗ root = nft_json_pack("{s:o, s:o, s:i}",
1686 "key", expr_print_json(stmt->meter.key, octx),
1687 "stmt", tmp,
1688 "size", stmt->meter.size);
1689 ✗ if (stmt->meter.set) {
1690 ✗ tmp = json_string(stmt->meter.set->set->handle.set.name);
1691 ✗ json_object_set_new(root, "name", tmp);
1692 }
1693
1694 ✗ return nft_json_pack("{s:o}", "meter", root);
1695 }
1696
1697 82 json_t *queue_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1698 {
1699 json_t *root, *flags;
1700
1701 82 root = json_object();
1702
1703
1/2
✓ Branch 0 (3→4) taken 82 times.
✗ Branch 1 (3→6) not taken.
82 if (stmt->queue.queue)
1704 82 json_object_set_new(root, "num",
1705 82 expr_print_json(stmt->queue.queue, octx));
1706
1707 82 flags = json_array();
1708
2/2
✓ Branch 0 (7→8) taken 36 times.
✓ Branch 1 (7→10) taken 46 times.
82 if (stmt->queue.flags & NFT_QUEUE_FLAG_BYPASS)
1709 36 json_array_append_new(flags, json_string("bypass"));
1710
2/2
✓ Branch 0 (10→11) taken 15 times.
✓ Branch 1 (10→13) taken 67 times.
82 if (stmt->queue.flags & NFT_QUEUE_FLAG_CPU_FANOUT)
1711 15 json_array_append_new(flags, json_string("fanout"));
1712 82 json_add_array_new(root, "flags", flags);
1713
1714
1/2
✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→18) taken 82 times.
82 if (!json_object_size(root)) {
1715 ✗ json_decref(root);
1716 ✗ root = json_null();
1717 }
1718
1719 82 return nft_json_pack("{s:o}", "queue", root);
1720 }
1721
1722 1381 json_t *verdict_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1723 {
1724 1381 return expr_print_json(stmt->expr, octx);
1725 }
1726
1727 12 json_t *connlimit_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1728 {
1729 12 json_t *root = nft_json_pack("{s:i}", "val", stmt->connlimit.count);
1730
1731
2/2
✓ Branch 0 (3→4) taken 9 times.
✓ Branch 1 (3→6) taken 3 times.
12 if (stmt->connlimit.flags & NFT_CONNLIMIT_F_INV)
1732 9 json_object_set_new(root, "inv", json_true());
1733
1734 12 return nft_json_pack("{s:o}", "ct count", root);
1735 }
1736
1737 23 json_t *tproxy_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1738 {
1739 23 json_t *tmp, *root = json_object();
1740
1741
2/2
✓ Branch 0 (3→4) taken 10 times.
✓ Branch 1 (3→8) taken 13 times.
23 if (stmt->tproxy.table_family == NFPROTO_INET &&
1742
2/2
✓ Branch 0 (4→5) taken 8 times.
✓ Branch 1 (4→8) taken 2 times.
10 stmt->tproxy.family != NFPROTO_UNSPEC) {
1743 8 tmp = json_string(family2str(stmt->tproxy.family));
1744 8 json_object_set_new(root, "family", tmp);
1745 }
1746
1747
2/2
✓ Branch 0 (8→9) taken 13 times.
✓ Branch 1 (8→11) taken 10 times.
23 if (stmt->tproxy.addr) {
1748 13 tmp = expr_print_json(stmt->tproxy.addr, octx);
1749 13 json_object_set_new(root, "addr", tmp);
1750 }
1751
1752
2/2
✓ Branch 0 (11→12) taken 17 times.
✓ Branch 1 (11→14) taken 6 times.
23 if (stmt->tproxy.port) {
1753 17 tmp = expr_print_json(stmt->tproxy.port, octx);
1754 17 json_object_set_new(root, "port", tmp);
1755 }
1756
1757 23 return nft_json_pack("{s:o}", "tproxy", root);
1758 }
1759
1760 21 json_t *synproxy_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1761 {
1762 21 json_t *root = json_object(), *flags = json_array();
1763
1764
2/2
✓ Branch 0 (4→5) taken 6 times.
✓ Branch 1 (4→7) taken 15 times.
21 if (stmt->synproxy.flags & NF_SYNPROXY_OPT_MSS)
1765 6 json_object_set_new(root, "mss",
1766 6 json_integer(stmt->synproxy.mss));
1767
2/2
✓ Branch 0 (7→8) taken 6 times.
✓ Branch 1 (7→10) taken 15 times.
21 if (stmt->synproxy.flags & NF_SYNPROXY_OPT_WSCALE)
1768 6 json_object_set_new(root, "wscale",
1769 6 json_integer(stmt->synproxy.wscale));
1770
2/2
✓ Branch 0 (10→11) taken 10 times.
✓ Branch 1 (10→13) taken 11 times.
21 if (stmt->synproxy.flags & NF_SYNPROXY_OPT_TIMESTAMP)
1771 10 json_array_append_new(flags, json_string("timestamp"));
1772
2/2
✓ Branch 0 (13→14) taken 12 times.
✓ Branch 1 (13→16) taken 9 times.
21 if (stmt->synproxy.flags & NF_SYNPROXY_OPT_SACK_PERM)
1773 12 json_array_append_new(flags, json_string("sack-perm"));
1774
1775 21 json_add_array_new(root, "flags", flags);
1776
1777
2/2
✓ Branch 0 (18→19) taken 3 times.
✓ Branch 1 (18→21) taken 18 times.
21 if (!json_object_size(root)) {
1778 3 json_decref(root);
1779 3 root = json_null();
1780 }
1781
1782 21 return nft_json_pack("{s:o}", "synproxy", root);
1783 }
1784
1785 10 json_t *optstrip_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1786 {
1787 10 return nft_json_pack("{s:o}", "reset",
1788 expr_print_json(stmt->optstrip.expr, octx));
1789 }
1790
1791 ✗ json_t *xt_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
1792 {
1793 static const char *xt_typename[NFT_XT_MAX] = {
1794 [NFT_XT_MATCH] = "match",
1795 [NFT_XT_TARGET] = "target",
1796 [NFT_XT_WATCHER] = "watcher",
1797 };
1798
1799 ✗ return nft_json_pack("{s:{s:s, s:s}}", "xt",
1800 "type", xt_typename[stmt->xt.type],
1801 "name", stmt->xt.name);
1802 }
1803
1804 6333 static json_t *table_print_json_full(struct netlink_ctx *ctx,
1805 struct table *table)
1806 {
1807 6333 json_t *root = json_array(), *rules = json_array(), *tmp;
1808 struct flowtable *flowtable;
1809 struct chain *chain;
1810 struct rule *rule;
1811 struct obj *obj;
1812 struct set *set;
1813
1814 6333 tmp = table_print_json(table);
1815 6333 json_array_append_new(root, tmp);
1816
1817 /* both maps and rules may refer to chains, list them first */
1818
2/2
✓ Branch 0 (10→7) taken 8650 times.
✓ Branch 1 (10→11) taken 6333 times.
14983 list_for_each_entry(chain, &table->chain_cache.list, cache.list) {
1819 8650 tmp = chain_print_json(chain);
1820 8650 json_array_append_new(root, tmp);
1821 }
1822
2/2
✓ Branch 0 (15→12) taken 135 times.
✓ Branch 1 (15→16) taken 6333 times.
6468 list_for_each_entry(obj, &table->obj_cache.list, cache.list) {
1823 135 tmp = obj_print_json(&ctx->nft->output, obj, false);
1824 135 json_array_append_new(root, tmp);
1825 }
1826
2/2
✓ Branch 0 (23→17) taken 3271 times.
✓ Branch 1 (23→24) taken 6333 times.
9604 list_for_each_entry(set, &table->set_cache.list, cache.list) {
1827
2/2
✓ Branch 0 (18→19) taken 1604 times.
✓ Branch 1 (18→20) taken 1667 times.
3271 if (set_is_anonymous(set->flags))
1828 1604 continue;
1829 1667 tmp = set_print_json(&ctx->nft->output, set);
1830 1667 json_array_append_new(root, tmp);
1831 }
1832
2/2
✓ Branch 0 (28→25) taken 16 times.
✓ Branch 1 (28→29) taken 6333 times.
6349 list_for_each_entry(flowtable, &table->ft_cache.list, cache.list) {
1833 16 tmp = flowtable_print_json(flowtable);
1834 16 json_array_append_new(root, tmp);
1835 }
1836
2/2
✓ Branch 0 (36→30) taken 8650 times.
✓ Branch 1 (36→37) taken 6333 times.
14983 list_for_each_entry(chain, &table->chain_cache.list, cache.list) {
1837
2/2
✓ Branch 0 (34→31) taken 26906 times.
✓ Branch 1 (34→35) taken 8650 times.
35556 list_for_each_entry(rule, &chain->rules, list) {
1838 26906 tmp = rule_print_json(&ctx->nft->output, rule);
1839 26906 json_array_append_new(rules, tmp);
1840 }
1841 }
1842
1843 6333 json_array_extend_new(root, rules);
1844
1845 6333 return root;
1846 }
1847
1848 513 static json_t *do_list_ruleset_json(struct netlink_ctx *ctx, struct cmd *cmd)
1849 {
1850 513 unsigned int family = cmd->handle.family;
1851 513 json_t *root = json_array();
1852 struct table *table;
1853
1854
2/2
✓ Branch 0 (10→4) taken 495 times.
✓ Branch 1 (10→11) taken 513 times.
1008 list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
1855
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 495 times.
495 if (family != NFPROTO_UNSPEC &&
1856 ✗ table->handle.family != family)
1857 ✗ continue;
1858
1859 495 json_array_extend_new(root, table_print_json_full(ctx, table));
1860 }
1861
1862 513 return root;
1863 }
1864
1865 ✗ static json_t *do_list_tables_json(struct netlink_ctx *ctx, struct cmd *cmd)
1866 {
1867 ✗ unsigned int family = cmd->handle.family;
1868 ✗ json_t *root = json_array();
1869 struct table *table;
1870
1871 ✗ list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
1872 ✗ if (family != NFPROTO_UNSPEC &&
1873 ✗ table->handle.family != family)
1874 ✗ continue;
1875
1876 ✗ json_array_append_new(root, table_print_json(table));
1877 }
1878
1879 ✗ return root;
1880 }
1881
1882 5838 static json_t *do_list_table_json(struct netlink_ctx *ctx,
1883 struct cmd *cmd, struct table *table)
1884 {
1885 5838 return table_print_json_full(ctx, table);
1886 }
1887
1888 ✗ static json_t *do_list_chain_json(struct netlink_ctx *ctx,
1889 struct cmd *cmd, struct table *table)
1890 {
1891 ✗ json_t *root = json_array();
1892 struct chain *chain;
1893 struct rule *rule;
1894
1895 ✗ list_for_each_entry(chain, &table->chain_cache.list, cache.list) {
1896 ✗ if (chain->handle.family != cmd->handle.family ||
1897 ✗ strcmp(cmd->handle.chain.name, chain->handle.chain.name))
1898 ✗ continue;
1899
1900 ✗ json_array_append_new(root, chain_print_json(chain));
1901
1902 ✗ list_for_each_entry(rule, &chain->rules, list) {
1903 ✗ json_t *tmp = rule_print_json(&ctx->nft->output, rule);
1904
1905 ✗ json_array_append_new(root, tmp);
1906 }
1907 }
1908
1909 ✗ return root;
1910 }
1911
1912 ✗ static json_t *do_list_chains_json(struct netlink_ctx *ctx, struct cmd *cmd)
1913 {
1914 ✗ json_t *root = json_array();
1915 struct table *table;
1916 struct chain *chain;
1917
1918 ✗ list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
1919 ✗ if (cmd->handle.family != NFPROTO_UNSPEC &&
1920 ✗ cmd->handle.family != table->handle.family)
1921 ✗ continue;
1922
1923 ✗ list_for_each_entry(chain, &table->chain_cache.list, cache.list) {
1924 ✗ json_t *tmp = chain_print_json(chain);
1925
1926 ✗ json_array_append_new(root, tmp);
1927 }
1928 }
1929
1930 ✗ return root;
1931 }
1932
1933 ✗ static json_t *do_list_set_json(struct netlink_ctx *ctx,
1934 struct cmd *cmd, struct table *table)
1935 {
1936 ✗ struct set *set = cmd->set;
1937
1938 ✗ if (!set) {
1939 ✗ set = set_cache_find(table, cmd->handle.set.name);
1940 ✗ if (set == NULL)
1941 ✗ return json_null();
1942 }
1943
1944 ✗ return nft_json_pack("[o]", set_print_json(&ctx->nft->output, set));
1945 }
1946
1947 ✗ static json_t *do_list_sets_json(struct netlink_ctx *ctx, struct cmd *cmd)
1948 {
1949 ✗ struct output_ctx *octx = &ctx->nft->output;
1950 ✗ json_t *root = json_array();
1951 struct table *table;
1952 struct set *set;
1953
1954 ✗ list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
1955 ✗ if (cmd->handle.family != NFPROTO_UNSPEC &&
1956 ✗ cmd->handle.family != table->handle.family)
1957 ✗ continue;
1958
1959 ✗ list_for_each_entry(set, &table->set_cache.list, cache.list) {
1960 ✗ if (cmd->obj == CMD_OBJ_SETS &&
1961 ✗ !set_is_literal(set->flags))
1962 ✗ continue;
1963 ✗ if (cmd->obj == CMD_OBJ_METERS &&
1964 ✗ !set_is_meter(set->flags))
1965 ✗ continue;
1966 ✗ if (cmd->obj == CMD_OBJ_MAPS &&
1967 ✗ !map_is_literal(set->flags))
1968 ✗ continue;
1969 ✗ json_array_append_new(root, set_print_json(octx, set));
1970 }
1971 }
1972
1973 ✗ return root;
1974 }
1975
1976 ✗ static json_t *do_list_obj_json(struct netlink_ctx *ctx,
1977 struct cmd *cmd, uint32_t type)
1978 {
1979 ✗ json_t *root = json_array(), *tmp;
1980 struct table *table;
1981 struct obj *obj;
1982
1983 ✗ list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
1984 ✗ if (cmd->handle.family != NFPROTO_UNSPEC &&
1985 ✗ cmd->handle.family != table->handle.family)
1986 ✗ continue;
1987
1988 ✗ if (cmd->handle.table.name &&
1989 ✗ strcmp(cmd->handle.table.name, table->handle.table.name))
1990 ✗ continue;
1991
1992 ✗ list_for_each_entry(obj, &table->obj_cache.list, cache.list) {
1993 ✗ if (obj->type != type ||
1994 ✗ (cmd->handle.obj.name &&
1995 ✗ strcmp(cmd->handle.obj.name, obj->handle.obj.name)))
1996 ✗ continue;
1997
1998 ✗ tmp = obj_print_json(&ctx->nft->output, obj, false);
1999 ✗ json_array_append_new(root, tmp);
2000 }
2001 }
2002
2003 ✗ return root;
2004 }
2005
2006 ✗ static json_t *do_list_flowtable_json(struct netlink_ctx *ctx,
2007 struct cmd *cmd, struct table *table)
2008 {
2009 ✗ json_t *root = json_array();
2010 struct flowtable *ft;
2011
2012 ✗ ft = ft_cache_find(table, cmd->handle.flowtable.name);
2013 ✗ if (!ft)
2014 ✗ return json_null();
2015
2016 ✗ json_array_append_new(root, flowtable_print_json(ft));
2017
2018 ✗ return root;
2019 }
2020
2021 ✗ static json_t *do_list_flowtables_json(struct netlink_ctx *ctx, struct cmd *cmd)
2022 {
2023 ✗ json_t *root = json_array(), *tmp;
2024 struct flowtable *flowtable;
2025 struct table *table;
2026
2027 ✗ list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
2028 ✗ if (cmd->handle.family != NFPROTO_UNSPEC &&
2029 ✗ cmd->handle.family != table->handle.family)
2030 ✗ continue;
2031
2032 ✗ list_for_each_entry(flowtable, &table->ft_cache.list, cache.list) {
2033 ✗ tmp = flowtable_print_json(flowtable);
2034 ✗ json_array_append_new(root, tmp);
2035 }
2036 }
2037
2038 ✗ return root;
2039 }
2040
2041 6351 static json_t *generate_json_metainfo(void)
2042 {
2043 6351 return nft_json_pack("{s: {s:s, s:s, s:i}}", "metainfo",
2044 "version", PACKAGE_VERSION,
2045 "release_name", RELEASE_NAME,
2046 "json_schema_version", JSON_SCHEMA_VERSION);
2047 }
2048
2049 6352 int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd)
2050 {
2051 6352 struct table *table = NULL;
2052 6352 json_t *root = NULL;
2053
2054
2/2
✓ Branch 0 (2→3) taken 5839 times.
✓ Branch 1 (2→6) taken 513 times.
6352 if (cmd->handle.table.name) {
2055 5839 table = table_cache_find(&ctx->nft->cache.table_cache,
2056 cmd->handle.table.name,
2057 cmd->handle.family);
2058
2/2
✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→6) taken 5838 times.
5839 if (!table) {
2059 1 errno = ENOENT;
2060 1 return -1;
2061 }
2062 }
2063
2064
2/25
✓ Branch 0 (6→7) taken 5838 times.
✗ Branch 1 (6→11) not taken.
✗ Branch 2 (6→13) not taken.
✗ Branch 3 (6→15) not taken.
✗ Branch 4 (6→17) not taken.
✓ Branch 5 (6→19) taken 513 times.
✗ Branch 6 (6→21) not taken.
✗ Branch 7 (6→23) not taken.
✗ Branch 8 (6→25) not taken.
✗ Branch 9 (6→27) not taken.
✗ Branch 10 (6→29) not taken.
✗ Branch 11 (6→31) not taken.
✗ Branch 12 (6→33) not taken.
✗ Branch 13 (6→35) not taken.
✗ Branch 14 (6→36) not taken.
✗ Branch 15 (6→38) not taken.
✗ Branch 16 (6→40) not taken.
✗ Branch 17 (6→42) not taken.
✗ Branch 18 (6→44) not taken.
✗ Branch 19 (6→46) not taken.
✗ Branch 20 (6→48) not taken.
✗ Branch 21 (6→50) not taken.
✗ Branch 22 (6→51) not taken.
✗ Branch 23 (6→52) not taken.
✗ Branch 24 (6→55) not taken.
6351 switch (cmd->obj) {
2065 5838 case CMD_OBJ_TABLE:
2066
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 5838 times.
5838 if (!cmd->handle.table.name) {
2067 ✗ root = do_list_tables_json(ctx, cmd);
2068 ✗ break;
2069 }
2070 5838 root = do_list_table_json(ctx, cmd, table);
2071 5838 break;
2072 ✗ case CMD_OBJ_CHAIN:
2073 ✗ root = do_list_chain_json(ctx, cmd, table);
2074 ✗ break;
2075 ✗ case CMD_OBJ_CHAINS:
2076 ✗ root = do_list_chains_json(ctx, cmd);
2077 ✗ break;
2078 ✗ case CMD_OBJ_SETS:
2079 ✗ root = do_list_sets_json(ctx, cmd);
2080 ✗ break;
2081 ✗ case CMD_OBJ_SET:
2082 ✗ root = do_list_set_json(ctx, cmd, table);
2083 ✗ break;
2084 513 case CMD_OBJ_RULES:
2085 case CMD_OBJ_RULESET:
2086 513 root = do_list_ruleset_json(ctx, cmd);
2087 513 break;
2088 ✗ case CMD_OBJ_METERS:
2089 ✗ root = do_list_sets_json(ctx, cmd);
2090 ✗ break;
2091 ✗ case CMD_OBJ_METER:
2092 ✗ root = do_list_set_json(ctx, cmd, table);
2093 ✗ break;
2094 ✗ case CMD_OBJ_MAPS:
2095 ✗ root = do_list_sets_json(ctx, cmd);
2096 ✗ break;
2097 ✗ case CMD_OBJ_MAP:
2098 ✗ root = do_list_set_json(ctx, cmd, table);
2099 ✗ break;
2100 ✗ case CMD_OBJ_COUNTER:
2101 case CMD_OBJ_COUNTERS:
2102 ✗ root = do_list_obj_json(ctx, cmd, NFT_OBJECT_COUNTER);
2103 ✗ break;
2104 ✗ case CMD_OBJ_QUOTA:
2105 case CMD_OBJ_QUOTAS:
2106 ✗ root = do_list_obj_json(ctx, cmd, NFT_OBJECT_QUOTA);
2107 ✗ break;
2108 ✗ case CMD_OBJ_CT_HELPER:
2109 case CMD_OBJ_CT_HELPERS:
2110 ✗ root = do_list_obj_json(ctx, cmd, NFT_OBJECT_CT_HELPER);
2111 ✗ break;
2112 ✗ case CMD_OBJ_CT_TIMEOUT:
2113 case CMD_OBJ_CT_TIMEOUTS:
2114 ✗ root = do_list_obj_json(ctx, cmd, NFT_OBJECT_CT_TIMEOUT);
2115 ✗ case CMD_OBJ_CT_EXPECT:
2116 case CMD_OBJ_CT_EXPECTATIONS:
2117 ✗ root = do_list_obj_json(ctx, cmd, NFT_OBJECT_CT_EXPECT);
2118 ✗ break;
2119 ✗ case CMD_OBJ_LIMIT:
2120 case CMD_OBJ_LIMITS:
2121 ✗ root = do_list_obj_json(ctx, cmd, NFT_OBJECT_LIMIT);
2122 ✗ break;
2123 ✗ case CMD_OBJ_SECMARK:
2124 case CMD_OBJ_SECMARKS:
2125 ✗ root = do_list_obj_json(ctx, cmd, NFT_OBJECT_SECMARK);
2126 ✗ break;
2127 ✗ case CMD_OBJ_SYNPROXY:
2128 case CMD_OBJ_SYNPROXYS:
2129 ✗ root = do_list_obj_json(ctx, cmd, NFT_OBJECT_SYNPROXY);
2130 ✗ break;
2131 ✗ case CMD_OBJ_TUNNEL:
2132 case CMD_OBJ_TUNNELS:
2133 ✗ root = do_list_obj_json(ctx, cmd, NFT_OBJECT_TUNNEL);
2134 ✗ break;
2135 ✗ case CMD_OBJ_FLOWTABLE:
2136 ✗ root = do_list_flowtable_json(ctx, cmd, table);
2137 ✗ break;
2138 ✗ case CMD_OBJ_FLOWTABLES:
2139 ✗ root = do_list_flowtables_json(ctx, cmd);
2140 ✗ break;
2141 ✗ case CMD_OBJ_HOOKS:
2142 ✗ return 0;
2143 ✗ case CMD_OBJ_MONITOR:
2144 case CMD_OBJ_MARKUP:
2145 case CMD_OBJ_SETELEMS:
2146 case CMD_OBJ_RULE:
2147 case CMD_OBJ_EXPR:
2148 case CMD_OBJ_ELEMENTS:
2149 ✗ errno = EOPNOTSUPP;
2150 ✗ return -1;
2151 ✗ case CMD_OBJ_INVALID:
2152 ✗ BUG("invalid command object type %u", cmd->obj);
2153 break;
2154 }
2155
2156
2/4
✓ Branch 0 (55→56) taken 6351 times.
✗ Branch 1 (55→57) not taken.
✗ Branch 2 (56→57) not taken.
✓ Branch 3 (56→60) taken 6351 times.
6351 if (!json_is_array(root)) {
2157 ✗ json_t *tmp = json_array();
2158
2159 ✗ json_array_append_new(tmp, root);
2160 ✗ root = tmp;
2161 }
2162
2163 6351 json_array_insert_new(root, 0, generate_json_metainfo());
2164
2165 6351 root = nft_json_pack("{s:o}", "nftables", root);
2166 6351 json_dumpf(root, ctx->nft->output.output_fp, 0);
2167 6351 json_decref(root);
2168 6351 fprintf(ctx->nft->output.output_fp, "\n");
2169 6351 fflush(ctx->nft->output.output_fp);
2170 6351 return 0;
2171 }
2172
2173 64 static void monitor_print_json(struct netlink_mon_handler *monh,
2174 const char *cmd, json_t *obj)
2175 {
2176 64 struct nft_ctx *nft = monh->ctx->nft;
2177
2178 64 obj = nft_json_pack("{s:o}", cmd, obj);
2179
2/4
✓ Branch 0 (4→5) taken 64 times.
✗ Branch 1 (4→7) not taken.
✓ Branch 2 (5→6) taken 64 times.
✗ Branch 3 (5→7) not taken.
64 if (nft_output_echo(&nft->output) && !nft->json_root) {
2180 64 json_array_append_new(nft->json_echo, obj);
2181 } else {
2182 ✗ json_dumpf(obj, nft->output.output_fp, 0);
2183 ✗ json_decref(obj);
2184 }
2185 64 }
2186
2187 13 void monitor_print_table_json(struct netlink_mon_handler *monh,
2188 const char *cmd, struct table *t)
2189 {
2190 13 monitor_print_json(monh, cmd, table_print_json(t));
2191 13 }
2192
2193 7 void monitor_print_chain_json(struct netlink_mon_handler *monh,
2194 const char *cmd, struct chain *c)
2195 {
2196 7 monitor_print_json(monh, cmd, chain_print_json(c));
2197 7 }
2198
2199 9 void monitor_print_set_json(struct netlink_mon_handler *monh,
2200 const char *cmd, struct set *s)
2201 {
2202 9 struct output_ctx *octx = &monh->ctx->nft->output;
2203
2204 9 monitor_print_json(monh, cmd, set_print_json(octx, s));
2205 9 }
2206
2207 19 void monitor_print_element_json(struct netlink_mon_handler *monh,
2208 const char *cmd, struct set *s)
2209 {
2210 19 struct output_ctx *octx = &monh->ctx->nft->output;
2211
2212 19 monitor_print_json(monh, cmd, element_print_json(octx, s));
2213 19 }
2214
2215 6 void monitor_print_obj_json(struct netlink_mon_handler *monh,
2216 const char *cmd, struct obj *o, bool delete)
2217 {
2218 6 struct output_ctx *octx = &monh->ctx->nft->output;
2219
2220 6 monitor_print_json(monh, cmd, obj_print_json(octx, o, delete));
2221 6 }
2222
2223 2 void monitor_print_flowtable_json(struct netlink_mon_handler *monh,
2224 const char *cmd, struct flowtable *ft)
2225 {
2226 2 monitor_print_json(monh, cmd, flowtable_print_json(ft));
2227 2 }
2228
2229 8 void monitor_print_rule_json(struct netlink_mon_handler *monh,
2230 const char *cmd, struct rule *r)
2231 {
2232 8 struct output_ctx *octx = &monh->ctx->nft->output;
2233
2234 8 monitor_print_json(monh, cmd, rule_print_json(octx, r));
2235 8 }
2236
2237 43 void json_alloc_echo(struct nft_ctx *nft)
2238 {
2239 43 nft->json_echo = json_array();
2240
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 43 times.
43 if (!nft->json_echo)
2241 ✗ memory_allocation_error();
2242 43 }
2243