| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Meta expression/statement related definition and types. | ||
| 3 | * | ||
| 4 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License version 2 as | ||
| 8 | * published by the Free Software Foundation. | ||
| 9 | * | ||
| 10 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include <nft.h> | ||
| 14 | |||
| 15 | #include <errno.h> | ||
| 16 | #include <limits.h> | ||
| 17 | #include <stddef.h> | ||
| 18 | #include <stdio.h> | ||
| 19 | #include <net/if.h> | ||
| 20 | #include <net/if_arp.h> | ||
| 21 | #include <pwd.h> | ||
| 22 | #include <grp.h> | ||
| 23 | #include <arpa/inet.h> | ||
| 24 | #include <linux/netfilter.h> | ||
| 25 | #include <linux/pkt_sched.h> | ||
| 26 | #include <linux/if_ether.h> | ||
| 27 | #include <linux/if_packet.h> | ||
| 28 | #include <time.h> | ||
| 29 | |||
| 30 | #include <nftables.h> | ||
| 31 | #include <expression.h> | ||
| 32 | #include <statement.h> | ||
| 33 | #include <datatype.h> | ||
| 34 | #include <meta.h> | ||
| 35 | #include <gmputil.h> | ||
| 36 | #include <utils.h> | ||
| 37 | #include <erec.h> | ||
| 38 | #include <iface.h> | ||
| 39 | #include <json.h> | ||
| 40 | |||
| 41 | 304 | static void tchandle_type_print(const struct expr *expr, | |
| 42 | struct output_ctx *octx) | ||
| 43 | { | ||
| 44 | 304 | uint32_t handle = mpz_get_uint32(expr->value); | |
| 45 | |||
| 46 |
3/3✓ Branch 0 (3→4) taken 14 times.
✓ Branch 1 (3→6) taken 14 times.
✓ Branch 2 (3→8) taken 276 times.
|
304 | switch(handle) { |
| 47 | 14 | case TC_H_ROOT: | |
| 48 | 14 | nft_print(octx, "root"); | |
| 49 | 14 | break; | |
| 50 | 14 | case TC_H_UNSPEC: | |
| 51 | 14 | nft_print(octx, "none"); | |
| 52 | 14 | break; | |
| 53 | 276 | default: | |
| 54 | 276 | nft_print(octx, "%0x:%0x", | |
| 55 | TC_H_MAJ(handle) >> 16, | ||
| 56 | TC_H_MIN(handle)); | ||
| 57 | 276 | break; | |
| 58 | } | ||
| 59 | 304 | } | |
| 60 | |||
| 61 | 357 | static struct error_record *tchandle_type_parse(struct parse_ctx *ctx, | |
| 62 | const struct expr *sym, | ||
| 63 | struct expr **res) | ||
| 64 | { | ||
| 65 | uint32_t handle; | ||
| 66 | |||
| 67 |
2/2✓ Branch 0 (2→3) taken 14 times.
✓ Branch 1 (2→4) taken 343 times.
|
357 | if (strcmp(sym->identifier, "root") == 0) |
| 68 | 14 | handle = TC_H_ROOT; | |
| 69 |
2/2✓ Branch 0 (4→5) taken 14 times.
✓ Branch 1 (4→6) taken 329 times.
|
343 | else if (strcmp(sym->identifier, "none") == 0) |
| 70 | 14 | handle = TC_H_UNSPEC; | |
| 71 |
2/2✓ Branch 0 (6→7) taken 300 times.
✓ Branch 1 (6→19) taken 29 times.
|
329 | else if (strchr(sym->identifier, ':')) { |
| 72 | char *colon, *end; | ||
| 73 | uint32_t tmp; | ||
| 74 | |||
| 75 | 300 | errno = 0; | |
| 76 | 300 | tmp = strtoul(sym->identifier, &colon, 16); | |
| 77 |
3/4✓ Branch 0 (8→9) taken 300 times.
✗ Branch 1 (8→10) not taken.
✓ Branch 2 (9→10) taken 1 times.
✓ Branch 3 (9→11) taken 299 times.
|
300 | if (errno != 0 || sym->identifier == colon) |
| 78 | 1 | goto err; | |
| 79 | |||
| 80 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 299 times.
|
299 | if (*colon != ':') |
| 81 | ✗ | goto err; | |
| 82 | |||
| 83 | 299 | handle = tmp << 16; | |
| 84 | 299 | errno = 0; | |
| 85 | 299 | tmp = strtoul(colon + 1, &end, 16); | |
| 86 |
2/4✓ Branch 0 (14→15) taken 299 times.
✗ Branch 1 (14→16) not taken.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→17) taken 299 times.
|
299 | if (errno != 0 || *end) |
| 87 | ✗ | goto err; | |
| 88 | |||
| 89 | 299 | handle |= tmp; | |
| 90 | } else { | ||
| 91 | 29 | handle = strtoull(sym->identifier, NULL, 0); | |
| 92 | } | ||
| 93 | |||
| 94 | 356 | *res = constant_expr_alloc(&sym->location, sym->dtype, | |
| 95 | BYTEORDER_HOST_ENDIAN, | ||
| 96 | sizeof(handle) * BITS_PER_BYTE, &handle); | ||
| 97 | 356 | return NULL; | |
| 98 | 1 | err: | |
| 99 | 1 | return error(&sym->location, "Could not parse %s", sym->dtype->desc); | |
| 100 | } | ||
| 101 | |||
| 102 | const struct datatype tchandle_type = { | ||
| 103 | .type = TYPE_CLASSID, | ||
| 104 | .name = "classid", | ||
| 105 | .desc = "TC classid", | ||
| 106 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 107 | .size = 4 * BITS_PER_BYTE, | ||
| 108 | .basetype = &integer_type, | ||
| 109 | .print = tchandle_type_print, | ||
| 110 | .parse = tchandle_type_parse, | ||
| 111 | }; | ||
| 112 | |||
| 113 | 439 | static void ifindex_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 114 | { | ||
| 115 | char name[IFNAMSIZ]; | ||
| 116 | int ifindex; | ||
| 117 | |||
| 118 | 439 | ifindex = mpz_get_uint32(expr->value); | |
| 119 |
2/2✓ Branch 0 (4→5) taken 431 times.
✓ Branch 1 (4→6) taken 8 times.
|
439 | if (nft_if_indextoname(ifindex, name)) |
| 120 | 431 | nft_print(octx, "\"%s\"", name); | |
| 121 | else | ||
| 122 | 8 | nft_print(octx, "%d", ifindex); | |
| 123 | 439 | } | |
| 124 | |||
| 125 | 1332 | static struct error_record *ifindex_type_parse(struct parse_ctx *ctx, | |
| 126 | const struct expr *sym, | ||
| 127 | struct expr **res) | ||
| 128 | { | ||
| 129 | int ifindex; | ||
| 130 | |||
| 131 | 1332 | ifindex = nft_if_nametoindex(sym->identifier); | |
| 132 |
2/2✓ Branch 0 (3→4) taken 25 times.
✓ Branch 1 (3→13) taken 1307 times.
|
1332 | if (ifindex == 0) { |
| 133 | char *end; | ||
| 134 | long res; | ||
| 135 | |||
| 136 | 25 | errno = 0; | |
| 137 | 25 | res = strtol(sym->identifier, &end, 10); | |
| 138 | |||
| 139 |
4/8✓ Branch 0 (5→6) taken 25 times.
✗ Branch 1 (5→9) not taken.
✓ Branch 2 (6→7) taken 25 times.
✗ Branch 3 (6→9) not taken.
✓ Branch 4 (7→8) taken 25 times.
✗ Branch 5 (7→9) not taken.
✗ Branch 6 (8→9) not taken.
✓ Branch 7 (8→11) taken 25 times.
|
25 | if (res < 0 || res > INT_MAX || *end || errno) |
| 140 | ✗ | return error(&sym->location, "Interface does not exist"); | |
| 141 | |||
| 142 | 25 | ifindex = (int)res; | |
| 143 | } | ||
| 144 | |||
| 145 | 1332 | *res = constant_expr_alloc(&sym->location, sym->dtype, | |
| 146 | BYTEORDER_HOST_ENDIAN, | ||
| 147 | sizeof(ifindex) * BITS_PER_BYTE, &ifindex); | ||
| 148 | 1332 | return NULL; | |
| 149 | } | ||
| 150 | |||
| 151 | const struct datatype ifindex_type = { | ||
| 152 | .type = TYPE_IFINDEX, | ||
| 153 | .name = "iface_index", | ||
| 154 | .desc = "network interface index", | ||
| 155 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 156 | .size = 4 * BITS_PER_BYTE, | ||
| 157 | .basetype = &integer_type, | ||
| 158 | .print = ifindex_type_print, | ||
| 159 | .parse = ifindex_type_parse, | ||
| 160 | }; | ||
| 161 | |||
| 162 | static const struct symbol_table arphrd_tbl = { | ||
| 163 | .base = BASE_HEXADECIMAL, | ||
| 164 | .symbols = { | ||
| 165 | SYMBOL("ether", ARPHRD_ETHER), | ||
| 166 | SYMBOL("ppp", ARPHRD_PPP), | ||
| 167 | /* dummy types */ | ||
| 168 | SYMBOL("ipip", ARPHRD_TUNNEL), | ||
| 169 | SYMBOL("ipip6", ARPHRD_TUNNEL6), | ||
| 170 | SYMBOL("loopback", ARPHRD_LOOPBACK), | ||
| 171 | SYMBOL("sit", ARPHRD_SIT), | ||
| 172 | SYMBOL("ipgre", ARPHRD_IPGRE), | ||
| 173 | SYMBOL_LIST_END, | ||
| 174 | }, | ||
| 175 | }; | ||
| 176 | |||
| 177 | const struct datatype arphrd_type = { | ||
| 178 | .type = TYPE_ARPHRD, | ||
| 179 | .name = "iface_type", | ||
| 180 | .desc = "network interface type", | ||
| 181 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 182 | .size = 2 * BITS_PER_BYTE, | ||
| 183 | .basetype = &integer_type, | ||
| 184 | .sym_tbl = &arphrd_tbl, | ||
| 185 | }; | ||
| 186 | |||
| 187 | 168 | static void uid_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 188 | { | ||
| 189 | struct passwd *pw; | ||
| 190 | |||
| 191 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→10) taken 168 times.
|
168 | if (nft_output_guid(octx)) { |
| 192 | ✗ | uint32_t uid = mpz_get_uint32(expr->value); | |
| 193 | |||
| 194 | ✗ | pw = getpwuid(uid); | |
| 195 | ✗ | if (pw != NULL) | |
| 196 | ✗ | nft_print(octx, "\"%s\"", pw->pw_name); | |
| 197 | else | ||
| 198 | ✗ | nft_print(octx, "%d", uid); | |
| 199 | ✗ | return; | |
| 200 | } | ||
| 201 | 168 | expr_basetype(expr)->print(expr, octx); | |
| 202 | } | ||
| 203 | |||
| 204 | 413 | static struct error_record *uid_type_parse(struct parse_ctx *ctx, | |
| 205 | const struct expr *sym, | ||
| 206 | struct expr **res) | ||
| 207 | { | ||
| 208 | struct passwd *pw; | ||
| 209 | uid_t uid; | ||
| 210 | 413 | char *endptr = NULL; | |
| 211 | |||
| 212 | 413 | pw = getpwnam(sym->identifier); | |
| 213 |
2/2✓ Branch 0 (3→4) taken 112 times.
✓ Branch 1 (3→5) taken 301 times.
|
413 | if (pw != NULL) |
| 214 | 112 | uid = pw->pw_uid; | |
| 215 | else { | ||
| 216 | 301 | uint64_t _uid = strtoull(sym->identifier, &endptr, 10); | |
| 217 | |||
| 218 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 301 times.
|
301 | if (_uid > UINT32_MAX) |
| 219 | ✗ | return error(&sym->location, "Value too large"); | |
| 220 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 301 times.
|
301 | else if (*endptr) |
| 221 | ✗ | return error(&sym->location, "User does not exist"); | |
| 222 | 301 | uid = _uid; | |
| 223 | } | ||
| 224 | |||
| 225 | 413 | *res = constant_expr_alloc(&sym->location, sym->dtype, | |
| 226 | BYTEORDER_HOST_ENDIAN, | ||
| 227 | sizeof(pw->pw_uid) * BITS_PER_BYTE, &uid); | ||
| 228 | 413 | return NULL; | |
| 229 | } | ||
| 230 | |||
| 231 | const struct datatype uid_type = { | ||
| 232 | .type = TYPE_UID, | ||
| 233 | .name = "uid", | ||
| 234 | .desc = "user ID", | ||
| 235 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 236 | .size = sizeof(uid_t) * BITS_PER_BYTE, | ||
| 237 | .basetype = &integer_type, | ||
| 238 | .print = uid_type_print, | ||
| 239 | .json = uid_type_json, | ||
| 240 | .parse = uid_type_parse, | ||
| 241 | }; | ||
| 242 | |||
| 243 | 112 | static void gid_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 244 | { | ||
| 245 | struct group *gr; | ||
| 246 | |||
| 247 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→10) taken 112 times.
|
112 | if (nft_output_guid(octx)) { |
| 248 | ✗ | uint32_t gid = mpz_get_uint32(expr->value); | |
| 249 | |||
| 250 | ✗ | gr = getgrgid(gid); | |
| 251 | ✗ | if (gr != NULL) | |
| 252 | ✗ | nft_print(octx, "\"%s\"", gr->gr_name); | |
| 253 | else | ||
| 254 | ✗ | nft_print(octx, "%u", gid); | |
| 255 | ✗ | return; | |
| 256 | } | ||
| 257 | 112 | expr_basetype(expr)->print(expr, octx); | |
| 258 | } | ||
| 259 | |||
| 260 | 301 | static struct error_record *gid_type_parse(struct parse_ctx *ctx, | |
| 261 | const struct expr *sym, | ||
| 262 | struct expr **res) | ||
| 263 | { | ||
| 264 | struct group *gr; | ||
| 265 | gid_t gid; | ||
| 266 | 301 | char *endptr = NULL; | |
| 267 | |||
| 268 | 301 | gr = getgrnam(sym->identifier); | |
| 269 |
2/2✓ Branch 0 (3→4) taken 112 times.
✓ Branch 1 (3→5) taken 189 times.
|
301 | if (gr != NULL) |
| 270 | 112 | gid = gr->gr_gid; | |
| 271 | else { | ||
| 272 | 189 | uint64_t _gid = strtoull(sym->identifier, &endptr, 0); | |
| 273 | |||
| 274 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 189 times.
|
189 | if (_gid > UINT32_MAX) |
| 275 | ✗ | return error(&sym->location, "Value too large"); | |
| 276 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 189 times.
|
189 | else if (*endptr) |
| 277 | ✗ | return error(&sym->location, "Group does not exist"); | |
| 278 | 189 | gid = _gid; | |
| 279 | } | ||
| 280 | |||
| 281 | 301 | *res = constant_expr_alloc(&sym->location, sym->dtype, | |
| 282 | BYTEORDER_HOST_ENDIAN, | ||
| 283 | sizeof(gr->gr_gid) * BITS_PER_BYTE, &gid); | ||
| 284 | 301 | return NULL; | |
| 285 | } | ||
| 286 | |||
| 287 | const struct datatype gid_type = { | ||
| 288 | .type = TYPE_GID, | ||
| 289 | .name = "gid", | ||
| 290 | .desc = "group ID", | ||
| 291 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 292 | .size = sizeof(gid_t) * BITS_PER_BYTE, | ||
| 293 | .basetype = &integer_type, | ||
| 294 | .print = gid_type_print, | ||
| 295 | .json = gid_type_json, | ||
| 296 | .parse = gid_type_parse, | ||
| 297 | }; | ||
| 298 | |||
| 299 | static const struct symbol_table pkttype_type_tbl = { | ||
| 300 | .base = BASE_DECIMAL, | ||
| 301 | .symbols = { | ||
| 302 | SYMBOL("host", PACKET_HOST), | ||
| 303 | SYMBOL("unicast", PACKET_HOST), /* backwards compat */ | ||
| 304 | SYMBOL("broadcast", PACKET_BROADCAST), | ||
| 305 | SYMBOL("multicast", PACKET_MULTICAST), | ||
| 306 | SYMBOL("other", PACKET_OTHERHOST), | ||
| 307 | SYMBOL_LIST_END, | ||
| 308 | }, | ||
| 309 | }; | ||
| 310 | |||
| 311 | 59 | static void pkttype_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 312 | { | ||
| 313 | 59 | return symbolic_constant_print(&pkttype_type_tbl, expr, false, octx); | |
| 314 | } | ||
| 315 | |||
| 316 | const struct datatype pkttype_type = { | ||
| 317 | .type = TYPE_PKTTYPE, | ||
| 318 | .name = "pkt_type", | ||
| 319 | .desc = "packet type", | ||
| 320 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 321 | .size = BITS_PER_BYTE, | ||
| 322 | .basetype = &integer_type, | ||
| 323 | .print = pkttype_type_print, | ||
| 324 | .sym_tbl = &pkttype_type_tbl, | ||
| 325 | }; | ||
| 326 | |||
| 327 | 13022 | void devgroup_table_init(struct nft_ctx *ctx) | |
| 328 | { | ||
| 329 | 13022 | ctx->output.tbl.devgroup = rt_symbol_table_init("group"); | |
| 330 | 13022 | } | |
| 331 | |||
| 332 | 13022 | void devgroup_table_exit(struct nft_ctx *ctx) | |
| 333 | { | ||
| 334 | 13022 | rt_symbol_table_free(ctx->output.tbl.devgroup); | |
| 335 | 13022 | } | |
| 336 | |||
| 337 | 280 | static void devgroup_type_print(const struct expr *expr, | |
| 338 | struct output_ctx *octx) | ||
| 339 | { | ||
| 340 | 280 | return symbolic_constant_print(octx->tbl.devgroup, expr, true, octx); | |
| 341 | } | ||
| 342 | |||
| 343 | 868 | static struct error_record *devgroup_type_parse(struct parse_ctx *ctx, | |
| 344 | const struct expr *sym, | ||
| 345 | struct expr **res) | ||
| 346 | { | ||
| 347 | 868 | return symbolic_constant_parse(ctx, sym, ctx->tbl->devgroup, res); | |
| 348 | } | ||
| 349 | |||
| 350 | ✗ | static void devgroup_type_describe(struct output_ctx *octx) | |
| 351 | { | ||
| 352 | ✗ | rt_symbol_table_describe(octx, "group", | |
| 353 | octx->tbl.devgroup, &devgroup_type); | ||
| 354 | ✗ | } | |
| 355 | |||
| 356 | const struct datatype devgroup_type = { | ||
| 357 | .type = TYPE_DEVGROUP, | ||
| 358 | .name = "devgroup", | ||
| 359 | .desc = "devgroup name", | ||
| 360 | .describe = devgroup_type_describe, | ||
| 361 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 362 | .size = 4 * BITS_PER_BYTE, | ||
| 363 | .basetype = &integer_type, | ||
| 364 | .print = devgroup_type_print, | ||
| 365 | .json = devgroup_type_json, | ||
| 366 | .parse = devgroup_type_parse, | ||
| 367 | }; | ||
| 368 | |||
| 369 | const struct datatype ifname_type = { | ||
| 370 | .type = TYPE_IFNAME, | ||
| 371 | .name = "ifname", | ||
| 372 | .desc = "network interface name", | ||
| 373 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 374 | .size = IFNAMSIZ * BITS_PER_BYTE, | ||
| 375 | .basetype = &string_type, | ||
| 376 | }; | ||
| 377 | |||
| 378 | 140 | static void date_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 379 | { | ||
| 380 | 140 | uint64_t tstamp64 = mpz_get_uint64(expr->value); | |
| 381 | char timestr[21]; | ||
| 382 | time_t tstamp; | ||
| 383 | struct tm tm; | ||
| 384 | |||
| 385 | /* Convert from nanoseconds to seconds */ | ||
| 386 | 140 | tstamp64 /= 1000000000L; | |
| 387 | |||
| 388 | /* Obtain current tm, to add tm_gmtoff to the timestamp */ | ||
| 389 | 140 | tstamp = tstamp64; | |
| 390 |
1/2✓ Branch 0 (4→5) taken 140 times.
✗ Branch 1 (4→6) not taken.
|
140 | if (localtime_r(&tstamp, &tm)) |
| 391 | 140 | tstamp64 += tm.tm_gmtoff; | |
| 392 | |||
| 393 | 140 | tstamp = tstamp64; | |
| 394 |
1/2✓ Branch 0 (7→8) taken 140 times.
✗ Branch 1 (7→10) not taken.
|
140 | if (gmtime_r(&tstamp, &tm) && |
| 395 |
1/2✓ Branch 0 (8→9) taken 140 times.
✗ Branch 1 (8→10) not taken.
|
140 | strftime(timestr, sizeof(timestr) - 1, "%Y-%m-%d %T", &tm)) |
| 396 | 140 | nft_print(octx, "\"%s\"", timestr); | |
| 397 | else | ||
| 398 | ✗ | nft_print(octx, "Error converting timestamp to printed time"); | |
| 399 | 140 | } | |
| 400 | |||
| 401 | 173 | static bool parse_iso_date(uint64_t *tstamp, const char *sym) | |
| 402 | { | ||
| 403 | struct tm cur_tm; | ||
| 404 | struct tm tm; | ||
| 405 | time_t ts; | ||
| 406 | |||
| 407 | 173 | memset(&tm, 0, sizeof(struct tm)); | |
| 408 | |||
| 409 |
2/2✓ Branch 0 (3→4) taken 152 times.
✓ Branch 1 (3→5) taken 21 times.
|
173 | if (strptime(sym, "%Y-%m-%d %T", &tm)) |
| 410 | 152 | goto success; | |
| 411 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 21 times.
|
21 | if (strptime(sym, "%Y-%m-%d %R", &tm)) |
| 412 | ✗ | goto success; | |
| 413 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 21 times.
|
21 | if (strptime(sym, "%Y-%m-%d", &tm)) |
| 414 | ✗ | goto success; | |
| 415 | |||
| 416 | 21 | return false; | |
| 417 | |||
| 418 | 152 | success: | |
| 419 | /* | ||
| 420 | * Overwriting TZ is problematic if we're parsing hour types in this same process, | ||
| 421 | * hence I'd rather use timegm() which doesn't take into account the TZ env variable, | ||
| 422 | * even though it's Linux-specific. | ||
| 423 | */ | ||
| 424 | 152 | ts = timegm(&tm); | |
| 425 | |||
| 426 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 152 times.
|
152 | if (ts == (time_t) -1) |
| 427 | ✗ | return false; | |
| 428 | |||
| 429 | /* Obtain current tm as well (at the specified time), so that we can substract tm_gmtoff */ | ||
| 430 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 152 times.
|
152 | if (!localtime_r(&ts, &cur_tm)) |
| 431 | ✗ | return false; | |
| 432 | |||
| 433 | /* Substract tm_gmtoff to get the current time */ | ||
| 434 | 152 | *tstamp = ts - cur_tm.tm_gmtoff; | |
| 435 | |||
| 436 | 152 | return true; | |
| 437 | } | ||
| 438 | |||
| 439 | 173 | static struct error_record *date_type_parse(struct parse_ctx *ctx, | |
| 440 | const struct expr *sym, | ||
| 441 | struct expr **res) | ||
| 442 | { | ||
| 443 | 173 | const char *endptr = sym->identifier; | |
| 444 | uint64_t tstamp; | ||
| 445 | |||
| 446 |
2/2✓ Branch 0 (3→4) taken 152 times.
✓ Branch 1 (3→5) taken 21 times.
|
173 | if (parse_iso_date(&tstamp, sym->identifier)) |
| 447 | 152 | goto success; | |
| 448 | |||
| 449 | 21 | tstamp = strtoul(sym->identifier, (char **) &endptr, 10); | |
| 450 |
3/4✓ Branch 0 (6→7) taken 14 times.
✓ Branch 1 (6→9) taken 7 times.
✓ Branch 2 (7→8) taken 14 times.
✗ Branch 3 (7→9) not taken.
|
21 | if (*endptr == '\0' && endptr != sym->identifier) |
| 451 | 14 | goto success; | |
| 452 | |||
| 453 | 7 | return error(&sym->location, "Cannot parse date"); | |
| 454 | |||
| 455 | 166 | success: | |
| 456 | /* Convert to nanoseconds */ | ||
| 457 | 166 | tstamp *= 1000000000L; | |
| 458 | 166 | *res = constant_expr_alloc(&sym->location, sym->dtype, | |
| 459 | BYTEORDER_HOST_ENDIAN, | ||
| 460 | sizeof(uint64_t) * BITS_PER_BYTE, | ||
| 461 | &tstamp); | ||
| 462 | 166 | return NULL; | |
| 463 | } | ||
| 464 | |||
| 465 | static const struct symbol_table day_type_tbl = { | ||
| 466 | .base = BASE_DECIMAL, | ||
| 467 | .symbols = { | ||
| 468 | SYMBOL("Sunday", 0), | ||
| 469 | SYMBOL("Monday", 1), | ||
| 470 | SYMBOL("Tuesday", 2), | ||
| 471 | SYMBOL("Wednesday", 3), | ||
| 472 | SYMBOL("Thursday", 4), | ||
| 473 | SYMBOL("Friday", 5), | ||
| 474 | SYMBOL("Saturday", 6), | ||
| 475 | SYMBOL_LIST_END, | ||
| 476 | }, | ||
| 477 | }; | ||
| 478 | |||
| 479 | 14 | static void day_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 480 | { | ||
| 481 | 14 | return symbolic_constant_print(&day_type_tbl, expr, true, octx); | |
| 482 | } | ||
| 483 | |||
| 484 | #define SECONDS_PER_DAY (60 * 60 * 24) | ||
| 485 | |||
| 486 | 432 | static void hour_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 487 | { | ||
| 488 | 432 | uint32_t seconds = mpz_get_uint32(expr->value), minutes, hours; | |
| 489 | struct tm cur_tm; | ||
| 490 | time_t ts; | ||
| 491 | |||
| 492 | /* Obtain current tm, so that we can add tm_gmtoff */ | ||
| 493 | 432 | ts = time(NULL); | |
| 494 |
2/4✓ Branch 0 (4→5) taken 432 times.
✗ Branch 1 (4→12) not taken.
✓ Branch 2 (6→7) taken 432 times.
✗ Branch 3 (6→12) not taken.
|
432 | if (ts != ((time_t) -1) && localtime_r(&ts, &cur_tm)) { |
| 495 | 432 | int32_t adj = seconds + cur_tm.tm_gmtoff; | |
| 496 | |||
| 497 |
2/2✓ Branch 0 (7→8) taken 2 times.
✓ Branch 1 (7→9) taken 430 times.
|
432 | if (adj < 0) |
| 498 | 2 | adj += SECONDS_PER_DAY; | |
| 499 |
2/2✓ Branch 0 (9→10) taken 102 times.
✓ Branch 1 (9→11) taken 328 times.
|
430 | else if (adj >= SECONDS_PER_DAY) |
| 500 | 102 | adj -= SECONDS_PER_DAY; | |
| 501 | |||
| 502 | 432 | seconds = adj; | |
| 503 | } | ||
| 504 | 432 | minutes = seconds / 60; | |
| 505 | 432 | seconds %= 60; | |
| 506 | 432 | hours = minutes / 60; | |
| 507 | 432 | minutes %= 60; | |
| 508 | |||
| 509 | 432 | nft_print(octx, "\"%02d:%02d", hours, minutes); | |
| 510 |
2/2✓ Branch 0 (13→14) taken 14 times.
✓ Branch 1 (13→15) taken 418 times.
|
432 | if (seconds) |
| 511 | 14 | nft_print(octx, ":%02d", seconds); | |
| 512 | 432 | nft_print(octx, "\""); | |
| 513 | 432 | } | |
| 514 | |||
| 515 | 353 | static struct error_record *hour_type_parse(struct parse_ctx *ctx, | |
| 516 | const struct expr *sym, | ||
| 517 | struct expr **res) | ||
| 518 | { | ||
| 519 | struct error_record *er; | ||
| 520 | struct tm cur_tm_data; | ||
| 521 | struct tm *cur_tm; | ||
| 522 | uint32_t result; | ||
| 523 | uint64_t tmp; | ||
| 524 | char *endptr; | ||
| 525 | struct tm tm; | ||
| 526 | time_t ts; | ||
| 527 | |||
| 528 | 353 | memset(&tm, 0, sizeof(struct tm)); | |
| 529 | |||
| 530 | /* First, try to parse it as a number */ | ||
| 531 | 353 | result = strtoul(sym->identifier, (char **) &endptr, 10); | |
| 532 |
1/4✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 353 times.
✗ Branch 2 (4→5) not taken.
✗ Branch 3 (4→6) not taken.
|
353 | if (*endptr == '\0' && endptr != sym->identifier) |
| 533 | ✗ | goto success; | |
| 534 | |||
| 535 | 353 | result = 0; | |
| 536 | |||
| 537 | /* Obtain current tm, so that we can substract tm_gmtoff */ | ||
| 538 | 353 | ts = time(NULL); | |
| 539 |
2/4✓ Branch 0 (7→8) taken 353 times.
✗ Branch 1 (7→11) not taken.
✓ Branch 2 (9→10) taken 353 times.
✗ Branch 3 (9→11) not taken.
|
353 | if (ts != ((time_t) -1) && localtime_r(&ts, &cur_tm_data)) |
| 540 | 353 | cur_tm = &cur_tm_data; | |
| 541 | else | ||
| 542 | ✗ | cur_tm = NULL; | |
| 543 | |||
| 544 | 353 | endptr = strptime(sym->identifier, "%T", &tm); | |
| 545 |
3/4✓ Branch 0 (13→14) taken 42 times.
✓ Branch 1 (13→16) taken 311 times.
✓ Branch 2 (14→15) taken 42 times.
✗ Branch 3 (14→16) not taken.
|
353 | if (endptr && *endptr == '\0') |
| 546 | 42 | goto convert; | |
| 547 | |||
| 548 | 311 | endptr = strptime(sym->identifier, "%R", &tm); | |
| 549 |
3/4✓ Branch 0 (17→18) taken 304 times.
✓ Branch 1 (17→20) taken 7 times.
✓ Branch 2 (18→19) taken 304 times.
✗ Branch 3 (18→20) not taken.
|
311 | if (endptr && *endptr == '\0') |
| 550 | 304 | goto convert; | |
| 551 | |||
| 552 |
1/4✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→24) taken 7 times.
✗ Branch 2 (21→22) not taken.
✗ Branch 3 (21→24) not taken.
|
7 | if (endptr && *endptr) |
| 553 | ✗ | return error(&sym->location, "Can't parse trailing input: \"%s\"\n", endptr); | |
| 554 | |||
| 555 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 7 times.
|
7 | if ((er = time_parse(&sym->location, sym->identifier, &tmp)) == NULL) { |
| 556 | ✗ | result = tmp / 1000; | |
| 557 | ✗ | goto convert; | |
| 558 | } | ||
| 559 | |||
| 560 | 7 | return er; | |
| 561 | |||
| 562 | 346 | convert: | |
| 563 | /* Convert the hour to the number of seconds since midnight */ | ||
| 564 |
1/2✓ Branch 0 (28→29) taken 346 times.
✗ Branch 1 (28→30) not taken.
|
346 | if (result == 0) |
| 565 | 346 | result = tm.tm_hour * 3600 + tm.tm_min * 60 + tm.tm_sec; | |
| 566 | |||
| 567 | /* Substract tm_gmtoff to get the current time */ | ||
| 568 |
1/2✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→32) taken 346 times.
|
346 | if (cur_tm) { |
| 569 |
2/2✓ Branch 0 (32→33) taken 261 times.
✓ Branch 1 (32→34) taken 85 times.
|
346 | if ((long int) result >= cur_tm->tm_gmtoff) |
| 570 | 261 | result = (result - cur_tm->tm_gmtoff) % 86400; | |
| 571 | else | ||
| 572 | 85 | result = 86400 - cur_tm->tm_gmtoff + result; | |
| 573 | } | ||
| 574 | |||
| 575 | ✗ | success: | |
| 576 | 346 | *res = constant_expr_alloc(&sym->location, sym->dtype, | |
| 577 | BYTEORDER_HOST_ENDIAN, | ||
| 578 | sizeof(uint32_t) * BITS_PER_BYTE, | ||
| 579 | &result); | ||
| 580 | 346 | return NULL; | |
| 581 | } | ||
| 582 | |||
| 583 | const struct datatype date_type = { | ||
| 584 | .type = TYPE_TIME_DATE, | ||
| 585 | .name = "time", | ||
| 586 | .desc = "Relative time of packet reception", | ||
| 587 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 588 | .size = sizeof(uint64_t) * BITS_PER_BYTE, | ||
| 589 | .basetype = &integer_type, | ||
| 590 | .print = date_type_print, | ||
| 591 | .parse = date_type_parse, | ||
| 592 | }; | ||
| 593 | |||
| 594 | const struct datatype day_type = { | ||
| 595 | .type = TYPE_TIME_DAY, | ||
| 596 | .name = "day", | ||
| 597 | .desc = "Day of week of packet reception", | ||
| 598 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 599 | .size = 1 * BITS_PER_BYTE, | ||
| 600 | .basetype = &integer_type, | ||
| 601 | .print = day_type_print, | ||
| 602 | .sym_tbl = &day_type_tbl, | ||
| 603 | }; | ||
| 604 | |||
| 605 | const struct datatype hour_type = { | ||
| 606 | .type = TYPE_TIME_HOUR, | ||
| 607 | .name = "hour", | ||
| 608 | .desc = "Hour of day of packet reception", | ||
| 609 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 610 | .size = sizeof(uint32_t) * BITS_PER_BYTE, | ||
| 611 | .basetype = &integer_type, | ||
| 612 | .print = hour_type_print, | ||
| 613 | .parse = hour_type_parse, | ||
| 614 | }; | ||
| 615 | |||
| 616 | const struct meta_template meta_templates[] = { | ||
| 617 | [NFT_META_LEN] = META_TEMPLATE("length", &integer_type, | ||
| 618 | 4 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 619 | [NFT_META_PROTOCOL] = META_TEMPLATE("protocol", ðertype_type, | ||
| 620 | 2 * 8, BYTEORDER_BIG_ENDIAN), | ||
| 621 | [NFT_META_NFPROTO] = META_TEMPLATE("nfproto", &nfproto_type, | ||
| 622 | 1 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 623 | [NFT_META_L4PROTO] = META_TEMPLATE("l4proto", &inet_protocol_type, | ||
| 624 | 1 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 625 | [NFT_META_PRIORITY] = META_TEMPLATE("priority", &tchandle_type, | ||
| 626 | 4 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 627 | [NFT_META_MARK] = META_TEMPLATE("mark", &mark_type, | ||
| 628 | 4 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 629 | [NFT_META_IIF] = META_TEMPLATE("iif", &ifindex_type, | ||
| 630 | 4 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 631 | [NFT_META_IIFNAME] = META_TEMPLATE("iifname", &ifname_type, | ||
| 632 | IFNAMSIZ * BITS_PER_BYTE, | ||
| 633 | BYTEORDER_HOST_ENDIAN), | ||
| 634 | [NFT_META_IIFTYPE] = META_TEMPLATE("iiftype", &arphrd_type, | ||
| 635 | 2 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 636 | [NFT_META_OIF] = META_TEMPLATE("oif", &ifindex_type, | ||
| 637 | 4 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 638 | [NFT_META_OIFNAME] = META_TEMPLATE("oifname", &ifname_type, | ||
| 639 | IFNAMSIZ * BITS_PER_BYTE, | ||
| 640 | BYTEORDER_HOST_ENDIAN), | ||
| 641 | [NFT_META_OIFTYPE] = META_TEMPLATE("oiftype", &arphrd_type, | ||
| 642 | 2 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 643 | [NFT_META_SKUID] = META_TEMPLATE("skuid", &uid_type, | ||
| 644 | 4 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 645 | [NFT_META_SKGID] = META_TEMPLATE("skgid", &gid_type, | ||
| 646 | 4 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 647 | [NFT_META_NFTRACE] = META_TEMPLATE("nftrace", &integer_type, | ||
| 648 | 1 , BYTEORDER_HOST_ENDIAN), | ||
| 649 | [NFT_META_RTCLASSID] = META_TEMPLATE("rtclassid", &realm_type, | ||
| 650 | 4 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 651 | [NFT_META_BRI_IIFNAME] = META_TEMPLATE("ibrname", &ifname_type, | ||
| 652 | IFNAMSIZ * BITS_PER_BYTE, | ||
| 653 | BYTEORDER_HOST_ENDIAN), | ||
| 654 | [NFT_META_BRI_OIFNAME] = META_TEMPLATE("obrname", &ifname_type, | ||
| 655 | IFNAMSIZ * BITS_PER_BYTE, | ||
| 656 | BYTEORDER_HOST_ENDIAN), | ||
| 657 | [NFT_META_PKTTYPE] = META_TEMPLATE("pkttype", &pkttype_type, | ||
| 658 | BITS_PER_BYTE, | ||
| 659 | BYTEORDER_HOST_ENDIAN), | ||
| 660 | [NFT_META_CPU] = META_TEMPLATE("cpu", &integer_type, | ||
| 661 | 4 * BITS_PER_BYTE, | ||
| 662 | BYTEORDER_HOST_ENDIAN), | ||
| 663 | [NFT_META_IIFGROUP] = META_TEMPLATE("iifgroup", &devgroup_type, | ||
| 664 | 4 * BITS_PER_BYTE, | ||
| 665 | BYTEORDER_HOST_ENDIAN), | ||
| 666 | [NFT_META_OIFGROUP] = META_TEMPLATE("oifgroup", &devgroup_type, | ||
| 667 | 4 * BITS_PER_BYTE, | ||
| 668 | BYTEORDER_HOST_ENDIAN), | ||
| 669 | [NFT_META_CGROUP] = META_TEMPLATE("cgroup", &integer_type, | ||
| 670 | 4 * BITS_PER_BYTE, | ||
| 671 | BYTEORDER_HOST_ENDIAN), | ||
| 672 | [NFT_META_PRANDOM] = META_TEMPLATE("random", &integer_type, | ||
| 673 | 4 * BITS_PER_BYTE, | ||
| 674 | BYTEORDER_BIG_ENDIAN), /* avoid conversion; doesn't have endianess */ | ||
| 675 | [NFT_META_SECPATH] = META_TEMPLATE("ipsec", &boolean_type, | ||
| 676 | BITS_PER_BYTE, BYTEORDER_HOST_ENDIAN), | ||
| 677 | [NFT_META_IIFKIND] = META_TEMPLATE("iifkind", &ifname_type, | ||
| 678 | IFNAMSIZ * BITS_PER_BYTE, | ||
| 679 | BYTEORDER_HOST_ENDIAN), | ||
| 680 | [NFT_META_OIFKIND] = META_TEMPLATE("oifkind", &ifname_type, | ||
| 681 | IFNAMSIZ * BITS_PER_BYTE, | ||
| 682 | BYTEORDER_HOST_ENDIAN), | ||
| 683 | [NFT_META_BRI_IIFPVID] = META_TEMPLATE("ibrpvid", &integer_type, | ||
| 684 | 2 * BITS_PER_BYTE, | ||
| 685 | BYTEORDER_HOST_ENDIAN), | ||
| 686 | [NFT_META_BRI_IIFVPROTO] = META_TEMPLATE("ibrvproto", ðertype_type, | ||
| 687 | 2 * BITS_PER_BYTE, | ||
| 688 | BYTEORDER_BIG_ENDIAN), | ||
| 689 | [NFT_META_TIME_NS] = META_TEMPLATE("time", &date_type, | ||
| 690 | 8 * BITS_PER_BYTE, | ||
| 691 | BYTEORDER_HOST_ENDIAN), | ||
| 692 | [NFT_META_TIME_DAY] = META_TEMPLATE("day", &day_type, | ||
| 693 | 1 * BITS_PER_BYTE, | ||
| 694 | BYTEORDER_HOST_ENDIAN), | ||
| 695 | [NFT_META_TIME_HOUR] = META_TEMPLATE("hour", &hour_type, | ||
| 696 | 4 * BITS_PER_BYTE, | ||
| 697 | BYTEORDER_HOST_ENDIAN), | ||
| 698 | [NFT_META_SECMARK] = META_TEMPLATE("secmark", &integer_type, | ||
| 699 | 32, BYTEORDER_HOST_ENDIAN), | ||
| 700 | [NFT_META_SDIF] = META_TEMPLATE("sdif", &ifindex_type, | ||
| 701 | sizeof(int) * BITS_PER_BYTE, | ||
| 702 | BYTEORDER_HOST_ENDIAN), | ||
| 703 | [NFT_META_SDIFNAME] = META_TEMPLATE("sdifname", &ifname_type, | ||
| 704 | IFNAMSIZ * BITS_PER_BYTE, | ||
| 705 | BYTEORDER_HOST_ENDIAN), | ||
| 706 | [NFT_META_BRI_BROUTE] = META_TEMPLATE("broute", &integer_type, | ||
| 707 | 1 , BYTEORDER_HOST_ENDIAN), | ||
| 708 | [NFT_META_BRI_IIFHWADDR] = META_TEMPLATE("ibrhwaddr", ðeraddr_type, | ||
| 709 | ETH_ALEN * BITS_PER_BYTE, | ||
| 710 | BYTEORDER_BIG_ENDIAN), | ||
| 711 | }; | ||
| 712 | |||
| 713 | 22090 | static bool meta_key_is_unqualified(enum nft_meta_keys key) | |
| 714 | { | ||
| 715 |
2/2✓ Branch 0 (2→3) taken 20612 times.
✓ Branch 1 (2→4) taken 1478 times.
|
22090 | switch (key) { |
| 716 | 20612 | case NFT_META_IIF: | |
| 717 | case NFT_META_OIF: | ||
| 718 | case NFT_META_IIFNAME: | ||
| 719 | case NFT_META_OIFNAME: | ||
| 720 | case NFT_META_IIFGROUP: | ||
| 721 | case NFT_META_OIFGROUP: | ||
| 722 | 20612 | return true; | |
| 723 | 1478 | default: | |
| 724 | 1478 | return false; | |
| 725 | } | ||
| 726 | } | ||
| 727 | |||
| 728 | 21921 | static void meta_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 729 | { | ||
| 730 | 21921 | const char *token = "unknown"; | |
| 731 | 21921 | uint32_t key = expr->meta.key; | |
| 732 | |||
| 733 |
1/2✓ Branch 0 (2→3) taken 21921 times.
✗ Branch 1 (2→4) not taken.
|
21921 | if (key < array_size(meta_templates)) |
| 734 | 21921 | token = meta_templates[key].token; | |
| 735 | |||
| 736 |
2/2✓ Branch 0 (5→6) taken 20612 times.
✓ Branch 1 (5→7) taken 1309 times.
|
21921 | if (meta_key_is_unqualified(key)) |
| 737 | 20612 | nft_print(octx, "%s", token); | |
| 738 | else | ||
| 739 | 1309 | nft_print(octx, "meta %s", token); | |
| 740 | 21921 | } | |
| 741 | |||
| 742 | ✗ | static bool meta_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 743 | { | ||
| 744 | ✗ | return e1->meta.key == e2->meta.key; | |
| 745 | } | ||
| 746 | |||
| 747 | 52241 | static void meta_expr_clone(struct expr *new, const struct expr *expr) | |
| 748 | { | ||
| 749 | 52241 | new->meta.key = expr->meta.key; | |
| 750 | 52241 | new->meta.base = expr->meta.base; | |
| 751 | 52241 | new->meta.inner_desc = expr->meta.inner_desc; | |
| 752 | 52241 | } | |
| 753 | |||
| 754 | /** | ||
| 755 | * meta_expr_pctx_update - update protocol context based on meta match | ||
| 756 | * | ||
| 757 | * @ctx: protocol context | ||
| 758 | * @expr: relational meta expression | ||
| 759 | * | ||
| 760 | * Update LL protocol context based on IIFTYPE meta match in non-LL hooks. | ||
| 761 | */ | ||
| 762 | 27362 | static void meta_expr_pctx_update(struct proto_ctx *ctx, | |
| 763 | const struct location *loc, | ||
| 764 | const struct expr *left, | ||
| 765 | const struct expr *right) | ||
| 766 | { | ||
| 767 | 27362 | const struct hook_proto_desc *h = &hook_proto_desc[ctx->family]; | |
| 768 | const struct proto_desc *desc; | ||
| 769 | uint8_t protonum; | ||
| 770 | |||
| 771 |
4/5✓ Branch 0 (2→3) taken 3449 times.
✓ Branch 1 (2→13) taken 4760 times.
✓ Branch 2 (2→26) taken 14088 times.
✓ Branch 3 (2→32) taken 5065 times.
✗ Branch 4 (2→43) not taken.
|
27362 | switch (left->meta.key) { |
| 772 | 3449 | case NFT_META_IIFTYPE: | |
| 773 |
2/2✓ Branch 0 (3→4) taken 3243 times.
✓ Branch 1 (3→7) taken 206 times.
|
3449 | if (h->base < PROTO_BASE_NETWORK_HDR && |
| 774 |
2/2✓ Branch 0 (4→5) taken 3020 times.
✓ Branch 1 (4→7) taken 223 times.
|
3243 | ctx->family != NFPROTO_INET && |
| 775 |
2/2✓ Branch 0 (5→6) taken 30 times.
✓ Branch 1 (5→7) taken 2990 times.
|
3020 | ctx->family != NFPROTO_NETDEV) |
| 776 | 30 | return; | |
| 777 | |||
| 778 | 3419 | desc = proto_dev_desc(mpz_get_uint16(right->value)); | |
| 779 |
2/2✓ Branch 0 (9→10) taken 96 times.
✓ Branch 1 (9→11) taken 3323 times.
|
3419 | if (desc == NULL) |
| 780 | 96 | desc = &proto_unknown; | |
| 781 | |||
| 782 | 3419 | proto_ctx_update(ctx, PROTO_BASE_LL_HDR, loc, desc); | |
| 783 | 3419 | break; | |
| 784 | 4760 | case NFT_META_NFPROTO: | |
| 785 | 4760 | protonum = mpz_get_uint8(right->value); | |
| 786 |
3/4✓ Branch 0 (14→15) taken 2252 times.
✓ Branch 1 (14→16) taken 2508 times.
✓ Branch 2 (15→16) taken 2252 times.
✗ Branch 3 (15→44) not taken.
|
4760 | if (protonum == NFPROTO_IPV4 && h->desc == &proto_ip) |
| 787 | break; | ||
| 788 |
3/4✓ Branch 0 (16→17) taken 2508 times.
✓ Branch 1 (16→19) taken 2252 times.
✗ Branch 2 (17→18) not taken.
✓ Branch 3 (17→19) taken 2508 times.
|
4760 | else if (protonum == NFPROTO_IPV6 && h->desc == &proto_ip6) |
| 789 | ✗ | break; | |
| 790 | |||
| 791 | 4760 | desc = proto_find_upper(h->desc, protonum); | |
| 792 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→24) taken 4760 times.
|
4760 | if (desc == NULL) { |
| 793 | ✗ | desc = &proto_unknown; | |
| 794 | |||
| 795 | ✗ | if (protonum == ctx->family && | |
| 796 | ✗ | h->base == PROTO_BASE_NETWORK_HDR) | |
| 797 | ✗ | desc = h->desc; | |
| 798 | } | ||
| 799 | |||
| 800 | 4760 | proto_ctx_update(ctx, PROTO_BASE_NETWORK_HDR, loc, desc); | |
| 801 | 4760 | break; | |
| 802 | 14088 | case NFT_META_L4PROTO: | |
| 803 | 14088 | desc = proto_find_upper(&proto_inet_service, | |
| 804 | 14088 | mpz_get_uint8(right->value)); | |
| 805 |
2/2✓ Branch 0 (28→29) taken 98 times.
✓ Branch 1 (28→30) taken 13990 times.
|
14088 | if (desc == NULL) |
| 806 | 98 | desc = &proto_unknown; | |
| 807 | |||
| 808 | 14088 | proto_ctx_update(ctx, PROTO_BASE_TRANSPORT_HDR, loc, desc); | |
| 809 | 14088 | break; | |
| 810 | 5065 | case NFT_META_PROTOCOL: | |
| 811 |
2/2✓ Branch 0 (32→33) taken 69 times.
✓ Branch 1 (32→34) taken 4996 times.
|
5065 | if (h->base != PROTO_BASE_LL_HDR) |
| 812 | 69 | return; | |
| 813 | |||
| 814 |
2/2✓ Branch 0 (34→35) taken 1555 times.
✓ Branch 1 (34→37) taken 3441 times.
|
4996 | if (ctx->family != NFPROTO_NETDEV && |
| 815 |
2/2✓ Branch 0 (35→36) taken 36 times.
✓ Branch 1 (35→37) taken 1519 times.
|
1555 | ctx->family != NFPROTO_BRIDGE) |
| 816 | 36 | return; | |
| 817 | |||
| 818 | 4960 | desc = proto_find_upper(h->desc, ntohs(mpz_get_uint16(right->value))); | |
| 819 |
1/2✗ Branch 0 (39→40) not taken.
✓ Branch 1 (39→41) taken 4960 times.
|
4960 | if (desc == NULL) |
| 820 | ✗ | desc = &proto_unknown; | |
| 821 | |||
| 822 | 4960 | proto_ctx_update(ctx, PROTO_BASE_NETWORK_HDR, loc, desc); | |
| 823 | 4960 | break; | |
| 824 | ✗ | default: | |
| 825 | ✗ | break; | |
| 826 | } | ||
| 827 | } | ||
| 828 | |||
| 829 | #define NFTNL_UDATA_META_KEY 0 | ||
| 830 | #define NFTNL_UDATA_META_INNER_DESC 1 | ||
| 831 | #define NFTNL_UDATA_META_MAX 2 | ||
| 832 | |||
| 833 | 1250 | static int meta_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 834 | const struct expr *expr) | ||
| 835 | { | ||
| 836 | 1250 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_META_KEY, expr->meta.key); | |
| 837 | |||
| 838 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 1250 times.
|
1250 | if (expr->meta.inner_desc) { |
| 839 | ✗ | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_META_INNER_DESC, | |
| 840 | ✗ | expr->meta.inner_desc->id); | |
| 841 | } | ||
| 842 | |||
| 843 | 1250 | return 0; | |
| 844 | } | ||
| 845 | |||
| 846 | 6395 | static int meta_parse_udata(const struct nftnl_udata *attr, void *data) | |
| 847 | { | ||
| 848 | 6395 | const struct nftnl_udata **ud = data; | |
| 849 | 6395 | uint8_t type = nftnl_udata_type(attr); | |
| 850 | 6395 | uint8_t len = nftnl_udata_len(attr); | |
| 851 | |||
| 852 |
1/2✓ Branch 0 (4→5) taken 6395 times.
✗ Branch 1 (4→8) not taken.
|
6395 | switch (type) { |
| 853 | 6395 | case NFTNL_UDATA_META_KEY: | |
| 854 | case NFTNL_UDATA_META_INNER_DESC: | ||
| 855 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 6395 times.
|
6395 | if (len != sizeof(uint32_t)) |
| 856 | ✗ | return -1; | |
| 857 | 6395 | break; | |
| 858 | ✗ | default: | |
| 859 | ✗ | return 0; | |
| 860 | } | ||
| 861 | |||
| 862 | 6395 | ud[type] = attr; | |
| 863 | 6395 | return 0; | |
| 864 | } | ||
| 865 | |||
| 866 | 6395 | static struct expr *meta_expr_parse_udata(const struct nftnl_udata *attr) | |
| 867 | { | ||
| 868 | 6395 | const struct nftnl_udata *ud[NFTNL_UDATA_META_MAX + 1] = {}; | |
| 869 | const struct proto_desc *desc; | ||
| 870 | struct expr *expr; | ||
| 871 | uint32_t key; | ||
| 872 | int err; | ||
| 873 | |||
| 874 | 6395 | err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr), | |
| 875 | meta_parse_udata, ud); | ||
| 876 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 6395 times.
|
6395 | if (err < 0) |
| 877 | ✗ | return NULL; | |
| 878 | |||
| 879 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 6395 times.
|
6395 | if (!ud[NFTNL_UDATA_META_KEY]) |
| 880 | ✗ | return NULL; | |
| 881 | |||
| 882 | 6395 | key = nftnl_udata_get_u32(ud[NFTNL_UDATA_META_KEY]); | |
| 883 | |||
| 884 | 6395 | expr = meta_expr_alloc(&internal_location, key); | |
| 885 | |||
| 886 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→14) taken 6395 times.
|
6395 | if (ud[NFTNL_UDATA_META_INNER_DESC]) { |
| 887 | ✗ | desc = find_proto_desc(ud[NFTNL_UDATA_META_INNER_DESC]); | |
| 888 | ✗ | expr->meta.inner_desc = desc; | |
| 889 | } | ||
| 890 | |||
| 891 | 6395 | return expr; | |
| 892 | } | ||
| 893 | |||
| 894 | const struct expr_ops meta_expr_ops = { | ||
| 895 | .type = EXPR_META, | ||
| 896 | .name = "meta", | ||
| 897 | .print = meta_expr_print, | ||
| 898 | .json = meta_expr_json, | ||
| 899 | .cmp = meta_expr_cmp, | ||
| 900 | .clone = meta_expr_clone, | ||
| 901 | .pctx_update = meta_expr_pctx_update, | ||
| 902 | .build_udata = meta_expr_build_udata, | ||
| 903 | .parse_udata = meta_expr_parse_udata, | ||
| 904 | }; | ||
| 905 | |||
| 906 | 153705 | struct expr *meta_expr_alloc(const struct location *loc, enum nft_meta_keys key) | |
| 907 | { | ||
| 908 | 153705 | const struct meta_template *tmpl = &meta_templates[key]; | |
| 909 | struct expr *expr; | ||
| 910 | |||
| 911 | 153705 | expr = expr_alloc(loc, EXPR_META, tmpl->dtype, | |
| 912 | 153705 | tmpl->byteorder, tmpl->len); | |
| 913 | 153705 | expr->meta.key = key; | |
| 914 | |||
| 915 |
5/5✓ Branch 0 (3→4) taken 3631 times.
✓ Branch 1 (3→5) taken 3073 times.
✓ Branch 2 (3→6) taken 10308 times.
✓ Branch 3 (3→7) taken 3588 times.
✓ Branch 4 (3→8) taken 133105 times.
|
153705 | switch (key) { |
| 916 | 3631 | case NFT_META_IIFTYPE: | |
| 917 | 3631 | expr->flags |= EXPR_F_PROTOCOL; | |
| 918 | 3631 | break; | |
| 919 | 3073 | case NFT_META_NFPROTO: | |
| 920 | 3073 | expr->flags |= EXPR_F_PROTOCOL; | |
| 921 | 3073 | expr->meta.base = PROTO_BASE_LL_HDR; | |
| 922 | 3073 | break; | |
| 923 | 10308 | case NFT_META_L4PROTO: | |
| 924 | 10308 | expr->flags |= EXPR_F_PROTOCOL; | |
| 925 | 10308 | expr->meta.base = PROTO_BASE_NETWORK_HDR; | |
| 926 | 10308 | break; | |
| 927 | 3588 | case NFT_META_PROTOCOL: | |
| 928 | 3588 | expr->flags |= EXPR_F_PROTOCOL; | |
| 929 | 3588 | expr->meta.base = PROTO_BASE_LL_HDR; | |
| 930 | 3588 | break; | |
| 931 | 133105 | default: | |
| 932 | 133105 | break; | |
| 933 | } | ||
| 934 | |||
| 935 | 153705 | return expr; | |
| 936 | } | ||
| 937 | |||
| 938 | 169 | static void meta_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 939 | { | ||
| 940 | 169 | const char *token = "unknown"; | |
| 941 | 169 | uint32_t key = stmt->meta.key; | |
| 942 | |||
| 943 |
1/2✓ Branch 0 (2→3) taken 169 times.
✗ Branch 1 (2→4) not taken.
|
169 | if (key < array_size(meta_templates)) |
| 944 | 169 | token = meta_templates[key].token; | |
| 945 | |||
| 946 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 169 times.
|
169 | if (meta_key_is_unqualified(stmt->meta.key)) |
| 947 | ✗ | nft_print(octx, "%s set ", token); | |
| 948 | else | ||
| 949 | 169 | nft_print(octx, "meta %s set ", token); | |
| 950 | |||
| 951 | 169 | expr_print(stmt->meta.expr, octx); | |
| 952 | 169 | } | |
| 953 | |||
| 954 | 841 | static void meta_stmt_destroy(struct stmt *stmt) | |
| 955 | { | ||
| 956 | 841 | expr_free(stmt->meta.expr); | |
| 957 | 841 | } | |
| 958 | |||
| 959 | const struct stmt_ops meta_stmt_ops = { | ||
| 960 | .type = STMT_META, | ||
| 961 | .name = "meta", | ||
| 962 | .print = meta_stmt_print, | ||
| 963 | .json = meta_stmt_json, | ||
| 964 | .destroy = meta_stmt_destroy, | ||
| 965 | }; | ||
| 966 | |||
| 967 | 841 | struct stmt *meta_stmt_alloc(const struct location *loc, enum nft_meta_keys key, | |
| 968 | struct expr *expr) | ||
| 969 | { | ||
| 970 | struct stmt *stmt; | ||
| 971 | |||
| 972 | 841 | stmt = stmt_alloc(loc, &meta_stmt_ops); | |
| 973 | 841 | stmt->meta.key = key; | |
| 974 | 841 | stmt->meta.expr = expr; | |
| 975 | |||
| 976 |
1/2✓ Branch 0 (3→4) taken 841 times.
✗ Branch 1 (3→5) not taken.
|
841 | if (key < array_size(meta_templates)) |
| 977 | 841 | stmt->meta.tmpl = &meta_templates[key]; | |
| 978 | |||
| 979 | 841 | return stmt; | |
| 980 | } | ||
| 981 | |||
| 982 | /* | ||
| 983 | * @expr: payload expression | ||
| 984 | * @res: dependency expression | ||
| 985 | * | ||
| 986 | * Generate a NFT_META_IIFTYPE expression to check for ethernet frames. | ||
| 987 | * Only works on input path. | ||
| 988 | */ | ||
| 989 | 2962 | struct stmt *meta_stmt_meta_iiftype(const struct location *loc, uint16_t type) | |
| 990 | { | ||
| 991 | struct expr *dep, *left, *right; | ||
| 992 | |||
| 993 | 2962 | left = meta_expr_alloc(loc, NFT_META_IIFTYPE); | |
| 994 | 2962 | right = constant_expr_alloc(loc, &arphrd_type, | |
| 995 | BYTEORDER_HOST_ENDIAN, | ||
| 996 | 2 * BITS_PER_BYTE, &type); | ||
| 997 | |||
| 998 | 2962 | dep = relational_expr_alloc(loc, OP_EQ, left, right); | |
| 999 | 2962 | return expr_stmt_alloc(&dep->location, dep); | |
| 1000 | } | ||
| 1001 | |||
| 1002 | 42580 | struct error_record *meta_key_parse(const struct location *loc, | |
| 1003 | const char *str, | ||
| 1004 | unsigned int *value) | ||
| 1005 | { | ||
| 1006 | 42580 | const char *sep = ""; | |
| 1007 | 42580 | size_t offset = 0; | |
| 1008 | unsigned int i; | ||
| 1009 | char buf[1024]; | ||
| 1010 | size_t len; | ||
| 1011 | |||
| 1012 |
2/2✓ Branch 0 (8→3) taken 313544 times.
✓ Branch 1 (8→9) taken 3 times.
|
313547 | for (i = 0; i < array_size(meta_templates); i++) { |
| 1013 |
4/4✓ Branch 0 (3→4) taken 313536 times.
✓ Branch 1 (3→5) taken 8 times.
✓ Branch 2 (4→5) taken 270959 times.
✓ Branch 3 (4→6) taken 42577 times.
|
313544 | if (!meta_templates[i].token || strcmp(meta_templates[i].token, str)) |
| 1014 | 270967 | continue; | |
| 1015 | |||
| 1016 | 42577 | *value = i; | |
| 1017 | 42577 | return NULL; | |
| 1018 | } | ||
| 1019 | |||
| 1020 | /* Backwards compat hack */ | ||
| 1021 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 3 times.
|
3 | if (strcmp(str, "ibriport") == 0) { |
| 1022 | ✗ | *value = NFT_META_BRI_IIFNAME; | |
| 1023 | ✗ | return NULL; | |
| 1024 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 3 times.
|
3 | } else if (strcmp(str, "obriport") == 0) { |
| 1025 | ✗ | *value = NFT_META_BRI_OIFNAME; | |
| 1026 | ✗ | return NULL; | |
| 1027 |
2/2✓ Branch 0 (13→14) taken 2 times.
✓ Branch 1 (13→15) taken 1 times.
|
3 | } else if (strcmp(str, "secpath") == 0) { |
| 1028 | 2 | *value = NFT_META_SECPATH; | |
| 1029 | 2 | return NULL; | |
| 1030 | } | ||
| 1031 | |||
| 1032 | 1 | len = (int)sizeof(buf); | |
| 1033 | |||
| 1034 |
2/2✓ Branch 0 (28→16) taken 38 times.
✓ Branch 1 (28→29) taken 1 times.
|
39 | for (i = 0; i < array_size(meta_templates); i++) { |
| 1035 | int ret; | ||
| 1036 | |||
| 1037 |
2/2✓ Branch 0 (16→17) taken 1 times.
✓ Branch 1 (16→18) taken 37 times.
|
38 | if (!meta_templates[i].token) |
| 1038 | 1 | continue; | |
| 1039 | |||
| 1040 |
2/2✓ Branch 0 (18→19) taken 36 times.
✓ Branch 1 (18→20) taken 1 times.
|
37 | if (offset) |
| 1041 | 36 | sep = ", "; | |
| 1042 | |||
| 1043 | 37 | ret = snprintf(buf+offset, len, "%s%s", sep, meta_templates[i].token); | |
| 1044 |
2/4✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 37 times.
✗ Branch 2 (22→23) not taken.
✓ Branch 3 (22→24) taken 37 times.
|
37 | SNPRINTF_BUFFER_SIZE(ret, &len, &offset); |
| 1045 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 37 times.
|
37 | assert(len > 0); |
| 1046 | } | ||
| 1047 | |||
| 1048 | 1 | return error(loc, "syntax error, unexpected %s, known keys are %s", str, buf); | |
| 1049 | } | ||
| 1050 |