| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 as | ||
| 6 | * published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <nft.h> | ||
| 12 | |||
| 13 | #include <inttypes.h> | ||
| 14 | #include <ctype.h> /* isdigit */ | ||
| 15 | #include <errno.h> | ||
| 16 | #include <netdb.h> | ||
| 17 | #include <arpa/inet.h> | ||
| 18 | #include <linux/types.h> | ||
| 19 | #include <linux/netfilter.h> | ||
| 20 | #include <linux/icmpv6.h> | ||
| 21 | #include <dirent.h> | ||
| 22 | #include <sys/stat.h> | ||
| 23 | |||
| 24 | #include <nftables.h> | ||
| 25 | #include <datatype.h> | ||
| 26 | #include <expression.h> | ||
| 27 | #include <gmputil.h> | ||
| 28 | #include <erec.h> | ||
| 29 | #include <netlink.h> | ||
| 30 | #include <json.h> | ||
| 31 | #include <misspell.h> | ||
| 32 | #include "nftutils.h" | ||
| 33 | |||
| 34 | #include <netinet/ip_icmp.h> | ||
| 35 | |||
| 36 | static const struct datatype *datatypes[TYPE_MAX + 1] = { | ||
| 37 | [TYPE_INVALID] = &invalid_type, | ||
| 38 | [TYPE_VERDICT] = &verdict_type, | ||
| 39 | [TYPE_NFPROTO] = &nfproto_type, | ||
| 40 | [TYPE_BITMASK] = &bitmask_type, | ||
| 41 | [TYPE_INTEGER] = &integer_type, | ||
| 42 | [TYPE_STRING] = &string_type, | ||
| 43 | [TYPE_LLADDR] = &lladdr_type, | ||
| 44 | [TYPE_IPADDR] = &ipaddr_type, | ||
| 45 | [TYPE_IP6ADDR] = &ip6addr_type, | ||
| 46 | [TYPE_ETHERADDR] = ðeraddr_type, | ||
| 47 | [TYPE_ETHERTYPE] = ðertype_type, | ||
| 48 | [TYPE_ARPOP] = &arpop_type, | ||
| 49 | [TYPE_INET_PROTOCOL] = &inet_protocol_type, | ||
| 50 | [TYPE_INET_SERVICE] = &inet_service_type, | ||
| 51 | [TYPE_ICMP_TYPE] = &icmp_type_type, | ||
| 52 | [TYPE_TCP_FLAG] = &tcp_flag_type, | ||
| 53 | [TYPE_DCCP_PKTTYPE] = &dccp_pkttype_type, | ||
| 54 | [TYPE_MH_TYPE] = &mh_type_type, | ||
| 55 | [TYPE_TIME] = &time_type, | ||
| 56 | [TYPE_MARK] = &mark_type, | ||
| 57 | [TYPE_IFINDEX] = &ifindex_type, | ||
| 58 | [TYPE_ARPHRD] = &arphrd_type, | ||
| 59 | [TYPE_REALM] = &realm_type, | ||
| 60 | [TYPE_CLASSID] = &tchandle_type, | ||
| 61 | [TYPE_UID] = &uid_type, | ||
| 62 | [TYPE_GID] = &gid_type, | ||
| 63 | [TYPE_CT_STATE] = &ct_state_type, | ||
| 64 | [TYPE_CT_DIR] = &ct_dir_type, | ||
| 65 | [TYPE_CT_STATUS] = &ct_status_type, | ||
| 66 | [TYPE_ICMP6_TYPE] = &icmp6_type_type, | ||
| 67 | [TYPE_CT_LABEL] = &ct_label_type, | ||
| 68 | [TYPE_PKTTYPE] = &pkttype_type, | ||
| 69 | [TYPE_ICMP_CODE] = &icmp_code_type, | ||
| 70 | [TYPE_ICMPV6_CODE] = &icmpv6_code_type, | ||
| 71 | [TYPE_ICMPX_CODE] = &icmpx_code_type, | ||
| 72 | [TYPE_DEVGROUP] = &devgroup_type, | ||
| 73 | [TYPE_DSCP] = &dscp_type, | ||
| 74 | [TYPE_ECN] = &ecn_type, | ||
| 75 | [TYPE_FIB_ADDR] = &fib_addr_type, | ||
| 76 | [TYPE_BOOLEAN] = &boolean_type, | ||
| 77 | [TYPE_CT_EVENTBIT] = &ct_event_type, | ||
| 78 | [TYPE_IFNAME] = &ifname_type, | ||
| 79 | [TYPE_IGMP_TYPE] = &igmp_type_type, | ||
| 80 | [TYPE_TIME_DATE] = &date_type, | ||
| 81 | [TYPE_TIME_HOUR] = &hour_type, | ||
| 82 | [TYPE_TIME_DAY] = &day_type, | ||
| 83 | [TYPE_CGROUPV2] = &cgroupv2_type, | ||
| 84 | }; | ||
| 85 | |||
| 86 | 302050 | const struct datatype *datatype_lookup(enum datatypes type) | |
| 87 | { | ||
| 88 | BUILD_BUG_ON(TYPE_MAX & ~TYPE_MASK); | ||
| 89 | |||
| 90 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 302050 times.
|
302050 | if (type > TYPE_MAX) |
| 91 | ✗ | return NULL; | |
| 92 | 302050 | return datatypes[type]; | |
| 93 | } | ||
| 94 | |||
| 95 | 2621 | const struct datatype *datatype_lookup_byname(const char *name) | |
| 96 | { | ||
| 97 | const struct datatype *dtype; | ||
| 98 | enum datatypes type; | ||
| 99 | |||
| 100 |
2/2✓ Branch 0 (8→3) taken 31762 times.
✓ Branch 1 (8→9) taken 3 times.
|
31765 | for (type = TYPE_INVALID; type <= TYPE_MAX; type++) { |
| 101 | 31762 | dtype = datatypes[type]; | |
| 102 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 31762 times.
|
31762 | if (dtype == NULL) |
| 103 | ✗ | continue; | |
| 104 |
2/2✓ Branch 0 (5→6) taken 2618 times.
✓ Branch 1 (5→7) taken 29144 times.
|
31762 | if (!strcmp(dtype->name, name)) |
| 105 | 2618 | return dtype; | |
| 106 | } | ||
| 107 | 3 | return NULL; | |
| 108 | } | ||
| 109 | |||
| 110 | 323889 | void datatype_print(const struct expr *expr, struct output_ctx *octx) | |
| 111 | { | ||
| 112 | 323889 | const struct datatype *dtype = expr->dtype; | |
| 113 | |||
| 114 | do { | ||
| 115 |
2/2✓ Branch 0 (3→4) taken 321470 times.
✓ Branch 1 (3→6) taken 24639 times.
|
346109 | if (dtype->print != NULL) |
| 116 | 321470 | return dtype->print(expr, octx); | |
| 117 |
2/2✓ Branch 0 (6→7) taken 2419 times.
✓ Branch 1 (6→9) taken 22220 times.
|
24639 | if (dtype->sym_tbl != NULL) |
| 118 | 2419 | return symbolic_constant_print(dtype->sym_tbl, expr, | |
| 119 | false, octx); | ||
| 120 |
1/2✓ Branch 0 (9→10) taken 22220 times.
✗ Branch 1 (9→11) not taken.
|
22220 | } while ((dtype = dtype->basetype)); |
| 121 | |||
| 122 | ✗ | BUG("datatype %s has no print method or symbol table", | |
| 123 | expr->dtype->name); | ||
| 124 | } | ||
| 125 | |||
| 126 | 157309 | bool datatype_prefix_notation(const struct datatype *dtype) | |
| 127 | { | ||
| 128 |
4/4✓ Branch 0 (2→3) taken 7503 times.
✓ Branch 1 (2→4) taken 149806 times.
✓ Branch 2 (3→4) taken 6026 times.
✓ Branch 3 (3→5) taken 1477 times.
|
157309 | return dtype->type == TYPE_IPADDR || dtype->type == TYPE_IP6ADDR; |
| 129 | } | ||
| 130 | |||
| 131 | 1593650 | struct error_record *symbol_parse(struct parse_ctx *ctx, const struct expr *sym, | |
| 132 | struct expr **res) | ||
| 133 | { | ||
| 134 | 1593650 | const struct datatype *dtype = sym->dtype; | |
| 135 | struct error_record *erec; | ||
| 136 | |||
| 137 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1593650 times.
|
1593650 | assert(sym->etype == EXPR_SYMBOL); |
| 138 | |||
| 139 |
2/2✓ Branch 0 (4→5) taken 4 times.
✓ Branch 1 (4→7) taken 1593646 times.
|
1593650 | if (dtype == NULL) |
| 140 | 4 | return error(&sym->location, "No symbol type information"); | |
| 141 | do { | ||
| 142 |
2/2✓ Branch 0 (8→9) taken 1586860 times.
✓ Branch 1 (8→11) taken 92784 times.
|
1679644 | if (dtype->parse != NULL) |
| 143 | 1586860 | return dtype->parse(ctx, sym, res); | |
| 144 |
2/2✓ Branch 0 (11→12) taken 6784 times.
✓ Branch 1 (11→14) taken 86000 times.
|
92784 | if (dtype->sym_tbl != NULL) |
| 145 | 6784 | return symbolic_constant_parse(ctx, sym, dtype->sym_tbl, | |
| 146 | res); | ||
| 147 |
2/2✓ Branch 0 (14→15) taken 85998 times.
✓ Branch 1 (14→16) taken 2 times.
|
86000 | } while ((dtype = dtype->basetype)); |
| 148 | |||
| 149 | 2 | dtype = sym->dtype; | |
| 150 |
2/2✓ Branch 0 (16→17) taken 1 times.
✓ Branch 1 (16→20) taken 1 times.
|
2 | if (dtype->err) { |
| 151 | 1 | erec = dtype->err(sym); | |
| 152 |
1/2✓ Branch 0 (18→19) taken 1 times.
✗ Branch 1 (18→20) not taken.
|
1 | if (erec) |
| 153 | 1 | return erec; | |
| 154 | } | ||
| 155 | |||
| 156 | 1 | return error(&sym->location, "Could not parse symbolic %s expression", | |
| 157 | sym->dtype->desc); | ||
| 158 | } | ||
| 159 | |||
| 160 | − | static struct error_record *__symbol_parse_fuzzy(const struct expr *sym, | |
| 161 | const struct symbol_table *tbl) | ||
| 162 | { | ||
| 163 | const struct symbolic_constant *s; | ||
| 164 | struct string_misspell_state st; | ||
| 165 | |||
| 166 | − | string_misspell_init(&st); | |
| 167 | |||
| 168 | − | for (s = tbl->symbols; s->identifier != NULL; s++) { | |
| 169 | − | string_misspell_update(sym->identifier, s->identifier, | |
| 170 | − | (void *)s->identifier, &st); | |
| 171 | } | ||
| 172 | |||
| 173 | − | if (st.obj) { | |
| 174 | − | return error(&sym->location, | |
| 175 | "Could not parse %s expression; did you you mean `%s`?", | ||
| 176 | sym->dtype->desc, (const char *)st.obj); | ||
| 177 | } | ||
| 178 | |||
| 179 | − | return NULL; | |
| 180 | } | ||
| 181 | |||
| 182 | 34 | static struct error_record *symbol_parse_fuzzy(const struct expr *sym, | |
| 183 | const struct symbol_table *tbl) | ||
| 184 | { | ||
| 185 | struct error_record *erec; | ||
| 186 | |||
| 187 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 34 times.
|
34 | if (!tbl) |
| 188 | ✗ | return NULL; | |
| 189 | |||
| 190 | 34 | erec = __symbol_parse_fuzzy(sym, tbl); | |
| 191 |
2/2✓ Branch 0 (5→6) taken 16 times.
✓ Branch 1 (5→7) taken 18 times.
|
34 | if (erec) |
| 192 | 16 | return erec; | |
| 193 | |||
| 194 | 18 | return NULL; | |
| 195 | } | ||
| 196 | |||
| 197 | 19656 | struct error_record *symbolic_constant_parse(struct parse_ctx *ctx, | |
| 198 | const struct expr *sym, | ||
| 199 | const struct symbol_table *tbl, | ||
| 200 | struct expr **res) | ||
| 201 | { | ||
| 202 | const struct symbolic_constant *s; | ||
| 203 | const struct datatype *dtype; | ||
| 204 | struct error_record *erec; | ||
| 205 | |||
| 206 |
2/2✓ Branch 0 (6→3) taken 34714 times.
✓ Branch 1 (6→7) taken 13024 times.
|
47738 | for (s = tbl->symbols; s->identifier != NULL; s++) { |
| 207 |
2/2✓ Branch 0 (3→4) taken 6632 times.
✓ Branch 1 (3→5) taken 28082 times.
|
34714 | if (!strcmp(sym->identifier, s->identifier)) |
| 208 | 6632 | break; | |
| 209 | } | ||
| 210 | |||
| 211 |
2/2✓ Branch 0 (7→8) taken 6632 times.
✓ Branch 1 (7→9) taken 13024 times.
|
19656 | if (s->identifier != NULL) |
| 212 | 6632 | goto out; | |
| 213 | |||
| 214 | 13024 | dtype = sym->dtype; | |
| 215 | 13024 | *res = NULL; | |
| 216 | do { | ||
| 217 |
2/2✓ Branch 0 (10→11) taken 13024 times.
✓ Branch 1 (10→21) taken 114 times.
|
13138 | if (dtype->basetype->parse) { |
| 218 | 13024 | erec = dtype->basetype->parse(ctx, sym, res); | |
| 219 |
2/2✓ Branch 0 (12→13) taken 34 times.
✓ Branch 1 (12→18) taken 12990 times.
|
13024 | if (erec != NULL) { |
| 220 | struct error_record *fuzzy_erec; | ||
| 221 | |||
| 222 | 34 | fuzzy_erec = symbol_parse_fuzzy(sym, tbl); | |
| 223 |
2/2✓ Branch 0 (14→15) taken 18 times.
✓ Branch 1 (14→16) taken 16 times.
|
34 | if (!fuzzy_erec) |
| 224 | 18 | return erec; | |
| 225 | |||
| 226 | 16 | erec_destroy(erec); | |
| 227 | 16 | return fuzzy_erec; | |
| 228 | } | ||
| 229 |
1/2✓ Branch 0 (18→19) taken 12990 times.
✗ Branch 1 (18→20) not taken.
|
12990 | if (*res) |
| 230 | 12990 | return NULL; | |
| 231 | ✗ | goto out; | |
| 232 | } | ||
| 233 |
1/2✓ Branch 0 (21→10) taken 114 times.
✗ Branch 1 (21→22) not taken.
|
114 | } while ((dtype = dtype->basetype)); |
| 234 | |||
| 235 | ✗ | return error(&sym->location, "Could not parse %s", sym->dtype->desc); | |
| 236 | 6632 | out: | |
| 237 | 13264 | *res = constant_expr_alloc(&sym->location, sym->dtype, | |
| 238 | 6632 | sym->dtype->byteorder, sym->dtype->size, | |
| 239 | 6632 | constant_data_ptr(s->value, | |
| 240 | sym->dtype->size)); | ||
| 241 | 6632 | return NULL; | |
| 242 | } | ||
| 243 | |||
| 244 | 5260 | void symbolic_constant_print(const struct symbol_table *tbl, | |
| 245 | const struct expr *expr, bool quotes, | ||
| 246 | struct output_ctx *octx) | ||
| 247 | { | ||
| 248 | 5260 | unsigned int len = div_round_up(expr->len, BITS_PER_BYTE); | |
| 249 | const struct symbolic_constant *s; | ||
| 250 | 5260 | uint64_t val = 0; | |
| 251 | |||
| 252 | /* Export the data in the correct byteorder for comparison */ | ||
| 253 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 5260 times.
|
5260 | assert(expr->len / BITS_PER_BYTE <= sizeof(val)); |
| 254 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 5260 times.
|
5260 | mpz_export_data(constant_data_ptr(val, expr->len), expr->value, |
| 255 | expr->byteorder, len); | ||
| 256 | |||
| 257 |
3/4✓ Branch 0 (8→9) taken 4456 times.
✓ Branch 1 (8→10) taken 804 times.
✗ Branch 2 (9→10) not taken.
✓ Branch 3 (9→11) taken 4456 times.
|
5260 | if (nft_output_numeric_symbol(octx) || !tbl) |
| 258 | 804 | goto basetype_print; | |
| 259 | |||
| 260 |
2/2✓ Branch 0 (15→12) taken 12614 times.
✓ Branch 1 (15→16) taken 1765 times.
|
14379 | for (s = tbl->symbols; s->identifier != NULL; s++) { |
| 261 |
2/2✓ Branch 0 (12→13) taken 2691 times.
✓ Branch 1 (12→14) taken 9923 times.
|
12614 | if (val == s->value) |
| 262 | 2691 | break; | |
| 263 | } | ||
| 264 |
2/2✓ Branch 0 (16→17) taken 2691 times.
✓ Branch 1 (16→22) taken 1765 times.
|
4456 | if (s->identifier) { |
| 265 |
2/2✓ Branch 0 (17→18) taken 118 times.
✓ Branch 1 (17→19) taken 2573 times.
|
2691 | nft_print(octx, quotes ? "\"%s\"" : "%s", s->identifier); |
| 266 | 2691 | return; | |
| 267 | } | ||
| 268 | 1765 | basetype_print: | |
| 269 | 2569 | expr_basetype(expr)->print(expr, octx); | |
| 270 | } | ||
| 271 | |||
| 272 | ✗ | static void switch_byteorder(void *data, unsigned int len) | |
| 273 | { | ||
| 274 | mpz_t op; | ||
| 275 | |||
| 276 | ✗ | mpz_init(op); | |
| 277 | ✗ | mpz_import_data(op, data, BYTEORDER_BIG_ENDIAN, len); | |
| 278 | ✗ | mpz_export_data(data, op, BYTEORDER_HOST_ENDIAN, len); | |
| 279 | ✗ | mpz_clear(op); | |
| 280 | ✗ | } | |
| 281 | |||
| 282 | ✗ | void symbol_table_print(const struct symbol_table *tbl, | |
| 283 | const struct datatype *dtype, | ||
| 284 | enum byteorder byteorder, | ||
| 285 | struct output_ctx *octx) | ||
| 286 | { | ||
| 287 | ✗ | unsigned int len = div_round_up(dtype->size, BITS_PER_BYTE); | |
| 288 | const struct symbolic_constant *s; | ||
| 289 | uint64_t value; | ||
| 290 | |||
| 291 | ✗ | for (s = tbl->symbols; s->identifier != NULL; s++) { | |
| 292 | ✗ | value = s->value; | |
| 293 | |||
| 294 | ✗ | if (byteorder == BYTEORDER_BIG_ENDIAN) | |
| 295 | ✗ | switch_byteorder(&value, len); | |
| 296 | |||
| 297 | ✗ | if (tbl->base == BASE_DECIMAL) | |
| 298 | ✗ | nft_print(octx, "\t%-30s\t%20" PRIu64 "\n", | |
| 299 | ✗ | s->identifier, value); | |
| 300 | else | ||
| 301 | ✗ | nft_print(octx, "\t%-30s\t0x%.*" PRIx64 "\n", | |
| 302 | ✗ | s->identifier, 2 * len, value); | |
| 303 | } | ||
| 304 | ✗ | } | |
| 305 | |||
| 306 | ✗ | static void invalid_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 307 | { | ||
| 308 | ✗ | nft_gmp_print(octx, "0x%Zx [invalid type]", expr->value); | |
| 309 | ✗ | } | |
| 310 | |||
| 311 | const struct datatype invalid_type = { | ||
| 312 | .type = TYPE_INVALID, | ||
| 313 | .name = "invalid", | ||
| 314 | .desc = "invalid", | ||
| 315 | .print = invalid_type_print, | ||
| 316 | }; | ||
| 317 | |||
| 318 | 1518 | void expr_chain_export(const struct expr *e, char *chain_name) | |
| 319 | { | ||
| 320 | unsigned int len; | ||
| 321 | |||
| 322 | 1518 | len = e->len / BITS_PER_BYTE; | |
| 323 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 1518 times.
|
1518 | if (len >= NFT_CHAIN_MAXNAMELEN) |
| 324 | ✗ | BUG("verdict expression length %u is too large (%u bits max)", | |
| 325 | e->len, NFT_CHAIN_MAXNAMELEN * BITS_PER_BYTE); | ||
| 326 | |||
| 327 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 1518 times.
|
1518 | mpz_export_data(chain_name, e->value, BYTEORDER_HOST_ENDIAN, len); |
| 328 | 1518 | } | |
| 329 | |||
| 330 | 590 | static void verdict_jump_chain_print(const char *what, const struct expr *e, | |
| 331 | struct output_ctx *octx) | ||
| 332 | { | ||
| 333 | char chain[NFT_CHAIN_MAXNAMELEN]; | ||
| 334 | |||
| 335 | 590 | memset(chain, 0, sizeof(chain)); | |
| 336 | 590 | expr_chain_export(e, chain); | |
| 337 | 590 | nft_print(octx, "%s %s", what, chain); | |
| 338 | 590 | } | |
| 339 | |||
| 340 | 5373 | static void verdict_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 341 | { | ||
| 342 |
5/6✓ Branch 0 (2→3) taken 14 times.
✗ Branch 1 (2→5) not taken.
✓ Branch 2 (2→7) taken 510 times.
✓ Branch 3 (2→12) taken 80 times.
✓ Branch 4 (2→17) taken 11 times.
✓ Branch 5 (2→19) taken 4758 times.
|
5373 | switch (expr->verdict) { |
| 343 | 14 | case NFT_CONTINUE: | |
| 344 | 14 | nft_print(octx, "continue"); | |
| 345 | 14 | break; | |
| 346 | ✗ | case NFT_BREAK: | |
| 347 | ✗ | nft_print(octx, "break"); | |
| 348 | ✗ | break; | |
| 349 | 510 | case NFT_JUMP: | |
| 350 |
1/2✓ Branch 0 (7→8) taken 510 times.
✗ Branch 1 (7→9) not taken.
|
510 | if (expr->chain->etype == EXPR_VALUE) { |
| 351 | 510 | verdict_jump_chain_print("jump", expr->chain, octx); | |
| 352 | } else { | ||
| 353 | ✗ | nft_print(octx, "jump "); | |
| 354 | ✗ | expr_print(expr->chain, octx); | |
| 355 | } | ||
| 356 | 510 | break; | |
| 357 | 80 | case NFT_GOTO: | |
| 358 |
1/2✓ Branch 0 (12→13) taken 80 times.
✗ Branch 1 (12→14) not taken.
|
80 | if (expr->chain->etype == EXPR_VALUE) { |
| 359 | 80 | verdict_jump_chain_print("goto", expr->chain, octx); | |
| 360 | } else { | ||
| 361 | ✗ | nft_print(octx, "goto "); | |
| 362 | ✗ | expr_print(expr->chain, octx); | |
| 363 | } | ||
| 364 | 80 | break; | |
| 365 | 11 | case NFT_RETURN: | |
| 366 | 11 | nft_print(octx, "return"); | |
| 367 | 11 | break; | |
| 368 | 4758 | default: | |
| 369 |
2/5✓ Branch 0 (19→20) taken 4242 times.
✓ Branch 1 (19→22) taken 516 times.
✗ Branch 2 (19→24) not taken.
✗ Branch 3 (19→26) not taken.
✗ Branch 4 (19→28) not taken.
|
4758 | switch (expr->verdict & NF_VERDICT_MASK) { |
| 370 | 4242 | case NF_ACCEPT: | |
| 371 | 4242 | nft_print(octx, "accept"); | |
| 372 | 4242 | break; | |
| 373 | 516 | case NF_DROP: | |
| 374 | 516 | nft_print(octx, "drop"); | |
| 375 | 516 | break; | |
| 376 | ✗ | case NF_QUEUE: | |
| 377 | ✗ | nft_print(octx, "queue"); | |
| 378 | ✗ | break; | |
| 379 | ✗ | case NF_STOLEN: | |
| 380 | ✗ | nft_print(octx, "stolen"); | |
| 381 | ✗ | break; | |
| 382 | ✗ | default: | |
| 383 | ✗ | nft_print(octx, "unknown verdict value %u", expr->verdict); | |
| 384 | ✗ | break; | |
| 385 | } | ||
| 386 | } | ||
| 387 | 5373 | } | |
| 388 | |||
| 389 | 1 | static struct error_record *verdict_type_error(const struct expr *sym) | |
| 390 | { | ||
| 391 | /* Skip jump and goto from fuzzy match to provide better error | ||
| 392 | * reporting, fall back to `jump chain' if no clue. | ||
| 393 | */ | ||
| 394 | static const char *verdict_array[] = { | ||
| 395 | "continue", "break", "return", "accept", "drop", "queue", | ||
| 396 | "stolen", NULL, | ||
| 397 | }; | ||
| 398 | struct string_misspell_state st; | ||
| 399 | int i; | ||
| 400 | |||
| 401 | 1 | string_misspell_init(&st); | |
| 402 | |||
| 403 |
2/2✓ Branch 0 (6→4) taken 7 times.
✓ Branch 1 (6→7) taken 1 times.
|
8 | for (i = 0; verdict_array[i] != NULL; i++) { |
| 404 | 7 | string_misspell_update(sym->identifier, verdict_array[i], | |
| 405 | 7 | (void *)verdict_array[i], &st); | |
| 406 | } | ||
| 407 | |||
| 408 |
1/2✓ Branch 0 (7→8) taken 1 times.
✗ Branch 1 (7→10) not taken.
|
1 | if (st.obj) { |
| 409 | 1 | return error(&sym->location, "Could not parse %s; did you mean `%s'?", | |
| 410 | sym->dtype->desc, (const char *)st.obj); | ||
| 411 | } | ||
| 412 | |||
| 413 | /* assume user would like to jump to chain as a hint. */ | ||
| 414 | ✗ | return error(&sym->location, "Could not parse %s; did you mean `jump %s'?", | |
| 415 | sym->dtype->desc, sym->identifier); | ||
| 416 | } | ||
| 417 | |||
| 418 | const struct datatype verdict_type = { | ||
| 419 | .type = TYPE_VERDICT, | ||
| 420 | .name = "verdict", | ||
| 421 | .desc = "netfilter verdict", | ||
| 422 | .print = verdict_type_print, | ||
| 423 | .err = verdict_type_error, | ||
| 424 | }; | ||
| 425 | |||
| 426 | static const struct symbol_table nfproto_tbl = { | ||
| 427 | .base = BASE_DECIMAL, | ||
| 428 | .symbols = { | ||
| 429 | SYMBOL("ipv4", NFPROTO_IPV4), | ||
| 430 | SYMBOL("ipv6", NFPROTO_IPV6), | ||
| 431 | SYMBOL_LIST_END | ||
| 432 | }, | ||
| 433 | }; | ||
| 434 | |||
| 435 | const struct datatype nfproto_type = { | ||
| 436 | .type = TYPE_NFPROTO, | ||
| 437 | .name = "nf_proto", | ||
| 438 | .desc = "netfilter protocol", | ||
| 439 | .size = 1 * BITS_PER_BYTE, | ||
| 440 | .basetype = &integer_type, | ||
| 441 | .sym_tbl = &nfproto_tbl, | ||
| 442 | }; | ||
| 443 | |||
| 444 | const struct datatype bitmask_type = { | ||
| 445 | .type = TYPE_BITMASK, | ||
| 446 | .name = "bitmask", | ||
| 447 | .desc = "bitmask", | ||
| 448 | .basefmt = "0x%Zx", | ||
| 449 | .basetype = &integer_type, | ||
| 450 | }; | ||
| 451 | |||
| 452 | 44258 | static void integer_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 453 | { | ||
| 454 | 44258 | const struct datatype *dtype = expr->dtype; | |
| 455 | 44258 | const char *fmt = "%Zu"; | |
| 456 | |||
| 457 | do { | ||
| 458 |
2/2✓ Branch 0 (3→4) taken 2434 times.
✓ Branch 1 (3→5) taken 80127 times.
|
82561 | if (dtype->basefmt != NULL) { |
| 459 | 2434 | fmt = dtype->basefmt; | |
| 460 | 2434 | break; | |
| 461 | } | ||
| 462 |
2/2✓ Branch 0 (5→6) taken 38303 times.
✓ Branch 1 (5→7) taken 41824 times.
|
80127 | } while ((dtype = dtype->basetype)); |
| 463 | |||
| 464 | 44258 | nft_gmp_print(octx, fmt, expr->value); | |
| 465 | 44258 | } | |
| 466 | |||
| 467 | 20984 | static struct error_record *integer_type_parse(struct parse_ctx *ctx, | |
| 468 | const struct expr *sym, | ||
| 469 | struct expr **res) | ||
| 470 | { | ||
| 471 | mpz_t v; | ||
| 472 | |||
| 473 | 20984 | mpz_init(v); | |
| 474 |
2/2✓ Branch 0 (4→5) taken 41 times.
✓ Branch 1 (4→8) taken 20943 times.
|
20984 | if (mpz_set_str(v, sym->identifier, 0)) { |
| 475 | 41 | mpz_clear(v); | |
| 476 | 41 | return error(&sym->location, "Could not parse %s", | |
| 477 | sym->dtype->desc); | ||
| 478 | } | ||
| 479 | |||
| 480 | 20943 | *res = constant_expr_alloc(&sym->location, sym->dtype, | |
| 481 | BYTEORDER_HOST_ENDIAN, 1, NULL); | ||
| 482 | 20943 | mpz_set((*res)->value, v); | |
| 483 | 20943 | mpz_clear(v); | |
| 484 | 20943 | return NULL; | |
| 485 | } | ||
| 486 | |||
| 487 | const struct datatype integer_type = { | ||
| 488 | .type = TYPE_INTEGER, | ||
| 489 | .name = "integer", | ||
| 490 | .desc = "integer", | ||
| 491 | .print = integer_type_print, | ||
| 492 | .json = integer_type_json, | ||
| 493 | .parse = integer_type_parse, | ||
| 494 | }; | ||
| 495 | |||
| 496 | 219 | static void xinteger_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 497 | { | ||
| 498 | 219 | nft_gmp_print(octx, "0x%Zx", expr->value); | |
| 499 | 219 | } | |
| 500 | |||
| 501 | /* Alias of integer_type to print raw payload expressions in hexadecimal. */ | ||
| 502 | const struct datatype xinteger_type = { | ||
| 503 | .type = TYPE_INTEGER, | ||
| 504 | .name = "integer", | ||
| 505 | .desc = "integer", | ||
| 506 | .basetype = &integer_type, | ||
| 507 | .print = xinteger_type_print, | ||
| 508 | .json = integer_type_json, | ||
| 509 | .parse = integer_type_parse, | ||
| 510 | }; | ||
| 511 | |||
| 512 | 1 | static void queue_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 513 | { | ||
| 514 | 1 | nft_gmp_print(octx, "queue"); | |
| 515 | 1 | } | |
| 516 | |||
| 517 | /* Dummy queue_type for set declaration with typeof, see | ||
| 518 | * constant_expr_build_udata and constant_expr_parse_udata, | ||
| 519 | * basetype is used for elements. | ||
| 520 | */ | ||
| 521 | const struct datatype queue_type = { | ||
| 522 | .type = TYPE_INTEGER, | ||
| 523 | .is_typeof = 1, | ||
| 524 | .name = "queue", | ||
| 525 | .desc = "queue", | ||
| 526 | .basetype = &integer_type, | ||
| 527 | .print = queue_type_print, | ||
| 528 | }; | ||
| 529 | |||
| 530 | 21095 | static void string_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 531 | 21095 | { | |
| 532 | 21095 | unsigned int len = div_round_up(expr->len, BITS_PER_BYTE); | |
| 533 | 21095 | char data[len+1]; | |
| 534 | |||
| 535 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 21095 times.
|
21095 | mpz_export_data(data, expr->value, BYTEORDER_HOST_ENDIAN, len); |
| 536 | 21095 | data[len] = '\0'; | |
| 537 | 21095 | nft_print(octx, "\"%s\"", data); | |
| 538 | 21095 | } | |
| 539 | |||
| 540 | 83486 | static struct error_record *string_type_parse(struct parse_ctx *ctx, | |
| 541 | const struct expr *sym, | ||
| 542 | struct expr **res) | ||
| 543 | { | ||
| 544 | 166972 | *res = constant_expr_alloc(&sym->location, &string_type, | |
| 545 | BYTEORDER_HOST_ENDIAN, | ||
| 546 | 83486 | (strlen(sym->identifier) + 1) * BITS_PER_BYTE, | |
| 547 | 83486 | sym->identifier); | |
| 548 | 83486 | return NULL; | |
| 549 | } | ||
| 550 | |||
| 551 | const struct datatype string_type = { | ||
| 552 | .type = TYPE_STRING, | ||
| 553 | .name = "string", | ||
| 554 | .desc = "string", | ||
| 555 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 556 | .print = string_type_print, | ||
| 557 | .json = string_type_json, | ||
| 558 | .parse = string_type_parse, | ||
| 559 | }; | ||
| 560 | |||
| 561 | 1389 | static void lladdr_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 562 | 1389 | { | |
| 563 | 1389 | unsigned int len = div_round_up(expr->len, BITS_PER_BYTE); | |
| 564 | 1389 | const char *delim = ""; | |
| 565 | 1389 | uint8_t data[len]; | |
| 566 | unsigned int i; | ||
| 567 | |||
| 568 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1389 times.
|
1389 | mpz_export_data(data, expr->value, BYTEORDER_BIG_ENDIAN, len); |
| 569 | |||
| 570 |
2/2✓ Branch 0 (8→6) taken 8334 times.
✓ Branch 1 (8→9) taken 1389 times.
|
9723 | for (i = 0; i < len; i++) { |
| 571 | 8334 | nft_print(octx, "%s%.2x", delim, data[i]); | |
| 572 | 8334 | delim = ":"; | |
| 573 | } | ||
| 574 | 1389 | } | |
| 575 | |||
| 576 | 2978 | static struct error_record *lladdr_type_parse(struct parse_ctx *ctx, | |
| 577 | const struct expr *sym, | ||
| 578 | struct expr **res) | ||
| 579 | 2978 | { | |
| 580 | 2978 | char buf[strlen(sym->identifier) + 1], *p; | |
| 581 | 2978 | const char *s = sym->identifier; | |
| 582 | unsigned int len, n; | ||
| 583 | |||
| 584 | 2978 | for (len = 0;;) { | |
| 585 | 17868 | n = strtoul(s, &p, 16); | |
| 586 |
2/4✓ Branch 0 (4→5) taken 17868 times.
✗ Branch 1 (4→6) not taken.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→8) taken 17868 times.
|
17868 | if (s == p || n > 0xff) |
| 587 | ✗ | return erec_create(EREC_ERROR, &sym->location, | |
| 588 | "Invalid LL address"); | ||
| 589 | 17868 | buf[len++] = n; | |
| 590 |
2/2✓ Branch 0 (8→9) taken 2978 times.
✓ Branch 1 (8→10) taken 14890 times.
|
17868 | if (*p == '\0') |
| 591 | 2978 | break; | |
| 592 | 14890 | s = ++p; | |
| 593 | } | ||
| 594 | |||
| 595 | 2978 | *res = constant_expr_alloc(&sym->location, sym->dtype, | |
| 596 | BYTEORDER_BIG_ENDIAN, len * BITS_PER_BYTE, | ||
| 597 | buf); | ||
| 598 | 2978 | return NULL; | |
| 599 | } | ||
| 600 | |||
| 601 | const struct datatype lladdr_type = { | ||
| 602 | .type = TYPE_LLADDR, | ||
| 603 | .name = "ll_addr", | ||
| 604 | .desc = "link layer address", | ||
| 605 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 606 | .basetype = &integer_type, | ||
| 607 | .print = lladdr_type_print, | ||
| 608 | .parse = lladdr_type_parse, | ||
| 609 | }; | ||
| 610 | |||
| 611 | 438133 | static void ipaddr_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 612 | { | ||
| 613 | 438133 | struct sockaddr_in sin = { .sin_family = AF_INET, }; | |
| 614 | char buf[NI_MAXHOST]; | ||
| 615 | int err; | ||
| 616 | |||
| 617 | 438133 | sin.sin_addr.s_addr = mpz_get_be32(expr->value); | |
| 618 | 438133 | err = getnameinfo((struct sockaddr *)&sin, sizeof(sin), buf, | |
| 619 | sizeof(buf), NULL, 0, | ||
| 620 | 438133 | nft_output_reversedns(octx) ? 0 : NI_NUMERICHOST); | |
| 621 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 438133 times.
|
438133 | if (err != 0) { |
| 622 | ✗ | getnameinfo((struct sockaddr *)&sin, sizeof(sin), buf, | |
| 623 | sizeof(buf), NULL, 0, NI_NUMERICHOST); | ||
| 624 | } | ||
| 625 | 438133 | nft_print(octx, "%s", buf); | |
| 626 | 438133 | } | |
| 627 | |||
| 628 | 1191909 | static struct error_record *ipaddr_type_parse(struct parse_ctx *ctx, | |
| 629 | const struct expr *sym, | ||
| 630 | struct expr **res) | ||
| 631 | { | ||
| 632 | struct in_addr addr; | ||
| 633 | |||
| 634 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→8) taken 1191909 times.
|
1191909 | if (nft_input_no_dns(ctx->input)) { |
| 635 | ✗ | if (inet_pton(AF_INET, sym->identifier, &addr) != 1) | |
| 636 | ✗ | return error(&sym->location, "Invalid IPv4 address"); | |
| 637 | } else { | ||
| 638 | 1191909 | struct addrinfo *ai, hints = { .ai_family = AF_INET, | |
| 639 | .ai_socktype = SOCK_DGRAM}; | ||
| 640 | int err; | ||
| 641 | |||
| 642 | 1191909 | err = getaddrinfo(sym->identifier, NULL, &hints, &ai); | |
| 643 |
2/2✓ Branch 0 (9→10) taken 11 times.
✓ Branch 1 (9→13) taken 1191898 times.
|
1191909 | if (err != 0) |
| 644 | 11 | return error(&sym->location, "Could not resolve hostname: %s", | |
| 645 | gai_strerror(err)); | ||
| 646 | |||
| 647 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→17) taken 1191898 times.
|
1191898 | if (ai->ai_next != NULL) { |
| 648 | ✗ | freeaddrinfo(ai); | |
| 649 | ✗ | return error(&sym->location, | |
| 650 | "Hostname resolves to multiple addresses"); | ||
| 651 | } | ||
| 652 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→19) taken 1191898 times.
|
1191898 | assert(ai->ai_addr->sa_family == AF_INET); |
| 653 | 1191898 | addr = ((struct sockaddr_in *) (void *) ai->ai_addr)->sin_addr; | |
| 654 | 1191898 | freeaddrinfo(ai); | |
| 655 | } | ||
| 656 | |||
| 657 | 1191898 | *res = constant_expr_alloc(&sym->location, &ipaddr_type, | |
| 658 | BYTEORDER_BIG_ENDIAN, | ||
| 659 | sizeof(addr) * BITS_PER_BYTE, &addr); | ||
| 660 | 1191898 | return NULL; | |
| 661 | } | ||
| 662 | |||
| 663 | const struct datatype ipaddr_type = { | ||
| 664 | .type = TYPE_IPADDR, | ||
| 665 | .name = "ipv4_addr", | ||
| 666 | .desc = "IPv4 address", | ||
| 667 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 668 | .size = 4 * BITS_PER_BYTE, | ||
| 669 | .basetype = &integer_type, | ||
| 670 | .print = ipaddr_type_print, | ||
| 671 | .parse = ipaddr_type_parse, | ||
| 672 | }; | ||
| 673 | |||
| 674 | 16812 | static void ip6addr_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 675 | { | ||
| 676 | 16812 | struct sockaddr_in6 sin6 = { .sin6_family = AF_INET6 }; | |
| 677 | char buf[NI_MAXHOST]; | ||
| 678 | int err; | ||
| 679 | |||
| 680 | 16812 | mpz_export_data(&sin6.sin6_addr, expr->value, BYTEORDER_BIG_ENDIAN, | |
| 681 | sizeof(sin6.sin6_addr)); | ||
| 682 | |||
| 683 | 16812 | err = getnameinfo((struct sockaddr *)&sin6, sizeof(sin6), buf, | |
| 684 | sizeof(buf), NULL, 0, | ||
| 685 | 16812 | nft_output_reversedns(octx) ? 0 : NI_NUMERICHOST); | |
| 686 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 16812 times.
|
16812 | if (err != 0) { |
| 687 | ✗ | getnameinfo((struct sockaddr *)&sin6, sizeof(sin6), buf, | |
| 688 | sizeof(buf), NULL, 0, NI_NUMERICHOST); | ||
| 689 | } | ||
| 690 | 16812 | nft_print(octx, "%s", buf); | |
| 691 | 16812 | } | |
| 692 | |||
| 693 | 3672 | static struct error_record *ip6addr_type_parse(struct parse_ctx *ctx, | |
| 694 | const struct expr *sym, | ||
| 695 | struct expr **res) | ||
| 696 | { | ||
| 697 | struct in6_addr addr; | ||
| 698 | |||
| 699 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→8) taken 3672 times.
|
3672 | if (nft_input_no_dns(ctx->input)) { |
| 700 | ✗ | if (inet_pton(AF_INET6, sym->identifier, &addr) != 1) | |
| 701 | ✗ | return error(&sym->location, "Invalid IPv6 address"); | |
| 702 | } else { | ||
| 703 | 3672 | struct addrinfo *ai, hints = { .ai_family = AF_INET6, | |
| 704 | .ai_socktype = SOCK_DGRAM}; | ||
| 705 | int err; | ||
| 706 | |||
| 707 | 3672 | err = getaddrinfo(sym->identifier, NULL, &hints, &ai); | |
| 708 |
2/2✓ Branch 0 (9→10) taken 8 times.
✓ Branch 1 (9→13) taken 3664 times.
|
3672 | if (err != 0) |
| 709 | 8 | return error(&sym->location, "Could not resolve hostname: %s", | |
| 710 | gai_strerror(err)); | ||
| 711 | |||
| 712 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→17) taken 3664 times.
|
3664 | if (ai->ai_next != NULL) { |
| 713 | ✗ | freeaddrinfo(ai); | |
| 714 | ✗ | return error(&sym->location, | |
| 715 | "Hostname resolves to multiple addresses"); | ||
| 716 | } | ||
| 717 | |||
| 718 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→19) taken 3664 times.
|
3664 | assert(ai->ai_addr->sa_family == AF_INET6); |
| 719 | 3664 | addr = ((struct sockaddr_in6 *)(void *)ai->ai_addr)->sin6_addr; | |
| 720 | 3664 | freeaddrinfo(ai); | |
| 721 | } | ||
| 722 | |||
| 723 | 3664 | *res = constant_expr_alloc(&sym->location, &ip6addr_type, | |
| 724 | BYTEORDER_BIG_ENDIAN, | ||
| 725 | sizeof(addr) * BITS_PER_BYTE, &addr); | ||
| 726 | 3664 | return NULL; | |
| 727 | } | ||
| 728 | |||
| 729 | const struct datatype ip6addr_type = { | ||
| 730 | .type = TYPE_IP6ADDR, | ||
| 731 | .name = "ipv6_addr", | ||
| 732 | .desc = "IPv6 address", | ||
| 733 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 734 | .size = 16 * BITS_PER_BYTE, | ||
| 735 | .basetype = &integer_type, | ||
| 736 | .print = ip6addr_type_print, | ||
| 737 | .parse = ip6addr_type_parse, | ||
| 738 | }; | ||
| 739 | |||
| 740 | 1711 | static void inet_protocol_type_print(const struct expr *expr, | |
| 741 | struct output_ctx *octx) | ||
| 742 | { | ||
| 743 |
2/2✓ Branch 0 (3→4) taken 895 times.
✓ Branch 1 (3→11) taken 816 times.
|
1711 | if (!nft_output_numeric_proto(octx) && |
| 744 |
1/2✓ Branch 0 (4→5) taken 895 times.
✗ Branch 1 (4→11) not taken.
|
895 | mpz_cmp_ui(expr->value, UINT8_MAX) <= 0) { |
| 745 | char name[NFT_PROTONAME_MAXSIZE]; | ||
| 746 | |||
| 747 |
1/2✓ Branch 0 (7→8) taken 895 times.
✗ Branch 1 (7→10) not taken.
|
895 | if (nft_getprotobynumber(mpz_get_uint8(expr->value), name, sizeof(name))) { |
| 748 | 895 | nft_print(octx, "%s", name); | |
| 749 | 895 | return; | |
| 750 | } | ||
| 751 | } | ||
| 752 | 816 | integer_type_print(expr, octx); | |
| 753 | } | ||
| 754 | |||
| 755 | ✗ | static void inet_protocol_type_describe(struct output_ctx *octx) | |
| 756 | { | ||
| 757 | uint8_t protonum; | ||
| 758 | |||
| 759 | ✗ | for (protonum = 0; protonum < UINT8_MAX; protonum++) { | |
| 760 | char name[NFT_PROTONAME_MAXSIZE]; | ||
| 761 | |||
| 762 | ✗ | if (!nft_getprotobynumber(protonum, name, sizeof(name))) | |
| 763 | ✗ | continue; | |
| 764 | |||
| 765 | ✗ | nft_print(octx, "\t%-30s\t%u\n", name, protonum); | |
| 766 | } | ||
| 767 | ✗ | } | |
| 768 | |||
| 769 | 1356 | static struct error_record *inet_protocol_type_parse(struct parse_ctx *ctx, | |
| 770 | const struct expr *sym, | ||
| 771 | struct expr **res) | ||
| 772 | { | ||
| 773 | uint8_t proto; | ||
| 774 | uintmax_t i; | ||
| 775 | char *end; | ||
| 776 | |||
| 777 | 1356 | errno = 0; | |
| 778 | 1356 | i = strtoumax(sym->identifier, &end, 0); | |
| 779 |
3/4✓ Branch 0 (3→4) taken 1249 times.
✓ Branch 1 (3→10) taken 107 times.
✓ Branch 2 (4→5) taken 1249 times.
✗ Branch 3 (4→10) not taken.
|
1356 | if (sym->identifier != end && *end == '\0') { |
| 780 |
3/4✓ Branch 0 (5→6) taken 1249 times.
✗ Branch 1 (5→7) not taken.
✓ Branch 2 (6→7) taken 5 times.
✓ Branch 3 (6→9) taken 1244 times.
|
1249 | if (errno == ERANGE || i > UINT8_MAX) |
| 781 | 5 | return error(&sym->location, "Protocol out of range"); | |
| 782 | |||
| 783 | 1244 | proto = i; | |
| 784 | } else { | ||
| 785 | int r; | ||
| 786 | |||
| 787 | 107 | r = nft_getprotobyname(sym->identifier); | |
| 788 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→14) taken 107 times.
|
107 | if (r < 0) |
| 789 | ✗ | return error(&sym->location, "Could not resolve protocol name"); | |
| 790 | |||
| 791 | 107 | proto = r; | |
| 792 | } | ||
| 793 | |||
| 794 | 1351 | *res = constant_expr_alloc(&sym->location, &inet_protocol_type, | |
| 795 | BYTEORDER_HOST_ENDIAN, BITS_PER_BYTE, | ||
| 796 | &proto); | ||
| 797 | 1351 | return NULL; | |
| 798 | } | ||
| 799 | |||
| 800 | const struct datatype inet_protocol_type = { | ||
| 801 | .type = TYPE_INET_PROTOCOL, | ||
| 802 | .name = "inet_proto", | ||
| 803 | .desc = "Internet protocol", | ||
| 804 | .size = BITS_PER_BYTE, | ||
| 805 | .basetype = &integer_type, | ||
| 806 | .print = inet_protocol_type_print, | ||
| 807 | .json = inet_protocol_type_json, | ||
| 808 | .parse = inet_protocol_type_parse, | ||
| 809 | .describe = inet_protocol_type_describe, | ||
| 810 | }; | ||
| 811 | |||
| 812 | ✗ | static void inet_service_print(const struct expr *expr, struct output_ctx *octx) | |
| 813 | { | ||
| 814 | ✗ | uint16_t port = mpz_get_be16(expr->value); | |
| 815 | char name[NFT_SERVNAME_MAXSIZE]; | ||
| 816 | |||
| 817 | ✗ | if (!nft_getservbyport(port, NULL, name, sizeof(name))) | |
| 818 | ✗ | nft_print(octx, "%hu", ntohs(port)); | |
| 819 | else | ||
| 820 | ✗ | nft_print(octx, "\"%s\"", name); | |
| 821 | ✗ | } | |
| 822 | |||
| 823 | 36967 | void inet_service_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 824 | { | ||
| 825 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→7) taken 36967 times.
|
36967 | if (nft_output_service(octx) && |
| 826 | ✗ | mpz_cmp_ui(expr->value, UINT16_MAX) <= 0) { | |
| 827 | ✗ | inet_service_print(expr, octx); | |
| 828 | ✗ | return; | |
| 829 | } | ||
| 830 | 36967 | integer_type_print(expr, octx); | |
| 831 | } | ||
| 832 | |||
| 833 | 279444 | static struct error_record *inet_service_type_parse(struct parse_ctx *ctx, | |
| 834 | const struct expr *sym, | ||
| 835 | struct expr **res) | ||
| 836 | { | ||
| 837 | struct addrinfo *ai; | ||
| 838 | uint16_t port; | ||
| 839 | uintmax_t i; | ||
| 840 | char *end; | ||
| 841 | int err; | ||
| 842 | |||
| 843 | 279444 | errno = 0; | |
| 844 | 279444 | i = strtoumax(sym->identifier, &end, 0); | |
| 845 |
3/4✓ Branch 0 (3→4) taken 279351 times.
✓ Branch 1 (3→10) taken 93 times.
✓ Branch 2 (4→5) taken 279351 times.
✗ Branch 3 (4→10) not taken.
|
279444 | if (sym->identifier != end && *end == '\0') { |
| 846 |
3/4✓ Branch 0 (5→6) taken 279351 times.
✗ Branch 1 (5→7) not taken.
✓ Branch 2 (6→7) taken 2 times.
✓ Branch 3 (6→9) taken 279349 times.
|
279351 | if (errno == ERANGE || i > UINT16_MAX) |
| 847 | 2 | return error(&sym->location, "Service out of range"); | |
| 848 | |||
| 849 | 279349 | port = htons(i); | |
| 850 | } else { | ||
| 851 | 93 | err = getaddrinfo(NULL, sym->identifier, NULL, &ai); | |
| 852 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→15) taken 93 times.
|
93 | if (err != 0) |
| 853 | ✗ | return error(&sym->location, "Could not resolve service: %s", | |
| 854 | gai_strerror(err)); | ||
| 855 | |||
| 856 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 93 times.
|
93 | if (ai->ai_addr->sa_family == AF_INET) { |
| 857 | ✗ | port = ((struct sockaddr_in *)(void *)ai->ai_addr)->sin_port; | |
| 858 | } else { | ||
| 859 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→19) taken 93 times.
|
93 | assert(ai->ai_addr->sa_family == AF_INET6); |
| 860 | 93 | port = ((struct sockaddr_in6 *)(void *)ai->ai_addr)->sin6_port; | |
| 861 | } | ||
| 862 | 93 | freeaddrinfo(ai); | |
| 863 | } | ||
| 864 | |||
| 865 | 279442 | *res = constant_expr_alloc(&sym->location, &inet_service_type, | |
| 866 | BYTEORDER_BIG_ENDIAN, | ||
| 867 | sizeof(port) * BITS_PER_BYTE, &port); | ||
| 868 | 279442 | return NULL; | |
| 869 | } | ||
| 870 | |||
| 871 | const struct datatype inet_service_type = { | ||
| 872 | .type = TYPE_INET_SERVICE, | ||
| 873 | .name = "inet_service", | ||
| 874 | .desc = "internet network service", | ||
| 875 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 876 | .size = 2 * BITS_PER_BYTE, | ||
| 877 | .basetype = &integer_type, | ||
| 878 | .print = inet_service_type_print, | ||
| 879 | .json = inet_service_type_json, | ||
| 880 | .parse = inet_service_type_parse, | ||
| 881 | }; | ||
| 882 | |||
| 883 | #define RT_SYM_TAB_INITIAL_SIZE 16 | ||
| 884 | |||
| 885 | 52068 | static FILE *open_iproute2_db(const char *filename, char **path) | |
| 886 | { | ||
| 887 | FILE *ret; | ||
| 888 | |||
| 889 |
2/2✓ Branch 0 (2→3) taken 13017 times.
✓ Branch 1 (2→5) taken 39051 times.
|
52068 | if (filename[0] == '/') |
| 890 | 13017 | return fopen(filename, "r"); | |
| 891 | |||
| 892 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 39051 times.
|
39051 | if (asprintf(path, "/etc/iproute2/%s", filename) == -1) |
| 893 | ✗ | goto fail; | |
| 894 | |||
| 895 | 39051 | ret = fopen(*path, "r"); | |
| 896 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 39051 times.
|
39051 | if (ret) |
| 897 | ✗ | return ret; | |
| 898 | |||
| 899 | 39051 | free(*path); | |
| 900 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 39051 times.
|
39051 | if (asprintf(path, "/usr/share/iproute2/%s", filename) == -1) |
| 901 | ✗ | goto fail; | |
| 902 | |||
| 903 | 39051 | ret = fopen(*path, "r"); | |
| 904 |
2/2✓ Branch 0 (15→16) taken 26034 times.
✓ Branch 1 (15→17) taken 13017 times.
|
39051 | if (ret) |
| 905 | 26034 | return ret; | |
| 906 | |||
| 907 | 13017 | free(*path); | |
| 908 | 13017 | fail: | |
| 909 | 13017 | *path = NULL; | |
| 910 | 13017 | return NULL; | |
| 911 | } | ||
| 912 | |||
| 913 | 52068 | struct symbol_table *rt_symbol_table_init(const char *filename) | |
| 914 | { | ||
| 915 | 52068 | char buf[512], namebuf[512], *p, *path = NULL; | |
| 916 | struct symbolic_constant s; | ||
| 917 | struct symbol_table *tbl; | ||
| 918 | unsigned int size, nelems, val; | ||
| 919 | FILE *f; | ||
| 920 | |||
| 921 | 52068 | size = RT_SYM_TAB_INITIAL_SIZE; | |
| 922 | 52068 | tbl = xmalloc(sizeof(*tbl) + size * sizeof(s)); | |
| 923 | 52068 | tbl->base = BASE_DECIMAL; | |
| 924 | 52068 | nelems = 0; | |
| 925 | |||
| 926 | 52068 | f = open_iproute2_db(filename, &path); | |
| 927 |
2/2✓ Branch 0 (4→5) taken 26034 times.
✓ Branch 1 (4→6) taken 26034 times.
|
52068 | if (f == NULL) |
| 928 | 26034 | goto out; | |
| 929 | |||
| 930 |
2/2✓ Branch 0 (32→7) taken 195255 times.
✓ Branch 1 (32→33) taken 26034 times.
|
221289 | while (fgets(buf, sizeof(buf), f)) { |
| 931 | 195255 | p = buf; | |
| 932 |
2/4✗ Branch 0 (9→8) not taken.
✓ Branch 1 (9→10) taken 195255 times.
✗ Branch 2 (10→8) not taken.
✓ Branch 3 (10→11) taken 195255 times.
|
195255 | while (*p == ' ' || *p == '\t') |
| 933 | ✗ | p++; | |
| 934 |
4/6✓ Branch 0 (11→12) taken 26034 times.
✓ Branch 1 (11→14) taken 169221 times.
✓ Branch 2 (12→13) taken 26034 times.
✗ Branch 3 (12→14) not taken.
✗ Branch 4 (13→14) not taken.
✓ Branch 5 (13→15) taken 26034 times.
|
195255 | if (*p == '#' || *p == '\n' || *p == '\0') |
| 935 | 169221 | continue; | |
| 936 |
1/2✓ Branch 0 (15→16) taken 26034 times.
✗ Branch 1 (15→17) not taken.
|
26034 | if (sscanf(p, "0x%x %511s\n", &val, namebuf) == 2 || |
| 937 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 26034 times.
|
26034 | sscanf(p, "0x%x %511s #", &val, namebuf) == 2) { |
| 938 | ✗ | tbl->base = BASE_HEXADECIMAL; | |
| 939 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 26034 times.
|
26034 | } else if (sscanf(p, "%u %511s\n", &val, namebuf) == 2 || |
| 940 | ✗ | sscanf(p, "%u %511s #", &val, namebuf) == 2) { | |
| 941 | 26034 | tbl->base = BASE_DECIMAL; | |
| 942 | } else { | ||
| 943 | ✗ | fprintf(stderr, "iproute database '%s' corrupted\n", | |
| 944 | ✗ | path ?: filename); | |
| 945 | ✗ | break; | |
| 946 | } | ||
| 947 | |||
| 948 | /* One element is reserved for list terminator */ | ||
| 949 |
1/2✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→28) taken 26034 times.
|
26034 | if (nelems == size - 2) { |
| 950 | ✗ | size *= 2; | |
| 951 | ✗ | tbl = xrealloc(tbl, sizeof(*tbl) + size * sizeof(s)); | |
| 952 | } | ||
| 953 | |||
| 954 | 26034 | tbl->symbols[nelems].identifier = xstrdup(namebuf); | |
| 955 | 26034 | tbl->symbols[nelems].value = val; | |
| 956 | 26034 | nelems++; | |
| 957 | } | ||
| 958 | |||
| 959 | 26034 | fclose(f); | |
| 960 | 52068 | out: | |
| 961 |
2/2✓ Branch 0 (34→35) taken 26034 times.
✓ Branch 1 (34→36) taken 26034 times.
|
52068 | if (path) |
| 962 | 26034 | free(path); | |
| 963 | 52068 | tbl->symbols[nelems] = SYMBOL_LIST_END; | |
| 964 | 52068 | return tbl; | |
| 965 | } | ||
| 966 | |||
| 967 | 52068 | void rt_symbol_table_free(const struct symbol_table *tbl) | |
| 968 | { | ||
| 969 | const struct symbolic_constant *s; | ||
| 970 | |||
| 971 |
2/2✓ Branch 0 (4→3) taken 26034 times.
✓ Branch 1 (4→5) taken 52068 times.
|
78102 | for (s = tbl->symbols; s->identifier != NULL; s++) |
| 972 | 26034 | free_const(s->identifier); | |
| 973 | 52068 | free_const(tbl); | |
| 974 | 52068 | } | |
| 975 | |||
| 976 | ✗ | void rt_symbol_table_describe(struct output_ctx *octx, const char *name, | |
| 977 | const struct symbol_table *tbl, | ||
| 978 | const struct datatype *type) | ||
| 979 | { | ||
| 980 | ✗ | char *path = NULL; | |
| 981 | FILE *f; | ||
| 982 | |||
| 983 | ✗ | if (!tbl || !tbl->symbols[0].identifier) | |
| 984 | ✗ | return; | |
| 985 | |||
| 986 | ✗ | f = open_iproute2_db(name, &path); | |
| 987 | ✗ | if (f) | |
| 988 | ✗ | fclose(f); | |
| 989 | ✗ | if (!path && asprintf(&path, "%s%s", | |
| 990 | ✗ | name[0] == '/' ? "" : "unknown location of ", | |
| 991 | name) < 0) | ||
| 992 | ✗ | return; | |
| 993 | |||
| 994 | ✗ | nft_print(octx, "\npre-defined symbolic constants from %s ", path); | |
| 995 | ✗ | if (tbl->base == BASE_DECIMAL) | |
| 996 | ✗ | nft_print(octx, "(in decimal):\n"); | |
| 997 | else | ||
| 998 | ✗ | nft_print(octx, "(in hexadecimal):\n"); | |
| 999 | ✗ | symbol_table_print(tbl, type, type->byteorder, octx); | |
| 1000 | ✗ | free(path); | |
| 1001 | } | ||
| 1002 | |||
| 1003 | 13017 | void mark_table_init(struct nft_ctx *ctx) | |
| 1004 | { | ||
| 1005 | 13017 | ctx->output.tbl.mark = rt_symbol_table_init("rt_marks"); | |
| 1006 | 13017 | } | |
| 1007 | |||
| 1008 | 13017 | void mark_table_exit(struct nft_ctx *ctx) | |
| 1009 | { | ||
| 1010 | 13017 | rt_symbol_table_free(ctx->output.tbl.mark); | |
| 1011 | 13017 | } | |
| 1012 | |||
| 1013 | 2309 | static void mark_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 1014 | { | ||
| 1015 | 2309 | return symbolic_constant_print(octx->tbl.mark, expr, true, octx); | |
| 1016 | } | ||
| 1017 | |||
| 1018 | 11920 | static struct error_record *mark_type_parse(struct parse_ctx *ctx, | |
| 1019 | const struct expr *sym, | ||
| 1020 | struct expr **res) | ||
| 1021 | { | ||
| 1022 | 11920 | return symbolic_constant_parse(ctx, sym, ctx->tbl->mark, res); | |
| 1023 | } | ||
| 1024 | |||
| 1025 | ✗ | static void mark_type_describe(struct output_ctx *octx) | |
| 1026 | { | ||
| 1027 | ✗ | rt_symbol_table_describe(octx, "rt_marks", | |
| 1028 | octx->tbl.mark, &mark_type); | ||
| 1029 | ✗ | } | |
| 1030 | |||
| 1031 | const struct datatype mark_type = { | ||
| 1032 | .type = TYPE_MARK, | ||
| 1033 | .name = "mark", | ||
| 1034 | .desc = "packet mark", | ||
| 1035 | .describe = mark_type_describe, | ||
| 1036 | .size = 4 * BITS_PER_BYTE, | ||
| 1037 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 1038 | .basetype = &integer_type, | ||
| 1039 | .basefmt = "0x%.8Zx", | ||
| 1040 | .print = mark_type_print, | ||
| 1041 | .json = mark_type_json, | ||
| 1042 | .parse = mark_type_parse, | ||
| 1043 | }; | ||
| 1044 | |||
| 1045 | /* symbol table for private datatypes for reject statement. */ | ||
| 1046 | static const struct symbol_table icmp_code_tbl = { | ||
| 1047 | .base = BASE_DECIMAL, | ||
| 1048 | .symbols = { | ||
| 1049 | SYMBOL("net-unreachable", ICMP_NET_UNREACH), | ||
| 1050 | SYMBOL("host-unreachable", ICMP_HOST_UNREACH), | ||
| 1051 | SYMBOL("prot-unreachable", ICMP_PROT_UNREACH), | ||
| 1052 | SYMBOL("port-unreachable", ICMP_PORT_UNREACH), | ||
| 1053 | SYMBOL("net-prohibited", ICMP_NET_ANO), | ||
| 1054 | SYMBOL("host-prohibited", ICMP_HOST_ANO), | ||
| 1055 | SYMBOL("admin-prohibited", ICMP_PKT_FILTERED), | ||
| 1056 | SYMBOL("frag-needed", ICMP_FRAG_NEEDED), | ||
| 1057 | SYMBOL_LIST_END | ||
| 1058 | }, | ||
| 1059 | }; | ||
| 1060 | |||
| 1061 | /* private datatype for reject statement. */ | ||
| 1062 | const struct datatype reject_icmp_code_type = { | ||
| 1063 | .name = "icmp_code", | ||
| 1064 | .desc = "reject icmp code", | ||
| 1065 | .size = BITS_PER_BYTE, | ||
| 1066 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 1067 | .basetype = &integer_type, | ||
| 1068 | .sym_tbl = &icmp_code_tbl, | ||
| 1069 | }; | ||
| 1070 | |||
| 1071 | /* symbol table for private datatypes for reject statement. */ | ||
| 1072 | static const struct symbol_table icmpv6_code_tbl = { | ||
| 1073 | .base = BASE_DECIMAL, | ||
| 1074 | .symbols = { | ||
| 1075 | SYMBOL("no-route", ICMPV6_NOROUTE), | ||
| 1076 | SYMBOL("admin-prohibited", ICMPV6_ADM_PROHIBITED), | ||
| 1077 | SYMBOL("addr-unreachable", ICMPV6_ADDR_UNREACH), | ||
| 1078 | SYMBOL("port-unreachable", ICMPV6_PORT_UNREACH), | ||
| 1079 | SYMBOL("policy-fail", ICMPV6_POLICY_FAIL), | ||
| 1080 | SYMBOL("reject-route", ICMPV6_REJECT_ROUTE), | ||
| 1081 | SYMBOL_LIST_END | ||
| 1082 | }, | ||
| 1083 | }; | ||
| 1084 | |||
| 1085 | /* private datatype for reject statement. */ | ||
| 1086 | const struct datatype reject_icmpv6_code_type = { | ||
| 1087 | .name = "icmpv6_code", | ||
| 1088 | .desc = "reject icmpv6 code", | ||
| 1089 | .size = BITS_PER_BYTE, | ||
| 1090 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 1091 | .basetype = &integer_type, | ||
| 1092 | .sym_tbl = &icmpv6_code_tbl, | ||
| 1093 | }; | ||
| 1094 | |||
| 1095 | /* symbol table for private datatypes for reject statement. */ | ||
| 1096 | static const struct symbol_table icmpx_code_tbl = { | ||
| 1097 | .base = BASE_DECIMAL, | ||
| 1098 | .symbols = { | ||
| 1099 | SYMBOL("port-unreachable", NFT_REJECT_ICMPX_PORT_UNREACH), | ||
| 1100 | SYMBOL("admin-prohibited", NFT_REJECT_ICMPX_ADMIN_PROHIBITED), | ||
| 1101 | SYMBOL("no-route", NFT_REJECT_ICMPX_NO_ROUTE), | ||
| 1102 | SYMBOL("host-unreachable", NFT_REJECT_ICMPX_HOST_UNREACH), | ||
| 1103 | SYMBOL_LIST_END | ||
| 1104 | }, | ||
| 1105 | }; | ||
| 1106 | |||
| 1107 | /* private datatype for reject statement. */ | ||
| 1108 | const struct datatype reject_icmpx_code_type = { | ||
| 1109 | .name = "icmpx_code", | ||
| 1110 | .desc = "reject icmpx code", | ||
| 1111 | .size = BITS_PER_BYTE, | ||
| 1112 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 1113 | .basetype = &integer_type, | ||
| 1114 | .sym_tbl = &icmpx_code_tbl, | ||
| 1115 | }; | ||
| 1116 | |||
| 1117 | /* Backward compatible parser for the reject statement. */ | ||
| 1118 | 41 | static struct error_record *icmp_code_parse(struct parse_ctx *ctx, | |
| 1119 | const struct expr *sym, | ||
| 1120 | struct expr **res) | ||
| 1121 | { | ||
| 1122 | 41 | return symbolic_constant_parse(ctx, sym, &icmp_code_tbl, res); | |
| 1123 | } | ||
| 1124 | |||
| 1125 | const struct datatype icmp_code_type = { | ||
| 1126 | .type = TYPE_ICMP_CODE, | ||
| 1127 | .name = "icmp_code", | ||
| 1128 | .desc = "icmp code", | ||
| 1129 | .size = BITS_PER_BYTE, | ||
| 1130 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 1131 | .basetype = &integer_type, | ||
| 1132 | .parse = icmp_code_parse, | ||
| 1133 | }; | ||
| 1134 | |||
| 1135 | /* Backward compatible parser for the reject statement. */ | ||
| 1136 | 23 | static struct error_record *icmpv6_code_parse(struct parse_ctx *ctx, | |
| 1137 | const struct expr *sym, | ||
| 1138 | struct expr **res) | ||
| 1139 | { | ||
| 1140 | 23 | return symbolic_constant_parse(ctx, sym, &icmpv6_code_tbl, res); | |
| 1141 | } | ||
| 1142 | |||
| 1143 | const struct datatype icmpv6_code_type = { | ||
| 1144 | .type = TYPE_ICMPV6_CODE, | ||
| 1145 | .name = "icmpv6_code", | ||
| 1146 | .desc = "icmpv6 code", | ||
| 1147 | .size = BITS_PER_BYTE, | ||
| 1148 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 1149 | .basetype = &integer_type, | ||
| 1150 | .parse = icmpv6_code_parse, | ||
| 1151 | }; | ||
| 1152 | |||
| 1153 | /* Backward compatible parser for the reject statement. */ | ||
| 1154 | ✗ | static struct error_record *icmpx_code_parse(struct parse_ctx *ctx, | |
| 1155 | const struct expr *sym, | ||
| 1156 | struct expr **res) | ||
| 1157 | { | ||
| 1158 | ✗ | return symbolic_constant_parse(ctx, sym, &icmpx_code_tbl, res); | |
| 1159 | } | ||
| 1160 | |||
| 1161 | const struct datatype icmpx_code_type = { | ||
| 1162 | .type = TYPE_ICMPX_CODE, | ||
| 1163 | .name = "icmpx_code", | ||
| 1164 | .desc = "icmpx code", | ||
| 1165 | .size = BITS_PER_BYTE, | ||
| 1166 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 1167 | .basetype = &integer_type, | ||
| 1168 | .parse = icmpx_code_parse, | ||
| 1169 | }; | ||
| 1170 | |||
| 1171 | 1058 | void time_print(uint64_t ms, struct output_ctx *octx) | |
| 1172 | { | ||
| 1173 | uint64_t days, hours, minutes, seconds; | ||
| 1174 | 1058 | bool printed = false; | |
| 1175 | |||
| 1176 |
2/2✓ Branch 0 (3→4) taken 44 times.
✓ Branch 1 (3→6) taken 1014 times.
|
1058 | if (nft_output_seconds(octx)) { |
| 1177 | 44 | nft_print(octx, "%" PRIu64 "s", ms / 1000); | |
| 1178 | 44 | return; | |
| 1179 | } | ||
| 1180 | |||
| 1181 | 1014 | days = ms / 86400000; | |
| 1182 | 1014 | ms %= 86400000; | |
| 1183 | |||
| 1184 | 1014 | hours = ms / 3600000; | |
| 1185 | 1014 | ms %= 3600000; | |
| 1186 | |||
| 1187 | 1014 | minutes = ms / 60000; | |
| 1188 | 1014 | ms %= 60000; | |
| 1189 | |||
| 1190 | 1014 | seconds = ms / 1000; | |
| 1191 | 1014 | ms %= 1000; | |
| 1192 | |||
| 1193 |
2/2✓ Branch 0 (6→7) taken 33 times.
✓ Branch 1 (6→9) taken 981 times.
|
1014 | if (days > 0) { |
| 1194 | 33 | nft_print(octx, "%" PRIu64 "d", days); | |
| 1195 | 33 | printed = true; | |
| 1196 | } | ||
| 1197 |
2/2✓ Branch 0 (9→10) taken 38 times.
✓ Branch 1 (9→12) taken 976 times.
|
1014 | if (hours > 0) { |
| 1198 | 38 | nft_print(octx, "%" PRIu64 "h", hours); | |
| 1199 | 38 | printed = true; | |
| 1200 | } | ||
| 1201 |
2/2✓ Branch 0 (12→13) taken 866 times.
✓ Branch 1 (12→15) taken 148 times.
|
1014 | if (minutes > 0) { |
| 1202 | 866 | nft_print(octx, "%" PRIu64 "m", minutes); | |
| 1203 | 866 | printed = true; | |
| 1204 | } | ||
| 1205 |
2/2✓ Branch 0 (15→16) taken 696 times.
✓ Branch 1 (15→18) taken 318 times.
|
1014 | if (seconds > 0) { |
| 1206 | 696 | nft_print(octx, "%" PRIu64 "s", seconds); | |
| 1207 | 696 | printed = true; | |
| 1208 | } | ||
| 1209 |
2/2✓ Branch 0 (18→19) taken 432 times.
✓ Branch 1 (18→21) taken 582 times.
|
1014 | if (ms > 0) { |
| 1210 | 432 | nft_print(octx, "%" PRIu64 "ms", ms); | |
| 1211 | 432 | printed = true; | |
| 1212 | } | ||
| 1213 | |||
| 1214 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 1014 times.
|
1014 | if (!printed) |
| 1215 | ✗ | nft_print(octx, "0s"); | |
| 1216 | } | ||
| 1217 | |||
| 1218 | enum { | ||
| 1219 | DAY = (1 << 0), | ||
| 1220 | HOUR = (1 << 1), | ||
| 1221 | MIN = (1 << 2), | ||
| 1222 | SECS = (1 << 3), | ||
| 1223 | MSECS = (1 << 4), | ||
| 1224 | }; | ||
| 1225 | |||
| 1226 | 210574 | static uint32_t str2int(const char *str) | |
| 1227 | { | ||
| 1228 | int ret, number; | ||
| 1229 | |||
| 1230 | 210574 | ret = sscanf(str, "%d", &number); | |
| 1231 |
1/2✓ Branch 0 (2→3) taken 210574 times.
✗ Branch 1 (2→4) not taken.
|
210574 | return ret == 1 ? number : 0; |
| 1232 | } | ||
| 1233 | |||
| 1234 | 210602 | struct error_record *time_parse(const struct location *loc, const char *str, | |
| 1235 | uint64_t *res) | ||
| 1236 | { | ||
| 1237 | 210602 | unsigned int max_digits = strlen("12345678"); | |
| 1238 | int i, len; | ||
| 1239 | 210602 | unsigned int k = 0; | |
| 1240 | const char *c; | ||
| 1241 | 210602 | uint64_t d = 0, h = 0, m = 0, s = 0, ms = 0; | |
| 1242 | 210602 | uint32_t mask = 0; | |
| 1243 | |||
| 1244 | 210602 | c = str; | |
| 1245 | 210602 | len = strlen(c); | |
| 1246 |
2/2✓ Branch 0 (38→3) taken 457988 times.
✓ Branch 1 (38→39) taken 210589 times.
|
668577 | for (i = 0; i < len; i++, c++) { |
| 1247 |
5/5✓ Branch 0 (3→4) taken 18 times.
✓ Branch 1 (3→9) taken 31 times.
✓ Branch 2 (3→14) taken 10786 times.
✓ Branch 3 (3→25) taken 199739 times.
✓ Branch 4 (3→30) taken 247414 times.
|
457988 | switch (*c) { |
| 1248 | 18 | case 'd': | |
| 1249 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 18 times.
|
18 | if (mask & DAY) |
| 1250 | ✗ | return error(loc, | |
| 1251 | "Day has been specified twice"); | ||
| 1252 | |||
| 1253 | 18 | d = str2int(c - k); | |
| 1254 | 18 | k = 0; | |
| 1255 | 18 | mask |= DAY; | |
| 1256 | 18 | break; | |
| 1257 | 31 | case 'h': | |
| 1258 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 31 times.
|
31 | if (mask & HOUR) |
| 1259 | ✗ | return error(loc, | |
| 1260 | "Hour has been specified twice"); | ||
| 1261 | |||
| 1262 | 31 | h = str2int(c - k); | |
| 1263 | 31 | k = 0; | |
| 1264 | 31 | mask |= HOUR; | |
| 1265 | 31 | break; | |
| 1266 | 10786 | case 'm': | |
| 1267 |
2/2✓ Branch 0 (14→15) taken 10419 times.
✓ Branch 1 (14→20) taken 367 times.
|
10786 | if (strcmp(c, "ms") == 0) { |
| 1268 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→18) taken 10419 times.
|
10419 | if (mask & MSECS) |
| 1269 | ✗ | return error(loc, | |
| 1270 | "Millisecond has been specified twice"); | ||
| 1271 | 10419 | ms = str2int(c - k); | |
| 1272 | 10419 | c++; | |
| 1273 | 10419 | i++; | |
| 1274 | 10419 | k = 0; | |
| 1275 | 10419 | mask |= MSECS; | |
| 1276 | 10419 | break; | |
| 1277 | } | ||
| 1278 | |||
| 1279 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→23) taken 367 times.
|
367 | if (mask & MIN) |
| 1280 | ✗ | return error(loc, | |
| 1281 | "Minute has been specified twice"); | ||
| 1282 | |||
| 1283 | 367 | m = str2int(c - k); | |
| 1284 | 367 | k = 0; | |
| 1285 | 367 | mask |= MIN; | |
| 1286 | 367 | break; | |
| 1287 | 199739 | case 's': | |
| 1288 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→28) taken 199739 times.
|
199739 | if (mask & SECS) |
| 1289 | ✗ | return error(loc, | |
| 1290 | "Second has been specified twice"); | ||
| 1291 | |||
| 1292 | 199739 | s = str2int(c - k); | |
| 1293 | 199739 | k = 0; | |
| 1294 | 199739 | mask |= SECS; | |
| 1295 | 199739 | break; | |
| 1296 | 247414 | default: | |
| 1297 |
2/2✓ Branch 0 (30→31) taken 13 times.
✓ Branch 1 (30→33) taken 247401 times.
|
247414 | if (!isdigit(*c)) |
| 1298 | 13 | return error(loc, "wrong time format"); | |
| 1299 | |||
| 1300 |
1/2✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→36) taken 247401 times.
|
247401 | if (k++ >= max_digits) |
| 1301 | ✗ | return error(loc, "value too large"); | |
| 1302 | 247401 | break; | |
| 1303 | } | ||
| 1304 | } | ||
| 1305 | |||
| 1306 | /* default to seconds if no unit was specified */ | ||
| 1307 |
2/2✓ Branch 0 (39→40) taken 126 times.
✓ Branch 1 (39→41) taken 210463 times.
|
210589 | if (!mask) |
| 1308 | 126 | ms = atoi(str) * MSEC_PER_SEC; | |
| 1309 | else | ||
| 1310 | 210463 | ms = 24*60*60*MSEC_PER_SEC * d + | |
| 1311 | 210463 | 60*60*MSEC_PER_SEC * h + | |
| 1312 | 210463 | 60*MSEC_PER_SEC * m + | |
| 1313 | 210463 | MSEC_PER_SEC * s + ms; | |
| 1314 | |||
| 1315 | 210589 | *res = ms; | |
| 1316 | 210589 | return NULL; | |
| 1317 | } | ||
| 1318 | |||
| 1319 | |||
| 1320 | 78 | static void time_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 1321 | { | ||
| 1322 | 78 | time_print(mpz_get_uint64(expr->value), octx); | |
| 1323 | 78 | } | |
| 1324 | |||
| 1325 | 234 | static struct error_record *time_type_parse(struct parse_ctx *ctx, | |
| 1326 | const struct expr *sym, | ||
| 1327 | struct expr **res) | ||
| 1328 | { | ||
| 1329 | struct error_record *erec; | ||
| 1330 | uint32_t s32; | ||
| 1331 | uint64_t s; | ||
| 1332 | |||
| 1333 | 234 | erec = time_parse(&sym->location, sym->identifier, &s); | |
| 1334 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 234 times.
|
234 | if (erec != NULL) |
| 1335 | ✗ | return erec; | |
| 1336 | |||
| 1337 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 234 times.
|
234 | if (s > UINT32_MAX) |
| 1338 | ✗ | return error(&sym->location, "value too large"); | |
| 1339 | |||
| 1340 | 234 | s32 = s; | |
| 1341 | 234 | *res = constant_expr_alloc(&sym->location, &time_type, | |
| 1342 | BYTEORDER_HOST_ENDIAN, | ||
| 1343 | sizeof(uint32_t) * BITS_PER_BYTE, &s32); | ||
| 1344 | 234 | return NULL; | |
| 1345 | } | ||
| 1346 | |||
| 1347 | const struct datatype time_type = { | ||
| 1348 | .type = TYPE_TIME, | ||
| 1349 | .name = "time", | ||
| 1350 | .desc = "relative time", | ||
| 1351 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 1352 | .size = 4 * BITS_PER_BYTE, | ||
| 1353 | .basetype = &integer_type, | ||
| 1354 | .print = time_type_print, | ||
| 1355 | .json = time_type_json, | ||
| 1356 | .parse = time_type_parse, | ||
| 1357 | }; | ||
| 1358 | |||
| 1359 | ✗ | static struct error_record *concat_type_parse(struct parse_ctx *ctx, | |
| 1360 | const struct expr *sym, | ||
| 1361 | struct expr **res) | ||
| 1362 | { | ||
| 1363 | ✗ | return error(&sym->location, "invalid data type, expected %s", | |
| 1364 | sym->dtype->desc); | ||
| 1365 | } | ||
| 1366 | |||
| 1367 | 94901 | static struct datatype *datatype_alloc(void) | |
| 1368 | { | ||
| 1369 | struct datatype *dtype; | ||
| 1370 | |||
| 1371 | 94901 | dtype = xzalloc(sizeof(*dtype)); | |
| 1372 | 94901 | dtype->alloc = 1; | |
| 1373 | 94901 | dtype->refcnt = 1; | |
| 1374 | |||
| 1375 | 94901 | return dtype; | |
| 1376 | } | ||
| 1377 | |||
| 1378 | 37856812 | const struct datatype *datatype_get(const struct datatype *ptr) | |
| 1379 | { | ||
| 1380 | 37856812 | struct datatype *dtype = (struct datatype *)ptr; | |
| 1381 | |||
| 1382 |
2/2✓ Branch 0 (2→3) taken 29 times.
✓ Branch 1 (2→4) taken 37856783 times.
|
37856812 | if (!dtype) |
| 1383 | 29 | return NULL; | |
| 1384 |
2/2✓ Branch 0 (4→5) taken 37716782 times.
✓ Branch 1 (4→6) taken 140001 times.
|
37856783 | if (!dtype->alloc) |
| 1385 | 37716782 | return dtype; | |
| 1386 | |||
| 1387 | 140001 | dtype->refcnt++; | |
| 1388 | 140001 | return dtype; | |
| 1389 | } | ||
| 1390 | |||
| 1391 | − | void __datatype_set(struct expr *expr, const struct datatype *dtype) | |
| 1392 | { | ||
| 1393 | const struct datatype *dtype_free; | ||
| 1394 | |||
| 1395 | − | dtype_free = expr->dtype; | |
| 1396 | − | expr->dtype = dtype; | |
| 1397 | − | datatype_free(dtype_free); | |
| 1398 | − | } | |
| 1399 | |||
| 1400 | 10553598 | void datatype_set(struct expr *expr, const struct datatype *dtype) | |
| 1401 | { | ||
| 1402 |
2/2✓ Branch 0 (2→3) taken 10244559 times.
✓ Branch 1 (2→5) taken 309039 times.
|
10553598 | if (dtype != expr->dtype) |
| 1403 | 10244559 | __datatype_set(expr, datatype_get(dtype)); | |
| 1404 | 10553598 | } | |
| 1405 | |||
| 1406 | 5548 | struct datatype *datatype_clone(const struct datatype *orig_dtype) | |
| 1407 | { | ||
| 1408 | struct datatype *dtype; | ||
| 1409 | |||
| 1410 | 5548 | dtype = xmalloc(sizeof(*dtype)); | |
| 1411 | 5548 | *dtype = *orig_dtype; | |
| 1412 | 5548 | dtype->name = xstrdup(orig_dtype->name); | |
| 1413 | 5548 | dtype->desc = xstrdup(orig_dtype->desc); | |
| 1414 | 5548 | dtype->alloc = 1; | |
| 1415 | 5548 | dtype->refcnt = 1; | |
| 1416 | |||
| 1417 | 5548 | return dtype; | |
| 1418 | } | ||
| 1419 | |||
| 1420 | 37766461 | void datatype_free(const struct datatype *ptr) | |
| 1421 | { | ||
| 1422 | 37766461 | struct datatype *dtype = (struct datatype *)ptr; | |
| 1423 | |||
| 1424 |
2/2✓ Branch 0 (2→3) taken 54710 times.
✓ Branch 1 (2→4) taken 37711751 times.
|
37766461 | if (!dtype) |
| 1425 | 54710 | return; | |
| 1426 |
2/2✓ Branch 0 (4→5) taken 37471301 times.
✓ Branch 1 (4→6) taken 240450 times.
|
37711751 | if (!dtype->alloc) |
| 1427 | 37471301 | return; | |
| 1428 | |||
| 1429 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 240450 times.
|
240450 | assert(dtype->refcnt != 0); |
| 1430 | |||
| 1431 |
2/2✓ Branch 0 (8→9) taken 140001 times.
✓ Branch 1 (8→10) taken 100449 times.
|
240450 | if (--dtype->refcnt > 0) |
| 1432 | 140001 | return; | |
| 1433 | |||
| 1434 | 100449 | free_const(dtype->name); | |
| 1435 | 100449 | free_const(dtype->desc); | |
| 1436 | 100449 | free(dtype); | |
| 1437 | } | ||
| 1438 | |||
| 1439 | 94901 | const struct datatype *concat_type_alloc(uint32_t type) | |
| 1440 | { | ||
| 1441 | const struct datatype *i; | ||
| 1442 | struct datatype *dtype; | ||
| 1443 | 94901 | char desc[256] = "concatenation of ("; | |
| 1444 | 94901 | char name[256] = ""; | |
| 1445 | 94901 | unsigned int size = 0, subtypes = 0, n; | |
| 1446 | |||
| 1447 | 94901 | n = div_round_up(fls(type), TYPE_BITS); | |
| 1448 |
3/4✓ Branch 0 (11→12) taken 197252 times.
✓ Branch 1 (11→14) taken 94901 times.
✓ Branch 2 (13→4) taken 197252 times.
✗ Branch 3 (13→14) not taken.
|
292153 | while (n > 0 && concat_subtype_id(type, --n)) { |
| 1449 | 197252 | i = concat_subtype_lookup(type, n); | |
| 1450 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 197252 times.
|
197252 | if (i == NULL) |
| 1451 | ✗ | return NULL; | |
| 1452 | |||
| 1453 |
2/2✓ Branch 0 (7→8) taken 102353 times.
✓ Branch 1 (7→9) taken 94899 times.
|
197252 | if (subtypes != 0) { |
| 1454 | 102353 | strncat(desc, ", ", sizeof(desc) - strlen(desc) - 1); | |
| 1455 | 102353 | strncat(name, " . ", sizeof(name) - strlen(name) - 1); | |
| 1456 | } | ||
| 1457 | 197252 | strncat(desc, i->desc, sizeof(desc) - strlen(desc) - 1); | |
| 1458 | 197252 | strncat(name, i->name, sizeof(name) - strlen(name) - 1); | |
| 1459 | |||
| 1460 | 197252 | size += netlink_padded_len(i->size); | |
| 1461 | 197252 | subtypes++; | |
| 1462 | } | ||
| 1463 | 94901 | strncat(desc, ")", sizeof(desc) - strlen(desc) - 1); | |
| 1464 | |||
| 1465 | 94901 | dtype = datatype_alloc(); | |
| 1466 | 94901 | dtype->type = type; | |
| 1467 | 94901 | dtype->size = size; | |
| 1468 | 94901 | dtype->subtypes = subtypes; | |
| 1469 | 94901 | dtype->name = xstrdup(name); | |
| 1470 | 94901 | dtype->desc = xstrdup(desc); | |
| 1471 | 94901 | dtype->parse = concat_type_parse; | |
| 1472 | |||
| 1473 | 94901 | return dtype; | |
| 1474 | } | ||
| 1475 | |||
| 1476 | 47733 | const struct datatype *set_datatype_alloc(const struct datatype *orig_dtype, | |
| 1477 | enum byteorder byteorder) | ||
| 1478 | { | ||
| 1479 | struct datatype *dtype; | ||
| 1480 | |||
| 1481 | /* Restrict dynamic datatype allocation to generic integer datatype. */ | ||
| 1482 |
2/2✓ Branch 0 (2→3) taken 43771 times.
✓ Branch 1 (2→5) taken 3962 times.
|
47733 | if (orig_dtype != &integer_type) |
| 1483 | 43771 | return datatype_get(orig_dtype); | |
| 1484 | |||
| 1485 | 3962 | dtype = datatype_clone(orig_dtype); | |
| 1486 | 3962 | dtype->byteorder = byteorder; | |
| 1487 | |||
| 1488 | 3962 | return dtype; | |
| 1489 | } | ||
| 1490 | |||
| 1491 | static const struct symbol_table boolean_tbl = { | ||
| 1492 | .base = BASE_DECIMAL, | ||
| 1493 | .symbols = { | ||
| 1494 | SYMBOL("exists", true), | ||
| 1495 | SYMBOL("missing", false), | ||
| 1496 | SYMBOL_LIST_END | ||
| 1497 | }, | ||
| 1498 | }; | ||
| 1499 | |||
| 1500 | 11 | static struct error_record *boolean_type_parse(struct parse_ctx *ctx, | |
| 1501 | const struct expr *sym, | ||
| 1502 | struct expr **res) | ||
| 1503 | { | ||
| 1504 | struct error_record *erec; | ||
| 1505 | uint8_t num; | ||
| 1506 | |||
| 1507 | 11 | erec = integer_type_parse(ctx, sym, res); | |
| 1508 |
2/2✓ Branch 0 (3→4) taken 3 times.
✓ Branch 1 (3→5) taken 8 times.
|
11 | if (erec) |
| 1509 | 3 | return erec; | |
| 1510 | |||
| 1511 |
3/4✓ Branch 0 (5→6) taken 8 times.
✗ Branch 1 (5→7) not taken.
✓ Branch 2 (6→7) taken 4 times.
✓ Branch 3 (6→8) taken 4 times.
|
8 | if (mpz_cmp_ui((*res)->value, 0)) |
| 1512 | 4 | num = 1; | |
| 1513 | else | ||
| 1514 | 4 | num = 0; | |
| 1515 | |||
| 1516 | 8 | expr_free(*res); | |
| 1517 | |||
| 1518 | 8 | *res = constant_expr_alloc(&sym->location, &boolean_type, | |
| 1519 | BYTEORDER_HOST_ENDIAN, 1, &num); | ||
| 1520 | 8 | return NULL; | |
| 1521 | } | ||
| 1522 | |||
| 1523 | const struct datatype boolean_type = { | ||
| 1524 | .type = TYPE_BOOLEAN, | ||
| 1525 | .name = "boolean", | ||
| 1526 | .desc = "boolean type", | ||
| 1527 | .size = 1, | ||
| 1528 | .parse = boolean_type_parse, | ||
| 1529 | .basetype = &integer_type, | ||
| 1530 | .sym_tbl = &boolean_tbl, | ||
| 1531 | .json = boolean_type_json, | ||
| 1532 | }; | ||
| 1533 | |||
| 1534 | 4 | static struct error_record *priority_type_parse(struct parse_ctx *ctx, | |
| 1535 | const struct expr *sym, | ||
| 1536 | struct expr **res) | ||
| 1537 | { | ||
| 1538 | struct error_record *erec; | ||
| 1539 | int num; | ||
| 1540 | |||
| 1541 | 4 | erec = integer_type_parse(ctx, sym, res); | |
| 1542 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→7) taken 3 times.
|
4 | if (!erec) { |
| 1543 | 1 | num = atoi(sym->identifier); | |
| 1544 | 1 | expr_free(*res); | |
| 1545 | 1 | *res = constant_expr_alloc(&sym->location, &integer_type, | |
| 1546 | BYTEORDER_HOST_ENDIAN, | ||
| 1547 | sizeof(int) * BITS_PER_BYTE, &num); | ||
| 1548 | } else { | ||
| 1549 | 3 | erec_destroy(erec); | |
| 1550 | 3 | *res = constant_expr_alloc(&sym->location, &string_type, | |
| 1551 | BYTEORDER_HOST_ENDIAN, | ||
| 1552 | 3 | strlen(sym->identifier) * BITS_PER_BYTE, | |
| 1553 | 3 | sym->identifier); | |
| 1554 | } | ||
| 1555 | |||
| 1556 | 4 | return NULL; | |
| 1557 | } | ||
| 1558 | |||
| 1559 | /* This datatype is not registered via datatype_register() | ||
| 1560 | * since this datatype should not ever be used from either | ||
| 1561 | * rules or elements. | ||
| 1562 | */ | ||
| 1563 | const struct datatype priority_type = { | ||
| 1564 | .type = TYPE_STRING, | ||
| 1565 | .name = "priority", | ||
| 1566 | .desc = "priority type", | ||
| 1567 | .parse = priority_type_parse, | ||
| 1568 | }; | ||
| 1569 | |||
| 1570 | 4 | static struct error_record *policy_type_parse(struct parse_ctx *ctx, | |
| 1571 | const struct expr *sym, | ||
| 1572 | struct expr **res) | ||
| 1573 | { | ||
| 1574 | int policy; | ||
| 1575 | |||
| 1576 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 3 times.
|
4 | if (!strcmp(sym->identifier, "accept")) |
| 1577 | 1 | policy = NF_ACCEPT; | |
| 1578 |
2/2✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→6) taken 2 times.
|
3 | else if (!strcmp(sym->identifier, "drop")) |
| 1579 | 1 | policy = NF_DROP; | |
| 1580 | else | ||
| 1581 | 2 | return error(&sym->location, "wrong policy"); | |
| 1582 | |||
| 1583 | 2 | *res = constant_expr_alloc(&sym->location, &integer_type, | |
| 1584 | BYTEORDER_HOST_ENDIAN, | ||
| 1585 | sizeof(int) * BITS_PER_BYTE, &policy); | ||
| 1586 | 2 | return NULL; | |
| 1587 | } | ||
| 1588 | |||
| 1589 | /* This datatype is not registered via datatype_register() | ||
| 1590 | * since this datatype should not ever be used from either | ||
| 1591 | * rules or elements. | ||
| 1592 | */ | ||
| 1593 | const struct datatype policy_type = { | ||
| 1594 | .type = TYPE_STRING, | ||
| 1595 | .name = "policy", | ||
| 1596 | .desc = "policy type", | ||
| 1597 | .parse = policy_type_parse, | ||
| 1598 | }; | ||
| 1599 | |||
| 1600 | #define SYSFS_CGROUPSV2_PATH "/sys/fs/cgroup" | ||
| 1601 | |||
| 1602 | ✗ | static char *cgroupv2_get_path(const char *path, uint64_t id) | |
| 1603 | { | ||
| 1604 | char dent_name[PATH_MAX + 1]; | ||
| 1605 | ✗ | char *cgroup_path = NULL; | |
| 1606 | struct dirent *dent; | ||
| 1607 | struct stat st; | ||
| 1608 | DIR *d; | ||
| 1609 | |||
| 1610 | ✗ | d = opendir(path); | |
| 1611 | ✗ | if (!d) | |
| 1612 | ✗ | return NULL; | |
| 1613 | |||
| 1614 | ✗ | while ((dent = readdir(d)) != NULL) { | |
| 1615 | ✗ | if (!strcmp(dent->d_name, ".") || | |
| 1616 | ✗ | !strcmp(dent->d_name, "..")) | |
| 1617 | ✗ | continue; | |
| 1618 | |||
| 1619 | ✗ | snprintf(dent_name, sizeof(dent_name), "%s/%s", | |
| 1620 | ✗ | path, dent->d_name); | |
| 1621 | ✗ | dent_name[sizeof(dent_name) - 1] = '\0'; | |
| 1622 | |||
| 1623 | ✗ | if (dent->d_ino == id) { | |
| 1624 | ✗ | cgroup_path = xstrdup(dent_name); | |
| 1625 | ✗ | break; | |
| 1626 | } | ||
| 1627 | |||
| 1628 | ✗ | if (stat(dent_name, &st) >= 0 && S_ISDIR(st.st_mode)) { | |
| 1629 | ✗ | cgroup_path = cgroupv2_get_path(dent_name, id); | |
| 1630 | ✗ | if (cgroup_path) | |
| 1631 | ✗ | break; | |
| 1632 | } | ||
| 1633 | } | ||
| 1634 | ✗ | closedir(d); | |
| 1635 | |||
| 1636 | ✗ | return cgroup_path; | |
| 1637 | } | ||
| 1638 | |||
| 1639 | ✗ | static void cgroupv2_type_print(const struct expr *expr, | |
| 1640 | struct output_ctx *octx) | ||
| 1641 | { | ||
| 1642 | ✗ | uint64_t id = mpz_get_uint64(expr->value); | |
| 1643 | char *cgroup_path; | ||
| 1644 | |||
| 1645 | ✗ | cgroup_path = cgroupv2_get_path(SYSFS_CGROUPSV2_PATH, id); | |
| 1646 | ✗ | if (cgroup_path) | |
| 1647 | ✗ | nft_print(octx, "\"%s\"", | |
| 1648 | &cgroup_path[strlen(SYSFS_CGROUPSV2_PATH) + 1]); | ||
| 1649 | else | ||
| 1650 | ✗ | nft_print(octx, "%" PRIu64, id); | |
| 1651 | |||
| 1652 | ✗ | free(cgroup_path); | |
| 1653 | ✗ | } | |
| 1654 | |||
| 1655 | 1 | static struct error_record *cgroupv2_type_parse(struct parse_ctx *ctx, | |
| 1656 | const struct expr *sym, | ||
| 1657 | struct expr **res) | ||
| 1658 | { | ||
| 1659 | char cgroupv2_path[PATH_MAX + 1]; | ||
| 1660 | struct stat st; | ||
| 1661 | uint64_t ino; | ||
| 1662 | |||
| 1663 | 1 | snprintf(cgroupv2_path, sizeof(cgroupv2_path), "%s/%s", | |
| 1664 | 1 | SYSFS_CGROUPSV2_PATH, sym->identifier); | |
| 1665 | 1 | cgroupv2_path[sizeof(cgroupv2_path) - 1] = '\0'; | |
| 1666 | |||
| 1667 |
1/2✓ Branch 0 (3→4) taken 1 times.
✗ Branch 1 (3→7) not taken.
|
1 | if (stat(cgroupv2_path, &st) < 0) |
| 1668 | 1 | return error(&sym->location, "cgroupv2 path fails: %s", | |
| 1669 | strerror(errno)); | ||
| 1670 | |||
| 1671 | ✗ | ino = st.st_ino; | |
| 1672 | ✗ | *res = constant_expr_alloc(&sym->location, &cgroupv2_type, | |
| 1673 | BYTEORDER_HOST_ENDIAN, | ||
| 1674 | sizeof(ino) * BITS_PER_BYTE, &ino); | ||
| 1675 | ✗ | return NULL; | |
| 1676 | } | ||
| 1677 | |||
| 1678 | const struct datatype cgroupv2_type = { | ||
| 1679 | .type = TYPE_CGROUPV2, | ||
| 1680 | .name = "cgroupsv2", | ||
| 1681 | .desc = "cgroupsv2 path", | ||
| 1682 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 1683 | .size = 8 * BITS_PER_BYTE, | ||
| 1684 | .basetype = &integer_type, | ||
| 1685 | .print = cgroupv2_type_print, | ||
| 1686 | .parse = cgroupv2_type_parse, | ||
| 1687 | }; | ||
| 1688 |