| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2019 Pablo Neira Ayuso <pablo@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 <expression.h> | ||
| 12 | #include <statement.h> | ||
| 13 | #include <rule.h> | ||
| 14 | #include <erec.h> | ||
| 15 | #include <utils.h> | ||
| 16 | #include <cache.h> | ||
| 17 | #include <netlink.h> | ||
| 18 | #include <mnl.h> | ||
| 19 | #include <libnftnl/chain.h> | ||
| 20 | #include <linux/netfilter.h> | ||
| 21 | #include <linux/netfilter/nf_tables.h> | ||
| 22 | |||
| 23 | 99795 | static unsigned int evaluate_cache_add(struct cmd *cmd, unsigned int flags) | |
| 24 | { | ||
| 25 | struct set *set; | ||
| 26 | |||
| 27 |
5/5✓ Branch 0 (2→3) taken 3524 times.
✓ Branch 1 (2→11) taken 7286 times.
✓ Branch 2 (2→12) taken 23357 times.
✓ Branch 3 (2→13) taken 65620 times.
✓ Branch 4 (2→16) taken 8 times.
|
99795 | switch (cmd->obj) { |
| 28 | 3524 | case CMD_OBJ_TABLE: | |
| 29 |
2/2✓ Branch 0 (3→4) taken 2073 times.
✓ Branch 1 (3→5) taken 1451 times.
|
3524 | if (!cmd->table) |
| 30 | 2073 | break; | |
| 31 | |||
| 32 | 1451 | flags |= NFT_CACHE_TABLE | | |
| 33 | NFT_CACHE_SET; | ||
| 34 |
2/2✓ Branch 0 (9→6) taken 875 times.
✓ Branch 1 (9→10) taken 1451 times.
|
2326 | list_for_each_entry(set, &cmd->table->sets, list) { |
| 35 |
2/2✓ Branch 0 (6→7) taken 14 times.
✓ Branch 1 (6→8) taken 861 times.
|
875 | if (set->automerge) |
| 36 | 14 | flags |= NFT_CACHE_SETELEM_MAYBE; | |
| 37 | } | ||
| 38 | 1451 | break; | |
| 39 | 7286 | case CMD_OBJ_CHAIN: | |
| 40 | case CMD_OBJ_SET: | ||
| 41 | case CMD_OBJ_COUNTER: | ||
| 42 | case CMD_OBJ_QUOTA: | ||
| 43 | case CMD_OBJ_LIMIT: | ||
| 44 | case CMD_OBJ_SECMARK: | ||
| 45 | case CMD_OBJ_CT_HELPER: | ||
| 46 | case CMD_OBJ_CT_TIMEOUT: | ||
| 47 | case CMD_OBJ_CT_EXPECT: | ||
| 48 | case CMD_OBJ_SYNPROXY: | ||
| 49 | case CMD_OBJ_FLOWTABLE: | ||
| 50 | 7286 | flags |= NFT_CACHE_TABLE; | |
| 51 | 7286 | break; | |
| 52 | 23357 | case CMD_OBJ_ELEMENTS: | |
| 53 | 23357 | flags |= NFT_CACHE_TABLE | | |
| 54 | NFT_CACHE_SET | | ||
| 55 | NFT_CACHE_SETELEM_MAYBE; | ||
| 56 | 23357 | break; | |
| 57 | 65620 | case CMD_OBJ_RULE: | |
| 58 | 65620 | flags |= NFT_CACHE_TABLE | | |
| 59 | NFT_CACHE_SET; | ||
| 60 | |||
| 61 |
2/2✓ Branch 0 (13→14) taken 28 times.
✓ Branch 1 (13→15) taken 65592 times.
|
65620 | if (cmd->handle.index.id) |
| 62 | 28 | flags |= NFT_CACHE_FULL | NFT_CACHE_UPDATE; | |
| 63 | 65620 | break; | |
| 64 | 8 | default: | |
| 65 | 8 | break; | |
| 66 | } | ||
| 67 | |||
| 68 | 99795 | return flags; | |
| 69 | } | ||
| 70 | |||
| 71 | 2853 | static unsigned int evaluate_cache_del(struct cmd *cmd, unsigned int flags) | |
| 72 | { | ||
| 73 |
2/2✓ Branch 0 (2→3) taken 2063 times.
✓ Branch 1 (2→4) taken 790 times.
|
2853 | switch (cmd->obj) { |
| 74 | 2063 | case CMD_OBJ_ELEMENTS: | |
| 75 | 2063 | flags |= NFT_CACHE_SET | | |
| 76 | NFT_CACHE_SETELEM_MAYBE; | ||
| 77 | 2063 | break; | |
| 78 | 790 | default: | |
| 79 | 790 | flags = NFT_CACHE_TABLE; | |
| 80 | 790 | break; | |
| 81 | } | ||
| 82 | |||
| 83 | 2853 | return flags; | |
| 84 | } | ||
| 85 | |||
| 86 | 1064 | static unsigned int evaluate_cache_get(struct cmd *cmd, unsigned int flags) | |
| 87 | { | ||
| 88 |
1/2✓ Branch 0 (2→3) taken 1064 times.
✗ Branch 1 (2→4) not taken.
|
1064 | switch (cmd->obj) { |
| 89 | 1064 | case CMD_OBJ_ELEMENTS: | |
| 90 | 1064 | flags |= NFT_CACHE_TABLE | | |
| 91 | NFT_CACHE_SET | | ||
| 92 | NFT_CACHE_SETELEM; | ||
| 93 | 1064 | break; | |
| 94 | ✗ | default: | |
| 95 | ✗ | break; | |
| 96 | } | ||
| 97 | |||
| 98 | 1064 | return flags; | |
| 99 | } | ||
| 100 | |||
| 101 | 56356 | struct nft_cache_filter *nft_cache_filter_init(void) | |
| 102 | { | ||
| 103 | struct nft_cache_filter *filter; | ||
| 104 | int i; | ||
| 105 | |||
| 106 | 56356 | filter = xzalloc(sizeof(struct nft_cache_filter)); | |
| 107 | 56356 | memset(&filter->list, 0, sizeof(filter->list)); | |
| 108 |
2/2✓ Branch 0 (6→4) taken 461668352 times.
✓ Branch 1 (6→7) taken 56356 times.
|
461724708 | for (i = 0; i < NFT_CACHE_HSIZE; i++) |
| 109 | 461668352 | init_list_head(&filter->obj[i].head); | |
| 110 | |||
| 111 | 56356 | return filter; | |
| 112 | } | ||
| 113 | |||
| 114 | 56356 | void nft_cache_filter_fini(struct nft_cache_filter *filter) | |
| 115 | { | ||
| 116 | int i; | ||
| 117 | |||
| 118 |
2/2✓ Branch 0 (7→3) taken 461668352 times.
✓ Branch 1 (7→8) taken 56356 times.
|
461724708 | for (i = 0; i < NFT_CACHE_HSIZE; i++) { |
| 119 | struct nft_filter_obj *obj, *next; | ||
| 120 | |||
| 121 |
2/2✓ Branch 0 (5→4) taken 209 times.
✓ Branch 1 (5→6) taken 461668352 times.
|
461668561 | list_for_each_entry_safe(obj, next, &filter->obj[i].head, list) |
| 122 | 209 | free(obj); | |
| 123 | } | ||
| 124 | 56356 | free(filter); | |
| 125 | 56356 | } | |
| 126 | |||
| 127 | 209 | static void cache_filter_add(struct nft_cache_filter *filter, | |
| 128 | const struct cmd *cmd) | ||
| 129 | { | ||
| 130 | struct nft_filter_obj *obj; | ||
| 131 | uint32_t hash; | ||
| 132 | |||
| 133 | 209 | obj = xmalloc(sizeof(struct nft_filter_obj)); | |
| 134 | 209 | obj->family = cmd->handle.family; | |
| 135 | 209 | obj->table = cmd->handle.table.name; | |
| 136 | 209 | obj->set = cmd->handle.set.name; | |
| 137 | |||
| 138 | 209 | hash = djb_hash(cmd->handle.set.name) % NFT_CACHE_HSIZE; | |
| 139 | 209 | list_add_tail(&obj->list, &filter->obj[hash].head); | |
| 140 | 209 | } | |
| 141 | |||
| 142 | 20116 | static bool cache_filter_find(const struct nft_cache_filter *filter, | |
| 143 | const struct handle *handle) | ||
| 144 | { | ||
| 145 | struct nft_filter_obj *obj; | ||
| 146 | uint32_t hash; | ||
| 147 | |||
| 148 | 20116 | hash = djb_hash(handle->set.name) % NFT_CACHE_HSIZE; | |
| 149 | |||
| 150 |
2/2✓ Branch 0 (9→4) taken 184 times.
✓ Branch 1 (9→10) taken 19932 times.
|
20116 | list_for_each_entry(obj, &filter->obj[hash].head, list) { |
| 151 |
1/2✓ Branch 0 (4→5) taken 184 times.
✗ Branch 1 (4→8) not taken.
|
184 | if (obj->family == handle->family && |
| 152 |
1/2✓ Branch 0 (5→6) taken 184 times.
✗ Branch 1 (5→8) not taken.
|
184 | !strcmp(obj->table, handle->table.name) && |
| 153 |
1/2✓ Branch 0 (6→7) taken 184 times.
✗ Branch 1 (6→8) not taken.
|
184 | !strcmp(obj->set, handle->set.name)) |
| 154 | 184 | return true; | |
| 155 | } | ||
| 156 | |||
| 157 | 19932 | return false; | |
| 158 | } | ||
| 159 | |||
| 160 | 15689 | static unsigned int evaluate_cache_flush(struct cmd *cmd, unsigned int flags, | |
| 161 | struct nft_cache_filter *filter) | ||
| 162 | { | ||
| 163 |
3/3✓ Branch 0 (2→3) taken 209 times.
✓ Branch 1 (2→5) taken 1377 times.
✓ Branch 2 (2→6) taken 14103 times.
|
15689 | switch (cmd->obj) { |
| 164 | 209 | case CMD_OBJ_SET: | |
| 165 | case CMD_OBJ_MAP: | ||
| 166 | case CMD_OBJ_METER: | ||
| 167 | 209 | flags |= NFT_CACHE_SET; | |
| 168 | 209 | cache_filter_add(filter, cmd); | |
| 169 | 209 | break; | |
| 170 | 1377 | case CMD_OBJ_RULESET: | |
| 171 | 1377 | flags |= NFT_CACHE_FLUSHED; | |
| 172 | 1377 | break; | |
| 173 | 14103 | default: | |
| 174 | 14103 | break; | |
| 175 | } | ||
| 176 | |||
| 177 | 15689 | return flags; | |
| 178 | } | ||
| 179 | |||
| 180 | 4 | static unsigned int evaluate_cache_rename(struct cmd *cmd, unsigned int flags) | |
| 181 | { | ||
| 182 |
1/2✓ Branch 0 (2→3) taken 4 times.
✗ Branch 1 (2→4) not taken.
|
4 | switch (cmd->obj) { |
| 183 | 4 | case CMD_OBJ_CHAIN: | |
| 184 | 4 | flags |= NFT_CACHE_CHAIN; | |
| 185 | 4 | break; | |
| 186 | ✗ | default: | |
| 187 | ✗ | break; | |
| 188 | } | ||
| 189 | |||
| 190 | 4 | return flags; | |
| 191 | } | ||
| 192 | |||
| 193 | 416 | static void obj_filter_setup(const struct cmd *cmd, unsigned int *flags, | |
| 194 | struct nft_cache_filter *filter, int type) | ||
| 195 | { | ||
| 196 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 416 times.
|
416 | assert(filter); |
| 197 | |||
| 198 |
2/2✓ Branch 0 (4→5) taken 406 times.
✓ Branch 1 (4→6) taken 10 times.
|
416 | if (cmd->handle.family) |
| 199 | 406 | filter->list.family = cmd->handle.family; | |
| 200 |
2/2✓ Branch 0 (6→7) taken 404 times.
✓ Branch 1 (6→8) taken 12 times.
|
416 | if (cmd->handle.table.name) |
| 201 | 404 | filter->list.table = cmd->handle.table.name; | |
| 202 |
2/2✓ Branch 0 (8→9) taken 404 times.
✓ Branch 1 (8→10) taken 12 times.
|
416 | if (cmd->handle.obj.name) |
| 203 | 404 | filter->list.obj = cmd->handle.obj.name; | |
| 204 | |||
| 205 | 416 | filter->list.obj_type = type; | |
| 206 | 416 | *flags |= NFT_CACHE_TABLE | NFT_CACHE_OBJECT; | |
| 207 | 416 | } | |
| 208 | |||
| 209 | 16411 | static unsigned int evaluate_cache_list(struct nft_ctx *nft, struct cmd *cmd, | |
| 210 | unsigned int flags, | ||
| 211 | struct nft_cache_filter *filter) | ||
| 212 | { | ||
| 213 |
15/17✓ Branch 0 (2→3) taken 12608 times.
✓ Branch 1 (2→6) taken 1275 times.
✓ Branch 2 (2→9) taken 1021 times.
✓ Branch 3 (2→17) taken 1 times.
✓ Branch 4 (2→18) taken 4 times.
✓ Branch 5 (2→22) taken 2 times.
✓ Branch 6 (2→25) taken 1 times.
✓ Branch 7 (2→26) taken 284 times.
✓ Branch 8 (2→28) taken 13 times.
✓ Branch 9 (2→30) taken 4 times.
✓ Branch 10 (2→32) taken 8 times.
✓ Branch 11 (2→34) taken 10 times.
✗ Branch 12 (2→36) not taken.
✓ Branch 13 (2→38) taken 10 times.
✓ Branch 14 (2→40) taken 4 times.
✗ Branch 15 (2→42) not taken.
✓ Branch 16 (2→44) taken 1166 times.
|
16411 | switch (cmd->obj) { |
| 214 | 12608 | case CMD_OBJ_TABLE: | |
| 215 | 12608 | filter->list.family = cmd->handle.family; | |
| 216 |
2/2✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 12606 times.
|
12608 | if (!cmd->handle.table.name) { |
| 217 | 2 | flags |= NFT_CACHE_TABLE; | |
| 218 | 2 | break; | |
| 219 | } else { | ||
| 220 | 12606 | filter->list.table = cmd->handle.table.name; | |
| 221 | } | ||
| 222 | 12606 | flags |= NFT_CACHE_FULL; | |
| 223 | 12606 | break; | |
| 224 | 1275 | case CMD_OBJ_CHAIN: | |
| 225 |
1/2✓ Branch 0 (6→7) taken 1275 times.
✗ Branch 1 (6→8) not taken.
|
1275 | if (cmd->handle.chain.name) { |
| 226 | 1275 | filter->list.family = cmd->handle.family; | |
| 227 | 1275 | filter->list.table = cmd->handle.table.name; | |
| 228 | 1275 | filter->list.chain = cmd->handle.chain.name; | |
| 229 | /* implicit terse listing to fetch content of anonymous | ||
| 230 | * sets only when chain name is specified. | ||
| 231 | */ | ||
| 232 | 1275 | flags |= NFT_CACHE_TERSE; | |
| 233 | } | ||
| 234 | 1275 | flags |= NFT_CACHE_FULL; | |
| 235 | 1275 | break; | |
| 236 | 1021 | case CMD_OBJ_SET: | |
| 237 | case CMD_OBJ_MAP: | ||
| 238 |
2/4✓ Branch 0 (9→10) taken 1021 times.
✗ Branch 1 (9→12) not taken.
✓ Branch 2 (10→11) taken 1021 times.
✗ Branch 3 (10→12) not taken.
|
1021 | if (cmd->handle.table.name && cmd->handle.set.name) { |
| 239 | 1021 | filter->list.family = cmd->handle.family; | |
| 240 | 1021 | filter->list.table = cmd->handle.table.name; | |
| 241 | 1021 | filter->list.set = cmd->handle.set.name; | |
| 242 | } | ||
| 243 |
2/4✓ Branch 0 (12→13) taken 1021 times.
✗ Branch 1 (12→15) not taken.
✓ Branch 2 (13→14) taken 1021 times.
✗ Branch 3 (13→15) not taken.
|
1021 | if (filter->list.table && filter->list.set) |
| 244 | 1021 | flags |= NFT_CACHE_TABLE | NFT_CACHE_SET | NFT_CACHE_SETELEM; | |
| 245 | else | ||
| 246 | ✗ | flags |= NFT_CACHE_FULL; | |
| 247 | 1021 | break; | |
| 248 | 1 | case CMD_OBJ_CHAINS: | |
| 249 | 1 | flags |= NFT_CACHE_TABLE | NFT_CACHE_CHAIN; | |
| 250 | 1 | break; | |
| 251 | 4 | case CMD_OBJ_SETS: | |
| 252 | case CMD_OBJ_MAPS: | ||
| 253 | 4 | flags |= NFT_CACHE_TABLE | NFT_CACHE_SET; | |
| 254 |
1/2✓ Branch 0 (19→20) taken 4 times.
✗ Branch 1 (19→21) not taken.
|
4 | if (!nft_output_terse(&nft->output)) |
| 255 | 4 | flags |= NFT_CACHE_SETELEM; | |
| 256 | 4 | break; | |
| 257 | 2 | case CMD_OBJ_FLOWTABLE: | |
| 258 |
1/2✓ Branch 0 (22→23) taken 2 times.
✗ Branch 1 (22→25) not taken.
|
2 | if (cmd->handle.table.name && |
| 259 |
1/2✓ Branch 0 (23→24) taken 2 times.
✗ Branch 1 (23→25) not taken.
|
2 | cmd->handle.flowtable.name) { |
| 260 | 2 | filter->list.family = cmd->handle.family; | |
| 261 | 2 | filter->list.table = cmd->handle.table.name; | |
| 262 | 2 | filter->list.ft = cmd->handle.flowtable.name; | |
| 263 | } | ||
| 264 | /* fall through */ | ||
| 265 | case CMD_OBJ_FLOWTABLES: | ||
| 266 | 3 | flags |= NFT_CACHE_TABLE | NFT_CACHE_FLOWTABLE; | |
| 267 | 3 | break; | |
| 268 | 284 | case CMD_OBJ_COUNTER: | |
| 269 | case CMD_OBJ_COUNTERS: | ||
| 270 | 284 | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_COUNTER); | |
| 271 | 284 | break; | |
| 272 | 13 | case CMD_OBJ_QUOTA: | |
| 273 | case CMD_OBJ_QUOTAS: | ||
| 274 | 13 | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_QUOTA); | |
| 275 | 13 | break; | |
| 276 | 4 | case CMD_OBJ_CT_HELPER: | |
| 277 | case CMD_OBJ_CT_HELPERS: | ||
| 278 | 4 | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_CT_HELPER); | |
| 279 | 4 | break; | |
| 280 | 8 | case CMD_OBJ_LIMIT: | |
| 281 | case CMD_OBJ_LIMITS: | ||
| 282 | 8 | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_LIMIT); | |
| 283 | 8 | break; | |
| 284 | 10 | case CMD_OBJ_CT_TIMEOUT: | |
| 285 | case CMD_OBJ_CT_TIMEOUTS: | ||
| 286 | 10 | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_CT_TIMEOUT); | |
| 287 | 10 | break; | |
| 288 | ✗ | case CMD_OBJ_SECMARK: | |
| 289 | case CMD_OBJ_SECMARKS: | ||
| 290 | ✗ | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_SECMARK); | |
| 291 | ✗ | break; | |
| 292 | 10 | case CMD_OBJ_CT_EXPECT: | |
| 293 | 10 | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_CT_EXPECT); | |
| 294 | 10 | break; | |
| 295 | 4 | case CMD_OBJ_SYNPROXY: | |
| 296 | case CMD_OBJ_SYNPROXYS: | ||
| 297 | 4 | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_SYNPROXY); | |
| 298 | 4 | break; | |
| 299 | ✗ | case CMD_OBJ_TUNNEL: | |
| 300 | case CMD_OBJ_TUNNELS: | ||
| 301 | ✗ | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_TUNNEL); | |
| 302 | ✗ | break; | |
| 303 | 1166 | case CMD_OBJ_RULESET: | |
| 304 | default: | ||
| 305 | 1166 | flags |= NFT_CACHE_FULL; | |
| 306 | 1166 | break; | |
| 307 | } | ||
| 308 | 16411 | flags |= NFT_CACHE_REFRESH; | |
| 309 | |||
| 310 |
2/2✓ Branch 0 (46→47) taken 3 times.
✓ Branch 1 (46→48) taken 16408 times.
|
16411 | if (nft_output_terse(&nft->output)) |
| 311 | 3 | flags |= NFT_CACHE_TERSE; | |
| 312 | |||
| 313 | 16411 | return flags; | |
| 314 | } | ||
| 315 | |||
| 316 | 917 | static unsigned int evaluate_cache_reset(struct cmd *cmd, unsigned int flags, | |
| 317 | struct nft_cache_filter *filter) | ||
| 318 | { | ||
| 319 |
5/5✓ Branch 0 (2→3) taken 9 times.
✓ Branch 1 (2→12) taken 80 times.
✓ Branch 2 (2→14) taken 3 times.
✓ Branch 3 (2→16) taken 4 times.
✓ Branch 4 (2→20) taken 821 times.
|
917 | switch (cmd->obj) { |
| 320 | 9 | case CMD_OBJ_TABLE: | |
| 321 | case CMD_OBJ_CHAIN: | ||
| 322 | case CMD_OBJ_RULES: | ||
| 323 | case CMD_OBJ_RULE: | ||
| 324 |
2/2✓ Branch 0 (3→4) taken 5 times.
✓ Branch 1 (3→5) taken 4 times.
|
9 | if (cmd->handle.table.name) { |
| 325 | 5 | filter->list.family = cmd->handle.family; | |
| 326 | 5 | filter->list.table = cmd->handle.table.name; | |
| 327 | } | ||
| 328 |
2/2✓ Branch 0 (5→6) taken 4 times.
✓ Branch 1 (5→7) taken 5 times.
|
9 | if (cmd->handle.chain.name) |
| 329 | 4 | filter->list.chain = cmd->handle.chain.name; | |
| 330 |
2/2✓ Branch 0 (7→8) taken 6 times.
✓ Branch 1 (7→9) taken 3 times.
|
9 | if (cmd->handle.family) |
| 331 | 6 | filter->list.family = cmd->handle.family; | |
| 332 |
2/2✓ Branch 0 (9→10) taken 1 times.
✓ Branch 1 (9→11) taken 8 times.
|
9 | if (cmd->handle.handle.id) |
| 333 | 1 | filter->list.rule_handle = cmd->handle.handle.id; | |
| 334 | |||
| 335 | 9 | filter->reset.rule = true; | |
| 336 | 9 | flags |= NFT_CACHE_FULL; | |
| 337 | 9 | break; | |
| 338 | 80 | case CMD_OBJ_COUNTER: | |
| 339 | case CMD_OBJ_COUNTERS: | ||
| 340 | 80 | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_COUNTER); | |
| 341 | 80 | filter->reset.obj = true; | |
| 342 | 80 | break; | |
| 343 | 3 | case CMD_OBJ_QUOTA: | |
| 344 | case CMD_OBJ_QUOTAS: | ||
| 345 | 3 | obj_filter_setup(cmd, &flags, filter, NFT_OBJECT_QUOTA); | |
| 346 | 3 | filter->reset.obj = true; | |
| 347 | 3 | break; | |
| 348 | 4 | case CMD_OBJ_SET: | |
| 349 | case CMD_OBJ_MAP: | ||
| 350 |
2/4✓ Branch 0 (16→17) taken 4 times.
✗ Branch 1 (16→19) not taken.
✓ Branch 2 (17→18) taken 4 times.
✗ Branch 3 (17→19) not taken.
|
4 | if (cmd->handle.table.name && cmd->handle.set.name) { |
| 351 | 4 | filter->list.family = cmd->handle.family; | |
| 352 | 4 | filter->list.table = cmd->handle.table.name; | |
| 353 | 4 | filter->list.set = cmd->handle.set.name; | |
| 354 | } | ||
| 355 | 4 | flags |= NFT_CACHE_SETELEM; | |
| 356 | 4 | filter->reset.elem = true; | |
| 357 | 4 | break; | |
| 358 | 821 | default: | |
| 359 | 821 | flags |= NFT_CACHE_FULL; | |
| 360 | 821 | break; | |
| 361 | } | ||
| 362 | 917 | flags |= NFT_CACHE_REFRESH; | |
| 363 | |||
| 364 | 917 | return flags; | |
| 365 | } | ||
| 366 | |||
| 367 | 136744 | static int nft_handle_validate(const struct cmd *cmd, struct list_head *msgs) | |
| 368 | { | ||
| 369 | 136744 | const struct handle *h = &cmd->handle; | |
| 370 | const struct location *loc; | ||
| 371 | |||
| 372 |
6/7✓ Branch 0 (2→3) taken 30080 times.
✓ Branch 1 (2→7) taken 72694 times.
✓ Branch 2 (2→14) taken 30688 times.
✓ Branch 3 (2→21) taken 104 times.
✓ Branch 4 (2→28) taken 2541 times.
✓ Branch 5 (2→29) taken 637 times.
✗ Branch 6 (2→36) not taken.
|
136744 | switch (cmd->obj) { |
| 373 | 30080 | case CMD_OBJ_TABLE: | |
| 374 |
2/2✓ Branch 0 (3→4) taken 30075 times.
✓ Branch 1 (3→6) taken 5 times.
|
30080 | if (h->table.name && |
| 375 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 30075 times.
|
30075 | strlen(h->table.name) > NFT_NAME_MAXLEN) { |
| 376 | ✗ | loc = &h->table.location; | |
| 377 | ✗ | goto err_name_too_long; | |
| 378 | } | ||
| 379 | 30080 | break; | |
| 380 | 72694 | case CMD_OBJ_RULE: | |
| 381 | case CMD_OBJ_RULES: | ||
| 382 | case CMD_OBJ_CHAIN: | ||
| 383 | case CMD_OBJ_CHAINS: | ||
| 384 |
2/2✓ Branch 0 (7→8) taken 72689 times.
✓ Branch 1 (7→10) taken 5 times.
|
72694 | if (h->table.name && |
| 385 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 72689 times.
|
72689 | strlen(h->table.name) > NFT_NAME_MAXLEN) { |
| 386 | ✗ | loc = &h->table.location; | |
| 387 | ✗ | goto err_name_too_long; | |
| 388 | } | ||
| 389 |
2/2✓ Branch 0 (10→11) taken 72685 times.
✓ Branch 1 (10→13) taken 9 times.
|
72694 | if (h->chain.name && |
| 390 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 72685 times.
|
72685 | strlen(h->chain.name) > NFT_NAME_MAXLEN) { |
| 391 | ✗ | loc = &h->chain.location; | |
| 392 | ✗ | goto err_name_too_long; | |
| 393 | } | ||
| 394 | 72694 | break; | |
| 395 | 30688 | case CMD_OBJ_ELEMENTS: | |
| 396 | case CMD_OBJ_SET: | ||
| 397 | case CMD_OBJ_SETS: | ||
| 398 | case CMD_OBJ_MAP: | ||
| 399 | case CMD_OBJ_MAPS: | ||
| 400 | case CMD_OBJ_METER: | ||
| 401 | case CMD_OBJ_METERS: | ||
| 402 |
2/2✓ Branch 0 (14→15) taken 30682 times.
✓ Branch 1 (14→17) taken 6 times.
|
30688 | if (h->table.name && |
| 403 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 30682 times.
|
30682 | strlen(h->table.name) > NFT_NAME_MAXLEN) { |
| 404 | ✗ | loc = &h->table.location; | |
| 405 | ✗ | goto err_name_too_long; | |
| 406 | } | ||
| 407 |
2/2✓ Branch 0 (17→18) taken 30677 times.
✓ Branch 1 (17→20) taken 11 times.
|
30688 | if (h->set.name && |
| 408 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 30677 times.
|
30677 | strlen(h->set.name) > NFT_NAME_MAXLEN) { |
| 409 | ✗ | loc = &h->set.location; | |
| 410 | ✗ | goto err_name_too_long; | |
| 411 | } | ||
| 412 | 30688 | break; | |
| 413 | 104 | case CMD_OBJ_FLOWTABLE: | |
| 414 | case CMD_OBJ_FLOWTABLES: | ||
| 415 |
2/2✓ Branch 0 (21→22) taken 103 times.
✓ Branch 1 (21→24) taken 1 times.
|
104 | if (h->table.name && |
| 416 |
1/2✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 103 times.
|
103 | strlen(h->table.name) > NFT_NAME_MAXLEN) { |
| 417 | ✗ | loc = &h->table.location; | |
| 418 | ✗ | goto err_name_too_long; | |
| 419 | } | ||
| 420 |
2/2✓ Branch 0 (24→25) taken 100 times.
✓ Branch 1 (24→27) taken 4 times.
|
104 | if (h->flowtable.name && |
| 421 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 100 times.
|
100 | strlen(h->flowtable.name) > NFT_NAME_MAXLEN) { |
| 422 | ✗ | loc = &h->flowtable.location; | |
| 423 | ✗ | goto err_name_too_long; | |
| 424 | } | ||
| 425 | 104 | break; | |
| 426 | 2541 | case CMD_OBJ_INVALID: | |
| 427 | case CMD_OBJ_EXPR: | ||
| 428 | case CMD_OBJ_RULESET: | ||
| 429 | case CMD_OBJ_MARKUP: | ||
| 430 | case CMD_OBJ_MONITOR: | ||
| 431 | case CMD_OBJ_SETELEMS: | ||
| 432 | case CMD_OBJ_HOOKS: | ||
| 433 | 2541 | break; | |
| 434 | 637 | case CMD_OBJ_COUNTER: | |
| 435 | case CMD_OBJ_COUNTERS: | ||
| 436 | case CMD_OBJ_QUOTA: | ||
| 437 | case CMD_OBJ_QUOTAS: | ||
| 438 | case CMD_OBJ_LIMIT: | ||
| 439 | case CMD_OBJ_LIMITS: | ||
| 440 | case CMD_OBJ_SECMARK: | ||
| 441 | case CMD_OBJ_SECMARKS: | ||
| 442 | case CMD_OBJ_SYNPROXY: | ||
| 443 | case CMD_OBJ_SYNPROXYS: | ||
| 444 | case CMD_OBJ_CT_HELPER: | ||
| 445 | case CMD_OBJ_CT_HELPERS: | ||
| 446 | case CMD_OBJ_CT_TIMEOUT: | ||
| 447 | case CMD_OBJ_CT_TIMEOUTS: | ||
| 448 | case CMD_OBJ_CT_EXPECT: | ||
| 449 | case CMD_OBJ_CT_EXPECTATIONS: | ||
| 450 | case CMD_OBJ_TUNNEL: | ||
| 451 | case CMD_OBJ_TUNNELS: | ||
| 452 |
2/2✓ Branch 0 (29→30) taken 625 times.
✓ Branch 1 (29→32) taken 12 times.
|
637 | if (h->table.name && |
| 453 |
1/2✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→32) taken 625 times.
|
625 | strlen(h->table.name) > NFT_NAME_MAXLEN) { |
| 454 | ✗ | loc = &h->table.location; | |
| 455 | ✗ | goto err_name_too_long; | |
| 456 | } | ||
| 457 |
2/2✓ Branch 0 (32→33) taken 621 times.
✓ Branch 1 (32→35) taken 16 times.
|
637 | if (h->obj.name && |
| 458 |
1/2✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 621 times.
|
621 | strlen(h->obj.name) > NFT_NAME_MAXLEN) { |
| 459 | ✗ | loc = &h->obj.location; | |
| 460 | ✗ | goto err_name_too_long; | |
| 461 | } | ||
| 462 | 637 | break; | |
| 463 | } | ||
| 464 | |||
| 465 | 136744 | return 0; | |
| 466 | |||
| 467 | ✗ | err_name_too_long: | |
| 468 | ✗ | erec_queue(error(loc, "name too long, %d characters maximum allowed", | |
| 469 | NFT_NAME_MAXLEN), | ||
| 470 | msgs); | ||
| 471 | ✗ | return -1; | |
| 472 | } | ||
| 473 | |||
| 474 | 136744 | static void reset_filter(struct nft_cache_filter *filter) | |
| 475 | { | ||
| 476 | 136744 | memset(&filter->list, 0, sizeof(filter->list)); | |
| 477 | 136744 | memset(&filter->reset, 0, sizeof(filter->reset)); | |
| 478 | 136744 | } | |
| 479 | |||
| 480 | 56356 | int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds, | |
| 481 | struct list_head *msgs, struct nft_cache_filter *filter, | ||
| 482 | unsigned int *pflags) | ||
| 483 | { | ||
| 484 | 56356 | unsigned int flags, batch_flags = NFT_CACHE_EMPTY; | |
| 485 | struct cmd *cmd; | ||
| 486 | |||
| 487 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 56356 times.
|
56356 | assert(filter); |
| 488 | |||
| 489 |
2/2✓ Branch 0 (29→5) taken 136744 times.
✓ Branch 1 (29→30) taken 56356 times.
|
193100 | list_for_each_entry(cmd, cmds, list) { |
| 490 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 136744 times.
|
136744 | if (nft_handle_validate(cmd, msgs) < 0) |
| 491 | ✗ | return -1; | |
| 492 | |||
| 493 | 136744 | flags = NFT_CACHE_EMPTY; | |
| 494 | 136744 | reset_filter(filter); | |
| 495 | |||
| 496 |
8/11✓ Branch 0 (9→10) taken 99795 times.
✓ Branch 1 (9→12) taken 11 times.
✓ Branch 2 (9→13) taken 2853 times.
✓ Branch 3 (9→15) taken 1064 times.
✓ Branch 4 (9→17) taken 917 times.
✓ Branch 5 (9→19) taken 16411 times.
✗ Branch 6 (9→21) not taken.
✓ Branch 7 (9→22) taken 15689 times.
✓ Branch 8 (9→24) taken 4 times.
✗ Branch 9 (9→26) not taken.
✗ Branch 10 (9→27) not taken.
|
136744 | switch (cmd->op) { |
| 497 | 99795 | case CMD_ADD: | |
| 498 | case CMD_INSERT: | ||
| 499 | case CMD_CREATE: | ||
| 500 | 99795 | flags = evaluate_cache_add(cmd, flags); | |
| 501 | 99795 | break; | |
| 502 | 11 | case CMD_REPLACE: /* only for rule */ | |
| 503 | 11 | flags = NFT_CACHE_TABLE | NFT_CACHE_SET; | |
| 504 | 11 | break; | |
| 505 | 2853 | case CMD_DELETE: | |
| 506 | case CMD_DESTROY: | ||
| 507 | 2853 | flags = evaluate_cache_del(cmd, flags); | |
| 508 | 2853 | break; | |
| 509 | 1064 | case CMD_GET: | |
| 510 | 1064 | flags = evaluate_cache_get(cmd, flags); | |
| 511 | 1064 | break; | |
| 512 | 917 | case CMD_RESET: | |
| 513 | 917 | flags = evaluate_cache_reset(cmd, flags, filter); | |
| 514 | 917 | break; | |
| 515 | 16411 | case CMD_LIST: | |
| 516 | 16411 | flags = evaluate_cache_list(nft, cmd, flags, filter); | |
| 517 | 16411 | break; | |
| 518 | ✗ | case CMD_MONITOR: | |
| 519 | ✗ | flags = NFT_CACHE_FULL; | |
| 520 | ✗ | break; | |
| 521 | 15689 | case CMD_FLUSH: | |
| 522 | 15689 | flags = evaluate_cache_flush(cmd, flags, filter); | |
| 523 | 15689 | break; | |
| 524 | 4 | case CMD_RENAME: | |
| 525 | 4 | flags = evaluate_cache_rename(cmd, flags); | |
| 526 | 4 | break; | |
| 527 | ✗ | case CMD_DESCRIBE: | |
| 528 | case CMD_IMPORT: | ||
| 529 | case CMD_EXPORT: | ||
| 530 | ✗ | break; | |
| 531 | ✗ | default: | |
| 532 | ✗ | break; | |
| 533 | } | ||
| 534 | 136744 | batch_flags |= flags; | |
| 535 | } | ||
| 536 | 56356 | *pflags = batch_flags; | |
| 537 | |||
| 538 | 56356 | return 0; | |
| 539 | } | ||
| 540 | |||
| 541 | 82300 | void table_cache_add(struct table *table, struct nft_cache *cache) | |
| 542 | { | ||
| 543 | 82300 | cache_add(&table->cache, &cache->table_cache, table->handle.table.name); | |
| 544 | 82300 | } | |
| 545 | |||
| 546 | 82300 | void table_cache_del(struct table *table) | |
| 547 | { | ||
| 548 | 82300 | cache_del(&table->cache); | |
| 549 | 82300 | } | |
| 550 | |||
| 551 | 321249 | struct table *table_cache_find(const struct cache *cache, | |
| 552 | const char *name, uint32_t family) | ||
| 553 | { | ||
| 554 | struct table *table; | ||
| 555 | uint32_t hash; | ||
| 556 | |||
| 557 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 321249 times.
|
321249 | assert(name); |
| 558 | |||
| 559 | 321249 | hash = djb_hash(name) % NFT_CACHE_HSIZE; | |
| 560 |
2/2✓ Branch 0 (10→6) taken 323804 times.
✓ Branch 1 (10→11) taken 3837 times.
|
327641 | list_for_each_entry(table, &cache->ht[hash], cache.hlist) { |
| 561 |
2/2✓ Branch 0 (6→7) taken 317412 times.
✓ Branch 1 (6→9) taken 6392 times.
|
323804 | if (table->handle.family == family && |
| 562 |
1/2✓ Branch 0 (7→8) taken 317412 times.
✗ Branch 1 (7→9) not taken.
|
317412 | !strcmp(table->handle.table.name, name)) |
| 563 | 317412 | return table; | |
| 564 | } | ||
| 565 | |||
| 566 | 3837 | return NULL; | |
| 567 | } | ||
| 568 | |||
| 569 | struct chain_cache_dump_ctx { | ||
| 570 | struct netlink_ctx *nlctx; | ||
| 571 | struct table *table; | ||
| 572 | }; | ||
| 573 | |||
| 574 | 61376 | static int chain_cache_cb(struct nftnl_chain *nlc, void *arg) | |
| 575 | { | ||
| 576 | 61376 | struct chain_cache_dump_ctx *ctx = arg; | |
| 577 | const char *chain_name, *table_name; | ||
| 578 | struct chain *chain; | ||
| 579 | uint32_t family; | ||
| 580 | |||
| 581 | 61376 | table_name = nftnl_chain_get_str(nlc, NFTNL_CHAIN_TABLE); | |
| 582 | 61376 | family = nftnl_chain_get_u32(nlc, NFTNL_CHAIN_FAMILY); | |
| 583 | |||
| 584 |
2/2✓ Branch 0 (4→5) taken 19277 times.
✓ Branch 1 (4→6) taken 42099 times.
|
61376 | if (family != ctx->table->handle.family || |
| 585 |
2/2✓ Branch 0 (5→6) taken 42 times.
✓ Branch 1 (5→7) taken 19235 times.
|
19277 | strcmp(table_name, ctx->table->handle.table.name)) |
| 586 | 42141 | return 0; | |
| 587 | |||
| 588 | 19235 | chain_name = nftnl_chain_get_str(nlc, NFTNL_CHAIN_NAME); | |
| 589 | |||
| 590 | 19235 | chain = netlink_delinearize_chain(ctx->nlctx, nlc); | |
| 591 |
2/2✓ Branch 0 (9→10) taken 27 times.
✓ Branch 1 (9→11) taken 19208 times.
|
19235 | if (chain->flags & CHAIN_F_BINDING) { |
| 592 | 27 | list_add_tail(&chain->cache.list, &ctx->table->chain_bindings); | |
| 593 | } else { | ||
| 594 | 19208 | cache_add(&chain->cache, &ctx->table->chain_cache, chain_name); | |
| 595 | } | ||
| 596 | |||
| 597 | 19235 | nftnl_chain_list_del(nlc); | |
| 598 | 19235 | nftnl_chain_free(nlc); | |
| 599 | |||
| 600 | 19235 | return 0; | |
| 601 | } | ||
| 602 | |||
| 603 | 15514 | static int chain_cache_init(struct netlink_ctx *ctx, struct table *table, | |
| 604 | struct nftnl_chain_list *chain_list) | ||
| 605 | { | ||
| 606 | 15514 | struct chain_cache_dump_ctx dump_ctx = { | |
| 607 | .nlctx = ctx, | ||
| 608 | .table = table, | ||
| 609 | }; | ||
| 610 | 15514 | nftnl_chain_list_foreach(chain_list, chain_cache_cb, &dump_ctx); | |
| 611 | |||
| 612 | 15514 | return 0; | |
| 613 | } | ||
| 614 | |||
| 615 | static struct nftnl_chain_list * | ||
| 616 | 15915 | chain_cache_dump(struct netlink_ctx *ctx, | |
| 617 | const struct nft_cache_filter *filter, int *err) | ||
| 618 | { | ||
| 619 | struct nftnl_chain_list *chain_list; | ||
| 620 | 15915 | const char *table = NULL; | |
| 621 | 15915 | const char *chain = NULL; | |
| 622 | 15915 | int family = NFPROTO_UNSPEC; | |
| 623 | |||
| 624 |
6/6✓ Branch 0 (2→3) taken 15899 times.
✓ Branch 1 (2→6) taken 16 times.
✓ Branch 2 (3→4) taken 13886 times.
✓ Branch 3 (3→6) taken 2013 times.
✓ Branch 4 (4→5) taken 1281 times.
✓ Branch 5 (4→6) taken 12605 times.
|
15915 | if (filter && filter->list.table && filter->list.chain) { |
| 625 | 1281 | family = filter->list.family; | |
| 626 | 1281 | table = filter->list.table; | |
| 627 | 1281 | chain = filter->list.chain; | |
| 628 | } | ||
| 629 | |||
| 630 | 15915 | chain_list = mnl_nft_chain_dump(ctx, family, table, chain); | |
| 631 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→11) taken 15914 times.
|
15915 | if (chain_list == NULL) { |
| 632 |
1/2✓ Branch 0 (8→9) taken 1 times.
✗ Branch 1 (8→10) not taken.
|
1 | if (errno == EINTR) { |
| 633 | 1 | *err = -1; | |
| 634 | 1 | return NULL; | |
| 635 | } | ||
| 636 | ✗ | *err = 0; | |
| 637 | ✗ | return NULL; | |
| 638 | } | ||
| 639 | |||
| 640 | 15914 | return chain_list; | |
| 641 | } | ||
| 642 | |||
| 643 | 3 | void nft_chain_cache_update(struct netlink_ctx *ctx, struct table *table, | |
| 644 | const char *chain) | ||
| 645 | { | ||
| 646 | struct nftnl_chain_list *chain_list; | ||
| 647 | |||
| 648 | 3 | chain_list = mnl_nft_chain_dump(ctx, table->handle.family, | |
| 649 | table->handle.table.name, chain); | ||
| 650 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 3 times.
|
3 | if (!chain_list) |
| 651 | ✗ | return; | |
| 652 | |||
| 653 | 3 | chain_cache_init(ctx, table, chain_list); | |
| 654 | |||
| 655 | 3 | nftnl_chain_list_free(chain_list); | |
| 656 | } | ||
| 657 | |||
| 658 | 7496 | void chain_cache_add(struct chain *chain, struct table *table) | |
| 659 | { | ||
| 660 | 7496 | cache_add(&chain->cache, &table->chain_cache, chain->handle.chain.name); | |
| 661 | 7496 | } | |
| 662 | |||
| 663 | 4 | void chain_cache_del(struct chain *chain) | |
| 664 | { | ||
| 665 | 4 | cache_del(&chain->cache); | |
| 666 | 4 | } | |
| 667 | |||
| 668 | 66718 | struct chain *chain_cache_find(const struct table *table, const char *name) | |
| 669 | { | ||
| 670 | struct chain *chain; | ||
| 671 | uint32_t hash; | ||
| 672 | |||
| 673 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 66718 times.
|
66718 | assert(name); |
| 674 | |||
| 675 | 66718 | hash = djb_hash(name) % NFT_CACHE_HSIZE; | |
| 676 |
2/2✓ Branch 0 (9→6) taken 58252 times.
✓ Branch 1 (9→10) taken 8466 times.
|
66718 | list_for_each_entry(chain, &table->chain_cache.ht[hash], cache.hlist) { |
| 677 |
1/2✓ Branch 0 (6→7) taken 58252 times.
✗ Branch 1 (6→8) not taken.
|
58252 | if (!strcmp(chain->handle.chain.name, name)) |
| 678 | 58252 | return chain; | |
| 679 | } | ||
| 680 | |||
| 681 | 8466 | return NULL; | |
| 682 | } | ||
| 683 | |||
| 684 | 56642 | static int list_rule_cb(struct nftnl_rule *nlr, void *data) | |
| 685 | { | ||
| 686 | 56642 | struct netlink_ctx *ctx = data; | |
| 687 | 56642 | const struct handle *h = ctx->data; | |
| 688 | const char *table, *chain; | ||
| 689 | struct rule *rule; | ||
| 690 | uint32_t family; | ||
| 691 | |||
| 692 | 56642 | family = nftnl_rule_get_u32(nlr, NFTNL_RULE_FAMILY); | |
| 693 | 56642 | table = nftnl_rule_get_str(nlr, NFTNL_RULE_TABLE); | |
| 694 | 56642 | chain = nftnl_rule_get_str(nlr, NFTNL_RULE_CHAIN); | |
| 695 | |||
| 696 |
2/4✓ Branch 0 (5→6) taken 56642 times.
✗ Branch 1 (5→7) not taken.
✓ Branch 2 (6→7) taken 56642 times.
✗ Branch 3 (6→11) not taken.
|
56642 | if ((h->family != NFPROTO_UNSPEC && h->family != family) || |
| 697 |
2/4✓ Branch 0 (7→8) taken 56642 times.
✗ Branch 1 (7→9) not taken.
✓ Branch 2 (8→9) taken 56642 times.
✗ Branch 3 (8→11) not taken.
|
56642 | (h->table.name && strcmp(table, h->table.name) != 0) || |
| 698 |
1/4✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 56642 times.
✗ Branch 2 (10→11) not taken.
✗ Branch 3 (10→12) not taken.
|
56642 | (h->chain.name && strcmp(chain, h->chain.name) != 0)) |
| 699 | ✗ | return 0; | |
| 700 | |||
| 701 | 56642 | netlink_dump_rule(nlr, ctx); | |
| 702 | 56642 | rule = netlink_delinearize_rule(ctx, nlr); | |
| 703 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 56642 times.
|
56642 | assert(rule); |
| 704 | 56642 | list_add_tail(&rule->list, &ctx->list); | |
| 705 | |||
| 706 | 56642 | return 0; | |
| 707 | } | ||
| 708 | |||
| 709 | 15502 | static int rule_cache_dump(struct netlink_ctx *ctx, const struct handle *h, | |
| 710 | const struct nft_cache_filter *filter) | ||
| 711 | { | ||
| 712 | struct nftnl_rule_list *rule_cache; | ||
| 713 | 15502 | const char *table = h->table.name; | |
| 714 | 15502 | const char *chain = NULL; | |
| 715 | 15502 | uint64_t rule_handle = 0; | |
| 716 | 15502 | int family = h->family; | |
| 717 | 15502 | bool reset = false; | |
| 718 | 15502 | bool dump = true; | |
| 719 | |||
| 720 |
1/2✓ Branch 0 (2→3) taken 15502 times.
✗ Branch 1 (2→12) not taken.
|
15502 | if (filter) { |
| 721 |
2/2✓ Branch 0 (3→4) taken 13430 times.
✓ Branch 1 (3→5) taken 2072 times.
|
15502 | if (filter->list.table) |
| 722 | 13430 | table = filter->list.table; | |
| 723 |
2/2✓ Branch 0 (5→6) taken 1283 times.
✓ Branch 1 (5→7) taken 14219 times.
|
15502 | if (filter->list.chain) |
| 724 | 1283 | chain = filter->list.chain; | |
| 725 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 15501 times.
|
15502 | if (filter->list.rule_handle) { |
| 726 | 1 | rule_handle = filter->list.rule_handle; | |
| 727 | 1 | dump = false; | |
| 728 | } | ||
| 729 |
2/2✓ Branch 0 (9→10) taken 13429 times.
✓ Branch 1 (9→11) taken 2073 times.
|
15502 | if (filter->list.family) |
| 730 | 13429 | family = filter->list.family; | |
| 731 | |||
| 732 | 15502 | reset = filter->reset.rule; | |
| 733 | } | ||
| 734 | |||
| 735 | 15502 | rule_cache = mnl_nft_rule_dump(ctx, family, | |
| 736 | table, chain, rule_handle, dump, reset); | ||
| 737 |
2/2✓ Branch 0 (13→14) taken 1 times.
✓ Branch 1 (13→17) taken 15501 times.
|
15502 | if (rule_cache == NULL) { |
| 738 |
1/2✓ Branch 0 (14→15) taken 1 times.
✗ Branch 1 (14→16) not taken.
|
1 | if (errno == EINTR) |
| 739 | 1 | return -1; | |
| 740 | |||
| 741 | ✗ | return 0; | |
| 742 | } | ||
| 743 | |||
| 744 | 15501 | ctx->data = h; | |
| 745 | 15501 | nftnl_rule_list_foreach(rule_cache, list_rule_cb, ctx); | |
| 746 | 15501 | nftnl_rule_list_free(rule_cache); | |
| 747 | 15501 | return 0; | |
| 748 | } | ||
| 749 | |||
| 750 | struct set_cache_dump_ctx { | ||
| 751 | struct netlink_ctx *nlctx; | ||
| 752 | struct table *table; | ||
| 753 | }; | ||
| 754 | |||
| 755 | 64266 | static int set_cache_cb(struct nftnl_set *nls, void *arg) | |
| 756 | { | ||
| 757 | 64266 | struct set_cache_dump_ctx *ctx = arg; | |
| 758 | const char *set_table; | ||
| 759 | const char *set_name; | ||
| 760 | uint32_t set_family; | ||
| 761 | struct set *set; | ||
| 762 | |||
| 763 | 64266 | set_table = nftnl_set_get_str(nls, NFTNL_SET_TABLE); | |
| 764 | 64266 | set_family = nftnl_set_get_u32(nls, NFTNL_SET_FAMILY); | |
| 765 | |||
| 766 |
2/2✓ Branch 0 (4→5) taken 37350 times.
✓ Branch 1 (4→6) taken 26916 times.
|
64266 | if (set_family != ctx->table->handle.family || |
| 767 |
2/2✓ Branch 0 (5→6) taken 16 times.
✓ Branch 1 (5→7) taken 37334 times.
|
37350 | strcmp(set_table, ctx->table->handle.table.name)) |
| 768 | 26932 | return 0; | |
| 769 | |||
| 770 | 37334 | set = netlink_delinearize_set(ctx->nlctx, nls); | |
| 771 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 37334 times.
|
37334 | if (!set) |
| 772 | ✗ | return -1; | |
| 773 | |||
| 774 | 37334 | set_name = nftnl_set_get_str(nls, NFTNL_SET_NAME); | |
| 775 | 37334 | cache_add(&set->cache, &ctx->table->set_cache, set_name); | |
| 776 | |||
| 777 | 37334 | nftnl_set_list_del(nls); | |
| 778 | 37334 | nftnl_set_free(nls); | |
| 779 | 37334 | return 0; | |
| 780 | } | ||
| 781 | |||
| 782 | 75769 | static int set_cache_init(struct netlink_ctx *ctx, struct table *table, | |
| 783 | struct nftnl_set_list *set_list) | ||
| 784 | { | ||
| 785 | 75769 | struct set_cache_dump_ctx dump_ctx = { | |
| 786 | .nlctx = ctx, | ||
| 787 | .table = table, | ||
| 788 | }; | ||
| 789 | |||
| 790 | 75769 | nftnl_set_list_foreach(set_list, set_cache_cb, &dump_ctx); | |
| 791 | |||
| 792 | 75769 | return 0; | |
| 793 | } | ||
| 794 | |||
| 795 | static struct nftnl_set_list * | ||
| 796 | 37685 | set_cache_dump(struct netlink_ctx *ctx, | |
| 797 | const struct nft_cache_filter *filter, int *err) | ||
| 798 | { | ||
| 799 | struct nftnl_set_list *set_list; | ||
| 800 | 37685 | int family = NFPROTO_UNSPEC; | |
| 801 | 37685 | const char *table = NULL; | |
| 802 | 37685 | const char *set = NULL; | |
| 803 | |||
| 804 |
2/2✓ Branch 0 (2→3) taken 37679 times.
✓ Branch 1 (2→4) taken 6 times.
|
37685 | if (filter) { |
| 805 | 37679 | family = filter->list.family; | |
| 806 | 37679 | table = filter->list.table; | |
| 807 | 37679 | set = filter->list.set; | |
| 808 | } | ||
| 809 | |||
| 810 | 37685 | set_list = mnl_nft_set_dump(ctx, family, table, set); | |
| 811 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→9) taken 37685 times.
|
37685 | if (!set_list) { |
| 812 | ✗ | if (errno == EINTR) { | |
| 813 | ✗ | *err = -1; | |
| 814 | ✗ | return NULL; | |
| 815 | } | ||
| 816 | ✗ | *err = 0; | |
| 817 | ✗ | return NULL; | |
| 818 | } | ||
| 819 | |||
| 820 | 37685 | return set_list; | |
| 821 | } | ||
| 822 | |||
| 823 | 2914 | void set_cache_add(struct set *set, struct table *table) | |
| 824 | { | ||
| 825 | 2914 | cache_add(&set->cache, &table->set_cache, set->handle.set.name); | |
| 826 | 2914 | } | |
| 827 | |||
| 828 | 7 | void set_cache_del(struct set *set) | |
| 829 | { | ||
| 830 | 7 | cache_del(&set->cache); | |
| 831 | 7 | } | |
| 832 | |||
| 833 | 40228 | struct set *set_cache_find(const struct table *table, const char *name) | |
| 834 | { | ||
| 835 | struct set *set; | ||
| 836 | uint32_t hash; | ||
| 837 | |||
| 838 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 40228 times.
|
40228 | assert(name); |
| 839 | |||
| 840 | 40228 | hash = djb_hash(name) % NFT_CACHE_HSIZE; | |
| 841 |
2/2✓ Branch 0 (9→6) taken 37050 times.
✓ Branch 1 (9→10) taken 3178 times.
|
40228 | list_for_each_entry(set, &table->set_cache.ht[hash], cache.hlist) { |
| 842 |
1/2✓ Branch 0 (6→7) taken 37050 times.
✗ Branch 1 (6→8) not taken.
|
37050 | if (!strcmp(set->handle.set.name, name)) |
| 843 | 37050 | return set; | |
| 844 | } | ||
| 845 | |||
| 846 | 3178 | return NULL; | |
| 847 | } | ||
| 848 | |||
| 849 | struct obj_cache_dump_ctx { | ||
| 850 | struct netlink_ctx *nlctx; | ||
| 851 | struct table *table; | ||
| 852 | }; | ||
| 853 | |||
| 854 | 2544 | static int obj_cache_cb(struct nftnl_obj *nlo, void *arg) | |
| 855 | { | ||
| 856 | 2544 | struct obj_cache_dump_ctx *ctx = arg; | |
| 857 | const char *obj_table; | ||
| 858 | const char *obj_name; | ||
| 859 | uint32_t obj_family; | ||
| 860 | struct obj *obj; | ||
| 861 | |||
| 862 | 2544 | obj_table = nftnl_obj_get_str(nlo, NFTNL_OBJ_TABLE); | |
| 863 | 2544 | obj_family = nftnl_obj_get_u32(nlo, NFTNL_OBJ_FAMILY); | |
| 864 | |||
| 865 |
2/2✓ Branch 0 (4→5) taken 2496 times.
✓ Branch 1 (4→6) taken 48 times.
|
2544 | if (obj_family != ctx->table->handle.family || |
| 866 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 2496 times.
|
2496 | strcmp(obj_table, ctx->table->handle.table.name)) |
| 867 | 48 | return 0; | |
| 868 | |||
| 869 | 2496 | obj = netlink_delinearize_obj(ctx->nlctx, nlo); | |
| 870 |
1/2✓ Branch 0 (8→9) taken 2496 times.
✗ Branch 1 (8→11) not taken.
|
2496 | if (obj) { |
| 871 | 2496 | obj_name = nftnl_obj_get_str(nlo, NFTNL_OBJ_NAME); | |
| 872 | 2496 | cache_add(&obj->cache, &ctx->table->obj_cache, obj_name); | |
| 873 | } | ||
| 874 | |||
| 875 | 2496 | nftnl_obj_list_del(nlo); | |
| 876 | 2496 | nftnl_obj_free(nlo); | |
| 877 | |||
| 878 | 2496 | return 0; | |
| 879 | } | ||
| 880 | |||
| 881 | 15917 | static int obj_cache_init(struct netlink_ctx *ctx, struct table *table, | |
| 882 | struct nftnl_obj_list *obj_list) | ||
| 883 | { | ||
| 884 | 15917 | struct obj_cache_dump_ctx dump_ctx = { | |
| 885 | .nlctx = ctx, | ||
| 886 | .table = table, | ||
| 887 | }; | ||
| 888 | 15917 | nftnl_obj_list_foreach(obj_list, obj_cache_cb, &dump_ctx); | |
| 889 | |||
| 890 | 15917 | return 0; | |
| 891 | } | ||
| 892 | |||
| 893 | 16311 | static struct nftnl_obj_list *obj_cache_dump(struct netlink_ctx *ctx, | |
| 894 | const struct nft_cache_filter *filter) | ||
| 895 | { | ||
| 896 | struct nftnl_obj_list *obj_list; | ||
| 897 | 16311 | int type = NFT_OBJECT_UNSPEC; | |
| 898 | 16311 | int family = NFPROTO_UNSPEC; | |
| 899 | 16311 | const char *table = NULL; | |
| 900 | 16311 | const char *obj = NULL; | |
| 901 | 16311 | bool reset = false; | |
| 902 | 16311 | bool dump = true; | |
| 903 | |||
| 904 |
2/2✓ Branch 0 (2→3) taken 16310 times.
✓ Branch 1 (2→10) taken 1 times.
|
16311 | if (filter) { |
| 905 | 16310 | family = filter->list.family; | |
| 906 |
2/2✓ Branch 0 (3→4) taken 14289 times.
✓ Branch 1 (3→5) taken 2021 times.
|
16310 | if (filter->list.table) |
| 907 | 14289 | table = filter->list.table; | |
| 908 |
2/2✓ Branch 0 (5→6) taken 404 times.
✓ Branch 1 (5→7) taken 15906 times.
|
16310 | if (filter->list.obj) { |
| 909 | 404 | obj = filter->list.obj; | |
| 910 | 404 | dump = false; | |
| 911 | } | ||
| 912 |
2/2✓ Branch 0 (7→8) taken 416 times.
✓ Branch 1 (7→9) taken 15894 times.
|
16310 | if (filter->list.obj_type) |
| 913 | 416 | type = filter->list.obj_type; | |
| 914 | |||
| 915 | 16310 | reset = filter->reset.obj; | |
| 916 | } | ||
| 917 | 16311 | obj_list = mnl_nft_obj_dump(ctx, family, table, obj, type, dump, reset); | |
| 918 |
2/2✓ Branch 0 (11→12) taken 31 times.
✓ Branch 1 (11→18) taken 16280 times.
|
16311 | if (!obj_list) { |
| 919 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 31 times.
|
31 | if (errno == EINTR) |
| 920 | ✗ | return NULL; | |
| 921 | |||
| 922 | /* old kernels do not support this, provide an empty list. */ | ||
| 923 | 31 | obj_list = nftnl_obj_list_alloc(); | |
| 924 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 31 times.
|
31 | if (!obj_list) |
| 925 | ✗ | memory_allocation_error(); | |
| 926 | |||
| 927 | 31 | return obj_list; | |
| 928 | } | ||
| 929 | |||
| 930 | 16280 | return obj_list; | |
| 931 | } | ||
| 932 | |||
| 933 | 469 | void obj_cache_add(struct obj *obj, struct table *table) | |
| 934 | { | ||
| 935 | 469 | cache_add(&obj->cache, &table->obj_cache, obj->handle.obj.name); | |
| 936 | 469 | } | |
| 937 | |||
| 938 | 1 | void obj_cache_del(struct obj *obj) | |
| 939 | { | ||
| 940 | 1 | cache_del(&obj->cache); | |
| 941 | 1 | } | |
| 942 | |||
| 943 | 817 | struct obj *obj_cache_find(const struct table *table, const char *name, | |
| 944 | uint32_t obj_type) | ||
| 945 | { | ||
| 946 | struct obj *obj; | ||
| 947 | uint32_t hash; | ||
| 948 | |||
| 949 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 817 times.
|
817 | assert(name); |
| 950 | |||
| 951 | 817 | hash = djb_hash(name) % NFT_CACHE_HSIZE; | |
| 952 |
2/2✓ Branch 0 (10→6) taken 303 times.
✓ Branch 1 (10→11) taken 522 times.
|
825 | list_for_each_entry(obj, &table->obj_cache.ht[hash], cache.hlist) { |
| 953 |
1/2✓ Branch 0 (6→7) taken 303 times.
✗ Branch 1 (6→9) not taken.
|
303 | if (!strcmp(obj->handle.obj.name, name) && |
| 954 |
2/2✓ Branch 0 (7→8) taken 295 times.
✓ Branch 1 (7→9) taken 8 times.
|
303 | obj->type == obj_type) |
| 955 | 295 | return obj; | |
| 956 | } | ||
| 957 | |||
| 958 | 522 | return NULL; | |
| 959 | } | ||
| 960 | |||
| 961 | struct ft_cache_dump_ctx { | ||
| 962 | struct netlink_ctx *nlctx; | ||
| 963 | struct table *table; | ||
| 964 | }; | ||
| 965 | |||
| 966 | 65 | static int ft_cache_cb(struct nftnl_flowtable *nlf, void *arg) | |
| 967 | { | ||
| 968 | 65 | struct ft_cache_dump_ctx *ctx = arg; | |
| 969 | struct flowtable *ft; | ||
| 970 | const char *ft_table; | ||
| 971 | const char *ft_name; | ||
| 972 | uint32_t ft_family; | ||
| 973 | |||
| 974 | 65 | ft_family = nftnl_flowtable_get_u32(nlf, NFTNL_FLOWTABLE_FAMILY); | |
| 975 | 65 | ft_table = nftnl_flowtable_get_str(nlf, NFTNL_FLOWTABLE_TABLE); | |
| 976 | |||
| 977 |
2/2✓ Branch 0 (4→5) taken 59 times.
✓ Branch 1 (4→6) taken 6 times.
|
65 | if (ft_family != ctx->table->handle.family || |
| 978 |
2/2✓ Branch 0 (5→6) taken 12 times.
✓ Branch 1 (5→7) taken 47 times.
|
59 | strcmp(ft_table, ctx->table->handle.table.name)) |
| 979 | 18 | return 0; | |
| 980 | |||
| 981 | 47 | ft = netlink_delinearize_flowtable(ctx->nlctx, nlf); | |
| 982 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 47 times.
|
47 | if (!ft) |
| 983 | ✗ | return -1; | |
| 984 | |||
| 985 | 47 | ft_name = nftnl_flowtable_get_str(nlf, NFTNL_FLOWTABLE_NAME); | |
| 986 | 47 | cache_add(&ft->cache, &ctx->table->ft_cache, ft_name); | |
| 987 | |||
| 988 | 47 | nftnl_flowtable_list_del(nlf); | |
| 989 | 47 | nftnl_flowtable_free(nlf); | |
| 990 | 47 | return 0; | |
| 991 | } | ||
| 992 | |||
| 993 | 15503 | static int ft_cache_init(struct netlink_ctx *ctx, struct table *table, | |
| 994 | struct nftnl_flowtable_list *ft_list) | ||
| 995 | { | ||
| 996 | 15503 | struct ft_cache_dump_ctx dump_ctx = { | |
| 997 | .nlctx = ctx, | ||
| 998 | .table = table, | ||
| 999 | }; | ||
| 1000 | 15503 | nftnl_flowtable_list_foreach(ft_list, ft_cache_cb, &dump_ctx); | |
| 1001 | |||
| 1002 | 15503 | return 0; | |
| 1003 | } | ||
| 1004 | |||
| 1005 | static struct nftnl_flowtable_list * | ||
| 1006 | 15899 | ft_cache_dump(struct netlink_ctx *ctx, const struct nft_cache_filter *filter) | |
| 1007 | { | ||
| 1008 | struct nftnl_flowtable_list *ft_list; | ||
| 1009 | 15899 | int family = NFPROTO_UNSPEC; | |
| 1010 | 15899 | const char *table = NULL; | |
| 1011 | 15899 | const char *ft = NULL; | |
| 1012 | |||
| 1013 |
2/2✓ Branch 0 (2→3) taken 15897 times.
✓ Branch 1 (2→4) taken 2 times.
|
15899 | if (filter) { |
| 1014 | 15897 | family = filter->list.family; | |
| 1015 | 15897 | table = filter->list.table; | |
| 1016 | 15897 | ft = filter->list.ft; | |
| 1017 | } | ||
| 1018 | |||
| 1019 | 15899 | ft_list = mnl_nft_flowtable_dump(ctx, family, table, ft); | |
| 1020 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→12) taken 15899 times.
|
15899 | if (!ft_list) { |
| 1021 | ✗ | if (errno == EINTR) | |
| 1022 | ✗ | return NULL; | |
| 1023 | |||
| 1024 | /* old kernels do not support this, provide an empty list. */ | ||
| 1025 | ✗ | ft_list = nftnl_flowtable_list_alloc(); | |
| 1026 | ✗ | if (!ft_list) | |
| 1027 | ✗ | memory_allocation_error(); | |
| 1028 | |||
| 1029 | ✗ | return ft_list; | |
| 1030 | } | ||
| 1031 | |||
| 1032 | 15899 | return ft_list; | |
| 1033 | } | ||
| 1034 | |||
| 1035 | 100 | void ft_cache_add(struct flowtable *ft, struct table *table) | |
| 1036 | { | ||
| 1037 | 100 | cache_add(&ft->cache, &table->ft_cache, ft->handle.flowtable.name); | |
| 1038 | 100 | } | |
| 1039 | |||
| 1040 | ✗ | void ft_cache_del(struct flowtable *ft) | |
| 1041 | { | ||
| 1042 | ✗ | cache_del(&ft->cache); | |
| 1043 | ✗ | } | |
| 1044 | |||
| 1045 | 139 | struct flowtable *ft_cache_find(const struct table *table, const char *name) | |
| 1046 | { | ||
| 1047 | struct flowtable *ft; | ||
| 1048 | uint32_t hash; | ||
| 1049 | |||
| 1050 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 139 times.
|
139 | assert(name); |
| 1051 | |||
| 1052 | 139 | hash = djb_hash(name) % NFT_CACHE_HSIZE; | |
| 1053 |
2/2✓ Branch 0 (9→6) taken 4 times.
✓ Branch 1 (9→10) taken 135 times.
|
139 | list_for_each_entry(ft, &table->ft_cache.ht[hash], cache.hlist) { |
| 1054 |
1/2✓ Branch 0 (6→7) taken 4 times.
✗ Branch 1 (6→8) not taken.
|
4 | if (!strcmp(ft->handle.flowtable.name, name)) |
| 1055 | 4 | return ft; | |
| 1056 | } | ||
| 1057 | |||
| 1058 | 135 | return NULL; | |
| 1059 | } | ||
| 1060 | |||
| 1061 | 39978 | static int cache_init_tables(struct netlink_ctx *ctx, struct handle *h, | |
| 1062 | struct nft_cache *cache, | ||
| 1063 | const struct nft_cache_filter *filter) | ||
| 1064 | { | ||
| 1065 | struct table *table, *next; | ||
| 1066 | int ret; | ||
| 1067 | |||
| 1068 | 39978 | ret = netlink_list_tables(ctx, h, filter); | |
| 1069 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 39978 times.
|
39978 | if (ret < 0) |
| 1070 | ✗ | return -1; | |
| 1071 | |||
| 1072 |
2/2✓ Branch 0 (9→6) taken 78943 times.
✓ Branch 1 (9→10) taken 39978 times.
|
118921 | list_for_each_entry_safe(table, next, &ctx->list, list) { |
| 1073 | 78943 | list_del(&table->list); | |
| 1074 | 78943 | table_cache_add(table, cache); | |
| 1075 | } | ||
| 1076 | |||
| 1077 | 39978 | return 0; | |
| 1078 | } | ||
| 1079 | |||
| 1080 | 15502 | static int rule_init_cache(struct netlink_ctx *ctx, struct table *table, | |
| 1081 | const struct nft_cache_filter *filter) | ||
| 1082 | { | ||
| 1083 | struct rule *rule, *nrule; | ||
| 1084 | struct chain *chain; | ||
| 1085 | int ret; | ||
| 1086 | |||
| 1087 | 15502 | ret = rule_cache_dump(ctx, &table->handle, filter); | |
| 1088 | |||
| 1089 |
2/2✓ Branch 0 (11→4) taken 56642 times.
✓ Branch 1 (11→12) taken 15502 times.
|
72144 | list_for_each_entry_safe(rule, nrule, &ctx->list, list) { |
| 1090 | 56642 | chain = chain_cache_find(table, rule->handle.chain.name); | |
| 1091 |
2/2✓ Branch 0 (5→6) taken 55 times.
✓ Branch 1 (5→7) taken 56587 times.
|
56642 | if (!chain) |
| 1092 | 55 | chain = chain_binding_lookup(table, | |
| 1093 | rule->handle.chain.name); | ||
| 1094 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 56642 times.
|
56642 | if (!chain) |
| 1095 | ✗ | goto err_ctx_list; | |
| 1096 | |||
| 1097 | 56642 | list_move_tail(&rule->list, &chain->rules); | |
| 1098 | } | ||
| 1099 | |||
| 1100 | 15502 | return ret; | |
| 1101 | |||
| 1102 | ✗ | err_ctx_list: | |
| 1103 | ✗ | list_for_each_entry_safe(rule, nrule, &ctx->list, list) { | |
| 1104 | ✗ | list_del(&rule->list); | |
| 1105 | ✗ | rule_free(rule); | |
| 1106 | } | ||
| 1107 | ✗ | errno = EINTR; | |
| 1108 | |||
| 1109 | ✗ | return -1; | |
| 1110 | } | ||
| 1111 | |||
| 1112 | 1279 | static int implicit_chain_cache(struct netlink_ctx *ctx, struct table *table, | |
| 1113 | const char *chain_name) | ||
| 1114 | { | ||
| 1115 | 1279 | struct nft_cache_filter filter = {}; | |
| 1116 | struct chain *chain; | ||
| 1117 | 1279 | int ret = 0; | |
| 1118 | |||
| 1119 |
2/2✓ Branch 0 (5→3) taken 3 times.
✓ Branch 1 (5→6) taken 1279 times.
|
1282 | list_for_each_entry(chain, &table->chain_bindings, cache.list) { |
| 1120 | 3 | filter.list.table = table->handle.table.name; | |
| 1121 | 3 | filter.list.chain = chain->handle.chain.name; | |
| 1122 | |||
| 1123 | 3 | ret = rule_init_cache(ctx, table, &filter); | |
| 1124 | } | ||
| 1125 | |||
| 1126 | 1279 | return ret; | |
| 1127 | } | ||
| 1128 | |||
| 1129 | 39978 | static int cache_init_objects(struct netlink_ctx *ctx, unsigned int flags, | |
| 1130 | const struct nft_cache_filter *filter) | ||
| 1131 | { | ||
| 1132 | 39978 | struct nftnl_flowtable_list *ft_list = NULL; | |
| 1133 | 39978 | struct nftnl_chain_list *chain_list = NULL; | |
| 1134 | 39978 | struct nftnl_set_list *set_list = NULL; | |
| 1135 | 39978 | struct nftnl_obj_list *obj_list = NULL; | |
| 1136 | struct table *table; | ||
| 1137 | struct set *set; | ||
| 1138 | 39978 | int ret = 0; | |
| 1139 | |||
| 1140 |
2/2✓ Branch 0 (2→3) taken 15915 times.
✓ Branch 1 (2→6) taken 24063 times.
|
39978 | if (flags & NFT_CACHE_CHAIN_BIT) { |
| 1141 | 15915 | chain_list = chain_cache_dump(ctx, filter, &ret); | |
| 1142 |
2/2✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→6) taken 15914 times.
|
15915 | if (!chain_list) |
| 1143 | 1 | return -1; | |
| 1144 | } | ||
| 1145 |
2/2✓ Branch 0 (6→7) taken 37685 times.
✓ Branch 1 (6→10) taken 2292 times.
|
39977 | if (flags & NFT_CACHE_SET_BIT) { |
| 1146 | 37685 | set_list = set_cache_dump(ctx, filter, &ret); | |
| 1147 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 37685 times.
|
37685 | if (!set_list) { |
| 1148 | ✗ | ret = -1; | |
| 1149 | ✗ | goto cache_fails; | |
| 1150 | } | ||
| 1151 | } | ||
| 1152 |
2/2✓ Branch 0 (10→11) taken 16311 times.
✓ Branch 1 (10→14) taken 23666 times.
|
39977 | if (flags & NFT_CACHE_OBJECT_BIT) { |
| 1153 | 16311 | obj_list = obj_cache_dump(ctx, filter); | |
| 1154 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 16311 times.
|
16311 | if (!obj_list) { |
| 1155 | ✗ | ret = -1; | |
| 1156 | ✗ | goto cache_fails; | |
| 1157 | } | ||
| 1158 | } | ||
| 1159 |
2/2✓ Branch 0 (14→15) taken 15899 times.
✓ Branch 1 (14→18) taken 24078 times.
|
39977 | if (flags & NFT_CACHE_FLOWTABLE_BIT) { |
| 1160 | 15899 | ft_list = ft_cache_dump(ctx, filter); | |
| 1161 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 15899 times.
|
15899 | if (!ft_list) { |
| 1162 | ✗ | ret = -1; | |
| 1163 | ✗ | goto cache_fails; | |
| 1164 | } | ||
| 1165 | } | ||
| 1166 | |||
| 1167 |
2/2✓ Branch 0 (73→19) taken 78942 times.
✓ Branch 1 (73→74) taken 39976 times.
|
118918 | list_for_each_entry(table, &ctx->nft->cache.table_cache.list, cache.list) { |
| 1168 |
2/2✓ Branch 0 (19→20) taken 75769 times.
✓ Branch 1 (19→23) taken 3173 times.
|
78942 | if (flags & NFT_CACHE_SET_BIT) { |
| 1169 | 75769 | ret = set_cache_init(ctx, table, set_list); | |
| 1170 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 75769 times.
|
75769 | if (ret < 0) |
| 1171 | ✗ | goto cache_fails; | |
| 1172 | } | ||
| 1173 |
2/2✓ Branch 0 (23→24) taken 17597 times.
✓ Branch 1 (23→37) taken 61345 times.
|
78942 | if (flags & NFT_CACHE_SETELEM_BIT) { |
| 1174 |
2/2✓ Branch 0 (36→25) taken 10384 times.
✓ Branch 1 (36→50) taken 17597 times.
|
27981 | list_for_each_entry(set, &table->set_cache.list, cache.list) { |
| 1175 |
1/2✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→28) taken 10384 times.
|
10384 | if (cache_filter_find(filter, &set->handle)) |
| 1176 | ✗ | continue; | |
| 1177 |
2/2✓ Branch 0 (29→30) taken 6934 times.
✓ Branch 1 (29→32) taken 3450 times.
|
10384 | if (!set_is_anonymous(set->flags) && |
| 1178 |
2/2✓ Branch 0 (30→31) taken 456 times.
✓ Branch 1 (30→32) taken 6478 times.
|
6934 | flags & NFT_CACHE_TERSE) |
| 1179 | 456 | continue; | |
| 1180 | |||
| 1181 | 19856 | ret = netlink_list_setelems(ctx, &set->handle, | |
| 1182 | 9928 | set, filter->reset.elem); | |
| 1183 |
1/2✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 9928 times.
|
9928 | if (ret < 0) |
| 1184 | ✗ | goto cache_fails; | |
| 1185 | } | ||
| 1186 |
2/2✓ Branch 0 (37→38) taken 4416 times.
✓ Branch 1 (37→50) taken 56929 times.
|
61345 | } else if (flags & NFT_CACHE_SETELEM_MAYBE) { |
| 1187 |
2/2✓ Branch 0 (49→39) taken 9732 times.
✓ Branch 1 (49→50) taken 4416 times.
|
14148 | list_for_each_entry(set, &table->set_cache.list, cache.list) { |
| 1188 |
2/2✓ Branch 0 (40→41) taken 184 times.
✓ Branch 1 (40→42) taken 9548 times.
|
9732 | if (cache_filter_find(filter, &set->handle)) |
| 1189 | 184 | continue; | |
| 1190 | |||
| 1191 |
2/2✓ Branch 0 (43→44) taken 7103 times.
✓ Branch 1 (43→45) taken 2445 times.
|
9548 | if (!set_is_non_concat_range(set)) |
| 1192 | 7103 | continue; | |
| 1193 | |||
| 1194 | 4890 | ret = netlink_list_setelems(ctx, &set->handle, | |
| 1195 | 2445 | set, filter->reset.elem); | |
| 1196 |
1/2✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→48) taken 2445 times.
|
2445 | if (ret < 0) |
| 1197 | ✗ | goto cache_fails; | |
| 1198 | } | ||
| 1199 | } | ||
| 1200 |
2/2✓ Branch 0 (50→51) taken 15511 times.
✓ Branch 1 (50→54) taken 63431 times.
|
78942 | if (flags & NFT_CACHE_CHAIN_BIT) { |
| 1201 | 15511 | ret = chain_cache_init(ctx, table, chain_list); | |
| 1202 |
1/2✗ Branch 0 (52→53) not taken.
✓ Branch 1 (52→54) taken 15511 times.
|
15511 | if (ret < 0) |
| 1203 | ✗ | goto cache_fails; | |
| 1204 | } | ||
| 1205 |
2/2✓ Branch 0 (54→55) taken 15503 times.
✓ Branch 1 (54→58) taken 63439 times.
|
78942 | if (flags & NFT_CACHE_FLOWTABLE_BIT) { |
| 1206 | 15503 | ret = ft_cache_init(ctx, table, ft_list); | |
| 1207 |
1/2✗ Branch 0 (56→57) not taken.
✓ Branch 1 (56→58) taken 15503 times.
|
15503 | if (ret < 0) |
| 1208 | ✗ | goto cache_fails; | |
| 1209 | } | ||
| 1210 |
2/2✓ Branch 0 (58→59) taken 15917 times.
✓ Branch 1 (58→62) taken 63025 times.
|
78942 | if (flags & NFT_CACHE_OBJECT_BIT) { |
| 1211 | 15917 | ret = obj_cache_init(ctx, table, obj_list); | |
| 1212 |
1/2✗ Branch 0 (60→61) not taken.
✓ Branch 1 (60→62) taken 15917 times.
|
15917 | if (ret < 0) |
| 1213 | ✗ | goto cache_fails; | |
| 1214 | } | ||
| 1215 | |||
| 1216 |
2/2✓ Branch 0 (62→63) taken 15499 times.
✓ Branch 1 (62→72) taken 63443 times.
|
78942 | if (flags & NFT_CACHE_RULE_BIT) { |
| 1217 | 15499 | ret = rule_init_cache(ctx, table, filter); | |
| 1218 |
2/2✓ Branch 0 (64→65) taken 1 times.
✓ Branch 1 (64→66) taken 15498 times.
|
15499 | if (ret < 0) |
| 1219 | 1 | goto cache_fails; | |
| 1220 | |||
| 1221 |
5/6✓ Branch 0 (66→67) taken 15498 times.
✗ Branch 1 (66→72) not taken.
✓ Branch 2 (67→68) taken 13426 times.
✓ Branch 3 (67→72) taken 2072 times.
✓ Branch 4 (68→69) taken 1279 times.
✓ Branch 5 (68→72) taken 12147 times.
|
15498 | if (filter && filter->list.table && filter->list.chain) { |
| 1222 | 1279 | ret = implicit_chain_cache(ctx, table, filter->list.chain); | |
| 1223 |
1/2✗ Branch 0 (70→71) not taken.
✓ Branch 1 (70→72) taken 1279 times.
|
1279 | if (ret < 0) |
| 1224 | ✗ | goto cache_fails; | |
| 1225 | } | ||
| 1226 | } | ||
| 1227 | } | ||
| 1228 | |||
| 1229 | 39976 | cache_fails: | |
| 1230 |
2/2✓ Branch 0 (75→76) taken 37685 times.
✓ Branch 1 (75→77) taken 2292 times.
|
39977 | if (set_list) |
| 1231 | 37685 | nftnl_set_list_free(set_list); | |
| 1232 |
2/2✓ Branch 0 (77→78) taken 16311 times.
✓ Branch 1 (77→79) taken 23666 times.
|
39977 | if (obj_list) |
| 1233 | 16311 | nftnl_obj_list_free(obj_list); | |
| 1234 |
2/2✓ Branch 0 (79→80) taken 15899 times.
✓ Branch 1 (79→81) taken 24078 times.
|
39977 | if (ft_list) |
| 1235 | 15899 | nftnl_flowtable_list_free(ft_list); | |
| 1236 | |||
| 1237 |
2/2✓ Branch 0 (81→82) taken 15914 times.
✓ Branch 1 (81→83) taken 24063 times.
|
39977 | if (flags & NFT_CACHE_CHAIN_BIT) |
| 1238 | 15914 | nftnl_chain_list_free(chain_list); | |
| 1239 | |||
| 1240 | 39977 | return ret; | |
| 1241 | } | ||
| 1242 | |||
| 1243 | 54890 | static int nft_cache_init(struct netlink_ctx *ctx, unsigned int flags, | |
| 1244 | const struct nft_cache_filter *filter) | ||
| 1245 | { | ||
| 1246 | 54890 | struct handle handle = { | |
| 1247 | .family = NFPROTO_UNSPEC, | ||
| 1248 | }; | ||
| 1249 | int ret; | ||
| 1250 | |||
| 1251 |
2/2✓ Branch 0 (2→3) taken 14912 times.
✓ Branch 1 (2→4) taken 39978 times.
|
54890 | if (flags == NFT_CACHE_EMPTY) |
| 1252 | 14912 | return 0; | |
| 1253 | |||
| 1254 | /* assume NFT_CACHE_TABLE is always set. */ | ||
| 1255 | 39978 | ret = cache_init_tables(ctx, &handle, &ctx->nft->cache, filter); | |
| 1256 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 39978 times.
|
39978 | if (ret < 0) |
| 1257 | ✗ | return ret; | |
| 1258 | 39978 | ret = cache_init_objects(ctx, flags, filter); | |
| 1259 |
2/2✓ Branch 0 (8→9) taken 2 times.
✓ Branch 1 (8→10) taken 39976 times.
|
39978 | if (ret < 0) |
| 1260 | 2 | return ret; | |
| 1261 | |||
| 1262 | 39976 | return 0; | |
| 1263 | } | ||
| 1264 | |||
| 1265 | 26654 | static bool nft_cache_is_complete(struct nft_cache *cache, unsigned int flags) | |
| 1266 | { | ||
| 1267 | 26654 | return (cache->flags & flags) == flags; | |
| 1268 | } | ||
| 1269 | |||
| 1270 | 56387 | static bool nft_cache_needs_refresh(struct nft_cache *cache, unsigned int flags) | |
| 1271 | { | ||
| 1272 |
2/2✓ Branch 0 (2→3) taken 43384 times.
✓ Branch 1 (2→4) taken 13003 times.
|
99771 | return (cache->flags & NFT_CACHE_REFRESH) || |
| 1273 |
2/2✓ Branch 0 (3→4) taken 16730 times.
✓ Branch 1 (3→5) taken 26654 times.
|
43384 | (flags & NFT_CACHE_REFRESH); |
| 1274 | } | ||
| 1275 | |||
| 1276 | 3171 | static bool nft_cache_is_updated(struct nft_cache *cache, uint16_t genid) | |
| 1277 | { | ||
| 1278 |
3/4✓ Branch 0 (2→3) taken 3171 times.
✗ Branch 1 (2→5) not taken.
✓ Branch 2 (3→4) taken 122 times.
✓ Branch 3 (3→5) taken 3049 times.
|
3171 | return genid && genid == cache->genid; |
| 1279 | } | ||
| 1280 | |||
| 1281 | 108417 | bool nft_cache_needs_update(struct nft_cache *cache) | |
| 1282 | { | ||
| 1283 | 108417 | return cache->flags & NFT_CACHE_UPDATE; | |
| 1284 | } | ||
| 1285 | |||
| 1286 | 56384 | int nft_cache_update(struct nft_ctx *nft, unsigned int flags, | |
| 1287 | struct list_head *msgs, | ||
| 1288 | const struct nft_cache_filter *filter) | ||
| 1289 | { | ||
| 1290 | 56384 | struct netlink_ctx ctx = { | |
| 1291 | .list = LIST_HEAD_INIT(ctx.list), | ||
| 1292 | .nft = nft, | ||
| 1293 | .msgs = msgs, | ||
| 1294 | }; | ||
| 1295 | 56384 | struct nft_cache *cache = &nft->cache; | |
| 1296 | uint32_t genid, genid_stop, oldflags; | ||
| 1297 | int ret; | ||
| 1298 | 3 | replay: | |
| 1299 | 56387 | ctx.seqnum = cache->seqnum++; | |
| 1300 | 56387 | genid = mnl_genid_get(&ctx); | |
| 1301 |
4/4✓ Branch 0 (6→7) taken 26654 times.
✓ Branch 1 (6→12) taken 29733 times.
✓ Branch 2 (8→9) taken 3171 times.
✓ Branch 3 (8→12) taken 23483 times.
|
83041 | if (!nft_cache_needs_refresh(cache, flags) && |
| 1302 |
2/2✓ Branch 0 (10→11) taken 122 times.
✓ Branch 1 (10→12) taken 3049 times.
|
29825 | nft_cache_is_complete(cache, flags) && |
| 1303 | 3171 | nft_cache_is_updated(cache, genid)) | |
| 1304 | 122 | return 0; | |
| 1305 | |||
| 1306 |
2/2✓ Branch 0 (12→13) taken 41410 times.
✓ Branch 1 (12→14) taken 14855 times.
|
56265 | if (cache->genid) |
| 1307 | 41410 | nft_cache_release(cache); | |
| 1308 | |||
| 1309 |
2/2✓ Branch 0 (14→15) taken 1375 times.
✓ Branch 1 (14→18) taken 54890 times.
|
56265 | if (flags & NFT_CACHE_FLUSHED) { |
| 1310 | 1375 | oldflags = flags; | |
| 1311 | 1375 | flags = NFT_CACHE_EMPTY; | |
| 1312 |
2/2✓ Branch 0 (15→16) taken 4 times.
✓ Branch 1 (15→17) taken 1371 times.
|
1375 | if (oldflags & NFT_CACHE_UPDATE) |
| 1313 | 4 | flags |= NFT_CACHE_UPDATE; | |
| 1314 | 1375 | goto skip; | |
| 1315 | } | ||
| 1316 | |||
| 1317 | 54890 | ret = nft_cache_init(&ctx, flags, filter); | |
| 1318 |
2/2✓ Branch 0 (19→20) taken 2 times.
✓ Branch 1 (19→28) taken 54888 times.
|
54890 | if (ret < 0) { |
| 1319 |
1/2✓ Branch 0 (20→21) taken 2 times.
✗ Branch 1 (20→23) not taken.
|
2 | if (errno == EINTR) { |
| 1320 | 2 | nft_cache_release(cache); | |
| 1321 | 2 | goto replay; | |
| 1322 | } | ||
| 1323 | |||
| 1324 | ✗ | erec_queue(error(&netlink_location, "cache initialization failed: %s", | |
| 1325 | strerror(errno)), | ||
| 1326 | msgs); | ||
| 1327 | ✗ | nft_cache_release(cache); | |
| 1328 | |||
| 1329 | ✗ | return -1; | |
| 1330 | } | ||
| 1331 | |||
| 1332 | 54888 | genid_stop = mnl_genid_get(&ctx); | |
| 1333 |
2/2✓ Branch 0 (29→30) taken 1 times.
✓ Branch 1 (29→32) taken 54887 times.
|
54888 | if (genid != genid_stop) { |
| 1334 | 1 | nft_cache_release(cache); | |
| 1335 | 1 | goto replay; | |
| 1336 | } | ||
| 1337 | 54887 | skip: | |
| 1338 | 56262 | cache->genid = genid; | |
| 1339 | 56262 | cache->flags = flags; | |
| 1340 | 56262 | return 0; | |
| 1341 | } | ||
| 1342 | |||
| 1343 | 58759 | static void nft_cache_flush(struct cache *table_cache) | |
| 1344 | { | ||
| 1345 | struct table *table, *next; | ||
| 1346 | |||
| 1347 |
2/2✓ Branch 0 (6→3) taken 82035 times.
✓ Branch 1 (6→7) taken 58759 times.
|
140794 | list_for_each_entry_safe(table, next, &table_cache->list, cache.list) { |
| 1348 | 82035 | table_cache_del(table); | |
| 1349 | 82035 | table_free(table); | |
| 1350 | } | ||
| 1351 | 58759 | } | |
| 1352 | |||
| 1353 | 58759 | void nft_cache_release(struct nft_cache *cache) | |
| 1354 | { | ||
| 1355 | 58759 | nft_cache_flush(&cache->table_cache); | |
| 1356 | 58759 | cache->genid = 0; | |
| 1357 | 58759 | cache->flags = NFT_CACHE_EMPTY; | |
| 1358 | 58759 | } | |
| 1359 | |||
| 1360 | 353904 | void cache_init(struct cache *cache) | |
| 1361 | { | ||
| 1362 | int i; | ||
| 1363 | |||
| 1364 | 353904 | cache->ht = xmalloc(sizeof(struct list_head) * NFT_CACHE_HSIZE); | |
| 1365 |
2/2✓ Branch 0 (6→4) taken 2899181568 times.
✓ Branch 1 (6→7) taken 353904 times.
|
2899535472 | for (i = 0; i < NFT_CACHE_HSIZE; i++) |
| 1366 | 2899181568 | init_list_head(&cache->ht[i]); | |
| 1367 | |||
| 1368 | 353904 | init_list_head(&cache->list); | |
| 1369 | 353904 | } | |
| 1370 | |||
| 1371 | 353904 | void cache_free(struct cache *cache) | |
| 1372 | { | ||
| 1373 | 353904 | free(cache->ht); | |
| 1374 | 353904 | } | |
| 1375 | |||
| 1376 | 152364 | void cache_add(struct cache_item *item, struct cache *cache, const char *name) | |
| 1377 | { | ||
| 1378 | uint32_t hash; | ||
| 1379 | |||
| 1380 | 152364 | hash = djb_hash(name) % NFT_CACHE_HSIZE; | |
| 1381 | 152364 | list_add_tail(&item->hlist, &cache->ht[hash]); | |
| 1382 | 152364 | list_add_tail(&item->list, &cache->list); | |
| 1383 | 152364 | } | |
| 1384 | |||
| 1385 | 82312 | void cache_del(struct cache_item *item) | |
| 1386 | { | ||
| 1387 | 82312 | list_del(&item->hlist); | |
| 1388 | 82312 | list_del(&item->list); | |
| 1389 | 82312 | } | |
| 1390 |