| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) Red Hat GmbH. Author: Phil Sutter <phil@nwl.cc> | ||
| 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 <errno.h> | ||
| 12 | #include <syslog.h> | ||
| 13 | |||
| 14 | #include <erec.h> | ||
| 15 | #include <expression.h> | ||
| 16 | #include <tcpopt.h> | ||
| 17 | #include <list.h> | ||
| 18 | #include <netlink.h> | ||
| 19 | #include <parser.h> | ||
| 20 | #include <rule.h> | ||
| 21 | #include <cmd.h> | ||
| 22 | #include <sctp_chunk.h> | ||
| 23 | #include <socket.h> | ||
| 24 | |||
| 25 | #include <netdb.h> | ||
| 26 | #include <netinet/icmp6.h> | ||
| 27 | #include <netinet/ip.h> | ||
| 28 | #include <netinet/ip_icmp.h> | ||
| 29 | #include <net/if.h> | ||
| 30 | #include <linux/xfrm.h> | ||
| 31 | |||
| 32 | #include <linux/netfilter.h> | ||
| 33 | #include <linux/netfilter/nf_conntrack_tuple_common.h> | ||
| 34 | #include <linux/netfilter/nf_log.h> | ||
| 35 | #include <linux/netfilter/nf_nat.h> | ||
| 36 | #include <linux/netfilter/nf_synproxy.h> | ||
| 37 | #include <linux/netfilter/nf_tables.h> | ||
| 38 | #include <jansson.h> | ||
| 39 | |||
| 40 | #include <mnl.h> | ||
| 41 | #include <libnftnl/rule.h> | ||
| 42 | #include <linux/netfilter/nfnetlink.h> | ||
| 43 | |||
| 44 | #define CTX_F_RHS (1 << 0) | ||
| 45 | #define CTX_F_STMT (1 << 1) | ||
| 46 | #define CTX_F_PRIMARY (1 << 2) | ||
| 47 | #define CTX_F_DTYPE (1 << 3) | ||
| 48 | #define CTX_F_SET_RHS (1 << 4) | ||
| 49 | #define CTX_F_MANGLE (1 << 5) | ||
| 50 | #define CTX_F_SES (1 << 6) /* set_elem_expr_stmt */ | ||
| 51 | #define CTX_F_MAP (1 << 7) /* LHS of map_expr */ | ||
| 52 | #define CTX_F_CONCAT (1 << 8) /* inside concat_expr */ | ||
| 53 | #define CTX_F_COLLAPSED (1 << 9) | ||
| 54 | #define CTX_F_IMPLICIT (1 << 10) /* implicit add (export/import format) */ | ||
| 55 | |||
| 56 | /* Mask for flags that affect expression parsing context (all except command-level flags) */ | ||
| 57 | #define CTX_F_EXPR_MASK (UINT32_MAX & ~(CTX_F_COLLAPSED | CTX_F_IMPLICIT)) | ||
| 58 | |||
| 59 | struct json_ctx { | ||
| 60 | struct nft_ctx *nft; | ||
| 61 | struct list_head *msgs; | ||
| 62 | struct list_head *cmds; | ||
| 63 | uint32_t flags; | ||
| 64 | }; | ||
| 65 | |||
| 66 | #define is_RHS(ctx) (ctx->flags & CTX_F_RHS) | ||
| 67 | #define is_STMT(ctx) (ctx->flags & CTX_F_STMT) | ||
| 68 | #define is_PRIMARY(ctx) (ctx->flags & CTX_F_PRIMARY) | ||
| 69 | #define is_DTYPE(ctx) (ctx->flags & CTX_F_DTYPE) | ||
| 70 | #define is_SET_RHS(ctx) (ctx->flags & CTX_F_SET_RHS) | ||
| 71 | |||
| 72 | 2 | static char *ctx_flags_to_string(struct json_ctx *ctx) | |
| 73 | { | ||
| 74 | static char buf[1024]; | ||
| 75 | 2 | const char *sep = ""; | |
| 76 | |||
| 77 | 2 | buf[0] = '\0'; | |
| 78 | |||
| 79 |
1/2✓ Branch 0 (2→3) taken 2 times.
✗ Branch 1 (2→4) not taken.
|
2 | if (is_RHS(ctx)) { |
| 80 | 2 | strcat(buf, sep); | |
| 81 | 2 | strcat(buf, "RHS"); | |
| 82 | 2 | sep = ", "; | |
| 83 | } | ||
| 84 |
1/2✓ Branch 0 (4→5) taken 2 times.
✗ Branch 1 (4→6) not taken.
|
2 | if (is_STMT(ctx)) { |
| 85 | 2 | strcat(buf, sep); | |
| 86 | 2 | strcat(buf, "STMT"); | |
| 87 | 2 | sep = ", "; | |
| 88 | } | ||
| 89 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 2 times.
|
2 | if (is_PRIMARY(ctx)) { |
| 90 | ✗ | strcat(buf, sep); | |
| 91 | ✗ | strcat(buf, "PRIMARY"); | |
| 92 | ✗ | sep = ", "; | |
| 93 | } | ||
| 94 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 2 times.
|
2 | if (is_DTYPE(ctx)) { |
| 95 | ✗ | strcat(buf, sep); | |
| 96 | ✗ | strcat(buf, "DTYPE"); | |
| 97 | ✗ | sep = ", "; | |
| 98 | } | ||
| 99 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 2 times.
|
2 | if (is_SET_RHS(ctx)) { |
| 100 | ✗ | strcat(buf, sep); | |
| 101 | ✗ | strcat(buf, "SET_RHS"); | |
| 102 | ✗ | sep = ", "; | |
| 103 | } | ||
| 104 | 2 | return buf; | |
| 105 | } | ||
| 106 | |||
| 107 | /* common parser entry points */ | ||
| 108 | |||
| 109 | static struct expr *json_parse_expr(struct json_ctx *ctx, json_t *root); | ||
| 110 | static struct expr *json_parse_rhs_expr(struct json_ctx *ctx, json_t *root); | ||
| 111 | static struct expr *json_parse_stmt_expr(struct json_ctx *ctx, json_t *root); | ||
| 112 | static struct expr *json_parse_primary_expr(struct json_ctx *ctx, json_t *root); | ||
| 113 | static struct expr *json_parse_set_rhs_expr(struct json_ctx *ctx, json_t *root); | ||
| 114 | static struct expr *json_parse_set_elem_expr_stmt(struct json_ctx *ctx, json_t *root); | ||
| 115 | static struct expr *json_parse_map_lhs_expr(struct json_ctx *ctx, json_t *root); | ||
| 116 | static struct expr *json_parse_concat_elem_expr(struct json_ctx *ctx, json_t *root); | ||
| 117 | static struct stmt *json_parse_stmt(struct json_ctx *ctx, json_t *root); | ||
| 118 | |||
| 119 | /* parsing helpers */ | ||
| 120 | |||
| 121 | const struct location *int_loc = &internal_location; | ||
| 122 | static struct input_descriptor json_indesc; | ||
| 123 | |||
| 124 | ✗ | static void json_lib_error(struct json_ctx *ctx, json_error_t *err) | |
| 125 | { | ||
| 126 | ✗ | struct location loc = { | |
| 127 | .indesc = &json_indesc, | ||
| 128 | ✗ | .line_offset = err->position - err->column, | |
| 129 | ✗ | .first_line = err->line, | |
| 130 | ✗ | .first_column = err->column, | |
| 131 | /* no information where problematic part ends :( */ | ||
| 132 | ✗ | .last_column = err->column, | |
| 133 | }; | ||
| 134 | |||
| 135 | ✗ | erec_queue(error(&loc, "%s", err->text), ctx->msgs); | |
| 136 | ✗ | } | |
| 137 | |||
| 138 | __attribute__((format(printf, 2, 3))) | ||
| 139 | 69 | static void json_error(struct json_ctx *ctx, const char *fmt, ...) | |
| 140 | { | ||
| 141 | struct error_record *erec; | ||
| 142 | va_list ap; | ||
| 143 | |||
| 144 | 69 | va_start(ap, fmt); | |
| 145 | 69 | erec = erec_vcreate(EREC_ERROR, int_loc, fmt, ap); | |
| 146 | 69 | va_end(ap); | |
| 147 | 69 | erec_queue(erec, ctx->msgs); | |
| 148 | 69 | } | |
| 149 | |||
| 150 | 10 | static const char *json_typename(const json_t *val) | |
| 151 | { | ||
| 152 | 10 | const char *type_name[] = { | |
| 153 | [JSON_OBJECT] = "object", | ||
| 154 | [JSON_ARRAY] = "array", | ||
| 155 | [JSON_STRING] = "string", | ||
| 156 | [JSON_INTEGER] = "integer", | ||
| 157 | [JSON_REAL] = "real", | ||
| 158 | [JSON_TRUE] = "true", | ||
| 159 | [JSON_FALSE] = "false", | ||
| 160 | [JSON_NULL] = "null" | ||
| 161 | }; | ||
| 162 | |||
| 163 | 10 | return type_name[json_typeof(val)]; | |
| 164 | } | ||
| 165 | |||
| 166 | 349968 | static int json_unpack_err(struct json_ctx *ctx, | |
| 167 | json_t *root, const char *fmt, ...) | ||
| 168 | { | ||
| 169 | json_error_t err; | ||
| 170 | va_list ap; | ||
| 171 | int rc; | ||
| 172 | |||
| 173 | 349968 | va_start(ap, fmt); | |
| 174 | 349968 | rc = json_vunpack_ex(root, &err, 0, fmt, ap); | |
| 175 | 349968 | va_end(ap); | |
| 176 | |||
| 177 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 349968 times.
|
349968 | if (rc) |
| 178 | ✗ | json_lib_error(ctx, &err); | |
| 179 | 349968 | return rc; | |
| 180 | } | ||
| 181 | |||
| 182 | 246445 | static int json_unpack_stmt(struct json_ctx *ctx, json_t *root, | |
| 183 | const char **key, json_t **value) | ||
| 184 | { | ||
| 185 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 246445 times.
|
246445 | assert(key); |
| 186 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 246445 times.
|
246445 | assert(value); |
| 187 | |||
| 188 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→11) taken 246444 times.
|
246445 | if (json_object_size(root) != 1) { |
| 189 | 1 | const char *dump = json_dumps(root, 0); | |
| 190 | |||
| 191 | 1 | json_error(ctx, "Malformed object (too many properties): '%s'.", | |
| 192 | dump); | ||
| 193 | 1 | free_const(dump); | |
| 194 | 1 | return 1; | |
| 195 | } | ||
| 196 | |||
| 197 |
2/4✓ Branch 0 (15→16) taken 246444 times.
✗ Branch 1 (15→19) not taken.
✓ Branch 2 (18→14) taken 246444 times.
✗ Branch 3 (18→19) not taken.
|
246444 | json_object_foreach(root, *key, *value) |
| 198 | 246444 | return 0; | |
| 199 | |||
| 200 | /* not reached */ | ||
| 201 | ✗ | return 1; | |
| 202 | } | ||
| 203 | |||
| 204 | /** | ||
| 205 | * parse_flags_array - parse JSON property as an array of flags | ||
| 206 | * | ||
| 207 | * @ctx: JSON parser context | ||
| 208 | * @obj: JSON object to extract property from | ||
| 209 | * @key: name of property containing the flags array | ||
| 210 | * @flag_parser: Callback parsing a single flag, returns 0 on error | ||
| 211 | * | ||
| 212 | * The property value may be a string representing a single flag or an array of | ||
| 213 | * strings representing a number of flags whose values are ORed together. | ||
| 214 | * | ||
| 215 | * @return: Combined flag value, 0 if no such property found or -1 if data is | ||
| 216 | * malformed or flag parsing failed. | ||
| 217 | */ | ||
| 218 | 2386 | static int parse_flags_array(struct json_ctx *ctx, json_t *obj, const char *key, | |
| 219 | unsigned int (*flag_parser)(const char *flag)) | ||
| 220 | { | ||
| 221 | 2386 | json_t *value = json_object_get(obj, key), *tmp; | |
| 222 | size_t index; | ||
| 223 | 2386 | int ret = 0; | |
| 224 | |||
| 225 |
2/2✓ Branch 0 (3→4) taken 1890 times.
✓ Branch 1 (3→5) taken 496 times.
|
2386 | if (!value) |
| 226 | 1890 | return 0; | |
| 227 | |||
| 228 |
3/4✓ Branch 0 (5→6) taken 496 times.
✗ Branch 1 (5→13) not taken.
✓ Branch 2 (6→7) taken 67 times.
✓ Branch 3 (6→13) taken 429 times.
|
496 | if (json_is_string(value)) { |
| 229 | 67 | ret = flag_parser(json_string_value(value)); | |
| 230 |
1/2✓ Branch 0 (9→10) taken 67 times.
✗ Branch 1 (9→11) not taken.
|
67 | return ret ?: -1; |
| 231 | } | ||
| 232 | |||
| 233 |
2/4✓ Branch 0 (13→14) taken 429 times.
✗ Branch 1 (13→15) not taken.
✗ Branch 2 (14→15) not taken.
✓ Branch 3 (14→17) taken 429 times.
|
429 | if (!json_is_array(value)) { |
| 234 | ✗ | json_error(ctx, | |
| 235 | "Expecting string or array in '%s' property.", key); | ||
| 236 | ✗ | return -1; | |
| 237 | } | ||
| 238 | |||
| 239 |
3/4✓ Branch 0 (28→29) taken 619 times.
✓ Branch 1 (28→31) taken 429 times.
✓ Branch 2 (30→18) taken 619 times.
✗ Branch 3 (30→31) not taken.
|
1048 | json_array_foreach(value, index, tmp) { |
| 240 | 619 | int flag = 0; | |
| 241 | |||
| 242 |
2/4✓ Branch 0 (18→19) taken 619 times.
✗ Branch 1 (18→23) not taken.
✓ Branch 2 (19→20) taken 619 times.
✗ Branch 3 (19→23) not taken.
|
619 | if (json_is_string(tmp)) |
| 243 | 619 | flag = flag_parser(json_string_value(tmp)); | |
| 244 | |||
| 245 |
1/2✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→26) taken 619 times.
|
619 | if (!flag) { |
| 246 | ✗ | json_error(ctx, | |
| 247 | "Invalid flag in '%s' property array at index %zu.", | ||
| 248 | key, index); | ||
| 249 | ✗ | return -1; | |
| 250 | } | ||
| 251 | |||
| 252 | 619 | ret |= flag; | |
| 253 | } | ||
| 254 | |||
| 255 | 429 | return ret; | |
| 256 | } | ||
| 257 | |||
| 258 | 55438 | static int parse_family(const char *name, uint32_t *family) | |
| 259 | { | ||
| 260 | unsigned int i; | ||
| 261 | struct { | ||
| 262 | const char *name; | ||
| 263 | int val; | ||
| 264 | 55438 | } family_tbl[] = { | |
| 265 | { "ip", NFPROTO_IPV4 }, | ||
| 266 | { "ip6", NFPROTO_IPV6 }, | ||
| 267 | { "inet", NFPROTO_INET }, | ||
| 268 | { "arp", NFPROTO_ARP }, | ||
| 269 | { "bridge", NFPROTO_BRIDGE }, | ||
| 270 | { "netdev", NFPROTO_NETDEV } | ||
| 271 | }; | ||
| 272 | |||
| 273 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 55438 times.
|
55438 | assert(family); |
| 274 | |||
| 275 |
1/2✓ Branch 0 (9→5) taken 83278 times.
✗ Branch 1 (9→10) not taken.
|
83278 | for (i = 0; i < array_size(family_tbl); i++) { |
| 276 |
2/2✓ Branch 0 (5→6) taken 27840 times.
✓ Branch 1 (5→7) taken 55438 times.
|
83278 | if (strcmp(name, family_tbl[i].name)) |
| 277 | 27840 | continue; | |
| 278 | |||
| 279 | 55438 | *family = family_tbl[i].val; | |
| 280 | 55438 | return 0; | |
| 281 | } | ||
| 282 | ✗ | return -1; | |
| 283 | } | ||
| 284 | |||
| 285 | 358 | static int json_parse_family(struct json_ctx *ctx, json_t *root) | |
| 286 | { | ||
| 287 | const char *family; | ||
| 288 | |||
| 289 |
2/2✓ Branch 0 (3→4) taken 133 times.
✓ Branch 1 (3→12) taken 225 times.
|
358 | if (!json_unpack(root, "{s:s}", "family", &family)) { |
| 290 | uint32_t familyval; | ||
| 291 | |||
| 292 |
1/2✓ Branch 0 (5→6) taken 133 times.
✗ Branch 1 (5→8) not taken.
|
133 | if (parse_family(family, &familyval) || |
| 293 |
2/2✓ Branch 0 (6→7) taken 95 times.
✓ Branch 1 (6→10) taken 38 times.
|
133 | (familyval != NFPROTO_IPV6 && |
| 294 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 95 times.
|
95 | familyval != NFPROTO_IPV4)) { |
| 295 | ✗ | json_error(ctx, "Invalid family '%s'.", family); | |
| 296 | ✗ | return -1; | |
| 297 | } | ||
| 298 | 133 | return familyval; | |
| 299 | } | ||
| 300 | |||
| 301 | 225 | return NFPROTO_UNSPEC; | |
| 302 | } | ||
| 303 | |||
| 304 | 456887 | static bool is_keyword(const char *keyword) | |
| 305 | { | ||
| 306 | 456887 | const char *keywords[] = { | |
| 307 | "ether", | ||
| 308 | "ip", | ||
| 309 | "ip6", | ||
| 310 | "vlan", | ||
| 311 | "arp", | ||
| 312 | "dnat", | ||
| 313 | "snat", | ||
| 314 | "ecn", | ||
| 315 | "reset", | ||
| 316 | "original", | ||
| 317 | "reply", | ||
| 318 | "label", | ||
| 319 | }; | ||
| 320 | unsigned int i; | ||
| 321 | |||
| 322 |
2/2✓ Branch 0 (6→3) taken 5480376 times.
✓ Branch 1 (6→7) taken 456580 times.
|
5936956 | for (i = 0; i < array_size(keywords); i++) { |
| 323 |
2/2✓ Branch 0 (3→4) taken 307 times.
✓ Branch 1 (3→5) taken 5480069 times.
|
5480376 | if (!strcmp(keyword, keywords[i])) |
| 324 | 307 | return true; | |
| 325 | } | ||
| 326 | 456580 | return false; | |
| 327 | } | ||
| 328 | |||
| 329 | 456580 | static bool is_constant(const char *keyword) | |
| 330 | { | ||
| 331 | 456580 | const char *constants[] = { | |
| 332 | "tcp", | ||
| 333 | "udp", | ||
| 334 | "udplite", | ||
| 335 | "esp", | ||
| 336 | "ah", | ||
| 337 | "icmp", | ||
| 338 | "icmpv6", | ||
| 339 | "comp", | ||
| 340 | "dccp", | ||
| 341 | "sctp", | ||
| 342 | "redirect", | ||
| 343 | }; | ||
| 344 | unsigned int i; | ||
| 345 | |||
| 346 |
2/2✓ Branch 0 (6→3) taken 5018352 times.
✓ Branch 1 (6→7) taken 455994 times.
|
5474346 | for (i = 0; i < array_size(constants); i++) { |
| 347 |
2/2✓ Branch 0 (3→4) taken 586 times.
✓ Branch 1 (3→5) taken 5017766 times.
|
5018352 | if (!strcmp(keyword, constants[i])) |
| 348 | 586 | return true; | |
| 349 | } | ||
| 350 | 455994 | return false; | |
| 351 | } | ||
| 352 | |||
| 353 | 586 | static struct expr *json_parse_constant(struct json_ctx *ctx, const char *name) | |
| 354 | { | ||
| 355 | const struct { | ||
| 356 | const char *name; | ||
| 357 | uint8_t data; | ||
| 358 | const struct datatype *dtype; | ||
| 359 | 586 | } constant_tbl[] = { | |
| 360 | { "tcp", IPPROTO_TCP, &inet_protocol_type }, | ||
| 361 | { "udp", IPPROTO_UDP, &inet_protocol_type }, | ||
| 362 | { "udplite", IPPROTO_UDPLITE, &inet_protocol_type }, | ||
| 363 | { "esp", IPPROTO_ESP, &inet_protocol_type }, | ||
| 364 | { "ah", IPPROTO_AH, &inet_protocol_type }, | ||
| 365 | { "icmp", IPPROTO_ICMP, &inet_protocol_type }, | ||
| 366 | { "icmpv6", IPPROTO_ICMPV6, &inet_protocol_type }, | ||
| 367 | { "comp", IPPROTO_COMP, &inet_protocol_type }, | ||
| 368 | { "dccp", IPPROTO_DCCP, &inet_protocol_type }, | ||
| 369 | { "sctp", IPPROTO_SCTP, &inet_protocol_type }, | ||
| 370 | { "redirect", ICMP_REDIRECT, &icmp_type_type }, | ||
| 371 | }; | ||
| 372 | unsigned int i; | ||
| 373 | |||
| 374 |
1/2✓ Branch 0 (7→3) taken 2418 times.
✗ Branch 1 (7→8) not taken.
|
2418 | for (i = 0; i < array_size(constant_tbl); i++) { |
| 375 |
2/2✓ Branch 0 (3→4) taken 1832 times.
✓ Branch 1 (3→5) taken 586 times.
|
2418 | if (strcmp(name, constant_tbl[i].name)) |
| 376 | 1832 | continue; | |
| 377 | 586 | return constant_expr_alloc(int_loc, | |
| 378 | 586 | constant_tbl[i].dtype, | |
| 379 | BYTEORDER_HOST_ENDIAN, | ||
| 380 | BITS_PER_BYTE, | ||
| 381 | 586 | &constant_tbl[i].data); | |
| 382 | } | ||
| 383 | ✗ | json_error(ctx, "Unknown constant '%s'.", name); | |
| 384 | ✗ | return NULL; | |
| 385 | } | ||
| 386 | |||
| 387 | /* this is a combination of symbol_expr, integer_expr, boolean_expr ... */ | ||
| 388 | 529846 | static struct expr *json_parse_immediate(struct json_ctx *ctx, json_t *root) | |
| 389 | { | ||
| 390 | 529846 | enum symbol_types symtype = SYMBOL_VALUE; | |
| 391 | const char *str; | ||
| 392 | 529846 | char buf[64] = {}; | |
| 393 | |||
| 394 |
3/4✓ Branch 0 (2→3) taken 457275 times.
✓ Branch 1 (2→19) taken 72394 times.
✓ Branch 2 (2→21) taken 177 times.
✗ Branch 3 (2→26) not taken.
|
529846 | switch (json_typeof(root)) { |
| 395 | 457275 | case JSON_STRING: | |
| 396 | 457275 | str = json_string_value(root); | |
| 397 |
2/2✓ Branch 0 (4→5) taken 357 times.
✓ Branch 1 (4→6) taken 456918 times.
|
457275 | if (str[0] == '@') { |
| 398 | 357 | symtype = SYMBOL_SET; | |
| 399 | 357 | str++; | |
| 400 |
3/4✓ Branch 0 (6→7) taken 31 times.
✓ Branch 1 (6→10) taken 456887 times.
✓ Branch 2 (7→8) taken 31 times.
✗ Branch 3 (7→10) not taken.
|
456918 | } else if (str[0] == '*' && str[1] == '\0') { |
| 401 | 31 | return set_elem_catchall_expr_alloc(int_loc); | |
| 402 |
2/2✓ Branch 0 (11→12) taken 307 times.
✓ Branch 1 (11→14) taken 456580 times.
|
456887 | } else if (is_keyword(str)) { |
| 403 | 307 | return symbol_expr_alloc(int_loc, | |
| 404 | SYMBOL_VALUE, NULL, str); | ||
| 405 |
2/2✓ Branch 0 (15→16) taken 586 times.
✓ Branch 1 (15→18) taken 455994 times.
|
456580 | } else if (is_constant(str)) { |
| 406 | 586 | return json_parse_constant(ctx, str); | |
| 407 | } | ||
| 408 | 456351 | break; | |
| 409 | 72394 | case JSON_INTEGER: | |
| 410 | 72394 | snprintf(buf, sizeof(buf), | |
| 411 | "%" JSON_INTEGER_FORMAT, json_integer_value(root)); | ||
| 412 | 72394 | str = buf; | |
| 413 | 72394 | break; | |
| 414 | 177 | case JSON_TRUE: | |
| 415 | case JSON_FALSE: | ||
| 416 |
3/4✓ Branch 0 (21→22) taken 177 times.
✗ Branch 1 (21→24) not taken.
✓ Branch 2 (22→23) taken 150 times.
✓ Branch 3 (22→24) taken 27 times.
|
177 | buf[0] = json_is_true(root); |
| 417 | 177 | return constant_expr_alloc(int_loc, &boolean_type, | |
| 418 | BYTEORDER_HOST_ENDIAN, | ||
| 419 | BITS_PER_BYTE, buf); | ||
| 420 | ✗ | default: | |
| 421 | ✗ | json_error(ctx, "Unexpected JSON type %s for immediate value.", | |
| 422 | json_typename(root)); | ||
| 423 | ✗ | return NULL; | |
| 424 | } | ||
| 425 | |||
| 426 | 528745 | return symbol_expr_alloc(int_loc, symtype, NULL, str); | |
| 427 | } | ||
| 428 | |||
| 429 | 42098 | static struct expr *json_parse_meta_expr(struct json_ctx *ctx, | |
| 430 | const char *type, json_t *root) | ||
| 431 | { | ||
| 432 | struct error_record *erec; | ||
| 433 | unsigned int key; | ||
| 434 | const char *name; | ||
| 435 | |||
| 436 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 42098 times.
|
42098 | if (json_unpack_err(ctx, root, "{s:s}", "key", &name)) |
| 437 | ✗ | return NULL; | |
| 438 | 42098 | erec = meta_key_parse(int_loc, name, &key); | |
| 439 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 42098 times.
|
42098 | if (erec) { |
| 440 | ✗ | erec_queue(erec, ctx->msgs); | |
| 441 | ✗ | return NULL; | |
| 442 | } | ||
| 443 | 42098 | return meta_expr_alloc(int_loc, key); | |
| 444 | } | ||
| 445 | |||
| 446 | 3 | static struct expr *json_parse_tunnel_expr(struct json_ctx *ctx, | |
| 447 | const char *type, json_t *root) | ||
| 448 | { | ||
| 449 | struct error_record *erec; | ||
| 450 | unsigned int key; | ||
| 451 | const char *name; | ||
| 452 | |||
| 453 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 3 times.
|
3 | if (json_unpack_err(ctx, root, "{s:s}", "key", &name)) |
| 454 | ✗ | return NULL; | |
| 455 | 3 | erec = tunnel_key_parse(int_loc, name, &key); | |
| 456 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 3 times.
|
3 | if (erec) { |
| 457 | ✗ | erec_queue(erec, ctx->msgs); | |
| 458 | ✗ | return NULL; | |
| 459 | } | ||
| 460 | 3 | return tunnel_expr_alloc(int_loc, key); | |
| 461 | } | ||
| 462 | |||
| 463 | 32 | static struct expr *json_parse_osf_expr(struct json_ctx *ctx, | |
| 464 | const char *type, json_t *root) | ||
| 465 | { | ||
| 466 | const char *key, *ttl; | ||
| 467 | 32 | uint32_t flagval = 0; | |
| 468 | 32 | uint8_t ttlval = 0; | |
| 469 | |||
| 470 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 32 times.
|
32 | if (json_unpack_err(ctx, root, "{s:s}", "key", &key)) |
| 471 | ✗ | return NULL; | |
| 472 | |||
| 473 |
2/2✓ Branch 0 (6→7) taken 9 times.
✓ Branch 1 (6→13) taken 23 times.
|
32 | if (!json_unpack(root, "{s:s}", "ttl", &ttl)) { |
| 474 |
2/2✓ Branch 0 (7→8) taken 3 times.
✓ Branch 1 (7→9) taken 6 times.
|
9 | if (!strcmp(ttl, "loose")) { |
| 475 | 3 | ttlval = 1; | |
| 476 |
1/2✓ Branch 0 (9→10) taken 6 times.
✗ Branch 1 (9→11) not taken.
|
6 | } else if (!strcmp(ttl, "skip")) { |
| 477 | 6 | ttlval = 2; | |
| 478 | } else { | ||
| 479 | ✗ | json_error(ctx, "Invalid osf ttl option '%s'.", ttl); | |
| 480 | ✗ | return NULL; | |
| 481 | } | ||
| 482 | } | ||
| 483 | |||
| 484 |
2/2✓ Branch 0 (13→14) taken 23 times.
✓ Branch 1 (13→16) taken 9 times.
|
32 | if (!strcmp(key, "name")) { |
| 485 | 23 | return osf_expr_alloc(int_loc, ttlval, flagval); | |
| 486 |
1/2✓ Branch 0 (16→17) taken 9 times.
✗ Branch 1 (16→19) not taken.
|
9 | } else if (!strcmp(key, "version")) { |
| 487 | 9 | flagval |= NFT_OSF_F_VERSION; | |
| 488 | 9 | return osf_expr_alloc(int_loc, ttlval, flagval); | |
| 489 | } | ||
| 490 | |||
| 491 | ✗ | json_error(ctx, "Invalid osf key value."); | |
| 492 | ✗ | return NULL; | |
| 493 | } | ||
| 494 | |||
| 495 | 15 | static struct expr *json_parse_socket_expr(struct json_ctx *ctx, | |
| 496 | const char *type, json_t *root) | ||
| 497 | { | ||
| 498 | const char *key; | ||
| 499 | 15 | int keyval = -1; | |
| 500 | |||
| 501 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 15 times.
|
15 | if (json_unpack_err(ctx, root, "{s:s}", "key", &key)) |
| 502 | ✗ | return NULL; | |
| 503 | |||
| 504 |
2/2✓ Branch 0 (5→6) taken 6 times.
✓ Branch 1 (5→7) taken 9 times.
|
15 | if (!strcmp(key, "transparent")) |
| 505 | 6 | keyval = NFT_SOCKET_TRANSPARENT; | |
| 506 |
2/2✓ Branch 0 (7→8) taken 3 times.
✓ Branch 1 (7→9) taken 6 times.
|
9 | else if (!strcmp(key, "mark")) |
| 507 | 3 | keyval = NFT_SOCKET_MARK; | |
| 508 |
1/2✓ Branch 0 (9→10) taken 6 times.
✗ Branch 1 (9→11) not taken.
|
6 | else if (!strcmp(key, "wildcard")) |
| 509 | 6 | keyval = NFT_SOCKET_WILDCARD; | |
| 510 | |||
| 511 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→14) taken 15 times.
|
15 | if (keyval == -1) { |
| 512 | ✗ | json_error(ctx, "Invalid socket key value."); | |
| 513 | ✗ | return NULL; | |
| 514 | } | ||
| 515 | |||
| 516 | 15 | return socket_expr_alloc(int_loc, keyval, 0); | |
| 517 | } | ||
| 518 | |||
| 519 | 4650 | static int json_parse_payload_field(const struct proto_desc *desc, | |
| 520 | const char *name, int *field) | ||
| 521 | { | ||
| 522 | unsigned int i; | ||
| 523 | |||
| 524 |
1/2✓ Branch 0 (9→3) taken 30100 times.
✗ Branch 1 (9→10) not taken.
|
30100 | for (i = 0; i < PROTO_HDRS_MAX; i++) { |
| 525 |
2/2✓ Branch 0 (3→4) taken 27455 times.
✓ Branch 1 (3→8) taken 2645 times.
|
30100 | if (desc->templates[i].token && |
| 526 |
2/2✓ Branch 0 (4→5) taken 4650 times.
✓ Branch 1 (4→8) taken 22805 times.
|
27455 | !strcmp(desc->templates[i].token, name)) { |
| 527 |
1/2✓ Branch 0 (5→6) taken 4650 times.
✗ Branch 1 (5→7) not taken.
|
4650 | if (field) |
| 528 | 4650 | *field = i; | |
| 529 | 4650 | return 0; | |
| 530 | } | ||
| 531 | } | ||
| 532 | ✗ | return 1; | |
| 533 | } | ||
| 534 | |||
| 535 | 138 | static int json_parse_tcp_option_type(const char *name, int *val) | |
| 536 | { | ||
| 537 | unsigned int i; | ||
| 538 | |||
| 539 |
2/2✓ Branch 0 (9→3) taken 2312 times.
✓ Branch 1 (9→10) taken 24 times.
|
2336 | for (i = 0; i < array_size(tcpopt_protocols); i++) { |
| 540 |
2/2✓ Branch 0 (3→4) taken 905 times.
✓ Branch 1 (3→8) taken 1407 times.
|
2312 | if (tcpopt_protocols[i] && |
| 541 |
2/2✓ Branch 0 (4→5) taken 114 times.
✓ Branch 1 (4→8) taken 791 times.
|
905 | !strcmp(tcpopt_protocols[i]->name, name)) { |
| 542 |
1/2✓ Branch 0 (5→6) taken 114 times.
✗ Branch 1 (5→7) not taken.
|
114 | if (val) |
| 543 | 114 | *val = i; | |
| 544 | 114 | return 0; | |
| 545 | } | ||
| 546 | } | ||
| 547 | /* special case for sack0 - sack3 */ | ||
| 548 |
2/4✓ Branch 0 (10→11) taken 24 times.
✗ Branch 1 (10→19) not taken.
✓ Branch 2 (11→12) taken 24 times.
✗ Branch 3 (11→19) not taken.
|
24 | if (sscanf(name, "sack%u", &i) == 1 && i < 4) { |
| 549 |
3/4✓ Branch 0 (12→13) taken 24 times.
✗ Branch 1 (12→15) not taken.
✓ Branch 2 (13→14) taken 6 times.
✓ Branch 3 (13→15) taken 18 times.
|
24 | if (val && i == 0) |
| 550 | 6 | *val = TCPOPT_KIND_SACK; | |
| 551 |
2/4✓ Branch 0 (15→16) taken 18 times.
✗ Branch 1 (15→18) not taken.
✓ Branch 2 (16→17) taken 18 times.
✗ Branch 3 (16→18) not taken.
|
18 | else if (val && i > 0) |
| 552 | 18 | *val = TCPOPT_KIND_SACK1 + i - 1; | |
| 553 | 24 | return 0; | |
| 554 | } | ||
| 555 | ✗ | return 1; | |
| 556 | } | ||
| 557 | |||
| 558 | 89 | static int json_parse_tcp_option_field(int type, const char *name, int *val) | |
| 559 | { | ||
| 560 | const struct exthdr_desc *desc; | ||
| 561 | 89 | unsigned int block = 0; | |
| 562 | unsigned int i; | ||
| 563 | |||
| 564 |
4/4✓ Branch 0 (2→3) taken 6 times.
✓ Branch 1 (2→4) taken 6 times.
✓ Branch 2 (2→5) taken 6 times.
✓ Branch 3 (2→6) taken 71 times.
|
89 | switch (type) { |
| 565 | 6 | case TCPOPT_KIND_SACK1: | |
| 566 | 6 | type = TCPOPT_KIND_SACK; | |
| 567 | 6 | block = 1; | |
| 568 | 6 | break; | |
| 569 | 6 | case TCPOPT_KIND_SACK2: | |
| 570 | 6 | type = TCPOPT_KIND_SACK; | |
| 571 | 6 | block = 2; | |
| 572 | 6 | break; | |
| 573 | 6 | case TCPOPT_KIND_SACK3: | |
| 574 | 6 | type = TCPOPT_KIND_SACK; | |
| 575 | 6 | block = 3; | |
| 576 | 6 | break; | |
| 577 | } | ||
| 578 | |||
| 579 |
2/4✓ Branch 0 (6→7) taken 89 times.
✗ Branch 1 (6→8) not taken.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→9) taken 89 times.
|
89 | if (type < 0 || type >= (int)array_size(tcpopt_protocols)) |
| 580 | ✗ | return 1; | |
| 581 | |||
| 582 | 89 | desc = tcpopt_protocols[type]; | |
| 583 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 89 times.
|
89 | if (!desc) |
| 584 | ✗ | return 1; | |
| 585 | |||
| 586 |
1/2✓ Branch 0 (20→12) taken 340 times.
✗ Branch 1 (20→21) not taken.
|
340 | for (i = 0; i < array_size(desc->templates); i++) { |
| 587 |
1/2✓ Branch 0 (12→13) taken 340 times.
✗ Branch 1 (12→19) not taken.
|
340 | if (desc->templates[i].token && |
| 588 |
2/2✓ Branch 0 (13→14) taken 125 times.
✓ Branch 1 (13→19) taken 215 times.
|
340 | !strcmp(desc->templates[i].token, name)) { |
| 589 |
2/2✓ Branch 0 (14→15) taken 36 times.
✓ Branch 1 (14→16) taken 89 times.
|
125 | if (block) { |
| 590 | 36 | block--; | |
| 591 | 36 | continue; | |
| 592 | } | ||
| 593 | |||
| 594 |
1/2✓ Branch 0 (16→17) taken 89 times.
✗ Branch 1 (16→18) not taken.
|
89 | if (val) |
| 595 | 89 | *val = i; | |
| 596 | 89 | return 0; | |
| 597 | } | ||
| 598 | } | ||
| 599 | ✗ | return 1; | |
| 600 | } | ||
| 601 | |||
| 602 | 4650 | static const struct proto_desc *proto_lookup_byname(const char *name) | |
| 603 | { | ||
| 604 | 4650 | const struct proto_desc *proto_tbl[] = { | |
| 605 | &proto_eth, | ||
| 606 | &proto_vlan, | ||
| 607 | &proto_arp, | ||
| 608 | &proto_ip, | ||
| 609 | &proto_icmp, | ||
| 610 | &proto_igmp, | ||
| 611 | &proto_ip6, | ||
| 612 | &proto_icmp6, | ||
| 613 | &proto_ah, | ||
| 614 | &proto_esp, | ||
| 615 | &proto_comp, | ||
| 616 | &proto_udp, | ||
| 617 | &proto_udplite, | ||
| 618 | &proto_tcp, | ||
| 619 | &proto_dccp, | ||
| 620 | &proto_sctp, | ||
| 621 | &proto_th, | ||
| 622 | &proto_vxlan, | ||
| 623 | &proto_gre, | ||
| 624 | &proto_gretap, | ||
| 625 | &proto_geneve, | ||
| 626 | }; | ||
| 627 | unsigned int i; | ||
| 628 | |||
| 629 |
1/2✓ Branch 0 (6→3) taken 37173 times.
✗ Branch 1 (6→7) not taken.
|
37173 | for (i = 0; i < array_size(proto_tbl); i++) { |
| 630 |
2/2✓ Branch 0 (3→4) taken 4650 times.
✓ Branch 1 (3→5) taken 32523 times.
|
37173 | if (!strcmp(proto_tbl[i]->name, name)) |
| 631 | 4650 | return proto_tbl[i]; | |
| 632 | } | ||
| 633 | ✗ | return NULL; | |
| 634 | } | ||
| 635 | |||
| 636 | 216 | static const struct proto_desc *inner_proto_lookup_byname(const char *name) | |
| 637 | { | ||
| 638 | 216 | const struct proto_desc *proto_tbl[] = { | |
| 639 | &proto_geneve, | ||
| 640 | &proto_gre, | ||
| 641 | &proto_gretap, | ||
| 642 | &proto_vxlan, | ||
| 643 | }; | ||
| 644 | unsigned int i; | ||
| 645 | |||
| 646 |
1/2✓ Branch 0 (6→3) taken 569 times.
✗ Branch 1 (6→7) not taken.
|
569 | for (i = 0; i < array_size(proto_tbl); i++) { |
| 647 |
2/2✓ Branch 0 (3→4) taken 216 times.
✓ Branch 1 (3→5) taken 353 times.
|
569 | if (!strcmp(proto_tbl[i]->name, name)) |
| 648 | 216 | return proto_tbl[i]; | |
| 649 | } | ||
| 650 | ✗ | return NULL; | |
| 651 | } | ||
| 652 | |||
| 653 | 4753 | static struct expr *json_parse_payload_expr(struct json_ctx *ctx, | |
| 654 | const char *type, json_t *root) | ||
| 655 | { | ||
| 656 | const char *tunnel, *protocol, *field, *base; | ||
| 657 | int offset, len, val; | ||
| 658 | struct expr *expr; | ||
| 659 | |||
| 660 |
2/2✓ Branch 0 (3→4) taken 103 times.
✓ Branch 1 (3→21) taken 4650 times.
|
4753 | if (!json_unpack(root, "{s:s, s:i, s:i}", |
| 661 | "base", &base, "offset", &offset, "len", &len)) { | ||
| 662 |
2/2✓ Branch 0 (4→5) taken 9 times.
✓ Branch 1 (4→6) taken 94 times.
|
103 | if (!strcmp(base, "ll")) { |
| 663 | 9 | val = PROTO_BASE_LL_HDR; | |
| 664 |
2/2✓ Branch 0 (6→7) taken 24 times.
✓ Branch 1 (6→8) taken 70 times.
|
94 | } else if (!strcmp(base, "nh")) { |
| 665 | 24 | val = PROTO_BASE_NETWORK_HDR; | |
| 666 |
2/2✓ Branch 0 (8→9) taken 23 times.
✓ Branch 1 (8→10) taken 47 times.
|
70 | } else if (!strcmp(base, "th")) { |
| 667 | 23 | val = PROTO_BASE_TRANSPORT_HDR; | |
| 668 |
1/2✓ Branch 0 (10→11) taken 47 times.
✗ Branch 1 (10→12) not taken.
|
47 | } else if (!strcmp(base, "ih")) { |
| 669 | 47 | val = PROTO_BASE_INNER_HDR; | |
| 670 | } else { | ||
| 671 | ✗ | json_error(ctx, "Invalid payload base '%s'.", base); | |
| 672 | ✗ | return NULL; | |
| 673 | } | ||
| 674 | |||
| 675 |
2/4✓ Branch 0 (14→15) taken 103 times.
✗ Branch 1 (14→16) not taken.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→18) taken 103 times.
|
103 | if (len <= 0 || len > (int)NFT_MAX_EXPR_LEN_BITS) { |
| 676 | ✗ | json_error(ctx, "Payload length must be between 0 and %lu, got %d", | |
| 677 | NFT_MAX_EXPR_LEN_BITS, len); | ||
| 678 | ✗ | return NULL; | |
| 679 | } | ||
| 680 | |||
| 681 | 103 | expr = payload_expr_alloc(int_loc, NULL, 0); | |
| 682 | 103 | payload_init_raw(expr, val, offset, len); | |
| 683 | 103 | expr->byteorder = BYTEORDER_BIG_ENDIAN; | |
| 684 | 103 | expr->payload.is_raw = true; | |
| 685 | 103 | return expr; | |
| 686 |
2/2✓ Branch 0 (22→23) taken 216 times.
✓ Branch 1 (22→39) taken 4434 times.
|
4650 | } else if (!json_unpack(root, "{s:s, s:s, s:s}", |
| 687 | "tunnel", &tunnel, "protocol", &protocol, "field", &field)) { | ||
| 688 | 216 | const struct proto_desc *proto = proto_lookup_byname(protocol); | |
| 689 | 216 | const struct proto_desc *inner_proto = inner_proto_lookup_byname(tunnel); | |
| 690 | |||
| 691 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→28) taken 216 times.
|
216 | if (!inner_proto) { |
| 692 | ✗ | json_error(ctx, "Unknown payload tunnel protocol '%s'.", | |
| 693 | tunnel); | ||
| 694 | ✗ | return NULL; | |
| 695 | } | ||
| 696 |
1/2✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→31) taken 216 times.
|
216 | if (!proto) { |
| 697 | ✗ | json_error(ctx, "Unknown payload protocol '%s'.", | |
| 698 | protocol); | ||
| 699 | ✗ | return NULL; | |
| 700 | } | ||
| 701 |
1/2✗ Branch 0 (32→33) not taken.
✓ Branch 1 (32→35) taken 216 times.
|
216 | if (json_parse_payload_field(proto, field, &val)) { |
| 702 | ✗ | json_error(ctx, "Unknown %s field '%s'.", | |
| 703 | protocol, field); | ||
| 704 | ✗ | return NULL; | |
| 705 | } | ||
| 706 | 216 | expr = payload_expr_alloc(int_loc, proto, val); | |
| 707 | 216 | expr->payload.inner_desc = inner_proto; | |
| 708 | |||
| 709 |
1/2✗ Branch 0 (36→37) not taken.
✓ Branch 1 (36→38) taken 216 times.
|
216 | if (proto == &proto_th) |
| 710 | ✗ | expr->payload.is_raw = true; | |
| 711 | |||
| 712 | 216 | return expr; | |
| 713 |
1/2✓ Branch 0 (40→41) taken 4434 times.
✗ Branch 1 (40→53) not taken.
|
4434 | } else if (!json_unpack(root, "{s:s, s:s}", |
| 714 | "protocol", &protocol, "field", &field)) { | ||
| 715 | 4434 | const struct proto_desc *proto = proto_lookup_byname(protocol); | |
| 716 | |||
| 717 |
1/2✗ Branch 0 (42→43) not taken.
✓ Branch 1 (42→45) taken 4434 times.
|
4434 | if (!proto) { |
| 718 | ✗ | json_error(ctx, "Unknown payload protocol '%s'.", | |
| 719 | protocol); | ||
| 720 | ✗ | return NULL; | |
| 721 | } | ||
| 722 |
1/2✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→49) taken 4434 times.
|
4434 | if (json_parse_payload_field(proto, field, &val)) { |
| 723 | ✗ | json_error(ctx, "Unknown %s field '%s'.", | |
| 724 | protocol, field); | ||
| 725 | ✗ | return NULL; | |
| 726 | } | ||
| 727 | 4434 | expr = payload_expr_alloc(int_loc, proto, val); | |
| 728 | |||
| 729 |
2/2✓ Branch 0 (50→51) taken 39 times.
✓ Branch 1 (50→52) taken 4395 times.
|
4434 | if (proto == &proto_th) |
| 730 | 39 | expr->payload.is_raw = true; | |
| 731 | |||
| 732 | 4434 | return expr; | |
| 733 | } | ||
| 734 | ✗ | json_error(ctx, "Invalid payload expression properties."); | |
| 735 | ✗ | return NULL; | |
| 736 | } | ||
| 737 | |||
| 738 | 152 | static struct expr *json_parse_tcp_option_expr(struct json_ctx *ctx, | |
| 739 | const char *type, json_t *root) | ||
| 740 | { | ||
| 741 | int fieldval, kind, offset, len; | ||
| 742 | const char *desc, *field; | ||
| 743 | struct expr *expr; | ||
| 744 | |||
| 745 |
2/2✓ Branch 0 (3→4) taken 14 times.
✓ Branch 1 (3→17) taken 138 times.
|
152 | if (!json_unpack(root, "{s:i, s:i, s:i}", |
| 746 | "base", &kind, "offset", &offset, "len", &len)) { | ||
| 747 | 14 | uint32_t flag = 0; | |
| 748 | |||
| 749 |
2/4✓ Branch 0 (4→5) taken 14 times.
✗ Branch 1 (4→6) not taken.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→7) taken 14 times.
|
14 | if (kind < 0 || kind > 255) |
| 750 | ✗ | return NULL; | |
| 751 | |||
| 752 |
2/4✓ Branch 0 (7→8) taken 14 times.
✗ Branch 1 (7→9) not taken.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→11) taken 14 times.
|
14 | if (len < 0 || len > (int)NFT_MAX_EXPR_LEN_BITS) { |
| 753 | ✗ | json_error(ctx, "option length must be between 0 and %lu, got %d", | |
| 754 | NFT_MAX_EXPR_LEN_BITS, len); | ||
| 755 | ✗ | return NULL; | |
| 756 | } | ||
| 757 | |||
| 758 | 14 | expr = tcpopt_expr_alloc(int_loc, kind, | |
| 759 | TCPOPT_COMMON_KIND); | ||
| 760 | |||
| 761 |
4/4✓ Branch 0 (12→13) taken 9 times.
✓ Branch 1 (12→15) taken 5 times.
✓ Branch 2 (13→14) taken 6 times.
✓ Branch 3 (13→15) taken 3 times.
|
14 | if (offset == TCPOPT_COMMON_KIND && len == 8) |
| 762 | 6 | flag = NFT_EXTHDR_F_PRESENT; | |
| 763 | |||
| 764 | 14 | tcpopt_init_raw(expr, kind, offset, len, flag); | |
| 765 | 14 | return expr; | |
| 766 |
1/2✓ Branch 0 (18→19) taken 138 times.
✗ Branch 1 (18→33) not taken.
|
138 | } else if (!json_unpack(root, "{s:s}", "name", &desc)) { |
| 767 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→23) taken 138 times.
|
138 | if (json_parse_tcp_option_type(desc, &kind)) { |
| 768 | ✗ | json_error(ctx, "Unknown tcp option name '%s'.", desc); | |
| 769 | ✗ | return NULL; | |
| 770 | } | ||
| 771 | |||
| 772 |
2/2✓ Branch 0 (24→25) taken 49 times.
✓ Branch 1 (24→27) taken 89 times.
|
138 | if (json_unpack(root, "{s:s}", "field", &field)) { |
| 773 | 49 | expr = tcpopt_expr_alloc(int_loc, kind, | |
| 774 | TCPOPT_COMMON_KIND); | ||
| 775 | 49 | expr->exthdr.flags = NFT_EXTHDR_F_PRESENT; | |
| 776 | 49 | return expr; | |
| 777 | } | ||
| 778 | |||
| 779 |
1/2✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→31) taken 89 times.
|
89 | if (json_parse_tcp_option_field(kind, field, &fieldval)) { |
| 780 | ✗ | json_error(ctx, "Unknown tcp option field '%s'.", field); | |
| 781 | ✗ | return NULL; | |
| 782 | } | ||
| 783 | |||
| 784 | 89 | return tcpopt_expr_alloc(int_loc, kind, fieldval); | |
| 785 | } | ||
| 786 | |||
| 787 | ✗ | json_error(ctx, "Invalid tcp option expression properties."); | |
| 788 | ✗ | return NULL; | |
| 789 | } | ||
| 790 | |||
| 791 | 4 | static int json_parse_ip_option_type(const char *name, int *val) | |
| 792 | { | ||
| 793 | unsigned int i; | ||
| 794 | |||
| 795 |
1/2✓ Branch 0 (9→3) taken 596 times.
✗ Branch 1 (9→10) not taken.
|
596 | for (i = 0; i < array_size(ipopt_protocols); i++) { |
| 796 |
2/2✓ Branch 0 (3→4) taken 16 times.
✓ Branch 1 (3→8) taken 580 times.
|
596 | if (ipopt_protocols[i] && |
| 797 |
2/2✓ Branch 0 (4→5) taken 4 times.
✓ Branch 1 (4→8) taken 12 times.
|
16 | !strcmp(ipopt_protocols[i]->name, name)) { |
| 798 |
1/2✓ Branch 0 (5→6) taken 4 times.
✗ Branch 1 (5→7) not taken.
|
4 | if (val) |
| 799 | 4 | *val = i; | |
| 800 | 4 | return 0; | |
| 801 | } | ||
| 802 | } | ||
| 803 | ✗ | return 1; | |
| 804 | } | ||
| 805 | |||
| 806 | 4 | static int json_parse_ip_option_field(int type, const char *name, int *val) | |
| 807 | { | ||
| 808 | unsigned int i; | ||
| 809 | 4 | const struct exthdr_desc *desc = ipopt_protocols[type]; | |
| 810 | |||
| 811 |
1/2✓ Branch 0 (9→3) taken 16 times.
✗ Branch 1 (9→10) not taken.
|
16 | for (i = 0; i < array_size(desc->templates); i++) { |
| 812 |
2/2✓ Branch 0 (3→4) taken 12 times.
✓ Branch 1 (3→8) taken 4 times.
|
16 | if (desc->templates[i].token && |
| 813 |
2/2✓ Branch 0 (4→5) taken 4 times.
✓ Branch 1 (4→8) taken 8 times.
|
12 | !strcmp(desc->templates[i].token, name)) { |
| 814 |
1/2✓ Branch 0 (5→6) taken 4 times.
✗ Branch 1 (5→7) not taken.
|
4 | if (val) |
| 815 | 4 | *val = i; | |
| 816 | 4 | return 0; | |
| 817 | } | ||
| 818 | } | ||
| 819 | ✗ | return 1; | |
| 820 | } | ||
| 821 | |||
| 822 | 4 | static struct expr *json_parse_ip_option_expr(struct json_ctx *ctx, | |
| 823 | const char *type, json_t *root) | ||
| 824 | { | ||
| 825 | const char *desc, *field; | ||
| 826 | int descval, fieldval; | ||
| 827 | struct expr *expr; | ||
| 828 | |||
| 829 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 4 times.
|
4 | if (json_unpack_err(ctx, root, "{s:s}", "name", &desc)) |
| 830 | ✗ | return NULL; | |
| 831 | |||
| 832 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 4 times.
|
4 | if (json_parse_ip_option_type(desc, &descval)) { |
| 833 | ✗ | json_error(ctx, "Unknown ip option name '%s'.", desc); | |
| 834 | ✗ | return NULL; | |
| 835 | } | ||
| 836 | |||
| 837 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 4 times.
|
4 | if (json_unpack(root, "{s:s}", "field", &field)) { |
| 838 | ✗ | expr = ipopt_expr_alloc(int_loc, descval, | |
| 839 | IPOPT_FIELD_TYPE); | ||
| 840 | ✗ | expr->exthdr.flags = NFT_EXTHDR_F_PRESENT; | |
| 841 | |||
| 842 | ✗ | return expr; | |
| 843 | } | ||
| 844 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→17) taken 4 times.
|
4 | if (json_parse_ip_option_field(descval, field, &fieldval)) { |
| 845 | ✗ | json_error(ctx, "Unknown ip option field '%s'.", field); | |
| 846 | ✗ | return NULL; | |
| 847 | } | ||
| 848 | 4 | return ipopt_expr_alloc(int_loc, descval, fieldval); | |
| 849 | } | ||
| 850 | |||
| 851 | 69 | static int json_parse_sctp_chunk_field(const struct exthdr_desc *desc, | |
| 852 | const char *name, int *val) | ||
| 853 | { | ||
| 854 | unsigned int i; | ||
| 855 | |||
| 856 |
1/2✓ Branch 0 (9→3) taken 298 times.
✗ Branch 1 (9→10) not taken.
|
298 | for (i = 0; i < array_size(desc->templates); i++) { |
| 857 |
1/2✓ Branch 0 (3→4) taken 298 times.
✗ Branch 1 (3→8) not taken.
|
298 | if (desc->templates[i].token && |
| 858 |
2/2✓ Branch 0 (4→5) taken 69 times.
✓ Branch 1 (4→8) taken 229 times.
|
298 | !strcmp(desc->templates[i].token, name)) { |
| 859 |
1/2✓ Branch 0 (5→6) taken 69 times.
✗ Branch 1 (5→7) not taken.
|
69 | if (val) |
| 860 | 69 | *val = i; | |
| 861 | 69 | return 0; | |
| 862 | } | ||
| 863 | } | ||
| 864 | ✗ | return 1; | |
| 865 | } | ||
| 866 | |||
| 867 | 159 | static struct expr *json_parse_sctp_chunk_expr(struct json_ctx *ctx, | |
| 868 | const char *type, json_t *root) | ||
| 869 | { | ||
| 870 | const struct exthdr_desc *desc; | ||
| 871 | const char *name, *field; | ||
| 872 | struct expr *expr; | ||
| 873 | int fieldval; | ||
| 874 | |||
| 875 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 159 times.
|
159 | if (json_unpack_err(ctx, root, "{s:s}", "name", &name)) |
| 876 | ✗ | return NULL; | |
| 877 | |||
| 878 | 159 | desc = sctp_chunk_protocol_find(name); | |
| 879 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 159 times.
|
159 | if (!desc) { |
| 880 | ✗ | json_error(ctx, "Unknown sctp chunk name '%s'.", name); | |
| 881 | ✗ | return NULL; | |
| 882 | } | ||
| 883 | |||
| 884 |
2/2✓ Branch 0 (10→11) taken 90 times.
✓ Branch 1 (10→13) taken 69 times.
|
159 | if (json_unpack(root, "{s:s}", "field", &field)) { |
| 885 | 90 | expr = sctp_chunk_expr_alloc(int_loc, desc->type, | |
| 886 | SCTP_CHUNK_COMMON_TYPE); | ||
| 887 | 90 | expr->exthdr.flags = NFT_EXTHDR_F_PRESENT; | |
| 888 | |||
| 889 | 90 | return expr; | |
| 890 | } | ||
| 891 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→17) taken 69 times.
|
69 | if (json_parse_sctp_chunk_field(desc, field, &fieldval)) { |
| 892 | ✗ | json_error(ctx, "Unknown sctp chunk field '%s'.", field); | |
| 893 | ✗ | return NULL; | |
| 894 | } | ||
| 895 | 69 | return sctp_chunk_expr_alloc(int_loc, desc->type, fieldval); | |
| 896 | } | ||
| 897 | |||
| 898 | ✗ | static struct expr *json_parse_dccp_option_expr(struct json_ctx *ctx, | |
| 899 | const char *type, json_t *root) | ||
| 900 | { | ||
| 901 | int opt_type; | ||
| 902 | |||
| 903 | ✗ | if (json_unpack_err(ctx, root, "{s:i}", "type", &opt_type)) | |
| 904 | ✗ | return NULL; | |
| 905 | |||
| 906 | ✗ | if (opt_type < DCCPOPT_TYPE_MIN || opt_type > DCCPOPT_TYPE_MAX) { | |
| 907 | ✗ | json_error(ctx, "Unknown dccp option type '%d'.", opt_type); | |
| 908 | ✗ | return NULL; | |
| 909 | } | ||
| 910 | |||
| 911 | ✗ | return dccpopt_expr_alloc(int_loc, opt_type); | |
| 912 | } | ||
| 913 | |||
| 914 | 327 | static const struct exthdr_desc *exthdr_lookup_byname(const char *name) | |
| 915 | { | ||
| 916 | 327 | const struct exthdr_desc *exthdr_tbl[] = { | |
| 917 | &exthdr_hbh, | ||
| 918 | &exthdr_rt, | ||
| 919 | &exthdr_rt0, | ||
| 920 | &exthdr_rt2, | ||
| 921 | &exthdr_rt4, | ||
| 922 | &exthdr_frag, | ||
| 923 | &exthdr_dst, | ||
| 924 | &exthdr_mh, | ||
| 925 | }; | ||
| 926 | unsigned int i; | ||
| 927 | |||
| 928 |
1/2✓ Branch 0 (6→3) taken 1672 times.
✗ Branch 1 (6→7) not taken.
|
1672 | for (i = 0; i < array_size(exthdr_tbl); i++) { |
| 929 |
2/2✓ Branch 0 (3→4) taken 327 times.
✓ Branch 1 (3→5) taken 1345 times.
|
1672 | if (!strcmp(exthdr_tbl[i]->name, name)) |
| 930 | 327 | return exthdr_tbl[i]; | |
| 931 | } | ||
| 932 | ✗ | return NULL; | |
| 933 | } | ||
| 934 | |||
| 935 | 315 | static int json_parse_exthdr_field(const struct exthdr_desc *desc, | |
| 936 | const char *name, int *field) | ||
| 937 | { | ||
| 938 | unsigned int i; | ||
| 939 | |||
| 940 |
1/2✓ Branch 0 (9→3) taken 1112 times.
✗ Branch 1 (9→10) not taken.
|
1112 | for (i = 0; i < array_size(desc->templates); i++) { |
| 941 |
2/2✓ Branch 0 (3→4) taken 797 times.
✓ Branch 1 (3→8) taken 315 times.
|
1112 | if (desc->templates[i].token && |
| 942 |
2/2✓ Branch 0 (4→5) taken 315 times.
✓ Branch 1 (4→8) taken 482 times.
|
797 | !strcmp(desc->templates[i].token, name)) { |
| 943 |
1/2✓ Branch 0 (5→6) taken 315 times.
✗ Branch 1 (5→7) not taken.
|
315 | if (field) |
| 944 | 315 | *field = i; | |
| 945 | 315 | return 0; | |
| 946 | } | ||
| 947 | } | ||
| 948 | ✗ | return 1; | |
| 949 | } | ||
| 950 | |||
| 951 | 327 | static struct expr *json_parse_exthdr_expr(struct json_ctx *ctx, | |
| 952 | const char *type, json_t *root) | ||
| 953 | { | ||
| 954 | 327 | const char *name, *field = NULL; | |
| 955 | struct expr *expr; | ||
| 956 | 327 | int offset = 0, fieldval; | |
| 957 | const struct exthdr_desc *desc; | ||
| 958 | |||
| 959 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 327 times.
|
327 | if (json_unpack_err(ctx, root, "{s:s}", "name", &name)) |
| 960 | ✗ | return NULL; | |
| 961 | |||
| 962 | 327 | desc = exthdr_lookup_byname(name); | |
| 963 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 327 times.
|
327 | if (!desc) { |
| 964 | ✗ | json_error(ctx, "Invalid exthdr protocol '%s'.", name); | |
| 965 | ✗ | return NULL; | |
| 966 | } | ||
| 967 | |||
| 968 |
2/2✓ Branch 0 (10→11) taken 12 times.
✓ Branch 1 (10→13) taken 315 times.
|
327 | if (json_unpack(root, "{s:s}", "field", &field)) { |
| 969 | 12 | expr = exthdr_expr_alloc(int_loc, desc, 1); | |
| 970 | 12 | expr->exthdr.flags = NFT_EXTHDR_F_PRESENT; | |
| 971 | 12 | return expr; | |
| 972 | } | ||
| 973 | |||
| 974 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→17) taken 315 times.
|
315 | if (json_parse_exthdr_field(desc, field, &fieldval)) { |
| 975 | ✗ | json_error(ctx, "Unknown %s field %s.", desc->name, field); | |
| 976 | ✗ | return NULL; | |
| 977 | } | ||
| 978 | |||
| 979 | /* special treatment for rt0 */ | ||
| 980 |
1/4✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→21) taken 315 times.
✗ Branch 2 (19→20) not taken.
✗ Branch 3 (19→21) not taken.
|
315 | if (desc == &exthdr_rt0 && |
| 981 | ✗ | json_unpack_err(ctx, root, "{s:i}", "offset", &offset)) | |
| 982 | ✗ | return NULL; | |
| 983 | |||
| 984 | 315 | return exthdr_expr_alloc(int_loc, desc, fieldval + offset); | |
| 985 | } | ||
| 986 | |||
| 987 | 14 | static struct expr *json_parse_rt_expr(struct json_ctx *ctx, | |
| 988 | const char *type, json_t *root) | ||
| 989 | { | ||
| 990 | const struct { | ||
| 991 | const char *name; | ||
| 992 | int val; | ||
| 993 | 14 | } rt_key_tbl[] = { | |
| 994 | { "classid", NFT_RT_CLASSID }, | ||
| 995 | { "nexthop", NFT_RT_NEXTHOP4 }, | ||
| 996 | { "mtu", NFT_RT_TCPMSS }, | ||
| 997 | { "ipsec", NFT_RT_XFRM }, | ||
| 998 | }; | ||
| 999 | const char *key; | ||
| 1000 | unsigned int i; | ||
| 1001 | int familyval; | ||
| 1002 | |||
| 1003 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 14 times.
|
14 | if (json_unpack_err(ctx, root, "{s:s}", "key", &key)) |
| 1004 | ✗ | return NULL; | |
| 1005 | 14 | familyval = json_parse_family(ctx, root); | |
| 1006 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 14 times.
|
14 | if (familyval < 0) |
| 1007 | ✗ | return NULL; | |
| 1008 | |||
| 1009 |
1/2✓ Branch 0 (18→9) taken 38 times.
✗ Branch 1 (18→19) not taken.
|
38 | for (i = 0; i < array_size(rt_key_tbl); i++) { |
| 1010 | 38 | int val = rt_key_tbl[i].val; | |
| 1011 | 38 | bool invalid = true; | |
| 1012 | |||
| 1013 |
2/2✓ Branch 0 (9→10) taken 24 times.
✓ Branch 1 (9→11) taken 14 times.
|
38 | if (strcmp(key, rt_key_tbl[i].name)) |
| 1014 | 24 | continue; | |
| 1015 | |||
| 1016 |
2/2✓ Branch 0 (11→12) taken 3 times.
✓ Branch 1 (11→16) taken 11 times.
|
14 | if (familyval) { |
| 1017 |
3/4✓ Branch 0 (12→13) taken 2 times.
✓ Branch 1 (12→15) taken 1 times.
✓ Branch 2 (13→14) taken 2 times.
✗ Branch 3 (13→15) not taken.
|
3 | if (familyval == NFPROTO_IPV6 && |
| 1018 | val == NFT_RT_NEXTHOP4) | ||
| 1019 | 2 | val = NFT_RT_NEXTHOP6; | |
| 1020 | 3 | invalid = false; | |
| 1021 | } | ||
| 1022 | 14 | return rt_expr_alloc(int_loc, val, invalid); | |
| 1023 | } | ||
| 1024 | ✗ | json_error(ctx, "Unknown rt key '%s'.", key); | |
| 1025 | ✗ | return NULL; | |
| 1026 | } | ||
| 1027 | |||
| 1028 | 44 | static bool ct_key_is_dir(enum nft_ct_keys key) | |
| 1029 | { | ||
| 1030 | 44 | const enum nft_ct_keys ct_dir_keys[] = { | |
| 1031 | NFT_CT_L3PROTOCOL, | ||
| 1032 | NFT_CT_SRC, | ||
| 1033 | NFT_CT_DST, | ||
| 1034 | NFT_CT_PROTOCOL, | ||
| 1035 | NFT_CT_PROTO_SRC, | ||
| 1036 | NFT_CT_PROTO_DST, | ||
| 1037 | NFT_CT_PKTS, | ||
| 1038 | NFT_CT_BYTES, | ||
| 1039 | NFT_CT_AVGPKT, | ||
| 1040 | NFT_CT_ZONE, | ||
| 1041 | NFT_CT_SRC_IP, | ||
| 1042 | NFT_CT_DST_IP, | ||
| 1043 | NFT_CT_SRC_IP6, | ||
| 1044 | NFT_CT_DST_IP6, | ||
| 1045 | }; | ||
| 1046 | unsigned int i; | ||
| 1047 | |||
| 1048 |
1/2✓ Branch 0 (6→3) taken 396 times.
✗ Branch 1 (6→7) not taken.
|
396 | for (i = 0; i < array_size(ct_dir_keys); i++) { |
| 1049 |
2/2✓ Branch 0 (3→4) taken 44 times.
✓ Branch 1 (3→5) taken 352 times.
|
396 | if (key == ct_dir_keys[i]) |
| 1050 | 44 | return true; | |
| 1051 | } | ||
| 1052 | ✗ | return false; | |
| 1053 | } | ||
| 1054 | |||
| 1055 | 517 | static struct expr *json_parse_ct_expr(struct json_ctx *ctx, | |
| 1056 | const char *type, json_t *root) | ||
| 1057 | { | ||
| 1058 | 517 | int dirval = -1, keyval = -1; | |
| 1059 | const char *key, *dir; | ||
| 1060 | unsigned int i; | ||
| 1061 | |||
| 1062 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 517 times.
|
517 | if (json_unpack_err(ctx, root, "{s:s}", "key", &key)) |
| 1063 | ✗ | return NULL; | |
| 1064 | |||
| 1065 |
1/2✓ Branch 0 (10→6) taken 3028 times.
✗ Branch 1 (10→11) not taken.
|
3028 | for (i = 0; i < array_size(ct_templates); i++) { |
| 1066 |
1/2✓ Branch 0 (6→7) taken 3028 times.
✗ Branch 1 (6→9) not taken.
|
3028 | if (ct_templates[i].token && |
| 1067 |
2/2✓ Branch 0 (7→8) taken 517 times.
✓ Branch 1 (7→9) taken 2511 times.
|
3028 | !strcmp(key, ct_templates[i].token)) { |
| 1068 | 517 | keyval = i; | |
| 1069 | 517 | break; | |
| 1070 | } | ||
| 1071 | } | ||
| 1072 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→14) taken 517 times.
|
517 | if (keyval == -1) { |
| 1073 | ✗ | json_error(ctx, "Unknown ct key '%s'.", key); | |
| 1074 | ✗ | return NULL; | |
| 1075 | } | ||
| 1076 | |||
| 1077 |
2/2✓ Branch 0 (15→16) taken 44 times.
✓ Branch 1 (15→26) taken 473 times.
|
517 | if (!json_unpack(root, "{s:s}", "dir", &dir)) { |
| 1078 |
2/2✓ Branch 0 (16→17) taken 30 times.
✓ Branch 1 (16→18) taken 14 times.
|
44 | if (!strcmp(dir, "original")) { |
| 1079 | 30 | dirval = IP_CT_DIR_ORIGINAL; | |
| 1080 |
1/2✓ Branch 0 (18→19) taken 14 times.
✗ Branch 1 (18→20) not taken.
|
14 | } else if (!strcmp(dir, "reply")) { |
| 1081 | 14 | dirval = IP_CT_DIR_REPLY; | |
| 1082 | } else { | ||
| 1083 | ✗ | json_error(ctx, "Invalid direction '%s'.", dir); | |
| 1084 | ✗ | return NULL; | |
| 1085 | } | ||
| 1086 | |||
| 1087 |
1/2✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→26) taken 44 times.
|
44 | if (!ct_key_is_dir(keyval)) { |
| 1088 | ✗ | json_error(ctx, "Direction not supported by CT key '%s'.", key); | |
| 1089 | ✗ | return NULL; | |
| 1090 | } | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | 517 | return ct_expr_alloc(int_loc, keyval, dirval); | |
| 1094 | } | ||
| 1095 | |||
| 1096 | 44 | static struct expr *json_parse_numgen_expr(struct json_ctx *ctx, | |
| 1097 | const char *type, json_t *root) | ||
| 1098 | { | ||
| 1099 | 44 | int modeval, mod, offset = 0; | |
| 1100 | const char *mode; | ||
| 1101 | |||
| 1102 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 44 times.
|
44 | if (json_unpack_err(ctx, root, "{s:s, s:i}", |
| 1103 | "mode", &mode, "mod", &mod)) | ||
| 1104 | ✗ | return NULL; | |
| 1105 | 44 | json_unpack(root, "{s:i}", "offset", &offset); | |
| 1106 | |||
| 1107 |
1/2✓ Branch 0 (6→7) taken 44 times.
✗ Branch 1 (6→8) not taken.
|
44 | if (!strcmp(mode, "inc")) { |
| 1108 | 44 | modeval = NFT_NG_INCREMENTAL; | |
| 1109 | ✗ | } else if (!strcmp(mode, "random")) { | |
| 1110 | ✗ | modeval = NFT_NG_RANDOM; | |
| 1111 | } else { | ||
| 1112 | ✗ | json_error(ctx, "Unknown numgen mode '%s'.", mode); | |
| 1113 | ✗ | return NULL; | |
| 1114 | } | ||
| 1115 | |||
| 1116 | 44 | return numgen_expr_alloc(int_loc, modeval, mod, offset); | |
| 1117 | } | ||
| 1118 | |||
| 1119 | 20 | static struct expr *json_parse_hash_expr(struct json_ctx *ctx, | |
| 1120 | const char *type, json_t *root) | ||
| 1121 | { | ||
| 1122 | 20 | int mod, offset = 0, seed = 0; | |
| 1123 | struct expr *expr, *hash_expr; | ||
| 1124 | bool have_seed; | ||
| 1125 | json_t *jexpr; | ||
| 1126 | |||
| 1127 | |||
| 1128 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 20 times.
|
20 | if (json_unpack_err(ctx, root, "{s:i}", "mod", &mod)) |
| 1129 | ✗ | return NULL; | |
| 1130 | 20 | json_unpack(root, "{s:i}", "offset", &offset); | |
| 1131 | |||
| 1132 |
2/2✓ Branch 0 (6→7) taken 8 times.
✓ Branch 1 (6→9) taken 12 times.
|
20 | if (!strcmp(type, "symhash")) { |
| 1133 | 8 | return hash_expr_alloc(int_loc, mod, false, 0, | |
| 1134 | offset, NFT_HASH_SYM); | ||
| 1135 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 12 times.
|
12 | } else if (strcmp(type, "jhash")) { |
| 1136 | ✗ | json_error(ctx, "Unknown hash type '%s'.", type); | |
| 1137 | ✗ | return NULL; | |
| 1138 | } | ||
| 1139 | |||
| 1140 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 12 times.
|
12 | if (json_unpack_err(ctx, root, "{s:o}", "expr", &jexpr)) |
| 1141 | ✗ | return NULL; | |
| 1142 | 12 | expr = json_parse_expr(ctx, jexpr); | |
| 1143 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→19) taken 12 times.
|
12 | if (!expr) { |
| 1144 | ✗ | json_error(ctx, "Invalid jhash expression."); | |
| 1145 | ✗ | return NULL; | |
| 1146 | } | ||
| 1147 | 12 | have_seed = !json_unpack(root, "{s:i}", "seed", &seed); | |
| 1148 | |||
| 1149 | 12 | hash_expr = hash_expr_alloc(int_loc, mod, have_seed, | |
| 1150 | seed, offset, NFT_HASH_JENKINS); | ||
| 1151 | 12 | hash_expr->hash.expr = expr; | |
| 1152 | 12 | return hash_expr; | |
| 1153 | } | ||
| 1154 | |||
| 1155 | 40 | static unsigned int fib_flag_parse(const char *name) | |
| 1156 | { | ||
| 1157 | 40 | const char *fib_flags[] = { | |
| 1158 | "saddr", | ||
| 1159 | "daddr", | ||
| 1160 | "mark", | ||
| 1161 | "iif", | ||
| 1162 | "oif", | ||
| 1163 | }; | ||
| 1164 | unsigned int i; | ||
| 1165 | |||
| 1166 |
1/2✓ Branch 0 (6→3) taken 94 times.
✗ Branch 1 (6→7) not taken.
|
94 | for (i = 0; i < array_size(fib_flags); i++) { |
| 1167 |
2/2✓ Branch 0 (3→4) taken 40 times.
✓ Branch 1 (3→5) taken 54 times.
|
94 | if (!strcmp(name, fib_flags[i])) |
| 1168 | 40 | return 1 << i; | |
| 1169 | } | ||
| 1170 | ✗ | return 0; | |
| 1171 | } | ||
| 1172 | |||
| 1173 | 26 | static struct expr *json_parse_fib_expr(struct json_ctx *ctx, | |
| 1174 | const char *type, json_t *root) | ||
| 1175 | { | ||
| 1176 | 26 | const char *fib_result_tbl[] = { | |
| 1177 | [NFT_FIB_RESULT_UNSPEC] = NULL, | ||
| 1178 | [NFT_FIB_RESULT_OIF] = "oif", | ||
| 1179 | [NFT_FIB_RESULT_OIFNAME] = "oifname", | ||
| 1180 | [NFT_FIB_RESULT_ADDRTYPE] = "type", | ||
| 1181 | [__NFT_FIB_RESULT_MAX] = "check", /* Actually, NFT_FIB_F_PRESENT. */ | ||
| 1182 | }; | ||
| 1183 | 26 | enum nft_fib_result resultval = NFT_FIB_RESULT_UNSPEC; | |
| 1184 | const char *result; | ||
| 1185 | 26 | int flagval = 0; | |
| 1186 | unsigned int i; | ||
| 1187 | |||
| 1188 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 26 times.
|
26 | if (json_unpack_err(ctx, root, "{s:s}", "result", &result)) |
| 1189 | ✗ | return NULL; | |
| 1190 | |||
| 1191 |
1/2✓ Branch 0 (9→6) taken 50 times.
✗ Branch 1 (9→10) not taken.
|
50 | for (i = 1; i < array_size(fib_result_tbl); i++) { |
| 1192 |
2/2✓ Branch 0 (6→7) taken 26 times.
✓ Branch 1 (6→8) taken 24 times.
|
50 | if (!strcmp(result, fib_result_tbl[i])) { |
| 1193 | 26 | resultval = i; | |
| 1194 | 26 | break; | |
| 1195 | } | ||
| 1196 | } | ||
| 1197 |
2/3✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 4 times.
✓ Branch 2 (10→14) taken 22 times.
|
26 | switch (resultval) { |
| 1198 | ✗ | case NFT_FIB_RESULT_UNSPEC: | |
| 1199 | ✗ | json_error(ctx, "Invalid fib result '%s'.", result); | |
| 1200 | ✗ | return NULL; | |
| 1201 | 4 | case __NFT_FIB_RESULT_MAX: | |
| 1202 | 4 | resultval = NFT_FIB_RESULT_OIF; | |
| 1203 | 4 | flagval = NFTA_FIB_F_PRESENT; | |
| 1204 | 4 | break; | |
| 1205 | 22 | default: | |
| 1206 | 22 | break; | |
| 1207 | } | ||
| 1208 | |||
| 1209 | 26 | flagval |= parse_flags_array(ctx, root, "flags", fib_flag_parse); | |
| 1210 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 26 times.
|
26 | if (flagval < 0) |
| 1211 | ✗ | return NULL; | |
| 1212 | |||
| 1213 | /* sanity checks from fib_expr in parser_bison.y */ | ||
| 1214 | |||
| 1215 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→21) taken 26 times.
|
26 | if ((flagval & (NFTA_FIB_F_SADDR|NFTA_FIB_F_DADDR)) == 0) { |
| 1216 | ✗ | json_error(ctx, "fib: need either saddr or daddr"); | |
| 1217 | ✗ | return NULL; | |
| 1218 | } | ||
| 1219 | |||
| 1220 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→24) taken 26 times.
|
26 | if ((flagval & (NFTA_FIB_F_SADDR|NFTA_FIB_F_DADDR)) == |
| 1221 | (NFTA_FIB_F_SADDR|NFTA_FIB_F_DADDR)) { | ||
| 1222 | ✗ | json_error(ctx, "fib: saddr and daddr are mutually exclusive"); | |
| 1223 | ✗ | return NULL; | |
| 1224 | } | ||
| 1225 | |||
| 1226 |
1/2✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→27) taken 26 times.
|
26 | if ((flagval & (NFTA_FIB_F_IIF|NFTA_FIB_F_OIF)) == |
| 1227 | (NFTA_FIB_F_IIF|NFTA_FIB_F_OIF)) { | ||
| 1228 | ✗ | json_error(ctx, "fib: iif and oif are mutually exclusive"); | |
| 1229 | ✗ | return NULL; | |
| 1230 | } | ||
| 1231 | |||
| 1232 | 26 | return fib_expr_alloc(int_loc, flagval, resultval); | |
| 1233 | } | ||
| 1234 | |||
| 1235 | 628 | static struct expr *json_parse_binop_expr(struct json_ctx *ctx, | |
| 1236 | const char *type, json_t *root) | ||
| 1237 | { | ||
| 1238 | const struct { | ||
| 1239 | const char *type; | ||
| 1240 | enum ops op; | ||
| 1241 | 628 | } op_tbl[] = { | |
| 1242 | { "|", OP_OR }, | ||
| 1243 | { "^", OP_XOR }, | ||
| 1244 | { "&", OP_AND }, | ||
| 1245 | { ">>", OP_RSHIFT }, | ||
| 1246 | { "<<", OP_LSHIFT }, | ||
| 1247 | }; | ||
| 1248 | 628 | enum ops thisop = OP_INVALID; | |
| 1249 | struct expr *left, *right; | ||
| 1250 | json_t *jleft, *jright; | ||
| 1251 | unsigned int i; | ||
| 1252 | |||
| 1253 |
1/2✓ Branch 0 (7→3) taken 1284 times.
✗ Branch 1 (7→8) not taken.
|
1284 | for (i = 0; i < array_size(op_tbl); i++) { |
| 1254 |
2/2✓ Branch 0 (3→4) taken 656 times.
✓ Branch 1 (3→5) taken 628 times.
|
1284 | if (strcmp(type, op_tbl[i].type)) |
| 1255 | 656 | continue; | |
| 1256 | |||
| 1257 | 628 | thisop = op_tbl[i].op; | |
| 1258 | 628 | break; | |
| 1259 | } | ||
| 1260 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 628 times.
|
628 | if (thisop == OP_INVALID) { |
| 1261 | ✗ | json_error(ctx, "Invalid binop type '%s'.", type); | |
| 1262 | ✗ | return NULL; | |
| 1263 | } | ||
| 1264 | |||
| 1265 |
2/2✓ Branch 0 (12→13) taken 115 times.
✓ Branch 1 (12→37) taken 513 times.
|
628 | if (json_array_size(root) > 2) { |
| 1266 | 115 | left = json_parse_primary_expr(ctx, json_array_get(root, 0)); | |
| 1267 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→18) taken 115 times.
|
115 | if (!left) { |
| 1268 | ✗ | json_error(ctx, "Failed to parse LHS of binop expression."); | |
| 1269 | ✗ | return NULL; | |
| 1270 | } | ||
| 1271 | 115 | right = json_parse_primary_expr(ctx, json_array_get(root, 1)); | |
| 1272 |
2/2✓ Branch 0 (20→21) taken 1 times.
✓ Branch 1 (20→24) taken 114 times.
|
115 | if (!right) { |
| 1273 | 1 | json_error(ctx, "Failed to parse RHS of binop expression."); | |
| 1274 | 1 | expr_free(left); | |
| 1275 | 1 | return NULL; | |
| 1276 | } | ||
| 1277 | 114 | left = binop_expr_alloc(int_loc, thisop, left, right); | |
| 1278 |
2/2✓ Branch 0 (35→26) taken 247 times.
✓ Branch 1 (35→36) taken 114 times.
|
361 | for (i = 2; i < json_array_size(root); i++) { |
| 1279 | 247 | jright = json_array_get(root, i); | |
| 1280 | 247 | right = json_parse_primary_expr(ctx, jright); | |
| 1281 |
1/2✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→32) taken 247 times.
|
247 | if (!right) { |
| 1282 | ✗ | json_error(ctx, "Failed to parse RHS of binop expression."); | |
| 1283 | ✗ | expr_free(left); | |
| 1284 | ✗ | return NULL; | |
| 1285 | } | ||
| 1286 | 247 | left = binop_expr_alloc(int_loc, thisop, left, right); | |
| 1287 | } | ||
| 1288 | 114 | return left; | |
| 1289 | } | ||
| 1290 | |||
| 1291 |
1/2✗ Branch 0 (38→39) not taken.
✓ Branch 1 (38→40) taken 513 times.
|
513 | if (json_unpack_err(ctx, root, "[o, o!]", &jleft, &jright)) |
| 1292 | ✗ | return NULL; | |
| 1293 | |||
| 1294 | 513 | left = json_parse_primary_expr(ctx, jleft); | |
| 1295 |
1/2✗ Branch 0 (41→42) not taken.
✓ Branch 1 (41→44) taken 513 times.
|
513 | if (!left) { |
| 1296 | ✗ | json_error(ctx, "Failed to parse LHS of binop expression."); | |
| 1297 | ✗ | return NULL; | |
| 1298 | } | ||
| 1299 | 513 | right = json_parse_rhs_expr(ctx, jright); | |
| 1300 |
2/2✓ Branch 0 (45→46) taken 2 times.
✓ Branch 1 (45→49) taken 511 times.
|
513 | if (!right) { |
| 1301 | 2 | json_error(ctx, "Failed to parse RHS of binop expression."); | |
| 1302 | 2 | expr_free(left); | |
| 1303 | 2 | return NULL; | |
| 1304 | } | ||
| 1305 | 511 | return binop_expr_alloc(int_loc, thisop, left, right); | |
| 1306 | } | ||
| 1307 | |||
| 1308 | 1263 | static struct expr *json_check_concat_expr(struct json_ctx *ctx, struct expr *e) | |
| 1309 | { | ||
| 1310 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→6) taken 1262 times.
|
1263 | if (e->etype != EXPR_CONCAT) { |
| 1311 | 1 | json_error(ctx, "Expected concatenation, got %s", expr_name(e)); | |
| 1312 | 1 | goto err_free; | |
| 1313 | } | ||
| 1314 | |||
| 1315 |
3/4✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 1262 times.
✓ Branch 2 (8→9) taken 1260 times.
✓ Branch 3 (8→10) taken 2 times.
|
1262 | if (expr_concat(e)->size >= 2) |
| 1316 | 1260 | return e; | |
| 1317 | |||
| 1318 | 2 | json_error(ctx, "Concatenation with %d elements is illegal", | |
| 1319 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 2 times.
|
2 | expr_concat(e)->size); |
| 1320 | |||
| 1321 | 3 | err_free: | |
| 1322 | 3 | expr_free(e); | |
| 1323 | 3 | return NULL; | |
| 1324 | } | ||
| 1325 | |||
| 1326 | 1140 | static struct expr *json_parse_concat_expr(struct json_ctx *ctx, | |
| 1327 | const char *type, json_t *root) | ||
| 1328 | { | ||
| 1329 | 1140 | struct expr *expr = NULL, *tmp; | |
| 1330 | json_t *value; | ||
| 1331 | size_t index; | ||
| 1332 | |||
| 1333 |
2/4✓ Branch 0 (2→3) taken 1140 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→7) taken 1140 times.
|
1140 | if (!json_is_array(root)) { |
| 1334 | ✗ | json_error(ctx, "Unexpected concat object type %s.", | |
| 1335 | json_typename(root)); | ||
| 1336 | ✗ | return NULL; | |
| 1337 | } | ||
| 1338 | |||
| 1339 |
3/4✓ Branch 0 (22→23) taken 2450 times.
✓ Branch 1 (22→25) taken 1140 times.
✓ Branch 2 (24→8) taken 2450 times.
✗ Branch 3 (24→25) not taken.
|
3590 | json_array_foreach(root, index, value) { |
| 1340 | 2450 | tmp = json_parse_concat_elem_expr(ctx, value); | |
| 1341 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→13) taken 2450 times.
|
2450 | if (!tmp) { |
| 1342 | ✗ | json_error(ctx, "Parsing expr at index %zd failed.", index); | |
| 1343 | ✗ | expr_free(expr); | |
| 1344 | ✗ | return NULL; | |
| 1345 | } | ||
| 1346 |
2/2✓ Branch 0 (13→14) taken 1140 times.
✓ Branch 1 (13→15) taken 1310 times.
|
2450 | if (!expr) { |
| 1347 | 1140 | expr = tmp; | |
| 1348 | 1140 | continue; | |
| 1349 | } | ||
| 1350 |
2/2✓ Branch 0 (15→16) taken 1139 times.
✓ Branch 1 (15→19) taken 171 times.
|
1310 | if (expr->etype != EXPR_CONCAT) { |
| 1351 | struct expr *concat; | ||
| 1352 | |||
| 1353 | 1139 | concat = concat_expr_alloc(int_loc); | |
| 1354 | 1139 | concat_expr_add(concat, expr); | |
| 1355 | 1139 | expr = concat; | |
| 1356 | } | ||
| 1357 | 1310 | concat_expr_add(expr, tmp); | |
| 1358 | } | ||
| 1359 |
1/2✓ Branch 0 (25→26) taken 1140 times.
✗ Branch 1 (25→27) not taken.
|
1140 | return expr ? json_check_concat_expr(ctx, expr) : NULL; |
| 1360 | } | ||
| 1361 | |||
| 1362 | 138557 | static struct expr *json_parse_prefix_expr(struct json_ctx *ctx, | |
| 1363 | const char *type, json_t *root) | ||
| 1364 | { | ||
| 1365 | struct expr *expr; | ||
| 1366 | json_t *addr; | ||
| 1367 | int len; | ||
| 1368 | |||
| 1369 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 138557 times.
|
138557 | if (json_unpack_err(ctx, root, "{s:o, s:i}", |
| 1370 | "addr", &addr, "len", &len)) | ||
| 1371 | ✗ | return NULL; | |
| 1372 | |||
| 1373 | 138557 | expr = json_parse_primary_expr(ctx, addr); | |
| 1374 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 138557 times.
|
138557 | if (!expr) { |
| 1375 | ✗ | json_error(ctx, "Invalid address in prefix expr."); | |
| 1376 | ✗ | return NULL; | |
| 1377 | } | ||
| 1378 | 138557 | return prefix_expr_alloc(int_loc, expr, len); | |
| 1379 | } | ||
| 1380 | |||
| 1381 | 1458 | static struct expr *json_parse_range_expr(struct json_ctx *ctx, | |
| 1382 | const char *type, json_t *root) | ||
| 1383 | { | ||
| 1384 | struct expr *expr_low, *expr_high, *tmp; | ||
| 1385 | json_t *low, *high; | ||
| 1386 | |||
| 1387 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 1458 times.
|
1458 | if (json_unpack_err(ctx, root, "[o, o!]", &low, &high)) |
| 1388 | ✗ | return NULL; | |
| 1389 | |||
| 1390 | 1458 | expr_low = json_parse_primary_expr(ctx, low); | |
| 1391 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 1458 times.
|
1458 | if (!expr_low) { |
| 1392 | ✗ | json_error(ctx, "Invalid low value in range expression."); | |
| 1393 | ✗ | return NULL; | |
| 1394 | } | ||
| 1395 | 1458 | expr_high = json_parse_primary_expr(ctx, high); | |
| 1396 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→14) taken 1458 times.
|
1458 | if (!expr_high) { |
| 1397 | ✗ | json_error(ctx, "Invalid high value in range expression."); | |
| 1398 | ✗ | expr_free(expr_low); | |
| 1399 | ✗ | return NULL; | |
| 1400 | } | ||
| 1401 |
4/8✓ Branch 0 (14→15) taken 1458 times.
✗ Branch 1 (14→22) not taken.
✓ Branch 2 (15→16) taken 1458 times.
✗ Branch 3 (15→22) not taken.
✓ Branch 4 (16→17) taken 1458 times.
✗ Branch 5 (16→22) not taken.
✓ Branch 6 (17→18) taken 1458 times.
✗ Branch 7 (17→22) not taken.
|
1458 | if (is_symbol_value_expr(expr_low) && is_symbol_value_expr(expr_high)) { |
| 1402 | 1458 | tmp = symbol_range_expr_alloc(int_loc, | |
| 1403 | SYMBOL_VALUE, | ||
| 1404 | expr_low->scope, | ||
| 1405 | expr_low->identifier, | ||
| 1406 | expr_high->identifier); | ||
| 1407 | 1458 | expr_free(expr_low); | |
| 1408 | 1458 | expr_free(expr_high); | |
| 1409 | 1458 | return tmp; | |
| 1410 | } | ||
| 1411 | ✗ | return range_expr_alloc(int_loc, expr_low, expr_high); | |
| 1412 | } | ||
| 1413 | |||
| 1414 | 5597 | static struct expr *json_alloc_chain_expr(const char *chain) | |
| 1415 | { | ||
| 1416 |
2/2✓ Branch 0 (2→3) taken 4572 times.
✓ Branch 1 (2→4) taken 1025 times.
|
5597 | if (!chain) |
| 1417 | 4572 | return NULL; | |
| 1418 | |||
| 1419 | 1025 | return constant_expr_alloc(int_loc, &string_type, BYTEORDER_HOST_ENDIAN, | |
| 1420 | 1025 | strlen(chain) * BITS_PER_BYTE, chain); | |
| 1421 | } | ||
| 1422 | |||
| 1423 | 5598 | static struct expr *json_parse_verdict_expr(struct json_ctx *ctx, | |
| 1424 | const char *type, json_t *root) | ||
| 1425 | { | ||
| 1426 | const struct { | ||
| 1427 | int verdict; | ||
| 1428 | const char *name; | ||
| 1429 | bool need_chain; | ||
| 1430 | 5598 | } verdict_tbl[] = { | |
| 1431 | { NFT_CONTINUE, "continue", false }, | ||
| 1432 | { NFT_JUMP, "jump", true }, | ||
| 1433 | { NFT_GOTO, "goto", true }, | ||
| 1434 | { NFT_RETURN, "return", false }, | ||
| 1435 | { NF_ACCEPT, "accept", false }, | ||
| 1436 | { NF_DROP, "drop", false }, | ||
| 1437 | }; | ||
| 1438 | 5598 | const char *chain = NULL; | |
| 1439 | unsigned int i; | ||
| 1440 | |||
| 1441 |
1/2✓ Branch 0 (15→3) taken 25445 times.
✗ Branch 1 (15→16) not taken.
|
25445 | for (i = 0; i < array_size(verdict_tbl); i++) { |
| 1442 |
2/2✓ Branch 0 (3→4) taken 19847 times.
✓ Branch 1 (3→5) taken 5598 times.
|
25445 | if (strcmp(type, verdict_tbl[i].name)) |
| 1443 | 19847 | continue; | |
| 1444 | |||
| 1445 |
2/2✓ Branch 0 (5→6) taken 1026 times.
✓ Branch 1 (5→12) taken 4572 times.
|
5598 | if (verdict_tbl[i].need_chain) { |
| 1446 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 1026 times.
|
1026 | if (json_unpack_err(ctx, root, "{s:s}", "target", &chain)) |
| 1447 | ✗ | return NULL; | |
| 1448 | |||
| 1449 |
3/4✓ Branch 0 (9→10) taken 1026 times.
✗ Branch 1 (9→11) not taken.
✓ Branch 2 (10→11) taken 1 times.
✓ Branch 3 (10→12) taken 1025 times.
|
1026 | if (!chain || chain[0] == '\0') |
| 1450 | 1 | return NULL; | |
| 1451 | } | ||
| 1452 | |||
| 1453 | 5597 | return verdict_expr_alloc(int_loc, verdict_tbl[i].verdict, | |
| 1454 | json_alloc_chain_expr(chain)); | ||
| 1455 | } | ||
| 1456 | ✗ | json_error(ctx, "Unknown verdict '%s'.", type); | |
| 1457 | ✗ | return NULL; | |
| 1458 | } | ||
| 1459 | |||
| 1460 | 2085 | static struct expr *json_parse_set_expr(struct json_ctx *ctx, | |
| 1461 | const char *type, json_t *root) | ||
| 1462 | { | ||
| 1463 | 2085 | struct expr *expr, *set_expr = NULL; | |
| 1464 | json_t *value; | ||
| 1465 | size_t index; | ||
| 1466 | |||
| 1467 |
2/4✓ Branch 0 (2→3) taken 2085 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→14) taken 2085 times.
|
2085 | if (!json_is_array(root)) { |
| 1468 | ✗ | expr = json_parse_immediate(ctx, root); | |
| 1469 | ✗ | if (!expr) | |
| 1470 | ✗ | return NULL; | |
| 1471 | |||
| 1472 | ✗ | if (expr->etype == EXPR_SYMBOL && | |
| 1473 | ✗ | expr->symtype == SYMBOL_SET) | |
| 1474 | ✗ | return expr; | |
| 1475 | |||
| 1476 | ✗ | expr = set_elem_expr_alloc(int_loc, expr); | |
| 1477 | ✗ | set_expr = set_expr_alloc(int_loc, NULL); | |
| 1478 | ✗ | set_expr_add(set_expr, expr); | |
| 1479 | ✗ | return set_expr; | |
| 1480 | } | ||
| 1481 | |||
| 1482 |
3/4✓ Branch 0 (45→46) taken 470576 times.
✓ Branch 1 (45→48) taken 2083 times.
✓ Branch 2 (47→15) taken 470576 times.
✗ Branch 3 (47→48) not taken.
|
472659 | json_array_foreach(root, index, value) { |
| 1483 | struct expr *expr; | ||
| 1484 | json_t *jleft, *jright; | ||
| 1485 | |||
| 1486 |
2/2✓ Branch 0 (16→17) taken 10797 times.
✓ Branch 1 (16→32) taken 459779 times.
|
470576 | if (!json_unpack(value, "[o, o!]", &jleft, &jright)) { |
| 1487 | struct expr *expr2; | ||
| 1488 | |||
| 1489 | 10797 | expr = json_parse_rhs_expr(ctx, jleft); | |
| 1490 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→22) taken 10797 times.
|
10797 | if (!expr) { |
| 1491 | ✗ | json_error(ctx, "Invalid set elem at index %zu.", index); | |
| 1492 | ✗ | expr_free(set_expr); | |
| 1493 | 2 | return NULL; | |
| 1494 | } | ||
| 1495 |
2/2✓ Branch 0 (22→23) taken 10759 times.
✓ Branch 1 (22→24) taken 38 times.
|
10797 | if (expr->etype != EXPR_SET_ELEM) |
| 1496 | 10759 | expr = set_elem_expr_alloc(int_loc, expr); | |
| 1497 | |||
| 1498 | 10797 | expr2 = json_parse_set_rhs_expr(ctx, jright); | |
| 1499 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→30) taken 10797 times.
|
10797 | if (!expr2) { |
| 1500 | ✗ | json_error(ctx, "Invalid set elem at index %zu.", index); | |
| 1501 | ✗ | expr_free(expr); | |
| 1502 | ✗ | expr_free(set_expr); | |
| 1503 | ✗ | return NULL; | |
| 1504 | } | ||
| 1505 | 10797 | expr2 = mapping_expr_alloc(int_loc, expr, expr2); | |
| 1506 | 10797 | expr = expr2; | |
| 1507 | } else { | ||
| 1508 | 459779 | expr = json_parse_rhs_expr(ctx, value); | |
| 1509 | |||
| 1510 |
2/2✓ Branch 0 (33→34) taken 2 times.
✓ Branch 1 (33→37) taken 459777 times.
|
459779 | if (!expr) { |
| 1511 | 2 | json_error(ctx, "Invalid set elem at index %zu.", index); | |
| 1512 | 2 | expr_free(set_expr); | |
| 1513 | 2 | return NULL; | |
| 1514 | } | ||
| 1515 | |||
| 1516 |
2/2✓ Branch 0 (37→38) taken 459651 times.
✓ Branch 1 (37→39) taken 126 times.
|
459777 | if (expr->etype != EXPR_SET_ELEM) |
| 1517 | 459651 | expr = set_elem_expr_alloc(int_loc, expr); | |
| 1518 | } | ||
| 1519 | |||
| 1520 |
2/2✓ Branch 0 (39→40) taken 2083 times.
✓ Branch 1 (39→41) taken 468491 times.
|
470574 | if (!set_expr) |
| 1521 | 2083 | set_expr = set_expr_alloc(int_loc, NULL); | |
| 1522 | 470574 | set_expr_add(set_expr, expr); | |
| 1523 | } | ||
| 1524 | 2083 | return set_expr; | |
| 1525 | } | ||
| 1526 | |||
| 1527 | 569 | static struct expr *json_parse_map_expr(struct json_ctx *ctx, | |
| 1528 | const char *type, json_t *root) | ||
| 1529 | { | ||
| 1530 | json_t *jkey, *jdata; | ||
| 1531 | struct expr *key, *data; | ||
| 1532 | |||
| 1533 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 569 times.
|
569 | if (json_unpack_err(ctx, root, "{s:o, s:o}", |
| 1534 | "key", &jkey, "data", &jdata)) | ||
| 1535 | ✗ | return NULL; | |
| 1536 | |||
| 1537 | 569 | key = json_parse_map_lhs_expr(ctx, jkey); | |
| 1538 |
2/2✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→9) taken 567 times.
|
569 | if (!key) { |
| 1539 | 2 | json_error(ctx, "Illegal map expression key."); | |
| 1540 | 2 | return NULL; | |
| 1541 | } | ||
| 1542 | |||
| 1543 | 567 | data = json_parse_rhs_expr(ctx, jdata); | |
| 1544 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→14) taken 567 times.
|
567 | if (!data) { |
| 1545 | ✗ | json_error(ctx, "Illegal map expression data."); | |
| 1546 | ✗ | expr_free(key); | |
| 1547 | ✗ | return NULL; | |
| 1548 | } | ||
| 1549 | |||
| 1550 | 567 | return map_expr_alloc(int_loc, key, data); | |
| 1551 | } | ||
| 1552 | |||
| 1553 | 209 | static struct expr *json_parse_set_elem_expr(struct json_ctx *ctx, | |
| 1554 | const char *type, json_t *root) | ||
| 1555 | { | ||
| 1556 | struct expr *expr; | ||
| 1557 | json_t *tmp; | ||
| 1558 | uint64_t i; | ||
| 1559 | |||
| 1560 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 209 times.
|
209 | if (json_unpack_err(ctx, root, "{s:o}", "val", &tmp)) |
| 1561 | ✗ | return NULL; | |
| 1562 | |||
| 1563 | 209 | expr = json_parse_expr(ctx, tmp); | |
| 1564 |
2/2✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→8) taken 208 times.
|
209 | if (!expr) |
| 1565 | 1 | return NULL; | |
| 1566 | |||
| 1567 | 208 | expr = set_elem_expr_alloc(int_loc, expr); | |
| 1568 | |||
| 1569 |
2/2✓ Branch 0 (10→11) taken 56 times.
✓ Branch 1 (10→12) taken 152 times.
|
208 | if (!json_unpack(root, "{s:I}", "timeout", &i)) |
| 1570 | 56 | expr->timeout = i * 1000; | |
| 1571 |
2/2✓ Branch 0 (13→14) taken 48 times.
✓ Branch 1 (13→15) taken 160 times.
|
208 | if (!json_unpack(root, "{s:I}", "expires", &i)) |
| 1572 | 48 | expr->expiration = i * 1000; | |
| 1573 |
2/2✓ Branch 0 (16→17) taken 10 times.
✓ Branch 1 (16→19) taken 198 times.
|
208 | if (!json_unpack(root, "{s:s}", "comment", &expr->comment)) |
| 1574 | 10 | expr->comment = xstrdup(expr->comment); | |
| 1575 | |||
| 1576 | 208 | return expr; | |
| 1577 | } | ||
| 1578 | |||
| 1579 | 40 | static struct expr *json_parse_xfrm_expr(struct json_ctx *ctx, | |
| 1580 | const char *type, json_t *root) | ||
| 1581 | { | ||
| 1582 | const char *key, *dir; | ||
| 1583 | unsigned int i, spnum; | ||
| 1584 | 40 | int dirval = -1, familyval, keyval = -1; | |
| 1585 | |||
| 1586 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 40 times.
|
40 | if (json_unpack_err(ctx, root, "{s:s}", "key", &key)) |
| 1587 | ✗ | return NULL; | |
| 1588 | |||
| 1589 |
1/2✓ Branch 0 (10→6) taken 167 times.
✗ Branch 1 (10→11) not taken.
|
167 | for (i = 1; i < array_size(xfrm_templates); i++) { |
| 1590 |
2/2✓ Branch 0 (6→7) taken 127 times.
✓ Branch 1 (6→8) taken 40 times.
|
167 | if (strcmp(key, xfrm_templates[i].token)) |
| 1591 | 127 | continue; | |
| 1592 | 40 | keyval = i; | |
| 1593 | 40 | break; | |
| 1594 | } | ||
| 1595 | |||
| 1596 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→14) taken 40 times.
|
40 | if (keyval == -1) { |
| 1597 | ✗ | json_error(ctx, "Unknown xfrm key '%s'.", key); | |
| 1598 | ✗ | return NULL; | |
| 1599 | } | ||
| 1600 | |||
| 1601 | 40 | familyval = json_parse_family(ctx, root); | |
| 1602 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 40 times.
|
40 | if (familyval < 0) |
| 1603 | ✗ | return NULL; | |
| 1604 | |||
| 1605 |
1/2✓ Branch 0 (18→19) taken 40 times.
✗ Branch 1 (18→25) not taken.
|
40 | if (!json_unpack(root, "{s:s}", "dir", &dir)) { |
| 1606 |
2/2✓ Branch 0 (19→20) taken 28 times.
✓ Branch 1 (19→21) taken 12 times.
|
40 | if (!strcmp(dir, "in")) { |
| 1607 | 28 | dirval = XFRM_POLICY_IN; | |
| 1608 |
1/2✓ Branch 0 (21→22) taken 12 times.
✗ Branch 1 (21→23) not taken.
|
12 | } else if (!strcmp(dir, "out")) { |
| 1609 | 12 | dirval = XFRM_POLICY_OUT; | |
| 1610 | } else { | ||
| 1611 | ✗ | json_error(ctx, "Invalid direction '%s'.", dir); | |
| 1612 | ✗ | return NULL; | |
| 1613 | } | ||
| 1614 | } | ||
| 1615 | |||
| 1616 | 40 | spnum = 0; | |
| 1617 |
1/2✓ Branch 0 (26→27) taken 40 times.
✗ Branch 1 (26→30) not taken.
|
40 | if (!json_unpack(root, "{s:i}", "spnum", &spnum)) { |
| 1618 |
1/2✗ Branch 0 (27→28) not taken.
✓ Branch 1 (27→30) taken 40 times.
|
40 | if (spnum > 255) { |
| 1619 | ✗ | json_error(ctx, "Invalid spnum'%d'.", spnum); | |
| 1620 | ✗ | return NULL; | |
| 1621 | } | ||
| 1622 | } | ||
| 1623 | |||
| 1624 |
3/3✓ Branch 0 (30→31) taken 6 times.
✓ Branch 1 (30→34) taken 6 times.
✓ Branch 2 (30→37) taken 28 times.
|
40 | switch (keyval) { |
| 1625 | 6 | case NFT_XFRM_KEY_SADDR_IP4: | |
| 1626 |
2/2✓ Branch 0 (31→32) taken 3 times.
✓ Branch 1 (31→33) taken 3 times.
|
6 | if (familyval == NFPROTO_IPV6) |
| 1627 | 3 | keyval = NFT_XFRM_KEY_SADDR_IP6; | |
| 1628 | 6 | break; | |
| 1629 | 6 | case NFT_XFRM_KEY_DADDR_IP4: | |
| 1630 |
2/2✓ Branch 0 (34→35) taken 3 times.
✓ Branch 1 (34→36) taken 3 times.
|
6 | if (familyval == NFPROTO_IPV6) |
| 1631 | 3 | keyval = NFT_XFRM_KEY_DADDR_IP6; | |
| 1632 | 6 | break; | |
| 1633 | 28 | default: | |
| 1634 | 28 | break; | |
| 1635 | } | ||
| 1636 | |||
| 1637 | 40 | return xfrm_expr_alloc(int_loc, dirval, spnum, keyval); | |
| 1638 | } | ||
| 1639 | |||
| 1640 | 722477 | static struct expr *json_parse_expr(struct json_ctx *ctx, json_t *root) | |
| 1641 | { | ||
| 1642 | const struct { | ||
| 1643 | const char *name; | ||
| 1644 | struct expr *(*cb)(struct json_ctx *, const char *, json_t *); | ||
| 1645 | uint32_t flags; | ||
| 1646 | 722477 | } cb_tbl[] = { | |
| 1647 | { "concat", json_parse_concat_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_DTYPE | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP }, | ||
| 1648 | { "set", json_parse_set_expr, CTX_F_RHS | CTX_F_STMT }, /* allow this as stmt expr because that allows set references */ | ||
| 1649 | { "map", json_parse_map_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS }, | ||
| 1650 | /* below three are multiton_rhs_expr */ | ||
| 1651 | { "prefix", json_parse_prefix_expr, CTX_F_RHS | CTX_F_SET_RHS | CTX_F_STMT | CTX_F_CONCAT }, | ||
| 1652 | { "range", json_parse_range_expr, CTX_F_RHS | CTX_F_SET_RHS | CTX_F_STMT | CTX_F_CONCAT }, | ||
| 1653 | { "payload", json_parse_payload_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1654 | { "exthdr", json_parse_exthdr_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1655 | { "tcp option", json_parse_tcp_option_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_CONCAT }, | ||
| 1656 | { "ip option", json_parse_ip_option_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_CONCAT }, | ||
| 1657 | { "sctp chunk", json_parse_sctp_chunk_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_CONCAT }, | ||
| 1658 | { "dccp option", json_parse_dccp_option_expr, CTX_F_PRIMARY }, | ||
| 1659 | { "meta", json_parse_meta_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1660 | { "osf", json_parse_osf_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1661 | { "ipsec", json_parse_xfrm_expr, CTX_F_PRIMARY | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1662 | { "socket", json_parse_socket_expr, CTX_F_PRIMARY | CTX_F_CONCAT }, | ||
| 1663 | { "rt", json_parse_rt_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1664 | { "ct", json_parse_ct_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1665 | { "numgen", json_parse_numgen_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1666 | { "tunnel", json_parse_tunnel_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SES | CTX_F_MAP }, | ||
| 1667 | /* below two are hash expr */ | ||
| 1668 | { "jhash", json_parse_hash_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1669 | { "symhash", json_parse_hash_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1670 | { "fib", json_parse_fib_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1671 | { "|", json_parse_binop_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1672 | { "^", json_parse_binop_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1673 | { "&", json_parse_binop_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1674 | { ">>", json_parse_binop_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1675 | { "<<", json_parse_binop_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, | ||
| 1676 | { "accept", json_parse_verdict_expr, CTX_F_RHS | CTX_F_SET_RHS }, | ||
| 1677 | { "drop", json_parse_verdict_expr, CTX_F_RHS | CTX_F_SET_RHS }, | ||
| 1678 | { "continue", json_parse_verdict_expr, CTX_F_RHS | CTX_F_SET_RHS }, | ||
| 1679 | { "jump", json_parse_verdict_expr, CTX_F_RHS | CTX_F_SET_RHS }, | ||
| 1680 | { "goto", json_parse_verdict_expr, CTX_F_RHS | CTX_F_SET_RHS }, | ||
| 1681 | { "return", json_parse_verdict_expr, CTX_F_RHS | CTX_F_SET_RHS }, | ||
| 1682 | { "elem", json_parse_set_elem_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SES }, | ||
| 1683 | }; | ||
| 1684 | struct expr *list; | ||
| 1685 | const char *type; | ||
| 1686 | unsigned int i; | ||
| 1687 | json_t *value; | ||
| 1688 | size_t index; | ||
| 1689 | |||
| 1690 | |||
| 1691 |
4/4✓ Branch 0 (2→3) taken 79 times.
✓ Branch 1 (2→23) taken 177 times.
✓ Branch 2 (2→27) taken 529574 times.
✓ Branch 3 (2→28) taken 192647 times.
|
722477 | switch (json_typeof(root)) { |
| 1692 | 79 | case JSON_ARRAY: | |
| 1693 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 79 times.
|
79 | if (!(ctx->flags & (CTX_F_RHS | CTX_F_STMT))) { |
| 1694 | ✗ | json_error(ctx, "List expression only allowed on RHS or in statement expression."); | |
| 1695 | ✗ | return NULL; | |
| 1696 | } | ||
| 1697 | |||
| 1698 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 79 times.
|
79 | if (is_PRIMARY(ctx)) { |
| 1699 | ✗ | json_error(ctx, "List expression not allowed as primary expression."); | |
| 1700 | ✗ | return NULL; | |
| 1701 | } | ||
| 1702 | |||
| 1703 | 79 | list = list_expr_alloc(int_loc); | |
| 1704 |
3/4✓ Branch 0 (19→20) taken 192 times.
✓ Branch 1 (19→22) taken 78 times.
✓ Branch 2 (21→11) taken 192 times.
✗ Branch 3 (21→22) not taken.
|
270 | json_array_foreach(root, index, value) { |
| 1705 | 192 | struct expr *expr = json_parse_expr(ctx, value); | |
| 1706 |
2/2✓ Branch 0 (12→13) taken 1 times.
✓ Branch 1 (12→16) taken 191 times.
|
192 | if (!expr) { |
| 1707 | 1 | json_error(ctx, "Parsing list expression item at index %zu failed.", index); | |
| 1708 | 1 | expr_free(list); | |
| 1709 | 1 | return NULL; | |
| 1710 | } | ||
| 1711 | 191 | list_expr_add(list, expr); | |
| 1712 | } | ||
| 1713 | 78 | return list; | |
| 1714 | 177 | case JSON_TRUE: | |
| 1715 | case JSON_FALSE: | ||
| 1716 |
1/4✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→27) taken 177 times.
✗ Branch 2 (24→25) not taken.
✗ Branch 3 (24→27) not taken.
|
177 | if (!is_RHS(ctx) && !is_PRIMARY(ctx)) { |
| 1717 | ✗ | json_error(ctx, "Boolean values not allowed in this context."); | |
| 1718 | ✗ | return NULL; | |
| 1719 | } | ||
| 1720 | /* fall through */ | ||
| 1721 | case JSON_STRING: | ||
| 1722 | case JSON_INTEGER: | ||
| 1723 | 529751 | return json_parse_immediate(ctx, root); | |
| 1724 | 192647 | default: | |
| 1725 | 192647 | break; | |
| 1726 | } | ||
| 1727 | |||
| 1728 |
2/2✓ Branch 0 (30→31) taken 1 times.
✓ Branch 1 (30→32) taken 192646 times.
|
192647 | if (json_unpack_stmt(ctx, root, &type, &value)) |
| 1729 | 1 | return NULL; | |
| 1730 | |||
| 1731 |
1/2✓ Branch 0 (41→33) taken 1154175 times.
✗ Branch 1 (41→42) not taken.
|
1154175 | for (i = 0; i < array_size(cb_tbl); i++) { |
| 1732 | uint32_t expr_flags; | ||
| 1733 | |||
| 1734 |
2/2✓ Branch 0 (33→34) taken 961529 times.
✓ Branch 1 (33→35) taken 192646 times.
|
1154175 | if (strcmp(type, cb_tbl[i].name)) |
| 1735 | 961529 | continue; | |
| 1736 | |||
| 1737 | /* Only check expression context flags, not command-level flags */ | ||
| 1738 | 192646 | expr_flags = ctx->flags & CTX_F_EXPR_MASK; | |
| 1739 |
2/2✓ Branch 0 (35→36) taken 2 times.
✓ Branch 1 (35→39) taken 192644 times.
|
192646 | if ((cb_tbl[i].flags & expr_flags) != expr_flags) { |
| 1740 | 2 | json_error(ctx, "Expression type %s not allowed in context (%s).", | |
| 1741 | type, ctx_flags_to_string(ctx)); | ||
| 1742 | 2 | return NULL; | |
| 1743 | } | ||
| 1744 | |||
| 1745 | 192644 | return cb_tbl[i].cb(ctx, type, value); | |
| 1746 | } | ||
| 1747 | ✗ | json_error(ctx, "Unknown expression type '%s'.", type); | |
| 1748 | ✗ | return NULL; | |
| 1749 | } | ||
| 1750 | |||
| 1751 | 675670 | static struct expr *json_parse_flagged_expr(struct json_ctx *ctx, | |
| 1752 | uint32_t flags, json_t *root) | ||
| 1753 | { | ||
| 1754 | 675670 | uint32_t old_flags = ctx->flags; | |
| 1755 | struct expr *expr; | ||
| 1756 | |||
| 1757 | 675670 | ctx->flags |= flags; | |
| 1758 | 675670 | expr = json_parse_expr(ctx, root); | |
| 1759 | 675670 | ctx->flags = old_flags; | |
| 1760 | |||
| 1761 | 675670 | return expr; | |
| 1762 | } | ||
| 1763 | |||
| 1764 | 517871 | static struct expr *json_parse_rhs_expr(struct json_ctx *ctx, json_t *root) | |
| 1765 | { | ||
| 1766 | 517871 | return json_parse_flagged_expr(ctx, CTX_F_RHS, root); | |
| 1767 | } | ||
| 1768 | |||
| 1769 | 904 | static struct expr *json_parse_stmt_expr(struct json_ctx *ctx, json_t *root) | |
| 1770 | { | ||
| 1771 | 904 | return json_parse_flagged_expr(ctx, CTX_F_STMT, root); | |
| 1772 | } | ||
| 1773 | |||
| 1774 | 142463 | static struct expr *json_parse_primary_expr(struct json_ctx *ctx, json_t *root) | |
| 1775 | { | ||
| 1776 | 142463 | return json_parse_flagged_expr(ctx, CTX_F_PRIMARY, root); | |
| 1777 | } | ||
| 1778 | |||
| 1779 | 10797 | static struct expr *json_parse_set_rhs_expr(struct json_ctx *ctx, json_t *root) | |
| 1780 | { | ||
| 1781 | 10797 | return json_parse_flagged_expr(ctx, CTX_F_SET_RHS, root); | |
| 1782 | } | ||
| 1783 | |||
| 1784 | 463 | static struct expr *json_parse_mangle_lhs_expr(struct json_ctx *ctx, json_t *root) | |
| 1785 | { | ||
| 1786 | 463 | return json_parse_flagged_expr(ctx, CTX_F_MANGLE, root); | |
| 1787 | } | ||
| 1788 | |||
| 1789 | 153 | static struct expr *json_parse_set_elem_expr_stmt(struct json_ctx *ctx, json_t *root) | |
| 1790 | { | ||
| 1791 | 153 | struct expr *expr = json_parse_flagged_expr(ctx, CTX_F_SES, root); | |
| 1792 | |||
| 1793 |
3/4✓ Branch 0 (3→4) taken 153 times.
✗ Branch 1 (3→6) not taken.
✓ Branch 2 (4→5) taken 109 times.
✓ Branch 3 (4→6) taken 44 times.
|
153 | if (expr && expr->etype != EXPR_SET_ELEM) |
| 1794 | 109 | expr = set_elem_expr_alloc(int_loc, expr); | |
| 1795 | |||
| 1796 | 153 | return expr; | |
| 1797 | } | ||
| 1798 | |||
| 1799 | 569 | static struct expr *json_parse_map_lhs_expr(struct json_ctx *ctx, json_t *root) | |
| 1800 | { | ||
| 1801 | 569 | return json_parse_flagged_expr(ctx, CTX_F_MAP, root); | |
| 1802 | } | ||
| 1803 | |||
| 1804 | 2450 | static struct expr *json_parse_concat_elem_expr(struct json_ctx *ctx, json_t *root) | |
| 1805 | { | ||
| 1806 | 2450 | return json_parse_flagged_expr(ctx, CTX_F_CONCAT, root); | |
| 1807 | } | ||
| 1808 | |||
| 1809 | 1099 | static struct expr *json_parse_dtype_expr(struct json_ctx *ctx, json_t *root) | |
| 1810 | { | ||
| 1811 |
3/4✓ Branch 0 (2→3) taken 1099 times.
✗ Branch 1 (2→12) not taken.
✓ Branch 2 (3→4) taken 824 times.
✓ Branch 3 (3→12) taken 275 times.
|
1099 | if (json_is_string(root)) { |
| 1812 | const struct datatype *dtype; | ||
| 1813 | |||
| 1814 | 824 | dtype = datatype_lookup_byname(json_string_value(root)); | |
| 1815 |
2/2✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→10) taken 822 times.
|
824 | if (!dtype) { |
| 1816 | 2 | json_error(ctx, "Invalid datatype '%s'.", | |
| 1817 | json_string_value(root)); | ||
| 1818 | 2 | return NULL; | |
| 1819 | } | ||
| 1820 | 822 | return constant_expr_alloc(int_loc, dtype, | |
| 1821 | 822 | dtype->byteorder, dtype->size, NULL); | |
| 1822 |
3/4✓ Branch 0 (12→13) taken 275 times.
✗ Branch 1 (12→29) not taken.
✓ Branch 2 (13→14) taken 123 times.
✓ Branch 3 (13→29) taken 152 times.
|
275 | } else if (json_is_array(root)) { |
| 1823 | json_t *value; | ||
| 1824 | size_t index; | ||
| 1825 | 123 | struct expr *expr = concat_expr_alloc(int_loc); | |
| 1826 | |||
| 1827 |
3/4✓ Branch 0 (24→25) taken 288 times.
✓ Branch 1 (24→27) taken 123 times.
✓ Branch 2 (26→16) taken 288 times.
✗ Branch 3 (26→27) not taken.
|
411 | json_array_foreach(root, index, value) { |
| 1828 | 288 | struct expr *i = json_parse_dtype_expr(ctx, value); | |
| 1829 | |||
| 1830 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→21) taken 288 times.
|
288 | if (!i) { |
| 1831 | ✗ | json_error(ctx, "Invalid datatype at index %zu.", index); | |
| 1832 | ✗ | expr_free(expr); | |
| 1833 | ✗ | return NULL; | |
| 1834 | } | ||
| 1835 | 288 | concat_expr_add(expr, i); | |
| 1836 | } | ||
| 1837 | |||
| 1838 | 123 | return json_check_concat_expr(ctx, expr); | |
| 1839 |
2/4✓ Branch 0 (29→30) taken 152 times.
✗ Branch 1 (29→38) not taken.
✓ Branch 2 (30→31) taken 152 times.
✗ Branch 3 (30→38) not taken.
|
152 | } else if (json_is_object(root)) { |
| 1840 | const char *key; | ||
| 1841 | json_t *val; | ||
| 1842 | |||
| 1843 |
1/2✓ Branch 0 (32→33) taken 152 times.
✗ Branch 1 (32→36) not taken.
|
152 | if (!json_unpack_stmt(ctx, root, &key, &val) && |
| 1844 |
1/2✓ Branch 0 (33→34) taken 152 times.
✗ Branch 1 (33→36) not taken.
|
152 | !strcmp(key, "typeof")) { |
| 1845 | 152 | return json_parse_expr(ctx, val); | |
| 1846 | } | ||
| 1847 | } | ||
| 1848 | |||
| 1849 | ✗ | json_error(ctx, "Invalid set datatype."); | |
| 1850 | ✗ | return NULL; | |
| 1851 | } | ||
| 1852 | |||
| 1853 | 46215 | static struct stmt *json_parse_match_stmt(struct json_ctx *ctx, | |
| 1854 | const char *key, json_t *value) | ||
| 1855 | { | ||
| 1856 | struct expr *left, *right, *rel_expr; | ||
| 1857 | json_t *jleft, *jright; | ||
| 1858 | 46215 | const char *opstr = NULL; | |
| 1859 | enum ops op; | ||
| 1860 | |||
| 1861 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 46215 times.
|
46215 | if (json_unpack_err(ctx, value, "{s:o, s:o, s:s}", |
| 1862 | "left", &jleft, | ||
| 1863 | "right", &jright, | ||
| 1864 | "op", &opstr)) | ||
| 1865 | ✗ | return NULL; | |
| 1866 | |||
| 1867 |
2/2✓ Branch 0 (10→6) taken 464989 times.
✓ Branch 1 (10→11) taken 182 times.
|
465171 | for (op = OP_INVALID; op < __OP_MAX; op++) { |
| 1868 |
2/2✓ Branch 0 (6→7) taken 418774 times.
✓ Branch 1 (6→9) taken 46215 times.
|
464989 | if (expr_op_symbols[op] && |
| 1869 |
2/2✓ Branch 0 (7→8) taken 46033 times.
✓ Branch 1 (7→9) taken 372741 times.
|
418774 | !strcmp(opstr, expr_op_symbols[op])) |
| 1870 | 46033 | break; | |
| 1871 | } | ||
| 1872 |
2/3✓ Branch 0 (11→12) taken 46033 times.
✓ Branch 1 (11→13) taken 182 times.
✗ Branch 2 (11→15) not taken.
|
46215 | switch (op) { |
| 1873 | 46033 | case OP_EQ ... OP_NEG: | |
| 1874 | 46033 | break; | |
| 1875 | 182 | case __OP_MAX: | |
| 1876 |
1/2✓ Branch 0 (13→14) taken 182 times.
✗ Branch 1 (13→15) not taken.
|
182 | if (!strcmp(opstr, "in")) { |
| 1877 | 182 | op = OP_IMPLICIT; | |
| 1878 | 182 | break; | |
| 1879 | } | ||
| 1880 | /* fall through */ | ||
| 1881 | default: | ||
| 1882 | ✗ | json_error(ctx, "Invalid relational op '%s'.", opstr); | |
| 1883 | ✗ | return NULL; | |
| 1884 | } | ||
| 1885 | |||
| 1886 | 46215 | left = json_parse_expr(ctx, jleft); | |
| 1887 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→21) taken 46215 times.
|
46215 | if (!left) { |
| 1888 | ✗ | json_error(ctx, "Invalid LHS of relational."); | |
| 1889 | ✗ | return NULL; | |
| 1890 | } | ||
| 1891 | 46215 | right = json_parse_rhs_expr(ctx, jright); | |
| 1892 |
1/2✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→26) taken 46215 times.
|
46215 | if (!right) { |
| 1893 | ✗ | expr_free(left); | |
| 1894 | ✗ | json_error(ctx, "Invalid RHS of relational."); | |
| 1895 | ✗ | return NULL; | |
| 1896 | } | ||
| 1897 | |||
| 1898 | 46215 | rel_expr = relational_expr_alloc(int_loc, op, left, right); | |
| 1899 | 46215 | return expr_stmt_alloc(int_loc, rel_expr); | |
| 1900 | } | ||
| 1901 | |||
| 1902 | 313 | static struct stmt *json_parse_counter_stmt(struct json_ctx *ctx, | |
| 1903 | const char *key, json_t *value) | ||
| 1904 | { | ||
| 1905 | uint64_t packets, bytes; | ||
| 1906 | struct stmt *stmt; | ||
| 1907 | |||
| 1908 |
3/4✓ Branch 0 (2→3) taken 313 times.
✗ Branch 1 (2→6) not taken.
✓ Branch 2 (3→4) taken 79 times.
✓ Branch 3 (3→6) taken 234 times.
|
313 | if (json_is_null(value)) |
| 1909 | 79 | return counter_stmt_alloc(int_loc); | |
| 1910 | |||
| 1911 |
2/2✓ Branch 0 (7→8) taken 208 times.
✓ Branch 1 (7→10) taken 26 times.
|
234 | if (!json_unpack(value, "{s:I, s:I}", |
| 1912 | "packets", &packets, | ||
| 1913 | "bytes", &bytes)) { | ||
| 1914 | 208 | stmt = counter_stmt_alloc(int_loc); | |
| 1915 | 208 | stmt->counter.packets = packets; | |
| 1916 | 208 | stmt->counter.bytes = bytes; | |
| 1917 | 208 | return stmt; | |
| 1918 | } | ||
| 1919 | |||
| 1920 | 26 | stmt = objref_stmt_alloc(int_loc); | |
| 1921 | 26 | stmt->objref.type = NFT_OBJECT_COUNTER; | |
| 1922 | 26 | stmt->objref.expr = json_parse_stmt_expr(ctx, value); | |
| 1923 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→16) taken 26 times.
|
26 | if (!stmt->objref.expr) { |
| 1924 | ✗ | json_error(ctx, "Invalid counter reference."); | |
| 1925 | ✗ | stmt_free(stmt); | |
| 1926 | ✗ | return NULL; | |
| 1927 | } | ||
| 1928 | 26 | return stmt; | |
| 1929 | } | ||
| 1930 | |||
| 1931 | 14 | static struct stmt *json_parse_last_stmt(struct json_ctx *ctx, | |
| 1932 | const char *key, json_t *value) | ||
| 1933 | { | ||
| 1934 | struct stmt *stmt; | ||
| 1935 | int64_t used; | ||
| 1936 | |||
| 1937 |
3/4✓ Branch 0 (2→3) taken 14 times.
✗ Branch 1 (2→6) not taken.
✓ Branch 2 (3→4) taken 8 times.
✓ Branch 3 (3→6) taken 6 times.
|
14 | if (json_is_null(value)) |
| 1938 | 8 | return last_stmt_alloc(int_loc); | |
| 1939 | |||
| 1940 |
1/2✓ Branch 0 (7→8) taken 6 times.
✗ Branch 1 (7→12) not taken.
|
6 | if (!json_unpack(value, "{s:I}", "used", &used)) { |
| 1941 | 6 | stmt = last_stmt_alloc(int_loc); | |
| 1942 |
1/2✓ Branch 0 (9→10) taken 6 times.
✗ Branch 1 (9→11) not taken.
|
6 | if (used != -1) { |
| 1943 | 6 | stmt->last.used = used; | |
| 1944 | 6 | stmt->last.set = 1; | |
| 1945 | } | ||
| 1946 | 6 | return stmt; | |
| 1947 | } | ||
| 1948 | |||
| 1949 | ✗ | return NULL; | |
| 1950 | } | ||
| 1951 | |||
| 1952 | 5103 | static struct stmt *json_parse_verdict_stmt(struct json_ctx *ctx, | |
| 1953 | const char *key, json_t *value) | ||
| 1954 | { | ||
| 1955 | struct expr *expr; | ||
| 1956 | |||
| 1957 | 5103 | expr = json_parse_verdict_expr(ctx, key, value); | |
| 1958 |
1/2✓ Branch 0 (3→4) taken 5103 times.
✗ Branch 1 (3→6) not taken.
|
5103 | if (expr) |
| 1959 | 5103 | return verdict_stmt_alloc(int_loc, expr); | |
| 1960 | |||
| 1961 | ✗ | return NULL; | |
| 1962 | } | ||
| 1963 | |||
| 1964 | 463 | static struct stmt *json_parse_mangle_stmt(struct json_ctx *ctx, | |
| 1965 | const char *type, json_t *root) | ||
| 1966 | { | ||
| 1967 | json_t *jkey, *jvalue; | ||
| 1968 | struct expr *key, *value; | ||
| 1969 | struct stmt *stmt; | ||
| 1970 | |||
| 1971 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 463 times.
|
463 | if (json_unpack_err(ctx, root, "{s:o, s:o}", |
| 1972 | "key", &jkey, "value", &jvalue)) | ||
| 1973 | ✗ | return NULL; | |
| 1974 | |||
| 1975 | 463 | key = json_parse_mangle_lhs_expr(ctx, jkey); | |
| 1976 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 463 times.
|
463 | if (!key) { |
| 1977 | ✗ | json_error(ctx, "Invalid mangle statement key"); | |
| 1978 | ✗ | return NULL; | |
| 1979 | } | ||
| 1980 | 463 | value = json_parse_stmt_expr(ctx, jvalue); | |
| 1981 |
2/2✓ Branch 0 (10→11) taken 3 times.
✓ Branch 1 (10→14) taken 460 times.
|
463 | if (!value) { |
| 1982 | 3 | json_error(ctx, "Invalid mangle statement value"); | |
| 1983 | 3 | expr_free(key); | |
| 1984 | 3 | return NULL; | |
| 1985 | } | ||
| 1986 | |||
| 1987 |
4/5✓ Branch 0 (14→15) taken 4 times.
✓ Branch 1 (14→16) taken 170 times.
✓ Branch 2 (14→17) taken 170 times.
✓ Branch 3 (14→20) taken 116 times.
✗ Branch 4 (14→26) not taken.
|
460 | switch (key->etype) { |
| 1988 | 4 | case EXPR_EXTHDR: | |
| 1989 | 4 | return exthdr_stmt_alloc(int_loc, key, value); | |
| 1990 | 170 | case EXPR_PAYLOAD: | |
| 1991 | 170 | return payload_stmt_alloc(int_loc, key, value); | |
| 1992 | 170 | case EXPR_META: | |
| 1993 | 170 | stmt = meta_stmt_alloc(int_loc, key->meta.key, value); | |
| 1994 | 170 | expr_free(key); | |
| 1995 | 170 | return stmt; | |
| 1996 | 116 | case EXPR_CT: | |
| 1997 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→23) taken 116 times.
|
116 | if (key->ct.key == NFT_CT_HELPER) { |
| 1998 | ✗ | stmt = objref_stmt_alloc(int_loc); | |
| 1999 | ✗ | stmt->objref.type = NFT_OBJECT_CT_HELPER; | |
| 2000 | ✗ | stmt->objref.expr = value; | |
| 2001 | } else { | ||
| 2002 | 116 | stmt = ct_stmt_alloc(int_loc, key->ct.key, | |
| 2003 | 116 | key->ct.direction, value); | |
| 2004 | } | ||
| 2005 | 116 | expr_free(key); | |
| 2006 | 116 | return stmt; | |
| 2007 | ✗ | default: | |
| 2008 | ✗ | json_error(ctx, "Invalid mangle statement key expression type."); | |
| 2009 | ✗ | expr_free(key); | |
| 2010 | ✗ | expr_free(value); | |
| 2011 | ✗ | return NULL; | |
| 2012 | } | ||
| 2013 | } | ||
| 2014 | |||
| 2015 | 449 | static uint64_t rate_to_bytes(uint64_t val, const char *unit) | |
| 2016 | { | ||
| 2017 | 449 | uint64_t bytes = val; | |
| 2018 | |||
| 2019 |
2/2✓ Branch 0 (2→3) taken 135 times.
✓ Branch 1 (2→4) taken 314 times.
|
449 | if (!strcmp(unit, "kbytes")) |
| 2020 | 135 | return bytes * 1024; | |
| 2021 |
2/2✓ Branch 0 (4→5) taken 97 times.
✓ Branch 1 (4→6) taken 217 times.
|
314 | if (!strcmp(unit, "mbytes")) |
| 2022 | 97 | return bytes * 1024 * 1024; | |
| 2023 | 217 | return bytes; | |
| 2024 | } | ||
| 2025 | |||
| 2026 | 103 | static struct stmt *json_parse_quota_stmt(struct json_ctx *ctx, | |
| 2027 | const char *key, json_t *value) | ||
| 2028 | { | ||
| 2029 | struct stmt *stmt; | ||
| 2030 | 103 | int inv = 0; | |
| 2031 | 103 | const char *val_unit = "bytes", *used_unit = "bytes"; | |
| 2032 | 103 | uint64_t val, used = 0; | |
| 2033 | |||
| 2034 |
2/2✓ Branch 0 (3→4) taken 97 times.
✓ Branch 1 (3→14) taken 6 times.
|
103 | if (!json_unpack(value, "{s:I}", "val", &val)) { |
| 2035 | 97 | json_unpack(value, "{s:b}", "inv", &inv); | |
| 2036 | 97 | json_unpack(value, "{s:s}", "val_unit", &val_unit); | |
| 2037 | 97 | json_unpack(value, "{s:I}", "used", &used); | |
| 2038 | 97 | json_unpack(value, "{s:s}", "used_unit", &used_unit); | |
| 2039 | 97 | stmt = quota_stmt_alloc(int_loc); | |
| 2040 | 97 | stmt->quota.bytes = rate_to_bytes(val, val_unit); | |
| 2041 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 97 times.
|
97 | if (used) |
| 2042 | ✗ | stmt->quota.used = rate_to_bytes(used, used_unit); | |
| 2043 | 97 | stmt->quota.flags = (inv ? NFT_QUOTA_F_INV : 0); | |
| 2044 | 97 | return stmt; | |
| 2045 | } | ||
| 2046 | 6 | stmt = objref_stmt_alloc(int_loc); | |
| 2047 | 6 | stmt->objref.type = NFT_OBJECT_QUOTA; | |
| 2048 | 6 | stmt->objref.expr = json_parse_stmt_expr(ctx, value); | |
| 2049 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→20) taken 6 times.
|
6 | if (!stmt->objref.expr) { |
| 2050 | ✗ | json_error(ctx, "Invalid quota reference."); | |
| 2051 | ✗ | stmt_free(stmt); | |
| 2052 | ✗ | return NULL; | |
| 2053 | } | ||
| 2054 | 6 | return stmt; | |
| 2055 | } | ||
| 2056 | |||
| 2057 | 283 | static uint64_t seconds_from_unit(const char *unit) | |
| 2058 | { | ||
| 2059 |
2/2✓ Branch 0 (2→3) taken 14 times.
✓ Branch 1 (2→4) taken 269 times.
|
283 | if (!strcmp(unit, "week")) |
| 2060 | 14 | return 60 * 60 * 24 * 7; | |
| 2061 |
2/2✓ Branch 0 (4→5) taken 14 times.
✓ Branch 1 (4→6) taken 255 times.
|
269 | if (!strcmp(unit, "day")) |
| 2062 | 14 | return 60 * 60 * 24; | |
| 2063 |
2/2✓ Branch 0 (6→7) taken 16 times.
✓ Branch 1 (6→8) taken 239 times.
|
255 | if (!strcmp(unit, "hour")) |
| 2064 | 16 | return 60 * 60; | |
| 2065 |
2/2✓ Branch 0 (8→9) taken 16 times.
✓ Branch 1 (8→10) taken 223 times.
|
239 | if (!strcmp(unit, "minute")) |
| 2066 | 16 | return 60; | |
| 2067 | 223 | return 1; | |
| 2068 | } | ||
| 2069 | |||
| 2070 | 279 | static struct stmt *json_parse_limit_stmt(struct json_ctx *ctx, | |
| 2071 | const char *key, json_t *value) | ||
| 2072 | { | ||
| 2073 | struct stmt *stmt; | ||
| 2074 | 279 | uint64_t rate, burst = 0; | |
| 2075 | 279 | const char *rate_unit = "packets", *time, *burst_unit = "bytes"; | |
| 2076 | 279 | int inv = 0; | |
| 2077 | |||
| 2078 |
2/2✓ Branch 0 (3→4) taken 271 times.
✓ Branch 1 (3→18) taken 8 times.
|
279 | if (!json_unpack(value, "{s:I, s:s}", |
| 2079 | "rate", &rate, "per", &time)) { | ||
| 2080 | 271 | json_unpack(value, "{s:s}", "rate_unit", &rate_unit); | |
| 2081 | 271 | json_unpack(value, "{s:b}", "inv", &inv); | |
| 2082 | 271 | json_unpack(value, "{s:I}", "burst", &burst); | |
| 2083 | 271 | json_unpack(value, "{s:s}", "burst_unit", &burst_unit); | |
| 2084 | |||
| 2085 | 271 | stmt = limit_stmt_alloc(int_loc); | |
| 2086 | |||
| 2087 |
2/2✓ Branch 0 (9→10) taken 101 times.
✓ Branch 1 (9→13) taken 170 times.
|
271 | if (!strcmp(rate_unit, "packets")) { |
| 2088 |
2/2✓ Branch 0 (10→11) taken 70 times.
✓ Branch 1 (10→12) taken 31 times.
|
101 | if (burst == 0) |
| 2089 | 70 | burst = 5; | |
| 2090 | |||
| 2091 | 101 | stmt->limit.type = NFT_LIMIT_PKTS; | |
| 2092 | 101 | stmt->limit.rate = rate; | |
| 2093 | 101 | stmt->limit.burst = burst; | |
| 2094 | } else { | ||
| 2095 | 170 | stmt->limit.type = NFT_LIMIT_PKT_BYTES; | |
| 2096 | 170 | stmt->limit.rate = rate_to_bytes(rate, rate_unit); | |
| 2097 | 170 | stmt->limit.burst = rate_to_bytes(burst, burst_unit); | |
| 2098 | } | ||
| 2099 | 271 | stmt->limit.unit = seconds_from_unit(time); | |
| 2100 | 271 | stmt->limit.flags = inv ? NFT_LIMIT_F_INV : 0; | |
| 2101 | 271 | return stmt; | |
| 2102 | } | ||
| 2103 | |||
| 2104 | 8 | stmt = objref_stmt_alloc(int_loc); | |
| 2105 | 8 | stmt->objref.type = NFT_OBJECT_LIMIT; | |
| 2106 | 8 | stmt->objref.expr = json_parse_stmt_expr(ctx, value); | |
| 2107 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→24) taken 8 times.
|
8 | if (!stmt->objref.expr) { |
| 2108 | ✗ | json_error(ctx, "Invalid limit reference."); | |
| 2109 | ✗ | stmt_free(stmt); | |
| 2110 | ✗ | return NULL; | |
| 2111 | } | ||
| 2112 | 8 | return stmt; | |
| 2113 | } | ||
| 2114 | |||
| 2115 | 6 | static struct stmt *json_parse_fwd_stmt(struct json_ctx *ctx, | |
| 2116 | const char *key, json_t *value) | ||
| 2117 | { | ||
| 2118 | json_t *jaddr, *jdev; | ||
| 2119 | struct stmt *stmt; | ||
| 2120 | int familyval; | ||
| 2121 | |||
| 2122 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 6 times.
|
6 | if (json_unpack_err(ctx, value, "{s:o}", "dev", &jdev)) |
| 2123 | ✗ | return NULL; | |
| 2124 | |||
| 2125 | 6 | stmt = fwd_stmt_alloc(int_loc); | |
| 2126 | |||
| 2127 | 6 | stmt->fwd.dev = json_parse_stmt_expr(ctx, jdev); | |
| 2128 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 6 times.
|
6 | if (!stmt->fwd.dev) { |
| 2129 | ✗ | json_error(ctx, "Invalid fwd dev value."); | |
| 2130 | ✗ | goto out_err; | |
| 2131 | } | ||
| 2132 | |||
| 2133 | 6 | familyval = json_parse_family(ctx, value); | |
| 2134 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 6 times.
|
6 | if (familyval < 0) |
| 2135 | ✗ | goto out_err; | |
| 2136 | |||
| 2137 |
3/4✓ Branch 0 (13→14) taken 2 times.
✓ Branch 1 (13→16) taken 4 times.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→17) taken 2 times.
|
8 | if (familyval == NFPROTO_UNSPEC || |
| 2138 | 2 | json_unpack(value, "{s:o}", "addr", &jaddr)) | |
| 2139 | 4 | return stmt; | |
| 2140 | |||
| 2141 | 2 | stmt->fwd.family = familyval; | |
| 2142 | 2 | stmt->fwd.addr = json_parse_stmt_expr(ctx, jaddr); | |
| 2143 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→21) taken 2 times.
|
2 | if (!stmt->fwd.addr) { |
| 2144 | ✗ | json_error(ctx, "Invalid fwd addr value."); | |
| 2145 | ✗ | goto out_err; | |
| 2146 | } | ||
| 2147 | |||
| 2148 | 2 | return stmt; | |
| 2149 | ✗ | out_err: | |
| 2150 | ✗ | stmt_free(stmt); | |
| 2151 | ✗ | return NULL; | |
| 2152 | } | ||
| 2153 | |||
| 2154 | 6 | static struct stmt *json_parse_flow_offload_stmt(struct json_ctx *ctx, | |
| 2155 | const char *key, json_t *value) | ||
| 2156 | { | ||
| 2157 | const char *opstr, *flowtable; | ||
| 2158 | |||
| 2159 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 6 times.
|
6 | if (json_unpack_err(ctx, value, "{s:s, s:s}", |
| 2160 | "op", &opstr, "flowtable", &flowtable)) | ||
| 2161 | ✗ | return NULL; | |
| 2162 | |||
| 2163 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 6 times.
|
6 | if (strcmp(opstr, "add")) { |
| 2164 | ✗ | json_error(ctx, "Unknown flow offload statement op '%s'.", opstr); | |
| 2165 | ✗ | return NULL; | |
| 2166 | } | ||
| 2167 | |||
| 2168 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 6 times.
|
6 | if (flowtable[0] != '@') { |
| 2169 | ✗ | json_error(ctx, "Illegal flowtable reference in flow offload statement."); | |
| 2170 | ✗ | return NULL; | |
| 2171 | } | ||
| 2172 | |||
| 2173 | 6 | return flow_offload_stmt_alloc(int_loc, xstrdup(flowtable + 1)); | |
| 2174 | } | ||
| 2175 | |||
| 2176 | 4 | static struct stmt *json_parse_notrack_stmt(struct json_ctx *ctx, | |
| 2177 | const char *key, json_t *value) | ||
| 2178 | { | ||
| 2179 | 4 | return notrack_stmt_alloc(int_loc); | |
| 2180 | } | ||
| 2181 | |||
| 2182 | 10 | static struct stmt *json_parse_dup_stmt(struct json_ctx *ctx, | |
| 2183 | const char *key, json_t *value) | ||
| 2184 | { | ||
| 2185 | struct stmt *stmt; | ||
| 2186 | struct expr *expr; | ||
| 2187 | json_t *tmp; | ||
| 2188 | |||
| 2189 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 10 times.
|
10 | if (json_unpack_err(ctx, value, "{s:o}", "addr", &tmp)) |
| 2190 | ✗ | return NULL; | |
| 2191 | |||
| 2192 | 10 | expr = json_parse_stmt_expr(ctx, tmp); | |
| 2193 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 10 times.
|
10 | if (!expr) { |
| 2194 | ✗ | json_error(ctx, "Illegal dup addr arg."); | |
| 2195 | ✗ | return NULL; | |
| 2196 | } | ||
| 2197 | |||
| 2198 | 10 | stmt = dup_stmt_alloc(int_loc); | |
| 2199 | 10 | stmt->dup.to = expr; | |
| 2200 | |||
| 2201 |
2/2✓ Branch 0 (11→12) taken 6 times.
✓ Branch 1 (11→13) taken 4 times.
|
10 | if (json_unpack(value, "{s:o}", "dev", &tmp)) |
| 2202 | 6 | return stmt; | |
| 2203 | |||
| 2204 | 4 | expr = json_parse_stmt_expr(ctx, tmp); | |
| 2205 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→18) taken 4 times.
|
4 | if (!expr) { |
| 2206 | ✗ | json_error(ctx, "Illegal dup dev."); | |
| 2207 | ✗ | stmt_free(stmt); | |
| 2208 | ✗ | return NULL; | |
| 2209 | } | ||
| 2210 | 4 | stmt->dup.dev = expr; | |
| 2211 | 4 | return stmt; | |
| 2212 | } | ||
| 2213 | |||
| 2214 | 3 | static struct stmt *json_parse_secmark_stmt(struct json_ctx *ctx, | |
| 2215 | const char *key, json_t *value) | ||
| 2216 | { | ||
| 2217 | struct stmt *stmt; | ||
| 2218 | |||
| 2219 | 3 | stmt = objref_stmt_alloc(int_loc); | |
| 2220 | 3 | stmt->objref.type = NFT_OBJECT_SECMARK; | |
| 2221 | 3 | stmt->objref.expr = json_parse_stmt_expr(ctx, value); | |
| 2222 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→8) taken 3 times.
|
3 | if (!stmt->objref.expr) { |
| 2223 | ✗ | json_error(ctx, "Invalid secmark reference."); | |
| 2224 | ✗ | stmt_free(stmt); | |
| 2225 | ✗ | return NULL; | |
| 2226 | } | ||
| 2227 | |||
| 2228 | 3 | return stmt; | |
| 2229 | } | ||
| 2230 | |||
| 2231 | 2 | static struct stmt *json_parse_tunnel_stmt(struct json_ctx *ctx, | |
| 2232 | const char *key, json_t *value) | ||
| 2233 | { | ||
| 2234 | struct stmt *stmt; | ||
| 2235 | |||
| 2236 | 2 | stmt = objref_stmt_alloc(int_loc); | |
| 2237 | 2 | stmt->objref.type = NFT_OBJECT_TUNNEL; | |
| 2238 | 2 | stmt->objref.expr = json_parse_stmt_expr(ctx, value); | |
| 2239 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→8) taken 2 times.
|
2 | if (!stmt->objref.expr) { |
| 2240 | ✗ | json_error(ctx, "Invalid tunnel reference."); | |
| 2241 | ✗ | stmt_free(stmt); | |
| 2242 | ✗ | return NULL; | |
| 2243 | } | ||
| 2244 | |||
| 2245 | 2 | return stmt; | |
| 2246 | } | ||
| 2247 | |||
| 2248 | 106 | static unsigned int json_parse_nat_flag(const char *flag) | |
| 2249 | { | ||
| 2250 | const struct { | ||
| 2251 | const char *flag; | ||
| 2252 | int val; | ||
| 2253 | 106 | } flag_tbl[] = { | |
| 2254 | { "random", NF_NAT_RANGE_PROTO_RANDOM }, | ||
| 2255 | { "fully-random", NF_NAT_RANGE_PROTO_RANDOM_FULLY }, | ||
| 2256 | { "persistent", NF_NAT_RANGE_PERSISTENT }, | ||
| 2257 | { "netmap", NF_NAT_RANGE_NETMAP }, | ||
| 2258 | }; | ||
| 2259 | unsigned int i; | ||
| 2260 | |||
| 2261 |
1/2✓ Branch 0 (6→3) taken 223 times.
✗ Branch 1 (6→7) not taken.
|
223 | for (i = 0; i < array_size(flag_tbl); i++) { |
| 2262 |
2/2✓ Branch 0 (3→4) taken 106 times.
✓ Branch 1 (3→5) taken 117 times.
|
223 | if (!strcmp(flag, flag_tbl[i].flag)) |
| 2263 | 106 | return flag_tbl[i].val; | |
| 2264 | } | ||
| 2265 | ✗ | return 0; | |
| 2266 | } | ||
| 2267 | |||
| 2268 | 6 | static unsigned int json_parse_nat_type_flag(const char *flag) | |
| 2269 | { | ||
| 2270 | const struct { | ||
| 2271 | const char *flag; | ||
| 2272 | int val; | ||
| 2273 | 6 | } flag_tbl[] = { | |
| 2274 | { "interval", STMT_NAT_F_INTERVAL }, | ||
| 2275 | { "prefix", STMT_NAT_F_PREFIX }, | ||
| 2276 | { "concat", STMT_NAT_F_CONCAT }, | ||
| 2277 | }; | ||
| 2278 | unsigned int i; | ||
| 2279 | |||
| 2280 |
1/2✓ Branch 0 (6→3) taken 11 times.
✗ Branch 1 (6→7) not taken.
|
11 | for (i = 0; i < array_size(flag_tbl); i++) { |
| 2281 |
2/2✓ Branch 0 (3→4) taken 6 times.
✓ Branch 1 (3→5) taken 5 times.
|
11 | if (!strcmp(flag, flag_tbl[i].flag)) |
| 2282 | 6 | return flag_tbl[i].val; | |
| 2283 | } | ||
| 2284 | ✗ | return 0; | |
| 2285 | } | ||
| 2286 | |||
| 2287 | 273 | static int nat_type_parse(const char *type) | |
| 2288 | { | ||
| 2289 | 273 | const char * const nat_etypes[] = { | |
| 2290 | [NFT_NAT_SNAT] = "snat", | ||
| 2291 | [NFT_NAT_DNAT] = "dnat", | ||
| 2292 | [NFT_NAT_MASQ] = "masquerade", | ||
| 2293 | [NFT_NAT_REDIR] = "redirect", | ||
| 2294 | }; | ||
| 2295 | size_t i; | ||
| 2296 | |||
| 2297 |
1/2✓ Branch 0 (6→3) taken 664 times.
✗ Branch 1 (6→7) not taken.
|
664 | for (i = 0; i < array_size(nat_etypes); i++) { |
| 2298 |
2/2✓ Branch 0 (3→4) taken 273 times.
✓ Branch 1 (3→5) taken 391 times.
|
664 | if (!strcmp(type, nat_etypes[i])) |
| 2299 | 273 | return i; | |
| 2300 | } | ||
| 2301 | ✗ | return -1; | |
| 2302 | } | ||
| 2303 | |||
| 2304 | 273 | static struct stmt *json_parse_nat_stmt(struct json_ctx *ctx, | |
| 2305 | const char *key, json_t *value) | ||
| 2306 | { | ||
| 2307 | int type, familyval, flags; | ||
| 2308 | struct stmt *stmt; | ||
| 2309 | json_t *tmp; | ||
| 2310 | |||
| 2311 | 273 | type = nat_type_parse(key); | |
| 2312 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 273 times.
|
273 | if (type < 0) { |
| 2313 | ✗ | json_error(ctx, "Unknown nat type '%s'.", key); | |
| 2314 | ✗ | return NULL; | |
| 2315 | } | ||
| 2316 | |||
| 2317 | 273 | familyval = json_parse_family(ctx, value); | |
| 2318 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 273 times.
|
273 | if (familyval < 0) |
| 2319 | ✗ | return NULL; | |
| 2320 | |||
| 2321 | 273 | stmt = nat_stmt_alloc(int_loc, type); | |
| 2322 | 273 | stmt->nat.family = familyval; | |
| 2323 | |||
| 2324 |
2/2✓ Branch 0 (11→12) taken 169 times.
✓ Branch 1 (11→17) taken 104 times.
|
273 | if (!json_unpack(value, "{s:o}", "addr", &tmp)) { |
| 2325 | 169 | stmt->nat.addr = json_parse_stmt_expr(ctx, tmp); | |
| 2326 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→17) taken 169 times.
|
169 | if (!stmt->nat.addr) { |
| 2327 | ✗ | json_error(ctx, "Invalid nat addr."); | |
| 2328 | ✗ | stmt_free(stmt); | |
| 2329 | ✗ | return NULL; | |
| 2330 | } | ||
| 2331 | } | ||
| 2332 |
2/2✓ Branch 0 (18→19) taken 55 times.
✓ Branch 1 (18→24) taken 218 times.
|
273 | if (!json_unpack(value, "{s:o}", "port", &tmp)) { |
| 2333 | 55 | stmt->nat.proto = json_parse_stmt_expr(ctx, tmp); | |
| 2334 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→24) taken 55 times.
|
55 | if (!stmt->nat.proto) { |
| 2335 | ✗ | json_error(ctx, "Invalid nat port."); | |
| 2336 | ✗ | stmt_free(stmt); | |
| 2337 | ✗ | return NULL; | |
| 2338 | } | ||
| 2339 | } | ||
| 2340 | 273 | flags = parse_flags_array(ctx, value, "flags", json_parse_nat_flag); | |
| 2341 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→28) taken 273 times.
|
273 | if (flags < 0) { |
| 2342 | ✗ | stmt_free(stmt); | |
| 2343 | ✗ | return NULL; | |
| 2344 | } | ||
| 2345 | 273 | stmt->nat.flags = flags; | |
| 2346 | |||
| 2347 | 273 | flags = parse_flags_array(ctx, value, "type_flags", | |
| 2348 | json_parse_nat_type_flag); | ||
| 2349 |
1/2✗ Branch 0 (29→30) not taken.
✓ Branch 1 (29→32) taken 273 times.
|
273 | if (flags < 0) { |
| 2350 | ✗ | stmt_free(stmt); | |
| 2351 | ✗ | return NULL; | |
| 2352 | } | ||
| 2353 | 273 | stmt->nat.type_flags = flags; | |
| 2354 | |||
| 2355 | 273 | return stmt; | |
| 2356 | } | ||
| 2357 | |||
| 2358 | 25 | static struct stmt *json_parse_tproxy_stmt(struct json_ctx *ctx, | |
| 2359 | const char *key, json_t *value) | ||
| 2360 | { | ||
| 2361 | json_t *jaddr, *tmp; | ||
| 2362 | struct stmt *stmt; | ||
| 2363 | int familyval; | ||
| 2364 | |||
| 2365 | 25 | stmt = tproxy_stmt_alloc(int_loc); | |
| 2366 | |||
| 2367 | 25 | familyval = json_parse_family(ctx, value); | |
| 2368 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 25 times.
|
25 | if (familyval < 0) |
| 2369 | ✗ | goto out_free; | |
| 2370 | |||
| 2371 | 25 | stmt->tproxy.family = familyval; | |
| 2372 | |||
| 2373 |
2/2✓ Branch 0 (7→8) taken 13 times.
✓ Branch 1 (7→12) taken 12 times.
|
25 | if (!json_unpack(value, "{s:o}", "addr", &jaddr)) { |
| 2374 | 13 | stmt->tproxy.addr = json_parse_stmt_expr(ctx, jaddr); | |
| 2375 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 13 times.
|
13 | if (!stmt->tproxy.addr) { |
| 2376 | ✗ | json_error(ctx, "Invalid addr."); | |
| 2377 | ✗ | goto out_free; | |
| 2378 | } | ||
| 2379 | } | ||
| 2380 |
2/2✓ Branch 0 (13→14) taken 19 times.
✓ Branch 1 (13→18) taken 6 times.
|
25 | if (!json_unpack(value, "{s:o}", "port", &tmp)) { |
| 2381 | 19 | stmt->tproxy.port = json_parse_stmt_expr(ctx, tmp); | |
| 2382 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→18) taken 19 times.
|
19 | if (!stmt->tproxy.port) { |
| 2383 | ✗ | json_error(ctx, "Invalid port."); | |
| 2384 | ✗ | goto out_free; | |
| 2385 | } | ||
| 2386 | } | ||
| 2387 | 25 | return stmt; | |
| 2388 | |||
| 2389 | ✗ | out_free: | |
| 2390 | ✗ | stmt_free(stmt); | |
| 2391 | ✗ | return NULL; | |
| 2392 | } | ||
| 2393 | |||
| 2394 | 124 | static struct stmt *json_parse_reject_stmt(struct json_ctx *ctx, | |
| 2395 | const char *key, json_t *value) | ||
| 2396 | { | ||
| 2397 | 124 | struct stmt *stmt = reject_stmt_alloc(int_loc); | |
| 2398 | 124 | const struct datatype *dtype = NULL; | |
| 2399 | const char *type; | ||
| 2400 | json_t *tmp; | ||
| 2401 | |||
| 2402 | 124 | stmt->reject.type = -1; | |
| 2403 | 124 | stmt->reject.icmp_code = -1; | |
| 2404 | |||
| 2405 |
2/2✓ Branch 0 (4→5) taken 112 times.
✓ Branch 1 (4→13) taken 12 times.
|
124 | if (!json_unpack(value, "{s:s}", "type", &type)) { |
| 2406 |
2/2✓ Branch 0 (5→6) taken 16 times.
✓ Branch 1 (5→7) taken 96 times.
|
112 | if (!strcmp(type, "tcp reset")) { |
| 2407 | 16 | stmt->reject.type = NFT_REJECT_TCP_RST; | |
| 2408 | 16 | stmt->reject.icmp_code = 0; | |
| 2409 |
2/2✓ Branch 0 (7→8) taken 30 times.
✓ Branch 1 (7→9) taken 66 times.
|
96 | } else if (!strcmp(type, "icmpx")) { |
| 2410 | 30 | stmt->reject.type = NFT_REJECT_ICMPX_UNREACH; | |
| 2411 | 30 | dtype = &reject_icmpx_code_type; | |
| 2412 | 30 | stmt->reject.icmp_code = 0; | |
| 2413 |
2/2✓ Branch 0 (9→10) taken 34 times.
✓ Branch 1 (9→11) taken 32 times.
|
66 | } else if (!strcmp(type, "icmp")) { |
| 2414 | 34 | stmt->reject.type = NFT_REJECT_ICMP_UNREACH; | |
| 2415 | 34 | stmt->reject.family = NFPROTO_IPV4; | |
| 2416 | 34 | dtype = &reject_icmp_code_type; | |
| 2417 | 34 | stmt->reject.icmp_code = 0; | |
| 2418 |
1/2✓ Branch 0 (11→12) taken 32 times.
✗ Branch 1 (11→13) not taken.
|
32 | } else if (!strcmp(type, "icmpv6")) { |
| 2419 | 32 | stmt->reject.type = NFT_REJECT_ICMP_UNREACH; | |
| 2420 | 32 | stmt->reject.family = NFPROTO_IPV6; | |
| 2421 | 32 | dtype = &reject_icmpv6_code_type; | |
| 2422 | 32 | stmt->reject.icmp_code = 0; | |
| 2423 | } | ||
| 2424 | } | ||
| 2425 |
2/2✓ Branch 0 (14→15) taken 95 times.
✓ Branch 1 (14→21) taken 29 times.
|
124 | if (!json_unpack(value, "{s:o}", "expr", &tmp)) { |
| 2426 | 95 | stmt->reject.expr = json_parse_immediate(ctx, tmp); | |
| 2427 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→20) taken 95 times.
|
95 | if (!stmt->reject.expr) { |
| 2428 | ✗ | json_error(ctx, "Illegal reject expr."); | |
| 2429 | ✗ | stmt_free(stmt); | |
| 2430 | ✗ | return NULL; | |
| 2431 | } | ||
| 2432 | 95 | datatype_set(stmt->reject.expr, dtype); | |
| 2433 | } | ||
| 2434 | 124 | return stmt; | |
| 2435 | } | ||
| 2436 | |||
| 2437 | 75 | static int json_parse_set_stmt_list(struct json_ctx *ctx, | |
| 2438 | struct list_head *stmt_list, | ||
| 2439 | json_t *stmt_json) | ||
| 2440 | { | ||
| 2441 | struct list_head *head; | ||
| 2442 | struct stmt *stmt; | ||
| 2443 | json_t *value; | ||
| 2444 | size_t index; | ||
| 2445 | |||
| 2446 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 75 times.
|
75 | if (!stmt_json) |
| 2447 | ✗ | return 0; | |
| 2448 | |||
| 2449 |
2/4✓ Branch 0 (4→5) taken 75 times.
✗ Branch 1 (4→6) not taken.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 75 times.
|
75 | if (!json_is_array(stmt_json)) { |
| 2450 | ✗ | json_error(ctx, "Unexpected object type in stmt"); | |
| 2451 | ✗ | return -1; | |
| 2452 | } | ||
| 2453 | |||
| 2454 | 75 | head = stmt_list; | |
| 2455 |
3/4✓ Branch 0 (22→23) taken 81 times.
✓ Branch 1 (22→25) taken 74 times.
✓ Branch 2 (24→9) taken 81 times.
✗ Branch 3 (24→25) not taken.
|
155 | json_array_foreach(stmt_json, index, value) { |
| 2456 | 81 | stmt = json_parse_stmt(ctx, value); | |
| 2457 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→14) taken 81 times.
|
81 | if (!stmt) { |
| 2458 | ✗ | json_error(ctx, "Parsing set statements array at index %zd failed.", index); | |
| 2459 | ✗ | stmt_list_free(stmt_list); | |
| 2460 | ✗ | return -1; | |
| 2461 | } | ||
| 2462 |
2/2✓ Branch 0 (14→15) taken 1 times.
✓ Branch 1 (14→19) taken 80 times.
|
81 | if (!(stmt->flags & STMT_F_STATEFUL)) { |
| 2463 | 1 | stmt_free(stmt); | |
| 2464 | 1 | json_error(ctx, "Unsupported set statements array at index %zd failed.", index); | |
| 2465 | 1 | stmt_list_free(stmt_list); | |
| 2466 | 1 | return -1; | |
| 2467 | } | ||
| 2468 | 80 | list_add(&stmt->list, head); | |
| 2469 | 80 | head = &stmt->list; | |
| 2470 | } | ||
| 2471 | |||
| 2472 | 74 | return 0; | |
| 2473 | } | ||
| 2474 | |||
| 2475 | 81 | static struct stmt *json_parse_set_stmt(struct json_ctx *ctx, | |
| 2476 | const char *key, json_t *value) | ||
| 2477 | { | ||
| 2478 | const char *opstr, *set; | ||
| 2479 | struct expr *expr, *expr2; | ||
| 2480 | json_t *elem, *stmt_json; | ||
| 2481 | struct stmt *stmt; | ||
| 2482 | int op; | ||
| 2483 | |||
| 2484 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 81 times.
|
81 | if (json_unpack_err(ctx, value, "{s:s, s:o, s:s}", |
| 2485 | "op", &opstr, "elem", &elem, "set", &set)) | ||
| 2486 | ✗ | return NULL; | |
| 2487 | |||
| 2488 |
2/2✓ Branch 0 (5→6) taken 43 times.
✓ Branch 1 (5→7) taken 38 times.
|
81 | if (!strcmp(opstr, "add")) { |
| 2489 | 43 | op = NFT_DYNSET_OP_ADD; | |
| 2490 |
2/2✓ Branch 0 (7→8) taken 34 times.
✓ Branch 1 (7→9) taken 4 times.
|
38 | } else if (!strcmp(opstr, "update")) { |
| 2491 | 34 | op = NFT_DYNSET_OP_UPDATE; | |
| 2492 |
1/2✓ Branch 0 (9→10) taken 4 times.
✗ Branch 1 (9→11) not taken.
|
4 | } else if (!strcmp(opstr, "delete")) { |
| 2493 | 4 | op = NFT_DYNSET_OP_DELETE; | |
| 2494 | } else { | ||
| 2495 | ✗ | json_error(ctx, "Unknown set statement op '%s'.", opstr); | |
| 2496 | ✗ | return NULL; | |
| 2497 | } | ||
| 2498 | |||
| 2499 | 81 | expr = json_parse_set_elem_expr_stmt(ctx, elem); | |
| 2500 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→17) taken 81 times.
|
81 | if (!expr) { |
| 2501 | ✗ | json_error(ctx, "Illegal set statement element."); | |
| 2502 | ✗ | return NULL; | |
| 2503 | } | ||
| 2504 | |||
| 2505 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→21) taken 81 times.
|
81 | if (set[0] != '@') { |
| 2506 | ✗ | json_error(ctx, "Illegal set reference in set statement."); | |
| 2507 | ✗ | expr_free(expr); | |
| 2508 | ✗ | return NULL; | |
| 2509 | } | ||
| 2510 | 81 | expr2 = symbol_expr_alloc(int_loc, SYMBOL_SET, NULL, set + 1); | |
| 2511 | |||
| 2512 | 81 | stmt = set_stmt_alloc(int_loc); | |
| 2513 | 81 | stmt->set.op = op; | |
| 2514 | 81 | stmt->set.key = expr; | |
| 2515 | 81 | stmt->set.set = expr2; | |
| 2516 | |||
| 2517 |
3/4✓ Branch 0 (24→25) taken 21 times.
✓ Branch 1 (24→29) taken 60 times.
✗ Branch 2 (26→27) not taken.
✓ Branch 3 (26→29) taken 21 times.
|
102 | if (!json_unpack(value, "{s:o}", "stmt", &stmt_json) && |
| 2518 | 21 | json_parse_set_stmt_list(ctx, &stmt->set.stmt_list, stmt_json) < 0) { | |
| 2519 | ✗ | stmt_free(stmt); | |
| 2520 | ✗ | return NULL; | |
| 2521 | } | ||
| 2522 | |||
| 2523 | 81 | return stmt; | |
| 2524 | } | ||
| 2525 | |||
| 2526 | 36 | static struct stmt *json_parse_map_stmt(struct json_ctx *ctx, | |
| 2527 | const char *key, json_t *value) | ||
| 2528 | { | ||
| 2529 | struct expr *expr, *expr2, *expr_data; | ||
| 2530 | json_t *elem, *data, *stmt_json; | ||
| 2531 | const char *opstr, *set; | ||
| 2532 | struct stmt *stmt; | ||
| 2533 | int op; | ||
| 2534 | |||
| 2535 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 36 times.
|
36 | if (json_unpack_err(ctx, value, "{s:s, s:o, s:o, s:s}", |
| 2536 | "op", &opstr, "elem", &elem, "data", &data, "map", &set)) | ||
| 2537 | ✗ | return NULL; | |
| 2538 | |||
| 2539 |
2/2✓ Branch 0 (5→6) taken 20 times.
✓ Branch 1 (5→7) taken 16 times.
|
36 | if (!strcmp(opstr, "add")) { |
| 2540 | 20 | op = NFT_DYNSET_OP_ADD; | |
| 2541 |
2/2✓ Branch 0 (7→8) taken 14 times.
✓ Branch 1 (7→9) taken 2 times.
|
16 | } else if (!strcmp(opstr, "update")) { |
| 2542 | 14 | op = NFT_DYNSET_OP_UPDATE; | |
| 2543 |
1/2✓ Branch 0 (9→10) taken 2 times.
✗ Branch 1 (9→11) not taken.
|
2 | } else if (!strcmp(opstr, "delete")) { |
| 2544 | 2 | op = NFT_DYNSET_OP_DELETE; | |
| 2545 | } else { | ||
| 2546 | ✗ | json_error(ctx, "Unknown map statement op '%s'.", opstr); | |
| 2547 | ✗ | return NULL; | |
| 2548 | } | ||
| 2549 | |||
| 2550 | 36 | expr = json_parse_set_elem_expr_stmt(ctx, elem); | |
| 2551 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→17) taken 36 times.
|
36 | if (!expr) { |
| 2552 | ✗ | json_error(ctx, "Illegal map statement element."); | |
| 2553 | ✗ | return NULL; | |
| 2554 | } | ||
| 2555 | |||
| 2556 | 36 | expr_data = json_parse_set_elem_expr_stmt(ctx, data); | |
| 2557 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→22) taken 36 times.
|
36 | if (!expr_data) { |
| 2558 | ✗ | json_error(ctx, "Illegal map expression data."); | |
| 2559 | ✗ | expr_free(expr); | |
| 2560 | ✗ | return NULL; | |
| 2561 | } | ||
| 2562 | |||
| 2563 |
1/2✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→27) taken 36 times.
|
36 | if (set[0] != '@') { |
| 2564 | ✗ | json_error(ctx, "Illegal map reference in map statement."); | |
| 2565 | ✗ | expr_free(expr); | |
| 2566 | ✗ | expr_free(expr_data); | |
| 2567 | ✗ | return NULL; | |
| 2568 | } | ||
| 2569 | 36 | expr2 = symbol_expr_alloc(int_loc, SYMBOL_SET, NULL, set + 1); | |
| 2570 | |||
| 2571 | 36 | stmt = map_stmt_alloc(int_loc); | |
| 2572 | 36 | stmt->map.op = op; | |
| 2573 | 36 | stmt->map.key = expr; | |
| 2574 | 36 | stmt->map.data = expr_data; | |
| 2575 | 36 | stmt->map.set = expr2; | |
| 2576 | |||
| 2577 |
1/4✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→35) taken 36 times.
✗ Branch 2 (32→33) not taken.
✗ Branch 3 (32→35) not taken.
|
36 | if (!json_unpack(value, "{s:o}", "stmt", &stmt_json) && |
| 2578 | ✗ | json_parse_set_stmt_list(ctx, &stmt->set.stmt_list, stmt_json) < 0) { | |
| 2579 | ✗ | stmt_free(stmt); | |
| 2580 | ✗ | return NULL; | |
| 2581 | } | ||
| 2582 | |||
| 2583 | 36 | return stmt; | |
| 2584 | } | ||
| 2585 | |||
| 2586 | 54 | static unsigned int json_parse_log_flag(const char *flag) | |
| 2587 | { | ||
| 2588 | const struct { | ||
| 2589 | const char *flag; | ||
| 2590 | int val; | ||
| 2591 | 54 | } flag_tbl[] = { | |
| 2592 | { "tcp sequence", NF_LOG_TCPSEQ }, | ||
| 2593 | { "tcp options", NF_LOG_TCPOPT }, | ||
| 2594 | { "ip options", NF_LOG_IPOPT }, | ||
| 2595 | { "skuid", NF_LOG_UID }, | ||
| 2596 | { "ether", NF_LOG_MACDECODE }, | ||
| 2597 | { "all", NF_LOG_MASK }, | ||
| 2598 | }; | ||
| 2599 | unsigned int i; | ||
| 2600 | |||
| 2601 |
1/2✓ Branch 0 (6→3) taken 160 times.
✗ Branch 1 (6→7) not taken.
|
160 | for (i = 0; i < array_size(flag_tbl); i++) { |
| 2602 |
2/2✓ Branch 0 (3→4) taken 54 times.
✓ Branch 1 (3→5) taken 106 times.
|
160 | if (!strcmp(flag, flag_tbl[i].flag)) |
| 2603 | 54 | return flag_tbl[i].val; | |
| 2604 | } | ||
| 2605 | ✗ | return 0; | |
| 2606 | } | ||
| 2607 | |||
| 2608 | 104 | static struct stmt *json_parse_log_stmt(struct json_ctx *ctx, | |
| 2609 | const char *key, json_t *value) | ||
| 2610 | { | ||
| 2611 | const char *tmpstr; | ||
| 2612 | struct stmt *stmt; | ||
| 2613 | int tmp, flags; | ||
| 2614 | |||
| 2615 | 104 | stmt = log_stmt_alloc(int_loc); | |
| 2616 | |||
| 2617 |
2/2✓ Branch 0 (4→5) taken 14 times.
✓ Branch 1 (4→7) taken 90 times.
|
104 | if (!json_unpack(value, "{s:s}", "prefix", &tmpstr)) { |
| 2618 | 14 | stmt->log.prefix = xstrdup(tmpstr); | |
| 2619 | 14 | stmt->log.flags |= STMT_LOG_PREFIX; | |
| 2620 | } | ||
| 2621 |
2/2✓ Branch 0 (8→9) taken 20 times.
✓ Branch 1 (8→10) taken 84 times.
|
104 | if (!json_unpack(value, "{s:i}", "group", &tmp)) { |
| 2622 | 20 | stmt->log.group = tmp; | |
| 2623 | 20 | stmt->log.flags |= STMT_LOG_GROUP; | |
| 2624 | } | ||
| 2625 |
2/2✓ Branch 0 (11→12) taken 10 times.
✓ Branch 1 (11→13) taken 94 times.
|
104 | if (!json_unpack(value, "{s:i}", "snaplen", &tmp)) { |
| 2626 | 10 | stmt->log.snaplen = tmp; | |
| 2627 | 10 | stmt->log.flags |= STMT_LOG_SNAPLEN; | |
| 2628 | } | ||
| 2629 |
2/2✓ Branch 0 (14→15) taken 5 times.
✓ Branch 1 (14→16) taken 99 times.
|
104 | if (!json_unpack(value, "{s:i}", "queue-threshold", &tmp)) { |
| 2630 | 5 | stmt->log.qthreshold = tmp; | |
| 2631 | 5 | stmt->log.flags |= STMT_LOG_QTHRESHOLD; | |
| 2632 | } | ||
| 2633 |
2/2✓ Branch 0 (17→18) taken 50 times.
✓ Branch 1 (17→24) taken 54 times.
|
104 | if (!json_unpack(value, "{s:s}", "level", &tmpstr)) { |
| 2634 | 50 | int level = log_level_parse(tmpstr); | |
| 2635 | |||
| 2636 |
1/2✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→23) taken 50 times.
|
50 | if (level < 0) { |
| 2637 | ✗ | json_error(ctx, "Invalid log level '%s'.", tmpstr); | |
| 2638 | ✗ | stmt_free(stmt); | |
| 2639 | ✗ | return NULL; | |
| 2640 | } | ||
| 2641 | 50 | stmt->log.level = level; | |
| 2642 | 50 | stmt->log.flags |= STMT_LOG_LEVEL; | |
| 2643 | } | ||
| 2644 | 104 | flags = parse_flags_array(ctx, value, "flags", json_parse_log_flag); | |
| 2645 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→28) taken 104 times.
|
104 | if (flags < 0) { |
| 2646 | ✗ | stmt_free(stmt); | |
| 2647 | ✗ | return NULL; | |
| 2648 | } | ||
| 2649 | 104 | stmt->log.logflags = flags; | |
| 2650 | |||
| 2651 | 104 | return stmt; | |
| 2652 | } | ||
| 2653 | |||
| 2654 | 30 | static unsigned int json_parse_synproxy_flag(const char *flag) | |
| 2655 | { | ||
| 2656 | const struct { | ||
| 2657 | const char *flag; | ||
| 2658 | int val; | ||
| 2659 | 30 | } flag_tbl[] = { | |
| 2660 | { "timestamp", NF_SYNPROXY_OPT_TIMESTAMP }, | ||
| 2661 | { "sack-perm", NF_SYNPROXY_OPT_SACK_PERM }, | ||
| 2662 | }; | ||
| 2663 | unsigned int i; | ||
| 2664 | |||
| 2665 |
1/2✓ Branch 0 (6→3) taken 47 times.
✗ Branch 1 (6→7) not taken.
|
47 | for (i = 0; i < array_size(flag_tbl); i++) { |
| 2666 |
2/2✓ Branch 0 (3→4) taken 30 times.
✓ Branch 1 (3→5) taken 17 times.
|
47 | if (!strcmp(flag, flag_tbl[i].flag)) |
| 2667 | 30 | return flag_tbl[i].val; | |
| 2668 | } | ||
| 2669 | ✗ | return 0; | |
| 2670 | } | ||
| 2671 | |||
| 2672 | 24 | static struct stmt *json_parse_synproxy_stmt(struct json_ctx *ctx, | |
| 2673 | const char *key, json_t *value) | ||
| 2674 | { | ||
| 2675 | 24 | struct stmt *stmt = NULL; | |
| 2676 | int tmp, flags; | ||
| 2677 | |||
| 2678 |
2/2✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→5) taken 21 times.
|
24 | if (json_typeof(value) == JSON_NULL) { |
| 2679 | 3 | stmt = synproxy_stmt_alloc(int_loc); | |
| 2680 | 3 | return stmt; | |
| 2681 | } | ||
| 2682 | |||
| 2683 |
2/2✓ Branch 0 (6→7) taken 6 times.
✓ Branch 1 (6→14) taken 15 times.
|
21 | if (!json_unpack(value, "{s:i}", "mss", &tmp)) { |
| 2684 |
1/2✓ Branch 0 (7→8) taken 6 times.
✗ Branch 1 (7→9) not taken.
|
6 | if (!stmt) |
| 2685 | 6 | stmt = synproxy_stmt_alloc(int_loc); | |
| 2686 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→13) taken 6 times.
|
6 | if (tmp < 0) { |
| 2687 | ✗ | json_error(ctx, "Invalid synproxy mss value '%d'", tmp); | |
| 2688 | ✗ | stmt_free(stmt); | |
| 2689 | ✗ | return NULL; | |
| 2690 | } | ||
| 2691 | 6 | stmt->synproxy.mss = tmp; | |
| 2692 | 6 | stmt->synproxy.flags |= NF_SYNPROXY_OPT_MSS; | |
| 2693 | } | ||
| 2694 |
2/2✓ Branch 0 (15→16) taken 6 times.
✓ Branch 1 (15→23) taken 15 times.
|
21 | if (!json_unpack(value, "{s:i}", "wscale", &tmp)) { |
| 2695 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 6 times.
|
6 | if (!stmt) |
| 2696 | ✗ | stmt = synproxy_stmt_alloc(int_loc); | |
| 2697 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→22) taken 6 times.
|
6 | if (tmp < 0) { |
| 2698 | ✗ | json_error(ctx, "Invalid synproxy wscale value '%d'", tmp); | |
| 2699 | ✗ | stmt_free(stmt); | |
| 2700 | ✗ | return NULL; | |
| 2701 | } | ||
| 2702 | 6 | stmt->synproxy.wscale = tmp; | |
| 2703 | 6 | stmt->synproxy.flags |= NF_SYNPROXY_OPT_WSCALE; | |
| 2704 | } | ||
| 2705 | |||
| 2706 | 21 | flags = parse_flags_array(ctx, value, "flags", | |
| 2707 | json_parse_synproxy_flag); | ||
| 2708 |
1/2✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→27) taken 21 times.
|
21 | if (flags < 0) { |
| 2709 | ✗ | stmt_free(stmt); | |
| 2710 | ✗ | return NULL; | |
| 2711 | } | ||
| 2712 |
2/2✓ Branch 0 (27→28) taken 15 times.
✓ Branch 1 (27→31) taken 6 times.
|
21 | if (flags) { |
| 2713 |
2/2✓ Branch 0 (28→29) taken 12 times.
✓ Branch 1 (28→30) taken 3 times.
|
15 | if (!stmt) |
| 2714 | 12 | stmt = synproxy_stmt_alloc(int_loc); | |
| 2715 | 15 | stmt->synproxy.flags |= flags; | |
| 2716 | } | ||
| 2717 | |||
| 2718 |
2/2✓ Branch 0 (31→32) taken 3 times.
✓ Branch 1 (31→38) taken 18 times.
|
21 | if (!stmt) { |
| 2719 | 3 | stmt = objref_stmt_alloc(int_loc); | |
| 2720 | 3 | stmt->objref.type = NFT_OBJECT_SYNPROXY; | |
| 2721 | 3 | stmt->objref.expr = json_parse_stmt_expr(ctx, value); | |
| 2722 |
1/2✗ Branch 0 (34→35) not taken.
✓ Branch 1 (34→38) taken 3 times.
|
3 | if (!stmt->objref.expr) { |
| 2723 | ✗ | json_error(ctx, "Invalid synproxy reference"); | |
| 2724 | ✗ | stmt_free(stmt); | |
| 2725 | ✗ | return NULL; | |
| 2726 | } | ||
| 2727 | } | ||
| 2728 | 21 | return stmt; | |
| 2729 | } | ||
| 2730 | |||
| 2731 | 8 | static struct stmt *json_parse_cthelper_stmt(struct json_ctx *ctx, | |
| 2732 | const char *key, json_t *value) | ||
| 2733 | { | ||
| 2734 | 8 | struct stmt *stmt = objref_stmt_alloc(int_loc); | |
| 2735 | |||
| 2736 | 8 | stmt->objref.type = NFT_OBJECT_CT_HELPER; | |
| 2737 | 8 | stmt->objref.expr = json_parse_stmt_expr(ctx, value); | |
| 2738 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→8) taken 8 times.
|
8 | if (!stmt->objref.expr) { |
| 2739 | ✗ | json_error(ctx, "Invalid ct helper reference."); | |
| 2740 | ✗ | stmt_free(stmt); | |
| 2741 | ✗ | return NULL; | |
| 2742 | } | ||
| 2743 | 8 | return stmt; | |
| 2744 | } | ||
| 2745 | |||
| 2746 | 13 | static struct stmt *json_parse_cttimeout_stmt(struct json_ctx *ctx, | |
| 2747 | const char *key, json_t *value) | ||
| 2748 | { | ||
| 2749 | 13 | struct stmt *stmt = objref_stmt_alloc(int_loc); | |
| 2750 | |||
| 2751 | 13 | stmt->objref.type = NFT_OBJECT_CT_TIMEOUT; | |
| 2752 | 13 | stmt->objref.expr = json_parse_stmt_expr(ctx, value); | |
| 2753 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→8) taken 13 times.
|
13 | if (!stmt->objref.expr) { |
| 2754 | ✗ | json_error(ctx, "Invalid ct timeout reference."); | |
| 2755 | ✗ | stmt_free(stmt); | |
| 2756 | ✗ | return NULL; | |
| 2757 | } | ||
| 2758 | 13 | return stmt; | |
| 2759 | } | ||
| 2760 | |||
| 2761 | 11 | static struct stmt *json_parse_ctexpect_stmt(struct json_ctx *ctx, | |
| 2762 | const char *key, json_t *value) | ||
| 2763 | { | ||
| 2764 | 11 | struct stmt *stmt = objref_stmt_alloc(int_loc); | |
| 2765 | |||
| 2766 | 11 | stmt->objref.type = NFT_OBJECT_CT_EXPECT; | |
| 2767 | 11 | stmt->objref.expr = json_parse_stmt_expr(ctx, value); | |
| 2768 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→8) taken 11 times.
|
11 | if (!stmt->objref.expr) { |
| 2769 | ✗ | json_error(ctx, "Invalid ct expectation reference."); | |
| 2770 | ✗ | stmt_free(stmt); | |
| 2771 | ✗ | return NULL; | |
| 2772 | } | ||
| 2773 | 11 | return stmt; | |
| 2774 | } | ||
| 2775 | |||
| 2776 | ✗ | static struct stmt *json_parse_meter_stmt(struct json_ctx *ctx, | |
| 2777 | const char *key, json_t *value) | ||
| 2778 | { | ||
| 2779 | json_t *jkey, *jstmt; | ||
| 2780 | struct stmt *stmt; | ||
| 2781 | const char *name; | ||
| 2782 | ✗ | uint32_t size = 0; | |
| 2783 | |||
| 2784 | ✗ | if (json_unpack_err(ctx, value, "{s:s, s:o, s:o}", | |
| 2785 | "name", &name, "key", &jkey, "stmt", &jstmt)) | ||
| 2786 | ✗ | return NULL; | |
| 2787 | ✗ | json_unpack(value, "{s:i}", "size", &size); | |
| 2788 | |||
| 2789 | ✗ | stmt = meter_stmt_alloc(int_loc); | |
| 2790 | ✗ | stmt->meter.name = xstrdup(name); | |
| 2791 | ✗ | stmt->meter.size = size; | |
| 2792 | |||
| 2793 | ✗ | stmt->meter.key = json_parse_set_elem_expr_stmt(ctx, jkey); | |
| 2794 | ✗ | if (!stmt->meter.key) { | |
| 2795 | ✗ | json_error(ctx, "Invalid meter key."); | |
| 2796 | ✗ | stmt_free(stmt); | |
| 2797 | ✗ | return NULL; | |
| 2798 | } | ||
| 2799 | |||
| 2800 | ✗ | stmt->meter.stmt = json_parse_stmt(ctx, jstmt); | |
| 2801 | ✗ | if (!stmt->meter.stmt) { | |
| 2802 | ✗ | json_error(ctx, "Invalid meter statement."); | |
| 2803 | ✗ | stmt_free(stmt); | |
| 2804 | ✗ | return NULL; | |
| 2805 | } | ||
| 2806 | ✗ | return stmt; | |
| 2807 | } | ||
| 2808 | |||
| 2809 | 56 | static unsigned int queue_flag_parse(const char *name) | |
| 2810 | { | ||
| 2811 |
2/2✓ Branch 0 (2→3) taken 39 times.
✓ Branch 1 (2→4) taken 17 times.
|
56 | if (!strcmp(name, "bypass")) |
| 2812 | 39 | return NFT_QUEUE_FLAG_BYPASS; | |
| 2813 |
1/2✓ Branch 0 (4→5) taken 17 times.
✗ Branch 1 (4→6) not taken.
|
17 | else if (!strcmp(name, "fanout")) |
| 2814 | 17 | return NFT_QUEUE_FLAG_CPU_FANOUT; | |
| 2815 | ✗ | return 0; | |
| 2816 | } | ||
| 2817 | |||
| 2818 | 87 | static struct stmt *json_parse_queue_stmt(struct json_ctx *ctx, | |
| 2819 | const char *key, json_t *value) | ||
| 2820 | { | ||
| 2821 | 87 | struct expr *qexpr = NULL; | |
| 2822 | json_t *tmp; | ||
| 2823 | int flags; | ||
| 2824 | |||
| 2825 |
2/2✓ Branch 0 (3→4) taken 83 times.
✓ Branch 1 (3→8) taken 4 times.
|
87 | if (!json_unpack(value, "{s:o}", "num", &tmp)) { |
| 2826 | 83 | qexpr = json_parse_stmt_expr(ctx, tmp); | |
| 2827 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 83 times.
|
83 | if (!qexpr) { |
| 2828 | ✗ | json_error(ctx, "Invalid queue num."); | |
| 2829 | ✗ | return NULL; | |
| 2830 | } | ||
| 2831 | } | ||
| 2832 | |||
| 2833 | 87 | flags = parse_flags_array(ctx, value, "flags", queue_flag_parse); | |
| 2834 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 87 times.
|
87 | if (flags < 0) { |
| 2835 | ✗ | expr_free(qexpr); | |
| 2836 | ✗ | return NULL; | |
| 2837 | } | ||
| 2838 | |||
| 2839 | 87 | return queue_stmt_alloc(int_loc, qexpr, flags); | |
| 2840 | } | ||
| 2841 | |||
| 2842 | 10 | static struct stmt *json_parse_connlimit_stmt(struct json_ctx *ctx, | |
| 2843 | const char *key, json_t *value) | ||
| 2844 | { | ||
| 2845 | 10 | struct stmt *stmt = connlimit_stmt_alloc(int_loc); | |
| 2846 | |||
| 2847 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 10 times.
|
10 | if (json_unpack_err(ctx, value, "{s:i}", |
| 2848 | "val", &stmt->connlimit.count)) { | ||
| 2849 | ✗ | stmt_free(stmt); | |
| 2850 | ✗ | return NULL; | |
| 2851 | } | ||
| 2852 | |||
| 2853 | 10 | json_unpack(value, "{s:b}", "inv", &stmt->connlimit.flags); | |
| 2854 |
2/2✓ Branch 0 (8→9) taken 7 times.
✓ Branch 1 (8→10) taken 3 times.
|
10 | if (stmt->connlimit.flags) |
| 2855 | 7 | stmt->connlimit.flags = NFT_CONNLIMIT_F_INV; | |
| 2856 | |||
| 2857 | 10 | return stmt; | |
| 2858 | } | ||
| 2859 | |||
| 2860 | 11 | static struct stmt *json_parse_optstrip_stmt(struct json_ctx *ctx, | |
| 2861 | const char *key, json_t *value) | ||
| 2862 | { | ||
| 2863 | 11 | struct expr *expr = json_parse_expr(ctx, value); | |
| 2864 | |||
| 2865 |
1/2✓ Branch 0 (3→4) taken 11 times.
✗ Branch 1 (3→6) not taken.
|
11 | if (!expr || |
| 2866 |
1/2✓ Branch 0 (4→5) taken 11 times.
✗ Branch 1 (4→6) not taken.
|
11 | expr->etype != EXPR_EXTHDR || |
| 2867 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→9) taken 11 times.
|
11 | expr->exthdr.op != NFT_EXTHDR_OP_TCPOPT) { |
| 2868 | ✗ | json_error(ctx, "Illegal TCP optstrip argument"); | |
| 2869 | ✗ | expr_free(expr); | |
| 2870 | ✗ | return NULL; | |
| 2871 | } | ||
| 2872 | |||
| 2873 | 11 | return optstrip_stmt_alloc(int_loc, expr); | |
| 2874 | } | ||
| 2875 | |||
| 2876 | 53646 | static struct stmt *json_parse_stmt(struct json_ctx *ctx, json_t *root) | |
| 2877 | { | ||
| 2878 | struct { | ||
| 2879 | const char *key; | ||
| 2880 | struct stmt *(*cb)(struct json_ctx *, const char *, json_t *); | ||
| 2881 | 53646 | } stmt_parser_tbl[] = { | |
| 2882 | { "accept", json_parse_verdict_stmt }, | ||
| 2883 | { "drop", json_parse_verdict_stmt }, | ||
| 2884 | { "continue", json_parse_verdict_stmt }, | ||
| 2885 | { "jump", json_parse_verdict_stmt }, | ||
| 2886 | { "goto", json_parse_verdict_stmt }, | ||
| 2887 | { "return", json_parse_verdict_stmt }, | ||
| 2888 | { "match", json_parse_match_stmt }, | ||
| 2889 | { "counter", json_parse_counter_stmt }, | ||
| 2890 | { "mangle", json_parse_mangle_stmt }, | ||
| 2891 | { "quota", json_parse_quota_stmt }, | ||
| 2892 | { "last", json_parse_last_stmt }, | ||
| 2893 | { "limit", json_parse_limit_stmt }, | ||
| 2894 | { "flow", json_parse_flow_offload_stmt }, | ||
| 2895 | { "fwd", json_parse_fwd_stmt }, | ||
| 2896 | { "notrack", json_parse_notrack_stmt }, | ||
| 2897 | { "dup", json_parse_dup_stmt }, | ||
| 2898 | { "snat", json_parse_nat_stmt }, | ||
| 2899 | { "dnat", json_parse_nat_stmt }, | ||
| 2900 | { "masquerade", json_parse_nat_stmt }, | ||
| 2901 | { "redirect", json_parse_nat_stmt }, | ||
| 2902 | { "reject", json_parse_reject_stmt }, | ||
| 2903 | { "set", json_parse_set_stmt }, | ||
| 2904 | { "map", json_parse_map_stmt }, | ||
| 2905 | { "log", json_parse_log_stmt }, | ||
| 2906 | { "ct helper", json_parse_cthelper_stmt }, | ||
| 2907 | { "ct timeout", json_parse_cttimeout_stmt }, | ||
| 2908 | { "ct expectation", json_parse_ctexpect_stmt }, | ||
| 2909 | { "meter", json_parse_meter_stmt }, | ||
| 2910 | { "queue", json_parse_queue_stmt }, | ||
| 2911 | { "ct count", json_parse_connlimit_stmt }, | ||
| 2912 | { "tproxy", json_parse_tproxy_stmt }, | ||
| 2913 | { "synproxy", json_parse_synproxy_stmt }, | ||
| 2914 | { "reset", json_parse_optstrip_stmt }, | ||
| 2915 | { "secmark", json_parse_secmark_stmt }, | ||
| 2916 | { "tunnel", json_parse_tunnel_stmt }, | ||
| 2917 | }; | ||
| 2918 | const char *type; | ||
| 2919 | unsigned int i; | ||
| 2920 | json_t *tmp; | ||
| 2921 | |||
| 2922 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 53646 times.
|
53646 | if (json_unpack_stmt(ctx, root, &type, &tmp)) |
| 2923 | ✗ | return NULL; | |
| 2924 | |||
| 2925 | /* Yes, verdict_map_stmt is actually an expression */ | ||
| 2926 |
2/2✓ Branch 0 (5→6) taken 318 times.
✓ Branch 1 (5→12) taken 53328 times.
|
53646 | if (!strcmp(type, "vmap")) { |
| 2927 | 318 | struct expr *expr = json_parse_map_expr(ctx, type, tmp); | |
| 2928 | |||
| 2929 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 318 times.
|
318 | if (!expr) { |
| 2930 | ✗ | json_error(ctx, "Illegal vmap statement."); | |
| 2931 | ✗ | return NULL; | |
| 2932 | } | ||
| 2933 | 318 | return verdict_stmt_alloc(int_loc, expr); | |
| 2934 | } | ||
| 2935 | |||
| 2936 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→15) taken 53328 times.
|
53328 | if (!strcmp(type, "xt")) { |
| 2937 | ✗ | json_error(ctx, "unsupported xtables compat expression, use iptables-nft with this ruleset"); | |
| 2938 | ✗ | return NULL; | |
| 2939 | } | ||
| 2940 | |||
| 2941 |
1/2✓ Branch 0 (20→16) taken 361988 times.
✗ Branch 1 (20→21) not taken.
|
361988 | for (i = 0; i < array_size(stmt_parser_tbl); i++) { |
| 2942 |
2/2✓ Branch 0 (16→17) taken 53328 times.
✓ Branch 1 (16→19) taken 308660 times.
|
361988 | if (!strcmp(type, stmt_parser_tbl[i].key)) |
| 2943 | 53328 | return stmt_parser_tbl[i].cb(ctx, stmt_parser_tbl[i].key, tmp); | |
| 2944 | } | ||
| 2945 | |||
| 2946 | ✗ | json_error(ctx, "Unknown statement object '%s'.", type); | |
| 2947 | ✗ | return NULL; | |
| 2948 | } | ||
| 2949 | |||
| 2950 | 967 | static struct cmd *json_parse_cmd_add_table(struct json_ctx *ctx, json_t *root, | |
| 2951 | enum cmd_ops op, enum cmd_obj obj) | ||
| 2952 | { | ||
| 2953 | 967 | const char *family = "", *comment = NULL; | |
| 2954 | 967 | struct handle h = { | |
| 2955 | 967 | .table.location = *int_loc, | |
| 2956 | }; | ||
| 2957 | 967 | struct table *table = NULL; | |
| 2958 | 967 | int flags = 0; | |
| 2959 | |||
| 2960 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 967 times.
|
967 | if (json_unpack_err(ctx, root, "{s:s}", |
| 2961 | "family", &family)) | ||
| 2962 | ✗ | return NULL; | |
| 2963 | |||
| 2964 |
2/2✓ Branch 0 (5→6) taken 966 times.
✓ Branch 1 (5→13) taken 1 times.
|
967 | if (op != CMD_DELETE) { |
| 2965 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 966 times.
|
966 | if (json_unpack_err(ctx, root, "{s:s}", "name", &h.table.name)) |
| 2966 | ✗ | return NULL; | |
| 2967 | |||
| 2968 | 966 | json_unpack(root, "{s:s}", "comment", &comment); | |
| 2969 | |||
| 2970 | 966 | flags = parse_flags_array(ctx, root, "flags", parse_table_flag); | |
| 2971 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→20) taken 966 times.
|
966 | if (flags < 0) |
| 2972 | ✗ | return NULL; | |
| 2973 |
2/4✓ Branch 0 (13→14) taken 1 times.
✗ Branch 1 (13→20) not taken.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→20) taken 1 times.
|
2 | } else if (op == CMD_DELETE && |
| 2974 |
0/2✗ Branch 0 (17→18) not taken.
✗ Branch 1 (17→20) not taken.
|
1 | json_unpack(root, "{s:s}", "name", &h.table.name) && |
| 2975 | ✗ | json_unpack(root, "{s:I}", "handle", &h.handle.id)) { | |
| 2976 | ✗ | json_error(ctx, "Either name or handle required to delete a table."); | |
| 2977 | ✗ | return NULL; | |
| 2978 | } | ||
| 2979 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→24) taken 967 times.
|
967 | if (parse_family(family, &h.family)) { |
| 2980 | ✗ | json_error(ctx, "Unknown family '%s'.", family); | |
| 2981 | ✗ | return NULL; | |
| 2982 | } | ||
| 2983 |
1/2✓ Branch 0 (24→25) taken 967 times.
✗ Branch 1 (24→27) not taken.
|
967 | if (h.table.name) |
| 2984 | 967 | h.table.name = xstrdup(h.table.name); | |
| 2985 | |||
| 2986 |
4/4✓ Branch 0 (27→28) taken 965 times.
✓ Branch 1 (27→29) taken 2 times.
✓ Branch 2 (28→29) taken 10 times.
✓ Branch 3 (28→35) taken 955 times.
|
967 | if (comment || flags) { |
| 2987 | 12 | table = table_alloc(); | |
| 2988 | 12 | handle_merge(&table->handle, &h); | |
| 2989 |
2/2✓ Branch 0 (31→32) taken 2 times.
✓ Branch 1 (31→34) taken 10 times.
|
12 | if (comment) |
| 2990 | 2 | table->comment = xstrdup(comment); | |
| 2991 | 12 | table->flags = flags; | |
| 2992 | } | ||
| 2993 | |||
| 2994 |
2/2✓ Branch 0 (35→36) taken 964 times.
✓ Branch 1 (35→37) taken 3 times.
|
967 | if (op == CMD_ADD) |
| 2995 | 964 | json_object_del(root, "handle"); | |
| 2996 | |||
| 2997 | 967 | return cmd_alloc(op, obj, &h, int_loc, table); | |
| 2998 | } | ||
| 2999 | |||
| 3000 | 1099 | static struct expr *parse_policy(const char *policy) | |
| 3001 | { | ||
| 3002 | int policy_num; | ||
| 3003 | |||
| 3004 |
2/2✓ Branch 0 (2→3) taken 1073 times.
✓ Branch 1 (2→4) taken 26 times.
|
1099 | if (!strcmp(policy, "accept")) |
| 3005 | 1073 | policy_num = NF_ACCEPT; | |
| 3006 |
1/2✓ Branch 0 (4→5) taken 26 times.
✗ Branch 1 (4→6) not taken.
|
26 | else if (!strcmp(policy, "drop")) |
| 3007 | 26 | policy_num = NF_DROP; | |
| 3008 | else | ||
| 3009 | ✗ | return NULL; | |
| 3010 | |||
| 3011 | 1099 | return constant_expr_alloc(int_loc, &integer_type, | |
| 3012 | BYTEORDER_HOST_ENDIAN, | ||
| 3013 | sizeof(int) * BITS_PER_BYTE, &policy_num); | ||
| 3014 | } | ||
| 3015 | |||
| 3016 | 84 | static struct expr *ifname_expr_alloc(struct json_ctx *ctx, | |
| 3017 | const char *name) | ||
| 3018 | { | ||
| 3019 | 84 | size_t length = strlen(name); | |
| 3020 | |||
| 3021 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→5) taken 83 times.
|
84 | if (length == 0) { |
| 3022 | 1 | json_error(ctx, "empty interface name"); | |
| 3023 | 1 | return NULL; | |
| 3024 | } | ||
| 3025 | |||
| 3026 |
2/2✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→8) taken 82 times.
|
83 | if (length >= IFNAMSIZ) { |
| 3027 | 1 | json_error(ctx, "Device name %s too long", name); | |
| 3028 | 1 | return NULL; | |
| 3029 | } | ||
| 3030 | |||
| 3031 | 82 | return constant_expr_alloc(int_loc, &ifname_type, BYTEORDER_HOST_ENDIAN, | |
| 3032 | length * BITS_PER_BYTE, name); | ||
| 3033 | } | ||
| 3034 | |||
| 3035 | 72 | static struct expr *json_parse_devs(struct json_ctx *ctx, json_t *root) | |
| 3036 | { | ||
| 3037 | 72 | struct expr *tmp, *expr = list_expr_alloc(int_loc); | |
| 3038 | const char *dev; | ||
| 3039 | json_t *value; | ||
| 3040 | size_t index; | ||
| 3041 | |||
| 3042 |
2/2✓ Branch 0 (4→5) taken 61 times.
✓ Branch 1 (4→11) taken 11 times.
|
72 | if (!json_unpack(root, "s", &dev)) { |
| 3043 | 61 | tmp = ifname_expr_alloc(ctx, dev); | |
| 3044 |
2/2✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→9) taken 60 times.
|
61 | if (!tmp) { |
| 3045 | 1 | expr_free(expr); | |
| 3046 | 1 | return NULL; | |
| 3047 | } | ||
| 3048 | |||
| 3049 | 60 | list_expr_add(expr, tmp); | |
| 3050 | 60 | return expr; | |
| 3051 | } | ||
| 3052 |
2/4✓ Branch 0 (11→12) taken 11 times.
✗ Branch 1 (11→13) not taken.
✗ Branch 2 (12→13) not taken.
✓ Branch 3 (12→15) taken 11 times.
|
11 | if (!json_is_array(root)) { |
| 3053 | ✗ | expr_free(expr); | |
| 3054 | ✗ | return NULL; | |
| 3055 | } | ||
| 3056 | |||
| 3057 |
3/4✓ Branch 0 (28→29) taken 23 times.
✓ Branch 1 (28→31) taken 10 times.
✓ Branch 2 (30→16) taken 23 times.
✗ Branch 3 (30→31) not taken.
|
33 | json_array_foreach(root, index, value) { |
| 3058 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→21) taken 23 times.
|
23 | if (json_unpack(value, "s", &dev)) { |
| 3059 | ✗ | json_error(ctx, "Invalid device at index %zu.", | |
| 3060 | index); | ||
| 3061 | ✗ | expr_free(expr); | |
| 3062 | ✗ | return NULL; | |
| 3063 | } | ||
| 3064 | |||
| 3065 | 23 | tmp = ifname_expr_alloc(ctx, dev); | |
| 3066 |
2/2✓ Branch 0 (22→23) taken 1 times.
✓ Branch 1 (22→25) taken 22 times.
|
23 | if (!tmp) { |
| 3067 | 1 | expr_free(expr); | |
| 3068 | 1 | return NULL; | |
| 3069 | } | ||
| 3070 | 22 | list_expr_add(expr, tmp); | |
| 3071 | } | ||
| 3072 | 10 | return expr; | |
| 3073 | } | ||
| 3074 | |||
| 3075 | 2545 | static struct cmd *json_parse_cmd_add_chain(struct json_ctx *ctx, json_t *root, | |
| 3076 | enum cmd_ops op, enum cmd_obj obj) | ||
| 3077 | { | ||
| 3078 | 2545 | struct handle h = { | |
| 3079 | 2545 | .table.location = *int_loc, | |
| 3080 | }; | ||
| 3081 | 2545 | const char *family = "", *policy = "", *type, *hookstr, *comment = NULL; | |
| 3082 | 2545 | struct chain *chain = NULL; | |
| 3083 | 2545 | json_t *devs = NULL; | |
| 3084 | int prio; | ||
| 3085 | |||
| 3086 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2545 times.
|
2545 | if (json_unpack_err(ctx, root, "{s:s, s:s}", |
| 3087 | "family", &family, | ||
| 3088 | "table", &h.table.name)) | ||
| 3089 | ✗ | return NULL; | |
| 3090 |
2/2✓ Branch 0 (5→6) taken 2544 times.
✓ Branch 1 (5→10) taken 1 times.
|
2545 | if (op != CMD_DELETE) { |
| 3091 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 2544 times.
|
2544 | if (json_unpack_err(ctx, root, "{s:s}", "name", &h.chain.name)) |
| 3092 | ✗ | return NULL; | |
| 3093 | |||
| 3094 | 2544 | json_unpack(root, "{s:s}", "comment", &comment); | |
| 3095 |
2/4✓ Branch 0 (10→11) taken 1 times.
✗ Branch 1 (10→17) not taken.
✗ Branch 2 (12→13) not taken.
✓ Branch 3 (12→17) taken 1 times.
|
2 | } else if (op == CMD_DELETE && |
| 3096 |
0/2✗ Branch 0 (14→15) not taken.
✗ Branch 1 (14→17) not taken.
|
1 | json_unpack(root, "{s:s}", "name", &h.chain.name) && |
| 3097 | ✗ | json_unpack(root, "{s:I}", "handle", &h.handle.id)) { | |
| 3098 | ✗ | json_error(ctx, "Either name or handle required to delete a chain."); | |
| 3099 | ✗ | return NULL; | |
| 3100 | } | ||
| 3101 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→21) taken 2545 times.
|
2545 | if (parse_family(family, &h.family)) { |
| 3102 | ✗ | json_error(ctx, "Unknown family '%s'.", family); | |
| 3103 | ✗ | return NULL; | |
| 3104 | } | ||
| 3105 | 2545 | h.table.name = xstrdup(h.table.name); | |
| 3106 |
1/2✓ Branch 0 (22→23) taken 2545 times.
✗ Branch 1 (22→25) not taken.
|
2545 | if (h.chain.name) |
| 3107 | 2545 | h.chain.name = xstrdup(h.chain.name); | |
| 3108 | |||
| 3109 |
2/2✓ Branch 0 (25→26) taken 2 times.
✓ Branch 1 (25→30) taken 2543 times.
|
2545 | if (comment) { |
| 3110 | 2 | chain = chain_alloc(); | |
| 3111 | 2 | handle_merge(&chain->handle, &h); | |
| 3112 | 2 | chain->comment = xstrdup(comment); | |
| 3113 | } | ||
| 3114 | |||
| 3115 |
3/4✓ Branch 0 (30→31) taken 2544 times.
✓ Branch 1 (30→35) taken 1 times.
✓ Branch 2 (31→32) taken 2544 times.
✗ Branch 3 (31→35) not taken.
|
2545 | if (op == CMD_DELETE || |
| 3116 |
1/2✓ Branch 0 (32→33) taken 2544 times.
✗ Branch 1 (32→35) not taken.
|
2544 | op == CMD_LIST || |
| 3117 |
2/2✓ Branch 0 (34→35) taken 1434 times.
✓ Branch 1 (34→37) taken 1110 times.
|
2544 | op == CMD_FLUSH || |
| 3118 | 2544 | json_unpack(root, "{s:s, s:s, s:i}", | |
| 3119 | "type", &type, "hook", &hookstr, "prio", &prio)) | ||
| 3120 | 1435 | return cmd_alloc(op, obj, &h, int_loc, chain); | |
| 3121 | |||
| 3122 |
1/2✓ Branch 0 (37→38) taken 1110 times.
✗ Branch 1 (37→39) not taken.
|
1110 | if (!chain) |
| 3123 | 1110 | chain = chain_alloc(); | |
| 3124 | |||
| 3125 | 1110 | chain->flags |= CHAIN_F_BASECHAIN; | |
| 3126 | 1110 | chain->type.str = xstrdup(type); | |
| 3127 | 1110 | chain->priority.expr = constant_expr_alloc(int_loc, &integer_type, | |
| 3128 | BYTEORDER_HOST_ENDIAN, | ||
| 3129 | sizeof(int) * BITS_PER_BYTE, | ||
| 3130 | &prio); | ||
| 3131 | 1110 | chain->hook.name = chain_hookname_lookup(hookstr); | |
| 3132 |
1/2✗ Branch 0 (42→43) not taken.
✓ Branch 1 (42→45) taken 1110 times.
|
1110 | if (!chain->hook.name) { |
| 3133 | ✗ | json_error(ctx, "Invalid chain hook '%s'.", hookstr); | |
| 3134 | ✗ | goto err_free_chain; | |
| 3135 | } | ||
| 3136 | |||
| 3137 | 1110 | json_unpack(root, "{s:o}", "dev", &devs); | |
| 3138 | |||
| 3139 |
2/2✓ Branch 0 (46→47) taken 46 times.
✓ Branch 1 (46→51) taken 1064 times.
|
1110 | if (devs) { |
| 3140 | 46 | chain->dev_expr = json_parse_devs(ctx, devs); | |
| 3141 |
2/2✓ Branch 0 (48→49) taken 2 times.
✓ Branch 1 (48→51) taken 44 times.
|
46 | if (!chain->dev_expr) { |
| 3142 | 2 | json_error(ctx, "Invalid chain dev."); | |
| 3143 | 2 | goto err_free_chain; | |
| 3144 | } | ||
| 3145 | } | ||
| 3146 | |||
| 3147 |
2/2✓ Branch 0 (52→53) taken 1099 times.
✓ Branch 1 (52→57) taken 9 times.
|
1108 | if (!json_unpack(root, "{s:s}", "policy", &policy)) { |
| 3148 | 1099 | chain->policy = parse_policy(policy); | |
| 3149 |
1/2✗ Branch 0 (54→55) not taken.
✓ Branch 1 (54→57) taken 1099 times.
|
1099 | if (!chain->policy) { |
| 3150 | ✗ | json_error(ctx, "Unknown policy '%s'.", policy); | |
| 3151 | ✗ | goto err_free_chain; | |
| 3152 | } | ||
| 3153 | } | ||
| 3154 | |||
| 3155 |
1/2✓ Branch 0 (57→58) taken 1108 times.
✗ Branch 1 (57→59) not taken.
|
1108 | if (op == CMD_ADD) |
| 3156 | 1108 | json_object_del(root, "handle"); | |
| 3157 | |||
| 3158 | 1108 | handle_merge(&chain->handle, &h); | |
| 3159 | 1108 | return cmd_alloc(op, obj, &h, int_loc, chain); | |
| 3160 | |||
| 3161 | 2 | err_free_chain: | |
| 3162 | 2 | chain_free(chain); | |
| 3163 | 2 | handle_free(&h); | |
| 3164 | 2 | return NULL; | |
| 3165 | } | ||
| 3166 | |||
| 3167 | 50955 | static struct cmd *json_parse_cmd_add_rule(struct json_ctx *ctx, json_t *root, | |
| 3168 | enum cmd_ops op, enum cmd_obj obj) | ||
| 3169 | { | ||
| 3170 | 50955 | struct handle h = { | |
| 3171 | 50955 | .table.location = *int_loc, | |
| 3172 | 50955 | .chain.location = *int_loc, | |
| 3173 | 50955 | .index.location = *int_loc, | |
| 3174 | }; | ||
| 3175 | 50955 | const char *family = "", *comment = NULL; | |
| 3176 | struct rule *rule; | ||
| 3177 | size_t index; | ||
| 3178 | json_t *tmp, *value; | ||
| 3179 | |||
| 3180 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 50955 times.
|
50955 | if (json_unpack_err(ctx, root, "{s:s, s:s, s:s}", |
| 3181 | "family", &family, | ||
| 3182 | "table", &h.table.name, | ||
| 3183 | "chain", &h.chain.name)) | ||
| 3184 | ✗ | return NULL; | |
| 3185 |
3/4✓ Branch 0 (5→6) taken 50954 times.
✓ Branch 1 (5→9) taken 1 times.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→9) taken 50954 times.
|
101909 | if (op != CMD_DELETE && |
| 3186 | 50954 | json_unpack_err(ctx, root, "{s:o}", "expr", &tmp)) | |
| 3187 | ✗ | return NULL; | |
| 3188 |
4/6✓ Branch 0 (9→10) taken 50954 times.
✓ Branch 1 (9→11) taken 1 times.
✗ Branch 2 (10→11) not taken.
✓ Branch 3 (10→14) taken 50954 times.
✗ Branch 4 (12→13) not taken.
✓ Branch 5 (12→14) taken 1 times.
|
50956 | else if ((op == CMD_DELETE || op == CMD_DESTROY) && |
| 3189 | 1 | json_unpack_err(ctx, root, "{s:I}", "handle", &h.handle.id)) | |
| 3190 | ✗ | return NULL; | |
| 3191 | |||
| 3192 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→18) taken 50955 times.
|
50955 | if (parse_family(family, &h.family)) { |
| 3193 | ✗ | json_error(ctx, "Unknown family '%s'.", family); | |
| 3194 | ✗ | return NULL; | |
| 3195 | } | ||
| 3196 | 50955 | h.table.name = xstrdup(h.table.name); | |
| 3197 | 50955 | h.chain.name = xstrdup(h.chain.name); | |
| 3198 | |||
| 3199 |
3/4✓ Branch 0 (20→21) taken 50954 times.
✓ Branch 1 (20→22) taken 1 times.
✗ Branch 2 (21→22) not taken.
✓ Branch 3 (21→24) taken 50954 times.
|
50955 | if (op == CMD_DELETE || op == CMD_DESTROY) |
| 3200 | 1 | return cmd_alloc(op, obj, &h, int_loc, NULL); | |
| 3201 | |||
| 3202 |
2/4✓ Branch 0 (24→25) taken 50954 times.
✗ Branch 1 (24→26) not taken.
✗ Branch 2 (25→26) not taken.
✓ Branch 3 (25→29) taken 50954 times.
|
50954 | if (!json_is_array(tmp)) { |
| 3203 | ✗ | json_error(ctx, "Value of property \"expr\" must be an array."); | |
| 3204 | ✗ | handle_free(&h); | |
| 3205 | ✗ | return NULL; | |
| 3206 | } | ||
| 3207 | |||
| 3208 |
2/2✓ Branch 0 (30→31) taken 6 times.
✓ Branch 1 (30→32) taken 50948 times.
|
50954 | if (!json_unpack(root, "{s:I}", "index", &h.index.id)) { |
| 3209 | 6 | h.index.id++; | |
| 3210 | } | ||
| 3211 | |||
| 3212 | /* For explicit add/insert/create commands, handle is used for positioning. | ||
| 3213 | * Convert handle to position for proper rule placement. | ||
| 3214 | * Skip this for implicit adds (export/import format). | ||
| 3215 | */ | ||
| 3216 |
3/4✓ Branch 0 (32→33) taken 9022 times.
✓ Branch 1 (32→39) taken 41932 times.
✓ Branch 2 (33→34) taken 9022 times.
✗ Branch 3 (33→36) not taken.
|
50954 | if (!(ctx->flags & CTX_F_IMPLICIT) && |
| 3217 |
3/6✗ Branch 0 (34→35) not taken.
✓ Branch 1 (34→36) taken 9022 times.
✗ Branch 2 (35→36) not taken.
✗ Branch 3 (35→39) not taken.
✓ Branch 4 (37→38) taken 4 times.
✓ Branch 5 (37→39) taken 9018 times.
|
18044 | (op == CMD_INSERT || op == CMD_ADD || op == CMD_CREATE) && |
| 3218 | 9022 | !json_unpack(root, "{s:I}", "handle", &h.handle.id)) { | |
| 3219 | 4 | h.position.id = h.handle.id; | |
| 3220 | 4 | h.handle.id = 0; | |
| 3221 | } | ||
| 3222 | |||
| 3223 | 50954 | rule = rule_alloc(int_loc, NULL); | |
| 3224 | |||
| 3225 | 50954 | json_unpack(root, "{s:s}", "comment", &comment); | |
| 3226 |
2/2✓ Branch 0 (41→42) taken 3030 times.
✓ Branch 1 (41→44) taken 47924 times.
|
50954 | if (comment) |
| 3227 | 3030 | rule->comment = xstrdup(comment); | |
| 3228 | |||
| 3229 |
3/4✓ Branch 0 (57→58) taken 53528 times.
✓ Branch 1 (57→60) taken 50941 times.
✓ Branch 2 (59→45) taken 53528 times.
✗ Branch 3 (59→60) not taken.
|
104469 | json_array_foreach(tmp, index, value) { |
| 3230 | struct stmt *stmt; | ||
| 3231 | |||
| 3232 |
3/4✓ Branch 0 (45→46) taken 53528 times.
✗ Branch 1 (45→47) not taken.
✓ Branch 2 (46→47) taken 10 times.
✓ Branch 3 (46→50) taken 53518 times.
|
53528 | if (!json_is_object(value)) { |
| 3233 | 10 | json_error(ctx, "Unexpected expr array element of type %s, expected object.", | |
| 3234 | json_typename(value)); | ||
| 3235 | 10 | goto err_free_rule; | |
| 3236 | } | ||
| 3237 | |||
| 3238 | 53518 | stmt = json_parse_stmt(ctx, value); | |
| 3239 | |||
| 3240 |
2/2✓ Branch 0 (51→52) taken 3 times.
✓ Branch 1 (51→54) taken 53515 times.
|
53518 | if (!stmt) { |
| 3241 | 3 | json_error(ctx, "Parsing expr array at index %zd failed.", index); | |
| 3242 | 3 | goto err_free_rule; | |
| 3243 | } | ||
| 3244 | |||
| 3245 | 53515 | rule_stmt_append(rule, stmt); | |
| 3246 | } | ||
| 3247 | |||
| 3248 |
1/2✓ Branch 0 (60→61) taken 50941 times.
✗ Branch 1 (60→62) not taken.
|
50941 | if (op == CMD_ADD) |
| 3249 | 50941 | json_object_del(root, "handle"); | |
| 3250 | |||
| 3251 | 50941 | return cmd_alloc(op, obj, &h, int_loc, rule); | |
| 3252 | |||
| 3253 | 13 | err_free_rule: | |
| 3254 | 13 | rule_free(rule); | |
| 3255 | 13 | handle_free(&h); | |
| 3256 | 13 | return NULL; | |
| 3257 | } | ||
| 3258 | |||
| 3259 | 172 | static int string_to_nft_object(const char *str) | |
| 3260 | { | ||
| 3261 | 172 | const char *obj_tbl[__NFT_OBJECT_MAX] = { | |
| 3262 | [NFT_OBJECT_COUNTER] = "counter", | ||
| 3263 | [NFT_OBJECT_QUOTA] = "quota", | ||
| 3264 | [NFT_OBJECT_CT_HELPER] = "ct helper", | ||
| 3265 | [NFT_OBJECT_LIMIT] = "limit", | ||
| 3266 | [NFT_OBJECT_CT_TIMEOUT] = "ct timeout", | ||
| 3267 | [NFT_OBJECT_SECMARK] = "secmark", | ||
| 3268 | [NFT_OBJECT_CT_EXPECT] = "ct expectation", | ||
| 3269 | [NFT_OBJECT_SYNPROXY] = "synproxy", | ||
| 3270 | [NFT_OBJECT_TUNNEL] = "tunnel", | ||
| 3271 | }; | ||
| 3272 | unsigned int i; | ||
| 3273 | |||
| 3274 |
2/2✓ Branch 0 (7→3) taken 1722 times.
✓ Branch 1 (7→8) taken 144 times.
|
1866 | for (i = 0; i <= NFT_OBJECT_MAX; i++) { |
| 3275 |
4/4✓ Branch 0 (3→4) taken 1398 times.
✓ Branch 1 (3→6) taken 324 times.
✓ Branch 2 (4→5) taken 28 times.
✓ Branch 3 (4→6) taken 1370 times.
|
1722 | if (obj_tbl[i] && !strcmp(str, obj_tbl[i])) |
| 3276 | 28 | return i; | |
| 3277 | } | ||
| 3278 | 144 | return 0; | |
| 3279 | } | ||
| 3280 | |||
| 3281 | 383 | static unsigned int string_to_set_flag(const char *str) | |
| 3282 | { | ||
| 3283 | const struct { | ||
| 3284 | enum nft_set_flags val; | ||
| 3285 | const char *name; | ||
| 3286 | 383 | } flag_tbl[] = { | |
| 3287 | { NFT_SET_CONSTANT, "constant" }, | ||
| 3288 | { NFT_SET_INTERVAL, "interval" }, | ||
| 3289 | { NFT_SET_TIMEOUT, "timeout" }, | ||
| 3290 | { NFT_SET_EVAL, "dynamic" }, | ||
| 3291 | }; | ||
| 3292 | unsigned int i; | ||
| 3293 | |||
| 3294 |
1/2✓ Branch 0 (6→3) taken 1027 times.
✗ Branch 1 (6→7) not taken.
|
1027 | for (i = 0; i < array_size(flag_tbl); i++) { |
| 3295 |
2/2✓ Branch 0 (3→4) taken 383 times.
✓ Branch 1 (3→5) taken 644 times.
|
1027 | if (!strcmp(str, flag_tbl[i].name)) |
| 3296 | 383 | return flag_tbl[i].val; | |
| 3297 | } | ||
| 3298 | ✗ | return 0; | |
| 3299 | } | ||
| 3300 | |||
| 3301 | 632 | static struct cmd *json_parse_cmd_add_set(struct json_ctx *ctx, json_t *root, | |
| 3302 | enum cmd_ops op, enum cmd_obj obj) | ||
| 3303 | { | ||
| 3304 | 632 | struct handle h = { 0 }; | |
| 3305 | 632 | const char *family = "", *policy; | |
| 3306 | json_t *tmp, *stmt_json; | ||
| 3307 | struct set *set; | ||
| 3308 | int flags; | ||
| 3309 | |||
| 3310 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 632 times.
|
632 | if (json_unpack_err(ctx, root, "{s:s, s:s}", |
| 3311 | "family", &family, | ||
| 3312 | "table", &h.table.name)) | ||
| 3313 | ✗ | return NULL; | |
| 3314 |
3/4✓ Branch 0 (5→6) taken 631 times.
✓ Branch 1 (5→9) taken 1 times.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→9) taken 631 times.
|
1263 | if (op != CMD_DELETE && |
| 3315 | 631 | json_unpack_err(ctx, root, "{s:s}", "name", &h.set.name)) { | |
| 3316 | ✗ | return NULL; | |
| 3317 |
4/6✓ Branch 0 (9→10) taken 631 times.
✓ Branch 1 (9→11) taken 1 times.
✗ Branch 2 (10→11) not taken.
✓ Branch 3 (10→17) taken 631 times.
✗ Branch 4 (12→13) not taken.
✓ Branch 5 (12→17) taken 1 times.
|
633 | } else if ((op == CMD_DELETE || op == CMD_DESTROY) && |
| 3318 |
0/2✗ Branch 0 (14→15) not taken.
✗ Branch 1 (14→17) not taken.
|
1 | json_unpack(root, "{s:s}", "name", &h.set.name) && |
| 3319 | ✗ | json_unpack(root, "{s:I}", "handle", &h.handle.id)) { | |
| 3320 | ✗ | json_error(ctx, "Either name or handle required to delete a set."); | |
| 3321 | ✗ | return NULL; | |
| 3322 | } | ||
| 3323 | |||
| 3324 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→21) taken 632 times.
|
632 | if (parse_family(family, &h.family)) { |
| 3325 | ✗ | json_error(ctx, "Unknown family '%s'.", family); | |
| 3326 | ✗ | return NULL; | |
| 3327 | } | ||
| 3328 | 632 | h.table.name = xstrdup(h.table.name); | |
| 3329 |
1/2✓ Branch 0 (22→23) taken 632 times.
✗ Branch 1 (22→25) not taken.
|
632 | if (h.set.name) |
| 3330 | 632 | h.set.name = xstrdup(h.set.name); | |
| 3331 | |||
| 3332 |
2/2✓ Branch 0 (25→26) taken 1 times.
✓ Branch 1 (25→27) taken 631 times.
|
632 | switch (op) { |
| 3333 | 1 | case CMD_DELETE: | |
| 3334 | case CMD_DESTROY: | ||
| 3335 | case CMD_LIST: | ||
| 3336 | case CMD_FLUSH: | ||
| 3337 | case CMD_RESET: | ||
| 3338 | 1 | return cmd_alloc(op, obj, &h, int_loc, NULL); | |
| 3339 | 631 | default: | |
| 3340 | 631 | break; | |
| 3341 | } | ||
| 3342 | |||
| 3343 | 631 | set = set_alloc(&internal_location); | |
| 3344 | |||
| 3345 |
1/2✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→35) taken 631 times.
|
631 | if (json_unpack(root, "{s:o}", "type", &tmp)) { |
| 3346 | ✗ | json_error(ctx, "Invalid set type."); | |
| 3347 | ✗ | set_free(set); | |
| 3348 | ✗ | handle_free(&h); | |
| 3349 | ✗ | return NULL; | |
| 3350 | } | ||
| 3351 | 631 | set->key = json_parse_dtype_expr(ctx, tmp); | |
| 3352 |
2/2✓ Branch 0 (36→37) taken 2 times.
✓ Branch 1 (36→41) taken 629 times.
|
631 | if (!set->key) { |
| 3353 | 2 | json_error(ctx, "Invalid set type."); | |
| 3354 | 2 | set_free(set); | |
| 3355 | 2 | handle_free(&h); | |
| 3356 | 2 | return NULL; | |
| 3357 | } | ||
| 3358 | |||
| 3359 |
2/2✓ Branch 0 (42→43) taken 208 times.
✓ Branch 1 (42→58) taken 421 times.
|
629 | if (!json_unpack(root, "{s:o}", "map", &tmp)) { |
| 3360 |
3/4✓ Branch 0 (43→44) taken 208 times.
✗ Branch 1 (43→48) not taken.
✓ Branch 2 (44→45) taken 172 times.
✓ Branch 3 (44→48) taken 36 times.
|
208 | if (json_is_string(tmp)) { |
| 3361 | 172 | const char *s = json_string_value(tmp); | |
| 3362 | |||
| 3363 | 172 | set->objtype = string_to_nft_object(s); | |
| 3364 | } | ||
| 3365 |
2/2✓ Branch 0 (48→49) taken 28 times.
✓ Branch 1 (48→50) taken 180 times.
|
208 | if (set->objtype) { |
| 3366 | 28 | set->flags |= NFT_SET_OBJECT; | |
| 3367 |
2/2✓ Branch 0 (51→52) taken 178 times.
✓ Branch 1 (51→53) taken 2 times.
|
180 | } else if ((set->data = json_parse_dtype_expr(ctx, tmp))) { |
| 3368 | 178 | set->flags |= NFT_SET_MAP; | |
| 3369 | } else { | ||
| 3370 | 2 | const char *dump = json_dumps(tmp, 0); | |
| 3371 | |||
| 3372 | 2 | json_error(ctx, "Invalid map type '%s'.", dump); | |
| 3373 | 2 | free_const(dump); | |
| 3374 | 2 | set_free(set); | |
| 3375 | 2 | handle_free(&h); | |
| 3376 | 2 | return NULL; | |
| 3377 | } | ||
| 3378 | } | ||
| 3379 |
1/2✗ Branch 0 (59→60) not taken.
✓ Branch 1 (59→68) taken 627 times.
|
627 | if (!json_unpack(root, "{s:s}", "policy", &policy)) { |
| 3380 | ✗ | if (!strcmp(policy, "performance")) | |
| 3381 | ✗ | set->policy = NFT_SET_POL_PERFORMANCE; | |
| 3382 | ✗ | else if (!strcmp(policy, "memory")) { | |
| 3383 | ✗ | set->policy = NFT_SET_POL_MEMORY; | |
| 3384 | } else { | ||
| 3385 | ✗ | json_error(ctx, "Unknown set policy '%s'.", policy); | |
| 3386 | ✗ | set_free(set); | |
| 3387 | ✗ | handle_free(&h); | |
| 3388 | ✗ | return NULL; | |
| 3389 | } | ||
| 3390 | } | ||
| 3391 | |||
| 3392 | 627 | flags = parse_flags_array(ctx, root, "flags", string_to_set_flag); | |
| 3393 |
1/2✗ Branch 0 (69→70) not taken.
✓ Branch 1 (69→74) taken 627 times.
|
627 | if (flags < 0) { |
| 3394 | ✗ | json_error(ctx, "Invalid set flags in set '%s'.", h.set.name); | |
| 3395 | ✗ | set_free(set); | |
| 3396 | ✗ | handle_free(&h); | |
| 3397 | ✗ | return NULL; | |
| 3398 | } | ||
| 3399 | 627 | set->flags |= flags; | |
| 3400 | |||
| 3401 |
2/2✓ Branch 0 (75→76) taken 383 times.
✓ Branch 1 (75→82) taken 244 times.
|
627 | if (!json_unpack(root, "{s:o}", "elem", &tmp)) { |
| 3402 | 383 | set->init = json_parse_set_expr(ctx, "elem", tmp); | |
| 3403 |
2/2✓ Branch 0 (77→78) taken 2 times.
✓ Branch 1 (77→82) taken 381 times.
|
383 | if (!set->init) { |
| 3404 | 2 | json_error(ctx, "Invalid set elem expression."); | |
| 3405 | 2 | set_free(set); | |
| 3406 | 2 | handle_free(&h); | |
| 3407 | 2 | return NULL; | |
| 3408 | } | ||
| 3409 | } | ||
| 3410 |
2/2✓ Branch 0 (83→84) taken 40 times.
✓ Branch 1 (83→85) taken 585 times.
|
625 | if (!json_unpack(root, "{s:I}", "timeout", &set->timeout)) |
| 3411 | 40 | set->timeout *= 1000; | |
| 3412 |
2/2✓ Branch 0 (86→87) taken 8 times.
✓ Branch 1 (86→88) taken 617 times.
|
625 | if (!json_unpack(root, "{s:i}", "gc-interval", &set->gc_int)) |
| 3413 | 8 | set->gc_int *= 1000; | |
| 3414 | 625 | json_unpack(root, "{s:i}", "size", &set->desc.size); | |
| 3415 | 625 | json_unpack(root, "{s:b}", "auto-merge", &set->automerge); | |
| 3416 | |||
| 3417 |
4/4✓ Branch 0 (91→92) taken 54 times.
✓ Branch 1 (91→97) taken 571 times.
✓ Branch 2 (93→94) taken 1 times.
✓ Branch 3 (93→97) taken 53 times.
|
679 | if (!json_unpack(root, "{s:o}", "stmt", &stmt_json) && |
| 3418 | 54 | json_parse_set_stmt_list(ctx, &set->stmt_list, stmt_json) < 0) { | |
| 3419 | 1 | set_free(set); | |
| 3420 | 1 | handle_free(&h); | |
| 3421 | 1 | return NULL; | |
| 3422 | } | ||
| 3423 | |||
| 3424 | 624 | handle_merge(&set->handle, &h); | |
| 3425 | |||
| 3426 |
1/2✓ Branch 0 (98→99) taken 624 times.
✗ Branch 1 (98→100) not taken.
|
624 | if (op == CMD_ADD) |
| 3427 | 624 | json_object_del(root, "handle"); | |
| 3428 | |||
| 3429 | 624 | return cmd_alloc(op, obj, &h, int_loc, set); | |
| 3430 | } | ||
| 3431 | |||
| 3432 | ✗ | static struct cmd *json_parse_cmd_add_element(struct json_ctx *ctx, | |
| 3433 | json_t *root, enum cmd_ops op, | ||
| 3434 | enum cmd_obj cmd_obj) | ||
| 3435 | { | ||
| 3436 | ✗ | struct handle h = { 0 }; | |
| 3437 | const char *family; | ||
| 3438 | struct expr *expr; | ||
| 3439 | json_t *tmp; | ||
| 3440 | |||
| 3441 | ✗ | if (json_unpack_err(ctx, root, "{s:s, s:s, s:s, s:o}", | |
| 3442 | "family", &family, | ||
| 3443 | "table", &h.table.name, | ||
| 3444 | "name", &h.set.name, | ||
| 3445 | "elem", &tmp)) | ||
| 3446 | ✗ | return NULL; | |
| 3447 | |||
| 3448 | ✗ | if (parse_family(family, &h.family)) { | |
| 3449 | ✗ | json_error(ctx, "Unknown family '%s'.", family); | |
| 3450 | ✗ | return NULL; | |
| 3451 | } | ||
| 3452 | ✗ | h.table.name = xstrdup(h.table.name); | |
| 3453 | ✗ | h.set.name = xstrdup(h.set.name); | |
| 3454 | |||
| 3455 | ✗ | expr = json_parse_set_expr(ctx, "elem", tmp); | |
| 3456 | ✗ | if (!expr) { | |
| 3457 | ✗ | json_error(ctx, "Invalid set."); | |
| 3458 | ✗ | handle_free(&h); | |
| 3459 | ✗ | return NULL; | |
| 3460 | } | ||
| 3461 | |||
| 3462 | ✗ | if ((op == CMD_CREATE || op == CMD_ADD) && | |
| 3463 | ✗ | nft_cmd_collapse_elems(op, ctx->cmds, &h, expr)) { | |
| 3464 | ✗ | handle_free(&h); | |
| 3465 | ✗ | expr_free(expr); | |
| 3466 | ✗ | ctx->flags |= CTX_F_COLLAPSED; | |
| 3467 | ✗ | return NULL; | |
| 3468 | } | ||
| 3469 | |||
| 3470 | ✗ | return cmd_alloc(op, cmd_obj, &h, int_loc, expr); | |
| 3471 | } | ||
| 3472 | |||
| 3473 | 30 | static struct cmd *json_parse_cmd_add_flowtable(struct json_ctx *ctx, | |
| 3474 | json_t *root, enum cmd_ops op, | ||
| 3475 | enum cmd_obj cmd_obj) | ||
| 3476 | { | ||
| 3477 | const char *family, *hook, *hookstr; | ||
| 3478 | struct flowtable *flowtable; | ||
| 3479 | 30 | struct handle h = { 0 }; | |
| 3480 | 30 | json_t *devs = NULL; | |
| 3481 | int prio; | ||
| 3482 | |||
| 3483 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 30 times.
|
30 | if (json_unpack_err(ctx, root, "{s:s, s:s}", |
| 3484 | "family", &family, | ||
| 3485 | "table", &h.table.name)) | ||
| 3486 | ✗ | return NULL; | |
| 3487 | |||
| 3488 |
2/4✓ Branch 0 (5→6) taken 30 times.
✗ Branch 1 (5→9) not taken.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→9) taken 30 times.
|
60 | if (op != CMD_DELETE && |
| 3489 | 30 | json_unpack_err(ctx, root, "{s:s}", "name", &h.flowtable.name)) { | |
| 3490 | ✗ | return NULL; | |
| 3491 |
2/6✓ Branch 0 (9→10) taken 30 times.
✗ Branch 1 (9→11) not taken.
✗ Branch 2 (10→11) not taken.
✓ Branch 3 (10→17) taken 30 times.
✗ Branch 4 (12→13) not taken.
✗ Branch 5 (12→17) not taken.
|
30 | } else if ((op == CMD_DELETE || op == CMD_DESTROY) && |
| 3492 | ✗ | json_unpack(root, "{s:s}", "name", &h.flowtable.name) && | |
| 3493 | ✗ | json_unpack(root, "{s:I}", "handle", &h.handle.id)) { | |
| 3494 | ✗ | json_error(ctx, "Either name or handle required to delete a flowtable."); | |
| 3495 | ✗ | return NULL; | |
| 3496 | } | ||
| 3497 | |||
| 3498 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→21) taken 30 times.
|
30 | if (parse_family(family, &h.family)) { |
| 3499 | ✗ | json_error(ctx, "Unknown family '%s'.", family); | |
| 3500 | ✗ | return NULL; | |
| 3501 | } | ||
| 3502 | 30 | h.table.name = xstrdup(h.table.name); | |
| 3503 |
1/2✓ Branch 0 (22→23) taken 30 times.
✗ Branch 1 (22→25) not taken.
|
30 | if (h.flowtable.name) |
| 3504 | 30 | h.flowtable.name = xstrdup(h.flowtable.name); | |
| 3505 | |||
| 3506 |
3/6✓ Branch 0 (25→26) taken 30 times.
✗ Branch 1 (25→28) not taken.
✓ Branch 2 (26→27) taken 30 times.
✗ Branch 3 (26→28) not taken.
✗ Branch 4 (27→28) not taken.
✓ Branch 5 (27→30) taken 30 times.
|
30 | if (op == CMD_DELETE || op == CMD_LIST || op == CMD_DESTROY) |
| 3507 | ✗ | return cmd_alloc(op, cmd_obj, &h, int_loc, NULL); | |
| 3508 | |||
| 3509 |
1/2✗ Branch 0 (31→32) not taken.
✓ Branch 1 (31→34) taken 30 times.
|
30 | if (json_unpack_err(ctx, root, "{s:s, s:i}", |
| 3510 | "hook", &hook, | ||
| 3511 | "prio", &prio)) { | ||
| 3512 | ✗ | handle_free(&h); | |
| 3513 | ✗ | return NULL; | |
| 3514 | } | ||
| 3515 | |||
| 3516 | 30 | json_unpack(root, "{s:o}", "dev", &devs); | |
| 3517 | |||
| 3518 | 30 | hookstr = chain_hookname_lookup(hook); | |
| 3519 |
1/2✗ Branch 0 (36→37) not taken.
✓ Branch 1 (36→40) taken 30 times.
|
30 | if (!hookstr) { |
| 3520 | ✗ | json_error(ctx, "Invalid flowtable hook '%s'.", hook); | |
| 3521 | ✗ | handle_free(&h); | |
| 3522 | ✗ | return NULL; | |
| 3523 | } | ||
| 3524 | |||
| 3525 | 30 | flowtable = flowtable_alloc(int_loc); | |
| 3526 | 30 | flowtable->hook.name = hookstr; | |
| 3527 | 30 | flowtable->priority.expr = | |
| 3528 | 30 | constant_expr_alloc(int_loc, &integer_type, | |
| 3529 | BYTEORDER_HOST_ENDIAN, | ||
| 3530 | sizeof(int) * BITS_PER_BYTE, &prio); | ||
| 3531 | |||
| 3532 |
2/2✓ Branch 0 (42→43) taken 26 times.
✓ Branch 1 (42→49) taken 4 times.
|
30 | if (devs) { |
| 3533 | 26 | flowtable->dev_expr = json_parse_devs(ctx, devs); | |
| 3534 |
1/2✗ Branch 0 (44→45) not taken.
✓ Branch 1 (44→49) taken 26 times.
|
26 | if (!flowtable->dev_expr) { |
| 3535 | ✗ | json_error(ctx, "Invalid flowtable dev."); | |
| 3536 | ✗ | flowtable_free(flowtable); | |
| 3537 | ✗ | handle_free(&h); | |
| 3538 | ✗ | return NULL; | |
| 3539 | } | ||
| 3540 | } | ||
| 3541 | 30 | return cmd_alloc(op, cmd_obj, &h, int_loc, flowtable); | |
| 3542 | } | ||
| 3543 | |||
| 3544 | 6 | static int json_parse_ct_timeout_policy(struct json_ctx *ctx, | |
| 3545 | json_t *root, struct obj *obj) | ||
| 3546 | { | ||
| 3547 | json_t *tmp, *val; | ||
| 3548 | const char *key; | ||
| 3549 | |||
| 3550 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 6 times.
|
6 | if (json_unpack(root, "{s:o}", "policy", &tmp)) |
| 3551 | ✗ | return 0; | |
| 3552 | |||
| 3553 |
2/4✓ Branch 0 (5→6) taken 6 times.
✗ Branch 1 (5→7) not taken.
✗ Branch 2 (6→7) not taken.
✓ Branch 3 (6→9) taken 6 times.
|
6 | if (!json_is_object(tmp)) { |
| 3554 | ✗ | json_error(ctx, "Invalid ct timeout policy."); | |
| 3555 | ✗ | return 1; | |
| 3556 | } | ||
| 3557 | |||
| 3558 |
3/4✓ Branch 0 (23→24) taken 12 times.
✓ Branch 1 (23→27) taken 6 times.
✓ Branch 2 (26→11) taken 12 times.
✗ Branch 3 (26→27) not taken.
|
18 | json_object_foreach(tmp, key, val) { |
| 3559 | struct timeout_state *ts; | ||
| 3560 | |||
| 3561 |
2/4✓ Branch 0 (11→12) taken 12 times.
✗ Branch 1 (11→13) not taken.
✗ Branch 2 (12→13) not taken.
✓ Branch 3 (12→15) taken 12 times.
|
12 | if (!json_is_integer(val)) { |
| 3562 | ✗ | json_error(ctx, "Invalid ct timeout policy value for '%s'.", key); | |
| 3563 | ✗ | return 1; | |
| 3564 | } | ||
| 3565 | |||
| 3566 | 12 | ts = xzalloc(sizeof(*ts)); | |
| 3567 | 12 | ts->timeout_str = xstrdup(key); | |
| 3568 | 12 | ts->timeout_value = json_integer_value(val); | |
| 3569 | 12 | ts->location = *int_loc; | |
| 3570 | 12 | init_list_head(&ts->head); | |
| 3571 | 12 | list_add_tail(&ts->head, &obj->ct_timeout.timeout_list); | |
| 3572 | } | ||
| 3573 | 6 | return 0; | |
| 3574 | } | ||
| 3575 | |||
| 3576 | 4 | static int json_parse_tunnel_erspan(struct json_ctx *ctx, | |
| 3577 | json_t *root, struct obj *obj) | ||
| 3578 | { | ||
| 3579 | const char *dir; | ||
| 3580 | json_t *tmp; | ||
| 3581 | int i; | ||
| 3582 | |||
| 3583 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 4 times.
|
4 | if (json_unpack_err(ctx, root, "{s:o}", "tunnel", &tmp)) |
| 3584 | ✗ | return 1; | |
| 3585 | |||
| 3586 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 4 times.
|
4 | if (json_unpack_err(ctx, tmp, "{s:i}", "version", &obj->tunnel.erspan.version)) |
| 3587 | ✗ | return 1; | |
| 3588 | |||
| 3589 |
2/3✓ Branch 0 (8→9) taken 2 times.
✓ Branch 1 (8→13) taken 2 times.
✗ Branch 2 (8→23) not taken.
|
4 | switch (obj->tunnel.erspan.version) { |
| 3590 | 2 | case 1: | |
| 3591 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 2 times.
|
2 | if (json_unpack_err(ctx, tmp, "{s:i}", |
| 3592 | "index", &obj->tunnel.erspan.v1.index)) | ||
| 3593 | ✗ | return 1; | |
| 3594 | 2 | break; | |
| 3595 | 2 | case 2: | |
| 3596 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 2 times.
|
2 | if (json_unpack_err(ctx, tmp, "{s:s, s:i}", |
| 3597 | "dir", &dir, | ||
| 3598 | "hwid", &i)) | ||
| 3599 | ✗ | return 1; | |
| 3600 | 2 | obj->tunnel.erspan.v2.hwid = i; | |
| 3601 | |||
| 3602 |
1/2✓ Branch 0 (16→17) taken 2 times.
✗ Branch 1 (16→18) not taken.
|
2 | if (!strcmp(dir, "ingress")) { |
| 3603 | 2 | obj->tunnel.erspan.v2.direction = 0; | |
| 3604 | ✗ | } else if (!strcmp(dir, "egress")) { | |
| 3605 | ✗ | obj->tunnel.erspan.v2.direction = 1; | |
| 3606 | } else { | ||
| 3607 | ✗ | json_error(ctx, "Invalid direction '%s'.", dir); | |
| 3608 | ✗ | return 1; | |
| 3609 | } | ||
| 3610 | 2 | break; | |
| 3611 | ✗ | default: | |
| 3612 | ✗ | json_error(ctx, "Invalid erspan version %u" , obj->tunnel.erspan.version); | |
| 3613 | ✗ | return 1; | |
| 3614 | } | ||
| 3615 | |||
| 3616 | 4 | return 0; | |
| 3617 | } | ||
| 3618 | |||
| 3619 | 8 | static enum tunnel_type json_parse_tunnel_type(struct json_ctx *ctx, | |
| 3620 | const char *type) | ||
| 3621 | { | ||
| 3622 | const struct { | ||
| 3623 | const char *type; | ||
| 3624 | int val; | ||
| 3625 | 8 | } type_tbl[] = { | |
| 3626 | { "erspan", TUNNEL_ERSPAN }, | ||
| 3627 | { "vxlan", TUNNEL_VXLAN }, | ||
| 3628 | { "geneve", TUNNEL_GENEVE }, | ||
| 3629 | }; | ||
| 3630 | unsigned int i; | ||
| 3631 | |||
| 3632 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 8 times.
|
8 | if (!type) |
| 3633 | ✗ | return TUNNEL_UNSPEC; | |
| 3634 | |||
| 3635 |
1/2✓ Branch 0 (8→5) taken 14 times.
✗ Branch 1 (8→9) not taken.
|
14 | for (i = 0; i < array_size(type_tbl); i++) { |
| 3636 |
2/2✓ Branch 0 (5→6) taken 8 times.
✓ Branch 1 (5→7) taken 6 times.
|
14 | if (!strcmp(type, type_tbl[i].type)) |
| 3637 | 8 | return type_tbl[i].val; | |
| 3638 | } | ||
| 3639 | |||
| 3640 | ✗ | return TUNNEL_UNSPEC; | |
| 3641 | } | ||
| 3642 | |||
| 3643 | 8 | static int json_parse_tunnel_src_and_dst(struct json_ctx *ctx, | |
| 3644 | json_t *root, | ||
| 3645 | struct obj *obj) | ||
| 3646 | { | ||
| 3647 | 8 | bool is_ipv4 = false, src_set = false, dst_set = false; | |
| 3648 | struct expr *expr; | ||
| 3649 | json_t *tmp; | ||
| 3650 | |||
| 3651 |
1/2✓ Branch 0 (3→4) taken 8 times.
✗ Branch 1 (3→9) not taken.
|
8 | if (!json_unpack(root, "{s:o}", "src-ipv4", &tmp)) { |
| 3652 | 8 | is_ipv4 = true; | |
| 3653 | 8 | src_set = true; | |
| 3654 | 8 | expr = json_parse_expr(ctx, tmp); | |
| 3655 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 8 times.
|
8 | if (!expr) |
| 3656 | ✗ | return -1; | |
| 3657 | 8 | datatype_set(expr, &ipaddr_type); | |
| 3658 | 8 | obj->tunnel.src = expr; | |
| 3659 | } | ||
| 3660 | |||
| 3661 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→19) taken 8 times.
|
8 | if (!json_unpack(root, "{s:o}", "src-ipv6", &tmp)) { |
| 3662 | ✗ | if (is_ipv4 || src_set) | |
| 3663 | ✗ | return -1; | |
| 3664 | ✗ | src_set = true; | |
| 3665 | ✗ | expr = json_parse_expr(ctx, tmp); | |
| 3666 | ✗ | if (!expr) | |
| 3667 | ✗ | return -1; | |
| 3668 | ✗ | datatype_set(expr, &ip6addr_type); | |
| 3669 | ✗ | obj->tunnel.src = expr; | |
| 3670 | } | ||
| 3671 | |||
| 3672 |
1/2✓ Branch 0 (20→21) taken 8 times.
✗ Branch 1 (20→28) not taken.
|
8 | if (!json_unpack(root, "{s:o}", "dst-ipv4", &tmp)) { |
| 3673 | 8 | dst_set = true; | |
| 3674 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 8 times.
|
8 | if (!is_ipv4) |
| 3675 | ✗ | return -1; | |
| 3676 | 8 | expr = json_parse_expr(ctx, tmp); | |
| 3677 |
1/2✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 8 times.
|
8 | if (!expr) |
| 3678 | ✗ | return -1; | |
| 3679 | 8 | datatype_set(expr, &ipaddr_type); | |
| 3680 | 8 | obj->tunnel.dst = expr; | |
| 3681 | } | ||
| 3682 | |||
| 3683 |
1/2✗ Branch 0 (29→30) not taken.
✓ Branch 1 (29→38) taken 8 times.
|
8 | if (!json_unpack(root, "{s:o}", "dst-ipv6", &tmp)) { |
| 3684 | ✗ | if (is_ipv4 || dst_set) | |
| 3685 | ✗ | return -1; | |
| 3686 | ✗ | dst_set = true; | |
| 3687 | ✗ | expr = json_parse_expr(ctx, tmp); | |
| 3688 | ✗ | if (!expr) | |
| 3689 | ✗ | return -1; | |
| 3690 | ✗ | datatype_set(expr, &ip6addr_type); | |
| 3691 | ✗ | obj->tunnel.dst = expr; | |
| 3692 | } | ||
| 3693 | |||
| 3694 |
2/4✓ Branch 0 (38→39) taken 8 times.
✗ Branch 1 (38→40) not taken.
✗ Branch 2 (39→40) not taken.
✓ Branch 3 (39→41) taken 8 times.
|
8 | if (!dst_set || !src_set) |
| 3695 | ✗ | return -1; | |
| 3696 | |||
| 3697 | 8 | return 0; | |
| 3698 | } | ||
| 3699 | |||
| 3700 | 125 | static struct cmd *json_parse_cmd_add_object(struct json_ctx *ctx, | |
| 3701 | json_t *root, enum cmd_ops op, | ||
| 3702 | enum cmd_obj cmd_obj) | ||
| 3703 | { | ||
| 3704 | 125 | const char *family, *tmp = NULL, *rate_unit = "packets", *burst_unit = "bytes"; | |
| 3705 | 125 | uint32_t l3proto = NFPROTO_UNSPEC; | |
| 3706 | 125 | int inv = 0, flags = 0, i, j; | |
| 3707 | 125 | struct handle h = { 0 }; | |
| 3708 | json_t *tmp_json; | ||
| 3709 | struct obj *obj; | ||
| 3710 | |||
| 3711 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 125 times.
|
125 | if (json_unpack_err(ctx, root, "{s:s, s:s}", |
| 3712 | "family", &family, | ||
| 3713 | "table", &h.table.name)) | ||
| 3714 | ✗ | return NULL; | |
| 3715 |
3/4✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→7) taken 124 times.
✗ Branch 2 (6→7) not taken.
✓ Branch 3 (6→10) taken 1 times.
|
125 | if ((op != CMD_DELETE || |
| 3716 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 124 times.
|
124 | cmd_obj == NFT_OBJECT_CT_HELPER) && |
| 3717 | 124 | json_unpack_err(ctx, root, "{s:s}", "name", &h.obj.name)) { | |
| 3718 | ✗ | return NULL; | |
| 3719 |
4/6✓ Branch 0 (10→11) taken 124 times.
✓ Branch 1 (10→12) taken 1 times.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→19) taken 124 times.
✓ Branch 4 (12→13) taken 1 times.
✗ Branch 5 (12→19) not taken.
|
125 | } else if ((op == CMD_DELETE || op == CMD_DESTROY) && |
| 3720 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→19) taken 1 times.
|
1 | cmd_obj != NFT_OBJECT_CT_HELPER && |
| 3721 |
0/2✗ Branch 0 (16→17) not taken.
✗ Branch 1 (16→19) not taken.
|
1 | json_unpack(root, "{s:s}", "name", &h.obj.name) && |
| 3722 | ✗ | json_unpack(root, "{s:I}", "handle", &h.handle.id)) { | |
| 3723 | ✗ | json_error(ctx, "Either name or handle required to delete an object."); | |
| 3724 | ✗ | return NULL; | |
| 3725 | } | ||
| 3726 | |||
| 3727 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→23) taken 125 times.
|
125 | if (parse_family(family, &h.family)) { |
| 3728 | ✗ | json_error(ctx, "Unknown family '%s'.", family); | |
| 3729 | ✗ | return NULL; | |
| 3730 | } | ||
| 3731 | 125 | h.table.name = xstrdup(h.table.name); | |
| 3732 |
1/2✓ Branch 0 (24→25) taken 125 times.
✗ Branch 1 (24→27) not taken.
|
125 | if (h.obj.name) |
| 3733 | 125 | h.obj.name = xstrdup(h.obj.name); | |
| 3734 | |||
| 3735 |
4/6✓ Branch 0 (27→28) taken 124 times.
✓ Branch 1 (27→30) taken 1 times.
✓ Branch 2 (28→29) taken 124 times.
✗ Branch 3 (28→30) not taken.
✗ Branch 4 (29→30) not taken.
✓ Branch 5 (29→36) taken 124 times.
|
125 | if (op == CMD_DELETE || op == CMD_LIST || op == CMD_DESTROY) { |
| 3736 |
1/2✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→34) taken 1 times.
|
1 | if (cmd_obj == NFT_OBJECT_CT_HELPER) |
| 3737 | ✗ | return cmd_alloc_obj_ct(op, NFT_OBJECT_CT_HELPER, | |
| 3738 | &h, int_loc, obj_alloc(int_loc)); | ||
| 3739 | 1 | return cmd_alloc(op, cmd_obj, &h, int_loc, NULL); | |
| 3740 | } | ||
| 3741 | |||
| 3742 | 124 | obj = obj_alloc(int_loc); | |
| 3743 | |||
| 3744 |
2/2✓ Branch 0 (38→39) taken 17 times.
✓ Branch 1 (38→41) taken 107 times.
|
124 | if (!json_unpack(root, "{s:s}", "comment", &obj->comment)) |
| 3745 | 17 | obj->comment = xstrdup(obj->comment); | |
| 3746 | |||
| 3747 |
9/10✓ Branch 0 (41→42) taken 38 times.
✓ Branch 1 (41→44) taken 25 times.
✓ Branch 2 (41→50) taken 3 times.
✓ Branch 3 (41→57) taken 12 times.
✓ Branch 4 (41→78) taken 7 times.
✓ Branch 5 (41→97) taken 10 times.
✓ Branch 6 (41→121) taken 12 times.
✓ Branch 7 (41→135) taken 9 times.
✓ Branch 8 (41→142) taken 8 times.
✗ Branch 9 (41→186) not taken.
|
124 | switch (cmd_obj) { |
| 3748 | 38 | case CMD_OBJ_COUNTER: | |
| 3749 | 38 | obj->type = NFT_OBJECT_COUNTER; | |
| 3750 | 38 | json_unpack(root, "{s:I}", "packets", &obj->counter.packets); | |
| 3751 | 38 | json_unpack(root, "{s:I}", "bytes", &obj->counter.bytes); | |
| 3752 | 38 | break; | |
| 3753 | 25 | case CMD_OBJ_QUOTA: | |
| 3754 | 25 | obj->type = NFT_OBJECT_QUOTA; | |
| 3755 | 25 | json_unpack(root, "{s:I}", "bytes", &obj->quota.bytes); | |
| 3756 | 25 | json_unpack(root, "{s:I}", "used", &obj->quota.used); | |
| 3757 | 25 | json_unpack(root, "{s:b}", "inv", &obj->quota.flags); | |
| 3758 |
2/2✓ Branch 0 (47→48) taken 12 times.
✓ Branch 1 (47→49) taken 13 times.
|
25 | if (obj->quota.flags) |
| 3759 | 12 | obj->quota.flags = NFT_QUOTA_F_INV; | |
| 3760 | 25 | break; | |
| 3761 | 3 | case CMD_OBJ_SECMARK: | |
| 3762 | 3 | obj->type = NFT_OBJECT_SECMARK; | |
| 3763 |
1/2✓ Branch 0 (51→52) taken 3 times.
✗ Branch 1 (51→56) not taken.
|
3 | if (!json_unpack(root, "{s:s}", "context", &tmp)) { |
| 3764 | int ret; | ||
| 3765 | 3 | ret = snprintf(obj->secmark.ctx, sizeof(obj->secmark.ctx), "%s", tmp); | |
| 3766 |
2/4✓ Branch 0 (52→53) taken 3 times.
✗ Branch 1 (52→54) not taken.
✗ Branch 2 (53→54) not taken.
✓ Branch 3 (53→56) taken 3 times.
|
3 | if (ret < 0 || ret >= (int)sizeof(obj->secmark.ctx)) { |
| 3767 | ✗ | json_error(ctx, "Invalid secmark context '%s', max length is %zu.", | |
| 3768 | tmp, sizeof(obj->secmark.ctx)); | ||
| 3769 | ✗ | goto err_free_obj; | |
| 3770 | } | ||
| 3771 | } | ||
| 3772 | 3 | break; | |
| 3773 | 12 | case NFT_OBJECT_CT_HELPER: | |
| 3774 | 12 | cmd_obj = CMD_OBJ_CT_HELPER; | |
| 3775 | 12 | obj->type = NFT_OBJECT_CT_HELPER; | |
| 3776 |
1/2✓ Branch 0 (58→59) taken 12 times.
✗ Branch 1 (58→63) not taken.
|
12 | if (!json_unpack(root, "{s:s}", "type", &tmp)) { |
| 3777 | int ret; | ||
| 3778 | |||
| 3779 | 12 | ret = snprintf(obj->ct_helper.name, | |
| 3780 | sizeof(obj->ct_helper.name), "%s", tmp); | ||
| 3781 |
2/4✓ Branch 0 (59→60) taken 12 times.
✗ Branch 1 (59→61) not taken.
✗ Branch 2 (60→61) not taken.
✓ Branch 3 (60→63) taken 12 times.
|
12 | if (ret < 0 || |
| 3782 | ret >= (int)sizeof(obj->ct_helper.name)) { | ||
| 3783 | ✗ | json_error(ctx, "Invalid CT helper type '%s', max length is %zu.", | |
| 3784 | tmp, sizeof(obj->ct_helper.name)); | ||
| 3785 | ✗ | goto err_free_obj; | |
| 3786 | } | ||
| 3787 | } | ||
| 3788 |
1/2✓ Branch 0 (64→65) taken 12 times.
✗ Branch 1 (64→71) not taken.
|
12 | if (!json_unpack(root, "{s:s}", "protocol", &tmp)) { |
| 3789 |
2/2✓ Branch 0 (65→66) taken 10 times.
✓ Branch 1 (65→67) taken 2 times.
|
12 | if (!strcmp(tmp, "tcp")) { |
| 3790 | 10 | obj->ct_helper.l4proto = IPPROTO_TCP; | |
| 3791 |
1/2✓ Branch 0 (67→68) taken 2 times.
✗ Branch 1 (67→69) not taken.
|
2 | } else if (!strcmp(tmp, "udp")) { |
| 3792 | 2 | obj->ct_helper.l4proto = IPPROTO_UDP; | |
| 3793 | } else { | ||
| 3794 | ✗ | json_error(ctx, "Invalid ct helper protocol '%s'.", tmp); | |
| 3795 | ✗ | goto err_free_obj; | |
| 3796 | } | ||
| 3797 | } | ||
| 3798 |
2/4✓ Branch 0 (72→73) taken 12 times.
✗ Branch 1 (72→77) not taken.
✗ Branch 2 (74→75) not taken.
✓ Branch 3 (74→77) taken 12 times.
|
24 | if (!json_unpack(root, "{s:s}", "l3proto", &tmp) && |
| 3799 | 12 | parse_family(tmp, &l3proto)) { | |
| 3800 | ✗ | json_error(ctx, "Invalid ct helper l3proto '%s'.", tmp); | |
| 3801 | ✗ | goto err_free_obj; | |
| 3802 | } | ||
| 3803 | 12 | obj->ct_helper.l3proto = l3proto; | |
| 3804 | 12 | break; | |
| 3805 | 7 | case NFT_OBJECT_CT_TIMEOUT: | |
| 3806 | 7 | cmd_obj = CMD_OBJ_CT_TIMEOUT; | |
| 3807 | 7 | init_list_head(&obj->ct_timeout.timeout_list); | |
| 3808 | 7 | obj->type = NFT_OBJECT_CT_TIMEOUT; | |
| 3809 |
1/2✓ Branch 0 (80→81) taken 7 times.
✗ Branch 1 (80→87) not taken.
|
7 | if (!json_unpack(root, "{s:s}", "protocol", &tmp)) { |
| 3810 |
2/2✓ Branch 0 (81→82) taken 4 times.
✓ Branch 1 (81→83) taken 3 times.
|
7 | if (!strcmp(tmp, "tcp")) { |
| 3811 | 4 | obj->ct_timeout.l4proto = IPPROTO_TCP; | |
| 3812 |
2/2✓ Branch 0 (83→84) taken 2 times.
✓ Branch 1 (83→85) taken 1 times.
|
3 | } else if (!strcmp(tmp, "udp")) { |
| 3813 | 2 | obj->ct_timeout.l4proto = IPPROTO_UDP; | |
| 3814 | } else { | ||
| 3815 | 1 | json_error(ctx, "Invalid ct timeout protocol '%s'.", tmp); | |
| 3816 | 1 | goto err_free_obj; | |
| 3817 | } | ||
| 3818 | } | ||
| 3819 |
2/4✓ Branch 0 (88→89) taken 6 times.
✗ Branch 1 (88→93) not taken.
✗ Branch 2 (90→91) not taken.
✓ Branch 3 (90→93) taken 6 times.
|
12 | if (!json_unpack(root, "{s:s}", "l3proto", &tmp) && |
| 3820 | 6 | parse_family(tmp, &l3proto)) { | |
| 3821 | ✗ | json_error(ctx, "Invalid ct timeout l3proto '%s'.", tmp); | |
| 3822 | ✗ | goto err_free_obj; | |
| 3823 | } | ||
| 3824 | 6 | obj->ct_timeout.l3proto = l3proto; | |
| 3825 | |||
| 3826 |
1/2✗ Branch 0 (94→95) not taken.
✓ Branch 1 (94→96) taken 6 times.
|
6 | if (json_parse_ct_timeout_policy(ctx, root, obj)) |
| 3827 | ✗ | goto err_free_obj; | |
| 3828 | 6 | break; | |
| 3829 | 10 | case NFT_OBJECT_CT_EXPECT: | |
| 3830 | 10 | cmd_obj = CMD_OBJ_CT_EXPECT; | |
| 3831 | 10 | obj->type = NFT_OBJECT_CT_EXPECT; | |
| 3832 |
2/4✓ Branch 0 (98→99) taken 10 times.
✗ Branch 1 (98→103) not taken.
✗ Branch 2 (100→101) not taken.
✓ Branch 3 (100→103) taken 10 times.
|
20 | if (!json_unpack(root, "{s:s}", "l3proto", &tmp) && |
| 3833 | 10 | parse_family(tmp, &l3proto)) { | |
| 3834 | ✗ | json_error(ctx, "Invalid ct expectation l3proto '%s'.", tmp); | |
| 3835 | ✗ | goto err_free_obj; | |
| 3836 | } | ||
| 3837 | 10 | obj->ct_expect.l3proto = l3proto; | |
| 3838 |
1/2✓ Branch 0 (104→105) taken 10 times.
✗ Branch 1 (104→111) not taken.
|
10 | if (!json_unpack(root, "{s:s}", "protocol", &tmp)) { |
| 3839 |
1/2✓ Branch 0 (105→106) taken 10 times.
✗ Branch 1 (105→107) not taken.
|
10 | if (!strcmp(tmp, "tcp")) { |
| 3840 | 10 | obj->ct_expect.l4proto = IPPROTO_TCP; | |
| 3841 | ✗ | } else if (!strcmp(tmp, "udp")) { | |
| 3842 | ✗ | obj->ct_expect.l4proto = IPPROTO_UDP; | |
| 3843 | } else { | ||
| 3844 | ✗ | json_error(ctx, "Invalid ct expectation protocol '%s'.", tmp); | |
| 3845 | ✗ | goto err_free_obj; | |
| 3846 | } | ||
| 3847 | } | ||
| 3848 |
1/2✓ Branch 0 (112→113) taken 10 times.
✗ Branch 1 (112→114) not taken.
|
10 | if (!json_unpack(root, "{s:i}", "dport", &i)) |
| 3849 | 10 | obj->ct_expect.dport = i; | |
| 3850 |
1/2✓ Branch 0 (115→116) taken 10 times.
✗ Branch 1 (115→117) not taken.
|
10 | if (!json_unpack(root, "{s:i}", "timeout", &i)) |
| 3851 | 10 | obj->ct_expect.timeout = i; | |
| 3852 |
1/2✓ Branch 0 (118→119) taken 10 times.
✗ Branch 1 (118→120) not taken.
|
10 | if (!json_unpack(root, "{s:i}", "size", &i)) |
| 3853 | 10 | obj->ct_expect.size = i; | |
| 3854 | 10 | break; | |
| 3855 | 12 | case CMD_OBJ_LIMIT: | |
| 3856 | 12 | obj->type = NFT_OBJECT_LIMIT; | |
| 3857 |
1/2✗ Branch 0 (122→123) not taken.
✓ Branch 1 (122→124) taken 12 times.
|
12 | if (json_unpack_err(ctx, root, "{s:I, s:s}", |
| 3858 | "rate", &obj->limit.rate, | ||
| 3859 | "per", &tmp)) | ||
| 3860 | ✗ | goto err_free_obj; | |
| 3861 | |||
| 3862 | 12 | json_unpack(root, "{s:s}", "rate_unit", &rate_unit); | |
| 3863 | 12 | json_unpack(root, "{s:b}", "inv", &inv); | |
| 3864 | 12 | json_unpack(root, "{s:i}", "burst", &obj->limit.burst); | |
| 3865 | 12 | json_unpack(root, "{s:s}", "burst_unit", &burst_unit); | |
| 3866 | |||
| 3867 |
2/2✓ Branch 0 (128→129) taken 6 times.
✓ Branch 1 (128→130) taken 6 times.
|
12 | if (!strcmp(rate_unit, "packets")) { |
| 3868 | 6 | obj->limit.type = NFT_LIMIT_PKTS; | |
| 3869 | } else { | ||
| 3870 | 6 | obj->limit.type = NFT_LIMIT_PKT_BYTES; | |
| 3871 | 6 | obj->limit.rate = rate_to_bytes(obj->limit.rate, | |
| 3872 | rate_unit); | ||
| 3873 | 6 | obj->limit.burst = rate_to_bytes(obj->limit.burst, | |
| 3874 | burst_unit); | ||
| 3875 | } | ||
| 3876 | 12 | obj->limit.unit = seconds_from_unit(tmp); | |
| 3877 | 12 | obj->limit.flags = inv ? NFT_LIMIT_F_INV : 0; | |
| 3878 | 12 | break; | |
| 3879 | 9 | case CMD_OBJ_SYNPROXY: | |
| 3880 | 9 | obj->type = NFT_OBJECT_SYNPROXY; | |
| 3881 |
1/2✗ Branch 0 (136→137) not taken.
✓ Branch 1 (136→138) taken 9 times.
|
9 | if (json_unpack_err(ctx, root, "{s:i, s:i}", |
| 3882 | "mss", &i, "wscale", &j)) | ||
| 3883 | ✗ | goto err_free_obj; | |
| 3884 | |||
| 3885 | 9 | obj->synproxy.mss = i; | |
| 3886 | 9 | obj->synproxy.wscale = j; | |
| 3887 | 9 | obj->synproxy.flags |= NF_SYNPROXY_OPT_MSS; | |
| 3888 | 9 | obj->synproxy.flags |= NF_SYNPROXY_OPT_WSCALE; | |
| 3889 | 9 | flags = parse_flags_array(ctx, root, "flags", | |
| 3890 | json_parse_synproxy_flag); | ||
| 3891 |
1/2✗ Branch 0 (139→140) not taken.
✓ Branch 1 (139→141) taken 9 times.
|
9 | if (flags < 0) |
| 3892 | ✗ | goto err_free_obj; | |
| 3893 | |||
| 3894 | 9 | obj->synproxy.flags |= flags; | |
| 3895 | 9 | break; | |
| 3896 | 8 | case NFT_OBJECT_TUNNEL: | |
| 3897 | 8 | cmd_obj = CMD_OBJ_TUNNEL; | |
| 3898 | 8 | obj->type = NFT_OBJECT_TUNNEL; | |
| 3899 | |||
| 3900 |
1/2✗ Branch 0 (143→144) not taken.
✓ Branch 1 (143→145) taken 8 times.
|
8 | if (json_parse_tunnel_src_and_dst(ctx, root, obj)) |
| 3901 | ✗ | goto err_free_obj; | |
| 3902 | |||
| 3903 | 8 | json_unpack(root, "{s:i}", "id", &obj->tunnel.id); | |
| 3904 | 8 | json_unpack(root, "{s:i}", "sport", &i); | |
| 3905 | 8 | obj->tunnel.sport = i; | |
| 3906 | 8 | json_unpack(root, "{s:i}", "dport", &i); | |
| 3907 | 8 | obj->tunnel.sport = i; | |
| 3908 | 8 | json_unpack(root, "{s:i}", "ttl", &i); | |
| 3909 | 8 | obj->tunnel.ttl = i; | |
| 3910 | 8 | json_unpack(root, "{s:i}", "tos", &i); | |
| 3911 | 8 | obj->tunnel.tos = i; | |
| 3912 | 8 | json_unpack(root, "{s:s}", "type", &tmp); | |
| 3913 | |||
| 3914 | 8 | obj->tunnel.type = json_parse_tunnel_type(ctx, tmp); | |
| 3915 |
3/5✗ Branch 0 (152→153) not taken.
✓ Branch 1 (152→154) taken 4 times.
✓ Branch 2 (152→158) taken 2 times.
✓ Branch 3 (152→163) taken 2 times.
✗ Branch 4 (152→185) not taken.
|
8 | switch (obj->tunnel.type) { |
| 3916 | ✗ | case TUNNEL_UNSPEC: | |
| 3917 | ✗ | break; | |
| 3918 | 4 | case TUNNEL_ERSPAN: | |
| 3919 |
1/2✗ Branch 0 (155→156) not taken.
✓ Branch 1 (155→157) taken 4 times.
|
4 | if (json_parse_tunnel_erspan(ctx, root, obj)) |
| 3920 | ✗ | goto err_free_obj; | |
| 3921 | 4 | break; | |
| 3922 | 2 | case TUNNEL_VXLAN: | |
| 3923 |
1/2✗ Branch 0 (159→160) not taken.
✓ Branch 1 (159→161) taken 2 times.
|
2 | if (json_unpack_err(ctx, root, |
| 3924 | "{s:o}", "tunnel", &tmp_json)) | ||
| 3925 | ✗ | goto err_free_obj; | |
| 3926 | |||
| 3927 | 2 | json_unpack(tmp_json, "{s:i}", | |
| 3928 | "gbp", &obj->tunnel.vxlan.gbp); | ||
| 3929 | 2 | break; | |
| 3930 | 2 | case TUNNEL_GENEVE: | |
| 3931 | json_t *value; | ||
| 3932 | size_t index; | ||
| 3933 | |||
| 3934 |
1/2✗ Branch 0 (164→165) not taken.
✓ Branch 1 (164→166) taken 2 times.
|
2 | if (json_unpack_err(ctx, root, |
| 3935 | "{s:o}", "tunnel", &tmp_json)) | ||
| 3936 | ✗ | goto err_free_obj; | |
| 3937 | |||
| 3938 |
3/4✓ Branch 0 (181→182) taken 6 times.
✓ Branch 1 (181→184) taken 2 times.
✓ Branch 2 (183→167) taken 6 times.
✗ Branch 3 (183→184) not taken.
|
8 | json_array_foreach(tmp_json, index, value) { |
| 3939 | 6 | struct tunnel_geneve *geneve = xmalloc(sizeof(struct tunnel_geneve)); | |
| 3940 |
1/2✗ Branch 0 (168→169) not taken.
✓ Branch 1 (168→170) taken 6 times.
|
6 | if (!geneve) |
| 3941 | ✗ | memory_allocation_error(); | |
| 3942 | |||
| 3943 |
1/2✗ Branch 0 (171→172) not taken.
✓ Branch 1 (171→173) taken 6 times.
|
6 | if (json_unpack_err(ctx, value, "{s:i, s:i, s:s}", |
| 3944 | "class", &i, | ||
| 3945 | "opt-type", &j, | ||
| 3946 | "data", &tmp)) { | ||
| 3947 | ✗ | free(geneve); | |
| 3948 | ✗ | goto err_free_obj; | |
| 3949 | } | ||
| 3950 | 6 | geneve->geneve_class = i; | |
| 3951 | 6 | geneve->type = j; | |
| 3952 | |||
| 3953 |
1/2✗ Branch 0 (174→175) not taken.
✓ Branch 1 (174→176) taken 6 times.
|
6 | if (tunnel_geneve_data_str2array(tmp, |
| 3954 | 6 | geneve->data, | |
| 3955 | &geneve->data_len)) { | ||
| 3956 | ✗ | free(geneve); | |
| 3957 | ✗ | goto err_free_obj; | |
| 3958 | } | ||
| 3959 | |||
| 3960 |
2/2✓ Branch 0 (176→177) taken 2 times.
✓ Branch 1 (176→178) taken 4 times.
|
6 | if (index == 0) |
| 3961 | 2 | init_list_head(&obj->tunnel.geneve_opts); | |
| 3962 | |||
| 3963 | 6 | list_add_tail(&geneve->list, &obj->tunnel.geneve_opts); | |
| 3964 | } | ||
| 3965 | 2 | break; | |
| 3966 | } | ||
| 3967 | 8 | break; | |
| 3968 | ✗ | default: | |
| 3969 | ✗ | BUG("Invalid CMD '%d'", cmd_obj); | |
| 3970 | } | ||
| 3971 | |||
| 3972 |
1/2✓ Branch 0 (189→190) taken 123 times.
✗ Branch 1 (189→191) not taken.
|
123 | if (op == CMD_ADD) |
| 3973 | 123 | json_object_del(root, "handle"); | |
| 3974 | |||
| 3975 | 123 | return cmd_alloc(op, cmd_obj, &h, int_loc, obj); | |
| 3976 | |||
| 3977 | 1 | err_free_obj: | |
| 3978 | 1 | obj_free(obj); | |
| 3979 | 1 | handle_free(&h); | |
| 3980 | 1 | return NULL; | |
| 3981 | } | ||
| 3982 | |||
| 3983 | 55254 | static struct cmd *json_parse_cmd_add(struct json_ctx *ctx, | |
| 3984 | json_t *root, enum cmd_ops op) | ||
| 3985 | { | ||
| 3986 | struct { | ||
| 3987 | const char *key; | ||
| 3988 | enum cmd_obj obj; | ||
| 3989 | struct cmd *(*cb)(struct json_ctx *, json_t *, | ||
| 3990 | enum cmd_ops, enum cmd_obj); | ||
| 3991 | 55254 | } cmd_obj_table[] = { | |
| 3992 | { "table", CMD_OBJ_TABLE, json_parse_cmd_add_table }, | ||
| 3993 | { "chain", CMD_OBJ_CHAIN, json_parse_cmd_add_chain }, | ||
| 3994 | { "rule", CMD_OBJ_RULE, json_parse_cmd_add_rule }, | ||
| 3995 | { "set", CMD_OBJ_SET, json_parse_cmd_add_set }, | ||
| 3996 | { "map", CMD_OBJ_SET, json_parse_cmd_add_set }, | ||
| 3997 | { "element", CMD_OBJ_ELEMENTS, json_parse_cmd_add_element }, | ||
| 3998 | { "flowtable", CMD_OBJ_FLOWTABLE, json_parse_cmd_add_flowtable }, | ||
| 3999 | { "counter", CMD_OBJ_COUNTER, json_parse_cmd_add_object }, | ||
| 4000 | { "quota", CMD_OBJ_QUOTA, json_parse_cmd_add_object }, | ||
| 4001 | { "ct helper", NFT_OBJECT_CT_HELPER, json_parse_cmd_add_object }, | ||
| 4002 | { "ct timeout", NFT_OBJECT_CT_TIMEOUT, json_parse_cmd_add_object }, | ||
| 4003 | { "ct expectation", NFT_OBJECT_CT_EXPECT, json_parse_cmd_add_object }, | ||
| 4004 | { "tunnel", NFT_OBJECT_TUNNEL, json_parse_cmd_add_object }, | ||
| 4005 | { "limit", CMD_OBJ_LIMIT, json_parse_cmd_add_object }, | ||
| 4006 | { "secmark", CMD_OBJ_SECMARK, json_parse_cmd_add_object }, | ||
| 4007 | { "synproxy", CMD_OBJ_SYNPROXY, json_parse_cmd_add_object } | ||
| 4008 | }; | ||
| 4009 | unsigned int i; | ||
| 4010 | json_t *tmp; | ||
| 4011 | |||
| 4012 |
2/4✓ Branch 0 (2→3) taken 55254 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→7) taken 55254 times.
|
55254 | if (!json_is_object(root)) { |
| 4013 | ✗ | json_error(ctx, "Value of add command must be object (got %s instead).", | |
| 4014 | json_typename(root)); | ||
| 4015 | ✗ | return NULL; | |
| 4016 | } | ||
| 4017 | |||
| 4018 |
1/2✓ Branch 0 (17→8) taken 163183 times.
✗ Branch 1 (17→18) not taken.
|
163183 | for (i = 0; i < array_size(cmd_obj_table); i++) { |
| 4019 | 163183 | tmp = json_object_get(root, cmd_obj_table[i].key); | |
| 4020 |
2/2✓ Branch 0 (9→10) taken 107929 times.
✓ Branch 1 (9→11) taken 55254 times.
|
163183 | if (!tmp) |
| 4021 | 107929 | continue; | |
| 4022 | |||
| 4023 |
3/4✓ Branch 0 (11→12) taken 2 times.
✓ Branch 1 (11→15) taken 55252 times.
✗ Branch 2 (12→13) not taken.
✓ Branch 3 (12→15) taken 2 times.
|
55254 | if (op == CMD_CREATE && cmd_obj_table[i].obj == CMD_OBJ_RULE) { |
| 4024 | ✗ | json_error(ctx, "Create command not available for rules."); | |
| 4025 | ✗ | return NULL; | |
| 4026 | } | ||
| 4027 | |||
| 4028 | 55254 | return cmd_obj_table[i].cb(ctx, tmp, op, cmd_obj_table[i].obj); | |
| 4029 | } | ||
| 4030 | ✗ | json_error(ctx, "Unknown object passed to add command."); | |
| 4031 | ✗ | return NULL; | |
| 4032 | } | ||
| 4033 | |||
| 4034 | 23 | static struct cmd *json_parse_cmd_replace(struct json_ctx *ctx, | |
| 4035 | json_t *root, enum cmd_ops op) | ||
| 4036 | { | ||
| 4037 | 23 | struct handle h = { | |
| 4038 | 23 | .table.location = *int_loc, | |
| 4039 | 23 | .chain.location = *int_loc, | |
| 4040 | 23 | .index.location = *int_loc, | |
| 4041 | }; | ||
| 4042 | json_t *tmp, *value; | ||
| 4043 | const char *family; | ||
| 4044 | struct rule *rule; | ||
| 4045 | size_t index; | ||
| 4046 | |||
| 4047 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 23 times.
|
23 | if (json_unpack_err(ctx, root, "{s:o}", "rule", &tmp)) |
| 4048 | ✗ | return NULL; | |
| 4049 | 23 | root = tmp; | |
| 4050 | |||
| 4051 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 23 times.
|
23 | if (json_unpack_err(ctx, root, "{s:s, s:s, s:s, s:o}", |
| 4052 | "family", &family, | ||
| 4053 | "table", &h.table.name, | ||
| 4054 | "chain", &h.chain.name, | ||
| 4055 | "expr", &tmp)) | ||
| 4056 | ✗ | return NULL; | |
| 4057 | 23 | json_unpack(root, "{s:I}", "handle", &h.handle.id); | |
| 4058 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 23 times.
|
23 | if (!json_unpack(root, "{s:I}", "index", &h.index.id)) { |
| 4059 | ✗ | h.index.id++; | |
| 4060 | } | ||
| 4061 | |||
| 4062 |
3/4✓ Branch 0 (12→13) taken 1 times.
✓ Branch 1 (12→16) taken 22 times.
✗ Branch 2 (13→14) not taken.
✓ Branch 3 (13→16) taken 1 times.
|
23 | if (op == CMD_REPLACE && !h.handle.id) { |
| 4063 | ✗ | json_error(ctx, "Handle is required when replacing a rule."); | |
| 4064 | ✗ | return NULL; | |
| 4065 | } | ||
| 4066 | |||
| 4067 |
5/6✓ Branch 0 (16→17) taken 1 times.
✓ Branch 1 (16→18) taken 22 times.
✗ Branch 2 (17→18) not taken.
✓ Branch 3 (17→20) taken 1 times.
✓ Branch 4 (18→19) taken 1 times.
✓ Branch 5 (18→20) taken 21 times.
|
23 | if ((op == CMD_INSERT || op == CMD_ADD) && h.handle.id) { |
| 4068 | 1 | h.position.id = h.handle.id; | |
| 4069 | 1 | h.handle.id = 0; | |
| 4070 | } | ||
| 4071 | |||
| 4072 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→24) taken 23 times.
|
23 | if (parse_family(family, &h.family)) { |
| 4073 | ✗ | json_error(ctx, "Unknown family '%s'.", family); | |
| 4074 | ✗ | return NULL; | |
| 4075 | } | ||
| 4076 | |||
| 4077 |
2/4✓ Branch 0 (24→25) taken 23 times.
✗ Branch 1 (24→26) not taken.
✗ Branch 2 (25→26) not taken.
✓ Branch 3 (25→28) taken 23 times.
|
23 | if (!json_is_array(tmp)) { |
| 4078 | ✗ | json_error(ctx, "Value of property \"expr\" must be an array."); | |
| 4079 | ✗ | return NULL; | |
| 4080 | } | ||
| 4081 | |||
| 4082 | 23 | h.table.name = xstrdup(h.table.name); | |
| 4083 | 23 | h.chain.name = xstrdup(h.chain.name); | |
| 4084 | |||
| 4085 | 23 | rule = rule_alloc(int_loc, NULL); | |
| 4086 | |||
| 4087 |
1/2✗ Branch 0 (32→33) not taken.
✓ Branch 1 (32→35) taken 23 times.
|
23 | if (!json_unpack(root, "{s:s}", "comment", &rule->comment)) |
| 4088 | ✗ | rule->comment = xstrdup(rule->comment); | |
| 4089 | |||
| 4090 |
3/4✓ Branch 0 (48→49) taken 47 times.
✓ Branch 1 (48→51) taken 23 times.
✓ Branch 2 (50→36) taken 47 times.
✗ Branch 3 (50→51) not taken.
|
70 | json_array_foreach(tmp, index, value) { |
| 4091 | struct stmt *stmt; | ||
| 4092 | |||
| 4093 |
2/4✓ Branch 0 (36→37) taken 47 times.
✗ Branch 1 (36→38) not taken.
✗ Branch 2 (37→38) not taken.
✓ Branch 3 (37→41) taken 47 times.
|
47 | if (!json_is_object(value)) { |
| 4094 | ✗ | json_error(ctx, "Unexpected expr array element of type %s, expected object.", | |
| 4095 | json_typename(value)); | ||
| 4096 | ✗ | goto err_free_replace; | |
| 4097 | } | ||
| 4098 | |||
| 4099 | 47 | stmt = json_parse_stmt(ctx, value); | |
| 4100 | |||
| 4101 |
1/2✗ Branch 0 (42→43) not taken.
✓ Branch 1 (42→45) taken 47 times.
|
47 | if (!stmt) { |
| 4102 | ✗ | json_error(ctx, "Parsing expr array at index %zd failed.", | |
| 4103 | index); | ||
| 4104 | ✗ | goto err_free_replace; | |
| 4105 | } | ||
| 4106 | |||
| 4107 | 47 | rule_stmt_append(rule, stmt); | |
| 4108 | } | ||
| 4109 | |||
| 4110 |
2/2✓ Branch 0 (51→52) taken 1 times.
✓ Branch 1 (51→53) taken 22 times.
|
23 | if (op == CMD_REPLACE) |
| 4111 | 1 | json_object_del(root, "handle"); | |
| 4112 | |||
| 4113 | 23 | return cmd_alloc(op, CMD_OBJ_RULE, &h, int_loc, rule); | |
| 4114 | |||
| 4115 | ✗ | err_free_replace: | |
| 4116 | ✗ | rule_free(rule); | |
| 4117 | ✗ | handle_free(&h); | |
| 4118 | ✗ | return NULL; | |
| 4119 | } | ||
| 4120 | |||
| 4121 | 10 | static struct cmd *json_parse_cmd_list_multiple(struct json_ctx *ctx, | |
| 4122 | json_t *root, enum cmd_ops op, | ||
| 4123 | enum cmd_obj obj) | ||
| 4124 | { | ||
| 4125 | 10 | struct handle h = { | |
| 4126 | .family = NFPROTO_UNSPEC, | ||
| 4127 | }; | ||
| 4128 | const char *tmp; | ||
| 4129 | |||
| 4130 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→8) taken 10 times.
|
10 | if (!json_unpack(root, "{s:s}", "family", &tmp)) { |
| 4131 | ✗ | if (parse_family(tmp, &h.family)) { | |
| 4132 | ✗ | json_error(ctx, "Unknown family '%s'.", tmp); | |
| 4133 | ✗ | return NULL; | |
| 4134 | } | ||
| 4135 | } | ||
| 4136 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→14) taken 10 times.
|
10 | switch (obj) { |
| 4137 | ✗ | case CMD_OBJ_SETS: | |
| 4138 | case CMD_OBJ_COUNTERS: | ||
| 4139 | case CMD_OBJ_CT_HELPERS: | ||
| 4140 | case CMD_OBJ_TUNNELS: | ||
| 4141 | ✗ | if (!json_unpack(root, "{s:s}", "table", &tmp)) | |
| 4142 | ✗ | h.table.name = xstrdup(tmp); | |
| 4143 | ✗ | break; | |
| 4144 | 10 | default: | |
| 4145 | 10 | break; | |
| 4146 | } | ||
| 4147 |
1/4✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→19) taken 10 times.
✗ Branch 2 (16→17) not taken.
✗ Branch 3 (16→19) not taken.
|
10 | if (obj == CMD_OBJ_CT_HELPERS && !h.table.name) { |
| 4148 | ✗ | json_error(ctx, "Listing ct helpers requires table reference."); | |
| 4149 | ✗ | return NULL; | |
| 4150 | } | ||
| 4151 | 10 | return cmd_alloc(op, obj, &h, int_loc, NULL); | |
| 4152 | } | ||
| 4153 | |||
| 4154 | 7 | static struct cmd *json_parse_cmd_list(struct json_ctx *ctx, | |
| 4155 | json_t *root, enum cmd_ops op) | ||
| 4156 | { | ||
| 4157 | struct { | ||
| 4158 | const char *key; | ||
| 4159 | enum cmd_obj obj; | ||
| 4160 | struct cmd *(*cb)(struct json_ctx *, json_t *, | ||
| 4161 | enum cmd_ops, enum cmd_obj); | ||
| 4162 | 7 | } cmd_obj_table[] = { | |
| 4163 | { "table", CMD_OBJ_TABLE, json_parse_cmd_add_table }, | ||
| 4164 | { "tables", CMD_OBJ_TABLE, json_parse_cmd_list_multiple }, | ||
| 4165 | { "chain", CMD_OBJ_CHAIN, json_parse_cmd_add_chain }, | ||
| 4166 | { "chains", CMD_OBJ_CHAINS, json_parse_cmd_list_multiple }, | ||
| 4167 | { "set", CMD_OBJ_SET, json_parse_cmd_add_set }, | ||
| 4168 | { "sets", CMD_OBJ_SETS, json_parse_cmd_list_multiple }, | ||
| 4169 | { "map", CMD_OBJ_MAP, json_parse_cmd_add_set }, | ||
| 4170 | { "maps", CMD_OBJ_MAPS, json_parse_cmd_list_multiple }, | ||
| 4171 | { "counter", CMD_OBJ_COUNTER, json_parse_cmd_add_object }, | ||
| 4172 | { "counters", CMD_OBJ_COUNTERS, json_parse_cmd_list_multiple }, | ||
| 4173 | { "quota", CMD_OBJ_QUOTA, json_parse_cmd_add_object }, | ||
| 4174 | { "quotas", CMD_OBJ_QUOTAS, json_parse_cmd_list_multiple }, | ||
| 4175 | { "ct helper", NFT_OBJECT_CT_HELPER, json_parse_cmd_add_object }, | ||
| 4176 | { "ct helpers", CMD_OBJ_CT_HELPERS, json_parse_cmd_list_multiple }, | ||
| 4177 | { "ct timeout", NFT_OBJECT_CT_TIMEOUT, json_parse_cmd_add_object }, | ||
| 4178 | { "ct expectation", NFT_OBJECT_CT_EXPECT, json_parse_cmd_add_object }, | ||
| 4179 | { "tunnel", NFT_OBJECT_TUNNEL, json_parse_cmd_add_object }, | ||
| 4180 | { "tunnels", CMD_OBJ_TUNNELS, json_parse_cmd_list_multiple }, | ||
| 4181 | { "limit", CMD_OBJ_LIMIT, json_parse_cmd_add_object }, | ||
| 4182 | { "limits", CMD_OBJ_LIMIT, json_parse_cmd_list_multiple }, | ||
| 4183 | { "ruleset", CMD_OBJ_RULESET, json_parse_cmd_list_multiple }, | ||
| 4184 | { "meter", CMD_OBJ_METER, json_parse_cmd_add_set }, | ||
| 4185 | { "meters", CMD_OBJ_METERS, json_parse_cmd_list_multiple }, | ||
| 4186 | { "flowtables", CMD_OBJ_FLOWTABLES, json_parse_cmd_list_multiple }, | ||
| 4187 | { "secmark", CMD_OBJ_SECMARK, json_parse_cmd_add_object }, | ||
| 4188 | { "secmarks", CMD_OBJ_SECMARKS, json_parse_cmd_list_multiple }, | ||
| 4189 | }; | ||
| 4190 | unsigned int i; | ||
| 4191 | json_t *tmp; | ||
| 4192 | |||
| 4193 |
2/4✓ Branch 0 (2→3) taken 7 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→7) taken 7 times.
|
7 | if (!json_is_object(root)) { |
| 4194 | ✗ | json_error(ctx, "Value of list command must be object (got %s instead).", | |
| 4195 | json_typename(root)); | ||
| 4196 | ✗ | return NULL; | |
| 4197 | } | ||
| 4198 | |||
| 4199 |
1/2✓ Branch 0 (13→8) taken 147 times.
✗ Branch 1 (13→14) not taken.
|
147 | for (i = 0; i < array_size(cmd_obj_table); i++) { |
| 4200 | 147 | tmp = json_object_get(root, cmd_obj_table[i].key); | |
| 4201 |
2/2✓ Branch 0 (9→10) taken 140 times.
✓ Branch 1 (9→11) taken 7 times.
|
147 | if (!tmp) |
| 4202 | 140 | continue; | |
| 4203 | |||
| 4204 | 7 | return cmd_obj_table[i].cb(ctx, tmp, op, cmd_obj_table[i].obj); | |
| 4205 | } | ||
| 4206 | ✗ | json_error(ctx, "Unknown object passed to list command."); | |
| 4207 | ✗ | return NULL; | |
| 4208 | } | ||
| 4209 | |||
| 4210 | ✗ | static struct cmd *json_parse_cmd_reset_rule(struct json_ctx *ctx, | |
| 4211 | json_t *root, enum cmd_ops op, | ||
| 4212 | enum cmd_obj obj) | ||
| 4213 | { | ||
| 4214 | ✗ | struct handle h = { | |
| 4215 | .family = NFPROTO_UNSPEC, | ||
| 4216 | }; | ||
| 4217 | ✗ | const char *family = NULL, *table = NULL, *chain = NULL; | |
| 4218 | |||
| 4219 | |||
| 4220 | ✗ | if (obj == CMD_OBJ_RULE && | |
| 4221 | ✗ | json_unpack_err(ctx, root, "{s:s, s:s, s:s, s:I}", | |
| 4222 | "family", &family, "table", &table, | ||
| 4223 | "chain", &chain, "handle", &h.handle.id)) | ||
| 4224 | ✗ | return NULL; | |
| 4225 | ✗ | else if (obj == CMD_OBJ_RULES) { | |
| 4226 | ✗ | json_unpack(root, "{s:s}", "family", &family); | |
| 4227 | ✗ | json_unpack(root, "{s:s}", "table", &table); | |
| 4228 | ✗ | json_unpack(root, "{s:s}", "chain", &chain); | |
| 4229 | } | ||
| 4230 | |||
| 4231 | ✗ | if (family && parse_family(family, &h.family)) { | |
| 4232 | ✗ | json_error(ctx, "Unknown family '%s'.", family); | |
| 4233 | ✗ | return NULL; | |
| 4234 | } | ||
| 4235 | ✗ | if (table) { | |
| 4236 | ✗ | h.table.name = xstrdup(table); | |
| 4237 | ✗ | if (chain) | |
| 4238 | ✗ | h.chain.name = xstrdup(chain); | |
| 4239 | } | ||
| 4240 | ✗ | return cmd_alloc(op, obj, &h, int_loc, NULL); | |
| 4241 | } | ||
| 4242 | |||
| 4243 | ✗ | static struct cmd *json_parse_cmd_reset(struct json_ctx *ctx, | |
| 4244 | json_t *root, enum cmd_ops op) | ||
| 4245 | { | ||
| 4246 | struct { | ||
| 4247 | const char *key; | ||
| 4248 | enum cmd_obj obj; | ||
| 4249 | struct cmd *(*cb)(struct json_ctx *, json_t *, | ||
| 4250 | enum cmd_ops, enum cmd_obj); | ||
| 4251 | ✗ | } cmd_obj_table[] = { | |
| 4252 | { "counter", CMD_OBJ_COUNTER, json_parse_cmd_add_object }, | ||
| 4253 | { "counters", CMD_OBJ_COUNTERS, json_parse_cmd_list_multiple }, | ||
| 4254 | { "quota", CMD_OBJ_QUOTA, json_parse_cmd_add_object }, | ||
| 4255 | { "quotas", CMD_OBJ_QUOTAS, json_parse_cmd_list_multiple }, | ||
| 4256 | { "rule", CMD_OBJ_RULE, json_parse_cmd_reset_rule }, | ||
| 4257 | { "rules", CMD_OBJ_RULES, json_parse_cmd_reset_rule }, | ||
| 4258 | { "element", CMD_OBJ_ELEMENTS, json_parse_cmd_add_element }, | ||
| 4259 | { "set", CMD_OBJ_SET, json_parse_cmd_add_set }, | ||
| 4260 | { "map", CMD_OBJ_MAP, json_parse_cmd_add_set }, | ||
| 4261 | }; | ||
| 4262 | unsigned int i; | ||
| 4263 | json_t *tmp; | ||
| 4264 | |||
| 4265 | ✗ | if (!json_is_object(root)) { | |
| 4266 | ✗ | json_error(ctx, "Value of reset command must be object (got %s instead).", | |
| 4267 | json_typename(root)); | ||
| 4268 | ✗ | return NULL; | |
| 4269 | } | ||
| 4270 | |||
| 4271 | ✗ | for (i = 0; i < array_size(cmd_obj_table); i++) { | |
| 4272 | ✗ | tmp = json_object_get(root, cmd_obj_table[i].key); | |
| 4273 | ✗ | if (!tmp) | |
| 4274 | ✗ | continue; | |
| 4275 | |||
| 4276 | ✗ | return cmd_obj_table[i].cb(ctx, tmp, op, cmd_obj_table[i].obj); | |
| 4277 | } | ||
| 4278 | ✗ | json_error(ctx, "Unknown object passed to reset command."); | |
| 4279 | ✗ | return NULL; | |
| 4280 | } | ||
| 4281 | |||
| 4282 | 3 | static struct cmd *json_parse_cmd_flush(struct json_ctx *ctx, | |
| 4283 | json_t *root, enum cmd_ops op) | ||
| 4284 | { | ||
| 4285 | struct { | ||
| 4286 | const char *key; | ||
| 4287 | enum cmd_obj obj; | ||
| 4288 | struct cmd *(*cb)(struct json_ctx *, json_t *, | ||
| 4289 | enum cmd_ops, enum cmd_obj); | ||
| 4290 | 3 | } cmd_obj_table[] = { | |
| 4291 | { "table", CMD_OBJ_TABLE, json_parse_cmd_add_table }, | ||
| 4292 | { "chain", CMD_OBJ_CHAIN, json_parse_cmd_add_chain }, | ||
| 4293 | { "set", CMD_OBJ_SET, json_parse_cmd_add_set }, | ||
| 4294 | { "map", CMD_OBJ_MAP, json_parse_cmd_add_set }, | ||
| 4295 | { "meter", CMD_OBJ_METER, json_parse_cmd_add_set }, | ||
| 4296 | { "ruleset", CMD_OBJ_RULESET, json_parse_cmd_list_multiple }, | ||
| 4297 | }; | ||
| 4298 | unsigned int i; | ||
| 4299 | json_t *tmp; | ||
| 4300 | |||
| 4301 |
2/4✓ Branch 0 (2→3) taken 3 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→7) taken 3 times.
|
3 | if (!json_is_object(root)) { |
| 4302 | ✗ | json_error(ctx, "Value of flush command must be object (got %s instead).", | |
| 4303 | json_typename(root)); | ||
| 4304 | ✗ | return NULL; | |
| 4305 | } | ||
| 4306 | |||
| 4307 |
1/2✓ Branch 0 (13→8) taken 18 times.
✗ Branch 1 (13→14) not taken.
|
18 | for (i = 0; i < array_size(cmd_obj_table); i++) { |
| 4308 | 18 | tmp = json_object_get(root, cmd_obj_table[i].key); | |
| 4309 |
2/2✓ Branch 0 (9→10) taken 15 times.
✓ Branch 1 (9→11) taken 3 times.
|
18 | if (!tmp) |
| 4310 | 15 | continue; | |
| 4311 | |||
| 4312 | 3 | return cmd_obj_table[i].cb(ctx, tmp, op, cmd_obj_table[i].obj); | |
| 4313 | } | ||
| 4314 | ✗ | json_error(ctx, "Unknown object passed to flush command."); | |
| 4315 | ✗ | return NULL; | |
| 4316 | } | ||
| 4317 | |||
| 4318 | ✗ | static struct cmd *json_parse_cmd_rename(struct json_ctx *ctx, | |
| 4319 | json_t *root, enum cmd_ops op) | ||
| 4320 | { | ||
| 4321 | const char *family, *newname; | ||
| 4322 | ✗ | struct handle h = { 0 }; | |
| 4323 | struct cmd *cmd; | ||
| 4324 | |||
| 4325 | ✗ | if (json_unpack_err(ctx, root, "{s:{s:s, s:s, s:s, s:s}}", "chain", | |
| 4326 | "family", &family, | ||
| 4327 | "table", &h.table.name, | ||
| 4328 | "name", &h.chain.name, | ||
| 4329 | "newname", &newname)) | ||
| 4330 | ✗ | return NULL; | |
| 4331 | ✗ | if (parse_family(family, &h.family)) { | |
| 4332 | ✗ | json_error(ctx, "Unknown family '%s'.", family); | |
| 4333 | ✗ | return NULL; | |
| 4334 | } | ||
| 4335 | ✗ | h.table.name = xstrdup(h.table.name); | |
| 4336 | ✗ | h.chain.name = xstrdup(h.chain.name); | |
| 4337 | |||
| 4338 | ✗ | cmd = cmd_alloc(op, CMD_OBJ_CHAIN, &h, int_loc, NULL); | |
| 4339 | ✗ | cmd->arg = xstrdup(newname); | |
| 4340 | ✗ | return cmd; | |
| 4341 | } | ||
| 4342 | |||
| 4343 | 55287 | static struct cmd *json_parse_cmd(struct json_ctx *ctx, json_t *root) | |
| 4344 | { | ||
| 4345 | struct { | ||
| 4346 | const char *key; | ||
| 4347 | enum cmd_ops op; | ||
| 4348 | struct cmd *(*cb)(struct json_ctx *ctx, json_t *, enum cmd_ops); | ||
| 4349 | 55287 | } parse_cb_table[] = { | |
| 4350 | { "add", CMD_ADD, json_parse_cmd_add }, | ||
| 4351 | { "replace", CMD_REPLACE, json_parse_cmd_replace }, | ||
| 4352 | { "create", CMD_CREATE, json_parse_cmd_add }, | ||
| 4353 | { "insert", CMD_INSERT, json_parse_cmd_replace }, | ||
| 4354 | { "delete", CMD_DELETE, json_parse_cmd_add }, | ||
| 4355 | { "list", CMD_LIST, json_parse_cmd_list }, | ||
| 4356 | { "reset", CMD_RESET, json_parse_cmd_reset }, | ||
| 4357 | { "flush", CMD_FLUSH, json_parse_cmd_flush }, | ||
| 4358 | { "rename", CMD_RENAME, json_parse_cmd_rename }, | ||
| 4359 | { "destroy", CMD_DESTROY, json_parse_cmd_add }, | ||
| 4360 | //{ "export", CMD_EXPORT, json_parse_cmd_export }, | ||
| 4361 | //{ "monitor", CMD_MONITOR, json_parse_cmd_monitor }, | ||
| 4362 | //{ "describe", CMD_DESCRIBE, json_parse_cmd_describe } | ||
| 4363 | }; | ||
| 4364 | uint32_t old_flags; | ||
| 4365 | struct cmd *cmd; | ||
| 4366 | unsigned int i; | ||
| 4367 | json_t *tmp; | ||
| 4368 | |||
| 4369 |
2/2✓ Branch 0 (8→3) taken 469632 times.
✓ Branch 1 (8→9) taken 46022 times.
|
515654 | for (i = 0; i < array_size(parse_cb_table); i++) { |
| 4370 | 469632 | tmp = json_object_get(root, parse_cb_table[i].key); | |
| 4371 |
2/2✓ Branch 0 (4→5) taken 460367 times.
✓ Branch 1 (4→6) taken 9265 times.
|
469632 | if (!tmp) |
| 4372 | 460367 | continue; | |
| 4373 | |||
| 4374 | 9265 | return parse_cb_table[i].cb(ctx, tmp, parse_cb_table[i].op); | |
| 4375 | } | ||
| 4376 | /* to accept 'list ruleset' output 1:1, try add command | ||
| 4377 | * Mark as implicit to distinguish from explicit add commands. | ||
| 4378 | * This allows explicit {"add": {"rule": ...}} to use handle for positioning | ||
| 4379 | * while implicit {"rule": ...} (export format) ignores handles. | ||
| 4380 | */ | ||
| 4381 | 46022 | old_flags = ctx->flags; | |
| 4382 | 46022 | ctx->flags |= CTX_F_IMPLICIT; | |
| 4383 | 46022 | cmd = json_parse_cmd_add(ctx, root, CMD_ADD); | |
| 4384 | 46022 | ctx->flags = old_flags; | |
| 4385 | |||
| 4386 | 46022 | return cmd; | |
| 4387 | } | ||
| 4388 | |||
| 4389 | 966 | static int json_verify_metainfo(struct json_ctx *ctx, json_t *root) | |
| 4390 | { | ||
| 4391 | int schema_version; | ||
| 4392 | |||
| 4393 |
1/2✓ Branch 0 (3→4) taken 966 times.
✗ Branch 1 (3→7) not taken.
|
966 | if (!json_unpack(root, "{s:i}", "json_schema_version", &schema_version)) { |
| 4394 |
2/2✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→7) taken 965 times.
|
966 | if (schema_version > JSON_SCHEMA_VERSION) { |
| 4395 | 1 | json_error(ctx, | |
| 4396 | "Schema version %d not supported, maximum" | ||
| 4397 | " supported version is %d\n", | ||
| 4398 | schema_version, JSON_SCHEMA_VERSION); | ||
| 4399 | 1 | return 1; | |
| 4400 | } | ||
| 4401 | } | ||
| 4402 | |||
| 4403 | 965 | return 0; | |
| 4404 | } | ||
| 4405 | |||
| 4406 | struct json_cmd_assoc { | ||
| 4407 | struct json_cmd_assoc *next; | ||
| 4408 | struct hlist_node hnode; | ||
| 4409 | const struct cmd *cmd; | ||
| 4410 | json_t *json; | ||
| 4411 | }; | ||
| 4412 | |||
| 4413 | #define CMD_ASSOC_HSIZE 512 | ||
| 4414 | static struct hlist_head json_cmd_assoc_hash[CMD_ASSOC_HSIZE]; | ||
| 4415 | static struct json_cmd_assoc *json_cmd_assoc_list; | ||
| 4416 | |||
| 4417 | 17 | static void json_cmd_assoc_free(void) | |
| 4418 | { | ||
| 4419 | struct json_cmd_assoc *cur; | ||
| 4420 | struct hlist_node *pos, *n; | ||
| 4421 | int i; | ||
| 4422 | |||
| 4423 |
2/2✓ Branch 0 (4→3) taken 2 times.
✓ Branch 1 (4→5) taken 17 times.
|
19 | while (json_cmd_assoc_list) { |
| 4424 | 2 | cur = json_cmd_assoc_list->next; | |
| 4425 | 2 | free(json_cmd_assoc_list); | |
| 4426 | 2 | json_cmd_assoc_list = cur; | |
| 4427 | } | ||
| 4428 | |||
| 4429 |
2/2✓ Branch 0 (13→6) taken 8704 times.
✓ Branch 1 (13→14) taken 17 times.
|
8721 | for (i = 0; i < CMD_ASSOC_HSIZE; i++) { |
| 4430 |
4/6✓ Branch 0 (9→10) taken 3396 times.
✓ Branch 1 (9→12) taken 8704 times.
✓ Branch 2 (10→11) taken 3396 times.
✗ Branch 3 (10→12) not taken.
✓ Branch 4 (11→7) taken 3396 times.
✗ Branch 5 (11→12) not taken.
|
12100 | hlist_for_each_entry_safe(cur, pos, n, |
| 4431 | &json_cmd_assoc_hash[i], hnode) { | ||
| 4432 | 3396 | hlist_del(&cur->hnode); | |
| 4433 | 3396 | free(cur); | |
| 4434 | } | ||
| 4435 | } | ||
| 4436 | 17 | } | |
| 4437 | |||
| 4438 | 3398 | static void json_cmd_assoc_add(json_t *json, const struct cmd *cmd) | |
| 4439 | { | ||
| 4440 | 3398 | struct json_cmd_assoc *new = xzalloc(sizeof *new); | |
| 4441 | |||
| 4442 | 3398 | new->json = json; | |
| 4443 | 3398 | new->cmd = cmd; | |
| 4444 | 3398 | new->next = json_cmd_assoc_list; | |
| 4445 | |||
| 4446 | 3398 | json_cmd_assoc_list = new; | |
| 4447 | 3398 | } | |
| 4448 | |||
| 4449 | 3396 | static json_t *seqnum_to_json(const uint32_t seqnum) | |
| 4450 | { | ||
| 4451 | struct json_cmd_assoc *cur; | ||
| 4452 | struct hlist_node *n; | ||
| 4453 | int key; | ||
| 4454 | |||
| 4455 |
2/2✓ Branch 0 (4→3) taken 3396 times.
✓ Branch 1 (4→5) taken 3396 times.
|
6792 | while (json_cmd_assoc_list) { |
| 4456 | 3396 | cur = json_cmd_assoc_list; | |
| 4457 | 3396 | json_cmd_assoc_list = cur->next; | |
| 4458 | |||
| 4459 | 3396 | key = cur->cmd->seqnum_from % CMD_ASSOC_HSIZE; | |
| 4460 | 3396 | hlist_add_head(&cur->hnode, &json_cmd_assoc_hash[key]); | |
| 4461 | } | ||
| 4462 | |||
| 4463 | 3396 | key = seqnum % CMD_ASSOC_HSIZE; | |
| 4464 |
3/6✓ Branch 0 (9→10) taken 10716 times.
✗ Branch 1 (9→12) not taken.
✓ Branch 2 (10→11) taken 10716 times.
✗ Branch 3 (10→12) not taken.
✓ Branch 4 (11→6) taken 10716 times.
✗ Branch 5 (11→12) not taken.
|
10716 | hlist_for_each_entry(cur, n, &json_cmd_assoc_hash[key], hnode) { |
| 4465 |
2/2✓ Branch 0 (6→7) taken 3396 times.
✓ Branch 1 (6→8) taken 7320 times.
|
10716 | if (cur->cmd->seqnum_from == seqnum) |
| 4466 | 3396 | return cur->json; | |
| 4467 | } | ||
| 4468 | |||
| 4469 | ✗ | return NULL; | |
| 4470 | } | ||
| 4471 | |||
| 4472 | − | static int __json_parse(struct json_ctx *ctx) | |
| 4473 | { | ||
| 4474 | json_t *tmp, *value; | ||
| 4475 | size_t index; | ||
| 4476 | |||
| 4477 | − | if (json_unpack_err(ctx, ctx->nft->json_root, | |
| 4478 | "{s:o}", "nftables", &tmp)) | ||
| 4479 | − | return -1; | |
| 4480 | |||
| 4481 | − | if (!json_is_array(tmp)) { | |
| 4482 | − | json_error(ctx, "Value of property \"nftables\" must be an array."); | |
| 4483 | − | return -1; | |
| 4484 | } | ||
| 4485 | |||
| 4486 | − | json_array_foreach(tmp, index, value) { | |
| 4487 | /* this is more or less from parser_bison.y:716 */ | ||
| 4488 | − | LIST_HEAD(list); | |
| 4489 | struct cmd *cmd; | ||
| 4490 | json_t *tmp2; | ||
| 4491 | |||
| 4492 | − | if (!json_is_object(value)) { | |
| 4493 | − | json_error(ctx, "Unexpected command array element of type %s, expected object.", json_typename(value)); | |
| 4494 | − | return -1; | |
| 4495 | } | ||
| 4496 | |||
| 4497 | − | tmp2 = json_object_get(value, "metainfo"); | |
| 4498 | − | if (tmp2) { | |
| 4499 | − | if (json_verify_metainfo(ctx, tmp2)) { | |
| 4500 | − | json_error(ctx, "Metainfo verification failed."); | |
| 4501 | − | return -1; | |
| 4502 | } | ||
| 4503 | − | continue; | |
| 4504 | } | ||
| 4505 | |||
| 4506 | − | cmd = json_parse_cmd(ctx, value); | |
| 4507 | |||
| 4508 | − | if (!cmd) { | |
| 4509 | − | if (ctx->flags & CTX_F_COLLAPSED) { | |
| 4510 | − | ctx->flags &= ~CTX_F_COLLAPSED; | |
| 4511 | − | continue; | |
| 4512 | } | ||
| 4513 | |||
| 4514 | − | json_error(ctx, "Parsing command array at index %zd failed.", index); | |
| 4515 | − | return -1; | |
| 4516 | } | ||
| 4517 | |||
| 4518 | − | list_add_tail(&cmd->list, &list); | |
| 4519 | |||
| 4520 | − | list_splice_tail(&list, ctx->cmds); | |
| 4521 | |||
| 4522 | − | if (nft_output_echo(&ctx->nft->output)) | |
| 4523 | − | json_cmd_assoc_add(value, cmd); | |
| 4524 | } | ||
| 4525 | |||
| 4526 | − | return 0; | |
| 4527 | } | ||
| 4528 | |||
| 4529 | 12315 | int nft_parse_json_buffer(struct nft_ctx *nft, const char *buf, | |
| 4530 | struct list_head *msgs, struct list_head *cmds) | ||
| 4531 | { | ||
| 4532 | 12315 | struct json_ctx ctx = { | |
| 4533 | .nft = nft, | ||
| 4534 | .msgs = msgs, | ||
| 4535 | .cmds = cmds, | ||
| 4536 | }; | ||
| 4537 | int ret; | ||
| 4538 | |||
| 4539 | 12315 | json_indesc.type = INDESC_BUFFER; | |
| 4540 | 12315 | json_indesc.data = buf; | |
| 4541 | |||
| 4542 | 12315 | parser_init(nft, nft->state, msgs, cmds, nft->top_scope); | |
| 4543 | 12315 | nft->json_root = json_loads(buf, 0, NULL); | |
| 4544 |
2/2✓ Branch 0 (4→5) taken 6403 times.
✓ Branch 1 (4→6) taken 5912 times.
|
12315 | if (!nft->json_root) |
| 4545 | 6403 | return -EINVAL; | |
| 4546 | |||
| 4547 | 5912 | ret = __json_parse(&ctx); | |
| 4548 | |||
| 4549 |
2/2✓ Branch 0 (8→9) taken 5895 times.
✓ Branch 1 (8→11) taken 17 times.
|
5912 | if (!nft_output_echo(&nft->output)) { |
| 4550 | 5895 | json_decref(nft->json_root); | |
| 4551 | 5895 | nft->json_root = NULL; | |
| 4552 | } | ||
| 4553 | 5912 | return ret; | |
| 4554 | } | ||
| 4555 | |||
| 4556 | 1082 | int nft_parse_json_filename(struct nft_ctx *nft, const char *filename, | |
| 4557 | struct list_head *msgs, struct list_head *cmds) | ||
| 4558 | { | ||
| 4559 | 1082 | struct json_ctx ctx = { | |
| 4560 | .nft = nft, | ||
| 4561 | .msgs = msgs, | ||
| 4562 | .cmds = cmds, | ||
| 4563 | }; | ||
| 4564 | json_error_t err; | ||
| 4565 | int ret; | ||
| 4566 | |||
| 4567 |
2/2✓ Branch 0 (2→3) taken 115 times.
✓ Branch 1 (2→5) taken 967 times.
|
1082 | if (nft->stdin_buf) { |
| 4568 | 115 | json_indesc.type = INDESC_STDIN; | |
| 4569 | 115 | json_indesc.name = "/dev/stdin"; | |
| 4570 | |||
| 4571 | 115 | return nft_parse_json_buffer(nft, nft->stdin_buf, msgs, cmds); | |
| 4572 | } | ||
| 4573 | |||
| 4574 | 967 | json_indesc.type = INDESC_FILE; | |
| 4575 | 967 | json_indesc.name = filename; | |
| 4576 | |||
| 4577 | 967 | parser_init(nft, nft->state, msgs, cmds, nft->top_scope); | |
| 4578 | 967 | nft->json_root = json_load_file(filename, 0, &err); | |
| 4579 |
2/2✓ Branch 0 (7→8) taken 4 times.
✓ Branch 1 (7→9) taken 963 times.
|
967 | if (!nft->json_root) |
| 4580 | 4 | return -EINVAL; | |
| 4581 | |||
| 4582 | 963 | ret = __json_parse(&ctx); | |
| 4583 | |||
| 4584 |
1/2✓ Branch 0 (11→12) taken 963 times.
✗ Branch 1 (11→14) not taken.
|
963 | if (!nft_output_echo(&nft->output)) { |
| 4585 | 963 | json_decref(nft->json_root); | |
| 4586 | 963 | nft->json_root = NULL; | |
| 4587 | } | ||
| 4588 | 963 | return ret; | |
| 4589 | } | ||
| 4590 | |||
| 4591 | __attribute__((format(printf, 2, 3))) | ||
| 4592 | ✗ | static int json_echo_error(struct netlink_mon_handler *monh, | |
| 4593 | const char *fmt, ...) | ||
| 4594 | { | ||
| 4595 | struct error_record *erec; | ||
| 4596 | va_list ap; | ||
| 4597 | |||
| 4598 | ✗ | va_start(ap, fmt); | |
| 4599 | ✗ | erec = erec_vcreate(EREC_ERROR, int_loc, fmt, ap); | |
| 4600 | ✗ | va_end(ap); | |
| 4601 | ✗ | erec_queue(erec, monh->ctx->msgs); | |
| 4602 | |||
| 4603 | ✗ | return MNL_CB_ERROR; | |
| 4604 | } | ||
| 4605 | |||
| 4606 | 3482 | static uint64_t handle_from_nlmsg(const struct nlmsghdr *nlh) | |
| 4607 | { | ||
| 4608 | struct nftnl_flowtable *nlf; | ||
| 4609 | struct nftnl_table *nlt; | ||
| 4610 | struct nftnl_chain *nlc; | ||
| 4611 | struct nftnl_rule *nlr; | ||
| 4612 | struct nftnl_set *nls; | ||
| 4613 | struct nftnl_obj *nlo; | ||
| 4614 | 3482 | uint64_t handle = 0; | |
| 4615 | uint32_t flags; | ||
| 4616 | |||
| 4617 |
6/7✓ Branch 0 (2→3) taken 6 times.
✓ Branch 1 (2→7) taken 183 times.
✓ Branch 2 (2→11) taken 3200 times.
✓ Branch 3 (2→15) taken 16 times.
✓ Branch 4 (2→22) taken 4 times.
✗ Branch 5 (2→26) not taken.
✓ Branch 6 (2→30) taken 73 times.
|
3482 | switch (NFNL_MSG_TYPE(nlh->nlmsg_type)) { |
| 4618 | 6 | case NFT_MSG_NEWTABLE: | |
| 4619 | 6 | nlt = netlink_table_alloc(nlh); | |
| 4620 | 6 | handle = nftnl_table_get_u64(nlt, NFTNL_TABLE_HANDLE); | |
| 4621 | 6 | nftnl_table_free(nlt); | |
| 4622 | 6 | break; | |
| 4623 | 183 | case NFT_MSG_NEWCHAIN: | |
| 4624 | 183 | nlc = netlink_chain_alloc(nlh); | |
| 4625 | 183 | handle = nftnl_chain_get_u64(nlc, NFTNL_CHAIN_HANDLE); | |
| 4626 | 183 | nftnl_chain_free(nlc); | |
| 4627 | 183 | break; | |
| 4628 | 3200 | case NFT_MSG_NEWRULE: | |
| 4629 | 3200 | nlr = netlink_rule_alloc(nlh); | |
| 4630 | 3200 | handle = nftnl_rule_get_u64(nlr, NFTNL_RULE_HANDLE); | |
| 4631 | 3200 | nftnl_rule_free(nlr); | |
| 4632 | 3200 | break; | |
| 4633 | 16 | case NFT_MSG_NEWSET: | |
| 4634 | 16 | nls = netlink_set_alloc(nlh); | |
| 4635 | 16 | flags = nftnl_set_get_u32(nls, NFTNL_SET_FLAGS); | |
| 4636 |
2/2✓ Branch 0 (18→19) taken 3 times.
✓ Branch 1 (18→20) taken 13 times.
|
16 | if (!set_is_anonymous(flags)) |
| 4637 | 3 | handle = nftnl_set_get_u64(nls, NFTNL_SET_HANDLE); | |
| 4638 | 16 | nftnl_set_free(nls); | |
| 4639 | 16 | break; | |
| 4640 | 4 | case NFT_MSG_NEWOBJ: | |
| 4641 | 4 | nlo = netlink_obj_alloc(nlh); | |
| 4642 | 4 | handle = nftnl_obj_get_u64(nlo, NFTNL_OBJ_HANDLE); | |
| 4643 | 4 | nftnl_obj_free(nlo); | |
| 4644 | 4 | break; | |
| 4645 | ✗ | case NFT_MSG_NEWFLOWTABLE: | |
| 4646 | ✗ | nlf = netlink_flowtable_alloc(nlh); | |
| 4647 | ✗ | handle = nftnl_flowtable_get_u64(nlf, NFTNL_FLOWTABLE_HANDLE); | |
| 4648 | ✗ | nftnl_flowtable_free(nlf); | |
| 4649 | ✗ | break; | |
| 4650 | } | ||
| 4651 | 3482 | return handle; | |
| 4652 | } | ||
| 4653 | 3482 | int json_events_cb(const struct nlmsghdr *nlh, struct netlink_mon_handler *monh) | |
| 4654 | { | ||
| 4655 | 3482 | uint64_t handle = handle_from_nlmsg(nlh); | |
| 4656 | json_t *tmp, *json; | ||
| 4657 | void *iter; | ||
| 4658 | |||
| 4659 |
2/2✓ Branch 0 (3→4) taken 86 times.
✓ Branch 1 (3→5) taken 3396 times.
|
3482 | if (!handle) |
| 4660 | 86 | return MNL_CB_OK; | |
| 4661 | |||
| 4662 | 3396 | json = seqnum_to_json(nlh->nlmsg_seq); | |
| 4663 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 3396 times.
|
3396 | if (!json) { |
| 4664 | ✗ | json_echo_error(monh, "No JSON command found with seqnum %u\n", | |
| 4665 | ✗ | nlh->nlmsg_seq); | |
| 4666 | ✗ | return MNL_CB_OK; | |
| 4667 | } | ||
| 4668 | |||
| 4669 | 3396 | tmp = json_object_get(json, "add"); | |
| 4670 |
2/2✓ Branch 0 (10→11) taken 20 times.
✓ Branch 1 (10→12) taken 3376 times.
|
3396 | if (!tmp) |
| 4671 | 20 | tmp = json_object_get(json, "insert"); | |
| 4672 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 3396 times.
|
3396 | if (!tmp) |
| 4673 | /* assume loading JSON dump */ | ||
| 4674 | ✗ | tmp = json; | |
| 4675 | |||
| 4676 | 3396 | iter = json_object_iter(tmp); | |
| 4677 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→18) taken 3396 times.
|
3396 | if (!iter) { |
| 4678 | ✗ | json_echo_error(monh, "Empty JSON object in cmd list\n"); | |
| 4679 | ✗ | return MNL_CB_OK; | |
| 4680 | } | ||
| 4681 | 3396 | json = json_object_iter_value(iter); | |
| 4682 |
3/6✓ Branch 0 (19→20) taken 3396 times.
✗ Branch 1 (19→23) not taken.
✓ Branch 2 (20→21) taken 3396 times.
✗ Branch 3 (20→23) not taken.
✗ Branch 4 (22→23) not taken.
✓ Branch 5 (22→25) taken 3396 times.
|
3396 | if (!json_is_object(json) || json_object_iter_next(tmp, iter)) { |
| 4683 | ✗ | json_echo_error(monh, "Malformed JSON object in cmd list\n"); | |
| 4684 | ✗ | return MNL_CB_OK; | |
| 4685 | } | ||
| 4686 | |||
| 4687 | 3396 | json_object_set_new(json, "handle", json_integer(handle)); | |
| 4688 | 3396 | return MNL_CB_OK; | |
| 4689 | } | ||
| 4690 | |||
| 4691 | 79 | void json_print_echo(struct nft_ctx *ctx) | |
| 4692 | { | ||
| 4693 |
2/2✓ Branch 0 (2→3) taken 62 times.
✓ Branch 1 (2→10) taken 17 times.
|
79 | if (!ctx->json_root) { |
| 4694 |
2/2✓ Branch 0 (3→4) taken 19 times.
✓ Branch 1 (3→5) taken 43 times.
|
62 | if (!ctx->json_echo) |
| 4695 | 19 | return; | |
| 4696 | |||
| 4697 | 43 | ctx->json_echo = json_pack("{s:o}", "nftables", ctx->json_echo); | |
| 4698 | 43 | json_dumpf(ctx->json_echo, ctx->output.output_fp, JSON_PRESERVE_ORDER); | |
| 4699 | 43 | json_decref(ctx->json_echo); | |
| 4700 | 43 | ctx->json_echo = NULL; | |
| 4701 | 43 | fprintf(ctx->output.output_fp, "\n"); | |
| 4702 | 43 | fflush(ctx->output.output_fp); | |
| 4703 | } else { | ||
| 4704 | 17 | json_dumpf(ctx->json_root, ctx->output.output_fp, JSON_PRESERVE_ORDER); | |
| 4705 | 17 | json_cmd_assoc_free(); | |
| 4706 | 17 | json_decref(ctx->json_root); | |
| 4707 | 17 | ctx->json_root = NULL; | |
| 4708 | } | ||
| 4709 | } | ||
| 4710 |