GCC Code Coverage Report


Directory: ./
File: src/rule.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 1459 1650 88.4%
Functions: 117 124 94.4%
Branches: 720 907 79.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2008-2012 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11 #include <nft.h>
12
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <inttypes.h>
16 #include <errno.h>
17
18 #include <statement.h>
19 #include <rule.h>
20 #include <utils.h>
21 #include <netdb.h>
22 #include <netlink.h>
23 #include <mnl.h>
24 #include <misspell.h>
25 #include <json.h>
26 #include <cache.h>
27 #include <owner.h>
28 #include <intervals.h>
29 #include "nftutils.h"
30
31 #include <libnftnl/common.h>
32 #include <libnftnl/ruleset.h>
33 #include <netinet/ip.h>
34 #include <linux/netfilter.h>
35 #include <linux/netfilter_arp.h>
36 #include <linux/netfilter_ipv4.h>
37 #include <linux/netfilter/nf_synproxy.h>
38 #include <net/if.h>
39 #include <linux/netfilter_bridge.h>
40
41 static const char *const tcp_state_to_name[] = {
42 [NFTNL_CTTIMEOUT_TCP_SYN_SENT] = "syn_sent",
43 [NFTNL_CTTIMEOUT_TCP_SYN_RECV] = "syn_recv",
44 [NFTNL_CTTIMEOUT_TCP_ESTABLISHED] = "established",
45 [NFTNL_CTTIMEOUT_TCP_FIN_WAIT] = "fin_wait",
46 [NFTNL_CTTIMEOUT_TCP_CLOSE_WAIT] = "close_wait",
47 [NFTNL_CTTIMEOUT_TCP_LAST_ACK] = "last_ack",
48 [NFTNL_CTTIMEOUT_TCP_TIME_WAIT] = "time_wait",
49 [NFTNL_CTTIMEOUT_TCP_CLOSE] = "close",
50 [NFTNL_CTTIMEOUT_TCP_SYN_SENT2] = "syn_sent2",
51 [NFTNL_CTTIMEOUT_TCP_RETRANS] = "retrans",
52 [NFTNL_CTTIMEOUT_TCP_UNACK] = "unack",
53 };
54
55 static const char *const udp_state_to_name[] = {
56 [NFTNL_CTTIMEOUT_UDP_UNREPLIED] = "unreplied",
57 [NFTNL_CTTIMEOUT_UDP_REPLIED] = "replied",
58 };
59
60 static uint32_t tcp_dflt_timeout[] = {
61 [NFTNL_CTTIMEOUT_TCP_SYN_SENT] = 120,
62 [NFTNL_CTTIMEOUT_TCP_SYN_RECV] = 60,
63 [NFTNL_CTTIMEOUT_TCP_ESTABLISHED] = 432000,
64 [NFTNL_CTTIMEOUT_TCP_FIN_WAIT] = 120,
65 [NFTNL_CTTIMEOUT_TCP_CLOSE_WAIT] = 60,
66 [NFTNL_CTTIMEOUT_TCP_LAST_ACK] = 30,
67 [NFTNL_CTTIMEOUT_TCP_TIME_WAIT] = 120,
68 [NFTNL_CTTIMEOUT_TCP_CLOSE] = 10,
69 [NFTNL_CTTIMEOUT_TCP_SYN_SENT2] = 120,
70 [NFTNL_CTTIMEOUT_TCP_RETRANS] = 300,
71 [NFTNL_CTTIMEOUT_TCP_UNACK] = 300,
72 };
73
74 static uint32_t udp_dflt_timeout[] = {
75 [NFTNL_CTTIMEOUT_UDP_UNREPLIED] = 30,
76 [NFTNL_CTTIMEOUT_UDP_REPLIED] = 120,
77 };
78
79 struct timeout_protocol timeout_protocol[UINT8_MAX + 1] = {
80 [IPPROTO_TCP] = {
81 .array_size = NFTNL_CTTIMEOUT_TCP_MAX,
82 .state_to_name = tcp_state_to_name,
83 .dflt_timeout = tcp_dflt_timeout,
84 },
85 [IPPROTO_UDP] = {
86 .array_size = NFTNL_CTTIMEOUT_UDP_MAX,
87 .state_to_name = udp_state_to_name,
88 .dflt_timeout = udp_dflt_timeout,
89 },
90 };
91
92 41 int timeout_str2num(uint16_t l4proto, struct timeout_state *ts)
93 {
94 unsigned int i;
95
96
2/2
✓ Branch 0 (6→3) taken 150 times.
✓ Branch 1 (6→7) taken 2 times.
152 for (i = 0; i < timeout_protocol[l4proto].array_size; i++) {
97
2/2
✓ Branch 0 (3→4) taken 39 times.
✓ Branch 1 (3→5) taken 111 times.
150 if (!strcmp(timeout_protocol[l4proto].state_to_name[i], ts->timeout_str)) {
98 39 ts->timeout_index = i;
99 39 return 0;
100 }
101 }
102 2 return -1;
103 }
104
105 708777 void handle_free(struct handle *h)
106 {
107 708777 free_const(h->table.name);
108 708777 free_const(h->chain.name);
109 708777 free_const(h->set.name);
110 708777 free_const(h->flowtable.name);
111 708777 free_const(h->obj.name);
112 708777 }
113
114 417445 void handle_merge(struct handle *dst, const struct handle *src)
115 {
116
2/2
✓ Branch 0 (2→3) taken 370987 times.
✓ Branch 1 (2→4) taken 46458 times.
417445 if (dst->family == 0)
117 370987 dst->family = src->family;
118
4/4
✓ Branch 0 (4→5) taken 370987 times.
✓ Branch 1 (4→8) taken 46458 times.
✓ Branch 2 (5→6) taken 367212 times.
✓ Branch 3 (5→8) taken 3775 times.
417445 if (dst->table.name == NULL && src->table.name != NULL) {
119 367212 dst->table.name = xstrdup(src->table.name);
120 367212 dst->table.location = src->table.location;
121 }
122
4/4
✓ Branch 0 (8→9) taken 371761 times.
✓ Branch 1 (8→12) taken 45684 times.
✓ Branch 2 (9→10) taken 170312 times.
✓ Branch 3 (9→12) taken 201449 times.
417445 if (dst->chain.name == NULL && src->chain.name != NULL) {
123 170312 dst->chain.name = xstrdup(src->chain.name);
124 170312 dst->chain.location = src->chain.location;
125 }
126
4/4
✓ Branch 0 (12→13) taken 409477 times.
✓ Branch 1 (12→16) taken 7968 times.
✓ Branch 2 (13→14) taken 196533 times.
✓ Branch 3 (13→16) taken 212944 times.
417445 if (dst->set.name == NULL && src->set.name != NULL) {
127 196533 dst->set.name = xstrdup(src->set.name);
128 196533 dst->set.location = src->set.location;
129 }
130
4/4
✓ Branch 0 (16→17) taken 417336 times.
✓ Branch 1 (16→20) taken 109 times.
✓ Branch 2 (17→18) taken 151 times.
✓ Branch 3 (17→20) taken 417185 times.
417445 if (dst->flowtable.name == NULL && src->flowtable.name != NULL)
131 151 dst->flowtable.name = xstrdup(src->flowtable.name);
132
4/4
✓ Branch 0 (20→21) taken 416875 times.
✓ Branch 1 (20→24) taken 570 times.
✓ Branch 2 (21→22) taken 747 times.
✓ Branch 3 (21→24) taken 416128 times.
417445 if (dst->obj.name == NULL && src->obj.name != NULL)
133 747 dst->obj.name = xstrdup(src->obj.name);
134
1/2
✓ Branch 0 (24→25) taken 417445 times.
✗ Branch 1 (24→26) not taken.
417445 if (dst->handle.id == 0)
135 417445 dst->handle = src->handle;
136
1/2
✓ Branch 0 (26→27) taken 417445 times.
✗ Branch 1 (26→28) not taken.
417445 if (dst->position.id == 0)
137 417445 dst->position = src->position;
138
1/2
✓ Branch 0 (28→29) taken 417445 times.
✗ Branch 1 (28→30) not taken.
417445 if (dst->index.id == 0)
139 417445 dst->index = src->index;
140 417445 }
141
142 /* internal ID to uniquely identify a set in the batch */
143 static uint32_t set_id;
144
145 46437 struct set *set_alloc(const struct location *loc)
146 {
147 struct set *set;
148
149
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 46437 times.
46437 assert(loc);
150
151 46437 set = xzalloc(sizeof(*set));
152 46437 set->refcnt = 1;
153 46437 set->handle.set_id = ++set_id;
154 46437 set->location = *loc;
155
156 46437 init_list_head(&set->stmt_list);
157
158 46437 return set;
159 }
160
161 1885 struct set *set_clone(const struct set *set)
162 {
163 struct set *new_set;
164
165 1885 new_set = set_alloc(&internal_location);
166 1885 handle_merge(&new_set->handle, &set->handle);
167 1885 new_set->flags = set->flags;
168 1885 new_set->gc_int = set->gc_int;
169 1885 new_set->timeout = set->timeout;
170 1885 new_set->key = expr_clone(set->key);
171
2/2
✓ Branch 0 (5→6) taken 110 times.
✓ Branch 1 (5→8) taken 1775 times.
1885 if (set->data)
172 110 new_set->data = expr_clone(set->data);
173 1885 new_set->objtype = set->objtype;
174 1885 new_set->policy = set->policy;
175 1885 new_set->automerge = set->automerge;
176 1885 new_set->desc = set->desc;
177 1885 init_list_head(&new_set->stmt_list);
178
179 1885 return new_set;
180 }
181
182 45993 struct set *set_get(struct set *set)
183 {
184
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 45993 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 45993 times.
45993 assert_refcount_safe(set->refcnt);
185 45993 set->refcnt++;
186 45993 return set;
187 }
188
189 92430 void set_free(struct set *set)
190 {
191 struct stmt *stmt, *next;
192
193
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 92430 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 92430 times.
92430 assert_refcount_safe(set->refcnt);
194
2/2
✓ Branch 0 (8→9) taken 45993 times.
✓ Branch 1 (8→10) taken 46437 times.
92430 if (--set->refcnt > 0)
195 45993 return;
196
197 46437 expr_free(set->init);
198
2/2
✓ Branch 0 (11→12) taken 8 times.
✓ Branch 1 (11→13) taken 46429 times.
46437 if (set->comment)
199 8 free_const(set->comment);
200 46437 handle_free(&set->handle);
201
2/2
✓ Branch 0 (17→15) taken 1967 times.
✓ Branch 1 (17→18) taken 46437 times.
48404 list_for_each_entry_safe(stmt, next, &set->stmt_list, list)
202 1967 stmt_free(stmt);
203 46437 expr_free(set->key);
204 46437 expr_free(set->data);
205 46437 free(set);
206 }
207
208 200 struct set *set_lookup_fuzzy(const char *set_name,
209 const struct nft_cache *cache,
210 const struct table **t)
211 {
212 struct string_misspell_state st;
213 struct table *table;
214 struct set *set;
215
216
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 200 times.
200 if (!set_name)
217 ✗ return NULL;
218
219 200 string_misspell_init(&st);
220
221
2/2
✓ Branch 0 (16→6) taken 248 times.
✓ Branch 1 (16→17) taken 200 times.
448 list_for_each_entry(table, &cache->table_cache.list, cache.list) {
222
2/2
✓ Branch 0 (14→7) taken 428 times.
✓ Branch 1 (14→15) taken 248 times.
676 list_for_each_entry(set, &table->set_cache.list, cache.list) {
223
2/2
✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→10) taken 427 times.
428 if (set_is_anonymous(set->flags))
224 1 continue;
225
2/2
✓ Branch 0 (11→12) taken 23 times.
✓ Branch 1 (11→13) taken 404 times.
427 if (string_misspell_update(set->handle.set.name,
226 set_name, set, &st))
227 23 *t = table;
228 }
229 }
230 200 return st.obj;
231 }
232
233 190 struct set *set_lookup_global(uint32_t family, const char *table,
234 const char *name, struct nft_cache *cache)
235 {
236 struct table *t;
237
238 190 t = table_cache_find(&cache->table_cache, table, family);
239
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 190 times.
190 if (t == NULL)
240 ✗ return NULL;
241
242 190 return set_cache_find(t, name);
243 }
244
245 struct print_fmt_options {
246 const char *tab;
247 const char *nl;
248 const char *table;
249 const char *family;
250 const char *stmt_separator;
251 };
252
253 ✗ const char *set_policy2str(uint32_t policy)
254 {
255 ✗ switch (policy) {
256 ✗ case NFT_SET_POL_PERFORMANCE:
257 ✗ return "performance";
258 ✗ case NFT_SET_POL_MEMORY:
259 ✗ return "memory";
260 ✗ default:
261 ✗ return "unknown";
262 }
263 }
264
265 5521 static void set_print_key(const struct expr *expr, struct output_ctx *octx)
266 {
267 5521 const struct datatype *dtype = expr->dtype;
268
269
3/4
✓ Branch 0 (2→3) taken 17 times.
✓ Branch 1 (2→4) taken 5504 times.
✓ Branch 2 (3→4) taken 17 times.
✗ Branch 3 (3→5) not taken.
5521 if (dtype->size || dtype->type == TYPE_VERDICT)
270 5521 nft_print(octx, "%s", dtype->name);
271 else
272 ✗ expr_print(expr, octx);
273 5521 }
274
275 4578 static void set_print_key_and_data(const struct set *set, struct output_ctx *octx)
276 {
277 4578 bool use_typeof = set->key_typeof_valid;
278
279
2/2
✓ Branch 0 (2→3) taken 126 times.
✓ Branch 1 (2→4) taken 4452 times.
4578 nft_print(octx, "%s ", use_typeof ? "typeof" : "type");
280
281
2/2
✓ Branch 0 (6→7) taken 126 times.
✓ Branch 1 (6→8) taken 4452 times.
4578 if (use_typeof)
282 126 expr_print(set->key, octx);
283 else
284 4452 set_print_key(set->key, octx);
285
286
2/2
✓ Branch 0 (10→11) taken 1136 times.
✓ Branch 1 (10→17) taken 3442 times.
4578 if (set_is_datamap(set->flags)) {
287 1136 nft_print(octx, " : ");
288
2/2
✓ Branch 0 (12→13) taken 6 times.
✓ Branch 1 (12→14) taken 1130 times.
1136 if (set->data->flags & EXPR_F_INTERVAL)
289 6 nft_print(octx, "interval ");
290
291
2/2
✓ Branch 0 (14→15) taken 67 times.
✓ Branch 1 (14→16) taken 1069 times.
1136 if (use_typeof)
292 67 expr_print(set->data, octx);
293 else
294 1069 set_print_key(set->data, octx);
295
2/2
✓ Branch 0 (18→19) taken 18 times.
✓ Branch 1 (18→21) taken 3424 times.
3442 } else if (set_is_objmap(set->flags)) {
296 18 nft_print(octx, " : %s", obj_type_name(set->objtype));
297 }
298 4578 }
299
300 4578 static void set_print_declaration(const struct set *set,
301 struct print_fmt_options *opts,
302 struct output_ctx *octx)
303 {
304 4578 const char *delim = "";
305 struct stmt *stmt;
306 const char *type;
307 uint32_t flags;
308
309
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 4578 times.
4578 if (set_is_meter(set->flags))
310 ✗ type = "meter";
311
2/2
✓ Branch 0 (6→7) taken 1154 times.
✓ Branch 1 (6→8) taken 3424 times.
4578 else if (set_is_map(set->flags))
312 1154 type = "map";
313 else
314 3424 type = "set";
315
316 4578 nft_print(octx, "%s%s", opts->tab, type);
317
318
2/2
✓ Branch 0 (10→11) taken 10 times.
✓ Branch 1 (10→12) taken 4568 times.
4578 if (opts->family != NULL)
319 10 nft_print(octx, " %s", opts->family);
320
321
2/2
✓ Branch 0 (12→13) taken 10 times.
✓ Branch 1 (12→14) taken 4568 times.
4578 if (opts->table != NULL)
322 10 nft_print(octx, " %s", opts->table);
323
324 4578 nft_print(octx, " %s {", set->handle.set.name);
325
326
2/2
✓ Branch 0 (16→17) taken 9 times.
✓ Branch 1 (16→18) taken 4569 times.
4578 if (nft_output_handle(octx))
327 9 nft_print(octx, " # handle %" PRIu64, set->handle.handle.id);
328 4578 nft_print(octx, "%s%s%s", opts->nl, opts->tab, opts->tab);
329
330 4578 set_print_key_and_data(set, octx);
331
332 4578 nft_print(octx, "%s", opts->stmt_separator);
333
334
2/2
✓ Branch 0 (21→22) taken 4567 times.
✓ Branch 1 (21→30) taken 11 times.
4578 if (!(set->flags & (NFT_SET_CONSTANT))) {
335
1/2
✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→25) taken 4567 times.
4567 if (set->policy != NFT_SET_POL_PERFORMANCE) {
336 ✗ nft_print(octx, "%s%spolicy %s%s",
337 opts->tab, opts->tab,
338 ✗ set_policy2str(set->policy),
339 opts->stmt_separator);
340 }
341
342
2/2
✓ Branch 0 (25→26) taken 831 times.
✓ Branch 1 (25→30) taken 3736 times.
4567 if (set->desc.size > 0) {
343 831 nft_print(octx, "%s%ssize %u",
344 opts->tab, opts->tab,
345 831 set->desc.size);
346
2/2
✓ Branch 0 (27→28) taken 24 times.
✓ Branch 1 (27→29) taken 807 times.
831 if (set->count > 0)
347 24 nft_print(octx, "%s# count %u", opts->tab,
348 24 set->count);
349 831 nft_print(octx, "%s", opts->stmt_separator);
350 }
351 }
352
353 4578 flags = set->flags;
354 /* "timeout" flag is redundant if a default timeout exists */
355
2/2
✓ Branch 0 (30→31) taken 171 times.
✓ Branch 1 (30→32) taken 4407 times.
4578 if (set->timeout)
356 171 flags &= ~NFT_SET_TIMEOUT;
357
358
2/2
✓ Branch 0 (32→33) taken 1883 times.
✓ Branch 1 (32→47) taken 2695 times.
4578 if (flags & (NFT_SET_CONSTANT | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | NFT_SET_EVAL)) {
359 1883 nft_print(octx, "%s%sflags ", opts->tab, opts->tab);
360
2/2
✓ Branch 0 (34→35) taken 11 times.
✓ Branch 1 (34→37) taken 1872 times.
1883 if (set->flags & NFT_SET_CONSTANT) {
361 11 nft_print(octx, "%sconstant", delim);
362 11 delim = ",";
363 }
364
2/2
✓ Branch 0 (37→38) taken 62 times.
✓ Branch 1 (37→40) taken 1821 times.
1883 if (set->flags & NFT_SET_EVAL) {
365 62 nft_print(octx, "%sdynamic", delim);
366 62 delim = ",";
367 }
368
2/2
✓ Branch 0 (40→41) taken 1803 times.
✓ Branch 1 (40→43) taken 80 times.
1883 if (set->flags & NFT_SET_INTERVAL) {
369 1803 nft_print(octx, "%sinterval", delim);
370 1803 delim = ",";
371 }
372
2/2
✓ Branch 0 (43→44) taken 701 times.
✓ Branch 1 (43→46) taken 1182 times.
1883 if (set->flags & NFT_SET_TIMEOUT) {
373 701 nft_print(octx, "%stimeout", delim);
374 701 delim = ",";
375 }
376 1883 nft_print(octx, "%s", opts->stmt_separator);
377 }
378
379
2/2
✓ Branch 0 (48→49) taken 70 times.
✓ Branch 1 (48→58) taken 4508 times.
4578 if (!list_empty(&set->stmt_list)) {
380 70 unsigned int flags = octx->flags;
381
382 70 nft_print(octx, "%s%s", opts->tab, opts->tab);
383
384 70 octx->flags |= NFT_CTX_OUTPUT_STATELESS;
385
2/2
✓ Branch 0 (56→51) taken 72 times.
✓ Branch 1 (56→57) taken 70 times.
142 list_for_each_entry(stmt, &set->stmt_list, list) {
386 72 stmt_print(stmt, octx);
387
2/2
✓ Branch 0 (53→54) taken 2 times.
✓ Branch 1 (53→55) taken 70 times.
72 if (!list_is_last(&stmt->list, &set->stmt_list))
388 2 nft_print(octx, " ");
389 }
390 70 octx->flags = flags;
391
392 70 nft_print(octx, "%s", opts->stmt_separator);
393 }
394
395
2/2
✓ Branch 0 (58→59) taken 57 times.
✓ Branch 1 (58→60) taken 4521 times.
4578 if (set->automerge)
396 57 nft_print(octx, "%s%sauto-merge%s", opts->tab, opts->tab,
397 opts->stmt_separator);
398
399
2/2
✓ Branch 0 (60→61) taken 171 times.
✓ Branch 1 (60→64) taken 4407 times.
4578 if (set->timeout) {
400 171 nft_print(octx, "%s%stimeout ", opts->tab, opts->tab);
401 171 time_print(set->timeout, octx);
402 171 nft_print(octx, "%s", opts->stmt_separator);
403 }
404
2/2
✓ Branch 0 (64→65) taken 55 times.
✓ Branch 1 (64→68) taken 4523 times.
4578 if (set->gc_int) {
405 55 nft_print(octx, "%s%sgc-interval ", opts->tab, opts->tab);
406 55 time_print(set->gc_int, octx);
407 55 nft_print(octx, "%s", opts->stmt_separator);
408 }
409
410
2/2
✓ Branch 0 (68→69) taken 2 times.
✓ Branch 1 (68→70) taken 4576 times.
4578 if (set->comment) {
411 2 nft_print(octx, "%s%scomment \"%s\"%s",
412 opts->tab, opts->tab,
413 2 set->comment,
414 opts->stmt_separator);
415 }
416 4578 }
417
418 4578 static void do_set_print(const struct set *set, struct print_fmt_options *opts,
419 struct output_ctx *octx)
420 {
421 4578 set_print_declaration(set, opts, octx);
422
423
3/6
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 4578 times.
✗ Branch 2 (6→7) not taken.
✗ Branch 3 (6→9) not taken.
✓ Branch 4 (8→9) taken 2 times.
✓ Branch 5 (8→11) taken 4576 times.
9156 if ((set_is_meter(set->flags) && nft_output_stateless(octx)) ||
424 4578 nft_output_terse(octx)) {
425 2 nft_print(octx, "%s}%s", opts->tab, opts->nl);
426 2 return;
427 }
428
429
5/6
✓ Branch 0 (11→12) taken 4566 times.
✓ Branch 1 (11→24) taken 10 times.
✗ Branch 2 (12→13) not taken.
✓ Branch 3 (12→14) taken 4566 times.
✓ Branch 4 (14→15) taken 3178 times.
✓ Branch 5 (14→24) taken 1388 times.
4576 if (set->init != NULL && expr_set(set->init)->size > 0) {
430 3178 nft_print(octx, "%s%selements = ", opts->tab, opts->tab);
431
432
4/4
✓ Branch 0 (16→17) taken 3041 times.
✓ Branch 1 (16→21) taken 137 times.
✓ Branch 2 (17→18) taken 3032 times.
✓ Branch 3 (17→21) taken 9 times.
3178 if (set->timeout || set->elem_has_comment ||
433
2/2
✓ Branch 0 (18→19) taken 2238 times.
✓ Branch 1 (18→21) taken 794 times.
3032 (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT |
434
2/2
✓ Branch 0 (20→21) taken 43 times.
✓ Branch 1 (20→22) taken 2195 times.
2238 NFT_SET_TIMEOUT | NFT_SET_CONCAT)) ||
435 2238 !list_empty(&set->stmt_list))
436 983 octx->force_newline = true;
437
438 3178 expr_print(set->init, octx);
439 3178 octx->force_newline = false;
440
441 3178 nft_print(octx, "%s", opts->nl);
442 }
443 4576 nft_print(octx, "%s}%s", opts->tab, opts->nl);
444 }
445
446 4568 void set_print(const struct set *s, struct output_ctx *octx)
447 {
448 4568 struct print_fmt_options opts = {
449 .tab = "\t",
450 .nl = "\n",
451 .stmt_separator = "\n",
452 };
453
454 4568 do_set_print(s, &opts, octx);
455 4568 }
456
457 10 void set_print_plain(const struct set *s, struct output_ctx *octx)
458 {
459 20 struct print_fmt_options opts = {
460 .tab = "",
461 .nl = " ",
462 10 .table = s->handle.table.name,
463 10 .family = family2str(s->handle.family),
464 .stmt_separator = "; ",
465 };
466
467 10 do_set_print(s, &opts, octx);
468 10 }
469
470 354079 struct rule *rule_alloc(const struct location *loc, const struct handle *h)
471 {
472 struct rule *rule;
473
474
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 354079 times.
354079 assert(loc);
475
476 354079 rule = xzalloc(sizeof(*rule));
477 354079 rule->location = *loc;
478 354079 init_list_head(&rule->list);
479 354079 init_list_head(&rule->stmts);
480 354079 rule->refcnt = 1;
481
2/2
✓ Branch 0 (7→8) taken 245155 times.
✓ Branch 1 (7→9) taken 108924 times.
354079 if (h != NULL)
482 245155 rule->handle = *h;
483
484 354079 return rule;
485 }
486
487 229 struct rule *rule_get(struct rule *rule)
488 {
489
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 229 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 229 times.
229 assert_refcount_safe(rule->refcnt);
490 229 rule->refcnt++;
491 229 return rule;
492 }
493
494 354308 void rule_free(struct rule *rule)
495 {
496
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 354308 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 354308 times.
354308 assert_refcount_safe(rule->refcnt);
497
2/2
✓ Branch 0 (8→9) taken 229 times.
✓ Branch 1 (8→10) taken 354079 times.
354308 if (--rule->refcnt > 0)
498 229 return;
499 354079 stmt_list_free(&rule->stmts);
500 354079 handle_free(&rule->handle);
501 354079 free_const(rule->comment);
502 354079 free(rule);
503 }
504
505 31128 void rule_print(const struct rule *rule, struct output_ctx *octx)
506 {
507 const struct stmt_ops *ops;
508 const struct stmt *stmt;
509
510
2/2
✓ Branch 0 (9→3) taken 34078 times.
✓ Branch 1 (9→10) taken 31128 times.
65206 list_for_each_entry(stmt, &rule->stmts, list) {
511 34078 ops = stmt_ops(stmt);
512 34078 ops->print(stmt, octx);
513
2/2
✓ Branch 0 (6→7) taken 2950 times.
✓ Branch 1 (6→8) taken 31128 times.
34078 if (!list_is_last(&stmt->list, &rule->stmts))
514 2950 nft_print(octx, " ");
515 }
516
517
2/2
✓ Branch 0 (10→11) taken 3131 times.
✓ Branch 1 (10→12) taken 27997 times.
31128 if (rule->comment)
518 3131 nft_print(octx, " comment \"%s\"", rule->comment);
519
520
2/2
✓ Branch 0 (13→14) taken 3015 times.
✓ Branch 1 (13→15) taken 28113 times.
31128 if (nft_output_handle(octx))
521 3015 nft_print(octx, " # handle %" PRIu64, rule->handle.handle.id);
522 31128 }
523
524 1 struct rule *rule_lookup(const struct chain *chain, uint64_t handle)
525 {
526 struct rule *rule;
527
528
1/2
✓ Branch 0 (6→3) taken 1 times.
✗ Branch 1 (6→7) not taken.
1 list_for_each_entry(rule, &chain->rules, list) {
529
1/2
✓ Branch 0 (3→4) taken 1 times.
✗ Branch 1 (3→5) not taken.
1 if (rule->handle.handle.id == handle)
530 1 return rule;
531 }
532 ✗ return NULL;
533 }
534
535 27 struct rule *rule_lookup_by_index(const struct chain *chain, uint64_t index)
536 {
537 struct rule *rule;
538
539
2/2
✓ Branch 0 (6→3) taken 59 times.
✓ Branch 1 (6→7) taken 2 times.
61 list_for_each_entry(rule, &chain->rules, list) {
540
2/2
✓ Branch 0 (3→4) taken 25 times.
✓ Branch 1 (3→5) taken 34 times.
59 if (!--index)
541 25 return rule;
542 }
543 2 return NULL;
544 }
545
546 127449 void rule_stmt_append(struct rule *rule, struct stmt *stmt)
547 {
548 127449 list_add_tail(&stmt->list, &rule->stmts);
549 127449 rule->num_stmts++;
550 127449 }
551
552 12076 void rule_stmt_insert_at(struct rule *rule, struct stmt *nstmt,
553 struct stmt *stmt)
554 {
555 12076 list_add_tail(&nstmt->list, &stmt->list);
556 12076 rule->num_stmts++;
557 12076 }
558
559 13026 struct scope *scope_alloc(void)
560 {
561 13026 struct scope *scope = xzalloc(sizeof(struct scope));
562
563 13026 init_list_head(&scope->symbols);
564
565 13026 return scope;
566 }
567
568 68630 struct scope *scope_init(struct scope *scope, const struct scope *parent)
569 {
570 68630 scope->parent = parent;
571 68630 return scope;
572 }
573
574 128519 void scope_release(const struct scope *scope)
575 {
576 struct symbol *sym, *next;
577
578
2/2
✓ Branch 0 (8→3) taken 20114 times.
✓ Branch 1 (8→9) taken 128519 times.
148633 list_for_each_entry_safe(sym, next, &scope->symbols, list) {
579
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 20114 times.
20114 assert(sym->refcnt == 1);
580 20114 list_del(&sym->list);
581 20114 free_const(sym->identifier);
582 20114 expr_free(sym->expr);
583 20114 free(sym);
584 }
585 128519 }
586
587 13026 void scope_free(struct scope *scope)
588 {
589 13026 scope_release(scope);
590 13026 free(scope);
591 13026 }
592
593 20117 void symbol_bind(struct scope *scope, const char *identifier, struct expr *expr)
594 {
595 struct symbol *sym;
596
597 20117 sym = xzalloc(sizeof(*sym));
598 20117 sym->identifier = xstrdup(identifier);
599 20117 sym->expr = expr;
600 20117 sym->refcnt = 1;
601
602 20117 list_add(&sym->list, &scope->symbols);
603 20117 }
604
605 20221 struct symbol *symbol_get(const struct scope *scope, const char *identifier)
606 {
607 struct symbol *sym;
608
609 20221 sym = symbol_lookup(scope, identifier);
610
2/2
✓ Branch 0 (3→4) taken 9 times.
✓ Branch 1 (3→5) taken 20212 times.
20221 if (!sym)
611 9 return NULL;
612
613
2/4
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 20212 times.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→11) taken 20212 times.
20212 assert_refcount_safe(sym->refcnt);
614 20212 sym->refcnt++;
615
616 20212 return sym;
617 }
618
619 3 static void symbol_put(struct symbol *sym)
620 {
621
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 3 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 3 times.
3 assert_refcount_safe(sym->refcnt);
622
1/2
✓ Branch 0 (8→9) taken 3 times.
✗ Branch 1 (8→11) not taken.
3 if (--sym->refcnt == 0) {
623 3 free_const(sym->identifier);
624 3 expr_free(sym->expr);
625 3 free(sym);
626 }
627 3 }
628
629 3 static void symbol_remove(struct symbol *sym)
630 {
631 3 list_del(&sym->list);
632 3 symbol_put(sym);
633 3 }
634
635 2 int symbol_unbind(const struct scope *scope, const char *identifier)
636 {
637 struct symbol *sym, *next;
638
639
2/2
✓ Branch 0 (6→3) taken 5 times.
✓ Branch 1 (6→7) taken 2 times.
7 list_for_each_entry_safe(sym, next, &scope->symbols, list) {
640
2/2
✓ Branch 0 (3→4) taken 3 times.
✓ Branch 1 (3→5) taken 2 times.
5 if (!strcmp(sym->identifier, identifier))
641 3 symbol_remove(sym);
642 }
643
644 2 return 0;
645 }
646
647 40339 struct symbol *symbol_lookup(const struct scope *scope, const char *identifier)
648 {
649 struct symbol *sym;
650
651
2/2
✓ Branch 0 (9→3) taken 80702 times.
✓ Branch 1 (9→10) taken 20124 times.
100826 while (scope != NULL) {
652
2/2
✓ Branch 0 (7→4) taken 400041322 times.
✓ Branch 1 (7→8) taken 60487 times.
400101809 list_for_each_entry(sym, &scope->symbols, list) {
653
2/2
✓ Branch 0 (4→5) taken 20215 times.
✓ Branch 1 (4→6) taken 400021107 times.
400041322 if (!strcmp(sym->identifier, identifier))
654 20215 return sym;
655 }
656 60487 scope = scope->parent;
657 }
658 20124 return NULL;
659 }
660
661 9 struct symbol *symbol_lookup_fuzzy(const struct scope *scope,
662 const char *identifier)
663 {
664 struct string_misspell_state st;
665 struct symbol *sym;
666
667 9 string_misspell_init(&st);
668
669
2/2
✓ Branch 0 (9→4) taken 20 times.
✓ Branch 1 (9→10) taken 9 times.
29 while (scope != NULL) {
670
2/2
✓ Branch 0 (7→5) taken 1 times.
✓ Branch 1 (7→8) taken 20 times.
21 list_for_each_entry(sym, &scope->symbols, list)
671 1 string_misspell_update(sym->identifier, identifier,
672 sym, &st);
673
674 20 scope = scope->parent;
675 }
676 9 return st.obj;
677 }
678
679 static const char * const chain_hookname_str_array[] = {
680 "prerouting",
681 "input",
682 "forward",
683 "postrouting",
684 "output",
685 "ingress",
686 "egress",
687 NULL,
688 };
689
690 4135 const char *chain_hookname_lookup(const char *name)
691 {
692 int i;
693
694
2/2
✓ Branch 0 (6→3) taken 17210 times.
✓ Branch 1 (6→7) taken 1 times.
17211 for (i = 0; chain_hookname_str_array[i]; i++) {
695
2/2
✓ Branch 0 (3→4) taken 4134 times.
✓ Branch 1 (3→5) taken 13076 times.
17210 if (!strcmp(name, chain_hookname_str_array[i]))
696 4134 return chain_hookname_str_array[i];
697 }
698
699 1 return NULL;
700 }
701
702 /* internal ID to uniquely identify a set in the batch */
703 static uint32_t chain_id;
704
705 26831 struct chain *chain_alloc(void)
706 {
707 struct chain *chain;
708
709 26831 chain = xzalloc(sizeof(*chain));
710 26831 chain->location = internal_location;
711 26831 chain->refcnt = 1;
712 26831 chain->handle.chain_id = ++chain_id;
713 26831 init_list_head(&chain->rules);
714 26831 init_list_head(&chain->scope.symbols);
715
716 26831 chain->policy = NULL;
717 26831 return chain;
718 }
719
720 8049 struct chain *chain_get(struct chain *chain)
721 {
722
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 8049 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 8049 times.
8049 assert_refcount_safe(chain->refcnt);
723 8049 chain->refcnt++;
724 8049 return chain;
725 }
726
727 34880 void chain_free(struct chain *chain)
728 {
729 struct rule *rule, *next;
730 int i;
731
732
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 34880 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 34880 times.
34880 assert_refcount_safe(chain->refcnt);
733
2/2
✓ Branch 0 (8→9) taken 8049 times.
✓ Branch 1 (8→10) taken 26831 times.
34880 if (--chain->refcnt > 0)
734 8049 return;
735
2/2
✓ Branch 0 (13→11) taken 56871 times.
✓ Branch 1 (13→14) taken 26831 times.
83702 list_for_each_entry_safe(rule, next, &chain->rules, list)
736 56871 rule_free(rule);
737 26831 handle_free(&chain->handle);
738 26831 free_const(chain->type.str);
739 26831 expr_free(chain->dev_expr);
740
2/2
✓ Branch 0 (18→17) taken 6788 times.
✓ Branch 1 (18→19) taken 26831 times.
33619 for (i = 0; i < chain->dev_array_len; i++)
741 6788 free_const(chain->dev_array[i]);
742 26831 free(chain->dev_array);
743 26831 expr_free(chain->priority.expr);
744 26831 expr_free(chain->policy);
745 26831 free_const(chain->comment);
746
747 /* MUST be released after all expressions, they could
748 * hold refcounts.
749 */
750 26831 scope_release(&chain->scope);
751 26831 free(chain);
752 }
753
754 974 struct chain *chain_binding_lookup(const struct table *table,
755 const char *chain_name)
756 {
757 struct chain *chain;
758
759
2/2
✓ Branch 0 (6→3) taken 246 times.
✓ Branch 1 (6→7) taken 892 times.
1138 list_for_each_entry(chain, &table->chain_bindings, cache.list) {
760
2/2
✓ Branch 0 (3→4) taken 82 times.
✓ Branch 1 (3→5) taken 164 times.
246 if (!strcmp(chain->handle.chain.name, chain_name))
761 82 return chain;
762 }
763 892 return NULL;
764 }
765
766 587 struct chain *chain_lookup_fuzzy(const struct handle *h,
767 const struct nft_cache *cache,
768 const struct table **t)
769 {
770 struct string_misspell_state st;
771 struct table *table;
772 struct chain *chain;
773
774
2/2
✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 586 times.
587 if (!h->chain.name)
775 1 return NULL;
776
777 586 string_misspell_init(&st);
778
779
2/2
✓ Branch 0 (13→6) taken 577 times.
✓ Branch 1 (13→14) taken 586 times.
1163 list_for_each_entry(table, &cache->table_cache.list, cache.list) {
780
2/2
✓ Branch 0 (11→7) taken 9 times.
✓ Branch 1 (11→12) taken 577 times.
586 list_for_each_entry(chain, &table->chain_cache.list, cache.list) {
781
2/2
✓ Branch 0 (8→9) taken 4 times.
✓ Branch 1 (8→10) taken 5 times.
9 if (string_misspell_update(chain->handle.chain.name,
782 9 h->chain.name, chain, &st))
783 4 *t = table;
784 }
785 }
786 586 return st.obj;
787 }
788
789 58191 const char *family2str(unsigned int family)
790 {
791
6/7
✓ Branch 0 (2→3) taken 29092 times.
✓ Branch 1 (2→4) taken 5777 times.
✓ Branch 2 (2→5) taken 10762 times.
✓ Branch 3 (2→6) taken 9180 times.
✓ Branch 4 (2→7) taken 1293 times.
✓ Branch 5 (2→8) taken 2087 times.
✗ Branch 6 (2→9) not taken.
58191 switch (family) {
792 29092 case NFPROTO_IPV4:
793 29092 return "ip";
794 5777 case NFPROTO_IPV6:
795 5777 return "ip6";
796 10762 case NFPROTO_INET:
797 10762 return "inet";
798 9180 case NFPROTO_NETDEV:
799 9180 return "netdev";
800 1293 case NFPROTO_ARP:
801 1293 return "arp";
802 2087 case NFPROTO_BRIDGE:
803 2087 return "bridge";
804 ✗ default:
805 ✗ break;
806 }
807 ✗ return "unknown";
808 }
809
810 34682 const char *hooknum2str(unsigned int family, unsigned int hooknum)
811 {
812
3/4
✓ Branch 0 (2→3) taken 19651 times.
✓ Branch 1 (2→12) taken 1272 times.
✓ Branch 2 (2→19) taken 13759 times.
✗ Branch 3 (2→23) not taken.
34682 switch (family) {
813 19651 case NFPROTO_IPV4:
814 case NFPROTO_BRIDGE:
815 case NFPROTO_IPV6:
816 case NFPROTO_INET:
817
6/7
✓ Branch 0 (3→4) taken 2000 times.
✓ Branch 1 (3→5) taken 12883 times.
✓ Branch 2 (3→6) taken 442 times.
✓ Branch 3 (3→7) taken 652 times.
✓ Branch 4 (3→8) taken 3656 times.
✓ Branch 5 (3→9) taken 18 times.
✗ Branch 6 (3→10) not taken.
19651 switch (hooknum) {
818 2000 case NF_INET_PRE_ROUTING:
819 2000 return "prerouting";
820 12883 case NF_INET_LOCAL_IN:
821 12883 return "input";
822 442 case NF_INET_FORWARD:
823 442 return "forward";
824 652 case NF_INET_POST_ROUTING:
825 652 return "postrouting";
826 3656 case NF_INET_LOCAL_OUT:
827 3656 return "output";
828 18 case NF_INET_INGRESS:
829 18 return "ingress";
830 ✗ default:
831 ✗ break;
832 };
833 ✗ break;
834 1272 case NFPROTO_ARP:
835
2/5
✓ Branch 0 (12→13) taken 964 times.
✗ Branch 1 (12→14) not taken.
✓ Branch 2 (12→15) taken 308 times.
✗ Branch 3 (12→16) not taken.
✗ Branch 4 (12→17) not taken.
1272 switch (hooknum) {
836 964 case NF_ARP_IN:
837 964 return "input";
838 ✗ case NF_ARP_FORWARD:
839 ✗ return "forward";
840 308 case NF_ARP_OUT:
841 308 return "output";
842 ✗ case __NF_ARP_INGRESS:
843 ✗ return "ingress";
844 ✗ default:
845 ✗ break;
846 }
847 ✗ break;
848 13759 case NFPROTO_NETDEV:
849
2/3
✓ Branch 0 (19→20) taken 7111 times.
✓ Branch 1 (19→21) taken 6648 times.
✗ Branch 2 (19→22) not taken.
13759 switch (hooknum) {
850 7111 case NF_NETDEV_INGRESS:
851 7111 return "ingress";
852 6648 case NF_NETDEV_EGRESS:
853 6648 return "egress";
854 }
855 ✗ break;
856 ✗ default:
857 ✗ break;
858 };
859
860 ✗ return "unknown";
861 }
862
863 16777 const char *chain_policy2str(uint32_t policy)
864 {
865
2/3
✓ Branch 0 (2→3) taken 28 times.
✓ Branch 1 (2→4) taken 16749 times.
✗ Branch 2 (2→5) not taken.
16777 switch (policy) {
866 28 case NF_DROP:
867 28 return "drop";
868 16749 case NF_ACCEPT:
869 16749 return "accept";
870 }
871 ✗ return "unknown";
872 }
873
874 struct prio_tag {
875 int val;
876 const char *str;
877 };
878
879 static const struct prio_tag std_prios[] = {
880 { NF_IP_PRI_RAW, "raw" },
881 { NF_IP_PRI_MANGLE, "mangle" },
882 { NF_IP_PRI_NAT_DST, "dstnat" },
883 { NF_IP_PRI_FILTER, "filter" },
884 { NF_IP_PRI_SECURITY, "security" },
885 { NF_IP_PRI_NAT_SRC, "srcnat" },
886 };
887
888 static const struct prio_tag bridge_std_prios[] = {
889 { NF_BR_PRI_NAT_DST_BRIDGED, "dstnat" },
890 { NF_BR_PRI_FILTER_BRIDGED, "filter" },
891 { NF_BR_PRI_NAT_DST_OTHER, "out" },
892 { NF_BR_PRI_NAT_SRC, "srcnat" },
893 };
894
895 10110 static bool std_prio_family_hook_compat(int prio, int family, int hook)
896 {
897 /* bridge family has different values */
898
2/2
✓ Branch 0 (2→3) taken 100 times.
✓ Branch 1 (2→14) taken 10010 times.
10110 if (family == NFPROTO_BRIDGE) {
899
4/5
✓ Branch 0 (3→4) taken 15 times.
✓ Branch 1 (3→7) taken 55 times.
✓ Branch 2 (3→8) taken 15 times.
✓ Branch 3 (3→11) taken 15 times.
✗ Branch 4 (3→13) not taken.
100 switch (prio) {
900 15 case NF_BR_PRI_NAT_DST_BRIDGED:
901
2/2
✓ Branch 0 (4→5) taken 11 times.
✓ Branch 1 (4→6) taken 4 times.
15 if (hook == NF_BR_PRE_ROUTING)
902 11 return true;
903 4 break;
904 55 case NF_BR_PRI_FILTER_BRIDGED:
905 55 return true;
906 15 case NF_BR_PRI_NAT_DST_OTHER:
907
2/2
✓ Branch 0 (8→9) taken 11 times.
✓ Branch 1 (8→10) taken 4 times.
15 if (hook == NF_BR_LOCAL_OUT)
908 11 return true;
909 4 break;
910 15 case NF_BR_PRI_NAT_SRC:
911
2/2
✓ Branch 0 (11→12) taken 11 times.
✓ Branch 1 (11→13) taken 4 times.
15 if (hook == NF_BR_POST_ROUTING)
912 11 return true;
913 }
914 12 return false;
915 }
916
4/5
✓ Branch 0 (14→15) taken 9245 times.
✓ Branch 1 (14→18) taken 590 times.
✓ Branch 2 (14→21) taken 97 times.
✓ Branch 3 (14→26) taken 78 times.
✗ Branch 4 (14→30) not taken.
10010 switch(prio) {
917 9245 case NF_IP_PRI_FILTER:
918
1/2
✓ Branch 0 (15→16) taken 9245 times.
✗ Branch 1 (15→17) not taken.
9245 switch (family) {
919 9245 case NFPROTO_INET:
920 case NFPROTO_IPV4:
921 case NFPROTO_IPV6:
922 case NFPROTO_ARP:
923 case NFPROTO_NETDEV:
924 9245 return true;
925 }
926 ✗ break;
927 590 case NF_IP_PRI_RAW:
928 case NF_IP_PRI_MANGLE:
929 case NF_IP_PRI_SECURITY:
930
2/2
✓ Branch 0 (18→19) taken 575 times.
✓ Branch 1 (18→20) taken 15 times.
590 switch (family) {
931 575 case NFPROTO_INET:
932 case NFPROTO_IPV4:
933 case NFPROTO_IPV6:
934 575 return true;
935 }
936 15 break;
937 97 case NF_IP_PRI_NAT_DST:
938
2/2
✓ Branch 0 (21→22) taken 93 times.
✓ Branch 1 (21→25) taken 4 times.
97 switch(family) {
939 93 case NFPROTO_INET:
940 case NFPROTO_IPV4:
941 case NFPROTO_IPV6:
942
3/4
✓ Branch 0 (22→23) taken 9 times.
✓ Branch 1 (22→24) taken 84 times.
✗ Branch 2 (23→24) not taken.
✓ Branch 3 (23→25) taken 9 times.
93 if (hook == NF_INET_PRE_ROUTING ||
943 hook == NF_INET_LOCAL_OUT)
944 84 return true;
945 }
946 13 break;
947 78 case NF_IP_PRI_NAT_SRC:
948
2/2
✓ Branch 0 (26→27) taken 73 times.
✓ Branch 1 (26→30) taken 5 times.
78 switch(family) {
949 73 case NFPROTO_INET:
950 case NFPROTO_IPV4:
951 case NFPROTO_IPV6:
952
3/4
✓ Branch 0 (27→28) taken 73 times.
✗ Branch 1 (27→29) not taken.
✓ Branch 2 (28→29) taken 64 times.
✓ Branch 3 (28→30) taken 9 times.
73 if (hook == NF_INET_LOCAL_IN ||
953 hook == NF_INET_POST_ROUTING)
954 64 return true;
955 }
956 }
957 42 return false;
958 }
959
960 2012 int std_prio_lookup(const char *std_prio_name, int family, int hook)
961 {
962 const struct prio_tag *prio_arr;
963 size_t i, arr_size;
964
965
2/2
✓ Branch 0 (2→3) taken 76 times.
✓ Branch 1 (2→4) taken 1936 times.
2012 if (family == NFPROTO_BRIDGE) {
966 76 prio_arr = bridge_std_prios;
967 76 arr_size = array_size(bridge_std_prios);
968 } else {
969 1936 prio_arr = std_prios;
970 1936 arr_size = array_size(std_prios);
971 }
972
973
2/2
✓ Branch 0 (11→6) taken 7529 times.
✓ Branch 1 (11→12) taken 59 times.
7588 for (i = 0; i < arr_size; ++i) {
974
4/4
✓ Branch 0 (6→7) taken 2007 times.
✓ Branch 1 (6→10) taken 5522 times.
✓ Branch 2 (8→9) taken 1953 times.
✓ Branch 3 (8→10) taken 54 times.
9536 if (strcmp(prio_arr[i].str, std_prio_name) == 0 &&
975 2007 std_prio_family_hook_compat(prio_arr[i].val, family, hook))
976 1953 return prio_arr[i].val;
977 }
978 59 return NF_IP_PRI_LAST;
979 }
980
981 8795 static const char *prio2str(const struct output_ctx *octx,
982 char *buf, size_t bufsize, int family, int hook,
983 const struct expr *expr)
984 {
985 const struct prio_tag *prio_arr;
986 8795 const uint32_t reach = 10;
987 const char *std_prio_str;
988 int std_prio, prio;
989 size_t i, arr_size;
990 int64_t offset;
991
992 8795 mpz_export_data(&prio, expr->value, BYTEORDER_HOST_ENDIAN, sizeof(int));
993
2/2
✓ Branch 0 (4→5) taken 561 times.
✓ Branch 1 (4→6) taken 8234 times.
8795 if (family == NFPROTO_BRIDGE) {
994 561 prio_arr = bridge_std_prios;
995 561 arr_size = array_size(bridge_std_prios);
996 } else {
997 8234 prio_arr = std_prios;
998 8234 arr_size = array_size(std_prios);
999 }
1000
1001
2/2
✓ Branch 0 (8→9) taken 8790 times.
✓ Branch 1 (8→21) taken 5 times.
8795 if (!nft_output_numeric_prio(octx)) {
1002
2/2
✓ Branch 0 (20→10) taken 35125 times.
✓ Branch 1 (20→21) taken 687 times.
35812 for (i = 0; i < arr_size; ++i) {
1003 35125 std_prio = prio_arr[i].val;
1004 35125 std_prio_str = prio_arr[i].str;
1005
1006 35125 offset = (int64_t)prio - std_prio;
1007
2/2
✓ Branch 0 (10→11) taken 8103 times.
✓ Branch 1 (10→19) taken 27022 times.
35125 if (llabs(offset) <= reach) {
1008
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 8103 times.
8103 if (!std_prio_family_hook_compat(std_prio,
1009 family, hook))
1010 ✗ break;
1011
1012 8103 strncpy(buf, std_prio_str, bufsize);
1013
2/2
✓ Branch 0 (14→15) taken 102 times.
✓ Branch 1 (14→16) taken 8001 times.
8103 if (offset > 0)
1014 102 snprintf(buf + strlen(buf),
1015 102 bufsize - strlen(buf), " + %" PRIu64,
1016 offset);
1017
2/2
✓ Branch 0 (16→17) taken 82 times.
✓ Branch 1 (16→18) taken 7919 times.
8001 else if (offset < 0)
1018 82 snprintf(buf + strlen(buf),
1019 82 bufsize - strlen(buf), " - %" PRIu64,
1020 -offset);
1021 8103 return buf;
1022 }
1023 }
1024 }
1025 692 snprintf(buf, bufsize, "%d", prio);
1026 692 return buf;
1027 }
1028
1029 9707 static void chain_print_declaration(const struct chain *chain,
1030 struct output_ctx *octx)
1031 {
1032 char priobuf[STD_PRIO_BUFSIZE];
1033 int policy, i;
1034
1035
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 9707 times.
9707 if (chain->flags & CHAIN_F_BINDING)
1036 ✗ return;
1037
1038 9707 nft_print(octx, "\tchain %s {", chain->handle.chain.name);
1039
2/2
✓ Branch 0 (6→7) taken 23 times.
✓ Branch 1 (6→8) taken 9684 times.
9707 if (nft_output_handle(octx))
1040 23 nft_print(octx, " # handle %" PRIu64, chain->handle.handle.id);
1041
2/2
✓ Branch 0 (8→9) taken 2 times.
✓ Branch 1 (8→10) taken 9705 times.
9707 if (chain->comment)
1042 2 nft_print(octx, "\n\t\tcomment \"%s\"", chain->comment);
1043 9707 nft_print(octx, "\n");
1044
2/2
✓ Branch 0 (11→12) taken 8768 times.
✓ Branch 1 (11→36) taken 939 times.
9707 if (chain->flags & CHAIN_F_BASECHAIN) {
1045
2/2
✓ Branch 0 (12→13) taken 8767 times.
✓ Branch 1 (12→15) taken 1 times.
8768 if (chain->type.str)
1046 8767 nft_print(octx, "\t\ttype %s hook %s", chain->type.str,
1047 8767 hooknum2str(chain->handle.family, chain->hook.num));
1048
1049
2/2
✓ Branch 0 (15→16) taken 3457 times.
✓ Branch 1 (15→17) taken 5311 times.
8768 if (chain->dev_array_len == 1) {
1050 3457 nft_print(octx, " device \"%s\"", chain->dev_array[0]);
1051
2/2
✓ Branch 0 (17→18) taken 5 times.
✓ Branch 1 (17→26) taken 5306 times.
5311 } else if (chain->dev_array_len > 1) {
1052 5 nft_print(octx, " devices = { ");
1053
2/2
✓ Branch 0 (24→20) taken 12 times.
✓ Branch 1 (24→25) taken 5 times.
17 for (i = 0; i < chain->dev_array_len; i++) {
1054 12 nft_print(octx, "\"%s\"", chain->dev_array[i]);
1055
2/2
✓ Branch 0 (21→22) taken 7 times.
✓ Branch 1 (21→23) taken 5 times.
12 if (i + 1 != chain->dev_array_len)
1056 7 nft_print(octx, ", ");
1057 }
1058 5 nft_print(octx, " }");
1059 }
1060
1061
2/2
✓ Branch 0 (26→27) taken 8767 times.
✓ Branch 1 (26→29) taken 1 times.
8768 if (chain->priority.expr)
1062 8767 nft_print(octx, " priority %s;",
1063 prio2str(octx, priobuf, sizeof(priobuf),
1064 8767 chain->handle.family, chain->hook.num,
1065 8767 chain->priority.expr));
1066
2/2
✓ Branch 0 (29→30) taken 8766 times.
✓ Branch 1 (29→33) taken 2 times.
8768 if (chain->policy) {
1067 8766 mpz_export_data(&policy, chain->policy->value,
1068 BYTEORDER_HOST_ENDIAN, sizeof(int));
1069 8766 nft_print(octx, " policy %s;",
1070 chain_policy2str(policy));
1071 }
1072
1/2
✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 8768 times.
8768 if (chain->flags & CHAIN_F_HW_OFFLOAD)
1073 ✗ nft_print(octx, " flags offload;");
1074
1075 8768 nft_print(octx, "\n");
1076 }
1077 }
1078
1079 9734 void chain_rules_print(const struct chain *chain, struct output_ctx *octx,
1080 const char *indent)
1081 {
1082 9734 unsigned int flags = octx->flags;
1083 struct rule *rule;
1084
1085
2/2
✓ Branch 0 (2→3) taken 27 times.
✓ Branch 1 (2→4) taken 9707 times.
9734 if (chain->flags & CHAIN_F_BINDING)
1086 27 octx->flags &= ~NFT_CTX_OUTPUT_HANDLE;
1087
1088
2/2
✓ Branch 0 (12→5) taken 28081 times.
✓ Branch 1 (12→13) taken 9734 times.
37815 list_for_each_entry(rule, &chain->rules, list) {
1089
2/2
✓ Branch 0 (5→6) taken 55 times.
✓ Branch 1 (5→7) taken 28026 times.
28081 nft_print(octx, "\t\t%s", indent ? : "");
1090 28081 rule_print(rule, octx);
1091 28081 nft_print(octx, "\n");
1092 }
1093
1094 9734 octx->flags = flags;
1095 9734 }
1096
1097 9707 static void chain_print(const struct chain *chain, struct output_ctx *octx)
1098 {
1099 9707 chain_print_declaration(chain, octx);
1100 9707 chain_rules_print(chain, octx, NULL);
1101 9707 nft_print(octx, "\t}\n");
1102 9707 }
1103
1104 8 void chain_print_plain(const struct chain *chain, struct output_ctx *octx)
1105 {
1106 char priobuf[STD_PRIO_BUFSIZE];
1107 int policy;
1108
1109 8 nft_print(octx, "chain %s %s %s", family2str(chain->handle.family),
1110 8 chain->handle.table.name, chain->handle.chain.name);
1111
1112
2/2
✓ Branch 0 (4→5) taken 4 times.
✓ Branch 1 (4→19) taken 4 times.
8 if (chain->flags & CHAIN_F_BASECHAIN) {
1113 4 mpz_export_data(&policy, chain->policy->value,
1114 BYTEORDER_HOST_ENDIAN, sizeof(int));
1115 4 nft_print(octx, " { type %s hook %s ",
1116 4 chain->type.str, chain->hook.name);
1117
1118
2/2
✓ Branch 0 (7→8) taken 2 times.
✓ Branch 1 (7→16) taken 2 times.
4 if (chain->dev_array_len > 0) {
1119 int i;
1120
1121 2 nft_print(octx, "devices = { ");
1122
2/2
✓ Branch 0 (14→10) taken 3 times.
✓ Branch 1 (14→15) taken 2 times.
5 for (i = 0; i < chain->dev_array_len; i++) {
1123 3 nft_print(octx, "\"%s\"", chain->dev_array[i]);
1124
2/2
✓ Branch 0 (11→12) taken 1 times.
✓ Branch 1 (11→13) taken 2 times.
3 if (i + 1 != chain->dev_array_len)
1125 1 nft_print(octx, ", ");
1126 }
1127 2 nft_print(octx, " } ");
1128 }
1129 8 nft_print(octx, "priority %s; policy %s; }",
1130 prio2str(octx, priobuf, sizeof(priobuf),
1131 4 chain->handle.family, chain->hook.num,
1132 4 chain->priority.expr),
1133 chain_policy2str(policy));
1134 }
1135
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 8 times.
8 if (nft_output_handle(octx))
1136 ✗ nft_print(octx, " # handle %" PRIu64, chain->handle.handle.id);
1137 8 }
1138
1139 85224 struct table *table_alloc(void)
1140 {
1141 struct table *table;
1142
1143 85224 table = xzalloc(sizeof(*table));
1144 85224 table->location = internal_location;
1145 85224 init_list_head(&table->chains);
1146 85224 init_list_head(&table->sets);
1147 85224 init_list_head(&table->objs);
1148 85224 init_list_head(&table->flowtables);
1149 85224 init_list_head(&table->chain_bindings);
1150 85224 init_list_head(&table->scope.symbols);
1151 85224 table->refcnt = 1;
1152
1153 85224 cache_init(&table->chain_cache);
1154 85224 cache_init(&table->set_cache);
1155 85224 cache_init(&table->obj_cache);
1156 85224 cache_init(&table->ft_cache);
1157
1158 85224 return table;
1159 }
1160
1161 86513 void table_free(struct table *table)
1162 {
1163 struct chain *chain, *next;
1164 struct flowtable *ft, *nft;
1165 struct set *set, *nset;
1166 struct obj *obj, *nobj;
1167
1168
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 86513 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 86513 times.
86513 assert_refcount_safe(table->refcnt);
1169
2/2
✓ Branch 0 (8→9) taken 1289 times.
✓ Branch 1 (8→10) taken 85224 times.
86513 if (--table->refcnt > 0)
1170 1289 return;
1171
2/2
✓ Branch 0 (10→11) taken 16 times.
✓ Branch 1 (10→12) taken 85208 times.
85224 if (table->comment)
1172 16 free_const(table->comment);
1173
2/2
✓ Branch 0 (15→13) taken 2522 times.
✓ Branch 1 (15→16) taken 85224 times.
87746 list_for_each_entry_safe(chain, next, &table->chains, list)
1174 2522 chain_free(chain);
1175
2/2
✓ Branch 0 (19→17) taken 27 times.
✓ Branch 1 (19→20) taken 85224 times.
85251 list_for_each_entry_safe(chain, next, &table->chain_bindings, cache.list)
1176 27 chain_free(chain);
1177 /* this is implicitly releasing chains in the hashtable cache */
1178
2/2
✓ Branch 0 (23→21) taken 26700 times.
✓ Branch 1 (23→24) taken 85224 times.
111924 list_for_each_entry_safe(chain, next, &table->chain_cache.list, cache.list)
1179 26700 chain_free(chain);
1180
2/2
✓ Branch 0 (27→25) taken 875 times.
✓ Branch 1 (27→28) taken 85224 times.
86099 list_for_each_entry_safe(set, nset, &table->sets, list)
1181 875 set_free(set);
1182 /* this is implicitly releasing sets in the hashtable cache */
1183
2/2
✓ Branch 0 (31→29) taken 40241 times.
✓ Branch 1 (31→32) taken 85224 times.
125465 list_for_each_entry_safe(set, nset, &table->set_cache.list, cache.list)
1184 40241 set_free(set);
1185
2/2
✓ Branch 0 (35→33) taken 37 times.
✓ Branch 1 (35→36) taken 85224 times.
85261 list_for_each_entry_safe(ft, nft, &table->flowtables, list)
1186 37 flowtable_free(ft);
1187 /* this is implicitly releasing flowtables in the hashtable cache */
1188
2/2
✓ Branch 0 (39→37) taken 147 times.
✓ Branch 1 (39→40) taken 85224 times.
85371 list_for_each_entry_safe(ft, nft, &table->ft_cache.list, cache.list)
1189 147 flowtable_free(ft);
1190
2/2
✓ Branch 0 (43→41) taken 278 times.
✓ Branch 1 (43→44) taken 85224 times.
85502 list_for_each_entry_safe(obj, nobj, &table->objs, list)
1191 278 obj_free(obj);
1192 /* this is implicitly releasing objs in the hashtable cache */
1193
2/2
✓ Branch 0 (47→45) taken 2964 times.
✓ Branch 1 (47→48) taken 85224 times.
88188 list_for_each_entry_safe(obj, nobj, &table->obj_cache.list, cache.list)
1194 2964 obj_free(obj);
1195
1196 85224 handle_free(&table->handle);
1197 85224 scope_release(&table->scope);
1198 85224 cache_free(&table->chain_cache);
1199 85224 cache_free(&table->set_cache);
1200 85224 cache_free(&table->obj_cache);
1201 85224 cache_free(&table->ft_cache);
1202 85224 free(table);
1203 }
1204
1205 1289 struct table *table_get(struct table *table)
1206 {
1207
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 1289 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 1289 times.
1289 assert_refcount_safe(table->refcnt);
1208 1289 table->refcnt++;
1209 1289 return table;
1210 }
1211
1212 470 struct table *table_lookup_fuzzy(const struct handle *h,
1213 const struct nft_cache *cache)
1214 {
1215 struct string_misspell_state st;
1216 struct table *table;
1217
1218
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 470 times.
470 if (!h->table.name)
1219 ✗ return NULL;
1220
1221 470 string_misspell_init(&st);
1222
1223
2/2
✓ Branch 0 (8→6) taken 2 times.
✓ Branch 1 (8→9) taken 470 times.
472 list_for_each_entry(table, &cache->table_cache.list, cache.list) {
1224 2 string_misspell_update(table->handle.table.name,
1225 2 h->table.name, table, &st);
1226 }
1227 470 return st.obj;
1228 }
1229
1230 static const char *table_flags_name[TABLE_FLAGS_MAX] = {
1231 "dormant",
1232 "owner",
1233 "persist",
1234 };
1235
1236 22 const char *table_flag_name(uint32_t flag)
1237 {
1238
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 22 times.
22 if (flag >= TABLE_FLAGS_MAX)
1239 ✗ return "unknown";
1240
1241 22 return table_flags_name[flag];
1242 }
1243
1244 40 unsigned int parse_table_flag(const char *name)
1245 {
1246 int i;
1247
1248
1/2
✓ Branch 0 (6→3) taken 61 times.
✗ Branch 1 (6→7) not taken.
61 for (i = 0; i < TABLE_FLAGS_MAX; i++) {
1249
2/2
✓ Branch 0 (3→4) taken 40 times.
✓ Branch 1 (3→5) taken 21 times.
61 if (!strcmp(name, table_flags_name[i]))
1250 40 return 1 << i;
1251 }
1252 ✗ return 0;
1253 }
1254
1255 7018 static void table_print_flags(const struct table *table, const char **delim,
1256 struct output_ctx *octx)
1257 {
1258 7018 uint32_t flags = table->flags;
1259 7018 bool comma = false;
1260 int i;
1261
1262
2/2
✓ Branch 0 (2→3) taken 7007 times.
✓ Branch 1 (2→4) taken 11 times.
7018 if (!table->flags)
1263 7007 return;
1264
1265 11 nft_print(octx, "\tflags ");
1266
2/2
✓ Branch 0 (13→6) taken 33 times.
✓ Branch 1 (13→14) taken 11 times.
44 for (i = 0; i < TABLE_FLAGS_MAX; i++) {
1267
2/2
✓ Branch 0 (6→7) taken 14 times.
✓ Branch 1 (6→12) taken 19 times.
33 if (flags & (1 << i)) {
1268
2/2
✓ Branch 0 (7→8) taken 3 times.
✓ Branch 1 (7→9) taken 11 times.
14 if (comma)
1269 3 nft_print(octx, ",");
1270
1271 14 nft_print(octx, "%s", table_flag_name(i));
1272 14 comma = true;
1273 }
1274 }
1275 11 nft_print(octx, "\n");
1276 11 *delim = "\n";
1277 }
1278
1279 7018 static void table_print(const struct table *table, struct output_ctx *octx)
1280 {
1281 struct flowtable *flowtable;
1282 struct chain *chain;
1283 struct obj *obj;
1284 struct set *set;
1285 7018 const char *delim = "";
1286 7018 const char *family = family2str(table->handle.family);
1287
1288
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 7018 times.
7018 if (table->has_xt_stmts)
1289 ✗ fprintf(octx->error_fp,
1290 "# Warning: table %s %s is managed by iptables-nft, do not touch!\n",
1291 ✗ family, table->handle.table.name);
1292
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 7018 times.
7018 if (table->is_from_future)
1293 ✗ fprintf(octx->error_fp,
1294 "# Warning: table %s %s was created by a newer version of nftables? Content may be incomplete!\n",
1295 ✗ family, table->handle.table.name);
1296
1297 7018 nft_print(octx, "table %s %s {", family, table->handle.table.name);
1298
4/4
✓ Branch 0 (9→10) taken 6998 times.
✓ Branch 1 (9→11) taken 20 times.
✓ Branch 2 (10→11) taken 2 times.
✓ Branch 3 (10→12) taken 6996 times.
7018 if (nft_output_handle(octx) || table->flags & TABLE_F_OWNER)
1299 22 nft_print(octx, " #");
1300
2/2
✓ Branch 0 (13→14) taken 20 times.
✓ Branch 1 (13→15) taken 6998 times.
7018 if (nft_output_handle(octx))
1301 20 nft_print(octx, " handle %" PRIu64, table->handle.handle.id);
1302
2/2
✓ Branch 0 (15→16) taken 2 times.
✓ Branch 1 (15→18) taken 7016 times.
7018 if (table->flags & TABLE_F_OWNER)
1303 2 nft_print(octx, " progname %s", get_progname(table->owner));
1304
1305 7018 nft_print(octx, "\n");
1306 7018 table_print_flags(table, &delim, octx);
1307
1308
2/2
✓ Branch 0 (20→21) taken 1 times.
✓ Branch 1 (20→22) taken 7017 times.
7018 if (table->comment)
1309 1 nft_print(octx, "\tcomment \"%s\"\n", table->comment);
1310
1311
2/2
✓ Branch 0 (26→23) taken 300 times.
✓ Branch 1 (26→27) taken 7018 times.
7318 list_for_each_entry(obj, &table->obj_cache.list, cache.list) {
1312 300 nft_print(octx, "%s", delim);
1313 300 obj_print(obj, octx);
1314 300 delim = "\n";
1315 }
1316
2/2
✓ Branch 0 (35→28) taken 3504 times.
✓ Branch 1 (35→36) taken 7018 times.
10522 list_for_each_entry(set, &table->set_cache.list, cache.list) {
1317
2/2
✓ Branch 0 (29→30) taken 1676 times.
✓ Branch 1 (29→31) taken 1828 times.
3504 if (set_is_anonymous(set->flags))
1318 1676 continue;
1319 1828 nft_print(octx, "%s", delim);
1320 1828 set_print(set, octx);
1321 1828 delim = "\n";
1322 }
1323
2/2
✓ Branch 0 (40→37) taken 18 times.
✓ Branch 1 (40→41) taken 7018 times.
7036 list_for_each_entry(flowtable, &table->ft_cache.list, cache.list) {
1324 18 nft_print(octx, "%s", delim);
1325 18 flowtable_print(flowtable, octx);
1326 18 delim = "\n";
1327 }
1328
2/2
✓ Branch 0 (45→42) taken 8999 times.
✓ Branch 1 (45→46) taken 7018 times.
16017 list_for_each_entry(chain, &table->chain_cache.list, cache.list) {
1329 8999 nft_print(octx, "%s", delim);
1330 8999 chain_print(chain, octx);
1331 8999 delim = "\n";
1332 }
1333 7018 nft_print(octx, "}\n");
1334 7018 }
1335
1336 189024 struct cmd *cmd_alloc(enum cmd_ops op, enum cmd_obj obj,
1337 const struct handle *h, const struct location *loc,
1338 void *data)
1339 {
1340 struct cmd *cmd;
1341
1342
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 189024 times.
189024 assert(loc);
1343
1344 189024 cmd = xzalloc(sizeof(*cmd));
1345 189024 init_list_head(&cmd->list);
1346 189024 cmd->op = op;
1347 189024 cmd->obj = obj;
1348 189024 cmd->handle = *h;
1349 189024 cmd->location = *loc;
1350 189024 cmd->data = data;
1351 189024 cmd->attr = xzalloc_array(NFT_NLATTR_LOC_MAX,
1352 sizeof(struct nlerr_loc));
1353 189024 cmd->attr_array_len = NFT_NLATTR_LOC_MAX;
1354
1355 189024 return cmd;
1356 }
1357
1358 ✗ struct markup *markup_alloc(uint32_t format)
1359 {
1360 struct markup *markup;
1361
1362 ✗ markup = xmalloc(sizeof(struct markup));
1363 ✗ markup->format = format;
1364
1365 ✗ return markup;
1366 }
1367
1368 ✗ void markup_free(struct markup *m)
1369 {
1370 ✗ free(m);
1371 ✗ }
1372
1373 ✗ struct monitor *monitor_alloc(uint32_t format, uint32_t type,
1374 enum cmd_monitor_event event)
1375 {
1376 struct monitor *mon;
1377
1378 ✗ mon = xmalloc(sizeof(struct monitor));
1379 ✗ mon->format = format;
1380 ✗ mon->type = type;
1381 ✗ mon->event = event;
1382 ✗ mon->flags = 0;
1383
1384 ✗ return mon;
1385 }
1386
1387 ✗ void monitor_free(struct monitor *m)
1388 {
1389 ✗ free(m);
1390 ✗ }
1391
1392 189025 void cmd_free(struct cmd *cmd)
1393 {
1394
2/2
✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 189024 times.
189025 if (cmd == NULL)
1395 1 return;
1396
1397 189024 handle_free(&cmd->handle);
1398
2/2
✓ Branch 0 (5→6) taken 152782 times.
✓ Branch 1 (5→31) taken 36242 times.
189024 if (cmd->data != NULL) {
1399
7/11
✓ Branch 0 (6→7) taken 27340 times.
✓ Branch 1 (6→11) taken 9035 times.
✓ Branch 2 (6→12) taken 108851 times.
✓ Branch 3 (6→14) taken 5513 times.
✓ Branch 4 (6→16) taken 1451 times.
✗ Branch 5 (6→18) not taken.
✗ Branch 6 (6→20) not taken.
✗ Branch 7 (6→22) not taken.
✓ Branch 8 (6→24) taken 478 times.
✓ Branch 9 (6→26) taken 114 times.
✗ Branch 10 (6→28) not taken.
152782 switch (cmd->obj) {
1400 27340 case CMD_OBJ_ELEMENTS:
1401 27340 expr_free(cmd->expr);
1402
2/2
✓ Branch 0 (8→9) taken 27317 times.
✓ Branch 1 (8→10) taken 23 times.
27340 if (cmd->elem.set)
1403 27317 set_free(cmd->elem.set);
1404 27340 break;
1405 9035 case CMD_OBJ_SET:
1406 case CMD_OBJ_MAP:
1407 case CMD_OBJ_METER:
1408 case CMD_OBJ_SETELEMS:
1409 9035 set_free(cmd->set);
1410 9035 break;
1411 108851 case CMD_OBJ_RULE:
1412 108851 rule_free(cmd->rule);
1413 108851 break;
1414 5513 case CMD_OBJ_CHAIN:
1415 5513 chain_free(cmd->chain);
1416 5513 break;
1417 1451 case CMD_OBJ_TABLE:
1418 1451 table_free(cmd->table);
1419 1451 break;
1420 ✗ case CMD_OBJ_EXPR:
1421 ✗ expr_free(cmd->expr);
1422 ✗ break;
1423 ✗ case CMD_OBJ_MONITOR:
1424 ✗ monitor_free(cmd->monitor);
1425 ✗ break;
1426 ✗ case CMD_OBJ_MARKUP:
1427 ✗ markup_free(cmd->markup);
1428 ✗ break;
1429 478 case CMD_OBJ_COUNTER:
1430 case CMD_OBJ_QUOTA:
1431 case CMD_OBJ_CT_HELPER:
1432 case CMD_OBJ_CT_TIMEOUT:
1433 case CMD_OBJ_CT_EXPECT:
1434 case CMD_OBJ_LIMIT:
1435 case CMD_OBJ_SECMARK:
1436 case CMD_OBJ_SYNPROXY:
1437 case CMD_OBJ_TUNNEL:
1438 478 obj_free(cmd->object);
1439 478 break;
1440 114 case CMD_OBJ_FLOWTABLE:
1441 114 flowtable_free(cmd->flowtable);
1442 114 break;
1443 ✗ default:
1444 ✗ BUG("invalid command object type %u", cmd->obj);
1445 }
1446 }
1447 189024 free(cmd->attr);
1448 189024 free_const(cmd->arg);
1449 189024 free(cmd);
1450 }
1451
1452 #include <netlink.h>
1453 #include <mnl.h>
1454
1455 − static int __do_add_elements(struct netlink_ctx *ctx, struct cmd *cmd,
1456 struct set *set, struct expr *expr, uint32_t flags)
1457 {
1458 − expr_set(expr)->set_flags |= set->flags;
1459 − if (mnl_nft_setelem_add(ctx, cmd, set, expr, flags) < 0)
1460 − return -1;
1461
1462 − return 0;
1463 }
1464
1465 23307 static int do_add_elements(struct netlink_ctx *ctx, struct cmd *cmd,
1466 uint32_t flags)
1467 {
1468 23307 struct expr *init = cmd->expr;
1469 23307 struct set *set = cmd->elem.set;
1470
1471
3/4
✓ Branch 0 (3→4) taken 5324 times.
✓ Branch 1 (3→7) taken 17983 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→7) taken 5324 times.
28631 if (set_is_non_concat_range(set) &&
1472 5324 set_to_intervals(set, init, true) < 0)
1473 ✗ return -1;
1474
1475 23307 return __do_add_elements(ctx, cmd, set, init, flags);
1476 }
1477
1478 1022 static int do_add_setelems(struct netlink_ctx *ctx, struct cmd *cmd,
1479 uint32_t flags)
1480 {
1481 1022 struct set *set = cmd->set;
1482
1483
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1022 times.
1022 if (!set->init)
1484 ✗ return 0;
1485
1486 1022 return __do_add_elements(ctx, cmd, set, set->init, flags);
1487 }
1488
1489 6970 static int do_add_set(struct netlink_ctx *ctx, struct cmd *cmd,
1490 uint32_t flags)
1491 {
1492 6970 struct set *set = cmd->set;
1493
1494
2/2
✓ Branch 0 (2→3) taken 5204 times.
✓ Branch 1 (2→8) taken 1766 times.
6970 if (set->init != NULL) {
1495 /* Update set->init->size (NFTNL_SET_DESC_SIZE) before adding
1496 * the set to the kernel. Calling this from do_add_setelems()
1497 * comes too late which might result in spurious ENFILE errors.
1498 */
1499
3/4
✓ Branch 0 (4→5) taken 738 times.
✓ Branch 1 (4→8) taken 4466 times.
✗ Branch 2 (6→7) not taken.
✓ Branch 3 (6→8) taken 738 times.
5942 if (set_is_non_concat_range(set) &&
1500 738 set_to_intervals(set, set->init, true) < 0)
1501 ✗ return -1;
1502 }
1503
1504
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 6970 times.
6970 if (mnl_nft_set_add(ctx, cmd, flags) < 0)
1505 ✗ return -1;
1506
1507
2/2
✓ Branch 0 (12→13) taken 4119 times.
✓ Branch 1 (12→15) taken 2851 times.
6970 if (set_is_anonymous(set->flags))
1508 4119 return __do_add_elements(ctx, cmd, set, set->init, flags);
1509
1510 2851 return 0;
1511 }
1512
1513 150825 static int do_command_add(struct netlink_ctx *ctx, struct cmd *cmd, bool excl)
1514 {
1515
2/2
✓ Branch 0 (2→3) taken 226 times.
✓ Branch 1 (2→4) taken 150599 times.
150825 uint32_t flags = excl ? NLM_F_EXCL : 0;
1516
1517
2/2
✓ Branch 0 (6→7) taken 6516 times.
✓ Branch 1 (6→8) taken 144309 times.
150825 if (nft_output_echo(&ctx->nft->output))
1518 6516 flags |= NLM_F_ECHO;
1519
1520
8/9
✓ Branch 0 (8→9) taken 3394 times.
✓ Branch 1 (8→10) taken 7362 times.
✓ Branch 2 (8→11) taken 108220 times.
✓ Branch 3 (8→12) taken 6970 times.
✓ Branch 4 (8→13) taken 1022 times.
✓ Branch 5 (8→14) taken 23307 times.
✓ Branch 6 (8→15) taken 457 times.
✓ Branch 7 (8→16) taken 93 times.
✗ Branch 8 (8→17) not taken.
150825 switch (cmd->obj) {
1521 3394 case CMD_OBJ_TABLE:
1522 3394 return mnl_nft_table_add(ctx, cmd, flags);
1523 7362 case CMD_OBJ_CHAIN:
1524 7362 return mnl_nft_chain_add(ctx, cmd, flags);
1525 108220 case CMD_OBJ_RULE:
1526 108220 return mnl_nft_rule_add(ctx, cmd, flags | NLM_F_APPEND);
1527 6970 case CMD_OBJ_SET:
1528 6970 return do_add_set(ctx, cmd, flags);
1529 1022 case CMD_OBJ_SETELEMS:
1530 1022 return do_add_setelems(ctx, cmd, flags);
1531 23307 case CMD_OBJ_ELEMENTS:
1532 23307 return do_add_elements(ctx, cmd, flags);
1533 457 case CMD_OBJ_COUNTER:
1534 case CMD_OBJ_QUOTA:
1535 case CMD_OBJ_CT_HELPER:
1536 case CMD_OBJ_CT_TIMEOUT:
1537 case CMD_OBJ_CT_EXPECT:
1538 case CMD_OBJ_LIMIT:
1539 case CMD_OBJ_SECMARK:
1540 case CMD_OBJ_SYNPROXY:
1541 case CMD_OBJ_TUNNEL:
1542 457 return mnl_nft_obj_add(ctx, cmd, flags);
1543 93 case CMD_OBJ_FLOWTABLE:
1544 93 return mnl_nft_flowtable_add(ctx, cmd, flags);
1545 ✗ default:
1546 ✗ BUG("invalid command object type %u", cmd->obj);
1547 }
1548 return 0;
1549 }
1550
1551 11 static int do_command_replace(struct netlink_ctx *ctx, struct cmd *cmd)
1552 {
1553
1/2
✓ Branch 0 (2→3) taken 11 times.
✗ Branch 1 (2→4) not taken.
11 switch (cmd->obj) {
1554 11 case CMD_OBJ_RULE:
1555 11 return mnl_nft_rule_replace(ctx, cmd);
1556 ✗ default:
1557 ✗ BUG("invalid command object type %u", cmd->obj);
1558 }
1559 return 0;
1560 }
1561
1562 141 static int do_command_insert(struct netlink_ctx *ctx, struct cmd *cmd)
1563 {
1564 141 uint32_t flags = 0;
1565
1566
2/2
✓ Branch 0 (3→4) taken 23 times.
✓ Branch 1 (3→5) taken 118 times.
141 if (nft_output_echo(&ctx->nft->output))
1567 23 flags |= NLM_F_ECHO;
1568
1569
1/2
✓ Branch 0 (5→6) taken 141 times.
✗ Branch 1 (5→7) not taken.
141 switch (cmd->obj) {
1570 141 case CMD_OBJ_RULE:
1571 141 return mnl_nft_rule_add(ctx, cmd, flags);
1572 ✗ default:
1573 ✗ BUG("invalid command object type %u", cmd->obj);
1574 }
1575 return 0;
1576 }
1577
1578 2079 static int do_delete_setelems(struct netlink_ctx *ctx, struct cmd *cmd)
1579 {
1580 2079 const struct set *set = cmd->elem.set;
1581 2079 struct expr *expr = cmd->elem.expr;
1582
1583
3/4
✓ Branch 0 (3→4) taken 398 times.
✓ Branch 1 (3→7) taken 1681 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→7) taken 398 times.
2477 if (set_is_non_concat_range(set) &&
1584 398 set_to_intervals(set, expr, false) < 0)
1585 ✗ return -1;
1586
1587
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 2079 times.
2079 if (mnl_nft_setelem_del(ctx, cmd, &cmd->handle, set, cmd->elem.expr) < 0)
1588 ✗ return -1;
1589
1590 2079 return 0;
1591 }
1592
1593 2866 static int do_command_delete(struct netlink_ctx *ctx, struct cmd *cmd)
1594 {
1595
11/16
✓ Branch 0 (2→3) taken 270 times.
✓ Branch 1 (2→4) taken 354 times.
✓ Branch 2 (2→5) taken 11 times.
✓ Branch 3 (2→6) taken 88 times.
✓ Branch 4 (2→7) taken 2079 times.
✓ Branch 5 (2→8) taken 9 times.
✓ Branch 6 (2→9) taken 6 times.
✓ Branch 7 (2→10) taken 5 times.
✓ Branch 8 (2→11) taken 4 times.
✗ Branch 9 (2→12) not taken.
✓ Branch 10 (2→13) taken 4 times.
✗ Branch 11 (2→14) not taken.
✗ Branch 12 (2→15) not taken.
✗ Branch 13 (2→16) not taken.
✓ Branch 14 (2→17) taken 36 times.
✗ Branch 15 (2→18) not taken.
2866 switch (cmd->obj) {
1596 270 case CMD_OBJ_TABLE:
1597 270 return mnl_nft_table_del(ctx, cmd);
1598 354 case CMD_OBJ_CHAIN:
1599 354 return mnl_nft_chain_del(ctx, cmd);
1600 11 case CMD_OBJ_RULE:
1601 11 return mnl_nft_rule_del(ctx, cmd);
1602 88 case CMD_OBJ_SET:
1603 88 return mnl_nft_set_del(ctx, cmd);
1604 2079 case CMD_OBJ_ELEMENTS:
1605 2079 return do_delete_setelems(ctx, cmd);
1606 9 case CMD_OBJ_COUNTER:
1607 9 return mnl_nft_obj_del(ctx, cmd, NFT_OBJECT_COUNTER);
1608 6 case CMD_OBJ_QUOTA:
1609 6 return mnl_nft_obj_del(ctx, cmd, NFT_OBJECT_QUOTA);
1610 5 case CMD_OBJ_CT_HELPER:
1611 5 return mnl_nft_obj_del(ctx, cmd, NFT_OBJECT_CT_HELPER);
1612 4 case CMD_OBJ_CT_TIMEOUT:
1613 4 return mnl_nft_obj_del(ctx, cmd, NFT_OBJECT_CT_TIMEOUT);
1614 ✗ case CMD_OBJ_CT_EXPECT:
1615 ✗ return mnl_nft_obj_del(ctx, cmd, NFT_OBJECT_CT_EXPECT);
1616 4 case CMD_OBJ_LIMIT:
1617 4 return mnl_nft_obj_del(ctx, cmd, NFT_OBJECT_LIMIT);
1618 ✗ case CMD_OBJ_SECMARK:
1619 ✗ return mnl_nft_obj_del(ctx, cmd, NFT_OBJECT_SECMARK);
1620 ✗ case CMD_OBJ_SYNPROXY:
1621 ✗ return mnl_nft_obj_del(ctx, cmd, NFT_OBJECT_SYNPROXY);
1622 ✗ case CMD_OBJ_TUNNEL:
1623 ✗ return mnl_nft_obj_del(ctx, cmd, NFT_OBJECT_TUNNEL);
1624 36 case CMD_OBJ_FLOWTABLE:
1625 36 return mnl_nft_flowtable_del(ctx, cmd);
1626 ✗ default:
1627 ✗ BUG("invalid command object type %u", cmd->obj);
1628 }
1629 }
1630
1631 7018 static int do_list_table(struct netlink_ctx *ctx, struct table *table)
1632 {
1633 7018 table_print(table, &ctx->nft->output);
1634 7018 return 0;
1635 }
1636
1637 6 static int do_list_sets(struct netlink_ctx *ctx, struct cmd *cmd)
1638 {
1639 struct table *table;
1640 struct set *set;
1641
1642
2/2
✓ Branch 0 (26→3) taken 19 times.
✓ Branch 1 (26→27) taken 6 times.
25 list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
1643
2/2
✓ Branch 0 (3→4) taken 5 times.
✓ Branch 1 (3→6) taken 14 times.
19 if (cmd->handle.family != NFPROTO_UNSPEC &&
1644
2/2
✓ Branch 0 (4→5) taken 4 times.
✓ Branch 1 (4→6) taken 1 times.
5 cmd->handle.family != table->handle.family)
1645 4 continue;
1646
1647 15 nft_print(&ctx->nft->output, "table %s %s {\n",
1648 15 family2str(table->handle.family),
1649 table->handle.table.name);
1650
1651
2/2
✓ Branch 0 (23→9) taken 19 times.
✓ Branch 1 (23→24) taken 15 times.
34 list_for_each_entry(set, &table->set_cache.list, cache.list) {
1652
2/2
✓ Branch 0 (9→10) taken 16 times.
✓ Branch 1 (9→13) taken 3 times.
19 if (cmd->obj == CMD_OBJ_SETS &&
1653
2/2
✓ Branch 0 (11→12) taken 5 times.
✓ Branch 1 (11→13) taken 11 times.
16 !set_is_literal(set->flags))
1654 5 continue;
1655
2/2
✓ Branch 0 (13→14) taken 3 times.
✓ Branch 1 (13→17) taken 11 times.
14 if (cmd->obj == CMD_OBJ_METERS &&
1656
1/2
✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 3 times.
3 !set_is_meter_compat(set->flags))
1657 ✗ continue;
1658
1/2
✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→21) taken 14 times.
14 if (cmd->obj == CMD_OBJ_MAPS &&
1659 ✗ !map_is_literal(set->flags))
1660 ✗ continue;
1661 14 set_print(set, &ctx->nft->output);
1662 }
1663
1664 15 nft_print(&ctx->nft->output, "}\n");
1665 }
1666 6 return 0;
1667 }
1668
1669 2996 struct obj *obj_alloc(const struct location *loc)
1670 {
1671 struct obj *obj;
1672
1673
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2996 times.
2996 assert(loc);
1674
1675 2996 obj = xzalloc(sizeof(*obj));
1676 2996 obj->location = *loc;
1677
1678 2996 obj->refcnt = 1;
1679 2996 return obj;
1680 }
1681
1682 745 struct obj *obj_get(struct obj *obj)
1683 {
1684
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 745 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 745 times.
745 assert_refcount_safe(obj->refcnt);
1685 745 obj->refcnt++;
1686 745 return obj;
1687 }
1688
1689 3741 void obj_free(struct obj *obj)
1690 {
1691
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 3741 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 3741 times.
3741 assert_refcount_safe(obj->refcnt);
1692
2/2
✓ Branch 0 (8→9) taken 745 times.
✓ Branch 1 (8→10) taken 2996 times.
3741 if (--obj->refcnt > 0)
1693 745 return;
1694 2996 free_const(obj->comment);
1695 2996 handle_free(&obj->handle);
1696
3/3
✓ Branch 0 (11→12) taken 67 times.
✓ Branch 1 (11→17) taken 29 times.
✓ Branch 2 (11→25) taken 2900 times.
2996 switch (obj->type) {
1697 67 case NFT_OBJECT_CT_TIMEOUT: {
1698 struct timeout_state *ts, *next;
1699
1700
2/2
✓ Branch 0 (15→13) taken 3 times.
✓ Branch 1 (15→16) taken 67 times.
70 list_for_each_entry_safe(ts, next, &obj->ct_timeout.timeout_list, head) {
1701 3 list_del(&ts->head);
1702 3 free_const(ts->timeout_str);
1703 3 free(ts);
1704 }
1705 }
1706 67 break;
1707 29 case NFT_OBJECT_TUNNEL:
1708 29 expr_free(obj->tunnel.src);
1709 29 expr_free(obj->tunnel.dst);
1710
2/2
✓ Branch 0 (19→20) taken 7 times.
✓ Branch 1 (19→24) taken 22 times.
29 if (obj->tunnel.type == TUNNEL_GENEVE) {
1711 struct tunnel_geneve *geneve, *next;
1712
1713
2/2
✓ Branch 0 (23→21) taken 21 times.
✓ Branch 1 (23→24) taken 7 times.
28 list_for_each_entry_safe(geneve, next, &obj->tunnel.geneve_opts, list) {
1714 21 list_del(&geneve->list);
1715 21 free(geneve);
1716 }
1717 }
1718 29 break;
1719 2900 default:
1720 2900 break;
1721 }
1722 2996 free(obj);
1723 }
1724
1725 32 struct obj *obj_lookup_fuzzy(const char *obj_name,
1726 const struct nft_cache *cache,
1727 const struct table **t)
1728 {
1729 struct string_misspell_state st;
1730 struct table *table;
1731 struct obj *obj;
1732
1733
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 32 times.
32 if (!obj_name)
1734 ✗ return NULL;
1735
1736 32 string_misspell_init(&st);
1737
1738
2/2
✓ Branch 0 (13→6) taken 31 times.
✓ Branch 1 (13→14) taken 32 times.
63 list_for_each_entry(table, &cache->table_cache.list, cache.list) {
1739
1/2
✗ Branch 0 (11→7) not taken.
✓ Branch 1 (11→12) taken 31 times.
31 list_for_each_entry(obj, &table->obj_cache.list, cache.list) {
1740 ✗ if (string_misspell_update(obj->handle.obj.name,
1741 obj_name, obj, &st))
1742 ✗ *t = table;
1743 }
1744 }
1745 32 return st.obj;
1746 }
1747
1748 48 static void print_proto_name_proto(uint8_t l4, struct output_ctx *octx)
1749 {
1750 char name[NFT_PROTONAME_MAXSIZE];
1751
1752
1/2
✓ Branch 0 (3→4) taken 48 times.
✗ Branch 1 (3→5) not taken.
48 if (nft_getprotobynumber(l4, name, sizeof(name)))
1753 48 nft_print(octx, "%s", name);
1754 else
1755 ✗ nft_print(octx, "%d", l4);
1756 48 }
1757
1758 19 static void print_proto_timeout_policy(uint8_t l4, const uint32_t *timeout,
1759 struct print_fmt_options *opts,
1760 struct output_ctx *octx)
1761 {
1762 19 bool comma = false;
1763 unsigned int i;
1764
1765 19 nft_print(octx, "%s%spolicy = { ", opts->tab, opts->tab);
1766
2/2
✓ Branch 0 (11→4) taken 146 times.
✓ Branch 1 (11→12) taken 19 times.
165 for (i = 0; i < timeout_protocol[l4].array_size; i++) {
1767
2/2
✓ Branch 0 (4→5) taken 38 times.
✓ Branch 1 (4→10) taken 108 times.
146 if (timeout[i] != timeout_protocol[l4].dflt_timeout[i]) {
1768 uint64_t timeout_ms;
1769
1770
2/2
✓ Branch 0 (5→6) taken 19 times.
✓ Branch 1 (5→7) taken 19 times.
38 if (comma)
1771 19 nft_print(octx, ", ");
1772 38 timeout_ms = timeout[i] * 1000u;
1773 38 nft_print(octx, "%s : ",
1774 38 timeout_protocol[l4].state_to_name[i]);
1775 38 time_print(timeout_ms, octx);
1776 38 comma = true;
1777 }
1778 }
1779 19 nft_print(octx, " }%s", opts->stmt_separator);
1780 19 }
1781
1782 11 static const char *synproxy_sack_to_str(const uint32_t flags)
1783 {
1784
2/2
✓ Branch 0 (2→3) taken 6 times.
✓ Branch 1 (2→4) taken 5 times.
11 if (flags & NF_SYNPROXY_OPT_SACK_PERM)
1785 6 return "sack-perm";
1786
1787 5 return "";
1788 }
1789
1790 11 static const char *synproxy_timestamp_to_str(const uint32_t flags)
1791 {
1792
2/2
✓ Branch 0 (2→3) taken 5 times.
✓ Branch 1 (2→4) taken 6 times.
11 if (flags & NF_SYNPROXY_OPT_TIMESTAMP)
1793 5 return "timestamp";
1794
1795 6 return "";
1796 }
1797
1798 15 int tunnel_geneve_data_str2array(const char *hexstr,
1799 uint8_t *out_data,
1800 uint32_t *out_len)
1801 {
1802 15 char bytestr[3] = {0};
1803 size_t len;
1804
1805
2/6
✓ Branch 0 (2→3) taken 15 times.
✗ Branch 1 (2→6) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→5) taken 15 times.
✗ Branch 4 (4→5) not taken.
✗ Branch 5 (4→6) not taken.
15 if (hexstr[0] == '0' && (hexstr[1] == 'x' || hexstr[1] == 'X'))
1806 15 hexstr += 2;
1807 else
1808 ✗ return -1;
1809
1810 15 len = strlen(hexstr);
1811
1/2
✗ Branch 0 (5→7) not taken.
✓ Branch 1 (5→8) taken 15 times.
15 if (len % 4 != 0)
1812 ✗ return -1;
1813
1814 15 len = len / 2;
1815
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 15 times.
15 if (len > NFTNL_TUNNEL_GENEVE_DATA_MAXLEN)
1816 ✗ return -1;
1817
1818
2/2
✓ Branch 0 (15→11) taken 80 times.
✓ Branch 1 (15→16) taken 15 times.
95 for (size_t i = 0; i < len; i++) {
1819 uint32_t value;
1820 char *endptr;
1821
1822 80 bytestr[0] = hexstr[i * 2];
1823 80 bytestr[1] = hexstr[i * 2 + 1];
1824
1825 80 value = strtoul(bytestr, &endptr, 16);
1826
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 80 times.
80 if (*endptr != '\0')
1827 ✗ return -1;
1828
1829 80 out_data[i] = (uint8_t) value;
1830 }
1831 15 *out_len = (uint8_t) len;
1832
1833 15 return 0;
1834 }
1835
1836 690 static void obj_print_comment(const struct obj *obj,
1837 struct print_fmt_options *opts,
1838 struct output_ctx *octx)
1839 {
1840
2/2
✓ Branch 0 (2→3) taken 8 times.
✓ Branch 1 (2→4) taken 682 times.
690 if (obj->comment)
1841 8 nft_print(octx, "%s%s%scomment \"%s\"",
1842 opts->nl, opts->tab, opts->tab,
1843 8 obj->comment);
1844 690 }
1845
1846 690 static void obj_print_data(const struct obj *obj,
1847 struct print_fmt_options *opts,
1848 struct output_ctx *octx)
1849 {
1850
9/10
✓ Branch 0 (2→3) taken 544 times.
✓ Branch 1 (2→13) taken 57 times.
✓ Branch 2 (2→31) taken 1 times.
✓ Branch 3 (2→39) taken 16 times.
✓ Branch 4 (2→51) taken 19 times.
✓ Branch 5 (2→64) taken 13 times.
✓ Branch 6 (2→81) taken 25 times.
✓ Branch 7 (2→110) taken 11 times.
✓ Branch 8 (2→128) taken 4 times.
✗ Branch 9 (2→185) not taken.
690 switch (obj->type) {
1851 544 case NFT_OBJECT_COUNTER:
1852 544 nft_print(octx, " %s {", obj->handle.obj.name);
1853
2/2
✓ Branch 0 (5→6) taken 5 times.
✓ Branch 1 (5→7) taken 539 times.
544 if (nft_output_handle(octx))
1854 5 nft_print(octx, " # handle %" PRIu64, obj->handle.handle.id);
1855
1856 544 obj_print_comment(obj, opts, octx);
1857
2/2
✓ Branch 0 (9→10) taken 22 times.
✓ Branch 1 (9→11) taken 522 times.
544 if (nft_output_stateless(octx))
1858 22 nft_print(octx, "%s", opts->nl);
1859 else
1860 522 nft_print(octx, "%s%s%spackets %" PRIu64 " bytes %" PRIu64 "%s",
1861 opts->nl, opts->tab, opts->tab,
1862 522 obj->counter.packets, obj->counter.bytes, opts->nl);
1863 544 break;
1864 57 case NFT_OBJECT_QUOTA: {
1865 const char *data_unit;
1866 uint64_t bytes;
1867
1868 57 nft_print(octx, " %s {", obj->handle.obj.name);
1869
2/2
✓ Branch 0 (15→16) taken 6 times.
✓ Branch 1 (15→17) taken 51 times.
57 if (nft_output_handle(octx))
1870 6 nft_print(octx, " # handle %" PRIu64, obj->handle.handle.id);
1871
1872 57 obj_print_comment(obj, opts, octx);
1873 57 nft_print(octx, "%s%s%s", opts->nl, opts->tab, opts->tab);
1874 57 data_unit = get_rate(obj->quota.bytes, &bytes);
1875 57 nft_print(octx, "%s%" PRIu64 " %s",
1876
2/2
✓ Branch 0 (20→21) taken 22 times.
✓ Branch 1 (20→22) taken 35 times.
57 obj->quota.flags & NFT_QUOTA_F_INV ? "over " : "",
1877 bytes, data_unit);
1878
4/4
✓ Branch 0 (25→26) taken 39 times.
✓ Branch 1 (25→29) taken 18 times.
✓ Branch 2 (26→27) taken 3 times.
✓ Branch 3 (26→29) taken 36 times.
57 if (!nft_output_stateless(octx) && obj->quota.used) {
1879 3 data_unit = get_rate(obj->quota.used, &bytes);
1880 3 nft_print(octx, " used %" PRIu64 " %s",
1881 bytes, data_unit);
1882 }
1883 57 nft_print(octx, "%s", opts->nl);
1884 }
1885 57 break;
1886 1 case NFT_OBJECT_SECMARK:
1887 1 nft_print(octx, " %s {", obj->handle.obj.name);
1888
1/2
✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 1 times.
1 if (nft_output_handle(octx))
1889 ✗ nft_print(octx, " # handle %" PRIu64, obj->handle.handle.id);
1890
1891 1 obj_print_comment(obj, opts, octx);
1892 1 nft_print(octx, "%s%s%s", opts->nl, opts->tab, opts->tab);
1893 1 nft_print(octx, "\"%s\"%s", obj->secmark.ctx, opts->nl);
1894 1 break;
1895 16 case NFT_OBJECT_CT_HELPER:
1896 16 nft_print(octx, " %s {", obj->handle.obj.name);
1897
1/2
✗ Branch 0 (41→42) not taken.
✓ Branch 1 (41→43) taken 16 times.
16 if (nft_output_handle(octx))
1898 ✗ nft_print(octx, " # handle %" PRIu64, obj->handle.handle.id);
1899
1900 16 obj_print_comment(obj, opts, octx);
1901 16 nft_print(octx, "%s", opts->nl);
1902 16 nft_print(octx, "%s%stype \"%s\" protocol ",
1903 16 opts->tab, opts->tab, obj->ct_helper.name);
1904 16 print_proto_name_proto(obj->ct_helper.l4proto, octx);
1905 16 nft_print(octx, "%s", opts->stmt_separator);
1906 16 nft_print(octx, "%s%sl3proto %s%s",
1907 opts->tab, opts->tab,
1908 16 family2str(obj->ct_helper.l3proto),
1909 opts->stmt_separator);
1910 16 break;
1911 19 case NFT_OBJECT_CT_TIMEOUT:
1912 19 nft_print(octx, " %s {", obj->handle.obj.name);
1913
1/2
✗ Branch 0 (53→54) not taken.
✓ Branch 1 (53→55) taken 19 times.
19 if (nft_output_handle(octx))
1914 ✗ nft_print(octx, " # handle %" PRIu64, obj->handle.handle.id);
1915
1916 19 obj_print_comment(obj, opts, octx);
1917 19 nft_print(octx, "%s", opts->nl);
1918 19 nft_print(octx, "%s%sprotocol ", opts->tab, opts->tab);
1919 19 print_proto_name_proto(obj->ct_timeout.l4proto, octx);
1920 19 nft_print(octx, "%s", opts->stmt_separator);
1921 19 nft_print(octx, "%s%sl3proto %s%s",
1922 opts->tab, opts->tab,
1923 19 family2str(obj->ct_timeout.l3proto),
1924 opts->stmt_separator);
1925 19 print_proto_timeout_policy(obj->ct_timeout.l4proto,
1926 19 obj->ct_timeout.timeout, opts, octx);
1927 19 break;
1928 13 case NFT_OBJECT_CT_EXPECT:
1929 13 nft_print(octx, " %s {", obj->handle.obj.name);
1930
1/2
✗ Branch 0 (66→67) not taken.
✓ Branch 1 (66→68) taken 13 times.
13 if (nft_output_handle(octx))
1931 ✗ nft_print(octx, " # handle %" PRIu64, obj->handle.handle.id);
1932
1933 13 obj_print_comment(obj, opts, octx);
1934 13 nft_print(octx, "%s", opts->nl);
1935 13 nft_print(octx, "%s%sprotocol ", opts->tab, opts->tab);
1936 13 print_proto_name_proto(obj->ct_expect.l4proto, octx);
1937 13 nft_print(octx, "%s", opts->stmt_separator);
1938 13 nft_print(octx, "%s%sdport %d%s",
1939 opts->tab, opts->tab,
1940 13 obj->ct_expect.dport,
1941 opts->stmt_separator);
1942 13 nft_print(octx, "%s%stimeout ", opts->tab, opts->tab);
1943 13 time_print(obj->ct_expect.timeout, octx);
1944 13 nft_print(octx, "%s", opts->stmt_separator);
1945 13 nft_print(octx, "%s%ssize %d%s",
1946 opts->tab, opts->tab,
1947 13 obj->ct_expect.size,
1948 opts->stmt_separator);
1949 13 nft_print(octx, "%s%sl3proto %s%s",
1950 opts->tab, opts->tab,
1951 13 family2str(obj->ct_expect.l3proto),
1952 opts->stmt_separator);
1953 13 break;
1954 25 case NFT_OBJECT_LIMIT: {
1955 25 bool inv = obj->limit.flags & NFT_LIMIT_F_INV;
1956 const char *data_unit;
1957 uint64_t rate;
1958
1959 25 nft_print(octx, " %s {", obj->handle.obj.name);
1960
1/2
✗ Branch 0 (83→84) not taken.
✓ Branch 1 (83→85) taken 25 times.
25 if (nft_output_handle(octx))
1961 ✗ nft_print(octx, " # handle %" PRIu64, obj->handle.handle.id);
1962
1963 25 obj_print_comment(obj, opts, octx);
1964 25 nft_print(octx, "%s%s%s", opts->nl, opts->tab, opts->tab);
1965
2/3
✓ Branch 0 (87→88) taken 13 times.
✓ Branch 1 (87→97) taken 12 times.
✗ Branch 2 (87→108) not taken.
25 switch (obj->limit.type) {
1966 13 case NFT_LIMIT_PKTS:
1967 13 nft_print(octx, "rate %s%" PRIu64 "/%s",
1968
1/2
✗ Branch 0 (89→90) not taken.
✓ Branch 1 (89→91) taken 13 times.
13 inv ? "over " : "", obj->limit.rate,
1969 13 get_unit(obj->limit.unit));
1970
2/4
✓ Branch 0 (93→94) taken 13 times.
✗ Branch 1 (93→96) not taken.
✗ Branch 2 (94→95) not taken.
✓ Branch 3 (94→96) taken 13 times.
13 if (obj->limit.burst > 0 && obj->limit.burst != 5)
1971 ✗ nft_print(octx, " burst %u packets",
1972 ✗ obj->limit.burst);
1973 13 break;
1974 12 case NFT_LIMIT_PKT_BYTES:
1975 12 data_unit = get_rate(obj->limit.rate, &rate);
1976
1977
2/2
✓ Branch 0 (99→100) taken 9 times.
✓ Branch 1 (99→101) taken 3 times.
12 nft_print(octx, "rate %s%" PRIu64 " %s/%s",
1978 inv ? "over " : "", rate, data_unit,
1979 12 get_unit(obj->limit.unit));
1980
2/2
✓ Branch 0 (103→104) taken 9 times.
✓ Branch 1 (103→107) taken 3 times.
12 if (obj->limit.burst > 0) {
1981 uint64_t burst;
1982
1983 9 data_unit = get_rate(obj->limit.burst, &burst);
1984 9 nft_print(octx, " burst %"PRIu64" %s",
1985 burst, data_unit);
1986 }
1987 12 break;
1988 }
1989 25 nft_print(octx, "%s", opts->nl);
1990 }
1991 25 break;
1992 11 case NFT_OBJECT_SYNPROXY: {
1993 11 uint32_t flags = obj->synproxy.flags;
1994 11 const char *sack_str = synproxy_sack_to_str(flags);
1995 11 const char *ts_str = synproxy_timestamp_to_str(flags);
1996
1997 11 nft_print(octx, " %s {", obj->handle.obj.name);
1998
1/2
✗ Branch 0 (114→115) not taken.
✓ Branch 1 (114→116) taken 11 times.
11 if (nft_output_handle(octx))
1999 ✗ nft_print(octx, " # handle %" PRIu64, obj->handle.handle.id);
2000
2001 11 obj_print_comment(obj, opts, octx);
2002
2003
1/2
✓ Branch 0 (117→118) taken 11 times.
✗ Branch 1 (117→120) not taken.
11 if (flags & NF_SYNPROXY_OPT_MSS) {
2004 11 nft_print(octx, "%s%s%s", opts->nl, opts->tab, opts->tab);
2005 11 nft_print(octx, "mss %u", obj->synproxy.mss);
2006 }
2007
1/2
✓ Branch 0 (120→121) taken 11 times.
✗ Branch 1 (120→123) not taken.
11 if (flags & NF_SYNPROXY_OPT_WSCALE) {
2008 11 nft_print(octx, "%s%s%s", opts->nl, opts->tab, opts->tab);
2009 11 nft_print(octx, "wscale %u", obj->synproxy.wscale);
2010 }
2011
2/2
✓ Branch 0 (123→124) taken 6 times.
✓ Branch 1 (123→126) taken 5 times.
11 if (flags & (NF_SYNPROXY_OPT_TIMESTAMP | NF_SYNPROXY_OPT_SACK_PERM)) {
2012 6 nft_print(octx, "%s%s%s", opts->nl, opts->tab, opts->tab);
2013 6 nft_print(octx, "%s %s", ts_str, sack_str);
2014 }
2015 11 nft_print(octx, "%s", opts->stmt_separator);
2016 }
2017 11 break;
2018 4 case NFT_OBJECT_TUNNEL:
2019 4 nft_print(octx, " %s {", obj->handle.obj.name);
2020
1/2
✗ Branch 0 (130→131) not taken.
✓ Branch 1 (130→132) taken 4 times.
4 if (nft_output_handle(octx))
2021 ✗ nft_print(octx, " # handle %" PRIu64, obj->handle.handle.id);
2022
2023 4 obj_print_comment(obj, opts, octx);
2024
2025 4 nft_print(octx, "%s%s%sid %u",
2026 4 opts->nl, opts->tab, opts->tab, obj->tunnel.id);
2027
2028
1/2
✓ Branch 0 (134→135) taken 4 times.
✗ Branch 1 (134→141) not taken.
4 if (obj->tunnel.src) {
2029
1/2
✓ Branch 0 (135→136) taken 4 times.
✗ Branch 1 (135→138) not taken.
4 if (obj->tunnel.src->len == 32) {
2030 4 nft_print(octx, "%s%s%sip saddr ",
2031 opts->nl, opts->tab, opts->tab);
2032 4 expr_print(obj->tunnel.src, octx);
2033 ✗ } else if (obj->tunnel.src->len == 128) {
2034 ✗ nft_print(octx, "%s%s%sip6 saddr ",
2035 opts->nl, opts->tab, opts->tab);
2036 ✗ expr_print(obj->tunnel.src, octx);
2037 }
2038 }
2039
1/2
✓ Branch 0 (141→142) taken 4 times.
✗ Branch 1 (141→148) not taken.
4 if (obj->tunnel.dst) {
2040
1/2
✓ Branch 0 (142→143) taken 4 times.
✗ Branch 1 (142→145) not taken.
4 if (obj->tunnel.dst->len == 32) {
2041 4 nft_print(octx, "%s%s%sip daddr ",
2042 opts->nl, opts->tab, opts->tab);
2043 4 expr_print(obj->tunnel.dst, octx);
2044 ✗ } else if (obj->tunnel.dst->len == 128) {
2045 ✗ nft_print(octx, "%s%s%sip6 daddr ",
2046 opts->nl, opts->tab, opts->tab);
2047 ✗ expr_print(obj->tunnel.dst, octx);
2048 }
2049 }
2050
1/2
✓ Branch 0 (148→149) taken 4 times.
✗ Branch 1 (148→150) not taken.
4 if (obj->tunnel.sport) {
2051 4 nft_print(octx, "%s%s%ssport %u",
2052 opts->nl, opts->tab, opts->tab,
2053 4 obj->tunnel.sport);
2054 }
2055
1/2
✓ Branch 0 (150→151) taken 4 times.
✗ Branch 1 (150→152) not taken.
4 if (obj->tunnel.dport) {
2056 4 nft_print(octx, "%s%s%sdport %u",
2057 opts->nl, opts->tab, opts->tab,
2058 4 obj->tunnel.dport);
2059 }
2060
1/2
✓ Branch 0 (152→153) taken 4 times.
✗ Branch 1 (152→154) not taken.
4 if (obj->tunnel.tos) {
2061 4 nft_print(octx, "%s%s%stos %u",
2062 opts->nl, opts->tab, opts->tab,
2063 4 obj->tunnel.tos);
2064 }
2065
1/2
✓ Branch 0 (154→155) taken 4 times.
✗ Branch 1 (154→156) not taken.
4 if (obj->tunnel.ttl) {
2066 4 nft_print(octx, "%s%s%sttl %u",
2067 opts->nl, opts->tab, opts->tab,
2068 4 obj->tunnel.ttl);
2069 }
2070
3/4
✓ Branch 0 (156→157) taken 2 times.
✓ Branch 1 (156→168) taken 1 times.
✓ Branch 2 (156→172) taken 1 times.
✗ Branch 3 (156→182) not taken.
4 switch (obj->tunnel.type) {
2071 2 case TUNNEL_ERSPAN:
2072 2 nft_print(octx, "%s%s%serspan {",
2073 opts->nl, opts->tab, opts->tab);
2074 2 nft_print(octx, "%s%s%s%sversion %u",
2075 opts->nl, opts->tab, opts->tab, opts->tab,
2076 2 obj->tunnel.erspan.version);
2077
2/2
✓ Branch 0 (159→160) taken 1 times.
✓ Branch 1 (159→161) taken 1 times.
2 if (obj->tunnel.erspan.version == 1) {
2078 1 nft_print(octx, "%s%s%s%sindex %u",
2079 opts->nl, opts->tab, opts->tab, opts->tab,
2080 1 obj->tunnel.erspan.v1.index);
2081 } else {
2082 1 nft_print(octx, "%s%s%s%sdirection %s",
2083 opts->nl, opts->tab, opts->tab, opts->tab,
2084
1/2
✗ Branch 0 (161→162) not taken.
✓ Branch 1 (161→163) taken 1 times.
1 obj->tunnel.erspan.v2.direction ? "egress"
2085 : "ingress");
2086 1 nft_print(octx, "%s%s%s%sid %u",
2087 opts->nl, opts->tab, opts->tab, opts->tab,
2088 1 obj->tunnel.erspan.v2.hwid);
2089 }
2090 2 nft_print(octx, "%s%s%s}",
2091 opts->nl, opts->tab, opts->tab);
2092 2 break;
2093 1 case TUNNEL_VXLAN:
2094 1 nft_print(octx, "%s%s%svxlan {",
2095 opts->nl, opts->tab, opts->tab);
2096 1 nft_print(octx, "%s%s%s%sgbp %u",
2097 opts->nl, opts->tab, opts->tab, opts->tab,
2098 1 obj->tunnel.vxlan.gbp);
2099 1 nft_print(octx, "%s%s%s}",
2100 opts->nl, opts->tab, opts->tab);
2101 1 break;
2102 1 case TUNNEL_GENEVE:
2103 struct tunnel_geneve *geneve;
2104
2105 1 nft_print(octx, "%s%s%sgeneve {", opts->nl, opts->tab, opts->tab);
2106
2/2
✓ Branch 0 (179→174) taken 3 times.
✓ Branch 1 (179→180) taken 1 times.
4 list_for_each_entry(geneve, &obj->tunnel.geneve_opts, list) {
2107 char data_str[256];
2108 3 int offset = 0;
2109
2110
2/2
✓ Branch 0 (176→175) taken 16 times.
✓ Branch 1 (176→177) taken 3 times.
19 for (uint32_t i = 0; i < geneve->data_len; i++) {
2111 16 offset += snprintf(data_str + offset,
2112 16 geneve->data_len,
2113 "%x",
2114 16 geneve->data[i]);
2115 }
2116 3 nft_print(octx, "%s%s%s%sclass 0x%x opt-type 0x%x data \"0x%s\"",
2117 opts->nl, opts->tab, opts->tab, opts->tab,
2118 3 geneve->geneve_class, geneve->type, data_str);
2119
2120 }
2121 1 nft_print(octx, "%s%s%s}", opts->nl, opts->tab, opts->tab);
2122 1 break;
2123 ✗ default:
2124 ✗ break;
2125 }
2126
2127 4 nft_print(octx, "%s", opts->stmt_separator);
2128 4 break;
2129 ✗ default:
2130 ✗ nft_print(octx, " unknown {%s", opts->nl);
2131 ✗ break;
2132 }
2133 690 }
2134
2135 static const char * const obj_type_name_array[] = {
2136 [NFT_OBJECT_COUNTER] = "counter",
2137 [NFT_OBJECT_QUOTA] = "quota",
2138 [NFT_OBJECT_CT_HELPER] = "ct helper",
2139 [NFT_OBJECT_LIMIT] = "limit",
2140 [NFT_OBJECT_CT_TIMEOUT] = "ct timeout",
2141 [NFT_OBJECT_SECMARK] = "secmark",
2142 [NFT_OBJECT_SYNPROXY] = "synproxy",
2143 [NFT_OBJECT_CT_EXPECT] = "ct expectation",
2144 [NFT_OBJECT_TUNNEL] = "tunnel",
2145 };
2146
2147 863 const char *obj_type_name(unsigned int type)
2148 {
2149
2/4
✓ Branch 0 (2→3) taken 863 times.
✗ Branch 1 (2→5) not taken.
✓ Branch 2 (3→4) taken 863 times.
✗ Branch 3 (3→5) not taken.
863 assert(type <= NFT_OBJECT_MAX && obj_type_name_array[type]);
2150
2151 863 return obj_type_name_array[type];
2152 }
2153
2154 static uint32_t obj_type_cmd_array[NFT_OBJECT_MAX + 1] = {
2155 [NFT_OBJECT_COUNTER] = CMD_OBJ_COUNTER,
2156 [NFT_OBJECT_QUOTA] = CMD_OBJ_QUOTA,
2157 [NFT_OBJECT_CT_HELPER] = CMD_OBJ_CT_HELPER,
2158 [NFT_OBJECT_LIMIT] = CMD_OBJ_LIMIT,
2159 [NFT_OBJECT_CT_TIMEOUT] = CMD_OBJ_CT_TIMEOUT,
2160 [NFT_OBJECT_SECMARK] = CMD_OBJ_SECMARK,
2161 [NFT_OBJECT_SYNPROXY] = CMD_OBJ_SYNPROXY,
2162 [NFT_OBJECT_CT_EXPECT] = CMD_OBJ_CT_EXPECT,
2163 [NFT_OBJECT_TUNNEL] = CMD_OBJ_TUNNEL,
2164 };
2165
2166 276 enum cmd_obj obj_type_to_cmd(uint32_t type)
2167 {
2168
2/4
✓ Branch 0 (2→3) taken 276 times.
✗ Branch 1 (2→5) not taken.
✓ Branch 2 (3→4) taken 276 times.
✗ Branch 3 (3→5) not taken.
276 assert(type <= NFT_OBJECT_MAX && obj_type_cmd_array[type]);
2169
2170 276 return obj_type_cmd_array[type];
2171 }
2172
2173 690 static void obj_print_declaration(const struct obj *obj,
2174 struct print_fmt_options *opts,
2175 struct output_ctx *octx)
2176 {
2177 690 nft_print(octx, "%s%s", opts->tab, obj_type_name(obj->type));
2178
2179
2/2
✓ Branch 0 (4→5) taken 6 times.
✓ Branch 1 (4→6) taken 684 times.
690 if (opts->family != NULL)
2180 6 nft_print(octx, " %s", opts->family);
2181
2182
2/2
✓ Branch 0 (6→7) taken 6 times.
✓ Branch 1 (6→8) taken 684 times.
690 if (opts->table != NULL)
2183 6 nft_print(octx, " %s", opts->table);
2184
2185 690 obj_print_data(obj, opts, octx);
2186
2187 690 nft_print(octx, "%s}%s", opts->tab, opts->nl);
2188 690 }
2189
2190 300 void obj_print(const struct obj *obj, struct output_ctx *octx)
2191 {
2192 300 struct print_fmt_options opts = {
2193 .tab = "\t",
2194 .nl = "\n",
2195 .stmt_separator = "\n",
2196 };
2197
2198 300 obj_print_declaration(obj, &opts, octx);
2199 300 }
2200
2201 6 void obj_print_plain(const struct obj *obj, struct output_ctx *octx)
2202 {
2203 12 struct print_fmt_options opts = {
2204 .tab = "",
2205 .nl = " ",
2206 6 .table = obj->handle.table.name,
2207 6 .family = family2str(obj->handle.family),
2208 .stmt_separator = "; ",
2209 };
2210
2211 6 obj_print_declaration(obj, &opts, octx);
2212 6 }
2213
2214 385 static int do_list_obj(struct netlink_ctx *ctx, struct cmd *cmd, uint32_t type)
2215 {
2216 385 struct print_fmt_options opts = {
2217 .tab = "\t",
2218 .nl = "\n",
2219 .stmt_separator = "\n",
2220 };
2221 struct table *table;
2222 struct obj *obj;
2223
2224
2/2
✓ Branch 0 (24→3) taken 387 times.
✓ Branch 1 (24→25) taken 385 times.
772 list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
2225
2/2
✓ Branch 0 (3→4) taken 375 times.
✓ Branch 1 (3→6) taken 12 times.
387 if (cmd->handle.family != NFPROTO_UNSPEC &&
2226
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 375 times.
375 cmd->handle.family != table->handle.family)
2227 ✗ continue;
2228
2229
2/2
✓ Branch 0 (6→7) taken 373 times.
✓ Branch 1 (6→9) taken 14 times.
387 if (cmd->handle.table.name != NULL &&
2230
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 373 times.
373 strcmp(cmd->handle.table.name, table->handle.table.name))
2231 ✗ continue;
2232
2233
2/2
✓ Branch 0 (10→11) taken 3 times.
✓ Branch 1 (10→12) taken 384 times.
387 if (list_empty(&table->obj_cache.list))
2234 3 continue;
2235
2236 384 nft_print(&ctx->nft->output, "table %s %s {\n",
2237 384 family2str(table->handle.family),
2238 table->handle.table.name);
2239
2240
2/2
✓ Branch 0 (21→15) taken 384 times.
✓ Branch 1 (21→22) taken 384 times.
768 list_for_each_entry(obj, &table->obj_cache.list, cache.list) {
2241
1/2
✓ Branch 0 (15→16) taken 384 times.
✗ Branch 1 (15→18) not taken.
384 if (obj->type != type ||
2242
2/2
✓ Branch 0 (16→17) taken 373 times.
✓ Branch 1 (16→19) taken 11 times.
384 (cmd->handle.obj.name != NULL &&
2243
1/2
✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→19) taken 373 times.
373 strcmp(cmd->handle.obj.name, obj->handle.obj.name)))
2244 ✗ continue;
2245
2246 384 obj_print_declaration(obj, &opts, &ctx->nft->output);
2247 }
2248
2249 384 nft_print(&ctx->nft->output, "}\n");
2250 }
2251 385 return 0;
2252 }
2253
2254 167 struct flowtable *flowtable_alloc(const struct location *loc)
2255 {
2256 struct flowtable *flowtable;
2257
2258
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 167 times.
167 assert(loc);
2259
2260 167 flowtable = xzalloc(sizeof(*flowtable));
2261 167 flowtable->location = *loc;
2262
2263 167 flowtable->refcnt = 1;
2264 167 return flowtable;
2265 }
2266
2267 137 struct flowtable *flowtable_get(struct flowtable *flowtable)
2268 {
2269
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 137 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 137 times.
137 assert_refcount_safe(flowtable->refcnt);
2270 137 flowtable->refcnt++;
2271 137 return flowtable;
2272 }
2273
2274 304 void flowtable_free(struct flowtable *flowtable)
2275 {
2276 int i;
2277
2278
2/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 304 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 304 times.
304 assert_refcount_safe(flowtable->refcnt);
2279
2/2
✓ Branch 0 (8→9) taken 137 times.
✓ Branch 1 (8→10) taken 167 times.
304 if (--flowtable->refcnt > 0)
2280 137 return;
2281 167 handle_free(&flowtable->handle);
2282 167 expr_free(flowtable->priority.expr);
2283 167 expr_free(flowtable->dev_expr);
2284
2285
2/2
✓ Branch 0 (13→14) taken 47 times.
✓ Branch 1 (13→18) taken 120 times.
167 if (flowtable->dev_array != NULL) {
2286
2/2
✓ Branch 0 (16→15) taken 58 times.
✓ Branch 1 (16→17) taken 47 times.
105 for (i = 0; i < flowtable->dev_array_len; i++)
2287 58 free_const(flowtable->dev_array[i]);
2288 47 free(flowtable->dev_array);
2289 }
2290 167 free(flowtable);
2291 }
2292
2293 24 static void flowtable_print_declaration(const struct flowtable *flowtable,
2294 struct print_fmt_options *opts,
2295 struct output_ctx *octx)
2296 {
2297 char priobuf[STD_PRIO_BUFSIZE];
2298 int i;
2299
2300 24 nft_print(octx, "%sflowtable", opts->tab);
2301
2302
2/2
✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 22 times.
24 if (opts->family != NULL)
2303 2 nft_print(octx, " %s", opts->family);
2304
2305
2/2
✓ Branch 0 (5→6) taken 2 times.
✓ Branch 1 (5→7) taken 22 times.
24 if (opts->table != NULL)
2306 2 nft_print(octx, " %s", opts->table);
2307
2308 24 nft_print(octx, " %s {", flowtable->handle.flowtable.name);
2309
2310
2/2
✓ Branch 0 (9→10) taken 2 times.
✓ Branch 1 (9→11) taken 22 times.
24 if (nft_output_handle(octx))
2311 2 nft_print(octx, " # handle %" PRIu64, flowtable->handle.handle.id);
2312 24 nft_print(octx, "%s", opts->nl);
2313
2314
1/2
✓ Branch 0 (12→13) taken 24 times.
✗ Branch 1 (12→16) not taken.
24 if (flowtable->priority.expr) {
2315 24 nft_print(octx, "%s%shook %s priority %s%s",
2316 opts->tab, opts->tab,
2317 24 hooknum2str(NFPROTO_NETDEV, flowtable->hook.num),
2318 prio2str(octx, priobuf, sizeof(priobuf), NFPROTO_NETDEV,
2319 24 flowtable->hook.num, flowtable->priority.expr),
2320 opts->stmt_separator);
2321 }
2322
2323
2/2
✓ Branch 0 (16→17) taken 22 times.
✓ Branch 1 (16→25) taken 2 times.
24 if (flowtable->dev_array_len > 0) {
2324 22 nft_print(octx, "%s%sdevices = { ", opts->tab, opts->tab);
2325
2/2
✓ Branch 0 (23→19) taken 24 times.
✓ Branch 1 (23→24) taken 22 times.
46 for (i = 0; i < flowtable->dev_array_len; i++) {
2326 24 nft_print(octx, "\"%s\"", flowtable->dev_array[i]);
2327
2/2
✓ Branch 0 (20→21) taken 2 times.
✓ Branch 1 (20→22) taken 22 times.
24 if (i + 1 != flowtable->dev_array_len)
2328 2 nft_print(octx, ", ");
2329 }
2330 22 nft_print(octx, " }%s", opts->stmt_separator);
2331 }
2332
2333
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 24 times.
24 if (flowtable->flags & NFT_FLOWTABLE_HW_OFFLOAD)
2334 ✗ nft_print(octx, "%s%sflags offload%s", opts->tab, opts->tab,
2335 opts->stmt_separator);
2336
2337
2/2
✓ Branch 0 (27→28) taken 6 times.
✓ Branch 1 (27→29) taken 18 times.
24 if (flowtable->flags & NFT_FLOWTABLE_COUNTER)
2338 6 nft_print(octx, "%s%scounter%s", opts->tab, opts->tab,
2339 opts->stmt_separator);
2340 24 }
2341
2342 20 static void do_flowtable_print(const struct flowtable *flowtable,
2343 struct print_fmt_options *opts,
2344 struct output_ctx *octx)
2345 {
2346 20 flowtable_print_declaration(flowtable, opts, octx);
2347 20 nft_print(octx, "%s}%s", opts->tab, opts->nl);
2348 20 }
2349
2350 20 void flowtable_print(const struct flowtable *s, struct output_ctx *octx)
2351 {
2352 20 struct print_fmt_options opts = {
2353 .tab = "\t",
2354 .nl = "\n",
2355 .stmt_separator = "\n",
2356 };
2357
2358 20 do_flowtable_print(s, &opts, octx);
2359 20 }
2360
2361 2 void flowtable_print_plain(const struct flowtable *ft, struct output_ctx *octx)
2362 {
2363 4 struct print_fmt_options opts = {
2364 .tab = "",
2365 .nl = " ",
2366 2 .table = ft->handle.table.name,
2367 2 .family = family2str(ft->handle.family),
2368 .stmt_separator = "; ",
2369 };
2370
2371 2 flowtable_print_declaration(ft, &opts, octx);
2372 2 nft_print(octx, "}");
2373 2 }
2374
2375
2376 2 struct flowtable *flowtable_lookup_fuzzy(const char *ft_name,
2377 const struct nft_cache *cache,
2378 const struct table **t)
2379 {
2380 struct string_misspell_state st;
2381 struct table *table;
2382 struct flowtable *ft;
2383
2384
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2 times.
2 if (!ft_name)
2385 ✗ return NULL;
2386
2387 2 string_misspell_init(&st);
2388
2389
1/2
✗ Branch 0 (13→6) not taken.
✓ Branch 1 (13→14) taken 2 times.
2 list_for_each_entry(table, &cache->table_cache.list, cache.list) {
2390 ✗ list_for_each_entry(ft, &table->ft_cache.list, cache.list) {
2391 ✗ if (string_misspell_update(ft->handle.flowtable.name,
2392 ft_name, ft, &st))
2393 ✗ *t = table;
2394 }
2395 }
2396 2 return st.obj;
2397 }
2398
2399 2 static int do_list_flowtable(struct netlink_ctx *ctx, struct cmd *cmd,
2400 struct table *table)
2401 {
2402 struct flowtable *ft;
2403
2404 2 ft = ft_cache_find(table, cmd->handle.flowtable.name);
2405
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2 times.
2 if (!ft)
2406 ✗ return -1;
2407
2408 2 nft_print(&ctx->nft->output, "table %s %s {\n",
2409 2 family2str(table->handle.family),
2410 table->handle.table.name);
2411
2412 2 flowtable_print(ft, &ctx->nft->output);
2413 2 nft_print(&ctx->nft->output, "}\n");
2414
2415 2 return 0;
2416 }
2417
2418 1 static int do_list_flowtables(struct netlink_ctx *ctx, struct cmd *cmd)
2419 {
2420 1 struct print_fmt_options opts = {
2421 .tab = "\t",
2422 .nl = "\n",
2423 .stmt_separator = "\n",
2424 };
2425 struct flowtable *flowtable;
2426 struct table *table;
2427
2428
2/2
✓ Branch 0 (15→3) taken 2 times.
✓ Branch 1 (15→16) taken 1 times.
3 list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
2429
1/2
✓ Branch 0 (3→4) taken 2 times.
✗ Branch 1 (3→6) not taken.
2 if (cmd->handle.family != NFPROTO_UNSPEC &&
2430
2/2
✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→6) taken 1 times.
2 cmd->handle.family != table->handle.family)
2431 1 continue;
2432
2433 1 nft_print(&ctx->nft->output, "table %s %s {\n",
2434 1 family2str(table->handle.family),
2435 table->handle.table.name);
2436
2437
2/2
✓ Branch 0 (12→9) taken 2 times.
✓ Branch 1 (12→13) taken 1 times.
3 list_for_each_entry(flowtable, &table->ft_cache.list, cache.list) {
2438 2 flowtable_print_declaration(flowtable, &opts, &ctx->nft->output);
2439 2 nft_print(&ctx->nft->output, "%s}%s", opts.tab, opts.nl);
2440 }
2441
2442 1 nft_print(&ctx->nft->output, "}\n");
2443 }
2444 1 return 0;
2445 }
2446
2447 644 static int do_list_ruleset(struct netlink_ctx *ctx, struct cmd *cmd)
2448 {
2449 644 unsigned int family = cmd->handle.family;
2450 struct table *table;
2451
2452
2/2
✓ Branch 0 (10→3) taken 729 times.
✓ Branch 1 (10→11) taken 644 times.
1373 list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
2453
2/2
✓ Branch 0 (3→4) taken 29 times.
✓ Branch 1 (3→6) taken 700 times.
729 if (family != NFPROTO_UNSPEC &&
2454
2/2
✓ Branch 0 (4→5) taken 20 times.
✓ Branch 1 (4→6) taken 9 times.
29 table->handle.family != family)
2455 20 continue;
2456
2457
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 709 times.
709 if (do_list_table(ctx, table) < 0)
2458 ✗ return -1;
2459 }
2460
2461 644 return 0;
2462 }
2463
2464 2 static int do_list_tables(struct netlink_ctx *ctx, struct cmd *cmd)
2465 {
2466 struct table *table;
2467
2468
2/2
✓ Branch 0 (9→3) taken 3 times.
✓ Branch 1 (9→10) taken 2 times.
5 list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
2469
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 3 times.
3 if (cmd->handle.family != NFPROTO_UNSPEC &&
2470 ✗ cmd->handle.family != table->handle.family)
2471 ✗ continue;
2472
2473 3 nft_print(&ctx->nft->output, "table %s %s\n",
2474 3 family2str(table->handle.family),
2475 table->handle.table.name);
2476 }
2477
2478 2 return 0;
2479 }
2480
2481 3436 static void table_print_declaration(struct table *table,
2482 struct output_ctx *octx)
2483 {
2484 3436 const char *family = family2str(table->handle.family);
2485
2486
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 3436 times.
3436 if (table->has_xt_stmts)
2487 ✗ fprintf(octx->error_fp,
2488 "# Warning: table %s %s is managed by iptables-nft, do not touch!\n",
2489 family, table->handle.table.name);
2490
2491 3436 nft_print(octx, "table %s %s {\n", family, table->handle.table.name);
2492 3436 }
2493
2494 708 static int do_list_chain(struct netlink_ctx *ctx, struct cmd *cmd,
2495 struct table *table)
2496 {
2497 struct chain *chain;
2498
2499 708 table_print_declaration(table, &ctx->nft->output);
2500
2501 708 chain = chain_cache_find(table, cmd->handle.chain.name);
2502
1/2
✓ Branch 0 (4→5) taken 708 times.
✗ Branch 1 (4→6) not taken.
708 if (chain)
2503 708 chain_print(chain, &ctx->nft->output);
2504
2505 708 nft_print(&ctx->nft->output, "}\n");
2506
2507 708 return 0;
2508 }
2509
2510 1 static int do_list_chains(struct netlink_ctx *ctx, struct cmd *cmd)
2511 {
2512 struct table *table;
2513 struct chain *chain;
2514
2515
2/2
✓ Branch 0 (14→3) taken 2 times.
✓ Branch 1 (14→15) taken 1 times.
3 list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) {
2516
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 2 times.
2 if (cmd->handle.family != NFPROTO_UNSPEC &&
2517 ✗ cmd->handle.family != table->handle.family)
2518 ✗ continue;
2519
2520 2 table_print_declaration(table, &ctx->nft->output);
2521
2522
1/2
✗ Branch 0 (11→8) not taken.
✓ Branch 1 (11→12) taken 2 times.
2 list_for_each_entry(chain, &table->chain_cache.list, cache.list) {
2523 ✗ chain_print_declaration(chain, &ctx->nft->output);
2524 ✗ nft_print(&ctx->nft->output, "\t}\n");
2525 }
2526 2 nft_print(&ctx->nft->output, "}\n");
2527 }
2528
2529 1 return 0;
2530 }
2531
2532 − static void __do_list_set(struct netlink_ctx *ctx, struct cmd *cmd,
2533 struct set *set)
2534 {
2535 − struct table *table = table_alloc();
2536
2537 − table->handle.table.name = xstrdup(cmd->handle.table.name);
2538 − table->handle.family = cmd->handle.family;
2539 − table_print_declaration(table, &ctx->nft->output);
2540 − table_free(table);
2541
2542 − set_print(set, &ctx->nft->output);
2543 − nft_print(&ctx->nft->output, "}\n");
2544 − }
2545
2546 864 static int do_list_set(struct netlink_ctx *ctx, struct cmd *cmd,
2547 struct table *table)
2548 {
2549 864 struct set *set = cmd->set;
2550
2551
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→6) taken 864 times.
864 if (!set) {
2552 ✗ set = set_cache_find(table, cmd->handle.set.name);
2553 ✗ if (set == NULL)
2554 ✗ return -1;
2555 }
2556
2557 864 __do_list_set(ctx, cmd, set);
2558
2559 864 return 0;
2560 }
2561
2562 15 static int do_list_hooks(struct netlink_ctx *ctx, struct cmd *cmd)
2563 {
2564 15 const char *devname = cmd->handle.obj.name;
2565
2566 15 return mnl_nft_dump_nf_hooks(ctx, cmd->handle.family, devname);
2567 }
2568
2569 15290 static int do_command_list(struct netlink_ctx *ctx, struct cmd *cmd)
2570 {
2571 15290 struct table *table = NULL;
2572
2573
2/2
✓ Branch 0 (3→4) taken 6352 times.
✓ Branch 1 (3→6) taken 8938 times.
15290 if (nft_output_json(&ctx->nft->output))
2574 6352 return do_command_list_json(ctx, cmd);
2575
2576
2/2
✓ Branch 0 (6→7) taken 8258 times.
✓ Branch 1 (6→10) taken 680 times.
8938 if (cmd->handle.table.name != NULL) {
2577 8258 table = table_cache_find(&ctx->nft->cache.table_cache,
2578 cmd->handle.table.name,
2579 cmd->handle.family);
2580
2/2
✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→10) taken 8257 times.
8258 if (!table) {
2581 1 errno = ENOENT;
2582 1 return -1;
2583 }
2584 }
2585
2586
18/25
✓ Branch 0 (10→11) taken 6311 times.
✓ Branch 1 (10→15) taken 708 times.
✓ Branch 2 (10→16) taken 1 times.
✓ Branch 3 (10→17) taken 4 times.
✓ Branch 4 (10→18) taken 642 times.
✓ Branch 5 (10→19) taken 644 times.
✓ Branch 6 (10→20) taken 2 times.
✗ Branch 7 (10→21) not taken.
✗ Branch 8 (10→22) not taken.
✓ Branch 9 (10→23) taken 222 times.
✓ Branch 10 (10→24) taken 361 times.
✓ Branch 11 (10→25) taken 12 times.
✓ Branch 12 (10→26) taken 1 times.
✓ Branch 13 (10→27) taken 3 times.
✓ Branch 14 (10→28) taken 2 times.
✓ Branch 15 (10→29) taken 4 times.
✗ Branch 16 (10→30) not taken.
✓ Branch 17 (10→31) taken 2 times.
✗ Branch 18 (10→32) not taken.
✓ Branch 19 (10→33) taken 2 times.
✓ Branch 20 (10→34) taken 1 times.
✓ Branch 21 (10→35) taken 15 times.
✗ Branch 22 (10→36) not taken.
✗ Branch 23 (10→37) not taken.
✗ Branch 24 (10→38) not taken.
8937 switch (cmd->obj) {
2587 6311 case CMD_OBJ_TABLE:
2588
2/2
✓ Branch 0 (11→12) taken 2 times.
✓ Branch 1 (11→14) taken 6309 times.
6311 if (!cmd->handle.table.name)
2589 2 return do_list_tables(ctx, cmd);
2590 6309 return do_list_table(ctx, table);
2591 708 case CMD_OBJ_CHAIN:
2592 708 return do_list_chain(ctx, cmd, table);
2593 1 case CMD_OBJ_CHAINS:
2594 1 return do_list_chains(ctx, cmd);
2595 4 case CMD_OBJ_SETS:
2596 4 return do_list_sets(ctx, cmd);
2597 642 case CMD_OBJ_SET:
2598 642 return do_list_set(ctx, cmd, table);
2599 644 case CMD_OBJ_RULESET:
2600 case CMD_OBJ_RULES:
2601 case CMD_OBJ_RULE:
2602 644 return do_list_ruleset(ctx, cmd);
2603 2 case CMD_OBJ_METERS:
2604 2 return do_list_sets(ctx, cmd);
2605 ✗ case CMD_OBJ_METER:
2606 ✗ return do_list_set(ctx, cmd, table);
2607 ✗ case CMD_OBJ_MAPS:
2608 ✗ return do_list_sets(ctx, cmd);
2609 222 case CMD_OBJ_MAP:
2610 222 return do_list_set(ctx, cmd, table);
2611 361 case CMD_OBJ_COUNTER:
2612 case CMD_OBJ_COUNTERS:
2613 361 return do_list_obj(ctx, cmd, NFT_OBJECT_COUNTER);
2614 12 case CMD_OBJ_QUOTA:
2615 case CMD_OBJ_QUOTAS:
2616 12 return do_list_obj(ctx, cmd, NFT_OBJECT_QUOTA);
2617 1 case CMD_OBJ_CT_HELPER:
2618 case CMD_OBJ_CT_HELPERS:
2619 1 return do_list_obj(ctx, cmd, NFT_OBJECT_CT_HELPER);
2620 3 case CMD_OBJ_CT_TIMEOUT:
2621 case CMD_OBJ_CT_TIMEOUTS:
2622 3 return do_list_obj(ctx, cmd, NFT_OBJECT_CT_TIMEOUT);
2623 2 case CMD_OBJ_CT_EXPECT:
2624 case CMD_OBJ_CT_EXPECTATIONS:
2625 2 return do_list_obj(ctx, cmd, NFT_OBJECT_CT_EXPECT);
2626 4 case CMD_OBJ_LIMIT:
2627 case CMD_OBJ_LIMITS:
2628 4 return do_list_obj(ctx, cmd, NFT_OBJECT_LIMIT);
2629 ✗ case CMD_OBJ_SECMARK:
2630 case CMD_OBJ_SECMARKS:
2631 ✗ return do_list_obj(ctx, cmd, NFT_OBJECT_SECMARK);
2632 2 case CMD_OBJ_SYNPROXY:
2633 case CMD_OBJ_SYNPROXYS:
2634 2 return do_list_obj(ctx, cmd, NFT_OBJECT_SYNPROXY);
2635 ✗ case CMD_OBJ_TUNNEL:
2636 case CMD_OBJ_TUNNELS:
2637 ✗ return do_list_obj(ctx, cmd, NFT_OBJECT_TUNNEL);
2638 2 case CMD_OBJ_FLOWTABLE:
2639 2 return do_list_flowtable(ctx, cmd, table);
2640 1 case CMD_OBJ_FLOWTABLES:
2641 1 return do_list_flowtables(ctx, cmd);
2642 15 case CMD_OBJ_HOOKS:
2643 15 return do_list_hooks(ctx, cmd);
2644 ✗ case CMD_OBJ_MONITOR:
2645 case CMD_OBJ_MARKUP:
2646 case CMD_OBJ_SETELEMS:
2647 case CMD_OBJ_EXPR:
2648 case CMD_OBJ_ELEMENTS:
2649 ✗ errno = EOPNOTSUPP;
2650 ✗ return -1;
2651 ✗ case CMD_OBJ_INVALID:
2652 ✗ break;
2653 }
2654
2655 ✗ BUG("invalid command object type %u", cmd->obj);
2656 return 0;
2657 }
2658
2659 1885 static int do_get_setelems(struct netlink_ctx *ctx, struct cmd *cmd, bool reset)
2660 {
2661 struct set *set, *new_set;
2662 struct expr *init;
2663 int err;
2664
2665 1885 set = cmd->elem.set;
2666
2667 /* Create a list of elements based of what we got from command line. */
2668
2/2
✓ Branch 0 (3→4) taken 722 times.
✓ Branch 1 (3→5) taken 1163 times.
1885 if (set_is_non_concat_range(set))
2669 722 init = get_set_intervals(set, cmd->expr);
2670 else
2671 1163 init = cmd->expr;
2672
2673 1885 new_set = set_clone(set);
2674
2675 /* Fetch from kernel the elements that have been requested .*/
2676 1885 err = netlink_get_setelem(ctx, &cmd->handle, &cmd->location,
2677 cmd->elem.set, new_set, init, reset);
2678
2/2
✓ Branch 0 (8→9) taken 1862 times.
✓ Branch 1 (8→10) taken 23 times.
1885 if (err >= 0)
2679 1862 __do_list_set(ctx, cmd, new_set);
2680
2681
2/2
✓ Branch 0 (11→12) taken 722 times.
✓ Branch 1 (11→13) taken 1163 times.
1885 if (set_is_non_concat_range(set))
2682 722 expr_free(init);
2683
2684 1885 set_free(new_set);
2685
2686 1885 return err;
2687 }
2688
2689 1064 static int do_command_get(struct netlink_ctx *ctx, struct cmd *cmd)
2690 {
2691
1/2
✓ Branch 0 (2→3) taken 1064 times.
✗ Branch 1 (2→4) not taken.
1064 switch (cmd->obj) {
2692 1064 case CMD_OBJ_ELEMENTS:
2693 1064 return do_get_setelems(ctx, cmd, false);
2694 ✗ default:
2695 ✗ BUG("invalid command object type %u", cmd->obj);
2696 }
2697
2698 return 0;
2699 }
2700
2701 916 static int do_command_reset(struct netlink_ctx *ctx, struct cmd *cmd)
2702 {
2703
2/2
✓ Branch 0 (2→3) taken 821 times.
✓ Branch 1 (2→4) taken 95 times.
916 switch (cmd->obj) {
2704 821 case CMD_OBJ_ELEMENTS:
2705 821 return do_get_setelems(ctx, cmd, true);
2706 95 default:
2707 95 break;
2708 }
2709
2710 95 return do_command_list(ctx, cmd);
2711 }
2712
2713 15682 static int do_command_flush(struct netlink_ctx *ctx, struct cmd *cmd)
2714 {
2715
3/4
✓ Branch 0 (2→3) taken 14103 times.
✓ Branch 1 (2→4) taken 201 times.
✓ Branch 2 (2→5) taken 1378 times.
✗ Branch 3 (2→6) not taken.
15682 switch (cmd->obj) {
2716 14103 case CMD_OBJ_TABLE:
2717 case CMD_OBJ_CHAIN:
2718 14103 return mnl_nft_rule_del(ctx, cmd);
2719 201 case CMD_OBJ_SET:
2720 case CMD_OBJ_MAP:
2721 case CMD_OBJ_METER:
2722 201 return mnl_nft_setelem_flush(ctx, cmd);
2723 1378 case CMD_OBJ_RULESET:
2724 1378 return mnl_nft_table_del(ctx, cmd);
2725 ✗ default:
2726 ✗ BUG("invalid command object type %u", cmd->obj);
2727 }
2728 return 0;
2729 }
2730
2731 4 static int do_command_rename(struct netlink_ctx *ctx, struct cmd *cmd)
2732 {
2733 4 struct table *table = table_cache_find(&ctx->nft->cache.table_cache,
2734 cmd->handle.table.name,
2735 cmd->handle.family);
2736 const struct chain *chain;
2737
2738
1/2
✓ Branch 0 (3→4) taken 4 times.
✗ Branch 1 (3→6) not taken.
4 switch (cmd->obj) {
2739 4 case CMD_OBJ_CHAIN:
2740 4 chain = chain_cache_find(table, cmd->handle.chain.name);
2741
2742 4 return mnl_nft_chain_rename(ctx, cmd, chain);
2743 ✗ default:
2744 ✗ BUG("invalid command object type %u", cmd->obj);
2745 }
2746 return 0;
2747 }
2748
2749 ✗ static int do_command_monitor(struct netlink_ctx *ctx, struct cmd *cmd)
2750 {
2751 ✗ struct netlink_mon_handler monhandler = {
2752 ✗ .monitor_flags = cmd->monitor->flags,
2753 ✗ .format = cmd->monitor->format,
2754 .ctx = ctx,
2755 ✗ .loc = &cmd->location,
2756 ✗ .cache = &ctx->nft->cache,
2757 ✗ .debug_mask = ctx->nft->debug_mask,
2758 };
2759
2760 ✗ if (nft_output_json(&ctx->nft->output))
2761 ✗ monhandler.format = NFTNL_OUTPUT_JSON;
2762
2763 ✗ return netlink_monitor(&monhandler, ctx->nft->nf_sock);
2764 }
2765
2766 ✗ static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd,
2767 struct output_ctx *octx)
2768 {
2769 ✗ expr_describe(cmd->expr, octx);
2770 ✗ return 0;
2771 }
2772
2773 58 struct cmd *cmd_alloc_obj_ct(enum cmd_ops op, int type, const struct handle *h,
2774 const struct location *loc, struct obj *obj)
2775 {
2776 enum cmd_obj cmd_obj;
2777
2778
2/2
✓ Branch 0 (2→3) taken 34 times.
✓ Branch 1 (2→4) taken 24 times.
58 if (obj)
2779 34 obj->type = type;
2780
2781
3/4
✓ Branch 0 (4→5) taken 18 times.
✓ Branch 1 (4→6) taken 24 times.
✓ Branch 2 (4→7) taken 16 times.
✗ Branch 3 (4→8) not taken.
58 switch (type) {
2782 18 case NFT_OBJECT_CT_HELPER:
2783 18 cmd_obj = CMD_OBJ_CT_HELPER;
2784 18 break;
2785 24 case NFT_OBJECT_CT_TIMEOUT:
2786 24 cmd_obj = CMD_OBJ_CT_TIMEOUT;
2787 24 break;
2788 16 case NFT_OBJECT_CT_EXPECT:
2789 16 cmd_obj = CMD_OBJ_CT_EXPECT;
2790 16 break;
2791 ✗ default:
2792 ✗ BUG("missing type mapping");
2793 }
2794
2795 58 return cmd_alloc(op, cmd_obj, h, loc, obj);
2796 }
2797
2798 186704 int do_command(struct netlink_ctx *ctx, struct cmd *cmd)
2799 {
2800
10/14
✓ Branch 0 (2→3) taken 150599 times.
✓ Branch 1 (2→4) taken 226 times.
✓ Branch 2 (2→5) taken 141 times.
✓ Branch 3 (2→6) taken 11 times.
✓ Branch 4 (2→7) taken 2866 times.
✓ Branch 5 (2→8) taken 1064 times.
✓ Branch 6 (2→9) taken 15195 times.
✓ Branch 7 (2→10) taken 916 times.
✓ Branch 8 (2→11) taken 15682 times.
✓ Branch 9 (2→12) taken 4 times.
✗ Branch 10 (2→13) not taken.
✗ Branch 11 (2→14) not taken.
✗ Branch 12 (2→15) not taken.
✗ Branch 13 (2→16) not taken.
186704 switch (cmd->op) {
2801 150599 case CMD_ADD:
2802 150599 return do_command_add(ctx, cmd, false);
2803 226 case CMD_CREATE:
2804 226 return do_command_add(ctx, cmd, true);
2805 141 case CMD_INSERT:
2806 141 return do_command_insert(ctx, cmd);
2807 11 case CMD_REPLACE:
2808 11 return do_command_replace(ctx, cmd);
2809 2866 case CMD_DELETE:
2810 case CMD_DESTROY:
2811 2866 return do_command_delete(ctx, cmd);
2812 1064 case CMD_GET:
2813 1064 return do_command_get(ctx, cmd);
2814 15195 case CMD_LIST:
2815 15195 return do_command_list(ctx, cmd);
2816 916 case CMD_RESET:
2817 916 return do_command_reset(ctx, cmd);
2818 15682 case CMD_FLUSH:
2819 15682 return do_command_flush(ctx, cmd);
2820 4 case CMD_RENAME:
2821 4 return do_command_rename(ctx, cmd);
2822 ✗ case CMD_IMPORT:
2823 case CMD_EXPORT:
2824 ✗ errno = EOPNOTSUPP;
2825 ✗ return -1;
2826 ✗ case CMD_MONITOR:
2827 ✗ return do_command_monitor(ctx, cmd);
2828 ✗ case CMD_DESCRIBE:
2829 ✗ return do_command_describe(ctx, cmd, &ctx->nft->output);
2830 ✗ default:
2831 ✗ BUG("invalid command object type %u", cmd->obj);
2832 }
2833 }
2834
2835 1030 static int payload_match_stmt_cmp(const void *p1, const void *p2)
2836 {
2837 1030 const struct stmt *s1 = *(struct stmt * const *)p1;
2838 1030 const struct stmt *s2 = *(struct stmt * const *)p2;
2839 1030 const struct expr *e1 = s1->expr, *e2 = s2->expr;
2840 int d;
2841
2842 1030 d = e1->left->payload.base - e2->left->payload.base;
2843
2/2
✓ Branch 0 (2→3) taken 573 times.
✓ Branch 1 (2→4) taken 457 times.
1030 if (d != 0)
2844 573 return d;
2845 457 return e1->left->payload.offset - e2->left->payload.offset;
2846 }
2847
2848 144 static bool relational_ops_match(const struct expr *e1, const struct expr *e2)
2849 {
2850 enum ops op1, op2;
2851
2852
2/2
✓ Branch 0 (2→3) taken 56 times.
✓ Branch 1 (2→4) taken 88 times.
144 op1 = e1->op == OP_IMPLICIT ? OP_EQ : e1->op;
2853
2/2
✓ Branch 0 (5→6) taken 76 times.
✓ Branch 1 (5→7) taken 68 times.
144 op2 = e2->op == OP_IMPLICIT ? OP_EQ : e2->op;
2854
2855 144 return op1 == op2;
2856 }
2857
2858 722 static void payload_do_merge(struct stmt *sa[], unsigned int n)
2859 {
2860 struct expr *last, *this, *expr1, *expr2;
2861 struct stmt *stmt;
2862 unsigned int i, j;
2863
2864 722 qsort(sa, n, sizeof(sa[0]), payload_match_stmt_cmp);
2865
2866 722 last = sa[0]->expr;
2867
2/2
✓ Branch 0 (20→4) taken 928 times.
✓ Branch 1 (20→21) taken 722 times.
1650 for (j = 0, i = 1; i < n; i++) {
2868 928 stmt = sa[i];
2869 928 this = stmt->expr;
2870
2871
2/2
✓ Branch 0 (5→6) taken 144 times.
✓ Branch 1 (5→8) taken 784 times.
928 if (!payload_can_merge(last->left, this->left) ||
2872
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 144 times.
144 !relational_ops_match(last, this)) {
2873 784 last = this;
2874 784 j = i;
2875 784 continue;
2876 }
2877
2878 144 expr1 = payload_expr_join(last->left, this->left);
2879 144 expr2 = constant_expr_join(last->right, this->right);
2880
2881 /* We can merge last into this, but we can't replace
2882 * the statement associated with this if it does contain
2883 * a higher level protocol.
2884 *
2885 * ether type ip ip saddr X ether saddr Y
2886 * ... can be changed to
2887 * ether type ip ether saddr Y ip saddr X
2888 * ... but not
2889 * ip saddr X ether type ip ether saddr Y
2890 *
2891 * The latter form means we perform ip saddr test before
2892 * ensuring ip dependency, plus it makes decoding harder
2893 * since we don't know the type of the network header
2894 * right away.
2895 *
2896 * So, if we're about to replace a statement
2897 * containing a protocol identifier, just swap this and last
2898 * and replace the other one (i.e., replace 'load ether type ip'
2899 * with the combined 'load both ether type and saddr') and not
2900 * the other way around.
2901 */
2902
2/2
✓ Branch 0 (11→12) taken 33 times.
✓ Branch 1 (11→15) taken 111 times.
144 if (this->left->flags & EXPR_F_PROTOCOL) {
2903 33 struct expr *tmp = last;
2904
2905 33 last = this;
2906 33 this = tmp;
2907
2908 33 expr1->flags |= EXPR_F_PROTOCOL;
2909 33 stmt = sa[j];
2910
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 33 times.
33 assert(stmt->expr == this);
2911 33 j = i;
2912 }
2913
2914 144 expr_free(last->left);
2915 144 last->left = expr1;
2916
2917 144 expr_free(last->right);
2918 144 last->right = expr2;
2919
2920 144 list_del(&stmt->list);
2921 144 stmt_free(stmt);
2922 }
2923 722 }
2924
2925 /**
2926 * stmt_reduce - reduce statements in rule
2927 *
2928 * @rule: nftables rule
2929 *
2930 * This function aims to:
2931 *
2932 * - remove redundant statement, e.g. remove 'meta protocol ip' if family is ip
2933 * - merge consecutive payload match statements
2934 *
2935 * Locate sequences of payload match statements referring to adjacent
2936 * header locations and merge those using only equality relations.
2937 *
2938 * As a side-effect, payload match statements are ordered in ascending
2939 * order according to the location of the payload.
2940 */
2941 108417 static void stmt_reduce(const struct rule *rule)
2942 108417 {
2943 108417 struct stmt *stmt, *dstmt = NULL, *next;
2944 108417 struct stmt *sa[rule->num_stmts];
2945 108417 unsigned int idx = 0;
2946
2947
2/2
✓ Branch 0 (33→3) taken 131379 times.
✓ Branch 1 (33→34) taken 108417 times.
239796 list_for_each_entry_safe(stmt, next, &rule->stmts, list) {
2948 /* delete this redundant statement */
2949
2/2
✓ Branch 0 (3→4) taken 5 times.
✓ Branch 1 (3→7) taken 131374 times.
131379 if (dstmt) {
2950 5 list_del(&dstmt->list);
2951 5 stmt_free(dstmt);
2952 5 dstmt = NULL;
2953 }
2954
2955 /* Must not merge across other statements */
2956
2/2
✓ Branch 0 (7→8) taken 22088 times.
✓ Branch 1 (7→11) taken 109291 times.
131379 if (stmt->type != STMT_EXPRESSION) {
2957
2/2
✓ Branch 0 (8→9) taken 259 times.
✓ Branch 1 (8→10) taken 21829 times.
22088 if (idx >= 2)
2958 259 payload_do_merge(sa, idx);
2959 22088 idx = 0;
2960 22088 continue;
2961 }
2962
2963
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 109291 times.
109291 if (stmt->expr->etype != EXPR_RELATIONAL)
2964 ✗ continue;
2965
2/2
✓ Branch 0 (13→14) taken 6561 times.
✓ Branch 1 (13→15) taken 102730 times.
109291 if (stmt->expr->right->etype != EXPR_VALUE)
2966 6561 continue;
2967
2968
2/2
✓ Branch 0 (15→16) taken 6260 times.
✓ Branch 1 (15→20) taken 96470 times.
102730 if (stmt->expr->left->etype == EXPR_PAYLOAD) {
2969
2/2
✓ Branch 0 (16→17) taken 5718 times.
✓ Branch 1 (16→18) taken 542 times.
6260 switch (stmt->expr->op) {
2970 5718 case OP_EQ:
2971 case OP_IMPLICIT:
2972 5718 break;
2973 542 default:
2974 542 continue;
2975 }
2976
2977 5718 sa[idx++] = stmt;
2978
2/2
✓ Branch 0 (20→21) taken 93685 times.
✓ Branch 1 (20→32) taken 2785 times.
96470 } else if (stmt->expr->left->etype == EXPR_META) {
2979
2/2
✓ Branch 0 (21→22) taken 93199 times.
✓ Branch 1 (21→31) taken 486 times.
93685 switch (stmt->expr->op) {
2980 93199 case OP_EQ:
2981 case OP_IMPLICIT:
2982
2/2
✓ Branch 0 (22→23) taken 1717 times.
✓ Branch 1 (22→30) taken 91482 times.
93199 if (stmt->expr->left->meta.key == NFT_META_PROTOCOL &&
2983
2/2
✓ Branch 0 (23→24) taken 1494 times.
✓ Branch 1 (23→30) taken 223 times.
1717 !stmt->expr->left->meta.inner_desc) {
2984 uint16_t protocol;
2985
2986 1494 protocol = mpz_get_uint16(stmt->expr->right->value);
2987
4/4
✓ Branch 0 (25→26) taken 8 times.
✓ Branch 1 (25→27) taken 1486 times.
✓ Branch 2 (26→27) taken 4 times.
✓ Branch 3 (26→29) taken 4 times.
1494 if ((rule->handle.family == NFPROTO_IPV4 &&
2988 1490 protocol == ETH_P_IP) ||
2989
4/4
✓ Branch 0 (27→28) taken 11 times.
✓ Branch 1 (27→30) taken 1479 times.
✓ Branch 2 (28→29) taken 3 times.
✓ Branch 3 (28→30) taken 8 times.
1490 (rule->handle.family == NFPROTO_IPV6 &&
2990 protocol == ETH_P_IPV6))
2991 7 dstmt = stmt;
2992 }
2993 93199 break;
2994 486 default:
2995 486 break;
2996 }
2997 }
2998 }
2999
3000
2/2
✓ Branch 0 (34→35) taken 463 times.
✓ Branch 1 (34→36) taken 107954 times.
108417 if (idx > 1)
3001 463 payload_do_merge(sa, idx);
3002 108417 }
3003
3004 108417 struct error_record *rule_postprocess(struct rule *rule)
3005 {
3006 108417 stmt_reduce(rule);
3007 108417 return NULL;
3008 }
3009