| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2017 Eric Leblond <eric@regit.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 <nftables/libnftables.h> | ||
| 12 | #include <afl++.h> | ||
| 13 | #include <erec.h> | ||
| 14 | #include <mnl.h> | ||
| 15 | #include <parser.h> | ||
| 16 | #include <utils.h> | ||
| 17 | #include <iface.h> | ||
| 18 | #include <cmd.h> | ||
| 19 | #include <errno.h> | ||
| 20 | #include <sys/stat.h> | ||
| 21 | #include <libgen.h> | ||
| 22 | |||
| 23 | 36814 | static int do_mnl_batch_talk(struct netlink_ctx *ctx, struct list_head *err_list, | |
| 24 | uint32_t num_cmds) | ||
| 25 | { | ||
| 26 | #if HAVE_FUZZER_BUILD | ||
| 27 | if (ctx->nft->afl_ctx_stage && | ||
| 28 | ctx->nft->afl_ctx_stage < NFT_AFL_FUZZER_NETLINK_RW) | ||
| 29 | return 0; | ||
| 30 | #endif | ||
| 31 | 36814 | return mnl_batch_talk(ctx, err_list, num_cmds); | |
| 32 | } | ||
| 33 | |||
| 34 | 54278 | static int nft_netlink(struct nft_ctx *nft, | |
| 35 | struct list_head *cmds, struct list_head *msgs) | ||
| 36 | { | ||
| 37 | 54278 | uint32_t batch_seqnum, seqnum = 0, last_seqnum = UINT32_MAX, num_cmds = 0; | |
| 38 | 108556 | struct netlink_ctx ctx = { | |
| 39 | .nft = nft, | ||
| 40 | .msgs = msgs, | ||
| 41 | .list = LIST_HEAD_INIT(ctx.list), | ||
| 42 | 54278 | .batch = mnl_batch_init(), | |
| 43 | }; | ||
| 44 | struct cmd *cmd; | ||
| 45 | struct mnl_err *err, *tmp; | ||
| 46 | 54278 | LIST_HEAD(err_list); | |
| 47 | 54278 | int ret = 0; | |
| 48 | |||
| 49 |
2/2✓ Branch 0 (4→5) taken 305 times.
✓ Branch 1 (4→6) taken 53973 times.
|
54278 | if (list_empty(cmds)) |
| 50 | 305 | goto out; | |
| 51 | |||
| 52 | #if HAVE_FUZZER_BUILD | ||
| 53 | if (nft->afl_ctx_stage && | ||
| 54 | nft->afl_ctx_stage <= NFT_AFL_FUZZER_EVALUATION) | ||
| 55 | goto out; | ||
| 56 | #endif | ||
| 57 | 53973 | batch_seqnum = mnl_batch_begin(ctx.batch, mnl_seqnum_inc(&seqnum)); | |
| 58 | |||
| 59 |
2/2✓ Branch 0 (17→9) taken 186699 times.
✓ Branch 1 (17→18) taken 53946 times.
|
240645 | list_for_each_entry(cmd, cmds, list) { |
| 60 | 186699 | ctx.seqnum = cmd->seqnum_from = mnl_seqnum_inc(&seqnum); | |
| 61 | 186699 | ret = do_command(&ctx, cmd); | |
| 62 |
2/2✓ Branch 0 (11→12) taken 27 times.
✓ Branch 1 (11→15) taken 186672 times.
|
186699 | if (ret < 0) { |
| 63 | 27 | netlink_io_error(&ctx, &cmd->location, | |
| 64 | "Could not process rule: %s", | ||
| 65 | 27 | strerror(errno)); | |
| 66 | 27 | goto out; | |
| 67 | } | ||
| 68 | 186672 | seqnum = cmd->seqnum_to = ctx.seqnum; | |
| 69 | 186672 | mnl_seqnum_inc(&seqnum); | |
| 70 | 186672 | num_cmds++; | |
| 71 | } | ||
| 72 |
2/2✓ Branch 0 (18→19) taken 52748 times.
✓ Branch 1 (18→21) taken 1198 times.
|
53946 | if (!nft->check) |
| 73 | 52748 | mnl_batch_end(ctx.batch, mnl_seqnum_inc(&seqnum)); | |
| 74 | |||
| 75 |
2/2✓ Branch 0 (22→23) taken 17132 times.
✓ Branch 1 (22→24) taken 36814 times.
|
53946 | if (!mnl_batch_ready(ctx.batch)) |
| 76 | 17132 | goto out; | |
| 77 | |||
| 78 | 36814 | ret = do_mnl_batch_talk(&ctx, &err_list, num_cmds); | |
| 79 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→35) taken 36814 times.
|
36814 | if (ret < 0) { |
| 80 | ✗ | if (ctx.maybe_emsgsize && errno == EMSGSIZE) { | |
| 81 | ✗ | netlink_io_error(&ctx, NULL, | |
| 82 | "Could not process rule: %s\n" | ||
| 83 | "Please, rise /proc/sys/net/core/wmem_max on the host namespace. Hint: %d bytes", | ||
| 84 | ✗ | strerror(errno), round_pow_2(ctx.maybe_emsgsize)); | |
| 85 | ✗ | goto out; | |
| 86 | } | ||
| 87 | ✗ | netlink_io_error(&ctx, NULL, | |
| 88 | ✗ | "Could not process rule: %s", strerror(errno)); | |
| 89 | ✗ | goto out; | |
| 90 | } | ||
| 91 | |||
| 92 |
2/2✓ Branch 0 (36→37) taken 743 times.
✓ Branch 1 (36→38) taken 36071 times.
|
36814 | if (!list_empty(&err_list)) |
| 93 | 743 | ret = -1; | |
| 94 | |||
| 95 |
2/2✓ Branch 0 (55→39) taken 941 times.
✓ Branch 1 (55→56) taken 36814 times.
|
37755 | list_for_each_entry_safe(err, tmp, &err_list, head) { |
| 96 | /* cmd seqnums are monotonic: only reset the starting position | ||
| 97 | * if the error seqnum is lower than the previous one. | ||
| 98 | */ | ||
| 99 |
2/2✓ Branch 0 (39→40) taken 743 times.
✓ Branch 1 (39→41) taken 198 times.
|
941 | if (err->seqnum < last_seqnum) |
| 100 | 743 | cmd = list_first_entry(cmds, struct cmd, list); | |
| 101 | |||
| 102 |
1/2✓ Branch 0 (51→42) taken 8840 times.
✗ Branch 1 (51→52) not taken.
|
8840 | list_for_each_entry_from(cmd, cmds, list) { |
| 103 | 8840 | last_seqnum = cmd->seqnum_to; | |
| 104 |
1/2✓ Branch 0 (42→43) taken 8840 times.
✗ Branch 1 (42→44) not taken.
|
8840 | if ((err->seqnum >= cmd->seqnum_from && |
| 105 |
2/2✓ Branch 0 (43→44) taken 7899 times.
✓ Branch 1 (43→45) taken 941 times.
|
8840 | err->seqnum <= cmd->seqnum_to) || |
| 106 |
1/2✗ Branch 0 (44→45) not taken.
✓ Branch 1 (44→50) taken 7899 times.
|
7899 | err->seqnum == batch_seqnum) { |
| 107 | 941 | nft_cmd_error(&ctx, cmd, err); | |
| 108 | 941 | errno = err->err; | |
| 109 |
1/2✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→48) taken 941 times.
|
941 | if (err->seqnum >= cmd->seqnum_from || |
| 110 | ✗ | err->seqnum <= cmd->seqnum_to) { | |
| 111 | 941 | mnl_err_list_free(err); | |
| 112 | 941 | break; | |
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 |
1/2✗ Branch 0 (52→53) not taken.
✓ Branch 1 (52→54) taken 941 times.
|
941 | if (&cmd->list == cmds) { |
| 118 | /* not found, rewind */ | ||
| 119 | ✗ | last_seqnum = UINT32_MAX; | |
| 120 | } | ||
| 121 | } | ||
| 122 | /* nfnetlink uses the first netlink message header in the batch whose | ||
| 123 | * sequence number is zero to report for EOPNOTSUPP and EPERM errors in | ||
| 124 | * some scenarios. Now it is safe to release pending errors here. | ||
| 125 | */ | ||
| 126 |
1/2✗ Branch 0 (59→57) not taken.
✓ Branch 1 (59→60) taken 36814 times.
|
36814 | list_for_each_entry_safe(err, tmp, &err_list, head) |
| 127 | ✗ | mnl_err_list_free(err); | |
| 128 | 36814 | out: | |
| 129 | 54278 | mnl_batch_reset(ctx.batch); | |
| 130 | 54278 | return ret; | |
| 131 | } | ||
| 132 | |||
| 133 | 13021 | static void nft_init(struct nft_ctx *ctx) | |
| 134 | { | ||
| 135 | 13021 | mark_table_init(ctx); | |
| 136 | 13021 | realm_table_rt_init(ctx); | |
| 137 | 13021 | devgroup_table_init(ctx); | |
| 138 | 13021 | ct_label_table_init(ctx); | |
| 139 | 13021 | } | |
| 140 | |||
| 141 | 13021 | static void nft_exit(struct nft_ctx *ctx) | |
| 142 | { | ||
| 143 | 13021 | cache_free(&ctx->cache.table_cache); | |
| 144 | 13021 | ct_label_table_exit(ctx); | |
| 145 | 13021 | realm_table_rt_exit(ctx); | |
| 146 | 13021 | devgroup_table_exit(ctx); | |
| 147 | 13021 | mark_table_exit(ctx); | |
| 148 | 13021 | } | |
| 149 | |||
| 150 | EXPORT_SYMBOL(nft_ctx_add_var); | ||
| 151 | 5 | int nft_ctx_add_var(struct nft_ctx *ctx, const char *var) | |
| 152 | { | ||
| 153 | 5 | char *separator = strchr(var, '='); | |
| 154 | 5 | int pcount = ctx->num_vars; | |
| 155 | struct nft_vars *tmp; | ||
| 156 | const char *value; | ||
| 157 | |||
| 158 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 5 times.
|
5 | if (!separator) |
| 159 | ✗ | return -1; | |
| 160 | |||
| 161 | 5 | tmp = xrealloc(ctx->vars, (pcount + 1) * sizeof(struct nft_vars)); | |
| 162 | |||
| 163 | 5 | *separator = '\0'; | |
| 164 | 5 | value = separator + 1; | |
| 165 | |||
| 166 | 5 | ctx->vars = tmp; | |
| 167 | 5 | ctx->vars[pcount].key = xstrdup(var); | |
| 168 | 5 | ctx->vars[pcount].value = xstrdup(value); | |
| 169 | 5 | ctx->num_vars++; | |
| 170 | |||
| 171 | 5 | return 0; | |
| 172 | } | ||
| 173 | |||
| 174 | EXPORT_SYMBOL(nft_ctx_clear_vars); | ||
| 175 | 13021 | void nft_ctx_clear_vars(struct nft_ctx *ctx) | |
| 176 | { | ||
| 177 | unsigned int i; | ||
| 178 | |||
| 179 |
2/2✓ Branch 0 (4→3) taken 5 times.
✓ Branch 1 (4→5) taken 13021 times.
|
13026 | for (i = 0; i < ctx->num_vars; i++) { |
| 180 | 5 | free_const(ctx->vars[i].key); | |
| 181 | 5 | free_const(ctx->vars[i].value); | |
| 182 | } | ||
| 183 | 13021 | ctx->num_vars = 0; | |
| 184 | 13021 | free(ctx->vars); | |
| 185 | 13021 | ctx->vars = NULL; | |
| 186 | 13021 | } | |
| 187 | |||
| 188 | 1622 | static bool nft_ctx_find_include_path(struct nft_ctx *ctx, const char *path) | |
| 189 | { | ||
| 190 | unsigned int i; | ||
| 191 | |||
| 192 |
2/2✓ Branch 0 (6→3) taken 6 times.
✓ Branch 1 (6→7) taken 1620 times.
|
1626 | for (i = 0; i < ctx->num_include_paths; i++) { |
| 193 |
2/2✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 4 times.
|
6 | if (!strcmp(ctx->include_paths[i], path)) |
| 194 | 2 | return true; | |
| 195 | } | ||
| 196 | |||
| 197 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 1620 times.
|
1620 | if (!strcmp(path, DEFAULT_INCLUDE_PATH)) |
| 198 | ✗ | return true; | |
| 199 | |||
| 200 | 1620 | return false; | |
| 201 | } | ||
| 202 | |||
| 203 | − | static int __nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path) | |
| 204 | { | ||
| 205 | char **tmp; | ||
| 206 | − | int pcount = ctx->num_include_paths; | |
| 207 | |||
| 208 | − | tmp = xrealloc(ctx->include_paths, (pcount + 1) * sizeof(char *)); | |
| 209 | |||
| 210 | − | ctx->include_paths = tmp; | |
| 211 | |||
| 212 | − | if (asprintf(&ctx->include_paths[pcount], "%s", path) < 0) | |
| 213 | − | return -1; | |
| 214 | |||
| 215 | − | ctx->num_include_paths++; | |
| 216 | − | return 0; | |
| 217 | } | ||
| 218 | |||
| 219 | EXPORT_SYMBOL(nft_ctx_add_include_path); | ||
| 220 | 1622 | int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path) | |
| 221 | { | ||
| 222 | char canonical_path[PATH_MAX]; | ||
| 223 | |||
| 224 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 1622 times.
|
1622 | if (!realpath(path, canonical_path)) |
| 225 | ✗ | return -1; | |
| 226 | |||
| 227 |
2/2✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→8) taken 1620 times.
|
1622 | if (nft_ctx_find_include_path(ctx, canonical_path)) |
| 228 | 2 | return 0; | |
| 229 | |||
| 230 | 1620 | return __nft_ctx_add_include_path(ctx, canonical_path); | |
| 231 | } | ||
| 232 | |||
| 233 | EXPORT_SYMBOL(nft_ctx_clear_include_paths); | ||
| 234 | 13021 | void nft_ctx_clear_include_paths(struct nft_ctx *ctx) | |
| 235 | { | ||
| 236 |
2/2✓ Branch 0 (4→3) taken 1620 times.
✓ Branch 1 (4→5) taken 13021 times.
|
14641 | while (ctx->num_include_paths) |
| 237 | 1620 | free(ctx->include_paths[--ctx->num_include_paths]); | |
| 238 | |||
| 239 | 13021 | free(ctx->include_paths); | |
| 240 | 13021 | ctx->include_paths = NULL; | |
| 241 | 13021 | } | |
| 242 | |||
| 243 | EXPORT_SYMBOL(nft_ctx_new); | ||
| 244 | 13021 | struct nft_ctx *nft_ctx_new(uint32_t flags) | |
| 245 | { | ||
| 246 | struct nft_ctx *ctx; | ||
| 247 | |||
| 248 | #ifdef HAVE_LIBXTABLES | ||
| 249 | 13021 | xt_init(); | |
| 250 | #endif | ||
| 251 | |||
| 252 | 13021 | ctx = xzalloc(sizeof(struct nft_ctx)); | |
| 253 | 13021 | nft_init(ctx); | |
| 254 | |||
| 255 | 13021 | ctx->state = xzalloc(sizeof(struct parser_state)); | |
| 256 | 13021 | ctx->parser_max_errors = 10; | |
| 257 | 13021 | cache_init(&ctx->cache.table_cache); | |
| 258 | 13021 | ctx->top_scope = scope_alloc(); | |
| 259 | 13021 | ctx->flags = flags; | |
| 260 | 13021 | ctx->output.output_fp = stdout; | |
| 261 | 13021 | ctx->output.error_fp = stderr; | |
| 262 | 13021 | init_list_head(&ctx->vars_ctx.indesc_list); | |
| 263 | |||
| 264 | 13021 | ctx->nf_sock = nft_mnl_socket_open(); | |
| 265 | |||
| 266 | 13021 | return ctx; | |
| 267 | } | ||
| 268 | |||
| 269 | 174015 | static ssize_t cookie_write(void *cptr, const char *buf, size_t buflen) | |
| 270 | { | ||
| 271 | 174015 | struct cookie *cookie = cptr; | |
| 272 | |||
| 273 |
2/2✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→5) taken 174012 times.
|
174015 | if (!cookie->buflen) { |
| 274 | 3 | cookie->buflen = buflen + 1; | |
| 275 | 3 | cookie->buf = xmalloc(cookie->buflen); | |
| 276 |
2/2✓ Branch 0 (5→6) taken 14 times.
✓ Branch 1 (5→11) taken 173998 times.
|
174012 | } else if (cookie->pos + buflen >= cookie->buflen) { |
| 277 | 14 | size_t newlen = cookie->buflen * 2; | |
| 278 | |||
| 279 |
2/2✓ Branch 0 (8→7) taken 2 times.
✓ Branch 1 (8→9) taken 14 times.
|
16 | while (newlen <= cookie->pos + buflen) |
| 280 | 2 | newlen *= 2; | |
| 281 | |||
| 282 | 14 | cookie->buf = xrealloc(cookie->buf, newlen); | |
| 283 | 14 | cookie->buflen = newlen; | |
| 284 | } | ||
| 285 | 174015 | memcpy(cookie->buf + cookie->pos, buf, buflen); | |
| 286 | 174015 | cookie->pos += buflen; | |
| 287 | 174015 | cookie->buf[cookie->pos] = '\0'; | |
| 288 | |||
| 289 | 174015 | return buflen; | |
| 290 | } | ||
| 291 | |||
| 292 | 4 | static int init_cookie(struct cookie *cookie) | |
| 293 | { | ||
| 294 | 4 | cookie_io_functions_t cookie_fops = { | |
| 295 | .write = cookie_write, | ||
| 296 | }; | ||
| 297 | |||
| 298 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→6) taken 4 times.
|
4 | if (cookie->orig_fp) { /* just rewind buffer */ |
| 299 | ✗ | if (cookie->buflen) { | |
| 300 | ✗ | cookie->pos = 0; | |
| 301 | ✗ | cookie->buf[0] = '\0'; | |
| 302 | } | ||
| 303 | ✗ | return 0; | |
| 304 | } | ||
| 305 | |||
| 306 | 4 | cookie->orig_fp = cookie->fp; | |
| 307 | |||
| 308 | 4 | cookie->fp = fopencookie(cookie, "w", cookie_fops); | |
| 309 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 4 times.
|
4 | if (!cookie->fp) { |
| 310 | ✗ | cookie->fp = cookie->orig_fp; | |
| 311 | ✗ | cookie->orig_fp = NULL; | |
| 312 | ✗ | return 1; | |
| 313 | } | ||
| 314 | |||
| 315 | 4 | return 0; | |
| 316 | } | ||
| 317 | |||
| 318 | 26042 | static int exit_cookie(struct cookie *cookie) | |
| 319 | { | ||
| 320 |
2/2✓ Branch 0 (2→3) taken 26038 times.
✓ Branch 1 (2→4) taken 4 times.
|
26042 | if (!cookie->orig_fp) |
| 321 | 26038 | return 1; | |
| 322 | |||
| 323 | 4 | fclose(cookie->fp); | |
| 324 | 4 | cookie->fp = cookie->orig_fp; | |
| 325 | 4 | cookie->orig_fp = NULL; | |
| 326 | 4 | free(cookie->buf); | |
| 327 | 4 | cookie->buf = NULL; | |
| 328 | 4 | cookie->buflen = 0; | |
| 329 | 4 | cookie->pos = 0; | |
| 330 | 4 | return 0; | |
| 331 | } | ||
| 332 | |||
| 333 | EXPORT_SYMBOL(nft_ctx_buffer_output); | ||
| 334 | 2 | int nft_ctx_buffer_output(struct nft_ctx *ctx) | |
| 335 | { | ||
| 336 | 2 | return init_cookie(&ctx->output.output_cookie); | |
| 337 | } | ||
| 338 | |||
| 339 | EXPORT_SYMBOL(nft_ctx_unbuffer_output); | ||
| 340 | ✗ | int nft_ctx_unbuffer_output(struct nft_ctx *ctx) | |
| 341 | { | ||
| 342 | ✗ | return exit_cookie(&ctx->output.output_cookie); | |
| 343 | } | ||
| 344 | |||
| 345 | EXPORT_SYMBOL(nft_ctx_buffer_error); | ||
| 346 | 2 | int nft_ctx_buffer_error(struct nft_ctx *ctx) | |
| 347 | { | ||
| 348 | 2 | return init_cookie(&ctx->output.error_cookie); | |
| 349 | } | ||
| 350 | |||
| 351 | EXPORT_SYMBOL(nft_ctx_unbuffer_error); | ||
| 352 | ✗ | int nft_ctx_unbuffer_error(struct nft_ctx *ctx) | |
| 353 | { | ||
| 354 | ✗ | return exit_cookie(&ctx->output.error_cookie); | |
| 355 | } | ||
| 356 | |||
| 357 | 86594 | static const char *get_cookie_buffer(struct cookie *cookie) | |
| 358 | { | ||
| 359 | 86594 | fflush(cookie->fp); | |
| 360 | |||
| 361 | /* This is a bit tricky: Rewind the buffer for future use and return | ||
| 362 | * the old content at the same time. Therefore return an empty string | ||
| 363 | * if buffer position is zero, otherwise just rewind buffer position | ||
| 364 | * and return the unmodified buffer. */ | ||
| 365 | |||
| 366 |
2/2✓ Branch 0 (3→4) taken 58606 times.
✓ Branch 1 (3→5) taken 27988 times.
|
86594 | if (!cookie->pos) |
| 367 | 58606 | return ""; | |
| 368 | |||
| 369 | 27988 | cookie->pos = 0; | |
| 370 | 27988 | return cookie->buf; | |
| 371 | } | ||
| 372 | |||
| 373 | EXPORT_SYMBOL(nft_ctx_get_output_buffer); | ||
| 374 | 43297 | const char *nft_ctx_get_output_buffer(struct nft_ctx *ctx) | |
| 375 | { | ||
| 376 | 43297 | return get_cookie_buffer(&ctx->output.output_cookie); | |
| 377 | } | ||
| 378 | |||
| 379 | EXPORT_SYMBOL(nft_ctx_get_error_buffer); | ||
| 380 | 43297 | const char *nft_ctx_get_error_buffer(struct nft_ctx *ctx) | |
| 381 | { | ||
| 382 | 43297 | return get_cookie_buffer(&ctx->output.error_cookie); | |
| 383 | } | ||
| 384 | |||
| 385 | EXPORT_SYMBOL(nft_ctx_free); | ||
| 386 | 13021 | void nft_ctx_free(struct nft_ctx *ctx) | |
| 387 | { | ||
| 388 | 13021 | mnl_socket_close(ctx->nf_sock); | |
| 389 | |||
| 390 | 13021 | exit_cookie(&ctx->output.output_cookie); | |
| 391 | 13021 | exit_cookie(&ctx->output.error_cookie); | |
| 392 | 13021 | iface_cache_release(); | |
| 393 | 13021 | nft_cache_release(&ctx->cache); | |
| 394 | 13021 | nft_ctx_clear_vars(ctx); | |
| 395 | 13021 | nft_ctx_clear_include_paths(ctx); | |
| 396 | 13021 | scope_free(ctx->top_scope); | |
| 397 | 13021 | free(ctx->state); | |
| 398 | 13021 | nft_exit(ctx); | |
| 399 | 13021 | free(ctx); | |
| 400 | 13021 | } | |
| 401 | |||
| 402 | EXPORT_SYMBOL(nft_ctx_set_output); | ||
| 403 | ✗ | FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp) | |
| 404 | { | ||
| 405 | ✗ | FILE *old = ctx->output.output_fp; | |
| 406 | |||
| 407 | ✗ | if (!fp || ferror(fp)) | |
| 408 | ✗ | return NULL; | |
| 409 | |||
| 410 | ✗ | ctx->output.output_fp = fp; | |
| 411 | |||
| 412 | ✗ | return old; | |
| 413 | } | ||
| 414 | |||
| 415 | EXPORT_SYMBOL(nft_ctx_set_error); | ||
| 416 | ✗ | FILE *nft_ctx_set_error(struct nft_ctx *ctx, FILE *fp) | |
| 417 | { | ||
| 418 | ✗ | FILE *old = ctx->output.error_fp; | |
| 419 | |||
| 420 | ✗ | if (!fp || ferror(fp)) | |
| 421 | ✗ | return NULL; | |
| 422 | |||
| 423 | ✗ | ctx->output.error_fp = fp; | |
| 424 | |||
| 425 | ✗ | return old; | |
| 426 | } | ||
| 427 | |||
| 428 | EXPORT_SYMBOL(nft_ctx_get_dry_run); | ||
| 429 | ✗ | bool nft_ctx_get_dry_run(struct nft_ctx *ctx) | |
| 430 | { | ||
| 431 | ✗ | return ctx->check; | |
| 432 | } | ||
| 433 | |||
| 434 | EXPORT_SYMBOL(nft_ctx_set_dry_run); | ||
| 435 | 1576 | void nft_ctx_set_dry_run(struct nft_ctx *ctx, bool dry) | |
| 436 | { | ||
| 437 | 1576 | ctx->check = dry; | |
| 438 | 1576 | } | |
| 439 | |||
| 440 | EXPORT_SYMBOL(nft_ctx_get_optimize); | ||
| 441 | ✗ | uint32_t nft_ctx_get_optimize(struct nft_ctx *ctx) | |
| 442 | { | ||
| 443 | ✗ | return ctx->optimize_flags; | |
| 444 | } | ||
| 445 | |||
| 446 | EXPORT_SYMBOL(nft_ctx_set_optimize); | ||
| 447 | 25 | void nft_ctx_set_optimize(struct nft_ctx *ctx, uint32_t flags) | |
| 448 | { | ||
| 449 | 25 | ctx->optimize_flags = flags; | |
| 450 | 25 | } | |
| 451 | |||
| 452 | EXPORT_SYMBOL(nft_ctx_input_get_flags); | ||
| 453 | ✗ | unsigned int nft_ctx_input_get_flags(struct nft_ctx *ctx) | |
| 454 | { | ||
| 455 | ✗ | return ctx->input.flags; | |
| 456 | } | ||
| 457 | |||
| 458 | EXPORT_SYMBOL(nft_ctx_input_set_flags); | ||
| 459 | ✗ | unsigned int nft_ctx_input_set_flags(struct nft_ctx *ctx, unsigned int flags) | |
| 460 | { | ||
| 461 | unsigned int old_flags; | ||
| 462 | |||
| 463 | ✗ | old_flags = ctx->input.flags; | |
| 464 | ✗ | ctx->input.flags = flags; | |
| 465 | ✗ | return old_flags; | |
| 466 | } | ||
| 467 | |||
| 468 | EXPORT_SYMBOL(nft_ctx_output_get_flags); | ||
| 469 | 70122 | unsigned int nft_ctx_output_get_flags(struct nft_ctx *ctx) | |
| 470 | { | ||
| 471 | 70122 | return ctx->output.flags; | |
| 472 | } | ||
| 473 | |||
| 474 | EXPORT_SYMBOL(nft_ctx_output_set_flags); | ||
| 475 | 83134 | void nft_ctx_output_set_flags(struct nft_ctx *ctx, unsigned int flags) | |
| 476 | { | ||
| 477 | 83134 | ctx->output.flags = flags; | |
| 478 | 83134 | } | |
| 479 | |||
| 480 | EXPORT_SYMBOL(nft_ctx_output_get_debug); | ||
| 481 | 41015 | unsigned int nft_ctx_output_get_debug(struct nft_ctx *ctx) | |
| 482 | { | ||
| 483 | 41015 | return ctx->debug_mask; | |
| 484 | } | ||
| 485 | EXPORT_SYMBOL(nft_ctx_output_set_debug); | ||
| 486 | 27347 | void nft_ctx_output_set_debug(struct nft_ctx *ctx, unsigned int mask) | |
| 487 | { | ||
| 488 | 27347 | ctx->debug_mask = mask; | |
| 489 | 27347 | } | |
| 490 | |||
| 491 | static const struct input_descriptor indesc_cmdline = { | ||
| 492 | .type = INDESC_BUFFER, | ||
| 493 | .name = "<cmdline>", | ||
| 494 | }; | ||
| 495 | |||
| 496 | 48837 | static int nft_parse_bison_buffer(struct nft_ctx *nft, const char *buf, | |
| 497 | struct list_head *msgs, struct list_head *cmds, | ||
| 498 | const struct input_descriptor *indesc) | ||
| 499 | { | ||
| 500 | int ret; | ||
| 501 | |||
| 502 | 48837 | parser_init(nft, nft->state, msgs, cmds, nft->top_scope); | |
| 503 | 48837 | nft->scanner = scanner_init(nft->state); | |
| 504 | 48837 | scanner_push_buffer(nft->scanner, indesc, buf); | |
| 505 | |||
| 506 | 48837 | ret = nft_parse(nft, nft->scanner, nft->state); | |
| 507 |
4/4✓ Branch 0 (6→7) taken 48835 times.
✓ Branch 1 (6→8) taken 2 times.
✓ Branch 2 (7→8) taken 221 times.
✓ Branch 3 (7→9) taken 48614 times.
|
48837 | if (ret != 0 || nft->state->nerrs > 0) |
| 508 | 223 | return -1; | |
| 509 | |||
| 510 | 48614 | return 0; | |
| 511 | } | ||
| 512 | |||
| 513 | 1797 | static char *stdin_to_buffer(void) | |
| 514 | { | ||
| 515 | 1797 | unsigned int bufsiz = 16384, consumed = 0; | |
| 516 | int numbytes; | ||
| 517 | char *buf; | ||
| 518 | |||
| 519 | 1797 | buf = xmalloc(bufsiz); | |
| 520 | |||
| 521 | 1797 | numbytes = read(STDIN_FILENO, buf, bufsiz); | |
| 522 |
2/2✓ Branch 0 (9→5) taken 1852 times.
✓ Branch 1 (9→10) taken 1797 times.
|
3649 | while (numbytes > 0) { |
| 523 | 1852 | consumed += numbytes; | |
| 524 |
2/2✓ Branch 0 (5→6) taken 24 times.
✓ Branch 1 (5→7) taken 1828 times.
|
1852 | if (consumed == bufsiz) { |
| 525 | 24 | bufsiz *= 2; | |
| 526 | 24 | buf = xrealloc(buf, bufsiz); | |
| 527 | } | ||
| 528 | 1852 | numbytes = read(STDIN_FILENO, buf + consumed, bufsiz - consumed); | |
| 529 | } | ||
| 530 | 1797 | buf[consumed] = '\0'; | |
| 531 | |||
| 532 | 1797 | return buf; | |
| 533 | } | ||
| 534 | |||
| 535 | static const struct input_descriptor indesc_stdin = { | ||
| 536 | .type = INDESC_STDIN, | ||
| 537 | .name = "/dev/stdin", | ||
| 538 | }; | ||
| 539 | |||
| 540 | 2422 | static int nft_parse_bison_filename(struct nft_ctx *nft, const char *filename, | |
| 541 | struct list_head *msgs, struct list_head *cmds) | ||
| 542 | { | ||
| 543 | int ret; | ||
| 544 | |||
| 545 |
2/2✓ Branch 0 (2→3) taken 1769 times.
✓ Branch 1 (2→5) taken 653 times.
|
2422 | if (nft->stdin_buf) |
| 546 | 1769 | return nft_parse_bison_buffer(nft, nft->stdin_buf, msgs, cmds, | |
| 547 | &indesc_stdin); | ||
| 548 | |||
| 549 | 653 | parser_init(nft, nft->state, msgs, cmds, nft->top_scope); | |
| 550 | 653 | nft->scanner = scanner_init(nft->state); | |
| 551 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 653 times.
|
653 | if (scanner_read_file(nft, filename, &internal_location) < 0) |
| 552 | ✗ | return -1; | |
| 553 | |||
| 554 | 653 | ret = nft_parse(nft, nft->scanner, nft->state); | |
| 555 |
4/4✓ Branch 0 (11→12) taken 643 times.
✓ Branch 1 (11→13) taken 10 times.
✓ Branch 2 (12→13) taken 21 times.
✓ Branch 3 (12→14) taken 622 times.
|
653 | if (ret != 0 || nft->state->nerrs > 0) |
| 556 | 31 | return -1; | |
| 557 | |||
| 558 | 622 | return 0; | |
| 559 | } | ||
| 560 | |||
| 561 | 56361 | static int nft_evaluate(struct nft_ctx *nft, struct list_head *msgs, | |
| 562 | struct list_head *cmds) | ||
| 563 | { | ||
| 564 | struct nft_cache_filter *filter; | ||
| 565 | struct cmd *cmd, *next; | ||
| 566 | unsigned int flags; | ||
| 567 | 56361 | int err = 0; | |
| 568 | |||
| 569 | 56361 | filter = nft_cache_filter_init(); | |
| 570 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 56361 times.
|
56361 | if (nft_cache_evaluate(nft, cmds, msgs, filter, &flags) < 0) { |
| 571 | ✗ | nft_cache_filter_fini(filter); | |
| 572 | ✗ | return -1; | |
| 573 | } | ||
| 574 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 56361 times.
|
56361 | if (nft_cache_update(nft, flags, msgs, filter) < 0) { |
| 575 | ✗ | nft_cache_filter_fini(filter); | |
| 576 | ✗ | return -1; | |
| 577 | } | ||
| 578 | |||
| 579 | 56361 | nft_cache_filter_fini(filter); | |
| 580 | |||
| 581 |
2/2✓ Branch 0 (18→13) taken 184647 times.
✓ Branch 1 (18→19) taken 56361 times.
|
241008 | list_for_each_entry(cmd, cmds, list) { |
| 582 |
2/2✓ Branch 0 (13→14) taken 37323 times.
✓ Branch 1 (13→16) taken 147324 times.
|
184647 | if (cmd->op != CMD_ADD && |
| 583 |
2/2✓ Branch 0 (14→15) taken 37096 times.
✓ Branch 1 (14→16) taken 227 times.
|
37323 | cmd->op != CMD_CREATE) |
| 584 | 37096 | continue; | |
| 585 | |||
| 586 | 147551 | nft_cmd_expand(cmd); | |
| 587 | } | ||
| 588 | |||
| 589 |
2/2✓ Branch 0 (25→20) taken 184647 times.
✓ Branch 1 (25→26) taken 56361 times.
|
241008 | list_for_each_entry_safe(cmd, next, cmds, list) { |
| 590 | 184647 | struct eval_ctx ectx = { | |
| 591 | .nft = nft, | ||
| 592 | .msgs = msgs, | ||
| 593 | }; | ||
| 594 | |||
| 595 |
2/2✓ Branch 0 (21→22) taken 1848 times.
✓ Branch 1 (21→24) taken 182799 times.
|
184647 | if (cmd_evaluate(&ectx, cmd) < 0 && |
| 596 |
1/2✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 1848 times.
|
1848 | ++nft->state->nerrs == nft->parser_max_errors) { |
| 597 | ✗ | err = -1; | |
| 598 | ✗ | break; | |
| 599 | } | ||
| 600 | } | ||
| 601 | |||
| 602 |
3/4✓ Branch 0 (26→27) taken 56361 times.
✗ Branch 1 (26→28) not taken.
✓ Branch 2 (27→28) taken 2054 times.
✓ Branch 3 (27→29) taken 54307 times.
|
56361 | if (err < 0 || nft->state->nerrs) |
| 603 | 2054 | return -1; | |
| 604 | |||
| 605 | 54307 | return 0; | |
| 606 | } | ||
| 607 | |||
| 608 | EXPORT_SYMBOL(nft_run_cmd_from_buffer); | ||
| 609 | 52924 | int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf) | |
| 610 | { | ||
| 611 | 52924 | int rc = -EINVAL, parser_rc; | |
| 612 | struct cmd *cmd, *next; | ||
| 613 | 52924 | LIST_HEAD(msgs); | |
| 614 | 52924 | LIST_HEAD(cmds); | |
| 615 | char *nlbuf; | ||
| 616 | |||
| 617 | 52924 | nlbuf = xzalloc(strlen(buf) + 2); | |
| 618 | 52924 | sprintf(nlbuf, "%s\n", buf); | |
| 619 | |||
| 620 |
3/4✓ Branch 0 (4→5) taken 40724 times.
✓ Branch 1 (4→7) taken 12200 times.
✗ Branch 2 (6→7) not taken.
✓ Branch 3 (6→8) taken 40724 times.
|
52924 | if (nft_output_json(&nft->output) || nft_input_json(&nft->input)) |
| 621 | 12200 | rc = nft_parse_json_buffer(nft, nlbuf, &msgs, &cmds); | |
| 622 |
2/2✓ Branch 0 (8→9) taken 47064 times.
✓ Branch 1 (8→10) taken 5860 times.
|
52924 | if (rc == -EINVAL) |
| 623 | 47064 | rc = nft_parse_bison_buffer(nft, nlbuf, &msgs, &cmds, | |
| 624 | &indesc_cmdline); | ||
| 625 | |||
| 626 | #if HAVE_FUZZER_BUILD | ||
| 627 | if (nft->afl_ctx_stage == NFT_AFL_FUZZER_PARSER) | ||
| 628 | goto err; | ||
| 629 | #endif | ||
| 630 | 52924 | parser_rc = rc; | |
| 631 | |||
| 632 | 52924 | rc = nft_evaluate(nft, &msgs, &cmds); | |
| 633 |
2/2✓ Branch 0 (11→12) taken 1932 times.
✓ Branch 1 (11→16) taken 50992 times.
|
52924 | if (rc < 0) { |
| 634 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→15) taken 1932 times.
|
1932 | if (errno == EPERM) { |
| 635 | ✗ | fprintf(stderr, "%s (you must be root)\n", | |
| 636 | ✗ | strerror(errno)); | |
| 637 | } | ||
| 638 | 1932 | goto err; | |
| 639 | } | ||
| 640 | |||
| 641 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 50992 times.
|
50992 | if (parser_rc) { |
| 642 | ✗ | rc = parser_rc; | |
| 643 | ✗ | goto err; | |
| 644 | } | ||
| 645 | |||
| 646 |
2/2✓ Branch 0 (19→20) taken 50270 times.
✓ Branch 1 (19→21) taken 722 times.
|
50992 | if (nft_netlink(nft, &cmds, &msgs) != 0) |
| 647 | 722 | rc = -1; | |
| 648 | 50270 | err: | |
| 649 | 52924 | erec_print_list(&nft->output, &msgs, nft->debug_mask); | |
| 650 |
2/2✓ Branch 0 (27→24) taken 56413 times.
✓ Branch 1 (27→28) taken 52924 times.
|
109337 | list_for_each_entry_safe(cmd, next, &cmds, list) { |
| 651 | 56413 | list_del(&cmd->list); | |
| 652 | 56413 | cmd_free(cmd); | |
| 653 | } | ||
| 654 | 52924 | iface_cache_release(); | |
| 655 |
2/2✓ Branch 0 (29→30) taken 47064 times.
✓ Branch 1 (29→32) taken 5860 times.
|
52924 | if (nft->scanner) { |
| 656 | 47064 | scanner_destroy(nft); | |
| 657 | 47064 | nft->scanner = NULL; | |
| 658 | } | ||
| 659 | 52924 | free(nlbuf); | |
| 660 | |||
| 661 |
4/4✓ Branch 0 (32→33) taken 50270 times.
✓ Branch 1 (32→38) taken 2654 times.
✓ Branch 2 (34→35) taken 12200 times.
✓ Branch 3 (34→38) taken 38070 times.
|
103194 | if (!rc && |
| 662 |
2/2✓ Branch 0 (36→37) taken 15 times.
✓ Branch 1 (36→38) taken 12185 times.
|
62470 | nft_output_json(&nft->output) && |
| 663 | 12200 | nft_output_echo(&nft->output)) | |
| 664 | 15 | json_print_echo(nft); | |
| 665 | |||
| 666 |
3/4✓ Branch 0 (38→39) taken 50270 times.
✓ Branch 1 (38→40) taken 2654 times.
✗ Branch 2 (39→40) not taken.
✓ Branch 3 (39→41) taken 50270 times.
|
52924 | if (rc || nft->check) |
| 667 | 2654 | nft_cache_release(&nft->cache); | |
| 668 | |||
| 669 | 52924 | return rc; | |
| 670 | } | ||
| 671 | |||
| 672 | 3438 | static int load_cmdline_vars(struct nft_ctx *ctx, struct list_head *msgs) | |
| 673 | { | ||
| 674 | 3438 | unsigned int bufsize, ret, i, offset = 0; | |
| 675 | 3438 | LIST_HEAD(cmds); | |
| 676 | char *buf; | ||
| 677 | int rc; | ||
| 678 | |||
| 679 |
2/2✓ Branch 0 (2→3) taken 3434 times.
✓ Branch 1 (2→4) taken 4 times.
|
3438 | if (ctx->num_vars == 0) |
| 680 | 3434 | return 0; | |
| 681 | |||
| 682 | 4 | bufsize = 1024; | |
| 683 | 4 | buf = xzalloc(bufsize + 1); | |
| 684 |
2/2✓ Branch 0 (10→11) taken 5 times.
✓ Branch 1 (10→13) taken 4 times.
|
9 | for (i = 0; i < ctx->num_vars; i++) { |
| 685 | 5 | retry: | |
| 686 | 5 | ret = snprintf(buf + offset, bufsize - offset, | |
| 687 | "define %s=%s; ", | ||
| 688 | 5 | ctx->vars[i].key, ctx->vars[i].value); | |
| 689 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 5 times.
|
5 | if (ret >= bufsize - offset) { |
| 690 | ✗ | bufsize *= 2; | |
| 691 | ✗ | buf = xrealloc(buf, bufsize + 1); | |
| 692 | ✗ | goto retry; | |
| 693 | } | ||
| 694 | 5 | offset += ret; | |
| 695 | } | ||
| 696 | 4 | snprintf(buf + offset, bufsize - offset, "\n"); | |
| 697 | |||
| 698 | 4 | rc = nft_parse_bison_buffer(ctx, buf, msgs, &cmds, &indesc_cmdline); | |
| 699 | |||
| 700 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 4 times.
|
4 | assert(list_empty(&cmds)); |
| 701 | /* Stash the buffer that contains the variable definitions and zap the | ||
| 702 | * list of input descriptors before releasing the scanner state, | ||
| 703 | * otherwise error reporting path walks over released objects. | ||
| 704 | */ | ||
| 705 | 4 | ctx->vars_ctx.buf = buf; | |
| 706 | 4 | list_splice_init(&ctx->state->indesc_list, &ctx->vars_ctx.indesc_list); | |
| 707 | 4 | scanner_destroy(ctx); | |
| 708 | 4 | ctx->scanner = NULL; | |
| 709 | |||
| 710 | 4 | return rc; | |
| 711 | } | ||
| 712 | |||
| 713 | /* need to use stat() to, fopen() will block for named fifos and | ||
| 714 | * libjansson makes no checks before or after open either. | ||
| 715 | * /dev/stdin is *never* used, read() from STDIN_FILENO is used instead. | ||
| 716 | */ | ||
| 717 | 3438 | static struct error_record *filename_is_useable(struct nft_ctx *nft, const char *name) | |
| 718 | { | ||
| 719 | unsigned int type; | ||
| 720 | struct stat sb; | ||
| 721 | int err; | ||
| 722 | |||
| 723 |
2/2✓ Branch 0 (2→3) taken 1822 times.
✓ Branch 1 (2→4) taken 1616 times.
|
3438 | if (!strcmp(name, "/dev/stdin")) |
| 724 | 1822 | return NULL; | |
| 725 | |||
| 726 | 1616 | err = stat(name, &sb); | |
| 727 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→9) taken 1616 times.
|
1616 | if (err) |
| 728 | ✗ | return error(&internal_location, "Could not open file \"%s\": %s\n", | |
| 729 | name, strerror(errno)); | ||
| 730 | |||
| 731 | 1616 | type = sb.st_mode & S_IFMT; | |
| 732 | |||
| 733 |
1/4✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 1616 times.
✗ Branch 2 (10→11) not taken.
✗ Branch 3 (10→12) not taken.
|
1616 | if (type == S_IFREG || type == S_IFIFO) |
| 734 | 1616 | return NULL; | |
| 735 | |||
| 736 | ✗ | return error(&internal_location, "Not a regular file: \"%s\"\n", name); | |
| 737 | } | ||
| 738 | |||
| 739 | − | static int __nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename) | |
| 740 | { | ||
| 741 | struct error_record *erec; | ||
| 742 | struct cmd *cmd, *next; | ||
| 743 | int rc, parser_rc; | ||
| 744 | − | LIST_HEAD(msgs); | |
| 745 | − | LIST_HEAD(cmds); | |
| 746 | |||
| 747 | − | erec = filename_is_useable(nft, filename); | |
| 748 | − | if (erec) { | |
| 749 | − | erec_print(&nft->output, erec, nft->debug_mask); | |
| 750 | − | erec_destroy(erec); | |
| 751 | − | return -1; | |
| 752 | } | ||
| 753 | |||
| 754 | − | rc = load_cmdline_vars(nft, &msgs); | |
| 755 | − | if (rc < 0) | |
| 756 | − | goto err; | |
| 757 | |||
| 758 | − | rc = -EINVAL; | |
| 759 | − | if (nft_output_json(&nft->output) || nft_input_json(&nft->input)) | |
| 760 | − | rc = nft_parse_json_filename(nft, filename, &msgs, &cmds); | |
| 761 | − | if (rc == -EINVAL) | |
| 762 | − | rc = nft_parse_bison_filename(nft, filename, &msgs, &cmds); | |
| 763 | |||
| 764 | − | parser_rc = rc; | |
| 765 | |||
| 766 | − | if (nft->optimize_flags) | |
| 767 | − | nft_optimize(nft, &cmds); | |
| 768 | |||
| 769 | − | rc = nft_evaluate(nft, &msgs, &cmds); | |
| 770 | − | if (rc < 0) | |
| 771 | − | goto err; | |
| 772 | |||
| 773 | − | if (parser_rc) { | |
| 774 | − | rc = parser_rc; | |
| 775 | − | goto err; | |
| 776 | } | ||
| 777 | |||
| 778 | − | if (nft_netlink(nft, &cmds, &msgs) != 0) | |
| 779 | − | rc = -1; | |
| 780 | − | err: | |
| 781 | − | erec_print_list(&nft->output, &msgs, nft->debug_mask); | |
| 782 | − | list_for_each_entry_safe(cmd, next, &cmds, list) { | |
| 783 | − | list_del(&cmd->list); | |
| 784 | − | cmd_free(cmd); | |
| 785 | } | ||
| 786 | − | iface_cache_release(); | |
| 787 | − | if (nft->scanner) { | |
| 788 | − | scanner_destroy(nft); | |
| 789 | − | nft->scanner = NULL; | |
| 790 | } | ||
| 791 | − | if (!list_empty(&nft->vars_ctx.indesc_list)) { | |
| 792 | struct input_descriptor *indesc, *next; | ||
| 793 | |||
| 794 | − | list_for_each_entry_safe(indesc, next, &nft->vars_ctx.indesc_list, list) { | |
| 795 | − | if (indesc->name) | |
| 796 | − | free_const(indesc->name); | |
| 797 | |||
| 798 | − | free(indesc); | |
| 799 | } | ||
| 800 | } | ||
| 801 | − | free_const(nft->vars_ctx.buf); | |
| 802 | |||
| 803 | − | if (!rc && | |
| 804 | − | nft_output_json(&nft->output) && | |
| 805 | − | nft_output_echo(&nft->output)) | |
| 806 | − | json_print_echo(nft); | |
| 807 | |||
| 808 | − | if (rc || nft->check) | |
| 809 | − | nft_cache_release(&nft->cache); | |
| 810 | |||
| 811 | − | scope_release(nft->state->scopes[0]); | |
| 812 | |||
| 813 | − | return rc; | |
| 814 | } | ||
| 815 | |||
| 816 | 25 | static int nft_run_optimized_file(struct nft_ctx *nft, const char *filename) | |
| 817 | { | ||
| 818 | uint32_t optimize_flags; | ||
| 819 | bool check; | ||
| 820 | int ret; | ||
| 821 | |||
| 822 | 25 | check = nft->check; | |
| 823 | 25 | nft->check = true; | |
| 824 | 25 | optimize_flags = nft->optimize_flags; | |
| 825 | 25 | nft->optimize_flags = 0; | |
| 826 | |||
| 827 | /* First check the original ruleset loads fine as is. */ | ||
| 828 | 25 | ret = __nft_run_cmd_from_filename(nft, filename); | |
| 829 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 25 times.
|
25 | if (ret < 0) |
| 830 | ✗ | return ret; | |
| 831 | |||
| 832 | 25 | nft->check = check; | |
| 833 | 25 | nft->optimize_flags = optimize_flags; | |
| 834 | |||
| 835 | 25 | return __nft_run_cmd_from_filename(nft, filename); | |
| 836 | } | ||
| 837 | |||
| 838 | 1616 | static int nft_ctx_add_basedir_include_path(struct nft_ctx *nft, | |
| 839 | const char *filename) | ||
| 840 | { | ||
| 841 | 1616 | char *basedir = xstrdup(filename); | |
| 842 | int ret; | ||
| 843 | |||
| 844 | 1616 | ret = nft_ctx_add_include_path(nft, dirname(basedir)); | |
| 845 | |||
| 846 | 1616 | free(basedir); | |
| 847 | |||
| 848 | 1616 | return ret; | |
| 849 | } | ||
| 850 | |||
| 851 | EXPORT_SYMBOL(nft_run_cmd_from_filename); | ||
| 852 | 3413 | int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename) | |
| 853 | { | ||
| 854 | int ret; | ||
| 855 | |||
| 856 |
2/2✓ Branch 0 (2→3) taken 1774 times.
✓ Branch 1 (2→4) taken 1639 times.
|
3413 | if (!strcmp(filename, "-")) |
| 857 | 1774 | filename = "/dev/stdin"; | |
| 858 | |||
| 859 |
2/2✓ Branch 0 (4→5) taken 1797 times.
✓ Branch 1 (4→7) taken 1616 times.
|
3413 | if (!strcmp(filename, "/dev/stdin")) |
| 860 | 1797 | nft->stdin_buf = stdin_to_buffer(); | |
| 861 | |||
| 862 |
3/4✓ Branch 0 (7→8) taken 1616 times.
✓ Branch 1 (7→11) taken 1797 times.
✗ Branch 2 (9→10) not taken.
✓ Branch 3 (9→11) taken 1616 times.
|
5029 | if (!nft->stdin_buf && |
| 863 | 1616 | nft_ctx_add_basedir_include_path(nft, filename) < 0) | |
| 864 | ✗ | return -1; | |
| 865 | |||
| 866 |
2/2✓ Branch 0 (11→12) taken 25 times.
✓ Branch 1 (11→14) taken 3388 times.
|
3413 | if (nft->optimize_flags) { |
| 867 | 25 | ret = nft_run_optimized_file(nft, filename); | |
| 868 | 25 | free_const(nft->stdin_buf); | |
| 869 | 25 | return ret; | |
| 870 | } | ||
| 871 | |||
| 872 | 3388 | ret = __nft_run_cmd_from_filename(nft, filename); | |
| 873 | 3388 | free_const(nft->stdin_buf); | |
| 874 | |||
| 875 | 3388 | return ret; | |
| 876 | } | ||
| 877 |