| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Conntrack expression related definitions 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 <stddef.h> | ||
| 16 | #include <stdio.h> | ||
| 17 | #include <inttypes.h> | ||
| 18 | |||
| 19 | #include <netinet/ip.h> | ||
| 20 | #include <linux/netfilter.h> | ||
| 21 | #include <linux/netfilter/nf_tables.h> | ||
| 22 | #include <linux/netfilter/nf_conntrack_common.h> | ||
| 23 | #include <linux/netfilter/nf_conntrack_tuple_common.h> | ||
| 24 | |||
| 25 | #include <errno.h> | ||
| 26 | #include <erec.h> | ||
| 27 | #include <expression.h> | ||
| 28 | #include <datatype.h> | ||
| 29 | #include <ct.h> | ||
| 30 | #include <gmputil.h> | ||
| 31 | #include <utils.h> | ||
| 32 | #include <statement.h> | ||
| 33 | |||
| 34 | #define CONNLABEL_CONF DEFAULT_INCLUDE_PATH "/connlabel.conf" | ||
| 35 | |||
| 36 | static const struct symbol_table ct_state_tbl = { | ||
| 37 | .base = BASE_HEXADECIMAL, | ||
| 38 | .symbols = { | ||
| 39 | SYMBOL("invalid", NF_CT_STATE_INVALID_BIT), | ||
| 40 | SYMBOL("new", NF_CT_STATE_BIT(IP_CT_NEW)), | ||
| 41 | SYMBOL("established", NF_CT_STATE_BIT(IP_CT_ESTABLISHED)), | ||
| 42 | SYMBOL("related", NF_CT_STATE_BIT(IP_CT_RELATED)), | ||
| 43 | SYMBOL("untracked", NF_CT_STATE_UNTRACKED_BIT), | ||
| 44 | SYMBOL_LIST_END | ||
| 45 | } | ||
| 46 | }; | ||
| 47 | |||
| 48 | const struct datatype ct_state_type = { | ||
| 49 | .type = TYPE_CT_STATE, | ||
| 50 | .name = "ct_state", | ||
| 51 | .desc = "conntrack state", | ||
| 52 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 53 | .size = 4 * BITS_PER_BYTE, | ||
| 54 | .basetype = &bitmask_type, | ||
| 55 | .sym_tbl = &ct_state_tbl, | ||
| 56 | }; | ||
| 57 | |||
| 58 | static const struct symbol_table ct_dir_tbl = { | ||
| 59 | .base = BASE_DECIMAL, | ||
| 60 | .symbols = { | ||
| 61 | SYMBOL("original", IP_CT_DIR_ORIGINAL), | ||
| 62 | SYMBOL("reply", IP_CT_DIR_REPLY), | ||
| 63 | SYMBOL_LIST_END | ||
| 64 | } | ||
| 65 | }; | ||
| 66 | |||
| 67 | 925 | const char *ct_dir2str(int dir) | |
| 68 | { | ||
| 69 | const struct symbolic_constant *s; | ||
| 70 | |||
| 71 |
2/2✓ Branch 0 (6→3) taken 1798 times.
✓ Branch 1 (6→7) taken 845 times.
|
2643 | for (s = ct_dir_tbl.symbols; s->identifier != NULL; s++) { |
| 72 |
2/2✓ Branch 0 (3→4) taken 80 times.
✓ Branch 1 (3→5) taken 1718 times.
|
1798 | if (dir == (int)s->value) |
| 73 | 80 | return s->identifier; | |
| 74 | } | ||
| 75 | |||
| 76 | 845 | return NULL; | |
| 77 | } | ||
| 78 | |||
| 79 | const struct datatype ct_dir_type = { | ||
| 80 | .type = TYPE_CT_DIR, | ||
| 81 | .name = "ct_dir", | ||
| 82 | .desc = "conntrack direction", | ||
| 83 | .byteorder = BYTEORDER_INVALID, | ||
| 84 | .size = BITS_PER_BYTE, | ||
| 85 | .basetype = &integer_type, | ||
| 86 | .sym_tbl = &ct_dir_tbl, | ||
| 87 | }; | ||
| 88 | |||
| 89 | static const struct symbol_table ct_status_tbl = { | ||
| 90 | /* | ||
| 91 | * There are more, but most of them don't make sense for filtering. | ||
| 92 | */ | ||
| 93 | .base = BASE_HEXADECIMAL, | ||
| 94 | .symbols = { | ||
| 95 | SYMBOL("expected", IPS_EXPECTED), | ||
| 96 | SYMBOL("seen-reply", IPS_SEEN_REPLY), | ||
| 97 | SYMBOL("assured", IPS_ASSURED), | ||
| 98 | SYMBOL("confirmed", IPS_CONFIRMED), | ||
| 99 | SYMBOL("snat", IPS_SRC_NAT), | ||
| 100 | SYMBOL("dnat", IPS_DST_NAT), | ||
| 101 | SYMBOL("seq-adjust", IPS_SEQ_ADJUST), | ||
| 102 | SYMBOL("snat-done", IPS_SRC_NAT_DONE), | ||
| 103 | SYMBOL("dnat-done", IPS_DST_NAT_DONE), | ||
| 104 | SYMBOL("dying", IPS_DYING), | ||
| 105 | SYMBOL("fixed-timeout", IPS_FIXED_TIMEOUT), | ||
| 106 | SYMBOL("helper", IPS_HELPER_BIT), | ||
| 107 | SYMBOL("offload", IPS_OFFLOAD_BIT), | ||
| 108 | SYMBOL("hw-offload", IPS_HW_OFFLOAD_BIT), | ||
| 109 | SYMBOL_LIST_END | ||
| 110 | }, | ||
| 111 | }; | ||
| 112 | |||
| 113 | const struct datatype ct_status_type = { | ||
| 114 | .type = TYPE_CT_STATUS, | ||
| 115 | .name = "ct_status", | ||
| 116 | .desc = "conntrack status", | ||
| 117 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 118 | .size = 4 * BITS_PER_BYTE, | ||
| 119 | .basetype = &bitmask_type, | ||
| 120 | .sym_tbl = &ct_status_tbl, | ||
| 121 | }; | ||
| 122 | |||
| 123 | static const struct symbol_table ct_events_tbl = { | ||
| 124 | .base = BASE_HEXADECIMAL, | ||
| 125 | .symbols = { | ||
| 126 | SYMBOL("new", 1 << IPCT_NEW), | ||
| 127 | SYMBOL("related", 1 << IPCT_RELATED), | ||
| 128 | SYMBOL("destroy", 1 << IPCT_DESTROY), | ||
| 129 | SYMBOL("reply", 1 << IPCT_REPLY), | ||
| 130 | SYMBOL("assured", 1 << IPCT_ASSURED), | ||
| 131 | SYMBOL("protoinfo", 1 << IPCT_PROTOINFO), | ||
| 132 | SYMBOL("helper", 1 << IPCT_HELPER), | ||
| 133 | SYMBOL("mark", 1 << IPCT_MARK), | ||
| 134 | SYMBOL("seqadj", 1 << IPCT_SEQADJ), | ||
| 135 | SYMBOL("secmark", 1 << IPCT_SECMARK), | ||
| 136 | SYMBOL("label", 1 << IPCT_LABEL), | ||
| 137 | SYMBOL_LIST_END | ||
| 138 | }, | ||
| 139 | }; | ||
| 140 | |||
| 141 | const struct datatype ct_event_type = { | ||
| 142 | .type = TYPE_CT_EVENTBIT, | ||
| 143 | .name = "ct_event", | ||
| 144 | .desc = "conntrack event bits", | ||
| 145 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 146 | .size = 4 * BITS_PER_BYTE, | ||
| 147 | .basetype = &bitmask_type, | ||
| 148 | .sym_tbl = &ct_events_tbl, | ||
| 149 | }; | ||
| 150 | |||
| 151 | #define CT_LABEL_BIT_SIZE 128 | ||
| 152 | |||
| 153 | 12 | const char *ct_label2str(const struct symbol_table *ct_label_tbl, | |
| 154 | unsigned long value) | ||
| 155 | { | ||
| 156 | const struct symbolic_constant *s; | ||
| 157 | |||
| 158 |
1/2✗ Branch 0 (6→3) not taken.
✓ Branch 1 (6→7) taken 12 times.
|
12 | for (s = ct_label_tbl->symbols; s->identifier; s++) { |
| 159 | ✗ | if (value == s->value) | |
| 160 | ✗ | return s->identifier; | |
| 161 | } | ||
| 162 | |||
| 163 | 12 | return NULL; | |
| 164 | } | ||
| 165 | |||
| 166 | 6 | static void ct_label_type_print(const struct expr *expr, | |
| 167 | struct output_ctx *octx) | ||
| 168 | { | ||
| 169 | 6 | unsigned long bit = mpz_scan1(expr->value, 0); | |
| 170 | 6 | const char *labelstr = ct_label2str(octx->tbl.ct_label, bit); | |
| 171 | |||
| 172 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 6 times.
|
6 | if (labelstr) { |
| 173 | ✗ | nft_print(octx, "\"%s\"", labelstr); | |
| 174 | ✗ | return; | |
| 175 | } | ||
| 176 | /* can happen when connlabel.conf is altered after rules were added */ | ||
| 177 | 6 | nft_print(octx, "%lu", bit); | |
| 178 | } | ||
| 179 | |||
| 180 | 15 | static struct error_record *ct_label_type_parse(struct parse_ctx *ctx, | |
| 181 | const struct expr *sym, | ||
| 182 | struct expr **res) | ||
| 183 | { | ||
| 184 | const struct symbolic_constant *s; | ||
| 185 | const struct datatype *dtype; | ||
| 186 | uint8_t data[CT_LABEL_BIT_SIZE / BITS_PER_BYTE]; | ||
| 187 | uint64_t bit; | ||
| 188 | mpz_t value; | ||
| 189 | |||
| 190 |
1/2✗ Branch 0 (6→3) not taken.
✓ Branch 1 (6→7) taken 15 times.
|
15 | for (s = ctx->tbl->ct_label->symbols; s->identifier != NULL; s++) { |
| 191 | ✗ | if (!strcmp(sym->identifier, s->identifier)) | |
| 192 | ✗ | break; | |
| 193 | } | ||
| 194 | |||
| 195 | 15 | dtype = sym->dtype; | |
| 196 |
1/2✓ Branch 0 (7→8) taken 15 times.
✗ Branch 1 (7→18) not taken.
|
15 | if (s->identifier == NULL) { |
| 197 | char *ptr; | ||
| 198 | |||
| 199 | 15 | errno = 0; | |
| 200 | 15 | bit = strtoull(sym->identifier, &ptr, 0); | |
| 201 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 15 times.
|
15 | if (*ptr) |
| 202 | ✗ | return error(&sym->location, "%s: could not parse %s \"%s\"", | |
| 203 | CONNLABEL_CONF, dtype->desc, sym->identifier); | ||
| 204 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→16) taken 15 times.
|
15 | if (errno) |
| 205 | ✗ | return error(&sym->location, "%s: could not parse %s \"%s\": %s", | |
| 206 | CONNLABEL_CONF, dtype->desc, sym->identifier, strerror(errno)); | ||
| 207 | |||
| 208 | } else { | ||
| 209 | ✗ | bit = s->value; | |
| 210 | } | ||
| 211 | |||
| 212 |
2/2✓ Branch 0 (19→20) taken 3 times.
✓ Branch 1 (19→22) taken 12 times.
|
15 | if (bit >= CT_LABEL_BIT_SIZE) |
| 213 | 3 | return error(&sym->location, "%s: bit %" PRIu64 " out of range (%u max)", | |
| 214 | sym->identifier, bit, CT_LABEL_BIT_SIZE); | ||
| 215 | |||
| 216 | 12 | mpz_init2(value, dtype->size); | |
| 217 | 12 | mpz_setbit(value, bit); | |
| 218 | 12 | mpz_export_data(data, value, BYTEORDER_HOST_ENDIAN, sizeof(data)); | |
| 219 | |||
| 220 | 24 | *res = constant_expr_alloc(&sym->location, dtype, | |
| 221 | 12 | dtype->byteorder, CT_LABEL_BIT_SIZE, data); | |
| 222 | 12 | mpz_clear(value); | |
| 223 | 12 | return NULL; | |
| 224 | } | ||
| 225 | |||
| 226 | ✗ | static void ct_label_type_describe(struct output_ctx *octx) | |
| 227 | { | ||
| 228 | ✗ | rt_symbol_table_describe(octx, CONNLABEL_CONF, | |
| 229 | octx->tbl.ct_label, &ct_label_type); | ||
| 230 | ✗ | } | |
| 231 | |||
| 232 | const struct datatype ct_label_type = { | ||
| 233 | .type = TYPE_CT_LABEL, | ||
| 234 | .name = "ct_label", | ||
| 235 | .desc = "conntrack label", | ||
| 236 | .describe = ct_label_type_describe, | ||
| 237 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 238 | .size = CT_LABEL_BIT_SIZE, | ||
| 239 | .basetype = &bitmask_type, | ||
| 240 | .print = ct_label_type_print, | ||
| 241 | .json = ct_label_type_json, | ||
| 242 | .parse = ct_label_type_parse, | ||
| 243 | }; | ||
| 244 | |||
| 245 | 13017 | void ct_label_table_init(struct nft_ctx *ctx) | |
| 246 | { | ||
| 247 | 13017 | ctx->output.tbl.ct_label = rt_symbol_table_init(CONNLABEL_CONF); | |
| 248 | 13017 | } | |
| 249 | |||
| 250 | 13017 | void ct_label_table_exit(struct nft_ctx *ctx) | |
| 251 | { | ||
| 252 | 13017 | rt_symbol_table_free(ctx->output.tbl.ct_label); | |
| 253 | 13017 | } | |
| 254 | |||
| 255 | #ifndef NF_CT_HELPER_NAME_LEN | ||
| 256 | #define NF_CT_HELPER_NAME_LEN 16 | ||
| 257 | #endif | ||
| 258 | |||
| 259 | const struct ct_template ct_templates[__NFT_CT_MAX] = { | ||
| 260 | [NFT_CT_STATE] = CT_TEMPLATE("state", &ct_state_type, | ||
| 261 | BYTEORDER_HOST_ENDIAN, | ||
| 262 | 4 * BITS_PER_BYTE), | ||
| 263 | [NFT_CT_DIRECTION] = CT_TEMPLATE("direction", &ct_dir_type, | ||
| 264 | BYTEORDER_HOST_ENDIAN, | ||
| 265 | BITS_PER_BYTE), | ||
| 266 | [NFT_CT_STATUS] = CT_TEMPLATE("status", &ct_status_type, | ||
| 267 | BYTEORDER_HOST_ENDIAN, | ||
| 268 | 4 * BITS_PER_BYTE), | ||
| 269 | [NFT_CT_MARK] = CT_TEMPLATE("mark", &mark_type, | ||
| 270 | BYTEORDER_HOST_ENDIAN, | ||
| 271 | 4 * BITS_PER_BYTE), | ||
| 272 | [NFT_CT_EXPIRATION] = CT_TEMPLATE("expiration", &time_type, | ||
| 273 | BYTEORDER_HOST_ENDIAN, | ||
| 274 | 4 * BITS_PER_BYTE), | ||
| 275 | [NFT_CT_HELPER] = CT_TEMPLATE("helper", &string_type, | ||
| 276 | BYTEORDER_HOST_ENDIAN, | ||
| 277 | NF_CT_HELPER_NAME_LEN * BITS_PER_BYTE), | ||
| 278 | [NFT_CT_L3PROTOCOL] = CT_TEMPLATE("l3proto", &nfproto_type, | ||
| 279 | BYTEORDER_HOST_ENDIAN, | ||
| 280 | BITS_PER_BYTE), | ||
| 281 | [NFT_CT_SRC] = CT_TEMPLATE("saddr", &invalid_type, | ||
| 282 | BYTEORDER_BIG_ENDIAN, 0), | ||
| 283 | [NFT_CT_DST] = CT_TEMPLATE("daddr", &invalid_type, | ||
| 284 | BYTEORDER_BIG_ENDIAN, 0), | ||
| 285 | [NFT_CT_PROTOCOL] = CT_TEMPLATE("protocol", &inet_protocol_type, | ||
| 286 | BYTEORDER_BIG_ENDIAN, | ||
| 287 | BITS_PER_BYTE), | ||
| 288 | [NFT_CT_PROTO_SRC] = CT_TEMPLATE("proto-src", &inet_service_type, | ||
| 289 | BYTEORDER_BIG_ENDIAN, | ||
| 290 | 2 * BITS_PER_BYTE), | ||
| 291 | [NFT_CT_PROTO_DST] = CT_TEMPLATE("proto-dst", &inet_service_type, | ||
| 292 | BYTEORDER_BIG_ENDIAN, | ||
| 293 | 2 * BITS_PER_BYTE), | ||
| 294 | [NFT_CT_LABELS] = CT_TEMPLATE("label", &ct_label_type, | ||
| 295 | BYTEORDER_HOST_ENDIAN, | ||
| 296 | CT_LABEL_BIT_SIZE), | ||
| 297 | [NFT_CT_BYTES] = CT_TEMPLATE("bytes", &integer_type, | ||
| 298 | BYTEORDER_HOST_ENDIAN, 64), | ||
| 299 | [NFT_CT_PKTS] = CT_TEMPLATE("packets", &integer_type, | ||
| 300 | BYTEORDER_HOST_ENDIAN, 64), | ||
| 301 | [NFT_CT_AVGPKT] = CT_TEMPLATE("avgpkt", &integer_type, | ||
| 302 | BYTEORDER_HOST_ENDIAN, 64), | ||
| 303 | [NFT_CT_ZONE] = CT_TEMPLATE("zone", &integer_type, | ||
| 304 | BYTEORDER_HOST_ENDIAN, 16), | ||
| 305 | [NFT_CT_EVENTMASK] = CT_TEMPLATE("event", &ct_event_type, | ||
| 306 | BYTEORDER_HOST_ENDIAN, 32), | ||
| 307 | [NFT_CT_SRC_IP] = CT_TEMPLATE("ip saddr", &ipaddr_type, | ||
| 308 | BYTEORDER_BIG_ENDIAN, 32), | ||
| 309 | [NFT_CT_DST_IP] = CT_TEMPLATE("ip daddr", &ipaddr_type, | ||
| 310 | BYTEORDER_BIG_ENDIAN, 32), | ||
| 311 | [NFT_CT_SRC_IP6] = CT_TEMPLATE("ip6 saddr", &ip6addr_type, | ||
| 312 | BYTEORDER_BIG_ENDIAN, 128), | ||
| 313 | [NFT_CT_DST_IP6] = CT_TEMPLATE("ip6 daddr", &ip6addr_type, | ||
| 314 | BYTEORDER_BIG_ENDIAN, 128), | ||
| 315 | [NFT_CT_SECMARK] = CT_TEMPLATE("secmark", &integer_type, | ||
| 316 | BYTEORDER_HOST_ENDIAN, 32), | ||
| 317 | [NFT_CT_ID] = CT_TEMPLATE("id", &integer_type, | ||
| 318 | BYTEORDER_BIG_ENDIAN, 32), | ||
| 319 | }; | ||
| 320 | |||
| 321 | 506 | static void ct_print(enum nft_ct_keys key, int8_t dir, uint8_t nfproto, | |
| 322 | struct output_ctx *octx) | ||
| 323 | { | ||
| 324 | 506 | const char *dirstr = ct_dir2str(dir); | |
| 325 | const struct proto_desc *desc; | ||
| 326 | |||
| 327 | 506 | nft_print(octx, "ct "); | |
| 328 |
2/2✓ Branch 0 (4→5) taken 466 times.
✓ Branch 1 (4→6) taken 40 times.
|
506 | if (dir < 0) |
| 329 | 466 | goto done; | |
| 330 | |||
| 331 |
1/2✓ Branch 0 (6→7) taken 40 times.
✗ Branch 1 (6→8) not taken.
|
40 | if (dirstr) |
| 332 | 40 | nft_print(octx, "%s ", dirstr); | |
| 333 | |||
| 334 |
2/2✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→13) taken 39 times.
|
40 | switch (key) { |
| 335 | 1 | case NFT_CT_SRC: | |
| 336 | case NFT_CT_DST: | ||
| 337 | 1 | desc = proto_find_upper(&proto_inet, nfproto); | |
| 338 |
1/2✓ Branch 0 (10→11) taken 1 times.
✗ Branch 1 (10→12) not taken.
|
1 | if (desc) |
| 339 | 1 | nft_print(octx, "%s ", desc->name); | |
| 340 | 1 | break; | |
| 341 | 39 | default: | |
| 342 | 39 | break; | |
| 343 | } | ||
| 344 | |||
| 345 | 506 | done: | |
| 346 | 506 | nft_print(octx, "%s", ct_templates[key].token); | |
| 347 | 506 | } | |
| 348 | |||
| 349 | 397 | static void ct_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 350 | { | ||
| 351 | 397 | ct_print(expr->ct.key, expr->ct.direction, expr->ct.nfproto, octx); | |
| 352 | 397 | } | |
| 353 | |||
| 354 | ✗ | static bool ct_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 355 | { | ||
| 356 | ✗ | if (e1->ct.key != e2->ct.key) | |
| 357 | ✗ | return false; | |
| 358 | |||
| 359 | ✗ | return e1->ct.direction == e2->ct.direction; | |
| 360 | } | ||
| 361 | |||
| 362 | 1176 | static void ct_expr_clone(struct expr *new, const struct expr *expr) | |
| 363 | { | ||
| 364 | 1176 | new->ct = expr->ct; | |
| 365 | 1176 | } | |
| 366 | |||
| 367 | 17 | static void ct_expr_pctx_update(struct proto_ctx *ctx, | |
| 368 | const struct location *loc, | ||
| 369 | const struct expr *left, | ||
| 370 | const struct expr *right) | ||
| 371 | { | ||
| 372 | 17 | const struct proto_desc *base = NULL, *desc; | |
| 373 | uint32_t nhproto; | ||
| 374 | |||
| 375 | 17 | nhproto = mpz_get_uint32(right->value); | |
| 376 | |||
| 377 | 17 | base = ctx->protocol[left->ct.base].desc; | |
| 378 |
2/2✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→5) taken 13 times.
|
17 | if (!base) |
| 379 | 4 | return; | |
| 380 | 13 | desc = proto_find_upper(base, nhproto); | |
| 381 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 13 times.
|
13 | if (!desc) |
| 382 | ✗ | return; | |
| 383 | |||
| 384 | 13 | proto_ctx_update(ctx, left->ct.base + 1, loc, desc); | |
| 385 | } | ||
| 386 | |||
| 387 | #define NFTNL_UDATA_CT_KEY 0 | ||
| 388 | #define NFTNL_UDATA_CT_DIR 1 | ||
| 389 | #define NFTNL_UDATA_CT_MAX 2 | ||
| 390 | |||
| 391 | 207 | static int ct_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 392 | const struct expr *expr) | ||
| 393 | { | ||
| 394 | 207 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_CT_KEY, expr->ct.key); | |
| 395 | 207 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_CT_DIR, expr->ct.direction); | |
| 396 | |||
| 397 | 207 | return 0; | |
| 398 | } | ||
| 399 | |||
| 400 | 956 | static int ct_parse_udata(const struct nftnl_udata *attr, void *data) | |
| 401 | { | ||
| 402 | 956 | const struct nftnl_udata **ud = data; | |
| 403 | 956 | uint8_t type = nftnl_udata_type(attr); | |
| 404 | 956 | uint8_t len = nftnl_udata_len(attr); | |
| 405 | |||
| 406 |
1/2✓ Branch 0 (4→5) taken 956 times.
✗ Branch 1 (4→8) not taken.
|
956 | switch (type) { |
| 407 | 956 | case NFTNL_UDATA_CT_KEY: | |
| 408 | case NFTNL_UDATA_CT_DIR: | ||
| 409 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 956 times.
|
956 | if (len != sizeof(uint32_t)) |
| 410 | ✗ | return -1; | |
| 411 | 956 | break; | |
| 412 | ✗ | default: | |
| 413 | ✗ | return 0; | |
| 414 | } | ||
| 415 | |||
| 416 | 956 | ud[type] = attr; | |
| 417 | 956 | return 0; | |
| 418 | } | ||
| 419 | |||
| 420 | 478 | static struct expr *ct_expr_parse_udata(const struct nftnl_udata *attr) | |
| 421 | { | ||
| 422 | 478 | const struct nftnl_udata *ud[NFTNL_UDATA_CT_MAX + 1] = {}; | |
| 423 | uint32_t key, dir; | ||
| 424 | int err; | ||
| 425 | |||
| 426 | 478 | err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr), | |
| 427 | ct_parse_udata, ud); | ||
| 428 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 478 times.
|
478 | if (err < 0) |
| 429 | ✗ | return NULL; | |
| 430 | |||
| 431 |
1/2✓ Branch 0 (7→8) taken 478 times.
✗ Branch 1 (7→9) not taken.
|
478 | if (!ud[NFTNL_UDATA_CT_KEY] || |
| 432 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 478 times.
|
478 | !ud[NFTNL_UDATA_CT_DIR]) |
| 433 | ✗ | return NULL; | |
| 434 | |||
| 435 | 478 | key = nftnl_udata_get_u32(ud[NFTNL_UDATA_CT_KEY]); | |
| 436 | 478 | dir = nftnl_udata_get_u32(ud[NFTNL_UDATA_CT_DIR]); | |
| 437 | |||
| 438 | 478 | return ct_expr_alloc(&internal_location, key, dir); | |
| 439 | } | ||
| 440 | |||
| 441 | const struct expr_ops ct_expr_ops = { | ||
| 442 | .type = EXPR_CT, | ||
| 443 | .name = "ct", | ||
| 444 | .print = ct_expr_print, | ||
| 445 | .json = ct_expr_json, | ||
| 446 | .cmp = ct_expr_cmp, | ||
| 447 | .clone = ct_expr_clone, | ||
| 448 | .pctx_update = ct_expr_pctx_update, | ||
| 449 | .parse_udata = ct_expr_parse_udata, | ||
| 450 | .build_udata = ct_expr_build_udata, | ||
| 451 | }; | ||
| 452 | |||
| 453 | 2331 | struct expr *ct_expr_alloc(const struct location *loc, enum nft_ct_keys key, | |
| 454 | int8_t direction) | ||
| 455 | { | ||
| 456 | 2331 | const struct ct_template *tmpl = &ct_templates[key]; | |
| 457 | struct expr *expr; | ||
| 458 | |||
| 459 | 2331 | expr = expr_alloc(loc, EXPR_CT, tmpl->dtype, | |
| 460 | 2331 | tmpl->byteorder, tmpl->len); | |
| 461 | 2331 | expr->ct.key = key; | |
| 462 | 2331 | expr->ct.direction = direction; | |
| 463 | |||
| 464 |
5/5✓ Branch 0 (3→4) taken 72 times.
✓ Branch 1 (3→5) taken 27 times.
✓ Branch 2 (3→6) taken 13 times.
✓ Branch 3 (3→7) taken 6 times.
✓ Branch 4 (3→8) taken 2213 times.
|
2331 | switch (key) { |
| 465 | 72 | case NFT_CT_SRC: | |
| 466 | case NFT_CT_SRC_IP: | ||
| 467 | case NFT_CT_SRC_IP6: | ||
| 468 | case NFT_CT_DST: | ||
| 469 | case NFT_CT_DST_IP: | ||
| 470 | case NFT_CT_DST_IP6: | ||
| 471 | 72 | expr->ct.base = PROTO_BASE_NETWORK_HDR; | |
| 472 | 72 | break; | |
| 473 | 27 | case NFT_CT_PROTO_SRC: | |
| 474 | case NFT_CT_PROTO_DST: | ||
| 475 | 27 | expr->ct.base = PROTO_BASE_TRANSPORT_HDR; | |
| 476 | 27 | break; | |
| 477 | 13 | case NFT_CT_PROTOCOL: | |
| 478 | 13 | expr->flags = EXPR_F_PROTOCOL; | |
| 479 | 13 | expr->ct.base = PROTO_BASE_NETWORK_HDR; | |
| 480 | 13 | break; | |
| 481 | 6 | case NFT_CT_L3PROTOCOL: | |
| 482 | 6 | expr->flags = EXPR_F_PROTOCOL; | |
| 483 | 6 | expr->ct.base = PROTO_BASE_LL_HDR; | |
| 484 | 6 | break; | |
| 485 | 2213 | default: | |
| 486 | 2213 | break; | |
| 487 | } | ||
| 488 | |||
| 489 | 2331 | return expr; | |
| 490 | } | ||
| 491 | |||
| 492 | 1692 | void ct_expr_update_type(struct proto_ctx *ctx, struct expr *expr) | |
| 493 | { | ||
| 494 | const struct proto_desc *desc; | ||
| 495 | |||
| 496 | 1692 | desc = ctx->protocol[expr->ct.base].desc; | |
| 497 | |||
| 498 |
5/5✓ Branch 0 (2→3) taken 5 times.
✓ Branch 1 (2→10) taken 21 times.
✓ Branch 2 (2→14) taken 51 times.
✓ Branch 3 (2→15) taken 4 times.
✓ Branch 4 (2→16) taken 1611 times.
|
1692 | switch (expr->ct.key) { |
| 499 | 5 | case NFT_CT_SRC: | |
| 500 | case NFT_CT_DST: | ||
| 501 |
2/2✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→6) taken 1 times.
|
5 | if (desc == &proto_ip) { |
| 502 | 4 | datatype_set(expr, &ipaddr_type); | |
| 503 | 4 | expr->ct.nfproto = NFPROTO_IPV4; | |
| 504 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 1 times.
|
1 | } else if (desc == &proto_ip6) { |
| 505 | ✗ | datatype_set(expr, &ip6addr_type); | |
| 506 | ✗ | expr->ct.nfproto = NFPROTO_IPV6; | |
| 507 | } | ||
| 508 | |||
| 509 | 5 | expr->len = expr->dtype->size; | |
| 510 | 5 | break; | |
| 511 | 21 | case NFT_CT_PROTO_SRC: | |
| 512 | case NFT_CT_PROTO_DST: | ||
| 513 |
2/2✓ Branch 0 (10→11) taken 7 times.
✓ Branch 1 (10→12) taken 14 times.
|
21 | if (desc == NULL) |
| 514 | 7 | break; | |
| 515 | 14 | datatype_set(expr, &inet_service_type); | |
| 516 | 14 | break; | |
| 517 | 51 | case NFT_CT_SRC_IP: | |
| 518 | case NFT_CT_DST_IP: | ||
| 519 | 51 | expr->dtype = &ipaddr_type; | |
| 520 | 51 | expr->len = expr->dtype->size; | |
| 521 | 51 | break; | |
| 522 | 4 | case NFT_CT_SRC_IP6: | |
| 523 | case NFT_CT_DST_IP6: | ||
| 524 | 4 | expr->dtype = &ip6addr_type; | |
| 525 | 4 | expr->len = expr->dtype->size; | |
| 526 | 4 | break; | |
| 527 | 1611 | default: | |
| 528 | 1611 | break; | |
| 529 | } | ||
| 530 | 1692 | } | |
| 531 | |||
| 532 | 109 | static void ct_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 533 | { | ||
| 534 | 109 | ct_print(stmt->ct.key, stmt->ct.direction, 0, octx); | |
| 535 | 109 | nft_print(octx, " set "); | |
| 536 | 109 | expr_print(stmt->ct.expr, octx); | |
| 537 | 109 | } | |
| 538 | |||
| 539 | 489 | static void ct_stmt_destroy(struct stmt *stmt) | |
| 540 | { | ||
| 541 | 489 | expr_free(stmt->ct.expr); | |
| 542 | 489 | } | |
| 543 | |||
| 544 | const struct stmt_ops ct_stmt_ops = { | ||
| 545 | .type = STMT_CT, | ||
| 546 | .name = "ct", | ||
| 547 | .print = ct_stmt_print, | ||
| 548 | .json = ct_stmt_json, | ||
| 549 | .destroy = ct_stmt_destroy, | ||
| 550 | }; | ||
| 551 | |||
| 552 | 489 | struct stmt *ct_stmt_alloc(const struct location *loc, enum nft_ct_keys key, | |
| 553 | int8_t direction, struct expr *expr) | ||
| 554 | { | ||
| 555 | struct stmt *stmt; | ||
| 556 | |||
| 557 | 489 | stmt = stmt_alloc(loc, &ct_stmt_ops); | |
| 558 | 489 | stmt->ct.key = key; | |
| 559 | 489 | stmt->ct.tmpl = &ct_templates[key]; | |
| 560 | 489 | stmt->ct.expr = expr; | |
| 561 | 489 | stmt->ct.direction = direction; | |
| 562 | |||
| 563 | 489 | return stmt; | |
| 564 | } | ||
| 565 | |||
| 566 | 3 | static void notrack_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 567 | { | ||
| 568 | 3 | nft_print(octx, "notrack"); | |
| 569 | 3 | } | |
| 570 | |||
| 571 | const struct stmt_ops notrack_stmt_ops = { | ||
| 572 | .type = STMT_NOTRACK, | ||
| 573 | .name = "notrack", | ||
| 574 | .print = notrack_stmt_print, | ||
| 575 | .json = notrack_stmt_json, | ||
| 576 | }; | ||
| 577 | |||
| 578 | 13 | struct stmt *notrack_stmt_alloc(const struct location *loc) | |
| 579 | { | ||
| 580 | 13 | return stmt_alloc(loc, ¬rack_stmt_ops); | |
| 581 | } | ||
| 582 | |||
| 583 | 3 | static void flow_offload_stmt_print(const struct stmt *stmt, | |
| 584 | struct output_ctx *octx) | ||
| 585 | { | ||
| 586 | 3 | nft_print(octx, "flow add @%s", stmt->flow.table_name); | |
| 587 | 3 | } | |
| 588 | |||
| 589 | 24 | static void flow_offload_stmt_destroy(struct stmt *stmt) | |
| 590 | { | ||
| 591 | 24 | free_const(stmt->flow.table_name); | |
| 592 | 24 | } | |
| 593 | |||
| 594 | const struct stmt_ops flow_offload_stmt_ops = { | ||
| 595 | .type = STMT_FLOW_OFFLOAD, | ||
| 596 | .name = "flow_offload", | ||
| 597 | .print = flow_offload_stmt_print, | ||
| 598 | .destroy = flow_offload_stmt_destroy, | ||
| 599 | .json = flow_offload_stmt_json, | ||
| 600 | }; | ||
| 601 | |||
| 602 | 24 | struct stmt *flow_offload_stmt_alloc(const struct location *loc, | |
| 603 | const char *table_name) | ||
| 604 | { | ||
| 605 | struct stmt *stmt; | ||
| 606 | |||
| 607 | 24 | stmt = stmt_alloc(loc, &flow_offload_stmt_ops); | |
| 608 | 24 | stmt->flow.table_name = table_name; | |
| 609 | |||
| 610 | 24 | return stmt; | |
| 611 | } | ||
| 612 |