| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 as | ||
| 6 | * published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <nft.h> | ||
| 12 | |||
| 13 | #include <stddef.h> | ||
| 14 | #include <stdio.h> | ||
| 15 | #include <inttypes.h> | ||
| 16 | #include <syslog.h> | ||
| 17 | #include <rule.h> | ||
| 18 | |||
| 19 | #include <arpa/inet.h> | ||
| 20 | #include <linux/netfilter.h> | ||
| 21 | #include <linux/netfilter/nf_log.h> | ||
| 22 | #include <netinet/ip_icmp.h> | ||
| 23 | #include <netinet/icmp6.h> | ||
| 24 | #include <statement.h> | ||
| 25 | #include <tcpopt.h> | ||
| 26 | #include <utils.h> | ||
| 27 | #include <list.h> | ||
| 28 | #include <xt.h> | ||
| 29 | #include <json.h> | ||
| 30 | |||
| 31 | #include <netinet/in.h> | ||
| 32 | #include <linux/netfilter/nf_nat.h> | ||
| 33 | #include <linux/netfilter/nf_log.h> | ||
| 34 | #include <linux/netfilter/nf_synproxy.h> | ||
| 35 | |||
| 36 | 396698 | struct stmt *stmt_alloc(const struct location *loc, const struct stmt_ops *ops) | |
| 37 | { | ||
| 38 | struct stmt *stmt; | ||
| 39 | |||
| 40 | 396698 | stmt = xzalloc(sizeof(*stmt)); | |
| 41 | 396698 | init_list_head(&stmt->list); | |
| 42 | 396698 | stmt->location = *loc; | |
| 43 | 396698 | stmt->type = ops->type; | |
| 44 | 396698 | return stmt; | |
| 45 | } | ||
| 46 | |||
| 47 | 396730 | void stmt_free(struct stmt *stmt) | |
| 48 | { | ||
| 49 | const struct stmt_ops *ops; | ||
| 50 | |||
| 51 |
2/2✓ Branch 0 (2→3) taken 17 times.
✓ Branch 1 (2→4) taken 396713 times.
|
396730 | if (stmt == NULL) |
| 52 | 17 | return; | |
| 53 | |||
| 54 | 396713 | ops = stmt_ops(stmt); | |
| 55 |
2/2✓ Branch 0 (5→6) taken 206531 times.
✓ Branch 1 (5→7) taken 190182 times.
|
396713 | if (ops->destroy) |
| 56 | 206531 | ops->destroy(stmt); | |
| 57 | 396713 | free(stmt); | |
| 58 | } | ||
| 59 | |||
| 60 | 354095 | void stmt_list_free(struct list_head *list) | |
| 61 | { | ||
| 62 | struct stmt *i, *next; | ||
| 63 | |||
| 64 |
2/2✓ Branch 0 (6→3) taken 198530 times.
✓ Branch 1 (6→7) taken 354095 times.
|
552625 | list_for_each_entry_safe(i, next, list, list) { |
| 65 | 198530 | list_del(&i->list); | |
| 66 | 198530 | stmt_free(i); | |
| 67 | } | ||
| 68 | 354095 | } | |
| 69 | |||
| 70 | 4433 | void stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 71 | { | ||
| 72 | 4433 | const struct stmt_ops *ops = stmt_ops(stmt); | |
| 73 | |||
| 74 | 4433 | ops->print(stmt, octx); | |
| 75 | 4433 | } | |
| 76 | |||
| 77 | 31764 | static void expr_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 78 | { | ||
| 79 | 31764 | expr_print(stmt->expr, octx); | |
| 80 | 31764 | } | |
| 81 | |||
| 82 | 197655 | static void expr_stmt_destroy(struct stmt *stmt) | |
| 83 | { | ||
| 84 | 197655 | expr_free(stmt->expr); | |
| 85 | 197655 | } | |
| 86 | |||
| 87 | static const struct stmt_ops expr_stmt_ops = { | ||
| 88 | .type = STMT_EXPRESSION, | ||
| 89 | .name = "expression", | ||
| 90 | .print = expr_stmt_print, | ||
| 91 | .json = expr_stmt_json, | ||
| 92 | .destroy = expr_stmt_destroy, | ||
| 93 | }; | ||
| 94 | |||
| 95 | 175495 | struct stmt *expr_stmt_alloc(const struct location *loc, struct expr *expr) | |
| 96 | { | ||
| 97 | struct stmt *stmt; | ||
| 98 | |||
| 99 | 175495 | stmt = stmt_alloc(loc, &expr_stmt_ops); | |
| 100 | 175495 | stmt->expr = expr; | |
| 101 | 175495 | return stmt; | |
| 102 | } | ||
| 103 | |||
| 104 | static const struct stmt_ops verdict_stmt_ops = { | ||
| 105 | .type = STMT_VERDICT, | ||
| 106 | .name = "verdict", | ||
| 107 | .print = expr_stmt_print, | ||
| 108 | .json = verdict_stmt_json, | ||
| 109 | .destroy = expr_stmt_destroy, | ||
| 110 | }; | ||
| 111 | |||
| 112 | 22003 | struct stmt *verdict_stmt_alloc(const struct location *loc, struct expr *expr) | |
| 113 | { | ||
| 114 | struct stmt *stmt; | ||
| 115 | |||
| 116 | 22003 | stmt = stmt_alloc(loc, &verdict_stmt_ops); | |
| 117 | 22003 | stmt->expr = expr; | |
| 118 | 22003 | return stmt; | |
| 119 | } | ||
| 120 | |||
| 121 | 27 | static const char *chain_verdict(const struct expr *expr) | |
| 122 | { | ||
| 123 |
1/3✓ Branch 0 (2→3) taken 27 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (2→5) not taken.
|
27 | switch (expr->verdict) { |
| 124 | 27 | case NFT_JUMP: | |
| 125 | 27 | return "jump"; | |
| 126 | ✗ | case NFT_GOTO: | |
| 127 | ✗ | return "goto"; | |
| 128 | ✗ | default: | |
| 129 | ✗ | BUG("unknown chain verdict"); | |
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | 27 | static void chain_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 134 | { | ||
| 135 | 27 | nft_print(octx, "%s {\n", chain_verdict(stmt->chain.expr)); | |
| 136 | 27 | chain_rules_print(stmt->chain.chain, octx, "\t"); | |
| 137 | 27 | nft_print(octx, "\t\t}"); | |
| 138 | 27 | } | |
| 139 | |||
| 140 | 91 | static void chain_stmt_destroy(struct stmt *stmt) | |
| 141 | { | ||
| 142 | 91 | expr_free(stmt->chain.expr); | |
| 143 | 91 | chain_free(stmt->chain.chain); | |
| 144 | 91 | } | |
| 145 | |||
| 146 | static const struct stmt_ops chain_stmt_ops = { | ||
| 147 | .type = STMT_CHAIN, | ||
| 148 | .name = "chain", | ||
| 149 | .print = chain_stmt_print, | ||
| 150 | .destroy = chain_stmt_destroy, | ||
| 151 | }; | ||
| 152 | |||
| 153 | 91 | struct stmt *chain_stmt_alloc(const struct location *loc, struct chain *chain, | |
| 154 | enum nft_verdicts verdict) | ||
| 155 | { | ||
| 156 | struct stmt *stmt; | ||
| 157 | |||
| 158 | 91 | stmt = stmt_alloc(loc, &chain_stmt_ops); | |
| 159 | 91 | stmt->chain.chain = chain; | |
| 160 | 91 | stmt->chain.expr = verdict_expr_alloc(loc, verdict, NULL); | |
| 161 | 91 | stmt->chain.expr->chain_id = chain->handle.chain_id; | |
| 162 | |||
| 163 | 91 | return stmt; | |
| 164 | } | ||
| 165 | |||
| 166 | ✗ | static void meter_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 167 | { | ||
| 168 | ✗ | unsigned int flags = octx->flags; | |
| 169 | |||
| 170 | ✗ | nft_print(octx, "meter "); | |
| 171 | ✗ | if (stmt->meter.set) { | |
| 172 | ✗ | expr_print(stmt->meter.set, octx); | |
| 173 | ✗ | nft_print(octx, " "); | |
| 174 | } | ||
| 175 | ✗ | nft_print(octx, "size %u { ", stmt->meter.size); | |
| 176 | ✗ | expr_print(stmt->meter.key, octx); | |
| 177 | ✗ | nft_print(octx, " "); | |
| 178 | |||
| 179 | ✗ | octx->flags |= NFT_CTX_OUTPUT_STATELESS; | |
| 180 | ✗ | stmt_print(stmt->meter.stmt, octx); | |
| 181 | ✗ | octx->flags = flags; | |
| 182 | |||
| 183 | ✗ | nft_print(octx, " }"); | |
| 184 | |||
| 185 | ✗ | } | |
| 186 | |||
| 187 | 7 | static void meter_stmt_destroy(struct stmt *stmt) | |
| 188 | { | ||
| 189 | 7 | expr_free(stmt->meter.key); | |
| 190 | 7 | expr_free(stmt->meter.set); | |
| 191 | 7 | stmt_free(stmt->meter.stmt); | |
| 192 | 7 | free_const(stmt->meter.name); | |
| 193 | 7 | } | |
| 194 | |||
| 195 | static const struct stmt_ops meter_stmt_ops = { | ||
| 196 | .type = STMT_METER, | ||
| 197 | .name = "meter", | ||
| 198 | .print = meter_stmt_print, | ||
| 199 | .json = meter_stmt_json, | ||
| 200 | .destroy = meter_stmt_destroy, | ||
| 201 | }; | ||
| 202 | |||
| 203 | 7 | struct stmt *meter_stmt_alloc(const struct location *loc) | |
| 204 | { | ||
| 205 | 7 | return stmt_alloc(loc, &meter_stmt_ops); | |
| 206 | } | ||
| 207 | |||
| 208 | 15 | static void connlimit_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 209 | { | ||
| 210 | 15 | nft_print(octx, "ct count %s%u", | |
| 211 |
2/2✓ Branch 0 (2→3) taken 12 times.
✓ Branch 1 (2→4) taken 3 times.
|
15 | stmt->connlimit.flags ? "over " : "", stmt->connlimit.count); |
| 212 | 15 | } | |
| 213 | |||
| 214 | static const struct stmt_ops connlimit_stmt_ops = { | ||
| 215 | .type = STMT_CONNLIMIT, | ||
| 216 | .name = "connlimit", | ||
| 217 | .print = connlimit_stmt_print, | ||
| 218 | .json = connlimit_stmt_json, | ||
| 219 | }; | ||
| 220 | |||
| 221 | 65 | struct stmt *connlimit_stmt_alloc(const struct location *loc) | |
| 222 | { | ||
| 223 | struct stmt *stmt; | ||
| 224 | |||
| 225 | 65 | stmt = stmt_alloc(loc, &connlimit_stmt_ops); | |
| 226 | 65 | stmt->flags |= STMT_F_STATEFUL; | |
| 227 | 65 | return stmt; | |
| 228 | } | ||
| 229 | |||
| 230 | 4785 | static void counter_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 231 | { | ||
| 232 | 4785 | nft_print(octx, "counter"); | |
| 233 | |||
| 234 |
2/2✓ Branch 0 (4→5) taken 144 times.
✓ Branch 1 (4→6) taken 4641 times.
|
4785 | if (nft_output_stateless(octx)) |
| 235 | 144 | return; | |
| 236 | |||
| 237 | 4641 | nft_print(octx, " packets %" PRIu64 " bytes %" PRIu64, | |
| 238 | 4641 | stmt->counter.packets, stmt->counter.bytes); | |
| 239 | } | ||
| 240 | |||
| 241 | static const struct stmt_ops counter_stmt_ops = { | ||
| 242 | .type = STMT_COUNTER, | ||
| 243 | .name = "counter", | ||
| 244 | .print = counter_stmt_print, | ||
| 245 | .json = counter_stmt_json, | ||
| 246 | }; | ||
| 247 | |||
| 248 | 188105 | struct stmt *counter_stmt_alloc(const struct location *loc) | |
| 249 | { | ||
| 250 | struct stmt *stmt; | ||
| 251 | |||
| 252 | 188105 | stmt = stmt_alloc(loc, &counter_stmt_ops); | |
| 253 | 188105 | stmt->flags |= STMT_F_STATEFUL; | |
| 254 | 188105 | return stmt; | |
| 255 | } | ||
| 256 | |||
| 257 | 17 | static void last_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 258 | { | ||
| 259 | 17 | nft_print(octx, "last"); | |
| 260 | |||
| 261 |
2/2✓ Branch 0 (4→5) taken 13 times.
✓ Branch 1 (4→6) taken 4 times.
|
17 | if (nft_output_stateless(octx)) |
| 262 | 13 | return; | |
| 263 | |||
| 264 | 4 | nft_print(octx, " used "); | |
| 265 | |||
| 266 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 4 times.
|
4 | if (stmt->last.set) |
| 267 | ✗ | time_print(stmt->last.used, octx); | |
| 268 | else | ||
| 269 | 4 | nft_print(octx, "never"); | |
| 270 | } | ||
| 271 | |||
| 272 | static const struct stmt_ops last_stmt_ops = { | ||
| 273 | .type = STMT_LAST, | ||
| 274 | .name = "last", | ||
| 275 | .print = last_stmt_print, | ||
| 276 | .json = last_stmt_json, | ||
| 277 | }; | ||
| 278 | |||
| 279 | 86 | struct stmt *last_stmt_alloc(const struct location *loc) | |
| 280 | { | ||
| 281 | struct stmt *stmt; | ||
| 282 | |||
| 283 | 86 | stmt = stmt_alloc(loc, &last_stmt_ops); | |
| 284 | 86 | stmt->flags |= STMT_F_STATEFUL; | |
| 285 | 86 | return stmt; | |
| 286 | } | ||
| 287 | |||
| 288 | static const char *objref_type[NFT_OBJECT_MAX + 1] = { | ||
| 289 | [NFT_OBJECT_COUNTER] = "counter", | ||
| 290 | [NFT_OBJECT_QUOTA] = "quota", | ||
| 291 | [NFT_OBJECT_CT_HELPER] = "ct helper", | ||
| 292 | [NFT_OBJECT_LIMIT] = "limit", | ||
| 293 | [NFT_OBJECT_TUNNEL] = "tunnel", | ||
| 294 | [NFT_OBJECT_CT_TIMEOUT] = "ct timeout", | ||
| 295 | [NFT_OBJECT_SECMARK] = "secmark", | ||
| 296 | [NFT_OBJECT_SYNPROXY] = "synproxy", | ||
| 297 | [NFT_OBJECT_CT_EXPECT] = "ct expectation", | ||
| 298 | }; | ||
| 299 | |||
| 300 | 204 | const char *objref_type_name(uint32_t type) | |
| 301 | { | ||
| 302 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 204 times.
|
204 | if (type > NFT_OBJECT_MAX) |
| 303 | ✗ | return "unknown"; | |
| 304 | |||
| 305 | 204 | return objref_type[type]; | |
| 306 | } | ||
| 307 | |||
| 308 | 178 | static void objref_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 309 | { | ||
| 310 |
5/5✓ Branch 0 (2→3) taken 5 times.
✓ Branch 1 (2→5) taken 7 times.
✓ Branch 2 (2→7) taken 6 times.
✓ Branch 3 (2→9) taken 1 times.
✓ Branch 4 (2→11) taken 159 times.
|
178 | switch (stmt->objref.type) { |
| 311 | 5 | case NFT_OBJECT_CT_HELPER: | |
| 312 | 5 | nft_print(octx, "ct helper set "); | |
| 313 | 5 | break; | |
| 314 | 7 | case NFT_OBJECT_CT_TIMEOUT: | |
| 315 | 7 | nft_print(octx, "ct timeout set "); | |
| 316 | 7 | break; | |
| 317 | 6 | case NFT_OBJECT_CT_EXPECT: | |
| 318 | 6 | nft_print(octx, "ct expectation set "); | |
| 319 | 6 | break; | |
| 320 | 1 | case NFT_OBJECT_SECMARK: | |
| 321 | 1 | nft_print(octx, "meta secmark set "); | |
| 322 | 1 | break; | |
| 323 | 159 | default: | |
| 324 | 159 | nft_print(octx, "%s name ", | |
| 325 | 159 | objref_type_name(stmt->objref.type)); | |
| 326 | 159 | break; | |
| 327 | } | ||
| 328 | 178 | expr_print(stmt->objref.expr, octx); | |
| 329 | 178 | } | |
| 330 | |||
| 331 | 2175 | static void objref_stmt_destroy(struct stmt *stmt) | |
| 332 | { | ||
| 333 | 2175 | expr_free(stmt->objref.expr); | |
| 334 | 2175 | } | |
| 335 | |||
| 336 | static const struct stmt_ops objref_stmt_ops = { | ||
| 337 | .type = STMT_OBJREF, | ||
| 338 | .name = "objref", | ||
| 339 | .print = objref_stmt_print, | ||
| 340 | .json = objref_stmt_json, | ||
| 341 | .destroy = objref_stmt_destroy, | ||
| 342 | }; | ||
| 343 | |||
| 344 | 2175 | struct stmt *objref_stmt_alloc(const struct location *loc) | |
| 345 | { | ||
| 346 | struct stmt *stmt; | ||
| 347 | |||
| 348 | 2175 | stmt = stmt_alloc(loc, &objref_stmt_ops); | |
| 349 | 2175 | return stmt; | |
| 350 | } | ||
| 351 | |||
| 352 | static const char *syslog_level[NFT_LOGLEVEL_MAX + 1] = { | ||
| 353 | [NFT_LOGLEVEL_EMERG] = "emerg", | ||
| 354 | [NFT_LOGLEVEL_ALERT] = "alert", | ||
| 355 | [NFT_LOGLEVEL_CRIT] = "crit", | ||
| 356 | [NFT_LOGLEVEL_ERR] = "err", | ||
| 357 | [NFT_LOGLEVEL_WARNING] = "warn", | ||
| 358 | [NFT_LOGLEVEL_NOTICE] = "notice", | ||
| 359 | [NFT_LOGLEVEL_INFO] = "info", | ||
| 360 | [NFT_LOGLEVEL_DEBUG] = "debug", | ||
| 361 | [NFT_LOGLEVEL_AUDIT] = "audit" | ||
| 362 | }; | ||
| 363 | |||
| 364 | 90 | const char *log_level(uint32_t level) | |
| 365 | { | ||
| 366 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 90 times.
|
90 | if (level > NFT_LOGLEVEL_MAX) |
| 367 | ✗ | return "unknown"; | |
| 368 | |||
| 369 | 90 | return syslog_level[level]; | |
| 370 | } | ||
| 371 | |||
| 372 | 50 | int log_level_parse(const char *level) | |
| 373 | { | ||
| 374 | int i; | ||
| 375 | |||
| 376 |
1/2✓ Branch 0 (7→3) taken 265 times.
✗ Branch 1 (7→8) not taken.
|
265 | for (i = 0; i <= NFT_LOGLEVEL_MAX; i++) { |
| 377 |
1/2✓ Branch 0 (3→4) taken 265 times.
✗ Branch 1 (3→6) not taken.
|
265 | if (syslog_level[i] && |
| 378 |
2/2✓ Branch 0 (4→5) taken 50 times.
✓ Branch 1 (4→6) taken 215 times.
|
265 | !strcmp(level, syslog_level[i])) |
| 379 | 50 | return i; | |
| 380 | } | ||
| 381 | ✗ | return -1; | |
| 382 | } | ||
| 383 | |||
| 384 | 105 | static void log_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 385 | { | ||
| 386 | 105 | nft_print(octx, "log"); | |
| 387 |
2/2✓ Branch 0 (3→4) taken 18 times.
✓ Branch 1 (3→5) taken 87 times.
|
105 | if (stmt->log.flags & STMT_LOG_PREFIX) |
| 388 | 18 | nft_print(octx, " prefix \"%s\"", stmt->log.prefix); | |
| 389 |
2/2✓ Branch 0 (5→6) taken 20 times.
✓ Branch 1 (5→7) taken 85 times.
|
105 | if (stmt->log.flags & STMT_LOG_GROUP) |
| 390 | 20 | nft_print(octx, " group %u", stmt->log.group); | |
| 391 |
2/2✓ Branch 0 (7→8) taken 10 times.
✓ Branch 1 (7→9) taken 95 times.
|
105 | if (stmt->log.flags & STMT_LOG_SNAPLEN) |
| 392 | 10 | nft_print(octx, " snaplen %u", stmt->log.snaplen); | |
| 393 |
2/2✓ Branch 0 (9→10) taken 5 times.
✓ Branch 1 (9→11) taken 100 times.
|
105 | if (stmt->log.flags & STMT_LOG_QTHRESHOLD) |
| 394 | 5 | nft_print(octx, " queue-threshold %u", stmt->log.qthreshold); | |
| 395 |
2/2✓ Branch 0 (11→12) taken 85 times.
✓ Branch 1 (11→15) taken 20 times.
|
105 | if ((stmt->log.flags & STMT_LOG_LEVEL) && |
| 396 |
2/2✓ Branch 0 (12→13) taken 45 times.
✓ Branch 1 (12→15) taken 40 times.
|
85 | stmt->log.level != LOG_WARNING) |
| 397 | 45 | nft_print(octx, " level %s", log_level(stmt->log.level)); | |
| 398 | |||
| 399 |
2/2✓ Branch 0 (15→16) taken 10 times.
✓ Branch 1 (15→17) taken 95 times.
|
105 | if ((stmt->log.logflags & NF_LOG_MASK) == NF_LOG_MASK) { |
| 400 | 10 | nft_print(octx, " flags all"); | |
| 401 | } else { | ||
| 402 |
2/2✓ Branch 0 (17→18) taken 7 times.
✓ Branch 1 (17→24) taken 88 times.
|
95 | if (stmt->log.logflags & (NF_LOG_TCPSEQ | NF_LOG_TCPOPT)) { |
| 403 | 7 | const char *delim = " "; | |
| 404 | |||
| 405 | 7 | nft_print(octx, " flags tcp"); | |
| 406 |
1/2✓ Branch 0 (19→20) taken 7 times.
✗ Branch 1 (19→22) not taken.
|
7 | if (stmt->log.logflags & NF_LOG_TCPSEQ) { |
| 407 | 7 | nft_print(octx, " sequence"); | |
| 408 | 7 | delim = ","; | |
| 409 | } | ||
| 410 |
2/2✓ Branch 0 (22→23) taken 6 times.
✓ Branch 1 (22→24) taken 1 times.
|
7 | if (stmt->log.logflags & NF_LOG_TCPOPT) |
| 411 | 6 | nft_print(octx, "%soptions", | |
| 412 | delim); | ||
| 413 | } | ||
| 414 |
2/2✓ Branch 0 (24→25) taken 11 times.
✓ Branch 1 (24→26) taken 84 times.
|
95 | if (stmt->log.logflags & NF_LOG_IPOPT) |
| 415 | 11 | nft_print(octx, " flags ip options"); | |
| 416 |
2/2✓ Branch 0 (26→27) taken 5 times.
✓ Branch 1 (26→28) taken 90 times.
|
95 | if (stmt->log.logflags & NF_LOG_UID) |
| 417 | 5 | nft_print(octx, " flags skuid"); | |
| 418 |
2/2✓ Branch 0 (28→29) taken 6 times.
✓ Branch 1 (28→30) taken 89 times.
|
95 | if (stmt->log.logflags & NF_LOG_MACDECODE) |
| 419 | 6 | nft_print(octx, " flags ether"); | |
| 420 | } | ||
| 421 | 105 | } | |
| 422 | |||
| 423 | 507 | static void log_stmt_destroy(struct stmt *stmt) | |
| 424 | { | ||
| 425 | 507 | free_const(stmt->log.prefix); | |
| 426 | 507 | } | |
| 427 | |||
| 428 | static const struct stmt_ops log_stmt_ops = { | ||
| 429 | .type = STMT_LOG, | ||
| 430 | .name = "log", | ||
| 431 | .print = log_stmt_print, | ||
| 432 | .json = log_stmt_json, | ||
| 433 | .destroy = log_stmt_destroy, | ||
| 434 | }; | ||
| 435 | |||
| 436 | 506 | struct stmt *log_stmt_alloc(const struct location *loc) | |
| 437 | { | ||
| 438 | 506 | return stmt_alloc(loc, &log_stmt_ops); | |
| 439 | } | ||
| 440 | |||
| 441 | 600 | const char *get_unit(uint64_t u) | |
| 442 | { | ||
| 443 |
5/6✓ Branch 0 (2→3) taken 468 times.
✓ Branch 1 (2→4) taken 46 times.
✓ Branch 2 (2→5) taken 30 times.
✓ Branch 3 (2→6) taken 28 times.
✓ Branch 4 (2→7) taken 28 times.
✗ Branch 5 (2→8) not taken.
|
600 | switch (u) { |
| 444 | 468 | case 1: return "second"; | |
| 445 | 46 | case 60: return "minute"; | |
| 446 | 30 | case 60 * 60: return "hour"; | |
| 447 | 28 | case 60 * 60 * 24: return "day"; | |
| 448 | 28 | case 60 * 60 * 24 * 7: return "week"; | |
| 449 | } | ||
| 450 | |||
| 451 | ✗ | return "error"; | |
| 452 | } | ||
| 453 | |||
| 454 | static const char * const data_unit[] = { | ||
| 455 | "bytes", | ||
| 456 | "kbytes", | ||
| 457 | "mbytes", | ||
| 458 | NULL | ||
| 459 | }; | ||
| 460 | |||
| 461 | 930 | const char *get_rate(uint64_t byte_rate, uint64_t *rate) | |
| 462 | { | ||
| 463 | int i; | ||
| 464 | |||
| 465 |
2/2✓ Branch 0 (2→3) taken 97 times.
✓ Branch 1 (2→4) taken 833 times.
|
930 | if (!byte_rate) { |
| 466 | 97 | *rate = 0; | |
| 467 | 97 | return data_unit[0]; | |
| 468 | } | ||
| 469 | |||
| 470 |
2/2✓ Branch 0 (8→5) taken 1366 times.
✓ Branch 1 (8→9) taken 238 times.
|
1604 | for (i = 0; data_unit[i + 1] != NULL; i++) { |
| 471 |
2/2✓ Branch 0 (5→6) taken 595 times.
✓ Branch 1 (5→7) taken 771 times.
|
1366 | if (byte_rate % 1024) |
| 472 | 595 | break; | |
| 473 | 771 | byte_rate /= 1024; | |
| 474 | } | ||
| 475 | |||
| 476 | 833 | *rate = byte_rate; | |
| 477 | 833 | return data_unit[i]; | |
| 478 | } | ||
| 479 | |||
| 480 | 284 | static void limit_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 481 | { | ||
| 482 | 284 | bool inv = stmt->limit.flags & NFT_LIMIT_F_INV; | |
| 483 | const char *data_unit; | ||
| 484 | uint64_t rate; | ||
| 485 | |||
| 486 |
2/3✓ Branch 0 (2→3) taken 106 times.
✓ Branch 1 (2→10) taken 178 times.
✗ Branch 2 (2→21) not taken.
|
284 | switch (stmt->limit.type) { |
| 487 | 106 | case NFT_LIMIT_PKTS: | |
| 488 | 106 | nft_print(octx, "limit rate %s%" PRIu64 "/%s", | |
| 489 |
2/2✓ Branch 0 (4→5) taken 43 times.
✓ Branch 1 (4→6) taken 63 times.
|
106 | inv ? "over " : "", stmt->limit.rate, |
| 490 | 106 | get_unit(stmt->limit.unit)); | |
| 491 | 106 | nft_print(octx, " burst %u packets", stmt->limit.burst); | |
| 492 | 106 | break; | |
| 493 | 178 | case NFT_LIMIT_PKT_BYTES: | |
| 494 | 178 | data_unit = get_rate(stmt->limit.rate, &rate); | |
| 495 | |||
| 496 |
2/2✓ Branch 0 (12→13) taken 101 times.
✓ Branch 1 (12→14) taken 77 times.
|
178 | nft_print(octx, "limit rate %s%" PRIu64 " %s/%s", |
| 497 | inv ? "over " : "", rate, data_unit, | ||
| 498 | 178 | get_unit(stmt->limit.unit)); | |
| 499 |
2/2✓ Branch 0 (16→17) taken 83 times.
✓ Branch 1 (16→20) taken 95 times.
|
178 | if (stmt->limit.burst != 0) { |
| 500 | uint64_t burst; | ||
| 501 | |||
| 502 | 83 | data_unit = get_rate(stmt->limit.burst, &burst); | |
| 503 | 83 | nft_print(octx, " burst %" PRIu64 " %s", burst, | |
| 504 | data_unit); | ||
| 505 | } | ||
| 506 | 178 | break; | |
| 507 | } | ||
| 508 | 284 | } | |
| 509 | |||
| 510 | static const struct stmt_ops limit_stmt_ops = { | ||
| 511 | .type = STMT_LIMIT, | ||
| 512 | .name = "limit", | ||
| 513 | .print = limit_stmt_print, | ||
| 514 | .json = limit_stmt_json, | ||
| 515 | }; | ||
| 516 | |||
| 517 | 1299 | struct stmt *limit_stmt_alloc(const struct location *loc) | |
| 518 | { | ||
| 519 | struct stmt *stmt; | ||
| 520 | |||
| 521 | 1299 | stmt = stmt_alloc(loc, &limit_stmt_ops); | |
| 522 | 1299 | stmt->flags |= STMT_F_STATEFUL; | |
| 523 | 1299 | return stmt; | |
| 524 | } | ||
| 525 | |||
| 526 | 85 | static void queue_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 527 | { | ||
| 528 | 85 | struct expr *e = stmt->queue.queue; | |
| 529 | 85 | const char *delim = " flags "; | |
| 530 | |||
| 531 | 85 | nft_print(octx, "queue"); | |
| 532 | |||
| 533 |
2/2✓ Branch 0 (3→4) taken 39 times.
✓ Branch 1 (3→6) taken 46 times.
|
85 | if (stmt->queue.flags & NFT_QUEUE_FLAG_BYPASS) { |
| 534 | 39 | nft_print(octx, "%sbypass", delim); | |
| 535 | 39 | delim = ","; | |
| 536 | } | ||
| 537 | |||
| 538 |
2/2✓ Branch 0 (6→7) taken 15 times.
✓ Branch 1 (6→8) taken 70 times.
|
85 | if (stmt->queue.flags & NFT_QUEUE_FLAG_CPU_FANOUT) |
| 539 | 15 | nft_print(octx, "%sfanout", delim); | |
| 540 | |||
| 541 |
1/2✓ Branch 0 (8→9) taken 85 times.
✗ Branch 1 (8→11) not taken.
|
85 | if (e) { |
| 542 | 85 | nft_print(octx, " to "); | |
| 543 | 85 | expr_print(stmt->queue.queue, octx); | |
| 544 | } else { | ||
| 545 | ✗ | nft_print(octx, " to 0"); | |
| 546 | } | ||
| 547 | 85 | } | |
| 548 | |||
| 549 | 397 | static void queue_stmt_destroy(struct stmt *stmt) | |
| 550 | { | ||
| 551 | 397 | expr_free(stmt->queue.queue); | |
| 552 | 397 | } | |
| 553 | |||
| 554 | static const struct stmt_ops queue_stmt_ops = { | ||
| 555 | .type = STMT_QUEUE, | ||
| 556 | .name = "queue", | ||
| 557 | .print = queue_stmt_print, | ||
| 558 | .json = queue_stmt_json, | ||
| 559 | .destroy = queue_stmt_destroy, | ||
| 560 | }; | ||
| 561 | |||
| 562 | 397 | struct stmt *queue_stmt_alloc(const struct location *loc, struct expr *e, uint16_t flags) | |
| 563 | { | ||
| 564 | struct stmt *stmt; | ||
| 565 | |||
| 566 | 397 | stmt = stmt_alloc(loc, &queue_stmt_ops); | |
| 567 | 397 | stmt->queue.queue = e; | |
| 568 | 397 | stmt->queue.flags = flags; | |
| 569 | |||
| 570 | 397 | return stmt; | |
| 571 | } | ||
| 572 | |||
| 573 | 122 | static void quota_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 574 | { | ||
| 575 | 122 | bool inv = stmt->quota.flags & NFT_QUOTA_F_INV; | |
| 576 | const char *data_unit; | ||
| 577 | uint64_t bytes, used; | ||
| 578 | |||
| 579 | 122 | data_unit = get_rate(stmt->quota.bytes, &bytes); | |
| 580 |
2/2✓ Branch 0 (3→4) taken 53 times.
✓ Branch 1 (3→5) taken 69 times.
|
122 | nft_print(octx, "quota %s%" PRIu64 " %s", |
| 581 | inv ? "over " : "", bytes, data_unit); | ||
| 582 | |||
| 583 |
4/4✓ Branch 0 (8→9) taken 25 times.
✓ Branch 1 (8→12) taken 97 times.
✓ Branch 2 (9→10) taken 8 times.
✓ Branch 3 (9→12) taken 17 times.
|
122 | if (!nft_output_stateless(octx) && stmt->quota.used) { |
| 584 | 8 | data_unit = get_rate(stmt->quota.used, &used); | |
| 585 | 8 | nft_print(octx, " used %" PRIu64 " %s", used, data_unit); | |
| 586 | } | ||
| 587 | 122 | } | |
| 588 | |||
| 589 | static const struct stmt_ops quota_stmt_ops = { | ||
| 590 | .type = STMT_QUOTA, | ||
| 591 | .name = "quota", | ||
| 592 | .print = quota_stmt_print, | ||
| 593 | .json = quota_stmt_json, | ||
| 594 | }; | ||
| 595 | |||
| 596 | 497 | struct stmt *quota_stmt_alloc(const struct location *loc) | |
| 597 | { | ||
| 598 | struct stmt *stmt; | ||
| 599 | |||
| 600 | 497 | stmt = stmt_alloc(loc, "a_stmt_ops); | |
| 601 | 497 | stmt->flags |= STMT_F_STATEFUL; | |
| 602 | 497 | return stmt; | |
| 603 | } | ||
| 604 | |||
| 605 | 118 | static void reject_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 606 | { | ||
| 607 | 118 | nft_print(octx, "reject"); | |
| 608 |
4/4✓ Branch 0 (3→4) taken 13 times.
✓ Branch 1 (3→6) taken 33 times.
✓ Branch 2 (3→11) taken 70 times.
✓ Branch 3 (3→25) taken 2 times.
|
118 | switch (stmt->reject.type) { |
| 609 | 13 | case NFT_REJECT_TCP_RST: | |
| 610 | 13 | nft_print(octx, " with tcp reset"); | |
| 611 | 13 | break; | |
| 612 | 33 | case NFT_REJECT_ICMPX_UNREACH: | |
| 613 |
2/2✓ Branch 0 (6→7) taken 11 times.
✓ Branch 1 (6→8) taken 22 times.
|
33 | if (stmt->reject.icmp_code == NFT_REJECT_ICMPX_PORT_UNREACH) |
| 614 | 11 | break; | |
| 615 | 22 | nft_print(octx, " with icmpx "); | |
| 616 | 22 | expr_print(stmt->reject.expr, octx); | |
| 617 | 22 | break; | |
| 618 | 70 | case NFT_REJECT_ICMP_UNREACH: | |
| 619 |
2/3✓ Branch 0 (11→12) taken 38 times.
✓ Branch 1 (11→18) taken 32 times.
✗ Branch 2 (11→24) not taken.
|
70 | switch (stmt->reject.family) { |
| 620 | 38 | case NFPROTO_IPV4: | |
| 621 |
2/2✓ Branch 0 (12→13) taken 10 times.
✓ Branch 1 (12→15) taken 28 times.
|
38 | if (!stmt->reject.verbose_print && |
| 622 |
2/2✓ Branch 0 (13→14) taken 4 times.
✓ Branch 1 (13→15) taken 6 times.
|
10 | stmt->reject.icmp_code == ICMP_PORT_UNREACH) |
| 623 | 4 | break; | |
| 624 | 34 | nft_print(octx, " with icmp "); | |
| 625 | 34 | expr_print(stmt->reject.expr, octx); | |
| 626 | 34 | break; | |
| 627 | 32 | case NFPROTO_IPV6: | |
| 628 |
2/2✓ Branch 0 (18→19) taken 9 times.
✓ Branch 1 (18→21) taken 23 times.
|
32 | if (!stmt->reject.verbose_print && |
| 629 |
2/2✓ Branch 0 (19→20) taken 3 times.
✓ Branch 1 (19→21) taken 6 times.
|
9 | stmt->reject.icmp_code == ICMP6_DST_UNREACH_NOPORT) |
| 630 | 3 | break; | |
| 631 | 29 | nft_print(octx, " with icmpv6 "); | |
| 632 | 29 | expr_print(stmt->reject.expr, octx); | |
| 633 | 29 | break; | |
| 634 | } | ||
| 635 | 70 | break; | |
| 636 | } | ||
| 637 | 118 | } | |
| 638 | |||
| 639 | 556 | static void reject_stmt_destroy(struct stmt *stmt) | |
| 640 | { | ||
| 641 | 556 | expr_free(stmt->reject.expr); | |
| 642 | 556 | } | |
| 643 | |||
| 644 | static const struct stmt_ops reject_stmt_ops = { | ||
| 645 | .type = STMT_REJECT, | ||
| 646 | .name = "reject", | ||
| 647 | .print = reject_stmt_print, | ||
| 648 | .json = reject_stmt_json, | ||
| 649 | .destroy = reject_stmt_destroy, | ||
| 650 | }; | ||
| 651 | |||
| 652 | 551 | struct stmt *reject_stmt_alloc(const struct location *loc) | |
| 653 | { | ||
| 654 | 551 | return stmt_alloc(loc, &reject_stmt_ops); | |
| 655 | } | ||
| 656 | |||
| 657 | 223 | static void print_nf_nat_flags(uint32_t flags, struct output_ctx *octx) | |
| 658 | { | ||
| 659 | 223 | const char *delim = " "; | |
| 660 | |||
| 661 |
2/2✓ Branch 0 (2→3) taken 38 times.
✓ Branch 1 (2→4) taken 185 times.
|
223 | if (flags == 0) |
| 662 | 38 | return; | |
| 663 | |||
| 664 |
2/2✓ Branch 0 (4→5) taken 37 times.
✓ Branch 1 (4→7) taken 148 times.
|
185 | if (flags & NF_NAT_RANGE_PROTO_RANDOM) { |
| 665 | 37 | nft_print(octx, "%srandom", delim); | |
| 666 | 37 | delim = ","; | |
| 667 | } | ||
| 668 | |||
| 669 |
2/2✓ Branch 0 (7→8) taken 26 times.
✓ Branch 1 (7→10) taken 159 times.
|
185 | if (flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY) { |
| 670 | 26 | nft_print(octx, "%sfully-random", delim); | |
| 671 | 26 | delim = ","; | |
| 672 | } | ||
| 673 | |||
| 674 |
2/2✓ Branch 0 (10→11) taken 37 times.
✓ Branch 1 (10→12) taken 148 times.
|
185 | if (flags & NF_NAT_RANGE_PERSISTENT) |
| 675 | 37 | nft_print(octx, "%spersistent", delim); | |
| 676 | } | ||
| 677 | |||
| 678 | 428 | const char *nat_etype2str(enum nft_nat_etypes type) | |
| 679 | { | ||
| 680 | static const char * const nat_types[] = { | ||
| 681 | [NFT_NAT_SNAT] = "snat", | ||
| 682 | [NFT_NAT_DNAT] = "dnat", | ||
| 683 | [NFT_NAT_MASQ] = "masquerade", | ||
| 684 | [NFT_NAT_REDIR] = "redirect", | ||
| 685 | }; | ||
| 686 | |||
| 687 | 428 | return nat_types[type]; | |
| 688 | } | ||
| 689 | |||
| 690 | 223 | static void nat_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 691 | { | ||
| 692 | 223 | nft_print(octx, "%s", nat_etype2str(stmt->nat.type)); | |
| 693 |
4/4✓ Branch 0 (4→5) taken 101 times.
✓ Branch 1 (4→6) taken 122 times.
✓ Branch 2 (5→6) taken 29 times.
✓ Branch 3 (5→14) taken 72 times.
|
223 | if (stmt->nat.addr || stmt->nat.proto) { |
| 694 |
3/3✓ Branch 0 (6→7) taken 54 times.
✓ Branch 1 (6→9) taken 13 times.
✓ Branch 2 (6→11) taken 84 times.
|
151 | switch (stmt->nat.family) { |
| 695 | 54 | case NFPROTO_IPV4: | |
| 696 | 54 | nft_print(octx, " ip"); | |
| 697 | 54 | break; | |
| 698 | 13 | case NFPROTO_IPV6: | |
| 699 | 13 | nft_print(octx, " ip6"); | |
| 700 | 13 | break; | |
| 701 | } | ||
| 702 | |||
| 703 |
2/2✓ Branch 0 (11→12) taken 3 times.
✓ Branch 1 (11→13) taken 148 times.
|
151 | if (stmt->nat.type_flags & STMT_NAT_F_PREFIX) |
| 704 | 3 | nft_print(octx, " prefix"); | |
| 705 | |||
| 706 | 151 | nft_print(octx, " to"); | |
| 707 | } | ||
| 708 | |||
| 709 |
2/2✓ Branch 0 (14→15) taken 122 times.
✓ Branch 1 (14→32) taken 101 times.
|
223 | if (stmt->nat.addr) { |
| 710 | 122 | nft_print(octx, " "); | |
| 711 |
2/2✓ Branch 0 (16→17) taken 20 times.
✓ Branch 1 (16→31) taken 102 times.
|
122 | if (stmt->nat.proto) { |
| 712 |
2/2✓ Branch 0 (17→18) taken 13 times.
✓ Branch 1 (17→22) taken 7 times.
|
20 | if (stmt->nat.addr->etype == EXPR_VALUE && |
| 713 |
2/2✓ Branch 0 (18→19) taken 4 times.
✓ Branch 1 (18→22) taken 9 times.
|
13 | stmt->nat.addr->dtype->type == TYPE_IP6ADDR) { |
| 714 | 4 | nft_print(octx, "["); | |
| 715 | 4 | expr_print(stmt->nat.addr, octx); | |
| 716 | 4 | nft_print(octx, "]"); | |
| 717 |
2/2✓ Branch 0 (22→23) taken 7 times.
✓ Branch 1 (22→29) taken 9 times.
|
16 | } else if (stmt->nat.addr->etype == EXPR_RANGE && |
| 718 |
2/2✓ Branch 0 (23→24) taken 5 times.
✓ Branch 1 (23→29) taken 2 times.
|
7 | stmt->nat.addr->left->dtype->type == TYPE_IP6ADDR) { |
| 719 | 5 | nft_print(octx, "["); | |
| 720 | 5 | expr_print(stmt->nat.addr->left, octx); | |
| 721 | 5 | nft_print(octx, "]-["); | |
| 722 | 5 | expr_print(stmt->nat.addr->right, octx); | |
| 723 | 5 | nft_print(octx, "]"); | |
| 724 | } else { | ||
| 725 | 11 | expr_print(stmt->nat.addr, octx); | |
| 726 | } | ||
| 727 | } else { | ||
| 728 | 102 | expr_print(stmt->nat.addr, octx); | |
| 729 | } | ||
| 730 | } | ||
| 731 | |||
| 732 |
2/2✓ Branch 0 (32→33) taken 49 times.
✓ Branch 1 (32→37) taken 174 times.
|
223 | if (stmt->nat.proto) { |
| 733 |
2/2✓ Branch 0 (33→34) taken 29 times.
✓ Branch 1 (33→35) taken 20 times.
|
49 | if (!stmt->nat.addr) |
| 734 | 29 | nft_print(octx, " "); | |
| 735 | 49 | nft_print(octx, ":"); | |
| 736 | 49 | expr_print(stmt->nat.proto, octx); | |
| 737 | } | ||
| 738 | |||
| 739 | 223 | print_nf_nat_flags(stmt->nat.flags, octx); | |
| 740 | 223 | } | |
| 741 | |||
| 742 | 1155 | static void nat_stmt_destroy(struct stmt *stmt) | |
| 743 | { | ||
| 744 | 1155 | expr_free(stmt->nat.addr); | |
| 745 | 1155 | expr_free(stmt->nat.proto); | |
| 746 | 1155 | } | |
| 747 | |||
| 748 | static const struct stmt_ops nat_stmt_ops = { | ||
| 749 | .type = STMT_NAT, | ||
| 750 | .name = "nat", | ||
| 751 | .print = nat_stmt_print, | ||
| 752 | .json = nat_stmt_json, | ||
| 753 | .destroy = nat_stmt_destroy, | ||
| 754 | }; | ||
| 755 | |||
| 756 | 1131 | struct stmt *nat_stmt_alloc(const struct location *loc, | |
| 757 | enum nft_nat_etypes type) | ||
| 758 | { | ||
| 759 | 1131 | struct stmt *stmt = stmt_alloc(loc, &nat_stmt_ops); | |
| 760 | |||
| 761 | 1131 | stmt->nat.type = type; | |
| 762 | 1131 | return stmt; | |
| 763 | } | ||
| 764 | |||
| 765 | const char * const set_stmt_op_names[] = { | ||
| 766 | [NFT_DYNSET_OP_ADD] = "add", | ||
| 767 | [NFT_DYNSET_OP_UPDATE] = "update", | ||
| 768 | [NFT_DYNSET_OP_DELETE] = "delete", | ||
| 769 | }; | ||
| 770 | |||
| 771 | 164 | static void set_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 772 | { | ||
| 773 | 164 | unsigned int flags = octx->flags; | |
| 774 | struct stmt *this; | ||
| 775 | |||
| 776 | 164 | nft_print(octx, "%s ", set_stmt_op_names[stmt->set.op]); | |
| 777 | 164 | expr_print(stmt->set.set, octx); | |
| 778 | 164 | nft_print(octx, " { "); | |
| 779 | 164 | expr_print(stmt->set.key, octx); | |
| 780 |
2/2✓ Branch 0 (10→7) taken 11 times.
✓ Branch 1 (10→11) taken 164 times.
|
175 | list_for_each_entry(this, &stmt->set.stmt_list, list) { |
| 781 | 11 | nft_print(octx, " "); | |
| 782 | 11 | octx->flags |= NFT_CTX_OUTPUT_STATELESS; | |
| 783 | 11 | stmt_print(this, octx); | |
| 784 | 11 | octx->flags = flags; | |
| 785 | } | ||
| 786 | 164 | nft_print(octx, " }"); | |
| 787 | 164 | } | |
| 788 | |||
| 789 | 1379 | static void set_stmt_destroy(struct stmt *stmt) | |
| 790 | { | ||
| 791 | struct stmt *this, *next; | ||
| 792 | |||
| 793 | 1379 | expr_free(stmt->set.key); | |
| 794 | 1379 | expr_free(stmt->set.set); | |
| 795 |
2/2✓ Branch 0 (7→5) taken 66 times.
✓ Branch 1 (7→8) taken 1379 times.
|
1445 | list_for_each_entry_safe(this, next, &stmt->set.stmt_list, list) |
| 796 | 66 | stmt_free(this); | |
| 797 | 1379 | } | |
| 798 | |||
| 799 | static const struct stmt_ops set_stmt_ops = { | ||
| 800 | .type = STMT_SET, | ||
| 801 | .name = "set", | ||
| 802 | .print = set_stmt_print, | ||
| 803 | .json = set_stmt_json, | ||
| 804 | .destroy = set_stmt_destroy, | ||
| 805 | }; | ||
| 806 | |||
| 807 | 1379 | struct stmt *set_stmt_alloc(const struct location *loc) | |
| 808 | { | ||
| 809 | struct stmt *stmt; | ||
| 810 | |||
| 811 | 1379 | stmt = stmt_alloc(loc, &set_stmt_ops); | |
| 812 | 1379 | init_list_head(&stmt->set.stmt_list); | |
| 813 | |||
| 814 | 1379 | return stmt; | |
| 815 | } | ||
| 816 | |||
| 817 | 35 | static void map_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 818 | { | ||
| 819 | 35 | unsigned int flags = octx->flags; | |
| 820 | struct stmt *this; | ||
| 821 | |||
| 822 | 35 | nft_print(octx, "%s ", set_stmt_op_names[stmt->map.op]); | |
| 823 | 35 | expr_print(stmt->map.set, octx); | |
| 824 | 35 | nft_print(octx, " { "); | |
| 825 | 35 | expr_print(stmt->map.key, octx); | |
| 826 |
1/2✗ Branch 0 (10→7) not taken.
✓ Branch 1 (10→11) taken 35 times.
|
35 | list_for_each_entry(this, &stmt->map.stmt_list, list) { |
| 827 | ✗ | nft_print(octx, " "); | |
| 828 | ✗ | octx->flags |= NFT_CTX_OUTPUT_STATELESS; | |
| 829 | ✗ | stmt_print(this, octx); | |
| 830 | ✗ | octx->flags = flags; | |
| 831 | } | ||
| 832 | 35 | nft_print(octx, " : "); | |
| 833 | 35 | expr_print(stmt->map.data, octx); | |
| 834 | 35 | nft_print(octx, " }"); | |
| 835 | 35 | } | |
| 836 | |||
| 837 | 134 | static void map_stmt_destroy(struct stmt *stmt) | |
| 838 | { | ||
| 839 | struct stmt *this, *next; | ||
| 840 | |||
| 841 | 134 | expr_free(stmt->map.key); | |
| 842 | 134 | expr_free(stmt->map.data); | |
| 843 | 134 | expr_free(stmt->map.set); | |
| 844 |
1/2✗ Branch 0 (8→6) not taken.
✓ Branch 1 (8→9) taken 134 times.
|
134 | list_for_each_entry_safe(this, next, &stmt->map.stmt_list, list) |
| 845 | ✗ | stmt_free(this); | |
| 846 | 134 | } | |
| 847 | |||
| 848 | static const struct stmt_ops map_stmt_ops = { | ||
| 849 | .type = STMT_MAP, | ||
| 850 | .name = "map", | ||
| 851 | .print = map_stmt_print, | ||
| 852 | .destroy = map_stmt_destroy, | ||
| 853 | .json = map_stmt_json, | ||
| 854 | }; | ||
| 855 | |||
| 856 | 134 | struct stmt *map_stmt_alloc(const struct location *loc) | |
| 857 | { | ||
| 858 | struct stmt *stmt; | ||
| 859 | |||
| 860 | 134 | stmt = stmt_alloc(loc, &map_stmt_ops); | |
| 861 | 134 | init_list_head(&stmt->map.stmt_list); | |
| 862 | |||
| 863 | 134 | return stmt; | |
| 864 | } | ||
| 865 | |||
| 866 | 13 | static void dup_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 867 | { | ||
| 868 | 13 | nft_print(octx, "dup"); | |
| 869 |
1/2✓ Branch 0 (3→4) taken 13 times.
✗ Branch 1 (3→9) not taken.
|
13 | if (stmt->dup.to != NULL) { |
| 870 | 13 | nft_print(octx, " to "); | |
| 871 | 13 | expr_print(stmt->dup.to, octx); | |
| 872 | |||
| 873 |
2/2✓ Branch 0 (6→7) taken 6 times.
✓ Branch 1 (6→9) taken 7 times.
|
13 | if (stmt->dup.dev != NULL) { |
| 874 | 6 | nft_print(octx, " device "); | |
| 875 | 6 | expr_print(stmt->dup.dev, octx); | |
| 876 | } | ||
| 877 | } | ||
| 878 | 13 | } | |
| 879 | |||
| 880 | 45 | static void dup_stmt_destroy(struct stmt *stmt) | |
| 881 | { | ||
| 882 | 45 | expr_free(stmt->dup.to); | |
| 883 | 45 | expr_free(stmt->dup.dev); | |
| 884 | 45 | } | |
| 885 | |||
| 886 | static const struct stmt_ops dup_stmt_ops = { | ||
| 887 | .type = STMT_DUP, | ||
| 888 | .name = "dup", | ||
| 889 | .print = dup_stmt_print, | ||
| 890 | .json = dup_stmt_json, | ||
| 891 | .destroy = dup_stmt_destroy, | ||
| 892 | }; | ||
| 893 | |||
| 894 | 45 | struct stmt *dup_stmt_alloc(const struct location *loc) | |
| 895 | { | ||
| 896 | 45 | return stmt_alloc(loc, &dup_stmt_ops); | |
| 897 | } | ||
| 898 | |||
| 899 | static const char * const nfproto_family_name_array[NFPROTO_NUMPROTO] = { | ||
| 900 | [NFPROTO_IPV4] = "ip", | ||
| 901 | [NFPROTO_IPV6] = "ip6", | ||
| 902 | }; | ||
| 903 | |||
| 904 | 12 | static const char *nfproto_family_name(uint8_t nfproto) | |
| 905 | { | ||
| 906 |
2/4✓ Branch 0 (2→3) taken 12 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→5) taken 12 times.
|
12 | if (nfproto >= NFPROTO_NUMPROTO || !nfproto_family_name_array[nfproto]) |
| 907 | ✗ | return "unknown"; | |
| 908 | |||
| 909 | 12 | return nfproto_family_name_array[nfproto]; | |
| 910 | } | ||
| 911 | |||
| 912 | 7 | static void fwd_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 913 | { | ||
| 914 |
2/2✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→8) taken 4 times.
|
7 | if (stmt->fwd.addr) { |
| 915 | 3 | nft_print(octx, "fwd %s to ", | |
| 916 | 3 | nfproto_family_name(stmt->fwd.family)); | |
| 917 | 3 | expr_print(stmt->fwd.addr, octx); | |
| 918 | 3 | nft_print(octx, " device "); | |
| 919 | 3 | expr_print(stmt->fwd.dev, octx); | |
| 920 | } else { | ||
| 921 | 4 | nft_print(octx, "fwd to "); | |
| 922 | 4 | expr_print(stmt->fwd.dev, octx); | |
| 923 | } | ||
| 924 | 7 | } | |
| 925 | |||
| 926 | 26 | static void fwd_stmt_destroy(struct stmt *stmt) | |
| 927 | { | ||
| 928 | 26 | expr_free(stmt->fwd.addr); | |
| 929 | 26 | expr_free(stmt->fwd.dev); | |
| 930 | 26 | } | |
| 931 | |||
| 932 | static const struct stmt_ops fwd_stmt_ops = { | ||
| 933 | .type = STMT_FWD, | ||
| 934 | .name = "fwd", | ||
| 935 | .print = fwd_stmt_print, | ||
| 936 | .json = fwd_stmt_json, | ||
| 937 | .destroy = fwd_stmt_destroy, | ||
| 938 | }; | ||
| 939 | |||
| 940 | 26 | struct stmt *fwd_stmt_alloc(const struct location *loc) | |
| 941 | { | ||
| 942 | 26 | return stmt_alloc(loc, &fwd_stmt_ops); | |
| 943 | } | ||
| 944 | |||
| 945 | 10 | static void optstrip_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 946 | { | ||
| 947 | 10 | const struct expr *expr = stmt->optstrip.expr; | |
| 948 | |||
| 949 | 10 | nft_print(octx, "reset "); | |
| 950 | 10 | expr_print(expr, octx); | |
| 951 | 10 | } | |
| 952 | |||
| 953 | 46 | static void optstrip_stmt_destroy(struct stmt *stmt) | |
| 954 | { | ||
| 955 | 46 | expr_free(stmt->optstrip.expr); | |
| 956 | 46 | } | |
| 957 | |||
| 958 | static const struct stmt_ops optstrip_stmt_ops = { | ||
| 959 | .type = STMT_OPTSTRIP, | ||
| 960 | .name = "optstrip", | ||
| 961 | .print = optstrip_stmt_print, | ||
| 962 | .json = optstrip_stmt_json, | ||
| 963 | .destroy = optstrip_stmt_destroy, | ||
| 964 | }; | ||
| 965 | |||
| 966 | 46 | struct stmt *optstrip_stmt_alloc(const struct location *loc, struct expr *e) | |
| 967 | { | ||
| 968 | 46 | struct stmt *stmt = stmt_alloc(loc, &optstrip_stmt_ops); | |
| 969 | |||
| 970 | 46 | e->exthdr.flags |= NFT_EXTHDR_F_PRESENT; | |
| 971 | 46 | stmt->optstrip.expr = e; | |
| 972 | |||
| 973 | 46 | return stmt; | |
| 974 | } | ||
| 975 | |||
| 976 | 26 | static void tproxy_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 977 | { | ||
| 978 | 26 | nft_print(octx, "tproxy"); | |
| 979 | |||
| 980 |
2/2✓ Branch 0 (3→4) taken 11 times.
✓ Branch 1 (3→7) taken 15 times.
|
26 | if (stmt->tproxy.table_family == NFPROTO_INET && |
| 981 |
2/2✓ Branch 0 (4→5) taken 9 times.
✓ Branch 1 (4→7) taken 2 times.
|
11 | stmt->tproxy.family != NFPROTO_UNSPEC) |
| 982 | 9 | nft_print(octx, " %s", nfproto_family_name(stmt->tproxy.family)); | |
| 983 | 26 | nft_print(octx, " to"); | |
| 984 |
2/2✓ Branch 0 (8→9) taken 14 times.
✓ Branch 1 (8→16) taken 12 times.
|
26 | if (stmt->tproxy.addr) { |
| 985 | 14 | nft_print(octx, " "); | |
| 986 |
1/2✓ Branch 0 (10→11) taken 14 times.
✗ Branch 1 (10→15) not taken.
|
14 | if (stmt->tproxy.addr->etype == EXPR_VALUE && |
| 987 |
2/2✓ Branch 0 (11→12) taken 6 times.
✓ Branch 1 (11→15) taken 8 times.
|
14 | stmt->tproxy.addr->dtype->type == TYPE_IP6ADDR) { |
| 988 | 6 | nft_print(octx, "["); | |
| 989 | 6 | expr_print(stmt->tproxy.addr, octx); | |
| 990 | 6 | nft_print(octx, "]"); | |
| 991 | } else { | ||
| 992 | 8 | expr_print(stmt->tproxy.addr, octx); | |
| 993 | } | ||
| 994 | } | ||
| 995 |
2/2✓ Branch 0 (16→17) taken 20 times.
✓ Branch 1 (16→21) taken 6 times.
|
26 | if (stmt->tproxy.port) { |
| 996 |
2/2✓ Branch 0 (17→18) taken 12 times.
✓ Branch 1 (17→19) taken 8 times.
|
20 | if (!stmt->tproxy.addr) |
| 997 | 12 | nft_print(octx, " "); | |
| 998 | 20 | nft_print(octx, ":"); | |
| 999 | 20 | expr_print(stmt->tproxy.port, octx); | |
| 1000 | } | ||
| 1001 | 26 | } | |
| 1002 | |||
| 1003 | 119 | static void tproxy_stmt_destroy(struct stmt *stmt) | |
| 1004 | { | ||
| 1005 | 119 | expr_free(stmt->tproxy.addr); | |
| 1006 | 119 | expr_free(stmt->tproxy.port); | |
| 1007 | 119 | } | |
| 1008 | |||
| 1009 | static const struct stmt_ops tproxy_stmt_ops = { | ||
| 1010 | .type = STMT_TPROXY, | ||
| 1011 | .name = "tproxy", | ||
| 1012 | .print = tproxy_stmt_print, | ||
| 1013 | .json = tproxy_stmt_json, | ||
| 1014 | .destroy = tproxy_stmt_destroy, | ||
| 1015 | }; | ||
| 1016 | |||
| 1017 | 119 | struct stmt *tproxy_stmt_alloc(const struct location *loc) | |
| 1018 | { | ||
| 1019 | 119 | return stmt_alloc(loc, &tproxy_stmt_ops); | |
| 1020 | } | ||
| 1021 | |||
| 1022 | ✗ | static void xt_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 1023 | { | ||
| 1024 | ✗ | xt_stmt_xlate(stmt, octx); | |
| 1025 | ✗ | } | |
| 1026 | |||
| 1027 | static const struct stmt_ops xt_stmt_ops = { | ||
| 1028 | .type = STMT_XT, | ||
| 1029 | .name = "xt", | ||
| 1030 | .print = xt_stmt_print, | ||
| 1031 | .destroy = xt_stmt_destroy, | ||
| 1032 | .json = xt_stmt_json, | ||
| 1033 | }; | ||
| 1034 | |||
| 1035 | ✗ | struct stmt *xt_stmt_alloc(const struct location *loc) | |
| 1036 | { | ||
| 1037 | ✗ | return stmt_alloc(loc, &xt_stmt_ops); | |
| 1038 | } | ||
| 1039 | |||
| 1040 | 23 | static const char *synproxy_sack_to_str(const uint32_t flags) | |
| 1041 | { | ||
| 1042 |
2/2✓ Branch 0 (2→3) taken 14 times.
✓ Branch 1 (2→4) taken 9 times.
|
23 | if (flags & NF_SYNPROXY_OPT_SACK_PERM) |
| 1043 | 14 | return " sack-perm"; | |
| 1044 | |||
| 1045 | 9 | return ""; | |
| 1046 | } | ||
| 1047 | |||
| 1048 | 23 | static const char *synproxy_timestamp_to_str(const uint32_t flags) | |
| 1049 | { | ||
| 1050 |
2/2✓ Branch 0 (2→3) taken 10 times.
✓ Branch 1 (2→4) taken 13 times.
|
23 | if (flags & NF_SYNPROXY_OPT_TIMESTAMP) |
| 1051 | 10 | return " timestamp"; | |
| 1052 | |||
| 1053 | 13 | return ""; | |
| 1054 | } | ||
| 1055 | |||
| 1056 | 23 | static void synproxy_stmt_print(const struct stmt *stmt, | |
| 1057 | struct output_ctx *octx) | ||
| 1058 | { | ||
| 1059 | 23 | uint32_t flags = stmt->synproxy.flags; | |
| 1060 | 23 | const char *ts_str = synproxy_timestamp_to_str(flags); | |
| 1061 | 23 | const char *sack_str = synproxy_sack_to_str(flags); | |
| 1062 | |||
| 1063 |
2/2✓ Branch 0 (4→5) taken 6 times.
✓ Branch 1 (4→6) taken 17 times.
|
23 | if (flags & (NF_SYNPROXY_OPT_MSS | NF_SYNPROXY_OPT_WSCALE)) |
| 1064 | 6 | nft_print(octx, "synproxy mss %u wscale %u%s%s", | |
| 1065 | 6 | stmt->synproxy.mss, stmt->synproxy.wscale, | |
| 1066 | ts_str, sack_str); | ||
| 1067 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 17 times.
|
17 | else if (flags & NF_SYNPROXY_OPT_MSS) |
| 1068 | ✗ | nft_print(octx, "synproxy mss %u%s%s", stmt->synproxy.mss, | |
| 1069 | ts_str, sack_str); | ||
| 1070 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 17 times.
|
17 | else if (flags & NF_SYNPROXY_OPT_WSCALE) |
| 1071 | ✗ | nft_print(octx, "synproxy wscale %u%s%s", stmt->synproxy.wscale, | |
| 1072 | ts_str, sack_str); | ||
| 1073 | else | ||
| 1074 | 17 | nft_print(octx, "synproxy%s%s", ts_str, sack_str); | |
| 1075 | |||
| 1076 | 23 | } | |
| 1077 | |||
| 1078 | static const struct stmt_ops synproxy_stmt_ops = { | ||
| 1079 | .type = STMT_SYNPROXY, | ||
| 1080 | .name = "synproxy", | ||
| 1081 | .print = synproxy_stmt_print, | ||
| 1082 | .json = synproxy_stmt_json, | ||
| 1083 | }; | ||
| 1084 | |||
| 1085 | 85 | struct stmt *synproxy_stmt_alloc(const struct location *loc) | |
| 1086 | { | ||
| 1087 | 85 | return stmt_alloc(loc, &synproxy_stmt_ops); | |
| 1088 | } | ||
| 1089 | |||
| 1090 | /* For src/optimize.c */ | ||
| 1091 | static struct stmt_ops invalid_stmt_ops = { | ||
| 1092 | .type = STMT_INVALID, | ||
| 1093 | .name = "unsupported", | ||
| 1094 | }; | ||
| 1095 | |||
| 1096 | − | static const struct stmt_ops *__stmt_ops_by_type(enum stmt_types type) | |
| 1097 | { | ||
| 1098 | − | switch (type) { | |
| 1099 | − | case STMT_INVALID: return &invalid_stmt_ops; | |
| 1100 | − | case STMT_EXPRESSION: return &expr_stmt_ops; | |
| 1101 | − | case STMT_VERDICT: return &verdict_stmt_ops; | |
| 1102 | − | case STMT_METER: return &meter_stmt_ops; | |
| 1103 | − | case STMT_COUNTER: return &counter_stmt_ops; | |
| 1104 | − | case STMT_PAYLOAD: return &payload_stmt_ops; | |
| 1105 | − | case STMT_META: return &meta_stmt_ops; | |
| 1106 | − | case STMT_LIMIT: return &limit_stmt_ops; | |
| 1107 | − | case STMT_LOG: return &log_stmt_ops; | |
| 1108 | − | case STMT_REJECT: return &reject_stmt_ops; | |
| 1109 | − | case STMT_NAT: return &nat_stmt_ops; | |
| 1110 | − | case STMT_TPROXY: return &tproxy_stmt_ops; | |
| 1111 | − | case STMT_QUEUE: return &queue_stmt_ops; | |
| 1112 | − | case STMT_CT: return &ct_stmt_ops; | |
| 1113 | − | case STMT_SET: return &set_stmt_ops; | |
| 1114 | − | case STMT_DUP: return &dup_stmt_ops; | |
| 1115 | − | case STMT_FWD: return &fwd_stmt_ops; | |
| 1116 | − | case STMT_XT: return &xt_stmt_ops; | |
| 1117 | − | case STMT_QUOTA: return "a_stmt_ops; | |
| 1118 | − | case STMT_NOTRACK: return ¬rack_stmt_ops; | |
| 1119 | − | case STMT_OBJREF: return &objref_stmt_ops; | |
| 1120 | − | case STMT_EXTHDR: return &exthdr_stmt_ops; | |
| 1121 | − | case STMT_FLOW_OFFLOAD: return &flow_offload_stmt_ops; | |
| 1122 | − | case STMT_CONNLIMIT: return &connlimit_stmt_ops; | |
| 1123 | − | case STMT_MAP: return &map_stmt_ops; | |
| 1124 | − | case STMT_SYNPROXY: return &synproxy_stmt_ops; | |
| 1125 | − | case STMT_CHAIN: return &chain_stmt_ops; | |
| 1126 | − | case STMT_OPTSTRIP: return &optstrip_stmt_ops; | |
| 1127 | − | case STMT_LAST: return &last_stmt_ops; | |
| 1128 | − | default: | |
| 1129 | − | break; | |
| 1130 | } | ||
| 1131 | |||
| 1132 | − | return NULL; | |
| 1133 | } | ||
| 1134 | |||
| 1135 | 464602 | const struct stmt_ops *stmt_ops(const struct stmt *stmt) | |
| 1136 | { | ||
| 1137 | const struct stmt_ops *ops; | ||
| 1138 | |||
| 1139 | 464602 | ops = __stmt_ops_by_type(stmt->type); | |
| 1140 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 464602 times.
|
464602 | if (!ops) |
| 1141 | ✗ | BUG("Unknown statement type %d", stmt->type); | |
| 1142 | |||
| 1143 | 464602 | return ops; | |
| 1144 | } | ||
| 1145 |