GCC Code Coverage Report


Directory: ./
File: src/monitor.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 432 610 70.8%
Functions: 29 37 78.4%
Branches: 157 262 59.9%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015 Arturo Borrero Gonzalez <arturo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 (or any
6 * later) as published by the Free Software Foundation.
7 */
8
9 #include <nft.h>
10
11 #include <fcntl.h>
12 #include <errno.h>
13 #include <libmnl/libmnl.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <inttypes.h>
17
18 #include <libnftnl/table.h>
19 #include <libnftnl/chain.h>
20 #include <libnftnl/expr.h>
21 #include <libnftnl/object.h>
22 #include <libnftnl/set.h>
23 #include <libnftnl/flowtable.h>
24 #include <libnftnl/udata.h>
25 #include <libnftnl/ruleset.h>
26 #include <libnftnl/common.h>
27 #include <linux/netfilter/nfnetlink.h>
28 #include <linux/netfilter/nf_tables.h>
29 #include <linux/netfilter.h>
30
31 #include <nftables.h>
32 #include <netlink.h>
33 #include <mnl.h>
34 #include <trace.h>
35 #include <expression.h>
36 #include <statement.h>
37 #include <gmputil.h>
38 #include <utils.h>
39 #include <erec.h>
40 #include <iface.h>
41 #include <json.h>
42
43 enum {
44 NFT_OF_EVENT_ADD,
45 NFT_OF_EVENT_INSERT,
46 NFT_OF_EVENT_DEL,
47 NFT_OF_EVENT_CREATE,
48 };
49
50 #define nft_mon_print(monh, ...) nft_print(&monh->ctx->nft->output, __VA_ARGS__)
51
52 36 struct nftnl_table *netlink_table_alloc(const struct nlmsghdr *nlh)
53 {
54 struct nftnl_table *nlt;
55
56 36 nlt = nftnl_table_alloc();
57
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 36 times.
36 if (nlt == NULL)
58 ✗ memory_allocation_error();
59
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 36 times.
36 if (nftnl_table_nlmsg_parse(nlh, nlt) < 0)
60 ✗ netlink_abi_error();
61
62 36 return nlt;
63 }
64
65 198 struct nftnl_chain *netlink_chain_alloc(const struct nlmsghdr *nlh)
66 {
67 struct nftnl_chain *nlc;
68
69 198 nlc = nftnl_chain_alloc();
70
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 198 times.
198 if (nlc == NULL)
71 ✗ memory_allocation_error();
72
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 198 times.
198 if (nftnl_chain_nlmsg_parse(nlh, nlc) < 0)
73 ✗ netlink_abi_error();
74
75 198 return nlc;
76 }
77
78 66 struct nftnl_set *netlink_set_alloc(const struct nlmsghdr *nlh)
79 {
80 struct nftnl_set *nls;
81
82 66 nls = nftnl_set_alloc();
83
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 66 times.
66 if (nls == NULL)
84 ✗ memory_allocation_error();
85
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 66 times.
66 if (nftnl_set_nlmsg_parse(nlh, nls) < 0)
86 ✗ netlink_abi_error();
87
88 66 return nls;
89 }
90
91 182 static struct nftnl_set *netlink_setelem_alloc(const struct nlmsghdr *nlh)
92 {
93 struct nftnl_set *nls;
94
95 182 nls = nftnl_set_alloc();
96
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 182 times.
182 if (nls == NULL)
97 ✗ memory_allocation_error();
98
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 182 times.
182 if (nftnl_set_elems_nlmsg_parse(nlh, nls) < 0)
99 ✗ netlink_abi_error();
100
101 182 return nls;
102 }
103
104 6219 struct nftnl_rule *netlink_rule_alloc(const struct nlmsghdr *nlh)
105 {
106 struct nftnl_rule *nlr;
107
108 6219 nlr = nftnl_rule_alloc();
109
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 6219 times.
6219 if (nlr == NULL)
110 ✗ memory_allocation_error();
111
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 6219 times.
6219 if (nftnl_rule_nlmsg_parse(nlh, nlr) < 0)
112 ✗ netlink_abi_error();
113
114 6219 return nlr;
115 }
116
117 16 struct nftnl_obj *netlink_obj_alloc(const struct nlmsghdr *nlh)
118 {
119 struct nftnl_obj *nlo;
120
121 16 nlo = nftnl_obj_alloc();
122
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 16 times.
16 if (nlo == NULL)
123 ✗ memory_allocation_error();
124
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 16 times.
16 if (nftnl_obj_nlmsg_parse(nlh, nlo) < 0)
125 ✗ netlink_abi_error();
126
127 16 return nlo;
128 }
129
130 4 struct nftnl_flowtable *netlink_flowtable_alloc(const struct nlmsghdr *nlh)
131 {
132 struct nftnl_flowtable *nlf;
133
134 4 nlf = nftnl_flowtable_alloc();
135
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 4 times.
4 if (nlf == NULL)
136 ✗ memory_allocation_error();
137
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 4 times.
4 if (nftnl_flowtable_nlmsg_parse(nlh, nlf) < 0)
138 ✗ netlink_abi_error();
139
140 4 return nlf;
141 }
142
143 3190 static uint32_t netlink_msg2nftnl_of(uint32_t type, uint16_t flags)
144 {
145
3/4
✓ Branch 0 (2→3) taken 3017 times.
✓ Branch 1 (2→6) taken 171 times.
✓ Branch 2 (2→9) taken 2 times.
✗ Branch 3 (2→10) not taken.
3190 switch (type) {
146 3017 case NFT_MSG_NEWRULE:
147
2/2
✓ Branch 0 (3→4) taken 3014 times.
✓ Branch 1 (3→5) taken 3 times.
3017 if (flags & NLM_F_APPEND)
148 3014 return NFT_OF_EVENT_ADD;
149 else
150 3 return NFT_OF_EVENT_INSERT;
151 171 case NFT_MSG_NEWTABLE:
152 case NFT_MSG_NEWCHAIN:
153 case NFT_MSG_NEWSET:
154 case NFT_MSG_NEWSETELEM:
155 case NFT_MSG_NEWOBJ:
156 case NFT_MSG_NEWFLOWTABLE:
157
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 171 times.
171 if (flags & NLM_F_EXCL)
158 ✗ return NFT_OF_EVENT_CREATE;
159 else
160 171 return NFT_OF_EVENT_ADD;
161 2 case NFT_MSG_DELTABLE:
162 case NFT_MSG_DELCHAIN:
163 case NFT_MSG_DELSET:
164 case NFT_MSG_DELSETELEM:
165 case NFT_MSG_DELRULE:
166 case NFT_MSG_DELOBJ:
167 case NFT_MSG_DELFLOWTABLE:
168 2 return NFTNL_OF_EVENT_DEL;
169 }
170
171 ✗ return 0;
172 }
173
174 3190 static const char *nftnl_of2cmd(uint32_t of)
175 {
176
3/5
✓ Branch 0 (2→3) taken 3185 times.
✗ Branch 1 (2→4) not taken.
✓ Branch 2 (2→5) taken 3 times.
✓ Branch 3 (2→6) taken 2 times.
✗ Branch 4 (2→7) not taken.
3190 switch (of) {
177 3185 case NFT_OF_EVENT_ADD:
178 3185 return "add";
179 ✗ case NFT_OF_EVENT_CREATE:
180 ✗ return "create";
181 3 case NFT_OF_EVENT_INSERT:
182 3 return "insert";
183 2 case NFT_OF_EVENT_DEL:
184 2 return "delete";
185 ✗ default:
186 ✗ return "???";
187 }
188 }
189
190 3190 static const char *netlink_msg2cmd(uint32_t type, uint16_t flags)
191 {
192 3190 return nftnl_of2cmd(netlink_msg2nftnl_of(type, flags));
193 }
194
195 3019 static void nlr_for_each_set(struct nftnl_rule *nlr,
196 void (*cb)(struct set *s, void *data),
197 void *data, struct nft_cache *cache)
198 {
199 struct nftnl_expr_iter *nlrei;
200 struct nftnl_expr *nlre;
201 const char *set_name, *table;
202 const char *name;
203 struct set *s;
204 uint32_t family;
205
206 3019 nlrei = nftnl_expr_iter_create(nlr);
207
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 3019 times.
3019 if (nlrei == NULL)
208 ✗ memory_allocation_error();
209
210 3019 family = nftnl_rule_get_u32(nlr, NFTNL_RULE_FAMILY);
211 3019 table = nftnl_rule_get_str(nlr, NFTNL_RULE_TABLE);
212
213 3019 nlre = nftnl_expr_iter_next(nlrei);
214
2/2
✓ Branch 0 (18→9) taken 3062 times.
✓ Branch 1 (18→19) taken 3019 times.
6081 while (nlre != NULL) {
215 3062 name = nftnl_expr_get_str(nlre, NFTNL_EXPR_NAME);
216
2/2
✓ Branch 0 (10→11) taken 3054 times.
✓ Branch 1 (10→12) taken 8 times.
3062 if (strcmp(name, "lookup") != 0)
217 3054 goto next;
218
219 8 set_name = nftnl_expr_get_str(nlre, NFTNL_EXPR_LOOKUP_SET);
220 8 s = set_lookup_global(family, table, set_name, cache);
221
1/2
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 8 times.
8 if (s == NULL)
222 ✗ goto next;
223
224 8 cb(s, data);
225 3062 next:
226 3062 nlre = nftnl_expr_iter_next(nlrei);
227 }
228 3019 nftnl_expr_iter_destroy(nlrei);
229 3019 }
230
231 30 static int netlink_events_table_cb(const struct nlmsghdr *nlh, int type,
232 struct netlink_mon_handler *monh)
233 {
234 struct nftnl_table *nlt;
235 struct table *t;
236 const char *cmd;
237
238 30 nlt = netlink_table_alloc(nlh);
239 30 t = netlink_delinearize_table(monh->ctx, nlt);
240
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 30 times.
30 if (!t) {
241 ✗ nftnl_table_free(nlt);
242 ✗ return MNL_CB_ERROR;
243 }
244 30 cmd = netlink_msg2cmd(type, nlh->nlmsg_flags);
245
246
2/3
✓ Branch 0 (8→9) taken 17 times.
✓ Branch 1 (8→19) taken 13 times.
✗ Branch 2 (8→24) not taken.
30 switch (monh->format) {
247 17 case NFTNL_OUTPUT_DEFAULT:
248 17 nft_mon_print(monh, "%s table ", cmd);
249
250 17 nft_mon_print(monh, "%s %s", family2str(t->handle.family),
251 t->handle.table.name);
252
253
2/2
✓ Branch 0 (12→13) taken 1 times.
✓ Branch 1 (12→14) taken 16 times.
17 if (t->flags & TABLE_F_DORMANT)
254 1 nft_mon_print(monh, " { flags dormant; }");
255
256
2/2
✓ Branch 0 (15→16) taken 1 times.
✓ Branch 1 (15→17) taken 16 times.
17 if (nft_output_handle(&monh->ctx->nft->output))
257 1 nft_mon_print(monh, " # handle %" PRIu64 "",
258 t->handle.handle.id);
259 17 nft_mon_print(monh, "\n");
260 17 break;
261 13 case NFTNL_OUTPUT_JSON:
262 13 monitor_print_table_json(monh, cmd, t);
263
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 13 times.
13 if (!nft_output_echo(&monh->ctx->nft->output))
264 ✗ nft_mon_print(monh, "\n");
265 13 break;
266 }
267 30 table_free(t);
268 30 nftnl_table_free(nlt);
269 30 return MNL_CB_OK;
270 }
271
272 15 static int netlink_events_chain_cb(const struct nlmsghdr *nlh, int type,
273 struct netlink_mon_handler *monh)
274 {
275 struct nftnl_chain *nlc;
276 struct chain *c;
277 const char *cmd;
278
279 15 nlc = netlink_chain_alloc(nlh);
280 15 c = netlink_delinearize_chain(monh->ctx, nlc);
281 15 cmd = netlink_msg2cmd(type, nlh->nlmsg_flags);
282
283
2/3
✓ Branch 0 (5→6) taken 8 times.
✓ Branch 1 (5→17) taken 7 times.
✗ Branch 2 (5→22) not taken.
15 switch (monh->format) {
284 8 case NFTNL_OUTPUT_DEFAULT:
285 8 nft_mon_print(monh, "%s ", cmd);
286
287
1/3
✓ Branch 0 (7→8) taken 8 times.
✗ Branch 1 (7→10) not taken.
✗ Branch 2 (7→15) not taken.
8 switch (type) {
288 8 case NFT_MSG_NEWCHAIN:
289 8 chain_print_plain(c, &monh->ctx->nft->output);
290 8 break;
291 ✗ case NFT_MSG_DELCHAIN:
292 ✗ if (c->dev_array_len > 0)
293 ✗ chain_print_plain(c, &monh->ctx->nft->output);
294 else
295 ✗ nft_mon_print(monh, "chain %s %s %s",
296 family2str(c->handle.family),
297 c->handle.table.name,
298 c->handle.chain.name);
299 ✗ break;
300 }
301 8 nft_mon_print(monh, "\n");
302 8 break;
303 7 case NFTNL_OUTPUT_JSON:
304 7 monitor_print_chain_json(monh, cmd, c);
305
1/2
✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→21) taken 7 times.
7 if (!nft_output_echo(&monh->ctx->nft->output))
306 ✗ nft_mon_print(monh, "\n");
307 7 break;
308 }
309 15 chain_free(c);
310 15 nftnl_chain_free(nlc);
311 15 return MNL_CB_OK;
312 }
313
314 25 static int netlink_events_set_cb(const struct nlmsghdr *nlh, int type,
315 struct netlink_mon_handler *monh)
316 {
317 struct nftnl_set *nls;
318 const char *family, *cmd;
319 struct set *set;
320 uint32_t flags;
321
322 25 nls = netlink_set_alloc(nlh);
323 25 flags = nftnl_set_get_u32(nls, NFTNL_SET_FLAGS);
324
2/2
✓ Branch 0 (5→6) taken 6 times.
✓ Branch 1 (5→7) taken 19 times.
25 if (set_is_anonymous(flags))
325 6 goto out;
326
327 19 set = netlink_delinearize_set(monh->ctx, nls);
328
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 19 times.
19 if (set == NULL) {
329 ✗ nftnl_set_free(nls);
330 ✗ return MNL_CB_ERROR;
331 }
332 19 family = family2str(set->handle.family);
333 19 cmd = netlink_msg2cmd(type, nlh->nlmsg_flags);
334
335
2/3
✓ Branch 0 (13→14) taken 10 times.
✓ Branch 1 (13→22) taken 9 times.
✗ Branch 2 (13→27) not taken.
19 switch (monh->format) {
336 10 case NFTNL_OUTPUT_DEFAULT:
337 10 nft_mon_print(monh, "%s ", cmd);
338
339
1/3
✓ Branch 0 (15→16) taken 10 times.
✗ Branch 1 (15→18) not taken.
✗ Branch 2 (15→20) not taken.
10 switch (type) {
340 10 case NFT_MSG_NEWSET:
341 10 set_print_plain(set, &monh->ctx->nft->output);
342 10 break;
343 ✗ case NFT_MSG_DELSET:
344 ✗ nft_mon_print(monh, "set %s %s %s", family,
345 set->handle.table.name,
346 set->handle.set.name);
347 ✗ break;
348 }
349 10 nft_mon_print(monh, "\n");
350 10 break;
351 9 case NFTNL_OUTPUT_JSON:
352 9 monitor_print_set_json(monh, cmd, set);
353
1/2
✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 9 times.
9 if (!nft_output_echo(&monh->ctx->nft->output))
354 ✗ nft_mon_print(monh, "\n");
355 9 break;
356 }
357 19 set_free(set);
358 25 out:
359 25 nftnl_set_free(nls);
360 25 return MNL_CB_OK;
361 }
362
363 /* returns true if the event should be ignored (i.e. null element) */
364 73 static bool netlink_event_ignore_range_event(struct nftnl_set_elem *nlse)
365 {
366 73 uint32_t flags = 0;
367
368
2/2
✓ Branch 0 (3→4) taken 34 times.
✓ Branch 1 (3→5) taken 39 times.
73 if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_FLAGS))
369 34 flags = nftnl_set_elem_get_u32(nlse, NFTNL_SET_ELEM_FLAGS);
370
2/2
✓ Branch 0 (5→6) taken 39 times.
✓ Branch 1 (5→7) taken 34 times.
73 if (!(flags & NFT_SET_ELEM_INTERVAL_END))
371 39 return false;
372
373
2/2
✓ Branch 0 (8→9) taken 20 times.
✓ Branch 1 (8→10) taken 14 times.
34 if (nftnl_set_elem_get_u32(nlse, NFTNL_SET_ELEM_KEY) != 0)
374 20 return false;
375
376 14 return true;
377 }
378
379 44 static bool set_elem_is_open_interval(struct expr *elem)
380 {
381
2/3
✓ Branch 0 (2→3) taken 36 times.
✓ Branch 1 (2→4) taken 8 times.
✗ Branch 2 (2→5) not taken.
44 switch (elem->etype) {
382 36 case EXPR_SET_ELEM:
383 36 return elem->flags & EXPR_F_INTERVAL_OPEN;
384 8 case EXPR_MAPPING:
385 8 return set_elem_is_open_interval(elem->left);
386 ✗ default:
387 ✗ return false;
388 }
389 }
390
391 /* returns true if the we cached the range element */
392 59 static bool netlink_event_range_cache(struct set *cached_set,
393 struct set *dummyset)
394 {
395 struct expr *elem;
396
397 /* not an interval ? */
398
2/2
✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→4) taken 56 times.
59 if (!(cached_set->flags & NFT_SET_INTERVAL))
399 3 return false;
400
401 /* if cache exists, dummyset must contain the other end of the range */
402
2/2
✓ Branch 0 (4→5) taken 20 times.
✓ Branch 1 (4→7) taken 36 times.
56 if (cached_set->rg_cache) {
403 20 set_expr_add(dummyset->init, cached_set->rg_cache);
404 20 cached_set->rg_cache = NULL;
405 20 goto out_decompose;
406 }
407
408 /* don't cache half-open range elements */
409
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 36 times.
36 elem = list_entry(expr_set(dummyset->init)->expressions.prev, struct expr, list);
410
2/2
✓ Branch 0 (10→11) taken 24 times.
✓ Branch 1 (10→15) taken 12 times.
36 if (!set_elem_is_open_interval(elem) &&
411
2/2
✓ Branch 0 (11→12) taken 4 times.
✓ Branch 1 (11→13) taken 20 times.
24 dummyset->desc.field_count <= 1) {
412 20 cached_set->rg_cache = expr_clone(elem);
413 20 return true;
414 }
415
416 16 out_decompose:
417
1/2
✓ Branch 0 (16→17) taken 36 times.
✗ Branch 1 (16→19) not taken.
36 if (dummyset->flags & NFT_SET_INTERVAL &&
418
2/2
✓ Branch 0 (17→18) taken 4 times.
✓ Branch 1 (17→19) taken 32 times.
36 dummyset->desc.field_count > 1)
419 4 concat_range_aggregate(dummyset->init);
420 else
421 32 interval_map_decompose(dummyset->init);
422
423 36 return false;
424 }
425
426 91 static int netlink_events_setelem_cb(const struct nlmsghdr *nlh, int type,
427 struct netlink_mon_handler *monh)
428 {
429 struct nftnl_set_elems_iter *nlsei;
430 struct nftnl_set_elem *nlse;
431 struct nftnl_set *nls;
432 struct set *dummyset;
433 struct set *set;
434 const char *setname, *table, *cmd;
435 uint32_t family;
436
437 91 nls = netlink_setelem_alloc(nlh);
438 91 table = nftnl_set_get_str(nls, NFTNL_SET_TABLE);
439 91 setname = nftnl_set_get_str(nls, NFTNL_SET_NAME);
440 91 family = nftnl_set_get_u32(nls, NFTNL_SET_FAMILY);
441 91 cmd = netlink_msg2cmd(type, nlh->nlmsg_flags);
442
443 91 set = set_lookup_global(family, table, setname, &monh->ctx->nft->cache);
444
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 91 times.
91 if (set == NULL) {
445 ✗ fprintf(stderr, "W: Received event for an unknown set.\n");
446 ✗ goto out;
447 }
448
449
2/2
✓ Branch 0 (12→13) taken 18 times.
✓ Branch 1 (12→14) taken 73 times.
91 if (set_is_anonymous(set->flags))
450 18 goto out;
451
452 /* we want to 'delinearize' the set_elem, but don't
453 * modify the original cached set. This path is only
454 * used by named sets, so use a dummy set.
455 */
456 73 dummyset = set_alloc(monh->loc);
457 73 handle_merge(&dummyset->handle, &set->handle);
458 73 dummyset->key = expr_clone(set->key);
459
2/2
✓ Branch 0 (17→18) taken 12 times.
✓ Branch 1 (17→20) taken 61 times.
73 if (set->data)
460 12 dummyset->data = expr_clone(set->data);
461 73 dummyset->flags = set->flags;
462 73 dummyset->init = set_expr_alloc(monh->loc, set);
463 73 dummyset->desc.field_count = set->desc.field_count;
464
465 73 nlsei = nftnl_set_elems_iter_create(nls);
466
1/2
✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 73 times.
73 if (nlsei == NULL)
467 ✗ memory_allocation_error();
468
469 73 nlse = nftnl_set_elems_iter_next(nlsei);
470
2/2
✓ Branch 0 (37→26) taken 73 times.
✓ Branch 1 (37→38) taken 59 times.
132 while (nlse != NULL) {
471
2/2
✓ Branch 0 (27→28) taken 14 times.
✓ Branch 1 (27→31) taken 59 times.
73 if (netlink_event_ignore_range_event(nlse)) {
472 14 set_free(dummyset);
473 14 nftnl_set_elems_iter_destroy(nlsei);
474 14 goto out;
475 }
476
1/2
✗ Branch 0 (32→33) not taken.
✓ Branch 1 (32→36) taken 59 times.
59 if (netlink_delinearize_setelem(monh->ctx,
477 nlse, dummyset) < 0) {
478 ✗ set_free(dummyset);
479 ✗ nftnl_set_elems_iter_destroy(nlsei);
480 ✗ goto out;
481 }
482 59 nlse = nftnl_set_elems_iter_next(nlsei);
483 }
484 59 nftnl_set_elems_iter_destroy(nlsei);
485
486
2/2
✓ Branch 0 (40→41) taken 20 times.
✓ Branch 1 (40→43) taken 39 times.
59 if (netlink_event_range_cache(set, dummyset)) {
487 20 set_free(dummyset);
488 20 goto out;
489 }
490
491
2/3
✓ Branch 0 (43→44) taken 20 times.
✓ Branch 1 (43→49) taken 19 times.
✗ Branch 2 (43→54) not taken.
39 switch (monh->format) {
492 20 case NFTNL_OUTPUT_DEFAULT:
493 20 nft_mon_print(monh, "%s element %s %s %s ",
494 cmd, family2str(family), table, setname);
495 20 expr_print(dummyset->init, &monh->ctx->nft->output);
496 20 nft_mon_print(monh, "\n");
497 20 break;
498 19 case NFTNL_OUTPUT_JSON:
499 19 dummyset->handle.family = family;
500 19 dummyset->handle.set.name = setname;
501 19 dummyset->handle.table.name = table;
502 19 monitor_print_element_json(monh, cmd, dummyset);
503 /* prevent set_free() from trying to free those */
504 19 dummyset->handle.set.name = NULL;
505 19 dummyset->handle.table.name = NULL;
506
1/2
✗ Branch 0 (51→52) not taken.
✓ Branch 1 (51→53) taken 19 times.
19 if (!nft_output_echo(&monh->ctx->nft->output))
507 ✗ nft_mon_print(monh, "\n");
508 19 break;
509 }
510 39 set_free(dummyset);
511 91 out:
512 91 nftnl_set_free(nls);
513 91 return MNL_CB_OK;
514 }
515
516 12 static int netlink_events_obj_cb(const struct nlmsghdr *nlh, int type,
517 struct netlink_mon_handler *monh)
518 {
519 const char *family, *cmd;
520 struct nftnl_obj *nlo;
521 struct obj *obj;
522
523 12 nlo = netlink_obj_alloc(nlh);
524
525 12 obj = netlink_delinearize_obj(monh->ctx, nlo);
526
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 12 times.
12 if (!obj) {
527 ✗ nftnl_obj_free(nlo);
528 ✗ return MNL_CB_ERROR;
529 }
530 12 family = family2str(obj->handle.family);
531 12 cmd = netlink_msg2cmd(type, nlh->nlmsg_flags);
532
533
2/3
✓ Branch 0 (9→10) taken 6 times.
✓ Branch 1 (9→19) taken 6 times.
✗ Branch 2 (9→24) not taken.
12 switch (monh->format) {
534 6 case NFTNL_OUTPUT_DEFAULT:
535 6 nft_mon_print(monh, "%s ", cmd);
536
537
1/3
✓ Branch 0 (11→12) taken 6 times.
✗ Branch 1 (11→14) not taken.
✗ Branch 2 (11→17) not taken.
6 switch (type) {
538 6 case NFT_MSG_NEWOBJ:
539 6 obj_print_plain(obj, &monh->ctx->nft->output);
540 6 break;
541 ✗ case NFT_MSG_DELOBJ:
542 ✗ nft_mon_print(monh, "%s %s %s %s",
543 obj_type_name(obj->type),
544 family,
545 obj->handle.table.name,
546 obj->handle.obj.name);
547 ✗ break;
548 }
549 6 nft_mon_print(monh, "\n");
550 6 break;
551 6 case NFTNL_OUTPUT_JSON:
552 6 monitor_print_obj_json(monh, cmd, obj, type == NFT_MSG_DELOBJ);
553
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 6 times.
6 if (!nft_output_echo(&monh->ctx->nft->output))
554 ✗ nft_mon_print(monh, "\n");
555 6 break;
556 }
557 12 obj_free(obj);
558 12 nftnl_obj_free(nlo);
559 12 return MNL_CB_OK;
560 }
561
562 4 static int netlink_events_flowtable_cb(const struct nlmsghdr *nlh, int type,
563 struct netlink_mon_handler *monh)
564 {
565 const char *family, *cmd;
566 struct nftnl_flowtable *nlf;
567 struct flowtable *ft;
568
569 4 nlf = netlink_flowtable_alloc(nlh);
570
571 4 ft = netlink_delinearize_flowtable(monh->ctx, nlf);
572
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 4 times.
4 if (!ft) {
573 ✗ nftnl_flowtable_free(nlf);
574 ✗ return MNL_CB_ERROR;
575 }
576 4 family = family2str(ft->handle.family);
577 4 cmd = netlink_msg2cmd(type, nlh->nlmsg_flags);
578
579
2/3
✓ Branch 0 (9→10) taken 2 times.
✓ Branch 1 (9→19) taken 2 times.
✗ Branch 2 (9→24) not taken.
4 switch (monh->format) {
580 2 case NFTNL_OUTPUT_DEFAULT:
581 2 nft_mon_print(monh, "%s ", cmd);
582
583
1/3
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→15) taken 2 times.
✗ Branch 2 (11→17) not taken.
2 switch (type) {
584 ✗ case NFT_MSG_DELFLOWTABLE:
585 ✗ if (!ft->dev_array_len) {
586 ✗ nft_mon_print(monh, "flowtable %s %s %s",
587 family,
588 ft->handle.table.name,
589 ft->handle.flowtable.name);
590 ✗ break;
591 }
592 /* fall through */
593 case NFT_MSG_NEWFLOWTABLE:
594 2 flowtable_print_plain(ft, &monh->ctx->nft->output);
595 2 break;
596 }
597 2 nft_mon_print(monh, "\n");
598 2 break;
599 2 case NFTNL_OUTPUT_JSON:
600 2 monitor_print_flowtable_json(monh, cmd, ft);
601
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 2 times.
2 if (!nft_output_echo(&monh->ctx->nft->output))
602 ✗ nft_mon_print(monh, "\n");
603 2 break;
604 }
605 4 flowtable_free(ft);
606 4 nftnl_flowtable_free(nlf);
607 4 return MNL_CB_OK;
608 }
609
610 8 static void rule_map_decompose_cb(struct set *s, void *data)
611 {
612
2/2
✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 6 times.
8 if (!set_is_anonymous(s->flags))
613 2 return;
614
615
2/2
✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→8) taken 4 times.
6 if (set_is_non_concat_range(s))
616 2 interval_map_decompose(s->init);
617
2/2
✓ Branch 0 (9→10) taken 2 times.
✓ Branch 1 (9→11) taken 2 times.
4 else if (set_is_interval(s->flags))
618 2 concat_range_aggregate(s->init);
619 }
620
621 3019 static int netlink_events_rule_cb(const struct nlmsghdr *nlh, int type,
622 struct netlink_mon_handler *monh)
623 {
624 const char *family, *cmd;
625 struct nftnl_rule *nlr;
626 struct rule *r;
627
628 3019 nlr = netlink_rule_alloc(nlh);
629 3019 r = netlink_delinearize_rule(monh->ctx, nlr);
630
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 3019 times.
3019 if (!r) {
631 ✗ fprintf(stderr, "W: Received event for an unknown table.\n");
632 ✗ goto out_free_nlr;
633 }
634 3019 nlr_for_each_set(nlr, rule_map_decompose_cb, NULL,
635 3019 &monh->ctx->nft->cache);
636 3019 cmd = netlink_msg2cmd(type, nlh->nlmsg_flags);
637
638
2/3
✓ Branch 0 (9→10) taken 3011 times.
✓ Branch 1 (9→21) taken 8 times.
✗ Branch 2 (9→26) not taken.
3019 switch (monh->format) {
639 3011 case NFTNL_OUTPUT_DEFAULT:
640 3011 family = family2str(r->handle.family);
641
642 3011 nft_mon_print(monh, "%s rule %s %s %s ",
643 cmd,
644 family,
645 r->handle.table.name,
646 r->handle.chain.name);
647
2/2
✓ Branch 0 (12→13) taken 3000 times.
✓ Branch 1 (12→14) taken 11 times.
3011 if (r->handle.position.id) {
648 3000 nft_mon_print(monh, "handle %" PRIu64" ",
649 r->handle.position.id);
650 }
651
2/3
✓ Branch 0 (14→15) taken 3010 times.
✓ Branch 1 (14→17) taken 1 times.
✗ Branch 2 (14→19) not taken.
3011 switch (type) {
652 3010 case NFT_MSG_NEWRULE:
653 3010 rule_print(r, &monh->ctx->nft->output);
654
655 3010 break;
656 1 case NFT_MSG_DELRULE:
657 1 nft_mon_print(monh, "handle %" PRIu64,
658 r->handle.handle.id);
659 1 break;
660 }
661 3011 nft_mon_print(monh, "\n");
662 3011 break;
663 8 case NFTNL_OUTPUT_JSON:
664 8 monitor_print_rule_json(monh, cmd, r);
665
1/2
✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→25) taken 8 times.
8 if (!nft_output_echo(&monh->ctx->nft->output))
666 ✗ nft_mon_print(monh, "\n");
667 8 break;
668 }
669 3019 rule_free(r);
670 3019 out_free_nlr:
671 3019 nftnl_rule_free(nlr);
672 3019 return MNL_CB_OK;
673 }
674
675 ✗ static void netlink_events_cache_addtable(struct netlink_mon_handler *monh,
676 const struct nlmsghdr *nlh)
677 {
678 struct nftnl_table *nlt;
679 struct table *t;
680
681 ✗ nlt = netlink_table_alloc(nlh);
682 ✗ t = netlink_delinearize_table(monh->ctx, nlt);
683 ✗ nftnl_table_free(nlt);
684
685 ✗ table_cache_add(t, &monh->ctx->nft->cache);
686 ✗ }
687
688 ✗ static void netlink_events_cache_deltable(struct netlink_mon_handler *monh,
689 const struct nlmsghdr *nlh)
690 {
691 struct nftnl_table *nlt;
692 struct table *t;
693 struct handle h;
694
695 ✗ nlt = netlink_table_alloc(nlh);
696 ✗ h.family = nftnl_table_get_u32(nlt, NFTNL_TABLE_FAMILY);
697 ✗ h.table.name = nftnl_table_get_str(nlt, NFTNL_TABLE_NAME);
698
699 ✗ t = table_cache_find(&monh->ctx->nft->cache.table_cache,
700 h.table.name, h.family);
701 ✗ if (t == NULL)
702 ✗ goto out;
703
704 ✗ table_cache_del(t);
705 ✗ table_free(t);
706 ✗ out:
707 ✗ nftnl_table_free(nlt);
708 ✗ }
709
710 25 static void netlink_events_cache_addset(struct netlink_mon_handler *monh,
711 const struct nlmsghdr *nlh)
712 {
713 struct netlink_ctx set_tmpctx;
714 struct nftnl_set *nls;
715 struct table *t;
716 struct set *s;
717 25 LIST_HEAD(msgs);
718
719 25 memset(&set_tmpctx, 0, sizeof(set_tmpctx));
720 25 init_list_head(&set_tmpctx.list);
721 25 init_list_head(&msgs);
722 25 set_tmpctx.nft = monh->ctx->nft;
723 25 set_tmpctx.msgs = &msgs;
724
725 25 nls = netlink_set_alloc(nlh);
726 25 s = netlink_delinearize_set(&set_tmpctx, nls);
727
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 25 times.
25 if (s == NULL)
728 ✗ goto out;
729 25 s->init = set_expr_alloc(monh->loc, s);
730
731 25 t = table_cache_find(&monh->ctx->nft->cache.table_cache,
732 s->handle.table.name, s->handle.family);
733
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→14) taken 25 times.
25 if (t == NULL) {
734 ✗ fprintf(stderr, "W: Unable to cache set: table not found.\n");
735 ✗ set_free(s);
736 ✗ goto out;
737 }
738
739
1/2
✓ Branch 0 (15→16) taken 25 times.
✗ Branch 1 (15→20) not taken.
25 if (nft_output_echo(&monh->ctx->nft->output) &&
740
2/2
✓ Branch 0 (17→18) taken 19 times.
✓ Branch 1 (17→20) taken 6 times.
25 !set_is_anonymous(s->flags)) {
741 19 set_free(s);
742 19 goto out;
743 }
744
745 6 set_cache_add(s, t);
746 25 out:
747 25 nftnl_set_free(nls);
748 25 }
749
750 91 static void netlink_events_cache_addsetelem(struct netlink_mon_handler *monh,
751 const struct nlmsghdr *nlh)
752 {
753 struct nftnl_set_elems_iter *nlsei;
754 struct nftnl_set_elem *nlse;
755 struct nftnl_set *nls;
756 struct set *set;
757 const char *table, *setname;
758 uint32_t family;
759
760 91 nls = netlink_setelem_alloc(nlh);
761 91 family = nftnl_set_get_u32(nls, NFTNL_SET_FAMILY);
762 91 table = nftnl_set_get_str(nls, NFTNL_SET_TABLE);
763 91 setname = nftnl_set_get_str(nls, NFTNL_SET_NAME);
764
765 91 set = set_lookup_global(family, table, setname, &monh->ctx->nft->cache);
766
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 91 times.
91 if (set == NULL) {
767 ✗ fprintf(stderr,
768 "W: Unable to cache set_elem. Set not found.\n");
769 ✗ goto out;
770 }
771
772
1/2
✓ Branch 0 (11→12) taken 91 times.
✗ Branch 1 (11→15) not taken.
91 if (nft_output_echo(&monh->ctx->nft->output) &&
773
2/2
✓ Branch 0 (13→14) taken 73 times.
✓ Branch 1 (13→15) taken 18 times.
91 !set_is_anonymous(set->flags))
774 73 goto out;
775
776 18 nlsei = nftnl_set_elems_iter_create(nls);
777
1/2
✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 18 times.
18 if (nlsei == NULL)
778 ✗ memory_allocation_error();
779
780 18 nlse = nftnl_set_elems_iter_next(nlsei);
781
2/2
✓ Branch 0 (26→20) taken 18 times.
✓ Branch 1 (26→27) taken 18 times.
36 while (nlse != NULL) {
782
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→25) taken 18 times.
18 if (netlink_delinearize_setelem(monh->ctx, nlse, set) < 0) {
783 ✗ fprintf(stderr,
784 "W: Unable to cache set_elem. "
785 "Delinearize failed.\n");
786 ✗ nftnl_set_elems_iter_destroy(nlsei);
787 ✗ goto out;
788 }
789 18 nlse = nftnl_set_elems_iter_next(nlsei);
790 }
791 18 nftnl_set_elems_iter_destroy(nlsei);
792 91 out:
793 91 nftnl_set_free(nls);
794 91 }
795
796 ✗ static void netlink_events_cache_delset_cb(struct set *s,
797 void *data)
798 {
799 ✗ set_cache_del(s);
800 ✗ set_free(s);
801 ✗ }
802
803 ✗ static void netlink_events_cache_delsets(struct netlink_mon_handler *monh,
804 const struct nlmsghdr *nlh)
805 {
806 ✗ struct nftnl_rule *nlr = netlink_rule_alloc(nlh);
807
808 ✗ nlr_for_each_set(nlr, netlink_events_cache_delset_cb, NULL,
809 ✗ &monh->ctx->nft->cache);
810 ✗ nftnl_rule_free(nlr);
811 ✗ }
812
813 ✗ static void netlink_events_cache_addobj(struct netlink_mon_handler *monh,
814 const struct nlmsghdr *nlh)
815 {
816 struct netlink_ctx obj_tmpctx;
817 struct nftnl_obj *nlo;
818 struct table *t;
819 struct obj *obj;
820 ✗ LIST_HEAD(msgs);
821
822 ✗ memset(&obj_tmpctx, 0, sizeof(obj_tmpctx));
823 ✗ init_list_head(&obj_tmpctx.list);
824 ✗ init_list_head(&msgs);
825 ✗ obj_tmpctx.msgs = &msgs;
826
827 ✗ nlo = netlink_obj_alloc(nlh);
828 ✗ obj = netlink_delinearize_obj(&obj_tmpctx, nlo);
829 ✗ if (obj == NULL)
830 ✗ goto out;
831
832 ✗ t = table_cache_find(&monh->ctx->nft->cache.table_cache,
833 obj->handle.table.name, obj->handle.family);
834 ✗ if (t == NULL) {
835 ✗ fprintf(stderr, "W: Unable to cache object: table not found.\n");
836 ✗ obj_free(obj);
837 ✗ goto out;
838 }
839
840 ✗ obj_cache_add(obj, t);
841 ✗ out:
842 ✗ nftnl_obj_free(nlo);
843 ✗ }
844
845 ✗ static void netlink_events_cache_delobj(struct netlink_mon_handler *monh,
846 const struct nlmsghdr *nlh)
847 {
848 struct nftnl_obj *nlo;
849 const char *name;
850 struct obj *obj;
851 struct handle h;
852 struct table *t;
853 uint32_t type;
854
855 ✗ nlo = netlink_obj_alloc(nlh);
856 ✗ h.family = nftnl_obj_get_u32(nlo, NFTNL_OBJ_FAMILY);
857 ✗ h.table.name = nftnl_obj_get_str(nlo, NFTNL_OBJ_TABLE);
858
859 ✗ name = nftnl_obj_get_str(nlo, NFTNL_OBJ_NAME);
860 ✗ type = nftnl_obj_get_u32(nlo, NFTNL_OBJ_TYPE);
861 ✗ h.handle.id = nftnl_obj_get_u64(nlo, NFTNL_OBJ_HANDLE);
862
863 ✗ t = table_cache_find(&monh->ctx->nft->cache.table_cache,
864 h.table.name, h.family);
865 ✗ if (t == NULL) {
866 ✗ fprintf(stderr, "W: Unable to cache object: table not found.\n");
867 ✗ goto out;
868 }
869
870 ✗ obj = obj_cache_find(t, name, type);
871 ✗ if (obj == NULL) {
872 ✗ fprintf(stderr, "W: Unable to find object in cache\n");
873 ✗ goto out;
874 }
875
876 ✗ obj_cache_del(obj);
877 ✗ obj_free(obj);
878 ✗ out:
879 ✗ nftnl_obj_free(nlo);
880 ✗ }
881
882 3288 static void netlink_events_cache_update(struct netlink_mon_handler *monh,
883 const struct nlmsghdr *nlh, int type)
884 {
885
3/4
✓ Branch 0 (3→4) taken 3288 times.
✗ Branch 1 (3→7) not taken.
✓ Branch 2 (4→5) taken 3263 times.
✓ Branch 3 (4→7) taken 25 times.
3288 if (nft_output_echo(&monh->ctx->nft->output) &&
886
2/2
✓ Branch 0 (5→6) taken 3172 times.
✓ Branch 1 (5→7) taken 91 times.
3263 type != NFT_MSG_NEWSET && type != NFT_MSG_NEWSETELEM)
887 3172 return;
888
889
2/8
✗ Branch 0 (7→8) not taken.
✗ Branch 1 (7→10) not taken.
✓ Branch 2 (7→12) taken 25 times.
✓ Branch 3 (7→14) taken 91 times.
✗ Branch 4 (7→16) not taken.
✗ Branch 5 (7→18) not taken.
✗ Branch 6 (7→20) not taken.
✗ Branch 7 (7→22) not taken.
116 switch (type) {
890 ✗ case NFT_MSG_NEWTABLE:
891 ✗ netlink_events_cache_addtable(monh, nlh);
892 ✗ break;
893 ✗ case NFT_MSG_DELTABLE:
894 ✗ netlink_events_cache_deltable(monh, nlh);
895 ✗ break;
896 25 case NFT_MSG_NEWSET:
897 25 netlink_events_cache_addset(monh, nlh);
898 25 break;
899 91 case NFT_MSG_NEWSETELEM:
900 91 netlink_events_cache_addsetelem(monh, nlh);
901 91 break;
902 ✗ case NFT_MSG_DELRULE:
903 /* there are no notification for anon-set deletion */
904 ✗ netlink_events_cache_delsets(monh, nlh);
905 ✗ break;
906 ✗ case NFT_MSG_NEWOBJ:
907 ✗ netlink_events_cache_addobj(monh, nlh);
908 ✗ break;
909 ✗ case NFT_MSG_DELOBJ:
910 ✗ netlink_events_cache_delobj(monh, nlh);
911 ✗ break;
912 }
913 }
914
915 /* only those which could be useful listening to events */
916 static const char *const nftnl_msg_types[NFT_MSG_MAX] = {
917 [NFT_MSG_NEWTABLE] = "NFT_MSG_NEWTABLE",
918 [NFT_MSG_DELTABLE] = "NFT_MSG_DELTABLE",
919 [NFT_MSG_NEWCHAIN] = "NFT_MSG_NEWCHAIN",
920 [NFT_MSG_DELCHAIN] = "NFT_MSG_DELCHAIN",
921 [NFT_MSG_NEWSET] = "NFT_MSG_NEWSET",
922 [NFT_MSG_DELSET] = "NFT_MSG_DELSET",
923 [NFT_MSG_NEWSETELEM] = "NFT_MSG_NEWSETELEM",
924 [NFT_MSG_DELSETELEM] = "NFT_MSG_DELSETELEM",
925 [NFT_MSG_NEWRULE] = "NFT_MSG_NEWRULE",
926 [NFT_MSG_DELRULE] = "NFT_MSG_DELRULE",
927 [NFT_MSG_TRACE] = "NFT_MSG_TRACE",
928 [NFT_MSG_NEWGEN] = "NFT_MSG_NEWGEN",
929 [NFT_MSG_NEWOBJ] = "NFT_MSG_NEWOBJ",
930 [NFT_MSG_DELOBJ] = "NFT_MSG_DELOBJ",
931 };
932
933 ✗ static const char *nftnl_msgtype2str(uint16_t type)
934 {
935 ✗ if (type >= NFT_MSG_MAX || !nftnl_msg_types[type])
936 ✗ return "unknown";
937
938 ✗ return nftnl_msg_types[type];
939 }
940
941 3288 static void netlink_events_debug(uint16_t type, unsigned int debug_mask)
942 {
943
1/2
✓ Branch 0 (2→3) taken 3288 times.
✗ Branch 1 (2→4) not taken.
3288 if (!(debug_mask & NFT_DEBUG_NETLINK))
944 3288 return;
945
946 ✗ printf("netlink event: %s\n", nftnl_msgtype2str(type));
947 }
948
949 92 static int netlink_events_newgen_cb(const struct nlmsghdr *nlh, int type,
950 struct netlink_mon_handler *monh)
951 {
952 const struct nlattr *attr;
953 92 char name[256] = "";
954 92 int genid = -1, pid = -1;
955
956
2/2
✓ Branch 0 (2→3) taken 43 times.
✓ Branch 1 (2→4) taken 49 times.
92 if (monh->format != NFTNL_OUTPUT_DEFAULT)
957 43 return MNL_CB_OK;
958
959
2/2
✓ Branch 0 (25→5) taken 147 times.
✓ Branch 1 (25→26) taken 49 times.
196 mnl_attr_for_each(attr, nlh, sizeof(struct nfgenmsg)) {
960
3/4
✓ Branch 0 (6→7) taken 49 times.
✓ Branch 1 (6→12) taken 49 times.
✓ Branch 2 (6→17) taken 49 times.
✗ Branch 3 (6→22) not taken.
147 switch (mnl_attr_get_type(attr)) {
961 49 case NFTA_GEN_ID:
962
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 49 times.
49 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
963 ✗ break;
964 49 genid = ntohl(mnl_attr_get_u32(attr));
965 49 break;
966 49 case NFTA_GEN_PROC_NAME:
967
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 49 times.
49 if (mnl_attr_validate(attr, MNL_TYPE_NUL_STRING) < 0)
968 ✗ break;
969 49 snprintf(name, sizeof(name), "%s", mnl_attr_get_str(attr));
970 49 break;
971 49 case NFTA_GEN_PROC_PID:
972
1/2
✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 49 times.
49 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
973 ✗ break;
974 49 pid = ntohl(mnl_attr_get_u32(attr));
975 49 break;
976 }
977 }
978
1/2
✓ Branch 0 (26→27) taken 49 times.
✗ Branch 1 (26→31) not taken.
49 if (genid >= 0) {
979 49 nft_mon_print(monh, "# new generation %d", genid);
980
1/2
✓ Branch 0 (28→29) taken 49 times.
✗ Branch 1 (28→30) not taken.
49 if (pid >= 0)
981 49 nft_mon_print(monh, " by process %d (%s)", pid, name);
982
983 49 nft_mon_print(monh, "\n");
984 }
985
986 49 return MNL_CB_OK;
987 }
988
989 3288 static int netlink_events_cb(const struct nlmsghdr *nlh, void *data)
990 {
991 3288 int ret = MNL_CB_OK;
992 3288 uint16_t type = NFNL_MSG_TYPE(nlh->nlmsg_type);
993 3288 struct netlink_mon_handler *monh = (struct netlink_mon_handler *)data;
994
995 3288 netlink_events_debug(type, monh->ctx->nft->debug_mask);
996 3288 netlink_events_cache_update(monh, nlh, type);
997
998
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 3288 times.
3288 if (!(monh->monitor_flags & (1 << type)))
999 ✗ return ret;
1000
1001
8/10
✓ Branch 0 (6→7) taken 30 times.
✓ Branch 1 (6→9) taken 15 times.
✓ Branch 2 (6→11) taken 25 times.
✓ Branch 3 (6→13) taken 91 times.
✓ Branch 4 (6→15) taken 3019 times.
✗ Branch 5 (6→17) not taken.
✓ Branch 6 (6→19) taken 12 times.
✓ Branch 7 (6→21) taken 4 times.
✓ Branch 8 (6→23) taken 92 times.
✗ Branch 9 (6→25) not taken.
3288 switch (type) {
1002 30 case NFT_MSG_NEWTABLE:
1003 case NFT_MSG_DELTABLE:
1004 30 ret = netlink_events_table_cb(nlh, type, monh);
1005 30 break;
1006 15 case NFT_MSG_NEWCHAIN:
1007 case NFT_MSG_DELCHAIN:
1008 15 ret = netlink_events_chain_cb(nlh, type, monh);
1009 15 break;
1010 25 case NFT_MSG_NEWSET:
1011 case NFT_MSG_DELSET: /* nft {add|delete} set */
1012 25 ret = netlink_events_set_cb(nlh, type, monh);
1013 25 break;
1014 91 case NFT_MSG_NEWSETELEM:
1015 case NFT_MSG_DELSETELEM: /* nft {add|delete} element */
1016 91 ret = netlink_events_setelem_cb(nlh, type, monh);
1017 91 break;
1018 3019 case NFT_MSG_NEWRULE:
1019 case NFT_MSG_DELRULE:
1020 3019 ret = netlink_events_rule_cb(nlh, type, monh);
1021 3019 break;
1022 ✗ case NFT_MSG_TRACE:
1023 ✗ ret = netlink_events_trace_cb(nlh, type, monh);
1024 ✗ break;
1025 12 case NFT_MSG_NEWOBJ:
1026 case NFT_MSG_DELOBJ:
1027 12 ret = netlink_events_obj_cb(nlh, type, monh);
1028 12 break;
1029 4 case NFT_MSG_NEWFLOWTABLE:
1030 case NFT_MSG_DELFLOWTABLE:
1031 4 ret = netlink_events_flowtable_cb(nlh, type, monh);
1032 4 break;
1033 92 case NFT_MSG_NEWGEN:
1034 92 ret = netlink_events_newgen_cb(nlh, type, monh);
1035 92 break;
1036 }
1037
1038 3288 return ret;
1039 }
1040
1041 6770 int netlink_echo_callback(const struct nlmsghdr *nlh, void *data)
1042 {
1043 6770 struct netlink_cb_data *nl_cb_data = data;
1044 6770 struct netlink_ctx *ctx = nl_cb_data->nl_ctx;
1045 6770 struct nft_ctx *nft = ctx->nft;
1046
1047 6770 struct netlink_mon_handler echo_monh = {
1048 .format = NFTNL_OUTPUT_DEFAULT,
1049 .ctx = ctx,
1050 .loc = &netlink_location,
1051 .monitor_flags = 0xffffffff,
1052 };
1053
1054
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 6770 times.
6770 if (!nft_output_echo(&echo_monh.ctx->nft->output))
1055 ✗ return MNL_CB_OK;
1056
1057
2/2
✓ Branch 0 (6→7) taken 3618 times.
✓ Branch 1 (6→13) taken 3152 times.
6770 if (nft_output_json(&nft->output)) {
1058
2/2
✓ Branch 0 (7→8) taken 3482 times.
✓ Branch 1 (7→10) taken 136 times.
3618 if (nft->json_root)
1059 3482 return json_events_cb(nlh, &echo_monh);
1060
2/2
✓ Branch 0 (10→11) taken 43 times.
✓ Branch 1 (10→12) taken 93 times.
136 if (!nft->json_echo)
1061 43 json_alloc_echo(nft);
1062 136 echo_monh.format = NFTNL_OUTPUT_JSON;
1063 }
1064
1065 3288 return netlink_events_cb(nlh, &echo_monh);
1066 }
1067
1068 ✗ int netlink_monitor(struct netlink_mon_handler *monhandler,
1069 struct mnl_socket *nf_sock)
1070 {
1071 int group;
1072
1073 ✗ if (monhandler->monitor_flags & (1 << NFT_MSG_TRACE)) {
1074 ✗ group = NFNLGRP_NFTRACE;
1075 ✗ if (mnl_socket_setsockopt(nf_sock, NETLINK_ADD_MEMBERSHIP,
1076 &group, sizeof(int)) < 0)
1077 ✗ return -1;
1078 }
1079 ✗ if (monhandler->monitor_flags & ~(1 << NFT_MSG_TRACE)) {
1080 ✗ group = NFNLGRP_NFTABLES;
1081 ✗ if (mnl_socket_setsockopt(nf_sock, NETLINK_ADD_MEMBERSHIP,
1082 &group, sizeof(int)) < 0)
1083 ✗ return -1;
1084 }
1085
1086 ✗ return mnl_nft_event_listener(nf_sock, monhandler->ctx->nft->debug_mask,
1087 ✗ &monhandler->ctx->nft->output,
1088 netlink_events_cb, monhandler);
1089 }
1090