GCC Code Coverage Report


Directory: ./
File: src/netlink.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 1031 1165 88.5%
Functions: 62 65 95.4%
Branches: 428 586 73.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2008-2012 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2013 Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
10 */
11
12 #include <nft.h>
13 #include <nftversion.h>
14
15 #include <errno.h>
16 #include <libmnl/libmnl.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <inttypes.h>
20
21 #include <libnftnl/table.h>
22 #include <libnftnl/chain.h>
23 #include <libnftnl/expr.h>
24 #include <libnftnl/object.h>
25 #include <libnftnl/set.h>
26 #include <libnftnl/flowtable.h>
27 #include <libnftnl/udata.h>
28 #include <libnftnl/ruleset.h>
29 #include <libnftnl/common.h>
30 #include <libnftnl/udata.h>
31 #include <linux/netfilter/nfnetlink.h>
32 #include <linux/netfilter/nf_tables.h>
33 #include <linux/netfilter.h>
34
35 #include <nftables.h>
36 #include <parser.h>
37 #include <netlink.h>
38 #include <mnl.h>
39 #include <expression.h>
40 #include <statement.h>
41 #include <gmputil.h>
42 #include <utils.h>
43 #include <erec.h>
44
45 #define nft_mon_print(monh, ...) nft_print(&monh->ctx->nft->output, __VA_ARGS__)
46
47 const struct input_descriptor indesc_netlink = {
48 .name = "netlink",
49 .type = INDESC_NETLINK,
50 };
51
52 const struct location netlink_location = {
53 .indesc = &indesc_netlink,
54 };
55
56 − void __noreturn __netlink_abi_error(const char *file, int line,
57 const char *reason)
58 {
59 − fprintf(stderr, "E: Contact urgently your Linux kernel vendor. "
60 "Netlink ABI is broken: %s:%d %s\n", file, line, reason);
61 − abort();
62 }
63
64 968 int netlink_io_error(struct netlink_ctx *ctx, const struct location *loc,
65 const char *fmt, ...)
66 {
67 struct error_record *erec;
68 va_list ap;
69
70
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 968 times.
968 if (loc == NULL)
71 ✗ loc = &netlink_location;
72
73 968 va_start(ap, fmt);
74 968 erec = erec_vcreate(EREC_ERROR, loc, fmt, ap);
75 968 va_end(ap);
76 968 erec_queue(erec, ctx->msgs);
77 968 return -1;
78 }
79
80 − void __noreturn __netlink_init_error(const char *filename, int line,
81 const char *reason)
82 {
83 − fprintf(stderr, "%s:%d: Unable to initialize Netlink socket: %s\n",
84 filename, line, reason);
85 − exit(NFT_EXIT_NONL);
86 }
87
88 252989 struct nftnl_expr *alloc_nft_expr(const char *name)
89 {
90 struct nftnl_expr *nle;
91
92 252989 nle = nftnl_expr_alloc(name);
93
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 252989 times.
252989 if (nle == NULL)
94 ✗ memory_allocation_error();
95
96 252989 return nle;
97 }
98 static void netlink_gen_key(const struct expr *expr,
99 struct nft_data_linearize *data);
100 static void __netlink_gen_data(const struct expr *expr,
101 struct nft_data_linearize *data, bool expand);
102
103 2050773 struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
104 const struct expr *expr)
105 {
106 const struct expr *elem, *data;
107 struct nftnl_set_elem *nlse;
108 struct nft_data_linearize nld;
109 2050773 struct nftnl_udata_buf *udbuf = NULL;
110 2050773 uint32_t flags = 0;
111 2050773 int num_exprs = 0;
112 struct stmt *stmt;
113 struct expr *key;
114
115 2050773 nlse = nftnl_set_elem_alloc();
116
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2050773 times.
2050773 if (nlse == NULL)
117 ✗ memory_allocation_error();
118
119 2050773 data = NULL;
120
2/2
✓ Branch 0 (5→6) taken 173210 times.
✓ Branch 1 (5→8) taken 1877563 times.
2050773 if (expr->etype == EXPR_MAPPING) {
121 173210 elem = expr->left;
122
2/2
✓ Branch 0 (6→7) taken 170363 times.
✓ Branch 1 (6→9) taken 2847 times.
173210 if (!(expr->flags & EXPR_F_INTERVAL_END))
123 170363 data = expr->right;
124 } else {
125 1877563 elem = expr;
126 }
127
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 2050773 times.
2050773 if (elem->etype != EXPR_SET_ELEM)
128 ✗ BUG("Unexpected expression type: got %d", elem->etype);
129
130 2050773 key = elem->key;
131
132
2/2
✓ Branch 0 (12→13) taken 81 times.
✓ Branch 1 (12→14) taken 2050692 times.
2050773 switch (key->etype) {
133 81 case EXPR_SET_ELEM_CATCHALL:
134 81 break;
135 2050692 default:
136
3/4
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 2050692 times.
✓ Branch 2 (16→17) taken 1371835 times.
✓ Branch 3 (16→25) taken 678857 times.
2050692 if (expr_set(set)->set_flags & NFT_SET_INTERVAL &&
137
4/6
✓ Branch 0 (17→18) taken 10649 times.
✓ Branch 1 (17→25) taken 1361186 times.
✗ Branch 2 (18→19) not taken.
✓ Branch 3 (18→20) taken 10649 times.
✓ Branch 4 (20→21) taken 10649 times.
✗ Branch 5 (20→25) not taken.
1371835 key->etype == EXPR_CONCAT && expr_concat(key)->field_count > 1) {
138 10649 key->flags |= EXPR_F_INTERVAL;
139 10649 netlink_gen_key(key, &nld);
140 10649 key->flags &= ~EXPR_F_INTERVAL;
141
142 10649 nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_KEY, nld.value, nld.len);
143
144 10649 key->flags |= EXPR_F_INTERVAL_END;
145 10649 netlink_gen_key(key, &nld);
146 10649 key->flags &= ~EXPR_F_INTERVAL_END;
147
148 10649 nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_KEY_END,
149 nld.value, nld.len);
150 } else {
151 2040043 netlink_gen_key(key, &nld);
152 2040043 nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_KEY, nld.value, nld.len);
153 }
154 2050692 break;
155 }
156
157
2/2
✓ Branch 0 (28→29) taken 136005 times.
✓ Branch 1 (28→32) taken 1914768 times.
2050773 if (elem->timeout) {
158 136005 uint64_t timeout = elem->timeout;
159
160
1/2
✗ Branch 0 (29→30) not taken.
✓ Branch 1 (29→31) taken 136005 times.
136005 if (elem->timeout == NFT_NEVER_TIMEOUT)
161 ✗ timeout = 0;
162
163 136005 nftnl_set_elem_set_u64(nlse, NFTNL_SET_ELEM_TIMEOUT, timeout);
164 }
165
2/2
✓ Branch 0 (32→33) taken 73186 times.
✓ Branch 1 (32→34) taken 1977587 times.
2050773 if (elem->expiration)
166 73186 nftnl_set_elem_set_u64(nlse, NFTNL_SET_ELEM_EXPIRATION,
167 73186 elem->expiration);
168
2/2
✓ Branch 0 (36→35) taken 126 times.
✓ Branch 1 (36→37) taken 2050773 times.
2050899 list_for_each_entry(stmt, &elem->stmt_list, list)
169 126 num_exprs++;
170
171
2/2
✓ Branch 0 (37→38) taken 102 times.
✓ Branch 1 (37→43) taken 2050671 times.
2050773 if (num_exprs == 1) {
172
2/2
✓ Branch 0 (42→39) taken 102 times.
✓ Branch 1 (42→49) taken 102 times.
204 list_for_each_entry(stmt, &elem->stmt_list, list) {
173 102 nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_EXPR,
174 102 netlink_gen_stmt_stateful(stmt), 0);
175 }
176
2/2
✓ Branch 0 (43→44) taken 12 times.
✓ Branch 1 (43→49) taken 2050659 times.
2050671 } else if (num_exprs > 1) {
177
2/2
✓ Branch 0 (48→45) taken 24 times.
✓ Branch 1 (48→49) taken 12 times.
36 list_for_each_entry(stmt, &elem->stmt_list, list) {
178 24 nftnl_set_elem_add_expr(nlse,
179 netlink_gen_stmt_stateful(stmt));
180 }
181 }
182
4/4
✓ Branch 0 (49→50) taken 2050043 times.
✓ Branch 1 (49→51) taken 730 times.
✓ Branch 2 (50→51) taken 109 times.
✓ Branch 3 (50→54) taken 2049934 times.
2050773 if (elem->comment || expr->flags & EXPR_F_INTERVAL_OPEN) {
183 839 udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
184
1/2
✗ Branch 0 (52→53) not taken.
✓ Branch 1 (52→54) taken 839 times.
839 if (!udbuf)
185 ✗ memory_allocation_error();
186 }
187
2/2
✓ Branch 0 (54→55) taken 730 times.
✓ Branch 1 (54→58) taken 2050043 times.
2050773 if (elem->comment) {
188
1/2
✗ Branch 0 (56→57) not taken.
✓ Branch 1 (56→58) taken 730 times.
730 if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_SET_ELEM_COMMENT,
189 730 elem->comment))
190 ✗ memory_allocation_error();
191 }
192
2/2
✓ Branch 0 (58→59) taken 109 times.
✓ Branch 1 (58→62) taken 2050664 times.
2050773 if (expr->flags & EXPR_F_INTERVAL_OPEN) {
193
1/2
✗ Branch 0 (60→61) not taken.
✓ Branch 1 (60→62) taken 109 times.
109 if (!nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_ELEM_FLAGS,
194 NFTNL_SET_ELEM_F_INTERVAL_OPEN))
195 ✗ memory_allocation_error();
196 }
197
2/2
✓ Branch 0 (62→63) taken 839 times.
✓ Branch 1 (62→67) taken 2049934 times.
2050773 if (udbuf) {
198 1678 nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_USERDATA,
199 839 nftnl_udata_buf_data(udbuf),
200 nftnl_udata_buf_len(udbuf));
201 839 nftnl_udata_buf_free(udbuf);
202 }
203
5/6
✗ Branch 0 (67→68) not taken.
✓ Branch 1 (67→69) taken 2050773 times.
✓ Branch 2 (70→71) taken 194838 times.
✓ Branch 3 (70→84) taken 1855935 times.
✓ Branch 4 (71→72) taken 169051 times.
✓ Branch 5 (71→84) taken 25787 times.
2050773 if (set_is_datamap(expr_set(set)->set_flags) && data != NULL) {
204 169051 __netlink_gen_data(data, &nld, !(data->flags & EXPR_F_SINGLETON));
205
3/4
✓ Branch 0 (73→74) taken 137290 times.
✓ Branch 1 (73→78) taken 78 times.
✓ Branch 2 (73→80) taken 31683 times.
✗ Branch 3 (73→81) not taken.
169051 switch (data->etype) {
206 137290 case EXPR_VERDICT:
207 137290 nftnl_set_elem_set_u32(nlse, NFTNL_SET_ELEM_VERDICT,
208 137290 data->verdict);
209
2/2
✓ Branch 0 (75→76) taken 135952 times.
✓ Branch 1 (75→77) taken 1338 times.
137290 if (data->chain != NULL)
210 135952 nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_CHAIN,
211 135952 nld.chain, strlen(nld.chain));
212 137290 break;
213 78 case EXPR_CONCAT:
214
1/2
✗ Branch 0 (78→79) not taken.
✓ Branch 1 (78→80) taken 78 times.
78 assert(nld.len > 0);
215 /* fallthrough */
216 case EXPR_VALUE:
217 case EXPR_RANGE:
218 case EXPR_RANGE_VALUE:
219 case EXPR_PREFIX:
220 31761 nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_DATA,
221 nld.value, nld.len);
222 31761 break;
223 ✗ default:
224 ✗ BUG("unexpected set element expression");
225 break;
226 }
227 }
228
5/6
✗ Branch 0 (84→85) not taken.
✓ Branch 1 (84→86) taken 2050773 times.
✓ Branch 2 (87→88) taken 369 times.
✓ Branch 3 (87→91) taken 2050404 times.
✓ Branch 4 (88→89) taken 262 times.
✓ Branch 5 (88→91) taken 107 times.
2050773 if (set_is_objmap(expr_set(set)->set_flags) && data != NULL) {
229 262 netlink_gen_data(data, &nld);
230 262 nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_OBJREF,
231 nld.value, nld.len);
232 }
233
234
2/2
✓ Branch 0 (91→92) taken 682914 times.
✓ Branch 1 (91→93) taken 1367859 times.
2050773 if (expr->flags & EXPR_F_INTERVAL_END)
235 682914 flags |= NFT_SET_ELEM_INTERVAL_END;
236
2/2
✓ Branch 0 (93→94) taken 81 times.
✓ Branch 1 (93→95) taken 2050692 times.
2050773 if (key->etype == EXPR_SET_ELEM_CATCHALL)
237 81 flags |= NFT_SET_ELEM_CATCHALL;
238
239
2/2
✓ Branch 0 (95→96) taken 682995 times.
✓ Branch 1 (95→97) taken 1367778 times.
2050773 if (flags)
240 682995 nftnl_set_elem_set_u32(nlse, NFTNL_SET_ELEM_FLAGS, flags);
241
242 2050773 return nlse;
243 }
244
245 2116131 void netlink_gen_raw_data(const mpz_t value, enum byteorder byteorder,
246 unsigned int len, struct nft_data_linearize *data)
247 {
248
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2116131 times.
2116131 assert(len > 0);
249
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 2116131 times.
2116131 mpz_export_data(data->value, value, byteorder, len);
250 2116131 data->len = len;
251 2116131 }
252
253 188365 static int netlink_export_pad(unsigned char *data, const mpz_t v,
254 const struct expr *i)
255 {
256
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 188365 times.
188365 mpz_export_data(data, v, i->byteorder,
257 div_round_up(i->len, BITS_PER_BYTE));
258
259 188365 return netlink_padded_len(i->len) / BITS_PER_BYTE;
260 }
261
262 4430 static void byteorder_switch_expr_value(mpz_t v, const struct expr *e)
263 {
264
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 4430 times.
4430 mpz_switch_byteorder(v, div_round_up(e->len, BITS_PER_BYTE));
265 4430 }
266
267 − static int __netlink_gen_concat_key(uint32_t flags, const struct expr *i,
268 unsigned char *data)
269 {
270 struct expr *expr;
271 mpz_t value;
272 int ret;
273
274 − switch (i->etype) {
275 − case EXPR_RANGE:
276 − if (flags & EXPR_F_INTERVAL_END)
277 − expr = i->right;
278 else
279 − expr = i->left;
280
281 − mpz_init_set(value, expr->value);
282
283 − if (expr_basetype(expr)->type == TYPE_INTEGER &&
284 − expr->byteorder == BYTEORDER_HOST_ENDIAN)
285 − byteorder_switch_expr_value(value, expr);
286
287 − i = expr;
288 − break;
289 − case EXPR_RANGE_VALUE:
290 − if (flags & EXPR_F_INTERVAL_END)
291 − mpz_init_set(value, i->range.high);
292 else
293 − mpz_init_set(value, i->range.low);
294
295 − if (expr_basetype(i)->type == TYPE_INTEGER &&
296 − i->byteorder == BYTEORDER_HOST_ENDIAN)
297 − byteorder_switch_expr_value(value, i);
298
299 − break;
300 − case EXPR_PREFIX:
301 − if (flags & EXPR_F_INTERVAL_END) {
302 int count;
303 mpz_t v;
304
305 − mpz_init_bitmask(v, i->len - i->prefix_len);
306
307 − if (i->byteorder == BYTEORDER_HOST_ENDIAN)
308 − byteorder_switch_expr_value(v, i);
309
310 − mpz_add(v, i->prefix->value, v);
311 − count = netlink_export_pad(data, v, i);
312 − mpz_clear(v);
313 − return count;
314 }
315 − return netlink_export_pad(data, i->prefix->value, i);
316 − case EXPR_VALUE:
317 − mpz_init_set(value, i->value);
318
319 /* Switch byteorder to big endian representation when the set
320 * contains concatenation of intervals.
321 */
322 − if (!(flags & (EXPR_F_INTERVAL| EXPR_F_INTERVAL_END)))
323 − break;
324
325 − expr = (struct expr *)i;
326 − if (expr_basetype(expr)->type == TYPE_INTEGER &&
327 − expr->byteorder == BYTEORDER_HOST_ENDIAN)
328 − byteorder_switch_expr_value(value, expr);
329 − break;
330 − default:
331 − BUG("invalid expression type '%s' in set", expr_ops(i)->name);
332 }
333
334 − ret = netlink_export_pad(data, value, i);
335 − mpz_clear(value);
336
337 − return ret;
338 }
339
340 90764 static void nft_data_memcpy(struct nft_data_linearize *nld,
341 const void *src, unsigned int len)
342 {
343
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 90764 times.
90764 if (len > sizeof(nld->value))
344 ✗ BUG("nld buffer overflow: want to copy %u, max %u",
345 len, (unsigned int)sizeof(nld->value));
346
347 90764 memcpy(nld->value, src, len);
348 90764 nld->len = len;
349 90764 }
350
351 90633 static void netlink_gen_concat_key(const struct expr *expr,
352 struct nft_data_linearize *nld)
353 {
354 90633 unsigned int len = netlink_padded_len(expr->len) / BITS_PER_BYTE;
355 unsigned char data[NFT_MAX_EXPR_LEN_BYTES];
356 90633 unsigned int offset = 0;
357 const struct expr *i;
358
359
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 90633 times.
90633 if (len > sizeof(data))
360 ✗ BUG("Value export of %u bytes would overflow", len);
361
362 90633 memset(data, 0, sizeof(data));
363
364
4/6
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 90633 times.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→13) taken 278684 times.
✓ Branch 4 (13→9) taken 188051 times.
✓ Branch 5 (13→14) taken 90633 times.
278684 list_for_each_entry(i, &expr_concat(expr)->expressions, list)
365 188051 offset += __netlink_gen_concat_key(expr->flags, i, data + offset);
366
367 90633 nft_data_memcpy(nld, data, len);
368 90633 }
369
370 − static int __netlink_gen_concat_data(int end, const struct expr *i,
371 unsigned char *data)
372 {
373 mpz_t value;
374 int ret;
375
376 − switch (i->etype) {
377 − case EXPR_RANGE:
378 − if (end)
379 − i = i->right;
380 else
381 − i = i->left;
382
383 − mpz_init_set(value, i->value);
384 − break;
385 − case EXPR_RANGE_VALUE:
386 − if (end)
387 − mpz_init_set(value, i->range.high);
388 else
389 − mpz_init_set(value, i->range.low);
390 − break;
391 − case EXPR_PREFIX:
392 − if (end) {
393 int count;
394
395 − mpz_init_bitmask(value, i->len - i->prefix_len);
396 − mpz_add(value, i->prefix->value, value);
397 − count = netlink_export_pad(data, value, i);
398 − mpz_clear(value);
399 − return count;
400 }
401 − return netlink_export_pad(data, i->prefix->value, i);
402 − case EXPR_VALUE:
403 − mpz_init_set(value, i->value);
404 − break;
405 − default:
406 − BUG("invalid expression type '%s' in set", expr_ops(i)->name);
407 }
408
409 − ret = netlink_export_pad(data, value, i);
410 − mpz_clear(value);
411
412 − return ret;
413 }
414
415 − static void __netlink_gen_concat_expand(const struct expr *expr,
416 struct nft_data_linearize *nld)
417 {
418 − unsigned int len = (netlink_padded_len(expr->len) / BITS_PER_BYTE) * 2;
419 unsigned char data[NFT_MAX_EXPR_LEN_BYTES];
420 − unsigned int offset = 0;
421 const struct expr *i;
422
423 − if (len > sizeof(data))
424 − BUG("Value export of %u bytes would overflow", len);
425
426 − memset(data, 0, sizeof(data));
427
428 − list_for_each_entry(i, &expr_concat(expr)->expressions, list)
429 − offset += __netlink_gen_concat_data(false, i, data + offset);
430
431 − list_for_each_entry(i, &expr_concat(expr)->expressions, list)
432 − offset += __netlink_gen_concat_data(true, i, data + offset);
433
434 − nft_data_memcpy(nld, data, len);
435 − }
436
437 − static void __netlink_gen_concat(const struct expr *expr,
438 struct nft_data_linearize *nld)
439 {
440 − unsigned int len = netlink_padded_len(expr->len) / BITS_PER_BYTE;
441 unsigned char data[NFT_MAX_EXPR_LEN_BYTES];
442 − unsigned int offset = 0;
443 const struct expr *i;
444
445 − if (len > sizeof(data))
446 − BUG("Value export of %u bytes would overflow", len);
447
448 − memset(data, 0, sizeof(data));
449
450 − list_for_each_entry(i, &expr_concat(expr)->expressions, list)
451 − offset += __netlink_gen_concat_data(expr->flags, i, data + offset);
452
453 − nft_data_memcpy(nld, data, len);
454 − }
455
456 78 static void netlink_gen_concat_data(const struct expr *expr,
457 struct nft_data_linearize *nld, bool expand)
458 {
459
2/2
✓ Branch 0 (2→3) taken 26 times.
✓ Branch 1 (2→4) taken 52 times.
78 if (expand)
460 26 __netlink_gen_concat_expand(expr, nld);
461 else
462 52 __netlink_gen_concat(expr, nld);
463 78 }
464
465 2109639 static void netlink_gen_constant_data(const struct expr *expr,
466 struct nft_data_linearize *data)
467 {
468
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2109639 times.
2109639 assert(expr->etype == EXPR_VALUE);
469 2109639 netlink_gen_raw_data(expr->value, expr->byteorder,
470 2109639 div_round_up(expr->len, BITS_PER_BYTE), data);
471 2109639 }
472
473 138547 static void netlink_gen_chain(const struct expr *expr,
474 struct nft_data_linearize *data)
475 {
476 char chain[NFT_CHAIN_MAXNAMELEN];
477 unsigned int len;
478
479 138547 len = expr->chain->len / BITS_PER_BYTE;
480
481
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 138547 times.
138547 if (!len)
482 ✗ BUG("chain length is 0");
483
484
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 138547 times.
138547 if (len > sizeof(chain))
485 ✗ BUG("chain is too large (%u, %u max)",
486 len, (unsigned int)sizeof(chain));
487
488 138547 memset(chain, 0, sizeof(chain));
489
490
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 138547 times.
138547 mpz_export_data(chain, expr->chain->value,
491 BYTEORDER_HOST_ENDIAN, len);
492 138547 snprintf(data->chain, NFT_CHAIN_MAXNAMELEN, "%s", chain);
493 138547 }
494
495 151339 static void netlink_gen_verdict(const struct expr *expr,
496 struct nft_data_linearize *data)
497 {
498
499 151339 data->verdict = expr->verdict;
500
501
2/2
✓ Branch 0 (2→3) taken 138609 times.
✓ Branch 1 (2→7) taken 12730 times.
151339 switch (expr->verdict) {
502 138609 case NFT_JUMP:
503 case NFT_GOTO:
504
2/2
✓ Branch 0 (3→4) taken 138547 times.
✓ Branch 1 (3→5) taken 62 times.
138609 if (expr->chain)
505 138547 netlink_gen_chain(expr, data);
506 else
507 62 data->chain_id = expr->chain_id;
508 138609 break;
509 }
510 151339 }
511
512 3 static void netlink_gen_range(const struct expr *expr,
513 struct nft_data_linearize *nld)
514 {
515 3 unsigned int len = (netlink_padded_len(expr->left->len) / BITS_PER_BYTE) * 2;
516 unsigned char data[NFT_MAX_EXPR_LEN_BYTES];
517 unsigned int offset;
518
519
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 3 times.
3 if (len > sizeof(data))
520 ✗ BUG("Value export of %u bytes would overflow", len);
521
522 3 memset(data, 0, sizeof(data));
523 3 offset = netlink_export_pad(data, expr->left->value, expr->left);
524 3 netlink_export_pad(data + offset, expr->right->value, expr->right);
525 3 nft_data_memcpy(nld, data, len);
526 3 }
527
528 21 static void netlink_gen_range_value(const struct expr *expr,
529 struct nft_data_linearize *nld)
530 {
531 21 unsigned int len = (netlink_padded_len(expr->len) / BITS_PER_BYTE) * 2;
532 unsigned char data[NFT_MAX_EXPR_LEN_BYTES];
533 unsigned int offset;
534
535
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 21 times.
21 if (len > sizeof(data))
536 ✗ BUG("Value export of %u bytes would overflow", len);
537
538 21 memset(data, 0, sizeof(data));
539 21 offset = netlink_export_pad(data, expr->range.low, expr);
540 21 netlink_export_pad(data + offset, expr->range.high, expr);
541 21 nft_data_memcpy(nld, data, len);
542 21 }
543
544 29 static void netlink_gen_prefix(const struct expr *expr,
545 struct nft_data_linearize *nld)
546 {
547 29 unsigned int len = (netlink_padded_len(expr->len) / BITS_PER_BYTE) * 2;
548 unsigned char data[NFT_MAX_EXPR_LEN_BYTES];
549 int offset;
550 mpz_t v;
551
552
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 29 times.
29 if (len > sizeof(data))
553 ✗ BUG("Value export of %u bytes would overflow", len);
554
555 29 offset = netlink_export_pad(data, expr->prefix->value, expr);
556 29 mpz_init_bitmask(v, expr->len - expr->prefix_len);
557 29 mpz_add(v, expr->prefix->value, v);
558 29 netlink_export_pad(data + offset, v, expr->prefix);
559 29 mpz_clear(v);
560
561 29 nft_data_memcpy(nld, data, len);
562 29 }
563
564 2061341 static void netlink_gen_key(const struct expr *expr,
565 struct nft_data_linearize *data)
566 {
567 2061341 memset(data, 0, sizeof(*data));
568
569
2/5
✓ Branch 0 (2→3) taken 1970708 times.
✓ Branch 1 (2→4) taken 90633 times.
✗ Branch 2 (2→5) not taken.
✗ Branch 3 (2→6) not taken.
✗ Branch 4 (2→7) not taken.
2061341 switch (expr->etype) {
570 1970708 case EXPR_VALUE:
571 1970708 return netlink_gen_constant_data(expr, data);
572 90633 case EXPR_CONCAT:
573 90633 return netlink_gen_concat_key(expr, data);
574 ✗ case EXPR_RANGE:
575 ✗ return netlink_gen_range(expr, data);
576 ✗ case EXPR_PREFIX:
577 ✗ return netlink_gen_prefix(expr, data);
578 ✗ default:
579 ✗ BUG("invalid data expression type %s", expr_name(expr));
580 }
581 }
582
583 − static void __netlink_gen_data(const struct expr *expr,
584 struct nft_data_linearize *data, bool expand)
585 {
586 − memset(data, 0, sizeof(*data));
587
588 − switch (expr->etype) {
589 − case EXPR_VALUE:
590 − return netlink_gen_constant_data(expr, data);
591 − case EXPR_CONCAT:
592 − return netlink_gen_concat_data(expr, data, expand);
593 − case EXPR_VERDICT:
594 − return netlink_gen_verdict(expr, data);
595 − case EXPR_RANGE:
596 − return netlink_gen_range(expr, data);
597 − case EXPR_RANGE_VALUE:
598 − return netlink_gen_range_value(expr, data);
599 − case EXPR_PREFIX:
600 − return netlink_gen_prefix(expr, data);
601 − default:
602 − BUG("invalid data expression type %s", expr_name(expr));
603 }
604 }
605
606 121350 void netlink_gen_data(const struct expr *expr, struct nft_data_linearize *data)
607 {
608 121350 __netlink_gen_data(expr, data, false);
609 121350 }
610
611 7209275 struct expr *netlink_alloc_value(const struct location *loc,
612 const struct nft_data_delinearize *nld)
613 {
614 14418550 return constant_expr_alloc(loc, &invalid_type, BYTEORDER_INVALID,
615 7209275 nld->len * BITS_PER_BYTE, nld->value);
616 }
617
618 14493 static struct expr *netlink_alloc_verdict(const struct location *loc,
619 const struct nft_data_delinearize *nld)
620 {
621 struct expr *chain;
622
623
2/2
✓ Branch 0 (2→3) taken 1293 times.
✓ Branch 1 (2→5) taken 13200 times.
14493 switch (nld->verdict) {
624 1293 case NFT_JUMP:
625 case NFT_GOTO:
626 1293 chain = constant_expr_alloc(loc, &string_type,
627 BYTEORDER_HOST_ENDIAN,
628 1293 strlen(nld->chain) * BITS_PER_BYTE,
629 1293 nld->chain);
630 1293 break;
631 13200 default:
632 13200 chain = NULL;
633 13200 break;
634 }
635
636 14493 return verdict_expr_alloc(loc, nld->verdict, chain);
637 }
638
639 36702 struct expr *netlink_alloc_data(const struct location *loc,
640 const struct nft_data_delinearize *nld,
641 enum nft_registers dreg)
642 {
643
2/2
✓ Branch 0 (2→3) taken 14493 times.
✓ Branch 1 (2→4) taken 22209 times.
36702 switch (dreg) {
644 14493 case NFT_REG_VERDICT:
645 14493 return netlink_alloc_verdict(loc, nld);
646 22209 default:
647 22209 return netlink_alloc_value(loc, nld);
648 }
649 }
650
651 69751 void netlink_dump_rule(const struct nftnl_rule *nlr, struct netlink_ctx *ctx)
652 {
653 69751 FILE *fp = ctx->nft->output.output_fp;
654
655
3/4
✓ Branch 0 (2→3) taken 13129 times.
✓ Branch 1 (2→4) taken 56622 times.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→5) taken 13129 times.
69751 if (!(ctx->nft->debug_mask & NFT_DEBUG_NETLINK) || !fp)
656 56622 return;
657
658 13129 nftnl_rule_fprintf(fp, nlr, 0, 0);
659 13129 fprintf(fp, "\n");
660 }
661
662 ✗ void netlink_dump_expr(const struct nftnl_expr *nle,
663 FILE *fp, unsigned int debug_mask)
664 {
665 ✗ if (!(debug_mask & NFT_DEBUG_NETLINK))
666 ✗ return;
667
668 ✗ nftnl_expr_fprintf(fp, nle, 0, 0);
669 ✗ fprintf(fp, "\n");
670 }
671
672 7366 void netlink_dump_chain(const struct nftnl_chain *nlc, struct netlink_ctx *ctx)
673 {
674 7366 FILE *fp = ctx->nft->output.output_fp;
675
676
1/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 7366 times.
✗ Branch 2 (3→4) not taken.
✗ Branch 3 (3→5) not taken.
7366 if (!(ctx->nft->debug_mask & NFT_DEBUG_NETLINK) || !fp)
677 7366 return;
678
679 ✗ nftnl_chain_fprintf(fp, nlc, 0, 0);
680 ✗ fprintf(fp, "\n");
681 }
682
683 3 static int chain_parse_udata_cb(const struct nftnl_udata *attr, void *data)
684 {
685 3 unsigned char *value = nftnl_udata_get(attr);
686 3 uint8_t type = nftnl_udata_type(attr);
687 3 const struct nftnl_udata **tb = data;
688 3 uint8_t len = nftnl_udata_len(attr);
689
690
1/2
✓ Branch 0 (5→6) taken 3 times.
✗ Branch 1 (5→9) not taken.
3 switch (type) {
691 3 case NFTNL_UDATA_CHAIN_COMMENT:
692
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 3 times.
3 if (value[len - 1] != '\0')
693 ✗ return -1;
694 3 break;
695 ✗ default:
696 ✗ return 0;
697 }
698 3 tb[type] = attr;
699 3 return 0;
700 }
701
702 38 static int qsort_device_cmp(const void *a, const void *b)
703 {
704 38 const char **x = (const char **)a;
705 38 const char **y = (const char **)b;
706
707 38 return strcmp(*x, *y);
708 }
709
710 19250 struct chain *netlink_delinearize_chain(struct netlink_ctx *ctx,
711 const struct nftnl_chain *nlc)
712 {
713 19250 const struct nftnl_udata *ud[NFTNL_UDATA_CHAIN_MAX + 1] = {};
714 19250 int priority, policy, len = 0, i;
715 const char * const *dev_array;
716 struct chain *chain;
717 const char *udata;
718 uint32_t ulen;
719
720 19250 chain = chain_alloc();
721 19250 chain->handle.family =
722 19250 nftnl_chain_get_u32(nlc, NFTNL_CHAIN_FAMILY);
723 19250 chain->handle.table.name =
724 19250 xstrdup(nftnl_chain_get_str(nlc, NFTNL_CHAIN_TABLE));
725 19250 chain->handle.chain.name =
726 19250 xstrdup(nftnl_chain_get_str(nlc, NFTNL_CHAIN_NAME));
727 19250 chain->handle.handle.id =
728 19250 nftnl_chain_get_u64(nlc, NFTNL_CHAIN_HANDLE);
729
2/2
✓ Branch 0 (10→11) taken 17631 times.
✓ Branch 1 (10→13) taken 1619 times.
19250 if (nftnl_chain_is_set(nlc, NFTNL_CHAIN_FLAGS))
730 17631 chain->flags = nftnl_chain_get_u32(nlc, NFTNL_CHAIN_FLAGS);
731
732
3/4
✓ Branch 0 (14→15) taken 17604 times.
✓ Branch 1 (14→50) taken 1646 times.
✓ Branch 2 (16→17) taken 17604 times.
✗ Branch 3 (16→50) not taken.
36854 if (nftnl_chain_is_set(nlc, NFTNL_CHAIN_HOOKNUM) &&
733
1/2
✓ Branch 0 (18→19) taken 17604 times.
✗ Branch 1 (18→50) not taken.
35208 nftnl_chain_is_set(nlc, NFTNL_CHAIN_PRIO) &&
734
1/2
✓ Branch 0 (20→21) taken 17604 times.
✗ Branch 1 (20→50) not taken.
35208 nftnl_chain_is_set(nlc, NFTNL_CHAIN_TYPE) &&
735 17604 nftnl_chain_is_set(nlc, NFTNL_CHAIN_POLICY)) {
736 17604 chain->hook.num =
737 17604 nftnl_chain_get_u32(nlc, NFTNL_CHAIN_HOOKNUM);
738 17604 chain->hook.name =
739 17604 hooknum2str(chain->handle.family, chain->hook.num);
740 17604 priority = nftnl_chain_get_s32(nlc, NFTNL_CHAIN_PRIO);
741 17604 chain->priority.expr =
742 17604 constant_expr_alloc(&netlink_location,
743 &integer_type,
744 BYTEORDER_HOST_ENDIAN,
745 sizeof(int) * BITS_PER_BYTE,
746 &priority);
747 17604 chain->type.str =
748 17604 xstrdup(nftnl_chain_get_str(nlc, NFTNL_CHAIN_TYPE));
749 17604 policy = nftnl_chain_get_u32(nlc, NFTNL_CHAIN_POLICY);
750 17604 chain->policy = constant_expr_alloc(&netlink_location,
751 &integer_type,
752 BYTEORDER_HOST_ENDIAN,
753 sizeof(int) * BITS_PER_BYTE,
754 &policy);
755 17604 nftnl_chain_get_u32(nlc, NFTNL_CHAIN_POLICY);
756
2/2
✓ Branch 0 (31→32) taken 6746 times.
✓ Branch 1 (31→36) taken 10858 times.
17604 if (nftnl_chain_is_set(nlc, NFTNL_CHAIN_DEV)) {
757 6746 chain->dev_array = xmalloc(sizeof(char *) * 2);
758 6746 chain->dev_array_len = 1;
759 13492 chain->dev_array[0] =
760 6746 xstrdup(nftnl_chain_get_str(nlc, NFTNL_CHAIN_DEV));
761 6746 chain->dev_array[1] = NULL;
762
2/2
✓ Branch 0 (37→38) taken 19 times.
✓ Branch 1 (37→48) taken 10839 times.
10858 } else if (nftnl_chain_is_set(nlc, NFTNL_CHAIN_DEVICES)) {
763 19 dev_array = nftnl_chain_get(nlc, NFTNL_CHAIN_DEVICES);
764
2/2
✓ Branch 0 (41→40) taken 42 times.
✓ Branch 1 (41→42) taken 19 times.
61 while (dev_array[len])
765 42 len++;
766
767 19 chain->dev_array = xmalloc((len + 1)* sizeof(char *));
768
2/2
✓ Branch 0 (46→44) taken 42 times.
✓ Branch 1 (46→47) taken 19 times.
61 for (i = 0; i < len; i++)
769 42 chain->dev_array[i] = xstrdup(dev_array[i]);
770
771 19 chain->dev_array[i] = NULL;
772 19 chain->dev_array_len = len;
773 }
774 17604 chain->flags |= CHAIN_F_BASECHAIN;
775
776
2/2
✓ Branch 0 (48→49) taken 6765 times.
✓ Branch 1 (48→50) taken 10839 times.
17604 if (chain->dev_array_len) {
777 6765 qsort(chain->dev_array, chain->dev_array_len,
778 sizeof(char *), qsort_device_cmp);
779 }
780 }
781
782
2/2
✓ Branch 0 (51→52) taken 3 times.
✓ Branch 1 (51→62) taken 19247 times.
19250 if (nftnl_chain_is_set(nlc, NFTNL_CHAIN_USERDATA)) {
783 3 udata = nftnl_chain_get_data(nlc, NFTNL_CHAIN_USERDATA, &ulen);
784
1/2
✗ Branch 0 (54→55) not taken.
✓ Branch 1 (54→58) taken 3 times.
3 if (nftnl_udata_parse(udata, ulen, chain_parse_udata_cb, ud) < 0) {
785 ✗ netlink_io_error(ctx, NULL, "Cannot parse userdata");
786 ✗ chain_free(chain);
787 ✗ return NULL;
788 }
789
1/2
✓ Branch 0 (58→59) taken 3 times.
✗ Branch 1 (58→62) not taken.
3 if (ud[NFTNL_UDATA_CHAIN_COMMENT])
790 3 chain->comment = xstrdup(nftnl_udata_get(ud[NFTNL_UDATA_CHAIN_COMMENT]));
791 }
792
793 19250 return chain;
794 }
795
796 157956 static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)
797 {
798 157956 unsigned char *value = nftnl_udata_get(attr);
799 157956 const struct nftnl_udata **tb = data;
800 157956 uint8_t type = nftnl_udata_type(attr);
801 157956 uint8_t len = nftnl_udata_len(attr);
802
803
3/4
✓ Branch 0 (5→6) taken 8 times.
✓ Branch 1 (5→9) taken 78974 times.
✓ Branch 2 (5→12) taken 78974 times.
✗ Branch 3 (5→15) not taken.
157956 switch (type) {
804 8 case NFTNL_UDATA_TABLE_COMMENT:
805
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 8 times.
8 if (value[len - 1] != '\0')
806 ✗ return -1;
807 8 break;
808 78974 case NFTNL_UDATA_TABLE_NFTVER:
809
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 78974 times.
78974 if (len != sizeof(nftversion))
810 ✗ return -1;
811 78974 break;
812 78974 case NFTNL_UDATA_TABLE_NFTBLD:
813
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 78974 times.
78974 if (len != sizeof(nftbuildstamp))
814 ✗ return -1;
815 78974 break;
816 ✗ default:
817 ✗ return 0;
818 }
819 157956 tb[type] = attr;
820 157956 return 0;
821 }
822
823 78974 static int version_cmp(const struct nftnl_udata **ud)
824 {
825 const char *udbuf;
826 size_t i;
827
828 /* netlink attribute lengths checked by table_parse_udata_cb() */
829
1/2
✓ Branch 0 (2→3) taken 78974 times.
✗ Branch 1 (2→9) not taken.
78974 if (ud[NFTNL_UDATA_TABLE_NFTVER]) {
830 78974 udbuf = nftnl_udata_get(ud[NFTNL_UDATA_TABLE_NFTVER]);
831
2/2
✓ Branch 0 (8→5) taken 315896 times.
✓ Branch 1 (8→9) taken 78974 times.
394870 for (i = 0; i < sizeof(nftversion); i++) {
832
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 315896 times.
315896 if (nftversion[i] != udbuf[i])
833 ✗ return nftversion[i] - udbuf[i];
834 }
835 }
836
1/2
✓ Branch 0 (9→10) taken 78974 times.
✗ Branch 1 (9→16) not taken.
78974 if (ud[NFTNL_UDATA_TABLE_NFTBLD]) {
837 78974 udbuf = nftnl_udata_get(ud[NFTNL_UDATA_TABLE_NFTBLD]);
838
1/2
✓ Branch 0 (15→12) taken 631792 times.
✗ Branch 1 (15→16) not taken.
631792 for (i = 0; i < sizeof(nftbuildstamp); i++) {
839
2/2
✓ Branch 0 (12→13) taken 78974 times.
✓ Branch 1 (12→14) taken 552818 times.
631792 if (nftbuildstamp[i] != udbuf[i])
840 78974 return nftbuildstamp[i] - udbuf[i];
841 }
842 }
843 ✗ return 0;
844 }
845
846 78974 struct table *netlink_delinearize_table(struct netlink_ctx *ctx,
847 const struct nftnl_table *nlt)
848 {
849 78974 const struct nftnl_udata *ud[NFTNL_UDATA_TABLE_MAX + 1] = {};
850 struct table *table;
851 const char *udata;
852 uint32_t ulen;
853
854 78974 table = table_alloc();
855 78974 table->handle.family = nftnl_table_get_u32(nlt, NFTNL_TABLE_FAMILY);
856 78974 table->handle.table.name = xstrdup(nftnl_table_get_str(nlt, NFTNL_TABLE_NAME));
857 78974 table->flags = nftnl_table_get_u32(nlt, NFTNL_TABLE_FLAGS);
858 78974 table->handle.handle.id = nftnl_table_get_u64(nlt, NFTNL_TABLE_HANDLE);
859 78974 table->owner = nftnl_table_get_u32(nlt, NFTNL_TABLE_OWNER);
860
861
1/2
✓ Branch 0 (10→11) taken 78974 times.
✗ Branch 1 (10→23) not taken.
78974 if (nftnl_table_is_set(nlt, NFTNL_TABLE_USERDATA)) {
862 78974 udata = nftnl_table_get_data(nlt, NFTNL_TABLE_USERDATA, &ulen);
863
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→17) taken 78974 times.
78974 if (nftnl_udata_parse(udata, ulen, table_parse_udata_cb, ud) < 0) {
864 ✗ netlink_io_error(ctx, NULL, "Cannot parse userdata");
865 ✗ table_free(table);
866 ✗ return NULL;
867 }
868
2/2
✓ Branch 0 (17→18) taken 8 times.
✓ Branch 1 (17→21) taken 78966 times.
78974 if (ud[NFTNL_UDATA_TABLE_COMMENT])
869 8 table->comment = xstrdup(nftnl_udata_get(ud[NFTNL_UDATA_TABLE_COMMENT]));
870 78974 table->is_from_future = version_cmp(ud) < 0;
871 }
872
873 78974 return table;
874 }
875
876 78944 static int list_table_cb(struct nftnl_table *nlt, void *arg)
877 {
878 78944 struct netlink_ctx *ctx = arg;
879 struct table *table;
880
881 78944 table = netlink_delinearize_table(ctx, nlt);
882
1/2
✓ Branch 0 (3→4) taken 78944 times.
✗ Branch 1 (3→5) not taken.
78944 if (table)
883 78944 list_add_tail(&table->list, &ctx->list);
884
885 78944 return 0;
886 }
887
888 39982 int netlink_list_tables(struct netlink_ctx *ctx, const struct handle *h,
889 const struct nft_cache_filter *filter)
890 {
891 struct nftnl_table_list *table_cache;
892 39982 uint32_t family = h->family;
893 39982 const char *table = NULL;
894
895
2/2
✓ Branch 0 (2→3) taken 39957 times.
✓ Branch 1 (2→4) taken 25 times.
39982 if (filter) {
896 39957 family = filter->list.family;
897 39957 table = filter->list.table;
898 }
899
900 39982 table_cache = mnl_nft_table_dump(ctx, family, table);
901
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→9) taken 39982 times.
39982 if (table_cache == NULL) {
902 ✗ if (errno == EINTR)
903 ✗ return -1;
904
905 ✗ return -1;
906 }
907
908 39982 ctx->data = h;
909 39982 nftnl_table_list_foreach(table_cache, list_table_cb, ctx);
910 39982 nftnl_table_list_free(table_cache);
911 39982 return 0;
912 }
913
914 24214 enum nft_data_types dtype_map_to_kernel(const struct datatype *dtype)
915 {
916
2/2
✓ Branch 0 (2→3) taken 9032 times.
✓ Branch 1 (2→4) taken 15182 times.
24214 switch (dtype->type) {
917 9032 case TYPE_VERDICT:
918 9032 return NFT_DATA_VERDICT;
919 15182 default:
920 15182 return dtype->type;
921 }
922 }
923
924 47253 static const struct datatype *dtype_map_from_kernel(enum nft_data_types type)
925 {
926 /* The function always returns ownership of a reference. But for
927 * &verdict_Type and datatype_lookup(), those are static instances,
928 * we can omit the datatype_get() call.
929 */
930
2/2
✓ Branch 0 (2→3) taken 2870 times.
✓ Branch 1 (2→4) taken 44383 times.
47253 switch (type) {
931 2870 case NFT_DATA_VERDICT:
932 2870 return &verdict_type;
933 44383 default:
934
2/2
✓ Branch 0 (4→5) taken 9132 times.
✓ Branch 1 (4→7) taken 35251 times.
44383 if (type & ~TYPE_MASK)
935 9132 return concat_type_alloc(type);
936 35251 return datatype_lookup((enum datatypes) type);
937 }
938 }
939
940 76917 void netlink_dump_set(const struct nftnl_set *nls, struct netlink_ctx *ctx)
941 {
942 76917 FILE *fp = ctx->nft->output.output_fp;
943 uint32_t family;
944
945
3/4
✓ Branch 0 (2→3) taken 23023 times.
✓ Branch 1 (2→4) taken 53894 times.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→5) taken 23023 times.
76917 if (!(ctx->nft->debug_mask & NFT_DEBUG_NETLINK) || !fp)
946 53894 return;
947
948 23023 family = nftnl_set_get_u32(nls, NFTNL_SET_FAMILY);
949 23023 fprintf(fp, "family %d ", family);
950 23023 nftnl_set_fprintf(fp, nls, 0, 0);
951 23023 fprintf(fp, "\n");
952 }
953
954 106075 static int set_parse_udata_cb(const struct nftnl_udata *attr, void *data)
955 {
956 106075 unsigned char *value = nftnl_udata_get(attr);
957 106075 const struct nftnl_udata **tb = data;
958 106075 uint8_t type = nftnl_udata_type(attr);
959 106075 uint8_t len = nftnl_udata_len(attr);
960
961
3/4
✓ Branch 0 (5→6) taken 58986 times.
✓ Branch 1 (5→9) taken 47085 times.
✓ Branch 2 (5→12) taken 4 times.
✗ Branch 3 (5→15) not taken.
106075 switch (type) {
962 58986 case NFTNL_UDATA_SET_KEYBYTEORDER:
963 case NFTNL_UDATA_SET_DATABYTEORDER:
964 case NFTNL_UDATA_SET_MERGE_ELEMENTS:
965 case NFTNL_UDATA_SET_DATA_INTERVAL:
966
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 58986 times.
58986 if (len != sizeof(uint32_t))
967 ✗ return -1;
968 58986 break;
969 47085 case NFTNL_UDATA_SET_KEY_TYPEOF:
970 case NFTNL_UDATA_SET_DATA_TYPEOF:
971
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 47085 times.
47085 if (len < 3)
972 ✗ return -1;
973 47085 break;
974 4 case NFTNL_UDATA_SET_COMMENT:
975
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 4 times.
4 if (value[len - 1] != '\0')
976 ✗ return -1;
977 4 break;
978 ✗ default:
979 ✗ return 0;
980 }
981 106075 tb[type] = attr;
982 106075 return 0;
983 }
984
985 94170 static int set_key_parse_udata(const struct nftnl_udata *attr, void *data)
986 {
987 94170 const struct nftnl_udata **tb = data;
988 94170 uint8_t type = nftnl_udata_type(attr);
989 94170 uint8_t len = nftnl_udata_len(attr);
990
991
2/3
✓ Branch 0 (4→5) taken 47085 times.
✓ Branch 1 (4→8) taken 47085 times.
✗ Branch 2 (4→9) not taken.
94170 switch (type) {
992 47085 case NFTNL_UDATA_SET_TYPEOF_EXPR:
993
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 47085 times.
47085 if (len != sizeof(uint32_t))
994 ✗ return -1;
995 47085 break;
996 47085 case NFTNL_UDATA_SET_TYPEOF_DATA:
997 47085 break;
998 ✗ default:
999 ✗ return 0;
1000 }
1001 94170 tb[type] = attr;
1002 94170 return 0;
1003 }
1004
1005 47410 static struct expr *set_make_key(const struct nftnl_udata *attr)
1006 {
1007 47410 const struct nftnl_udata *ud[NFTNL_UDATA_SET_TYPEOF_MAX + 1] = {};
1008 const struct expr_ops *ops;
1009 struct expr *expr;
1010 uint32_t etype;
1011 int err;
1012
1013
2/2
✓ Branch 0 (2→3) taken 325 times.
✓ Branch 1 (2→4) taken 47085 times.
47410 if (!attr)
1014 325 return NULL;
1015
1016 47085 err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr),
1017 set_key_parse_udata, ud);
1018
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 47085 times.
47085 if (err < 0)
1019 ✗ return NULL;
1020
1021
1/2
✓ Branch 0 (9→10) taken 47085 times.
✗ Branch 1 (9→11) not taken.
47085 if (!ud[NFTNL_UDATA_SET_TYPEOF_EXPR] ||
1022
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 47085 times.
47085 !ud[NFTNL_UDATA_SET_TYPEOF_DATA])
1023 ✗ return NULL;
1024
1025 47085 etype = nftnl_udata_get_u32(ud[NFTNL_UDATA_SET_TYPEOF_EXPR]);
1026 47085 ops = expr_ops_by_type_u32(etype);
1027
2/4
✓ Branch 0 (14→15) taken 47085 times.
✗ Branch 1 (14→16) not taken.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→17) taken 47085 times.
47085 if (!ops || !ops->parse_udata)
1028 ✗ return NULL;
1029
1030 47085 expr = ops->parse_udata(ud[NFTNL_UDATA_SET_TYPEOF_DATA]);
1031
2/2
✓ Branch 0 (18→19) taken 32631 times.
✓ Branch 1 (18→20) taken 14454 times.
47085 if (!expr)
1032 32631 return NULL;
1033
1034 14454 return expr;
1035 }
1036
1037 47410 static bool set_udata_key_valid(const struct expr *e, uint32_t len)
1038 {
1039
2/2
✓ Branch 0 (2→3) taken 33001 times.
✓ Branch 1 (2→4) taken 14409 times.
47410 if (!e)
1040 33001 return false;
1041
1042 14409 return div_round_up(e->len, BITS_PER_BYTE) == len / BITS_PER_BYTE;
1043 }
1044
1045 struct setelem_parse_ctx {
1046 struct set *set;
1047 struct nft_cache *cache;
1048 struct list_head stmt_list;
1049 };
1050
1051 48 static int set_elem_parse_expressions(struct nftnl_expr *e, void *data)
1052 {
1053 48 struct setelem_parse_ctx *setelem_parse_ctx = data;
1054 48 struct nft_cache *cache = setelem_parse_ctx->cache;
1055 48 struct set *set = setelem_parse_ctx->set;
1056 struct stmt *stmt;
1057
1058 48 stmt = netlink_parse_set_expr(set, cache, e);
1059
1/2
✓ Branch 0 (3→4) taken 48 times.
✗ Branch 1 (3→5) not taken.
48 if (stmt)
1060 48 list_add_tail(&stmt->list, &setelem_parse_ctx->stmt_list);
1061
1062 48 return 0;
1063 }
1064
1065 37378 struct set *netlink_delinearize_set(struct netlink_ctx *ctx,
1066 const struct nftnl_set *nls)
1067 {
1068 37378 const struct nftnl_udata *ud[NFTNL_UDATA_SET_MAX + 1] = {};
1069 37378 enum byteorder keybyteorder = BYTEORDER_INVALID;
1070 37378 enum byteorder databyteorder = BYTEORDER_INVALID;
1071 struct setelem_parse_ctx set_parse_ctx;
1072 37378 const struct datatype *datatype = NULL;
1073 37378 const struct datatype *keytype = NULL;
1074 37378 const struct datatype *dtype2 = NULL;
1075 37378 const struct datatype *dtype = NULL;
1076 37378 struct expr *typeof_expr_data = NULL;
1077 37378 struct expr *typeof_expr_key = NULL;
1078 37378 const char *udata, *comment = NULL;
1079 37378 uint32_t flags, key, objtype = 0;
1080 37378 uint32_t data_interval = 0;
1081 37378 bool automerge = false;
1082 struct set *set;
1083 uint32_t ulen;
1084 uint32_t klen;
1085
1086
1/2
✓ Branch 0 (3→4) taken 37378 times.
✗ Branch 1 (3→23) not taken.
37378 if (nftnl_set_is_set(nls, NFTNL_SET_USERDATA)) {
1087 37378 udata = nftnl_set_get_data(nls, NFTNL_SET_USERDATA, &ulen);
1088
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 37378 times.
37378 if (nftnl_udata_parse(udata, ulen, set_parse_udata_cb, ud) < 0) {
1089 ✗ netlink_io_error(ctx, NULL, "Cannot parse userdata");
1090 ✗ return NULL;
1091 }
1092
1093 #define GET_U32_UDATA(var, attr) \
1094 if (ud[attr]) \
1095 var = nftnl_udata_get_u32(ud[attr])
1096
1097
1/2
✓ Branch 0 (9→10) taken 37378 times.
✗ Branch 1 (9→11) not taken.
37378 GET_U32_UDATA(keybyteorder, NFTNL_UDATA_SET_KEYBYTEORDER);
1098
2/2
✓ Branch 0 (11→12) taken 9875 times.
✓ Branch 1 (11→13) taken 27503 times.
37378 GET_U32_UDATA(databyteorder, NFTNL_UDATA_SET_DATABYTEORDER);
1099
2/2
✓ Branch 0 (13→14) taken 1701 times.
✓ Branch 1 (13→16) taken 35677 times.
37378 GET_U32_UDATA(automerge, NFTNL_UDATA_SET_MERGE_ELEMENTS);
1100
2/2
✓ Branch 0 (16→17) taken 10032 times.
✓ Branch 1 (16→18) taken 27346 times.
37378 GET_U32_UDATA(data_interval, NFTNL_UDATA_SET_DATA_INTERVAL);
1101
1102 #undef GET_U32_UDATA
1103 37378 typeof_expr_key = set_make_key(ud[NFTNL_UDATA_SET_KEY_TYPEOF]);
1104
2/2
✓ Branch 0 (19→20) taken 10032 times.
✓ Branch 1 (19→21) taken 27346 times.
37378 if (ud[NFTNL_UDATA_SET_DATA_TYPEOF])
1105 10032 typeof_expr_data = set_make_key(ud[NFTNL_UDATA_SET_DATA_TYPEOF]);
1106
2/2
✓ Branch 0 (21→22) taken 4 times.
✓ Branch 1 (21→23) taken 37374 times.
37378 if (ud[NFTNL_UDATA_SET_COMMENT])
1107 4 comment = nftnl_udata_get(ud[NFTNL_UDATA_SET_COMMENT]);
1108 }
1109
1110 37378 key = nftnl_set_get_u32(nls, NFTNL_SET_KEY_TYPE);
1111 37378 keytype = dtype_map_from_kernel(key);
1112
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→28) taken 37378 times.
37378 if (keytype == NULL) {
1113 ✗ netlink_io_error(ctx, NULL, "Unknown data type in set key %u",
1114 key);
1115 ✗ return NULL;
1116 }
1117
1118 37378 flags = nftnl_set_get_u32(nls, NFTNL_SET_FLAGS);
1119
2/2
✓ Branch 0 (30→31) taken 9875 times.
✓ Branch 1 (30→36) taken 27503 times.
37378 if (set_is_datamap(flags)) {
1120 uint32_t data;
1121
1122 9875 data = nftnl_set_get_u32(nls, NFTNL_SET_DATA_TYPE);
1123 9875 datatype = dtype_map_from_kernel(data);
1124
1/2
✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→36) taken 9875 times.
9875 if (datatype == NULL) {
1125 ✗ netlink_io_error(ctx, NULL,
1126 "Unknown data type in set key %u",
1127 data);
1128 ✗ set = NULL;
1129 ✗ goto out;
1130 }
1131 }
1132
1133
2/2
✓ Branch 0 (37→38) taken 157 times.
✓ Branch 1 (37→42) taken 37221 times.
37378 if (set_is_objmap(flags)) {
1134 157 objtype = nftnl_set_get_u32(nls, NFTNL_SET_OBJ_TYPE);
1135
1/2
✗ Branch 0 (39→40) not taken.
✓ Branch 1 (39→41) taken 157 times.
157 assert(!datatype);
1136 157 datatype = &string_type;
1137 }
1138
1139 37378 set = set_alloc(&netlink_location);
1140 37378 set->handle.family = nftnl_set_get_u32(nls, NFTNL_SET_FAMILY);
1141 37378 set->handle.table.name = xstrdup(nftnl_set_get_str(nls, NFTNL_SET_TABLE));
1142 37378 set->handle.set.name = xstrdup(nftnl_set_get_str(nls, NFTNL_SET_NAME));
1143 37378 set->automerge = automerge;
1144
2/2
✓ Branch 0 (48→49) taken 4 times.
✓ Branch 1 (48→51) taken 37374 times.
37378 if (comment)
1145 4 set->comment = xstrdup(comment);
1146
1147 37378 init_list_head(&set_parse_ctx.stmt_list);
1148
1149
2/2
✓ Branch 0 (53→54) taken 1820 times.
✓ Branch 1 (53→57) taken 35558 times.
37378 if (nftnl_set_is_set(nls, NFTNL_SET_EXPR)) {
1150 const struct nftnl_expr *nle;
1151 struct stmt *stmt;
1152
1153 1820 nle = nftnl_set_get(nls, NFTNL_SET_EXPR);
1154 1820 stmt = netlink_parse_set_expr(set, &ctx->nft->cache, nle);
1155 1820 list_add_tail(&stmt->list, &set_parse_ctx.stmt_list);
1156
2/2
✓ Branch 0 (58→59) taken 10 times.
✓ Branch 1 (58→60) taken 35548 times.
35558 } else if (nftnl_set_is_set(nls, NFTNL_SET_EXPRESSIONS)) {
1157 10 set_parse_ctx.cache = &ctx->nft->cache;
1158 10 set_parse_ctx.set = set;
1159 10 nftnl_set_expr_foreach(nls, set_elem_parse_expressions,
1160 &set_parse_ctx);
1161 }
1162 37378 list_splice_tail(&set_parse_ctx.stmt_list, &set->stmt_list);
1163
1164 37378 set->flags = nftnl_set_get_u32(nls, NFTNL_SET_FLAGS);
1165
1166
2/2
✓ Branch 0 (62→63) taken 10032 times.
✓ Branch 1 (62→79) taken 27346 times.
37378 if (datatype) {
1167 uint32_t dlen;
1168
1169 10032 dtype2 = set_datatype_alloc(datatype, databyteorder);
1170 10032 klen = nftnl_set_get_u32(nls, NFTNL_SET_DATA_LEN) * BITS_PER_BYTE;
1171
1172
2/2
✓ Branch 0 (65→66) taken 56 times.
✓ Branch 1 (65→67) taken 9976 times.
10032 dlen = data_interval ? klen / 2 : klen;
1173
1174
2/2
✓ Branch 0 (69→70) taken 2737 times.
✓ Branch 1 (69→71) taken 7295 times.
10032 if (set_udata_key_valid(typeof_expr_data, dlen)) {
1175 2737 typeof_expr_data->len = klen;
1176 2737 set->data = typeof_expr_data;
1177 2737 typeof_expr_data = NULL;
1178
2/2
✓ Branch 0 (71→72) taken 157 times.
✓ Branch 1 (71→74) taken 7138 times.
7295 } else if (set->flags & NFT_SET_OBJECT) {
1179 157 set->data = constant_expr_alloc(&netlink_location,
1180 dtype2,
1181 databyteorder, klen,
1182 NULL);
1183 } else {
1184 7138 set->data = constant_expr_alloc(&netlink_location,
1185 dtype2,
1186 databyteorder, klen,
1187 NULL);
1188
1189 /* Can't use 'typeof' keyword, so discard key too */
1190 7138 expr_free(typeof_expr_key);
1191 7138 typeof_expr_key = NULL;
1192 }
1193
1194
2/2
✓ Branch 0 (77→78) taken 56 times.
✓ Branch 1 (77→79) taken 9976 times.
10032 if (data_interval)
1195 56 set->data->flags |= EXPR_F_INTERVAL;
1196 }
1197
1198 37378 dtype = set_datatype_alloc(keytype, keybyteorder);
1199 37378 klen = nftnl_set_get_u32(nls, NFTNL_SET_KEY_LEN) * BITS_PER_BYTE;
1200
1201
2/2
✓ Branch 0 (82→83) taken 11668 times.
✓ Branch 1 (82→84) taken 25710 times.
37378 if (set_udata_key_valid(typeof_expr_key, klen)) {
1202 11668 set->key = typeof_expr_key;
1203 11668 typeof_expr_key = NULL;
1204 11668 set->key_typeof_valid = true;
1205 } else {
1206 25710 set->key = constant_expr_alloc(&netlink_location, dtype,
1207 keybyteorder, klen,
1208 NULL);
1209 }
1210
1211 37378 set->handle.handle.id = nftnl_set_get_u64(nls, NFTNL_SET_HANDLE);
1212
1213 37378 set->objtype = objtype;
1214
1215
2/2
✓ Branch 0 (88→89) taken 1119 times.
✓ Branch 1 (88→91) taken 36259 times.
37378 if (nftnl_set_is_set(nls, NFTNL_SET_TIMEOUT))
1216 1119 set->timeout = nftnl_set_get_u64(nls, NFTNL_SET_TIMEOUT);
1217
2/2
✓ Branch 0 (92→93) taken 852 times.
✓ Branch 1 (92→95) taken 36526 times.
37378 if (nftnl_set_is_set(nls, NFTNL_SET_GC_INTERVAL))
1218 852 set->gc_int = nftnl_set_get_u32(nls, NFTNL_SET_GC_INTERVAL);
1219
1220
1/2
✗ Branch 0 (96→97) not taken.
✓ Branch 1 (96→99) taken 37378 times.
37378 if (nftnl_set_is_set(nls, NFTNL_SET_POLICY))
1221 ✗ set->policy = nftnl_set_get_u32(nls, NFTNL_SET_POLICY);
1222
1223
2/2
✓ Branch 0 (100→101) taken 2766 times.
✓ Branch 1 (100→103) taken 34612 times.
37378 if (nftnl_set_is_set(nls, NFTNL_SET_DESC_SIZE))
1224 2766 set->desc.size = nftnl_set_get_u32(nls, NFTNL_SET_DESC_SIZE);
1225
1226
1/2
✓ Branch 0 (104→105) taken 37378 times.
✗ Branch 1 (104→107) not taken.
37378 if (nftnl_set_is_set(nls, NFTNL_SET_COUNT))
1227 37378 set->count = nftnl_set_get_u32(nls, NFTNL_SET_COUNT);
1228
1229
2/2
✓ Branch 0 (108→109) taken 34292 times.
✓ Branch 1 (108→110) taken 3086 times.
37378 if (nftnl_set_is_set(nls, NFTNL_SET_DESC_CONCAT)) {
1230 3086 uint32_t len = NFT_REG32_COUNT;
1231 const uint8_t *data;
1232
1233 3086 data = nftnl_set_get_data(nls, NFTNL_SET_DESC_CONCAT, &len);
1234
1/2
✓ Branch 0 (111→112) taken 3086 times.
✗ Branch 1 (111→113) not taken.
3086 if (data) {
1235 3086 memcpy(set->desc.field_len, data, len);
1236 3086 set->desc.field_count = len;
1237 }
1238 }
1239
1240 34292 out:
1241 37378 expr_free(typeof_expr_data);
1242 37378 expr_free(typeof_expr_key);
1243 37378 datatype_free(datatype);
1244 37378 datatype_free(keytype);
1245 37378 datatype_free(dtype2);
1246 37378 datatype_free(dtype);
1247 37378 return set;
1248 }
1249
1250 1885 void alloc_setelem_cache(const struct expr *set, struct nftnl_set *nls)
1251 {
1252 struct nftnl_set_elem *nlse;
1253 const struct expr *expr;
1254
1255
4/6
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1885 times.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→10) taken 3815 times.
✓ Branch 4 (10→5) taken 1930 times.
✓ Branch 5 (10→11) taken 1885 times.
3815 list_for_each_entry(expr, &expr_set(set)->expressions, list) {
1256 1930 nlse = alloc_nftnl_setelem(set, expr);
1257 1930 nftnl_set_elem_add(nls, nlse);
1258 }
1259 1885 }
1260
1261 108 static bool range_expr_is_prefix(const struct expr *range, uint32_t *prefix_len)
1262 {
1263 108 const struct expr *right = range->right;
1264 108 const struct expr *left = range->left;
1265 108 uint32_t len = left->len;
1266 unsigned long n1, n2;
1267 uint32_t plen;
1268 mpz_t bitmask;
1269
1270 108 mpz_init2(bitmask, left->len);
1271 108 mpz_xor(bitmask, left->value, right->value);
1272
1273 108 n1 = mpz_scan0(bitmask, 0);
1274
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 108 times.
108 if (n1 == ULONG_MAX)
1275 ✗ goto not_a_prefix;
1276
1277 108 n2 = mpz_scan1(bitmask, n1 + 1);
1278
2/2
✓ Branch 0 (6→7) taken 55 times.
✓ Branch 1 (6→8) taken 53 times.
108 if (n2 < len)
1279 55 goto not_a_prefix;
1280
1281 53 plen = len - n1;
1282
1283
2/2
✓ Branch 0 (8→9) taken 2 times.
✓ Branch 1 (8→10) taken 51 times.
53 if (mpz_scan1(left->value, 0) < len - plen)
1284 2 goto not_a_prefix;
1285
1286 51 mpz_clear(bitmask);
1287 51 *prefix_len = plen;
1288
1289 51 return true;
1290
1291 57 not_a_prefix:
1292 57 mpz_clear(bitmask);
1293
1294 57 return false;
1295 }
1296
1297 108 struct expr *range_expr_to_prefix(struct expr *range)
1298 {
1299 struct expr *prefix;
1300 uint32_t prefix_len;
1301
1302
2/2
✓ Branch 0 (3→4) taken 51 times.
✓ Branch 1 (3→8) taken 57 times.
108 if (range_expr_is_prefix(range, &prefix_len)) {
1303 51 prefix = prefix_expr_alloc(&range->location,
1304 expr_get(range->left),
1305 prefix_len);
1306 51 expr_free(range);
1307 51 return prefix;
1308 }
1309
1310 57 return range;
1311 }
1312
1313 86 static struct expr *range_expr_reduce(struct expr *range)
1314 {
1315 struct expr *expr;
1316
1317
2/2
✓ Branch 0 (2→3) taken 44 times.
✓ Branch 1 (2→6) taken 42 times.
86 if (!mpz_cmp(range->left->value, range->right->value)) {
1318 44 expr = expr_get(range->left);
1319 44 expr_free(range);
1320 44 return expr;
1321 }
1322
1323
2/2
✓ Branch 0 (6→7) taken 17 times.
✓ Branch 1 (6→9) taken 25 times.
42 if (range->left->dtype->type != TYPE_IPADDR &&
1324
1/2
✓ Branch 0 (7→8) taken 17 times.
✗ Branch 1 (7→9) not taken.
17 range->left->dtype->type != TYPE_IP6ADDR)
1325 17 return range;
1326
1327 25 return range_expr_to_prefix(range);
1328 }
1329
1330 56 static struct expr *netlink_parse_interval_elem(const struct set *set,
1331 struct expr *expr)
1332 {
1333 56 unsigned int len = netlink_padded_len(expr->len) / BITS_PER_BYTE;
1334 56 const struct datatype *dtype = set->data->dtype;
1335 struct expr *range, *left, *right;
1336 char data[NFT_MAX_EXPR_LEN_BYTES];
1337
1338
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 56 times.
56 if (len > sizeof(data))
1339 ✗ BUG("Value export of %u bytes would overflow", len);
1340
1341 56 memset(data, 0, sizeof(data));
1342
1343
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 56 times.
56 mpz_export_data(data, expr->value, dtype->byteorder, len);
1344 56 left = constant_expr_alloc(&internal_location, dtype,
1345 56 dtype->byteorder,
1346 56 (len / 2) * BITS_PER_BYTE, &data[0]);
1347 56 right = constant_expr_alloc(&internal_location, dtype,
1348 56 dtype->byteorder,
1349 56 (len / 2) * BITS_PER_BYTE, &data[len / 2]);
1350 56 range = range_expr_alloc(&expr->location, left, right);
1351 56 expr_free(expr);
1352
1353 56 return range_expr_to_prefix(range);
1354 }
1355
1356 60337 static struct expr *concat_elem_expr(const struct set *set, struct expr *key,
1357 const struct datatype *dtype,
1358 struct expr *data, int *off)
1359 {
1360 const struct datatype *subtype;
1361 unsigned int sub_length;
1362 struct expr *expr;
1363
1364
2/2
✓ Branch 0 (2→3) taken 1535 times.
✓ Branch 1 (2→6) taken 58802 times.
60337 if (key) {
1365 1535 (*off)--;
1366 1535 sub_length = round_up(key->len, BITS_PER_BYTE);
1367
1368 1535 expr = constant_expr_splice(data, sub_length);
1369 1535 expr->dtype = datatype_get(key->dtype);
1370 1535 expr->byteorder = key->byteorder;
1371 1535 expr->len = key->len;
1372 } else {
1373 58802 subtype = concat_subtype_lookup(dtype->type, --(*off));
1374 58802 sub_length = round_up(subtype->size, BITS_PER_BYTE);
1375 58802 expr = constant_expr_splice(data, sub_length);
1376 58802 expr->dtype = subtype;
1377 58802 expr->byteorder = subtype->byteorder;
1378 }
1379
1380
2/2
✓ Branch 0 (10→11) taken 47185 times.
✓ Branch 1 (10→13) taken 13152 times.
60337 if (expr_basetype(expr)->type == TYPE_STRING ||
1381
2/2
✓ Branch 0 (11→12) taken 13769 times.
✓ Branch 1 (11→16) taken 33416 times.
47185 (!(set->flags & NFT_SET_INTERVAL) &&
1382
2/2
✓ Branch 0 (12→13) taken 251 times.
✓ Branch 1 (12→16) taken 13518 times.
13769 expr->byteorder == BYTEORDER_HOST_ENDIAN))
1383
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 13403 times.
13403 mpz_switch_byteorder(expr->value, expr->len / BITS_PER_BYTE);
1384
1385
2/2
✓ Branch 0 (16→17) taken 60252 times.
✓ Branch 1 (16→19) taken 85 times.
60337 if (expr->dtype->basetype != NULL &&
1386
2/2
✓ Branch 0 (17→18) taken 38 times.
✓ Branch 1 (17→19) taken 60214 times.
60252 expr->dtype->basetype->type == TYPE_BITMASK)
1387 38 expr = bitmask_expr_to_binops(expr);
1388
1389 60337 data->len -= netlink_padding_len(sub_length);
1390
1391 60337 return expr;
1392 }
1393
1394 27983 static struct expr *netlink_parse_concat_elem_key(const struct set *set,
1395 struct expr *data)
1396 {
1397 27983 const struct datatype *dtype = set->key->dtype;
1398 27983 struct expr *concat, *expr, *n = NULL;
1399 27983 int off = dtype->subtypes;
1400
1401
2/2
✓ Branch 0 (2→3) taken 709 times.
✓ Branch 1 (2→6) taken 27274 times.
27983 if (set->key->etype == EXPR_CONCAT)
1402
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 709 times.
709 n = list_first_entry(&expr_concat(set->key)->expressions, struct expr, list);
1403
1404 27983 concat = concat_expr_alloc(&data->location);
1405
2/2
✓ Branch 0 (13→8) taken 60105 times.
✓ Branch 1 (13→14) taken 27983 times.
88088 while (off > 0) {
1406 60105 expr = concat_elem_expr(set, n, dtype, data, &off);
1407 60105 concat_expr_add(concat, expr);
1408
2/2
✓ Branch 0 (10→11) taken 1535 times.
✓ Branch 1 (10→12) taken 58570 times.
60105 if (set->key->etype == EXPR_CONCAT)
1409 1535 n = list_next_entry(n, list);
1410 }
1411
1412 27983 expr_free(data);
1413
1414 27983 return concat;
1415 }
1416
1417 73 static struct expr *netlink_parse_concat_elem(const struct set *set,
1418 struct expr *data)
1419 {
1420 73 const struct datatype *dtype = set->data->dtype;
1421 struct expr *concat, *expr, *left, *range;
1422 struct list_head expressions;
1423 73 int off = dtype->subtypes;
1424
1425 73 init_list_head(&expressions);
1426
1427 73 concat = concat_expr_alloc(&data->location);
1428
2/2
✓ Branch 0 (7→5) taken 146 times.
✓ Branch 1 (7→8) taken 73 times.
219 while (off > 0) {
1429 146 expr = concat_elem_expr(set, NULL, dtype, data, &off);
1430 146 list_add_tail(&expr->list, &expressions);
1431 }
1432
1433
2/2
✓ Branch 0 (8→9) taken 43 times.
✓ Branch 1 (8→22) taken 30 times.
73 if (set->data->flags & EXPR_F_INTERVAL) {
1434
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 43 times.
43 assert(!list_empty(&expressions));
1435
1436 43 off = dtype->subtypes;
1437
1438
2/2
✓ Branch 0 (18→13) taken 86 times.
✓ Branch 1 (18→19) taken 43 times.
129 while (off > 0) {
1439 86 left = list_first_entry(&expressions, struct expr, list);
1440
1441 86 expr = concat_elem_expr(set, NULL, dtype, data, &off);
1442 86 list_del(&left->list);
1443
1444 86 range = range_expr_alloc(&data->location, left, expr);
1445 86 range = range_expr_reduce(range);
1446 86 concat_expr_add(concat, range);
1447 }
1448
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→25) taken 43 times.
43 assert(list_empty(&expressions));
1449 } else {
1450
1/2
✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 30 times.
30 list_splice_tail(&expressions, &expr_concat(concat)->expressions);
1451 }
1452
1453 73 expr_free(data);
1454
1455 73 return concat;
1456 }
1457
1458 83 static int set_elem_parse_udata_cb(const struct nftnl_udata *attr, void *data)
1459 {
1460 83 const struct nftnl_udata **tb = data;
1461 83 unsigned char *value = nftnl_udata_get(attr);
1462 83 uint8_t type = nftnl_udata_type(attr);
1463 83 uint8_t len = nftnl_udata_len(attr);
1464
1465
2/3
✓ Branch 0 (5→6) taken 16 times.
✓ Branch 1 (5→9) taken 67 times.
✗ Branch 2 (5→12) not taken.
83 switch (type) {
1466 16 case NFTNL_UDATA_SET_ELEM_COMMENT:
1467
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 16 times.
16 if (value[len - 1] != '\0')
1468 ✗ return -1;
1469 16 break;
1470 67 case NFTNL_UDATA_SET_ELEM_FLAGS:
1471
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 67 times.
67 if (len != sizeof(uint32_t))
1472 ✗ return -1;
1473 67 break;
1474 ✗ default:
1475 ✗ return 0;
1476 }
1477 83 tb[type] = attr;
1478 83 return 0;
1479 }
1480
1481 83 static void set_elem_parse_udata(struct nftnl_set_elem *nlse,
1482 struct expr *expr)
1483 {
1484 83 const struct nftnl_udata *ud[NFTNL_UDATA_SET_ELEM_MAX + 1] = {};
1485 const void *data;
1486 uint32_t len;
1487
1488 83 data = nftnl_set_elem_get(nlse, NFTNL_SET_ELEM_USERDATA, &len);
1489
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 83 times.
83 if (nftnl_udata_parse(data, len, set_elem_parse_udata_cb, ud))
1490 ✗ return;
1491
1492
2/2
✓ Branch 0 (6→7) taken 16 times.
✓ Branch 1 (6→10) taken 67 times.
83 if (ud[NFTNL_UDATA_SET_ELEM_COMMENT])
1493 16 expr->comment =
1494 16 xstrdup(nftnl_udata_get(ud[NFTNL_UDATA_SET_ELEM_COMMENT]));
1495
2/2
✓ Branch 0 (10→11) taken 67 times.
✓ Branch 1 (10→14) taken 16 times.
83 if (ud[NFTNL_UDATA_SET_ELEM_FLAGS]) {
1496 uint32_t elem_flags;
1497
1498 elem_flags =
1499 67 nftnl_udata_get_u32(ud[NFTNL_UDATA_SET_ELEM_FLAGS]);
1500
1/2
✓ Branch 0 (12→13) taken 67 times.
✗ Branch 1 (12→14) not taken.
67 if (elem_flags & NFTNL_SET_ELEM_F_INTERVAL_OPEN)
1501 67 expr->flags |= EXPR_F_INTERVAL_OPEN;
1502 }
1503 }
1504
1505 7115261 int netlink_delinearize_setelem(struct netlink_ctx *ctx,
1506 struct nftnl_set_elem *nlse,
1507 struct set *set)
1508 {
1509 7115261 struct setelem_parse_ctx setelem_parse_ctx = {
1510 .set = set,
1511 7115261 .cache = &ctx->nft->cache,
1512 };
1513 struct nft_data_delinearize nld;
1514 struct expr *expr, *key, *data;
1515 7115261 uint32_t flags = 0;
1516
1517 7115261 init_list_head(&setelem_parse_ctx.stmt_list);
1518
1519
2/2
✓ Branch 0 (4→5) taken 7115179 times.
✓ Branch 1 (4→7) taken 82 times.
7115261 if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_KEY))
1520 7115179 nld.value = nftnl_set_elem_get(nlse, NFTNL_SET_ELEM_KEY, &nld.len);
1521
2/2
✓ Branch 0 (8→9) taken 3704003 times.
✓ Branch 1 (8→10) taken 3411258 times.
7115261 if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_FLAGS))
1522 3411258 flags = nftnl_set_elem_get_u32(nlse, NFTNL_SET_ELEM_FLAGS);
1523
1524 7115261 key_end:
1525
2/2
✓ Branch 0 (13→14) taken 7122566 times.
✓ Branch 1 (13→26) taken 82 times.
7122648 if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_KEY)) {
1526 7122566 key = netlink_alloc_value(&netlink_location, &nld);
1527 7122566 datatype_set(key, set->key->dtype);
1528 7122566 key->byteorder = set->key->byteorder;
1529
2/2
✓ Branch 0 (16→17) taken 27983 times.
✓ Branch 1 (16→18) taken 7094583 times.
7122566 if (set->key->dtype->subtypes)
1530 27983 key = netlink_parse_concat_elem_key(set, key);
1531
1532
2/2
✓ Branch 0 (18→19) taken 288337 times.
✓ Branch 1 (18→23) taken 6834229 times.
7122566 if (!(set->flags & NFT_SET_INTERVAL) &&
1533
2/2
✓ Branch 0 (19→20) taken 3939 times.
✓ Branch 1 (19→23) taken 284398 times.
288337 key->byteorder == BYTEORDER_HOST_ENDIAN)
1534
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 3939 times.
3939 mpz_switch_byteorder(key->value, key->len / BITS_PER_BYTE);
1535
1536
2/2
✓ Branch 0 (23→24) taken 7091225 times.
✓ Branch 1 (23→32) taken 31341 times.
7122566 if (key->dtype->basetype != NULL &&
1537
2/2
✓ Branch 0 (24→25) taken 402 times.
✓ Branch 1 (24→32) taken 7090823 times.
7091225 key->dtype->basetype->type == TYPE_BITMASK)
1538 402 key = bitmask_expr_to_binops(key);
1539
1/2
✓ Branch 0 (26→27) taken 82 times.
✗ Branch 1 (26→30) not taken.
82 } else if (flags & NFT_SET_ELEM_CATCHALL) {
1540 82 key = set_elem_catchall_expr_alloc(&netlink_location);
1541 82 datatype_set(key, set->key->dtype);
1542 82 key->byteorder = set->key->byteorder;
1543 82 key->len = set->key->len;
1544 } else {
1545 ✗ netlink_io_error(ctx, NULL,
1546 "Unexpected set element with no key");
1547 ✗ return 0;
1548 }
1549
1550 7122648 expr = set_elem_expr_alloc(&netlink_location, key);
1551 7122648 expr->flags |= EXPR_F_KERNEL;
1552
1553
2/2
✓ Branch 0 (34→35) taken 1636 times.
✓ Branch 1 (34→38) taken 7121012 times.
7122648 if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_TIMEOUT)) {
1554 1636 expr->timeout = nftnl_set_elem_get_u64(nlse, NFTNL_SET_ELEM_TIMEOUT);
1555
2/2
✓ Branch 0 (36→37) taken 659 times.
✓ Branch 1 (36→38) taken 977 times.
1636 if (expr->timeout == 0)
1556 659 expr->timeout = NFT_NEVER_TIMEOUT;
1557 }
1558
1559
2/2
✓ Branch 0 (39→40) taken 1818 times.
✓ Branch 1 (39→42) taken 7120830 times.
7122648 if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_EXPIRATION))
1560 1818 expr->expiration = nftnl_set_elem_get_u64(nlse, NFTNL_SET_ELEM_EXPIRATION);
1561
2/2
✓ Branch 0 (43→44) taken 83 times.
✓ Branch 1 (43→47) taken 7122565 times.
7122648 if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_USERDATA)) {
1562 83 set_elem_parse_udata(nlse, expr);
1563
2/2
✓ Branch 0 (45→46) taken 16 times.
✓ Branch 1 (45→47) taken 67 times.
83 if (expr->comment)
1564 16 set->elem_has_comment = true;
1565 }
1566
2/2
✓ Branch 0 (48→49) taken 183626 times.
✓ Branch 1 (48→52) taken 6939022 times.
7122648 if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_EXPR)) {
1567 const struct nftnl_expr *nle;
1568 struct stmt *stmt;
1569
1570 183626 nle = nftnl_set_elem_get(nlse, NFTNL_SET_ELEM_EXPR, NULL);
1571 183626 stmt = netlink_parse_set_expr(set, &ctx->nft->cache, nle);
1572 183626 list_add_tail(&stmt->list, &setelem_parse_ctx.stmt_list);
1573
2/2
✓ Branch 0 (53→54) taken 14 times.
✓ Branch 1 (53→55) taken 6939008 times.
6939022 } else if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_EXPRESSIONS)) {
1574 14 nftnl_set_elem_expr_foreach(nlse, set_elem_parse_expressions,
1575 &setelem_parse_ctx);
1576 }
1577 7122648 list_splice_tail_init(&setelem_parse_ctx.stmt_list, &expr->stmt_list);
1578
1579
2/2
✓ Branch 0 (56→57) taken 3418563 times.
✓ Branch 1 (56→60) taken 3704085 times.
7122648 if (flags & NFT_SET_ELEM_INTERVAL_END) {
1580 3418563 expr->flags |= EXPR_F_INTERVAL_END;
1581
3/4
✓ Branch 0 (57→58) taken 3418563 times.
✗ Branch 1 (57→60) not taken.
✓ Branch 2 (58→59) taken 3417904 times.
✓ Branch 3 (58→60) taken 659 times.
3418563 if (mpz_cmp_ui(set->key->value, 0) == 0)
1582 3417904 set->root = true;
1583 }
1584
1585
2/2
✓ Branch 0 (61→62) taken 51580 times.
✓ Branch 1 (61→89) taken 7071068 times.
7122648 if (set_is_datamap(set->flags)) {
1586
2/2
✓ Branch 0 (63→64) taken 21436 times.
✓ Branch 1 (63→66) taken 30144 times.
51580 if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_DATA)) {
1587 21436 nld.value = nftnl_set_elem_get(nlse, NFTNL_SET_ELEM_DATA,
1588 &nld.len);
1589
2/2
✓ Branch 0 (67→68) taken 377 times.
✓ Branch 1 (67→71) taken 29767 times.
30144 } else if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_CHAIN)) {
1590 377 nld.chain = nftnl_set_elem_get_str(nlse, NFTNL_SET_ELEM_CHAIN);
1591 377 nld.verdict = nftnl_set_elem_get_u32(nlse, NFTNL_SET_ELEM_VERDICT);
1592
2/2
✓ Branch 0 (72→73) taken 7006 times.
✓ Branch 1 (72→75) taken 22761 times.
29767 } else if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_VERDICT)) {
1593 7006 nld.verdict = nftnl_set_elem_get_u32(nlse, NFTNL_SET_ELEM_VERDICT);
1594 } else
1595 22761 goto out;
1596
1597 28819 data = netlink_alloc_data(&netlink_location, &nld,
1598 28819 set->data->dtype->type == TYPE_VERDICT ?
1599 NFT_REG_VERDICT : NFT_REG_1);
1600
1601
2/2
✓ Branch 0 (77→78) taken 4 times.
✓ Branch 1 (77→79) taken 28815 times.
28819 if (set->data->dtype->is_typeof)
1602 4 datatype_set(data, set->data->dtype->basetype);
1603 else
1604 28815 datatype_set(data, set->data->dtype);
1605 28819 data->byteorder = set->data->byteorder;
1606
1607
2/2
✓ Branch 0 (80→81) taken 73 times.
✓ Branch 1 (80→82) taken 28746 times.
28819 if (set->data->dtype->subtypes) {
1608 73 data = netlink_parse_concat_elem(set, data);
1609
2/2
✓ Branch 0 (82→83) taken 56 times.
✓ Branch 1 (82→84) taken 28690 times.
28746 } else if (set->data->flags & EXPR_F_INTERVAL)
1610 56 data = netlink_parse_interval_elem(set, data);
1611
1612
2/2
✓ Branch 0 (84→85) taken 2270 times.
✓ Branch 1 (84→88) taken 26549 times.
28819 if (data->byteorder == BYTEORDER_HOST_ENDIAN)
1613
1/2
✗ Branch 0 (85→86) not taken.
✓ Branch 1 (85→87) taken 2270 times.
2270 mpz_switch_byteorder(data->value, data->len / BITS_PER_BYTE);
1614
1615 28819 expr = mapping_expr_alloc(&netlink_location, expr, data);
1616 }
1617
2/2
✓ Branch 0 (90→91) taken 7099536 times.
✓ Branch 1 (90→92) taken 351 times.
7099887 if (set_is_objmap(set->flags)) {
1618
2/2
✓ Branch 0 (93→94) taken 212 times.
✓ Branch 1 (93→96) taken 139 times.
351 if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_OBJREF)) {
1619 212 nld.value = nftnl_set_elem_get(nlse,
1620 NFTNL_SET_ELEM_OBJREF,
1621 &nld.len);
1622 } else
1623 139 goto out;
1624
1625 212 data = netlink_alloc_value(&netlink_location, &nld);
1626 212 data->dtype = &string_type;
1627 212 data->byteorder = BYTEORDER_HOST_ENDIAN;
1628
1/2
✗ Branch 0 (98→99) not taken.
✓ Branch 1 (98→100) taken 212 times.
212 mpz_switch_byteorder(data->value, data->len / BITS_PER_BYTE);
1629 212 expr = mapping_expr_alloc(&netlink_location, expr, data);
1630 }
1631 7099536 out:
1632 7122648 set_expr_add(set->init, expr);
1633
1634
4/4
✓ Branch 0 (103→104) taken 3704085 times.
✓ Branch 1 (103→108) taken 3418563 times.
✓ Branch 2 (105→106) taken 7387 times.
✓ Branch 3 (105→108) taken 3696698 times.
10826733 if (!(flags & NFT_SET_ELEM_INTERVAL_END) &&
1635 3704085 nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_KEY_END)) {
1636 7387 flags |= NFT_SET_ELEM_INTERVAL_END;
1637 7387 nld.value = nftnl_set_elem_get(nlse, NFTNL_SET_ELEM_KEY_END,
1638 &nld.len);
1639 7387 goto key_end;
1640 }
1641
1642 7115261 return 0;
1643 }
1644
1645 7115184 static int list_setelem_cb(struct nftnl_set_elem *nlse, void *arg)
1646 {
1647 7115184 struct netlink_ctx *ctx = arg;
1648 7115184 return netlink_delinearize_setelem(ctx, nlse, ctx->set);
1649 }
1650
1651 788 static int list_setelem_debug_cb(struct nftnl_set_elem *nlse, void *arg)
1652 {
1653 int r;
1654
1655 788 r = list_setelem_cb(nlse, arg);
1656
1/2
✓ Branch 0 (3→4) taken 788 times.
✗ Branch 1 (3→7) not taken.
788 if (r == 0) {
1657 788 struct netlink_ctx *ctx = arg;
1658 788 FILE *fp = ctx->nft->output.output_fp;
1659
1660 788 fprintf(fp, "\t");
1661 788 nftnl_set_elem_fprintf(fp, nlse, 0, 0);
1662 788 fprintf(fp, "\n");
1663 }
1664
1665 788 return r;
1666 }
1667
1668 14239 static int list_setelements(struct nftnl_set *s, struct netlink_ctx *ctx)
1669 {
1670 14239 FILE *fp = ctx->nft->output.output_fp;
1671
1672
3/4
✓ Branch 0 (2→3) taken 14239 times.
✗ Branch 1 (2→11) not taken.
✓ Branch 2 (3→4) taken 16 times.
✓ Branch 3 (3→11) taken 14223 times.
14239 if (fp && (ctx->nft->debug_mask & NFT_DEBUG_NETLINK)) {
1673 const char *table, *name;
1674 16 uint32_t family = nftnl_set_get_u32(s, NFTNL_SET_FAMILY);
1675
1676 16 table = nftnl_set_get_str(s, NFTNL_SET_TABLE);
1677 16 name = nftnl_set_get_str(s, NFTNL_SET_NAME);
1678
1679 16 fprintf(fp, "%s %s @%s\n", family2str(family), table, name);
1680
1681 16 return nftnl_set_elem_foreach(s, list_setelem_debug_cb, ctx);
1682 }
1683
1684 14223 return nftnl_set_elem_foreach(s, list_setelem_cb, ctx);
1685 }
1686
1687 12373 int netlink_list_setelems(struct netlink_ctx *ctx, const struct handle *h,
1688 struct set *set, bool reset)
1689 {
1690 struct nftnl_set *nls;
1691 int err;
1692
1693 12373 nls = nftnl_set_alloc();
1694
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 12373 times.
12373 if (nls == NULL)
1695 ✗ memory_allocation_error();
1696
1697 12373 nftnl_set_set_u32(nls, NFTNL_SET_FAMILY, h->family);
1698 12373 nftnl_set_set_str(nls, NFTNL_SET_TABLE, h->table.name);
1699 12373 nftnl_set_set_str(nls, NFTNL_SET_NAME, h->set.name);
1700
1/2
✓ Branch 0 (8→9) taken 12373 times.
✗ Branch 1 (8→10) not taken.
12373 if (h->handle.id)
1701 12373 nftnl_set_set_u64(nls, NFTNL_SET_HANDLE, h->handle.id);
1702
1703 12373 err = mnl_nft_setelem_get(ctx, nls, reset);
1704
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→16) taken 12373 times.
12373 if (err < 0) {
1705 ✗ nftnl_set_free(nls);
1706 ✗ if (errno == EINTR)
1707 ✗ return -1;
1708
1709 ✗ return 0;
1710 }
1711
1712 12373 ctx->set = set;
1713 12373 set->init = set_expr_alloc(&internal_location, set);
1714 12373 list_setelements(nls, ctx);
1715
1716
4/4
✓ Branch 0 (18→19) taken 5059 times.
✓ Branch 1 (18→21) taken 7314 times.
✓ Branch 2 (19→20) taken 954 times.
✓ Branch 3 (19→21) taken 4105 times.
12373 if (set->flags & NFT_SET_INTERVAL && set->desc.field_count > 1)
1717 954 concat_range_aggregate(set->init);
1718
2/2
✓ Branch 0 (21→22) taken 4105 times.
✓ Branch 1 (21→23) taken 7314 times.
11419 else if (set->flags & NFT_SET_INTERVAL)
1719 4105 interval_map_decompose(set->init);
1720 else
1721
1/2
✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→25) taken 7314 times.
7314 list_expr_sort(&expr_set(ctx->set->init)->expressions);
1722
1723 12373 nftnl_set_free(nls);
1724 12373 ctx->set = NULL;
1725
1726 12373 return 0;
1727 }
1728
1729 1885 int netlink_get_setelem(struct netlink_ctx *ctx, const struct handle *h,
1730 const struct location *loc, struct set *cache_set,
1731 struct set *set, struct expr *init, bool reset)
1732 {
1733 1885 struct nftnl_set *nls, *nls_out = NULL;
1734 1885 int err = 0;
1735
1736 1885 nls = nftnl_set_alloc();
1737
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 1885 times.
1885 if (nls == NULL)
1738 ✗ memory_allocation_error();
1739
1740 1885 nftnl_set_set_u32(nls, NFTNL_SET_FAMILY, h->family);
1741 1885 nftnl_set_set_str(nls, NFTNL_SET_TABLE, h->table.name);
1742 1885 nftnl_set_set_str(nls, NFTNL_SET_NAME, h->set.name);
1743
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 1885 times.
1885 if (h->handle.id)
1744 ✗ nftnl_set_set_u64(nls, NFTNL_SET_HANDLE, h->handle.id);
1745
1746 1885 alloc_setelem_cache(init, nls);
1747
1748 1885 netlink_dump_set(nls, ctx);
1749
1750 1885 nls_out = mnl_nft_setelem_get_one(ctx, nls, reset);
1751
2/2
✓ Branch 0 (13→14) taken 19 times.
✓ Branch 1 (13→16) taken 1866 times.
1885 if (!nls_out) {
1752 19 nftnl_set_free(nls);
1753 19 return -1;
1754 }
1755
1756 1866 ctx->set = set;
1757 1866 set->init = set_expr_alloc(loc, set);
1758 1866 list_setelements(nls_out, ctx);
1759
1760
4/4
✓ Branch 0 (18→19) taken 916 times.
✓ Branch 1 (18→21) taken 950 times.
✓ Branch 2 (19→20) taken 201 times.
✓ Branch 3 (19→21) taken 715 times.
1866 if (set->flags & NFT_SET_INTERVAL && set->desc.field_count > 1)
1761 201 concat_range_aggregate(set->init);
1762
2/2
✓ Branch 0 (21→22) taken 715 times.
✓ Branch 1 (21→23) taken 950 times.
1665 else if (set->flags & NFT_SET_INTERVAL)
1763 715 err = get_set_decompose(cache_set, set);
1764 else
1765
1/2
✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→25) taken 950 times.
950 list_expr_sort(&expr_set(ctx->set->init)->expressions);
1766
1767 1866 nftnl_set_free(nls);
1768 1866 nftnl_set_free(nls_out);
1769 1866 ctx->set = NULL;
1770
1771 1866 return err;
1772 }
1773
1774 457 void netlink_dump_obj(struct nftnl_obj *nln, struct netlink_ctx *ctx)
1775 {
1776 457 FILE *fp = ctx->nft->output.output_fp;
1777
1778
1/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 457 times.
✗ Branch 2 (3→4) not taken.
✗ Branch 3 (3→5) not taken.
457 if (!(ctx->nft->debug_mask & NFT_DEBUG_NETLINK) || !fp)
1779 457 return;
1780
1781 ✗ nftnl_obj_fprintf(fp, nln, 0, 0);
1782 ✗ fprintf(fp, "\n");
1783 }
1784
1785 static struct in6_addr all_zeroes;
1786
1787 static struct expr *
1788 16 netlink_obj_tunnel_parse_addr(struct nftnl_obj *nlo, int attr)
1789 {
1790 struct nft_data_delinearize nld;
1791 const struct datatype *dtype;
1792 const uint32_t *addr6;
1793 struct expr *expr;
1794 uint32_t addr;
1795
1796 16 memset(&nld, 0, sizeof(nld));
1797
1798
1/3
✓ Branch 0 (2→3) taken 16 times.
✗ Branch 1 (2→7) not taken.
✗ Branch 2 (2→11) not taken.
16 switch (attr) {
1799 16 case NFTNL_OBJ_TUNNEL_IPV4_SRC:
1800 case NFTNL_OBJ_TUNNEL_IPV4_DST:
1801 16 addr = nftnl_obj_get_u32(nlo, attr);
1802
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 16 times.
16 if (!addr)
1803 ✗ return NULL;
1804
1805 16 dtype = &ipaddr_type;
1806 16 nld.value = &addr;
1807 16 nld.len = sizeof(struct in_addr);
1808 16 break;
1809 ✗ case NFTNL_OBJ_TUNNEL_IPV6_SRC:
1810 case NFTNL_OBJ_TUNNEL_IPV6_DST:
1811 ✗ addr6 = nftnl_obj_get(nlo, attr);
1812 ✗ if (!memcmp(addr6, &all_zeroes, sizeof(all_zeroes)))
1813 ✗ return NULL;
1814
1815 ✗ dtype = &ip6addr_type;
1816 ✗ nld.value = addr6;
1817 ✗ nld.len = sizeof(struct in6_addr);
1818 ✗ break;
1819 ✗ default:
1820 ✗ return NULL;
1821 }
1822
1823 16 expr = netlink_alloc_value(&netlink_location, &nld);
1824 16 expr->dtype = dtype;
1825 16 expr->byteorder = BYTEORDER_BIG_ENDIAN;
1826
1827 16 return expr;
1828 }
1829
1830 16 static int obj_parse_udata_cb(const struct nftnl_udata *attr, void *data)
1831 {
1832 16 unsigned char *value = nftnl_udata_get(attr);
1833 16 uint8_t type = nftnl_udata_type(attr);
1834 16 const struct nftnl_udata **tb = data;
1835 16 uint8_t len = nftnl_udata_len(attr);
1836
1837
1/2
✓ Branch 0 (5→6) taken 16 times.
✗ Branch 1 (5→9) not taken.
16 switch (type) {
1838 16 case NFTNL_UDATA_OBJ_COMMENT:
1839
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 16 times.
16 if (value[len - 1] != '\0')
1840 ✗ return -1;
1841 16 break;
1842 ✗ default:
1843 ✗ return 0;
1844 }
1845 16 tb[type] = attr;
1846 16 return 0;
1847 }
1848
1849 12 static int tunnel_parse_opt_cb(struct nftnl_tunnel_opt *opt, void *data) {
1850
1851 12 struct obj *obj = data;
1852
1853
3/4
✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→21) taken 2 times.
✓ Branch 2 (3→26) taken 6 times.
✗ Branch 3 (3→48) not taken.
12 switch (nftnl_tunnel_opt_get_type(opt)) {
1854 4 case NFTNL_TUNNEL_TYPE_ERSPAN:
1855 4 obj->tunnel.type = TUNNEL_ERSPAN;
1856
1/2
✓ Branch 0 (5→6) taken 4 times.
✗ Branch 1 (5→8) not taken.
4 if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_ERSPAN_VERSION)) {
1857 4 obj->tunnel.erspan.version =
1858 4 nftnl_tunnel_opt_get_u32(opt,
1859 NFTNL_TUNNEL_ERSPAN_VERSION);
1860 }
1861
2/2
✓ Branch 0 (9→10) taken 2 times.
✓ Branch 1 (9→12) taken 2 times.
4 if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_ERSPAN_V1_INDEX)) {
1862 2 obj->tunnel.erspan.v1.index =
1863 2 nftnl_tunnel_opt_get_u32(opt,
1864 NFTNL_TUNNEL_ERSPAN_V1_INDEX);
1865 }
1866
2/2
✓ Branch 0 (13→14) taken 2 times.
✓ Branch 1 (13→16) taken 2 times.
4 if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_ERSPAN_V2_HWID)) {
1867 2 obj->tunnel.erspan.v2.hwid =
1868 2 nftnl_tunnel_opt_get_u8(opt,
1869 NFTNL_TUNNEL_ERSPAN_V2_HWID);
1870 }
1871
2/2
✓ Branch 0 (17→18) taken 2 times.
✓ Branch 1 (17→20) taken 2 times.
4 if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_ERSPAN_V2_DIR)) {
1872 2 obj->tunnel.erspan.v2.direction =
1873 2 nftnl_tunnel_opt_get_u8(opt,
1874 NFTNL_TUNNEL_ERSPAN_V2_DIR);
1875 }
1876 4 break;
1877 2 case NFTNL_TUNNEL_TYPE_VXLAN:
1878 2 obj->tunnel.type = TUNNEL_VXLAN;
1879
1/2
✓ Branch 0 (22→23) taken 2 times.
✗ Branch 1 (22→25) not taken.
2 if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_VXLAN_GBP)) {
1880 2 obj->tunnel.type = TUNNEL_VXLAN;
1881 2 obj->tunnel.vxlan.gbp = nftnl_tunnel_opt_get_u32(opt, NFTNL_TUNNEL_VXLAN_GBP);
1882 }
1883 2 break;
1884 6 case NFTNL_TUNNEL_TYPE_GENEVE:
1885 struct tunnel_geneve *geneve;
1886 const void *data;
1887
1888
2/2
✓ Branch 0 (26→27) taken 2 times.
✓ Branch 1 (26→29) taken 4 times.
6 if (!obj->tunnel.type) {
1889 2 init_list_head(&obj->tunnel.geneve_opts);
1890 2 obj->tunnel.type = TUNNEL_GENEVE;
1891 }
1892
1893 6 geneve = xmalloc(sizeof(struct tunnel_geneve));
1894
1/2
✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→32) taken 6 times.
6 if (!geneve)
1895 ✗ memory_allocation_error();
1896
1897
1/2
✓ Branch 0 (33→34) taken 6 times.
✗ Branch 1 (33→36) not taken.
6 if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_GENEVE_TYPE))
1898 6 geneve->type = nftnl_tunnel_opt_get_u8(opt, NFTNL_TUNNEL_GENEVE_TYPE);
1899
1900
1/2
✓ Branch 0 (37→38) taken 6 times.
✗ Branch 1 (37→40) not taken.
6 if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_GENEVE_CLASS))
1901 6 geneve->geneve_class = nftnl_tunnel_opt_get_u16(opt, NFTNL_TUNNEL_GENEVE_CLASS);
1902
1903
1/2
✓ Branch 0 (41→42) taken 6 times.
✗ Branch 1 (41→46) not taken.
6 if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_GENEVE_DATA)) {
1904 6 data = nftnl_tunnel_opt_get_data(opt, NFTNL_TUNNEL_GENEVE_DATA,
1905 &geneve->data_len);
1906
1/2
✗ Branch 0 (43→44) not taken.
✓ Branch 1 (43→45) taken 6 times.
6 if (!data)
1907 ✗ return -1;
1908 6 memcpy(&geneve->data, data, geneve->data_len);
1909 }
1910
1911 6 list_add_tail(&geneve->list, &obj->tunnel.geneve_opts);
1912 6 break;
1913 ✗ default:
1914 ✗ break;
1915 }
1916
1917 12 return 0;
1918 }
1919
1920 2508 struct obj *netlink_delinearize_obj(struct netlink_ctx *ctx,
1921 struct nftnl_obj *nlo)
1922 {
1923 2508 const struct nftnl_udata *ud[NFTNL_UDATA_OBJ_MAX + 1] = {};
1924 const char *udata;
1925 struct obj *obj;
1926 uint32_t type;
1927 uint32_t ulen;
1928
1929 2508 obj = obj_alloc(&netlink_location);
1930 2508 obj->handle.family = nftnl_obj_get_u32(nlo, NFTNL_OBJ_FAMILY);
1931 2508 obj->handle.table.name =
1932 2508 xstrdup(nftnl_obj_get_str(nlo, NFTNL_OBJ_TABLE));
1933 2508 obj->handle.obj.name =
1934 2508 xstrdup(nftnl_obj_get_str(nlo, NFTNL_OBJ_NAME));
1935 2508 obj->handle.handle.id =
1936 2508 nftnl_obj_get_u64(nlo, NFTNL_OBJ_HANDLE);
1937
2/2
✓ Branch 0 (10→11) taken 16 times.
✓ Branch 1 (10→21) taken 2492 times.
2508 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_USERDATA)) {
1938 16 udata = nftnl_obj_get_data(nlo, NFTNL_OBJ_USERDATA, &ulen);
1939
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→17) taken 16 times.
16 if (nftnl_udata_parse(udata, ulen, obj_parse_udata_cb, ud) < 0) {
1940 ✗ netlink_io_error(ctx, NULL, "Cannot parse userdata");
1941 ✗ obj_free(obj);
1942 ✗ return NULL;
1943 }
1944
1/2
✓ Branch 0 (17→18) taken 16 times.
✗ Branch 1 (17→21) not taken.
16 if (ud[NFTNL_UDATA_OBJ_COMMENT])
1945 16 obj->comment = xstrdup(nftnl_udata_get(ud[NFTNL_UDATA_OBJ_COMMENT]));
1946 }
1947
1948 2508 type = nftnl_obj_get_u32(nlo, NFTNL_OBJ_TYPE);
1949
9/10
✓ Branch 0 (22→23) taken 2228 times.
✓ Branch 1 (22→26) taken 101 times.
✓ Branch 2 (22→30) taken 2 times.
✓ Branch 3 (22→32) taken 32 times.
✓ Branch 4 (22→36) taken 38 times.
✓ Branch 5 (22→44) taken 50 times.
✓ Branch 6 (22→50) taken 26 times.
✓ Branch 7 (22→56) taken 23 times.
✓ Branch 8 (22→60) taken 8 times.
✗ Branch 9 (22→100) not taken.
2508 switch (type) {
1950 2228 case NFT_OBJECT_COUNTER:
1951 2228 obj->counter.packets =
1952 2228 nftnl_obj_get_u64(nlo, NFTNL_OBJ_CTR_PKTS);
1953 2228 obj->counter.bytes =
1954 2228 nftnl_obj_get_u64(nlo, NFTNL_OBJ_CTR_BYTES);
1955 2228 break;
1956 101 case NFT_OBJECT_QUOTA:
1957 101 obj->quota.bytes =
1958 101 nftnl_obj_get_u64(nlo, NFTNL_OBJ_QUOTA_BYTES);
1959 101 obj->quota.used =
1960 101 nftnl_obj_get_u64(nlo, NFTNL_OBJ_QUOTA_CONSUMED);
1961 101 obj->quota.flags =
1962 101 nftnl_obj_get_u32(nlo, NFTNL_OBJ_QUOTA_FLAGS);
1963 101 break;
1964 2 case NFT_OBJECT_SECMARK:
1965 2 snprintf(obj->secmark.ctx, sizeof(obj->secmark.ctx), "%s",
1966 nftnl_obj_get_str(nlo, NFTNL_OBJ_SECMARK_CTX));
1967 2 break;
1968 32 case NFT_OBJECT_CT_HELPER:
1969 32 snprintf(obj->ct_helper.name, sizeof(obj->ct_helper.name), "%s",
1970 nftnl_obj_get_str(nlo, NFTNL_OBJ_CT_HELPER_NAME));
1971 32 obj->ct_helper.l3proto = nftnl_obj_get_u16(nlo, NFTNL_OBJ_CT_HELPER_L3PROTO);
1972 32 obj->ct_helper.l4proto = nftnl_obj_get_u8(nlo, NFTNL_OBJ_CT_HELPER_L4PROTO);
1973 32 break;
1974 38 case NFT_OBJECT_CT_TIMEOUT:
1975 38 init_list_head(&obj->ct_timeout.timeout_list);
1976 38 obj->ct_timeout.l3proto = nftnl_obj_get_u16(nlo, NFTNL_OBJ_CT_TIMEOUT_L3PROTO);
1977 38 obj->ct_timeout.l4proto = nftnl_obj_get_u8(nlo, NFTNL_OBJ_CT_TIMEOUT_L4PROTO);
1978
1/2
✓ Branch 0 (40→41) taken 38 times.
✗ Branch 1 (40→43) not taken.
38 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_CT_TIMEOUT_ARRAY))
1979 38 memcpy(obj->ct_timeout.timeout,
1980 38 nftnl_obj_get(nlo, NFTNL_OBJ_CT_TIMEOUT_ARRAY),
1981 NFTNL_CTTIMEOUT_ARRAY_MAX * sizeof(uint32_t));
1982 38 break;
1983 50 case NFT_OBJECT_LIMIT:
1984 50 obj->limit.rate =
1985 50 nftnl_obj_get_u64(nlo, NFTNL_OBJ_LIMIT_RATE);
1986 50 obj->limit.unit =
1987 50 nftnl_obj_get_u64(nlo, NFTNL_OBJ_LIMIT_UNIT);
1988 50 obj->limit.burst =
1989 50 nftnl_obj_get_u32(nlo, NFTNL_OBJ_LIMIT_BURST);
1990 50 obj->limit.type =
1991 50 nftnl_obj_get_u32(nlo, NFTNL_OBJ_LIMIT_TYPE);
1992 50 obj->limit.flags =
1993 50 nftnl_obj_get_u32(nlo, NFTNL_OBJ_LIMIT_FLAGS);
1994 50 break;
1995 26 case NFT_OBJECT_CT_EXPECT:
1996 26 obj->ct_expect.l3proto =
1997 26 nftnl_obj_get_u16(nlo, NFTNL_OBJ_CT_EXPECT_L3PROTO);
1998 26 obj->ct_expect.l4proto =
1999 26 nftnl_obj_get_u8(nlo, NFTNL_OBJ_CT_EXPECT_L4PROTO);
2000 26 obj->ct_expect.dport =
2001 26 nftnl_obj_get_u16(nlo, NFTNL_OBJ_CT_EXPECT_DPORT);
2002 26 obj->ct_expect.timeout =
2003 26 nftnl_obj_get_u32(nlo, NFTNL_OBJ_CT_EXPECT_TIMEOUT);
2004 26 obj->ct_expect.size =
2005 26 nftnl_obj_get_u8(nlo, NFTNL_OBJ_CT_EXPECT_SIZE);
2006 26 break;
2007 23 case NFT_OBJECT_SYNPROXY:
2008 23 obj->synproxy.mss =
2009 23 nftnl_obj_get_u16(nlo, NFTNL_OBJ_SYNPROXY_MSS);
2010 23 obj->synproxy.wscale =
2011 23 nftnl_obj_get_u8(nlo, NFTNL_OBJ_SYNPROXY_WSCALE);
2012 23 obj->synproxy.flags =
2013 23 nftnl_obj_get_u32(nlo, NFTNL_OBJ_SYNPROXY_FLAGS);
2014 23 break;
2015 8 case NFT_OBJECT_TUNNEL:
2016
1/2
✓ Branch 0 (61→62) taken 8 times.
✗ Branch 1 (61→64) not taken.
8 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_ID))
2017 8 obj->tunnel.id = nftnl_obj_get_u32(nlo, NFTNL_OBJ_TUNNEL_ID);
2018
1/2
✓ Branch 0 (65→66) taken 8 times.
✗ Branch 1 (65→68) not taken.
8 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_SPORT)) {
2019 8 obj->tunnel.sport =
2020 8 nftnl_obj_get_u16(nlo, NFTNL_OBJ_TUNNEL_SPORT);
2021 }
2022
1/2
✓ Branch 0 (69→70) taken 8 times.
✗ Branch 1 (69→72) not taken.
8 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_DPORT)) {
2023 8 obj->tunnel.dport =
2024 8 nftnl_obj_get_u16(nlo, NFTNL_OBJ_TUNNEL_DPORT);
2025 }
2026
1/2
✓ Branch 0 (73→74) taken 8 times.
✗ Branch 1 (73→76) not taken.
8 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_TOS)) {
2027 8 obj->tunnel.tos =
2028 8 nftnl_obj_get_u8(nlo, NFTNL_OBJ_TUNNEL_TOS);
2029 }
2030
1/2
✓ Branch 0 (77→78) taken 8 times.
✗ Branch 1 (77→80) not taken.
8 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_TTL)) {
2031 8 obj->tunnel.ttl =
2032 8 nftnl_obj_get_u8(nlo, NFTNL_OBJ_TUNNEL_TTL);
2033 }
2034
1/2
✓ Branch 0 (81→82) taken 8 times.
✗ Branch 1 (81→84) not taken.
8 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_IPV4_SRC)) {
2035 8 obj->tunnel.src =
2036 8 netlink_obj_tunnel_parse_addr(nlo, NFTNL_OBJ_TUNNEL_IPV4_SRC);
2037 }
2038
1/2
✓ Branch 0 (85→86) taken 8 times.
✗ Branch 1 (85→88) not taken.
8 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_IPV4_DST)) {
2039 8 obj->tunnel.dst =
2040 8 netlink_obj_tunnel_parse_addr(nlo, NFTNL_OBJ_TUNNEL_IPV4_DST);
2041 }
2042
1/2
✗ Branch 0 (89→90) not taken.
✓ Branch 1 (89→92) taken 8 times.
8 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_IPV6_SRC)) {
2043 ✗ obj->tunnel.src =
2044 ✗ netlink_obj_tunnel_parse_addr(nlo, NFTNL_OBJ_TUNNEL_IPV6_SRC);
2045 }
2046
1/2
✗ Branch 0 (93→94) not taken.
✓ Branch 1 (93→96) taken 8 times.
8 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_IPV6_DST)) {
2047 ✗ obj->tunnel.dst =
2048 ✗ netlink_obj_tunnel_parse_addr(nlo, NFTNL_OBJ_TUNNEL_IPV6_DST);
2049 }
2050
1/2
✓ Branch 0 (97→98) taken 8 times.
✗ Branch 1 (97→99) not taken.
8 if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_OPTS)) {
2051 8 nftnl_obj_tunnel_opts_foreach(nlo, tunnel_parse_opt_cb, obj);
2052 }
2053 8 break;
2054 ✗ default:
2055 ✗ netlink_io_error(ctx, NULL, "Unknown object type %u", type);
2056 ✗ obj_free(obj);
2057 ✗ return NULL;
2058 }
2059 2508 obj->type = type;
2060
2061 2508 return obj;
2062 }
2063
2064 93 void netlink_dump_flowtable(struct nftnl_flowtable *flo,
2065 struct netlink_ctx *ctx)
2066 {
2067 93 FILE *fp = ctx->nft->output.output_fp;
2068
2069
1/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 93 times.
✗ Branch 2 (3→4) not taken.
✗ Branch 3 (3→5) not taken.
93 if (!(ctx->nft->debug_mask & NFT_DEBUG_NETLINK) || !fp)
2070 93 return;
2071
2072 ✗ nftnl_flowtable_fprintf(fp, flo, 0, 0);
2073 ✗ fprintf(fp, "\n");
2074 }
2075
2076 struct flowtable *
2077 51 netlink_delinearize_flowtable(struct netlink_ctx *ctx,
2078 struct nftnl_flowtable *nlo)
2079 {
2080 struct flowtable *flowtable;
2081 const char * const *dev_array;
2082 51 int len = 0, i, priority;
2083
2084 51 flowtable = flowtable_alloc(&netlink_location);
2085 51 flowtable->handle.family =
2086 51 nftnl_flowtable_get_u32(nlo, NFTNL_FLOWTABLE_FAMILY);
2087 51 flowtable->handle.table.name =
2088 51 xstrdup(nftnl_flowtable_get_str(nlo, NFTNL_FLOWTABLE_TABLE));
2089 51 flowtable->handle.flowtable.name =
2090 51 xstrdup(nftnl_flowtable_get_str(nlo, NFTNL_FLOWTABLE_NAME));
2091 51 flowtable->handle.handle.id =
2092 51 nftnl_flowtable_get_u64(nlo, NFTNL_FLOWTABLE_HANDLE);
2093
1/2
✓ Branch 0 (10→11) taken 51 times.
✗ Branch 1 (10→13) not taken.
51 if (nftnl_flowtable_is_set(nlo, NFTNL_FLOWTABLE_FLAGS))
2094 51 flowtable->flags = nftnl_flowtable_get_u32(nlo, NFTNL_FLOWTABLE_FLAGS);
2095 51 dev_array = nftnl_flowtable_get(nlo, NFTNL_FLOWTABLE_DEVICES);
2096
3/4
✓ Branch 0 (16→17) taken 109 times.
✗ Branch 1 (16→18) not taken.
✓ Branch 2 (17→15) taken 58 times.
✓ Branch 3 (17→18) taken 51 times.
109 while (dev_array && dev_array[len])
2097 58 len++;
2098
2099
2/2
✓ Branch 0 (18→19) taken 47 times.
✓ Branch 1 (18→21) taken 4 times.
51 if (len)
2100 47 flowtable->dev_array = xmalloc(len * sizeof(char *));
2101
2/2
✓ Branch 0 (24→22) taken 58 times.
✓ Branch 1 (24→25) taken 51 times.
109 for (i = 0; i < len; i++)
2102 58 flowtable->dev_array[i] = xstrdup(dev_array[i]);
2103
2104 51 flowtable->dev_array_len = len;
2105
2106
2/2
✓ Branch 0 (25→26) taken 47 times.
✓ Branch 1 (25→27) taken 4 times.
51 if (flowtable->dev_array_len) {
2107 47 qsort(flowtable->dev_array, flowtable->dev_array_len,
2108 sizeof(char *), qsort_device_cmp);
2109 }
2110
2111
1/2
✓ Branch 0 (28→29) taken 51 times.
✗ Branch 1 (28→32) not taken.
51 if (nftnl_flowtable_is_set(nlo, NFTNL_FLOWTABLE_PRIO)) {
2112 51 priority = nftnl_flowtable_get_u32(nlo, NFTNL_FLOWTABLE_PRIO);
2113 51 flowtable->priority.expr =
2114 51 constant_expr_alloc(&netlink_location,
2115 &integer_type,
2116 BYTEORDER_HOST_ENDIAN,
2117 sizeof(int) *
2118 BITS_PER_BYTE,
2119 &priority);
2120 }
2121 51 flowtable->hook.num =
2122 51 nftnl_flowtable_get_u32(nlo, NFTNL_FLOWTABLE_HOOKNUM);
2123 51 flowtable->flags =
2124 51 nftnl_flowtable_get_u32(nlo, NFTNL_FLOWTABLE_FLAGS);
2125
2126 51 return flowtable;
2127 }
2128
2129 ✗ static int list_flowtable_cb(struct nftnl_flowtable *nls, void *arg)
2130 {
2131 ✗ struct netlink_ctx *ctx = arg;
2132 struct flowtable *flowtable;
2133
2134 ✗ flowtable = netlink_delinearize_flowtable(ctx, nls);
2135 ✗ if (flowtable == NULL)
2136 ✗ return -1;
2137 ✗ list_add_tail(&flowtable->list, &ctx->list);
2138 ✗ return 0;
2139 }
2140
2141 ✗ int netlink_list_flowtables(struct netlink_ctx *ctx, const struct handle *h)
2142 {
2143 struct nftnl_flowtable_list *flowtable_cache;
2144 int err;
2145
2146 ✗ flowtable_cache = mnl_nft_flowtable_dump(ctx, h->family,
2147 ✗ h->table.name, NULL);
2148 ✗ if (flowtable_cache == NULL) {
2149 ✗ if (errno == EINTR)
2150 ✗ return -1;
2151
2152 ✗ return 0;
2153 }
2154
2155 ✗ err = nftnl_flowtable_list_foreach(flowtable_cache, list_flowtable_cb, ctx);
2156 ✗ nftnl_flowtable_list_free(flowtable_cache);
2157 ✗ return err;
2158 }
2159